-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSimple_communications2.cpp
More file actions
31 lines (24 loc) · 1.25 KB
/
Copy pathSimple_communications2.cpp
File metadata and controls
31 lines (24 loc) · 1.25 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
/*** An example to demonstrate a point-to-point communication using multiple ranks ***/
#include <stdio.h>
#include "mpi.h" /* Include MPI header file that contains the library’s API */
int main (int argc, char* argv[])
{
int my_rank, num_procs;
MPI_Init (&argc, &argv); /* start up MPI */
MPI_Comm_rank (MPI_COMM_WORLD, &my_rank); /* get current process' "ID" number (rank) */
MPI_Comm_size (MPI_COMM_WORLD, &num_procs); /* get total number of processes */
MPI_Status status; /* stores the status of whether a communication is successful or not */
int message;
message = my_rank * 10;
if (my_rank == 0) { // Rank 0 receives messages from the others
for (int i=1; i<num_procs; i++) {
//MPI_Recv(&message, 1, MPI_INT, i, i, MPI_COMM_WORLD, &status);
MPI_Recv(&message, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
printf("Received message %d from rank %d\n", message, status.MPI_SOURCE);
}
}
else // Every rank (except Rank 0) sends a message to Rank 0
MPI_Send(&message, 1, MPI_INT, 0, my_rank, MPI_COMM_WORLD);
MPI_Finalize(); /* finish up MPI */
return 0;
}