How can I initialize MPI in a function?

I want to use multiprocesses in a function and how to do it.

As you know, MPI_Init requires two parameters: "int argc, char ** argv". Does this mean that I should add these two parameters to the function definition?

My requirement is that I want to parallelize a step in a function instead of a step in the main program.

For instance,

func(mat &A, vec &x) { some computation on A; auto B = sub_mat(A, 0, 10); B*x; // I want to parallelize this computation } main(){ mat A; vec x; func(A, x); } 

I just want to use MPI in B * x, but I don't know how to initialize MPI? By the way, if I can initialize MPI int func, does A exist in every process now?

Help me and thanks!

+4
source share
2 answers

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); // Perform work in parallel ... // You also need this when your program is about to exit MPI_Finalized(&finalized); if (!finalized) MPI_Finalize(); 

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.

+6
source

If you want to use MPI_Init in a subfunction, you need to pass int argc, char **argv function to pass it.

But even if you just want to parallelize part of the subfunction, you can (and should for more transparent code) use MPI_Init at the beginning of the program. For instance. after completing other initialization actions, or if you want to use it close to your parallelized function, immediately before calling the function.

Basically, a function should not know about argc and argv , right?

0
source

All Articles