16 #include "kmp_stats.h"
17 #include "kmp_wait_release.h"
18 #include "kmp_taskdeps.h"
21 #include "ompt-specific.h"
24 #include "tsan_annotations.h"
27 static void __kmp_enable_tasking(kmp_task_team_t *task_team,
28 kmp_info_t *this_thr);
29 static void __kmp_alloc_task_deque(kmp_info_t *thread,
30 kmp_thread_data_t *thread_data);
31 static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
32 kmp_task_team_t *task_team);
33 static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask);
35 #ifdef BUILD_TIED_TASK_STACK
44 static void __kmp_trace_task_stack(kmp_int32 gtid,
45 kmp_thread_data_t *thread_data,
46 int threshold,
char *location) {
47 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
48 kmp_taskdata_t **stack_top = task_stack->ts_top;
49 kmp_int32 entries = task_stack->ts_entries;
50 kmp_taskdata_t *tied_task;
54 (
"__kmp_trace_task_stack(start): location = %s, gtid = %d, entries = %d, "
55 "first_block = %p, stack_top = %p \n",
56 location, gtid, entries, task_stack->ts_first_block, stack_top));
58 KMP_DEBUG_ASSERT(stack_top != NULL);
59 KMP_DEBUG_ASSERT(entries > 0);
61 while (entries != 0) {
62 KMP_DEBUG_ASSERT(stack_top != &task_stack->ts_first_block.sb_block[0]);
64 if (entries & TASK_STACK_INDEX_MASK == 0) {
65 kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(stack_top);
67 stack_block = stack_block->sb_prev;
68 stack_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE];
75 tied_task = *stack_top;
77 KMP_DEBUG_ASSERT(tied_task != NULL);
78 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
81 (
"__kmp_trace_task_stack(%s): gtid=%d, entry=%d, "
82 "stack_top=%p, tied_task=%p\n",
83 location, gtid, entries, stack_top, tied_task));
85 KMP_DEBUG_ASSERT(stack_top == &task_stack->ts_first_block.sb_block[0]);
88 (
"__kmp_trace_task_stack(exit): location = %s, gtid = %d\n",
98 static void __kmp_init_task_stack(kmp_int32 gtid,
99 kmp_thread_data_t *thread_data) {
100 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
101 kmp_stack_block_t *first_block;
104 first_block = &task_stack->ts_first_block;
105 task_stack->ts_top = (kmp_taskdata_t **)first_block;
106 memset((
void *)first_block,
'\0',
107 TASK_STACK_BLOCK_SIZE *
sizeof(kmp_taskdata_t *));
110 task_stack->ts_entries = TASK_STACK_EMPTY;
111 first_block->sb_next = NULL;
112 first_block->sb_prev = NULL;
119 static void __kmp_free_task_stack(kmp_int32 gtid,
120 kmp_thread_data_t *thread_data) {
121 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
122 kmp_stack_block_t *stack_block = &task_stack->ts_first_block;
124 KMP_DEBUG_ASSERT(task_stack->ts_entries == TASK_STACK_EMPTY);
126 while (stack_block != NULL) {
127 kmp_stack_block_t *next_block = (stack_block) ? stack_block->sb_next : NULL;
129 stack_block->sb_next = NULL;
130 stack_block->sb_prev = NULL;
131 if (stack_block != &task_stack->ts_first_block) {
132 __kmp_thread_free(thread,
135 stack_block = next_block;
138 task_stack->ts_entries = 0;
139 task_stack->ts_top = NULL;
148 static void __kmp_push_task_stack(kmp_int32 gtid, kmp_info_t *thread,
149 kmp_taskdata_t *tied_task) {
151 kmp_thread_data_t *thread_data =
152 &thread->th.th_task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
153 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
155 if (tied_task->td_flags.team_serial || tied_task->td_flags.tasking_ser) {
159 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
160 KMP_DEBUG_ASSERT(task_stack->ts_top != NULL);
163 (
"__kmp_push_task_stack(enter): GTID: %d; THREAD: %p; TASK: %p\n",
164 gtid, thread, tied_task));
166 *(task_stack->ts_top) = tied_task;
169 task_stack->ts_top++;
170 task_stack->ts_entries++;
172 if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) {
174 kmp_stack_block_t *stack_block =
175 (kmp_stack_block_t *)(task_stack->ts_top - TASK_STACK_BLOCK_SIZE);
178 if (stack_block->sb_next !=
180 task_stack->ts_top = &stack_block->sb_next->sb_block[0];
182 kmp_stack_block_t *new_block = (kmp_stack_block_t *)__kmp_thread_calloc(
183 thread,
sizeof(kmp_stack_block_t));
185 task_stack->ts_top = &new_block->sb_block[0];
186 stack_block->sb_next = new_block;
187 new_block->sb_prev = stack_block;
188 new_block->sb_next = NULL;
192 (
"__kmp_push_task_stack(): GTID: %d; TASK: %p; Alloc new block: %p\n",
193 gtid, tied_task, new_block));
196 KA_TRACE(20, (
"__kmp_push_task_stack(exit): GTID: %d; TASK: %p\n", gtid,
207 static void __kmp_pop_task_stack(kmp_int32 gtid, kmp_info_t *thread,
208 kmp_taskdata_t *ending_task) {
210 kmp_thread_data_t *thread_data =
211 &thread->th.th_task_team->tt_threads_data[__kmp_tid_from_gtid(gtid)];
212 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
213 kmp_taskdata_t *tied_task;
215 if (ending_task->td_flags.team_serial || ending_task->td_flags.tasking_ser) {
220 KMP_DEBUG_ASSERT(task_stack->ts_top != NULL);
221 KMP_DEBUG_ASSERT(task_stack->ts_entries > 0);
223 KA_TRACE(20, (
"__kmp_pop_task_stack(enter): GTID: %d; THREAD: %p\n", gtid,
227 if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) {
228 kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(task_stack->ts_top);
230 stack_block = stack_block->sb_prev;
231 task_stack->ts_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE];
235 task_stack->ts_top--;
236 task_stack->ts_entries--;
238 tied_task = *(task_stack->ts_top);
240 KMP_DEBUG_ASSERT(tied_task != NULL);
241 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
242 KMP_DEBUG_ASSERT(tied_task == ending_task);
244 KA_TRACE(20, (
"__kmp_pop_task_stack(exit): GTID: %d; TASK: %p\n", gtid,
253 static bool __kmp_task_is_allowed(
int gtid,
const kmp_int32 is_constrained,
254 const kmp_taskdata_t *tasknew,
255 const kmp_taskdata_t *taskcurr) {
256 if (is_constrained && (tasknew->td_flags.tiedness == TASK_TIED)) {
260 kmp_taskdata_t *current = taskcurr->td_last_tied;
261 KMP_DEBUG_ASSERT(current != NULL);
263 if (current->td_flags.tasktype == TASK_EXPLICIT ||
264 current->td_taskwait_thread > 0) {
265 kmp_int32 level = current->td_level;
266 kmp_taskdata_t *parent = tasknew->td_parent;
267 while (parent != current && parent->td_level > level) {
269 parent = parent->td_parent;
270 KMP_DEBUG_ASSERT(parent != NULL);
272 if (parent != current)
277 kmp_depnode_t *node = tasknew->td_depnode;
278 if (UNLIKELY(node && (node->dn.mtx_num_locks > 0))) {
279 for (
int i = 0; i < node->dn.mtx_num_locks; ++i) {
280 KMP_DEBUG_ASSERT(node->dn.mtx_locks[i] != NULL);
281 if (__kmp_test_lock(node->dn.mtx_locks[i], gtid))
284 for (
int j = i - 1; j >= 0; --j)
285 __kmp_release_lock(node->dn.mtx_locks[j], gtid);
289 node->dn.mtx_num_locks = -node->dn.mtx_num_locks;
298 static void __kmp_realloc_task_deque(kmp_info_t *thread,
299 kmp_thread_data_t *thread_data) {
300 kmp_int32 size = TASK_DEQUE_SIZE(thread_data->td);
301 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == size);
302 kmp_int32 new_size = 2 * size;
304 KE_TRACE(10, (
"__kmp_realloc_task_deque: T#%d reallocating deque[from %d to "
305 "%d] for thread_data %p\n",
306 __kmp_gtid_from_thread(thread), size, new_size, thread_data));
308 kmp_taskdata_t **new_deque =
309 (kmp_taskdata_t **)__kmp_allocate(new_size *
sizeof(kmp_taskdata_t *));
312 for (i = thread_data->td.td_deque_head, j = 0; j < size;
313 i = (i + 1) & TASK_DEQUE_MASK(thread_data->td), j++)
314 new_deque[j] = thread_data->td.td_deque[i];
316 __kmp_free(thread_data->td.td_deque);
318 thread_data->td.td_deque_head = 0;
319 thread_data->td.td_deque_tail = size;
320 thread_data->td.td_deque = new_deque;
321 thread_data->td.td_deque_size = new_size;
325 static kmp_int32 __kmp_push_task(kmp_int32 gtid, kmp_task_t *task) {
326 kmp_info_t *thread = __kmp_threads[gtid];
327 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
330 if (taskdata->td_flags.hidden_helper && !KMP_HIDDEN_HELPER_THREAD(gtid)) {
331 gtid = KMP_GTID_TO_SHADOW_GTID(gtid);
332 thread = __kmp_threads[gtid];
335 kmp_task_team_t *task_team = thread->th.th_task_team;
336 kmp_int32 tid = __kmp_tid_from_gtid(gtid);
337 kmp_thread_data_t *thread_data;
340 (
"__kmp_push_task: T#%d trying to push task %p.\n", gtid, taskdata));
342 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
345 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
346 KMP_DEBUG_USE_VAR(counter);
349 (
"__kmp_push_task: T#%d untied_count (%d) incremented for task %p\n",
350 gtid, counter, taskdata));
354 if (UNLIKELY(taskdata->td_flags.task_serial)) {
355 KA_TRACE(20, (
"__kmp_push_task: T#%d team serialized; returning "
356 "TASK_NOT_PUSHED for task %p\n",
358 return TASK_NOT_PUSHED;
363 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
364 if (UNLIKELY(!KMP_TASKING_ENABLED(task_team))) {
365 __kmp_enable_tasking(task_team, thread);
367 KMP_DEBUG_ASSERT(TCR_4(task_team->tt.tt_found_tasks) == TRUE);
368 KMP_DEBUG_ASSERT(TCR_PTR(task_team->tt.tt_threads_data) != NULL);
371 thread_data = &task_team->tt.tt_threads_data[tid];
376 if (UNLIKELY(thread_data->td.td_deque == NULL)) {
377 __kmp_alloc_task_deque(thread, thread_data);
382 if (TCR_4(thread_data->td.td_deque_ntasks) >=
383 TASK_DEQUE_SIZE(thread_data->td)) {
384 if (__kmp_enable_task_throttling &&
385 __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
386 thread->th.th_current_task)) {
387 KA_TRACE(20, (
"__kmp_push_task: T#%d deque is full; returning "
388 "TASK_NOT_PUSHED for task %p\n",
390 return TASK_NOT_PUSHED;
392 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
394 if (TCR_4(thread_data->td.td_deque_ntasks) >=
395 TASK_DEQUE_SIZE(thread_data->td)) {
397 __kmp_realloc_task_deque(thread, thread_data);
403 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
405 if (TCR_4(thread_data->td.td_deque_ntasks) >=
406 TASK_DEQUE_SIZE(thread_data->td)) {
407 if (__kmp_enable_task_throttling &&
408 __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
409 thread->th.th_current_task)) {
410 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
411 KA_TRACE(20, (
"__kmp_push_task: T#%d deque is full on 2nd check; "
412 "returning TASK_NOT_PUSHED for task %p\n",
414 return TASK_NOT_PUSHED;
417 __kmp_realloc_task_deque(thread, thread_data);
422 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) <
423 TASK_DEQUE_SIZE(thread_data->td));
425 thread_data->td.td_deque[thread_data->td.td_deque_tail] =
428 thread_data->td.td_deque_tail =
429 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
430 TCW_4(thread_data->td.td_deque_ntasks,
431 TCR_4(thread_data->td.td_deque_ntasks) + 1);
432 KMP_FSYNC_RELEASING(thread->th.th_current_task);
433 KMP_FSYNC_RELEASING(taskdata);
434 KA_TRACE(20, (
"__kmp_push_task: T#%d returning TASK_SUCCESSFULLY_PUSHED: "
435 "task=%p ntasks=%d head=%u tail=%u\n",
436 gtid, taskdata, thread_data->td.td_deque_ntasks,
437 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
439 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
442 if (taskdata->td_flags.hidden_helper) {
444 __kmp_hidden_helper_worker_thread_signal();
447 return TASK_SUCCESSFULLY_PUSHED;
454 void __kmp_pop_current_task_from_thread(kmp_info_t *this_thr) {
455 KF_TRACE(10, (
"__kmp_pop_current_task_from_thread(enter): T#%d "
456 "this_thread=%p, curtask=%p, "
457 "curtask_parent=%p\n",
458 0, this_thr, this_thr->th.th_current_task,
459 this_thr->th.th_current_task->td_parent));
461 this_thr->th.th_current_task = this_thr->th.th_current_task->td_parent;
463 KF_TRACE(10, (
"__kmp_pop_current_task_from_thread(exit): T#%d "
464 "this_thread=%p, curtask=%p, "
465 "curtask_parent=%p\n",
466 0, this_thr, this_thr->th.th_current_task,
467 this_thr->th.th_current_task->td_parent));
476 void __kmp_push_current_task_to_thread(kmp_info_t *this_thr, kmp_team_t *team,
480 KF_TRACE(10, (
"__kmp_push_current_task_to_thread(enter): T#%d this_thread=%p "
483 tid, this_thr, this_thr->th.th_current_task,
484 team->t.t_implicit_task_taskdata[tid].td_parent));
486 KMP_DEBUG_ASSERT(this_thr != NULL);
489 if (this_thr->th.th_current_task != &team->t.t_implicit_task_taskdata[0]) {
490 team->t.t_implicit_task_taskdata[0].td_parent =
491 this_thr->th.th_current_task;
492 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[0];
495 team->t.t_implicit_task_taskdata[tid].td_parent =
496 team->t.t_implicit_task_taskdata[0].td_parent;
497 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[tid];
500 KF_TRACE(10, (
"__kmp_push_current_task_to_thread(exit): T#%d this_thread=%p "
503 tid, this_thr, this_thr->th.th_current_task,
504 team->t.t_implicit_task_taskdata[tid].td_parent));
512 static void __kmp_task_start(kmp_int32 gtid, kmp_task_t *task,
513 kmp_taskdata_t *current_task) {
514 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
515 kmp_info_t *thread = __kmp_threads[gtid];
518 (
"__kmp_task_start(enter): T#%d starting task %p: current_task=%p\n",
519 gtid, taskdata, current_task));
521 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
526 current_task->td_flags.executing = 0;
529 #ifdef BUILD_TIED_TASK_STACK
530 if (taskdata->td_flags.tiedness == TASK_TIED) {
531 __kmp_push_task_stack(gtid, thread, taskdata);
536 thread->th.th_current_task = taskdata;
538 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 0 ||
539 taskdata->td_flags.tiedness == TASK_UNTIED);
540 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0 ||
541 taskdata->td_flags.tiedness == TASK_UNTIED);
542 taskdata->td_flags.started = 1;
543 taskdata->td_flags.executing = 1;
544 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
545 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
552 KA_TRACE(10, (
"__kmp_task_start(exit): T#%d task=%p\n", gtid, taskdata));
563 static inline void __ompt_task_init(kmp_taskdata_t *task,
int tid) {
565 task->ompt_task_info.task_data.value = 0;
566 task->ompt_task_info.frame.exit_frame = ompt_data_none;
567 task->ompt_task_info.frame.enter_frame = ompt_data_none;
568 task->ompt_task_info.frame.exit_frame_flags = ompt_frame_runtime | ompt_frame_framepointer;
569 task->ompt_task_info.frame.enter_frame_flags = ompt_frame_runtime | ompt_frame_framepointer;
574 static inline void __ompt_task_start(kmp_task_t *task,
575 kmp_taskdata_t *current_task,
577 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
578 ompt_task_status_t status = ompt_task_switch;
579 if (__kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded) {
580 status = ompt_task_yield;
581 __kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded = 0;
584 if (ompt_enabled.ompt_callback_task_schedule) {
585 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
586 &(current_task->ompt_task_info.task_data), status,
587 &(taskdata->ompt_task_info.task_data));
589 taskdata->ompt_task_info.scheduling_parent = current_task;
594 static inline void __ompt_task_finish(kmp_task_t *task,
595 kmp_taskdata_t *resumed_task,
596 ompt_task_status_t status) {
597 if (ompt_enabled.ompt_callback_task_schedule) {
598 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
599 if (__kmp_omp_cancellation && taskdata->td_taskgroup &&
600 taskdata->td_taskgroup->cancel_request == cancel_taskgroup) {
601 status = ompt_task_cancel;
605 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
606 &(taskdata->ompt_task_info.task_data), status,
607 (resumed_task ? &(resumed_task->ompt_task_info.task_data) : NULL));
613 static void __kmpc_omp_task_begin_if0_template(
ident_t *loc_ref, kmp_int32 gtid,
616 void *return_address) {
617 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
618 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
620 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(enter): T#%d loc=%p task=%p "
622 gtid, loc_ref, taskdata, current_task));
624 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
627 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
628 KMP_DEBUG_USE_VAR(counter);
629 KA_TRACE(20, (
"__kmpc_omp_task_begin_if0: T#%d untied_count (%d) "
630 "incremented for task %p\n",
631 gtid, counter, taskdata));
634 taskdata->td_flags.task_serial =
636 __kmp_task_start(gtid, task, current_task);
640 if (current_task->ompt_task_info.frame.enter_frame.ptr == NULL) {
641 current_task->ompt_task_info.frame.enter_frame.ptr =
642 taskdata->ompt_task_info.frame.exit_frame.ptr = frame_address;
643 current_task->ompt_task_info.frame.enter_frame_flags =
644 taskdata->ompt_task_info.frame.exit_frame_flags = ompt_frame_application | ompt_frame_framepointer;
646 if (ompt_enabled.ompt_callback_task_create) {
647 ompt_task_info_t *parent_info = &(current_task->ompt_task_info);
648 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
649 &(parent_info->task_data), &(parent_info->frame),
650 &(taskdata->ompt_task_info.task_data),
651 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(taskdata), 0,
654 __ompt_task_start(task, current_task, gtid);
658 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(exit): T#%d loc=%p task=%p,\n", gtid,
664 static void __kmpc_omp_task_begin_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
667 void *return_address) {
668 __kmpc_omp_task_begin_if0_template<true>(loc_ref, gtid, task, frame_address,
679 void __kmpc_omp_task_begin_if0(
ident_t *loc_ref, kmp_int32 gtid,
682 if (UNLIKELY(ompt_enabled.enabled)) {
683 OMPT_STORE_RETURN_ADDRESS(gtid);
684 __kmpc_omp_task_begin_if0_ompt(loc_ref, gtid, task,
685 OMPT_GET_FRAME_ADDRESS(1),
686 OMPT_LOAD_RETURN_ADDRESS(gtid));
690 __kmpc_omp_task_begin_if0_template<false>(loc_ref, gtid, task, NULL, NULL);
696 void __kmpc_omp_task_begin(
ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task) {
697 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
701 (
"__kmpc_omp_task_begin(enter): T#%d loc=%p task=%p current_task=%p\n",
702 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task), current_task));
704 __kmp_task_start(gtid, task, current_task);
706 KA_TRACE(10, (
"__kmpc_omp_task_begin(exit): T#%d loc=%p task=%p,\n", gtid,
707 loc_ref, KMP_TASK_TO_TASKDATA(task)));
717 static void __kmp_free_task(kmp_int32 gtid, kmp_taskdata_t *taskdata,
718 kmp_info_t *thread) {
719 KA_TRACE(30, (
"__kmp_free_task: T#%d freeing data from task %p\n", gtid,
723 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
724 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0);
725 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 1);
726 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
727 KMP_DEBUG_ASSERT(taskdata->td_allocated_child_tasks == 0 ||
728 taskdata->td_flags.task_serial == 1);
729 KMP_DEBUG_ASSERT(taskdata->td_incomplete_child_tasks == 0);
731 taskdata->td_flags.freed = 1;
732 ANNOTATE_HAPPENS_BEFORE(taskdata);
735 __kmp_fast_free(thread, taskdata);
737 __kmp_thread_free(thread, taskdata);
739 KA_TRACE(20, (
"__kmp_free_task: T#%d freed task %p\n", gtid, taskdata));
748 static void __kmp_free_task_and_ancestors(kmp_int32 gtid,
749 kmp_taskdata_t *taskdata,
750 kmp_info_t *thread) {
753 kmp_int32 team_serial =
754 (taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) &&
755 !taskdata->td_flags.proxy;
756 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
758 kmp_int32 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
759 KMP_DEBUG_ASSERT(children >= 0);
762 while (children == 0) {
763 kmp_taskdata_t *parent_taskdata = taskdata->td_parent;
765 KA_TRACE(20, (
"__kmp_free_task_and_ancestors(enter): T#%d task %p complete "
766 "and freeing itself\n",
770 __kmp_free_task(gtid, taskdata, thread);
772 taskdata = parent_taskdata;
778 if (taskdata->td_flags.tasktype == TASK_IMPLICIT) {
779 if (taskdata->td_dephash) {
780 int children = KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks);
781 kmp_tasking_flags_t flags_old = taskdata->td_flags;
782 if (children == 0 && flags_old.complete == 1) {
783 kmp_tasking_flags_t flags_new = flags_old;
784 flags_new.complete = 0;
785 if (KMP_COMPARE_AND_STORE_ACQ32(
786 RCAST(kmp_int32 *, &taskdata->td_flags),
787 *RCAST(kmp_int32 *, &flags_old),
788 *RCAST(kmp_int32 *, &flags_new))) {
789 KA_TRACE(100, (
"__kmp_free_task_and_ancestors: T#%d cleans "
790 "dephash of implicit task %p\n",
793 __kmp_dephash_free_entries(thread, taskdata->td_dephash);
800 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
801 KMP_DEBUG_ASSERT(children >= 0);
805 20, (
"__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; "
806 "not freeing it yet\n",
807 gtid, taskdata, children));
820 static void __kmp_task_finish(kmp_int32 gtid, kmp_task_t *task,
821 kmp_taskdata_t *resumed_task) {
822 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
823 kmp_info_t *thread = __kmp_threads[gtid];
824 kmp_task_team_t *task_team =
825 thread->th.th_task_team;
826 kmp_int32 children = 0;
828 KA_TRACE(10, (
"__kmp_task_finish(enter): T#%d finishing task %p and resuming "
830 gtid, taskdata, resumed_task));
832 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
835 #ifdef BUILD_TIED_TASK_STACK
836 if (taskdata->td_flags.tiedness == TASK_TIED) {
837 __kmp_pop_task_stack(gtid, thread, taskdata);
841 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
844 kmp_int32 counter = KMP_ATOMIC_DEC(&taskdata->td_untied_count) - 1;
847 (
"__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n",
848 gtid, counter, taskdata));
852 if (resumed_task == NULL) {
853 KMP_DEBUG_ASSERT(taskdata->td_flags.task_serial);
854 resumed_task = taskdata->td_parent;
857 thread->th.th_current_task = resumed_task;
858 resumed_task->td_flags.executing = 1;
859 KA_TRACE(10, (
"__kmp_task_finish(exit): T#%d partially done task %p, "
860 "resuming task %p\n",
861 gtid, taskdata, resumed_task));
869 (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) ==
870 taskdata->td_flags.task_serial);
871 if (taskdata->td_flags.task_serial) {
872 if (resumed_task == NULL) {
873 resumed_task = taskdata->td_parent;
877 KMP_DEBUG_ASSERT(resumed_task !=
887 if (taskdata->td_flags.destructors_thunk) {
888 kmp_routine_entry_t destr_thunk = task->data1.destructors;
889 KMP_ASSERT(destr_thunk);
890 destr_thunk(gtid, task);
893 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
894 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 1);
895 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
898 if (taskdata->td_flags.detachable == TASK_DETACHABLE) {
899 if (taskdata->td_allow_completion_event.type ==
900 KMP_EVENT_ALLOW_COMPLETION) {
902 __kmp_acquire_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
903 if (taskdata->td_allow_completion_event.type ==
904 KMP_EVENT_ALLOW_COMPLETION) {
906 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
907 taskdata->td_flags.executing = 0;
914 __ompt_task_finish(task, resumed_task, ompt_task_detach);
920 taskdata->td_flags.proxy = TASK_PROXY;
923 __kmp_release_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
928 taskdata->td_flags.complete = 1;
933 __ompt_task_finish(task, resumed_task, ompt_task_complete);
938 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) ||
939 taskdata->td_flags.detachable == TASK_DETACHABLE ||
940 taskdata->td_flags.hidden_helper) {
943 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks) - 1;
944 KMP_DEBUG_ASSERT(children >= 0);
945 if (taskdata->td_taskgroup)
946 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
947 __kmp_release_deps(gtid, taskdata);
948 }
else if (task_team && task_team->tt.tt_found_proxy_tasks) {
951 __kmp_release_deps(gtid, taskdata);
957 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
958 taskdata->td_flags.executing = 0;
963 20, (
"__kmp_task_finish: T#%d finished task %p, %d incomplete children\n",
964 gtid, taskdata, children));
970 thread->th.th_current_task = resumed_task;
972 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
976 resumed_task->td_flags.executing = 1;
979 10, (
"__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n",
980 gtid, taskdata, resumed_task));
986 static void __kmpc_omp_task_complete_if0_template(
ident_t *loc_ref,
989 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n",
990 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
991 __kmp_assert_valid_gtid(gtid);
993 __kmp_task_finish<ompt>(gtid, task, NULL);
995 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n",
996 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
1000 ompt_frame_t *ompt_frame;
1001 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
1002 ompt_frame->enter_frame = ompt_data_none;
1003 ompt_frame->enter_frame_flags = ompt_frame_runtime | ompt_frame_framepointer;
1012 void __kmpc_omp_task_complete_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
1014 __kmpc_omp_task_complete_if0_template<true>(loc_ref, gtid, task);
1023 void __kmpc_omp_task_complete_if0(
ident_t *loc_ref, kmp_int32 gtid,
1026 if (UNLIKELY(ompt_enabled.enabled)) {
1027 __kmpc_omp_task_complete_if0_ompt(loc_ref, gtid, task);
1031 __kmpc_omp_task_complete_if0_template<false>(loc_ref, gtid, task);
1037 void __kmpc_omp_task_complete(
ident_t *loc_ref, kmp_int32 gtid,
1039 KA_TRACE(10, (
"__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n", gtid,
1040 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1042 __kmp_task_finish<false>(gtid, task,
1045 KA_TRACE(10, (
"__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n", gtid,
1046 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1062 void __kmp_init_implicit_task(
ident_t *loc_ref, kmp_info_t *this_thr,
1063 kmp_team_t *team,
int tid,
int set_curr_task) {
1064 kmp_taskdata_t *task = &team->t.t_implicit_task_taskdata[tid];
1068 (
"__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n",
1069 tid, team, task, set_curr_task ?
"TRUE" :
"FALSE"));
1071 task->td_task_id = KMP_GEN_TASK_ID();
1072 task->td_team = team;
1075 task->td_ident = loc_ref;
1076 task->td_taskwait_ident = NULL;
1077 task->td_taskwait_counter = 0;
1078 task->td_taskwait_thread = 0;
1080 task->td_flags.tiedness = TASK_TIED;
1081 task->td_flags.tasktype = TASK_IMPLICIT;
1082 task->td_flags.proxy = TASK_FULL;
1085 task->td_flags.task_serial = 1;
1086 task->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1087 task->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1089 task->td_flags.started = 1;
1090 task->td_flags.executing = 1;
1091 task->td_flags.complete = 0;
1092 task->td_flags.freed = 0;
1094 task->td_depnode = NULL;
1095 task->td_last_tied = task;
1096 task->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1098 if (set_curr_task) {
1099 KMP_ATOMIC_ST_REL(&task->td_incomplete_child_tasks, 0);
1101 KMP_ATOMIC_ST_REL(&task->td_allocated_child_tasks, 0);
1102 task->td_taskgroup = NULL;
1103 task->td_dephash = NULL;
1104 __kmp_push_current_task_to_thread(this_thr, team, tid);
1106 KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0);
1107 KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0);
1111 if (UNLIKELY(ompt_enabled.enabled))
1112 __ompt_task_init(task, tid);
1115 KF_TRACE(10, (
"__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n", tid,
1124 void __kmp_finish_implicit_task(kmp_info_t *thread) {
1125 kmp_taskdata_t *task = thread->th.th_current_task;
1126 if (task->td_dephash) {
1128 task->td_flags.complete = 1;
1129 children = KMP_ATOMIC_LD_ACQ(&task->td_incomplete_child_tasks);
1130 kmp_tasking_flags_t flags_old = task->td_flags;
1131 if (children == 0 && flags_old.complete == 1) {
1132 kmp_tasking_flags_t flags_new = flags_old;
1133 flags_new.complete = 0;
1134 if (KMP_COMPARE_AND_STORE_ACQ32(RCAST(kmp_int32 *, &task->td_flags),
1135 *RCAST(kmp_int32 *, &flags_old),
1136 *RCAST(kmp_int32 *, &flags_new))) {
1137 KA_TRACE(100, (
"__kmp_finish_implicit_task: T#%d cleans "
1138 "dephash of implicit task %p\n",
1139 thread->th.th_info.ds.ds_gtid, task));
1140 __kmp_dephash_free_entries(thread, task->td_dephash);
1150 void __kmp_free_implicit_task(kmp_info_t *thread) {
1151 kmp_taskdata_t *task = thread->th.th_current_task;
1152 if (task && task->td_dephash) {
1153 __kmp_dephash_free(thread, task->td_dephash);
1154 task->td_dephash = NULL;
1160 static size_t __kmp_round_up_to_val(
size_t size,
size_t val) {
1161 if (size & (val - 1)) {
1163 if (size <= KMP_SIZE_T_MAX - val) {
1182 kmp_task_t *__kmp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1183 kmp_tasking_flags_t *flags,
1184 size_t sizeof_kmp_task_t,
size_t sizeof_shareds,
1185 kmp_routine_entry_t task_entry) {
1187 kmp_taskdata_t *taskdata;
1188 kmp_info_t *thread = __kmp_threads[gtid];
1189 kmp_info_t *encountering_thread = thread;
1190 kmp_team_t *team = thread->th.th_team;
1191 kmp_taskdata_t *parent_task = thread->th.th_current_task;
1192 size_t shareds_offset;
1194 if (UNLIKELY(!TCR_4(__kmp_init_middle)))
1195 __kmp_middle_initialize();
1197 if (flags->hidden_helper) {
1198 if (__kmp_enable_hidden_helper) {
1199 if (!TCR_4(__kmp_init_hidden_helper))
1200 __kmp_hidden_helper_initialize();
1205 if (!KMP_HIDDEN_HELPER_THREAD(gtid)) {
1206 thread = __kmp_threads[KMP_GTID_TO_SHADOW_GTID(gtid)];
1212 flags->hidden_helper = FALSE;
1216 KA_TRACE(10, (
"__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) "
1217 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1218 gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t,
1219 sizeof_shareds, task_entry));
1221 if (parent_task->td_flags.final) {
1222 if (flags->merged_if0) {
1227 if (flags->tiedness == TASK_UNTIED && !team->t.t_serialized) {
1232 encountering_thread->th.th_task_team->tt.tt_untied_task_encountered, 1);
1238 if (flags->proxy == TASK_PROXY || flags->detachable == TASK_DETACHABLE ||
1239 flags->hidden_helper) {
1240 if (flags->proxy == TASK_PROXY) {
1241 flags->tiedness = TASK_UNTIED;
1242 flags->merged_if0 = 1;
1246 if ((encountering_thread->th.th_task_team) == NULL) {
1249 KMP_DEBUG_ASSERT(team->t.t_serialized);
1251 (
"T#%d creating task team in __kmp_task_alloc for proxy task\n",
1253 __kmp_task_team_setup(
1254 encountering_thread, team,
1256 encountering_thread->th.th_task_team =
1257 team->t.t_task_team[encountering_thread->th.th_task_state];
1259 kmp_task_team_t *task_team = encountering_thread->th.th_task_team;
1262 if (!KMP_TASKING_ENABLED(task_team)) {
1265 (
"T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid));
1266 __kmp_enable_tasking(task_team, encountering_thread);
1267 kmp_int32 tid = encountering_thread->th.th_info.ds.ds_tid;
1268 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
1270 if (thread_data->td.td_deque == NULL) {
1271 __kmp_alloc_task_deque(encountering_thread, thread_data);
1275 if (flags->proxy == TASK_PROXY &&
1276 task_team->tt.tt_found_proxy_tasks == FALSE)
1277 TCW_4(task_team->tt.tt_found_proxy_tasks, TRUE);
1278 if (flags->hidden_helper &&
1279 task_team->tt.tt_hidden_helper_task_encountered == FALSE)
1280 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, TRUE);
1285 shareds_offset =
sizeof(kmp_taskdata_t) + sizeof_kmp_task_t;
1286 shareds_offset = __kmp_round_up_to_val(shareds_offset,
sizeof(
void *));
1289 KA_TRACE(30, (
"__kmp_task_alloc: T#%d First malloc size: %ld\n", gtid,
1291 KA_TRACE(30, (
"__kmp_task_alloc: T#%d Second malloc size: %ld\n", gtid,
1296 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(
1297 encountering_thread, shareds_offset + sizeof_shareds);
1299 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(
1300 encountering_thread, shareds_offset + sizeof_shareds);
1302 ANNOTATE_HAPPENS_AFTER(taskdata);
1304 task = KMP_TASKDATA_TO_TASK(taskdata);
1307 #if KMP_ARCH_X86 || KMP_ARCH_PPC64 || !KMP_HAVE_QUAD
1308 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(
double) - 1)) == 0);
1309 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(
double) - 1)) == 0);
1311 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(_Quad) - 1)) == 0);
1312 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(_Quad) - 1)) == 0);
1314 if (sizeof_shareds > 0) {
1316 task->shareds = &((
char *)taskdata)[shareds_offset];
1318 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
1321 task->shareds = NULL;
1323 task->routine = task_entry;
1326 taskdata->td_task_id = KMP_GEN_TASK_ID();
1327 taskdata->td_team = thread->th.th_team;
1328 taskdata->td_alloc_thread = encountering_thread;
1329 taskdata->td_parent = parent_task;
1330 taskdata->td_level = parent_task->td_level + 1;
1331 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
1332 taskdata->td_ident = loc_ref;
1333 taskdata->td_taskwait_ident = NULL;
1334 taskdata->td_taskwait_counter = 0;
1335 taskdata->td_taskwait_thread = 0;
1336 KMP_DEBUG_ASSERT(taskdata->td_parent != NULL);
1338 if (flags->proxy == TASK_FULL)
1339 copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs);
1341 taskdata->td_flags.tiedness = flags->tiedness;
1342 taskdata->td_flags.final = flags->final;
1343 taskdata->td_flags.merged_if0 = flags->merged_if0;
1344 taskdata->td_flags.destructors_thunk = flags->destructors_thunk;
1345 taskdata->td_flags.proxy = flags->proxy;
1346 taskdata->td_flags.detachable = flags->detachable;
1347 taskdata->td_flags.hidden_helper = flags->hidden_helper;
1348 taskdata->encountering_gtid = gtid;
1349 taskdata->td_task_team = thread->th.th_task_team;
1350 taskdata->td_size_alloc = shareds_offset + sizeof_shareds;
1351 taskdata->td_flags.tasktype = TASK_EXPLICIT;
1354 taskdata->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1357 taskdata->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1363 taskdata->td_flags.task_serial =
1364 (parent_task->td_flags.final || taskdata->td_flags.team_serial ||
1365 taskdata->td_flags.tasking_ser || flags->merged_if0);
1367 taskdata->td_flags.started = 0;
1368 taskdata->td_flags.executing = 0;
1369 taskdata->td_flags.complete = 0;
1370 taskdata->td_flags.freed = 0;
1372 taskdata->td_flags.native = flags->native;
1374 KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0);
1376 KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1);
1377 taskdata->td_taskgroup =
1378 parent_task->td_taskgroup;
1379 taskdata->td_dephash = NULL;
1380 taskdata->td_depnode = NULL;
1381 if (flags->tiedness == TASK_UNTIED)
1382 taskdata->td_last_tied = NULL;
1384 taskdata->td_last_tied = taskdata;
1385 taskdata->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1387 if (UNLIKELY(ompt_enabled.enabled))
1388 __ompt_task_init(taskdata, gtid);
1392 if (flags->proxy == TASK_PROXY || flags->detachable == TASK_DETACHABLE ||
1393 flags->hidden_helper ||
1394 !(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
1395 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
1396 if (parent_task->td_taskgroup)
1397 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
1400 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) {
1401 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
1405 if (flags->hidden_helper) {
1406 taskdata->td_flags.task_serial = FALSE;
1408 KMP_ATOMIC_INC(&__kmp_unexecuted_hidden_helper_tasks);
1411 KA_TRACE(20, (
"__kmp_task_alloc(exit): T#%d created task %p parent=%p\n",
1412 gtid, taskdata, taskdata->td_parent));
1413 ANNOTATE_HAPPENS_BEFORE(task);
1418 kmp_task_t *__kmpc_omp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1419 kmp_int32 flags,
size_t sizeof_kmp_task_t,
1420 size_t sizeof_shareds,
1421 kmp_routine_entry_t task_entry) {
1423 kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
1424 __kmp_assert_valid_gtid(gtid);
1425 input_flags->native = FALSE;
1427 KA_TRACE(10, (
"__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s %s) "
1428 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1429 gtid, loc_ref, input_flags->tiedness ?
"tied " :
"untied",
1430 input_flags->proxy ?
"proxy" :
"",
1431 input_flags->detachable ?
"detachable" :
"", sizeof_kmp_task_t,
1432 sizeof_shareds, task_entry));
1434 retval = __kmp_task_alloc(loc_ref, gtid, input_flags, sizeof_kmp_task_t,
1435 sizeof_shareds, task_entry);
1437 KA_TRACE(20, (
"__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval));
1442 kmp_task_t *__kmpc_omp_target_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1444 size_t sizeof_kmp_task_t,
1445 size_t sizeof_shareds,
1446 kmp_routine_entry_t task_entry,
1447 kmp_int64 device_id) {
1448 if (__kmp_enable_hidden_helper) {
1449 auto &input_flags =
reinterpret_cast<kmp_tasking_flags_t &
>(flags);
1450 input_flags.hidden_helper = TRUE;
1453 return __kmpc_omp_task_alloc(loc_ref, gtid, flags, sizeof_kmp_task_t,
1454 sizeof_shareds, task_entry);
1472 kmp_task_t *new_task, kmp_int32 naffins,
1473 kmp_task_affinity_info_t *affin_list) {
1482 static void __kmp_invoke_task(kmp_int32 gtid, kmp_task_t *task,
1483 kmp_taskdata_t *current_task) {
1484 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
1488 30, (
"__kmp_invoke_task(enter): T#%d invoking task %p, current_task=%p\n",
1489 gtid, taskdata, current_task));
1490 KMP_DEBUG_ASSERT(task);
1491 if (UNLIKELY(taskdata->td_flags.proxy == TASK_PROXY &&
1492 taskdata->td_flags.complete == 1)) {
1497 (
"__kmp_invoke_task: T#%d running bottom finish for proxy task %p\n",
1500 __kmp_bottom_half_finish_proxy(gtid, task);
1502 KA_TRACE(30, (
"__kmp_invoke_task(exit): T#%d completed bottom finish for "
1503 "proxy task %p, resuming task %p\n",
1504 gtid, taskdata, current_task));
1512 ompt_thread_info_t oldInfo;
1513 if (UNLIKELY(ompt_enabled.enabled)) {
1515 thread = __kmp_threads[gtid];
1516 oldInfo = thread->th.ompt_thread_info;
1517 thread->th.ompt_thread_info.wait_id = 0;
1518 thread->th.ompt_thread_info.state = (thread->th.th_team_serialized)
1519 ? ompt_state_work_serial
1520 : ompt_state_work_parallel;
1521 taskdata->ompt_task_info.frame.exit_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1526 if (taskdata->td_flags.hidden_helper) {
1528 KMP_ASSERT(KMP_HIDDEN_HELPER_THREAD(gtid));
1529 KMP_ATOMIC_DEC(&__kmp_unexecuted_hidden_helper_tasks);
1533 if (taskdata->td_flags.proxy != TASK_PROXY) {
1534 ANNOTATE_HAPPENS_AFTER(task);
1535 __kmp_task_start(gtid, task, current_task);
1541 if (UNLIKELY(__kmp_omp_cancellation)) {
1542 thread = __kmp_threads[gtid];
1543 kmp_team_t *this_team = thread->th.th_team;
1544 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
1545 if ((taskgroup && taskgroup->cancel_request) ||
1546 (this_team->t.t_cancel_request == cancel_parallel)) {
1547 #if OMPT_SUPPORT && OMPT_OPTIONAL
1548 ompt_data_t *task_data;
1549 if (UNLIKELY(ompt_enabled.ompt_callback_cancel)) {
1550 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, NULL, NULL);
1551 ompt_callbacks.ompt_callback(ompt_callback_cancel)(
1553 ((taskgroup && taskgroup->cancel_request) ? ompt_cancel_taskgroup
1554 : ompt_cancel_parallel) |
1555 ompt_cancel_discarded_task,
1568 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
1569 taskdata->td_last_tied = current_task->td_last_tied;
1570 KMP_DEBUG_ASSERT(taskdata->td_last_tied);
1572 #if KMP_STATS_ENABLED
1574 switch (KMP_GET_THREAD_STATE()) {
1575 case FORK_JOIN_BARRIER:
1576 KMP_PUSH_PARTITIONED_TIMER(OMP_task_join_bar);
1579 KMP_PUSH_PARTITIONED_TIMER(OMP_task_plain_bar);
1582 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskyield);
1585 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskwait);
1588 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskgroup);
1591 KMP_PUSH_PARTITIONED_TIMER(OMP_task_immediate);
1598 if (UNLIKELY(ompt_enabled.enabled))
1599 __ompt_task_start(task, current_task, gtid);
1602 #if USE_ITT_BUILD && USE_ITT_NOTIFY
1603 kmp_uint64 cur_time;
1604 kmp_int32 kmp_itt_count_task =
1605 __kmp_forkjoin_frames_mode == 3 && !taskdata->td_flags.task_serial &&
1606 current_task->td_flags.tasktype == TASK_IMPLICIT;
1607 if (kmp_itt_count_task) {
1608 thread = __kmp_threads[gtid];
1610 if (thread->th.th_bar_arrive_time)
1611 cur_time = __itt_get_timestamp();
1613 kmp_itt_count_task = 0;
1615 KMP_FSYNC_ACQUIRED(taskdata);
1618 #ifdef KMP_GOMP_COMPAT
1619 if (taskdata->td_flags.native) {
1620 ((void (*)(
void *))(*(task->routine)))(task->shareds);
1624 (*(task->routine))(gtid, task);
1626 KMP_POP_PARTITIONED_TIMER();
1628 #if USE_ITT_BUILD && USE_ITT_NOTIFY
1629 if (kmp_itt_count_task) {
1631 thread->th.th_bar_arrive_time += (__itt_get_timestamp() - cur_time);
1633 KMP_FSYNC_CANCEL(taskdata);
1634 KMP_FSYNC_RELEASING(taskdata->td_parent);
1640 if (taskdata->td_flags.proxy != TASK_PROXY) {
1641 ANNOTATE_HAPPENS_BEFORE(taskdata->td_parent);
1643 if (UNLIKELY(ompt_enabled.enabled)) {
1644 thread->th.ompt_thread_info = oldInfo;
1645 if (taskdata->td_flags.tiedness == TASK_TIED) {
1646 taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
1648 __kmp_task_finish<true>(gtid, task, current_task);
1651 __kmp_task_finish<false>(gtid, task, current_task);
1656 (
"__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n",
1657 gtid, taskdata, current_task));
1671 kmp_int32 __kmpc_omp_task_parts(
ident_t *loc_ref, kmp_int32 gtid,
1672 kmp_task_t *new_task) {
1673 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1675 KA_TRACE(10, (
"__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n", gtid,
1676 loc_ref, new_taskdata));
1679 kmp_taskdata_t *parent;
1680 if (UNLIKELY(ompt_enabled.enabled)) {
1681 parent = new_taskdata->td_parent;
1682 if (ompt_enabled.ompt_callback_task_create) {
1683 ompt_data_t task_data = ompt_data_none;
1684 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1685 parent ? &(parent->ompt_task_info.task_data) : &task_data,
1686 parent ? &(parent->ompt_task_info.frame) : NULL,
1687 &(new_taskdata->ompt_task_info.task_data), ompt_task_explicit, 0,
1688 OMPT_GET_RETURN_ADDRESS(0));
1696 if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1698 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1699 new_taskdata->td_flags.task_serial = 1;
1700 __kmp_invoke_task(gtid, new_task, current_task);
1705 (
"__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: "
1706 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
1707 gtid, loc_ref, new_taskdata));
1709 ANNOTATE_HAPPENS_BEFORE(new_task);
1711 if (UNLIKELY(ompt_enabled.enabled)) {
1712 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1715 return TASK_CURRENT_NOT_QUEUED;
1729 kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task,
1730 bool serialize_immediate) {
1731 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1735 if (new_taskdata->td_flags.proxy == TASK_PROXY ||
1736 __kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1738 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1739 if (serialize_immediate)
1740 new_taskdata->td_flags.task_serial = 1;
1741 __kmp_invoke_task(gtid, new_task, current_task);
1744 ANNOTATE_HAPPENS_BEFORE(new_task);
1745 return TASK_CURRENT_NOT_QUEUED;
1760 kmp_int32 __kmpc_omp_task(
ident_t *loc_ref, kmp_int32 gtid,
1761 kmp_task_t *new_task) {
1763 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1765 #if KMP_DEBUG || OMPT_SUPPORT
1766 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1768 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1770 __kmp_assert_valid_gtid(gtid);
1773 kmp_taskdata_t *parent = NULL;
1774 if (UNLIKELY(ompt_enabled.enabled)) {
1775 if (!new_taskdata->td_flags.started) {
1776 OMPT_STORE_RETURN_ADDRESS(gtid);
1777 parent = new_taskdata->td_parent;
1778 if (!parent->ompt_task_info.frame.enter_frame.ptr) {
1779 parent->ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1781 if (ompt_enabled.ompt_callback_task_create) {
1782 ompt_data_t task_data = ompt_data_none;
1783 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1784 parent ? &(parent->ompt_task_info.task_data) : &task_data,
1785 parent ? &(parent->ompt_task_info.frame) : NULL,
1786 &(new_taskdata->ompt_task_info.task_data),
1787 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1788 OMPT_LOAD_RETURN_ADDRESS(gtid));
1793 __ompt_task_finish(new_task,
1794 new_taskdata->ompt_task_info.scheduling_parent,
1796 new_taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
1801 res = __kmp_omp_task(gtid, new_task,
true);
1803 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
1804 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1805 gtid, loc_ref, new_taskdata));
1807 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
1808 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1827 kmp_int32 __kmp_omp_taskloop_task(
ident_t *loc_ref, kmp_int32 gtid,
1828 kmp_task_t *new_task,
void *codeptr_ra) {
1830 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1832 #if KMP_DEBUG || OMPT_SUPPORT
1833 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1835 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1839 kmp_taskdata_t *parent = NULL;
1840 if (UNLIKELY(ompt_enabled.enabled && !new_taskdata->td_flags.started)) {
1841 parent = new_taskdata->td_parent;
1842 if (!parent->ompt_task_info.frame.enter_frame.ptr)
1843 parent->ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1844 if (ompt_enabled.ompt_callback_task_create) {
1845 ompt_data_t task_data = ompt_data_none;
1846 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1847 parent ? &(parent->ompt_task_info.task_data) : &task_data,
1848 parent ? &(parent->ompt_task_info.frame) : NULL,
1849 &(new_taskdata->ompt_task_info.task_data),
1850 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1856 res = __kmp_omp_task(gtid, new_task,
true);
1858 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
1859 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1860 gtid, loc_ref, new_taskdata));
1862 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
1863 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1869 template <
bool ompt>
1870 static kmp_int32 __kmpc_omp_taskwait_template(
ident_t *loc_ref, kmp_int32 gtid,
1871 void *frame_address,
1872 void *return_address) {
1873 kmp_taskdata_t *taskdata;
1875 int thread_finished = FALSE;
1876 KMP_SET_THREAD_STATE_BLOCK(TASKWAIT);
1878 KA_TRACE(10, (
"__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref));
1879 __kmp_assert_valid_gtid(gtid);
1881 if (__kmp_tasking_mode != tskm_immediate_exec) {
1882 thread = __kmp_threads[gtid];
1883 taskdata = thread->th.th_current_task;
1885 #if OMPT_SUPPORT && OMPT_OPTIONAL
1886 ompt_data_t *my_task_data;
1887 ompt_data_t *my_parallel_data;
1890 my_task_data = &(taskdata->ompt_task_info.task_data);
1891 my_parallel_data = OMPT_CUR_TEAM_DATA(thread);
1893 taskdata->ompt_task_info.frame.enter_frame.ptr = frame_address;
1895 if (ompt_enabled.ompt_callback_sync_region) {
1896 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
1897 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
1898 my_task_data, return_address);
1901 if (ompt_enabled.ompt_callback_sync_region_wait) {
1902 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
1903 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
1904 my_task_data, return_address);
1914 taskdata->td_taskwait_counter += 1;
1915 taskdata->td_taskwait_ident = loc_ref;
1916 taskdata->td_taskwait_thread = gtid + 1;
1919 void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
1920 if (UNLIKELY(itt_sync_obj != NULL))
1921 __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
1925 !taskdata->td_flags.team_serial && !taskdata->td_flags.final;
1927 must_wait = must_wait || (thread->th.th_task_team != NULL &&
1928 thread->th.th_task_team->tt.tt_found_proxy_tasks);
1932 (__kmp_enable_hidden_helper && thread->th.th_task_team != NULL &&
1933 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered);
1936 kmp_flag_32<false, false> flag(
1937 RCAST(std::atomic<kmp_uint32> *,
1938 &(taskdata->td_incomplete_child_tasks)),
1940 while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) != 0) {
1941 flag.execute_tasks(thread, gtid, FALSE,
1942 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
1943 __kmp_task_stealing_constraint);
1947 if (UNLIKELY(itt_sync_obj != NULL))
1948 __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
1949 KMP_FSYNC_ACQUIRED(taskdata);
1954 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
1956 #if OMPT_SUPPORT && OMPT_OPTIONAL
1958 if (ompt_enabled.ompt_callback_sync_region_wait) {
1959 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
1960 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
1961 my_task_data, return_address);
1963 if (ompt_enabled.ompt_callback_sync_region) {
1964 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
1965 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
1966 my_task_data, return_address);
1968 taskdata->ompt_task_info.frame.enter_frame = ompt_data_none;
1972 ANNOTATE_HAPPENS_AFTER(taskdata);
1975 KA_TRACE(10, (
"__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, "
1976 "returning TASK_CURRENT_NOT_QUEUED\n",
1979 return TASK_CURRENT_NOT_QUEUED;
1982 #if OMPT_SUPPORT && OMPT_OPTIONAL
1984 static kmp_int32 __kmpc_omp_taskwait_ompt(
ident_t *loc_ref, kmp_int32 gtid,
1985 void *frame_address,
1986 void *return_address) {
1987 return __kmpc_omp_taskwait_template<true>(loc_ref, gtid, frame_address,
1994 kmp_int32 __kmpc_omp_taskwait(
ident_t *loc_ref, kmp_int32 gtid) {
1995 #if OMPT_SUPPORT && OMPT_OPTIONAL
1996 if (UNLIKELY(ompt_enabled.enabled)) {
1997 OMPT_STORE_RETURN_ADDRESS(gtid);
1998 return __kmpc_omp_taskwait_ompt(loc_ref, gtid, OMPT_GET_FRAME_ADDRESS(0),
1999 OMPT_LOAD_RETURN_ADDRESS(gtid));
2002 return __kmpc_omp_taskwait_template<false>(loc_ref, gtid, NULL, NULL);
2006 kmp_int32 __kmpc_omp_taskyield(
ident_t *loc_ref, kmp_int32 gtid,
int end_part) {
2007 kmp_taskdata_t *taskdata;
2009 int thread_finished = FALSE;
2012 KMP_SET_THREAD_STATE_BLOCK(TASKYIELD);
2014 KA_TRACE(10, (
"__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n",
2015 gtid, loc_ref, end_part));
2016 __kmp_assert_valid_gtid(gtid);
2018 if (__kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel) {
2019 thread = __kmp_threads[gtid];
2020 taskdata = thread->th.th_current_task;
2027 taskdata->td_taskwait_counter += 1;
2028 taskdata->td_taskwait_ident = loc_ref;
2029 taskdata->td_taskwait_thread = gtid + 1;
2032 void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
2033 if (UNLIKELY(itt_sync_obj != NULL))
2034 __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
2036 if (!taskdata->td_flags.team_serial) {
2037 kmp_task_team_t *task_team = thread->th.th_task_team;
2038 if (task_team != NULL) {
2039 if (KMP_TASKING_ENABLED(task_team)) {
2041 if (UNLIKELY(ompt_enabled.enabled))
2042 thread->th.ompt_thread_info.ompt_task_yielded = 1;
2044 __kmp_execute_tasks_32(
2045 thread, gtid, (kmp_flag_32<> *)NULL, FALSE,
2046 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2047 __kmp_task_stealing_constraint);
2049 if (UNLIKELY(ompt_enabled.enabled))
2050 thread->th.ompt_thread_info.ompt_task_yielded = 0;
2056 if (UNLIKELY(itt_sync_obj != NULL))
2057 __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
2062 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2065 KA_TRACE(10, (
"__kmpc_omp_taskyield(exit): T#%d task %p resuming, "
2066 "returning TASK_CURRENT_NOT_QUEUED\n",
2069 return TASK_CURRENT_NOT_QUEUED;
2090 unsigned reserved31 : 31;
2170 template <
typename T>
2171 void *__kmp_task_reduction_init(
int gtid,
int num, T *data) {
2172 __kmp_assert_valid_gtid(gtid);
2173 kmp_info_t *thread = __kmp_threads[gtid];
2174 kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup;
2175 kmp_uint32 nth = thread->th.th_team_nproc;
2179 KMP_ASSERT(tg != NULL);
2180 KMP_ASSERT(data != NULL);
2181 KMP_ASSERT(num > 0);
2183 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n",
2187 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, taskgroup %p, #items %d\n",
2191 for (
int i = 0; i < num; ++i) {
2192 size_t size = data[i].reduce_size - 1;
2194 size += CACHE_LINE - size % CACHE_LINE;
2195 KMP_ASSERT(data[i].reduce_comb != NULL);
2198 arr[i].
flags = data[i].flags;
2202 __kmp_assign_orig<T>(arr[i], data[i]);
2203 if (!arr[i].flags.lazy_priv) {
2206 arr[i].
reduce_pend = (
char *)(arr[i].reduce_priv) + nth * size;
2207 if (arr[i].reduce_init != NULL) {
2209 for (
size_t j = 0; j < nth; ++j) {
2210 __kmp_call_init<T>(arr[i], j * size);
2217 arr[i].
reduce_priv = __kmp_allocate(nth *
sizeof(
void *));
2220 tg->reduce_data = (
void *)arr;
2221 tg->reduce_num_data = num;
2260 template <
typename T>
2261 void __kmp_task_reduction_init_copy(kmp_info_t *thr,
int num, T *data,
2262 kmp_taskgroup_t *tg,
void *reduce_data) {
2264 KA_TRACE(20, (
"__kmp_task_reduction_init_copy: Th %p, init taskgroup %p,"
2266 thr, tg, reduce_data));
2271 for (
int i = 0; i < num; ++i) {
2274 tg->reduce_data = (
void *)arr;
2275 tg->reduce_num_data = num;
2288 __kmp_assert_valid_gtid(gtid);
2289 kmp_info_t *thread = __kmp_threads[gtid];
2290 kmp_int32 nth = thread->th.th_team_nproc;
2294 kmp_taskgroup_t *tg = (kmp_taskgroup_t *)tskgrp;
2296 tg = thread->th.th_current_task->td_taskgroup;
2297 KMP_ASSERT(tg != NULL);
2299 kmp_int32 num = tg->reduce_num_data;
2300 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
2302 KMP_ASSERT(data != NULL);
2303 while (tg != NULL) {
2304 for (
int i = 0; i < num; ++i) {
2305 if (!arr[i].flags.lazy_priv) {
2306 if (data == arr[i].reduce_shar ||
2307 (data >= arr[i].reduce_priv && data < arr[i].reduce_pend))
2308 return (
char *)(arr[i].
reduce_priv) + tid * arr[i].reduce_size;
2311 void **p_priv = (
void **)(arr[i].reduce_priv);
2312 if (data == arr[i].reduce_shar)
2315 for (
int j = 0; j < nth; ++j)
2316 if (data == p_priv[j])
2320 if (p_priv[tid] == NULL) {
2322 p_priv[tid] = __kmp_allocate(arr[i].reduce_size);
2323 if (arr[i].reduce_init != NULL) {
2324 if (arr[i].reduce_orig != NULL) {
2326 p_priv[tid], arr[i].reduce_orig);
2328 ((void (*)(
void *))arr[i].
reduce_init)(p_priv[tid]);
2337 num = tg->reduce_num_data;
2339 KMP_ASSERT2(0,
"Unknown task reduction item");
2345 static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg) {
2346 kmp_int32 nth = th->th.th_team_nproc;
2347 KMP_DEBUG_ASSERT(nth > 1);
2349 kmp_int32 num = tg->reduce_num_data;
2350 for (
int i = 0; i < num; ++i) {
2352 void (*f_fini)(
void *) = (
void (*)(
void *))(arr[i].
reduce_fini);
2353 void (*f_comb)(
void *,
void *) =
2355 if (!arr[i].flags.lazy_priv) {
2358 for (
int j = 0; j < nth; ++j) {
2359 void *priv_data = (
char *)pr_data + j * size;
2360 f_comb(sh_data, priv_data);
2365 void **pr_data = (
void **)(arr[i].reduce_priv);
2366 for (
int j = 0; j < nth; ++j) {
2367 if (pr_data[j] != NULL) {
2368 f_comb(sh_data, pr_data[j]);
2371 __kmp_free(pr_data[j]);
2375 __kmp_free(arr[i].reduce_priv);
2377 __kmp_thread_free(th, arr);
2378 tg->reduce_data = NULL;
2379 tg->reduce_num_data = 0;
2385 static void __kmp_task_reduction_clean(kmp_info_t *th, kmp_taskgroup_t *tg) {
2386 __kmp_thread_free(th, tg->reduce_data);
2387 tg->reduce_data = NULL;
2388 tg->reduce_num_data = 0;
2391 template <
typename T>
2392 void *__kmp_task_reduction_modifier_init(
ident_t *loc,
int gtid,
int is_ws,
2394 __kmp_assert_valid_gtid(gtid);
2395 kmp_info_t *thr = __kmp_threads[gtid];
2396 kmp_int32 nth = thr->th.th_team_nproc;
2397 __kmpc_taskgroup(loc, gtid);
2400 (
"__kmpc_reduction_modifier_init: T#%d, tg %p, exiting nth=1\n",
2401 gtid, thr->th.th_current_task->td_taskgroup));
2402 return (
void *)thr->th.th_current_task->td_taskgroup;
2404 kmp_team_t *team = thr->th.th_team;
2406 kmp_taskgroup_t *tg;
2407 reduce_data = KMP_ATOMIC_LD_RLX(&team->t.t_tg_reduce_data[is_ws]);
2408 if (reduce_data == NULL &&
2409 __kmp_atomic_compare_store(&team->t.t_tg_reduce_data[is_ws], reduce_data,
2412 KMP_DEBUG_ASSERT(reduce_data == NULL);
2414 tg = (kmp_taskgroup_t *)__kmp_task_reduction_init<T>(gtid, num, data);
2418 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[0]) == 0);
2419 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[1]) == 0);
2420 KMP_ATOMIC_ST_REL(&team->t.t_tg_reduce_data[is_ws], reduce_data);
2423 (reduce_data = KMP_ATOMIC_LD_ACQ(&team->t.t_tg_reduce_data[is_ws])) ==
2427 KMP_DEBUG_ASSERT(reduce_data > (
void *)1);
2428 tg = thr->th.th_current_task->td_taskgroup;
2429 __kmp_task_reduction_init_copy<T>(thr, num, data, tg, reduce_data);
2451 int num,
void *data) {
2452 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2472 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2485 __kmpc_end_taskgroup(loc, gtid);
2489 void __kmpc_taskgroup(
ident_t *loc,
int gtid) {
2490 __kmp_assert_valid_gtid(gtid);
2491 kmp_info_t *thread = __kmp_threads[gtid];
2492 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2493 kmp_taskgroup_t *tg_new =
2494 (kmp_taskgroup_t *)__kmp_thread_malloc(thread,
sizeof(kmp_taskgroup_t));
2495 KA_TRACE(10, (
"__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new));
2496 KMP_ATOMIC_ST_RLX(&tg_new->count, 0);
2497 KMP_ATOMIC_ST_RLX(&tg_new->cancel_request, cancel_noreq);
2498 tg_new->parent = taskdata->td_taskgroup;
2499 tg_new->reduce_data = NULL;
2500 tg_new->reduce_num_data = 0;
2501 taskdata->td_taskgroup = tg_new;
2503 #if OMPT_SUPPORT && OMPT_OPTIONAL
2504 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2505 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2507 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2508 kmp_team_t *team = thread->th.th_team;
2509 ompt_data_t my_task_data = taskdata->ompt_task_info.task_data;
2511 ompt_data_t my_parallel_data = team->t.ompt_team_info.parallel_data;
2513 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2514 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2515 &(my_task_data), codeptr);
2522 void __kmpc_end_taskgroup(
ident_t *loc,
int gtid) {
2523 __kmp_assert_valid_gtid(gtid);
2524 kmp_info_t *thread = __kmp_threads[gtid];
2525 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2526 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
2527 int thread_finished = FALSE;
2529 #if OMPT_SUPPORT && OMPT_OPTIONAL
2531 ompt_data_t my_task_data;
2532 ompt_data_t my_parallel_data;
2534 if (UNLIKELY(ompt_enabled.enabled)) {
2535 team = thread->th.th_team;
2536 my_task_data = taskdata->ompt_task_info.task_data;
2538 my_parallel_data = team->t.ompt_team_info.parallel_data;
2539 codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2541 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2545 KA_TRACE(10, (
"__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc));
2546 KMP_DEBUG_ASSERT(taskgroup != NULL);
2547 KMP_SET_THREAD_STATE_BLOCK(TASKGROUP);
2549 if (__kmp_tasking_mode != tskm_immediate_exec) {
2551 taskdata->td_taskwait_counter += 1;
2552 taskdata->td_taskwait_ident = loc;
2553 taskdata->td_taskwait_thread = gtid + 1;
2557 void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
2558 if (UNLIKELY(itt_sync_obj != NULL))
2559 __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
2562 #if OMPT_SUPPORT && OMPT_OPTIONAL
2563 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2564 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2565 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2566 &(my_task_data), codeptr);
2570 if (!taskdata->td_flags.team_serial ||
2571 (thread->th.th_task_team != NULL &&
2572 thread->th.th_task_team->tt.tt_found_proxy_tasks)) {
2573 kmp_flag_32<false, false> flag(
2574 RCAST(std::atomic<kmp_uint32> *, &(taskgroup->count)), 0U);
2575 while (KMP_ATOMIC_LD_ACQ(&taskgroup->count) != 0) {
2576 flag.execute_tasks(thread, gtid, FALSE,
2577 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2578 __kmp_task_stealing_constraint);
2581 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2583 #if OMPT_SUPPORT && OMPT_OPTIONAL
2584 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2585 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2586 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2587 &(my_task_data), codeptr);
2592 if (UNLIKELY(itt_sync_obj != NULL))
2593 __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
2594 KMP_FSYNC_ACQUIRED(taskdata);
2597 KMP_DEBUG_ASSERT(taskgroup->count == 0);
2599 if (taskgroup->reduce_data != NULL) {
2602 kmp_team_t *t = thread->th.th_team;
2606 if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[0])) != NULL &&
2609 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[0]);
2610 if (cnt == thread->th.th_team_nproc - 1) {
2613 __kmp_task_reduction_fini(thread, taskgroup);
2616 __kmp_thread_free(thread, reduce_data);
2617 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[0], NULL);
2618 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[0], 0);
2622 __kmp_task_reduction_clean(thread, taskgroup);
2624 }
else if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[1])) !=
2628 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[1]);
2629 if (cnt == thread->th.th_team_nproc - 1) {
2631 __kmp_task_reduction_fini(thread, taskgroup);
2634 __kmp_thread_free(thread, reduce_data);
2635 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[1], NULL);
2636 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[1], 0);
2640 __kmp_task_reduction_clean(thread, taskgroup);
2644 __kmp_task_reduction_fini(thread, taskgroup);
2648 taskdata->td_taskgroup = taskgroup->parent;
2649 __kmp_thread_free(thread, taskgroup);
2651 KA_TRACE(10, (
"__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n",
2653 ANNOTATE_HAPPENS_AFTER(taskdata);
2655 #if OMPT_SUPPORT && OMPT_OPTIONAL
2656 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2657 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2658 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2659 &(my_task_data), codeptr);
2665 static kmp_task_t *__kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid,
2666 kmp_task_team_t *task_team,
2667 kmp_int32 is_constrained) {
2669 kmp_taskdata_t *taskdata;
2670 kmp_thread_data_t *thread_data;
2673 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2674 KMP_DEBUG_ASSERT(task_team->tt.tt_threads_data !=
2677 thread_data = &task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
2679 KA_TRACE(10, (
"__kmp_remove_my_task(enter): T#%d ntasks=%d head=%u tail=%u\n",
2680 gtid, thread_data->td.td_deque_ntasks,
2681 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2683 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2685 (
"__kmp_remove_my_task(exit #1): T#%d No tasks to remove: "
2686 "ntasks=%d head=%u tail=%u\n",
2687 gtid, thread_data->td.td_deque_ntasks,
2688 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2692 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
2694 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2695 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2697 (
"__kmp_remove_my_task(exit #2): T#%d No tasks to remove: "
2698 "ntasks=%d head=%u tail=%u\n",
2699 gtid, thread_data->td.td_deque_ntasks,
2700 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2704 tail = (thread_data->td.td_deque_tail - 1) &
2705 TASK_DEQUE_MASK(thread_data->td);
2706 taskdata = thread_data->td.td_deque[tail];
2708 if (!__kmp_task_is_allowed(gtid, is_constrained, taskdata,
2709 thread->th.th_current_task)) {
2711 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2713 (
"__kmp_remove_my_task(exit #3): T#%d TSC blocks tail task: "
2714 "ntasks=%d head=%u tail=%u\n",
2715 gtid, thread_data->td.td_deque_ntasks,
2716 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2720 thread_data->td.td_deque_tail = tail;
2721 TCW_4(thread_data->td.td_deque_ntasks, thread_data->td.td_deque_ntasks - 1);
2723 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2725 KA_TRACE(10, (
"__kmp_remove_my_task(exit #4): T#%d task %p removed: "
2726 "ntasks=%d head=%u tail=%u\n",
2727 gtid, taskdata, thread_data->td.td_deque_ntasks,
2728 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2730 task = KMP_TASKDATA_TO_TASK(taskdata);
2737 static kmp_task_t *__kmp_steal_task(kmp_info_t *victim_thr, kmp_int32 gtid,
2738 kmp_task_team_t *task_team,
2739 std::atomic<kmp_int32> *unfinished_threads,
2740 int *thread_finished,
2741 kmp_int32 is_constrained) {
2743 kmp_taskdata_t *taskdata;
2744 kmp_taskdata_t *current;
2745 kmp_thread_data_t *victim_td, *threads_data;
2747 kmp_int32 victim_tid;
2749 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2751 threads_data = task_team->tt.tt_threads_data;
2752 KMP_DEBUG_ASSERT(threads_data != NULL);
2754 victim_tid = victim_thr->th.th_info.ds.ds_tid;
2755 victim_td = &threads_data[victim_tid];
2757 KA_TRACE(10, (
"__kmp_steal_task(enter): T#%d try to steal from T#%d: "
2758 "task_team=%p ntasks=%d head=%u tail=%u\n",
2759 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
2760 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
2761 victim_td->td.td_deque_tail));
2763 if (TCR_4(victim_td->td.td_deque_ntasks) == 0) {
2764 KA_TRACE(10, (
"__kmp_steal_task(exit #1): T#%d could not steal from T#%d: "
2765 "task_team=%p ntasks=%d head=%u tail=%u\n",
2766 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
2767 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
2768 victim_td->td.td_deque_tail));
2772 __kmp_acquire_bootstrap_lock(&victim_td->td.td_deque_lock);
2774 int ntasks = TCR_4(victim_td->td.td_deque_ntasks);
2777 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2778 KA_TRACE(10, (
"__kmp_steal_task(exit #2): T#%d could not steal from T#%d: "
2779 "task_team=%p ntasks=%d head=%u tail=%u\n",
2780 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2781 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2785 KMP_DEBUG_ASSERT(victim_td->td.td_deque != NULL);
2786 current = __kmp_threads[gtid]->th.th_current_task;
2787 taskdata = victim_td->td.td_deque[victim_td->td.td_deque_head];
2788 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
2790 victim_td->td.td_deque_head =
2791 (victim_td->td.td_deque_head + 1) & TASK_DEQUE_MASK(victim_td->td);
2793 if (!task_team->tt.tt_untied_task_encountered) {
2795 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2796 KA_TRACE(10, (
"__kmp_steal_task(exit #3): T#%d could not steal from "
2797 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
2798 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2799 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2804 target = victim_td->td.td_deque_head;
2806 for (i = 1; i < ntasks; ++i) {
2807 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
2808 taskdata = victim_td->td.td_deque[target];
2809 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
2815 if (taskdata == NULL) {
2817 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2818 KA_TRACE(10, (
"__kmp_steal_task(exit #4): T#%d could not steal from "
2819 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
2820 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2821 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2825 for (i = i + 1; i < ntasks; ++i) {
2827 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
2828 victim_td->td.td_deque[prev] = victim_td->td.td_deque[target];
2832 victim_td->td.td_deque_tail ==
2833 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(victim_td->td)));
2834 victim_td->td.td_deque_tail = target;
2836 if (*thread_finished) {
2842 count = KMP_ATOMIC_INC(unfinished_threads);
2846 (
"__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n",
2847 gtid, count + 1, task_team));
2849 *thread_finished = FALSE;
2851 TCW_4(victim_td->td.td_deque_ntasks, ntasks - 1);
2853 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2857 (
"__kmp_steal_task(exit #5): T#%d stole task %p from T#%d: "
2858 "task_team=%p ntasks=%d head=%u tail=%u\n",
2859 gtid, taskdata, __kmp_gtid_from_thread(victim_thr), task_team,
2860 ntasks, victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2862 task = KMP_TASKDATA_TO_TASK(taskdata);
2876 static inline int __kmp_execute_tasks_template(
2877 kmp_info_t *thread, kmp_int32 gtid, C *flag,
int final_spin,
2878 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
2879 kmp_int32 is_constrained) {
2880 kmp_task_team_t *task_team = thread->th.th_task_team;
2881 kmp_thread_data_t *threads_data;
2883 kmp_info_t *other_thread;
2884 kmp_taskdata_t *current_task = thread->th.th_current_task;
2885 std::atomic<kmp_int32> *unfinished_threads;
2886 kmp_int32 nthreads, victim_tid = -2, use_own_tasks = 1, new_victim = 0,
2887 tid = thread->th.th_info.ds.ds_tid;
2889 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2890 KMP_DEBUG_ASSERT(thread == __kmp_threads[gtid]);
2892 if (task_team == NULL || current_task == NULL)
2895 KA_TRACE(15, (
"__kmp_execute_tasks_template(enter): T#%d final_spin=%d "
2896 "*thread_finished=%d\n",
2897 gtid, final_spin, *thread_finished));
2899 thread->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
2900 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
2902 KMP_DEBUG_ASSERT(threads_data != NULL);
2904 nthreads = task_team->tt.tt_nproc;
2905 unfinished_threads = &(task_team->tt.tt_unfinished_threads);
2906 KMP_DEBUG_ASSERT(nthreads > 1 || task_team->tt.tt_found_proxy_tasks ||
2907 task_team->tt.tt_hidden_helper_task_encountered);
2908 KMP_DEBUG_ASSERT(*unfinished_threads >= 0);
2914 if (use_own_tasks) {
2915 task = __kmp_remove_my_task(thread, gtid, task_team, is_constrained);
2917 if ((task == NULL) && (nthreads > 1)) {
2921 if (victim_tid == -2) {
2922 victim_tid = threads_data[tid].td.td_deque_last_stolen;
2925 other_thread = threads_data[victim_tid].td.td_thr;
2927 if (victim_tid != -1) {
2929 }
else if (!new_victim) {
2935 victim_tid = __kmp_get_random(thread) % (nthreads - 1);
2936 if (victim_tid >= tid) {
2940 other_thread = threads_data[victim_tid].td.td_thr;
2950 if ((__kmp_tasking_mode == tskm_task_teams) &&
2951 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) &&
2952 (TCR_PTR(CCAST(
void *, other_thread->th.th_sleep_loc)) !=
2955 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(other_thread),
2956 other_thread->th.th_sleep_loc);
2969 task = __kmp_steal_task(other_thread, gtid, task_team,
2970 unfinished_threads, thread_finished,
2974 if (threads_data[tid].td.td_deque_last_stolen != victim_tid) {
2975 threads_data[tid].td.td_deque_last_stolen = victim_tid;
2982 KMP_CHECK_UPDATE(threads_data[tid].td.td_deque_last_stolen, -1);
2991 #if USE_ITT_BUILD && USE_ITT_NOTIFY
2992 if (__itt_sync_create_ptr || KMP_ITT_DEBUG) {
2993 if (itt_sync_obj == NULL) {
2995 itt_sync_obj = __kmp_itt_barrier_object(gtid, bs_forkjoin_barrier);
2997 __kmp_itt_task_starting(itt_sync_obj);
3000 __kmp_invoke_task(gtid, task, current_task);
3002 if (itt_sync_obj != NULL)
3003 __kmp_itt_task_finished(itt_sync_obj);
3010 if (flag == NULL || (!final_spin && flag->done_check())) {
3013 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3017 if (thread->th.th_task_team == NULL) {
3020 KMP_YIELD(__kmp_library == library_throughput);
3023 if (!use_own_tasks && TCR_4(threads_data[tid].td.td_deque_ntasks) != 0) {
3024 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d stolen task spawned "
3025 "other tasks, restart\n",
3036 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks) == 0) {
3040 if (!*thread_finished) {
3043 count = KMP_ATOMIC_DEC(unfinished_threads) - 1;
3044 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d dec "
3045 "unfinished_threads to %d task_team=%p\n",
3046 gtid, count, task_team));
3047 *thread_finished = TRUE;
3055 if (flag != NULL && flag->done_check()) {
3058 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3066 if (thread->th.th_task_team == NULL) {
3068 (
"__kmp_execute_tasks_template: T#%d no more tasks\n", gtid));
3074 if (nthreads == 1 &&
3075 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks))
3079 (
"__kmp_execute_tasks_template: T#%d can't find work\n", gtid));
3085 template <
bool C,
bool S>
3086 int __kmp_execute_tasks_32(
3087 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32<C, S> *flag,
int final_spin,
3088 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3089 kmp_int32 is_constrained) {
3090 return __kmp_execute_tasks_template(
3091 thread, gtid, flag, final_spin,
3092 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3095 template <
bool C,
bool S>
3096 int __kmp_execute_tasks_64(
3097 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64<C, S> *flag,
int final_spin,
3098 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3099 kmp_int32 is_constrained) {
3100 return __kmp_execute_tasks_template(
3101 thread, gtid, flag, final_spin,
3102 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3105 int __kmp_execute_tasks_oncore(
3106 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag,
int final_spin,
3107 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3108 kmp_int32 is_constrained) {
3109 return __kmp_execute_tasks_template(
3110 thread, gtid, flag, final_spin,
3111 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3115 __kmp_execute_tasks_32<false, false>(kmp_info_t *, kmp_int32,
3116 kmp_flag_32<false, false> *,
int,
3117 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3119 template int __kmp_execute_tasks_64<false, true>(kmp_info_t *, kmp_int32,
3120 kmp_flag_64<false, true> *,
3122 int *USE_ITT_BUILD_ARG(
void *),
3125 template int __kmp_execute_tasks_64<true, false>(kmp_info_t *, kmp_int32,
3126 kmp_flag_64<true, false> *,
3128 int *USE_ITT_BUILD_ARG(
void *),
3134 static void __kmp_enable_tasking(kmp_task_team_t *task_team,
3135 kmp_info_t *this_thr) {
3136 kmp_thread_data_t *threads_data;
3137 int nthreads, i, is_init_thread;
3139 KA_TRACE(10, (
"__kmp_enable_tasking(enter): T#%d\n",
3140 __kmp_gtid_from_thread(this_thr)));
3142 KMP_DEBUG_ASSERT(task_team != NULL);
3143 KMP_DEBUG_ASSERT(this_thr->th.th_team != NULL);
3145 nthreads = task_team->tt.tt_nproc;
3146 KMP_DEBUG_ASSERT(nthreads > 0);
3147 KMP_DEBUG_ASSERT(nthreads == this_thr->th.th_team->t.t_nproc);
3150 is_init_thread = __kmp_realloc_task_threads_data(this_thr, task_team);
3152 if (!is_init_thread) {
3156 (
"__kmp_enable_tasking(exit): T#%d: threads array already set up.\n",
3157 __kmp_gtid_from_thread(this_thr)));
3160 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
3161 KMP_DEBUG_ASSERT(threads_data != NULL);
3163 if (__kmp_tasking_mode == tskm_task_teams &&
3164 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME)) {
3168 for (i = 0; i < nthreads; i++) {
3169 volatile void *sleep_loc;
3170 kmp_info_t *thread = threads_data[i].td.td_thr;
3172 if (i == this_thr->th.th_info.ds.ds_tid) {
3181 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3183 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d waking up thread T#%d\n",
3184 __kmp_gtid_from_thread(this_thr),
3185 __kmp_gtid_from_thread(thread)));
3186 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc);
3188 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d don't wake up thread T#%d\n",
3189 __kmp_gtid_from_thread(this_thr),
3190 __kmp_gtid_from_thread(thread)));
3195 KA_TRACE(10, (
"__kmp_enable_tasking(exit): T#%d\n",
3196 __kmp_gtid_from_thread(this_thr)));
3229 static kmp_task_team_t *__kmp_free_task_teams =
3232 kmp_bootstrap_lock_t __kmp_task_team_lock =
3233 KMP_BOOTSTRAP_LOCK_INITIALIZER(__kmp_task_team_lock);
3240 static void __kmp_alloc_task_deque(kmp_info_t *thread,
3241 kmp_thread_data_t *thread_data) {
3242 __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
3243 KMP_DEBUG_ASSERT(thread_data->td.td_deque == NULL);
3246 thread_data->td.td_deque_last_stolen = -1;
3248 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == 0);
3249 KMP_DEBUG_ASSERT(thread_data->td.td_deque_head == 0);
3250 KMP_DEBUG_ASSERT(thread_data->td.td_deque_tail == 0);
3254 (
"__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n",
3255 __kmp_gtid_from_thread(thread), INITIAL_TASK_DEQUE_SIZE, thread_data));
3259 thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
3260 INITIAL_TASK_DEQUE_SIZE *
sizeof(kmp_taskdata_t *));
3261 thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
3267 static void __kmp_free_task_deque(kmp_thread_data_t *thread_data) {
3268 if (thread_data->td.td_deque != NULL) {
3269 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3270 TCW_4(thread_data->td.td_deque_ntasks, 0);
3271 __kmp_free(thread_data->td.td_deque);
3272 thread_data->td.td_deque = NULL;
3273 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3276 #ifdef BUILD_TIED_TASK_STACK
3278 if (thread_data->td.td_susp_tied_tasks.ts_entries != TASK_STACK_EMPTY) {
3279 __kmp_free_task_stack(__kmp_thread_from_gtid(gtid), thread_data);
3291 static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
3292 kmp_task_team_t *task_team) {
3293 kmp_thread_data_t **threads_data_p;
3294 kmp_int32 nthreads, maxthreads;
3295 int is_init_thread = FALSE;
3297 if (TCR_4(task_team->tt.tt_found_tasks)) {
3302 threads_data_p = &task_team->tt.tt_threads_data;
3303 nthreads = task_team->tt.tt_nproc;
3304 maxthreads = task_team->tt.tt_max_threads;
3309 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3311 if (!TCR_4(task_team->tt.tt_found_tasks)) {
3313 kmp_team_t *team = thread->th.th_team;
3316 is_init_thread = TRUE;
3317 if (maxthreads < nthreads) {
3319 if (*threads_data_p != NULL) {
3320 kmp_thread_data_t *old_data = *threads_data_p;
3321 kmp_thread_data_t *new_data = NULL;
3325 (
"__kmp_realloc_task_threads_data: T#%d reallocating "
3326 "threads data for task_team %p, new_size = %d, old_size = %d\n",
3327 __kmp_gtid_from_thread(thread), task_team, nthreads, maxthreads));
3332 new_data = (kmp_thread_data_t *)__kmp_allocate(
3333 nthreads *
sizeof(kmp_thread_data_t));
3335 KMP_MEMCPY_S((
void *)new_data, nthreads *
sizeof(kmp_thread_data_t),
3336 (
void *)old_data, maxthreads *
sizeof(kmp_thread_data_t));
3338 #ifdef BUILD_TIED_TASK_STACK
3340 for (i = maxthreads; i < nthreads; i++) {
3341 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3342 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3346 (*threads_data_p) = new_data;
3347 __kmp_free(old_data);
3349 KE_TRACE(10, (
"__kmp_realloc_task_threads_data: T#%d allocating "
3350 "threads data for task_team %p, size = %d\n",
3351 __kmp_gtid_from_thread(thread), task_team, nthreads));
3355 ANNOTATE_IGNORE_WRITES_BEGIN();
3356 *threads_data_p = (kmp_thread_data_t *)__kmp_allocate(
3357 nthreads *
sizeof(kmp_thread_data_t));
3358 ANNOTATE_IGNORE_WRITES_END();
3359 #ifdef BUILD_TIED_TASK_STACK
3361 for (i = 0; i < nthreads; i++) {
3362 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3363 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3367 task_team->tt.tt_max_threads = nthreads;
3370 KMP_DEBUG_ASSERT(*threads_data_p != NULL);
3374 for (i = 0; i < nthreads; i++) {
3375 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3376 thread_data->td.td_thr = team->t.t_threads[i];
3378 if (thread_data->td.td_deque_last_stolen >= nthreads) {
3382 thread_data->td.td_deque_last_stolen = -1;
3387 TCW_SYNC_4(task_team->tt.tt_found_tasks, TRUE);
3390 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3391 return is_init_thread;
3397 static void __kmp_free_task_threads_data(kmp_task_team_t *task_team) {
3398 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3399 if (task_team->tt.tt_threads_data != NULL) {
3401 for (i = 0; i < task_team->tt.tt_max_threads; i++) {
3402 __kmp_free_task_deque(&task_team->tt.tt_threads_data[i]);
3404 __kmp_free(task_team->tt.tt_threads_data);
3405 task_team->tt.tt_threads_data = NULL;
3407 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3414 static kmp_task_team_t *__kmp_allocate_task_team(kmp_info_t *thread,
3416 kmp_task_team_t *task_team = NULL;
3419 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d entering; team = %p\n",
3420 (thread ? __kmp_gtid_from_thread(thread) : -1), team));
3422 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3424 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3425 if (__kmp_free_task_teams != NULL) {
3426 task_team = __kmp_free_task_teams;
3427 TCW_PTR(__kmp_free_task_teams, task_team->tt.tt_next);
3428 task_team->tt.tt_next = NULL;
3430 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3433 if (task_team == NULL) {
3434 KE_TRACE(10, (
"__kmp_allocate_task_team: T#%d allocating "
3435 "task team for team %p\n",
3436 __kmp_gtid_from_thread(thread), team));
3439 task_team = (kmp_task_team_t *)__kmp_allocate(
sizeof(kmp_task_team_t));
3440 __kmp_init_bootstrap_lock(&task_team->tt.tt_threads_lock);
3441 #if USE_ITT_BUILD && USE_ITT_NOTIFY && KMP_DEBUG
3444 __itt_suppress_mark_range(
3445 __itt_suppress_range, __itt_suppress_threading_errors,
3446 &task_team->tt.tt_found_tasks,
sizeof(task_team->tt.tt_found_tasks));
3447 __itt_suppress_mark_range(__itt_suppress_range,
3448 __itt_suppress_threading_errors,
3449 CCAST(kmp_uint32 *, &task_team->tt.tt_active),
3450 sizeof(task_team->tt.tt_active));
3458 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3459 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3460 task_team->tt.tt_nproc = nthreads = team->t.t_nproc;
3462 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads, nthreads);
3463 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
3464 TCW_4(task_team->tt.tt_active, TRUE);
3466 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d exiting; task_team = %p "
3467 "unfinished_threads init'd to %d\n",
3468 (thread ? __kmp_gtid_from_thread(thread) : -1), task_team,
3469 KMP_ATOMIC_LD_RLX(&task_team->tt.tt_unfinished_threads)));
3476 void __kmp_free_task_team(kmp_info_t *thread, kmp_task_team_t *task_team) {
3477 KA_TRACE(20, (
"__kmp_free_task_team: T#%d task_team = %p\n",
3478 thread ? __kmp_gtid_from_thread(thread) : -1, task_team));
3481 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3483 KMP_DEBUG_ASSERT(task_team->tt.tt_next == NULL);
3484 task_team->tt.tt_next = __kmp_free_task_teams;
3485 TCW_PTR(__kmp_free_task_teams, task_team);
3487 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3495 void __kmp_reap_task_teams(
void) {
3496 kmp_task_team_t *task_team;
3498 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3500 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3501 while ((task_team = __kmp_free_task_teams) != NULL) {
3502 __kmp_free_task_teams = task_team->tt.tt_next;
3503 task_team->tt.tt_next = NULL;
3506 if (task_team->tt.tt_threads_data != NULL) {
3507 __kmp_free_task_threads_data(task_team);
3509 __kmp_free(task_team);
3511 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3518 void __kmp_wait_to_unref_task_teams(
void) {
3523 KMP_INIT_YIELD(spins);
3531 for (thread = CCAST(kmp_info_t *, __kmp_thread_pool); thread != NULL;
3532 thread = thread->th.th_next_pool) {
3536 if (TCR_PTR(thread->th.th_task_team) == NULL) {
3537 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n",
3538 __kmp_gtid_from_thread(thread)));
3543 if (!__kmp_is_thread_alive(thread, &exit_val)) {
3544 thread->th.th_task_team = NULL;
3551 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: Waiting for T#%d to "
3552 "unreference task_team\n",
3553 __kmp_gtid_from_thread(thread)));
3555 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
3556 volatile void *sleep_loc;
3558 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3562 (
"__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n",
3563 __kmp_gtid_from_thread(thread), __kmp_gtid_from_thread(thread)));
3564 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc);
3573 KMP_YIELD_OVERSUB_ELSE_SPIN(spins);
3579 void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team,
int always) {
3580 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3586 if (team->t.t_task_team[this_thr->th.th_task_state] == NULL &&
3587 (always || team->t.t_nproc > 1)) {
3588 team->t.t_task_team[this_thr->th.th_task_state] =
3589 __kmp_allocate_task_team(this_thr, team);
3590 KA_TRACE(20, (
"__kmp_task_team_setup: Master T#%d created new task_team %p "
3591 "for team %d at parity=%d\n",
3592 __kmp_gtid_from_thread(this_thr),
3593 team->t.t_task_team[this_thr->th.th_task_state],
3594 ((team != NULL) ? team->t.t_id : -1),
3595 this_thr->th.th_task_state));
3605 if (team->t.t_nproc > 1) {
3606 int other_team = 1 - this_thr->th.th_task_state;
3607 if (team->t.t_task_team[other_team] == NULL) {
3608 team->t.t_task_team[other_team] =
3609 __kmp_allocate_task_team(this_thr, team);
3610 KA_TRACE(20, (
"__kmp_task_team_setup: Master T#%d created second new "
3611 "task_team %p for team %d at parity=%d\n",
3612 __kmp_gtid_from_thread(this_thr),
3613 team->t.t_task_team[other_team],
3614 ((team != NULL) ? team->t.t_id : -1), other_team));
3617 kmp_task_team_t *task_team = team->t.t_task_team[other_team];
3618 if (!task_team->tt.tt_active ||
3619 team->t.t_nproc != task_team->tt.tt_nproc) {
3620 TCW_4(task_team->tt.tt_nproc, team->t.t_nproc);
3621 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3622 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3623 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads,
3625 TCW_4(task_team->tt.tt_active, TRUE);
3629 KA_TRACE(20, (
"__kmp_task_team_setup: Master T#%d reset next task_team "
3630 "%p for team %d at parity=%d\n",
3631 __kmp_gtid_from_thread(this_thr),
3632 team->t.t_task_team[other_team],
3633 ((team != NULL) ? team->t.t_id : -1), other_team));
3641 if (this_thr == __kmp_hidden_helper_main_thread) {
3642 for (
int i = 0; i < 2; ++i) {
3643 kmp_task_team_t *task_team = team->t.t_task_team[i];
3644 if (KMP_TASKING_ENABLED(task_team)) {
3647 __kmp_enable_tasking(task_team, this_thr);
3648 for (
int j = 0; j < task_team->tt.tt_nproc; ++j) {
3649 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[j];
3650 if (thread_data->td.td_deque == NULL) {
3651 __kmp_alloc_task_deque(__kmp_hidden_helper_threads[j], thread_data);
3661 void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team) {
3662 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3666 this_thr->th.th_task_state = (kmp_uint8)(1 - this_thr->th.th_task_state);
3670 TCW_PTR(this_thr->th.th_task_team,
3671 team->t.t_task_team[this_thr->th.th_task_state]);
3673 (
"__kmp_task_team_sync: Thread T#%d task team switched to task_team "
3674 "%p from Team #%d (parity=%d)\n",
3675 __kmp_gtid_from_thread(this_thr), this_thr->th.th_task_team,
3676 ((team != NULL) ? team->t.t_id : -1), this_thr->th.th_task_state));
3686 void __kmp_task_team_wait(
3687 kmp_info_t *this_thr,
3688 kmp_team_t *team USE_ITT_BUILD_ARG(
void *itt_sync_obj),
int wait) {
3689 kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state];
3691 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3692 KMP_DEBUG_ASSERT(task_team == this_thr->th.th_task_team);
3694 if ((task_team != NULL) && KMP_TASKING_ENABLED(task_team)) {
3696 KA_TRACE(20, (
"__kmp_task_team_wait: Master T#%d waiting for all tasks "
3697 "(for unfinished_threads to reach 0) on task_team = %p\n",
3698 __kmp_gtid_from_thread(this_thr), task_team));
3702 kmp_flag_32<false, false> flag(
3703 RCAST(std::atomic<kmp_uint32> *,
3704 &task_team->tt.tt_unfinished_threads),
3706 flag.wait(this_thr, TRUE USE_ITT_BUILD_ARG(itt_sync_obj));
3712 (
"__kmp_task_team_wait: Master T#%d deactivating task_team %p: "
3713 "setting active to false, setting local and team's pointer to NULL\n",
3714 __kmp_gtid_from_thread(this_thr), task_team));
3715 KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1 ||
3716 task_team->tt.tt_found_proxy_tasks == TRUE);
3717 TCW_SYNC_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3718 KMP_CHECK_UPDATE(task_team->tt.tt_untied_task_encountered, 0);
3719 TCW_SYNC_4(task_team->tt.tt_active, FALSE);
3722 TCW_PTR(this_thr->th.th_task_team, NULL);
3731 void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread,
int gtid) {
3732 std::atomic<kmp_uint32> *spin = RCAST(
3733 std::atomic<kmp_uint32> *,
3734 &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads);
3736 KMP_DEBUG_ASSERT(__kmp_tasking_mode == tskm_extra_barrier);
3739 KMP_FSYNC_SPIN_INIT(spin, NULL);
3741 kmp_flag_32<false, false> spin_flag(spin, 0U);
3742 while (!spin_flag.execute_tasks(thread, gtid, TRUE,
3743 &flag USE_ITT_BUILD_ARG(NULL), 0)) {
3746 KMP_FSYNC_SPIN_PREPARE(RCAST(
void *, spin));
3749 if (TCR_4(__kmp_global.g.g_done)) {
3750 if (__kmp_global.g.g_abort)
3751 __kmp_abort_thread();
3757 KMP_FSYNC_SPIN_ACQUIRED(RCAST(
void *, spin));
3766 static bool __kmp_give_task(kmp_info_t *thread, kmp_int32 tid, kmp_task_t *task,
3768 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
3769 kmp_task_team_t *task_team = taskdata->td_task_team;
3771 KA_TRACE(20, (
"__kmp_give_task: trying to give task %p to thread %d.\n",
3775 KMP_DEBUG_ASSERT(task_team != NULL);
3777 bool result =
false;
3778 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
3780 if (thread_data->td.td_deque == NULL) {
3784 (
"__kmp_give_task: thread %d has no queue while giving task %p.\n",
3789 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3790 TASK_DEQUE_SIZE(thread_data->td)) {
3793 (
"__kmp_give_task: queue is full while giving task %p to thread %d.\n",
3798 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
3801 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3802 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3803 TASK_DEQUE_SIZE(thread_data->td)) {
3805 __kmp_realloc_task_deque(thread, thread_data);
3810 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3812 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3813 TASK_DEQUE_SIZE(thread_data->td)) {
3814 KA_TRACE(30, (
"__kmp_give_task: queue is full while giving task %p to "
3820 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
3821 goto release_and_exit;
3823 __kmp_realloc_task_deque(thread, thread_data);
3829 thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
3831 thread_data->td.td_deque_tail =
3832 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
3833 TCW_4(thread_data->td.td_deque_ntasks,
3834 TCR_4(thread_data->td.td_deque_ntasks) + 1);
3837 KA_TRACE(30, (
"__kmp_give_task: successfully gave task %p to thread %d.\n",
3841 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3862 static void __kmp_first_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
3863 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
3864 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3865 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
3866 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
3868 taskdata->td_flags.complete = 1;
3870 if (taskdata->td_taskgroup)
3871 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
3875 KMP_ATOMIC_INC(&taskdata->td_incomplete_child_tasks);
3878 static void __kmp_second_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
3879 kmp_int32 children = 0;
3883 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks) - 1;
3884 KMP_DEBUG_ASSERT(children >= 0);
3887 KMP_ATOMIC_DEC(&taskdata->td_incomplete_child_tasks);
3890 static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask) {
3891 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3892 kmp_info_t *thread = __kmp_threads[gtid];
3894 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3895 KMP_DEBUG_ASSERT(taskdata->td_flags.complete ==
3900 while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) > 0)
3903 __kmp_release_deps(gtid, taskdata);
3904 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
3916 KMP_DEBUG_ASSERT(ptask != NULL);
3917 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3919 10, (
"__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n",
3921 __kmp_assert_valid_gtid(gtid);
3922 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3924 __kmp_first_top_half_finish_proxy(taskdata);
3925 __kmp_second_top_half_finish_proxy(taskdata);
3926 __kmp_bottom_half_finish_proxy(gtid, ptask);
3929 (
"__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n",
3941 KMP_DEBUG_ASSERT(ptask != NULL);
3942 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3946 (
"__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n",
3949 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3951 __kmp_first_top_half_finish_proxy(taskdata);
3955 kmp_team_t *team = taskdata->td_team;
3956 kmp_int32 nthreads = team->t.t_nproc;
3961 kmp_int32 start_k = 0;
3963 kmp_int32 k = start_k;
3967 thread = team->t.t_threads[k];
3968 k = (k + 1) % nthreads;
3974 }
while (!__kmp_give_task(thread, k, ptask, pass));
3976 __kmp_second_top_half_finish_proxy(taskdata);
3980 (
"__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n",
3984 kmp_event_t *__kmpc_task_allow_completion_event(
ident_t *loc_ref,
int gtid,
3986 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(task);
3987 if (td->td_allow_completion_event.type == KMP_EVENT_UNINITIALIZED) {
3988 td->td_allow_completion_event.type = KMP_EVENT_ALLOW_COMPLETION;
3989 td->td_allow_completion_event.ed.task = task;
3990 __kmp_init_tas_lock(&td->td_allow_completion_event.lock);
3992 return &td->td_allow_completion_event;
3995 void __kmp_fulfill_event(kmp_event_t *event) {
3996 if (event->type == KMP_EVENT_ALLOW_COMPLETION) {
3997 kmp_task_t *ptask = event->ed.task;
3998 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3999 bool detached =
false;
4000 int gtid = __kmp_get_gtid();
4005 __kmp_acquire_tas_lock(&event->lock, gtid);
4006 if (taskdata->td_flags.proxy == TASK_PROXY) {
4012 if (UNLIKELY(ompt_enabled.enabled))
4013 __ompt_task_finish(ptask, NULL, ompt_task_early_fulfill);
4016 event->type = KMP_EVENT_UNINITIALIZED;
4017 __kmp_release_tas_lock(&event->lock, gtid);
4023 if (UNLIKELY(ompt_enabled.enabled))
4024 __ompt_task_finish(ptask, NULL, ompt_task_late_fulfill);
4028 kmp_team_t *team = taskdata->td_team;
4029 kmp_info_t *thread = __kmp_get_thread();
4030 if (thread->th.th_team == team) {
4048 kmp_task_t *__kmp_task_dup_alloc(kmp_info_t *thread, kmp_task_t *task_src) {
4050 kmp_taskdata_t *taskdata;
4051 kmp_taskdata_t *taskdata_src = KMP_TASK_TO_TASKDATA(task_src);
4052 kmp_taskdata_t *parent_task = taskdata_src->td_parent;
4053 size_t shareds_offset;
4056 KA_TRACE(10, (
"__kmp_task_dup_alloc(enter): Th %p, source task %p\n", thread,
4058 KMP_DEBUG_ASSERT(taskdata_src->td_flags.proxy ==
4060 KMP_DEBUG_ASSERT(taskdata_src->td_flags.tasktype == TASK_EXPLICIT);
4061 task_size = taskdata_src->td_size_alloc;
4064 KA_TRACE(30, (
"__kmp_task_dup_alloc: Th %p, malloc size %ld\n", thread,
4067 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, task_size);
4069 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, task_size);
4071 KMP_MEMCPY(taskdata, taskdata_src, task_size);
4073 task = KMP_TASKDATA_TO_TASK(taskdata);
4076 taskdata->td_task_id = KMP_GEN_TASK_ID();
4077 if (task->shareds != NULL) {
4078 shareds_offset = (
char *)task_src->shareds - (
char *)taskdata_src;
4079 task->shareds = &((
char *)taskdata)[shareds_offset];
4080 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
4083 taskdata->td_alloc_thread = thread;
4084 taskdata->td_parent = parent_task;
4086 taskdata->td_taskgroup = parent_task->td_taskgroup;
4089 if (taskdata->td_flags.tiedness == TASK_TIED)
4090 taskdata->td_last_tied = taskdata;
4094 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
4095 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
4096 if (parent_task->td_taskgroup)
4097 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
4100 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT)
4101 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
4105 (
"__kmp_task_dup_alloc(exit): Th %p, created task %p, parent=%p\n",
4106 thread, taskdata, taskdata->td_parent));
4108 if (UNLIKELY(ompt_enabled.enabled))
4109 __ompt_task_init(taskdata, thread->th.th_info.ds.ds_gtid);
4118 typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32);
4120 KMP_BUILD_ASSERT(
sizeof(
long) == 4 ||
sizeof(
long) == 8);
4125 class kmp_taskloop_bounds_t {
4127 const kmp_taskdata_t *taskdata;
4128 size_t lower_offset;
4129 size_t upper_offset;
4132 kmp_taskloop_bounds_t(kmp_task_t *_task, kmp_uint64 *lb, kmp_uint64 *ub)
4133 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(task)),
4134 lower_offset((char *)lb - (char *)task),
4135 upper_offset((char *)ub - (char *)task) {
4136 KMP_DEBUG_ASSERT((
char *)lb > (
char *)_task);
4137 KMP_DEBUG_ASSERT((
char *)ub > (
char *)_task);
4139 kmp_taskloop_bounds_t(kmp_task_t *_task,
const kmp_taskloop_bounds_t &bounds)
4140 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(_task)),
4141 lower_offset(bounds.lower_offset), upper_offset(bounds.upper_offset) {}
4142 size_t get_lower_offset()
const {
return lower_offset; }
4143 size_t get_upper_offset()
const {
return upper_offset; }
4144 kmp_uint64 get_lb()
const {
4146 #if defined(KMP_GOMP_COMPAT)
4148 if (!taskdata->td_flags.native) {
4149 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4152 if (taskdata->td_size_loop_bounds == 4) {
4153 kmp_int32 *lb = RCAST(kmp_int32 *, task->shareds);
4154 retval = (kmp_int64)*lb;
4156 kmp_int64 *lb = RCAST(kmp_int64 *, task->shareds);
4157 retval = (kmp_int64)*lb;
4161 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4165 kmp_uint64 get_ub()
const {
4167 #if defined(KMP_GOMP_COMPAT)
4169 if (!taskdata->td_flags.native) {
4170 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4173 if (taskdata->td_size_loop_bounds == 4) {
4174 kmp_int32 *ub = RCAST(kmp_int32 *, task->shareds) + 1;
4175 retval = (kmp_int64)*ub;
4177 kmp_int64 *ub = RCAST(kmp_int64 *, task->shareds) + 1;
4178 retval = (kmp_int64)*ub;
4182 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4186 void set_lb(kmp_uint64 lb) {
4187 #if defined(KMP_GOMP_COMPAT)
4189 if (!taskdata->td_flags.native) {
4190 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4193 if (taskdata->td_size_loop_bounds == 4) {
4194 kmp_uint32 *lower = RCAST(kmp_uint32 *, task->shareds);
4195 *lower = (kmp_uint32)lb;
4197 kmp_uint64 *lower = RCAST(kmp_uint64 *, task->shareds);
4198 *lower = (kmp_uint64)lb;
4202 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4205 void set_ub(kmp_uint64 ub) {
4206 #if defined(KMP_GOMP_COMPAT)
4208 if (!taskdata->td_flags.native) {
4209 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4212 if (taskdata->td_size_loop_bounds == 4) {
4213 kmp_uint32 *upper = RCAST(kmp_uint32 *, task->shareds) + 1;
4214 *upper = (kmp_uint32)ub;
4216 kmp_uint64 *upper = RCAST(kmp_uint64 *, task->shareds) + 1;
4217 *upper = (kmp_uint64)ub;
4221 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4242 void __kmp_taskloop_linear(
ident_t *loc,
int gtid, kmp_task_t *task,
4243 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4244 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
4245 kmp_uint64 grainsize, kmp_uint64 extras,
4246 kmp_int64 last_chunk, kmp_uint64 tc,
4252 KMP_TIME_PARTITIONED_BLOCK(OMP_taskloop_scheduling);
4253 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
4255 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4256 kmp_uint64 lower = task_bounds.get_lb();
4257 kmp_uint64 upper = task_bounds.get_ub();
4259 kmp_info_t *thread = __kmp_threads[gtid];
4260 kmp_taskdata_t *current_task = thread->th.th_current_task;
4261 kmp_task_t *next_task;
4262 kmp_int32 lastpriv = 0;
4265 tc == num_tasks * grainsize + (last_chunk < 0 ? last_chunk : extras));
4266 KMP_DEBUG_ASSERT(num_tasks > extras);
4267 KMP_DEBUG_ASSERT(num_tasks > 0);
4268 KA_TRACE(20, (
"__kmp_taskloop_linear: T#%d: %lld tasks, grainsize %lld, "
4269 "extras %lld, last_chunk %lld, i=%lld,%lld(%d)%lld, dup %p\n",
4270 gtid, num_tasks, grainsize, extras, last_chunk, lower, upper,
4271 ub_glob, st, task_dup));
4274 for (i = 0; i < num_tasks; ++i) {
4275 kmp_uint64 chunk_minus_1;
4277 chunk_minus_1 = grainsize - 1;
4279 chunk_minus_1 = grainsize;
4282 upper = lower + st * chunk_minus_1;
4286 if (i == num_tasks - 1) {
4289 KMP_DEBUG_ASSERT(upper == *ub);
4290 if (upper == ub_glob)
4292 }
else if (st > 0) {
4293 KMP_DEBUG_ASSERT((kmp_uint64)st > *ub - upper);
4294 if ((kmp_uint64)st > ub_glob - upper)
4297 KMP_DEBUG_ASSERT(upper + st < *ub);
4298 if (upper - ub_glob < (kmp_uint64)(-st))
4302 next_task = __kmp_task_dup_alloc(thread, task);
4303 kmp_taskdata_t *next_taskdata = KMP_TASK_TO_TASKDATA(next_task);
4304 kmp_taskloop_bounds_t next_task_bounds =
4305 kmp_taskloop_bounds_t(next_task, task_bounds);
4308 next_task_bounds.set_lb(lower);
4309 if (next_taskdata->td_flags.native) {
4310 next_task_bounds.set_ub(upper + (st > 0 ? 1 : -1));
4312 next_task_bounds.set_ub(upper);
4314 if (ptask_dup != NULL)
4316 ptask_dup(next_task, task, lastpriv);
4318 (
"__kmp_taskloop_linear: T#%d; task #%llu: task %p: lower %lld, "
4319 "upper %lld stride %lld, (offsets %p %p)\n",
4320 gtid, i, next_task, lower, upper, st,
4321 next_task_bounds.get_lower_offset(),
4322 next_task_bounds.get_upper_offset()));
4324 __kmp_omp_taskloop_task(NULL, gtid, next_task,
4327 __kmp_omp_task(gtid, next_task,
true);
4332 __kmp_task_start(gtid, task, current_task);
4334 __kmp_task_finish<false>(gtid, task, current_task);
4339 typedef struct __taskloop_params {
4346 kmp_uint64 num_tasks;
4347 kmp_uint64 grainsize;
4349 kmp_int64 last_chunk;
4351 kmp_uint64 num_t_min;
4355 } __taskloop_params_t;
4357 void __kmp_taskloop_recur(
ident_t *,
int, kmp_task_t *, kmp_uint64 *,
4358 kmp_uint64 *, kmp_int64, kmp_uint64, kmp_uint64,
4359 kmp_uint64, kmp_uint64, kmp_int64, kmp_uint64,
4367 int __kmp_taskloop_task(
int gtid,
void *ptask) {
4368 __taskloop_params_t *p =
4369 (__taskloop_params_t *)((kmp_task_t *)ptask)->shareds;
4370 kmp_task_t *task = p->task;
4371 kmp_uint64 *lb = p->lb;
4372 kmp_uint64 *ub = p->ub;
4373 void *task_dup = p->task_dup;
4375 kmp_int64 st = p->st;
4376 kmp_uint64 ub_glob = p->ub_glob;
4377 kmp_uint64 num_tasks = p->num_tasks;
4378 kmp_uint64 grainsize = p->grainsize;
4379 kmp_uint64 extras = p->extras;
4380 kmp_int64 last_chunk = p->last_chunk;
4381 kmp_uint64 tc = p->tc;
4382 kmp_uint64 num_t_min = p->num_t_min;
4384 void *codeptr_ra = p->codeptr_ra;
4387 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4388 KMP_DEBUG_ASSERT(task != NULL);
4390 (
"__kmp_taskloop_task: T#%d, task %p: %lld tasks, grainsize"
4391 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
4392 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
4395 KMP_DEBUG_ASSERT(num_tasks * 2 + 1 > num_t_min);
4396 if (num_tasks > num_t_min)
4397 __kmp_taskloop_recur(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
4398 grainsize, extras, last_chunk, tc, num_t_min,
4404 __kmp_taskloop_linear(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
4405 grainsize, extras, last_chunk, tc,
4411 KA_TRACE(40, (
"__kmp_taskloop_task(exit): T#%d\n", gtid));
4433 void __kmp_taskloop_recur(
ident_t *loc,
int gtid, kmp_task_t *task,
4434 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4435 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
4436 kmp_uint64 grainsize, kmp_uint64 extras,
4437 kmp_int64 last_chunk, kmp_uint64 tc,
4438 kmp_uint64 num_t_min,
4443 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4444 KMP_DEBUG_ASSERT(task != NULL);
4445 KMP_DEBUG_ASSERT(num_tasks > num_t_min);
4447 (
"__kmp_taskloop_recur: T#%d, task %p: %lld tasks, grainsize"
4448 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
4449 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
4451 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
4452 kmp_uint64 lower = *lb;
4453 kmp_info_t *thread = __kmp_threads[gtid];
4455 kmp_task_t *next_task;
4456 size_t lower_offset =
4457 (
char *)lb - (
char *)task;
4458 size_t upper_offset =
4459 (
char *)ub - (
char *)task;
4462 tc == num_tasks * grainsize + (last_chunk < 0 ? last_chunk : extras));
4463 KMP_DEBUG_ASSERT(num_tasks > extras);
4464 KMP_DEBUG_ASSERT(num_tasks > 0);
4467 kmp_uint64 lb1, ub0, tc0, tc1, ext0, ext1;
4468 kmp_int64 last_chunk0 = 0, last_chunk1 = 0;
4469 kmp_uint64 gr_size0 = grainsize;
4470 kmp_uint64 n_tsk0 = num_tasks >> 1;
4471 kmp_uint64 n_tsk1 = num_tasks - n_tsk0;
4472 if (last_chunk < 0) {
4474 last_chunk1 = last_chunk;
4475 tc0 = grainsize * n_tsk0;
4477 }
else if (n_tsk0 <= extras) {
4480 ext1 = extras - n_tsk0;
4481 tc0 = gr_size0 * n_tsk0;
4486 tc1 = grainsize * n_tsk1;
4489 ub0 = lower + st * (tc0 - 1);
4493 next_task = __kmp_task_dup_alloc(thread, task);
4495 *(kmp_uint64 *)((
char *)next_task + lower_offset) = lb1;
4496 if (ptask_dup != NULL)
4497 ptask_dup(next_task, task, 0);
4502 kmp_taskdata_t *current_task = thread->th.th_current_task;
4503 thread->th.th_current_task = taskdata->td_parent;
4504 kmp_task_t *new_task =
4505 __kmpc_omp_task_alloc(loc, gtid, 1, 3 *
sizeof(
void *),
4506 sizeof(__taskloop_params_t), &__kmp_taskloop_task);
4508 thread->th.th_current_task = current_task;
4509 __taskloop_params_t *p = (__taskloop_params_t *)new_task->shareds;
4510 p->task = next_task;
4511 p->lb = (kmp_uint64 *)((
char *)next_task + lower_offset);
4512 p->ub = (kmp_uint64 *)((
char *)next_task + upper_offset);
4513 p->task_dup = task_dup;
4515 p->ub_glob = ub_glob;
4516 p->num_tasks = n_tsk1;
4517 p->grainsize = grainsize;
4519 p->last_chunk = last_chunk1;
4521 p->num_t_min = num_t_min;
4523 p->codeptr_ra = codeptr_ra;
4528 __kmp_omp_taskloop_task(NULL, gtid, new_task, codeptr_ra);
4530 __kmp_omp_task(gtid, new_task,
true);
4534 if (n_tsk0 > num_t_min)
4535 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0, gr_size0,
4536 ext0, last_chunk0, tc0, num_t_min,
4542 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0,
4543 gr_size0, ext0, last_chunk0, tc0,
4549 KA_TRACE(40, (
"__kmp_taskloop_recur(exit): T#%d\n", gtid));
4552 static void __kmp_taskloop(
ident_t *loc,
int gtid, kmp_task_t *task,
int if_val,
4553 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4554 int nogroup,
int sched, kmp_uint64 grainsize,
4555 int modifier,
void *task_dup) {
4556 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4557 KMP_DEBUG_ASSERT(task != NULL);
4559 #if OMPT_SUPPORT && OMPT_OPTIONAL
4560 OMPT_STORE_RETURN_ADDRESS(gtid);
4562 __kmpc_taskgroup(loc, gtid);
4567 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4570 kmp_uint64 lower = task_bounds.get_lb();
4571 kmp_uint64 upper = task_bounds.get_ub();
4572 kmp_uint64 ub_glob = upper;
4573 kmp_uint64 num_tasks = 0, extras = 0;
4574 kmp_int64 last_chunk =
4576 kmp_uint64 num_tasks_min = __kmp_taskloop_min_tasks;
4577 kmp_info_t *thread = __kmp_threads[gtid];
4578 kmp_taskdata_t *current_task = thread->th.th_current_task;
4580 KA_TRACE(20, (
"__kmp_taskloop: T#%d, task %p, lb %lld, ub %lld, st %lld, "
4581 "grain %llu(%d, %d), dup %p\n",
4582 gtid, taskdata, lower, upper, st, grainsize, sched, modifier,
4587 tc = upper - lower + 1;
4588 }
else if (st < 0) {
4589 tc = (lower - upper) / (-st) + 1;
4591 tc = (upper - lower) / st + 1;
4594 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d zero-trip loop\n", gtid));
4596 __kmp_task_start(gtid, task, current_task);
4598 __kmp_task_finish<false>(gtid, task, current_task);
4602 #if OMPT_SUPPORT && OMPT_OPTIONAL
4603 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
4604 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
4605 if (ompt_enabled.ompt_callback_work) {
4606 ompt_callbacks.ompt_callback(ompt_callback_work)(
4607 ompt_work_taskloop, ompt_scope_begin, &(team_info->parallel_data),
4608 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
4612 if (num_tasks_min == 0)
4615 KMP_MIN(thread->th.th_team_nproc * 10, INITIAL_TASK_DEQUE_SIZE);
4621 grainsize = thread->th.th_team_nproc * 10;
4624 if (grainsize > tc) {
4629 num_tasks = grainsize;
4630 grainsize = tc / num_tasks;
4631 extras = tc % num_tasks;
4635 if (grainsize > tc) {
4641 num_tasks = (tc + grainsize - 1) / grainsize;
4642 last_chunk = tc - (num_tasks * grainsize);
4645 num_tasks = tc / grainsize;
4647 grainsize = tc / num_tasks;
4648 extras = tc % num_tasks;
4653 KMP_ASSERT2(0,
"unknown scheduling of taskloop");
4657 tc == num_tasks * grainsize + (last_chunk < 0 ? last_chunk : extras));
4658 KMP_DEBUG_ASSERT(num_tasks > extras);
4659 KMP_DEBUG_ASSERT(num_tasks > 0);
4665 taskdata->td_flags.task_serial = 1;
4666 taskdata->td_flags.tiedness = TASK_TIED;
4668 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4669 grainsize, extras, last_chunk, tc,
4671 OMPT_GET_RETURN_ADDRESS(0),
4676 }
else if (num_tasks > num_tasks_min && !taskdata->td_flags.native) {
4677 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go recursive: tc %llu, #tasks %llu"
4678 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
4679 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
4681 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4682 grainsize, extras, last_chunk, tc, num_tasks_min,
4684 OMPT_GET_RETURN_ADDRESS(0),
4688 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go linear: tc %llu, #tasks %llu"
4689 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
4690 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
4692 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4693 grainsize, extras, last_chunk, tc,
4695 OMPT_GET_RETURN_ADDRESS(0),
4700 #if OMPT_SUPPORT && OMPT_OPTIONAL
4701 if (ompt_enabled.ompt_callback_work) {
4702 ompt_callbacks.ompt_callback(ompt_callback_work)(
4703 ompt_work_taskloop, ompt_scope_end, &(team_info->parallel_data),
4704 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
4709 #if OMPT_SUPPORT && OMPT_OPTIONAL
4710 OMPT_STORE_RETURN_ADDRESS(gtid);
4712 __kmpc_end_taskgroup(loc, gtid);
4714 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d\n", gtid));
4734 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
int nogroup,
4735 int sched, kmp_uint64 grainsize,
void *task_dup) {
4736 __kmp_assert_valid_gtid(gtid);
4737 KA_TRACE(20, (
"__kmpc_taskloop(enter): T#%d\n", gtid));
4738 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
4740 KA_TRACE(20, (
"__kmpc_taskloop(exit): T#%d\n", gtid));
4761 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4762 int nogroup,
int sched, kmp_uint64 grainsize,
4763 int modifier,
void *task_dup) {
4764 __kmp_assert_valid_gtid(gtid);
4765 KA_TRACE(20, (
"__kmpc_taskloop_5(enter): T#%d\n", gtid));
4766 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
4767 modifier, task_dup);
4768 KA_TRACE(20, (
"__kmpc_taskloop_5(exit): T#%d\n", gtid));
struct kmp_taskred_data kmp_taskred_data_t
struct kmp_task_red_input kmp_task_red_input_t
struct kmp_taskred_flags kmp_taskred_flags_t
struct kmp_taskred_input kmp_taskred_input_t
#define KMP_COUNT_BLOCK(name)
Increments specified counter (name).
void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int sched, kmp_uint64 grainsize, void *task_dup)
void * __kmpc_taskred_modifier_init(ident_t *loc, int gtid, int is_ws, int num, void *data)
void * __kmpc_taskred_init(int gtid, int num, void *data)
void * __kmpc_task_reduction_init(int gtid, int num, void *data)
void __kmpc_proxy_task_completed_ooo(kmp_task_t *ptask)
void __kmpc_task_reduction_modifier_fini(ident_t *loc, int gtid, int is_ws)
kmp_int32 __kmpc_omp_reg_task_with_affinity(ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *new_task, kmp_int32 naffins, kmp_task_affinity_info_t *affin_list)
void * __kmpc_task_reduction_modifier_init(ident_t *loc, int gtid, int is_ws, int num, void *data)
void __kmpc_taskloop_5(ident_t *loc, int gtid, kmp_task_t *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int sched, kmp_uint64 grainsize, int modifier, void *task_dup)
void __kmpc_proxy_task_completed(kmp_int32 gtid, kmp_task_t *ptask)
void * __kmpc_task_reduction_get_th_data(int gtid, void *tskgrp, void *data)
kmp_taskred_flags_t flags