Skip to content

Commit 5059134

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>
1 parent fe5d20e commit 5059134

File tree

4 files changed

+64
-25
lines changed

4 files changed

+64
-25
lines changed

ompi/mpi/c/init.c.in

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,13 @@ PROTOTYPE INT init(INT_OUT argc, ARGV argv)
3838
{
3939
int err;
4040
int provided;
41-
char *env;
4241
int required = MPI_THREAD_SINGLE;
4342

4443
/* check for environment overrides for required thread level. If
4544
there is, check to see that it is a valid/supported thread level.
4645
If not, default to MPI_THREAD_MULTIPLE. */
47-
48-
if (NULL != (env = getenv("OMPI_MPI_THREAD_LEVEL"))) {
49-
required = atoi(env);
50-
/* In the future we may have to contend with non-sequential (MPI ABI) values
51-
* If you are implementing MPI ABI changes please refer to
52-
* https://github.com/open-mpi/ompi/pull/13211#discussion_r2085086844
53-
*/
54-
if (required != MPI_THREAD_SINGLE && required != MPI_THREAD_FUNNELED &&
55-
required != MPI_THREAD_SERIALIZED && required != MPI_THREAD_MULTIPLE) {
56-
required = MPI_THREAD_MULTIPLE;
57-
}
46+
if (OMPI_SUCCESS > ompi_getenv_mpi_thread_level(&required)) {
47+
required = MPI_THREAD_MULTIPLE;
5848
}
5949

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

ompi/mpi/c/init_thread.c.in

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ PROTOTYPE ERROR_CLASS init_thread(INT_OUT argc, ARGV argv, INT required,
4242
{
4343
int err, safe_required = MPI_THREAD_SERIALIZED;
4444
bool err_arg_required = false;
45-
char *env;
4645

4746
ompi_hook_base_mpi_init_thread_top(argc, argv, required, provided);
4847

4948
/* Detect an incorrect thread support level, but dont report until we have the minimum
5049
* infrastructure setup.
50+
* In the future integer MPI_ABI values for MPI_THREAD_SINGLE-MULTIPLE
51+
* may have gaps between them, so just checking the range is not enough.
5152
*/
5253
err_arg_required = (required != MPI_THREAD_SINGLE && required != MPI_THREAD_FUNNELED &&
5354
required != MPI_THREAD_SERIALIZED && required != MPI_THREAD_MULTIPLE);
@@ -61,18 +62,7 @@ PROTOTYPE ERROR_CLASS init_thread(INT_OUT argc, ARGV argv, INT required,
6162
* level (even if lower than argument `required`). A user program can
6263
* check `provided != required` to check if `required` has been overruled.
6364
*/
64-
if (NULL != (env = getenv("OMPI_MPI_THREAD_LEVEL"))) {
65-
int env_required = atoi(env);
66-
/* In the future we may have to contend with non-sequential (MPI ABI) values
67-
* If you are implementing MPI ABI changes please refer to
68-
* https://github.com/open-mpi/ompi/pull/13211#discussion_r2085086844
69-
*/
70-
err_arg_required |= (env_required != MPI_THREAD_SINGLE && env_required != MPI_THREAD_FUNNELED &&
71-
env_required != MPI_THREAD_SERIALIZED && env_required != MPI_THREAD_MULTIPLE);
72-
if (!err_arg_required) {
73-
safe_required = env_required;
74-
}
75-
}
65+
err_arg_required |= (OMPI_SUCCESS > ompi_getenv_mpi_thread_level(&safe_required));
7666

7767
*provided = safe_required;
7868

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)