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