root/orte/test/mpi/simple_spawn.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main

   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 <mpi.h>
   8 
   9 int main(int argc, char* argv[])
  10 {
  11     int msg, rc;
  12     MPI_Comm parent, child;
  13     int rank, size;
  14     char hostname[MAXHOSTNAMELEN];
  15     pid_t pid;
  16     char *env_rank,*env_nspace;
  17 
  18     env_rank = getenv("PMIX_RANK");
  19     env_nspace = getenv("PMIX_NAMESPACE");
  20     pid = getpid();
  21     gethostname(hostname, sizeof(hostname));
  22 
  23     printf("[%s:%s pid %ld] starting up on node %s!\n", env_nspace, env_rank, (long)pid, hostname);
  24 
  25     MPI_Init(NULL, NULL);
  26     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  27     printf("%d completed MPI_Init\n", 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         if (MPI_SUCCESS != (rc = MPI_Comm_spawn(argv[0], MPI_ARGV_NULL, 3, MPI_INFO_NULL,
  35                                                 0, MPI_COMM_WORLD, &child, MPI_ERRCODES_IGNORE))) {
  36             printf("Child failed to spawn\n");
  37             return rc;
  38         }
  39         printf("Parent done with spawn\n");
  40         if (0 == rank) {
  41             msg = 38;
  42             printf("Parent sending message to child\n");
  43             MPI_Send(&msg, 1, MPI_INT, 0, 1, child);
  44         }
  45         MPI_Comm_disconnect(&child);
  46         printf("Parent disconnected\n");
  47     }
  48     /* Otherwise, we're the child */
  49     else {
  50         MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  51         MPI_Comm_size(MPI_COMM_WORLD, &size);
  52         pid = getpid();
  53         printf("Hello from the child %d of %d on host %s pid %ld\n", rank, 3, hostname, (long)pid);
  54         if (0 == rank) {
  55             MPI_Recv(&msg, 1, MPI_INT, 0, 1, parent, MPI_STATUS_IGNORE);
  56             printf("Child %d received msg: %d\n", rank, msg);
  57         }
  58         MPI_Comm_disconnect(&parent);
  59         printf("Child %d disconnected\n", rank);
  60     }
  61 
  62     MPI_Finalize();
  63     fprintf(stderr, "%d: exiting\n", pid);
  64     return 0;
  65 }

/* [<][>][^][v][top][bottom][index][help] */