-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathParallelHello.c
More file actions
44 lines (35 loc) · 1.15 KB
/
ParallelHello.c
File metadata and controls
44 lines (35 loc) · 1.15 KB
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
// Author: Edward Chan
#include "mpi.h"
int main(int argc, char *argv[]) {
MPI_Init(&argc, &argv);
static const int MASTER = 0, START = 10, PRINT = 20;
int token, currRank, numNodes, len;
MPI_Comm_rank(MPI_COMM_WORLD, &currRank);
MPI_Comm_size(MPI_COMM_WORLD, &numNodes);
char processor_name[MPI_MAX_PROCESSOR_NAME];
MPI_Get_processor_name(processor_name, &len);
if (currRank == MASTER) {
token = START;
// Start the ball rolling
MPI_Send(&token, 1, MPI_INT, (currRank + 1) % numNodes, 0, MPI_COMM_WORLD);
// Rank of the expected processor
int expected = MASTER + 1;
//Receive token in order
while (numNodes - expected) {
MPI_Recv(&token, 1, MPI_INT, expected, 0,
MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("Hello world from Processor %d\n", expected);
expected++;
}
}
else {
// Receive token from previous node
MPI_Recv(&token, 1, MPI_INT, currRank - 1, 0,
MPI_COMM_WORLD, MPI_STATUS_IGNORE);
// Send reply to master node
MPI_Send(&PRINT, 1, MPI_INT, MASTER, 0, MPI_COMM_WORLD);
// Send token to next node
MPI_Send(&token, 1, MPI_INT, (currRank + 1) % numNodes, 0, MPI_COMM_WORLD);
}
MPI_Finalize();
}