-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpi_collective_scatter.cpp
49 lines (38 loc) · 1.31 KB
/
mpi_collective_scatter.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
# 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] = {0};
int recv_buffer[1] = {0};
int nprocs, rank;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) { // root
for (int i=0; i<8; i++) {
data_array[i] = 100+i;
}
}
int send_count = 2;
int recv_count = 2;
cout << "Before Scatter, data_array = ";
for (int j=0; j<8; j++) {cout << data_array[j] << " ";}
cout << " in rank = " << rank << endl;
cout << "Before Scatter, recv_buffer = ";
for (int j=0; j<1; j++) {cout << recv_buffer[j] << " ";}
cout << " in rank = " << rank << endl;
MPI_Scatter(&data_array, send_count, MPI_INT, &recv_buffer, recv_count, MPI_INT, 0, MPI_COMM_WORLD);
cout << "After Scatter, data_array = ";
for (int j=0; j<8; j++) {cout << data_array[j] << " ";}
cout << " in rank = " << rank << endl;
cout << "After Scatter, recv_buffer = ";
for (int j=0; j<1; j++) {cout << recv_buffer[j] << " ";}
cout << " in rank = " << rank << endl;
MPI_Finalize();
return 0;
}