]>
Commit | Line | Data |
---|---|---|
a68ab351 | 1 | /* Copyright (C) 2005, 2007, 2008 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 handles the (bare) PARALLEL construct. */ | |
29 | ||
30 | #include "libgomp.h" | |
a68ab351 | 31 | #include <limits.h> |
953ff289 DN |
32 | |
33 | ||
34 | /* Determine the number of threads to be launched for a PARALLEL construct. | |
a68ab351 | 35 | This algorithm is explicitly described in OpenMP 3.0 section 2.4.1. |
953ff289 DN |
36 | SPECIFIED is a combination of the NUM_THREADS clause and the IF clause. |
37 | If the IF clause is false, SPECIFIED is forced to 1. When NUM_THREADS | |
38 | is not present, SPECIFIED is 0. */ | |
39 | ||
40 | unsigned | |
a68ab351 | 41 | gomp_resolve_num_threads (unsigned specified, unsigned count) |
953ff289 | 42 | { |
a68ab351 JJ |
43 | struct gomp_thread *thread = gomp_thread(); |
44 | struct gomp_task_icv *icv; | |
45 | unsigned threads_requested, max_num_threads, num_threads; | |
46 | unsigned long remaining; | |
47 | ||
48 | icv = gomp_icv (false); | |
49 | ||
953ff289 DN |
50 | if (specified == 1) |
51 | return 1; | |
a68ab351 JJ |
52 | else if (thread->ts.active_level >= 1 && !icv->nest_var) |
53 | return 1; | |
54 | else if (thread->ts.active_level >= gomp_max_active_levels_var) | |
953ff289 DN |
55 | return 1; |
56 | ||
57 | /* If NUM_THREADS not specified, use nthreads_var. */ | |
58 | if (specified == 0) | |
a68ab351 JJ |
59 | threads_requested = icv->nthreads_var; |
60 | else | |
61 | threads_requested = specified; | |
62 | ||
63 | max_num_threads = threads_requested; | |
953ff289 DN |
64 | |
65 | /* If dynamic threads are enabled, bound the number of threads | |
66 | that we launch. */ | |
a68ab351 | 67 | if (icv->dyn_var) |
953ff289 DN |
68 | { |
69 | unsigned dyn = gomp_dynamic_max_threads (); | |
a68ab351 JJ |
70 | if (dyn < max_num_threads) |
71 | max_num_threads = dyn; | |
72 | ||
73 | /* Optimization for parallel sections. */ | |
74 | if (count && count < max_num_threads) | |
75 | max_num_threads = count; | |
953ff289 DN |
76 | } |
77 | ||
a68ab351 JJ |
78 | /* ULONG_MAX stands for infinity. */ |
79 | if (__builtin_expect (gomp_thread_limit_var == ULONG_MAX, 1) | |
80 | || max_num_threads == 1) | |
81 | return max_num_threads; | |
82 | ||
83 | #ifdef HAVE_SYNC_BUILTINS | |
84 | do | |
85 | { | |
86 | remaining = gomp_remaining_threads_count; | |
87 | num_threads = max_num_threads; | |
88 | if (num_threads > remaining) | |
89 | num_threads = remaining + 1; | |
90 | } | |
91 | while (__sync_val_compare_and_swap (&gomp_remaining_threads_count, | |
92 | remaining, remaining - num_threads + 1) | |
93 | != remaining); | |
94 | #else | |
95 | gomp_mutex_lock (&gomp_remaining_threads_lock); | |
96 | num_threads = max_num_threads; | |
97 | remaining = gomp_remaining_threads_count; | |
98 | if (num_threads > remaining) | |
99 | num_threads = remaining + 1; | |
100 | gomp_remaining_threads_count -= num_threads - 1; | |
101 | gomp_mutex_unlock (&gomp_remaining_threads_lock); | |
102 | #endif | |
103 | ||
104 | return num_threads; | |
953ff289 DN |
105 | } |
106 | ||
107 | void | |
108 | GOMP_parallel_start (void (*fn) (void *), void *data, unsigned num_threads) | |
109 | { | |
a68ab351 JJ |
110 | num_threads = gomp_resolve_num_threads (num_threads, 0); |
111 | gomp_team_start (fn, data, num_threads, gomp_new_team (num_threads)); | |
953ff289 DN |
112 | } |
113 | ||
114 | void | |
115 | GOMP_parallel_end (void) | |
116 | { | |
a68ab351 JJ |
117 | if (__builtin_expect (gomp_thread_limit_var != ULONG_MAX, 0)) |
118 | { | |
119 | struct gomp_thread *thr = gomp_thread (); | |
120 | struct gomp_team *team = thr->ts.team; | |
121 | if (team && team->nthreads > 1) | |
122 | { | |
123 | #ifdef HAVE_SYNC_BUILTINS | |
124 | __sync_fetch_and_add (&gomp_remaining_threads_count, | |
125 | 1UL - team->nthreads); | |
126 | #else | |
127 | gomp_mutex_lock (&gomp_remaining_threads_lock); | |
128 | gomp_remaining_threads_count -= team->nthreads - 1; | |
129 | #endif | |
130 | } | |
131 | } | |
953ff289 DN |
132 | gomp_team_end (); |
133 | } | |
134 | ||
135 | \f | |
136 | /* The public OpenMP API for thread and team related inquiries. */ | |
137 | ||
138 | int | |
139 | omp_get_num_threads (void) | |
140 | { | |
141 | struct gomp_team *team = gomp_thread ()->ts.team; | |
142 | return team ? team->nthreads : 1; | |
143 | } | |
144 | ||
953ff289 | 145 | int |
a68ab351 | 146 | omp_get_thread_num (void) |
953ff289 | 147 | { |
a68ab351 | 148 | return gomp_thread ()->ts.team_id; |
953ff289 DN |
149 | } |
150 | ||
a68ab351 JJ |
151 | /* This wasn't right for OpenMP 2.5. Active region used to be non-zero |
152 | when the IF clause doesn't evaluate to false, starting with OpenMP 3.0 | |
153 | it is non-zero with more than one thread in the team. */ | |
154 | ||
953ff289 | 155 | int |
a68ab351 | 156 | omp_in_parallel (void) |
953ff289 | 157 | { |
a68ab351 | 158 | return gomp_thread ()->ts.active_level > 0; |
953ff289 DN |
159 | } |
160 | ||
a68ab351 JJ |
161 | int |
162 | omp_get_level (void) | |
163 | { | |
164 | return gomp_thread ()->ts.level; | |
165 | } | |
953ff289 | 166 | |
a68ab351 JJ |
167 | int |
168 | omp_get_ancestor_thread_num (int level) | |
953ff289 | 169 | { |
a68ab351 JJ |
170 | struct gomp_team_state *ts = &gomp_thread ()->ts; |
171 | if (level < 0 || level > ts->level) | |
172 | return -1; | |
173 | for (level = ts->level - level; level > 0; --level) | |
174 | ts = &ts->team->prev_ts; | |
175 | return ts->team_id; | |
176 | } | |
953ff289 | 177 | |
a68ab351 JJ |
178 | int |
179 | omp_get_team_size (int level) | |
180 | { | |
181 | struct gomp_team_state *ts = &gomp_thread ()->ts; | |
182 | if (level < 0 || level > ts->level) | |
183 | return -1; | |
184 | for (level = ts->level - level; level > 0; --level) | |
185 | ts = &ts->team->prev_ts; | |
186 | if (ts->team == NULL) | |
187 | return 1; | |
188 | else | |
189 | return ts->team->nthreads; | |
190 | } | |
953ff289 | 191 | |
a68ab351 JJ |
192 | int |
193 | omp_get_active_level (void) | |
194 | { | |
195 | return gomp_thread ()->ts.active_level; | |
953ff289 DN |
196 | } |
197 | ||
198 | ialias (omp_get_num_threads) | |
953ff289 DN |
199 | ialias (omp_get_thread_num) |
200 | ialias (omp_in_parallel) | |
a68ab351 JJ |
201 | ialias (omp_get_level) |
202 | ialias (omp_get_ancestor_thread_num) | |
203 | ialias (omp_get_team_size) | |
204 | ialias (omp_get_active_level) |