Fortran流访问的效率与MPI-IO

Fortran流访问的效率与MPI-IO,io,fortran,mpi,mpi-io,Io,Fortran,Mpi,Mpi Io,我有一段并行代码,在代码中,我以块的形式写出n个大数组(表示一个数值网格),然后以不同大小的块读取。为了做到这一点,我使用了流访问,因此每个处理器都独立地写入它们的块,但在本节中,我看到使用两个处理器组测试时,不一致的计时时间为0.5-4秒 我知道你可以用MPI-IO做一些类似的事情,但我不确定有什么好处,因为不需要同步。我想知道是否有办法提高我的写操作的性能,或者是否有理由认为MPI-IO是本节的更好选择 下面是代码部分的一个示例,我在其中创建文件,以使用两个组(mygroup=0或1)写入n

我有一段并行代码,在代码中,我以块的形式写出n个大数组(表示一个数值网格),然后以不同大小的块读取。为了做到这一点,我使用了流访问,因此每个处理器都独立地写入它们的块,但在本节中,我看到使用两个处理器组测试时,不一致的计时时间为0.5-4秒

我知道你可以用MPI-IO做一些类似的事情,但我不确定有什么好处,因为不需要同步。我想知道是否有办法提高我的写操作的性能,或者是否有理由认为MPI-IO是本节的更好选择

下面是代码部分的一个示例,我在其中创建文件,以使用两个组(
mygroup
=0或1)写入
norb
数组:

do irbsic=1,norb
  [various operations]

  blocksize=int(nmsh_tot/ngroups)
  OPEN(unit=iunit,FILE='ZPOT',STATUS='UNKNOWN',ACCESS='STREAM')
  mypos = 1 + (IRBSIC-1)*nmsh_tot*8     ! starting point for writing IRBSIC
  mypos = mypos + mygroup*(8*blocksize) ! starting point for mesh group
  WRITE(iunit,POS=mypos) POT(1:nmsh)  
  CLOSE(iunit)

  OPEN(unit=iunit,FILE='RHOI',STATUS='UNKNOWN',ACCESS='STREAM')
  mypos = 1 + (IRBSIC-1)*nmsh_tot*8     ! starting point for writing IRBSIC
  mypos = mypos + mygroup*(8*blocksize) ! starting point for mesh group
  WRITE(iunit,POS=mypos) RHOG(1:nmsh,1,1)
  CLOSE(iunit)

  [various operations]
end do
(如评论中所述)我强烈建议不要为此使用Fortran流访问。标准Fortran I/O只有在文件由单个进程访问时才能保证工作,在我自己的工作中,我看到多个进程试图同时写入文件时,文件会随机损坏,即使这些进程正在写入文件的不同部分e、 MPI-I/O或使用MPI-I/O的库(如HDF5或NetCDF)是实现这一点的唯一合理方法。下面是一个简单的程序,说明如何使用
MPI文件写入

ian@eris:~/work/stack$ cat at.f90
Program write_at

  Use mpi

  Implicit None

  Integer, Parameter :: n = 4

  Real, Dimension( 1:n ) :: a

  Real, Dimension( : ), Allocatable :: all_of_a
  
  Integer :: me, nproc
  Integer :: handle
  Integer :: i
  Integer :: error
  
  ! Set up MPI
  Call mpi_init( error )
  Call mpi_comm_size( mpi_comm_world, nproc, error )
  Call mpi_comm_rank( mpi_comm_world, me   , error )

  ! Provide some data
  a = [ ( i, i = n * me, n * ( me + 1 ) - 1 ) ]

  ! Open the file
  Call mpi_file_open( mpi_comm_world, 'stuff.dat', &
       mpi_mode_create + mpi_mode_wronly, mpi_info_null, handle, error )

  ! Describe how the processes will view the file - in this case
  ! simply a stream of mpi_real
  Call mpi_file_set_view( handle, 0_mpi_offset_kind, &
       mpi_real, mpi_real, 'native', &
       mpi_info_null, error )

  ! Write the data using a collective routine - generally the most efficent
  ! but as collective all processes within the communicator must call the routine
  Call mpi_file_write_at_all( handle, Int( me * n,mpi_offset_kind ) , &
       a, Size( a ), mpi_real, mpi_status_ignore, error )

  ! Close the file
  Call mpi_file_close( handle, error )

  ! Read the file on rank zero using Fortran to check the data
  If( me == 0 ) Then
     Open( 10, file = 'stuff.dat', access = 'stream' )
     Allocate( all_of_a( 1:n * nproc ) )
     Read( 10, pos = 1 ) all_of_a
     Write( *, * ) all_of_a
  End If

  ! Shut down MPI
  Call mpi_finalize( error )
  
End Program write_at
ian@eris:~/work/stack$ mpif90 --version
GNU Fortran (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

ian@eris:~/work/stack$ mpif90 -Wall -Wextra -fcheck=all -std=f2008 at.f90 
ian@eris:~/work/stack$ mpirun -np 2 ./a.out 
   0.00000000       1.00000000       2.00000000       3.00000000       4.00000000       5.00000000       6.00000000       7.00000000    
ian@eris:~/work/stack$ mpirun -np 5 ./a.out 
   0.00000000       1.00000000       2.00000000       3.00000000       4.00000000       5.00000000       6.00000000       7.00000000       8.00000000       9.00000000       10.0000000       11.0000000       12.0000000       13.0000000       14.0000000       15.0000000       16.0000000       17.0000000       18.0000000       19.0000000    
ian@eris:~/work/stack$ 

是否有多个进程写入一个给定文件?如果是这样,我强烈建议MPI I/O-如果不这样做,可能会得到错误的结果,这是我遇到的一个严重问题如果您写入不同的文件,这意味着您有不同的单元号,那么您可以使用
ASYNCHRONOUS=“YES”
。您的程序不会等待IO完成,因为它已经将IO交给了操作系统,您现在受到文件系统的约束。顺便问一下,为什么要计算
mypos
两次?而且,
IRBSIC
是否假设为do循环索引
iorbsrc
?@IanBush是的,多个进程写入一个文件,但每个进程都是写入的g文件的不同部分。打开同一个文件是否仍然存在冲突?如果一个文件中写入了多个进程,则Fortran I/O不能保证工作-这不仅仅是理论上的标准冲突,我已经看到,无法生成部分填充了不可读值的文件。引用Cray工程师的话“多个进程写入文件的唯一合理、可移植的方法是通过MPI I/O”感谢您的解释。这似乎是我现在要走的路线。有一个问题,MPI_file_set_view是阻塞操作吗?对于我来说,进程将在不同的时间到达。我看到有非阻塞版本的写入(mpi_file_iwrite_at),但我不知道如何处理set_视图。我可以在“irbsic”循环外打开和关闭文件,但看起来mpi_file_set_视图需要偏移量,因此必须在循环内。mpi标准第13.3节中的“mpi_file_set_视图”是集体的".mpi_file_open和mpi_file_close也都是集体例程。但我不认为这是一个大问题,考虑到上面的内容-只需使用0作为所有进程的偏移量,在文件中使用全局偏移量,然后在主计算之外打开、设置视图并关闭一次,所有这些都应该没问题,如果您对ans满意的话,我也理解我们要做正确的标记——我追求的不仅仅是声誉,它表明你不希望别人对它有太多的看法