OSDN Git Service

2007-01-14 Daniel Franke <franke.daniel@gmail.com>
[pf3gnuchains/gcc-fork.git] / libgomp / parallel.c
1 /* Copyright (C) 2005 Free Software Foundation, Inc.
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"
31
32
33 /* Determine the number of threads to be launched for a PARALLEL construct.
34    This algorithm is explicitly described in OpenMP 2.5 section 2.4.1.
35    SPECIFIED is a combination of the NUM_THREADS clause and the IF clause.
36    If the IF clause is false, SPECIFIED is forced to 1.  When NUM_THREADS
37    is not present, SPECIFIED is 0.  */
38
39 unsigned
40 gomp_resolve_num_threads (unsigned specified)
41 {
42   /* Early exit for false IF condition or degenerate NUM_THREADS.  */
43   if (specified == 1)
44     return 1;
45
46   /* If this is a nested region, and nested regions are disabled, force
47      this team to use only one thread.  */
48   if (gomp_thread()->ts.team && !gomp_nest_var)
49     return 1;
50
51   /* If NUM_THREADS not specified, use nthreads_var.  */
52   if (specified == 0)
53     specified = gomp_nthreads_var;
54
55   /* If dynamic threads are enabled, bound the number of threads
56      that we launch.  */
57   if (gomp_dyn_var)
58     {
59       unsigned dyn = gomp_dynamic_max_threads ();
60       if (dyn < specified)
61         return dyn;
62     }
63
64   return specified;
65 }
66
67 void
68 GOMP_parallel_start (void (*fn) (void *), void *data, unsigned num_threads)
69 {
70   num_threads = gomp_resolve_num_threads (num_threads);
71   gomp_team_start (fn, data, num_threads, NULL);
72 }
73
74 void
75 GOMP_parallel_end (void)
76 {
77   gomp_team_end ();
78 }
79
80 \f
81 /* The public OpenMP API for thread and team related inquiries.  */
82
83 int
84 omp_get_num_threads (void)
85 {
86   struct gomp_team *team = gomp_thread ()->ts.team;
87   return team ? team->nthreads : 1;
88 }
89
90 /* ??? Does this function need to disregard dyn_var?  I don't see
91    how else one could get a useable "maximum".  */
92
93 int
94 omp_get_max_threads (void)
95 {
96   return gomp_resolve_num_threads (0);
97 }
98
99 int
100 omp_get_thread_num (void)
101 {
102   return gomp_thread ()->ts.team_id;
103 }
104
105 /* ??? This isn't right.  The definition of this function is false if any
106    of the IF clauses for any of the parallels is false.  Which is not the
107    same thing as any outer team having more than one thread.  */
108
109 int omp_in_parallel (void)
110 {
111   struct gomp_team *team = gomp_thread ()->ts.team;
112
113   while (team)
114     {
115       if (team->nthreads > 1)
116         return true;
117       team = team->prev_ts.team;
118     }
119
120   return false;
121 }
122
123 ialias (omp_get_num_threads)
124 ialias (omp_get_max_threads)
125 ialias (omp_get_thread_num)
126 ialias (omp_in_parallel)