首页 > 解决方案 > 关于 mpi_gatherv 的一些提示

问题描述

我试图通过一个简单的例子来理解 mpi_gatherv:

每个等级都有一个名为 x_local 的数组,大小为“id+1”,因此等级 1 具有 x_local(1),等级 2 具有 x_local(2),依此类推。我总共使用了 4 个 procs。

为简单起见 x_local = real(id+1)

在输出时,我想要一个数组 x (仅适用于根处理器),如下所示:

x=(1,2,2,3,3,3,4,4,4,4)

这是我的代码:

    subroutine testGatherV

    ! Global variables

    use mpi
    use mpivar

    ! Local variables

    implicit none

    integer i, ierr, displs(nid), rcounts(nid)

    real, dimension(:), allocatable:: x_local

    real, dimension(:), allocatable:: x

    allocate(x_local(id+1))

    allocate(x(10))

   ! 

    x=-1.0

    displs(1) = 0
    displs(2) = 1
    displs(3) = 3
    displs(4) = 6

    rcounts(1) = 1
    rcounts(2) = 2
    rcounts(3) = 3
    rcounts(4) = 4

    x_local = real(id+1)

    call mpi_gatherv(x_local, id+1, MPI_REAL, x, rcounts, displs, &
            MPI_REAL, 0, MPI_COMM_WORLD, ierr)

    if(id.eq.0)  write(*,*) 'x = ', x

    end

输出是:

 x =    0.0000000000000000        0.0000000000000000        0.0000000000000000        4.0000000000000000        4.0000000000000000       -1.0000000000000000       -1.0000000000000000       -1.0000000000000000       -1.0000000000000000       -1.0000000000000000 

我哪里错了?任何帮助都感激不尽。谢谢你。

标签: fortranmpi

解决方案


推荐阅读