Skip to content

Commit 6ad6cc8

Browse files
committed
OMPI_MPI_THREAD_LEVEL can now take 'multiple' 'MPI_THREAD_MULTIPLE'
(single,etc) in addition to numeric 0-3 values Signed-off-by: Aurelien Bouteiller <abouteil@amd.com> (cherry picked from commit 3de2489)
1 parent defea5f commit 6ad6cc8

File tree

4 files changed

+64
-25
lines changed

4 files changed

+64
-25
lines changed

ompi/mpi/c/init.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,13 @@ int MPI_Init(int *argc, char ***argv)
4646
{
4747
int err;
4848
int provided;
49-
char *env;
5049
int required = MPI_THREAD_SINGLE;
5150

5251
/* check for environment overrides for required thread level. If
5352
there is, check to see that it is a valid/supported thread level.
5453
If not, default to MPI_THREAD_MULTIPLE. */
55-
56-
if (NULL != (env = getenv("OMPI_MPI_THREAD_LEVEL"))) {
57-
required = atoi(env);
58-
/* In the future we may have to contend with non-sequential (MPI ABI) values
59-
* If you are implementing MPI ABI changes please refer to
60-
* https://github.com/open-mpi/ompi/pull/13211#discussion_r2085086844
61-
*/
62-
if (required != MPI_THREAD_SINGLE && required != MPI_THREAD_FUNNELED &&
63-
required != MPI_THREAD_SERIALIZED && required != MPI_THREAD_MULTIPLE) {
64-
required = MPI_THREAD_MULTIPLE;
65-
}
54+
if (OMPI_SUCCESS > ompi_getenv_mpi_thread_level(&required)) {
55+
required = MPI_THREAD_MULTIPLE;
6656
}
6757

6858
/* Call the back-end initialization function (we need to put as

ompi/mpi/c/init_thread.c

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ int MPI_Init_thread(int *argc, char ***argv, int required,
5050
{
5151
int err, safe_required = MPI_THREAD_SERIALIZED;
5252
bool err_arg_required = false;
53-
char *env;
5453

5554
ompi_hook_base_mpi_init_thread_top(argc, argv, required, provided);
5655

5756
/* Detect an incorrect thread support level, but dont report until we have the minimum
5857
* infrastructure setup.
58+
* In the future integer MPI_ABI values for MPI_THREAD_SINGLE-MULTIPLE
59+
* may have gaps between them, so just checking the range is not enough.
5960
*/
6061
err_arg_required = (required != MPI_THREAD_SINGLE && required != MPI_THREAD_FUNNELED &&
6162
required != MPI_THREAD_SERIALIZED && required != MPI_THREAD_MULTIPLE);
@@ -69,18 +70,7 @@ int MPI_Init_thread(int *argc, char ***argv, int required,
6970
* level (even if lower than argument `required`). A user program can
7071
* check `provided != required` to check if `required` has been overruled.
7172
*/
72-
if (NULL != (env = getenv("OMPI_MPI_THREAD_LEVEL"))) {
73-
int env_required = atoi(env);
74-
/* In the future we may have to contend with non-sequential (MPI ABI) values
75-
* If you are implementing MPI ABI changes please refer to
76-
* https://github.com/open-mpi/ompi/pull/13211#discussion_r2085086844
77-
*/
78-
err_arg_required |= (env_required != MPI_THREAD_SINGLE && env_required != MPI_THREAD_FUNNELED &&
79-
env_required != MPI_THREAD_SERIALIZED && env_required != MPI_THREAD_MULTIPLE);
80-
if (!err_arg_required) {
81-
safe_required = env_required;
82-
}
83-
}
73+
err_arg_required |= (OMPI_SUCCESS > ompi_getenv_mpi_thread_level(&safe_required));
8474

8575
*provided = safe_required;
8676

ompi/runtime/mpiruntime.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* Copyright (c) 2009 University of Houston. All rights reserved.
1717
* Copyright (c) 2014 Intel, Inc. All rights reserved.
1818
* Copyright (c) 2018 FUJITSU LIMITED. All rights reserved.
19+
* Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
1920
* $COPYRIGHT$
2021
*
2122
* Additional copyrights may follow
@@ -163,6 +164,27 @@ extern opal_hash_table_t ompi_mpi_f90_complex_hashtable;
163164
/** version string of ompi */
164165
OMPI_DECLSPEC extern const char ompi_version_string[];
165166

167+
/**
168+
* Obtain the required thread level from environment (if any)
169+
*
170+
* @param requested Thread support that is requested (OUT)
171+
*
172+
* @returns Error code if environment exist but has an invalid value
173+
*
174+
* The function reads the environment variable OMPI_MPI_THREAD_LEVEL
175+
* and set parameter requested accordingly. If the environment is not
176+
* set, or has an invalid value, requested is left unchanged.
177+
*/
178+
int ompi_getenv_mpi_thread_level(int *requested);
179+
180+
/**
181+
* Determine the thread level
182+
*
183+
* @param requested Thread support that is requested (IN)
184+
* @param provided Thread support that is provided (OUT)
185+
*/
186+
void ompi_mpi_thread_level(int requested, int *provided);
187+
166188
/**
167189
* Determine the thread level
168190
*

ompi/runtime/ompi_mpi_init.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* Copyright (c) 2021 Nanook Consulting. All rights reserved.
3030
* Copyright (c) 2021-2022 Triad National Security, LLC. All rights
3131
* reserved.
32+
* Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
3233
* $COPYRIGHT$
3334
*
3435
* Additional copyrights may follow
@@ -268,6 +269,42 @@ MPI_Fint *MPI_F08_STATUSES_IGNORE = NULL;
268269

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

272+
int ompi_getenv_mpi_thread_level(int *requested)
273+
{
274+
char* env;
275+
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;
296+
}
297+
if (0 == strcasecmp(env, "single") ||
298+
0 == strcasecmp(env, "MPI_THREAD_SINGLE") ||
299+
0 == strcmp(env, "0")) {
300+
return *requested = MPI_THREAD_SINGLE;
301+
}
302+
/* the env value is invalid... */
303+
return OMPI_ERR_BAD_PARAM;
304+
}
305+
return OMPI_SUCCESS;
306+
}
307+
271308
void ompi_mpi_thread_level(int requested, int *provided)
272309
{
273310
/**

0 commit comments

Comments
 (0)