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