Search code examples
cmpicluster-computingdistributed-computingmpich

MPI_Init must be called by one thread only


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.


Solution

  • 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();
     }