https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128796
Bug ID: 128796
Summary: Unformatted I/O of a multi-dimensional array is done
one column or one element at a time
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libfortran
Assignee: unassigned at gcc dot gnu.org
Reporter: suvarchal.cheedela at awi dot de
Target Milestone: ---
Created attachment 66190
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66190&action=edit
reproducer
mtransfer_array_inner (libgfortran/io/transfer.c) passes an array to the
transfer routine in pieces that are contiguous along the first
dimension only, one call per piece:
- a contiguous array of rank 2 or more goes one column at a time;
- an array whose first extent is 1, such as B(1,N), goes one element
at a time, although it is contiguous;
- a section whose first dimension is strided, such as UV(1,:,:) of a
UV(2,M,N) array, goes one element at a time.
Each piece is a separate call and is copied through the unit buffer.
The attached program writes and reads the same 8 MB of REAL(8) in five
layouts (unformatted sequential, file in the page cache, one core,
-O2). Milliseconds per statement, WRITE / READ, median of 5 runs:
15.2.0 trunk (7647c00b77e)
V(N) 1.38 / 0.63 1.32 / 0.60
A(64,N/64) 1.73 / 0.91 1.71 / 0.88
B(1,N) 9.32 / 8.28 8.41 / 7.84
U(1,:,:) of U(1,64,N/64) 1.75 / 0.90 1.72 / 0.87
UV(1,:,:) of UV(2,64,N/64) 8.68 / 8.11 7.51 / 7.23
N = 1048576, x86_64 (i9-13950HX), Ubuntu gfortran 15.2.0-4ubuntu4
and trunk built with --disable-bootstrap.
B(1,N) holds the same contiguous data as V(N) but takes 6-7 times as
long to write and 13 times as long to read. ifx 2025.3 and nvfortran
25.11 write and read B(1,N) as fast as V(N).
The layouts are not artificial. The FESOM2 ocean model maps its 2-D
fields as PTR(1:1,1:N) => FIELD(:) and stores the two velocity
components interleaved, writing U and V to its restart files as
UV(1,:,:) and UV(2,:,:). In a traced 2-rank run the UV sections alone
took about half of the time of each restart.
With ASYNCHRONOUS='YES' the same loop runs on the I/O thread
(AIO_TRANSFER_ARRAY in async.c), so the cost moves there but remains.
Pointer arrays are scalarized by the front end before the library sees
them (PR128722), so they are slower still.
Two changes would fix this:
1. Merge the leading dimensions that are contiguous in memory into one
piece. This fixes A, B and U: with it all three take 1.28-1.30 /
0.56-0.57 ms, the same as V. I have a patch for this (about 20
lines in transfer_array_inner, no regressions in check-fortran on
x86_64-pc-linux-gnu) and will post it to gcc-patches, if confirmed.
2. For a strided section, gather the elements into a staging buffer in
one loop and transfer the buffer as one piece (scatter on READ),
for unformatted transfers without byte-order conversion. This
would fix UV(1,:,:). I plan to work on it as a follow-up.
Reproducer: io_shapes.f90 (attached).