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