|
| 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-2017 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 | +#define _GNU_SOURCE |
| 27 | +#include <stdbool.h> |
| 28 | +#include <stdio.h> |
| 29 | +#include <stdlib.h> |
| 30 | +#include <unistd.h> |
| 31 | +#include <time.h> |
| 32 | + |
| 33 | +#include <pmix.h> |
| 34 | + |
| 35 | +static pmix_proc_t myproc; |
| 36 | + |
| 37 | +/* this is the event notification function we pass down below |
| 38 | + * when registering for general events - i.e.,, the default |
| 39 | + * handler. We don't technically need to register one, but it |
| 40 | + * is usually good practice to catch any events that occur */ |
| 41 | +static void notification_fn(size_t evhdlr_registration_id, |
| 42 | + pmix_status_t status, |
| 43 | + const pmix_proc_t *source, |
| 44 | + pmix_info_t info[], size_t ninfo, |
| 45 | + pmix_info_t results[], size_t nresults, |
| 46 | + pmix_event_notification_cbfunc_fn_t cbfunc, |
| 47 | + void *cbdata) |
| 48 | +{ |
| 49 | + if (NULL != cbfunc) { |
| 50 | + cbfunc(PMIX_EVENT_ACTION_COMPLETE, NULL, 0, NULL, NULL, cbdata); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/* event handler registration is done asynchronously because it |
| 55 | + * may involve the PMIx server registering with the host RM for |
| 56 | + * external events. So we provide a callback function that returns |
| 57 | + * the status of the request (success or an error), plus a numerical index |
| 58 | + * to the registered event. The index is used later on to deregister |
| 59 | + * an event handler - if we don't explicitly deregister it, then the |
| 60 | + * PMIx server will do so when it see us exit */ |
| 61 | +static void evhandler_reg_callbk(pmix_status_t status, |
| 62 | + size_t evhandler_ref, |
| 63 | + void *cbdata) |
| 64 | +{ |
| 65 | + volatile int *active = (volatile int*)cbdata; |
| 66 | + |
| 67 | + if (PMIX_SUCCESS != status) { |
| 68 | + fprintf(stderr, "Client %s:%d EVENT HANDLER REGISTRATION FAILED WITH STATUS %d, ref=%lu\n", |
| 69 | + myproc.nspace, myproc.rank, status, (unsigned long)evhandler_ref); |
| 70 | + } |
| 71 | + *active = status; |
| 72 | +} |
| 73 | + |
| 74 | +int main(int argc, char **argv) |
| 75 | +{ |
| 76 | + int rc; |
| 77 | + pmix_value_t value; |
| 78 | + pmix_value_t *val, *vptr; |
| 79 | + pmix_proc_t proc; |
| 80 | + uint32_t nprocs, n, k; |
| 81 | + pmix_info_t *info; |
| 82 | + bool flag; |
| 83 | + volatile int active; |
| 84 | + pmix_data_array_t da, *dptr; |
| 85 | + |
| 86 | + /* init us - note that the call to "init" includes the return of |
| 87 | + * any job-related info provided by the RM. This includes any |
| 88 | + * debugger flag instructing us to stop-in-init. If such a directive |
| 89 | + * is included, then the process will be stopped in this call until |
| 90 | + * the "debugger release" notification arrives */ |
| 91 | + if (PMIX_SUCCESS != (rc = PMIx_Init(&myproc, NULL, 0))) { |
| 92 | + fprintf(stderr, "Client ns %s rank %d: PMIx_Init failed: %d\n", myproc.nspace, myproc.rank, rc); |
| 93 | + exit(0); |
| 94 | + } |
| 95 | + fprintf(stderr, "Client ns %s rank %d: Running\n", myproc.nspace, myproc.rank); |
| 96 | + |
| 97 | + |
| 98 | + /* register our default event handler - again, this isn't strictly |
| 99 | + * required, but is generally good practice */ |
| 100 | + active = -1; |
| 101 | + PMIx_Register_event_handler(NULL, 0, NULL, 0, |
| 102 | + notification_fn, evhandler_reg_callbk, (void*)&active); |
| 103 | + while (-1 == active) { |
| 104 | + sleep(1); |
| 105 | + } |
| 106 | + if (0 != active) { |
| 107 | + fprintf(stderr, "[%s:%d] Default handler registration failed\n", myproc.nspace, myproc.rank); |
| 108 | + exit(active); |
| 109 | + } |
| 110 | + |
| 111 | + /* job-related info is found in our nspace, assigned to the |
| 112 | + * wildcard rank as it doesn't relate to a specific rank. Setup |
| 113 | + * a name to retrieve such values */ |
| 114 | + PMIX_PROC_CONSTRUCT(&proc); |
| 115 | + (void)strncpy(proc.nspace, myproc.nspace, PMIX_MAX_NSLEN); |
| 116 | + proc.rank = PMIX_RANK_WILDCARD; |
| 117 | + |
| 118 | + /* get our job size */ |
| 119 | + if (PMIX_SUCCESS != (rc = PMIx_Get(&proc, PMIX_JOB_SIZE, NULL, 0, &val))) { |
| 120 | + fprintf(stderr, "Client ns %s rank %d: PMIx_Get universe size failed: %d\n", myproc.nspace, myproc.rank, rc); |
| 121 | + goto done; |
| 122 | + } |
| 123 | + nprocs = val->data.uint32; |
| 124 | + PMIX_VALUE_RELEASE(val); |
| 125 | + fprintf(stderr, "Client %s:%d job size %d\n", myproc.nspace, myproc.rank, nprocs); |
| 126 | + |
| 127 | + /* put a data array of pmix_value's */ |
| 128 | + val = (pmix_value_t*)malloc(32 * sizeof(pmix_value_t)); |
| 129 | + for (n=0; n < 32; n++) { |
| 130 | + val[n].type = PMIX_UINT64; |
| 131 | + val[n].data.uint64 = 2*n; |
| 132 | + } |
| 133 | + da.type = PMIX_VALUE; |
| 134 | + da.size = 32; |
| 135 | + da.array = val; |
| 136 | + value.type = PMIX_DATA_ARRAY; |
| 137 | + value.data.darray = &da; |
| 138 | + rc = PMIx_Put(PMIX_GLOBAL, "test-key", &value); |
| 139 | + if (PMIX_SUCCESS != rc) { |
| 140 | + fprintf(stderr, "Client ns %s rank %d: PMIx_Put failed: %d\n", myproc.nspace, myproc.rank, rc); |
| 141 | + goto done; |
| 142 | + } |
| 143 | + free(val); |
| 144 | + |
| 145 | + /* push the data to our PMIx server */ |
| 146 | + if (PMIX_SUCCESS != (rc = PMIx_Commit())) { |
| 147 | + fprintf(stderr, "Client ns %s rank %d: PMIx_Commit failed: %d\n", myproc.nspace, myproc.rank, rc); |
| 148 | + goto done; |
| 149 | + } |
| 150 | + |
| 151 | + /* call fence to synchronize with our peers - instruct |
| 152 | + * the fence operation to collect and return all "put" |
| 153 | + * data from our peers */ |
| 154 | + PMIX_INFO_CREATE(info, 1); |
| 155 | + flag = true; |
| 156 | + PMIX_INFO_LOAD(info, PMIX_COLLECT_DATA, &flag, PMIX_BOOL); |
| 157 | + if (PMIX_SUCCESS != (rc = PMIx_Fence(&proc, 1, info, 1))) { |
| 158 | + fprintf(stderr, "Client ns %s rank %d: PMIx_Fence failed: %d\n", myproc.nspace, myproc.rank, rc); |
| 159 | + goto done; |
| 160 | + } |
| 161 | + PMIX_INFO_FREE(info, 1); |
| 162 | + |
| 163 | + /* check the returned data */ |
| 164 | + for (n=0; n < nprocs; n++) { |
| 165 | + proc.rank = n; |
| 166 | + if (PMIX_SUCCESS != (rc = PMIx_Get(&proc, "test-key", NULL, 0, &val))) { |
| 167 | + fprintf(stderr, "Client ns %s rank %d: PMIx_Get on rank %u failed: %d\n", |
| 168 | + myproc.nspace, myproc.rank, proc.rank, rc); |
| 169 | + goto done; |
| 170 | + } |
| 171 | + if (PMIX_DATA_ARRAY != val->type) { |
| 172 | + fprintf(stderr, "Client ns %s rank %d: PMIx_Get on rank %u returned wrong type: %d\n", |
| 173 | + myproc.nspace, myproc.rank, proc.rank, val->type); |
| 174 | + PMIX_VALUE_RELEASE(val); |
| 175 | + goto done; |
| 176 | + } |
| 177 | + dptr = val->data.darray; |
| 178 | + if (NULL == dptr) { |
| 179 | + fprintf(stderr, "Client ns %s rank %d: PMIx_Get %d returned NULL array\n", |
| 180 | + myproc.nspace, myproc.rank, proc.rank); |
| 181 | + PMIX_VALUE_RELEASE(val); |
| 182 | + goto done; |
| 183 | + } |
| 184 | + if (PMIX_VALUE != dptr->type) { |
| 185 | + fprintf(stderr, "Client ns %s rank %d: PMIx_Get %d returned wrong array value type %d\n", |
| 186 | + myproc.nspace, myproc.rank, proc.rank, dptr->type); |
| 187 | + PMIX_VALUE_RELEASE(val); |
| 188 | + goto done; |
| 189 | + } |
| 190 | + if (32 != dptr->size) { |
| 191 | + fprintf(stderr, "Client ns %s rank %d: PMIx_Get %d returned wrong array value size %d\n", |
| 192 | + myproc.nspace, myproc.rank, proc.rank, (int)dptr->size); |
| 193 | + PMIX_VALUE_RELEASE(val); |
| 194 | + goto done; |
| 195 | + } |
| 196 | + vptr = (pmix_value_t*)dptr->array; |
| 197 | + for (k=0; k < 32; k++) { |
| 198 | + if (PMIX_UINT64 != vptr[k].type) { |
| 199 | + fprintf(stderr, "Client ns %s rank %d: PMIx_Get %d returned wrong type: %d\n", |
| 200 | + myproc.nspace, myproc.rank, proc.rank, vptr[k].type); |
| 201 | + PMIX_VALUE_RELEASE(val); |
| 202 | + goto done; |
| 203 | + } |
| 204 | + if (2*k != vptr[k].data.uint64) { |
| 205 | + fprintf(stderr, "Client ns %s rank %d: PMIx_Get %d returned wrong value: %lu\n", |
| 206 | + myproc.nspace, myproc.rank, proc.rank, (unsigned long)vptr[k].data.uint64); |
| 207 | + PMIX_VALUE_RELEASE(val); |
| 208 | + goto done; |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + done: |
| 214 | + /* finalize us */ |
| 215 | + fprintf(stderr, "Client ns %s rank %d: Finalizing\n", myproc.nspace, myproc.rank); |
| 216 | + if (PMIX_SUCCESS != (rc = PMIx_Finalize(NULL, 0))) { |
| 217 | + fprintf(stderr, "Client ns %s rank %d:PMIx_Finalize failed: %d\n", myproc.nspace, myproc.rank, rc); |
| 218 | + } else { |
| 219 | + fprintf(stderr, "Client ns %s rank %d:PMIx_Finalize successfully completed\n", myproc.nspace, myproc.rank); |
| 220 | + } |
| 221 | + fflush(stderr); |
| 222 | + return(0); |
| 223 | +} |
0 commit comments