5

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
Barth
  • 15,135
  • 20
  • 70
  • 105
Madeleine P. Vincent
  • 3,361
  • 5
  • 25
  • 30
  • Two different PROCESSES or TWO threads? – Mats Petersson Apr 09 '13 at 12:02
  • I was asking about 2 different processes. – Madeleine P. Vincent Apr 09 '13 at 12:07
  • Running on the same machine or different machines? – Mats Petersson Apr 09 '13 at 12:07
  • I think this has the same [underlying issue as stdout](http://stackoverflow.com/questions/5305061/ordering-output-in-mpi). – Brett Hale Apr 09 '13 at 12:08
  • @Mats, both cases are important. The reason for my including "other streams" in my post is because I am planning on using a singleton custom stream to direct all output to either cout or a file (or both). – Madeleine P. Vincent Apr 09 '13 at 12:12
  • Well, there are several potential problems. One is in case of multiple processes, each process will have its own buffer for the I/O, so only when the buffer is flushed (at `endl`, but could of course happen before that in some cases) will the output appear on screen or in a file. The second problem is that if two processes write to the same output device, what are the rules ("not defined" tends to be the rule). This is far from an exclusive list. – Mats Petersson Apr 09 '13 at 12:13
  • With multiple machines you have a further problem of "no trivial synchronisation primitives" (between processes on the same machine, you can use a named semaphore or something like that). – Mats Petersson Apr 09 '13 at 12:14

1 Answers1

3

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.

osgx
  • 90,338
  • 53
  • 357
  • 513