]>
Commit | Line | Data |
---|---|---|
953ff289 DN |
1 | /* Copyright (C) 2005 Free Software Foundation, Inc. |
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" | |
33 | #include <stdlib.h> | |
34 | #include <string.h> | |
d0d1b24d RH |
35 | #include <limits.h> |
36 | #include <errno.h> | |
953ff289 DN |
37 | |
38 | ||
d0d1b24d | 39 | unsigned long gomp_nthreads_var = 1; |
953ff289 DN |
40 | bool gomp_dyn_var = false; |
41 | bool gomp_nest_var = false; | |
42 | enum gomp_schedule_type gomp_run_sched_var = GFS_DYNAMIC; | |
d0d1b24d | 43 | unsigned long gomp_run_sched_chunk = 1; |
953ff289 DN |
44 | |
45 | /* Parse the OMP_SCHEDULE environment variable. */ | |
46 | ||
47 | static void | |
48 | parse_schedule (void) | |
49 | { | |
50 | char *env, *end; | |
51 | ||
52 | env = getenv ("OMP_SCHEDULE"); | |
53 | if (env == NULL) | |
54 | return; | |
55 | ||
56 | if (strncmp (env, "static", 6) == 0) | |
57 | { | |
58 | gomp_run_sched_var = GFS_STATIC; | |
59 | env += 6; | |
60 | } | |
61 | else if (strncmp (env, "dynamic", 7) == 0) | |
62 | { | |
63 | gomp_run_sched_var = GFS_DYNAMIC; | |
64 | env += 7; | |
65 | } | |
66 | else if (strncmp (env, "guided", 6) == 0) | |
67 | { | |
68 | gomp_run_sched_var = GFS_GUIDED; | |
69 | env += 6; | |
70 | } | |
71 | else | |
72 | goto unknown; | |
73 | ||
74 | if (*env == '\0') | |
75 | return; | |
76 | if (*env != ' ' && *env != ',') | |
77 | goto unknown; | |
78 | while (*env == ' ') | |
79 | env++; | |
80 | if (*env == '\0') | |
81 | return; | |
82 | if (*env != ',') | |
83 | goto unknown; | |
84 | if (*++env == '\0') | |
85 | goto invalid; | |
86 | ||
87 | gomp_run_sched_chunk = strtoul (env, &end, 10); | |
88 | if (*end != '\0') | |
89 | goto invalid; | |
90 | return; | |
91 | ||
92 | unknown: | |
93 | gomp_error ("Unknown value for environment variable OMP_SCHEDULE"); | |
94 | return; | |
95 | ||
96 | invalid: | |
97 | gomp_error ("Invalid value for chunk size in " | |
98 | "environment variable OMP_SCHEDULE"); | |
99 | gomp_run_sched_chunk = 1; | |
100 | return; | |
101 | } | |
102 | ||
d0d1b24d | 103 | /* Parse an unsigned long environment varible. Return true if one was |
953ff289 DN |
104 | present and it was successfully parsed. */ |
105 | ||
106 | static bool | |
d0d1b24d | 107 | parse_unsigned_long (const char *name, unsigned long *pvalue) |
953ff289 DN |
108 | { |
109 | char *env, *end; | |
d0d1b24d | 110 | unsigned long value; |
953ff289 | 111 | |
d0d1b24d | 112 | env = getenv (name); |
953ff289 DN |
113 | if (env == NULL) |
114 | return false; | |
115 | ||
116 | if (*env == '\0') | |
117 | goto invalid; | |
118 | ||
d0d1b24d | 119 | value = strtoul (env, &end, 10); |
953ff289 DN |
120 | if (*end != '\0') |
121 | goto invalid; | |
d0d1b24d RH |
122 | |
123 | *pvalue = value; | |
953ff289 DN |
124 | return true; |
125 | ||
126 | invalid: | |
d0d1b24d | 127 | gomp_error ("Invalid value for environment variable %s", name); |
953ff289 DN |
128 | return false; |
129 | } | |
130 | ||
d0d1b24d | 131 | /* Parse a boolean value for environment variable NAME and store the |
953ff289 DN |
132 | result in VALUE. */ |
133 | ||
134 | static void | |
135 | parse_boolean (const char *name, bool *value) | |
136 | { | |
137 | const char *env; | |
138 | ||
139 | env = getenv (name); | |
140 | if (env == NULL) | |
141 | return; | |
142 | ||
143 | if (strcmp (env, "true") == 0) | |
144 | *value = true; | |
145 | else if (strcmp (env, "false") == 0) | |
146 | *value = false; | |
147 | else | |
d0d1b24d | 148 | gomp_error ("Invalid value for environment variable %s", name); |
953ff289 DN |
149 | } |
150 | ||
151 | static void __attribute__((constructor)) | |
152 | initialize_env (void) | |
153 | { | |
d0d1b24d RH |
154 | unsigned long stacksize; |
155 | ||
953ff289 DN |
156 | /* Do a compile time check that mkomp_h.pl did good job. */ |
157 | omp_check_defines (); | |
158 | ||
159 | parse_schedule (); | |
160 | parse_boolean ("OMP_DYNAMIC", &gomp_dyn_var); | |
161 | parse_boolean ("OMP_NESTED", &gomp_nest_var); | |
d0d1b24d | 162 | if (!parse_unsigned_long ("OMP_NUM_THREADS", &gomp_nthreads_var)) |
953ff289 | 163 | gomp_init_num_threads (); |
d0d1b24d RH |
164 | |
165 | /* Not strictly environment related, but ordering constructors is tricky. */ | |
166 | pthread_attr_init (&gomp_thread_attr); | |
167 | pthread_attr_setdetachstate (&gomp_thread_attr, PTHREAD_CREATE_DETACHED); | |
168 | ||
169 | if (parse_unsigned_long ("OMP_STACKSIZE", &stacksize)) | |
170 | { | |
171 | stacksize *= 1024; | |
172 | if (stacksize < PTHREAD_STACK_MIN) | |
173 | gomp_error ("Stack size less than minimum of %luk", | |
174 | PTHREAD_STACK_MIN / 1024ul | |
175 | + (PTHREAD_STACK_MIN % 1024 != 0)); | |
176 | else | |
177 | { | |
178 | int err = pthread_attr_setstacksize (&gomp_thread_attr, stacksize); | |
179 | if (err == EINVAL) | |
180 | gomp_error ("Stack size larger than system limit"); | |
181 | else if (err != 0) | |
182 | gomp_error ("Stack size change failed: %s", strerror (err)); | |
183 | } | |
184 | } | |
953ff289 DN |
185 | } |
186 | ||
187 | \f | |
188 | /* The public OpenMP API routines that access these variables. */ | |
189 | ||
190 | void | |
191 | omp_set_num_threads (int n) | |
192 | { | |
193 | gomp_nthreads_var = n; | |
194 | } | |
195 | ||
196 | void | |
197 | omp_set_dynamic (int val) | |
198 | { | |
199 | gomp_dyn_var = val; | |
200 | } | |
201 | ||
202 | int | |
203 | omp_get_dynamic (void) | |
204 | { | |
205 | return gomp_dyn_var; | |
206 | } | |
207 | ||
208 | void | |
209 | omp_set_nested (int val) | |
210 | { | |
211 | gomp_nest_var = val; | |
212 | } | |
213 | ||
214 | int | |
215 | omp_get_nested (void) | |
216 | { | |
217 | return gomp_nest_var; | |
218 | } | |
219 | ||
220 | ialias (omp_set_dynamic) | |
221 | ialias (omp_set_nested) | |
222 | ialias (omp_set_num_threads) | |
223 | ialias (omp_get_dynamic) | |
224 | ialias (omp_get_nested) |