On Monday, April 18, 2016 at 7:45:00 PM UTC-7, Ian Harvey wrote:
> On 2016-04-19 11:30 AM, 博陈 wrote:
> > In my fortran program, I just saved a large array, up = Array(Complex128, 8192, 8192), in this way:
> > open(100 , file = 'data/p.bin', access = 'direct' , form = 'unformatted' , recl = 4)
> > do j=1, ns
> > do i=1, ns
> > write(100, rec = ns*(j-1)+i ) up(i, j)
> > end do
> > end do
> > close(100)
(snip)
> Is there a particular need to write to a direct access file? Is there a
> particular need to save each value in the array to its own record?
Yes. There is overhead for each I/O statement, and for each direct access
file operation. Even more, there might be less buffering for direct access.
> Depending on requirements, I would consider writing the whole array to a
> single record, perhaps using stream access.
I wouldn't go quite that far. Depending on the array size, I might
do it one row at a time. Writing a whole large array, or even more,
many large arrays, as one unformatted WRITE may (there are system
dependencies) require some large buffers that might slow things down.
It used to be that I liked to keep each I/O operation below about 32K,
but computers have gotten bigger and faster. At 32K, you have most
of the speed advantage of larger blocks, but could go to 128K or
even 1M.
If you don't need direct access, I would use ordinary sequential access,
which has less overhead, and often makes more optimal use of buffers.
Some I/O systems can do read ahead, anticipating reads. That is
rare for direct access, where the suggestion is that you won't do
sequential access.
open(100 , file = 'data/p.bin', access = 'sequential' , form = 'unformatted' )
do j=1, ns
write(100 ) up(:, j)
end do
close(100)
> (Note that the absolute specification of record length will not be
> portable between Fortran processors. Consider using an INQUIRE
> statement to obtain the number of file storage units required for the
> thing you are writing.)
If you really do need direct access, try to find a larger block.
It used to be that disks used 512 byte blocks, and that allowed
for optimal access. That is less true today, but it isn't a bad size
for direct access. 2K, 4K, or 8K are also often not bad.