Skip to content

Commit

Permalink
util/pmi: avoid calling PMIx_Init multiple times
Browse files Browse the repository at this point in the history
Since now we only call PMIx_Finalize once in the at exit handler, we
need make sure that we only call PMIx_Init once. Otherwise, the openpmix
server may complain abnormal exit.
  • Loading branch information
hzhou committed Jun 21, 2023
1 parent ceca980 commit a67bf3c
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/util/mpir_pmi.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ static char *pmi_kvs_name;
#elif defined USE_PMI2_API
static char *pmi_jobid;
#elif defined USE_PMIX_API
static int pmix_init_count = 0;
static pmix_proc_t pmix_proc;
static pmix_proc_t pmix_wcproc;
#endif
Expand All @@ -119,6 +120,7 @@ static void MPIR_pmi_finalize_on_exit(void)
PMI2_Finalize();
#elif defined USE_PMIX_API
PMIx_Finalize(NULL, 0);
pmix_init_count = 0;
#endif
}

Expand Down Expand Up @@ -189,20 +191,26 @@ int MPIR_pmi_init(void)

pmix_value_t *pvalue = NULL;

pmi_errno = PMIx_Init(&pmix_proc, NULL, 0);
if (pmi_errno == PMIX_ERR_UNREACH) {
/* no pmi server, assume we are a singleton */
rank = 0;
size = 1;
goto singleton_out;
/* Since we only call PMIx_Finalize once at `atexit` handler, we need prevent
* calling PMIx_Init multiple times. */
pmix_init_count++;
if (pmix_init_count == 1) {
pmi_errno = PMIx_Init(&pmix_proc, NULL, 0);
if (pmi_errno == PMIX_ERR_UNREACH) {
/* no pmi server, assume we are a singleton */
rank = 0;
size = 1;
goto singleton_out;
}
MPIR_ERR_CHKANDJUMP1(pmi_errno != PMIX_SUCCESS, mpi_errno, MPI_ERR_OTHER,
"**pmix_init", "**pmix_init %d", pmi_errno);

PMIX_PROC_CONSTRUCT(&pmix_wcproc);
MPL_strncpy(pmix_wcproc.nspace, pmix_proc.nspace, PMIX_MAX_NSLEN);
pmix_wcproc.rank = PMIX_RANK_WILDCARD;
}
MPIR_ERR_CHKANDJUMP1(pmi_errno != PMIX_SUCCESS, mpi_errno, MPI_ERR_OTHER,
"**pmix_init", "**pmix_init %d", pmi_errno);

rank = pmix_proc.rank;
PMIX_PROC_CONSTRUCT(&pmix_wcproc);
MPL_strncpy(pmix_wcproc.nspace, pmix_proc.nspace, PMIX_MAX_NSLEN);
pmix_wcproc.rank = PMIX_RANK_WILDCARD;

pmi_errno = PMIx_Get(&pmix_wcproc, PMIX_JOB_SIZE, NULL, 0, &pvalue);
MPIR_ERR_CHKANDJUMP1(pmi_errno != PMIX_SUCCESS, mpi_errno, MPI_ERR_OTHER,
Expand Down

0 comments on commit a67bf3c

Please sign in to comment.