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);
328 if (taskdata->td_flags.hidden_helper && !KMP_HIDDEN_HELPER_THREAD(gtid)) {
329 gtid = KMP_GTID_TO_SHADOW_GTID(gtid);
330 thread = __kmp_threads[gtid];
333 kmp_task_team_t *task_team = thread->th.th_task_team;
334 kmp_int32 tid = __kmp_tid_from_gtid(gtid);
335 kmp_thread_data_t *thread_data;
338 (
"__kmp_push_task: T#%d trying to push task %p.\n", gtid, taskdata));
340 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
343 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
344 KMP_DEBUG_USE_VAR(counter);
347 (
"__kmp_push_task: T#%d untied_count (%d) incremented for task %p\n",
348 gtid, counter, taskdata));
352 if (UNLIKELY(taskdata->td_flags.task_serial)) {
353 KA_TRACE(20, (
"__kmp_push_task: T#%d team serialized; returning "
354 "TASK_NOT_PUSHED for task %p\n",
356 return TASK_NOT_PUSHED;
361 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
362 if (UNLIKELY(!KMP_TASKING_ENABLED(task_team))) {
363 __kmp_enable_tasking(task_team, thread);
365 KMP_DEBUG_ASSERT(TCR_4(task_team->tt.tt_found_tasks) == TRUE);
366 KMP_DEBUG_ASSERT(TCR_PTR(task_team->tt.tt_threads_data) != NULL);
369 thread_data = &task_team->tt.tt_threads_data[tid];
374 if (UNLIKELY(thread_data->td.td_deque == NULL)) {
375 __kmp_alloc_task_deque(thread, thread_data);
380 if (TCR_4(thread_data->td.td_deque_ntasks) >=
381 TASK_DEQUE_SIZE(thread_data->td)) {
382 if (__kmp_enable_task_throttling &&
383 __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
384 thread->th.th_current_task)) {
385 KA_TRACE(20, (
"__kmp_push_task: T#%d deque is full; returning "
386 "TASK_NOT_PUSHED for task %p\n",
388 return TASK_NOT_PUSHED;
390 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
392 if (TCR_4(thread_data->td.td_deque_ntasks) >=
393 TASK_DEQUE_SIZE(thread_data->td)) {
395 __kmp_realloc_task_deque(thread, thread_data);
401 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
403 if (TCR_4(thread_data->td.td_deque_ntasks) >=
404 TASK_DEQUE_SIZE(thread_data->td)) {
405 if (__kmp_enable_task_throttling &&
406 __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
407 thread->th.th_current_task)) {
408 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
409 KA_TRACE(20, (
"__kmp_push_task: T#%d deque is full on 2nd check; "
410 "returning TASK_NOT_PUSHED for task %p\n",
412 return TASK_NOT_PUSHED;
415 __kmp_realloc_task_deque(thread, thread_data);
420 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) <
421 TASK_DEQUE_SIZE(thread_data->td));
423 thread_data->td.td_deque[thread_data->td.td_deque_tail] =
426 thread_data->td.td_deque_tail =
427 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
428 TCW_4(thread_data->td.td_deque_ntasks,
429 TCR_4(thread_data->td.td_deque_ntasks) + 1);
430 KMP_FSYNC_RELEASING(thread->th.th_current_task);
431 KMP_FSYNC_RELEASING(taskdata);
432 KA_TRACE(20, (
"__kmp_push_task: T#%d returning TASK_SUCCESSFULLY_PUSHED: "
433 "task=%p ntasks=%d head=%u tail=%u\n",
434 gtid, taskdata, thread_data->td.td_deque_ntasks,
435 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
437 auto hidden_helper = taskdata->td_flags.hidden_helper;
439 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
442 if (UNLIKELY(hidden_helper)) {
444 __kmp_hidden_helper_worker_thread_signal();
447 return TASK_SUCCESSFULLY_PUSHED;
454 void __kmp_pop_current_task_from_thread(kmp_info_t *this_thr) {
455 KF_TRACE(10, (
"__kmp_pop_current_task_from_thread(enter): T#%d "
456 "this_thread=%p, curtask=%p, "
457 "curtask_parent=%p\n",
458 0, this_thr, this_thr->th.th_current_task,
459 this_thr->th.th_current_task->td_parent));
461 this_thr->th.th_current_task = this_thr->th.th_current_task->td_parent;
463 KF_TRACE(10, (
"__kmp_pop_current_task_from_thread(exit): T#%d "
464 "this_thread=%p, curtask=%p, "
465 "curtask_parent=%p\n",
466 0, this_thr, this_thr->th.th_current_task,
467 this_thr->th.th_current_task->td_parent));
476 void __kmp_push_current_task_to_thread(kmp_info_t *this_thr, kmp_team_t *team,
480 KF_TRACE(10, (
"__kmp_push_current_task_to_thread(enter): T#%d this_thread=%p "
483 tid, this_thr, this_thr->th.th_current_task,
484 team->t.t_implicit_task_taskdata[tid].td_parent));
486 KMP_DEBUG_ASSERT(this_thr != NULL);
489 if (this_thr->th.th_current_task != &team->t.t_implicit_task_taskdata[0]) {
490 team->t.t_implicit_task_taskdata[0].td_parent =
491 this_thr->th.th_current_task;
492 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[0];
495 team->t.t_implicit_task_taskdata[tid].td_parent =
496 team->t.t_implicit_task_taskdata[0].td_parent;
497 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[tid];
500 KF_TRACE(10, (
"__kmp_push_current_task_to_thread(exit): T#%d this_thread=%p "
503 tid, this_thr, this_thr->th.th_current_task,
504 team->t.t_implicit_task_taskdata[tid].td_parent));
512 static void __kmp_task_start(kmp_int32 gtid, kmp_task_t *task,
513 kmp_taskdata_t *current_task) {
514 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
515 kmp_info_t *thread = __kmp_threads[gtid];
518 (
"__kmp_task_start(enter): T#%d starting task %p: current_task=%p\n",
519 gtid, taskdata, current_task));
521 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
526 current_task->td_flags.executing = 0;
529 #ifdef BUILD_TIED_TASK_STACK
530 if (taskdata->td_flags.tiedness == TASK_TIED) {
531 __kmp_push_task_stack(gtid, thread, taskdata);
536 thread->th.th_current_task = taskdata;
538 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 0 ||
539 taskdata->td_flags.tiedness == TASK_UNTIED);
540 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0 ||
541 taskdata->td_flags.tiedness == TASK_UNTIED);
542 taskdata->td_flags.started = 1;
543 taskdata->td_flags.executing = 1;
544 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
545 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
552 KA_TRACE(10, (
"__kmp_task_start(exit): T#%d task=%p\n", gtid, taskdata));
563 static inline void __ompt_task_init(kmp_taskdata_t *task,
int tid) {
565 task->ompt_task_info.task_data.value = 0;
566 task->ompt_task_info.frame.exit_frame = ompt_data_none;
567 task->ompt_task_info.frame.enter_frame = ompt_data_none;
568 task->ompt_task_info.frame.exit_frame_flags =
569 ompt_frame_runtime | ompt_frame_framepointer;
570 task->ompt_task_info.frame.enter_frame_flags =
571 ompt_frame_runtime | ompt_frame_framepointer;
576 static inline void __ompt_task_start(kmp_task_t *task,
577 kmp_taskdata_t *current_task,
579 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
580 ompt_task_status_t status = ompt_task_switch;
581 if (__kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded) {
582 status = ompt_task_yield;
583 __kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded = 0;
586 if (ompt_enabled.ompt_callback_task_schedule) {
587 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
588 &(current_task->ompt_task_info.task_data), status,
589 &(taskdata->ompt_task_info.task_data));
591 taskdata->ompt_task_info.scheduling_parent = current_task;
596 static inline void __ompt_task_finish(kmp_task_t *task,
597 kmp_taskdata_t *resumed_task,
598 ompt_task_status_t status) {
599 if (ompt_enabled.ompt_callback_task_schedule) {
600 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
601 if (__kmp_omp_cancellation && taskdata->td_taskgroup &&
602 taskdata->td_taskgroup->cancel_request == cancel_taskgroup) {
603 status = ompt_task_cancel;
607 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
608 &(taskdata->ompt_task_info.task_data), status,
609 (resumed_task ? &(resumed_task->ompt_task_info.task_data) : NULL));
615 static void __kmpc_omp_task_begin_if0_template(
ident_t *loc_ref, kmp_int32 gtid,
618 void *return_address) {
619 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
620 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
622 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(enter): T#%d loc=%p task=%p "
624 gtid, loc_ref, taskdata, current_task));
626 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
629 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
630 KMP_DEBUG_USE_VAR(counter);
631 KA_TRACE(20, (
"__kmpc_omp_task_begin_if0: T#%d untied_count (%d) "
632 "incremented for task %p\n",
633 gtid, counter, taskdata));
636 taskdata->td_flags.task_serial =
638 __kmp_task_start(gtid, task, current_task);
642 if (current_task->ompt_task_info.frame.enter_frame.ptr == NULL) {
643 current_task->ompt_task_info.frame.enter_frame.ptr =
644 taskdata->ompt_task_info.frame.exit_frame.ptr = frame_address;
645 current_task->ompt_task_info.frame.enter_frame_flags =
646 taskdata->ompt_task_info.frame.exit_frame_flags =
647 ompt_frame_application | ompt_frame_framepointer;
649 if (ompt_enabled.ompt_callback_task_create) {
650 ompt_task_info_t *parent_info = &(current_task->ompt_task_info);
651 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
652 &(parent_info->task_data), &(parent_info->frame),
653 &(taskdata->ompt_task_info.task_data),
654 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(taskdata), 0,
657 __ompt_task_start(task, current_task, gtid);
661 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(exit): T#%d loc=%p task=%p,\n", gtid,
667 static void __kmpc_omp_task_begin_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
670 void *return_address) {
671 __kmpc_omp_task_begin_if0_template<true>(loc_ref, gtid, task, frame_address,
682 void __kmpc_omp_task_begin_if0(
ident_t *loc_ref, kmp_int32 gtid,
685 if (UNLIKELY(ompt_enabled.enabled)) {
686 OMPT_STORE_RETURN_ADDRESS(gtid);
687 __kmpc_omp_task_begin_if0_ompt(loc_ref, gtid, task,
688 OMPT_GET_FRAME_ADDRESS(1),
689 OMPT_LOAD_RETURN_ADDRESS(gtid));
693 __kmpc_omp_task_begin_if0_template<false>(loc_ref, gtid, task, NULL, NULL);
699 void __kmpc_omp_task_begin(
ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task) {
700 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
704 (
"__kmpc_omp_task_begin(enter): T#%d loc=%p task=%p current_task=%p\n",
705 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task), current_task));
707 __kmp_task_start(gtid, task, current_task);
709 KA_TRACE(10, (
"__kmpc_omp_task_begin(exit): T#%d loc=%p task=%p,\n", gtid,
710 loc_ref, KMP_TASK_TO_TASKDATA(task)));
720 static void __kmp_free_task(kmp_int32 gtid, kmp_taskdata_t *taskdata,
721 kmp_info_t *thread) {
722 KA_TRACE(30, (
"__kmp_free_task: T#%d freeing data from task %p\n", gtid,
726 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
727 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0);
728 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 1);
729 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
730 KMP_DEBUG_ASSERT(taskdata->td_allocated_child_tasks == 0 ||
731 taskdata->td_flags.task_serial == 1);
732 KMP_DEBUG_ASSERT(taskdata->td_incomplete_child_tasks == 0);
734 taskdata->td_flags.freed = 1;
737 __kmp_fast_free(thread, taskdata);
739 __kmp_thread_free(thread, taskdata);
741 KA_TRACE(20, (
"__kmp_free_task: T#%d freed task %p\n", gtid, taskdata));
750 static void __kmp_free_task_and_ancestors(kmp_int32 gtid,
751 kmp_taskdata_t *taskdata,
752 kmp_info_t *thread) {
755 kmp_int32 team_serial =
756 (taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) &&
757 !taskdata->td_flags.proxy;
758 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
760 kmp_int32 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
761 KMP_DEBUG_ASSERT(children >= 0);
764 while (children == 0) {
765 kmp_taskdata_t *parent_taskdata = taskdata->td_parent;
767 KA_TRACE(20, (
"__kmp_free_task_and_ancestors(enter): T#%d task %p complete "
768 "and freeing itself\n",
772 __kmp_free_task(gtid, taskdata, thread);
774 taskdata = parent_taskdata;
780 if (taskdata->td_flags.tasktype == TASK_IMPLICIT) {
781 if (taskdata->td_dephash) {
782 int children = KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks);
783 kmp_tasking_flags_t flags_old = taskdata->td_flags;
784 if (children == 0 && flags_old.complete == 1) {
785 kmp_tasking_flags_t flags_new = flags_old;
786 flags_new.complete = 0;
787 if (KMP_COMPARE_AND_STORE_ACQ32(
788 RCAST(kmp_int32 *, &taskdata->td_flags),
789 *RCAST(kmp_int32 *, &flags_old),
790 *RCAST(kmp_int32 *, &flags_new))) {
791 KA_TRACE(100, (
"__kmp_free_task_and_ancestors: T#%d cleans "
792 "dephash of implicit task %p\n",
795 __kmp_dephash_free_entries(thread, taskdata->td_dephash);
802 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
803 KMP_DEBUG_ASSERT(children >= 0);
807 20, (
"__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; "
808 "not freeing it yet\n",
809 gtid, taskdata, children));
822 static void __kmp_task_finish(kmp_int32 gtid, kmp_task_t *task,
823 kmp_taskdata_t *resumed_task) {
824 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
825 kmp_info_t *thread = __kmp_threads[gtid];
826 kmp_task_team_t *task_team =
827 thread->th.th_task_team;
829 kmp_int32 children = 0;
831 KA_TRACE(10, (
"__kmp_task_finish(enter): T#%d finishing task %p and resuming "
833 gtid, taskdata, resumed_task));
835 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
838 #ifdef BUILD_TIED_TASK_STACK
839 if (taskdata->td_flags.tiedness == TASK_TIED) {
840 __kmp_pop_task_stack(gtid, thread, taskdata);
844 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
847 kmp_int32 counter = KMP_ATOMIC_DEC(&taskdata->td_untied_count) - 1;
850 (
"__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n",
851 gtid, counter, taskdata));
855 if (resumed_task == NULL) {
856 KMP_DEBUG_ASSERT(taskdata->td_flags.task_serial);
857 resumed_task = taskdata->td_parent;
860 thread->th.th_current_task = resumed_task;
861 resumed_task->td_flags.executing = 1;
862 KA_TRACE(10, (
"__kmp_task_finish(exit): T#%d partially done task %p, "
863 "resuming task %p\n",
864 gtid, taskdata, resumed_task));
872 (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) ==
873 taskdata->td_flags.task_serial);
874 if (taskdata->td_flags.task_serial) {
875 if (resumed_task == NULL) {
876 resumed_task = taskdata->td_parent;
880 KMP_DEBUG_ASSERT(resumed_task !=
890 if (UNLIKELY(taskdata->td_flags.destructors_thunk)) {
891 kmp_routine_entry_t destr_thunk = task->data1.destructors;
892 KMP_ASSERT(destr_thunk);
893 destr_thunk(gtid, task);
896 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
897 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 1);
898 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
901 if (UNLIKELY(taskdata->td_flags.detachable == TASK_DETACHABLE)) {
902 if (taskdata->td_allow_completion_event.type ==
903 KMP_EVENT_ALLOW_COMPLETION) {
905 __kmp_acquire_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
906 if (taskdata->td_allow_completion_event.type ==
907 KMP_EVENT_ALLOW_COMPLETION) {
909 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
910 taskdata->td_flags.executing = 0;
917 __ompt_task_finish(task, resumed_task, ompt_task_detach);
923 taskdata->td_flags.proxy = TASK_PROXY;
926 __kmp_release_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
931 taskdata->td_flags.complete = 1;
936 __ompt_task_finish(task, resumed_task, ompt_task_complete);
941 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) ||
942 taskdata->td_flags.detachable == TASK_DETACHABLE ||
943 taskdata->td_flags.hidden_helper) {
944 __kmp_release_deps(gtid, taskdata);
949 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks);
950 KMP_DEBUG_ASSERT(children >= 0);
951 if (taskdata->td_taskgroup)
952 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
953 }
else if (task_team && (task_team->tt.tt_found_proxy_tasks ||
954 task_team->tt.tt_hidden_helper_task_encountered)) {
957 __kmp_release_deps(gtid, taskdata);
963 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
964 taskdata->td_flags.executing = 0;
968 20, (
"__kmp_task_finish: T#%d finished task %p, %d incomplete children\n",
969 gtid, taskdata, children));
975 thread->th.th_current_task = resumed_task;
977 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
981 resumed_task->td_flags.executing = 1;
984 10, (
"__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n",
985 gtid, taskdata, resumed_task));
991 static void __kmpc_omp_task_complete_if0_template(
ident_t *loc_ref,
994 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n",
995 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
996 KMP_DEBUG_ASSERT(gtid >= 0);
998 __kmp_task_finish<ompt>(gtid, task, NULL);
1000 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n",
1001 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
1005 ompt_frame_t *ompt_frame;
1006 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
1007 ompt_frame->enter_frame = ompt_data_none;
1008 ompt_frame->enter_frame_flags =
1009 ompt_frame_runtime | ompt_frame_framepointer;
1018 void __kmpc_omp_task_complete_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
1020 __kmpc_omp_task_complete_if0_template<true>(loc_ref, gtid, task);
1029 void __kmpc_omp_task_complete_if0(
ident_t *loc_ref, kmp_int32 gtid,
1032 if (UNLIKELY(ompt_enabled.enabled)) {
1033 __kmpc_omp_task_complete_if0_ompt(loc_ref, gtid, task);
1037 __kmpc_omp_task_complete_if0_template<false>(loc_ref, gtid, task);
1043 void __kmpc_omp_task_complete(
ident_t *loc_ref, kmp_int32 gtid,
1045 KA_TRACE(10, (
"__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n", gtid,
1046 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1048 __kmp_task_finish<false>(gtid, task,
1051 KA_TRACE(10, (
"__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n", gtid,
1052 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1068 void __kmp_init_implicit_task(
ident_t *loc_ref, kmp_info_t *this_thr,
1069 kmp_team_t *team,
int tid,
int set_curr_task) {
1070 kmp_taskdata_t *task = &team->t.t_implicit_task_taskdata[tid];
1074 (
"__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n",
1075 tid, team, task, set_curr_task ?
"TRUE" :
"FALSE"));
1077 task->td_task_id = KMP_GEN_TASK_ID();
1078 task->td_team = team;
1081 task->td_ident = loc_ref;
1082 task->td_taskwait_ident = NULL;
1083 task->td_taskwait_counter = 0;
1084 task->td_taskwait_thread = 0;
1086 task->td_flags.tiedness = TASK_TIED;
1087 task->td_flags.tasktype = TASK_IMPLICIT;
1088 task->td_flags.proxy = TASK_FULL;
1091 task->td_flags.task_serial = 1;
1092 task->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1093 task->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1095 task->td_flags.started = 1;
1096 task->td_flags.executing = 1;
1097 task->td_flags.complete = 0;
1098 task->td_flags.freed = 0;
1100 task->td_depnode = NULL;
1101 task->td_last_tied = task;
1102 task->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1104 if (set_curr_task) {
1105 KMP_ATOMIC_ST_REL(&task->td_incomplete_child_tasks, 0);
1107 KMP_ATOMIC_ST_REL(&task->td_allocated_child_tasks, 0);
1108 task->td_taskgroup = NULL;
1109 task->td_dephash = NULL;
1110 __kmp_push_current_task_to_thread(this_thr, team, tid);
1112 KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0);
1113 KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0);
1117 if (UNLIKELY(ompt_enabled.enabled))
1118 __ompt_task_init(task, tid);
1121 KF_TRACE(10, (
"__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n", tid,
1130 void __kmp_finish_implicit_task(kmp_info_t *thread) {
1131 kmp_taskdata_t *task = thread->th.th_current_task;
1132 if (task->td_dephash) {
1134 task->td_flags.complete = 1;
1135 children = KMP_ATOMIC_LD_ACQ(&task->td_incomplete_child_tasks);
1136 kmp_tasking_flags_t flags_old = task->td_flags;
1137 if (children == 0 && flags_old.complete == 1) {
1138 kmp_tasking_flags_t flags_new = flags_old;
1139 flags_new.complete = 0;
1140 if (KMP_COMPARE_AND_STORE_ACQ32(RCAST(kmp_int32 *, &task->td_flags),
1141 *RCAST(kmp_int32 *, &flags_old),
1142 *RCAST(kmp_int32 *, &flags_new))) {
1143 KA_TRACE(100, (
"__kmp_finish_implicit_task: T#%d cleans "
1144 "dephash of implicit task %p\n",
1145 thread->th.th_info.ds.ds_gtid, task));
1146 __kmp_dephash_free_entries(thread, task->td_dephash);
1156 void __kmp_free_implicit_task(kmp_info_t *thread) {
1157 kmp_taskdata_t *task = thread->th.th_current_task;
1158 if (task && task->td_dephash) {
1159 __kmp_dephash_free(thread, task->td_dephash);
1160 task->td_dephash = NULL;
1166 static size_t __kmp_round_up_to_val(
size_t size,
size_t val) {
1167 if (size & (val - 1)) {
1169 if (size <= KMP_SIZE_T_MAX - val) {
1188 kmp_task_t *__kmp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1189 kmp_tasking_flags_t *flags,
1190 size_t sizeof_kmp_task_t,
size_t sizeof_shareds,
1191 kmp_routine_entry_t task_entry) {
1193 kmp_taskdata_t *taskdata;
1194 kmp_info_t *thread = __kmp_threads[gtid];
1195 kmp_info_t *encountering_thread = thread;
1196 kmp_team_t *team = thread->th.th_team;
1197 kmp_taskdata_t *parent_task = thread->th.th_current_task;
1198 size_t shareds_offset;
1200 if (UNLIKELY(!TCR_4(__kmp_init_middle)))
1201 __kmp_middle_initialize();
1203 if (flags->hidden_helper) {
1204 if (__kmp_enable_hidden_helper) {
1205 if (!TCR_4(__kmp_init_hidden_helper))
1206 __kmp_hidden_helper_initialize();
1211 if (!KMP_HIDDEN_HELPER_THREAD(gtid)) {
1212 thread = __kmp_threads[KMP_GTID_TO_SHADOW_GTID(gtid)];
1218 flags->hidden_helper = FALSE;
1222 KA_TRACE(10, (
"__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) "
1223 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1224 gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t,
1225 sizeof_shareds, task_entry));
1227 KMP_DEBUG_ASSERT(parent_task);
1228 if (parent_task->td_flags.final) {
1229 if (flags->merged_if0) {
1234 if (flags->tiedness == TASK_UNTIED && !team->t.t_serialized) {
1239 encountering_thread->th.th_task_team->tt.tt_untied_task_encountered, 1);
1245 if (UNLIKELY(flags->proxy == TASK_PROXY ||
1246 flags->detachable == TASK_DETACHABLE || flags->hidden_helper)) {
1247 if (flags->proxy == TASK_PROXY) {
1248 flags->tiedness = TASK_UNTIED;
1249 flags->merged_if0 = 1;
1253 if ((encountering_thread->th.th_task_team) == NULL) {
1256 KMP_DEBUG_ASSERT(team->t.t_serialized);
1258 (
"T#%d creating task team in __kmp_task_alloc for proxy task\n",
1260 __kmp_task_team_setup(
1261 encountering_thread, team,
1263 encountering_thread->th.th_task_team =
1264 team->t.t_task_team[encountering_thread->th.th_task_state];
1266 kmp_task_team_t *task_team = encountering_thread->th.th_task_team;
1269 if (!KMP_TASKING_ENABLED(task_team)) {
1272 (
"T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid));
1273 __kmp_enable_tasking(task_team, encountering_thread);
1274 kmp_int32 tid = encountering_thread->th.th_info.ds.ds_tid;
1275 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
1277 if (thread_data->td.td_deque == NULL) {
1278 __kmp_alloc_task_deque(encountering_thread, thread_data);
1282 if ((flags->proxy == TASK_PROXY || flags->detachable == TASK_DETACHABLE) &&
1283 task_team->tt.tt_found_proxy_tasks == FALSE)
1284 TCW_4(task_team->tt.tt_found_proxy_tasks, TRUE);
1285 if (flags->hidden_helper &&
1286 task_team->tt.tt_hidden_helper_task_encountered == FALSE)
1287 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, TRUE);
1292 shareds_offset =
sizeof(kmp_taskdata_t) + sizeof_kmp_task_t;
1293 shareds_offset = __kmp_round_up_to_val(shareds_offset,
sizeof(
void *));
1296 KA_TRACE(30, (
"__kmp_task_alloc: T#%d First malloc size: %ld\n", gtid,
1298 KA_TRACE(30, (
"__kmp_task_alloc: T#%d Second malloc size: %ld\n", gtid,
1303 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(
1304 encountering_thread, shareds_offset + sizeof_shareds);
1306 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(
1307 encountering_thread, shareds_offset + sizeof_shareds);
1310 task = KMP_TASKDATA_TO_TASK(taskdata);
1313 #if KMP_ARCH_X86 || KMP_ARCH_PPC64 || !KMP_HAVE_QUAD
1314 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(
double) - 1)) == 0);
1315 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(
double) - 1)) == 0);
1317 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(_Quad) - 1)) == 0);
1318 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(_Quad) - 1)) == 0);
1320 if (sizeof_shareds > 0) {
1322 task->shareds = &((
char *)taskdata)[shareds_offset];
1324 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
1327 task->shareds = NULL;
1329 task->routine = task_entry;
1332 taskdata->td_task_id = KMP_GEN_TASK_ID();
1333 taskdata->td_team = thread->th.th_team;
1334 taskdata->td_alloc_thread = encountering_thread;
1335 taskdata->td_parent = parent_task;
1336 taskdata->td_level = parent_task->td_level + 1;
1337 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
1338 taskdata->td_ident = loc_ref;
1339 taskdata->td_taskwait_ident = NULL;
1340 taskdata->td_taskwait_counter = 0;
1341 taskdata->td_taskwait_thread = 0;
1342 KMP_DEBUG_ASSERT(taskdata->td_parent != NULL);
1344 if (flags->proxy == TASK_FULL)
1345 copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs);
1347 taskdata->td_flags = *flags;
1348 taskdata->encountering_gtid = gtid;
1349 taskdata->td_task_team = thread->th.th_task_team;
1350 taskdata->td_size_alloc = shareds_offset + sizeof_shareds;
1351 taskdata->td_flags.tasktype = TASK_EXPLICIT;
1354 taskdata->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1357 taskdata->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1363 taskdata->td_flags.task_serial =
1364 (parent_task->td_flags.final || taskdata->td_flags.team_serial ||
1365 taskdata->td_flags.tasking_ser || flags->merged_if0);
1367 taskdata->td_flags.started = 0;
1368 taskdata->td_flags.executing = 0;
1369 taskdata->td_flags.complete = 0;
1370 taskdata->td_flags.freed = 0;
1372 KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0);
1374 KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1);
1375 taskdata->td_taskgroup =
1376 parent_task->td_taskgroup;
1377 taskdata->td_dephash = NULL;
1378 taskdata->td_depnode = NULL;
1379 if (flags->tiedness == TASK_UNTIED)
1380 taskdata->td_last_tied = NULL;
1382 taskdata->td_last_tied = taskdata;
1383 taskdata->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1385 if (UNLIKELY(ompt_enabled.enabled))
1386 __ompt_task_init(taskdata, gtid);
1390 if (flags->proxy == TASK_PROXY || flags->detachable == TASK_DETACHABLE ||
1391 flags->hidden_helper ||
1392 !(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
1393 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
1394 if (parent_task->td_taskgroup)
1395 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
1398 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) {
1399 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
1401 if (flags->hidden_helper) {
1402 taskdata->td_flags.task_serial = FALSE;
1404 KMP_ATOMIC_INC(&__kmp_unexecuted_hidden_helper_tasks);
1408 KA_TRACE(20, (
"__kmp_task_alloc(exit): T#%d created task %p parent=%p\n",
1409 gtid, taskdata, taskdata->td_parent));
1414 kmp_task_t *__kmpc_omp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1415 kmp_int32 flags,
size_t sizeof_kmp_task_t,
1416 size_t sizeof_shareds,
1417 kmp_routine_entry_t task_entry) {
1419 kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
1420 __kmp_assert_valid_gtid(gtid);
1421 input_flags->native = FALSE;
1423 KA_TRACE(10, (
"__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s %s) "
1424 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1425 gtid, loc_ref, input_flags->tiedness ?
"tied " :
"untied",
1426 input_flags->proxy ?
"proxy" :
"",
1427 input_flags->detachable ?
"detachable" :
"", sizeof_kmp_task_t,
1428 sizeof_shareds, task_entry));
1430 retval = __kmp_task_alloc(loc_ref, gtid, input_flags, sizeof_kmp_task_t,
1431 sizeof_shareds, task_entry);
1433 KA_TRACE(20, (
"__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval));
1438 kmp_task_t *__kmpc_omp_target_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1440 size_t sizeof_kmp_task_t,
1441 size_t sizeof_shareds,
1442 kmp_routine_entry_t task_entry,
1443 kmp_int64 device_id) {
1444 if (__kmp_enable_hidden_helper) {
1445 auto &input_flags =
reinterpret_cast<kmp_tasking_flags_t &
>(flags);
1446 input_flags.hidden_helper = TRUE;
1449 return __kmpc_omp_task_alloc(loc_ref, gtid, flags, sizeof_kmp_task_t,
1450 sizeof_shareds, task_entry);
1468 kmp_task_t *new_task, kmp_int32 naffins,
1469 kmp_task_affinity_info_t *affin_list) {
1478 static void __kmp_invoke_task(kmp_int32 gtid, kmp_task_t *task,
1479 kmp_taskdata_t *current_task) {
1480 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
1484 30, (
"__kmp_invoke_task(enter): T#%d invoking task %p, current_task=%p\n",
1485 gtid, taskdata, current_task));
1486 KMP_DEBUG_ASSERT(task);
1487 if (UNLIKELY(taskdata->td_flags.proxy == TASK_PROXY &&
1488 taskdata->td_flags.complete == 1)) {
1493 (
"__kmp_invoke_task: T#%d running bottom finish for proxy task %p\n",
1496 __kmp_bottom_half_finish_proxy(gtid, task);
1498 KA_TRACE(30, (
"__kmp_invoke_task(exit): T#%d completed bottom finish for "
1499 "proxy task %p, resuming task %p\n",
1500 gtid, taskdata, current_task));
1508 ompt_thread_info_t oldInfo;
1509 if (UNLIKELY(ompt_enabled.enabled)) {
1511 thread = __kmp_threads[gtid];
1512 oldInfo = thread->th.ompt_thread_info;
1513 thread->th.ompt_thread_info.wait_id = 0;
1514 thread->th.ompt_thread_info.state = (thread->th.th_team_serialized)
1515 ? ompt_state_work_serial
1516 : ompt_state_work_parallel;
1517 taskdata->ompt_task_info.frame.exit_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1522 if (taskdata->td_flags.hidden_helper) {
1524 KMP_ASSERT(KMP_HIDDEN_HELPER_THREAD(gtid));
1525 KMP_ATOMIC_DEC(&__kmp_unexecuted_hidden_helper_tasks);
1529 if (taskdata->td_flags.proxy != TASK_PROXY) {
1530 __kmp_task_start(gtid, task, current_task);
1536 if (UNLIKELY(__kmp_omp_cancellation)) {
1537 thread = __kmp_threads[gtid];
1538 kmp_team_t *this_team = thread->th.th_team;
1539 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
1540 if ((taskgroup && taskgroup->cancel_request) ||
1541 (this_team->t.t_cancel_request == cancel_parallel)) {
1542 #if OMPT_SUPPORT && OMPT_OPTIONAL
1543 ompt_data_t *task_data;
1544 if (UNLIKELY(ompt_enabled.ompt_callback_cancel)) {
1545 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, NULL, NULL);
1546 ompt_callbacks.ompt_callback(ompt_callback_cancel)(
1548 ((taskgroup && taskgroup->cancel_request) ? ompt_cancel_taskgroup
1549 : ompt_cancel_parallel) |
1550 ompt_cancel_discarded_task,
1563 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
1564 taskdata->td_last_tied = current_task->td_last_tied;
1565 KMP_DEBUG_ASSERT(taskdata->td_last_tied);
1567 #if KMP_STATS_ENABLED
1569 switch (KMP_GET_THREAD_STATE()) {
1570 case FORK_JOIN_BARRIER:
1571 KMP_PUSH_PARTITIONED_TIMER(OMP_task_join_bar);
1574 KMP_PUSH_PARTITIONED_TIMER(OMP_task_plain_bar);
1577 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskyield);
1580 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskwait);
1583 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskgroup);
1586 KMP_PUSH_PARTITIONED_TIMER(OMP_task_immediate);
1593 if (UNLIKELY(ompt_enabled.enabled))
1594 __ompt_task_start(task, current_task, gtid);
1598 if (ompd_state & OMPD_ENABLE_BP)
1599 ompd_bp_task_begin();
1602 #if USE_ITT_BUILD && USE_ITT_NOTIFY
1603 kmp_uint64 cur_time;
1604 kmp_int32 kmp_itt_count_task =
1605 __kmp_forkjoin_frames_mode == 3 && !taskdata->td_flags.task_serial &&
1606 current_task->td_flags.tasktype == TASK_IMPLICIT;
1607 if (kmp_itt_count_task) {
1608 thread = __kmp_threads[gtid];
1610 if (thread->th.th_bar_arrive_time)
1611 cur_time = __itt_get_timestamp();
1613 kmp_itt_count_task = 0;
1615 KMP_FSYNC_ACQUIRED(taskdata);
1618 #ifdef KMP_GOMP_COMPAT
1619 if (taskdata->td_flags.native) {
1620 ((void (*)(
void *))(*(task->routine)))(task->shareds);
1624 (*(task->routine))(gtid, task);
1626 KMP_POP_PARTITIONED_TIMER();
1628 #if USE_ITT_BUILD && USE_ITT_NOTIFY
1629 if (kmp_itt_count_task) {
1631 thread->th.th_bar_arrive_time += (__itt_get_timestamp() - cur_time);
1633 KMP_FSYNC_CANCEL(taskdata);
1634 KMP_FSYNC_RELEASING(taskdata->td_parent);
1639 if (ompd_state & OMPD_ENABLE_BP)
1644 if (taskdata->td_flags.proxy != TASK_PROXY) {
1646 if (UNLIKELY(ompt_enabled.enabled)) {
1647 thread->th.ompt_thread_info = oldInfo;
1648 if (taskdata->td_flags.tiedness == TASK_TIED) {
1649 taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
1651 __kmp_task_finish<true>(gtid, task, current_task);
1654 __kmp_task_finish<false>(gtid, task, current_task);
1659 (
"__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n",
1660 gtid, taskdata, current_task));
1674 kmp_int32 __kmpc_omp_task_parts(
ident_t *loc_ref, kmp_int32 gtid,
1675 kmp_task_t *new_task) {
1676 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1678 KA_TRACE(10, (
"__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n", gtid,
1679 loc_ref, new_taskdata));
1682 kmp_taskdata_t *parent;
1683 if (UNLIKELY(ompt_enabled.enabled)) {
1684 parent = new_taskdata->td_parent;
1685 if (ompt_enabled.ompt_callback_task_create) {
1686 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1687 &(parent->ompt_task_info.task_data), &(parent->ompt_task_info.frame),
1688 &(new_taskdata->ompt_task_info.task_data), ompt_task_explicit, 0,
1689 OMPT_GET_RETURN_ADDRESS(0));
1697 if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1699 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1700 new_taskdata->td_flags.task_serial = 1;
1701 __kmp_invoke_task(gtid, new_task, current_task);
1706 (
"__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: "
1707 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
1708 gtid, loc_ref, new_taskdata));
1711 if (UNLIKELY(ompt_enabled.enabled)) {
1712 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1715 return TASK_CURRENT_NOT_QUEUED;
1729 kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task,
1730 bool serialize_immediate) {
1731 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1735 if (new_taskdata->td_flags.proxy == TASK_PROXY ||
1736 __kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1738 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1739 if (serialize_immediate)
1740 new_taskdata->td_flags.task_serial = 1;
1741 __kmp_invoke_task(gtid, new_task, current_task);
1744 return TASK_CURRENT_NOT_QUEUED;
1759 kmp_int32 __kmpc_omp_task(
ident_t *loc_ref, kmp_int32 gtid,
1760 kmp_task_t *new_task) {
1762 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1764 #if KMP_DEBUG || OMPT_SUPPORT
1765 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1767 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1769 __kmp_assert_valid_gtid(gtid);
1772 kmp_taskdata_t *parent = NULL;
1773 if (UNLIKELY(ompt_enabled.enabled)) {
1774 if (!new_taskdata->td_flags.started) {
1775 OMPT_STORE_RETURN_ADDRESS(gtid);
1776 parent = new_taskdata->td_parent;
1777 if (!parent->ompt_task_info.frame.enter_frame.ptr) {
1778 parent->ompt_task_info.frame.enter_frame.ptr =
1779 OMPT_GET_FRAME_ADDRESS(0);
1781 if (ompt_enabled.ompt_callback_task_create) {
1782 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1783 &(parent->ompt_task_info.task_data),
1784 &(parent->ompt_task_info.frame),
1785 &(new_taskdata->ompt_task_info.task_data),
1786 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1787 OMPT_LOAD_RETURN_ADDRESS(gtid));
1792 __ompt_task_finish(new_task,
1793 new_taskdata->ompt_task_info.scheduling_parent,
1795 new_taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
1800 res = __kmp_omp_task(gtid, new_task,
true);
1802 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
1803 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1804 gtid, loc_ref, new_taskdata));
1806 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
1807 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1826 kmp_int32 __kmp_omp_taskloop_task(
ident_t *loc_ref, kmp_int32 gtid,
1827 kmp_task_t *new_task,
void *codeptr_ra) {
1829 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1831 #if KMP_DEBUG || OMPT_SUPPORT
1832 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1834 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1838 kmp_taskdata_t *parent = NULL;
1839 if (UNLIKELY(ompt_enabled.enabled && !new_taskdata->td_flags.started)) {
1840 parent = new_taskdata->td_parent;
1841 if (!parent->ompt_task_info.frame.enter_frame.ptr)
1842 parent->ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1843 if (ompt_enabled.ompt_callback_task_create) {
1844 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1845 &(parent->ompt_task_info.task_data), &(parent->ompt_task_info.frame),
1846 &(new_taskdata->ompt_task_info.task_data),
1847 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1853 res = __kmp_omp_task(gtid, new_task,
true);
1855 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
1856 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1857 gtid, loc_ref, new_taskdata));
1859 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
1860 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1866 template <
bool ompt>
1867 static kmp_int32 __kmpc_omp_taskwait_template(
ident_t *loc_ref, kmp_int32 gtid,
1868 void *frame_address,
1869 void *return_address) {
1870 kmp_taskdata_t *taskdata =
nullptr;
1872 int thread_finished = FALSE;
1873 KMP_SET_THREAD_STATE_BLOCK(TASKWAIT);
1875 KA_TRACE(10, (
"__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref));
1876 KMP_DEBUG_ASSERT(gtid >= 0);
1878 if (__kmp_tasking_mode != tskm_immediate_exec) {
1879 thread = __kmp_threads[gtid];
1880 taskdata = thread->th.th_current_task;
1882 #if OMPT_SUPPORT && OMPT_OPTIONAL
1883 ompt_data_t *my_task_data;
1884 ompt_data_t *my_parallel_data;
1887 my_task_data = &(taskdata->ompt_task_info.task_data);
1888 my_parallel_data = OMPT_CUR_TEAM_DATA(thread);
1890 taskdata->ompt_task_info.frame.enter_frame.ptr = frame_address;
1892 if (ompt_enabled.ompt_callback_sync_region) {
1893 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
1894 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
1895 my_task_data, return_address);
1898 if (ompt_enabled.ompt_callback_sync_region_wait) {
1899 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
1900 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
1901 my_task_data, return_address);
1911 taskdata->td_taskwait_counter += 1;
1912 taskdata->td_taskwait_ident = loc_ref;
1913 taskdata->td_taskwait_thread = gtid + 1;
1916 void *itt_sync_obj = NULL;
1918 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
1923 !taskdata->td_flags.team_serial && !taskdata->td_flags.final;
1925 must_wait = must_wait || (thread->th.th_task_team != NULL &&
1926 thread->th.th_task_team->tt.tt_found_proxy_tasks);
1930 (__kmp_enable_hidden_helper && thread->th.th_task_team != NULL &&
1931 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered);
1934 kmp_flag_32<false, false> flag(
1935 RCAST(std::atomic<kmp_uint32> *,
1936 &(taskdata->td_incomplete_child_tasks)),
1938 while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) != 0) {
1939 flag.execute_tasks(thread, gtid, FALSE,
1940 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
1941 __kmp_task_stealing_constraint);
1945 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
1946 KMP_FSYNC_ACQUIRED(taskdata);
1951 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
1953 #if OMPT_SUPPORT && OMPT_OPTIONAL
1955 if (ompt_enabled.ompt_callback_sync_region_wait) {
1956 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
1957 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
1958 my_task_data, return_address);
1960 if (ompt_enabled.ompt_callback_sync_region) {
1961 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
1962 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
1963 my_task_data, return_address);
1965 taskdata->ompt_task_info.frame.enter_frame = ompt_data_none;
1971 KA_TRACE(10, (
"__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, "
1972 "returning TASK_CURRENT_NOT_QUEUED\n",
1975 return TASK_CURRENT_NOT_QUEUED;
1978 #if OMPT_SUPPORT && OMPT_OPTIONAL
1980 static kmp_int32 __kmpc_omp_taskwait_ompt(
ident_t *loc_ref, kmp_int32 gtid,
1981 void *frame_address,
1982 void *return_address) {
1983 return __kmpc_omp_taskwait_template<true>(loc_ref, gtid, frame_address,
1990 kmp_int32 __kmpc_omp_taskwait(
ident_t *loc_ref, kmp_int32 gtid) {
1991 #if OMPT_SUPPORT && OMPT_OPTIONAL
1992 if (UNLIKELY(ompt_enabled.enabled)) {
1993 OMPT_STORE_RETURN_ADDRESS(gtid);
1994 return __kmpc_omp_taskwait_ompt(loc_ref, gtid, OMPT_GET_FRAME_ADDRESS(0),
1995 OMPT_LOAD_RETURN_ADDRESS(gtid));
1998 return __kmpc_omp_taskwait_template<false>(loc_ref, gtid, NULL, NULL);
2002 kmp_int32 __kmpc_omp_taskyield(
ident_t *loc_ref, kmp_int32 gtid,
int end_part) {
2003 kmp_taskdata_t *taskdata = NULL;
2005 int thread_finished = FALSE;
2008 KMP_SET_THREAD_STATE_BLOCK(TASKYIELD);
2010 KA_TRACE(10, (
"__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n",
2011 gtid, loc_ref, end_part));
2012 __kmp_assert_valid_gtid(gtid);
2014 if (__kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel) {
2015 thread = __kmp_threads[gtid];
2016 taskdata = thread->th.th_current_task;
2023 taskdata->td_taskwait_counter += 1;
2024 taskdata->td_taskwait_ident = loc_ref;
2025 taskdata->td_taskwait_thread = gtid + 1;
2028 void *itt_sync_obj = NULL;
2030 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2033 if (!taskdata->td_flags.team_serial) {
2034 kmp_task_team_t *task_team = thread->th.th_task_team;
2035 if (task_team != NULL) {
2036 if (KMP_TASKING_ENABLED(task_team)) {
2038 if (UNLIKELY(ompt_enabled.enabled))
2039 thread->th.ompt_thread_info.ompt_task_yielded = 1;
2041 __kmp_execute_tasks_32(
2042 thread, gtid, (kmp_flag_32<> *)NULL, FALSE,
2043 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2044 __kmp_task_stealing_constraint);
2046 if (UNLIKELY(ompt_enabled.enabled))
2047 thread->th.ompt_thread_info.ompt_task_yielded = 0;
2053 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2058 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2061 KA_TRACE(10, (
"__kmpc_omp_taskyield(exit): T#%d task %p resuming, "
2062 "returning TASK_CURRENT_NOT_QUEUED\n",
2065 return TASK_CURRENT_NOT_QUEUED;
2086 unsigned reserved31 : 31;
2166 template <
typename T>
2167 void *__kmp_task_reduction_init(
int gtid,
int num, T *data) {
2168 __kmp_assert_valid_gtid(gtid);
2169 kmp_info_t *thread = __kmp_threads[gtid];
2170 kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup;
2171 kmp_uint32 nth = thread->th.th_team_nproc;
2175 KMP_ASSERT(tg != NULL);
2176 KMP_ASSERT(data != NULL);
2177 KMP_ASSERT(num > 0);
2179 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n",
2183 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, taskgroup %p, #items %d\n",
2187 for (
int i = 0; i < num; ++i) {
2188 size_t size = data[i].reduce_size - 1;
2190 size += CACHE_LINE - size % CACHE_LINE;
2191 KMP_ASSERT(data[i].reduce_comb != NULL);
2194 arr[i].
flags = data[i].flags;
2198 __kmp_assign_orig<T>(arr[i], data[i]);
2199 if (!arr[i].flags.lazy_priv) {
2202 arr[i].
reduce_pend = (
char *)(arr[i].reduce_priv) + nth * size;
2203 if (arr[i].reduce_init != NULL) {
2205 for (
size_t j = 0; j < nth; ++j) {
2206 __kmp_call_init<T>(arr[i], j * size);
2213 arr[i].
reduce_priv = __kmp_allocate(nth *
sizeof(
void *));
2216 tg->reduce_data = (
void *)arr;
2217 tg->reduce_num_data = num;
2256 template <
typename T>
2257 void __kmp_task_reduction_init_copy(kmp_info_t *thr,
int num, T *data,
2258 kmp_taskgroup_t *tg,
void *reduce_data) {
2260 KA_TRACE(20, (
"__kmp_task_reduction_init_copy: Th %p, init taskgroup %p,"
2262 thr, tg, reduce_data));
2267 for (
int i = 0; i < num; ++i) {
2270 tg->reduce_data = (
void *)arr;
2271 tg->reduce_num_data = num;
2284 __kmp_assert_valid_gtid(gtid);
2285 kmp_info_t *thread = __kmp_threads[gtid];
2286 kmp_int32 nth = thread->th.th_team_nproc;
2290 kmp_taskgroup_t *tg = (kmp_taskgroup_t *)tskgrp;
2292 tg = thread->th.th_current_task->td_taskgroup;
2293 KMP_ASSERT(tg != NULL);
2295 kmp_int32 num = tg->reduce_num_data;
2296 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
2298 KMP_ASSERT(data != NULL);
2299 while (tg != NULL) {
2300 for (
int i = 0; i < num; ++i) {
2301 if (!arr[i].flags.lazy_priv) {
2302 if (data == arr[i].reduce_shar ||
2303 (data >= arr[i].reduce_priv && data < arr[i].reduce_pend))
2304 return (
char *)(arr[i].
reduce_priv) + tid * arr[i].reduce_size;
2307 void **p_priv = (
void **)(arr[i].reduce_priv);
2308 if (data == arr[i].reduce_shar)
2311 for (
int j = 0; j < nth; ++j)
2312 if (data == p_priv[j])
2316 if (p_priv[tid] == NULL) {
2318 p_priv[tid] = __kmp_allocate(arr[i].reduce_size);
2319 if (arr[i].reduce_init != NULL) {
2320 if (arr[i].reduce_orig != NULL) {
2322 p_priv[tid], arr[i].reduce_orig);
2324 ((void (*)(
void *))arr[i].
reduce_init)(p_priv[tid]);
2333 num = tg->reduce_num_data;
2335 KMP_ASSERT2(0,
"Unknown task reduction item");
2341 static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg) {
2342 kmp_int32 nth = th->th.th_team_nproc;
2343 KMP_DEBUG_ASSERT(nth > 1);
2345 kmp_int32 num = tg->reduce_num_data;
2346 for (
int i = 0; i < num; ++i) {
2348 void (*f_fini)(
void *) = (
void (*)(
void *))(arr[i].
reduce_fini);
2349 void (*f_comb)(
void *,
void *) =
2351 if (!arr[i].flags.lazy_priv) {
2354 for (
int j = 0; j < nth; ++j) {
2355 void *priv_data = (
char *)pr_data + j * size;
2356 f_comb(sh_data, priv_data);
2361 void **pr_data = (
void **)(arr[i].reduce_priv);
2362 for (
int j = 0; j < nth; ++j) {
2363 if (pr_data[j] != NULL) {
2364 f_comb(sh_data, pr_data[j]);
2367 __kmp_free(pr_data[j]);
2371 __kmp_free(arr[i].reduce_priv);
2373 __kmp_thread_free(th, arr);
2374 tg->reduce_data = NULL;
2375 tg->reduce_num_data = 0;
2381 static void __kmp_task_reduction_clean(kmp_info_t *th, kmp_taskgroup_t *tg) {
2382 __kmp_thread_free(th, tg->reduce_data);
2383 tg->reduce_data = NULL;
2384 tg->reduce_num_data = 0;
2387 template <
typename T>
2388 void *__kmp_task_reduction_modifier_init(
ident_t *loc,
int gtid,
int is_ws,
2390 __kmp_assert_valid_gtid(gtid);
2391 kmp_info_t *thr = __kmp_threads[gtid];
2392 kmp_int32 nth = thr->th.th_team_nproc;
2393 __kmpc_taskgroup(loc, gtid);
2396 (
"__kmpc_reduction_modifier_init: T#%d, tg %p, exiting nth=1\n",
2397 gtid, thr->th.th_current_task->td_taskgroup));
2398 return (
void *)thr->th.th_current_task->td_taskgroup;
2400 kmp_team_t *team = thr->th.th_team;
2402 kmp_taskgroup_t *tg;
2403 reduce_data = KMP_ATOMIC_LD_RLX(&team->t.t_tg_reduce_data[is_ws]);
2404 if (reduce_data == NULL &&
2405 __kmp_atomic_compare_store(&team->t.t_tg_reduce_data[is_ws], reduce_data,
2408 KMP_DEBUG_ASSERT(reduce_data == NULL);
2410 tg = (kmp_taskgroup_t *)__kmp_task_reduction_init<T>(gtid, num, data);
2414 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[0]) == 0);
2415 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[1]) == 0);
2416 KMP_ATOMIC_ST_REL(&team->t.t_tg_reduce_data[is_ws], reduce_data);
2419 (reduce_data = KMP_ATOMIC_LD_ACQ(&team->t.t_tg_reduce_data[is_ws])) ==
2423 KMP_DEBUG_ASSERT(reduce_data > (
void *)1);
2424 tg = thr->th.th_current_task->td_taskgroup;
2425 __kmp_task_reduction_init_copy<T>(thr, num, data, tg, reduce_data);
2447 int num,
void *data) {
2448 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2468 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2481 __kmpc_end_taskgroup(loc, gtid);
2485 void __kmpc_taskgroup(
ident_t *loc,
int gtid) {
2486 __kmp_assert_valid_gtid(gtid);
2487 kmp_info_t *thread = __kmp_threads[gtid];
2488 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2489 kmp_taskgroup_t *tg_new =
2490 (kmp_taskgroup_t *)__kmp_thread_malloc(thread,
sizeof(kmp_taskgroup_t));
2491 KA_TRACE(10, (
"__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new));
2492 KMP_ATOMIC_ST_RLX(&tg_new->count, 0);
2493 KMP_ATOMIC_ST_RLX(&tg_new->cancel_request, cancel_noreq);
2494 tg_new->parent = taskdata->td_taskgroup;
2495 tg_new->reduce_data = NULL;
2496 tg_new->reduce_num_data = 0;
2497 tg_new->gomp_data = NULL;
2498 taskdata->td_taskgroup = tg_new;
2500 #if OMPT_SUPPORT && OMPT_OPTIONAL
2501 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2502 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2504 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2505 kmp_team_t *team = thread->th.th_team;
2506 ompt_data_t my_task_data = taskdata->ompt_task_info.task_data;
2508 ompt_data_t my_parallel_data = team->t.ompt_team_info.parallel_data;
2510 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2511 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2512 &(my_task_data), codeptr);
2519 void __kmpc_end_taskgroup(
ident_t *loc,
int gtid) {
2520 __kmp_assert_valid_gtid(gtid);
2521 kmp_info_t *thread = __kmp_threads[gtid];
2522 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2523 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
2524 int thread_finished = FALSE;
2526 #if OMPT_SUPPORT && OMPT_OPTIONAL
2528 ompt_data_t my_task_data;
2529 ompt_data_t my_parallel_data;
2530 void *codeptr =
nullptr;
2531 if (UNLIKELY(ompt_enabled.enabled)) {
2532 team = thread->th.th_team;
2533 my_task_data = taskdata->ompt_task_info.task_data;
2535 my_parallel_data = team->t.ompt_team_info.parallel_data;
2536 codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2538 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2542 KA_TRACE(10, (
"__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc));
2543 KMP_DEBUG_ASSERT(taskgroup != NULL);
2544 KMP_SET_THREAD_STATE_BLOCK(TASKGROUP);
2546 if (__kmp_tasking_mode != tskm_immediate_exec) {
2548 taskdata->td_taskwait_counter += 1;
2549 taskdata->td_taskwait_ident = loc;
2550 taskdata->td_taskwait_thread = gtid + 1;
2554 void *itt_sync_obj = NULL;
2556 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2560 #if OMPT_SUPPORT && OMPT_OPTIONAL
2561 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2562 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2563 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2564 &(my_task_data), codeptr);
2568 if (!taskdata->td_flags.team_serial ||
2569 (thread->th.th_task_team != NULL &&
2570 (thread->th.th_task_team->tt.tt_found_proxy_tasks ||
2571 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered))) {
2572 kmp_flag_32<false, false> flag(
2573 RCAST(std::atomic<kmp_uint32> *, &(taskgroup->count)), 0U);
2574 while (KMP_ATOMIC_LD_ACQ(&taskgroup->count) != 0) {
2575 flag.execute_tasks(thread, gtid, FALSE,
2576 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2577 __kmp_task_stealing_constraint);
2580 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2582 #if OMPT_SUPPORT && OMPT_OPTIONAL
2583 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2584 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2585 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2586 &(my_task_data), codeptr);
2591 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2592 KMP_FSYNC_ACQUIRED(taskdata);
2595 KMP_DEBUG_ASSERT(taskgroup->count == 0);
2597 if (taskgroup->reduce_data != NULL &&
2598 !taskgroup->gomp_data) {
2601 kmp_team_t *t = thread->th.th_team;
2605 if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[0])) != NULL &&
2608 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[0]);
2609 if (cnt == thread->th.th_team_nproc - 1) {
2612 __kmp_task_reduction_fini(thread, taskgroup);
2615 __kmp_thread_free(thread, reduce_data);
2616 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[0], NULL);
2617 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[0], 0);
2621 __kmp_task_reduction_clean(thread, taskgroup);
2623 }
else if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[1])) !=
2627 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[1]);
2628 if (cnt == thread->th.th_team_nproc - 1) {
2630 __kmp_task_reduction_fini(thread, taskgroup);
2633 __kmp_thread_free(thread, reduce_data);
2634 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[1], NULL);
2635 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[1], 0);
2639 __kmp_task_reduction_clean(thread, taskgroup);
2643 __kmp_task_reduction_fini(thread, taskgroup);
2647 taskdata->td_taskgroup = taskgroup->parent;
2648 __kmp_thread_free(thread, taskgroup);
2650 KA_TRACE(10, (
"__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n",
2653 #if OMPT_SUPPORT && OMPT_OPTIONAL
2654 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2655 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2656 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2657 &(my_task_data), codeptr);
2663 static kmp_task_t *__kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid,
2664 kmp_task_team_t *task_team,
2665 kmp_int32 is_constrained) {
2667 kmp_taskdata_t *taskdata;
2668 kmp_thread_data_t *thread_data;
2671 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2672 KMP_DEBUG_ASSERT(task_team->tt.tt_threads_data !=
2675 thread_data = &task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
2677 KA_TRACE(10, (
"__kmp_remove_my_task(enter): T#%d 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));
2681 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2683 (
"__kmp_remove_my_task(exit #1): T#%d No tasks to remove: "
2684 "ntasks=%d head=%u tail=%u\n",
2685 gtid, thread_data->td.td_deque_ntasks,
2686 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2690 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
2692 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2693 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2695 (
"__kmp_remove_my_task(exit #2): T#%d No tasks to remove: "
2696 "ntasks=%d head=%u tail=%u\n",
2697 gtid, thread_data->td.td_deque_ntasks,
2698 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2702 tail = (thread_data->td.td_deque_tail - 1) &
2703 TASK_DEQUE_MASK(thread_data->td);
2704 taskdata = thread_data->td.td_deque[tail];
2706 if (!__kmp_task_is_allowed(gtid, is_constrained, taskdata,
2707 thread->th.th_current_task)) {
2709 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2711 (
"__kmp_remove_my_task(exit #3): T#%d TSC blocks tail task: "
2712 "ntasks=%d head=%u tail=%u\n",
2713 gtid, thread_data->td.td_deque_ntasks,
2714 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2718 thread_data->td.td_deque_tail = tail;
2719 TCW_4(thread_data->td.td_deque_ntasks, thread_data->td.td_deque_ntasks - 1);
2721 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2723 KA_TRACE(10, (
"__kmp_remove_my_task(exit #4): T#%d task %p removed: "
2724 "ntasks=%d head=%u tail=%u\n",
2725 gtid, taskdata, thread_data->td.td_deque_ntasks,
2726 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2728 task = KMP_TASKDATA_TO_TASK(taskdata);
2735 static kmp_task_t *__kmp_steal_task(kmp_info_t *victim_thr, kmp_int32 gtid,
2736 kmp_task_team_t *task_team,
2737 std::atomic<kmp_int32> *unfinished_threads,
2738 int *thread_finished,
2739 kmp_int32 is_constrained) {
2741 kmp_taskdata_t *taskdata;
2742 kmp_taskdata_t *current;
2743 kmp_thread_data_t *victim_td, *threads_data;
2745 kmp_int32 victim_tid;
2747 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2749 threads_data = task_team->tt.tt_threads_data;
2750 KMP_DEBUG_ASSERT(threads_data != NULL);
2752 victim_tid = victim_thr->th.th_info.ds.ds_tid;
2753 victim_td = &threads_data[victim_tid];
2755 KA_TRACE(10, (
"__kmp_steal_task(enter): T#%d try to 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));
2761 if (TCR_4(victim_td->td.td_deque_ntasks) == 0) {
2762 KA_TRACE(10, (
"__kmp_steal_task(exit #1): T#%d could not steal from T#%d: "
2763 "task_team=%p ntasks=%d head=%u tail=%u\n",
2764 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
2765 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
2766 victim_td->td.td_deque_tail));
2770 __kmp_acquire_bootstrap_lock(&victim_td->td.td_deque_lock);
2772 int ntasks = TCR_4(victim_td->td.td_deque_ntasks);
2775 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2776 KA_TRACE(10, (
"__kmp_steal_task(exit #2): T#%d could not steal from T#%d: "
2777 "task_team=%p ntasks=%d head=%u tail=%u\n",
2778 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2779 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2783 KMP_DEBUG_ASSERT(victim_td->td.td_deque != NULL);
2784 current = __kmp_threads[gtid]->th.th_current_task;
2785 taskdata = victim_td->td.td_deque[victim_td->td.td_deque_head];
2786 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
2788 victim_td->td.td_deque_head =
2789 (victim_td->td.td_deque_head + 1) & TASK_DEQUE_MASK(victim_td->td);
2791 if (!task_team->tt.tt_untied_task_encountered) {
2793 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2794 KA_TRACE(10, (
"__kmp_steal_task(exit #3): T#%d could not steal from "
2795 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
2796 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2797 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2802 target = victim_td->td.td_deque_head;
2804 for (i = 1; i < ntasks; ++i) {
2805 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
2806 taskdata = victim_td->td.td_deque[target];
2807 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
2813 if (taskdata == NULL) {
2815 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2816 KA_TRACE(10, (
"__kmp_steal_task(exit #4): T#%d could not steal from "
2817 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
2818 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2819 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2823 for (i = i + 1; i < ntasks; ++i) {
2825 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
2826 victim_td->td.td_deque[prev] = victim_td->td.td_deque[target];
2830 victim_td->td.td_deque_tail ==
2831 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(victim_td->td)));
2832 victim_td->td.td_deque_tail = target;
2834 if (*thread_finished) {
2841 KMP_ATOMIC_INC(unfinished_threads);
2844 (
"__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n",
2845 gtid, count + 1, task_team));
2846 *thread_finished = FALSE;
2848 TCW_4(victim_td->td.td_deque_ntasks, ntasks - 1);
2850 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2854 (
"__kmp_steal_task(exit #5): T#%d stole task %p from T#%d: "
2855 "task_team=%p ntasks=%d head=%u tail=%u\n",
2856 gtid, taskdata, __kmp_gtid_from_thread(victim_thr), task_team,
2857 ntasks, victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2859 task = KMP_TASKDATA_TO_TASK(taskdata);
2873 static inline int __kmp_execute_tasks_template(
2874 kmp_info_t *thread, kmp_int32 gtid, C *flag,
int final_spin,
2875 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
2876 kmp_int32 is_constrained) {
2877 kmp_task_team_t *task_team = thread->th.th_task_team;
2878 kmp_thread_data_t *threads_data;
2880 kmp_info_t *other_thread;
2881 kmp_taskdata_t *current_task = thread->th.th_current_task;
2882 std::atomic<kmp_int32> *unfinished_threads;
2883 kmp_int32 nthreads, victim_tid = -2, use_own_tasks = 1, new_victim = 0,
2884 tid = thread->th.th_info.ds.ds_tid;
2886 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2887 KMP_DEBUG_ASSERT(thread == __kmp_threads[gtid]);
2889 if (task_team == NULL || current_task == NULL)
2892 KA_TRACE(15, (
"__kmp_execute_tasks_template(enter): T#%d final_spin=%d "
2893 "*thread_finished=%d\n",
2894 gtid, final_spin, *thread_finished));
2896 thread->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
2897 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
2899 KMP_DEBUG_ASSERT(threads_data != NULL);
2901 nthreads = task_team->tt.tt_nproc;
2902 unfinished_threads = &(task_team->tt.tt_unfinished_threads);
2903 KMP_DEBUG_ASSERT(nthreads > 1 || task_team->tt.tt_found_proxy_tasks ||
2904 task_team->tt.tt_hidden_helper_task_encountered);
2905 KMP_DEBUG_ASSERT(*unfinished_threads >= 0);
2911 if (use_own_tasks) {
2912 task = __kmp_remove_my_task(thread, gtid, task_team, is_constrained);
2914 if ((task == NULL) && (nthreads > 1)) {
2918 if (victim_tid == -2) {
2919 victim_tid = threads_data[tid].td.td_deque_last_stolen;
2922 other_thread = threads_data[victim_tid].td.td_thr;
2924 if (victim_tid != -1) {
2926 }
else if (!new_victim) {
2932 victim_tid = __kmp_get_random(thread) % (nthreads - 1);
2933 if (victim_tid >= tid) {
2937 other_thread = threads_data[victim_tid].td.td_thr;
2947 if ((__kmp_tasking_mode == tskm_task_teams) &&
2948 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) &&
2949 (TCR_PTR(CCAST(
void *, other_thread->th.th_sleep_loc)) !=
2952 __kmp_null_resume_wrapper(other_thread);
2965 task = __kmp_steal_task(other_thread, gtid, task_team,
2966 unfinished_threads, thread_finished,
2970 if (threads_data[tid].td.td_deque_last_stolen != victim_tid) {
2971 threads_data[tid].td.td_deque_last_stolen = victim_tid;
2978 KMP_CHECK_UPDATE(threads_data[tid].td.td_deque_last_stolen, -1);
2987 #if USE_ITT_BUILD && USE_ITT_NOTIFY
2988 if (__itt_sync_create_ptr || KMP_ITT_DEBUG) {
2989 if (itt_sync_obj == NULL) {
2991 itt_sync_obj = __kmp_itt_barrier_object(gtid, bs_forkjoin_barrier);
2993 __kmp_itt_task_starting(itt_sync_obj);
2996 __kmp_invoke_task(gtid, task, current_task);
2998 if (itt_sync_obj != NULL)
2999 __kmp_itt_task_finished(itt_sync_obj);
3006 if (flag == NULL || (!final_spin && flag->done_check())) {
3009 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3013 if (thread->th.th_task_team == NULL) {
3016 KMP_YIELD(__kmp_library == library_throughput);
3019 if (!use_own_tasks && TCR_4(threads_data[tid].td.td_deque_ntasks) != 0) {
3020 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d stolen task spawned "
3021 "other tasks, restart\n",
3032 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks) == 0) {
3036 if (!*thread_finished) {
3038 kmp_int32 count = -1 +
3040 KMP_ATOMIC_DEC(unfinished_threads);
3041 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d dec "
3042 "unfinished_threads to %d task_team=%p\n",
3043 gtid, count, task_team));
3044 *thread_finished = TRUE;
3052 if (flag != NULL && flag->done_check()) {
3055 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3063 if (thread->th.th_task_team == NULL) {
3065 (
"__kmp_execute_tasks_template: T#%d no more tasks\n", gtid));
3071 if (nthreads == 1 &&
3072 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks))
3076 (
"__kmp_execute_tasks_template: T#%d can't find work\n", gtid));
3082 template <
bool C,
bool S>
3083 int __kmp_execute_tasks_32(
3084 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32<C, S> *flag,
int final_spin,
3085 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3086 kmp_int32 is_constrained) {
3087 return __kmp_execute_tasks_template(
3088 thread, gtid, flag, final_spin,
3089 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3092 template <
bool C,
bool S>
3093 int __kmp_execute_tasks_64(
3094 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64<C, S> *flag,
int final_spin,
3095 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3096 kmp_int32 is_constrained) {
3097 return __kmp_execute_tasks_template(
3098 thread, gtid, flag, final_spin,
3099 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3102 template <
bool C,
bool S>
3103 int __kmp_atomic_execute_tasks_64(
3104 kmp_info_t *thread, kmp_int32 gtid, kmp_atomic_flag_64<C, S> *flag,
3105 int final_spin,
int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3106 kmp_int32 is_constrained) {
3107 return __kmp_execute_tasks_template(
3108 thread, gtid, flag, final_spin,
3109 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3112 int __kmp_execute_tasks_oncore(
3113 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag,
int final_spin,
3114 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3115 kmp_int32 is_constrained) {
3116 return __kmp_execute_tasks_template(
3117 thread, gtid, flag, final_spin,
3118 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3122 __kmp_execute_tasks_32<false, false>(kmp_info_t *, kmp_int32,
3123 kmp_flag_32<false, false> *,
int,
3124 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3126 template int __kmp_execute_tasks_64<false, true>(kmp_info_t *, kmp_int32,
3127 kmp_flag_64<false, true> *,
3129 int *USE_ITT_BUILD_ARG(
void *),
3132 template int __kmp_execute_tasks_64<true, false>(kmp_info_t *, kmp_int32,
3133 kmp_flag_64<true, false> *,
3135 int *USE_ITT_BUILD_ARG(
void *),
3138 template int __kmp_atomic_execute_tasks_64<false, true>(
3139 kmp_info_t *, kmp_int32, kmp_atomic_flag_64<false, true> *,
int,
3140 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3142 template int __kmp_atomic_execute_tasks_64<true, false>(
3143 kmp_info_t *, kmp_int32, kmp_atomic_flag_64<true, false> *,
int,
3144 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3149 static void __kmp_enable_tasking(kmp_task_team_t *task_team,
3150 kmp_info_t *this_thr) {
3151 kmp_thread_data_t *threads_data;
3152 int nthreads, i, is_init_thread;
3154 KA_TRACE(10, (
"__kmp_enable_tasking(enter): T#%d\n",
3155 __kmp_gtid_from_thread(this_thr)));
3157 KMP_DEBUG_ASSERT(task_team != NULL);
3158 KMP_DEBUG_ASSERT(this_thr->th.th_team != NULL);
3160 nthreads = task_team->tt.tt_nproc;
3161 KMP_DEBUG_ASSERT(nthreads > 0);
3162 KMP_DEBUG_ASSERT(nthreads == this_thr->th.th_team->t.t_nproc);
3165 is_init_thread = __kmp_realloc_task_threads_data(this_thr, task_team);
3167 if (!is_init_thread) {
3171 (
"__kmp_enable_tasking(exit): T#%d: threads array already set up.\n",
3172 __kmp_gtid_from_thread(this_thr)));
3175 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
3176 KMP_DEBUG_ASSERT(threads_data != NULL);
3178 if (__kmp_tasking_mode == tskm_task_teams &&
3179 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME)) {
3183 for (i = 0; i < nthreads; i++) {
3185 kmp_info_t *thread = threads_data[i].td.td_thr;
3187 if (i == this_thr->th.th_info.ds.ds_tid) {
3196 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3198 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d waking up thread T#%d\n",
3199 __kmp_gtid_from_thread(this_thr),
3200 __kmp_gtid_from_thread(thread)));
3201 __kmp_null_resume_wrapper(thread);
3203 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d don't wake up thread T#%d\n",
3204 __kmp_gtid_from_thread(this_thr),
3205 __kmp_gtid_from_thread(thread)));
3210 KA_TRACE(10, (
"__kmp_enable_tasking(exit): T#%d\n",
3211 __kmp_gtid_from_thread(this_thr)));
3244 static kmp_task_team_t *__kmp_free_task_teams =
3247 kmp_bootstrap_lock_t __kmp_task_team_lock =
3248 KMP_BOOTSTRAP_LOCK_INITIALIZER(__kmp_task_team_lock);
3255 static void __kmp_alloc_task_deque(kmp_info_t *thread,
3256 kmp_thread_data_t *thread_data) {
3257 __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
3258 KMP_DEBUG_ASSERT(thread_data->td.td_deque == NULL);
3261 thread_data->td.td_deque_last_stolen = -1;
3263 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == 0);
3264 KMP_DEBUG_ASSERT(thread_data->td.td_deque_head == 0);
3265 KMP_DEBUG_ASSERT(thread_data->td.td_deque_tail == 0);
3269 (
"__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n",
3270 __kmp_gtid_from_thread(thread), INITIAL_TASK_DEQUE_SIZE, thread_data));
3274 thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
3275 INITIAL_TASK_DEQUE_SIZE *
sizeof(kmp_taskdata_t *));
3276 thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
3282 static void __kmp_free_task_deque(kmp_thread_data_t *thread_data) {
3283 if (thread_data->td.td_deque != NULL) {
3284 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3285 TCW_4(thread_data->td.td_deque_ntasks, 0);
3286 __kmp_free(thread_data->td.td_deque);
3287 thread_data->td.td_deque = NULL;
3288 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3291 #ifdef BUILD_TIED_TASK_STACK
3293 if (thread_data->td.td_susp_tied_tasks.ts_entries != TASK_STACK_EMPTY) {
3294 __kmp_free_task_stack(__kmp_thread_from_gtid(gtid), thread_data);
3306 static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
3307 kmp_task_team_t *task_team) {
3308 kmp_thread_data_t **threads_data_p;
3309 kmp_int32 nthreads, maxthreads;
3310 int is_init_thread = FALSE;
3312 if (TCR_4(task_team->tt.tt_found_tasks)) {
3317 threads_data_p = &task_team->tt.tt_threads_data;
3318 nthreads = task_team->tt.tt_nproc;
3319 maxthreads = task_team->tt.tt_max_threads;
3324 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3326 if (!TCR_4(task_team->tt.tt_found_tasks)) {
3328 kmp_team_t *team = thread->th.th_team;
3331 is_init_thread = TRUE;
3332 if (maxthreads < nthreads) {
3334 if (*threads_data_p != NULL) {
3335 kmp_thread_data_t *old_data = *threads_data_p;
3336 kmp_thread_data_t *new_data = NULL;
3340 (
"__kmp_realloc_task_threads_data: T#%d reallocating "
3341 "threads data for task_team %p, new_size = %d, old_size = %d\n",
3342 __kmp_gtid_from_thread(thread), task_team, nthreads, maxthreads));
3347 new_data = (kmp_thread_data_t *)__kmp_allocate(
3348 nthreads *
sizeof(kmp_thread_data_t));
3350 KMP_MEMCPY_S((
void *)new_data, nthreads *
sizeof(kmp_thread_data_t),
3351 (
void *)old_data, maxthreads *
sizeof(kmp_thread_data_t));
3353 #ifdef BUILD_TIED_TASK_STACK
3355 for (i = maxthreads; i < nthreads; i++) {
3356 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3357 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3361 (*threads_data_p) = new_data;
3362 __kmp_free(old_data);
3364 KE_TRACE(10, (
"__kmp_realloc_task_threads_data: T#%d allocating "
3365 "threads data for task_team %p, size = %d\n",
3366 __kmp_gtid_from_thread(thread), task_team, nthreads));
3370 *threads_data_p = (kmp_thread_data_t *)__kmp_allocate(
3371 nthreads *
sizeof(kmp_thread_data_t));
3372 #ifdef BUILD_TIED_TASK_STACK
3374 for (i = 0; i < nthreads; i++) {
3375 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3376 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3380 task_team->tt.tt_max_threads = nthreads;
3383 KMP_DEBUG_ASSERT(*threads_data_p != NULL);
3387 for (i = 0; i < nthreads; i++) {
3388 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3389 thread_data->td.td_thr = team->t.t_threads[i];
3391 if (thread_data->td.td_deque_last_stolen >= nthreads) {
3395 thread_data->td.td_deque_last_stolen = -1;
3400 TCW_SYNC_4(task_team->tt.tt_found_tasks, TRUE);
3403 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3404 return is_init_thread;
3410 static void __kmp_free_task_threads_data(kmp_task_team_t *task_team) {
3411 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3412 if (task_team->tt.tt_threads_data != NULL) {
3414 for (i = 0; i < task_team->tt.tt_max_threads; i++) {
3415 __kmp_free_task_deque(&task_team->tt.tt_threads_data[i]);
3417 __kmp_free(task_team->tt.tt_threads_data);
3418 task_team->tt.tt_threads_data = NULL;
3420 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3427 static kmp_task_team_t *__kmp_allocate_task_team(kmp_info_t *thread,
3429 kmp_task_team_t *task_team = NULL;
3432 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d entering; team = %p\n",
3433 (thread ? __kmp_gtid_from_thread(thread) : -1), team));
3435 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3437 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3438 if (__kmp_free_task_teams != NULL) {
3439 task_team = __kmp_free_task_teams;
3440 TCW_PTR(__kmp_free_task_teams, task_team->tt.tt_next);
3441 task_team->tt.tt_next = NULL;
3443 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3446 if (task_team == NULL) {
3447 KE_TRACE(10, (
"__kmp_allocate_task_team: T#%d allocating "
3448 "task team for team %p\n",
3449 __kmp_gtid_from_thread(thread), team));
3452 task_team = (kmp_task_team_t *)__kmp_allocate(
sizeof(kmp_task_team_t));
3453 __kmp_init_bootstrap_lock(&task_team->tt.tt_threads_lock);
3454 #if USE_ITT_BUILD && USE_ITT_NOTIFY && KMP_DEBUG
3457 __itt_suppress_mark_range(
3458 __itt_suppress_range, __itt_suppress_threading_errors,
3459 &task_team->tt.tt_found_tasks,
sizeof(task_team->tt.tt_found_tasks));
3460 __itt_suppress_mark_range(__itt_suppress_range,
3461 __itt_suppress_threading_errors,
3462 CCAST(kmp_uint32 *, &task_team->tt.tt_active),
3463 sizeof(task_team->tt.tt_active));
3471 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3472 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3473 task_team->tt.tt_nproc = nthreads = team->t.t_nproc;
3475 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads, nthreads);
3476 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
3477 TCW_4(task_team->tt.tt_active, TRUE);
3479 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d exiting; task_team = %p "
3480 "unfinished_threads init'd to %d\n",
3481 (thread ? __kmp_gtid_from_thread(thread) : -1), task_team,
3482 KMP_ATOMIC_LD_RLX(&task_team->tt.tt_unfinished_threads)));
3489 void __kmp_free_task_team(kmp_info_t *thread, kmp_task_team_t *task_team) {
3490 KA_TRACE(20, (
"__kmp_free_task_team: T#%d task_team = %p\n",
3491 thread ? __kmp_gtid_from_thread(thread) : -1, task_team));
3494 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3496 KMP_DEBUG_ASSERT(task_team->tt.tt_next == NULL);
3497 task_team->tt.tt_next = __kmp_free_task_teams;
3498 TCW_PTR(__kmp_free_task_teams, task_team);
3500 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3508 void __kmp_reap_task_teams(
void) {
3509 kmp_task_team_t *task_team;
3511 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3513 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3514 while ((task_team = __kmp_free_task_teams) != NULL) {
3515 __kmp_free_task_teams = task_team->tt.tt_next;
3516 task_team->tt.tt_next = NULL;
3519 if (task_team->tt.tt_threads_data != NULL) {
3520 __kmp_free_task_threads_data(task_team);
3522 __kmp_free(task_team);
3524 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3531 void __kmp_wait_to_unref_task_teams(
void) {
3536 KMP_INIT_YIELD(spins);
3544 for (thread = CCAST(kmp_info_t *, __kmp_thread_pool); thread != NULL;
3545 thread = thread->th.th_next_pool) {
3549 if (TCR_PTR(thread->th.th_task_team) == NULL) {
3550 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n",
3551 __kmp_gtid_from_thread(thread)));
3556 if (!__kmp_is_thread_alive(thread, &exit_val)) {
3557 thread->th.th_task_team = NULL;
3564 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: Waiting for T#%d to "
3565 "unreference task_team\n",
3566 __kmp_gtid_from_thread(thread)));
3568 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
3571 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3575 (
"__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n",
3576 __kmp_gtid_from_thread(thread), __kmp_gtid_from_thread(thread)));
3577 __kmp_null_resume_wrapper(thread);
3586 KMP_YIELD_OVERSUB_ELSE_SPIN(spins);
3592 void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team,
int always) {
3593 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3599 if (team->t.t_task_team[this_thr->th.th_task_state] == NULL &&
3600 (always || team->t.t_nproc > 1)) {
3601 team->t.t_task_team[this_thr->th.th_task_state] =
3602 __kmp_allocate_task_team(this_thr, team);
3603 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d created new task_team %p"
3604 " for team %d at parity=%d\n",
3605 __kmp_gtid_from_thread(this_thr),
3606 team->t.t_task_team[this_thr->th.th_task_state], team->t.t_id,
3607 this_thr->th.th_task_state));
3617 if (team->t.t_nproc > 1) {
3618 int other_team = 1 - this_thr->th.th_task_state;
3619 KMP_DEBUG_ASSERT(other_team >= 0 && other_team < 2);
3620 if (team->t.t_task_team[other_team] == NULL) {
3621 team->t.t_task_team[other_team] =
3622 __kmp_allocate_task_team(this_thr, team);
3623 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d created second new "
3624 "task_team %p for team %d at parity=%d\n",
3625 __kmp_gtid_from_thread(this_thr),
3626 team->t.t_task_team[other_team], team->t.t_id, other_team));
3629 kmp_task_team_t *task_team = team->t.t_task_team[other_team];
3630 if (!task_team->tt.tt_active ||
3631 team->t.t_nproc != task_team->tt.tt_nproc) {
3632 TCW_4(task_team->tt.tt_nproc, team->t.t_nproc);
3633 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3634 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3635 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads,
3637 TCW_4(task_team->tt.tt_active, TRUE);
3641 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d reset next task_team "
3642 "%p for team %d at parity=%d\n",
3643 __kmp_gtid_from_thread(this_thr),
3644 team->t.t_task_team[other_team], team->t.t_id, other_team));
3652 if (this_thr == __kmp_hidden_helper_main_thread) {
3653 for (
int i = 0; i < 2; ++i) {
3654 kmp_task_team_t *task_team = team->t.t_task_team[i];
3655 if (KMP_TASKING_ENABLED(task_team)) {
3658 __kmp_enable_tasking(task_team, this_thr);
3659 for (
int j = 0; j < task_team->tt.tt_nproc; ++j) {
3660 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[j];
3661 if (thread_data->td.td_deque == NULL) {
3662 __kmp_alloc_task_deque(__kmp_hidden_helper_threads[j], thread_data);
3672 void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team) {
3673 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3677 this_thr->th.th_task_state = (kmp_uint8)(1 - this_thr->th.th_task_state);
3681 TCW_PTR(this_thr->th.th_task_team,
3682 team->t.t_task_team[this_thr->th.th_task_state]);
3684 (
"__kmp_task_team_sync: Thread T#%d task team switched to task_team "
3685 "%p from Team #%d (parity=%d)\n",
3686 __kmp_gtid_from_thread(this_thr), this_thr->th.th_task_team,
3687 team->t.t_id, this_thr->th.th_task_state));
3697 void __kmp_task_team_wait(
3698 kmp_info_t *this_thr,
3699 kmp_team_t *team USE_ITT_BUILD_ARG(
void *itt_sync_obj),
int wait) {
3700 kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state];
3702 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3703 KMP_DEBUG_ASSERT(task_team == this_thr->th.th_task_team);
3705 if ((task_team != NULL) && KMP_TASKING_ENABLED(task_team)) {
3707 KA_TRACE(20, (
"__kmp_task_team_wait: Primary T#%d waiting for all tasks "
3708 "(for unfinished_threads to reach 0) on task_team = %p\n",
3709 __kmp_gtid_from_thread(this_thr), task_team));
3713 kmp_flag_32<false, false> flag(
3714 RCAST(std::atomic<kmp_uint32> *,
3715 &task_team->tt.tt_unfinished_threads),
3717 flag.wait(this_thr, TRUE USE_ITT_BUILD_ARG(itt_sync_obj));
3723 (
"__kmp_task_team_wait: Primary T#%d deactivating task_team %p: "
3724 "setting active to false, setting local and team's pointer to NULL\n",
3725 __kmp_gtid_from_thread(this_thr), task_team));
3726 KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1 ||
3727 task_team->tt.tt_found_proxy_tasks == TRUE);
3728 TCW_SYNC_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3729 KMP_CHECK_UPDATE(task_team->tt.tt_untied_task_encountered, 0);
3730 TCW_SYNC_4(task_team->tt.tt_active, FALSE);
3733 TCW_PTR(this_thr->th.th_task_team, NULL);
3742 void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread,
int gtid) {
3743 std::atomic<kmp_uint32> *spin = RCAST(
3744 std::atomic<kmp_uint32> *,
3745 &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads);
3747 KMP_DEBUG_ASSERT(__kmp_tasking_mode == tskm_extra_barrier);
3750 KMP_FSYNC_SPIN_INIT(spin, NULL);
3752 kmp_flag_32<false, false> spin_flag(spin, 0U);
3753 while (!spin_flag.execute_tasks(thread, gtid, TRUE,
3754 &flag USE_ITT_BUILD_ARG(NULL), 0)) {
3757 KMP_FSYNC_SPIN_PREPARE(RCAST(
void *, spin));
3760 if (TCR_4(__kmp_global.g.g_done)) {
3761 if (__kmp_global.g.g_abort)
3762 __kmp_abort_thread();
3768 KMP_FSYNC_SPIN_ACQUIRED(RCAST(
void *, spin));
3777 static bool __kmp_give_task(kmp_info_t *thread, kmp_int32 tid, kmp_task_t *task,
3779 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
3780 kmp_task_team_t *task_team = taskdata->td_task_team;
3782 KA_TRACE(20, (
"__kmp_give_task: trying to give task %p to thread %d.\n",
3786 KMP_DEBUG_ASSERT(task_team != NULL);
3788 bool result =
false;
3789 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
3791 if (thread_data->td.td_deque == NULL) {
3795 (
"__kmp_give_task: thread %d has no queue while giving task %p.\n",
3800 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3801 TASK_DEQUE_SIZE(thread_data->td)) {
3804 (
"__kmp_give_task: queue is full while giving task %p to thread %d.\n",
3809 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
3812 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3813 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3814 TASK_DEQUE_SIZE(thread_data->td)) {
3816 __kmp_realloc_task_deque(thread, thread_data);
3821 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3823 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3824 TASK_DEQUE_SIZE(thread_data->td)) {
3825 KA_TRACE(30, (
"__kmp_give_task: queue is full while giving task %p to "
3831 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
3832 goto release_and_exit;
3834 __kmp_realloc_task_deque(thread, thread_data);
3840 thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
3842 thread_data->td.td_deque_tail =
3843 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
3844 TCW_4(thread_data->td.td_deque_ntasks,
3845 TCR_4(thread_data->td.td_deque_ntasks) + 1);
3848 KA_TRACE(30, (
"__kmp_give_task: successfully gave task %p to thread %d.\n",
3852 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3857 #define PROXY_TASK_FLAG 0x40000000
3874 static void __kmp_first_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
3875 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
3876 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3877 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
3878 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
3880 taskdata->td_flags.complete = 1;
3882 if (taskdata->td_taskgroup)
3883 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
3887 KMP_ATOMIC_OR(&taskdata->td_incomplete_child_tasks, PROXY_TASK_FLAG);
3890 static void __kmp_second_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
3892 kmp_int32 children = 0;
3896 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks);
3897 KMP_DEBUG_ASSERT(children >= 0);
3900 KMP_ATOMIC_AND(&taskdata->td_incomplete_child_tasks, ~PROXY_TASK_FLAG);
3903 static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask) {
3904 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3905 kmp_info_t *thread = __kmp_threads[gtid];
3907 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3908 KMP_DEBUG_ASSERT(taskdata->td_flags.complete ==
3913 while ((KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) &
3914 PROXY_TASK_FLAG) > 0)
3917 __kmp_release_deps(gtid, taskdata);
3918 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
3930 KMP_DEBUG_ASSERT(ptask != NULL);
3931 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3933 10, (
"__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n",
3935 __kmp_assert_valid_gtid(gtid);
3936 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3938 __kmp_first_top_half_finish_proxy(taskdata);
3939 __kmp_second_top_half_finish_proxy(taskdata);
3940 __kmp_bottom_half_finish_proxy(gtid, ptask);
3943 (
"__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n",
3947 void __kmpc_give_task(kmp_task_t *ptask, kmp_int32 start = 0) {
3948 KMP_DEBUG_ASSERT(ptask != NULL);
3949 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3953 kmp_team_t *team = taskdata->td_team;
3954 kmp_int32 nthreads = team->t.t_nproc;
3959 kmp_int32 start_k = start;
3961 kmp_int32 k = start_k;
3965 thread = team->t.t_threads[k];
3966 k = (k + 1) % nthreads;
3972 }
while (!__kmp_give_task(thread, k, ptask, pass));
3983 KMP_DEBUG_ASSERT(ptask != NULL);
3984 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3988 (
"__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n",
3991 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3993 __kmp_first_top_half_finish_proxy(taskdata);
3995 __kmpc_give_task(ptask);
3997 __kmp_second_top_half_finish_proxy(taskdata);
4001 (
"__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n",
4005 kmp_event_t *__kmpc_task_allow_completion_event(
ident_t *loc_ref,
int gtid,
4007 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(task);
4008 if (td->td_allow_completion_event.type == KMP_EVENT_UNINITIALIZED) {
4009 td->td_allow_completion_event.type = KMP_EVENT_ALLOW_COMPLETION;
4010 td->td_allow_completion_event.ed.task = task;
4011 __kmp_init_tas_lock(&td->td_allow_completion_event.lock);
4013 return &td->td_allow_completion_event;
4016 void __kmp_fulfill_event(kmp_event_t *event) {
4017 if (event->type == KMP_EVENT_ALLOW_COMPLETION) {
4018 kmp_task_t *ptask = event->ed.task;
4019 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4020 bool detached =
false;
4021 int gtid = __kmp_get_gtid();
4026 __kmp_acquire_tas_lock(&event->lock, gtid);
4027 if (taskdata->td_flags.proxy == TASK_PROXY) {
4033 if (UNLIKELY(ompt_enabled.enabled))
4034 __ompt_task_finish(ptask, NULL, ompt_task_early_fulfill);
4037 event->type = KMP_EVENT_UNINITIALIZED;
4038 __kmp_release_tas_lock(&event->lock, gtid);
4044 if (UNLIKELY(ompt_enabled.enabled))
4045 __ompt_task_finish(ptask, NULL, ompt_task_late_fulfill);
4049 kmp_team_t *team = taskdata->td_team;
4050 kmp_info_t *thread = __kmp_get_thread();
4051 if (thread->th.th_team == team) {
4069 kmp_task_t *__kmp_task_dup_alloc(kmp_info_t *thread, kmp_task_t *task_src) {
4071 kmp_taskdata_t *taskdata;
4072 kmp_taskdata_t *taskdata_src = KMP_TASK_TO_TASKDATA(task_src);
4073 kmp_taskdata_t *parent_task = taskdata_src->td_parent;
4074 size_t shareds_offset;
4077 KA_TRACE(10, (
"__kmp_task_dup_alloc(enter): Th %p, source task %p\n", thread,
4079 KMP_DEBUG_ASSERT(taskdata_src->td_flags.proxy ==
4081 KMP_DEBUG_ASSERT(taskdata_src->td_flags.tasktype == TASK_EXPLICIT);
4082 task_size = taskdata_src->td_size_alloc;
4085 KA_TRACE(30, (
"__kmp_task_dup_alloc: Th %p, malloc size %ld\n", thread,
4088 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, task_size);
4090 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, task_size);
4092 KMP_MEMCPY(taskdata, taskdata_src, task_size);
4094 task = KMP_TASKDATA_TO_TASK(taskdata);
4097 taskdata->td_task_id = KMP_GEN_TASK_ID();
4098 if (task->shareds != NULL) {
4099 shareds_offset = (
char *)task_src->shareds - (
char *)taskdata_src;
4100 task->shareds = &((
char *)taskdata)[shareds_offset];
4101 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
4104 taskdata->td_alloc_thread = thread;
4105 taskdata->td_parent = parent_task;
4107 taskdata->td_taskgroup = parent_task->td_taskgroup;
4110 if (taskdata->td_flags.tiedness == TASK_TIED)
4111 taskdata->td_last_tied = taskdata;
4115 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
4116 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
4117 if (parent_task->td_taskgroup)
4118 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
4121 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT)
4122 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
4126 (
"__kmp_task_dup_alloc(exit): Th %p, created task %p, parent=%p\n",
4127 thread, taskdata, taskdata->td_parent));
4129 if (UNLIKELY(ompt_enabled.enabled))
4130 __ompt_task_init(taskdata, thread->th.th_info.ds.ds_gtid);
4139 typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32);
4141 KMP_BUILD_ASSERT(
sizeof(
long) == 4 ||
sizeof(
long) == 8);
4146 class kmp_taskloop_bounds_t {
4148 const kmp_taskdata_t *taskdata;
4149 size_t lower_offset;
4150 size_t upper_offset;
4153 kmp_taskloop_bounds_t(kmp_task_t *_task, kmp_uint64 *lb, kmp_uint64 *ub)
4154 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(task)),
4155 lower_offset((char *)lb - (char *)task),
4156 upper_offset((char *)ub - (char *)task) {
4157 KMP_DEBUG_ASSERT((
char *)lb > (
char *)_task);
4158 KMP_DEBUG_ASSERT((
char *)ub > (
char *)_task);
4160 kmp_taskloop_bounds_t(kmp_task_t *_task,
const kmp_taskloop_bounds_t &bounds)
4161 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(_task)),
4162 lower_offset(bounds.lower_offset), upper_offset(bounds.upper_offset) {}
4163 size_t get_lower_offset()
const {
return lower_offset; }
4164 size_t get_upper_offset()
const {
return upper_offset; }
4165 kmp_uint64 get_lb()
const {
4167 #if defined(KMP_GOMP_COMPAT)
4169 if (!taskdata->td_flags.native) {
4170 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4173 if (taskdata->td_size_loop_bounds == 4) {
4174 kmp_int32 *lb = RCAST(kmp_int32 *, task->shareds);
4175 retval = (kmp_int64)*lb;
4177 kmp_int64 *lb = RCAST(kmp_int64 *, task->shareds);
4178 retval = (kmp_int64)*lb;
4183 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4187 kmp_uint64 get_ub()
const {
4189 #if defined(KMP_GOMP_COMPAT)
4191 if (!taskdata->td_flags.native) {
4192 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4195 if (taskdata->td_size_loop_bounds == 4) {
4196 kmp_int32 *ub = RCAST(kmp_int32 *, task->shareds) + 1;
4197 retval = (kmp_int64)*ub;
4199 kmp_int64 *ub = RCAST(kmp_int64 *, task->shareds) + 1;
4200 retval = (kmp_int64)*ub;
4204 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4208 void set_lb(kmp_uint64 lb) {
4209 #if defined(KMP_GOMP_COMPAT)
4211 if (!taskdata->td_flags.native) {
4212 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4215 if (taskdata->td_size_loop_bounds == 4) {
4216 kmp_uint32 *lower = RCAST(kmp_uint32 *, task->shareds);
4217 *lower = (kmp_uint32)lb;
4219 kmp_uint64 *lower = RCAST(kmp_uint64 *, task->shareds);
4220 *lower = (kmp_uint64)lb;
4224 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4227 void set_ub(kmp_uint64 ub) {
4228 #if defined(KMP_GOMP_COMPAT)
4230 if (!taskdata->td_flags.native) {
4231 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4234 if (taskdata->td_size_loop_bounds == 4) {
4235 kmp_uint32 *upper = RCAST(kmp_uint32 *, task->shareds) + 1;
4236 *upper = (kmp_uint32)ub;
4238 kmp_uint64 *upper = RCAST(kmp_uint64 *, task->shareds) + 1;
4239 *upper = (kmp_uint64)ub;
4243 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4264 void __kmp_taskloop_linear(
ident_t *loc,
int gtid, kmp_task_t *task,
4265 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4266 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
4267 kmp_uint64 grainsize, kmp_uint64 extras,
4268 kmp_int64 last_chunk, kmp_uint64 tc,
4274 KMP_TIME_PARTITIONED_BLOCK(OMP_taskloop_scheduling);
4275 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
4277 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4278 kmp_uint64 lower = task_bounds.get_lb();
4279 kmp_uint64 upper = task_bounds.get_ub();
4281 kmp_info_t *thread = __kmp_threads[gtid];
4282 kmp_taskdata_t *current_task = thread->th.th_current_task;
4283 kmp_task_t *next_task;
4284 kmp_int32 lastpriv = 0;
4286 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4287 (last_chunk < 0 ? last_chunk : extras));
4288 KMP_DEBUG_ASSERT(num_tasks > extras);
4289 KMP_DEBUG_ASSERT(num_tasks > 0);
4290 KA_TRACE(20, (
"__kmp_taskloop_linear: T#%d: %lld tasks, grainsize %lld, "
4291 "extras %lld, last_chunk %lld, i=%lld,%lld(%d)%lld, dup %p\n",
4292 gtid, num_tasks, grainsize, extras, last_chunk, lower, upper,
4293 ub_glob, st, task_dup));
4296 for (i = 0; i < num_tasks; ++i) {
4297 kmp_uint64 chunk_minus_1;
4299 chunk_minus_1 = grainsize - 1;
4301 chunk_minus_1 = grainsize;
4304 upper = lower + st * chunk_minus_1;
4308 if (i == num_tasks - 1) {
4311 KMP_DEBUG_ASSERT(upper == *ub);
4312 if (upper == ub_glob)
4314 }
else if (st > 0) {
4315 KMP_DEBUG_ASSERT((kmp_uint64)st > *ub - upper);
4316 if ((kmp_uint64)st > ub_glob - upper)
4319 KMP_DEBUG_ASSERT(upper + st < *ub);
4320 if (upper - ub_glob < (kmp_uint64)(-st))
4324 next_task = __kmp_task_dup_alloc(thread, task);
4325 kmp_taskdata_t *next_taskdata = KMP_TASK_TO_TASKDATA(next_task);
4326 kmp_taskloop_bounds_t next_task_bounds =
4327 kmp_taskloop_bounds_t(next_task, task_bounds);
4330 next_task_bounds.set_lb(lower);
4331 if (next_taskdata->td_flags.native) {
4332 next_task_bounds.set_ub(upper + (st > 0 ? 1 : -1));
4334 next_task_bounds.set_ub(upper);
4336 if (ptask_dup != NULL)
4338 ptask_dup(next_task, task, lastpriv);
4340 (
"__kmp_taskloop_linear: T#%d; task #%llu: task %p: lower %lld, "
4341 "upper %lld stride %lld, (offsets %p %p)\n",
4342 gtid, i, next_task, lower, upper, st,
4343 next_task_bounds.get_lower_offset(),
4344 next_task_bounds.get_upper_offset()));
4346 __kmp_omp_taskloop_task(NULL, gtid, next_task,
4349 __kmp_omp_task(gtid, next_task,
true);
4354 __kmp_task_start(gtid, task, current_task);
4356 __kmp_task_finish<false>(gtid, task, current_task);
4361 typedef struct __taskloop_params {
4368 kmp_uint64 num_tasks;
4369 kmp_uint64 grainsize;
4371 kmp_int64 last_chunk;
4373 kmp_uint64 num_t_min;
4377 } __taskloop_params_t;
4379 void __kmp_taskloop_recur(
ident_t *,
int, kmp_task_t *, kmp_uint64 *,
4380 kmp_uint64 *, kmp_int64, kmp_uint64, kmp_uint64,
4381 kmp_uint64, kmp_uint64, kmp_int64, kmp_uint64,
4389 int __kmp_taskloop_task(
int gtid,
void *ptask) {
4390 __taskloop_params_t *p =
4391 (__taskloop_params_t *)((kmp_task_t *)ptask)->shareds;
4392 kmp_task_t *task = p->task;
4393 kmp_uint64 *lb = p->lb;
4394 kmp_uint64 *ub = p->ub;
4395 void *task_dup = p->task_dup;
4397 kmp_int64 st = p->st;
4398 kmp_uint64 ub_glob = p->ub_glob;
4399 kmp_uint64 num_tasks = p->num_tasks;
4400 kmp_uint64 grainsize = p->grainsize;
4401 kmp_uint64 extras = p->extras;
4402 kmp_int64 last_chunk = p->last_chunk;
4403 kmp_uint64 tc = p->tc;
4404 kmp_uint64 num_t_min = p->num_t_min;
4406 void *codeptr_ra = p->codeptr_ra;
4409 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4410 KMP_DEBUG_ASSERT(task != NULL);
4412 (
"__kmp_taskloop_task: T#%d, task %p: %lld tasks, grainsize"
4413 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
4414 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
4417 KMP_DEBUG_ASSERT(num_tasks * 2 + 1 > num_t_min);
4418 if (num_tasks > num_t_min)
4419 __kmp_taskloop_recur(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
4420 grainsize, extras, last_chunk, tc, num_t_min,
4426 __kmp_taskloop_linear(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
4427 grainsize, extras, last_chunk, tc,
4433 KA_TRACE(40, (
"__kmp_taskloop_task(exit): T#%d\n", gtid));
4455 void __kmp_taskloop_recur(
ident_t *loc,
int gtid, kmp_task_t *task,
4456 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4457 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
4458 kmp_uint64 grainsize, kmp_uint64 extras,
4459 kmp_int64 last_chunk, kmp_uint64 tc,
4460 kmp_uint64 num_t_min,
4465 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4466 KMP_DEBUG_ASSERT(task != NULL);
4467 KMP_DEBUG_ASSERT(num_tasks > num_t_min);
4469 (
"__kmp_taskloop_recur: T#%d, task %p: %lld tasks, grainsize"
4470 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
4471 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
4473 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
4474 kmp_uint64 lower = *lb;
4475 kmp_info_t *thread = __kmp_threads[gtid];
4477 kmp_task_t *next_task;
4478 size_t lower_offset =
4479 (
char *)lb - (
char *)task;
4480 size_t upper_offset =
4481 (
char *)ub - (
char *)task;
4483 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4484 (last_chunk < 0 ? last_chunk : extras));
4485 KMP_DEBUG_ASSERT(num_tasks > extras);
4486 KMP_DEBUG_ASSERT(num_tasks > 0);
4489 kmp_uint64 lb1, ub0, tc0, tc1, ext0, ext1;
4490 kmp_int64 last_chunk0 = 0, last_chunk1 = 0;
4491 kmp_uint64 gr_size0 = grainsize;
4492 kmp_uint64 n_tsk0 = num_tasks >> 1;
4493 kmp_uint64 n_tsk1 = num_tasks - n_tsk0;
4494 if (last_chunk < 0) {
4496 last_chunk1 = last_chunk;
4497 tc0 = grainsize * n_tsk0;
4499 }
else if (n_tsk0 <= extras) {
4502 ext1 = extras - n_tsk0;
4503 tc0 = gr_size0 * n_tsk0;
4508 tc1 = grainsize * n_tsk1;
4511 ub0 = lower + st * (tc0 - 1);
4515 next_task = __kmp_task_dup_alloc(thread, task);
4517 *(kmp_uint64 *)((
char *)next_task + lower_offset) = lb1;
4518 if (ptask_dup != NULL)
4519 ptask_dup(next_task, task, 0);
4524 kmp_taskdata_t *current_task = thread->th.th_current_task;
4525 thread->th.th_current_task = taskdata->td_parent;
4526 kmp_task_t *new_task =
4527 __kmpc_omp_task_alloc(loc, gtid, 1, 3 *
sizeof(
void *),
4528 sizeof(__taskloop_params_t), &__kmp_taskloop_task);
4530 thread->th.th_current_task = current_task;
4531 __taskloop_params_t *p = (__taskloop_params_t *)new_task->shareds;
4532 p->task = next_task;
4533 p->lb = (kmp_uint64 *)((
char *)next_task + lower_offset);
4534 p->ub = (kmp_uint64 *)((
char *)next_task + upper_offset);
4535 p->task_dup = task_dup;
4537 p->ub_glob = ub_glob;
4538 p->num_tasks = n_tsk1;
4539 p->grainsize = grainsize;
4541 p->last_chunk = last_chunk1;
4543 p->num_t_min = num_t_min;
4545 p->codeptr_ra = codeptr_ra;
4550 __kmp_omp_taskloop_task(NULL, gtid, new_task, codeptr_ra);
4552 __kmp_omp_task(gtid, new_task,
true);
4556 if (n_tsk0 > num_t_min)
4557 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0, gr_size0,
4558 ext0, last_chunk0, tc0, num_t_min,
4564 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0,
4565 gr_size0, ext0, last_chunk0, tc0,
4571 KA_TRACE(40, (
"__kmp_taskloop_recur(exit): T#%d\n", gtid));
4574 static void __kmp_taskloop(
ident_t *loc,
int gtid, kmp_task_t *task,
int if_val,
4575 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4576 int nogroup,
int sched, kmp_uint64 grainsize,
4577 int modifier,
void *task_dup) {
4578 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4579 KMP_DEBUG_ASSERT(task != NULL);
4581 #if OMPT_SUPPORT && OMPT_OPTIONAL
4582 OMPT_STORE_RETURN_ADDRESS(gtid);
4584 __kmpc_taskgroup(loc, gtid);
4589 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4592 kmp_uint64 lower = task_bounds.get_lb();
4593 kmp_uint64 upper = task_bounds.get_ub();
4594 kmp_uint64 ub_glob = upper;
4595 kmp_uint64 num_tasks = 0, extras = 0;
4596 kmp_int64 last_chunk =
4598 kmp_uint64 num_tasks_min = __kmp_taskloop_min_tasks;
4599 kmp_info_t *thread = __kmp_threads[gtid];
4600 kmp_taskdata_t *current_task = thread->th.th_current_task;
4602 KA_TRACE(20, (
"__kmp_taskloop: T#%d, task %p, lb %lld, ub %lld, st %lld, "
4603 "grain %llu(%d, %d), dup %p\n",
4604 gtid, taskdata, lower, upper, st, grainsize, sched, modifier,
4609 tc = upper - lower + 1;
4610 }
else if (st < 0) {
4611 tc = (lower - upper) / (-st) + 1;
4613 tc = (upper - lower) / st + 1;
4616 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d zero-trip loop\n", gtid));
4618 __kmp_task_start(gtid, task, current_task);
4620 __kmp_task_finish<false>(gtid, task, current_task);
4624 #if OMPT_SUPPORT && OMPT_OPTIONAL
4625 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
4626 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
4627 if (ompt_enabled.ompt_callback_work) {
4628 ompt_callbacks.ompt_callback(ompt_callback_work)(
4629 ompt_work_taskloop, ompt_scope_begin, &(team_info->parallel_data),
4630 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
4634 if (num_tasks_min == 0)
4637 KMP_MIN(thread->th.th_team_nproc * 10, INITIAL_TASK_DEQUE_SIZE);
4643 grainsize = thread->th.th_team_nproc * 10;
4646 if (grainsize > tc) {
4651 num_tasks = grainsize;
4652 grainsize = tc / num_tasks;
4653 extras = tc % num_tasks;
4657 if (grainsize > tc) {
4663 num_tasks = (tc + grainsize - 1) / grainsize;
4664 last_chunk = tc - (num_tasks * grainsize);
4667 num_tasks = tc / grainsize;
4669 grainsize = tc / num_tasks;
4670 extras = tc % num_tasks;
4675 KMP_ASSERT2(0,
"unknown scheduling of taskloop");
4678 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4679 (last_chunk < 0 ? last_chunk : extras));
4680 KMP_DEBUG_ASSERT(num_tasks > extras);
4681 KMP_DEBUG_ASSERT(num_tasks > 0);
4687 taskdata->td_flags.task_serial = 1;
4688 taskdata->td_flags.tiedness = TASK_TIED;
4690 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4691 grainsize, extras, last_chunk, tc,
4693 OMPT_GET_RETURN_ADDRESS(0),
4698 }
else if (num_tasks > num_tasks_min && !taskdata->td_flags.native) {
4699 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go recursive: tc %llu, #tasks %llu"
4700 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
4701 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
4703 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4704 grainsize, extras, last_chunk, tc, num_tasks_min,
4706 OMPT_GET_RETURN_ADDRESS(0),
4710 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go linear: tc %llu, #tasks %llu"
4711 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
4712 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
4714 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4715 grainsize, extras, last_chunk, tc,
4717 OMPT_GET_RETURN_ADDRESS(0),
4722 #if OMPT_SUPPORT && OMPT_OPTIONAL
4723 if (ompt_enabled.ompt_callback_work) {
4724 ompt_callbacks.ompt_callback(ompt_callback_work)(
4725 ompt_work_taskloop, ompt_scope_end, &(team_info->parallel_data),
4726 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
4731 #if OMPT_SUPPORT && OMPT_OPTIONAL
4732 OMPT_STORE_RETURN_ADDRESS(gtid);
4734 __kmpc_end_taskgroup(loc, gtid);
4736 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d\n", gtid));
4756 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
int nogroup,
4757 int sched, kmp_uint64 grainsize,
void *task_dup) {
4758 __kmp_assert_valid_gtid(gtid);
4759 KA_TRACE(20, (
"__kmpc_taskloop(enter): T#%d\n", gtid));
4760 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
4762 KA_TRACE(20, (
"__kmpc_taskloop(exit): T#%d\n", gtid));
4783 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4784 int nogroup,
int sched, kmp_uint64 grainsize,
4785 int modifier,
void *task_dup) {
4786 __kmp_assert_valid_gtid(gtid);
4787 KA_TRACE(20, (
"__kmpc_taskloop_5(enter): T#%d\n", gtid));
4788 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
4789 modifier, task_dup);
4790 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