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.