xref: /openbsd-src/gnu/gcc/libgomp/parallel.c (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert /* Copyright (C) 2005 Free Software Foundation, Inc.
2*404b540aSrobert    Contributed by Richard Henderson <rth@redhat.com>.
3*404b540aSrobert 
4*404b540aSrobert    This file is part of the GNU OpenMP Library (libgomp).
5*404b540aSrobert 
6*404b540aSrobert    Libgomp is free software; you can redistribute it and/or modify it
7*404b540aSrobert    under the terms of the GNU Lesser General Public License as published by
8*404b540aSrobert    the Free Software Foundation; either version 2.1 of the License, or
9*404b540aSrobert    (at your option) any later version.
10*404b540aSrobert 
11*404b540aSrobert    Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
12*404b540aSrobert    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13*404b540aSrobert    FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
14*404b540aSrobert    more details.
15*404b540aSrobert 
16*404b540aSrobert    You should have received a copy of the GNU Lesser General Public License
17*404b540aSrobert    along with libgomp; see the file COPYING.LIB.  If not, write to the
18*404b540aSrobert    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19*404b540aSrobert    MA 02110-1301, USA.  */
20*404b540aSrobert 
21*404b540aSrobert /* As a special exception, if you link this library with other files, some
22*404b540aSrobert    of which are compiled with GCC, to produce an executable, this library
23*404b540aSrobert    does not by itself cause the resulting executable to be covered by the
24*404b540aSrobert    GNU General Public License.  This exception does not however invalidate
25*404b540aSrobert    any other reasons why the executable file might be covered by the GNU
26*404b540aSrobert    General Public License.  */
27*404b540aSrobert 
28*404b540aSrobert /* This file handles the (bare) PARALLEL construct.  */
29*404b540aSrobert 
30*404b540aSrobert #include "libgomp.h"
31*404b540aSrobert 
32*404b540aSrobert 
33*404b540aSrobert /* Determine the number of threads to be launched for a PARALLEL construct.
34*404b540aSrobert    This algorithm is explicitly described in OpenMP 2.5 section 2.4.1.
35*404b540aSrobert    SPECIFIED is a combination of the NUM_THREADS clause and the IF clause.
36*404b540aSrobert    If the IF clause is false, SPECIFIED is forced to 1.  When NUM_THREADS
37*404b540aSrobert    is not present, SPECIFIED is 0.  */
38*404b540aSrobert 
39*404b540aSrobert unsigned
gomp_resolve_num_threads(unsigned specified)40*404b540aSrobert gomp_resolve_num_threads (unsigned specified)
41*404b540aSrobert {
42*404b540aSrobert   /* Early exit for false IF condition or degenerate NUM_THREADS.  */
43*404b540aSrobert   if (specified == 1)
44*404b540aSrobert     return 1;
45*404b540aSrobert 
46*404b540aSrobert   /* If this is a nested region, and nested regions are disabled, force
47*404b540aSrobert      this team to use only one thread.  */
48*404b540aSrobert   if (gomp_thread()->ts.team && !gomp_nest_var)
49*404b540aSrobert     return 1;
50*404b540aSrobert 
51*404b540aSrobert   /* If NUM_THREADS not specified, use nthreads_var.  */
52*404b540aSrobert   if (specified == 0)
53*404b540aSrobert     specified = gomp_nthreads_var;
54*404b540aSrobert 
55*404b540aSrobert   /* If dynamic threads are enabled, bound the number of threads
56*404b540aSrobert      that we launch.  */
57*404b540aSrobert   if (gomp_dyn_var)
58*404b540aSrobert     {
59*404b540aSrobert       unsigned dyn = gomp_dynamic_max_threads ();
60*404b540aSrobert       if (dyn < specified)
61*404b540aSrobert 	return dyn;
62*404b540aSrobert     }
63*404b540aSrobert 
64*404b540aSrobert   return specified;
65*404b540aSrobert }
66*404b540aSrobert 
67*404b540aSrobert void
GOMP_parallel_start(void (* fn)(void *),void * data,unsigned num_threads)68*404b540aSrobert GOMP_parallel_start (void (*fn) (void *), void *data, unsigned num_threads)
69*404b540aSrobert {
70*404b540aSrobert   num_threads = gomp_resolve_num_threads (num_threads);
71*404b540aSrobert   gomp_team_start (fn, data, num_threads, NULL);
72*404b540aSrobert }
73*404b540aSrobert 
74*404b540aSrobert void
GOMP_parallel_end(void)75*404b540aSrobert GOMP_parallel_end (void)
76*404b540aSrobert {
77*404b540aSrobert   gomp_team_end ();
78*404b540aSrobert }
79*404b540aSrobert 
80*404b540aSrobert 
81*404b540aSrobert /* The public OpenMP API for thread and team related inquiries.  */
82*404b540aSrobert 
83*404b540aSrobert int
omp_get_num_threads(void)84*404b540aSrobert omp_get_num_threads (void)
85*404b540aSrobert {
86*404b540aSrobert   struct gomp_team *team = gomp_thread ()->ts.team;
87*404b540aSrobert   return team ? team->nthreads : 1;
88*404b540aSrobert }
89*404b540aSrobert 
90*404b540aSrobert /* ??? Does this function need to disregard dyn_var?  I don't see
91*404b540aSrobert    how else one could get a useable "maximum".  */
92*404b540aSrobert 
93*404b540aSrobert int
omp_get_max_threads(void)94*404b540aSrobert omp_get_max_threads (void)
95*404b540aSrobert {
96*404b540aSrobert   return gomp_resolve_num_threads (0);
97*404b540aSrobert }
98*404b540aSrobert 
99*404b540aSrobert int
omp_get_thread_num(void)100*404b540aSrobert omp_get_thread_num (void)
101*404b540aSrobert {
102*404b540aSrobert   return gomp_thread ()->ts.team_id;
103*404b540aSrobert }
104*404b540aSrobert 
105*404b540aSrobert /* ??? This isn't right.  The definition of this function is false if any
106*404b540aSrobert    of the IF clauses for any of the parallels is false.  Which is not the
107*404b540aSrobert    same thing as any outer team having more than one thread.  */
108*404b540aSrobert 
omp_in_parallel(void)109*404b540aSrobert int omp_in_parallel (void)
110*404b540aSrobert {
111*404b540aSrobert   struct gomp_team *team = gomp_thread ()->ts.team;
112*404b540aSrobert 
113*404b540aSrobert   while (team)
114*404b540aSrobert     {
115*404b540aSrobert       if (team->nthreads > 1)
116*404b540aSrobert 	return true;
117*404b540aSrobert       team = team->prev_ts.team;
118*404b540aSrobert     }
119*404b540aSrobert 
120*404b540aSrobert   return false;
121*404b540aSrobert }
122*404b540aSrobert 
123*404b540aSrobert ialias (omp_get_num_threads)
124*404b540aSrobert ialias (omp_get_max_threads)
125*404b540aSrobert ialias (omp_get_thread_num)
126*404b540aSrobert ialias (omp_in_parallel)
127