-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPI_Reduce.c
36 lines (30 loc) · 886 Bytes
/
MPI_Reduce.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* Simple MPI program to understand Reduce Operation. */
/* Author : Dhaval Lad */
#include "mpi.h"
#include<stdio.h>
int main(int argc, char *argv[]){
int numTasks, rank,i;
// Initilizae MPI enivronment.
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&numTasks);
int sendBuf[] = {-1,-2};
sendBuf[0] = rank + 100;
sendBuf[1] = rank + 10;
int receiveBufSize = 2;
int receiveBuf[receiveBufSize];
for(i=0;i<receiveBufSize;i++){
receiveBuf[i] = 0;
}
// Master node receive all interger send by other process.
MPI_Reduce(sendBuf,receiveBuf,receiveBufSize,MPI_INT,MPI_SUM,0,MPI_COMM_WORLD);
// Master node print sum.
if(rank == 0){
printf("\nAfter Reduce operation at processor#%d",rank);
for(i=0;i<receiveBufSize;i++){
printf("\nReceived Buffer[%d] = %d",i,receiveBuf[i]);
}
printf("\n");
}
MPI_Finalize();
}