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