1 /* Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
2 Contributed by Richard Henderson <rth@redhat.com>.
4 This file is part of the GNU OpenMP Library (libgomp).
6 Libgomp is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
16 You should have received a copy of the GNU Lesser General Public License
17 along with libgomp; see the file COPYING.LIB. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 MA 02110-1301, USA. */
21 /* As a special exception, if you link this library with other files, some
22 of which are compiled with GCC, to produce an executable, this library
23 does not by itself cause the resulting executable to be covered by the
24 GNU General Public License. This exception does not however invalidate
25 any other reasons why the executable file might be covered by the GNU
26 General Public License. */
28 /* This file defines the OpenMP internal control variables, and arranges
29 for them to be initialized from environment variables at startup. */
32 #include "libgomp_f.h"
35 #ifdef STRING_WITH_STRINGS
42 # ifdef HAVE_STRINGS_H
51 unsigned long gomp_nthreads_var
= 1;
52 bool gomp_dyn_var
= false;
53 bool gomp_nest_var
= false;
54 enum gomp_schedule_type gomp_run_sched_var
= GFS_DYNAMIC
;
55 unsigned long gomp_run_sched_chunk
= 1;
56 unsigned short *gomp_cpu_affinity
;
57 size_t gomp_cpu_affinity_len
;
59 /* Parse the OMP_SCHEDULE environment variable. */
67 env
= getenv ("OMP_SCHEDULE");
71 while (isspace ((unsigned char) *env
))
73 if (strncasecmp (env
, "static", 6) == 0)
75 gomp_run_sched_var
= GFS_STATIC
;
78 else if (strncasecmp (env
, "dynamic", 7) == 0)
80 gomp_run_sched_var
= GFS_DYNAMIC
;
83 else if (strncasecmp (env
, "guided", 6) == 0)
85 gomp_run_sched_var
= GFS_GUIDED
;
91 while (isspace ((unsigned char) *env
))
97 while (isspace ((unsigned char) *env
))
103 value
= strtoul (env
, &end
, 10);
107 while (isspace ((unsigned char) *end
))
112 gomp_run_sched_chunk
= value
;
116 gomp_error ("Unknown value for environment variable OMP_SCHEDULE");
120 gomp_error ("Invalid value for chunk size in "
121 "environment variable OMP_SCHEDULE");
125 /* Parse an unsigned long environment varible. Return true if one was
126 present and it was successfully parsed. */
129 parse_unsigned_long (const char *name
, unsigned long *pvalue
)
138 while (isspace ((unsigned char) *env
))
144 value
= strtoul (env
, &end
, 10);
145 if (errno
|| (long) value
<= 0)
148 while (isspace ((unsigned char) *end
))
157 gomp_error ("Invalid value for environment variable %s", name
);
161 /* Parse a boolean value for environment variable NAME and store the
165 parse_boolean (const char *name
, bool *value
)
173 while (isspace ((unsigned char) *env
))
175 if (strncasecmp (env
, "true", 4) == 0)
180 else if (strncasecmp (env
, "false", 5) == 0)
187 while (isspace ((unsigned char) *env
))
190 gomp_error ("Invalid value for environment variable %s", name
);
193 /* Parse the GOMP_CPU_AFFINITY environment varible. Return true if one was
194 present and it was successfully parsed. */
197 parse_affinity (void)
200 unsigned long cpu_beg
, cpu_end
, cpu_stride
;
201 unsigned short *cpus
= NULL
;
202 size_t allocated
= 0, used
= 0, needed
;
204 env
= getenv ("GOMP_CPU_AFFINITY");
210 while (*env
== ' ' || *env
== '\t')
213 cpu_beg
= strtoul (env
, &end
, 0);
216 if (env
== end
|| cpu_beg
>= 65536)
222 cpu_end
= strtoul (++env
, &end
, 0);
223 if (env
== end
|| cpu_end
>= 65536 || cpu_end
< cpu_beg
)
229 cpu_stride
= strtoul (++env
, &end
, 0);
230 if (env
== end
|| cpu_stride
== 0 || cpu_stride
>= 65536)
237 needed
= (cpu_end
- cpu_beg
) / cpu_stride
+ 1;
238 if (used
+ needed
>= allocated
)
240 unsigned short *new_cpus
;
244 if (allocated
> needed
)
247 allocated
+= 2 * needed
;
248 new_cpus
= realloc (cpus
, allocated
* sizeof (unsigned short));
249 if (new_cpus
== NULL
)
252 gomp_error ("not enough memory to store GOMP_CPU_AFFINITY list");
261 cpus
[used
++] = cpu_beg
;
262 cpu_beg
+= cpu_stride
;
265 while (*env
== ' ' || *env
== '\t')
270 else if (*env
== '\0')
275 gomp_cpu_affinity
= cpus
;
276 gomp_cpu_affinity_len
= used
;
280 gomp_error ("Invalid value for enviroment variable GOMP_CPU_AFFINITY");
284 static void __attribute__((constructor
))
285 initialize_env (void)
287 unsigned long stacksize
;
289 /* Do a compile time check that mkomp_h.pl did good job. */
290 omp_check_defines ();
293 parse_boolean ("OMP_DYNAMIC", &gomp_dyn_var
);
294 parse_boolean ("OMP_NESTED", &gomp_nest_var
);
295 if (!parse_unsigned_long ("OMP_NUM_THREADS", &gomp_nthreads_var
))
296 gomp_init_num_threads ();
297 if (parse_affinity ())
298 gomp_init_affinity ();
300 /* Not strictly environment related, but ordering constructors is tricky. */
301 pthread_attr_init (&gomp_thread_attr
);
302 pthread_attr_setdetachstate (&gomp_thread_attr
, PTHREAD_CREATE_DETACHED
);
304 if (parse_unsigned_long ("GOMP_STACKSIZE", &stacksize
))
309 err
= pthread_attr_setstacksize (&gomp_thread_attr
, stacksize
);
311 #ifdef PTHREAD_STACK_MIN
314 if (stacksize
< PTHREAD_STACK_MIN
)
315 gomp_error ("Stack size less than minimum of %luk",
316 PTHREAD_STACK_MIN
/ 1024ul
317 + (PTHREAD_STACK_MIN
% 1024 != 0));
319 gomp_error ("Stack size larger than system limit");
324 gomp_error ("Stack size change failed: %s", strerror (err
));
329 /* The public OpenMP API routines that access these variables. */
332 omp_set_num_threads (int n
)
334 gomp_nthreads_var
= (n
> 0 ? n
: 1);
338 omp_set_dynamic (int val
)
344 omp_get_dynamic (void)
350 omp_set_nested (int val
)
356 omp_get_nested (void)
358 return gomp_nest_var
;
361 ialias (omp_set_dynamic
)
362 ialias (omp_set_nested
)
363 ialias (omp_set_num_threads
)
364 ialias (omp_get_dynamic
)
365 ialias (omp_get_nested
)