annotate libgomp/parallel.c @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1 /* Copyright (C) 2005-2020 Free Software Foundation, Inc.
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 Contributed by Richard Henderson <rth@redhat.com>.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3
111
kono
parents: 67
diff changeset
4 This file is part of the GNU Offloading and Multi Processing Library
kono
parents: 67
diff changeset
5 (libgomp).
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 Libgomp is free software; you can redistribute it and/or modify it
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 under the terms of the GNU General Public License as published by
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 the Free Software Foundation; either version 3, or (at your option)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 any later version.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 more details.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 Under Section 7 of GPL version 3, you are granted additional
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 permissions described in the GCC Runtime Library Exception, version
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 3.1, as published by the Free Software Foundation.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 You should have received a copy of the GNU General Public License and
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 a copy of the GCC Runtime Library Exception along with this program;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 <http://www.gnu.org/licenses/>. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
25
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 /* This file handles the (bare) PARALLEL construct. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
27
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 #include "libgomp.h"
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 #include <limits.h>
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
30
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
31
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 /* Determine the number of threads to be launched for a PARALLEL construct.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 This algorithm is explicitly described in OpenMP 3.0 section 2.4.1.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 SPECIFIED is a combination of the NUM_THREADS clause and the IF clause.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 If the IF clause is false, SPECIFIED is forced to 1. When NUM_THREADS
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 is not present, SPECIFIED is 0. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
37
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 unsigned
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 gomp_resolve_num_threads (unsigned specified, unsigned count)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 {
111
kono
parents: 67
diff changeset
41 struct gomp_thread *thr = gomp_thread ();
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 struct gomp_task_icv *icv;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 unsigned threads_requested, max_num_threads, num_threads;
111
kono
parents: 67
diff changeset
44 unsigned long busy;
kono
parents: 67
diff changeset
45 struct gomp_thread_pool *pool;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
46
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 icv = gomp_icv (false);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
48
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 if (specified == 1)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 return 1;
111
kono
parents: 67
diff changeset
51 else if (thr->ts.active_level >= 1 && !icv->nest_var)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 return 1;
111
kono
parents: 67
diff changeset
53 else if (thr->ts.active_level >= gomp_max_active_levels_var)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 return 1;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
55
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 /* If NUM_THREADS not specified, use nthreads_var. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 if (specified == 0)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 threads_requested = icv->nthreads_var;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 threads_requested = specified;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
61
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 max_num_threads = threads_requested;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
63
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 /* If dynamic threads are enabled, bound the number of threads
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 that we launch. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 if (icv->dyn_var)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 unsigned dyn = gomp_dynamic_max_threads ();
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 if (dyn < max_num_threads)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 max_num_threads = dyn;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
71
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 /* Optimization for parallel sections. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 if (count && count < max_num_threads)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 max_num_threads = count;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
76
111
kono
parents: 67
diff changeset
77 /* UINT_MAX stands for infinity. */
kono
parents: 67
diff changeset
78 if (__builtin_expect (icv->thread_limit_var == UINT_MAX, 1)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 || max_num_threads == 1)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 return max_num_threads;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
81
111
kono
parents: 67
diff changeset
82 /* The threads_busy counter lives in thread_pool, if there
kono
parents: 67
diff changeset
83 isn't a thread_pool yet, there must be just one thread
kono
parents: 67
diff changeset
84 in the contention group. If thr->team is NULL, this isn't
kono
parents: 67
diff changeset
85 nested parallel, so there is just one thread in the
kono
parents: 67
diff changeset
86 contention group as well, no need to handle it atomically. */
kono
parents: 67
diff changeset
87 pool = thr->thread_pool;
kono
parents: 67
diff changeset
88 if (thr->ts.team == NULL || pool == NULL)
kono
parents: 67
diff changeset
89 {
kono
parents: 67
diff changeset
90 num_threads = max_num_threads;
kono
parents: 67
diff changeset
91 if (num_threads > icv->thread_limit_var)
kono
parents: 67
diff changeset
92 num_threads = icv->thread_limit_var;
kono
parents: 67
diff changeset
93 if (pool)
kono
parents: 67
diff changeset
94 pool->threads_busy = num_threads;
kono
parents: 67
diff changeset
95 return num_threads;
kono
parents: 67
diff changeset
96 }
kono
parents: 67
diff changeset
97
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 #ifdef HAVE_SYNC_BUILTINS
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 do
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 {
111
kono
parents: 67
diff changeset
101 busy = pool->threads_busy;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 num_threads = max_num_threads;
111
kono
parents: 67
diff changeset
103 if (icv->thread_limit_var - busy + 1 < num_threads)
kono
parents: 67
diff changeset
104 num_threads = icv->thread_limit_var - busy + 1;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 }
111
kono
parents: 67
diff changeset
106 while (__sync_val_compare_and_swap (&pool->threads_busy,
kono
parents: 67
diff changeset
107 busy, busy + num_threads - 1)
kono
parents: 67
diff changeset
108 != busy);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 #else
111
kono
parents: 67
diff changeset
110 gomp_mutex_lock (&gomp_managed_threads_lock);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 num_threads = max_num_threads;
111
kono
parents: 67
diff changeset
112 busy = pool->threads_busy;
kono
parents: 67
diff changeset
113 if (icv->thread_limit_var - busy + 1 < num_threads)
kono
parents: 67
diff changeset
114 num_threads = icv->thread_limit_var - busy + 1;
kono
parents: 67
diff changeset
115 pool->threads_busy += num_threads - 1;
kono
parents: 67
diff changeset
116 gomp_mutex_unlock (&gomp_managed_threads_lock);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 #endif
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
118
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 return num_threads;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
121
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 GOMP_parallel_start (void (*fn) (void *), void *data, unsigned num_threads)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
124 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
125 num_threads = gomp_resolve_num_threads (num_threads, 0);
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
126 gomp_team_start (fn, data, num_threads, 0, gomp_new_team (num_threads),
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
127 NULL);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
128 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
129
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
130 void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
131 GOMP_parallel_end (void)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 {
111
kono
parents: 67
diff changeset
133 struct gomp_task_icv *icv = gomp_icv (false);
kono
parents: 67
diff changeset
134 if (__builtin_expect (icv->thread_limit_var != UINT_MAX, 0))
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
135 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
136 struct gomp_thread *thr = gomp_thread ();
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
137 struct gomp_team *team = thr->ts.team;
111
kono
parents: 67
diff changeset
138 unsigned int nthreads = team ? team->nthreads : 1;
kono
parents: 67
diff changeset
139 gomp_team_end ();
kono
parents: 67
diff changeset
140 if (nthreads > 1)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
141 {
111
kono
parents: 67
diff changeset
142 /* If not nested, there is just one thread in the
kono
parents: 67
diff changeset
143 contention group left, no need for atomicity. */
kono
parents: 67
diff changeset
144 if (thr->ts.team == NULL)
kono
parents: 67
diff changeset
145 thr->thread_pool->threads_busy = 1;
kono
parents: 67
diff changeset
146 else
kono
parents: 67
diff changeset
147 {
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
148 #ifdef HAVE_SYNC_BUILTINS
111
kono
parents: 67
diff changeset
149 __sync_fetch_and_add (&thr->thread_pool->threads_busy,
kono
parents: 67
diff changeset
150 1UL - nthreads);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
151 #else
111
kono
parents: 67
diff changeset
152 gomp_mutex_lock (&gomp_managed_threads_lock);
kono
parents: 67
diff changeset
153 thr->thread_pool->threads_busy -= nthreads - 1;
kono
parents: 67
diff changeset
154 gomp_mutex_unlock (&gomp_managed_threads_lock);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
155 #endif
111
kono
parents: 67
diff changeset
156 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
157 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
158 }
111
kono
parents: 67
diff changeset
159 else
kono
parents: 67
diff changeset
160 gomp_team_end ();
kono
parents: 67
diff changeset
161 }
kono
parents: 67
diff changeset
162 ialias (GOMP_parallel_end)
kono
parents: 67
diff changeset
163
kono
parents: 67
diff changeset
164 void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
165 GOMP_parallel (void (*fn) (void *), void *data, unsigned num_threads,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
166 unsigned int flags)
111
kono
parents: 67
diff changeset
167 {
kono
parents: 67
diff changeset
168 num_threads = gomp_resolve_num_threads (num_threads, 0);
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
169 gomp_team_start (fn, data, num_threads, flags, gomp_new_team (num_threads),
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
170 NULL);
111
kono
parents: 67
diff changeset
171 fn (data);
kono
parents: 67
diff changeset
172 ialias_call (GOMP_parallel_end) ();
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
173 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
174
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
175 unsigned
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
176 GOMP_parallel_reductions (void (*fn) (void *), void *data,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
177 unsigned num_threads, unsigned int flags)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
178 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
179 struct gomp_taskgroup *taskgroup;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
180 num_threads = gomp_resolve_num_threads (num_threads, 0);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
181 uintptr_t *rdata = *(uintptr_t **)data;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
182 taskgroup = gomp_parallel_reduction_register (rdata, num_threads);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
183 gomp_team_start (fn, data, num_threads, flags, gomp_new_team (num_threads),
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
184 taskgroup);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
185 fn (data);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
186 ialias_call (GOMP_parallel_end) ();
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
187 gomp_sem_destroy (&taskgroup->taskgroup_sem);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
188 free (taskgroup);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
189 return num_threads;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
190 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
191
111
kono
parents: 67
diff changeset
192 bool
kono
parents: 67
diff changeset
193 GOMP_cancellation_point (int which)
kono
parents: 67
diff changeset
194 {
kono
parents: 67
diff changeset
195 if (!gomp_cancel_var)
kono
parents: 67
diff changeset
196 return false;
kono
parents: 67
diff changeset
197
kono
parents: 67
diff changeset
198 struct gomp_thread *thr = gomp_thread ();
kono
parents: 67
diff changeset
199 struct gomp_team *team = thr->ts.team;
kono
parents: 67
diff changeset
200 if (which & (GOMP_CANCEL_LOOP | GOMP_CANCEL_SECTIONS))
kono
parents: 67
diff changeset
201 {
kono
parents: 67
diff changeset
202 if (team == NULL)
kono
parents: 67
diff changeset
203 return false;
kono
parents: 67
diff changeset
204 return team->work_share_cancelled != 0;
kono
parents: 67
diff changeset
205 }
kono
parents: 67
diff changeset
206 else if (which & GOMP_CANCEL_TASKGROUP)
kono
parents: 67
diff changeset
207 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
208 if (thr->task->taskgroup)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
209 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
210 if (thr->task->taskgroup->cancelled)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
211 return true;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
212 if (thr->task->taskgroup->workshare
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
213 && thr->task->taskgroup->prev
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
214 && thr->task->taskgroup->prev->cancelled)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
215 return true;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
216 }
111
kono
parents: 67
diff changeset
217 /* FALLTHRU into the GOMP_CANCEL_PARALLEL case,
kono
parents: 67
diff changeset
218 as #pragma omp cancel parallel also cancels all explicit
kono
parents: 67
diff changeset
219 tasks. */
kono
parents: 67
diff changeset
220 }
kono
parents: 67
diff changeset
221 if (team)
kono
parents: 67
diff changeset
222 return gomp_team_barrier_cancelled (&team->barrier);
kono
parents: 67
diff changeset
223 return false;
kono
parents: 67
diff changeset
224 }
kono
parents: 67
diff changeset
225 ialias (GOMP_cancellation_point)
kono
parents: 67
diff changeset
226
kono
parents: 67
diff changeset
227 bool
kono
parents: 67
diff changeset
228 GOMP_cancel (int which, bool do_cancel)
kono
parents: 67
diff changeset
229 {
kono
parents: 67
diff changeset
230 if (!gomp_cancel_var)
kono
parents: 67
diff changeset
231 return false;
kono
parents: 67
diff changeset
232
kono
parents: 67
diff changeset
233 if (!do_cancel)
kono
parents: 67
diff changeset
234 return ialias_call (GOMP_cancellation_point) (which);
kono
parents: 67
diff changeset
235
kono
parents: 67
diff changeset
236 struct gomp_thread *thr = gomp_thread ();
kono
parents: 67
diff changeset
237 struct gomp_team *team = thr->ts.team;
kono
parents: 67
diff changeset
238 if (which & (GOMP_CANCEL_LOOP | GOMP_CANCEL_SECTIONS))
kono
parents: 67
diff changeset
239 {
kono
parents: 67
diff changeset
240 /* In orphaned worksharing region, all we want to cancel
kono
parents: 67
diff changeset
241 is current thread. */
kono
parents: 67
diff changeset
242 if (team != NULL)
kono
parents: 67
diff changeset
243 team->work_share_cancelled = 1;
kono
parents: 67
diff changeset
244 return true;
kono
parents: 67
diff changeset
245 }
kono
parents: 67
diff changeset
246 else if (which & GOMP_CANCEL_TASKGROUP)
kono
parents: 67
diff changeset
247 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
248 if (thr->task->taskgroup)
111
kono
parents: 67
diff changeset
249 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
250 struct gomp_taskgroup *taskgroup = thr->task->taskgroup;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
251 if (taskgroup->workshare && taskgroup->prev)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
252 taskgroup = taskgroup->prev;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
253 if (!taskgroup->cancelled)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
254 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
255 gomp_mutex_lock (&team->task_lock);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
256 taskgroup->cancelled = true;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
257 gomp_mutex_unlock (&team->task_lock);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
258 }
111
kono
parents: 67
diff changeset
259 }
kono
parents: 67
diff changeset
260 return true;
kono
parents: 67
diff changeset
261 }
kono
parents: 67
diff changeset
262 team->team_cancelled = 1;
kono
parents: 67
diff changeset
263 gomp_team_barrier_cancel (team);
kono
parents: 67
diff changeset
264 return true;
kono
parents: 67
diff changeset
265 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
266
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
267 /* The public OpenMP API for thread and team related inquiries. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
268
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
269 int
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
270 omp_get_num_threads (void)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
271 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
272 struct gomp_team *team = gomp_thread ()->ts.team;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
273 return team ? team->nthreads : 1;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
274 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
275
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
276 int
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
277 omp_get_thread_num (void)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
278 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
279 return gomp_thread ()->ts.team_id;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
280 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
281
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
282 /* This wasn't right for OpenMP 2.5. Active region used to be non-zero
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
283 when the IF clause doesn't evaluate to false, starting with OpenMP 3.0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
284 it is non-zero with more than one thread in the team. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
285
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
286 int
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
287 omp_in_parallel (void)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
288 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
289 return gomp_thread ()->ts.active_level > 0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
290 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
291
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
292 int
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
293 omp_get_level (void)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
294 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
295 return gomp_thread ()->ts.level;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
296 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
297
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
298 int
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
299 omp_get_ancestor_thread_num (int level)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
300 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
301 struct gomp_team_state *ts = &gomp_thread ()->ts;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
302 if (level < 0 || level > ts->level)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
303 return -1;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
304 for (level = ts->level - level; level > 0; --level)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
305 ts = &ts->team->prev_ts;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
306 return ts->team_id;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
307 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
308
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
309 int
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
310 omp_get_team_size (int level)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
311 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
312 struct gomp_team_state *ts = &gomp_thread ()->ts;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
313 if (level < 0 || level > ts->level)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
314 return -1;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
315 for (level = ts->level - level; level > 0; --level)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
316 ts = &ts->team->prev_ts;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
317 if (ts->team == NULL)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
318 return 1;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
319 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
320 return ts->team->nthreads;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
321 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
322
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
323 int
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
324 omp_get_active_level (void)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
325 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
326 return gomp_thread ()->ts.active_level;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
327 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
328
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
329 ialias (omp_get_num_threads)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
330 ialias (omp_get_thread_num)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
331 ialias (omp_in_parallel)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
332 ialias (omp_get_level)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
333 ialias (omp_get_ancestor_thread_num)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
334 ialias (omp_get_team_size)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
335 ialias (omp_get_active_level)