Skip to content

Commit 717f451

Browse files
committed
A slightly more flexible way to select the thread level.
This function support prepositions (such as mpi_thread_) and partial matching (such as "fun" for funnelled). Signed-off-by: George Bosilca <gbosilca@nvidia.com>
1 parent 3de2489 commit 717f451

File tree

1 file changed

+44
-25
lines changed

1 file changed

+44
-25
lines changed

ompi/runtime/ompi_mpi_init.c

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* Copyright (c) 2021-2022 Triad National Security, LLC. All rights
3131
* reserved.
3232
* Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
33+
* Copyright (c) 2025 NVIDIA. All rights reserved.
3334
* $COPYRIGHT$
3435
*
3536
* Additional copyrights may follow
@@ -269,37 +270,55 @@ MPI_Fint *MPI_F08_STATUSES_IGNORE = NULL;
269270

270271
#include "mpif-c-constants.h"
271272

273+
static const char *ompi_thread_level_keywords[] = {"single", "serialized", "funneled", "multiple"};
274+
static const char *ompi_thread_level_prepositions[] = {"mpi_thread_", "thread_", NULL};
275+
/* In the future integer MPI_ABI values for MPI_THREAD_SINGLE-MULTIPLE may be
276+
* non-sequential (but ordered) integer values. If you are implementing MPI
277+
* ABI changes please refer to
278+
* https://github.com/open-mpi/ompi/pull/13211#discussion_r2085086844
279+
*/
280+
static const int ompi_thread_level_values[] = {MPI_THREAD_SINGLE, MPI_THREAD_SERIALIZED,
281+
MPI_THREAD_FUNNELED, MPI_THREAD_MULTIPLE};
282+
272283
int ompi_getenv_mpi_thread_level(int *requested)
273284
{
274285
char* env;
275286
if (NULL != (env = getenv("OMPI_MPI_THREAD_LEVEL"))) {
276-
/* deal with string values, int values (no atoi, it doesn't error check) */
277-
/* In the future integer MPI_ABI values for MPI_THREAD_SINGLE-MULTIPLE
278-
* may be non-sequential (but ordered) integer values.
279-
* If you are implementing MPI ABI changes please refer to
280-
* https://github.com/open-mpi/ompi/pull/13211#discussion_r2085086844
281-
*/
282-
if (0 == strcasecmp(env, "multiple") ||
283-
0 == strcasecmp(env, "MPI_THREAD_MULTIPLE") ||
284-
0 == strcmp(env, "3")) {
285-
return *requested = MPI_THREAD_MULTIPLE;
286-
}
287-
if (0 == strcasecmp(env, "serialized") ||
288-
0 == strcasecmp(env, "MPI_THREAD_SERIALIZED") ||
289-
0 == strcmp(env, "2")) {
290-
return *requested = MPI_THREAD_SERIALIZED;
291-
}
292-
if (0 == strcasecmp(env, "funneled") ||
293-
0 == strcasecmp(env, "MPI_THREAD_FUNNELED") ||
294-
0 == strcmp(env, "1")) {
295-
return *requested = MPI_THREAD_FUNNELED;
287+
char *prep = NULL, *token = (char *) env /* full match */;
288+
int pidx = 0;
289+
long found = strtol(env, &prep, 10);
290+
291+
if (prep == env) { /* no digits found */
292+
found = -1;
293+
while (NULL != (prep = (char *) ompi_thread_level_prepositions[pidx])) {
294+
if (0 == strncasecmp(prep, env, strlen(prep))) {
295+
token = env + strlen(prep);
296+
break; /* got a token let's find a match */
297+
}
298+
pidx++;
299+
}
300+
const int nb_keywords = sizeof(ompi_thread_level_keywords)/sizeof(ompi_thread_level_keywords[0]);
301+
for (int i = 0; i < nb_keywords; i++) {
302+
if (0 == strncasecmp(ompi_thread_level_keywords[i], token, strlen(token))) {
303+
if (-1 != found) { /* not the first match, bail out */
304+
return OMPI_ERR_BAD_PARAM;
305+
}
306+
found = i;
307+
}
308+
}
309+
} else { /* at least a digit has been found */
310+
if (*prep != '\0') { /* non-numerical left-over */
311+
return OMPI_ERR_BAD_PARAM;
312+
}
313+
/* did we found a valid integer value? */
314+
const int nb_keywords = sizeof(ompi_thread_level_values)/sizeof(ompi_thread_level_values[0]);
315+
if (found >= nb_keywords) {
316+
return OMPI_ERR_BAD_PARAM;
317+
}
296318
}
297-
if (0 == strcasecmp(env, "single") ||
298-
0 == strcasecmp(env, "MPI_THREAD_SINGLE") ||
299-
0 == strcmp(env, "0")) {
300-
return *requested = MPI_THREAD_SINGLE;
319+
if (-1 != found) {
320+
return *requested = ompi_thread_level_values[found];
301321
}
302-
/* the env value is invalid... */
303322
return OMPI_ERR_BAD_PARAM;
304323
}
305324
return OMPI_SUCCESS;

0 commit comments

Comments
 (0)