Fortran I/O:指定大记录大小
我正在尝试将一个数组写入文件,我以这种方式打开文件:
open(unit=20, FILE="output.txt", form='unformatted', access='direct', recl=sizeof(u))
这里,u
是一个数组,sizeof(u)
是2730025920,这是〜 2.5GB。 当我运行程序时,我收到错误Fortran运行时错误:RECL参数在OPEN语句中为非正数
,我认为这意味着记录大小太大。
有办法处理这个问题吗?一种选择是在多个写入调用中写入数组,以便每次写入的记录大小小于 2.5GB。但我想知道是否可以在一次调用中写入整个数组。
编辑: u
已声明为双精度 u(5,0:408,0:408,0:407)
该程序编译为gfortran -O3 -fopenmp -mcmodel=medium test.f
该程序中有一些 OpenMP 代码,但文件 I/O 是顺序的。
gfortran v 4.5.0,操作系统:64 位 AMD Opteron 上的 Opensuse 11.3
感谢您的帮助。
I am trying to write an array to file, where I have opened the file this way:
open(unit=20, FILE="output.txt", form='unformatted', access='direct', recl=sizeof(u))
Here, u
is an array and sizeof(u)
is 2730025920, which is ~2.5GB.
When I run the program, I get an error Fortran runtime error: RECL parameter is non-positive in OPEN statement
, which I believe means that the record size is too large.
Is there a way to handle this? One option would be to write the array in more than one write call such that the record size in each write is smaller than 2.5GB. But I am wondering if I can write the entire array in a single call.
Edit:u
has been declared as double precision u(5,0:408,0:408,0:407)
The program was compiled as gfortran -O3 -fopenmp -mcmodel=medium test.f
There is some OpenMP code in this program, but the file I/O is sequential.
gfortran v 4.5.0, OS: Opensuse 11.3 on 64 bit AMD Opteron
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只要内存允许,您应该能够编写大数组。看起来您正在使用
sizeof
函数进行整数溢出。sizeof
不是 Fortran 标准,我不建议使用它(编译器之间的实现可能有所不同)。相反,更好的做法是使用inquire
语句来获取记录长度。我能够用 ifort 重现你的问题,这个解决方案对我有用。您可以通过声明更高种类的变量来避免整数溢出:编辑:经过一番调查,这似乎是一个 gfortran 问题。为整数
reclen
设置更高的类型可以解决 ifort 和 pgf90 的问题,但不能解决 gfortran 的问题 - 我刚刚在 4.6.2 版本中尝试过此操作。尽管reclen
具有正确的正值,但recl
似乎是 gfortran 内部的 32 位有符号整数(感谢 @MSB 指出了这一点)。 Fortran 运行时错误表明了这一点,而不是表明该值大于最大值。我怀疑这是操作系统问题。如果可能,请尝试使用 ifort(免费用于非商业用途):英特尔非商业软件下载。You should be able to write big arrays as long as it's memory permitting. It seems like you are getting integer overflow with the
sizeof
function.sizeof
is not Fortran standard and I would not recommend using it (implementations may vary between compilers). Instead, it is a better practice to use theinquire
statement to obtain record length. I was able to reproduce your problem with ifort and this solution works for me. You can avoid integer overflow by declaring a higher kind variable:EDIT: After some investigation, this seems to be a gfortran problem. Setting a higher kind for integer
reclen
solves the problem for ifort and pgf90, but not for gfortran - I just tried this with version 4.6.2. Even thoughreclen
has the correct positive value, it seems thatrecl
is 32-bit signed integer internally with gfortran (Thanks @M.S.B. for pointing this out). The Fortran run-time error suggests this, and not that the value is larger than maximum. I doubt it is an OS issue. If possible, try using ifort (free for non-commercial use): Intel Non-Commercial Software Download.