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