Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An "atomic" call to cout in MPI

I am interested in whether there is a command or a technique within OpenMPI to have an atomic call to write to stdout (or, for that matter, any stream).

What I have noticed is that during the execution of MPI programs, calls to write to cout (or other streams) can become confusing, as each proc may write whenever it gets to a certain section of code. When reporting results, a line can be written to by several procs, confusing the issue. So 2 different procs might do something like this:

//Proc 10 - results calculated somewhere above
//  result1 = 10
//  result2 = 11
cout << "My results are: " << result1 << " " << resul2 << endl;

and:

//Proc 20 - results calculated somewhere above
//  result1 = 20
//  result2 = 21
cout << "My results are: " << result1 << " " << resul2 << endl;

But the result might be:

My results are: 20 My results are: 10 11 21

What I'm looking for is something like a "blocking" or atomic cout (as well as possibly writing to other streams, such as file streams). So once I start writing a to cout, it blocks until the end of the statement, or until endl or a flush to the stream is issued. If this were the case, I'd be guaranteed that the 2 lines would be separate (but, of course, I still wouldn't know which line would come first):

My results are: 20 21
My results are: 10 11
like image 512
Madeleine P. Vincent Avatar asked Oct 22 '22 12:10

Madeleine P. Vincent


1 Answers

Working with standard i/o streams (stdout and stdin in terms of C language or cout and cin in C++) is not the best part of MPI (OpenMPI implements MPI). There are some slides about this: http://www.csee.wvu.edu/~donm/classes/cs591x/notes/io1.ppt and Slide 10 has a solution. (or chapter 8.1 of the book "Parallel Programming With Mpi By P. S. Pacheco")

Do all your I/O with cout in the process with rank 0. If you want to output some data from other processes, just send MPI message with this data to rank 0.

like image 72
osgx Avatar answered Oct 27 '22 09:10

osgx