This source file includes following definitions.
- main
1 #define _GNU_SOURCE
2 #include "orte_config.h"
3
4 #include <stdio.h>
5 #include <sys/types.h>
6 #include <unistd.h>
7 #include <stdlib.h>
8
9 #include <mpi.h>
10
11 #define NUM_CHILDREN 5
12
13 int main(int argc, char* argv[])
14 {
15 int msg;
16 MPI_Comm parent, children[NUM_CHILDREN];
17 int rank, size, i;
18 char hostname[OPAL_MAXHOSTNAMELEN];
19 pid_t pid;
20 char *child_argv[2] = { "", NULL };
21
22 MPI_Init(NULL, NULL);
23 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
24 MPI_Comm_size(MPI_COMM_WORLD, &size);
25 MPI_Comm_get_parent(&parent);
26
27 if (MPI_COMM_NULL == parent) {
28 pid = getpid();
29
30
31
32 for (i = 0; i < NUM_CHILDREN; ++i) {
33 printf("Parent [pid %ld] about to spawn child #%d\n",
34 (long)pid, i);
35 opal_asprintf(&(child_argv[0]), "%d", i);
36 MPI_Comm_spawn(argv[0], child_argv, 1, MPI_INFO_NULL,
37 0, MPI_COMM_WORLD, &children[i],
38 MPI_ERRCODES_IGNORE);
39 printf("Parent done with spawn of child %d\n", i);
40 }
41
42
43 if (0 == rank) {
44 for (i = 0; i < NUM_CHILDREN; ++i) {
45 printf("Parent sending message to child %d\n", i);
46 MPI_Send(&i, 1, MPI_INT, 0, 1, children[i]);
47 }
48 }
49
50
51 for (i = 0; i < NUM_CHILDREN; ++i) {
52 printf("Parent disconnecting from child %d\n", i);
53 MPI_Comm_disconnect(&children[i]);
54 printf("Parent disconnected from child %d\n", i);
55 }
56 }
57
58 else {
59 gethostname(hostname, sizeof(hostname));
60 if (argc == 1) {
61 printf("ERROR: child did not receive exepcted argv!\n");
62 i = -1;
63 } else {
64 i = atoi(argv[1]);
65 }
66 pid = getpid();
67 printf("Hello from the child %d on host %s pid %ld\n", i, hostname, (long)pid);
68 if (0 == rank) {
69 MPI_Recv(&msg, 1, MPI_INT, 0, 1, parent, MPI_STATUS_IGNORE);
70 printf("Child %d received msg: %d\n", i, msg);
71 if (i != msg) {
72 printf("ERROR: Child %d got wrong message (got %d, expected %d)\n",
73 i, msg, i);
74 }
75 }
76 MPI_Comm_disconnect(&parent);
77 printf("Child %d disconnected\n", i);
78 }
79
80 MPI_Finalize();
81 return 0;
82 }