C Question

MPI - Printing in an order

I'm trying to write a function in C where every processor prints it's own data.
Here is what i have:

void print_mesh(int p,int myid,int** U0,int X,int Y){
int i,m,n;
for(i=0;i<p;i++){
if(myid==i){
printf("myid=%d\n",myid);
for(n=0;n<X;n++){
for(m=0;m<Y;m++){
printf("%d ",U0[n][m]);
}
printf("\n");
}
}
else MPI_Barrier(MPI_COMM_WORLD);
}
}


It doesn't work for some reason. The arrays are printed all mixed up.
Do you have any insight as to why this doesn't work? Any other ideas that work?
If possible, I don't want to send the whole array in a master process. Also I don't want to use precompiled functions.

Answer

There is no way to guarantee that messages from many different processes will arrive in the "correct" order when they arrive to another process. This is essentially what is happening here.

Even though you aren't explicitly sending messages, when you print something to the screen, it has to be sent to the process on your local system (mpiexec or mpirun) where it can be printed to the screen. There is no way for MPI to know what the correct order for these messages is so it just prints them as they arrive.

If you require that your messages are printed in a specific order, you must send them all to one rank which can print them in whatever order you like. As long as one rank does all of the printing, all of the messages will be ordered correctly.

It should be said that there will probably be answers that you can find out there which say you can put a newline at the end of your string or use flush() to ensure that the buffers are flushed, but that won't guarantee ordering on the remote end for the reasons mentioned above.

Comments