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