16 #include "kmp_stats.h"
17 #include "kmp_wait_release.h"
18 #include "kmp_taskdeps.h"
21 #include "ompt-specific.h"
25 static void __kmp_enable_tasking(kmp_task_team_t *task_team,
26 kmp_info_t *this_thr);
27 static void __kmp_alloc_task_deque(kmp_info_t *thread,
28 kmp_thread_data_t *thread_data);
29 static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
30 kmp_task_team_t *task_team);
31 static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask);
33 #ifdef BUILD_TIED_TASK_STACK
42 static void __kmp_trace_task_stack(kmp_int32 gtid,
43 kmp_thread_data_t *thread_data,
44 int threshold,
char *location) {
45 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
46 kmp_taskdata_t **stack_top = task_stack->ts_top;
47 kmp_int32 entries = task_stack->ts_entries;
48 kmp_taskdata_t *tied_task;
52 (
"__kmp_trace_task_stack(start): location = %s, gtid = %d, entries = %d, "
53 "first_block = %p, stack_top = %p \n",
54 location, gtid, entries, task_stack->ts_first_block, stack_top));
56 KMP_DEBUG_ASSERT(stack_top != NULL);
57 KMP_DEBUG_ASSERT(entries > 0);
59 while (entries != 0) {
60 KMP_DEBUG_ASSERT(stack_top != &task_stack->ts_first_block.sb_block[0]);
62 if (entries & TASK_STACK_INDEX_MASK == 0) {
63 kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(stack_top);
65 stack_block = stack_block->sb_prev;
66 stack_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE];
73 tied_task = *stack_top;
75 KMP_DEBUG_ASSERT(tied_task != NULL);
76 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
79 (
"__kmp_trace_task_stack(%s): gtid=%d, entry=%d, "
80 "stack_top=%p, tied_task=%p\n",
81 location, gtid, entries, stack_top, tied_task));
83 KMP_DEBUG_ASSERT(stack_top == &task_stack->ts_first_block.sb_block[0]);
86 (
"__kmp_trace_task_stack(exit): location = %s, gtid = %d\n",
96 static void __kmp_init_task_stack(kmp_int32 gtid,
97 kmp_thread_data_t *thread_data) {
98 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
99 kmp_stack_block_t *first_block;
102 first_block = &task_stack->ts_first_block;
103 task_stack->ts_top = (kmp_taskdata_t **)first_block;
104 memset((
void *)first_block,
'\0',
105 TASK_STACK_BLOCK_SIZE *
sizeof(kmp_taskdata_t *));
108 task_stack->ts_entries = TASK_STACK_EMPTY;
109 first_block->sb_next = NULL;
110 first_block->sb_prev = NULL;
117 static void __kmp_free_task_stack(kmp_int32 gtid,
118 kmp_thread_data_t *thread_data) {
119 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
120 kmp_stack_block_t *stack_block = &task_stack->ts_first_block;
122 KMP_DEBUG_ASSERT(task_stack->ts_entries == TASK_STACK_EMPTY);
124 while (stack_block != NULL) {
125 kmp_stack_block_t *next_block = (stack_block) ? stack_block->sb_next : NULL;
127 stack_block->sb_next = NULL;
128 stack_block->sb_prev = NULL;
129 if (stack_block != &task_stack->ts_first_block) {
130 __kmp_thread_free(thread,
133 stack_block = next_block;
136 task_stack->ts_entries = 0;
137 task_stack->ts_top = NULL;
146 static void __kmp_push_task_stack(kmp_int32 gtid, kmp_info_t *thread,
147 kmp_taskdata_t *tied_task) {
149 kmp_thread_data_t *thread_data =
150 &thread->th.th_task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
151 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
153 if (tied_task->td_flags.team_serial || tied_task->td_flags.tasking_ser) {
157 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
158 KMP_DEBUG_ASSERT(task_stack->ts_top != NULL);
161 (
"__kmp_push_task_stack(enter): GTID: %d; THREAD: %p; TASK: %p\n",
162 gtid, thread, tied_task));
164 *(task_stack->ts_top) = tied_task;
167 task_stack->ts_top++;
168 task_stack->ts_entries++;
170 if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) {
172 kmp_stack_block_t *stack_block =
173 (kmp_stack_block_t *)(task_stack->ts_top - TASK_STACK_BLOCK_SIZE);
176 if (stack_block->sb_next !=
178 task_stack->ts_top = &stack_block->sb_next->sb_block[0];
180 kmp_stack_block_t *new_block = (kmp_stack_block_t *)__kmp_thread_calloc(
181 thread,
sizeof(kmp_stack_block_t));
183 task_stack->ts_top = &new_block->sb_block[0];
184 stack_block->sb_next = new_block;
185 new_block->sb_prev = stack_block;
186 new_block->sb_next = NULL;
190 (
"__kmp_push_task_stack(): GTID: %d; TASK: %p; Alloc new block: %p\n",
191 gtid, tied_task, new_block));
194 KA_TRACE(20, (
"__kmp_push_task_stack(exit): GTID: %d; TASK: %p\n", gtid,
205 static void __kmp_pop_task_stack(kmp_int32 gtid, kmp_info_t *thread,
206 kmp_taskdata_t *ending_task) {
208 kmp_thread_data_t *thread_data =
209 &thread->th.th_task_team->tt_threads_data[__kmp_tid_from_gtid(gtid)];
210 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
211 kmp_taskdata_t *tied_task;
213 if (ending_task->td_flags.team_serial || ending_task->td_flags.tasking_ser) {
218 KMP_DEBUG_ASSERT(task_stack->ts_top != NULL);
219 KMP_DEBUG_ASSERT(task_stack->ts_entries > 0);
221 KA_TRACE(20, (
"__kmp_pop_task_stack(enter): GTID: %d; THREAD: %p\n", gtid,
225 if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) {
226 kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(task_stack->ts_top);
228 stack_block = stack_block->sb_prev;
229 task_stack->ts_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE];
233 task_stack->ts_top--;
234 task_stack->ts_entries--;
236 tied_task = *(task_stack->ts_top);
238 KMP_DEBUG_ASSERT(tied_task != NULL);
239 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
240 KMP_DEBUG_ASSERT(tied_task == ending_task);
242 KA_TRACE(20, (
"__kmp_pop_task_stack(exit): GTID: %d; TASK: %p\n", gtid,
251 static bool __kmp_task_is_allowed(
int gtid,
const kmp_int32 is_constrained,
252 const kmp_taskdata_t *tasknew,
253 const kmp_taskdata_t *taskcurr) {
254 if (is_constrained && (tasknew->td_flags.tiedness == TASK_TIED)) {
258 kmp_taskdata_t *current = taskcurr->td_last_tied;
259 KMP_DEBUG_ASSERT(current != NULL);
261 if (current->td_flags.tasktype == TASK_EXPLICIT ||
262 current->td_taskwait_thread > 0) {
263 kmp_int32 level = current->td_level;
264 kmp_taskdata_t *parent = tasknew->td_parent;
265 while (parent != current && parent->td_level > level) {
267 parent = parent->td_parent;
268 KMP_DEBUG_ASSERT(parent != NULL);
270 if (parent != current)
275 kmp_depnode_t *node = tasknew->td_depnode;
276 if (UNLIKELY(node && (node->dn.mtx_num_locks > 0))) {
277 for (
int i = 0; i < node->dn.mtx_num_locks; ++i) {
278 KMP_DEBUG_ASSERT(node->dn.mtx_locks[i] != NULL);
279 if (__kmp_test_lock(node->dn.mtx_locks[i], gtid))
282 for (
int j = i - 1; j >= 0; --j)
283 __kmp_release_lock(node->dn.mtx_locks[j], gtid);
287 node->dn.mtx_num_locks = -node->dn.mtx_num_locks;
296 static void __kmp_realloc_task_deque(kmp_info_t *thread,
297 kmp_thread_data_t *thread_data) {
298 kmp_int32 size = TASK_DEQUE_SIZE(thread_data->td);
299 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == size);
300 kmp_int32 new_size = 2 * size;
302 KE_TRACE(10, (
"__kmp_realloc_task_deque: T#%d reallocating deque[from %d to "
303 "%d] for thread_data %p\n",
304 __kmp_gtid_from_thread(thread), size, new_size, thread_data));
306 kmp_taskdata_t **new_deque =
307 (kmp_taskdata_t **)__kmp_allocate(new_size *
sizeof(kmp_taskdata_t *));
310 for (i = thread_data->td.td_deque_head, j = 0; j < size;
311 i = (i + 1) & TASK_DEQUE_MASK(thread_data->td), j++)
312 new_deque[j] = thread_data->td.td_deque[i];
314 __kmp_free(thread_data->td.td_deque);
316 thread_data->td.td_deque_head = 0;
317 thread_data->td.td_deque_tail = size;
318 thread_data->td.td_deque = new_deque;
319 thread_data->td.td_deque_size = new_size;
323 static kmp_int32 __kmp_push_task(kmp_int32 gtid, kmp_task_t *task) {
324 kmp_info_t *thread = __kmp_threads[gtid];
325 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
330 if (UNLIKELY(taskdata->td_flags.hidden_helper &&
331 !KMP_HIDDEN_HELPER_THREAD(gtid))) {
332 kmp_int32 shadow_gtid = KMP_GTID_TO_SHADOW_GTID(gtid);
333 __kmpc_give_task(task, __kmp_tid_from_gtid(shadow_gtid));
335 __kmp_hidden_helper_worker_thread_signal();
336 return TASK_SUCCESSFULLY_PUSHED;
339 kmp_task_team_t *task_team = thread->th.th_task_team;
340 kmp_int32 tid = __kmp_tid_from_gtid(gtid);
341 kmp_thread_data_t *thread_data;
344 (
"__kmp_push_task: T#%d trying to push task %p.\n", gtid, taskdata));
346 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
349 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
350 KMP_DEBUG_USE_VAR(counter);
353 (
"__kmp_push_task: T#%d untied_count (%d) incremented for task %p\n",
354 gtid, counter, taskdata));
358 if (UNLIKELY(taskdata->td_flags.task_serial)) {
359 KA_TRACE(20, (
"__kmp_push_task: T#%d team serialized; returning "
360 "TASK_NOT_PUSHED for task %p\n",
362 return TASK_NOT_PUSHED;
367 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
368 if (UNLIKELY(!KMP_TASKING_ENABLED(task_team))) {
369 __kmp_enable_tasking(task_team, thread);
371 KMP_DEBUG_ASSERT(TCR_4(task_team->tt.tt_found_tasks) == TRUE);
372 KMP_DEBUG_ASSERT(TCR_PTR(task_team->tt.tt_threads_data) != NULL);
375 thread_data = &task_team->tt.tt_threads_data[tid];
380 if (UNLIKELY(thread_data->td.td_deque == NULL)) {
381 __kmp_alloc_task_deque(thread, thread_data);
386 if (TCR_4(thread_data->td.td_deque_ntasks) >=
387 TASK_DEQUE_SIZE(thread_data->td)) {
388 if (__kmp_enable_task_throttling &&
389 __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
390 thread->th.th_current_task)) {
391 KA_TRACE(20, (
"__kmp_push_task: T#%d deque is full; returning "
392 "TASK_NOT_PUSHED for task %p\n",
394 return TASK_NOT_PUSHED;
396 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
398 if (TCR_4(thread_data->td.td_deque_ntasks) >=
399 TASK_DEQUE_SIZE(thread_data->td)) {
401 __kmp_realloc_task_deque(thread, thread_data);
407 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
409 if (TCR_4(thread_data->td.td_deque_ntasks) >=
410 TASK_DEQUE_SIZE(thread_data->td)) {
411 if (__kmp_enable_task_throttling &&
412 __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
413 thread->th.th_current_task)) {
414 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
415 KA_TRACE(20, (
"__kmp_push_task: T#%d deque is full on 2nd check; "
416 "returning TASK_NOT_PUSHED for task %p\n",
418 return TASK_NOT_PUSHED;
421 __kmp_realloc_task_deque(thread, thread_data);
426 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) <
427 TASK_DEQUE_SIZE(thread_data->td));
429 thread_data->td.td_deque[thread_data->td.td_deque_tail] =
432 thread_data->td.td_deque_tail =
433 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
434 TCW_4(thread_data->td.td_deque_ntasks,
435 TCR_4(thread_data->td.td_deque_ntasks) + 1);
436 KMP_FSYNC_RELEASING(thread->th.th_current_task);
437 KMP_FSYNC_RELEASING(taskdata);
438 KA_TRACE(20, (
"__kmp_push_task: T#%d returning TASK_SUCCESSFULLY_PUSHED: "
439 "task=%p ntasks=%d head=%u tail=%u\n",
440 gtid, taskdata, thread_data->td.td_deque_ntasks,
441 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
443 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
445 return TASK_SUCCESSFULLY_PUSHED;
452 void __kmp_pop_current_task_from_thread(kmp_info_t *this_thr) {
453 KF_TRACE(10, (
"__kmp_pop_current_task_from_thread(enter): T#%d "
454 "this_thread=%p, curtask=%p, "
455 "curtask_parent=%p\n",
456 0, this_thr, this_thr->th.th_current_task,
457 this_thr->th.th_current_task->td_parent));
459 this_thr->th.th_current_task = this_thr->th.th_current_task->td_parent;
461 KF_TRACE(10, (
"__kmp_pop_current_task_from_thread(exit): T#%d "
462 "this_thread=%p, curtask=%p, "
463 "curtask_parent=%p\n",
464 0, this_thr, this_thr->th.th_current_task,
465 this_thr->th.th_current_task->td_parent));
474 void __kmp_push_current_task_to_thread(kmp_info_t *this_thr, kmp_team_t *team,
478 KF_TRACE(10, (
"__kmp_push_current_task_to_thread(enter): T#%d this_thread=%p "
481 tid, this_thr, this_thr->th.th_current_task,
482 team->t.t_implicit_task_taskdata[tid].td_parent));
484 KMP_DEBUG_ASSERT(this_thr != NULL);
487 if (this_thr->th.th_current_task != &team->t.t_implicit_task_taskdata[0]) {
488 team->t.t_implicit_task_taskdata[0].td_parent =
489 this_thr->th.th_current_task;
490 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[0];
493 team->t.t_implicit_task_taskdata[tid].td_parent =
494 team->t.t_implicit_task_taskdata[0].td_parent;
495 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[tid];
498 KF_TRACE(10, (
"__kmp_push_current_task_to_thread(exit): T#%d this_thread=%p "
501 tid, this_thr, this_thr->th.th_current_task,
502 team->t.t_implicit_task_taskdata[tid].td_parent));
510 static void __kmp_task_start(kmp_int32 gtid, kmp_task_t *task,
511 kmp_taskdata_t *current_task) {
512 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
513 kmp_info_t *thread = __kmp_threads[gtid];
516 (
"__kmp_task_start(enter): T#%d starting task %p: current_task=%p\n",
517 gtid, taskdata, current_task));
519 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
524 current_task->td_flags.executing = 0;
527 #ifdef BUILD_TIED_TASK_STACK
528 if (taskdata->td_flags.tiedness == TASK_TIED) {
529 __kmp_push_task_stack(gtid, thread, taskdata);
534 thread->th.th_current_task = taskdata;
536 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 0 ||
537 taskdata->td_flags.tiedness == TASK_UNTIED);
538 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0 ||
539 taskdata->td_flags.tiedness == TASK_UNTIED);
540 taskdata->td_flags.started = 1;
541 taskdata->td_flags.executing = 1;
542 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
543 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
550 KA_TRACE(10, (
"__kmp_task_start(exit): T#%d task=%p\n", gtid, taskdata));
561 static inline void __ompt_task_init(kmp_taskdata_t *task,
int tid) {
563 task->ompt_task_info.task_data.value = 0;
564 task->ompt_task_info.frame.exit_frame = ompt_data_none;
565 task->ompt_task_info.frame.enter_frame = ompt_data_none;
566 task->ompt_task_info.frame.exit_frame_flags =
567 ompt_frame_runtime | ompt_frame_framepointer;
568 task->ompt_task_info.frame.enter_frame_flags =
569 ompt_frame_runtime | ompt_frame_framepointer;
574 static inline void __ompt_task_start(kmp_task_t *task,
575 kmp_taskdata_t *current_task,
577 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
578 ompt_task_status_t status = ompt_task_switch;
579 if (__kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded) {
580 status = ompt_task_yield;
581 __kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded = 0;
584 if (ompt_enabled.ompt_callback_task_schedule) {
585 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
586 &(current_task->ompt_task_info.task_data), status,
587 &(taskdata->ompt_task_info.task_data));
589 taskdata->ompt_task_info.scheduling_parent = current_task;
594 static inline void __ompt_task_finish(kmp_task_t *task,
595 kmp_taskdata_t *resumed_task,
596 ompt_task_status_t status) {
597 if (ompt_enabled.ompt_callback_task_schedule) {
598 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
599 if (__kmp_omp_cancellation && taskdata->td_taskgroup &&
600 taskdata->td_taskgroup->cancel_request == cancel_taskgroup) {
601 status = ompt_task_cancel;
605 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
606 &(taskdata->ompt_task_info.task_data), status,
607 (resumed_task ? &(resumed_task->ompt_task_info.task_data) : NULL));
613 static void __kmpc_omp_task_begin_if0_template(
ident_t *loc_ref, kmp_int32 gtid,
616 void *return_address) {
617 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
618 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
620 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(enter): T#%d loc=%p task=%p "
622 gtid, loc_ref, taskdata, current_task));
624 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
627 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
628 KMP_DEBUG_USE_VAR(counter);
629 KA_TRACE(20, (
"__kmpc_omp_task_begin_if0: T#%d untied_count (%d) "
630 "incremented for task %p\n",
631 gtid, counter, taskdata));
634 taskdata->td_flags.task_serial =
636 __kmp_task_start(gtid, task, current_task);
640 if (current_task->ompt_task_info.frame.enter_frame.ptr == NULL) {
641 current_task->ompt_task_info.frame.enter_frame.ptr =
642 taskdata->ompt_task_info.frame.exit_frame.ptr = frame_address;
643 current_task->ompt_task_info.frame.enter_frame_flags =
644 taskdata->ompt_task_info.frame.exit_frame_flags =
645 ompt_frame_application | ompt_frame_framepointer;
647 if (ompt_enabled.ompt_callback_task_create) {
648 ompt_task_info_t *parent_info = &(current_task->ompt_task_info);
649 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
650 &(parent_info->task_data), &(parent_info->frame),
651 &(taskdata->ompt_task_info.task_data),
652 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(taskdata), 0,
655 __ompt_task_start(task, current_task, gtid);
659 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(exit): T#%d loc=%p task=%p,\n", gtid,
665 static void __kmpc_omp_task_begin_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
668 void *return_address) {
669 __kmpc_omp_task_begin_if0_template<true>(loc_ref, gtid, task, frame_address,
680 void __kmpc_omp_task_begin_if0(
ident_t *loc_ref, kmp_int32 gtid,
683 if (UNLIKELY(ompt_enabled.enabled)) {
684 OMPT_STORE_RETURN_ADDRESS(gtid);
685 __kmpc_omp_task_begin_if0_ompt(loc_ref, gtid, task,
686 OMPT_GET_FRAME_ADDRESS(1),
687 OMPT_LOAD_RETURN_ADDRESS(gtid));
691 __kmpc_omp_task_begin_if0_template<false>(loc_ref, gtid, task, NULL, NULL);
697 void __kmpc_omp_task_begin(
ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task) {
698 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
702 (
"__kmpc_omp_task_begin(enter): T#%d loc=%p task=%p current_task=%p\n",
703 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task), current_task));
705 __kmp_task_start(gtid, task, current_task);
707 KA_TRACE(10, (
"__kmpc_omp_task_begin(exit): T#%d loc=%p task=%p,\n", gtid,
708 loc_ref, KMP_TASK_TO_TASKDATA(task)));
718 static void __kmp_free_task(kmp_int32 gtid, kmp_taskdata_t *taskdata,
719 kmp_info_t *thread) {
720 KA_TRACE(30, (
"__kmp_free_task: T#%d freeing data from task %p\n", gtid,
724 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
725 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0);
726 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 1);
727 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
728 KMP_DEBUG_ASSERT(taskdata->td_allocated_child_tasks == 0 ||
729 taskdata->td_flags.task_serial == 1);
730 KMP_DEBUG_ASSERT(taskdata->td_incomplete_child_tasks == 0);
732 taskdata->td_flags.freed = 1;
735 __kmp_fast_free(thread, taskdata);
737 __kmp_thread_free(thread, taskdata);
739 KA_TRACE(20, (
"__kmp_free_task: T#%d freed task %p\n", gtid, taskdata));
748 static void __kmp_free_task_and_ancestors(kmp_int32 gtid,
749 kmp_taskdata_t *taskdata,
750 kmp_info_t *thread) {
753 kmp_int32 team_serial =
754 (taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) &&
755 !taskdata->td_flags.proxy;
756 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
758 kmp_int32 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
759 KMP_DEBUG_ASSERT(children >= 0);
762 while (children == 0) {
763 kmp_taskdata_t *parent_taskdata = taskdata->td_parent;
765 KA_TRACE(20, (
"__kmp_free_task_and_ancestors(enter): T#%d task %p complete "
766 "and freeing itself\n",
770 __kmp_free_task(gtid, taskdata, thread);
772 taskdata = parent_taskdata;
778 if (taskdata->td_flags.tasktype == TASK_IMPLICIT) {
779 if (taskdata->td_dephash) {
780 int children = KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks);
781 kmp_tasking_flags_t flags_old = taskdata->td_flags;
782 if (children == 0 && flags_old.complete == 1) {
783 kmp_tasking_flags_t flags_new = flags_old;
784 flags_new.complete = 0;
785 if (KMP_COMPARE_AND_STORE_ACQ32(
786 RCAST(kmp_int32 *, &taskdata->td_flags),
787 *RCAST(kmp_int32 *, &flags_old),
788 *RCAST(kmp_int32 *, &flags_new))) {
789 KA_TRACE(100, (
"__kmp_free_task_and_ancestors: T#%d cleans "
790 "dephash of implicit task %p\n",
793 __kmp_dephash_free_entries(thread, taskdata->td_dephash);
800 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
801 KMP_DEBUG_ASSERT(children >= 0);
805 20, (
"__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; "
806 "not freeing it yet\n",
807 gtid, taskdata, children));
820 static void __kmp_task_finish(kmp_int32 gtid, kmp_task_t *task,
821 kmp_taskdata_t *resumed_task) {
822 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
823 kmp_info_t *thread = __kmp_threads[gtid];
824 kmp_task_team_t *task_team =
825 thread->th.th_task_team;
827 kmp_int32 children = 0;
829 KA_TRACE(10, (
"__kmp_task_finish(enter): T#%d finishing task %p and resuming "
831 gtid, taskdata, resumed_task));
833 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
836 #ifdef BUILD_TIED_TASK_STACK
837 if (taskdata->td_flags.tiedness == TASK_TIED) {
838 __kmp_pop_task_stack(gtid, thread, taskdata);
842 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
845 kmp_int32 counter = KMP_ATOMIC_DEC(&taskdata->td_untied_count) - 1;
848 (
"__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n",
849 gtid, counter, taskdata));
853 if (resumed_task == NULL) {
854 KMP_DEBUG_ASSERT(taskdata->td_flags.task_serial);
855 resumed_task = taskdata->td_parent;
858 thread->th.th_current_task = resumed_task;
859 resumed_task->td_flags.executing = 1;
860 KA_TRACE(10, (
"__kmp_task_finish(exit): T#%d partially done task %p, "
861 "resuming task %p\n",
862 gtid, taskdata, resumed_task));
870 (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) ==
871 taskdata->td_flags.task_serial);
872 if (taskdata->td_flags.task_serial) {
873 if (resumed_task == NULL) {
874 resumed_task = taskdata->td_parent;
878 KMP_DEBUG_ASSERT(resumed_task !=
888 if (UNLIKELY(taskdata->td_flags.destructors_thunk)) {
889 kmp_routine_entry_t destr_thunk = task->data1.destructors;
890 KMP_ASSERT(destr_thunk);
891 destr_thunk(gtid, task);
894 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
895 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 1);
896 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
899 if (UNLIKELY(taskdata->td_flags.detachable == TASK_DETACHABLE)) {
900 if (taskdata->td_allow_completion_event.type ==
901 KMP_EVENT_ALLOW_COMPLETION) {
903 __kmp_acquire_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
904 if (taskdata->td_allow_completion_event.type ==
905 KMP_EVENT_ALLOW_COMPLETION) {
907 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
908 taskdata->td_flags.executing = 0;
915 __ompt_task_finish(task, resumed_task, ompt_task_detach);
921 taskdata->td_flags.proxy = TASK_PROXY;
924 __kmp_release_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
929 taskdata->td_flags.complete = 1;
934 __ompt_task_finish(task, resumed_task, ompt_task_complete);
939 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) ||
940 taskdata->td_flags.detachable == TASK_DETACHABLE ||
941 taskdata->td_flags.hidden_helper) {
942 __kmp_release_deps(gtid, taskdata);
947 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks);
948 KMP_DEBUG_ASSERT(children >= 0);
949 if (taskdata->td_taskgroup)
950 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
951 }
else if (task_team && (task_team->tt.tt_found_proxy_tasks ||
952 task_team->tt.tt_hidden_helper_task_encountered)) {
955 __kmp_release_deps(gtid, taskdata);
961 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
962 taskdata->td_flags.executing = 0;
966 20, (
"__kmp_task_finish: T#%d finished task %p, %d incomplete children\n",
967 gtid, taskdata, children));
973 thread->th.th_current_task = resumed_task;
975 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
979 resumed_task->td_flags.executing = 1;
982 10, (
"__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n",
983 gtid, taskdata, resumed_task));
989 static void __kmpc_omp_task_complete_if0_template(
ident_t *loc_ref,
992 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n",
993 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
994 KMP_DEBUG_ASSERT(gtid >= 0);
996 __kmp_task_finish<ompt>(gtid, task, NULL);
998 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n",
999 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
1003 ompt_frame_t *ompt_frame;
1004 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
1005 ompt_frame->enter_frame = ompt_data_none;
1006 ompt_frame->enter_frame_flags =
1007 ompt_frame_runtime | ompt_frame_framepointer;
1016 void __kmpc_omp_task_complete_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
1018 __kmpc_omp_task_complete_if0_template<true>(loc_ref, gtid, task);
1027 void __kmpc_omp_task_complete_if0(
ident_t *loc_ref, kmp_int32 gtid,
1030 if (UNLIKELY(ompt_enabled.enabled)) {
1031 __kmpc_omp_task_complete_if0_ompt(loc_ref, gtid, task);
1035 __kmpc_omp_task_complete_if0_template<false>(loc_ref, gtid, task);
1041 void __kmpc_omp_task_complete(
ident_t *loc_ref, kmp_int32 gtid,
1043 KA_TRACE(10, (
"__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n", gtid,
1044 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1046 __kmp_task_finish<false>(gtid, task,
1049 KA_TRACE(10, (
"__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n", gtid,
1050 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1066 void __kmp_init_implicit_task(
ident_t *loc_ref, kmp_info_t *this_thr,
1067 kmp_team_t *team,
int tid,
int set_curr_task) {
1068 kmp_taskdata_t *task = &team->t.t_implicit_task_taskdata[tid];
1072 (
"__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n",
1073 tid, team, task, set_curr_task ?
"TRUE" :
"FALSE"));
1075 task->td_task_id = KMP_GEN_TASK_ID();
1076 task->td_team = team;
1079 task->td_ident = loc_ref;
1080 task->td_taskwait_ident = NULL;
1081 task->td_taskwait_counter = 0;
1082 task->td_taskwait_thread = 0;
1084 task->td_flags.tiedness = TASK_TIED;
1085 task->td_flags.tasktype = TASK_IMPLICIT;
1086 task->td_flags.proxy = TASK_FULL;
1089 task->td_flags.task_serial = 1;
1090 task->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1091 task->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1093 task->td_flags.started = 1;
1094 task->td_flags.executing = 1;
1095 task->td_flags.complete = 0;
1096 task->td_flags.freed = 0;
1098 task->td_depnode = NULL;
1099 task->td_last_tied = task;
1100 task->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1102 if (set_curr_task) {
1103 KMP_ATOMIC_ST_REL(&task->td_incomplete_child_tasks, 0);
1105 KMP_ATOMIC_ST_REL(&task->td_allocated_child_tasks, 0);
1106 task->td_taskgroup = NULL;
1107 task->td_dephash = NULL;
1108 __kmp_push_current_task_to_thread(this_thr, team, tid);
1110 KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0);
1111 KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0);
1115 if (UNLIKELY(ompt_enabled.enabled))
1116 __ompt_task_init(task, tid);
1119 KF_TRACE(10, (
"__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n", tid,
1128 void __kmp_finish_implicit_task(kmp_info_t *thread) {
1129 kmp_taskdata_t *task = thread->th.th_current_task;
1130 if (task->td_dephash) {
1132 task->td_flags.complete = 1;
1133 children = KMP_ATOMIC_LD_ACQ(&task->td_incomplete_child_tasks);
1134 kmp_tasking_flags_t flags_old = task->td_flags;
1135 if (children == 0 && flags_old.complete == 1) {
1136 kmp_tasking_flags_t flags_new = flags_old;
1137 flags_new.complete = 0;
1138 if (KMP_COMPARE_AND_STORE_ACQ32(RCAST(kmp_int32 *, &task->td_flags),
1139 *RCAST(kmp_int32 *, &flags_old),
1140 *RCAST(kmp_int32 *, &flags_new))) {
1141 KA_TRACE(100, (
"__kmp_finish_implicit_task: T#%d cleans "
1142 "dephash of implicit task %p\n",
1143 thread->th.th_info.ds.ds_gtid, task));
1144 __kmp_dephash_free_entries(thread, task->td_dephash);
1154 void __kmp_free_implicit_task(kmp_info_t *thread) {
1155 kmp_taskdata_t *task = thread->th.th_current_task;
1156 if (task && task->td_dephash) {
1157 __kmp_dephash_free(thread, task->td_dephash);
1158 task->td_dephash = NULL;
1164 static size_t __kmp_round_up_to_val(
size_t size,
size_t val) {
1165 if (size & (val - 1)) {
1167 if (size <= KMP_SIZE_T_MAX - val) {
1186 kmp_task_t *__kmp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1187 kmp_tasking_flags_t *flags,
1188 size_t sizeof_kmp_task_t,
size_t sizeof_shareds,
1189 kmp_routine_entry_t task_entry) {
1191 kmp_taskdata_t *taskdata;
1192 kmp_info_t *thread = __kmp_threads[gtid];
1193 kmp_team_t *team = thread->th.th_team;
1194 kmp_taskdata_t *parent_task = thread->th.th_current_task;
1195 size_t shareds_offset;
1197 if (UNLIKELY(!TCR_4(__kmp_init_middle)))
1198 __kmp_middle_initialize();
1200 if (flags->hidden_helper) {
1201 if (__kmp_enable_hidden_helper) {
1202 if (!TCR_4(__kmp_init_hidden_helper))
1203 __kmp_hidden_helper_initialize();
1206 flags->hidden_helper = FALSE;
1210 KA_TRACE(10, (
"__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) "
1211 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1212 gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t,
1213 sizeof_shareds, task_entry));
1215 KMP_DEBUG_ASSERT(parent_task);
1216 if (parent_task->td_flags.final) {
1217 if (flags->merged_if0) {
1222 if (flags->tiedness == TASK_UNTIED && !team->t.t_serialized) {
1226 KMP_CHECK_UPDATE(thread->th.th_task_team->tt.tt_untied_task_encountered, 1);
1232 if (UNLIKELY(flags->proxy == TASK_PROXY ||
1233 flags->detachable == TASK_DETACHABLE || flags->hidden_helper)) {
1234 if (flags->proxy == TASK_PROXY) {
1235 flags->tiedness = TASK_UNTIED;
1236 flags->merged_if0 = 1;
1240 if ((thread->th.th_task_team) == NULL) {
1243 KMP_DEBUG_ASSERT(team->t.t_serialized);
1245 (
"T#%d creating task team in __kmp_task_alloc for proxy task\n",
1248 __kmp_task_team_setup(thread, team, 1);
1249 thread->th.th_task_team = team->t.t_task_team[thread->th.th_task_state];
1251 kmp_task_team_t *task_team = thread->th.th_task_team;
1254 if (!KMP_TASKING_ENABLED(task_team)) {
1257 (
"T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid));
1258 __kmp_enable_tasking(task_team, thread);
1259 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
1260 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
1262 if (thread_data->td.td_deque == NULL) {
1263 __kmp_alloc_task_deque(thread, thread_data);
1267 if ((flags->proxy == TASK_PROXY || flags->detachable == TASK_DETACHABLE) &&
1268 task_team->tt.tt_found_proxy_tasks == FALSE)
1269 TCW_4(task_team->tt.tt_found_proxy_tasks, TRUE);
1270 if (flags->hidden_helper &&
1271 task_team->tt.tt_hidden_helper_task_encountered == FALSE)
1272 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, TRUE);
1277 shareds_offset =
sizeof(kmp_taskdata_t) + sizeof_kmp_task_t;
1278 shareds_offset = __kmp_round_up_to_val(shareds_offset,
sizeof(
void *));
1281 KA_TRACE(30, (
"__kmp_task_alloc: T#%d First malloc size: %ld\n", gtid,
1283 KA_TRACE(30, (
"__kmp_task_alloc: T#%d Second malloc size: %ld\n", gtid,
1288 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, shareds_offset +
1291 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, shareds_offset +
1295 task = KMP_TASKDATA_TO_TASK(taskdata);
1298 #if KMP_ARCH_X86 || KMP_ARCH_PPC64 || !KMP_HAVE_QUAD
1299 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(
double) - 1)) == 0);
1300 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(
double) - 1)) == 0);
1302 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(_Quad) - 1)) == 0);
1303 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(_Quad) - 1)) == 0);
1305 if (sizeof_shareds > 0) {
1307 task->shareds = &((
char *)taskdata)[shareds_offset];
1309 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
1312 task->shareds = NULL;
1314 task->routine = task_entry;
1317 taskdata->td_task_id = KMP_GEN_TASK_ID();
1318 taskdata->td_team = thread->th.th_team;
1319 taskdata->td_alloc_thread = thread;
1320 taskdata->td_parent = parent_task;
1321 taskdata->td_level = parent_task->td_level + 1;
1322 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
1323 taskdata->td_ident = loc_ref;
1324 taskdata->td_taskwait_ident = NULL;
1325 taskdata->td_taskwait_counter = 0;
1326 taskdata->td_taskwait_thread = 0;
1327 KMP_DEBUG_ASSERT(taskdata->td_parent != NULL);
1329 if (flags->proxy == TASK_FULL)
1330 copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs);
1332 taskdata->td_flags = *flags;
1333 taskdata->td_task_team = thread->th.th_task_team;
1334 taskdata->td_size_alloc = shareds_offset + sizeof_shareds;
1335 taskdata->td_flags.tasktype = TASK_EXPLICIT;
1338 if (flags->hidden_helper) {
1339 kmp_info_t *shadow_thread = __kmp_threads[KMP_GTID_TO_SHADOW_GTID(gtid)];
1340 taskdata->td_team = shadow_thread->th.th_team;
1341 taskdata->td_task_team = shadow_thread->th.th_task_team;
1345 taskdata->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1348 taskdata->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1354 taskdata->td_flags.task_serial =
1355 (parent_task->td_flags.final || taskdata->td_flags.team_serial ||
1356 taskdata->td_flags.tasking_ser || flags->merged_if0);
1358 taskdata->td_flags.started = 0;
1359 taskdata->td_flags.executing = 0;
1360 taskdata->td_flags.complete = 0;
1361 taskdata->td_flags.freed = 0;
1363 KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0);
1365 KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1);
1366 taskdata->td_taskgroup =
1367 parent_task->td_taskgroup;
1368 taskdata->td_dephash = NULL;
1369 taskdata->td_depnode = NULL;
1370 if (flags->tiedness == TASK_UNTIED)
1371 taskdata->td_last_tied = NULL;
1373 taskdata->td_last_tied = taskdata;
1374 taskdata->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1376 if (UNLIKELY(ompt_enabled.enabled))
1377 __ompt_task_init(taskdata, gtid);
1381 if (flags->proxy == TASK_PROXY || flags->detachable == TASK_DETACHABLE ||
1382 flags->hidden_helper ||
1383 !(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
1384 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
1385 if (parent_task->td_taskgroup)
1386 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
1389 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) {
1390 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
1392 if (flags->hidden_helper) {
1393 taskdata->td_flags.task_serial = FALSE;
1395 KMP_ATOMIC_INC(&__kmp_unexecuted_hidden_helper_tasks);
1399 KA_TRACE(20, (
"__kmp_task_alloc(exit): T#%d created task %p parent=%p\n",
1400 gtid, taskdata, taskdata->td_parent));
1405 kmp_task_t *__kmpc_omp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1406 kmp_int32 flags,
size_t sizeof_kmp_task_t,
1407 size_t sizeof_shareds,
1408 kmp_routine_entry_t task_entry) {
1410 kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
1411 __kmp_assert_valid_gtid(gtid);
1412 input_flags->native = FALSE;
1414 KA_TRACE(10, (
"__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s %s) "
1415 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1416 gtid, loc_ref, input_flags->tiedness ?
"tied " :
"untied",
1417 input_flags->proxy ?
"proxy" :
"",
1418 input_flags->detachable ?
"detachable" :
"", sizeof_kmp_task_t,
1419 sizeof_shareds, task_entry));
1421 retval = __kmp_task_alloc(loc_ref, gtid, input_flags, sizeof_kmp_task_t,
1422 sizeof_shareds, task_entry);
1424 KA_TRACE(20, (
"__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval));
1429 kmp_task_t *__kmpc_omp_target_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1431 size_t sizeof_kmp_task_t,
1432 size_t sizeof_shareds,
1433 kmp_routine_entry_t task_entry,
1434 kmp_int64 device_id) {
1435 auto &input_flags =
reinterpret_cast<kmp_tasking_flags_t &
>(flags);
1437 input_flags.tiedness = TASK_UNTIED;
1439 if (__kmp_enable_hidden_helper)
1440 input_flags.hidden_helper = TRUE;
1442 return __kmpc_omp_task_alloc(loc_ref, gtid, flags, sizeof_kmp_task_t,
1443 sizeof_shareds, task_entry);
1461 kmp_task_t *new_task, kmp_int32 naffins,
1462 kmp_task_affinity_info_t *affin_list) {
1471 static void __kmp_invoke_task(kmp_int32 gtid, kmp_task_t *task,
1472 kmp_taskdata_t *current_task) {
1473 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
1477 30, (
"__kmp_invoke_task(enter): T#%d invoking task %p, current_task=%p\n",
1478 gtid, taskdata, current_task));
1479 KMP_DEBUG_ASSERT(task);
1480 if (UNLIKELY(taskdata->td_flags.proxy == TASK_PROXY &&
1481 taskdata->td_flags.complete == 1)) {
1486 (
"__kmp_invoke_task: T#%d running bottom finish for proxy task %p\n",
1489 __kmp_bottom_half_finish_proxy(gtid, task);
1491 KA_TRACE(30, (
"__kmp_invoke_task(exit): T#%d completed bottom finish for "
1492 "proxy task %p, resuming task %p\n",
1493 gtid, taskdata, current_task));
1501 ompt_thread_info_t oldInfo;
1502 if (UNLIKELY(ompt_enabled.enabled)) {
1504 thread = __kmp_threads[gtid];
1505 oldInfo = thread->th.ompt_thread_info;
1506 thread->th.ompt_thread_info.wait_id = 0;
1507 thread->th.ompt_thread_info.state = (thread->th.th_team_serialized)
1508 ? ompt_state_work_serial
1509 : ompt_state_work_parallel;
1510 taskdata->ompt_task_info.frame.exit_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1515 if (taskdata->td_flags.hidden_helper) {
1517 KMP_ASSERT(KMP_HIDDEN_HELPER_THREAD(gtid));
1518 KMP_ATOMIC_DEC(&__kmp_unexecuted_hidden_helper_tasks);
1522 if (taskdata->td_flags.proxy != TASK_PROXY) {
1523 __kmp_task_start(gtid, task, current_task);
1529 if (UNLIKELY(__kmp_omp_cancellation)) {
1530 thread = __kmp_threads[gtid];
1531 kmp_team_t *this_team = thread->th.th_team;
1532 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
1533 if ((taskgroup && taskgroup->cancel_request) ||
1534 (this_team->t.t_cancel_request == cancel_parallel)) {
1535 #if OMPT_SUPPORT && OMPT_OPTIONAL
1536 ompt_data_t *task_data;
1537 if (UNLIKELY(ompt_enabled.ompt_callback_cancel)) {
1538 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, NULL, NULL);
1539 ompt_callbacks.ompt_callback(ompt_callback_cancel)(
1541 ((taskgroup && taskgroup->cancel_request) ? ompt_cancel_taskgroup
1542 : ompt_cancel_parallel) |
1543 ompt_cancel_discarded_task,
1556 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
1557 taskdata->td_last_tied = current_task->td_last_tied;
1558 KMP_DEBUG_ASSERT(taskdata->td_last_tied);
1560 #if KMP_STATS_ENABLED
1562 switch (KMP_GET_THREAD_STATE()) {
1563 case FORK_JOIN_BARRIER:
1564 KMP_PUSH_PARTITIONED_TIMER(OMP_task_join_bar);
1567 KMP_PUSH_PARTITIONED_TIMER(OMP_task_plain_bar);
1570 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskyield);
1573 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskwait);
1576 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskgroup);
1579 KMP_PUSH_PARTITIONED_TIMER(OMP_task_immediate);
1586 if (UNLIKELY(ompt_enabled.enabled))
1587 __ompt_task_start(task, current_task, gtid);
1591 if (ompd_state & OMPD_ENABLE_BP)
1592 ompd_bp_task_begin();
1595 #if USE_ITT_BUILD && USE_ITT_NOTIFY
1596 kmp_uint64 cur_time;
1597 kmp_int32 kmp_itt_count_task =
1598 __kmp_forkjoin_frames_mode == 3 && !taskdata->td_flags.task_serial &&
1599 current_task->td_flags.tasktype == TASK_IMPLICIT;
1600 if (kmp_itt_count_task) {
1601 thread = __kmp_threads[gtid];
1603 if (thread->th.th_bar_arrive_time)
1604 cur_time = __itt_get_timestamp();
1606 kmp_itt_count_task = 0;
1608 KMP_FSYNC_ACQUIRED(taskdata);
1611 #ifdef KMP_GOMP_COMPAT
1612 if (taskdata->td_flags.native) {
1613 ((void (*)(
void *))(*(task->routine)))(task->shareds);
1617 (*(task->routine))(gtid, task);
1619 KMP_POP_PARTITIONED_TIMER();
1621 #if USE_ITT_BUILD && USE_ITT_NOTIFY
1622 if (kmp_itt_count_task) {
1624 thread->th.th_bar_arrive_time += (__itt_get_timestamp() - cur_time);
1626 KMP_FSYNC_CANCEL(taskdata);
1627 KMP_FSYNC_RELEASING(taskdata->td_parent);
1632 if (ompd_state & OMPD_ENABLE_BP)
1637 if (taskdata->td_flags.proxy != TASK_PROXY) {
1639 if (UNLIKELY(ompt_enabled.enabled)) {
1640 thread->th.ompt_thread_info = oldInfo;
1641 if (taskdata->td_flags.tiedness == TASK_TIED) {
1642 taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
1644 __kmp_task_finish<true>(gtid, task, current_task);
1647 __kmp_task_finish<false>(gtid, task, current_task);
1652 (
"__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n",
1653 gtid, taskdata, current_task));
1667 kmp_int32 __kmpc_omp_task_parts(
ident_t *loc_ref, kmp_int32 gtid,
1668 kmp_task_t *new_task) {
1669 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1671 KA_TRACE(10, (
"__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n", gtid,
1672 loc_ref, new_taskdata));
1675 kmp_taskdata_t *parent;
1676 if (UNLIKELY(ompt_enabled.enabled)) {
1677 parent = new_taskdata->td_parent;
1678 if (ompt_enabled.ompt_callback_task_create) {
1679 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1680 &(parent->ompt_task_info.task_data), &(parent->ompt_task_info.frame),
1681 &(new_taskdata->ompt_task_info.task_data), ompt_task_explicit, 0,
1682 OMPT_GET_RETURN_ADDRESS(0));
1690 if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1692 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1693 new_taskdata->td_flags.task_serial = 1;
1694 __kmp_invoke_task(gtid, new_task, current_task);
1699 (
"__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: "
1700 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
1701 gtid, loc_ref, new_taskdata));
1704 if (UNLIKELY(ompt_enabled.enabled)) {
1705 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1708 return TASK_CURRENT_NOT_QUEUED;
1722 kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task,
1723 bool serialize_immediate) {
1724 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1728 if (new_taskdata->td_flags.proxy == TASK_PROXY ||
1729 __kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1731 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1732 if (serialize_immediate)
1733 new_taskdata->td_flags.task_serial = 1;
1734 __kmp_invoke_task(gtid, new_task, current_task);
1737 return TASK_CURRENT_NOT_QUEUED;
1752 kmp_int32 __kmpc_omp_task(
ident_t *loc_ref, kmp_int32 gtid,
1753 kmp_task_t *new_task) {
1755 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1757 #if KMP_DEBUG || OMPT_SUPPORT
1758 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1760 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1762 __kmp_assert_valid_gtid(gtid);
1765 kmp_taskdata_t *parent = NULL;
1766 if (UNLIKELY(ompt_enabled.enabled)) {
1767 if (!new_taskdata->td_flags.started) {
1768 OMPT_STORE_RETURN_ADDRESS(gtid);
1769 parent = new_taskdata->td_parent;
1770 if (!parent->ompt_task_info.frame.enter_frame.ptr) {
1771 parent->ompt_task_info.frame.enter_frame.ptr =
1772 OMPT_GET_FRAME_ADDRESS(0);
1774 if (ompt_enabled.ompt_callback_task_create) {
1775 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1776 &(parent->ompt_task_info.task_data),
1777 &(parent->ompt_task_info.frame),
1778 &(new_taskdata->ompt_task_info.task_data),
1779 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1780 OMPT_LOAD_RETURN_ADDRESS(gtid));
1785 __ompt_task_finish(new_task,
1786 new_taskdata->ompt_task_info.scheduling_parent,
1788 new_taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
1793 res = __kmp_omp_task(gtid, new_task,
true);
1795 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
1796 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1797 gtid, loc_ref, new_taskdata));
1799 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
1800 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1819 kmp_int32 __kmp_omp_taskloop_task(
ident_t *loc_ref, kmp_int32 gtid,
1820 kmp_task_t *new_task,
void *codeptr_ra) {
1822 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1824 #if KMP_DEBUG || OMPT_SUPPORT
1825 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1827 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1831 kmp_taskdata_t *parent = NULL;
1832 if (UNLIKELY(ompt_enabled.enabled && !new_taskdata->td_flags.started)) {
1833 parent = new_taskdata->td_parent;
1834 if (!parent->ompt_task_info.frame.enter_frame.ptr)
1835 parent->ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1836 if (ompt_enabled.ompt_callback_task_create) {
1837 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1838 &(parent->ompt_task_info.task_data), &(parent->ompt_task_info.frame),
1839 &(new_taskdata->ompt_task_info.task_data),
1840 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1846 res = __kmp_omp_task(gtid, new_task,
true);
1848 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
1849 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1850 gtid, loc_ref, new_taskdata));
1852 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
1853 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1859 template <
bool ompt>
1860 static kmp_int32 __kmpc_omp_taskwait_template(
ident_t *loc_ref, kmp_int32 gtid,
1861 void *frame_address,
1862 void *return_address) {
1863 kmp_taskdata_t *taskdata =
nullptr;
1865 int thread_finished = FALSE;
1866 KMP_SET_THREAD_STATE_BLOCK(TASKWAIT);
1868 KA_TRACE(10, (
"__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref));
1869 KMP_DEBUG_ASSERT(gtid >= 0);
1871 if (__kmp_tasking_mode != tskm_immediate_exec) {
1872 thread = __kmp_threads[gtid];
1873 taskdata = thread->th.th_current_task;
1875 #if OMPT_SUPPORT && OMPT_OPTIONAL
1876 ompt_data_t *my_task_data;
1877 ompt_data_t *my_parallel_data;
1880 my_task_data = &(taskdata->ompt_task_info.task_data);
1881 my_parallel_data = OMPT_CUR_TEAM_DATA(thread);
1883 taskdata->ompt_task_info.frame.enter_frame.ptr = frame_address;
1885 if (ompt_enabled.ompt_callback_sync_region) {
1886 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
1887 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
1888 my_task_data, return_address);
1891 if (ompt_enabled.ompt_callback_sync_region_wait) {
1892 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
1893 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
1894 my_task_data, return_address);
1904 taskdata->td_taskwait_counter += 1;
1905 taskdata->td_taskwait_ident = loc_ref;
1906 taskdata->td_taskwait_thread = gtid + 1;
1909 void *itt_sync_obj = NULL;
1911 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
1916 !taskdata->td_flags.team_serial && !taskdata->td_flags.final;
1918 must_wait = must_wait || (thread->th.th_task_team != NULL &&
1919 thread->th.th_task_team->tt.tt_found_proxy_tasks);
1923 (__kmp_enable_hidden_helper && thread->th.th_task_team != NULL &&
1924 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered);
1927 kmp_flag_32<false, false> flag(
1928 RCAST(std::atomic<kmp_uint32> *,
1929 &(taskdata->td_incomplete_child_tasks)),
1931 while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) != 0) {
1932 flag.execute_tasks(thread, gtid, FALSE,
1933 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
1934 __kmp_task_stealing_constraint);
1938 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
1939 KMP_FSYNC_ACQUIRED(taskdata);
1944 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
1946 #if OMPT_SUPPORT && OMPT_OPTIONAL
1948 if (ompt_enabled.ompt_callback_sync_region_wait) {
1949 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
1950 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
1951 my_task_data, return_address);
1953 if (ompt_enabled.ompt_callback_sync_region) {
1954 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
1955 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
1956 my_task_data, return_address);
1958 taskdata->ompt_task_info.frame.enter_frame = ompt_data_none;
1964 KA_TRACE(10, (
"__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, "
1965 "returning TASK_CURRENT_NOT_QUEUED\n",
1968 return TASK_CURRENT_NOT_QUEUED;
1971 #if OMPT_SUPPORT && OMPT_OPTIONAL
1973 static kmp_int32 __kmpc_omp_taskwait_ompt(
ident_t *loc_ref, kmp_int32 gtid,
1974 void *frame_address,
1975 void *return_address) {
1976 return __kmpc_omp_taskwait_template<true>(loc_ref, gtid, frame_address,
1983 kmp_int32 __kmpc_omp_taskwait(
ident_t *loc_ref, kmp_int32 gtid) {
1984 #if OMPT_SUPPORT && OMPT_OPTIONAL
1985 if (UNLIKELY(ompt_enabled.enabled)) {
1986 OMPT_STORE_RETURN_ADDRESS(gtid);
1987 return __kmpc_omp_taskwait_ompt(loc_ref, gtid, OMPT_GET_FRAME_ADDRESS(0),
1988 OMPT_LOAD_RETURN_ADDRESS(gtid));
1991 return __kmpc_omp_taskwait_template<false>(loc_ref, gtid, NULL, NULL);
1995 kmp_int32 __kmpc_omp_taskyield(
ident_t *loc_ref, kmp_int32 gtid,
int end_part) {
1996 kmp_taskdata_t *taskdata = NULL;
1998 int thread_finished = FALSE;
2001 KMP_SET_THREAD_STATE_BLOCK(TASKYIELD);
2003 KA_TRACE(10, (
"__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n",
2004 gtid, loc_ref, end_part));
2005 __kmp_assert_valid_gtid(gtid);
2007 if (__kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel) {
2008 thread = __kmp_threads[gtid];
2009 taskdata = thread->th.th_current_task;
2016 taskdata->td_taskwait_counter += 1;
2017 taskdata->td_taskwait_ident = loc_ref;
2018 taskdata->td_taskwait_thread = gtid + 1;
2021 void *itt_sync_obj = NULL;
2023 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2026 if (!taskdata->td_flags.team_serial) {
2027 kmp_task_team_t *task_team = thread->th.th_task_team;
2028 if (task_team != NULL) {
2029 if (KMP_TASKING_ENABLED(task_team)) {
2031 if (UNLIKELY(ompt_enabled.enabled))
2032 thread->th.ompt_thread_info.ompt_task_yielded = 1;
2034 __kmp_execute_tasks_32(
2035 thread, gtid, (kmp_flag_32<> *)NULL, FALSE,
2036 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2037 __kmp_task_stealing_constraint);
2039 if (UNLIKELY(ompt_enabled.enabled))
2040 thread->th.ompt_thread_info.ompt_task_yielded = 0;
2046 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2051 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2054 KA_TRACE(10, (
"__kmpc_omp_taskyield(exit): T#%d task %p resuming, "
2055 "returning TASK_CURRENT_NOT_QUEUED\n",
2058 return TASK_CURRENT_NOT_QUEUED;
2079 unsigned reserved31 : 31;
2159 template <
typename T>
2160 void *__kmp_task_reduction_init(
int gtid,
int num, T *data) {
2161 __kmp_assert_valid_gtid(gtid);
2162 kmp_info_t *thread = __kmp_threads[gtid];
2163 kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup;
2164 kmp_uint32 nth = thread->th.th_team_nproc;
2168 KMP_ASSERT(tg != NULL);
2169 KMP_ASSERT(data != NULL);
2170 KMP_ASSERT(num > 0);
2172 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n",
2176 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, taskgroup %p, #items %d\n",
2180 for (
int i = 0; i < num; ++i) {
2181 size_t size = data[i].reduce_size - 1;
2183 size += CACHE_LINE - size % CACHE_LINE;
2184 KMP_ASSERT(data[i].reduce_comb != NULL);
2187 arr[i].
flags = data[i].flags;
2191 __kmp_assign_orig<T>(arr[i], data[i]);
2192 if (!arr[i].flags.lazy_priv) {
2195 arr[i].
reduce_pend = (
char *)(arr[i].reduce_priv) + nth * size;
2196 if (arr[i].reduce_init != NULL) {
2198 for (
size_t j = 0; j < nth; ++j) {
2199 __kmp_call_init<T>(arr[i], j * size);
2206 arr[i].
reduce_priv = __kmp_allocate(nth *
sizeof(
void *));
2209 tg->reduce_data = (
void *)arr;
2210 tg->reduce_num_data = num;
2249 template <
typename T>
2250 void __kmp_task_reduction_init_copy(kmp_info_t *thr,
int num, T *data,
2251 kmp_taskgroup_t *tg,
void *reduce_data) {
2253 KA_TRACE(20, (
"__kmp_task_reduction_init_copy: Th %p, init taskgroup %p,"
2255 thr, tg, reduce_data));
2260 for (
int i = 0; i < num; ++i) {
2263 tg->reduce_data = (
void *)arr;
2264 tg->reduce_num_data = num;
2277 __kmp_assert_valid_gtid(gtid);
2278 kmp_info_t *thread = __kmp_threads[gtid];
2279 kmp_int32 nth = thread->th.th_team_nproc;
2283 kmp_taskgroup_t *tg = (kmp_taskgroup_t *)tskgrp;
2285 tg = thread->th.th_current_task->td_taskgroup;
2286 KMP_ASSERT(tg != NULL);
2288 kmp_int32 num = tg->reduce_num_data;
2289 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
2291 KMP_ASSERT(data != NULL);
2292 while (tg != NULL) {
2293 for (
int i = 0; i < num; ++i) {
2294 if (!arr[i].flags.lazy_priv) {
2295 if (data == arr[i].reduce_shar ||
2296 (data >= arr[i].reduce_priv && data < arr[i].reduce_pend))
2297 return (
char *)(arr[i].
reduce_priv) + tid * arr[i].reduce_size;
2300 void **p_priv = (
void **)(arr[i].reduce_priv);
2301 if (data == arr[i].reduce_shar)
2304 for (
int j = 0; j < nth; ++j)
2305 if (data == p_priv[j])
2309 if (p_priv[tid] == NULL) {
2311 p_priv[tid] = __kmp_allocate(arr[i].reduce_size);
2312 if (arr[i].reduce_init != NULL) {
2313 if (arr[i].reduce_orig != NULL) {
2315 p_priv[tid], arr[i].reduce_orig);
2317 ((void (*)(
void *))arr[i].
reduce_init)(p_priv[tid]);
2326 num = tg->reduce_num_data;
2328 KMP_ASSERT2(0,
"Unknown task reduction item");
2334 static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg) {
2335 kmp_int32 nth = th->th.th_team_nproc;
2336 KMP_DEBUG_ASSERT(nth > 1);
2338 kmp_int32 num = tg->reduce_num_data;
2339 for (
int i = 0; i < num; ++i) {
2341 void (*f_fini)(
void *) = (
void (*)(
void *))(arr[i].
reduce_fini);
2342 void (*f_comb)(
void *,
void *) =
2344 if (!arr[i].flags.lazy_priv) {
2347 for (
int j = 0; j < nth; ++j) {
2348 void *priv_data = (
char *)pr_data + j * size;
2349 f_comb(sh_data, priv_data);
2354 void **pr_data = (
void **)(arr[i].reduce_priv);
2355 for (
int j = 0; j < nth; ++j) {
2356 if (pr_data[j] != NULL) {
2357 f_comb(sh_data, pr_data[j]);
2360 __kmp_free(pr_data[j]);
2364 __kmp_free(arr[i].reduce_priv);
2366 __kmp_thread_free(th, arr);
2367 tg->reduce_data = NULL;
2368 tg->reduce_num_data = 0;
2374 static void __kmp_task_reduction_clean(kmp_info_t *th, kmp_taskgroup_t *tg) {
2375 __kmp_thread_free(th, tg->reduce_data);
2376 tg->reduce_data = NULL;
2377 tg->reduce_num_data = 0;
2380 template <
typename T>
2381 void *__kmp_task_reduction_modifier_init(
ident_t *loc,
int gtid,
int is_ws,
2383 __kmp_assert_valid_gtid(gtid);
2384 kmp_info_t *thr = __kmp_threads[gtid];
2385 kmp_int32 nth = thr->th.th_team_nproc;
2386 __kmpc_taskgroup(loc, gtid);
2389 (
"__kmpc_reduction_modifier_init: T#%d, tg %p, exiting nth=1\n",
2390 gtid, thr->th.th_current_task->td_taskgroup));
2391 return (
void *)thr->th.th_current_task->td_taskgroup;
2393 kmp_team_t *team = thr->th.th_team;
2395 kmp_taskgroup_t *tg;
2396 reduce_data = KMP_ATOMIC_LD_RLX(&team->t.t_tg_reduce_data[is_ws]);
2397 if (reduce_data == NULL &&
2398 __kmp_atomic_compare_store(&team->t.t_tg_reduce_data[is_ws], reduce_data,
2401 KMP_DEBUG_ASSERT(reduce_data == NULL);
2403 tg = (kmp_taskgroup_t *)__kmp_task_reduction_init<T>(gtid, num, data);
2407 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[0]) == 0);
2408 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[1]) == 0);
2409 KMP_ATOMIC_ST_REL(&team->t.t_tg_reduce_data[is_ws], reduce_data);
2412 (reduce_data = KMP_ATOMIC_LD_ACQ(&team->t.t_tg_reduce_data[is_ws])) ==
2416 KMP_DEBUG_ASSERT(reduce_data > (
void *)1);
2417 tg = thr->th.th_current_task->td_taskgroup;
2418 __kmp_task_reduction_init_copy<T>(thr, num, data, tg, reduce_data);
2440 int num,
void *data) {
2441 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2461 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2474 __kmpc_end_taskgroup(loc, gtid);
2478 void __kmpc_taskgroup(
ident_t *loc,
int gtid) {
2479 __kmp_assert_valid_gtid(gtid);
2480 kmp_info_t *thread = __kmp_threads[gtid];
2481 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2482 kmp_taskgroup_t *tg_new =
2483 (kmp_taskgroup_t *)__kmp_thread_malloc(thread,
sizeof(kmp_taskgroup_t));
2484 KA_TRACE(10, (
"__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new));
2485 KMP_ATOMIC_ST_RLX(&tg_new->count, 0);
2486 KMP_ATOMIC_ST_RLX(&tg_new->cancel_request, cancel_noreq);
2487 tg_new->parent = taskdata->td_taskgroup;
2488 tg_new->reduce_data = NULL;
2489 tg_new->reduce_num_data = 0;
2490 tg_new->gomp_data = NULL;
2491 taskdata->td_taskgroup = tg_new;
2493 #if OMPT_SUPPORT && OMPT_OPTIONAL
2494 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2495 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2497 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2498 kmp_team_t *team = thread->th.th_team;
2499 ompt_data_t my_task_data = taskdata->ompt_task_info.task_data;
2501 ompt_data_t my_parallel_data = team->t.ompt_team_info.parallel_data;
2503 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2504 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2505 &(my_task_data), codeptr);
2512 void __kmpc_end_taskgroup(
ident_t *loc,
int gtid) {
2513 __kmp_assert_valid_gtid(gtid);
2514 kmp_info_t *thread = __kmp_threads[gtid];
2515 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2516 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
2517 int thread_finished = FALSE;
2519 #if OMPT_SUPPORT && OMPT_OPTIONAL
2521 ompt_data_t my_task_data;
2522 ompt_data_t my_parallel_data;
2523 void *codeptr =
nullptr;
2524 if (UNLIKELY(ompt_enabled.enabled)) {
2525 team = thread->th.th_team;
2526 my_task_data = taskdata->ompt_task_info.task_data;
2528 my_parallel_data = team->t.ompt_team_info.parallel_data;
2529 codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2531 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2535 KA_TRACE(10, (
"__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc));
2536 KMP_DEBUG_ASSERT(taskgroup != NULL);
2537 KMP_SET_THREAD_STATE_BLOCK(TASKGROUP);
2539 if (__kmp_tasking_mode != tskm_immediate_exec) {
2541 taskdata->td_taskwait_counter += 1;
2542 taskdata->td_taskwait_ident = loc;
2543 taskdata->td_taskwait_thread = gtid + 1;
2547 void *itt_sync_obj = NULL;
2549 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2553 #if OMPT_SUPPORT && OMPT_OPTIONAL
2554 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2555 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2556 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2557 &(my_task_data), codeptr);
2561 if (!taskdata->td_flags.team_serial ||
2562 (thread->th.th_task_team != NULL &&
2563 (thread->th.th_task_team->tt.tt_found_proxy_tasks ||
2564 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered))) {
2565 kmp_flag_32<false, false> flag(
2566 RCAST(std::atomic<kmp_uint32> *, &(taskgroup->count)), 0U);
2567 while (KMP_ATOMIC_LD_ACQ(&taskgroup->count) != 0) {
2568 flag.execute_tasks(thread, gtid, FALSE,
2569 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2570 __kmp_task_stealing_constraint);
2573 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2575 #if OMPT_SUPPORT && OMPT_OPTIONAL
2576 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2577 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2578 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2579 &(my_task_data), codeptr);
2584 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2585 KMP_FSYNC_ACQUIRED(taskdata);
2588 KMP_DEBUG_ASSERT(taskgroup->count == 0);
2590 if (taskgroup->reduce_data != NULL &&
2591 !taskgroup->gomp_data) {
2594 kmp_team_t *t = thread->th.th_team;
2598 if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[0])) != NULL &&
2601 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[0]);
2602 if (cnt == thread->th.th_team_nproc - 1) {
2605 __kmp_task_reduction_fini(thread, taskgroup);
2608 __kmp_thread_free(thread, reduce_data);
2609 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[0], NULL);
2610 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[0], 0);
2614 __kmp_task_reduction_clean(thread, taskgroup);
2616 }
else if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[1])) !=
2620 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[1]);
2621 if (cnt == thread->th.th_team_nproc - 1) {
2623 __kmp_task_reduction_fini(thread, taskgroup);
2626 __kmp_thread_free(thread, reduce_data);
2627 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[1], NULL);
2628 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[1], 0);
2632 __kmp_task_reduction_clean(thread, taskgroup);
2636 __kmp_task_reduction_fini(thread, taskgroup);
2640 taskdata->td_taskgroup = taskgroup->parent;
2641 __kmp_thread_free(thread, taskgroup);
2643 KA_TRACE(10, (
"__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n",
2646 #if OMPT_SUPPORT && OMPT_OPTIONAL
2647 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2648 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2649 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2650 &(my_task_data), codeptr);
2656 static kmp_task_t *__kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid,
2657 kmp_task_team_t *task_team,
2658 kmp_int32 is_constrained) {
2660 kmp_taskdata_t *taskdata;
2661 kmp_thread_data_t *thread_data;
2664 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2665 KMP_DEBUG_ASSERT(task_team->tt.tt_threads_data !=
2668 thread_data = &task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
2670 KA_TRACE(10, (
"__kmp_remove_my_task(enter): T#%d ntasks=%d head=%u tail=%u\n",
2671 gtid, thread_data->td.td_deque_ntasks,
2672 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2674 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2676 (
"__kmp_remove_my_task(exit #1): T#%d No tasks to remove: "
2677 "ntasks=%d head=%u tail=%u\n",
2678 gtid, thread_data->td.td_deque_ntasks,
2679 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2683 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
2685 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2686 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2688 (
"__kmp_remove_my_task(exit #2): T#%d No tasks to remove: "
2689 "ntasks=%d head=%u tail=%u\n",
2690 gtid, thread_data->td.td_deque_ntasks,
2691 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2695 tail = (thread_data->td.td_deque_tail - 1) &
2696 TASK_DEQUE_MASK(thread_data->td);
2697 taskdata = thread_data->td.td_deque[tail];
2699 if (!__kmp_task_is_allowed(gtid, is_constrained, taskdata,
2700 thread->th.th_current_task)) {
2702 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2704 (
"__kmp_remove_my_task(exit #3): T#%d TSC blocks tail task: "
2705 "ntasks=%d head=%u tail=%u\n",
2706 gtid, thread_data->td.td_deque_ntasks,
2707 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2711 thread_data->td.td_deque_tail = tail;
2712 TCW_4(thread_data->td.td_deque_ntasks, thread_data->td.td_deque_ntasks - 1);
2714 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2716 KA_TRACE(10, (
"__kmp_remove_my_task(exit #4): T#%d task %p removed: "
2717 "ntasks=%d head=%u tail=%u\n",
2718 gtid, taskdata, thread_data->td.td_deque_ntasks,
2719 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2721 task = KMP_TASKDATA_TO_TASK(taskdata);
2728 static kmp_task_t *__kmp_steal_task(kmp_info_t *victim_thr, kmp_int32 gtid,
2729 kmp_task_team_t *task_team,
2730 std::atomic<kmp_int32> *unfinished_threads,
2731 int *thread_finished,
2732 kmp_int32 is_constrained) {
2734 kmp_taskdata_t *taskdata;
2735 kmp_taskdata_t *current;
2736 kmp_thread_data_t *victim_td, *threads_data;
2738 kmp_int32 victim_tid;
2740 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2742 threads_data = task_team->tt.tt_threads_data;
2743 KMP_DEBUG_ASSERT(threads_data != NULL);
2745 victim_tid = victim_thr->th.th_info.ds.ds_tid;
2746 victim_td = &threads_data[victim_tid];
2748 KA_TRACE(10, (
"__kmp_steal_task(enter): T#%d try to steal from T#%d: "
2749 "task_team=%p ntasks=%d head=%u tail=%u\n",
2750 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
2751 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
2752 victim_td->td.td_deque_tail));
2754 if (TCR_4(victim_td->td.td_deque_ntasks) == 0) {
2755 KA_TRACE(10, (
"__kmp_steal_task(exit #1): T#%d could not steal from T#%d: "
2756 "task_team=%p ntasks=%d head=%u tail=%u\n",
2757 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
2758 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
2759 victim_td->td.td_deque_tail));
2763 __kmp_acquire_bootstrap_lock(&victim_td->td.td_deque_lock);
2765 int ntasks = TCR_4(victim_td->td.td_deque_ntasks);
2768 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2769 KA_TRACE(10, (
"__kmp_steal_task(exit #2): T#%d could not steal from T#%d: "
2770 "task_team=%p ntasks=%d head=%u tail=%u\n",
2771 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2772 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2776 KMP_DEBUG_ASSERT(victim_td->td.td_deque != NULL);
2777 current = __kmp_threads[gtid]->th.th_current_task;
2778 taskdata = victim_td->td.td_deque[victim_td->td.td_deque_head];
2779 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
2781 victim_td->td.td_deque_head =
2782 (victim_td->td.td_deque_head + 1) & TASK_DEQUE_MASK(victim_td->td);
2784 if (!task_team->tt.tt_untied_task_encountered) {
2786 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2787 KA_TRACE(10, (
"__kmp_steal_task(exit #3): T#%d could not steal from "
2788 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
2789 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2790 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2795 target = victim_td->td.td_deque_head;
2797 for (i = 1; i < ntasks; ++i) {
2798 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
2799 taskdata = victim_td->td.td_deque[target];
2800 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
2806 if (taskdata == NULL) {
2808 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2809 KA_TRACE(10, (
"__kmp_steal_task(exit #4): T#%d could not steal from "
2810 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
2811 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2812 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2816 for (i = i + 1; i < ntasks; ++i) {
2818 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
2819 victim_td->td.td_deque[prev] = victim_td->td.td_deque[target];
2823 victim_td->td.td_deque_tail ==
2824 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(victim_td->td)));
2825 victim_td->td.td_deque_tail = target;
2827 if (*thread_finished) {
2834 KMP_ATOMIC_INC(unfinished_threads);
2837 (
"__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n",
2838 gtid, count + 1, task_team));
2839 *thread_finished = FALSE;
2841 TCW_4(victim_td->td.td_deque_ntasks, ntasks - 1);
2843 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2847 (
"__kmp_steal_task(exit #5): T#%d stole task %p from T#%d: "
2848 "task_team=%p ntasks=%d head=%u tail=%u\n",
2849 gtid, taskdata, __kmp_gtid_from_thread(victim_thr), task_team,
2850 ntasks, victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2852 task = KMP_TASKDATA_TO_TASK(taskdata);
2866 static inline int __kmp_execute_tasks_template(
2867 kmp_info_t *thread, kmp_int32 gtid, C *flag,
int final_spin,
2868 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
2869 kmp_int32 is_constrained) {
2870 kmp_task_team_t *task_team = thread->th.th_task_team;
2871 kmp_thread_data_t *threads_data;
2873 kmp_info_t *other_thread;
2874 kmp_taskdata_t *current_task = thread->th.th_current_task;
2875 std::atomic<kmp_int32> *unfinished_threads;
2876 kmp_int32 nthreads, victim_tid = -2, use_own_tasks = 1, new_victim = 0,
2877 tid = thread->th.th_info.ds.ds_tid;
2879 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2880 KMP_DEBUG_ASSERT(thread == __kmp_threads[gtid]);
2882 if (task_team == NULL || current_task == NULL)
2885 KA_TRACE(15, (
"__kmp_execute_tasks_template(enter): T#%d final_spin=%d "
2886 "*thread_finished=%d\n",
2887 gtid, final_spin, *thread_finished));
2889 thread->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
2890 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
2892 KMP_DEBUG_ASSERT(threads_data != NULL);
2894 nthreads = task_team->tt.tt_nproc;
2895 unfinished_threads = &(task_team->tt.tt_unfinished_threads);
2896 KMP_DEBUG_ASSERT(nthreads > 1 || task_team->tt.tt_found_proxy_tasks ||
2897 task_team->tt.tt_hidden_helper_task_encountered);
2898 KMP_DEBUG_ASSERT(*unfinished_threads >= 0);
2904 if (use_own_tasks) {
2905 task = __kmp_remove_my_task(thread, gtid, task_team, is_constrained);
2907 if ((task == NULL) && (nthreads > 1)) {
2911 if (victim_tid == -2) {
2912 victim_tid = threads_data[tid].td.td_deque_last_stolen;
2915 other_thread = threads_data[victim_tid].td.td_thr;
2917 if (victim_tid != -1) {
2919 }
else if (!new_victim) {
2925 victim_tid = __kmp_get_random(thread) % (nthreads - 1);
2926 if (victim_tid >= tid) {
2930 other_thread = threads_data[victim_tid].td.td_thr;
2940 if ((__kmp_tasking_mode == tskm_task_teams) &&
2941 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) &&
2942 (TCR_PTR(CCAST(
void *, other_thread->th.th_sleep_loc)) !=
2945 __kmp_null_resume_wrapper(other_thread);
2958 task = __kmp_steal_task(other_thread, gtid, task_team,
2959 unfinished_threads, thread_finished,
2963 if (threads_data[tid].td.td_deque_last_stolen != victim_tid) {
2964 threads_data[tid].td.td_deque_last_stolen = victim_tid;
2971 KMP_CHECK_UPDATE(threads_data[tid].td.td_deque_last_stolen, -1);
2980 #if USE_ITT_BUILD && USE_ITT_NOTIFY
2981 if (__itt_sync_create_ptr || KMP_ITT_DEBUG) {
2982 if (itt_sync_obj == NULL) {
2984 itt_sync_obj = __kmp_itt_barrier_object(gtid, bs_forkjoin_barrier);
2986 __kmp_itt_task_starting(itt_sync_obj);
2989 __kmp_invoke_task(gtid, task, current_task);
2991 if (itt_sync_obj != NULL)
2992 __kmp_itt_task_finished(itt_sync_obj);
2999 if (flag == NULL || (!final_spin && flag->done_check())) {
3002 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3006 if (thread->th.th_task_team == NULL) {
3009 KMP_YIELD(__kmp_library == library_throughput);
3012 if (!use_own_tasks && TCR_4(threads_data[tid].td.td_deque_ntasks) != 0) {
3013 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d stolen task spawned "
3014 "other tasks, restart\n",
3025 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks) == 0) {
3029 if (!*thread_finished) {
3031 kmp_int32 count = -1 +
3033 KMP_ATOMIC_DEC(unfinished_threads);
3034 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d dec "
3035 "unfinished_threads to %d task_team=%p\n",
3036 gtid, count, task_team));
3037 *thread_finished = TRUE;
3045 if (flag != NULL && flag->done_check()) {
3048 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3056 if (thread->th.th_task_team == NULL) {
3058 (
"__kmp_execute_tasks_template: T#%d no more tasks\n", gtid));
3064 if (nthreads == 1 &&
3065 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks))
3069 (
"__kmp_execute_tasks_template: T#%d can't find work\n", gtid));
3075 template <
bool C,
bool S>
3076 int __kmp_execute_tasks_32(
3077 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32<C, S> *flag,
int final_spin,
3078 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3079 kmp_int32 is_constrained) {
3080 return __kmp_execute_tasks_template(
3081 thread, gtid, flag, final_spin,
3082 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3085 template <
bool C,
bool S>
3086 int __kmp_execute_tasks_64(
3087 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64<C, S> *flag,
int final_spin,
3088 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3089 kmp_int32 is_constrained) {
3090 return __kmp_execute_tasks_template(
3091 thread, gtid, flag, final_spin,
3092 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3095 template <
bool C,
bool S>
3096 int __kmp_atomic_execute_tasks_64(
3097 kmp_info_t *thread, kmp_int32 gtid, kmp_atomic_flag_64<C, S> *flag,
3098 int final_spin,
int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3099 kmp_int32 is_constrained) {
3100 return __kmp_execute_tasks_template(
3101 thread, gtid, flag, final_spin,
3102 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3105 int __kmp_execute_tasks_oncore(
3106 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag,
int final_spin,
3107 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3108 kmp_int32 is_constrained) {
3109 return __kmp_execute_tasks_template(
3110 thread, gtid, flag, final_spin,
3111 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3115 __kmp_execute_tasks_32<false, false>(kmp_info_t *, kmp_int32,
3116 kmp_flag_32<false, false> *,
int,
3117 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3119 template int __kmp_execute_tasks_64<false, true>(kmp_info_t *, kmp_int32,
3120 kmp_flag_64<false, true> *,
3122 int *USE_ITT_BUILD_ARG(
void *),
3125 template int __kmp_execute_tasks_64<true, false>(kmp_info_t *, kmp_int32,
3126 kmp_flag_64<true, false> *,
3128 int *USE_ITT_BUILD_ARG(
void *),
3131 template int __kmp_atomic_execute_tasks_64<false, true>(
3132 kmp_info_t *, kmp_int32, kmp_atomic_flag_64<false, true> *,
int,
3133 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3135 template int __kmp_atomic_execute_tasks_64<true, false>(
3136 kmp_info_t *, kmp_int32, kmp_atomic_flag_64<true, false> *,
int,
3137 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3142 static void __kmp_enable_tasking(kmp_task_team_t *task_team,
3143 kmp_info_t *this_thr) {
3144 kmp_thread_data_t *threads_data;
3145 int nthreads, i, is_init_thread;
3147 KA_TRACE(10, (
"__kmp_enable_tasking(enter): T#%d\n",
3148 __kmp_gtid_from_thread(this_thr)));
3150 KMP_DEBUG_ASSERT(task_team != NULL);
3151 KMP_DEBUG_ASSERT(this_thr->th.th_team != NULL);
3153 nthreads = task_team->tt.tt_nproc;
3154 KMP_DEBUG_ASSERT(nthreads > 0);
3155 KMP_DEBUG_ASSERT(nthreads == this_thr->th.th_team->t.t_nproc);
3158 is_init_thread = __kmp_realloc_task_threads_data(this_thr, task_team);
3160 if (!is_init_thread) {
3164 (
"__kmp_enable_tasking(exit): T#%d: threads array already set up.\n",
3165 __kmp_gtid_from_thread(this_thr)));
3168 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
3169 KMP_DEBUG_ASSERT(threads_data != NULL);
3171 if (__kmp_tasking_mode == tskm_task_teams &&
3172 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME)) {
3176 for (i = 0; i < nthreads; i++) {
3178 kmp_info_t *thread = threads_data[i].td.td_thr;
3180 if (i == this_thr->th.th_info.ds.ds_tid) {
3189 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3191 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d waking up thread T#%d\n",
3192 __kmp_gtid_from_thread(this_thr),
3193 __kmp_gtid_from_thread(thread)));
3194 __kmp_null_resume_wrapper(thread);
3196 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d don't wake up thread T#%d\n",
3197 __kmp_gtid_from_thread(this_thr),
3198 __kmp_gtid_from_thread(thread)));
3203 KA_TRACE(10, (
"__kmp_enable_tasking(exit): T#%d\n",
3204 __kmp_gtid_from_thread(this_thr)));
3237 static kmp_task_team_t *__kmp_free_task_teams =
3240 kmp_bootstrap_lock_t __kmp_task_team_lock =
3241 KMP_BOOTSTRAP_LOCK_INITIALIZER(__kmp_task_team_lock);
3248 static void __kmp_alloc_task_deque(kmp_info_t *thread,
3249 kmp_thread_data_t *thread_data) {
3250 __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
3251 KMP_DEBUG_ASSERT(thread_data->td.td_deque == NULL);
3254 thread_data->td.td_deque_last_stolen = -1;
3256 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == 0);
3257 KMP_DEBUG_ASSERT(thread_data->td.td_deque_head == 0);
3258 KMP_DEBUG_ASSERT(thread_data->td.td_deque_tail == 0);
3262 (
"__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n",
3263 __kmp_gtid_from_thread(thread), INITIAL_TASK_DEQUE_SIZE, thread_data));
3267 thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
3268 INITIAL_TASK_DEQUE_SIZE *
sizeof(kmp_taskdata_t *));
3269 thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
3275 static void __kmp_free_task_deque(kmp_thread_data_t *thread_data) {
3276 if (thread_data->td.td_deque != NULL) {
3277 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3278 TCW_4(thread_data->td.td_deque_ntasks, 0);
3279 __kmp_free(thread_data->td.td_deque);
3280 thread_data->td.td_deque = NULL;
3281 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3284 #ifdef BUILD_TIED_TASK_STACK
3286 if (thread_data->td.td_susp_tied_tasks.ts_entries != TASK_STACK_EMPTY) {
3287 __kmp_free_task_stack(__kmp_thread_from_gtid(gtid), thread_data);
3299 static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
3300 kmp_task_team_t *task_team) {
3301 kmp_thread_data_t **threads_data_p;
3302 kmp_int32 nthreads, maxthreads;
3303 int is_init_thread = FALSE;
3305 if (TCR_4(task_team->tt.tt_found_tasks)) {
3310 threads_data_p = &task_team->tt.tt_threads_data;
3311 nthreads = task_team->tt.tt_nproc;
3312 maxthreads = task_team->tt.tt_max_threads;
3317 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3319 if (!TCR_4(task_team->tt.tt_found_tasks)) {
3321 kmp_team_t *team = thread->th.th_team;
3324 is_init_thread = TRUE;
3325 if (maxthreads < nthreads) {
3327 if (*threads_data_p != NULL) {
3328 kmp_thread_data_t *old_data = *threads_data_p;
3329 kmp_thread_data_t *new_data = NULL;
3333 (
"__kmp_realloc_task_threads_data: T#%d reallocating "
3334 "threads data for task_team %p, new_size = %d, old_size = %d\n",
3335 __kmp_gtid_from_thread(thread), task_team, nthreads, maxthreads));
3340 new_data = (kmp_thread_data_t *)__kmp_allocate(
3341 nthreads *
sizeof(kmp_thread_data_t));
3343 KMP_MEMCPY_S((
void *)new_data, nthreads *
sizeof(kmp_thread_data_t),
3344 (
void *)old_data, maxthreads *
sizeof(kmp_thread_data_t));
3346 #ifdef BUILD_TIED_TASK_STACK
3348 for (i = maxthreads; i < nthreads; i++) {
3349 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3350 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3354 (*threads_data_p) = new_data;
3355 __kmp_free(old_data);
3357 KE_TRACE(10, (
"__kmp_realloc_task_threads_data: T#%d allocating "
3358 "threads data for task_team %p, size = %d\n",
3359 __kmp_gtid_from_thread(thread), task_team, nthreads));
3363 *threads_data_p = (kmp_thread_data_t *)__kmp_allocate(
3364 nthreads *
sizeof(kmp_thread_data_t));
3365 #ifdef BUILD_TIED_TASK_STACK
3367 for (i = 0; i < nthreads; i++) {
3368 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3369 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3373 task_team->tt.tt_max_threads = nthreads;
3376 KMP_DEBUG_ASSERT(*threads_data_p != NULL);
3380 for (i = 0; i < nthreads; i++) {
3381 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3382 thread_data->td.td_thr = team->t.t_threads[i];
3384 if (thread_data->td.td_deque_last_stolen >= nthreads) {
3388 thread_data->td.td_deque_last_stolen = -1;
3393 TCW_SYNC_4(task_team->tt.tt_found_tasks, TRUE);
3396 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3397 return is_init_thread;
3403 static void __kmp_free_task_threads_data(kmp_task_team_t *task_team) {
3404 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3405 if (task_team->tt.tt_threads_data != NULL) {
3407 for (i = 0; i < task_team->tt.tt_max_threads; i++) {
3408 __kmp_free_task_deque(&task_team->tt.tt_threads_data[i]);
3410 __kmp_free(task_team->tt.tt_threads_data);
3411 task_team->tt.tt_threads_data = NULL;
3413 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3420 static kmp_task_team_t *__kmp_allocate_task_team(kmp_info_t *thread,
3422 kmp_task_team_t *task_team = NULL;
3425 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d entering; team = %p\n",
3426 (thread ? __kmp_gtid_from_thread(thread) : -1), team));
3428 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3430 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3431 if (__kmp_free_task_teams != NULL) {
3432 task_team = __kmp_free_task_teams;
3433 TCW_PTR(__kmp_free_task_teams, task_team->tt.tt_next);
3434 task_team->tt.tt_next = NULL;
3436 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3439 if (task_team == NULL) {
3440 KE_TRACE(10, (
"__kmp_allocate_task_team: T#%d allocating "
3441 "task team for team %p\n",
3442 __kmp_gtid_from_thread(thread), team));
3445 task_team = (kmp_task_team_t *)__kmp_allocate(
sizeof(kmp_task_team_t));
3446 __kmp_init_bootstrap_lock(&task_team->tt.tt_threads_lock);
3447 #if USE_ITT_BUILD && USE_ITT_NOTIFY && KMP_DEBUG
3450 __itt_suppress_mark_range(
3451 __itt_suppress_range, __itt_suppress_threading_errors,
3452 &task_team->tt.tt_found_tasks,
sizeof(task_team->tt.tt_found_tasks));
3453 __itt_suppress_mark_range(__itt_suppress_range,
3454 __itt_suppress_threading_errors,
3455 CCAST(kmp_uint32 *, &task_team->tt.tt_active),
3456 sizeof(task_team->tt.tt_active));
3464 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3465 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3466 task_team->tt.tt_nproc = nthreads = team->t.t_nproc;
3468 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads, nthreads);
3469 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
3470 TCW_4(task_team->tt.tt_active, TRUE);
3472 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d exiting; task_team = %p "
3473 "unfinished_threads init'd to %d\n",
3474 (thread ? __kmp_gtid_from_thread(thread) : -1), task_team,
3475 KMP_ATOMIC_LD_RLX(&task_team->tt.tt_unfinished_threads)));
3482 void __kmp_free_task_team(kmp_info_t *thread, kmp_task_team_t *task_team) {
3483 KA_TRACE(20, (
"__kmp_free_task_team: T#%d task_team = %p\n",
3484 thread ? __kmp_gtid_from_thread(thread) : -1, task_team));
3487 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3489 KMP_DEBUG_ASSERT(task_team->tt.tt_next == NULL);
3490 task_team->tt.tt_next = __kmp_free_task_teams;
3491 TCW_PTR(__kmp_free_task_teams, task_team);
3493 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3501 void __kmp_reap_task_teams(
void) {
3502 kmp_task_team_t *task_team;
3504 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3506 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3507 while ((task_team = __kmp_free_task_teams) != NULL) {
3508 __kmp_free_task_teams = task_team->tt.tt_next;
3509 task_team->tt.tt_next = NULL;
3512 if (task_team->tt.tt_threads_data != NULL) {
3513 __kmp_free_task_threads_data(task_team);
3515 __kmp_free(task_team);
3517 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3524 void __kmp_wait_to_unref_task_teams(
void) {
3529 KMP_INIT_YIELD(spins);
3537 for (thread = CCAST(kmp_info_t *, __kmp_thread_pool); thread != NULL;
3538 thread = thread->th.th_next_pool) {
3542 if (TCR_PTR(thread->th.th_task_team) == NULL) {
3543 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n",
3544 __kmp_gtid_from_thread(thread)));
3549 if (!__kmp_is_thread_alive(thread, &exit_val)) {
3550 thread->th.th_task_team = NULL;
3557 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: Waiting for T#%d to "
3558 "unreference task_team\n",
3559 __kmp_gtid_from_thread(thread)));
3561 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
3564 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3568 (
"__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n",
3569 __kmp_gtid_from_thread(thread), __kmp_gtid_from_thread(thread)));
3570 __kmp_null_resume_wrapper(thread);
3579 KMP_YIELD_OVERSUB_ELSE_SPIN(spins);
3585 void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team,
int always) {
3586 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3592 if (team->t.t_task_team[this_thr->th.th_task_state] == NULL &&
3593 (always || team->t.t_nproc > 1)) {
3594 team->t.t_task_team[this_thr->th.th_task_state] =
3595 __kmp_allocate_task_team(this_thr, team);
3596 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d created new task_team %p"
3597 " for team %d at parity=%d\n",
3598 __kmp_gtid_from_thread(this_thr),
3599 team->t.t_task_team[this_thr->th.th_task_state], team->t.t_id,
3600 this_thr->th.th_task_state));
3610 if (team->t.t_nproc > 1) {
3611 int other_team = 1 - this_thr->th.th_task_state;
3612 KMP_DEBUG_ASSERT(other_team >= 0 && other_team < 2);
3613 if (team->t.t_task_team[other_team] == NULL) {
3614 team->t.t_task_team[other_team] =
3615 __kmp_allocate_task_team(this_thr, team);
3616 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d created second new "
3617 "task_team %p for team %d at parity=%d\n",
3618 __kmp_gtid_from_thread(this_thr),
3619 team->t.t_task_team[other_team], team->t.t_id, other_team));
3622 kmp_task_team_t *task_team = team->t.t_task_team[other_team];
3623 if (!task_team->tt.tt_active ||
3624 team->t.t_nproc != task_team->tt.tt_nproc) {
3625 TCW_4(task_team->tt.tt_nproc, team->t.t_nproc);
3626 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3627 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3628 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads,
3630 TCW_4(task_team->tt.tt_active, TRUE);
3634 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d reset next task_team "
3635 "%p for team %d at parity=%d\n",
3636 __kmp_gtid_from_thread(this_thr),
3637 team->t.t_task_team[other_team], team->t.t_id, other_team));
3645 if (this_thr == __kmp_hidden_helper_main_thread) {
3646 for (
int i = 0; i < 2; ++i) {
3647 kmp_task_team_t *task_team = team->t.t_task_team[i];
3648 if (KMP_TASKING_ENABLED(task_team)) {
3651 __kmp_enable_tasking(task_team, this_thr);
3652 for (
int j = 0; j < task_team->tt.tt_nproc; ++j) {
3653 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[j];
3654 if (thread_data->td.td_deque == NULL) {
3655 __kmp_alloc_task_deque(__kmp_hidden_helper_threads[j], thread_data);
3665 void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team) {
3666 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3670 this_thr->th.th_task_state = (kmp_uint8)(1 - this_thr->th.th_task_state);
3674 TCW_PTR(this_thr->th.th_task_team,
3675 team->t.t_task_team[this_thr->th.th_task_state]);
3677 (
"__kmp_task_team_sync: Thread T#%d task team switched to task_team "
3678 "%p from Team #%d (parity=%d)\n",
3679 __kmp_gtid_from_thread(this_thr), this_thr->th.th_task_team,
3680 team->t.t_id, this_thr->th.th_task_state));
3690 void __kmp_task_team_wait(
3691 kmp_info_t *this_thr,
3692 kmp_team_t *team USE_ITT_BUILD_ARG(
void *itt_sync_obj),
int wait) {
3693 kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state];
3695 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3696 KMP_DEBUG_ASSERT(task_team == this_thr->th.th_task_team);
3698 if ((task_team != NULL) && KMP_TASKING_ENABLED(task_team)) {
3700 KA_TRACE(20, (
"__kmp_task_team_wait: Primary T#%d waiting for all tasks "
3701 "(for unfinished_threads to reach 0) on task_team = %p\n",
3702 __kmp_gtid_from_thread(this_thr), task_team));
3706 kmp_flag_32<false, false> flag(
3707 RCAST(std::atomic<kmp_uint32> *,
3708 &task_team->tt.tt_unfinished_threads),
3710 flag.wait(this_thr, TRUE USE_ITT_BUILD_ARG(itt_sync_obj));
3716 (
"__kmp_task_team_wait: Primary T#%d deactivating task_team %p: "
3717 "setting active to false, setting local and team's pointer to NULL\n",
3718 __kmp_gtid_from_thread(this_thr), task_team));
3719 KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1 ||
3720 task_team->tt.tt_found_proxy_tasks == TRUE);
3721 TCW_SYNC_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3722 KMP_CHECK_UPDATE(task_team->tt.tt_untied_task_encountered, 0);
3723 TCW_SYNC_4(task_team->tt.tt_active, FALSE);
3726 TCW_PTR(this_thr->th.th_task_team, NULL);
3735 void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread,
int gtid) {
3736 std::atomic<kmp_uint32> *spin = RCAST(
3737 std::atomic<kmp_uint32> *,
3738 &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads);
3740 KMP_DEBUG_ASSERT(__kmp_tasking_mode == tskm_extra_barrier);
3743 KMP_FSYNC_SPIN_INIT(spin, NULL);
3745 kmp_flag_32<false, false> spin_flag(spin, 0U);
3746 while (!spin_flag.execute_tasks(thread, gtid, TRUE,
3747 &flag USE_ITT_BUILD_ARG(NULL), 0)) {
3750 KMP_FSYNC_SPIN_PREPARE(RCAST(
void *, spin));
3753 if (TCR_4(__kmp_global.g.g_done)) {
3754 if (__kmp_global.g.g_abort)
3755 __kmp_abort_thread();
3761 KMP_FSYNC_SPIN_ACQUIRED(RCAST(
void *, spin));
3770 static bool __kmp_give_task(kmp_info_t *thread, kmp_int32 tid, kmp_task_t *task,
3772 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
3773 kmp_task_team_t *task_team = taskdata->td_task_team;
3775 KA_TRACE(20, (
"__kmp_give_task: trying to give task %p to thread %d.\n",
3779 KMP_DEBUG_ASSERT(task_team != NULL);
3781 bool result =
false;
3782 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
3784 if (thread_data->td.td_deque == NULL) {
3788 (
"__kmp_give_task: thread %d has no queue while giving task %p.\n",
3793 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3794 TASK_DEQUE_SIZE(thread_data->td)) {
3797 (
"__kmp_give_task: queue is full while giving task %p to thread %d.\n",
3802 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
3805 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3806 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3807 TASK_DEQUE_SIZE(thread_data->td)) {
3809 __kmp_realloc_task_deque(thread, thread_data);
3814 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3816 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3817 TASK_DEQUE_SIZE(thread_data->td)) {
3818 KA_TRACE(30, (
"__kmp_give_task: queue is full while giving task %p to "
3824 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
3825 goto release_and_exit;
3827 __kmp_realloc_task_deque(thread, thread_data);
3833 thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
3835 thread_data->td.td_deque_tail =
3836 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
3837 TCW_4(thread_data->td.td_deque_ntasks,
3838 TCR_4(thread_data->td.td_deque_ntasks) + 1);
3841 KA_TRACE(30, (
"__kmp_give_task: successfully gave task %p to thread %d.\n",
3845 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3850 #define PROXY_TASK_FLAG 0x40000000
3867 static void __kmp_first_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
3868 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
3869 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3870 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
3871 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
3873 taskdata->td_flags.complete = 1;
3875 if (taskdata->td_taskgroup)
3876 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
3880 KMP_ATOMIC_OR(&taskdata->td_incomplete_child_tasks, PROXY_TASK_FLAG);
3883 static void __kmp_second_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
3885 kmp_int32 children = 0;
3889 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks);
3890 KMP_DEBUG_ASSERT(children >= 0);
3893 KMP_ATOMIC_AND(&taskdata->td_incomplete_child_tasks, ~PROXY_TASK_FLAG);
3896 static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask) {
3897 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3898 kmp_info_t *thread = __kmp_threads[gtid];
3900 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3901 KMP_DEBUG_ASSERT(taskdata->td_flags.complete ==
3906 while ((KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) &
3907 PROXY_TASK_FLAG) > 0)
3910 __kmp_release_deps(gtid, taskdata);
3911 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
3923 KMP_DEBUG_ASSERT(ptask != NULL);
3924 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3926 10, (
"__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n",
3928 __kmp_assert_valid_gtid(gtid);
3929 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3931 __kmp_first_top_half_finish_proxy(taskdata);
3932 __kmp_second_top_half_finish_proxy(taskdata);
3933 __kmp_bottom_half_finish_proxy(gtid, ptask);
3936 (
"__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n",
3940 void __kmpc_give_task(kmp_task_t *ptask, kmp_int32 start = 0) {
3941 KMP_DEBUG_ASSERT(ptask != NULL);
3942 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3946 kmp_team_t *team = taskdata->td_team;
3947 kmp_int32 nthreads = team->t.t_nproc;
3952 kmp_int32 start_k = start % nthreads;
3954 kmp_int32 k = start_k;
3958 thread = team->t.t_threads[k];
3959 k = (k + 1) % nthreads;
3965 }
while (!__kmp_give_task(thread, k, ptask, pass));
3976 KMP_DEBUG_ASSERT(ptask != NULL);
3977 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3981 (
"__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n",
3984 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3986 __kmp_first_top_half_finish_proxy(taskdata);
3988 __kmpc_give_task(ptask);
3990 __kmp_second_top_half_finish_proxy(taskdata);
3994 (
"__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n",
3998 kmp_event_t *__kmpc_task_allow_completion_event(
ident_t *loc_ref,
int gtid,
4000 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(task);
4001 if (td->td_allow_completion_event.type == KMP_EVENT_UNINITIALIZED) {
4002 td->td_allow_completion_event.type = KMP_EVENT_ALLOW_COMPLETION;
4003 td->td_allow_completion_event.ed.task = task;
4004 __kmp_init_tas_lock(&td->td_allow_completion_event.lock);
4006 return &td->td_allow_completion_event;
4009 void __kmp_fulfill_event(kmp_event_t *event) {
4010 if (event->type == KMP_EVENT_ALLOW_COMPLETION) {
4011 kmp_task_t *ptask = event->ed.task;
4012 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4013 bool detached =
false;
4014 int gtid = __kmp_get_gtid();
4019 __kmp_acquire_tas_lock(&event->lock, gtid);
4020 if (taskdata->td_flags.proxy == TASK_PROXY) {
4026 if (UNLIKELY(ompt_enabled.enabled))
4027 __ompt_task_finish(ptask, NULL, ompt_task_early_fulfill);
4030 event->type = KMP_EVENT_UNINITIALIZED;
4031 __kmp_release_tas_lock(&event->lock, gtid);
4037 if (UNLIKELY(ompt_enabled.enabled))
4038 __ompt_task_finish(ptask, NULL, ompt_task_late_fulfill);
4042 kmp_team_t *team = taskdata->td_team;
4043 kmp_info_t *thread = __kmp_get_thread();
4044 if (thread->th.th_team == team) {
4062 kmp_task_t *__kmp_task_dup_alloc(kmp_info_t *thread, kmp_task_t *task_src) {
4064 kmp_taskdata_t *taskdata;
4065 kmp_taskdata_t *taskdata_src = KMP_TASK_TO_TASKDATA(task_src);
4066 kmp_taskdata_t *parent_task = taskdata_src->td_parent;
4067 size_t shareds_offset;
4070 KA_TRACE(10, (
"__kmp_task_dup_alloc(enter): Th %p, source task %p\n", thread,
4072 KMP_DEBUG_ASSERT(taskdata_src->td_flags.proxy ==
4074 KMP_DEBUG_ASSERT(taskdata_src->td_flags.tasktype == TASK_EXPLICIT);
4075 task_size = taskdata_src->td_size_alloc;
4078 KA_TRACE(30, (
"__kmp_task_dup_alloc: Th %p, malloc size %ld\n", thread,
4081 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, task_size);
4083 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, task_size);
4085 KMP_MEMCPY(taskdata, taskdata_src, task_size);
4087 task = KMP_TASKDATA_TO_TASK(taskdata);
4090 taskdata->td_task_id = KMP_GEN_TASK_ID();
4091 if (task->shareds != NULL) {
4092 shareds_offset = (
char *)task_src->shareds - (
char *)taskdata_src;
4093 task->shareds = &((
char *)taskdata)[shareds_offset];
4094 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
4097 taskdata->td_alloc_thread = thread;
4098 taskdata->td_parent = parent_task;
4100 taskdata->td_taskgroup = parent_task->td_taskgroup;
4103 if (taskdata->td_flags.tiedness == TASK_TIED)
4104 taskdata->td_last_tied = taskdata;
4108 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
4109 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
4110 if (parent_task->td_taskgroup)
4111 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
4114 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT)
4115 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
4119 (
"__kmp_task_dup_alloc(exit): Th %p, created task %p, parent=%p\n",
4120 thread, taskdata, taskdata->td_parent));
4122 if (UNLIKELY(ompt_enabled.enabled))
4123 __ompt_task_init(taskdata, thread->th.th_info.ds.ds_gtid);
4132 typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32);
4134 KMP_BUILD_ASSERT(
sizeof(
long) == 4 ||
sizeof(
long) == 8);
4139 class kmp_taskloop_bounds_t {
4141 const kmp_taskdata_t *taskdata;
4142 size_t lower_offset;
4143 size_t upper_offset;
4146 kmp_taskloop_bounds_t(kmp_task_t *_task, kmp_uint64 *lb, kmp_uint64 *ub)
4147 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(task)),
4148 lower_offset((char *)lb - (char *)task),
4149 upper_offset((char *)ub - (char *)task) {
4150 KMP_DEBUG_ASSERT((
char *)lb > (
char *)_task);
4151 KMP_DEBUG_ASSERT((
char *)ub > (
char *)_task);
4153 kmp_taskloop_bounds_t(kmp_task_t *_task,
const kmp_taskloop_bounds_t &bounds)
4154 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(_task)),
4155 lower_offset(bounds.lower_offset), upper_offset(bounds.upper_offset) {}
4156 size_t get_lower_offset()
const {
return lower_offset; }
4157 size_t get_upper_offset()
const {
return upper_offset; }
4158 kmp_uint64 get_lb()
const {
4160 #if defined(KMP_GOMP_COMPAT)
4162 if (!taskdata->td_flags.native) {
4163 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4166 if (taskdata->td_size_loop_bounds == 4) {
4167 kmp_int32 *lb = RCAST(kmp_int32 *, task->shareds);
4168 retval = (kmp_int64)*lb;
4170 kmp_int64 *lb = RCAST(kmp_int64 *, task->shareds);
4171 retval = (kmp_int64)*lb;
4176 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4180 kmp_uint64 get_ub()
const {
4182 #if defined(KMP_GOMP_COMPAT)
4184 if (!taskdata->td_flags.native) {
4185 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4188 if (taskdata->td_size_loop_bounds == 4) {
4189 kmp_int32 *ub = RCAST(kmp_int32 *, task->shareds) + 1;
4190 retval = (kmp_int64)*ub;
4192 kmp_int64 *ub = RCAST(kmp_int64 *, task->shareds) + 1;
4193 retval = (kmp_int64)*ub;
4197 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4201 void set_lb(kmp_uint64 lb) {
4202 #if defined(KMP_GOMP_COMPAT)
4204 if (!taskdata->td_flags.native) {
4205 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4208 if (taskdata->td_size_loop_bounds == 4) {
4209 kmp_uint32 *lower = RCAST(kmp_uint32 *, task->shareds);
4210 *lower = (kmp_uint32)lb;
4212 kmp_uint64 *lower = RCAST(kmp_uint64 *, task->shareds);
4213 *lower = (kmp_uint64)lb;
4217 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4220 void set_ub(kmp_uint64 ub) {
4221 #if defined(KMP_GOMP_COMPAT)
4223 if (!taskdata->td_flags.native) {
4224 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4227 if (taskdata->td_size_loop_bounds == 4) {
4228 kmp_uint32 *upper = RCAST(kmp_uint32 *, task->shareds) + 1;
4229 *upper = (kmp_uint32)ub;
4231 kmp_uint64 *upper = RCAST(kmp_uint64 *, task->shareds) + 1;
4232 *upper = (kmp_uint64)ub;
4236 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4257 void __kmp_taskloop_linear(
ident_t *loc,
int gtid, kmp_task_t *task,
4258 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4259 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
4260 kmp_uint64 grainsize, kmp_uint64 extras,
4261 kmp_int64 last_chunk, kmp_uint64 tc,
4267 KMP_TIME_PARTITIONED_BLOCK(OMP_taskloop_scheduling);
4268 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
4270 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4271 kmp_uint64 lower = task_bounds.get_lb();
4272 kmp_uint64 upper = task_bounds.get_ub();
4274 kmp_info_t *thread = __kmp_threads[gtid];
4275 kmp_taskdata_t *current_task = thread->th.th_current_task;
4276 kmp_task_t *next_task;
4277 kmp_int32 lastpriv = 0;
4279 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4280 (last_chunk < 0 ? last_chunk : extras));
4281 KMP_DEBUG_ASSERT(num_tasks > extras);
4282 KMP_DEBUG_ASSERT(num_tasks > 0);
4283 KA_TRACE(20, (
"__kmp_taskloop_linear: T#%d: %lld tasks, grainsize %lld, "
4284 "extras %lld, last_chunk %lld, i=%lld,%lld(%d)%lld, dup %p\n",
4285 gtid, num_tasks, grainsize, extras, last_chunk, lower, upper,
4286 ub_glob, st, task_dup));
4289 for (i = 0; i < num_tasks; ++i) {
4290 kmp_uint64 chunk_minus_1;
4292 chunk_minus_1 = grainsize - 1;
4294 chunk_minus_1 = grainsize;
4297 upper = lower + st * chunk_minus_1;
4301 if (i == num_tasks - 1) {
4304 KMP_DEBUG_ASSERT(upper == *ub);
4305 if (upper == ub_glob)
4307 }
else if (st > 0) {
4308 KMP_DEBUG_ASSERT((kmp_uint64)st > *ub - upper);
4309 if ((kmp_uint64)st > ub_glob - upper)
4312 KMP_DEBUG_ASSERT(upper + st < *ub);
4313 if (upper - ub_glob < (kmp_uint64)(-st))
4317 next_task = __kmp_task_dup_alloc(thread, task);
4318 kmp_taskdata_t *next_taskdata = KMP_TASK_TO_TASKDATA(next_task);
4319 kmp_taskloop_bounds_t next_task_bounds =
4320 kmp_taskloop_bounds_t(next_task, task_bounds);
4323 next_task_bounds.set_lb(lower);
4324 if (next_taskdata->td_flags.native) {
4325 next_task_bounds.set_ub(upper + (st > 0 ? 1 : -1));
4327 next_task_bounds.set_ub(upper);
4329 if (ptask_dup != NULL)
4331 ptask_dup(next_task, task, lastpriv);
4333 (
"__kmp_taskloop_linear: T#%d; task #%llu: task %p: lower %lld, "
4334 "upper %lld stride %lld, (offsets %p %p)\n",
4335 gtid, i, next_task, lower, upper, st,
4336 next_task_bounds.get_lower_offset(),
4337 next_task_bounds.get_upper_offset()));
4339 __kmp_omp_taskloop_task(NULL, gtid, next_task,
4342 __kmp_omp_task(gtid, next_task,
true);
4347 __kmp_task_start(gtid, task, current_task);
4349 __kmp_task_finish<false>(gtid, task, current_task);
4354 typedef struct __taskloop_params {
4361 kmp_uint64 num_tasks;
4362 kmp_uint64 grainsize;
4364 kmp_int64 last_chunk;
4366 kmp_uint64 num_t_min;
4370 } __taskloop_params_t;
4372 void __kmp_taskloop_recur(
ident_t *,
int, kmp_task_t *, kmp_uint64 *,
4373 kmp_uint64 *, kmp_int64, kmp_uint64, kmp_uint64,
4374 kmp_uint64, kmp_uint64, kmp_int64, kmp_uint64,
4382 int __kmp_taskloop_task(
int gtid,
void *ptask) {
4383 __taskloop_params_t *p =
4384 (__taskloop_params_t *)((kmp_task_t *)ptask)->shareds;
4385 kmp_task_t *task = p->task;
4386 kmp_uint64 *lb = p->lb;
4387 kmp_uint64 *ub = p->ub;
4388 void *task_dup = p->task_dup;
4390 kmp_int64 st = p->st;
4391 kmp_uint64 ub_glob = p->ub_glob;
4392 kmp_uint64 num_tasks = p->num_tasks;
4393 kmp_uint64 grainsize = p->grainsize;
4394 kmp_uint64 extras = p->extras;
4395 kmp_int64 last_chunk = p->last_chunk;
4396 kmp_uint64 tc = p->tc;
4397 kmp_uint64 num_t_min = p->num_t_min;
4399 void *codeptr_ra = p->codeptr_ra;
4402 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4403 KMP_DEBUG_ASSERT(task != NULL);
4405 (
"__kmp_taskloop_task: T#%d, task %p: %lld tasks, grainsize"
4406 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
4407 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
4410 KMP_DEBUG_ASSERT(num_tasks * 2 + 1 > num_t_min);
4411 if (num_tasks > num_t_min)
4412 __kmp_taskloop_recur(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
4413 grainsize, extras, last_chunk, tc, num_t_min,
4419 __kmp_taskloop_linear(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
4420 grainsize, extras, last_chunk, tc,
4426 KA_TRACE(40, (
"__kmp_taskloop_task(exit): T#%d\n", gtid));
4448 void __kmp_taskloop_recur(
ident_t *loc,
int gtid, kmp_task_t *task,
4449 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4450 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
4451 kmp_uint64 grainsize, kmp_uint64 extras,
4452 kmp_int64 last_chunk, kmp_uint64 tc,
4453 kmp_uint64 num_t_min,
4458 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4459 KMP_DEBUG_ASSERT(task != NULL);
4460 KMP_DEBUG_ASSERT(num_tasks > num_t_min);
4462 (
"__kmp_taskloop_recur: T#%d, task %p: %lld tasks, grainsize"
4463 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
4464 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
4466 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
4467 kmp_uint64 lower = *lb;
4468 kmp_info_t *thread = __kmp_threads[gtid];
4470 kmp_task_t *next_task;
4471 size_t lower_offset =
4472 (
char *)lb - (
char *)task;
4473 size_t upper_offset =
4474 (
char *)ub - (
char *)task;
4476 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4477 (last_chunk < 0 ? last_chunk : extras));
4478 KMP_DEBUG_ASSERT(num_tasks > extras);
4479 KMP_DEBUG_ASSERT(num_tasks > 0);
4482 kmp_uint64 lb1, ub0, tc0, tc1, ext0, ext1;
4483 kmp_int64 last_chunk0 = 0, last_chunk1 = 0;
4484 kmp_uint64 gr_size0 = grainsize;
4485 kmp_uint64 n_tsk0 = num_tasks >> 1;
4486 kmp_uint64 n_tsk1 = num_tasks - n_tsk0;
4487 if (last_chunk < 0) {
4489 last_chunk1 = last_chunk;
4490 tc0 = grainsize * n_tsk0;
4492 }
else if (n_tsk0 <= extras) {
4495 ext1 = extras - n_tsk0;
4496 tc0 = gr_size0 * n_tsk0;
4501 tc1 = grainsize * n_tsk1;
4504 ub0 = lower + st * (tc0 - 1);
4508 next_task = __kmp_task_dup_alloc(thread, task);
4510 *(kmp_uint64 *)((
char *)next_task + lower_offset) = lb1;
4511 if (ptask_dup != NULL)
4512 ptask_dup(next_task, task, 0);
4517 kmp_taskdata_t *current_task = thread->th.th_current_task;
4518 thread->th.th_current_task = taskdata->td_parent;
4519 kmp_task_t *new_task =
4520 __kmpc_omp_task_alloc(loc, gtid, 1, 3 *
sizeof(
void *),
4521 sizeof(__taskloop_params_t), &__kmp_taskloop_task);
4523 thread->th.th_current_task = current_task;
4524 __taskloop_params_t *p = (__taskloop_params_t *)new_task->shareds;
4525 p->task = next_task;
4526 p->lb = (kmp_uint64 *)((
char *)next_task + lower_offset);
4527 p->ub = (kmp_uint64 *)((
char *)next_task + upper_offset);
4528 p->task_dup = task_dup;
4530 p->ub_glob = ub_glob;
4531 p->num_tasks = n_tsk1;
4532 p->grainsize = grainsize;
4534 p->last_chunk = last_chunk1;
4536 p->num_t_min = num_t_min;
4538 p->codeptr_ra = codeptr_ra;
4543 __kmp_omp_taskloop_task(NULL, gtid, new_task, codeptr_ra);
4545 __kmp_omp_task(gtid, new_task,
true);
4549 if (n_tsk0 > num_t_min)
4550 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0, gr_size0,
4551 ext0, last_chunk0, tc0, num_t_min,
4557 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0,
4558 gr_size0, ext0, last_chunk0, tc0,
4564 KA_TRACE(40, (
"__kmp_taskloop_recur(exit): T#%d\n", gtid));
4567 static void __kmp_taskloop(
ident_t *loc,
int gtid, kmp_task_t *task,
int if_val,
4568 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4569 int nogroup,
int sched, kmp_uint64 grainsize,
4570 int modifier,
void *task_dup) {
4571 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4572 KMP_DEBUG_ASSERT(task != NULL);
4574 #if OMPT_SUPPORT && OMPT_OPTIONAL
4575 OMPT_STORE_RETURN_ADDRESS(gtid);
4577 __kmpc_taskgroup(loc, gtid);
4582 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4585 kmp_uint64 lower = task_bounds.get_lb();
4586 kmp_uint64 upper = task_bounds.get_ub();
4587 kmp_uint64 ub_glob = upper;
4588 kmp_uint64 num_tasks = 0, extras = 0;
4589 kmp_int64 last_chunk =
4591 kmp_uint64 num_tasks_min = __kmp_taskloop_min_tasks;
4592 kmp_info_t *thread = __kmp_threads[gtid];
4593 kmp_taskdata_t *current_task = thread->th.th_current_task;
4595 KA_TRACE(20, (
"__kmp_taskloop: T#%d, task %p, lb %lld, ub %lld, st %lld, "
4596 "grain %llu(%d, %d), dup %p\n",
4597 gtid, taskdata, lower, upper, st, grainsize, sched, modifier,
4602 tc = upper - lower + 1;
4603 }
else if (st < 0) {
4604 tc = (lower - upper) / (-st) + 1;
4606 tc = (upper - lower) / st + 1;
4609 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d zero-trip loop\n", gtid));
4611 __kmp_task_start(gtid, task, current_task);
4613 __kmp_task_finish<false>(gtid, task, current_task);
4617 #if OMPT_SUPPORT && OMPT_OPTIONAL
4618 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
4619 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
4620 if (ompt_enabled.ompt_callback_work) {
4621 ompt_callbacks.ompt_callback(ompt_callback_work)(
4622 ompt_work_taskloop, ompt_scope_begin, &(team_info->parallel_data),
4623 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
4627 if (num_tasks_min == 0)
4630 KMP_MIN(thread->th.th_team_nproc * 10, INITIAL_TASK_DEQUE_SIZE);
4636 grainsize = thread->th.th_team_nproc * 10;
4639 if (grainsize > tc) {
4644 num_tasks = grainsize;
4645 grainsize = tc / num_tasks;
4646 extras = tc % num_tasks;
4650 if (grainsize > tc) {
4656 num_tasks = (tc + grainsize - 1) / grainsize;
4657 last_chunk = tc - (num_tasks * grainsize);
4660 num_tasks = tc / grainsize;
4662 grainsize = tc / num_tasks;
4663 extras = tc % num_tasks;
4668 KMP_ASSERT2(0,
"unknown scheduling of taskloop");
4671 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4672 (last_chunk < 0 ? last_chunk : extras));
4673 KMP_DEBUG_ASSERT(num_tasks > extras);
4674 KMP_DEBUG_ASSERT(num_tasks > 0);
4680 taskdata->td_flags.task_serial = 1;
4681 taskdata->td_flags.tiedness = TASK_TIED;
4683 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4684 grainsize, extras, last_chunk, tc,
4686 OMPT_GET_RETURN_ADDRESS(0),
4691 }
else if (num_tasks > num_tasks_min && !taskdata->td_flags.native) {
4692 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go recursive: tc %llu, #tasks %llu"
4693 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
4694 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
4696 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4697 grainsize, extras, last_chunk, tc, num_tasks_min,
4699 OMPT_GET_RETURN_ADDRESS(0),
4703 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go linear: tc %llu, #tasks %llu"
4704 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
4705 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
4707 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4708 grainsize, extras, last_chunk, tc,
4710 OMPT_GET_RETURN_ADDRESS(0),
4715 #if OMPT_SUPPORT && OMPT_OPTIONAL
4716 if (ompt_enabled.ompt_callback_work) {
4717 ompt_callbacks.ompt_callback(ompt_callback_work)(
4718 ompt_work_taskloop, ompt_scope_end, &(team_info->parallel_data),
4719 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
4724 #if OMPT_SUPPORT && OMPT_OPTIONAL
4725 OMPT_STORE_RETURN_ADDRESS(gtid);
4727 __kmpc_end_taskgroup(loc, gtid);
4729 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d\n", gtid));
4749 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
int nogroup,
4750 int sched, kmp_uint64 grainsize,
void *task_dup) {
4751 __kmp_assert_valid_gtid(gtid);
4752 KA_TRACE(20, (
"__kmpc_taskloop(enter): T#%d\n", gtid));
4753 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
4755 KA_TRACE(20, (
"__kmpc_taskloop(exit): T#%d\n", gtid));
4776 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4777 int nogroup,
int sched, kmp_uint64 grainsize,
4778 int modifier,
void *task_dup) {
4779 __kmp_assert_valid_gtid(gtid);
4780 KA_TRACE(20, (
"__kmpc_taskloop_5(enter): T#%d\n", gtid));
4781 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
4782 modifier, task_dup);
4783 KA_TRACE(20, (
"__kmpc_taskloop_5(exit): T#%d\n", gtid));
struct kmp_taskred_data kmp_taskred_data_t
struct kmp_task_red_input kmp_task_red_input_t
struct kmp_taskred_flags kmp_taskred_flags_t
struct kmp_taskred_input kmp_taskred_input_t
#define KMP_COUNT_BLOCK(name)
Increments specified counter (name).
void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int sched, kmp_uint64 grainsize, void *task_dup)
void * __kmpc_taskred_modifier_init(ident_t *loc, int gtid, int is_ws, int num, void *data)
void * __kmpc_taskred_init(int gtid, int num, void *data)
void * __kmpc_task_reduction_init(int gtid, int num, void *data)
void __kmpc_proxy_task_completed_ooo(kmp_task_t *ptask)
void __kmpc_task_reduction_modifier_fini(ident_t *loc, int gtid, int is_ws)
kmp_int32 __kmpc_omp_reg_task_with_affinity(ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *new_task, kmp_int32 naffins, kmp_task_affinity_info_t *affin_list)
void * __kmpc_task_reduction_modifier_init(ident_t *loc, int gtid, int is_ws, int num, void *data)
void __kmpc_taskloop_5(ident_t *loc, int gtid, kmp_task_t *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int sched, kmp_uint64 grainsize, int modifier, void *task_dup)
void __kmpc_proxy_task_completed(kmp_int32 gtid, kmp_task_t *ptask)
void * __kmpc_task_reduction_get_th_data(int gtid, void *tskgrp, void *data)
kmp_taskred_flags_t flags