1 /* Initialize OpenMP. 2 3 Copyright (C) 2017-2021 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 #ifdef _OPENMP 21 22 /* Specification. */ 23 # include <omp.h> 24 25 #endif 26 27 #include <stdlib.h> 28 29 #include "c-ctype.h" 30 31 #if defined _AIX 32 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) /* */ 37 { 38 unsigned long int ret = 0; 39 40 if (threads == NULL) 41 return ret; 42 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++; 47 48 /* Convert it from positive decimal to 'unsigned long'. */ 49 if (c_isdigit (*threads)) 50 { 51 char *endptr = NULL; 52 unsigned long int value = strtoul (threads, &endptr, 10); 53 54 if (endptr != NULL) 55 { 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; 64 } 65 } 66 67 return ret; 68 } 69 70 #endif 71 72 void 73 openmp_init (void) /* */ 74 { 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)) 83 { 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")); 87 88 if (!(omp_env_limit > 0 && omp_env_limit <= 32)) 89 setenv ("OMP_THREAD_LIMIT", "32", 1); 90 } 91 #endif 92 }