Skip to content

Commit 8ad7579

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 9b06b34 commit 8ad7579

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

ompi/mpi/c/init.c.in

Lines changed: 3 additions & 1 deletion
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

Lines changed: 20 additions & 8 deletions
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
@@ -40,21 +41,32 @@ PROTOTYPE ERROR_CLASS init_thread(INT_OUT argc, ARGV argv, INT required,
4041
INT_OUT provided)
4142
{
4243
int err, safe_required = MPI_THREAD_SERIALIZED;
44+
bool err_arg_required = false;
4345
char *env;
4446

4547
ompi_hook_base_mpi_init_thread_top(argc, argv, required, provided);
4648

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

53-
if (NULL != (env = getenv("OMPI_MPI_THREAD_LEVEL"))) {
54-
safe_required = atoi(env);
55-
}
56-
else {
57-
safe_required = required;
58+
/* check for environment overrides for required thread level. If
59+
* there is, check to see that it is a valid/supported thread level.
60+
* If valid, the environment variable always override the provided thread
61+
* level (even if lower than argument `required`). A user program can
62+
* check `provided != required` to check if `required` has been overruled.
63+
*/
64+
if (NULL != (env = getenv("OMPI_MPI_THREAD_LEVEL"))) {
65+
int env_required = atoi(env);
66+
err_arg_required |= (env_required != MPI_THREAD_SINGLE && env_required != MPI_THREAD_FUNNELED &&
67+
env_required != MPI_THREAD_SERIALIZED && env_required != MPI_THREAD_MULTIPLE);
68+
if (!err_arg_required) {
69+
safe_required = env_required;
5870
}
5971
}
6072

@@ -70,7 +82,7 @@ PROTOTYPE ERROR_CLASS init_thread(INT_OUT argc, ARGV argv, INT required,
7082
err = ompi_mpi_init(0, NULL, safe_required, provided, false);
7183
}
7284

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

0 commit comments

Comments
 (0)