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