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"
40 unsigned long gomp_nthreads_var
= 1;
41 bool gomp_dyn_var
= false;
42 bool gomp_nest_var
= false;
43 enum gomp_schedule_type gomp_run_sched_var
= GFS_DYNAMIC
;
44 unsigned long gomp_run_sched_chunk
= 1;
45 unsigned short *gomp_cpu_affinity
;
46 size_t gomp_cpu_affinity_len
;
48 /* Parse the OMP_SCHEDULE environment variable. */
56 env
= getenv ("OMP_SCHEDULE");
60 while (isspace ((unsigned char) *env
))
62 if (strncasecmp (env
, "static", 6) == 0)
64 gomp_run_sched_var
= GFS_STATIC
;
67 else if (strncasecmp (env
, "dynamic", 7) == 0)
69 gomp_run_sched_var
= GFS_DYNAMIC
;
72 else if (strncasecmp (env
, "guided", 6) == 0)
74 gomp_run_sched_var
= GFS_GUIDED
;
80 while (isspace ((unsigned char) *env
))
86 while (isspace ((unsigned char) *env
))
92 value
= strtoul (env
, &end
, 10);
96 while (isspace ((unsigned char) *end
))
101 gomp_run_sched_chunk
= value
;
105 gomp_error ("Unknown value for environment variable OMP_SCHEDULE");
109 gomp_error ("Invalid value for chunk size in "
110 "environment variable OMP_SCHEDULE");
114 /* Parse an unsigned long environment varible. Return true if one was
115 present and it was successfully parsed. */
118 parse_unsigned_long (const char *name
, unsigned long *pvalue
)
127 while (isspace ((unsigned char) *env
))
133 value
= strtoul (env
, &end
, 10);
134 if (errno
|| (long) value
<= 0)
137 while (isspace ((unsigned char) *end
))
146 gomp_error ("Invalid value for environment variable %s", name
);
150 /* Parse a boolean value for environment variable NAME and store the
154 parse_boolean (const char *name
, bool *value
)
162 while (isspace ((unsigned char) *env
))
164 if (strncasecmp (env
, "true", 4) == 0)
169 else if (strncasecmp (env
, "false", 5) == 0)
176 while (isspace ((unsigned char) *env
))
179 gomp_error ("Invalid value for environment variable %s", name
);
182 /* Parse the GOMP_CPU_AFFINITY environment varible. Return true if one was
183 present and it was successfully parsed. */
186 parse_affinity (void)
189 unsigned long cpu_beg
, cpu_end
, cpu_stride
;
190 unsigned short *cpus
= NULL
;
191 size_t allocated
= 0, used
= 0, needed
;
193 env
= getenv ("GOMP_CPU_AFFINITY");
199 while (*env
== ' ' || *env
== '\t')
202 cpu_beg
= strtoul (env
, &end
, 0);
205 if (env
== end
|| cpu_beg
>= 65536)
211 cpu_end
= strtoul (++env
, &end
, 0);
212 if (env
== end
|| cpu_end
>= 65536 || cpu_end
< cpu_beg
)
218 cpu_stride
= strtoul (++env
, &end
, 0);
219 if (env
== end
|| cpu_stride
== 0 || cpu_stride
>= 65536)
226 needed
= (cpu_end
- cpu_beg
) / cpu_stride
+ 1;
227 if (used
+ needed
>= allocated
)
229 unsigned short *new_cpus
;
233 if (allocated
> needed
)
236 allocated
+= 2 * needed
;
237 new_cpus
= realloc (cpus
, allocated
* sizeof (unsigned short));
238 if (new_cpus
== NULL
)
241 gomp_error ("not enough memory to store GOMP_CPU_AFFINITY list");
250 cpus
[used
++] = cpu_beg
;
251 cpu_beg
+= cpu_stride
;
254 while (*env
== ' ' || *env
== '\t')
259 else if (*env
== '\0')
264 gomp_cpu_affinity
= cpus
;
265 gomp_cpu_affinity_len
= used
;
269 gomp_error ("Invalid value for enviroment variable GOMP_CPU_AFFINITY");
273 static void __attribute__((constructor
))
274 initialize_env (void)
276 unsigned long stacksize
;
278 /* Do a compile time check that mkomp_h.pl did good job. */
279 omp_check_defines ();
282 parse_boolean ("OMP_DYNAMIC", &gomp_dyn_var
);
283 parse_boolean ("OMP_NESTED", &gomp_nest_var
);
284 if (!parse_unsigned_long ("OMP_NUM_THREADS", &gomp_nthreads_var
))
285 gomp_init_num_threads ();
286 if (parse_affinity ())
287 gomp_init_affinity ();
289 /* Not strictly environment related, but ordering constructors is tricky. */
290 pthread_attr_init (&gomp_thread_attr
);
291 pthread_attr_setdetachstate (&gomp_thread_attr
, PTHREAD_CREATE_DETACHED
);
293 if (parse_unsigned_long ("GOMP_STACKSIZE", &stacksize
))
298 err
= pthread_attr_setstacksize (&gomp_thread_attr
, stacksize
);
300 #ifdef PTHREAD_STACK_MIN
303 if (stacksize
< PTHREAD_STACK_MIN
)
304 gomp_error ("Stack size less than minimum of %luk",
305 PTHREAD_STACK_MIN
/ 1024ul
306 + (PTHREAD_STACK_MIN
% 1024 != 0));
308 gomp_error ("Stack size larger than system limit");
313 gomp_error ("Stack size change failed: %s", strerror (err
));
318 /* The public OpenMP API routines that access these variables. */
321 omp_set_num_threads (int n
)
323 gomp_nthreads_var
= (n
> 0 ? n
: 1);
327 omp_set_dynamic (int val
)
333 omp_get_dynamic (void)
339 omp_set_nested (int val
)
345 omp_get_nested (void)
347 return gomp_nest_var
;
350 ialias (omp_set_dynamic
)
351 ialias (omp_set_nested
)
352 ialias (omp_set_num_threads
)
353 ialias (omp_get_dynamic
)
354 ialias (omp_get_nested
)