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