root/opal/mca/pmix/pmix4x/pmix/examples/pub.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-2018 Intel, Inc. All rights reserved.
  17  * Copyright (c) 2015      Mellanox Technologies, Inc.  All rights reserved.
  18  * $COPYRIGHT$
  19  *
  20  * Additional copyrights may follow
  21  *
  22  * $HEADER$
  23  *
  24  */
  25 
  26 #include <stdbool.h>
  27 #include <stdio.h>
  28 #include <stdlib.h>
  29 #include <unistd.h>
  30 #include <time.h>
  31 
  32 #include <pmix.h>
  33 
  34 int main(int argc, char **argv)
  35 {
  36     pmix_proc_t myproc;
  37     int rc;
  38     pmix_value_t value;
  39     pmix_value_t *val = &value;
  40     pmix_proc_t proc;
  41     uint32_t nprocs;
  42     pmix_info_t *info;
  43     pmix_pdata_t *pdata;
  44 
  45     /* init us */
  46     if (PMIX_SUCCESS != (rc = PMIx_Init(&myproc, NULL, 0))) {
  47         fprintf(stderr, "Client ns %s rank %d: PMIx_Init failed: %d\n", myproc.nspace, myproc.rank, rc);
  48         exit(0);
  49     }
  50     fprintf(stderr, "Client ns %s rank %d: Running\n", myproc.nspace, myproc.rank);
  51 
  52     /* get our universe size */
  53     PMIX_PROC_CONSTRUCT(&proc);
  54     (void)strncpy(proc.nspace, myproc.nspace, PMIX_MAX_NSLEN);
  55     proc.rank = PMIX_RANK_WILDCARD;
  56     if (PMIX_SUCCESS != (rc = PMIx_Get(&proc, PMIX_UNIV_SIZE, NULL, 0, &val))) {
  57         fprintf(stderr, "Client ns %s rank %d: PMIx_Get universe size failed: %d\n", myproc.nspace, myproc.rank, rc);
  58         goto done;
  59     }
  60     nprocs = val->data.uint32;
  61     PMIX_VALUE_RELEASE(val);
  62     fprintf(stderr, "Client %s:%d universe size %d\n", myproc.nspace, myproc.rank, nprocs);
  63 
  64     /* call fence to ensure the data is received */
  65     if (PMIX_SUCCESS != (rc = PMIx_Fence(&proc, 1, NULL, 0))) {
  66         fprintf(stderr, "Client ns %s rank %d: PMIx_Fence failed: %d\n", myproc.nspace, myproc.rank, rc);
  67         goto done;
  68     }
  69 
  70     /* publish something */
  71     if (0 == myproc.rank) {
  72         fprintf(stderr, "%s:%d publishing two keys\n", myproc.nspace, myproc.rank);
  73         PMIX_INFO_CREATE(info, 2);
  74         (void)strncpy(info[0].key, "FOOBAR", PMIX_MAX_KEYLEN);
  75         info[0].value.type = PMIX_UINT8;
  76         info[0].value.data.uint8 = 1;
  77         (void)strncpy(info[1].key, "PANDA", PMIX_MAX_KEYLEN);
  78         info[1].value.type = PMIX_SIZE;
  79         info[1].value.data.size = 123456;
  80         if (PMIX_SUCCESS != (rc = PMIx_Publish(info, 2))) {
  81             fprintf(stderr, "Client ns %s rank %d: PMIx_Publish failed: %d\n", myproc.nspace, myproc.rank, rc);
  82             goto done;
  83         }
  84         fprintf(stderr, "%s:%d publish complete\n", myproc.nspace, myproc.rank);
  85         PMIX_INFO_FREE(info, 2);
  86     }
  87 
  88     /* call fence again so all procs know the data
  89      * has been published */
  90     if (PMIX_SUCCESS != (rc = PMIx_Fence(&proc, 1, NULL, 0))) {
  91         fprintf(stderr, "Client ns %s rank %d: PMIx_Fence failed: %d\n", myproc.nspace, myproc.rank, rc);
  92         goto done;
  93     }
  94 
  95     /* lookup something */
  96     if (0 != myproc.rank) {
  97         PMIX_PDATA_CREATE(pdata, 1);
  98         fprintf(stderr, "%s:%d looking up key FOOBAR\n", myproc.nspace, myproc.rank);
  99         (void)strncpy(pdata[0].key, "FOOBAR", PMIX_MAX_KEYLEN);
 100         if (PMIX_SUCCESS != (rc = PMIx_Lookup(pdata, 1, NULL, 0))) {
 101             fprintf(stderr, "Client ns %s rank %d: PMIx_Lookup failed: %d\n", myproc.nspace, myproc.rank, rc);
 102             goto done;
 103         }
 104         /* check the return for value and source */
 105         if (0 != strncmp(myproc.nspace, pdata[0].proc.nspace, PMIX_MAX_NSLEN)) {
 106             fprintf(stderr, "Client ns %s rank %d: PMIx_Lookup returned wrong nspace: %s\n",
 107                         myproc.nspace, myproc.rank, pdata[0].proc.nspace);
 108             goto done;
 109         }
 110         if (0 != pdata[0].proc.rank) {
 111             fprintf(stderr, "Client ns %s rank %d: PMIx_Lookup returned wrong rank: %d\n",
 112                         myproc.nspace, myproc.rank, pdata[0].proc.rank);
 113             goto done;
 114         }
 115         if (PMIX_UINT8 != pdata[0].value.type) {
 116             fprintf(stderr, "Client ns %s rank %d: PMIx_Lookup returned wrong type: %d\n",
 117                         myproc.nspace, myproc.rank, pdata[0].value.type);
 118             goto done;
 119         }
 120         if (1 != pdata[0].value.data.uint8) {
 121             fprintf(stderr, "Client ns %s rank %d: PMIx_Lookup returned wrong value: %d\n",
 122                         myproc.nspace, myproc.rank, (int)pdata[0].value.data.uint8);
 123             goto done;
 124         }
 125         PMIX_PDATA_FREE(pdata, 1);
 126         fprintf(stderr, "PUBLISH-LOOKUP SUCCEEDED\n");
 127     }
 128 
 129     /* call fence again so rank 0 waits before leaving */
 130     if (PMIX_SUCCESS != (rc = PMIx_Fence(&proc, 1, NULL, 0))) {
 131         fprintf(stderr, "Client ns %s rank %d: PMIx_Fence failed: %d\n", myproc.nspace, myproc.rank, rc);
 132         goto done;
 133     }
 134 
 135     if (0 == myproc.rank) {
 136         char **keys;
 137         keys = (char**)malloc(3 * sizeof(char*));
 138         keys[0] = "FOOBAR";
 139         keys[1] = "PANDA";
 140         keys[2] = NULL;
 141 
 142         fprintf(stderr, "%s:%d unpublishing two keys\n", myproc.nspace, myproc.rank);
 143         if (PMIX_SUCCESS != (rc = PMIx_Unpublish(keys, NULL, 0))) {
 144             fprintf(stderr, "Client ns %s rank %d: PMIx_Unpublish failed: %d\n", myproc.nspace, myproc.rank, rc);
 145             free(keys);
 146             goto done;
 147         }
 148         free(keys);
 149         fprintf(stderr, "UNPUBLISH SUCCEEDED\n");
 150     }
 151 
 152     /* call fence again so everyone waits for rank 0 before leaving */
 153     if (PMIX_SUCCESS != (rc = PMIx_Fence(&proc, 1, NULL, 0))) {
 154         fprintf(stderr, "Client ns %s rank %d: PMIx_Fence failed: %d\n", myproc.nspace, myproc.rank, rc);
 155         goto done;
 156     }
 157 
 158  done:
 159     /* finalize us */
 160     fprintf(stderr, "Client ns %s rank %d: Finalizing\n", myproc.nspace, myproc.rank);
 161     if (PMIX_SUCCESS != (rc = PMIx_Finalize(NULL, 0))) {
 162         fprintf(stderr, "Client ns %s rank %d:PMIx_Finalize failed: %d\n", myproc.nspace, myproc.rank, rc);
 163     } else {
 164         fprintf(stderr, "Client ns %s rank %d:PMIx_Finalize successfully completed\n", myproc.nspace, myproc.rank);
 165     }
 166     fflush(stderr);
 167     return(0);
 168 }

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