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_TEST_THEN_INC32(&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;
491 static inline void __ompt_task_finish(kmp_task_t *task,
492 kmp_taskdata_t *resumed_task) {
493 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
494 ompt_task_status_t status = ompt_task_complete;
495 if (taskdata->td_flags.tiedness == TASK_UNTIED &&
496 KMP_TEST_THEN_ADD32(&(taskdata->td_untied_count), 0) > 1)
497 status = ompt_task_others;
498 if (__kmp_omp_cancellation && taskdata->td_taskgroup &&
499 taskdata->td_taskgroup->cancel_request == cancel_taskgroup) {
500 status = ompt_task_cancel;
504 if (ompt_enabled.ompt_callback_task_schedule) {
505 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
506 &(taskdata->ompt_task_info.task_data), status,
507 &((resumed_task ? resumed_task
508 : (taskdata->ompt_task_info.scheduling_parent
509 ? taskdata->ompt_task_info.scheduling_parent
510 : taskdata->td_parent))
511 ->ompt_task_info.task_data));
517 static void __kmpc_omp_task_begin_if0_template(
ident_t *loc_ref, kmp_int32 gtid,
520 void *return_address) {
521 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
522 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
524 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(enter): T#%d loc=%p task=%p " 526 gtid, loc_ref, taskdata, current_task));
528 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
531 kmp_int32 counter = 1 + KMP_TEST_THEN_INC32(&taskdata->td_untied_count);
532 KA_TRACE(20, (
"__kmpc_omp_task_begin_if0: T#%d untied_count (%d) " 533 "incremented for task %p\n",
534 gtid, counter, taskdata));
537 taskdata->td_flags.task_serial =
539 __kmp_task_start(gtid, task, current_task);
543 if (current_task->ompt_task_info.frame.enter_frame == NULL) {
544 current_task->ompt_task_info.frame.enter_frame =
545 taskdata->ompt_task_info.frame.exit_frame = frame_address;
547 if (ompt_enabled.ompt_callback_task_create) {
548 ompt_task_info_t *parent_info = &(current_task->ompt_task_info);
549 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
550 &(parent_info->task_data), &(parent_info->frame),
551 &(taskdata->ompt_task_info.task_data),
552 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(taskdata), 0,
555 __ompt_task_start(task, current_task, gtid);
557 #endif // OMPT_SUPPORT 559 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(exit): T#%d loc=%p task=%p,\n", gtid,
565 static void __kmpc_omp_task_begin_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
568 void *return_address) {
569 __kmpc_omp_task_begin_if0_template<true>(loc_ref, gtid, task, frame_address,
572 #endif // OMPT_SUPPORT 580 void __kmpc_omp_task_begin_if0(
ident_t *loc_ref, kmp_int32 gtid,
583 if (UNLIKELY(ompt_enabled.enabled)) {
584 OMPT_STORE_RETURN_ADDRESS(gtid);
585 __kmpc_omp_task_begin_if0_ompt(loc_ref, gtid, task,
586 OMPT_GET_FRAME_ADDRESS(1),
587 OMPT_LOAD_RETURN_ADDRESS(gtid));
591 __kmpc_omp_task_begin_if0_template<false>(loc_ref, gtid, task, NULL, NULL);
597 void __kmpc_omp_task_begin(
ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task) {
598 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
602 (
"__kmpc_omp_task_begin(enter): T#%d loc=%p task=%p current_task=%p\n",
603 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task), current_task));
605 __kmp_task_start(gtid, task, current_task);
607 KA_TRACE(10, (
"__kmpc_omp_task_begin(exit): T#%d loc=%p task=%p,\n", gtid,
608 loc_ref, KMP_TASK_TO_TASKDATA(task)));
611 #endif // TASK_UNUSED 618 static void __kmp_free_task(kmp_int32 gtid, kmp_taskdata_t *taskdata,
619 kmp_info_t *thread) {
620 KA_TRACE(30, (
"__kmp_free_task: T#%d freeing data from task %p\n", gtid,
624 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
625 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0);
626 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 1);
627 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
628 KMP_DEBUG_ASSERT(TCR_4(taskdata->td_allocated_child_tasks) == 0 ||
629 taskdata->td_flags.task_serial == 1);
630 KMP_DEBUG_ASSERT(TCR_4(taskdata->td_incomplete_child_tasks) == 0);
632 taskdata->td_flags.freed = 1;
633 ANNOTATE_HAPPENS_BEFORE(taskdata);
636 __kmp_fast_free(thread, taskdata);
638 __kmp_thread_free(thread, taskdata);
641 KA_TRACE(20, (
"__kmp_free_task: T#%d freed task %p\n", gtid, taskdata));
650 static void __kmp_free_task_and_ancestors(kmp_int32 gtid,
651 kmp_taskdata_t *taskdata,
652 kmp_info_t *thread) {
656 kmp_int32 team_serial =
657 (taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) &&
658 !taskdata->td_flags.proxy;
660 kmp_int32 team_serial =
661 taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser;
663 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
666 KMP_TEST_THEN_DEC32(&taskdata->td_allocated_child_tasks) - 1;
667 KMP_DEBUG_ASSERT(children >= 0);
670 while (children == 0) {
671 kmp_taskdata_t *parent_taskdata = taskdata->td_parent;
673 KA_TRACE(20, (
"__kmp_free_task_and_ancestors(enter): T#%d task %p complete " 674 "and freeing itself\n",
678 __kmp_free_task(gtid, taskdata, thread);
680 taskdata = parent_taskdata;
684 if (team_serial || taskdata->td_flags.tasktype == TASK_IMPLICIT)
688 children = KMP_TEST_THEN_DEC32(&taskdata->td_allocated_child_tasks) - 1;
689 KMP_DEBUG_ASSERT(children >= 0);
693 20, (
"__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; " 694 "not freeing it yet\n",
695 gtid, taskdata, children));
703 static void __kmp_task_finish(kmp_int32 gtid, kmp_task_t *task,
704 kmp_taskdata_t *resumed_task) {
705 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
706 kmp_info_t *thread = __kmp_threads[gtid];
707 kmp_task_team_t *task_team =
708 thread->th.th_task_team;
709 kmp_int32 children = 0;
711 KA_TRACE(10, (
"__kmp_task_finish(enter): T#%d finishing task %p and resuming " 713 gtid, taskdata, resumed_task));
715 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
718 #ifdef BUILD_TIED_TASK_STACK 719 if (taskdata->td_flags.tiedness == TASK_TIED) {
720 __kmp_pop_task_stack(gtid, thread, taskdata);
724 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
727 kmp_int32 counter = KMP_TEST_THEN_DEC32(&taskdata->td_untied_count) - 1;
730 (
"__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n",
731 gtid, counter, taskdata));
735 if (resumed_task == NULL) {
736 KMP_DEBUG_ASSERT(taskdata->td_flags.task_serial);
737 resumed_task = taskdata->td_parent;
740 thread->th.th_current_task = resumed_task;
741 resumed_task->td_flags.executing = 1;
742 KA_TRACE(10, (
"__kmp_task_finish(exit): T#%d partially done task %p, " 743 "resuming task %p\n",
744 gtid, taskdata, resumed_task));
749 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
750 taskdata->td_flags.complete = 1;
751 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 1);
752 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
756 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
759 KMP_TEST_THEN_DEC32(&taskdata->td_parent->td_incomplete_child_tasks) -
761 KMP_DEBUG_ASSERT(children >= 0);
763 if (taskdata->td_taskgroup)
764 KMP_TEST_THEN_DEC32((kmp_int32 *)(&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(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 __ompt_task_finish(task, NULL);
848 ompt_frame_t *ompt_frame;
849 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
850 ompt_frame->enter_frame = NULL;
859 void __kmpc_omp_task_complete_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
861 __kmpc_omp_task_complete_if0_template<true>(loc_ref, gtid, task);
863 #endif // OMPT_SUPPORT 870 void __kmpc_omp_task_complete_if0(
ident_t *loc_ref, kmp_int32 gtid,
873 if (UNLIKELY(ompt_enabled.enabled)) {
874 __kmpc_omp_task_complete_if0_ompt(loc_ref, gtid, task);
878 __kmpc_omp_task_complete_if0_template<false>(loc_ref, gtid, task);
884 void __kmpc_omp_task_complete(
ident_t *loc_ref, kmp_int32 gtid,
886 KA_TRACE(10, (
"__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n", gtid,
887 loc_ref, KMP_TASK_TO_TASKDATA(task)));
889 __kmp_task_finish(gtid, task, NULL);
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 task->td_incomplete_child_tasks = 0;
950 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->td_dephash)
988 __kmp_dephash_free(thread, task->td_dephash);
989 task->td_dephash = NULL;
994 static size_t __kmp_round_up_to_val(
size_t size,
size_t val) {
995 if (size & (val - 1)) {
997 if (size <= KMP_SIZE_T_MAX - val) {
1016 kmp_task_t *__kmp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1017 kmp_tasking_flags_t *flags,
1018 size_t sizeof_kmp_task_t,
size_t sizeof_shareds,
1019 kmp_routine_entry_t task_entry) {
1021 kmp_taskdata_t *taskdata;
1022 kmp_info_t *thread = __kmp_threads[gtid];
1023 kmp_team_t *team = thread->th.th_team;
1024 kmp_taskdata_t *parent_task = thread->th.th_current_task;
1025 size_t shareds_offset;
1027 KA_TRACE(10, (
"__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) " 1028 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1029 gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t,
1030 sizeof_shareds, task_entry));
1032 if (parent_task->td_flags.final) {
1033 if (flags->merged_if0) {
1037 if (flags->tiedness == TASK_UNTIED && !team->t.t_serialized) {
1041 KMP_CHECK_UPDATE(thread->th.th_task_team->tt.tt_untied_task_encountered, 1);
1045 if (flags->proxy == TASK_PROXY) {
1046 flags->tiedness = TASK_UNTIED;
1047 flags->merged_if0 = 1;
1051 if ((thread->th.th_task_team) == NULL) {
1054 KMP_DEBUG_ASSERT(team->t.t_serialized);
1056 (
"T#%d creating task team in __kmp_task_alloc for proxy task\n",
1058 __kmp_task_team_setup(
1061 thread->th.th_task_team = team->t.t_task_team[thread->th.th_task_state];
1063 kmp_task_team_t *task_team = thread->th.th_task_team;
1066 if (!KMP_TASKING_ENABLED(task_team)) {
1069 (
"T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid));
1070 __kmp_enable_tasking(task_team, thread);
1071 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
1072 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
1074 if (thread_data->td.td_deque == NULL) {
1075 __kmp_alloc_task_deque(thread, thread_data);
1079 if (task_team->tt.tt_found_proxy_tasks == FALSE)
1080 TCW_4(task_team->tt.tt_found_proxy_tasks, TRUE);
1086 shareds_offset =
sizeof(kmp_taskdata_t) + sizeof_kmp_task_t;
1087 shareds_offset = __kmp_round_up_to_val(shareds_offset,
sizeof(
void *));
1090 KA_TRACE(30, (
"__kmp_task_alloc: T#%d First malloc size: %ld\n", gtid,
1092 KA_TRACE(30, (
"__kmp_task_alloc: T#%d Second malloc size: %ld\n", gtid,
1097 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, shareds_offset +
1100 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, shareds_offset +
1103 ANNOTATE_HAPPENS_AFTER(taskdata);
1105 task = KMP_TASKDATA_TO_TASK(taskdata);
1108 #if KMP_ARCH_X86 || KMP_ARCH_PPC64 || !KMP_HAVE_QUAD 1109 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(
double) - 1)) == 0);
1110 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(
double) - 1)) == 0);
1112 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(_Quad) - 1)) == 0);
1113 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(_Quad) - 1)) == 0);
1115 if (sizeof_shareds > 0) {
1117 task->shareds = &((
char *)taskdata)[shareds_offset];
1119 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
1122 task->shareds = NULL;
1124 task->routine = task_entry;
1127 taskdata->td_task_id = KMP_GEN_TASK_ID();
1128 taskdata->td_team = team;
1129 taskdata->td_alloc_thread = thread;
1130 taskdata->td_parent = parent_task;
1131 taskdata->td_level = parent_task->td_level + 1;
1132 taskdata->td_untied_count = 0;
1133 taskdata->td_ident = loc_ref;
1134 taskdata->td_taskwait_ident = NULL;
1135 taskdata->td_taskwait_counter = 0;
1136 taskdata->td_taskwait_thread = 0;
1137 KMP_DEBUG_ASSERT(taskdata->td_parent != NULL);
1140 if (flags->proxy == TASK_FULL)
1142 copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs);
1144 taskdata->td_flags.tiedness = flags->tiedness;
1145 taskdata->td_flags.final = flags->final;
1146 taskdata->td_flags.merged_if0 = flags->merged_if0;
1148 taskdata->td_flags.destructors_thunk = flags->destructors_thunk;
1149 #endif // OMP_40_ENABLED 1151 taskdata->td_flags.proxy = flags->proxy;
1152 taskdata->td_task_team = thread->th.th_task_team;
1153 taskdata->td_size_alloc = shareds_offset + sizeof_shareds;
1155 taskdata->td_flags.tasktype = TASK_EXPLICIT;
1158 taskdata->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1161 taskdata->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1167 taskdata->td_flags.task_serial =
1168 (parent_task->td_flags.final || taskdata->td_flags.team_serial ||
1169 taskdata->td_flags.tasking_ser);
1171 taskdata->td_flags.started = 0;
1172 taskdata->td_flags.executing = 0;
1173 taskdata->td_flags.complete = 0;
1174 taskdata->td_flags.freed = 0;
1176 taskdata->td_flags.native = flags->native;
1178 taskdata->td_incomplete_child_tasks = 0;
1179 taskdata->td_allocated_child_tasks = 1;
1182 taskdata->td_taskgroup =
1183 parent_task->td_taskgroup;
1184 taskdata->td_dephash = NULL;
1185 taskdata->td_depnode = NULL;
1187 if (flags->tiedness == TASK_UNTIED)
1188 taskdata->td_last_tied = NULL;
1190 taskdata->td_last_tied = taskdata;
1195 if (flags->proxy == TASK_PROXY ||
1196 !(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser))
1198 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser))
1201 KMP_TEST_THEN_INC32(&parent_task->td_incomplete_child_tasks);
1203 if (parent_task->td_taskgroup)
1204 KMP_TEST_THEN_INC32((kmp_int32 *)(&parent_task->td_taskgroup->count));
1208 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) {
1209 KMP_TEST_THEN_INC32(&taskdata->td_parent->td_allocated_child_tasks);
1213 KA_TRACE(20, (
"__kmp_task_alloc(exit): T#%d created task %p parent=%p\n",
1214 gtid, taskdata, taskdata->td_parent));
1215 ANNOTATE_HAPPENS_BEFORE(task);
1218 if (UNLIKELY(ompt_enabled.enabled))
1219 __ompt_task_init(taskdata, gtid);
1225 kmp_task_t *__kmpc_omp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1226 kmp_int32 flags,
size_t sizeof_kmp_task_t,
1227 size_t sizeof_shareds,
1228 kmp_routine_entry_t task_entry) {
1230 kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
1232 input_flags->native = FALSE;
1236 KA_TRACE(10, (
"__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s) " 1237 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1238 gtid, loc_ref, input_flags->tiedness ?
"tied " :
"untied",
1239 input_flags->proxy ?
"proxy" :
"", sizeof_kmp_task_t,
1240 sizeof_shareds, task_entry));
1242 KA_TRACE(10, (
"__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s) " 1243 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1244 gtid, loc_ref, input_flags->tiedness ?
"tied " :
"untied",
1245 sizeof_kmp_task_t, sizeof_shareds, task_entry));
1248 retval = __kmp_task_alloc(loc_ref, gtid, input_flags, sizeof_kmp_task_t,
1249 sizeof_shareds, task_entry);
1251 KA_TRACE(20, (
"__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval));
1261 static void __kmp_invoke_task(kmp_int32 gtid, kmp_task_t *task,
1262 kmp_taskdata_t *current_task) {
1263 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
1264 kmp_uint64 cur_time;
1269 30, (
"__kmp_invoke_task(enter): T#%d invoking task %p, current_task=%p\n",
1270 gtid, taskdata, current_task));
1271 KMP_DEBUG_ASSERT(task);
1273 if (taskdata->td_flags.proxy == TASK_PROXY &&
1274 taskdata->td_flags.complete == 1) {
1279 (
"__kmp_invoke_task: T#%d running bottom finish for proxy task %p\n",
1282 __kmp_bottom_half_finish_proxy(gtid, task);
1284 KA_TRACE(30, (
"__kmp_invoke_task(exit): T#%d completed bottom finish for " 1285 "proxy task %p, resuming task %p\n",
1286 gtid, taskdata, current_task));
1292 #if USE_ITT_BUILD && USE_ITT_NOTIFY 1293 if (__kmp_forkjoin_frames_mode == 3) {
1296 cur_time = __itt_get_timestamp();
1302 if (taskdata->td_flags.proxy != TASK_PROXY) {
1304 ANNOTATE_HAPPENS_AFTER(task);
1305 __kmp_task_start(gtid, task, current_task);
1311 ompt_thread_info_t oldInfo;
1313 if (UNLIKELY(ompt_enabled.enabled)) {
1315 thread = __kmp_threads[gtid];
1316 oldInfo = thread->th.ompt_thread_info;
1317 thread->th.ompt_thread_info.wait_id = 0;
1318 thread->th.ompt_thread_info.state = (thread->th.th_team_serialized)
1319 ? omp_state_work_serial
1320 : omp_state_work_parallel;
1321 taskdata->ompt_task_info.frame.exit_frame = OMPT_GET_FRAME_ADDRESS(0);
1329 if (__kmp_omp_cancellation) {
1330 kmp_info_t *this_thr = __kmp_threads[gtid];
1331 kmp_team_t *this_team = this_thr->th.th_team;
1332 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
1333 if ((taskgroup && taskgroup->cancel_request) ||
1334 (this_team->t.t_cancel_request == cancel_parallel)) {
1335 #if OMPT_SUPPORT && OMPT_OPTIONAL 1336 ompt_data_t *task_data;
1337 if (UNLIKELY(ompt_enabled.ompt_callback_cancel)) {
1338 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, NULL, NULL);
1339 ompt_callbacks.ompt_callback(ompt_callback_cancel)(
1341 ((taskgroup && taskgroup->cancel_request) ? ompt_cancel_taskgroup
1342 : ompt_cancel_parallel) |
1343 ompt_cancel_discarded_task,
1356 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
1357 taskdata->td_last_tied = current_task->td_last_tied;
1358 KMP_DEBUG_ASSERT(taskdata->td_last_tied);
1360 #if KMP_STATS_ENABLED 1362 switch (KMP_GET_THREAD_STATE()) {
1363 case FORK_JOIN_BARRIER:
1364 KMP_PUSH_PARTITIONED_TIMER(OMP_task_join_bar);
1367 KMP_PUSH_PARTITIONED_TIMER(OMP_task_plain_bar);
1370 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskyield);
1373 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskwait);
1376 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskgroup);
1379 KMP_PUSH_PARTITIONED_TIMER(OMP_task_immediate);
1382 #endif // KMP_STATS_ENABLED 1383 #endif // OMP_40_ENABLED 1387 if (UNLIKELY(ompt_enabled.enabled))
1388 __ompt_task_start(task, current_task, gtid);
1391 #ifdef KMP_GOMP_COMPAT 1392 if (taskdata->td_flags.native) {
1393 ((void (*)(
void *))(*(task->routine)))(task->shareds);
1397 (*(task->routine))(gtid, task);
1399 KMP_POP_PARTITIONED_TIMER();
1402 if (UNLIKELY(ompt_enabled.enabled))
1403 __ompt_task_finish(task, current_task);
1407 #endif // OMP_40_ENABLED 1410 if (UNLIKELY(ompt_enabled.enabled)) {
1411 thread->th.ompt_thread_info = oldInfo;
1412 taskdata->ompt_task_info.frame.exit_frame = NULL;
1418 if (taskdata->td_flags.proxy != TASK_PROXY) {
1420 ANNOTATE_HAPPENS_BEFORE(taskdata->td_parent);
1421 __kmp_task_finish(gtid, task, current_task);
1426 #if USE_ITT_BUILD && USE_ITT_NOTIFY 1428 if (__kmp_forkjoin_frames_mode == 3) {
1429 kmp_info_t *this_thr = __kmp_threads[gtid];
1430 if (this_thr->th.th_bar_arrive_time) {
1431 this_thr->th.th_bar_arrive_time += (__itt_get_timestamp() - cur_time);
1437 (
"__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n",
1438 gtid, taskdata, current_task));
1452 kmp_int32 __kmpc_omp_task_parts(
ident_t *loc_ref, kmp_int32 gtid,
1453 kmp_task_t *new_task) {
1454 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1456 KA_TRACE(10, (
"__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n", gtid,
1457 loc_ref, new_taskdata));
1460 kmp_taskdata_t *parent;
1461 if (UNLIKELY(ompt_enabled.enabled)) {
1462 parent = new_taskdata->td_parent;
1463 if (ompt_enabled.ompt_callback_task_create) {
1464 ompt_data_t task_data = ompt_data_none;
1465 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1466 parent ? &(parent->ompt_task_info.task_data) : &task_data,
1467 parent ? &(parent->ompt_task_info.frame) : NULL,
1468 &(new_taskdata->ompt_task_info.task_data), ompt_task_explicit, 0,
1469 OMPT_GET_RETURN_ADDRESS(0));
1477 if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1479 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1480 new_taskdata->td_flags.task_serial = 1;
1481 __kmp_invoke_task(gtid, new_task, current_task);
1486 (
"__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: " 1487 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
1488 gtid, loc_ref, new_taskdata));
1490 ANNOTATE_HAPPENS_BEFORE(new_task);
1492 if (UNLIKELY(ompt_enabled.enabled)) {
1493 parent->ompt_task_info.frame.enter_frame = NULL;
1496 return TASK_CURRENT_NOT_QUEUED;
1510 kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task,
1511 bool serialize_immediate) {
1512 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1517 if (new_taskdata->td_flags.proxy == TASK_PROXY ||
1518 __kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1520 if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1523 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1524 if (serialize_immediate)
1525 new_taskdata->td_flags.task_serial = 1;
1526 __kmp_invoke_task(gtid, new_task, current_task);
1529 ANNOTATE_HAPPENS_BEFORE(new_task);
1530 return TASK_CURRENT_NOT_QUEUED;
1545 kmp_int32 __kmpc_omp_task(
ident_t *loc_ref, kmp_int32 gtid,
1546 kmp_task_t *new_task) {
1548 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1550 #if KMP_DEBUG || OMPT_SUPPORT 1551 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1553 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1557 kmp_taskdata_t *parent = NULL;
1558 if (UNLIKELY(ompt_enabled.enabled && !new_taskdata->td_flags.started)) {
1559 OMPT_STORE_RETURN_ADDRESS(gtid);
1560 parent = new_taskdata->td_parent;
1561 if (!parent->ompt_task_info.frame.enter_frame)
1562 parent->ompt_task_info.frame.enter_frame = OMPT_GET_FRAME_ADDRESS(1);
1563 if (ompt_enabled.ompt_callback_task_create) {
1564 ompt_data_t task_data = ompt_data_none;
1565 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1566 parent ? &(parent->ompt_task_info.task_data) : &task_data,
1567 parent ? &(parent->ompt_task_info.frame) : NULL,
1568 &(new_taskdata->ompt_task_info.task_data),
1569 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1570 OMPT_LOAD_RETURN_ADDRESS(gtid));
1575 res = __kmp_omp_task(gtid, new_task,
true);
1577 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning " 1578 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1579 gtid, loc_ref, new_taskdata));
1581 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
1582 parent->ompt_task_info.frame.enter_frame = NULL;
1588 template <
bool ompt>
1589 static kmp_int32 __kmpc_omp_taskwait_template(
ident_t *loc_ref, kmp_int32 gtid,
1590 void *frame_address,
1591 void *return_address) {
1592 kmp_taskdata_t *taskdata;
1594 int thread_finished = FALSE;
1595 KMP_SET_THREAD_STATE_BLOCK(TASKWAIT);
1597 KA_TRACE(10, (
"__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref));
1599 if (__kmp_tasking_mode != tskm_immediate_exec) {
1600 thread = __kmp_threads[gtid];
1601 taskdata = thread->th.th_current_task;
1603 #if OMPT_SUPPORT && OMPT_OPTIONAL 1604 ompt_data_t *my_task_data;
1605 ompt_data_t *my_parallel_data;
1608 my_task_data = &(taskdata->ompt_task_info.task_data);
1609 my_parallel_data = OMPT_CUR_TEAM_DATA(thread);
1611 taskdata->ompt_task_info.frame.enter_frame = frame_address;
1613 if (ompt_enabled.ompt_callback_sync_region) {
1614 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
1615 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
1616 my_task_data, return_address);
1619 if (ompt_enabled.ompt_callback_sync_region_wait) {
1620 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
1621 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
1622 my_task_data, return_address);
1625 #endif // OMPT_SUPPORT && OMPT_OPTIONAL 1632 taskdata->td_taskwait_counter += 1;
1633 taskdata->td_taskwait_ident = loc_ref;
1634 taskdata->td_taskwait_thread = gtid + 1;
1637 void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
1638 if (itt_sync_obj != NULL)
1639 __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
1643 !taskdata->td_flags.team_serial && !taskdata->td_flags.final;
1646 must_wait = must_wait || (thread->th.th_task_team != NULL &&
1647 thread->th.th_task_team->tt.tt_found_proxy_tasks);
1651 RCAST(
volatile kmp_uint32 *, &taskdata->td_incomplete_child_tasks),
1653 while (TCR_4(taskdata->td_incomplete_child_tasks) != 0) {
1654 flag.execute_tasks(thread, gtid, FALSE,
1655 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
1656 __kmp_task_stealing_constraint);
1660 if (itt_sync_obj != NULL)
1661 __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
1666 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
1668 #if OMPT_SUPPORT && OMPT_OPTIONAL 1670 if (ompt_enabled.ompt_callback_sync_region_wait) {
1671 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
1672 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
1673 my_task_data, return_address);
1675 if (ompt_enabled.ompt_callback_sync_region) {
1676 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
1677 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
1678 my_task_data, return_address);
1680 taskdata->ompt_task_info.frame.enter_frame = NULL;
1682 #endif // OMPT_SUPPORT && OMPT_OPTIONAL 1684 ANNOTATE_HAPPENS_AFTER(taskdata);
1687 KA_TRACE(10, (
"__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, " 1688 "returning TASK_CURRENT_NOT_QUEUED\n",
1691 return TASK_CURRENT_NOT_QUEUED;
1696 static kmp_int32 __kmpc_omp_taskwait_ompt(
ident_t *loc_ref, kmp_int32 gtid,
1697 void *frame_address,
1698 void *return_address) {
1699 return __kmpc_omp_taskwait_template<true>(loc_ref, gtid, frame_address,
1702 #endif // OMPT_SUPPORT 1706 kmp_int32 __kmpc_omp_taskwait(
ident_t *loc_ref, kmp_int32 gtid) {
1707 #if OMPT_SUPPORT && OMPT_OPTIONAL 1708 if (UNLIKELY(ompt_enabled.enabled)) {
1709 OMPT_STORE_RETURN_ADDRESS(gtid);
1710 return __kmpc_omp_taskwait_ompt(loc_ref, gtid, OMPT_GET_FRAME_ADDRESS(1),
1711 OMPT_LOAD_RETURN_ADDRESS(gtid));
1714 return __kmpc_omp_taskwait_template<false>(loc_ref, gtid, NULL, NULL);
1718 kmp_int32 __kmpc_omp_taskyield(
ident_t *loc_ref, kmp_int32 gtid,
int end_part) {
1719 kmp_taskdata_t *taskdata;
1721 int thread_finished = FALSE;
1724 KMP_SET_THREAD_STATE_BLOCK(TASKYIELD);
1726 KA_TRACE(10, (
"__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n",
1727 gtid, loc_ref, end_part));
1729 if (__kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel) {
1730 thread = __kmp_threads[gtid];
1731 taskdata = thread->th.th_current_task;
1738 taskdata->td_taskwait_counter += 1;
1739 taskdata->td_taskwait_ident = loc_ref;
1740 taskdata->td_taskwait_thread = gtid + 1;
1743 void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
1744 if (itt_sync_obj != NULL)
1745 __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
1747 if (!taskdata->td_flags.team_serial) {
1748 kmp_task_team_t *task_team = thread->th.th_task_team;
1749 if (task_team != NULL) {
1750 if (KMP_TASKING_ENABLED(task_team)) {
1752 if (UNLIKELY(ompt_enabled.enabled))
1753 thread->th.ompt_thread_info.ompt_task_yielded = 1;
1755 __kmp_execute_tasks_32(
1756 thread, gtid, NULL, FALSE,
1757 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
1758 __kmp_task_stealing_constraint);
1760 if (UNLIKELY(ompt_enabled.enabled))
1761 thread->th.ompt_thread_info.ompt_task_yielded = 0;
1767 if (itt_sync_obj != NULL)
1768 __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
1773 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
1776 KA_TRACE(10, (
"__kmpc_omp_taskyield(exit): T#%d task %p resuming, " 1777 "returning TASK_CURRENT_NOT_QUEUED\n",
1780 return TASK_CURRENT_NOT_QUEUED;
1787 typedef struct kmp_task_red_flags {
1788 unsigned lazy_priv : 1;
1789 unsigned reserved31 : 31;
1790 } kmp_task_red_flags_t;
1793 typedef struct kmp_task_red_data {
1801 kmp_task_red_flags_t flags;
1802 } kmp_task_red_data_t;
1805 typedef struct kmp_task_red_input {
1811 kmp_task_red_flags_t flags;
1812 } kmp_task_red_input_t;
1823 void *__kmpc_task_reduction_init(
int gtid,
int num,
void *data) {
1824 kmp_info_t *thread = __kmp_threads[gtid];
1825 kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup;
1826 kmp_int32 nth = thread->th.th_team_nproc;
1827 kmp_task_red_input_t *input = (kmp_task_red_input_t *)data;
1828 kmp_task_red_data_t *arr;
1831 KMP_ASSERT(tg != NULL);
1832 KMP_ASSERT(data != NULL);
1833 KMP_ASSERT(num > 0);
1835 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n",
1839 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, taskgroup %p, #items %d\n",
1841 arr = (kmp_task_red_data_t *)__kmp_thread_malloc(
1842 thread, num *
sizeof(kmp_task_red_data_t));
1843 for (
int i = 0; i < num; ++i) {
1844 void (*f_init)(
void *) = (
void (*)(
void *))(input[i].reduce_init);
1845 size_t size = input[i].reduce_size - 1;
1847 size += CACHE_LINE - size % CACHE_LINE;
1848 KMP_ASSERT(input[i].reduce_comb != NULL);
1849 arr[i].reduce_shar = input[i].reduce_shar;
1850 arr[i].reduce_size = size;
1851 arr[i].reduce_init = input[i].reduce_init;
1852 arr[i].reduce_fini = input[i].reduce_fini;
1853 arr[i].reduce_comb = input[i].reduce_comb;
1854 arr[i].flags = input[i].flags;
1855 if (!input[i].flags.lazy_priv) {
1857 arr[i].reduce_priv = __kmp_allocate(nth * size);
1858 arr[i].reduce_pend = (
char *)(arr[i].reduce_priv) + nth * size;
1859 if (f_init != NULL) {
1861 for (
int j = 0; j < nth; ++j) {
1862 f_init((
char *)(arr[i].reduce_priv) + j * size);
1868 arr[i].reduce_priv = __kmp_allocate(nth *
sizeof(
void *));
1871 tg->reduce_data = (
void *)arr;
1872 tg->reduce_num_data = num;
1885 void *__kmpc_task_reduction_get_th_data(
int gtid,
void *tskgrp,
void *data) {
1886 kmp_info_t *thread = __kmp_threads[gtid];
1887 kmp_int32 nth = thread->th.th_team_nproc;
1891 kmp_taskgroup_t *tg = (kmp_taskgroup_t *)tskgrp;
1893 tg = thread->th.th_current_task->td_taskgroup;
1894 KMP_ASSERT(tg != NULL);
1895 kmp_task_red_data_t *arr = (kmp_task_red_data_t *)(tg->reduce_data);
1896 kmp_int32 num = tg->reduce_num_data;
1897 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
1899 KMP_ASSERT(data != NULL);
1900 while (tg != NULL) {
1901 for (
int i = 0; i < num; ++i) {
1902 if (!arr[i].flags.lazy_priv) {
1903 if (data == arr[i].reduce_shar ||
1904 (data >= arr[i].reduce_priv && data < arr[i].reduce_pend))
1905 return (
char *)(arr[i].reduce_priv) + tid * arr[i].reduce_size;
1908 void **p_priv = (
void **)(arr[i].reduce_priv);
1909 if (data == arr[i].reduce_shar)
1912 for (
int j = 0; j < nth; ++j)
1913 if (data == p_priv[j])
1917 if (p_priv[tid] == NULL) {
1919 void (*f_init)(
void *) = (
void (*)(
void *))(arr[i].reduce_init);
1920 p_priv[tid] = __kmp_allocate(arr[i].reduce_size);
1921 if (f_init != NULL) {
1922 f_init(p_priv[tid]);
1929 arr = (kmp_task_red_data_t *)(tg->reduce_data);
1930 num = tg->reduce_num_data;
1932 KMP_ASSERT2(0,
"Unknown task reduction item");
1938 static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg) {
1939 kmp_int32 nth = th->th.th_team_nproc;
1940 KMP_DEBUG_ASSERT(nth > 1);
1941 kmp_task_red_data_t *arr = (kmp_task_red_data_t *)tg->reduce_data;
1942 kmp_int32 num = tg->reduce_num_data;
1943 for (
int i = 0; i < num; ++i) {
1944 void *sh_data = arr[i].reduce_shar;
1945 void (*f_fini)(
void *) = (
void (*)(
void *))(arr[i].reduce_fini);
1946 void (*f_comb)(
void *,
void *) =
1947 (
void (*)(
void *,
void *))(arr[i].reduce_comb);
1948 if (!arr[i].flags.lazy_priv) {
1949 void *pr_data = arr[i].reduce_priv;
1950 size_t size = arr[i].reduce_size;
1951 for (
int j = 0; j < nth; ++j) {
1952 void *priv_data = (
char *)pr_data + j * size;
1953 f_comb(sh_data, priv_data);
1958 void **pr_data = (
void **)(arr[i].reduce_priv);
1959 for (
int j = 0; j < nth; ++j) {
1960 if (pr_data[j] != NULL) {
1961 f_comb(sh_data, pr_data[j]);
1964 __kmp_free(pr_data[j]);
1968 __kmp_free(arr[i].reduce_priv);
1970 __kmp_thread_free(th, arr);
1971 tg->reduce_data = NULL;
1972 tg->reduce_num_data = 0;
1978 void __kmpc_taskgroup(
ident_t *loc,
int gtid) {
1979 kmp_info_t *thread = __kmp_threads[gtid];
1980 kmp_taskdata_t *taskdata = thread->th.th_current_task;
1981 kmp_taskgroup_t *tg_new =
1982 (kmp_taskgroup_t *)__kmp_thread_malloc(thread,
sizeof(kmp_taskgroup_t));
1983 KA_TRACE(10, (
"__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new));
1985 tg_new->cancel_request = cancel_noreq;
1986 tg_new->parent = taskdata->td_taskgroup;
1989 tg_new->reduce_data = NULL;
1990 tg_new->reduce_num_data = 0;
1992 taskdata->td_taskgroup = tg_new;
1994 #if OMPT_SUPPORT && OMPT_OPTIONAL 1995 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
1996 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
1998 codeptr = OMPT_GET_RETURN_ADDRESS(0);
1999 kmp_team_t *team = thread->th.th_team;
2000 ompt_data_t my_task_data = taskdata->ompt_task_info.task_data;
2002 ompt_data_t my_parallel_data = team->t.ompt_team_info.parallel_data;
2004 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2005 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2006 &(my_task_data), codeptr);
2013 void __kmpc_end_taskgroup(
ident_t *loc,
int gtid) {
2014 kmp_info_t *thread = __kmp_threads[gtid];
2015 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2016 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
2017 int thread_finished = FALSE;
2019 #if OMPT_SUPPORT && OMPT_OPTIONAL 2021 ompt_data_t my_task_data;
2022 ompt_data_t my_parallel_data;
2024 if (UNLIKELY(ompt_enabled.enabled)) {
2025 team = thread->th.th_team;
2026 my_task_data = taskdata->ompt_task_info.task_data;
2028 my_parallel_data = team->t.ompt_team_info.parallel_data;
2029 codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2031 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2035 KA_TRACE(10, (
"__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc));
2036 KMP_DEBUG_ASSERT(taskgroup != NULL);
2037 KMP_SET_THREAD_STATE_BLOCK(TASKGROUP);
2039 if (__kmp_tasking_mode != tskm_immediate_exec) {
2041 taskdata->td_taskwait_counter += 1;
2042 taskdata->td_taskwait_ident = loc;
2043 taskdata->td_taskwait_thread = gtid + 1;
2047 void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
2048 if (itt_sync_obj != NULL)
2049 __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
2052 #if OMPT_SUPPORT && OMPT_OPTIONAL 2053 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2054 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2055 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2056 &(my_task_data), codeptr);
2061 if (!taskdata->td_flags.team_serial ||
2062 (thread->th.th_task_team != NULL &&
2063 thread->th.th_task_team->tt.tt_found_proxy_tasks))
2065 if (!taskdata->td_flags.team_serial)
2068 kmp_flag_32 flag(RCAST(kmp_uint32 *, &taskgroup->count), 0U);
2069 while (TCR_4(taskgroup->count) != 0) {
2070 flag.execute_tasks(thread, gtid, FALSE,
2071 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2072 __kmp_task_stealing_constraint);
2075 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2077 #if OMPT_SUPPORT && OMPT_OPTIONAL 2078 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2079 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2080 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2081 &(my_task_data), codeptr);
2086 if (itt_sync_obj != NULL)
2087 __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
2090 KMP_DEBUG_ASSERT(taskgroup->count == 0);
2094 if (taskgroup->reduce_data != NULL)
2095 __kmp_task_reduction_fini(thread, taskgroup);
2098 taskdata->td_taskgroup = taskgroup->parent;
2099 __kmp_thread_free(thread, taskgroup);
2101 KA_TRACE(10, (
"__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n",
2103 ANNOTATE_HAPPENS_AFTER(taskdata);
2105 #if OMPT_SUPPORT && OMPT_OPTIONAL 2106 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2107 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2108 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2109 &(my_task_data), codeptr);
2116 static kmp_task_t *__kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid,
2117 kmp_task_team_t *task_team,
2118 kmp_int32 is_constrained) {
2120 kmp_taskdata_t *taskdata;
2121 kmp_thread_data_t *thread_data;
2124 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2125 KMP_DEBUG_ASSERT(task_team->tt.tt_threads_data !=
2128 thread_data = &task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
2130 KA_TRACE(10, (
"__kmp_remove_my_task(enter): T#%d ntasks=%d head=%u tail=%u\n",
2131 gtid, thread_data->td.td_deque_ntasks,
2132 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2134 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2136 (
"__kmp_remove_my_task(exit #1): T#%d No tasks to remove: " 2137 "ntasks=%d head=%u tail=%u\n",
2138 gtid, thread_data->td.td_deque_ntasks,
2139 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2143 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
2145 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2146 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2148 (
"__kmp_remove_my_task(exit #2): T#%d No tasks to remove: " 2149 "ntasks=%d head=%u tail=%u\n",
2150 gtid, thread_data->td.td_deque_ntasks,
2151 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2155 tail = (thread_data->td.td_deque_tail - 1) &
2156 TASK_DEQUE_MASK(thread_data->td);
2157 taskdata = thread_data->td.td_deque[tail];
2159 if (is_constrained && (taskdata->td_flags.tiedness == TASK_TIED)) {
2163 kmp_taskdata_t *current = thread->th.th_current_task->td_last_tied;
2164 KMP_DEBUG_ASSERT(current != NULL);
2166 if (current->td_flags.tasktype == TASK_EXPLICIT ||
2167 current->td_taskwait_thread > 0) {
2168 kmp_int32 level = current->td_level;
2169 kmp_taskdata_t *parent = taskdata->td_parent;
2170 while (parent != current && parent->td_level > level) {
2171 parent = parent->td_parent;
2173 KMP_DEBUG_ASSERT(parent != NULL);
2175 if (parent != current) {
2177 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2178 KA_TRACE(10, (
"__kmp_remove_my_task(exit #2): T#%d No tasks to remove: " 2179 "ntasks=%d head=%u tail=%u\n",
2180 gtid, thread_data->td.td_deque_ntasks,
2181 thread_data->td.td_deque_head,
2182 thread_data->td.td_deque_tail));
2188 thread_data->td.td_deque_tail = tail;
2189 TCW_4(thread_data->td.td_deque_ntasks, thread_data->td.td_deque_ntasks - 1);
2191 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2193 KA_TRACE(10, (
"__kmp_remove_my_task(exit #2): T#%d task %p removed: " 2194 "ntasks=%d head=%u tail=%u\n",
2195 gtid, taskdata, thread_data->td.td_deque_ntasks,
2196 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2198 task = KMP_TASKDATA_TO_TASK(taskdata);
2205 static kmp_task_t *__kmp_steal_task(kmp_info_t *victim_thr, kmp_int32 gtid,
2206 kmp_task_team_t *task_team,
2207 volatile kmp_int32 *unfinished_threads,
2208 int *thread_finished,
2209 kmp_int32 is_constrained) {
2211 kmp_taskdata_t *taskdata;
2212 kmp_taskdata_t *current;
2213 kmp_thread_data_t *victim_td, *threads_data;
2214 kmp_int32 level, target;
2215 kmp_int32 victim_tid;
2217 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2219 threads_data = task_team->tt.tt_threads_data;
2220 KMP_DEBUG_ASSERT(threads_data != NULL);
2222 victim_tid = victim_thr->th.th_info.ds.ds_tid;
2223 victim_td = &threads_data[victim_tid];
2225 KA_TRACE(10, (
"__kmp_steal_task(enter): T#%d try to steal from T#%d: " 2226 "task_team=%p ntasks=%d head=%u tail=%u\n",
2227 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
2228 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
2229 victim_td->td.td_deque_tail));
2231 if (TCR_4(victim_td->td.td_deque_ntasks) == 0) {
2232 KA_TRACE(10, (
"__kmp_steal_task(exit #1): T#%d could not steal from T#%d: " 2233 "task_team=%p ntasks=%d head=%u tail=%u\n",
2234 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
2235 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
2236 victim_td->td.td_deque_tail));
2240 __kmp_acquire_bootstrap_lock(&victim_td->td.td_deque_lock);
2242 int ntasks = TCR_4(victim_td->td.td_deque_ntasks);
2245 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2246 KA_TRACE(10, (
"__kmp_steal_task(exit #2): T#%d could not steal from T#%d: " 2247 "task_team=%p ntasks=%d head=%u tail=%u\n",
2248 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2249 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2253 KMP_DEBUG_ASSERT(victim_td->td.td_deque != NULL);
2255 taskdata = victim_td->td.td_deque[victim_td->td.td_deque_head];
2256 if (is_constrained && (taskdata->td_flags.tiedness == TASK_TIED)) {
2260 current = __kmp_threads[gtid]->th.th_current_task->td_last_tied;
2261 KMP_DEBUG_ASSERT(current != NULL);
2263 if (current->td_flags.tasktype == TASK_EXPLICIT ||
2264 current->td_taskwait_thread > 0) {
2265 level = current->td_level;
2266 kmp_taskdata_t *parent = taskdata->td_parent;
2267 while (parent != current && parent->td_level > level) {
2268 parent = parent->td_parent;
2270 KMP_DEBUG_ASSERT(parent != NULL);
2272 if (parent != current) {
2273 if (!task_team->tt.tt_untied_task_encountered) {
2275 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2277 (
"__kmp_steal_task(exit #3): T#%d could not steal from " 2278 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
2279 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2280 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2287 if (taskdata != NULL) {
2289 victim_td->td.td_deque_head =
2290 (victim_td->td.td_deque_head + 1) & TASK_DEQUE_MASK(victim_td->td);
2294 target = victim_td->td.td_deque_head;
2295 for (i = 1; i < ntasks; ++i) {
2296 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
2297 taskdata = victim_td->td.td_deque[target];
2298 if (taskdata->td_flags.tiedness == TASK_TIED) {
2300 kmp_taskdata_t *parent = taskdata->td_parent;
2302 while (parent != current && parent->td_level > level) {
2303 parent = parent->td_parent;
2304 KMP_DEBUG_ASSERT(parent != NULL);
2306 if (parent != current) {
2319 if (taskdata == NULL) {
2321 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2322 KA_TRACE(10, (
"__kmp_steal_task(exit #4): T#%d could not steal from " 2323 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
2324 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2325 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2329 for (i = i + 1; i < ntasks; ++i) {
2331 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
2332 victim_td->td.td_deque[prev] = victim_td->td.td_deque[target];
2335 KMP_DEBUG_ASSERT(victim_td->td.td_deque_tail ==
2336 ((target + 1) & TASK_DEQUE_MASK(victim_td->td)));
2337 victim_td->td.td_deque_tail = target;
2339 if (*thread_finished) {
2345 count = KMP_TEST_THEN_INC32(unfinished_threads);
2349 (
"__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n",
2350 gtid, count + 1, task_team));
2352 *thread_finished = FALSE;
2354 TCW_4(victim_td->td.td_deque_ntasks, ntasks - 1);
2356 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2360 (
"__kmp_steal_task(exit #5): T#%d stole task %p from T#%d: " 2361 "task_team=%p ntasks=%d head=%u tail=%u\n",
2362 gtid, taskdata, __kmp_gtid_from_thread(victim_thr), task_team,
2363 ntasks, victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2365 task = KMP_TASKDATA_TO_TASK(taskdata);
2379 static inline int __kmp_execute_tasks_template(
2380 kmp_info_t *thread, kmp_int32 gtid, C *flag,
int final_spin,
2381 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
2382 kmp_int32 is_constrained) {
2383 kmp_task_team_t *task_team = thread->th.th_task_team;
2384 kmp_thread_data_t *threads_data;
2386 kmp_info_t *other_thread;
2387 kmp_taskdata_t *current_task = thread->th.th_current_task;
2388 volatile kmp_int32 *unfinished_threads;
2389 kmp_int32 nthreads, victim_tid = -2, use_own_tasks = 1, new_victim = 0,
2390 tid = thread->th.th_info.ds.ds_tid;
2392 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2393 KMP_DEBUG_ASSERT(thread == __kmp_threads[gtid]);
2395 if (task_team == NULL)
2398 KA_TRACE(15, (
"__kmp_execute_tasks_template(enter): T#%d final_spin=%d " 2399 "*thread_finished=%d\n",
2400 gtid, final_spin, *thread_finished));
2402 thread->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
2403 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
2404 KMP_DEBUG_ASSERT(threads_data != NULL);
2406 nthreads = task_team->tt.tt_nproc;
2407 unfinished_threads = &(task_team->tt.tt_unfinished_threads);
2409 KMP_DEBUG_ASSERT(nthreads > 1 || task_team->tt.tt_found_proxy_tasks);
2411 KMP_DEBUG_ASSERT(nthreads > 1);
2413 KMP_DEBUG_ASSERT(TCR_4(*unfinished_threads) >= 0);
2419 if (use_own_tasks) {
2420 task = __kmp_remove_my_task(thread, gtid, task_team, is_constrained);
2422 if ((task == NULL) && (nthreads > 1)) {
2426 if (victim_tid == -2) {
2427 victim_tid = threads_data[tid].td.td_deque_last_stolen;
2430 other_thread = threads_data[victim_tid].td.td_thr;
2432 if (victim_tid != -1) {
2434 }
else if (!new_victim) {
2440 victim_tid = __kmp_get_random(thread) % (nthreads - 1);
2441 if (victim_tid >= tid) {
2445 other_thread = threads_data[victim_tid].td.td_thr;
2455 if ((__kmp_tasking_mode == tskm_task_teams) &&
2456 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) &&
2457 (TCR_PTR(CCAST(
void *, other_thread->th.th_sleep_loc)) !=
2460 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(other_thread),
2461 other_thread->th.th_sleep_loc);
2474 task = __kmp_steal_task(other_thread, gtid, task_team,
2475 unfinished_threads, thread_finished,
2479 if (threads_data[tid].td.td_deque_last_stolen != victim_tid) {
2480 threads_data[tid].td.td_deque_last_stolen = victim_tid;
2487 KMP_CHECK_UPDATE(threads_data[tid].td.td_deque_last_stolen, -1);
2496 #if USE_ITT_BUILD && USE_ITT_NOTIFY 2497 if (__itt_sync_create_ptr || KMP_ITT_DEBUG) {
2498 if (itt_sync_obj == NULL) {
2500 itt_sync_obj = __kmp_itt_barrier_object(gtid, bs_forkjoin_barrier);
2502 __kmp_itt_task_starting(itt_sync_obj);
2505 __kmp_invoke_task(gtid, task, current_task);
2507 if (itt_sync_obj != NULL)
2508 __kmp_itt_task_finished(itt_sync_obj);
2515 if (flag == NULL || (!final_spin && flag->done_check())) {
2518 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
2522 if (thread->th.th_task_team == NULL) {
2526 KMP_YIELD(__kmp_library == library_throughput);
2529 if (!use_own_tasks && TCR_4(threads_data[tid].td.td_deque_ntasks) != 0) {
2530 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d stolen task spawned " 2531 "other tasks, restart\n",
2543 if (final_spin && TCR_4(current_task->td_incomplete_child_tasks) == 0)
2551 if (!*thread_finished) {
2554 count = KMP_TEST_THEN_DEC32(unfinished_threads) - 1;
2555 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d dec " 2556 "unfinished_threads to %d task_team=%p\n",
2557 gtid, count, task_team));
2558 *thread_finished = TRUE;
2566 if (flag != NULL && flag->done_check()) {
2569 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
2577 if (thread->th.th_task_team == NULL) {
2579 (
"__kmp_execute_tasks_template: T#%d no more tasks\n", gtid));
2592 (
"__kmp_execute_tasks_template: T#%d can't find work\n", gtid));
2598 int __kmp_execute_tasks_32(
2599 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32 *flag,
int final_spin,
2600 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
2601 kmp_int32 is_constrained) {
2602 return __kmp_execute_tasks_template(
2603 thread, gtid, flag, final_spin,
2604 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
2607 int __kmp_execute_tasks_64(
2608 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64 *flag,
int final_spin,
2609 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
2610 kmp_int32 is_constrained) {
2611 return __kmp_execute_tasks_template(
2612 thread, gtid, flag, final_spin,
2613 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
2616 int __kmp_execute_tasks_oncore(
2617 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag,
int final_spin,
2618 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
2619 kmp_int32 is_constrained) {
2620 return __kmp_execute_tasks_template(
2621 thread, gtid, flag, final_spin,
2622 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
2628 static void __kmp_enable_tasking(kmp_task_team_t *task_team,
2629 kmp_info_t *this_thr) {
2630 kmp_thread_data_t *threads_data;
2631 int nthreads, i, is_init_thread;
2633 KA_TRACE(10, (
"__kmp_enable_tasking(enter): T#%d\n",
2634 __kmp_gtid_from_thread(this_thr)));
2636 KMP_DEBUG_ASSERT(task_team != NULL);
2637 KMP_DEBUG_ASSERT(this_thr->th.th_team != NULL);
2639 nthreads = task_team->tt.tt_nproc;
2640 KMP_DEBUG_ASSERT(nthreads > 0);
2641 KMP_DEBUG_ASSERT(nthreads == this_thr->th.th_team->t.t_nproc);
2644 is_init_thread = __kmp_realloc_task_threads_data(this_thr, task_team);
2646 if (!is_init_thread) {
2650 (
"__kmp_enable_tasking(exit): T#%d: threads array already set up.\n",
2651 __kmp_gtid_from_thread(this_thr)));
2654 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
2655 KMP_DEBUG_ASSERT(threads_data != NULL);
2657 if ((__kmp_tasking_mode == tskm_task_teams) &&
2658 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME)) {
2662 for (i = 0; i < nthreads; i++) {
2663 volatile void *sleep_loc;
2664 kmp_info_t *thread = threads_data[i].td.td_thr;
2666 if (i == this_thr->th.th_info.ds.ds_tid) {
2675 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
2677 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d waking up thread T#%d\n",
2678 __kmp_gtid_from_thread(this_thr),
2679 __kmp_gtid_from_thread(thread)));
2680 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc);
2682 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d don't wake up thread T#%d\n",
2683 __kmp_gtid_from_thread(this_thr),
2684 __kmp_gtid_from_thread(thread)));
2689 KA_TRACE(10, (
"__kmp_enable_tasking(exit): T#%d\n",
2690 __kmp_gtid_from_thread(this_thr)));
2727 static kmp_task_team_t *__kmp_free_task_teams =
2730 static kmp_bootstrap_lock_t __kmp_task_team_lock =
2731 KMP_BOOTSTRAP_LOCK_INITIALIZER(__kmp_task_team_lock);
2738 static void __kmp_alloc_task_deque(kmp_info_t *thread,
2739 kmp_thread_data_t *thread_data) {
2740 __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
2741 KMP_DEBUG_ASSERT(thread_data->td.td_deque == NULL);
2744 thread_data->td.td_deque_last_stolen = -1;
2746 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == 0);
2747 KMP_DEBUG_ASSERT(thread_data->td.td_deque_head == 0);
2748 KMP_DEBUG_ASSERT(thread_data->td.td_deque_tail == 0);
2752 (
"__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n",
2753 __kmp_gtid_from_thread(thread), INITIAL_TASK_DEQUE_SIZE, thread_data));
2757 thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
2758 INITIAL_TASK_DEQUE_SIZE *
sizeof(kmp_taskdata_t *));
2759 thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
2766 static void __kmp_realloc_task_deque(kmp_info_t *thread,
2767 kmp_thread_data_t *thread_data) {
2768 kmp_int32 size = TASK_DEQUE_SIZE(thread_data->td);
2769 kmp_int32 new_size = 2 * size;
2771 KE_TRACE(10, (
"__kmp_realloc_task_deque: T#%d reallocating deque[from %d to " 2772 "%d] for thread_data %p\n",
2773 __kmp_gtid_from_thread(thread), size, new_size, thread_data));
2775 kmp_taskdata_t **new_deque =
2776 (kmp_taskdata_t **)__kmp_allocate(new_size *
sizeof(kmp_taskdata_t *));
2779 for (i = thread_data->td.td_deque_head, j = 0; j < size;
2780 i = (i + 1) & TASK_DEQUE_MASK(thread_data->td), j++)
2781 new_deque[j] = thread_data->td.td_deque[i];
2783 __kmp_free(thread_data->td.td_deque);
2785 thread_data->td.td_deque_head = 0;
2786 thread_data->td.td_deque_tail = size;
2787 thread_data->td.td_deque = new_deque;
2788 thread_data->td.td_deque_size = new_size;
2794 static void __kmp_free_task_deque(kmp_thread_data_t *thread_data) {
2795 if (thread_data->td.td_deque != NULL) {
2796 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
2797 TCW_4(thread_data->td.td_deque_ntasks, 0);
2798 __kmp_free(thread_data->td.td_deque);
2799 thread_data->td.td_deque = NULL;
2800 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2803 #ifdef BUILD_TIED_TASK_STACK 2805 if (thread_data->td.td_susp_tied_tasks.ts_entries != TASK_STACK_EMPTY) {
2806 __kmp_free_task_stack(__kmp_thread_from_gtid(gtid), thread_data);
2808 #endif // BUILD_TIED_TASK_STACK 2818 static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
2819 kmp_task_team_t *task_team) {
2820 kmp_thread_data_t **threads_data_p;
2821 kmp_int32 nthreads, maxthreads;
2822 int is_init_thread = FALSE;
2824 if (TCR_4(task_team->tt.tt_found_tasks)) {
2829 threads_data_p = &task_team->tt.tt_threads_data;
2830 nthreads = task_team->tt.tt_nproc;
2831 maxthreads = task_team->tt.tt_max_threads;
2836 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
2838 if (!TCR_4(task_team->tt.tt_found_tasks)) {
2840 kmp_team_t *team = thread->th.th_team;
2843 is_init_thread = TRUE;
2844 if (maxthreads < nthreads) {
2846 if (*threads_data_p != NULL) {
2847 kmp_thread_data_t *old_data = *threads_data_p;
2848 kmp_thread_data_t *new_data = NULL;
2852 (
"__kmp_realloc_task_threads_data: T#%d reallocating " 2853 "threads data for task_team %p, new_size = %d, old_size = %d\n",
2854 __kmp_gtid_from_thread(thread), task_team, nthreads, maxthreads));
2859 new_data = (kmp_thread_data_t *)__kmp_allocate(
2860 nthreads *
sizeof(kmp_thread_data_t));
2862 KMP_MEMCPY_S((
void *)new_data, nthreads *
sizeof(kmp_thread_data_t),
2863 (
void *)old_data, maxthreads *
sizeof(kmp_thread_data_t));
2865 #ifdef BUILD_TIED_TASK_STACK 2867 for (i = maxthreads; i < nthreads; i++) {
2868 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
2869 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
2871 #endif // BUILD_TIED_TASK_STACK 2873 (*threads_data_p) = new_data;
2874 __kmp_free(old_data);
2876 KE_TRACE(10, (
"__kmp_realloc_task_threads_data: T#%d allocating " 2877 "threads data for task_team %p, size = %d\n",
2878 __kmp_gtid_from_thread(thread), task_team, nthreads));
2882 ANNOTATE_IGNORE_WRITES_BEGIN();
2883 *threads_data_p = (kmp_thread_data_t *)__kmp_allocate(
2884 nthreads *
sizeof(kmp_thread_data_t));
2885 ANNOTATE_IGNORE_WRITES_END();
2886 #ifdef BUILD_TIED_TASK_STACK 2888 for (i = 0; i < nthreads; i++) {
2889 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
2890 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
2892 #endif // BUILD_TIED_TASK_STACK 2894 task_team->tt.tt_max_threads = nthreads;
2897 KMP_DEBUG_ASSERT(*threads_data_p != NULL);
2901 for (i = 0; i < nthreads; i++) {
2902 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
2903 thread_data->td.td_thr = team->t.t_threads[i];
2905 if (thread_data->td.td_deque_last_stolen >= nthreads) {
2909 thread_data->td.td_deque_last_stolen = -1;
2914 TCW_SYNC_4(task_team->tt.tt_found_tasks, TRUE);
2917 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
2918 return is_init_thread;
2924 static void __kmp_free_task_threads_data(kmp_task_team_t *task_team) {
2925 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
2926 if (task_team->tt.tt_threads_data != NULL) {
2928 for (i = 0; i < task_team->tt.tt_max_threads; i++) {
2929 __kmp_free_task_deque(&task_team->tt.tt_threads_data[i]);
2931 __kmp_free(task_team->tt.tt_threads_data);
2932 task_team->tt.tt_threads_data = NULL;
2934 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
2941 static kmp_task_team_t *__kmp_allocate_task_team(kmp_info_t *thread,
2943 kmp_task_team_t *task_team = NULL;
2946 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d entering; team = %p\n",
2947 (thread ? __kmp_gtid_from_thread(thread) : -1), team));
2949 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
2951 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
2952 if (__kmp_free_task_teams != NULL) {
2953 task_team = __kmp_free_task_teams;
2954 TCW_PTR(__kmp_free_task_teams, task_team->tt.tt_next);
2955 task_team->tt.tt_next = NULL;
2957 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
2960 if (task_team == NULL) {
2961 KE_TRACE(10, (
"__kmp_allocate_task_team: T#%d allocating " 2962 "task team for team %p\n",
2963 __kmp_gtid_from_thread(thread), team));
2967 task_team = (kmp_task_team_t *)__kmp_allocate(
sizeof(kmp_task_team_t));
2968 __kmp_init_bootstrap_lock(&task_team->tt.tt_threads_lock);
2975 TCW_4(task_team->tt.tt_found_tasks, FALSE);
2977 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
2979 task_team->tt.tt_nproc = nthreads = team->t.t_nproc;
2981 TCW_4(task_team->tt.tt_unfinished_threads, nthreads);
2982 TCW_4(task_team->tt.tt_active, TRUE);
2984 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d exiting; task_team = %p " 2985 "unfinished_threads init'd to %d\n",
2986 (thread ? __kmp_gtid_from_thread(thread) : -1), task_team,
2987 task_team->tt.tt_unfinished_threads));
2994 void __kmp_free_task_team(kmp_info_t *thread, kmp_task_team_t *task_team) {
2995 KA_TRACE(20, (
"__kmp_free_task_team: T#%d task_team = %p\n",
2996 thread ? __kmp_gtid_from_thread(thread) : -1, task_team));
2999 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3001 KMP_DEBUG_ASSERT(task_team->tt.tt_next == NULL);
3002 task_team->tt.tt_next = __kmp_free_task_teams;
3003 TCW_PTR(__kmp_free_task_teams, task_team);
3005 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3013 void __kmp_reap_task_teams(
void) {
3014 kmp_task_team_t *task_team;
3016 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3018 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3019 while ((task_team = __kmp_free_task_teams) != NULL) {
3020 __kmp_free_task_teams = task_team->tt.tt_next;
3021 task_team->tt.tt_next = NULL;
3024 if (task_team->tt.tt_threads_data != NULL) {
3025 __kmp_free_task_threads_data(task_team);
3027 __kmp_free(task_team);
3029 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3036 void __kmp_wait_to_unref_task_teams(
void) {
3041 KMP_INIT_YIELD(spins);
3049 for (thread = CCAST(kmp_info_t *, __kmp_thread_pool); thread != NULL;
3050 thread = thread->th.th_next_pool) {
3054 if (TCR_PTR(thread->th.th_task_team) == NULL) {
3055 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n",
3056 __kmp_gtid_from_thread(thread)));
3061 if (!__kmp_is_thread_alive(thread, &exit_val)) {
3062 thread->th.th_task_team = NULL;
3069 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: Waiting for T#%d to " 3070 "unreference task_team\n",
3071 __kmp_gtid_from_thread(thread)));
3073 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
3074 volatile void *sleep_loc;
3076 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3080 (
"__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n",
3081 __kmp_gtid_from_thread(thread), __kmp_gtid_from_thread(thread)));
3082 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc);
3092 KMP_YIELD(TCR_4(__kmp_nth) > __kmp_avail_proc);
3093 KMP_YIELD_SPIN(spins);
3099 void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team,
int always) {
3100 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3106 if (team->t.t_task_team[this_thr->th.th_task_state] == NULL &&
3107 (always || team->t.t_nproc > 1)) {
3108 team->t.t_task_team[this_thr->th.th_task_state] =
3109 __kmp_allocate_task_team(this_thr, team);
3110 KA_TRACE(20, (
"__kmp_task_team_setup: Master T#%d created new task_team %p " 3111 "for team %d at parity=%d\n",
3112 __kmp_gtid_from_thread(this_thr),
3113 team->t.t_task_team[this_thr->th.th_task_state],
3114 ((team != NULL) ? team->t.t_id : -1),
3115 this_thr->th.th_task_state));
3125 if (team->t.t_nproc > 1) {
3126 int other_team = 1 - this_thr->th.th_task_state;
3127 if (team->t.t_task_team[other_team] == NULL) {
3128 team->t.t_task_team[other_team] =
3129 __kmp_allocate_task_team(this_thr, team);
3130 KA_TRACE(20, (
"__kmp_task_team_setup: Master T#%d created second new " 3131 "task_team %p for team %d at parity=%d\n",
3132 __kmp_gtid_from_thread(this_thr),
3133 team->t.t_task_team[other_team],
3134 ((team != NULL) ? team->t.t_id : -1), other_team));
3137 kmp_task_team_t *task_team = team->t.t_task_team[other_team];
3138 if (!task_team->tt.tt_active ||
3139 team->t.t_nproc != task_team->tt.tt_nproc) {
3140 TCW_4(task_team->tt.tt_nproc, team->t.t_nproc);
3141 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3143 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3145 TCW_4(task_team->tt.tt_unfinished_threads, team->t.t_nproc);
3146 TCW_4(task_team->tt.tt_active, TRUE);
3150 KA_TRACE(20, (
"__kmp_task_team_setup: Master T#%d reset next task_team " 3151 "%p for team %d at parity=%d\n",
3152 __kmp_gtid_from_thread(this_thr),
3153 team->t.t_task_team[other_team],
3154 ((team != NULL) ? team->t.t_id : -1), other_team));
3162 void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team) {
3163 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3167 this_thr->th.th_task_state = 1 - this_thr->th.th_task_state;
3170 TCW_PTR(this_thr->th.th_task_team,
3171 team->t.t_task_team[this_thr->th.th_task_state]);
3173 (
"__kmp_task_team_sync: Thread T#%d task team switched to task_team " 3174 "%p from Team #%d (parity=%d)\n",
3175 __kmp_gtid_from_thread(this_thr), this_thr->th.th_task_team,
3176 ((team != NULL) ? team->t.t_id : -1), this_thr->th.th_task_state));
3186 void __kmp_task_team_wait(
3187 kmp_info_t *this_thr,
3188 kmp_team_t *team USE_ITT_BUILD_ARG(
void *itt_sync_obj),
int wait) {
3189 kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state];
3191 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3192 KMP_DEBUG_ASSERT(task_team == this_thr->th.th_task_team);
3194 if ((task_team != NULL) && KMP_TASKING_ENABLED(task_team)) {
3196 KA_TRACE(20, (
"__kmp_task_team_wait: Master T#%d waiting for all tasks " 3197 "(for unfinished_threads to reach 0) on task_team = %p\n",
3198 __kmp_gtid_from_thread(this_thr), task_team));
3203 RCAST(
volatile kmp_uint32 *, &task_team->tt.tt_unfinished_threads),
3205 flag.wait(this_thr, TRUE USE_ITT_BUILD_ARG(itt_sync_obj));
3211 (
"__kmp_task_team_wait: Master T#%d deactivating task_team %p: " 3212 "setting active to false, setting local and team's pointer to NULL\n",
3213 __kmp_gtid_from_thread(this_thr), task_team));
3215 KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1 ||
3216 task_team->tt.tt_found_proxy_tasks == TRUE);
3217 TCW_SYNC_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3219 KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1);
3221 KMP_CHECK_UPDATE(task_team->tt.tt_untied_task_encountered, 0);
3222 TCW_SYNC_4(task_team->tt.tt_active, FALSE);
3225 TCW_PTR(this_thr->th.th_task_team, NULL);
3234 void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread,
int gtid) {
3235 volatile kmp_uint32 *spin = RCAST(
3236 volatile kmp_uint32 *,
3237 &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads);
3239 KMP_DEBUG_ASSERT(__kmp_tasking_mode == tskm_extra_barrier);
3242 KMP_FSYNC_SPIN_INIT(spin, (kmp_uint32 *)NULL);
3244 kmp_flag_32 spin_flag(spin, 0U);
3245 while (!spin_flag.execute_tasks(thread, gtid, TRUE,
3246 &flag USE_ITT_BUILD_ARG(NULL), 0)) {
3249 KMP_FSYNC_SPIN_PREPARE(CCAST(kmp_uint32 *, spin));
3252 if (TCR_4(__kmp_global.g.g_done)) {
3253 if (__kmp_global.g.g_abort)
3254 __kmp_abort_thread();
3260 KMP_FSYNC_SPIN_ACQUIRED(CCAST(kmp_uint32 *, spin));
3271 static bool __kmp_give_task(kmp_info_t *thread, kmp_int32 tid, kmp_task_t *task,
3273 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
3274 kmp_task_team_t *task_team = taskdata->td_task_team;
3276 KA_TRACE(20, (
"__kmp_give_task: trying to give task %p to thread %d.\n",
3280 KMP_DEBUG_ASSERT(task_team != NULL);
3282 bool result =
false;
3283 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
3285 if (thread_data->td.td_deque == NULL) {
3289 (
"__kmp_give_task: thread %d has no queue while giving task %p.\n",
3294 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3295 TASK_DEQUE_SIZE(thread_data->td)) {
3298 (
"__kmp_give_task: queue is full while giving task %p to thread %d.\n",
3303 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
3306 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3307 __kmp_realloc_task_deque(thread, thread_data);
3311 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3313 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3314 TASK_DEQUE_SIZE(thread_data->td)) {
3315 KA_TRACE(30, (
"__kmp_give_task: queue is full while giving task %p to " 3321 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
3322 goto release_and_exit;
3324 __kmp_realloc_task_deque(thread, thread_data);
3330 thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
3332 thread_data->td.td_deque_tail =
3333 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
3334 TCW_4(thread_data->td.td_deque_ntasks,
3335 TCR_4(thread_data->td.td_deque_ntasks) + 1);
3338 KA_TRACE(30, (
"__kmp_give_task: successfully gave task %p to thread %d.\n",
3342 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3363 static void __kmp_first_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
3364 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
3365 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3366 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
3367 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
3369 taskdata->td_flags.complete = 1;
3371 if (taskdata->td_taskgroup)
3372 KMP_TEST_THEN_DEC32(&taskdata->td_taskgroup->count);
3376 TCI_4(taskdata->td_incomplete_child_tasks);
3379 static void __kmp_second_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
3380 kmp_int32 children = 0;
3384 KMP_TEST_THEN_DEC32(&taskdata->td_parent->td_incomplete_child_tasks) - 1;
3385 KMP_DEBUG_ASSERT(children >= 0);
3388 TCD_4(taskdata->td_incomplete_child_tasks);
3391 static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask) {
3392 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3393 kmp_info_t *thread = __kmp_threads[gtid];
3395 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3396 KMP_DEBUG_ASSERT(taskdata->td_flags.complete ==
3401 while (TCR_4(taskdata->td_incomplete_child_tasks) > 0)
3404 __kmp_release_deps(gtid, taskdata);
3405 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
3416 void __kmpc_proxy_task_completed(kmp_int32 gtid, kmp_task_t *ptask) {
3417 KMP_DEBUG_ASSERT(ptask != NULL);
3418 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3420 10, (
"__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n",
3423 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3425 __kmp_first_top_half_finish_proxy(taskdata);
3426 __kmp_second_top_half_finish_proxy(taskdata);
3427 __kmp_bottom_half_finish_proxy(gtid, ptask);
3430 (
"__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n",
3441 void __kmpc_proxy_task_completed_ooo(kmp_task_t *ptask) {
3442 KMP_DEBUG_ASSERT(ptask != NULL);
3443 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3447 (
"__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n",
3450 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3452 __kmp_first_top_half_finish_proxy(taskdata);
3456 kmp_team_t *team = taskdata->td_team;
3457 kmp_int32 nthreads = team->t.t_nproc;
3462 kmp_int32 start_k = 0;
3464 kmp_int32 k = start_k;
3468 thread = team->t.t_threads[k];
3469 k = (k + 1) % nthreads;
3475 }
while (!__kmp_give_task(thread, k, ptask, pass));
3477 __kmp_second_top_half_finish_proxy(taskdata);
3481 (
"__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n",
3491 kmp_task_t *__kmp_task_dup_alloc(kmp_info_t *thread, kmp_task_t *task_src) {
3493 kmp_taskdata_t *taskdata;
3494 kmp_taskdata_t *taskdata_src;
3495 kmp_taskdata_t *parent_task = thread->th.th_current_task;
3496 size_t shareds_offset;
3499 KA_TRACE(10, (
"__kmp_task_dup_alloc(enter): Th %p, source task %p\n", thread,
3501 taskdata_src = KMP_TASK_TO_TASKDATA(task_src);
3502 KMP_DEBUG_ASSERT(taskdata_src->td_flags.proxy ==
3504 KMP_DEBUG_ASSERT(taskdata_src->td_flags.tasktype == TASK_EXPLICIT);
3505 task_size = taskdata_src->td_size_alloc;
3508 KA_TRACE(30, (
"__kmp_task_dup_alloc: Th %p, malloc size %ld\n", thread,
3511 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, task_size);
3513 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, task_size);
3515 KMP_MEMCPY(taskdata, taskdata_src, task_size);
3517 task = KMP_TASKDATA_TO_TASK(taskdata);
3520 taskdata->td_task_id = KMP_GEN_TASK_ID();
3521 if (task->shareds != NULL) {
3522 shareds_offset = (
char *)task_src->shareds - (
char *)taskdata_src;
3523 task->shareds = &((
char *)taskdata)[shareds_offset];
3524 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
3527 taskdata->td_alloc_thread = thread;
3528 taskdata->td_parent = parent_task;
3529 taskdata->td_taskgroup =
3535 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
3536 KMP_TEST_THEN_INC32(&parent_task->td_incomplete_child_tasks);
3537 if (parent_task->td_taskgroup)
3538 KMP_TEST_THEN_INC32(&parent_task->td_taskgroup->count);
3541 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT)
3542 KMP_TEST_THEN_INC32(&taskdata->td_parent->td_allocated_child_tasks);
3546 (
"__kmp_task_dup_alloc(exit): Th %p, created task %p, parent=%p\n",
3547 thread, taskdata, taskdata->td_parent));
3549 if (UNLIKELY(ompt_enabled.enabled))
3550 __ompt_task_init(taskdata, thread->th.th_info.ds.ds_gtid);
3559 typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32);
3575 void __kmp_taskloop_linear(
ident_t *loc,
int gtid, kmp_task_t *task,
3576 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
3577 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
3578 kmp_uint64 grainsize, kmp_uint64 extras,
3579 kmp_uint64 tc,
void *task_dup) {
3581 KMP_TIME_PARTITIONED_BLOCK(OMP_taskloop_scheduling);
3582 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
3583 kmp_uint64 lower = *lb;
3584 kmp_uint64 upper = *ub;
3586 kmp_info_t *thread = __kmp_threads[gtid];
3587 kmp_taskdata_t *current_task = thread->th.th_current_task;
3588 kmp_task_t *next_task;
3589 kmp_int32 lastpriv = 0;
3590 size_t lower_offset =
3591 (
char *)lb - (
char *)task;
3592 size_t upper_offset =
3593 (
char *)ub - (
char *)task;
3595 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras);
3596 KMP_DEBUG_ASSERT(num_tasks > extras);
3597 KMP_DEBUG_ASSERT(num_tasks > 0);
3598 KA_TRACE(20, (
"__kmp_taskloop_linear: T#%d: %lld tasks, grainsize %lld, " 3599 "extras %lld, i=%lld,%lld(%d)%lld, dup %p\n",
3600 gtid, num_tasks, grainsize, extras, lower, upper, ub_glob, st,
3604 for (i = 0; i < num_tasks; ++i) {
3605 kmp_uint64 chunk_minus_1;
3607 chunk_minus_1 = grainsize - 1;
3609 chunk_minus_1 = grainsize;
3612 upper = lower + st * chunk_minus_1;
3613 if (i == num_tasks - 1) {
3616 KMP_DEBUG_ASSERT(upper == *ub);
3617 if (upper == ub_glob)
3619 }
else if (st > 0) {
3620 KMP_DEBUG_ASSERT((kmp_uint64)st > *ub - upper);
3621 if ((kmp_uint64)st > ub_glob - upper)
3624 KMP_DEBUG_ASSERT(upper + st < *ub);
3625 if (upper - ub_glob < (kmp_uint64)(-st))
3629 next_task = __kmp_task_dup_alloc(thread, task);
3631 *(kmp_uint64 *)((
char *)next_task + lower_offset) = lower;
3632 *(kmp_uint64 *)((
char *)next_task + upper_offset) = upper;
3633 if (ptask_dup != NULL)
3634 ptask_dup(next_task, task, lastpriv);
3635 KA_TRACE(40, (
"__kmp_taskloop_linear: T#%d; task %p: lower %lld, " 3636 "upper %lld (offsets %p %p)\n",
3637 gtid, next_task, lower, upper, lower_offset, upper_offset));
3638 __kmp_omp_task(gtid, next_task,
true);
3642 __kmp_task_start(gtid, task, current_task);
3644 __kmp_task_finish(gtid, task, current_task);
3649 typedef struct __taskloop_params {
3656 kmp_uint64 num_tasks;
3657 kmp_uint64 grainsize;
3660 kmp_uint64 num_t_min;
3661 } __taskloop_params_t;
3663 void __kmp_taskloop_recur(
ident_t *,
int, kmp_task_t *, kmp_uint64 *,
3664 kmp_uint64 *, kmp_int64, kmp_uint64, kmp_uint64,
3665 kmp_uint64, kmp_uint64, kmp_uint64, kmp_uint64,
3669 int __kmp_taskloop_task(
int gtid,
void *ptask) {
3670 __taskloop_params_t *p =
3671 (__taskloop_params_t *)((kmp_task_t *)ptask)->shareds;
3672 kmp_task_t *task = p->task;
3673 kmp_uint64 *lb = p->lb;
3674 kmp_uint64 *ub = p->ub;
3675 void *task_dup = p->task_dup;
3677 kmp_int64 st = p->st;
3678 kmp_uint64 ub_glob = p->ub_glob;
3679 kmp_uint64 num_tasks = p->num_tasks;
3680 kmp_uint64 grainsize = p->grainsize;
3681 kmp_uint64 extras = p->extras;
3682 kmp_uint64 tc = p->tc;
3683 kmp_uint64 num_t_min = p->num_t_min;
3685 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
3686 KMP_DEBUG_ASSERT(task != NULL);
3687 KA_TRACE(20, (
"__kmp_taskloop_task: T#%d, task %p: %lld tasks, grainsize" 3688 " %lld, extras %lld, i=%lld,%lld(%d), dup %p\n",
3689 gtid, taskdata, num_tasks, grainsize, extras, *lb, *ub, st,
3692 KMP_DEBUG_ASSERT(num_tasks * 2 + 1 > num_t_min);
3693 if (num_tasks > num_t_min)
3694 __kmp_taskloop_recur(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
3695 grainsize, extras, tc, num_t_min, task_dup);
3697 __kmp_taskloop_linear(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
3698 grainsize, extras, tc, task_dup);
3700 KA_TRACE(40, (
"__kmp_taskloop_task(exit): T#%d\n", gtid));
3720 void __kmp_taskloop_recur(
ident_t *loc,
int gtid, kmp_task_t *task,
3721 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
3722 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
3723 kmp_uint64 grainsize, kmp_uint64 extras,
3724 kmp_uint64 tc, kmp_uint64 num_t_min,
void *task_dup) {
3726 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
3727 KMP_DEBUG_ASSERT(task != NULL);
3728 KMP_DEBUG_ASSERT(num_tasks > num_t_min);
3729 KA_TRACE(20, (
"__kmp_taskloop_recur: T#%d, task %p: %lld tasks, grainsize" 3730 " %lld, extras %lld, i=%lld,%lld(%d), dup %p\n",
3731 gtid, taskdata, num_tasks, grainsize, extras, *lb, *ub, st,
3734 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
3735 kmp_uint64 lower = *lb;
3736 kmp_uint64 upper = *ub;
3737 kmp_info_t *thread = __kmp_threads[gtid];
3739 kmp_task_t *next_task;
3740 kmp_int32 lastpriv = 0;
3741 size_t lower_offset =
3742 (
char *)lb - (
char *)task;
3743 size_t upper_offset =
3744 (
char *)ub - (
char *)task;
3746 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras);
3747 KMP_DEBUG_ASSERT(num_tasks > extras);
3748 KMP_DEBUG_ASSERT(num_tasks > 0);
3751 kmp_uint64 lb1, ub0, tc0, tc1, ext0, ext1;
3752 kmp_uint64 gr_size0 = grainsize;
3753 kmp_uint64 n_tsk0 = num_tasks >> 1;
3754 kmp_uint64 n_tsk1 = num_tasks - n_tsk0;
3755 if (n_tsk0 <= extras) {
3758 ext1 = extras - n_tsk0;
3759 tc0 = gr_size0 * n_tsk0;
3764 tc1 = grainsize * n_tsk1;
3767 ub0 = lower + st * (tc0 - 1);
3771 next_task = __kmp_task_dup_alloc(thread, task);
3773 *(kmp_uint64 *)((
char *)next_task + lower_offset) = lb1;
3774 if (ptask_dup != NULL)
3775 ptask_dup(next_task, task, 0);
3779 kmp_task_t *new_task =
3780 __kmpc_omp_task_alloc(loc, gtid, 1, 3 *
sizeof(
void *),
3781 sizeof(__taskloop_params_t), &__kmp_taskloop_task);
3782 __taskloop_params_t *p = (__taskloop_params_t *)new_task->shareds;
3783 p->task = next_task;
3784 p->lb = (kmp_uint64 *)((
char *)next_task + lower_offset);
3785 p->ub = (kmp_uint64 *)((
char *)next_task + upper_offset);
3786 p->task_dup = task_dup;
3788 p->ub_glob = ub_glob;
3789 p->num_tasks = n_tsk1;
3790 p->grainsize = grainsize;
3793 p->num_t_min = num_t_min;
3794 __kmp_omp_task(gtid, new_task,
true);
3797 if (n_tsk0 > num_t_min)
3798 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0, gr_size0,
3799 ext0, tc0, num_t_min, task_dup);
3801 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0,
3802 gr_size0, ext0, tc0, task_dup);
3804 KA_TRACE(40, (
"__kmpc_taskloop_recur(exit): T#%d\n", gtid));
3823 void __kmpc_taskloop(
ident_t *loc,
int gtid, kmp_task_t *task,
int if_val,
3824 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
int nogroup,
3825 int sched, kmp_uint64 grainsize,
void *task_dup) {
3826 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
3827 KMP_DEBUG_ASSERT(task != NULL);
3829 KA_TRACE(20, (
"__kmpc_taskloop: T#%d, task %p, lb %lld, ub %lld, st %lld, " 3830 "grain %llu(%d), dup %p\n",
3831 gtid, taskdata, *lb, *ub, st, grainsize, sched, task_dup));
3833 #if OMPT_SUPPORT && OMPT_OPTIONAL 3834 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
3835 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
3836 if (ompt_enabled.ompt_callback_work) {
3837 ompt_callbacks.ompt_callback(ompt_callback_work)(
3838 ompt_work_taskloop, ompt_scope_begin, &(team_info->parallel_data),
3839 &(task_info->task_data), 0, OMPT_GET_RETURN_ADDRESS(0));
3844 #if OMPT_SUPPORT && OMPT_OPTIONAL 3845 OMPT_STORE_RETURN_ADDRESS(gtid);
3847 __kmpc_taskgroup(loc, gtid);
3853 kmp_uint64 lower = *lb;
3854 kmp_uint64 upper = *ub;
3855 kmp_uint64 ub_glob = upper;
3856 kmp_uint64 num_tasks = 0, extras = 0;
3857 kmp_uint64 num_tasks_min = __kmp_taskloop_min_tasks;
3858 kmp_info_t *thread = __kmp_threads[gtid];
3859 kmp_taskdata_t *current_task = thread->th.th_current_task;
3863 tc = upper - lower + 1;
3864 }
else if (st < 0) {
3865 tc = (lower - upper) / (-st) + 1;
3867 tc = (upper - lower) / st + 1;
3870 KA_TRACE(20, (
"__kmpc_taskloop(exit): T#%d zero-trip loop\n", gtid));
3872 __kmp_task_start(gtid, task, current_task);
3874 __kmp_task_finish(gtid, task, current_task);
3877 if (num_tasks_min == 0)
3880 KMP_MIN(thread->th.th_team_nproc * 10, INITIAL_TASK_DEQUE_SIZE);
3886 grainsize = thread->th.th_team_nproc * 10;
3888 if (grainsize > tc) {
3893 num_tasks = grainsize;
3894 grainsize = tc / num_tasks;
3895 extras = tc % num_tasks;
3899 if (grainsize > tc) {
3904 num_tasks = tc / grainsize;
3906 grainsize = tc / num_tasks;
3907 extras = tc % num_tasks;
3911 KMP_ASSERT2(0,
"unknown scheduling of taskloop");
3913 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras);
3914 KMP_DEBUG_ASSERT(num_tasks > extras);
3915 KMP_DEBUG_ASSERT(num_tasks > 0);
3920 taskdata->td_flags.task_serial = 1;
3921 taskdata->td_flags.tiedness = TASK_TIED;
3922 #if OMPT_SUPPORT && OMPT_OPTIONAL 3923 OMPT_STORE_RETURN_ADDRESS(gtid);
3926 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
3927 grainsize, extras, tc, task_dup);
3928 }
else if (num_tasks > num_tasks_min) {
3929 KA_TRACE(20, (
"__kmpc_taskloop: T#%d, go recursive: tc %llu, #tasks %llu" 3930 "(%lld), grain %llu, extras %llu\n",
3931 gtid, tc, num_tasks, num_tasks_min, grainsize, extras));
3932 #if OMPT_SUPPORT && OMPT_OPTIONAL 3933 OMPT_STORE_RETURN_ADDRESS(gtid);
3935 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
3936 grainsize, extras, tc, num_tasks_min, task_dup);
3938 KA_TRACE(20, (
"__kmpc_taskloop: T#%d, go linear: tc %llu, #tasks %llu" 3939 "(%lld), grain %llu, extras %llu\n",
3940 gtid, tc, num_tasks, num_tasks_min, grainsize, extras));
3941 #if OMPT_SUPPORT && OMPT_OPTIONAL 3942 OMPT_STORE_RETURN_ADDRESS(gtid);
3944 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
3945 grainsize, extras, tc, task_dup);
3949 #if OMPT_SUPPORT && OMPT_OPTIONAL 3950 OMPT_STORE_RETURN_ADDRESS(gtid);
3952 __kmpc_end_taskgroup(loc, gtid);
3954 #if OMPT_SUPPORT && OMPT_OPTIONAL 3955 if (ompt_enabled.ompt_callback_work) {
3956 ompt_callbacks.ompt_callback(ompt_callback_work)(
3957 ompt_work_taskloop, ompt_scope_end, &(team_info->parallel_data),
3958 &(task_info->task_data), 0, OMPT_GET_RETURN_ADDRESS(0));
3961 KA_TRACE(20, (
"__kmpc_taskloop(exit): T#%d\n", gtid));
#define KMP_COUNT_BLOCK(name)
Increments specified counter (name).