Discussion:
MPI with cout
(too old to reply)
Stefan Kuhne
2009-12-03 12:03:50 UTC
Permalink
Hello,

i'll port my C code to C++.
My hello.c has:
printf("Hier ist Job %2.0f von %2.d auf %s\n", (double) myrank, size,
cpu_name);

and hello.cpp:
cout << setw(2) << "Hier ist Job " << myrank << " von " << size << " auf
" << cpu_name << endl;

The C Output is correct put the C++ output is:
Hier ist Job Hier ist Job Hier ist Job Hier ist Job 3 von 4 auf 10 von 4
auf fb5


2 von 4 auf fb5-cluster-head

or somethink simular.

How can i get cout sync?

Regards,
Stefan Kuhne
Heiko Bauke
2009-12-03 12:40:40 UTC
Permalink
Hi,

On Thu, 03 Dec 2009 13:03:50 +0100
Post by Stefan Kuhne
i'll port my C code to C++.
printf("Hier ist Job %2.0f von %2.d auf %s\n", (double) myrank, size,
cpu_name);
cout << setw(2) << "Hier ist Job " << myrank << " von " << size << "
auf " << cpu_name << endl;
Hier ist Job Hier ist Job Hier ist Job Hier ist Job 3 von 4 auf 10
von 4 auf fb5
2 von 4 auf fb5-cluster-head
or somethink simular.
How can i get cout sync?
the MPI standard does not have any notion of C++ std::cout. Thus,
it's up to the MPI implementation how it deals with reading from and
writing from standard streams. A valid MPI implementation may
even suppress reading from and writing from standard streams completely.
Therefore, I would suggest to try another MPI implementation.


Heiko
--
-- Wurst ist eine Götterspeise. Denn nur Gott weiß, was drin ist.
-- (Jean Paul)
-- Cluster Computing @ http://www.clustercomputing.de
-- Heiko Bauke @ http://www.mpi-hd.mpg.de/personalhomes/bauke
Stefan Kuhne
2009-12-03 12:48:12 UTC
Permalink
Post by Heiko Bauke
the MPI standard does not have any notion of C++ std::cout. Thus,
it's up to the MPI implementation how it deals with reading from and
writing from standard streams. A valid MPI implementation may
even suppress reading from and writing from standard streams completely.
Therefore, I would suggest to try another MPI implementation.
But MPICH2 is the only one with MPE support.
So i have to use printf in this case.

Regards,
Stefan Kuhne
Heiko Bauke
2009-12-03 13:03:08 UTC
Permalink
Hi,

On Thu, 03 Dec 2009 13:48:12 +0100
Post by Stefan Kuhne
But MPICH2 is the only one with MPE support.
MPE is not included in MPI implementations except MPICH2. However, to my
knowledge, MPE should work with any MPI standard compliant MPI
implementation. Download the source of the latest version of MPICH2,
extract the folder mpich2-1.2.1/src/mpe2/ from the tar ball and read
the README file and the INSTALL file.


Heiko
--
--
-- Derjenige, der zum ersten Mal anstatt eines Speeres ein Schimpfwort
-- benutzte, war der Begründer der Zivilisation. (Jean Paul)
-- Cluster Computing @ http://www.clustercomputing.de
-- Heiko Bauke @ http://www.mpi-hd.mpg.de/personalhomes/bauke
Stefan Kuhne
2009-12-04 08:47:57 UTC
Permalink
Heiko Bauke schrieb:

Hello,
Post by Heiko Bauke
On Thu, 03 Dec 2009 13:48:12 +0100
Post by Stefan Kuhne
But MPICH2 is the only one with MPE support.
MPE is not included in MPI implementations except MPICH2. However, to my
knowledge, MPE should work with any MPI standard compliant MPI
implementation. Download the source of the latest version of MPICH2,
extract the folder mpich2-1.2.1/src/mpe2/ from the tar ball and read
the README file and the INSTALL file.
openMPI runs with cout fine.

But when i try to build MPE2:

***@head:mpe2$ ./configure
--with-mpiinc=/opt/mpi/gcc/openmpi-1.3.3/include/
--with-mpilibs=/opt/mpi/gcc/openmpi-1.3.3/lib64/ --with-mpicc=
/opt/mpi/gcc/openmpi-1.3.3/bin/
configure: WARNING: you should use --build, --host, --target
configure: WARNING: invalid host type: /opt/mpi/gcc/openmpi-1.3.3/bin/
Configuring MPE Profiling System with
'--with-mpiinc=/opt/mpi/gcc/openmpi-1.3.3/include/'
'--with-mpilibs=/opt/mpi/gcc/openmpi-1.3.3/lib64/' '--with-mpicc='
'/opt/mpi/gcc/openmpi-1.3.3/bin/'
'build_alias=/opt/mpi/gcc/openmpi-1.3.3/bin/'
'host_alias=/opt/mpi/gcc/openmpi-1.3.3/bin/'
'target_alias=/opt/mpi/gcc/openmpi-1.3.3/bin/'
'CPPFLAGS=-I/usr/lib/jvm/java-6-sun-1.6.0.16/include/
-I/usr/lib/jvm/java-6-sun-1.6.0.16/include/linux/ -I/opt/include/
-I/opt/mpi/gcc/openmpi-1.3.3/include'
checking for current directory name... /home/sk/Cluster/lib/mpe2
checking gnumake... yes using --no-print-directory
checking BSD 4.4 make... no - whew
checking OSF V3 make... no
checking for virtual path format... VPATH
User supplied MPI implmentation (Good Luck!)
checking for leftover Makefiles in subpackages ... none
checking for /opt/mpi/gcc/openmpi-1.3.3/bin/-gcc... cc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether cc accepts -g... yes
checking for cc option to accept ISO C89... none needed
checking whether MPI_CC has been set ... cc
checking for /opt/mpi/gcc/openmpi-1.3.3/bin/-g77... f77
checking whether we are using the GNU Fortran 77 compiler... no
checking whether f77 accepts -g... no
checking whether MPI_F77 has been set ... f77
checking for the linkage of the supplied MPI C definitions ... no
configure: error: Cannot link with basic MPI C program!
Check your MPI include paths, MPI libraries and MPI CC compiler
***@head:mpe2$

But i can build and run mpi code with mpicc and mpirun.

Regards,
Stefan Kuhne
Stefan Kuhne
2009-12-04 09:02:23 UTC
Permalink
Post by Stefan Kuhne
checking for the linkage of the supplied MPI C definitions ... no
configure: error: Cannot link with basic MPI C program!
Check your MPI include paths, MPI libraries and MPI CC compiler
RTMF ...

With configure option from INSTALL configure runs.

Stefan Kuhne
Familie Bisseling
2009-12-05 15:16:35 UTC
Permalink
Post by Stefan Kuhne
Hello,
i'll port my C code to C++.
printf("Hier ist Job %2.0f von %2.d auf %s\n", (double) myrank, size,
cpu_name);
cout << setw(2) << "Hier ist Job " << myrank << " von " << size << " auf
" << cpu_name << endl;
Hier ist Job Hier ist Job Hier ist Job Hier ist Job 3 von 4 auf 10 von 4
auf fb5
2 von 4 auf fb5-cluster-head
or somethink simular.
How can i get cout sync?
Regards,
Stefan Kuhne
You should not rely on any proper interleaving of lines printed by
different
MPI processes since this behavior is not guaranteed in any standard.

The only difference between cout and stdout that I am aware of might be an
additional buffering. You may try to adjust that buffering or force a
buffer
flush by calling ostream.flush() on cout after each line.

It may help to use stderr/cerr since that should be buffered line wise. But
if this works than you are still not on solid ground regarding the MPI
behavior.

Good luck!
--
Erstellt mit Operas revolutionärem E-Mail-Modul: http://www.opera.com/mail/
Colin Paul Gloster
2010-02-18 12:21:59 UTC
Permalink
On Sat, 5 Dec 2009, Bisseling sent:

|----------------------------------------------------------------------------|
|"Am 03.12.2009, 13:03 Uhr, schrieb Stefan Kuhne <SK-***@gmx.net>: |
| |
|> Hello, |
|> |
|> i'll port my C code to C++. |
|> My hello.c has: |
|> printf("Hier ist Job %2.0f von %2.d auf %s\n", (double) myrank, size, |
|> cpu_name); |
|> |
|> and hello.cpp: |
|> cout << setw(2) << "Hier ist Job " << myrank << " von " << size << " auf |
|> " << cpu_name << endl; |
|> |
|> The C Output is correct put the C++ output is: |
|> Hier ist Job Hier ist Job Hier ist Job Hier ist Job 3 von 4 auf 10 von 4 |
|> auf fb5 |
|> |
|> |
|> 2 von 4 auf fb5-cluster-head |
|> |
|> or somethink simular. |
|> |
|> How can i get cout sync? |
|> |
|> Regards, |
|> Stefan Kuhne |
| |
|You should not rely on any proper interleaving of lines printed by different|
|MPI processes since this behavior is not guaranteed in any standard. |
| |
|The only difference between cout and stdout that I am aware of might be an |
|additional buffering. You may try to adjust that buffering or force a buffer|
|flush by calling ostream.flush() on cout after each line. |
| |
|It may help to use stderr/cerr since that should be buffered line wise. But |
|if this works than you are still not on solid ground regarding the MPI |
|behavior. |
| |
|Good luck!" |
|----------------------------------------------------------------------------|

In order to not rely on undefined behavior, it might be portable for
each process to write to a different file instead of to stdout (with
printf()) or stderr (with fprintf()).

You can look at a file while it is being written with
less filename
then press both Ctrl and C and then press F.

b***@myrealbox.com
2009-12-14 08:02:40 UTC
Permalink
Post by Stefan Kuhne
Hello,
i'll port my C code to C++.
printf("Hier ist Job %2.0f von %2.d auf %s\n", (double) myrank, size,
cpu_name);
cout << setw(2) << "Hier ist Job " << myrank << " von " << size << " auf
" << cpu_name << endl;
Hier ist Job Hier ist Job Hier ist Job Hier ist Job 3 von 4 auf 10 von 4
auf fb5
2 von 4 auf fb5-cluster-head
or somethink simular.
How can i get cout sync?
Others have pointed out that the MPI standard makes no guarantees
about how -- or even whether(?) -- output to stdout will work.
Still, a workaround that might be worth trying is to assemble your
message in a string and then send the whole thing to stdout at once.
So your example would become:

ostringstream msgStream;
msgStream << setw(2) << "Hier ist Job " << myrank << " von "
<< size << " auf " << cpu_name << endl;
string msg = msgStream.str();
cout << msg;

No guarantees, but might help.
--
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.
Keith Thompson
2009-12-29 03:01:55 UTC
Permalink
Post by b***@myrealbox.com
Post by Stefan Kuhne
i'll port my C code to C++.
printf("Hier ist Job %2.0f von %2.d auf %s\n", (double) myrank, size,
cpu_name);
cout << setw(2) << "Hier ist Job " << myrank << " von " << size << " auf
" << cpu_name << endl;
Hier ist Job Hier ist Job Hier ist Job Hier ist Job 3 von 4 auf 10 von 4
auf fb5
2 von 4 auf fb5-cluster-head
or somethink simular.
How can i get cout sync?
Others have pointed out that the MPI standard makes no guarantees
about how -- or even whether(?) -- output to stdout will work.
Still, a workaround that might be worth trying is to assemble your
message in a string and then send the whole thing to stdout at once.
ostringstream msgStream;
msgStream << setw(2) << "Hier ist Job " << myrank << " von "
<< size << " auf " << cpu_name << endl;
string msg = msgStream.str();
cout << msg;
No guarantees, but might help.
That's likely to work.

The C printf call is likely to assemble the output in memory, and then
print it in a single call to a lower-level routine. In the C++
"cout << foo << bar", on the other hand, each "<<" is a distinct
function call; the implementation is unlikely to assemble the chained
output calls into a single chunk of data.

But even so, there are no guarantees; printf *could* invoke multiple
output operations. If synchronization is important, you'll have to
find some way to do it explicitly.
--
Keith Thompson (The_Other_Keith) kst-***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Loading...