1 /* Copyright (C) 2005-2013 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 General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
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 General Public License for
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 /* This file handles the LOOP (FOR/DO) construct. */
31 typedef unsigned long long gomp_ull
;
33 /* Initialize the given work share construct from the given arguments. */
36 gomp_loop_ull_init (struct gomp_work_share
*ws
, bool up
, gomp_ull start
,
37 gomp_ull end
, gomp_ull incr
, enum gomp_schedule_type sched
,
41 ws
->chunk_size_ull
= chunk_size
;
42 /* Canonicalize loops that have zero iterations to ->next == ->end. */
43 ws
->end_ull
= ((up
&& start
> end
) || (!up
&& start
< end
))
48 if (sched
== GFS_DYNAMIC
)
50 ws
->chunk_size_ull
*= incr
;
52 #if defined HAVE_SYNC_BUILTINS && defined __LP64__
54 /* For dynamic scheduling prepare things to make each iteration
56 struct gomp_thread
*thr
= gomp_thread ();
57 struct gomp_team
*team
= thr
->ts
.team
;
58 long nthreads
= team
? team
->nthreads
: 1;
60 if (__builtin_expect (up
, 1))
62 /* Cheap overflow protection. */
63 if (__builtin_expect ((nthreads
| ws
->chunk_size_ull
)
64 < 1ULL << (sizeof (gomp_ull
)
65 * __CHAR_BIT__
/ 2 - 1), 1))
66 ws
->mode
= ws
->end_ull
< (__LONG_LONG_MAX__
* 2ULL + 1
67 - (nthreads
+ 1) * ws
->chunk_size_ull
);
69 /* Cheap overflow protection. */
70 else if (__builtin_expect ((nthreads
| -ws
->chunk_size_ull
)
71 < 1ULL << (sizeof (gomp_ull
)
72 * __CHAR_BIT__
/ 2 - 1), 1))
73 ws
->mode
= ws
->end_ull
> ((nthreads
+ 1) * -ws
->chunk_size_ull
74 - (__LONG_LONG_MAX__
* 2ULL + 1));
82 /* The *_start routines are called when first encountering a loop construct
83 that is not bound directly to a parallel construct. The first thread
84 that arrives will create the work-share construct; subsequent threads
85 will see the construct exists and allocate work from it.
87 START, END, INCR are the bounds of the loop; due to the restrictions of
88 OpenMP, these values must be the same in every thread. This is not
89 verified (nor is it entirely verifiable, since START is not necessarily
90 retained intact in the work-share data structure). CHUNK_SIZE is the
91 scheduling parameter; again this must be identical in all threads.
93 Returns true if there's any work for this thread to perform. If so,
94 *ISTART and *IEND are filled with the bounds of the iteration block
95 allocated to this thread. Returns false if all work was assigned to
96 other threads prior to this thread's arrival. */
99 gomp_loop_ull_static_start (bool up
, gomp_ull start
, gomp_ull end
,
100 gomp_ull incr
, gomp_ull chunk_size
,
101 gomp_ull
*istart
, gomp_ull
*iend
)
103 struct gomp_thread
*thr
= gomp_thread ();
105 thr
->ts
.static_trip
= 0;
106 if (gomp_work_share_start (false))
108 gomp_loop_ull_init (thr
->ts
.work_share
, up
, start
, end
, incr
,
109 GFS_STATIC
, chunk_size
);
110 gomp_work_share_init_done ();
113 return !gomp_iter_ull_static_next (istart
, iend
);
117 gomp_loop_ull_dynamic_start (bool up
, gomp_ull start
, gomp_ull end
,
118 gomp_ull incr
, gomp_ull chunk_size
,
119 gomp_ull
*istart
, gomp_ull
*iend
)
121 struct gomp_thread
*thr
= gomp_thread ();
124 if (gomp_work_share_start (false))
126 gomp_loop_ull_init (thr
->ts
.work_share
, up
, start
, end
, incr
,
127 GFS_DYNAMIC
, chunk_size
);
128 gomp_work_share_init_done ();
131 #if defined HAVE_SYNC_BUILTINS && defined __LP64__
132 ret
= gomp_iter_ull_dynamic_next (istart
, iend
);
134 gomp_mutex_lock (&thr
->ts
.work_share
->lock
);
135 ret
= gomp_iter_ull_dynamic_next_locked (istart
, iend
);
136 gomp_mutex_unlock (&thr
->ts
.work_share
->lock
);
143 gomp_loop_ull_guided_start (bool up
, gomp_ull start
, gomp_ull end
,
144 gomp_ull incr
, gomp_ull chunk_size
,
145 gomp_ull
*istart
, gomp_ull
*iend
)
147 struct gomp_thread
*thr
= gomp_thread ();
150 if (gomp_work_share_start (false))
152 gomp_loop_ull_init (thr
->ts
.work_share
, up
, start
, end
, incr
,
153 GFS_GUIDED
, chunk_size
);
154 gomp_work_share_init_done ();
157 #if defined HAVE_SYNC_BUILTINS && defined __LP64__
158 ret
= gomp_iter_ull_guided_next (istart
, iend
);
160 gomp_mutex_lock (&thr
->ts
.work_share
->lock
);
161 ret
= gomp_iter_ull_guided_next_locked (istart
, iend
);
162 gomp_mutex_unlock (&thr
->ts
.work_share
->lock
);
169 GOMP_loop_ull_runtime_start (bool up
, gomp_ull start
, gomp_ull end
,
170 gomp_ull incr
, gomp_ull
*istart
, gomp_ull
*iend
)
172 struct gomp_task_icv
*icv
= gomp_icv (false);
173 switch (icv
->run_sched_var
)
176 return gomp_loop_ull_static_start (up
, start
, end
, incr
,
177 icv
->run_sched_modifier
,
180 return gomp_loop_ull_dynamic_start (up
, start
, end
, incr
,
181 icv
->run_sched_modifier
,
184 return gomp_loop_ull_guided_start (up
, start
, end
, incr
,
185 icv
->run_sched_modifier
,
188 /* For now map to schedule(static), later on we could play with feedback
190 return gomp_loop_ull_static_start (up
, start
, end
, incr
,
197 /* The *_ordered_*_start routines are similar. The only difference is that
198 this work-share construct is initialized to expect an ORDERED section. */
201 gomp_loop_ull_ordered_static_start (bool up
, gomp_ull start
, gomp_ull end
,
202 gomp_ull incr
, gomp_ull chunk_size
,
203 gomp_ull
*istart
, gomp_ull
*iend
)
205 struct gomp_thread
*thr
= gomp_thread ();
207 thr
->ts
.static_trip
= 0;
208 if (gomp_work_share_start (true))
210 gomp_loop_ull_init (thr
->ts
.work_share
, up
, start
, end
, incr
,
211 GFS_STATIC
, chunk_size
);
212 gomp_ordered_static_init ();
213 gomp_work_share_init_done ();
216 return !gomp_iter_ull_static_next (istart
, iend
);
220 gomp_loop_ull_ordered_dynamic_start (bool up
, gomp_ull start
, gomp_ull end
,
221 gomp_ull incr
, gomp_ull chunk_size
,
222 gomp_ull
*istart
, gomp_ull
*iend
)
224 struct gomp_thread
*thr
= gomp_thread ();
227 if (gomp_work_share_start (true))
229 gomp_loop_ull_init (thr
->ts
.work_share
, up
, start
, end
, incr
,
230 GFS_DYNAMIC
, chunk_size
);
231 gomp_mutex_lock (&thr
->ts
.work_share
->lock
);
232 gomp_work_share_init_done ();
235 gomp_mutex_lock (&thr
->ts
.work_share
->lock
);
237 ret
= gomp_iter_ull_dynamic_next_locked (istart
, iend
);
239 gomp_ordered_first ();
240 gomp_mutex_unlock (&thr
->ts
.work_share
->lock
);
246 gomp_loop_ull_ordered_guided_start (bool up
, gomp_ull start
, gomp_ull end
,
247 gomp_ull incr
, gomp_ull chunk_size
,
248 gomp_ull
*istart
, gomp_ull
*iend
)
250 struct gomp_thread
*thr
= gomp_thread ();
253 if (gomp_work_share_start (true))
255 gomp_loop_ull_init (thr
->ts
.work_share
, up
, start
, end
, incr
,
256 GFS_GUIDED
, chunk_size
);
257 gomp_mutex_lock (&thr
->ts
.work_share
->lock
);
258 gomp_work_share_init_done ();
261 gomp_mutex_lock (&thr
->ts
.work_share
->lock
);
263 ret
= gomp_iter_ull_guided_next_locked (istart
, iend
);
265 gomp_ordered_first ();
266 gomp_mutex_unlock (&thr
->ts
.work_share
->lock
);
272 GOMP_loop_ull_ordered_runtime_start (bool up
, gomp_ull start
, gomp_ull end
,
273 gomp_ull incr
, gomp_ull
*istart
,
276 struct gomp_task_icv
*icv
= gomp_icv (false);
277 switch (icv
->run_sched_var
)
280 return gomp_loop_ull_ordered_static_start (up
, start
, end
, incr
,
281 icv
->run_sched_modifier
,
284 return gomp_loop_ull_ordered_dynamic_start (up
, start
, end
, incr
,
285 icv
->run_sched_modifier
,
288 return gomp_loop_ull_ordered_guided_start (up
, start
, end
, incr
,
289 icv
->run_sched_modifier
,
292 /* For now map to schedule(static), later on we could play with feedback
294 return gomp_loop_ull_ordered_static_start (up
, start
, end
, incr
,
301 /* The *_next routines are called when the thread completes processing of
302 the iteration block currently assigned to it. If the work-share
303 construct is bound directly to a parallel construct, then the iteration
304 bounds may have been set up before the parallel. In which case, this
305 may be the first iteration for the thread.
307 Returns true if there is work remaining to be performed; *ISTART and
308 *IEND are filled with a new iteration block. Returns false if all work
309 has been assigned. */
312 gomp_loop_ull_static_next (gomp_ull
*istart
, gomp_ull
*iend
)
314 return !gomp_iter_ull_static_next (istart
, iend
);
318 gomp_loop_ull_dynamic_next (gomp_ull
*istart
, gomp_ull
*iend
)
322 #if defined HAVE_SYNC_BUILTINS && defined __LP64__
323 ret
= gomp_iter_ull_dynamic_next (istart
, iend
);
325 struct gomp_thread
*thr
= gomp_thread ();
326 gomp_mutex_lock (&thr
->ts
.work_share
->lock
);
327 ret
= gomp_iter_ull_dynamic_next_locked (istart
, iend
);
328 gomp_mutex_unlock (&thr
->ts
.work_share
->lock
);
335 gomp_loop_ull_guided_next (gomp_ull
*istart
, gomp_ull
*iend
)
339 #if defined HAVE_SYNC_BUILTINS && defined __LP64__
340 ret
= gomp_iter_ull_guided_next (istart
, iend
);
342 struct gomp_thread
*thr
= gomp_thread ();
343 gomp_mutex_lock (&thr
->ts
.work_share
->lock
);
344 ret
= gomp_iter_ull_guided_next_locked (istart
, iend
);
345 gomp_mutex_unlock (&thr
->ts
.work_share
->lock
);
352 GOMP_loop_ull_runtime_next (gomp_ull
*istart
, gomp_ull
*iend
)
354 struct gomp_thread
*thr
= gomp_thread ();
356 switch (thr
->ts
.work_share
->sched
)
360 return gomp_loop_ull_static_next (istart
, iend
);
362 return gomp_loop_ull_dynamic_next (istart
, iend
);
364 return gomp_loop_ull_guided_next (istart
, iend
);
370 /* The *_ordered_*_next routines are called when the thread completes
371 processing of the iteration block currently assigned to it.
373 Returns true if there is work remaining to be performed; *ISTART and
374 *IEND are filled with a new iteration block. Returns false if all work
375 has been assigned. */
378 gomp_loop_ull_ordered_static_next (gomp_ull
*istart
, gomp_ull
*iend
)
380 struct gomp_thread
*thr
= gomp_thread ();
383 gomp_ordered_sync ();
384 gomp_mutex_lock (&thr
->ts
.work_share
->lock
);
385 test
= gomp_iter_ull_static_next (istart
, iend
);
387 gomp_ordered_static_next ();
388 gomp_mutex_unlock (&thr
->ts
.work_share
->lock
);
394 gomp_loop_ull_ordered_dynamic_next (gomp_ull
*istart
, gomp_ull
*iend
)
396 struct gomp_thread
*thr
= gomp_thread ();
399 gomp_ordered_sync ();
400 gomp_mutex_lock (&thr
->ts
.work_share
->lock
);
401 ret
= gomp_iter_ull_dynamic_next_locked (istart
, iend
);
403 gomp_ordered_next ();
405 gomp_ordered_last ();
406 gomp_mutex_unlock (&thr
->ts
.work_share
->lock
);
412 gomp_loop_ull_ordered_guided_next (gomp_ull
*istart
, gomp_ull
*iend
)
414 struct gomp_thread
*thr
= gomp_thread ();
417 gomp_ordered_sync ();
418 gomp_mutex_lock (&thr
->ts
.work_share
->lock
);
419 ret
= gomp_iter_ull_guided_next_locked (istart
, iend
);
421 gomp_ordered_next ();
423 gomp_ordered_last ();
424 gomp_mutex_unlock (&thr
->ts
.work_share
->lock
);
430 GOMP_loop_ull_ordered_runtime_next (gomp_ull
*istart
, gomp_ull
*iend
)
432 struct gomp_thread
*thr
= gomp_thread ();
434 switch (thr
->ts
.work_share
->sched
)
438 return gomp_loop_ull_ordered_static_next (istart
, iend
);
440 return gomp_loop_ull_ordered_dynamic_next (istart
, iend
);
442 return gomp_loop_ull_ordered_guided_next (istart
, iend
);
448 /* We use static functions above so that we're sure that the "runtime"
449 function can defer to the proper routine without interposition. We
450 export the static function with a strong alias when possible, or with
451 a wrapper function otherwise. */
453 #ifdef HAVE_ATTRIBUTE_ALIAS
454 extern __typeof(gomp_loop_ull_static_start
) GOMP_loop_ull_static_start
455 __attribute__((alias ("gomp_loop_ull_static_start")));
456 extern __typeof(gomp_loop_ull_dynamic_start
) GOMP_loop_ull_dynamic_start
457 __attribute__((alias ("gomp_loop_ull_dynamic_start")));
458 extern __typeof(gomp_loop_ull_guided_start
) GOMP_loop_ull_guided_start
459 __attribute__((alias ("gomp_loop_ull_guided_start")));
461 extern __typeof(gomp_loop_ull_ordered_static_start
) GOMP_loop_ull_ordered_static_start
462 __attribute__((alias ("gomp_loop_ull_ordered_static_start")));
463 extern __typeof(gomp_loop_ull_ordered_dynamic_start
) GOMP_loop_ull_ordered_dynamic_start
464 __attribute__((alias ("gomp_loop_ull_ordered_dynamic_start")));
465 extern __typeof(gomp_loop_ull_ordered_guided_start
) GOMP_loop_ull_ordered_guided_start
466 __attribute__((alias ("gomp_loop_ull_ordered_guided_start")));
468 extern __typeof(gomp_loop_ull_static_next
) GOMP_loop_ull_static_next
469 __attribute__((alias ("gomp_loop_ull_static_next")));
470 extern __typeof(gomp_loop_ull_dynamic_next
) GOMP_loop_ull_dynamic_next
471 __attribute__((alias ("gomp_loop_ull_dynamic_next")));
472 extern __typeof(gomp_loop_ull_guided_next
) GOMP_loop_ull_guided_next
473 __attribute__((alias ("gomp_loop_ull_guided_next")));
475 extern __typeof(gomp_loop_ull_ordered_static_next
) GOMP_loop_ull_ordered_static_next
476 __attribute__((alias ("gomp_loop_ull_ordered_static_next")));
477 extern __typeof(gomp_loop_ull_ordered_dynamic_next
) GOMP_loop_ull_ordered_dynamic_next
478 __attribute__((alias ("gomp_loop_ull_ordered_dynamic_next")));
479 extern __typeof(gomp_loop_ull_ordered_guided_next
) GOMP_loop_ull_ordered_guided_next
480 __attribute__((alias ("gomp_loop_ull_ordered_guided_next")));
483 GOMP_loop_ull_static_start (bool up
, gomp_ull start
, gomp_ull end
,
484 gomp_ull incr
, gomp_ull chunk_size
,
485 gomp_ull
*istart
, gomp_ull
*iend
)
487 return gomp_loop_ull_static_start (up
, start
, end
, incr
, chunk_size
, istart
,
492 GOMP_loop_ull_dynamic_start (bool up
, gomp_ull start
, gomp_ull end
,
493 gomp_ull incr
, gomp_ull chunk_size
,
494 gomp_ull
*istart
, gomp_ull
*iend
)
496 return gomp_loop_ull_dynamic_start (up
, start
, end
, incr
, chunk_size
, istart
,
501 GOMP_loop_ull_guided_start (bool up
, gomp_ull start
, gomp_ull end
,
502 gomp_ull incr
, gomp_ull chunk_size
,
503 gomp_ull
*istart
, gomp_ull
*iend
)
505 return gomp_loop_ull_guided_start (up
, start
, end
, incr
, chunk_size
, istart
,
510 GOMP_loop_ull_ordered_static_start (bool up
, gomp_ull start
, gomp_ull end
,
511 gomp_ull incr
, gomp_ull chunk_size
,
512 gomp_ull
*istart
, gomp_ull
*iend
)
514 return gomp_loop_ull_ordered_static_start (up
, start
, end
, incr
, chunk_size
,
519 GOMP_loop_ull_ordered_dynamic_start (bool up
, gomp_ull start
, gomp_ull end
,
520 gomp_ull incr
, gomp_ull chunk_size
,
521 gomp_ull
*istart
, gomp_ull
*iend
)
523 return gomp_loop_ull_ordered_dynamic_start (up
, start
, end
, incr
, chunk_size
,
528 GOMP_loop_ull_ordered_guided_start (bool up
, gomp_ull start
, gomp_ull end
,
529 gomp_ull incr
, gomp_ull chunk_size
,
530 gomp_ull
*istart
, gomp_ull
*iend
)
532 return gomp_loop_ull_ordered_guided_start (up
, start
, end
, incr
, chunk_size
,
537 GOMP_loop_ull_static_next (gomp_ull
*istart
, gomp_ull
*iend
)
539 return gomp_loop_ull_static_next (istart
, iend
);
543 GOMP_loop_ull_dynamic_next (gomp_ull
*istart
, gomp_ull
*iend
)
545 return gomp_loop_ull_dynamic_next (istart
, iend
);
549 GOMP_loop_ull_guided_next (gomp_ull
*istart
, gomp_ull
*iend
)
551 return gomp_loop_ull_guided_next (istart
, iend
);
555 GOMP_loop_ull_ordered_static_next (gomp_ull
*istart
, gomp_ull
*iend
)
557 return gomp_loop_ull_ordered_static_next (istart
, iend
);
561 GOMP_loop_ull_ordered_dynamic_next (gomp_ull
*istart
, gomp_ull
*iend
)
563 return gomp_loop_ull_ordered_dynamic_next (istart
, iend
);
567 GOMP_loop_ull_ordered_guided_next (gomp_ull
*istart
, gomp_ull
*iend
)
569 return gomp_loop_ull_ordered_guided_next (istart
, iend
);