1 /* Copyright (C) 2005, 2006 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;
46 /* Parse the OMP_SCHEDULE environment variable. */
53 env
= getenv ("OMP_SCHEDULE");
57 while (isspace ((unsigned char) *env
))
59 if (strncasecmp (env
, "static", 6) == 0)
61 gomp_run_sched_var
= GFS_STATIC
;
64 else if (strncasecmp (env
, "dynamic", 7) == 0)
66 gomp_run_sched_var
= GFS_DYNAMIC
;
69 else if (strncasecmp (env
, "guided", 6) == 0)
71 gomp_run_sched_var
= GFS_GUIDED
;
77 while (isspace ((unsigned char) *env
))
83 while (isspace ((unsigned char) *env
))
88 gomp_run_sched_chunk
= strtoul (env
, &end
, 10);
89 while (isspace ((unsigned char) *end
))
96 gomp_error ("Unknown value for environment variable OMP_SCHEDULE");
100 gomp_error ("Invalid value for chunk size in "
101 "environment variable OMP_SCHEDULE");
102 gomp_run_sched_chunk
= 1;
106 /* Parse an unsigned long environment varible. Return true if one was
107 present and it was successfully parsed. */
110 parse_unsigned_long (const char *name
, unsigned long *pvalue
)
119 while (isspace ((unsigned char) *env
))
124 value
= strtoul (env
, &end
, 10);
125 while (isspace ((unsigned char) *end
))
134 gomp_error ("Invalid value for environment variable %s", name
);
138 /* Parse a boolean value for environment variable NAME and store the
142 parse_boolean (const char *name
, bool *value
)
150 while (isspace ((unsigned char) *env
))
152 if (strncasecmp (env
, "true", 4) == 0)
157 else if (strncasecmp (env
, "false", 5) == 0)
164 while (isspace ((unsigned char) *env
))
167 gomp_error ("Invalid value for environment variable %s", name
);
170 static void __attribute__((constructor
))
171 initialize_env (void)
173 unsigned long stacksize
;
175 /* Do a compile time check that mkomp_h.pl did good job. */
176 omp_check_defines ();
179 parse_boolean ("OMP_DYNAMIC", &gomp_dyn_var
);
180 parse_boolean ("OMP_NESTED", &gomp_nest_var
);
181 if (!parse_unsigned_long ("OMP_NUM_THREADS", &gomp_nthreads_var
))
182 gomp_init_num_threads ();
184 /* Not strictly environment related, but ordering constructors is tricky. */
185 pthread_attr_init (&gomp_thread_attr
);
186 pthread_attr_setdetachstate (&gomp_thread_attr
, PTHREAD_CREATE_DETACHED
);
188 if (parse_unsigned_long ("GOMP_STACKSIZE", &stacksize
))
193 err
= pthread_attr_setstacksize (&gomp_thread_attr
, stacksize
);
195 #ifdef PTHREAD_STACK_MIN
198 if (stacksize
< PTHREAD_STACK_MIN
)
199 gomp_error ("Stack size less than minimum of %luk",
200 PTHREAD_STACK_MIN
/ 1024ul
201 + (PTHREAD_STACK_MIN
% 1024 != 0));
203 gomp_error ("Stack size larger than system limit");
208 gomp_error ("Stack size change failed: %s", strerror (err
));
213 /* The public OpenMP API routines that access these variables. */
216 omp_set_num_threads (int n
)
218 gomp_nthreads_var
= n
;
222 omp_set_dynamic (int val
)
228 omp_get_dynamic (void)
234 omp_set_nested (int val
)
240 omp_get_nested (void)
242 return gomp_nest_var
;
245 ialias (omp_set_dynamic
)
246 ialias (omp_set_nested
)
247 ialias (omp_set_num_threads
)
248 ialias (omp_get_dynamic
)
249 ialias (omp_get_nested
)