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