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
| #include "mpi.h"
int main(int argc, char *argv[]){
int rank; //rank of the process
int source; //rank of the sender
int dest; //rank of the receiver
int num; //number of the processers
int tags;
char messages[1000];
MPI_Status status;
//setting up the MPI
MPI_init(&argc, &argv);
//find out the rank of the curret program
MPI_Comm_rank(MPI_COMM_WORLD, rank);
//find out the number of the processers
MPI_Common_Size(MPI_COMM_WORLD, &p);
if(rank != 0){ //if the current program is not the root program
sprintf(message, "hello from %d", rank);
dest = 0;
MPI_Send(message, strlen(message) + 1, MPI_CHAR, dest, tag, MPI_COMM_WORLD);
}else{
sprintf(message, "hello from ");
MPI_Recv(message, 100, MPI_CHAR, source, tag, MPI_COMM_WORLD, &status);
printf("%s\n", message);
}
MPI_Finalize();
}
|