Skip to content

Commit 413c1a8

Browse files
committed
Fix the "client" example
Need to discover which procs are local vs remote and properly look for their posted data. Signed-off-by: Ralph Castain <[email protected]>
1 parent f2b472a commit 413c1a8

File tree

1 file changed

+90
-50
lines changed

1 file changed

+90
-50
lines changed

examples/client.c

Lines changed: 90 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* Copyright (c) 2013-2020 Intel, Inc. All rights reserved.
1717
* Copyright (c) 2015 Mellanox Technologies, Inc. All rights reserved.
1818
* Copyright (c) 2019 IBM Corporation. All rights reserved.
19-
* Copyright (c) 2021 Nanook Consulting. All rights reserved.
19+
* Copyright (c) 2021-2022 Nanook Consulting. All rights reserved.
2020
* $COPYRIGHT$
2121
*
2222
* Additional copyrights may follow
@@ -125,14 +125,17 @@ int main(int argc, char **argv)
125125
pmix_value_t *val = NULL;
126126
char *tmp;
127127
pmix_proc_t proc;
128-
uint32_t nprocs, n;
128+
uint32_t nprocs, n, k, nlocal;
129+
bool local, all_local = false;;
129130
pmix_info_t *info;
130131
bool flag;
131132
mylock_t mylock;
132133
myrel_t myrel;
133134
pmix_status_t dbg = PMIX_ERR_DEBUGGER_RELEASE;
134135
pid_t pid;
135136
pmix_topology_t mytopo;
137+
char **peers;
138+
pmix_rank_t *locals = NULL;
136139

137140
EXAMPLES_HIDE_UNUSED_PARAMS(argc, argv);
138141

@@ -295,62 +298,99 @@ int main(int argc, char **argv)
295298
}
296299
PMIX_INFO_FREE(info, 1);
297300

301+
/* get a list of our local peers */
302+
if (PMIX_SUCCESS != (rc = PMIx_Get(&proc, PMIX_LOCAL_PEERS, NULL, 0, &val))) {
303+
fprintf(stderr, "Client ns %s rank %d: PMIx_Get local peers failed: %s\n", myproc.nspace,
304+
myproc.rank, PMIx_Error_string(rc));
305+
goto done;
306+
}
307+
/* split the returned string to get the rank of each local peer */
308+
peers = pmix_argv_split(val->data.string, ',');
309+
PMIX_VALUE_RELEASE(val);
310+
nlocal = pmix_argv_count(peers);
311+
if (nprocs == nlocal) {
312+
all_local = true;
313+
} else {
314+
all_local = false;
315+
locals = (pmix_rank_t *) malloc(pmix_argv_count(peers) * sizeof(pmix_rank_t));
316+
for (n = 0; NULL != peers[n]; n++) {
317+
locals[n] = strtoul(peers[n], NULL, 10);
318+
}
319+
}
320+
PMIX_ARGV_FREE(peers);
321+
298322
/* check the returned data */
299323
for (n = 0; n < nprocs; n++) {
300-
if (0 > asprintf(&tmp, "%s-%d-local", myproc.nspace, myproc.rank)) {
301-
exit(1);
302-
}
303-
if (PMIX_SUCCESS != (rc = PMIx_Get(&myproc, tmp, NULL, 0, &val))) {
304-
fprintf(stderr, "Client ns %s rank %d: PMIx_Get %s failed: %d\n", myproc.nspace,
305-
myproc.rank, tmp, rc);
306-
free(tmp);
307-
goto done;
308-
}
309-
if (PMIX_UINT64 != val->type) {
310-
fprintf(stderr, "Client ns %s rank %d: PMIx_Get %s returned wrong type: %d\n",
311-
myproc.nspace, myproc.rank, tmp, val->type);
312-
PMIX_VALUE_RELEASE(val);
313-
free(tmp);
314-
goto done;
315-
}
316-
if (1234 != val->data.uint64) {
317-
fprintf(stderr, "Client ns %s rank %d: PMIx_Get %s returned wrong value: %d\n",
318-
myproc.nspace, myproc.rank, tmp, (int) val->data.uint64);
319-
PMIX_VALUE_RELEASE(val);
320-
free(tmp);
321-
goto done;
322-
}
323-
fprintf(stderr, "Client ns %s rank %d: PMIx_Get %s returned correct\n", myproc.nspace,
324-
myproc.rank, tmp);
325-
PMIX_VALUE_RELEASE(val);
326-
free(tmp);
327-
if (0 > asprintf(&tmp, "%s-%d-remote", myproc.nspace, myproc.rank)) {
328-
exit(1);
324+
if (all_local) {
325+
local = true;
326+
} else {
327+
local = false;
328+
/* see if this proc is local to us */
329+
for (k = 0; k < nlocal; k++) {
330+
if (n == locals[k]) {
331+
local = true;
332+
break;
333+
}
334+
}
329335
}
330-
if (PMIX_SUCCESS != (rc = PMIx_Get(&myproc, tmp, NULL, 0, &val))) {
331-
fprintf(stderr, "Client ns %s rank %d: PMIx_Get %s failed: %d\n", myproc.nspace,
332-
myproc.rank, tmp, rc);
333-
free(tmp);
334-
goto done;
335-
}
336-
if (PMIX_STRING != val->type) {
337-
fprintf(stderr, "Client ns %s rank %d: PMIx_Get %s returned wrong type: %d\n",
338-
myproc.nspace, myproc.rank, tmp, val->type);
336+
proc.rank = n;
337+
if (local) {
338+
if (0 > asprintf(&tmp, "%s-%d-local", myproc.nspace, proc.rank)) {
339+
exit(1);
340+
}
341+
if (PMIX_SUCCESS != (rc = PMIx_Get(&proc, tmp, NULL, 0, &val))) {
342+
fprintf(stderr, "Client ns %s rank %d: PMIx_Get %s failed: %d\n", myproc.nspace,
343+
myproc.rank, tmp, rc);
344+
free(tmp);
345+
goto done;
346+
}
347+
if (PMIX_UINT64 != val->type) {
348+
fprintf(stderr, "Client ns %s rank %d: PMIx_Get %s returned wrong type: %d\n",
349+
myproc.nspace, myproc.rank, tmp, val->type);
350+
PMIX_VALUE_RELEASE(val);
351+
free(tmp);
352+
goto done;
353+
}
354+
if (1234 != val->data.uint64) {
355+
fprintf(stderr, "Client ns %s rank %d: PMIx_Get %s returned wrong value: %d\n",
356+
myproc.nspace, myproc.rank, tmp, (int) val->data.uint64);
357+
PMIX_VALUE_RELEASE(val);
358+
free(tmp);
359+
goto done;
360+
}
361+
fprintf(stderr, "Client ns %s rank %d: PMIx_Get %s returned correct\n", myproc.nspace,
362+
myproc.rank, tmp);
339363
PMIX_VALUE_RELEASE(val);
340364
free(tmp);
341-
goto done;
342-
}
343-
if (0 != strcmp(val->data.string, "1234")) {
344-
fprintf(stderr, "Client ns %s rank %d: PMIx_Get %s returned wrong value: %s\n",
345-
myproc.nspace, myproc.rank, tmp, val->data.string);
365+
} else {
366+
if (0 > asprintf(&tmp, "%s-%d-remote", myproc.nspace, proc.rank)) {
367+
exit(1);
368+
}
369+
if (PMIX_SUCCESS != (rc = PMIx_Get(&proc, tmp, NULL, 0, &val))) {
370+
fprintf(stderr, "Client ns %s rank %d: PMIx_Get %s failed: %d\n", myproc.nspace,
371+
myproc.rank, tmp, rc);
372+
free(tmp);
373+
goto done;
374+
}
375+
if (PMIX_STRING != val->type) {
376+
fprintf(stderr, "Client ns %s rank %d: PMIx_Get %s returned wrong type: %d\n",
377+
myproc.nspace, myproc.rank, tmp, val->type);
378+
PMIX_VALUE_RELEASE(val);
379+
free(tmp);
380+
goto done;
381+
}
382+
if (0 != strcmp(val->data.string, "1234")) {
383+
fprintf(stderr, "Client ns %s rank %d: PMIx_Get %s returned wrong value: %s\n",
384+
myproc.nspace, myproc.rank, tmp, val->data.string);
385+
PMIX_VALUE_RELEASE(val);
386+
free(tmp);
387+
goto done;
388+
}
389+
fprintf(stderr, "Client ns %s rank %d: PMIx_Get %s returned correct\n", myproc.nspace,
390+
myproc.rank, tmp);
346391
PMIX_VALUE_RELEASE(val);
347392
free(tmp);
348-
goto done;
349393
}
350-
fprintf(stderr, "Client ns %s rank %d: PMIx_Get %s returned correct\n", myproc.nspace,
351-
myproc.rank, tmp);
352-
PMIX_VALUE_RELEASE(val);
353-
free(tmp);
354394
}
355395

356396
done:

0 commit comments

Comments
 (0)