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