这个问题遵循MPI_type_create_subarray 和 MPI_Gather上的现有线程。我的目标是使用 Fortran 90 中的 MPI_Type_Create_Subarray 和 MPI_Gatherv 将来自所有从属进程(数量为 4)的更大数组的子数组收集到主进程(rank=0)上的更大数组中。这将帮助我理解 MPI_Gatherv项目。以下是我的示例代码:
program main
implicit none
include "mpif.h"
integer :: ierr, myRank, nProcs
integer :: sendsubarray, recvsubarray, resizedrecvsubarray
integer, dimension(2) :: starts,sizes,subsizes
integer, dimension(:), allocatable :: counts, disps
integer, parameter :: nx_glb=10, ny_glb=10, nx=5, ny=5
integer, dimension(:,:), target, allocatable :: mat, matG
integer, pointer :: sendPtr(:,:), recvPtr(:,:)
integer :: i, j
call mpi_init(ierr)
call mpi_comm_rank(mpi_comm_world, myRank, ierr)
call mpi_comm_size(mpi_comm_world, nProcs, ierr)
sizes(1)=nx+2; sizes(2)=ny+2
subsizes(1)=nx; subsizes(2)=ny
starts(1)=2; starts(2)=2
call mpi_type_create_subarray(2, sizes, subsizes, starts, mpi_order_fortran, &
mpi_integer, sendsubarray, ierr)
call mpi_type_commit(sendsubarray,ierr)
allocate(mat(1:nx+2,1:ny+2))
do j=1, ny+2
do i=1, nx+2
if(i.eq.1 .or. i.eq.nx+2 .or. j.eq.1 .or. j.eq.ny+2) then
mat(i,j)=1000
else
mat(i,j) = myRank
end if
end do
end do
sendPtr=>mat
if(myRank.eq.0) then
allocate(matG(nx_glb,ny_glb))
matG=1000
sizes(1)=nx_glb; sizes(2)=ny_glb
subsizes(1)=nx; subsizes(2)=ny
starts(1)=1; starts(2)=1
call mpi_type_create_subarray(2, sizes, subsizes, starts, mpi_order_fortran, &
mpi_integer, recvsubarray, ierr)
call mpi_type_commit(recvsubarray, ierr)
call mpi_type_create_resized(recvsubarray, 1, sizeof(i), resizedrecvsubarray, ierr)
call mpi_type_commit(resizedrecvsubarray,ierr)
recvPtr=>matG
end if
counts(1:4) = (/1, 1, 1, 1/)
disps(1:4) = (/0, 5, 50, 55/)
call mpi_gatherv(sendPtr,1,sendsubarray,recvPtr,counts,disps,resizedrecvsubarray, &
0,mpi_comm_world,ierr)
if(myRank.eq.0) then
do i=1, nx_glb
write(1000,*) (matG(i,j),j=1, ny_glb)
end do
end if
call mpi_finalize(ierr)
end program main
但是,执行此代码会导致forrtl: severe(174): SIGSEGV, segmentation fault occurred
.
似乎我试图指向一个在收集时尚未初始化或声明的数组的变量/位置。我尝试以多种方式进行调试,但徒劳无功。
提前谢谢了。