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)
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
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. */
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. */
38 gomp_resolve_num_threads (unsigned specified, unsigned count)
40 struct gomp_thread *thread = gomp_thread();
41 struct gomp_task_icv *icv;
42 unsigned threads_requested, max_num_threads, num_threads;
43 unsigned long remaining;
45 icv = gomp_icv (false);
49 else if (thread->ts.active_level >= 1 && !icv->nest_var)
51 else if (thread->ts.active_level >= gomp_max_active_levels_var)
54 /* If NUM_THREADS not specified, use nthreads_var. */
56 threads_requested = icv->nthreads_var;
58 threads_requested = specified;
60 max_num_threads = threads_requested;
62 /* If dynamic threads are enabled, bound the number of threads
66 unsigned dyn = gomp_dynamic_max_threads ();
67 if (dyn < max_num_threads)
68 max_num_threads = dyn;
70 /* Optimization for parallel sections. */
71 if (count && count < max_num_threads)
72 max_num_threads = count;
75 /* ULONG_MAX stands for infinity. */
76 if (__builtin_expect (gomp_thread_limit_var == ULONG_MAX, 1)
77 || max_num_threads == 1)
78 return max_num_threads;
80 #ifdef HAVE_SYNC_BUILTINS
83 remaining = gomp_remaining_threads_count;
84 num_threads = max_num_threads;
85 if (num_threads > remaining)
86 num_threads = remaining + 1;
88 while (__sync_val_compare_and_swap (&gomp_remaining_threads_count,
89 remaining, remaining - num_threads + 1)
92 gomp_mutex_lock (&gomp_remaining_threads_lock);
93 num_threads = max_num_threads;
94 remaining = gomp_remaining_threads_count;
95 if (num_threads > remaining)
96 num_threads = remaining + 1;
97 gomp_remaining_threads_count -= num_threads - 1;
98 gomp_mutex_unlock (&gomp_remaining_threads_lock);
105 GOMP_parallel_start (void (*fn) (void *), void *data, unsigned num_threads)
107 num_threads = gomp_resolve_num_threads (num_threads, 0);
108 gomp_team_start (fn, data, num_threads, gomp_new_team (num_threads));
112 GOMP_parallel_end (void)
114 if (__builtin_expect (gomp_thread_limit_var != ULONG_MAX, 0))
116 struct gomp_thread *thr = gomp_thread ();
117 struct gomp_team *team = thr->ts.team;
118 unsigned int nthreads = team ? team->nthreads : 1;
122 #ifdef HAVE_SYNC_BUILTINS
123 __sync_fetch_and_add (&gomp_remaining_threads_count,
126 gomp_mutex_lock (&gomp_remaining_threads_lock);
127 gomp_remaining_threads_count += nthreads - 1;
128 gomp_mutex_unlock (&gomp_remaining_threads_lock);
137 /* The public OpenMP API for thread and team related inquiries. */
140 omp_get_num_threads (void)
142 struct gomp_team *team = gomp_thread ()->ts.team;
143 return team ? team->nthreads : 1;
147 omp_get_thread_num (void)
149 return gomp_thread ()->ts.team_id;
152 /* This wasn't right for OpenMP 2.5. Active region used to be non-zero
153 when the IF clause doesn't evaluate to false, starting with OpenMP 3.0
154 it is non-zero with more than one thread in the team. */
157 omp_in_parallel (void)
159 return gomp_thread ()->ts.active_level > 0;
165 return gomp_thread ()->ts.level;
169 omp_get_ancestor_thread_num (int level)
171 struct gomp_team_state *ts = &gomp_thread ()->ts;
172 if (level < 0 || level > ts->level)
174 for (level = ts->level - level; level > 0; --level)
175 ts = &ts->team->prev_ts;
180 omp_get_team_size (int level)
182 struct gomp_team_state *ts = &gomp_thread ()->ts;
183 if (level < 0 || level > ts->level)
185 for (level = ts->level - level; level > 0; --level)
186 ts = &ts->team->prev_ts;
187 if (ts->team == NULL)
190 return ts->team->nthreads;
194 omp_get_active_level (void)
196 return gomp_thread ()->ts.active_level;
199 ialias (omp_get_num_threads)
200 ialias (omp_get_thread_num)
201 ialias (omp_in_parallel)
202 ialias (omp_get_level)
203 ialias (omp_get_ancestor_thread_num)
204 ialias (omp_get_team_size)
205 ialias (omp_get_active_level)