root/orte/test/mpi/add_host.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     MPI_Info info;
  18 
  19     env_rank = getenv("PMIX_RANK");
  20     env_nspace = getenv("PMIX_NAMESPACE");
  21     pid = getpid();
  22     gethostname(hostname, sizeof(hostname));
  23 
  24     printf("[%s:%s pid %ld] starting up on node %s!\n", env_nspace, env_rank, (long)pid, hostname);
  25 
  26     MPI_Init(NULL, NULL);
  27     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  28     printf("%d completed MPI_Init\n", rank);
  29     MPI_Comm_size(MPI_COMM_WORLD, &size);
  30     MPI_Comm_get_parent(&parent);
  31     /* If we get COMM_NULL back, then we're the parent */
  32     if (MPI_COMM_NULL == parent) {
  33         pid = getpid();
  34         printf("Parent [pid %ld] about to spawn!\n", (long)pid);
  35         MPI_Info_create(&info);
  36         MPI_Info_set(info, "add-host", "rhc002:24");
  37         if (MPI_SUCCESS != (rc = MPI_Comm_spawn(argv[0], MPI_ARGV_NULL, 3, info,
  38                                                 0, MPI_COMM_WORLD, &child, MPI_ERRCODES_IGNORE))) {
  39             printf("Child failed to spawn\n");
  40             return rc;
  41         }
  42         printf("Parent done with spawn\n");
  43         if (0 == rank) {
  44             msg = 38;
  45             printf("Parent sending message to child\n");
  46             MPI_Send(&msg, 1, MPI_INT, 0, 1, child);
  47         }
  48         MPI_Comm_disconnect(&child);
  49         printf("Parent disconnected\n");
  50         /* do it again */
  51         MPI_Info_set(info, "add-host", "rhc003:24");
  52         if (MPI_SUCCESS != (rc = MPI_Comm_spawn(argv[0], MPI_ARGV_NULL, 3, info,
  53                                                 0, MPI_COMM_WORLD, &child, MPI_ERRCODES_IGNORE))) {
  54             printf("Child failed to spawn\n");
  55             return rc;
  56         }
  57         printf("Parent done with second spawn\n");
  58         if (0 == rank) {
  59             msg = 38;
  60             printf("Parent sending message to second children\n");
  61             MPI_Send(&msg, 1, MPI_INT, 0, 1, child);
  62         }
  63         MPI_Comm_disconnect(&child);
  64         printf("Parent disconnected again\n");
  65     }
  66     /* Otherwise, we're the child */
  67     else {
  68         MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  69         MPI_Comm_size(MPI_COMM_WORLD, &size);
  70         pid = getpid();
  71         printf("Hello from the child %d of %d on host %s pid %ld\n", rank, 3, hostname, (long)pid);
  72         if (0 == rank) {
  73             MPI_Recv(&msg, 1, MPI_INT, 0, 1, parent, MPI_STATUS_IGNORE);
  74             printf("Child %d received msg: %d\n", rank, msg);
  75         }
  76         MPI_Comm_disconnect(&parent);
  77         printf("Child %d disconnected\n", rank);
  78     }
  79 
  80     MPI_Finalize();
  81     fprintf(stderr, "%d: exiting\n", pid);
  82     return 0;
  83 }

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