Skip to content

Commit b481893

Browse files
committed
bugfix: Setting OMPI_MPI_THREAD_LEVEL to a value different than
`requested` in `MPI_Init_thread` would invoke the error handler, even though it is an useful override in some threaded library use cases. Signed-off-by: Aurelien Bouteiller <[email protected]>
1 parent 1d301f7 commit b481893

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

ompi/mpi/c/init.c.in

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* and Technology (RIST). All rights reserved.
1616
* Copyright (c) 2024 Triad National Security, LLC. All rights
1717
* reserved.
18+
* Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
1819
* $COPYRIGHT$
1920
*
2021
* Additional copyrights may follow
@@ -46,7 +47,8 @@ PROTOTYPE INT init(INT_OUT argc, ARGV argv)
4647

4748
if (NULL != (env = getenv("OMPI_MPI_THREAD_LEVEL"))) {
4849
required = atoi(env);
49-
if (required < MPI_THREAD_SINGLE || required > MPI_THREAD_MULTIPLE) {
50+
if (required != MPI_THREAD_SINGLE && required != MPI_THREAD_FUNNELED &&
51+
required != MPI_THREAD_SERIALIZED && required != MPI_THREAD_MULTIPLE) {
5052
required = MPI_THREAD_MULTIPLE;
5153
}
5254
}

ompi/mpi/c/init_thread.c.in

+20-9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* reserved.
1919
* Copyright (c) 2024 Triad National Security, LLC. All rights
2020
* reserved.
21+
* Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
2122
* $COPYRIGHT$
2223
*
2324
* Additional copyrights may follow
@@ -39,22 +40,32 @@
3940
PROTOTYPE ERROR_CLASS init_thread(INT_OUT argc, ARGV argv, INT required,
4041
INT_OUT provided)
4142
{
42-
int err, safe_required = MPI_THREAD_SERIALIZED;
43+
int err, err_arg_required = false, safe_required = MPI_THREAD_SERIALIZED;
4344
char *env;
4445

4546
ompi_hook_base_mpi_init_thread_top(argc, argv, required, provided);
4647

4748
/* Detect an incorrect thread support level, but dont report until we have the minimum
4849
* infrastructure setup.
4950
*/
50-
if( (MPI_THREAD_SINGLE == required) || (MPI_THREAD_SERIALIZED == required) ||
51-
(MPI_THREAD_FUNNELED == required) || (MPI_THREAD_MULTIPLE == required) ) {
51+
err_arg_required = (required != MPI_THREAD_SINGLE && required != MPI_THREAD_FUNNELED &&
52+
required != MPI_THREAD_SERIALIZED && required != MPI_THREAD_MULTIPLE);
53+
if (!err_arg_required) {
54+
safe_required = required;
55+
}
5256

53-
if (NULL != (env = getenv("OMPI_MPI_THREAD_LEVEL"))) {
54-
safe_required = atoi(env);
55-
}
56-
else {
57-
safe_required = required;
57+
/* check for environment overrides for required thread level. If
58+
* there is, check to see that it is a valid/supported thread level.
59+
* if it is lower than argument required, set it anyway, program can
60+
* check provided == required to verify that required has been ignored
61+
*/
62+
if (NULL != (env = getenv("OMPI_MPI_THREAD_LEVEL"))) {
63+
int env_required = atoi(env);
64+
err_arg_required = err_arg_required ||
65+
(env_required != MPI_THREAD_SINGLE && env_required != MPI_THREAD_FUNNELED &&
66+
env_required != MPI_THREAD_SERIALIZED && env_required != MPI_THREAD_MULTIPLE);
67+
if (!err_arg_required) {
68+
safe_required = env_required;
5869
}
5970
}
6071

@@ -70,7 +81,7 @@ PROTOTYPE ERROR_CLASS init_thread(INT_OUT argc, ARGV argv, INT required,
7081
err = ompi_mpi_init(0, NULL, safe_required, provided, false);
7182
}
7283

73-
if( safe_required != required ) {
84+
if (err_arg_required) {
7485
/* Trigger the error handler for the incorrect argument. Keep it separate from the
7586
* check on the ompi_mpi_init return and report a nice, meaningful error message to
7687
* the user. */

0 commit comments

Comments
 (0)