3 Copyright (C) 2017-2024 Free Software Foundation, Inc.
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.
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.
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/>. */
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
)
34 unsigned long int ret
= 0;
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
))
44 /* Convert it from positive decimal to 'unsigned long'. */
45 if (c_isdigit (*threads
))
48 unsigned long int value
= strtoul (threads
, &endptr
, 10);
52 while (*endptr
!= '\0' && c_isspace (*endptr
))
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
== ',')
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
78 if (sizeof (long) == sizeof (int))
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"));
84 if (!(omp_env_limit
> 0 && omp_env_limit
<= 32))
85 setenv ("OMP_THREAD_LIMIT", "32", 1);