如何用mpic++并行化函数

How to parallelize function with MPI c++

本文关键字:函数 并行化 mpic++ 何用      更新时间:2023-10-16

我是MPI的新手,在学习了基础知识之后,我仍然不明白如何利用MPI并行执行两个函数,一个用于元素的第一部分,第二个用于第二部分。例如:

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

如何使用MPI_Send, MPI_recv ecc ?

MPI是一个多处理器库,而不是多线程库。因此,数据被分割,不能被看作是一个单一的数组,内核可以处理其中的一部分。

在这里,为了处理数据,每个核心需要有一个包含一半数据的数组。使用MPI_SEND和MPI_RECV,它看起来像这样

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

之后,还存在一些函数,使数据的传播和统一更加容易

好运