OSDN Git Service

fortran/
[pf3gnuchains/gcc-fork.git] / libgomp / env.c
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>
35
36
37 unsigned gomp_nthreads_var = 1;
38 bool gomp_dyn_var = false;
39 bool gomp_nest_var = false;
40 enum gomp_schedule_type gomp_run_sched_var = GFS_DYNAMIC;
41 unsigned gomp_run_sched_chunk = 1;
42
43 /* Parse the OMP_SCHEDULE environment variable.  */
44
45 static void
46 parse_schedule (void)
47 {
48   char *env, *end;
49
50   env = getenv ("OMP_SCHEDULE");
51   if (env == NULL)
52     return;
53
54   if (strncmp (env, "static", 6) == 0)
55     {
56       gomp_run_sched_var = GFS_STATIC;
57       env += 6;
58     }
59   else if (strncmp (env, "dynamic", 7) == 0)
60     {
61       gomp_run_sched_var = GFS_DYNAMIC;
62       env += 7;
63     }
64   else if (strncmp (env, "guided", 6) == 0)
65     {
66       gomp_run_sched_var = GFS_GUIDED;
67       env += 6;
68     }
69   else
70     goto unknown;
71
72   if (*env == '\0')
73     return;
74   if (*env != ' ' && *env != ',')
75     goto unknown;
76   while (*env == ' ')
77     env++;
78   if (*env == '\0')
79     return;
80   if (*env != ',')
81     goto unknown;
82   if (*++env == '\0')
83     goto invalid;
84
85   gomp_run_sched_chunk = strtoul (env, &end, 10);
86   if (*end != '\0')
87     goto invalid;
88   return;
89
90  unknown:
91   gomp_error ("Unknown value for environment variable OMP_SCHEDULE");
92   return;
93
94  invalid:
95   gomp_error ("Invalid value for chunk size in "
96               "environment variable OMP_SCHEDULE");
97   gomp_run_sched_chunk = 1;
98   return;
99 }
100
101 /* Parse the OMP_NUM_THREADS environment varible.  Return true if one was
102    present and it was successfully parsed.  */
103
104 static bool
105 parse_num_threads (void)
106 {
107   char *env, *end;
108
109   env = getenv ("OMP_NUM_THREADS");
110   if (env == NULL)
111     return false;
112
113   if (*env == '\0')
114     goto invalid;
115
116   gomp_nthreads_var = strtoul (env, &end, 10);
117   if (*end != '\0')
118     goto invalid;
119   return true;
120
121  invalid:
122   gomp_error ("Invalid value for enviroment variable OMP_NUM_THREADS");
123   gomp_nthreads_var = 1;
124   return false;
125 }
126
127 /* Parse a boolean value for environement variable NAME and store the 
128    result in VALUE.  */
129
130 static void
131 parse_boolean (const char *name, bool *value)
132 {
133   const char *env;
134
135   env = getenv (name);
136   if (env == NULL)
137     return;
138
139   if (strcmp (env, "true") == 0)
140     *value = true;
141   else if (strcmp (env, "false") == 0)
142     *value = false;
143   else
144     gomp_error ("Invalid value for environement variable %s", name);
145 }
146
147 static void __attribute__((constructor))
148 initialize_env (void)
149 {
150   /* Do a compile time check that mkomp_h.pl did good job.  */
151   omp_check_defines ();
152
153   parse_schedule ();
154   parse_boolean ("OMP_DYNAMIC", &gomp_dyn_var);
155   parse_boolean ("OMP_NESTED", &gomp_nest_var);
156   if (!parse_num_threads ())
157     gomp_init_num_threads ();
158 }
159
160 \f
161 /* The public OpenMP API routines that access these variables.  */
162
163 void
164 omp_set_num_threads (int n)
165 {
166   gomp_nthreads_var = n;
167 }
168
169 void
170 omp_set_dynamic (int val)
171 {
172   gomp_dyn_var = val;
173 }
174
175 int
176 omp_get_dynamic (void)
177 {
178   return gomp_dyn_var;
179 }
180
181 void
182 omp_set_nested (int val)
183 {
184   gomp_nest_var = val;
185 }
186
187 int
188 omp_get_nested (void)
189 {
190   return gomp_nest_var;
191 }
192
193 ialias (omp_set_dynamic)
194 ialias (omp_set_nested)
195 ialias (omp_set_num_threads)
196 ialias (omp_get_dynamic)
197 ialias (omp_get_nested)