30#include "kmp_dispatch.h"
32#include "kmp_dispatch_hier.h"
36#include "ompt-specific.h"
42void __kmp_dispatch_deo_error(
int *gtid_ref,
int *cid_ref,
ident_t *loc_ref) {
45 KMP_DEBUG_ASSERT(gtid_ref);
47 if (__kmp_env_consistency_check) {
48 th = __kmp_threads[*gtid_ref];
49 if (th->th.th_root->r.r_active &&
50 (th->th.th_dispatch->th_dispatch_pr_current->pushed_ws != ct_none)) {
51#if KMP_USE_DYNAMIC_LOCK
52 __kmp_push_sync(*gtid_ref, ct_ordered_in_pdo, loc_ref, NULL, 0);
54 __kmp_push_sync(*gtid_ref, ct_ordered_in_pdo, loc_ref, NULL);
60void __kmp_dispatch_dxo_error(
int *gtid_ref,
int *cid_ref,
ident_t *loc_ref) {
63 if (__kmp_env_consistency_check) {
64 th = __kmp_threads[*gtid_ref];
65 if (th->th.th_dispatch->th_dispatch_pr_current->pushed_ws != ct_none) {
66 __kmp_pop_sync(*gtid_ref, ct_ordered_in_pdo, loc_ref);
73 bool use_hier =
false) {
76 int monotonicity = SCHEDULE_NONMONOTONIC;
80 if (loc != NULL && loc->get_openmp_version() < 50)
81 monotonicity = SCHEDULE_MONOTONIC;
83 if (use_hier || __kmp_force_monotonic)
84 monotonicity = SCHEDULE_MONOTONIC;
85 else if (SCHEDULE_HAS_NONMONOTONIC(schedule))
86 monotonicity = SCHEDULE_NONMONOTONIC;
87 else if (SCHEDULE_HAS_MONOTONIC(schedule))
88 monotonicity = SCHEDULE_MONOTONIC;
93#if KMP_WEIGHTED_ITERATIONS_SUPPORTED
95static inline float __kmp_round_2decimal_val(
float num) {
96 return (
float)(
static_cast<int>(num * 100 + 0.5)) / 100;
98static inline int __kmp_get_round_val(
float num) {
99 return static_cast<int>(num < 0 ? num - 0.5 : num + 0.5);
105__kmp_initialize_self_buffer(kmp_team_t *team, T
id,
106 dispatch_private_info_template<T> *pr,
107 typename traits_t<T>::unsigned_t nchunks, T nproc,
108 typename traits_t<T>::unsigned_t &init,
109 T &small_chunk, T &extras, T &p_extra) {
111#if KMP_WEIGHTED_ITERATIONS_SUPPORTED
112 if (pr->flags.use_hybrid) {
113 kmp_info_t *th = __kmp_threads[__kmp_gtid_from_tid((
int)
id, team)];
114 kmp_hw_core_type_t type =
115 (kmp_hw_core_type_t)th->th.th_topology_attrs.core_type;
116 T pchunks = pr->u.p.pchunks;
117 T echunks = nchunks - pchunks;
118 T num_procs_with_pcore = pr->u.p.num_procs_with_pcore;
119 T num_procs_with_ecore = nproc - num_procs_with_pcore;
120 T first_thread_with_ecore = pr->u.p.first_thread_with_ecore;
122 pchunks / num_procs_with_pcore;
124 echunks / num_procs_with_ecore;
127 (pchunks % num_procs_with_pcore) + (echunks % num_procs_with_ecore);
129 p_extra = (big_chunk - small_chunk);
131 if (type == KMP_HW_CORE_TYPE_CORE) {
132 if (
id < first_thread_with_ecore) {
133 init =
id * small_chunk +
id * p_extra + (
id < extras ? id : extras);
135 init =
id * small_chunk + (
id - num_procs_with_ecore) * p_extra +
136 (
id < extras ?
id : extras);
139 if (
id == first_thread_with_ecore) {
140 init =
id * small_chunk +
id * p_extra + (
id < extras ? id : extras);
142 init =
id * small_chunk + first_thread_with_ecore * p_extra +
143 (
id < extras ? id : extras);
146 p_extra = (type == KMP_HW_CORE_TYPE_CORE) ? p_extra : 0;
151 small_chunk = nchunks / nproc;
152 extras = nchunks % nproc;
154 init =
id * small_chunk + (
id < extras ? id : extras);
157#if KMP_STATIC_STEAL_ENABLED
184void __kmp_dispatch_init_algorithm(
ident_t *loc,
int gtid,
185 dispatch_private_info_template<T> *pr,
187 typename traits_t<T>::signed_t st,
189 kmp_uint64 *cur_chunk,
191 typename traits_t<T>::signed_t chunk,
193 typedef typename traits_t<T>::unsigned_t UT;
194 typedef typename traits_t<T>::floating_t DBL;
204 typedef typename traits_t<T>::signed_t ST;
208 buff = __kmp_str_format(
"__kmp_dispatch_init_algorithm: T#%%d called "
209 "pr:%%p lb:%%%s ub:%%%s st:%%%s "
210 "schedule:%%d chunk:%%%s nproc:%%%s tid:%%%s\n",
211 traits_t<T>::spec, traits_t<T>::spec,
212 traits_t<ST>::spec, traits_t<ST>::spec,
213 traits_t<T>::spec, traits_t<T>::spec);
214 KD_TRACE(10, (buff, gtid, pr, lb, ub, st, schedule, chunk, nproc, tid));
215 __kmp_str_free(&buff);
219 th = __kmp_threads[gtid];
220 team = th->th.th_team;
221 active = !team->t.t_serialized;
224 int itt_need_metadata_reporting =
225 __itt_metadata_add_ptr && __kmp_forkjoin_frames_mode == 3 &&
226 KMP_MASTER_GTID(gtid) && th->th.th_teams_microtask == NULL &&
227 team->t.t_active_level == 1;
230#if KMP_USE_HIER_SCHED
231 use_hier = pr->flags.use_hier;
237 monotonicity = __kmp_get_monotonicity(loc, schedule, use_hier);
238 schedule = SCHEDULE_WITHOUT_MODIFIERS(schedule);
242 pr->flags.nomerge = TRUE;
246 pr->flags.nomerge = FALSE;
248 pr->type_size = traits_t<T>::type_size;
250 pr->flags.ordered = TRUE;
254 pr->flags.ordered = FALSE;
257 if (pr->flags.ordered) {
258 monotonicity = SCHEDULE_MONOTONIC;
262 schedule = __kmp_static;
264 if (schedule == kmp_sch_runtime) {
267 schedule = team->t.t_sched.r_sched_type;
268 monotonicity = __kmp_get_monotonicity(loc, schedule, use_hier);
269 schedule = SCHEDULE_WITHOUT_MODIFIERS(schedule);
270 if (pr->flags.ordered)
271 monotonicity = SCHEDULE_MONOTONIC;
275 schedule = __kmp_guided;
277 schedule = __kmp_static;
281 chunk = team->t.t_sched.chunk;
290 buff = __kmp_str_format(
"__kmp_dispatch_init_algorithm: T#%%d new: "
291 "schedule:%%d chunk:%%%s\n",
293 KD_TRACE(10, (buff, gtid, schedule, chunk));
294 __kmp_str_free(&buff);
299 schedule = __kmp_guided;
302 chunk = KMP_DEFAULT_CHUNK;
308 schedule = __kmp_auto;
313 buff = __kmp_str_format(
314 "__kmp_dispatch_init_algorithm: kmp_sch_auto: T#%%d new: "
315 "schedule:%%d chunk:%%%s\n",
317 KD_TRACE(10, (buff, gtid, schedule, chunk));
318 __kmp_str_free(&buff);
322#if KMP_STATIC_STEAL_ENABLED
324 if (schedule == kmp_sch_dynamic_chunked) {
325 if (monotonicity == SCHEDULE_NONMONOTONIC)
326 schedule = kmp_sch_static_steal;
330 if (schedule == kmp_sch_guided_analytical_chunked && nproc > 1 << 20) {
331 schedule = kmp_sch_guided_iterative_chunked;
332 KMP_WARNING(DispatchManyThreads);
336 schedule = team->t.t_sched.r_sched_type;
337 monotonicity = __kmp_get_monotonicity(loc, schedule, use_hier);
338 schedule = SCHEDULE_WITHOUT_MODIFIERS(schedule);
342 schedule == __kmp_static) {
343 schedule = kmp_sch_static_balanced_chunked;
348 chunk = team->t.t_sched.chunk * chunk;
358 buff = __kmp_str_format(
359 "__kmp_dispatch_init_algorithm: T#%%d new: schedule:%%d"
362 KD_TRACE(10, (buff, gtid, schedule, chunk));
363 __kmp_str_free(&buff);
367 pr->u.p.parm1 = chunk;
370 "unknown scheduling type");
374 if (__kmp_env_consistency_check) {
376 __kmp_error_construct(kmp_i18n_msg_CnsLoopIncrZeroProhibited,
377 (pr->flags.ordered ? ct_pdo_ordered : ct_pdo), loc);
391 tc = (UT)(lb - ub) / (-st) + 1;
399 tc = (UT)(ub - lb) / st + 1;
406 if (KMP_MASTER_GTID(gtid)) {
417 pr->u.p.last_upper = ub + st;
423 if (pr->flags.ordered) {
424 pr->ordered_bumped = 0;
425 pr->u.p.ordered_lower = 1;
426 pr->u.p.ordered_upper = 0;
431#if KMP_STATIC_STEAL_ENABLED
432 case kmp_sch_static_steal: {
436 (
"__kmp_dispatch_init_algorithm: T#%d kmp_sch_static_steal case\n",
439 ntc = (tc % chunk ? 1 : 0) + tc / chunk;
440 if (nproc > 1 && ntc >= nproc) {
443 T small_chunk, extras, p_extra = 0;
444 kmp_uint32 old = UNUSED;
445 int claimed = pr->steal_flag.compare_exchange_strong(old, CLAIMED);
446 if (traits_t<T>::type_size > 4) {
452 pr->u.p.steal_lock = (kmp_lock_t *)__kmp_allocate(
sizeof(kmp_lock_t));
453 __kmp_init_lock(pr->u.p.steal_lock);
456#if KMP_WEIGHTED_ITERATIONS_SUPPORTED
459 bool use_hybrid =
false;
460 kmp_hw_core_type_t core_type = KMP_HW_CORE_TYPE_UNKNOWN;
461 T first_thread_with_ecore = 0;
462 T num_procs_with_pcore = 0;
463 T num_procs_with_ecore = 0;
464 T p_ntc = 0, e_ntc = 0;
465 if (__kmp_is_hybrid_cpu() && __kmp_affinity.type != affinity_none &&
466 __kmp_affinity.type != affinity_explicit) {
468 core_type = (kmp_hw_core_type_t)th->th.th_topology_attrs.core_type;
469 if (core_type != KMP_HW_CORE_TYPE_UNKNOWN &&
470 __kmp_first_osid_with_ecore > -1) {
471 for (
int i = 0; i < team->t.t_nproc; ++i) {
472 kmp_hw_core_type_t type = (kmp_hw_core_type_t)team->t.t_threads[i]
473 ->th.th_topology_attrs.core_type;
474 int id = team->t.t_threads[i]->th.th_topology_ids.os_id;
475 if (
id == __kmp_first_osid_with_ecore) {
476 first_thread_with_ecore =
477 team->t.t_threads[i]->th.th_info.ds.ds_tid;
479 if (type == KMP_HW_CORE_TYPE_CORE) {
480 num_procs_with_pcore++;
481 }
else if (type == KMP_HW_CORE_TYPE_ATOM) {
482 num_procs_with_ecore++;
489 if (num_procs_with_pcore > 0 && num_procs_with_ecore > 0) {
490 float multiplier = 60.0 / 40.0;
491 float p_ratio = (float)num_procs_with_pcore / nproc;
492 float e_ratio = (float)num_procs_with_ecore / nproc;
495 (((multiplier * num_procs_with_pcore) / nproc) + e_ratio);
496 float p_multiplier = multiplier * e_multiplier;
497 p_ntc = __kmp_get_round_val(ntc * p_ratio * p_multiplier);
498 if ((
int)p_ntc > (
int)(ntc * p_ratio * p_multiplier))
500 (int)(__kmp_round_2decimal_val(ntc * e_ratio * e_multiplier));
502 e_ntc = __kmp_get_round_val(ntc * e_ratio * e_multiplier);
503 KMP_DEBUG_ASSERT(ntc == p_ntc + e_ntc);
507 use_hybrid = (use_hybrid && (p_ntc >= num_procs_with_pcore &&
508 e_ntc >= num_procs_with_ecore)
515 pr->flags.use_hybrid = use_hybrid;
516 pr->u.p.pchunks = p_ntc;
517 pr->u.p.num_procs_with_pcore = num_procs_with_pcore;
518 pr->u.p.first_thread_with_ecore = first_thread_with_ecore;
521 KMP_DEBUG_ASSERT(nproc == num_procs_with_pcore + num_procs_with_ecore);
522 T big_chunk = p_ntc / num_procs_with_pcore;
523 small_chunk = e_ntc / num_procs_with_ecore;
526 (p_ntc % num_procs_with_pcore) + (e_ntc % num_procs_with_ecore);
528 p_extra = (big_chunk - small_chunk);
530 if (core_type == KMP_HW_CORE_TYPE_CORE) {
531 if (
id < first_thread_with_ecore) {
533 id * small_chunk +
id * p_extra + (
id < extras ? id : extras);
535 init =
id * small_chunk + (
id - num_procs_with_ecore) * p_extra +
536 (
id < extras ?
id : extras);
539 if (
id == first_thread_with_ecore) {
541 id * small_chunk +
id * p_extra + (
id < extras ? id : extras);
543 init =
id * small_chunk + first_thread_with_ecore * p_extra +
544 (
id < extras ? id : extras);
547 p_extra = (core_type == KMP_HW_CORE_TYPE_CORE) ? p_extra : 0;
551 small_chunk = ntc / nproc;
552 extras = ntc % nproc;
553 init =
id * small_chunk + (
id < extras ? id : extras);
556 pr->u.p.count = init;
558 pr->u.p.ub = init + small_chunk + p_extra + (
id < extras ? 1 : 0);
561 KMP_ATOMIC_ST_REL(&pr->steal_flag, READY);
564 KMP_DEBUG_ASSERT(pr->steal_flag == THIEF);
570 pr->u.p.parm3 = nproc;
571 pr->u.p.parm4 = (
id + 1) % nproc;
575 schedule = kmp_sch_dynamic_chunked;
576 KD_TRACE(100, (
"__kmp_dispatch_init_algorithm: T#%d switching to "
577 "kmp_sch_dynamic_chunked\n",
584 case kmp_sch_static_balanced: {
589 (
"__kmp_dispatch_init_algorithm: T#%d kmp_sch_static_balanced case\n",
599 pr->u.p.parm1 = (
id == tc - 1);
602 pr->u.p.parm1 = FALSE;
606 T small_chunk = tc / nproc;
607 T extras = tc % nproc;
608 init =
id * small_chunk + (
id < extras ? id : extras);
609 limit = init + small_chunk - (
id < extras ? 0 : 1);
610 pr->u.p.parm1 = (
id == nproc - 1);
616 pr->u.p.parm1 = TRUE;
620 pr->u.p.parm1 = FALSE;
626 if (itt_need_metadata_reporting)
628 *cur_chunk = limit - init + 1;
631 pr->u.p.lb = lb + init;
632 pr->u.p.ub = lb + limit;
635 T ub_tmp = lb + limit * st;
636 pr->u.p.lb = lb + init * st;
640 pr->u.p.ub = (ub_tmp + st > ub ? ub : ub_tmp);
642 pr->u.p.ub = (ub_tmp + st < ub ? ub : ub_tmp);
645 if (pr->flags.ordered) {
646 pr->u.p.ordered_lower = init;
647 pr->u.p.ordered_upper = limit;
651 case kmp_sch_static_balanced_chunked: {
654 KD_TRACE(100, (
"__kmp_dispatch_init_algorithm: T#%d runtime(simd:static)"
655 " -> falling-through to static_greedy\n",
657 schedule = kmp_sch_static_greedy;
659 pr->u.p.parm1 = ((tc + nth - 1) / nth + chunk - 1) & ~(chunk - 1);
665 case kmp_sch_guided_iterative_chunked: {
668 (
"__kmp_dispatch_init_algorithm: T#%d kmp_sch_guided_iterative_chunked"
673 if ((2L * chunk + 1) * nproc >= tc) {
675 schedule = kmp_sch_dynamic_chunked;
679 pr->u.p.parm2 = guided_int_param * nproc * (chunk + 1);
680 *(
double *)&pr->u.p.parm3 =
681 guided_flt_param / (
double)nproc;
684 KD_TRACE(100, (
"__kmp_dispatch_init_algorithm: T#%d falling-through to "
685 "kmp_sch_static_greedy\n",
687 schedule = kmp_sch_static_greedy;
691 (
"__kmp_dispatch_init_algorithm: T#%d kmp_sch_static_greedy case\n",
697 case kmp_sch_guided_analytical_chunked: {
698 KD_TRACE(100, (
"__kmp_dispatch_init_algorithm: T#%d "
699 "kmp_sch_guided_analytical_chunked case\n",
703 if ((2L * chunk + 1) * nproc >= tc) {
705 schedule = kmp_sch_dynamic_chunked;
711#if KMP_USE_X87CONTROL
721 unsigned int oldFpcw = _control87(0, 0);
722 _control87(_PC_64, _MCW_PC);
726 long double target = ((
long double)chunk * 2 + 1) * nproc / tc;
733 x = 1.0 - 0.5 / (double)nproc;
744 ptrdiff_t natural_alignment =
745 (ptrdiff_t)&t.b - (ptrdiff_t)&t - (ptrdiff_t)1;
749 (((ptrdiff_t)&pr->u.p.parm3) & (natural_alignment)) == 0);
754 *(DBL *)&pr->u.p.parm3 = x;
767 p = __kmp_pow<UT>(x, right);
772 }
while (p > target && right < (1 << 27));
780 while (left + 1 < right) {
781 mid = (left + right) / 2;
782 if (__kmp_pow<UT>(x, mid) > target) {
791 KMP_ASSERT(cross && __kmp_pow<UT>(x, cross - 1) > target &&
792 __kmp_pow<UT>(x, cross) <= target);
795 pr->u.p.parm2 = cross;
798#if ((KMP_OS_LINUX || KMP_OS_WINDOWS) && KMP_ARCH_X86) && (!defined(KMP_I8))
799#define GUIDED_ANALYTICAL_WORKAROUND (*(DBL *)&pr->u.p.parm3)
801#define GUIDED_ANALYTICAL_WORKAROUND (x)
805 __kmp_dispatch_guided_remaining(
806 tc, GUIDED_ANALYTICAL_WORKAROUND, cross) -
808#if KMP_USE_X87CONTROL
810 _control87(oldFpcw, _MCW_PC);
814 KD_TRACE(100, (
"__kmp_dispatch_init_algorithm: T#%d falling-through to "
815 "kmp_sch_static_greedy\n",
817 schedule = kmp_sch_static_greedy;
823 case kmp_sch_static_greedy:
826 (
"__kmp_dispatch_init_algorithm: T#%d kmp_sch_static_greedy case\n",
828 pr->u.p.parm1 = (nproc > 1) ? (tc + nproc - 1) / nproc : tc;
830 case kmp_sch_static_chunked:
831 case kmp_sch_dynamic_chunked:
835 if (pr->u.p.parm1 <= 0)
836 pr->u.p.parm1 = KMP_DEFAULT_CHUNK;
837 else if (pr->u.p.parm1 > tc)
841 pr->u.p.parm2 = (tc / pr->u.p.parm1) + (tc % pr->u.p.parm1 ? 1 : 0);
842 KD_TRACE(100, (
"__kmp_dispatch_init_algorithm: T#%d "
843 "kmp_sch_static_chunked/kmp_sch_dynamic_chunked cases\n",
846 case kmp_sch_trapezoidal: {
849 T parm1, parm2, parm3, parm4;
851 (
"__kmp_dispatch_init_algorithm: T#%d kmp_sch_trapezoidal case\n",
857 parm2 = (tc / (2 * nproc));
867 }
else if (parm1 > parm2) {
872 parm3 = (parm2 + parm1);
873 parm3 = (2 * tc + parm3 - 1) / parm3;
881 parm4 = (parm2 - parm1) / parm4;
888 pr->u.p.parm1 = parm1;
889 pr->u.p.parm2 = parm2;
890 pr->u.p.parm3 = parm3;
891 pr->u.p.parm4 = parm4;
896 __kmp_fatal(KMP_MSG(UnknownSchedTypeDetected),
897 KMP_HNT(GetNewerLibrary),
902 pr->schedule = schedule;
905#if KMP_USE_HIER_SCHED
907inline void __kmp_dispatch_init_hier_runtime(
ident_t *loc, T lb, T ub,
908 typename traits_t<T>::signed_t st);
911__kmp_dispatch_init_hier_runtime<kmp_int32>(
ident_t *loc, kmp_int32 lb,
912 kmp_int32 ub, kmp_int32 st) {
913 __kmp_dispatch_init_hierarchy<kmp_int32>(
914 loc, __kmp_hier_scheds.size, __kmp_hier_scheds.layers,
915 __kmp_hier_scheds.scheds, __kmp_hier_scheds.small_chunks, lb, ub, st);
919__kmp_dispatch_init_hier_runtime<kmp_uint32>(
ident_t *loc, kmp_uint32 lb,
920 kmp_uint32 ub, kmp_int32 st) {
921 __kmp_dispatch_init_hierarchy<kmp_uint32>(
922 loc, __kmp_hier_scheds.size, __kmp_hier_scheds.layers,
923 __kmp_hier_scheds.scheds, __kmp_hier_scheds.small_chunks, lb, ub, st);
927__kmp_dispatch_init_hier_runtime<kmp_int64>(
ident_t *loc, kmp_int64 lb,
928 kmp_int64 ub, kmp_int64 st) {
929 __kmp_dispatch_init_hierarchy<kmp_int64>(
930 loc, __kmp_hier_scheds.size, __kmp_hier_scheds.layers,
931 __kmp_hier_scheds.scheds, __kmp_hier_scheds.large_chunks, lb, ub, st);
935__kmp_dispatch_init_hier_runtime<kmp_uint64>(
ident_t *loc, kmp_uint64 lb,
936 kmp_uint64 ub, kmp_int64 st) {
937 __kmp_dispatch_init_hierarchy<kmp_uint64>(
938 loc, __kmp_hier_scheds.size, __kmp_hier_scheds.layers,
939 __kmp_hier_scheds.scheds, __kmp_hier_scheds.large_chunks, lb, ub, st);
943void __kmp_dispatch_free_hierarchies(kmp_team_t *team) {
944 int num_disp_buff = team->t.t_max_nproc > 1 ? __kmp_dispatch_num_buffers : 2;
945 for (
int i = 0; i < num_disp_buff; ++i) {
948 reinterpret_cast<dispatch_shared_info_template<kmp_int32>
volatile *
>(
949 &team->t.t_disp_buffer[i]);
951 sh->hier->deallocate();
952 __kmp_free(sh->hier);
963 T ub,
typename traits_t<T>::signed_t st,
964 typename traits_t<T>::signed_t chunk,
int push_ws) {
965 typedef typename traits_t<T>::unsigned_t UT;
970 kmp_uint32 my_buffer_index;
971 dispatch_private_info_template<T> *pr;
972 dispatch_shared_info_template<T>
volatile *sh;
974 KMP_BUILD_ASSERT(
sizeof(dispatch_private_info_template<T>) ==
975 sizeof(dispatch_private_info));
976 KMP_BUILD_ASSERT(
sizeof(dispatch_shared_info_template<UT>) ==
977 sizeof(dispatch_shared_info));
978 __kmp_assert_valid_gtid(gtid);
980 if (!TCR_4(__kmp_init_parallel))
981 __kmp_parallel_initialize();
983 __kmp_resume_if_soft_paused();
986 SSC_MARK_DISPATCH_INIT();
989 typedef typename traits_t<T>::signed_t ST;
993 buff = __kmp_str_format(
"__kmp_dispatch_init: T#%%d called: schedule:%%d "
994 "chunk:%%%s lb:%%%s ub:%%%s st:%%%s\n",
995 traits_t<ST>::spec, traits_t<T>::spec,
996 traits_t<T>::spec, traits_t<ST>::spec);
997 KD_TRACE(10, (buff, gtid, schedule, chunk, lb, ub, st));
998 __kmp_str_free(&buff);
1002 th = __kmp_threads[gtid];
1003 team = th->th.th_team;
1004 active = !team->t.t_serialized;
1005 th->th.th_ident = loc;
1010 if (schedule == __kmp_static) {
1016#if KMP_USE_HIER_SCHED
1022 my_buffer_index = th->th.th_dispatch->th_disp_index;
1023 pr =
reinterpret_cast<dispatch_private_info_template<T> *
>(
1025 ->th_disp_buffer[my_buffer_index % __kmp_dispatch_num_buffers]);
1026 my_sched = SCHEDULE_WITHOUT_MODIFIERS(my_sched);
1031 if (pr->flags.use_hier) {
1033 KD_TRACE(100, (
"__kmp_dispatch_init: T#%d ordered loop detected. "
1034 "Disabling hierarchical scheduling.\n",
1036 pr->flags.use_hier = FALSE;
1039 if (schedule == kmp_sch_runtime && __kmp_hier_scheds.size > 0) {
1042 if (!ordered && !pr->flags.use_hier)
1043 __kmp_dispatch_init_hier_runtime<T>(loc, lb, ub, st);
1048 kmp_uint64 cur_chunk = chunk;
1049 int itt_need_metadata_reporting =
1050 __itt_metadata_add_ptr && __kmp_forkjoin_frames_mode == 3 &&
1051 KMP_MASTER_GTID(gtid) && th->th.th_teams_microtask == NULL &&
1052 team->t.t_active_level == 1;
1055 pr =
reinterpret_cast<dispatch_private_info_template<T> *
>(
1056 th->th.th_dispatch->th_disp_buffer);
1058 KMP_DEBUG_ASSERT(th->th.th_dispatch ==
1059 &th->th.th_team->t.t_dispatch[th->th.th_info.ds.ds_tid]);
1061 my_buffer_index = th->th.th_dispatch->th_disp_index++;
1064 pr =
reinterpret_cast<dispatch_private_info_template<T> *
>(
1066 ->th_disp_buffer[my_buffer_index % __kmp_dispatch_num_buffers]);
1067 sh =
reinterpret_cast<dispatch_shared_info_template<T>
volatile *
>(
1068 &team->t.t_disp_buffer[my_buffer_index % __kmp_dispatch_num_buffers]);
1069 KD_TRACE(10, (
"__kmp_dispatch_init: T#%d my_buffer_index:%d\n", gtid,
1071 if (sh->buffer_index != my_buffer_index) {
1072 KD_TRACE(100, (
"__kmp_dispatch_init: T#%d before wait: my_buffer_index:%d"
1073 " sh->buffer_index:%d\n",
1074 gtid, my_buffer_index, sh->buffer_index));
1075 __kmp_wait<kmp_uint32>(&sh->buffer_index, my_buffer_index,
1076 __kmp_eq<kmp_uint32> USE_ITT_BUILD_ARG(NULL));
1079 KD_TRACE(100, (
"__kmp_dispatch_init: T#%d after wait: my_buffer_index:%d "
1080 "sh->buffer_index:%d\n",
1081 gtid, my_buffer_index, sh->buffer_index));
1085 __kmp_dispatch_init_algorithm(loc, gtid, pr, schedule, lb, ub, st,
1089 chunk, (T)th->th.th_team_nproc,
1090 (T)th->th.th_info.ds.ds_tid);
1092 if (pr->flags.ordered == 0) {
1093 th->th.th_dispatch->th_deo_fcn = __kmp_dispatch_deo_error;
1094 th->th.th_dispatch->th_dxo_fcn = __kmp_dispatch_dxo_error;
1096 th->th.th_dispatch->th_deo_fcn = __kmp_dispatch_deo<UT>;
1097 th->th.th_dispatch->th_dxo_fcn = __kmp_dispatch_dxo<UT>;
1099 th->th.th_dispatch->th_dispatch_pr_current = (dispatch_private_info_t *)pr;
1100 th->th.th_dispatch->th_dispatch_sh_current =
1101 CCAST(dispatch_shared_info_t *, (
volatile dispatch_shared_info_t *)sh);
1103 if (pr->flags.ordered) {
1104 __kmp_itt_ordered_init(gtid);
1107 if (itt_need_metadata_reporting) {
1109 kmp_uint64 schedtype = 0;
1111 case kmp_sch_static_chunked:
1112 case kmp_sch_static_balanced:
1114 case kmp_sch_static_greedy:
1115 cur_chunk = pr->u.p.parm1;
1117 case kmp_sch_dynamic_chunked:
1120 case kmp_sch_guided_iterative_chunked:
1121 case kmp_sch_guided_analytical_chunked:
1131 __kmp_itt_metadata_loop(loc, schedtype, pr->u.p.tc, cur_chunk);
1133#if KMP_USE_HIER_SCHED
1134 if (pr->flags.use_hier) {
1136 pr->u.p.ub = pr->u.p.lb = pr->u.p.st = pr->u.p.tc = 0;
1146 buff = __kmp_str_format(
1147 "__kmp_dispatch_init: T#%%d returning: schedule:%%d ordered:%%%s "
1149 " st:%%%s tc:%%%s count:%%%s\n\tordered_lower:%%%s ordered_upper:%%%s"
1150 " parm1:%%%s parm2:%%%s parm3:%%%s parm4:%%%s\n",
1151 traits_t<UT>::spec, traits_t<T>::spec, traits_t<T>::spec,
1152 traits_t<ST>::spec, traits_t<UT>::spec, traits_t<UT>::spec,
1153 traits_t<UT>::spec, traits_t<UT>::spec, traits_t<T>::spec,
1154 traits_t<T>::spec, traits_t<T>::spec, traits_t<T>::spec);
1155 KD_TRACE(10, (buff, gtid, pr->schedule, pr->flags.ordered, pr->u.p.lb,
1156 pr->u.p.ub, pr->u.p.st, pr->u.p.tc, pr->u.p.count,
1157 pr->u.p.ordered_lower, pr->u.p.ordered_upper, pr->u.p.parm1,
1158 pr->u.p.parm2, pr->u.p.parm3, pr->u.p.parm4));
1159 __kmp_str_free(&buff);
1162#if OMPT_SUPPORT && OMPT_OPTIONAL
1163 if (ompt_enabled.ompt_callback_work) {
1164 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
1165 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
1166 ompt_callbacks.ompt_callback(ompt_callback_work)(
1167 ompt_work_loop, ompt_scope_begin, &(team_info->parallel_data),
1168 &(task_info->task_data), pr->u.p.tc, OMPT_LOAD_RETURN_ADDRESS(gtid));
1171 KMP_PUSH_PARTITIONED_TIMER(OMP_loop_dynamic);
1179template <
typename UT>
1180static void __kmp_dispatch_finish(
int gtid,
ident_t *loc) {
1181 typedef typename traits_t<UT>::signed_t ST;
1182 __kmp_assert_valid_gtid(gtid);
1183 kmp_info_t *th = __kmp_threads[gtid];
1185 KD_TRACE(100, (
"__kmp_dispatch_finish: T#%d called\n", gtid));
1186 if (!th->th.th_team->t.t_serialized) {
1188 dispatch_private_info_template<UT> *pr =
1189 reinterpret_cast<dispatch_private_info_template<UT> *
>(
1190 th->th.th_dispatch->th_dispatch_pr_current);
1191 dispatch_shared_info_template<UT>
volatile *sh =
1192 reinterpret_cast<dispatch_shared_info_template<UT>
volatile *
>(
1193 th->th.th_dispatch->th_dispatch_sh_current);
1194 KMP_DEBUG_ASSERT(pr);
1195 KMP_DEBUG_ASSERT(sh);
1196 KMP_DEBUG_ASSERT(th->th.th_dispatch ==
1197 &th->th.th_team->t.t_dispatch[th->th.th_info.ds.ds_tid]);
1199 if (pr->ordered_bumped) {
1202 (
"__kmp_dispatch_finish: T#%d resetting ordered_bumped to zero\n",
1204 pr->ordered_bumped = 0;
1206 UT lower = pr->u.p.ordered_lower;
1212 buff = __kmp_str_format(
"__kmp_dispatch_finish: T#%%d before wait: "
1213 "ordered_iteration:%%%s lower:%%%s\n",
1214 traits_t<UT>::spec, traits_t<UT>::spec);
1215 KD_TRACE(1000, (buff, gtid, sh->u.s.ordered_iteration, lower));
1216 __kmp_str_free(&buff);
1220 __kmp_wait<UT>(&sh->u.s.ordered_iteration, lower,
1221 __kmp_ge<UT> USE_ITT_BUILD_ARG(NULL));
1227 buff = __kmp_str_format(
"__kmp_dispatch_finish: T#%%d after wait: "
1228 "ordered_iteration:%%%s lower:%%%s\n",
1229 traits_t<UT>::spec, traits_t<UT>::spec);
1230 KD_TRACE(1000, (buff, gtid, sh->u.s.ordered_iteration, lower));
1231 __kmp_str_free(&buff);
1235 test_then_inc<ST>((
volatile ST *)&sh->u.s.ordered_iteration);
1238 KD_TRACE(100, (
"__kmp_dispatch_finish: T#%d returned\n", gtid));
1241#ifdef KMP_GOMP_COMPAT
1243template <
typename UT>
1244static void __kmp_dispatch_finish_chunk(
int gtid,
ident_t *loc) {
1245 typedef typename traits_t<UT>::signed_t ST;
1246 __kmp_assert_valid_gtid(gtid);
1247 kmp_info_t *th = __kmp_threads[gtid];
1249 KD_TRACE(100, (
"__kmp_dispatch_finish_chunk: T#%d called\n", gtid));
1250 if (!th->th.th_team->t.t_serialized) {
1251 dispatch_private_info_template<UT> *pr =
1252 reinterpret_cast<dispatch_private_info_template<UT> *
>(
1253 th->th.th_dispatch->th_dispatch_pr_current);
1254 dispatch_shared_info_template<UT>
volatile *sh =
1255 reinterpret_cast<dispatch_shared_info_template<UT>
volatile *
>(
1256 th->th.th_dispatch->th_dispatch_sh_current);
1257 KMP_DEBUG_ASSERT(pr);
1258 KMP_DEBUG_ASSERT(sh);
1259 KMP_DEBUG_ASSERT(th->th.th_dispatch ==
1260 &th->th.th_team->t.t_dispatch[th->th.th_info.ds.ds_tid]);
1262 UT lower = pr->u.p.ordered_lower;
1263 UT upper = pr->u.p.ordered_upper;
1264 UT inc = upper - lower + 1;
1266 if (pr->ordered_bumped == inc) {
1269 (
"__kmp_dispatch_finish: T#%d resetting ordered_bumped to zero\n",
1271 pr->ordered_bumped = 0;
1273 inc -= pr->ordered_bumped;
1279 buff = __kmp_str_format(
1280 "__kmp_dispatch_finish_chunk: T#%%d before wait: "
1281 "ordered_iteration:%%%s lower:%%%s upper:%%%s\n",
1282 traits_t<UT>::spec, traits_t<UT>::spec, traits_t<UT>::spec);
1283 KD_TRACE(1000, (buff, gtid, sh->u.s.ordered_iteration, lower, upper));
1284 __kmp_str_free(&buff);
1288 __kmp_wait<UT>(&sh->u.s.ordered_iteration, lower,
1289 __kmp_ge<UT> USE_ITT_BUILD_ARG(NULL));
1292 KD_TRACE(1000, (
"__kmp_dispatch_finish_chunk: T#%d resetting "
1293 "ordered_bumped to zero\n",
1295 pr->ordered_bumped = 0;
1301 buff = __kmp_str_format(
1302 "__kmp_dispatch_finish_chunk: T#%%d after wait: "
1303 "ordered_iteration:%%%s inc:%%%s lower:%%%s upper:%%%s\n",
1304 traits_t<UT>::spec, traits_t<UT>::spec, traits_t<UT>::spec,
1305 traits_t<UT>::spec);
1307 (buff, gtid, sh->u.s.ordered_iteration, inc, lower, upper));
1308 __kmp_str_free(&buff);
1312 test_then_add<ST>((
volatile ST *)&sh->u.s.ordered_iteration, inc);
1316 KD_TRACE(100, (
"__kmp_dispatch_finish_chunk: T#%d returned\n", gtid));
1321template <
typename T>
1322int __kmp_dispatch_next_algorithm(
int gtid,
1323 dispatch_private_info_template<T> *pr,
1324 dispatch_shared_info_template<T>
volatile *sh,
1325 kmp_int32 *p_last, T *p_lb, T *p_ub,
1326 typename traits_t<T>::signed_t *p_st, T nproc,
1328 typedef typename traits_t<T>::unsigned_t UT;
1329 typedef typename traits_t<T>::signed_t ST;
1330 typedef typename traits_t<T>::floating_t DBL;
1335 UT limit, trip, init;
1336 kmp_info_t *th = __kmp_threads[gtid];
1337 kmp_team_t *team = th->th.th_team;
1339 KMP_DEBUG_ASSERT(th->th.th_dispatch ==
1340 &th->th.th_team->t.t_dispatch[th->th.th_info.ds.ds_tid]);
1341 KMP_DEBUG_ASSERT(pr);
1342 KMP_DEBUG_ASSERT(sh);
1343 KMP_DEBUG_ASSERT(tid >= 0 && tid < nproc);
1349 __kmp_str_format(
"__kmp_dispatch_next_algorithm: T#%%d called pr:%%p "
1350 "sh:%%p nproc:%%%s tid:%%%s\n",
1351 traits_t<T>::spec, traits_t<T>::spec);
1352 KD_TRACE(10, (buff, gtid, pr, sh, nproc, tid));
1353 __kmp_str_free(&buff);
1358 if (pr->u.p.tc == 0) {
1360 (
"__kmp_dispatch_next_algorithm: T#%d early exit trip count is "
1366 switch (pr->schedule) {
1367#if KMP_STATIC_STEAL_ENABLED
1368 case kmp_sch_static_steal: {
1369 T chunk = pr->u.p.parm1;
1370 UT nchunks = pr->u.p.parm2;
1372 (
"__kmp_dispatch_next_algorithm: T#%d kmp_sch_static_steal case\n",
1375 trip = pr->u.p.tc - 1;
1377 if (traits_t<T>::type_size > 4) {
1380 kmp_lock_t *lck = pr->u.p.steal_lock;
1381 KMP_DEBUG_ASSERT(lck != NULL);
1382 if (pr->u.p.count < (UT)pr->u.p.ub) {
1383 KMP_DEBUG_ASSERT(pr->steal_flag == READY);
1384 __kmp_acquire_lock(lck, gtid);
1386 init = (pr->u.p.count)++;
1387 status = (init < (UT)pr->u.p.ub);
1388 __kmp_release_lock(lck, gtid);
1394 T while_limit = pr->u.p.parm3;
1396 int idx = (th->th.th_dispatch->th_disp_index - 1) %
1397 __kmp_dispatch_num_buffers;
1399 KMP_ATOMIC_ST_REL(&pr->steal_flag, THIEF);
1400 while ((!status) && (while_limit != ++while_index)) {
1401 dispatch_private_info_template<T> *v;
1403 T victimId = pr->u.p.parm4;
1404 T oldVictimId = victimId ? victimId - 1 : nproc - 1;
1405 v =
reinterpret_cast<dispatch_private_info_template<T> *
>(
1406 &team->t.t_dispatch[victimId].th_disp_buffer[idx]);
1407 KMP_DEBUG_ASSERT(v);
1408 while ((v == pr || KMP_ATOMIC_LD_RLX(&v->steal_flag) == THIEF) &&
1409 oldVictimId != victimId) {
1410 victimId = (victimId + 1) % nproc;
1411 v =
reinterpret_cast<dispatch_private_info_template<T> *
>(
1412 &team->t.t_dispatch[victimId].th_disp_buffer[idx]);
1413 KMP_DEBUG_ASSERT(v);
1415 if (v == pr || KMP_ATOMIC_LD_RLX(&v->steal_flag) == THIEF) {
1418 if (KMP_ATOMIC_LD_RLX(&v->steal_flag) == UNUSED) {
1419 kmp_uint32 old = UNUSED;
1421 status = v->steal_flag.compare_exchange_strong(old, THIEF);
1425 T small_chunk = 0, extras = 0, p_extra = 0;
1426 __kmp_initialize_self_buffer<T>(team,
id, pr, nchunks, nproc,
1427 init, small_chunk, extras,
1429 __kmp_acquire_lock(lck, gtid);
1430 pr->u.p.count = init + 1;
1431 pr->u.p.ub = init + small_chunk + p_extra + (
id < extras ? 1 : 0);
1432 __kmp_release_lock(lck, gtid);
1433 pr->u.p.parm4 = (
id + 1) % nproc;
1439 buff = __kmp_str_format(
"__kmp_dispatch_next_algorithm: T#%%d "
1440 "stolen chunks from T#%%d, "
1441 "count:%%%s ub:%%%s\n",
1442 traits_t<UT>::spec, traits_t<T>::spec);
1443 KD_TRACE(10, (buff, gtid,
id, pr->u.p.count, pr->u.p.ub));
1444 __kmp_str_free(&buff);
1448 if (pr->u.p.count < (UT)pr->u.p.ub)
1449 KMP_ATOMIC_ST_REL(&pr->steal_flag, READY);
1453 if (KMP_ATOMIC_LD_ACQ(&v->steal_flag) != READY ||
1454 v->u.p.count >= (UT)v->u.p.ub) {
1455 pr->u.p.parm4 = (victimId + 1) % nproc;
1458 lckv = v->u.p.steal_lock;
1459 KMP_ASSERT(lckv != NULL);
1460 __kmp_acquire_lock(lckv, gtid);
1462 if (v->u.p.count >= limit) {
1463 __kmp_release_lock(lckv, gtid);
1464 pr->u.p.parm4 = (victimId + 1) % nproc;
1470 remaining = limit - v->u.p.count;
1471 if (remaining > 7) {
1473 KMP_COUNT_DEVELOPER_VALUE(FOR_static_steal_stolen, remaining >> 2);
1474 init = (v->u.p.ub -= (remaining >> 2));
1477 KMP_COUNT_DEVELOPER_VALUE(FOR_static_steal_stolen, 1);
1478 init = (v->u.p.ub -= 1);
1480 __kmp_release_lock(lckv, gtid);
1485 buff = __kmp_str_format(
1486 "__kmp_dispatch_next: T#%%d stolen chunks from T#%%d, "
1487 "count:%%%s ub:%%%s\n",
1488 traits_t<UT>::spec, traits_t<UT>::spec);
1489 KD_TRACE(10, (buff, gtid, victimId, init, limit));
1490 __kmp_str_free(&buff);
1493 KMP_DEBUG_ASSERT(init + 1 <= limit);
1494 pr->u.p.parm4 = victimId;
1497 __kmp_acquire_lock(lck, gtid);
1498 pr->u.p.count = init + 1;
1500 __kmp_release_lock(lck, gtid);
1502 if (init + 1 < limit)
1503 KMP_ATOMIC_ST_REL(&pr->steal_flag, READY);
1516 union_i4 vold, vnew;
1517 if (pr->u.p.count < (UT)pr->u.p.ub) {
1518 KMP_DEBUG_ASSERT(pr->steal_flag == READY);
1519 vold.b = *(
volatile kmp_int64 *)(&pr->u.p.count);
1522 while (!KMP_COMPARE_AND_STORE_REL64(
1523 (
volatile kmp_int64 *)&pr->u.p.count,
1524 *VOLATILE_CAST(kmp_int64 *) & vold.b,
1525 *VOLATILE_CAST(kmp_int64 *) & vnew.b)) {
1527 vold.b = *(
volatile kmp_int64 *)(&pr->u.p.count);
1531 init = vold.p.count;
1532 status = (init < (UT)vold.p.ub);
1537 T while_limit = pr->u.p.parm3;
1539 int idx = (th->th.th_dispatch->th_disp_index - 1) %
1540 __kmp_dispatch_num_buffers;
1542 KMP_ATOMIC_ST_REL(&pr->steal_flag, THIEF);
1543 while ((!status) && (while_limit != ++while_index)) {
1544 dispatch_private_info_template<T> *v;
1546 T victimId = pr->u.p.parm4;
1547 T oldVictimId = victimId ? victimId - 1 : nproc - 1;
1548 v =
reinterpret_cast<dispatch_private_info_template<T> *
>(
1549 &team->t.t_dispatch[victimId].th_disp_buffer[idx]);
1550 KMP_DEBUG_ASSERT(v);
1551 while ((v == pr || KMP_ATOMIC_LD_RLX(&v->steal_flag) == THIEF) &&
1552 oldVictimId != victimId) {
1553 victimId = (victimId + 1) % nproc;
1554 v =
reinterpret_cast<dispatch_private_info_template<T> *
>(
1555 &team->t.t_dispatch[victimId].th_disp_buffer[idx]);
1556 KMP_DEBUG_ASSERT(v);
1558 if (v == pr || KMP_ATOMIC_LD_RLX(&v->steal_flag) == THIEF) {
1561 if (KMP_ATOMIC_LD_RLX(&v->steal_flag) == UNUSED) {
1562 kmp_uint32 old = UNUSED;
1564 status = v->steal_flag.compare_exchange_strong(old, THIEF);
1568 T small_chunk = 0, extras = 0, p_extra = 0;
1569 __kmp_initialize_self_buffer<T>(team,
id, pr, nchunks, nproc,
1570 init, small_chunk, extras,
1572 vnew.p.count = init + 1;
1573 vnew.p.ub = init + small_chunk + p_extra + (
id < extras ? 1 : 0);
1576 KMP_XCHG_FIXED64((
volatile kmp_int64 *)(&pr->u.p.count), vnew.b);
1578 *(
volatile kmp_int64 *)(&pr->u.p.count) = vnew.b;
1580 pr->u.p.parm4 = (
id + 1) % nproc;
1586 buff = __kmp_str_format(
"__kmp_dispatch_next_algorithm: T#%%d "
1587 "stolen chunks from T#%%d, "
1588 "count:%%%s ub:%%%s\n",
1589 traits_t<UT>::spec, traits_t<T>::spec);
1590 KD_TRACE(10, (buff, gtid,
id, pr->u.p.count, pr->u.p.ub));
1591 __kmp_str_free(&buff);
1595 if (pr->u.p.count < (UT)pr->u.p.ub)
1596 KMP_ATOMIC_ST_REL(&pr->steal_flag, READY);
1602 vold.b = *(
volatile kmp_int64 *)(&v->u.p.count);
1603 if (KMP_ATOMIC_LD_ACQ(&v->steal_flag) != READY ||
1604 vold.p.count >= (UT)vold.p.ub) {
1605 pr->u.p.parm4 = (victimId + 1) % nproc;
1609 remaining = vold.p.ub - vold.p.count;
1612 if (remaining > 7) {
1613 vnew.p.ub -= remaining >> 2;
1617 KMP_DEBUG_ASSERT(vnew.p.ub * (UT)chunk <= trip);
1618 if (KMP_COMPARE_AND_STORE_REL64(
1619 (
volatile kmp_int64 *)&v->u.p.count,
1620 *VOLATILE_CAST(kmp_int64 *) & vold.b,
1621 *VOLATILE_CAST(kmp_int64 *) & vnew.b)) {
1627 buff = __kmp_str_format(
1628 "__kmp_dispatch_next: T#%%d stolen chunks from T#%%d, "
1629 "count:%%%s ub:%%%s\n",
1630 traits_t<T>::spec, traits_t<T>::spec);
1631 KD_TRACE(10, (buff, gtid, victimId, vnew.p.ub, vold.p.ub));
1632 __kmp_str_free(&buff);
1635 KMP_COUNT_DEVELOPER_VALUE(FOR_static_steal_stolen,
1636 vold.p.ub - vnew.p.ub);
1638 pr->u.p.parm4 = victimId;
1641 vold.p.count = init + 1;
1643 KMP_XCHG_FIXED64((
volatile kmp_int64 *)(&pr->u.p.count), vold.b);
1645 *(
volatile kmp_int64 *)(&pr->u.p.count) = vold.b;
1648 if (vold.p.count < (UT)vold.p.ub)
1649 KMP_ATOMIC_ST_REL(&pr->steal_flag, READY);
1665 limit = chunk + init - 1;
1667 KMP_COUNT_DEVELOPER_VALUE(FOR_static_steal_chunks, 1);
1669 KMP_DEBUG_ASSERT(init <= trip);
1673 if ((last = (limit >= trip)) != 0)
1679 *p_lb = start + init;
1680 *p_ub = start + limit;
1682 *p_lb = start + init * incr;
1683 *p_ub = start + limit * incr;
1689 case kmp_sch_static_balanced: {
1692 (
"__kmp_dispatch_next_algorithm: T#%d kmp_sch_static_balanced case\n",
1695 if ((status = !pr->u.p.count) != 0) {
1699 last = (pr->u.p.parm1 != 0);
1703 pr->u.p.lb = pr->u.p.ub + pr->u.p.st;
1707 case kmp_sch_static_greedy:
1709 case kmp_sch_static_chunked: {
1712 KD_TRACE(100, (
"__kmp_dispatch_next_algorithm: T#%d "
1713 "kmp_sch_static_[affinity|chunked] case\n",
1715 parm1 = pr->u.p.parm1;
1717 trip = pr->u.p.tc - 1;
1718 init = parm1 * (pr->u.p.count + tid);
1720 if ((status = (init <= trip)) != 0) {
1723 limit = parm1 + init - 1;
1725 if ((last = (limit >= trip)) != 0)
1731 pr->u.p.count += nproc;
1734 *p_lb = start + init;
1735 *p_ub = start + limit;
1737 *p_lb = start + init * incr;
1738 *p_ub = start + limit * incr;
1741 if (pr->flags.ordered) {
1742 pr->u.p.ordered_lower = init;
1743 pr->u.p.ordered_upper = limit;
1749 case kmp_sch_dynamic_chunked: {
1751 UT chunk_size = pr->u.p.parm1;
1752 UT nchunks = pr->u.p.parm2;
1756 (
"__kmp_dispatch_next_algorithm: T#%d kmp_sch_dynamic_chunked case\n",
1759 chunk_number = test_then_inc_acq<ST>((
volatile ST *)&sh->u.s.iteration);
1760 status = (chunk_number < nchunks);
1767 init = chunk_size * chunk_number;
1768 trip = pr->u.p.tc - 1;
1772 if ((last = (trip - init < (UT)chunk_size)))
1775 limit = chunk_size + init - 1;
1781 *p_lb = start + init;
1782 *p_ub = start + limit;
1784 *p_lb = start + init * incr;
1785 *p_ub = start + limit * incr;
1788 if (pr->flags.ordered) {
1789 pr->u.p.ordered_lower = init;
1790 pr->u.p.ordered_upper = limit;
1796 case kmp_sch_guided_iterative_chunked: {
1797 T chunkspec = pr->u.p.parm1;
1798 KD_TRACE(100, (
"__kmp_dispatch_next_algorithm: T#%d kmp_sch_guided_chunked "
1805 init = sh->u.s.iteration;
1806 remaining = trip - init;
1807 if (remaining <= 0) {
1816 init = test_then_add<ST>(RCAST(
volatile ST *, &sh->u.s.iteration),
1818 remaining = trip - init;
1819 if (remaining <= 0) {
1824 if ((T)remaining > chunkspec) {
1825 limit = init + chunkspec - 1;
1828 limit = init + remaining - 1;
1833 limit = init + (UT)((
double)remaining *
1834 *(
double *)&pr->u.p.parm3);
1835 if (compare_and_swap<ST>(RCAST(
volatile ST *, &sh->u.s.iteration),
1836 (ST)init, (ST)limit)) {
1848 *p_lb = start + init * incr;
1849 *p_ub = start + limit * incr;
1850 if (pr->flags.ordered) {
1851 pr->u.p.ordered_lower = init;
1852 pr->u.p.ordered_upper = limit;
1866 T chunk = pr->u.p.parm1;
1868 (
"__kmp_dispatch_next_algorithm: T#%d kmp_sch_guided_simd case\n",
1874 init = sh->u.s.iteration;
1875 remaining = trip - init;
1876 if (remaining <= 0) {
1880 KMP_DEBUG_ASSERT(chunk && init % chunk == 0);
1882 if ((T)remaining < pr->u.p.parm2) {
1885 init = test_then_add<ST>(RCAST(
volatile ST *, &sh->u.s.iteration),
1887 remaining = trip - init;
1888 if (remaining <= 0) {
1893 if ((T)remaining > chunk) {
1894 limit = init + chunk - 1;
1897 limit = init + remaining - 1;
1904 __kmp_type_convert((
double)remaining * (*(
double *)&pr->u.p.parm3),
1906 UT rem = span % chunk;
1908 span += chunk - rem;
1909 limit = init + span;
1910 if (compare_and_swap<ST>(RCAST(
volatile ST *, &sh->u.s.iteration),
1911 (ST)init, (ST)limit)) {
1923 *p_lb = start + init * incr;
1924 *p_ub = start + limit * incr;
1925 if (pr->flags.ordered) {
1926 pr->u.p.ordered_lower = init;
1927 pr->u.p.ordered_upper = limit;
1938 case kmp_sch_guided_analytical_chunked: {
1939 T chunkspec = pr->u.p.parm1;
1941#if KMP_USE_X87CONTROL
1944 unsigned int oldFpcw;
1945 unsigned int fpcwSet = 0;
1947 KD_TRACE(100, (
"__kmp_dispatch_next_algorithm: T#%d "
1948 "kmp_sch_guided_analytical_chunked case\n",
1953 KMP_DEBUG_ASSERT(nproc > 1);
1954 KMP_DEBUG_ASSERT((2UL * chunkspec + 1) * (UT)nproc < trip);
1958 chunkIdx = test_then_inc_acq<ST>((
volatile ST *)&sh->u.s.iteration);
1959 if (chunkIdx >= (UT)pr->u.p.parm2) {
1962 init = chunkIdx * chunkspec + pr->u.p.count;
1965 if ((status = (init > 0 && init <= trip)) != 0) {
1966 limit = init + chunkspec - 1;
1968 if ((last = (limit >= trip)) != 0)
1978#if KMP_USE_X87CONTROL
1983 oldFpcw = _control87(0, 0);
1984 _control87(_PC_64, _MCW_PC);
1989 init = __kmp_dispatch_guided_remaining<T>(
1990 trip, *(DBL *)&pr->u.p.parm3, chunkIdx);
1991 KMP_DEBUG_ASSERT(init);
1995 limit = trip - __kmp_dispatch_guided_remaining<T>(
1996 trip, *(DBL *)&pr->u.p.parm3, chunkIdx + 1);
1997 KMP_ASSERT(init <= limit);
1999 KMP_DEBUG_ASSERT(limit <= trip);
2006#if KMP_USE_X87CONTROL
2010 if (fpcwSet && (oldFpcw & fpcwSet))
2011 _control87(oldFpcw, _MCW_PC);
2018 *p_lb = start + init * incr;
2019 *p_ub = start + limit * incr;
2020 if (pr->flags.ordered) {
2021 pr->u.p.ordered_lower = init;
2022 pr->u.p.ordered_upper = limit;
2033 case kmp_sch_trapezoidal: {
2035 T parm2 = pr->u.p.parm2;
2036 T parm3 = pr->u.p.parm3;
2037 T parm4 = pr->u.p.parm4;
2039 (
"__kmp_dispatch_next_algorithm: T#%d kmp_sch_trapezoidal case\n",
2042 index = test_then_inc<ST>((
volatile ST *)&sh->u.s.iteration);
2044 init = (index * ((2 * parm2) - (index - 1) * parm4)) / 2;
2045 trip = pr->u.p.tc - 1;
2047 if ((status = ((T)index < parm3 && init <= trip)) == 0) {
2054 limit = ((index + 1) * (2 * parm2 - index * parm4)) / 2 - 1;
2057 if ((last = (limit >= trip)) != 0)
2064 *p_lb = start + init;
2065 *p_ub = start + limit;
2067 *p_lb = start + init * incr;
2068 *p_ub = start + limit * incr;
2071 if (pr->flags.ordered) {
2072 pr->u.p.ordered_lower = init;
2073 pr->u.p.ordered_upper = limit;
2080 __kmp_fatal(KMP_MSG(UnknownSchedTypeDetected),
2081 KMP_HNT(GetNewerLibrary),
2089 if (pr->flags.ordered) {
2092 buff = __kmp_str_format(
"__kmp_dispatch_next_algorithm: T#%%d "
2093 "ordered_lower:%%%s ordered_upper:%%%s\n",
2094 traits_t<UT>::spec, traits_t<UT>::spec);
2095 KD_TRACE(1000, (buff, gtid, pr->u.p.ordered_lower, pr->u.p.ordered_upper));
2096 __kmp_str_free(&buff);
2101 buff = __kmp_str_format(
2102 "__kmp_dispatch_next_algorithm: T#%%d exit status:%%d p_last:%%d "
2103 "p_lb:%%%s p_ub:%%%s p_st:%%%s\n",
2104 traits_t<T>::spec, traits_t<T>::spec, traits_t<ST>::spec);
2105 KMP_DEBUG_ASSERT(p_last);
2106 KMP_DEBUG_ASSERT(p_st);
2107 KD_TRACE(10, (buff, gtid, status, *p_last, *p_lb, *p_ub, *p_st));
2108 __kmp_str_free(&buff);
2117#if OMPT_SUPPORT && OMPT_OPTIONAL
2118#define OMPT_LOOP_END \
2119 if (status == 0) { \
2120 if (ompt_enabled.ompt_callback_work) { \
2121 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL); \
2122 ompt_task_info_t *task_info = __ompt_get_task_info_object(0); \
2123 ompt_callbacks.ompt_callback(ompt_callback_work)( \
2124 ompt_work_loop, ompt_scope_end, &(team_info->parallel_data), \
2125 &(task_info->task_data), 0, codeptr); \
2128#define OMPT_LOOP_DISPATCH(lb, ub, st, status) \
2129 if (ompt_enabled.ompt_callback_dispatch && status) { \
2130 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL); \
2131 ompt_task_info_t *task_info = __ompt_get_task_info_object(0); \
2132 ompt_dispatch_chunk_t chunk; \
2133 ompt_data_t instance = ompt_data_none; \
2134 OMPT_GET_DISPATCH_CHUNK(chunk, lb, ub, st); \
2135 instance.ptr = &chunk; \
2136 ompt_callbacks.ompt_callback(ompt_callback_dispatch)( \
2137 &(team_info->parallel_data), &(task_info->task_data), \
2138 ompt_dispatch_ws_loop_chunk, instance); \
2142#define OMPT_LOOP_END
2143#define OMPT_LOOP_DISPATCH(lb, ub, st, status)
2146#if KMP_STATS_ENABLED
2147#define KMP_STATS_LOOP_END \
2149 kmp_int64 u, l, t, i; \
2150 l = (kmp_int64)(*p_lb); \
2151 u = (kmp_int64)(*p_ub); \
2152 i = (kmp_int64)(pr->u.p.st); \
2153 if (status == 0) { \
2155 KMP_POP_PARTITIONED_TIMER(); \
2156 } else if (i == 1) { \
2161 } else if (i < 0) { \
2163 t = (l - u) / (-i) + 1; \
2168 t = (u - l) / i + 1; \
2172 KMP_COUNT_VALUE(OMP_loop_dynamic_iterations, t); \
2175#define KMP_STATS_LOOP_END
2178template <
typename T>
2179static int __kmp_dispatch_next(
ident_t *loc,
int gtid, kmp_int32 *p_last,
2181 typename traits_t<T>::signed_t *p_st
2182#
if OMPT_SUPPORT && OMPT_OPTIONAL
2188 typedef typename traits_t<T>::unsigned_t UT;
2189 typedef typename traits_t<T>::signed_t ST;
2194 KMP_TIME_PARTITIONED_BLOCK(OMP_loop_dynamic_scheduling);
2197 dispatch_private_info_template<T> *pr;
2198 __kmp_assert_valid_gtid(gtid);
2199 kmp_info_t *th = __kmp_threads[gtid];
2200 kmp_team_t *team = th->th.th_team;
2202 KMP_DEBUG_ASSERT(p_lb && p_ub && p_st);
2205 (
"__kmp_dispatch_next: T#%d called p_lb:%p p_ub:%p p_st:%p p_last: %p\n",
2206 gtid, p_lb, p_ub, p_st, p_last));
2208 if (team->t.t_serialized) {
2210 pr =
reinterpret_cast<dispatch_private_info_template<T> *
>(
2211 th->th.th_dispatch->th_disp_buffer);
2212 KMP_DEBUG_ASSERT(pr);
2214 if ((status = (pr->u.p.tc != 0)) == 0) {
2221 if (__kmp_env_consistency_check) {
2222 if (pr->pushed_ws != ct_none) {
2223 pr->pushed_ws = __kmp_pop_workshare(gtid, pr->pushed_ws, loc);
2226 }
else if (pr->flags.nomerge) {
2229 UT limit, trip, init;
2231 T chunk = pr->u.p.parm1;
2233 KD_TRACE(100, (
"__kmp_dispatch_next: T#%d kmp_sch_dynamic_chunked case\n",
2236 init = chunk * pr->u.p.count++;
2237 trip = pr->u.p.tc - 1;
2239 if ((status = (init <= trip)) == 0) {
2246 if (__kmp_env_consistency_check) {
2247 if (pr->pushed_ws != ct_none) {
2248 pr->pushed_ws = __kmp_pop_workshare(gtid, pr->pushed_ws, loc);
2253 limit = chunk + init - 1;
2256 if ((last = (limit >= trip)) != 0) {
2259 pr->u.p.last_upper = pr->u.p.ub;
2267 *p_lb = start + init;
2268 *p_ub = start + limit;
2270 *p_lb = start + init * incr;
2271 *p_ub = start + limit * incr;
2274 if (pr->flags.ordered) {
2275 pr->u.p.ordered_lower = init;
2276 pr->u.p.ordered_upper = limit;
2281 buff = __kmp_str_format(
"__kmp_dispatch_next: T#%%d "
2282 "ordered_lower:%%%s ordered_upper:%%%s\n",
2283 traits_t<UT>::spec, traits_t<UT>::spec);
2284 KD_TRACE(1000, (buff, gtid, pr->u.p.ordered_lower,
2285 pr->u.p.ordered_upper));
2286 __kmp_str_free(&buff);
2296 pr->u.p.last_upper = *p_ub;
2307 buff = __kmp_str_format(
2308 "__kmp_dispatch_next: T#%%d serialized case: p_lb:%%%s "
2309 "p_ub:%%%s p_st:%%%s p_last:%%p %%d returning:%%d\n",
2310 traits_t<T>::spec, traits_t<T>::spec, traits_t<ST>::spec);
2311 KD_TRACE(10, (buff, gtid, *p_lb, *p_ub, *p_st, p_last,
2312 (p_last ? *p_last : 0), status));
2313 __kmp_str_free(&buff);
2316#if INCLUDE_SSC_MARKS
2317 SSC_MARK_DISPATCH_NEXT();
2319 OMPT_LOOP_DISPATCH(*p_lb, *p_ub, pr->u.p.st, status);
2325 dispatch_shared_info_template<T>
volatile *sh;
2327 KMP_DEBUG_ASSERT(th->th.th_dispatch ==
2328 &th->th.th_team->t.t_dispatch[th->th.th_info.ds.ds_tid]);
2330 pr =
reinterpret_cast<dispatch_private_info_template<T> *
>(
2331 th->th.th_dispatch->th_dispatch_pr_current);
2332 KMP_DEBUG_ASSERT(pr);
2333 sh =
reinterpret_cast<dispatch_shared_info_template<T>
volatile *
>(
2334 th->th.th_dispatch->th_dispatch_sh_current);
2335 KMP_DEBUG_ASSERT(sh);
2337#if KMP_USE_HIER_SCHED
2338 if (pr->flags.use_hier)
2339 status = sh->hier->next(loc, gtid, pr, &last, p_lb, p_ub, p_st);
2342 status = __kmp_dispatch_next_algorithm<T>(gtid, pr, sh, &last, p_lb, p_ub,
2343 p_st, th->th.th_team_nproc,
2344 th->th.th_info.ds.ds_tid);
2348 num_done = test_then_inc<ST>(&sh->u.s.num_done);
2353 buff = __kmp_str_format(
2354 "__kmp_dispatch_next: T#%%d increment num_done:%%%s\n",
2355 traits_t<ST>::spec);
2356 KD_TRACE(10, (buff, gtid, sh->u.s.num_done));
2357 __kmp_str_free(&buff);
2361#if KMP_USE_HIER_SCHED
2362 pr->flags.use_hier = FALSE;
2364 if (num_done == th->th.th_team_nproc - 1) {
2365#if KMP_STATIC_STEAL_ENABLED
2366 if (pr->schedule == kmp_sch_static_steal) {
2368 int idx = (th->th.th_dispatch->th_disp_index - 1) %
2369 __kmp_dispatch_num_buffers;
2371 for (i = 0; i < th->th.th_team_nproc; ++i) {
2372 dispatch_private_info_template<T> *buf =
2373 reinterpret_cast<dispatch_private_info_template<T> *
>(
2374 &team->t.t_dispatch[i].th_disp_buffer[idx]);
2375 KMP_ASSERT(buf->steal_flag == THIEF);
2376 KMP_ATOMIC_ST_RLX(&buf->steal_flag, UNUSED);
2377 if (traits_t<T>::type_size > 4) {
2379 kmp_lock_t *lck = buf->u.p.steal_lock;
2380 KMP_ASSERT(lck != NULL);
2381 __kmp_destroy_lock(lck);
2383 buf->u.p.steal_lock = NULL;
2392 sh->u.s.num_done = 0;
2393 sh->u.s.iteration = 0;
2396 if (pr->flags.ordered) {
2397 sh->u.s.ordered_iteration = 0;
2400 sh->buffer_index += __kmp_dispatch_num_buffers;
2401 KD_TRACE(100, (
"__kmp_dispatch_next: T#%d change buffer_index:%d\n",
2402 gtid, sh->buffer_index));
2407 if (__kmp_env_consistency_check) {
2408 if (pr->pushed_ws != ct_none) {
2409 pr->pushed_ws = __kmp_pop_workshare(gtid, pr->pushed_ws, loc);
2413 th->th.th_dispatch->th_deo_fcn = NULL;
2414 th->th.th_dispatch->th_dxo_fcn = NULL;
2415 th->th.th_dispatch->th_dispatch_sh_current = NULL;
2416 th->th.th_dispatch->th_dispatch_pr_current = NULL;
2420 pr->u.p.last_upper = pr->u.p.ub;
2423 if (p_last != NULL && status != 0)
2431 buff = __kmp_str_format(
2432 "__kmp_dispatch_next: T#%%d normal case: "
2433 "p_lb:%%%s p_ub:%%%s p_st:%%%s p_last:%%p (%%d) returning:%%d\n",
2434 traits_t<T>::spec, traits_t<T>::spec, traits_t<ST>::spec);
2435 KD_TRACE(10, (buff, gtid, *p_lb, *p_ub, p_st ? *p_st : 0, p_last,
2436 (p_last ? *p_last : 0), status));
2437 __kmp_str_free(&buff);
2440#if INCLUDE_SSC_MARKS
2441 SSC_MARK_DISPATCH_NEXT();
2443 OMPT_LOOP_DISPATCH(*p_lb, *p_ub, pr->u.p.st, status);
2469 kmp_uint32 my_buffer_index;
2470 dispatch_shared_info_template<kmp_int32>
volatile *sh;
2472 KMP_DEBUG_ASSERT(__kmp_init_serial);
2474 if (!TCR_4(__kmp_init_parallel))
2475 __kmp_parallel_initialize();
2476 __kmp_resume_if_soft_paused();
2479 th = __kmp_threads[gtid];
2480 team = th->th.th_team;
2481 active = !team->t.t_serialized;
2482 th->th.th_ident = loc;
2485 KD_TRACE(10, (
"__kmpc_sections: called by T#%d\n", gtid));
2492 KMP_DEBUG_ASSERT(th->th.th_dispatch ==
2493 &th->th.th_team->t.t_dispatch[th->th.th_info.ds.ds_tid]);
2495 my_buffer_index = th->th.th_dispatch->th_disp_index++;
2498 sh =
reinterpret_cast<dispatch_shared_info_template<kmp_int32>
volatile *
>(
2499 &team->t.t_disp_buffer[my_buffer_index % __kmp_dispatch_num_buffers]);
2500 KD_TRACE(10, (
"__kmpc_sections_init: T#%d my_buffer_index:%d\n", gtid,
2503 th->th.th_dispatch->th_deo_fcn = __kmp_dispatch_deo_error;
2504 th->th.th_dispatch->th_dxo_fcn = __kmp_dispatch_dxo_error;
2506 KD_TRACE(100, (
"__kmpc_sections_init: T#%d before wait: my_buffer_index:%d "
2507 "sh->buffer_index:%d\n",
2508 gtid, my_buffer_index, sh->buffer_index));
2509 __kmp_wait<kmp_uint32>(&sh->buffer_index, my_buffer_index,
2510 __kmp_eq<kmp_uint32> USE_ITT_BUILD_ARG(NULL));
2514 KD_TRACE(100, (
"__kmpc_sections_init: T#%d after wait: my_buffer_index:%d "
2515 "sh->buffer_index:%d\n",
2516 gtid, my_buffer_index, sh->buffer_index));
2518 th->th.th_dispatch->th_dispatch_pr_current =
2520 th->th.th_dispatch->th_dispatch_sh_current =
2521 CCAST(dispatch_shared_info_t *, (
volatile dispatch_shared_info_t *)sh);
2524#if OMPT_SUPPORT && OMPT_OPTIONAL
2525 if (ompt_enabled.ompt_callback_work) {
2526 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
2527 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
2528 ompt_callbacks.ompt_callback(ompt_callback_work)(
2529 ompt_work_sections, ompt_scope_begin, &(team_info->parallel_data),
2530 &(task_info->task_data), 0, OMPT_GET_RETURN_ADDRESS(0));
2533 KMP_PUSH_PARTITIONED_TIMER(OMP_sections);
2549 kmp_int32 numberOfSections) {
2551 KMP_TIME_PARTITIONED_BLOCK(OMP_sections_overhead);
2553 kmp_info_t *th = __kmp_threads[gtid];
2555 kmp_team_t *team = th->th.th_team;
2558 KD_TRACE(1000, (
"__kmp_dispatch_next: T#%d; number of sections:%d\n", gtid,
2562 KMP_DEBUG_ASSERT(!team->t.t_serialized);
2564 dispatch_shared_info_template<kmp_int32>
volatile *sh;
2566 KMP_DEBUG_ASSERT(th->th.th_dispatch ==
2567 &th->th.th_team->t.t_dispatch[th->th.th_info.ds.ds_tid]);
2569 KMP_DEBUG_ASSERT(!(th->th.th_dispatch->th_dispatch_pr_current));
2570 sh =
reinterpret_cast<dispatch_shared_info_template<kmp_int32>
volatile *
>(
2571 th->th.th_dispatch->th_dispatch_sh_current);
2572 KMP_DEBUG_ASSERT(sh);
2574 kmp_int32 sectionIndex = 0;
2575 bool moreSectionsToExecute =
true;
2578 sectionIndex = test_then_inc<kmp_int32>((kmp_int32 *)&sh->u.s.iteration);
2579 if (sectionIndex >= numberOfSections) {
2580 moreSectionsToExecute =
false;
2585 if (!moreSectionsToExecute) {
2588 num_done = test_then_inc<kmp_int32>((kmp_int32 *)(&sh->u.s.num_done));
2590 if (num_done == th->th.th_team_nproc - 1) {
2595 sh->u.s.num_done = 0;
2596 sh->u.s.iteration = 0;
2600 sh->buffer_index += __kmp_dispatch_num_buffers;
2601 KD_TRACE(100, (
"__kmpc_next_section: T#%d change buffer_index:%d\n", gtid,
2608 th->th.th_dispatch->th_deo_fcn = NULL;
2609 th->th.th_dispatch->th_dxo_fcn = NULL;
2610 th->th.th_dispatch->th_dispatch_sh_current = NULL;
2611 th->th.th_dispatch->th_dispatch_pr_current = NULL;
2613#if OMPT_SUPPORT && OMPT_OPTIONAL
2614 if (ompt_enabled.ompt_callback_dispatch) {
2615 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
2616 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
2617 ompt_data_t instance = ompt_data_none;
2618 instance.ptr = OMPT_GET_RETURN_ADDRESS(0);
2619 ompt_callbacks.ompt_callback(ompt_callback_dispatch)(
2620 &(team_info->parallel_data), &(task_info->task_data),
2621 ompt_dispatch_section, instance);
2626 return sectionIndex;
2639 kmp_info_t *th = __kmp_threads[gtid];
2640 int active = !th->th.th_team->t.t_serialized;
2642 KD_TRACE(100, (
"__kmpc_end_sections: T#%d called\n", gtid));
2646#if OMPT_SUPPORT && OMPT_OPTIONAL
2647 if (ompt_enabled.ompt_callback_work) {
2648 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
2649 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
2650 ompt_callbacks.ompt_callback(ompt_callback_work)(
2651 ompt_work_sections, ompt_scope_end, &(team_info->parallel_data),
2652 &(task_info->task_data), 0, OMPT_GET_RETURN_ADDRESS(0));
2657 KMP_POP_PARTITIONED_TIMER();
2658 KD_TRACE(100, (
"__kmpc_end_sections: T#%d returned\n", gtid));
2661template <
typename T>
2662static void __kmp_dist_get_bounds(
ident_t *loc, kmp_int32 gtid,
2663 kmp_int32 *plastiter, T *plower, T *pupper,
2664 typename traits_t<T>::signed_t incr) {
2665 typedef typename traits_t<T>::unsigned_t UT;
2672 KMP_DEBUG_ASSERT(plastiter && plower && pupper);
2673 KE_TRACE(10, (
"__kmpc_dist_get_bounds called (%d)\n", gtid));
2675 typedef typename traits_t<T>::signed_t ST;
2679 buff = __kmp_str_format(
"__kmpc_dist_get_bounds: T#%%d liter=%%d "
2680 "iter=(%%%s, %%%s, %%%s) signed?<%s>\n",
2681 traits_t<T>::spec, traits_t<T>::spec,
2682 traits_t<ST>::spec, traits_t<T>::spec);
2683 KD_TRACE(100, (buff, gtid, *plastiter, *plower, *pupper, incr));
2684 __kmp_str_free(&buff);
2688 if (__kmp_env_consistency_check) {
2690 __kmp_error_construct(kmp_i18n_msg_CnsLoopIncrZeroProhibited, ct_pdo,
2693 if (incr > 0 ? (*pupper < *plower) : (*plower < *pupper)) {
2703 __kmp_error_construct(kmp_i18n_msg_CnsLoopIncrIllegal, ct_pdo, loc);
2706 __kmp_assert_valid_gtid(gtid);
2707 th = __kmp_threads[gtid];
2708 team = th->th.th_team;
2709 KMP_DEBUG_ASSERT(th->th.th_teams_microtask);
2710 nteams = th->th.th_teams_size.nteams;
2711 team_id = team->t.t_master_tid;
2712 KMP_DEBUG_ASSERT(nteams == (kmp_uint32)team->t.t_parent->t.t_nproc);
2716 trip_count = *pupper - *plower + 1;
2717 }
else if (incr == -1) {
2718 trip_count = *plower - *pupper + 1;
2719 }
else if (incr > 0) {
2721 trip_count = (UT)(*pupper - *plower) / incr + 1;
2723 trip_count = (UT)(*plower - *pupper) / (-incr) + 1;
2726 if (trip_count <= nteams) {
2728 __kmp_static == kmp_sch_static_greedy ||
2730 kmp_sch_static_balanced);
2732 if (team_id < trip_count) {
2733 *pupper = *plower = *plower + team_id * incr;
2735 *plower = *pupper + incr;
2737 if (plastiter != NULL)
2738 *plastiter = (team_id == trip_count - 1);
2740 if (__kmp_static == kmp_sch_static_balanced) {
2741 UT chunk = trip_count / nteams;
2742 UT extras = trip_count % nteams;
2744 incr * (team_id * chunk + (team_id < extras ? team_id : extras));
2745 *pupper = *plower + chunk * incr - (team_id < extras ? 0 : incr);
2746 if (plastiter != NULL)
2747 *plastiter = (team_id == nteams - 1);
2750 (trip_count / nteams + ((trip_count % nteams) ? 1 : 0)) * incr;
2752 KMP_DEBUG_ASSERT(__kmp_static == kmp_sch_static_greedy);
2754 *plower += team_id * chunk_inc_count;
2755 *pupper = *plower + chunk_inc_count - incr;
2758 if (*pupper < *plower)
2759 *pupper = traits_t<T>::max_value;
2760 if (plastiter != NULL)
2761 *plastiter = *plower <= upper && *pupper > upper - incr;
2762 if (*pupper > upper)
2765 if (*pupper > *plower)
2766 *pupper = traits_t<T>::min_value;
2767 if (plastiter != NULL)
2768 *plastiter = *plower >= upper && *pupper < upper - incr;
2769 if (*pupper < upper)
2801 kmp_int32 ub, kmp_int32 st, kmp_int32 chunk) {
2802 KMP_DEBUG_ASSERT(__kmp_init_serial);
2803#if OMPT_SUPPORT && OMPT_OPTIONAL
2804 OMPT_STORE_RETURN_ADDRESS(gtid);
2806 __kmp_dispatch_init<kmp_int32>(loc, gtid, schedule, lb, ub, st, chunk,
true);
2813 kmp_uint32 ub, kmp_int32 st, kmp_int32 chunk) {
2814 KMP_DEBUG_ASSERT(__kmp_init_serial);
2815#if OMPT_SUPPORT && OMPT_OPTIONAL
2816 OMPT_STORE_RETURN_ADDRESS(gtid);
2818 __kmp_dispatch_init<kmp_uint32>(loc, gtid, schedule, lb, ub, st, chunk,
true);
2826 kmp_int64 ub, kmp_int64 st, kmp_int64 chunk) {
2827 KMP_DEBUG_ASSERT(__kmp_init_serial);
2828#if OMPT_SUPPORT && OMPT_OPTIONAL
2829 OMPT_STORE_RETURN_ADDRESS(gtid);
2831 __kmp_dispatch_init<kmp_int64>(loc, gtid, schedule, lb, ub, st, chunk,
true);
2839 kmp_uint64 ub, kmp_int64 st, kmp_int64 chunk) {
2840 KMP_DEBUG_ASSERT(__kmp_init_serial);
2841#if OMPT_SUPPORT && OMPT_OPTIONAL
2842 OMPT_STORE_RETURN_ADDRESS(gtid);
2844 __kmp_dispatch_init<kmp_uint64>(loc, gtid, schedule, lb, ub, st, chunk,
true);
2858 kmp_int32 lb, kmp_int32 ub, kmp_int32 st,
2860 KMP_DEBUG_ASSERT(__kmp_init_serial);
2861#if OMPT_SUPPORT && OMPT_OPTIONAL
2862 OMPT_STORE_RETURN_ADDRESS(gtid);
2864 __kmp_dist_get_bounds<kmp_int32>(loc, gtid, p_last, &lb, &ub, st);
2865 __kmp_dispatch_init<kmp_int32>(loc, gtid, schedule, lb, ub, st, chunk,
true);
2868void __kmpc_dist_dispatch_init_4u(
ident_t *loc, kmp_int32 gtid,
2870 kmp_uint32 lb, kmp_uint32 ub, kmp_int32 st,
2872 KMP_DEBUG_ASSERT(__kmp_init_serial);
2873#if OMPT_SUPPORT && OMPT_OPTIONAL
2874 OMPT_STORE_RETURN_ADDRESS(gtid);
2876 __kmp_dist_get_bounds<kmp_uint32>(loc, gtid, p_last, &lb, &ub, st);
2877 __kmp_dispatch_init<kmp_uint32>(loc, gtid, schedule, lb, ub, st, chunk,
true);
2880void __kmpc_dist_dispatch_init_8(
ident_t *loc, kmp_int32 gtid,
2882 kmp_int64 lb, kmp_int64 ub, kmp_int64 st,
2884 KMP_DEBUG_ASSERT(__kmp_init_serial);
2885#if OMPT_SUPPORT && OMPT_OPTIONAL
2886 OMPT_STORE_RETURN_ADDRESS(gtid);
2888 __kmp_dist_get_bounds<kmp_int64>(loc, gtid, p_last, &lb, &ub, st);
2889 __kmp_dispatch_init<kmp_int64>(loc, gtid, schedule, lb, ub, st, chunk,
true);
2892void __kmpc_dist_dispatch_init_8u(
ident_t *loc, kmp_int32 gtid,
2894 kmp_uint64 lb, kmp_uint64 ub, kmp_int64 st,
2896 KMP_DEBUG_ASSERT(__kmp_init_serial);
2897#if OMPT_SUPPORT && OMPT_OPTIONAL
2898 OMPT_STORE_RETURN_ADDRESS(gtid);
2900 __kmp_dist_get_bounds<kmp_uint64>(loc, gtid, p_last, &lb, &ub, st);
2901 __kmp_dispatch_init<kmp_uint64>(loc, gtid, schedule, lb, ub, st, chunk,
true);
2918 kmp_int32 *p_lb, kmp_int32 *p_ub, kmp_int32 *p_st) {
2919#if OMPT_SUPPORT && OMPT_OPTIONAL
2920 OMPT_STORE_RETURN_ADDRESS(gtid);
2922 return __kmp_dispatch_next<kmp_int32>(loc, gtid, p_last, p_lb, p_ub, p_st
2923#
if OMPT_SUPPORT && OMPT_OPTIONAL
2925 OMPT_LOAD_RETURN_ADDRESS(gtid)
2934 kmp_uint32 *p_lb, kmp_uint32 *p_ub,
2936#if OMPT_SUPPORT && OMPT_OPTIONAL
2937 OMPT_STORE_RETURN_ADDRESS(gtid);
2939 return __kmp_dispatch_next<kmp_uint32>(loc, gtid, p_last, p_lb, p_ub, p_st
2940#
if OMPT_SUPPORT && OMPT_OPTIONAL
2942 OMPT_LOAD_RETURN_ADDRESS(gtid)
2951 kmp_int64 *p_lb, kmp_int64 *p_ub, kmp_int64 *p_st) {
2952#if OMPT_SUPPORT && OMPT_OPTIONAL
2953 OMPT_STORE_RETURN_ADDRESS(gtid);
2955 return __kmp_dispatch_next<kmp_int64>(loc, gtid, p_last, p_lb, p_ub, p_st
2956#
if OMPT_SUPPORT && OMPT_OPTIONAL
2958 OMPT_LOAD_RETURN_ADDRESS(gtid)
2967 kmp_uint64 *p_lb, kmp_uint64 *p_ub,
2969#if OMPT_SUPPORT && OMPT_OPTIONAL
2970 OMPT_STORE_RETURN_ADDRESS(gtid);
2972 return __kmp_dispatch_next<kmp_uint64>(loc, gtid, p_last, p_lb, p_ub, p_st
2973#
if OMPT_SUPPORT && OMPT_OPTIONAL
2975 OMPT_LOAD_RETURN_ADDRESS(gtid)
2987 __kmp_dispatch_finish<kmp_uint32>(gtid, loc);
2994 __kmp_dispatch_finish<kmp_uint64>(gtid, loc);
3001 __kmp_dispatch_finish<kmp_uint32>(gtid, loc);
3008 __kmp_dispatch_finish<kmp_uint64>(gtid, loc);
3015kmp_uint32 __kmp_eq_4(kmp_uint32 value, kmp_uint32 checker) {
3016 return value == checker;
3019kmp_uint32 __kmp_neq_4(kmp_uint32 value, kmp_uint32 checker) {
3020 return value != checker;
3023kmp_uint32 __kmp_lt_4(kmp_uint32 value, kmp_uint32 checker) {
3024 return value < checker;
3027kmp_uint32 __kmp_ge_4(kmp_uint32 value, kmp_uint32 checker) {
3028 return value >= checker;
3031kmp_uint32 __kmp_le_4(kmp_uint32 value, kmp_uint32 checker) {
3032 return value <= checker;
3036__kmp_wait_4(
volatile kmp_uint32 *spinner, kmp_uint32 checker,
3037 kmp_uint32 (*pred)(kmp_uint32, kmp_uint32),
3041 volatile kmp_uint32 *spin = spinner;
3042 kmp_uint32 check = checker;
3044 kmp_uint32 (*f)(kmp_uint32, kmp_uint32) = pred;
3048 KMP_FSYNC_SPIN_INIT(obj, CCAST(kmp_uint32 *, spin));
3049 KMP_INIT_YIELD(spins);
3050 KMP_INIT_BACKOFF(time);
3052 while (!f(r = TCR_4(*spin), check)) {
3053 KMP_FSYNC_SPIN_PREPARE(obj);
3058 KMP_YIELD_OVERSUB_ELSE_SPIN(spins, time);
3060 KMP_FSYNC_SPIN_ACQUIRED(obj);
3064void __kmp_wait_4_ptr(
void *spinner, kmp_uint32 checker,
3065 kmp_uint32 (*pred)(
void *, kmp_uint32),
3069 void *spin = spinner;
3070 kmp_uint32 check = checker;
3072 kmp_uint32 (*f)(
void *, kmp_uint32) = pred;
3075 KMP_FSYNC_SPIN_INIT(obj, spin);
3076 KMP_INIT_YIELD(spins);
3077 KMP_INIT_BACKOFF(time);
3079 while (!f(spin, check)) {
3080 KMP_FSYNC_SPIN_PREPARE(obj);
3083 KMP_YIELD_OVERSUB_ELSE_SPIN(spins, time);
3085 KMP_FSYNC_SPIN_ACQUIRED(obj);
3090#ifdef KMP_GOMP_COMPAT
3092void __kmp_aux_dispatch_init_4(
ident_t *loc, kmp_int32 gtid,
3094 kmp_int32 ub, kmp_int32 st, kmp_int32 chunk,
3096 __kmp_dispatch_init<kmp_int32>(loc, gtid, schedule, lb, ub, st, chunk,
3100void __kmp_aux_dispatch_init_4u(
ident_t *loc, kmp_int32 gtid,
3102 kmp_uint32 ub, kmp_int32 st, kmp_int32 chunk,
3104 __kmp_dispatch_init<kmp_uint32>(loc, gtid, schedule, lb, ub, st, chunk,
3108void __kmp_aux_dispatch_init_8(
ident_t *loc, kmp_int32 gtid,
3110 kmp_int64 ub, kmp_int64 st, kmp_int64 chunk,
3112 __kmp_dispatch_init<kmp_int64>(loc, gtid, schedule, lb, ub, st, chunk,
3116void __kmp_aux_dispatch_init_8u(
ident_t *loc, kmp_int32 gtid,
3118 kmp_uint64 ub, kmp_int64 st, kmp_int64 chunk,
3120 __kmp_dispatch_init<kmp_uint64>(loc, gtid, schedule, lb, ub, st, chunk,
3124void __kmp_aux_dispatch_fini_chunk_4(
ident_t *loc, kmp_int32 gtid) {
3125 __kmp_dispatch_finish_chunk<kmp_uint32>(gtid, loc);
3128void __kmp_aux_dispatch_fini_chunk_8(
ident_t *loc, kmp_int32 gtid) {
3129 __kmp_dispatch_finish_chunk<kmp_uint64>(gtid, loc);
3132void __kmp_aux_dispatch_fini_chunk_4u(
ident_t *loc, kmp_int32 gtid) {
3133 __kmp_dispatch_finish_chunk<kmp_uint32>(gtid, loc);
3136void __kmp_aux_dispatch_fini_chunk_8u(
ident_t *loc, kmp_int32 gtid) {
3137 __kmp_dispatch_finish_chunk<kmp_uint64>(gtid, loc);
#define KMP_COUNT_VALUE(name, value)
Adds value to specified timer (name).
#define KMP_COUNT_BLOCK(name)
Increments specified counter (name).
int __kmpc_dispatch_next_4(ident_t *loc, kmp_int32 gtid, kmp_int32 *p_last, kmp_int32 *p_lb, kmp_int32 *p_ub, kmp_int32 *p_st)
void __kmpc_dispatch_fini_4(ident_t *loc, kmp_int32 gtid)
kmp_int32 __kmpc_next_section(ident_t *loc, kmp_int32 gtid, kmp_int32 numberOfSections)
void __kmpc_dist_dispatch_init_4(ident_t *loc, kmp_int32 gtid, enum sched_type schedule, kmp_int32 *p_last, kmp_int32 lb, kmp_int32 ub, kmp_int32 st, kmp_int32 chunk)
int __kmpc_dispatch_next_4u(ident_t *loc, kmp_int32 gtid, kmp_int32 *p_last, kmp_uint32 *p_lb, kmp_uint32 *p_ub, kmp_int32 *p_st)
void __kmpc_end_sections(ident_t *loc, kmp_int32 gtid)
int __kmpc_dispatch_next_8u(ident_t *loc, kmp_int32 gtid, kmp_int32 *p_last, kmp_uint64 *p_lb, kmp_uint64 *p_ub, kmp_int64 *p_st)
void __kmpc_dispatch_fini_8(ident_t *loc, kmp_int32 gtid)
kmp_int32 __kmpc_sections_init(ident_t *loc, kmp_int32 gtid)
int __kmpc_dispatch_next_8(ident_t *loc, kmp_int32 gtid, kmp_int32 *p_last, kmp_int64 *p_lb, kmp_int64 *p_ub, kmp_int64 *p_st)
void __kmpc_dispatch_fini_8u(ident_t *loc, kmp_int32 gtid)
void __kmpc_dispatch_init_4(ident_t *loc, kmp_int32 gtid, enum sched_type schedule, kmp_int32 lb, kmp_int32 ub, kmp_int32 st, kmp_int32 chunk)
void __kmpc_dispatch_init_4u(ident_t *loc, kmp_int32 gtid, enum sched_type schedule, kmp_uint32 lb, kmp_uint32 ub, kmp_int32 st, kmp_int32 chunk)
void __kmpc_dispatch_init_8u(ident_t *loc, kmp_int32 gtid, enum sched_type schedule, kmp_uint64 lb, kmp_uint64 ub, kmp_int64 st, kmp_int64 chunk)
void __kmpc_dispatch_fini_4u(ident_t *loc, kmp_int32 gtid)
void __kmpc_dispatch_init_8(ident_t *loc, kmp_int32 gtid, enum sched_type schedule, kmp_int64 lb, kmp_int64 ub, kmp_int64 st, kmp_int64 chunk)