23 #if defined(_WIN32_WINNT) && defined(_M_IX86) 25 #define _WIN32_WINNT 0x0502 29 #include "kmp_error.h" 32 #include "kmp_stats.h" 34 #if KMP_OS_WINDOWS && KMP_ARCH_X86 38 #include "kmp_dispatch.h" 39 #if KMP_USE_HIER_SCHED 40 #include "kmp_dispatch_hier.h" 44 #include "ompt-specific.h" 50 void __kmp_dispatch_deo_error(
int *gtid_ref,
int *cid_ref,
ident_t *loc_ref) {
53 KMP_DEBUG_ASSERT(gtid_ref);
55 if (__kmp_env_consistency_check) {
56 th = __kmp_threads[*gtid_ref];
57 if (th->th.th_root->r.r_active &&
58 (th->th.th_dispatch->th_dispatch_pr_current->pushed_ws != ct_none)) {
59 #if KMP_USE_DYNAMIC_LOCK 60 __kmp_push_sync(*gtid_ref, ct_ordered_in_pdo, loc_ref, NULL, 0);
62 __kmp_push_sync(*gtid_ref, ct_ordered_in_pdo, loc_ref, NULL);
68 void __kmp_dispatch_dxo_error(
int *gtid_ref,
int *cid_ref,
ident_t *loc_ref) {
71 if (__kmp_env_consistency_check) {
72 th = __kmp_threads[*gtid_ref];
73 if (th->th.th_dispatch->th_dispatch_pr_current->pushed_ws != ct_none) {
74 __kmp_pop_sync(*gtid_ref, ct_ordered_in_pdo, loc_ref);
90 void __kmp_dispatch_init_algorithm(
ident_t *loc,
int gtid,
91 dispatch_private_info_template<T> *pr,
93 typename traits_t<T>::signed_t st,
95 kmp_uint64 *cur_chunk,
97 typename traits_t<T>::signed_t chunk,
99 typedef typename traits_t<T>::unsigned_t UT;
100 typedef typename traits_t<T>::floating_t DBL;
108 typedef typename traits_t<T>::signed_t ST;
112 buff = __kmp_str_format(
"__kmp_dispatch_init_algorithm: T#%%d called " 113 "pr:%%p lb:%%%s ub:%%%s st:%%%s " 114 "schedule:%%d chunk:%%%s nproc:%%%s tid:%%%s\n",
115 traits_t<T>::spec, traits_t<T>::spec,
116 traits_t<ST>::spec, traits_t<ST>::spec,
117 traits_t<T>::spec, traits_t<T>::spec);
118 KD_TRACE(10, (buff, gtid, pr, lb, ub, st, schedule, chunk, nproc, tid));
119 __kmp_str_free(&buff);
123 th = __kmp_threads[gtid];
124 team = th->th.th_team;
125 active = !team->t.t_serialized;
128 int itt_need_metadata_reporting = __itt_metadata_add_ptr &&
129 __kmp_forkjoin_frames_mode == 3 &&
130 KMP_MASTER_GTID(gtid) &&
132 th->th.th_teams_microtask == NULL &&
134 team->t.t_active_level == 1;
136 #if (KMP_STATIC_STEAL_ENABLED) 137 if (SCHEDULE_HAS_NONMONOTONIC(schedule))
139 schedule = kmp_sch_static_steal;
142 schedule = SCHEDULE_WITHOUT_MODIFIERS(schedule);
146 pr->flags.nomerge = TRUE;
150 pr->flags.nomerge = FALSE;
152 pr->type_size = traits_t<T>::type_size;
154 pr->flags.ordered = TRUE;
158 pr->flags.ordered = FALSE;
162 schedule = __kmp_static;
164 if (schedule == kmp_sch_runtime) {
167 schedule = team->t.t_sched.r_sched_type;
171 schedule = __kmp_guided;
173 schedule = __kmp_static;
177 chunk = team->t.t_sched.chunk;
186 buff = __kmp_str_format(
"__kmp_dispatch_init_algorithm: T#%%d new: " 187 "schedule:%%d chunk:%%%s\n",
189 KD_TRACE(10, (buff, gtid, schedule, chunk));
190 __kmp_str_free(&buff);
195 schedule = __kmp_guided;
198 chunk = KMP_DEFAULT_CHUNK;
204 schedule = __kmp_auto;
209 buff = __kmp_str_format(
210 "__kmp_dispatch_init_algorithm: kmp_sch_auto: T#%%d new: " 211 "schedule:%%d chunk:%%%s\n",
213 KD_TRACE(10, (buff, gtid, schedule, chunk));
214 __kmp_str_free(&buff);
220 if (schedule == kmp_sch_guided_analytical_chunked && nproc > 1 << 20) {
221 schedule = kmp_sch_guided_iterative_chunked;
222 KMP_WARNING(DispatchManyThreads);
225 if (schedule == kmp_sch_runtime_simd) {
227 schedule = team->t.t_sched.r_sched_type;
231 schedule == __kmp_static) {
232 schedule = kmp_sch_static_balanced_chunked;
235 schedule = kmp_sch_guided_simd;
237 chunk = team->t.t_sched.chunk * chunk;
247 buff = __kmp_str_format(
"__kmp_dispatch_init: T#%%d new: schedule:%%d" 250 KD_TRACE(10, (buff, gtid, schedule, chunk));
251 __kmp_str_free(&buff);
255 #endif // OMP_45_ENABLED 256 pr->u.p.parm1 = chunk;
259 "unknown scheduling type");
263 if (__kmp_env_consistency_check) {
265 __kmp_error_construct(kmp_i18n_msg_CnsLoopIncrZeroProhibited,
266 (pr->flags.ordered ? ct_pdo_ordered : ct_pdo), loc);
280 tc = (UT)(lb - ub) / (-st) + 1;
288 tc = (UT)(ub - lb) / st + 1;
300 pr->u.p.last_upper = ub + st;
306 if (pr->flags.ordered) {
307 pr->ordered_bumped = 0;
308 pr->u.p.ordered_lower = 1;
309 pr->u.p.ordered_upper = 0;
314 #if (KMP_STATIC_STEAL_ENABLED) 315 case kmp_sch_static_steal: {
319 (
"__kmp_dispatch_init_algorithm: T#%d kmp_sch_static_steal case\n",
322 ntc = (tc % chunk ? 1 : 0) + tc / chunk;
323 if (nproc > 1 && ntc >= nproc) {
326 T small_chunk, extras;
328 small_chunk = ntc / nproc;
329 extras = ntc % nproc;
331 init =
id * small_chunk + (
id < extras ? id : extras);
332 pr->u.p.count = init;
333 pr->u.p.ub = init + small_chunk + (
id < extras ? 1 : 0);
337 pr->u.p.parm4 = (
id + 1) % nproc;
339 if (traits_t<T>::type_size > 4) {
345 KMP_DEBUG_ASSERT(th->th.th_dispatch->th_steal_lock == NULL);
346 th->th.th_dispatch->th_steal_lock =
347 (kmp_lock_t *)__kmp_allocate(
sizeof(kmp_lock_t));
348 __kmp_init_lock(th->th.th_dispatch->th_steal_lock);
352 KD_TRACE(100, (
"__kmp_dispatch_init_algorithm: T#%d falling-through to " 353 "kmp_sch_static_balanced\n",
355 schedule = kmp_sch_static_balanced;
361 case kmp_sch_static_balanced: {
366 (
"__kmp_dispatch_init_algorithm: T#%d kmp_sch_static_balanced case\n",
376 pr->u.p.parm1 = (
id == tc - 1);
379 pr->u.p.parm1 = FALSE;
383 T small_chunk = tc / nproc;
384 T extras = tc % nproc;
385 init =
id * small_chunk + (
id < extras ? id : extras);
386 limit = init + small_chunk - (
id < extras ? 0 : 1);
387 pr->u.p.parm1 = (
id == nproc - 1);
393 pr->u.p.parm1 = TRUE;
397 pr->u.p.parm1 = FALSE;
403 if (itt_need_metadata_reporting)
405 *cur_chunk = limit - init + 1;
408 pr->u.p.lb = lb + init;
409 pr->u.p.ub = lb + limit;
412 T ub_tmp = lb + limit * st;
413 pr->u.p.lb = lb + init * st;
417 pr->u.p.ub = (ub_tmp + st > ub ? ub : ub_tmp);
419 pr->u.p.ub = (ub_tmp + st < ub ? ub : ub_tmp);
422 if (pr->flags.ordered) {
423 pr->u.p.ordered_lower = init;
424 pr->u.p.ordered_upper = limit;
429 case kmp_sch_static_balanced_chunked: {
432 KD_TRACE(100, (
"__kmp_dispatch_init_algorithm: T#%d runtime(simd:static)" 433 " -> falling-through to static_greedy\n",
435 schedule = kmp_sch_static_greedy;
437 pr->u.p.parm1 = ((tc + nth - 1) / nth + chunk - 1) & ~(chunk - 1);
442 case kmp_sch_guided_simd:
443 #endif // OMP_45_ENABLED 444 case kmp_sch_guided_iterative_chunked: {
447 (
"__kmp_dispatch_init_algorithm: T#%d kmp_sch_guided_iterative_chunked" 452 if ((2L * chunk + 1) * nproc >= tc) {
454 schedule = kmp_sch_dynamic_chunked;
457 pr->u.p.parm2 = guided_int_param * nproc * (chunk + 1);
458 *(
double *)&pr->u.p.parm3 =
459 guided_flt_param / nproc;
462 KD_TRACE(100, (
"__kmp_dispatch_init_algorithm: T#%d falling-through to " 463 "kmp_sch_static_greedy\n",
465 schedule = kmp_sch_static_greedy;
469 (
"__kmp_dispatch_init_algorithm: T#%d kmp_sch_static_greedy case\n",
475 case kmp_sch_guided_analytical_chunked: {
476 KD_TRACE(100, (
"__kmp_dispatch_init_algorithm: T#%d " 477 "kmp_sch_guided_analytical_chunked case\n",
481 if ((2L * chunk + 1) * nproc >= tc) {
483 schedule = kmp_sch_dynamic_chunked;
488 #if KMP_OS_WINDOWS && KMP_ARCH_X86 498 unsigned int oldFpcw = _control87(0, 0);
499 _control87(_PC_64, _MCW_PC);
502 long double target = ((
long double)chunk * 2 + 1) * nproc / tc;
509 x = (
long double)1.0 - (
long double)0.5 / nproc;
520 ptrdiff_t natural_alignment =
521 (ptrdiff_t)&t.b - (ptrdiff_t)&t - (ptrdiff_t)1;
525 (((ptrdiff_t)&pr->u.p.parm3) & (natural_alignment)) == 0);
530 *(DBL *)&pr->u.p.parm3 = x;
543 p = __kmp_pow<UT>(x, right);
548 }
while (p > target && right < (1 << 27));
556 while (left + 1 < right) {
557 mid = (left + right) / 2;
558 if (__kmp_pow<UT>(x, mid) > target) {
567 KMP_ASSERT(cross && __kmp_pow<UT>(x, cross - 1) > target &&
568 __kmp_pow<UT>(x, cross) <= target);
571 pr->u.p.parm2 = cross;
574 #if ((KMP_OS_LINUX || KMP_OS_WINDOWS) && KMP_ARCH_X86) && (!defined(KMP_I8)) 575 #define GUIDED_ANALYTICAL_WORKAROUND (*(DBL *)&pr->u.p.parm3) 577 #define GUIDED_ANALYTICAL_WORKAROUND (x) 580 pr->u.p.count = tc - __kmp_dispatch_guided_remaining(
581 tc, GUIDED_ANALYTICAL_WORKAROUND, cross) -
583 #if KMP_OS_WINDOWS && KMP_ARCH_X86 585 _control87(oldFpcw, _MCW_PC);
589 KD_TRACE(100, (
"__kmp_dispatch_init_algorithm: T#%d falling-through to " 590 "kmp_sch_static_greedy\n",
592 schedule = kmp_sch_static_greedy;
598 case kmp_sch_static_greedy:
601 (
"__kmp_dispatch_init_algorithm: T#%d kmp_sch_static_greedy case\n",
603 pr->u.p.parm1 = (nproc > 1) ? (tc + nproc - 1) / nproc : tc;
605 case kmp_sch_static_chunked:
606 case kmp_sch_dynamic_chunked:
607 if (pr->u.p.parm1 <= 0) {
608 pr->u.p.parm1 = KMP_DEFAULT_CHUNK;
610 KD_TRACE(100, (
"__kmp_dispatch_init_algorithm: T#%d " 611 "kmp_sch_static_chunked/kmp_sch_dynamic_chunked cases\n",
614 case kmp_sch_trapezoidal: {
617 T parm1, parm2, parm3, parm4;
619 (
"__kmp_dispatch_init_algorithm: T#%d kmp_sch_trapezoidal case\n",
625 parm2 = (tc / (2 * nproc));
635 }
else if (parm1 > parm2) {
640 parm3 = (parm2 + parm1);
641 parm3 = (2 * tc + parm3 - 1) / parm3;
649 parm4 = (parm2 - parm1) / parm4;
656 pr->u.p.parm1 = parm1;
657 pr->u.p.parm2 = parm2;
658 pr->u.p.parm3 = parm3;
659 pr->u.p.parm4 = parm4;
664 __kmp_fatal(KMP_MSG(UnknownSchedTypeDetected),
665 KMP_HNT(GetNewerLibrary),
670 pr->schedule = schedule;
673 #if KMP_USE_HIER_SCHED 674 template <
typename T>
675 inline void __kmp_dispatch_init_hier_runtime(
ident_t *loc, T lb, T ub,
676 typename traits_t<T>::signed_t st);
679 __kmp_dispatch_init_hier_runtime<kmp_int32>(
ident_t *loc, kmp_int32 lb,
680 kmp_int32 ub, kmp_int32 st) {
681 __kmp_dispatch_init_hierarchy<kmp_int32>(
682 loc, __kmp_hier_scheds.size, __kmp_hier_scheds.layers,
683 __kmp_hier_scheds.scheds, __kmp_hier_scheds.small_chunks, lb, ub, st);
687 __kmp_dispatch_init_hier_runtime<kmp_uint32>(
ident_t *loc, kmp_uint32 lb,
688 kmp_uint32 ub, kmp_int32 st) {
689 __kmp_dispatch_init_hierarchy<kmp_uint32>(
690 loc, __kmp_hier_scheds.size, __kmp_hier_scheds.layers,
691 __kmp_hier_scheds.scheds, __kmp_hier_scheds.small_chunks, lb, ub, st);
695 __kmp_dispatch_init_hier_runtime<kmp_int64>(
ident_t *loc, kmp_int64 lb,
696 kmp_int64 ub, kmp_int64 st) {
697 __kmp_dispatch_init_hierarchy<kmp_int64>(
698 loc, __kmp_hier_scheds.size, __kmp_hier_scheds.layers,
699 __kmp_hier_scheds.scheds, __kmp_hier_scheds.large_chunks, lb, ub, st);
703 __kmp_dispatch_init_hier_runtime<kmp_uint64>(
ident_t *loc, kmp_uint64 lb,
704 kmp_uint64 ub, kmp_int64 st) {
705 __kmp_dispatch_init_hierarchy<kmp_uint64>(
706 loc, __kmp_hier_scheds.size, __kmp_hier_scheds.layers,
707 __kmp_hier_scheds.scheds, __kmp_hier_scheds.large_chunks, lb, ub, st);
711 void __kmp_dispatch_free_hierarchies(kmp_team_t *team) {
712 int num_disp_buff = team->t.t_max_nproc > 1 ? __kmp_dispatch_num_buffers : 2;
713 for (
int i = 0; i < num_disp_buff; ++i) {
716 reinterpret_cast<dispatch_shared_info_template<kmp_int32>
volatile *
>(
717 &team->t.t_disp_buffer[i]);
719 sh->hier->deallocate();
720 __kmp_free(sh->hier);
728 template <
typename T>
731 T ub,
typename traits_t<T>::signed_t st,
732 typename traits_t<T>::signed_t chunk,
int push_ws) {
733 typedef typename traits_t<T>::unsigned_t UT;
738 kmp_uint32 my_buffer_index;
739 dispatch_private_info_template<T> *pr;
740 dispatch_shared_info_template<T>
volatile *sh;
742 KMP_BUILD_ASSERT(
sizeof(dispatch_private_info_template<T>) ==
743 sizeof(dispatch_private_info));
744 KMP_BUILD_ASSERT(
sizeof(dispatch_shared_info_template<UT>) ==
745 sizeof(dispatch_shared_info));
747 if (!TCR_4(__kmp_init_parallel))
748 __kmp_parallel_initialize();
750 #if INCLUDE_SSC_MARKS 751 SSC_MARK_DISPATCH_INIT();
754 typedef typename traits_t<T>::signed_t ST;
758 buff = __kmp_str_format(
"__kmp_dispatch_init: T#%%d called: schedule:%%d " 759 "chunk:%%%s lb:%%%s ub:%%%s st:%%%s\n",
760 traits_t<ST>::spec, traits_t<T>::spec,
761 traits_t<T>::spec, traits_t<ST>::spec);
762 KD_TRACE(10, (buff, gtid, schedule, chunk, lb, ub, st));
763 __kmp_str_free(&buff);
767 th = __kmp_threads[gtid];
768 team = th->th.th_team;
769 active = !team->t.t_serialized;
770 th->th.th_ident = loc;
775 if (schedule == __kmp_static) {
781 #if KMP_USE_HIER_SCHED 787 my_buffer_index = th->th.th_dispatch->th_disp_index;
788 pr =
reinterpret_cast<dispatch_private_info_template<T> *
>(
790 ->th_disp_buffer[my_buffer_index % __kmp_dispatch_num_buffers]);
791 my_sched = SCHEDULE_WITHOUT_MODIFIERS(my_sched);
796 if (pr->flags.use_hier) {
798 KD_TRACE(100, (
"__kmp_dispatch_init: T#%d ordered loop detected. " 799 "Disabling hierarchical scheduling.\n",
801 pr->flags.use_hier = FALSE;
804 if (schedule == kmp_sch_runtime && __kmp_hier_scheds.size > 0) {
807 if (!ordered && !pr->flags.use_hier)
808 __kmp_dispatch_init_hier_runtime<T>(loc, lb, ub, st);
810 #endif // KMP_USE_HIER_SCHED 813 kmp_uint64 cur_chunk = chunk;
814 int itt_need_metadata_reporting = __itt_metadata_add_ptr &&
815 __kmp_forkjoin_frames_mode == 3 &&
816 KMP_MASTER_GTID(gtid) &&
818 th->th.th_teams_microtask == NULL &&
820 team->t.t_active_level == 1;
823 pr =
reinterpret_cast<dispatch_private_info_template<T> *
>(
824 th->th.th_dispatch->th_disp_buffer);
826 KMP_DEBUG_ASSERT(th->th.th_dispatch ==
827 &th->th.th_team->t.t_dispatch[th->th.th_info.ds.ds_tid]);
829 my_buffer_index = th->th.th_dispatch->th_disp_index++;
832 pr =
reinterpret_cast<dispatch_private_info_template<T> *
>(
834 ->th_disp_buffer[my_buffer_index % __kmp_dispatch_num_buffers]);
835 sh =
reinterpret_cast<dispatch_shared_info_template<T>
volatile *
>(
836 &team->t.t_disp_buffer[my_buffer_index % __kmp_dispatch_num_buffers]);
837 KD_TRACE(10, (
"__kmp_dispatch_init: T#%d my_buffer_index:%d\n", gtid,
841 __kmp_dispatch_init_algorithm(loc, gtid, pr, schedule, lb, ub, st,
845 chunk, (T)th->th.th_team_nproc,
846 (T)th->th.th_info.ds.ds_tid);
848 if (pr->flags.ordered == 0) {
849 th->th.th_dispatch->th_deo_fcn = __kmp_dispatch_deo_error;
850 th->th.th_dispatch->th_dxo_fcn = __kmp_dispatch_dxo_error;
852 th->th.th_dispatch->th_deo_fcn = __kmp_dispatch_deo<UT>;
853 th->th.th_dispatch->th_dxo_fcn = __kmp_dispatch_dxo<UT>;
861 KD_TRACE(100, (
"__kmp_dispatch_init: T#%d before wait: my_buffer_index:%d " 862 "sh->buffer_index:%d\n",
863 gtid, my_buffer_index, sh->buffer_index));
864 __kmp_wait_yield<kmp_uint32>(&sh->buffer_index, my_buffer_index,
865 __kmp_eq<kmp_uint32> USE_ITT_BUILD_ARG(NULL));
869 KD_TRACE(100, (
"__kmp_dispatch_init: T#%d after wait: my_buffer_index:%d " 870 "sh->buffer_index:%d\n",
871 gtid, my_buffer_index, sh->buffer_index));
873 th->th.th_dispatch->th_dispatch_pr_current = (dispatch_private_info_t *)pr;
874 th->th.th_dispatch->th_dispatch_sh_current =
875 CCAST(dispatch_shared_info_t *, (
volatile dispatch_shared_info_t *)sh);
877 if (pr->flags.ordered) {
878 __kmp_itt_ordered_init(gtid);
881 if (itt_need_metadata_reporting) {
883 kmp_uint64 schedtype = 0;
885 case kmp_sch_static_chunked:
886 case kmp_sch_static_balanced:
888 case kmp_sch_static_greedy:
889 cur_chunk = pr->u.p.parm1;
891 case kmp_sch_dynamic_chunked:
894 case kmp_sch_guided_iterative_chunked:
895 case kmp_sch_guided_analytical_chunked:
897 case kmp_sch_guided_simd:
907 __kmp_itt_metadata_loop(loc, schedtype, pr->u.p.tc, cur_chunk);
909 #if KMP_USE_HIER_SCHED 910 if (pr->flags.use_hier) {
912 pr->u.p.ub = pr->u.p.lb = pr->u.p.st = pr->u.p.tc = 0;
914 #endif // KMP_USER_HIER_SCHED 922 buff = __kmp_str_format(
923 "__kmp_dispatch_init: T#%%d returning: schedule:%%d ordered:%%%s " 925 " st:%%%s tc:%%%s count:%%%s\n\tordered_lower:%%%s ordered_upper:%%%s" 926 " parm1:%%%s parm2:%%%s parm3:%%%s parm4:%%%s\n",
927 traits_t<UT>::spec, traits_t<T>::spec, traits_t<T>::spec,
928 traits_t<ST>::spec, traits_t<UT>::spec, traits_t<UT>::spec,
929 traits_t<UT>::spec, traits_t<UT>::spec, traits_t<T>::spec,
930 traits_t<T>::spec, traits_t<T>::spec, traits_t<T>::spec);
931 KD_TRACE(10, (buff, gtid, pr->schedule, pr->flags.ordered, pr->u.p.lb,
932 pr->u.p.ub, pr->u.p.st, pr->u.p.tc, pr->u.p.count,
933 pr->u.p.ordered_lower, pr->u.p.ordered_upper, pr->u.p.parm1,
934 pr->u.p.parm2, pr->u.p.parm3, pr->u.p.parm4));
935 __kmp_str_free(&buff);
938 #if (KMP_STATIC_STEAL_ENABLED) 944 if (schedule == kmp_sch_static_steal) {
948 volatile T *p = &pr->u.p.static_steal_counter;
951 #endif // ( KMP_STATIC_STEAL_ENABLED ) 953 #if OMPT_SUPPORT && OMPT_OPTIONAL 954 if (ompt_enabled.ompt_callback_work) {
955 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
956 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
957 ompt_callbacks.ompt_callback(ompt_callback_work)(
958 ompt_work_loop, ompt_scope_begin, &(team_info->parallel_data),
959 &(task_info->task_data), pr->u.p.tc, OMPT_LOAD_RETURN_ADDRESS(gtid));
962 KMP_PUSH_PARTITIONED_TIMER(OMP_loop_dynamic);
970 template <
typename UT>
971 static void __kmp_dispatch_finish(
int gtid,
ident_t *loc) {
972 typedef typename traits_t<UT>::signed_t ST;
973 kmp_info_t *th = __kmp_threads[gtid];
975 KD_TRACE(100, (
"__kmp_dispatch_finish: T#%d called\n", gtid));
976 if (!th->th.th_team->t.t_serialized) {
978 dispatch_private_info_template<UT> *pr =
979 reinterpret_cast<dispatch_private_info_template<UT> *
>(
980 th->th.th_dispatch->th_dispatch_pr_current);
981 dispatch_shared_info_template<UT>
volatile *sh =
982 reinterpret_cast<dispatch_shared_info_template<UT>
volatile *
>(
983 th->th.th_dispatch->th_dispatch_sh_current);
984 KMP_DEBUG_ASSERT(pr);
985 KMP_DEBUG_ASSERT(sh);
986 KMP_DEBUG_ASSERT(th->th.th_dispatch ==
987 &th->th.th_team->t.t_dispatch[th->th.th_info.ds.ds_tid]);
989 if (pr->ordered_bumped) {
992 (
"__kmp_dispatch_finish: T#%d resetting ordered_bumped to zero\n",
994 pr->ordered_bumped = 0;
996 UT lower = pr->u.p.ordered_lower;
1002 buff = __kmp_str_format(
"__kmp_dispatch_finish: T#%%d before wait: " 1003 "ordered_iteration:%%%s lower:%%%s\n",
1004 traits_t<UT>::spec, traits_t<UT>::spec);
1005 KD_TRACE(1000, (buff, gtid, sh->u.s.ordered_iteration, lower));
1006 __kmp_str_free(&buff);
1010 __kmp_wait_yield<UT>(&sh->u.s.ordered_iteration, lower,
1011 __kmp_ge<UT> USE_ITT_BUILD_ARG(NULL));
1017 buff = __kmp_str_format(
"__kmp_dispatch_finish: T#%%d after wait: " 1018 "ordered_iteration:%%%s lower:%%%s\n",
1019 traits_t<UT>::spec, traits_t<UT>::spec);
1020 KD_TRACE(1000, (buff, gtid, sh->u.s.ordered_iteration, lower));
1021 __kmp_str_free(&buff);
1025 test_then_inc<ST>((
volatile ST *)&sh->u.s.ordered_iteration);
1028 KD_TRACE(100, (
"__kmp_dispatch_finish: T#%d returned\n", gtid));
1031 #ifdef KMP_GOMP_COMPAT 1033 template <
typename UT>
1034 static void __kmp_dispatch_finish_chunk(
int gtid,
ident_t *loc) {
1035 typedef typename traits_t<UT>::signed_t ST;
1036 kmp_info_t *th = __kmp_threads[gtid];
1038 KD_TRACE(100, (
"__kmp_dispatch_finish_chunk: T#%d called\n", gtid));
1039 if (!th->th.th_team->t.t_serialized) {
1041 dispatch_private_info_template<UT> *pr =
1042 reinterpret_cast<dispatch_private_info_template<UT> *
>(
1043 th->th.th_dispatch->th_dispatch_pr_current);
1044 dispatch_shared_info_template<UT>
volatile *sh =
1045 reinterpret_cast<dispatch_shared_info_template<UT>
volatile *
>(
1046 th->th.th_dispatch->th_dispatch_sh_current);
1047 KMP_DEBUG_ASSERT(pr);
1048 KMP_DEBUG_ASSERT(sh);
1049 KMP_DEBUG_ASSERT(th->th.th_dispatch ==
1050 &th->th.th_team->t.t_dispatch[th->th.th_info.ds.ds_tid]);
1053 UT lower = pr->u.p.ordered_lower;
1054 UT upper = pr->u.p.ordered_upper;
1055 UT inc = upper - lower + 1;
1057 if (pr->ordered_bumped == inc) {
1060 (
"__kmp_dispatch_finish: T#%d resetting ordered_bumped to zero\n",
1062 pr->ordered_bumped = 0;
1064 inc -= pr->ordered_bumped;
1070 buff = __kmp_str_format(
1071 "__kmp_dispatch_finish_chunk: T#%%d before wait: " 1072 "ordered_iteration:%%%s lower:%%%s upper:%%%s\n",
1073 traits_t<UT>::spec, traits_t<UT>::spec, traits_t<UT>::spec);
1074 KD_TRACE(1000, (buff, gtid, sh->u.s.ordered_iteration, lower, upper));
1075 __kmp_str_free(&buff);
1079 __kmp_wait_yield<UT>(&sh->u.s.ordered_iteration, lower,
1080 __kmp_ge<UT> USE_ITT_BUILD_ARG(NULL));
1083 KD_TRACE(1000, (
"__kmp_dispatch_finish_chunk: T#%d resetting " 1084 "ordered_bumped to zero\n",
1086 pr->ordered_bumped = 0;
1092 buff = __kmp_str_format(
1093 "__kmp_dispatch_finish_chunk: T#%%d after wait: " 1094 "ordered_iteration:%%%s inc:%%%s lower:%%%s upper:%%%s\n",
1095 traits_t<UT>::spec, traits_t<UT>::spec, traits_t<UT>::spec,
1096 traits_t<UT>::spec);
1098 (buff, gtid, sh->u.s.ordered_iteration, inc, lower, upper));
1099 __kmp_str_free(&buff);
1103 test_then_add<ST>((
volatile ST *)&sh->u.s.ordered_iteration, inc);
1107 KD_TRACE(100, (
"__kmp_dispatch_finish_chunk: T#%d returned\n", gtid));
1112 template <
typename T>
1113 int __kmp_dispatch_next_algorithm(
int gtid,
1114 dispatch_private_info_template<T> *pr,
1115 dispatch_shared_info_template<T>
volatile *sh,
1116 kmp_int32 *p_last, T *p_lb, T *p_ub,
1117 typename traits_t<T>::signed_t *p_st, T nproc,
1119 typedef typename traits_t<T>::unsigned_t UT;
1120 typedef typename traits_t<T>::signed_t ST;
1121 typedef typename traits_t<T>::floating_t DBL;
1126 UT limit, trip, init;
1127 kmp_info_t *th = __kmp_threads[gtid];
1128 kmp_team_t *team = th->th.th_team;
1130 KMP_DEBUG_ASSERT(th->th.th_dispatch ==
1131 &th->th.th_team->t.t_dispatch[th->th.th_info.ds.ds_tid]);
1132 KMP_DEBUG_ASSERT(pr);
1133 KMP_DEBUG_ASSERT(sh);
1134 KMP_DEBUG_ASSERT(tid >= 0 && tid < nproc);
1140 __kmp_str_format(
"__kmp_dispatch_next_algorithm: T#%%d called pr:%%p " 1141 "sh:%%p nproc:%%%s tid:%%%s\n",
1142 traits_t<T>::spec, traits_t<T>::spec);
1143 KD_TRACE(10, (buff, gtid, pr, sh, nproc, tid));
1144 __kmp_str_free(&buff);
1149 if (pr->u.p.tc == 0) {
1151 (
"__kmp_dispatch_next_algorithm: T#%d early exit trip count is " 1157 switch (pr->schedule) {
1158 #if (KMP_STATIC_STEAL_ENABLED) 1159 case kmp_sch_static_steal: {
1160 T chunk = pr->u.p.parm1;
1163 (
"__kmp_dispatch_next_algorithm: T#%d kmp_sch_static_steal case\n",
1166 trip = pr->u.p.tc - 1;
1168 if (traits_t<T>::type_size > 4) {
1171 kmp_lock_t *lck = th->th.th_dispatch->th_steal_lock;
1172 KMP_DEBUG_ASSERT(lck != NULL);
1173 if (pr->u.p.count < (UT)pr->u.p.ub) {
1174 __kmp_acquire_lock(lck, gtid);
1176 init = (pr->u.p.count)++;
1177 status = (init < (UT)pr->u.p.ub);
1178 __kmp_release_lock(lck, gtid);
1183 kmp_info_t **other_threads = team->t.t_threads;
1184 int while_limit = nproc;
1185 int while_index = 0;
1188 while ((!status) && (while_limit != ++while_index)) {
1190 T victimIdx = pr->u.p.parm4;
1191 T oldVictimIdx = victimIdx ? victimIdx - 1 : nproc - 1;
1192 dispatch_private_info_template<T> *victim =
1193 reinterpret_cast<dispatch_private_info_template<T> *
>(
1194 other_threads[victimIdx]
1195 ->th.th_dispatch->th_dispatch_pr_current);
1196 while ((victim == NULL || victim == pr ||
1197 (*(
volatile T *)&victim->u.p.static_steal_counter !=
1198 *(
volatile T *)&pr->u.p.static_steal_counter)) &&
1199 oldVictimIdx != victimIdx) {
1200 victimIdx = (victimIdx + 1) % nproc;
1201 victim =
reinterpret_cast<dispatch_private_info_template<T> *
>(
1202 other_threads[victimIdx]
1203 ->th.th_dispatch->th_dispatch_pr_current);
1205 if (!victim || (*(
volatile T *)&victim->u.p.static_steal_counter !=
1206 *(
volatile T *)&pr->u.p.static_steal_counter)) {
1211 if (victim->u.p.count + 2 > (UT)victim->u.p.ub) {
1212 pr->u.p.parm4 = (victimIdx + 1) % nproc;
1216 lck = other_threads[victimIdx]->th.th_dispatch->th_steal_lock;
1217 KMP_ASSERT(lck != NULL);
1218 __kmp_acquire_lock(lck, gtid);
1219 limit = victim->u.p.ub;
1220 if (victim->u.p.count >= limit ||
1221 (remaining = limit - victim->u.p.count) < 2) {
1222 __kmp_release_lock(lck, gtid);
1223 pr->u.p.parm4 = (victimIdx + 1) % nproc;
1228 if (remaining > 3) {
1230 KMP_COUNT_DEVELOPER_VALUE(FOR_static_steal_stolen, remaining >> 2);
1231 init = (victim->u.p.ub -= (remaining >> 2));
1234 KMP_COUNT_DEVELOPER_VALUE(FOR_static_steal_stolen, 1);
1235 init = (victim->u.p.ub -= 1);
1237 __kmp_release_lock(lck, gtid);
1239 KMP_DEBUG_ASSERT(init + 1 <= limit);
1240 pr->u.p.parm4 = victimIdx;
1244 __kmp_acquire_lock(th->th.th_dispatch->th_steal_lock, gtid);
1245 pr->u.p.count = init + 1;
1247 __kmp_release_lock(th->th.th_dispatch->th_steal_lock, gtid);
1262 union_i4 vold, vnew;
1263 vold.b = *(
volatile kmp_int64 *)(&pr->u.p.count);
1266 while (!KMP_COMPARE_AND_STORE_ACQ64(
1267 (
volatile kmp_int64 *)&pr->u.p.count,
1268 *VOLATILE_CAST(kmp_int64 *) & vold.b,
1269 *VOLATILE_CAST(kmp_int64 *) & vnew.b)) {
1271 vold.b = *(
volatile kmp_int64 *)(&pr->u.p.count);
1276 init = vnew.p.count;
1277 status = (init < (UT)vnew.p.ub);
1281 kmp_info_t **other_threads = team->t.t_threads;
1282 int while_limit = nproc;
1283 int while_index = 0;
1287 while ((!status) && (while_limit != ++while_index)) {
1288 union_i4 vold, vnew;
1289 kmp_int32 remaining;
1290 T victimIdx = pr->u.p.parm4;
1291 T oldVictimIdx = victimIdx ? victimIdx - 1 : nproc - 1;
1292 dispatch_private_info_template<T> *victim =
1293 reinterpret_cast<dispatch_private_info_template<T> *
>(
1294 other_threads[victimIdx]
1295 ->th.th_dispatch->th_dispatch_pr_current);
1296 while ((victim == NULL || victim == pr ||
1297 (*(
volatile T *)&victim->u.p.static_steal_counter !=
1298 *(
volatile T *)&pr->u.p.static_steal_counter)) &&
1299 oldVictimIdx != victimIdx) {
1300 victimIdx = (victimIdx + 1) % nproc;
1301 victim =
reinterpret_cast<dispatch_private_info_template<T> *
>(
1302 other_threads[victimIdx]
1303 ->th.th_dispatch->th_dispatch_pr_current);
1305 if (!victim || (*(
volatile T *)&victim->u.p.static_steal_counter !=
1306 *(
volatile T *)&pr->u.p.static_steal_counter)) {
1311 pr->u.p.parm4 = victimIdx;
1313 vold.b = *(
volatile kmp_int64 *)(&victim->u.p.count);
1316 KMP_DEBUG_ASSERT((vnew.p.ub - 1) * (UT)chunk <= trip);
1317 if (vnew.p.count >= (UT)vnew.p.ub ||
1318 (remaining = vnew.p.ub - vnew.p.count) < 2) {
1319 pr->u.p.parm4 = (victimIdx + 1) % nproc;
1322 if (remaining > 3) {
1323 vnew.p.ub -= (remaining >> 2);
1327 KMP_DEBUG_ASSERT((vnew.p.ub - 1) * (UT)chunk <= trip);
1329 if (KMP_COMPARE_AND_STORE_ACQ64(
1330 (
volatile kmp_int64 *)&victim->u.p.count,
1331 *VOLATILE_CAST(kmp_int64 *) & vold.b,
1332 *VOLATILE_CAST(kmp_int64 *) & vnew.b)) {
1334 KMP_COUNT_DEVELOPER_VALUE(FOR_static_steal_stolen,
1335 vold.p.ub - vnew.p.ub);
1340 vold.p.count = init + 1;
1342 KMP_XCHG_FIXED64((
volatile kmp_int64 *)(&pr->u.p.count), vold.b);
1344 *(
volatile kmp_int64 *)(&pr->u.p.count) = vold.b;
1359 start = pr->u.p.parm2;
1361 limit = chunk + init - 1;
1363 KMP_COUNT_DEVELOPER_VALUE(FOR_static_steal_chunks, 1);
1365 KMP_DEBUG_ASSERT(init <= trip);
1366 if ((last = (limit >= trip)) != 0)
1372 *p_lb = start + init;
1373 *p_ub = start + limit;
1375 *p_lb = start + init * incr;
1376 *p_ub = start + limit * incr;
1379 if (pr->flags.ordered) {
1380 pr->u.p.ordered_lower = init;
1381 pr->u.p.ordered_upper = limit;
1386 #endif // ( KMP_STATIC_STEAL_ENABLED ) 1387 case kmp_sch_static_balanced: {
1390 (
"__kmp_dispatch_next_algorithm: T#%d kmp_sch_static_balanced case\n",
1393 if ((status = !pr->u.p.count) != 0) {
1397 last = pr->u.p.parm1;
1401 pr->u.p.lb = pr->u.p.ub + pr->u.p.st;
1405 case kmp_sch_static_greedy:
1407 case kmp_sch_static_chunked: {
1410 KD_TRACE(100, (
"__kmp_dispatch_next_algorithm: T#%d " 1411 "kmp_sch_static_[affinity|chunked] case\n",
1413 parm1 = pr->u.p.parm1;
1415 trip = pr->u.p.tc - 1;
1416 init = parm1 * (pr->u.p.count + tid);
1418 if ((status = (init <= trip)) != 0) {
1421 limit = parm1 + init - 1;
1423 if ((last = (limit >= trip)) != 0)
1429 pr->u.p.count += nproc;
1432 *p_lb = start + init;
1433 *p_ub = start + limit;
1435 *p_lb = start + init * incr;
1436 *p_ub = start + limit * incr;
1439 if (pr->flags.ordered) {
1440 pr->u.p.ordered_lower = init;
1441 pr->u.p.ordered_upper = limit;
1447 case kmp_sch_dynamic_chunked: {
1448 T chunk = pr->u.p.parm1;
1452 (
"__kmp_dispatch_next_algorithm: T#%d kmp_sch_dynamic_chunked case\n",
1455 init = chunk * test_then_inc_acq<ST>((
volatile ST *)&sh->u.s.iteration);
1456 trip = pr->u.p.tc - 1;
1458 if ((status = (init <= trip)) == 0) {
1465 limit = chunk + init - 1;
1468 if ((last = (limit >= trip)) != 0)
1475 *p_lb = start + init;
1476 *p_ub = start + limit;
1478 *p_lb = start + init * incr;
1479 *p_ub = start + limit * incr;
1482 if (pr->flags.ordered) {
1483 pr->u.p.ordered_lower = init;
1484 pr->u.p.ordered_upper = limit;
1490 case kmp_sch_guided_iterative_chunked: {
1491 T chunkspec = pr->u.p.parm1;
1492 KD_TRACE(100, (
"__kmp_dispatch_next_algorithm: T#%d kmp_sch_guided_chunked " 1499 init = sh->u.s.iteration;
1500 remaining = trip - init;
1501 if (remaining <= 0) {
1510 init = test_then_add<ST>(RCAST(
volatile ST *, &sh->u.s.iteration),
1512 remaining = trip - init;
1513 if (remaining <= 0) {
1518 if ((T)remaining > chunkspec) {
1519 limit = init + chunkspec - 1;
1522 limit = init + remaining - 1;
1528 (UT)(remaining * *(
double *)&pr->u.p.parm3);
1529 if (compare_and_swap<ST>(RCAST(
volatile ST *, &sh->u.s.iteration),
1530 (ST)init, (ST)limit)) {
1542 *p_lb = start + init * incr;
1543 *p_ub = start + limit * incr;
1544 if (pr->flags.ordered) {
1545 pr->u.p.ordered_lower = init;
1546 pr->u.p.ordered_upper = limit;
1558 case kmp_sch_guided_simd: {
1561 T chunk = pr->u.p.parm1;
1563 (
"__kmp_dispatch_next_algorithm: T#%d kmp_sch_guided_simd case\n",
1569 init = sh->u.s.iteration;
1570 remaining = trip - init;
1571 if (remaining <= 0) {
1575 KMP_DEBUG_ASSERT(init % chunk == 0);
1577 if ((T)remaining < pr->u.p.parm2) {
1580 init = test_then_add<ST>(RCAST(
volatile ST *, &sh->u.s.iteration),
1582 remaining = trip - init;
1583 if (remaining <= 0) {
1588 if ((T)remaining > chunk) {
1589 limit = init + chunk - 1;
1592 limit = init + remaining - 1;
1598 UT span = remaining * (*(
double *)&pr->u.p.parm3);
1599 UT rem = span % chunk;
1601 span += chunk - rem;
1602 limit = init + span;
1603 if (compare_and_swap<ST>(RCAST(
volatile ST *, &sh->u.s.iteration),
1604 (ST)init, (ST)limit)) {
1616 *p_lb = start + init * incr;
1617 *p_ub = start + limit * incr;
1618 if (pr->flags.ordered) {
1619 pr->u.p.ordered_lower = init;
1620 pr->u.p.ordered_upper = limit;
1630 #endif // OMP_45_ENABLED 1632 case kmp_sch_guided_analytical_chunked: {
1633 T chunkspec = pr->u.p.parm1;
1635 #if KMP_OS_WINDOWS && KMP_ARCH_X86 1638 unsigned int oldFpcw;
1639 unsigned int fpcwSet = 0;
1641 KD_TRACE(100, (
"__kmp_dispatch_next_algorithm: T#%d " 1642 "kmp_sch_guided_analytical_chunked case\n",
1647 KMP_DEBUG_ASSERT(nproc > 1);
1648 KMP_DEBUG_ASSERT((2UL * chunkspec + 1) * (UT)nproc < trip);
1652 chunkIdx = test_then_inc_acq<ST>((
volatile ST *)&sh->u.s.iteration);
1653 if (chunkIdx >= (UT)pr->u.p.parm2) {
1656 init = chunkIdx * chunkspec + pr->u.p.count;
1659 if ((status = (init > 0 && init <= trip)) != 0) {
1660 limit = init + chunkspec - 1;
1662 if ((last = (limit >= trip)) != 0)
1672 #if KMP_OS_WINDOWS && KMP_ARCH_X86 1677 oldFpcw = _control87(0, 0);
1678 _control87(_PC_64, _MCW_PC);
1683 init = __kmp_dispatch_guided_remaining<T>(
1684 trip, *(DBL *)&pr->u.p.parm3, chunkIdx);
1685 KMP_DEBUG_ASSERT(init);
1689 limit = trip - __kmp_dispatch_guided_remaining<T>(
1690 trip, *(DBL *)&pr->u.p.parm3, chunkIdx + 1);
1691 KMP_ASSERT(init <= limit);
1693 KMP_DEBUG_ASSERT(limit <= trip);
1700 #if KMP_OS_WINDOWS && KMP_ARCH_X86 1704 if (fpcwSet && (oldFpcw & fpcwSet))
1705 _control87(oldFpcw, _MCW_PC);
1712 *p_lb = start + init * incr;
1713 *p_ub = start + limit * incr;
1714 if (pr->flags.ordered) {
1715 pr->u.p.ordered_lower = init;
1716 pr->u.p.ordered_upper = limit;
1727 case kmp_sch_trapezoidal: {
1729 T parm2 = pr->u.p.parm2;
1730 T parm3 = pr->u.p.parm3;
1731 T parm4 = pr->u.p.parm4;
1733 (
"__kmp_dispatch_next_algorithm: T#%d kmp_sch_trapezoidal case\n",
1736 index = test_then_inc<ST>((
volatile ST *)&sh->u.s.iteration);
1738 init = (index * ((2 * parm2) - (index - 1) * parm4)) / 2;
1739 trip = pr->u.p.tc - 1;
1741 if ((status = ((T)index < parm3 && init <= trip)) == 0) {
1748 limit = ((index + 1) * (2 * parm2 - index * parm4)) / 2 - 1;
1751 if ((last = (limit >= trip)) != 0)
1758 *p_lb = start + init;
1759 *p_ub = start + limit;
1761 *p_lb = start + init * incr;
1762 *p_ub = start + limit * incr;
1765 if (pr->flags.ordered) {
1766 pr->u.p.ordered_lower = init;
1767 pr->u.p.ordered_upper = limit;
1774 __kmp_fatal(KMP_MSG(UnknownSchedTypeDetected),
1775 KMP_HNT(GetNewerLibrary),
1783 if (pr->flags.ordered) {
1786 buff = __kmp_str_format(
"__kmp_dispatch_next_algorithm: T#%%d " 1787 "ordered_lower:%%%s ordered_upper:%%%s\n",
1788 traits_t<UT>::spec, traits_t<UT>::spec);
1789 KD_TRACE(1000, (buff, gtid, pr->u.p.ordered_lower, pr->u.p.ordered_upper));
1790 __kmp_str_free(&buff);
1795 buff = __kmp_str_format(
1796 "__kmp_dispatch_next_algorithm: T#%%d exit status:%%d p_last:%%d " 1797 "p_lb:%%%s p_ub:%%%s p_st:%%%s\n",
1798 traits_t<T>::spec, traits_t<T>::spec, traits_t<ST>::spec);
1799 KD_TRACE(10, (buff, gtid, status, *p_last, *p_lb, *p_ub, *p_st));
1800 __kmp_str_free(&buff);
1809 #if OMPT_SUPPORT && OMPT_OPTIONAL 1810 #define OMPT_LOOP_END \ 1811 if (status == 0) { \ 1812 if (ompt_enabled.ompt_callback_work) { \ 1813 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL); \ 1814 ompt_task_info_t *task_info = __ompt_get_task_info_object(0); \ 1815 ompt_callbacks.ompt_callback(ompt_callback_work)( \ 1816 ompt_work_loop, ompt_scope_end, &(team_info->parallel_data), \ 1817 &(task_info->task_data), 0, codeptr); \ 1822 #define OMPT_LOOP_END // no-op 1825 #if KMP_STATS_ENABLED 1826 #define KMP_STATS_LOOP_END \ 1828 kmp_int64 u, l, t, i; \ 1829 l = (kmp_int64)(*p_lb); \ 1830 u = (kmp_int64)(*p_ub); \ 1831 i = (kmp_int64)(pr->u.p.st); \ 1832 if (status == 0) { \ 1834 KMP_POP_PARTITIONED_TIMER(); \ 1835 } else if (i == 1) { \ 1840 } else if (i < 0) { \ 1842 t = (l - u) / (-i) + 1; \ 1847 t = (u - l) / i + 1; \ 1851 KMP_COUNT_VALUE(OMP_loop_dynamic_iterations, t); \ 1854 #define KMP_STATS_LOOP_END 1857 template <
typename T>
1858 static int __kmp_dispatch_next(
ident_t *loc,
int gtid, kmp_int32 *p_last,
1860 typename traits_t<T>::signed_t *p_st
1861 #
if OMPT_SUPPORT && OMPT_OPTIONAL
1867 typedef typename traits_t<T>::unsigned_t UT;
1868 typedef typename traits_t<T>::signed_t ST;
1873 KMP_TIME_PARTITIONED_BLOCK(OMP_loop_dynamic_scheduling);
1876 dispatch_private_info_template<T> *pr;
1877 kmp_info_t *th = __kmp_threads[gtid];
1878 kmp_team_t *team = th->th.th_team;
1880 KMP_DEBUG_ASSERT(p_lb && p_ub && p_st);
1883 (
"__kmp_dispatch_next: T#%d called p_lb:%p p_ub:%p p_st:%p p_last: %p\n",
1884 gtid, p_lb, p_ub, p_st, p_last));
1886 if (team->t.t_serialized) {
1888 pr =
reinterpret_cast<dispatch_private_info_template<T> *
>(
1889 th->th.th_dispatch->th_disp_buffer);
1890 KMP_DEBUG_ASSERT(pr);
1892 if ((status = (pr->u.p.tc != 0)) == 0) {
1899 if (__kmp_env_consistency_check) {
1900 if (pr->pushed_ws != ct_none) {
1901 pr->pushed_ws = __kmp_pop_workshare(gtid, pr->pushed_ws, loc);
1904 }
else if (pr->flags.nomerge) {
1907 UT limit, trip, init;
1909 T chunk = pr->u.p.parm1;
1911 KD_TRACE(100, (
"__kmp_dispatch_next: T#%d kmp_sch_dynamic_chunked case\n",
1914 init = chunk * pr->u.p.count++;
1915 trip = pr->u.p.tc - 1;
1917 if ((status = (init <= trip)) == 0) {
1924 if (__kmp_env_consistency_check) {
1925 if (pr->pushed_ws != ct_none) {
1926 pr->pushed_ws = __kmp_pop_workshare(gtid, pr->pushed_ws, loc);
1931 limit = chunk + init - 1;
1934 if ((last = (limit >= trip)) != 0) {
1937 pr->u.p.last_upper = pr->u.p.ub;
1945 *p_lb = start + init;
1946 *p_ub = start + limit;
1948 *p_lb = start + init * incr;
1949 *p_ub = start + limit * incr;
1952 if (pr->flags.ordered) {
1953 pr->u.p.ordered_lower = init;
1954 pr->u.p.ordered_upper = limit;
1959 buff = __kmp_str_format(
"__kmp_dispatch_next: T#%%d " 1960 "ordered_lower:%%%s ordered_upper:%%%s\n",
1961 traits_t<UT>::spec, traits_t<UT>::spec);
1962 KD_TRACE(1000, (buff, gtid, pr->u.p.ordered_lower,
1963 pr->u.p.ordered_upper));
1964 __kmp_str_free(&buff);
1974 pr->u.p.last_upper = *p_ub;
1985 buff = __kmp_str_format(
1986 "__kmp_dispatch_next: T#%%d serialized case: p_lb:%%%s " 1987 "p_ub:%%%s p_st:%%%s p_last:%%p %%d returning:%%d\n",
1988 traits_t<T>::spec, traits_t<T>::spec, traits_t<ST>::spec);
1989 KD_TRACE(10, (buff, gtid, *p_lb, *p_ub, *p_st, p_last, *p_last, status));
1990 __kmp_str_free(&buff);
1993 #if INCLUDE_SSC_MARKS 1994 SSC_MARK_DISPATCH_NEXT();
2001 dispatch_shared_info_template<T>
volatile *sh;
2003 KMP_DEBUG_ASSERT(th->th.th_dispatch ==
2004 &th->th.th_team->t.t_dispatch[th->th.th_info.ds.ds_tid]);
2006 pr =
reinterpret_cast<dispatch_private_info_template<T> *
>(
2007 th->th.th_dispatch->th_dispatch_pr_current);
2008 KMP_DEBUG_ASSERT(pr);
2009 sh =
reinterpret_cast<dispatch_shared_info_template<T>
volatile *
>(
2010 th->th.th_dispatch->th_dispatch_sh_current);
2011 KMP_DEBUG_ASSERT(sh);
2013 #if KMP_USE_HIER_SCHED 2014 if (pr->flags.use_hier)
2015 status = sh->hier->next(loc, gtid, pr, &last, p_lb, p_ub, p_st);
2017 #endif // KMP_USE_HIER_SCHED 2018 status = __kmp_dispatch_next_algorithm<T>(gtid, pr, sh, &last, p_lb, p_ub,
2019 p_st, th->th.th_team_nproc,
2020 th->th.th_info.ds.ds_tid);
2025 num_done = test_then_inc<ST>((
volatile ST *)&sh->u.s.num_done);
2030 buff = __kmp_str_format(
2031 "__kmp_dispatch_next: T#%%d increment num_done:%%%s\n",
2032 traits_t<UT>::spec);
2033 KD_TRACE(10, (buff, gtid, sh->u.s.num_done));
2034 __kmp_str_free(&buff);
2038 #if KMP_USE_HIER_SCHED 2039 pr->flags.use_hier = FALSE;
2041 if ((ST)num_done == th->th.th_team_nproc - 1) {
2042 #if (KMP_STATIC_STEAL_ENABLED) 2043 if (pr->schedule == kmp_sch_static_steal &&
2044 traits_t<T>::type_size > 4) {
2046 kmp_info_t **other_threads = team->t.t_threads;
2048 for (i = 0; i < th->th.th_team_nproc; ++i) {
2049 kmp_lock_t *lck = other_threads[i]->th.th_dispatch->th_steal_lock;
2050 KMP_ASSERT(lck != NULL);
2051 __kmp_destroy_lock(lck);
2053 other_threads[i]->th.th_dispatch->th_steal_lock = NULL;
2061 sh->u.s.num_done = 0;
2062 sh->u.s.iteration = 0;
2065 if (pr->flags.ordered) {
2066 sh->u.s.ordered_iteration = 0;
2071 sh->buffer_index += __kmp_dispatch_num_buffers;
2072 KD_TRACE(100, (
"__kmp_dispatch_next: T#%d change buffer_index:%d\n",
2073 gtid, sh->buffer_index));
2078 if (__kmp_env_consistency_check) {
2079 if (pr->pushed_ws != ct_none) {
2080 pr->pushed_ws = __kmp_pop_workshare(gtid, pr->pushed_ws, loc);
2084 th->th.th_dispatch->th_deo_fcn = NULL;
2085 th->th.th_dispatch->th_dxo_fcn = NULL;
2086 th->th.th_dispatch->th_dispatch_sh_current = NULL;
2087 th->th.th_dispatch->th_dispatch_pr_current = NULL;
2091 pr->u.p.last_upper = pr->u.p.ub;
2094 if (p_last != NULL && status != 0)
2102 buff = __kmp_str_format(
2103 "__kmp_dispatch_next: T#%%d normal case: " 2104 "p_lb:%%%s p_ub:%%%s p_st:%%%s p_last:%%p (%%d) returning:%%d\n",
2105 traits_t<T>::spec, traits_t<T>::spec, traits_t<ST>::spec);
2106 KD_TRACE(10, (buff, gtid, *p_lb, *p_ub, p_st ? *p_st : 0, p_last,
2107 (p_last ? *p_last : 0), status));
2108 __kmp_str_free(&buff);
2111 #if INCLUDE_SSC_MARKS 2112 SSC_MARK_DISPATCH_NEXT();
2119 template <
typename T>
2120 static void __kmp_dist_get_bounds(
ident_t *loc, kmp_int32 gtid,
2121 kmp_int32 *plastiter, T *plower, T *pupper,
2122 typename traits_t<T>::signed_t incr) {
2123 typedef typename traits_t<T>::unsigned_t UT;
2130 KMP_DEBUG_ASSERT(plastiter && plower && pupper);
2131 KE_TRACE(10, (
"__kmpc_dist_get_bounds called (%d)\n", gtid));
2133 typedef typename traits_t<T>::signed_t ST;
2137 buff = __kmp_str_format(
"__kmpc_dist_get_bounds: T#%%d liter=%%d " 2138 "iter=(%%%s, %%%s, %%%s) signed?<%s>\n",
2139 traits_t<T>::spec, traits_t<T>::spec,
2140 traits_t<ST>::spec, traits_t<T>::spec);
2141 KD_TRACE(100, (buff, gtid, *plastiter, *plower, *pupper, incr));
2142 __kmp_str_free(&buff);
2146 if (__kmp_env_consistency_check) {
2148 __kmp_error_construct(kmp_i18n_msg_CnsLoopIncrZeroProhibited, ct_pdo,
2151 if (incr > 0 ? (*pupper < *plower) : (*plower < *pupper)) {
2161 __kmp_error_construct(kmp_i18n_msg_CnsLoopIncrIllegal, ct_pdo, loc);
2164 th = __kmp_threads[gtid];
2165 team = th->th.th_team;
2167 KMP_DEBUG_ASSERT(th->th.th_teams_microtask);
2168 nteams = th->th.th_teams_size.nteams;
2170 team_id = team->t.t_master_tid;
2171 KMP_DEBUG_ASSERT(nteams == (kmp_uint32)team->t.t_parent->t.t_nproc);
2175 trip_count = *pupper - *plower + 1;
2176 }
else if (incr == -1) {
2177 trip_count = *plower - *pupper + 1;
2178 }
else if (incr > 0) {
2180 trip_count = (UT)(*pupper - *plower) / incr + 1;
2182 trip_count = (UT)(*plower - *pupper) / (-incr) + 1;
2185 if (trip_count <= nteams) {
2187 __kmp_static == kmp_sch_static_greedy ||
2189 kmp_sch_static_balanced);
2191 if (team_id < trip_count) {
2192 *pupper = *plower = *plower + team_id * incr;
2194 *plower = *pupper + incr;
2196 if (plastiter != NULL)
2197 *plastiter = (team_id == trip_count - 1);
2199 if (__kmp_static == kmp_sch_static_balanced) {
2200 UT chunk = trip_count / nteams;
2201 UT extras = trip_count % nteams;
2203 incr * (team_id * chunk + (team_id < extras ? team_id : extras));
2204 *pupper = *plower + chunk * incr - (team_id < extras ? 0 : incr);
2205 if (plastiter != NULL)
2206 *plastiter = (team_id == nteams - 1);
2209 (trip_count / nteams + ((trip_count % nteams) ? 1 : 0)) * incr;
2211 KMP_DEBUG_ASSERT(__kmp_static == kmp_sch_static_greedy);
2213 *plower += team_id * chunk_inc_count;
2214 *pupper = *plower + chunk_inc_count - incr;
2217 if (*pupper < *plower)
2218 *pupper = traits_t<T>::max_value;
2219 if (plastiter != NULL)
2220 *plastiter = *plower <= upper && *pupper > upper - incr;
2221 if (*pupper > upper)
2224 if (*pupper > *plower)
2225 *pupper = traits_t<T>::min_value;
2226 if (plastiter != NULL)
2227 *plastiter = *plower >= upper && *pupper < upper - incr;
2228 if (*pupper < upper)
2260 kmp_int32 ub, kmp_int32 st, kmp_int32 chunk) {
2261 KMP_DEBUG_ASSERT(__kmp_init_serial);
2262 #if OMPT_SUPPORT && OMPT_OPTIONAL 2263 OMPT_STORE_RETURN_ADDRESS(gtid);
2265 __kmp_dispatch_init<kmp_int32>(loc, gtid, schedule, lb, ub, st, chunk,
true);
2272 kmp_uint32 ub, kmp_int32 st, kmp_int32 chunk) {
2273 KMP_DEBUG_ASSERT(__kmp_init_serial);
2274 #if OMPT_SUPPORT && OMPT_OPTIONAL 2275 OMPT_STORE_RETURN_ADDRESS(gtid);
2277 __kmp_dispatch_init<kmp_uint32>(loc, gtid, schedule, lb, ub, st, chunk,
true);
2285 kmp_int64 ub, kmp_int64 st, kmp_int64 chunk) {
2286 KMP_DEBUG_ASSERT(__kmp_init_serial);
2287 #if OMPT_SUPPORT && OMPT_OPTIONAL 2288 OMPT_STORE_RETURN_ADDRESS(gtid);
2290 __kmp_dispatch_init<kmp_int64>(loc, gtid, schedule, lb, ub, st, chunk,
true);
2298 kmp_uint64 ub, kmp_int64 st, kmp_int64 chunk) {
2299 KMP_DEBUG_ASSERT(__kmp_init_serial);
2300 #if OMPT_SUPPORT && OMPT_OPTIONAL 2301 OMPT_STORE_RETURN_ADDRESS(gtid);
2303 __kmp_dispatch_init<kmp_uint64>(loc, gtid, schedule, lb, ub, st, chunk,
true);
2317 kmp_int32 lb, kmp_int32 ub, kmp_int32 st,
2319 KMP_DEBUG_ASSERT(__kmp_init_serial);
2320 #if OMPT_SUPPORT && OMPT_OPTIONAL 2321 OMPT_STORE_RETURN_ADDRESS(gtid);
2323 __kmp_dist_get_bounds<kmp_int32>(loc, gtid, p_last, &lb, &ub, st);
2324 __kmp_dispatch_init<kmp_int32>(loc, gtid, schedule, lb, ub, st, chunk,
true);
2327 void __kmpc_dist_dispatch_init_4u(
ident_t *loc, kmp_int32 gtid,
2329 kmp_uint32 lb, kmp_uint32 ub, kmp_int32 st,
2331 KMP_DEBUG_ASSERT(__kmp_init_serial);
2332 #if OMPT_SUPPORT && OMPT_OPTIONAL 2333 OMPT_STORE_RETURN_ADDRESS(gtid);
2335 __kmp_dist_get_bounds<kmp_uint32>(loc, gtid, p_last, &lb, &ub, st);
2336 __kmp_dispatch_init<kmp_uint32>(loc, gtid, schedule, lb, ub, st, chunk,
true);
2339 void __kmpc_dist_dispatch_init_8(
ident_t *loc, kmp_int32 gtid,
2341 kmp_int64 lb, kmp_int64 ub, kmp_int64 st,
2343 KMP_DEBUG_ASSERT(__kmp_init_serial);
2344 #if OMPT_SUPPORT && OMPT_OPTIONAL 2345 OMPT_STORE_RETURN_ADDRESS(gtid);
2347 __kmp_dist_get_bounds<kmp_int64>(loc, gtid, p_last, &lb, &ub, st);
2348 __kmp_dispatch_init<kmp_int64>(loc, gtid, schedule, lb, ub, st, chunk,
true);
2351 void __kmpc_dist_dispatch_init_8u(
ident_t *loc, kmp_int32 gtid,
2353 kmp_uint64 lb, kmp_uint64 ub, kmp_int64 st,
2355 KMP_DEBUG_ASSERT(__kmp_init_serial);
2356 #if OMPT_SUPPORT && OMPT_OPTIONAL 2357 OMPT_STORE_RETURN_ADDRESS(gtid);
2359 __kmp_dist_get_bounds<kmp_uint64>(loc, gtid, p_last, &lb, &ub, st);
2360 __kmp_dispatch_init<kmp_uint64>(loc, gtid, schedule, lb, ub, st, chunk,
true);
2377 kmp_int32 *p_lb, kmp_int32 *p_ub, kmp_int32 *p_st) {
2378 #if OMPT_SUPPORT && OMPT_OPTIONAL 2379 OMPT_STORE_RETURN_ADDRESS(gtid);
2381 return __kmp_dispatch_next<kmp_int32>(loc, gtid, p_last, p_lb, p_ub, p_st
2382 #if OMPT_SUPPORT && OMPT_OPTIONAL 2384 OMPT_LOAD_RETURN_ADDRESS(gtid)
2393 kmp_uint32 *p_lb, kmp_uint32 *p_ub,
2395 #if OMPT_SUPPORT && OMPT_OPTIONAL 2396 OMPT_STORE_RETURN_ADDRESS(gtid);
2398 return __kmp_dispatch_next<kmp_uint32>(loc, gtid, p_last, p_lb, p_ub, p_st
2399 #if OMPT_SUPPORT && OMPT_OPTIONAL 2401 OMPT_LOAD_RETURN_ADDRESS(gtid)
2410 kmp_int64 *p_lb, kmp_int64 *p_ub, kmp_int64 *p_st) {
2411 #if OMPT_SUPPORT && OMPT_OPTIONAL 2412 OMPT_STORE_RETURN_ADDRESS(gtid);
2414 return __kmp_dispatch_next<kmp_int64>(loc, gtid, p_last, p_lb, p_ub, p_st
2415 #if OMPT_SUPPORT && OMPT_OPTIONAL 2417 OMPT_LOAD_RETURN_ADDRESS(gtid)
2426 kmp_uint64 *p_lb, kmp_uint64 *p_ub,
2428 #if OMPT_SUPPORT && OMPT_OPTIONAL 2429 OMPT_STORE_RETURN_ADDRESS(gtid);
2431 return __kmp_dispatch_next<kmp_uint64>(loc, gtid, p_last, p_lb, p_ub, p_st
2432 #if OMPT_SUPPORT && OMPT_OPTIONAL 2434 OMPT_LOAD_RETURN_ADDRESS(gtid)
2446 __kmp_dispatch_finish<kmp_uint32>(gtid, loc);
2453 __kmp_dispatch_finish<kmp_uint64>(gtid, loc);
2460 __kmp_dispatch_finish<kmp_uint32>(gtid, loc);
2467 __kmp_dispatch_finish<kmp_uint64>(gtid, loc);
2474 kmp_uint32 __kmp_eq_4(kmp_uint32 value, kmp_uint32 checker) {
2475 return value == checker;
2478 kmp_uint32 __kmp_neq_4(kmp_uint32 value, kmp_uint32 checker) {
2479 return value != checker;
2482 kmp_uint32 __kmp_lt_4(kmp_uint32 value, kmp_uint32 checker) {
2483 return value < checker;
2486 kmp_uint32 __kmp_ge_4(kmp_uint32 value, kmp_uint32 checker) {
2487 return value >= checker;
2490 kmp_uint32 __kmp_le_4(kmp_uint32 value, kmp_uint32 checker) {
2491 return value <= checker;
2495 __kmp_wait_yield_4(
volatile kmp_uint32 *spinner, kmp_uint32 checker,
2496 kmp_uint32 (*pred)(kmp_uint32, kmp_uint32),
2500 volatile kmp_uint32 *spin = spinner;
2501 kmp_uint32 check = checker;
2503 kmp_uint32 (*f)(kmp_uint32, kmp_uint32) = pred;
2506 KMP_FSYNC_SPIN_INIT(obj, CCAST(kmp_uint32 *, spin));
2507 KMP_INIT_YIELD(spins);
2509 while (!f(r = TCR_4(*spin), check)) {
2510 KMP_FSYNC_SPIN_PREPARE(obj);
2518 KMP_YIELD(TCR_4(__kmp_nth) > __kmp_avail_proc);
2519 KMP_YIELD_SPIN(spins);
2521 KMP_FSYNC_SPIN_ACQUIRED(obj);
2525 void __kmp_wait_yield_4_ptr(
2526 void *spinner, kmp_uint32 checker, kmp_uint32 (*pred)(
void *, kmp_uint32),
2530 void *spin = spinner;
2531 kmp_uint32 check = checker;
2533 kmp_uint32 (*f)(
void *, kmp_uint32) = pred;
2535 KMP_FSYNC_SPIN_INIT(obj, spin);
2536 KMP_INIT_YIELD(spins);
2538 while (!f(spin, check)) {
2539 KMP_FSYNC_SPIN_PREPARE(obj);
2542 KMP_YIELD(TCR_4(__kmp_nth) > __kmp_avail_proc);
2543 KMP_YIELD_SPIN(spins);
2545 KMP_FSYNC_SPIN_ACQUIRED(obj);
2550 #ifdef KMP_GOMP_COMPAT 2552 void __kmp_aux_dispatch_init_4(
ident_t *loc, kmp_int32 gtid,
2554 kmp_int32 ub, kmp_int32 st, kmp_int32 chunk,
2556 __kmp_dispatch_init<kmp_int32>(loc, gtid, schedule, lb, ub, st, chunk,
2560 void __kmp_aux_dispatch_init_4u(
ident_t *loc, kmp_int32 gtid,
2562 kmp_uint32 ub, kmp_int32 st, kmp_int32 chunk,
2564 __kmp_dispatch_init<kmp_uint32>(loc, gtid, schedule, lb, ub, st, chunk,
2568 void __kmp_aux_dispatch_init_8(
ident_t *loc, kmp_int32 gtid,
2570 kmp_int64 ub, kmp_int64 st, kmp_int64 chunk,
2572 __kmp_dispatch_init<kmp_int64>(loc, gtid, schedule, lb, ub, st, chunk,
2576 void __kmp_aux_dispatch_init_8u(
ident_t *loc, kmp_int32 gtid,
2578 kmp_uint64 ub, kmp_int64 st, kmp_int64 chunk,
2580 __kmp_dispatch_init<kmp_uint64>(loc, gtid, schedule, lb, ub, st, chunk,
2584 void __kmp_aux_dispatch_fini_chunk_4(
ident_t *loc, kmp_int32 gtid) {
2585 __kmp_dispatch_finish_chunk<kmp_uint32>(gtid, loc);
2588 void __kmp_aux_dispatch_fini_chunk_8(
ident_t *loc, kmp_int32 gtid) {
2589 __kmp_dispatch_finish_chunk<kmp_uint64>(gtid, loc);
2592 void __kmp_aux_dispatch_fini_chunk_4u(
ident_t *loc, kmp_int32 gtid) {
2593 __kmp_dispatch_finish_chunk<kmp_uint32>(gtid, loc);
2596 void __kmp_aux_dispatch_fini_chunk_8u(
ident_t *loc, kmp_int32 gtid) {
2597 __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)
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)