简体   繁体   中英

How to parallelize function with MPI c++

i'm new on MPI and after studying the basics, still I do not understand how I could leverage mpi for a parallel execution of two function, one for the first part of elements and second for the second part. For example:

for(int i = 0; i < argc/2; i++){
  function(el[i]);
 }

for(int i = 0; i < argc-1; i++){
  function(el[i]);
 }

How i can use MPI_Send, MPI_recv ecc.?

MPI is a multiprocessor library, not a multithreading one. Consequently the data are splitted and cannot be seen as a single array on which the cores can process a part of it.

Here, to process the data, each core need to have an array which contains half the data. With MPI_SEND and MPI_RECV, it will look something like this

Get rank and number of proc
Create subarray
Pass parts of initial array from proc 0 to other procs (With MPI_SEND for proc 0 and MPI_RECV for others)
Each proc do the calculation 
Reunite the results with a new set of send/recv

After, it exists too some functions to make the data spreading and reunificating easier

Good Luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM