c++ source phello.cpp
#include <iostream>
#include <mpi.h> //mpi include files are in /usr/include
int main() {
int rank, size ;
MPI::Init() ;
rank = MPI::COMM_WORLD.Get_rank() ;
size = MPI::COMM_WORLD.Get_size() ;
cout << "Hello, world! I am " << rank
<< " of " << size << "." << endl;
MPI::Finalize() ;
return 0;
}
"COMM_WORLD" is the default communicator, the group of all processes among which messages are passed. Additional communicators (e.g., subsets of COMM_WORLD) can be defined.
compile and link
[psiders@beowulf source]
mpiCC phello.cpp -o
phelloC
-llammpi++
The command "mpiCC" equals
c++ -llamf77mpi -lmpi -llam -lnsl -lutil
The libraries are in /usr/lib.
Note that c++ code requires one additional library:
lammpi++.
execute
[psiders@beowulf source]
mpirun N phelloC
"N" instructs mpirun to use all booted processes.
The output is
Hello, world! I am 0 of 7.
Hello, world! I am 1 of 7.
Hello, world! I am 2 of 7.
Hello, world! I am 4 of 7.
Hello, world! I am 6 of 7.
Hello, world! I am 3 of 7.
Hello, world! I am 5 of 7.
Note that stdout is collected in one place.
The order of execution by various processors is not
readily predicted.
A different number of processes may be specified by using the
command in the form
mpirun -np # phelloC
where "#" is the number
of processes to be used. There can be multiple processes on
a single CPU, so "#" can be larger than the number
of computers or CPUs listed in the lamhosts file.