1*404b540aSrobert /* Copyright (C) 2005, 2006 Free Software Foundation, Inc.
2*404b540aSrobert Contributed by Richard Henderson <rth@redhat.com>.
3*404b540aSrobert
4*404b540aSrobert This file is part of the GNU OpenMP Library (libgomp).
5*404b540aSrobert
6*404b540aSrobert Libgomp is free software; you can redistribute it and/or modify it
7*404b540aSrobert under the terms of the GNU Lesser General Public License as published by
8*404b540aSrobert the Free Software Foundation; either version 2.1 of the License, or
9*404b540aSrobert (at your option) any later version.
10*404b540aSrobert
11*404b540aSrobert Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
12*404b540aSrobert WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13*404b540aSrobert FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
14*404b540aSrobert more details.
15*404b540aSrobert
16*404b540aSrobert You should have received a copy of the GNU Lesser General Public License
17*404b540aSrobert along with libgomp; see the file COPYING.LIB. If not, write to the
18*404b540aSrobert Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19*404b540aSrobert MA 02110-1301, USA. */
20*404b540aSrobert
21*404b540aSrobert /* As a special exception, if you link this library with other files, some
22*404b540aSrobert of which are compiled with GCC, to produce an executable, this library
23*404b540aSrobert does not by itself cause the resulting executable to be covered by the
24*404b540aSrobert GNU General Public License. This exception does not however invalidate
25*404b540aSrobert any other reasons why the executable file might be covered by the GNU
26*404b540aSrobert General Public License. */
27*404b540aSrobert
28*404b540aSrobert /* This file defines the OpenMP internal control variables, and arranges
29*404b540aSrobert for them to be initialized from environment variables at startup. */
30*404b540aSrobert
31*404b540aSrobert #include "libgomp.h"
32*404b540aSrobert #include "libgomp_f.h"
33*404b540aSrobert #include <ctype.h>
34*404b540aSrobert #include <stdlib.h>
35*404b540aSrobert #include <string.h>
36*404b540aSrobert #include <limits.h>
37*404b540aSrobert #include <errno.h>
38*404b540aSrobert
39*404b540aSrobert
40*404b540aSrobert unsigned long gomp_nthreads_var = 1;
41*404b540aSrobert bool gomp_dyn_var = false;
42*404b540aSrobert bool gomp_nest_var = false;
43*404b540aSrobert enum gomp_schedule_type gomp_run_sched_var = GFS_DYNAMIC;
44*404b540aSrobert unsigned long gomp_run_sched_chunk = 1;
45*404b540aSrobert
46*404b540aSrobert /* Parse the OMP_SCHEDULE environment variable. */
47*404b540aSrobert
48*404b540aSrobert static void
parse_schedule(void)49*404b540aSrobert parse_schedule (void)
50*404b540aSrobert {
51*404b540aSrobert char *env, *end;
52*404b540aSrobert unsigned long value;
53*404b540aSrobert
54*404b540aSrobert env = getenv ("OMP_SCHEDULE");
55*404b540aSrobert if (env == NULL)
56*404b540aSrobert return;
57*404b540aSrobert
58*404b540aSrobert while (isspace ((unsigned char) *env))
59*404b540aSrobert ++env;
60*404b540aSrobert if (strncasecmp (env, "static", 6) == 0)
61*404b540aSrobert {
62*404b540aSrobert gomp_run_sched_var = GFS_STATIC;
63*404b540aSrobert env += 6;
64*404b540aSrobert }
65*404b540aSrobert else if (strncasecmp (env, "dynamic", 7) == 0)
66*404b540aSrobert {
67*404b540aSrobert gomp_run_sched_var = GFS_DYNAMIC;
68*404b540aSrobert env += 7;
69*404b540aSrobert }
70*404b540aSrobert else if (strncasecmp (env, "guided", 6) == 0)
71*404b540aSrobert {
72*404b540aSrobert gomp_run_sched_var = GFS_GUIDED;
73*404b540aSrobert env += 6;
74*404b540aSrobert }
75*404b540aSrobert else
76*404b540aSrobert goto unknown;
77*404b540aSrobert
78*404b540aSrobert while (isspace ((unsigned char) *env))
79*404b540aSrobert ++env;
80*404b540aSrobert if (*env == '\0')
81*404b540aSrobert return;
82*404b540aSrobert if (*env++ != ',')
83*404b540aSrobert goto unknown;
84*404b540aSrobert while (isspace ((unsigned char) *env))
85*404b540aSrobert ++env;
86*404b540aSrobert if (*env == '\0')
87*404b540aSrobert goto invalid;
88*404b540aSrobert
89*404b540aSrobert errno = 0;
90*404b540aSrobert value = strtoul (env, &end, 10);
91*404b540aSrobert if (errno)
92*404b540aSrobert goto invalid;
93*404b540aSrobert
94*404b540aSrobert while (isspace ((unsigned char) *end))
95*404b540aSrobert ++end;
96*404b540aSrobert if (*end != '\0')
97*404b540aSrobert goto invalid;
98*404b540aSrobert
99*404b540aSrobert gomp_run_sched_chunk = value;
100*404b540aSrobert return;
101*404b540aSrobert
102*404b540aSrobert unknown:
103*404b540aSrobert gomp_error ("Unknown value for environment variable OMP_SCHEDULE");
104*404b540aSrobert return;
105*404b540aSrobert
106*404b540aSrobert invalid:
107*404b540aSrobert gomp_error ("Invalid value for chunk size in "
108*404b540aSrobert "environment variable OMP_SCHEDULE");
109*404b540aSrobert return;
110*404b540aSrobert }
111*404b540aSrobert
112*404b540aSrobert /* Parse an unsigned long environment varible. Return true if one was
113*404b540aSrobert present and it was successfully parsed. */
114*404b540aSrobert
115*404b540aSrobert static bool
parse_unsigned_long(const char * name,unsigned long * pvalue)116*404b540aSrobert parse_unsigned_long (const char *name, unsigned long *pvalue)
117*404b540aSrobert {
118*404b540aSrobert char *env, *end;
119*404b540aSrobert unsigned long value;
120*404b540aSrobert
121*404b540aSrobert env = getenv (name);
122*404b540aSrobert if (env == NULL)
123*404b540aSrobert return false;
124*404b540aSrobert
125*404b540aSrobert while (isspace ((unsigned char) *env))
126*404b540aSrobert ++env;
127*404b540aSrobert if (*env == '\0')
128*404b540aSrobert goto invalid;
129*404b540aSrobert
130*404b540aSrobert errno = 0;
131*404b540aSrobert value = strtoul (env, &end, 10);
132*404b540aSrobert if (errno || (long) value <= 0)
133*404b540aSrobert goto invalid;
134*404b540aSrobert
135*404b540aSrobert while (isspace ((unsigned char) *end))
136*404b540aSrobert ++end;
137*404b540aSrobert if (*end != '\0')
138*404b540aSrobert goto invalid;
139*404b540aSrobert
140*404b540aSrobert *pvalue = value;
141*404b540aSrobert return true;
142*404b540aSrobert
143*404b540aSrobert invalid:
144*404b540aSrobert gomp_error ("Invalid value for environment variable %s", name);
145*404b540aSrobert return false;
146*404b540aSrobert }
147*404b540aSrobert
148*404b540aSrobert /* Parse a boolean value for environment variable NAME and store the
149*404b540aSrobert result in VALUE. */
150*404b540aSrobert
151*404b540aSrobert static void
parse_boolean(const char * name,bool * value)152*404b540aSrobert parse_boolean (const char *name, bool *value)
153*404b540aSrobert {
154*404b540aSrobert const char *env;
155*404b540aSrobert
156*404b540aSrobert env = getenv (name);
157*404b540aSrobert if (env == NULL)
158*404b540aSrobert return;
159*404b540aSrobert
160*404b540aSrobert while (isspace ((unsigned char) *env))
161*404b540aSrobert ++env;
162*404b540aSrobert if (strncasecmp (env, "true", 4) == 0)
163*404b540aSrobert {
164*404b540aSrobert *value = true;
165*404b540aSrobert env += 4;
166*404b540aSrobert }
167*404b540aSrobert else if (strncasecmp (env, "false", 5) == 0)
168*404b540aSrobert {
169*404b540aSrobert *value = false;
170*404b540aSrobert env += 5;
171*404b540aSrobert }
172*404b540aSrobert else
173*404b540aSrobert env = "X";
174*404b540aSrobert while (isspace ((unsigned char) *env))
175*404b540aSrobert ++env;
176*404b540aSrobert if (*env != '\0')
177*404b540aSrobert gomp_error ("Invalid value for environment variable %s", name);
178*404b540aSrobert }
179*404b540aSrobert
180*404b540aSrobert static void __attribute__((constructor))
initialize_env(void)181*404b540aSrobert initialize_env (void)
182*404b540aSrobert {
183*404b540aSrobert unsigned long stacksize;
184*404b540aSrobert
185*404b540aSrobert /* Do a compile time check that mkomp_h.pl did good job. */
186*404b540aSrobert omp_check_defines ();
187*404b540aSrobert
188*404b540aSrobert parse_schedule ();
189*404b540aSrobert parse_boolean ("OMP_DYNAMIC", &gomp_dyn_var);
190*404b540aSrobert parse_boolean ("OMP_NESTED", &gomp_nest_var);
191*404b540aSrobert if (!parse_unsigned_long ("OMP_NUM_THREADS", &gomp_nthreads_var))
192*404b540aSrobert gomp_init_num_threads ();
193*404b540aSrobert
194*404b540aSrobert /* Not strictly environment related, but ordering constructors is tricky. */
195*404b540aSrobert pthread_attr_init (&gomp_thread_attr);
196*404b540aSrobert pthread_attr_setdetachstate (&gomp_thread_attr, PTHREAD_CREATE_DETACHED);
197*404b540aSrobert
198*404b540aSrobert if (parse_unsigned_long ("GOMP_STACKSIZE", &stacksize))
199*404b540aSrobert {
200*404b540aSrobert int err;
201*404b540aSrobert
202*404b540aSrobert stacksize *= 1024;
203*404b540aSrobert err = pthread_attr_setstacksize (&gomp_thread_attr, stacksize);
204*404b540aSrobert
205*404b540aSrobert #ifdef PTHREAD_STACK_MIN
206*404b540aSrobert if (err == EINVAL)
207*404b540aSrobert {
208*404b540aSrobert if (stacksize < PTHREAD_STACK_MIN)
209*404b540aSrobert gomp_error ("Stack size less than minimum of %luk",
210*404b540aSrobert PTHREAD_STACK_MIN / 1024ul
211*404b540aSrobert + (PTHREAD_STACK_MIN % 1024 != 0));
212*404b540aSrobert else
213*404b540aSrobert gomp_error ("Stack size larger than system limit");
214*404b540aSrobert }
215*404b540aSrobert else
216*404b540aSrobert #endif
217*404b540aSrobert if (err != 0)
218*404b540aSrobert gomp_error ("Stack size change failed: %s", strerror (err));
219*404b540aSrobert }
220*404b540aSrobert }
221*404b540aSrobert
222*404b540aSrobert
223*404b540aSrobert /* The public OpenMP API routines that access these variables. */
224*404b540aSrobert
225*404b540aSrobert void
omp_set_num_threads(int n)226*404b540aSrobert omp_set_num_threads (int n)
227*404b540aSrobert {
228*404b540aSrobert gomp_nthreads_var = (n > 0 ? n : 1);
229*404b540aSrobert }
230*404b540aSrobert
231*404b540aSrobert void
omp_set_dynamic(int val)232*404b540aSrobert omp_set_dynamic (int val)
233*404b540aSrobert {
234*404b540aSrobert gomp_dyn_var = val;
235*404b540aSrobert }
236*404b540aSrobert
237*404b540aSrobert int
omp_get_dynamic(void)238*404b540aSrobert omp_get_dynamic (void)
239*404b540aSrobert {
240*404b540aSrobert return gomp_dyn_var;
241*404b540aSrobert }
242*404b540aSrobert
243*404b540aSrobert void
omp_set_nested(int val)244*404b540aSrobert omp_set_nested (int val)
245*404b540aSrobert {
246*404b540aSrobert gomp_nest_var = val;
247*404b540aSrobert }
248*404b540aSrobert
249*404b540aSrobert int
omp_get_nested(void)250*404b540aSrobert omp_get_nested (void)
251*404b540aSrobert {
252*404b540aSrobert return gomp_nest_var;
253*404b540aSrobert }
254*404b540aSrobert
255*404b540aSrobert ialias (omp_set_dynamic)
256*404b540aSrobert ialias (omp_set_nested)
257*404b540aSrobert ialias (omp_set_num_threads)
258*404b540aSrobert ialias (omp_get_dynamic)
259*404b540aSrobert ialias (omp_get_nested)
260