(root)/
gettext-0.22.4/
gettext-tools/
gnulib-lib/
omp-init.c
       1  /* Initialize OpenMP.
       2  
       3     Copyright (C) 2017-2023 Free Software Foundation, Inc.
       4  
       5     This file is free software: you can redistribute it and/or modify
       6     it under the terms of the GNU Lesser General Public License as
       7     published by the Free Software Foundation, either version 3 of the
       8     License, or (at your option) any later version.
       9  
      10     This file is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13     GNU Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public License
      16     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      17  
      18  #include <config.h>
      19  
      20  /* Specification.  */
      21  #include <omp.h>
      22  
      23  #include <stdlib.h>
      24  
      25  #include "c-ctype.h"
      26  
      27  #if defined _AIX
      28  
      29  /* Parse OMP environment variables without dependence on OMP.
      30     Return 0 for invalid values.  */
      31  static unsigned long int
      32  parse_omp_threads (char const* threads)
      33  {
      34    unsigned long int ret = 0;
      35  
      36    if (threads == NULL)
      37      return ret;
      38  
      39    /* The OpenMP spec says that the value assigned to the environment variables
      40       "may have leading and trailing white space".  */
      41    while (*threads != '\0' && c_isspace (*threads))
      42      threads++;
      43  
      44    /* Convert it from positive decimal to 'unsigned long'.  */
      45    if (c_isdigit (*threads))
      46      {
      47        char *endptr = NULL;
      48        unsigned long int value = strtoul (threads, &endptr, 10);
      49  
      50        if (endptr != NULL)
      51          {
      52            while (*endptr != '\0' && c_isspace (*endptr))
      53              endptr++;
      54            if (*endptr == '\0')
      55              return value;
      56            /* Also accept the first value in a nesting level,
      57               since we can't determine the nesting level from env vars.  */
      58            else if (*endptr == ',')
      59              return value;
      60          }
      61      }
      62  
      63    return ret;
      64  }
      65  
      66  #endif
      67  
      68  void
      69  openmp_init (void)
      70  {
      71    /* On AIX 7.2, in 32-bit mode, use of OpenMP on machines with 64 or 128
      72       processors makes the program fail with an error message
      73       "1587-120 SMP runtime library error. Memory allocation failed when creating thread number 62.".
      74       The workaround is to tell the OpenMP library to create fewer than 62
      75       threads.  This can be done through the OMP_THREAD_LIMIT environment
      76       variable.  */
      77  #if defined _AIX
      78    if (sizeof (long) == sizeof (int))
      79      {
      80        /* Ensure that OMP_THREAD_LIMIT has a value <= 32.  */
      81        unsigned long int omp_env_limit =
      82          parse_omp_threads (getenv ("OMP_THREAD_LIMIT"));
      83  
      84        if (!(omp_env_limit > 0 && omp_env_limit <= 32))
      85          setenv ("OMP_THREAD_LIMIT", "32", 1);
      86      }
      87  #endif
      88  }