PMIX_EXPORT int PMI2_Nameserv_lookup(const char service_name[], const PMI_keyval_t *info_ptr, char port[], int portLen) { pmix_status_t rc = PMIX_SUCCESS; int nvals; pmix_pdata_t pdata[2]; PMI2_CHECK(); if (NULL == service_name || NULL == info_ptr || NULL == port) { return PMI2_ERR_INVALID_ARG; } if (pmi2_singleton) { return PMI2_FAIL; } PMIX_PDATA_CONSTRUCT(&pdata[0]); PMIX_PDATA_CONSTRUCT(&pdata[1]); /* pass the service */ (void)strncpy(pdata[0].key, service_name, PMIX_MAX_KEYLEN); nvals = 1; /* if provided, add any other value */ if (NULL != info_ptr) { (void)strncpy(pdata[1].key, info_ptr->key, PMIX_MAX_KEYLEN); pdata[1].value.type = PMIX_STRING; pdata[1].value.data.string = info_ptr->val; nvals = 2; } /* lookup the info */ if (PMIX_SUCCESS != (rc = PMIx_Lookup(pdata, nvals, NULL, 0))) { PMIX_PDATA_DESTRUCT(&pdata[0]); PMIX_PDATA_DESTRUCT(&pdata[1]); return convert_err(rc); } /* should have received a string back */ if (PMIX_STRING != pdata[0].value.type || NULL == pdata[0].value.data.string) { PMIX_PDATA_DESTRUCT(&pdata[0]); PMIX_PDATA_DESTRUCT(&pdata[1]); return PMI2_FAIL; } /* return the port */ (void)strncpy(port, pdata[0].value.data.string, portLen); PMIX_PDATA_DESTRUCT(&pdata[0]); if (NULL != info_ptr) { } PMIX_PDATA_DESTRUCT(&pdata[1]); return PMI2_SUCCESS; }
PMIX_EXPORT int PMI_Lookup_name(const char service_name[], char port[]) { pmix_status_t rc = PMIX_SUCCESS; pmix_pdata_t pdata; PMI_CHECK(); if (NULL == service_name || NULL == port) { return PMI_ERR_INVALID_ARG; } if (pmi_singleton) { return PMI_FAIL; } PMIX_PDATA_CONSTRUCT(&pdata); /* pass the service */ (void) strncpy(pdata.key, service_name, PMIX_MAX_KEYLEN); /* PMI-1 doesn't want the nspace back */ if (PMIX_SUCCESS != (rc = PMIx_Lookup(&pdata, 1, NULL, 0))) { return convert_err(rc); } /* should have received a string back */ if (PMIX_STRING != pdata.value.type || NULL == pdata.value.data.string) { return convert_err(PMIX_ERR_NOT_FOUND); } /* return the port - sadly, this API doesn't tell us * the size of the port array, and so there is a * potential we could overrun it. As this feature * isn't widely supported in PMI-1, try being * conservative */ (void) strncpy(port, pdata.value.data.string, PMIX_MAX_KEYLEN); PMIX_PDATA_DESTRUCT(&pdata); return PMIX_SUCCESS; }
int main(int argc, char **argv) { int rc; pmix_value_t value; pmix_value_t *val = &value; pmix_proc_t proc; uint32_t nprocs; pmix_info_t *info; pmix_pdata_t *pdata; pmix_proc_t myproc; /* init us */ if (PMIX_SUCCESS != (rc = PMIx_Init(&myproc))) { pmix_output(0, "Client ns %s rank %d: PMIx_Init failed: %d", myproc.nspace, myproc.rank, rc); exit(0); } pmix_output(0, "Client ns %s rank %d: Running", myproc.nspace, myproc.rank); /* get our universe size */ if (PMIX_SUCCESS != (rc = PMIx_Get(&myproc, PMIX_UNIV_SIZE, NULL, 0, &val))) { pmix_output(0, "Client ns %s rank %d: PMIx_Get universe size failed: %d", myproc.nspace, myproc.rank, rc); goto done; } nprocs = val->data.uint32; PMIX_VALUE_RELEASE(val); pmix_output(0, "Client %s:%d universe size %d", myproc.nspace, myproc.rank, nprocs); /* call fence to ensure the data is received */ PMIX_PROC_CONSTRUCT(&proc); (void)strncpy(proc.nspace, myproc.nspace, PMIX_MAX_NSLEN); proc.rank = PMIX_RANK_WILDCARD; if (PMIX_SUCCESS != (rc = PMIx_Fence(&proc, 1, NULL, 0))) { pmix_output(0, "Client ns %s rank %d: PMIx_Fence failed: %d", myproc.nspace, myproc.rank, rc); goto done; } /* publish something */ if (0 == myproc.rank) { PMIX_INFO_CREATE(info, 2); (void)strncpy(info[0].key, "FOOBAR", PMIX_MAX_KEYLEN); info[0].value.type = PMIX_UINT8; info[0].value.data.uint8 = 1; (void)strncpy(info[1].key, "PANDA", PMIX_MAX_KEYLEN); info[1].value.type = PMIX_SIZE; info[1].value.data.size = 123456; if (PMIX_SUCCESS != (rc = PMIx_Publish(info, 2))) { pmix_output(0, "Client ns %s rank %d: PMIx_Publish failed: %d", myproc.nspace, myproc.rank, rc); goto done; } PMIX_INFO_FREE(info, 2); } /* call fence again so all procs know the data * has been published */ if (PMIX_SUCCESS != (rc = PMIx_Fence(&proc, 1, NULL, 0))) { pmix_output(0, "Client ns %s rank %d: PMIx_Fence failed: %d", myproc.nspace, myproc.rank, rc); goto done; } /* lookup something */ if (0 != myproc.rank) { PMIX_PDATA_CREATE(pdata, 1); (void)strncpy(pdata[0].key, "FOOBAR", PMIX_MAX_KEYLEN); if (PMIX_SUCCESS != (rc = PMIx_Lookup(pdata, 1, NULL, 0))) { pmix_output(0, "Client ns %s rank %d: PMIx_Lookup failed: %d", myproc.nspace, myproc.rank, rc); goto done; } /* check the return for value and source */ if (0 != strncmp(myproc.nspace, pdata[0].proc.nspace, PMIX_MAX_NSLEN)) { pmix_output(0, "Client ns %s rank %d: PMIx_Lookup returned wrong nspace: %s", myproc.nspace, myproc.rank, pdata[0].proc.nspace); goto done; } if (0 != pdata[0].proc.rank) { pmix_output(0, "Client ns %s rank %d: PMIx_Lookup returned wrong rank: %d", myproc.nspace, myproc.rank, pdata[0].proc.rank); goto done; } if (PMIX_UINT8 != pdata[0].value.type) { pmix_output(0, "Client ns %s rank %d: PMIx_Lookup returned wrong type: %d", myproc.nspace, myproc.rank, pdata[0].value.type); goto done; } if (1 != pdata[0].value.data.uint8) { pmix_output(0, "Client ns %s rank %d: PMIx_Lookup returned wrong value: %d", myproc.nspace, myproc.rank, (int)pdata[0].value.data.uint8); goto done; } PMIX_PDATA_FREE(pdata, 1); pmix_output(0, "PUBLISH-LOOKUP SUCCEEDED"); } /* call fence again so rank 0 waits before leaving */ if (PMIX_SUCCESS != (rc = PMIx_Fence(&proc, 1, NULL, 0))) { pmix_output(0, "Client ns %s rank %d: PMIx_Fence failed: %d", myproc.nspace, myproc.rank, rc); goto done; } if (0 == myproc.rank) { char **keys = NULL; pmix_argv_append_nosize(&keys, "FOOBAR"); pmix_argv_append_nosize(&keys, "PANDA"); if (PMIX_SUCCESS != (rc = PMIx_Unpublish(keys, NULL, 0))) { pmix_output(0, "Client ns %s rank %d: PMIx_Unpublish failed: %d", myproc.nspace, myproc.rank, rc); goto done; } pmix_output(0, "UNPUBLISH SUCCEEDED"); } /* call fence again so everyone waits for rank 0 before leaving */ proc.rank = PMIX_RANK_WILDCARD; if (PMIX_SUCCESS != (rc = PMIx_Fence(&proc, 1, NULL, 0))) { pmix_output(0, "Client ns %s rank %d: PMIx_Fence failed: %d", myproc.nspace, myproc.rank, rc); goto done; } done: /* finalize us */ pmix_output(0, "Client ns %s rank %d: Finalizing", myproc.nspace, myproc.rank); if (PMIX_SUCCESS != (rc = PMIx_Finalize())) { fprintf(stderr, "Client ns %s rank %d:PMIx_Finalize failed: %d\n", myproc.nspace, myproc.rank, rc); } else { fprintf(stderr, "Client ns %s rank %d:PMIx_Finalize successfully completed\n", myproc.nspace, myproc.rank); } fflush(stderr); return(0); }
/* allocate and return a string containing the current job id */ char* scr_env_jobid() { char* jobid = NULL; char* value; #ifdef SCR_RESOURCE_MANAGER_SLURM /* read $SLURM_JOBID environment variable for jobid string */ if ((value = getenv("SLURM_JOBID")) != NULL) { jobid = strdup(value); if (jobid == NULL) { scr_err("Failed to allocate memory to record jobid (%s) @ %s:%d", value, __FILE__, __LINE__ ); } } #endif #ifdef SCR_RESOURCE_MANAGER_APRUN /* read $PBS_JOBID environment variable for jobid string */ if ((value = getenv("PBS_JOBID")) != NULL) { jobid = strdup(value); if (jobid == NULL) { scr_err("Failed to allocate memory to record jobid (%s) @ %s:%d", value, __FILE__, __LINE__ ); } } #endif #ifdef SCR_RESOURCE_MANAGER_PMIX /* todo: must replace this in the scr_env script as well */ pmix_pdata_t *pmix_query_data = NULL; PMIX_PDATA_CREATE(pmix_query_data, 1); /* todo: pmix_pdata_destroy ?? */ /* specify that we want our jobid from pmix */ strncpy(pmix_query_data[0].key, PMIX_JOBID, PMIX_MAX_KEYLEN); /* query pmix for our job id */ pmix_status_t retval = PMIx_Lookup(pmix_query_data, 1, NULL, 0); if (retval == PMIX_SUCCESS) { /* got it, strdup the value from pmix */ jobid = strdup(pmix_query_data[0].value.data.string); scr_dbg(1, "PMIx_Lookup for jobid success '%s'", jobid); } else { /* failed to get our jobid from pmix, make one up */ char *pmix_hardcoded_id = "pmix_hardcoded_jobid"; jobid = strdup(pmix_hardcoded_id); scr_dbg(1, "PMIx_Lookup for jobid failed: rc=%d, using hardcoded jobid '%s'", retval, jobid ); } /* free pmix query structure */ PMIX_PDATA_FREE(pmix_query_data, 1); #endif #ifdef SCR_RESOURCE_MANAGER_LSF /* read $PBS_JOBID environment variable for jobid string */ if ((value = getenv("LSB_JOBID")) != NULL) { jobid = strdup(value); if (jobid == NULL) { scr_err("Failed to allocate memory to record jobid (%s) @ %s:%d", value, __FILE__, __LINE__ ); } } #endif return jobid; }