-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpi_collective_gather.cpp
58 lines (47 loc) · 1.52 KB
/
mpi_collective_gather.cpp
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# include <iostream>
# include <cstdlib> // has exit(), etc.
# include <ctime>
# include "mpi.h" // MPI header file
using namespace std;
//****************************************************************************80
int main (int argc, char **argv)
//****************************************************************************80
{
int data_array[8] = {};
int recv_buffer[8] = {};
int nprocs, rank;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
// assign data_array
for (int i=0; i<2; i++) {
data_array[i] = 100+i+rank*2;
}
int send_count = 2;
int recv_count = 2;
cout << "Before Gather, data_array = ";
for (int j=0; j<8; j++) {cout << data_array[j] << " ";}
cout << " in rank = " << rank << endl;
cout << "Before Gather, recv_buffer = ";
for (int j=0; j<8; j++) {cout << recv_buffer[j] << " ";}
cout << " in rank = " << rank << endl;
MPI_Gather(&data_array, send_count, MPI_INT, &recv_buffer, recv_count, MPI_INT, 0, MPI_COMM_WORLD);
cout << "After Gather, data_array = ";
for (int j=0; j<8; j++) {cout << data_array[j] << " ";}
cout << " in rank = " << rank << endl;
cout << "After Gather, recv_buffer = ";
for (int j=0; j<8; j++) {cout << recv_buffer[j] << " ";}
cout << " in rank = " << rank << endl;
// get average
if (rank == 0) {
float avg = 0;
float sum = 0;
for (int i=0; i<8; i++) {
sum += recv_buffer[i];
}
avg = sum / 8;
cout << "Avg = " << avg << endl;
}
MPI_Finalize();
return 0;
}