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