17 #include "kmp_stats.h" 18 #include "kmp_wait_release.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);
35 static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask);
38 #ifdef BUILD_TIED_TASK_STACK 47 static void __kmp_trace_task_stack(kmp_int32 gtid,
48 kmp_thread_data_t *thread_data,
49 int threshold,
char *location) {
50 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
51 kmp_taskdata_t **stack_top = task_stack->ts_top;
52 kmp_int32 entries = task_stack->ts_entries;
53 kmp_taskdata_t *tied_task;
57 (
"__kmp_trace_task_stack(start): location = %s, gtid = %d, entries = %d, " 58 "first_block = %p, stack_top = %p \n",
59 location, gtid, entries, task_stack->ts_first_block, stack_top));
61 KMP_DEBUG_ASSERT(stack_top != NULL);
62 KMP_DEBUG_ASSERT(entries > 0);
64 while (entries != 0) {
65 KMP_DEBUG_ASSERT(stack_top != &task_stack->ts_first_block.sb_block[0]);
67 if (entries & TASK_STACK_INDEX_MASK == 0) {
68 kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(stack_top);
70 stack_block = stack_block->sb_prev;
71 stack_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE];
78 tied_task = *stack_top;
80 KMP_DEBUG_ASSERT(tied_task != NULL);
81 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
84 (
"__kmp_trace_task_stack(%s): gtid=%d, entry=%d, " 85 "stack_top=%p, tied_task=%p\n",
86 location, gtid, entries, stack_top, tied_task));
88 KMP_DEBUG_ASSERT(stack_top == &task_stack->ts_first_block.sb_block[0]);
91 (
"__kmp_trace_task_stack(exit): location = %s, gtid = %d\n",
101 static void __kmp_init_task_stack(kmp_int32 gtid,
102 kmp_thread_data_t *thread_data) {
103 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
104 kmp_stack_block_t *first_block;
107 first_block = &task_stack->ts_first_block;
108 task_stack->ts_top = (kmp_taskdata_t **)first_block;
109 memset((
void *)first_block,
'\0',
110 TASK_STACK_BLOCK_SIZE *
sizeof(kmp_taskdata_t *));
113 task_stack->ts_entries = TASK_STACK_EMPTY;
114 first_block->sb_next = NULL;
115 first_block->sb_prev = NULL;
122 static void __kmp_free_task_stack(kmp_int32 gtid,
123 kmp_thread_data_t *thread_data) {
124 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
125 kmp_stack_block_t *stack_block = &task_stack->ts_first_block;
127 KMP_DEBUG_ASSERT(task_stack->ts_entries == TASK_STACK_EMPTY);
129 while (stack_block != NULL) {
130 kmp_stack_block_t *next_block = (stack_block) ? stack_block->sb_next : NULL;
132 stack_block->sb_next = NULL;
133 stack_block->sb_prev = NULL;
134 if (stack_block != &task_stack->ts_first_block) {
135 __kmp_thread_free(thread,
138 stack_block = next_block;
141 task_stack->ts_entries = 0;
142 task_stack->ts_top = NULL;
151 static void __kmp_push_task_stack(kmp_int32 gtid, kmp_info_t *thread,
152 kmp_taskdata_t *tied_task) {
154 kmp_thread_data_t *thread_data =
155 &thread->th.th_task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
156 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
158 if (tied_task->td_flags.team_serial || tied_task->td_flags.tasking_ser) {
162 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
163 KMP_DEBUG_ASSERT(task_stack->ts_top != NULL);
166 (
"__kmp_push_task_stack(enter): GTID: %d; THREAD: %p; TASK: %p\n",
167 gtid, thread, tied_task));
169 *(task_stack->ts_top) = tied_task;
172 task_stack->ts_top++;
173 task_stack->ts_entries++;
175 if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) {
177 kmp_stack_block_t *stack_block =
178 (kmp_stack_block_t *)(task_stack->ts_top - TASK_STACK_BLOCK_SIZE);
181 if (stack_block->sb_next !=
183 task_stack->ts_top = &stack_block->sb_next->sb_block[0];
185 kmp_stack_block_t *new_block = (kmp_stack_block_t *)__kmp_thread_calloc(
186 thread,
sizeof(kmp_stack_block_t));
188 task_stack->ts_top = &new_block->sb_block[0];
189 stack_block->sb_next = new_block;
190 new_block->sb_prev = stack_block;
191 new_block->sb_next = NULL;
195 (
"__kmp_push_task_stack(): GTID: %d; TASK: %p; Alloc new block: %p\n",
196 gtid, tied_task, new_block));
199 KA_TRACE(20, (
"__kmp_push_task_stack(exit): GTID: %d; TASK: %p\n", gtid,
210 static void __kmp_pop_task_stack(kmp_int32 gtid, kmp_info_t *thread,
211 kmp_taskdata_t *ending_task) {
213 kmp_thread_data_t *thread_data =
214 &thread->th.th_task_team->tt_threads_data[__kmp_tid_from_gtid(gtid)];
215 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
216 kmp_taskdata_t *tied_task;
218 if (ending_task->td_flags.team_serial || ending_task->td_flags.tasking_ser) {
223 KMP_DEBUG_ASSERT(task_stack->ts_top != NULL);
224 KMP_DEBUG_ASSERT(task_stack->ts_entries > 0);
226 KA_TRACE(20, (
"__kmp_pop_task_stack(enter): GTID: %d; THREAD: %p\n", gtid,
230 if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) {
231 kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(task_stack->ts_top);
233 stack_block = stack_block->sb_prev;
234 task_stack->ts_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE];
238 task_stack->ts_top--;
239 task_stack->ts_entries--;
241 tied_task = *(task_stack->ts_top);
243 KMP_DEBUG_ASSERT(tied_task != NULL);
244 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
245 KMP_DEBUG_ASSERT(tied_task == ending_task);
247 KA_TRACE(20, (
"__kmp_pop_task_stack(exit): GTID: %d; TASK: %p\n", gtid,
254 static kmp_int32 __kmp_push_task(kmp_int32 gtid, kmp_task_t *task) {
255 kmp_info_t *thread = __kmp_threads[gtid];
256 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
257 kmp_task_team_t *task_team = thread->th.th_task_team;
258 kmp_int32 tid = __kmp_tid_from_gtid(gtid);
259 kmp_thread_data_t *thread_data;
262 (
"__kmp_push_task: T#%d trying to push task %p.\n", gtid, taskdata));
264 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
267 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
268 KMP_DEBUG_USE_VAR(counter);
271 (
"__kmp_push_task: T#%d untied_count (%d) incremented for task %p\n",
272 gtid, counter, taskdata));
276 if (taskdata->td_flags.task_serial) {
277 KA_TRACE(20, (
"__kmp_push_task: T#%d team serialized; returning " 278 "TASK_NOT_PUSHED for task %p\n",
280 return TASK_NOT_PUSHED;
285 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
286 if (!KMP_TASKING_ENABLED(task_team)) {
287 __kmp_enable_tasking(task_team, thread);
289 KMP_DEBUG_ASSERT(TCR_4(task_team->tt.tt_found_tasks) == TRUE);
290 KMP_DEBUG_ASSERT(TCR_PTR(task_team->tt.tt_threads_data) != NULL);
293 thread_data = &task_team->tt.tt_threads_data[tid];
296 if (thread_data->td.td_deque == NULL) {
297 __kmp_alloc_task_deque(thread, thread_data);
301 if (TCR_4(thread_data->td.td_deque_ntasks) >=
302 TASK_DEQUE_SIZE(thread_data->td)) {
303 KA_TRACE(20, (
"__kmp_push_task: T#%d deque is full; returning " 304 "TASK_NOT_PUSHED for task %p\n",
306 return TASK_NOT_PUSHED;
310 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
314 if (TCR_4(thread_data->td.td_deque_ntasks) >=
315 TASK_DEQUE_SIZE(thread_data->td)) {
316 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
317 KA_TRACE(20, (
"__kmp_push_task: T#%d deque is full on 2nd check; returning " 318 "TASK_NOT_PUSHED for task %p\n",
320 return TASK_NOT_PUSHED;
324 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) <
325 TASK_DEQUE_SIZE(thread_data->td));
328 thread_data->td.td_deque[thread_data->td.td_deque_tail] =
331 thread_data->td.td_deque_tail =
332 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
333 TCW_4(thread_data->td.td_deque_ntasks,
334 TCR_4(thread_data->td.td_deque_ntasks) + 1);
336 KA_TRACE(20, (
"__kmp_push_task: T#%d returning TASK_SUCCESSFULLY_PUSHED: " 337 "task=%p ntasks=%d head=%u tail=%u\n",
338 gtid, taskdata, thread_data->td.td_deque_ntasks,
339 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
341 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
343 return TASK_SUCCESSFULLY_PUSHED;
350 void __kmp_pop_current_task_from_thread(kmp_info_t *this_thr) {
351 KF_TRACE(10, (
"__kmp_pop_current_task_from_thread(enter): T#%d " 352 "this_thread=%p, curtask=%p, " 353 "curtask_parent=%p\n",
354 0, this_thr, this_thr->th.th_current_task,
355 this_thr->th.th_current_task->td_parent));
357 this_thr->th.th_current_task = this_thr->th.th_current_task->td_parent;
359 KF_TRACE(10, (
"__kmp_pop_current_task_from_thread(exit): T#%d " 360 "this_thread=%p, curtask=%p, " 361 "curtask_parent=%p\n",
362 0, this_thr, this_thr->th.th_current_task,
363 this_thr->th.th_current_task->td_parent));
372 void __kmp_push_current_task_to_thread(kmp_info_t *this_thr, kmp_team_t *team,
376 KF_TRACE(10, (
"__kmp_push_current_task_to_thread(enter): T#%d this_thread=%p " 379 tid, this_thr, this_thr->th.th_current_task,
380 team->t.t_implicit_task_taskdata[tid].td_parent));
382 KMP_DEBUG_ASSERT(this_thr != NULL);
385 if (this_thr->th.th_current_task != &team->t.t_implicit_task_taskdata[0]) {
386 team->t.t_implicit_task_taskdata[0].td_parent =
387 this_thr->th.th_current_task;
388 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[0];
391 team->t.t_implicit_task_taskdata[tid].td_parent =
392 team->t.t_implicit_task_taskdata[0].td_parent;
393 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[tid];
396 KF_TRACE(10, (
"__kmp_push_current_task_to_thread(exit): T#%d this_thread=%p " 399 tid, this_thr, this_thr->th.th_current_task,
400 team->t.t_implicit_task_taskdata[tid].td_parent));
408 static void __kmp_task_start(kmp_int32 gtid, kmp_task_t *task,
409 kmp_taskdata_t *current_task) {
410 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
411 kmp_info_t *thread = __kmp_threads[gtid];
414 (
"__kmp_task_start(enter): T#%d starting task %p: current_task=%p\n",
415 gtid, taskdata, current_task));
417 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
422 current_task->td_flags.executing = 0;
425 #ifdef BUILD_TIED_TASK_STACK 426 if (taskdata->td_flags.tiedness == TASK_TIED) {
427 __kmp_push_task_stack(gtid, thread, taskdata);
432 thread->th.th_current_task = taskdata;
434 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 0 ||
435 taskdata->td_flags.tiedness == TASK_UNTIED);
436 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0 ||
437 taskdata->td_flags.tiedness == TASK_UNTIED);
438 taskdata->td_flags.started = 1;
439 taskdata->td_flags.executing = 1;
440 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
441 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
448 KA_TRACE(10, (
"__kmp_task_start(exit): T#%d task=%p\n", gtid, taskdata));
459 static inline void __ompt_task_init(kmp_taskdata_t *task,
int tid) {
461 task->ompt_task_info.task_data.value = 0;
462 task->ompt_task_info.frame.exit_frame = NULL;
463 task->ompt_task_info.frame.enter_frame = NULL;
465 task->ompt_task_info.ndeps = 0;
466 task->ompt_task_info.deps = NULL;
472 static inline void __ompt_task_start(kmp_task_t *task,
473 kmp_taskdata_t *current_task,
475 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
476 ompt_task_status_t status = ompt_task_switch;
477 if (__kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded) {
478 status = ompt_task_yield;
479 __kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded = 0;
482 if (ompt_enabled.ompt_callback_task_schedule) {
483 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
484 &(current_task->ompt_task_info.task_data), status,
485 &(taskdata->ompt_task_info.task_data));
487 taskdata->ompt_task_info.scheduling_parent = current_task;
493 __ompt_task_finish(kmp_task_t *task, kmp_taskdata_t *resumed_task,
494 ompt_task_status_t status = ompt_task_complete) {
495 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
496 if (__kmp_omp_cancellation && taskdata->td_taskgroup &&
497 taskdata->td_taskgroup->cancel_request == cancel_taskgroup) {
498 status = ompt_task_cancel;
502 if (ompt_enabled.ompt_callback_task_schedule) {
503 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
504 &(taskdata->ompt_task_info.task_data), status,
505 &((resumed_task ? resumed_task
506 : (taskdata->ompt_task_info.scheduling_parent
507 ? taskdata->ompt_task_info.scheduling_parent
508 : taskdata->td_parent))
509 ->ompt_task_info.task_data));
515 static void __kmpc_omp_task_begin_if0_template(
ident_t *loc_ref, kmp_int32 gtid,
518 void *return_address) {
519 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
520 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
522 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(enter): T#%d loc=%p task=%p " 524 gtid, loc_ref, taskdata, current_task));
526 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
529 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
530 KMP_DEBUG_USE_VAR(counter);
531 KA_TRACE(20, (
"__kmpc_omp_task_begin_if0: T#%d untied_count (%d) " 532 "incremented for task %p\n",
533 gtid, counter, taskdata));
536 taskdata->td_flags.task_serial =
538 __kmp_task_start(gtid, task, current_task);
542 if (current_task->ompt_task_info.frame.enter_frame == NULL) {
543 current_task->ompt_task_info.frame.enter_frame =
544 taskdata->ompt_task_info.frame.exit_frame = frame_address;
546 if (ompt_enabled.ompt_callback_task_create) {
547 ompt_task_info_t *parent_info = &(current_task->ompt_task_info);
548 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
549 &(parent_info->task_data), &(parent_info->frame),
550 &(taskdata->ompt_task_info.task_data),
551 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(taskdata), 0,
554 __ompt_task_start(task, current_task, gtid);
556 #endif // OMPT_SUPPORT 558 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(exit): T#%d loc=%p task=%p,\n", gtid,
564 static void __kmpc_omp_task_begin_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
567 void *return_address) {
568 __kmpc_omp_task_begin_if0_template<true>(loc_ref, gtid, task, frame_address,
571 #endif // OMPT_SUPPORT 579 void __kmpc_omp_task_begin_if0(
ident_t *loc_ref, kmp_int32 gtid,
582 if (UNLIKELY(ompt_enabled.enabled)) {
583 OMPT_STORE_RETURN_ADDRESS(gtid);
584 __kmpc_omp_task_begin_if0_ompt(loc_ref, gtid, task,
585 OMPT_GET_FRAME_ADDRESS(1),
586 OMPT_LOAD_RETURN_ADDRESS(gtid));
590 __kmpc_omp_task_begin_if0_template<false>(loc_ref, gtid, task, NULL, NULL);
596 void __kmpc_omp_task_begin(
ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task) {
597 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
601 (
"__kmpc_omp_task_begin(enter): T#%d loc=%p task=%p current_task=%p\n",
602 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task), current_task));
604 __kmp_task_start(gtid, task, current_task);
606 KA_TRACE(10, (
"__kmpc_omp_task_begin(exit): T#%d loc=%p task=%p,\n", gtid,
607 loc_ref, KMP_TASK_TO_TASKDATA(task)));
610 #endif // TASK_UNUSED 617 static void __kmp_free_task(kmp_int32 gtid, kmp_taskdata_t *taskdata,
618 kmp_info_t *thread) {
619 KA_TRACE(30, (
"__kmp_free_task: T#%d freeing data from task %p\n", gtid,
623 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
624 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0);
625 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 1);
626 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
627 KMP_DEBUG_ASSERT(taskdata->td_allocated_child_tasks == 0 ||
628 taskdata->td_flags.task_serial == 1);
629 KMP_DEBUG_ASSERT(taskdata->td_incomplete_child_tasks == 0);
631 taskdata->td_flags.freed = 1;
632 ANNOTATE_HAPPENS_BEFORE(taskdata);
635 __kmp_fast_free(thread, taskdata);
637 __kmp_thread_free(thread, taskdata);
640 KA_TRACE(20, (
"__kmp_free_task: T#%d freed task %p\n", gtid, taskdata));
649 static void __kmp_free_task_and_ancestors(kmp_int32 gtid,
650 kmp_taskdata_t *taskdata,
651 kmp_info_t *thread) {
655 kmp_int32 team_serial =
656 (taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) &&
657 !taskdata->td_flags.proxy;
659 kmp_int32 team_serial =
660 taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser;
662 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
664 kmp_int32 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
665 KMP_DEBUG_ASSERT(children >= 0);
668 while (children == 0) {
669 kmp_taskdata_t *parent_taskdata = taskdata->td_parent;
671 KA_TRACE(20, (
"__kmp_free_task_and_ancestors(enter): T#%d task %p complete " 672 "and freeing itself\n",
676 __kmp_free_task(gtid, taskdata, thread);
678 taskdata = parent_taskdata;
682 if (team_serial || taskdata->td_flags.tasktype == TASK_IMPLICIT)
686 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
687 KMP_DEBUG_ASSERT(children >= 0);
691 20, (
"__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; " 692 "not freeing it yet\n",
693 gtid, taskdata, children));
702 static void __kmp_task_finish(kmp_int32 gtid, kmp_task_t *task,
703 kmp_taskdata_t *resumed_task) {
704 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
705 kmp_info_t *thread = __kmp_threads[gtid];
706 kmp_task_team_t *task_team =
707 thread->th.th_task_team;
708 kmp_int32 children = 0;
710 KA_TRACE(10, (
"__kmp_task_finish(enter): T#%d finishing task %p and resuming " 712 gtid, taskdata, resumed_task));
714 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
717 #ifdef BUILD_TIED_TASK_STACK 718 if (taskdata->td_flags.tiedness == TASK_TIED) {
719 __kmp_pop_task_stack(gtid, thread, taskdata);
723 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
726 kmp_int32 counter = KMP_ATOMIC_DEC(&taskdata->td_untied_count) - 1;
729 (
"__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n",
730 gtid, counter, taskdata));
734 if (resumed_task == NULL) {
735 KMP_DEBUG_ASSERT(taskdata->td_flags.task_serial);
736 resumed_task = taskdata->td_parent;
739 thread->th.th_current_task = resumed_task;
740 resumed_task->td_flags.executing = 1;
741 KA_TRACE(10, (
"__kmp_task_finish(exit): T#%d partially done task %p, " 742 "resuming task %p\n",
743 gtid, taskdata, resumed_task));
749 __ompt_task_finish(task, resumed_task);
752 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
753 taskdata->td_flags.complete = 1;
754 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 1);
755 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
759 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
762 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks) - 1;
763 KMP_DEBUG_ASSERT(children >= 0);
765 if (taskdata->td_taskgroup)
766 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
771 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) ||
772 (task_team && task_team->tt.tt_found_proxy_tasks)) {
774 __kmp_release_deps(gtid, taskdata);
781 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
782 taskdata->td_flags.executing = 0;
785 20, (
"__kmp_task_finish: T#%d finished task %p, %d incomplete children\n",
786 gtid, taskdata, children));
795 if (taskdata->td_flags.destructors_thunk) {
796 kmp_routine_entry_t destr_thunk = task->data1.destructors;
797 KMP_ASSERT(destr_thunk);
798 destr_thunk(gtid, task);
800 #endif // OMP_40_ENABLED 805 (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) ==
806 taskdata->td_flags.task_serial);
807 if (taskdata->td_flags.task_serial) {
808 if (resumed_task == NULL) {
809 resumed_task = taskdata->td_parent;
813 KMP_DEBUG_ASSERT(resumed_task !=
821 thread->th.th_current_task = resumed_task;
822 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
826 resumed_task->td_flags.executing = 1;
829 10, (
"__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n",
830 gtid, taskdata, resumed_task));
836 static void __kmpc_omp_task_complete_if0_template(
ident_t *loc_ref,
839 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n",
840 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
842 __kmp_task_finish<ompt>(gtid, task, NULL);
844 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n",
845 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
849 omp_frame_t *ompt_frame;
850 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
851 ompt_frame->enter_frame = NULL;
860 void __kmpc_omp_task_complete_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
862 __kmpc_omp_task_complete_if0_template<true>(loc_ref, gtid, task);
864 #endif // OMPT_SUPPORT 871 void __kmpc_omp_task_complete_if0(
ident_t *loc_ref, kmp_int32 gtid,
874 if (UNLIKELY(ompt_enabled.enabled)) {
875 __kmpc_omp_task_complete_if0_ompt(loc_ref, gtid, task);
879 __kmpc_omp_task_complete_if0_template<false>(loc_ref, gtid, task);
885 void __kmpc_omp_task_complete(
ident_t *loc_ref, kmp_int32 gtid,
887 KA_TRACE(10, (
"__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n", gtid,
888 loc_ref, KMP_TASK_TO_TASKDATA(task)));
890 __kmp_task_finish<false>(gtid, task,
893 KA_TRACE(10, (
"__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n", gtid,
894 loc_ref, KMP_TASK_TO_TASKDATA(task)));
897 #endif // TASK_UNUSED 910 void __kmp_init_implicit_task(
ident_t *loc_ref, kmp_info_t *this_thr,
911 kmp_team_t *team,
int tid,
int set_curr_task) {
912 kmp_taskdata_t *task = &team->t.t_implicit_task_taskdata[tid];
916 (
"__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n",
917 tid, team, task, set_curr_task ?
"TRUE" :
"FALSE"));
919 task->td_task_id = KMP_GEN_TASK_ID();
920 task->td_team = team;
923 task->td_ident = loc_ref;
924 task->td_taskwait_ident = NULL;
925 task->td_taskwait_counter = 0;
926 task->td_taskwait_thread = 0;
928 task->td_flags.tiedness = TASK_TIED;
929 task->td_flags.tasktype = TASK_IMPLICIT;
931 task->td_flags.proxy = TASK_FULL;
935 task->td_flags.task_serial = 1;
936 task->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
937 task->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
939 task->td_flags.started = 1;
940 task->td_flags.executing = 1;
941 task->td_flags.complete = 0;
942 task->td_flags.freed = 0;
945 task->td_depnode = NULL;
947 task->td_last_tied = task;
950 KMP_ATOMIC_ST_REL(&task->td_incomplete_child_tasks, 0);
952 KMP_ATOMIC_ST_REL(&task->td_allocated_child_tasks, 0);
954 task->td_taskgroup = NULL;
955 task->td_dephash = NULL;
957 __kmp_push_current_task_to_thread(this_thr, team, tid);
959 KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0);
960 KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0);
964 if (UNLIKELY(ompt_enabled.enabled))
965 __ompt_task_init(task, tid);
968 KF_TRACE(10, (
"__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n", tid,
977 void __kmp_finish_implicit_task(kmp_info_t *thread) {
978 kmp_taskdata_t *task = thread->th.th_current_task;
979 if (task->td_dephash)
980 __kmp_dephash_free_entries(thread, task->td_dephash);
987 void __kmp_free_implicit_task(kmp_info_t *thread) {
988 kmp_taskdata_t *task = thread->th.th_current_task;
989 if (task && task->td_dephash) {
990 __kmp_dephash_free(thread, task->td_dephash);
991 task->td_dephash = NULL;
997 static size_t __kmp_round_up_to_val(
size_t size,
size_t val) {
998 if (size & (val - 1)) {
1000 if (size <= KMP_SIZE_T_MAX - val) {
1019 kmp_task_t *__kmp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1020 kmp_tasking_flags_t *flags,
1021 size_t sizeof_kmp_task_t,
size_t sizeof_shareds,
1022 kmp_routine_entry_t task_entry) {
1024 kmp_taskdata_t *taskdata;
1025 kmp_info_t *thread = __kmp_threads[gtid];
1026 kmp_team_t *team = thread->th.th_team;
1027 kmp_taskdata_t *parent_task = thread->th.th_current_task;
1028 size_t shareds_offset;
1030 if (!TCR_4(__kmp_init_middle))
1031 __kmp_middle_initialize();
1033 KA_TRACE(10, (
"__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) " 1034 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1035 gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t,
1036 sizeof_shareds, task_entry));
1038 if (parent_task->td_flags.final) {
1039 if (flags->merged_if0) {
1043 if (flags->tiedness == TASK_UNTIED && !team->t.t_serialized) {
1047 KMP_CHECK_UPDATE(thread->th.th_task_team->tt.tt_untied_task_encountered, 1);
1051 if (flags->proxy == TASK_PROXY) {
1052 flags->tiedness = TASK_UNTIED;
1053 flags->merged_if0 = 1;
1057 if ((thread->th.th_task_team) == NULL) {
1060 KMP_DEBUG_ASSERT(team->t.t_serialized);
1062 (
"T#%d creating task team in __kmp_task_alloc for proxy task\n",
1064 __kmp_task_team_setup(
1067 thread->th.th_task_team = team->t.t_task_team[thread->th.th_task_state];
1069 kmp_task_team_t *task_team = thread->th.th_task_team;
1072 if (!KMP_TASKING_ENABLED(task_team)) {
1075 (
"T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid));
1076 __kmp_enable_tasking(task_team, thread);
1077 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
1078 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
1080 if (thread_data->td.td_deque == NULL) {
1081 __kmp_alloc_task_deque(thread, thread_data);
1085 if (task_team->tt.tt_found_proxy_tasks == FALSE)
1086 TCW_4(task_team->tt.tt_found_proxy_tasks, TRUE);
1092 shareds_offset =
sizeof(kmp_taskdata_t) + sizeof_kmp_task_t;
1093 shareds_offset = __kmp_round_up_to_val(shareds_offset,
sizeof(
void *));
1096 KA_TRACE(30, (
"__kmp_task_alloc: T#%d First malloc size: %ld\n", gtid,
1098 KA_TRACE(30, (
"__kmp_task_alloc: T#%d Second malloc size: %ld\n", gtid,
1103 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, shareds_offset +
1106 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, shareds_offset +
1109 ANNOTATE_HAPPENS_AFTER(taskdata);
1111 task = KMP_TASKDATA_TO_TASK(taskdata);
1114 #if KMP_ARCH_X86 || KMP_ARCH_PPC64 || !KMP_HAVE_QUAD 1115 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(
double) - 1)) == 0);
1116 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(
double) - 1)) == 0);
1118 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(_Quad) - 1)) == 0);
1119 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(_Quad) - 1)) == 0);
1121 if (sizeof_shareds > 0) {
1123 task->shareds = &((
char *)taskdata)[shareds_offset];
1125 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
1128 task->shareds = NULL;
1130 task->routine = task_entry;
1133 taskdata->td_task_id = KMP_GEN_TASK_ID();
1134 taskdata->td_team = team;
1135 taskdata->td_alloc_thread = thread;
1136 taskdata->td_parent = parent_task;
1137 taskdata->td_level = parent_task->td_level + 1;
1138 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
1139 taskdata->td_ident = loc_ref;
1140 taskdata->td_taskwait_ident = NULL;
1141 taskdata->td_taskwait_counter = 0;
1142 taskdata->td_taskwait_thread = 0;
1143 KMP_DEBUG_ASSERT(taskdata->td_parent != NULL);
1146 if (flags->proxy == TASK_FULL)
1148 copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs);
1150 taskdata->td_flags.tiedness = flags->tiedness;
1151 taskdata->td_flags.final = flags->final;
1152 taskdata->td_flags.merged_if0 = flags->merged_if0;
1154 taskdata->td_flags.destructors_thunk = flags->destructors_thunk;
1155 #endif // OMP_40_ENABLED 1157 taskdata->td_flags.proxy = flags->proxy;
1158 taskdata->td_task_team = thread->th.th_task_team;
1159 taskdata->td_size_alloc = shareds_offset + sizeof_shareds;
1161 taskdata->td_flags.tasktype = TASK_EXPLICIT;
1164 taskdata->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1167 taskdata->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1173 taskdata->td_flags.task_serial =
1174 (parent_task->td_flags.final || taskdata->td_flags.team_serial ||
1175 taskdata->td_flags.tasking_ser);
1177 taskdata->td_flags.started = 0;
1178 taskdata->td_flags.executing = 0;
1179 taskdata->td_flags.complete = 0;
1180 taskdata->td_flags.freed = 0;
1182 taskdata->td_flags.native = flags->native;
1184 KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0);
1186 KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1);
1188 taskdata->td_taskgroup =
1189 parent_task->td_taskgroup;
1190 taskdata->td_dephash = NULL;
1191 taskdata->td_depnode = NULL;
1193 if (flags->tiedness == TASK_UNTIED)
1194 taskdata->td_last_tied = NULL;
1196 taskdata->td_last_tied = taskdata;
1199 if (UNLIKELY(ompt_enabled.enabled))
1200 __ompt_task_init(taskdata, gtid);
1205 if (flags->proxy == TASK_PROXY ||
1206 !(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser))
1208 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser))
1211 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
1213 if (parent_task->td_taskgroup)
1214 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
1218 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) {
1219 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
1223 KA_TRACE(20, (
"__kmp_task_alloc(exit): T#%d created task %p parent=%p\n",
1224 gtid, taskdata, taskdata->td_parent));
1225 ANNOTATE_HAPPENS_BEFORE(task);
1230 kmp_task_t *__kmpc_omp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1231 kmp_int32 flags,
size_t sizeof_kmp_task_t,
1232 size_t sizeof_shareds,
1233 kmp_routine_entry_t task_entry) {
1235 kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
1237 input_flags->native = FALSE;
1241 KA_TRACE(10, (
"__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s) " 1242 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1243 gtid, loc_ref, input_flags->tiedness ?
"tied " :
"untied",
1244 input_flags->proxy ?
"proxy" :
"", sizeof_kmp_task_t,
1245 sizeof_shareds, task_entry));
1247 KA_TRACE(10, (
"__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s) " 1248 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1249 gtid, loc_ref, input_flags->tiedness ?
"tied " :
"untied",
1250 sizeof_kmp_task_t, sizeof_shareds, task_entry));
1253 retval = __kmp_task_alloc(loc_ref, gtid, input_flags, sizeof_kmp_task_t,
1254 sizeof_shareds, task_entry);
1256 KA_TRACE(20, (
"__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval));
1266 static void __kmp_invoke_task(kmp_int32 gtid, kmp_task_t *task,
1267 kmp_taskdata_t *current_task) {
1268 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
1274 30, (
"__kmp_invoke_task(enter): T#%d invoking task %p, current_task=%p\n",
1275 gtid, taskdata, current_task));
1276 KMP_DEBUG_ASSERT(task);
1278 if (taskdata->td_flags.proxy == TASK_PROXY &&
1279 taskdata->td_flags.complete == 1) {
1284 (
"__kmp_invoke_task: T#%d running bottom finish for proxy task %p\n",
1287 __kmp_bottom_half_finish_proxy(gtid, task);
1289 KA_TRACE(30, (
"__kmp_invoke_task(exit): T#%d completed bottom finish for " 1290 "proxy task %p, resuming task %p\n",
1291 gtid, taskdata, current_task));
1300 ompt_thread_info_t oldInfo;
1301 if (UNLIKELY(ompt_enabled.enabled)) {
1303 thread = __kmp_threads[gtid];
1304 oldInfo = thread->th.ompt_thread_info;
1305 thread->th.ompt_thread_info.wait_id = 0;
1306 thread->th.ompt_thread_info.state = (thread->th.th_team_serialized)
1307 ? omp_state_work_serial
1308 : omp_state_work_parallel;
1309 taskdata->ompt_task_info.frame.exit_frame = OMPT_GET_FRAME_ADDRESS(0);
1315 if (taskdata->td_flags.proxy != TASK_PROXY) {
1317 ANNOTATE_HAPPENS_AFTER(task);
1318 __kmp_task_start(gtid, task, current_task);
1327 if (__kmp_omp_cancellation) {
1328 thread = __kmp_threads[gtid];
1329 kmp_team_t *this_team = thread->th.th_team;
1330 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
1331 if ((taskgroup && taskgroup->cancel_request) ||
1332 (this_team->t.t_cancel_request == cancel_parallel)) {
1333 #if OMPT_SUPPORT && OMPT_OPTIONAL 1334 ompt_data_t *task_data;
1335 if (UNLIKELY(ompt_enabled.ompt_callback_cancel)) {
1336 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, NULL, NULL);
1337 ompt_callbacks.ompt_callback(ompt_callback_cancel)(
1339 ((taskgroup && taskgroup->cancel_request) ? ompt_cancel_taskgroup
1340 : ompt_cancel_parallel) |
1341 ompt_cancel_discarded_task,
1354 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
1355 taskdata->td_last_tied = current_task->td_last_tied;
1356 KMP_DEBUG_ASSERT(taskdata->td_last_tied);
1358 #if KMP_STATS_ENABLED 1360 switch (KMP_GET_THREAD_STATE()) {
1361 case FORK_JOIN_BARRIER:
1362 KMP_PUSH_PARTITIONED_TIMER(OMP_task_join_bar);
1365 KMP_PUSH_PARTITIONED_TIMER(OMP_task_plain_bar);
1368 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskyield);
1371 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskwait);
1374 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskgroup);
1377 KMP_PUSH_PARTITIONED_TIMER(OMP_task_immediate);
1380 #endif // KMP_STATS_ENABLED 1381 #endif // OMP_40_ENABLED 1385 if (UNLIKELY(ompt_enabled.enabled))
1386 __ompt_task_start(task, current_task, gtid);
1389 #if USE_ITT_BUILD && USE_ITT_NOTIFY 1390 kmp_uint64 cur_time;
1391 kmp_int32 kmp_itt_count_task =
1392 __kmp_forkjoin_frames_mode == 3 && !taskdata->td_flags.task_serial &&
1393 current_task->td_flags.tasktype == TASK_IMPLICIT;
1394 if (kmp_itt_count_task) {
1395 thread = __kmp_threads[gtid];
1397 if (thread->th.th_bar_arrive_time)
1398 cur_time = __itt_get_timestamp();
1400 kmp_itt_count_task = 0;
1404 #ifdef KMP_GOMP_COMPAT 1405 if (taskdata->td_flags.native) {
1406 ((void (*)(
void *))(*(task->routine)))(task->shareds);
1410 (*(task->routine))(gtid, task);
1412 KMP_POP_PARTITIONED_TIMER();
1414 #if USE_ITT_BUILD && USE_ITT_NOTIFY 1415 if (kmp_itt_count_task) {
1417 thread->th.th_bar_arrive_time += (__itt_get_timestamp() - cur_time);
1423 #endif // OMP_40_ENABLED 1428 if (taskdata->td_flags.proxy != TASK_PROXY) {
1430 ANNOTATE_HAPPENS_BEFORE(taskdata->td_parent);
1432 if (UNLIKELY(ompt_enabled.enabled)) {
1433 thread->th.ompt_thread_info = oldInfo;
1434 if (taskdata->td_flags.tiedness == TASK_TIED) {
1435 taskdata->ompt_task_info.frame.exit_frame = NULL;
1437 __kmp_task_finish<true>(gtid, task, current_task);
1440 __kmp_task_finish<false>(gtid, task, current_task);
1447 (
"__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n",
1448 gtid, taskdata, current_task));
1462 kmp_int32 __kmpc_omp_task_parts(
ident_t *loc_ref, kmp_int32 gtid,
1463 kmp_task_t *new_task) {
1464 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1466 KA_TRACE(10, (
"__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n", gtid,
1467 loc_ref, new_taskdata));
1470 kmp_taskdata_t *parent;
1471 if (UNLIKELY(ompt_enabled.enabled)) {
1472 parent = new_taskdata->td_parent;
1473 if (ompt_enabled.ompt_callback_task_create) {
1474 ompt_data_t task_data = ompt_data_none;
1475 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1476 parent ? &(parent->ompt_task_info.task_data) : &task_data,
1477 parent ? &(parent->ompt_task_info.frame) : NULL,
1478 &(new_taskdata->ompt_task_info.task_data), ompt_task_explicit, 0,
1479 OMPT_GET_RETURN_ADDRESS(0));
1487 if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1489 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1490 new_taskdata->td_flags.task_serial = 1;
1491 __kmp_invoke_task(gtid, new_task, current_task);
1496 (
"__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: " 1497 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
1498 gtid, loc_ref, new_taskdata));
1500 ANNOTATE_HAPPENS_BEFORE(new_task);
1502 if (UNLIKELY(ompt_enabled.enabled)) {
1503 parent->ompt_task_info.frame.enter_frame = NULL;
1506 return TASK_CURRENT_NOT_QUEUED;
1520 kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task,
1521 bool serialize_immediate) {
1522 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1527 if (new_taskdata->td_flags.proxy == TASK_PROXY ||
1528 __kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1530 if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1533 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1534 if (serialize_immediate)
1535 new_taskdata->td_flags.task_serial = 1;
1536 __kmp_invoke_task(gtid, new_task, current_task);
1539 ANNOTATE_HAPPENS_BEFORE(new_task);
1540 return TASK_CURRENT_NOT_QUEUED;
1555 kmp_int32 __kmpc_omp_task(
ident_t *loc_ref, kmp_int32 gtid,
1556 kmp_task_t *new_task) {
1558 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1560 #if KMP_DEBUG || OMPT_SUPPORT 1561 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1563 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1567 kmp_taskdata_t *parent = NULL;
1568 if (UNLIKELY(ompt_enabled.enabled)) {
1569 if (!new_taskdata->td_flags.started) {
1570 OMPT_STORE_RETURN_ADDRESS(gtid);
1571 parent = new_taskdata->td_parent;
1572 if (!parent->ompt_task_info.frame.enter_frame) {
1573 parent->ompt_task_info.frame.enter_frame = OMPT_GET_FRAME_ADDRESS(1);
1575 if (ompt_enabled.ompt_callback_task_create) {
1576 ompt_data_t task_data = ompt_data_none;
1577 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1578 parent ? &(parent->ompt_task_info.task_data) : &task_data,
1579 parent ? &(parent->ompt_task_info.frame) : NULL,
1580 &(new_taskdata->ompt_task_info.task_data),
1581 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1582 OMPT_LOAD_RETURN_ADDRESS(gtid));
1587 __ompt_task_finish(new_task,
1588 new_taskdata->ompt_task_info.scheduling_parent,
1590 new_taskdata->ompt_task_info.frame.exit_frame = NULL;
1595 res = __kmp_omp_task(gtid, new_task,
true);
1597 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning " 1598 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1599 gtid, loc_ref, new_taskdata));
1601 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
1602 parent->ompt_task_info.frame.enter_frame = NULL;
1621 kmp_int32 __kmp_omp_taskloop_task(
ident_t *loc_ref, kmp_int32 gtid,
1622 kmp_task_t *new_task,
void *codeptr_ra) {
1624 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1626 #if KMP_DEBUG || OMPT_SUPPORT 1627 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1629 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1633 kmp_taskdata_t *parent = NULL;
1634 if (UNLIKELY(ompt_enabled.enabled && !new_taskdata->td_flags.started)) {
1635 parent = new_taskdata->td_parent;
1636 if (!parent->ompt_task_info.frame.enter_frame)
1637 parent->ompt_task_info.frame.enter_frame = OMPT_GET_FRAME_ADDRESS(1);
1638 if (ompt_enabled.ompt_callback_task_create) {
1639 ompt_data_t task_data = ompt_data_none;
1640 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1641 parent ? &(parent->ompt_task_info.task_data) : &task_data,
1642 parent ? &(parent->ompt_task_info.frame) : NULL,
1643 &(new_taskdata->ompt_task_info.task_data),
1644 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1650 res = __kmp_omp_task(gtid, new_task,
true);
1652 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning " 1653 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1654 gtid, loc_ref, new_taskdata));
1656 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
1657 parent->ompt_task_info.frame.enter_frame = NULL;
1663 template <
bool ompt>
1664 static kmp_int32 __kmpc_omp_taskwait_template(
ident_t *loc_ref, kmp_int32 gtid,
1665 void *frame_address,
1666 void *return_address) {
1667 kmp_taskdata_t *taskdata;
1669 int thread_finished = FALSE;
1670 KMP_SET_THREAD_STATE_BLOCK(TASKWAIT);
1672 KA_TRACE(10, (
"__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref));
1674 if (__kmp_tasking_mode != tskm_immediate_exec) {
1675 thread = __kmp_threads[gtid];
1676 taskdata = thread->th.th_current_task;
1678 #if OMPT_SUPPORT && OMPT_OPTIONAL 1679 ompt_data_t *my_task_data;
1680 ompt_data_t *my_parallel_data;
1683 my_task_data = &(taskdata->ompt_task_info.task_data);
1684 my_parallel_data = OMPT_CUR_TEAM_DATA(thread);
1686 taskdata->ompt_task_info.frame.enter_frame = frame_address;
1688 if (ompt_enabled.ompt_callback_sync_region) {
1689 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
1690 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
1691 my_task_data, return_address);
1694 if (ompt_enabled.ompt_callback_sync_region_wait) {
1695 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
1696 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
1697 my_task_data, return_address);
1700 #endif // OMPT_SUPPORT && OMPT_OPTIONAL 1707 taskdata->td_taskwait_counter += 1;
1708 taskdata->td_taskwait_ident = loc_ref;
1709 taskdata->td_taskwait_thread = gtid + 1;
1712 void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
1713 if (itt_sync_obj != NULL)
1714 __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
1718 !taskdata->td_flags.team_serial && !taskdata->td_flags.final;
1721 must_wait = must_wait || (thread->th.th_task_team != NULL &&
1722 thread->th.th_task_team->tt.tt_found_proxy_tasks);
1725 kmp_flag_32 flag(RCAST(std::atomic<kmp_uint32> *,
1726 &(taskdata->td_incomplete_child_tasks)),
1728 while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) != 0) {
1729 flag.execute_tasks(thread, gtid, FALSE,
1730 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
1731 __kmp_task_stealing_constraint);
1735 if (itt_sync_obj != NULL)
1736 __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
1741 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
1743 #if OMPT_SUPPORT && OMPT_OPTIONAL 1745 if (ompt_enabled.ompt_callback_sync_region_wait) {
1746 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
1747 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
1748 my_task_data, return_address);
1750 if (ompt_enabled.ompt_callback_sync_region) {
1751 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
1752 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
1753 my_task_data, return_address);
1755 taskdata->ompt_task_info.frame.enter_frame = NULL;
1757 #endif // OMPT_SUPPORT && OMPT_OPTIONAL 1759 ANNOTATE_HAPPENS_AFTER(taskdata);
1762 KA_TRACE(10, (
"__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, " 1763 "returning TASK_CURRENT_NOT_QUEUED\n",
1766 return TASK_CURRENT_NOT_QUEUED;
1769 #if OMPT_SUPPORT && OMPT_OPTIONAL 1771 static kmp_int32 __kmpc_omp_taskwait_ompt(
ident_t *loc_ref, kmp_int32 gtid,
1772 void *frame_address,
1773 void *return_address) {
1774 return __kmpc_omp_taskwait_template<true>(loc_ref, gtid, frame_address,
1777 #endif // OMPT_SUPPORT && OMPT_OPTIONAL 1781 kmp_int32 __kmpc_omp_taskwait(
ident_t *loc_ref, kmp_int32 gtid) {
1782 #if OMPT_SUPPORT && OMPT_OPTIONAL 1783 if (UNLIKELY(ompt_enabled.enabled)) {
1784 OMPT_STORE_RETURN_ADDRESS(gtid);
1785 return __kmpc_omp_taskwait_ompt(loc_ref, gtid, OMPT_GET_FRAME_ADDRESS(1),
1786 OMPT_LOAD_RETURN_ADDRESS(gtid));
1789 return __kmpc_omp_taskwait_template<false>(loc_ref, gtid, NULL, NULL);
1793 kmp_int32 __kmpc_omp_taskyield(
ident_t *loc_ref, kmp_int32 gtid,
int end_part) {
1794 kmp_taskdata_t *taskdata;
1796 int thread_finished = FALSE;
1799 KMP_SET_THREAD_STATE_BLOCK(TASKYIELD);
1801 KA_TRACE(10, (
"__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n",
1802 gtid, loc_ref, end_part));
1804 if (__kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel) {
1805 thread = __kmp_threads[gtid];
1806 taskdata = thread->th.th_current_task;
1813 taskdata->td_taskwait_counter += 1;
1814 taskdata->td_taskwait_ident = loc_ref;
1815 taskdata->td_taskwait_thread = gtid + 1;
1818 void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
1819 if (itt_sync_obj != NULL)
1820 __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
1822 if (!taskdata->td_flags.team_serial) {
1823 kmp_task_team_t *task_team = thread->th.th_task_team;
1824 if (task_team != NULL) {
1825 if (KMP_TASKING_ENABLED(task_team)) {
1827 if (UNLIKELY(ompt_enabled.enabled))
1828 thread->th.ompt_thread_info.ompt_task_yielded = 1;
1830 __kmp_execute_tasks_32(
1831 thread, gtid, NULL, FALSE,
1832 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
1833 __kmp_task_stealing_constraint);
1835 if (UNLIKELY(ompt_enabled.enabled))
1836 thread->th.ompt_thread_info.ompt_task_yielded = 0;
1842 if (itt_sync_obj != NULL)
1843 __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
1848 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
1851 KA_TRACE(10, (
"__kmpc_omp_taskyield(exit): T#%d task %p resuming, " 1852 "returning TASK_CURRENT_NOT_QUEUED\n",
1855 return TASK_CURRENT_NOT_QUEUED;
1862 typedef struct kmp_task_red_flags {
1863 unsigned lazy_priv : 1;
1864 unsigned reserved31 : 31;
1865 } kmp_task_red_flags_t;
1868 typedef struct kmp_task_red_data {
1876 kmp_task_red_flags_t flags;
1877 } kmp_task_red_data_t;
1880 typedef struct kmp_task_red_input {
1886 kmp_task_red_flags_t flags;
1887 } kmp_task_red_input_t;
1898 void *__kmpc_task_reduction_init(
int gtid,
int num,
void *data) {
1899 kmp_info_t *thread = __kmp_threads[gtid];
1900 kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup;
1901 kmp_int32 nth = thread->th.th_team_nproc;
1902 kmp_task_red_input_t *input = (kmp_task_red_input_t *)data;
1903 kmp_task_red_data_t *arr;
1906 KMP_ASSERT(tg != NULL);
1907 KMP_ASSERT(data != NULL);
1908 KMP_ASSERT(num > 0);
1910 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n",
1914 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, taskgroup %p, #items %d\n",
1916 arr = (kmp_task_red_data_t *)__kmp_thread_malloc(
1917 thread, num *
sizeof(kmp_task_red_data_t));
1918 for (
int i = 0; i < num; ++i) {
1919 void (*f_init)(
void *) = (
void (*)(
void *))(input[i].reduce_init);
1920 size_t size = input[i].reduce_size - 1;
1922 size += CACHE_LINE - size % CACHE_LINE;
1923 KMP_ASSERT(input[i].reduce_comb != NULL);
1924 arr[i].reduce_shar = input[i].reduce_shar;
1925 arr[i].reduce_size = size;
1926 arr[i].reduce_init = input[i].reduce_init;
1927 arr[i].reduce_fini = input[i].reduce_fini;
1928 arr[i].reduce_comb = input[i].reduce_comb;
1929 arr[i].flags = input[i].flags;
1930 if (!input[i].flags.lazy_priv) {
1932 arr[i].reduce_priv = __kmp_allocate(nth * size);
1933 arr[i].reduce_pend = (
char *)(arr[i].reduce_priv) + nth * size;
1934 if (f_init != NULL) {
1936 for (
int j = 0; j < nth; ++j) {
1937 f_init((
char *)(arr[i].reduce_priv) + j * size);
1943 arr[i].reduce_priv = __kmp_allocate(nth *
sizeof(
void *));
1946 tg->reduce_data = (
void *)arr;
1947 tg->reduce_num_data = num;
1960 void *__kmpc_task_reduction_get_th_data(
int gtid,
void *tskgrp,
void *data) {
1961 kmp_info_t *thread = __kmp_threads[gtid];
1962 kmp_int32 nth = thread->th.th_team_nproc;
1966 kmp_taskgroup_t *tg = (kmp_taskgroup_t *)tskgrp;
1968 tg = thread->th.th_current_task->td_taskgroup;
1969 KMP_ASSERT(tg != NULL);
1970 kmp_task_red_data_t *arr = (kmp_task_red_data_t *)(tg->reduce_data);
1971 kmp_int32 num = tg->reduce_num_data;
1972 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
1974 KMP_ASSERT(data != NULL);
1975 while (tg != NULL) {
1976 for (
int i = 0; i < num; ++i) {
1977 if (!arr[i].flags.lazy_priv) {
1978 if (data == arr[i].reduce_shar ||
1979 (data >= arr[i].reduce_priv && data < arr[i].reduce_pend))
1980 return (
char *)(arr[i].reduce_priv) + tid * arr[i].reduce_size;
1983 void **p_priv = (
void **)(arr[i].reduce_priv);
1984 if (data == arr[i].reduce_shar)
1987 for (
int j = 0; j < nth; ++j)
1988 if (data == p_priv[j])
1992 if (p_priv[tid] == NULL) {
1994 void (*f_init)(
void *) = (
void (*)(
void *))(arr[i].reduce_init);
1995 p_priv[tid] = __kmp_allocate(arr[i].reduce_size);
1996 if (f_init != NULL) {
1997 f_init(p_priv[tid]);
2004 arr = (kmp_task_red_data_t *)(tg->reduce_data);
2005 num = tg->reduce_num_data;
2007 KMP_ASSERT2(0,
"Unknown task reduction item");
2013 static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg) {
2014 kmp_int32 nth = th->th.th_team_nproc;
2015 KMP_DEBUG_ASSERT(nth > 1);
2016 kmp_task_red_data_t *arr = (kmp_task_red_data_t *)tg->reduce_data;
2017 kmp_int32 num = tg->reduce_num_data;
2018 for (
int i = 0; i < num; ++i) {
2019 void *sh_data = arr[i].reduce_shar;
2020 void (*f_fini)(
void *) = (
void (*)(
void *))(arr[i].reduce_fini);
2021 void (*f_comb)(
void *,
void *) =
2022 (
void (*)(
void *,
void *))(arr[i].reduce_comb);
2023 if (!arr[i].flags.lazy_priv) {
2024 void *pr_data = arr[i].reduce_priv;
2025 size_t size = arr[i].reduce_size;
2026 for (
int j = 0; j < nth; ++j) {
2027 void *priv_data = (
char *)pr_data + j * size;
2028 f_comb(sh_data, priv_data);
2033 void **pr_data = (
void **)(arr[i].reduce_priv);
2034 for (
int j = 0; j < nth; ++j) {
2035 if (pr_data[j] != NULL) {
2036 f_comb(sh_data, pr_data[j]);
2039 __kmp_free(pr_data[j]);
2043 __kmp_free(arr[i].reduce_priv);
2045 __kmp_thread_free(th, arr);
2046 tg->reduce_data = NULL;
2047 tg->reduce_num_data = 0;
2053 void __kmpc_taskgroup(
ident_t *loc,
int gtid) {
2054 kmp_info_t *thread = __kmp_threads[gtid];
2055 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2056 kmp_taskgroup_t *tg_new =
2057 (kmp_taskgroup_t *)__kmp_thread_malloc(thread,
sizeof(kmp_taskgroup_t));
2058 KA_TRACE(10, (
"__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new));
2059 KMP_ATOMIC_ST_RLX(&tg_new->count, 0);
2060 KMP_ATOMIC_ST_RLX(&tg_new->cancel_request, cancel_noreq);
2061 tg_new->parent = taskdata->td_taskgroup;
2064 tg_new->reduce_data = NULL;
2065 tg_new->reduce_num_data = 0;
2067 taskdata->td_taskgroup = tg_new;
2069 #if OMPT_SUPPORT && OMPT_OPTIONAL 2070 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2071 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2073 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2074 kmp_team_t *team = thread->th.th_team;
2075 ompt_data_t my_task_data = taskdata->ompt_task_info.task_data;
2077 ompt_data_t my_parallel_data = team->t.ompt_team_info.parallel_data;
2079 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2080 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2081 &(my_task_data), codeptr);
2088 void __kmpc_end_taskgroup(
ident_t *loc,
int gtid) {
2089 kmp_info_t *thread = __kmp_threads[gtid];
2090 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2091 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
2092 int thread_finished = FALSE;
2094 #if OMPT_SUPPORT && OMPT_OPTIONAL 2096 ompt_data_t my_task_data;
2097 ompt_data_t my_parallel_data;
2099 if (UNLIKELY(ompt_enabled.enabled)) {
2100 team = thread->th.th_team;
2101 my_task_data = taskdata->ompt_task_info.task_data;
2103 my_parallel_data = team->t.ompt_team_info.parallel_data;
2104 codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2106 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2110 KA_TRACE(10, (
"__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc));
2111 KMP_DEBUG_ASSERT(taskgroup != NULL);
2112 KMP_SET_THREAD_STATE_BLOCK(TASKGROUP);
2114 if (__kmp_tasking_mode != tskm_immediate_exec) {
2116 taskdata->td_taskwait_counter += 1;
2117 taskdata->td_taskwait_ident = loc;
2118 taskdata->td_taskwait_thread = gtid + 1;
2122 void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
2123 if (itt_sync_obj != NULL)
2124 __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
2127 #if OMPT_SUPPORT && OMPT_OPTIONAL 2128 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2129 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2130 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2131 &(my_task_data), codeptr);
2136 if (!taskdata->td_flags.team_serial ||
2137 (thread->th.th_task_team != NULL &&
2138 thread->th.th_task_team->tt.tt_found_proxy_tasks))
2140 if (!taskdata->td_flags.team_serial)
2143 kmp_flag_32 flag(RCAST(std::atomic<kmp_uint32> *, &(taskgroup->count)),
2145 while (KMP_ATOMIC_LD_ACQ(&taskgroup->count) != 0) {
2146 flag.execute_tasks(thread, gtid, FALSE,
2147 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2148 __kmp_task_stealing_constraint);
2151 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2153 #if OMPT_SUPPORT && OMPT_OPTIONAL 2154 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2155 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2156 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2157 &(my_task_data), codeptr);
2162 if (itt_sync_obj != NULL)
2163 __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
2166 KMP_DEBUG_ASSERT(taskgroup->count == 0);
2170 if (taskgroup->reduce_data != NULL)
2171 __kmp_task_reduction_fini(thread, taskgroup);
2174 taskdata->td_taskgroup = taskgroup->parent;
2175 __kmp_thread_free(thread, taskgroup);
2177 KA_TRACE(10, (
"__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n",
2179 ANNOTATE_HAPPENS_AFTER(taskdata);
2181 #if OMPT_SUPPORT && OMPT_OPTIONAL 2182 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2183 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2184 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2185 &(my_task_data), codeptr);
2192 static kmp_task_t *__kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid,
2193 kmp_task_team_t *task_team,
2194 kmp_int32 is_constrained) {
2196 kmp_taskdata_t *taskdata;
2197 kmp_thread_data_t *thread_data;
2200 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2201 KMP_DEBUG_ASSERT(task_team->tt.tt_threads_data !=
2204 thread_data = &task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
2206 KA_TRACE(10, (
"__kmp_remove_my_task(enter): T#%d ntasks=%d head=%u tail=%u\n",
2207 gtid, thread_data->td.td_deque_ntasks,
2208 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2210 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2212 (
"__kmp_remove_my_task(exit #1): T#%d No tasks to remove: " 2213 "ntasks=%d head=%u tail=%u\n",
2214 gtid, thread_data->td.td_deque_ntasks,
2215 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2219 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
2221 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2222 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2224 (
"__kmp_remove_my_task(exit #2): T#%d No tasks to remove: " 2225 "ntasks=%d head=%u tail=%u\n",
2226 gtid, thread_data->td.td_deque_ntasks,
2227 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2231 tail = (thread_data->td.td_deque_tail - 1) &
2232 TASK_DEQUE_MASK(thread_data->td);
2233 taskdata = thread_data->td.td_deque[tail];
2235 if (is_constrained && (taskdata->td_flags.tiedness == TASK_TIED)) {
2239 kmp_taskdata_t *current = thread->th.th_current_task->td_last_tied;
2240 KMP_DEBUG_ASSERT(current != NULL);
2242 if (current->td_flags.tasktype == TASK_EXPLICIT ||
2243 current->td_taskwait_thread > 0) {
2244 kmp_int32 level = current->td_level;
2245 kmp_taskdata_t *parent = taskdata->td_parent;
2246 while (parent != current && parent->td_level > level) {
2247 parent = parent->td_parent;
2249 KMP_DEBUG_ASSERT(parent != NULL);
2251 if (parent != current) {
2253 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2254 KA_TRACE(10, (
"__kmp_remove_my_task(exit #2): T#%d No tasks to remove: " 2255 "ntasks=%d head=%u tail=%u\n",
2256 gtid, thread_data->td.td_deque_ntasks,
2257 thread_data->td.td_deque_head,
2258 thread_data->td.td_deque_tail));
2264 thread_data->td.td_deque_tail = tail;
2265 TCW_4(thread_data->td.td_deque_ntasks, thread_data->td.td_deque_ntasks - 1);
2267 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2269 KA_TRACE(10, (
"__kmp_remove_my_task(exit #2): T#%d task %p removed: " 2270 "ntasks=%d head=%u tail=%u\n",
2271 gtid, taskdata, thread_data->td.td_deque_ntasks,
2272 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2274 task = KMP_TASKDATA_TO_TASK(taskdata);
2281 static kmp_task_t *__kmp_steal_task(kmp_info_t *victim_thr, kmp_int32 gtid,
2282 kmp_task_team_t *task_team,
2283 std::atomic<kmp_int32> *unfinished_threads,
2284 int *thread_finished,
2285 kmp_int32 is_constrained) {
2287 kmp_taskdata_t *taskdata;
2288 kmp_taskdata_t *current;
2289 kmp_thread_data_t *victim_td, *threads_data;
2290 kmp_int32 level, target;
2291 kmp_int32 victim_tid;
2293 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2295 threads_data = task_team->tt.tt_threads_data;
2296 KMP_DEBUG_ASSERT(threads_data != NULL);
2298 victim_tid = victim_thr->th.th_info.ds.ds_tid;
2299 victim_td = &threads_data[victim_tid];
2301 KA_TRACE(10, (
"__kmp_steal_task(enter): T#%d try to steal from T#%d: " 2302 "task_team=%p ntasks=%d head=%u tail=%u\n",
2303 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
2304 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
2305 victim_td->td.td_deque_tail));
2307 if (TCR_4(victim_td->td.td_deque_ntasks) == 0) {
2308 KA_TRACE(10, (
"__kmp_steal_task(exit #1): T#%d could not steal from T#%d: " 2309 "task_team=%p ntasks=%d head=%u tail=%u\n",
2310 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
2311 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
2312 victim_td->td.td_deque_tail));
2316 __kmp_acquire_bootstrap_lock(&victim_td->td.td_deque_lock);
2318 int ntasks = TCR_4(victim_td->td.td_deque_ntasks);
2321 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2322 KA_TRACE(10, (
"__kmp_steal_task(exit #2): T#%d could not steal from T#%d: " 2323 "task_team=%p ntasks=%d head=%u tail=%u\n",
2324 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2325 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2329 KMP_DEBUG_ASSERT(victim_td->td.td_deque != NULL);
2331 taskdata = victim_td->td.td_deque[victim_td->td.td_deque_head];
2332 if (is_constrained && (taskdata->td_flags.tiedness == TASK_TIED)) {
2336 current = __kmp_threads[gtid]->th.th_current_task->td_last_tied;
2337 KMP_DEBUG_ASSERT(current != NULL);
2339 if (current->td_flags.tasktype == TASK_EXPLICIT ||
2340 current->td_taskwait_thread > 0) {
2341 level = current->td_level;
2342 kmp_taskdata_t *parent = taskdata->td_parent;
2343 while (parent != current && parent->td_level > level) {
2344 parent = parent->td_parent;
2346 KMP_DEBUG_ASSERT(parent != NULL);
2348 if (parent != current) {
2349 if (!task_team->tt.tt_untied_task_encountered) {
2351 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2353 (
"__kmp_steal_task(exit #3): T#%d could not steal from " 2354 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
2355 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2356 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2363 if (taskdata != NULL) {
2365 victim_td->td.td_deque_head =
2366 (victim_td->td.td_deque_head + 1) & TASK_DEQUE_MASK(victim_td->td);
2370 target = victim_td->td.td_deque_head;
2371 for (i = 1; i < ntasks; ++i) {
2372 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
2373 taskdata = victim_td->td.td_deque[target];
2374 if (taskdata->td_flags.tiedness == TASK_TIED) {
2376 kmp_taskdata_t *parent = taskdata->td_parent;
2378 while (parent != current && parent->td_level > level) {
2379 parent = parent->td_parent;
2380 KMP_DEBUG_ASSERT(parent != NULL);
2382 if (parent != current) {
2395 if (taskdata == NULL) {
2397 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2398 KA_TRACE(10, (
"__kmp_steal_task(exit #4): T#%d could not steal from " 2399 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
2400 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2401 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2405 for (i = i + 1; i < ntasks; ++i) {
2407 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
2408 victim_td->td.td_deque[prev] = victim_td->td.td_deque[target];
2412 victim_td->td.td_deque_tail ==
2413 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(victim_td->td)));
2414 victim_td->td.td_deque_tail = target;
2416 if (*thread_finished) {
2422 count = KMP_ATOMIC_INC(unfinished_threads);
2426 (
"__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n",
2427 gtid, count + 1, task_team));
2429 *thread_finished = FALSE;
2431 TCW_4(victim_td->td.td_deque_ntasks, ntasks - 1);
2433 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2437 (
"__kmp_steal_task(exit #5): T#%d stole task %p from T#%d: " 2438 "task_team=%p ntasks=%d head=%u tail=%u\n",
2439 gtid, taskdata, __kmp_gtid_from_thread(victim_thr), task_team,
2440 ntasks, victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2442 task = KMP_TASKDATA_TO_TASK(taskdata);
2456 static inline int __kmp_execute_tasks_template(
2457 kmp_info_t *thread, kmp_int32 gtid, C *flag,
int final_spin,
2458 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
2459 kmp_int32 is_constrained) {
2460 kmp_task_team_t *task_team = thread->th.th_task_team;
2461 kmp_thread_data_t *threads_data;
2463 kmp_info_t *other_thread;
2464 kmp_taskdata_t *current_task = thread->th.th_current_task;
2465 std::atomic<kmp_int32> *unfinished_threads;
2466 kmp_int32 nthreads, victim_tid = -2, use_own_tasks = 1, new_victim = 0,
2467 tid = thread->th.th_info.ds.ds_tid;
2469 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2470 KMP_DEBUG_ASSERT(thread == __kmp_threads[gtid]);
2472 if (task_team == NULL || current_task == NULL)
2475 KA_TRACE(15, (
"__kmp_execute_tasks_template(enter): T#%d final_spin=%d " 2476 "*thread_finished=%d\n",
2477 gtid, final_spin, *thread_finished));
2479 thread->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
2480 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
2481 KMP_DEBUG_ASSERT(threads_data != NULL);
2483 nthreads = task_team->tt.tt_nproc;
2484 unfinished_threads = &(task_team->tt.tt_unfinished_threads);
2486 KMP_DEBUG_ASSERT(nthreads > 1 || task_team->tt.tt_found_proxy_tasks);
2488 KMP_DEBUG_ASSERT(nthreads > 1);
2490 KMP_DEBUG_ASSERT(*unfinished_threads >= 0);
2496 if (use_own_tasks) {
2497 task = __kmp_remove_my_task(thread, gtid, task_team, is_constrained);
2499 if ((task == NULL) && (nthreads > 1)) {
2503 if (victim_tid == -2) {
2504 victim_tid = threads_data[tid].td.td_deque_last_stolen;
2507 other_thread = threads_data[victim_tid].td.td_thr;
2509 if (victim_tid != -1) {
2511 }
else if (!new_victim) {
2517 victim_tid = __kmp_get_random(thread) % (nthreads - 1);
2518 if (victim_tid >= tid) {
2522 other_thread = threads_data[victim_tid].td.td_thr;
2532 if ((__kmp_tasking_mode == tskm_task_teams) &&
2533 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) &&
2534 (TCR_PTR(CCAST(
void *, other_thread->th.th_sleep_loc)) !=
2537 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(other_thread),
2538 other_thread->th.th_sleep_loc);
2551 task = __kmp_steal_task(other_thread, gtid, task_team,
2552 unfinished_threads, thread_finished,
2556 if (threads_data[tid].td.td_deque_last_stolen != victim_tid) {
2557 threads_data[tid].td.td_deque_last_stolen = victim_tid;
2564 KMP_CHECK_UPDATE(threads_data[tid].td.td_deque_last_stolen, -1);
2573 #if USE_ITT_BUILD && USE_ITT_NOTIFY 2574 if (__itt_sync_create_ptr || KMP_ITT_DEBUG) {
2575 if (itt_sync_obj == NULL) {
2577 itt_sync_obj = __kmp_itt_barrier_object(gtid, bs_forkjoin_barrier);
2579 __kmp_itt_task_starting(itt_sync_obj);
2582 __kmp_invoke_task(gtid, task, current_task);
2584 if (itt_sync_obj != NULL)
2585 __kmp_itt_task_finished(itt_sync_obj);
2592 if (flag == NULL || (!final_spin && flag->done_check())) {
2595 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
2599 if (thread->th.th_task_team == NULL) {
2603 KMP_YIELD(__kmp_library == library_throughput);
2606 if (!use_own_tasks && TCR_4(threads_data[tid].td.td_deque_ntasks) != 0) {
2607 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d stolen task spawned " 2608 "other tasks, restart\n",
2621 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks) == 0)
2629 if (!*thread_finished) {
2632 count = KMP_ATOMIC_DEC(unfinished_threads) - 1;
2633 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d dec " 2634 "unfinished_threads to %d task_team=%p\n",
2635 gtid, count, task_team));
2636 *thread_finished = TRUE;
2644 if (flag != NULL && flag->done_check()) {
2647 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
2655 if (thread->th.th_task_team == NULL) {
2657 (
"__kmp_execute_tasks_template: T#%d no more tasks\n", gtid));
2670 (
"__kmp_execute_tasks_template: T#%d can't find work\n", gtid));
2676 int __kmp_execute_tasks_32(
2677 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32 *flag,
int final_spin,
2678 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
2679 kmp_int32 is_constrained) {
2680 return __kmp_execute_tasks_template(
2681 thread, gtid, flag, final_spin,
2682 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
2685 int __kmp_execute_tasks_64(
2686 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64 *flag,
int final_spin,
2687 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
2688 kmp_int32 is_constrained) {
2689 return __kmp_execute_tasks_template(
2690 thread, gtid, flag, final_spin,
2691 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
2694 int __kmp_execute_tasks_oncore(
2695 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag,
int final_spin,
2696 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
2697 kmp_int32 is_constrained) {
2698 return __kmp_execute_tasks_template(
2699 thread, gtid, flag, final_spin,
2700 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
2706 static void __kmp_enable_tasking(kmp_task_team_t *task_team,
2707 kmp_info_t *this_thr) {
2708 kmp_thread_data_t *threads_data;
2709 int nthreads, i, is_init_thread;
2711 KA_TRACE(10, (
"__kmp_enable_tasking(enter): T#%d\n",
2712 __kmp_gtid_from_thread(this_thr)));
2714 KMP_DEBUG_ASSERT(task_team != NULL);
2715 KMP_DEBUG_ASSERT(this_thr->th.th_team != NULL);
2717 nthreads = task_team->tt.tt_nproc;
2718 KMP_DEBUG_ASSERT(nthreads > 0);
2719 KMP_DEBUG_ASSERT(nthreads == this_thr->th.th_team->t.t_nproc);
2722 is_init_thread = __kmp_realloc_task_threads_data(this_thr, task_team);
2724 if (!is_init_thread) {
2728 (
"__kmp_enable_tasking(exit): T#%d: threads array already set up.\n",
2729 __kmp_gtid_from_thread(this_thr)));
2732 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
2733 KMP_DEBUG_ASSERT(threads_data != NULL);
2735 if ((__kmp_tasking_mode == tskm_task_teams) &&
2736 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME)) {
2740 for (i = 0; i < nthreads; i++) {
2741 volatile void *sleep_loc;
2742 kmp_info_t *thread = threads_data[i].td.td_thr;
2744 if (i == this_thr->th.th_info.ds.ds_tid) {
2753 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
2755 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d waking up thread T#%d\n",
2756 __kmp_gtid_from_thread(this_thr),
2757 __kmp_gtid_from_thread(thread)));
2758 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc);
2760 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d don't wake up thread T#%d\n",
2761 __kmp_gtid_from_thread(this_thr),
2762 __kmp_gtid_from_thread(thread)));
2767 KA_TRACE(10, (
"__kmp_enable_tasking(exit): T#%d\n",
2768 __kmp_gtid_from_thread(this_thr)));
2805 static kmp_task_team_t *__kmp_free_task_teams =
2808 kmp_bootstrap_lock_t __kmp_task_team_lock =
2809 KMP_BOOTSTRAP_LOCK_INITIALIZER(__kmp_task_team_lock);
2816 static void __kmp_alloc_task_deque(kmp_info_t *thread,
2817 kmp_thread_data_t *thread_data) {
2818 __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
2819 KMP_DEBUG_ASSERT(thread_data->td.td_deque == NULL);
2822 thread_data->td.td_deque_last_stolen = -1;
2824 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == 0);
2825 KMP_DEBUG_ASSERT(thread_data->td.td_deque_head == 0);
2826 KMP_DEBUG_ASSERT(thread_data->td.td_deque_tail == 0);
2830 (
"__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n",
2831 __kmp_gtid_from_thread(thread), INITIAL_TASK_DEQUE_SIZE, thread_data));
2835 thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
2836 INITIAL_TASK_DEQUE_SIZE *
sizeof(kmp_taskdata_t *));
2837 thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
2844 static void __kmp_realloc_task_deque(kmp_info_t *thread,
2845 kmp_thread_data_t *thread_data) {
2846 kmp_int32 size = TASK_DEQUE_SIZE(thread_data->td);
2847 kmp_int32 new_size = 2 * size;
2849 KE_TRACE(10, (
"__kmp_realloc_task_deque: T#%d reallocating deque[from %d to " 2850 "%d] for thread_data %p\n",
2851 __kmp_gtid_from_thread(thread), size, new_size, thread_data));
2853 kmp_taskdata_t **new_deque =
2854 (kmp_taskdata_t **)__kmp_allocate(new_size *
sizeof(kmp_taskdata_t *));
2857 for (i = thread_data->td.td_deque_head, j = 0; j < size;
2858 i = (i + 1) & TASK_DEQUE_MASK(thread_data->td), j++)
2859 new_deque[j] = thread_data->td.td_deque[i];
2861 __kmp_free(thread_data->td.td_deque);
2863 thread_data->td.td_deque_head = 0;
2864 thread_data->td.td_deque_tail = size;
2865 thread_data->td.td_deque = new_deque;
2866 thread_data->td.td_deque_size = new_size;
2872 static void __kmp_free_task_deque(kmp_thread_data_t *thread_data) {
2873 if (thread_data->td.td_deque != NULL) {
2874 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
2875 TCW_4(thread_data->td.td_deque_ntasks, 0);
2876 __kmp_free(thread_data->td.td_deque);
2877 thread_data->td.td_deque = NULL;
2878 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2881 #ifdef BUILD_TIED_TASK_STACK 2883 if (thread_data->td.td_susp_tied_tasks.ts_entries != TASK_STACK_EMPTY) {
2884 __kmp_free_task_stack(__kmp_thread_from_gtid(gtid), thread_data);
2886 #endif // BUILD_TIED_TASK_STACK 2896 static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
2897 kmp_task_team_t *task_team) {
2898 kmp_thread_data_t **threads_data_p;
2899 kmp_int32 nthreads, maxthreads;
2900 int is_init_thread = FALSE;
2902 if (TCR_4(task_team->tt.tt_found_tasks)) {
2907 threads_data_p = &task_team->tt.tt_threads_data;
2908 nthreads = task_team->tt.tt_nproc;
2909 maxthreads = task_team->tt.tt_max_threads;
2914 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
2916 if (!TCR_4(task_team->tt.tt_found_tasks)) {
2918 kmp_team_t *team = thread->th.th_team;
2921 is_init_thread = TRUE;
2922 if (maxthreads < nthreads) {
2924 if (*threads_data_p != NULL) {
2925 kmp_thread_data_t *old_data = *threads_data_p;
2926 kmp_thread_data_t *new_data = NULL;
2930 (
"__kmp_realloc_task_threads_data: T#%d reallocating " 2931 "threads data for task_team %p, new_size = %d, old_size = %d\n",
2932 __kmp_gtid_from_thread(thread), task_team, nthreads, maxthreads));
2937 new_data = (kmp_thread_data_t *)__kmp_allocate(
2938 nthreads *
sizeof(kmp_thread_data_t));
2940 KMP_MEMCPY_S((
void *)new_data, nthreads *
sizeof(kmp_thread_data_t),
2941 (
void *)old_data, maxthreads *
sizeof(kmp_thread_data_t));
2943 #ifdef BUILD_TIED_TASK_STACK 2945 for (i = maxthreads; i < nthreads; i++) {
2946 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
2947 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
2949 #endif // BUILD_TIED_TASK_STACK 2951 (*threads_data_p) = new_data;
2952 __kmp_free(old_data);
2954 KE_TRACE(10, (
"__kmp_realloc_task_threads_data: T#%d allocating " 2955 "threads data for task_team %p, size = %d\n",
2956 __kmp_gtid_from_thread(thread), task_team, nthreads));
2960 ANNOTATE_IGNORE_WRITES_BEGIN();
2961 *threads_data_p = (kmp_thread_data_t *)__kmp_allocate(
2962 nthreads *
sizeof(kmp_thread_data_t));
2963 ANNOTATE_IGNORE_WRITES_END();
2964 #ifdef BUILD_TIED_TASK_STACK 2966 for (i = 0; i < nthreads; i++) {
2967 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
2968 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
2970 #endif // BUILD_TIED_TASK_STACK 2972 task_team->tt.tt_max_threads = nthreads;
2975 KMP_DEBUG_ASSERT(*threads_data_p != NULL);
2979 for (i = 0; i < nthreads; i++) {
2980 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
2981 thread_data->td.td_thr = team->t.t_threads[i];
2983 if (thread_data->td.td_deque_last_stolen >= nthreads) {
2987 thread_data->td.td_deque_last_stolen = -1;
2992 TCW_SYNC_4(task_team->tt.tt_found_tasks, TRUE);
2995 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
2996 return is_init_thread;
3002 static void __kmp_free_task_threads_data(kmp_task_team_t *task_team) {
3003 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3004 if (task_team->tt.tt_threads_data != NULL) {
3006 for (i = 0; i < task_team->tt.tt_max_threads; i++) {
3007 __kmp_free_task_deque(&task_team->tt.tt_threads_data[i]);
3009 __kmp_free(task_team->tt.tt_threads_data);
3010 task_team->tt.tt_threads_data = NULL;
3012 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3019 static kmp_task_team_t *__kmp_allocate_task_team(kmp_info_t *thread,
3021 kmp_task_team_t *task_team = NULL;
3024 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d entering; team = %p\n",
3025 (thread ? __kmp_gtid_from_thread(thread) : -1), team));
3027 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3029 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3030 if (__kmp_free_task_teams != NULL) {
3031 task_team = __kmp_free_task_teams;
3032 TCW_PTR(__kmp_free_task_teams, task_team->tt.tt_next);
3033 task_team->tt.tt_next = NULL;
3035 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3038 if (task_team == NULL) {
3039 KE_TRACE(10, (
"__kmp_allocate_task_team: T#%d allocating " 3040 "task team for team %p\n",
3041 __kmp_gtid_from_thread(thread), team));
3045 task_team = (kmp_task_team_t *)__kmp_allocate(
sizeof(kmp_task_team_t));
3046 __kmp_init_bootstrap_lock(&task_team->tt.tt_threads_lock);
3053 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3055 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3057 task_team->tt.tt_nproc = nthreads = team->t.t_nproc;
3059 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads, nthreads);
3060 TCW_4(task_team->tt.tt_active, TRUE);
3062 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d exiting; task_team = %p " 3063 "unfinished_threads init'd to %d\n",
3064 (thread ? __kmp_gtid_from_thread(thread) : -1), task_team,
3065 KMP_ATOMIC_LD_RLX(&task_team->tt.tt_unfinished_threads)));
3072 void __kmp_free_task_team(kmp_info_t *thread, kmp_task_team_t *task_team) {
3073 KA_TRACE(20, (
"__kmp_free_task_team: T#%d task_team = %p\n",
3074 thread ? __kmp_gtid_from_thread(thread) : -1, task_team));
3077 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3079 KMP_DEBUG_ASSERT(task_team->tt.tt_next == NULL);
3080 task_team->tt.tt_next = __kmp_free_task_teams;
3081 TCW_PTR(__kmp_free_task_teams, task_team);
3083 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3091 void __kmp_reap_task_teams(
void) {
3092 kmp_task_team_t *task_team;
3094 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3096 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3097 while ((task_team = __kmp_free_task_teams) != NULL) {
3098 __kmp_free_task_teams = task_team->tt.tt_next;
3099 task_team->tt.tt_next = NULL;
3102 if (task_team->tt.tt_threads_data != NULL) {
3103 __kmp_free_task_threads_data(task_team);
3105 __kmp_free(task_team);
3107 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3114 void __kmp_wait_to_unref_task_teams(
void) {
3119 KMP_INIT_YIELD(spins);
3127 for (thread = CCAST(kmp_info_t *, __kmp_thread_pool); thread != NULL;
3128 thread = thread->th.th_next_pool) {
3132 if (TCR_PTR(thread->th.th_task_team) == NULL) {
3133 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n",
3134 __kmp_gtid_from_thread(thread)));
3139 if (!__kmp_is_thread_alive(thread, &exit_val)) {
3140 thread->th.th_task_team = NULL;
3147 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: Waiting for T#%d to " 3148 "unreference task_team\n",
3149 __kmp_gtid_from_thread(thread)));
3151 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
3152 volatile void *sleep_loc;
3154 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3158 (
"__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n",
3159 __kmp_gtid_from_thread(thread), __kmp_gtid_from_thread(thread)));
3160 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc);
3170 KMP_YIELD(TCR_4(__kmp_nth) > __kmp_avail_proc);
3171 KMP_YIELD_SPIN(spins);
3177 void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team,
int always) {
3178 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3184 if (team->t.t_task_team[this_thr->th.th_task_state] == NULL &&
3185 (always || team->t.t_nproc > 1)) {
3186 team->t.t_task_team[this_thr->th.th_task_state] =
3187 __kmp_allocate_task_team(this_thr, team);
3188 KA_TRACE(20, (
"__kmp_task_team_setup: Master T#%d created new task_team %p " 3189 "for team %d at parity=%d\n",
3190 __kmp_gtid_from_thread(this_thr),
3191 team->t.t_task_team[this_thr->th.th_task_state],
3192 ((team != NULL) ? team->t.t_id : -1),
3193 this_thr->th.th_task_state));
3203 if (team->t.t_nproc > 1) {
3204 int other_team = 1 - this_thr->th.th_task_state;
3205 if (team->t.t_task_team[other_team] == NULL) {
3206 team->t.t_task_team[other_team] =
3207 __kmp_allocate_task_team(this_thr, team);
3208 KA_TRACE(20, (
"__kmp_task_team_setup: Master T#%d created second new " 3209 "task_team %p for team %d at parity=%d\n",
3210 __kmp_gtid_from_thread(this_thr),
3211 team->t.t_task_team[other_team],
3212 ((team != NULL) ? team->t.t_id : -1), other_team));
3215 kmp_task_team_t *task_team = team->t.t_task_team[other_team];
3216 if (!task_team->tt.tt_active ||
3217 team->t.t_nproc != task_team->tt.tt_nproc) {
3218 TCW_4(task_team->tt.tt_nproc, team->t.t_nproc);
3219 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3221 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3223 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads,
3225 TCW_4(task_team->tt.tt_active, TRUE);
3229 KA_TRACE(20, (
"__kmp_task_team_setup: Master T#%d reset next task_team " 3230 "%p for team %d at parity=%d\n",
3231 __kmp_gtid_from_thread(this_thr),
3232 team->t.t_task_team[other_team],
3233 ((team != NULL) ? team->t.t_id : -1), other_team));
3241 void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team) {
3242 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3246 this_thr->th.th_task_state = 1 - this_thr->th.th_task_state;
3249 TCW_PTR(this_thr->th.th_task_team,
3250 team->t.t_task_team[this_thr->th.th_task_state]);
3252 (
"__kmp_task_team_sync: Thread T#%d task team switched to task_team " 3253 "%p from Team #%d (parity=%d)\n",
3254 __kmp_gtid_from_thread(this_thr), this_thr->th.th_task_team,
3255 ((team != NULL) ? team->t.t_id : -1), this_thr->th.th_task_state));
3265 void __kmp_task_team_wait(
3266 kmp_info_t *this_thr,
3267 kmp_team_t *team USE_ITT_BUILD_ARG(
void *itt_sync_obj),
int wait) {
3268 kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state];
3270 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3271 KMP_DEBUG_ASSERT(task_team == this_thr->th.th_task_team);
3273 if ((task_team != NULL) && KMP_TASKING_ENABLED(task_team)) {
3275 KA_TRACE(20, (
"__kmp_task_team_wait: Master T#%d waiting for all tasks " 3276 "(for unfinished_threads to reach 0) on task_team = %p\n",
3277 __kmp_gtid_from_thread(this_thr), task_team));
3281 kmp_flag_32 flag(RCAST(std::atomic<kmp_uint32> *,
3282 &task_team->tt.tt_unfinished_threads),
3284 flag.wait(this_thr, TRUE USE_ITT_BUILD_ARG(itt_sync_obj));
3290 (
"__kmp_task_team_wait: Master T#%d deactivating task_team %p: " 3291 "setting active to false, setting local and team's pointer to NULL\n",
3292 __kmp_gtid_from_thread(this_thr), task_team));
3294 KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1 ||
3295 task_team->tt.tt_found_proxy_tasks == TRUE);
3296 TCW_SYNC_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3298 KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1);
3300 KMP_CHECK_UPDATE(task_team->tt.tt_untied_task_encountered, 0);
3301 TCW_SYNC_4(task_team->tt.tt_active, FALSE);
3304 TCW_PTR(this_thr->th.th_task_team, NULL);
3313 void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread,
int gtid) {
3314 std::atomic<kmp_uint32> *spin = RCAST(
3315 std::atomic<kmp_uint32> *,
3316 &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads);
3318 KMP_DEBUG_ASSERT(__kmp_tasking_mode == tskm_extra_barrier);
3321 KMP_FSYNC_SPIN_INIT(spin, NULL);
3323 kmp_flag_32 spin_flag(spin, 0U);
3324 while (!spin_flag.execute_tasks(thread, gtid, TRUE,
3325 &flag USE_ITT_BUILD_ARG(NULL), 0)) {
3328 KMP_FSYNC_SPIN_PREPARE(RCAST(
void *, spin));
3331 if (TCR_4(__kmp_global.g.g_done)) {
3332 if (__kmp_global.g.g_abort)
3333 __kmp_abort_thread();
3339 KMP_FSYNC_SPIN_ACQUIRED(RCAST(
void *, spin));
3350 static bool __kmp_give_task(kmp_info_t *thread, kmp_int32 tid, kmp_task_t *task,
3352 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
3353 kmp_task_team_t *task_team = taskdata->td_task_team;
3355 KA_TRACE(20, (
"__kmp_give_task: trying to give task %p to thread %d.\n",
3359 KMP_DEBUG_ASSERT(task_team != NULL);
3361 bool result =
false;
3362 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
3364 if (thread_data->td.td_deque == NULL) {
3368 (
"__kmp_give_task: thread %d has no queue while giving task %p.\n",
3373 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3374 TASK_DEQUE_SIZE(thread_data->td)) {
3377 (
"__kmp_give_task: queue is full while giving task %p to thread %d.\n",
3382 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
3385 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3386 __kmp_realloc_task_deque(thread, thread_data);
3390 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3392 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3393 TASK_DEQUE_SIZE(thread_data->td)) {
3394 KA_TRACE(30, (
"__kmp_give_task: queue is full while giving task %p to " 3400 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
3401 goto release_and_exit;
3403 __kmp_realloc_task_deque(thread, thread_data);
3409 thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
3411 thread_data->td.td_deque_tail =
3412 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
3413 TCW_4(thread_data->td.td_deque_ntasks,
3414 TCR_4(thread_data->td.td_deque_ntasks) + 1);
3417 KA_TRACE(30, (
"__kmp_give_task: successfully gave task %p to thread %d.\n",
3421 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3442 static void __kmp_first_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
3443 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
3444 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3445 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
3446 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
3448 taskdata->td_flags.complete = 1;
3450 if (taskdata->td_taskgroup)
3451 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
3455 KMP_ATOMIC_INC(&taskdata->td_incomplete_child_tasks);
3458 static void __kmp_second_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
3459 kmp_int32 children = 0;
3463 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks) - 1;
3464 KMP_DEBUG_ASSERT(children >= 0);
3467 KMP_ATOMIC_DEC(&taskdata->td_incomplete_child_tasks);
3470 static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask) {
3471 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3472 kmp_info_t *thread = __kmp_threads[gtid];
3474 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3475 KMP_DEBUG_ASSERT(taskdata->td_flags.complete ==
3480 while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) > 0)
3483 __kmp_release_deps(gtid, taskdata);
3484 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
3495 void __kmpc_proxy_task_completed(kmp_int32 gtid, kmp_task_t *ptask) {
3496 KMP_DEBUG_ASSERT(ptask != NULL);
3497 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3499 10, (
"__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n",
3502 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3504 __kmp_first_top_half_finish_proxy(taskdata);
3505 __kmp_second_top_half_finish_proxy(taskdata);
3506 __kmp_bottom_half_finish_proxy(gtid, ptask);
3509 (
"__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n",
3520 void __kmpc_proxy_task_completed_ooo(kmp_task_t *ptask) {
3521 KMP_DEBUG_ASSERT(ptask != NULL);
3522 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3526 (
"__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n",
3529 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3531 __kmp_first_top_half_finish_proxy(taskdata);
3535 kmp_team_t *team = taskdata->td_team;
3536 kmp_int32 nthreads = team->t.t_nproc;
3541 kmp_int32 start_k = 0;
3543 kmp_int32 k = start_k;
3547 thread = team->t.t_threads[k];
3548 k = (k + 1) % nthreads;
3554 }
while (!__kmp_give_task(thread, k, ptask, pass));
3556 __kmp_second_top_half_finish_proxy(taskdata);
3560 (
"__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n",
3570 kmp_task_t *__kmp_task_dup_alloc(kmp_info_t *thread, kmp_task_t *task_src) {
3572 kmp_taskdata_t *taskdata;
3573 kmp_taskdata_t *taskdata_src;
3574 kmp_taskdata_t *parent_task = thread->th.th_current_task;
3575 size_t shareds_offset;
3578 KA_TRACE(10, (
"__kmp_task_dup_alloc(enter): Th %p, source task %p\n", thread,
3580 taskdata_src = KMP_TASK_TO_TASKDATA(task_src);
3581 KMP_DEBUG_ASSERT(taskdata_src->td_flags.proxy ==
3583 KMP_DEBUG_ASSERT(taskdata_src->td_flags.tasktype == TASK_EXPLICIT);
3584 task_size = taskdata_src->td_size_alloc;
3587 KA_TRACE(30, (
"__kmp_task_dup_alloc: Th %p, malloc size %ld\n", thread,
3590 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, task_size);
3592 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, task_size);
3594 KMP_MEMCPY(taskdata, taskdata_src, task_size);
3596 task = KMP_TASKDATA_TO_TASK(taskdata);
3599 taskdata->td_task_id = KMP_GEN_TASK_ID();
3600 if (task->shareds != NULL) {
3601 shareds_offset = (
char *)task_src->shareds - (
char *)taskdata_src;
3602 task->shareds = &((
char *)taskdata)[shareds_offset];
3603 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
3606 taskdata->td_alloc_thread = thread;
3607 taskdata->td_parent = parent_task;
3608 taskdata->td_taskgroup =
3614 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
3615 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
3616 if (parent_task->td_taskgroup)
3617 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
3620 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT)
3621 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
3625 (
"__kmp_task_dup_alloc(exit): Th %p, created task %p, parent=%p\n",
3626 thread, taskdata, taskdata->td_parent));
3628 if (UNLIKELY(ompt_enabled.enabled))
3629 __ompt_task_init(taskdata, thread->th.th_info.ds.ds_gtid);
3638 typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32);
3640 KMP_BUILD_ASSERT(
sizeof(
long) == 4 ||
sizeof(
long) == 8);
3645 class kmp_taskloop_bounds_t {
3647 const kmp_taskdata_t *taskdata;
3648 size_t lower_offset;
3649 size_t upper_offset;
3652 kmp_taskloop_bounds_t(kmp_task_t *_task, kmp_uint64 *lb, kmp_uint64 *ub)
3653 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(task)),
3654 lower_offset((char *)lb - (char *)task),
3655 upper_offset((char *)ub - (char *)task) {
3656 KMP_DEBUG_ASSERT((
char *)lb > (
char *)_task);
3657 KMP_DEBUG_ASSERT((
char *)ub > (
char *)_task);
3659 kmp_taskloop_bounds_t(kmp_task_t *_task,
const kmp_taskloop_bounds_t &bounds)
3660 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(_task)),
3661 lower_offset(bounds.lower_offset), upper_offset(bounds.upper_offset) {}
3662 size_t get_lower_offset()
const {
return lower_offset; }
3663 size_t get_upper_offset()
const {
return upper_offset; }
3664 kmp_uint64 get_lb()
const {
3666 #if defined(KMP_GOMP_COMPAT) 3668 if (!taskdata->td_flags.native) {
3669 retval = *(kmp_int64 *)((
char *)task + lower_offset);
3672 if (taskdata->td_size_loop_bounds == 4) {
3673 kmp_int32 *lb = RCAST(kmp_int32 *, task->shareds);
3674 retval = (kmp_int64)*lb;
3676 kmp_int64 *lb = RCAST(kmp_int64 *, task->shareds);
3677 retval = (kmp_int64)*lb;
3681 retval = *(kmp_int64 *)((
char *)task + lower_offset);
3682 #endif // defined(KMP_GOMP_COMPAT) 3685 kmp_uint64 get_ub()
const {
3687 #if defined(KMP_GOMP_COMPAT) 3689 if (!taskdata->td_flags.native) {
3690 retval = *(kmp_int64 *)((
char *)task + upper_offset);
3693 if (taskdata->td_size_loop_bounds == 4) {
3694 kmp_int32 *ub = RCAST(kmp_int32 *, task->shareds) + 1;
3695 retval = (kmp_int64)*ub;
3697 kmp_int64 *ub = RCAST(kmp_int64 *, task->shareds) + 1;
3698 retval = (kmp_int64)*ub;
3702 retval = *(kmp_int64 *)((
char *)task + upper_offset);
3703 #endif // defined(KMP_GOMP_COMPAT) 3706 void set_lb(kmp_uint64 lb) {
3707 #if defined(KMP_GOMP_COMPAT) 3709 if (!taskdata->td_flags.native) {
3710 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
3713 if (taskdata->td_size_loop_bounds == 4) {
3714 kmp_uint32 *lower = RCAST(kmp_uint32 *, task->shareds);
3715 *lower = (kmp_uint32)lb;
3717 kmp_uint64 *lower = RCAST(kmp_uint64 *, task->shareds);
3718 *lower = (kmp_uint64)lb;
3722 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
3723 #endif // defined(KMP_GOMP_COMPAT) 3725 void set_ub(kmp_uint64 ub) {
3726 #if defined(KMP_GOMP_COMPAT) 3728 if (!taskdata->td_flags.native) {
3729 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
3732 if (taskdata->td_size_loop_bounds == 4) {
3733 kmp_uint32 *upper = RCAST(kmp_uint32 *, task->shareds) + 1;
3734 *upper = (kmp_uint32)ub;
3736 kmp_uint64 *upper = RCAST(kmp_uint64 *, task->shareds) + 1;
3737 *upper = (kmp_uint64)ub;
3741 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
3742 #endif // defined(KMP_GOMP_COMPAT) 3761 void __kmp_taskloop_linear(
ident_t *loc,
int gtid, kmp_task_t *task,
3762 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
3763 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
3764 kmp_uint64 grainsize, kmp_uint64 extras,
3771 KMP_TIME_PARTITIONED_BLOCK(OMP_taskloop_scheduling);
3772 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
3774 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
3775 kmp_uint64 lower = task_bounds.get_lb();
3776 kmp_uint64 upper = task_bounds.get_ub();
3778 kmp_info_t *thread = __kmp_threads[gtid];
3779 kmp_taskdata_t *current_task = thread->th.th_current_task;
3780 kmp_task_t *next_task;
3781 kmp_int32 lastpriv = 0;
3783 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras);
3784 KMP_DEBUG_ASSERT(num_tasks > extras);
3785 KMP_DEBUG_ASSERT(num_tasks > 0);
3786 KA_TRACE(20, (
"__kmp_taskloop_linear: T#%d: %lld tasks, grainsize %lld, " 3787 "extras %lld, i=%lld,%lld(%d)%lld, dup %p\n",
3788 gtid, num_tasks, grainsize, extras, lower, upper, ub_glob, st,
3792 for (i = 0; i < num_tasks; ++i) {
3793 kmp_uint64 chunk_minus_1;
3795 chunk_minus_1 = grainsize - 1;
3797 chunk_minus_1 = grainsize;
3800 upper = lower + st * chunk_minus_1;
3801 if (i == num_tasks - 1) {
3804 KMP_DEBUG_ASSERT(upper == *ub);
3805 if (upper == ub_glob)
3807 }
else if (st > 0) {
3808 KMP_DEBUG_ASSERT((kmp_uint64)st > *ub - upper);
3809 if ((kmp_uint64)st > ub_glob - upper)
3812 KMP_DEBUG_ASSERT(upper + st < *ub);
3813 if (upper - ub_glob < (kmp_uint64)(-st))
3817 next_task = __kmp_task_dup_alloc(thread, task);
3818 kmp_taskdata_t *next_taskdata = KMP_TASK_TO_TASKDATA(next_task);
3819 kmp_taskloop_bounds_t next_task_bounds =
3820 kmp_taskloop_bounds_t(next_task, task_bounds);
3823 next_task_bounds.set_lb(lower);
3824 if (next_taskdata->td_flags.native) {
3825 next_task_bounds.set_ub(upper + (st > 0 ? 1 : -1));
3827 next_task_bounds.set_ub(upper);
3829 if (ptask_dup != NULL)
3830 ptask_dup(next_task, task, lastpriv);
3832 (
"__kmp_taskloop_linear: T#%d; task #%llu: task %p: lower %lld, " 3833 "upper %lld stride %lld, (offsets %p %p)\n",
3834 gtid, i, next_task, lower, upper, st,
3835 next_task_bounds.get_lower_offset(),
3836 next_task_bounds.get_upper_offset()));
3838 __kmp_omp_taskloop_task(NULL, gtid, next_task,
3841 __kmp_omp_task(gtid, next_task,
true);
3846 __kmp_task_start(gtid, task, current_task);
3848 __kmp_task_finish<false>(gtid, task, current_task);
3853 typedef struct __taskloop_params {
3860 kmp_uint64 num_tasks;
3861 kmp_uint64 grainsize;
3864 kmp_uint64 num_t_min;
3868 } __taskloop_params_t;
3870 void __kmp_taskloop_recur(
ident_t *,
int, kmp_task_t *, kmp_uint64 *,
3871 kmp_uint64 *, kmp_int64, kmp_uint64, kmp_uint64,
3872 kmp_uint64, kmp_uint64, kmp_uint64, kmp_uint64,
3879 int __kmp_taskloop_task(
int gtid,
void *ptask) {
3880 __taskloop_params_t *p =
3881 (__taskloop_params_t *)((kmp_task_t *)ptask)->shareds;
3882 kmp_task_t *task = p->task;
3883 kmp_uint64 *lb = p->lb;
3884 kmp_uint64 *ub = p->ub;
3885 void *task_dup = p->task_dup;
3887 kmp_int64 st = p->st;
3888 kmp_uint64 ub_glob = p->ub_glob;
3889 kmp_uint64 num_tasks = p->num_tasks;
3890 kmp_uint64 grainsize = p->grainsize;
3891 kmp_uint64 extras = p->extras;
3892 kmp_uint64 tc = p->tc;
3893 kmp_uint64 num_t_min = p->num_t_min;
3895 void *codeptr_ra = p->codeptr_ra;
3898 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
3899 KMP_DEBUG_ASSERT(task != NULL);
3900 KA_TRACE(20, (
"__kmp_taskloop_task: T#%d, task %p: %lld tasks, grainsize" 3901 " %lld, extras %lld, i=%lld,%lld(%d), dup %p\n",
3902 gtid, taskdata, num_tasks, grainsize, extras, *lb, *ub, st,
3905 KMP_DEBUG_ASSERT(num_tasks * 2 + 1 > num_t_min);
3906 if (num_tasks > num_t_min)
3907 __kmp_taskloop_recur(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
3908 grainsize, extras, tc, num_t_min,
3914 __kmp_taskloop_linear(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
3915 grainsize, extras, tc,
3921 KA_TRACE(40, (
"__kmp_taskloop_task(exit): T#%d\n", gtid));
3942 void __kmp_taskloop_recur(
ident_t *loc,
int gtid, kmp_task_t *task,
3943 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
3944 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
3945 kmp_uint64 grainsize, kmp_uint64 extras,
3946 kmp_uint64 tc, kmp_uint64 num_t_min,
3952 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
3953 KMP_DEBUG_ASSERT(task != NULL);
3954 KMP_DEBUG_ASSERT(num_tasks > num_t_min);
3955 KA_TRACE(20, (
"__kmp_taskloop_recur: T#%d, task %p: %lld tasks, grainsize" 3956 " %lld, extras %lld, i=%lld,%lld(%d), dup %p\n",
3957 gtid, taskdata, num_tasks, grainsize, extras, *lb, *ub, st,
3960 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
3961 kmp_uint64 lower = *lb;
3962 kmp_info_t *thread = __kmp_threads[gtid];
3964 kmp_task_t *next_task;
3965 size_t lower_offset =
3966 (
char *)lb - (
char *)task;
3967 size_t upper_offset =
3968 (
char *)ub - (
char *)task;
3970 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras);
3971 KMP_DEBUG_ASSERT(num_tasks > extras);
3972 KMP_DEBUG_ASSERT(num_tasks > 0);
3975 kmp_uint64 lb1, ub0, tc0, tc1, ext0, ext1;
3976 kmp_uint64 gr_size0 = grainsize;
3977 kmp_uint64 n_tsk0 = num_tasks >> 1;
3978 kmp_uint64 n_tsk1 = num_tasks - n_tsk0;
3979 if (n_tsk0 <= extras) {
3982 ext1 = extras - n_tsk0;
3983 tc0 = gr_size0 * n_tsk0;
3988 tc1 = grainsize * n_tsk1;
3991 ub0 = lower + st * (tc0 - 1);
3995 next_task = __kmp_task_dup_alloc(thread, task);
3997 *(kmp_uint64 *)((
char *)next_task + lower_offset) = lb1;
3998 if (ptask_dup != NULL)
3999 ptask_dup(next_task, task, 0);
4003 kmp_task_t *new_task =
4004 __kmpc_omp_task_alloc(loc, gtid, 1, 3 *
sizeof(
void *),
4005 sizeof(__taskloop_params_t), &__kmp_taskloop_task);
4006 __taskloop_params_t *p = (__taskloop_params_t *)new_task->shareds;
4007 p->task = next_task;
4008 p->lb = (kmp_uint64 *)((
char *)next_task + lower_offset);
4009 p->ub = (kmp_uint64 *)((
char *)next_task + upper_offset);
4010 p->task_dup = task_dup;
4012 p->ub_glob = ub_glob;
4013 p->num_tasks = n_tsk1;
4014 p->grainsize = grainsize;
4017 p->num_t_min = num_t_min;
4019 p->codeptr_ra = codeptr_ra;
4024 __kmp_omp_taskloop_task(NULL, gtid, new_task, codeptr_ra);
4026 __kmp_omp_task(gtid, new_task,
true);
4030 if (n_tsk0 > num_t_min)
4031 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0, gr_size0,
4032 ext0, tc0, num_t_min,
4038 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0,
4039 gr_size0, ext0, tc0,
4045 KA_TRACE(40, (
"__kmpc_taskloop_recur(exit): T#%d\n", gtid));
4064 void __kmpc_taskloop(
ident_t *loc,
int gtid, kmp_task_t *task,
int if_val,
4065 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
int nogroup,
4066 int sched, kmp_uint64 grainsize,
void *task_dup) {
4067 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4068 KMP_DEBUG_ASSERT(task != NULL);
4071 #if OMPT_SUPPORT && OMPT_OPTIONAL 4072 OMPT_STORE_RETURN_ADDRESS(gtid);
4074 __kmpc_taskgroup(loc, gtid);
4079 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4082 kmp_uint64 lower = task_bounds.get_lb();
4083 kmp_uint64 upper = task_bounds.get_ub();
4084 kmp_uint64 ub_glob = upper;
4085 kmp_uint64 num_tasks = 0, extras = 0;
4086 kmp_uint64 num_tasks_min = __kmp_taskloop_min_tasks;
4087 kmp_info_t *thread = __kmp_threads[gtid];
4088 kmp_taskdata_t *current_task = thread->th.th_current_task;
4090 KA_TRACE(20, (
"__kmpc_taskloop: T#%d, task %p, lb %lld, ub %lld, st %lld, " 4091 "grain %llu(%d), dup %p\n",
4092 gtid, taskdata, lower, upper, st, grainsize, sched, task_dup));
4096 tc = upper - lower + 1;
4097 }
else if (st < 0) {
4098 tc = (lower - upper) / (-st) + 1;
4100 tc = (upper - lower) / st + 1;
4103 KA_TRACE(20, (
"__kmpc_taskloop(exit): T#%d zero-trip loop\n", gtid));
4105 __kmp_task_start(gtid, task, current_task);
4107 __kmp_task_finish<false>(gtid, task, current_task);
4111 #if OMPT_SUPPORT && OMPT_OPTIONAL 4112 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
4113 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
4114 if (ompt_enabled.ompt_callback_work) {
4115 ompt_callbacks.ompt_callback(ompt_callback_work)(
4116 ompt_work_taskloop, ompt_scope_begin, &(team_info->parallel_data),
4117 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
4121 if (num_tasks_min == 0)
4124 KMP_MIN(thread->th.th_team_nproc * 10, INITIAL_TASK_DEQUE_SIZE);
4130 grainsize = thread->th.th_team_nproc * 10;
4132 if (grainsize > tc) {
4137 num_tasks = grainsize;
4138 grainsize = tc / num_tasks;
4139 extras = tc % num_tasks;
4143 if (grainsize > tc) {
4148 num_tasks = tc / grainsize;
4150 grainsize = tc / num_tasks;
4151 extras = tc % num_tasks;
4155 KMP_ASSERT2(0,
"unknown scheduling of taskloop");
4157 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras);
4158 KMP_DEBUG_ASSERT(num_tasks > extras);
4159 KMP_DEBUG_ASSERT(num_tasks > 0);
4165 taskdata->td_flags.task_serial = 1;
4166 taskdata->td_flags.tiedness = TASK_TIED;
4168 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4169 grainsize, extras, tc,
4171 OMPT_GET_RETURN_ADDRESS(0),
4176 }
else if (num_tasks > num_tasks_min && !taskdata->td_flags.native) {
4177 KA_TRACE(20, (
"__kmpc_taskloop: T#%d, go recursive: tc %llu, #tasks %llu" 4178 "(%lld), grain %llu, extras %llu\n",
4179 gtid, tc, num_tasks, num_tasks_min, grainsize, extras));
4180 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4181 grainsize, extras, tc, num_tasks_min,
4183 OMPT_GET_RETURN_ADDRESS(0),
4187 KA_TRACE(20, (
"__kmpc_taskloop: T#%d, go linear: tc %llu, #tasks %llu" 4188 "(%lld), grain %llu, extras %llu\n",
4189 gtid, tc, num_tasks, num_tasks_min, grainsize, extras));
4190 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4191 grainsize, extras, tc,
4193 OMPT_GET_RETURN_ADDRESS(0),
4198 #if OMPT_SUPPORT && OMPT_OPTIONAL 4199 if (ompt_enabled.ompt_callback_work) {
4200 ompt_callbacks.ompt_callback(ompt_callback_work)(
4201 ompt_work_taskloop, ompt_scope_end, &(team_info->parallel_data),
4202 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
4207 #if OMPT_SUPPORT && OMPT_OPTIONAL 4208 OMPT_STORE_RETURN_ADDRESS(gtid);
4210 __kmpc_end_taskgroup(loc, gtid);
4212 KA_TRACE(20, (
"__kmpc_taskloop(exit): T#%d\n", gtid));
#define KMP_COUNT_BLOCK(name)
Increments specified counter (name).