I hav this simple MPI program running on two processors. In my example (or maybe just on my computer) my console output the reception message before the send message.
I know that there’s a way of ordering receptions using MPI, but I tought that my example would wait for processors 0 to send data, so the reception output would be necessarily in second.
How can I print the reception message after the send message?
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mpi.h>
int main(int argc, char *argv[]){
int np, myId;
char send[100], recv[100];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &np);
MPI_Comm_rank(MPI_COMM_WORLD, &myId);
MPI_Status stat;
if(myId == 0){
int length = sprintf(send, "hey!");
for(int i = 1; i < np; i++){
printf("send %d => %d (%d)", myId, i, length);
MPI_Send(send, length, MPI_CHAR, i, 0, MPI_COMM_WORLD);
}
}else if (myId == 1){
MPI_Recv(send, 41, MPI_CHAR, 0, 0, MPI_COMM_WORLD, &stat);
printf("receive %d <= %d\n", myId, 0);
}
MPI_Finalize();
return 0;
}
When writing to a shared buffer in parallel, without using a parallel i/o library there is no guarantee which buffer will be cleared first. If you want them to come out in order, you should either flush the output buffer after the print command, e.g.,
fflush(stdout);, print to a buffer which is automatically flushed every time, e.g.,fprintf(stderr,...);or useMPI_Barrierto separate the print commands. The last one is not recommended as it may lead to poor performance.