Update concepts branch to revision 131834
[official-gcc.git] / libgomp / parallel.c
blob3f2a305613893a662e75382713d9ae898855998e
1 /* Copyright (C) 2005, 2007, 2008 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 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.
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.
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. */
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. */
28 /* This file handles the (bare) PARALLEL construct. */
30 #include "libgomp.h"
31 #include <limits.h>
34 /* Determine the number of threads to be launched for a PARALLEL construct.
35 This algorithm is explicitly described in OpenMP 3.0 section 2.4.1.
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. */
40 unsigned
41 gomp_resolve_num_threads (unsigned specified, unsigned count)
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;
48 icv = gomp_icv (false);
50 if (specified == 1)
51 return 1;
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)
55 return 1;
57 /* If NUM_THREADS not specified, use nthreads_var. */
58 if (specified == 0)
59 threads_requested = icv->nthreads_var;
60 else
61 threads_requested = specified;
63 max_num_threads = threads_requested;
65 /* If dynamic threads are enabled, bound the number of threads
66 that we launch. */
67 if (icv->dyn_var)
69 unsigned dyn = gomp_dynamic_max_threads ();
70 if (dyn < max_num_threads)
71 max_num_threads = dyn;
73 /* Optimization for parallel sections. */
74 if (count && count < max_num_threads)
75 max_num_threads = count;
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;
83 #ifdef HAVE_SYNC_BUILTINS
86 remaining = gomp_remaining_threads_count;
87 num_threads = max_num_threads;
88 if (num_threads > remaining)
89 num_threads = remaining + 1;
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
104 return num_threads;
107 void
108 GOMP_parallel_start (void (*fn) (void *), void *data, unsigned num_threads)
110 num_threads = gomp_resolve_num_threads (num_threads, 0);
111 gomp_team_start (fn, data, num_threads, gomp_new_team (num_threads));
114 void
115 GOMP_parallel_end (void)
117 if (__builtin_expect (gomp_thread_limit_var != ULONG_MAX, 0))
119 struct gomp_thread *thr = gomp_thread ();
120 struct gomp_team *team = thr->ts.team;
121 if (team && team->nthreads > 1)
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
132 gomp_team_end ();
136 /* The public OpenMP API for thread and team related inquiries. */
139 omp_get_num_threads (void)
141 struct gomp_team *team = gomp_thread ()->ts.team;
142 return team ? team->nthreads : 1;
146 omp_get_thread_num (void)
148 return gomp_thread ()->ts.team_id;
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. */
156 omp_in_parallel (void)
158 return gomp_thread ()->ts.active_level > 0;
162 omp_get_level (void)
164 return gomp_thread ()->ts.level;
168 omp_get_ancestor_thread_num (int level)
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;
179 omp_get_team_size (int level)
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;
193 omp_get_active_level (void)
195 return gomp_thread ()->ts.active_level;
198 ialias (omp_get_num_threads)
199 ialias (omp_get_thread_num)
200 ialias (omp_in_parallel)
201 ialias (omp_get_level)
202 ialias (omp_get_ancestor_thread_num)
203 ialias (omp_get_team_size)
204 ialias (omp_get_active_level)