With nvfortran, an unformatted stream WRITE of a contiguous array section is passed to the OS in 8 KiB write() calls. Writing a whole array of the same size goes out in one or two calls. On a fast filesystem this makes section writes 2–3× slower.
Reproducer (128 writes of 4 MiB each, 512 MiB in total):
program iotest5
! mode 1 writes array sections a(:,:,lo:hi), mode 2 copies each chunk into
! the whole array b first, so no section appears in the write statement.
implicit none
integer,parameter::nx=8,ny=6,nchunk=10922,nwrite=128
real(kind=8),allocatable::a(:,:,:),b(:,:,:)
integer::k,mode
integer(kind=8)::t0,t1,rate
character(len=8)::arg
mode=1
if(command_argument_count()>=1)then
call get_command_argument(1,arg); read(arg,*)mode
endif
allocate(a(nx,ny,nchunk*nwrite),b(nx,ny,nchunk))
call random_number(a)
open(10,file='iotest.bin',access='stream',form='unformatted',status='replace')
call system_clock(t0,rate)
do k=1,nwrite
if(mode==1)then
write(10)a(:,:,(k-1)*nchunk+1:k*nchunk)
else
b=a(:,:,(k-1)*nchunk+1:k*nchunk)
write(10)b
endif
end do
close(10)
call system_clock(t1)
write(\*,'(a,i1,a,f8.3,a)')' mode ',mode,': wrote 512 MiB in ',dble(t1-t0)/dble(rate),' s'
end program iotest5
nvfortran -O4 iotest5.f90 -o iotest5
strace -f -c -e trace=write ./iotest5 1
strace -f -c -e trace=write ./iotest5 2
Results (nvfortran 26.5, x86-64 Linux, local scratch filesystem):
┌──────────────────────────────┬───────────────┬─────────┬───────────┐
│ │ write() calls │ time │ speed │
├──────────────────────────────┼───────────────┼─────────┼───────────┤
│ mode 1, section a(:,:,lo:hi) │ 65,533 │ 0.358 s │ 1.43 GB/s │
├──────────────────────────────┼───────────────┼─────────┼───────────┤
│ mode 2, whole array b │ 256 │ 0.162 s │ 3.16 GB/s │
└──────────────────────────────┴───────────────┴─────────┴───────────┘
A single write(10) a of the full 512 MiB array takes 3 calls and runs at 4.96 GB/s. Adding action=“write” and/or positioning with write(10, POS=…) doesn’t change the behaviour. We see the same 8 KiB splitting with nvhpc 25.1 and 26.3 on another cluster.
Expected: a section that is contiguous in memory should be written like a whole array, without going through an 8 KiB staging buffer. More generally, large transfers should not be cut into buffer-sized pieces.
Impact: we hit this in an astrophysics simulation code (RAMSES) that writes its outputs as array sections. We now work around it by writing with POSIX pwrite, but any Fortran code writing large stream files from array sections is affected.