【问题标题】:Using MPI_GATHERV in Fortran when gathering unequal sized arrays收集大小不等的数组时在 Fortran 中使用 MPI_GATHERV
【发布时间】:2016-10-29 16:05:22
【问题描述】:

这个问题与this one 类似,复杂之处在于被收集的矩阵的大小在行长上不相等,但在列长上相等。 (我也看过 this question 和 this one 但无法弄清楚)。

背景

我正在执行一个计算,直到计算结束我才知道结果矩阵的行数。连续地,我分配了一个非常大的矩阵,该矩阵被填满,并且在计算结束时(当我知道行数的限制时)我切断了这个大数组的末尾,得到了我想要的结果。使用 MPI,我应用相同的逻辑:

  1. 在每个进程中,我都有一个被填满的大数组
  2. 在计算结束时,我需要在每个数组各自的限制下截断(每个进程的限制不同)
  3. 然后我需要将每个进程的结果数组收集到一个中,然后将其提供给根进程,以便继续执行程序的其余部分。

目前的尝试

为了了解 MPI_GATHERV 的工作原理以及在我的情况下如何使用它,我编辑了this question 答案中给出的代码,以便从每个进程中接受可变大小的数组。

program main
use mpi
implicit none
integer :: ierr, myRank, nProcs
integer :: sendsubarray, recvsubarray, resizedrecvsubarray
integer, dimension(2) :: starts,sizes,subsizes
integer, dimension(:), allocatable :: counts, disps
integer, parameter :: nx_glb=20, ny_glb=5, ny=5
integer :: nx
integer, dimension(:), allocatable :: nx_all  
character, dimension(:,:), target, allocatable :: mat, matG
character :: c
integer :: i, j
integer(kind=mpi_address_kind) :: start, extent

call mpi_init(ierr)
call mpi_comm_rank(mpi_comm_world, myRank, ierr)
call mpi_comm_size(mpi_comm_world, nProcs, ierr)
allocate(nx_all(nProcs))
nx_all = (/5, 4, 5, 5/)
nx = nx_all(myRank+1)  
sizes(1)=nx; sizes(2)=ny
subsizes(1)=nx; subsizes(2)=ny
starts(1)=0; starts(2)=0
call mpi_type_create_subarray(2, sizes, subsizes, starts,    mpi_order_fortran, &
                            mpi_character, sendsubarray, ierr)
call mpi_type_commit(sendsubarray,ierr)
allocate(mat(1:nx,1:ny))
mat='.'
forall (i=1:nx,j=1:ny) mat(i,j)=ACHAR(ICHAR('0')+myRank)

if(myRank.eq.0) then
 allocate(matG(nx_glb,ny_glb))
 matG='.'
 sizes(1)=nx_glb; sizes(2)=ny_glb
 subsizes(1)=nx; subsizes(2)=ny
 starts(1)=0; starts(2)=0
 call mpi_type_create_subarray(2, sizes, subsizes, starts, mpi_order_fortran, &
                               mpi_character, recvsubarray, ierr)
 call mpi_type_commit(recvsubarray, ierr)
 extent = sizeof(c)
 start = 0
 call mpi_type_create_resized(recvsubarray, start, extent, resizedrecvsubarray, ierr)
 call mpi_type_commit(resizedrecvsubarray,ierr)
end if

allocate(counts(4),disps(4))
counts(1:4) = (/1, 1, 1, 1/)
disps(1:4) = (/0, 5, 10, 15/)
call mpi_barrier(mpi_comm_world,ierr)
print *, mat, "process", myRank    
call mpi_gatherv(mat,1,sendsubarray,matG,counts,disps,resizedrecvsubarray, &
                 0,mpi_comm_world,ierr)
do p=0,nProcs
  if (myRank == p) then
     print *, 'Local array for rank ', myRank
     do i=1, nx
      print *, (mat(i,j),j=1,ny)
     end do
  endif
enddo
call MPI_Barrier(MPI_COMM_WORLD,ierr)
if(myRank.eq.0) then
 print * , matG, "process", myRank    
 print *, 'Global array: '
 do i=1, nx_glb
  print *, (matG(i,j),j=1, ny_glb)
 end do
end if

call mpi_finalize(ierr)

end program main

理想的结果(请注意排名 1 少一行):

Local array for rank 0
00000
00000
00000
00000
00000
Local array for rank 1
11111
11111
11111
11111
Local array for rank 2
22222
22222
22222
22222
22222
Local array for rank 3
33333
33333
33333
33333
33333

Global array: 
00000
00000
00000
00000
00000
11111
11111
11111
11111
22222
22222
22222
22222
22222
33333
33333
33333
33333
33333

实际结果(请注意我在本地有预期的行为,但在全局矩阵中,额外的 1 行在那里并且它在末尾有点):

Local array for rank 0
00000
00000
00000
00000
00000
Local array for rank 1
11111
11111
11111
11111
Local array for rank 2
22222
22222
22222
22222
22222
Local array for rank 3
33333
33333
33333
33333
33333

Global array: 
00000
00000
00000
00000
00000
1111.
1111.
1111.
1111.
1111.
22222
22222
22222
22222
22222
33333
33333
33333
33333
33333

我知道在内存中,矩阵被保存为数组,所以我得到的全局数组是这样的:

0000011111222223333300000111112222233333000001111122222333330000011111222223333300000.....2222233333

问题

如何删除点(代表排名 1 的空行)? 如何让它显示为具有正确行数的矩阵?

编辑 全局数组中出现额外行的原因是,在根进程中创建的 recvsubarray 的尺寸为 5x5,尽管来自进程 1 的 sendsubarray 的尺寸为 4x5。现在的问题是,如何根据接收信息的等级来定义具有可变维度的recvsubarray?

【问题讨论】:

    标签: parallel-processing fortran mpi


    【解决方案1】:

    通过将全局矩阵定义为在第一维 (nx) 而不是第二维 (ny) 中更大,您的生活变得非常艰难。 Fortran 存储数组的方式,具有较大的 ny 更加自然,因为它对应于将所有子矩阵按顺序存储在内存中。

    如果您愿意交换 nx 和 ny,那么您不需要使用任何复杂的派生类型。事实上,我怀疑您是否可以使用 Scatterv 执行此模式,因为该函数需要单一接收类型,但您对每个传入矩阵都有不同的模式(由于您选择了 nx 和 ny 的排序)。

    这段代码,交换了 nx 和 ny,似乎可以正常工作。虚线在最后——我猜你总是会有一些点,因为你分配的空间比子矩阵占用的空间多。

    program main
    use mpi
    implicit none
    integer :: ierr, myRank, nProcs
    integer :: sendsubarray, recvsubarray, resizedrecvsubarray
    integer, dimension(2) :: starts,sizes,subsizes
    integer, dimension(:), allocatable :: counts, disps
    integer, parameter :: ny_glb=20, nx_glb=5, nx=5
    integer :: ny
    integer, dimension(:), allocatable :: ny_all
    character, dimension(:,:), target, allocatable :: mat, matG
    character :: c
    integer :: i, j, p
    integer(kind=mpi_address_kind) :: start, extent
    
    call mpi_init(ierr)
    call mpi_comm_rank(mpi_comm_world, myRank, ierr)
    call mpi_comm_size(mpi_comm_world, nProcs, ierr)
    allocate(ny_all(nProcs))
    ny_all = (/5, 4, 5, 5/)
    ny = ny_all(myRank+1)
    allocate(mat(1:nx,1:ny))
    mat='.'
    forall (i=1:nx,j=1:ny) mat(i,j)=ACHAR(ICHAR('0')+myRank)
    
    if(myRank.eq.0) then
     allocate(matG(nx_glb,ny_glb))
     matG='.'
    end if
    
    allocate(counts(4),disps(4))
    counts(:) = nx*ny_all(:)
    
    disps(1)=0
    do i = 2, 4
      disps(i) = disps(i-1)+counts(i-1)
    end do
    
    call mpi_barrier(mpi_comm_world,ierr)
    print *, mat, "process", myRank
    call mpi_gatherv(mat,nx*ny,MPI_CHARACTER,matG,counts,disps,MPI_CHARACTER, &
                     0,mpi_comm_world,ierr)
    do p=0,nProcs
      if (myRank == p) then
         print *, 'Local array for rank ', myRank
         do i=1, nx
          print *, (mat(i,j),j=1,ny)
         end do
      endif
    enddo
    call MPI_Barrier(MPI_COMM_WORLD,ierr)
    if(myRank.eq.0) then
     print * , matG, "process", myRank
     print *, 'Global array: '
     do i=1, nx_glb
      print *, (matG(i,j),j=1, ny_glb)
     end do
    end if
    
    call mpi_finalize(ierr)
    
    end program main
    

    这是一些输出:

     Global array: 
     0000011112222233333.
     0000011112222233333.
     0000011112222233333.
     0000011112222233333.
     0000011112222233333.
    

    00000000000000000000000000111111111111111111112222222222222222222222223333333333333333333333333.....

    希望这是有用的。

    【讨论】:

    • 很高兴知道 fortran 基于 ny 维度存储数组。我无法支持你的回答,因为我没有足够的代表。
    【解决方案2】:

    这就是我解决上述问题的方法(答案是同事给我的,所以全归功于他):

    由于最终矩阵的其中一个维度是固定的,并且由于矩阵无论如何都存储在数组中,因此最好将mpi_gather 与mpi_type_vector 一起使用而不是mpi_type_create_subarray。因此,程序结构如下:a)确定每个秩中子矩阵的感兴趣大小,b)将其转换为向量,c)从每个秩中收集向量,d)将向量重塑为最终矩阵。这样就不需要收集不需要的信息(如上问题中的点所示),因此在使用mpi_gather 后无需将其删除。

    因此,为了将不同长度但宽度恒定的子数组收集到一个全局矩阵中,下面的代码可以解决问题:

    program main
      use mpi
      implicit none
      integer :: ierr, myRank, iProcs, nProcs, master
      integer :: ix, iy, ip
      integer :: nx, nxSum, offset, newtype
      integer, parameter :: ny=5
      integer, allocatable:: vec(:), vecG(:), nxAll(:), displs(:), rcounts(:), matG(:,:)
    
      call mpi_init(ierr)
      call mpi_comm_rank(mpi_comm_world, myRank, ierr)
      call mpi_comm_size(mpi_comm_world, nProcs, ierr)
    
      master = 0
    
      nx = myRank+1
      allocate(vec(nx*ny))
      do ix = 1,nx
         do iy = 1,ny
            ip = (ix-1)*ny + iy
            vec(ip) = myRank
         enddo
      enddo
    
      call mpi_barrier(mpi_comm_world,ierr)
    
      allocate(nxAll(nProcs))
      call mpi_gather(nx, 1, mpi_integer, nxAll, 1, mpi_integer, &
           master, mpi_comm_world, ierr)
    
      if (myRank == master) then
         ! print *, 'nxAll = ', nxAll, 'sum(nxAll) = ',sum(nxAll)
         nxSum = sum(nxAll)        
         allocate(vecG(nxSum*ny))
    
         allocate(displs(nProcs),rcounts(nProcs))
         offset = 0
         do iProcs = 1,nProcs
            displs(iProcs) = offset
            rcounts(iProcs) = nxAll(iProcs)*ny
            offset = offset + rcounts(iProcs)
            ! print *,'iProcs',iProcs,'displs = ',displs(iProcs),'rcounts',rcounts(iProcs)
         enddo
    
      endif
    
      call mpi_type_vector(nx*ny, 1, 1, mpi_integer,newtype,ierr)
      call mpi_type_commit(newtype,ierr)
    
      call mpi_gatherv(vec,1,newtype,vecG,rcounts,displs,mpi_integer, &
           master,mpi_comm_world,ierr)    
    
    
      if (myRank == master) then
         print *, 'Global vector, vecG = ',vecG
    
         ! Reshape into matrix
         print *, 'Global matrix'
         allocate(matG(nxSum,ny))
         do ix = 1,nxSum
            do iy = 1,ny
               ip = (ix-1)*ny + iy
               matG(ix,iy) = vecG(ip)
            enddo
            print *, (matG(ix,iy),iy=1,ny)
         enddo
    
      endif
    
      call mpi_finalize(ierr)
    
    end program main
    

    【讨论】:

      猜你喜欢
      • 2018-11-04
      • 1970-01-01
      • 2013-03-25
      • 2016-02-17
      • 1970-01-01
      • 2012-01-13
      • 2017-07-16
      • 1970-01-01
      相关资源
      最近更新 更多