21 #include "kmp_error.h"
24 #include "kmp_stats.h"
26 #if KMP_USE_X87CONTROL
30 #include "kmp_dispatch.h"
31 #if KMP_USE_HIER_SCHED
32 #include "kmp_dispatch_hier.h"
36 #include "ompt-specific.h"
42 void __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);
60 void __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);
72 static inline int __kmp_get_monotonicity(
enum sched_type schedule,
73 bool use_hier =
false) {
77 monotonicity = SCHEDULE_MONOTONIC;
78 if (SCHEDULE_HAS_NONMONOTONIC(schedule))
79 monotonicity = SCHEDULE_NONMONOTONIC;
80 else if (SCHEDULE_HAS_MONOTONIC(schedule))
81 monotonicity = SCHEDULE_MONOTONIC;
96 void __kmp_dispatch_init_algorithm(
ident_t *loc,
int gtid,
97 dispatch_private_info_template<T> *pr,
99 typename traits_t<T>::signed_t st,
101 kmp_uint64 *cur_chunk,
103 typename traits_t<T>::signed_t chunk,
105 typedef typename traits_t<T>::unsigned_t UT;
106 typedef typename traits_t<T>::floating_t DBL;
116 typedef typename traits_t<T>::signed_t ST;
120 buff = __kmp_str_format(
"__kmp_dispatch_init_algorithm: T#%%d called "
121 "pr:%%p lb:%%%s ub:%%%s st:%%%s "
122 "schedule:%%d chunk:%%%s nproc:%%%s tid:%%%s\n",
123 traits_t<T>::spec, traits_t<T>::spec,
124 traits_t<ST>::spec, traits_t<ST>::spec,
125 traits_t<T>::spec, traits_t<T>::spec);
126 KD_TRACE(10, (buff, gtid, pr, lb, ub, st, schedule, chunk, nproc, tid));
127 __kmp_str_free(&buff);
131 th = __kmp_threads[gtid];
132 team = th->th.th_team;
133 active = !team->t.t_serialized;
136 int itt_need_metadata_reporting = __itt_metadata_add_ptr &&
137 __kmp_forkjoin_frames_mode == 3 &&
138 KMP_MASTER_GTID(gtid) &&
140 th->th.th_teams_microtask == NULL &&
142 team->t.t_active_level == 1;
145 #if KMP_USE_HIER_SCHED
146 use_hier = pr->flags.use_hier;
152 monotonicity = __kmp_get_monotonicity(schedule, use_hier);
153 schedule = SCHEDULE_WITHOUT_MODIFIERS(schedule);
157 pr->flags.nomerge = TRUE;
161 pr->flags.nomerge = FALSE;
163 pr->type_size = traits_t<T>::type_size;
165 pr->flags.ordered = TRUE;
169 pr->flags.ordered = FALSE;
172 if (pr->flags.ordered) {
173 monotonicity = SCHEDULE_MONOTONIC;
177 schedule = __kmp_static;
179 if (schedule == kmp_sch_runtime) {
182 schedule = team->t.t_sched.r_sched_type;
183 monotonicity = __kmp_get_monotonicity(schedule, use_hier);
184 schedule = SCHEDULE_WITHOUT_MODIFIERS(schedule);
188 schedule = __kmp_guided;
190 schedule = __kmp_static;
194 chunk = team->t.t_sched.chunk;
203 buff = __kmp_str_format(
"__kmp_dispatch_init_algorithm: T#%%d new: "
204 "schedule:%%d chunk:%%%s\n",
206 KD_TRACE(10, (buff, gtid, schedule, chunk));
207 __kmp_str_free(&buff);
212 schedule = __kmp_guided;
215 chunk = KMP_DEFAULT_CHUNK;
221 schedule = __kmp_auto;
226 buff = __kmp_str_format(
227 "__kmp_dispatch_init_algorithm: kmp_sch_auto: T#%%d new: "
228 "schedule:%%d chunk:%%%s\n",
230 KD_TRACE(10, (buff, gtid, schedule, chunk));
231 __kmp_str_free(&buff);
235 #if KMP_STATIC_STEAL_ENABLED
237 if (schedule == kmp_sch_dynamic_chunked) {
238 if (monotonicity == SCHEDULE_NONMONOTONIC)
239 schedule = kmp_sch_static_steal;
243 if (schedule == kmp_sch_guided_analytical_chunked && nproc > 1 << 20) {
244 schedule = kmp_sch_guided_iterative_chunked;
245 KMP_WARNING(DispatchManyThreads);
248 if (schedule == kmp_sch_runtime_simd) {
250 schedule = team->t.t_sched.r_sched_type;
251 monotonicity = __kmp_get_monotonicity(schedule, use_hier);
252 schedule = SCHEDULE_WITHOUT_MODIFIERS(schedule);
256 schedule == __kmp_static) {
257 schedule = kmp_sch_static_balanced_chunked;
260 schedule = kmp_sch_guided_simd;
262 chunk = team->t.t_sched.chunk * chunk;
272 buff = __kmp_str_format(
273 "__kmp_dispatch_init_algorithm: T#%%d new: schedule:%%d"
276 KD_TRACE(10, (buff, gtid, schedule, chunk));
277 __kmp_str_free(&buff);
281 #endif // OMP_45_ENABLED
282 pr->u.p.parm1 = chunk;
285 "unknown scheduling type");
289 if (__kmp_env_consistency_check) {
291 __kmp_error_construct(kmp_i18n_msg_CnsLoopIncrZeroProhibited,
292 (pr->flags.ordered ? ct_pdo_ordered : ct_pdo), loc);
306 tc = (UT)(lb - ub) / (-st) + 1;
314 tc = (UT)(ub - lb) / st + 1;
320 #if KMP_STATS_ENABLED
321 if (KMP_MASTER_GTID(gtid)) {
332 pr->u.p.last_upper = ub + st;
338 if (pr->flags.ordered) {
339 pr->ordered_bumped = 0;
340 pr->u.p.ordered_lower = 1;
341 pr->u.p.ordered_upper = 0;
346 #if (KMP_STATIC_STEAL_ENABLED)
347 case kmp_sch_static_steal: {
351 (
"__kmp_dispatch_init_algorithm: T#%d kmp_sch_static_steal case\n",
354 ntc = (tc % chunk ? 1 : 0) + tc / chunk;
355 if (nproc > 1 && ntc >= nproc) {
358 T small_chunk, extras;
360 small_chunk = ntc / nproc;
361 extras = ntc % nproc;
363 init =
id * small_chunk + (
id < extras ?
id : extras);
364 pr->u.p.count = init;
365 pr->u.p.ub = init + small_chunk + (
id < extras ? 1 : 0);
371 pr->u.p.parm3 = KMP_MIN(small_chunk + extras, nproc);
372 pr->u.p.parm4 = (
id + 1) % nproc;
374 if (traits_t<T>::type_size > 4) {
380 KMP_DEBUG_ASSERT(th->th.th_dispatch->th_steal_lock == NULL);
381 th->th.th_dispatch->th_steal_lock =
382 (kmp_lock_t *)__kmp_allocate(
sizeof(kmp_lock_t));
383 __kmp_init_lock(th->th.th_dispatch->th_steal_lock);
387 KD_TRACE(100, (
"__kmp_dispatch_init_algorithm: T#%d falling-through to "
388 "kmp_sch_static_balanced\n",
390 schedule = kmp_sch_static_balanced;
397 case kmp_sch_static_balanced: {
402 (
"__kmp_dispatch_init_algorithm: T#%d kmp_sch_static_balanced case\n",
412 pr->u.p.parm1 = (
id == tc - 1);
415 pr->u.p.parm1 = FALSE;
419 T small_chunk = tc / nproc;
420 T extras = tc % nproc;
421 init =
id * small_chunk + (
id < extras ?
id : extras);
422 limit = init + small_chunk - (
id < extras ? 0 : 1);
423 pr->u.p.parm1 = (
id == nproc - 1);
429 pr->u.p.parm1 = TRUE;
433 pr->u.p.parm1 = FALSE;
439 if (itt_need_metadata_reporting)
441 *cur_chunk = limit - init + 1;
444 pr->u.p.lb = lb + init;
445 pr->u.p.ub = lb + limit;
448 T ub_tmp = lb + limit * st;
449 pr->u.p.lb = lb + init * st;
453 pr->u.p.ub = (ub_tmp + st > ub ? ub : ub_tmp);
455 pr->u.p.ub = (ub_tmp + st < ub ? ub : ub_tmp);
458 if (pr->flags.ordered) {
459 pr->u.p.ordered_lower = init;
460 pr->u.p.ordered_upper = limit;
465 case kmp_sch_static_balanced_chunked: {
468 KD_TRACE(100, (
"__kmp_dispatch_init_algorithm: T#%d runtime(simd:static)"
469 " -> falling-through to static_greedy\n",
471 schedule = kmp_sch_static_greedy;
473 pr->u.p.parm1 = ((tc + nth - 1) / nth + chunk - 1) & ~(chunk - 1);
478 case kmp_sch_guided_simd:
479 #endif // OMP_45_ENABLED
480 case kmp_sch_guided_iterative_chunked: {
483 (
"__kmp_dispatch_init_algorithm: T#%d kmp_sch_guided_iterative_chunked"
488 if ((2L * chunk + 1) * nproc >= tc) {
490 schedule = kmp_sch_dynamic_chunked;
493 pr->u.p.parm2 = guided_int_param * nproc * (chunk + 1);
494 *(
double *)&pr->u.p.parm3 =
495 guided_flt_param / nproc;
498 KD_TRACE(100, (
"__kmp_dispatch_init_algorithm: T#%d falling-through to "
499 "kmp_sch_static_greedy\n",
501 schedule = kmp_sch_static_greedy;
505 (
"__kmp_dispatch_init_algorithm: T#%d kmp_sch_static_greedy case\n",
511 case kmp_sch_guided_analytical_chunked: {
512 KD_TRACE(100, (
"__kmp_dispatch_init_algorithm: T#%d "
513 "kmp_sch_guided_analytical_chunked case\n",
517 if ((2L * chunk + 1) * nproc >= tc) {
519 schedule = kmp_sch_dynamic_chunked;
524 #if KMP_USE_X87CONTROL
534 unsigned int oldFpcw = _control87(0, 0);
535 _control87(_PC_64, _MCW_PC);
538 long double target = ((
long double)chunk * 2 + 1) * nproc / tc;
545 x = (
long double)1.0 - (
long double)0.5 / nproc;
556 ptrdiff_t natural_alignment =
557 (ptrdiff_t)&t.b - (ptrdiff_t)&t - (ptrdiff_t)1;
561 (((ptrdiff_t)&pr->u.p.parm3) & (natural_alignment)) == 0);
566 *(DBL *)&pr->u.p.parm3 = x;
579 p = __kmp_pow<UT>(x, right);
584 }
while (p > target && right < (1 << 27));
592 while (left + 1 < right) {
593 mid = (left + right) / 2;
594 if (__kmp_pow<UT>(x, mid) > target) {
603 KMP_ASSERT(cross && __kmp_pow<UT>(x, cross - 1) > target &&
604 __kmp_pow<UT>(x, cross) <= target);
607 pr->u.p.parm2 = cross;
610 #if ((KMP_OS_LINUX || KMP_OS_WINDOWS) && KMP_ARCH_X86) && (!defined(KMP_I8))
611 #define GUIDED_ANALYTICAL_WORKAROUND (*(DBL *)&pr->u.p.parm3)
613 #define GUIDED_ANALYTICAL_WORKAROUND (x)
616 pr->u.p.count = tc - __kmp_dispatch_guided_remaining(
617 tc, GUIDED_ANALYTICAL_WORKAROUND, cross) -
619 #if KMP_USE_X87CONTROL
621 _control87(oldFpcw, _MCW_PC);
625 KD_TRACE(100, (
"__kmp_dispatch_init_algorithm: T#%d falling-through to "
626 "kmp_sch_static_greedy\n",
628 schedule = kmp_sch_static_greedy;
634 case kmp_sch_static_greedy:
637 (
"__kmp_dispatch_init_algorithm: T#%d kmp_sch_static_greedy case\n",
639 pr->u.p.parm1 = (nproc > 1) ? (tc + nproc - 1) / nproc : tc;
641 case kmp_sch_static_chunked:
642 case kmp_sch_dynamic_chunked:
643 if (pr->u.p.parm1 <= 0) {
644 pr->u.p.parm1 = KMP_DEFAULT_CHUNK;
646 KD_TRACE(100, (
"__kmp_dispatch_init_algorithm: T#%d "
647 "kmp_sch_static_chunked/kmp_sch_dynamic_chunked cases\n",
650 case kmp_sch_trapezoidal: {
653 T parm1, parm2, parm3, parm4;
655 (
"__kmp_dispatch_init_algorithm: T#%d kmp_sch_trapezoidal case\n",
661 parm2 = (tc / (2 * nproc));
671 }
else if (parm1 > parm2) {
676 parm3 = (parm2 + parm1);
677 parm3 = (2 * tc + parm3 - 1) / parm3;
685 parm4 = (parm2 - parm1) / parm4;
692 pr->u.p.parm1 = parm1;
693 pr->u.p.parm2 = parm2;
694 pr->u.p.parm3 = parm3;
695 pr->u.p.parm4 = parm4;
700 __kmp_fatal(KMP_MSG(UnknownSchedTypeDetected),
701 KMP_HNT(GetNewerLibrary),
706 pr->schedule = schedule;
709 #if KMP_USE_HIER_SCHED
710 template <
typename T>
711 inline void __kmp_dispatch_init_hier_runtime(
ident_t *loc, T lb, T ub,
712 typename traits_t<T>::signed_t st);
715 __kmp_dispatch_init_hier_runtime<kmp_int32>(
ident_t *loc, kmp_int32 lb,
716 kmp_int32 ub, kmp_int32 st) {
717 __kmp_dispatch_init_hierarchy<kmp_int32>(
718 loc, __kmp_hier_scheds.size, __kmp_hier_scheds.layers,
719 __kmp_hier_scheds.scheds, __kmp_hier_scheds.small_chunks, lb, ub, st);
723 __kmp_dispatch_init_hier_runtime<kmp_uint32>(
ident_t *loc, kmp_uint32 lb,
724 kmp_uint32 ub, kmp_int32 st) {
725 __kmp_dispatch_init_hierarchy<kmp_uint32>(
726 loc, __kmp_hier_scheds.size, __kmp_hier_scheds.layers,
727 __kmp_hier_scheds.scheds, __kmp_hier_scheds.small_chunks, lb, ub, st);
731 __kmp_dispatch_init_hier_runtime<kmp_int64>(
ident_t *loc, kmp_int64 lb,
732 kmp_int64 ub, kmp_int64 st) {
733 __kmp_dispatch_init_hierarchy<kmp_int64>(
734 loc, __kmp_hier_scheds.size, __kmp_hier_scheds.layers,
735 __kmp_hier_scheds.scheds, __kmp_hier_scheds.large_chunks, lb, ub, st);
739 __kmp_dispatch_init_hier_runtime<kmp_uint64>(
ident_t *loc, kmp_uint64 lb,
740 kmp_uint64 ub, kmp_int64 st) {
741 __kmp_dispatch_init_hierarchy<kmp_uint64>(
742 loc, __kmp_hier_scheds.size, __kmp_hier_scheds.layers,
743 __kmp_hier_scheds.scheds, __kmp_hier_scheds.large_chunks, lb, ub, st);
747 void __kmp_dispatch_free_hierarchies(kmp_team_t *team) {
748 int num_disp_buff = team->t.t_max_nproc > 1 ? __kmp_dispatch_num_buffers : 2;
749 for (
int i = 0; i < num_disp_buff; ++i) {
752 reinterpret_cast<dispatch_shared_info_template<kmp_int32>
volatile *
>(
753 &team->t.t_disp_buffer[i]);
755 sh->hier->deallocate();
756 __kmp_free(sh->hier);
764 template <
typename T>
767 T ub,
typename traits_t<T>::signed_t st,
768 typename traits_t<T>::signed_t chunk,
int push_ws) {
769 typedef typename traits_t<T>::unsigned_t UT;
774 kmp_uint32 my_buffer_index;
775 dispatch_private_info_template<T> *pr;
776 dispatch_shared_info_template<T>
volatile *sh;
778 KMP_BUILD_ASSERT(
sizeof(dispatch_private_info_template<T>) ==
779 sizeof(dispatch_private_info));
780 KMP_BUILD_ASSERT(
sizeof(dispatch_shared_info_template<UT>) ==
781 sizeof(dispatch_shared_info));
783 if (!TCR_4(__kmp_init_parallel))
784 __kmp_parallel_initialize();
787 __kmp_resume_if_soft_paused();
790 #if INCLUDE_SSC_MARKS
791 SSC_MARK_DISPATCH_INIT();
794 typedef typename traits_t<T>::signed_t ST;
798 buff = __kmp_str_format(
"__kmp_dispatch_init: T#%%d called: schedule:%%d "
799 "chunk:%%%s lb:%%%s ub:%%%s st:%%%s\n",
800 traits_t<ST>::spec, traits_t<T>::spec,
801 traits_t<T>::spec, traits_t<ST>::spec);
802 KD_TRACE(10, (buff, gtid, schedule, chunk, lb, ub, st));
803 __kmp_str_free(&buff);
807 th = __kmp_threads[gtid];
808 team = th->th.th_team;
809 active = !team->t.t_serialized;
810 th->th.th_ident = loc;
815 if (schedule == __kmp_static) {
821 #if KMP_USE_HIER_SCHED
827 my_buffer_index = th->th.th_dispatch->th_disp_index;
828 pr =
reinterpret_cast<dispatch_private_info_template<T> *
>(
830 ->th_disp_buffer[my_buffer_index % __kmp_dispatch_num_buffers]);
831 my_sched = SCHEDULE_WITHOUT_MODIFIERS(my_sched);
836 if (pr->flags.use_hier) {
838 KD_TRACE(100, (
"__kmp_dispatch_init: T#%d ordered loop detected. "
839 "Disabling hierarchical scheduling.\n",
841 pr->flags.use_hier = FALSE;
844 if (schedule == kmp_sch_runtime && __kmp_hier_scheds.size > 0) {
847 if (!ordered && !pr->flags.use_hier)
848 __kmp_dispatch_init_hier_runtime<T>(loc, lb, ub, st);
850 #endif // KMP_USE_HIER_SCHED
853 kmp_uint64 cur_chunk = chunk;
854 int itt_need_metadata_reporting = __itt_metadata_add_ptr &&
855 __kmp_forkjoin_frames_mode == 3 &&
856 KMP_MASTER_GTID(gtid) &&
858 th->th.th_teams_microtask == NULL &&
860 team->t.t_active_level == 1;
863 pr =
reinterpret_cast<dispatch_private_info_template<T> *
>(
864 th->th.th_dispatch->th_disp_buffer);
866 KMP_DEBUG_ASSERT(th->th.th_dispatch ==
867 &th->th.th_team->t.t_dispatch[th->th.th_info.ds.ds_tid]);
869 my_buffer_index = th->th.th_dispatch->th_disp_index++;
872 pr =
reinterpret_cast<dispatch_private_info_template<T> *
>(
874 ->th_disp_buffer[my_buffer_index % __kmp_dispatch_num_buffers]);
875 sh =
reinterpret_cast<dispatch_shared_info_template<T>
volatile *
>(
876 &team->t.t_disp_buffer[my_buffer_index % __kmp_dispatch_num_buffers]);
877 KD_TRACE(10, (
"__kmp_dispatch_init: T#%d my_buffer_index:%d\n", gtid,
881 __kmp_dispatch_init_algorithm(loc, gtid, pr, schedule, lb, ub, st,
885 chunk, (T)th->th.th_team_nproc,
886 (T)th->th.th_info.ds.ds_tid);
888 if (pr->flags.ordered == 0) {
889 th->th.th_dispatch->th_deo_fcn = __kmp_dispatch_deo_error;
890 th->th.th_dispatch->th_dxo_fcn = __kmp_dispatch_dxo_error;
892 th->th.th_dispatch->th_deo_fcn = __kmp_dispatch_deo<UT>;
893 th->th.th_dispatch->th_dxo_fcn = __kmp_dispatch_dxo<UT>;
901 KD_TRACE(100, (
"__kmp_dispatch_init: T#%d before wait: my_buffer_index:%d "
902 "sh->buffer_index:%d\n",
903 gtid, my_buffer_index, sh->buffer_index));
904 __kmp_wait<kmp_uint32>(&sh->buffer_index, my_buffer_index,
905 __kmp_eq<kmp_uint32> USE_ITT_BUILD_ARG(NULL));
909 KD_TRACE(100, (
"__kmp_dispatch_init: T#%d after wait: my_buffer_index:%d "
910 "sh->buffer_index:%d\n",
911 gtid, my_buffer_index, sh->buffer_index));
913 th->th.th_dispatch->th_dispatch_pr_current = (dispatch_private_info_t *)pr;
914 th->th.th_dispatch->th_dispatch_sh_current =
915 CCAST(dispatch_shared_info_t *, (
volatile dispatch_shared_info_t *)sh);
917 if (pr->flags.ordered) {
918 __kmp_itt_ordered_init(gtid);
921 if (itt_need_metadata_reporting) {
923 kmp_uint64 schedtype = 0;
925 case kmp_sch_static_chunked:
926 case kmp_sch_static_balanced:
928 case kmp_sch_static_greedy:
929 cur_chunk = pr->u.p.parm1;
931 case kmp_sch_dynamic_chunked:
934 case kmp_sch_guided_iterative_chunked:
935 case kmp_sch_guided_analytical_chunked:
937 case kmp_sch_guided_simd:
947 __kmp_itt_metadata_loop(loc, schedtype, pr->u.p.tc, cur_chunk);
949 #if KMP_USE_HIER_SCHED
950 if (pr->flags.use_hier) {
952 pr->u.p.ub = pr->u.p.lb = pr->u.p.st = pr->u.p.tc = 0;
954 #endif // KMP_USER_HIER_SCHED
962 buff = __kmp_str_format(
963 "__kmp_dispatch_init: T#%%d returning: schedule:%%d ordered:%%%s "
965 " st:%%%s tc:%%%s count:%%%s\n\tordered_lower:%%%s ordered_upper:%%%s"
966 " parm1:%%%s parm2:%%%s parm3:%%%s parm4:%%%s\n",
967 traits_t<UT>::spec, traits_t<T>::spec, traits_t<T>::spec,
968 traits_t<ST>::spec, traits_t<UT>::spec, traits_t<UT>::spec,
969 traits_t<UT>::spec, traits_t<UT>::spec, traits_t<T>::spec,
970 traits_t<T>::spec, traits_t<T>::spec, traits_t<T>::spec);
971 KD_TRACE(10, (buff, gtid, pr->schedule, pr->flags.ordered, pr->u.p.lb,
972 pr->u.p.ub, pr->u.p.st, pr->u.p.tc, pr->u.p.count,
973 pr->u.p.ordered_lower, pr->u.p.ordered_upper, pr->u.p.parm1,
974 pr->u.p.parm2, pr->u.p.parm3, pr->u.p.parm4));
975 __kmp_str_free(&buff);
978 #if (KMP_STATIC_STEAL_ENABLED)
984 if (schedule == kmp_sch_static_steal) {
988 volatile T *p = &pr->u.p.static_steal_counter;
991 #endif // ( KMP_STATIC_STEAL_ENABLED )
993 #if OMPT_SUPPORT && OMPT_OPTIONAL
994 if (ompt_enabled.ompt_callback_work) {
995 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
996 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
997 ompt_callbacks.ompt_callback(ompt_callback_work)(
998 ompt_work_loop, ompt_scope_begin, &(team_info->parallel_data),
999 &(task_info->task_data), pr->u.p.tc, OMPT_LOAD_RETURN_ADDRESS(gtid));
1002 KMP_PUSH_PARTITIONED_TIMER(OMP_loop_dynamic);
1010 template <
typename UT>
1011 static void __kmp_dispatch_finish(
int gtid,
ident_t *loc) {
1012 typedef typename traits_t<UT>::signed_t ST;
1013 kmp_info_t *th = __kmp_threads[gtid];
1015 KD_TRACE(100, (
"__kmp_dispatch_finish: T#%d called\n", gtid));
1016 if (!th->th.th_team->t.t_serialized) {
1018 dispatch_private_info_template<UT> *pr =
1019 reinterpret_cast<dispatch_private_info_template<UT> *
>(
1020 th->th.th_dispatch->th_dispatch_pr_current);
1021 dispatch_shared_info_template<UT>
volatile *sh =
1022 reinterpret_cast<dispatch_shared_info_template<UT>
volatile *
>(
1023 th->th.th_dispatch->th_dispatch_sh_current);
1024 KMP_DEBUG_ASSERT(pr);
1025 KMP_DEBUG_ASSERT(sh);
1026 KMP_DEBUG_ASSERT(th->th.th_dispatch ==
1027 &th->th.th_team->t.t_dispatch[th->th.th_info.ds.ds_tid]);
1029 if (pr->ordered_bumped) {
1032 (
"__kmp_dispatch_finish: T#%d resetting ordered_bumped to zero\n",
1034 pr->ordered_bumped = 0;
1036 UT lower = pr->u.p.ordered_lower;
1042 buff = __kmp_str_format(
"__kmp_dispatch_finish: T#%%d before wait: "
1043 "ordered_iteration:%%%s lower:%%%s\n",
1044 traits_t<UT>::spec, traits_t<UT>::spec);
1045 KD_TRACE(1000, (buff, gtid, sh->u.s.ordered_iteration, lower));
1046 __kmp_str_free(&buff);
1050 __kmp_wait<UT>(&sh->u.s.ordered_iteration, lower,
1051 __kmp_ge<UT> USE_ITT_BUILD_ARG(NULL));
1057 buff = __kmp_str_format(
"__kmp_dispatch_finish: T#%%d after wait: "
1058 "ordered_iteration:%%%s lower:%%%s\n",
1059 traits_t<UT>::spec, traits_t<UT>::spec);
1060 KD_TRACE(1000, (buff, gtid, sh->u.s.ordered_iteration, lower));
1061 __kmp_str_free(&buff);
1065 test_then_inc<ST>((
volatile ST *)&sh->u.s.ordered_iteration);
1068 KD_TRACE(100, (
"__kmp_dispatch_finish: T#%d returned\n", gtid));
1071 #ifdef KMP_GOMP_COMPAT
1073 template <
typename UT>
1074 static void __kmp_dispatch_finish_chunk(
int gtid,
ident_t *loc) {
1075 typedef typename traits_t<UT>::signed_t ST;
1076 kmp_info_t *th = __kmp_threads[gtid];
1078 KD_TRACE(100, (
"__kmp_dispatch_finish_chunk: T#%d called\n", gtid));
1079 if (!th->th.th_team->t.t_serialized) {
1081 dispatch_private_info_template<UT> *pr =
1082 reinterpret_cast<dispatch_private_info_template<UT> *
>(
1083 th->th.th_dispatch->th_dispatch_pr_current);
1084 dispatch_shared_info_template<UT>
volatile *sh =
1085 reinterpret_cast<dispatch_shared_info_template<UT>
volatile *
>(
1086 th->th.th_dispatch->th_dispatch_sh_current);
1087 KMP_DEBUG_ASSERT(pr);
1088 KMP_DEBUG_ASSERT(sh);
1089 KMP_DEBUG_ASSERT(th->th.th_dispatch ==
1090 &th->th.th_team->t.t_dispatch[th->th.th_info.ds.ds_tid]);
1093 UT lower = pr->u.p.ordered_lower;
1094 UT upper = pr->u.p.ordered_upper;
1095 UT inc = upper - lower + 1;
1097 if (pr->ordered_bumped == inc) {
1100 (
"__kmp_dispatch_finish: T#%d resetting ordered_bumped to zero\n",
1102 pr->ordered_bumped = 0;
1104 inc -= pr->ordered_bumped;
1110 buff = __kmp_str_format(
1111 "__kmp_dispatch_finish_chunk: T#%%d before wait: "
1112 "ordered_iteration:%%%s lower:%%%s upper:%%%s\n",
1113 traits_t<UT>::spec, traits_t<UT>::spec, traits_t<UT>::spec);
1114 KD_TRACE(1000, (buff, gtid, sh->u.s.ordered_iteration, lower, upper));
1115 __kmp_str_free(&buff);
1119 __kmp_wait<UT>(&sh->u.s.ordered_iteration, lower,
1120 __kmp_ge<UT> USE_ITT_BUILD_ARG(NULL));
1123 KD_TRACE(1000, (
"__kmp_dispatch_finish_chunk: T#%d resetting "
1124 "ordered_bumped to zero\n",
1126 pr->ordered_bumped = 0;
1132 buff = __kmp_str_format(
1133 "__kmp_dispatch_finish_chunk: T#%%d after wait: "
1134 "ordered_iteration:%%%s inc:%%%s lower:%%%s upper:%%%s\n",
1135 traits_t<UT>::spec, traits_t<UT>::spec, traits_t<UT>::spec,
1136 traits_t<UT>::spec);
1138 (buff, gtid, sh->u.s.ordered_iteration, inc, lower, upper));
1139 __kmp_str_free(&buff);
1143 test_then_add<ST>((
volatile ST *)&sh->u.s.ordered_iteration, inc);
1147 KD_TRACE(100, (
"__kmp_dispatch_finish_chunk: T#%d returned\n", gtid));
1152 template <
typename T>
1153 int __kmp_dispatch_next_algorithm(
int gtid,
1154 dispatch_private_info_template<T> *pr,
1155 dispatch_shared_info_template<T>
volatile *sh,
1156 kmp_int32 *p_last, T *p_lb, T *p_ub,
1157 typename traits_t<T>::signed_t *p_st, T nproc,
1159 typedef typename traits_t<T>::unsigned_t UT;
1160 typedef typename traits_t<T>::signed_t ST;
1161 typedef typename traits_t<T>::floating_t DBL;
1166 UT limit, trip, init;
1167 kmp_info_t *th = __kmp_threads[gtid];
1168 kmp_team_t *team = th->th.th_team;
1170 KMP_DEBUG_ASSERT(th->th.th_dispatch ==
1171 &th->th.th_team->t.t_dispatch[th->th.th_info.ds.ds_tid]);
1172 KMP_DEBUG_ASSERT(pr);
1173 KMP_DEBUG_ASSERT(sh);
1174 KMP_DEBUG_ASSERT(tid >= 0 && tid < nproc);
1180 __kmp_str_format(
"__kmp_dispatch_next_algorithm: T#%%d called pr:%%p "
1181 "sh:%%p nproc:%%%s tid:%%%s\n",
1182 traits_t<T>::spec, traits_t<T>::spec);
1183 KD_TRACE(10, (buff, gtid, pr, sh, nproc, tid));
1184 __kmp_str_free(&buff);
1189 if (pr->u.p.tc == 0) {
1191 (
"__kmp_dispatch_next_algorithm: T#%d early exit trip count is "
1197 switch (pr->schedule) {
1198 #if (KMP_STATIC_STEAL_ENABLED)
1199 case kmp_sch_static_steal: {
1200 T chunk = pr->u.p.parm1;
1203 (
"__kmp_dispatch_next_algorithm: T#%d kmp_sch_static_steal case\n",
1206 trip = pr->u.p.tc - 1;
1208 if (traits_t<T>::type_size > 4) {
1211 kmp_lock_t *lck = th->th.th_dispatch->th_steal_lock;
1212 KMP_DEBUG_ASSERT(lck != NULL);
1213 if (pr->u.p.count < (UT)pr->u.p.ub) {
1214 __kmp_acquire_lock(lck, gtid);
1216 init = (pr->u.p.count)++;
1217 status = (init < (UT)pr->u.p.ub);
1218 __kmp_release_lock(lck, gtid);
1223 kmp_info_t **other_threads = team->t.t_threads;
1224 int while_limit = pr->u.p.parm3;
1225 int while_index = 0;
1228 while ((!status) && (while_limit != ++while_index)) {
1230 T victimIdx = pr->u.p.parm4;
1231 T oldVictimIdx = victimIdx ? victimIdx - 1 : nproc - 1;
1232 dispatch_private_info_template<T> *victim =
1233 reinterpret_cast<dispatch_private_info_template<T> *
>(
1234 other_threads[victimIdx]
1235 ->th.th_dispatch->th_dispatch_pr_current);
1236 while ((victim == NULL || victim == pr ||
1237 (*(
volatile T *)&victim->u.p.static_steal_counter !=
1238 *(
volatile T *)&pr->u.p.static_steal_counter)) &&
1239 oldVictimIdx != victimIdx) {
1240 victimIdx = (victimIdx + 1) % nproc;
1241 victim =
reinterpret_cast<dispatch_private_info_template<T> *
>(
1242 other_threads[victimIdx]
1243 ->th.th_dispatch->th_dispatch_pr_current);
1245 if (!victim || (*(
volatile T *)&victim->u.p.static_steal_counter !=
1246 *(
volatile T *)&pr->u.p.static_steal_counter)) {
1251 if (victim->u.p.count + 2 > (UT)victim->u.p.ub) {
1252 pr->u.p.parm4 = (victimIdx + 1) % nproc;
1256 lck = other_threads[victimIdx]->th.th_dispatch->th_steal_lock;
1257 KMP_ASSERT(lck != NULL);
1258 __kmp_acquire_lock(lck, gtid);
1259 limit = victim->u.p.ub;
1260 if (victim->u.p.count >= limit ||
1261 (remaining = limit - victim->u.p.count) < 2) {
1262 __kmp_release_lock(lck, gtid);
1263 pr->u.p.parm4 = (victimIdx + 1) % nproc;
1268 if (remaining > 3) {
1270 KMP_COUNT_DEVELOPER_VALUE(FOR_static_steal_stolen, remaining >> 2);
1271 init = (victim->u.p.ub -= (remaining >> 2));
1274 KMP_COUNT_DEVELOPER_VALUE(FOR_static_steal_stolen, 1);
1275 init = (victim->u.p.ub -= 1);
1277 __kmp_release_lock(lck, gtid);
1279 KMP_DEBUG_ASSERT(init + 1 <= limit);
1280 pr->u.p.parm4 = victimIdx;
1284 __kmp_acquire_lock(th->th.th_dispatch->th_steal_lock, gtid);
1285 pr->u.p.count = init + 1;
1287 __kmp_release_lock(th->th.th_dispatch->th_steal_lock, gtid);
1302 union_i4 vold, vnew;
1303 vold.b = *(
volatile kmp_int64 *)(&pr->u.p.count);
1306 while (!KMP_COMPARE_AND_STORE_ACQ64(
1307 (
volatile kmp_int64 *)&pr->u.p.count,
1308 *VOLATILE_CAST(kmp_int64 *) & vold.b,
1309 *VOLATILE_CAST(kmp_int64 *) & vnew.b)) {
1311 vold.b = *(
volatile kmp_int64 *)(&pr->u.p.count);
1316 init = vnew.p.count;
1317 status = (init < (UT)vnew.p.ub);
1321 kmp_info_t **other_threads = team->t.t_threads;
1322 int while_limit = pr->u.p.parm3;
1323 int while_index = 0;
1327 while ((!status) && (while_limit != ++while_index)) {
1328 union_i4 vold, vnew;
1329 kmp_int32 remaining;
1330 T victimIdx = pr->u.p.parm4;
1331 T oldVictimIdx = victimIdx ? victimIdx - 1 : nproc - 1;
1332 dispatch_private_info_template<T> *victim =
1333 reinterpret_cast<dispatch_private_info_template<T> *
>(
1334 other_threads[victimIdx]
1335 ->th.th_dispatch->th_dispatch_pr_current);
1336 while ((victim == NULL || victim == pr ||
1337 (*(
volatile T *)&victim->u.p.static_steal_counter !=
1338 *(
volatile T *)&pr->u.p.static_steal_counter)) &&
1339 oldVictimIdx != victimIdx) {
1340 victimIdx = (victimIdx + 1) % nproc;
1341 victim =
reinterpret_cast<dispatch_private_info_template<T> *
>(
1342 other_threads[victimIdx]
1343 ->th.th_dispatch->th_dispatch_pr_current);
1345 if (!victim || (*(
volatile T *)&victim->u.p.static_steal_counter !=
1346 *(
volatile T *)&pr->u.p.static_steal_counter)) {
1351 pr->u.p.parm4 = victimIdx;
1353 vold.b = *(
volatile kmp_int64 *)(&victim->u.p.count);
1356 KMP_DEBUG_ASSERT((vnew.p.ub - 1) * (UT)chunk <= trip);
1357 if (vnew.p.count >= (UT)vnew.p.ub ||
1358 (remaining = vnew.p.ub - vnew.p.count) < 2) {
1359 pr->u.p.parm4 = (victimIdx + 1) % nproc;
1362 if (remaining > 3) {
1363 vnew.p.ub -= (remaining >> 2);
1367 KMP_DEBUG_ASSERT((vnew.p.ub - 1) * (UT)chunk <= trip);
1369 if (KMP_COMPARE_AND_STORE_ACQ64(
1370 (
volatile kmp_int64 *)&victim->u.p.count,
1371 *VOLATILE_CAST(kmp_int64 *) & vold.b,
1372 *VOLATILE_CAST(kmp_int64 *) & vnew.b)) {
1374 KMP_COUNT_DEVELOPER_VALUE(FOR_static_steal_stolen,
1375 vold.p.ub - vnew.p.ub);
1380 vold.p.count = init + 1;
1382 KMP_XCHG_FIXED64((
volatile kmp_int64 *)(&pr->u.p.count), vold.b);
1384 *(
volatile kmp_int64 *)(&pr->u.p.count) = vold.b;
1399 start = pr->u.p.parm2;
1401 limit = chunk + init - 1;
1403 KMP_COUNT_DEVELOPER_VALUE(FOR_static_steal_chunks, 1);
1405 KMP_DEBUG_ASSERT(init <= trip);
1406 if ((last = (limit >= trip)) != 0)
1412 *p_lb = start + init;
1413 *p_ub = start + limit;
1415 *p_lb = start + init * incr;
1416 *p_ub = start + limit * incr;
1419 if (pr->flags.ordered) {
1420 pr->u.p.ordered_lower = init;
1421 pr->u.p.ordered_upper = limit;
1426 #endif // ( KMP_STATIC_STEAL_ENABLED )
1427 case kmp_sch_static_balanced: {
1430 (
"__kmp_dispatch_next_algorithm: T#%d kmp_sch_static_balanced case\n",
1433 if ((status = !pr->u.p.count) != 0) {
1437 last = pr->u.p.parm1;
1441 pr->u.p.lb = pr->u.p.ub + pr->u.p.st;
1445 case kmp_sch_static_greedy:
1447 case kmp_sch_static_chunked: {
1450 KD_TRACE(100, (
"__kmp_dispatch_next_algorithm: T#%d "
1451 "kmp_sch_static_[affinity|chunked] case\n",
1453 parm1 = pr->u.p.parm1;
1455 trip = pr->u.p.tc - 1;
1456 init = parm1 * (pr->u.p.count + tid);
1458 if ((status = (init <= trip)) != 0) {
1461 limit = parm1 + init - 1;
1463 if ((last = (limit >= trip)) != 0)
1469 pr->u.p.count += nproc;
1472 *p_lb = start + init;
1473 *p_ub = start + limit;
1475 *p_lb = start + init * incr;
1476 *p_ub = start + limit * incr;
1479 if (pr->flags.ordered) {
1480 pr->u.p.ordered_lower = init;
1481 pr->u.p.ordered_upper = limit;
1487 case kmp_sch_dynamic_chunked: {
1488 T chunk = pr->u.p.parm1;
1492 (
"__kmp_dispatch_next_algorithm: T#%d kmp_sch_dynamic_chunked case\n",
1495 init = chunk * test_then_inc_acq<ST>((
volatile ST *)&sh->u.s.iteration);
1496 trip = pr->u.p.tc - 1;
1498 if ((status = (init <= trip)) == 0) {
1505 limit = chunk + init - 1;
1508 if ((last = (limit >= trip)) != 0)
1515 *p_lb = start + init;
1516 *p_ub = start + limit;
1518 *p_lb = start + init * incr;
1519 *p_ub = start + limit * incr;
1522 if (pr->flags.ordered) {
1523 pr->u.p.ordered_lower = init;
1524 pr->u.p.ordered_upper = limit;
1530 case kmp_sch_guided_iterative_chunked: {
1531 T chunkspec = pr->u.p.parm1;
1532 KD_TRACE(100, (
"__kmp_dispatch_next_algorithm: T#%d kmp_sch_guided_chunked "
1539 init = sh->u.s.iteration;
1540 remaining = trip - init;
1541 if (remaining <= 0) {
1550 init = test_then_add<ST>(RCAST(
volatile ST *, &sh->u.s.iteration),
1552 remaining = trip - init;
1553 if (remaining <= 0) {
1558 if ((T)remaining > chunkspec) {
1559 limit = init + chunkspec - 1;
1562 limit = init + remaining - 1;
1568 (UT)(remaining * *(
double *)&pr->u.p.parm3);
1569 if (compare_and_swap<ST>(RCAST(
volatile ST *, &sh->u.s.iteration),
1570 (ST)init, (ST)limit)) {
1582 *p_lb = start + init * incr;
1583 *p_ub = start + limit * incr;
1584 if (pr->flags.ordered) {
1585 pr->u.p.ordered_lower = init;
1586 pr->u.p.ordered_upper = limit;
1598 case kmp_sch_guided_simd: {
1601 T chunk = pr->u.p.parm1;
1603 (
"__kmp_dispatch_next_algorithm: T#%d kmp_sch_guided_simd case\n",
1609 init = sh->u.s.iteration;
1610 remaining = trip - init;
1611 if (remaining <= 0) {
1615 KMP_DEBUG_ASSERT(init % chunk == 0);
1617 if ((T)remaining < pr->u.p.parm2) {
1620 init = test_then_add<ST>(RCAST(
volatile ST *, &sh->u.s.iteration),
1622 remaining = trip - init;
1623 if (remaining <= 0) {
1628 if ((T)remaining > chunk) {
1629 limit = init + chunk - 1;
1632 limit = init + remaining - 1;
1638 UT span = remaining * (*(
double *)&pr->u.p.parm3);
1639 UT rem = span % chunk;
1641 span += chunk - rem;
1642 limit = init + span;
1643 if (compare_and_swap<ST>(RCAST(
volatile ST *, &sh->u.s.iteration),
1644 (ST)init, (ST)limit)) {
1656 *p_lb = start + init * incr;
1657 *p_ub = start + limit * incr;
1658 if (pr->flags.ordered) {
1659 pr->u.p.ordered_lower = init;
1660 pr->u.p.ordered_upper = limit;
1670 #endif // OMP_45_ENABLED
1672 case kmp_sch_guided_analytical_chunked: {
1673 T chunkspec = pr->u.p.parm1;
1675 #if KMP_USE_X87CONTROL
1678 unsigned int oldFpcw;
1679 unsigned int fpcwSet = 0;
1681 KD_TRACE(100, (
"__kmp_dispatch_next_algorithm: T#%d "
1682 "kmp_sch_guided_analytical_chunked case\n",
1687 KMP_DEBUG_ASSERT(nproc > 1);
1688 KMP_DEBUG_ASSERT((2UL * chunkspec + 1) * (UT)nproc < trip);
1692 chunkIdx = test_then_inc_acq<ST>((
volatile ST *)&sh->u.s.iteration);
1693 if (chunkIdx >= (UT)pr->u.p.parm2) {
1696 init = chunkIdx * chunkspec + pr->u.p.count;
1699 if ((status = (init > 0 && init <= trip)) != 0) {
1700 limit = init + chunkspec - 1;
1702 if ((last = (limit >= trip)) != 0)
1712 #if KMP_USE_X87CONTROL
1717 oldFpcw = _control87(0, 0);
1718 _control87(_PC_64, _MCW_PC);
1723 init = __kmp_dispatch_guided_remaining<T>(
1724 trip, *(DBL *)&pr->u.p.parm3, chunkIdx);
1725 KMP_DEBUG_ASSERT(init);
1729 limit = trip - __kmp_dispatch_guided_remaining<T>(
1730 trip, *(DBL *)&pr->u.p.parm3, chunkIdx + 1);
1731 KMP_ASSERT(init <= limit);
1733 KMP_DEBUG_ASSERT(limit <= trip);
1740 #if KMP_USE_X87CONTROL
1744 if (fpcwSet && (oldFpcw & fpcwSet))
1745 _control87(oldFpcw, _MCW_PC);
1752 *p_lb = start + init * incr;
1753 *p_ub = start + limit * incr;
1754 if (pr->flags.ordered) {
1755 pr->u.p.ordered_lower = init;
1756 pr->u.p.ordered_upper = limit;
1767 case kmp_sch_trapezoidal: {
1769 T parm2 = pr->u.p.parm2;
1770 T parm3 = pr->u.p.parm3;
1771 T parm4 = pr->u.p.parm4;
1773 (
"__kmp_dispatch_next_algorithm: T#%d kmp_sch_trapezoidal case\n",
1776 index = test_then_inc<ST>((
volatile ST *)&sh->u.s.iteration);
1778 init = (index * ((2 * parm2) - (index - 1) * parm4)) / 2;
1779 trip = pr->u.p.tc - 1;
1781 if ((status = ((T)index < parm3 && init <= trip)) == 0) {
1788 limit = ((index + 1) * (2 * parm2 - index * parm4)) / 2 - 1;
1791 if ((last = (limit >= trip)) != 0)
1798 *p_lb = start + init;
1799 *p_ub = start + limit;
1801 *p_lb = start + init * incr;
1802 *p_ub = start + limit * incr;
1805 if (pr->flags.ordered) {
1806 pr->u.p.ordered_lower = init;
1807 pr->u.p.ordered_upper = limit;
1814 __kmp_fatal(KMP_MSG(UnknownSchedTypeDetected),
1815 KMP_HNT(GetNewerLibrary),
1823 if (pr->flags.ordered) {
1826 buff = __kmp_str_format(
"__kmp_dispatch_next_algorithm: T#%%d "
1827 "ordered_lower:%%%s ordered_upper:%%%s\n",
1828 traits_t<UT>::spec, traits_t<UT>::spec);
1829 KD_TRACE(1000, (buff, gtid, pr->u.p.ordered_lower, pr->u.p.ordered_upper));
1830 __kmp_str_free(&buff);
1835 buff = __kmp_str_format(
1836 "__kmp_dispatch_next_algorithm: T#%%d exit status:%%d p_last:%%d "
1837 "p_lb:%%%s p_ub:%%%s p_st:%%%s\n",
1838 traits_t<T>::spec, traits_t<T>::spec, traits_t<ST>::spec);
1839 KD_TRACE(10, (buff, gtid, status, *p_last, *p_lb, *p_ub, *p_st));
1840 __kmp_str_free(&buff);
1849 #if OMPT_SUPPORT && OMPT_OPTIONAL
1850 #define OMPT_LOOP_END \
1851 if (status == 0) { \
1852 if (ompt_enabled.ompt_callback_work) { \
1853 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL); \
1854 ompt_task_info_t *task_info = __ompt_get_task_info_object(0); \
1855 ompt_callbacks.ompt_callback(ompt_callback_work)( \
1856 ompt_work_loop, ompt_scope_end, &(team_info->parallel_data), \
1857 &(task_info->task_data), 0, codeptr); \
1862 #define OMPT_LOOP_END // no-op
1865 #if KMP_STATS_ENABLED
1866 #define KMP_STATS_LOOP_END \
1868 kmp_int64 u, l, t, i; \
1869 l = (kmp_int64)(*p_lb); \
1870 u = (kmp_int64)(*p_ub); \
1871 i = (kmp_int64)(pr->u.p.st); \
1872 if (status == 0) { \
1874 KMP_POP_PARTITIONED_TIMER(); \
1875 } else if (i == 1) { \
1880 } else if (i < 0) { \
1882 t = (l - u) / (-i) + 1; \
1887 t = (u - l) / i + 1; \
1891 KMP_COUNT_VALUE(OMP_loop_dynamic_iterations, t); \
1894 #define KMP_STATS_LOOP_END
1897 template <
typename T>
1898 static int __kmp_dispatch_next(
ident_t *loc,
int gtid, kmp_int32 *p_last,
1900 typename traits_t<T>::signed_t *p_st
1901 #
if OMPT_SUPPORT && OMPT_OPTIONAL
1907 typedef typename traits_t<T>::unsigned_t UT;
1908 typedef typename traits_t<T>::signed_t ST;
1913 KMP_TIME_PARTITIONED_BLOCK(OMP_loop_dynamic_scheduling);
1916 dispatch_private_info_template<T> *pr;
1917 kmp_info_t *th = __kmp_threads[gtid];
1918 kmp_team_t *team = th->th.th_team;
1920 KMP_DEBUG_ASSERT(p_lb && p_ub && p_st);
1923 (
"__kmp_dispatch_next: T#%d called p_lb:%p p_ub:%p p_st:%p p_last: %p\n",
1924 gtid, p_lb, p_ub, p_st, p_last));
1926 if (team->t.t_serialized) {
1928 pr =
reinterpret_cast<dispatch_private_info_template<T> *
>(
1929 th->th.th_dispatch->th_disp_buffer);
1930 KMP_DEBUG_ASSERT(pr);
1932 if ((status = (pr->u.p.tc != 0)) == 0) {
1939 if (__kmp_env_consistency_check) {
1940 if (pr->pushed_ws != ct_none) {
1941 pr->pushed_ws = __kmp_pop_workshare(gtid, pr->pushed_ws, loc);
1944 }
else if (pr->flags.nomerge) {
1947 UT limit, trip, init;
1949 T chunk = pr->u.p.parm1;
1951 KD_TRACE(100, (
"__kmp_dispatch_next: T#%d kmp_sch_dynamic_chunked case\n",
1954 init = chunk * pr->u.p.count++;
1955 trip = pr->u.p.tc - 1;
1957 if ((status = (init <= trip)) == 0) {
1964 if (__kmp_env_consistency_check) {
1965 if (pr->pushed_ws != ct_none) {
1966 pr->pushed_ws = __kmp_pop_workshare(gtid, pr->pushed_ws, loc);
1971 limit = chunk + init - 1;
1974 if ((last = (limit >= trip)) != 0) {
1977 pr->u.p.last_upper = pr->u.p.ub;
1985 *p_lb = start + init;
1986 *p_ub = start + limit;
1988 *p_lb = start + init * incr;
1989 *p_ub = start + limit * incr;
1992 if (pr->flags.ordered) {
1993 pr->u.p.ordered_lower = init;
1994 pr->u.p.ordered_upper = limit;
1999 buff = __kmp_str_format(
"__kmp_dispatch_next: T#%%d "
2000 "ordered_lower:%%%s ordered_upper:%%%s\n",
2001 traits_t<UT>::spec, traits_t<UT>::spec);
2002 KD_TRACE(1000, (buff, gtid, pr->u.p.ordered_lower,
2003 pr->u.p.ordered_upper));
2004 __kmp_str_free(&buff);
2014 pr->u.p.last_upper = *p_ub;
2025 buff = __kmp_str_format(
2026 "__kmp_dispatch_next: T#%%d serialized case: p_lb:%%%s "
2027 "p_ub:%%%s p_st:%%%s p_last:%%p %%d returning:%%d\n",
2028 traits_t<T>::spec, traits_t<T>::spec, traits_t<ST>::spec);
2029 KD_TRACE(10, (buff, gtid, *p_lb, *p_ub, *p_st, p_last, *p_last, status));
2030 __kmp_str_free(&buff);
2033 #if INCLUDE_SSC_MARKS
2034 SSC_MARK_DISPATCH_NEXT();
2041 dispatch_shared_info_template<T>
volatile *sh;
2043 KMP_DEBUG_ASSERT(th->th.th_dispatch ==
2044 &th->th.th_team->t.t_dispatch[th->th.th_info.ds.ds_tid]);
2046 pr =
reinterpret_cast<dispatch_private_info_template<T> *
>(
2047 th->th.th_dispatch->th_dispatch_pr_current);
2048 KMP_DEBUG_ASSERT(pr);
2049 sh =
reinterpret_cast<dispatch_shared_info_template<T>
volatile *
>(
2050 th->th.th_dispatch->th_dispatch_sh_current);
2051 KMP_DEBUG_ASSERT(sh);
2053 #if KMP_USE_HIER_SCHED
2054 if (pr->flags.use_hier)
2055 status = sh->hier->next(loc, gtid, pr, &last, p_lb, p_ub, p_st);
2057 #endif // KMP_USE_HIER_SCHED
2058 status = __kmp_dispatch_next_algorithm<T>(gtid, pr, sh, &last, p_lb, p_ub,
2059 p_st, th->th.th_team_nproc,
2060 th->th.th_info.ds.ds_tid);
2065 num_done = test_then_inc<ST>((
volatile ST *)&sh->u.s.num_done);
2070 buff = __kmp_str_format(
2071 "__kmp_dispatch_next: T#%%d increment num_done:%%%s\n",
2072 traits_t<UT>::spec);
2073 KD_TRACE(10, (buff, gtid, sh->u.s.num_done));
2074 __kmp_str_free(&buff);
2078 #if KMP_USE_HIER_SCHED
2079 pr->flags.use_hier = FALSE;
2081 if ((ST)num_done == th->th.th_team_nproc - 1) {
2082 #if (KMP_STATIC_STEAL_ENABLED)
2083 if (pr->schedule == kmp_sch_static_steal &&
2084 traits_t<T>::type_size > 4) {
2086 kmp_info_t **other_threads = team->t.t_threads;
2088 for (i = 0; i < th->th.th_team_nproc; ++i) {
2089 kmp_lock_t *lck = other_threads[i]->th.th_dispatch->th_steal_lock;
2090 KMP_ASSERT(lck != NULL);
2091 __kmp_destroy_lock(lck);
2093 other_threads[i]->th.th_dispatch->th_steal_lock = NULL;
2101 sh->u.s.num_done = 0;
2102 sh->u.s.iteration = 0;
2105 if (pr->flags.ordered) {
2106 sh->u.s.ordered_iteration = 0;
2111 sh->buffer_index += __kmp_dispatch_num_buffers;
2112 KD_TRACE(100, (
"__kmp_dispatch_next: T#%d change buffer_index:%d\n",
2113 gtid, sh->buffer_index));
2118 if (__kmp_env_consistency_check) {
2119 if (pr->pushed_ws != ct_none) {
2120 pr->pushed_ws = __kmp_pop_workshare(gtid, pr->pushed_ws, loc);
2124 th->th.th_dispatch->th_deo_fcn = NULL;
2125 th->th.th_dispatch->th_dxo_fcn = NULL;
2126 th->th.th_dispatch->th_dispatch_sh_current = NULL;
2127 th->th.th_dispatch->th_dispatch_pr_current = NULL;
2131 pr->u.p.last_upper = pr->u.p.ub;
2134 if (p_last != NULL && status != 0)
2142 buff = __kmp_str_format(
2143 "__kmp_dispatch_next: T#%%d normal case: "
2144 "p_lb:%%%s p_ub:%%%s p_st:%%%s p_last:%%p (%%d) returning:%%d\n",
2145 traits_t<T>::spec, traits_t<T>::spec, traits_t<ST>::spec);
2146 KD_TRACE(10, (buff, gtid, *p_lb, *p_ub, p_st ? *p_st : 0, p_last,
2147 (p_last ? *p_last : 0), status));
2148 __kmp_str_free(&buff);
2151 #if INCLUDE_SSC_MARKS
2152 SSC_MARK_DISPATCH_NEXT();
2159 template <
typename T>
2160 static void __kmp_dist_get_bounds(
ident_t *loc, kmp_int32 gtid,
2161 kmp_int32 *plastiter, T *plower, T *pupper,
2162 typename traits_t<T>::signed_t incr) {
2163 typedef typename traits_t<T>::unsigned_t UT;
2170 KMP_DEBUG_ASSERT(plastiter && plower && pupper);
2171 KE_TRACE(10, (
"__kmpc_dist_get_bounds called (%d)\n", gtid));
2173 typedef typename traits_t<T>::signed_t ST;
2177 buff = __kmp_str_format(
"__kmpc_dist_get_bounds: T#%%d liter=%%d "
2178 "iter=(%%%s, %%%s, %%%s) signed?<%s>\n",
2179 traits_t<T>::spec, traits_t<T>::spec,
2180 traits_t<ST>::spec, traits_t<T>::spec);
2181 KD_TRACE(100, (buff, gtid, *plastiter, *plower, *pupper, incr));
2182 __kmp_str_free(&buff);
2186 if (__kmp_env_consistency_check) {
2188 __kmp_error_construct(kmp_i18n_msg_CnsLoopIncrZeroProhibited, ct_pdo,
2191 if (incr > 0 ? (*pupper < *plower) : (*plower < *pupper)) {
2201 __kmp_error_construct(kmp_i18n_msg_CnsLoopIncrIllegal, ct_pdo, loc);
2204 th = __kmp_threads[gtid];
2205 team = th->th.th_team;
2207 KMP_DEBUG_ASSERT(th->th.th_teams_microtask);
2208 nteams = th->th.th_teams_size.nteams;
2210 team_id = team->t.t_master_tid;
2211 KMP_DEBUG_ASSERT(nteams == (kmp_uint32)team->t.t_parent->t.t_nproc);
2215 trip_count = *pupper - *plower + 1;
2216 }
else if (incr == -1) {
2217 trip_count = *plower - *pupper + 1;
2218 }
else if (incr > 0) {
2220 trip_count = (UT)(*pupper - *plower) / incr + 1;
2222 trip_count = (UT)(*plower - *pupper) / (-incr) + 1;
2225 if (trip_count <= nteams) {
2227 __kmp_static == kmp_sch_static_greedy ||
2229 kmp_sch_static_balanced);
2231 if (team_id < trip_count) {
2232 *pupper = *plower = *plower + team_id * incr;
2234 *plower = *pupper + incr;
2236 if (plastiter != NULL)
2237 *plastiter = (team_id == trip_count - 1);
2239 if (__kmp_static == kmp_sch_static_balanced) {
2240 UT chunk = trip_count / nteams;
2241 UT extras = trip_count % nteams;
2243 incr * (team_id * chunk + (team_id < extras ? team_id : extras));
2244 *pupper = *plower + chunk * incr - (team_id < extras ? 0 : incr);
2245 if (plastiter != NULL)
2246 *plastiter = (team_id == nteams - 1);
2249 (trip_count / nteams + ((trip_count % nteams) ? 1 : 0)) * incr;
2251 KMP_DEBUG_ASSERT(__kmp_static == kmp_sch_static_greedy);
2253 *plower += team_id * chunk_inc_count;
2254 *pupper = *plower + chunk_inc_count - incr;
2257 if (*pupper < *plower)
2258 *pupper = traits_t<T>::max_value;
2259 if (plastiter != NULL)
2260 *plastiter = *plower <= upper && *pupper > upper - incr;
2261 if (*pupper > upper)
2264 if (*pupper > *plower)
2265 *pupper = traits_t<T>::min_value;
2266 if (plastiter != NULL)
2267 *plastiter = *plower >= upper && *pupper < upper - incr;
2268 if (*pupper < upper)
2300 kmp_int32 ub, kmp_int32 st, kmp_int32 chunk) {
2301 KMP_DEBUG_ASSERT(__kmp_init_serial);
2302 #if OMPT_SUPPORT && OMPT_OPTIONAL
2303 OMPT_STORE_RETURN_ADDRESS(gtid);
2305 __kmp_dispatch_init<kmp_int32>(loc, gtid, schedule, lb, ub, st, chunk,
true);
2312 kmp_uint32 ub, kmp_int32 st, kmp_int32 chunk) {
2313 KMP_DEBUG_ASSERT(__kmp_init_serial);
2314 #if OMPT_SUPPORT && OMPT_OPTIONAL
2315 OMPT_STORE_RETURN_ADDRESS(gtid);
2317 __kmp_dispatch_init<kmp_uint32>(loc, gtid, schedule, lb, ub, st, chunk,
true);
2325 kmp_int64 ub, kmp_int64 st, kmp_int64 chunk) {
2326 KMP_DEBUG_ASSERT(__kmp_init_serial);
2327 #if OMPT_SUPPORT && OMPT_OPTIONAL
2328 OMPT_STORE_RETURN_ADDRESS(gtid);
2330 __kmp_dispatch_init<kmp_int64>(loc, gtid, schedule, lb, ub, st, chunk,
true);
2338 kmp_uint64 ub, kmp_int64 st, kmp_int64 chunk) {
2339 KMP_DEBUG_ASSERT(__kmp_init_serial);
2340 #if OMPT_SUPPORT && OMPT_OPTIONAL
2341 OMPT_STORE_RETURN_ADDRESS(gtid);
2343 __kmp_dispatch_init<kmp_uint64>(loc, gtid, schedule, lb, ub, st, chunk,
true);
2357 kmp_int32 lb, kmp_int32 ub, kmp_int32 st,
2359 KMP_DEBUG_ASSERT(__kmp_init_serial);
2360 #if OMPT_SUPPORT && OMPT_OPTIONAL
2361 OMPT_STORE_RETURN_ADDRESS(gtid);
2363 __kmp_dist_get_bounds<kmp_int32>(loc, gtid, p_last, &lb, &ub, st);
2364 __kmp_dispatch_init<kmp_int32>(loc, gtid, schedule, lb, ub, st, chunk,
true);
2367 void __kmpc_dist_dispatch_init_4u(
ident_t *loc, kmp_int32 gtid,
2369 kmp_uint32 lb, kmp_uint32 ub, kmp_int32 st,
2371 KMP_DEBUG_ASSERT(__kmp_init_serial);
2372 #if OMPT_SUPPORT && OMPT_OPTIONAL
2373 OMPT_STORE_RETURN_ADDRESS(gtid);
2375 __kmp_dist_get_bounds<kmp_uint32>(loc, gtid, p_last, &lb, &ub, st);
2376 __kmp_dispatch_init<kmp_uint32>(loc, gtid, schedule, lb, ub, st, chunk,
true);
2379 void __kmpc_dist_dispatch_init_8(
ident_t *loc, kmp_int32 gtid,
2381 kmp_int64 lb, kmp_int64 ub, kmp_int64 st,
2383 KMP_DEBUG_ASSERT(__kmp_init_serial);
2384 #if OMPT_SUPPORT && OMPT_OPTIONAL
2385 OMPT_STORE_RETURN_ADDRESS(gtid);
2387 __kmp_dist_get_bounds<kmp_int64>(loc, gtid, p_last, &lb, &ub, st);
2388 __kmp_dispatch_init<kmp_int64>(loc, gtid, schedule, lb, ub, st, chunk,
true);
2391 void __kmpc_dist_dispatch_init_8u(
ident_t *loc, kmp_int32 gtid,
2393 kmp_uint64 lb, kmp_uint64 ub, kmp_int64 st,
2395 KMP_DEBUG_ASSERT(__kmp_init_serial);
2396 #if OMPT_SUPPORT && OMPT_OPTIONAL
2397 OMPT_STORE_RETURN_ADDRESS(gtid);
2399 __kmp_dist_get_bounds<kmp_uint64>(loc, gtid, p_last, &lb, &ub, st);
2400 __kmp_dispatch_init<kmp_uint64>(loc, gtid, schedule, lb, ub, st, chunk,
true);
2417 kmp_int32 *p_lb, kmp_int32 *p_ub, kmp_int32 *p_st) {
2418 #if OMPT_SUPPORT && OMPT_OPTIONAL
2419 OMPT_STORE_RETURN_ADDRESS(gtid);
2421 return __kmp_dispatch_next<kmp_int32>(loc, gtid, p_last, p_lb, p_ub, p_st
2422 #if OMPT_SUPPORT && OMPT_OPTIONAL
2424 OMPT_LOAD_RETURN_ADDRESS(gtid)
2433 kmp_uint32 *p_lb, kmp_uint32 *p_ub,
2435 #if OMPT_SUPPORT && OMPT_OPTIONAL
2436 OMPT_STORE_RETURN_ADDRESS(gtid);
2438 return __kmp_dispatch_next<kmp_uint32>(loc, gtid, p_last, p_lb, p_ub, p_st
2439 #if OMPT_SUPPORT && OMPT_OPTIONAL
2441 OMPT_LOAD_RETURN_ADDRESS(gtid)
2450 kmp_int64 *p_lb, kmp_int64 *p_ub, kmp_int64 *p_st) {
2451 #if OMPT_SUPPORT && OMPT_OPTIONAL
2452 OMPT_STORE_RETURN_ADDRESS(gtid);
2454 return __kmp_dispatch_next<kmp_int64>(loc, gtid, p_last, p_lb, p_ub, p_st
2455 #if OMPT_SUPPORT && OMPT_OPTIONAL
2457 OMPT_LOAD_RETURN_ADDRESS(gtid)
2466 kmp_uint64 *p_lb, kmp_uint64 *p_ub,
2468 #if OMPT_SUPPORT && OMPT_OPTIONAL
2469 OMPT_STORE_RETURN_ADDRESS(gtid);
2471 return __kmp_dispatch_next<kmp_uint64>(loc, gtid, p_last, p_lb, p_ub, p_st
2472 #if OMPT_SUPPORT && OMPT_OPTIONAL
2474 OMPT_LOAD_RETURN_ADDRESS(gtid)
2486 __kmp_dispatch_finish<kmp_uint32>(gtid, loc);
2493 __kmp_dispatch_finish<kmp_uint64>(gtid, loc);
2500 __kmp_dispatch_finish<kmp_uint32>(gtid, loc);
2507 __kmp_dispatch_finish<kmp_uint64>(gtid, loc);
2514 kmp_uint32 __kmp_eq_4(kmp_uint32 value, kmp_uint32 checker) {
2515 return value == checker;
2518 kmp_uint32 __kmp_neq_4(kmp_uint32 value, kmp_uint32 checker) {
2519 return value != checker;
2522 kmp_uint32 __kmp_lt_4(kmp_uint32 value, kmp_uint32 checker) {
2523 return value < checker;
2526 kmp_uint32 __kmp_ge_4(kmp_uint32 value, kmp_uint32 checker) {
2527 return value >= checker;
2530 kmp_uint32 __kmp_le_4(kmp_uint32 value, kmp_uint32 checker) {
2531 return value <= checker;
2535 __kmp_wait_4(
volatile kmp_uint32 *spinner, kmp_uint32 checker,
2536 kmp_uint32 (*pred)(kmp_uint32, kmp_uint32),
2540 volatile kmp_uint32 *spin = spinner;
2541 kmp_uint32 check = checker;
2543 kmp_uint32 (*f)(kmp_uint32, kmp_uint32) = pred;
2546 KMP_FSYNC_SPIN_INIT(obj, CCAST(kmp_uint32 *, spin));
2547 KMP_INIT_YIELD(spins);
2549 while (!f(r = TCR_4(*spin), check)) {
2550 KMP_FSYNC_SPIN_PREPARE(obj);
2555 KMP_YIELD_OVERSUB_ELSE_SPIN(spins);
2557 KMP_FSYNC_SPIN_ACQUIRED(obj);
2561 void __kmp_wait_4_ptr(
void *spinner, kmp_uint32 checker,
2562 kmp_uint32 (*pred)(
void *, kmp_uint32),
2566 void *spin = spinner;
2567 kmp_uint32 check = checker;
2569 kmp_uint32 (*f)(
void *, kmp_uint32) = pred;
2571 KMP_FSYNC_SPIN_INIT(obj, spin);
2572 KMP_INIT_YIELD(spins);
2574 while (!f(spin, check)) {
2575 KMP_FSYNC_SPIN_PREPARE(obj);
2578 KMP_YIELD_OVERSUB_ELSE_SPIN(spins);
2580 KMP_FSYNC_SPIN_ACQUIRED(obj);
2585 #ifdef KMP_GOMP_COMPAT
2587 void __kmp_aux_dispatch_init_4(
ident_t *loc, kmp_int32 gtid,
2589 kmp_int32 ub, kmp_int32 st, kmp_int32 chunk,
2591 __kmp_dispatch_init<kmp_int32>(loc, gtid, schedule, lb, ub, st, chunk,
2595 void __kmp_aux_dispatch_init_4u(
ident_t *loc, kmp_int32 gtid,
2597 kmp_uint32 ub, kmp_int32 st, kmp_int32 chunk,
2599 __kmp_dispatch_init<kmp_uint32>(loc, gtid, schedule, lb, ub, st, chunk,
2603 void __kmp_aux_dispatch_init_8(
ident_t *loc, kmp_int32 gtid,
2605 kmp_int64 ub, kmp_int64 st, kmp_int64 chunk,
2607 __kmp_dispatch_init<kmp_int64>(loc, gtid, schedule, lb, ub, st, chunk,
2611 void __kmp_aux_dispatch_init_8u(
ident_t *loc, kmp_int32 gtid,
2613 kmp_uint64 ub, kmp_int64 st, kmp_int64 chunk,
2615 __kmp_dispatch_init<kmp_uint64>(loc, gtid, schedule, lb, ub, st, chunk,
2619 void __kmp_aux_dispatch_fini_chunk_4(
ident_t *loc, kmp_int32 gtid) {
2620 __kmp_dispatch_finish_chunk<kmp_uint32>(gtid, loc);
2623 void __kmp_aux_dispatch_fini_chunk_8(
ident_t *loc, kmp_int32 gtid) {
2624 __kmp_dispatch_finish_chunk<kmp_uint64>(gtid, loc);
2627 void __kmp_aux_dispatch_fini_chunk_4u(
ident_t *loc, kmp_int32 gtid) {
2628 __kmp_dispatch_finish_chunk<kmp_uint32>(gtid, loc);
2631 void __kmp_aux_dispatch_fini_chunk_8u(
ident_t *loc, kmp_int32 gtid) {
2632 __kmp_dispatch_finish_chunk<kmp_uint64>(gtid, loc);
void __kmpc_dispatch_fini_4(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)
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)
#define KMP_COUNT_VALUE(name, value)
Adds value to specified timer (name).
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)
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)
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)
#define KMP_COUNT_BLOCK(name)
Increments specified counter (name).
void __kmpc_dispatch_fini_8u(ident_t *loc, kmp_int32 gtid)
void __kmpc_dispatch_fini_4u(ident_t *loc, kmp_int32 gtid)
void __kmpc_dispatch_fini_8(ident_t *loc, kmp_int32 gtid)
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_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_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_init_8(ident_t *loc, kmp_int32 gtid, enum sched_type schedule, kmp_int64 lb, kmp_int64 ub, kmp_int64 st, kmp_int64 chunk)