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