|
| 1 | +#include <stdio.h> |
| 2 | +#include <sys/types.h> |
| 3 | +#include <unistd.h> |
| 4 | + |
| 5 | +#include <mpi.h> |
| 6 | + |
| 7 | +int main(int argc, char* argv[]) |
| 8 | +{ |
| 9 | + int msg; |
| 10 | + MPI_Comm parent, child; |
| 11 | + int rank, size; |
| 12 | + char hostname[1024]; |
| 13 | + pid_t pid; |
| 14 | + int i; |
| 15 | + char *cmds[2]; |
| 16 | + char *argv0[] = { "foo", NULL }; |
| 17 | + char *argv1[] = { "bar", NULL }; |
| 18 | + char **spawn_argv[2]; |
| 19 | + int maxprocs[] = { 2, 2 }; |
| 20 | + MPI_Info info[] = { MPI_INFO_NULL, MPI_INFO_NULL }; |
| 21 | + |
| 22 | + cmds[1] = cmds[0] = argv[0]; |
| 23 | + spawn_argv[0] = argv0; |
| 24 | + spawn_argv[1] = argv1; |
| 25 | + |
| 26 | + MPI_Init(NULL, NULL); |
| 27 | + MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 28 | + MPI_Comm_size(MPI_COMM_WORLD, &size); |
| 29 | + MPI_Comm_get_parent(&parent); |
| 30 | + /* If we get COMM_NULL back, then we're the parent */ |
| 31 | + if (MPI_COMM_NULL == parent) { |
| 32 | + pid = getpid(); |
| 33 | + printf("Parent [pid %ld] about to spawn!\n", (long)pid); |
| 34 | + MPI_Comm_spawn_multiple(2, cmds, spawn_argv, maxprocs, |
| 35 | + info, 0, MPI_COMM_WORLD, |
| 36 | + &child, MPI_ERRCODES_IGNORE); |
| 37 | + printf("Parent done with spawn\n"); |
| 38 | + MPI_Comm_disconnect(&child); |
| 39 | + printf("Parent disconnected\n"); |
| 40 | + } |
| 41 | + /* Otherwise, we're the child */ |
| 42 | + else { |
| 43 | + MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 44 | + MPI_Comm_size(MPI_COMM_WORLD, &size); |
| 45 | + gethostname(hostname, sizeof(hostname)); |
| 46 | + pid = getpid(); |
| 47 | + printf("Hello from the child %d of %d on host %s pid %ld: argv[1] = %s\n", rank, size, hostname, (long)pid, argv[1]); |
| 48 | + MPI_Comm_disconnect(&parent); |
| 49 | + printf("Child %d disconnected\n", rank); |
| 50 | + } |
| 51 | + |
| 52 | + MPI_Finalize(); |
| 53 | + return 0; |
| 54 | +} |
0 commit comments