【发布时间】:2016-10-29 16:05:22
【问题描述】:
这个问题与this one 类似,复杂之处在于被收集的矩阵的大小在行长上不相等,但在列长上相等。 (我也看过 this question 和 this one 但无法弄清楚)。
背景
我正在执行一个计算,直到计算结束我才知道结果矩阵的行数。连续地,我分配了一个非常大的矩阵,该矩阵被填满,并且在计算结束时(当我知道行数的限制时)我切断了这个大数组的末尾,得到了我想要的结果。使用 MPI,我应用相同的逻辑:
- 在每个进程中,我都有一个被填满的大数组
- 在计算结束时,我需要在每个数组各自的限制下截断(每个进程的限制不同)
- 然后我需要将每个进程的结果数组收集到一个中,然后将其提供给根进程,以便继续执行程序的其余部分。
目前的尝试
为了了解 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