Bump date stamp to 20140105
[official-gcc.git] / libgomp / parallel.c
blobccc0b6a6973c25733d3fdc70e59a41d6c6e6ad52
1 /* Copyright (C) 2005-2013 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 General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 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 General Public License for
14 more details.
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 /* This file handles the (bare) PARALLEL construct. */
27 #include "libgomp.h"
28 #include <limits.h>
31 /* Determine the number of threads to be launched for a PARALLEL construct.
32 This algorithm is explicitly described in OpenMP 3.0 section 2.4.1.
33 SPECIFIED is a combination of the NUM_THREADS clause and the IF clause.
34 If the IF clause is false, SPECIFIED is forced to 1. When NUM_THREADS
35 is not present, SPECIFIED is 0. */
37 unsigned
38 gomp_resolve_num_threads (unsigned specified, unsigned count)
40 struct gomp_thread *thr = gomp_thread ();
41 struct gomp_task_icv *icv;
42 unsigned threads_requested, max_num_threads, num_threads;
43 unsigned long busy;
44 struct gomp_thread_pool *pool;
46 icv = gomp_icv (false);
48 if (specified == 1)
49 return 1;
50 else if (thr->ts.active_level >= 1 && !icv->nest_var)
51 return 1;
52 else if (thr->ts.active_level >= gomp_max_active_levels_var)
53 return 1;
55 /* If NUM_THREADS not specified, use nthreads_var. */
56 if (specified == 0)
57 threads_requested = icv->nthreads_var;
58 else
59 threads_requested = specified;
61 max_num_threads = threads_requested;
63 /* If dynamic threads are enabled, bound the number of threads
64 that we launch. */
65 if (icv->dyn_var)
67 unsigned dyn = gomp_dynamic_max_threads ();
68 if (dyn < max_num_threads)
69 max_num_threads = dyn;
71 /* Optimization for parallel sections. */
72 if (count && count < max_num_threads)
73 max_num_threads = count;
76 /* UINT_MAX stands for infinity. */
77 if (__builtin_expect (icv->thread_limit_var == UINT_MAX, 1)
78 || max_num_threads == 1)
79 return max_num_threads;
81 /* The threads_busy counter lives in thread_pool, if there
82 isn't a thread_pool yet, there must be just one thread
83 in the contention group. If thr->team is NULL, this isn't
84 nested parallel, so there is just one thread in the
85 contention group as well, no need to handle it atomically. */
86 pool = thr->thread_pool;
87 if (thr->ts.team == NULL)
89 num_threads = max_num_threads;
90 if (num_threads > icv->thread_limit_var)
91 num_threads = icv->thread_limit_var;
92 if (pool)
93 pool->threads_busy = num_threads;
94 return num_threads;
97 #ifdef HAVE_SYNC_BUILTINS
100 busy = pool->threads_busy;
101 num_threads = max_num_threads;
102 if (icv->thread_limit_var - busy + 1 < num_threads)
103 num_threads = icv->thread_limit_var - busy + 1;
105 while (__sync_val_compare_and_swap (&pool->threads_busy,
106 busy, busy + num_threads - 1)
107 != busy);
108 #else
109 gomp_mutex_lock (&gomp_managed_threads_lock);
110 num_threads = max_num_threads;
111 busy = pool->threads_busy;
112 if (icv->thread_limit_var - busy + 1 < num_threads)
113 num_threads = icv->thread_limit_var - busy + 1;
114 pool->threads_busy += num_threads - 1;
115 gomp_mutex_unlock (&gomp_managed_threads_lock);
116 #endif
118 return num_threads;
121 void
122 GOMP_parallel_start (void (*fn) (void *), void *data, unsigned num_threads)
124 num_threads = gomp_resolve_num_threads (num_threads, 0);
125 gomp_team_start (fn, data, num_threads, 0, gomp_new_team (num_threads));
128 void
129 GOMP_parallel_end (void)
131 struct gomp_task_icv *icv = gomp_icv (false);
132 if (__builtin_expect (icv->thread_limit_var != UINT_MAX, 0))
134 struct gomp_thread *thr = gomp_thread ();
135 struct gomp_team *team = thr->ts.team;
136 unsigned int nthreads = team ? team->nthreads : 1;
137 gomp_team_end ();
138 if (nthreads > 1)
140 /* If not nested, there is just one thread in the
141 contention group left, no need for atomicity. */
142 if (thr->ts.team == NULL)
143 thr->thread_pool->threads_busy = 1;
144 else
146 #ifdef HAVE_SYNC_BUILTINS
147 __sync_fetch_and_add (&thr->thread_pool->threads_busy,
148 1UL - nthreads);
149 #else
150 gomp_mutex_lock (&gomp_managed_threads_lock);
151 thr->thread_pool->threads_busy -= nthreads - 1;
152 gomp_mutex_unlock (&gomp_managed_threads_lock);
153 #endif
157 else
158 gomp_team_end ();
160 ialias (GOMP_parallel_end)
162 void
163 GOMP_parallel (void (*fn) (void *), void *data, unsigned num_threads, unsigned int flags)
165 num_threads = gomp_resolve_num_threads (num_threads, 0);
166 gomp_team_start (fn, data, num_threads, flags, gomp_new_team (num_threads));
167 fn (data);
168 ialias_call (GOMP_parallel_end) ();
171 bool
172 GOMP_cancellation_point (int which)
174 if (!gomp_cancel_var)
175 return false;
177 struct gomp_thread *thr = gomp_thread ();
178 struct gomp_team *team = thr->ts.team;
179 if (which & (GOMP_CANCEL_LOOP | GOMP_CANCEL_SECTIONS))
181 if (team == NULL)
182 return false;
183 return team->work_share_cancelled != 0;
185 else if (which & GOMP_CANCEL_TASKGROUP)
187 if (thr->task->taskgroup && thr->task->taskgroup->cancelled)
188 return true;
189 /* FALLTHRU into the GOMP_CANCEL_PARALLEL case,
190 as #pragma omp cancel parallel also cancels all explicit
191 tasks. */
193 if (team)
194 return gomp_team_barrier_cancelled (&team->barrier);
195 return false;
197 ialias (GOMP_cancellation_point)
199 bool
200 GOMP_cancel (int which, bool do_cancel)
202 if (!gomp_cancel_var)
203 return false;
205 if (!do_cancel)
206 return ialias_call (GOMP_cancellation_point) (which);
208 struct gomp_thread *thr = gomp_thread ();
209 struct gomp_team *team = thr->ts.team;
210 if (which & (GOMP_CANCEL_LOOP | GOMP_CANCEL_SECTIONS))
212 /* In orphaned worksharing region, all we want to cancel
213 is current thread. */
214 if (team != NULL)
215 team->work_share_cancelled = 1;
216 return true;
218 else if (which & GOMP_CANCEL_TASKGROUP)
220 if (thr->task->taskgroup && !thr->task->taskgroup->cancelled)
222 gomp_mutex_lock (&team->task_lock);
223 thr->task->taskgroup->cancelled = true;
224 gomp_mutex_unlock (&team->task_lock);
226 return true;
228 team->team_cancelled = 1;
229 gomp_team_barrier_cancel (team);
230 return true;
233 /* The public OpenMP API for thread and team related inquiries. */
236 omp_get_num_threads (void)
238 struct gomp_team *team = gomp_thread ()->ts.team;
239 return team ? team->nthreads : 1;
243 omp_get_thread_num (void)
245 return gomp_thread ()->ts.team_id;
248 /* This wasn't right for OpenMP 2.5. Active region used to be non-zero
249 when the IF clause doesn't evaluate to false, starting with OpenMP 3.0
250 it is non-zero with more than one thread in the team. */
253 omp_in_parallel (void)
255 return gomp_thread ()->ts.active_level > 0;
259 omp_get_level (void)
261 return gomp_thread ()->ts.level;
265 omp_get_ancestor_thread_num (int level)
267 struct gomp_team_state *ts = &gomp_thread ()->ts;
268 if (level < 0 || level > ts->level)
269 return -1;
270 for (level = ts->level - level; level > 0; --level)
271 ts = &ts->team->prev_ts;
272 return ts->team_id;
276 omp_get_team_size (int level)
278 struct gomp_team_state *ts = &gomp_thread ()->ts;
279 if (level < 0 || level > ts->level)
280 return -1;
281 for (level = ts->level - level; level > 0; --level)
282 ts = &ts->team->prev_ts;
283 if (ts->team == NULL)
284 return 1;
285 else
286 return ts->team->nthreads;
290 omp_get_active_level (void)
292 return gomp_thread ()->ts.active_level;
295 ialias (omp_get_num_threads)
296 ialias (omp_get_thread_num)
297 ialias (omp_in_parallel)
298 ialias (omp_get_level)
299 ialias (omp_get_ancestor_thread_num)
300 ialias (omp_get_team_size)
301 ialias (omp_get_active_level)