I'm writing a c++ program with MPI (C interface, no boost etc). In my program, I have many many outputs, either to cout or to files, which are only done on rank 0. Is there a good way to avoid
writing if (rank == 0) cout << string
?
There are two ways that are working on my computer with my MPI implementation, but both look shaky to me:
if (rank != 0)
cout.setstate(ios_base::badbit);
This effectively disables output on all ranks but 0, but is it allowed? Will there be problems if I do this?
The other idea was to create an ofstream, which is not opened, and redirect the output there.
ostream* os;
ofstream nullstream;
if (rank == 0)
os = &cout;
else
os = &nullstream;
*os << "rank " << rank << endl;
This leaves nullstream in an error state, but effectively also disables output on all ranks that are not 0...
This question would seem common to me, so I'm sorry if it is already answered somwhere else. I didn't find an answer by searching and am happy for any redirection to an existing question.