This source file includes following definitions.
- main
1 #include "orte_config.h"
2
3 #include <stdio.h>
4 #include <sys/types.h>
5 #include <unistd.h>
6
7 #include <mpi.h>
8
9 int main(int argc, char* argv[])
10 {
11 int msg;
12 MPI_Comm parent, child;
13 int rank, size;
14 char hostname[OPAL_MAXHOSTNAMELEN];
15 pid_t pid;
16 int i;
17 char *cmds[2];
18 char *argv0[] = { "foo", NULL };
19 char *argv1[] = { "bar", NULL };
20 char **spawn_argv[2];
21 int maxprocs[] = { 1, 1 };
22 MPI_Info info[] = { MPI_INFO_NULL, MPI_INFO_NULL };
23
24 cmds[1] = cmds[0] = argv[0];
25 spawn_argv[0] = argv0;
26 spawn_argv[1] = argv1;
27
28 MPI_Init(NULL, NULL);
29 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
30 MPI_Comm_size(MPI_COMM_WORLD, &size);
31 MPI_Comm_get_parent(&parent);
32
33 if (MPI_COMM_NULL == parent) {
34 pid = getpid();
35 printf("Parent [pid %ld] about to spawn!\n", (long)pid);
36 MPI_Comm_spawn_multiple(2, cmds, spawn_argv, maxprocs,
37 info, 0, MPI_COMM_WORLD,
38 &child, MPI_ERRCODES_IGNORE);
39 printf("Parent done with spawn\n");
40 if (0 == rank) {
41 msg = 38;
42 printf("Parent sending message to children\n");
43 MPI_Send(&msg, 1, MPI_INT, 0, 1, child);
44 MPI_Send(&msg, 1, MPI_INT, 1, 1, child);
45 }
46 MPI_Comm_disconnect(&child);
47 printf("Parent disconnected\n");
48 }
49
50 else {
51 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
52 MPI_Comm_size(MPI_COMM_WORLD, &size);
53 gethostname(hostname, sizeof(hostname));
54 pid = getpid();
55 printf("Hello from the child %d of %d on host %s pid %ld: argv[1] = %s\n", rank, size, hostname, (long)pid, argv[1]);
56 MPI_Recv(&msg, 1, MPI_INT, 0, 1, parent, MPI_STATUS_IGNORE);
57 printf("Child %d received msg: %d\n", rank, msg);
58 MPI_Comm_disconnect(&parent);
59 printf("Child %d disconnected\n", rank);
60 }
61
62 MPI_Finalize();
63 return 0;
64 }