|
| 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-2020 Intel, Inc. All rights reserved. |
| 17 | + * Copyright (c) 2015 Mellanox Technologies, Inc. All rights reserved. |
| 18 | + * Copyright (c) 2019 IBM Corporation. All rights reserved. |
| 19 | + * Copyright (c) 2021-2025 Nanook Consulting All rights reserved. |
| 20 | + * $COPYRIGHT$ |
| 21 | + * |
| 22 | + * Additional copyrights may follow |
| 23 | + * |
| 24 | + * $HEADER$ |
| 25 | + * |
| 26 | + */ |
| 27 | + |
| 28 | +#include <pthread.h> |
| 29 | +#include <stdbool.h> |
| 30 | +#include <stdio.h> |
| 31 | +#include <stdlib.h> |
| 32 | +#include <time.h> |
| 33 | +#include <unistd.h> |
| 34 | +#include <libgen.h> |
| 35 | + |
| 36 | +#include <pmix.h> |
| 37 | + |
| 38 | +int main(int argc, char **argv) |
| 39 | +{ |
| 40 | + pmix_proc_t myproc; |
| 41 | + int rc, ret; |
| 42 | + pmix_value_t *val = NULL; |
| 43 | + uint32_t nprocs; |
| 44 | + pmix_proc_t proc, *parray = NULL; |
| 45 | + pmix_info_t *results = NULL, info[3]; |
| 46 | + size_t nresults, cid, n, m, psize = 0; |
| 47 | + pmix_data_array_t dry; |
| 48 | + char hostname[1024]; |
| 49 | + |
| 50 | + gethostname(hostname, 1024); |
| 51 | + |
| 52 | + /* init us */ |
| 53 | + if (PMIX_SUCCESS != (rc = PMIx_Init(&myproc, NULL, 0))) { |
| 54 | + fprintf(stderr, "Client ns %s rank %d: PMIx_Init failed: %s\n", myproc.nspace, myproc.rank, |
| 55 | + PMIx_Error_string(rc)); |
| 56 | + exit(1); |
| 57 | + } |
| 58 | + fprintf(stderr, "Client ns %s rank %d: Running on host %s\n", myproc.nspace, myproc.rank, hostname); |
| 59 | + |
| 60 | + PMIX_PROC_CONSTRUCT(&proc); |
| 61 | + PMIX_LOAD_PROCID(&proc, myproc.nspace, PMIX_RANK_WILDCARD); |
| 62 | + |
| 63 | + /* get our job size */ |
| 64 | + if (PMIX_SUCCESS != (rc = PMIx_Get(&proc, PMIX_JOB_SIZE, NULL, 0, &val))) { |
| 65 | + fprintf(stderr, "Client ns %s rank %d: PMIx_Get job size failed: %s\n", myproc.nspace, |
| 66 | + myproc.rank, PMIx_Error_string(rc)); |
| 67 | + goto done; |
| 68 | + } |
| 69 | + nprocs = val->data.uint32; |
| 70 | + PMIX_VALUE_RELEASE(val); |
| 71 | + if (nprocs < 6) { |
| 72 | + if (0 == myproc.rank) { |
| 73 | + fprintf(stderr, "This example with add-members requires a minimum of 6 processes\n"); |
| 74 | + } |
| 75 | + exit(1); |
| 76 | + } |
| 77 | + fprintf(stderr, "Client %s:%d job size %u\n", myproc.nspace, myproc.rank, nprocs); |
| 78 | + |
| 79 | + /* call fence to sync */ |
| 80 | + PMIX_PROC_CONSTRUCT(&proc); |
| 81 | + PMIX_LOAD_PROCID(&proc, myproc.nspace, PMIX_RANK_WILDCARD); |
| 82 | + if (PMIX_SUCCESS != (rc = PMIx_Fence(&proc, 1, NULL, 0))) { |
| 83 | + fprintf(stderr, "Client ns %s rank %d: PMIx_Fence failed: %d\n", myproc.nspace, myproc.rank, |
| 84 | + rc); |
| 85 | + goto done; |
| 86 | + } |
| 87 | + |
| 88 | +//<EG BEGIN ID="bootstrap"> |
| 89 | + /* rank=0 and 3 bootstrap a new group */ |
| 90 | + if (0 == myproc.rank || 3 == myproc.rank) { |
| 91 | + fprintf(stderr, "%d executing Group_construct\n", myproc.rank); |
| 92 | + PMIX_INFO_LOAD(&info[0], PMIX_GROUP_ASSIGN_CONTEXT_ID, NULL, PMIX_BOOL); |
| 93 | + // two procs are performing the bootstrap |
| 94 | + n = 2; |
| 95 | + PMIX_INFO_LOAD(&info[1], PMIX_GROUP_BOOTSTRAP, &n, PMIX_SIZE); |
| 96 | + |
| 97 | + PMIX_DATA_ARRAY_CONSTRUCT(&dry, 1, PMIX_PROC); |
| 98 | + parray = (pmix_proc_t*)dry.array; |
| 99 | + if (3 == myproc.rank) { |
| 100 | + fprintf(stderr, "[%u]: Adding members\n", myproc.rank); |
| 101 | + PMIX_LOAD_PROCID(&parray[0], myproc.nspace, 4); |
| 102 | + } else { |
| 103 | + fprintf(stderr, "[%u]: Adding members\n", myproc.rank); |
| 104 | + PMIX_LOAD_PROCID(&parray[0], myproc.nspace, 5); |
| 105 | + } |
| 106 | + PMIX_INFO_LOAD(&info[2], PMIX_GROUP_ADD_MEMBERS, &dry, PMIX_DATA_ARRAY); |
| 107 | + PMIX_DATA_ARRAY_DESTRUCT(&dry); |
| 108 | + rc = PMIx_Group_construct("ourgroup", &myproc, 1, info, 3, &results, &nresults); |
| 109 | + if (PMIX_SUCCESS != rc) { |
| 110 | + fprintf(stderr, "Client ns %s rank %d: PMIx_Group_construct failed: %s\n", |
| 111 | + myproc.nspace, myproc.rank, PMIx_Error_string(rc)); |
| 112 | + goto done; |
| 113 | + } |
| 114 | + } else if (4 == myproc.rank || 5 == myproc.rank) { |
| 115 | + fprintf(stderr, "%d executing Group_construct\n", myproc.rank); |
| 116 | + fflush(stderr); |
| 117 | + rc = PMIx_Group_construct("ourgroup", NULL, 0, NULL, 0, &results, &nresults); |
| 118 | + if (PMIX_SUCCESS != rc) { |
| 119 | + fprintf(stderr, "Client ns %s rank %d: PMIx_Group_construct failed: %s\n", |
| 120 | + myproc.nspace, myproc.rank, PMIx_Error_string(rc)); |
| 121 | + goto done; |
| 122 | + } |
| 123 | + } |
| 124 | +//<EG END ID="bootstrap"> |
| 125 | + fprintf(stderr, "%d GROUP CONSTRUCT COMPLETE\n", myproc.rank); |
| 126 | + fflush(stderr); |
| 127 | + |
| 128 | + if (0 == myproc.rank || 3 == myproc.rank || |
| 129 | + 4 == myproc.rank || 5 == myproc.rank) { |
| 130 | + /* we should have a single results object */ |
| 131 | + if (NULL != results) { |
| 132 | + cid = 0; |
| 133 | + for (n=0; n < nresults; n++) { |
| 134 | + if (PMIX_CHECK_KEY(&results[n], PMIX_GROUP_CONTEXT_ID)) { |
| 135 | + PMIX_VALUE_GET_NUMBER(rc, &results[n].value, cid, size_t); |
| 136 | + fprintf(stderr, "%d Group construct complete with status %s KEY %s CID %lu\n", |
| 137 | + myproc.rank, PMIx_Error_string(rc), results[n].key, (unsigned long) cid); |
| 138 | + } else if (PMIX_CHECK_KEY(&results[n], PMIX_GROUP_MEMBERSHIP)) { |
| 139 | + parray = (pmix_proc_t*)results[n].value.data.darray->array; |
| 140 | + psize = results[n].value.data.darray->size; |
| 141 | + fprintf(stderr, "[%u] NUM MEMBERS: %u MEMBERSHIP:\n", myproc.rank, (unsigned)psize); |
| 142 | + for (m=0; m < psize; m++) { |
| 143 | + fprintf(stderr, "\t%s:%u\n", parray[m].nspace, parray[m].rank); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } else { |
| 148 | + fprintf(stderr, "%d Group construct complete, but no results returned\n", myproc.rank); |
| 149 | + } |
| 150 | + if (NULL == parray) { |
| 151 | + fprintf(stderr, "%d NULL proc array\n", myproc.rank); |
| 152 | + goto done; |
| 153 | + } |
| 154 | + |
| 155 | + fprintf(stderr, "%d Executing group fence\n", myproc.rank); |
| 156 | + PMIX_PROC_CONSTRUCT(&proc); |
| 157 | + PMIX_LOAD_PROCID(&proc, "ourgroup", PMIX_RANK_WILDCARD); |
| 158 | + if (PMIX_SUCCESS != (rc = PMIx_Fence(&proc, 1, NULL, 0))) { |
| 159 | + fprintf(stderr, "Client ns %s rank %d: PMIx_Fence failed: %d\n", myproc.nspace, myproc.rank, |
| 160 | + rc); |
| 161 | + goto done; |
| 162 | + } |
| 163 | + |
| 164 | + fprintf(stderr, "%d executing Group_destruct\n", myproc.rank); |
| 165 | + rc = PMIx_Group_destruct("ourgroup", NULL, 0); |
| 166 | + if (PMIX_SUCCESS != rc) { |
| 167 | + fprintf(stderr, "Client ns %s rank %d: PMIx_Group_destruct failed: %s\n", myproc.nspace, |
| 168 | + myproc.rank, PMIx_Error_string(rc)); |
| 169 | + goto done; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | +done: |
| 174 | + /* finalize us */ |
| 175 | + fprintf(stderr, "Client ns %s rank %d: Finalizing\n", myproc.nspace, myproc.rank); |
| 176 | + if (PMIX_SUCCESS != (ret = PMIx_Finalize(NULL, 0))) { |
| 177 | + fprintf(stderr, "Client ns %s rank %d:PMIx_Finalize failed: %s\n", myproc.nspace, |
| 178 | + myproc.rank, PMIx_Error_string(ret)); |
| 179 | + rc = ret; |
| 180 | + } else { |
| 181 | + fprintf(stderr, "Client ns %s rank %d:PMIx_Finalize successfully completed\n", |
| 182 | + myproc.nspace, myproc.rank); |
| 183 | + } |
| 184 | + fprintf(stderr, "%s:%d COMPLETE\n", myproc.nspace, myproc.rank); |
| 185 | + fflush(stderr); |
| 186 | + return (rc); |
| 187 | +} |
0 commit comments