PR c++/22494
[official-gcc.git] / libgomp / parallel.c
blobedd344a90a8744422e5825e5709c776f3c990029
1 /* Copyright (C) 2005 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"
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. */
39 unsigned
40 gomp_resolve_num_threads (unsigned specified)
42 /* Early exit for false IF condition or degenerate NUM_THREADS. */
43 if (specified == 1)
44 return 1;
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;
51 /* If NUM_THREADS not specified, use nthreads_var. */
52 if (specified == 0)
53 specified = gomp_nthreads_var;
55 /* If dynamic threads are enabled, bound the number of threads
56 that we launch. */
57 if (gomp_dyn_var)
59 unsigned dyn = gomp_dynamic_max_threads ();
60 if (dyn < specified)
61 return dyn;
64 return specified;
67 void
68 GOMP_parallel_start (void (*fn) (void *), void *data, unsigned num_threads)
70 num_threads = gomp_resolve_num_threads (num_threads);
71 gomp_team_start (fn, data, num_threads, NULL);
74 void
75 GOMP_parallel_end (void)
77 gomp_team_end ();
81 /* The public OpenMP API for thread and team related inquiries. */
83 int
84 omp_get_num_threads (void)
86 struct gomp_team *team = gomp_thread ()->ts.team;
87 return team ? team->nthreads : 1;
90 /* ??? Does this function need to disregard dyn_var? I don't see
91 how else one could get a useable "maximum". */
93 int
94 omp_get_max_threads (void)
96 return gomp_resolve_num_threads (0);
99 int
100 omp_get_thread_num (void)
102 return gomp_thread ()->ts.team_id;
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. */
109 int omp_in_parallel (void)
111 struct gomp_team *team = gomp_thread ()->ts.team;
113 while (team)
115 if (team->nthreads > 1)
116 return true;
117 team = team->prev_ts.team;
120 return false;
123 ialias (omp_get_num_threads)
124 ialias (omp_get_max_threads)
125 ialias (omp_get_thread_num)
126 ialias (omp_in_parallel)