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