200x-xx-xx Kaz Kylheku <kaz@zeugmasystems.com>
[official-gcc.git] / libgomp / env.c
blob4a07bfa4975a4f319e318750f6afc2c08facca55
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
14 more details.
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. */
31 #include "libgomp.h"
32 #include "libgomp_f.h"
33 #include <ctype.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <limits.h>
37 #include <errno.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. */
50 static void
51 parse_schedule (void)
53 char *env, *end;
54 unsigned long value;
56 env = getenv ("OMP_SCHEDULE");
57 if (env == NULL)
58 return;
60 while (isspace ((unsigned char) *env))
61 ++env;
62 if (strncasecmp (env, "static", 6) == 0)
64 gomp_run_sched_var = GFS_STATIC;
65 env += 6;
67 else if (strncasecmp (env, "dynamic", 7) == 0)
69 gomp_run_sched_var = GFS_DYNAMIC;
70 env += 7;
72 else if (strncasecmp (env, "guided", 6) == 0)
74 gomp_run_sched_var = GFS_GUIDED;
75 env += 6;
77 else
78 goto unknown;
80 while (isspace ((unsigned char) *env))
81 ++env;
82 if (*env == '\0')
83 return;
84 if (*env++ != ',')
85 goto unknown;
86 while (isspace ((unsigned char) *env))
87 ++env;
88 if (*env == '\0')
89 goto invalid;
91 errno = 0;
92 value = strtoul (env, &end, 10);
93 if (errno)
94 goto invalid;
96 while (isspace ((unsigned char) *end))
97 ++end;
98 if (*end != '\0')
99 goto invalid;
101 gomp_run_sched_chunk = value;
102 return;
104 unknown:
105 gomp_error ("Unknown value for environment variable OMP_SCHEDULE");
106 return;
108 invalid:
109 gomp_error ("Invalid value for chunk size in "
110 "environment variable OMP_SCHEDULE");
111 return;
114 /* Parse an unsigned long environment varible. Return true if one was
115 present and it was successfully parsed. */
117 static bool
118 parse_unsigned_long (const char *name, unsigned long *pvalue)
120 char *env, *end;
121 unsigned long value;
123 env = getenv (name);
124 if (env == NULL)
125 return false;
127 while (isspace ((unsigned char) *env))
128 ++env;
129 if (*env == '\0')
130 goto invalid;
132 errno = 0;
133 value = strtoul (env, &end, 10);
134 if (errno || (long) value <= 0)
135 goto invalid;
137 while (isspace ((unsigned char) *end))
138 ++end;
139 if (*end != '\0')
140 goto invalid;
142 *pvalue = value;
143 return true;
145 invalid:
146 gomp_error ("Invalid value for environment variable %s", name);
147 return false;
150 /* Parse a boolean value for environment variable NAME and store the
151 result in VALUE. */
153 static void
154 parse_boolean (const char *name, bool *value)
156 const char *env;
158 env = getenv (name);
159 if (env == NULL)
160 return;
162 while (isspace ((unsigned char) *env))
163 ++env;
164 if (strncasecmp (env, "true", 4) == 0)
166 *value = true;
167 env += 4;
169 else if (strncasecmp (env, "false", 5) == 0)
171 *value = false;
172 env += 5;
174 else
175 env = "X";
176 while (isspace ((unsigned char) *env))
177 ++env;
178 if (*env != '\0')
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. */
185 static bool
186 parse_affinity (void)
188 char *env, *end;
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");
194 if (env == NULL)
195 return false;
199 while (*env == ' ' || *env == '\t')
200 env++;
202 cpu_beg = strtoul (env, &end, 0);
203 cpu_end = cpu_beg;
204 cpu_stride = 1;
205 if (env == end || cpu_beg >= 65536)
206 goto invalid;
208 env = end;
209 if (*env == '-')
211 cpu_end = strtoul (++env, &end, 0);
212 if (env == end || cpu_end >= 65536 || cpu_end < cpu_beg)
213 goto invalid;
215 env = end;
216 if (*env == ':')
218 cpu_stride = strtoul (++env, &end, 0);
219 if (env == end || cpu_stride == 0 || cpu_stride >= 65536)
220 goto invalid;
222 env = end;
226 needed = (cpu_end - cpu_beg) / cpu_stride + 1;
227 if (used + needed >= allocated)
229 unsigned short *new_cpus;
231 if (allocated < 64)
232 allocated = 64;
233 if (allocated > needed)
234 allocated <<= 1;
235 else
236 allocated += 2 * needed;
237 new_cpus = realloc (cpus, allocated * sizeof (unsigned short));
238 if (new_cpus == NULL)
240 free (cpus);
241 gomp_error ("not enough memory to store GOMP_CPU_AFFINITY list");
242 return false;
245 cpus = new_cpus;
248 while (needed--)
250 cpus[used++] = cpu_beg;
251 cpu_beg += cpu_stride;
254 while (*env == ' ' || *env == '\t')
255 env++;
257 if (*env == ',')
258 env++;
259 else if (*env == '\0')
260 break;
262 while (1);
264 gomp_cpu_affinity = cpus;
265 gomp_cpu_affinity_len = used;
266 return true;
268 invalid:
269 gomp_error ("Invalid value for enviroment variable GOMP_CPU_AFFINITY");
270 return false;
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 ();
281 parse_schedule ();
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))
295 int err;
297 stacksize *= 1024;
298 err = pthread_attr_setstacksize (&gomp_thread_attr, stacksize);
300 #ifdef PTHREAD_STACK_MIN
301 if (err == EINVAL)
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));
307 else
308 gomp_error ("Stack size larger than system limit");
310 else
311 #endif
312 if (err != 0)
313 gomp_error ("Stack size change failed: %s", strerror (err));
318 /* The public OpenMP API routines that access these variables. */
320 void
321 omp_set_num_threads (int n)
323 gomp_nthreads_var = (n > 0 ? n : 1);
326 void
327 omp_set_dynamic (int val)
329 gomp_dyn_var = val;
333 omp_get_dynamic (void)
335 return gomp_dyn_var;
338 void
339 omp_set_nested (int val)
341 gomp_nest_var = 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)