You do not need to skip argc and argv since MPI-2 lifted the restriction on MPI-1, which may require MPI_Init arguments for compilation implementations to be the same as main arguments:
MPI-2 implementations do not allow this requirement. Appropriate MPI implementations are needed so that applications can pass NULL for the arguments argc and argv main .
But you still need to check if MPI is already initialized, since MPI_Init() (or MPI_Init_thread() ) should be called no more than once. This is done using MPI_Initialized() , so your code should look like this:
int initialized, finalized; MPI_Initialized(&initialized); if (!initialized) MPI_Init(NULL, NULL);
Note that MPI can be initialized and then completed only once for the entire life of the application. What surrounds the function code block with MPI_Init() ... MPI_Finalize() will not work if the function needs to be called several times, i.e. MPI does not work the same as OpenMP with its parallel areas.
By the way, if I can initialize MPI int func, does A exist in every process at this time?
A running MPI program consists of several processes with their own address spaces. Usually these are several copies of the same program code (the so-called Single Program Multiple Data or SPMD paradigm), but they can also be several copies of several programs written for collaboration (also called Multiple Programs Multiple Data or MPMD). SPMD is a simpler and more common case where all processes execute exactly the same code until their MPI rank is used to branch the execution in several directions. So, yes, A exists in every process, and if in the previous calculations there are no (pseudo) random numbers / events, then A will have the same value in each MPI process until the MPI library is initialized. Note that MPI_Init() is a regular library call like any other library call. It does not alter the contents of user memory - it only makes many running MPI processes aware of each other and allows them to communicate with each other, which allows them to work collectively to solve a specific problem.
source share