16 #ifndef KMP_WAIT_RELEASE_H 17 #define KMP_WAIT_RELEASE_H 21 #include "kmp_stats.h" 58 volatile P *
get() {
return loc; }
62 void set(
volatile P *new_loc) { loc = new_loc; }
91 __kmp_wait_template(kmp_info_t *this_thr, C *flag,
int final_spin
92 USE_ITT_BUILD_ARG(
void * itt_sync_obj) )
95 volatile typename C::flag_t *spin = flag->get();
99 int tasks_completed = FALSE;
102 KMP_FSYNC_SPIN_INIT(spin, NULL);
103 if (flag->done_check()) {
104 KMP_FSYNC_SPIN_ACQUIRED(spin);
107 th_gtid = this_thr->th.th_info.ds.ds_gtid;
108 KA_TRACE(20, (
"__kmp_wait_sleep: T#%d waiting for flag(%p)\n", th_gtid, flag));
109 #if KMP_STATS_ENABLED 113 #if OMPT_SUPPORT && OMPT_BLAME 114 ompt_state_t ompt_state = this_thr->th.ompt_thread_info.state;
116 ompt_state != ompt_state_undefined) {
117 if (ompt_state == ompt_state_idle) {
118 if (ompt_callbacks.ompt_callback(ompt_event_idle_begin)) {
119 ompt_callbacks.ompt_callback(ompt_event_idle_begin)(th_gtid + 1);
121 }
else if (ompt_callbacks.ompt_callback(ompt_event_wait_barrier_begin)) {
122 KMP_DEBUG_ASSERT(ompt_state == ompt_state_wait_barrier ||
123 ompt_state == ompt_state_wait_barrier_implicit ||
124 ompt_state == ompt_state_wait_barrier_explicit);
126 ompt_lw_taskteam_t* team = this_thr->th.th_team->t.ompt_serialized_team_info;
127 ompt_parallel_id_t pId;
130 pId = team->ompt_team_info.parallel_id;
131 tId = team->ompt_task_info.task_id;
133 pId = this_thr->th.th_team->t.ompt_team_info.parallel_id;
134 tId = this_thr->th.th_current_task->ompt_task_info.task_id;
136 ompt_callbacks.ompt_callback(ompt_event_wait_barrier_begin)(pId, tId);
142 KMP_INIT_YIELD(spins);
144 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
147 #ifdef KMP_ADJUST_BLOCKTIME 148 if (__kmp_zero_bt && !this_thr->th.th_team_bt_set)
152 hibernate = this_thr->th.th_team_bt_intervals;
154 hibernate = this_thr->th.th_team_bt_intervals;
164 hibernate += TCR_4(__kmp_global.g.g_time.dt.t_value);
165 KF_TRACE(20, (
"__kmp_wait_sleep: T#%d now=%d, hibernate=%d, intervals=%d\n",
166 th_gtid, __kmp_global.g.g_time.dt.t_value, hibernate,
167 hibernate - __kmp_global.g.g_time.dt.t_value));
170 oversubscribed = (TCR_4(__kmp_nth) > __kmp_avail_proc);
174 while (flag->notdone_check()) {
176 kmp_task_team_t * task_team = NULL;
177 if (__kmp_tasking_mode != tskm_immediate_exec) {
178 task_team = this_thr->th.th_task_team;
185 if (task_team != NULL) {
186 if (TCR_SYNC_4(task_team->tt.tt_active)) {
187 if (KMP_TASKING_ENABLED(task_team))
188 flag->execute_tasks(this_thr, th_gtid, final_spin, &tasks_completed
189 USE_ITT_BUILD_ARG(itt_sync_obj), 0);
192 KMP_DEBUG_ASSERT(!KMP_MASTER_TID(this_thr->th.th_info.ds.ds_tid));
193 this_thr->th.th_task_team = NULL;
198 KMP_FSYNC_SPIN_PREPARE(spin);
199 if (TCR_4(__kmp_global.g.g_done)) {
200 if (__kmp_global.g.g_abort)
201 __kmp_abort_thread();
206 KMP_YIELD(oversubscribed);
210 KMP_YIELD_SPIN(spins);
214 in_pool = !!TCR_4(this_thr->th.th_in_pool);
215 if (in_pool != !!this_thr->th.th_active_in_pool) {
217 KMP_TEST_THEN_INC32((kmp_int32 *)&__kmp_thread_pool_active_nth);
218 this_thr->th.th_active_in_pool = TRUE;
226 KMP_TEST_THEN_DEC32((kmp_int32 *) &__kmp_thread_pool_active_nth);
227 KMP_DEBUG_ASSERT(TCR_4(__kmp_thread_pool_active_nth) >= 0);
228 this_thr->th.th_active_in_pool = FALSE;
232 #if KMP_STATS_ENABLED 235 if (this_thr->th.th_stats->isIdle() && KMP_GET_THREAD_STATE() == FORK_JOIN_BARRIER) {
236 KMP_SET_THREAD_STATE(IDLE);
237 KMP_PUSH_PARTITIONED_TIMER(OMP_idle);
242 if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME)
246 if ((task_team != NULL) && TCR_4(task_team->tt.tt_found_tasks))
250 if (TCR_4(__kmp_global.g.g_time.dt.t_value) < hibernate)
253 KF_TRACE(50, (
"__kmp_wait_sleep: T#%d suspend time reached\n", th_gtid));
255 flag->suspend(th_gtid);
257 if (TCR_4(__kmp_global.g.g_done)) {
258 if (__kmp_global.g.g_abort)
259 __kmp_abort_thread();
265 #if OMPT_SUPPORT && OMPT_BLAME 267 ompt_state != ompt_state_undefined) {
268 if (ompt_state == ompt_state_idle) {
269 if (ompt_callbacks.ompt_callback(ompt_event_idle_end)) {
270 ompt_callbacks.ompt_callback(ompt_event_idle_end)(th_gtid + 1);
272 }
else if (ompt_callbacks.ompt_callback(ompt_event_wait_barrier_end)) {
273 KMP_DEBUG_ASSERT(ompt_state == ompt_state_wait_barrier ||
274 ompt_state == ompt_state_wait_barrier_implicit ||
275 ompt_state == ompt_state_wait_barrier_explicit);
277 ompt_lw_taskteam_t* team = this_thr->th.th_team->t.ompt_serialized_team_info;
278 ompt_parallel_id_t pId;
281 pId = team->ompt_team_info.parallel_id;
282 tId = team->ompt_task_info.task_id;
284 pId = this_thr->th.th_team->t.ompt_team_info.parallel_id;
285 tId = this_thr->th.th_current_task->ompt_task_info.task_id;
287 ompt_callbacks.ompt_callback(ompt_event_wait_barrier_end)(pId, tId);
291 #if KMP_STATS_ENABLED 293 if (KMP_GET_THREAD_STATE() == IDLE) {
294 KMP_POP_PARTITIONED_TIMER();
295 KMP_SET_THREAD_STATE(thread_state);
296 this_thr->th.th_stats->resetIdleFlag();
300 KMP_FSYNC_SPIN_ACQUIRED(spin);
308 __kmp_release_template(C *flag)
311 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
313 KF_TRACE(20, (
"__kmp_release: T#%d releasing flag(%x)\n", gtid, flag->get()));
314 KMP_DEBUG_ASSERT(flag->get());
315 KMP_FSYNC_RELEASING(flag->get());
317 flag->internal_release();
319 KF_TRACE(100, (
"__kmp_release: T#%d set new spin=%d\n", gtid, flag->get(), *(flag->get())));
321 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
323 if (flag->is_any_sleeping()) {
324 for (
unsigned int i=0; i<flag->get_num_waiters(); ++i) {
325 kmp_info_t * waiter = flag->get_waiter(i);
327 int wait_gtid = waiter->th.th_info.ds.ds_gtid;
329 KF_TRACE(50, (
"__kmp_release: T#%d waking up thread T#%d since sleep flag(%p) set\n",
330 gtid, wait_gtid, flag->get()));
331 flag->resume(wait_gtid);
338 template <
typename FlagType>
339 struct flag_traits {};
342 struct flag_traits<kmp_uint32> {
343 typedef kmp_uint32 flag_t;
345 static inline flag_t tcr(flag_t f) {
return TCR_4(f); }
346 static inline flag_t test_then_add4(
volatile flag_t *f) {
return KMP_TEST_THEN_ADD4_32((
volatile kmp_int32 *)f); }
347 static inline flag_t test_then_or(
volatile flag_t *f, flag_t v) {
return KMP_TEST_THEN_OR32((
volatile kmp_int32 *)f, v); }
348 static inline flag_t test_then_and(
volatile flag_t *f, flag_t v) {
return KMP_TEST_THEN_AND32((
volatile kmp_int32 *)f, v); }
352 struct flag_traits<kmp_uint64> {
353 typedef kmp_uint64 flag_t;
355 static inline flag_t tcr(flag_t f) {
return TCR_8(f); }
356 static inline flag_t test_then_add4(
volatile flag_t *f) {
return KMP_TEST_THEN_ADD4_64((
volatile kmp_int64 *)f); }
357 static inline flag_t test_then_or(
volatile flag_t *f, flag_t v) {
return KMP_TEST_THEN_OR64((
volatile kmp_int64 *)f, v); }
358 static inline flag_t test_then_and(
volatile flag_t *f, flag_t v) {
return KMP_TEST_THEN_AND64((
volatile kmp_int64 *)f, v); }
361 template <
typename FlagType>
362 class kmp_basic_flag :
public kmp_flag<FlagType> {
363 typedef flag_traits<FlagType> traits_type;
365 kmp_info_t * waiting_threads[1];
366 kmp_uint32 num_waiting_threads;
368 kmp_basic_flag(
volatile FlagType *p) :
kmp_flag<FlagType>(p, traits_type::t), num_waiting_threads(0) {}
369 kmp_basic_flag(
volatile FlagType *p, kmp_info_t *thr) :
kmp_flag<FlagType>(p, traits_type::t), num_waiting_threads(1) {
370 waiting_threads[0] = thr;
372 kmp_basic_flag(
volatile FlagType *p, FlagType c) :
kmp_flag<FlagType>(p, traits_type::t), checker(c), num_waiting_threads(0) {}
377 kmp_info_t * get_waiter(kmp_uint32 i) {
378 KMP_DEBUG_ASSERT(i<num_waiting_threads);
379 return waiting_threads[i];
384 kmp_uint32 get_num_waiters() {
return num_waiting_threads; }
390 void set_waiter(kmp_info_t *thr) {
391 waiting_threads[0] = thr;
392 num_waiting_threads = 1;
397 bool done_check() {
return traits_type::tcr(*(this->
get())) == checker; }
402 bool done_check_val(FlagType old_loc) {
return old_loc == checker; }
410 bool notdone_check() {
return traits_type::tcr(*(this->
get())) != checker; }
415 void internal_release() {
416 (void) traits_type::test_then_add4((
volatile FlagType *)this->
get());
422 FlagType set_sleeping() {
423 return traits_type::test_then_or((
volatile FlagType *)this->
get(), KMP_BARRIER_SLEEP_STATE);
429 FlagType unset_sleeping() {
430 return traits_type::test_then_and((
volatile FlagType *)this->
get(), ~KMP_BARRIER_SLEEP_STATE);
436 bool is_sleeping_val(FlagType old_loc) {
return old_loc & KMP_BARRIER_SLEEP_STATE; }
440 bool is_sleeping() {
return is_sleeping_val(*(this->
get())); }
441 bool is_any_sleeping() {
return is_sleeping_val(*(this->
get())); }
442 kmp_uint8 *get_stolen() {
return NULL; }
443 enum barrier_type get_bt() {
return bs_last_barrier; }
446 class kmp_flag_32 :
public kmp_basic_flag<kmp_uint32> {
448 kmp_flag_32(
volatile kmp_uint32 *p) : kmp_basic_flag<kmp_uint32>(p) {}
449 kmp_flag_32(
volatile kmp_uint32 *p, kmp_info_t *thr) : kmp_basic_flag<kmp_uint32>(p, thr) {}
450 kmp_flag_32(
volatile kmp_uint32 *p, kmp_uint32 c) : kmp_basic_flag<kmp_uint32>(p, c) {}
451 void suspend(
int th_gtid) { __kmp_suspend_32(th_gtid,
this); }
452 void resume(
int th_gtid) { __kmp_resume_32(th_gtid,
this); }
453 int execute_tasks(kmp_info_t *this_thr, kmp_int32 gtid,
int final_spin,
int *thread_finished
454 USE_ITT_BUILD_ARG(
void * itt_sync_obj), kmp_int32 is_constrained) {
455 return __kmp_execute_tasks_32(this_thr, gtid,
this, final_spin, thread_finished
456 USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
458 void wait(kmp_info_t *this_thr,
int final_spin
459 USE_ITT_BUILD_ARG(
void * itt_sync_obj)) {
460 __kmp_wait_template(this_thr,
this, final_spin
461 USE_ITT_BUILD_ARG(itt_sync_obj));
463 void release() { __kmp_release_template(
this); }
467 class kmp_flag_64 :
public kmp_basic_flag<kmp_uint64> {
469 kmp_flag_64(
volatile kmp_uint64 *p) : kmp_basic_flag<kmp_uint64>(p) {}
470 kmp_flag_64(
volatile kmp_uint64 *p, kmp_info_t *thr) : kmp_basic_flag<kmp_uint64>(p, thr) {}
471 kmp_flag_64(
volatile kmp_uint64 *p, kmp_uint64 c) : kmp_basic_flag<kmp_uint64>(p, c) {}
472 void suspend(
int th_gtid) { __kmp_suspend_64(th_gtid,
this); }
473 void resume(
int th_gtid) { __kmp_resume_64(th_gtid,
this); }
474 int execute_tasks(kmp_info_t *this_thr, kmp_int32 gtid,
int final_spin,
int *thread_finished
475 USE_ITT_BUILD_ARG(
void * itt_sync_obj), kmp_int32 is_constrained) {
476 return __kmp_execute_tasks_64(this_thr, gtid,
this, final_spin, thread_finished
477 USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
479 void wait(kmp_info_t *this_thr,
int final_spin
480 USE_ITT_BUILD_ARG(
void * itt_sync_obj)) {
481 __kmp_wait_template(this_thr,
this, final_spin
482 USE_ITT_BUILD_ARG(itt_sync_obj));
484 void release() { __kmp_release_template(
this); }
489 class kmp_flag_oncore :
public kmp_flag<kmp_uint64> {
491 kmp_info_t * waiting_threads[1];
492 kmp_uint32 num_waiting_threads;
495 enum barrier_type bt;
496 kmp_info_t * this_thr;
500 unsigned char& byteref(
volatile kmp_uint64*
loc,
size_t offset) {
return ((
unsigned char *)loc)[offset]; }
502 kmp_flag_oncore(
volatile kmp_uint64 *p)
504 kmp_flag_oncore(
volatile kmp_uint64 *p, kmp_uint32 idx)
506 kmp_flag_oncore(
volatile kmp_uint64 *p, kmp_uint64 c, kmp_uint32 idx,
enum barrier_type bar_t,
513 flag_switch(
false), bt(bar_t), this_thr(thr)
518 kmp_info_t * get_waiter(kmp_uint32 i) {
519 KMP_DEBUG_ASSERT(i<num_waiting_threads);
520 return waiting_threads[i];
522 kmp_uint32 get_num_waiters() {
return num_waiting_threads; }
523 void set_waiter(kmp_info_t *thr) {
524 waiting_threads[0] = thr;
525 num_waiting_threads = 1;
527 bool done_check_val(kmp_uint64 old_loc) {
return byteref(&old_loc,offset) == checker; }
528 bool done_check() {
return done_check_val(*
get()); }
529 bool notdone_check() {
531 if (this_thr->th.th_bar[bt].bb.wait_flag == KMP_BARRIER_SWITCH_TO_OWN_FLAG)
533 if (byteref(
get(),offset) != 1 && !flag_switch)
535 else if (flag_switch) {
536 this_thr->th.th_bar[bt].bb.wait_flag = KMP_BARRIER_SWITCHING;
537 kmp_flag_64 flag(&this_thr->th.th_bar[bt].bb.b_go, (kmp_uint64)KMP_BARRIER_STATE_BUMP);
538 __kmp_wait_64(this_thr, &flag, TRUE
546 void internal_release() {
547 if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME) {
548 byteref(
get(),offset) = 1;
552 byteref(&mask,offset) = 1;
553 (void) KMP_TEST_THEN_OR64((
volatile kmp_int64 *)
get(), mask);
556 kmp_uint64 set_sleeping() {
557 return KMP_TEST_THEN_OR64((kmp_int64
volatile *)
get(), KMP_BARRIER_SLEEP_STATE);
559 kmp_uint64 unset_sleeping() {
560 return KMP_TEST_THEN_AND64((kmp_int64
volatile *)
get(), ~KMP_BARRIER_SLEEP_STATE);
562 bool is_sleeping_val(kmp_uint64 old_loc) {
return old_loc & KMP_BARRIER_SLEEP_STATE; }
563 bool is_sleeping() {
return is_sleeping_val(*
get()); }
564 bool is_any_sleeping() {
return is_sleeping_val(*
get()); }
565 void wait(kmp_info_t *this_thr,
int final_spin) {
566 __kmp_wait_template<kmp_flag_oncore>(this_thr,
this, final_spin
567 USE_ITT_BUILD_ARG(itt_sync_obj));
569 void release() { __kmp_release_template(
this); }
570 void suspend(
int th_gtid) { __kmp_suspend_oncore(th_gtid,
this); }
571 void resume(
int th_gtid) { __kmp_resume_oncore(th_gtid,
this); }
572 int execute_tasks(kmp_info_t *this_thr, kmp_int32 gtid,
int final_spin,
int *thread_finished
573 USE_ITT_BUILD_ARG(
void * itt_sync_obj), kmp_int32 is_constrained) {
574 return __kmp_execute_tasks_oncore(this_thr, gtid,
this, final_spin, thread_finished
575 USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
577 kmp_uint8 *get_stolen() {
return NULL; }
578 enum barrier_type get_bt() {
return bt; }
584 static inline void __kmp_null_resume_wrapper(
int gtid,
volatile void *flag) {
587 switch (((kmp_flag_64 *)flag)->
get_type()) {
588 case flag32: __kmp_resume_32(gtid, NULL);
break;
589 case flag64: __kmp_resume_64(gtid, NULL);
break;
590 case flag_oncore: __kmp_resume_oncore(gtid, NULL);
break;
598 #endif // KMP_WAIT_RELEASE_H
stats_state_e
the states which a thread can be in