4

The ref of MPI_Init, states:

This routine must be called by one thread only. That thread is called the main thread and must be the thread that calls MPI_Finalize.

How to do this? I mean every example I have seen looks like this and in my code, I tried:

MPI_Comm_rank(MPI_COMM_WORLD, &mpirank);
bool mpiroot = (mpirank == 0);
if(mpiroot)
  MPI_Init(&argc, &argv);

but I got:

Attempting to use an MPI routine before initializing MPICH

However, notice that this will work fine, if I leave it as in the example, I just had to re-check, because of my code's failure here.


I am thinking that because we call mpiexec -n 4 ./test, 4 processes will be spawned, thus all of them will call MPI_Init. I just printed stuff at the very first line of main() and they will be printed as many times as the number of processes.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 4
    _Thread_, not process. Every process/rank must call `MPI_Init()`. The issue is that if you're using hybrid threading+MPI, only one thread (in *each* rank/process) can call MPI_Init; e.g., it would be in an `#pragma omp single` construct or similar. – Jonathan Dursi Aug 05 '15 at 13:19
  • I see @JonathanDursi, thanks, now I am going to struggle for another day in my other question.. – gsamaras Aug 05 '15 at 14:17

1 Answers1

5

MPI_Init must be the first MPI function called by your MPI program. It must be called by each process. Note that a process is not the same as a thread! If you go on to spawn threads from a process, those threads must not call MPI_Init again.

So your program should be something like this:

 int main(int argc, char **argv) 
 {
     MPI_Init(&argc, &argv);
     int mpirank;
     MPI_Comm_rank(MPI_COMM_WORLD, &mpirank);
     // No more calls to MPI_Init in here
     ...
     MPI_Finalize();
 }
mort
  • 12,988
  • 14
  • 52
  • 97