How can I initialize MPI in a function?

Viewed 10076

I want to use multi-processes in a function and how can I make it.

As you know, MPI_Init needs two parameter: "int argc, char **argv". Does it mean that I must add these two parameters in the function definition?

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

For example,

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 init MPI? By the way, if I can init MPI int func, does A exist in every processes at this time?

Help me & thanks!

2 Answers
Related