I am starting out with MPI and have written a quick demo program:
int main(int argc, char** argv)
{
MPI_Init(&argc, &argv);
int myRank = MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
if (myRank) {
cout << "slave" << endl;
}
else {
cout << "master" << endl;
}
MPI_Finalize();
return 0;
}
I run it with the following command:
aprun -n 4 test
My output is
master
master
master
master
I was expecting something like
slave
master
slave
slave
Why is this happening? Why are all my threads masters?
The problem is in this line:
You should not assign myRank to the result of the call to MPI_Comm_rank. Just do:
and it will work.