]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgomp/env.c
openmp: Add support for OMP_PLACES=ll_caches
[thirdparty/gcc.git] / libgomp / env.c
CommitLineData
99dee823 1/* Copyright (C) 2005-2021 Free Software Foundation, Inc.
953ff289
DN
2 Contributed by Richard Henderson <rth@redhat.com>.
3
f1f3453e
TS
4 This file is part of the GNU Offloading and Multi Processing Library
5 (libgomp).
953ff289
DN
6
7 Libgomp is free software; you can redistribute it and/or modify it
748086b7
JJ
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
953ff289
DN
11
12 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
748086b7 14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
953ff289
DN
15 more details.
16
748086b7
JJ
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
953ff289 20
748086b7
JJ
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
953ff289 25
630e3c3a
AM
26/* This file defines the OpenMP internal control variables and arranges
27 for them to be initialized from environment variables at startup. */
953ff289 28
22f1a037 29#define _GNU_SOURCE
953ff289 30#include "libgomp.h"
630e3c3a
AM
31#include "gomp-constants.h"
32#include <limits.h>
33#ifndef LIBGOMP_OFFLOADED_ONLY
953ff289 34#include "libgomp_f.h"
41dbbb37 35#include "oacc-int.h"
89b3e3cd 36#include <ctype.h>
953ff289 37#include <stdlib.h>
acf0174b
JJ
38#include <stdio.h>
39#ifdef HAVE_INTTYPES_H
40# include <inttypes.h> /* For PRIu64. */
41#endif
a1b25e49
PG
42#ifdef STRING_WITH_STRINGS
43# include <string.h>
44# include <strings.h>
45#else
46# ifdef HAVE_STRING_H
47# include <string.h>
48# else
49# ifdef HAVE_STRINGS_H
50# include <strings.h>
51# endif
52# endif
53#endif
d0d1b24d 54#include <errno.h>
c4060df4 55#include "thread-stacksize.h"
953ff289 56
976e44e3
JJ
57#ifndef HAVE_STRTOULL
58# define strtoull(ptr, eptr, base) strtoul (ptr, eptr, base)
59#endif
630e3c3a
AM
60#endif /* LIBGOMP_OFFLOADED_ONLY */
61
22f1a037
TV
62#include "secure_getenv.h"
63
630e3c3a
AM
64struct gomp_task_icv gomp_global_icv = {
65 .nthreads_var = 1,
66 .thread_limit_var = UINT_MAX,
67 .run_sched_var = GFS_DYNAMIC,
68 .run_sched_chunk_size = 1,
69 .default_device_var = 0,
70 .dyn_var = false,
6fae7eda 71 .max_active_levels_var = 1,
630e3c3a
AM
72 .bind_var = omp_proc_bind_false,
73 .target_data = NULL
74};
75
630e3c3a 76bool gomp_cancel_var = false;
1bfc07d1
KCY
77enum gomp_target_offload_t gomp_target_offload_var
78 = GOMP_TARGET_OFFLOAD_DEFAULT;
630e3c3a
AM
79int gomp_max_task_priority_var = 0;
80#ifndef HAVE_SYNC_BUILTINS
81gomp_mutex_t gomp_managed_threads_lock;
82#endif
83unsigned long gomp_available_cpus = 1, gomp_managed_threads = 1;
84unsigned long long gomp_spin_count_var, gomp_throttled_spin_count_var;
85unsigned long *gomp_nthreads_var_list, gomp_nthreads_var_list_len;
86char *gomp_bind_var_list;
87unsigned long gomp_bind_var_list_len;
88void **gomp_places_list;
89unsigned long gomp_places_list_len;
800bcc8c 90uintptr_t gomp_def_allocator = omp_default_mem_alloc;
630e3c3a
AM
91int gomp_debug_var;
92unsigned int gomp_num_teams_var;
07dd3bcd
JJ
93int gomp_nteams_var;
94int gomp_teams_thread_limit_var;
28567c40
JJ
95bool gomp_display_affinity_var;
96char *gomp_affinity_format_var = "level %L thread %i affinity %A";
97size_t gomp_affinity_format_len;
630e3c3a
AM
98char *goacc_device_type;
99int goacc_device_num;
ec00d3fa 100int goacc_default_dims[GOMP_DIM_MAX];
630e3c3a
AM
101
102#ifndef LIBGOMP_OFFLOADED_ONLY
953ff289 103
7123ae24
UD
104static int wait_policy;
105static unsigned long stacksize = GOMP_DEFAULT_STACKSIZE;
106
953ff289
DN
107/* Parse the OMP_SCHEDULE environment variable. */
108
109static void
110parse_schedule (void)
111{
112 char *env, *end;
6acf0b38 113 unsigned long value;
28567c40 114 int monotonic = 0;
953ff289
DN
115
116 env = getenv ("OMP_SCHEDULE");
117 if (env == NULL)
118 return;
119
89b3e3cd
JJ
120 while (isspace ((unsigned char) *env))
121 ++env;
28567c40
JJ
122 if (strncasecmp (env, "monotonic", 9) == 0)
123 {
124 monotonic = 1;
125 env += 9;
126 }
127 else if (strncasecmp (env, "nonmonotonic", 12) == 0)
128 {
129 monotonic = -1;
130 env += 12;
131 }
132 if (monotonic)
133 {
134 while (isspace ((unsigned char) *env))
135 ++env;
136 if (*env != ':')
137 goto unknown;
138 ++env;
139 while (isspace ((unsigned char) *env))
140 ++env;
141 }
89b3e3cd 142 if (strncasecmp (env, "static", 6) == 0)
953ff289 143 {
a68ab351 144 gomp_global_icv.run_sched_var = GFS_STATIC;
953ff289
DN
145 env += 6;
146 }
89b3e3cd 147 else if (strncasecmp (env, "dynamic", 7) == 0)
953ff289 148 {
a68ab351 149 gomp_global_icv.run_sched_var = GFS_DYNAMIC;
953ff289
DN
150 env += 7;
151 }
89b3e3cd 152 else if (strncasecmp (env, "guided", 6) == 0)
953ff289 153 {
a68ab351 154 gomp_global_icv.run_sched_var = GFS_GUIDED;
953ff289
DN
155 env += 6;
156 }
a68ab351
JJ
157 else if (strncasecmp (env, "auto", 4) == 0)
158 {
159 gomp_global_icv.run_sched_var = GFS_AUTO;
160 env += 4;
161 }
953ff289
DN
162 else
163 goto unknown;
164
28567c40
JJ
165 if (monotonic == 1
166 || (monotonic == 0 && gomp_global_icv.run_sched_var == GFS_STATIC))
167 gomp_global_icv.run_sched_var |= GFS_MONOTONIC;
168
89b3e3cd
JJ
169 while (isspace ((unsigned char) *env))
170 ++env;
953ff289 171 if (*env == '\0')
fb79f500 172 {
d9a6bd32 173 gomp_global_icv.run_sched_chunk_size
28567c40 174 = (gomp_global_icv.run_sched_var & ~GFS_MONOTONIC) != GFS_STATIC;
fb79f500
JJ
175 return;
176 }
89b3e3cd 177 if (*env++ != ',')
953ff289 178 goto unknown;
89b3e3cd
JJ
179 while (isspace ((unsigned char) *env))
180 ++env;
953ff289 181 if (*env == '\0')
953ff289
DN
182 goto invalid;
183
6acf0b38
UB
184 errno = 0;
185 value = strtoul (env, &end, 10);
186 if (errno)
187 goto invalid;
188
89b3e3cd
JJ
189 while (isspace ((unsigned char) *end))
190 ++end;
953ff289
DN
191 if (*end != '\0')
192 goto invalid;
6acf0b38 193
a68ab351
JJ
194 if ((int)value != value)
195 goto invalid;
196
28567c40
JJ
197 if (value == 0
198 && (gomp_global_icv.run_sched_var & ~GFS_MONOTONIC) != GFS_STATIC)
fb79f500 199 value = 1;
d9a6bd32 200 gomp_global_icv.run_sched_chunk_size = value;
953ff289
DN
201 return;
202
203 unknown:
204 gomp_error ("Unknown value for environment variable OMP_SCHEDULE");
205 return;
206
207 invalid:
208 gomp_error ("Invalid value for chunk size in "
209 "environment variable OMP_SCHEDULE");
953ff289
DN
210 return;
211}
212
a68ab351 213/* Parse an unsigned long environment variable. Return true if one was
22f1a037
TV
214 present and it was successfully parsed. If SECURE, use secure_getenv to the
215 environment variable. */
953ff289
DN
216
217static bool
22f1a037
TV
218parse_unsigned_long_1 (const char *name, unsigned long *pvalue, bool allow_zero,
219 bool secure)
953ff289
DN
220{
221 char *env, *end;
d0d1b24d 222 unsigned long value;
953ff289 223
22f1a037 224 env = (secure ? secure_getenv (name) : getenv (name));
953ff289
DN
225 if (env == NULL)
226 return false;
227
89b3e3cd
JJ
228 while (isspace ((unsigned char) *env))
229 ++env;
953ff289
DN
230 if (*env == '\0')
231 goto invalid;
232
6acf0b38 233 errno = 0;
d0d1b24d 234 value = strtoul (env, &end, 10);
80f046cc 235 if (errno || (long) value <= 0 - allow_zero)
6acf0b38
UB
236 goto invalid;
237
89b3e3cd
JJ
238 while (isspace ((unsigned char) *end))
239 ++end;
953ff289
DN
240 if (*end != '\0')
241 goto invalid;
d0d1b24d
RH
242
243 *pvalue = value;
953ff289
DN
244 return true;
245
246 invalid:
d0d1b24d 247 gomp_error ("Invalid value for environment variable %s", name);
953ff289
DN
248 return false;
249}
250
22f1a037
TV
251/* As parse_unsigned_long_1, but always use getenv. */
252
253static bool
254parse_unsigned_long (const char *name, unsigned long *pvalue, bool allow_zero)
255{
256 return parse_unsigned_long_1 (name, pvalue, allow_zero, false);
257}
258
acf0174b 259/* Parse a positive int environment variable. Return true if one was
22f1a037
TV
260 present and it was successfully parsed. If SECURE, use secure_getenv to the
261 environment variable. */
acf0174b
JJ
262
263static bool
22f1a037 264parse_int_1 (const char *name, int *pvalue, bool allow_zero, bool secure)
acf0174b
JJ
265{
266 unsigned long value;
22f1a037 267 if (!parse_unsigned_long_1 (name, &value, allow_zero, secure))
acf0174b
JJ
268 return false;
269 if (value > INT_MAX)
270 {
271 gomp_error ("Invalid value for environment variable %s", name);
272 return false;
273 }
274 *pvalue = (int) value;
275 return true;
276}
277
22f1a037
TV
278/* As parse_int_1, but use getenv. */
279
280static bool
281parse_int (const char *name, int *pvalue, bool allow_zero)
282{
283 return parse_int_1 (name, pvalue, allow_zero, false);
284}
285
286/* As parse_int_1, but use getenv_secure. */
287
288static bool
289parse_int_secure (const char *name, int *pvalue, bool allow_zero)
290{
291 return parse_int_1 (name, pvalue, allow_zero, true);
292}
293
20906c66
JJ
294/* Parse an unsigned long list environment variable. Return true if one was
295 present and it was successfully parsed. */
296
297static bool
298parse_unsigned_long_list (const char *name, unsigned long *p1stvalue,
299 unsigned long **pvalues,
300 unsigned long *pnvalues)
301{
302 char *env, *end;
303 unsigned long value, *values = NULL;
304
305 env = getenv (name);
306 if (env == NULL)
307 return false;
308
309 while (isspace ((unsigned char) *env))
310 ++env;
311 if (*env == '\0')
312 goto invalid;
313
314 errno = 0;
315 value = strtoul (env, &end, 10);
316 if (errno || (long) value <= 0)
317 goto invalid;
318
319 while (isspace ((unsigned char) *end))
320 ++end;
321 if (*end != '\0')
322 {
323 if (*end == ',')
324 {
325 unsigned long nvalues = 0, nalloced = 0;
326
327 do
328 {
329 env = end + 1;
330 if (nvalues == nalloced)
331 {
332 unsigned long *n;
333 nalloced = nalloced ? nalloced * 2 : 16;
334 n = realloc (values, nalloced * sizeof (unsigned long));
335 if (n == NULL)
336 {
337 free (values);
338 gomp_error ("Out of memory while trying to parse"
339 " environment variable %s", name);
340 return false;
341 }
342 values = n;
343 if (nvalues == 0)
344 values[nvalues++] = value;
345 }
346
347 while (isspace ((unsigned char) *env))
348 ++env;
349 if (*env == '\0')
350 goto invalid;
351
352 errno = 0;
353 value = strtoul (env, &end, 10);
354 if (errno || (long) value <= 0)
355 goto invalid;
356
357 values[nvalues++] = value;
358 while (isspace ((unsigned char) *end))
359 ++end;
360 if (*end == '\0')
361 break;
362 if (*end != ',')
363 goto invalid;
364 }
365 while (1);
366 *p1stvalue = values[0];
367 *pvalues = values;
368 *pnvalues = nvalues;
369 return true;
370 }
371 goto invalid;
372 }
373
374 *p1stvalue = value;
375 return true;
376
377 invalid:
378 free (values);
379 gomp_error ("Invalid value for environment variable %s", name);
380 return false;
381}
382
1bfc07d1
KCY
383static void
384parse_target_offload (const char *name, enum gomp_target_offload_t *offload)
385{
386 const char *env;
121a8812 387 int new_offload = -1;
1bfc07d1
KCY
388
389 env = getenv (name);
390 if (env == NULL)
391 return;
392
393 while (isspace ((unsigned char) *env))
394 ++env;
395 if (strncasecmp (env, "default", 7) == 0)
396 {
397 env += 7;
1bfc07d1
KCY
398 new_offload = GOMP_TARGET_OFFLOAD_DEFAULT;
399 }
400 else if (strncasecmp (env, "mandatory", 9) == 0)
401 {
402 env += 9;
1bfc07d1
KCY
403 new_offload = GOMP_TARGET_OFFLOAD_MANDATORY;
404 }
405 else if (strncasecmp (env, "disabled", 8) == 0)
406 {
407 env += 8;
1bfc07d1
KCY
408 new_offload = GOMP_TARGET_OFFLOAD_DISABLED;
409 }
410 while (isspace ((unsigned char) *env))
411 ++env;
121a8812 412 if (new_offload != -1 && *env == '\0')
1bfc07d1
KCY
413 {
414 *offload = new_offload;
415 return;
416 }
417
418 gomp_error ("Invalid value for environment variable OMP_TARGET_OFFLOAD");
419}
420
acf0174b
JJ
421/* Parse environment variable set to a boolean or list of omp_proc_bind_t
422 enum values. Return true if one was present and it was successfully
423 parsed. */
424
425static bool
426parse_bind_var (const char *name, char *p1stvalue,
427 char **pvalues, unsigned long *pnvalues)
428{
429 char *env;
c8673881 430 char value = omp_proc_bind_false, *values = NULL;
acf0174b
JJ
431 int i;
432 static struct proc_bind_kinds
433 {
434 const char name[7];
435 const char len;
436 omp_proc_bind_t kind;
437 } kinds[] =
438 {
439 { "false", 5, omp_proc_bind_false },
440 { "true", 4, omp_proc_bind_true },
441 { "master", 6, omp_proc_bind_master },
432de084 442 { "primary", 7, omp_proc_bind_primary },
acf0174b
JJ
443 { "close", 5, omp_proc_bind_close },
444 { "spread", 6, omp_proc_bind_spread }
445 };
446
447 env = getenv (name);
448 if (env == NULL)
449 return false;
450
451 while (isspace ((unsigned char) *env))
452 ++env;
453 if (*env == '\0')
454 goto invalid;
455
432de084 456 for (i = 0; i < 6; i++)
acf0174b
JJ
457 if (strncasecmp (env, kinds[i].name, kinds[i].len) == 0)
458 {
459 value = kinds[i].kind;
460 env += kinds[i].len;
461 break;
462 }
432de084 463 if (i == 6)
acf0174b
JJ
464 goto invalid;
465
466 while (isspace ((unsigned char) *env))
467 ++env;
468 if (*env != '\0')
469 {
470 if (*env == ',')
471 {
472 unsigned long nvalues = 0, nalloced = 0;
473
474 if (value == omp_proc_bind_false
475 || value == omp_proc_bind_true)
476 goto invalid;
477
478 do
479 {
480 env++;
481 if (nvalues == nalloced)
482 {
483 char *n;
484 nalloced = nalloced ? nalloced * 2 : 16;
485 n = realloc (values, nalloced);
486 if (n == NULL)
487 {
488 free (values);
489 gomp_error ("Out of memory while trying to parse"
490 " environment variable %s", name);
491 return false;
492 }
493 values = n;
494 if (nvalues == 0)
495 values[nvalues++] = value;
496 }
497
498 while (isspace ((unsigned char) *env))
499 ++env;
500 if (*env == '\0')
501 goto invalid;
502
432de084 503 for (i = 2; i < 6; i++)
acf0174b
JJ
504 if (strncasecmp (env, kinds[i].name, kinds[i].len) == 0)
505 {
506 value = kinds[i].kind;
507 env += kinds[i].len;
508 break;
509 }
432de084 510 if (i == 6)
acf0174b
JJ
511 goto invalid;
512
513 values[nvalues++] = value;
514 while (isspace ((unsigned char) *env))
515 ++env;
516 if (*env == '\0')
517 break;
518 if (*env != ',')
519 goto invalid;
520 }
521 while (1);
522 *p1stvalue = values[0];
523 *pvalues = values;
524 *pnvalues = nvalues;
525 return true;
526 }
527 goto invalid;
528 }
529
530 *p1stvalue = value;
531 return true;
532
533 invalid:
534 free (values);
535 gomp_error ("Invalid value for environment variable %s", name);
536 return false;
537}
538
539static bool
540parse_one_place (char **envp, bool *negatep, unsigned long *lenp,
541 long *stridep)
542{
543 char *env = *envp, *start;
544 void *p = gomp_places_list ? gomp_places_list[gomp_places_list_len] : NULL;
545 unsigned long len = 1;
546 long stride = 1;
547 int pass;
548 bool any_negate = false;
549 *negatep = false;
550 while (isspace ((unsigned char) *env))
551 ++env;
552 if (*env == '!')
553 {
554 *negatep = true;
555 ++env;
556 while (isspace ((unsigned char) *env))
557 ++env;
558 }
559 if (*env != '{')
560 return false;
561 ++env;
562 while (isspace ((unsigned char) *env))
563 ++env;
564 start = env;
565 for (pass = 0; pass < (any_negate ? 2 : 1); pass++)
566 {
567 env = start;
568 do
569 {
570 unsigned long this_num, this_len = 1;
571 long this_stride = 1;
572 bool this_negate = (*env == '!');
573 if (this_negate)
574 {
575 if (gomp_places_list)
576 any_negate = true;
577 ++env;
578 while (isspace ((unsigned char) *env))
579 ++env;
580 }
581
582 errno = 0;
583 this_num = strtoul (env, &env, 10);
584 if (errno)
585 return false;
586 while (isspace ((unsigned char) *env))
587 ++env;
588 if (*env == ':')
589 {
590 ++env;
591 while (isspace ((unsigned char) *env))
592 ++env;
593 errno = 0;
594 this_len = strtoul (env, &env, 10);
595 if (errno || this_len == 0)
596 return false;
597 while (isspace ((unsigned char) *env))
598 ++env;
599 if (*env == ':')
600 {
601 ++env;
602 while (isspace ((unsigned char) *env))
603 ++env;
604 errno = 0;
605 this_stride = strtol (env, &env, 10);
606 if (errno)
607 return false;
608 while (isspace ((unsigned char) *env))
609 ++env;
610 }
611 }
612 if (this_negate && this_len != 1)
613 return false;
614 if (gomp_places_list && pass == this_negate)
615 {
616 if (this_negate)
617 {
618 if (!gomp_affinity_remove_cpu (p, this_num))
619 return false;
620 }
621 else if (!gomp_affinity_add_cpus (p, this_num, this_len,
622 this_stride, false))
623 return false;
624 }
625 if (*env == '}')
626 break;
627 if (*env != ',')
628 return false;
629 ++env;
630 }
631 while (1);
632 }
633
634 ++env;
635 while (isspace ((unsigned char) *env))
636 ++env;
637 if (*env == ':')
638 {
639 ++env;
640 while (isspace ((unsigned char) *env))
641 ++env;
642 errno = 0;
643 len = strtoul (env, &env, 10);
644 if (errno || len == 0 || len >= 65536)
645 return false;
646 while (isspace ((unsigned char) *env))
647 ++env;
648 if (*env == ':')
649 {
650 ++env;
651 while (isspace ((unsigned char) *env))
652 ++env;
653 errno = 0;
654 stride = strtol (env, &env, 10);
655 if (errno)
656 return false;
657 while (isspace ((unsigned char) *env))
658 ++env;
659 }
660 }
661 if (*negatep && len != 1)
662 return false;
663 *envp = env;
664 *lenp = len;
665 *stridep = stride;
666 return true;
667}
668
669static bool
f89163fd 670parse_places_var (const char *name, bool ignore)
acf0174b
JJ
671{
672 char *env = getenv (name), *end;
673 bool any_negate = false;
674 int level = 0;
675 unsigned long count = 0;
676 if (env == NULL)
677 return false;
678
679 while (isspace ((unsigned char) *env))
680 ++env;
681 if (*env == '\0')
682 goto invalid;
683
684 if (strncasecmp (env, "threads", 7) == 0)
685 {
686 env += 7;
687 level = 1;
688 }
689 else if (strncasecmp (env, "cores", 5) == 0)
690 {
691 env += 5;
692 level = 2;
693 }
694 else if (strncasecmp (env, "sockets", 7) == 0)
695 {
696 env += 7;
697 level = 3;
698 }
5809be05
JJ
699 else if (strncasecmp (env, "ll_caches", 9) == 0)
700 {
701 env += 9;
702 level = 4;
703 }
acf0174b
JJ
704 if (level)
705 {
706 count = ULONG_MAX;
707 while (isspace ((unsigned char) *env))
708 ++env;
709 if (*env != '\0')
710 {
711 if (*env++ != '(')
712 goto invalid;
713 while (isspace ((unsigned char) *env))
714 ++env;
715
716 errno = 0;
717 count = strtoul (env, &end, 10);
718 if (errno)
719 goto invalid;
720 env = end;
721 while (isspace ((unsigned char) *env))
722 ++env;
723 if (*env != ')')
724 goto invalid;
725 ++env;
726 while (isspace ((unsigned char) *env))
727 ++env;
728 if (*env != '\0')
729 goto invalid;
730 }
f89163fd
JJ
731
732 if (ignore)
733 return false;
734
acf0174b
JJ
735 return gomp_affinity_init_level (level, count, false);
736 }
737
738 count = 0;
739 end = env;
740 do
741 {
742 bool negate;
743 unsigned long len;
744 long stride;
745 if (!parse_one_place (&end, &negate, &len, &stride))
746 goto invalid;
747 if (negate)
748 {
749 if (!any_negate)
750 count++;
751 any_negate = true;
752 }
753 else
754 count += len;
755 if (count > 65536)
756 goto invalid;
757 if (*end == '\0')
758 break;
759 if (*end != ',')
760 goto invalid;
761 end++;
762 }
763 while (1);
764
f89163fd 765 if (ignore)
acf0174b
JJ
766 return false;
767
768 gomp_places_list_len = 0;
769 gomp_places_list = gomp_affinity_alloc (count, false);
770 if (gomp_places_list == NULL)
771 return false;
772
773 do
774 {
775 bool negate;
776 unsigned long len;
777 long stride;
778 gomp_affinity_init_place (gomp_places_list[gomp_places_list_len]);
779 if (!parse_one_place (&env, &negate, &len, &stride))
780 goto invalid;
781 if (negate)
782 {
783 void *p;
784 for (count = 0; count < gomp_places_list_len; count++)
785 if (gomp_affinity_same_place
786 (gomp_places_list[count],
787 gomp_places_list[gomp_places_list_len]))
788 break;
789 if (count == gomp_places_list_len)
790 {
791 gomp_error ("Trying to remove a non-existing place from list "
792 "of places");
793 goto invalid;
794 }
795 p = gomp_places_list[count];
796 memmove (&gomp_places_list[count],
797 &gomp_places_list[count + 1],
798 (gomp_places_list_len - count - 1) * sizeof (void *));
799 --gomp_places_list_len;
800 gomp_places_list[gomp_places_list_len] = p;
801 }
802 else if (len == 1)
803 ++gomp_places_list_len;
804 else
805 {
806 for (count = 0; count < len - 1; count++)
807 if (!gomp_affinity_copy_place
808 (gomp_places_list[gomp_places_list_len + count + 1],
809 gomp_places_list[gomp_places_list_len + count],
810 stride))
811 goto invalid;
812 gomp_places_list_len += len;
813 }
814 if (*env == '\0')
815 break;
816 env++;
817 }
818 while (1);
819
820 if (gomp_places_list_len == 0)
821 {
822 gomp_error ("All places have been removed");
823 goto invalid;
824 }
825 if (!gomp_affinity_finalize_place_list (false))
826 goto invalid;
827 return true;
828
829 invalid:
830 free (gomp_places_list);
831 gomp_places_list = NULL;
832 gomp_places_list_len = 0;
833 gomp_error ("Invalid value for environment variable %s", name);
834 return false;
835}
836
a68ab351
JJ
837/* Parse the OMP_STACKSIZE environment varible. Return true if one was
838 present and it was successfully parsed. */
839
840static bool
841parse_stacksize (const char *name, unsigned long *pvalue)
842{
843 char *env, *end;
844 unsigned long value, shift = 10;
845
846 env = getenv (name);
847 if (env == NULL)
848 return false;
849
850 while (isspace ((unsigned char) *env))
851 ++env;
852 if (*env == '\0')
853 goto invalid;
854
855 errno = 0;
856 value = strtoul (env, &end, 10);
857 if (errno)
858 goto invalid;
859
860 while (isspace ((unsigned char) *end))
861 ++end;
862 if (*end != '\0')
863 {
323ff903 864 switch (tolower ((unsigned char) *end))
a68ab351
JJ
865 {
866 case 'b':
867 shift = 0;
868 break;
869 case 'k':
870 break;
871 case 'm':
872 shift = 20;
873 break;
874 case 'g':
875 shift = 30;
876 break;
877 default:
878 goto invalid;
879 }
880 ++end;
881 while (isspace ((unsigned char) *end))
882 ++end;
883 if (*end != '\0')
884 goto invalid;
885 }
886
887 if (((value << shift) >> shift) != value)
888 goto invalid;
889
890 *pvalue = value << shift;
891 return true;
892
893 invalid:
894 gomp_error ("Invalid value for environment variable %s", name);
895 return false;
896}
897
898/* Parse the GOMP_SPINCOUNT environment varible. Return true if one was
899 present and it was successfully parsed. */
900
901static bool
902parse_spincount (const char *name, unsigned long long *pvalue)
903{
904 char *env, *end;
905 unsigned long long value, mult = 1;
906
907 env = getenv (name);
908 if (env == NULL)
909 return false;
910
911 while (isspace ((unsigned char) *env))
912 ++env;
913 if (*env == '\0')
914 goto invalid;
915
916 if (strncasecmp (env, "infinite", 8) == 0
917 || strncasecmp (env, "infinity", 8) == 0)
918 {
919 value = ~0ULL;
920 end = env + 8;
921 goto check_tail;
922 }
923
924 errno = 0;
925 value = strtoull (env, &end, 10);
926 if (errno)
927 goto invalid;
928
929 while (isspace ((unsigned char) *end))
930 ++end;
931 if (*end != '\0')
932 {
323ff903 933 switch (tolower ((unsigned char) *end))
a68ab351
JJ
934 {
935 case 'k':
936 mult = 1000LL;
937 break;
938 case 'm':
939 mult = 1000LL * 1000LL;
940 break;
941 case 'g':
942 mult = 1000LL * 1000LL * 1000LL;
943 break;
944 case 't':
945 mult = 1000LL * 1000LL * 1000LL * 1000LL;
946 break;
947 default:
948 goto invalid;
949 }
950 ++end;
951 check_tail:
952 while (isspace ((unsigned char) *end))
953 ++end;
954 if (*end != '\0')
955 goto invalid;
956 }
957
958 if (value > ~0ULL / mult)
959 value = ~0ULL;
960 else
961 value *= mult;
962
963 *pvalue = value;
964 return true;
965
966 invalid:
967 gomp_error ("Invalid value for environment variable %s", name);
968 return false;
969}
970
971/* Parse a boolean value for environment variable NAME and store the
6fae7eda
KCY
972 result in VALUE. Return true if one was present and it was
973 successfully parsed. */
953ff289 974
6fae7eda 975static bool
953ff289
DN
976parse_boolean (const char *name, bool *value)
977{
978 const char *env;
979
980 env = getenv (name);
981 if (env == NULL)
6fae7eda 982 return false;
953ff289 983
89b3e3cd
JJ
984 while (isspace ((unsigned char) *env))
985 ++env;
986 if (strncasecmp (env, "true", 4) == 0)
987 {
988 *value = true;
989 env += 4;
990 }
991 else if (strncasecmp (env, "false", 5) == 0)
992 {
993 *value = false;
994 env += 5;
995 }
953ff289 996 else
89b3e3cd
JJ
997 env = "X";
998 while (isspace ((unsigned char) *env))
999 ++env;
1000 if (*env != '\0')
6fae7eda
KCY
1001 {
1002 gomp_error ("Invalid value for environment variable %s", name);
1003 return false;
1004 }
1005 return true;
953ff289
DN
1006}
1007
800bcc8c 1008/* Parse the OMP_WAIT_POLICY environment variable and return the value. */
a68ab351
JJ
1009
1010static int
1011parse_wait_policy (void)
1012{
1013 const char *env;
1014 int ret = -1;
1015
1016 env = getenv ("OMP_WAIT_POLICY");
1017 if (env == NULL)
1018 return -1;
1019
1020 while (isspace ((unsigned char) *env))
1021 ++env;
1022 if (strncasecmp (env, "active", 6) == 0)
1023 {
1024 ret = 1;
1025 env += 6;
1026 }
1027 else if (strncasecmp (env, "passive", 7) == 0)
1028 {
1029 ret = 0;
1030 env += 7;
1031 }
1032 else
1033 env = "X";
1034 while (isspace ((unsigned char) *env))
1035 ++env;
1036 if (*env == '\0')
1037 return ret;
1038 gomp_error ("Invalid value for environment variable OMP_WAIT_POLICY");
1039 return -1;
1040}
1041
a0884cf0
JJ
1042/* Parse the GOMP_CPU_AFFINITY environment varible. Return true if one was
1043 present and it was successfully parsed. */
1044
1045static bool
f89163fd 1046parse_affinity (bool ignore)
a0884cf0 1047{
acf0174b
JJ
1048 char *env, *end, *start;
1049 int pass;
a0884cf0 1050 unsigned long cpu_beg, cpu_end, cpu_stride;
acf0174b 1051 size_t count = 0, needed;
a0884cf0
JJ
1052
1053 env = getenv ("GOMP_CPU_AFFINITY");
1054 if (env == NULL)
1055 return false;
1056
acf0174b
JJ
1057 start = env;
1058 for (pass = 0; pass < 2; pass++)
a0884cf0 1059 {
acf0174b
JJ
1060 env = start;
1061 if (pass == 1)
a0884cf0 1062 {
f89163fd
JJ
1063 if (ignore)
1064 return false;
1065
acf0174b
JJ
1066 gomp_places_list_len = 0;
1067 gomp_places_list = gomp_affinity_alloc (count, true);
1068 if (gomp_places_list == NULL)
1069 return false;
1070 }
1071 do
1072 {
1073 while (isspace ((unsigned char) *env))
1074 ++env;
1075
1076 errno = 0;
1077 cpu_beg = strtoul (env, &end, 0);
1078 if (errno || cpu_beg >= 65536)
a0884cf0 1079 goto invalid;
acf0174b
JJ
1080 cpu_end = cpu_beg;
1081 cpu_stride = 1;
a0884cf0
JJ
1082
1083 env = end;
acf0174b 1084 if (*env == '-')
a0884cf0 1085 {
acf0174b
JJ
1086 errno = 0;
1087 cpu_end = strtoul (++env, &end, 0);
1088 if (errno || cpu_end >= 65536 || cpu_end < cpu_beg)
a0884cf0
JJ
1089 goto invalid;
1090
1091 env = end;
acf0174b
JJ
1092 if (*env == ':')
1093 {
1094 errno = 0;
1095 cpu_stride = strtoul (++env, &end, 0);
1096 if (errno || cpu_stride == 0 || cpu_stride >= 65536)
1097 goto invalid;
a0884cf0 1098
acf0174b
JJ
1099 env = end;
1100 }
1101 }
a0884cf0 1102
acf0174b
JJ
1103 needed = (cpu_end - cpu_beg) / cpu_stride + 1;
1104 if (pass == 0)
1105 count += needed;
a0884cf0 1106 else
a0884cf0 1107 {
acf0174b
JJ
1108 while (needed--)
1109 {
1110 void *p = gomp_places_list[gomp_places_list_len];
1111 gomp_affinity_init_place (p);
1112 if (gomp_affinity_add_cpus (p, cpu_beg, 1, 0, true))
1113 ++gomp_places_list_len;
1114 cpu_beg += cpu_stride;
1115 }
a0884cf0
JJ
1116 }
1117
acf0174b
JJ
1118 while (isspace ((unsigned char) *env))
1119 ++env;
a0884cf0 1120
acf0174b
JJ
1121 if (*env == ',')
1122 env++;
1123 else if (*env == '\0')
1124 break;
a0884cf0 1125 }
acf0174b 1126 while (1);
a0884cf0 1127 }
a0884cf0 1128
acf0174b
JJ
1129 if (gomp_places_list_len == 0)
1130 {
1131 free (gomp_places_list);
1132 gomp_places_list = NULL;
f89163fd 1133 return false;
acf0174b 1134 }
a0884cf0
JJ
1135 return true;
1136
1137 invalid:
1138 gomp_error ("Invalid value for enviroment variable GOMP_CPU_AFFINITY");
1139 return false;
1140}
1141
800bcc8c
JJ
1142/* Parse the OMP_ALLOCATOR environment variable and return the value. */
1143
1144static uintptr_t
1145parse_allocator (void)
1146{
1147 const char *env;
1148 uintptr_t ret = omp_default_mem_alloc;
1149
1150 env = getenv ("OMP_ALLOCATOR");
1151 if (env == NULL)
1152 return ret;
1153
1154 while (isspace ((unsigned char) *env))
1155 ++env;
1156 if (0)
1157 ;
1158#define C(v) \
1159 else if (strncasecmp (env, #v, sizeof (#v) - 1) == 0) \
1160 { \
1161 ret = v; \
1162 env += sizeof (#v) - 1; \
1163 }
1164 C (omp_default_mem_alloc)
1165 C (omp_large_cap_mem_alloc)
1166 C (omp_const_mem_alloc)
1167 C (omp_high_bw_mem_alloc)
1168 C (omp_low_lat_mem_alloc)
1169 C (omp_cgroup_mem_alloc)
1170 C (omp_pteam_mem_alloc)
1171 C (omp_thread_mem_alloc)
1172#undef C
1173 else
1174 env = "X";
1175 while (isspace ((unsigned char) *env))
1176 ++env;
1177 if (*env == '\0')
1178 return ret;
1179 gomp_error ("Invalid value for environment variable OMP_ALLOCATOR");
1180 return omp_default_mem_alloc;
1181}
1182
41dbbb37
TS
1183static void
1184parse_acc_device_type (void)
1185{
1186 const char *env = getenv ("ACC_DEVICE_TYPE");
1187
1188 if (env && *env != '\0')
1189 goacc_device_type = strdup (env);
1190 else
1191 goacc_device_type = NULL;
1192}
acf0174b 1193
ec00d3fa
TV
1194static void
1195parse_gomp_openacc_dim (void)
1196{
1197 /* The syntax is the same as for the -fopenacc-dim compilation option. */
1198 const char *var_name = "GOMP_OPENACC_DIM";
1199 const char *env_var = getenv (var_name);
1200 if (!env_var)
1201 return;
1202
1203 const char *pos = env_var;
1204 int i;
1205 for (i = 0; *pos && i != GOMP_DIM_MAX; i++)
1206 {
1207 if (i && *pos++ != ':')
1208 break;
1209
1210 if (*pos == ':')
1211 continue;
1212
1213 const char *eptr;
1214 errno = 0;
1215 long val = strtol (pos, (char **)&eptr, 10);
1216 if (errno || val < 0 || (unsigned)val != val)
1217 break;
1218
1219 goacc_default_dims[i] = (int)val;
1220 pos = eptr;
1221 }
1222}
1223
7123ae24
UD
1224void
1225omp_display_env (int verbose)
acf0174b 1226{
acf0174b
JJ
1227 int i;
1228
acf0174b
JJ
1229 fputs ("\nOPENMP DISPLAY ENVIRONMENT BEGIN\n", stderr);
1230
d9a6bd32 1231 fputs (" _OPENMP = '201511'\n", stderr);
acf0174b
JJ
1232 fprintf (stderr, " OMP_DYNAMIC = '%s'\n",
1233 gomp_global_icv.dyn_var ? "TRUE" : "FALSE");
1234 fprintf (stderr, " OMP_NESTED = '%s'\n",
6fae7eda 1235 gomp_global_icv.max_active_levels_var > 1 ? "TRUE" : "FALSE");
acf0174b
JJ
1236
1237 fprintf (stderr, " OMP_NUM_THREADS = '%lu", gomp_global_icv.nthreads_var);
1238 for (i = 1; i < gomp_nthreads_var_list_len; i++)
1239 fprintf (stderr, ",%lu", gomp_nthreads_var_list[i]);
1240 fputs ("'\n", stderr);
1241
1242 fprintf (stderr, " OMP_SCHEDULE = '");
28567c40
JJ
1243 if ((gomp_global_icv.run_sched_var & GFS_MONOTONIC))
1244 {
1245 if (gomp_global_icv.run_sched_var != (GFS_MONOTONIC | GFS_STATIC))
1246 fputs ("MONOTONIC:", stderr);
1247 }
1248 else if (gomp_global_icv.run_sched_var == GFS_STATIC)
1249 fputs ("NONMONOTONIC:", stderr);
1250 switch (gomp_global_icv.run_sched_var & ~GFS_MONOTONIC)
acf0174b
JJ
1251 {
1252 case GFS_RUNTIME:
1253 fputs ("RUNTIME", stderr);
28567c40
JJ
1254 if (gomp_global_icv.run_sched_chunk_size != 1)
1255 fprintf (stderr, ",%d", gomp_global_icv.run_sched_chunk_size);
acf0174b
JJ
1256 break;
1257 case GFS_STATIC:
1258 fputs ("STATIC", stderr);
28567c40
JJ
1259 if (gomp_global_icv.run_sched_chunk_size != 0)
1260 fprintf (stderr, ",%d", gomp_global_icv.run_sched_chunk_size);
acf0174b
JJ
1261 break;
1262 case GFS_DYNAMIC:
1263 fputs ("DYNAMIC", stderr);
28567c40
JJ
1264 if (gomp_global_icv.run_sched_chunk_size != 1)
1265 fprintf (stderr, ",%d", gomp_global_icv.run_sched_chunk_size);
acf0174b
JJ
1266 break;
1267 case GFS_GUIDED:
1268 fputs ("GUIDED", stderr);
28567c40
JJ
1269 if (gomp_global_icv.run_sched_chunk_size != 1)
1270 fprintf (stderr, ",%d", gomp_global_icv.run_sched_chunk_size);
acf0174b
JJ
1271 break;
1272 case GFS_AUTO:
1273 fputs ("AUTO", stderr);
1274 break;
1275 }
1276 fputs ("'\n", stderr);
1277
1278 fputs (" OMP_PROC_BIND = '", stderr);
1279 switch (gomp_global_icv.bind_var)
1280 {
1281 case omp_proc_bind_false:
1282 fputs ("FALSE", stderr);
1283 break;
1284 case omp_proc_bind_true:
1285 fputs ("TRUE", stderr);
1286 break;
1287 case omp_proc_bind_master:
432de084 1288 fputs ("MASTER", stderr); /* TODO: Change to PRIMARY for OpenMP 5.1. */
acf0174b
JJ
1289 break;
1290 case omp_proc_bind_close:
1291 fputs ("CLOSE", stderr);
1292 break;
1293 case omp_proc_bind_spread:
1294 fputs ("SPREAD", stderr);
1295 break;
1296 }
1297 for (i = 1; i < gomp_bind_var_list_len; i++)
1298 switch (gomp_bind_var_list[i])
1299 {
1300 case omp_proc_bind_master:
432de084 1301 fputs (",MASTER", stderr); /* TODO: Change to PRIMARY for OpenMP 5.1. */
acf0174b
JJ
1302 break;
1303 case omp_proc_bind_close:
1304 fputs (",CLOSE", stderr);
1305 break;
1306 case omp_proc_bind_spread:
1307 fputs (",SPREAD", stderr);
1308 break;
1309 }
1310 fputs ("'\n", stderr);
1311 fputs (" OMP_PLACES = '", stderr);
1312 for (i = 0; i < gomp_places_list_len; i++)
1313 {
1314 fputs ("{", stderr);
1315 gomp_affinity_print_place (gomp_places_list[i]);
1316 fputs (i + 1 == gomp_places_list_len ? "}" : "},", stderr);
1317 }
1318 fputs ("'\n", stderr);
1319
1320 fprintf (stderr, " OMP_STACKSIZE = '%lu'\n", stacksize);
1321
1322 /* GOMP's default value is actually neither active nor passive. */
1323 fprintf (stderr, " OMP_WAIT_POLICY = '%s'\n",
1324 wait_policy > 0 ? "ACTIVE" : "PASSIVE");
1325 fprintf (stderr, " OMP_THREAD_LIMIT = '%u'\n",
1326 gomp_global_icv.thread_limit_var);
6fae7eda
KCY
1327 fprintf (stderr, " OMP_MAX_ACTIVE_LEVELS = '%u'\n",
1328 gomp_global_icv.max_active_levels_var);
07dd3bcd
JJ
1329 fprintf (stderr, " OMP_NUM_TEAMS = '%u'\n", gomp_nteams_var);
1330 fprintf (stderr, " OMP_TEAMS_THREAD_LIMIT = '%u'\n",
1331 gomp_teams_thread_limit_var);
acf0174b
JJ
1332
1333 fprintf (stderr, " OMP_CANCELLATION = '%s'\n",
1334 gomp_cancel_var ? "TRUE" : "FALSE");
1335 fprintf (stderr, " OMP_DEFAULT_DEVICE = '%d'\n",
1336 gomp_global_icv.default_device_var);
d9a6bd32
JJ
1337 fprintf (stderr, " OMP_MAX_TASK_PRIORITY = '%d'\n",
1338 gomp_max_task_priority_var);
28567c40
JJ
1339 fprintf (stderr, " OMP_DISPLAY_AFFINITY = '%s'\n",
1340 gomp_display_affinity_var ? "TRUE" : "FALSE");
1341 fprintf (stderr, " OMP_AFFINITY_FORMAT = '%s'\n",
1342 gomp_affinity_format_var);
800bcc8c
JJ
1343 fprintf (stderr, " OMP_ALLOCATOR = '");
1344 switch (gomp_def_allocator)
1345 {
1346#define C(v) case v: fputs (#v, stderr); break;
1347 C (omp_default_mem_alloc)
1348 C (omp_large_cap_mem_alloc)
1349 C (omp_const_mem_alloc)
1350 C (omp_high_bw_mem_alloc)
1351 C (omp_low_lat_mem_alloc)
1352 C (omp_cgroup_mem_alloc)
1353 C (omp_pteam_mem_alloc)
1354 C (omp_thread_mem_alloc)
1355#undef C
1356 default: break;
1357 }
1358 fputs ("'\n", stderr);
acf0174b 1359
1bfc07d1
KCY
1360 fputs (" OMP_TARGET_OFFLOAD = '", stderr);
1361 switch (gomp_target_offload_var)
1362 {
1363 case GOMP_TARGET_OFFLOAD_DEFAULT:
1364 fputs ("DEFAULT", stderr);
1365 break;
1366 case GOMP_TARGET_OFFLOAD_MANDATORY:
1367 fputs ("MANDATORY", stderr);
1368 break;
1369 case GOMP_TARGET_OFFLOAD_DISABLED:
1370 fputs ("DISABLED", stderr);
1371 break;
1372 }
1373 fputs ("'\n", stderr);
1374
acf0174b
JJ
1375 if (verbose)
1376 {
1377 fputs (" GOMP_CPU_AFFINITY = ''\n", stderr);
1378 fprintf (stderr, " GOMP_STACKSIZE = '%lu'\n", stacksize);
1379#ifdef HAVE_INTTYPES_H
1380 fprintf (stderr, " GOMP_SPINCOUNT = '%"PRIu64"'\n",
1381 (uint64_t) gomp_spin_count_var);
1382#else
1383 fprintf (stderr, " GOMP_SPINCOUNT = '%lu'\n",
1384 (unsigned long) gomp_spin_count_var);
1385#endif
1386 }
1387
1388 fputs ("OPENMP DISPLAY ENVIRONMENT END\n", stderr);
1389}
7123ae24
UD
1390ialias (omp_display_env)
1391
1392static void
1393handle_omp_display_env (void)
1394{
1395 const char *env;
1396 bool display = false;
1397 bool verbose = false;
1398
1399 env = getenv ("OMP_DISPLAY_ENV");
1400 if (env == NULL)
1401 return;
1402
1403 while (isspace ((unsigned char) *env))
1404 ++env;
1405 if (strncasecmp (env, "true", 4) == 0)
1406 {
1407 display = true;
1408 env += 4;
1409 }
1410 else if (strncasecmp (env, "false", 5) == 0)
1411 {
1412 display = false;
1413 env += 5;
1414 }
1415 else if (strncasecmp (env, "verbose", 7) == 0)
1416 {
1417 display = true;
1418 verbose = true;
1419 env += 7;
1420 }
1421 else
1422 env = "X";
1423 while (isspace ((unsigned char) *env))
1424 ++env;
1425 if (*env != '\0')
1426 gomp_error ("Invalid value for environment variable OMP_DISPLAY_ENV");
1427
1428 if (display)
3749c3af 1429 ialias_call (omp_display_env) (verbose);
7123ae24 1430}
acf0174b
JJ
1431
1432
953ff289
DN
1433static void __attribute__((constructor))
1434initialize_env (void)
1435{
7123ae24 1436 unsigned long thread_limit_var;
6fae7eda 1437 unsigned long max_active_levels_var;
d0d1b24d 1438
953ff289
DN
1439 /* Do a compile time check that mkomp_h.pl did good job. */
1440 omp_check_defines ();
1441
1442 parse_schedule ();
a68ab351 1443 parse_boolean ("OMP_DYNAMIC", &gomp_global_icv.dyn_var);
acf0174b 1444 parse_boolean ("OMP_CANCELLATION", &gomp_cancel_var);
28567c40 1445 parse_boolean ("OMP_DISPLAY_AFFINITY", &gomp_display_affinity_var);
acf0174b 1446 parse_int ("OMP_DEFAULT_DEVICE", &gomp_global_icv.default_device_var, true);
1bfc07d1 1447 parse_target_offload ("OMP_TARGET_OFFLOAD", &gomp_target_offload_var);
d9a6bd32 1448 parse_int ("OMP_MAX_TASK_PRIORITY", &gomp_max_task_priority_var, true);
800bcc8c 1449 gomp_def_allocator = parse_allocator ();
acf0174b
JJ
1450 if (parse_unsigned_long ("OMP_THREAD_LIMIT", &thread_limit_var, false))
1451 {
1452 gomp_global_icv.thread_limit_var
1453 = thread_limit_var > INT_MAX ? UINT_MAX : thread_limit_var;
1454 }
22f1a037 1455 parse_int_secure ("GOMP_DEBUG", &gomp_debug_var, true);
a68ab351 1456#ifndef HAVE_SYNC_BUILTINS
acf0174b 1457 gomp_mutex_init (&gomp_managed_threads_lock);
a68ab351 1458#endif
a68ab351
JJ
1459 gomp_init_num_threads ();
1460 gomp_available_cpus = gomp_global_icv.nthreads_var;
20906c66
JJ
1461 if (!parse_unsigned_long_list ("OMP_NUM_THREADS",
1462 &gomp_global_icv.nthreads_var,
1463 &gomp_nthreads_var_list,
1464 &gomp_nthreads_var_list_len))
a68ab351 1465 gomp_global_icv.nthreads_var = gomp_available_cpus;
07dd3bcd
JJ
1466 parse_int ("OMP_NUM_TEAMS", &gomp_nteams_var, false);
1467 parse_int ("OMP_TEAMS_THREAD_LIMIT", &gomp_teams_thread_limit_var, false);
f89163fd
JJ
1468 bool ignore = false;
1469 if (parse_bind_var ("OMP_PROC_BIND",
1470 &gomp_global_icv.bind_var,
1471 &gomp_bind_var_list,
1472 &gomp_bind_var_list_len)
1473 && gomp_global_icv.bind_var == omp_proc_bind_false)
1474 ignore = true;
6fae7eda
KCY
1475 if (parse_unsigned_long ("OMP_MAX_ACTIVE_LEVELS",
1476 &max_active_levels_var, true))
1477 gomp_global_icv.max_active_levels_var
1478 = (max_active_levels_var > gomp_supported_active_levels)
1479 ? gomp_supported_active_levels : max_active_levels_var;
1480 else
1481 {
1482 bool nested = true;
1483
1484 /* OMP_NESTED is deprecated in OpenMP 5.0. */
1485 if (parse_boolean ("OMP_NESTED", &nested))
1486 gomp_global_icv.max_active_levels_var
1487 = nested ? gomp_supported_active_levels : 1;
1488 else if (gomp_nthreads_var_list_len > 1 || gomp_bind_var_list_len > 1)
1489 gomp_global_icv.max_active_levels_var = gomp_supported_active_levels;
1490 }
f89163fd
JJ
1491 /* Make sure OMP_PLACES and GOMP_CPU_AFFINITY env vars are always
1492 parsed if present in the environment. If OMP_PROC_BIND was set
93d90219 1493 explicitly to false, don't populate places list though. If places
f89163fd
JJ
1494 list was successfully set from OMP_PLACES, only parse but don't process
1495 GOMP_CPU_AFFINITY. If OMP_PROC_BIND was not set in the environment,
1496 default to OMP_PROC_BIND=true if OMP_PLACES or GOMP_CPU_AFFINITY
1497 was successfully parsed into a places list, otherwise to
1498 OMP_PROC_BIND=false. */
1499 if (parse_places_var ("OMP_PLACES", ignore))
1500 {
1501 if (gomp_global_icv.bind_var == omp_proc_bind_false)
1502 gomp_global_icv.bind_var = true;
1503 ignore = true;
1504 }
1505 if (parse_affinity (ignore))
1506 {
1507 if (gomp_global_icv.bind_var == omp_proc_bind_false)
1508 gomp_global_icv.bind_var = true;
1509 ignore = true;
1510 }
1511 if (gomp_global_icv.bind_var != omp_proc_bind_false)
a0884cf0 1512 gomp_init_affinity ();
28567c40
JJ
1513
1514 {
1515 const char *env = getenv ("OMP_AFFINITY_FORMAT");
1516 if (env != NULL)
1517 gomp_set_affinity_format (env, strlen (env));
1518 }
1519
a68ab351
JJ
1520 wait_policy = parse_wait_policy ();
1521 if (!parse_spincount ("GOMP_SPINCOUNT", &gomp_spin_count_var))
1522 {
1523 /* Using a rough estimation of 100000 spins per msec,
1524 use 5 min blocking for OMP_WAIT_POLICY=active,
4c698cf8 1525 3 msec blocking when OMP_WAIT_POLICY is not specificed
a68ab351
JJ
1526 and 0 when OMP_WAIT_POLICY=passive.
1527 Depending on the CPU speed, this can be e.g. 5 times longer
1528 or 5 times shorter. */
1529 if (wait_policy > 0)
1530 gomp_spin_count_var = 30000000000LL;
1531 else if (wait_policy < 0)
4c698cf8 1532 gomp_spin_count_var = 300000LL;
a68ab351
JJ
1533 }
1534 /* gomp_throttled_spin_count_var is used when there are more libgomp
1535 managed threads than available CPUs. Use very short spinning. */
1536 if (wait_policy > 0)
1537 gomp_throttled_spin_count_var = 1000LL;
1538 else if (wait_policy < 0)
1539 gomp_throttled_spin_count_var = 100LL;
1540 if (gomp_throttled_spin_count_var > gomp_spin_count_var)
1541 gomp_throttled_spin_count_var = gomp_spin_count_var;
d0d1b24d
RH
1542
1543 /* Not strictly environment related, but ordering constructors is tricky. */
1544 pthread_attr_init (&gomp_thread_attr);
d0d1b24d 1545
a68ab351 1546 if (parse_stacksize ("OMP_STACKSIZE", &stacksize)
c4060df4
JJ
1547 || parse_stacksize ("GOMP_STACKSIZE", &stacksize)
1548 || GOMP_DEFAULT_STACKSIZE)
d0d1b24d 1549 {
c3b11a40
RH
1550 int err;
1551
c3b11a40
RH
1552 err = pthread_attr_setstacksize (&gomp_thread_attr, stacksize);
1553
1554#ifdef PTHREAD_STACK_MIN
1555 if (err == EINVAL)
d0d1b24d 1556 {
c3b11a40
RH
1557 if (stacksize < PTHREAD_STACK_MIN)
1558 gomp_error ("Stack size less than minimum of %luk",
1559 PTHREAD_STACK_MIN / 1024ul
1560 + (PTHREAD_STACK_MIN % 1024 != 0));
1561 else
d0d1b24d 1562 gomp_error ("Stack size larger than system limit");
d0d1b24d 1563 }
c3b11a40
RH
1564 else
1565#endif
1566 if (err != 0)
1567 gomp_error ("Stack size change failed: %s", strerror (err));
d0d1b24d 1568 }
acf0174b 1569
7123ae24 1570 handle_omp_display_env ();
41dbbb37
TS
1571
1572 /* OpenACC. */
1573
1574 if (!parse_int ("ACC_DEVICE_NUM", &goacc_device_num, true))
1575 goacc_device_num = 0;
1576
1577 parse_acc_device_type ();
ec00d3fa 1578 parse_gomp_openacc_dim ();
41dbbb37
TS
1579
1580 goacc_runtime_initialize ();
5fae049d
TS
1581
1582 goacc_profiling_initialize ();
953ff289 1583}
630e3c3a 1584#endif /* LIBGOMP_OFFLOADED_ONLY */