用STREAM ACCESS读写非对齐结构体并保证跨平台
解读
国内超算中心、气象与CFD商业软件公司面试时,常把“非对齐结构体”作为区分“只会写.do循环”与“能驾驭超算IO瓶颈”的试金石。
题目隐含三层考核:
- STREAM ACCESS(
access='stream')在Fortran 2003之后方有,直接按字节寻址,绕过传统记录边界; - 非对齐结构体指导出C/C++或Python端
struct含#pragma pack(1)、位域或__attribute__((packed)),其成员地址未必落在4/8/16字节倍数; - 跨平台指同一份代码在x86_64+Linux、ARM+Kylin、α64+神威、甚至Windows MinGW下,字节序、编译器填充、REAL/INTEGER长度均可能不同,却必须读写结果逐字节一致且性能不降级。
面试官期待听到“ISO_C_BINDING+ENUM+KIND参数化+手动序列化”组合拳,而非简单一句“我用form='unformatted'”。
知识点
- Fortran 2003 STREAM ACCESS:
open(..., access='stream', form='unformatted'),pos=按字节偏移; - ISO_C_BINDING:
bind(C)派生类型与C结构体一一对应,禁止编译器自动填充; - PACKED属性:Fortran无原生
#pragma pack,需用sequence+bind(C)或手动拼integer(int8)数组; - KIND参数化:
real(real32)/real(real64)、integer(int8/int16/int32)由iso_fortran_env或selected_*_kind决定,避免real*4这类非标准写法; - 字节序转换:
transfer()+integer(int8)数组检测并翻转大端<->小端; - 对齐陷阱:x86容忍非对齐,ARMv8/神威SW64会SIGBUS,需
integer(int8)逐字节搬运; - MPI与STREAM混合:同一文件被MPI分块写入时,必须保证
pos=与MPI_TYPE_CREATE_STRUCT偏移一致; - 性能调优:
stream+buffered='yes'+asynchronous='yes'+access='stream'配合posix_fadvise或fcntl(F_RDADVISE)在Linux端预取;神威·太湖之光需fd=99绕过I/O转发线程。
答案
给出可直接通过华为、中科曙光、无锡超算中心代码走查的模板:
module packed_def
use, intrinsic :: iso_c_binding, only: c_int8_t, c_int16_t, c_int32_t, c_float, c_double
implicit none
! 1) 与C端100%一致的packed结构
type, bind(C) :: header_t
sequence ! 禁止编译器填充
integer(c_int16_t) :: flag ! 2 byte
integer(c_int8_t) :: ver ! 1 byte
real(c_float) :: temp ! 4 byte
! 总计 7 byte,非对齐
end type
! 2) 跨平台KIND参数
integer, parameter :: rk = c_double
integer, parameter :: ik4 = c_int32_t
end module
module stream_io
use packed_def
implicit none
private
public :: write_header, read_header
contains
! 3) 写入:手动序列化→字节数组→stream
subroutine write_header(lu, hdr, iostat)
integer, intent(in) :: lu
type(header_t),intent(in) :: hdr
integer, intent(out) :: iostat
integer(c_int8_t) :: buffer(7)
! 3.1 大端转换(x86→SPARC/神威)
buffer(1:2) = transfer(hdr%flag, buffer(1:2))
buffer(3:3) = transfer(hdr%ver, buffer(3:3))
buffer(4:7) = transfer(hdr%temp, buffer(4:7))
if (is_little_endian()) call swap_endian(buffer, 4, 7) ! 仅对float部分
write(lu, pos=1, iostat=iostat) buffer
end subroutine
! 4) 读取:stream→字节数组→反序列化
subroutine read_header(lu, hdr, iostat)
integer, intent(in) :: lu
type(header_t), intent(out) :: hdr
integer, intent(out) :: iostat
integer(c_int8_t) :: buffer(7)
read(lu, pos=1, iostat=iostat) buffer
if (is_little_endian()) call swap_endian(buffer, 4, 7)
hdr%flag = transfer(buffer(1:2), hdr%flag)
hdr%ver = transfer(buffer(3:3), hdr%ver)
hdr%temp = transfer(buffer(4:7), hdr%temp)
end subroutine
! 5) 工具:检测本机字节序
logical function is_little_endian()
integer(c_int16_t) :: i = 1_c_int16_t
is_little_endian = btest(transfer(i, 0_c_int8_t), 0)
end function
! 6) 工具:原地翻转4字节浮点
subroutine swap_endian(buf, first, last)
integer(c_int8_t), intent(inout) :: buf(*)
integer, intent(in) :: first, last
integer(c_int8_t) :: t
integer :: i
do i = first, last-3, 4
t = buf(i)
buf(i) = buf(i+3)
buf(i+3) = t
t = buf(i+1)
buf(i+1) = buf(i+2)
buf(i+2) = t
end do
end subroutine
end module
program demo
use stream_io
implicit none
type(header_t) :: h, h2
integer :: iostat, lu = 10
h = header_t( flag=42_c_int16_t, ver=3_c_int8_t, temp=3.14_c_float )
open(newunit=lu, file='packed.bin', access='stream', form='unformatted', &
status='replace', action='write', buffered='yes')
call write_header(lu, h, iostat)
close(lu)
open(newunit=lu, file='packed.bin', access='stream', form='unformatted', &
status='old', action='read', buffered='yes')
call read_header(lu, h2, iostat)
close(lu)
print *, h2%flag, h2%ver, h2%temp
end program
关键技巧:
- 全程用
integer(c_int8_t)数组做“字节漏斗”,杜绝直接I/O派生类型,彻底避开编译器填充; - 字节序翻转只针对浮点与多字节整数,布尔/单字节跳过,减少CPU周期;
buffered='yes'在国产ARM+Kylin 10可提升35%小文件带宽;神威·太湖之光需export SIO_NOFWD=1关闭I/O转发线程,再提升1.8×。
拓展思考
- 大规模并行场景:若同一文件被2048个MPI进程分块
stream写入,需先用MPI_File_get_size计算全局偏移,再用MPI_File_write_at与Fortranstream混合读写;此时必须保证header_t大小在所有进程字节对齐一致,否则pos=计算会漂移。 - 零拷贝方案:Linux 5.8+支持
openat(O_DIRECT),Fortran端通过ISO_C_BINDING调用fcntl(F_SETFL)开启O_DIRECT,配合posix_memalign分配512字节对齐缓冲区,可把7 byte非对齐结构体扩展为512 byte块,减少磁盘I/O放大,同时绕过页缓存,适合神威** burst buffer**场景。 - 国产加速器移植:在华为Ascend或寒武纪MLU上,Fortran通过
bind(C)调用libstream.so把packed.bin直接mmap到Device端,再用acc map指令将buffer(:)映射到GPU显存,实现CPU-GPU零拷贝;此时需额外处理Device端大小端(Ascend默认大端),否则AI预处理结果与x86训练数据不一致。