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             *out_type = affinity_explicit;
2244 # if OMP_40_ENABLED
2245             __kmp_nested_proc_bind.bind_types[0] = proc_bind_intel;
2246 # endif
2247         }
2248         else if ( *out_type != affinity_explicit ) {
2249             KMP_WARNING( AffProcListNotExplicit, name );
2250             KMP_ASSERT( *out_proclist != NULL );
2251             KMP_INTERNAL_FREE( *out_proclist );
2252             *out_proclist = NULL;
2253         }
2254     }
2255     switch ( *out_type ) {
2256         case affinity_logical:
2257         case affinity_physical: {
2258             if ( count > 0 ) {
2259                 *out_offset = number[ 0 ];
2260             }; // if
2261             if ( count > 1 ) {
2262                 KMP_WARNING( AffManyParamsForLogic, name, number[ 1 ] );
2263             }; // if
2264         } break;
2265         case affinity_balanced: {
2266             if ( count > 0 ) {
2267                 *out_compact = number[ 0 ];
2268             }; // if
2269             if ( count > 1 ) {
2270                 *out_offset = number[ 1 ];
2271             }; // if
2272 
2273             if ( __kmp_affinity_gran == affinity_gran_default ) {
2274 #if KMP_ARCH_X86_64 && (KMP_OS_LINUX || KMP_OS_WINDOWS)
2275                 if( __kmp_mic_type != non_mic ) {
2276                     if( __kmp_affinity_verbose || __kmp_affinity_warnings ) {
2277                         KMP_WARNING( AffGranUsing, "KMP_AFFINITY", "fine" );
2278                     }
2279                     __kmp_affinity_gran = affinity_gran_fine;
2280                 } else
2281 #endif
2282                 {
2283                     if( __kmp_affinity_verbose || __kmp_affinity_warnings ) {
2284                         KMP_WARNING( AffGranUsing, "KMP_AFFINITY", "core" );
2285                     }
2286                     __kmp_affinity_gran = affinity_gran_core;
2287                 }
2288             }
2289         } break;
2290         case affinity_scatter:
2291         case affinity_compact: {
2292             if ( count > 0 ) {
2293                 *out_compact = number[ 0 ];
2294             }; // if
2295             if ( count > 1 ) {
2296                 *out_offset = number[ 1 ];
2297             }; // if
2298         } break;
2299         case affinity_explicit: {
2300             if ( *out_proclist == NULL ) {
2301                 KMP_WARNING( AffNoProcList, name );
2302                 __kmp_affinity_type = affinity_none;
2303             }
2304             if ( count > 0 ) {
2305                 KMP_WARNING( AffNoParam, name, "explicit" );
2306             }
2307         } break;
2308         case affinity_none: {
2309             if ( count > 0 ) {
2310                 KMP_WARNING( AffNoParam, name, "none" );
2311             }; // if
2312         } break;
2313         case affinity_disabled: {
2314             if ( count > 0 ) {
2315                 KMP_WARNING( AffNoParam, name, "disabled" );
2316             }; // if
2317         } break;
2318         case affinity_default: {
2319             if ( count > 0 ) {
2320                 KMP_WARNING( AffNoParam, name, "default" );
2321             }; // if
2322         } break;
2323         default: {
2324             KMP_ASSERT( 0 );
2325         };
2326     }; // switch
2327 } // __kmp_parse_affinity_env
2328 
2329 static void
2330 __kmp_stg_parse_affinity( char const * name, char const * value, void * data )
2331 {
2332     kmp_setting_t **rivals = (kmp_setting_t **) data;
2333     int rc;
2334 
2335     rc = __kmp_stg_check_rivals( name, value, rivals );
2336     if ( rc ) {
2337         return;
2338     }
2339 
2340     __kmp_parse_affinity_env( name, value, & __kmp_affinity_type,
2341       & __kmp_affinity_proclist, & __kmp_affinity_verbose,
2342       & __kmp_affinity_warnings, & __kmp_affinity_respect_mask,
2343       & __kmp_affinity_gran, & __kmp_affinity_gran_levels,
2344       & __kmp_affinity_dups, & __kmp_affinity_compact,
2345       & __kmp_affinity_offset );
2346 
2347 } // __kmp_stg_parse_affinity
2348 
2349 static void
2350 __kmp_stg_print_affinity( kmp_str_buf_t * buffer, char const * name, void * data ) {
2351     if( __kmp_env_format ) {
2352         KMP_STR_BUF_PRINT_NAME_EX(name);
2353     } else {
2354         __kmp_str_buf_print( buffer, "   %s='", name );
2355     }
2356     if ( __kmp_affinity_verbose ) {
2357         __kmp_str_buf_print( buffer, "%s,", "verbose");
2358     } else {
2359         __kmp_str_buf_print( buffer, "%s,", "noverbose");
2360     }
2361     if ( __kmp_affinity_warnings ) {
2362         __kmp_str_buf_print( buffer, "%s,", "warnings");
2363     } else {
2364         __kmp_str_buf_print( buffer, "%s,", "nowarnings");
2365     }
2366     if ( KMP_AFFINITY_CAPABLE() ) {
2367         if ( __kmp_affinity_respect_mask ) {
2368             __kmp_str_buf_print( buffer, "%s,", "respect");
2369         } else {
2370             __kmp_str_buf_print( buffer, "%s,", "norespect");
2371         }
2372         switch ( __kmp_affinity_gran ) {
2373             case affinity_gran_default:
2374                 __kmp_str_buf_print( buffer, "%s", "granularity=default,");
2375                 break;
2376             case affinity_gran_fine:
2377                 __kmp_str_buf_print( buffer, "%s", "granularity=fine,");
2378                 break;
2379             case affinity_gran_thread:
2380                 __kmp_str_buf_print( buffer, "%s", "granularity=thread,");
2381                 break;
2382             case affinity_gran_core:
2383                 __kmp_str_buf_print( buffer, "%s", "granularity=core,");
2384                 break;
2385             case affinity_gran_package:
2386                 __kmp_str_buf_print( buffer, "%s", "granularity=package,");
2387                 break;
2388             case affinity_gran_node:
2389                 __kmp_str_buf_print( buffer, "%s", "granularity=node,");
2390                 break;
2391 # if KMP_GROUP_AFFINITY
2392             case affinity_gran_group:
2393                 __kmp_str_buf_print( buffer, "%s", "granularity=group,");
2394                 break;
2395 # endif /* KMP_GROUP_AFFINITY */
2396         }
2397         if ( __kmp_affinity_dups ) {
2398             __kmp_str_buf_print( buffer, "%s,", "duplicates");
2399         } else {
2400             __kmp_str_buf_print( buffer, "%s,", "noduplicates");
2401         }
2402     }
2403     if ( ! KMP_AFFINITY_CAPABLE() ) {
2404         __kmp_str_buf_print( buffer, "%s", "disabled" );
2405     }
2406     else switch ( __kmp_affinity_type ){
2407         case affinity_none:
2408             __kmp_str_buf_print( buffer, "%s", "none");
2409             break;
2410         case affinity_physical:
2411             __kmp_str_buf_print( buffer, "%s,%d", "physical",
2412               __kmp_affinity_offset );
2413             break;
2414         case affinity_logical:
2415             __kmp_str_buf_print( buffer, "%s,%d", "logical",
2416               __kmp_affinity_offset );
2417             break;
2418         case affinity_compact:
2419             __kmp_str_buf_print( buffer, "%s,%d,%d", "compact",
2420               __kmp_affinity_compact, __kmp_affinity_offset );
2421             break;
2422         case affinity_scatter:
2423             __kmp_str_buf_print( buffer, "%s,%d,%d", "scatter",
2424               __kmp_affinity_compact, __kmp_affinity_offset );
2425             break;
2426         case affinity_explicit:
2427             __kmp_str_buf_print( buffer, "%s=[%s],%s", "proclist",
2428               __kmp_affinity_proclist, "explicit" );
2429             break;
2430         case affinity_balanced:
2431             __kmp_str_buf_print( buffer, "%s,%d,%d", "balanced",
2432               __kmp_affinity_compact, __kmp_affinity_offset );
2433             break;
2434         case affinity_disabled:
2435             __kmp_str_buf_print( buffer, "%s", "disabled");
2436             break;
2437         case affinity_default:
2438             __kmp_str_buf_print( buffer, "%s", "default");
2439             break;
2440         default:
2441             __kmp_str_buf_print( buffer, "%s", "<unknown>");
2442             break;
2443     }
2444         __kmp_str_buf_print( buffer, "'\n" );
2445 } //__kmp_stg_print_affinity
2446 
2447 # ifdef KMP_GOMP_COMPAT
2448 
2449 static void
2450 __kmp_stg_parse_gomp_cpu_affinity( char const * name, char const * value, void * data )
2451 {
2452     const char * next = NULL;
2453     char * temp_proclist;
2454     kmp_setting_t **rivals = (kmp_setting_t **) data;
2455     int rc;
2456 
2457     rc = __kmp_stg_check_rivals( name, value, rivals );
2458     if ( rc ) {
2459         return;
2460     }
2461 
2462     if ( TCR_4(__kmp_init_middle) ) {
2463         KMP_WARNING( EnvMiddleWarn, name );
2464         __kmp_env_toPrint( name, 0 );
2465         return;
2466     }
2467 
2468     __kmp_env_toPrint( name, 1 );
2469 
2470     if ( __kmp_parse_affinity_proc_id_list( name, value, &next,
2471       &temp_proclist )) {
2472         SKIP_WS(next);
2473         if (*next == '\0') {
2474             //
2475             // GOMP_CPU_AFFINITY => granularity=fine,explicit,proclist=...
2476             //
2477             __kmp_affinity_proclist = temp_proclist;
2478             __kmp_affinity_type = affinity_explicit;
2479             __kmp_affinity_gran = affinity_gran_fine;
2480 # if OMP_40_ENABLED
2481             __kmp_nested_proc_bind.bind_types[0] = proc_bind_intel;
2482 # endif
2483         }
2484         else {
2485             KMP_WARNING( AffSyntaxError, name );
2486             if (temp_proclist != NULL) {
2487                 KMP_INTERNAL_FREE((void *)temp_proclist);
2488             }
2489         }
2490     }
2491     else {
2492         //
2493         // Warning already emitted
2494         //
2495         __kmp_affinity_type = affinity_none;
2496 # if OMP_40_ENABLED
2497         __kmp_nested_proc_bind.bind_types[0] = proc_bind_false;
2498 # endif
2499     }
2500 } // __kmp_stg_parse_gomp_cpu_affinity
2501 
2502 # endif /* KMP_GOMP_COMPAT */
2503 
2504 
2505 # if OMP_40_ENABLED
2506 
2507 /*-----------------------------------------------------------------------------
2508 
2509 The OMP_PLACES proc id list parser. Here is the grammar:
2510 
2511 place_list := place
2512 place_list := place , place_list
2513 place := num
2514 place := place : num
2515 place := place : num : signed
2516 place := { subplacelist }
2517 place := ! place                  // (lowest priority)
2518 subplace_list := subplace
2519 subplace_list := subplace , subplace_list
2520 subplace := num
2521 subplace := num : num
2522 subplace := num : num : signed
2523 signed := num
2524 signed := + signed
2525 signed := - signed
2526 
2527 -----------------------------------------------------------------------------*/
2528 
2529 static int
2530 __kmp_parse_subplace_list( const char *var, const char **scan )
2531 {
2532     const char *next;
2533 
2534     for (;;) {
2535         int start, count, stride;
2536 
2537         //
2538         // Read in the starting proc id
2539         //
2540         SKIP_WS(*scan);
2541         if ((**scan < '0') || (**scan > '9')) {
2542             KMP_WARNING( SyntaxErrorUsing, var, "\"threads\"" );
2543             return FALSE;
2544         }
2545         next = *scan;
2546         SKIP_DIGITS(next);
2547         start = __kmp_str_to_int(*scan, *next);
2548         KMP_ASSERT(start >= 0);
2549         *scan = next;
2550 
2551         //
2552         // valid follow sets are ',' ':' and '}'
2553         //
2554         SKIP_WS(*scan);
2555         if (**scan == '}') {
2556             break;
2557         }
2558         if (**scan == ',') {
2559             (*scan)++;  // skip ','
2560             continue;
2561         }
2562         if (**scan != ':') {
2563             KMP_WARNING( SyntaxErrorUsing, var, "\"threads\"" );
2564             return FALSE;
2565         }
2566         (*scan)++;      // skip ':'
2567 
2568         //
2569         // Read count parameter
2570         //
2571         SKIP_WS(*scan);
2572         if ((**scan < '0') || (**scan > '9')) {
2573             KMP_WARNING( SyntaxErrorUsing, var, "\"threads\"" );
2574             return FALSE;
2575         }
2576         next = *scan;
2577         SKIP_DIGITS(next);
2578         count = __kmp_str_to_int(*scan, *next);
2579         KMP_ASSERT(count >= 0);
2580         *scan = next;
2581 
2582         //
2583         // valid follow sets are ',' ':' and '}'
2584         //
2585         SKIP_WS(*scan);
2586         if (**scan == '}') {
2587             break;
2588         }
2589         if (**scan == ',') {
2590             (*scan)++;  // skip ','
2591             continue;
2592         }
2593         if (**scan != ':') {
2594             KMP_WARNING( SyntaxErrorUsing, var, "\"threads\"" );
2595             return FALSE;
2596         }
2597         (*scan)++;      // skip ':'
2598 
2599         //
2600         // Read stride parameter
2601         //
2602         int sign = +1;
2603         for (;;) {
2604             SKIP_WS(*scan);
2605             if (**scan == '+') {
2606                 (*scan)++; // skip '+'
2607                 continue;
2608             }
2609             if (**scan == '-') {
2610                 sign *= -1;
2611                 (*scan)++; // skip '-'
2612                 continue;
2613             }
2614             break;
2615         }
2616         SKIP_WS(*scan);
2617         if ((**scan < '0') || (**scan > '9')) {
2618             KMP_WARNING( SyntaxErrorUsing, var, "\"threads\"" );
2619             return FALSE;
2620         }
2621         next = *scan;
2622         SKIP_DIGITS(next);
2623         stride = __kmp_str_to_int(*scan, *next);
2624         KMP_ASSERT(stride >= 0);
2625         *scan = next;
2626         stride *= sign;
2627 
2628         //
2629         // valid follow sets are ',' and '}'
2630         //
2631         SKIP_WS(*scan);
2632         if (**scan == '}') {
2633             break;
2634         }
2635         if (**scan == ',') {
2636             (*scan)++;  // skip ','
2637             continue;
2638         }
2639 
2640         KMP_WARNING( SyntaxErrorUsing, var, "\"threads\"" );
2641         return FALSE;
2642     }
2643     return TRUE;
2644 }
2645 
2646 static int
2647 __kmp_parse_place( const char *var, const char ** scan )
2648 {
2649     const char *next;
2650 
2651     //
2652     // valid follow sets are '{' '!' and num
2653     //
2654     SKIP_WS(*scan);
2655     if (**scan == '{') {
2656         (*scan)++;      // skip '{'
2657         if (! __kmp_parse_subplace_list(var, scan)) {
2658             return FALSE;
2659         }
2660         if (**scan != '}') {
2661             KMP_WARNING( SyntaxErrorUsing, var, "\"threads\"" );
2662             return FALSE;
2663         }
2664         (*scan)++;      // skip '}'
2665     }
2666     else if (**scan == '!') {
2667         (*scan)++;      // skip '!'
2668         return __kmp_parse_place(var, scan); //'!' has lower precedence than ':'
2669     }
2670     else if ((**scan >= '0') && (**scan <= '9')) {
2671         next = *scan;
2672         SKIP_DIGITS(next);
2673         int proc = __kmp_str_to_int(*scan, *next);
2674         KMP_ASSERT(proc >= 0);
2675         *scan = next;
2676     }
2677     else {
2678         KMP_WARNING( SyntaxErrorUsing, var, "\"threads\"" );
2679         return FALSE;
2680     }
2681     return TRUE;
2682 }
2683 
2684 static int
2685 __kmp_parse_place_list( const char *var, const char *env, char **place_list )
2686 {
2687     const char *scan = env;
2688     const char *next = scan;
2689 
2690     for (;;) {
2691         int start, count, stride;
2692 
2693         if (! __kmp_parse_place(var, &scan)) {
2694             return FALSE;
2695         }
2696 
2697         //
2698         // valid follow sets are ',' ':' and EOL
2699         //
2700         SKIP_WS(scan);
2701         if (*scan == '\0') {
2702             break;
2703         }
2704         if (*scan == ',') {
2705             scan++;     // skip ','
2706             continue;
2707         }
2708         if (*scan != ':') {
2709             KMP_WARNING( SyntaxErrorUsing, var, "\"threads\"" );
2710             return FALSE;
2711         }
2712         scan++;         // skip ':'
2713 
2714         //
2715         // Read count parameter
2716         //
2717         SKIP_WS(scan);
2718         if ((*scan < '0') || (*scan > '9')) {
2719             KMP_WARNING( SyntaxErrorUsing, var, "\"threads\"" );
2720             return FALSE;
2721         }
2722         next = scan;
2723         SKIP_DIGITS(next);
2724         count = __kmp_str_to_int(scan, *next);
2725         KMP_ASSERT(count >= 0);
2726         scan = next;
2727 
2728         //
2729         // valid follow sets are ',' ':' and EOL
2730         //
2731         SKIP_WS(scan);
2732         if (*scan == '\0') {
2733             break;
2734         }
2735         if (*scan == ',') {
2736             scan++;     // skip ','
2737             continue;
2738         }
2739         if (*scan != ':') {
2740             KMP_WARNING( SyntaxErrorUsing, var, "\"threads\"" );
2741             return FALSE;
2742         }
2743         scan++;         // skip ':'
2744 
2745         //
2746         // Read stride parameter
2747         //
2748         int sign = +1;
2749         for (;;) {
2750             SKIP_WS(scan);
2751             if (*scan == '+') {
2752                 scan++; // skip '+'
2753                 continue;
2754             }
2755             if (*scan == '-') {
2756                 sign *= -1;
2757                 scan++; // skip '-'
2758                 continue;
2759             }
2760             break;
2761         }
2762         SKIP_WS(scan);
2763         if ((*scan < '0') || (*scan > '9')) {
2764             KMP_WARNING( SyntaxErrorUsing, var, "\"threads\"" );
2765             return FALSE;
2766         }
2767         next = scan;
2768         SKIP_DIGITS(next);
2769         stride = __kmp_str_to_int(scan, *next);
2770         KMP_ASSERT(stride >= 0);
2771         scan = next;
2772         stride *= sign;
2773 
2774         //
2775         // valid follow sets are ',' and EOL
2776         //
2777         SKIP_WS(scan);
2778         if (*scan == '\0') {
2779             break;
2780         }
2781         if (*scan == ',') {
2782             scan++;     // skip ','
2783             continue;
2784         }
2785 
2786         KMP_WARNING( SyntaxErrorUsing, var, "\"threads\"" );
2787         return FALSE;
2788     }
2789 
2790     {
2791         int len = scan - env;
2792         char *retlist = (char *)__kmp_allocate((len + 1) * sizeof(char));
2793         KMP_MEMCPY_S(retlist, (len+1)*sizeof(char), env, len * sizeof(char));
2794         retlist[len] = '\0';
2795         *place_list = retlist;
2796     }
2797     return TRUE;
2798 }
2799 
2800 static void
2801 __kmp_stg_parse_places( char const * name, char const * value, void * data )
2802 {
2803     int count;
2804     const char *scan = value;
2805     const char *next = scan;
2806     const char *kind = "\"threads\"";
2807     kmp_setting_t **rivals = (kmp_setting_t **) data;
2808     int rc;
2809 
2810     rc = __kmp_stg_check_rivals( name, value, rivals );
2811     if ( rc ) {
2812         return;
2813     }
2814 
2815     //
2816     // If OMP_PROC_BIND is not specified but OMP_PLACES is,
2817     // then let OMP_PROC_BIND default to true.
2818     //
2819     if ( __kmp_nested_proc_bind.bind_types[0] == proc_bind_default ) {
2820         __kmp_nested_proc_bind.bind_types[0] = proc_bind_true;
2821     }
2822 
2823     //__kmp_affinity_num_places = 0;
2824 
2825     if ( __kmp_match_str( "threads", scan, &next ) ) {
2826         scan = next;
2827         __kmp_affinity_type = affinity_compact;
2828         __kmp_affinity_gran = affinity_gran_thread;
2829         __kmp_affinity_dups = FALSE;
2830         kind = "\"threads\"";
2831     }
2832     else if ( __kmp_match_str( "cores", scan, &next ) ) {
2833         scan = next;
2834         __kmp_affinity_type = affinity_compact;
2835         __kmp_affinity_gran = affinity_gran_core;
2836         __kmp_affinity_dups = FALSE;
2837         kind = "\"cores\"";
2838     }
2839     else if ( __kmp_match_str( "sockets", scan, &next ) ) {
2840         scan = next;
2841         __kmp_affinity_type = affinity_compact;
2842         __kmp_affinity_gran = affinity_gran_package;
2843         __kmp_affinity_dups = FALSE;
2844         kind = "\"sockets\"";
2845     }
2846     else {
2847         if ( __kmp_affinity_proclist != NULL ) {
2848             KMP_INTERNAL_FREE( (void *)__kmp_affinity_proclist );
2849             __kmp_affinity_proclist = NULL;
2850         }
2851         if ( __kmp_parse_place_list( name, value, &__kmp_affinity_proclist ) ) {
2852             __kmp_affinity_type = affinity_explicit;
2853             __kmp_affinity_gran = affinity_gran_fine;
2854             __kmp_affinity_dups = FALSE;
2855             if ( __kmp_nested_proc_bind.bind_types[0] == proc_bind_default ) {
2856                  __kmp_nested_proc_bind.bind_types[0] = proc_bind_true;
2857             }
2858         }
2859         return;
2860     }
2861 
2862     if ( __kmp_nested_proc_bind.bind_types[0] == proc_bind_default ) {
2863         __kmp_nested_proc_bind.bind_types[0] = proc_bind_true;
2864     }
2865 
2866     SKIP_WS(scan);
2867     if ( *scan == '\0' ) {
2868         return;
2869     }
2870 
2871     //
2872     // Parse option count parameter in parentheses
2873     //
2874     if ( *scan != '(' ) {
2875         KMP_WARNING( SyntaxErrorUsing, name, kind );
2876         return;
2877     }
2878     scan++;     // skip '('
2879 
2880     SKIP_WS(scan);
2881     next = scan;
2882     SKIP_DIGITS(next);
2883     count = __kmp_str_to_int(scan, *next);
2884     KMP_ASSERT(count >= 0);
2885     scan = next;
2886 
2887     SKIP_WS(scan);
2888     if ( *scan != ')' ) {
2889         KMP_WARNING( SyntaxErrorUsing, name, kind );
2890         return;
2891     }
2892     scan++;     // skip ')'
2893 
2894     SKIP_WS(scan);
2895     if ( *scan != '\0' ) {
2896         KMP_WARNING( ParseExtraCharsWarn, name, scan );
2897     }
2898     __kmp_affinity_num_places = count;
2899 }
2900 
2901 static void
2902 __kmp_stg_print_places( kmp_str_buf_t * buffer, char const * name,
2903   void * data )
2904 {
2905     if( __kmp_env_format ) {
2906         KMP_STR_BUF_PRINT_NAME;
2907     } else {
2908         __kmp_str_buf_print( buffer, "   %s", name );
2909     }
2910     if ( ( __kmp_nested_proc_bind.used == 0 )
2911       || ( __kmp_nested_proc_bind.bind_types == NULL )
2912       || ( __kmp_nested_proc_bind.bind_types[0] == proc_bind_false ) ) {
2913         __kmp_str_buf_print( buffer, ": %s\n", KMP_I18N_STR( NotDefined ) );
2914     }
2915     else if ( __kmp_affinity_type == affinity_explicit ) {
2916         if ( __kmp_affinity_proclist != NULL ) {
2917             __kmp_str_buf_print( buffer, "='%s'\n", __kmp_affinity_proclist );
2918         }
2919         else {
2920             __kmp_str_buf_print( buffer, ": %s\n", KMP_I18N_STR( NotDefined ) );
2921         }
2922     }
2923     else if ( __kmp_affinity_type == affinity_compact ) {
2924         int num;
2925         if ( __kmp_affinity_num_masks > 0 ) {
2926             num = __kmp_affinity_num_masks;
2927         }
2928         else if ( __kmp_affinity_num_places > 0 ) {
2929             num = __kmp_affinity_num_places;
2930         }
2931         else {
2932             num = 0;
2933         }
2934         if ( __kmp_affinity_gran == affinity_gran_thread ) {
2935             if ( num > 0 ) {
2936                 __kmp_str_buf_print( buffer, "='threads(%d)'\n", num );
2937             }
2938             else {
2939                 __kmp_str_buf_print( buffer, "='threads'\n" );
2940             }
2941         }
2942         else if ( __kmp_affinity_gran == affinity_gran_core ) {
2943             if ( num > 0 ) {
2944                 __kmp_str_buf_print( buffer, "='cores(%d)' \n", num );
2945             }
2946             else {
2947                 __kmp_str_buf_print( buffer, "='cores'\n" );
2948             }
2949         }
2950         else if ( __kmp_affinity_gran == affinity_gran_package ) {
2951             if ( num > 0 ) {
2952                 __kmp_str_buf_print( buffer, "='sockets(%d)'\n", num );
2953             }
2954             else {
2955                 __kmp_str_buf_print( buffer, "='sockets'\n" );
2956             }
2957         }
2958         else {
2959             __kmp_str_buf_print( buffer, ": %s\n", KMP_I18N_STR( NotDefined ) );
2960         }
2961     }
2962     else {
2963         __kmp_str_buf_print( buffer, ": %s\n", KMP_I18N_STR( NotDefined ) );
2964     }
2965 }
2966 
2967 # endif /* OMP_40_ENABLED */
2968 
2969 # if (! OMP_40_ENABLED)
2970 
2971 static void
2972 __kmp_stg_parse_proc_bind( char const * name, char const * value, void * data )
2973 {
2974     int enabled;
2975     kmp_setting_t **rivals = (kmp_setting_t **) data;
2976     int rc;
2977 
2978     rc = __kmp_stg_check_rivals( name, value, rivals );
2979     if ( rc ) {
2980         return;
2981     }
2982 
2983     //
2984     // in OMP 3.1, OMP_PROC_BIND is strictly a boolean
2985     //
2986     __kmp_stg_parse_bool( name, value, & enabled );
2987     if ( enabled ) {
2988             //
2989             // OMP_PROC_BIND => granularity=fine,scatter on MIC
2990             // OMP_PROC_BIND => granularity=core,scatter elsewhere
2991             //
2992             __kmp_affinity_type = affinity_scatter;
2993 #  if KMP_ARCH_X86_64 && (KMP_OS_LINUX || KMP_OS_WINDOWS)
2994             if( __kmp_mic_type != non_mic )
2995                 __kmp_affinity_gran = affinity_gran_fine;
2996             else
2997 #  endif
2998                 __kmp_affinity_gran = affinity_gran_core;
2999     }
3000     else {
3001         __kmp_affinity_type = affinity_none;
3002     }
3003 } // __kmp_parse_proc_bind
3004 
3005 # endif /* if (! OMP_40_ENABLED) */
3006 
3007 
3008 static void
3009 __kmp_stg_parse_topology_method( char const * name, char const * value,
3010   void * data ) {
3011     if ( __kmp_str_match( "all", 1, value ) ) {
3012        __kmp_affinity_top_method = affinity_top_method_all;
3013     }
3014 # if KMP_ARCH_X86 || KMP_ARCH_X86_64
3015     else if ( __kmp_str_match( "x2apic id", 9, value )
3016       || __kmp_str_match( "x2apic_id", 9, value )
3017       || __kmp_str_match( "x2apic-id", 9, value )
3018       || __kmp_str_match( "x2apicid", 8, value )
3019       || __kmp_str_match( "cpuid leaf 11", 13, value )
3020       || __kmp_str_match( "cpuid_leaf_11", 13, value )
3021       || __kmp_str_match( "cpuid-leaf-11", 13, value )
3022       || __kmp_str_match( "cpuid leaf11", 12, value )
3023       || __kmp_str_match( "cpuid_leaf11", 12, value )
3024       || __kmp_str_match( "cpuid-leaf11", 12, value )
3025       || __kmp_str_match( "cpuidleaf 11", 12, value )
3026       || __kmp_str_match( "cpuidleaf_11", 12, value )
3027       || __kmp_str_match( "cpuidleaf-11", 12, value )
3028       || __kmp_str_match( "cpuidleaf11", 11, value )
3029       || __kmp_str_match( "cpuid 11", 8, value )
3030       || __kmp_str_match( "cpuid_11", 8, value )
3031       || __kmp_str_match( "cpuid-11", 8, value )
3032       || __kmp_str_match( "cpuid11", 7, value )
3033       || __kmp_str_match( "leaf 11", 7, value )
3034       || __kmp_str_match( "leaf_11", 7, value )
3035       || __kmp_str_match( "leaf-11", 7, value )
3036       || __kmp_str_match( "leaf11", 6, value ) ) {
3037         __kmp_affinity_top_method = affinity_top_method_x2apicid;
3038     }
3039     else if ( __kmp_str_match( "apic id", 7, value )
3040       || __kmp_str_match( "apic_id", 7, value )
3041       || __kmp_str_match( "apic-id", 7, value )
3042       || __kmp_str_match( "apicid", 6, value )
3043       || __kmp_str_match( "cpuid leaf 4", 12, value )
3044       || __kmp_str_match( "cpuid_leaf_4", 12, value )
3045       || __kmp_str_match( "cpuid-leaf-4", 12, value )
3046       || __kmp_str_match( "cpuid leaf4", 11, value )
3047       || __kmp_str_match( "cpuid_leaf4", 11, value )
3048       || __kmp_str_match( "cpuid-leaf4", 11, value )
3049       || __kmp_str_match( "cpuidleaf 4", 11, value )
3050       || __kmp_str_match( "cpuidleaf_4", 11, value )
3051       || __kmp_str_match( "cpuidleaf-4", 11, value )
3052       || __kmp_str_match( "cpuidleaf4", 10, value )
3053       || __kmp_str_match( "cpuid 4", 7, value )
3054       || __kmp_str_match( "cpuid_4", 7, value )
3055       || __kmp_str_match( "cpuid-4", 7, value )
3056       || __kmp_str_match( "cpuid4", 6, value )
3057       || __kmp_str_match( "leaf 4", 6, value )
3058       || __kmp_str_match( "leaf_4", 6, value )
3059       || __kmp_str_match( "leaf-4", 6, value )
3060       || __kmp_str_match( "leaf4", 5, value ) ) {
3061         __kmp_affinity_top_method = affinity_top_method_apicid;
3062     }
3063 # endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
3064     else if ( __kmp_str_match( "/proc/cpuinfo", 2, value )
3065       || __kmp_str_match( "cpuinfo", 5, value )) {
3066         __kmp_affinity_top_method = affinity_top_method_cpuinfo;
3067     }
3068 # if KMP_GROUP_AFFINITY
3069     else if ( __kmp_str_match( "group", 1, value ) ) {
3070         __kmp_affinity_top_method = affinity_top_method_group;
3071     }
3072 # endif /* KMP_GROUP_AFFINITY */
3073     else if ( __kmp_str_match( "flat", 1, value ) ) {
3074         __kmp_affinity_top_method = affinity_top_method_flat;
3075     }
3076 # if KMP_USE_HWLOC
3077     else if ( __kmp_str_match( "hwloc", 1, value) ) {
3078         __kmp_affinity_top_method = affinity_top_method_hwloc;
3079     }
3080 # endif
3081     else {
3082         KMP_WARNING( StgInvalidValue, name, value );
3083     }
3084 } // __kmp_stg_parse_topology_method
3085 
3086 static void
3087 __kmp_stg_print_topology_method( kmp_str_buf_t * buffer, char const * name,
3088   void * data ) {
3089 # if KMP_DEBUG
3090     char const * value = NULL;
3091 
3092     switch ( __kmp_affinity_top_method ) {
3093         case affinity_top_method_default:
3094         value = "default";
3095         break;
3096 
3097         case affinity_top_method_all:
3098         value = "all";
3099         break;
3100 
3101 #  if KMP_ARCH_X86 || KMP_ARCH_X86_64
3102         case affinity_top_method_x2apicid:
3103         value = "x2APIC id";
3104         break;
3105 
3106         case affinity_top_method_apicid:
3107         value = "APIC id";
3108         break;
3109 #  endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
3110 
3111         case affinity_top_method_cpuinfo:
3112         value = "cpuinfo";
3113         break;
3114 
3115 #  if KMP_GROUP_AFFINITY
3116         case affinity_top_method_group:
3117         value = "group";
3118         break;
3119 #  endif /* KMP_GROUP_AFFINITY */
3120 
3121         case affinity_top_method_flat:
3122         value = "flat";
3123         break;
3124     }
3125 
3126     if ( value != NULL ) {
3127         __kmp_stg_print_str( buffer, name, value );
3128     }
3129 # endif /* KMP_DEBUG */
3130 } // __kmp_stg_print_topology_method
3131 
3132 #endif /* KMP_AFFINITY_SUPPORTED */
3133 
3134 
3135 #if OMP_40_ENABLED
3136 
3137 //
3138 // OMP_PROC_BIND / bind-var is functional on all 4.0 builds, including OS X*
3139 // OMP_PLACES / place-partition-var is not.
3140 //
3141 static void
3142 __kmp_stg_parse_proc_bind( char const * name, char const * value, void * data )
3143 {
3144     kmp_setting_t **rivals = (kmp_setting_t **) data;
3145     int rc;
3146 
3147     rc = __kmp_stg_check_rivals( name, value, rivals );
3148     if ( rc ) {
3149         return;
3150     }
3151 
3152     //
3153     // in OMP 4.0 OMP_PROC_BIND is a vector of proc_bind types.
3154     //
3155     KMP_DEBUG_ASSERT( (__kmp_nested_proc_bind.bind_types != NULL)
3156       && ( __kmp_nested_proc_bind.used > 0 ) );
3157 
3158     const char *buf = value;
3159     const char *next;
3160     int num;
3161     SKIP_WS( buf );
3162     if ( (*buf >= '0') && (*buf <= '9') ) {
3163         next = buf;
3164         SKIP_DIGITS( next );
3165         num = __kmp_str_to_int( buf, *next );
3166         KMP_ASSERT( num >= 0 );
3167         buf = next;
3168         SKIP_WS( buf );
3169     }
3170     else {
3171         num = -1;
3172     }
3173 
3174     next = buf;
3175     if ( __kmp_match_str( "disabled", buf, &next ) ) {
3176         buf = next;
3177         SKIP_WS( buf );
3178 # if KMP_AFFINITY_SUPPORTED
3179         __kmp_affinity_type = affinity_disabled;
3180 # endif /* KMP_AFFINITY_SUPPORTED */
3181         __kmp_nested_proc_bind.used = 1;
3182         __kmp_nested_proc_bind.bind_types[0] = proc_bind_false;
3183     }
3184     else if ( ( num == (int)proc_bind_false )
3185       || __kmp_match_str( "false", buf, &next ) ) {
3186         buf = next;
3187         SKIP_WS( buf );
3188 # if KMP_AFFINITY_SUPPORTED
3189         __kmp_affinity_type = affinity_none;
3190 # endif /* KMP_AFFINITY_SUPPORTED */
3191         __kmp_nested_proc_bind.used = 1;
3192         __kmp_nested_proc_bind.bind_types[0] = proc_bind_false;
3193     }
3194     else if ( ( num == (int)proc_bind_true )
3195       || __kmp_match_str( "true", buf, &next ) ) {
3196         buf = next;
3197         SKIP_WS( buf );
3198         __kmp_nested_proc_bind.used = 1;
3199         __kmp_nested_proc_bind.bind_types[0] = proc_bind_true;
3200     }
3201     else {
3202         //
3203         // Count the number of values in the env var string
3204         //
3205         const char *scan;
3206         int nelem = 1;
3207         for ( scan = buf; *scan != '\0'; scan++ ) {
3208             if ( *scan == ',' ) {
3209                 nelem++;
3210             }
3211         }
3212 
3213         //
3214         // Create / expand the nested proc_bind array as needed
3215         //
3216         if ( __kmp_nested_proc_bind.size < nelem ) {
3217             __kmp_nested_proc_bind.bind_types = (kmp_proc_bind_t *)
3218                 KMP_INTERNAL_REALLOC( __kmp_nested_proc_bind.bind_types,
3219                 sizeof(kmp_proc_bind_t) * nelem );
3220             if ( __kmp_nested_proc_bind.bind_types == NULL ) {
3221                 KMP_FATAL( MemoryAllocFailed );
3222             }
3223             __kmp_nested_proc_bind.size = nelem;
3224         }
3225         __kmp_nested_proc_bind.used = nelem;
3226 
3227         //
3228         // Save values in the nested proc_bind array
3229         //
3230         int i = 0;
3231         for (;;) {
3232             enum kmp_proc_bind_t bind;
3233 
3234             if ( ( num == (int)proc_bind_master )
3235               || __kmp_match_str( "master", buf, &next ) ) {
3236                 buf = next;
3237                 SKIP_WS( buf );
3238                 bind = proc_bind_master;
3239             }
3240             else if ( ( num == (int)proc_bind_close )
3241               || __kmp_match_str( "close", buf, &next ) ) {
3242                 buf = next;
3243                 SKIP_WS( buf );
3244                 bind = proc_bind_close;
3245             }
3246             else if ( ( num == (int)proc_bind_spread )
3247               || __kmp_match_str( "spread", buf, &next ) ) {
3248                 buf = next;
3249                 SKIP_WS( buf );
3250                 bind = proc_bind_spread;
3251             }
3252             else {
3253                 KMP_WARNING( StgInvalidValue, name, value );
3254                 __kmp_nested_proc_bind.bind_types[0] = proc_bind_false;
3255                 __kmp_nested_proc_bind.used = 1;
3256                 return;
3257             }
3258 
3259             __kmp_nested_proc_bind.bind_types[i++] = bind;
3260             if ( i >= nelem ) {
3261                 break;
3262             }
3263             KMP_DEBUG_ASSERT( *buf == ',' );
3264             buf++;
3265             SKIP_WS( buf );
3266 
3267             //
3268             // Read next value if it was specified as an integer
3269             //
3270             if ( (*buf >= '0') && (*buf <= '9') ) {
3271                 next = buf;
3272                 SKIP_DIGITS( next );
3273                 num = __kmp_str_to_int( buf, *next );
3274                 KMP_ASSERT( num >= 0 );
3275                 buf = next;
3276                 SKIP_WS( buf );
3277             }
3278             else {
3279                 num = -1;
3280             }
3281         }
3282         SKIP_WS( buf );
3283     }
3284     if ( *buf != '\0' ) {
3285         KMP_WARNING( ParseExtraCharsWarn, name, buf );
3286     }
3287 }
3288 
3289 
3290 static void
3291 __kmp_stg_print_proc_bind( kmp_str_buf_t * buffer, char const * name,
3292   void * data )
3293 {
3294     int nelem = __kmp_nested_proc_bind.used;
3295     if( __kmp_env_format ) {
3296         KMP_STR_BUF_PRINT_NAME;
3297     } else {
3298         __kmp_str_buf_print( buffer, "   %s", name );
3299     }
3300     if ( nelem == 0 ) {
3301         __kmp_str_buf_print( buffer, ": %s\n", KMP_I18N_STR( NotDefined ) );
3302     }
3303     else {
3304         int i;
3305         __kmp_str_buf_print( buffer, "='", name );
3306         for ( i = 0; i < nelem; i++ ) {
3307             switch ( __kmp_nested_proc_bind.bind_types[i] ) {
3308                 case proc_bind_false:
3309                 __kmp_str_buf_print( buffer, "false" );
3310                 break;
3311 
3312                 case proc_bind_true:
3313                 __kmp_str_buf_print( buffer, "true" );
3314                 break;
3315 
3316                 case proc_bind_master:
3317                 __kmp_str_buf_print( buffer, "master" );
3318                 break;
3319 
3320                 case proc_bind_close:
3321                 __kmp_str_buf_print( buffer, "close" );
3322                 break;
3323 
3324                 case proc_bind_spread:
3325                 __kmp_str_buf_print( buffer, "spread" );
3326                 break;
3327 
3328                 case proc_bind_intel:
3329                 __kmp_str_buf_print( buffer, "intel" );
3330                 break;
3331 
3332                 case proc_bind_default:
3333                 __kmp_str_buf_print( buffer, "default" );
3334                 break;
3335             }
3336             if ( i < nelem - 1 ) {
3337                 __kmp_str_buf_print( buffer, "," );
3338             }
3339         }
3340         __kmp_str_buf_print( buffer, "'\n" );
3341     }
3342 }
3343 
3344 #endif /* OMP_40_ENABLED */
3345 
3346 
3347 // -------------------------------------------------------------------------------------------------
3348 // OMP_DYNAMIC
3349 // -------------------------------------------------------------------------------------------------
3350 
3351 static void
3352 __kmp_stg_parse_omp_dynamic( char const * name, char const * value, void * data )
3353 {
3354     __kmp_stg_parse_bool( name, value, & (__kmp_global.g.g_dynamic) );
3355 } // __kmp_stg_parse_omp_dynamic
3356 
3357 static void
3358 __kmp_stg_print_omp_dynamic( kmp_str_buf_t * buffer, char const * name, void * data )
3359 {
3360     __kmp_stg_print_bool( buffer, name, __kmp_global.g.g_dynamic );
3361 } // __kmp_stg_print_omp_dynamic
3362 
3363 static void
3364 __kmp_stg_parse_kmp_dynamic_mode( char const * name, char const * value, void * data )
3365 {
3366     if ( TCR_4(__kmp_init_parallel) ) {
3367         KMP_WARNING( EnvParallelWarn, name );
3368         __kmp_env_toPrint( name, 0 );
3369         return;
3370     }
3371 #ifdef USE_LOAD_BALANCE
3372     else if ( __kmp_str_match( "load balance", 2, value )
3373       || __kmp_str_match( "load_balance", 2, value )
3374       || __kmp_str_match( "load-balance", 2, value )
3375       || __kmp_str_match( "loadbalance", 2, value )
3376       || __kmp_str_match( "balance", 1, value ) ) {
3377         __kmp_global.g.g_dynamic_mode = dynamic_load_balance;
3378     }
3379 #endif /* USE_LOAD_BALANCE */
3380     else if ( __kmp_str_match( "thread limit", 1, value )
3381       || __kmp_str_match( "thread_limit", 1, value )
3382       || __kmp_str_match( "thread-limit", 1, value )
3383       || __kmp_str_match( "threadlimit", 1, value )
3384       || __kmp_str_match( "limit", 2, value ) ) {
3385         __kmp_global.g.g_dynamic_mode = dynamic_thread_limit;
3386     }
3387     else if ( __kmp_str_match( "random", 1, value ) ) {
3388         __kmp_global.g.g_dynamic_mode = dynamic_random;
3389     }
3390     else {
3391         KMP_WARNING( StgInvalidValue, name, value );
3392     }
3393 } //__kmp_stg_parse_kmp_dynamic_mode
3394 
3395 static void
3396 __kmp_stg_print_kmp_dynamic_mode( kmp_str_buf_t * buffer, char const * name, void * data )
3397 {
3398 #if KMP_DEBUG
3399     if ( __kmp_global.g.g_dynamic_mode == dynamic_default ) {
3400         __kmp_str_buf_print( buffer, "   %s: %s \n", name, KMP_I18N_STR( NotDefined ) );
3401     }
3402 # ifdef USE_LOAD_BALANCE
3403     else if ( __kmp_global.g.g_dynamic_mode == dynamic_load_balance ) {
3404         __kmp_stg_print_str( buffer, name, "load balance" );
3405     }
3406 # endif /* USE_LOAD_BALANCE */
3407     else if ( __kmp_global.g.g_dynamic_mode == dynamic_thread_limit ) {
3408         __kmp_stg_print_str( buffer, name, "thread limit" );
3409     }
3410     else if ( __kmp_global.g.g_dynamic_mode == dynamic_random ) {
3411         __kmp_stg_print_str( buffer, name, "random" );
3412     }
3413     else {
3414         KMP_ASSERT(0);
3415     }
3416 #endif /* KMP_DEBUG */
3417 } // __kmp_stg_print_kmp_dynamic_mode
3418 
3419 
3420 #ifdef USE_LOAD_BALANCE
3421 
3422 // -------------------------------------------------------------------------------------------------
3423 // KMP_LOAD_BALANCE_INTERVAL
3424 // -------------------------------------------------------------------------------------------------
3425 
3426 static void
3427 __kmp_stg_parse_ld_balance_interval( char const * name, char const * value, void * data )
3428 {
3429     double interval = __kmp_convert_to_double( value );
3430     if ( interval >= 0 ) {
3431         __kmp_load_balance_interval = interval;
3432     } else {
3433         KMP_WARNING( StgInvalidValue, name, value );
3434     }; // if
3435 } // __kmp_stg_parse_load_balance_interval
3436 
3437 static void
3438 __kmp_stg_print_ld_balance_interval( kmp_str_buf_t * buffer, char const * name, void * data ) {
3439 #if KMP_DEBUG
3440     __kmp_str_buf_print( buffer, "   %s=%8.6f\n", name, __kmp_load_balance_interval );
3441 #endif /* KMP_DEBUG */
3442 } // __kmp_stg_print_load_balance_interval
3443 
3444 #endif /* USE_LOAD_BALANCE */
3445 
3446 // -------------------------------------------------------------------------------------------------
3447 // KMP_INIT_AT_FORK
3448 // -------------------------------------------------------------------------------------------------
3449 
3450 static void
3451 __kmp_stg_parse_init_at_fork( char const * name, char const * value, void * data ) {
3452     __kmp_stg_parse_bool( name, value, & __kmp_need_register_atfork );
3453     if ( __kmp_need_register_atfork ) {
3454         __kmp_need_register_atfork_specified = TRUE;
3455     };
3456 } // __kmp_stg_parse_init_at_fork
3457 
3458 static void
3459 __kmp_stg_print_init_at_fork( kmp_str_buf_t * buffer, char const * name, void * data ) {
3460     __kmp_stg_print_bool( buffer, name, __kmp_need_register_atfork_specified );
3461 } // __kmp_stg_print_init_at_fork
3462 
3463 // -------------------------------------------------------------------------------------------------
3464 // KMP_SCHEDULE
3465 // -------------------------------------------------------------------------------------------------
3466 
3467 static void
3468 __kmp_stg_parse_schedule( char const * name, char const * value, void * data ) {
3469 
3470     if ( value != NULL ) {
3471         size_t length = KMP_STRLEN( value );
3472         if ( length > INT_MAX ) {
3473             KMP_WARNING( LongValue, name );
3474         } else {
3475             char *semicolon;
3476             if( value[ length - 1 ] == '"' || value[ length -1 ] == '\'' )
3477                 KMP_WARNING( UnbalancedQuotes, name );
3478             do {
3479                 char sentinel;
3480 
3481                 semicolon = (char *) strchr( value, ';' );
3482                 if( *value && semicolon != value ) {
3483                     char *comma = (char *) strchr( value, ',' );
3484 
3485                     if ( comma ) {
3486                         ++comma;
3487                         sentinel = ',';
3488                     } else
3489                         sentinel = ';';
3490                     if ( !__kmp_strcasecmp_with_sentinel( "static", value, sentinel ) ) {
3491                         if( !__kmp_strcasecmp_with_sentinel( "greedy", comma, ';' ) ) {
3492                             __kmp_static = kmp_sch_static_greedy;
3493                             continue;
3494                         } else if( !__kmp_strcasecmp_with_sentinel( "balanced", comma, ';' ) ) {
3495                             __kmp_static = kmp_sch_static_balanced;
3496                             continue;
3497                         }
3498                     } else if ( !__kmp_strcasecmp_with_sentinel( "guided", value, sentinel ) ) {
3499                         if ( !__kmp_strcasecmp_with_sentinel( "iterative", comma, ';' ) ) {
3500                             __kmp_guided = kmp_sch_guided_iterative_chunked;
3501                             continue;
3502                         } else if ( !__kmp_strcasecmp_with_sentinel( "analytical", comma, ';' ) ) {
3503                             /* analytical not allowed for too many threads */
3504                             __kmp_guided = kmp_sch_guided_analytical_chunked;
3505                             continue;
3506                         }
3507                     }
3508                     KMP_WARNING( InvalidClause, name, value );
3509                 } else
3510                     KMP_WARNING( EmptyClause, name );
3511             } while ( (value = semicolon ? semicolon + 1 : NULL) );
3512         }
3513     }; // if
3514 
3515 } // __kmp_stg_parse__schedule
3516 
3517 static void
3518 __kmp_stg_print_schedule( kmp_str_buf_t * buffer, char const * name, void * data ) {
3519     if( __kmp_env_format ) {
3520         KMP_STR_BUF_PRINT_NAME_EX(name);
3521     } else {
3522         __kmp_str_buf_print( buffer, "   %s='", name );
3523     }
3524     if ( __kmp_static == kmp_sch_static_greedy ) {
3525         __kmp_str_buf_print( buffer, "%s", "static,greedy");
3526     } else if ( __kmp_static == kmp_sch_static_balanced ) {
3527         __kmp_str_buf_print ( buffer, "%s", "static,balanced");
3528     }
3529     if ( __kmp_guided == kmp_sch_guided_iterative_chunked ) {
3530         __kmp_str_buf_print( buffer, ";%s'\n", "guided,iterative");
3531     } else if ( __kmp_guided == kmp_sch_guided_analytical_chunked ) {
3532         __kmp_str_buf_print( buffer, ";%s'\n", "guided,analytical");
3533     }
3534 } // __kmp_stg_print_schedule
3535 
3536 // -------------------------------------------------------------------------------------------------
3537 // OMP_SCHEDULE
3538 // -------------------------------------------------------------------------------------------------
3539 
3540 static void
3541 __kmp_stg_parse_omp_schedule( char const * name, char const * value, void * data )
3542 {
3543     size_t      length;
3544     if( value ) {
3545         length = KMP_STRLEN( value );
3546         if( length ) {
3547             char *comma = (char *) strchr( value, ',' );
3548             if( value[ length - 1 ] == '"' || value[ length -1 ] == '\'')
3549                 KMP_WARNING( UnbalancedQuotes, name );
3550             /* get the specified scheduling style */
3551             if (!__kmp_strcasecmp_with_sentinel("dynamic", value, ','))          /* DYNAMIC */
3552                 __kmp_sched = kmp_sch_dynamic_chunked;
3553             else if (!__kmp_strcasecmp_with_sentinel("guided", value, ','))      /* GUIDED */
3554                 __kmp_sched = kmp_sch_guided_chunked;
3555 // AC: TODO: add AUTO schedule, and pprobably remove TRAPEZOIDAL (OMP 3.0 does not allow it)
3556             else if (!__kmp_strcasecmp_with_sentinel("auto", value, ',')) {       /* AUTO */
3557                 __kmp_sched = kmp_sch_auto;
3558                 if( comma ) {
3559                     __kmp_msg( kmp_ms_warning, KMP_MSG( IgnoreChunk, name, comma ), __kmp_msg_null );
3560                     comma = NULL;
3561                 }
3562             }
3563             else if (!__kmp_strcasecmp_with_sentinel("trapezoidal", value, ',')) /* TRAPEZOIDAL */
3564                 __kmp_sched = kmp_sch_trapezoidal;
3565             else if (!__kmp_strcasecmp_with_sentinel("static", value, ','))      /* STATIC */
3566                 __kmp_sched = kmp_sch_static;
3567 #if KMP_STATIC_STEAL_ENABLED
3568             else if (!__kmp_strcasecmp_with_sentinel("static_steal", value, ','))
3569                 __kmp_sched = kmp_sch_static_steal;
3570 #endif
3571             else {
3572                 KMP_WARNING( StgInvalidValue, name, value );
3573                 value = NULL; /* skip processing of comma */
3574             }
3575             if( value && comma ) {
3576                 __kmp_env_chunk = TRUE;
3577 
3578                 if(__kmp_sched == kmp_sch_static)
3579                     __kmp_sched = kmp_sch_static_chunked;
3580                 ++comma;
3581                 __kmp_chunk = __kmp_str_to_int( comma, 0 );
3582                 if ( __kmp_chunk < 1 ) {
3583                     __kmp_chunk = KMP_DEFAULT_CHUNK;
3584                     __kmp_msg( kmp_ms_warning, KMP_MSG( InvalidChunk, name, comma ), __kmp_msg_null );
3585                     KMP_INFORM( Using_int_Value, name, __kmp_chunk );
3586 // AC: next block commented out until KMP_DEFAULT_CHUNK != KMP_MIN_CHUNK (to improve code coverage :)
3587 //     The default chunk size is 1 according to standard, thus making KMP_MIN_CHUNK not 1 we would introduce mess:
3588 //     wrong chunk becomes 1, but it will be impossible to explicitely set 1, because it becomes KMP_MIN_CHUNK...
3589 //                } else if ( __kmp_chunk < KMP_MIN_CHUNK ) {
3590 //                    __kmp_chunk = KMP_MIN_CHUNK;
3591                 } else if ( __kmp_chunk > KMP_MAX_CHUNK ) {
3592                     __kmp_chunk = KMP_MAX_CHUNK;
3593                     __kmp_msg( kmp_ms_warning, KMP_MSG( LargeChunk, name, comma ), __kmp_msg_null );
3594                     KMP_INFORM( Using_int_Value, name, __kmp_chunk );
3595                 }
3596             } else
3597                 __kmp_env_chunk = FALSE;
3598         } else
3599             KMP_WARNING( EmptyString, name );
3600     }
3601     K_DIAG(1, ("__kmp_static == %d\n", __kmp_static))
3602     K_DIAG(1, ("__kmp_guided == %d\n", __kmp_guided))
3603     K_DIAG(1, ("__kmp_sched == %d\n", __kmp_sched))
3604     K_DIAG(1, ("__kmp_chunk == %d\n", __kmp_chunk))
3605 } // __kmp_stg_parse_omp_schedule
3606 
3607 static void
3608 __kmp_stg_print_omp_schedule( kmp_str_buf_t * buffer, char const * name, void * data ) {
3609     if( __kmp_env_format ) {
3610         KMP_STR_BUF_PRINT_NAME_EX(name);
3611     } else {
3612         __kmp_str_buf_print( buffer, "   %s='", name );
3613     }
3614     if ( __kmp_chunk ) {
3615         switch ( __kmp_sched ) {
3616             case kmp_sch_dynamic_chunked:
3617                 __kmp_str_buf_print( buffer, "%s,%d'\n", "dynamic", __kmp_chunk);
3618                 break;
3619             case kmp_sch_guided_iterative_chunked:
3620             case kmp_sch_guided_analytical_chunked:
3621                 __kmp_str_buf_print( buffer, "%s,%d'\n", "guided", __kmp_chunk);
3622                 break;
3623             case kmp_sch_trapezoidal:
3624                 __kmp_str_buf_print( buffer, "%s,%d'\n", "trapezoidal", __kmp_chunk);
3625                 break;
3626             case kmp_sch_static:
3627             case kmp_sch_static_chunked:
3628             case kmp_sch_static_balanced:
3629             case kmp_sch_static_greedy:
3630                 __kmp_str_buf_print( buffer, "%s,%d'\n", "static", __kmp_chunk);
3631                 break;
3632             case kmp_sch_static_steal:
3633                 __kmp_str_buf_print( buffer, "%s,%d'\n", "static_steal", __kmp_chunk);
3634                 break;
3635             case kmp_sch_auto:
3636                 __kmp_str_buf_print( buffer, "%s,%d'\n", "auto", __kmp_chunk);
3637                 break;
3638         }
3639     } else {
3640         switch ( __kmp_sched ) {
3641             case kmp_sch_dynamic_chunked:
3642                 __kmp_str_buf_print( buffer, "%s'\n", "dynamic");
3643                 break;
3644             case kmp_sch_guided_iterative_chunked:
3645             case kmp_sch_guided_analytical_chunked:
3646                 __kmp_str_buf_print( buffer, "%s'\n", "guided");
3647                 break;
3648             case kmp_sch_trapezoidal:
3649                 __kmp_str_buf_print( buffer, "%s'\n", "trapezoidal");
3650                 break;
3651             case kmp_sch_static:
3652             case kmp_sch_static_chunked:
3653             case kmp_sch_static_balanced:
3654             case kmp_sch_static_greedy:
3655                 __kmp_str_buf_print( buffer, "%s'\n", "static");
3656                 break;
3657             case kmp_sch_static_steal:
3658                 __kmp_str_buf_print( buffer, "%s'\n", "static_steal");
3659                 break;
3660             case kmp_sch_auto:
3661                 __kmp_str_buf_print( buffer, "%s'\n", "auto");
3662                 break;
3663         }
3664     }
3665 } // __kmp_stg_print_omp_schedule
3666 
3667 // -------------------------------------------------------------------------------------------------
3668 // KMP_ATOMIC_MODE
3669 // -------------------------------------------------------------------------------------------------
3670 
3671 static void
3672 __kmp_stg_parse_atomic_mode( char const * name, char const * value, void * data ) {
3673     // Modes: 0 -- do not change default; 1 -- Intel perf mode, 2 -- GOMP compatibility mode.
3674     int mode = 0;
3675     int max  = 1;
3676     #ifdef KMP_GOMP_COMPAT
3677         max = 2;
3678     #endif /* KMP_GOMP_COMPAT */
3679     __kmp_stg_parse_int( name, value, 0, max, & mode );
3680     // TODO; parse_int is not very suitable for this case. In case of overflow it is better to use
3681     // 0 rather that max value.
3682     if ( mode > 0 ) {
3683         __kmp_atomic_mode = mode;
3684     }; // if
3685 } // __kmp_stg_parse_atomic_mode
3686 
3687 static void
3688 __kmp_stg_print_atomic_mode( kmp_str_buf_t * buffer, char const * name, void * data ) {
3689     __kmp_stg_print_int( buffer, name, __kmp_atomic_mode );
3690 } // __kmp_stg_print_atomic_mode
3691 
3692 
3693 // -------------------------------------------------------------------------------------------------
3694 // KMP_CONSISTENCY_CHECK
3695 // -------------------------------------------------------------------------------------------------
3696 
3697 static void
3698 __kmp_stg_parse_consistency_check( char const * name, char const * value, void * data ) {
3699     if ( ! __kmp_strcasecmp_with_sentinel( "all", value, 0 ) ) {
3700         // Note, this will not work from kmp_set_defaults because th_cons stack was not allocated
3701         // for existed thread(s) thus the first __kmp_push_<construct> will break with assertion.
3702         // TODO: allocate th_cons if called from kmp_set_defaults.
3703         __kmp_env_consistency_check = TRUE;
3704     } else if ( ! __kmp_strcasecmp_with_sentinel( "none", value, 0 ) ) {
3705         __kmp_env_consistency_check = FALSE;
3706     } else {
3707         KMP_WARNING( StgInvalidValue, name, value );
3708     }; // if
3709 } // __kmp_stg_parse_consistency_check
3710 
3711 static void
3712 __kmp_stg_print_consistency_check( kmp_str_buf_t * buffer, char const * name, void * data ) {
3713 #if KMP_DEBUG
3714     const char *value = NULL;
3715 
3716     if ( __kmp_env_consistency_check ) {
3717         value = "all";
3718     } else {
3719         value = "none";
3720     }
3721 
3722     if ( value != NULL ) {
3723         __kmp_stg_print_str( buffer, name, value );
3724     }
3725 #endif /* KMP_DEBUG */
3726 } // __kmp_stg_print_consistency_check
3727 
3728 
3729 #if USE_ITT_BUILD
3730 // -------------------------------------------------------------------------------------------------
3731 // KMP_ITT_PREPARE_DELAY
3732 // -------------------------------------------------------------------------------------------------
3733 
3734 #if USE_ITT_NOTIFY
3735 
3736 static void
3737 __kmp_stg_parse_itt_prepare_delay( char const * name, char const * value, void * data )
3738 {
3739     // Experimental code: KMP_ITT_PREPARE_DELAY specifies numbert of loop iterations.
3740     int delay = 0;
3741     __kmp_stg_parse_int( name, value, 0, INT_MAX, & delay );
3742     __kmp_itt_prepare_delay = delay;
3743 } // __kmp_str_parse_itt_prepare_delay
3744 
3745 static void
3746 __kmp_stg_print_itt_prepare_delay( kmp_str_buf_t * buffer, char const * name, void * data ) {
3747     __kmp_stg_print_uint64( buffer, name, __kmp_itt_prepare_delay );
3748 
3749 } // __kmp_str_print_itt_prepare_delay
3750 
3751 #endif // USE_ITT_NOTIFY
3752 #endif /* USE_ITT_BUILD */
3753 
3754 // -------------------------------------------------------------------------------------------------
3755 // KMP_MALLOC_POOL_INCR
3756 // -------------------------------------------------------------------------------------------------
3757 
3758 static void
3759 __kmp_stg_parse_malloc_pool_incr( char const * name, char const * value, void * data ) {
3760     __kmp_stg_parse_size(
3761             name,
3762             value,
3763             KMP_MIN_MALLOC_POOL_INCR,
3764             KMP_MAX_MALLOC_POOL_INCR,
3765             NULL,
3766             & __kmp_malloc_pool_incr,
3767             1
3768         );
3769 } // __kmp_stg_parse_malloc_pool_incr
3770 
3771 static void
3772 __kmp_stg_print_malloc_pool_incr( kmp_str_buf_t * buffer, char const * name, void * data ) {
3773        __kmp_stg_print_size( buffer, name, __kmp_malloc_pool_incr );
3774 
3775 } // _kmp_stg_print_malloc_pool_incr
3776 
3777 
3778 #ifdef KMP_DEBUG
3779 
3780 // -------------------------------------------------------------------------------------------------
3781 // KMP_PAR_RANGE
3782 // -------------------------------------------------------------------------------------------------
3783 
3784 static void
3785 __kmp_stg_parse_par_range_env( char const * name, char const * value, void * data ) {
3786         __kmp_stg_parse_par_range(
3787             name,
3788             value,
3789             & __kmp_par_range,
3790             __kmp_par_range_routine,
3791             __kmp_par_range_filename,
3792             & __kmp_par_range_lb,
3793             & __kmp_par_range_ub
3794         );
3795 } // __kmp_stg_parse_par_range_env
3796 
3797 static void
3798 __kmp_stg_print_par_range_env( kmp_str_buf_t * buffer, char const * name, void * data ) {
3799     if (__kmp_par_range != 0) {
3800         __kmp_stg_print_str( buffer, name, par_range_to_print );
3801     }
3802 } // __kmp_stg_print_par_range_env
3803 
3804 // -------------------------------------------------------------------------------------------------
3805 // KMP_YIELD_CYCLE, KMP_YIELD_ON, KMP_YIELD_OFF
3806 // -------------------------------------------------------------------------------------------------
3807 
3808 static void
3809 __kmp_stg_parse_yield_cycle( char const * name, char const * value, void * data ) {
3810     int flag = __kmp_yield_cycle;
3811     __kmp_stg_parse_bool( name, value, & flag );
3812     __kmp_yield_cycle = flag;
3813 } // __kmp_stg_parse_yield_cycle
3814 
3815 static void
3816 __kmp_stg_print_yield_cycle( kmp_str_buf_t * buffer, char const * name, void * data ) {
3817     __kmp_stg_print_bool( buffer, name, __kmp_yield_cycle );
3818 } // __kmp_stg_print_yield_cycle
3819 
3820 static void
3821 __kmp_stg_parse_yield_on( char const * name, char const * value, void * data ) {
3822         __kmp_stg_parse_int( name, value, 2, INT_MAX, & __kmp_yield_on_count );
3823 } // __kmp_stg_parse_yield_on
3824 
3825 static void
3826 __kmp_stg_print_yield_on( kmp_str_buf_t * buffer, char const * name, void * data ) {
3827     __kmp_stg_print_int( buffer, name, __kmp_yield_on_count );
3828 } // __kmp_stg_print_yield_on
3829 
3830 static void
3831 __kmp_stg_parse_yield_off( char const * name, char const * value, void * data ) {
3832         __kmp_stg_parse_int( name, value, 2, INT_MAX, & __kmp_yield_off_count );
3833 } // __kmp_stg_parse_yield_off
3834 
3835 static void
3836 __kmp_stg_print_yield_off( kmp_str_buf_t * buffer, char const * name, void * data ) {
3837     __kmp_stg_print_int( buffer, name, __kmp_yield_off_count );
3838 } // __kmp_stg_print_yield_off
3839 
3840 #endif
3841 
3842 // -------------------------------------------------------------------------------------------------
3843 // KMP_INIT_WAIT, KMP_NEXT_WAIT
3844 // -------------------------------------------------------------------------------------------------
3845 
3846 static void
3847 __kmp_stg_parse_init_wait( char const * name, char const * value, void * data ) {
3848    int wait;
3849    KMP_ASSERT( ( __kmp_init_wait & 1 ) == 0 );
3850    wait = __kmp_init_wait / 2;
3851     __kmp_stg_parse_int( name, value, KMP_MIN_INIT_WAIT, KMP_MAX_INIT_WAIT, & wait );
3852     __kmp_init_wait = wait * 2;
3853     KMP_ASSERT( ( __kmp_init_wait & 1 ) == 0 );
3854     __kmp_yield_init = __kmp_init_wait;
3855 } // __kmp_stg_parse_init_wait
3856 
3857 static void
3858 __kmp_stg_print_init_wait( kmp_str_buf_t * buffer, char const * name, void * data ) {
3859     __kmp_stg_print_int( buffer, name, __kmp_init_wait );
3860 } // __kmp_stg_print_init_wait
3861 
3862 static void
3863 __kmp_stg_parse_next_wait( char const * name, char const * value, void * data ) {
3864     int wait;
3865     KMP_ASSERT( ( __kmp_next_wait & 1 ) == 0 );
3866     wait = __kmp_next_wait / 2;
3867     __kmp_stg_parse_int( name, value, KMP_MIN_NEXT_WAIT, KMP_MAX_NEXT_WAIT, & wait );
3868     __kmp_next_wait = wait * 2;
3869     KMP_ASSERT( ( __kmp_next_wait & 1 ) == 0 );
3870     __kmp_yield_next = __kmp_next_wait;
3871 } // __kmp_stg_parse_next_wait
3872 
3873 static void
3874 __kmp_stg_print_next_wait( kmp_str_buf_t * buffer, char const * name, void * data ) {
3875     __kmp_stg_print_int( buffer, name, __kmp_next_wait );
3876 } //__kmp_stg_print_next_wait
3877 
3878 
3879 // -------------------------------------------------------------------------------------------------
3880 // KMP_GTID_MODE
3881 // -------------------------------------------------------------------------------------------------
3882 
3883 static void
3884 __kmp_stg_parse_gtid_mode( char const * name, char const * value, void * data ) {
3885     //
3886     // Modes:
3887     //   0 -- do not change default
3888     //   1 -- sp search
3889     //   2 -- use "keyed" TLS var, i.e.
3890     //        pthread_getspecific(Linux* OS/OS X*) or TlsGetValue(Windows* OS)
3891     //   3 -- __declspec(thread) TLS var in tdata section
3892     //
3893     int mode = 0;
3894     int max  = 2;
3895     #ifdef KMP_TDATA_GTID
3896         max = 3;
3897     #endif /* KMP_TDATA_GTID */
3898     __kmp_stg_parse_int( name, value, 0, max, & mode );
3899     // TODO; parse_int is not very suitable for this case. In case of overflow it is better to use
3900     // 0 rather that max value.
3901     if ( mode == 0 ) {
3902         __kmp_adjust_gtid_mode = TRUE;
3903     }
3904     else {
3905         __kmp_gtid_mode = mode;
3906         __kmp_adjust_gtid_mode = FALSE;
3907     }; // if
3908 } // __kmp_str_parse_gtid_mode
3909 
3910 static void
3911 __kmp_stg_print_gtid_mode( kmp_str_buf_t * buffer, char const * name, void * data ) {
3912     if ( __kmp_adjust_gtid_mode ) {
3913         __kmp_stg_print_int( buffer, name, 0 );
3914     }
3915     else {
3916         __kmp_stg_print_int( buffer, name, __kmp_gtid_mode );
3917     }
3918 } // __kmp_stg_print_gtid_mode
3919 
3920 // -------------------------------------------------------------------------------------------------
3921 // KMP_NUM_LOCKS_IN_BLOCK
3922 // -------------------------------------------------------------------------------------------------
3923 
3924 static void
3925 __kmp_stg_parse_lock_block( char const * name, char const * value, void * data ) {
3926     __kmp_stg_parse_int( name, value, 0, KMP_INT_MAX, & __kmp_num_locks_in_block );
3927 } // __kmp_str_parse_lock_block
3928 
3929 static void
3930 __kmp_stg_print_lock_block( kmp_str_buf_t * buffer, char const * name, void * data ) {
3931     __kmp_stg_print_int( buffer, name, __kmp_num_locks_in_block );
3932 } // __kmp_stg_print_lock_block
3933 
3934 // -------------------------------------------------------------------------------------------------
3935 // KMP_LOCK_KIND
3936 // -------------------------------------------------------------------------------------------------
3937 
3938 #if KMP_USE_DYNAMIC_LOCK
3939 # define KMP_STORE_LOCK_SEQ(a) (__kmp_user_lock_seq = lockseq_##a)
3940 #else
3941 # define KMP_STORE_LOCK_SEQ(a)
3942 #endif
3943 
3944 static void
3945 __kmp_stg_parse_lock_kind( char const * name, char const * value, void * data ) {
3946     if ( __kmp_init_user_locks ) {
3947         KMP_WARNING( EnvLockWarn, name );
3948         return;
3949     }
3950 
3951     if ( __kmp_str_match( "tas", 2, value )
3952       || __kmp_str_match( "test and set", 2, value )
3953       || __kmp_str_match( "test_and_set", 2, value )
3954       || __kmp_str_match( "test-and-set", 2, value )
3955       || __kmp_str_match( "test andset", 2, value )
3956       || __kmp_str_match( "test_andset", 2, value )
3957       || __kmp_str_match( "test-andset", 2, value )
3958       || __kmp_str_match( "testand set", 2, value )
3959       || __kmp_str_match( "testand_set", 2, value )
3960       || __kmp_str_match( "testand-set", 2, value )
3961       || __kmp_str_match( "testandset", 2, value ) ) {
3962         __kmp_user_lock_kind = lk_tas;
3963         KMP_STORE_LOCK_SEQ(tas);
3964     }
3965 #if KMP_USE_FUTEX
3966     else if ( __kmp_str_match( "futex", 1, value ) ) {
3967         if ( __kmp_futex_determine_capable() ) {
3968             __kmp_user_lock_kind = lk_futex;
3969             KMP_STORE_LOCK_SEQ(futex);
3970         }
3971         else {
3972             KMP_WARNING( FutexNotSupported, name, value );
3973         }
3974     }
3975 #endif
3976     else if ( __kmp_str_match( "ticket", 2, value ) ) {
3977         __kmp_user_lock_kind = lk_ticket;
3978         KMP_STORE_LOCK_SEQ(ticket);
3979     }
3980     else if ( __kmp_str_match( "queuing", 1, value )
3981       || __kmp_str_match( "queue", 1, value ) ) {
3982         __kmp_user_lock_kind = lk_queuing;
3983         KMP_STORE_LOCK_SEQ(queuing);
3984     }
3985     else if ( __kmp_str_match( "drdpa ticket", 1, value )
3986       || __kmp_str_match( "drdpa_ticket", 1, value )
3987       || __kmp_str_match( "drdpa-ticket", 1, value )
3988       || __kmp_str_match( "drdpaticket", 1, value )
3989       || __kmp_str_match( "drdpa", 1, value ) ) {
3990         __kmp_user_lock_kind = lk_drdpa;
3991         KMP_STORE_LOCK_SEQ(drdpa);
3992     }
3993 #if KMP_USE_ADAPTIVE_LOCKS
3994     else if ( __kmp_str_match( "adaptive", 1, value )  ) {
3995         if( __kmp_cpuinfo.rtm ) { // ??? Is cpuinfo available here?
3996             __kmp_user_lock_kind = lk_adaptive;
3997             KMP_STORE_LOCK_SEQ(adaptive);
3998         } else {
3999             KMP_WARNING( AdaptiveNotSupported, name, value );
4000             __kmp_user_lock_kind = lk_queuing;
4001             KMP_STORE_LOCK_SEQ(queuing);
4002         }
4003     }
4004 #endif // KMP_USE_ADAPTIVE_LOCKS
4005 #if KMP_USE_DYNAMIC_LOCK && KMP_USE_TSX
4006     else if ( __kmp_str_match("rtm", 1, value) ) {
4007         if ( __kmp_cpuinfo.rtm ) {
4008             __kmp_user_lock_kind = lk_rtm;
4009             KMP_STORE_LOCK_SEQ(rtm);
4010         } else {
4011             KMP_WARNING( AdaptiveNotSupported, name, value );
4012             __kmp_user_lock_kind = lk_queuing;
4013             KMP_STORE_LOCK_SEQ(queuing);
4014         }
4015     }
4016     else if ( __kmp_str_match("hle", 1, value) ) {
4017         __kmp_user_lock_kind = lk_hle;
4018         KMP_STORE_LOCK_SEQ(hle);
4019     }
4020 #endif
4021     else {
4022         KMP_WARNING( StgInvalidValue, name, value );
4023     }
4024 }
4025 
4026 static void
4027 __kmp_stg_print_lock_kind( kmp_str_buf_t * buffer, char const * name, void * data ) {
4028     const char *value = NULL;
4029 
4030     switch ( __kmp_user_lock_kind ) {
4031         case lk_default:
4032         value = "default";
4033         break;
4034 
4035         case lk_tas:
4036         value = "tas";
4037         break;
4038 
4039 #if KMP_USE_FUTEX
4040         case lk_futex:
4041         value = "futex";
4042         break;
4043 #endif
4044 
4045 #if KMP_USE_DYNAMIC_LOCK && KMP_USE_TSX
4046         case lk_rtm:
4047         value = "rtm";
4048         break;
4049 
4050         case lk_hle:
4051         value = "hle";
4052         break;
4053 #endif
4054 
4055         case lk_ticket:
4056         value = "ticket";
4057         break;
4058 
4059         case lk_queuing:
4060         value = "queuing";
4061         break;
4062 
4063         case lk_drdpa:
4064         value = "drdpa";
4065         break;
4066 #if KMP_USE_ADAPTIVE_LOCKS
4067         case lk_adaptive:
4068         value = "adaptive";
4069         break;
4070 #endif
4071     }
4072 
4073     if ( value != NULL ) {
4074         __kmp_stg_print_str( buffer, name, value );
4075     }
4076 }
4077 
4078 // -------------------------------------------------------------------------------------------------
4079 // KMP_SPIN_BACKOFF_PARAMS
4080 // -------------------------------------------------------------------------------------------------
4081 
4082 // KMP_SPIN_BACKOFF_PARAMS=max_backoff[,min_tick] (max backoff size, min tick for machine pause)
4083 static void
4084 __kmp_stg_parse_spin_backoff_params(const char* name, const char* value, void* data)
4085 {
4086     const char *next = value;
4087 
4088     int total = 0;          // Count elements that were set. It'll be used as an array size
4089     int prev_comma = FALSE; // For correct processing sequential commas
4090     int i;
4091 
4092     kmp_uint32 max_backoff = __kmp_spin_backoff_params.max_backoff;
4093     kmp_uint32 min_tick = __kmp_spin_backoff_params.min_tick;
4094 
4095     // Run only 3 iterations because it is enough to read two values or find a syntax error
4096     for ( i = 0; i < 3 ; i++) {
4097         SKIP_WS( next );
4098 
4099         if ( *next == '\0' ) {
4100             break;
4101         }
4102         // Next character is not an integer or not a comma OR number of values > 2 => end of list
4103         if ( ( ( *next < '0' || *next > '9' ) && *next !=',' ) || total > 2 ) {
4104             KMP_WARNING( EnvSyntaxError, name, value );
4105             return;
4106         }
4107         // The next character is ','
4108         if ( *next == ',' ) {
4109             // ',' is the fisrt character
4110             if ( total == 0 || prev_comma ) {
4111                 total++;
4112             }
4113             prev_comma = TRUE;
4114             next++; //skip ','
4115             SKIP_WS( next );
4116         }
4117         // Next character is a digit
4118         if ( *next >= '0' && *next <= '9' ) {
4119             int num;
4120             const char *buf = next;
4121             char const * msg  = NULL;
4122             prev_comma = FALSE;
4123             SKIP_DIGITS( next );
4124             total++;
4125 
4126             const char *tmp = next;
4127             SKIP_WS( tmp );
4128             if ( ( *next == ' ' || *next == '\t' ) && ( *tmp >= '0' && *tmp <= '9' ) ) {
4129                 KMP_WARNING( EnvSpacesNotAllowed, name, value );
4130                 return;
4131             }
4132 
4133             num = __kmp_str_to_int( buf, *next );
4134             if ( num <= 0 ) { // The number of retries should be > 0
4135                 msg = KMP_I18N_STR( ValueTooSmall );
4136                 num = 1;
4137             } else if ( num > KMP_INT_MAX ) {
4138                 msg = KMP_I18N_STR( ValueTooLarge );
4139                 num = KMP_INT_MAX;
4140             }
4141             if ( msg != NULL ) {
4142                 // Message is not empty. Print warning.
4143                 KMP_WARNING( ParseSizeIntWarn, name, value, msg );
4144                 KMP_INFORM( Using_int_Value, name, num );
4145             }
4146             if( total == 1 ) {
4147                 max_backoff = num;
4148             } else if( total == 2 ) {
4149                 min_tick = num;
4150             }
4151         }
4152     }
4153     KMP_DEBUG_ASSERT( total > 0 );
4154     if( total <= 0 ) {
4155         KMP_WARNING( EnvSyntaxError, name, value );
4156         return;
4157     }
4158     __kmp_spin_backoff_params.max_backoff = max_backoff;
4159     __kmp_spin_backoff_params.min_tick    = min_tick;
4160 }
4161 
4162 static void
4163 __kmp_stg_print_spin_backoff_params(kmp_str_buf_t *buffer, char const* name, void* data)
4164 {
4165     if( __kmp_env_format ) {
4166         KMP_STR_BUF_PRINT_NAME_EX(name);
4167     } else {
4168         __kmp_str_buf_print( buffer, "   %s='", name );
4169     }
4170     __kmp_str_buf_print( buffer, "%d,%d'\n", __kmp_spin_backoff_params.max_backoff,
4171                          __kmp_spin_backoff_params.min_tick );
4172 }
4173 
4174 #if KMP_USE_ADAPTIVE_LOCKS
4175 
4176 // -------------------------------------------------------------------------------------------------
4177 // KMP_ADAPTIVE_LOCK_PROPS, KMP_SPECULATIVE_STATSFILE
4178 // -------------------------------------------------------------------------------------------------
4179 
4180 // Parse out values for the tunable parameters from a string of the form
4181 // KMP_ADAPTIVE_LOCK_PROPS=max_soft_retries[,max_badness]
4182 static void
4183 __kmp_stg_parse_adaptive_lock_props( const char *name, const char *value, void *data )
4184 {
4185     int max_retries = 0;
4186     int max_badness = 0;
4187 
4188     const char *next = value;
4189 
4190     int total = 0;          // Count elements that were set. It'll be used as an array size
4191     int prev_comma = FALSE; // For correct processing sequential commas
4192     int i;
4193 
4194     // Save values in the structure __kmp_speculative_backoff_params
4195     // Run only 3 iterations because it is enough to read two values or find a syntax error
4196     for ( i = 0; i < 3 ; i++) {
4197         SKIP_WS( next );
4198 
4199         if ( *next == '\0' ) {
4200             break;
4201         }
4202         // Next character is not an integer or not a comma OR number of values > 2 => end of list
4203         if ( ( ( *next < '0' || *next > '9' ) && *next !=',' ) || total > 2 ) {
4204             KMP_WARNING( EnvSyntaxError, name, value );
4205             return;
4206         }
4207         // The next character is ','
4208         if ( *next == ',' ) {
4209             // ',' is the fisrt character
4210             if ( total == 0 || prev_comma ) {
4211                 total++;
4212             }
4213             prev_comma = TRUE;
4214             next++; //skip ','
4215             SKIP_WS( next );
4216         }
4217         // Next character is a digit
4218         if ( *next >= '0' && *next <= '9' ) {
4219             int num;
4220             const char *buf = next;
4221             char const * msg  = NULL;
4222             prev_comma = FALSE;
4223             SKIP_DIGITS( next );
4224             total++;
4225 
4226             const char *tmp = next;
4227             SKIP_WS( tmp );
4228             if ( ( *next == ' ' || *next == '\t' ) && ( *tmp >= '0' && *tmp <= '9' ) ) {
4229                 KMP_WARNING( EnvSpacesNotAllowed, name, value );
4230                 return;
4231             }
4232 
4233             num = __kmp_str_to_int( buf, *next );
4234             if ( num < 0 ) { // The number of retries should be >= 0
4235                 msg = KMP_I18N_STR( ValueTooSmall );
4236                 num = 1;
4237             } else if ( num > KMP_INT_MAX ) {
4238                 msg = KMP_I18N_STR( ValueTooLarge );
4239                 num = KMP_INT_MAX;
4240             }
4241             if ( msg != NULL ) {
4242                 // Message is not empty. Print warning.
4243                 KMP_WARNING( ParseSizeIntWarn, name, value, msg );
4244                 KMP_INFORM( Using_int_Value, name, num );
4245             }
4246             if( total == 1 ) {
4247                 max_retries = num;
4248             } else if( total == 2 ) {
4249                 max_badness = num;
4250             }
4251         }
4252     }
4253     KMP_DEBUG_ASSERT( total > 0 );
4254     if( total <= 0 ) {
4255         KMP_WARNING( EnvSyntaxError, name, value );
4256         return;
4257     }
4258     __kmp_adaptive_backoff_params.max_soft_retries = max_retries;
4259     __kmp_adaptive_backoff_params.max_badness = max_badness;
4260 }
4261 
4262 
4263 static void
4264 __kmp_stg_print_adaptive_lock_props(kmp_str_buf_t * buffer, char const * name, void * data )
4265 {
4266     if( __kmp_env_format ) {
4267         KMP_STR_BUF_PRINT_NAME_EX(name);
4268     } else {
4269         __kmp_str_buf_print( buffer, "   %s='", name );
4270     }
4271     __kmp_str_buf_print( buffer, "%d,%d'\n", __kmp_adaptive_backoff_params.max_soft_retries,
4272                          __kmp_adaptive_backoff_params.max_badness );
4273 } // __kmp_stg_print_adaptive_lock_props
4274 
4275 #if KMP_DEBUG_ADAPTIVE_LOCKS
4276 
4277 static void
4278 __kmp_stg_parse_speculative_statsfile( char const * name, char const * value, void * data ) {
4279     __kmp_stg_parse_file( name, value, "", & __kmp_speculative_statsfile );
4280 } // __kmp_stg_parse_speculative_statsfile
4281 
4282 static void
4283 __kmp_stg_print_speculative_statsfile( kmp_str_buf_t * buffer, char const * name, void * data ) {
4284     if ( __kmp_str_match( "-", 0, __kmp_speculative_statsfile )  ) {
4285         __kmp_stg_print_str( buffer, name, "stdout" );
4286     } else {
4287         __kmp_stg_print_str( buffer, name, __kmp_speculative_statsfile );
4288     }
4289 
4290 } // __kmp_stg_print_speculative_statsfile
4291 
4292 #endif // KMP_DEBUG_ADAPTIVE_LOCKS
4293 
4294 #endif // KMP_USE_ADAPTIVE_LOCKS
4295 
4296 // -------------------------------------------------------------------------------------------------
4297 // KMP_HW_SUBSET (was KMP_PLACE_THREADS)
4298 // -------------------------------------------------------------------------------------------------
4299 
4300 static void
4301 __kmp_stg_parse_hw_subset( char const * name, char const * value, void * data ) {
4302     // Value example: 5Cx2Tx15O
4303     // Which means "use 5 cores with offset 15, 2 threads per core"
4304     // AC: extended to sockets level, examples of
4305     //     "use 2 sockets with offset 6, 2 cores with offset 2 per socket, 2 threads per core":
4306     //     2s,6o,2c,2o,2t; 2s,6o,2c,2t,2o; 2s@6,2c@2,2t
4307     //     To not break legacy code core-offset can be last;
4308     //     postfix "o" or prefix @ can be offset designator.
4309     // Note: not all syntax errors are analyzed, some may be skipped.
4310 #define CHECK_DELIM(_x)   (*(_x) == ',' || *(_x) == 'x')
4311     static int parsed = 0;
4312     int         num;
4313     int single_warning = 0;
4314     int flagS = 0, flagC = 0, flagT = 0, flagSO = 0, flagCO = 0;
4315     const char *next = value;
4316     const char *prev;
4317 
4318     if( strcmp(name, "KMP_PLACE_THREADS") == 0 ) {
4319         KMP_INFORM(EnvVarDeprecated,name,"KMP_HW_SUBSET");
4320         if( parsed == 1 ) {
4321             return; // already parsed KMP_HW_SUBSET
4322         }
4323     }
4324     parsed = 1;
4325 
4326     SKIP_WS(next);  // skip white spaces
4327     if (*next == '\0')
4328         return;   // no data provided, retain default values
4329     if( strcmp(name, "KMP_PLACE_THREADS") == 0 ) {
4330         KMP_INFORM(EnvVarDeprecated,name,"KMP_HW_SUBSET");
4331         if( parsed == 1 ) {
4332             return; // already parsed KMP_HW_SUBSET
4333         }
4334     }
4335     parsed = 1;
4336 
4337     SKIP_WS(next);  // skip white spaces
4338     if (*next == '\0')
4339         return;   // no data provided, retain default values
4340     // Get num_sockets first (or whatever specified)
4341     if (*next >= '0' && *next <= '9') {
4342         prev = next;
4343         SKIP_DIGITS(next);
4344         num = __kmp_str_to_int(prev, *next);
4345         SKIP_WS(next);
4346         if (*next == 's' || *next == 'S') {  // e.g. "2s"
4347             __kmp_place_num_sockets = num;
4348             flagS = 1; // got num sockets
4349             next++;
4350             if (*next == '@') { // socket offset, e.g. "2s@4"
4351                 flagSO = 1;
4352                 prev = ++next;  // don't allow spaces for simplicity
4353                 if (!(*next >= '0' && *next <= '9')) {
4354                     KMP_WARNING(AffHWSubsetInvalid, name, value);
4355                     return;
4356                 }
4357                 SKIP_DIGITS(next);
4358                 num = __kmp_str_to_int(prev, *next);
4359                 __kmp_place_socket_offset = num;
4360             }
4361         } else if (*next == 'c' || *next == 'C') {
4362             __kmp_place_num_cores = num;
4363             flagS = flagC = 1; // sockets were not specified - use default
4364             next++;
4365             if (*next == '@') { // core offset, e.g. "2c@6"
4366                 flagCO = 1;
4367                 prev = ++next;  // don't allow spaces for simplicity
4368                 if (!(*next >= '0' && *next <= '9')) {
4369                     KMP_WARNING(AffHWSubsetInvalid, name, value);
4370                     return;
4371                 }
4372                 SKIP_DIGITS(next);
4373                 num = __kmp_str_to_int(prev, *next);
4374                 __kmp_place_core_offset = num;
4375             }
4376         } else if (CHECK_DELIM(next)) {
4377             __kmp_place_num_cores = num; // no letter-designator - num cores
4378             flagS = flagC = 1; // sockets were not specified - use default
4379             next++;
4380         } else if (*next == 't' || *next == 'T') {
4381             __kmp_place_num_threads_per_core = num;
4382             // sockets, cores were not specified - use default
4383             return;   // we ignore offset value in case all cores are used
4384         } else if (*next == '\0') {
4385             __kmp_place_num_cores = num;
4386             return;   // the only value provided - set num cores
4387         } else {
4388             KMP_WARNING(AffHWSubsetInvalid, name, value);
4389             return;
4390         }
4391     } else {
4392         KMP_WARNING(AffHWSubsetInvalid, name, value);
4393         return;
4394     }
4395     KMP_DEBUG_ASSERT(flagS); // num sockets should already be set here
4396     SKIP_WS(next);
4397     if (*next == '\0')
4398         return;   // " n  " - something like this
4399     if (CHECK_DELIM(next)) {
4400         next++;   // skip delimiter
4401         SKIP_WS(next);
4402     }
4403 
4404     // Get second value (could be offset, num_cores, num_threads)
4405     if (*next >= '0' && *next <= '9') {
4406         prev = next;
4407         SKIP_DIGITS(next);
4408         num = __kmp_str_to_int(prev, *next);
4409         SKIP_WS(next);
4410         if (*next == 'c' || *next == 'C') {
4411             KMP_DEBUG_ASSERT(flagC == 0);
4412             __kmp_place_num_cores = num;
4413             flagC = 1;
4414             next++;
4415             if (*next == '@') { // core offset, e.g. "2c@6"
4416                 flagCO = 1;
4417                 prev = ++next;  // don't allow spaces for simplicity
4418                 if (!(*next >= '0' && *next <= '9')) {
4419                     KMP_WARNING(AffHWSubsetInvalid, name, value);
4420                     return;
4421                 }
4422                 SKIP_DIGITS(next);
4423                 num = __kmp_str_to_int(prev, *next);
4424                 __kmp_place_core_offset = num;
4425             }
4426         } else if (*next == 'o' || *next == 'O') { // offset specified
4427             KMP_WARNING(AffHWSubsetDeprecated);
4428             single_warning = 1;
4429             if (flagC) { // whether num_cores already specified (sockets skipped)
4430                 KMP_DEBUG_ASSERT(!flagCO); // either "o" or @, not both
4431                 __kmp_place_core_offset = num;
4432             } else {
4433                 KMP_DEBUG_ASSERT(!flagSO); // either "o" or @, not both
4434                 __kmp_place_socket_offset = num;
4435             }
4436             next++;
4437         } else if (*next == 't' || *next == 'T') {
4438             KMP_DEBUG_ASSERT(flagT == 0);
4439             __kmp_place_num_threads_per_core = num;
4440             flagC = 1; // num_cores could be skipped ?
4441             flagT = 1;
4442             next++; // can have core-offset specified after num threads
4443         } else if (*next == '\0') {
4444             KMP_DEBUG_ASSERT(flagC); // 4x2 means 4 cores 2 threads per core
4445             __kmp_place_num_threads_per_core = num;
4446             return;   // two values provided without letter-designator
4447         } else {
4448             KMP_WARNING(AffHWSubsetInvalid, name, value);
4449             return;
4450         }
4451     } else {
4452         KMP_WARNING(AffHWSubsetInvalid, name, value);
4453         return;
4454     }
4455     SKIP_WS(next);
4456     if (*next == '\0')
4457         return;   // " Ns,Nc  " - something like this
4458     if (CHECK_DELIM(next)) {
4459         next++;   // skip delimiter
4460         SKIP_WS(next);
4461     }
4462 
4463     // Get third value (could be core-offset, num_cores, num_threads)
4464     if (*next >= '0' && *next <= '9') {
4465         prev = next;
4466         SKIP_DIGITS(next);
4467         num = __kmp_str_to_int(prev, *next);
4468         SKIP_WS(next);
4469         if (*next == 't' || *next == 'T') {
4470             KMP_DEBUG_ASSERT(flagT == 0);
4471             __kmp_place_num_threads_per_core = num;
4472             if (flagC == 0)
4473                 return; // num_cores could be skipped (e.g. 2s,4o,2t)
4474             flagT = 1;
4475             next++; // can have core-offset specified later (e.g. 2s,1c,2t,3o)
4476         } else if (*next == 'c' || *next == 'C') {
4477             KMP_DEBUG_ASSERT(flagC == 0);
4478             __kmp_place_num_cores = num;
4479             flagC = 1;
4480             next++;
4481             //KMP_DEBUG_ASSERT(*next != '@'); // socket offset used "o" designator
4482         } else if (*next == 'o' || *next == 'O') {
4483             KMP_WARNING(AffHWSubsetDeprecated);
4484             single_warning = 1;
4485             KMP_DEBUG_ASSERT(flagC);
4486             //KMP_DEBUG_ASSERT(!flagSO); // socket offset couldn't use @ designator
4487             __kmp_place_core_offset = num;
4488             next++;
4489         } else {
4490             KMP_WARNING(AffHWSubsetInvalid, name, value);
4491             return;
4492         }
4493     } else {
4494         KMP_WARNING(AffHWSubsetInvalid, name, value);
4495         return;
4496     }
4497     KMP_DEBUG_ASSERT(flagC);
4498     SKIP_WS(next);
4499     if ( *next == '\0' )
4500             return;
4501     if (CHECK_DELIM(next)) {
4502         next++;   // skip delimiter
4503         SKIP_WS(next);
4504     }
4505 
4506     // Get 4-th value (could be core-offset, num_threads)
4507     if (*next >= '0' && *next <= '9') {
4508         prev = next;
4509         SKIP_DIGITS(next);
4510         num = __kmp_str_to_int(prev, *next);
4511         SKIP_WS(next);
4512         if (*next == 'o' || *next == 'O') {
4513             if (!single_warning) { // warn once
4514                 KMP_WARNING(AffHWSubsetDeprecated);
4515             }
4516             KMP_DEBUG_ASSERT(!flagSO); // socket offset couldn't use @ designator
4517             __kmp_place_core_offset = num;
4518             next++;
4519         } else if (*next == 't' || *next == 'T') {
4520             KMP_DEBUG_ASSERT(flagT == 0);
4521             __kmp_place_num_threads_per_core = num;
4522             flagT = 1;
4523             next++; // can have core-offset specified after num threads
4524         } else {
4525             KMP_WARNING(AffHWSubsetInvalid, name, value);
4526             return;
4527         }
4528     } else {
4529         KMP_WARNING(AffHWSubsetInvalid, name, value);
4530         return;
4531     }
4532     SKIP_WS(next);
4533     if ( *next == '\0' )
4534         return;
4535     if (CHECK_DELIM(next)) {
4536         next++;   // skip delimiter
4537         SKIP_WS(next);
4538     }
4539 
4540     // Get 5-th value (could be core-offset, num_threads)
4541     if (*next >= '0' && *next <= '9') {
4542         prev = next;
4543         SKIP_DIGITS(next);
4544         num = __kmp_str_to_int(prev, *next);
4545         SKIP_WS(next);
4546         if (*next == 'o' || *next == 'O') {
4547             if (!single_warning) { // warn once
4548                 KMP_WARNING(AffHWSubsetDeprecated);
4549             }
4550             KMP_DEBUG_ASSERT(flagT);
4551             KMP_DEBUG_ASSERT(!flagSO); // socket offset couldn't use @ designator
4552             __kmp_place_core_offset = num;
4553         } else if (*next == 't' || *next == 'T') {
4554             KMP_DEBUG_ASSERT(flagT == 0);
4555             __kmp_place_num_threads_per_core = num;
4556         } else {
4557             KMP_WARNING(AffHWSubsetInvalid, name, value);
4558         }
4559     } else {
4560         KMP_WARNING(AffHWSubsetInvalid, name, value);
4561     }
4562     return;
4563 #undef CHECK_DELIM
4564 }
4565 
4566 static void
4567 __kmp_stg_print_hw_subset( kmp_str_buf_t * buffer, char const * name, void * data ) {
4568     if (__kmp_place_num_sockets + __kmp_place_num_cores + __kmp_place_num_threads_per_core) {
4569         int comma = 0;
4570         kmp_str_buf_t buf;
4571         __kmp_str_buf_init(&buf);
4572         if(__kmp_env_format)
4573             KMP_STR_BUF_PRINT_NAME_EX(name);
4574         else
4575             __kmp_str_buf_print(buffer, "   %s='", name);
4576         if (__kmp_place_num_sockets) {
4577             __kmp_str_buf_print(&buf, "%ds", __kmp_place_num_sockets);
4578             if (__kmp_place_socket_offset)
4579                 __kmp_str_buf_print(&buf, "@%d", __kmp_place_socket_offset);
4580             comma = 1;
4581         }
4582         if (__kmp_place_num_cores) {
4583             __kmp_str_buf_print(&buf, "%s%dc", comma?",":"", __kmp_place_num_cores);
4584             if (__kmp_place_core_offset)
4585                 __kmp_str_buf_print(&buf, "@%d", __kmp_place_core_offset);
4586             comma = 1;
4587         }
4588         if (__kmp_place_num_threads_per_core)
4589             __kmp_str_buf_print(&buf, "%s%dt", comma?",":"", __kmp_place_num_threads_per_core);
4590         __kmp_str_buf_print(buffer, "%s'\n", buf.str );
4591         __kmp_str_buf_free(&buf);
4592 /*
4593     } else {
4594         __kmp_str_buf_print( buffer, "   %s: %s \n", name, KMP_I18N_STR( NotDefined ) );
4595 */
4596     }
4597 }
4598 
4599 #if USE_ITT_BUILD
4600 // -------------------------------------------------------------------------------------------------
4601 // KMP_FORKJOIN_FRAMES
4602 // -------------------------------------------------------------------------------------------------
4603 
4604 static void
4605 __kmp_stg_parse_forkjoin_frames( char const * name, char const * value, void * data ) {
4606     __kmp_stg_parse_bool( name, value, & __kmp_forkjoin_frames );
4607 } // __kmp_stg_parse_forkjoin_frames
4608 
4609 static void
4610 __kmp_stg_print_forkjoin_frames( kmp_str_buf_t * buffer, char const * name, void * data ) {
4611     __kmp_stg_print_bool( buffer, name, __kmp_forkjoin_frames );
4612 } // __kmp_stg_print_forkjoin_frames
4613 
4614 // -------------------------------------------------------------------------------------------------
4615 // KMP_FORKJOIN_FRAMES_MODE
4616 // -------------------------------------------------------------------------------------------------
4617 
4618 static void
4619 __kmp_stg_parse_forkjoin_frames_mode( char const * name, char const * value, void * data ) {
4620     __kmp_stg_parse_int( name, value, 0, 3, & __kmp_forkjoin_frames_mode );
4621 } // __kmp_stg_parse_forkjoin_frames
4622 
4623 static void
4624 __kmp_stg_print_forkjoin_frames_mode( kmp_str_buf_t * buffer, char const * name, void * data ) {
4625     __kmp_stg_print_int( buffer, name, __kmp_forkjoin_frames_mode );
4626 } // __kmp_stg_print_forkjoin_frames
4627 #endif /* USE_ITT_BUILD */
4628 
4629 // -------------------------------------------------------------------------------------------------
4630 // OMP_DISPLAY_ENV
4631 // -------------------------------------------------------------------------------------------------
4632 
4633 #if OMP_40_ENABLED
4634 
4635 static void
4636 __kmp_stg_parse_omp_display_env( char const * name, char const * value, void * data )
4637 {
4638     if ( __kmp_str_match( "VERBOSE", 1, value ) )
4639     {
4640         __kmp_display_env_verbose = TRUE;
4641     } else {
4642         __kmp_stg_parse_bool( name, value, & __kmp_display_env );
4643     }
4644 
4645 } // __kmp_stg_parse_omp_display_env
4646 
4647 static void
4648 __kmp_stg_print_omp_display_env( kmp_str_buf_t * buffer, char const * name, void * data )
4649 {
4650     if ( __kmp_display_env_verbose )
4651     {
4652         __kmp_stg_print_str( buffer, name, "VERBOSE" );
4653     } else {
4654         __kmp_stg_print_bool( buffer, name, __kmp_display_env );
4655     }
4656 } // __kmp_stg_print_omp_display_env
4657 
4658 static void
4659 __kmp_stg_parse_omp_cancellation( char const * name, char const * value, void * data ) {
4660     if ( TCR_4(__kmp_init_parallel) ) {
4661         KMP_WARNING( EnvParallelWarn, name );
4662         return;
4663     }   // read value before first parallel only
4664     __kmp_stg_parse_bool( name, value, & __kmp_omp_cancellation );
4665 } // __kmp_stg_parse_omp_cancellation
4666 
4667 static void
4668 __kmp_stg_print_omp_cancellation( kmp_str_buf_t * buffer, char const * name, void * data ) {
4669     __kmp_stg_print_bool( buffer, name, __kmp_omp_cancellation );
4670 } // __kmp_stg_print_omp_cancellation
4671 
4672 #endif
4673 
4674 // -------------------------------------------------------------------------------------------------
4675 // Table.
4676 // -------------------------------------------------------------------------------------------------
4677 
4678 
4679 static kmp_setting_t __kmp_stg_table[] = {
4680 
4681     { "KMP_ALL_THREADS",                   __kmp_stg_parse_all_threads,        __kmp_stg_print_all_threads,        NULL, 0, 0 },
4682     { "KMP_BLOCKTIME",                     __kmp_stg_parse_blocktime,          __kmp_stg_print_blocktime,          NULL, 0, 0 },
4683     { "KMP_DUPLICATE_LIB_OK",              __kmp_stg_parse_duplicate_lib_ok,   __kmp_stg_print_duplicate_lib_ok,   NULL, 0, 0 },
4684     { "KMP_LIBRARY",                       __kmp_stg_parse_wait_policy,        __kmp_stg_print_wait_policy,        NULL, 0, 0 },
4685     { "KMP_MAX_THREADS",                   __kmp_stg_parse_all_threads,        NULL,                               NULL, 0, 0 }, // For backward compatibility
4686 #if KMP_USE_MONITOR
4687     { "KMP_MONITOR_STACKSIZE",             __kmp_stg_parse_monitor_stacksize,  __kmp_stg_print_monitor_stacksize,  NULL, 0, 0 },
4688 #endif
4689     { "KMP_SETTINGS",                      __kmp_stg_parse_settings,           __kmp_stg_print_settings,           NULL, 0, 0 },
4690     { "KMP_STACKOFFSET",                   __kmp_stg_parse_stackoffset,        __kmp_stg_print_stackoffset,        NULL, 0, 0 },
4691     { "KMP_STACKSIZE",                     __kmp_stg_parse_stacksize,          __kmp_stg_print_stacksize,          NULL, 0, 0 },
4692     { "KMP_STACKPAD",                      __kmp_stg_parse_stackpad,           __kmp_stg_print_stackpad,           NULL, 0, 0 },
4693     { "KMP_VERSION",                       __kmp_stg_parse_version,            __kmp_stg_print_version,            NULL, 0, 0 },
4694     { "KMP_WARNINGS",                      __kmp_stg_parse_warnings,           __kmp_stg_print_warnings,           NULL, 0, 0 },
4695 
4696     { "OMP_NESTED",                        __kmp_stg_parse_nested,             __kmp_stg_print_nested,             NULL, 0, 0 },
4697     { "OMP_NUM_THREADS",                   __kmp_stg_parse_num_threads,        __kmp_stg_print_num_threads,        NULL, 0, 0 },
4698     { "OMP_STACKSIZE",                     __kmp_stg_parse_stacksize,          __kmp_stg_print_stacksize,          NULL, 0, 0 },
4699 
4700     { "KMP_TASKING",                       __kmp_stg_parse_tasking,            __kmp_stg_print_tasking,            NULL, 0, 0 },
4701     { "KMP_TASK_STEALING_CONSTRAINT",      __kmp_stg_parse_task_stealing,      __kmp_stg_print_task_stealing,      NULL, 0, 0 },
4702     { "OMP_MAX_ACTIVE_LEVELS",             __kmp_stg_parse_max_active_levels,  __kmp_stg_print_max_active_levels,  NULL, 0, 0 },
4703 #if OMP_40_ENABLED
4704     { "OMP_DEFAULT_DEVICE",                __kmp_stg_parse_default_device,     __kmp_stg_print_default_device,     NULL, 0, 0 },
4705 #endif
4706 #if OMP_45_ENABLED
4707     { "OMP_MAX_TASK_PRIORITY",             __kmp_stg_parse_max_task_priority,  __kmp_stg_print_max_task_priority,  NULL, 0, 0 },
4708 #endif
4709     { "OMP_THREAD_LIMIT",                  __kmp_stg_parse_all_threads,        __kmp_stg_print_all_threads,        NULL, 0, 0 },
4710     { "OMP_WAIT_POLICY",                   __kmp_stg_parse_wait_policy,        __kmp_stg_print_wait_policy,        NULL, 0, 0 },
4711     { "KMP_DISP_NUM_BUFFERS",              __kmp_stg_parse_disp_buffers,       __kmp_stg_print_disp_buffers,       NULL, 0, 0 },
4712 #if KMP_NESTED_HOT_TEAMS
4713     { "KMP_HOT_TEAMS_MAX_LEVEL",           __kmp_stg_parse_hot_teams_level,    __kmp_stg_print_hot_teams_level,    NULL, 0, 0 },
4714     { "KMP_HOT_TEAMS_MODE",                __kmp_stg_parse_hot_teams_mode,     __kmp_stg_print_hot_teams_mode,     NULL, 0, 0 },
4715 #endif // KMP_NESTED_HOT_TEAMS
4716 
4717 #if KMP_HANDLE_SIGNALS
4718     { "KMP_HANDLE_SIGNALS",                __kmp_stg_parse_handle_signals,     __kmp_stg_print_handle_signals,     NULL, 0, 0 },
4719 #endif
4720 
4721 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
4722     { "KMP_INHERIT_FP_CONTROL",            __kmp_stg_parse_inherit_fp_control, __kmp_stg_print_inherit_fp_control, NULL, 0, 0 },
4723 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
4724 
4725 #ifdef KMP_GOMP_COMPAT
4726     { "GOMP_STACKSIZE",                    __kmp_stg_parse_stacksize,          NULL,                               NULL, 0, 0 },
4727 #endif
4728 
4729 #ifdef KMP_DEBUG
4730     { "KMP_A_DEBUG",                       __kmp_stg_parse_a_debug,            __kmp_stg_print_a_debug,            NULL, 0, 0 },
4731     { "KMP_B_DEBUG",                       __kmp_stg_parse_b_debug,            __kmp_stg_print_b_debug,            NULL, 0, 0 },
4732     { "KMP_C_DEBUG",                       __kmp_stg_parse_c_debug,            __kmp_stg_print_c_debug,            NULL, 0, 0 },
4733     { "KMP_D_DEBUG",                       __kmp_stg_parse_d_debug,            __kmp_stg_print_d_debug,            NULL, 0, 0 },
4734     { "KMP_E_DEBUG",                       __kmp_stg_parse_e_debug,            __kmp_stg_print_e_debug,            NULL, 0, 0 },
4735     { "KMP_F_DEBUG",                       __kmp_stg_parse_f_debug,            __kmp_stg_print_f_debug,            NULL, 0, 0 },
4736     { "KMP_DEBUG",                         __kmp_stg_parse_debug,              NULL, /* no print */                NULL, 0, 0 },
4737     { "KMP_DEBUG_BUF",                     __kmp_stg_parse_debug_buf,          __kmp_stg_print_debug_buf,          NULL, 0, 0 },
4738     { "KMP_DEBUG_BUF_ATOMIC",              __kmp_stg_parse_debug_buf_atomic,   __kmp_stg_print_debug_buf_atomic,   NULL, 0, 0 },
4739     { "KMP_DEBUG_BUF_CHARS",               __kmp_stg_parse_debug_buf_chars,    __kmp_stg_print_debug_buf_chars,    NULL, 0, 0 },
4740     { "KMP_DEBUG_BUF_LINES",               __kmp_stg_parse_debug_buf_lines,    __kmp_stg_print_debug_buf_lines,    NULL, 0, 0 },
4741     { "KMP_DIAG",                          __kmp_stg_parse_diag,               __kmp_stg_print_diag,               NULL, 0, 0 },
4742 
4743     { "KMP_PAR_RANGE",                     __kmp_stg_parse_par_range_env,      __kmp_stg_print_par_range_env,      NULL, 0, 0 },
4744     { "KMP_YIELD_CYCLE",                   __kmp_stg_parse_yield_cycle,        __kmp_stg_print_yield_cycle,        NULL, 0, 0 },
4745     { "KMP_YIELD_ON",                      __kmp_stg_parse_yield_on,           __kmp_stg_print_yield_on,           NULL, 0, 0 },
4746     { "KMP_YIELD_OFF",                     __kmp_stg_parse_yield_off,          __kmp_stg_print_yield_off,          NULL, 0, 0 },
4747 #endif // KMP_DEBUG
4748 
4749     { "KMP_ALIGN_ALLOC",                   __kmp_stg_parse_align_alloc,        __kmp_stg_print_align_alloc,        NULL, 0, 0 },
4750 
4751     { "KMP_PLAIN_BARRIER",                 __kmp_stg_parse_barrier_branch_bit, __kmp_stg_print_barrier_branch_bit, NULL, 0, 0 },
4752     { "KMP_PLAIN_BARRIER_PATTERN",         __kmp_stg_parse_barrier_pattern,    __kmp_stg_print_barrier_pattern,    NULL, 0, 0 },
4753     { "KMP_FORKJOIN_BARRIER",              __kmp_stg_parse_barrier_branch_bit, __kmp_stg_print_barrier_branch_bit, NULL, 0, 0 },
4754     { "KMP_FORKJOIN_BARRIER_PATTERN",      __kmp_stg_parse_barrier_pattern,    __kmp_stg_print_barrier_pattern,    NULL, 0, 0 },
4755 #if KMP_FAST_REDUCTION_BARRIER
4756     { "KMP_REDUCTION_BARRIER",             __kmp_stg_parse_barrier_branch_bit, __kmp_stg_print_barrier_branch_bit, NULL, 0, 0 },
4757     { "KMP_REDUCTION_BARRIER_PATTERN",     __kmp_stg_parse_barrier_pattern,    __kmp_stg_print_barrier_pattern,    NULL, 0, 0 },
4758 #endif
4759 
4760     { "KMP_ABORT_DELAY",                   __kmp_stg_parse_abort_delay,        __kmp_stg_print_abort_delay,        NULL, 0, 0 },
4761     { "KMP_CPUINFO_FILE",                  __kmp_stg_parse_cpuinfo_file,       __kmp_stg_print_cpuinfo_file,       NULL, 0, 0 },
4762     { "KMP_FORCE_REDUCTION",               __kmp_stg_parse_force_reduction,    __kmp_stg_print_force_reduction,    NULL, 0, 0 },
4763     { "KMP_DETERMINISTIC_REDUCTION",       __kmp_stg_parse_force_reduction,    __kmp_stg_print_force_reduction,    NULL, 0, 0 },
4764     { "KMP_STORAGE_MAP",                   __kmp_stg_parse_storage_map,        __kmp_stg_print_storage_map,        NULL, 0, 0 },
4765     { "KMP_ALL_THREADPRIVATE",             __kmp_stg_parse_all_threadprivate,  __kmp_stg_print_all_threadprivate,  NULL, 0, 0 },
4766     { "KMP_FOREIGN_THREADS_THREADPRIVATE", __kmp_stg_parse_foreign_threads_threadprivate, __kmp_stg_print_foreign_threads_threadprivate,     NULL, 0, 0 },
4767 
4768 #if KMP_AFFINITY_SUPPORTED
4769     { "KMP_AFFINITY",                      __kmp_stg_parse_affinity,           __kmp_stg_print_affinity,           NULL, 0, 0 },
4770 # ifdef KMP_GOMP_COMPAT
4771     { "GOMP_CPU_AFFINITY",                 __kmp_stg_parse_gomp_cpu_affinity,  NULL, /* no print */                NULL, 0, 0 },
4772 # endif /* KMP_GOMP_COMPAT */
4773 # if OMP_40_ENABLED
4774     { "OMP_PROC_BIND",                     __kmp_stg_parse_proc_bind,          __kmp_stg_print_proc_bind,          NULL, 0, 0 },
4775     { "OMP_PLACES",                        __kmp_stg_parse_places,             __kmp_stg_print_places,             NULL, 0, 0 },
4776 # else
4777     { "OMP_PROC_BIND",                     __kmp_stg_parse_proc_bind,          NULL, /* no print */                NULL, 0, 0 },
4778 # endif /* OMP_40_ENABLED */
4779 
4780     { "KMP_TOPOLOGY_METHOD",               __kmp_stg_parse_topology_method,    __kmp_stg_print_topology_method,    NULL, 0, 0 },
4781 
4782 #else
4783 
4784     //
4785     // KMP_AFFINITY is not supported on OS X*, nor is OMP_PLACES.
4786     // OMP_PROC_BIND and proc-bind-var are supported, however.
4787     //
4788 # if OMP_40_ENABLED
4789     { "OMP_PROC_BIND",                     __kmp_stg_parse_proc_bind,          __kmp_stg_print_proc_bind,          NULL, 0, 0 },
4790 # endif
4791 
4792 #endif // KMP_AFFINITY_SUPPORTED
4793 
4794     { "KMP_INIT_AT_FORK",                  __kmp_stg_parse_init_at_fork,       __kmp_stg_print_init_at_fork,       NULL, 0, 0 },
4795     { "KMP_SCHEDULE",                      __kmp_stg_parse_schedule,           __kmp_stg_print_schedule,           NULL, 0, 0 },
4796     { "OMP_SCHEDULE",                      __kmp_stg_parse_omp_schedule,       __kmp_stg_print_omp_schedule,       NULL, 0, 0 },
4797     { "KMP_ATOMIC_MODE",                   __kmp_stg_parse_atomic_mode,        __kmp_stg_print_atomic_mode,        NULL, 0, 0 },
4798     { "KMP_CONSISTENCY_CHECK",             __kmp_stg_parse_consistency_check,  __kmp_stg_print_consistency_check,  NULL, 0, 0 },
4799 
4800 #if USE_ITT_BUILD && USE_ITT_NOTIFY
4801     { "KMP_ITT_PREPARE_DELAY",             __kmp_stg_parse_itt_prepare_delay,  __kmp_stg_print_itt_prepare_delay,  NULL, 0, 0 },
4802 #endif /* USE_ITT_BUILD && USE_ITT_NOTIFY */
4803     { "KMP_MALLOC_POOL_INCR",              __kmp_stg_parse_malloc_pool_incr,   __kmp_stg_print_malloc_pool_incr,   NULL, 0, 0 },
4804     { "KMP_INIT_WAIT",                     __kmp_stg_parse_init_wait,          __kmp_stg_print_init_wait,          NULL, 0, 0 },
4805     { "KMP_NEXT_WAIT",                     __kmp_stg_parse_next_wait,          __kmp_stg_print_next_wait,          NULL, 0, 0 },
4806     { "KMP_GTID_MODE",                     __kmp_stg_parse_gtid_mode,          __kmp_stg_print_gtid_mode,          NULL, 0, 0 },
4807     { "OMP_DYNAMIC",                       __kmp_stg_parse_omp_dynamic,        __kmp_stg_print_omp_dynamic,        NULL, 0, 0 },
4808     { "KMP_DYNAMIC_MODE",                  __kmp_stg_parse_kmp_dynamic_mode,   __kmp_stg_print_kmp_dynamic_mode,   NULL, 0, 0 },
4809 
4810 #ifdef USE_LOAD_BALANCE
4811     { "KMP_LOAD_BALANCE_INTERVAL",         __kmp_stg_parse_ld_balance_interval,__kmp_stg_print_ld_balance_interval,NULL, 0, 0 },
4812 #endif
4813 
4814     { "KMP_NUM_LOCKS_IN_BLOCK",            __kmp_stg_parse_lock_block,         __kmp_stg_print_lock_block,         NULL, 0, 0 },
4815     { "KMP_LOCK_KIND",                     __kmp_stg_parse_lock_kind,          __kmp_stg_print_lock_kind,          NULL, 0, 0 },
4816     { "KMP_SPIN_BACKOFF_PARAMS",           __kmp_stg_parse_spin_backoff_params, __kmp_stg_print_spin_backoff_params, NULL, 0, 0 },
4817 #if KMP_USE_ADAPTIVE_LOCKS
4818     { "KMP_ADAPTIVE_LOCK_PROPS",           __kmp_stg_parse_adaptive_lock_props,__kmp_stg_print_adaptive_lock_props,  NULL, 0, 0 },
4819 #if KMP_DEBUG_ADAPTIVE_LOCKS
4820     { "KMP_SPECULATIVE_STATSFILE",         __kmp_stg_parse_speculative_statsfile,__kmp_stg_print_speculative_statsfile,  NULL, 0, 0 },
4821 #endif
4822 #endif // KMP_USE_ADAPTIVE_LOCKS
4823     { "KMP_PLACE_THREADS",                 __kmp_stg_parse_hw_subset,      __kmp_stg_print_hw_subset,      NULL, 0, 0 },
4824     { "KMP_HW_SUBSET",                     __kmp_stg_parse_hw_subset,      __kmp_stg_print_hw_subset,      NULL, 0, 0 },
4825 #if USE_ITT_BUILD
4826     { "KMP_FORKJOIN_FRAMES",               __kmp_stg_parse_forkjoin_frames,    __kmp_stg_print_forkjoin_frames,    NULL, 0, 0 },
4827     { "KMP_FORKJOIN_FRAMES_MODE",          __kmp_stg_parse_forkjoin_frames_mode,__kmp_stg_print_forkjoin_frames_mode,  NULL, 0, 0 },
4828 #endif
4829 
4830 # if OMP_40_ENABLED
4831     { "OMP_DISPLAY_ENV",                   __kmp_stg_parse_omp_display_env,    __kmp_stg_print_omp_display_env,    NULL, 0, 0 },
4832     { "OMP_CANCELLATION",                  __kmp_stg_parse_omp_cancellation,   __kmp_stg_print_omp_cancellation,   NULL, 0, 0 },
4833 #endif
4834     { "",                                  NULL,                               NULL,                               NULL, 0, 0 }
4835 }; // settings
4836 
4837 static int const __kmp_stg_count = sizeof( __kmp_stg_table ) / sizeof( kmp_setting_t );
4838 
4839 static inline
4840 kmp_setting_t *
4841 __kmp_stg_find( char const * name ) {
4842 
4843     int i;
4844     if ( name != NULL ) {
4845         for ( i = 0; i < __kmp_stg_count; ++ i ) {
4846             if ( strcmp( __kmp_stg_table[ i ].name, name ) == 0 ) {
4847                 return & __kmp_stg_table[ i ];
4848             }; // if
4849         }; // for
4850     }; // if
4851     return NULL;
4852 
4853 } // __kmp_stg_find
4854 
4855 
4856 static int
4857 __kmp_stg_cmp( void const * _a, void const * _b ) {
4858     kmp_setting_t * a = (kmp_setting_t *) _a;
4859     kmp_setting_t * b = (kmp_setting_t *) _b;
4860 
4861     //
4862     // Process KMP_AFFINITY last.
4863     // It needs to come after OMP_PLACES and GOMP_CPU_AFFINITY.
4864     //
4865     if ( strcmp( a->name, "KMP_AFFINITY" ) == 0 ) {
4866         if ( strcmp( b->name, "KMP_AFFINITY" ) == 0 ) {
4867             return 0;
4868         }
4869         return 1;
4870     }
4871     else if ( strcmp( b->name, "KMP_AFFINITY" ) == 0 ) {
4872         return -1;
4873     }
4874     return strcmp( a->name, b->name );
4875 } // __kmp_stg_cmp
4876 
4877 
4878 static void
4879 __kmp_stg_init( void
4880 ) {
4881 
4882     static int initialized = 0;
4883 
4884     if ( ! initialized ) {
4885 
4886         // Sort table.
4887         qsort( __kmp_stg_table, __kmp_stg_count - 1, sizeof( kmp_setting_t ), __kmp_stg_cmp );
4888 
4889         { // Initialize *_STACKSIZE data.
4890 
4891             kmp_setting_t * kmp_stacksize  = __kmp_stg_find( "KMP_STACKSIZE"  );      // 1st priority.
4892 #ifdef KMP_GOMP_COMPAT
4893             kmp_setting_t * gomp_stacksize = __kmp_stg_find( "GOMP_STACKSIZE" );      // 2nd priority.
4894 #endif
4895             kmp_setting_t * omp_stacksize  = __kmp_stg_find( "OMP_STACKSIZE"  );      // 3rd priority.
4896 
4897             // !!! volatile keyword is Intel (R) C Compiler bug CQ49908 workaround.
4898             // !!! Compiler does not understand rivals is used and optimizes out assignments
4899             // !!!     rivals[ i ++ ] = ...;
4900             static kmp_setting_t * volatile rivals[ 4 ];
4901             static kmp_stg_ss_data_t kmp_data  = {    1, (kmp_setting_t **)rivals };
4902 #ifdef KMP_GOMP_COMPAT
4903             static kmp_stg_ss_data_t gomp_data = { 1024, (kmp_setting_t **)rivals };
4904 #endif
4905             static kmp_stg_ss_data_t omp_data  = { 1024, (kmp_setting_t **)rivals };
4906             int i = 0;
4907 
4908             rivals[ i ++ ] = kmp_stacksize;
4909 #ifdef KMP_GOMP_COMPAT
4910             if ( gomp_stacksize != NULL ) {
4911                 rivals[ i ++ ] = gomp_stacksize;
4912             }; // if
4913 #endif
4914             rivals[ i ++ ] = omp_stacksize;
4915             rivals[ i ++ ] = NULL;
4916 
4917             kmp_stacksize->data = & kmp_data;
4918 #ifdef KMP_GOMP_COMPAT
4919             if ( gomp_stacksize != NULL ) {
4920                 gomp_stacksize->data = & gomp_data;
4921             }; // if
4922 #endif
4923             omp_stacksize->data = & omp_data;
4924 
4925         }
4926 
4927         { // Initialize KMP_LIBRARY and OMP_WAIT_POLICY data.
4928 
4929             kmp_setting_t * kmp_library     = __kmp_stg_find( "KMP_LIBRARY" );        // 1st priority.
4930             kmp_setting_t * omp_wait_policy = __kmp_stg_find( "OMP_WAIT_POLICY" );    // 2nd priority.
4931 
4932             // !!! volatile keyword is Intel (R) C Compiler bug CQ49908 workaround.
4933             static kmp_setting_t * volatile rivals[ 3 ];
4934             static kmp_stg_wp_data_t kmp_data  = { 0, (kmp_setting_t **)rivals };
4935             static kmp_stg_wp_data_t omp_data  = { 1, (kmp_setting_t **)rivals };
4936             int i = 0;
4937 
4938             rivals[ i ++ ] = kmp_library;
4939             if ( omp_wait_policy != NULL ) {
4940                 rivals[ i ++ ] = omp_wait_policy;
4941             }; // if
4942             rivals[ i ++ ] = NULL;
4943 
4944             kmp_library->data  = & kmp_data;
4945             if ( omp_wait_policy != NULL ) {
4946                 omp_wait_policy->data = & omp_data;
4947             }; // if
4948 
4949         }
4950 
4951         { // Initialize KMP_ALL_THREADS, KMP_MAX_THREADS, and OMP_THREAD_LIMIT data.
4952 
4953             kmp_setting_t * kmp_all_threads  = __kmp_stg_find( "KMP_ALL_THREADS"  );  // 1st priority.
4954             kmp_setting_t * kmp_max_threads  = __kmp_stg_find( "KMP_MAX_THREADS"  );  // 2nd priority.
4955             kmp_setting_t * omp_thread_limit = __kmp_stg_find( "OMP_THREAD_LIMIT" );  // 3rd priority.
4956 
4957             // !!! volatile keyword is Intel (R) C Compiler bug CQ49908 workaround.
4958             static kmp_setting_t * volatile rivals[ 4 ];
4959             int i = 0;
4960 
4961             rivals[ i ++ ] = kmp_all_threads;
4962             rivals[ i ++ ] = kmp_max_threads;
4963             if ( omp_thread_limit != NULL ) {
4964                 rivals[ i ++ ] = omp_thread_limit;
4965             }; // if
4966             rivals[ i ++ ] = NULL;
4967 
4968             kmp_all_threads->data = (void*)& rivals;
4969             kmp_max_threads->data = (void*)& rivals;
4970             if ( omp_thread_limit != NULL ) {
4971                 omp_thread_limit->data = (void*)& rivals;
4972             }; // if
4973 
4974         }
4975 
4976 #if KMP_AFFINITY_SUPPORTED
4977         { // Initialize KMP_AFFINITY, GOMP_CPU_AFFINITY, and OMP_PROC_BIND data.
4978 
4979             kmp_setting_t * kmp_affinity = __kmp_stg_find( "KMP_AFFINITY"  );  // 1st priority.
4980             KMP_DEBUG_ASSERT( kmp_affinity != NULL );
4981 
4982 # ifdef KMP_GOMP_COMPAT
4983             kmp_setting_t * gomp_cpu_affinity = __kmp_stg_find( "GOMP_CPU_AFFINITY"  );  // 2nd priority.
4984             KMP_DEBUG_ASSERT( gomp_cpu_affinity != NULL );
4985 # endif
4986 
4987             kmp_setting_t * omp_proc_bind = __kmp_stg_find( "OMP_PROC_BIND" );  // 3rd priority.
4988             KMP_DEBUG_ASSERT( omp_proc_bind != NULL );
4989 
4990             // !!! volatile keyword is Intel (R) C Compiler bug CQ49908 workaround.
4991             static kmp_setting_t * volatile rivals[ 4 ];
4992             int i = 0;
4993 
4994             rivals[ i ++ ] = kmp_affinity;
4995 
4996 # ifdef KMP_GOMP_COMPAT
4997             rivals[ i ++ ] = gomp_cpu_affinity;
4998             gomp_cpu_affinity->data = (void*)& rivals;
4999 # endif
5000 
5001             rivals[ i ++ ] = omp_proc_bind;
5002             omp_proc_bind->data = (void*)& rivals;
5003             rivals[ i ++ ] = NULL;
5004 
5005 # if OMP_40_ENABLED
5006             static kmp_setting_t * volatile places_rivals[ 4 ];
5007             i = 0;
5008 
5009             kmp_setting_t * omp_places = __kmp_stg_find( "OMP_PLACES" );  // 3rd priority.
5010             KMP_DEBUG_ASSERT( omp_places != NULL );
5011 
5012             places_rivals[ i ++ ] = kmp_affinity;
5013 #  ifdef KMP_GOMP_COMPAT
5014             places_rivals[ i ++ ] = gomp_cpu_affinity;
5015 #  endif
5016             places_rivals[ i ++ ] = omp_places;
5017             omp_places->data = (void*)& places_rivals;
5018             places_rivals[ i ++ ] = NULL;
5019 # endif
5020         }
5021 #else
5022     // KMP_AFFINITY not supported, so OMP_PROC_BIND has no rivals.
5023     // OMP_PLACES not supported yet.
5024 #endif // KMP_AFFINITY_SUPPORTED
5025 
5026         { // Initialize KMP_DETERMINISTIC_REDUCTION and KMP_FORCE_REDUCTION data.
5027 
5028             kmp_setting_t * kmp_force_red  = __kmp_stg_find( "KMP_FORCE_REDUCTION" );         // 1st priority.
5029             kmp_setting_t * kmp_determ_red = __kmp_stg_find( "KMP_DETERMINISTIC_REDUCTION" ); // 2nd priority.
5030 
5031             // !!! volatile keyword is Intel (R) C Compiler bug CQ49908 workaround.
5032             static kmp_setting_t * volatile rivals[ 3 ];
5033             static kmp_stg_fr_data_t force_data   = { 1, (kmp_setting_t **)rivals };
5034             static kmp_stg_fr_data_t determ_data  = { 0, (kmp_setting_t **)rivals };
5035             int i = 0;
5036 
5037             rivals[ i ++ ] = kmp_force_red;
5038             if ( kmp_determ_red != NULL ) {
5039                 rivals[ i ++ ] = kmp_determ_red;
5040             }; // if
5041             rivals[ i ++ ] = NULL;
5042 
5043             kmp_force_red->data = & force_data;
5044             if ( kmp_determ_red != NULL ) {
5045                 kmp_determ_red->data  = & determ_data;
5046             }; // if
5047         }
5048 
5049         initialized = 1;
5050 
5051     }; // if
5052 
5053     // Reset flags.
5054     int i;
5055     for ( i = 0; i < __kmp_stg_count; ++ i ) {
5056         __kmp_stg_table[ i ].set = 0;
5057     }; // for
5058 
5059 } // __kmp_stg_init
5060 
5061 
5062 static void
5063 __kmp_stg_parse(
5064     char const * name,
5065     char const * value
5066 ) {
5067 
5068     // On Windows* OS there are some nameless variables like "C:=C:\" (yeah, really nameless, they are
5069     // presented in environment block as "=C:=C\\\x00=D:=D:\\\x00...", so let us skip them.
5070     if ( name[ 0 ] == 0 ) {
5071         return;
5072     }; // if
5073 
5074     if ( value != NULL ) {
5075         kmp_setting_t * setting = __kmp_stg_find( name );
5076         if ( setting != NULL ) {
5077             setting->parse( name, value, setting->data );
5078             setting->defined = 1;
5079         }; // if
5080     }; // if
5081 
5082 } // __kmp_stg_parse
5083 
5084 
5085 static int
5086 __kmp_stg_check_rivals(          // 0 -- Ok, 1 -- errors found.
5087     char const *       name,     // Name of variable.
5088     char const *       value,    // Value of the variable.
5089     kmp_setting_t * *  rivals    // List of rival settings (the list must include current one).
5090 ) {
5091 
5092     if ( rivals == NULL ) {
5093         return 0;
5094     }
5095 
5096     // Loop thru higher priority settings (listed before current).
5097     int i = 0;
5098     for ( ; strcmp( rivals[ i ]->name, name ) != 0; i++ ) {
5099         KMP_DEBUG_ASSERT( rivals[ i ] != NULL );
5100 
5101 #if KMP_AFFINITY_SUPPORTED
5102         if ( rivals[ i ] == __kmp_affinity_notype ) {
5103             //
5104             // If KMP_AFFINITY is specified without a type name,
5105             // it does not rival OMP_PROC_BIND or GOMP_CPU_AFFINITY.
5106             //
5107             continue;
5108         }
5109 #endif
5110 
5111         if ( rivals[ i ]->set ) {
5112             KMP_WARNING( StgIgnored, name, rivals[ i ]->name );
5113             return 1;
5114         }; // if
5115     }; // while
5116 
5117     ++ i; // Skip current setting.
5118     return 0;
5119 
5120 }; // __kmp_stg_check_rivals
5121 
5122 
5123 static int
5124 __kmp_env_toPrint( char const * name, int flag ) {
5125     int rc = 0;
5126     kmp_setting_t * setting = __kmp_stg_find( name );
5127     if ( setting != NULL ) {
5128         rc = setting->defined;
5129         if ( flag >= 0 ) {
5130             setting->defined = flag;
5131         }; // if
5132     }; // if
5133     return rc;
5134 }
5135 
5136 
5137 static void
5138 __kmp_aux_env_initialize( kmp_env_blk_t* block ) {
5139 
5140     char const * value;
5141 
5142     /* OMP_NUM_THREADS */
5143     value = __kmp_env_blk_var( block, "OMP_NUM_THREADS" );
5144     if ( value ) {
5145         ompc_set_num_threads( __kmp_dflt_team_nth );
5146     }
5147 
5148     /* KMP_BLOCKTIME */
5149     value = __kmp_env_blk_var( block, "KMP_BLOCKTIME" );
5150     if ( value ) {
5151         kmpc_set_blocktime( __kmp_dflt_blocktime );
5152     }
5153 
5154     /* OMP_NESTED */
5155     value = __kmp_env_blk_var( block, "OMP_NESTED" );
5156     if ( value ) {
5157         ompc_set_nested( __kmp_dflt_nested );
5158     }
5159 
5160     /* OMP_DYNAMIC */
5161     value = __kmp_env_blk_var( block, "OMP_DYNAMIC" );
5162     if ( value ) {
5163         ompc_set_dynamic( __kmp_global.g.g_dynamic );
5164     }
5165 
5166 }
5167 
5168 void
5169 __kmp_env_initialize( char const * string ) {
5170 
5171     kmp_env_blk_t block;
5172     int           i;
5173 
5174     __kmp_stg_init();
5175 
5176     // Hack!!!
5177     if ( string == NULL ) {
5178         // __kmp_max_nth = __kmp_sys_max_nth;
5179         __kmp_threads_capacity = __kmp_initial_threads_capacity( __kmp_dflt_team_nth_ub );
5180     }; // if
5181     __kmp_env_blk_init( & block, string );
5182 
5183     //
5184     // update the set flag on all entries that have an env var
5185     //
5186     for ( i = 0; i < block.count; ++ i ) {
5187         if (( block.vars[ i ].name == NULL )
5188           || ( *block.vars[ i ].name == '\0')) {
5189             continue;
5190         }
5191         if ( block.vars[ i ].value == NULL ) {
5192             continue;
5193         }
5194         kmp_setting_t * setting = __kmp_stg_find( block.vars[ i ].name );
5195         if ( setting != NULL ) {
5196             setting->set = 1;
5197         }
5198     }; // for i
5199 
5200     // We need to know if blocktime was set when processing OMP_WAIT_POLICY
5201     blocktime_str = __kmp_env_blk_var( & block, "KMP_BLOCKTIME" );
5202 
5203     // Special case. If we parse environment, not a string, process KMP_WARNINGS first.
5204     if ( string == NULL ) {
5205         char const * name  = "KMP_WARNINGS";
5206         char const * value = __kmp_env_blk_var( & block, name );
5207         __kmp_stg_parse( name, value );
5208     }; // if
5209 
5210 #if KMP_AFFINITY_SUPPORTED
5211     //
5212     // Special case. KMP_AFFINITY is not a rival to other affinity env vars
5213     // if no affinity type is specified.  We want to allow
5214     // KMP_AFFINITY=[no],verbose/[no]warnings/etc.  to be enabled when
5215     // specifying the affinity type via GOMP_CPU_AFFINITY or the OMP 4.0
5216     // affinity mechanism.
5217     //
5218     __kmp_affinity_notype = NULL;
5219     char const *aff_str = __kmp_env_blk_var( & block, "KMP_AFFINITY" );
5220     if ( aff_str != NULL ) {
5221         //
5222         // Check if the KMP_AFFINITY type is specified in the string.
5223         // We just search the string for "compact", "scatter", etc.
5224         // without really parsing the string.  The syntax of the
5225         // KMP_AFFINITY env var is such that none of the affinity
5226         // type names can appear anywhere other that the type
5227         // specifier, even as substrings.
5228         //
5229         // I can't find a case-insensitive version of strstr on Windows* OS.
5230         // Use the case-sensitive version for now.
5231         //
5232 
5233 # if KMP_OS_WINDOWS
5234 #  define FIND strstr
5235 # else
5236 #  define FIND strcasestr
5237 # endif
5238 
5239         if ( ( FIND( aff_str, "none" ) == NULL )
5240           && ( FIND( aff_str, "physical" ) == NULL )
5241           && ( FIND( aff_str, "logical" ) == NULL )
5242           && ( FIND( aff_str, "compact" ) == NULL )
5243           && ( FIND( aff_str, "scatter" ) == NULL )
5244           && ( FIND( aff_str, "explicit" ) == NULL )
5245           && ( FIND( aff_str, "balanced" ) == NULL )
5246           && ( FIND( aff_str, "disabled" ) == NULL ) ) {
5247             __kmp_affinity_notype = __kmp_stg_find( "KMP_AFFINITY"  );
5248         }
5249         else {
5250             //
5251             // A new affinity type is specified.
5252             // Reset the affinity flags to their default values,
5253             // in case this is called from kmp_set_defaults().
5254             //
5255             __kmp_affinity_type = affinity_default;
5256             __kmp_affinity_gran = affinity_gran_default;
5257             __kmp_affinity_top_method = affinity_top_method_default;
5258             __kmp_affinity_respect_mask = affinity_respect_mask_default;
5259         }
5260 # undef FIND
5261 
5262 #if OMP_40_ENABLED
5263         //
5264         // Also reset the affinity flags if OMP_PROC_BIND is specified.
5265         //
5266         aff_str = __kmp_env_blk_var( & block, "OMP_PROC_BIND" );
5267         if ( aff_str != NULL ) {
5268             __kmp_affinity_type = affinity_default;
5269             __kmp_affinity_gran = affinity_gran_default;
5270             __kmp_affinity_top_method = affinity_top_method_default;
5271             __kmp_affinity_respect_mask = affinity_respect_mask_default;
5272         }
5273 #endif /* OMP_40_ENABLED */
5274     }
5275 
5276 #endif /* KMP_AFFINITY_SUPPORTED */
5277 
5278 #if OMP_40_ENABLED
5279     //
5280     // Set up the nested proc bind type vector.
5281     //
5282     if ( __kmp_nested_proc_bind.bind_types == NULL ) {
5283         __kmp_nested_proc_bind.bind_types = (kmp_proc_bind_t *)
5284           KMP_INTERNAL_MALLOC( sizeof(kmp_proc_bind_t) );
5285         if ( __kmp_nested_proc_bind.bind_types == NULL ) {
5286             KMP_FATAL( MemoryAllocFailed );
5287         }
5288         __kmp_nested_proc_bind.size = 1;
5289         __kmp_nested_proc_bind.used = 1;
5290 # if KMP_AFFINITY_SUPPORTED
5291         __kmp_nested_proc_bind.bind_types[0] = proc_bind_default;
5292 # else
5293         // default proc bind is false if affinity not supported
5294         __kmp_nested_proc_bind.bind_types[0] = proc_bind_false;
5295 # endif
5296 
5297     }
5298 #endif /* OMP_40_ENABLED */
5299 
5300     //
5301     // Now process all of the settings.
5302     //
5303     for ( i = 0; i < block.count; ++ i ) {
5304         __kmp_stg_parse( block.vars[ i ].name, block.vars[ i ].value );
5305     }; // for i
5306 
5307     //
5308     // If user locks have been allocated yet, don't reset the lock vptr table.
5309     //
5310     if ( ! __kmp_init_user_locks ) {
5311         if ( __kmp_user_lock_kind == lk_default ) {
5312             __kmp_user_lock_kind = lk_queuing;
5313         }
5314 #if KMP_USE_DYNAMIC_LOCK
5315         __kmp_init_dynamic_user_locks();
5316 #else
5317         __kmp_set_user_lock_vptrs( __kmp_user_lock_kind );
5318 #endif
5319     }
5320     else {
5321         KMP_DEBUG_ASSERT( string != NULL); // kmp_set_defaults() was called
5322         KMP_DEBUG_ASSERT( __kmp_user_lock_kind != lk_default );
5323         // Binds lock functions again to follow the transition between different
5324         // KMP_CONSISTENCY_CHECK values. Calling this again is harmless as long
5325         // as we do not allow lock kind changes after making a call to any
5326         // user lock functions (true).
5327 #if KMP_USE_DYNAMIC_LOCK
5328         __kmp_init_dynamic_user_locks();
5329 #else
5330         __kmp_set_user_lock_vptrs( __kmp_user_lock_kind );
5331 #endif
5332     }
5333 
5334 #if KMP_AFFINITY_SUPPORTED
5335 
5336     if ( ! TCR_4(__kmp_init_middle) ) {
5337         //
5338         // Determine if the machine/OS is actually capable of supporting
5339         // affinity.
5340         //
5341         const char *var = "KMP_AFFINITY";
5342         KMPAffinity::pick_api();
5343         if ( __kmp_affinity_type == affinity_disabled ) {
5344             KMP_AFFINITY_DISABLE();
5345         }
5346         else if ( ! KMP_AFFINITY_CAPABLE() ) {
5347             __kmp_affinity_dispatch->determine_capable(var);
5348             if ( ! KMP_AFFINITY_CAPABLE() ) {
5349                 if ( __kmp_affinity_verbose || ( __kmp_affinity_warnings
5350                   && ( __kmp_affinity_type != affinity_default )
5351                   && ( __kmp_affinity_type != affinity_none )
5352                   && ( __kmp_affinity_type != affinity_disabled ) ) ) {
5353                     KMP_WARNING( AffNotSupported, var );
5354                 }
5355                 __kmp_affinity_type = affinity_disabled;
5356                 __kmp_affinity_respect_mask = 0;
5357                 __kmp_affinity_gran = affinity_gran_fine;
5358             }
5359         }
5360 
5361 # if OMP_40_ENABLED
5362         if ( __kmp_affinity_type == affinity_disabled )  {
5363             __kmp_nested_proc_bind.bind_types[0] = proc_bind_false;
5364         }
5365         else if ( __kmp_nested_proc_bind.bind_types[0] == proc_bind_true ) {
5366             //
5367             // OMP_PROC_BIND=true maps to OMP_PROC_BIND=spread.
5368             //
5369             __kmp_nested_proc_bind.bind_types[0] = proc_bind_spread;
5370         }
5371 # endif /* OMP_40_ENABLED */
5372 
5373         if ( KMP_AFFINITY_CAPABLE() ) {
5374 
5375 # if KMP_GROUP_AFFINITY
5376 
5377             //
5378             // Handle the Win 64 group affinity stuff if there are multiple
5379             // processor groups, or if the user requested it, and OMP 4.0
5380             // affinity is not in effect.
5381             //
5382             if ( ( ( __kmp_num_proc_groups > 1 )
5383               && ( __kmp_affinity_type == affinity_default )
5384 #  if OMP_40_ENABLED
5385               && ( __kmp_nested_proc_bind.bind_types[0] == proc_bind_default ) )
5386 #  endif
5387               || ( __kmp_affinity_top_method == affinity_top_method_group ) ) {
5388                 if ( __kmp_affinity_respect_mask == affinity_respect_mask_default ) {
5389                     __kmp_affinity_respect_mask = FALSE;
5390                 }
5391                 if ( __kmp_affinity_type == affinity_default ) {
5392                     __kmp_affinity_type = affinity_compact;
5393 #  if OMP_40_ENABLED
5394                     __kmp_nested_proc_bind.bind_types[0] = proc_bind_intel;
5395 #  endif
5396                 }
5397                 if ( __kmp_affinity_top_method == affinity_top_method_default ) {
5398                     if ( __kmp_affinity_gran == affinity_gran_default ) {
5399                         __kmp_affinity_top_method = affinity_top_method_group;
5400                         __kmp_affinity_gran = affinity_gran_group;
5401                     }
5402                     else if ( __kmp_affinity_gran == affinity_gran_group ) {
5403                         __kmp_affinity_top_method = affinity_top_method_group;
5404                     }
5405                     else {
5406                         __kmp_affinity_top_method = affinity_top_method_all;
5407                     }
5408                 }
5409                 else if ( __kmp_affinity_top_method == affinity_top_method_group ) {
5410                     if ( __kmp_affinity_gran == affinity_gran_default ) {
5411                         __kmp_affinity_gran = affinity_gran_group;
5412                     }
5413                     else if ( ( __kmp_affinity_gran != affinity_gran_group )
5414                       && ( __kmp_affinity_gran != affinity_gran_fine )
5415                       && ( __kmp_affinity_gran != affinity_gran_thread ) ) {
5416                         const char *str = NULL;
5417                         switch ( __kmp_affinity_gran ) {
5418                             case affinity_gran_core: str = "core"; break;
5419                             case affinity_gran_package: str = "package"; break;
5420                             case affinity_gran_node: str = "node"; break;
5421                             default: KMP_DEBUG_ASSERT( 0 );
5422                         }
5423                         KMP_WARNING( AffGranTopGroup, var, str );
5424                         __kmp_affinity_gran = affinity_gran_fine;
5425                     }
5426                 }
5427                 else {
5428                     if ( __kmp_affinity_gran == affinity_gran_default ) {
5429                         __kmp_affinity_gran = affinity_gran_core;
5430                     }
5431                     else if ( __kmp_affinity_gran == affinity_gran_group ) {
5432                         const char *str = NULL;
5433                         switch ( __kmp_affinity_type ) {
5434                             case affinity_physical: str = "physical"; break;
5435                             case affinity_logical: str = "logical"; break;
5436                             case affinity_compact: str = "compact"; break;
5437                             case affinity_scatter: str = "scatter"; break;
5438                             case affinity_explicit: str = "explicit"; break;
5439                             // No MIC on windows, so no affinity_balanced case
5440                             default: KMP_DEBUG_ASSERT( 0 );
5441                         }
5442                         KMP_WARNING( AffGranGroupType, var, str );
5443                         __kmp_affinity_gran = affinity_gran_core;
5444                     }
5445                 }
5446             }
5447             else
5448 
5449 # endif /* KMP_GROUP_AFFINITY */
5450 
5451             {
5452                 if ( __kmp_affinity_respect_mask == affinity_respect_mask_default ) {
5453 # if KMP_GROUP_AFFINITY
5454                     if ( __kmp_num_proc_groups > 1 ) {
5455                         __kmp_affinity_respect_mask = FALSE;
5456                     }
5457                     else
5458 # endif /* KMP_GROUP_AFFINITY */
5459                     {
5460                         __kmp_affinity_respect_mask = TRUE;
5461                     }
5462                 }
5463 # if OMP_40_ENABLED
5464                 if ( ( __kmp_nested_proc_bind.bind_types[0] != proc_bind_intel )
5465                   && ( __kmp_nested_proc_bind.bind_types[0] != proc_bind_default ) ) {
5466                     if ( __kmp_affinity_type == affinity_default ) {
5467                         __kmp_affinity_type = affinity_compact;
5468                         __kmp_affinity_dups = FALSE;
5469                     }
5470                 }
5471                 else
5472 # endif /* OMP_40_ENABLED */
5473                 if ( __kmp_affinity_type == affinity_default ) {
5474 #if OMP_40_ENABLED
5475 #if KMP_ARCH_X86_64 && (KMP_OS_LINUX || KMP_OS_WINDOWS)
5476                     if( __kmp_mic_type != non_mic ) {
5477                         __kmp_nested_proc_bind.bind_types[0] = proc_bind_intel;
5478                     } else
5479 #endif
5480                     {
5481                         __kmp_nested_proc_bind.bind_types[0] = proc_bind_false;
5482                     }
5483 #endif /* OMP_40_ENABLED */
5484 #if KMP_ARCH_X86_64 && (KMP_OS_LINUX || KMP_OS_WINDOWS)
5485                     if( __kmp_mic_type != non_mic ) {
5486                         __kmp_affinity_type = affinity_scatter;
5487                     } else
5488 #endif
5489                     {
5490                         __kmp_affinity_type = affinity_none;
5491                     }
5492 
5493                 }
5494                 if ( ( __kmp_affinity_gran == affinity_gran_default )
5495                   &&  ( __kmp_affinity_gran_levels < 0 ) ) {
5496 #if KMP_ARCH_X86_64 && (KMP_OS_LINUX || KMP_OS_WINDOWS)
5497                     if( __kmp_mic_type != non_mic ) {
5498                         __kmp_affinity_gran = affinity_gran_fine;
5499                     } else
5500 #endif
5501                     {
5502                         __kmp_affinity_gran = affinity_gran_core;
5503                     }
5504                 }
5505                 if ( __kmp_affinity_top_method == affinity_top_method_default ) {
5506                     __kmp_affinity_top_method = affinity_top_method_all;
5507                 }
5508             }
5509         }
5510 
5511         K_DIAG( 1, ( "__kmp_affinity_type         == %d\n", __kmp_affinity_type         ) );
5512         K_DIAG( 1, ( "__kmp_affinity_compact      == %d\n", __kmp_affinity_compact      ) );
5513         K_DIAG( 1, ( "__kmp_affinity_offset       == %d\n", __kmp_affinity_offset       ) );
5514         K_DIAG( 1, ( "__kmp_affinity_verbose      == %d\n", __kmp_affinity_verbose      ) );
5515         K_DIAG( 1, ( "__kmp_affinity_warnings     == %d\n", __kmp_affinity_warnings     ) );
5516         K_DIAG( 1, ( "__kmp_affinity_respect_mask == %d\n", __kmp_affinity_respect_mask ) );
5517         K_DIAG( 1, ( "__kmp_affinity_gran         == %d\n", __kmp_affinity_gran         ) );
5518 
5519         KMP_DEBUG_ASSERT( __kmp_affinity_type != affinity_default);
5520 # if OMP_40_ENABLED
5521         KMP_DEBUG_ASSERT( __kmp_nested_proc_bind.bind_types[0] != proc_bind_default );
5522 # endif
5523     }
5524 
5525 #endif /* KMP_AFFINITY_SUPPORTED */
5526 
5527     if ( __kmp_version ) {
5528         __kmp_print_version_1();
5529     }; // if
5530 
5531     // Post-initialization step: some env. vars need their value's further processing
5532     if ( string != NULL) { // kmp_set_defaults() was called
5533         __kmp_aux_env_initialize( &block );
5534     }
5535 
5536     __kmp_env_blk_free( & block );
5537 
5538     KMP_MB();
5539 
5540 } // __kmp_env_initialize
5541 
5542 
5543 void
5544 __kmp_env_print() {
5545 
5546     kmp_env_blk_t block;
5547     int           i;
5548     kmp_str_buf_t buffer;
5549 
5550     __kmp_stg_init();
5551     __kmp_str_buf_init( & buffer );
5552 
5553     __kmp_env_blk_init( & block, NULL );
5554     __kmp_env_blk_sort( & block );
5555 
5556     // Print real environment values.
5557     __kmp_str_buf_print( & buffer, "\n%s\n\n", KMP_I18N_STR( UserSettings )  );
5558     for ( i = 0; i < block.count; ++ i ) {
5559         char const * name  = block.vars[ i ].name;
5560         char const * value = block.vars[ i ].value;
5561         if (
5562             ( KMP_STRLEN( name ) > 4 && strncmp( name, "KMP_", 4 ) == 0 )
5563             || strncmp( name, "OMP_", 4 ) == 0
5564             #ifdef KMP_GOMP_COMPAT
5565                 || strncmp( name, "GOMP_", 5 ) == 0
5566             #endif // KMP_GOMP_COMPAT
5567         ) {
5568             __kmp_str_buf_print( & buffer, "   %s=%s\n", name, value );
5569         }; // if
5570     }; // for
5571     __kmp_str_buf_print( & buffer, "\n" );
5572 
5573     // Print internal (effective) settings.
5574     __kmp_str_buf_print( & buffer, "%s\n\n", KMP_I18N_STR( EffectiveSettings ) );
5575     for ( int i = 0; i < __kmp_stg_count; ++ i ) {
5576         if (  __kmp_stg_table[ i ].print != NULL ) {
5577             __kmp_stg_table[ i ].print( & buffer, __kmp_stg_table[ i ].name, __kmp_stg_table[ i ].data );
5578         }; // if
5579     }; // for
5580 
5581     __kmp_printf( "%s", buffer.str );
5582 
5583     __kmp_env_blk_free( & block );
5584     __kmp_str_buf_free( & buffer );
5585 
5586     __kmp_printf("\n");
5587 
5588 } // __kmp_env_print
5589 
5590 
5591 #if OMP_40_ENABLED
5592 void
5593 __kmp_env_print_2() {
5594 
5595     kmp_env_blk_t block;
5596     kmp_str_buf_t buffer;
5597 
5598     __kmp_env_format = 1;
5599 
5600     __kmp_stg_init();
5601     __kmp_str_buf_init( & buffer );
5602 
5603     __kmp_env_blk_init( & block, NULL );
5604     __kmp_env_blk_sort( & block );
5605 
5606     __kmp_str_buf_print( & buffer, "\n%s\n", KMP_I18N_STR( DisplayEnvBegin )  );
5607     __kmp_str_buf_print( & buffer, "   _OPENMP='%d'\n", __kmp_openmp_version );
5608 
5609     for ( int i = 0; i < __kmp_stg_count; ++ i ) {
5610         if (  __kmp_stg_table[ i ].print != NULL &&
5611               ( ( __kmp_display_env && strncmp( __kmp_stg_table[ i ].name, "OMP_", 4 ) == 0 ) || __kmp_display_env_verbose ) ) {
5612             __kmp_stg_table[ i ].print( & buffer, __kmp_stg_table[ i ].name, __kmp_stg_table[ i ].data );
5613         }; // if
5614     }; // for
5615 
5616     __kmp_str_buf_print( & buffer, "%s\n", KMP_I18N_STR( DisplayEnvEnd )  );
5617     __kmp_str_buf_print( & buffer, "\n" );
5618 
5619     __kmp_printf( "%s", buffer.str );
5620 
5621     __kmp_env_blk_free( & block );
5622     __kmp_str_buf_free( & buffer );
5623 
5624     __kmp_printf("\n");
5625 
5626 } // __kmp_env_print_2
5627 #endif // OMP_40_ENABLED
5628 
5629 // end of file
5630 
5631