Skip to content

Commit defea5f

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> (cherry picked from commit 27332fc)
1 parent a16908d commit defea5f

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

ompi/mpi/c/init.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* Copyright (c) 2007-2008 Sun Microsystems, Inc. All rights reserved.
1414
* Copyright (c) 2015 Research Organization for Information Science
1515
* and Technology (RIST). All rights reserved.
16+
* Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
1617
* $COPYRIGHT$
1718
*
1819
* Additional copyrights may follow
@@ -54,7 +55,12 @@ int MPI_Init(int *argc, char ***argv)
5455

5556
if (NULL != (env = getenv("OMPI_MPI_THREAD_LEVEL"))) {
5657
required = atoi(env);
57-
if (required < MPI_THREAD_SINGLE || required > MPI_THREAD_MULTIPLE) {
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) {
5864
required = MPI_THREAD_MULTIPLE;
5965
}
6066
}

ompi/mpi/c/init_thread.c

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* Copyright (c) 2015-2018 Cisco Systems, Inc. All rights reserved
1717
* Copyright (c) 2016 Los Alamos National Security, LLC. All rights
1818
* reserved.
19+
* Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
1920
* $COPYRIGHT$
2021
*
2122
* Additional copyrights may follow
@@ -48,21 +49,36 @@ int MPI_Init_thread(int *argc, char ***argv, int required,
4849
int *provided)
4950
{
5051
int err, safe_required = MPI_THREAD_SERIALIZED;
52+
bool err_arg_required = false;
5153
char *env;
5254

5355
ompi_hook_base_mpi_init_thread_top(argc, argv, required, provided);
5456

5557
/* Detect an incorrect thread support level, but dont report until we have the minimum
5658
* infrastructure setup.
5759
*/
58-
if( (MPI_THREAD_SINGLE == required) || (MPI_THREAD_SERIALIZED == required) ||
59-
(MPI_THREAD_FUNNELED == required) || (MPI_THREAD_MULTIPLE == required) ) {
60+
err_arg_required = (required != MPI_THREAD_SINGLE && required != MPI_THREAD_FUNNELED &&
61+
required != MPI_THREAD_SERIALIZED && required != MPI_THREAD_MULTIPLE);
62+
if (!err_arg_required) {
63+
safe_required = required;
64+
}
6065

61-
if (NULL != (env = getenv("OMPI_MPI_THREAD_LEVEL"))) {
62-
safe_required = atoi(env);
63-
}
64-
else {
65-
safe_required = required;
66+
/* check for environment overrides for required thread level. If
67+
* there is, check to see that it is a valid/supported thread level.
68+
* If valid, the environment variable always override the provided thread
69+
* level (even if lower than argument `required`). A user program can
70+
* check `provided != required` to check if `required` has been overruled.
71+
*/
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;
6682
}
6783
}
6884

@@ -78,7 +94,7 @@ int MPI_Init_thread(int *argc, char ***argv, int required,
7894
err = ompi_mpi_init(0, NULL, safe_required, provided, false);
7995
}
8096

81-
if( safe_required != required ) {
97+
if (err_arg_required) {
8298
/* Trigger the error handler for the incorrect argument. Keep it separate from the
8399
* check on the ompi_mpi_init return and report a nice, meaningful error message to
84100
* the user. */

0 commit comments

Comments
 (0)