6
int main(int argc, char* argv[])
9
MPI_Comm parent, child;
15
printf("Parent [pid %ld] starting up!\n", (long)pid);
17
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
18
printf("%d completed MPI_Init\n", rank);
19
MPI_Comm_size(MPI_COMM_WORLD, &size);
20
MPI_Comm_get_parent(&parent);
21
/* If we get COMM_NULL back, then we're the parent */
22
if (MPI_COMM_NULL == parent) {
24
printf("Parent [pid %ld] about to spawn!\n", (long)pid);
25
if (MPI_SUCCESS != (rc = MPI_Comm_spawn(argv[0], MPI_ARGV_NULL, 3, MPI_INFO_NULL,
26
0, MPI_COMM_WORLD, &child, MPI_ERRCODES_IGNORE))) {
27
printf("Child failed to spawn\n");
30
printf("Parent done with spawn\n");
33
printf("Parent sending message to child\n");
34
MPI_Send(&msg, 1, MPI_INT, 0, 1, child);
36
MPI_Comm_disconnect(&child);
37
printf("Parent disconnected\n");
39
/* Otherwise, we're the child */
41
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
42
MPI_Comm_size(MPI_COMM_WORLD, &size);
43
gethostname(hostname, 512);
45
printf("Hello from the child %d of %d on host %s pid %ld\n", rank, 3, hostname, (long)pid);
47
MPI_Recv(&msg, 1, MPI_INT, 0, 1, parent, MPI_STATUS_IGNORE);
48
printf("Child %d received msg: %d\n", rank, msg);
50
MPI_Comm_disconnect(&parent);
51
printf("Child %d disconnected\n", rank);