17 #include "kmp_stats.h" 18 #include "kmp_wait_release.h" 19 #include "kmp_taskdeps.h" 22 #include "ompt-specific.h" 25 #include "tsan_annotations.h" 28 static void __kmp_enable_tasking(kmp_task_team_t *task_team,
29 kmp_info_t *this_thr);
30 static void __kmp_alloc_task_deque(kmp_info_t *thread,
31 kmp_thread_data_t *thread_data);
32 static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
33 kmp_task_team_t *task_team);
36 static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask);
39 #ifdef BUILD_TIED_TASK_STACK 48 static void __kmp_trace_task_stack(kmp_int32 gtid,
49 kmp_thread_data_t *thread_data,
50 int threshold,
char *location) {
51 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
52 kmp_taskdata_t **stack_top = task_stack->ts_top;
53 kmp_int32 entries = task_stack->ts_entries;
54 kmp_taskdata_t *tied_task;
58 (
"__kmp_trace_task_stack(start): location = %s, gtid = %d, entries = %d, " 59 "first_block = %p, stack_top = %p \n",
60 location, gtid, entries, task_stack->ts_first_block, stack_top));
62 KMP_DEBUG_ASSERT(stack_top != NULL);
63 KMP_DEBUG_ASSERT(entries > 0);
65 while (entries != 0) {
66 KMP_DEBUG_ASSERT(stack_top != &task_stack->ts_first_block.sb_block[0]);
68 if (entries & TASK_STACK_INDEX_MASK == 0) {
69 kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(stack_top);
71 stack_block = stack_block->sb_prev;
72 stack_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE];
79 tied_task = *stack_top;
81 KMP_DEBUG_ASSERT(tied_task != NULL);
82 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
85 (
"__kmp_trace_task_stack(%s): gtid=%d, entry=%d, " 86 "stack_top=%p, tied_task=%p\n",
87 location, gtid, entries, stack_top, tied_task));
89 KMP_DEBUG_ASSERT(stack_top == &task_stack->ts_first_block.sb_block[0]);
92 (
"__kmp_trace_task_stack(exit): location = %s, gtid = %d\n",
102 static void __kmp_init_task_stack(kmp_int32 gtid,
103 kmp_thread_data_t *thread_data) {
104 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
105 kmp_stack_block_t *first_block;
108 first_block = &task_stack->ts_first_block;
109 task_stack->ts_top = (kmp_taskdata_t **)first_block;
110 memset((
void *)first_block,
'\0',
111 TASK_STACK_BLOCK_SIZE *
sizeof(kmp_taskdata_t *));
114 task_stack->ts_entries = TASK_STACK_EMPTY;
115 first_block->sb_next = NULL;
116 first_block->sb_prev = NULL;
123 static void __kmp_free_task_stack(kmp_int32 gtid,
124 kmp_thread_data_t *thread_data) {
125 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
126 kmp_stack_block_t *stack_block = &task_stack->ts_first_block;
128 KMP_DEBUG_ASSERT(task_stack->ts_entries == TASK_STACK_EMPTY);
130 while (stack_block != NULL) {
131 kmp_stack_block_t *next_block = (stack_block) ? stack_block->sb_next : NULL;
133 stack_block->sb_next = NULL;
134 stack_block->sb_prev = NULL;
135 if (stack_block != &task_stack->ts_first_block) {
136 __kmp_thread_free(thread,
139 stack_block = next_block;
142 task_stack->ts_entries = 0;
143 task_stack->ts_top = NULL;
152 static void __kmp_push_task_stack(kmp_int32 gtid, kmp_info_t *thread,
153 kmp_taskdata_t *tied_task) {
155 kmp_thread_data_t *thread_data =
156 &thread->th.th_task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
157 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
159 if (tied_task->td_flags.team_serial || tied_task->td_flags.tasking_ser) {
163 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
164 KMP_DEBUG_ASSERT(task_stack->ts_top != NULL);
167 (
"__kmp_push_task_stack(enter): GTID: %d; THREAD: %p; TASK: %p\n",
168 gtid, thread, tied_task));
170 *(task_stack->ts_top) = tied_task;
173 task_stack->ts_top++;
174 task_stack->ts_entries++;
176 if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) {
178 kmp_stack_block_t *stack_block =
179 (kmp_stack_block_t *)(task_stack->ts_top - TASK_STACK_BLOCK_SIZE);
182 if (stack_block->sb_next !=
184 task_stack->ts_top = &stack_block->sb_next->sb_block[0];
186 kmp_stack_block_t *new_block = (kmp_stack_block_t *)__kmp_thread_calloc(
187 thread,
sizeof(kmp_stack_block_t));
189 task_stack->ts_top = &new_block->sb_block[0];
190 stack_block->sb_next = new_block;
191 new_block->sb_prev = stack_block;
192 new_block->sb_next = NULL;
196 (
"__kmp_push_task_stack(): GTID: %d; TASK: %p; Alloc new block: %p\n",
197 gtid, tied_task, new_block));
200 KA_TRACE(20, (
"__kmp_push_task_stack(exit): GTID: %d; TASK: %p\n", gtid,
211 static void __kmp_pop_task_stack(kmp_int32 gtid, kmp_info_t *thread,
212 kmp_taskdata_t *ending_task) {
214 kmp_thread_data_t *thread_data =
215 &thread->th.th_task_team->tt_threads_data[__kmp_tid_from_gtid(gtid)];
216 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
217 kmp_taskdata_t *tied_task;
219 if (ending_task->td_flags.team_serial || ending_task->td_flags.tasking_ser) {
224 KMP_DEBUG_ASSERT(task_stack->ts_top != NULL);
225 KMP_DEBUG_ASSERT(task_stack->ts_entries > 0);
227 KA_TRACE(20, (
"__kmp_pop_task_stack(enter): GTID: %d; THREAD: %p\n", gtid,
231 if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) {
232 kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(task_stack->ts_top);
234 stack_block = stack_block->sb_prev;
235 task_stack->ts_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE];
239 task_stack->ts_top--;
240 task_stack->ts_entries--;
242 tied_task = *(task_stack->ts_top);
244 KMP_DEBUG_ASSERT(tied_task != NULL);
245 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
246 KMP_DEBUG_ASSERT(tied_task == ending_task);
248 KA_TRACE(20, (
"__kmp_pop_task_stack(exit): GTID: %d; TASK: %p\n", gtid,
257 static bool __kmp_task_is_allowed(
int gtid,
const kmp_int32 is_constrained,
258 const kmp_taskdata_t *tasknew,
259 const kmp_taskdata_t *taskcurr) {
260 if (is_constrained && (tasknew->td_flags.tiedness == TASK_TIED)) {
264 kmp_taskdata_t *current = taskcurr->td_last_tied;
265 KMP_DEBUG_ASSERT(current != NULL);
267 if (current->td_flags.tasktype == TASK_EXPLICIT ||
268 current->td_taskwait_thread > 0) {
269 kmp_int32 level = current->td_level;
270 kmp_taskdata_t *parent = tasknew->td_parent;
271 while (parent != current && parent->td_level > level) {
273 parent = parent->td_parent;
274 KMP_DEBUG_ASSERT(parent != NULL);
276 if (parent != current)
281 kmp_depnode_t *node = tasknew->td_depnode;
282 if (node && (node->dn.mtx_num_locks > 0)) {
283 for (
int i = 0; i < node->dn.mtx_num_locks; ++i) {
284 KMP_DEBUG_ASSERT(node->dn.mtx_locks[i] != NULL);
285 if (__kmp_test_lock(node->dn.mtx_locks[i], gtid))
288 for (
int j = i - 1; j >= 0; --j)
289 __kmp_release_lock(node->dn.mtx_locks[j], gtid);
293 node->dn.mtx_num_locks = -node->dn.mtx_num_locks;
302 static void __kmp_realloc_task_deque(kmp_info_t *thread,
303 kmp_thread_data_t *thread_data) {
304 kmp_int32 size = TASK_DEQUE_SIZE(thread_data->td);
305 kmp_int32 new_size = 2 * size;
307 KE_TRACE(10, (
"__kmp_realloc_task_deque: T#%d reallocating deque[from %d to " 308 "%d] for thread_data %p\n",
309 __kmp_gtid_from_thread(thread), size, new_size, thread_data));
311 kmp_taskdata_t **new_deque =
312 (kmp_taskdata_t **)__kmp_allocate(new_size *
sizeof(kmp_taskdata_t *));
315 for (i = thread_data->td.td_deque_head, j = 0; j < size;
316 i = (i + 1) & TASK_DEQUE_MASK(thread_data->td), j++)
317 new_deque[j] = thread_data->td.td_deque[i];
319 __kmp_free(thread_data->td.td_deque);
321 thread_data->td.td_deque_head = 0;
322 thread_data->td.td_deque_tail = size;
323 thread_data->td.td_deque = new_deque;
324 thread_data->td.td_deque_size = new_size;
328 static kmp_int32 __kmp_push_task(kmp_int32 gtid, kmp_task_t *task) {
329 kmp_info_t *thread = __kmp_threads[gtid];
330 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
331 kmp_task_team_t *task_team = thread->th.th_task_team;
332 kmp_int32 tid = __kmp_tid_from_gtid(gtid);
333 kmp_thread_data_t *thread_data;
336 (
"__kmp_push_task: T#%d trying to push task %p.\n", gtid, taskdata));
338 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
341 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
342 KMP_DEBUG_USE_VAR(counter);
345 (
"__kmp_push_task: T#%d untied_count (%d) incremented for task %p\n",
346 gtid, counter, taskdata));
350 if (taskdata->td_flags.task_serial) {
351 KA_TRACE(20, (
"__kmp_push_task: T#%d team serialized; returning " 352 "TASK_NOT_PUSHED for task %p\n",
354 return TASK_NOT_PUSHED;
359 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
360 if (!KMP_TASKING_ENABLED(task_team)) {
361 __kmp_enable_tasking(task_team, thread);
363 KMP_DEBUG_ASSERT(TCR_4(task_team->tt.tt_found_tasks) == TRUE);
364 KMP_DEBUG_ASSERT(TCR_PTR(task_team->tt.tt_threads_data) != NULL);
367 thread_data = &task_team->tt.tt_threads_data[tid];
370 if (thread_data->td.td_deque == NULL) {
371 __kmp_alloc_task_deque(thread, thread_data);
376 if (TCR_4(thread_data->td.td_deque_ntasks) >=
377 TASK_DEQUE_SIZE(thread_data->td)) {
378 if (__kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
379 thread->th.th_current_task)) {
380 KA_TRACE(20, (
"__kmp_push_task: T#%d deque is full; returning " 381 "TASK_NOT_PUSHED for task %p\n",
383 return TASK_NOT_PUSHED;
385 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
388 __kmp_realloc_task_deque(thread, thread_data);
393 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
396 if (TCR_4(thread_data->td.td_deque_ntasks) >=
397 TASK_DEQUE_SIZE(thread_data->td)) {
398 if (__kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
399 thread->th.th_current_task)) {
400 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
401 KA_TRACE(20, (
"__kmp_push_task: T#%d deque is full on 2nd check; " 402 "returning TASK_NOT_PUSHED for task %p\n",
404 return TASK_NOT_PUSHED;
407 __kmp_realloc_task_deque(thread, thread_data);
413 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) <
414 TASK_DEQUE_SIZE(thread_data->td));
416 thread_data->td.td_deque[thread_data->td.td_deque_tail] =
419 thread_data->td.td_deque_tail =
420 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
421 TCW_4(thread_data->td.td_deque_ntasks,
422 TCR_4(thread_data->td.td_deque_ntasks) + 1);
424 KA_TRACE(20, (
"__kmp_push_task: T#%d returning TASK_SUCCESSFULLY_PUSHED: " 425 "task=%p ntasks=%d head=%u tail=%u\n",
426 gtid, taskdata, thread_data->td.td_deque_ntasks,
427 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
429 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
431 return TASK_SUCCESSFULLY_PUSHED;
438 void __kmp_pop_current_task_from_thread(kmp_info_t *this_thr) {
439 KF_TRACE(10, (
"__kmp_pop_current_task_from_thread(enter): T#%d " 440 "this_thread=%p, curtask=%p, " 441 "curtask_parent=%p\n",
442 0, this_thr, this_thr->th.th_current_task,
443 this_thr->th.th_current_task->td_parent));
445 this_thr->th.th_current_task = this_thr->th.th_current_task->td_parent;
447 KF_TRACE(10, (
"__kmp_pop_current_task_from_thread(exit): T#%d " 448 "this_thread=%p, curtask=%p, " 449 "curtask_parent=%p\n",
450 0, this_thr, this_thr->th.th_current_task,
451 this_thr->th.th_current_task->td_parent));
460 void __kmp_push_current_task_to_thread(kmp_info_t *this_thr, kmp_team_t *team,
464 KF_TRACE(10, (
"__kmp_push_current_task_to_thread(enter): T#%d this_thread=%p " 467 tid, this_thr, this_thr->th.th_current_task,
468 team->t.t_implicit_task_taskdata[tid].td_parent));
470 KMP_DEBUG_ASSERT(this_thr != NULL);
473 if (this_thr->th.th_current_task != &team->t.t_implicit_task_taskdata[0]) {
474 team->t.t_implicit_task_taskdata[0].td_parent =
475 this_thr->th.th_current_task;
476 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[0];
479 team->t.t_implicit_task_taskdata[tid].td_parent =
480 team->t.t_implicit_task_taskdata[0].td_parent;
481 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[tid];
484 KF_TRACE(10, (
"__kmp_push_current_task_to_thread(exit): T#%d this_thread=%p " 487 tid, this_thr, this_thr->th.th_current_task,
488 team->t.t_implicit_task_taskdata[tid].td_parent));
496 static void __kmp_task_start(kmp_int32 gtid, kmp_task_t *task,
497 kmp_taskdata_t *current_task) {
498 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
499 kmp_info_t *thread = __kmp_threads[gtid];
502 (
"__kmp_task_start(enter): T#%d starting task %p: current_task=%p\n",
503 gtid, taskdata, current_task));
505 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
510 current_task->td_flags.executing = 0;
513 #ifdef BUILD_TIED_TASK_STACK 514 if (taskdata->td_flags.tiedness == TASK_TIED) {
515 __kmp_push_task_stack(gtid, thread, taskdata);
520 thread->th.th_current_task = taskdata;
522 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 0 ||
523 taskdata->td_flags.tiedness == TASK_UNTIED);
524 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0 ||
525 taskdata->td_flags.tiedness == TASK_UNTIED);
526 taskdata->td_flags.started = 1;
527 taskdata->td_flags.executing = 1;
528 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
529 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
536 KA_TRACE(10, (
"__kmp_task_start(exit): T#%d task=%p\n", gtid, taskdata));
547 static inline void __ompt_task_init(kmp_taskdata_t *task,
int tid) {
549 task->ompt_task_info.task_data.value = 0;
550 task->ompt_task_info.frame.exit_frame = NULL;
551 task->ompt_task_info.frame.enter_frame = NULL;
553 task->ompt_task_info.ndeps = 0;
554 task->ompt_task_info.deps = NULL;
560 static inline void __ompt_task_start(kmp_task_t *task,
561 kmp_taskdata_t *current_task,
563 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
564 ompt_task_status_t status = ompt_task_switch;
565 if (__kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded) {
566 status = ompt_task_yield;
567 __kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded = 0;
570 if (ompt_enabled.ompt_callback_task_schedule) {
571 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
572 &(current_task->ompt_task_info.task_data), status,
573 &(taskdata->ompt_task_info.task_data));
575 taskdata->ompt_task_info.scheduling_parent = current_task;
581 __ompt_task_finish(kmp_task_t *task, kmp_taskdata_t *resumed_task,
582 ompt_task_status_t status = ompt_task_complete) {
583 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
584 if (__kmp_omp_cancellation && taskdata->td_taskgroup &&
585 taskdata->td_taskgroup->cancel_request == cancel_taskgroup) {
586 status = ompt_task_cancel;
590 if (ompt_enabled.ompt_callback_task_schedule) {
591 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
592 &(taskdata->ompt_task_info.task_data), status,
593 &((resumed_task ? resumed_task
594 : (taskdata->ompt_task_info.scheduling_parent
595 ? taskdata->ompt_task_info.scheduling_parent
596 : taskdata->td_parent))
597 ->ompt_task_info.task_data));
603 static void __kmpc_omp_task_begin_if0_template(
ident_t *loc_ref, kmp_int32 gtid,
606 void *return_address) {
607 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
608 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
610 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(enter): T#%d loc=%p task=%p " 612 gtid, loc_ref, taskdata, current_task));
614 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
617 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
618 KMP_DEBUG_USE_VAR(counter);
619 KA_TRACE(20, (
"__kmpc_omp_task_begin_if0: T#%d untied_count (%d) " 620 "incremented for task %p\n",
621 gtid, counter, taskdata));
624 taskdata->td_flags.task_serial =
626 __kmp_task_start(gtid, task, current_task);
630 if (current_task->ompt_task_info.frame.enter_frame == NULL) {
631 current_task->ompt_task_info.frame.enter_frame =
632 taskdata->ompt_task_info.frame.exit_frame = frame_address;
634 if (ompt_enabled.ompt_callback_task_create) {
635 ompt_task_info_t *parent_info = &(current_task->ompt_task_info);
636 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
637 &(parent_info->task_data), &(parent_info->frame),
638 &(taskdata->ompt_task_info.task_data),
639 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(taskdata), 0,
642 __ompt_task_start(task, current_task, gtid);
644 #endif // OMPT_SUPPORT 646 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(exit): T#%d loc=%p task=%p,\n", gtid,
652 static void __kmpc_omp_task_begin_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
655 void *return_address) {
656 __kmpc_omp_task_begin_if0_template<true>(loc_ref, gtid, task, frame_address,
659 #endif // OMPT_SUPPORT 667 void __kmpc_omp_task_begin_if0(
ident_t *loc_ref, kmp_int32 gtid,
670 if (UNLIKELY(ompt_enabled.enabled)) {
671 OMPT_STORE_RETURN_ADDRESS(gtid);
672 __kmpc_omp_task_begin_if0_ompt(loc_ref, gtid, task,
673 OMPT_GET_FRAME_ADDRESS(1),
674 OMPT_LOAD_RETURN_ADDRESS(gtid));
678 __kmpc_omp_task_begin_if0_template<false>(loc_ref, gtid, task, NULL, NULL);
684 void __kmpc_omp_task_begin(
ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task) {
685 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
689 (
"__kmpc_omp_task_begin(enter): T#%d loc=%p task=%p current_task=%p\n",
690 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task), current_task));
692 __kmp_task_start(gtid, task, current_task);
694 KA_TRACE(10, (
"__kmpc_omp_task_begin(exit): T#%d loc=%p task=%p,\n", gtid,
695 loc_ref, KMP_TASK_TO_TASKDATA(task)));
698 #endif // TASK_UNUSED 705 static void __kmp_free_task(kmp_int32 gtid, kmp_taskdata_t *taskdata,
706 kmp_info_t *thread) {
707 KA_TRACE(30, (
"__kmp_free_task: T#%d freeing data from task %p\n", gtid,
711 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
712 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0);
713 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 1);
714 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
715 KMP_DEBUG_ASSERT(taskdata->td_allocated_child_tasks == 0 ||
716 taskdata->td_flags.task_serial == 1);
717 KMP_DEBUG_ASSERT(taskdata->td_incomplete_child_tasks == 0);
719 taskdata->td_flags.freed = 1;
720 ANNOTATE_HAPPENS_BEFORE(taskdata);
723 __kmp_fast_free(thread, taskdata);
725 __kmp_thread_free(thread, taskdata);
728 KA_TRACE(20, (
"__kmp_free_task: T#%d freed task %p\n", gtid, taskdata));
737 static void __kmp_free_task_and_ancestors(kmp_int32 gtid,
738 kmp_taskdata_t *taskdata,
739 kmp_info_t *thread) {
743 kmp_int32 team_serial =
744 (taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) &&
745 !taskdata->td_flags.proxy;
747 kmp_int32 team_serial =
748 taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser;
750 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
752 kmp_int32 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
753 KMP_DEBUG_ASSERT(children >= 0);
756 while (children == 0) {
757 kmp_taskdata_t *parent_taskdata = taskdata->td_parent;
759 KA_TRACE(20, (
"__kmp_free_task_and_ancestors(enter): T#%d task %p complete " 760 "and freeing itself\n",
764 __kmp_free_task(gtid, taskdata, thread);
766 taskdata = parent_taskdata;
772 if (taskdata->td_flags.tasktype == TASK_IMPLICIT) {
773 if (taskdata->td_dephash) {
774 int children = KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks);
775 kmp_tasking_flags_t flags_old = taskdata->td_flags;
776 if (children == 0 && flags_old.complete == 1) {
777 kmp_tasking_flags_t flags_new = flags_old;
778 flags_new.complete = 0;
779 if (KMP_COMPARE_AND_STORE_ACQ32(
780 RCAST(kmp_int32 *, &taskdata->td_flags),
781 *RCAST(kmp_int32 *, &flags_old),
782 *RCAST(kmp_int32 *, &flags_new))) {
783 KA_TRACE(100, (
"__kmp_free_task_and_ancestors: T#%d cleans " 784 "dephash of implicit task %p\n",
787 __kmp_dephash_free_entries(thread, taskdata->td_dephash);
794 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
795 KMP_DEBUG_ASSERT(children >= 0);
799 20, (
"__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; " 800 "not freeing it yet\n",
801 gtid, taskdata, children));
810 static void __kmp_task_finish(kmp_int32 gtid, kmp_task_t *task,
811 kmp_taskdata_t *resumed_task) {
812 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
813 kmp_info_t *thread = __kmp_threads[gtid];
814 kmp_task_team_t *task_team =
815 thread->th.th_task_team;
816 kmp_int32 children = 0;
818 KA_TRACE(10, (
"__kmp_task_finish(enter): T#%d finishing task %p and resuming " 820 gtid, taskdata, resumed_task));
822 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
825 #ifdef BUILD_TIED_TASK_STACK 826 if (taskdata->td_flags.tiedness == TASK_TIED) {
827 __kmp_pop_task_stack(gtid, thread, taskdata);
831 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
834 kmp_int32 counter = KMP_ATOMIC_DEC(&taskdata->td_untied_count) - 1;
837 (
"__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n",
838 gtid, counter, taskdata));
842 if (resumed_task == NULL) {
843 KMP_DEBUG_ASSERT(taskdata->td_flags.task_serial);
844 resumed_task = taskdata->td_parent;
847 thread->th.th_current_task = resumed_task;
848 resumed_task->td_flags.executing = 1;
849 KA_TRACE(10, (
"__kmp_task_finish(exit): T#%d partially done task %p, " 850 "resuming task %p\n",
851 gtid, taskdata, resumed_task));
857 __ompt_task_finish(task, resumed_task);
861 kmp_depnode_t *node = taskdata->td_depnode;
862 if (node && (node->dn.mtx_num_locks < 0)) {
864 node->dn.mtx_num_locks = -node->dn.mtx_num_locks;
865 for (
int i = node->dn.mtx_num_locks - 1; i >= 0; --i) {
866 KMP_DEBUG_ASSERT(node->dn.mtx_locks[i] != NULL);
867 __kmp_release_lock(node->dn.mtx_locks[i], gtid);
871 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
872 taskdata->td_flags.complete = 1;
873 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 1);
874 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
878 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
881 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks) - 1;
882 KMP_DEBUG_ASSERT(children >= 0);
884 if (taskdata->td_taskgroup)
885 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
886 __kmp_release_deps(gtid, taskdata);
888 }
else if (task_team && task_team->tt.tt_found_proxy_tasks) {
891 __kmp_release_deps(gtid, taskdata);
892 #endif // OMP_45_ENABLED 893 #endif // OMP_40_ENABLED 899 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
900 taskdata->td_flags.executing = 0;
903 20, (
"__kmp_task_finish: T#%d finished task %p, %d incomplete children\n",
904 gtid, taskdata, children));
913 if (taskdata->td_flags.destructors_thunk) {
914 kmp_routine_entry_t destr_thunk = task->data1.destructors;
915 KMP_ASSERT(destr_thunk);
916 destr_thunk(gtid, task);
918 #endif // OMP_40_ENABLED 923 (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) ==
924 taskdata->td_flags.task_serial);
925 if (taskdata->td_flags.task_serial) {
926 if (resumed_task == NULL) {
927 resumed_task = taskdata->td_parent;
931 KMP_DEBUG_ASSERT(resumed_task !=
939 thread->th.th_current_task = resumed_task;
940 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
944 resumed_task->td_flags.executing = 1;
947 10, (
"__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n",
948 gtid, taskdata, resumed_task));
954 static void __kmpc_omp_task_complete_if0_template(
ident_t *loc_ref,
957 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n",
958 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
960 __kmp_task_finish<ompt>(gtid, task, NULL);
962 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n",
963 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
967 omp_frame_t *ompt_frame;
968 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
969 ompt_frame->enter_frame = NULL;
978 void __kmpc_omp_task_complete_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
980 __kmpc_omp_task_complete_if0_template<true>(loc_ref, gtid, task);
982 #endif // OMPT_SUPPORT 989 void __kmpc_omp_task_complete_if0(
ident_t *loc_ref, kmp_int32 gtid,
992 if (UNLIKELY(ompt_enabled.enabled)) {
993 __kmpc_omp_task_complete_if0_ompt(loc_ref, gtid, task);
997 __kmpc_omp_task_complete_if0_template<false>(loc_ref, gtid, task);
1003 void __kmpc_omp_task_complete(
ident_t *loc_ref, kmp_int32 gtid,
1005 KA_TRACE(10, (
"__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n", gtid,
1006 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1008 __kmp_task_finish<false>(gtid, task,
1011 KA_TRACE(10, (
"__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n", gtid,
1012 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1015 #endif // TASK_UNUSED 1028 void __kmp_init_implicit_task(
ident_t *loc_ref, kmp_info_t *this_thr,
1029 kmp_team_t *team,
int tid,
int set_curr_task) {
1030 kmp_taskdata_t *task = &team->t.t_implicit_task_taskdata[tid];
1034 (
"__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n",
1035 tid, team, task, set_curr_task ?
"TRUE" :
"FALSE"));
1037 task->td_task_id = KMP_GEN_TASK_ID();
1038 task->td_team = team;
1041 task->td_ident = loc_ref;
1042 task->td_taskwait_ident = NULL;
1043 task->td_taskwait_counter = 0;
1044 task->td_taskwait_thread = 0;
1046 task->td_flags.tiedness = TASK_TIED;
1047 task->td_flags.tasktype = TASK_IMPLICIT;
1049 task->td_flags.proxy = TASK_FULL;
1053 task->td_flags.task_serial = 1;
1054 task->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1055 task->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1057 task->td_flags.started = 1;
1058 task->td_flags.executing = 1;
1059 task->td_flags.complete = 0;
1060 task->td_flags.freed = 0;
1063 task->td_depnode = NULL;
1065 task->td_last_tied = task;
1067 if (set_curr_task) {
1068 KMP_ATOMIC_ST_REL(&task->td_incomplete_child_tasks, 0);
1070 KMP_ATOMIC_ST_REL(&task->td_allocated_child_tasks, 0);
1072 task->td_taskgroup = NULL;
1073 task->td_dephash = NULL;
1075 __kmp_push_current_task_to_thread(this_thr, team, tid);
1077 KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0);
1078 KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0);
1082 if (UNLIKELY(ompt_enabled.enabled))
1083 __ompt_task_init(task, tid);
1086 KF_TRACE(10, (
"__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n", tid,
1095 void __kmp_finish_implicit_task(kmp_info_t *thread) {
1096 kmp_taskdata_t *task = thread->th.th_current_task;
1097 if (task->td_dephash) {
1099 task->td_flags.complete = 1;
1100 children = KMP_ATOMIC_LD_ACQ(&task->td_incomplete_child_tasks);
1101 kmp_tasking_flags_t flags_old = task->td_flags;
1102 if (children == 0 && flags_old.complete == 1) {
1103 kmp_tasking_flags_t flags_new = flags_old;
1104 flags_new.complete = 0;
1105 if (KMP_COMPARE_AND_STORE_ACQ32(RCAST(kmp_int32 *, &task->td_flags),
1106 *RCAST(kmp_int32 *, &flags_old),
1107 *RCAST(kmp_int32 *, &flags_new))) {
1108 KA_TRACE(100, (
"__kmp_finish_implicit_task: T#%d cleans " 1109 "dephash of implicit task %p\n",
1110 thread->th.th_info.ds.ds_gtid, task));
1111 __kmp_dephash_free_entries(thread, task->td_dephash);
1121 void __kmp_free_implicit_task(kmp_info_t *thread) {
1122 kmp_taskdata_t *task = thread->th.th_current_task;
1123 if (task && task->td_dephash) {
1124 __kmp_dephash_free(thread, task->td_dephash);
1125 task->td_dephash = NULL;
1131 static size_t __kmp_round_up_to_val(
size_t size,
size_t val) {
1132 if (size & (val - 1)) {
1134 if (size <= KMP_SIZE_T_MAX - val) {
1153 kmp_task_t *__kmp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1154 kmp_tasking_flags_t *flags,
1155 size_t sizeof_kmp_task_t,
size_t sizeof_shareds,
1156 kmp_routine_entry_t task_entry) {
1158 kmp_taskdata_t *taskdata;
1159 kmp_info_t *thread = __kmp_threads[gtid];
1160 kmp_team_t *team = thread->th.th_team;
1161 kmp_taskdata_t *parent_task = thread->th.th_current_task;
1162 size_t shareds_offset;
1164 if (!TCR_4(__kmp_init_middle))
1165 __kmp_middle_initialize();
1167 KA_TRACE(10, (
"__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) " 1168 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1169 gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t,
1170 sizeof_shareds, task_entry));
1172 if (parent_task->td_flags.final) {
1173 if (flags->merged_if0) {
1177 if (flags->tiedness == TASK_UNTIED && !team->t.t_serialized) {
1181 KMP_CHECK_UPDATE(thread->th.th_task_team->tt.tt_untied_task_encountered, 1);
1185 if (flags->proxy == TASK_PROXY) {
1186 flags->tiedness = TASK_UNTIED;
1187 flags->merged_if0 = 1;
1191 if ((thread->th.th_task_team) == NULL) {
1194 KMP_DEBUG_ASSERT(team->t.t_serialized);
1196 (
"T#%d creating task team in __kmp_task_alloc for proxy task\n",
1198 __kmp_task_team_setup(
1201 thread->th.th_task_team = team->t.t_task_team[thread->th.th_task_state];
1203 kmp_task_team_t *task_team = thread->th.th_task_team;
1206 if (!KMP_TASKING_ENABLED(task_team)) {
1209 (
"T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid));
1210 __kmp_enable_tasking(task_team, thread);
1211 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
1212 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
1214 if (thread_data->td.td_deque == NULL) {
1215 __kmp_alloc_task_deque(thread, thread_data);
1219 if (task_team->tt.tt_found_proxy_tasks == FALSE)
1220 TCW_4(task_team->tt.tt_found_proxy_tasks, TRUE);
1226 shareds_offset =
sizeof(kmp_taskdata_t) + sizeof_kmp_task_t;
1227 shareds_offset = __kmp_round_up_to_val(shareds_offset,
sizeof(
void *));
1230 KA_TRACE(30, (
"__kmp_task_alloc: T#%d First malloc size: %ld\n", gtid,
1232 KA_TRACE(30, (
"__kmp_task_alloc: T#%d Second malloc size: %ld\n", gtid,
1237 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, shareds_offset +
1240 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, shareds_offset +
1243 ANNOTATE_HAPPENS_AFTER(taskdata);
1245 task = KMP_TASKDATA_TO_TASK(taskdata);
1248 #if KMP_ARCH_X86 || KMP_ARCH_PPC64 || !KMP_HAVE_QUAD 1249 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(
double) - 1)) == 0);
1250 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(
double) - 1)) == 0);
1252 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(_Quad) - 1)) == 0);
1253 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(_Quad) - 1)) == 0);
1255 if (sizeof_shareds > 0) {
1257 task->shareds = &((
char *)taskdata)[shareds_offset];
1259 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
1262 task->shareds = NULL;
1264 task->routine = task_entry;
1267 taskdata->td_task_id = KMP_GEN_TASK_ID();
1268 taskdata->td_team = team;
1269 taskdata->td_alloc_thread = thread;
1270 taskdata->td_parent = parent_task;
1271 taskdata->td_level = parent_task->td_level + 1;
1272 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
1273 taskdata->td_ident = loc_ref;
1274 taskdata->td_taskwait_ident = NULL;
1275 taskdata->td_taskwait_counter = 0;
1276 taskdata->td_taskwait_thread = 0;
1277 KMP_DEBUG_ASSERT(taskdata->td_parent != NULL);
1280 if (flags->proxy == TASK_FULL)
1282 copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs);
1284 taskdata->td_flags.tiedness = flags->tiedness;
1285 taskdata->td_flags.final = flags->final;
1286 taskdata->td_flags.merged_if0 = flags->merged_if0;
1288 taskdata->td_flags.destructors_thunk = flags->destructors_thunk;
1289 #endif // OMP_40_ENABLED 1291 taskdata->td_flags.proxy = flags->proxy;
1292 taskdata->td_task_team = thread->th.th_task_team;
1293 taskdata->td_size_alloc = shareds_offset + sizeof_shareds;
1295 taskdata->td_flags.tasktype = TASK_EXPLICIT;
1298 taskdata->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1301 taskdata->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1307 taskdata->td_flags.task_serial =
1308 (parent_task->td_flags.final || taskdata->td_flags.team_serial ||
1309 taskdata->td_flags.tasking_ser);
1311 taskdata->td_flags.started = 0;
1312 taskdata->td_flags.executing = 0;
1313 taskdata->td_flags.complete = 0;
1314 taskdata->td_flags.freed = 0;
1316 taskdata->td_flags.native = flags->native;
1318 KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0);
1320 KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1);
1322 taskdata->td_taskgroup =
1323 parent_task->td_taskgroup;
1324 taskdata->td_dephash = NULL;
1325 taskdata->td_depnode = NULL;
1327 if (flags->tiedness == TASK_UNTIED)
1328 taskdata->td_last_tied = NULL;
1330 taskdata->td_last_tied = taskdata;
1333 if (UNLIKELY(ompt_enabled.enabled))
1334 __ompt_task_init(taskdata, gtid);
1339 if (flags->proxy == TASK_PROXY ||
1340 !(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser))
1342 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser))
1345 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
1347 if (parent_task->td_taskgroup)
1348 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
1352 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) {
1353 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
1357 KA_TRACE(20, (
"__kmp_task_alloc(exit): T#%d created task %p parent=%p\n",
1358 gtid, taskdata, taskdata->td_parent));
1359 ANNOTATE_HAPPENS_BEFORE(task);
1364 kmp_task_t *__kmpc_omp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1365 kmp_int32 flags,
size_t sizeof_kmp_task_t,
1366 size_t sizeof_shareds,
1367 kmp_routine_entry_t task_entry) {
1369 kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
1371 input_flags->native = FALSE;
1375 KA_TRACE(10, (
"__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s) " 1376 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1377 gtid, loc_ref, input_flags->tiedness ?
"tied " :
"untied",
1378 input_flags->proxy ?
"proxy" :
"", sizeof_kmp_task_t,
1379 sizeof_shareds, task_entry));
1381 KA_TRACE(10, (
"__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s) " 1382 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1383 gtid, loc_ref, input_flags->tiedness ?
"tied " :
"untied",
1384 sizeof_kmp_task_t, sizeof_shareds, task_entry));
1387 retval = __kmp_task_alloc(loc_ref, gtid, input_flags, sizeof_kmp_task_t,
1388 sizeof_shareds, task_entry);
1390 KA_TRACE(20, (
"__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval));
1400 static void __kmp_invoke_task(kmp_int32 gtid, kmp_task_t *task,
1401 kmp_taskdata_t *current_task) {
1402 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
1408 30, (
"__kmp_invoke_task(enter): T#%d invoking task %p, current_task=%p\n",
1409 gtid, taskdata, current_task));
1410 KMP_DEBUG_ASSERT(task);
1412 if (taskdata->td_flags.proxy == TASK_PROXY &&
1413 taskdata->td_flags.complete == 1) {
1418 (
"__kmp_invoke_task: T#%d running bottom finish for proxy task %p\n",
1421 __kmp_bottom_half_finish_proxy(gtid, task);
1423 KA_TRACE(30, (
"__kmp_invoke_task(exit): T#%d completed bottom finish for " 1424 "proxy task %p, resuming task %p\n",
1425 gtid, taskdata, current_task));
1434 ompt_thread_info_t oldInfo;
1435 if (UNLIKELY(ompt_enabled.enabled)) {
1437 thread = __kmp_threads[gtid];
1438 oldInfo = thread->th.ompt_thread_info;
1439 thread->th.ompt_thread_info.wait_id = 0;
1440 thread->th.ompt_thread_info.state = (thread->th.th_team_serialized)
1441 ? omp_state_work_serial
1442 : omp_state_work_parallel;
1443 taskdata->ompt_task_info.frame.exit_frame = OMPT_GET_FRAME_ADDRESS(0);
1449 if (taskdata->td_flags.proxy != TASK_PROXY) {
1451 ANNOTATE_HAPPENS_AFTER(task);
1452 __kmp_task_start(gtid, task, current_task);
1461 if (__kmp_omp_cancellation) {
1462 thread = __kmp_threads[gtid];
1463 kmp_team_t *this_team = thread->th.th_team;
1464 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
1465 if ((taskgroup && taskgroup->cancel_request) ||
1466 (this_team->t.t_cancel_request == cancel_parallel)) {
1467 #if OMPT_SUPPORT && OMPT_OPTIONAL 1468 ompt_data_t *task_data;
1469 if (UNLIKELY(ompt_enabled.ompt_callback_cancel)) {
1470 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, NULL, NULL);
1471 ompt_callbacks.ompt_callback(ompt_callback_cancel)(
1473 ((taskgroup && taskgroup->cancel_request) ? ompt_cancel_taskgroup
1474 : ompt_cancel_parallel) |
1475 ompt_cancel_discarded_task,
1488 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
1489 taskdata->td_last_tied = current_task->td_last_tied;
1490 KMP_DEBUG_ASSERT(taskdata->td_last_tied);
1492 #if KMP_STATS_ENABLED 1494 switch (KMP_GET_THREAD_STATE()) {
1495 case FORK_JOIN_BARRIER:
1496 KMP_PUSH_PARTITIONED_TIMER(OMP_task_join_bar);
1499 KMP_PUSH_PARTITIONED_TIMER(OMP_task_plain_bar);
1502 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskyield);
1505 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskwait);
1508 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskgroup);
1511 KMP_PUSH_PARTITIONED_TIMER(OMP_task_immediate);
1514 #endif // KMP_STATS_ENABLED 1515 #endif // OMP_40_ENABLED 1519 if (UNLIKELY(ompt_enabled.enabled))
1520 __ompt_task_start(task, current_task, gtid);
1523 #if USE_ITT_BUILD && USE_ITT_NOTIFY 1524 kmp_uint64 cur_time;
1525 kmp_int32 kmp_itt_count_task =
1526 __kmp_forkjoin_frames_mode == 3 && !taskdata->td_flags.task_serial &&
1527 current_task->td_flags.tasktype == TASK_IMPLICIT;
1528 if (kmp_itt_count_task) {
1529 thread = __kmp_threads[gtid];
1531 if (thread->th.th_bar_arrive_time)
1532 cur_time = __itt_get_timestamp();
1534 kmp_itt_count_task = 0;
1538 #ifdef KMP_GOMP_COMPAT 1539 if (taskdata->td_flags.native) {
1540 ((void (*)(
void *))(*(task->routine)))(task->shareds);
1544 (*(task->routine))(gtid, task);
1546 KMP_POP_PARTITIONED_TIMER();
1548 #if USE_ITT_BUILD && USE_ITT_NOTIFY 1549 if (kmp_itt_count_task) {
1551 thread->th.th_bar_arrive_time += (__itt_get_timestamp() - cur_time);
1557 #endif // OMP_40_ENABLED 1562 if (taskdata->td_flags.proxy != TASK_PROXY) {
1564 ANNOTATE_HAPPENS_BEFORE(taskdata->td_parent);
1566 if (UNLIKELY(ompt_enabled.enabled)) {
1567 thread->th.ompt_thread_info = oldInfo;
1568 if (taskdata->td_flags.tiedness == TASK_TIED) {
1569 taskdata->ompt_task_info.frame.exit_frame = NULL;
1571 __kmp_task_finish<true>(gtid, task, current_task);
1574 __kmp_task_finish<false>(gtid, task, current_task);
1581 (
"__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n",
1582 gtid, taskdata, current_task));
1596 kmp_int32 __kmpc_omp_task_parts(
ident_t *loc_ref, kmp_int32 gtid,
1597 kmp_task_t *new_task) {
1598 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1600 KA_TRACE(10, (
"__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n", gtid,
1601 loc_ref, new_taskdata));
1604 kmp_taskdata_t *parent;
1605 if (UNLIKELY(ompt_enabled.enabled)) {
1606 parent = new_taskdata->td_parent;
1607 if (ompt_enabled.ompt_callback_task_create) {
1608 ompt_data_t task_data = ompt_data_none;
1609 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1610 parent ? &(parent->ompt_task_info.task_data) : &task_data,
1611 parent ? &(parent->ompt_task_info.frame) : NULL,
1612 &(new_taskdata->ompt_task_info.task_data), ompt_task_explicit, 0,
1613 OMPT_GET_RETURN_ADDRESS(0));
1621 if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1623 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1624 new_taskdata->td_flags.task_serial = 1;
1625 __kmp_invoke_task(gtid, new_task, current_task);
1630 (
"__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: " 1631 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
1632 gtid, loc_ref, new_taskdata));
1634 ANNOTATE_HAPPENS_BEFORE(new_task);
1636 if (UNLIKELY(ompt_enabled.enabled)) {
1637 parent->ompt_task_info.frame.enter_frame = NULL;
1640 return TASK_CURRENT_NOT_QUEUED;
1654 kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task,
1655 bool serialize_immediate) {
1656 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1661 if (new_taskdata->td_flags.proxy == TASK_PROXY ||
1662 __kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1664 if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1667 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1668 if (serialize_immediate)
1669 new_taskdata->td_flags.task_serial = 1;
1670 __kmp_invoke_task(gtid, new_task, current_task);
1673 ANNOTATE_HAPPENS_BEFORE(new_task);
1674 return TASK_CURRENT_NOT_QUEUED;
1689 kmp_int32 __kmpc_omp_task(
ident_t *loc_ref, kmp_int32 gtid,
1690 kmp_task_t *new_task) {
1692 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1694 #if KMP_DEBUG || OMPT_SUPPORT 1695 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1697 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1701 kmp_taskdata_t *parent = NULL;
1702 if (UNLIKELY(ompt_enabled.enabled)) {
1703 if (!new_taskdata->td_flags.started) {
1704 OMPT_STORE_RETURN_ADDRESS(gtid);
1705 parent = new_taskdata->td_parent;
1706 if (!parent->ompt_task_info.frame.enter_frame) {
1707 parent->ompt_task_info.frame.enter_frame = OMPT_GET_FRAME_ADDRESS(1);
1709 if (ompt_enabled.ompt_callback_task_create) {
1710 ompt_data_t task_data = ompt_data_none;
1711 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1712 parent ? &(parent->ompt_task_info.task_data) : &task_data,
1713 parent ? &(parent->ompt_task_info.frame) : NULL,
1714 &(new_taskdata->ompt_task_info.task_data),
1715 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1716 OMPT_LOAD_RETURN_ADDRESS(gtid));
1721 __ompt_task_finish(new_task,
1722 new_taskdata->ompt_task_info.scheduling_parent,
1724 new_taskdata->ompt_task_info.frame.exit_frame = NULL;
1729 res = __kmp_omp_task(gtid, new_task,
true);
1731 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning " 1732 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1733 gtid, loc_ref, new_taskdata));
1735 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
1736 parent->ompt_task_info.frame.enter_frame = NULL;
1755 kmp_int32 __kmp_omp_taskloop_task(
ident_t *loc_ref, kmp_int32 gtid,
1756 kmp_task_t *new_task,
void *codeptr_ra) {
1758 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1760 #if KMP_DEBUG || OMPT_SUPPORT 1761 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1763 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1767 kmp_taskdata_t *parent = NULL;
1768 if (UNLIKELY(ompt_enabled.enabled && !new_taskdata->td_flags.started)) {
1769 parent = new_taskdata->td_parent;
1770 if (!parent->ompt_task_info.frame.enter_frame)
1771 parent->ompt_task_info.frame.enter_frame = OMPT_GET_FRAME_ADDRESS(1);
1772 if (ompt_enabled.ompt_callback_task_create) {
1773 ompt_data_t task_data = ompt_data_none;
1774 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1775 parent ? &(parent->ompt_task_info.task_data) : &task_data,
1776 parent ? &(parent->ompt_task_info.frame) : NULL,
1777 &(new_taskdata->ompt_task_info.task_data),
1778 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1784 res = __kmp_omp_task(gtid, new_task,
true);
1786 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning " 1787 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1788 gtid, loc_ref, new_taskdata));
1790 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
1791 parent->ompt_task_info.frame.enter_frame = NULL;
1797 template <
bool ompt>
1798 static kmp_int32 __kmpc_omp_taskwait_template(
ident_t *loc_ref, kmp_int32 gtid,
1799 void *frame_address,
1800 void *return_address) {
1801 kmp_taskdata_t *taskdata;
1803 int thread_finished = FALSE;
1804 KMP_SET_THREAD_STATE_BLOCK(TASKWAIT);
1806 KA_TRACE(10, (
"__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref));
1808 if (__kmp_tasking_mode != tskm_immediate_exec) {
1809 thread = __kmp_threads[gtid];
1810 taskdata = thread->th.th_current_task;
1812 #if OMPT_SUPPORT && OMPT_OPTIONAL 1813 ompt_data_t *my_task_data;
1814 ompt_data_t *my_parallel_data;
1817 my_task_data = &(taskdata->ompt_task_info.task_data);
1818 my_parallel_data = OMPT_CUR_TEAM_DATA(thread);
1820 taskdata->ompt_task_info.frame.enter_frame = frame_address;
1822 if (ompt_enabled.ompt_callback_sync_region) {
1823 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
1824 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
1825 my_task_data, return_address);
1828 if (ompt_enabled.ompt_callback_sync_region_wait) {
1829 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
1830 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
1831 my_task_data, return_address);
1834 #endif // OMPT_SUPPORT && OMPT_OPTIONAL 1841 taskdata->td_taskwait_counter += 1;
1842 taskdata->td_taskwait_ident = loc_ref;
1843 taskdata->td_taskwait_thread = gtid + 1;
1846 void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
1847 if (itt_sync_obj != NULL)
1848 __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
1852 !taskdata->td_flags.team_serial && !taskdata->td_flags.final;
1855 must_wait = must_wait || (thread->th.th_task_team != NULL &&
1856 thread->th.th_task_team->tt.tt_found_proxy_tasks);
1859 kmp_flag_32 flag(RCAST(std::atomic<kmp_uint32> *,
1860 &(taskdata->td_incomplete_child_tasks)),
1862 while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) != 0) {
1863 flag.execute_tasks(thread, gtid, FALSE,
1864 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
1865 __kmp_task_stealing_constraint);
1869 if (itt_sync_obj != NULL)
1870 __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
1875 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
1877 #if OMPT_SUPPORT && OMPT_OPTIONAL 1879 if (ompt_enabled.ompt_callback_sync_region_wait) {
1880 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
1881 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
1882 my_task_data, return_address);
1884 if (ompt_enabled.ompt_callback_sync_region) {
1885 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
1886 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
1887 my_task_data, return_address);
1889 taskdata->ompt_task_info.frame.enter_frame = NULL;
1891 #endif // OMPT_SUPPORT && OMPT_OPTIONAL 1893 ANNOTATE_HAPPENS_AFTER(taskdata);
1896 KA_TRACE(10, (
"__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, " 1897 "returning TASK_CURRENT_NOT_QUEUED\n",
1900 return TASK_CURRENT_NOT_QUEUED;
1903 #if OMPT_SUPPORT && OMPT_OPTIONAL 1905 static kmp_int32 __kmpc_omp_taskwait_ompt(
ident_t *loc_ref, kmp_int32 gtid,
1906 void *frame_address,
1907 void *return_address) {
1908 return __kmpc_omp_taskwait_template<true>(loc_ref, gtid, frame_address,
1911 #endif // OMPT_SUPPORT && OMPT_OPTIONAL 1915 kmp_int32 __kmpc_omp_taskwait(
ident_t *loc_ref, kmp_int32 gtid) {
1916 #if OMPT_SUPPORT && OMPT_OPTIONAL 1917 if (UNLIKELY(ompt_enabled.enabled)) {
1918 OMPT_STORE_RETURN_ADDRESS(gtid);
1919 return __kmpc_omp_taskwait_ompt(loc_ref, gtid, OMPT_GET_FRAME_ADDRESS(1),
1920 OMPT_LOAD_RETURN_ADDRESS(gtid));
1923 return __kmpc_omp_taskwait_template<false>(loc_ref, gtid, NULL, NULL);
1927 kmp_int32 __kmpc_omp_taskyield(
ident_t *loc_ref, kmp_int32 gtid,
int end_part) {
1928 kmp_taskdata_t *taskdata;
1930 int thread_finished = FALSE;
1933 KMP_SET_THREAD_STATE_BLOCK(TASKYIELD);
1935 KA_TRACE(10, (
"__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n",
1936 gtid, loc_ref, end_part));
1938 if (__kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel) {
1939 thread = __kmp_threads[gtid];
1940 taskdata = thread->th.th_current_task;
1947 taskdata->td_taskwait_counter += 1;
1948 taskdata->td_taskwait_ident = loc_ref;
1949 taskdata->td_taskwait_thread = gtid + 1;
1952 void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
1953 if (itt_sync_obj != NULL)
1954 __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
1956 if (!taskdata->td_flags.team_serial) {
1957 kmp_task_team_t *task_team = thread->th.th_task_team;
1958 if (task_team != NULL) {
1959 if (KMP_TASKING_ENABLED(task_team)) {
1961 if (UNLIKELY(ompt_enabled.enabled))
1962 thread->th.ompt_thread_info.ompt_task_yielded = 1;
1964 __kmp_execute_tasks_32(
1965 thread, gtid, NULL, FALSE,
1966 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
1967 __kmp_task_stealing_constraint);
1969 if (UNLIKELY(ompt_enabled.enabled))
1970 thread->th.ompt_thread_info.ompt_task_yielded = 0;
1976 if (itt_sync_obj != NULL)
1977 __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
1982 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
1985 KA_TRACE(10, (
"__kmpc_omp_taskyield(exit): T#%d task %p resuming, " 1986 "returning TASK_CURRENT_NOT_QUEUED\n",
1989 return TASK_CURRENT_NOT_QUEUED;
1995 typedef struct kmp_task_red_flags {
1996 unsigned lazy_priv : 1;
1997 unsigned reserved31 : 31;
1998 } kmp_task_red_flags_t;
2001 typedef struct kmp_task_red_data {
2009 kmp_task_red_flags_t flags;
2010 } kmp_task_red_data_t;
2013 typedef struct kmp_task_red_input {
2019 kmp_task_red_flags_t flags;
2020 } kmp_task_red_input_t;
2031 void *__kmpc_task_reduction_init(
int gtid,
int num,
void *data) {
2032 kmp_info_t *thread = __kmp_threads[gtid];
2033 kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup;
2034 kmp_int32 nth = thread->th.th_team_nproc;
2035 kmp_task_red_input_t *input = (kmp_task_red_input_t *)data;
2036 kmp_task_red_data_t *arr;
2039 KMP_ASSERT(tg != NULL);
2040 KMP_ASSERT(data != NULL);
2041 KMP_ASSERT(num > 0);
2043 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n",
2047 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, taskgroup %p, #items %d\n",
2049 arr = (kmp_task_red_data_t *)__kmp_thread_malloc(
2050 thread, num *
sizeof(kmp_task_red_data_t));
2051 for (
int i = 0; i < num; ++i) {
2052 void (*f_init)(
void *) = (
void (*)(
void *))(input[i].reduce_init);
2053 size_t size = input[i].reduce_size - 1;
2055 size += CACHE_LINE - size % CACHE_LINE;
2056 KMP_ASSERT(input[i].reduce_comb != NULL);
2057 arr[i].reduce_shar = input[i].reduce_shar;
2058 arr[i].reduce_size = size;
2059 arr[i].reduce_init = input[i].reduce_init;
2060 arr[i].reduce_fini = input[i].reduce_fini;
2061 arr[i].reduce_comb = input[i].reduce_comb;
2062 arr[i].flags = input[i].flags;
2063 if (!input[i].flags.lazy_priv) {
2065 arr[i].reduce_priv = __kmp_allocate(nth * size);
2066 arr[i].reduce_pend = (
char *)(arr[i].reduce_priv) + nth * size;
2067 if (f_init != NULL) {
2069 for (
int j = 0; j < nth; ++j) {
2070 f_init((
char *)(arr[i].reduce_priv) + j * size);
2076 arr[i].reduce_priv = __kmp_allocate(nth *
sizeof(
void *));
2079 tg->reduce_data = (
void *)arr;
2080 tg->reduce_num_data = num;
2093 void *__kmpc_task_reduction_get_th_data(
int gtid,
void *tskgrp,
void *data) {
2094 kmp_info_t *thread = __kmp_threads[gtid];
2095 kmp_int32 nth = thread->th.th_team_nproc;
2099 kmp_taskgroup_t *tg = (kmp_taskgroup_t *)tskgrp;
2101 tg = thread->th.th_current_task->td_taskgroup;
2102 KMP_ASSERT(tg != NULL);
2103 kmp_task_red_data_t *arr = (kmp_task_red_data_t *)(tg->reduce_data);
2104 kmp_int32 num = tg->reduce_num_data;
2105 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
2107 KMP_ASSERT(data != NULL);
2108 while (tg != NULL) {
2109 for (
int i = 0; i < num; ++i) {
2110 if (!arr[i].flags.lazy_priv) {
2111 if (data == arr[i].reduce_shar ||
2112 (data >= arr[i].reduce_priv && data < arr[i].reduce_pend))
2113 return (
char *)(arr[i].reduce_priv) + tid * arr[i].reduce_size;
2116 void **p_priv = (
void **)(arr[i].reduce_priv);
2117 if (data == arr[i].reduce_shar)
2120 for (
int j = 0; j < nth; ++j)
2121 if (data == p_priv[j])
2125 if (p_priv[tid] == NULL) {
2127 void (*f_init)(
void *) = (
void (*)(
void *))(arr[i].reduce_init);
2128 p_priv[tid] = __kmp_allocate(arr[i].reduce_size);
2129 if (f_init != NULL) {
2130 f_init(p_priv[tid]);
2137 arr = (kmp_task_red_data_t *)(tg->reduce_data);
2138 num = tg->reduce_num_data;
2140 KMP_ASSERT2(0,
"Unknown task reduction item");
2146 static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg) {
2147 kmp_int32 nth = th->th.th_team_nproc;
2148 KMP_DEBUG_ASSERT(nth > 1);
2149 kmp_task_red_data_t *arr = (kmp_task_red_data_t *)tg->reduce_data;
2150 kmp_int32 num = tg->reduce_num_data;
2151 for (
int i = 0; i < num; ++i) {
2152 void *sh_data = arr[i].reduce_shar;
2153 void (*f_fini)(
void *) = (
void (*)(
void *))(arr[i].reduce_fini);
2154 void (*f_comb)(
void *,
void *) =
2155 (
void (*)(
void *,
void *))(arr[i].reduce_comb);
2156 if (!arr[i].flags.lazy_priv) {
2157 void *pr_data = arr[i].reduce_priv;
2158 size_t size = arr[i].reduce_size;
2159 for (
int j = 0; j < nth; ++j) {
2160 void *priv_data = (
char *)pr_data + j * size;
2161 f_comb(sh_data, priv_data);
2166 void **pr_data = (
void **)(arr[i].reduce_priv);
2167 for (
int j = 0; j < nth; ++j) {
2168 if (pr_data[j] != NULL) {
2169 f_comb(sh_data, pr_data[j]);
2172 __kmp_free(pr_data[j]);
2176 __kmp_free(arr[i].reduce_priv);
2178 __kmp_thread_free(th, arr);
2179 tg->reduce_data = NULL;
2180 tg->reduce_num_data = 0;
2186 void __kmpc_taskgroup(
ident_t *loc,
int gtid) {
2187 kmp_info_t *thread = __kmp_threads[gtid];
2188 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2189 kmp_taskgroup_t *tg_new =
2190 (kmp_taskgroup_t *)__kmp_thread_malloc(thread,
sizeof(kmp_taskgroup_t));
2191 KA_TRACE(10, (
"__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new));
2192 KMP_ATOMIC_ST_RLX(&tg_new->count, 0);
2193 KMP_ATOMIC_ST_RLX(&tg_new->cancel_request, cancel_noreq);
2194 tg_new->parent = taskdata->td_taskgroup;
2196 tg_new->reduce_data = NULL;
2197 tg_new->reduce_num_data = 0;
2199 taskdata->td_taskgroup = tg_new;
2201 #if OMPT_SUPPORT && OMPT_OPTIONAL 2202 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2203 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2205 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2206 kmp_team_t *team = thread->th.th_team;
2207 ompt_data_t my_task_data = taskdata->ompt_task_info.task_data;
2209 ompt_data_t my_parallel_data = team->t.ompt_team_info.parallel_data;
2211 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2212 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2213 &(my_task_data), codeptr);
2220 void __kmpc_end_taskgroup(
ident_t *loc,
int gtid) {
2221 kmp_info_t *thread = __kmp_threads[gtid];
2222 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2223 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
2224 int thread_finished = FALSE;
2226 #if OMPT_SUPPORT && OMPT_OPTIONAL 2228 ompt_data_t my_task_data;
2229 ompt_data_t my_parallel_data;
2231 if (UNLIKELY(ompt_enabled.enabled)) {
2232 team = thread->th.th_team;
2233 my_task_data = taskdata->ompt_task_info.task_data;
2235 my_parallel_data = team->t.ompt_team_info.parallel_data;
2236 codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2238 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2242 KA_TRACE(10, (
"__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc));
2243 KMP_DEBUG_ASSERT(taskgroup != NULL);
2244 KMP_SET_THREAD_STATE_BLOCK(TASKGROUP);
2246 if (__kmp_tasking_mode != tskm_immediate_exec) {
2248 taskdata->td_taskwait_counter += 1;
2249 taskdata->td_taskwait_ident = loc;
2250 taskdata->td_taskwait_thread = gtid + 1;
2254 void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
2255 if (itt_sync_obj != NULL)
2256 __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
2259 #if OMPT_SUPPORT && OMPT_OPTIONAL 2260 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2261 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2262 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2263 &(my_task_data), codeptr);
2268 if (!taskdata->td_flags.team_serial ||
2269 (thread->th.th_task_team != NULL &&
2270 thread->th.th_task_team->tt.tt_found_proxy_tasks))
2272 if (!taskdata->td_flags.team_serial)
2275 kmp_flag_32 flag(RCAST(std::atomic<kmp_uint32> *, &(taskgroup->count)),
2277 while (KMP_ATOMIC_LD_ACQ(&taskgroup->count) != 0) {
2278 flag.execute_tasks(thread, gtid, FALSE,
2279 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2280 __kmp_task_stealing_constraint);
2283 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2285 #if OMPT_SUPPORT && OMPT_OPTIONAL 2286 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2287 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2288 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2289 &(my_task_data), codeptr);
2294 if (itt_sync_obj != NULL)
2295 __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
2298 KMP_DEBUG_ASSERT(taskgroup->count == 0);
2301 if (taskgroup->reduce_data != NULL)
2302 __kmp_task_reduction_fini(thread, taskgroup);
2305 taskdata->td_taskgroup = taskgroup->parent;
2306 __kmp_thread_free(thread, taskgroup);
2308 KA_TRACE(10, (
"__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n",
2310 ANNOTATE_HAPPENS_AFTER(taskdata);
2312 #if OMPT_SUPPORT && OMPT_OPTIONAL 2313 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2314 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2315 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2316 &(my_task_data), codeptr);
2323 static kmp_task_t *__kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid,
2324 kmp_task_team_t *task_team,
2325 kmp_int32 is_constrained) {
2327 kmp_taskdata_t *taskdata;
2328 kmp_thread_data_t *thread_data;
2331 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2332 KMP_DEBUG_ASSERT(task_team->tt.tt_threads_data !=
2335 thread_data = &task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
2337 KA_TRACE(10, (
"__kmp_remove_my_task(enter): T#%d ntasks=%d head=%u tail=%u\n",
2338 gtid, thread_data->td.td_deque_ntasks,
2339 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2341 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2343 (
"__kmp_remove_my_task(exit #1): T#%d No tasks to remove: " 2344 "ntasks=%d head=%u tail=%u\n",
2345 gtid, thread_data->td.td_deque_ntasks,
2346 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2350 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
2352 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2353 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2355 (
"__kmp_remove_my_task(exit #2): T#%d No tasks to remove: " 2356 "ntasks=%d head=%u tail=%u\n",
2357 gtid, thread_data->td.td_deque_ntasks,
2358 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2362 tail = (thread_data->td.td_deque_tail - 1) &
2363 TASK_DEQUE_MASK(thread_data->td);
2364 taskdata = thread_data->td.td_deque[tail];
2366 if (!__kmp_task_is_allowed(gtid, is_constrained, taskdata,
2367 thread->th.th_current_task)) {
2369 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2371 (
"__kmp_remove_my_task(exit #3): T#%d TSC blocks tail task: " 2372 "ntasks=%d head=%u tail=%u\n",
2373 gtid, thread_data->td.td_deque_ntasks,
2374 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2378 thread_data->td.td_deque_tail = tail;
2379 TCW_4(thread_data->td.td_deque_ntasks, thread_data->td.td_deque_ntasks - 1);
2381 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2383 KA_TRACE(10, (
"__kmp_remove_my_task(exit #4): T#%d task %p removed: " 2384 "ntasks=%d head=%u tail=%u\n",
2385 gtid, taskdata, thread_data->td.td_deque_ntasks,
2386 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2388 task = KMP_TASKDATA_TO_TASK(taskdata);
2395 static kmp_task_t *__kmp_steal_task(kmp_info_t *victim_thr, kmp_int32 gtid,
2396 kmp_task_team_t *task_team,
2397 std::atomic<kmp_int32> *unfinished_threads,
2398 int *thread_finished,
2399 kmp_int32 is_constrained) {
2401 kmp_taskdata_t *taskdata;
2402 kmp_taskdata_t *current;
2403 kmp_thread_data_t *victim_td, *threads_data;
2405 kmp_int32 victim_tid;
2407 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2409 threads_data = task_team->tt.tt_threads_data;
2410 KMP_DEBUG_ASSERT(threads_data != NULL);
2412 victim_tid = victim_thr->th.th_info.ds.ds_tid;
2413 victim_td = &threads_data[victim_tid];
2415 KA_TRACE(10, (
"__kmp_steal_task(enter): T#%d try to steal from T#%d: " 2416 "task_team=%p ntasks=%d head=%u tail=%u\n",
2417 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
2418 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
2419 victim_td->td.td_deque_tail));
2421 if (TCR_4(victim_td->td.td_deque_ntasks) == 0) {
2422 KA_TRACE(10, (
"__kmp_steal_task(exit #1): T#%d could not steal from T#%d: " 2423 "task_team=%p ntasks=%d head=%u tail=%u\n",
2424 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
2425 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
2426 victim_td->td.td_deque_tail));
2430 __kmp_acquire_bootstrap_lock(&victim_td->td.td_deque_lock);
2432 int ntasks = TCR_4(victim_td->td.td_deque_ntasks);
2435 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2436 KA_TRACE(10, (
"__kmp_steal_task(exit #2): T#%d could not steal from T#%d: " 2437 "task_team=%p ntasks=%d head=%u tail=%u\n",
2438 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2439 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2443 KMP_DEBUG_ASSERT(victim_td->td.td_deque != NULL);
2444 current = __kmp_threads[gtid]->th.th_current_task;
2445 taskdata = victim_td->td.td_deque[victim_td->td.td_deque_head];
2446 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
2448 victim_td->td.td_deque_head =
2449 (victim_td->td.td_deque_head + 1) & TASK_DEQUE_MASK(victim_td->td);
2451 if (!task_team->tt.tt_untied_task_encountered) {
2453 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2454 KA_TRACE(10, (
"__kmp_steal_task(exit #3): T#%d could not steal from " 2455 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
2456 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2457 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2462 target = victim_td->td.td_deque_head;
2464 for (i = 1; i < ntasks; ++i) {
2465 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
2466 taskdata = victim_td->td.td_deque[target];
2467 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
2473 if (taskdata == NULL) {
2475 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2476 KA_TRACE(10, (
"__kmp_steal_task(exit #4): T#%d could not steal from " 2477 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
2478 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2479 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2483 for (i = i + 1; i < ntasks; ++i) {
2485 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
2486 victim_td->td.td_deque[prev] = victim_td->td.td_deque[target];
2490 victim_td->td.td_deque_tail ==
2491 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(victim_td->td)));
2492 victim_td->td.td_deque_tail = target;
2494 if (*thread_finished) {
2500 count = KMP_ATOMIC_INC(unfinished_threads);
2504 (
"__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n",
2505 gtid, count + 1, task_team));
2507 *thread_finished = FALSE;
2509 TCW_4(victim_td->td.td_deque_ntasks, ntasks - 1);
2511 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2515 (
"__kmp_steal_task(exit #5): T#%d stole task %p from T#%d: " 2516 "task_team=%p ntasks=%d head=%u tail=%u\n",
2517 gtid, taskdata, __kmp_gtid_from_thread(victim_thr), task_team,
2518 ntasks, victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2520 task = KMP_TASKDATA_TO_TASK(taskdata);
2534 static inline int __kmp_execute_tasks_template(
2535 kmp_info_t *thread, kmp_int32 gtid, C *flag,
int final_spin,
2536 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
2537 kmp_int32 is_constrained) {
2538 kmp_task_team_t *task_team = thread->th.th_task_team;
2539 kmp_thread_data_t *threads_data;
2541 kmp_info_t *other_thread;
2542 kmp_taskdata_t *current_task = thread->th.th_current_task;
2543 std::atomic<kmp_int32> *unfinished_threads;
2544 kmp_int32 nthreads, victim_tid = -2, use_own_tasks = 1, new_victim = 0,
2545 tid = thread->th.th_info.ds.ds_tid;
2547 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2548 KMP_DEBUG_ASSERT(thread == __kmp_threads[gtid]);
2550 if (task_team == NULL || current_task == NULL)
2553 KA_TRACE(15, (
"__kmp_execute_tasks_template(enter): T#%d final_spin=%d " 2554 "*thread_finished=%d\n",
2555 gtid, final_spin, *thread_finished));
2557 thread->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
2558 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
2559 KMP_DEBUG_ASSERT(threads_data != NULL);
2561 nthreads = task_team->tt.tt_nproc;
2562 unfinished_threads = &(task_team->tt.tt_unfinished_threads);
2564 KMP_DEBUG_ASSERT(nthreads > 1 || task_team->tt.tt_found_proxy_tasks);
2566 KMP_DEBUG_ASSERT(nthreads > 1);
2568 KMP_DEBUG_ASSERT(*unfinished_threads >= 0);
2574 if (use_own_tasks) {
2575 task = __kmp_remove_my_task(thread, gtid, task_team, is_constrained);
2577 if ((task == NULL) && (nthreads > 1)) {
2581 if (victim_tid == -2) {
2582 victim_tid = threads_data[tid].td.td_deque_last_stolen;
2585 other_thread = threads_data[victim_tid].td.td_thr;
2587 if (victim_tid != -1) {
2589 }
else if (!new_victim) {
2595 victim_tid = __kmp_get_random(thread) % (nthreads - 1);
2596 if (victim_tid >= tid) {
2600 other_thread = threads_data[victim_tid].td.td_thr;
2610 if ((__kmp_tasking_mode == tskm_task_teams) &&
2611 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) &&
2612 (TCR_PTR(CCAST(
void *, other_thread->th.th_sleep_loc)) !=
2615 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(other_thread),
2616 other_thread->th.th_sleep_loc);
2629 task = __kmp_steal_task(other_thread, gtid, task_team,
2630 unfinished_threads, thread_finished,
2634 if (threads_data[tid].td.td_deque_last_stolen != victim_tid) {
2635 threads_data[tid].td.td_deque_last_stolen = victim_tid;
2642 KMP_CHECK_UPDATE(threads_data[tid].td.td_deque_last_stolen, -1);
2651 #if USE_ITT_BUILD && USE_ITT_NOTIFY 2652 if (__itt_sync_create_ptr || KMP_ITT_DEBUG) {
2653 if (itt_sync_obj == NULL) {
2655 itt_sync_obj = __kmp_itt_barrier_object(gtid, bs_forkjoin_barrier);
2657 __kmp_itt_task_starting(itt_sync_obj);
2660 __kmp_invoke_task(gtid, task, current_task);
2662 if (itt_sync_obj != NULL)
2663 __kmp_itt_task_finished(itt_sync_obj);
2670 if (flag == NULL || (!final_spin && flag->done_check())) {
2673 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
2677 if (thread->th.th_task_team == NULL) {
2681 KMP_YIELD(__kmp_library == library_throughput);
2684 if (!use_own_tasks && TCR_4(threads_data[tid].td.td_deque_ntasks) != 0) {
2685 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d stolen task spawned " 2686 "other tasks, restart\n",
2699 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks) == 0)
2707 if (!*thread_finished) {
2710 count = KMP_ATOMIC_DEC(unfinished_threads) - 1;
2711 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d dec " 2712 "unfinished_threads to %d task_team=%p\n",
2713 gtid, count, task_team));
2714 *thread_finished = TRUE;
2722 if (flag != NULL && flag->done_check()) {
2725 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
2733 if (thread->th.th_task_team == NULL) {
2735 (
"__kmp_execute_tasks_template: T#%d no more tasks\n", gtid));
2748 (
"__kmp_execute_tasks_template: T#%d can't find work\n", gtid));
2754 int __kmp_execute_tasks_32(
2755 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32 *flag,
int final_spin,
2756 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
2757 kmp_int32 is_constrained) {
2758 return __kmp_execute_tasks_template(
2759 thread, gtid, flag, final_spin,
2760 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
2763 int __kmp_execute_tasks_64(
2764 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64 *flag,
int final_spin,
2765 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
2766 kmp_int32 is_constrained) {
2767 return __kmp_execute_tasks_template(
2768 thread, gtid, flag, final_spin,
2769 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
2772 int __kmp_execute_tasks_oncore(
2773 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag,
int final_spin,
2774 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
2775 kmp_int32 is_constrained) {
2776 return __kmp_execute_tasks_template(
2777 thread, gtid, flag, final_spin,
2778 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
2784 static void __kmp_enable_tasking(kmp_task_team_t *task_team,
2785 kmp_info_t *this_thr) {
2786 kmp_thread_data_t *threads_data;
2787 int nthreads, i, is_init_thread;
2789 KA_TRACE(10, (
"__kmp_enable_tasking(enter): T#%d\n",
2790 __kmp_gtid_from_thread(this_thr)));
2792 KMP_DEBUG_ASSERT(task_team != NULL);
2793 KMP_DEBUG_ASSERT(this_thr->th.th_team != NULL);
2795 nthreads = task_team->tt.tt_nproc;
2796 KMP_DEBUG_ASSERT(nthreads > 0);
2797 KMP_DEBUG_ASSERT(nthreads == this_thr->th.th_team->t.t_nproc);
2800 is_init_thread = __kmp_realloc_task_threads_data(this_thr, task_team);
2802 if (!is_init_thread) {
2806 (
"__kmp_enable_tasking(exit): T#%d: threads array already set up.\n",
2807 __kmp_gtid_from_thread(this_thr)));
2810 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
2811 KMP_DEBUG_ASSERT(threads_data != NULL);
2813 if ((__kmp_tasking_mode == tskm_task_teams) &&
2814 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME)) {
2818 for (i = 0; i < nthreads; i++) {
2819 volatile void *sleep_loc;
2820 kmp_info_t *thread = threads_data[i].td.td_thr;
2822 if (i == this_thr->th.th_info.ds.ds_tid) {
2831 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
2833 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d waking up thread T#%d\n",
2834 __kmp_gtid_from_thread(this_thr),
2835 __kmp_gtid_from_thread(thread)));
2836 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc);
2838 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d don't wake up thread T#%d\n",
2839 __kmp_gtid_from_thread(this_thr),
2840 __kmp_gtid_from_thread(thread)));
2845 KA_TRACE(10, (
"__kmp_enable_tasking(exit): T#%d\n",
2846 __kmp_gtid_from_thread(this_thr)));
2883 static kmp_task_team_t *__kmp_free_task_teams =
2886 kmp_bootstrap_lock_t __kmp_task_team_lock =
2887 KMP_BOOTSTRAP_LOCK_INITIALIZER(__kmp_task_team_lock);
2894 static void __kmp_alloc_task_deque(kmp_info_t *thread,
2895 kmp_thread_data_t *thread_data) {
2896 __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
2897 KMP_DEBUG_ASSERT(thread_data->td.td_deque == NULL);
2900 thread_data->td.td_deque_last_stolen = -1;
2902 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == 0);
2903 KMP_DEBUG_ASSERT(thread_data->td.td_deque_head == 0);
2904 KMP_DEBUG_ASSERT(thread_data->td.td_deque_tail == 0);
2908 (
"__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n",
2909 __kmp_gtid_from_thread(thread), INITIAL_TASK_DEQUE_SIZE, thread_data));
2913 thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
2914 INITIAL_TASK_DEQUE_SIZE *
sizeof(kmp_taskdata_t *));
2915 thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
2921 static void __kmp_free_task_deque(kmp_thread_data_t *thread_data) {
2922 if (thread_data->td.td_deque != NULL) {
2923 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
2924 TCW_4(thread_data->td.td_deque_ntasks, 0);
2925 __kmp_free(thread_data->td.td_deque);
2926 thread_data->td.td_deque = NULL;
2927 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2930 #ifdef BUILD_TIED_TASK_STACK 2932 if (thread_data->td.td_susp_tied_tasks.ts_entries != TASK_STACK_EMPTY) {
2933 __kmp_free_task_stack(__kmp_thread_from_gtid(gtid), thread_data);
2935 #endif // BUILD_TIED_TASK_STACK 2945 static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
2946 kmp_task_team_t *task_team) {
2947 kmp_thread_data_t **threads_data_p;
2948 kmp_int32 nthreads, maxthreads;
2949 int is_init_thread = FALSE;
2951 if (TCR_4(task_team->tt.tt_found_tasks)) {
2956 threads_data_p = &task_team->tt.tt_threads_data;
2957 nthreads = task_team->tt.tt_nproc;
2958 maxthreads = task_team->tt.tt_max_threads;
2963 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
2965 if (!TCR_4(task_team->tt.tt_found_tasks)) {
2967 kmp_team_t *team = thread->th.th_team;
2970 is_init_thread = TRUE;
2971 if (maxthreads < nthreads) {
2973 if (*threads_data_p != NULL) {
2974 kmp_thread_data_t *old_data = *threads_data_p;
2975 kmp_thread_data_t *new_data = NULL;
2979 (
"__kmp_realloc_task_threads_data: T#%d reallocating " 2980 "threads data for task_team %p, new_size = %d, old_size = %d\n",
2981 __kmp_gtid_from_thread(thread), task_team, nthreads, maxthreads));
2986 new_data = (kmp_thread_data_t *)__kmp_allocate(
2987 nthreads *
sizeof(kmp_thread_data_t));
2989 KMP_MEMCPY_S((
void *)new_data, nthreads *
sizeof(kmp_thread_data_t),
2990 (
void *)old_data, maxthreads *
sizeof(kmp_thread_data_t));
2992 #ifdef BUILD_TIED_TASK_STACK 2994 for (i = maxthreads; i < nthreads; i++) {
2995 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
2996 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
2998 #endif // BUILD_TIED_TASK_STACK 3000 (*threads_data_p) = new_data;
3001 __kmp_free(old_data);
3003 KE_TRACE(10, (
"__kmp_realloc_task_threads_data: T#%d allocating " 3004 "threads data for task_team %p, size = %d\n",
3005 __kmp_gtid_from_thread(thread), task_team, nthreads));
3009 ANNOTATE_IGNORE_WRITES_BEGIN();
3010 *threads_data_p = (kmp_thread_data_t *)__kmp_allocate(
3011 nthreads *
sizeof(kmp_thread_data_t));
3012 ANNOTATE_IGNORE_WRITES_END();
3013 #ifdef BUILD_TIED_TASK_STACK 3015 for (i = 0; i < nthreads; i++) {
3016 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3017 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3019 #endif // BUILD_TIED_TASK_STACK 3021 task_team->tt.tt_max_threads = nthreads;
3024 KMP_DEBUG_ASSERT(*threads_data_p != NULL);
3028 for (i = 0; i < nthreads; i++) {
3029 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3030 thread_data->td.td_thr = team->t.t_threads[i];
3032 if (thread_data->td.td_deque_last_stolen >= nthreads) {
3036 thread_data->td.td_deque_last_stolen = -1;
3041 TCW_SYNC_4(task_team->tt.tt_found_tasks, TRUE);
3044 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3045 return is_init_thread;
3051 static void __kmp_free_task_threads_data(kmp_task_team_t *task_team) {
3052 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3053 if (task_team->tt.tt_threads_data != NULL) {
3055 for (i = 0; i < task_team->tt.tt_max_threads; i++) {
3056 __kmp_free_task_deque(&task_team->tt.tt_threads_data[i]);
3058 __kmp_free(task_team->tt.tt_threads_data);
3059 task_team->tt.tt_threads_data = NULL;
3061 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3068 static kmp_task_team_t *__kmp_allocate_task_team(kmp_info_t *thread,
3070 kmp_task_team_t *task_team = NULL;
3073 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d entering; team = %p\n",
3074 (thread ? __kmp_gtid_from_thread(thread) : -1), team));
3076 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3078 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3079 if (__kmp_free_task_teams != NULL) {
3080 task_team = __kmp_free_task_teams;
3081 TCW_PTR(__kmp_free_task_teams, task_team->tt.tt_next);
3082 task_team->tt.tt_next = NULL;
3084 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3087 if (task_team == NULL) {
3088 KE_TRACE(10, (
"__kmp_allocate_task_team: T#%d allocating " 3089 "task team for team %p\n",
3090 __kmp_gtid_from_thread(thread), team));
3094 task_team = (kmp_task_team_t *)__kmp_allocate(
sizeof(kmp_task_team_t));
3095 __kmp_init_bootstrap_lock(&task_team->tt.tt_threads_lock);
3102 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3104 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3106 task_team->tt.tt_nproc = nthreads = team->t.t_nproc;
3108 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads, nthreads);
3109 TCW_4(task_team->tt.tt_active, TRUE);
3111 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d exiting; task_team = %p " 3112 "unfinished_threads init'd to %d\n",
3113 (thread ? __kmp_gtid_from_thread(thread) : -1), task_team,
3114 KMP_ATOMIC_LD_RLX(&task_team->tt.tt_unfinished_threads)));
3121 void __kmp_free_task_team(kmp_info_t *thread, kmp_task_team_t *task_team) {
3122 KA_TRACE(20, (
"__kmp_free_task_team: T#%d task_team = %p\n",
3123 thread ? __kmp_gtid_from_thread(thread) : -1, task_team));
3126 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3128 KMP_DEBUG_ASSERT(task_team->tt.tt_next == NULL);
3129 task_team->tt.tt_next = __kmp_free_task_teams;
3130 TCW_PTR(__kmp_free_task_teams, task_team);
3132 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3140 void __kmp_reap_task_teams(
void) {
3141 kmp_task_team_t *task_team;
3143 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3145 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3146 while ((task_team = __kmp_free_task_teams) != NULL) {
3147 __kmp_free_task_teams = task_team->tt.tt_next;
3148 task_team->tt.tt_next = NULL;
3151 if (task_team->tt.tt_threads_data != NULL) {
3152 __kmp_free_task_threads_data(task_team);
3154 __kmp_free(task_team);
3156 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3163 void __kmp_wait_to_unref_task_teams(
void) {
3168 KMP_INIT_YIELD(spins);
3176 for (thread = CCAST(kmp_info_t *, __kmp_thread_pool); thread != NULL;
3177 thread = thread->th.th_next_pool) {
3181 if (TCR_PTR(thread->th.th_task_team) == NULL) {
3182 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n",
3183 __kmp_gtid_from_thread(thread)));
3188 if (!__kmp_is_thread_alive(thread, &exit_val)) {
3189 thread->th.th_task_team = NULL;
3196 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: Waiting for T#%d to " 3197 "unreference task_team\n",
3198 __kmp_gtid_from_thread(thread)));
3200 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
3201 volatile void *sleep_loc;
3203 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3207 (
"__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n",
3208 __kmp_gtid_from_thread(thread), __kmp_gtid_from_thread(thread)));
3209 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc);
3219 KMP_YIELD(TCR_4(__kmp_nth) > __kmp_avail_proc);
3220 KMP_YIELD_SPIN(spins);
3226 void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team,
int always) {
3227 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3233 if (team->t.t_task_team[this_thr->th.th_task_state] == NULL &&
3234 (always || team->t.t_nproc > 1)) {
3235 team->t.t_task_team[this_thr->th.th_task_state] =
3236 __kmp_allocate_task_team(this_thr, team);
3237 KA_TRACE(20, (
"__kmp_task_team_setup: Master T#%d created new task_team %p " 3238 "for team %d at parity=%d\n",
3239 __kmp_gtid_from_thread(this_thr),
3240 team->t.t_task_team[this_thr->th.th_task_state],
3241 ((team != NULL) ? team->t.t_id : -1),
3242 this_thr->th.th_task_state));
3252 if (team->t.t_nproc > 1) {
3253 int other_team = 1 - this_thr->th.th_task_state;
3254 if (team->t.t_task_team[other_team] == NULL) {
3255 team->t.t_task_team[other_team] =
3256 __kmp_allocate_task_team(this_thr, team);
3257 KA_TRACE(20, (
"__kmp_task_team_setup: Master T#%d created second new " 3258 "task_team %p for team %d at parity=%d\n",
3259 __kmp_gtid_from_thread(this_thr),
3260 team->t.t_task_team[other_team],
3261 ((team != NULL) ? team->t.t_id : -1), other_team));
3264 kmp_task_team_t *task_team = team->t.t_task_team[other_team];
3265 if (!task_team->tt.tt_active ||
3266 team->t.t_nproc != task_team->tt.tt_nproc) {
3267 TCW_4(task_team->tt.tt_nproc, team->t.t_nproc);
3268 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3270 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3272 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads,
3274 TCW_4(task_team->tt.tt_active, TRUE);
3278 KA_TRACE(20, (
"__kmp_task_team_setup: Master T#%d reset next task_team " 3279 "%p for team %d at parity=%d\n",
3280 __kmp_gtid_from_thread(this_thr),
3281 team->t.t_task_team[other_team],
3282 ((team != NULL) ? team->t.t_id : -1), other_team));
3290 void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team) {
3291 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3295 this_thr->th.th_task_state = 1 - this_thr->th.th_task_state;
3298 TCW_PTR(this_thr->th.th_task_team,
3299 team->t.t_task_team[this_thr->th.th_task_state]);
3301 (
"__kmp_task_team_sync: Thread T#%d task team switched to task_team " 3302 "%p from Team #%d (parity=%d)\n",
3303 __kmp_gtid_from_thread(this_thr), this_thr->th.th_task_team,
3304 ((team != NULL) ? team->t.t_id : -1), this_thr->th.th_task_state));
3314 void __kmp_task_team_wait(
3315 kmp_info_t *this_thr,
3316 kmp_team_t *team USE_ITT_BUILD_ARG(
void *itt_sync_obj),
int wait) {
3317 kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state];
3319 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3320 KMP_DEBUG_ASSERT(task_team == this_thr->th.th_task_team);
3322 if ((task_team != NULL) && KMP_TASKING_ENABLED(task_team)) {
3324 KA_TRACE(20, (
"__kmp_task_team_wait: Master T#%d waiting for all tasks " 3325 "(for unfinished_threads to reach 0) on task_team = %p\n",
3326 __kmp_gtid_from_thread(this_thr), task_team));
3330 kmp_flag_32 flag(RCAST(std::atomic<kmp_uint32> *,
3331 &task_team->tt.tt_unfinished_threads),
3333 flag.wait(this_thr, TRUE USE_ITT_BUILD_ARG(itt_sync_obj));
3339 (
"__kmp_task_team_wait: Master T#%d deactivating task_team %p: " 3340 "setting active to false, setting local and team's pointer to NULL\n",
3341 __kmp_gtid_from_thread(this_thr), task_team));
3343 KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1 ||
3344 task_team->tt.tt_found_proxy_tasks == TRUE);
3345 TCW_SYNC_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3347 KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1);
3349 KMP_CHECK_UPDATE(task_team->tt.tt_untied_task_encountered, 0);
3350 TCW_SYNC_4(task_team->tt.tt_active, FALSE);
3353 TCW_PTR(this_thr->th.th_task_team, NULL);
3362 void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread,
int gtid) {
3363 std::atomic<kmp_uint32> *spin = RCAST(
3364 std::atomic<kmp_uint32> *,
3365 &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads);
3367 KMP_DEBUG_ASSERT(__kmp_tasking_mode == tskm_extra_barrier);
3370 KMP_FSYNC_SPIN_INIT(spin, NULL);
3372 kmp_flag_32 spin_flag(spin, 0U);
3373 while (!spin_flag.execute_tasks(thread, gtid, TRUE,
3374 &flag USE_ITT_BUILD_ARG(NULL), 0)) {
3377 KMP_FSYNC_SPIN_PREPARE(RCAST(
void *, spin));
3380 if (TCR_4(__kmp_global.g.g_done)) {
3381 if (__kmp_global.g.g_abort)
3382 __kmp_abort_thread();
3388 KMP_FSYNC_SPIN_ACQUIRED(RCAST(
void *, spin));
3399 static bool __kmp_give_task(kmp_info_t *thread, kmp_int32 tid, kmp_task_t *task,
3401 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
3402 kmp_task_team_t *task_team = taskdata->td_task_team;
3404 KA_TRACE(20, (
"__kmp_give_task: trying to give task %p to thread %d.\n",
3408 KMP_DEBUG_ASSERT(task_team != NULL);
3410 bool result =
false;
3411 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
3413 if (thread_data->td.td_deque == NULL) {
3417 (
"__kmp_give_task: thread %d has no queue while giving task %p.\n",
3422 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3423 TASK_DEQUE_SIZE(thread_data->td)) {
3426 (
"__kmp_give_task: queue is full while giving task %p to thread %d.\n",
3431 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
3434 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3435 __kmp_realloc_task_deque(thread, thread_data);
3439 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3441 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3442 TASK_DEQUE_SIZE(thread_data->td)) {
3443 KA_TRACE(30, (
"__kmp_give_task: queue is full while giving task %p to " 3449 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
3450 goto release_and_exit;
3452 __kmp_realloc_task_deque(thread, thread_data);
3458 thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
3460 thread_data->td.td_deque_tail =
3461 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
3462 TCW_4(thread_data->td.td_deque_ntasks,
3463 TCR_4(thread_data->td.td_deque_ntasks) + 1);
3466 KA_TRACE(30, (
"__kmp_give_task: successfully gave task %p to thread %d.\n",
3470 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3491 static void __kmp_first_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
3492 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
3493 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3494 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
3495 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
3497 taskdata->td_flags.complete = 1;
3499 if (taskdata->td_taskgroup)
3500 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
3504 KMP_ATOMIC_INC(&taskdata->td_incomplete_child_tasks);
3507 static void __kmp_second_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
3508 kmp_int32 children = 0;
3512 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks) - 1;
3513 KMP_DEBUG_ASSERT(children >= 0);
3516 KMP_ATOMIC_DEC(&taskdata->td_incomplete_child_tasks);
3519 static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask) {
3520 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3521 kmp_info_t *thread = __kmp_threads[gtid];
3523 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3524 KMP_DEBUG_ASSERT(taskdata->td_flags.complete ==
3529 while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) > 0)
3532 __kmp_release_deps(gtid, taskdata);
3533 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
3544 void __kmpc_proxy_task_completed(kmp_int32 gtid, kmp_task_t *ptask) {
3545 KMP_DEBUG_ASSERT(ptask != NULL);
3546 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3548 10, (
"__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n",
3551 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3553 __kmp_first_top_half_finish_proxy(taskdata);
3554 __kmp_second_top_half_finish_proxy(taskdata);
3555 __kmp_bottom_half_finish_proxy(gtid, ptask);
3558 (
"__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n",
3569 void __kmpc_proxy_task_completed_ooo(kmp_task_t *ptask) {
3570 KMP_DEBUG_ASSERT(ptask != NULL);
3571 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3575 (
"__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n",
3578 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3580 __kmp_first_top_half_finish_proxy(taskdata);
3584 kmp_team_t *team = taskdata->td_team;
3585 kmp_int32 nthreads = team->t.t_nproc;
3590 kmp_int32 start_k = 0;
3592 kmp_int32 k = start_k;
3596 thread = team->t.t_threads[k];
3597 k = (k + 1) % nthreads;
3603 }
while (!__kmp_give_task(thread, k, ptask, pass));
3605 __kmp_second_top_half_finish_proxy(taskdata);
3609 (
"__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n",
3619 kmp_task_t *__kmp_task_dup_alloc(kmp_info_t *thread, kmp_task_t *task_src) {
3621 kmp_taskdata_t *taskdata;
3622 kmp_taskdata_t *taskdata_src;
3623 kmp_taskdata_t *parent_task = thread->th.th_current_task;
3624 size_t shareds_offset;
3627 KA_TRACE(10, (
"__kmp_task_dup_alloc(enter): Th %p, source task %p\n", thread,
3629 taskdata_src = KMP_TASK_TO_TASKDATA(task_src);
3630 KMP_DEBUG_ASSERT(taskdata_src->td_flags.proxy ==
3632 KMP_DEBUG_ASSERT(taskdata_src->td_flags.tasktype == TASK_EXPLICIT);
3633 task_size = taskdata_src->td_size_alloc;
3636 KA_TRACE(30, (
"__kmp_task_dup_alloc: Th %p, malloc size %ld\n", thread,
3639 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, task_size);
3641 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, task_size);
3643 KMP_MEMCPY(taskdata, taskdata_src, task_size);
3645 task = KMP_TASKDATA_TO_TASK(taskdata);
3648 taskdata->td_task_id = KMP_GEN_TASK_ID();
3649 if (task->shareds != NULL) {
3650 shareds_offset = (
char *)task_src->shareds - (
char *)taskdata_src;
3651 task->shareds = &((
char *)taskdata)[shareds_offset];
3652 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
3655 taskdata->td_alloc_thread = thread;
3656 taskdata->td_parent = parent_task;
3657 taskdata->td_taskgroup =
3663 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
3664 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
3665 if (parent_task->td_taskgroup)
3666 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
3669 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT)
3670 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
3674 (
"__kmp_task_dup_alloc(exit): Th %p, created task %p, parent=%p\n",
3675 thread, taskdata, taskdata->td_parent));
3677 if (UNLIKELY(ompt_enabled.enabled))
3678 __ompt_task_init(taskdata, thread->th.th_info.ds.ds_gtid);
3687 typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32);
3689 KMP_BUILD_ASSERT(
sizeof(
long) == 4 ||
sizeof(
long) == 8);
3694 class kmp_taskloop_bounds_t {
3696 const kmp_taskdata_t *taskdata;
3697 size_t lower_offset;
3698 size_t upper_offset;
3701 kmp_taskloop_bounds_t(kmp_task_t *_task, kmp_uint64 *lb, kmp_uint64 *ub)
3702 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(task)),
3703 lower_offset((char *)lb - (char *)task),
3704 upper_offset((char *)ub - (char *)task) {
3705 KMP_DEBUG_ASSERT((
char *)lb > (
char *)_task);
3706 KMP_DEBUG_ASSERT((
char *)ub > (
char *)_task);
3708 kmp_taskloop_bounds_t(kmp_task_t *_task,
const kmp_taskloop_bounds_t &bounds)
3709 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(_task)),
3710 lower_offset(bounds.lower_offset), upper_offset(bounds.upper_offset) {}
3711 size_t get_lower_offset()
const {
return lower_offset; }
3712 size_t get_upper_offset()
const {
return upper_offset; }
3713 kmp_uint64 get_lb()
const {
3715 #if defined(KMP_GOMP_COMPAT) 3717 if (!taskdata->td_flags.native) {
3718 retval = *(kmp_int64 *)((
char *)task + lower_offset);
3721 if (taskdata->td_size_loop_bounds == 4) {
3722 kmp_int32 *lb = RCAST(kmp_int32 *, task->shareds);
3723 retval = (kmp_int64)*lb;
3725 kmp_int64 *lb = RCAST(kmp_int64 *, task->shareds);
3726 retval = (kmp_int64)*lb;
3730 retval = *(kmp_int64 *)((
char *)task + lower_offset);
3731 #endif // defined(KMP_GOMP_COMPAT) 3734 kmp_uint64 get_ub()
const {
3736 #if defined(KMP_GOMP_COMPAT) 3738 if (!taskdata->td_flags.native) {
3739 retval = *(kmp_int64 *)((
char *)task + upper_offset);
3742 if (taskdata->td_size_loop_bounds == 4) {
3743 kmp_int32 *ub = RCAST(kmp_int32 *, task->shareds) + 1;
3744 retval = (kmp_int64)*ub;
3746 kmp_int64 *ub = RCAST(kmp_int64 *, task->shareds) + 1;
3747 retval = (kmp_int64)*ub;
3751 retval = *(kmp_int64 *)((
char *)task + upper_offset);
3752 #endif // defined(KMP_GOMP_COMPAT) 3755 void set_lb(kmp_uint64 lb) {
3756 #if defined(KMP_GOMP_COMPAT) 3758 if (!taskdata->td_flags.native) {
3759 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
3762 if (taskdata->td_size_loop_bounds == 4) {
3763 kmp_uint32 *lower = RCAST(kmp_uint32 *, task->shareds);
3764 *lower = (kmp_uint32)lb;
3766 kmp_uint64 *lower = RCAST(kmp_uint64 *, task->shareds);
3767 *lower = (kmp_uint64)lb;
3771 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
3772 #endif // defined(KMP_GOMP_COMPAT) 3774 void set_ub(kmp_uint64 ub) {
3775 #if defined(KMP_GOMP_COMPAT) 3777 if (!taskdata->td_flags.native) {
3778 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
3781 if (taskdata->td_size_loop_bounds == 4) {
3782 kmp_uint32 *upper = RCAST(kmp_uint32 *, task->shareds) + 1;
3783 *upper = (kmp_uint32)ub;
3785 kmp_uint64 *upper = RCAST(kmp_uint64 *, task->shareds) + 1;
3786 *upper = (kmp_uint64)ub;
3790 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
3791 #endif // defined(KMP_GOMP_COMPAT) 3810 void __kmp_taskloop_linear(
ident_t *loc,
int gtid, kmp_task_t *task,
3811 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
3812 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
3813 kmp_uint64 grainsize, kmp_uint64 extras,
3820 KMP_TIME_PARTITIONED_BLOCK(OMP_taskloop_scheduling);
3821 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
3823 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
3824 kmp_uint64 lower = task_bounds.get_lb();
3825 kmp_uint64 upper = task_bounds.get_ub();
3827 kmp_info_t *thread = __kmp_threads[gtid];
3828 kmp_taskdata_t *current_task = thread->th.th_current_task;
3829 kmp_task_t *next_task;
3830 kmp_int32 lastpriv = 0;
3832 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras);
3833 KMP_DEBUG_ASSERT(num_tasks > extras);
3834 KMP_DEBUG_ASSERT(num_tasks > 0);
3835 KA_TRACE(20, (
"__kmp_taskloop_linear: T#%d: %lld tasks, grainsize %lld, " 3836 "extras %lld, i=%lld,%lld(%d)%lld, dup %p\n",
3837 gtid, num_tasks, grainsize, extras, lower, upper, ub_glob, st,
3841 for (i = 0; i < num_tasks; ++i) {
3842 kmp_uint64 chunk_minus_1;
3844 chunk_minus_1 = grainsize - 1;
3846 chunk_minus_1 = grainsize;
3849 upper = lower + st * chunk_minus_1;
3850 if (i == num_tasks - 1) {
3853 KMP_DEBUG_ASSERT(upper == *ub);
3854 if (upper == ub_glob)
3856 }
else if (st > 0) {
3857 KMP_DEBUG_ASSERT((kmp_uint64)st > *ub - upper);
3858 if ((kmp_uint64)st > ub_glob - upper)
3861 KMP_DEBUG_ASSERT(upper + st < *ub);
3862 if (upper - ub_glob < (kmp_uint64)(-st))
3866 next_task = __kmp_task_dup_alloc(thread, task);
3867 kmp_taskdata_t *next_taskdata = KMP_TASK_TO_TASKDATA(next_task);
3868 kmp_taskloop_bounds_t next_task_bounds =
3869 kmp_taskloop_bounds_t(next_task, task_bounds);
3872 next_task_bounds.set_lb(lower);
3873 if (next_taskdata->td_flags.native) {
3874 next_task_bounds.set_ub(upper + (st > 0 ? 1 : -1));
3876 next_task_bounds.set_ub(upper);
3878 if (ptask_dup != NULL)
3879 ptask_dup(next_task, task, lastpriv);
3881 (
"__kmp_taskloop_linear: T#%d; task #%llu: task %p: lower %lld, " 3882 "upper %lld stride %lld, (offsets %p %p)\n",
3883 gtid, i, next_task, lower, upper, st,
3884 next_task_bounds.get_lower_offset(),
3885 next_task_bounds.get_upper_offset()));
3887 __kmp_omp_taskloop_task(NULL, gtid, next_task,
3890 __kmp_omp_task(gtid, next_task,
true);
3895 __kmp_task_start(gtid, task, current_task);
3897 __kmp_task_finish<false>(gtid, task, current_task);
3902 typedef struct __taskloop_params {
3909 kmp_uint64 num_tasks;
3910 kmp_uint64 grainsize;
3913 kmp_uint64 num_t_min;
3917 } __taskloop_params_t;
3919 void __kmp_taskloop_recur(
ident_t *,
int, kmp_task_t *, kmp_uint64 *,
3920 kmp_uint64 *, kmp_int64, kmp_uint64, kmp_uint64,
3921 kmp_uint64, kmp_uint64, kmp_uint64, kmp_uint64,
3928 int __kmp_taskloop_task(
int gtid,
void *ptask) {
3929 __taskloop_params_t *p =
3930 (__taskloop_params_t *)((kmp_task_t *)ptask)->shareds;
3931 kmp_task_t *task = p->task;
3932 kmp_uint64 *lb = p->lb;
3933 kmp_uint64 *ub = p->ub;
3934 void *task_dup = p->task_dup;
3936 kmp_int64 st = p->st;
3937 kmp_uint64 ub_glob = p->ub_glob;
3938 kmp_uint64 num_tasks = p->num_tasks;
3939 kmp_uint64 grainsize = p->grainsize;
3940 kmp_uint64 extras = p->extras;
3941 kmp_uint64 tc = p->tc;
3942 kmp_uint64 num_t_min = p->num_t_min;
3944 void *codeptr_ra = p->codeptr_ra;
3947 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
3948 KMP_DEBUG_ASSERT(task != NULL);
3949 KA_TRACE(20, (
"__kmp_taskloop_task: T#%d, task %p: %lld tasks, grainsize" 3950 " %lld, extras %lld, i=%lld,%lld(%d), dup %p\n",
3951 gtid, taskdata, num_tasks, grainsize, extras, *lb, *ub, st,
3954 KMP_DEBUG_ASSERT(num_tasks * 2 + 1 > num_t_min);
3955 if (num_tasks > num_t_min)
3956 __kmp_taskloop_recur(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
3957 grainsize, extras, tc, num_t_min,
3963 __kmp_taskloop_linear(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
3964 grainsize, extras, tc,
3970 KA_TRACE(40, (
"__kmp_taskloop_task(exit): T#%d\n", gtid));
3991 void __kmp_taskloop_recur(
ident_t *loc,
int gtid, kmp_task_t *task,
3992 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
3993 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
3994 kmp_uint64 grainsize, kmp_uint64 extras,
3995 kmp_uint64 tc, kmp_uint64 num_t_min,
4001 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4002 KMP_DEBUG_ASSERT(task != NULL);
4003 KMP_DEBUG_ASSERT(num_tasks > num_t_min);
4004 KA_TRACE(20, (
"__kmp_taskloop_recur: T#%d, task %p: %lld tasks, grainsize" 4005 " %lld, extras %lld, i=%lld,%lld(%d), dup %p\n",
4006 gtid, taskdata, num_tasks, grainsize, extras, *lb, *ub, st,
4009 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
4010 kmp_uint64 lower = *lb;
4011 kmp_info_t *thread = __kmp_threads[gtid];
4013 kmp_task_t *next_task;
4014 size_t lower_offset =
4015 (
char *)lb - (
char *)task;
4016 size_t upper_offset =
4017 (
char *)ub - (
char *)task;
4019 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras);
4020 KMP_DEBUG_ASSERT(num_tasks > extras);
4021 KMP_DEBUG_ASSERT(num_tasks > 0);
4024 kmp_uint64 lb1, ub0, tc0, tc1, ext0, ext1;
4025 kmp_uint64 gr_size0 = grainsize;
4026 kmp_uint64 n_tsk0 = num_tasks >> 1;
4027 kmp_uint64 n_tsk1 = num_tasks - n_tsk0;
4028 if (n_tsk0 <= extras) {
4031 ext1 = extras - n_tsk0;
4032 tc0 = gr_size0 * n_tsk0;
4037 tc1 = grainsize * n_tsk1;
4040 ub0 = lower + st * (tc0 - 1);
4044 next_task = __kmp_task_dup_alloc(thread, task);
4046 *(kmp_uint64 *)((
char *)next_task + lower_offset) = lb1;
4047 if (ptask_dup != NULL)
4048 ptask_dup(next_task, task, 0);
4052 kmp_task_t *new_task =
4053 __kmpc_omp_task_alloc(loc, gtid, 1, 3 *
sizeof(
void *),
4054 sizeof(__taskloop_params_t), &__kmp_taskloop_task);
4055 __taskloop_params_t *p = (__taskloop_params_t *)new_task->shareds;
4056 p->task = next_task;
4057 p->lb = (kmp_uint64 *)((
char *)next_task + lower_offset);
4058 p->ub = (kmp_uint64 *)((
char *)next_task + upper_offset);
4059 p->task_dup = task_dup;
4061 p->ub_glob = ub_glob;
4062 p->num_tasks = n_tsk1;
4063 p->grainsize = grainsize;
4066 p->num_t_min = num_t_min;
4068 p->codeptr_ra = codeptr_ra;
4073 __kmp_omp_taskloop_task(NULL, gtid, new_task, codeptr_ra);
4075 __kmp_omp_task(gtid, new_task,
true);
4079 if (n_tsk0 > num_t_min)
4080 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0, gr_size0,
4081 ext0, tc0, num_t_min,
4087 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0,
4088 gr_size0, ext0, tc0,
4094 KA_TRACE(40, (
"__kmpc_taskloop_recur(exit): T#%d\n", gtid));
4113 void __kmpc_taskloop(
ident_t *loc,
int gtid, kmp_task_t *task,
int if_val,
4114 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
int nogroup,
4115 int sched, kmp_uint64 grainsize,
void *task_dup) {
4116 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4117 KMP_DEBUG_ASSERT(task != NULL);
4120 #if OMPT_SUPPORT && OMPT_OPTIONAL 4121 OMPT_STORE_RETURN_ADDRESS(gtid);
4123 __kmpc_taskgroup(loc, gtid);
4128 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4131 kmp_uint64 lower = task_bounds.get_lb();
4132 kmp_uint64 upper = task_bounds.get_ub();
4133 kmp_uint64 ub_glob = upper;
4134 kmp_uint64 num_tasks = 0, extras = 0;
4135 kmp_uint64 num_tasks_min = __kmp_taskloop_min_tasks;
4136 kmp_info_t *thread = __kmp_threads[gtid];
4137 kmp_taskdata_t *current_task = thread->th.th_current_task;
4139 KA_TRACE(20, (
"__kmpc_taskloop: T#%d, task %p, lb %lld, ub %lld, st %lld, " 4140 "grain %llu(%d), dup %p\n",
4141 gtid, taskdata, lower, upper, st, grainsize, sched, task_dup));
4145 tc = upper - lower + 1;
4146 }
else if (st < 0) {
4147 tc = (lower - upper) / (-st) + 1;
4149 tc = (upper - lower) / st + 1;
4152 KA_TRACE(20, (
"__kmpc_taskloop(exit): T#%d zero-trip loop\n", gtid));
4154 __kmp_task_start(gtid, task, current_task);
4156 __kmp_task_finish<false>(gtid, task, current_task);
4160 #if OMPT_SUPPORT && OMPT_OPTIONAL 4161 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
4162 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
4163 if (ompt_enabled.ompt_callback_work) {
4164 ompt_callbacks.ompt_callback(ompt_callback_work)(
4165 ompt_work_taskloop, ompt_scope_begin, &(team_info->parallel_data),
4166 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
4170 if (num_tasks_min == 0)
4173 KMP_MIN(thread->th.th_team_nproc * 10, INITIAL_TASK_DEQUE_SIZE);
4179 grainsize = thread->th.th_team_nproc * 10;
4181 if (grainsize > tc) {
4186 num_tasks = grainsize;
4187 grainsize = tc / num_tasks;
4188 extras = tc % num_tasks;
4192 if (grainsize > tc) {
4197 num_tasks = tc / grainsize;
4199 grainsize = tc / num_tasks;
4200 extras = tc % num_tasks;
4204 KMP_ASSERT2(0,
"unknown scheduling of taskloop");
4206 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras);
4207 KMP_DEBUG_ASSERT(num_tasks > extras);
4208 KMP_DEBUG_ASSERT(num_tasks > 0);
4214 taskdata->td_flags.task_serial = 1;
4215 taskdata->td_flags.tiedness = TASK_TIED;
4217 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4218 grainsize, extras, tc,
4220 OMPT_GET_RETURN_ADDRESS(0),
4225 }
else if (num_tasks > num_tasks_min && !taskdata->td_flags.native) {
4226 KA_TRACE(20, (
"__kmpc_taskloop: T#%d, go recursive: tc %llu, #tasks %llu" 4227 "(%lld), grain %llu, extras %llu\n",
4228 gtid, tc, num_tasks, num_tasks_min, grainsize, extras));
4229 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4230 grainsize, extras, tc, num_tasks_min,
4232 OMPT_GET_RETURN_ADDRESS(0),
4236 KA_TRACE(20, (
"__kmpc_taskloop: T#%d, go linear: tc %llu, #tasks %llu" 4237 "(%lld), grain %llu, extras %llu\n",
4238 gtid, tc, num_tasks, num_tasks_min, grainsize, extras));
4239 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4240 grainsize, extras, tc,
4242 OMPT_GET_RETURN_ADDRESS(0),
4247 #if OMPT_SUPPORT && OMPT_OPTIONAL 4248 if (ompt_enabled.ompt_callback_work) {
4249 ompt_callbacks.ompt_callback(ompt_callback_work)(
4250 ompt_work_taskloop, ompt_scope_end, &(team_info->parallel_data),
4251 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
4256 #if OMPT_SUPPORT && OMPT_OPTIONAL 4257 OMPT_STORE_RETURN_ADDRESS(gtid);
4259 __kmpc_end_taskgroup(loc, gtid);
4261 KA_TRACE(20, (
"__kmpc_taskloop(exit): T#%d\n", gtid));
#define KMP_COUNT_BLOCK(name)
Increments specified counter (name).