I'm using MPI in this code, a ring of pass of messages between processes, process n receives from n-1 and sends to n+1.
#include "mpi.h"
#include <stdio.h>
int main(argc,argv)
int argc;
char **argv;
{
int MyProc, size,tag=1;
char msg='A', msg_recpt;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &MyProc);
MPI_Comm_size(MPI_COMM_WORLD, &size);
printf("Process # %d started \n", MyProc);
MPI_Barrier(MPI_COMM_WORLD);
if(MyProc == 0){
printf("Sending: Proc #%d to Proc #%d\n",MyProc,(MyProc +1 )%size);
MPI_Send(&msg, 1, MPI_CHAR, (MyProc +1 )%size,tag, MPI_COMM_WORLD);
MPI_Recv(&msg_recpt, 1, MPI_CHAR, (MyProc + size - 1)%size, tag, MPI_COMM_WORLD, &status);
printf("Receving: Proc #%d from Proc #%d\n",MyProc,(MyProc + size - 1)%size);
}else{
printf("Receving: Proc #%d de Proc #%d\n",MyProc,(MyProc + size - 1)%size);
MPI_Recv(&msg_recpt, 1, MPI_CHAR, (MyProc + size - 1)%size, tag, MPI_COMM_WORLD, &status);
MPI_Send(&msg, 1, MPI_CHAR, (MyProc +1 )%size,tag, MPI_COMM_WORLD);
printf("Sending: Proc #%d from Proc #%d\n",MyProc,(MyProc +1 )%size);
}
printf("Finishing proc %d\n", MyProc);
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
}
and I get this output:
Process # 0 started
Process # 1 started
Process # 2 started
Sending: Proc #0 to Proc #1
Receving: Proc #0 from Proc #2
Finishing proc 0
Receving: Proc #1 de Proc #0
Sending: Proc #1 from Proc #2
Finishing proc 1
Receving: Proc #2 de Proc #1
Sending: Proc #2 from Proc #0
Finishing proc 2
Something is not correct because the process 0 receiving message appears before the process2 sending message, I thought that process0 should wait until process2 sends but I don't know what is happening.