Skip to content

bugfix: Setting OMPI_MPI_THREAD_LEVEL to a value different than requested crashes #13211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ompi/mpi/c/init.c.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* and Technology (RIST). All rights reserved.
* Copyright (c) 2024 Triad National Security, LLC. All rights
* reserved.
* Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
Expand Down Expand Up @@ -46,7 +47,8 @@ PROTOTYPE INT init(INT_OUT argc, ARGV argv)

if (NULL != (env = getenv("OMPI_MPI_THREAD_LEVEL"))) {
required = atoi(env);
if (required < MPI_THREAD_SINGLE || required > MPI_THREAD_MULTIPLE) {
if (required != MPI_THREAD_SINGLE && required != MPI_THREAD_FUNNELED &&
required != MPI_THREAD_SERIALIZED && required != MPI_THREAD_MULTIPLE) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious: what's the need for this? Doesn't MPI specify the relationship between all the MPI_THREAD_* values such that < SINGLE and > MULTIPLE is guaranteed to be invalid?

Copy link
Member Author

@abouteiller abouteiller May 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the new MPI ABI, values are still ordered but not consecutive, so users could use one of the invalid intermediate values.

https://github.com/mpi-forum/mpi-abi-stubs/blob/e89a80017a3fe9a05d903ced2564c6342d678165/mpi.h#L334

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sigh. Ok.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this is worth a comment for those of us who look at this code and wonder why it is this way (and to make sure someone doesn't mistakenly return it to <SINGLE || >MULTIPLE someday). Can you please add comments in these places and describe that checking each individual value is required because of ABI? Thanks.

required = MPI_THREAD_MULTIPLE;
}
}
Expand Down
28 changes: 20 additions & 8 deletions ompi/mpi/c/init_thread.c.in
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* reserved.
* Copyright (c) 2024 Triad National Security, LLC. All rights
* reserved.
* Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
Expand All @@ -40,21 +41,32 @@ PROTOTYPE ERROR_CLASS init_thread(INT_OUT argc, ARGV argv, INT required,
INT_OUT provided)
{
int err, safe_required = MPI_THREAD_SERIALIZED;
bool err_arg_required = false;
char *env;

ompi_hook_base_mpi_init_thread_top(argc, argv, required, provided);

/* Detect an incorrect thread support level, but dont report until we have the minimum
* infrastructure setup.
*/
if( (MPI_THREAD_SINGLE == required) || (MPI_THREAD_SERIALIZED == required) ||
(MPI_THREAD_FUNNELED == required) || (MPI_THREAD_MULTIPLE == required) ) {
err_arg_required = (required != MPI_THREAD_SINGLE && required != MPI_THREAD_FUNNELED &&
required != MPI_THREAD_SERIALIZED && required != MPI_THREAD_MULTIPLE);
if (!err_arg_required) {
safe_required = required;
}

if (NULL != (env = getenv("OMPI_MPI_THREAD_LEVEL"))) {
safe_required = atoi(env);
}
else {
safe_required = required;
/* check for environment overrides for required thread level. If
* there is, check to see that it is a valid/supported thread level.
* If valid, the environment variable always override the provided thread
* level (even if lower than argument `required`). A user program can
* check `provided != required` to check if `required` has been overruled.
*/
if (NULL != (env = getenv("OMPI_MPI_THREAD_LEVEL"))) {
int env_required = atoi(env);
err_arg_required |= (env_required != MPI_THREAD_SINGLE && env_required != MPI_THREAD_FUNNELED &&
env_required != MPI_THREAD_SERIALIZED && env_required != MPI_THREAD_MULTIPLE);
if (!err_arg_required) {
safe_required = env_required;
}
}

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

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