]>
Commit | Line | Data |
---|---|---|
89b3e3cd | 1 | /* Copyright (C) 2005, 2006 Free Software Foundation, Inc. |
953ff289 DN |
2 | Contributed by Richard Henderson <rth@redhat.com>. |
3 | ||
4 | This file is part of the GNU OpenMP Library (libgomp). | |
5 | ||
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. | |
10 | ||
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. | |
15 | ||
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. */ | |
20 | ||
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. */ | |
27 | ||
28 | /* This file defines the OpenMP internal control variables, and arranges | |
29 | for them to be initialized from environment variables at startup. */ | |
30 | ||
31 | #include "libgomp.h" | |
32 | #include "libgomp_f.h" | |
89b3e3cd | 33 | #include <ctype.h> |
953ff289 DN |
34 | #include <stdlib.h> |
35 | #include <string.h> | |
d0d1b24d RH |
36 | #include <limits.h> |
37 | #include <errno.h> | |
953ff289 DN |
38 | |
39 | ||
d0d1b24d | 40 | unsigned long gomp_nthreads_var = 1; |
953ff289 DN |
41 | bool gomp_dyn_var = false; |
42 | bool gomp_nest_var = false; | |
43 | enum gomp_schedule_type gomp_run_sched_var = GFS_DYNAMIC; | |
d0d1b24d | 44 | unsigned long gomp_run_sched_chunk = 1; |
953ff289 DN |
45 | |
46 | /* Parse the OMP_SCHEDULE environment variable. */ | |
47 | ||
48 | static void | |
49 | parse_schedule (void) | |
50 | { | |
51 | char *env, *end; | |
52 | ||
53 | env = getenv ("OMP_SCHEDULE"); | |
54 | if (env == NULL) | |
55 | return; | |
56 | ||
89b3e3cd JJ |
57 | while (isspace ((unsigned char) *env)) |
58 | ++env; | |
59 | if (strncasecmp (env, "static", 6) == 0) | |
953ff289 DN |
60 | { |
61 | gomp_run_sched_var = GFS_STATIC; | |
62 | env += 6; | |
63 | } | |
89b3e3cd | 64 | else if (strncasecmp (env, "dynamic", 7) == 0) |
953ff289 DN |
65 | { |
66 | gomp_run_sched_var = GFS_DYNAMIC; | |
67 | env += 7; | |
68 | } | |
89b3e3cd | 69 | else if (strncasecmp (env, "guided", 6) == 0) |
953ff289 DN |
70 | { |
71 | gomp_run_sched_var = GFS_GUIDED; | |
72 | env += 6; | |
73 | } | |
74 | else | |
75 | goto unknown; | |
76 | ||
89b3e3cd JJ |
77 | while (isspace ((unsigned char) *env)) |
78 | ++env; | |
953ff289 DN |
79 | if (*env == '\0') |
80 | return; | |
89b3e3cd | 81 | if (*env++ != ',') |
953ff289 | 82 | goto unknown; |
89b3e3cd JJ |
83 | while (isspace ((unsigned char) *env)) |
84 | ++env; | |
953ff289 | 85 | if (*env == '\0') |
953ff289 DN |
86 | goto invalid; |
87 | ||
88 | gomp_run_sched_chunk = strtoul (env, &end, 10); | |
89b3e3cd JJ |
89 | while (isspace ((unsigned char) *end)) |
90 | ++end; | |
953ff289 DN |
91 | if (*end != '\0') |
92 | goto invalid; | |
93 | return; | |
94 | ||
95 | unknown: | |
96 | gomp_error ("Unknown value for environment variable OMP_SCHEDULE"); | |
97 | return; | |
98 | ||
99 | invalid: | |
100 | gomp_error ("Invalid value for chunk size in " | |
101 | "environment variable OMP_SCHEDULE"); | |
102 | gomp_run_sched_chunk = 1; | |
103 | return; | |
104 | } | |
105 | ||
d0d1b24d | 106 | /* Parse an unsigned long environment varible. Return true if one was |
953ff289 DN |
107 | present and it was successfully parsed. */ |
108 | ||
109 | static bool | |
d0d1b24d | 110 | parse_unsigned_long (const char *name, unsigned long *pvalue) |
953ff289 DN |
111 | { |
112 | char *env, *end; | |
d0d1b24d | 113 | unsigned long value; |
953ff289 | 114 | |
d0d1b24d | 115 | env = getenv (name); |
953ff289 DN |
116 | if (env == NULL) |
117 | return false; | |
118 | ||
89b3e3cd JJ |
119 | while (isspace ((unsigned char) *env)) |
120 | ++env; | |
953ff289 DN |
121 | if (*env == '\0') |
122 | goto invalid; | |
123 | ||
d0d1b24d | 124 | value = strtoul (env, &end, 10); |
89b3e3cd JJ |
125 | while (isspace ((unsigned char) *end)) |
126 | ++end; | |
953ff289 DN |
127 | if (*end != '\0') |
128 | goto invalid; | |
d0d1b24d RH |
129 | |
130 | *pvalue = value; | |
953ff289 DN |
131 | return true; |
132 | ||
133 | invalid: | |
d0d1b24d | 134 | gomp_error ("Invalid value for environment variable %s", name); |
953ff289 DN |
135 | return false; |
136 | } | |
137 | ||
d0d1b24d | 138 | /* Parse a boolean value for environment variable NAME and store the |
953ff289 DN |
139 | result in VALUE. */ |
140 | ||
141 | static void | |
142 | parse_boolean (const char *name, bool *value) | |
143 | { | |
144 | const char *env; | |
145 | ||
146 | env = getenv (name); | |
147 | if (env == NULL) | |
148 | return; | |
149 | ||
89b3e3cd JJ |
150 | while (isspace ((unsigned char) *env)) |
151 | ++env; | |
152 | if (strncasecmp (env, "true", 4) == 0) | |
153 | { | |
154 | *value = true; | |
155 | env += 4; | |
156 | } | |
157 | else if (strncasecmp (env, "false", 5) == 0) | |
158 | { | |
159 | *value = false; | |
160 | env += 5; | |
161 | } | |
953ff289 | 162 | else |
89b3e3cd JJ |
163 | env = "X"; |
164 | while (isspace ((unsigned char) *env)) | |
165 | ++env; | |
166 | if (*env != '\0') | |
d0d1b24d | 167 | gomp_error ("Invalid value for environment variable %s", name); |
953ff289 DN |
168 | } |
169 | ||
170 | static void __attribute__((constructor)) | |
171 | initialize_env (void) | |
172 | { | |
d0d1b24d RH |
173 | unsigned long stacksize; |
174 | ||
953ff289 DN |
175 | /* Do a compile time check that mkomp_h.pl did good job. */ |
176 | omp_check_defines (); | |
177 | ||
178 | parse_schedule (); | |
179 | parse_boolean ("OMP_DYNAMIC", &gomp_dyn_var); | |
180 | parse_boolean ("OMP_NESTED", &gomp_nest_var); | |
d0d1b24d | 181 | if (!parse_unsigned_long ("OMP_NUM_THREADS", &gomp_nthreads_var)) |
953ff289 | 182 | gomp_init_num_threads (); |
d0d1b24d RH |
183 | |
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); | |
187 | ||
c3b11a40 | 188 | if (parse_unsigned_long ("GOMP_STACKSIZE", &stacksize)) |
d0d1b24d | 189 | { |
c3b11a40 RH |
190 | int err; |
191 | ||
d0d1b24d | 192 | stacksize *= 1024; |
c3b11a40 RH |
193 | err = pthread_attr_setstacksize (&gomp_thread_attr, stacksize); |
194 | ||
195 | #ifdef PTHREAD_STACK_MIN | |
196 | if (err == EINVAL) | |
d0d1b24d | 197 | { |
c3b11a40 RH |
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)); | |
202 | else | |
d0d1b24d | 203 | gomp_error ("Stack size larger than system limit"); |
d0d1b24d | 204 | } |
c3b11a40 RH |
205 | else |
206 | #endif | |
207 | if (err != 0) | |
208 | gomp_error ("Stack size change failed: %s", strerror (err)); | |
d0d1b24d | 209 | } |
953ff289 DN |
210 | } |
211 | ||
212 | \f | |
213 | /* The public OpenMP API routines that access these variables. */ | |
214 | ||
215 | void | |
216 | omp_set_num_threads (int n) | |
217 | { | |
218 | gomp_nthreads_var = n; | |
219 | } | |
220 | ||
221 | void | |
222 | omp_set_dynamic (int val) | |
223 | { | |
224 | gomp_dyn_var = val; | |
225 | } | |
226 | ||
227 | int | |
228 | omp_get_dynamic (void) | |
229 | { | |
230 | return gomp_dyn_var; | |
231 | } | |
232 | ||
233 | void | |
234 | omp_set_nested (int val) | |
235 | { | |
236 | gomp_nest_var = val; | |
237 | } | |
238 | ||
239 | int | |
240 | omp_get_nested (void) | |
241 | { | |
242 | return gomp_nest_var; | |
243 | } | |
244 | ||
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) |