Skip to content

Instantly share code, notes, and snippets.

@jjhursey
Created April 18, 2018 02:10
Show Gist options
  • Save jjhursey/b93a3c1e7d1ffe9827442d5aa114cc8e to your computer and use it in GitHub Desktop.
Save jjhursey/b93a3c1e7d1ffe9827442d5aa114cc8e to your computer and use it in GitHub Desktop.
PMIx Init/Finalize Example
/*
* Simple PMIx Init/Finalize with Hostname
*/
#define _GNU_SOURCE
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <pmix.h>
static pmix_proc_t myproc;
int main(int argc, char **argv)
{
int rc;
pmix_value_t value;
pmix_value_t *val = &value;
pmix_proc_t proc;
uint32_t nprocs;
/*
* Initialize
*/
if (PMIX_SUCCESS != (rc = PMIx_Init(&myproc, NULL, 0))) {
fprintf(stderr, "Client %s:%d PMIx_Init failed: %d\n", myproc.nspace, myproc.rank, rc);
exit(0);
}
fprintf(stderr, "Client %s:%d Running...\n", myproc.nspace, myproc.rank);
/*
* Get: job size
*/
PMIX_PROC_CONSTRUCT(&proc);
(void)strncpy(proc.nspace, myproc.nspace, PMIX_MAX_NSLEN);
proc.rank = PMIX_RANK_WILDCARD;
if (PMIX_SUCCESS != (rc = PMIx_Get(&proc, PMIX_JOB_SIZE, NULL, 0, &val))) {
fprintf(stderr, "Client %s:%d PMIx_Get job size failed: %d\n", myproc.nspace, myproc.rank, rc);
goto done;
}
nprocs = val->data.uint32;
fprintf(stderr, "Client %s:%d num procs %d\n", myproc.nspace, myproc.rank, nprocs);
/*
* Get: Peer hostname (should be in the job data too)
*/
proc.rank = (myproc.rank + 1) % nprocs;
rc = PMIx_Get(&proc, PMIX_HOSTNAME, NULL, 0, &val);
if (PMIX_SUCCESS != rc ) {
fprintf(stderr, "Client %s:%d PMIx_Get hostname of %d failed: %d\n", myproc.nspace, myproc.rank, proc.rank, rc);
goto done;
}
fprintf(stderr, "Client %s:%d hostname of rank %d is %s\n", myproc.nspace, myproc.rank, proc.rank, val->data.string);
PMIX_VALUE_RELEASE(val);
done:
/* finalize us */
fprintf(stderr, "Client %s:%d Finalizing...\n", myproc.nspace, myproc.rank);
if (PMIX_SUCCESS != (rc = PMIx_Finalize(NULL, 0))) {
fprintf(stderr, "Client %s:%d PMIx_Finalize failed: %d\n", myproc.nspace, myproc.rank, rc);
} else {
fprintf(stderr, "Client %s:%d PMIx_Finalize successfully completed\n", myproc.nspace, myproc.rank);
}
fflush(stderr);
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment