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