Fortran MPI Allgather 用于单独的数组

问题描述 投票:0回答:0

我正在尝试在不同进程之间分配具有多个数组的大型作业,以便每个进程计算一个给定的数组。在此之后,我需要所有进程最终获得相同的信息。我正在尝试了解如何执行此操作,到目前为止,我一直在使用

mpi_allgather
。但是,进程之间的通信似乎不起作用。我做错了什么?

这是我正在做的一个例子:

program main
use mpi
implicit none

integer           :: nproc,rank,ierr,i
integer,parameter :: n = 3
integer,parameter :: m = 2
double precision  :: x(n,m),y(n,m),z(m)

! Initializing MPI
call mpi_init(ierr)
call mpi_comm_rank(mpi_comm_world, rank,  ierr)
call mpi_comm_size(mpi_comm_world, nproc, ierr)

! Initial values for x
x = 0.0
y = 1.0

! Do the following m times
do i = 1,m
    
    ! Distribute work in 2 processes
    if (rank == 0) then
        x(:,i) = x(:,i) + i
    end if

    if (rank == 1) then
        y(:,i) = y(:,i) * (i+1.0)
    end if

    ! MPI barrier
    call mpi_barrier(mpi_comm_world, ierr)

    ! Gathering (MPI), so all processes have the same data
    call mpi_allgather(mpi_in_place, n, mpi_double_precision, x(:,i), n, mpi_double_precision, mpi_comm_world, ierr)
    call mpi_allgather(mpi_in_place, n, mpi_double_precision, y(:,i), n, mpi_double_precision, mpi_comm_world, ierr)
    
    ! MPI barrier
    call mpi_barrier(mpi_comm_world, ierr)
    
    ! Using data from both processes
    z(i) = sum(x(:,i)) + sum(y(:,i))

    if (rank == 0)  then
        print *, i, z(i)
    end if

end do

! Finalizing MPI
call mpi_finalize(ierr)

end program main

有了障碍和

mpi_allgather
,进程 0 应该像我连续运行它一样打印:

       1   9.00000000000000
       2   15.0000000000000

但它给了我错误的结果。有什么想法吗?

parallel-processing fortran mpi
© www.soinside.com 2019 - 2024. All rights reserved.