14 #ifndef KMP_WAIT_RELEASE_H 15 #define KMP_WAIT_RELEASE_H 19 #include "kmp_stats.h" 21 #include "ompt-specific.h" 56 volatile P *
get() {
return loc; }
57 void *get_void_p() {
return RCAST(
void *, CCAST(P *, loc)); }
58 void set(
volatile P *new_loc) { loc = new_loc; }
60 P load() {
return *loc; }
61 void store(P val) { *loc = val; }
78 std::atomic<P> *
get() {
return loc; }
86 void set(std::atomic<P> *new_loc) { loc = new_loc; }
94 P
load() {
return loc->load(std::memory_order_acquire); }
98 void store(P val) { loc->store(val, std::memory_order_release); }
122 static inline void __ompt_implicit_task_end(kmp_info_t *this_thr,
123 omp_state_t omp_state,
126 int ds_tid = this_thr->th.th_info.ds.ds_tid;
127 if (omp_state == omp_state_wait_barrier_implicit) {
128 this_thr->th.ompt_thread_info.state = omp_state_overhead;
130 void *codeptr = NULL;
131 if (ompt_enabled.ompt_callback_sync_region_wait) {
132 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
133 ompt_sync_region_barrier, ompt_scope_end, NULL, tId, codeptr);
135 if (ompt_enabled.ompt_callback_sync_region) {
136 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
137 ompt_sync_region_barrier, ompt_scope_end, NULL, tId, codeptr);
140 if (!KMP_MASTER_TID(ds_tid)) {
141 if (ompt_enabled.ompt_callback_implicit_task) {
142 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
143 ompt_scope_end, NULL, tId, 0, ds_tid);
146 this_thr->th.ompt_thread_info.state = omp_state_idle;
148 this_thr->th.ompt_thread_info.state = omp_state_overhead;
159 template <
class C,
int final_spin>
161 __kmp_wait_template(kmp_info_t *this_thr,
162 C *flag USE_ITT_BUILD_ARG(
void *itt_sync_obj)) {
163 #if USE_ITT_BUILD && USE_ITT_NOTIFY 164 volatile void *spin = flag->get();
168 int tasks_completed = FALSE;
171 kmp_uint64 poll_count;
172 kmp_uint64 hibernate_goal;
174 kmp_uint32 hibernate;
177 KMP_FSYNC_SPIN_INIT(spin, NULL);
178 if (flag->done_check()) {
179 KMP_FSYNC_SPIN_ACQUIRED(CCAST(
void *, spin));
182 th_gtid = this_thr->th.th_info.ds.ds_gtid;
185 KMP_ATOMIC_ST_REL(&this_thr->th.th_blocking,
true);
188 (
"__kmp_wait_sleep: T#%d waiting for flag(%p)\n", th_gtid, flag));
189 #if KMP_STATS_ENABLED 244 omp_state_t ompt_entry_state;
245 ompt_data_t *pId = NULL;
247 if (ompt_enabled.enabled) {
248 ompt_entry_state = this_thr->th.ompt_thread_info.state;
249 if (!final_spin || ompt_entry_state != omp_state_wait_barrier_implicit ||
250 KMP_MASTER_TID(this_thr->th.th_info.ds.ds_tid)) {
251 ompt_lw_taskteam_t *team =
252 this_thr->th.th_team->t.ompt_serialized_team_info;
254 pId = &(team->ompt_team_info.parallel_data);
255 tId = &(team->ompt_task_info.task_data);
257 pId = OMPT_CUR_TEAM_DATA(this_thr);
258 tId = OMPT_CUR_TASK_DATA(this_thr);
262 tId = &(this_thr->th.ompt_thread_info.task_data);
264 if (final_spin && (__kmp_tasking_mode == tskm_immediate_exec ||
265 this_thr->th.th_task_team == NULL)) {
267 __ompt_implicit_task_end(this_thr, ompt_entry_state, tId, pId);
273 KMP_INIT_YIELD(spins);
275 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
279 #ifdef KMP_ADJUST_BLOCKTIME 280 if (__kmp_zero_bt && !this_thr->th.th_team_bt_set)
285 hibernate = this_thr->th.th_team_bt_intervals;
287 hibernate = this_thr->th.th_team_bt_intervals;
298 hibernate += TCR_4(__kmp_global.g.g_time.dt.t_value);
299 KF_TRACE(20, (
"__kmp_wait_sleep: T#%d now=%d, hibernate=%d, intervals=%d\n",
300 th_gtid, __kmp_global.g.g_time.dt.t_value, hibernate,
301 hibernate - __kmp_global.g.g_time.dt.t_value));
303 hibernate_goal = KMP_NOW() + this_thr->th.th_team_bt_intervals;
305 #endif // KMP_USE_MONITOR 308 oversubscribed = (TCR_4(__kmp_nth) > __kmp_avail_proc);
312 while (flag->notdone_check()) {
314 kmp_task_team_t *task_team = NULL;
315 if (__kmp_tasking_mode != tskm_immediate_exec) {
316 task_team = this_thr->th.th_task_team;
324 if (task_team != NULL) {
325 if (TCR_SYNC_4(task_team->tt.tt_active)) {
326 if (KMP_TASKING_ENABLED(task_team))
328 this_thr, th_gtid, final_spin,
329 &tasks_completed USE_ITT_BUILD_ARG(itt_sync_obj), 0);
331 this_thr->th.th_reap_state = KMP_SAFE_TO_REAP;
333 KMP_DEBUG_ASSERT(!KMP_MASTER_TID(this_thr->th.th_info.ds.ds_tid));
336 if (final_spin && ompt_enabled.enabled)
337 __ompt_implicit_task_end(this_thr, ompt_entry_state, tId, pId);
339 this_thr->th.th_task_team = NULL;
340 this_thr->th.th_reap_state = KMP_SAFE_TO_REAP;
343 this_thr->th.th_reap_state = KMP_SAFE_TO_REAP;
347 KMP_FSYNC_SPIN_PREPARE(CCAST(
void *, spin));
348 if (TCR_4(__kmp_global.g.g_done)) {
349 if (__kmp_global.g.g_abort)
350 __kmp_abort_thread();
359 if (oversubscribed) {
362 KMP_YIELD_SPIN(spins);
366 in_pool = !!TCR_4(this_thr->th.th_in_pool);
367 if (in_pool != !!this_thr->th.th_active_in_pool) {
369 KMP_ATOMIC_INC(&__kmp_thread_pool_active_nth);
370 this_thr->th.th_active_in_pool = TRUE;
379 KMP_ATOMIC_DEC(&__kmp_thread_pool_active_nth);
380 KMP_DEBUG_ASSERT(TCR_4(__kmp_thread_pool_active_nth) >= 0);
381 this_thr->th.th_active_in_pool = FALSE;
385 #if KMP_STATS_ENABLED 388 if (this_thr->th.th_stats->isIdle() &&
389 KMP_GET_THREAD_STATE() == FORK_JOIN_BARRIER) {
390 KMP_SET_THREAD_STATE(IDLE);
391 KMP_PUSH_PARTITIONED_TIMER(OMP_idle);
396 if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME)
400 if ((task_team != NULL) && TCR_4(task_team->tt.tt_found_tasks))
405 if (TCR_4(__kmp_global.g.g_time.dt.t_value) < hibernate)
408 if (KMP_BLOCKING(hibernate_goal, poll_count++))
412 KF_TRACE(50, (
"__kmp_wait_sleep: T#%d suspend time reached\n", th_gtid));
415 KMP_ATOMIC_ST_REL(&this_thr->th.th_blocking,
false);
417 flag->suspend(th_gtid);
420 KMP_ATOMIC_ST_REL(&this_thr->th.th_blocking,
true);
423 if (TCR_4(__kmp_global.g.g_done)) {
424 if (__kmp_global.g.g_abort)
425 __kmp_abort_thread();
427 }
else if (__kmp_tasking_mode != tskm_immediate_exec &&
428 this_thr->th.th_reap_state == KMP_SAFE_TO_REAP) {
429 this_thr->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
435 omp_state_t ompt_exit_state = this_thr->th.ompt_thread_info.state;
436 if (ompt_enabled.enabled && ompt_exit_state != omp_state_undefined) {
439 __ompt_implicit_task_end(this_thr, ompt_exit_state, tId, pId);
440 ompt_exit_state = this_thr->th.ompt_thread_info.state;
443 if (ompt_exit_state == omp_state_idle) {
444 this_thr->th.ompt_thread_info.state = omp_state_overhead;
448 #if KMP_STATS_ENABLED 450 if (KMP_GET_THREAD_STATE() == IDLE) {
451 KMP_POP_PARTITIONED_TIMER();
452 KMP_SET_THREAD_STATE(thread_state);
453 this_thr->th.th_stats->resetIdleFlag();
459 KMP_ATOMIC_ST_REL(&this_thr->th.th_blocking,
false);
461 KMP_FSYNC_SPIN_ACQUIRED(CCAST(
void *, spin));
468 template <
class C>
static inline void __kmp_release_template(C *flag) {
470 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
472 KF_TRACE(20, (
"__kmp_release: T#%d releasing flag(%x)\n", gtid, flag->get()));
473 KMP_DEBUG_ASSERT(flag->get());
474 KMP_FSYNC_RELEASING(flag->get_void_p());
476 flag->internal_release();
478 KF_TRACE(100, (
"__kmp_release: T#%d set new spin=%d\n", gtid, flag->get(),
481 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
484 if (flag->is_any_sleeping()) {
485 for (
unsigned int i = 0; i < flag->get_num_waiters(); ++i) {
487 kmp_info_t *waiter = flag->get_waiter(i);
489 int wait_gtid = waiter->th.th_info.ds.ds_gtid;
491 KF_TRACE(50, (
"__kmp_release: T#%d waking up thread T#%d since sleep " 493 gtid, wait_gtid, flag->get()));
494 flag->resume(wait_gtid);
501 template <
typename FlagType>
struct flag_traits {};
503 template <>
struct flag_traits<kmp_uint32> {
504 typedef kmp_uint32 flag_t;
506 static inline flag_t tcr(flag_t f) {
return TCR_4(f); }
507 static inline flag_t test_then_add4(
volatile flag_t *f) {
508 return KMP_TEST_THEN_ADD4_32(RCAST(
volatile kmp_int32 *, f));
510 static inline flag_t test_then_or(
volatile flag_t *f, flag_t v) {
511 return KMP_TEST_THEN_OR32(f, v);
513 static inline flag_t test_then_and(
volatile flag_t *f, flag_t v) {
514 return KMP_TEST_THEN_AND32(f, v);
518 template <>
struct flag_traits<kmp_uint64> {
519 typedef kmp_uint64 flag_t;
521 static inline flag_t tcr(flag_t f) {
return TCR_8(f); }
522 static inline flag_t test_then_add4(
volatile flag_t *f) {
523 return KMP_TEST_THEN_ADD4_64(RCAST(
volatile kmp_int64 *, f));
525 static inline flag_t test_then_or(
volatile flag_t *f, flag_t v) {
526 return KMP_TEST_THEN_OR64(f, v);
528 static inline flag_t test_then_and(
volatile flag_t *f, flag_t v) {
529 return KMP_TEST_THEN_AND64(f, v);
534 template <
typename FlagType>
536 typedef flag_traits<FlagType> traits_type;
544 kmp_basic_flag_native(
volatile FlagType *p)
546 kmp_basic_flag_native(
volatile FlagType *p, kmp_info_t *thr)
548 waiting_threads[0] = thr;
550 kmp_basic_flag_native(
volatile FlagType *p, FlagType c)
552 num_waiting_threads(0) {}
557 kmp_info_t *get_waiter(kmp_uint32 i) {
558 KMP_DEBUG_ASSERT(i < num_waiting_threads);
559 return waiting_threads[i];
564 kmp_uint32 get_num_waiters() {
return num_waiting_threads; }
570 void set_waiter(kmp_info_t *thr) {
571 waiting_threads[0] = thr;
572 num_waiting_threads = 1;
577 bool done_check() {
return traits_type::tcr(*(this->
get())) == checker; }
582 bool done_check_val(FlagType old_loc) {
return old_loc == checker; }
590 bool notdone_check() {
return traits_type::tcr(*(this->
get())) != checker; }
595 void internal_release() {
596 (void)traits_type::test_then_add4((
volatile FlagType *)this->
get());
603 FlagType set_sleeping() {
604 return traits_type::test_then_or((
volatile FlagType *)this->
get(),
605 KMP_BARRIER_SLEEP_STATE);
612 FlagType unset_sleeping() {
613 return traits_type::test_then_and((
volatile FlagType *)this->
get(),
614 ~KMP_BARRIER_SLEEP_STATE);
620 bool is_sleeping_val(FlagType old_loc) {
621 return old_loc & KMP_BARRIER_SLEEP_STATE;
626 bool is_sleeping() {
return is_sleeping_val(*(this->
get())); }
627 bool is_any_sleeping() {
return is_sleeping_val(*(this->
get())); }
628 kmp_uint8 *get_stolen() {
return NULL; }
629 enum barrier_type get_bt() {
return bs_last_barrier; }
632 template <
typename FlagType>
class kmp_basic_flag :
public kmp_flag<FlagType> {
633 typedef flag_traits<FlagType> traits_type;
641 kmp_basic_flag(std::atomic<FlagType> *p)
643 kmp_basic_flag(std::atomic<FlagType> *p, kmp_info_t *thr)
645 waiting_threads[0] = thr;
647 kmp_basic_flag(std::atomic<FlagType> *p, FlagType c)
649 num_waiting_threads(0) {}
654 kmp_info_t *get_waiter(kmp_uint32 i) {
655 KMP_DEBUG_ASSERT(i < num_waiting_threads);
656 return waiting_threads[i];
661 kmp_uint32 get_num_waiters() {
return num_waiting_threads; }
667 void set_waiter(kmp_info_t *thr) {
668 waiting_threads[0] = thr;
669 num_waiting_threads = 1;
674 bool done_check() {
return this->load() == checker; }
679 bool done_check_val(FlagType old_loc) {
return old_loc == checker; }
687 bool notdone_check() {
return this->load() != checker; }
692 void internal_release() { KMP_ATOMIC_ADD(this->
get(), 4); }
698 FlagType set_sleeping() {
699 return KMP_ATOMIC_OR(this->
get(), KMP_BARRIER_SLEEP_STATE);
706 FlagType unset_sleeping() {
707 return KMP_ATOMIC_AND(this->
get(), ~KMP_BARRIER_SLEEP_STATE);
713 bool is_sleeping_val(FlagType old_loc) {
714 return old_loc & KMP_BARRIER_SLEEP_STATE;
719 bool is_sleeping() {
return is_sleeping_val(this->load()); }
720 bool is_any_sleeping() {
return is_sleeping_val(this->load()); }
721 kmp_uint8 *get_stolen() {
return NULL; }
722 enum barrier_type get_bt() {
return bs_last_barrier; }
725 class kmp_flag_32 :
public kmp_basic_flag<kmp_uint32> {
727 kmp_flag_32(std::atomic<kmp_uint32> *p) : kmp_basic_flag<kmp_uint32>(p) {}
728 kmp_flag_32(std::atomic<kmp_uint32> *p, kmp_info_t *thr)
729 : kmp_basic_flag<kmp_uint32>(p, thr) {}
730 kmp_flag_32(std::atomic<kmp_uint32> *p, kmp_uint32 c)
731 : kmp_basic_flag<kmp_uint32>(p, c) {}
732 void suspend(
int th_gtid) { __kmp_suspend_32(th_gtid,
this); }
733 void resume(
int th_gtid) { __kmp_resume_32(th_gtid,
this); }
734 int execute_tasks(kmp_info_t *this_thr, kmp_int32 gtid,
int final_spin,
735 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
736 kmp_int32 is_constrained) {
737 return __kmp_execute_tasks_32(
738 this_thr, gtid,
this, final_spin,
739 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
741 void wait(kmp_info_t *this_thr,
742 int final_spin USE_ITT_BUILD_ARG(
void *itt_sync_obj)) {
744 __kmp_wait_template<kmp_flag_32, TRUE>(
745 this_thr,
this USE_ITT_BUILD_ARG(itt_sync_obj));
747 __kmp_wait_template<kmp_flag_32, FALSE>(
748 this_thr,
this USE_ITT_BUILD_ARG(itt_sync_obj));
750 void release() { __kmp_release_template(
this); }
754 class kmp_flag_64 :
public kmp_basic_flag_native<kmp_uint64> {
756 kmp_flag_64(
volatile kmp_uint64 *p) : kmp_basic_flag_native<kmp_uint64>(p) {}
757 kmp_flag_64(
volatile kmp_uint64 *p, kmp_info_t *thr)
758 : kmp_basic_flag_native<kmp_uint64>(p, thr) {}
759 kmp_flag_64(
volatile kmp_uint64 *p, kmp_uint64 c)
760 : kmp_basic_flag_native<kmp_uint64>(p, c) {}
761 void suspend(
int th_gtid) { __kmp_suspend_64(th_gtid,
this); }
762 void resume(
int th_gtid) { __kmp_resume_64(th_gtid,
this); }
763 int execute_tasks(kmp_info_t *this_thr, kmp_int32 gtid,
int final_spin,
764 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
765 kmp_int32 is_constrained) {
766 return __kmp_execute_tasks_64(
767 this_thr, gtid,
this, final_spin,
768 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
770 void wait(kmp_info_t *this_thr,
771 int final_spin USE_ITT_BUILD_ARG(
void *itt_sync_obj)) {
773 __kmp_wait_template<kmp_flag_64, TRUE>(
774 this_thr,
this USE_ITT_BUILD_ARG(itt_sync_obj));
776 __kmp_wait_template<kmp_flag_64, FALSE>(
777 this_thr,
this USE_ITT_BUILD_ARG(itt_sync_obj));
779 void release() { __kmp_release_template(
this); }
786 kmp_info_t *waiting_threads[1];
787 kmp_uint32 num_waiting_threads;
791 enum barrier_type bt;
792 kmp_info_t *this_thr;
798 unsigned char &byteref(
volatile kmp_uint64 *loc,
size_t offset) {
799 return (RCAST(
unsigned char *, CCAST(kmp_uint64 *, loc)))[offset];
803 kmp_flag_oncore(
volatile kmp_uint64 *p)
805 flag_switch(
false) {}
806 kmp_flag_oncore(
volatile kmp_uint64 *p, kmp_uint32 idx)
808 offset(idx), flag_switch(
false) {}
809 kmp_flag_oncore(
volatile kmp_uint64 *p, kmp_uint64 c, kmp_uint32 idx,
810 enum barrier_type bar_t,
811 kmp_info_t *thr USE_ITT_BUILD_ARG(
void *itt))
813 num_waiting_threads(0), offset(idx), flag_switch(
false), bt(bar_t),
814 this_thr(thr) USE_ITT_BUILD_ARG(itt_sync_obj(itt)) {}
815 kmp_info_t *get_waiter(kmp_uint32 i) {
816 KMP_DEBUG_ASSERT(i < num_waiting_threads);
817 return waiting_threads[i];
819 kmp_uint32 get_num_waiters() {
return num_waiting_threads; }
820 void set_waiter(kmp_info_t *thr) {
821 waiting_threads[0] = thr;
822 num_waiting_threads = 1;
824 bool done_check_val(kmp_uint64 old_loc) {
825 return byteref(&old_loc, offset) == checker;
827 bool done_check() {
return done_check_val(*
get()); }
828 bool notdone_check() {
830 if (this_thr->th.th_bar[bt].bb.wait_flag == KMP_BARRIER_SWITCH_TO_OWN_FLAG)
832 if (byteref(
get(), offset) != 1 && !flag_switch)
834 else if (flag_switch) {
835 this_thr->th.th_bar[bt].bb.wait_flag = KMP_BARRIER_SWITCHING;
836 kmp_flag_64 flag(&this_thr->th.th_bar[bt].bb.b_go,
837 (kmp_uint64)KMP_BARRIER_STATE_BUMP);
838 __kmp_wait_64(this_thr, &flag, TRUE USE_ITT_BUILD_ARG(itt_sync_obj));
842 void internal_release() {
844 if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME) {
845 byteref(
get(), offset) = 1;
848 byteref(&mask, offset) = 1;
849 KMP_TEST_THEN_OR64(
get(), mask);
852 kmp_uint64 set_sleeping() {
853 return KMP_TEST_THEN_OR64(
get(), KMP_BARRIER_SLEEP_STATE);
855 kmp_uint64 unset_sleeping() {
856 return KMP_TEST_THEN_AND64(
get(), ~KMP_BARRIER_SLEEP_STATE);
858 bool is_sleeping_val(kmp_uint64 old_loc) {
859 return old_loc & KMP_BARRIER_SLEEP_STATE;
861 bool is_sleeping() {
return is_sleeping_val(*
get()); }
862 bool is_any_sleeping() {
return is_sleeping_val(*
get()); }
863 void wait(kmp_info_t *this_thr,
int final_spin) {
865 __kmp_wait_template<kmp_flag_oncore, TRUE>(
866 this_thr,
this USE_ITT_BUILD_ARG(itt_sync_obj));
868 __kmp_wait_template<kmp_flag_oncore, FALSE>(
869 this_thr,
this USE_ITT_BUILD_ARG(itt_sync_obj));
871 void release() { __kmp_release_template(
this); }
872 void suspend(
int th_gtid) { __kmp_suspend_oncore(th_gtid,
this); }
873 void resume(
int th_gtid) { __kmp_resume_oncore(th_gtid,
this); }
874 int execute_tasks(kmp_info_t *this_thr, kmp_int32 gtid,
int final_spin,
875 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
876 kmp_int32 is_constrained) {
877 return __kmp_execute_tasks_oncore(
878 this_thr, gtid,
this, final_spin,
879 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
881 kmp_uint8 *get_stolen() {
return NULL; }
882 enum barrier_type get_bt() {
return bt; }
888 static inline void __kmp_null_resume_wrapper(
int gtid,
volatile void *flag) {
892 switch (RCAST(kmp_flag_64 *, CCAST(
void *, flag))->get_type()) {
894 __kmp_resume_32(gtid, NULL);
897 __kmp_resume_64(gtid, NULL);
900 __kmp_resume_oncore(gtid, NULL);
909 #endif // KMP_WAIT_RELEASE_H
stats_state_e
the states which a thread can be in