Reputation: 1851
Here is my piece of code:
int world_size;
MPI_Comm_size(MPI_COMM_WORLD , &world_size);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD , &world_rank);
int *rbuf , arr[world_size];
if (world_rank == 0)
{
rbuf = (int *) malloc(world_size * 1 * sizeof(int));
}
else
{
rbuf[0] = rand() % 100;
MPI_Send(rbuf , 1 , MPI_INT , 0 , 0 , MPI_COMM_WORLD);
}
if (world_rank == 0)
{
MPI_Gather(arr , world_size , MPI_INT , rbuf , 1 , MPI_INT , 0 , MPI_COMM_WORLD);
for (int i = 0; i < world_size; ++i)
{
printf("%d " , arr[i]);
}
printf("\n");
}
I want to generate a random number at every process which is not the root process. Then I want each such process to send the number generated to the root process, ie. the root process should gather all the numbers generated at each process.
When I am trying to run this code, I get the error:
Fatal error in PMPI_Gather: Message truncated, error stack:
PMPI_Gather(896)......: MPI_Gather(sbuf=0x7ffc3b23c180, scount=10, MPI_INT, rbuf=0x564a753dc790, rcount=1, MPI_INT, root=0, MPI_COMM_WORLD) failed
MPIR_Gather_impl(718).:
MPIR_Gather(678)......:
MPIR_Gather_intra(184):
MPIR_Localcopy(74)....: Message truncated; 40 bytes received but buffer size is 4
(PS. I run this program using mpirun -n 10 ./helloworld
)
Upvotes: 1
Views: 178
Reputation: 459
MPI_Gather
send messages from all ranks to root and receive itself it does not need MPI_Send
.
I changed your example:
#include<stdio.h>
#include<mpi.h>
#include<stdlib.h>
#include<time.h>
int main(int argc, char **argv){
MPI_Init(&argc, &argv);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD , &world_size);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD , &world_rank);
srand(time(NULL)+world_rank);
int *rbuf,*sender;
if (world_rank == 0)
{
rbuf = (int *) malloc(world_size * 1 * sizeof(int));
}
sender = (int *) malloc( 1 * sizeof(int));
sender[0] = rand() % 100;
MPI_Gather(sender , 1 , MPI_INT , rbuf , 1 , MPI_INT , 0 , MPI_COMM_WORLD);
if (world_rank == 0)
{
for (int i = 0; i < world_size; ++i)
{
printf("%d " , rbuf[i]);
}
printf("\n");
}
MPI_Finalize();
}
Upvotes: 3