exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / omp-init.c
blobca99f041e2211650bf713888974afc2d7206378c
1 /* Initialize OpenMP.
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/>. */
18 #include <config.h>
20 /* Specification. */
21 #include <omp.h>
23 #include <stdlib.h>
25 #include "c-ctype.h"
27 #if defined _AIX
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;
36 if (threads == NULL)
37 return ret;
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++;
44 /* Convert it from positive decimal to 'unsigned long'. */
45 if (c_isdigit (*threads))
47 char *endptr = NULL;
48 unsigned long int value = strtoul (threads, &endptr, 10);
50 if (endptr != NULL)
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;
63 return ret;
66 #endif
68 void
69 openmp_init (void)
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))
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);
87 #endif