1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
/* Copyright (C) 2005, 2008 Free Software Foundation, Inc.
Contributed by Richard Henderson <rth@redhat.com>.
This file is part of the GNU OpenMP Library (libgomp).
Libgomp is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
more details.
You should have received a copy of the GNU Lesser General Public License
along with libgomp; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA. */
/* As a special exception, if you link this library with other files, some
of which are compiled with GCC, to produce an executable, this library
does not by itself cause the resulting executable to be covered by the
GNU General Public License. This exception does not however invalidate
any other reasons why the executable file might be covered by the GNU
General Public License. */
/* This file contains routines for managing work-share iteration, both
for loops and sections. */
#include "libgomp.h"
#include <stdlib.h>
typedef unsigned long long gomp_ull;
/* This function implements the STATIC scheduling method. The caller should
iterate *pstart <= x < *pend. Return zero if there are more iterations
to perform; nonzero if not. Return less than 0 if this thread had
received the absolutely last iteration. */
int
gomp_iter_ull_static_next (gomp_ull *pstart, gomp_ull *pend)
{
struct gomp_thread *thr = gomp_thread ();
struct gomp_team *team = thr->ts.team;
struct gomp_work_share *ws = thr->ts.work_share;
unsigned long nthreads = team ? team->nthreads : 1;
if (thr->ts.static_trip == -1)
return -1;
/* Quick test for degenerate teams and orphaned constructs. */
if (nthreads == 1)
{
*pstart = ws->next_ull;
*pend = ws->end_ull;
thr->ts.static_trip = -1;
return ws->next_ull == ws->end_ull;
}
/* We interpret chunk_size zero as "unspecified", which means that we
should break up the iterations such that each thread makes only one
trip through the outer loop. */
if (ws->chunk_size_ull == 0)
{
gomp_ull n, q, i, s0, e0, s, e;
if (thr->ts.static_trip > 0)
return 1;
/* Compute the total number of iterations. */
if (__builtin_expect (ws->mode, 0) == 0)
n = (ws->end_ull - ws->next_ull + ws->incr_ull - 1) / ws->incr_ull;
else
n = (ws->next_ull - ws->end_ull - ws->incr_ull - 1) / -ws->incr_ull;
i = thr->ts.team_id;
/* Compute the "zero-based" start and end points. That is, as
if the loop began at zero and incremented by one. */
q = n / nthreads;
q += (q * nthreads != n);
s0 = q * i;
e0 = s0 + q;
if (e0 > n)
e0 = n;
/* Notice when no iterations allocated for this thread. */
if (s0 >= e0)
{
thr->ts.static_trip = 1;
return 1;
}
/* Transform these to the actual start and end numbers. */
s = s0 * ws->incr_ull + ws->next_ull;
e = e0 * ws->incr_ull + ws->next_ull;
*pstart = s;
*pend = e;
thr->ts.static_trip = (e0 == n ? -1 : 1);
return 0;
}
else
{
gomp_ull n, s0, e0, i, c, s, e;
/* Otherwise, each thread gets exactly chunk_size iterations
(if available) each time through the loop. */
if (__builtin_expect (ws->mode, 0) == 0)
n = (ws->end_ull - ws->next_ull + ws->incr_ull - 1) / ws->incr_ull;
else
n = (ws->next_ull - ws->end_ull - ws->incr_ull - 1) / -ws->incr_ull;
i = thr->ts.team_id;
c = ws->chunk_size_ull;
/* Initial guess is a C sized chunk positioned nthreads iterations
in, offset by our thread number. */
s0 = (thr->ts.static_trip * (gomp_ull) nthreads + i) * c;
e0 = s0 + c;
/* Detect overflow. */
if (s0 >= n)
return 1;
if (e0 > n)
e0 = n;
/* Transform these to the actual start and end numbers. */
s = s0 * ws->incr_ull + ws->next_ull;
e = e0 * ws->incr_ull + ws->next_ull;
*pstart = s;
*pend = e;
if (e0 == n)
thr->ts.static_trip = -1;
else
thr->ts.static_trip++;
return 0;
}
}
/* This function implements the DYNAMIC scheduling method. Arguments are
as for gomp_iter_ull_static_next. This function must be called with
ws->lock held. */
bool
gomp_iter_ull_dynamic_next_locked (gomp_ull *pstart, gomp_ull *pend)
{
struct gomp_thread *thr = gomp_thread ();
struct gomp_work_share *ws = thr->ts.work_share;
gomp_ull start, end, chunk, left;
start = ws->next_ull;
if (start == ws->end_ull)
return false;
chunk = ws->chunk_size_ull;
left = ws->end_ull - start;
if (__builtin_expect (ws->mode & 2, 0))
{
if (chunk < left)
chunk = left;
}
else
{
if (chunk > left)
chunk = left;
}
end = start + chunk;
ws->next_ull = end;
*pstart = start;
*pend = end;
return true;
}
#if defined HAVE_SYNC_BUILTINS && defined __LP64__
/* Similar, but doesn't require the lock held, and uses compare-and-swap
instead. Note that the only memory value that changes is ws->next_ull. */
bool
gomp_iter_ull_dynamic_next (gomp_ull *pstart, gomp_ull *pend)
{
struct gomp_thread *thr = gomp_thread ();
struct gomp_work_share *ws = thr->ts.work_share;
gomp_ull start, end, nend, chunk;
end = ws->end_ull;
chunk = ws->chunk_size_ull;
if (__builtin_expect (ws->mode & 1, 1))
{
gomp_ull tmp = __sync_fetch_and_add (&ws->next_ull, chunk);
if (__builtin_expect (ws->mode & 2, 0) == 0)
{
if (tmp >= end)
return false;
nend = tmp + chunk;
if (nend > end)
nend = end;
*pstart = tmp;
*pend = nend;
return true;
}
else
{
if (tmp <= end)
return false;
nend = tmp + chunk;
if (nend < end)
nend = end;
*pstart = tmp;
*pend = nend;
return true;
}
}
start = ws->next_ull;
while (1)
{
gomp_ull left = end - start;
gomp_ull tmp;
if (start == end)
return false;
if (__builtin_expect (ws->mode & 2, 0))
{
if (chunk < left)
chunk = left;
}
else
{
if (chunk > left)
chunk = left;
}
nend = start + chunk;
tmp = __sync_val_compare_and_swap (&ws->next_ull, start, nend);
if (__builtin_expect (tmp == start, 1))
break;
start = tmp;
}
*pstart = start;
*pend = nend;
return true;
}
#endif /* HAVE_SYNC_BUILTINS */
/* This function implements the GUIDED scheduling method. Arguments are
as for gomp_iter_ull_static_next. This function must be called with the
work share lock held. */
bool
gomp_iter_ull_guided_next_locked (gomp_ull *pstart, gomp_ull *pend)
{
struct gomp_thread *thr = gomp_thread ();
struct gomp_work_share *ws = thr->ts.work_share;
struct gomp_team *team = thr->ts.team;
gomp_ull nthreads = team ? team->nthreads : 1;
gomp_ull n, q;
gomp_ull start, end;
if (ws->next_ull == ws->end_ull)
return false;
start = ws->next_ull;
if (__builtin_expect (ws->mode, 0) == 0)
n = (ws->end_ull - start) / ws->incr_ull;
else
n = (start - ws->end_ull) / -ws->incr_ull;
q = (n + nthreads - 1) / nthreads;
if (q < ws->chunk_size_ull)
q = ws->chunk_size_ull;
if (q <= n)
end = start + q * ws->incr_ull;
else
end = ws->end_ull;
ws->next_ull = end;
*pstart = start;
*pend = end;
return true;
}
#if defined HAVE_SYNC_BUILTINS && defined __LP64__
/* Similar, but doesn't require the lock held, and uses compare-and-swap
instead. Note that the only memory value that changes is ws->next_ull. */
bool
gomp_iter_ull_guided_next (gomp_ull *pstart, gomp_ull *pend)
{
struct gomp_thread *thr = gomp_thread ();
struct gomp_work_share *ws = thr->ts.work_share;
struct gomp_team *team = thr->ts.team;
gomp_ull nthreads = team ? team->nthreads : 1;
gomp_ull start, end, nend, incr;
gomp_ull chunk_size;
start = ws->next_ull;
end = ws->end_ull;
incr = ws->incr_ull;
chunk_size = ws->chunk_size_ull;
while (1)
{
gomp_ull n, q;
gomp_ull tmp;
if (start == end)
return false;
if (__builtin_expect (ws->mode, 0) == 0)
n = (end - start) / incr;
else
n = (start - end) / -incr;
q = (n + nthreads - 1) / nthreads;
if (q < chunk_size)
q = chunk_size;
if (__builtin_expect (q <= n, 1))
nend = start + q * incr;
else
nend = end;
tmp = __sync_val_compare_and_swap (&ws->next_ull, start, nend);
if (__builtin_expect (tmp == start, 1))
break;
start = tmp;
}
*pstart = start;
*pend = nend;
return true;
}
#endif /* HAVE_SYNC_BUILTINS */
|