root/orte/test/mpi/info_spawn.c

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

DEFINITIONS

This source file includes following definitions.
  1. main

   1 #include "orte_config.h"
   2 
   3 #include <stdio.h>
   4 #include <sys/types.h>
   5 #include <unistd.h>
   6 #include <string.h>
   7 #include <stdlib.h>
   8 
   9 #include <mpi.h>
  10 
  11 int main(int argc, char* argv[])
  12 {
  13     int msg, rc, i;
  14     MPI_Comm parent, child;
  15     int rank, size;
  16     char hostname[OPAL_MAXHOSTNAMELEN];
  17     pid_t pid;
  18     MPI_Info info;
  19     char *keyval, *tmp;
  20 
  21     pid = getpid();
  22     MPI_Init(NULL, NULL);
  23     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  24     MPI_Comm_size(MPI_COMM_WORLD, &size);
  25 
  26     MPI_Comm_get_parent(&parent);
  27     /* If we get COMM_NULL back, then we're the parent */
  28     if (MPI_COMM_NULL == parent) {
  29         if (argc < 2) {
  30             fprintf(stderr, "Usage: info_spawn key:value key:value...\n");
  31             exit(1);
  32         }
  33 
  34         pid = getpid();
  35         printf("Parent [pid %ld] about to spawn!\n", (long)pid);
  36         MPI_Info_create(&info);
  37         for (i=1; i < argc; i++) {
  38             tmp = strdup(argv[i]);
  39             keyval = strchr(tmp, ':');
  40             if (NULL == keyval) {
  41                 fprintf(stderr, "Missing colon separator in key-value\n");
  42                 exit(1);
  43             }
  44             *keyval = '\0';
  45             keyval++;
  46             fprintf(stderr, "SETTING %s TO %s\n", tmp, keyval);
  47             MPI_Info_set(info, tmp, keyval);
  48             free(tmp);
  49         }
  50         if (MPI_SUCCESS != (rc = MPI_Comm_spawn(argv[0], MPI_ARGV_NULL, 3, info,
  51                                                 0, MPI_COMM_WORLD, &child, MPI_ERRCODES_IGNORE))) {
  52             printf("Child failed to spawn\n");
  53             return rc;
  54         }
  55         printf("Parent done with spawn\n");
  56         if (0 == rank) {
  57             msg = 38;
  58             printf("Parent sending message to child\n");
  59             MPI_Send(&msg, 1, MPI_INT, 0, 1, child);
  60         }
  61         MPI_Comm_disconnect(&child);
  62         printf("Parent disconnected\n");
  63     }
  64     /* Otherwise, we're the child */
  65     else {
  66         MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  67         MPI_Comm_size(MPI_COMM_WORLD, &size);
  68         gethostname(hostname, sizeof(hostname));
  69         pid = getpid();
  70         printf("Hello from the child %d of %d on host %s pid %ld\n", rank, 3, hostname, (long)pid);
  71         if (0 == rank) {
  72             MPI_Recv(&msg, 1, MPI_INT, 0, 1, parent, MPI_STATUS_IGNORE);
  73             printf("Child %d received msg: %d\n", rank, msg);
  74         }
  75         MPI_Comm_disconnect(&parent);
  76         printf("Child %d disconnected\n", rank);
  77     }
  78 
  79     MPI_Finalize();
  80     fprintf(stderr, "%d: exiting\n", pid);
  81     return 0;
  82 }

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