root/opal/mca/pmix/pmix4x/pmix/examples/dynamic.c

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

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /*
   2  * Copyright (c) 2004-2010 The Trustees of Indiana University and Indiana
   3  *                         University Research and Technology
   4  *                         Corporation.  All rights reserved.
   5  * Copyright (c) 2004-2011 The University of Tennessee and The University
   6  *                         of Tennessee Research Foundation.  All rights
   7  *                         reserved.
   8  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
   9  *                         University of Stuttgart.  All rights reserved.
  10  * Copyright (c) 2004-2005 The Regents of the University of California.
  11  *                         All rights reserved.
  12  * Copyright (c) 2006-2013 Los Alamos National Security, LLC.
  13  *                         All rights reserved.
  14  * Copyright (c) 2009-2012 Cisco Systems, Inc.  All rights reserved.
  15  * Copyright (c) 2011      Oak Ridge National Labs.  All rights reserved.
  16  * Copyright (c) 2013-2019 Intel, Inc.  All rights reserved.
  17  * Copyright (c) 2015      Mellanox Technologies, Inc.  All rights reserved.
  18  * Copyright (c) 2016      Research Organization for Information Science
  19  *                         and Technology (RIST). All rights reserved.
  20  * $COPYRIGHT$
  21  *
  22  * Additional copyrights may follow
  23  *
  24  * $HEADER$
  25  *
  26  */
  27 
  28 #include <stdbool.h>
  29 
  30 #define _GNU_SOURCE
  31 #include <stdio.h>
  32 #include <stdlib.h>
  33 #include <unistd.h>
  34 #include <time.h>
  35 #include <sys/param.h>
  36 
  37 #include <pmix.h>
  38 #include "examples.h"
  39 
  40 static pmix_proc_t myproc;
  41 
  42 int main(int argc, char **argv)
  43 {
  44     int rc;
  45     pmix_value_t value;
  46     pmix_value_t *val = &value;
  47     pmix_proc_t proc;
  48     uint32_t nprocs;
  49     char nsp2[PMIX_MAX_NSLEN+1];
  50     pmix_app_t *app;
  51     char hostname[1024], dir[1024];
  52     pmix_proc_t *peers;
  53     size_t npeers, ntmp=0;
  54     char *nodelist;
  55 
  56     if (0 > gethostname(hostname, sizeof(hostname))) {
  57         exit(1);
  58     }
  59     if (NULL == getcwd(dir, 1024)) {
  60         exit(1);
  61     }
  62 
  63     /* init us */
  64     if (PMIX_SUCCESS != (rc = PMIx_Init(&myproc, NULL, 0))) {
  65         fprintf(stderr, "Client ns %s rank %d: PMIx_Init failed: %d\n", myproc.nspace, myproc.rank, rc);
  66         exit(0);
  67     }
  68     fprintf(stderr, "Client ns %s rank %d: Running\n", myproc.nspace, myproc.rank);
  69 
  70     PMIX_PROC_CONSTRUCT(&proc);
  71     (void)strncpy(proc.nspace, myproc.nspace, PMIX_MAX_NSLEN);
  72     proc.rank = PMIX_RANK_WILDCARD;
  73 
  74     /* get our universe size */
  75     if (PMIX_SUCCESS != (rc = PMIx_Get(&proc, PMIX_UNIV_SIZE, NULL, 0, &val))) {
  76         fprintf(stderr, "Client ns %s rank %d: PMIx_Get universe size failed: %d\n", myproc.nspace, myproc.rank, rc);
  77         goto done;
  78     }
  79     nprocs = val->data.uint32;
  80     PMIX_VALUE_RELEASE(val);
  81     fprintf(stderr, "Client %s:%d universe size %d\n", myproc.nspace, myproc.rank, nprocs);
  82 
  83     /* call fence to sync */
  84     (void)strncpy(proc.nspace, myproc.nspace, PMIX_MAX_NSLEN);
  85     proc.rank = PMIX_RANK_WILDCARD;
  86     if (PMIX_SUCCESS != (rc = PMIx_Fence(&proc, 1, NULL, 0))) {
  87         fprintf(stderr, "Client ns %s rank %d: PMIx_Fence failed: %d\n", myproc.nspace, myproc.rank, rc);
  88         goto done;
  89     }
  90 
  91     /* rank=0 calls spawn */
  92     if (0 == myproc.rank) {
  93         PMIX_APP_CREATE(app, 1);
  94         if (0 > asprintf(&app->cmd, "%s/client", dir)) {
  95             exit(1);
  96         }
  97         app->maxprocs = 2;
  98         app->argv = (char**)malloc(2 * sizeof(char*));
  99         if (0 > asprintf(&app->argv[0], "%s/client", dir)) {
 100             exit(1);
 101         }
 102         app->argv[1] = NULL;
 103         app->env = (char**)malloc(2 * sizeof(char*));
 104         app->env[0] = strdup("PMIX_ENV_VALUE=3");
 105         app->env[1] = NULL;
 106         PMIX_INFO_CREATE(app->info, 2);
 107         (void)strncpy(app->info[0].key, "DARTH", PMIX_MAX_KEYLEN);
 108         app->info[0].value.type = PMIX_INT8;
 109         app->info[0].value.data.int8 = 12;
 110         (void)strncpy(app->info[1].key, "VADER", PMIX_MAX_KEYLEN);
 111         app->info[1].value.type = PMIX_DOUBLE;
 112         app->info[1].value.data.dval = 12.34;
 113 
 114         fprintf(stderr, "Client ns %s rank %d: calling PMIx_Spawn\n", myproc.nspace, myproc.rank);
 115         if (PMIX_SUCCESS != (rc = PMIx_Spawn(NULL, 0, app, 1, nsp2))) {
 116             fprintf(stderr, "Client ns %s rank %d: PMIx_Spawn failed: %d\n", myproc.nspace, myproc.rank, rc);
 117             goto done;
 118         }
 119         PMIX_APP_FREE(app, 1);
 120 
 121         /* get their universe size */
 122         val = NULL;
 123         (void)strncpy(proc.nspace, nsp2, PMIX_MAX_NSLEN);
 124         proc.rank = PMIX_RANK_WILDCARD;
 125         if (PMIX_SUCCESS != (rc = PMIx_Get(&proc, PMIX_UNIV_SIZE, NULL, 0, &val)) ||
 126             NULL == val) {
 127             fprintf(stderr, "Client ns %s rank %d: PMIx_Get universe size failed: %d\n", myproc.nspace, myproc.rank, rc);
 128             goto done;
 129         }
 130         ntmp = val->data.uint32;
 131         PMIX_VALUE_RELEASE(val);
 132         fprintf(stderr, "Client %s:%d universe %s size %d\n", myproc.nspace, myproc.rank, nsp2, (int)ntmp);
 133     }
 134 
 135     /* just cycle the connect/disconnect functions */
 136     (void)strncpy(proc.nspace, myproc.nspace, PMIX_MAX_NSLEN);
 137     proc.rank = PMIX_RANK_WILDCARD;
 138     if (PMIX_SUCCESS != (rc = PMIx_Connect(&proc, 1, NULL, 0))) {
 139         fprintf(stderr, "Client ns %s rank %d: PMIx_Connect failed: %d\n", myproc.nspace, myproc.rank, rc);
 140         goto done;
 141     }
 142     fprintf(stderr, "Client ns %s rank %d: PMIx_Connect succeeded\n",
 143             myproc.nspace, myproc.rank);
 144     if (PMIX_SUCCESS != (rc = PMIx_Disconnect(&proc, 1, NULL, 0))) {
 145         fprintf(stderr, "Client ns %s rank %d: PMIx_Disonnect failed: %d\n", myproc.nspace, myproc.rank, rc);
 146         goto done;
 147     }
 148     fprintf(stderr, "Client ns %s rank %d: PMIx_Disconnect succeeded\n", myproc.nspace, myproc.rank);
 149 
 150     /* finally, test the resolve functions */
 151     if (0 == myproc.rank) {
 152         if (PMIX_SUCCESS != (rc = PMIx_Resolve_peers(hostname, NULL, &peers, &npeers))) {
 153             fprintf(stderr, "Client ns %s rank %d: PMIx_Resolve_peers failed for nspace %s: %d\n", myproc.nspace, myproc.rank, nsp2, rc);
 154             goto done;
 155         }
 156         if ((nprocs+ntmp) != npeers) {
 157             fprintf(stderr, "Client ns %s rank %d: PMIx_Resolve_peers returned incorrect npeers: %d vs %d\n", myproc.nspace, myproc.rank, (int)(nprocs+ntmp), (int)npeers);
 158             goto done;
 159         }
 160         fprintf(stderr, "Client ns %s rank %d: PMIx_Resolve_peers returned %d npeers\n", myproc.nspace, myproc.rank, (int)npeers);
 161         if (PMIX_SUCCESS != (rc = PMIx_Resolve_nodes(nsp2, &nodelist))) {
 162             fprintf(stderr, "Client ns %s rank %d: PMIx_Resolve_nodes failed for nspace %s: %d\n", myproc.nspace, myproc.rank, nsp2, rc);
 163             goto done;
 164         }
 165         fprintf(stderr, "Client ns %s rank %d: PMIx_Resolve_nodes %s", myproc.nspace, myproc.rank, nodelist);
 166     } else {
 167         if (PMIX_SUCCESS != (rc = PMIx_Resolve_peers(hostname, myproc.nspace, &peers, &npeers))) {
 168             fprintf(stderr, "Client ns %s rank %d: PMIx_Resolve_peers failed for nspace %s: %d\n", myproc.nspace, myproc.rank, myproc.nspace, rc);
 169             goto done;
 170         }
 171         if (nprocs != npeers) {
 172             fprintf(stderr, "Client ns %s rank %d: PMIx_Resolve_peers returned incorrect npeers: %d vs %d\n", myproc.nspace, myproc.rank, nprocs, (int)npeers);
 173             goto done;
 174         }
 175         fprintf(stderr, "Client ns %s rank %d: PMIx_Resolve_peers returned %d npeers\n", myproc.nspace, myproc.rank, (int)npeers);
 176         if (PMIX_SUCCESS != (rc = PMIx_Resolve_nodes(myproc.nspace, &nodelist))) {
 177             fprintf(stderr, "Client ns %s rank %d: PMIx_Resolve_nodes failed: %d\n", myproc.nspace, myproc.rank, rc);
 178             goto done;
 179         }
 180         fprintf(stderr, "Client ns %s rank %d: PMIx_Resolve_nodes %s\n", myproc.nspace, myproc.rank, nodelist);
 181     }
 182     PMIX_PROC_FREE(peers, npeers);
 183     free(nodelist);
 184 
 185  done:
 186     /* call fence to sync */
 187     (void)strncpy(proc.nspace, myproc.nspace, PMIX_MAX_NSLEN);
 188     proc.rank = PMIX_RANK_WILDCARD;
 189     if (PMIX_SUCCESS != (rc = PMIx_Fence(&proc, 1, NULL, 0))) {
 190         fprintf(stderr, "Client ns %s rank %d: PMIx_Fence failed: %d\n", myproc.nspace, myproc.rank, rc);
 191         goto done;
 192     }
 193 
 194     /* finalize us */
 195     fprintf(stderr, "Client ns %s rank %d: Finalizing\n", myproc.nspace, myproc.rank);
 196 
 197     if (PMIX_SUCCESS != (rc = PMIx_Finalize(NULL, 0))) {
 198         fprintf(stderr, "Client ns %s rank %d:PMIx_Finalize failed: %d\n", myproc.nspace, myproc.rank, rc);
 199     } else {
 200         fprintf(stderr, "Client ns %s rank %d:PMIx_Finalize successfully completed\n", myproc.nspace, myproc.rank);
 201     }
 202     fflush(stderr);
 203     return(0);
 204 }

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