[Fortran] 纯文本查看 复制代码
! example of parallel MPI write into a single file ,in fortran 独立文件指针
PROGRAM main
include 'mpif.h'
integer ierr, i, BUFSIZE, thefile,intsize,realsize
integer(kind=MPI_OFFSET_KIND) disp
character * (MPI_MAX_PROCESSOR_NAME) processor_name
integer myrank, numprocs, namelen, rc
call MPI_INIT(ierr)!mpi初始化
call MPI_COMM_RANK(MPI_COMM_WORLD, myrank, ierr)
call MPI_COMM_SIZE( MPI_COMM_WORLD, numprocs, ierr )
call MPI_GET_PROCESSOR_NAME(processor_name, namelen, ierr)
write(*,10) myrank,numprocs,processor_name
10 FORMAT('Hello World! Process ',I2,' of ',I1,' on ', 20A)
!两个测试变量
It=271
RR=2.5811919e+3
if(myrank==0)then
Write( * , 150 ) It,RR
150 FORMAT('input int number =',I,' input real number=',e15.8)
end if
!打开一个二进制文件
call MPI_FILE_OPEN(MPI_COMM_WORLD, 'testfile',
& MPI_MODE_WRONLY+MPI_MODE_CREATE, MPI_INFO_NULL, thefile, ierr)
call MPI_TYPE_SIZE(MPI_INTEGER, intsize, ierr) !整型变量
call MPI_TYPE_SIZE(MPI_real, realsize, ierr) !浮点数变量
if (myrank==0) then
write(*,100) intsize,realsize
100 FORMAT('Bytes of MPI_INTEGER=',I2,' Bytes of MPI_real= 'I2)
end if
! 计算各个进程之间的绝对偏移量
disp = myrank*(intsize+realsize)
!设置绝对偏移量
call MPI_FILE_SET_VIEW(thefile, disp, MPI_INTEGER, MPI_INTEGER,
& 'native', MPI_INFO_NULL, ierr)
!写入第一个数据
call MPI_FILE_WRITE(thefile, It, 1,MPI_INTEGER,
& MPI_STATUS_IGNORE, ierr)
!写入第二个数据
call MPI_FILE_WRITE(thefile, RR, 1,MPI_real,
& MPI_STATUS_IGNORE, ierr)
! 关闭文件
call MPI_FILE_CLOSE(thefile, ierr)
! ****读二进制文件testfile并存为testfile.txt---------------------------------------------------------------------------
if (myrank==0) then
!打开二进制文件
Open( 12 , File = 'testfile' , Access = 'Direct' ,
& Form = 'Unformatted' , RecL = 2 )!recl=2表示单次可以读或写4*2个字节长度的容量
!打开文本文件testfile.txt
Open( 13 , File = 'testfile.txt',form='formatted')
! 将第i模块rec=i(单个模块的长度recl=2为8个字节),对应的数据一个整型变量和一个浮点型变量
! 读入并存储到变量IIT和RRT中
do i = 1, numprocs
Read( 12 , Rec = i ) IIT,RRT
Write( * , 110 ) IIT,RRT
110 FORMAT('test int number = ',I,' test real number = ',e15.8)
! 将读取的文件输入到txt文件
Write(13 , 180 ) IIT,RRT
180 FORMAT(I,e15.8)
enddo
close(12)
close(13)
end if
call MPI_FINALIZE(ierr)
END PROGRAM main