Gatherv功能

时间:2014-12-06 14:42:15

标签: c parallel-processing mpi

在我的程序中,我使用4个过程,通过i值填充i + 1大小的数组,但是当我在使用Gatherv函数后显示RECEIVED表时,不能给出正确的结果:[0] [1 ] [ - 858993460] [2] [2] [ - 858993460] [3] [3] [3] [ - 858993460]

#include "mpi.h"
#include <stdio.h>
int main(int argc, char *argv[])
{
int buffer[10];
int rank, size, i,receive_counts[4] = { 1, 2, 3,4 },receive_displacements[4] = { 0, 1, 3, 6 };
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (size != 4)
{
    if (rank == 0)
    {
        printf("Please run with 4 processes\n"); fflush(stdout);
    }
    MPI_Finalize();
    return 0;
}
for (i = 0; i<=rank; i++)
{
    buffer[i] = rank;
}   
printf("\n");
MPI_Gatherv(buffer, rank, MPI_INT, buffer, receive_counts, receive_displacements, MPI_INT, 0, MPI_COMM_WORLD);
if (rank == 0)
{
    for (i = 0; i<10; i++)
    {
        printf("[%d]", buffer[i]);
    }
    printf("\n");
    fflush(stdout);
}
MPI_Finalize();
return 0;
}

1 个答案:

答案 0 :(得分:0)

似乎您使用此example作为起点。

由于rank+1元素是从每个进程而不是rank发送的,因此应该相应地修改MPI_Gatherv()的第二个参数:

MPI_Gatherv(buffer, rank+1, MPI_INT, buffer, receive_counts, receive_displacements, MPI_INT, 0, MPI_COMM_WORLD);