归约
当多个进程要做相同的操作,比如取最大值,加和等时,可以使用归约以简化代码。
基础的归约函数:
很好理解,就是把sendbuf开始的count个连续的数据,进行op操作,再存入recvbuf中。
下面举几个简单的例子:
MPI_MAX
例如求最大值
源码
//
// Created by zhangx on 2022/9/19.
//
#include "mpi.h"
#include "iostream"
#include "math.h"
#include "ctime"
using namespace std;
const int N = 10;
const int root = 0;
int main(){
int rank,size;
int x[N],y[N];
MPI_Init(NULL,NULL);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
srand(time(0)+rank);
for(int i=0;i<