当然; 您在本地元素数量上使用MPI_Exscan
withMPI_SUM
来获取您“左侧”的总数,并在您的视图中使用它(作为偏移量,或在您创建的类型中定义您的视图)。
这是一个小的 Fortran 程序,其中每个等级生成一个“随机”(嗯,2*rank+1
)大小的字符数组(等级 0 的“0”等),使用 MPI_Exscan 找出它在写入时应该使用的偏移量,然后写入:
program testexscan
use mpi
implicit none
integer :: nelements, nleft, total
character, allocatable, dimension(:) :: array
integer :: rank, nprocs, ierr, fh
integer(kind=MPI_OFFSET_KIND) :: disp
call MPI_Init(ierr)
call MPI_Comm_rank(MPI_COMM_WORLD, rank, ierr)
call MPI_Comm_rank(MPI_COMM_WORLD, nprocs, ierr)
call generatedata(rank, array)
nelements = size(array)
call MPI_Exscan (nelements, nleft, 1, MPI_INTEGER, MPI_SUM, MPI_COMM_WORLD, ierr)
print *, 'rank = ', rank, ' nelements = ', nelements, ' nleft= ', nleft
call MPI_File_open(MPI_COMM_WORLD, 'output.txt', ior(MPI_MODE_WRONLY,MPI_MODE_CREATE), &
MPI_INFO_NULL, fh, ierr)
disp = nleft * 1 ! offset in bytes
call MPI_File_set_view(fh, disp, MPI_CHARACTER, MPI_CHARACTER, "native", MPI_INFO_NULL, ierr )
call MPI_File_write_all(fh, array, nelements, MPI_CHARACTER, MPI_STATUS_IGNORE, ierr)
call MPI_File_close(fh, ierr)
deallocate(array)
call MPI_Finalize(ierr)
contains
subroutine generatedata(rank, arr)
character, dimension(:), allocatable, intent(inout) :: arr
integer, intent(in) :: rank
nelements = rank * 2 + 1
allocate(array(nelements))
array = char(ichar("0")+rank)
end subroutine generatedata
end program testexscan
运行这个给出:
$ mpif90 -o testexscan testexscan.f90
$ mpirun -np 4 ./testexscan
rank = 0 nelements = 1 nleft= 0
rank = 1 nelements = 3 nleft= 1
rank = 2 nelements = 5 nleft= 4
rank = 3 nelements = 7 nleft= 9
$ cat output.txt
0111222223333333