1 /*
2  * kmp_settings.cpp -- Initialize environment variables
3  */
4 
5 //===----------------------------------------------------------------------===//
6 //
7 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
8 // See https://llvm.org/LICENSE.txt for license information.
9 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "kmp.h"
14 #include "kmp_affinity.h"
15 #include "kmp_atomic.h"
16 #if KMP_USE_HIER_SCHED
17 #include "kmp_dispatch_hier.h"
18 #endif
19 #include "kmp_environment.h"
20 #include "kmp_i18n.h"
21 #include "kmp_io.h"
22 #include "kmp_itt.h"
23 #include "kmp_lock.h"
24 #include "kmp_settings.h"
25 #include "kmp_str.h"
26 #include "kmp_wrapper_getpid.h"
27 #include <ctype.h> // toupper()
28 #if OMPD_SUPPORT
29 #include "ompd-specific.h"
30 #endif
31 
32 static int __kmp_env_toPrint(char const *name, int flag);
33 
34 bool __kmp_env_format = 0; // 0 - old format; 1 - new format
35 
36 // -----------------------------------------------------------------------------
37 // Helper string functions. Subject to move to kmp_str.
38 
39 #ifdef USE_LOAD_BALANCE
__kmp_convert_to_double(char const * s)40 static double __kmp_convert_to_double(char const *s) {
41   double result;
42 
43   if (KMP_SSCANF(s, "%lf", &result) < 1) {
44     result = 0.0;
45   }
46 
47   return result;
48 }
49 #endif
50 
51 #ifdef KMP_DEBUG
__kmp_readstr_with_sentinel(char * dest,char const * src,size_t len,char sentinel)52 static unsigned int __kmp_readstr_with_sentinel(char *dest, char const *src,
53                                                 size_t len, char sentinel) {
54   unsigned int i;
55   for (i = 0; i < len; i++) {
56     if ((*src == '\0') || (*src == sentinel)) {
57       break;
58     }
59     *(dest++) = *(src++);
60   }
61   *dest = '\0';
62   return i;
63 }
64 #endif
65 
__kmp_match_with_sentinel(char const * a,char const * b,size_t len,char sentinel)66 static int __kmp_match_with_sentinel(char const *a, char const *b, size_t len,
67                                      char sentinel) {
68   size_t l = 0;
69 
70   if (a == NULL)
71     a = "";
72   if (b == NULL)
73     b = "";
74   while (*a && *b && *b != sentinel) {
75     char ca = *a, cb = *b;
76 
77     if (ca >= 'a' && ca <= 'z')
78       ca -= 'a' - 'A';
79     if (cb >= 'a' && cb <= 'z')
80       cb -= 'a' - 'A';
81     if (ca != cb)
82       return FALSE;
83     ++l;
84     ++a;
85     ++b;
86   }
87   return l >= len;
88 }
89 
90 // Expected usage:
91 //     token is the token to check for.
92 //     buf is the string being parsed.
93 //     *end returns the char after the end of the token.
94 //        it is not modified unless a match occurs.
95 //
96 // Example 1:
97 //
98 //     if (__kmp_match_str("token", buf, *end) {
99 //         <do something>
100 //         buf = end;
101 //     }
102 //
103 //  Example 2:
104 //
105 //     if (__kmp_match_str("token", buf, *end) {
106 //         char *save = **end;
107 //         **end = sentinel;
108 //         <use any of the __kmp*_with_sentinel() functions>
109 //         **end = save;
110 //         buf = end;
111 //     }
112 
__kmp_match_str(char const * token,char const * buf,const char ** end)113 static int __kmp_match_str(char const *token, char const *buf,
114                            const char **end) {
115 
116   KMP_ASSERT(token != NULL);
117   KMP_ASSERT(buf != NULL);
118   KMP_ASSERT(end != NULL);
119 
120   while (*token && *buf) {
121     char ct = *token, cb = *buf;
122 
123     if (ct >= 'a' && ct <= 'z')
124       ct -= 'a' - 'A';
125     if (cb >= 'a' && cb <= 'z')
126       cb -= 'a' - 'A';
127     if (ct != cb)
128       return FALSE;
129     ++token;
130     ++buf;
131   }
132   if (*token) {
133     return FALSE;
134   }
135   *end = buf;
136   return TRUE;
137 }
138 
139 #if KMP_OS_DARWIN
__kmp_round4k(size_t size)140 static size_t __kmp_round4k(size_t size) {
141   size_t _4k = 4 * 1024;
142   if (size & (_4k - 1)) {
143     size &= ~(_4k - 1);
144     if (size <= KMP_SIZE_T_MAX - _4k) {
145       size += _4k; // Round up if there is no overflow.
146     }
147   }
148   return size;
149 } // __kmp_round4k
150 #endif
151 
152 /* Here, multipliers are like __kmp_convert_to_seconds, but floating-point
153    values are allowed, and the return value is in milliseconds.  The default
154    multiplier is milliseconds.  Returns INT_MAX only if the value specified
155    matches "infinit*".  Returns -1 if specified string is invalid. */
__kmp_convert_to_milliseconds(char const * data)156 int __kmp_convert_to_milliseconds(char const *data) {
157   int ret, nvalues, factor;
158   char mult, extra;
159   double value;
160 
161   if (data == NULL)
162     return (-1);
163   if (__kmp_str_match("infinit", -1, data))
164     return (INT_MAX);
165   value = (double)0.0;
166   mult = '\0';
167 #if KMP_OS_WINDOWS && KMP_MSVC_COMPAT
168   // On Windows, each %c parameter needs additional size parameter for sscanf_s
169   nvalues = KMP_SSCANF(data, "%lf%c%c", &value, &mult, 1, &extra, 1);
170 #else
171   nvalues = KMP_SSCANF(data, "%lf%c%c", &value, &mult, &extra);
172 #endif
173   if (nvalues < 1)
174     return (-1);
175   if (nvalues == 1)
176     mult = '\0';
177   if (nvalues == 3)
178     return (-1);
179 
180   if (value < 0)
181     return (-1);
182 
183   switch (mult) {
184   case '\0':
185     /*  default is milliseconds  */
186     factor = 1;
187     break;
188   case 's':
189   case 'S':
190     factor = 1000;
191     break;
192   case 'm':
193   case 'M':
194     factor = 1000 * 60;
195     break;
196   case 'h':
197   case 'H':
198     factor = 1000 * 60 * 60;
199     break;
200   case 'd':
201   case 'D':
202     factor = 1000 * 24 * 60 * 60;
203     break;
204   default:
205     return (-1);
206   }
207 
208   if (value >= ((INT_MAX - 1) / factor))
209     ret = INT_MAX - 1; /* Don't allow infinite value here */
210   else
211     ret = (int)(value * (double)factor); /* truncate to int  */
212 
213   return ret;
214 }
215 
__kmp_strcasecmp_with_sentinel(char const * a,char const * b,char sentinel)216 static int __kmp_strcasecmp_with_sentinel(char const *a, char const *b,
217                                           char sentinel) {
218   if (a == NULL)
219     a = "";
220   if (b == NULL)
221     b = "";
222   while (*a && *b && *b != sentinel) {
223     char ca = *a, cb = *b;
224 
225     if (ca >= 'a' && ca <= 'z')
226       ca -= 'a' - 'A';
227     if (cb >= 'a' && cb <= 'z')
228       cb -= 'a' - 'A';
229     if (ca != cb)
230       return (int)(unsigned char)*a - (int)(unsigned char)*b;
231     ++a;
232     ++b;
233   }
234   return *a                       ? (*b && *b != sentinel)
235                                         ? (int)(unsigned char)*a - (int)(unsigned char)*b
236                                         : 1
237          : (*b && *b != sentinel) ? -1
238                                   : 0;
239 }
240 
241 // =============================================================================
242 // Table structures and helper functions.
243 
244 typedef struct __kmp_setting kmp_setting_t;
245 typedef struct __kmp_stg_ss_data kmp_stg_ss_data_t;
246 typedef struct __kmp_stg_wp_data kmp_stg_wp_data_t;
247 typedef struct __kmp_stg_fr_data kmp_stg_fr_data_t;
248 
249 typedef void (*kmp_stg_parse_func_t)(char const *name, char const *value,
250                                      void *data);
251 typedef void (*kmp_stg_print_func_t)(kmp_str_buf_t *buffer, char const *name,
252                                      void *data);
253 
254 struct __kmp_setting {
255   char const *name; // Name of setting (environment variable).
256   kmp_stg_parse_func_t parse; // Parser function.
257   kmp_stg_print_func_t print; // Print function.
258   void *data; // Data passed to parser and printer.
259   int set; // Variable set during this "session"
260   //     (__kmp_env_initialize() or kmp_set_defaults() call).
261   int defined; // Variable set in any "session".
262 }; // struct __kmp_setting
263 
264 struct __kmp_stg_ss_data {
265   size_t factor; // Default factor: 1 for KMP_STACKSIZE, 1024 for others.
266   kmp_setting_t **rivals; // Array of pointers to rivals (including itself).
267 }; // struct __kmp_stg_ss_data
268 
269 struct __kmp_stg_wp_data {
270   int omp; // 0 -- KMP_LIBRARY, 1 -- OMP_WAIT_POLICY.
271   kmp_setting_t **rivals; // Array of pointers to rivals (including itself).
272 }; // struct __kmp_stg_wp_data
273 
274 struct __kmp_stg_fr_data {
275   int force; // 0 -- KMP_DETERMINISTIC_REDUCTION, 1 -- KMP_FORCE_REDUCTION.
276   kmp_setting_t **rivals; // Array of pointers to rivals (including itself).
277 }; // struct __kmp_stg_fr_data
278 
279 static int __kmp_stg_check_rivals( // 0 -- Ok, 1 -- errors found.
280     char const *name, // Name of variable.
281     char const *value, // Value of the variable.
282     kmp_setting_t **rivals // List of rival settings (must include current one).
283 );
284 
285 // -----------------------------------------------------------------------------
286 // Helper parse functions.
287 
__kmp_stg_parse_bool(char const * name,char const * value,int * out)288 static void __kmp_stg_parse_bool(char const *name, char const *value,
289                                  int *out) {
290   if (__kmp_str_match_true(value)) {
291     *out = TRUE;
292   } else if (__kmp_str_match_false(value)) {
293     *out = FALSE;
294   } else {
295     __kmp_msg(kmp_ms_warning, KMP_MSG(BadBoolValue, name, value),
296               KMP_HNT(ValidBoolValues), __kmp_msg_null);
297   }
298 } // __kmp_stg_parse_bool
299 
300 // placed here in order to use __kmp_round4k static function
__kmp_check_stksize(size_t * val)301 void __kmp_check_stksize(size_t *val) {
302   // if system stack size is too big then limit the size for worker threads
303   if (*val > KMP_DEFAULT_STKSIZE * 16) // just a heuristics...
304     *val = KMP_DEFAULT_STKSIZE * 16;
305   if (*val < KMP_MIN_STKSIZE)
306     *val = KMP_MIN_STKSIZE;
307   if (*val > KMP_MAX_STKSIZE)
308     *val = KMP_MAX_STKSIZE; // dead code currently, but may work in future
309 #if KMP_OS_DARWIN
310   *val = __kmp_round4k(*val);
311 #endif // KMP_OS_DARWIN
312 }
313 
__kmp_stg_parse_size(char const * name,char const * value,size_t size_min,size_t size_max,int * is_specified,size_t * out,size_t factor)314 static void __kmp_stg_parse_size(char const *name, char const *value,
315                                  size_t size_min, size_t size_max,
316                                  int *is_specified, size_t *out,
317                                  size_t factor) {
318   char const *msg = NULL;
319 #if KMP_OS_DARWIN
320   size_min = __kmp_round4k(size_min);
321   size_max = __kmp_round4k(size_max);
322 #endif // KMP_OS_DARWIN
323   if (value) {
324     if (is_specified != NULL) {
325       *is_specified = 1;
326     }
327     __kmp_str_to_size(value, out, factor, &msg);
328     if (msg == NULL) {
329       if (*out > size_max) {
330         *out = size_max;
331         msg = KMP_I18N_STR(ValueTooLarge);
332       } else if (*out < size_min) {
333         *out = size_min;
334         msg = KMP_I18N_STR(ValueTooSmall);
335       } else {
336 #if KMP_OS_DARWIN
337         size_t round4k = __kmp_round4k(*out);
338         if (*out != round4k) {
339           *out = round4k;
340           msg = KMP_I18N_STR(NotMultiple4K);
341         }
342 #endif
343       }
344     } else {
345       // If integer overflow occurred, * out == KMP_SIZE_T_MAX. Cut it to
346       // size_max silently.
347       if (*out < size_min) {
348         *out = size_max;
349       } else if (*out > size_max) {
350         *out = size_max;
351       }
352     }
353     if (msg != NULL) {
354       // Message is not empty. Print warning.
355       kmp_str_buf_t buf;
356       __kmp_str_buf_init(&buf);
357       __kmp_str_buf_print_size(&buf, *out);
358       KMP_WARNING(ParseSizeIntWarn, name, value, msg);
359       KMP_INFORM(Using_str_Value, name, buf.str);
360       __kmp_str_buf_free(&buf);
361     }
362   }
363 } // __kmp_stg_parse_size
364 
__kmp_stg_parse_str(char const * name,char const * value,char ** out)365 static void __kmp_stg_parse_str(char const *name, char const *value,
366                                 char **out) {
367   __kmp_str_free(out);
368   *out = __kmp_str_format("%s", value);
369 } // __kmp_stg_parse_str
370 
__kmp_stg_parse_int(char const * name,char const * value,int min,int max,int * out)371 static void __kmp_stg_parse_int(
372     char const
373         *name, // I: Name of environment variable (used in warning messages).
374     char const *value, // I: Value of environment variable to parse.
375     int min, // I: Minimum allowed value.
376     int max, // I: Maximum allowed value.
377     int *out // O: Output (parsed) value.
378 ) {
379   char const *msg = NULL;
380   kmp_uint64 uint = *out;
381   __kmp_str_to_uint(value, &uint, &msg);
382   if (msg == NULL) {
383     if (uint < (unsigned int)min) {
384       msg = KMP_I18N_STR(ValueTooSmall);
385       uint = min;
386     } else if (uint > (unsigned int)max) {
387       msg = KMP_I18N_STR(ValueTooLarge);
388       uint = max;
389     }
390   } else {
391     // If overflow occurred msg contains error message and uint is very big. Cut
392     // tmp it to INT_MAX.
393     if (uint < (unsigned int)min) {
394       uint = min;
395     } else if (uint > (unsigned int)max) {
396       uint = max;
397     }
398   }
399   if (msg != NULL) {
400     // Message is not empty. Print warning.
401     kmp_str_buf_t buf;
402     KMP_WARNING(ParseSizeIntWarn, name, value, msg);
403     __kmp_str_buf_init(&buf);
404     __kmp_str_buf_print(&buf, "%" KMP_UINT64_SPEC "", uint);
405     KMP_INFORM(Using_uint64_Value, name, buf.str);
406     __kmp_str_buf_free(&buf);
407   }
408   __kmp_type_convert(uint, out);
409 } // __kmp_stg_parse_int
410 
411 #if KMP_DEBUG_ADAPTIVE_LOCKS
__kmp_stg_parse_file(char const * name,char const * value,const char * suffix,char ** out)412 static void __kmp_stg_parse_file(char const *name, char const *value,
413                                  const char *suffix, char **out) {
414   char buffer[256];
415   char *t;
416   int hasSuffix;
417   __kmp_str_free(out);
418   t = (char *)strrchr(value, '.');
419   hasSuffix = t && __kmp_str_eqf(t, suffix);
420   t = __kmp_str_format("%s%s", value, hasSuffix ? "" : suffix);
421   __kmp_expand_file_name(buffer, sizeof(buffer), t);
422   __kmp_str_free(&t);
423   *out = __kmp_str_format("%s", buffer);
424 } // __kmp_stg_parse_file
425 #endif
426 
427 #ifdef KMP_DEBUG
428 static char *par_range_to_print = NULL;
429 
__kmp_stg_parse_par_range(char const * name,char const * value,int * out_range,char * out_routine,char * out_file,int * out_lb,int * out_ub)430 static void __kmp_stg_parse_par_range(char const *name, char const *value,
431                                       int *out_range, char *out_routine,
432                                       char *out_file, int *out_lb,
433                                       int *out_ub) {
434   const char *par_range_value;
435   size_t len = KMP_STRLEN(value) + 1;
436   par_range_to_print = (char *)KMP_INTERNAL_MALLOC(len + 1);
437   KMP_STRNCPY_S(par_range_to_print, len + 1, value, len + 1);
438   __kmp_par_range = +1;
439   __kmp_par_range_lb = 0;
440   __kmp_par_range_ub = INT_MAX;
441   for (;;) {
442     unsigned int len;
443     if (!value || *value == '\0') {
444       break;
445     }
446     if (!__kmp_strcasecmp_with_sentinel("routine", value, '=')) {
447       par_range_value = strchr(value, '=') + 1;
448       if (!par_range_value)
449         goto par_range_error;
450       value = par_range_value;
451       len = __kmp_readstr_with_sentinel(out_routine, value,
452                                         KMP_PAR_RANGE_ROUTINE_LEN - 1, ',');
453       if (len == 0) {
454         goto par_range_error;
455       }
456       value = strchr(value, ',');
457       if (value != NULL) {
458         value++;
459       }
460       continue;
461     }
462     if (!__kmp_strcasecmp_with_sentinel("filename", value, '=')) {
463       par_range_value = strchr(value, '=') + 1;
464       if (!par_range_value)
465         goto par_range_error;
466       value = par_range_value;
467       len = __kmp_readstr_with_sentinel(out_file, value,
468                                         KMP_PAR_RANGE_FILENAME_LEN - 1, ',');
469       if (len == 0) {
470         goto par_range_error;
471       }
472       value = strchr(value, ',');
473       if (value != NULL) {
474         value++;
475       }
476       continue;
477     }
478     if ((!__kmp_strcasecmp_with_sentinel("range", value, '=')) ||
479         (!__kmp_strcasecmp_with_sentinel("incl_range", value, '='))) {
480       par_range_value = strchr(value, '=') + 1;
481       if (!par_range_value)
482         goto par_range_error;
483       value = par_range_value;
484       if (KMP_SSCANF(value, "%d:%d", out_lb, out_ub) != 2) {
485         goto par_range_error;
486       }
487       *out_range = +1;
488       value = strchr(value, ',');
489       if (value != NULL) {
490         value++;
491       }
492       continue;
493     }
494     if (!__kmp_strcasecmp_with_sentinel("excl_range", value, '=')) {
495       par_range_value = strchr(value, '=') + 1;
496       if (!par_range_value)
497         goto par_range_error;
498       value = par_range_value;
499       if (KMP_SSCANF(value, "%d:%d", out_lb, out_ub) != 2) {
500         goto par_range_error;
501       }
502       *out_range = -1;
503       value = strchr(value, ',');
504       if (value != NULL) {
505         value++;
506       }
507       continue;
508     }
509   par_range_error:
510     KMP_WARNING(ParRangeSyntax, name);
511     __kmp_par_range = 0;
512     break;
513   }
514 } // __kmp_stg_parse_par_range
515 #endif
516 
__kmp_initial_threads_capacity(int req_nproc)517 int __kmp_initial_threads_capacity(int req_nproc) {
518   int nth = 32;
519 
520   /* MIN( MAX( 32, 4 * $OMP_NUM_THREADS, 4 * omp_get_num_procs() ),
521    * __kmp_max_nth) */
522   if (nth < (4 * req_nproc))
523     nth = (4 * req_nproc);
524   if (nth < (4 * __kmp_xproc))
525     nth = (4 * __kmp_xproc);
526 
527   // If hidden helper task is enabled, we initialize the thread capacity with
528   // extra __kmp_hidden_helper_threads_num.
529   if (__kmp_enable_hidden_helper) {
530     nth += __kmp_hidden_helper_threads_num;
531   }
532 
533   if (nth > __kmp_max_nth)
534     nth = __kmp_max_nth;
535 
536   return nth;
537 }
538 
__kmp_default_tp_capacity(int req_nproc,int max_nth,int all_threads_specified)539 int __kmp_default_tp_capacity(int req_nproc, int max_nth,
540                               int all_threads_specified) {
541   int nth = 128;
542 
543   if (all_threads_specified)
544     return max_nth;
545   /* MIN( MAX (128, 4 * $OMP_NUM_THREADS, 4 * omp_get_num_procs() ),
546    * __kmp_max_nth ) */
547   if (nth < (4 * req_nproc))
548     nth = (4 * req_nproc);
549   if (nth < (4 * __kmp_xproc))
550     nth = (4 * __kmp_xproc);
551 
552   if (nth > __kmp_max_nth)
553     nth = __kmp_max_nth;
554 
555   return nth;
556 }
557 
558 // -----------------------------------------------------------------------------
559 // Helper print functions.
560 
__kmp_stg_print_bool(kmp_str_buf_t * buffer,char const * name,int value)561 static void __kmp_stg_print_bool(kmp_str_buf_t *buffer, char const *name,
562                                  int value) {
563   if (__kmp_env_format) {
564     KMP_STR_BUF_PRINT_BOOL;
565   } else {
566     __kmp_str_buf_print(buffer, "   %s=%s\n", name, value ? "true" : "false");
567   }
568 } // __kmp_stg_print_bool
569 
__kmp_stg_print_int(kmp_str_buf_t * buffer,char const * name,int value)570 static void __kmp_stg_print_int(kmp_str_buf_t *buffer, char const *name,
571                                 int value) {
572   if (__kmp_env_format) {
573     KMP_STR_BUF_PRINT_INT;
574   } else {
575     __kmp_str_buf_print(buffer, "   %s=%d\n", name, value);
576   }
577 } // __kmp_stg_print_int
578 
__kmp_stg_print_uint64(kmp_str_buf_t * buffer,char const * name,kmp_uint64 value)579 static void __kmp_stg_print_uint64(kmp_str_buf_t *buffer, char const *name,
580                                    kmp_uint64 value) {
581   if (__kmp_env_format) {
582     KMP_STR_BUF_PRINT_UINT64;
583   } else {
584     __kmp_str_buf_print(buffer, "   %s=%" KMP_UINT64_SPEC "\n", name, value);
585   }
586 } // __kmp_stg_print_uint64
587 
__kmp_stg_print_str(kmp_str_buf_t * buffer,char const * name,char const * value)588 static void __kmp_stg_print_str(kmp_str_buf_t *buffer, char const *name,
589                                 char const *value) {
590   if (__kmp_env_format) {
591     KMP_STR_BUF_PRINT_STR;
592   } else {
593     __kmp_str_buf_print(buffer, "   %s=%s\n", name, value);
594   }
595 } // __kmp_stg_print_str
596 
__kmp_stg_print_size(kmp_str_buf_t * buffer,char const * name,size_t value)597 static void __kmp_stg_print_size(kmp_str_buf_t *buffer, char const *name,
598                                  size_t value) {
599   if (__kmp_env_format) {
600     KMP_STR_BUF_PRINT_NAME_EX(name);
601     __kmp_str_buf_print_size(buffer, value);
602     __kmp_str_buf_print(buffer, "'\n");
603   } else {
604     __kmp_str_buf_print(buffer, "   %s=", name);
605     __kmp_str_buf_print_size(buffer, value);
606     __kmp_str_buf_print(buffer, "\n");
607     return;
608   }
609 } // __kmp_stg_print_size
610 
611 // =============================================================================
612 // Parse and print functions.
613 
614 // -----------------------------------------------------------------------------
615 // KMP_DEVICE_THREAD_LIMIT, KMP_ALL_THREADS
616 
__kmp_stg_parse_device_thread_limit(char const * name,char const * value,void * data)617 static void __kmp_stg_parse_device_thread_limit(char const *name,
618                                                 char const *value, void *data) {
619   kmp_setting_t **rivals = (kmp_setting_t **)data;
620   int rc;
621   if (strcmp(name, "KMP_ALL_THREADS") == 0) {
622     KMP_INFORM(EnvVarDeprecated, name, "KMP_DEVICE_THREAD_LIMIT");
623   }
624   rc = __kmp_stg_check_rivals(name, value, rivals);
625   if (rc) {
626     return;
627   }
628   if (!__kmp_strcasecmp_with_sentinel("all", value, 0)) {
629     __kmp_max_nth = __kmp_xproc;
630     __kmp_allThreadsSpecified = 1;
631   } else {
632     __kmp_stg_parse_int(name, value, 1, __kmp_sys_max_nth, &__kmp_max_nth);
633     __kmp_allThreadsSpecified = 0;
634   }
635   K_DIAG(1, ("__kmp_max_nth == %d\n", __kmp_max_nth));
636 
637 } // __kmp_stg_parse_device_thread_limit
638 
__kmp_stg_print_device_thread_limit(kmp_str_buf_t * buffer,char const * name,void * data)639 static void __kmp_stg_print_device_thread_limit(kmp_str_buf_t *buffer,
640                                                 char const *name, void *data) {
641   __kmp_stg_print_int(buffer, name, __kmp_max_nth);
642 } // __kmp_stg_print_device_thread_limit
643 
644 // -----------------------------------------------------------------------------
645 // OMP_THREAD_LIMIT
__kmp_stg_parse_thread_limit(char const * name,char const * value,void * data)646 static void __kmp_stg_parse_thread_limit(char const *name, char const *value,
647                                          void *data) {
648   __kmp_stg_parse_int(name, value, 1, __kmp_sys_max_nth, &__kmp_cg_max_nth);
649   K_DIAG(1, ("__kmp_cg_max_nth == %d\n", __kmp_cg_max_nth));
650 
651 } // __kmp_stg_parse_thread_limit
652 
__kmp_stg_print_thread_limit(kmp_str_buf_t * buffer,char const * name,void * data)653 static void __kmp_stg_print_thread_limit(kmp_str_buf_t *buffer,
654                                          char const *name, void *data) {
655   __kmp_stg_print_int(buffer, name, __kmp_cg_max_nth);
656 } // __kmp_stg_print_thread_limit
657 
658 // -----------------------------------------------------------------------------
659 // OMP_NUM_TEAMS
__kmp_stg_parse_nteams(char const * name,char const * value,void * data)660 static void __kmp_stg_parse_nteams(char const *name, char const *value,
661                                    void *data) {
662   __kmp_stg_parse_int(name, value, 1, __kmp_sys_max_nth, &__kmp_nteams);
663   K_DIAG(1, ("__kmp_nteams == %d\n", __kmp_nteams));
664 } // __kmp_stg_parse_nteams
665 
__kmp_stg_print_nteams(kmp_str_buf_t * buffer,char const * name,void * data)666 static void __kmp_stg_print_nteams(kmp_str_buf_t *buffer, char const *name,
667                                    void *data) {
668   __kmp_stg_print_int(buffer, name, __kmp_nteams);
669 } // __kmp_stg_print_nteams
670 
671 // -----------------------------------------------------------------------------
672 // OMP_TEAMS_THREAD_LIMIT
__kmp_stg_parse_teams_th_limit(char const * name,char const * value,void * data)673 static void __kmp_stg_parse_teams_th_limit(char const *name, char const *value,
674                                            void *data) {
675   __kmp_stg_parse_int(name, value, 1, __kmp_sys_max_nth,
676                       &__kmp_teams_thread_limit);
677   K_DIAG(1, ("__kmp_teams_thread_limit == %d\n", __kmp_teams_thread_limit));
678 } // __kmp_stg_parse_teams_th_limit
679 
__kmp_stg_print_teams_th_limit(kmp_str_buf_t * buffer,char const * name,void * data)680 static void __kmp_stg_print_teams_th_limit(kmp_str_buf_t *buffer,
681                                            char const *name, void *data) {
682   __kmp_stg_print_int(buffer, name, __kmp_teams_thread_limit);
683 } // __kmp_stg_print_teams_th_limit
684 
685 // -----------------------------------------------------------------------------
686 // KMP_TEAMS_THREAD_LIMIT
__kmp_stg_parse_teams_thread_limit(char const * name,char const * value,void * data)687 static void __kmp_stg_parse_teams_thread_limit(char const *name,
688                                                char const *value, void *data) {
689   __kmp_stg_parse_int(name, value, 1, __kmp_sys_max_nth, &__kmp_teams_max_nth);
690 } // __kmp_stg_teams_thread_limit
691 
__kmp_stg_print_teams_thread_limit(kmp_str_buf_t * buffer,char const * name,void * data)692 static void __kmp_stg_print_teams_thread_limit(kmp_str_buf_t *buffer,
693                                                char const *name, void *data) {
694   __kmp_stg_print_int(buffer, name, __kmp_teams_max_nth);
695 } // __kmp_stg_print_teams_thread_limit
696 
697 // -----------------------------------------------------------------------------
698 // KMP_USE_YIELD
__kmp_stg_parse_use_yield(char const * name,char const * value,void * data)699 static void __kmp_stg_parse_use_yield(char const *name, char const *value,
700                                       void *data) {
701   __kmp_stg_parse_int(name, value, 0, 2, &__kmp_use_yield);
702   __kmp_use_yield_exp_set = 1;
703 } // __kmp_stg_parse_use_yield
704 
__kmp_stg_print_use_yield(kmp_str_buf_t * buffer,char const * name,void * data)705 static void __kmp_stg_print_use_yield(kmp_str_buf_t *buffer, char const *name,
706                                       void *data) {
707   __kmp_stg_print_int(buffer, name, __kmp_use_yield);
708 } // __kmp_stg_print_use_yield
709 
710 // -----------------------------------------------------------------------------
711 // KMP_BLOCKTIME
712 
__kmp_stg_parse_blocktime(char const * name,char const * value,void * data)713 static void __kmp_stg_parse_blocktime(char const *name, char const *value,
714                                       void *data) {
715   __kmp_dflt_blocktime = __kmp_convert_to_milliseconds(value);
716   if (__kmp_dflt_blocktime < 0) {
717     __kmp_dflt_blocktime = KMP_DEFAULT_BLOCKTIME;
718     __kmp_msg(kmp_ms_warning, KMP_MSG(InvalidValue, name, value),
719               __kmp_msg_null);
720     KMP_INFORM(Using_int_Value, name, __kmp_dflt_blocktime);
721     __kmp_env_blocktime = FALSE; // Revert to default as if var not set.
722   } else {
723     if (__kmp_dflt_blocktime < KMP_MIN_BLOCKTIME) {
724       __kmp_dflt_blocktime = KMP_MIN_BLOCKTIME;
725       __kmp_msg(kmp_ms_warning, KMP_MSG(SmallValue, name, value),
726                 __kmp_msg_null);
727       KMP_INFORM(MinValueUsing, name, __kmp_dflt_blocktime);
728     } else if (__kmp_dflt_blocktime > KMP_MAX_BLOCKTIME) {
729       __kmp_dflt_blocktime = KMP_MAX_BLOCKTIME;
730       __kmp_msg(kmp_ms_warning, KMP_MSG(LargeValue, name, value),
731                 __kmp_msg_null);
732       KMP_INFORM(MaxValueUsing, name, __kmp_dflt_blocktime);
733     }
734     __kmp_env_blocktime = TRUE; // KMP_BLOCKTIME was specified.
735   }
736 #if KMP_USE_MONITOR
737   // calculate number of monitor thread wakeup intervals corresponding to
738   // blocktime.
739   __kmp_monitor_wakeups =
740       KMP_WAKEUPS_FROM_BLOCKTIME(__kmp_dflt_blocktime, __kmp_monitor_wakeups);
741   __kmp_bt_intervals =
742       KMP_INTERVALS_FROM_BLOCKTIME(__kmp_dflt_blocktime, __kmp_monitor_wakeups);
743 #endif
744   K_DIAG(1, ("__kmp_env_blocktime == %d\n", __kmp_env_blocktime));
745   if (__kmp_env_blocktime) {
746     K_DIAG(1, ("__kmp_dflt_blocktime == %d\n", __kmp_dflt_blocktime));
747   }
748 } // __kmp_stg_parse_blocktime
749 
__kmp_stg_print_blocktime(kmp_str_buf_t * buffer,char const * name,void * data)750 static void __kmp_stg_print_blocktime(kmp_str_buf_t *buffer, char const *name,
751                                       void *data) {
752   __kmp_stg_print_int(buffer, name, __kmp_dflt_blocktime);
753 } // __kmp_stg_print_blocktime
754 
755 // -----------------------------------------------------------------------------
756 // KMP_DUPLICATE_LIB_OK
757 
__kmp_stg_parse_duplicate_lib_ok(char const * name,char const * value,void * data)758 static void __kmp_stg_parse_duplicate_lib_ok(char const *name,
759                                              char const *value, void *data) {
760   /* actually this variable is not supported, put here for compatibility with
761      earlier builds and for static/dynamic combination */
762   __kmp_stg_parse_bool(name, value, &__kmp_duplicate_library_ok);
763 } // __kmp_stg_parse_duplicate_lib_ok
764 
__kmp_stg_print_duplicate_lib_ok(kmp_str_buf_t * buffer,char const * name,void * data)765 static void __kmp_stg_print_duplicate_lib_ok(kmp_str_buf_t *buffer,
766                                              char const *name, void *data) {
767   __kmp_stg_print_bool(buffer, name, __kmp_duplicate_library_ok);
768 } // __kmp_stg_print_duplicate_lib_ok
769 
770 // -----------------------------------------------------------------------------
771 // KMP_INHERIT_FP_CONTROL
772 
773 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
774 
__kmp_stg_parse_inherit_fp_control(char const * name,char const * value,void * data)775 static void __kmp_stg_parse_inherit_fp_control(char const *name,
776                                                char const *value, void *data) {
777   __kmp_stg_parse_bool(name, value, &__kmp_inherit_fp_control);
778 } // __kmp_stg_parse_inherit_fp_control
779 
__kmp_stg_print_inherit_fp_control(kmp_str_buf_t * buffer,char const * name,void * data)780 static void __kmp_stg_print_inherit_fp_control(kmp_str_buf_t *buffer,
781                                                char const *name, void *data) {
782 #if KMP_DEBUG
783   __kmp_stg_print_bool(buffer, name, __kmp_inherit_fp_control);
784 #endif /* KMP_DEBUG */
785 } // __kmp_stg_print_inherit_fp_control
786 
787 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
788 
789 // Used for OMP_WAIT_POLICY
790 static char const *blocktime_str = NULL;
791 
792 // -----------------------------------------------------------------------------
793 // KMP_LIBRARY, OMP_WAIT_POLICY
794 
__kmp_stg_parse_wait_policy(char const * name,char const * value,void * data)795 static void __kmp_stg_parse_wait_policy(char const *name, char const *value,
796                                         void *data) {
797 
798   kmp_stg_wp_data_t *wait = (kmp_stg_wp_data_t *)data;
799   int rc;
800 
801   rc = __kmp_stg_check_rivals(name, value, wait->rivals);
802   if (rc) {
803     return;
804   }
805 
806   if (wait->omp) {
807     if (__kmp_str_match("ACTIVE", 1, value)) {
808       __kmp_library = library_turnaround;
809       if (blocktime_str == NULL) {
810         // KMP_BLOCKTIME not specified, so set default to "infinite".
811         __kmp_dflt_blocktime = KMP_MAX_BLOCKTIME;
812       }
813     } else if (__kmp_str_match("PASSIVE", 1, value)) {
814       __kmp_library = library_throughput;
815       if (blocktime_str == NULL) {
816         // KMP_BLOCKTIME not specified, so set default to 0.
817         __kmp_dflt_blocktime = 0;
818       }
819     } else {
820       KMP_WARNING(StgInvalidValue, name, value);
821     }
822   } else {
823     if (__kmp_str_match("serial", 1, value)) { /* S */
824       __kmp_library = library_serial;
825     } else if (__kmp_str_match("throughput", 2, value)) { /* TH */
826       __kmp_library = library_throughput;
827       if (blocktime_str == NULL) {
828         // KMP_BLOCKTIME not specified, so set default to 0.
829         __kmp_dflt_blocktime = 0;
830       }
831     } else if (__kmp_str_match("turnaround", 2, value)) { /* TU */
832       __kmp_library = library_turnaround;
833     } else if (__kmp_str_match("dedicated", 1, value)) { /* D */
834       __kmp_library = library_turnaround;
835     } else if (__kmp_str_match("multiuser", 1, value)) { /* M */
836       __kmp_library = library_throughput;
837       if (blocktime_str == NULL) {
838         // KMP_BLOCKTIME not specified, so set default to 0.
839         __kmp_dflt_blocktime = 0;
840       }
841     } else {
842       KMP_WARNING(StgInvalidValue, name, value);
843     }
844   }
845 } // __kmp_stg_parse_wait_policy
846 
__kmp_stg_print_wait_policy(kmp_str_buf_t * buffer,char const * name,void * data)847 static void __kmp_stg_print_wait_policy(kmp_str_buf_t *buffer, char const *name,
848                                         void *data) {
849 
850   kmp_stg_wp_data_t *wait = (kmp_stg_wp_data_t *)data;
851   char const *value = NULL;
852 
853   if (wait->omp) {
854     switch (__kmp_library) {
855     case library_turnaround: {
856       value = "ACTIVE";
857     } break;
858     case library_throughput: {
859       value = "PASSIVE";
860     } break;
861     }
862   } else {
863     switch (__kmp_library) {
864     case library_serial: {
865       value = "serial";
866     } break;
867     case library_turnaround: {
868       value = "turnaround";
869     } break;
870     case library_throughput: {
871       value = "throughput";
872     } break;
873     }
874   }
875   if (value != NULL) {
876     __kmp_stg_print_str(buffer, name, value);
877   }
878 
879 } // __kmp_stg_print_wait_policy
880 
881 #if KMP_USE_MONITOR
882 // -----------------------------------------------------------------------------
883 // KMP_MONITOR_STACKSIZE
884 
__kmp_stg_parse_monitor_stacksize(char const * name,char const * value,void * data)885 static void __kmp_stg_parse_monitor_stacksize(char const *name,
886                                               char const *value, void *data) {
887   __kmp_stg_parse_size(name, value, __kmp_sys_min_stksize, KMP_MAX_STKSIZE,
888                        NULL, &__kmp_monitor_stksize, 1);
889 } // __kmp_stg_parse_monitor_stacksize
890 
__kmp_stg_print_monitor_stacksize(kmp_str_buf_t * buffer,char const * name,void * data)891 static void __kmp_stg_print_monitor_stacksize(kmp_str_buf_t *buffer,
892                                               char const *name, void *data) {
893   if (__kmp_env_format) {
894     if (__kmp_monitor_stksize > 0)
895       KMP_STR_BUF_PRINT_NAME_EX(name);
896     else
897       KMP_STR_BUF_PRINT_NAME;
898   } else {
899     __kmp_str_buf_print(buffer, "   %s", name);
900   }
901   if (__kmp_monitor_stksize > 0) {
902     __kmp_str_buf_print_size(buffer, __kmp_monitor_stksize);
903   } else {
904     __kmp_str_buf_print(buffer, ": %s\n", KMP_I18N_STR(NotDefined));
905   }
906   if (__kmp_env_format && __kmp_monitor_stksize) {
907     __kmp_str_buf_print(buffer, "'\n");
908   }
909 } // __kmp_stg_print_monitor_stacksize
910 #endif // KMP_USE_MONITOR
911 
912 // -----------------------------------------------------------------------------
913 // KMP_SETTINGS
914 
__kmp_stg_parse_settings(char const * name,char const * value,void * data)915 static void __kmp_stg_parse_settings(char const *name, char const *value,
916                                      void *data) {
917   __kmp_stg_parse_bool(name, value, &__kmp_settings);
918 } // __kmp_stg_parse_settings
919 
__kmp_stg_print_settings(kmp_str_buf_t * buffer,char const * name,void * data)920 static void __kmp_stg_print_settings(kmp_str_buf_t *buffer, char const *name,
921                                      void *data) {
922   __kmp_stg_print_bool(buffer, name, __kmp_settings);
923 } // __kmp_stg_print_settings
924 
925 // -----------------------------------------------------------------------------
926 // KMP_STACKPAD
927 
__kmp_stg_parse_stackpad(char const * name,char const * value,void * data)928 static void __kmp_stg_parse_stackpad(char const *name, char const *value,
929                                      void *data) {
930   __kmp_stg_parse_int(name, // Env var name
931                       value, // Env var value
932                       KMP_MIN_STKPADDING, // Min value
933                       KMP_MAX_STKPADDING, // Max value
934                       &__kmp_stkpadding // Var to initialize
935   );
936 } // __kmp_stg_parse_stackpad
937 
__kmp_stg_print_stackpad(kmp_str_buf_t * buffer,char const * name,void * data)938 static void __kmp_stg_print_stackpad(kmp_str_buf_t *buffer, char const *name,
939                                      void *data) {
940   __kmp_stg_print_int(buffer, name, __kmp_stkpadding);
941 } // __kmp_stg_print_stackpad
942 
943 // -----------------------------------------------------------------------------
944 // KMP_STACKOFFSET
945 
__kmp_stg_parse_stackoffset(char const * name,char const * value,void * data)946 static void __kmp_stg_parse_stackoffset(char const *name, char const *value,
947                                         void *data) {
948   __kmp_stg_parse_size(name, // Env var name
949                        value, // Env var value
950                        KMP_MIN_STKOFFSET, // Min value
951                        KMP_MAX_STKOFFSET, // Max value
952                        NULL, //
953                        &__kmp_stkoffset, // Var to initialize
954                        1);
955 } // __kmp_stg_parse_stackoffset
956 
__kmp_stg_print_stackoffset(kmp_str_buf_t * buffer,char const * name,void * data)957 static void __kmp_stg_print_stackoffset(kmp_str_buf_t *buffer, char const *name,
958                                         void *data) {
959   __kmp_stg_print_size(buffer, name, __kmp_stkoffset);
960 } // __kmp_stg_print_stackoffset
961 
962 // -----------------------------------------------------------------------------
963 // KMP_STACKSIZE, OMP_STACKSIZE, GOMP_STACKSIZE
964 
__kmp_stg_parse_stacksize(char const * name,char const * value,void * data)965 static void __kmp_stg_parse_stacksize(char const *name, char const *value,
966                                       void *data) {
967 
968   kmp_stg_ss_data_t *stacksize = (kmp_stg_ss_data_t *)data;
969   int rc;
970 
971   rc = __kmp_stg_check_rivals(name, value, stacksize->rivals);
972   if (rc) {
973     return;
974   }
975   __kmp_stg_parse_size(name, // Env var name
976                        value, // Env var value
977                        __kmp_sys_min_stksize, // Min value
978                        KMP_MAX_STKSIZE, // Max value
979                        &__kmp_env_stksize, //
980                        &__kmp_stksize, // Var to initialize
981                        stacksize->factor);
982 
983 } // __kmp_stg_parse_stacksize
984 
985 // This function is called for printing both KMP_STACKSIZE (factor is 1) and
986 // OMP_STACKSIZE (factor is 1024). Currently it is not possible to print
987 // OMP_STACKSIZE value in bytes. We can consider adding this possibility by a
988 // customer request in future.
__kmp_stg_print_stacksize(kmp_str_buf_t * buffer,char const * name,void * data)989 static void __kmp_stg_print_stacksize(kmp_str_buf_t *buffer, char const *name,
990                                       void *data) {
991   kmp_stg_ss_data_t *stacksize = (kmp_stg_ss_data_t *)data;
992   if (__kmp_env_format) {
993     KMP_STR_BUF_PRINT_NAME_EX(name);
994     __kmp_str_buf_print_size(buffer, (__kmp_stksize % 1024)
995                                          ? __kmp_stksize / stacksize->factor
996                                          : __kmp_stksize);
997     __kmp_str_buf_print(buffer, "'\n");
998   } else {
999     __kmp_str_buf_print(buffer, "   %s=", name);
1000     __kmp_str_buf_print_size(buffer, (__kmp_stksize % 1024)
1001                                          ? __kmp_stksize / stacksize->factor
1002                                          : __kmp_stksize);
1003     __kmp_str_buf_print(buffer, "\n");
1004   }
1005 } // __kmp_stg_print_stacksize
1006 
1007 // -----------------------------------------------------------------------------
1008 // KMP_VERSION
1009 
__kmp_stg_parse_version(char const * name,char const * value,void * data)1010 static void __kmp_stg_parse_version(char const *name, char const *value,
1011                                     void *data) {
1012   __kmp_stg_parse_bool(name, value, &__kmp_version);
1013 } // __kmp_stg_parse_version
1014 
__kmp_stg_print_version(kmp_str_buf_t * buffer,char const * name,void * data)1015 static void __kmp_stg_print_version(kmp_str_buf_t *buffer, char const *name,
1016                                     void *data) {
1017   __kmp_stg_print_bool(buffer, name, __kmp_version);
1018 } // __kmp_stg_print_version
1019 
1020 // -----------------------------------------------------------------------------
1021 // KMP_WARNINGS
1022 
__kmp_stg_parse_warnings(char const * name,char const * value,void * data)1023 static void __kmp_stg_parse_warnings(char const *name, char const *value,
1024                                      void *data) {
1025   __kmp_stg_parse_bool(name, value, &__kmp_generate_warnings);
1026   if (__kmp_generate_warnings != kmp_warnings_off) {
1027     // AC: only 0/1 values documented, so reset to explicit to distinguish from
1028     // default setting
1029     __kmp_generate_warnings = kmp_warnings_explicit;
1030   }
1031 } // __kmp_stg_parse_warnings
1032 
__kmp_stg_print_warnings(kmp_str_buf_t * buffer,char const * name,void * data)1033 static void __kmp_stg_print_warnings(kmp_str_buf_t *buffer, char const *name,
1034                                      void *data) {
1035   // AC: TODO: change to print_int? (needs documentation change)
1036   __kmp_stg_print_bool(buffer, name, __kmp_generate_warnings);
1037 } // __kmp_stg_print_warnings
1038 
1039 // -----------------------------------------------------------------------------
1040 // KMP_NESTING_MODE
1041 
__kmp_stg_parse_nesting_mode(char const * name,char const * value,void * data)1042 static void __kmp_stg_parse_nesting_mode(char const *name, char const *value,
1043                                          void *data) {
1044   __kmp_stg_parse_int(name, value, 0, INT_MAX, &__kmp_nesting_mode);
1045 #if KMP_AFFINITY_SUPPORTED && KMP_USE_HWLOC
1046   if (__kmp_nesting_mode > 0)
1047     __kmp_affinity_top_method = affinity_top_method_hwloc;
1048 #endif
1049 } // __kmp_stg_parse_nesting_mode
1050 
__kmp_stg_print_nesting_mode(kmp_str_buf_t * buffer,char const * name,void * data)1051 static void __kmp_stg_print_nesting_mode(kmp_str_buf_t *buffer,
1052                                          char const *name, void *data) {
1053   if (__kmp_env_format) {
1054     KMP_STR_BUF_PRINT_NAME;
1055   } else {
1056     __kmp_str_buf_print(buffer, "   %s", name);
1057   }
1058   __kmp_str_buf_print(buffer, "=%d\n", __kmp_nesting_mode);
1059 } // __kmp_stg_print_nesting_mode
1060 
1061 // -----------------------------------------------------------------------------
1062 // OMP_NESTED, OMP_NUM_THREADS
1063 
__kmp_stg_parse_nested(char const * name,char const * value,void * data)1064 static void __kmp_stg_parse_nested(char const *name, char const *value,
1065                                    void *data) {
1066   int nested;
1067   KMP_INFORM(EnvVarDeprecated, name, "OMP_MAX_ACTIVE_LEVELS");
1068   __kmp_stg_parse_bool(name, value, &nested);
1069   if (nested) {
1070     if (!__kmp_dflt_max_active_levels_set)
1071       __kmp_dflt_max_active_levels = KMP_MAX_ACTIVE_LEVELS_LIMIT;
1072   } else { // nesting explicitly turned off
1073     __kmp_dflt_max_active_levels = 1;
1074     __kmp_dflt_max_active_levels_set = true;
1075   }
1076 } // __kmp_stg_parse_nested
1077 
__kmp_stg_print_nested(kmp_str_buf_t * buffer,char const * name,void * data)1078 static void __kmp_stg_print_nested(kmp_str_buf_t *buffer, char const *name,
1079                                    void *data) {
1080   if (__kmp_env_format) {
1081     KMP_STR_BUF_PRINT_NAME;
1082   } else {
1083     __kmp_str_buf_print(buffer, "   %s", name);
1084   }
1085   __kmp_str_buf_print(buffer, ": deprecated; max-active-levels-var=%d\n",
1086                       __kmp_dflt_max_active_levels);
1087 } // __kmp_stg_print_nested
1088 
__kmp_parse_nested_num_threads(const char * var,const char * env,kmp_nested_nthreads_t * nth_array)1089 static void __kmp_parse_nested_num_threads(const char *var, const char *env,
1090                                            kmp_nested_nthreads_t *nth_array) {
1091   const char *next = env;
1092   const char *scan = next;
1093 
1094   int total = 0; // Count elements that were set. It'll be used as an array size
1095   int prev_comma = FALSE; // For correct processing sequential commas
1096 
1097   // Count the number of values in the env. var string
1098   for (;;) {
1099     SKIP_WS(next);
1100 
1101     if (*next == '\0') {
1102       break;
1103     }
1104     // Next character is not an integer or not a comma => end of list
1105     if (((*next < '0') || (*next > '9')) && (*next != ',')) {
1106       KMP_WARNING(NthSyntaxError, var, env);
1107       return;
1108     }
1109     // The next character is ','
1110     if (*next == ',') {
1111       // ',' is the first character
1112       if (total == 0 || prev_comma) {
1113         total++;
1114       }
1115       prev_comma = TRUE;
1116       next++; // skip ','
1117       SKIP_WS(next);
1118     }
1119     // Next character is a digit
1120     if (*next >= '0' && *next <= '9') {
1121       prev_comma = FALSE;
1122       SKIP_DIGITS(next);
1123       total++;
1124       const char *tmp = next;
1125       SKIP_WS(tmp);
1126       if ((*next == ' ' || *next == '\t') && (*tmp >= '0' && *tmp <= '9')) {
1127         KMP_WARNING(NthSpacesNotAllowed, var, env);
1128         return;
1129       }
1130     }
1131   }
1132   if (!__kmp_dflt_max_active_levels_set && total > 1)
1133     __kmp_dflt_max_active_levels = KMP_MAX_ACTIVE_LEVELS_LIMIT;
1134   KMP_DEBUG_ASSERT(total > 0);
1135   if (total <= 0) {
1136     KMP_WARNING(NthSyntaxError, var, env);
1137     return;
1138   }
1139 
1140   // Check if the nested nthreads array exists
1141   if (!nth_array->nth) {
1142     // Allocate an array of double size
1143     nth_array->nth = (int *)KMP_INTERNAL_MALLOC(sizeof(int) * total * 2);
1144     if (nth_array->nth == NULL) {
1145       KMP_FATAL(MemoryAllocFailed);
1146     }
1147     nth_array->size = total * 2;
1148   } else {
1149     if (nth_array->size < total) {
1150       // Increase the array size
1151       do {
1152         nth_array->size *= 2;
1153       } while (nth_array->size < total);
1154 
1155       nth_array->nth = (int *)KMP_INTERNAL_REALLOC(
1156           nth_array->nth, sizeof(int) * nth_array->size);
1157       if (nth_array->nth == NULL) {
1158         KMP_FATAL(MemoryAllocFailed);
1159       }
1160     }
1161   }
1162   nth_array->used = total;
1163   int i = 0;
1164 
1165   prev_comma = FALSE;
1166   total = 0;
1167   // Save values in the array
1168   for (;;) {
1169     SKIP_WS(scan);
1170     if (*scan == '\0') {
1171       break;
1172     }
1173     // The next character is ','
1174     if (*scan == ',') {
1175       // ',' in the beginning of the list
1176       if (total == 0) {
1177         // The value is supposed to be equal to __kmp_avail_proc but it is
1178         // unknown at the moment.
1179         // So let's put a placeholder (#threads = 0) to correct it later.
1180         nth_array->nth[i++] = 0;
1181         total++;
1182       } else if (prev_comma) {
1183         // Num threads is inherited from the previous level
1184         nth_array->nth[i] = nth_array->nth[i - 1];
1185         i++;
1186         total++;
1187       }
1188       prev_comma = TRUE;
1189       scan++; // skip ','
1190       SKIP_WS(scan);
1191     }
1192     // Next character is a digit
1193     if (*scan >= '0' && *scan <= '9') {
1194       int num;
1195       const char *buf = scan;
1196       char const *msg = NULL;
1197       prev_comma = FALSE;
1198       SKIP_DIGITS(scan);
1199       total++;
1200 
1201       num = __kmp_str_to_int(buf, *scan);
1202       if (num < KMP_MIN_NTH) {
1203         msg = KMP_I18N_STR(ValueTooSmall);
1204         num = KMP_MIN_NTH;
1205       } else if (num > __kmp_sys_max_nth) {
1206         msg = KMP_I18N_STR(ValueTooLarge);
1207         num = __kmp_sys_max_nth;
1208       }
1209       if (msg != NULL) {
1210         // Message is not empty. Print warning.
1211         KMP_WARNING(ParseSizeIntWarn, var, env, msg);
1212         KMP_INFORM(Using_int_Value, var, num);
1213       }
1214       nth_array->nth[i++] = num;
1215     }
1216   }
1217 }
1218 
__kmp_stg_parse_num_threads(char const * name,char const * value,void * data)1219 static void __kmp_stg_parse_num_threads(char const *name, char const *value,
1220                                         void *data) {
1221   // TODO: Remove this option. OMP_NUM_THREADS is a list of positive integers!
1222   if (!__kmp_strcasecmp_with_sentinel("all", value, 0)) {
1223     // The array of 1 element
1224     __kmp_nested_nth.nth = (int *)KMP_INTERNAL_MALLOC(sizeof(int));
1225     __kmp_nested_nth.size = __kmp_nested_nth.used = 1;
1226     __kmp_nested_nth.nth[0] = __kmp_dflt_team_nth = __kmp_dflt_team_nth_ub =
1227         __kmp_xproc;
1228   } else {
1229     __kmp_parse_nested_num_threads(name, value, &__kmp_nested_nth);
1230     if (__kmp_nested_nth.nth) {
1231       __kmp_dflt_team_nth = __kmp_nested_nth.nth[0];
1232       if (__kmp_dflt_team_nth_ub < __kmp_dflt_team_nth) {
1233         __kmp_dflt_team_nth_ub = __kmp_dflt_team_nth;
1234       }
1235     }
1236   }
1237   K_DIAG(1, ("__kmp_dflt_team_nth == %d\n", __kmp_dflt_team_nth));
1238 } // __kmp_stg_parse_num_threads
1239 
__kmp_stg_parse_num_hidden_helper_threads(char const * name,char const * value,void * data)1240 static void __kmp_stg_parse_num_hidden_helper_threads(char const *name,
1241                                                       char const *value,
1242                                                       void *data) {
1243   __kmp_stg_parse_int(name, value, 0, 16, &__kmp_hidden_helper_threads_num);
1244   // If the number of hidden helper threads is zero, we disable hidden helper
1245   // task
1246   if (__kmp_hidden_helper_threads_num == 0) {
1247     __kmp_enable_hidden_helper = FALSE;
1248   }
1249 } // __kmp_stg_parse_num_hidden_helper_threads
1250 
__kmp_stg_print_num_hidden_helper_threads(kmp_str_buf_t * buffer,char const * name,void * data)1251 static void __kmp_stg_print_num_hidden_helper_threads(kmp_str_buf_t *buffer,
1252                                                       char const *name,
1253                                                       void *data) {
1254   __kmp_stg_print_int(buffer, name, __kmp_hidden_helper_threads_num);
1255 } // __kmp_stg_print_num_hidden_helper_threads
1256 
__kmp_stg_parse_use_hidden_helper(char const * name,char const * value,void * data)1257 static void __kmp_stg_parse_use_hidden_helper(char const *name,
1258                                               char const *value, void *data) {
1259   __kmp_stg_parse_bool(name, value, &__kmp_enable_hidden_helper);
1260 #if !KMP_OS_LINUX
1261   __kmp_enable_hidden_helper = FALSE;
1262   K_DIAG(1,
1263          ("__kmp_stg_parse_use_hidden_helper: Disable hidden helper task on "
1264           "non-Linux platform although it is enabled by user explicitly.\n"));
1265 #endif
1266 } // __kmp_stg_parse_use_hidden_helper
1267 
__kmp_stg_print_use_hidden_helper(kmp_str_buf_t * buffer,char const * name,void * data)1268 static void __kmp_stg_print_use_hidden_helper(kmp_str_buf_t *buffer,
1269                                               char const *name, void *data) {
1270   __kmp_stg_print_bool(buffer, name, __kmp_enable_hidden_helper);
1271 } // __kmp_stg_print_use_hidden_helper
1272 
__kmp_stg_print_num_threads(kmp_str_buf_t * buffer,char const * name,void * data)1273 static void __kmp_stg_print_num_threads(kmp_str_buf_t *buffer, char const *name,
1274                                         void *data) {
1275   if (__kmp_env_format) {
1276     KMP_STR_BUF_PRINT_NAME;
1277   } else {
1278     __kmp_str_buf_print(buffer, "   %s", name);
1279   }
1280   if (__kmp_nested_nth.used) {
1281     kmp_str_buf_t buf;
1282     __kmp_str_buf_init(&buf);
1283     for (int i = 0; i < __kmp_nested_nth.used; i++) {
1284       __kmp_str_buf_print(&buf, "%d", __kmp_nested_nth.nth[i]);
1285       if (i < __kmp_nested_nth.used - 1) {
1286         __kmp_str_buf_print(&buf, ",");
1287       }
1288     }
1289     __kmp_str_buf_print(buffer, "='%s'\n", buf.str);
1290     __kmp_str_buf_free(&buf);
1291   } else {
1292     __kmp_str_buf_print(buffer, ": %s\n", KMP_I18N_STR(NotDefined));
1293   }
1294 } // __kmp_stg_print_num_threads
1295 
1296 // -----------------------------------------------------------------------------
1297 // OpenMP 3.0: KMP_TASKING, OMP_MAX_ACTIVE_LEVELS,
1298 
__kmp_stg_parse_tasking(char const * name,char const * value,void * data)1299 static void __kmp_stg_parse_tasking(char const *name, char const *value,
1300                                     void *data) {
1301   __kmp_stg_parse_int(name, value, 0, (int)tskm_max,
1302                       (int *)&__kmp_tasking_mode);
1303 } // __kmp_stg_parse_tasking
1304 
__kmp_stg_print_tasking(kmp_str_buf_t * buffer,char const * name,void * data)1305 static void __kmp_stg_print_tasking(kmp_str_buf_t *buffer, char const *name,
1306                                     void *data) {
1307   __kmp_stg_print_int(buffer, name, __kmp_tasking_mode);
1308 } // __kmp_stg_print_tasking
1309 
__kmp_stg_parse_task_stealing(char const * name,char const * value,void * data)1310 static void __kmp_stg_parse_task_stealing(char const *name, char const *value,
1311                                           void *data) {
1312   __kmp_stg_parse_int(name, value, 0, 1,
1313                       (int *)&__kmp_task_stealing_constraint);
1314 } // __kmp_stg_parse_task_stealing
1315 
__kmp_stg_print_task_stealing(kmp_str_buf_t * buffer,char const * name,void * data)1316 static void __kmp_stg_print_task_stealing(kmp_str_buf_t *buffer,
1317                                           char const *name, void *data) {
1318   __kmp_stg_print_int(buffer, name, __kmp_task_stealing_constraint);
1319 } // __kmp_stg_print_task_stealing
1320 
__kmp_stg_parse_max_active_levels(char const * name,char const * value,void * data)1321 static void __kmp_stg_parse_max_active_levels(char const *name,
1322                                               char const *value, void *data) {
1323   kmp_uint64 tmp_dflt = 0;
1324   char const *msg = NULL;
1325   if (!__kmp_dflt_max_active_levels_set) {
1326     // Don't overwrite __kmp_dflt_max_active_levels if we get an invalid setting
1327     __kmp_str_to_uint(value, &tmp_dflt, &msg);
1328     if (msg != NULL) { // invalid setting; print warning and ignore
1329       KMP_WARNING(ParseSizeIntWarn, name, value, msg);
1330     } else if (tmp_dflt > KMP_MAX_ACTIVE_LEVELS_LIMIT) {
1331       // invalid setting; print warning and ignore
1332       msg = KMP_I18N_STR(ValueTooLarge);
1333       KMP_WARNING(ParseSizeIntWarn, name, value, msg);
1334     } else { // valid setting
1335       __kmp_type_convert(tmp_dflt, &(__kmp_dflt_max_active_levels));
1336       __kmp_dflt_max_active_levels_set = true;
1337     }
1338   }
1339 } // __kmp_stg_parse_max_active_levels
1340 
__kmp_stg_print_max_active_levels(kmp_str_buf_t * buffer,char const * name,void * data)1341 static void __kmp_stg_print_max_active_levels(kmp_str_buf_t *buffer,
1342                                               char const *name, void *data) {
1343   __kmp_stg_print_int(buffer, name, __kmp_dflt_max_active_levels);
1344 } // __kmp_stg_print_max_active_levels
1345 
1346 // -----------------------------------------------------------------------------
1347 // OpenMP 4.0: OMP_DEFAULT_DEVICE
__kmp_stg_parse_default_device(char const * name,char const * value,void * data)1348 static void __kmp_stg_parse_default_device(char const *name, char const *value,
1349                                            void *data) {
1350   __kmp_stg_parse_int(name, value, 0, KMP_MAX_DEFAULT_DEVICE_LIMIT,
1351                       &__kmp_default_device);
1352 } // __kmp_stg_parse_default_device
1353 
__kmp_stg_print_default_device(kmp_str_buf_t * buffer,char const * name,void * data)1354 static void __kmp_stg_print_default_device(kmp_str_buf_t *buffer,
1355                                            char const *name, void *data) {
1356   __kmp_stg_print_int(buffer, name, __kmp_default_device);
1357 } // __kmp_stg_print_default_device
1358 
1359 // -----------------------------------------------------------------------------
1360 // OpenMP 5.0: OMP_TARGET_OFFLOAD
__kmp_stg_parse_target_offload(char const * name,char const * value,void * data)1361 static void __kmp_stg_parse_target_offload(char const *name, char const *value,
1362                                            void *data) {
1363   const char *next = value;
1364   const char *scan = next;
1365 
1366   __kmp_target_offload = tgt_default;
1367   SKIP_WS(next);
1368   if (*next == '\0')
1369     return;
1370   scan = next;
1371   if (!__kmp_strcasecmp_with_sentinel("mandatory", scan, 0)) {
1372     __kmp_target_offload = tgt_mandatory;
1373   } else if (!__kmp_strcasecmp_with_sentinel("disabled", scan, 0)) {
1374     __kmp_target_offload = tgt_disabled;
1375   } else if (!__kmp_strcasecmp_with_sentinel("default", scan, 0)) {
1376     __kmp_target_offload = tgt_default;
1377   } else {
1378     KMP_WARNING(SyntaxErrorUsing, name, "DEFAULT");
1379   }
1380 
1381 } // __kmp_stg_parse_target_offload
1382 
__kmp_stg_print_target_offload(kmp_str_buf_t * buffer,char const * name,void * data)1383 static void __kmp_stg_print_target_offload(kmp_str_buf_t *buffer,
1384                                            char const *name, void *data) {
1385   const char *value = NULL;
1386   if (__kmp_target_offload == tgt_default)
1387     value = "DEFAULT";
1388   else if (__kmp_target_offload == tgt_mandatory)
1389     value = "MANDATORY";
1390   else if (__kmp_target_offload == tgt_disabled)
1391     value = "DISABLED";
1392   KMP_DEBUG_ASSERT(value);
1393   if (__kmp_env_format) {
1394     KMP_STR_BUF_PRINT_NAME;
1395   } else {
1396     __kmp_str_buf_print(buffer, "   %s", name);
1397   }
1398   __kmp_str_buf_print(buffer, "=%s\n", value);
1399 } // __kmp_stg_print_target_offload
1400 
1401 // -----------------------------------------------------------------------------
1402 // OpenMP 4.5: OMP_MAX_TASK_PRIORITY
__kmp_stg_parse_max_task_priority(char const * name,char const * value,void * data)1403 static void __kmp_stg_parse_max_task_priority(char const *name,
1404                                               char const *value, void *data) {
1405   __kmp_stg_parse_int(name, value, 0, KMP_MAX_TASK_PRIORITY_LIMIT,
1406                       &__kmp_max_task_priority);
1407 } // __kmp_stg_parse_max_task_priority
1408 
__kmp_stg_print_max_task_priority(kmp_str_buf_t * buffer,char const * name,void * data)1409 static void __kmp_stg_print_max_task_priority(kmp_str_buf_t *buffer,
1410                                               char const *name, void *data) {
1411   __kmp_stg_print_int(buffer, name, __kmp_max_task_priority);
1412 } // __kmp_stg_print_max_task_priority
1413 
1414 // KMP_TASKLOOP_MIN_TASKS
1415 // taskloop threshold to switch from recursive to linear tasks creation
__kmp_stg_parse_taskloop_min_tasks(char const * name,char const * value,void * data)1416 static void __kmp_stg_parse_taskloop_min_tasks(char const *name,
1417                                                char const *value, void *data) {
1418   int tmp;
1419   __kmp_stg_parse_int(name, value, 0, INT_MAX, &tmp);
1420   __kmp_taskloop_min_tasks = tmp;
1421 } // __kmp_stg_parse_taskloop_min_tasks
1422 
__kmp_stg_print_taskloop_min_tasks(kmp_str_buf_t * buffer,char const * name,void * data)1423 static void __kmp_stg_print_taskloop_min_tasks(kmp_str_buf_t *buffer,
1424                                                char const *name, void *data) {
1425   __kmp_stg_print_uint64(buffer, name, __kmp_taskloop_min_tasks);
1426 } // __kmp_stg_print_taskloop_min_tasks
1427 
1428 // -----------------------------------------------------------------------------
1429 // KMP_DISP_NUM_BUFFERS
__kmp_stg_parse_disp_buffers(char const * name,char const * value,void * data)1430 static void __kmp_stg_parse_disp_buffers(char const *name, char const *value,
1431                                          void *data) {
1432   if (TCR_4(__kmp_init_serial)) {
1433     KMP_WARNING(EnvSerialWarn, name);
1434     return;
1435   } // read value before serial initialization only
1436   __kmp_stg_parse_int(name, value, KMP_MIN_DISP_NUM_BUFF, KMP_MAX_DISP_NUM_BUFF,
1437                       &__kmp_dispatch_num_buffers);
1438 } // __kmp_stg_parse_disp_buffers
1439 
__kmp_stg_print_disp_buffers(kmp_str_buf_t * buffer,char const * name,void * data)1440 static void __kmp_stg_print_disp_buffers(kmp_str_buf_t *buffer,
1441                                          char const *name, void *data) {
1442   __kmp_stg_print_int(buffer, name, __kmp_dispatch_num_buffers);
1443 } // __kmp_stg_print_disp_buffers
1444 
1445 #if KMP_NESTED_HOT_TEAMS
1446 // -----------------------------------------------------------------------------
1447 // KMP_HOT_TEAMS_MAX_LEVEL, KMP_HOT_TEAMS_MODE
1448 
__kmp_stg_parse_hot_teams_level(char const * name,char const * value,void * data)1449 static void __kmp_stg_parse_hot_teams_level(char const *name, char const *value,
1450                                             void *data) {
1451   if (TCR_4(__kmp_init_parallel)) {
1452     KMP_WARNING(EnvParallelWarn, name);
1453     return;
1454   } // read value before first parallel only
1455   __kmp_stg_parse_int(name, value, 0, KMP_MAX_ACTIVE_LEVELS_LIMIT,
1456                       &__kmp_hot_teams_max_level);
1457 } // __kmp_stg_parse_hot_teams_level
1458 
__kmp_stg_print_hot_teams_level(kmp_str_buf_t * buffer,char const * name,void * data)1459 static void __kmp_stg_print_hot_teams_level(kmp_str_buf_t *buffer,
1460                                             char const *name, void *data) {
1461   __kmp_stg_print_int(buffer, name, __kmp_hot_teams_max_level);
1462 } // __kmp_stg_print_hot_teams_level
1463 
__kmp_stg_parse_hot_teams_mode(char const * name,char const * value,void * data)1464 static void __kmp_stg_parse_hot_teams_mode(char const *name, char const *value,
1465                                            void *data) {
1466   if (TCR_4(__kmp_init_parallel)) {
1467     KMP_WARNING(EnvParallelWarn, name);
1468     return;
1469   } // read value before first parallel only
1470   __kmp_stg_parse_int(name, value, 0, KMP_MAX_ACTIVE_LEVELS_LIMIT,
1471                       &__kmp_hot_teams_mode);
1472 } // __kmp_stg_parse_hot_teams_mode
1473 
__kmp_stg_print_hot_teams_mode(kmp_str_buf_t * buffer,char const * name,void * data)1474 static void __kmp_stg_print_hot_teams_mode(kmp_str_buf_t *buffer,
1475                                            char const *name, void *data) {
1476   __kmp_stg_print_int(buffer, name, __kmp_hot_teams_mode);
1477 } // __kmp_stg_print_hot_teams_mode
1478 
1479 #endif // KMP_NESTED_HOT_TEAMS
1480 
1481 // -----------------------------------------------------------------------------
1482 // KMP_HANDLE_SIGNALS
1483 
1484 #if KMP_HANDLE_SIGNALS
1485 
__kmp_stg_parse_handle_signals(char const * name,char const * value,void * data)1486 static void __kmp_stg_parse_handle_signals(char const *name, char const *value,
1487                                            void *data) {
1488   __kmp_stg_parse_bool(name, value, &__kmp_handle_signals);
1489 } // __kmp_stg_parse_handle_signals
1490 
__kmp_stg_print_handle_signals(kmp_str_buf_t * buffer,char const * name,void * data)1491 static void __kmp_stg_print_handle_signals(kmp_str_buf_t *buffer,
1492                                            char const *name, void *data) {
1493   __kmp_stg_print_bool(buffer, name, __kmp_handle_signals);
1494 } // __kmp_stg_print_handle_signals
1495 
1496 #endif // KMP_HANDLE_SIGNALS
1497 
1498 // -----------------------------------------------------------------------------
1499 // KMP_X_DEBUG, KMP_DEBUG, KMP_DEBUG_BUF_*, KMP_DIAG
1500 
1501 #ifdef KMP_DEBUG
1502 
1503 #define KMP_STG_X_DEBUG(x)                                                     \
1504   static void __kmp_stg_parse_##x##_debug(char const *name, char const *value, \
1505                                           void *data) {                        \
1506     __kmp_stg_parse_int(name, value, 0, INT_MAX, &kmp_##x##_debug);            \
1507   } /* __kmp_stg_parse_x_debug */                                              \
1508   static void __kmp_stg_print_##x##_debug(kmp_str_buf_t *buffer,               \
1509                                           char const *name, void *data) {      \
1510     __kmp_stg_print_int(buffer, name, kmp_##x##_debug);                        \
1511   } /* __kmp_stg_print_x_debug */
1512 
1513 KMP_STG_X_DEBUG(a)
KMP_STG_X_DEBUG(b)1514 KMP_STG_X_DEBUG(b)
1515 KMP_STG_X_DEBUG(c)
1516 KMP_STG_X_DEBUG(d)
1517 KMP_STG_X_DEBUG(e)
1518 KMP_STG_X_DEBUG(f)
1519 
1520 #undef KMP_STG_X_DEBUG
1521 
1522 static void __kmp_stg_parse_debug(char const *name, char const *value,
1523                                   void *data) {
1524   int debug = 0;
1525   __kmp_stg_parse_int(name, value, 0, INT_MAX, &debug);
1526   if (kmp_a_debug < debug) {
1527     kmp_a_debug = debug;
1528   }
1529   if (kmp_b_debug < debug) {
1530     kmp_b_debug = debug;
1531   }
1532   if (kmp_c_debug < debug) {
1533     kmp_c_debug = debug;
1534   }
1535   if (kmp_d_debug < debug) {
1536     kmp_d_debug = debug;
1537   }
1538   if (kmp_e_debug < debug) {
1539     kmp_e_debug = debug;
1540   }
1541   if (kmp_f_debug < debug) {
1542     kmp_f_debug = debug;
1543   }
1544 } // __kmp_stg_parse_debug
1545 
__kmp_stg_parse_debug_buf(char const * name,char const * value,void * data)1546 static void __kmp_stg_parse_debug_buf(char const *name, char const *value,
1547                                       void *data) {
1548   __kmp_stg_parse_bool(name, value, &__kmp_debug_buf);
1549   // !!! TODO: Move buffer initialization of of this file! It may works
1550   // incorrectly if KMP_DEBUG_BUF is parsed before KMP_DEBUG_BUF_LINES or
1551   // KMP_DEBUG_BUF_CHARS.
1552   if (__kmp_debug_buf) {
1553     int i;
1554     int elements = __kmp_debug_buf_lines * __kmp_debug_buf_chars;
1555 
1556     /* allocate and initialize all entries in debug buffer to empty */
1557     __kmp_debug_buffer = (char *)__kmp_page_allocate(elements * sizeof(char));
1558     for (i = 0; i < elements; i += __kmp_debug_buf_chars)
1559       __kmp_debug_buffer[i] = '\0';
1560 
1561     __kmp_debug_count = 0;
1562   }
1563   K_DIAG(1, ("__kmp_debug_buf = %d\n", __kmp_debug_buf));
1564 } // __kmp_stg_parse_debug_buf
1565 
__kmp_stg_print_debug_buf(kmp_str_buf_t * buffer,char const * name,void * data)1566 static void __kmp_stg_print_debug_buf(kmp_str_buf_t *buffer, char const *name,
1567                                       void *data) {
1568   __kmp_stg_print_bool(buffer, name, __kmp_debug_buf);
1569 } // __kmp_stg_print_debug_buf
1570 
__kmp_stg_parse_debug_buf_atomic(char const * name,char const * value,void * data)1571 static void __kmp_stg_parse_debug_buf_atomic(char const *name,
1572                                              char const *value, void *data) {
1573   __kmp_stg_parse_bool(name, value, &__kmp_debug_buf_atomic);
1574 } // __kmp_stg_parse_debug_buf_atomic
1575 
__kmp_stg_print_debug_buf_atomic(kmp_str_buf_t * buffer,char const * name,void * data)1576 static void __kmp_stg_print_debug_buf_atomic(kmp_str_buf_t *buffer,
1577                                              char const *name, void *data) {
1578   __kmp_stg_print_bool(buffer, name, __kmp_debug_buf_atomic);
1579 } // __kmp_stg_print_debug_buf_atomic
1580 
__kmp_stg_parse_debug_buf_chars(char const * name,char const * value,void * data)1581 static void __kmp_stg_parse_debug_buf_chars(char const *name, char const *value,
1582                                             void *data) {
1583   __kmp_stg_parse_int(name, value, KMP_DEBUG_BUF_CHARS_MIN, INT_MAX,
1584                       &__kmp_debug_buf_chars);
1585 } // __kmp_stg_debug_parse_buf_chars
1586 
__kmp_stg_print_debug_buf_chars(kmp_str_buf_t * buffer,char const * name,void * data)1587 static void __kmp_stg_print_debug_buf_chars(kmp_str_buf_t *buffer,
1588                                             char const *name, void *data) {
1589   __kmp_stg_print_int(buffer, name, __kmp_debug_buf_chars);
1590 } // __kmp_stg_print_debug_buf_chars
1591 
__kmp_stg_parse_debug_buf_lines(char const * name,char const * value,void * data)1592 static void __kmp_stg_parse_debug_buf_lines(char const *name, char const *value,
1593                                             void *data) {
1594   __kmp_stg_parse_int(name, value, KMP_DEBUG_BUF_LINES_MIN, INT_MAX,
1595                       &__kmp_debug_buf_lines);
1596 } // __kmp_stg_parse_debug_buf_lines
1597 
__kmp_stg_print_debug_buf_lines(kmp_str_buf_t * buffer,char const * name,void * data)1598 static void __kmp_stg_print_debug_buf_lines(kmp_str_buf_t *buffer,
1599                                             char const *name, void *data) {
1600   __kmp_stg_print_int(buffer, name, __kmp_debug_buf_lines);
1601 } // __kmp_stg_print_debug_buf_lines
1602 
__kmp_stg_parse_diag(char const * name,char const * value,void * data)1603 static void __kmp_stg_parse_diag(char const *name, char const *value,
1604                                  void *data) {
1605   __kmp_stg_parse_int(name, value, 0, INT_MAX, &kmp_diag);
1606 } // __kmp_stg_parse_diag
1607 
__kmp_stg_print_diag(kmp_str_buf_t * buffer,char const * name,void * data)1608 static void __kmp_stg_print_diag(kmp_str_buf_t *buffer, char const *name,
1609                                  void *data) {
1610   __kmp_stg_print_int(buffer, name, kmp_diag);
1611 } // __kmp_stg_print_diag
1612 
1613 #endif // KMP_DEBUG
1614 
1615 // -----------------------------------------------------------------------------
1616 // KMP_ALIGN_ALLOC
1617 
__kmp_stg_parse_align_alloc(char const * name,char const * value,void * data)1618 static void __kmp_stg_parse_align_alloc(char const *name, char const *value,
1619                                         void *data) {
1620   __kmp_stg_parse_size(name, value, CACHE_LINE, INT_MAX, NULL,
1621                        &__kmp_align_alloc, 1);
1622 } // __kmp_stg_parse_align_alloc
1623 
__kmp_stg_print_align_alloc(kmp_str_buf_t * buffer,char const * name,void * data)1624 static void __kmp_stg_print_align_alloc(kmp_str_buf_t *buffer, char const *name,
1625                                         void *data) {
1626   __kmp_stg_print_size(buffer, name, __kmp_align_alloc);
1627 } // __kmp_stg_print_align_alloc
1628 
1629 // -----------------------------------------------------------------------------
1630 // KMP_PLAIN_BARRIER, KMP_FORKJOIN_BARRIER, KMP_REDUCTION_BARRIER
1631 
1632 // TODO: Remove __kmp_barrier_branch_bit_env_name varibale, remove loops from
1633 // parse and print functions, pass required info through data argument.
1634 
__kmp_stg_parse_barrier_branch_bit(char const * name,char const * value,void * data)1635 static void __kmp_stg_parse_barrier_branch_bit(char const *name,
1636                                                char const *value, void *data) {
1637   const char *var;
1638 
1639   /* ---------- Barrier branch bit control ------------ */
1640   for (int i = bs_plain_barrier; i < bs_last_barrier; i++) {
1641     var = __kmp_barrier_branch_bit_env_name[i];
1642     if ((strcmp(var, name) == 0) && (value != 0)) {
1643       char *comma;
1644 
1645       comma = CCAST(char *, strchr(value, ','));
1646       __kmp_barrier_gather_branch_bits[i] =
1647           (kmp_uint32)__kmp_str_to_int(value, ',');
1648       /* is there a specified release parameter? */
1649       if (comma == NULL) {
1650         __kmp_barrier_release_branch_bits[i] = __kmp_barrier_release_bb_dflt;
1651       } else {
1652         __kmp_barrier_release_branch_bits[i] =
1653             (kmp_uint32)__kmp_str_to_int(comma + 1, 0);
1654 
1655         if (__kmp_barrier_release_branch_bits[i] > KMP_MAX_BRANCH_BITS) {
1656           __kmp_msg(kmp_ms_warning,
1657                     KMP_MSG(BarrReleaseValueInvalid, name, comma + 1),
1658                     __kmp_msg_null);
1659           __kmp_barrier_release_branch_bits[i] = __kmp_barrier_release_bb_dflt;
1660         }
1661       }
1662       if (__kmp_barrier_gather_branch_bits[i] > KMP_MAX_BRANCH_BITS) {
1663         KMP_WARNING(BarrGatherValueInvalid, name, value);
1664         KMP_INFORM(Using_uint_Value, name, __kmp_barrier_gather_bb_dflt);
1665         __kmp_barrier_gather_branch_bits[i] = __kmp_barrier_gather_bb_dflt;
1666       }
1667     }
1668     K_DIAG(1, ("%s == %d,%d\n", __kmp_barrier_branch_bit_env_name[i],
1669                __kmp_barrier_gather_branch_bits[i],
1670                __kmp_barrier_release_branch_bits[i]))
1671   }
1672 } // __kmp_stg_parse_barrier_branch_bit
1673 
__kmp_stg_print_barrier_branch_bit(kmp_str_buf_t * buffer,char const * name,void * data)1674 static void __kmp_stg_print_barrier_branch_bit(kmp_str_buf_t *buffer,
1675                                                char const *name, void *data) {
1676   const char *var;
1677   for (int i = bs_plain_barrier; i < bs_last_barrier; i++) {
1678     var = __kmp_barrier_branch_bit_env_name[i];
1679     if (strcmp(var, name) == 0) {
1680       if (__kmp_env_format) {
1681         KMP_STR_BUF_PRINT_NAME_EX(__kmp_barrier_branch_bit_env_name[i]);
1682       } else {
1683         __kmp_str_buf_print(buffer, "   %s='",
1684                             __kmp_barrier_branch_bit_env_name[i]);
1685       }
1686       __kmp_str_buf_print(buffer, "%d,%d'\n",
1687                           __kmp_barrier_gather_branch_bits[i],
1688                           __kmp_barrier_release_branch_bits[i]);
1689     }
1690   }
1691 } // __kmp_stg_print_barrier_branch_bit
1692 
1693 // ----------------------------------------------------------------------------
1694 // KMP_PLAIN_BARRIER_PATTERN, KMP_FORKJOIN_BARRIER_PATTERN,
1695 // KMP_REDUCTION_BARRIER_PATTERN
1696 
1697 // TODO: Remove __kmp_barrier_pattern_name variable, remove loops from parse and
1698 // print functions, pass required data to functions through data argument.
1699 
__kmp_stg_parse_barrier_pattern(char const * name,char const * value,void * data)1700 static void __kmp_stg_parse_barrier_pattern(char const *name, char const *value,
1701                                             void *data) {
1702   const char *var;
1703   /* ---------- Barrier method control ------------ */
1704 
1705   static int dist_req = 0, non_dist_req = 0;
1706   static bool warn = 1;
1707   for (int i = bs_plain_barrier; i < bs_last_barrier; i++) {
1708     var = __kmp_barrier_pattern_env_name[i];
1709 
1710     if ((strcmp(var, name) == 0) && (value != 0)) {
1711       int j;
1712       char *comma = CCAST(char *, strchr(value, ','));
1713 
1714       /* handle first parameter: gather pattern */
1715       for (j = bp_linear_bar; j < bp_last_bar; j++) {
1716         if (__kmp_match_with_sentinel(__kmp_barrier_pattern_name[j], value, 1,
1717                                       ',')) {
1718           if (j == bp_dist_bar) {
1719             dist_req++;
1720           } else {
1721             non_dist_req++;
1722           }
1723           __kmp_barrier_gather_pattern[i] = (kmp_bar_pat_e)j;
1724           break;
1725         }
1726       }
1727       if (j == bp_last_bar) {
1728         KMP_WARNING(BarrGatherValueInvalid, name, value);
1729         KMP_INFORM(Using_str_Value, name,
1730                    __kmp_barrier_pattern_name[bp_linear_bar]);
1731       }
1732 
1733       /* handle second parameter: release pattern */
1734       if (comma != NULL) {
1735         for (j = bp_linear_bar; j < bp_last_bar; j++) {
1736           if (__kmp_str_match(__kmp_barrier_pattern_name[j], 1, comma + 1)) {
1737             if (j == bp_dist_bar) {
1738               dist_req++;
1739             } else {
1740               non_dist_req++;
1741             }
1742             __kmp_barrier_release_pattern[i] = (kmp_bar_pat_e)j;
1743             break;
1744           }
1745         }
1746         if (j == bp_last_bar) {
1747           __kmp_msg(kmp_ms_warning,
1748                     KMP_MSG(BarrReleaseValueInvalid, name, comma + 1),
1749                     __kmp_msg_null);
1750           KMP_INFORM(Using_str_Value, name,
1751                      __kmp_barrier_pattern_name[bp_linear_bar]);
1752         }
1753       }
1754     }
1755   }
1756   if ((dist_req == 0) && (non_dist_req != 0)) {
1757     // Something was set to a barrier other than dist; set all others to hyper
1758     for (int i = bs_plain_barrier; i < bs_last_barrier; i++) {
1759       if (__kmp_barrier_release_pattern[i] == bp_dist_bar)
1760         __kmp_barrier_release_pattern[i] = bp_hyper_bar;
1761       if (__kmp_barrier_gather_pattern[i] == bp_dist_bar)
1762         __kmp_barrier_gather_pattern[i] = bp_hyper_bar;
1763     }
1764   } else if (non_dist_req != 0) {
1765     // some requests for dist, plus requests for others; set all to dist
1766     if (non_dist_req > 0 && dist_req > 0 && warn) {
1767       KMP_INFORM(BarrierPatternOverride, name,
1768                  __kmp_barrier_pattern_name[bp_dist_bar]);
1769       warn = 0;
1770     }
1771     for (int i = bs_plain_barrier; i < bs_last_barrier; i++) {
1772       if (__kmp_barrier_release_pattern[i] != bp_dist_bar)
1773         __kmp_barrier_release_pattern[i] = bp_dist_bar;
1774       if (__kmp_barrier_gather_pattern[i] != bp_dist_bar)
1775         __kmp_barrier_gather_pattern[i] = bp_dist_bar;
1776     }
1777   }
1778 } // __kmp_stg_parse_barrier_pattern
1779 
__kmp_stg_print_barrier_pattern(kmp_str_buf_t * buffer,char const * name,void * data)1780 static void __kmp_stg_print_barrier_pattern(kmp_str_buf_t *buffer,
1781                                             char const *name, void *data) {
1782   const char *var;
1783   for (int i = bs_plain_barrier; i < bs_last_barrier; i++) {
1784     var = __kmp_barrier_pattern_env_name[i];
1785     if (strcmp(var, name) == 0) {
1786       int j = __kmp_barrier_gather_pattern[i];
1787       int k = __kmp_barrier_release_pattern[i];
1788       if (__kmp_env_format) {
1789         KMP_STR_BUF_PRINT_NAME_EX(__kmp_barrier_pattern_env_name[i]);
1790       } else {
1791         __kmp_str_buf_print(buffer, "   %s='",
1792                             __kmp_barrier_pattern_env_name[i]);
1793       }
1794       KMP_DEBUG_ASSERT(j < bp_last_bar && k < bp_last_bar);
1795       __kmp_str_buf_print(buffer, "%s,%s'\n", __kmp_barrier_pattern_name[j],
1796                           __kmp_barrier_pattern_name[k]);
1797     }
1798   }
1799 } // __kmp_stg_print_barrier_pattern
1800 
1801 // -----------------------------------------------------------------------------
1802 // KMP_ABORT_DELAY
1803 
__kmp_stg_parse_abort_delay(char const * name,char const * value,void * data)1804 static void __kmp_stg_parse_abort_delay(char const *name, char const *value,
1805                                         void *data) {
1806   // Units of KMP_DELAY_ABORT are seconds, units of __kmp_abort_delay is
1807   // milliseconds.
1808   int delay = __kmp_abort_delay / 1000;
1809   __kmp_stg_parse_int(name, value, 0, INT_MAX / 1000, &delay);
1810   __kmp_abort_delay = delay * 1000;
1811 } // __kmp_stg_parse_abort_delay
1812 
__kmp_stg_print_abort_delay(kmp_str_buf_t * buffer,char const * name,void * data)1813 static void __kmp_stg_print_abort_delay(kmp_str_buf_t *buffer, char const *name,
1814                                         void *data) {
1815   __kmp_stg_print_int(buffer, name, __kmp_abort_delay);
1816 } // __kmp_stg_print_abort_delay
1817 
1818 // -----------------------------------------------------------------------------
1819 // KMP_CPUINFO_FILE
1820 
__kmp_stg_parse_cpuinfo_file(char const * name,char const * value,void * data)1821 static void __kmp_stg_parse_cpuinfo_file(char const *name, char const *value,
1822                                          void *data) {
1823 #if KMP_AFFINITY_SUPPORTED
1824   __kmp_stg_parse_str(name, value, &__kmp_cpuinfo_file);
1825   K_DIAG(1, ("__kmp_cpuinfo_file == %s\n", __kmp_cpuinfo_file));
1826 #endif
1827 } //__kmp_stg_parse_cpuinfo_file
1828 
__kmp_stg_print_cpuinfo_file(kmp_str_buf_t * buffer,char const * name,void * data)1829 static void __kmp_stg_print_cpuinfo_file(kmp_str_buf_t *buffer,
1830                                          char const *name, void *data) {
1831 #if KMP_AFFINITY_SUPPORTED
1832   if (__kmp_env_format) {
1833     KMP_STR_BUF_PRINT_NAME;
1834   } else {
1835     __kmp_str_buf_print(buffer, "   %s", name);
1836   }
1837   if (__kmp_cpuinfo_file) {
1838     __kmp_str_buf_print(buffer, "='%s'\n", __kmp_cpuinfo_file);
1839   } else {
1840     __kmp_str_buf_print(buffer, ": %s\n", KMP_I18N_STR(NotDefined));
1841   }
1842 #endif
1843 } //__kmp_stg_print_cpuinfo_file
1844 
1845 // -----------------------------------------------------------------------------
1846 // KMP_FORCE_REDUCTION, KMP_DETERMINISTIC_REDUCTION
1847 
__kmp_stg_parse_force_reduction(char const * name,char const * value,void * data)1848 static void __kmp_stg_parse_force_reduction(char const *name, char const *value,
1849                                             void *data) {
1850   kmp_stg_fr_data_t *reduction = (kmp_stg_fr_data_t *)data;
1851   int rc;
1852 
1853   rc = __kmp_stg_check_rivals(name, value, reduction->rivals);
1854   if (rc) {
1855     return;
1856   }
1857   if (reduction->force) {
1858     if (value != 0) {
1859       if (__kmp_str_match("critical", 0, value))
1860         __kmp_force_reduction_method = critical_reduce_block;
1861       else if (__kmp_str_match("atomic", 0, value))
1862         __kmp_force_reduction_method = atomic_reduce_block;
1863       else if (__kmp_str_match("tree", 0, value))
1864         __kmp_force_reduction_method = tree_reduce_block;
1865       else {
1866         KMP_FATAL(UnknownForceReduction, name, value);
1867       }
1868     }
1869   } else {
1870     __kmp_stg_parse_bool(name, value, &__kmp_determ_red);
1871     if (__kmp_determ_red) {
1872       __kmp_force_reduction_method = tree_reduce_block;
1873     } else {
1874       __kmp_force_reduction_method = reduction_method_not_defined;
1875     }
1876   }
1877   K_DIAG(1, ("__kmp_force_reduction_method == %d\n",
1878              __kmp_force_reduction_method));
1879 } // __kmp_stg_parse_force_reduction
1880 
__kmp_stg_print_force_reduction(kmp_str_buf_t * buffer,char const * name,void * data)1881 static void __kmp_stg_print_force_reduction(kmp_str_buf_t *buffer,
1882                                             char const *name, void *data) {
1883 
1884   kmp_stg_fr_data_t *reduction = (kmp_stg_fr_data_t *)data;
1885   if (reduction->force) {
1886     if (__kmp_force_reduction_method == critical_reduce_block) {
1887       __kmp_stg_print_str(buffer, name, "critical");
1888     } else if (__kmp_force_reduction_method == atomic_reduce_block) {
1889       __kmp_stg_print_str(buffer, name, "atomic");
1890     } else if (__kmp_force_reduction_method == tree_reduce_block) {
1891       __kmp_stg_print_str(buffer, name, "tree");
1892     } else {
1893       if (__kmp_env_format) {
1894         KMP_STR_BUF_PRINT_NAME;
1895       } else {
1896         __kmp_str_buf_print(buffer, "   %s", name);
1897       }
1898       __kmp_str_buf_print(buffer, ": %s\n", KMP_I18N_STR(NotDefined));
1899     }
1900   } else {
1901     __kmp_stg_print_bool(buffer, name, __kmp_determ_red);
1902   }
1903 
1904 } // __kmp_stg_print_force_reduction
1905 
1906 // -----------------------------------------------------------------------------
1907 // KMP_STORAGE_MAP
1908 
__kmp_stg_parse_storage_map(char const * name,char const * value,void * data)1909 static void __kmp_stg_parse_storage_map(char const *name, char const *value,
1910                                         void *data) {
1911   if (__kmp_str_match("verbose", 1, value)) {
1912     __kmp_storage_map = TRUE;
1913     __kmp_storage_map_verbose = TRUE;
1914     __kmp_storage_map_verbose_specified = TRUE;
1915 
1916   } else {
1917     __kmp_storage_map_verbose = FALSE;
1918     __kmp_stg_parse_bool(name, value, &__kmp_storage_map); // !!!
1919   }
1920 } // __kmp_stg_parse_storage_map
1921 
__kmp_stg_print_storage_map(kmp_str_buf_t * buffer,char const * name,void * data)1922 static void __kmp_stg_print_storage_map(kmp_str_buf_t *buffer, char const *name,
1923                                         void *data) {
1924   if (__kmp_storage_map_verbose || __kmp_storage_map_verbose_specified) {
1925     __kmp_stg_print_str(buffer, name, "verbose");
1926   } else {
1927     __kmp_stg_print_bool(buffer, name, __kmp_storage_map);
1928   }
1929 } // __kmp_stg_print_storage_map
1930 
1931 // -----------------------------------------------------------------------------
1932 // KMP_ALL_THREADPRIVATE
1933 
__kmp_stg_parse_all_threadprivate(char const * name,char const * value,void * data)1934 static void __kmp_stg_parse_all_threadprivate(char const *name,
1935                                               char const *value, void *data) {
1936   __kmp_stg_parse_int(name, value,
1937                       __kmp_allThreadsSpecified ? __kmp_max_nth : 1,
1938                       __kmp_max_nth, &__kmp_tp_capacity);
1939 } // __kmp_stg_parse_all_threadprivate
1940 
__kmp_stg_print_all_threadprivate(kmp_str_buf_t * buffer,char const * name,void * data)1941 static void __kmp_stg_print_all_threadprivate(kmp_str_buf_t *buffer,
1942                                               char const *name, void *data) {
1943   __kmp_stg_print_int(buffer, name, __kmp_tp_capacity);
1944 }
1945 
1946 // -----------------------------------------------------------------------------
1947 // KMP_FOREIGN_THREADS_THREADPRIVATE
1948 
__kmp_stg_parse_foreign_threads_threadprivate(char const * name,char const * value,void * data)1949 static void __kmp_stg_parse_foreign_threads_threadprivate(char const *name,
1950                                                           char const *value,
1951                                                           void *data) {
1952   __kmp_stg_parse_bool(name, value, &__kmp_foreign_tp);
1953 } // __kmp_stg_parse_foreign_threads_threadprivate
1954 
__kmp_stg_print_foreign_threads_threadprivate(kmp_str_buf_t * buffer,char const * name,void * data)1955 static void __kmp_stg_print_foreign_threads_threadprivate(kmp_str_buf_t *buffer,
1956                                                           char const *name,
1957                                                           void *data) {
1958   __kmp_stg_print_bool(buffer, name, __kmp_foreign_tp);
1959 } // __kmp_stg_print_foreign_threads_threadprivate
1960 
1961 // -----------------------------------------------------------------------------
1962 // KMP_AFFINITY, GOMP_CPU_AFFINITY, KMP_TOPOLOGY_METHOD
1963 
1964 #if KMP_AFFINITY_SUPPORTED
1965 // Parse the proc id list.  Return TRUE if successful, FALSE otherwise.
__kmp_parse_affinity_proc_id_list(const char * var,const char * env,const char ** nextEnv,char ** proclist)1966 static int __kmp_parse_affinity_proc_id_list(const char *var, const char *env,
1967                                              const char **nextEnv,
1968                                              char **proclist) {
1969   const char *scan = env;
1970   const char *next = scan;
1971   int empty = TRUE;
1972 
1973   *proclist = NULL;
1974 
1975   for (;;) {
1976     int start, end, stride;
1977 
1978     SKIP_WS(scan);
1979     next = scan;
1980     if (*next == '\0') {
1981       break;
1982     }
1983 
1984     if (*next == '{') {
1985       int num;
1986       next++; // skip '{'
1987       SKIP_WS(next);
1988       scan = next;
1989 
1990       // Read the first integer in the set.
1991       if ((*next < '0') || (*next > '9')) {
1992         KMP_WARNING(AffSyntaxError, var);
1993         return FALSE;
1994       }
1995       SKIP_DIGITS(next);
1996       num = __kmp_str_to_int(scan, *next);
1997       KMP_ASSERT(num >= 0);
1998 
1999       for (;;) {
2000         // Check for end of set.
2001         SKIP_WS(next);
2002         if (*next == '}') {
2003           next++; // skip '}'
2004           break;
2005         }
2006 
2007         // Skip optional comma.
2008         if (*next == ',') {
2009           next++;
2010         }
2011         SKIP_WS(next);
2012 
2013         // Read the next integer in the set.
2014         scan = next;
2015         if ((*next < '0') || (*next > '9')) {
2016           KMP_WARNING(AffSyntaxError, var);
2017           return FALSE;
2018         }
2019 
2020         SKIP_DIGITS(next);
2021         num = __kmp_str_to_int(scan, *next);
2022         KMP_ASSERT(num >= 0);
2023       }
2024       empty = FALSE;
2025 
2026       SKIP_WS(next);
2027       if (*next == ',') {
2028         next++;
2029       }
2030       scan = next;
2031       continue;
2032     }
2033 
2034     // Next character is not an integer => end of list
2035     if ((*next < '0') || (*next > '9')) {
2036       if (empty) {
2037         KMP_WARNING(AffSyntaxError, var);
2038         return FALSE;
2039       }
2040       break;
2041     }
2042 
2043     // Read the first integer.
2044     SKIP_DIGITS(next);
2045     start = __kmp_str_to_int(scan, *next);
2046     KMP_ASSERT(start >= 0);
2047     SKIP_WS(next);
2048 
2049     // If this isn't a range, then go on.
2050     if (*next != '-') {
2051       empty = FALSE;
2052 
2053       // Skip optional comma.
2054       if (*next == ',') {
2055         next++;
2056       }
2057       scan = next;
2058       continue;
2059     }
2060 
2061     // This is a range.  Skip over the '-' and read in the 2nd int.
2062     next++; // skip '-'
2063     SKIP_WS(next);
2064     scan = next;
2065     if ((*next < '0') || (*next > '9')) {
2066       KMP_WARNING(AffSyntaxError, var);
2067       return FALSE;
2068     }
2069     SKIP_DIGITS(next);
2070     end = __kmp_str_to_int(scan, *next);
2071     KMP_ASSERT(end >= 0);
2072 
2073     // Check for a stride parameter
2074     stride = 1;
2075     SKIP_WS(next);
2076     if (*next == ':') {
2077       // A stride is specified.  Skip over the ':" and read the 3rd int.
2078       int sign = +1;
2079       next++; // skip ':'
2080       SKIP_WS(next);
2081       scan = next;
2082       if (*next == '-') {
2083         sign = -1;
2084         next++;
2085         SKIP_WS(next);
2086         scan = next;
2087       }
2088       if ((*next < '0') || (*next > '9')) {
2089         KMP_WARNING(AffSyntaxError, var);
2090         return FALSE;
2091       }
2092       SKIP_DIGITS(next);
2093       stride = __kmp_str_to_int(scan, *next);
2094       KMP_ASSERT(stride >= 0);
2095       stride *= sign;
2096     }
2097 
2098     // Do some range checks.
2099     if (stride == 0) {
2100       KMP_WARNING(AffZeroStride, var);
2101       return FALSE;
2102     }
2103     if (stride > 0) {
2104       if (start > end) {
2105         KMP_WARNING(AffStartGreaterEnd, var, start, end);
2106         return FALSE;
2107       }
2108     } else {
2109       if (start < end) {
2110         KMP_WARNING(AffStrideLessZero, var, start, end);
2111         return FALSE;
2112       }
2113     }
2114     if ((end - start) / stride > 65536) {
2115       KMP_WARNING(AffRangeTooBig, var, end, start, stride);
2116       return FALSE;
2117     }
2118 
2119     empty = FALSE;
2120 
2121     // Skip optional comma.
2122     SKIP_WS(next);
2123     if (*next == ',') {
2124       next++;
2125     }
2126     scan = next;
2127   }
2128 
2129   *nextEnv = next;
2130 
2131   {
2132     ptrdiff_t len = next - env;
2133     char *retlist = (char *)__kmp_allocate((len + 1) * sizeof(char));
2134     KMP_MEMCPY_S(retlist, (len + 1) * sizeof(char), env, len * sizeof(char));
2135     retlist[len] = '\0';
2136     *proclist = retlist;
2137   }
2138   return TRUE;
2139 }
2140 
2141 // If KMP_AFFINITY is specified without a type, then
2142 // __kmp_affinity_notype should point to its setting.
2143 static kmp_setting_t *__kmp_affinity_notype = NULL;
2144 
__kmp_parse_affinity_env(char const * name,char const * value,enum affinity_type * out_type,char ** out_proclist,int * out_verbose,int * out_warn,int * out_respect,kmp_hw_t * out_gran,int * out_gran_levels,int * out_dups,int * out_compact,int * out_offset)2145 static void __kmp_parse_affinity_env(char const *name, char const *value,
2146                                      enum affinity_type *out_type,
2147                                      char **out_proclist, int *out_verbose,
2148                                      int *out_warn, int *out_respect,
2149                                      kmp_hw_t *out_gran, int *out_gran_levels,
2150                                      int *out_dups, int *out_compact,
2151                                      int *out_offset) {
2152   char *buffer = NULL; // Copy of env var value.
2153   char *buf = NULL; // Buffer for strtok_r() function.
2154   char *next = NULL; // end of token / start of next.
2155   const char *start; // start of current token (for err msgs)
2156   int count = 0; // Counter of parsed integer numbers.
2157   int number[2]; // Parsed numbers.
2158 
2159   // Guards.
2160   int type = 0;
2161   int proclist = 0;
2162   int verbose = 0;
2163   int warnings = 0;
2164   int respect = 0;
2165   int gran = 0;
2166   int dups = 0;
2167   bool set = false;
2168 
2169   KMP_ASSERT(value != NULL);
2170 
2171   if (TCR_4(__kmp_init_middle)) {
2172     KMP_WARNING(EnvMiddleWarn, name);
2173     __kmp_env_toPrint(name, 0);
2174     return;
2175   }
2176   __kmp_env_toPrint(name, 1);
2177 
2178   buffer =
2179       __kmp_str_format("%s", value); // Copy env var to keep original intact.
2180   buf = buffer;
2181   SKIP_WS(buf);
2182 
2183 // Helper macros.
2184 
2185 // If we see a parse error, emit a warning and scan to the next ",".
2186 //
2187 // FIXME - there's got to be a better way to print an error
2188 // message, hopefully without overwriting peices of buf.
2189 #define EMIT_WARN(skip, errlist)                                               \
2190   {                                                                            \
2191     char ch;                                                                   \
2192     if (skip) {                                                                \
2193       SKIP_TO(next, ',');                                                      \
2194     }                                                                          \
2195     ch = *next;                                                                \
2196     *next = '\0';                                                              \
2197     KMP_WARNING errlist;                                                       \
2198     *next = ch;                                                                \
2199     if (skip) {                                                                \
2200       if (ch == ',')                                                           \
2201         next++;                                                                \
2202     }                                                                          \
2203     buf = next;                                                                \
2204   }
2205 
2206 #define _set_param(_guard, _var, _val)                                         \
2207   {                                                                            \
2208     if (_guard == 0) {                                                         \
2209       _var = _val;                                                             \
2210     } else {                                                                   \
2211       EMIT_WARN(FALSE, (AffParamDefined, name, start));                        \
2212     }                                                                          \
2213     ++_guard;                                                                  \
2214   }
2215 
2216 #define set_type(val) _set_param(type, *out_type, val)
2217 #define set_verbose(val) _set_param(verbose, *out_verbose, val)
2218 #define set_warnings(val) _set_param(warnings, *out_warn, val)
2219 #define set_respect(val) _set_param(respect, *out_respect, val)
2220 #define set_dups(val) _set_param(dups, *out_dups, val)
2221 #define set_proclist(val) _set_param(proclist, *out_proclist, val)
2222 
2223 #define set_gran(val, levels)                                                  \
2224   {                                                                            \
2225     if (gran == 0) {                                                           \
2226       *out_gran = val;                                                         \
2227       *out_gran_levels = levels;                                               \
2228     } else {                                                                   \
2229       EMIT_WARN(FALSE, (AffParamDefined, name, start));                        \
2230     }                                                                          \
2231     ++gran;                                                                    \
2232   }
2233 
2234   KMP_DEBUG_ASSERT((__kmp_nested_proc_bind.bind_types != NULL) &&
2235                    (__kmp_nested_proc_bind.used > 0));
2236 
2237   while (*buf != '\0') {
2238     start = next = buf;
2239 
2240     if (__kmp_match_str("none", buf, CCAST(const char **, &next))) {
2241       set_type(affinity_none);
2242       __kmp_nested_proc_bind.bind_types[0] = proc_bind_false;
2243       buf = next;
2244     } else if (__kmp_match_str("scatter", buf, CCAST(const char **, &next))) {
2245       set_type(affinity_scatter);
2246       __kmp_nested_proc_bind.bind_types[0] = proc_bind_intel;
2247       buf = next;
2248     } else if (__kmp_match_str("compact", buf, CCAST(const char **, &next))) {
2249       set_type(affinity_compact);
2250       __kmp_nested_proc_bind.bind_types[0] = proc_bind_intel;
2251       buf = next;
2252     } else if (__kmp_match_str("logical", buf, CCAST(const char **, &next))) {
2253       set_type(affinity_logical);
2254       __kmp_nested_proc_bind.bind_types[0] = proc_bind_intel;
2255       buf = next;
2256     } else if (__kmp_match_str("physical", buf, CCAST(const char **, &next))) {
2257       set_type(affinity_physical);
2258       __kmp_nested_proc_bind.bind_types[0] = proc_bind_intel;
2259       buf = next;
2260     } else if (__kmp_match_str("explicit", buf, CCAST(const char **, &next))) {
2261       set_type(affinity_explicit);
2262       __kmp_nested_proc_bind.bind_types[0] = proc_bind_intel;
2263       buf = next;
2264     } else if (__kmp_match_str("balanced", buf, CCAST(const char **, &next))) {
2265       set_type(affinity_balanced);
2266       __kmp_nested_proc_bind.bind_types[0] = proc_bind_intel;
2267       buf = next;
2268     } else if (__kmp_match_str("disabled", buf, CCAST(const char **, &next))) {
2269       set_type(affinity_disabled);
2270       __kmp_nested_proc_bind.bind_types[0] = proc_bind_false;
2271       buf = next;
2272     } else if (__kmp_match_str("verbose", buf, CCAST(const char **, &next))) {
2273       set_verbose(TRUE);
2274       buf = next;
2275     } else if (__kmp_match_str("noverbose", buf, CCAST(const char **, &next))) {
2276       set_verbose(FALSE);
2277       buf = next;
2278     } else if (__kmp_match_str("warnings", buf, CCAST(const char **, &next))) {
2279       set_warnings(TRUE);
2280       buf = next;
2281     } else if (__kmp_match_str("nowarnings", buf,
2282                                CCAST(const char **, &next))) {
2283       set_warnings(FALSE);
2284       buf = next;
2285     } else if (__kmp_match_str("respect", buf, CCAST(const char **, &next))) {
2286       set_respect(TRUE);
2287       buf = next;
2288     } else if (__kmp_match_str("norespect", buf, CCAST(const char **, &next))) {
2289       set_respect(FALSE);
2290       buf = next;
2291     } else if (__kmp_match_str("duplicates", buf,
2292                                CCAST(const char **, &next)) ||
2293                __kmp_match_str("dups", buf, CCAST(const char **, &next))) {
2294       set_dups(TRUE);
2295       buf = next;
2296     } else if (__kmp_match_str("noduplicates", buf,
2297                                CCAST(const char **, &next)) ||
2298                __kmp_match_str("nodups", buf, CCAST(const char **, &next))) {
2299       set_dups(FALSE);
2300       buf = next;
2301     } else if (__kmp_match_str("granularity", buf,
2302                                CCAST(const char **, &next)) ||
2303                __kmp_match_str("gran", buf, CCAST(const char **, &next))) {
2304       SKIP_WS(next);
2305       if (*next != '=') {
2306         EMIT_WARN(TRUE, (AffInvalidParam, name, start));
2307         continue;
2308       }
2309       next++; // skip '='
2310       SKIP_WS(next);
2311 
2312       buf = next;
2313 
2314       // Try any hardware topology type for granularity
2315       KMP_FOREACH_HW_TYPE(type) {
2316         const char *name = __kmp_hw_get_keyword(type);
2317         if (__kmp_match_str(name, buf, CCAST(const char **, &next))) {
2318           set_gran(type, -1);
2319           buf = next;
2320           set = true;
2321           break;
2322         }
2323       }
2324       if (!set) {
2325         // Support older names for different granularity layers
2326         if (__kmp_match_str("fine", buf, CCAST(const char **, &next))) {
2327           set_gran(KMP_HW_THREAD, -1);
2328           buf = next;
2329           set = true;
2330         } else if (__kmp_match_str("package", buf,
2331                                    CCAST(const char **, &next))) {
2332           set_gran(KMP_HW_SOCKET, -1);
2333           buf = next;
2334           set = true;
2335         } else if (__kmp_match_str("node", buf, CCAST(const char **, &next))) {
2336           set_gran(KMP_HW_NUMA, -1);
2337           buf = next;
2338           set = true;
2339 #if KMP_GROUP_AFFINITY
2340         } else if (__kmp_match_str("group", buf, CCAST(const char **, &next))) {
2341           set_gran(KMP_HW_PROC_GROUP, -1);
2342           buf = next;
2343           set = true;
2344 #endif /* KMP_GROUP AFFINITY */
2345         } else if ((*buf >= '0') && (*buf <= '9')) {
2346           int n;
2347           next = buf;
2348           SKIP_DIGITS(next);
2349           n = __kmp_str_to_int(buf, *next);
2350           KMP_ASSERT(n >= 0);
2351           buf = next;
2352           set_gran(KMP_HW_UNKNOWN, n);
2353           set = true;
2354         } else {
2355           EMIT_WARN(TRUE, (AffInvalidParam, name, start));
2356           continue;
2357         }
2358       }
2359     } else if (__kmp_match_str("proclist", buf, CCAST(const char **, &next))) {
2360       char *temp_proclist;
2361 
2362       SKIP_WS(next);
2363       if (*next != '=') {
2364         EMIT_WARN(TRUE, (AffInvalidParam, name, start));
2365         continue;
2366       }
2367       next++; // skip '='
2368       SKIP_WS(next);
2369       if (*next != '[') {
2370         EMIT_WARN(TRUE, (AffInvalidParam, name, start));
2371         continue;
2372       }
2373       next++; // skip '['
2374       buf = next;
2375       if (!__kmp_parse_affinity_proc_id_list(
2376               name, buf, CCAST(const char **, &next), &temp_proclist)) {
2377         // warning already emitted.
2378         SKIP_TO(next, ']');
2379         if (*next == ']')
2380           next++;
2381         SKIP_TO(next, ',');
2382         if (*next == ',')
2383           next++;
2384         buf = next;
2385         continue;
2386       }
2387       if (*next != ']') {
2388         EMIT_WARN(TRUE, (AffInvalidParam, name, start));
2389         continue;
2390       }
2391       next++; // skip ']'
2392       set_proclist(temp_proclist);
2393     } else if ((*buf >= '0') && (*buf <= '9')) {
2394       // Parse integer numbers -- permute and offset.
2395       int n;
2396       next = buf;
2397       SKIP_DIGITS(next);
2398       n = __kmp_str_to_int(buf, *next);
2399       KMP_ASSERT(n >= 0);
2400       buf = next;
2401       if (count < 2) {
2402         number[count] = n;
2403       } else {
2404         KMP_WARNING(AffManyParams, name, start);
2405       }
2406       ++count;
2407     } else {
2408       EMIT_WARN(TRUE, (AffInvalidParam, name, start));
2409       continue;
2410     }
2411 
2412     SKIP_WS(next);
2413     if (*next == ',') {
2414       next++;
2415       SKIP_WS(next);
2416     } else if (*next != '\0') {
2417       const char *temp = next;
2418       EMIT_WARN(TRUE, (ParseExtraCharsWarn, name, temp));
2419       continue;
2420     }
2421     buf = next;
2422   } // while
2423 
2424 #undef EMIT_WARN
2425 #undef _set_param
2426 #undef set_type
2427 #undef set_verbose
2428 #undef set_warnings
2429 #undef set_respect
2430 #undef set_granularity
2431 
2432   __kmp_str_free(&buffer);
2433 
2434   if (proclist) {
2435     if (!type) {
2436       KMP_WARNING(AffProcListNoType, name);
2437       *out_type = affinity_explicit;
2438       __kmp_nested_proc_bind.bind_types[0] = proc_bind_intel;
2439     } else if (*out_type != affinity_explicit) {
2440       KMP_WARNING(AffProcListNotExplicit, name);
2441       KMP_ASSERT(*out_proclist != NULL);
2442       KMP_INTERNAL_FREE(*out_proclist);
2443       *out_proclist = NULL;
2444     }
2445   }
2446   switch (*out_type) {
2447   case affinity_logical:
2448   case affinity_physical: {
2449     if (count > 0) {
2450       *out_offset = number[0];
2451     }
2452     if (count > 1) {
2453       KMP_WARNING(AffManyParamsForLogic, name, number[1]);
2454     }
2455   } break;
2456   case affinity_balanced: {
2457     if (count > 0) {
2458       *out_compact = number[0];
2459     }
2460     if (count > 1) {
2461       *out_offset = number[1];
2462     }
2463 
2464     if (__kmp_affinity_gran == KMP_HW_UNKNOWN) {
2465 #if KMP_MIC_SUPPORTED
2466       if (__kmp_mic_type != non_mic) {
2467         if (__kmp_affinity_verbose || __kmp_affinity_warnings) {
2468           KMP_WARNING(AffGranUsing, "KMP_AFFINITY", "fine");
2469         }
2470         __kmp_affinity_gran = KMP_HW_THREAD;
2471       } else
2472 #endif
2473       {
2474         if (__kmp_affinity_verbose || __kmp_affinity_warnings) {
2475           KMP_WARNING(AffGranUsing, "KMP_AFFINITY", "core");
2476         }
2477         __kmp_affinity_gran = KMP_HW_CORE;
2478       }
2479     }
2480   } break;
2481   case affinity_scatter:
2482   case affinity_compact: {
2483     if (count > 0) {
2484       *out_compact = number[0];
2485     }
2486     if (count > 1) {
2487       *out_offset = number[1];
2488     }
2489   } break;
2490   case affinity_explicit: {
2491     if (*out_proclist == NULL) {
2492       KMP_WARNING(AffNoProcList, name);
2493       __kmp_affinity_type = affinity_none;
2494     }
2495     if (count > 0) {
2496       KMP_WARNING(AffNoParam, name, "explicit");
2497     }
2498   } break;
2499   case affinity_none: {
2500     if (count > 0) {
2501       KMP_WARNING(AffNoParam, name, "none");
2502     }
2503   } break;
2504   case affinity_disabled: {
2505     if (count > 0) {
2506       KMP_WARNING(AffNoParam, name, "disabled");
2507     }
2508   } break;
2509   case affinity_default: {
2510     if (count > 0) {
2511       KMP_WARNING(AffNoParam, name, "default");
2512     }
2513   } break;
2514   default: {
2515     KMP_ASSERT(0);
2516   }
2517   }
2518 } // __kmp_parse_affinity_env
2519 
__kmp_stg_parse_affinity(char const * name,char const * value,void * data)2520 static void __kmp_stg_parse_affinity(char const *name, char const *value,
2521                                      void *data) {
2522   kmp_setting_t **rivals = (kmp_setting_t **)data;
2523   int rc;
2524 
2525   rc = __kmp_stg_check_rivals(name, value, rivals);
2526   if (rc) {
2527     return;
2528   }
2529 
2530   __kmp_parse_affinity_env(name, value, &__kmp_affinity_type,
2531                            &__kmp_affinity_proclist, &__kmp_affinity_verbose,
2532                            &__kmp_affinity_warnings,
2533                            &__kmp_affinity_respect_mask, &__kmp_affinity_gran,
2534                            &__kmp_affinity_gran_levels, &__kmp_affinity_dups,
2535                            &__kmp_affinity_compact, &__kmp_affinity_offset);
2536 
2537 } // __kmp_stg_parse_affinity
2538 
__kmp_stg_print_affinity(kmp_str_buf_t * buffer,char const * name,void * data)2539 static void __kmp_stg_print_affinity(kmp_str_buf_t *buffer, char const *name,
2540                                      void *data) {
2541   if (__kmp_env_format) {
2542     KMP_STR_BUF_PRINT_NAME_EX(name);
2543   } else {
2544     __kmp_str_buf_print(buffer, "   %s='", name);
2545   }
2546   if (__kmp_affinity_verbose) {
2547     __kmp_str_buf_print(buffer, "%s,", "verbose");
2548   } else {
2549     __kmp_str_buf_print(buffer, "%s,", "noverbose");
2550   }
2551   if (__kmp_affinity_warnings) {
2552     __kmp_str_buf_print(buffer, "%s,", "warnings");
2553   } else {
2554     __kmp_str_buf_print(buffer, "%s,", "nowarnings");
2555   }
2556   if (KMP_AFFINITY_CAPABLE()) {
2557     if (__kmp_affinity_respect_mask) {
2558       __kmp_str_buf_print(buffer, "%s,", "respect");
2559     } else {
2560       __kmp_str_buf_print(buffer, "%s,", "norespect");
2561     }
2562     __kmp_str_buf_print(buffer, "granularity=%s,",
2563                         __kmp_hw_get_keyword(__kmp_affinity_gran, false));
2564   }
2565   if (!KMP_AFFINITY_CAPABLE()) {
2566     __kmp_str_buf_print(buffer, "%s", "disabled");
2567   } else
2568     switch (__kmp_affinity_type) {
2569     case affinity_none:
2570       __kmp_str_buf_print(buffer, "%s", "none");
2571       break;
2572     case affinity_physical:
2573       __kmp_str_buf_print(buffer, "%s,%d", "physical", __kmp_affinity_offset);
2574       break;
2575     case affinity_logical:
2576       __kmp_str_buf_print(buffer, "%s,%d", "logical", __kmp_affinity_offset);
2577       break;
2578     case affinity_compact:
2579       __kmp_str_buf_print(buffer, "%s,%d,%d", "compact", __kmp_affinity_compact,
2580                           __kmp_affinity_offset);
2581       break;
2582     case affinity_scatter:
2583       __kmp_str_buf_print(buffer, "%s,%d,%d", "scatter", __kmp_affinity_compact,
2584                           __kmp_affinity_offset);
2585       break;
2586     case affinity_explicit:
2587       __kmp_str_buf_print(buffer, "%s=[%s],%s", "proclist",
2588                           __kmp_affinity_proclist, "explicit");
2589       break;
2590     case affinity_balanced:
2591       __kmp_str_buf_print(buffer, "%s,%d,%d", "balanced",
2592                           __kmp_affinity_compact, __kmp_affinity_offset);
2593       break;
2594     case affinity_disabled:
2595       __kmp_str_buf_print(buffer, "%s", "disabled");
2596       break;
2597     case affinity_default:
2598       __kmp_str_buf_print(buffer, "%s", "default");
2599       break;
2600     default:
2601       __kmp_str_buf_print(buffer, "%s", "<unknown>");
2602       break;
2603     }
2604   __kmp_str_buf_print(buffer, "'\n");
2605 } //__kmp_stg_print_affinity
2606 
2607 #ifdef KMP_GOMP_COMPAT
2608 
__kmp_stg_parse_gomp_cpu_affinity(char const * name,char const * value,void * data)2609 static void __kmp_stg_parse_gomp_cpu_affinity(char const *name,
2610                                               char const *value, void *data) {
2611   const char *next = NULL;
2612   char *temp_proclist;
2613   kmp_setting_t **rivals = (kmp_setting_t **)data;
2614   int rc;
2615 
2616   rc = __kmp_stg_check_rivals(name, value, rivals);
2617   if (rc) {
2618     return;
2619   }
2620 
2621   if (TCR_4(__kmp_init_middle)) {
2622     KMP_WARNING(EnvMiddleWarn, name);
2623     __kmp_env_toPrint(name, 0);
2624     return;
2625   }
2626 
2627   __kmp_env_toPrint(name, 1);
2628 
2629   if (__kmp_parse_affinity_proc_id_list(name, value, &next, &temp_proclist)) {
2630     SKIP_WS(next);
2631     if (*next == '\0') {
2632       // GOMP_CPU_AFFINITY => granularity=fine,explicit,proclist=...
2633       __kmp_affinity_proclist = temp_proclist;
2634       __kmp_affinity_type = affinity_explicit;
2635       __kmp_affinity_gran = KMP_HW_THREAD;
2636       __kmp_nested_proc_bind.bind_types[0] = proc_bind_intel;
2637     } else {
2638       KMP_WARNING(AffSyntaxError, name);
2639       if (temp_proclist != NULL) {
2640         KMP_INTERNAL_FREE((void *)temp_proclist);
2641       }
2642     }
2643   } else {
2644     // Warning already emitted
2645     __kmp_affinity_type = affinity_none;
2646     __kmp_nested_proc_bind.bind_types[0] = proc_bind_false;
2647   }
2648 } // __kmp_stg_parse_gomp_cpu_affinity
2649 
2650 #endif /* KMP_GOMP_COMPAT */
2651 
2652 /*-----------------------------------------------------------------------------
2653 The OMP_PLACES proc id list parser. Here is the grammar:
2654 
2655 place_list := place
2656 place_list := place , place_list
2657 place := num
2658 place := place : num
2659 place := place : num : signed
2660 place := { subplacelist }
2661 place := ! place                  // (lowest priority)
2662 subplace_list := subplace
2663 subplace_list := subplace , subplace_list
2664 subplace := num
2665 subplace := num : num
2666 subplace := num : num : signed
2667 signed := num
2668 signed := + signed
2669 signed := - signed
2670 -----------------------------------------------------------------------------*/
2671 
2672 // Warning to issue for syntax error during parsing of OMP_PLACES
__kmp_omp_places_syntax_warn(const char * var)2673 static inline void __kmp_omp_places_syntax_warn(const char *var) {
2674   KMP_WARNING(SyntaxErrorUsing, var, "\"cores\"");
2675 }
2676 
__kmp_parse_subplace_list(const char * var,const char ** scan)2677 static int __kmp_parse_subplace_list(const char *var, const char **scan) {
2678   const char *next;
2679 
2680   for (;;) {
2681     int start, count, stride;
2682 
2683     //
2684     // Read in the starting proc id
2685     //
2686     SKIP_WS(*scan);
2687     if ((**scan < '0') || (**scan > '9')) {
2688       __kmp_omp_places_syntax_warn(var);
2689       return FALSE;
2690     }
2691     next = *scan;
2692     SKIP_DIGITS(next);
2693     start = __kmp_str_to_int(*scan, *next);
2694     KMP_ASSERT(start >= 0);
2695     *scan = next;
2696 
2697     // valid follow sets are ',' ':' and '}'
2698     SKIP_WS(*scan);
2699     if (**scan == '}') {
2700       break;
2701     }
2702     if (**scan == ',') {
2703       (*scan)++; // skip ','
2704       continue;
2705     }
2706     if (**scan != ':') {
2707       __kmp_omp_places_syntax_warn(var);
2708       return FALSE;
2709     }
2710     (*scan)++; // skip ':'
2711 
2712     // Read count parameter
2713     SKIP_WS(*scan);
2714     if ((**scan < '0') || (**scan > '9')) {
2715       __kmp_omp_places_syntax_warn(var);
2716       return FALSE;
2717     }
2718     next = *scan;
2719     SKIP_DIGITS(next);
2720     count = __kmp_str_to_int(*scan, *next);
2721     KMP_ASSERT(count >= 0);
2722     *scan = next;
2723 
2724     // valid follow sets are ',' ':' and '}'
2725     SKIP_WS(*scan);
2726     if (**scan == '}') {
2727       break;
2728     }
2729     if (**scan == ',') {
2730       (*scan)++; // skip ','
2731       continue;
2732     }
2733     if (**scan != ':') {
2734       __kmp_omp_places_syntax_warn(var);
2735       return FALSE;
2736     }
2737     (*scan)++; // skip ':'
2738 
2739     // Read stride parameter
2740     int sign = +1;
2741     for (;;) {
2742       SKIP_WS(*scan);
2743       if (**scan == '+') {
2744         (*scan)++; // skip '+'
2745         continue;
2746       }
2747       if (**scan == '-') {
2748         sign *= -1;
2749         (*scan)++; // skip '-'
2750         continue;
2751       }
2752       break;
2753     }
2754     SKIP_WS(*scan);
2755     if ((**scan < '0') || (**scan > '9')) {
2756       __kmp_omp_places_syntax_warn(var);
2757       return FALSE;
2758     }
2759     next = *scan;
2760     SKIP_DIGITS(next);
2761     stride = __kmp_str_to_int(*scan, *next);
2762     KMP_ASSERT(stride >= 0);
2763     *scan = next;
2764     stride *= sign;
2765 
2766     // valid follow sets are ',' and '}'
2767     SKIP_WS(*scan);
2768     if (**scan == '}') {
2769       break;
2770     }
2771     if (**scan == ',') {
2772       (*scan)++; // skip ','
2773       continue;
2774     }
2775 
2776     __kmp_omp_places_syntax_warn(var);
2777     return FALSE;
2778   }
2779   return TRUE;
2780 }
2781 
__kmp_parse_place(const char * var,const char ** scan)2782 static int __kmp_parse_place(const char *var, const char **scan) {
2783   const char *next;
2784 
2785   // valid follow sets are '{' '!' and num
2786   SKIP_WS(*scan);
2787   if (**scan == '{') {
2788     (*scan)++; // skip '{'
2789     if (!__kmp_parse_subplace_list(var, scan)) {
2790       return FALSE;
2791     }
2792     if (**scan != '}') {
2793       __kmp_omp_places_syntax_warn(var);
2794       return FALSE;
2795     }
2796     (*scan)++; // skip '}'
2797   } else if (**scan == '!') {
2798     (*scan)++; // skip '!'
2799     return __kmp_parse_place(var, scan); //'!' has lower precedence than ':'
2800   } else if ((**scan >= '0') && (**scan <= '9')) {
2801     next = *scan;
2802     SKIP_DIGITS(next);
2803     int proc = __kmp_str_to_int(*scan, *next);
2804     KMP_ASSERT(proc >= 0);
2805     *scan = next;
2806   } else {
2807     __kmp_omp_places_syntax_warn(var);
2808     return FALSE;
2809   }
2810   return TRUE;
2811 }
2812 
__kmp_parse_place_list(const char * var,const char * env,char ** place_list)2813 static int __kmp_parse_place_list(const char *var, const char *env,
2814                                   char **place_list) {
2815   const char *scan = env;
2816   const char *next = scan;
2817 
2818   for (;;) {
2819     int count, stride;
2820 
2821     if (!__kmp_parse_place(var, &scan)) {
2822       return FALSE;
2823     }
2824 
2825     // valid follow sets are ',' ':' and EOL
2826     SKIP_WS(scan);
2827     if (*scan == '\0') {
2828       break;
2829     }
2830     if (*scan == ',') {
2831       scan++; // skip ','
2832       continue;
2833     }
2834     if (*scan != ':') {
2835       __kmp_omp_places_syntax_warn(var);
2836       return FALSE;
2837     }
2838     scan++; // skip ':'
2839 
2840     // Read count parameter
2841     SKIP_WS(scan);
2842     if ((*scan < '0') || (*scan > '9')) {
2843       __kmp_omp_places_syntax_warn(var);
2844       return FALSE;
2845     }
2846     next = scan;
2847     SKIP_DIGITS(next);
2848     count = __kmp_str_to_int(scan, *next);
2849     KMP_ASSERT(count >= 0);
2850     scan = next;
2851 
2852     // valid follow sets are ',' ':' and EOL
2853     SKIP_WS(scan);
2854     if (*scan == '\0') {
2855       break;
2856     }
2857     if (*scan == ',') {
2858       scan++; // skip ','
2859       continue;
2860     }
2861     if (*scan != ':') {
2862       __kmp_omp_places_syntax_warn(var);
2863       return FALSE;
2864     }
2865     scan++; // skip ':'
2866 
2867     // Read stride parameter
2868     int sign = +1;
2869     for (;;) {
2870       SKIP_WS(scan);
2871       if (*scan == '+') {
2872         scan++; // skip '+'
2873         continue;
2874       }
2875       if (*scan == '-') {
2876         sign *= -1;
2877         scan++; // skip '-'
2878         continue;
2879       }
2880       break;
2881     }
2882     SKIP_WS(scan);
2883     if ((*scan < '0') || (*scan > '9')) {
2884       __kmp_omp_places_syntax_warn(var);
2885       return FALSE;
2886     }
2887     next = scan;
2888     SKIP_DIGITS(next);
2889     stride = __kmp_str_to_int(scan, *next);
2890     KMP_ASSERT(stride >= 0);
2891     scan = next;
2892     stride *= sign;
2893 
2894     // valid follow sets are ',' and EOL
2895     SKIP_WS(scan);
2896     if (*scan == '\0') {
2897       break;
2898     }
2899     if (*scan == ',') {
2900       scan++; // skip ','
2901       continue;
2902     }
2903 
2904     __kmp_omp_places_syntax_warn(var);
2905     return FALSE;
2906   }
2907 
2908   {
2909     ptrdiff_t len = scan - env;
2910     char *retlist = (char *)__kmp_allocate((len + 1) * sizeof(char));
2911     KMP_MEMCPY_S(retlist, (len + 1) * sizeof(char), env, len * sizeof(char));
2912     retlist[len] = '\0';
2913     *place_list = retlist;
2914   }
2915   return TRUE;
2916 }
2917 
__kmp_stg_parse_places(char const * name,char const * value,void * data)2918 static void __kmp_stg_parse_places(char const *name, char const *value,
2919                                    void *data) {
2920   struct kmp_place_t {
2921     const char *name;
2922     kmp_hw_t type;
2923   };
2924   int count;
2925   bool set = false;
2926   const char *scan = value;
2927   const char *next = scan;
2928   const char *kind = "\"threads\"";
2929   kmp_place_t std_places[] = {{"threads", KMP_HW_THREAD},
2930                               {"cores", KMP_HW_CORE},
2931                               {"numa_domains", KMP_HW_NUMA},
2932                               {"ll_caches", KMP_HW_LLC},
2933                               {"sockets", KMP_HW_SOCKET}};
2934   kmp_setting_t **rivals = (kmp_setting_t **)data;
2935   int rc;
2936 
2937   rc = __kmp_stg_check_rivals(name, value, rivals);
2938   if (rc) {
2939     return;
2940   }
2941 
2942   // Standard choices
2943   for (size_t i = 0; i < sizeof(std_places) / sizeof(std_places[0]); ++i) {
2944     const kmp_place_t &place = std_places[i];
2945     if (__kmp_match_str(place.name, scan, &next)) {
2946       scan = next;
2947       __kmp_affinity_type = affinity_compact;
2948       __kmp_affinity_gran = place.type;
2949       __kmp_affinity_dups = FALSE;
2950       set = true;
2951       break;
2952     }
2953   }
2954   // Implementation choices for OMP_PLACES based on internal types
2955   if (!set) {
2956     KMP_FOREACH_HW_TYPE(type) {
2957       const char *name = __kmp_hw_get_keyword(type, true);
2958       if (__kmp_match_str("unknowns", scan, &next))
2959         continue;
2960       if (__kmp_match_str(name, scan, &next)) {
2961         scan = next;
2962         __kmp_affinity_type = affinity_compact;
2963         __kmp_affinity_gran = type;
2964         __kmp_affinity_dups = FALSE;
2965         set = true;
2966         break;
2967       }
2968     }
2969   }
2970   if (!set) {
2971     if (__kmp_affinity_proclist != NULL) {
2972       KMP_INTERNAL_FREE((void *)__kmp_affinity_proclist);
2973       __kmp_affinity_proclist = NULL;
2974     }
2975     if (__kmp_parse_place_list(name, value, &__kmp_affinity_proclist)) {
2976       __kmp_affinity_type = affinity_explicit;
2977       __kmp_affinity_gran = KMP_HW_THREAD;
2978       __kmp_affinity_dups = FALSE;
2979     } else {
2980       // Syntax error fallback
2981       __kmp_affinity_type = affinity_compact;
2982       __kmp_affinity_gran = KMP_HW_CORE;
2983       __kmp_affinity_dups = FALSE;
2984     }
2985     if (__kmp_nested_proc_bind.bind_types[0] == proc_bind_default) {
2986       __kmp_nested_proc_bind.bind_types[0] = proc_bind_true;
2987     }
2988     return;
2989   }
2990   if (__kmp_affinity_gran != KMP_HW_UNKNOWN) {
2991     kind = __kmp_hw_get_keyword(__kmp_affinity_gran);
2992   }
2993 
2994   if (__kmp_nested_proc_bind.bind_types[0] == proc_bind_default) {
2995     __kmp_nested_proc_bind.bind_types[0] = proc_bind_true;
2996   }
2997 
2998   SKIP_WS(scan);
2999   if (*scan == '\0') {
3000     return;
3001   }
3002 
3003   // Parse option count parameter in parentheses
3004   if (*scan != '(') {
3005     KMP_WARNING(SyntaxErrorUsing, name, kind);
3006     return;
3007   }
3008   scan++; // skip '('
3009 
3010   SKIP_WS(scan);
3011   next = scan;
3012   SKIP_DIGITS(next);
3013   count = __kmp_str_to_int(scan, *next);
3014   KMP_ASSERT(count >= 0);
3015   scan = next;
3016 
3017   SKIP_WS(scan);
3018   if (*scan != ')') {
3019     KMP_WARNING(SyntaxErrorUsing, name, kind);
3020     return;
3021   }
3022   scan++; // skip ')'
3023 
3024   SKIP_WS(scan);
3025   if (*scan != '\0') {
3026     KMP_WARNING(ParseExtraCharsWarn, name, scan);
3027   }
3028   __kmp_affinity_num_places = count;
3029 }
3030 
__kmp_stg_print_places(kmp_str_buf_t * buffer,char const * name,void * data)3031 static void __kmp_stg_print_places(kmp_str_buf_t *buffer, char const *name,
3032                                    void *data) {
3033   if (__kmp_env_format) {
3034     KMP_STR_BUF_PRINT_NAME;
3035   } else {
3036     __kmp_str_buf_print(buffer, "   %s", name);
3037   }
3038   if ((__kmp_nested_proc_bind.used == 0) ||
3039       (__kmp_nested_proc_bind.bind_types == NULL) ||
3040       (__kmp_nested_proc_bind.bind_types[0] == proc_bind_false)) {
3041     __kmp_str_buf_print(buffer, ": %s\n", KMP_I18N_STR(NotDefined));
3042   } else if (__kmp_affinity_type == affinity_explicit) {
3043     if (__kmp_affinity_proclist != NULL) {
3044       __kmp_str_buf_print(buffer, "='%s'\n", __kmp_affinity_proclist);
3045     } else {
3046       __kmp_str_buf_print(buffer, ": %s\n", KMP_I18N_STR(NotDefined));
3047     }
3048   } else if (__kmp_affinity_type == affinity_compact) {
3049     int num;
3050     if (__kmp_affinity_num_masks > 0) {
3051       num = __kmp_affinity_num_masks;
3052     } else if (__kmp_affinity_num_places > 0) {
3053       num = __kmp_affinity_num_places;
3054     } else {
3055       num = 0;
3056     }
3057     if (__kmp_affinity_gran != KMP_HW_UNKNOWN) {
3058       const char *name = __kmp_hw_get_keyword(__kmp_affinity_gran, true);
3059       if (num > 0) {
3060         __kmp_str_buf_print(buffer, "='%s(%d)'\n", name, num);
3061       } else {
3062         __kmp_str_buf_print(buffer, "='%s'\n", name);
3063       }
3064     } else {
3065       __kmp_str_buf_print(buffer, ": %s\n", KMP_I18N_STR(NotDefined));
3066     }
3067   } else {
3068     __kmp_str_buf_print(buffer, ": %s\n", KMP_I18N_STR(NotDefined));
3069   }
3070 }
3071 
__kmp_stg_parse_topology_method(char const * name,char const * value,void * data)3072 static void __kmp_stg_parse_topology_method(char const *name, char const *value,
3073                                             void *data) {
3074   if (__kmp_str_match("all", 1, value)) {
3075     __kmp_affinity_top_method = affinity_top_method_all;
3076   }
3077 #if KMP_USE_HWLOC
3078   else if (__kmp_str_match("hwloc", 1, value)) {
3079     __kmp_affinity_top_method = affinity_top_method_hwloc;
3080   }
3081 #endif
3082 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
3083   else if (__kmp_str_match("cpuid_leaf31", 12, value) ||
3084            __kmp_str_match("cpuid 1f", 8, value) ||
3085            __kmp_str_match("cpuid 31", 8, value) ||
3086            __kmp_str_match("cpuid1f", 7, value) ||
3087            __kmp_str_match("cpuid31", 7, value) ||
3088            __kmp_str_match("leaf 1f", 7, value) ||
3089            __kmp_str_match("leaf 31", 7, value) ||
3090            __kmp_str_match("leaf1f", 6, value) ||
3091            __kmp_str_match("leaf31", 6, value)) {
3092     __kmp_affinity_top_method = affinity_top_method_x2apicid_1f;
3093   } else if (__kmp_str_match("x2apic id", 9, value) ||
3094              __kmp_str_match("x2apic_id", 9, value) ||
3095              __kmp_str_match("x2apic-id", 9, value) ||
3096              __kmp_str_match("x2apicid", 8, value) ||
3097              __kmp_str_match("cpuid leaf 11", 13, value) ||
3098              __kmp_str_match("cpuid_leaf_11", 13, value) ||
3099              __kmp_str_match("cpuid-leaf-11", 13, value) ||
3100              __kmp_str_match("cpuid leaf11", 12, value) ||
3101              __kmp_str_match("cpuid_leaf11", 12, value) ||
3102              __kmp_str_match("cpuid-leaf11", 12, value) ||
3103              __kmp_str_match("cpuidleaf 11", 12, value) ||
3104              __kmp_str_match("cpuidleaf_11", 12, value) ||
3105              __kmp_str_match("cpuidleaf-11", 12, value) ||
3106              __kmp_str_match("cpuidleaf11", 11, value) ||
3107              __kmp_str_match("cpuid 11", 8, value) ||
3108              __kmp_str_match("cpuid_11", 8, value) ||
3109              __kmp_str_match("cpuid-11", 8, value) ||
3110              __kmp_str_match("cpuid11", 7, value) ||
3111              __kmp_str_match("leaf 11", 7, value) ||
3112              __kmp_str_match("leaf_11", 7, value) ||
3113              __kmp_str_match("leaf-11", 7, value) ||
3114              __kmp_str_match("leaf11", 6, value)) {
3115     __kmp_affinity_top_method = affinity_top_method_x2apicid;
3116   } else if (__kmp_str_match("apic id", 7, value) ||
3117              __kmp_str_match("apic_id", 7, value) ||
3118              __kmp_str_match("apic-id", 7, value) ||
3119              __kmp_str_match("apicid", 6, value) ||
3120              __kmp_str_match("cpuid leaf 4", 12, value) ||
3121              __kmp_str_match("cpuid_leaf_4", 12, value) ||
3122              __kmp_str_match("cpuid-leaf-4", 12, value) ||
3123              __kmp_str_match("cpuid leaf4", 11, value) ||
3124              __kmp_str_match("cpuid_leaf4", 11, value) ||
3125              __kmp_str_match("cpuid-leaf4", 11, value) ||
3126              __kmp_str_match("cpuidleaf 4", 11, value) ||
3127              __kmp_str_match("cpuidleaf_4", 11, value) ||
3128              __kmp_str_match("cpuidleaf-4", 11, value) ||
3129              __kmp_str_match("cpuidleaf4", 10, value) ||
3130              __kmp_str_match("cpuid 4", 7, value) ||
3131              __kmp_str_match("cpuid_4", 7, value) ||
3132              __kmp_str_match("cpuid-4", 7, value) ||
3133              __kmp_str_match("cpuid4", 6, value) ||
3134              __kmp_str_match("leaf 4", 6, value) ||
3135              __kmp_str_match("leaf_4", 6, value) ||
3136              __kmp_str_match("leaf-4", 6, value) ||
3137              __kmp_str_match("leaf4", 5, value)) {
3138     __kmp_affinity_top_method = affinity_top_method_apicid;
3139   }
3140 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
3141   else if (__kmp_str_match("/proc/cpuinfo", 2, value) ||
3142            __kmp_str_match("cpuinfo", 5, value)) {
3143     __kmp_affinity_top_method = affinity_top_method_cpuinfo;
3144   }
3145 #if KMP_GROUP_AFFINITY
3146   else if (__kmp_str_match("group", 1, value)) {
3147     __kmp_affinity_top_method = affinity_top_method_group;
3148   }
3149 #endif /* KMP_GROUP_AFFINITY */
3150   else if (__kmp_str_match("flat", 1, value)) {
3151     __kmp_affinity_top_method = affinity_top_method_flat;
3152   } else {
3153     KMP_WARNING(StgInvalidValue, name, value);
3154   }
3155 } // __kmp_stg_parse_topology_method
3156 
__kmp_stg_print_topology_method(kmp_str_buf_t * buffer,char const * name,void * data)3157 static void __kmp_stg_print_topology_method(kmp_str_buf_t *buffer,
3158                                             char const *name, void *data) {
3159   char const *value = NULL;
3160 
3161   switch (__kmp_affinity_top_method) {
3162   case affinity_top_method_default:
3163     value = "default";
3164     break;
3165 
3166   case affinity_top_method_all:
3167     value = "all";
3168     break;
3169 
3170 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
3171   case affinity_top_method_x2apicid_1f:
3172     value = "x2APIC id leaf 0x1f";
3173     break;
3174 
3175   case affinity_top_method_x2apicid:
3176     value = "x2APIC id leaf 0xb";
3177     break;
3178 
3179   case affinity_top_method_apicid:
3180     value = "APIC id";
3181     break;
3182 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
3183 
3184 #if KMP_USE_HWLOC
3185   case affinity_top_method_hwloc:
3186     value = "hwloc";
3187     break;
3188 #endif
3189 
3190   case affinity_top_method_cpuinfo:
3191     value = "cpuinfo";
3192     break;
3193 
3194 #if KMP_GROUP_AFFINITY
3195   case affinity_top_method_group:
3196     value = "group";
3197     break;
3198 #endif /* KMP_GROUP_AFFINITY */
3199 
3200   case affinity_top_method_flat:
3201     value = "flat";
3202     break;
3203   }
3204 
3205   if (value != NULL) {
3206     __kmp_stg_print_str(buffer, name, value);
3207   }
3208 } // __kmp_stg_print_topology_method
3209 
3210 #endif /* KMP_AFFINITY_SUPPORTED */
3211 
3212 // OMP_PROC_BIND / bind-var is functional on all 4.0 builds, including OS X*
3213 // OMP_PLACES / place-partition-var is not.
__kmp_stg_parse_proc_bind(char const * name,char const * value,void * data)3214 static void __kmp_stg_parse_proc_bind(char const *name, char const *value,
3215                                       void *data) {
3216   kmp_setting_t **rivals = (kmp_setting_t **)data;
3217   int rc;
3218 
3219   rc = __kmp_stg_check_rivals(name, value, rivals);
3220   if (rc) {
3221     return;
3222   }
3223 
3224   // In OMP 4.0 OMP_PROC_BIND is a vector of proc_bind types.
3225   KMP_DEBUG_ASSERT((__kmp_nested_proc_bind.bind_types != NULL) &&
3226                    (__kmp_nested_proc_bind.used > 0));
3227 
3228   const char *buf = value;
3229   const char *next;
3230   int num;
3231   SKIP_WS(buf);
3232   if ((*buf >= '0') && (*buf <= '9')) {
3233     next = buf;
3234     SKIP_DIGITS(next);
3235     num = __kmp_str_to_int(buf, *next);
3236     KMP_ASSERT(num >= 0);
3237     buf = next;
3238     SKIP_WS(buf);
3239   } else {
3240     num = -1;
3241   }
3242 
3243   next = buf;
3244   if (__kmp_match_str("disabled", buf, &next)) {
3245     buf = next;
3246     SKIP_WS(buf);
3247 #if KMP_AFFINITY_SUPPORTED
3248     __kmp_affinity_type = affinity_disabled;
3249 #endif /* KMP_AFFINITY_SUPPORTED */
3250     __kmp_nested_proc_bind.used = 1;
3251     __kmp_nested_proc_bind.bind_types[0] = proc_bind_false;
3252   } else if ((num == (int)proc_bind_false) ||
3253              __kmp_match_str("false", buf, &next)) {
3254     buf = next;
3255     SKIP_WS(buf);
3256 #if KMP_AFFINITY_SUPPORTED
3257     __kmp_affinity_type = affinity_none;
3258 #endif /* KMP_AFFINITY_SUPPORTED */
3259     __kmp_nested_proc_bind.used = 1;
3260     __kmp_nested_proc_bind.bind_types[0] = proc_bind_false;
3261   } else if ((num == (int)proc_bind_true) ||
3262              __kmp_match_str("true", buf, &next)) {
3263     buf = next;
3264     SKIP_WS(buf);
3265     __kmp_nested_proc_bind.used = 1;
3266     __kmp_nested_proc_bind.bind_types[0] = proc_bind_true;
3267   } else {
3268     // Count the number of values in the env var string
3269     const char *scan;
3270     int nelem = 1;
3271     for (scan = buf; *scan != '\0'; scan++) {
3272       if (*scan == ',') {
3273         nelem++;
3274       }
3275     }
3276 
3277     // Create / expand the nested proc_bind array as needed
3278     if (__kmp_nested_proc_bind.size < nelem) {
3279       __kmp_nested_proc_bind.bind_types =
3280           (kmp_proc_bind_t *)KMP_INTERNAL_REALLOC(
3281               __kmp_nested_proc_bind.bind_types,
3282               sizeof(kmp_proc_bind_t) * nelem);
3283       if (__kmp_nested_proc_bind.bind_types == NULL) {
3284         KMP_FATAL(MemoryAllocFailed);
3285       }
3286       __kmp_nested_proc_bind.size = nelem;
3287     }
3288     __kmp_nested_proc_bind.used = nelem;
3289 
3290     if (nelem > 1 && !__kmp_dflt_max_active_levels_set)
3291       __kmp_dflt_max_active_levels = KMP_MAX_ACTIVE_LEVELS_LIMIT;
3292 
3293     // Save values in the nested proc_bind array
3294     int i = 0;
3295     for (;;) {
3296       enum kmp_proc_bind_t bind;
3297 
3298       if ((num == (int)proc_bind_primary) ||
3299           __kmp_match_str("master", buf, &next) ||
3300           __kmp_match_str("primary", buf, &next)) {
3301         buf = next;
3302         SKIP_WS(buf);
3303         bind = proc_bind_primary;
3304       } else if ((num == (int)proc_bind_close) ||
3305                  __kmp_match_str("close", buf, &next)) {
3306         buf = next;
3307         SKIP_WS(buf);
3308         bind = proc_bind_close;
3309       } else if ((num == (int)proc_bind_spread) ||
3310                  __kmp_match_str("spread", buf, &next)) {
3311         buf = next;
3312         SKIP_WS(buf);
3313         bind = proc_bind_spread;
3314       } else {
3315         KMP_WARNING(StgInvalidValue, name, value);
3316         __kmp_nested_proc_bind.bind_types[0] = proc_bind_false;
3317         __kmp_nested_proc_bind.used = 1;
3318         return;
3319       }
3320 
3321       __kmp_nested_proc_bind.bind_types[i++] = bind;
3322       if (i >= nelem) {
3323         break;
3324       }
3325       KMP_DEBUG_ASSERT(*buf == ',');
3326       buf++;
3327       SKIP_WS(buf);
3328 
3329       // Read next value if it was specified as an integer
3330       if ((*buf >= '0') && (*buf <= '9')) {
3331         next = buf;
3332         SKIP_DIGITS(next);
3333         num = __kmp_str_to_int(buf, *next);
3334         KMP_ASSERT(num >= 0);
3335         buf = next;
3336         SKIP_WS(buf);
3337       } else {
3338         num = -1;
3339       }
3340     }
3341     SKIP_WS(buf);
3342   }
3343   if (*buf != '\0') {
3344     KMP_WARNING(ParseExtraCharsWarn, name, buf);
3345   }
3346 }
3347 
__kmp_stg_print_proc_bind(kmp_str_buf_t * buffer,char const * name,void * data)3348 static void __kmp_stg_print_proc_bind(kmp_str_buf_t *buffer, char const *name,
3349                                       void *data) {
3350   int nelem = __kmp_nested_proc_bind.used;
3351   if (__kmp_env_format) {
3352     KMP_STR_BUF_PRINT_NAME;
3353   } else {
3354     __kmp_str_buf_print(buffer, "   %s", name);
3355   }
3356   if (nelem == 0) {
3357     __kmp_str_buf_print(buffer, ": %s\n", KMP_I18N_STR(NotDefined));
3358   } else {
3359     int i;
3360     __kmp_str_buf_print(buffer, "='", name);
3361     for (i = 0; i < nelem; i++) {
3362       switch (__kmp_nested_proc_bind.bind_types[i]) {
3363       case proc_bind_false:
3364         __kmp_str_buf_print(buffer, "false");
3365         break;
3366 
3367       case proc_bind_true:
3368         __kmp_str_buf_print(buffer, "true");
3369         break;
3370 
3371       case proc_bind_primary:
3372         __kmp_str_buf_print(buffer, "primary");
3373         break;
3374 
3375       case proc_bind_close:
3376         __kmp_str_buf_print(buffer, "close");
3377         break;
3378 
3379       case proc_bind_spread:
3380         __kmp_str_buf_print(buffer, "spread");
3381         break;
3382 
3383       case proc_bind_intel:
3384         __kmp_str_buf_print(buffer, "intel");
3385         break;
3386 
3387       case proc_bind_default:
3388         __kmp_str_buf_print(buffer, "default");
3389         break;
3390       }
3391       if (i < nelem - 1) {
3392         __kmp_str_buf_print(buffer, ",");
3393       }
3394     }
3395     __kmp_str_buf_print(buffer, "'\n");
3396   }
3397 }
3398 
__kmp_stg_parse_display_affinity(char const * name,char const * value,void * data)3399 static void __kmp_stg_parse_display_affinity(char const *name,
3400                                              char const *value, void *data) {
3401   __kmp_stg_parse_bool(name, value, &__kmp_display_affinity);
3402 }
__kmp_stg_print_display_affinity(kmp_str_buf_t * buffer,char const * name,void * data)3403 static void __kmp_stg_print_display_affinity(kmp_str_buf_t *buffer,
3404                                              char const *name, void *data) {
3405   __kmp_stg_print_bool(buffer, name, __kmp_display_affinity);
3406 }
__kmp_stg_parse_affinity_format(char const * name,char const * value,void * data)3407 static void __kmp_stg_parse_affinity_format(char const *name, char const *value,
3408                                             void *data) {
3409   size_t length = KMP_STRLEN(value);
3410   __kmp_strncpy_truncate(__kmp_affinity_format, KMP_AFFINITY_FORMAT_SIZE, value,
3411                          length);
3412 }
__kmp_stg_print_affinity_format(kmp_str_buf_t * buffer,char const * name,void * data)3413 static void __kmp_stg_print_affinity_format(kmp_str_buf_t *buffer,
3414                                             char const *name, void *data) {
3415   if (__kmp_env_format) {
3416     KMP_STR_BUF_PRINT_NAME_EX(name);
3417   } else {
3418     __kmp_str_buf_print(buffer, "   %s='", name);
3419   }
3420   __kmp_str_buf_print(buffer, "%s'\n", __kmp_affinity_format);
3421 }
3422 
3423 /*-----------------------------------------------------------------------------
3424 OMP_ALLOCATOR sets default allocator. Here is the grammar:
3425 
3426 <allocator>        |= <predef-allocator> | <predef-mem-space> |
3427                       <predef-mem-space>:<traits>
3428 <traits>           |= <trait>=<value> | <trait>=<value>,<traits>
3429 <predef-allocator> |= omp_default_mem_alloc | omp_large_cap_mem_alloc |
3430                       omp_const_mem_alloc | omp_high_bw_mem_alloc |
3431                       omp_low_lat_mem_alloc | omp_cgroup_mem_alloc |
3432                       omp_pteam_mem_alloc | omp_thread_mem_alloc
3433 <predef-mem-space> |= omp_default_mem_space | omp_large_cap_mem_space |
3434                       omp_const_mem_space | omp_high_bw_mem_space |
3435                       omp_low_lat_mem_space
3436 <trait>            |= sync_hint | alignment | access | pool_size | fallback |
3437                       fb_data | pinned | partition
3438 <value>            |= one of the allowed values of trait |
3439                       non-negative integer | <predef-allocator>
3440 -----------------------------------------------------------------------------*/
3441 
__kmp_stg_parse_allocator(char const * name,char const * value,void * data)3442 static void __kmp_stg_parse_allocator(char const *name, char const *value,
3443                                       void *data) {
3444   const char *buf = value;
3445   const char *next, *scan, *start;
3446   char *key;
3447   omp_allocator_handle_t al;
3448   omp_memspace_handle_t ms = omp_default_mem_space;
3449   bool is_memspace = false;
3450   int ntraits = 0, count = 0;
3451 
3452   SKIP_WS(buf);
3453   next = buf;
3454   const char *delim = strchr(buf, ':');
3455   const char *predef_mem_space = strstr(buf, "mem_space");
3456 
3457   bool is_memalloc = (!predef_mem_space && !delim) ? true : false;
3458 
3459   // Count the number of traits in the env var string
3460   if (delim) {
3461     ntraits = 1;
3462     for (scan = buf; *scan != '\0'; scan++) {
3463       if (*scan == ',')
3464         ntraits++;
3465     }
3466   }
3467   omp_alloctrait_t *traits =
3468       (omp_alloctrait_t *)KMP_ALLOCA(ntraits * sizeof(omp_alloctrait_t));
3469 
3470 // Helper macros
3471 #define IS_POWER_OF_TWO(n) (((n) & ((n)-1)) == 0)
3472 
3473 #define GET_NEXT(sentinel)                                                     \
3474   {                                                                            \
3475     SKIP_WS(next);                                                             \
3476     if (*next == sentinel)                                                     \
3477       next++;                                                                  \
3478     SKIP_WS(next);                                                             \
3479     scan = next;                                                               \
3480   }
3481 
3482 #define SKIP_PAIR(key)                                                         \
3483   {                                                                            \
3484     char const str_delimiter[] = {',', 0};                                     \
3485     char *value = __kmp_str_token(CCAST(char *, scan), str_delimiter,          \
3486                                   CCAST(char **, &next));                      \
3487     KMP_WARNING(StgInvalidValue, key, value);                                  \
3488     ntraits--;                                                                 \
3489     SKIP_WS(next);                                                             \
3490     scan = next;                                                               \
3491   }
3492 
3493 #define SET_KEY()                                                              \
3494   {                                                                            \
3495     char const str_delimiter[] = {'=', 0};                                     \
3496     key = __kmp_str_token(CCAST(char *, start), str_delimiter,                 \
3497                           CCAST(char **, &next));                              \
3498     scan = next;                                                               \
3499   }
3500 
3501   scan = next;
3502   while (*next != '\0') {
3503     if (is_memalloc ||
3504         __kmp_match_str("fb_data", scan, &next)) { // allocator check
3505       start = scan;
3506       GET_NEXT('=');
3507       // check HBW and LCAP first as the only non-default supported
3508       if (__kmp_match_str("omp_high_bw_mem_alloc", scan, &next)) {
3509         SKIP_WS(next);
3510         if (is_memalloc) {
3511           if (__kmp_memkind_available) {
3512             __kmp_def_allocator = omp_high_bw_mem_alloc;
3513             return;
3514           } else {
3515             KMP_WARNING(OmpNoAllocator, "omp_high_bw_mem_alloc");
3516           }
3517         } else {
3518           traits[count].key = omp_atk_fb_data;
3519           traits[count].value = RCAST(omp_uintptr_t, omp_high_bw_mem_alloc);
3520         }
3521       } else if (__kmp_match_str("omp_large_cap_mem_alloc", scan, &next)) {
3522         SKIP_WS(next);
3523         if (is_memalloc) {
3524           if (__kmp_memkind_available) {
3525             __kmp_def_allocator = omp_large_cap_mem_alloc;
3526             return;
3527           } else {
3528             KMP_WARNING(OmpNoAllocator, "omp_large_cap_mem_alloc");
3529           }
3530         } else {
3531           traits[count].key = omp_atk_fb_data;
3532           traits[count].value = RCAST(omp_uintptr_t, omp_large_cap_mem_alloc);
3533         }
3534       } else if (__kmp_match_str("omp_default_mem_alloc", scan, &next)) {
3535         // default requested
3536         SKIP_WS(next);
3537         if (!is_memalloc) {
3538           traits[count].key = omp_atk_fb_data;
3539           traits[count].value = RCAST(omp_uintptr_t, omp_default_mem_alloc);
3540         }
3541       } else if (__kmp_match_str("omp_const_mem_alloc", scan, &next)) {
3542         SKIP_WS(next);
3543         if (is_memalloc) {
3544           KMP_WARNING(OmpNoAllocator, "omp_const_mem_alloc");
3545         } else {
3546           traits[count].key = omp_atk_fb_data;
3547           traits[count].value = RCAST(omp_uintptr_t, omp_const_mem_alloc);
3548         }
3549       } else if (__kmp_match_str("omp_low_lat_mem_alloc", scan, &next)) {
3550         SKIP_WS(next);
3551         if (is_memalloc) {
3552           KMP_WARNING(OmpNoAllocator, "omp_low_lat_mem_alloc");
3553         } else {
3554           traits[count].key = omp_atk_fb_data;
3555           traits[count].value = RCAST(omp_uintptr_t, omp_low_lat_mem_alloc);
3556         }
3557       } else if (__kmp_match_str("omp_cgroup_mem_alloc", scan, &next)) {
3558         SKIP_WS(next);
3559         if (is_memalloc) {
3560           KMP_WARNING(OmpNoAllocator, "omp_cgroup_mem_alloc");
3561         } else {
3562           traits[count].key = omp_atk_fb_data;
3563           traits[count].value = RCAST(omp_uintptr_t, omp_cgroup_mem_alloc);
3564         }
3565       } else if (__kmp_match_str("omp_pteam_mem_alloc", scan, &next)) {
3566         SKIP_WS(next);
3567         if (is_memalloc) {
3568           KMP_WARNING(OmpNoAllocator, "omp_pteam_mem_alloc");
3569         } else {
3570           traits[count].key = omp_atk_fb_data;
3571           traits[count].value = RCAST(omp_uintptr_t, omp_pteam_mem_alloc);
3572         }
3573       } else if (__kmp_match_str("omp_thread_mem_alloc", scan, &next)) {
3574         SKIP_WS(next);
3575         if (is_memalloc) {
3576           KMP_WARNING(OmpNoAllocator, "omp_thread_mem_alloc");
3577         } else {
3578           traits[count].key = omp_atk_fb_data;
3579           traits[count].value = RCAST(omp_uintptr_t, omp_thread_mem_alloc);
3580         }
3581       } else {
3582         if (!is_memalloc) {
3583           SET_KEY();
3584           SKIP_PAIR(key);
3585           continue;
3586         }
3587       }
3588       if (is_memalloc) {
3589         __kmp_def_allocator = omp_default_mem_alloc;
3590         if (next == buf || *next != '\0') {
3591           // either no match or extra symbols present after the matched token
3592           KMP_WARNING(StgInvalidValue, name, value);
3593         }
3594         return;
3595       } else {
3596         ++count;
3597         if (count == ntraits)
3598           break;
3599         GET_NEXT(',');
3600       }
3601     } else { // memspace
3602       if (!is_memspace) {
3603         if (__kmp_match_str("omp_default_mem_space", scan, &next)) {
3604           SKIP_WS(next);
3605           ms = omp_default_mem_space;
3606         } else if (__kmp_match_str("omp_large_cap_mem_space", scan, &next)) {
3607           SKIP_WS(next);
3608           ms = omp_large_cap_mem_space;
3609         } else if (__kmp_match_str("omp_const_mem_space", scan, &next)) {
3610           SKIP_WS(next);
3611           ms = omp_const_mem_space;
3612         } else if (__kmp_match_str("omp_high_bw_mem_space", scan, &next)) {
3613           SKIP_WS(next);
3614           ms = omp_high_bw_mem_space;
3615         } else if (__kmp_match_str("omp_low_lat_mem_space", scan, &next)) {
3616           SKIP_WS(next);
3617           ms = omp_low_lat_mem_space;
3618         } else {
3619           __kmp_def_allocator = omp_default_mem_alloc;
3620           if (next == buf || *next != '\0') {
3621             // either no match or extra symbols present after the matched token
3622             KMP_WARNING(StgInvalidValue, name, value);
3623           }
3624           return;
3625         }
3626         is_memspace = true;
3627       }
3628       if (delim) { // traits
3629         GET_NEXT(':');
3630         start = scan;
3631         if (__kmp_match_str("sync_hint", scan, &next)) {
3632           GET_NEXT('=');
3633           traits[count].key = omp_atk_sync_hint;
3634           if (__kmp_match_str("contended", scan, &next)) {
3635             traits[count].value = omp_atv_contended;
3636           } else if (__kmp_match_str("uncontended", scan, &next)) {
3637             traits[count].value = omp_atv_uncontended;
3638           } else if (__kmp_match_str("serialized", scan, &next)) {
3639             traits[count].value = omp_atv_serialized;
3640           } else if (__kmp_match_str("private", scan, &next)) {
3641             traits[count].value = omp_atv_private;
3642           } else {
3643             SET_KEY();
3644             SKIP_PAIR(key);
3645             continue;
3646           }
3647         } else if (__kmp_match_str("alignment", scan, &next)) {
3648           GET_NEXT('=');
3649           if (!isdigit(*next)) {
3650             SET_KEY();
3651             SKIP_PAIR(key);
3652             continue;
3653           }
3654           SKIP_DIGITS(next);
3655           int n = __kmp_str_to_int(scan, ',');
3656           if (n < 0 || !IS_POWER_OF_TWO(n)) {
3657             SET_KEY();
3658             SKIP_PAIR(key);
3659             continue;
3660           }
3661           traits[count].key = omp_atk_alignment;
3662           traits[count].value = n;
3663         } else if (__kmp_match_str("access", scan, &next)) {
3664           GET_NEXT('=');
3665           traits[count].key = omp_atk_access;
3666           if (__kmp_match_str("all", scan, &next)) {
3667             traits[count].value = omp_atv_all;
3668           } else if (__kmp_match_str("cgroup", scan, &next)) {
3669             traits[count].value = omp_atv_cgroup;
3670           } else if (__kmp_match_str("pteam", scan, &next)) {
3671             traits[count].value = omp_atv_pteam;
3672           } else if (__kmp_match_str("thread", scan, &next)) {
3673             traits[count].value = omp_atv_thread;
3674           } else {
3675             SET_KEY();
3676             SKIP_PAIR(key);
3677             continue;
3678           }
3679         } else if (__kmp_match_str("pool_size", scan, &next)) {
3680           GET_NEXT('=');
3681           if (!isdigit(*next)) {
3682             SET_KEY();
3683             SKIP_PAIR(key);
3684             continue;
3685           }
3686           SKIP_DIGITS(next);
3687           int n = __kmp_str_to_int(scan, ',');
3688           if (n < 0) {
3689             SET_KEY();
3690             SKIP_PAIR(key);
3691             continue;
3692           }
3693           traits[count].key = omp_atk_pool_size;
3694           traits[count].value = n;
3695         } else if (__kmp_match_str("fallback", scan, &next)) {
3696           GET_NEXT('=');
3697           traits[count].key = omp_atk_fallback;
3698           if (__kmp_match_str("default_mem_fb", scan, &next)) {
3699             traits[count].value = omp_atv_default_mem_fb;
3700           } else if (__kmp_match_str("null_fb", scan, &next)) {
3701             traits[count].value = omp_atv_null_fb;
3702           } else if (__kmp_match_str("abort_fb", scan, &next)) {
3703             traits[count].value = omp_atv_abort_fb;
3704           } else if (__kmp_match_str("allocator_fb", scan, &next)) {
3705             traits[count].value = omp_atv_allocator_fb;
3706           } else {
3707             SET_KEY();
3708             SKIP_PAIR(key);
3709             continue;
3710           }
3711         } else if (__kmp_match_str("pinned", scan, &next)) {
3712           GET_NEXT('=');
3713           traits[count].key = omp_atk_pinned;
3714           if (__kmp_str_match_true(next)) {
3715             traits[count].value = omp_atv_true;
3716           } else if (__kmp_str_match_false(next)) {
3717             traits[count].value = omp_atv_false;
3718           } else {
3719             SET_KEY();
3720             SKIP_PAIR(key);
3721             continue;
3722           }
3723         } else if (__kmp_match_str("partition", scan, &next)) {
3724           GET_NEXT('=');
3725           traits[count].key = omp_atk_partition;
3726           if (__kmp_match_str("environment", scan, &next)) {
3727             traits[count].value = omp_atv_environment;
3728           } else if (__kmp_match_str("nearest", scan, &next)) {
3729             traits[count].value = omp_atv_nearest;
3730           } else if (__kmp_match_str("blocked", scan, &next)) {
3731             traits[count].value = omp_atv_blocked;
3732           } else if (__kmp_match_str("interleaved", scan, &next)) {
3733             traits[count].value = omp_atv_interleaved;
3734           } else {
3735             SET_KEY();
3736             SKIP_PAIR(key);
3737             continue;
3738           }
3739         } else {
3740           SET_KEY();
3741           SKIP_PAIR(key);
3742           continue;
3743         }
3744         SKIP_WS(next);
3745         ++count;
3746         if (count == ntraits)
3747           break;
3748         GET_NEXT(',');
3749       } // traits
3750     } // memspace
3751   } // while
3752   al = __kmpc_init_allocator(__kmp_get_gtid(), ms, ntraits, traits);
3753   __kmp_def_allocator = (al == omp_null_allocator) ? omp_default_mem_alloc : al;
3754 }
3755 
__kmp_stg_print_allocator(kmp_str_buf_t * buffer,char const * name,void * data)3756 static void __kmp_stg_print_allocator(kmp_str_buf_t *buffer, char const *name,
3757                                       void *data) {
3758   if (__kmp_def_allocator == omp_default_mem_alloc) {
3759     __kmp_stg_print_str(buffer, name, "omp_default_mem_alloc");
3760   } else if (__kmp_def_allocator == omp_high_bw_mem_alloc) {
3761     __kmp_stg_print_str(buffer, name, "omp_high_bw_mem_alloc");
3762   } else if (__kmp_def_allocator == omp_large_cap_mem_alloc) {
3763     __kmp_stg_print_str(buffer, name, "omp_large_cap_mem_alloc");
3764   } else if (__kmp_def_allocator == omp_const_mem_alloc) {
3765     __kmp_stg_print_str(buffer, name, "omp_const_mem_alloc");
3766   } else if (__kmp_def_allocator == omp_low_lat_mem_alloc) {
3767     __kmp_stg_print_str(buffer, name, "omp_low_lat_mem_alloc");
3768   } else if (__kmp_def_allocator == omp_cgroup_mem_alloc) {
3769     __kmp_stg_print_str(buffer, name, "omp_cgroup_mem_alloc");
3770   } else if (__kmp_def_allocator == omp_pteam_mem_alloc) {
3771     __kmp_stg_print_str(buffer, name, "omp_pteam_mem_alloc");
3772   } else if (__kmp_def_allocator == omp_thread_mem_alloc) {
3773     __kmp_stg_print_str(buffer, name, "omp_thread_mem_alloc");
3774   }
3775 }
3776 
3777 // -----------------------------------------------------------------------------
3778 // OMP_DYNAMIC
3779 
__kmp_stg_parse_omp_dynamic(char const * name,char const * value,void * data)3780 static void __kmp_stg_parse_omp_dynamic(char const *name, char const *value,
3781                                         void *data) {
3782   __kmp_stg_parse_bool(name, value, &(__kmp_global.g.g_dynamic));
3783 } // __kmp_stg_parse_omp_dynamic
3784 
__kmp_stg_print_omp_dynamic(kmp_str_buf_t * buffer,char const * name,void * data)3785 static void __kmp_stg_print_omp_dynamic(kmp_str_buf_t *buffer, char const *name,
3786                                         void *data) {
3787   __kmp_stg_print_bool(buffer, name, __kmp_global.g.g_dynamic);
3788 } // __kmp_stg_print_omp_dynamic
3789 
__kmp_stg_parse_kmp_dynamic_mode(char const * name,char const * value,void * data)3790 static void __kmp_stg_parse_kmp_dynamic_mode(char const *name,
3791                                              char const *value, void *data) {
3792   if (TCR_4(__kmp_init_parallel)) {
3793     KMP_WARNING(EnvParallelWarn, name);
3794     __kmp_env_toPrint(name, 0);
3795     return;
3796   }
3797 #ifdef USE_LOAD_BALANCE
3798   else if (__kmp_str_match("load balance", 2, value) ||
3799            __kmp_str_match("load_balance", 2, value) ||
3800            __kmp_str_match("load-balance", 2, value) ||
3801            __kmp_str_match("loadbalance", 2, value) ||
3802            __kmp_str_match("balance", 1, value)) {
3803     __kmp_global.g.g_dynamic_mode = dynamic_load_balance;
3804   }
3805 #endif /* USE_LOAD_BALANCE */
3806   else if (__kmp_str_match("thread limit", 1, value) ||
3807            __kmp_str_match("thread_limit", 1, value) ||
3808            __kmp_str_match("thread-limit", 1, value) ||
3809            __kmp_str_match("threadlimit", 1, value) ||
3810            __kmp_str_match("limit", 2, value)) {
3811     __kmp_global.g.g_dynamic_mode = dynamic_thread_limit;
3812   } else if (__kmp_str_match("random", 1, value)) {
3813     __kmp_global.g.g_dynamic_mode = dynamic_random;
3814   } else {
3815     KMP_WARNING(StgInvalidValue, name, value);
3816   }
3817 } //__kmp_stg_parse_kmp_dynamic_mode
3818 
__kmp_stg_print_kmp_dynamic_mode(kmp_str_buf_t * buffer,char const * name,void * data)3819 static void __kmp_stg_print_kmp_dynamic_mode(kmp_str_buf_t *buffer,
3820                                              char const *name, void *data) {
3821 #if KMP_DEBUG
3822   if (__kmp_global.g.g_dynamic_mode == dynamic_default) {
3823     __kmp_str_buf_print(buffer, "   %s: %s \n", name, KMP_I18N_STR(NotDefined));
3824   }
3825 #ifdef USE_LOAD_BALANCE
3826   else if (__kmp_global.g.g_dynamic_mode == dynamic_load_balance) {
3827     __kmp_stg_print_str(buffer, name, "load balance");
3828   }
3829 #endif /* USE_LOAD_BALANCE */
3830   else if (__kmp_global.g.g_dynamic_mode == dynamic_thread_limit) {
3831     __kmp_stg_print_str(buffer, name, "thread limit");
3832   } else if (__kmp_global.g.g_dynamic_mode == dynamic_random) {
3833     __kmp_stg_print_str(buffer, name, "random");
3834   } else {
3835     KMP_ASSERT(0);
3836   }
3837 #endif /* KMP_DEBUG */
3838 } // __kmp_stg_print_kmp_dynamic_mode
3839 
3840 #ifdef USE_LOAD_BALANCE
3841 
3842 // -----------------------------------------------------------------------------
3843 // KMP_LOAD_BALANCE_INTERVAL
3844 
__kmp_stg_parse_ld_balance_interval(char const * name,char const * value,void * data)3845 static void __kmp_stg_parse_ld_balance_interval(char const *name,
3846                                                 char const *value, void *data) {
3847   double interval = __kmp_convert_to_double(value);
3848   if (interval >= 0) {
3849     __kmp_load_balance_interval = interval;
3850   } else {
3851     KMP_WARNING(StgInvalidValue, name, value);
3852   }
3853 } // __kmp_stg_parse_load_balance_interval
3854 
__kmp_stg_print_ld_balance_interval(kmp_str_buf_t * buffer,char const * name,void * data)3855 static void __kmp_stg_print_ld_balance_interval(kmp_str_buf_t *buffer,
3856                                                 char const *name, void *data) {
3857 #if KMP_DEBUG
3858   __kmp_str_buf_print(buffer, "   %s=%8.6f\n", name,
3859                       __kmp_load_balance_interval);
3860 #endif /* KMP_DEBUG */
3861 } // __kmp_stg_print_load_balance_interval
3862 
3863 #endif /* USE_LOAD_BALANCE */
3864 
3865 // -----------------------------------------------------------------------------
3866 // KMP_INIT_AT_FORK
3867 
__kmp_stg_parse_init_at_fork(char const * name,char const * value,void * data)3868 static void __kmp_stg_parse_init_at_fork(char const *name, char const *value,
3869                                          void *data) {
3870   __kmp_stg_parse_bool(name, value, &__kmp_need_register_atfork);
3871   if (__kmp_need_register_atfork) {
3872     __kmp_need_register_atfork_specified = TRUE;
3873   }
3874 } // __kmp_stg_parse_init_at_fork
3875 
__kmp_stg_print_init_at_fork(kmp_str_buf_t * buffer,char const * name,void * data)3876 static void __kmp_stg_print_init_at_fork(kmp_str_buf_t *buffer,
3877                                          char const *name, void *data) {
3878   __kmp_stg_print_bool(buffer, name, __kmp_need_register_atfork_specified);
3879 } // __kmp_stg_print_init_at_fork
3880 
3881 // -----------------------------------------------------------------------------
3882 // KMP_SCHEDULE
3883 
__kmp_stg_parse_schedule(char const * name,char const * value,void * data)3884 static void __kmp_stg_parse_schedule(char const *name, char const *value,
3885                                      void *data) {
3886 
3887   if (value != NULL) {
3888     size_t length = KMP_STRLEN(value);
3889     if (length > INT_MAX) {
3890       KMP_WARNING(LongValue, name);
3891     } else {
3892       const char *semicolon;
3893       if (value[length - 1] == '"' || value[length - 1] == '\'')
3894         KMP_WARNING(UnbalancedQuotes, name);
3895       do {
3896         char sentinel;
3897 
3898         semicolon = strchr(value, ';');
3899         if (*value && semicolon != value) {
3900           const char *comma = strchr(value, ',');
3901 
3902           if (comma) {
3903             ++comma;
3904             sentinel = ',';
3905           } else
3906             sentinel = ';';
3907           if (!__kmp_strcasecmp_with_sentinel("static", value, sentinel)) {
3908             if (!__kmp_strcasecmp_with_sentinel("greedy", comma, ';')) {
3909               __kmp_static = kmp_sch_static_greedy;
3910               continue;
3911             } else if (!__kmp_strcasecmp_with_sentinel("balanced", comma,
3912                                                        ';')) {
3913               __kmp_static = kmp_sch_static_balanced;
3914               continue;
3915             }
3916           } else if (!__kmp_strcasecmp_with_sentinel("guided", value,
3917                                                      sentinel)) {
3918             if (!__kmp_strcasecmp_with_sentinel("iterative", comma, ';')) {
3919               __kmp_guided = kmp_sch_guided_iterative_chunked;
3920               continue;
3921             } else if (!__kmp_strcasecmp_with_sentinel("analytical", comma,
3922                                                        ';')) {
3923               /* analytical not allowed for too many threads */
3924               __kmp_guided = kmp_sch_guided_analytical_chunked;
3925               continue;
3926             }
3927           }
3928           KMP_WARNING(InvalidClause, name, value);
3929         } else
3930           KMP_WARNING(EmptyClause, name);
3931       } while ((value = semicolon ? semicolon + 1 : NULL));
3932     }
3933   }
3934 
3935 } // __kmp_stg_parse__schedule
3936 
__kmp_stg_print_schedule(kmp_str_buf_t * buffer,char const * name,void * data)3937 static void __kmp_stg_print_schedule(kmp_str_buf_t *buffer, char const *name,
3938                                      void *data) {
3939   if (__kmp_env_format) {
3940     KMP_STR_BUF_PRINT_NAME_EX(name);
3941   } else {
3942     __kmp_str_buf_print(buffer, "   %s='", name);
3943   }
3944   if (__kmp_static == kmp_sch_static_greedy) {
3945     __kmp_str_buf_print(buffer, "%s", "static,greedy");
3946   } else if (__kmp_static == kmp_sch_static_balanced) {
3947     __kmp_str_buf_print(buffer, "%s", "static,balanced");
3948   }
3949   if (__kmp_guided == kmp_sch_guided_iterative_chunked) {
3950     __kmp_str_buf_print(buffer, ";%s'\n", "guided,iterative");
3951   } else if (__kmp_guided == kmp_sch_guided_analytical_chunked) {
3952     __kmp_str_buf_print(buffer, ";%s'\n", "guided,analytical");
3953   }
3954 } // __kmp_stg_print_schedule
3955 
3956 // -----------------------------------------------------------------------------
3957 // OMP_SCHEDULE
3958 
__kmp_omp_schedule_restore()3959 static inline void __kmp_omp_schedule_restore() {
3960 #if KMP_USE_HIER_SCHED
3961   __kmp_hier_scheds.deallocate();
3962 #endif
3963   __kmp_chunk = 0;
3964   __kmp_sched = kmp_sch_default;
3965 }
3966 
3967 // if parse_hier = true:
3968 //    Parse [HW,][modifier:]kind[,chunk]
3969 // else:
3970 //    Parse [modifier:]kind[,chunk]
__kmp_parse_single_omp_schedule(const char * name,const char * value,bool parse_hier=false)3971 static const char *__kmp_parse_single_omp_schedule(const char *name,
3972                                                    const char *value,
3973                                                    bool parse_hier = false) {
3974   /* get the specified scheduling style */
3975   const char *ptr = value;
3976   const char *delim;
3977   int chunk = 0;
3978   enum sched_type sched = kmp_sch_default;
3979   if (*ptr == '\0')
3980     return NULL;
3981   delim = ptr;
3982   while (*delim != ',' && *delim != ':' && *delim != '\0')
3983     delim++;
3984 #if KMP_USE_HIER_SCHED
3985   kmp_hier_layer_e layer = kmp_hier_layer_e::LAYER_THREAD;
3986   if (parse_hier) {
3987     if (*delim == ',') {
3988       if (!__kmp_strcasecmp_with_sentinel("L1", ptr, ',')) {
3989         layer = kmp_hier_layer_e::LAYER_L1;
3990       } else if (!__kmp_strcasecmp_with_sentinel("L2", ptr, ',')) {
3991         layer = kmp_hier_layer_e::LAYER_L2;
3992       } else if (!__kmp_strcasecmp_with_sentinel("L3", ptr, ',')) {
3993         layer = kmp_hier_layer_e::LAYER_L3;
3994       } else if (!__kmp_strcasecmp_with_sentinel("NUMA", ptr, ',')) {
3995         layer = kmp_hier_layer_e::LAYER_NUMA;
3996       }
3997     }
3998     if (layer != kmp_hier_layer_e::LAYER_THREAD && *delim != ',') {
3999       // If there is no comma after the layer, then this schedule is invalid
4000       KMP_WARNING(StgInvalidValue, name, value);
4001       __kmp_omp_schedule_restore();
4002       return NULL;
4003     } else if (layer != kmp_hier_layer_e::LAYER_THREAD) {
4004       ptr = ++delim;
4005       while (*delim != ',' && *delim != ':' && *delim != '\0')
4006         delim++;
4007     }
4008   }
4009 #endif // KMP_USE_HIER_SCHED
4010   // Read in schedule modifier if specified
4011   enum sched_type sched_modifier = (enum sched_type)0;
4012   if (*delim == ':') {
4013     if (!__kmp_strcasecmp_with_sentinel("monotonic", ptr, *delim)) {
4014       sched_modifier = sched_type::kmp_sch_modifier_monotonic;
4015       ptr = ++delim;
4016       while (*delim != ',' && *delim != ':' && *delim != '\0')
4017         delim++;
4018     } else if (!__kmp_strcasecmp_with_sentinel("nonmonotonic", ptr, *delim)) {
4019       sched_modifier = sched_type::kmp_sch_modifier_nonmonotonic;
4020       ptr = ++delim;
4021       while (*delim != ',' && *delim != ':' && *delim != '\0')
4022         delim++;
4023     } else if (!parse_hier) {
4024       // If there is no proper schedule modifier, then this schedule is invalid
4025       KMP_WARNING(StgInvalidValue, name, value);
4026       __kmp_omp_schedule_restore();
4027       return NULL;
4028     }
4029   }
4030   // Read in schedule kind (required)
4031   if (!__kmp_strcasecmp_with_sentinel("dynamic", ptr, *delim))
4032     sched = kmp_sch_dynamic_chunked;
4033   else if (!__kmp_strcasecmp_with_sentinel("guided", ptr, *delim))
4034     sched = kmp_sch_guided_chunked;
4035   // AC: TODO: probably remove TRAPEZOIDAL (OMP 3.0 does not allow it)
4036   else if (!__kmp_strcasecmp_with_sentinel("auto", ptr, *delim))
4037     sched = kmp_sch_auto;
4038   else if (!__kmp_strcasecmp_with_sentinel("trapezoidal", ptr, *delim))
4039     sched = kmp_sch_trapezoidal;
4040   else if (!__kmp_strcasecmp_with_sentinel("static", ptr, *delim))
4041     sched = kmp_sch_static;
4042 #if KMP_STATIC_STEAL_ENABLED
4043   else if (!__kmp_strcasecmp_with_sentinel("static_steal", ptr, *delim)) {
4044     // replace static_steal with dynamic to better cope with ordered loops
4045     sched = kmp_sch_dynamic_chunked;
4046     sched_modifier = sched_type::kmp_sch_modifier_nonmonotonic;
4047   }
4048 #endif
4049   else {
4050     // If there is no proper schedule kind, then this schedule is invalid
4051     KMP_WARNING(StgInvalidValue, name, value);
4052     __kmp_omp_schedule_restore();
4053     return NULL;
4054   }
4055 
4056   // Read in schedule chunk size if specified
4057   if (*delim == ',') {
4058     ptr = delim + 1;
4059     SKIP_WS(ptr);
4060     if (!isdigit(*ptr)) {
4061       // If there is no chunk after comma, then this schedule is invalid
4062       KMP_WARNING(StgInvalidValue, name, value);
4063       __kmp_omp_schedule_restore();
4064       return NULL;
4065     }
4066     SKIP_DIGITS(ptr);
4067     // auto schedule should not specify chunk size
4068     if (sched == kmp_sch_auto) {
4069       __kmp_msg(kmp_ms_warning, KMP_MSG(IgnoreChunk, name, delim),
4070                 __kmp_msg_null);
4071     } else {
4072       if (sched == kmp_sch_static)
4073         sched = kmp_sch_static_chunked;
4074       chunk = __kmp_str_to_int(delim + 1, *ptr);
4075       if (chunk < 1) {
4076         chunk = KMP_DEFAULT_CHUNK;
4077         __kmp_msg(kmp_ms_warning, KMP_MSG(InvalidChunk, name, delim),
4078                   __kmp_msg_null);
4079         KMP_INFORM(Using_int_Value, name, __kmp_chunk);
4080         // AC: next block commented out until KMP_DEFAULT_CHUNK != KMP_MIN_CHUNK
4081         // (to improve code coverage :)
4082         // The default chunk size is 1 according to standard, thus making
4083         // KMP_MIN_CHUNK not 1 we would introduce mess:
4084         // wrong chunk becomes 1, but it will be impossible to explicitly set
4085         // to 1 because it becomes KMP_MIN_CHUNK...
4086         // } else if ( chunk < KMP_MIN_CHUNK ) {
4087         //   chunk = KMP_MIN_CHUNK;
4088       } else if (chunk > KMP_MAX_CHUNK) {
4089         chunk = KMP_MAX_CHUNK;
4090         __kmp_msg(kmp_ms_warning, KMP_MSG(LargeChunk, name, delim),
4091                   __kmp_msg_null);
4092         KMP_INFORM(Using_int_Value, name, chunk);
4093       }
4094     }
4095   } else {
4096     ptr = delim;
4097   }
4098 
4099   SCHEDULE_SET_MODIFIERS(sched, sched_modifier);
4100 
4101 #if KMP_USE_HIER_SCHED
4102   if (layer != kmp_hier_layer_e::LAYER_THREAD) {
4103     __kmp_hier_scheds.append(sched, chunk, layer);
4104   } else
4105 #endif
4106   {
4107     __kmp_chunk = chunk;
4108     __kmp_sched = sched;
4109   }
4110   return ptr;
4111 }
4112 
__kmp_stg_parse_omp_schedule(char const * name,char const * value,void * data)4113 static void __kmp_stg_parse_omp_schedule(char const *name, char const *value,
4114                                          void *data) {
4115   size_t length;
4116   const char *ptr = value;
4117   SKIP_WS(ptr);
4118   if (value) {
4119     length = KMP_STRLEN(value);
4120     if (length) {
4121       if (value[length - 1] == '"' || value[length - 1] == '\'')
4122         KMP_WARNING(UnbalancedQuotes, name);
4123 /* get the specified scheduling style */
4124 #if KMP_USE_HIER_SCHED
4125       if (!__kmp_strcasecmp_with_sentinel("EXPERIMENTAL", ptr, ' ')) {
4126         SKIP_TOKEN(ptr);
4127         SKIP_WS(ptr);
4128         while ((ptr = __kmp_parse_single_omp_schedule(name, ptr, true))) {
4129           while (*ptr == ' ' || *ptr == '\t' || *ptr == ':')
4130             ptr++;
4131           if (*ptr == '\0')
4132             break;
4133         }
4134       } else
4135 #endif
4136         __kmp_parse_single_omp_schedule(name, ptr);
4137     } else
4138       KMP_WARNING(EmptyString, name);
4139   }
4140 #if KMP_USE_HIER_SCHED
4141   __kmp_hier_scheds.sort();
4142 #endif
4143   K_DIAG(1, ("__kmp_static == %d\n", __kmp_static))
4144   K_DIAG(1, ("__kmp_guided == %d\n", __kmp_guided))
4145   K_DIAG(1, ("__kmp_sched == %d\n", __kmp_sched))
4146   K_DIAG(1, ("__kmp_chunk == %d\n", __kmp_chunk))
4147 } // __kmp_stg_parse_omp_schedule
4148 
__kmp_stg_print_omp_schedule(kmp_str_buf_t * buffer,char const * name,void * data)4149 static void __kmp_stg_print_omp_schedule(kmp_str_buf_t *buffer,
4150                                          char const *name, void *data) {
4151   if (__kmp_env_format) {
4152     KMP_STR_BUF_PRINT_NAME_EX(name);
4153   } else {
4154     __kmp_str_buf_print(buffer, "   %s='", name);
4155   }
4156   enum sched_type sched = SCHEDULE_WITHOUT_MODIFIERS(__kmp_sched);
4157   if (SCHEDULE_HAS_MONOTONIC(__kmp_sched)) {
4158     __kmp_str_buf_print(buffer, "monotonic:");
4159   } else if (SCHEDULE_HAS_NONMONOTONIC(__kmp_sched)) {
4160     __kmp_str_buf_print(buffer, "nonmonotonic:");
4161   }
4162   if (__kmp_chunk) {
4163     switch (sched) {
4164     case kmp_sch_dynamic_chunked:
4165       __kmp_str_buf_print(buffer, "%s,%d'\n", "dynamic", __kmp_chunk);
4166       break;
4167     case kmp_sch_guided_iterative_chunked:
4168     case kmp_sch_guided_analytical_chunked:
4169       __kmp_str_buf_print(buffer, "%s,%d'\n", "guided", __kmp_chunk);
4170       break;
4171     case kmp_sch_trapezoidal:
4172       __kmp_str_buf_print(buffer, "%s,%d'\n", "trapezoidal", __kmp_chunk);
4173       break;
4174     case kmp_sch_static:
4175     case kmp_sch_static_chunked:
4176     case kmp_sch_static_balanced:
4177     case kmp_sch_static_greedy:
4178       __kmp_str_buf_print(buffer, "%s,%d'\n", "static", __kmp_chunk);
4179       break;
4180     case kmp_sch_static_steal:
4181       __kmp_str_buf_print(buffer, "%s,%d'\n", "static_steal", __kmp_chunk);
4182       break;
4183     case kmp_sch_auto:
4184       __kmp_str_buf_print(buffer, "%s,%d'\n", "auto", __kmp_chunk);
4185       break;
4186     }
4187   } else {
4188     switch (sched) {
4189     case kmp_sch_dynamic_chunked:
4190       __kmp_str_buf_print(buffer, "%s'\n", "dynamic");
4191       break;
4192     case kmp_sch_guided_iterative_chunked:
4193     case kmp_sch_guided_analytical_chunked:
4194       __kmp_str_buf_print(buffer, "%s'\n", "guided");
4195       break;
4196     case kmp_sch_trapezoidal:
4197       __kmp_str_buf_print(buffer, "%s'\n", "trapezoidal");
4198       break;
4199     case kmp_sch_static:
4200     case kmp_sch_static_chunked:
4201     case kmp_sch_static_balanced:
4202     case kmp_sch_static_greedy:
4203       __kmp_str_buf_print(buffer, "%s'\n", "static");
4204       break;
4205     case kmp_sch_static_steal:
4206       __kmp_str_buf_print(buffer, "%s'\n", "static_steal");
4207       break;
4208     case kmp_sch_auto:
4209       __kmp_str_buf_print(buffer, "%s'\n", "auto");
4210       break;
4211     }
4212   }
4213 } // __kmp_stg_print_omp_schedule
4214 
4215 #if KMP_USE_HIER_SCHED
4216 // -----------------------------------------------------------------------------
4217 // KMP_DISP_HAND_THREAD
__kmp_stg_parse_kmp_hand_thread(char const * name,char const * value,void * data)4218 static void __kmp_stg_parse_kmp_hand_thread(char const *name, char const *value,
4219                                             void *data) {
4220   __kmp_stg_parse_bool(name, value, &(__kmp_dispatch_hand_threading));
4221 } // __kmp_stg_parse_kmp_hand_thread
4222 
__kmp_stg_print_kmp_hand_thread(kmp_str_buf_t * buffer,char const * name,void * data)4223 static void __kmp_stg_print_kmp_hand_thread(kmp_str_buf_t *buffer,
4224                                             char const *name, void *data) {
4225   __kmp_stg_print_bool(buffer, name, __kmp_dispatch_hand_threading);
4226 } // __kmp_stg_print_kmp_hand_thread
4227 #endif
4228 
4229 // -----------------------------------------------------------------------------
4230 // KMP_FORCE_MONOTONIC_DYNAMIC_SCHEDULE
__kmp_stg_parse_kmp_force_monotonic(char const * name,char const * value,void * data)4231 static void __kmp_stg_parse_kmp_force_monotonic(char const *name,
4232                                                 char const *value, void *data) {
4233   __kmp_stg_parse_bool(name, value, &(__kmp_force_monotonic));
4234 } // __kmp_stg_parse_kmp_force_monotonic
4235 
__kmp_stg_print_kmp_force_monotonic(kmp_str_buf_t * buffer,char const * name,void * data)4236 static void __kmp_stg_print_kmp_force_monotonic(kmp_str_buf_t *buffer,
4237                                                 char const *name, void *data) {
4238   __kmp_stg_print_bool(buffer, name, __kmp_force_monotonic);
4239 } // __kmp_stg_print_kmp_force_monotonic
4240 
4241 // -----------------------------------------------------------------------------
4242 // KMP_ATOMIC_MODE
4243 
__kmp_stg_parse_atomic_mode(char const * name,char const * value,void * data)4244 static void __kmp_stg_parse_atomic_mode(char const *name, char const *value,
4245                                         void *data) {
4246   // Modes: 0 -- do not change default; 1 -- Intel perf mode, 2 -- GOMP
4247   // compatibility mode.
4248   int mode = 0;
4249   int max = 1;
4250 #ifdef KMP_GOMP_COMPAT
4251   max = 2;
4252 #endif /* KMP_GOMP_COMPAT */
4253   __kmp_stg_parse_int(name, value, 0, max, &mode);
4254   // TODO; parse_int is not very suitable for this case. In case of overflow it
4255   // is better to use
4256   // 0 rather that max value.
4257   if (mode > 0) {
4258     __kmp_atomic_mode = mode;
4259   }
4260 } // __kmp_stg_parse_atomic_mode
4261 
__kmp_stg_print_atomic_mode(kmp_str_buf_t * buffer,char const * name,void * data)4262 static void __kmp_stg_print_atomic_mode(kmp_str_buf_t *buffer, char const *name,
4263                                         void *data) {
4264   __kmp_stg_print_int(buffer, name, __kmp_atomic_mode);
4265 } // __kmp_stg_print_atomic_mode
4266 
4267 // -----------------------------------------------------------------------------
4268 // KMP_CONSISTENCY_CHECK
4269 
__kmp_stg_parse_consistency_check(char const * name,char const * value,void * data)4270 static void __kmp_stg_parse_consistency_check(char const *name,
4271                                               char const *value, void *data) {
4272   if (!__kmp_strcasecmp_with_sentinel("all", value, 0)) {
4273     // Note, this will not work from kmp_set_defaults because th_cons stack was
4274     // not allocated
4275     // for existed thread(s) thus the first __kmp_push_<construct> will break
4276     // with assertion.
4277     // TODO: allocate th_cons if called from kmp_set_defaults.
4278     __kmp_env_consistency_check = TRUE;
4279   } else if (!__kmp_strcasecmp_with_sentinel("none", value, 0)) {
4280     __kmp_env_consistency_check = FALSE;
4281   } else {
4282     KMP_WARNING(StgInvalidValue, name, value);
4283   }
4284 } // __kmp_stg_parse_consistency_check
4285 
__kmp_stg_print_consistency_check(kmp_str_buf_t * buffer,char const * name,void * data)4286 static void __kmp_stg_print_consistency_check(kmp_str_buf_t *buffer,
4287                                               char const *name, void *data) {
4288 #if KMP_DEBUG
4289   const char *value = NULL;
4290 
4291   if (__kmp_env_consistency_check) {
4292     value = "all";
4293   } else {
4294     value = "none";
4295   }
4296 
4297   if (value != NULL) {
4298     __kmp_stg_print_str(buffer, name, value);
4299   }
4300 #endif /* KMP_DEBUG */
4301 } // __kmp_stg_print_consistency_check
4302 
4303 #if USE_ITT_BUILD
4304 // -----------------------------------------------------------------------------
4305 // KMP_ITT_PREPARE_DELAY
4306 
4307 #if USE_ITT_NOTIFY
4308 
__kmp_stg_parse_itt_prepare_delay(char const * name,char const * value,void * data)4309 static void __kmp_stg_parse_itt_prepare_delay(char const *name,
4310                                               char const *value, void *data) {
4311   // Experimental code: KMP_ITT_PREPARE_DELAY specifies numbert of loop
4312   // iterations.
4313   int delay = 0;
4314   __kmp_stg_parse_int(name, value, 0, INT_MAX, &delay);
4315   __kmp_itt_prepare_delay = delay;
4316 } // __kmp_str_parse_itt_prepare_delay
4317 
__kmp_stg_print_itt_prepare_delay(kmp_str_buf_t * buffer,char const * name,void * data)4318 static void __kmp_stg_print_itt_prepare_delay(kmp_str_buf_t *buffer,
4319                                               char const *name, void *data) {
4320   __kmp_stg_print_uint64(buffer, name, __kmp_itt_prepare_delay);
4321 
4322 } // __kmp_str_print_itt_prepare_delay
4323 
4324 #endif // USE_ITT_NOTIFY
4325 #endif /* USE_ITT_BUILD */
4326 
4327 // -----------------------------------------------------------------------------
4328 // KMP_MALLOC_POOL_INCR
4329 
__kmp_stg_parse_malloc_pool_incr(char const * name,char const * value,void * data)4330 static void __kmp_stg_parse_malloc_pool_incr(char const *name,
4331                                              char const *value, void *data) {
4332   __kmp_stg_parse_size(name, value, KMP_MIN_MALLOC_POOL_INCR,
4333                        KMP_MAX_MALLOC_POOL_INCR, NULL, &__kmp_malloc_pool_incr,
4334                        1);
4335 } // __kmp_stg_parse_malloc_pool_incr
4336 
__kmp_stg_print_malloc_pool_incr(kmp_str_buf_t * buffer,char const * name,void * data)4337 static void __kmp_stg_print_malloc_pool_incr(kmp_str_buf_t *buffer,
4338                                              char const *name, void *data) {
4339   __kmp_stg_print_size(buffer, name, __kmp_malloc_pool_incr);
4340 
4341 } // _kmp_stg_print_malloc_pool_incr
4342 
4343 #ifdef KMP_DEBUG
4344 
4345 // -----------------------------------------------------------------------------
4346 // KMP_PAR_RANGE
4347 
__kmp_stg_parse_par_range_env(char const * name,char const * value,void * data)4348 static void __kmp_stg_parse_par_range_env(char const *name, char const *value,
4349                                           void *data) {
4350   __kmp_stg_parse_par_range(name, value, &__kmp_par_range,
4351                             __kmp_par_range_routine, __kmp_par_range_filename,
4352                             &__kmp_par_range_lb, &__kmp_par_range_ub);
4353 } // __kmp_stg_parse_par_range_env
4354 
__kmp_stg_print_par_range_env(kmp_str_buf_t * buffer,char const * name,void * data)4355 static void __kmp_stg_print_par_range_env(kmp_str_buf_t *buffer,
4356                                           char const *name, void *data) {
4357   if (__kmp_par_range != 0) {
4358     __kmp_stg_print_str(buffer, name, par_range_to_print);
4359   }
4360 } // __kmp_stg_print_par_range_env
4361 
4362 #endif
4363 
4364 // -----------------------------------------------------------------------------
4365 // KMP_GTID_MODE
4366 
__kmp_stg_parse_gtid_mode(char const * name,char const * value,void * data)4367 static void __kmp_stg_parse_gtid_mode(char const *name, char const *value,
4368                                       void *data) {
4369   // Modes:
4370   //   0 -- do not change default
4371   //   1 -- sp search
4372   //   2 -- use "keyed" TLS var, i.e.
4373   //        pthread_getspecific(Linux* OS/OS X*) or TlsGetValue(Windows* OS)
4374   //   3 -- __declspec(thread) TLS var in tdata section
4375   int mode = 0;
4376   int max = 2;
4377 #ifdef KMP_TDATA_GTID
4378   max = 3;
4379 #endif /* KMP_TDATA_GTID */
4380   __kmp_stg_parse_int(name, value, 0, max, &mode);
4381   // TODO; parse_int is not very suitable for this case. In case of overflow it
4382   // is better to use 0 rather that max value.
4383   if (mode == 0) {
4384     __kmp_adjust_gtid_mode = TRUE;
4385   } else {
4386     __kmp_gtid_mode = mode;
4387     __kmp_adjust_gtid_mode = FALSE;
4388   }
4389 } // __kmp_str_parse_gtid_mode
4390 
__kmp_stg_print_gtid_mode(kmp_str_buf_t * buffer,char const * name,void * data)4391 static void __kmp_stg_print_gtid_mode(kmp_str_buf_t *buffer, char const *name,
4392                                       void *data) {
4393   if (__kmp_adjust_gtid_mode) {
4394     __kmp_stg_print_int(buffer, name, 0);
4395   } else {
4396     __kmp_stg_print_int(buffer, name, __kmp_gtid_mode);
4397   }
4398 } // __kmp_stg_print_gtid_mode
4399 
4400 // -----------------------------------------------------------------------------
4401 // KMP_NUM_LOCKS_IN_BLOCK
4402 
__kmp_stg_parse_lock_block(char const * name,char const * value,void * data)4403 static void __kmp_stg_parse_lock_block(char const *name, char const *value,
4404                                        void *data) {
4405   __kmp_stg_parse_int(name, value, 0, KMP_INT_MAX, &__kmp_num_locks_in_block);
4406 } // __kmp_str_parse_lock_block
4407 
__kmp_stg_print_lock_block(kmp_str_buf_t * buffer,char const * name,void * data)4408 static void __kmp_stg_print_lock_block(kmp_str_buf_t *buffer, char const *name,
4409                                        void *data) {
4410   __kmp_stg_print_int(buffer, name, __kmp_num_locks_in_block);
4411 } // __kmp_stg_print_lock_block
4412 
4413 // -----------------------------------------------------------------------------
4414 // KMP_LOCK_KIND
4415 
4416 #if KMP_USE_DYNAMIC_LOCK
4417 #define KMP_STORE_LOCK_SEQ(a) (__kmp_user_lock_seq = lockseq_##a)
4418 #else
4419 #define KMP_STORE_LOCK_SEQ(a)
4420 #endif
4421 
__kmp_stg_parse_lock_kind(char const * name,char const * value,void * data)4422 static void __kmp_stg_parse_lock_kind(char const *name, char const *value,
4423                                       void *data) {
4424   if (__kmp_init_user_locks) {
4425     KMP_WARNING(EnvLockWarn, name);
4426     return;
4427   }
4428 
4429   if (__kmp_str_match("tas", 2, value) ||
4430       __kmp_str_match("test and set", 2, value) ||
4431       __kmp_str_match("test_and_set", 2, value) ||
4432       __kmp_str_match("test-and-set", 2, value) ||
4433       __kmp_str_match("test andset", 2, value) ||
4434       __kmp_str_match("test_andset", 2, value) ||
4435       __kmp_str_match("test-andset", 2, value) ||
4436       __kmp_str_match("testand set", 2, value) ||
4437       __kmp_str_match("testand_set", 2, value) ||
4438       __kmp_str_match("testand-set", 2, value) ||
4439       __kmp_str_match("testandset", 2, value)) {
4440     __kmp_user_lock_kind = lk_tas;
4441     KMP_STORE_LOCK_SEQ(tas);
4442   }
4443 #if KMP_USE_FUTEX
4444   else if (__kmp_str_match("futex", 1, value)) {
4445     if (__kmp_futex_determine_capable()) {
4446       __kmp_user_lock_kind = lk_futex;
4447       KMP_STORE_LOCK_SEQ(futex);
4448     } else {
4449       KMP_WARNING(FutexNotSupported, name, value);
4450     }
4451   }
4452 #endif
4453   else if (__kmp_str_match("ticket", 2, value)) {
4454     __kmp_user_lock_kind = lk_ticket;
4455     KMP_STORE_LOCK_SEQ(ticket);
4456   } else if (__kmp_str_match("queuing", 1, value) ||
4457              __kmp_str_match("queue", 1, value)) {
4458     __kmp_user_lock_kind = lk_queuing;
4459     KMP_STORE_LOCK_SEQ(queuing);
4460   } else if (__kmp_str_match("drdpa ticket", 1, value) ||
4461              __kmp_str_match("drdpa_ticket", 1, value) ||
4462              __kmp_str_match("drdpa-ticket", 1, value) ||
4463              __kmp_str_match("drdpaticket", 1, value) ||
4464              __kmp_str_match("drdpa", 1, value)) {
4465     __kmp_user_lock_kind = lk_drdpa;
4466     KMP_STORE_LOCK_SEQ(drdpa);
4467   }
4468 #if KMP_USE_ADAPTIVE_LOCKS
4469   else if (__kmp_str_match("adaptive", 1, value)) {
4470     if (__kmp_cpuinfo.flags.rtm) { // ??? Is cpuinfo available here?
4471       __kmp_user_lock_kind = lk_adaptive;
4472       KMP_STORE_LOCK_SEQ(adaptive);
4473     } else {
4474       KMP_WARNING(AdaptiveNotSupported, name, value);
4475       __kmp_user_lock_kind = lk_queuing;
4476       KMP_STORE_LOCK_SEQ(queuing);
4477     }
4478   }
4479 #endif // KMP_USE_ADAPTIVE_LOCKS
4480 #if KMP_USE_DYNAMIC_LOCK && KMP_USE_TSX
4481   else if (__kmp_str_match("rtm_queuing", 1, value)) {
4482     if (__kmp_cpuinfo.flags.rtm) {
4483       __kmp_user_lock_kind = lk_rtm_queuing;
4484       KMP_STORE_LOCK_SEQ(rtm_queuing);
4485     } else {
4486       KMP_WARNING(AdaptiveNotSupported, name, value);
4487       __kmp_user_lock_kind = lk_queuing;
4488       KMP_STORE_LOCK_SEQ(queuing);
4489     }
4490   } else if (__kmp_str_match("rtm_spin", 1, value)) {
4491     if (__kmp_cpuinfo.flags.rtm) {
4492       __kmp_user_lock_kind = lk_rtm_spin;
4493       KMP_STORE_LOCK_SEQ(rtm_spin);
4494     } else {
4495       KMP_WARNING(AdaptiveNotSupported, name, value);
4496       __kmp_user_lock_kind = lk_tas;
4497       KMP_STORE_LOCK_SEQ(queuing);
4498     }
4499   } else if (__kmp_str_match("hle", 1, value)) {
4500     __kmp_user_lock_kind = lk_hle;
4501     KMP_STORE_LOCK_SEQ(hle);
4502   }
4503 #endif
4504   else {
4505     KMP_WARNING(StgInvalidValue, name, value);
4506   }
4507 }
4508 
__kmp_stg_print_lock_kind(kmp_str_buf_t * buffer,char const * name,void * data)4509 static void __kmp_stg_print_lock_kind(kmp_str_buf_t *buffer, char const *name,
4510                                       void *data) {
4511   const char *value = NULL;
4512 
4513   switch (__kmp_user_lock_kind) {
4514   case lk_default:
4515     value = "default";
4516     break;
4517 
4518   case lk_tas:
4519     value = "tas";
4520     break;
4521 
4522 #if KMP_USE_FUTEX
4523   case lk_futex:
4524     value = "futex";
4525     break;
4526 #endif
4527 
4528 #if KMP_USE_DYNAMIC_LOCK && KMP_USE_TSX
4529   case lk_rtm_queuing:
4530     value = "rtm_queuing";
4531     break;
4532 
4533   case lk_rtm_spin:
4534     value = "rtm_spin";
4535     break;
4536 
4537   case lk_hle:
4538     value = "hle";
4539     break;
4540 #endif
4541 
4542   case lk_ticket:
4543     value = "ticket";
4544     break;
4545 
4546   case lk_queuing:
4547     value = "queuing";
4548     break;
4549 
4550   case lk_drdpa:
4551     value = "drdpa";
4552     break;
4553 #if KMP_USE_ADAPTIVE_LOCKS
4554   case lk_adaptive:
4555     value = "adaptive";
4556     break;
4557 #endif
4558   }
4559 
4560   if (value != NULL) {
4561     __kmp_stg_print_str(buffer, name, value);
4562   }
4563 }
4564 
4565 // -----------------------------------------------------------------------------
4566 // KMP_SPIN_BACKOFF_PARAMS
4567 
4568 // KMP_SPIN_BACKOFF_PARAMS=max_backoff[,min_tick] (max backoff size, min tick
4569 // for machine pause)
__kmp_stg_parse_spin_backoff_params(const char * name,const char * value,void * data)4570 static void __kmp_stg_parse_spin_backoff_params(const char *name,
4571                                                 const char *value, void *data) {
4572   const char *next = value;
4573 
4574   int total = 0; // Count elements that were set. It'll be used as an array size
4575   int prev_comma = FALSE; // For correct processing sequential commas
4576   int i;
4577 
4578   kmp_uint32 max_backoff = __kmp_spin_backoff_params.max_backoff;
4579   kmp_uint32 min_tick = __kmp_spin_backoff_params.min_tick;
4580 
4581   // Run only 3 iterations because it is enough to read two values or find a
4582   // syntax error
4583   for (i = 0; i < 3; i++) {
4584     SKIP_WS(next);
4585 
4586     if (*next == '\0') {
4587       break;
4588     }
4589     // Next character is not an integer or not a comma OR number of values > 2
4590     // => end of list
4591     if (((*next < '0' || *next > '9') && *next != ',') || total > 2) {
4592       KMP_WARNING(EnvSyntaxError, name, value);
4593       return;
4594     }
4595     // The next character is ','
4596     if (*next == ',') {
4597       // ',' is the first character
4598       if (total == 0 || prev_comma) {
4599         total++;
4600       }
4601       prev_comma = TRUE;
4602       next++; // skip ','
4603       SKIP_WS(next);
4604     }
4605     // Next character is a digit
4606     if (*next >= '0' && *next <= '9') {
4607       int num;
4608       const char *buf = next;
4609       char const *msg = NULL;
4610       prev_comma = FALSE;
4611       SKIP_DIGITS(next);
4612       total++;
4613 
4614       const char *tmp = next;
4615       SKIP_WS(tmp);
4616       if ((*next == ' ' || *next == '\t') && (*tmp >= '0' && *tmp <= '9')) {
4617         KMP_WARNING(EnvSpacesNotAllowed, name, value);
4618         return;
4619       }
4620 
4621       num = __kmp_str_to_int(buf, *next);
4622       if (num <= 0) { // The number of retries should be > 0
4623         msg = KMP_I18N_STR(ValueTooSmall);
4624         num = 1;
4625       } else if (num > KMP_INT_MAX) {
4626         msg = KMP_I18N_STR(ValueTooLarge);
4627         num = KMP_INT_MAX;
4628       }
4629       if (msg != NULL) {
4630         // Message is not empty. Print warning.
4631         KMP_WARNING(ParseSizeIntWarn, name, value, msg);
4632         KMP_INFORM(Using_int_Value, name, num);
4633       }
4634       if (total == 1) {
4635         max_backoff = num;
4636       } else if (total == 2) {
4637         min_tick = num;
4638       }
4639     }
4640   }
4641   KMP_DEBUG_ASSERT(total > 0);
4642   if (total <= 0) {
4643     KMP_WARNING(EnvSyntaxError, name, value);
4644     return;
4645   }
4646   __kmp_spin_backoff_params.max_backoff = max_backoff;
4647   __kmp_spin_backoff_params.min_tick = min_tick;
4648 }
4649 
__kmp_stg_print_spin_backoff_params(kmp_str_buf_t * buffer,char const * name,void * data)4650 static void __kmp_stg_print_spin_backoff_params(kmp_str_buf_t *buffer,
4651                                                 char const *name, void *data) {
4652   if (__kmp_env_format) {
4653     KMP_STR_BUF_PRINT_NAME_EX(name);
4654   } else {
4655     __kmp_str_buf_print(buffer, "   %s='", name);
4656   }
4657   __kmp_str_buf_print(buffer, "%d,%d'\n", __kmp_spin_backoff_params.max_backoff,
4658                       __kmp_spin_backoff_params.min_tick);
4659 }
4660 
4661 #if KMP_USE_ADAPTIVE_LOCKS
4662 
4663 // -----------------------------------------------------------------------------
4664 // KMP_ADAPTIVE_LOCK_PROPS, KMP_SPECULATIVE_STATSFILE
4665 
4666 // Parse out values for the tunable parameters from a string of the form
4667 // KMP_ADAPTIVE_LOCK_PROPS=max_soft_retries[,max_badness]
__kmp_stg_parse_adaptive_lock_props(const char * name,const char * value,void * data)4668 static void __kmp_stg_parse_adaptive_lock_props(const char *name,
4669                                                 const char *value, void *data) {
4670   int max_retries = 0;
4671   int max_badness = 0;
4672 
4673   const char *next = value;
4674 
4675   int total = 0; // Count elements that were set. It'll be used as an array size
4676   int prev_comma = FALSE; // For correct processing sequential commas
4677   int i;
4678 
4679   // Save values in the structure __kmp_speculative_backoff_params
4680   // Run only 3 iterations because it is enough to read two values or find a
4681   // syntax error
4682   for (i = 0; i < 3; i++) {
4683     SKIP_WS(next);
4684 
4685     if (*next == '\0') {
4686       break;
4687     }
4688     // Next character is not an integer or not a comma OR number of values > 2
4689     // => end of list
4690     if (((*next < '0' || *next > '9') && *next != ',') || total > 2) {
4691       KMP_WARNING(EnvSyntaxError, name, value);
4692       return;
4693     }
4694     // The next character is ','
4695     if (*next == ',') {
4696       // ',' is the first character
4697       if (total == 0 || prev_comma) {
4698         total++;
4699       }
4700       prev_comma = TRUE;
4701       next++; // skip ','
4702       SKIP_WS(next);
4703     }
4704     // Next character is a digit
4705     if (*next >= '0' && *next <= '9') {
4706       int num;
4707       const char *buf = next;
4708       char const *msg = NULL;
4709       prev_comma = FALSE;
4710       SKIP_DIGITS(next);
4711       total++;
4712 
4713       const char *tmp = next;
4714       SKIP_WS(tmp);
4715       if ((*next == ' ' || *next == '\t') && (*tmp >= '0' && *tmp <= '9')) {
4716         KMP_WARNING(EnvSpacesNotAllowed, name, value);
4717         return;
4718       }
4719 
4720       num = __kmp_str_to_int(buf, *next);
4721       if (num < 0) { // The number of retries should be >= 0
4722         msg = KMP_I18N_STR(ValueTooSmall);
4723         num = 1;
4724       } else if (num > KMP_INT_MAX) {
4725         msg = KMP_I18N_STR(ValueTooLarge);
4726         num = KMP_INT_MAX;
4727       }
4728       if (msg != NULL) {
4729         // Message is not empty. Print warning.
4730         KMP_WARNING(ParseSizeIntWarn, name, value, msg);
4731         KMP_INFORM(Using_int_Value, name, num);
4732       }
4733       if (total == 1) {
4734         max_retries = num;
4735       } else if (total == 2) {
4736         max_badness = num;
4737       }
4738     }
4739   }
4740   KMP_DEBUG_ASSERT(total > 0);
4741   if (total <= 0) {
4742     KMP_WARNING(EnvSyntaxError, name, value);
4743     return;
4744   }
4745   __kmp_adaptive_backoff_params.max_soft_retries = max_retries;
4746   __kmp_adaptive_backoff_params.max_badness = max_badness;
4747 }
4748 
__kmp_stg_print_adaptive_lock_props(kmp_str_buf_t * buffer,char const * name,void * data)4749 static void __kmp_stg_print_adaptive_lock_props(kmp_str_buf_t *buffer,
4750                                                 char const *name, void *data) {
4751   if (__kmp_env_format) {
4752     KMP_STR_BUF_PRINT_NAME_EX(name);
4753   } else {
4754     __kmp_str_buf_print(buffer, "   %s='", name);
4755   }
4756   __kmp_str_buf_print(buffer, "%d,%d'\n",
4757                       __kmp_adaptive_backoff_params.max_soft_retries,
4758                       __kmp_adaptive_backoff_params.max_badness);
4759 } // __kmp_stg_print_adaptive_lock_props
4760 
4761 #if KMP_DEBUG_ADAPTIVE_LOCKS
4762 
__kmp_stg_parse_speculative_statsfile(char const * name,char const * value,void * data)4763 static void __kmp_stg_parse_speculative_statsfile(char const *name,
4764                                                   char const *value,
4765                                                   void *data) {
4766   __kmp_stg_parse_file(name, value, "",
4767                        CCAST(char **, &__kmp_speculative_statsfile));
4768 } // __kmp_stg_parse_speculative_statsfile
4769 
__kmp_stg_print_speculative_statsfile(kmp_str_buf_t * buffer,char const * name,void * data)4770 static void __kmp_stg_print_speculative_statsfile(kmp_str_buf_t *buffer,
4771                                                   char const *name,
4772                                                   void *data) {
4773   if (__kmp_str_match("-", 0, __kmp_speculative_statsfile)) {
4774     __kmp_stg_print_str(buffer, name, "stdout");
4775   } else {
4776     __kmp_stg_print_str(buffer, name, __kmp_speculative_statsfile);
4777   }
4778 
4779 } // __kmp_stg_print_speculative_statsfile
4780 
4781 #endif // KMP_DEBUG_ADAPTIVE_LOCKS
4782 
4783 #endif // KMP_USE_ADAPTIVE_LOCKS
4784 
4785 // -----------------------------------------------------------------------------
4786 // KMP_HW_SUBSET (was KMP_PLACE_THREADS)
4787 // 2s16c,2t => 2S16C,2T => 2S16C \0 2T
4788 
4789 // Return KMP_HW_SUBSET preferred hardware type in case a token is ambiguously
4790 // short. The original KMP_HW_SUBSET environment variable had single letters:
4791 // s, c, t for sockets, cores, threads repsectively.
__kmp_hw_subset_break_tie(const kmp_hw_t * possible,size_t num_possible)4792 static kmp_hw_t __kmp_hw_subset_break_tie(const kmp_hw_t *possible,
4793                                           size_t num_possible) {
4794   for (size_t i = 0; i < num_possible; ++i) {
4795     if (possible[i] == KMP_HW_THREAD)
4796       return KMP_HW_THREAD;
4797     else if (possible[i] == KMP_HW_CORE)
4798       return KMP_HW_CORE;
4799     else if (possible[i] == KMP_HW_SOCKET)
4800       return KMP_HW_SOCKET;
4801   }
4802   return KMP_HW_UNKNOWN;
4803 }
4804 
4805 // Return hardware type from string or HW_UNKNOWN if string cannot be parsed
4806 // This algorithm is very forgiving to the user in that, the instant it can
4807 // reduce the search space to one, it assumes that is the topology level the
4808 // user wanted, even if it is misspelled later in the token.
__kmp_stg_parse_hw_subset_name(char const * token)4809 static kmp_hw_t __kmp_stg_parse_hw_subset_name(char const *token) {
4810   size_t index, num_possible, token_length;
4811   kmp_hw_t possible[KMP_HW_LAST];
4812   const char *end;
4813 
4814   // Find the end of the hardware token string
4815   end = token;
4816   token_length = 0;
4817   while (isalnum(*end) || *end == '_') {
4818     token_length++;
4819     end++;
4820   }
4821 
4822   // Set the possibilities to all hardware types
4823   num_possible = 0;
4824   KMP_FOREACH_HW_TYPE(type) { possible[num_possible++] = type; }
4825 
4826   // Eliminate hardware types by comparing the front of the token
4827   // with hardware names
4828   // In most cases, the first letter in the token will indicate exactly
4829   // which hardware type is parsed, e.g., 'C' = Core
4830   index = 0;
4831   while (num_possible > 1 && index < token_length) {
4832     size_t n = num_possible;
4833     char token_char = (char)toupper(token[index]);
4834     for (size_t i = 0; i < n; ++i) {
4835       const char *s;
4836       kmp_hw_t type = possible[i];
4837       s = __kmp_hw_get_keyword(type, false);
4838       if (index < KMP_STRLEN(s)) {
4839         char c = (char)toupper(s[index]);
4840         // Mark hardware types for removal when the characters do not match
4841         if (c != token_char) {
4842           possible[i] = KMP_HW_UNKNOWN;
4843           num_possible--;
4844         }
4845       }
4846     }
4847     // Remove hardware types that this token cannot be
4848     size_t start = 0;
4849     for (size_t i = 0; i < n; ++i) {
4850       if (possible[i] != KMP_HW_UNKNOWN) {
4851         kmp_hw_t temp = possible[i];
4852         possible[i] = possible[start];
4853         possible[start] = temp;
4854         start++;
4855       }
4856     }
4857     KMP_ASSERT(start == num_possible);
4858     index++;
4859   }
4860 
4861   // Attempt to break a tie if user has very short token
4862   // (e.g., is 'T' tile or thread?)
4863   if (num_possible > 1)
4864     return __kmp_hw_subset_break_tie(possible, num_possible);
4865   if (num_possible == 1)
4866     return possible[0];
4867   return KMP_HW_UNKNOWN;
4868 }
4869 
4870 // The longest observable sequence of items can only be HW_LAST length
4871 // The input string is usually short enough, let's use 512 limit for now
4872 #define MAX_T_LEVEL KMP_HW_LAST
4873 #define MAX_STR_LEN 512
__kmp_stg_parse_hw_subset(char const * name,char const * value,void * data)4874 static void __kmp_stg_parse_hw_subset(char const *name, char const *value,
4875                                       void *data) {
4876   // Value example: 1s,5c@3,2T
4877   // Which means "use 1 socket, 5 cores with offset 3, 2 threads per core"
4878   kmp_setting_t **rivals = (kmp_setting_t **)data;
4879   if (strcmp(name, "KMP_PLACE_THREADS") == 0) {
4880     KMP_INFORM(EnvVarDeprecated, name, "KMP_HW_SUBSET");
4881   }
4882   if (__kmp_stg_check_rivals(name, value, rivals)) {
4883     return;
4884   }
4885 
4886   char *components[MAX_T_LEVEL];
4887   char const *digits = "0123456789";
4888   char input[MAX_STR_LEN];
4889   size_t len = 0, mlen = MAX_STR_LEN;
4890   int level = 0;
4891   bool absolute = false;
4892   // Canonicalize the string (remove spaces, unify delimiters, etc.)
4893   char *pos = CCAST(char *, value);
4894   while (*pos && mlen) {
4895     if (*pos != ' ') { // skip spaces
4896       if (len == 0 && *pos == ':') {
4897         absolute = true;
4898       } else {
4899         input[len] = (char)(toupper(*pos));
4900         if (input[len] == 'X')
4901           input[len] = ','; // unify delimiters of levels
4902         if (input[len] == 'O' && strchr(digits, *(pos + 1)))
4903           input[len] = '@'; // unify delimiters of offset
4904         len++;
4905       }
4906     }
4907     mlen--;
4908     pos++;
4909   }
4910   if (len == 0 || mlen == 0) {
4911     goto err; // contents is either empty or too long
4912   }
4913   input[len] = '\0';
4914   // Split by delimiter
4915   pos = input;
4916   components[level++] = pos;
4917   while ((pos = strchr(pos, ','))) {
4918     if (level >= MAX_T_LEVEL)
4919       goto err; // too many components provided
4920     *pos = '\0'; // modify input and avoid more copying
4921     components[level++] = ++pos; // expect something after ","
4922   }
4923 
4924   __kmp_hw_subset = kmp_hw_subset_t::allocate();
4925   if (absolute)
4926     __kmp_hw_subset->set_absolute();
4927 
4928   // Check each component
4929   for (int i = 0; i < level; ++i) {
4930     int offset = 0;
4931     int num = atoi(components[i]); // each component should start with a number
4932     if (num <= 0) {
4933       goto err; // only positive integers are valid for count
4934     }
4935     if ((pos = strchr(components[i], '@'))) {
4936       offset = atoi(pos + 1); // save offset
4937       *pos = '\0'; // cut the offset from the component
4938     }
4939     pos = components[i] + strspn(components[i], digits);
4940     if (pos == components[i]) {
4941       goto err;
4942     }
4943     // detect the component type
4944     kmp_hw_t type = __kmp_stg_parse_hw_subset_name(pos);
4945     if (type == KMP_HW_UNKNOWN) {
4946       goto err;
4947     }
4948     if (__kmp_hw_subset->specified(type)) {
4949       goto err;
4950     }
4951     __kmp_hw_subset->push_back(num, type, offset);
4952   }
4953   return;
4954 err:
4955   KMP_WARNING(AffHWSubsetInvalid, name, value);
4956   if (__kmp_hw_subset) {
4957     kmp_hw_subset_t::deallocate(__kmp_hw_subset);
4958     __kmp_hw_subset = nullptr;
4959   }
4960   return;
4961 }
4962 
__kmp_stg_print_hw_subset(kmp_str_buf_t * buffer,char const * name,void * data)4963 static void __kmp_stg_print_hw_subset(kmp_str_buf_t *buffer, char const *name,
4964                                       void *data) {
4965   kmp_str_buf_t buf;
4966   int depth;
4967   if (!__kmp_hw_subset)
4968     return;
4969   __kmp_str_buf_init(&buf);
4970   if (__kmp_env_format)
4971     KMP_STR_BUF_PRINT_NAME_EX(name);
4972   else
4973     __kmp_str_buf_print(buffer, "   %s='", name);
4974 
4975   depth = __kmp_hw_subset->get_depth();
4976   for (int i = 0; i < depth; ++i) {
4977     const auto &item = __kmp_hw_subset->at(i);
4978     __kmp_str_buf_print(&buf, "%s%d%s", (i > 0 ? "," : ""), item.num,
4979                         __kmp_hw_get_keyword(item.type));
4980     if (item.offset)
4981       __kmp_str_buf_print(&buf, "@%d", item.offset);
4982   }
4983   __kmp_str_buf_print(buffer, "%s'\n", buf.str);
4984   __kmp_str_buf_free(&buf);
4985 }
4986 
4987 #if USE_ITT_BUILD
4988 // -----------------------------------------------------------------------------
4989 // KMP_FORKJOIN_FRAMES
4990 
__kmp_stg_parse_forkjoin_frames(char const * name,char const * value,void * data)4991 static void __kmp_stg_parse_forkjoin_frames(char const *name, char const *value,
4992                                             void *data) {
4993   __kmp_stg_parse_bool(name, value, &__kmp_forkjoin_frames);
4994 } // __kmp_stg_parse_forkjoin_frames
4995 
__kmp_stg_print_forkjoin_frames(kmp_str_buf_t * buffer,char const * name,void * data)4996 static void __kmp_stg_print_forkjoin_frames(kmp_str_buf_t *buffer,
4997                                             char const *name, void *data) {
4998   __kmp_stg_print_bool(buffer, name, __kmp_forkjoin_frames);
4999 } // __kmp_stg_print_forkjoin_frames
5000 
5001 // -----------------------------------------------------------------------------
5002 // KMP_FORKJOIN_FRAMES_MODE
5003 
__kmp_stg_parse_forkjoin_frames_mode(char const * name,char const * value,void * data)5004 static void __kmp_stg_parse_forkjoin_frames_mode(char const *name,
5005                                                  char const *value,
5006                                                  void *data) {
5007   __kmp_stg_parse_int(name, value, 0, 3, &__kmp_forkjoin_frames_mode);
5008 } // __kmp_stg_parse_forkjoin_frames
5009 
__kmp_stg_print_forkjoin_frames_mode(kmp_str_buf_t * buffer,char const * name,void * data)5010 static void __kmp_stg_print_forkjoin_frames_mode(kmp_str_buf_t *buffer,
5011                                                  char const *name, void *data) {
5012   __kmp_stg_print_int(buffer, name, __kmp_forkjoin_frames_mode);
5013 } // __kmp_stg_print_forkjoin_frames
5014 #endif /* USE_ITT_BUILD */
5015 
5016 // -----------------------------------------------------------------------------
5017 // KMP_ENABLE_TASK_THROTTLING
5018 
__kmp_stg_parse_task_throttling(char const * name,char const * value,void * data)5019 static void __kmp_stg_parse_task_throttling(char const *name, char const *value,
5020                                             void *data) {
5021   __kmp_stg_parse_bool(name, value, &__kmp_enable_task_throttling);
5022 } // __kmp_stg_parse_task_throttling
5023 
__kmp_stg_print_task_throttling(kmp_str_buf_t * buffer,char const * name,void * data)5024 static void __kmp_stg_print_task_throttling(kmp_str_buf_t *buffer,
5025                                             char const *name, void *data) {
5026   __kmp_stg_print_bool(buffer, name, __kmp_enable_task_throttling);
5027 } // __kmp_stg_print_task_throttling
5028 
5029 #if KMP_HAVE_MWAIT || KMP_HAVE_UMWAIT
5030 // -----------------------------------------------------------------------------
5031 // KMP_USER_LEVEL_MWAIT
5032 
__kmp_stg_parse_user_level_mwait(char const * name,char const * value,void * data)5033 static void __kmp_stg_parse_user_level_mwait(char const *name,
5034                                              char const *value, void *data) {
5035   __kmp_stg_parse_bool(name, value, &__kmp_user_level_mwait);
5036 } // __kmp_stg_parse_user_level_mwait
5037 
__kmp_stg_print_user_level_mwait(kmp_str_buf_t * buffer,char const * name,void * data)5038 static void __kmp_stg_print_user_level_mwait(kmp_str_buf_t *buffer,
5039                                              char const *name, void *data) {
5040   __kmp_stg_print_bool(buffer, name, __kmp_user_level_mwait);
5041 } // __kmp_stg_print_user_level_mwait
5042 
5043 // -----------------------------------------------------------------------------
5044 // KMP_MWAIT_HINTS
5045 
__kmp_stg_parse_mwait_hints(char const * name,char const * value,void * data)5046 static void __kmp_stg_parse_mwait_hints(char const *name, char const *value,
5047                                         void *data) {
5048   __kmp_stg_parse_int(name, value, 0, INT_MAX, &__kmp_mwait_hints);
5049 } // __kmp_stg_parse_mwait_hints
5050 
__kmp_stg_print_mwait_hints(kmp_str_buf_t * buffer,char const * name,void * data)5051 static void __kmp_stg_print_mwait_hints(kmp_str_buf_t *buffer, char const *name,
5052                                         void *data) {
5053   __kmp_stg_print_int(buffer, name, __kmp_mwait_hints);
5054 } // __kmp_stg_print_mwait_hints
5055 
5056 #endif // KMP_HAVE_MWAIT || KMP_HAVE_UMWAIT
5057 
5058 // -----------------------------------------------------------------------------
5059 // OMP_DISPLAY_ENV
5060 
__kmp_stg_parse_omp_display_env(char const * name,char const * value,void * data)5061 static void __kmp_stg_parse_omp_display_env(char const *name, char const *value,
5062                                             void *data) {
5063   if (__kmp_str_match("VERBOSE", 1, value)) {
5064     __kmp_display_env_verbose = TRUE;
5065   } else {
5066     __kmp_stg_parse_bool(name, value, &__kmp_display_env);
5067   }
5068 } // __kmp_stg_parse_omp_display_env
5069 
__kmp_stg_print_omp_display_env(kmp_str_buf_t * buffer,char const * name,void * data)5070 static void __kmp_stg_print_omp_display_env(kmp_str_buf_t *buffer,
5071                                             char const *name, void *data) {
5072   if (__kmp_display_env_verbose) {
5073     __kmp_stg_print_str(buffer, name, "VERBOSE");
5074   } else {
5075     __kmp_stg_print_bool(buffer, name, __kmp_display_env);
5076   }
5077 } // __kmp_stg_print_omp_display_env
5078 
__kmp_stg_parse_omp_cancellation(char const * name,char const * value,void * data)5079 static void __kmp_stg_parse_omp_cancellation(char const *name,
5080                                              char const *value, void *data) {
5081   if (TCR_4(__kmp_init_parallel)) {
5082     KMP_WARNING(EnvParallelWarn, name);
5083     return;
5084   } // read value before first parallel only
5085   __kmp_stg_parse_bool(name, value, &__kmp_omp_cancellation);
5086 } // __kmp_stg_parse_omp_cancellation
5087 
__kmp_stg_print_omp_cancellation(kmp_str_buf_t * buffer,char const * name,void * data)5088 static void __kmp_stg_print_omp_cancellation(kmp_str_buf_t *buffer,
5089                                              char const *name, void *data) {
5090   __kmp_stg_print_bool(buffer, name, __kmp_omp_cancellation);
5091 } // __kmp_stg_print_omp_cancellation
5092 
5093 #if OMPT_SUPPORT
5094 int __kmp_tool = 1;
5095 
__kmp_stg_parse_omp_tool(char const * name,char const * value,void * data)5096 static void __kmp_stg_parse_omp_tool(char const *name, char const *value,
5097                                      void *data) {
5098   __kmp_stg_parse_bool(name, value, &__kmp_tool);
5099 } // __kmp_stg_parse_omp_tool
5100 
__kmp_stg_print_omp_tool(kmp_str_buf_t * buffer,char const * name,void * data)5101 static void __kmp_stg_print_omp_tool(kmp_str_buf_t *buffer, char const *name,
5102                                      void *data) {
5103   if (__kmp_env_format) {
5104     KMP_STR_BUF_PRINT_BOOL_EX(name, __kmp_tool, "enabled", "disabled");
5105   } else {
5106     __kmp_str_buf_print(buffer, "   %s=%s\n", name,
5107                         __kmp_tool ? "enabled" : "disabled");
5108   }
5109 } // __kmp_stg_print_omp_tool
5110 
5111 char *__kmp_tool_libraries = NULL;
5112 
__kmp_stg_parse_omp_tool_libraries(char const * name,char const * value,void * data)5113 static void __kmp_stg_parse_omp_tool_libraries(char const *name,
5114                                                char const *value, void *data) {
5115   __kmp_stg_parse_str(name, value, &__kmp_tool_libraries);
5116 } // __kmp_stg_parse_omp_tool_libraries
5117 
__kmp_stg_print_omp_tool_libraries(kmp_str_buf_t * buffer,char const * name,void * data)5118 static void __kmp_stg_print_omp_tool_libraries(kmp_str_buf_t *buffer,
5119                                                char const *name, void *data) {
5120   if (__kmp_tool_libraries)
5121     __kmp_stg_print_str(buffer, name, __kmp_tool_libraries);
5122   else {
5123     if (__kmp_env_format) {
5124       KMP_STR_BUF_PRINT_NAME;
5125     } else {
5126       __kmp_str_buf_print(buffer, "   %s", name);
5127     }
5128     __kmp_str_buf_print(buffer, ": %s\n", KMP_I18N_STR(NotDefined));
5129   }
5130 } // __kmp_stg_print_omp_tool_libraries
5131 
5132 char *__kmp_tool_verbose_init = NULL;
5133 
__kmp_stg_parse_omp_tool_verbose_init(char const * name,char const * value,void * data)5134 static void __kmp_stg_parse_omp_tool_verbose_init(char const *name,
5135                                                   char const *value,
5136                                                   void *data) {
5137   __kmp_stg_parse_str(name, value, &__kmp_tool_verbose_init);
5138 } // __kmp_stg_parse_omp_tool_libraries
5139 
__kmp_stg_print_omp_tool_verbose_init(kmp_str_buf_t * buffer,char const * name,void * data)5140 static void __kmp_stg_print_omp_tool_verbose_init(kmp_str_buf_t *buffer,
5141                                                   char const *name,
5142                                                   void *data) {
5143   if (__kmp_tool_verbose_init)
5144     __kmp_stg_print_str(buffer, name, __kmp_tool_verbose_init);
5145   else {
5146     if (__kmp_env_format) {
5147       KMP_STR_BUF_PRINT_NAME;
5148     } else {
5149       __kmp_str_buf_print(buffer, "   %s", name);
5150     }
5151     __kmp_str_buf_print(buffer, ": %s\n", KMP_I18N_STR(NotDefined));
5152   }
5153 } // __kmp_stg_print_omp_tool_verbose_init
5154 
5155 #endif
5156 
5157 // Table.
5158 
5159 static kmp_setting_t __kmp_stg_table[] = {
5160 
5161     {"KMP_ALL_THREADS", __kmp_stg_parse_device_thread_limit, NULL, NULL, 0, 0},
5162     {"KMP_BLOCKTIME", __kmp_stg_parse_blocktime, __kmp_stg_print_blocktime,
5163      NULL, 0, 0},
5164     {"KMP_USE_YIELD", __kmp_stg_parse_use_yield, __kmp_stg_print_use_yield,
5165      NULL, 0, 0},
5166     {"KMP_DUPLICATE_LIB_OK", __kmp_stg_parse_duplicate_lib_ok,
5167      __kmp_stg_print_duplicate_lib_ok, NULL, 0, 0},
5168     {"KMP_LIBRARY", __kmp_stg_parse_wait_policy, __kmp_stg_print_wait_policy,
5169      NULL, 0, 0},
5170     {"KMP_DEVICE_THREAD_LIMIT", __kmp_stg_parse_device_thread_limit,
5171      __kmp_stg_print_device_thread_limit, NULL, 0, 0},
5172 #if KMP_USE_MONITOR
5173     {"KMP_MONITOR_STACKSIZE", __kmp_stg_parse_monitor_stacksize,
5174      __kmp_stg_print_monitor_stacksize, NULL, 0, 0},
5175 #endif
5176     {"KMP_SETTINGS", __kmp_stg_parse_settings, __kmp_stg_print_settings, NULL,
5177      0, 0},
5178     {"KMP_STACKOFFSET", __kmp_stg_parse_stackoffset,
5179      __kmp_stg_print_stackoffset, NULL, 0, 0},
5180     {"KMP_STACKSIZE", __kmp_stg_parse_stacksize, __kmp_stg_print_stacksize,
5181      NULL, 0, 0},
5182     {"KMP_STACKPAD", __kmp_stg_parse_stackpad, __kmp_stg_print_stackpad, NULL,
5183      0, 0},
5184     {"KMP_VERSION", __kmp_stg_parse_version, __kmp_stg_print_version, NULL, 0,
5185      0},
5186     {"KMP_WARNINGS", __kmp_stg_parse_warnings, __kmp_stg_print_warnings, NULL,
5187      0, 0},
5188 
5189     {"KMP_NESTING_MODE", __kmp_stg_parse_nesting_mode,
5190      __kmp_stg_print_nesting_mode, NULL, 0, 0},
5191     {"OMP_NESTED", __kmp_stg_parse_nested, __kmp_stg_print_nested, NULL, 0, 0},
5192     {"OMP_NUM_THREADS", __kmp_stg_parse_num_threads,
5193      __kmp_stg_print_num_threads, NULL, 0, 0},
5194     {"OMP_STACKSIZE", __kmp_stg_parse_stacksize, __kmp_stg_print_stacksize,
5195      NULL, 0, 0},
5196 
5197     {"KMP_TASKING", __kmp_stg_parse_tasking, __kmp_stg_print_tasking, NULL, 0,
5198      0},
5199     {"KMP_TASK_STEALING_CONSTRAINT", __kmp_stg_parse_task_stealing,
5200      __kmp_stg_print_task_stealing, NULL, 0, 0},
5201     {"OMP_MAX_ACTIVE_LEVELS", __kmp_stg_parse_max_active_levels,
5202      __kmp_stg_print_max_active_levels, NULL, 0, 0},
5203     {"OMP_DEFAULT_DEVICE", __kmp_stg_parse_default_device,
5204      __kmp_stg_print_default_device, NULL, 0, 0},
5205     {"OMP_TARGET_OFFLOAD", __kmp_stg_parse_target_offload,
5206      __kmp_stg_print_target_offload, NULL, 0, 0},
5207     {"OMP_MAX_TASK_PRIORITY", __kmp_stg_parse_max_task_priority,
5208      __kmp_stg_print_max_task_priority, NULL, 0, 0},
5209     {"KMP_TASKLOOP_MIN_TASKS", __kmp_stg_parse_taskloop_min_tasks,
5210      __kmp_stg_print_taskloop_min_tasks, NULL, 0, 0},
5211     {"OMP_THREAD_LIMIT", __kmp_stg_parse_thread_limit,
5212      __kmp_stg_print_thread_limit, NULL, 0, 0},
5213     {"KMP_TEAMS_THREAD_LIMIT", __kmp_stg_parse_teams_thread_limit,
5214      __kmp_stg_print_teams_thread_limit, NULL, 0, 0},
5215     {"OMP_NUM_TEAMS", __kmp_stg_parse_nteams, __kmp_stg_print_nteams, NULL, 0,
5216      0},
5217     {"OMP_TEAMS_THREAD_LIMIT", __kmp_stg_parse_teams_th_limit,
5218      __kmp_stg_print_teams_th_limit, NULL, 0, 0},
5219     {"OMP_WAIT_POLICY", __kmp_stg_parse_wait_policy,
5220      __kmp_stg_print_wait_policy, NULL, 0, 0},
5221     {"KMP_DISP_NUM_BUFFERS", __kmp_stg_parse_disp_buffers,
5222      __kmp_stg_print_disp_buffers, NULL, 0, 0},
5223 #if KMP_NESTED_HOT_TEAMS
5224     {"KMP_HOT_TEAMS_MAX_LEVEL", __kmp_stg_parse_hot_teams_level,
5225      __kmp_stg_print_hot_teams_level, NULL, 0, 0},
5226     {"KMP_HOT_TEAMS_MODE", __kmp_stg_parse_hot_teams_mode,
5227      __kmp_stg_print_hot_teams_mode, NULL, 0, 0},
5228 #endif // KMP_NESTED_HOT_TEAMS
5229 
5230 #if KMP_HANDLE_SIGNALS
5231     {"KMP_HANDLE_SIGNALS", __kmp_stg_parse_handle_signals,
5232      __kmp_stg_print_handle_signals, NULL, 0, 0},
5233 #endif
5234 
5235 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
5236     {"KMP_INHERIT_FP_CONTROL", __kmp_stg_parse_inherit_fp_control,
5237      __kmp_stg_print_inherit_fp_control, NULL, 0, 0},
5238 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
5239 
5240 #ifdef KMP_GOMP_COMPAT
5241     {"GOMP_STACKSIZE", __kmp_stg_parse_stacksize, NULL, NULL, 0, 0},
5242 #endif
5243 
5244 #ifdef KMP_DEBUG
5245     {"KMP_A_DEBUG", __kmp_stg_parse_a_debug, __kmp_stg_print_a_debug, NULL, 0,
5246      0},
5247     {"KMP_B_DEBUG", __kmp_stg_parse_b_debug, __kmp_stg_print_b_debug, NULL, 0,
5248      0},
5249     {"KMP_C_DEBUG", __kmp_stg_parse_c_debug, __kmp_stg_print_c_debug, NULL, 0,
5250      0},
5251     {"KMP_D_DEBUG", __kmp_stg_parse_d_debug, __kmp_stg_print_d_debug, NULL, 0,
5252      0},
5253     {"KMP_E_DEBUG", __kmp_stg_parse_e_debug, __kmp_stg_print_e_debug, NULL, 0,
5254      0},
5255     {"KMP_F_DEBUG", __kmp_stg_parse_f_debug, __kmp_stg_print_f_debug, NULL, 0,
5256      0},
5257     {"KMP_DEBUG", __kmp_stg_parse_debug, NULL, /* no print */ NULL, 0, 0},
5258     {"KMP_DEBUG_BUF", __kmp_stg_parse_debug_buf, __kmp_stg_print_debug_buf,
5259      NULL, 0, 0},
5260     {"KMP_DEBUG_BUF_ATOMIC", __kmp_stg_parse_debug_buf_atomic,
5261      __kmp_stg_print_debug_buf_atomic, NULL, 0, 0},
5262     {"KMP_DEBUG_BUF_CHARS", __kmp_stg_parse_debug_buf_chars,
5263      __kmp_stg_print_debug_buf_chars, NULL, 0, 0},
5264     {"KMP_DEBUG_BUF_LINES", __kmp_stg_parse_debug_buf_lines,
5265      __kmp_stg_print_debug_buf_lines, NULL, 0, 0},
5266     {"KMP_DIAG", __kmp_stg_parse_diag, __kmp_stg_print_diag, NULL, 0, 0},
5267 
5268     {"KMP_PAR_RANGE", __kmp_stg_parse_par_range_env,
5269      __kmp_stg_print_par_range_env, NULL, 0, 0},
5270 #endif // KMP_DEBUG
5271 
5272     {"KMP_ALIGN_ALLOC", __kmp_stg_parse_align_alloc,
5273      __kmp_stg_print_align_alloc, NULL, 0, 0},
5274 
5275     {"KMP_PLAIN_BARRIER", __kmp_stg_parse_barrier_branch_bit,
5276      __kmp_stg_print_barrier_branch_bit, NULL, 0, 0},
5277     {"KMP_PLAIN_BARRIER_PATTERN", __kmp_stg_parse_barrier_pattern,
5278      __kmp_stg_print_barrier_pattern, NULL, 0, 0},
5279     {"KMP_FORKJOIN_BARRIER", __kmp_stg_parse_barrier_branch_bit,
5280      __kmp_stg_print_barrier_branch_bit, NULL, 0, 0},
5281     {"KMP_FORKJOIN_BARRIER_PATTERN", __kmp_stg_parse_barrier_pattern,
5282      __kmp_stg_print_barrier_pattern, NULL, 0, 0},
5283 #if KMP_FAST_REDUCTION_BARRIER
5284     {"KMP_REDUCTION_BARRIER", __kmp_stg_parse_barrier_branch_bit,
5285      __kmp_stg_print_barrier_branch_bit, NULL, 0, 0},
5286     {"KMP_REDUCTION_BARRIER_PATTERN", __kmp_stg_parse_barrier_pattern,
5287      __kmp_stg_print_barrier_pattern, NULL, 0, 0},
5288 #endif
5289 
5290     {"KMP_ABORT_DELAY", __kmp_stg_parse_abort_delay,
5291      __kmp_stg_print_abort_delay, NULL, 0, 0},
5292     {"KMP_CPUINFO_FILE", __kmp_stg_parse_cpuinfo_file,
5293      __kmp_stg_print_cpuinfo_file, NULL, 0, 0},
5294     {"KMP_FORCE_REDUCTION", __kmp_stg_parse_force_reduction,
5295      __kmp_stg_print_force_reduction, NULL, 0, 0},
5296     {"KMP_DETERMINISTIC_REDUCTION", __kmp_stg_parse_force_reduction,
5297      __kmp_stg_print_force_reduction, NULL, 0, 0},
5298     {"KMP_STORAGE_MAP", __kmp_stg_parse_storage_map,
5299      __kmp_stg_print_storage_map, NULL, 0, 0},
5300     {"KMP_ALL_THREADPRIVATE", __kmp_stg_parse_all_threadprivate,
5301      __kmp_stg_print_all_threadprivate, NULL, 0, 0},
5302     {"KMP_FOREIGN_THREADS_THREADPRIVATE",
5303      __kmp_stg_parse_foreign_threads_threadprivate,
5304      __kmp_stg_print_foreign_threads_threadprivate, NULL, 0, 0},
5305 
5306 #if KMP_AFFINITY_SUPPORTED
5307     {"KMP_AFFINITY", __kmp_stg_parse_affinity, __kmp_stg_print_affinity, NULL,
5308      0, 0},
5309 #ifdef KMP_GOMP_COMPAT
5310     {"GOMP_CPU_AFFINITY", __kmp_stg_parse_gomp_cpu_affinity, NULL,
5311      /* no print */ NULL, 0, 0},
5312 #endif /* KMP_GOMP_COMPAT */
5313     {"OMP_PROC_BIND", __kmp_stg_parse_proc_bind, __kmp_stg_print_proc_bind,
5314      NULL, 0, 0},
5315     {"OMP_PLACES", __kmp_stg_parse_places, __kmp_stg_print_places, NULL, 0, 0},
5316     {"KMP_TOPOLOGY_METHOD", __kmp_stg_parse_topology_method,
5317      __kmp_stg_print_topology_method, NULL, 0, 0},
5318 
5319 #else
5320 
5321     // KMP_AFFINITY is not supported on OS X*, nor is OMP_PLACES.
5322     // OMP_PROC_BIND and proc-bind-var are supported, however.
5323     {"OMP_PROC_BIND", __kmp_stg_parse_proc_bind, __kmp_stg_print_proc_bind,
5324      NULL, 0, 0},
5325 
5326 #endif // KMP_AFFINITY_SUPPORTED
5327     {"OMP_DISPLAY_AFFINITY", __kmp_stg_parse_display_affinity,
5328      __kmp_stg_print_display_affinity, NULL, 0, 0},
5329     {"OMP_AFFINITY_FORMAT", __kmp_stg_parse_affinity_format,
5330      __kmp_stg_print_affinity_format, NULL, 0, 0},
5331     {"KMP_INIT_AT_FORK", __kmp_stg_parse_init_at_fork,
5332      __kmp_stg_print_init_at_fork, NULL, 0, 0},
5333     {"KMP_SCHEDULE", __kmp_stg_parse_schedule, __kmp_stg_print_schedule, NULL,
5334      0, 0},
5335     {"OMP_SCHEDULE", __kmp_stg_parse_omp_schedule, __kmp_stg_print_omp_schedule,
5336      NULL, 0, 0},
5337 #if KMP_USE_HIER_SCHED
5338     {"KMP_DISP_HAND_THREAD", __kmp_stg_parse_kmp_hand_thread,
5339      __kmp_stg_print_kmp_hand_thread, NULL, 0, 0},
5340 #endif
5341     {"KMP_FORCE_MONOTONIC_DYNAMIC_SCHEDULE",
5342      __kmp_stg_parse_kmp_force_monotonic, __kmp_stg_print_kmp_force_monotonic,
5343      NULL, 0, 0},
5344     {"KMP_ATOMIC_MODE", __kmp_stg_parse_atomic_mode,
5345      __kmp_stg_print_atomic_mode, NULL, 0, 0},
5346     {"KMP_CONSISTENCY_CHECK", __kmp_stg_parse_consistency_check,
5347      __kmp_stg_print_consistency_check, NULL, 0, 0},
5348 
5349 #if USE_ITT_BUILD && USE_ITT_NOTIFY
5350     {"KMP_ITT_PREPARE_DELAY", __kmp_stg_parse_itt_prepare_delay,
5351      __kmp_stg_print_itt_prepare_delay, NULL, 0, 0},
5352 #endif /* USE_ITT_BUILD && USE_ITT_NOTIFY */
5353     {"KMP_MALLOC_POOL_INCR", __kmp_stg_parse_malloc_pool_incr,
5354      __kmp_stg_print_malloc_pool_incr, NULL, 0, 0},
5355     {"KMP_GTID_MODE", __kmp_stg_parse_gtid_mode, __kmp_stg_print_gtid_mode,
5356      NULL, 0, 0},
5357     {"OMP_DYNAMIC", __kmp_stg_parse_omp_dynamic, __kmp_stg_print_omp_dynamic,
5358      NULL, 0, 0},
5359     {"KMP_DYNAMIC_MODE", __kmp_stg_parse_kmp_dynamic_mode,
5360      __kmp_stg_print_kmp_dynamic_mode, NULL, 0, 0},
5361 
5362 #ifdef USE_LOAD_BALANCE
5363     {"KMP_LOAD_BALANCE_INTERVAL", __kmp_stg_parse_ld_balance_interval,
5364      __kmp_stg_print_ld_balance_interval, NULL, 0, 0},
5365 #endif
5366 
5367     {"KMP_NUM_LOCKS_IN_BLOCK", __kmp_stg_parse_lock_block,
5368      __kmp_stg_print_lock_block, NULL, 0, 0},
5369     {"KMP_LOCK_KIND", __kmp_stg_parse_lock_kind, __kmp_stg_print_lock_kind,
5370      NULL, 0, 0},
5371     {"KMP_SPIN_BACKOFF_PARAMS", __kmp_stg_parse_spin_backoff_params,
5372      __kmp_stg_print_spin_backoff_params, NULL, 0, 0},
5373 #if KMP_USE_ADAPTIVE_LOCKS
5374     {"KMP_ADAPTIVE_LOCK_PROPS", __kmp_stg_parse_adaptive_lock_props,
5375      __kmp_stg_print_adaptive_lock_props, NULL, 0, 0},
5376 #if KMP_DEBUG_ADAPTIVE_LOCKS
5377     {"KMP_SPECULATIVE_STATSFILE", __kmp_stg_parse_speculative_statsfile,
5378      __kmp_stg_print_speculative_statsfile, NULL, 0, 0},
5379 #endif
5380 #endif // KMP_USE_ADAPTIVE_LOCKS
5381     {"KMP_PLACE_THREADS", __kmp_stg_parse_hw_subset, __kmp_stg_print_hw_subset,
5382      NULL, 0, 0},
5383     {"KMP_HW_SUBSET", __kmp_stg_parse_hw_subset, __kmp_stg_print_hw_subset,
5384      NULL, 0, 0},
5385 #if USE_ITT_BUILD
5386     {"KMP_FORKJOIN_FRAMES", __kmp_stg_parse_forkjoin_frames,
5387      __kmp_stg_print_forkjoin_frames, NULL, 0, 0},
5388     {"KMP_FORKJOIN_FRAMES_MODE", __kmp_stg_parse_forkjoin_frames_mode,
5389      __kmp_stg_print_forkjoin_frames_mode, NULL, 0, 0},
5390 #endif
5391     {"KMP_ENABLE_TASK_THROTTLING", __kmp_stg_parse_task_throttling,
5392      __kmp_stg_print_task_throttling, NULL, 0, 0},
5393 
5394     {"OMP_DISPLAY_ENV", __kmp_stg_parse_omp_display_env,
5395      __kmp_stg_print_omp_display_env, NULL, 0, 0},
5396     {"OMP_CANCELLATION", __kmp_stg_parse_omp_cancellation,
5397      __kmp_stg_print_omp_cancellation, NULL, 0, 0},
5398     {"OMP_ALLOCATOR", __kmp_stg_parse_allocator, __kmp_stg_print_allocator,
5399      NULL, 0, 0},
5400     {"LIBOMP_USE_HIDDEN_HELPER_TASK", __kmp_stg_parse_use_hidden_helper,
5401      __kmp_stg_print_use_hidden_helper, NULL, 0, 0},
5402     {"LIBOMP_NUM_HIDDEN_HELPER_THREADS",
5403      __kmp_stg_parse_num_hidden_helper_threads,
5404      __kmp_stg_print_num_hidden_helper_threads, NULL, 0, 0},
5405 
5406 #if OMPT_SUPPORT
5407     {"OMP_TOOL", __kmp_stg_parse_omp_tool, __kmp_stg_print_omp_tool, NULL, 0,
5408      0},
5409     {"OMP_TOOL_LIBRARIES", __kmp_stg_parse_omp_tool_libraries,
5410      __kmp_stg_print_omp_tool_libraries, NULL, 0, 0},
5411     {"OMP_TOOL_VERBOSE_INIT", __kmp_stg_parse_omp_tool_verbose_init,
5412      __kmp_stg_print_omp_tool_verbose_init, NULL, 0, 0},
5413 #endif
5414 
5415 #if KMP_HAVE_MWAIT || KMP_HAVE_UMWAIT
5416     {"KMP_USER_LEVEL_MWAIT", __kmp_stg_parse_user_level_mwait,
5417      __kmp_stg_print_user_level_mwait, NULL, 0, 0},
5418     {"KMP_MWAIT_HINTS", __kmp_stg_parse_mwait_hints,
5419      __kmp_stg_print_mwait_hints, NULL, 0, 0},
5420 #endif
5421     {"", NULL, NULL, NULL, 0, 0}}; // settings
5422 
5423 static int const __kmp_stg_count =
5424     sizeof(__kmp_stg_table) / sizeof(kmp_setting_t);
5425 
__kmp_stg_find(char const * name)5426 static inline kmp_setting_t *__kmp_stg_find(char const *name) {
5427 
5428   int i;
5429   if (name != NULL) {
5430     for (i = 0; i < __kmp_stg_count; ++i) {
5431       if (strcmp(__kmp_stg_table[i].name, name) == 0) {
5432         return &__kmp_stg_table[i];
5433       }
5434     }
5435   }
5436   return NULL;
5437 
5438 } // __kmp_stg_find
5439 
__kmp_stg_cmp(void const * _a,void const * _b)5440 static int __kmp_stg_cmp(void const *_a, void const *_b) {
5441   const kmp_setting_t *a = RCAST(const kmp_setting_t *, _a);
5442   const kmp_setting_t *b = RCAST(const kmp_setting_t *, _b);
5443 
5444   // Process KMP_AFFINITY last.
5445   // It needs to come after OMP_PLACES and GOMP_CPU_AFFINITY.
5446   if (strcmp(a->name, "KMP_AFFINITY") == 0) {
5447     if (strcmp(b->name, "KMP_AFFINITY") == 0) {
5448       return 0;
5449     }
5450     return 1;
5451   } else if (strcmp(b->name, "KMP_AFFINITY") == 0) {
5452     return -1;
5453   }
5454   return strcmp(a->name, b->name);
5455 } // __kmp_stg_cmp
5456 
__kmp_stg_init(void)5457 static void __kmp_stg_init(void) {
5458 
5459   static int initialized = 0;
5460 
5461   if (!initialized) {
5462 
5463     // Sort table.
5464     qsort(__kmp_stg_table, __kmp_stg_count - 1, sizeof(kmp_setting_t),
5465           __kmp_stg_cmp);
5466 
5467     { // Initialize *_STACKSIZE data.
5468       kmp_setting_t *kmp_stacksize =
5469           __kmp_stg_find("KMP_STACKSIZE"); // 1st priority.
5470 #ifdef KMP_GOMP_COMPAT
5471       kmp_setting_t *gomp_stacksize =
5472           __kmp_stg_find("GOMP_STACKSIZE"); // 2nd priority.
5473 #endif
5474       kmp_setting_t *omp_stacksize =
5475           __kmp_stg_find("OMP_STACKSIZE"); // 3rd priority.
5476 
5477       // !!! volatile keyword is Intel(R) C Compiler bug CQ49908 workaround.
5478       // !!! Compiler does not understand rivals is used and optimizes out
5479       // assignments
5480       // !!!     rivals[ i ++ ] = ...;
5481       static kmp_setting_t *volatile rivals[4];
5482       static kmp_stg_ss_data_t kmp_data = {1, CCAST(kmp_setting_t **, rivals)};
5483 #ifdef KMP_GOMP_COMPAT
5484       static kmp_stg_ss_data_t gomp_data = {1024,
5485                                             CCAST(kmp_setting_t **, rivals)};
5486 #endif
5487       static kmp_stg_ss_data_t omp_data = {1024,
5488                                            CCAST(kmp_setting_t **, rivals)};
5489       int i = 0;
5490 
5491       rivals[i++] = kmp_stacksize;
5492 #ifdef KMP_GOMP_COMPAT
5493       if (gomp_stacksize != NULL) {
5494         rivals[i++] = gomp_stacksize;
5495       }
5496 #endif
5497       rivals[i++] = omp_stacksize;
5498       rivals[i++] = NULL;
5499 
5500       kmp_stacksize->data = &kmp_data;
5501 #ifdef KMP_GOMP_COMPAT
5502       if (gomp_stacksize != NULL) {
5503         gomp_stacksize->data = &gomp_data;
5504       }
5505 #endif
5506       omp_stacksize->data = &omp_data;
5507     }
5508 
5509     { // Initialize KMP_LIBRARY and OMP_WAIT_POLICY data.
5510       kmp_setting_t *kmp_library =
5511           __kmp_stg_find("KMP_LIBRARY"); // 1st priority.
5512       kmp_setting_t *omp_wait_policy =
5513           __kmp_stg_find("OMP_WAIT_POLICY"); // 2nd priority.
5514 
5515       // !!! volatile keyword is Intel(R) C Compiler bug CQ49908 workaround.
5516       static kmp_setting_t *volatile rivals[3];
5517       static kmp_stg_wp_data_t kmp_data = {0, CCAST(kmp_setting_t **, rivals)};
5518       static kmp_stg_wp_data_t omp_data = {1, CCAST(kmp_setting_t **, rivals)};
5519       int i = 0;
5520 
5521       rivals[i++] = kmp_library;
5522       if (omp_wait_policy != NULL) {
5523         rivals[i++] = omp_wait_policy;
5524       }
5525       rivals[i++] = NULL;
5526 
5527       kmp_library->data = &kmp_data;
5528       if (omp_wait_policy != NULL) {
5529         omp_wait_policy->data = &omp_data;
5530       }
5531     }
5532 
5533     { // Initialize KMP_DEVICE_THREAD_LIMIT and KMP_ALL_THREADS
5534       kmp_setting_t *kmp_device_thread_limit =
5535           __kmp_stg_find("KMP_DEVICE_THREAD_LIMIT"); // 1st priority.
5536       kmp_setting_t *kmp_all_threads =
5537           __kmp_stg_find("KMP_ALL_THREADS"); // 2nd priority.
5538 
5539       // !!! volatile keyword is Intel(R) C Compiler bug CQ49908 workaround.
5540       static kmp_setting_t *volatile rivals[3];
5541       int i = 0;
5542 
5543       rivals[i++] = kmp_device_thread_limit;
5544       rivals[i++] = kmp_all_threads;
5545       rivals[i++] = NULL;
5546 
5547       kmp_device_thread_limit->data = CCAST(kmp_setting_t **, rivals);
5548       kmp_all_threads->data = CCAST(kmp_setting_t **, rivals);
5549     }
5550 
5551     { // Initialize KMP_HW_SUBSET and KMP_PLACE_THREADS
5552       // 1st priority
5553       kmp_setting_t *kmp_hw_subset = __kmp_stg_find("KMP_HW_SUBSET");
5554       // 2nd priority
5555       kmp_setting_t *kmp_place_threads = __kmp_stg_find("KMP_PLACE_THREADS");
5556 
5557       // !!! volatile keyword is Intel(R) C Compiler bug CQ49908 workaround.
5558       static kmp_setting_t *volatile rivals[3];
5559       int i = 0;
5560 
5561       rivals[i++] = kmp_hw_subset;
5562       rivals[i++] = kmp_place_threads;
5563       rivals[i++] = NULL;
5564 
5565       kmp_hw_subset->data = CCAST(kmp_setting_t **, rivals);
5566       kmp_place_threads->data = CCAST(kmp_setting_t **, rivals);
5567     }
5568 
5569 #if KMP_AFFINITY_SUPPORTED
5570     { // Initialize KMP_AFFINITY, GOMP_CPU_AFFINITY, and OMP_PROC_BIND data.
5571       kmp_setting_t *kmp_affinity =
5572           __kmp_stg_find("KMP_AFFINITY"); // 1st priority.
5573       KMP_DEBUG_ASSERT(kmp_affinity != NULL);
5574 
5575 #ifdef KMP_GOMP_COMPAT
5576       kmp_setting_t *gomp_cpu_affinity =
5577           __kmp_stg_find("GOMP_CPU_AFFINITY"); // 2nd priority.
5578       KMP_DEBUG_ASSERT(gomp_cpu_affinity != NULL);
5579 #endif
5580 
5581       kmp_setting_t *omp_proc_bind =
5582           __kmp_stg_find("OMP_PROC_BIND"); // 3rd priority.
5583       KMP_DEBUG_ASSERT(omp_proc_bind != NULL);
5584 
5585       // !!! volatile keyword is Intel(R) C Compiler bug CQ49908 workaround.
5586       static kmp_setting_t *volatile rivals[4];
5587       int i = 0;
5588 
5589       rivals[i++] = kmp_affinity;
5590 
5591 #ifdef KMP_GOMP_COMPAT
5592       rivals[i++] = gomp_cpu_affinity;
5593       gomp_cpu_affinity->data = CCAST(kmp_setting_t **, rivals);
5594 #endif
5595 
5596       rivals[i++] = omp_proc_bind;
5597       omp_proc_bind->data = CCAST(kmp_setting_t **, rivals);
5598       rivals[i++] = NULL;
5599 
5600       static kmp_setting_t *volatile places_rivals[4];
5601       i = 0;
5602 
5603       kmp_setting_t *omp_places = __kmp_stg_find("OMP_PLACES"); // 3rd priority.
5604       KMP_DEBUG_ASSERT(omp_places != NULL);
5605 
5606       places_rivals[i++] = kmp_affinity;
5607 #ifdef KMP_GOMP_COMPAT
5608       places_rivals[i++] = gomp_cpu_affinity;
5609 #endif
5610       places_rivals[i++] = omp_places;
5611       omp_places->data = CCAST(kmp_setting_t **, places_rivals);
5612       places_rivals[i++] = NULL;
5613     }
5614 #else
5615 // KMP_AFFINITY not supported, so OMP_PROC_BIND has no rivals.
5616 // OMP_PLACES not supported yet.
5617 #endif // KMP_AFFINITY_SUPPORTED
5618 
5619     { // Initialize KMP_DETERMINISTIC_REDUCTION and KMP_FORCE_REDUCTION data.
5620       kmp_setting_t *kmp_force_red =
5621           __kmp_stg_find("KMP_FORCE_REDUCTION"); // 1st priority.
5622       kmp_setting_t *kmp_determ_red =
5623           __kmp_stg_find("KMP_DETERMINISTIC_REDUCTION"); // 2nd priority.
5624 
5625       // !!! volatile keyword is Intel(R) C Compiler bug CQ49908 workaround.
5626       static kmp_setting_t *volatile rivals[3];
5627       static kmp_stg_fr_data_t force_data = {1,
5628                                              CCAST(kmp_setting_t **, rivals)};
5629       static kmp_stg_fr_data_t determ_data = {0,
5630                                               CCAST(kmp_setting_t **, rivals)};
5631       int i = 0;
5632 
5633       rivals[i++] = kmp_force_red;
5634       if (kmp_determ_red != NULL) {
5635         rivals[i++] = kmp_determ_red;
5636       }
5637       rivals[i++] = NULL;
5638 
5639       kmp_force_red->data = &force_data;
5640       if (kmp_determ_red != NULL) {
5641         kmp_determ_red->data = &determ_data;
5642       }
5643     }
5644 
5645     initialized = 1;
5646   }
5647 
5648   // Reset flags.
5649   int i;
5650   for (i = 0; i < __kmp_stg_count; ++i) {
5651     __kmp_stg_table[i].set = 0;
5652   }
5653 
5654 } // __kmp_stg_init
5655 
__kmp_stg_parse(char const * name,char const * value)5656 static void __kmp_stg_parse(char const *name, char const *value) {
5657   // On Windows* OS there are some nameless variables like "C:=C:\" (yeah,
5658   // really nameless, they are presented in environment block as
5659   // "=C:=C\\\x00=D:=D:\\\x00...", so let us skip them.
5660   if (name[0] == 0) {
5661     return;
5662   }
5663 
5664   if (value != NULL) {
5665     kmp_setting_t *setting = __kmp_stg_find(name);
5666     if (setting != NULL) {
5667       setting->parse(name, value, setting->data);
5668       setting->defined = 1;
5669     }
5670   }
5671 
5672 } // __kmp_stg_parse
5673 
__kmp_stg_check_rivals(char const * name,char const * value,kmp_setting_t ** rivals)5674 static int __kmp_stg_check_rivals( // 0 -- Ok, 1 -- errors found.
5675     char const *name, // Name of variable.
5676     char const *value, // Value of the variable.
5677     kmp_setting_t **rivals // List of rival settings (must include current one).
5678 ) {
5679 
5680   if (rivals == NULL) {
5681     return 0;
5682   }
5683 
5684   // Loop thru higher priority settings (listed before current).
5685   int i = 0;
5686   for (; strcmp(rivals[i]->name, name) != 0; i++) {
5687     KMP_DEBUG_ASSERT(rivals[i] != NULL);
5688 
5689 #if KMP_AFFINITY_SUPPORTED
5690     if (rivals[i] == __kmp_affinity_notype) {
5691       // If KMP_AFFINITY is specified without a type name,
5692       // it does not rival OMP_PROC_BIND or GOMP_CPU_AFFINITY.
5693       continue;
5694     }
5695 #endif
5696 
5697     if (rivals[i]->set) {
5698       KMP_WARNING(StgIgnored, name, rivals[i]->name);
5699       return 1;
5700     }
5701   }
5702 
5703   ++i; // Skip current setting.
5704   return 0;
5705 
5706 } // __kmp_stg_check_rivals
5707 
__kmp_env_toPrint(char const * name,int flag)5708 static int __kmp_env_toPrint(char const *name, int flag) {
5709   int rc = 0;
5710   kmp_setting_t *setting = __kmp_stg_find(name);
5711   if (setting != NULL) {
5712     rc = setting->defined;
5713     if (flag >= 0) {
5714       setting->defined = flag;
5715     }
5716   }
5717   return rc;
5718 }
5719 
__kmp_aux_env_initialize(kmp_env_blk_t * block)5720 static void __kmp_aux_env_initialize(kmp_env_blk_t *block) {
5721 
5722   char const *value;
5723 
5724   /* OMP_NUM_THREADS */
5725   value = __kmp_env_blk_var(block, "OMP_NUM_THREADS");
5726   if (value) {
5727     ompc_set_num_threads(__kmp_dflt_team_nth);
5728   }
5729 
5730   /* KMP_BLOCKTIME */
5731   value = __kmp_env_blk_var(block, "KMP_BLOCKTIME");
5732   if (value) {
5733     kmpc_set_blocktime(__kmp_dflt_blocktime);
5734   }
5735 
5736   /* OMP_NESTED */
5737   value = __kmp_env_blk_var(block, "OMP_NESTED");
5738   if (value) {
5739     ompc_set_nested(__kmp_dflt_max_active_levels > 1);
5740   }
5741 
5742   /* OMP_DYNAMIC */
5743   value = __kmp_env_blk_var(block, "OMP_DYNAMIC");
5744   if (value) {
5745     ompc_set_dynamic(__kmp_global.g.g_dynamic);
5746   }
5747 }
5748 
__kmp_env_initialize(char const * string)5749 void __kmp_env_initialize(char const *string) {
5750 
5751   kmp_env_blk_t block;
5752   int i;
5753 
5754   __kmp_stg_init();
5755 
5756   // Hack!!!
5757   if (string == NULL) {
5758     // __kmp_max_nth = __kmp_sys_max_nth;
5759     __kmp_threads_capacity =
5760         __kmp_initial_threads_capacity(__kmp_dflt_team_nth_ub);
5761   }
5762   __kmp_env_blk_init(&block, string);
5763 
5764   // update the set flag on all entries that have an env var
5765   for (i = 0; i < block.count; ++i) {
5766     if ((block.vars[i].name == NULL) || (*block.vars[i].name == '\0')) {
5767       continue;
5768     }
5769     if (block.vars[i].value == NULL) {
5770       continue;
5771     }
5772     kmp_setting_t *setting = __kmp_stg_find(block.vars[i].name);
5773     if (setting != NULL) {
5774       setting->set = 1;
5775     }
5776   }
5777 
5778   // We need to know if blocktime was set when processing OMP_WAIT_POLICY
5779   blocktime_str = __kmp_env_blk_var(&block, "KMP_BLOCKTIME");
5780 
5781   // Special case. If we parse environment, not a string, process KMP_WARNINGS
5782   // first.
5783   if (string == NULL) {
5784     char const *name = "KMP_WARNINGS";
5785     char const *value = __kmp_env_blk_var(&block, name);
5786     __kmp_stg_parse(name, value);
5787   }
5788 
5789 #if KMP_AFFINITY_SUPPORTED
5790   // Special case. KMP_AFFINITY is not a rival to other affinity env vars
5791   // if no affinity type is specified.  We want to allow
5792   // KMP_AFFINITY=[no],verbose/[no]warnings/etc.  to be enabled when
5793   // specifying the affinity type via GOMP_CPU_AFFINITY or the OMP 4.0
5794   // affinity mechanism.
5795   __kmp_affinity_notype = NULL;
5796   char const *aff_str = __kmp_env_blk_var(&block, "KMP_AFFINITY");
5797   if (aff_str != NULL) {
5798     // Check if the KMP_AFFINITY type is specified in the string.
5799     // We just search the string for "compact", "scatter", etc.
5800     // without really parsing the string.  The syntax of the
5801     // KMP_AFFINITY env var is such that none of the affinity
5802     // type names can appear anywhere other that the type
5803     // specifier, even as substrings.
5804     //
5805     // I can't find a case-insensitive version of strstr on Windows* OS.
5806     // Use the case-sensitive version for now.
5807 
5808 #if KMP_OS_WINDOWS
5809 #define FIND strstr
5810 #else
5811 #define FIND strcasestr
5812 #endif
5813 
5814     if ((FIND(aff_str, "none") == NULL) &&
5815         (FIND(aff_str, "physical") == NULL) &&
5816         (FIND(aff_str, "logical") == NULL) &&
5817         (FIND(aff_str, "compact") == NULL) &&
5818         (FIND(aff_str, "scatter") == NULL) &&
5819         (FIND(aff_str, "explicit") == NULL) &&
5820         (FIND(aff_str, "balanced") == NULL) &&
5821         (FIND(aff_str, "disabled") == NULL)) {
5822       __kmp_affinity_notype = __kmp_stg_find("KMP_AFFINITY");
5823     } else {
5824       // A new affinity type is specified.
5825       // Reset the affinity flags to their default values,
5826       // in case this is called from kmp_set_defaults().
5827       __kmp_affinity_type = affinity_default;
5828       __kmp_affinity_gran = KMP_HW_UNKNOWN;
5829       __kmp_affinity_top_method = affinity_top_method_default;
5830       __kmp_affinity_respect_mask = affinity_respect_mask_default;
5831     }
5832 #undef FIND
5833 
5834     // Also reset the affinity flags if OMP_PROC_BIND is specified.
5835     aff_str = __kmp_env_blk_var(&block, "OMP_PROC_BIND");
5836     if (aff_str != NULL) {
5837       __kmp_affinity_type = affinity_default;
5838       __kmp_affinity_gran = KMP_HW_UNKNOWN;
5839       __kmp_affinity_top_method = affinity_top_method_default;
5840       __kmp_affinity_respect_mask = affinity_respect_mask_default;
5841     }
5842   }
5843 
5844 #endif /* KMP_AFFINITY_SUPPORTED */
5845 
5846   // Set up the nested proc bind type vector.
5847   if (__kmp_nested_proc_bind.bind_types == NULL) {
5848     __kmp_nested_proc_bind.bind_types =
5849         (kmp_proc_bind_t *)KMP_INTERNAL_MALLOC(sizeof(kmp_proc_bind_t));
5850     if (__kmp_nested_proc_bind.bind_types == NULL) {
5851       KMP_FATAL(MemoryAllocFailed);
5852     }
5853     __kmp_nested_proc_bind.size = 1;
5854     __kmp_nested_proc_bind.used = 1;
5855 #if KMP_AFFINITY_SUPPORTED
5856     __kmp_nested_proc_bind.bind_types[0] = proc_bind_default;
5857 #else
5858     // default proc bind is false if affinity not supported
5859     __kmp_nested_proc_bind.bind_types[0] = proc_bind_false;
5860 #endif
5861   }
5862 
5863   // Set up the affinity format ICV
5864   // Grab the default affinity format string from the message catalog
5865   kmp_msg_t m =
5866       __kmp_msg_format(kmp_i18n_msg_AffFormatDefault, "%P", "%i", "%n", "%A");
5867   KMP_DEBUG_ASSERT(KMP_STRLEN(m.str) < KMP_AFFINITY_FORMAT_SIZE);
5868 
5869   if (__kmp_affinity_format == NULL) {
5870     __kmp_affinity_format =
5871         (char *)KMP_INTERNAL_MALLOC(sizeof(char) * KMP_AFFINITY_FORMAT_SIZE);
5872   }
5873   KMP_STRCPY_S(__kmp_affinity_format, KMP_AFFINITY_FORMAT_SIZE, m.str);
5874   __kmp_str_free(&m.str);
5875 
5876   // Now process all of the settings.
5877   for (i = 0; i < block.count; ++i) {
5878     __kmp_stg_parse(block.vars[i].name, block.vars[i].value);
5879   }
5880 
5881   // If user locks have been allocated yet, don't reset the lock vptr table.
5882   if (!__kmp_init_user_locks) {
5883     if (__kmp_user_lock_kind == lk_default) {
5884       __kmp_user_lock_kind = lk_queuing;
5885     }
5886 #if KMP_USE_DYNAMIC_LOCK
5887     __kmp_init_dynamic_user_locks();
5888 #else
5889     __kmp_set_user_lock_vptrs(__kmp_user_lock_kind);
5890 #endif
5891   } else {
5892     KMP_DEBUG_ASSERT(string != NULL); // kmp_set_defaults() was called
5893     KMP_DEBUG_ASSERT(__kmp_user_lock_kind != lk_default);
5894 // Binds lock functions again to follow the transition between different
5895 // KMP_CONSISTENCY_CHECK values. Calling this again is harmless as long
5896 // as we do not allow lock kind changes after making a call to any
5897 // user lock functions (true).
5898 #if KMP_USE_DYNAMIC_LOCK
5899     __kmp_init_dynamic_user_locks();
5900 #else
5901     __kmp_set_user_lock_vptrs(__kmp_user_lock_kind);
5902 #endif
5903   }
5904 
5905 #if KMP_AFFINITY_SUPPORTED
5906 
5907   if (!TCR_4(__kmp_init_middle)) {
5908 #if KMP_USE_HWLOC
5909     // Force using hwloc when either tiles or numa nodes requested within
5910     // KMP_HW_SUBSET or granularity setting and no other topology method
5911     // is requested
5912     if (__kmp_hw_subset &&
5913         __kmp_affinity_top_method == affinity_top_method_default)
5914       if (__kmp_hw_subset->specified(KMP_HW_NUMA) ||
5915           __kmp_hw_subset->specified(KMP_HW_TILE) ||
5916           __kmp_affinity_gran == KMP_HW_TILE ||
5917           __kmp_affinity_gran == KMP_HW_NUMA)
5918         __kmp_affinity_top_method = affinity_top_method_hwloc;
5919     // Force using hwloc when tiles or numa nodes requested for OMP_PLACES
5920     if (__kmp_affinity_gran == KMP_HW_NUMA ||
5921         __kmp_affinity_gran == KMP_HW_TILE)
5922       __kmp_affinity_top_method = affinity_top_method_hwloc;
5923 #endif
5924     // Determine if the machine/OS is actually capable of supporting
5925     // affinity.
5926     const char *var = "KMP_AFFINITY";
5927     KMPAffinity::pick_api();
5928 #if KMP_USE_HWLOC
5929     // If Hwloc topology discovery was requested but affinity was also disabled,
5930     // then tell user that Hwloc request is being ignored and use default
5931     // topology discovery method.
5932     if (__kmp_affinity_top_method == affinity_top_method_hwloc &&
5933         __kmp_affinity_dispatch->get_api_type() != KMPAffinity::HWLOC) {
5934       KMP_WARNING(AffIgnoringHwloc, var);
5935       __kmp_affinity_top_method = affinity_top_method_all;
5936     }
5937 #endif
5938     if (__kmp_affinity_type == affinity_disabled) {
5939       KMP_AFFINITY_DISABLE();
5940     } else if (!KMP_AFFINITY_CAPABLE()) {
5941       __kmp_affinity_dispatch->determine_capable(var);
5942       if (!KMP_AFFINITY_CAPABLE()) {
5943         if (__kmp_affinity_verbose ||
5944             (__kmp_affinity_warnings &&
5945              (__kmp_affinity_type != affinity_default) &&
5946              (__kmp_affinity_type != affinity_none) &&
5947              (__kmp_affinity_type != affinity_disabled))) {
5948           KMP_WARNING(AffNotSupported, var);
5949         }
5950         __kmp_affinity_type = affinity_disabled;
5951         __kmp_affinity_respect_mask = 0;
5952         __kmp_affinity_gran = KMP_HW_THREAD;
5953       }
5954     }
5955 
5956     if (__kmp_affinity_type == affinity_disabled) {
5957       __kmp_nested_proc_bind.bind_types[0] = proc_bind_false;
5958     } else if (__kmp_nested_proc_bind.bind_types[0] == proc_bind_true) {
5959       // OMP_PROC_BIND=true maps to OMP_PROC_BIND=spread.
5960       __kmp_nested_proc_bind.bind_types[0] = proc_bind_spread;
5961     }
5962 
5963     if (KMP_AFFINITY_CAPABLE()) {
5964 
5965 #if KMP_GROUP_AFFINITY
5966       // This checks to see if the initial affinity mask is equal
5967       // to a single windows processor group.  If it is, then we do
5968       // not respect the initial affinity mask and instead, use the
5969       // entire machine.
5970       bool exactly_one_group = false;
5971       if (__kmp_num_proc_groups > 1) {
5972         int group;
5973         bool within_one_group;
5974         // Get the initial affinity mask and determine if it is
5975         // contained within a single group.
5976         kmp_affin_mask_t *init_mask;
5977         KMP_CPU_ALLOC(init_mask);
5978         __kmp_get_system_affinity(init_mask, TRUE);
5979         group = __kmp_get_proc_group(init_mask);
5980         within_one_group = (group >= 0);
5981         // If the initial affinity is within a single group,
5982         // then determine if it is equal to that single group.
5983         if (within_one_group) {
5984           DWORD num_bits_in_group = __kmp_GetActiveProcessorCount(group);
5985           DWORD num_bits_in_mask = 0;
5986           for (int bit = init_mask->begin(); bit != init_mask->end();
5987                bit = init_mask->next(bit))
5988             num_bits_in_mask++;
5989           exactly_one_group = (num_bits_in_group == num_bits_in_mask);
5990         }
5991         KMP_CPU_FREE(init_mask);
5992       }
5993 
5994       // Handle the Win 64 group affinity stuff if there are multiple
5995       // processor groups, or if the user requested it, and OMP 4.0
5996       // affinity is not in effect.
5997       if (((__kmp_num_proc_groups > 1) &&
5998            (__kmp_affinity_type == affinity_default) &&
5999            (__kmp_nested_proc_bind.bind_types[0] == proc_bind_default)) ||
6000           (__kmp_affinity_top_method == affinity_top_method_group)) {
6001         if (__kmp_affinity_respect_mask == affinity_respect_mask_default &&
6002             exactly_one_group) {
6003           __kmp_affinity_respect_mask = FALSE;
6004         }
6005         if (__kmp_affinity_type == affinity_default) {
6006           __kmp_affinity_type = affinity_compact;
6007           __kmp_nested_proc_bind.bind_types[0] = proc_bind_intel;
6008         }
6009         if (__kmp_affinity_top_method == affinity_top_method_default) {
6010           if (__kmp_affinity_gran == KMP_HW_UNKNOWN) {
6011             __kmp_affinity_top_method = affinity_top_method_group;
6012             __kmp_affinity_gran = KMP_HW_PROC_GROUP;
6013           } else if (__kmp_affinity_gran == KMP_HW_PROC_GROUP) {
6014             __kmp_affinity_top_method = affinity_top_method_group;
6015           } else {
6016             __kmp_affinity_top_method = affinity_top_method_all;
6017           }
6018         } else if (__kmp_affinity_top_method == affinity_top_method_group) {
6019           if (__kmp_affinity_gran == KMP_HW_UNKNOWN) {
6020             __kmp_affinity_gran = KMP_HW_PROC_GROUP;
6021           } else if ((__kmp_affinity_gran != KMP_HW_PROC_GROUP) &&
6022                      (__kmp_affinity_gran != KMP_HW_THREAD)) {
6023             const char *str = __kmp_hw_get_keyword(__kmp_affinity_gran);
6024             KMP_WARNING(AffGranTopGroup, var, str);
6025             __kmp_affinity_gran = KMP_HW_THREAD;
6026           }
6027         } else {
6028           if (__kmp_affinity_gran == KMP_HW_UNKNOWN) {
6029             __kmp_affinity_gran = KMP_HW_CORE;
6030           } else if (__kmp_affinity_gran == KMP_HW_PROC_GROUP) {
6031             const char *str = NULL;
6032             switch (__kmp_affinity_type) {
6033             case affinity_physical:
6034               str = "physical";
6035               break;
6036             case affinity_logical:
6037               str = "logical";
6038               break;
6039             case affinity_compact:
6040               str = "compact";
6041               break;
6042             case affinity_scatter:
6043               str = "scatter";
6044               break;
6045             case affinity_explicit:
6046               str = "explicit";
6047               break;
6048             // No MIC on windows, so no affinity_balanced case
6049             default:
6050               KMP_DEBUG_ASSERT(0);
6051             }
6052             KMP_WARNING(AffGranGroupType, var, str);
6053             __kmp_affinity_gran = KMP_HW_CORE;
6054           }
6055         }
6056       } else
6057 
6058 #endif /* KMP_GROUP_AFFINITY */
6059 
6060       {
6061         if (__kmp_affinity_respect_mask == affinity_respect_mask_default) {
6062 #if KMP_GROUP_AFFINITY
6063           if (__kmp_num_proc_groups > 1 && exactly_one_group) {
6064             __kmp_affinity_respect_mask = FALSE;
6065           } else
6066 #endif /* KMP_GROUP_AFFINITY */
6067           {
6068             __kmp_affinity_respect_mask = TRUE;
6069           }
6070         }
6071         if ((__kmp_nested_proc_bind.bind_types[0] != proc_bind_intel) &&
6072             (__kmp_nested_proc_bind.bind_types[0] != proc_bind_default)) {
6073           if (__kmp_affinity_type == affinity_default) {
6074             __kmp_affinity_type = affinity_compact;
6075             __kmp_affinity_dups = FALSE;
6076           }
6077         } else if (__kmp_affinity_type == affinity_default) {
6078 #if KMP_MIC_SUPPORTED
6079           if (__kmp_mic_type != non_mic) {
6080             __kmp_nested_proc_bind.bind_types[0] = proc_bind_intel;
6081           } else
6082 #endif
6083           {
6084             __kmp_nested_proc_bind.bind_types[0] = proc_bind_false;
6085           }
6086 #if KMP_MIC_SUPPORTED
6087           if (__kmp_mic_type != non_mic) {
6088             __kmp_affinity_type = affinity_scatter;
6089           } else
6090 #endif
6091           {
6092             __kmp_affinity_type = affinity_none;
6093           }
6094         }
6095         if ((__kmp_affinity_gran == KMP_HW_UNKNOWN) &&
6096             (__kmp_affinity_gran_levels < 0)) {
6097 #if KMP_MIC_SUPPORTED
6098           if (__kmp_mic_type != non_mic) {
6099             __kmp_affinity_gran = KMP_HW_THREAD;
6100           } else
6101 #endif
6102           {
6103             __kmp_affinity_gran = KMP_HW_CORE;
6104           }
6105         }
6106         if (__kmp_affinity_top_method == affinity_top_method_default) {
6107           __kmp_affinity_top_method = affinity_top_method_all;
6108         }
6109       }
6110     }
6111 
6112     K_DIAG(1, ("__kmp_affinity_type         == %d\n", __kmp_affinity_type));
6113     K_DIAG(1, ("__kmp_affinity_compact      == %d\n", __kmp_affinity_compact));
6114     K_DIAG(1, ("__kmp_affinity_offset       == %d\n", __kmp_affinity_offset));
6115     K_DIAG(1, ("__kmp_affinity_verbose      == %d\n", __kmp_affinity_verbose));
6116     K_DIAG(1, ("__kmp_affinity_warnings     == %d\n", __kmp_affinity_warnings));
6117     K_DIAG(1, ("__kmp_affinity_respect_mask == %d\n",
6118                __kmp_affinity_respect_mask));
6119     K_DIAG(1, ("__kmp_affinity_gran         == %d\n", __kmp_affinity_gran));
6120 
6121     KMP_DEBUG_ASSERT(__kmp_affinity_type != affinity_default);
6122     KMP_DEBUG_ASSERT(__kmp_nested_proc_bind.bind_types[0] != proc_bind_default);
6123     K_DIAG(1, ("__kmp_nested_proc_bind.bind_types[0] == %d\n",
6124                __kmp_nested_proc_bind.bind_types[0]));
6125   }
6126 
6127 #endif /* KMP_AFFINITY_SUPPORTED */
6128 
6129   if (__kmp_version) {
6130     __kmp_print_version_1();
6131   }
6132 
6133   // Post-initialization step: some env. vars need their value's further
6134   // processing
6135   if (string != NULL) { // kmp_set_defaults() was called
6136     __kmp_aux_env_initialize(&block);
6137   }
6138 
6139   __kmp_env_blk_free(&block);
6140 
6141   KMP_MB();
6142 
6143 } // __kmp_env_initialize
6144 
__kmp_env_print()6145 void __kmp_env_print() {
6146 
6147   kmp_env_blk_t block;
6148   int i;
6149   kmp_str_buf_t buffer;
6150 
6151   __kmp_stg_init();
6152   __kmp_str_buf_init(&buffer);
6153 
6154   __kmp_env_blk_init(&block, NULL);
6155   __kmp_env_blk_sort(&block);
6156 
6157   // Print real environment values.
6158   __kmp_str_buf_print(&buffer, "\n%s\n\n", KMP_I18N_STR(UserSettings));
6159   for (i = 0; i < block.count; ++i) {
6160     char const *name = block.vars[i].name;
6161     char const *value = block.vars[i].value;
6162     if ((KMP_STRLEN(name) > 4 && strncmp(name, "KMP_", 4) == 0) ||
6163         strncmp(name, "OMP_", 4) == 0
6164 #ifdef KMP_GOMP_COMPAT
6165         || strncmp(name, "GOMP_", 5) == 0
6166 #endif // KMP_GOMP_COMPAT
6167     ) {
6168       __kmp_str_buf_print(&buffer, "   %s=%s\n", name, value);
6169     }
6170   }
6171   __kmp_str_buf_print(&buffer, "\n");
6172 
6173   // Print internal (effective) settings.
6174   __kmp_str_buf_print(&buffer, "%s\n\n", KMP_I18N_STR(EffectiveSettings));
6175   for (int i = 0; i < __kmp_stg_count; ++i) {
6176     if (__kmp_stg_table[i].print != NULL) {
6177       __kmp_stg_table[i].print(&buffer, __kmp_stg_table[i].name,
6178                                __kmp_stg_table[i].data);
6179     }
6180   }
6181 
6182   __kmp_printf("%s", buffer.str);
6183 
6184   __kmp_env_blk_free(&block);
6185   __kmp_str_buf_free(&buffer);
6186 
6187   __kmp_printf("\n");
6188 
6189 } // __kmp_env_print
6190 
__kmp_env_print_2()6191 void __kmp_env_print_2() {
6192   __kmp_display_env_impl(__kmp_display_env, __kmp_display_env_verbose);
6193 } // __kmp_env_print_2
6194 
__kmp_display_env_impl(int display_env,int display_env_verbose)6195 void __kmp_display_env_impl(int display_env, int display_env_verbose) {
6196   kmp_env_blk_t block;
6197   kmp_str_buf_t buffer;
6198 
6199   __kmp_env_format = 1;
6200 
6201   __kmp_stg_init();
6202   __kmp_str_buf_init(&buffer);
6203 
6204   __kmp_env_blk_init(&block, NULL);
6205   __kmp_env_blk_sort(&block);
6206 
6207   __kmp_str_buf_print(&buffer, "\n%s\n", KMP_I18N_STR(DisplayEnvBegin));
6208   __kmp_str_buf_print(&buffer, "   _OPENMP='%d'\n", __kmp_openmp_version);
6209 
6210   for (int i = 0; i < __kmp_stg_count; ++i) {
6211     if (__kmp_stg_table[i].print != NULL &&
6212         ((display_env && strncmp(__kmp_stg_table[i].name, "OMP_", 4) == 0) ||
6213          display_env_verbose)) {
6214       __kmp_stg_table[i].print(&buffer, __kmp_stg_table[i].name,
6215                                __kmp_stg_table[i].data);
6216     }
6217   }
6218 
6219   __kmp_str_buf_print(&buffer, "%s\n", KMP_I18N_STR(DisplayEnvEnd));
6220   __kmp_str_buf_print(&buffer, "\n");
6221 
6222   __kmp_printf("%s", buffer.str);
6223 
6224   __kmp_env_blk_free(&block);
6225   __kmp_str_buf_free(&buffer);
6226 
6227   __kmp_printf("\n");
6228 }
6229 
6230 #if OMPD_SUPPORT
6231 // Dump environment variables for OMPD
__kmp_env_dump()6232 void __kmp_env_dump() {
6233 
6234   kmp_env_blk_t block;
6235   kmp_str_buf_t buffer, env, notdefined;
6236 
6237   __kmp_stg_init();
6238   __kmp_str_buf_init(&buffer);
6239   __kmp_str_buf_init(&env);
6240   __kmp_str_buf_init(&notdefined);
6241 
6242   __kmp_env_blk_init(&block, NULL);
6243   __kmp_env_blk_sort(&block);
6244 
6245   __kmp_str_buf_print(&notdefined, ": %s", KMP_I18N_STR(NotDefined));
6246 
6247   for (int i = 0; i < __kmp_stg_count; ++i) {
6248     if (__kmp_stg_table[i].print == NULL)
6249       continue;
6250     __kmp_str_buf_clear(&env);
6251     __kmp_stg_table[i].print(&env, __kmp_stg_table[i].name,
6252                              __kmp_stg_table[i].data);
6253     if (env.used < 4) // valid definition must have indents (3) and a new line
6254       continue;
6255     if (strstr(env.str, notdefined.str))
6256       // normalize the string
6257       __kmp_str_buf_print(&buffer, "%s=undefined\n", __kmp_stg_table[i].name);
6258     else
6259       __kmp_str_buf_cat(&buffer, env.str + 3, env.used - 3);
6260   }
6261 
6262   ompd_env_block = (char *)__kmp_allocate(buffer.used + 1);
6263   KMP_MEMCPY(ompd_env_block, buffer.str, buffer.used + 1);
6264   ompd_env_block_size = (ompd_size_t)KMP_STRLEN(ompd_env_block);
6265 
6266   __kmp_env_blk_free(&block);
6267   __kmp_str_buf_free(&buffer);
6268   __kmp_str_buf_free(&env);
6269   __kmp_str_buf_free(&notdefined);
6270 }
6271 #endif // OMPD_SUPPORT
6272 
6273 // end of file
6274