@@ -269,37 +269,45 @@ MPI_Fint *MPI_F08_STATUSES_IGNORE = NULL;
269269
270270#include "mpif-c-constants.h"
271271
272+ static const char * ompi_thread_level_keywords [] = {"single" , "serialized" , "funneled" , "multiple" };
273+ static const char * ompi_thread_level_prepositions [] = {"mpi_thread_" , "thread_" , NULL };
274+ /* In the future integer MPI_ABI values for MPI_THREAD_SINGLE-MULTIPLE may be
275+ * non-sequential (but ordered) integer values. If you are implementing MPI
276+ * ABI changes please refer to
277+ * https://github.com/open-mpi/ompi/pull/13211#discussion_r2085086844
278+ */
279+ static const int ompi_thread_level_values [] = {MPI_THREAD_SINGLE , MPI_THREAD_SERIALIZED ,
280+ MPI_THREAD_FUNNELED , MPI_THREAD_MULTIPLE };
281+
272282int ompi_getenv_mpi_thread_level (int * requested )
273283{
274284 char * env ;
275285 if (NULL != (env = getenv ("OMPI_MPI_THREAD_LEVEL" ))) {
276- /* deal with string values, int values (no atoi, it doesn't error check) */
277- /* In the future integer MPI_ABI values for MPI_THREAD_SINGLE-MULTIPLE
278- * may be non-sequential (but ordered) integer values.
279- * If you are implementing MPI ABI changes please refer to
280- * https://github.com/open-mpi/ompi/pull/13211#discussion_r2085086844
281- */
282- if (0 == strcasecmp (env , "multiple" ) ||
283- 0 == strcasecmp (env , "MPI_THREAD_MULTIPLE" ) ||
284- 0 == strcmp (env , "3" )) {
285- return * requested = MPI_THREAD_MULTIPLE ;
286- }
287- if (0 == strcasecmp (env , "serialized" ) ||
288- 0 == strcasecmp (env , "MPI_THREAD_SERIALIZED" ) ||
289- 0 == strcmp (env , "2" )) {
290- return * requested = MPI_THREAD_SERIALIZED ;
291- }
292- if (0 == strcasecmp (env , "funneled" ) ||
293- 0 == strcasecmp (env , "MPI_THREAD_FUNNELED" ) ||
294- 0 == strcmp (env , "1" )) {
295- return * requested = MPI_THREAD_FUNNELED ;
286+ char * prep = NULL , * token = (char * ) env /* full match */ ;
287+ int pidx = 0 , found = strtol (env , & prep , 10 );
288+
289+ if (prep == env ) { /* no digits found */
290+ found = -1 ;
291+ while (NULL != (prep = (char * ) ompi_thread_level_prepositions [pidx ])) {
292+ if (0 == strncasecmp (prep , env , strlen (prep ))) {
293+ token = env + strlen (prep );
294+ break ; /* got a token let's find a match */
295+ }
296+ pidx ++ ;
297+ }
298+ const int nb_keywords = sizeof (ompi_thread_level_keywords )/sizeof (ompi_thread_level_keywords [0 ]);
299+ for (int i = 0 ; i < nb_keywords ; i ++ ) {
300+ if (0 == strncasecmp (ompi_thread_level_keywords [i ], token , strlen (token ))) {
301+ if (-1 != found ) { /* not the first match, bail out */
302+ return OMPI_ERR_BAD_PARAM ;
303+ }
304+ found = i ;
305+ }
306+ }
296307 }
297- if (0 == strcasecmp (env , "single" ) ||
298- 0 == strcasecmp (env , "MPI_THREAD_SINGLE" ) ||
299- 0 == strcmp (env , "0" )) {
300- return * requested = MPI_THREAD_SINGLE ;
308+ if (-1 != found ) {
309+ return * requested = ompi_thread_level_values [found ];
301310 }
302- /* the env value is invalid... */
303311 return OMPI_ERR_BAD_PARAM ;
304312 }
305313 return OMPI_SUCCESS ;
0 commit comments