-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter3.c
More file actions
27 lines (25 loc) · 811 Bytes
/
chapter3.c
File metadata and controls
27 lines (25 loc) · 811 Bytes
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
#include <stdio.h>
#include <string.h> /∗ For strlen ∗/
#include <mpi.h> /∗ For MPI functions, etc ∗/
const int MAX_STRING = 100;
int main(void) {
char greeting[MAX_STRING];
int comm_sz; /∗ Number of processes ∗/
int my_rank; /∗ My process rank ∗/
MPI_Init(NULL, NULL);
MPI_Comm size(MPI_COMM_WORLD, &comm_sz);
MPI_Comm rank(MPI_COMM_WORLD, &my_rank);
if (my rank != 0) {
sprintf(greeting, "Greetings from process %d of %d!", my_rank, comm_sz);
MPI_Send(greeting, strlen(greeting)+1, MPI_CHAR, 0, 0,
MPI_COMM_WORLD);
} else {
printf("Greetings from process %d of %d!\n", my_rank, comm_sz);
for (int q = 1; q < comm_sz; q++) {
MPI_Recv(greeting, MAX_STRING, MPI_CHAR, q, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("%s\n", greeting);
}
}
MPI_Finalize();
return 0;
} /∗ main ∗/