Skip to content

Commit fe5d20e

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 <abouteil@amd.com>
1 parent 9b06b34 commit fe5d20e

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

ompi/mpi/c/init.c.in

Lines changed: 7 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,12 @@ 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+
/* 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) {
5056
required = MPI_THREAD_MULTIPLE;
5157
}
5258
}

ompi/mpi/c/init_thread.c.in

Lines changed: 24 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,36 @@ 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+
/* 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;
5874
}
5975
}
6076

@@ -70,7 +86,7 @@ PROTOTYPE ERROR_CLASS init_thread(INT_OUT argc, ARGV argv, INT required,
7086
err = ompi_mpi_init(0, NULL, safe_required, provided, false);
7187
}
7288

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

0 commit comments

Comments
 (0)