fchmod-tests, fchmodat tests, lchmod tests: Add more tests.
[gnulib.git] / lib / omp-init.c
blob466a69118c36f8438befcfa807befcbe9a128864
1 /* Initialize OpenMP.
3 Copyright (C) 2017-2021 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 This program 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <https://www.gnu.org/licenses/>. */
18 #include <config.h>
20 #ifdef _OPENMP
22 /* Specification. */
23 # include <omp.h>
25 #endif
27 #include <stdlib.h>
29 #include "c-ctype.h"
31 #if defined _AIX
33 /* Parse OMP environment variables without dependence on OMP.
34 Return 0 for invalid values. */
35 static unsigned long int
36 parse_omp_threads (char const* threads)
38 unsigned long int ret = 0;
40 if (threads == NULL)
41 return ret;
43 /* The OpenMP spec says that the value assigned to the environment variables
44 "may have leading and trailing white space". */
45 while (*threads != '\0' && c_isspace (*threads))
46 threads++;
48 /* Convert it from positive decimal to 'unsigned long'. */
49 if (c_isdigit (*threads))
51 char *endptr = NULL;
52 unsigned long int value = strtoul (threads, &endptr, 10);
54 if (endptr != NULL)
56 while (*endptr != '\0' && c_isspace (*endptr))
57 endptr++;
58 if (*endptr == '\0')
59 return value;
60 /* Also accept the first value in a nesting level,
61 since we can't determine the nesting level from env vars. */
62 else if (*endptr == ',')
63 return value;
67 return ret;
70 #endif
72 void
73 openmp_init (void)
75 /* On AIX 7.2, in 32-bit mode, use of OpenMP on machines with 64 or 128
76 processors makes the program fail with an error message
77 "1587-120 SMP runtime library error. Memory allocation failed when creating thread number 62.".
78 The workaround is to tell the OpenMP library to create fewer than 62
79 threads. This can be done through the OMP_THREAD_LIMIT environment
80 variable. */
81 #if defined _AIX
82 if (sizeof (long) == sizeof (int))
84 /* Ensure that OMP_THREAD_LIMIT has a value <= 32. */
85 unsigned long int omp_env_limit =
86 parse_omp_threads (getenv ("OMP_THREAD_LIMIT"));
88 if (!(omp_env_limit > 0 && omp_env_limit <= 32))
89 setenv ("OMP_THREAD_LIMIT", "32", 1);
91 #endif