This source file includes following definitions.
- main
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <mpi.h>
5
6 int main (int argc, char **argv){
7 char buff[30];
8 MPI_Status st;
9 MPI_Comm comm[2], parent;
10 MPI_Request req[2];
11 int errcodes[1];
12 int level;
13 int x = 6, i, j;
14
15 MPI_Init(&argc, &argv);
16 MPI_Comm_get_parent(&parent);
17 argv++;
18 level = atoi(argv[0]);
19 printf("level = %d\n",level);
20
21 MPI_Recv(&buff, sizeof(char)*30, MPI_CHAR, MPI_ANY_SOURCE,
22 MPI_ANY_TAG, parent, &st);
23 printf("Parent sent: %s\n", buff);
24
25 if(level < x){
26 sprintf(argv[0], "%d", level+1);
27 MPI_Comm_spawn("ch_rec", argv, 1, MPI_INFO_NULL, 0, MPI_COMM_SELF,
28 &comm[0], errcodes);
29 sprintf(buff,"level %d (pid:%d)", level, getpid());
30 MPI_Send(&buff, sizeof(char)*30, MPI_CHAR, 0, 100, comm[0]);
31 MPI_Irecv(&buff, sizeof(char)*30, MPI_CHAR, MPI_ANY_SOURCE,
32 MPI_ANY_TAG, comm[0], &req[0]);
33
34
35 sprintf(argv[0], "%d", (level+1));
36 MPI_Comm_spawn("ch_rec", argv, 1, MPI_INFO_NULL, 0, MPI_COMM_SELF,
37 &comm[1], errcodes);
38 sprintf(buff,"level %d (pid:%d)", level, getpid());
39 MPI_Send(&buff, sizeof(char)*30, MPI_CHAR, 0, 100, comm[1]);
40 MPI_Irecv(&buff, sizeof(char)*30, MPI_CHAR, MPI_ANY_SOURCE,
41 MPI_ANY_TAG, comm[1], &req[1]);
42
43 for(i=0; i<2; i++){
44 MPI_Waitany(2, req, &j, MPI_STATUS_IGNORE);
45 printf("Child %d sent: %s\n", j, buff);
46 }
47 }
48 sprintf(buff,"level %d (pid:%d)", level, getpid());
49 MPI_Send(&buff, sizeof(char)*30, MPI_CHAR, 0, 100, parent);
50 MPI_Finalize();
51 return 0;
52 }