-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpi_p2p_array.cpp
48 lines (37 loc) · 1.26 KB
/
mpi_p2p_array.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
#include <iostream>
#include <mpi.h>
#define MASTER 0
#define LEN_VAL 100
using namespace std;
int main(int argc, char **argv)
{
int nprocs, rank;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
cout << "Hello from process rank " << rank << endl;
int val[LEN_VAL] = {0};
int chunk_size = LEN_VAL / nprocs;
int offset = 0;
if (rank == MASTER) {
for (int i=0; i < LEN_VAL; i++)
val[i] = i+1; // Stores numbers 1 to 100 in the array
// For each process, sends value of array from offset + chunk_size
for (int dest_rank = 1; dest_rank < nprocs; dest_rank++) {
offset = dest_rank * chunk_size;
MPI_Send(&val[offset], chunk_size, MPI_INT, dest_rank, 0, MPI_COMM_WORLD);
}
for (int i=0; i<chunk_size; i++)
cout << val[i] << " ";
cout << endl;
}
else {
MPI_Status status;
MPI_Recv(&val, chunk_size, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
cout << "Process rank " << rank << " has a value: ";
for (int i=0; i <chunk_size; i++)
cout << val[i] << " ";
cout << endl;
}
MPI_Finalize();
}