0

in my program i I use 4 process that fills an array of size i + 1 by the i values, but when I display the RECEIVED table after using the Gatherv function ca not give me the right result: [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;
}
  • [MSVC fills uninitialized memory with 0xCC to aid debugging](https://stackoverflow.com/q/370195/995714), and -858993460 = 0xCCCCCCCC – phuclv Aug 18 '18 at 11:17

1 Answers1

0

It seems that you used this example as a starting point.

Since rank+1 elements are sent from each process instead of rank, the second argument of MPI_Gatherv() should be modified accordingly :

MPI_Gatherv(buffer, rank+1, MPI_INT, buffer, receive_counts, receive_displacements, MPI_INT, 0, MPI_COMM_WORLD);
francis
  • 9,525
  • 2
  • 25
  • 41