|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <sys/types.h> |
| 4 | +#include <unistd.h> |
| 5 | +#include <sys/param.h> |
| 6 | + |
| 7 | +#include "opal/runtime/opal.h" |
| 8 | + |
| 9 | +#include <mpi.h> |
| 10 | + |
| 11 | +int main(int argc, char* argv[]) |
| 12 | +{ |
| 13 | + int msg, rc; |
| 14 | + MPI_Comm parent, child; |
| 15 | + int rank, size; |
| 16 | + const char *hostname; |
| 17 | + pid_t pid; |
| 18 | + char *env_rank,*env_nspace; |
| 19 | + MPI_Info info; |
| 20 | + |
| 21 | + env_rank = getenv("PMIX_RANK"); |
| 22 | + env_nspace = getenv("PMIX_NAMESPACE"); |
| 23 | + pid = getpid(); |
| 24 | + hostname = opal_gethostname(); |
| 25 | + |
| 26 | + printf("[%s:%s pid %ld] starting up on node %s!\n", env_nspace, env_rank, (long)pid, hostname); |
| 27 | + |
| 28 | + MPI_Init(NULL, NULL); |
| 29 | + MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 30 | + printf("%d completed MPI_Init\n", rank); |
| 31 | + MPI_Comm_size(MPI_COMM_WORLD, &size); |
| 32 | + MPI_Comm_get_parent(&parent); |
| 33 | + /* If we get COMM_NULL back, then we're the parent */ |
| 34 | + if (MPI_COMM_NULL == parent) { |
| 35 | + pid = getpid(); |
| 36 | + printf("Parent [pid %ld] about to spawn!\n", (long)pid); |
| 37 | + MPI_Info_create(&info); |
| 38 | + MPI_Info_set(info, "add-host", "rhc002:24"); |
| 39 | + if (MPI_SUCCESS != (rc = MPI_Comm_spawn(argv[0], MPI_ARGV_NULL, 3, info, |
| 40 | + 0, MPI_COMM_WORLD, &child, MPI_ERRCODES_IGNORE))) { |
| 41 | + printf("Child failed to spawn\n"); |
| 42 | + return rc; |
| 43 | + } |
| 44 | + printf("Parent done with spawn\n"); |
| 45 | + if (0 == rank) { |
| 46 | + msg = 38; |
| 47 | + printf("Parent sending message to child\n"); |
| 48 | + MPI_Send(&msg, 1, MPI_INT, 0, 1, child); |
| 49 | + } |
| 50 | + MPI_Comm_disconnect(&child); |
| 51 | + printf("Parent disconnected\n"); |
| 52 | + /* do it again */ |
| 53 | + MPI_Info_set(info, "add-host", "rhc003:24"); |
| 54 | + if (MPI_SUCCESS != (rc = MPI_Comm_spawn(argv[0], MPI_ARGV_NULL, 3, info, |
| 55 | + 0, MPI_COMM_WORLD, &child, MPI_ERRCODES_IGNORE))) { |
| 56 | + printf("Child failed to spawn\n"); |
| 57 | + return rc; |
| 58 | + } |
| 59 | + printf("Parent done with second spawn\n"); |
| 60 | + if (0 == rank) { |
| 61 | + msg = 38; |
| 62 | + printf("Parent sending message to second children\n"); |
| 63 | + MPI_Send(&msg, 1, MPI_INT, 0, 1, child); |
| 64 | + } |
| 65 | + MPI_Comm_disconnect(&child); |
| 66 | + printf("Parent disconnected again\n"); |
| 67 | + } |
| 68 | + /* Otherwise, we're the child */ |
| 69 | + else { |
| 70 | + MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 71 | + MPI_Comm_size(MPI_COMM_WORLD, &size); |
| 72 | + pid = getpid(); |
| 73 | + printf("Hello from the child %d of %d on host %s pid %ld\n", rank, 3, hostname, (long)pid); |
| 74 | + if (0 == rank) { |
| 75 | + MPI_Recv(&msg, 1, MPI_INT, 0, 1, parent, MPI_STATUS_IGNORE); |
| 76 | + printf("Child %d received msg: %d\n", rank, msg); |
| 77 | + } |
| 78 | + MPI_Comm_disconnect(&parent); |
| 79 | + printf("Child %d disconnected\n", rank); |
| 80 | + } |
| 81 | + |
| 82 | + MPI_Finalize(); |
| 83 | + fprintf(stderr, "%d: exiting\n", pid); |
| 84 | + return 0; |
| 85 | +} |
0 commit comments