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