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