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);
270 (
"__kmp_push_task: T#%d untied_count (%d) incremented for task %p\n",
271 gtid, counter, taskdata));
275 if (taskdata->td_flags.task_serial) {
276 KA_TRACE(20, (
"__kmp_push_task: T#%d team serialized; returning " 277 "TASK_NOT_PUSHED for task %p\n",
279 return TASK_NOT_PUSHED;
284 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
285 if (!KMP_TASKING_ENABLED(task_team)) {
286 __kmp_enable_tasking(task_team, thread);
288 KMP_DEBUG_ASSERT(TCR_4(task_team->tt.tt_found_tasks) == TRUE);
289 KMP_DEBUG_ASSERT(TCR_PTR(task_team->tt.tt_threads_data) != NULL);
292 thread_data = &task_team->tt.tt_threads_data[tid];
295 if (thread_data->td.td_deque == NULL) {
296 __kmp_alloc_task_deque(thread, thread_data);
300 if (TCR_4(thread_data->td.td_deque_ntasks) >=
301 TASK_DEQUE_SIZE(thread_data->td)) {
302 KA_TRACE(20, (
"__kmp_push_task: T#%d deque is full; returning " 303 "TASK_NOT_PUSHED for task %p\n",
305 return TASK_NOT_PUSHED;
309 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
313 if (TCR_4(thread_data->td.td_deque_ntasks) >=
314 TASK_DEQUE_SIZE(thread_data->td)) {
315 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
316 KA_TRACE(20, (
"__kmp_push_task: T#%d deque is full on 2nd check; returning " 317 "TASK_NOT_PUSHED for task %p\n",
319 return TASK_NOT_PUSHED;
323 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) <
324 TASK_DEQUE_SIZE(thread_data->td));
327 thread_data->td.td_deque[thread_data->td.td_deque_tail] =
330 thread_data->td.td_deque_tail =
331 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
332 TCW_4(thread_data->td.td_deque_ntasks,
333 TCR_4(thread_data->td.td_deque_ntasks) + 1);
335 KA_TRACE(20, (
"__kmp_push_task: T#%d returning TASK_SUCCESSFULLY_PUSHED: " 336 "task=%p ntasks=%d head=%u tail=%u\n",
337 gtid, taskdata, thread_data->td.td_deque_ntasks,
338 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
340 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
342 return TASK_SUCCESSFULLY_PUSHED;
349 void __kmp_pop_current_task_from_thread(kmp_info_t *this_thr) {
350 KF_TRACE(10, (
"__kmp_pop_current_task_from_thread(enter): T#%d " 351 "this_thread=%p, curtask=%p, " 352 "curtask_parent=%p\n",
353 0, this_thr, this_thr->th.th_current_task,
354 this_thr->th.th_current_task->td_parent));
356 this_thr->th.th_current_task = this_thr->th.th_current_task->td_parent;
358 KF_TRACE(10, (
"__kmp_pop_current_task_from_thread(exit): T#%d " 359 "this_thread=%p, curtask=%p, " 360 "curtask_parent=%p\n",
361 0, this_thr, this_thr->th.th_current_task,
362 this_thr->th.th_current_task->td_parent));
371 void __kmp_push_current_task_to_thread(kmp_info_t *this_thr, kmp_team_t *team,
375 KF_TRACE(10, (
"__kmp_push_current_task_to_thread(enter): T#%d this_thread=%p " 378 tid, this_thr, this_thr->th.th_current_task,
379 team->t.t_implicit_task_taskdata[tid].td_parent));
381 KMP_DEBUG_ASSERT(this_thr != NULL);
384 if (this_thr->th.th_current_task != &team->t.t_implicit_task_taskdata[0]) {
385 team->t.t_implicit_task_taskdata[0].td_parent =
386 this_thr->th.th_current_task;
387 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[0];
390 team->t.t_implicit_task_taskdata[tid].td_parent =
391 team->t.t_implicit_task_taskdata[0].td_parent;
392 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[tid];
395 KF_TRACE(10, (
"__kmp_push_current_task_to_thread(exit): T#%d this_thread=%p " 398 tid, this_thr, this_thr->th.th_current_task,
399 team->t.t_implicit_task_taskdata[tid].td_parent));
407 static void __kmp_task_start(kmp_int32 gtid, kmp_task_t *task,
408 kmp_taskdata_t *current_task) {
409 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
410 kmp_info_t *thread = __kmp_threads[gtid];
413 (
"__kmp_task_start(enter): T#%d starting task %p: current_task=%p\n",
414 gtid, taskdata, current_task));
416 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
421 current_task->td_flags.executing = 0;
424 #ifdef BUILD_TIED_TASK_STACK 425 if (taskdata->td_flags.tiedness == TASK_TIED) {
426 __kmp_push_task_stack(gtid, thread, taskdata);
431 thread->th.th_current_task = taskdata;
433 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 0 ||
434 taskdata->td_flags.tiedness == TASK_UNTIED);
435 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0 ||
436 taskdata->td_flags.tiedness == TASK_UNTIED);
437 taskdata->td_flags.started = 1;
438 taskdata->td_flags.executing = 1;
439 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
440 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
447 KA_TRACE(10, (
"__kmp_task_start(exit): T#%d task=%p\n", gtid, taskdata));
458 static inline void __ompt_task_init(kmp_taskdata_t *task,
int tid) {
460 task->ompt_task_info.task_data.value = 0;
461 task->ompt_task_info.frame.exit_frame = NULL;
462 task->ompt_task_info.frame.enter_frame = NULL;
464 task->ompt_task_info.ndeps = 0;
465 task->ompt_task_info.deps = NULL;
471 static inline void __ompt_task_start(kmp_task_t *task,
472 kmp_taskdata_t *current_task,
474 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
475 ompt_task_status_t status = ompt_task_others;
476 if (__kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded) {
477 status = ompt_task_yield;
478 __kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded = 0;
481 if (ompt_enabled.ompt_callback_task_schedule) {
482 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
483 &(current_task->ompt_task_info.task_data), status,
484 &(taskdata->ompt_task_info.task_data));
486 taskdata->ompt_task_info.scheduling_parent = current_task;
492 __ompt_task_finish(kmp_task_t *task, kmp_taskdata_t *resumed_task,
493 ompt_task_status_t status = ompt_task_complete) {
494 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
495 if (__kmp_omp_cancellation && taskdata->td_taskgroup &&
496 taskdata->td_taskgroup->cancel_request == cancel_taskgroup) {
497 status = ompt_task_cancel;
501 if (ompt_enabled.ompt_callback_task_schedule) {
502 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
503 &(taskdata->ompt_task_info.task_data), status,
504 &((resumed_task ? resumed_task
505 : (taskdata->ompt_task_info.scheduling_parent
506 ? taskdata->ompt_task_info.scheduling_parent
507 : taskdata->td_parent))
508 ->ompt_task_info.task_data));
514 static void __kmpc_omp_task_begin_if0_template(
ident_t *loc_ref, kmp_int32 gtid,
517 void *return_address) {
518 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
519 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
521 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(enter): T#%d loc=%p task=%p " 523 gtid, loc_ref, taskdata, current_task));
525 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
528 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
529 KA_TRACE(20, (
"__kmpc_omp_task_begin_if0: T#%d untied_count (%d) " 530 "incremented for task %p\n",
531 gtid, counter, taskdata));
534 taskdata->td_flags.task_serial =
536 __kmp_task_start(gtid, task, current_task);
540 if (current_task->ompt_task_info.frame.enter_frame == NULL) {
541 current_task->ompt_task_info.frame.enter_frame =
542 taskdata->ompt_task_info.frame.exit_frame = frame_address;
544 if (ompt_enabled.ompt_callback_task_create) {
545 ompt_task_info_t *parent_info = &(current_task->ompt_task_info);
546 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
547 &(parent_info->task_data), &(parent_info->frame),
548 &(taskdata->ompt_task_info.task_data),
549 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(taskdata), 0,
552 __ompt_task_start(task, current_task, gtid);
554 #endif // OMPT_SUPPORT 556 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(exit): T#%d loc=%p task=%p,\n", gtid,
562 static void __kmpc_omp_task_begin_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
565 void *return_address) {
566 __kmpc_omp_task_begin_if0_template<true>(loc_ref, gtid, task, frame_address,
569 #endif // OMPT_SUPPORT 577 void __kmpc_omp_task_begin_if0(
ident_t *loc_ref, kmp_int32 gtid,
580 if (UNLIKELY(ompt_enabled.enabled)) {
581 OMPT_STORE_RETURN_ADDRESS(gtid);
582 __kmpc_omp_task_begin_if0_ompt(loc_ref, gtid, task,
583 OMPT_GET_FRAME_ADDRESS(1),
584 OMPT_LOAD_RETURN_ADDRESS(gtid));
588 __kmpc_omp_task_begin_if0_template<false>(loc_ref, gtid, task, NULL, NULL);
594 void __kmpc_omp_task_begin(
ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task) {
595 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
599 (
"__kmpc_omp_task_begin(enter): T#%d loc=%p task=%p current_task=%p\n",
600 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task), current_task));
602 __kmp_task_start(gtid, task, current_task);
604 KA_TRACE(10, (
"__kmpc_omp_task_begin(exit): T#%d loc=%p task=%p,\n", gtid,
605 loc_ref, KMP_TASK_TO_TASKDATA(task)));
608 #endif // TASK_UNUSED 615 static void __kmp_free_task(kmp_int32 gtid, kmp_taskdata_t *taskdata,
616 kmp_info_t *thread) {
617 KA_TRACE(30, (
"__kmp_free_task: T#%d freeing data from task %p\n", gtid,
621 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
622 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0);
623 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 1);
624 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
625 KMP_DEBUG_ASSERT(taskdata->td_allocated_child_tasks == 0 ||
626 taskdata->td_flags.task_serial == 1);
627 KMP_DEBUG_ASSERT(taskdata->td_incomplete_child_tasks == 0);
629 taskdata->td_flags.freed = 1;
630 ANNOTATE_HAPPENS_BEFORE(taskdata);
633 __kmp_fast_free(thread, taskdata);
635 __kmp_thread_free(thread, taskdata);
638 KA_TRACE(20, (
"__kmp_free_task: T#%d freed task %p\n", gtid, taskdata));
647 static void __kmp_free_task_and_ancestors(kmp_int32 gtid,
648 kmp_taskdata_t *taskdata,
649 kmp_info_t *thread) {
653 kmp_int32 team_serial =
654 (taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) &&
655 !taskdata->td_flags.proxy;
657 kmp_int32 team_serial =
658 taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser;
660 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
662 kmp_int32 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
663 KMP_DEBUG_ASSERT(children >= 0);
666 while (children == 0) {
667 kmp_taskdata_t *parent_taskdata = taskdata->td_parent;
669 KA_TRACE(20, (
"__kmp_free_task_and_ancestors(enter): T#%d task %p complete " 670 "and freeing itself\n",
674 __kmp_free_task(gtid, taskdata, thread);
676 taskdata = parent_taskdata;
680 if (team_serial || taskdata->td_flags.tasktype == TASK_IMPLICIT)
684 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
685 KMP_DEBUG_ASSERT(children >= 0);
689 20, (
"__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; " 690 "not freeing it yet\n",
691 gtid, taskdata, children));
700 static void __kmp_task_finish(kmp_int32 gtid, kmp_task_t *task,
701 kmp_taskdata_t *resumed_task) {
702 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
703 kmp_info_t *thread = __kmp_threads[gtid];
704 kmp_task_team_t *task_team =
705 thread->th.th_task_team;
706 kmp_int32 children = 0;
708 KA_TRACE(10, (
"__kmp_task_finish(enter): T#%d finishing task %p and resuming " 710 gtid, taskdata, resumed_task));
712 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
715 #ifdef BUILD_TIED_TASK_STACK 716 if (taskdata->td_flags.tiedness == TASK_TIED) {
717 __kmp_pop_task_stack(gtid, thread, taskdata);
721 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
724 kmp_int32 counter = KMP_ATOMIC_DEC(&taskdata->td_untied_count) - 1;
727 (
"__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n",
728 gtid, counter, taskdata));
732 if (resumed_task == NULL) {
733 KMP_DEBUG_ASSERT(taskdata->td_flags.task_serial);
734 resumed_task = taskdata->td_parent;
737 thread->th.th_current_task = resumed_task;
738 resumed_task->td_flags.executing = 1;
739 KA_TRACE(10, (
"__kmp_task_finish(exit): T#%d partially done task %p, " 740 "resuming task %p\n",
741 gtid, taskdata, resumed_task));
747 __ompt_task_finish(task, resumed_task);
750 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
751 taskdata->td_flags.complete = 1;
752 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 1);
753 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
757 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
760 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks) - 1;
761 KMP_DEBUG_ASSERT(children >= 0);
763 if (taskdata->td_taskgroup)
764 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
769 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) ||
770 (task_team && task_team->tt.tt_found_proxy_tasks)) {
772 __kmp_release_deps(gtid, taskdata);
779 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
780 taskdata->td_flags.executing = 0;
783 20, (
"__kmp_task_finish: T#%d finished task %p, %d incomplete children\n",
784 gtid, taskdata, children));
793 if (taskdata->td_flags.destructors_thunk) {
794 kmp_routine_entry_t destr_thunk = task->data1.destructors;
795 KMP_ASSERT(destr_thunk);
796 destr_thunk(gtid, task);
798 #endif // OMP_40_ENABLED 803 (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) ==
804 taskdata->td_flags.task_serial);
805 if (taskdata->td_flags.task_serial) {
806 if (resumed_task == NULL) {
807 resumed_task = taskdata->td_parent;
811 KMP_DEBUG_ASSERT(resumed_task !=
819 thread->th.th_current_task = resumed_task;
820 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
824 resumed_task->td_flags.executing = 1;
827 10, (
"__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n",
828 gtid, taskdata, resumed_task));
834 static void __kmpc_omp_task_complete_if0_template(
ident_t *loc_ref,
837 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n",
838 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
840 __kmp_task_finish<ompt>(gtid, task, NULL);
842 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n",
843 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
847 omp_frame_t *ompt_frame;
848 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
849 ompt_frame->enter_frame = NULL;
858 void __kmpc_omp_task_complete_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
860 __kmpc_omp_task_complete_if0_template<true>(loc_ref, gtid, task);
862 #endif // OMPT_SUPPORT 869 void __kmpc_omp_task_complete_if0(
ident_t *loc_ref, kmp_int32 gtid,
872 if (UNLIKELY(ompt_enabled.enabled)) {
873 __kmpc_omp_task_complete_if0_ompt(loc_ref, gtid, task);
877 __kmpc_omp_task_complete_if0_template<false>(loc_ref, gtid, task);
883 void __kmpc_omp_task_complete(
ident_t *loc_ref, kmp_int32 gtid,
885 KA_TRACE(10, (
"__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n", gtid,
886 loc_ref, KMP_TASK_TO_TASKDATA(task)));
888 __kmp_task_finish<false>(gtid, task,
891 KA_TRACE(10, (
"__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n", gtid,
892 loc_ref, KMP_TASK_TO_TASKDATA(task)));
895 #endif // TASK_UNUSED 908 void __kmp_init_implicit_task(
ident_t *loc_ref, kmp_info_t *this_thr,
909 kmp_team_t *team,
int tid,
int set_curr_task) {
910 kmp_taskdata_t *task = &team->t.t_implicit_task_taskdata[tid];
914 (
"__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n",
915 tid, team, task, set_curr_task ?
"TRUE" :
"FALSE"));
917 task->td_task_id = KMP_GEN_TASK_ID();
918 task->td_team = team;
921 task->td_ident = loc_ref;
922 task->td_taskwait_ident = NULL;
923 task->td_taskwait_counter = 0;
924 task->td_taskwait_thread = 0;
926 task->td_flags.tiedness = TASK_TIED;
927 task->td_flags.tasktype = TASK_IMPLICIT;
929 task->td_flags.proxy = TASK_FULL;
933 task->td_flags.task_serial = 1;
934 task->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
935 task->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
937 task->td_flags.started = 1;
938 task->td_flags.executing = 1;
939 task->td_flags.complete = 0;
940 task->td_flags.freed = 0;
943 task->td_depnode = NULL;
945 task->td_last_tied = task;
948 KMP_ATOMIC_ST_REL(&task->td_incomplete_child_tasks, 0);
950 KMP_ATOMIC_ST_REL(&task->td_allocated_child_tasks, 0);
952 task->td_taskgroup = NULL;
953 task->td_dephash = NULL;
955 __kmp_push_current_task_to_thread(this_thr, team, tid);
957 KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0);
958 KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0);
962 if (UNLIKELY(ompt_enabled.enabled))
963 __ompt_task_init(task, tid);
966 KF_TRACE(10, (
"__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n", tid,
975 void __kmp_finish_implicit_task(kmp_info_t *thread) {
976 kmp_taskdata_t *task = thread->th.th_current_task;
977 if (task->td_dephash)
978 __kmp_dephash_free_entries(thread, task->td_dephash);
985 void __kmp_free_implicit_task(kmp_info_t *thread) {
986 kmp_taskdata_t *task = thread->th.th_current_task;
987 if (task && task->td_dephash) {
988 __kmp_dephash_free(thread, task->td_dephash);
989 task->td_dephash = NULL;
995 static size_t __kmp_round_up_to_val(
size_t size,
size_t val) {
996 if (size & (val - 1)) {
998 if (size <= KMP_SIZE_T_MAX - val) {
1017 kmp_task_t *__kmp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1018 kmp_tasking_flags_t *flags,
1019 size_t sizeof_kmp_task_t,
size_t sizeof_shareds,
1020 kmp_routine_entry_t task_entry) {
1022 kmp_taskdata_t *taskdata;
1023 kmp_info_t *thread = __kmp_threads[gtid];
1024 kmp_team_t *team = thread->th.th_team;
1025 kmp_taskdata_t *parent_task = thread->th.th_current_task;
1026 size_t shareds_offset;
1028 if (!TCR_4(__kmp_init_middle))
1029 __kmp_middle_initialize();
1031 KA_TRACE(10, (
"__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) " 1032 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1033 gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t,
1034 sizeof_shareds, task_entry));
1036 if (parent_task->td_flags.final) {
1037 if (flags->merged_if0) {
1041 if (flags->tiedness == TASK_UNTIED && !team->t.t_serialized) {
1045 KMP_CHECK_UPDATE(thread->th.th_task_team->tt.tt_untied_task_encountered, 1);
1049 if (flags->proxy == TASK_PROXY) {
1050 flags->tiedness = TASK_UNTIED;
1051 flags->merged_if0 = 1;
1055 if ((thread->th.th_task_team) == NULL) {
1058 KMP_DEBUG_ASSERT(team->t.t_serialized);
1060 (
"T#%d creating task team in __kmp_task_alloc for proxy task\n",
1062 __kmp_task_team_setup(
1065 thread->th.th_task_team = team->t.t_task_team[thread->th.th_task_state];
1067 kmp_task_team_t *task_team = thread->th.th_task_team;
1070 if (!KMP_TASKING_ENABLED(task_team)) {
1073 (
"T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid));
1074 __kmp_enable_tasking(task_team, thread);
1075 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
1076 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
1078 if (thread_data->td.td_deque == NULL) {
1079 __kmp_alloc_task_deque(thread, thread_data);
1083 if (task_team->tt.tt_found_proxy_tasks == FALSE)
1084 TCW_4(task_team->tt.tt_found_proxy_tasks, TRUE);
1090 shareds_offset =
sizeof(kmp_taskdata_t) + sizeof_kmp_task_t;
1091 shareds_offset = __kmp_round_up_to_val(shareds_offset,
sizeof(
void *));
1094 KA_TRACE(30, (
"__kmp_task_alloc: T#%d First malloc size: %ld\n", gtid,
1096 KA_TRACE(30, (
"__kmp_task_alloc: T#%d Second malloc size: %ld\n", gtid,
1101 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, shareds_offset +
1104 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, shareds_offset +
1107 ANNOTATE_HAPPENS_AFTER(taskdata);
1109 task = KMP_TASKDATA_TO_TASK(taskdata);
1112 #if KMP_ARCH_X86 || KMP_ARCH_PPC64 || !KMP_HAVE_QUAD 1113 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(
double) - 1)) == 0);
1114 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(
double) - 1)) == 0);
1116 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(_Quad) - 1)) == 0);
1117 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(_Quad) - 1)) == 0);
1119 if (sizeof_shareds > 0) {
1121 task->shareds = &((
char *)taskdata)[shareds_offset];
1123 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
1126 task->shareds = NULL;
1128 task->routine = task_entry;
1131 taskdata->td_task_id = KMP_GEN_TASK_ID();
1132 taskdata->td_team = team;
1133 taskdata->td_alloc_thread = thread;
1134 taskdata->td_parent = parent_task;
1135 taskdata->td_level = parent_task->td_level + 1;
1136 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
1137 taskdata->td_ident = loc_ref;
1138 taskdata->td_taskwait_ident = NULL;
1139 taskdata->td_taskwait_counter = 0;
1140 taskdata->td_taskwait_thread = 0;
1141 KMP_DEBUG_ASSERT(taskdata->td_parent != NULL);
1144 if (flags->proxy == TASK_FULL)
1146 copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs);
1148 taskdata->td_flags.tiedness = flags->tiedness;
1149 taskdata->td_flags.final = flags->final;
1150 taskdata->td_flags.merged_if0 = flags->merged_if0;
1152 taskdata->td_flags.destructors_thunk = flags->destructors_thunk;
1153 #endif // OMP_40_ENABLED 1155 taskdata->td_flags.proxy = flags->proxy;
1156 taskdata->td_task_team = thread->th.th_task_team;
1157 taskdata->td_size_alloc = shareds_offset + sizeof_shareds;
1159 taskdata->td_flags.tasktype = TASK_EXPLICIT;
1162 taskdata->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1165 taskdata->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1171 taskdata->td_flags.task_serial =
1172 (parent_task->td_flags.final || taskdata->td_flags.team_serial ||
1173 taskdata->td_flags.tasking_ser);
1175 taskdata->td_flags.started = 0;
1176 taskdata->td_flags.executing = 0;
1177 taskdata->td_flags.complete = 0;
1178 taskdata->td_flags.freed = 0;
1180 taskdata->td_flags.native = flags->native;
1182 KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0);
1184 KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1);
1186 taskdata->td_taskgroup =
1187 parent_task->td_taskgroup;
1188 taskdata->td_dephash = NULL;
1189 taskdata->td_depnode = NULL;
1191 if (flags->tiedness == TASK_UNTIED)
1192 taskdata->td_last_tied = NULL;
1194 taskdata->td_last_tied = taskdata;
1197 if (UNLIKELY(ompt_enabled.enabled))
1198 __ompt_task_init(taskdata, gtid);
1203 if (flags->proxy == TASK_PROXY ||
1204 !(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser))
1206 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser))
1209 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
1211 if (parent_task->td_taskgroup)
1212 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
1216 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) {
1217 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
1221 KA_TRACE(20, (
"__kmp_task_alloc(exit): T#%d created task %p parent=%p\n",
1222 gtid, taskdata, taskdata->td_parent));
1223 ANNOTATE_HAPPENS_BEFORE(task);
1228 kmp_task_t *__kmpc_omp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1229 kmp_int32 flags,
size_t sizeof_kmp_task_t,
1230 size_t sizeof_shareds,
1231 kmp_routine_entry_t task_entry) {
1233 kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
1235 input_flags->native = FALSE;
1239 KA_TRACE(10, (
"__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s) " 1240 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1241 gtid, loc_ref, input_flags->tiedness ?
"tied " :
"untied",
1242 input_flags->proxy ?
"proxy" :
"", sizeof_kmp_task_t,
1243 sizeof_shareds, task_entry));
1245 KA_TRACE(10, (
"__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s) " 1246 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1247 gtid, loc_ref, input_flags->tiedness ?
"tied " :
"untied",
1248 sizeof_kmp_task_t, sizeof_shareds, task_entry));
1251 retval = __kmp_task_alloc(loc_ref, gtid, input_flags, sizeof_kmp_task_t,
1252 sizeof_shareds, task_entry);
1254 KA_TRACE(20, (
"__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval));
1264 static void __kmp_invoke_task(kmp_int32 gtid, kmp_task_t *task,
1265 kmp_taskdata_t *current_task) {
1266 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
1267 kmp_uint64 cur_time;
1272 30, (
"__kmp_invoke_task(enter): T#%d invoking task %p, current_task=%p\n",
1273 gtid, taskdata, current_task));
1274 KMP_DEBUG_ASSERT(task);
1276 if (taskdata->td_flags.proxy == TASK_PROXY &&
1277 taskdata->td_flags.complete == 1) {
1282 (
"__kmp_invoke_task: T#%d running bottom finish for proxy task %p\n",
1285 __kmp_bottom_half_finish_proxy(gtid, task);
1287 KA_TRACE(30, (
"__kmp_invoke_task(exit): T#%d completed bottom finish for " 1288 "proxy task %p, resuming task %p\n",
1289 gtid, taskdata, current_task));
1295 #if USE_ITT_BUILD && USE_ITT_NOTIFY 1296 if (__kmp_forkjoin_frames_mode == 3) {
1299 cur_time = __itt_get_timestamp();
1306 ompt_thread_info_t oldInfo;
1308 if (UNLIKELY(ompt_enabled.enabled)) {
1310 thread = __kmp_threads[gtid];
1311 oldInfo = thread->th.ompt_thread_info;
1312 thread->th.ompt_thread_info.wait_id = 0;
1313 thread->th.ompt_thread_info.state = (thread->th.th_team_serialized)
1314 ? omp_state_work_serial
1315 : omp_state_work_parallel;
1316 taskdata->ompt_task_info.frame.exit_frame = OMPT_GET_FRAME_ADDRESS(0);
1322 if (taskdata->td_flags.proxy != TASK_PROXY) {
1324 ANNOTATE_HAPPENS_AFTER(task);
1325 __kmp_task_start(gtid, task, current_task);
1334 if (__kmp_omp_cancellation) {
1335 kmp_info_t *this_thr = __kmp_threads[gtid];
1336 kmp_team_t *this_team = this_thr->th.th_team;
1337 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
1338 if ((taskgroup && taskgroup->cancel_request) ||
1339 (this_team->t.t_cancel_request == cancel_parallel)) {
1340 #if OMPT_SUPPORT && OMPT_OPTIONAL 1341 ompt_data_t *task_data;
1342 if (UNLIKELY(ompt_enabled.ompt_callback_cancel)) {
1343 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, NULL, NULL);
1344 ompt_callbacks.ompt_callback(ompt_callback_cancel)(
1346 ((taskgroup && taskgroup->cancel_request) ? ompt_cancel_taskgroup
1347 : ompt_cancel_parallel) |
1348 ompt_cancel_discarded_task,
1361 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
1362 taskdata->td_last_tied = current_task->td_last_tied;
1363 KMP_DEBUG_ASSERT(taskdata->td_last_tied);
1365 #if KMP_STATS_ENABLED 1367 switch (KMP_GET_THREAD_STATE()) {
1368 case FORK_JOIN_BARRIER:
1369 KMP_PUSH_PARTITIONED_TIMER(OMP_task_join_bar);
1372 KMP_PUSH_PARTITIONED_TIMER(OMP_task_plain_bar);
1375 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskyield);
1378 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskwait);
1381 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskgroup);
1384 KMP_PUSH_PARTITIONED_TIMER(OMP_task_immediate);
1387 #endif // KMP_STATS_ENABLED 1388 #endif // OMP_40_ENABLED 1392 if (UNLIKELY(ompt_enabled.enabled))
1393 __ompt_task_start(task, current_task, gtid);
1396 #ifdef KMP_GOMP_COMPAT 1397 if (taskdata->td_flags.native) {
1398 ((void (*)(
void *))(*(task->routine)))(task->shareds);
1402 (*(task->routine))(gtid, task);
1404 KMP_POP_PARTITIONED_TIMER();
1408 #endif // OMP_40_ENABLED 1413 if (taskdata->td_flags.proxy != TASK_PROXY) {
1415 ANNOTATE_HAPPENS_BEFORE(taskdata->td_parent);
1417 if (UNLIKELY(ompt_enabled.enabled)) {
1418 thread->th.ompt_thread_info = oldInfo;
1419 if (taskdata->td_flags.tiedness == TASK_TIED) {
1420 taskdata->ompt_task_info.frame.exit_frame = NULL;
1422 __kmp_task_finish<true>(gtid, task, current_task);
1425 __kmp_task_finish<false>(gtid, task, current_task);
1430 #if USE_ITT_BUILD && USE_ITT_NOTIFY 1432 if (__kmp_forkjoin_frames_mode == 3) {
1433 kmp_info_t *this_thr = __kmp_threads[gtid];
1434 if (this_thr->th.th_bar_arrive_time) {
1435 this_thr->th.th_bar_arrive_time += (__itt_get_timestamp() - cur_time);
1441 (
"__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n",
1442 gtid, taskdata, current_task));
1456 kmp_int32 __kmpc_omp_task_parts(
ident_t *loc_ref, kmp_int32 gtid,
1457 kmp_task_t *new_task) {
1458 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1460 KA_TRACE(10, (
"__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n", gtid,
1461 loc_ref, new_taskdata));
1464 kmp_taskdata_t *parent;
1465 if (UNLIKELY(ompt_enabled.enabled)) {
1466 parent = new_taskdata->td_parent;
1467 if (ompt_enabled.ompt_callback_task_create) {
1468 ompt_data_t task_data = ompt_data_none;
1469 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1470 parent ? &(parent->ompt_task_info.task_data) : &task_data,
1471 parent ? &(parent->ompt_task_info.frame) : NULL,
1472 &(new_taskdata->ompt_task_info.task_data), ompt_task_explicit, 0,
1473 OMPT_GET_RETURN_ADDRESS(0));
1481 if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1483 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1484 new_taskdata->td_flags.task_serial = 1;
1485 __kmp_invoke_task(gtid, new_task, current_task);
1490 (
"__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: " 1491 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
1492 gtid, loc_ref, new_taskdata));
1494 ANNOTATE_HAPPENS_BEFORE(new_task);
1496 if (UNLIKELY(ompt_enabled.enabled)) {
1497 parent->ompt_task_info.frame.enter_frame = NULL;
1500 return TASK_CURRENT_NOT_QUEUED;
1514 kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task,
1515 bool serialize_immediate) {
1516 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1521 if (new_taskdata->td_flags.proxy == TASK_PROXY ||
1522 __kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1524 if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1527 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1528 if (serialize_immediate)
1529 new_taskdata->td_flags.task_serial = 1;
1530 __kmp_invoke_task(gtid, new_task, current_task);
1533 ANNOTATE_HAPPENS_BEFORE(new_task);
1534 return TASK_CURRENT_NOT_QUEUED;
1549 kmp_int32 __kmpc_omp_task(
ident_t *loc_ref, kmp_int32 gtid,
1550 kmp_task_t *new_task) {
1552 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1554 #if KMP_DEBUG || OMPT_SUPPORT 1555 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1557 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1561 kmp_taskdata_t *parent = NULL;
1562 if (UNLIKELY(ompt_enabled.enabled)) {
1563 if (!new_taskdata->td_flags.started) {
1564 OMPT_STORE_RETURN_ADDRESS(gtid);
1565 parent = new_taskdata->td_parent;
1566 if (!parent->ompt_task_info.frame.enter_frame) {
1567 parent->ompt_task_info.frame.enter_frame = OMPT_GET_FRAME_ADDRESS(1);
1569 if (ompt_enabled.ompt_callback_task_create) {
1570 ompt_data_t task_data = ompt_data_none;
1571 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1572 parent ? &(parent->ompt_task_info.task_data) : &task_data,
1573 parent ? &(parent->ompt_task_info.frame) : NULL,
1574 &(new_taskdata->ompt_task_info.task_data),
1575 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1576 OMPT_LOAD_RETURN_ADDRESS(gtid));
1581 __ompt_task_finish(new_task,
1582 new_taskdata->ompt_task_info.scheduling_parent,
1584 new_taskdata->ompt_task_info.frame.exit_frame = NULL;
1589 res = __kmp_omp_task(gtid, new_task,
true);
1591 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning " 1592 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1593 gtid, loc_ref, new_taskdata));
1595 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
1596 parent->ompt_task_info.frame.enter_frame = NULL;
1615 kmp_int32 __kmp_omp_taskloop_task(
ident_t *loc_ref, kmp_int32 gtid,
1616 kmp_task_t *new_task,
void *codeptr_ra) {
1618 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1620 #if KMP_DEBUG || OMPT_SUPPORT 1621 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1623 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1627 kmp_taskdata_t *parent = NULL;
1628 if (UNLIKELY(ompt_enabled.enabled && !new_taskdata->td_flags.started)) {
1629 parent = new_taskdata->td_parent;
1630 if (!parent->ompt_task_info.frame.enter_frame)
1631 parent->ompt_task_info.frame.enter_frame = OMPT_GET_FRAME_ADDRESS(1);
1632 if (ompt_enabled.ompt_callback_task_create) {
1633 ompt_data_t task_data = ompt_data_none;
1634 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1635 parent ? &(parent->ompt_task_info.task_data) : &task_data,
1636 parent ? &(parent->ompt_task_info.frame) : NULL,
1637 &(new_taskdata->ompt_task_info.task_data),
1638 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1644 res = __kmp_omp_task(gtid, new_task,
true);
1646 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning " 1647 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1648 gtid, loc_ref, new_taskdata));
1650 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
1651 parent->ompt_task_info.frame.enter_frame = NULL;
1657 template <
bool ompt>
1658 static kmp_int32 __kmpc_omp_taskwait_template(
ident_t *loc_ref, kmp_int32 gtid,
1659 void *frame_address,
1660 void *return_address) {
1661 kmp_taskdata_t *taskdata;
1663 int thread_finished = FALSE;
1664 KMP_SET_THREAD_STATE_BLOCK(TASKWAIT);
1666 KA_TRACE(10, (
"__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref));
1668 if (__kmp_tasking_mode != tskm_immediate_exec) {
1669 thread = __kmp_threads[gtid];
1670 taskdata = thread->th.th_current_task;
1672 #if OMPT_SUPPORT && OMPT_OPTIONAL 1673 ompt_data_t *my_task_data;
1674 ompt_data_t *my_parallel_data;
1677 my_task_data = &(taskdata->ompt_task_info.task_data);
1678 my_parallel_data = OMPT_CUR_TEAM_DATA(thread);
1680 taskdata->ompt_task_info.frame.enter_frame = frame_address;
1682 if (ompt_enabled.ompt_callback_sync_region) {
1683 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
1684 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
1685 my_task_data, return_address);
1688 if (ompt_enabled.ompt_callback_sync_region_wait) {
1689 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
1690 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
1691 my_task_data, return_address);
1694 #endif // OMPT_SUPPORT && OMPT_OPTIONAL 1701 taskdata->td_taskwait_counter += 1;
1702 taskdata->td_taskwait_ident = loc_ref;
1703 taskdata->td_taskwait_thread = gtid + 1;
1706 void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
1707 if (itt_sync_obj != NULL)
1708 __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
1712 !taskdata->td_flags.team_serial && !taskdata->td_flags.final;
1715 must_wait = must_wait || (thread->th.th_task_team != NULL &&
1716 thread->th.th_task_team->tt.tt_found_proxy_tasks);
1719 kmp_flag_32 flag(RCAST(std::atomic<kmp_uint32> *,
1720 &(taskdata->td_incomplete_child_tasks)),
1722 while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) != 0) {
1723 flag.execute_tasks(thread, gtid, FALSE,
1724 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
1725 __kmp_task_stealing_constraint);
1729 if (itt_sync_obj != NULL)
1730 __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
1735 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
1737 #if OMPT_SUPPORT && OMPT_OPTIONAL 1739 if (ompt_enabled.ompt_callback_sync_region_wait) {
1740 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
1741 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
1742 my_task_data, return_address);
1744 if (ompt_enabled.ompt_callback_sync_region) {
1745 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
1746 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
1747 my_task_data, return_address);
1749 taskdata->ompt_task_info.frame.enter_frame = NULL;
1751 #endif // OMPT_SUPPORT && OMPT_OPTIONAL 1753 ANNOTATE_HAPPENS_AFTER(taskdata);
1756 KA_TRACE(10, (
"__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, " 1757 "returning TASK_CURRENT_NOT_QUEUED\n",
1760 return TASK_CURRENT_NOT_QUEUED;
1765 static kmp_int32 __kmpc_omp_taskwait_ompt(
ident_t *loc_ref, kmp_int32 gtid,
1766 void *frame_address,
1767 void *return_address) {
1768 return __kmpc_omp_taskwait_template<true>(loc_ref, gtid, frame_address,
1771 #endif // OMPT_SUPPORT 1775 kmp_int32 __kmpc_omp_taskwait(
ident_t *loc_ref, kmp_int32 gtid) {
1776 #if OMPT_SUPPORT && OMPT_OPTIONAL 1777 if (UNLIKELY(ompt_enabled.enabled)) {
1778 OMPT_STORE_RETURN_ADDRESS(gtid);
1779 return __kmpc_omp_taskwait_ompt(loc_ref, gtid, OMPT_GET_FRAME_ADDRESS(1),
1780 OMPT_LOAD_RETURN_ADDRESS(gtid));
1783 return __kmpc_omp_taskwait_template<false>(loc_ref, gtid, NULL, NULL);
1787 kmp_int32 __kmpc_omp_taskyield(
ident_t *loc_ref, kmp_int32 gtid,
int end_part) {
1788 kmp_taskdata_t *taskdata;
1790 int thread_finished = FALSE;
1793 KMP_SET_THREAD_STATE_BLOCK(TASKYIELD);
1795 KA_TRACE(10, (
"__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n",
1796 gtid, loc_ref, end_part));
1798 if (__kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel) {
1799 thread = __kmp_threads[gtid];
1800 taskdata = thread->th.th_current_task;
1807 taskdata->td_taskwait_counter += 1;
1808 taskdata->td_taskwait_ident = loc_ref;
1809 taskdata->td_taskwait_thread = gtid + 1;
1812 void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
1813 if (itt_sync_obj != NULL)
1814 __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
1816 if (!taskdata->td_flags.team_serial) {
1817 kmp_task_team_t *task_team = thread->th.th_task_team;
1818 if (task_team != NULL) {
1819 if (KMP_TASKING_ENABLED(task_team)) {
1821 if (UNLIKELY(ompt_enabled.enabled))
1822 thread->th.ompt_thread_info.ompt_task_yielded = 1;
1824 __kmp_execute_tasks_32(
1825 thread, gtid, NULL, FALSE,
1826 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
1827 __kmp_task_stealing_constraint);
1829 if (UNLIKELY(ompt_enabled.enabled))
1830 thread->th.ompt_thread_info.ompt_task_yielded = 0;
1836 if (itt_sync_obj != NULL)
1837 __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
1842 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
1845 KA_TRACE(10, (
"__kmpc_omp_taskyield(exit): T#%d task %p resuming, " 1846 "returning TASK_CURRENT_NOT_QUEUED\n",
1849 return TASK_CURRENT_NOT_QUEUED;
1856 typedef struct kmp_task_red_flags {
1857 unsigned lazy_priv : 1;
1858 unsigned reserved31 : 31;
1859 } kmp_task_red_flags_t;
1862 typedef struct kmp_task_red_data {
1870 kmp_task_red_flags_t flags;
1871 } kmp_task_red_data_t;
1874 typedef struct kmp_task_red_input {
1880 kmp_task_red_flags_t flags;
1881 } kmp_task_red_input_t;
1892 void *__kmpc_task_reduction_init(
int gtid,
int num,
void *data) {
1893 kmp_info_t *thread = __kmp_threads[gtid];
1894 kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup;
1895 kmp_int32 nth = thread->th.th_team_nproc;
1896 kmp_task_red_input_t *input = (kmp_task_red_input_t *)data;
1897 kmp_task_red_data_t *arr;
1900 KMP_ASSERT(tg != NULL);
1901 KMP_ASSERT(data != NULL);
1902 KMP_ASSERT(num > 0);
1904 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n",
1908 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, taskgroup %p, #items %d\n",
1910 arr = (kmp_task_red_data_t *)__kmp_thread_malloc(
1911 thread, num *
sizeof(kmp_task_red_data_t));
1912 for (
int i = 0; i < num; ++i) {
1913 void (*f_init)(
void *) = (
void (*)(
void *))(input[i].reduce_init);
1914 size_t size = input[i].reduce_size - 1;
1916 size += CACHE_LINE - size % CACHE_LINE;
1917 KMP_ASSERT(input[i].reduce_comb != NULL);
1918 arr[i].reduce_shar = input[i].reduce_shar;
1919 arr[i].reduce_size = size;
1920 arr[i].reduce_init = input[i].reduce_init;
1921 arr[i].reduce_fini = input[i].reduce_fini;
1922 arr[i].reduce_comb = input[i].reduce_comb;
1923 arr[i].flags = input[i].flags;
1924 if (!input[i].flags.lazy_priv) {
1926 arr[i].reduce_priv = __kmp_allocate(nth * size);
1927 arr[i].reduce_pend = (
char *)(arr[i].reduce_priv) + nth * size;
1928 if (f_init != NULL) {
1930 for (
int j = 0; j < nth; ++j) {
1931 f_init((
char *)(arr[i].reduce_priv) + j * size);
1937 arr[i].reduce_priv = __kmp_allocate(nth *
sizeof(
void *));
1940 tg->reduce_data = (
void *)arr;
1941 tg->reduce_num_data = num;
1954 void *__kmpc_task_reduction_get_th_data(
int gtid,
void *tskgrp,
void *data) {
1955 kmp_info_t *thread = __kmp_threads[gtid];
1956 kmp_int32 nth = thread->th.th_team_nproc;
1960 kmp_taskgroup_t *tg = (kmp_taskgroup_t *)tskgrp;
1962 tg = thread->th.th_current_task->td_taskgroup;
1963 KMP_ASSERT(tg != NULL);
1964 kmp_task_red_data_t *arr = (kmp_task_red_data_t *)(tg->reduce_data);
1965 kmp_int32 num = tg->reduce_num_data;
1966 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
1968 KMP_ASSERT(data != NULL);
1969 while (tg != NULL) {
1970 for (
int i = 0; i < num; ++i) {
1971 if (!arr[i].flags.lazy_priv) {
1972 if (data == arr[i].reduce_shar ||
1973 (data >= arr[i].reduce_priv && data < arr[i].reduce_pend))
1974 return (
char *)(arr[i].reduce_priv) + tid * arr[i].reduce_size;
1977 void **p_priv = (
void **)(arr[i].reduce_priv);
1978 if (data == arr[i].reduce_shar)
1981 for (
int j = 0; j < nth; ++j)
1982 if (data == p_priv[j])
1986 if (p_priv[tid] == NULL) {
1988 void (*f_init)(
void *) = (
void (*)(
void *))(arr[i].reduce_init);
1989 p_priv[tid] = __kmp_allocate(arr[i].reduce_size);
1990 if (f_init != NULL) {
1991 f_init(p_priv[tid]);
1998 arr = (kmp_task_red_data_t *)(tg->reduce_data);
1999 num = tg->reduce_num_data;
2001 KMP_ASSERT2(0,
"Unknown task reduction item");
2007 static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg) {
2008 kmp_int32 nth = th->th.th_team_nproc;
2009 KMP_DEBUG_ASSERT(nth > 1);
2010 kmp_task_red_data_t *arr = (kmp_task_red_data_t *)tg->reduce_data;
2011 kmp_int32 num = tg->reduce_num_data;
2012 for (
int i = 0; i < num; ++i) {
2013 void *sh_data = arr[i].reduce_shar;
2014 void (*f_fini)(
void *) = (
void (*)(
void *))(arr[i].reduce_fini);
2015 void (*f_comb)(
void *,
void *) =
2016 (
void (*)(
void *,
void *))(arr[i].reduce_comb);
2017 if (!arr[i].flags.lazy_priv) {
2018 void *pr_data = arr[i].reduce_priv;
2019 size_t size = arr[i].reduce_size;
2020 for (
int j = 0; j < nth; ++j) {
2021 void *priv_data = (
char *)pr_data + j * size;
2022 f_comb(sh_data, priv_data);
2027 void **pr_data = (
void **)(arr[i].reduce_priv);
2028 for (
int j = 0; j < nth; ++j) {
2029 if (pr_data[j] != NULL) {
2030 f_comb(sh_data, pr_data[j]);
2033 __kmp_free(pr_data[j]);
2037 __kmp_free(arr[i].reduce_priv);
2039 __kmp_thread_free(th, arr);
2040 tg->reduce_data = NULL;
2041 tg->reduce_num_data = 0;
2047 void __kmpc_taskgroup(
ident_t *loc,
int gtid) {
2048 kmp_info_t *thread = __kmp_threads[gtid];
2049 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2050 kmp_taskgroup_t *tg_new =
2051 (kmp_taskgroup_t *)__kmp_thread_malloc(thread,
sizeof(kmp_taskgroup_t));
2052 KA_TRACE(10, (
"__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new));
2053 KMP_ATOMIC_ST_RLX(&tg_new->count, 0);
2054 KMP_ATOMIC_ST_RLX(&tg_new->cancel_request, cancel_noreq);
2055 tg_new->parent = taskdata->td_taskgroup;
2058 tg_new->reduce_data = NULL;
2059 tg_new->reduce_num_data = 0;
2061 taskdata->td_taskgroup = tg_new;
2063 #if OMPT_SUPPORT && OMPT_OPTIONAL 2064 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2065 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2067 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2068 kmp_team_t *team = thread->th.th_team;
2069 ompt_data_t my_task_data = taskdata->ompt_task_info.task_data;
2071 ompt_data_t my_parallel_data = team->t.ompt_team_info.parallel_data;
2073 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2074 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2075 &(my_task_data), codeptr);
2082 void __kmpc_end_taskgroup(
ident_t *loc,
int gtid) {
2083 kmp_info_t *thread = __kmp_threads[gtid];
2084 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2085 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
2086 int thread_finished = FALSE;
2088 #if OMPT_SUPPORT && OMPT_OPTIONAL 2090 ompt_data_t my_task_data;
2091 ompt_data_t my_parallel_data;
2093 if (UNLIKELY(ompt_enabled.enabled)) {
2094 team = thread->th.th_team;
2095 my_task_data = taskdata->ompt_task_info.task_data;
2097 my_parallel_data = team->t.ompt_team_info.parallel_data;
2098 codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2100 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2104 KA_TRACE(10, (
"__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc));
2105 KMP_DEBUG_ASSERT(taskgroup != NULL);
2106 KMP_SET_THREAD_STATE_BLOCK(TASKGROUP);
2108 if (__kmp_tasking_mode != tskm_immediate_exec) {
2110 taskdata->td_taskwait_counter += 1;
2111 taskdata->td_taskwait_ident = loc;
2112 taskdata->td_taskwait_thread = gtid + 1;
2116 void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
2117 if (itt_sync_obj != NULL)
2118 __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
2121 #if OMPT_SUPPORT && OMPT_OPTIONAL 2122 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2123 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2124 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2125 &(my_task_data), codeptr);
2130 if (!taskdata->td_flags.team_serial ||
2131 (thread->th.th_task_team != NULL &&
2132 thread->th.th_task_team->tt.tt_found_proxy_tasks))
2134 if (!taskdata->td_flags.team_serial)
2137 kmp_flag_32 flag(RCAST(std::atomic<kmp_uint32> *, &(taskgroup->count)),
2139 while (KMP_ATOMIC_LD_ACQ(&taskgroup->count) != 0) {
2140 flag.execute_tasks(thread, gtid, FALSE,
2141 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2142 __kmp_task_stealing_constraint);
2145 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2147 #if OMPT_SUPPORT && OMPT_OPTIONAL 2148 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2149 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2150 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2151 &(my_task_data), codeptr);
2156 if (itt_sync_obj != NULL)
2157 __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
2160 KMP_DEBUG_ASSERT(taskgroup->count == 0);
2164 if (taskgroup->reduce_data != NULL)
2165 __kmp_task_reduction_fini(thread, taskgroup);
2168 taskdata->td_taskgroup = taskgroup->parent;
2169 __kmp_thread_free(thread, taskgroup);
2171 KA_TRACE(10, (
"__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n",
2173 ANNOTATE_HAPPENS_AFTER(taskdata);
2175 #if OMPT_SUPPORT && OMPT_OPTIONAL 2176 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2177 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2178 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2179 &(my_task_data), codeptr);
2186 static kmp_task_t *__kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid,
2187 kmp_task_team_t *task_team,
2188 kmp_int32 is_constrained) {
2190 kmp_taskdata_t *taskdata;
2191 kmp_thread_data_t *thread_data;
2194 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2195 KMP_DEBUG_ASSERT(task_team->tt.tt_threads_data !=
2198 thread_data = &task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
2200 KA_TRACE(10, (
"__kmp_remove_my_task(enter): T#%d ntasks=%d head=%u tail=%u\n",
2201 gtid, thread_data->td.td_deque_ntasks,
2202 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2204 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2206 (
"__kmp_remove_my_task(exit #1): T#%d No tasks to remove: " 2207 "ntasks=%d head=%u tail=%u\n",
2208 gtid, thread_data->td.td_deque_ntasks,
2209 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2213 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
2215 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2216 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2218 (
"__kmp_remove_my_task(exit #2): T#%d No tasks to remove: " 2219 "ntasks=%d head=%u tail=%u\n",
2220 gtid, thread_data->td.td_deque_ntasks,
2221 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2225 tail = (thread_data->td.td_deque_tail - 1) &
2226 TASK_DEQUE_MASK(thread_data->td);
2227 taskdata = thread_data->td.td_deque[tail];
2229 if (is_constrained && (taskdata->td_flags.tiedness == TASK_TIED)) {
2233 kmp_taskdata_t *current = thread->th.th_current_task->td_last_tied;
2234 KMP_DEBUG_ASSERT(current != NULL);
2236 if (current->td_flags.tasktype == TASK_EXPLICIT ||
2237 current->td_taskwait_thread > 0) {
2238 kmp_int32 level = current->td_level;
2239 kmp_taskdata_t *parent = taskdata->td_parent;
2240 while (parent != current && parent->td_level > level) {
2241 parent = parent->td_parent;
2243 KMP_DEBUG_ASSERT(parent != NULL);
2245 if (parent != current) {
2247 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2248 KA_TRACE(10, (
"__kmp_remove_my_task(exit #2): T#%d No tasks to remove: " 2249 "ntasks=%d head=%u tail=%u\n",
2250 gtid, thread_data->td.td_deque_ntasks,
2251 thread_data->td.td_deque_head,
2252 thread_data->td.td_deque_tail));
2258 thread_data->td.td_deque_tail = tail;
2259 TCW_4(thread_data->td.td_deque_ntasks, thread_data->td.td_deque_ntasks - 1);
2261 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2263 KA_TRACE(10, (
"__kmp_remove_my_task(exit #2): T#%d task %p removed: " 2264 "ntasks=%d head=%u tail=%u\n",
2265 gtid, taskdata, thread_data->td.td_deque_ntasks,
2266 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2268 task = KMP_TASKDATA_TO_TASK(taskdata);
2275 static kmp_task_t *__kmp_steal_task(kmp_info_t *victim_thr, kmp_int32 gtid,
2276 kmp_task_team_t *task_team,
2277 std::atomic<kmp_int32> *unfinished_threads,
2278 int *thread_finished,
2279 kmp_int32 is_constrained) {
2281 kmp_taskdata_t *taskdata;
2282 kmp_taskdata_t *current;
2283 kmp_thread_data_t *victim_td, *threads_data;
2284 kmp_int32 level, target;
2285 kmp_int32 victim_tid;
2287 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2289 threads_data = task_team->tt.tt_threads_data;
2290 KMP_DEBUG_ASSERT(threads_data != NULL);
2292 victim_tid = victim_thr->th.th_info.ds.ds_tid;
2293 victim_td = &threads_data[victim_tid];
2295 KA_TRACE(10, (
"__kmp_steal_task(enter): T#%d try to steal from T#%d: " 2296 "task_team=%p ntasks=%d head=%u tail=%u\n",
2297 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
2298 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
2299 victim_td->td.td_deque_tail));
2301 if (TCR_4(victim_td->td.td_deque_ntasks) == 0) {
2302 KA_TRACE(10, (
"__kmp_steal_task(exit #1): T#%d could not steal from T#%d: " 2303 "task_team=%p ntasks=%d head=%u tail=%u\n",
2304 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
2305 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
2306 victim_td->td.td_deque_tail));
2310 __kmp_acquire_bootstrap_lock(&victim_td->td.td_deque_lock);
2312 int ntasks = TCR_4(victim_td->td.td_deque_ntasks);
2315 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2316 KA_TRACE(10, (
"__kmp_steal_task(exit #2): T#%d could not steal from T#%d: " 2317 "task_team=%p ntasks=%d head=%u tail=%u\n",
2318 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2319 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2323 KMP_DEBUG_ASSERT(victim_td->td.td_deque != NULL);
2325 taskdata = victim_td->td.td_deque[victim_td->td.td_deque_head];
2326 if (is_constrained && (taskdata->td_flags.tiedness == TASK_TIED)) {
2330 current = __kmp_threads[gtid]->th.th_current_task->td_last_tied;
2331 KMP_DEBUG_ASSERT(current != NULL);
2333 if (current->td_flags.tasktype == TASK_EXPLICIT ||
2334 current->td_taskwait_thread > 0) {
2335 level = current->td_level;
2336 kmp_taskdata_t *parent = taskdata->td_parent;
2337 while (parent != current && parent->td_level > level) {
2338 parent = parent->td_parent;
2340 KMP_DEBUG_ASSERT(parent != NULL);
2342 if (parent != current) {
2343 if (!task_team->tt.tt_untied_task_encountered) {
2345 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2347 (
"__kmp_steal_task(exit #3): T#%d could not steal from " 2348 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
2349 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2350 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2357 if (taskdata != NULL) {
2359 victim_td->td.td_deque_head =
2360 (victim_td->td.td_deque_head + 1) & TASK_DEQUE_MASK(victim_td->td);
2364 target = victim_td->td.td_deque_head;
2365 for (i = 1; i < ntasks; ++i) {
2366 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
2367 taskdata = victim_td->td.td_deque[target];
2368 if (taskdata->td_flags.tiedness == TASK_TIED) {
2370 kmp_taskdata_t *parent = taskdata->td_parent;
2372 while (parent != current && parent->td_level > level) {
2373 parent = parent->td_parent;
2374 KMP_DEBUG_ASSERT(parent != NULL);
2376 if (parent != current) {
2389 if (taskdata == NULL) {
2391 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2392 KA_TRACE(10, (
"__kmp_steal_task(exit #4): T#%d could not steal from " 2393 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
2394 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2395 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2399 for (i = i + 1; i < ntasks; ++i) {
2401 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
2402 victim_td->td.td_deque[prev] = victim_td->td.td_deque[target];
2405 KMP_DEBUG_ASSERT(victim_td->td.td_deque_tail ==
2406 ((target + 1) & TASK_DEQUE_MASK(victim_td->td)));
2407 victim_td->td.td_deque_tail = target;
2409 if (*thread_finished) {
2415 count = KMP_ATOMIC_INC(unfinished_threads);
2419 (
"__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n",
2420 gtid, count + 1, task_team));
2422 *thread_finished = FALSE;
2424 TCW_4(victim_td->td.td_deque_ntasks, ntasks - 1);
2426 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2430 (
"__kmp_steal_task(exit #5): T#%d stole task %p from T#%d: " 2431 "task_team=%p ntasks=%d head=%u tail=%u\n",
2432 gtid, taskdata, __kmp_gtid_from_thread(victim_thr), task_team,
2433 ntasks, victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2435 task = KMP_TASKDATA_TO_TASK(taskdata);
2449 static inline int __kmp_execute_tasks_template(
2450 kmp_info_t *thread, kmp_int32 gtid, C *flag,
int final_spin,
2451 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
2452 kmp_int32 is_constrained) {
2453 kmp_task_team_t *task_team = thread->th.th_task_team;
2454 kmp_thread_data_t *threads_data;
2456 kmp_info_t *other_thread;
2457 kmp_taskdata_t *current_task = thread->th.th_current_task;
2458 std::atomic<kmp_int32> *unfinished_threads;
2459 kmp_int32 nthreads, victim_tid = -2, use_own_tasks = 1, new_victim = 0,
2460 tid = thread->th.th_info.ds.ds_tid;
2462 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2463 KMP_DEBUG_ASSERT(thread == __kmp_threads[gtid]);
2465 if (task_team == NULL)
2468 KA_TRACE(15, (
"__kmp_execute_tasks_template(enter): T#%d final_spin=%d " 2469 "*thread_finished=%d\n",
2470 gtid, final_spin, *thread_finished));
2472 thread->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
2473 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
2474 KMP_DEBUG_ASSERT(threads_data != NULL);
2476 nthreads = task_team->tt.tt_nproc;
2477 unfinished_threads = &(task_team->tt.tt_unfinished_threads);
2479 KMP_DEBUG_ASSERT(nthreads > 1 || task_team->tt.tt_found_proxy_tasks);
2481 KMP_DEBUG_ASSERT(nthreads > 1);
2483 KMP_DEBUG_ASSERT(*unfinished_threads >= 0);
2489 if (use_own_tasks) {
2490 task = __kmp_remove_my_task(thread, gtid, task_team, is_constrained);
2492 if ((task == NULL) && (nthreads > 1)) {
2496 if (victim_tid == -2) {
2497 victim_tid = threads_data[tid].td.td_deque_last_stolen;
2500 other_thread = threads_data[victim_tid].td.td_thr;
2502 if (victim_tid != -1) {
2504 }
else if (!new_victim) {
2510 victim_tid = __kmp_get_random(thread) % (nthreads - 1);
2511 if (victim_tid >= tid) {
2515 other_thread = threads_data[victim_tid].td.td_thr;
2525 if ((__kmp_tasking_mode == tskm_task_teams) &&
2526 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) &&
2527 (TCR_PTR(CCAST(
void *, other_thread->th.th_sleep_loc)) !=
2530 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(other_thread),
2531 other_thread->th.th_sleep_loc);
2544 task = __kmp_steal_task(other_thread, gtid, task_team,
2545 unfinished_threads, thread_finished,
2549 if (threads_data[tid].td.td_deque_last_stolen != victim_tid) {
2550 threads_data[tid].td.td_deque_last_stolen = victim_tid;
2557 KMP_CHECK_UPDATE(threads_data[tid].td.td_deque_last_stolen, -1);
2566 #if USE_ITT_BUILD && USE_ITT_NOTIFY 2567 if (__itt_sync_create_ptr || KMP_ITT_DEBUG) {
2568 if (itt_sync_obj == NULL) {
2570 itt_sync_obj = __kmp_itt_barrier_object(gtid, bs_forkjoin_barrier);
2572 __kmp_itt_task_starting(itt_sync_obj);
2575 __kmp_invoke_task(gtid, task, current_task);
2577 if (itt_sync_obj != NULL)
2578 __kmp_itt_task_finished(itt_sync_obj);
2585 if (flag == NULL || (!final_spin && flag->done_check())) {
2588 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
2592 if (thread->th.th_task_team == NULL) {
2596 KMP_YIELD(__kmp_library == library_throughput);
2599 if (!use_own_tasks && TCR_4(threads_data[tid].td.td_deque_ntasks) != 0) {
2600 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d stolen task spawned " 2601 "other tasks, restart\n",
2614 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks) == 0)
2622 if (!*thread_finished) {
2625 count = KMP_ATOMIC_DEC(unfinished_threads) - 1;
2626 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d dec " 2627 "unfinished_threads to %d task_team=%p\n",
2628 gtid, count, task_team));
2629 *thread_finished = TRUE;
2637 if (flag != NULL && flag->done_check()) {
2640 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
2648 if (thread->th.th_task_team == NULL) {
2650 (
"__kmp_execute_tasks_template: T#%d no more tasks\n", gtid));
2663 (
"__kmp_execute_tasks_template: T#%d can't find work\n", gtid));
2669 int __kmp_execute_tasks_32(
2670 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32 *flag,
int final_spin,
2671 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
2672 kmp_int32 is_constrained) {
2673 return __kmp_execute_tasks_template(
2674 thread, gtid, flag, final_spin,
2675 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
2678 int __kmp_execute_tasks_64(
2679 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64 *flag,
int final_spin,
2680 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
2681 kmp_int32 is_constrained) {
2682 return __kmp_execute_tasks_template(
2683 thread, gtid, flag, final_spin,
2684 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
2687 int __kmp_execute_tasks_oncore(
2688 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag,
int final_spin,
2689 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
2690 kmp_int32 is_constrained) {
2691 return __kmp_execute_tasks_template(
2692 thread, gtid, flag, final_spin,
2693 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
2699 static void __kmp_enable_tasking(kmp_task_team_t *task_team,
2700 kmp_info_t *this_thr) {
2701 kmp_thread_data_t *threads_data;
2702 int nthreads, i, is_init_thread;
2704 KA_TRACE(10, (
"__kmp_enable_tasking(enter): T#%d\n",
2705 __kmp_gtid_from_thread(this_thr)));
2707 KMP_DEBUG_ASSERT(task_team != NULL);
2708 KMP_DEBUG_ASSERT(this_thr->th.th_team != NULL);
2710 nthreads = task_team->tt.tt_nproc;
2711 KMP_DEBUG_ASSERT(nthreads > 0);
2712 KMP_DEBUG_ASSERT(nthreads == this_thr->th.th_team->t.t_nproc);
2715 is_init_thread = __kmp_realloc_task_threads_data(this_thr, task_team);
2717 if (!is_init_thread) {
2721 (
"__kmp_enable_tasking(exit): T#%d: threads array already set up.\n",
2722 __kmp_gtid_from_thread(this_thr)));
2725 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
2726 KMP_DEBUG_ASSERT(threads_data != NULL);
2728 if ((__kmp_tasking_mode == tskm_task_teams) &&
2729 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME)) {
2733 for (i = 0; i < nthreads; i++) {
2734 volatile void *sleep_loc;
2735 kmp_info_t *thread = threads_data[i].td.td_thr;
2737 if (i == this_thr->th.th_info.ds.ds_tid) {
2746 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
2748 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d waking up thread T#%d\n",
2749 __kmp_gtid_from_thread(this_thr),
2750 __kmp_gtid_from_thread(thread)));
2751 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc);
2753 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d don't wake up thread T#%d\n",
2754 __kmp_gtid_from_thread(this_thr),
2755 __kmp_gtid_from_thread(thread)));
2760 KA_TRACE(10, (
"__kmp_enable_tasking(exit): T#%d\n",
2761 __kmp_gtid_from_thread(this_thr)));
2798 static kmp_task_team_t *__kmp_free_task_teams =
2801 kmp_bootstrap_lock_t __kmp_task_team_lock =
2802 KMP_BOOTSTRAP_LOCK_INITIALIZER(__kmp_task_team_lock);
2809 static void __kmp_alloc_task_deque(kmp_info_t *thread,
2810 kmp_thread_data_t *thread_data) {
2811 __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
2812 KMP_DEBUG_ASSERT(thread_data->td.td_deque == NULL);
2815 thread_data->td.td_deque_last_stolen = -1;
2817 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == 0);
2818 KMP_DEBUG_ASSERT(thread_data->td.td_deque_head == 0);
2819 KMP_DEBUG_ASSERT(thread_data->td.td_deque_tail == 0);
2823 (
"__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n",
2824 __kmp_gtid_from_thread(thread), INITIAL_TASK_DEQUE_SIZE, thread_data));
2828 thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
2829 INITIAL_TASK_DEQUE_SIZE *
sizeof(kmp_taskdata_t *));
2830 thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
2837 static void __kmp_realloc_task_deque(kmp_info_t *thread,
2838 kmp_thread_data_t *thread_data) {
2839 kmp_int32 size = TASK_DEQUE_SIZE(thread_data->td);
2840 kmp_int32 new_size = 2 * size;
2842 KE_TRACE(10, (
"__kmp_realloc_task_deque: T#%d reallocating deque[from %d to " 2843 "%d] for thread_data %p\n",
2844 __kmp_gtid_from_thread(thread), size, new_size, thread_data));
2846 kmp_taskdata_t **new_deque =
2847 (kmp_taskdata_t **)__kmp_allocate(new_size *
sizeof(kmp_taskdata_t *));
2850 for (i = thread_data->td.td_deque_head, j = 0; j < size;
2851 i = (i + 1) & TASK_DEQUE_MASK(thread_data->td), j++)
2852 new_deque[j] = thread_data->td.td_deque[i];
2854 __kmp_free(thread_data->td.td_deque);
2856 thread_data->td.td_deque_head = 0;
2857 thread_data->td.td_deque_tail = size;
2858 thread_data->td.td_deque = new_deque;
2859 thread_data->td.td_deque_size = new_size;
2865 static void __kmp_free_task_deque(kmp_thread_data_t *thread_data) {
2866 if (thread_data->td.td_deque != NULL) {
2867 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
2868 TCW_4(thread_data->td.td_deque_ntasks, 0);
2869 __kmp_free(thread_data->td.td_deque);
2870 thread_data->td.td_deque = NULL;
2871 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2874 #ifdef BUILD_TIED_TASK_STACK 2876 if (thread_data->td.td_susp_tied_tasks.ts_entries != TASK_STACK_EMPTY) {
2877 __kmp_free_task_stack(__kmp_thread_from_gtid(gtid), thread_data);
2879 #endif // BUILD_TIED_TASK_STACK 2889 static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
2890 kmp_task_team_t *task_team) {
2891 kmp_thread_data_t **threads_data_p;
2892 kmp_int32 nthreads, maxthreads;
2893 int is_init_thread = FALSE;
2895 if (TCR_4(task_team->tt.tt_found_tasks)) {
2900 threads_data_p = &task_team->tt.tt_threads_data;
2901 nthreads = task_team->tt.tt_nproc;
2902 maxthreads = task_team->tt.tt_max_threads;
2907 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
2909 if (!TCR_4(task_team->tt.tt_found_tasks)) {
2911 kmp_team_t *team = thread->th.th_team;
2914 is_init_thread = TRUE;
2915 if (maxthreads < nthreads) {
2917 if (*threads_data_p != NULL) {
2918 kmp_thread_data_t *old_data = *threads_data_p;
2919 kmp_thread_data_t *new_data = NULL;
2923 (
"__kmp_realloc_task_threads_data: T#%d reallocating " 2924 "threads data for task_team %p, new_size = %d, old_size = %d\n",
2925 __kmp_gtid_from_thread(thread), task_team, nthreads, maxthreads));
2930 new_data = (kmp_thread_data_t *)__kmp_allocate(
2931 nthreads *
sizeof(kmp_thread_data_t));
2933 KMP_MEMCPY_S((
void *)new_data, nthreads *
sizeof(kmp_thread_data_t),
2934 (
void *)old_data, maxthreads *
sizeof(kmp_thread_data_t));
2936 #ifdef BUILD_TIED_TASK_STACK 2938 for (i = maxthreads; i < nthreads; i++) {
2939 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
2940 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
2942 #endif // BUILD_TIED_TASK_STACK 2944 (*threads_data_p) = new_data;
2945 __kmp_free(old_data);
2947 KE_TRACE(10, (
"__kmp_realloc_task_threads_data: T#%d allocating " 2948 "threads data for task_team %p, size = %d\n",
2949 __kmp_gtid_from_thread(thread), task_team, nthreads));
2953 ANNOTATE_IGNORE_WRITES_BEGIN();
2954 *threads_data_p = (kmp_thread_data_t *)__kmp_allocate(
2955 nthreads *
sizeof(kmp_thread_data_t));
2956 ANNOTATE_IGNORE_WRITES_END();
2957 #ifdef BUILD_TIED_TASK_STACK 2959 for (i = 0; i < nthreads; i++) {
2960 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
2961 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
2963 #endif // BUILD_TIED_TASK_STACK 2965 task_team->tt.tt_max_threads = nthreads;
2968 KMP_DEBUG_ASSERT(*threads_data_p != NULL);
2972 for (i = 0; i < nthreads; i++) {
2973 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
2974 thread_data->td.td_thr = team->t.t_threads[i];
2976 if (thread_data->td.td_deque_last_stolen >= nthreads) {
2980 thread_data->td.td_deque_last_stolen = -1;
2985 TCW_SYNC_4(task_team->tt.tt_found_tasks, TRUE);
2988 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
2989 return is_init_thread;
2995 static void __kmp_free_task_threads_data(kmp_task_team_t *task_team) {
2996 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
2997 if (task_team->tt.tt_threads_data != NULL) {
2999 for (i = 0; i < task_team->tt.tt_max_threads; i++) {
3000 __kmp_free_task_deque(&task_team->tt.tt_threads_data[i]);
3002 __kmp_free(task_team->tt.tt_threads_data);
3003 task_team->tt.tt_threads_data = NULL;
3005 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3012 static kmp_task_team_t *__kmp_allocate_task_team(kmp_info_t *thread,
3014 kmp_task_team_t *task_team = NULL;
3017 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d entering; team = %p\n",
3018 (thread ? __kmp_gtid_from_thread(thread) : -1), team));
3020 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3022 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3023 if (__kmp_free_task_teams != NULL) {
3024 task_team = __kmp_free_task_teams;
3025 TCW_PTR(__kmp_free_task_teams, task_team->tt.tt_next);
3026 task_team->tt.tt_next = NULL;
3028 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3031 if (task_team == NULL) {
3032 KE_TRACE(10, (
"__kmp_allocate_task_team: T#%d allocating " 3033 "task team for team %p\n",
3034 __kmp_gtid_from_thread(thread), team));
3038 task_team = (kmp_task_team_t *)__kmp_allocate(
sizeof(kmp_task_team_t));
3039 __kmp_init_bootstrap_lock(&task_team->tt.tt_threads_lock);
3046 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3048 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3050 task_team->tt.tt_nproc = nthreads = team->t.t_nproc;
3052 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads, nthreads);
3053 TCW_4(task_team->tt.tt_active, TRUE);
3055 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d exiting; task_team = %p " 3056 "unfinished_threads init'd to %d\n",
3057 (thread ? __kmp_gtid_from_thread(thread) : -1), task_team,
3058 KMP_ATOMIC_LD_RLX(&task_team->tt.tt_unfinished_threads)));
3065 void __kmp_free_task_team(kmp_info_t *thread, kmp_task_team_t *task_team) {
3066 KA_TRACE(20, (
"__kmp_free_task_team: T#%d task_team = %p\n",
3067 thread ? __kmp_gtid_from_thread(thread) : -1, task_team));
3070 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3072 KMP_DEBUG_ASSERT(task_team->tt.tt_next == NULL);
3073 task_team->tt.tt_next = __kmp_free_task_teams;
3074 TCW_PTR(__kmp_free_task_teams, task_team);
3076 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3084 void __kmp_reap_task_teams(
void) {
3085 kmp_task_team_t *task_team;
3087 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3089 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3090 while ((task_team = __kmp_free_task_teams) != NULL) {
3091 __kmp_free_task_teams = task_team->tt.tt_next;
3092 task_team->tt.tt_next = NULL;
3095 if (task_team->tt.tt_threads_data != NULL) {
3096 __kmp_free_task_threads_data(task_team);
3098 __kmp_free(task_team);
3100 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3107 void __kmp_wait_to_unref_task_teams(
void) {
3112 KMP_INIT_YIELD(spins);
3120 for (thread = CCAST(kmp_info_t *, __kmp_thread_pool); thread != NULL;
3121 thread = thread->th.th_next_pool) {
3125 if (TCR_PTR(thread->th.th_task_team) == NULL) {
3126 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n",
3127 __kmp_gtid_from_thread(thread)));
3132 if (!__kmp_is_thread_alive(thread, &exit_val)) {
3133 thread->th.th_task_team = NULL;
3140 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: Waiting for T#%d to " 3141 "unreference task_team\n",
3142 __kmp_gtid_from_thread(thread)));
3144 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
3145 volatile void *sleep_loc;
3147 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3151 (
"__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n",
3152 __kmp_gtid_from_thread(thread), __kmp_gtid_from_thread(thread)));
3153 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc);
3163 KMP_YIELD(TCR_4(__kmp_nth) > __kmp_avail_proc);
3164 KMP_YIELD_SPIN(spins);
3170 void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team,
int always) {
3171 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3177 if (team->t.t_task_team[this_thr->th.th_task_state] == NULL &&
3178 (always || team->t.t_nproc > 1)) {
3179 team->t.t_task_team[this_thr->th.th_task_state] =
3180 __kmp_allocate_task_team(this_thr, team);
3181 KA_TRACE(20, (
"__kmp_task_team_setup: Master T#%d created new task_team %p " 3182 "for team %d at parity=%d\n",
3183 __kmp_gtid_from_thread(this_thr),
3184 team->t.t_task_team[this_thr->th.th_task_state],
3185 ((team != NULL) ? team->t.t_id : -1),
3186 this_thr->th.th_task_state));
3196 if (team->t.t_nproc > 1) {
3197 int other_team = 1 - this_thr->th.th_task_state;
3198 if (team->t.t_task_team[other_team] == NULL) {
3199 team->t.t_task_team[other_team] =
3200 __kmp_allocate_task_team(this_thr, team);
3201 KA_TRACE(20, (
"__kmp_task_team_setup: Master T#%d created second new " 3202 "task_team %p for team %d at parity=%d\n",
3203 __kmp_gtid_from_thread(this_thr),
3204 team->t.t_task_team[other_team],
3205 ((team != NULL) ? team->t.t_id : -1), other_team));
3208 kmp_task_team_t *task_team = team->t.t_task_team[other_team];
3209 if (!task_team->tt.tt_active ||
3210 team->t.t_nproc != task_team->tt.tt_nproc) {
3211 TCW_4(task_team->tt.tt_nproc, team->t.t_nproc);
3212 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3214 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3216 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads,
3218 TCW_4(task_team->tt.tt_active, TRUE);
3222 KA_TRACE(20, (
"__kmp_task_team_setup: Master T#%d reset next task_team " 3223 "%p for team %d at parity=%d\n",
3224 __kmp_gtid_from_thread(this_thr),
3225 team->t.t_task_team[other_team],
3226 ((team != NULL) ? team->t.t_id : -1), other_team));
3234 void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team) {
3235 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3239 this_thr->th.th_task_state = 1 - this_thr->th.th_task_state;
3242 TCW_PTR(this_thr->th.th_task_team,
3243 team->t.t_task_team[this_thr->th.th_task_state]);
3245 (
"__kmp_task_team_sync: Thread T#%d task team switched to task_team " 3246 "%p from Team #%d (parity=%d)\n",
3247 __kmp_gtid_from_thread(this_thr), this_thr->th.th_task_team,
3248 ((team != NULL) ? team->t.t_id : -1), this_thr->th.th_task_state));
3258 void __kmp_task_team_wait(
3259 kmp_info_t *this_thr,
3260 kmp_team_t *team USE_ITT_BUILD_ARG(
void *itt_sync_obj),
int wait) {
3261 kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state];
3263 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3264 KMP_DEBUG_ASSERT(task_team == this_thr->th.th_task_team);
3266 if ((task_team != NULL) && KMP_TASKING_ENABLED(task_team)) {
3268 KA_TRACE(20, (
"__kmp_task_team_wait: Master T#%d waiting for all tasks " 3269 "(for unfinished_threads to reach 0) on task_team = %p\n",
3270 __kmp_gtid_from_thread(this_thr), task_team));
3274 kmp_flag_32 flag(RCAST(std::atomic<kmp_uint32> *,
3275 &task_team->tt.tt_unfinished_threads),
3277 flag.wait(this_thr, TRUE USE_ITT_BUILD_ARG(itt_sync_obj));
3283 (
"__kmp_task_team_wait: Master T#%d deactivating task_team %p: " 3284 "setting active to false, setting local and team's pointer to NULL\n",
3285 __kmp_gtid_from_thread(this_thr), task_team));
3287 KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1 ||
3288 task_team->tt.tt_found_proxy_tasks == TRUE);
3289 TCW_SYNC_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3291 KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1);
3293 KMP_CHECK_UPDATE(task_team->tt.tt_untied_task_encountered, 0);
3294 TCW_SYNC_4(task_team->tt.tt_active, FALSE);
3297 TCW_PTR(this_thr->th.th_task_team, NULL);
3306 void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread,
int gtid) {
3307 std::atomic<kmp_uint32> *spin = RCAST(
3308 std::atomic<kmp_uint32> *,
3309 &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads);
3311 KMP_DEBUG_ASSERT(__kmp_tasking_mode == tskm_extra_barrier);
3314 KMP_FSYNC_SPIN_INIT(spin, NULL);
3316 kmp_flag_32 spin_flag(spin, 0U);
3317 while (!spin_flag.execute_tasks(thread, gtid, TRUE,
3318 &flag USE_ITT_BUILD_ARG(NULL), 0)) {
3321 KMP_FSYNC_SPIN_PREPARE(RCAST(
void *, spin));
3324 if (TCR_4(__kmp_global.g.g_done)) {
3325 if (__kmp_global.g.g_abort)
3326 __kmp_abort_thread();
3332 KMP_FSYNC_SPIN_ACQUIRED(RCAST(
void *, spin));
3343 static bool __kmp_give_task(kmp_info_t *thread, kmp_int32 tid, kmp_task_t *task,
3345 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
3346 kmp_task_team_t *task_team = taskdata->td_task_team;
3348 KA_TRACE(20, (
"__kmp_give_task: trying to give task %p to thread %d.\n",
3352 KMP_DEBUG_ASSERT(task_team != NULL);
3354 bool result =
false;
3355 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
3357 if (thread_data->td.td_deque == NULL) {
3361 (
"__kmp_give_task: thread %d has no queue while giving task %p.\n",
3366 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3367 TASK_DEQUE_SIZE(thread_data->td)) {
3370 (
"__kmp_give_task: queue is full while giving task %p to thread %d.\n",
3375 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
3378 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3379 __kmp_realloc_task_deque(thread, thread_data);
3383 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3385 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3386 TASK_DEQUE_SIZE(thread_data->td)) {
3387 KA_TRACE(30, (
"__kmp_give_task: queue is full while giving task %p to " 3393 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
3394 goto release_and_exit;
3396 __kmp_realloc_task_deque(thread, thread_data);
3402 thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
3404 thread_data->td.td_deque_tail =
3405 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
3406 TCW_4(thread_data->td.td_deque_ntasks,
3407 TCR_4(thread_data->td.td_deque_ntasks) + 1);
3410 KA_TRACE(30, (
"__kmp_give_task: successfully gave task %p to thread %d.\n",
3414 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3435 static void __kmp_first_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
3436 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
3437 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3438 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
3439 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
3441 taskdata->td_flags.complete = 1;
3443 if (taskdata->td_taskgroup)
3444 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
3448 KMP_ATOMIC_INC(&taskdata->td_incomplete_child_tasks);
3451 static void __kmp_second_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
3452 kmp_int32 children = 0;
3456 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks) - 1;
3457 KMP_DEBUG_ASSERT(children >= 0);
3460 KMP_ATOMIC_DEC(&taskdata->td_incomplete_child_tasks);
3463 static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask) {
3464 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3465 kmp_info_t *thread = __kmp_threads[gtid];
3467 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3468 KMP_DEBUG_ASSERT(taskdata->td_flags.complete ==
3473 while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) > 0)
3476 __kmp_release_deps(gtid, taskdata);
3477 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
3488 void __kmpc_proxy_task_completed(kmp_int32 gtid, kmp_task_t *ptask) {
3489 KMP_DEBUG_ASSERT(ptask != NULL);
3490 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3492 10, (
"__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n",
3495 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3497 __kmp_first_top_half_finish_proxy(taskdata);
3498 __kmp_second_top_half_finish_proxy(taskdata);
3499 __kmp_bottom_half_finish_proxy(gtid, ptask);
3502 (
"__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n",
3513 void __kmpc_proxy_task_completed_ooo(kmp_task_t *ptask) {
3514 KMP_DEBUG_ASSERT(ptask != NULL);
3515 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3519 (
"__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n",
3522 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3524 __kmp_first_top_half_finish_proxy(taskdata);
3528 kmp_team_t *team = taskdata->td_team;
3529 kmp_int32 nthreads = team->t.t_nproc;
3534 kmp_int32 start_k = 0;
3536 kmp_int32 k = start_k;
3540 thread = team->t.t_threads[k];
3541 k = (k + 1) % nthreads;
3547 }
while (!__kmp_give_task(thread, k, ptask, pass));
3549 __kmp_second_top_half_finish_proxy(taskdata);
3553 (
"__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n",
3563 kmp_task_t *__kmp_task_dup_alloc(kmp_info_t *thread, kmp_task_t *task_src) {
3565 kmp_taskdata_t *taskdata;
3566 kmp_taskdata_t *taskdata_src;
3567 kmp_taskdata_t *parent_task = thread->th.th_current_task;
3568 size_t shareds_offset;
3571 KA_TRACE(10, (
"__kmp_task_dup_alloc(enter): Th %p, source task %p\n", thread,
3573 taskdata_src = KMP_TASK_TO_TASKDATA(task_src);
3574 KMP_DEBUG_ASSERT(taskdata_src->td_flags.proxy ==
3576 KMP_DEBUG_ASSERT(taskdata_src->td_flags.tasktype == TASK_EXPLICIT);
3577 task_size = taskdata_src->td_size_alloc;
3580 KA_TRACE(30, (
"__kmp_task_dup_alloc: Th %p, malloc size %ld\n", thread,
3583 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, task_size);
3585 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, task_size);
3587 KMP_MEMCPY(taskdata, taskdata_src, task_size);
3589 task = KMP_TASKDATA_TO_TASK(taskdata);
3592 taskdata->td_task_id = KMP_GEN_TASK_ID();
3593 if (task->shareds != NULL) {
3594 shareds_offset = (
char *)task_src->shareds - (
char *)taskdata_src;
3595 task->shareds = &((
char *)taskdata)[shareds_offset];
3596 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
3599 taskdata->td_alloc_thread = thread;
3600 taskdata->td_parent = parent_task;
3601 taskdata->td_taskgroup =
3607 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
3608 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
3609 if (parent_task->td_taskgroup)
3610 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
3613 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT)
3614 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
3618 (
"__kmp_task_dup_alloc(exit): Th %p, created task %p, parent=%p\n",
3619 thread, taskdata, taskdata->td_parent));
3621 if (UNLIKELY(ompt_enabled.enabled))
3622 __ompt_task_init(taskdata, thread->th.th_info.ds.ds_gtid);
3631 typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32);
3633 KMP_BUILD_ASSERT(
sizeof(
long) == 4 ||
sizeof(
long) == 8);
3638 class kmp_taskloop_bounds_t {
3640 const kmp_taskdata_t *taskdata;
3641 size_t lower_offset;
3642 size_t upper_offset;
3645 kmp_taskloop_bounds_t(kmp_task_t *_task, kmp_uint64 *lb, kmp_uint64 *ub)
3646 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(task)),
3647 lower_offset((char *)lb - (char *)task),
3648 upper_offset((char *)ub - (char *)task) {
3649 KMP_DEBUG_ASSERT((
char *)lb > (
char *)_task);
3650 KMP_DEBUG_ASSERT((
char *)ub > (
char *)_task);
3652 kmp_taskloop_bounds_t(kmp_task_t *_task,
const kmp_taskloop_bounds_t &bounds)
3653 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(_task)),
3654 lower_offset(bounds.lower_offset), upper_offset(bounds.upper_offset) {}
3655 size_t get_lower_offset()
const {
return lower_offset; }
3656 size_t get_upper_offset()
const {
return upper_offset; }
3657 kmp_uint64 get_lb()
const {
3659 #if defined(KMP_GOMP_COMPAT) 3661 if (!taskdata->td_flags.native) {
3662 retval = *(kmp_int64 *)((
char *)task + lower_offset);
3665 if (taskdata->td_size_loop_bounds == 4) {
3666 kmp_int32 *lb = RCAST(kmp_int32 *, task->shareds);
3667 retval = (kmp_int64)*lb;
3669 kmp_int64 *lb = RCAST(kmp_int64 *, task->shareds);
3670 retval = (kmp_int64)*lb;
3674 retval = *(kmp_int64 *)((
char *)task + lower_offset);
3675 #endif // defined(KMP_GOMP_COMPAT) 3678 kmp_uint64 get_ub()
const {
3680 #if defined(KMP_GOMP_COMPAT) 3682 if (!taskdata->td_flags.native) {
3683 retval = *(kmp_int64 *)((
char *)task + upper_offset);
3686 if (taskdata->td_size_loop_bounds == 4) {
3687 kmp_int32 *ub = RCAST(kmp_int32 *, task->shareds) + 1;
3688 retval = (kmp_int64)*ub;
3690 kmp_int64 *ub = RCAST(kmp_int64 *, task->shareds) + 1;
3691 retval = (kmp_int64)*ub;
3695 retval = *(kmp_int64 *)((
char *)task + upper_offset);
3696 #endif // defined(KMP_GOMP_COMPAT) 3699 void set_lb(kmp_uint64 lb) {
3700 #if defined(KMP_GOMP_COMPAT) 3702 if (!taskdata->td_flags.native) {
3703 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
3706 if (taskdata->td_size_loop_bounds == 4) {
3707 kmp_uint32 *lower = RCAST(kmp_uint32 *, task->shareds);
3708 *lower = (kmp_uint32)lb;
3710 kmp_uint64 *lower = RCAST(kmp_uint64 *, task->shareds);
3711 *lower = (kmp_uint64)lb;
3715 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
3716 #endif // defined(KMP_GOMP_COMPAT) 3718 void set_ub(kmp_uint64 ub) {
3719 #if defined(KMP_GOMP_COMPAT) 3721 if (!taskdata->td_flags.native) {
3722 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
3725 if (taskdata->td_size_loop_bounds == 4) {
3726 kmp_uint32 *upper = RCAST(kmp_uint32 *, task->shareds) + 1;
3727 *upper = (kmp_uint32)ub;
3729 kmp_uint64 *upper = RCAST(kmp_uint64 *, task->shareds) + 1;
3730 *upper = (kmp_uint64)ub;
3734 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
3735 #endif // defined(KMP_GOMP_COMPAT) 3754 void __kmp_taskloop_linear(
ident_t *loc,
int gtid, kmp_task_t *task,
3755 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
3756 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
3757 kmp_uint64 grainsize, kmp_uint64 extras,
3764 KMP_TIME_PARTITIONED_BLOCK(OMP_taskloop_scheduling);
3765 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
3767 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
3768 kmp_uint64 lower = task_bounds.get_lb();
3769 kmp_uint64 upper = task_bounds.get_ub();
3771 kmp_info_t *thread = __kmp_threads[gtid];
3772 kmp_taskdata_t *current_task = thread->th.th_current_task;
3773 kmp_task_t *next_task;
3774 kmp_int32 lastpriv = 0;
3776 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras);
3777 KMP_DEBUG_ASSERT(num_tasks > extras);
3778 KMP_DEBUG_ASSERT(num_tasks > 0);
3779 KA_TRACE(20, (
"__kmp_taskloop_linear: T#%d: %lld tasks, grainsize %lld, " 3780 "extras %lld, i=%lld,%lld(%d)%lld, dup %p\n",
3781 gtid, num_tasks, grainsize, extras, lower, upper, ub_glob, st,
3785 for (i = 0; i < num_tasks; ++i) {
3786 kmp_uint64 chunk_minus_1;
3788 chunk_minus_1 = grainsize - 1;
3790 chunk_minus_1 = grainsize;
3793 upper = lower + st * chunk_minus_1;
3794 if (i == num_tasks - 1) {
3797 KMP_DEBUG_ASSERT(upper == *ub);
3798 if (upper == ub_glob)
3800 }
else if (st > 0) {
3801 KMP_DEBUG_ASSERT((kmp_uint64)st > *ub - upper);
3802 if ((kmp_uint64)st > ub_glob - upper)
3805 KMP_DEBUG_ASSERT(upper + st < *ub);
3806 if (upper - ub_glob < (kmp_uint64)(-st))
3810 next_task = __kmp_task_dup_alloc(thread, task);
3811 kmp_taskdata_t *next_taskdata = KMP_TASK_TO_TASKDATA(next_task);
3812 kmp_taskloop_bounds_t next_task_bounds =
3813 kmp_taskloop_bounds_t(next_task, task_bounds);
3816 next_task_bounds.set_lb(lower);
3817 if (next_taskdata->td_flags.native) {
3818 next_task_bounds.set_ub(upper + (st > 0 ? 1 : -1));
3820 next_task_bounds.set_ub(upper);
3822 if (ptask_dup != NULL)
3823 ptask_dup(next_task, task, lastpriv);
3825 (
"__kmp_taskloop_linear: T#%d; task #%llu: task %p: lower %lld, " 3826 "upper %lld stride %lld, (offsets %p %p)\n",
3827 gtid, i, next_task, lower, upper, st,
3828 next_task_bounds.get_lower_offset(),
3829 next_task_bounds.get_upper_offset()));
3831 __kmp_omp_taskloop_task(NULL, gtid, next_task,
3834 __kmp_omp_task(gtid, next_task,
true);
3839 __kmp_task_start(gtid, task, current_task);
3841 __kmp_task_finish<false>(gtid, task, current_task);
3846 typedef struct __taskloop_params {
3853 kmp_uint64 num_tasks;
3854 kmp_uint64 grainsize;
3857 kmp_uint64 num_t_min;
3861 } __taskloop_params_t;
3863 void __kmp_taskloop_recur(
ident_t *,
int, kmp_task_t *, kmp_uint64 *,
3864 kmp_uint64 *, kmp_int64, kmp_uint64, kmp_uint64,
3865 kmp_uint64, kmp_uint64, kmp_uint64, kmp_uint64,
3872 int __kmp_taskloop_task(
int gtid,
void *ptask) {
3873 __taskloop_params_t *p =
3874 (__taskloop_params_t *)((kmp_task_t *)ptask)->shareds;
3875 kmp_task_t *task = p->task;
3876 kmp_uint64 *lb = p->lb;
3877 kmp_uint64 *ub = p->ub;
3878 void *task_dup = p->task_dup;
3880 kmp_int64 st = p->st;
3881 kmp_uint64 ub_glob = p->ub_glob;
3882 kmp_uint64 num_tasks = p->num_tasks;
3883 kmp_uint64 grainsize = p->grainsize;
3884 kmp_uint64 extras = p->extras;
3885 kmp_uint64 tc = p->tc;
3886 kmp_uint64 num_t_min = p->num_t_min;
3888 void *codeptr_ra = p->codeptr_ra;
3891 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
3892 KMP_DEBUG_ASSERT(task != NULL);
3893 KA_TRACE(20, (
"__kmp_taskloop_task: T#%d, task %p: %lld tasks, grainsize" 3894 " %lld, extras %lld, i=%lld,%lld(%d), dup %p\n",
3895 gtid, taskdata, num_tasks, grainsize, extras, *lb, *ub, st,
3898 KMP_DEBUG_ASSERT(num_tasks * 2 + 1 > num_t_min);
3899 if (num_tasks > num_t_min)
3900 __kmp_taskloop_recur(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
3901 grainsize, extras, tc, num_t_min,
3907 __kmp_taskloop_linear(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
3908 grainsize, extras, tc,
3914 KA_TRACE(40, (
"__kmp_taskloop_task(exit): T#%d\n", gtid));
3935 void __kmp_taskloop_recur(
ident_t *loc,
int gtid, kmp_task_t *task,
3936 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
3937 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
3938 kmp_uint64 grainsize, kmp_uint64 extras,
3939 kmp_uint64 tc, kmp_uint64 num_t_min,
3945 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
3946 KMP_DEBUG_ASSERT(task != NULL);
3947 KMP_DEBUG_ASSERT(num_tasks > num_t_min);
3948 KA_TRACE(20, (
"__kmp_taskloop_recur: T#%d, task %p: %lld tasks, grainsize" 3949 " %lld, extras %lld, i=%lld,%lld(%d), dup %p\n",
3950 gtid, taskdata, num_tasks, grainsize, extras, *lb, *ub, st,
3953 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
3954 kmp_uint64 lower = *lb;
3955 kmp_uint64 upper = *ub;
3956 kmp_info_t *thread = __kmp_threads[gtid];
3958 kmp_task_t *next_task;
3959 kmp_int32 lastpriv = 0;
3960 size_t lower_offset =
3961 (
char *)lb - (
char *)task;
3962 size_t upper_offset =
3963 (
char *)ub - (
char *)task;
3965 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras);
3966 KMP_DEBUG_ASSERT(num_tasks > extras);
3967 KMP_DEBUG_ASSERT(num_tasks > 0);
3970 kmp_uint64 lb1, ub0, tc0, tc1, ext0, ext1;
3971 kmp_uint64 gr_size0 = grainsize;
3972 kmp_uint64 n_tsk0 = num_tasks >> 1;
3973 kmp_uint64 n_tsk1 = num_tasks - n_tsk0;
3974 if (n_tsk0 <= extras) {
3977 ext1 = extras - n_tsk0;
3978 tc0 = gr_size0 * n_tsk0;
3983 tc1 = grainsize * n_tsk1;
3986 ub0 = lower + st * (tc0 - 1);
3990 next_task = __kmp_task_dup_alloc(thread, task);
3992 *(kmp_uint64 *)((
char *)next_task + lower_offset) = lb1;
3993 if (ptask_dup != NULL)
3994 ptask_dup(next_task, task, 0);
3998 kmp_task_t *new_task =
3999 __kmpc_omp_task_alloc(loc, gtid, 1, 3 *
sizeof(
void *),
4000 sizeof(__taskloop_params_t), &__kmp_taskloop_task);
4001 __taskloop_params_t *p = (__taskloop_params_t *)new_task->shareds;
4002 p->task = next_task;
4003 p->lb = (kmp_uint64 *)((
char *)next_task + lower_offset);
4004 p->ub = (kmp_uint64 *)((
char *)next_task + upper_offset);
4005 p->task_dup = task_dup;
4007 p->ub_glob = ub_glob;
4008 p->num_tasks = n_tsk1;
4009 p->grainsize = grainsize;
4012 p->num_t_min = num_t_min;
4014 p->codeptr_ra = codeptr_ra;
4019 __kmp_omp_taskloop_task(NULL, gtid, new_task, codeptr_ra);
4021 __kmp_omp_task(gtid, new_task,
true);
4025 if (n_tsk0 > num_t_min)
4026 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0, gr_size0,
4027 ext0, tc0, num_t_min,
4033 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0,
4034 gr_size0, ext0, tc0,
4040 KA_TRACE(40, (
"__kmpc_taskloop_recur(exit): T#%d\n", gtid));
4059 void __kmpc_taskloop(
ident_t *loc,
int gtid, kmp_task_t *task,
int if_val,
4060 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
int nogroup,
4061 int sched, kmp_uint64 grainsize,
void *task_dup) {
4062 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4063 KMP_DEBUG_ASSERT(task != NULL);
4066 #if OMPT_SUPPORT && OMPT_OPTIONAL 4067 OMPT_STORE_RETURN_ADDRESS(gtid);
4069 __kmpc_taskgroup(loc, gtid);
4074 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4077 kmp_uint64 lower = task_bounds.get_lb();
4078 kmp_uint64 upper = task_bounds.get_ub();
4079 kmp_uint64 ub_glob = upper;
4080 kmp_uint64 num_tasks = 0, extras = 0;
4081 kmp_uint64 num_tasks_min = __kmp_taskloop_min_tasks;
4082 kmp_info_t *thread = __kmp_threads[gtid];
4083 kmp_taskdata_t *current_task = thread->th.th_current_task;
4085 KA_TRACE(20, (
"__kmpc_taskloop: T#%d, task %p, lb %lld, ub %lld, st %lld, " 4086 "grain %llu(%d), dup %p\n",
4087 gtid, taskdata, lower, upper, st, grainsize, sched, task_dup));
4091 tc = upper - lower + 1;
4092 }
else if (st < 0) {
4093 tc = (lower - upper) / (-st) + 1;
4095 tc = (upper - lower) / st + 1;
4098 KA_TRACE(20, (
"__kmpc_taskloop(exit): T#%d zero-trip loop\n", gtid));
4100 __kmp_task_start(gtid, task, current_task);
4102 __kmp_task_finish<false>(gtid, task, current_task);
4106 #if OMPT_SUPPORT && OMPT_OPTIONAL 4107 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
4108 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
4109 if (ompt_enabled.ompt_callback_work) {
4110 ompt_callbacks.ompt_callback(ompt_callback_work)(
4111 ompt_work_taskloop, ompt_scope_begin, &(team_info->parallel_data),
4112 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
4116 if (num_tasks_min == 0)
4119 KMP_MIN(thread->th.th_team_nproc * 10, INITIAL_TASK_DEQUE_SIZE);
4125 grainsize = thread->th.th_team_nproc * 10;
4127 if (grainsize > tc) {
4132 num_tasks = grainsize;
4133 grainsize = tc / num_tasks;
4134 extras = tc % num_tasks;
4138 if (grainsize > tc) {
4143 num_tasks = tc / grainsize;
4145 grainsize = tc / num_tasks;
4146 extras = tc % num_tasks;
4150 KMP_ASSERT2(0,
"unknown scheduling of taskloop");
4152 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras);
4153 KMP_DEBUG_ASSERT(num_tasks > extras);
4154 KMP_DEBUG_ASSERT(num_tasks > 0);
4160 taskdata->td_flags.task_serial = 1;
4161 taskdata->td_flags.tiedness = TASK_TIED;
4163 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4164 grainsize, extras, tc,
4166 OMPT_GET_RETURN_ADDRESS(0),
4171 }
else if (num_tasks > num_tasks_min && !taskdata->td_flags.native) {
4172 KA_TRACE(20, (
"__kmpc_taskloop: T#%d, go recursive: tc %llu, #tasks %llu" 4173 "(%lld), grain %llu, extras %llu\n",
4174 gtid, tc, num_tasks, num_tasks_min, grainsize, extras));
4175 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4176 grainsize, extras, tc, num_tasks_min,
4178 OMPT_GET_RETURN_ADDRESS(0),
4182 KA_TRACE(20, (
"__kmpc_taskloop: T#%d, go linear: tc %llu, #tasks %llu" 4183 "(%lld), grain %llu, extras %llu\n",
4184 gtid, tc, num_tasks, num_tasks_min, grainsize, extras));
4185 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4186 grainsize, extras, tc,
4188 OMPT_GET_RETURN_ADDRESS(0),
4193 #if OMPT_SUPPORT && OMPT_OPTIONAL 4194 if (ompt_enabled.ompt_callback_work) {
4195 ompt_callbacks.ompt_callback(ompt_callback_work)(
4196 ompt_work_taskloop, ompt_scope_end, &(team_info->parallel_data),
4197 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
4202 #if OMPT_SUPPORT && OMPT_OPTIONAL 4203 OMPT_STORE_RETURN_ADDRESS(gtid);
4205 __kmpc_end_taskgroup(loc, gtid);
4207 KA_TRACE(20, (
"__kmpc_taskloop(exit): T#%d\n", gtid));
#define KMP_COUNT_BLOCK(name)
Increments specified counter (name).