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