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;
828 kmp_int32 children = 0;
830 KA_TRACE(10, (
"__kmp_task_finish(enter): T#%d finishing task %p and resuming "
832 gtid, taskdata, resumed_task));
834 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
837 #ifdef BUILD_TIED_TASK_STACK
838 if (taskdata->td_flags.tiedness == TASK_TIED) {
839 __kmp_pop_task_stack(gtid, thread, taskdata);
843 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
846 kmp_int32 counter = KMP_ATOMIC_DEC(&taskdata->td_untied_count) - 1;
849 (
"__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n",
850 gtid, counter, taskdata));
854 if (resumed_task == NULL) {
855 KMP_DEBUG_ASSERT(taskdata->td_flags.task_serial);
856 resumed_task = taskdata->td_parent;
859 thread->th.th_current_task = resumed_task;
860 resumed_task->td_flags.executing = 1;
861 KA_TRACE(10, (
"__kmp_task_finish(exit): T#%d partially done task %p, "
862 "resuming task %p\n",
863 gtid, taskdata, resumed_task));
871 (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) ==
872 taskdata->td_flags.task_serial);
873 if (taskdata->td_flags.task_serial) {
874 if (resumed_task == NULL) {
875 resumed_task = taskdata->td_parent;
879 KMP_DEBUG_ASSERT(resumed_task !=
889 if (UNLIKELY(taskdata->td_flags.destructors_thunk)) {
890 kmp_routine_entry_t destr_thunk = task->data1.destructors;
891 KMP_ASSERT(destr_thunk);
892 destr_thunk(gtid, task);
895 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
896 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 1);
897 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
900 if (UNLIKELY(taskdata->td_flags.detachable == TASK_DETACHABLE)) {
901 if (taskdata->td_allow_completion_event.type ==
902 KMP_EVENT_ALLOW_COMPLETION) {
904 __kmp_acquire_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
905 if (taskdata->td_allow_completion_event.type ==
906 KMP_EVENT_ALLOW_COMPLETION) {
908 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
909 taskdata->td_flags.executing = 0;
916 __ompt_task_finish(task, resumed_task, ompt_task_detach);
922 taskdata->td_flags.proxy = TASK_PROXY;
925 __kmp_release_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
930 taskdata->td_flags.complete = 1;
935 __ompt_task_finish(task, resumed_task, ompt_task_complete);
940 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) ||
941 taskdata->td_flags.detachable == TASK_DETACHABLE ||
942 taskdata->td_flags.hidden_helper) {
943 __kmp_release_deps(gtid, taskdata);
946 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks) - 1;
947 KMP_DEBUG_ASSERT(children >= 0);
948 if (taskdata->td_taskgroup)
949 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
950 }
else if (task_team && (task_team->tt.tt_found_proxy_tasks ||
951 task_team->tt.tt_hidden_helper_task_encountered)) {
954 __kmp_release_deps(gtid, taskdata);
960 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
961 taskdata->td_flags.executing = 0;
965 20, (
"__kmp_task_finish: T#%d finished task %p, %d incomplete children\n",
966 gtid, taskdata, children));
972 thread->th.th_current_task = resumed_task;
974 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
978 resumed_task->td_flags.executing = 1;
981 10, (
"__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n",
982 gtid, taskdata, resumed_task));
988 static void __kmpc_omp_task_complete_if0_template(
ident_t *loc_ref,
991 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n",
992 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
993 KMP_DEBUG_ASSERT(gtid >= 0);
995 __kmp_task_finish<ompt>(gtid, task, NULL);
997 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n",
998 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
1002 ompt_frame_t *ompt_frame;
1003 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
1004 ompt_frame->enter_frame = ompt_data_none;
1005 ompt_frame->enter_frame_flags =
1006 ompt_frame_runtime | ompt_frame_framepointer;
1015 void __kmpc_omp_task_complete_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
1017 __kmpc_omp_task_complete_if0_template<true>(loc_ref, gtid, task);
1026 void __kmpc_omp_task_complete_if0(
ident_t *loc_ref, kmp_int32 gtid,
1029 if (UNLIKELY(ompt_enabled.enabled)) {
1030 __kmpc_omp_task_complete_if0_ompt(loc_ref, gtid, task);
1034 __kmpc_omp_task_complete_if0_template<false>(loc_ref, gtid, task);
1040 void __kmpc_omp_task_complete(
ident_t *loc_ref, kmp_int32 gtid,
1042 KA_TRACE(10, (
"__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n", gtid,
1043 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1045 __kmp_task_finish<false>(gtid, task,
1048 KA_TRACE(10, (
"__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n", gtid,
1049 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1065 void __kmp_init_implicit_task(
ident_t *loc_ref, kmp_info_t *this_thr,
1066 kmp_team_t *team,
int tid,
int set_curr_task) {
1067 kmp_taskdata_t *task = &team->t.t_implicit_task_taskdata[tid];
1071 (
"__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n",
1072 tid, team, task, set_curr_task ?
"TRUE" :
"FALSE"));
1074 task->td_task_id = KMP_GEN_TASK_ID();
1075 task->td_team = team;
1078 task->td_ident = loc_ref;
1079 task->td_taskwait_ident = NULL;
1080 task->td_taskwait_counter = 0;
1081 task->td_taskwait_thread = 0;
1083 task->td_flags.tiedness = TASK_TIED;
1084 task->td_flags.tasktype = TASK_IMPLICIT;
1085 task->td_flags.proxy = TASK_FULL;
1088 task->td_flags.task_serial = 1;
1089 task->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1090 task->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1092 task->td_flags.started = 1;
1093 task->td_flags.executing = 1;
1094 task->td_flags.complete = 0;
1095 task->td_flags.freed = 0;
1097 task->td_depnode = NULL;
1098 task->td_last_tied = task;
1099 task->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1101 if (set_curr_task) {
1102 KMP_ATOMIC_ST_REL(&task->td_incomplete_child_tasks, 0);
1104 KMP_ATOMIC_ST_REL(&task->td_allocated_child_tasks, 0);
1105 task->td_taskgroup = NULL;
1106 task->td_dephash = NULL;
1107 __kmp_push_current_task_to_thread(this_thr, team, tid);
1109 KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0);
1110 KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0);
1114 if (UNLIKELY(ompt_enabled.enabled))
1115 __ompt_task_init(task, tid);
1118 KF_TRACE(10, (
"__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n", tid,
1127 void __kmp_finish_implicit_task(kmp_info_t *thread) {
1128 kmp_taskdata_t *task = thread->th.th_current_task;
1129 if (task->td_dephash) {
1131 task->td_flags.complete = 1;
1132 children = KMP_ATOMIC_LD_ACQ(&task->td_incomplete_child_tasks);
1133 kmp_tasking_flags_t flags_old = task->td_flags;
1134 if (children == 0 && flags_old.complete == 1) {
1135 kmp_tasking_flags_t flags_new = flags_old;
1136 flags_new.complete = 0;
1137 if (KMP_COMPARE_AND_STORE_ACQ32(RCAST(kmp_int32 *, &task->td_flags),
1138 *RCAST(kmp_int32 *, &flags_old),
1139 *RCAST(kmp_int32 *, &flags_new))) {
1140 KA_TRACE(100, (
"__kmp_finish_implicit_task: T#%d cleans "
1141 "dephash of implicit task %p\n",
1142 thread->th.th_info.ds.ds_gtid, task));
1143 __kmp_dephash_free_entries(thread, task->td_dephash);
1153 void __kmp_free_implicit_task(kmp_info_t *thread) {
1154 kmp_taskdata_t *task = thread->th.th_current_task;
1155 if (task && task->td_dephash) {
1156 __kmp_dephash_free(thread, task->td_dephash);
1157 task->td_dephash = NULL;
1163 static size_t __kmp_round_up_to_val(
size_t size,
size_t val) {
1164 if (size & (val - 1)) {
1166 if (size <= KMP_SIZE_T_MAX - val) {
1185 kmp_task_t *__kmp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1186 kmp_tasking_flags_t *flags,
1187 size_t sizeof_kmp_task_t,
size_t sizeof_shareds,
1188 kmp_routine_entry_t task_entry) {
1190 kmp_taskdata_t *taskdata;
1191 kmp_info_t *thread = __kmp_threads[gtid];
1192 kmp_info_t *encountering_thread = thread;
1193 kmp_team_t *team = thread->th.th_team;
1194 kmp_taskdata_t *parent_task = thread->th.th_current_task;
1195 size_t shareds_offset;
1197 if (UNLIKELY(!TCR_4(__kmp_init_middle)))
1198 __kmp_middle_initialize();
1200 if (flags->hidden_helper) {
1201 if (__kmp_enable_hidden_helper) {
1202 if (!TCR_4(__kmp_init_hidden_helper))
1203 __kmp_hidden_helper_initialize();
1208 if (!KMP_HIDDEN_HELPER_THREAD(gtid)) {
1209 thread = __kmp_threads[KMP_GTID_TO_SHADOW_GTID(gtid)];
1215 flags->hidden_helper = FALSE;
1219 KA_TRACE(10, (
"__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) "
1220 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1221 gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t,
1222 sizeof_shareds, task_entry));
1224 KMP_DEBUG_ASSERT(parent_task);
1225 if (parent_task->td_flags.final) {
1226 if (flags->merged_if0) {
1231 if (flags->tiedness == TASK_UNTIED && !team->t.t_serialized) {
1236 encountering_thread->th.th_task_team->tt.tt_untied_task_encountered, 1);
1242 if (UNLIKELY(flags->proxy == TASK_PROXY ||
1243 flags->detachable == TASK_DETACHABLE || flags->hidden_helper)) {
1244 if (flags->proxy == TASK_PROXY) {
1245 flags->tiedness = TASK_UNTIED;
1246 flags->merged_if0 = 1;
1250 if ((encountering_thread->th.th_task_team) == NULL) {
1253 KMP_DEBUG_ASSERT(team->t.t_serialized);
1255 (
"T#%d creating task team in __kmp_task_alloc for proxy task\n",
1257 __kmp_task_team_setup(
1258 encountering_thread, team,
1260 encountering_thread->th.th_task_team =
1261 team->t.t_task_team[encountering_thread->th.th_task_state];
1263 kmp_task_team_t *task_team = encountering_thread->th.th_task_team;
1266 if (!KMP_TASKING_ENABLED(task_team)) {
1269 (
"T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid));
1270 __kmp_enable_tasking(task_team, encountering_thread);
1271 kmp_int32 tid = encountering_thread->th.th_info.ds.ds_tid;
1272 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
1274 if (thread_data->td.td_deque == NULL) {
1275 __kmp_alloc_task_deque(encountering_thread, thread_data);
1279 if ((flags->proxy == TASK_PROXY || flags->detachable == TASK_DETACHABLE) &&
1280 task_team->tt.tt_found_proxy_tasks == FALSE)
1281 TCW_4(task_team->tt.tt_found_proxy_tasks, TRUE);
1282 if (flags->hidden_helper &&
1283 task_team->tt.tt_hidden_helper_task_encountered == FALSE)
1284 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, TRUE);
1289 shareds_offset =
sizeof(kmp_taskdata_t) + sizeof_kmp_task_t;
1290 shareds_offset = __kmp_round_up_to_val(shareds_offset,
sizeof(
void *));
1293 KA_TRACE(30, (
"__kmp_task_alloc: T#%d First malloc size: %ld\n", gtid,
1295 KA_TRACE(30, (
"__kmp_task_alloc: T#%d Second malloc size: %ld\n", gtid,
1300 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(
1301 encountering_thread, shareds_offset + sizeof_shareds);
1303 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(
1304 encountering_thread, shareds_offset + sizeof_shareds);
1307 task = KMP_TASKDATA_TO_TASK(taskdata);
1310 #if KMP_ARCH_X86 || KMP_ARCH_PPC64 || !KMP_HAVE_QUAD
1311 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(
double) - 1)) == 0);
1312 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(
double) - 1)) == 0);
1314 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(_Quad) - 1)) == 0);
1315 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(_Quad) - 1)) == 0);
1317 if (sizeof_shareds > 0) {
1319 task->shareds = &((
char *)taskdata)[shareds_offset];
1321 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
1324 task->shareds = NULL;
1326 task->routine = task_entry;
1329 taskdata->td_task_id = KMP_GEN_TASK_ID();
1330 taskdata->td_team = thread->th.th_team;
1331 taskdata->td_alloc_thread = encountering_thread;
1332 taskdata->td_parent = parent_task;
1333 taskdata->td_level = parent_task->td_level + 1;
1334 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
1335 taskdata->td_ident = loc_ref;
1336 taskdata->td_taskwait_ident = NULL;
1337 taskdata->td_taskwait_counter = 0;
1338 taskdata->td_taskwait_thread = 0;
1339 KMP_DEBUG_ASSERT(taskdata->td_parent != NULL);
1341 if (flags->proxy == TASK_FULL)
1342 copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs);
1344 taskdata->td_flags = *flags;
1345 taskdata->encountering_gtid = gtid;
1346 taskdata->td_task_team = thread->th.th_task_team;
1347 taskdata->td_size_alloc = shareds_offset + sizeof_shareds;
1348 taskdata->td_flags.tasktype = TASK_EXPLICIT;
1351 taskdata->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1354 taskdata->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1360 taskdata->td_flags.task_serial =
1361 (parent_task->td_flags.final || taskdata->td_flags.team_serial ||
1362 taskdata->td_flags.tasking_ser || flags->merged_if0);
1364 taskdata->td_flags.started = 0;
1365 taskdata->td_flags.executing = 0;
1366 taskdata->td_flags.complete = 0;
1367 taskdata->td_flags.freed = 0;
1369 KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0);
1371 KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1);
1372 taskdata->td_taskgroup =
1373 parent_task->td_taskgroup;
1374 taskdata->td_dephash = NULL;
1375 taskdata->td_depnode = NULL;
1376 if (flags->tiedness == TASK_UNTIED)
1377 taskdata->td_last_tied = NULL;
1379 taskdata->td_last_tied = taskdata;
1380 taskdata->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1382 if (UNLIKELY(ompt_enabled.enabled))
1383 __ompt_task_init(taskdata, gtid);
1387 if (flags->proxy == TASK_PROXY || flags->detachable == TASK_DETACHABLE ||
1388 flags->hidden_helper ||
1389 !(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
1390 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
1391 if (parent_task->td_taskgroup)
1392 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
1395 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) {
1396 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
1398 if (flags->hidden_helper) {
1399 taskdata->td_flags.task_serial = FALSE;
1401 KMP_ATOMIC_INC(&__kmp_unexecuted_hidden_helper_tasks);
1405 KA_TRACE(20, (
"__kmp_task_alloc(exit): T#%d created task %p parent=%p\n",
1406 gtid, taskdata, taskdata->td_parent));
1411 kmp_task_t *__kmpc_omp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1412 kmp_int32 flags,
size_t sizeof_kmp_task_t,
1413 size_t sizeof_shareds,
1414 kmp_routine_entry_t task_entry) {
1416 kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
1417 __kmp_assert_valid_gtid(gtid);
1418 input_flags->native = FALSE;
1420 KA_TRACE(10, (
"__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s %s) "
1421 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1422 gtid, loc_ref, input_flags->tiedness ?
"tied " :
"untied",
1423 input_flags->proxy ?
"proxy" :
"",
1424 input_flags->detachable ?
"detachable" :
"", sizeof_kmp_task_t,
1425 sizeof_shareds, task_entry));
1427 retval = __kmp_task_alloc(loc_ref, gtid, input_flags, sizeof_kmp_task_t,
1428 sizeof_shareds, task_entry);
1430 KA_TRACE(20, (
"__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval));
1435 kmp_task_t *__kmpc_omp_target_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1437 size_t sizeof_kmp_task_t,
1438 size_t sizeof_shareds,
1439 kmp_routine_entry_t task_entry,
1440 kmp_int64 device_id) {
1441 if (__kmp_enable_hidden_helper) {
1442 auto &input_flags =
reinterpret_cast<kmp_tasking_flags_t &
>(flags);
1443 input_flags.hidden_helper = TRUE;
1444 input_flags.tiedness = TASK_UNTIED;
1447 return __kmpc_omp_task_alloc(loc_ref, gtid, flags, sizeof_kmp_task_t,
1448 sizeof_shareds, task_entry);
1466 kmp_task_t *new_task, kmp_int32 naffins,
1467 kmp_task_affinity_info_t *affin_list) {
1476 static void __kmp_invoke_task(kmp_int32 gtid, kmp_task_t *task,
1477 kmp_taskdata_t *current_task) {
1478 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
1482 30, (
"__kmp_invoke_task(enter): T#%d invoking task %p, current_task=%p\n",
1483 gtid, taskdata, current_task));
1484 KMP_DEBUG_ASSERT(task);
1485 if (UNLIKELY(taskdata->td_flags.proxy == TASK_PROXY &&
1486 taskdata->td_flags.complete == 1)) {
1491 (
"__kmp_invoke_task: T#%d running bottom finish for proxy task %p\n",
1494 __kmp_bottom_half_finish_proxy(gtid, task);
1496 KA_TRACE(30, (
"__kmp_invoke_task(exit): T#%d completed bottom finish for "
1497 "proxy task %p, resuming task %p\n",
1498 gtid, taskdata, current_task));
1506 ompt_thread_info_t oldInfo;
1507 if (UNLIKELY(ompt_enabled.enabled)) {
1509 thread = __kmp_threads[gtid];
1510 oldInfo = thread->th.ompt_thread_info;
1511 thread->th.ompt_thread_info.wait_id = 0;
1512 thread->th.ompt_thread_info.state = (thread->th.th_team_serialized)
1513 ? ompt_state_work_serial
1514 : ompt_state_work_parallel;
1515 taskdata->ompt_task_info.frame.exit_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1520 if (taskdata->td_flags.hidden_helper) {
1522 KMP_ASSERT(KMP_HIDDEN_HELPER_THREAD(gtid));
1523 KMP_ATOMIC_DEC(&__kmp_unexecuted_hidden_helper_tasks);
1527 if (taskdata->td_flags.proxy != TASK_PROXY) {
1528 __kmp_task_start(gtid, task, current_task);
1534 if (UNLIKELY(__kmp_omp_cancellation)) {
1535 thread = __kmp_threads[gtid];
1536 kmp_team_t *this_team = thread->th.th_team;
1537 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
1538 if ((taskgroup && taskgroup->cancel_request) ||
1539 (this_team->t.t_cancel_request == cancel_parallel)) {
1540 #if OMPT_SUPPORT && OMPT_OPTIONAL
1541 ompt_data_t *task_data;
1542 if (UNLIKELY(ompt_enabled.ompt_callback_cancel)) {
1543 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, NULL, NULL);
1544 ompt_callbacks.ompt_callback(ompt_callback_cancel)(
1546 ((taskgroup && taskgroup->cancel_request) ? ompt_cancel_taskgroup
1547 : ompt_cancel_parallel) |
1548 ompt_cancel_discarded_task,
1561 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
1562 taskdata->td_last_tied = current_task->td_last_tied;
1563 KMP_DEBUG_ASSERT(taskdata->td_last_tied);
1565 #if KMP_STATS_ENABLED
1567 switch (KMP_GET_THREAD_STATE()) {
1568 case FORK_JOIN_BARRIER:
1569 KMP_PUSH_PARTITIONED_TIMER(OMP_task_join_bar);
1572 KMP_PUSH_PARTITIONED_TIMER(OMP_task_plain_bar);
1575 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskyield);
1578 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskwait);
1581 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskgroup);
1584 KMP_PUSH_PARTITIONED_TIMER(OMP_task_immediate);
1591 if (UNLIKELY(ompt_enabled.enabled))
1592 __ompt_task_start(task, current_task, gtid);
1596 if (ompd_state & OMPD_ENABLE_BP)
1597 ompd_bp_task_begin();
1600 #if USE_ITT_BUILD && USE_ITT_NOTIFY
1601 kmp_uint64 cur_time;
1602 kmp_int32 kmp_itt_count_task =
1603 __kmp_forkjoin_frames_mode == 3 && !taskdata->td_flags.task_serial &&
1604 current_task->td_flags.tasktype == TASK_IMPLICIT;
1605 if (kmp_itt_count_task) {
1606 thread = __kmp_threads[gtid];
1608 if (thread->th.th_bar_arrive_time)
1609 cur_time = __itt_get_timestamp();
1611 kmp_itt_count_task = 0;
1613 KMP_FSYNC_ACQUIRED(taskdata);
1616 #ifdef KMP_GOMP_COMPAT
1617 if (taskdata->td_flags.native) {
1618 ((void (*)(
void *))(*(task->routine)))(task->shareds);
1622 (*(task->routine))(gtid, task);
1624 KMP_POP_PARTITIONED_TIMER();
1626 #if USE_ITT_BUILD && USE_ITT_NOTIFY
1627 if (kmp_itt_count_task) {
1629 thread->th.th_bar_arrive_time += (__itt_get_timestamp() - cur_time);
1631 KMP_FSYNC_CANCEL(taskdata);
1632 KMP_FSYNC_RELEASING(taskdata->td_parent);
1637 if (ompd_state & OMPD_ENABLE_BP)
1642 if (taskdata->td_flags.proxy != TASK_PROXY) {
1644 if (UNLIKELY(ompt_enabled.enabled)) {
1645 thread->th.ompt_thread_info = oldInfo;
1646 if (taskdata->td_flags.tiedness == TASK_TIED) {
1647 taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
1649 __kmp_task_finish<true>(gtid, task, current_task);
1652 __kmp_task_finish<false>(gtid, task, current_task);
1657 (
"__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n",
1658 gtid, taskdata, current_task));
1672 kmp_int32 __kmpc_omp_task_parts(
ident_t *loc_ref, kmp_int32 gtid,
1673 kmp_task_t *new_task) {
1674 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1676 KA_TRACE(10, (
"__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n", gtid,
1677 loc_ref, new_taskdata));
1680 kmp_taskdata_t *parent;
1681 if (UNLIKELY(ompt_enabled.enabled)) {
1682 parent = new_taskdata->td_parent;
1683 if (ompt_enabled.ompt_callback_task_create) {
1684 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1685 &(parent->ompt_task_info.task_data), &(parent->ompt_task_info.frame),
1686 &(new_taskdata->ompt_task_info.task_data), ompt_task_explicit, 0,
1687 OMPT_GET_RETURN_ADDRESS(0));
1695 if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1697 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1698 new_taskdata->td_flags.task_serial = 1;
1699 __kmp_invoke_task(gtid, new_task, current_task);
1704 (
"__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: "
1705 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
1706 gtid, loc_ref, new_taskdata));
1709 if (UNLIKELY(ompt_enabled.enabled)) {
1710 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1713 return TASK_CURRENT_NOT_QUEUED;
1727 kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task,
1728 bool serialize_immediate) {
1729 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1733 if (new_taskdata->td_flags.proxy == TASK_PROXY ||
1734 __kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1736 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1737 if (serialize_immediate)
1738 new_taskdata->td_flags.task_serial = 1;
1739 __kmp_invoke_task(gtid, new_task, current_task);
1742 return TASK_CURRENT_NOT_QUEUED;
1757 kmp_int32 __kmpc_omp_task(
ident_t *loc_ref, kmp_int32 gtid,
1758 kmp_task_t *new_task) {
1760 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1762 #if KMP_DEBUG || OMPT_SUPPORT
1763 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1765 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1767 __kmp_assert_valid_gtid(gtid);
1770 kmp_taskdata_t *parent = NULL;
1771 if (UNLIKELY(ompt_enabled.enabled)) {
1772 if (!new_taskdata->td_flags.started) {
1773 OMPT_STORE_RETURN_ADDRESS(gtid);
1774 parent = new_taskdata->td_parent;
1775 if (!parent->ompt_task_info.frame.enter_frame.ptr) {
1776 parent->ompt_task_info.frame.enter_frame.ptr =
1777 OMPT_GET_FRAME_ADDRESS(0);
1779 if (ompt_enabled.ompt_callback_task_create) {
1780 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1781 &(parent->ompt_task_info.task_data),
1782 &(parent->ompt_task_info.frame),
1783 &(new_taskdata->ompt_task_info.task_data),
1784 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1785 OMPT_LOAD_RETURN_ADDRESS(gtid));
1790 __ompt_task_finish(new_task,
1791 new_taskdata->ompt_task_info.scheduling_parent,
1793 new_taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
1798 res = __kmp_omp_task(gtid, new_task,
true);
1800 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
1801 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1802 gtid, loc_ref, new_taskdata));
1804 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
1805 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1824 kmp_int32 __kmp_omp_taskloop_task(
ident_t *loc_ref, kmp_int32 gtid,
1825 kmp_task_t *new_task,
void *codeptr_ra) {
1827 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1829 #if KMP_DEBUG || OMPT_SUPPORT
1830 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1832 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1836 kmp_taskdata_t *parent = NULL;
1837 if (UNLIKELY(ompt_enabled.enabled && !new_taskdata->td_flags.started)) {
1838 parent = new_taskdata->td_parent;
1839 if (!parent->ompt_task_info.frame.enter_frame.ptr)
1840 parent->ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1841 if (ompt_enabled.ompt_callback_task_create) {
1842 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1843 &(parent->ompt_task_info.task_data), &(parent->ompt_task_info.frame),
1844 &(new_taskdata->ompt_task_info.task_data),
1845 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1851 res = __kmp_omp_task(gtid, new_task,
true);
1853 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
1854 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1855 gtid, loc_ref, new_taskdata));
1857 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
1858 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1864 template <
bool ompt>
1865 static kmp_int32 __kmpc_omp_taskwait_template(
ident_t *loc_ref, kmp_int32 gtid,
1866 void *frame_address,
1867 void *return_address) {
1868 kmp_taskdata_t *taskdata =
nullptr;
1870 int thread_finished = FALSE;
1871 KMP_SET_THREAD_STATE_BLOCK(TASKWAIT);
1873 KA_TRACE(10, (
"__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref));
1874 KMP_DEBUG_ASSERT(gtid >= 0);
1876 if (__kmp_tasking_mode != tskm_immediate_exec) {
1877 thread = __kmp_threads[gtid];
1878 taskdata = thread->th.th_current_task;
1880 #if OMPT_SUPPORT && OMPT_OPTIONAL
1881 ompt_data_t *my_task_data;
1882 ompt_data_t *my_parallel_data;
1885 my_task_data = &(taskdata->ompt_task_info.task_data);
1886 my_parallel_data = OMPT_CUR_TEAM_DATA(thread);
1888 taskdata->ompt_task_info.frame.enter_frame.ptr = frame_address;
1890 if (ompt_enabled.ompt_callback_sync_region) {
1891 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
1892 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
1893 my_task_data, return_address);
1896 if (ompt_enabled.ompt_callback_sync_region_wait) {
1897 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
1898 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
1899 my_task_data, return_address);
1909 taskdata->td_taskwait_counter += 1;
1910 taskdata->td_taskwait_ident = loc_ref;
1911 taskdata->td_taskwait_thread = gtid + 1;
1914 void *itt_sync_obj = NULL;
1916 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
1921 !taskdata->td_flags.team_serial && !taskdata->td_flags.final;
1923 must_wait = must_wait || (thread->th.th_task_team != NULL &&
1924 thread->th.th_task_team->tt.tt_found_proxy_tasks);
1928 (__kmp_enable_hidden_helper && thread->th.th_task_team != NULL &&
1929 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered);
1932 kmp_flag_32<false, false> flag(
1933 RCAST(std::atomic<kmp_uint32> *,
1934 &(taskdata->td_incomplete_child_tasks)),
1936 while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) != 0) {
1937 flag.execute_tasks(thread, gtid, FALSE,
1938 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
1939 __kmp_task_stealing_constraint);
1943 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
1944 KMP_FSYNC_ACQUIRED(taskdata);
1949 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
1951 #if OMPT_SUPPORT && OMPT_OPTIONAL
1953 if (ompt_enabled.ompt_callback_sync_region_wait) {
1954 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
1955 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
1956 my_task_data, return_address);
1958 if (ompt_enabled.ompt_callback_sync_region) {
1959 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
1960 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
1961 my_task_data, return_address);
1963 taskdata->ompt_task_info.frame.enter_frame = ompt_data_none;
1969 KA_TRACE(10, (
"__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, "
1970 "returning TASK_CURRENT_NOT_QUEUED\n",
1973 return TASK_CURRENT_NOT_QUEUED;
1976 #if OMPT_SUPPORT && OMPT_OPTIONAL
1978 static kmp_int32 __kmpc_omp_taskwait_ompt(
ident_t *loc_ref, kmp_int32 gtid,
1979 void *frame_address,
1980 void *return_address) {
1981 return __kmpc_omp_taskwait_template<true>(loc_ref, gtid, frame_address,
1988 kmp_int32 __kmpc_omp_taskwait(
ident_t *loc_ref, kmp_int32 gtid) {
1989 #if OMPT_SUPPORT && OMPT_OPTIONAL
1990 if (UNLIKELY(ompt_enabled.enabled)) {
1991 OMPT_STORE_RETURN_ADDRESS(gtid);
1992 return __kmpc_omp_taskwait_ompt(loc_ref, gtid, OMPT_GET_FRAME_ADDRESS(0),
1993 OMPT_LOAD_RETURN_ADDRESS(gtid));
1996 return __kmpc_omp_taskwait_template<false>(loc_ref, gtid, NULL, NULL);
2000 kmp_int32 __kmpc_omp_taskyield(
ident_t *loc_ref, kmp_int32 gtid,
int end_part) {
2001 kmp_taskdata_t *taskdata = NULL;
2003 int thread_finished = FALSE;
2006 KMP_SET_THREAD_STATE_BLOCK(TASKYIELD);
2008 KA_TRACE(10, (
"__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n",
2009 gtid, loc_ref, end_part));
2010 __kmp_assert_valid_gtid(gtid);
2012 if (__kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel) {
2013 thread = __kmp_threads[gtid];
2014 taskdata = thread->th.th_current_task;
2021 taskdata->td_taskwait_counter += 1;
2022 taskdata->td_taskwait_ident = loc_ref;
2023 taskdata->td_taskwait_thread = gtid + 1;
2026 void *itt_sync_obj = NULL;
2028 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2031 if (!taskdata->td_flags.team_serial) {
2032 kmp_task_team_t *task_team = thread->th.th_task_team;
2033 if (task_team != NULL) {
2034 if (KMP_TASKING_ENABLED(task_team)) {
2036 if (UNLIKELY(ompt_enabled.enabled))
2037 thread->th.ompt_thread_info.ompt_task_yielded = 1;
2039 __kmp_execute_tasks_32(
2040 thread, gtid, (kmp_flag_32<> *)NULL, FALSE,
2041 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2042 __kmp_task_stealing_constraint);
2044 if (UNLIKELY(ompt_enabled.enabled))
2045 thread->th.ompt_thread_info.ompt_task_yielded = 0;
2051 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2056 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2059 KA_TRACE(10, (
"__kmpc_omp_taskyield(exit): T#%d task %p resuming, "
2060 "returning TASK_CURRENT_NOT_QUEUED\n",
2063 return TASK_CURRENT_NOT_QUEUED;
2084 unsigned reserved31 : 31;
2164 template <
typename T>
2165 void *__kmp_task_reduction_init(
int gtid,
int num, T *data) {
2166 __kmp_assert_valid_gtid(gtid);
2167 kmp_info_t *thread = __kmp_threads[gtid];
2168 kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup;
2169 kmp_uint32 nth = thread->th.th_team_nproc;
2173 KMP_ASSERT(tg != NULL);
2174 KMP_ASSERT(data != NULL);
2175 KMP_ASSERT(num > 0);
2177 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n",
2181 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, taskgroup %p, #items %d\n",
2185 for (
int i = 0; i < num; ++i) {
2186 size_t size = data[i].reduce_size - 1;
2188 size += CACHE_LINE - size % CACHE_LINE;
2189 KMP_ASSERT(data[i].reduce_comb != NULL);
2192 arr[i].
flags = data[i].flags;
2196 __kmp_assign_orig<T>(arr[i], data[i]);
2197 if (!arr[i].flags.lazy_priv) {
2200 arr[i].
reduce_pend = (
char *)(arr[i].reduce_priv) + nth * size;
2201 if (arr[i].reduce_init != NULL) {
2203 for (
size_t j = 0; j < nth; ++j) {
2204 __kmp_call_init<T>(arr[i], j * size);
2211 arr[i].
reduce_priv = __kmp_allocate(nth *
sizeof(
void *));
2214 tg->reduce_data = (
void *)arr;
2215 tg->reduce_num_data = num;
2254 template <
typename T>
2255 void __kmp_task_reduction_init_copy(kmp_info_t *thr,
int num, T *data,
2256 kmp_taskgroup_t *tg,
void *reduce_data) {
2258 KA_TRACE(20, (
"__kmp_task_reduction_init_copy: Th %p, init taskgroup %p,"
2260 thr, tg, reduce_data));
2265 for (
int i = 0; i < num; ++i) {
2268 tg->reduce_data = (
void *)arr;
2269 tg->reduce_num_data = num;
2282 __kmp_assert_valid_gtid(gtid);
2283 kmp_info_t *thread = __kmp_threads[gtid];
2284 kmp_int32 nth = thread->th.th_team_nproc;
2288 kmp_taskgroup_t *tg = (kmp_taskgroup_t *)tskgrp;
2290 tg = thread->th.th_current_task->td_taskgroup;
2291 KMP_ASSERT(tg != NULL);
2293 kmp_int32 num = tg->reduce_num_data;
2294 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
2296 KMP_ASSERT(data != NULL);
2297 while (tg != NULL) {
2298 for (
int i = 0; i < num; ++i) {
2299 if (!arr[i].flags.lazy_priv) {
2300 if (data == arr[i].reduce_shar ||
2301 (data >= arr[i].reduce_priv && data < arr[i].reduce_pend))
2302 return (
char *)(arr[i].
reduce_priv) + tid * arr[i].reduce_size;
2305 void **p_priv = (
void **)(arr[i].reduce_priv);
2306 if (data == arr[i].reduce_shar)
2309 for (
int j = 0; j < nth; ++j)
2310 if (data == p_priv[j])
2314 if (p_priv[tid] == NULL) {
2316 p_priv[tid] = __kmp_allocate(arr[i].reduce_size);
2317 if (arr[i].reduce_init != NULL) {
2318 if (arr[i].reduce_orig != NULL) {
2320 p_priv[tid], arr[i].reduce_orig);
2322 ((void (*)(
void *))arr[i].
reduce_init)(p_priv[tid]);
2331 num = tg->reduce_num_data;
2333 KMP_ASSERT2(0,
"Unknown task reduction item");
2339 static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg) {
2340 kmp_int32 nth = th->th.th_team_nproc;
2341 KMP_DEBUG_ASSERT(nth > 1);
2343 kmp_int32 num = tg->reduce_num_data;
2344 for (
int i = 0; i < num; ++i) {
2346 void (*f_fini)(
void *) = (
void (*)(
void *))(arr[i].
reduce_fini);
2347 void (*f_comb)(
void *,
void *) =
2349 if (!arr[i].flags.lazy_priv) {
2352 for (
int j = 0; j < nth; ++j) {
2353 void *priv_data = (
char *)pr_data + j * size;
2354 f_comb(sh_data, priv_data);
2359 void **pr_data = (
void **)(arr[i].reduce_priv);
2360 for (
int j = 0; j < nth; ++j) {
2361 if (pr_data[j] != NULL) {
2362 f_comb(sh_data, pr_data[j]);
2365 __kmp_free(pr_data[j]);
2369 __kmp_free(arr[i].reduce_priv);
2371 __kmp_thread_free(th, arr);
2372 tg->reduce_data = NULL;
2373 tg->reduce_num_data = 0;
2379 static void __kmp_task_reduction_clean(kmp_info_t *th, kmp_taskgroup_t *tg) {
2380 __kmp_thread_free(th, tg->reduce_data);
2381 tg->reduce_data = NULL;
2382 tg->reduce_num_data = 0;
2385 template <
typename T>
2386 void *__kmp_task_reduction_modifier_init(
ident_t *loc,
int gtid,
int is_ws,
2388 __kmp_assert_valid_gtid(gtid);
2389 kmp_info_t *thr = __kmp_threads[gtid];
2390 kmp_int32 nth = thr->th.th_team_nproc;
2391 __kmpc_taskgroup(loc, gtid);
2394 (
"__kmpc_reduction_modifier_init: T#%d, tg %p, exiting nth=1\n",
2395 gtid, thr->th.th_current_task->td_taskgroup));
2396 return (
void *)thr->th.th_current_task->td_taskgroup;
2398 kmp_team_t *team = thr->th.th_team;
2400 kmp_taskgroup_t *tg;
2401 reduce_data = KMP_ATOMIC_LD_RLX(&team->t.t_tg_reduce_data[is_ws]);
2402 if (reduce_data == NULL &&
2403 __kmp_atomic_compare_store(&team->t.t_tg_reduce_data[is_ws], reduce_data,
2406 KMP_DEBUG_ASSERT(reduce_data == NULL);
2408 tg = (kmp_taskgroup_t *)__kmp_task_reduction_init<T>(gtid, num, data);
2412 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[0]) == 0);
2413 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[1]) == 0);
2414 KMP_ATOMIC_ST_REL(&team->t.t_tg_reduce_data[is_ws], reduce_data);
2417 (reduce_data = KMP_ATOMIC_LD_ACQ(&team->t.t_tg_reduce_data[is_ws])) ==
2421 KMP_DEBUG_ASSERT(reduce_data > (
void *)1);
2422 tg = thr->th.th_current_task->td_taskgroup;
2423 __kmp_task_reduction_init_copy<T>(thr, num, data, tg, reduce_data);
2445 int num,
void *data) {
2446 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2466 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2479 __kmpc_end_taskgroup(loc, gtid);
2483 void __kmpc_taskgroup(
ident_t *loc,
int gtid) {
2484 __kmp_assert_valid_gtid(gtid);
2485 kmp_info_t *thread = __kmp_threads[gtid];
2486 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2487 kmp_taskgroup_t *tg_new =
2488 (kmp_taskgroup_t *)__kmp_thread_malloc(thread,
sizeof(kmp_taskgroup_t));
2489 KA_TRACE(10, (
"__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new));
2490 KMP_ATOMIC_ST_RLX(&tg_new->count, 0);
2491 KMP_ATOMIC_ST_RLX(&tg_new->cancel_request, cancel_noreq);
2492 tg_new->parent = taskdata->td_taskgroup;
2493 tg_new->reduce_data = NULL;
2494 tg_new->reduce_num_data = 0;
2495 tg_new->gomp_data = NULL;
2496 taskdata->td_taskgroup = tg_new;
2498 #if OMPT_SUPPORT && OMPT_OPTIONAL
2499 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2500 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2502 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2503 kmp_team_t *team = thread->th.th_team;
2504 ompt_data_t my_task_data = taskdata->ompt_task_info.task_data;
2506 ompt_data_t my_parallel_data = team->t.ompt_team_info.parallel_data;
2508 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2509 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2510 &(my_task_data), codeptr);
2517 void __kmpc_end_taskgroup(
ident_t *loc,
int gtid) {
2518 __kmp_assert_valid_gtid(gtid);
2519 kmp_info_t *thread = __kmp_threads[gtid];
2520 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2521 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
2522 int thread_finished = FALSE;
2524 #if OMPT_SUPPORT && OMPT_OPTIONAL
2526 ompt_data_t my_task_data;
2527 ompt_data_t my_parallel_data;
2528 void *codeptr =
nullptr;
2529 if (UNLIKELY(ompt_enabled.enabled)) {
2530 team = thread->th.th_team;
2531 my_task_data = taskdata->ompt_task_info.task_data;
2533 my_parallel_data = team->t.ompt_team_info.parallel_data;
2534 codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2536 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2540 KA_TRACE(10, (
"__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc));
2541 KMP_DEBUG_ASSERT(taskgroup != NULL);
2542 KMP_SET_THREAD_STATE_BLOCK(TASKGROUP);
2544 if (__kmp_tasking_mode != tskm_immediate_exec) {
2546 taskdata->td_taskwait_counter += 1;
2547 taskdata->td_taskwait_ident = loc;
2548 taskdata->td_taskwait_thread = gtid + 1;
2552 void *itt_sync_obj = NULL;
2554 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2558 #if OMPT_SUPPORT && OMPT_OPTIONAL
2559 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2560 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2561 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2562 &(my_task_data), codeptr);
2566 if (!taskdata->td_flags.team_serial ||
2567 (thread->th.th_task_team != NULL &&
2568 (thread->th.th_task_team->tt.tt_found_proxy_tasks ||
2569 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered))) {
2570 kmp_flag_32<false, false> flag(
2571 RCAST(std::atomic<kmp_uint32> *, &(taskgroup->count)), 0U);
2572 while (KMP_ATOMIC_LD_ACQ(&taskgroup->count) != 0) {
2573 flag.execute_tasks(thread, gtid, FALSE,
2574 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2575 __kmp_task_stealing_constraint);
2578 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2580 #if OMPT_SUPPORT && OMPT_OPTIONAL
2581 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2582 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2583 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2584 &(my_task_data), codeptr);
2589 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2590 KMP_FSYNC_ACQUIRED(taskdata);
2593 KMP_DEBUG_ASSERT(taskgroup->count == 0);
2595 if (taskgroup->reduce_data != NULL &&
2596 !taskgroup->gomp_data) {
2599 kmp_team_t *t = thread->th.th_team;
2603 if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[0])) != NULL &&
2606 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[0]);
2607 if (cnt == thread->th.th_team_nproc - 1) {
2610 __kmp_task_reduction_fini(thread, taskgroup);
2613 __kmp_thread_free(thread, reduce_data);
2614 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[0], NULL);
2615 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[0], 0);
2619 __kmp_task_reduction_clean(thread, taskgroup);
2621 }
else if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[1])) !=
2625 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[1]);
2626 if (cnt == thread->th.th_team_nproc - 1) {
2628 __kmp_task_reduction_fini(thread, taskgroup);
2631 __kmp_thread_free(thread, reduce_data);
2632 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[1], NULL);
2633 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[1], 0);
2637 __kmp_task_reduction_clean(thread, taskgroup);
2641 __kmp_task_reduction_fini(thread, taskgroup);
2645 taskdata->td_taskgroup = taskgroup->parent;
2646 __kmp_thread_free(thread, taskgroup);
2648 KA_TRACE(10, (
"__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n",
2651 #if OMPT_SUPPORT && OMPT_OPTIONAL
2652 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2653 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2654 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2655 &(my_task_data), codeptr);
2661 static kmp_task_t *__kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid,
2662 kmp_task_team_t *task_team,
2663 kmp_int32 is_constrained) {
2665 kmp_taskdata_t *taskdata;
2666 kmp_thread_data_t *thread_data;
2669 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2670 KMP_DEBUG_ASSERT(task_team->tt.tt_threads_data !=
2673 thread_data = &task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
2675 KA_TRACE(10, (
"__kmp_remove_my_task(enter): T#%d ntasks=%d head=%u tail=%u\n",
2676 gtid, thread_data->td.td_deque_ntasks,
2677 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2679 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2681 (
"__kmp_remove_my_task(exit #1): T#%d No tasks to remove: "
2682 "ntasks=%d head=%u tail=%u\n",
2683 gtid, thread_data->td.td_deque_ntasks,
2684 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2688 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
2690 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2691 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2693 (
"__kmp_remove_my_task(exit #2): T#%d No tasks to remove: "
2694 "ntasks=%d head=%u tail=%u\n",
2695 gtid, thread_data->td.td_deque_ntasks,
2696 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2700 tail = (thread_data->td.td_deque_tail - 1) &
2701 TASK_DEQUE_MASK(thread_data->td);
2702 taskdata = thread_data->td.td_deque[tail];
2704 if (!__kmp_task_is_allowed(gtid, is_constrained, taskdata,
2705 thread->th.th_current_task)) {
2707 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2709 (
"__kmp_remove_my_task(exit #3): T#%d TSC blocks tail task: "
2710 "ntasks=%d head=%u tail=%u\n",
2711 gtid, thread_data->td.td_deque_ntasks,
2712 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2716 thread_data->td.td_deque_tail = tail;
2717 TCW_4(thread_data->td.td_deque_ntasks, thread_data->td.td_deque_ntasks - 1);
2719 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2721 KA_TRACE(10, (
"__kmp_remove_my_task(exit #4): T#%d task %p removed: "
2722 "ntasks=%d head=%u tail=%u\n",
2723 gtid, taskdata, thread_data->td.td_deque_ntasks,
2724 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2726 task = KMP_TASKDATA_TO_TASK(taskdata);
2733 static kmp_task_t *__kmp_steal_task(kmp_info_t *victim_thr, kmp_int32 gtid,
2734 kmp_task_team_t *task_team,
2735 std::atomic<kmp_int32> *unfinished_threads,
2736 int *thread_finished,
2737 kmp_int32 is_constrained) {
2739 kmp_taskdata_t *taskdata;
2740 kmp_taskdata_t *current;
2741 kmp_thread_data_t *victim_td, *threads_data;
2743 kmp_int32 victim_tid;
2745 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2747 threads_data = task_team->tt.tt_threads_data;
2748 KMP_DEBUG_ASSERT(threads_data != NULL);
2750 victim_tid = victim_thr->th.th_info.ds.ds_tid;
2751 victim_td = &threads_data[victim_tid];
2753 KA_TRACE(10, (
"__kmp_steal_task(enter): T#%d try to steal from T#%d: "
2754 "task_team=%p ntasks=%d head=%u tail=%u\n",
2755 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
2756 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
2757 victim_td->td.td_deque_tail));
2759 if (TCR_4(victim_td->td.td_deque_ntasks) == 0) {
2760 KA_TRACE(10, (
"__kmp_steal_task(exit #1): T#%d could not steal from T#%d: "
2761 "task_team=%p ntasks=%d head=%u tail=%u\n",
2762 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
2763 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
2764 victim_td->td.td_deque_tail));
2768 __kmp_acquire_bootstrap_lock(&victim_td->td.td_deque_lock);
2770 int ntasks = TCR_4(victim_td->td.td_deque_ntasks);
2773 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2774 KA_TRACE(10, (
"__kmp_steal_task(exit #2): T#%d could not steal from T#%d: "
2775 "task_team=%p ntasks=%d head=%u tail=%u\n",
2776 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2777 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2781 KMP_DEBUG_ASSERT(victim_td->td.td_deque != NULL);
2782 current = __kmp_threads[gtid]->th.th_current_task;
2783 taskdata = victim_td->td.td_deque[victim_td->td.td_deque_head];
2784 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
2786 victim_td->td.td_deque_head =
2787 (victim_td->td.td_deque_head + 1) & TASK_DEQUE_MASK(victim_td->td);
2789 if (!task_team->tt.tt_untied_task_encountered) {
2791 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2792 KA_TRACE(10, (
"__kmp_steal_task(exit #3): T#%d could not steal from "
2793 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
2794 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2795 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2800 target = victim_td->td.td_deque_head;
2802 for (i = 1; i < ntasks; ++i) {
2803 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
2804 taskdata = victim_td->td.td_deque[target];
2805 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
2811 if (taskdata == NULL) {
2813 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2814 KA_TRACE(10, (
"__kmp_steal_task(exit #4): T#%d could not steal from "
2815 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
2816 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2817 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2821 for (i = i + 1; i < ntasks; ++i) {
2823 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
2824 victim_td->td.td_deque[prev] = victim_td->td.td_deque[target];
2828 victim_td->td.td_deque_tail ==
2829 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(victim_td->td)));
2830 victim_td->td.td_deque_tail = target;
2832 if (*thread_finished) {
2838 count = KMP_ATOMIC_INC(unfinished_threads);
2842 (
"__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n",
2843 gtid, count + 1, task_team));
2845 *thread_finished = FALSE;
2847 TCW_4(victim_td->td.td_deque_ntasks, ntasks - 1);
2849 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2853 (
"__kmp_steal_task(exit #5): T#%d stole task %p from T#%d: "
2854 "task_team=%p ntasks=%d head=%u tail=%u\n",
2855 gtid, taskdata, __kmp_gtid_from_thread(victim_thr), task_team,
2856 ntasks, victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2858 task = KMP_TASKDATA_TO_TASK(taskdata);
2872 static inline int __kmp_execute_tasks_template(
2873 kmp_info_t *thread, kmp_int32 gtid, C *flag,
int final_spin,
2874 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
2875 kmp_int32 is_constrained) {
2876 kmp_task_team_t *task_team = thread->th.th_task_team;
2877 kmp_thread_data_t *threads_data;
2879 kmp_info_t *other_thread;
2880 kmp_taskdata_t *current_task = thread->th.th_current_task;
2881 std::atomic<kmp_int32> *unfinished_threads;
2882 kmp_int32 nthreads, victim_tid = -2, use_own_tasks = 1, new_victim = 0,
2883 tid = thread->th.th_info.ds.ds_tid;
2885 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2886 KMP_DEBUG_ASSERT(thread == __kmp_threads[gtid]);
2888 if (task_team == NULL || current_task == NULL)
2891 KA_TRACE(15, (
"__kmp_execute_tasks_template(enter): T#%d final_spin=%d "
2892 "*thread_finished=%d\n",
2893 gtid, final_spin, *thread_finished));
2895 thread->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
2896 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
2898 KMP_DEBUG_ASSERT(threads_data != NULL);
2900 nthreads = task_team->tt.tt_nproc;
2901 unfinished_threads = &(task_team->tt.tt_unfinished_threads);
2902 KMP_DEBUG_ASSERT(nthreads > 1 || task_team->tt.tt_found_proxy_tasks ||
2903 task_team->tt.tt_hidden_helper_task_encountered);
2904 KMP_DEBUG_ASSERT(*unfinished_threads >= 0);
2910 if (use_own_tasks) {
2911 task = __kmp_remove_my_task(thread, gtid, task_team, is_constrained);
2913 if ((task == NULL) && (nthreads > 1)) {
2917 if (victim_tid == -2) {
2918 victim_tid = threads_data[tid].td.td_deque_last_stolen;
2921 other_thread = threads_data[victim_tid].td.td_thr;
2923 if (victim_tid != -1) {
2925 }
else if (!new_victim) {
2931 victim_tid = __kmp_get_random(thread) % (nthreads - 1);
2932 if (victim_tid >= tid) {
2936 other_thread = threads_data[victim_tid].td.td_thr;
2946 if ((__kmp_tasking_mode == tskm_task_teams) &&
2947 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) &&
2948 (TCR_PTR(CCAST(
void *, other_thread->th.th_sleep_loc)) !=
2951 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(other_thread),
2952 other_thread->th.th_sleep_loc);
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) {
3039 count = KMP_ATOMIC_DEC(unfinished_threads) - 1;
3040 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d dec "
3041 "unfinished_threads to %d task_team=%p\n",
3042 gtid, count, task_team));
3043 *thread_finished = TRUE;
3051 if (flag != NULL && flag->done_check()) {
3054 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3062 if (thread->th.th_task_team == NULL) {
3064 (
"__kmp_execute_tasks_template: T#%d no more tasks\n", gtid));
3070 if (nthreads == 1 &&
3071 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks))
3075 (
"__kmp_execute_tasks_template: T#%d can't find work\n", gtid));
3081 template <
bool C,
bool S>
3082 int __kmp_execute_tasks_32(
3083 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32<C, S> *flag,
int final_spin,
3084 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3085 kmp_int32 is_constrained) {
3086 return __kmp_execute_tasks_template(
3087 thread, gtid, flag, final_spin,
3088 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3091 template <
bool C,
bool S>
3092 int __kmp_execute_tasks_64(
3093 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64<C, S> *flag,
int final_spin,
3094 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3095 kmp_int32 is_constrained) {
3096 return __kmp_execute_tasks_template(
3097 thread, gtid, flag, final_spin,
3098 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3101 int __kmp_execute_tasks_oncore(
3102 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag,
int final_spin,
3103 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3104 kmp_int32 is_constrained) {
3105 return __kmp_execute_tasks_template(
3106 thread, gtid, flag, final_spin,
3107 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3111 __kmp_execute_tasks_32<false, false>(kmp_info_t *, kmp_int32,
3112 kmp_flag_32<false, false> *,
int,
3113 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3115 template int __kmp_execute_tasks_64<false, true>(kmp_info_t *, kmp_int32,
3116 kmp_flag_64<false, true> *,
3118 int *USE_ITT_BUILD_ARG(
void *),
3121 template int __kmp_execute_tasks_64<true, false>(kmp_info_t *, kmp_int32,
3122 kmp_flag_64<true, false> *,
3124 int *USE_ITT_BUILD_ARG(
void *),
3130 static void __kmp_enable_tasking(kmp_task_team_t *task_team,
3131 kmp_info_t *this_thr) {
3132 kmp_thread_data_t *threads_data;
3133 int nthreads, i, is_init_thread;
3135 KA_TRACE(10, (
"__kmp_enable_tasking(enter): T#%d\n",
3136 __kmp_gtid_from_thread(this_thr)));
3138 KMP_DEBUG_ASSERT(task_team != NULL);
3139 KMP_DEBUG_ASSERT(this_thr->th.th_team != NULL);
3141 nthreads = task_team->tt.tt_nproc;
3142 KMP_DEBUG_ASSERT(nthreads > 0);
3143 KMP_DEBUG_ASSERT(nthreads == this_thr->th.th_team->t.t_nproc);
3146 is_init_thread = __kmp_realloc_task_threads_data(this_thr, task_team);
3148 if (!is_init_thread) {
3152 (
"__kmp_enable_tasking(exit): T#%d: threads array already set up.\n",
3153 __kmp_gtid_from_thread(this_thr)));
3156 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
3157 KMP_DEBUG_ASSERT(threads_data != NULL);
3159 if (__kmp_tasking_mode == tskm_task_teams &&
3160 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME)) {
3164 for (i = 0; i < nthreads; i++) {
3165 volatile void *sleep_loc;
3166 kmp_info_t *thread = threads_data[i].td.td_thr;
3168 if (i == this_thr->th.th_info.ds.ds_tid) {
3177 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3179 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d waking up thread T#%d\n",
3180 __kmp_gtid_from_thread(this_thr),
3181 __kmp_gtid_from_thread(thread)));
3182 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc);
3184 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d don't wake up thread T#%d\n",
3185 __kmp_gtid_from_thread(this_thr),
3186 __kmp_gtid_from_thread(thread)));
3191 KA_TRACE(10, (
"__kmp_enable_tasking(exit): T#%d\n",
3192 __kmp_gtid_from_thread(this_thr)));
3225 static kmp_task_team_t *__kmp_free_task_teams =
3228 kmp_bootstrap_lock_t __kmp_task_team_lock =
3229 KMP_BOOTSTRAP_LOCK_INITIALIZER(__kmp_task_team_lock);
3236 static void __kmp_alloc_task_deque(kmp_info_t *thread,
3237 kmp_thread_data_t *thread_data) {
3238 __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
3239 KMP_DEBUG_ASSERT(thread_data->td.td_deque == NULL);
3242 thread_data->td.td_deque_last_stolen = -1;
3244 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == 0);
3245 KMP_DEBUG_ASSERT(thread_data->td.td_deque_head == 0);
3246 KMP_DEBUG_ASSERT(thread_data->td.td_deque_tail == 0);
3250 (
"__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n",
3251 __kmp_gtid_from_thread(thread), INITIAL_TASK_DEQUE_SIZE, thread_data));
3255 thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
3256 INITIAL_TASK_DEQUE_SIZE *
sizeof(kmp_taskdata_t *));
3257 thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
3263 static void __kmp_free_task_deque(kmp_thread_data_t *thread_data) {
3264 if (thread_data->td.td_deque != NULL) {
3265 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3266 TCW_4(thread_data->td.td_deque_ntasks, 0);
3267 __kmp_free(thread_data->td.td_deque);
3268 thread_data->td.td_deque = NULL;
3269 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3272 #ifdef BUILD_TIED_TASK_STACK
3274 if (thread_data->td.td_susp_tied_tasks.ts_entries != TASK_STACK_EMPTY) {
3275 __kmp_free_task_stack(__kmp_thread_from_gtid(gtid), thread_data);
3287 static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
3288 kmp_task_team_t *task_team) {
3289 kmp_thread_data_t **threads_data_p;
3290 kmp_int32 nthreads, maxthreads;
3291 int is_init_thread = FALSE;
3293 if (TCR_4(task_team->tt.tt_found_tasks)) {
3298 threads_data_p = &task_team->tt.tt_threads_data;
3299 nthreads = task_team->tt.tt_nproc;
3300 maxthreads = task_team->tt.tt_max_threads;
3305 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3307 if (!TCR_4(task_team->tt.tt_found_tasks)) {
3309 kmp_team_t *team = thread->th.th_team;
3312 is_init_thread = TRUE;
3313 if (maxthreads < nthreads) {
3315 if (*threads_data_p != NULL) {
3316 kmp_thread_data_t *old_data = *threads_data_p;
3317 kmp_thread_data_t *new_data = NULL;
3321 (
"__kmp_realloc_task_threads_data: T#%d reallocating "
3322 "threads data for task_team %p, new_size = %d, old_size = %d\n",
3323 __kmp_gtid_from_thread(thread), task_team, nthreads, maxthreads));
3328 new_data = (kmp_thread_data_t *)__kmp_allocate(
3329 nthreads *
sizeof(kmp_thread_data_t));
3331 KMP_MEMCPY_S((
void *)new_data, nthreads *
sizeof(kmp_thread_data_t),
3332 (
void *)old_data, maxthreads *
sizeof(kmp_thread_data_t));
3334 #ifdef BUILD_TIED_TASK_STACK
3336 for (i = maxthreads; i < nthreads; i++) {
3337 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3338 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3342 (*threads_data_p) = new_data;
3343 __kmp_free(old_data);
3345 KE_TRACE(10, (
"__kmp_realloc_task_threads_data: T#%d allocating "
3346 "threads data for task_team %p, size = %d\n",
3347 __kmp_gtid_from_thread(thread), task_team, nthreads));
3351 *threads_data_p = (kmp_thread_data_t *)__kmp_allocate(
3352 nthreads *
sizeof(kmp_thread_data_t));
3353 #ifdef BUILD_TIED_TASK_STACK
3355 for (i = 0; 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 task_team->tt.tt_max_threads = nthreads;
3364 KMP_DEBUG_ASSERT(*threads_data_p != NULL);
3368 for (i = 0; i < nthreads; i++) {
3369 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3370 thread_data->td.td_thr = team->t.t_threads[i];
3372 if (thread_data->td.td_deque_last_stolen >= nthreads) {
3376 thread_data->td.td_deque_last_stolen = -1;
3381 TCW_SYNC_4(task_team->tt.tt_found_tasks, TRUE);
3384 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3385 return is_init_thread;
3391 static void __kmp_free_task_threads_data(kmp_task_team_t *task_team) {
3392 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3393 if (task_team->tt.tt_threads_data != NULL) {
3395 for (i = 0; i < task_team->tt.tt_max_threads; i++) {
3396 __kmp_free_task_deque(&task_team->tt.tt_threads_data[i]);
3398 __kmp_free(task_team->tt.tt_threads_data);
3399 task_team->tt.tt_threads_data = NULL;
3401 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3408 static kmp_task_team_t *__kmp_allocate_task_team(kmp_info_t *thread,
3410 kmp_task_team_t *task_team = NULL;
3413 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d entering; team = %p\n",
3414 (thread ? __kmp_gtid_from_thread(thread) : -1), team));
3416 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3418 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3419 if (__kmp_free_task_teams != NULL) {
3420 task_team = __kmp_free_task_teams;
3421 TCW_PTR(__kmp_free_task_teams, task_team->tt.tt_next);
3422 task_team->tt.tt_next = NULL;
3424 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3427 if (task_team == NULL) {
3428 KE_TRACE(10, (
"__kmp_allocate_task_team: T#%d allocating "
3429 "task team for team %p\n",
3430 __kmp_gtid_from_thread(thread), team));
3433 task_team = (kmp_task_team_t *)__kmp_allocate(
sizeof(kmp_task_team_t));
3434 __kmp_init_bootstrap_lock(&task_team->tt.tt_threads_lock);
3435 #if USE_ITT_BUILD && USE_ITT_NOTIFY && KMP_DEBUG
3438 __itt_suppress_mark_range(
3439 __itt_suppress_range, __itt_suppress_threading_errors,
3440 &task_team->tt.tt_found_tasks,
sizeof(task_team->tt.tt_found_tasks));
3441 __itt_suppress_mark_range(__itt_suppress_range,
3442 __itt_suppress_threading_errors,
3443 CCAST(kmp_uint32 *, &task_team->tt.tt_active),
3444 sizeof(task_team->tt.tt_active));
3452 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3453 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3454 task_team->tt.tt_nproc = nthreads = team->t.t_nproc;
3456 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads, nthreads);
3457 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
3458 TCW_4(task_team->tt.tt_active, TRUE);
3460 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d exiting; task_team = %p "
3461 "unfinished_threads init'd to %d\n",
3462 (thread ? __kmp_gtid_from_thread(thread) : -1), task_team,
3463 KMP_ATOMIC_LD_RLX(&task_team->tt.tt_unfinished_threads)));
3470 void __kmp_free_task_team(kmp_info_t *thread, kmp_task_team_t *task_team) {
3471 KA_TRACE(20, (
"__kmp_free_task_team: T#%d task_team = %p\n",
3472 thread ? __kmp_gtid_from_thread(thread) : -1, task_team));
3475 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3477 KMP_DEBUG_ASSERT(task_team->tt.tt_next == NULL);
3478 task_team->tt.tt_next = __kmp_free_task_teams;
3479 TCW_PTR(__kmp_free_task_teams, task_team);
3481 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3489 void __kmp_reap_task_teams(
void) {
3490 kmp_task_team_t *task_team;
3492 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3494 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3495 while ((task_team = __kmp_free_task_teams) != NULL) {
3496 __kmp_free_task_teams = task_team->tt.tt_next;
3497 task_team->tt.tt_next = NULL;
3500 if (task_team->tt.tt_threads_data != NULL) {
3501 __kmp_free_task_threads_data(task_team);
3503 __kmp_free(task_team);
3505 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3512 void __kmp_wait_to_unref_task_teams(
void) {
3517 KMP_INIT_YIELD(spins);
3525 for (thread = CCAST(kmp_info_t *, __kmp_thread_pool); thread != NULL;
3526 thread = thread->th.th_next_pool) {
3530 if (TCR_PTR(thread->th.th_task_team) == NULL) {
3531 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n",
3532 __kmp_gtid_from_thread(thread)));
3537 if (!__kmp_is_thread_alive(thread, &exit_val)) {
3538 thread->th.th_task_team = NULL;
3545 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: Waiting for T#%d to "
3546 "unreference task_team\n",
3547 __kmp_gtid_from_thread(thread)));
3549 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
3550 volatile void *sleep_loc;
3552 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3556 (
"__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n",
3557 __kmp_gtid_from_thread(thread), __kmp_gtid_from_thread(thread)));
3558 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc);
3567 KMP_YIELD_OVERSUB_ELSE_SPIN(spins);
3573 void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team,
int always) {
3574 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3580 if (team->t.t_task_team[this_thr->th.th_task_state] == NULL &&
3581 (always || team->t.t_nproc > 1)) {
3582 team->t.t_task_team[this_thr->th.th_task_state] =
3583 __kmp_allocate_task_team(this_thr, team);
3584 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d created new task_team %p"
3585 " for team %d at parity=%d\n",
3586 __kmp_gtid_from_thread(this_thr),
3587 team->t.t_task_team[this_thr->th.th_task_state], team->t.t_id,
3588 this_thr->th.th_task_state));
3598 if (team->t.t_nproc > 1) {
3599 int other_team = 1 - this_thr->th.th_task_state;
3600 KMP_DEBUG_ASSERT(other_team >= 0 && other_team < 2);
3601 if (team->t.t_task_team[other_team] == NULL) {
3602 team->t.t_task_team[other_team] =
3603 __kmp_allocate_task_team(this_thr, team);
3604 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d created second new "
3605 "task_team %p for team %d at parity=%d\n",
3606 __kmp_gtid_from_thread(this_thr),
3607 team->t.t_task_team[other_team], team->t.t_id, other_team));
3610 kmp_task_team_t *task_team = team->t.t_task_team[other_team];
3611 if (!task_team->tt.tt_active ||
3612 team->t.t_nproc != task_team->tt.tt_nproc) {
3613 TCW_4(task_team->tt.tt_nproc, team->t.t_nproc);
3614 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3615 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3616 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads,
3618 TCW_4(task_team->tt.tt_active, TRUE);
3622 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d reset next task_team "
3623 "%p for team %d at parity=%d\n",
3624 __kmp_gtid_from_thread(this_thr),
3625 team->t.t_task_team[other_team], team->t.t_id, other_team));
3633 if (this_thr == __kmp_hidden_helper_main_thread) {
3634 for (
int i = 0; i < 2; ++i) {
3635 kmp_task_team_t *task_team = team->t.t_task_team[i];
3636 if (KMP_TASKING_ENABLED(task_team)) {
3639 __kmp_enable_tasking(task_team, this_thr);
3640 for (
int j = 0; j < task_team->tt.tt_nproc; ++j) {
3641 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[j];
3642 if (thread_data->td.td_deque == NULL) {
3643 __kmp_alloc_task_deque(__kmp_hidden_helper_threads[j], thread_data);
3653 void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team) {
3654 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3658 this_thr->th.th_task_state = (kmp_uint8)(1 - this_thr->th.th_task_state);
3662 TCW_PTR(this_thr->th.th_task_team,
3663 team->t.t_task_team[this_thr->th.th_task_state]);
3665 (
"__kmp_task_team_sync: Thread T#%d task team switched to task_team "
3666 "%p from Team #%d (parity=%d)\n",
3667 __kmp_gtid_from_thread(this_thr), this_thr->th.th_task_team,
3668 team->t.t_id, this_thr->th.th_task_state));
3678 void __kmp_task_team_wait(
3679 kmp_info_t *this_thr,
3680 kmp_team_t *team USE_ITT_BUILD_ARG(
void *itt_sync_obj),
int wait) {
3681 kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state];
3683 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3684 KMP_DEBUG_ASSERT(task_team == this_thr->th.th_task_team);
3686 if ((task_team != NULL) && KMP_TASKING_ENABLED(task_team)) {
3688 KA_TRACE(20, (
"__kmp_task_team_wait: Primary T#%d waiting for all tasks "
3689 "(for unfinished_threads to reach 0) on task_team = %p\n",
3690 __kmp_gtid_from_thread(this_thr), task_team));
3694 kmp_flag_32<false, false> flag(
3695 RCAST(std::atomic<kmp_uint32> *,
3696 &task_team->tt.tt_unfinished_threads),
3698 flag.wait(this_thr, TRUE USE_ITT_BUILD_ARG(itt_sync_obj));
3704 (
"__kmp_task_team_wait: Primary T#%d deactivating task_team %p: "
3705 "setting active to false, setting local and team's pointer to NULL\n",
3706 __kmp_gtid_from_thread(this_thr), task_team));
3707 KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1 ||
3708 task_team->tt.tt_found_proxy_tasks == TRUE);
3709 TCW_SYNC_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3710 KMP_CHECK_UPDATE(task_team->tt.tt_untied_task_encountered, 0);
3711 TCW_SYNC_4(task_team->tt.tt_active, FALSE);
3714 TCW_PTR(this_thr->th.th_task_team, NULL);
3723 void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread,
int gtid) {
3724 std::atomic<kmp_uint32> *spin = RCAST(
3725 std::atomic<kmp_uint32> *,
3726 &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads);
3728 KMP_DEBUG_ASSERT(__kmp_tasking_mode == tskm_extra_barrier);
3731 KMP_FSYNC_SPIN_INIT(spin, NULL);
3733 kmp_flag_32<false, false> spin_flag(spin, 0U);
3734 while (!spin_flag.execute_tasks(thread, gtid, TRUE,
3735 &flag USE_ITT_BUILD_ARG(NULL), 0)) {
3738 KMP_FSYNC_SPIN_PREPARE(RCAST(
void *, spin));
3741 if (TCR_4(__kmp_global.g.g_done)) {
3742 if (__kmp_global.g.g_abort)
3743 __kmp_abort_thread();
3749 KMP_FSYNC_SPIN_ACQUIRED(RCAST(
void *, spin));
3758 static bool __kmp_give_task(kmp_info_t *thread, kmp_int32 tid, kmp_task_t *task,
3760 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
3761 kmp_task_team_t *task_team = taskdata->td_task_team;
3763 KA_TRACE(20, (
"__kmp_give_task: trying to give task %p to thread %d.\n",
3767 KMP_DEBUG_ASSERT(task_team != NULL);
3769 bool result =
false;
3770 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
3772 if (thread_data->td.td_deque == NULL) {
3776 (
"__kmp_give_task: thread %d has no queue while giving task %p.\n",
3781 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3782 TASK_DEQUE_SIZE(thread_data->td)) {
3785 (
"__kmp_give_task: queue is full while giving task %p to thread %d.\n",
3790 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
3793 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3794 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3795 TASK_DEQUE_SIZE(thread_data->td)) {
3797 __kmp_realloc_task_deque(thread, thread_data);
3802 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3804 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3805 TASK_DEQUE_SIZE(thread_data->td)) {
3806 KA_TRACE(30, (
"__kmp_give_task: queue is full while giving task %p to "
3812 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
3813 goto release_and_exit;
3815 __kmp_realloc_task_deque(thread, thread_data);
3821 thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
3823 thread_data->td.td_deque_tail =
3824 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
3825 TCW_4(thread_data->td.td_deque_ntasks,
3826 TCR_4(thread_data->td.td_deque_ntasks) + 1);
3829 KA_TRACE(30, (
"__kmp_give_task: successfully gave task %p to thread %d.\n",
3833 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3838 #define PROXY_TASK_FLAG 0x40000000
3855 static void __kmp_first_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
3856 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
3857 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3858 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
3859 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
3861 taskdata->td_flags.complete = 1;
3863 if (taskdata->td_taskgroup)
3864 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
3868 KMP_ATOMIC_OR(&taskdata->td_incomplete_child_tasks, PROXY_TASK_FLAG);
3871 static void __kmp_second_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
3872 kmp_int32 children = 0;
3876 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks) - 1;
3877 KMP_DEBUG_ASSERT(children >= 0);
3880 KMP_ATOMIC_AND(&taskdata->td_incomplete_child_tasks, ~PROXY_TASK_FLAG);
3883 static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask) {
3884 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3885 kmp_info_t *thread = __kmp_threads[gtid];
3887 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3888 KMP_DEBUG_ASSERT(taskdata->td_flags.complete ==
3893 while ((KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) &
3894 PROXY_TASK_FLAG) > 0)
3897 __kmp_release_deps(gtid, taskdata);
3898 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
3910 KMP_DEBUG_ASSERT(ptask != NULL);
3911 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3913 10, (
"__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n",
3915 __kmp_assert_valid_gtid(gtid);
3916 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3918 __kmp_first_top_half_finish_proxy(taskdata);
3919 __kmp_second_top_half_finish_proxy(taskdata);
3920 __kmp_bottom_half_finish_proxy(gtid, ptask);
3923 (
"__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n",
3927 void __kmpc_give_task(kmp_task_t *ptask, kmp_int32 start = 0) {
3928 KMP_DEBUG_ASSERT(ptask != NULL);
3929 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3933 kmp_team_t *team = taskdata->td_team;
3934 kmp_int32 nthreads = team->t.t_nproc;
3939 kmp_int32 start_k = start;
3941 kmp_int32 k = start_k;
3945 thread = team->t.t_threads[k];
3946 k = (k + 1) % nthreads;
3952 }
while (!__kmp_give_task(thread, k, ptask, pass));
3963 KMP_DEBUG_ASSERT(ptask != NULL);
3964 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3968 (
"__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n",
3971 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3973 __kmp_first_top_half_finish_proxy(taskdata);
3975 __kmpc_give_task(ptask);
3977 __kmp_second_top_half_finish_proxy(taskdata);
3981 (
"__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n",
3985 kmp_event_t *__kmpc_task_allow_completion_event(
ident_t *loc_ref,
int gtid,
3987 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(task);
3988 if (td->td_allow_completion_event.type == KMP_EVENT_UNINITIALIZED) {
3989 td->td_allow_completion_event.type = KMP_EVENT_ALLOW_COMPLETION;
3990 td->td_allow_completion_event.ed.task = task;
3991 __kmp_init_tas_lock(&td->td_allow_completion_event.lock);
3993 return &td->td_allow_completion_event;
3996 void __kmp_fulfill_event(kmp_event_t *event) {
3997 if (event->type == KMP_EVENT_ALLOW_COMPLETION) {
3998 kmp_task_t *ptask = event->ed.task;
3999 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4000 bool detached =
false;
4001 int gtid = __kmp_get_gtid();
4006 __kmp_acquire_tas_lock(&event->lock, gtid);
4007 if (taskdata->td_flags.proxy == TASK_PROXY) {
4013 if (UNLIKELY(ompt_enabled.enabled))
4014 __ompt_task_finish(ptask, NULL, ompt_task_early_fulfill);
4017 event->type = KMP_EVENT_UNINITIALIZED;
4018 __kmp_release_tas_lock(&event->lock, gtid);
4024 if (UNLIKELY(ompt_enabled.enabled))
4025 __ompt_task_finish(ptask, NULL, ompt_task_late_fulfill);
4029 kmp_team_t *team = taskdata->td_team;
4030 kmp_info_t *thread = __kmp_get_thread();
4031 if (thread->th.th_team == team) {
4049 kmp_task_t *__kmp_task_dup_alloc(kmp_info_t *thread, kmp_task_t *task_src) {
4051 kmp_taskdata_t *taskdata;
4052 kmp_taskdata_t *taskdata_src = KMP_TASK_TO_TASKDATA(task_src);
4053 kmp_taskdata_t *parent_task = taskdata_src->td_parent;
4054 size_t shareds_offset;
4057 KA_TRACE(10, (
"__kmp_task_dup_alloc(enter): Th %p, source task %p\n", thread,
4059 KMP_DEBUG_ASSERT(taskdata_src->td_flags.proxy ==
4061 KMP_DEBUG_ASSERT(taskdata_src->td_flags.tasktype == TASK_EXPLICIT);
4062 task_size = taskdata_src->td_size_alloc;
4065 KA_TRACE(30, (
"__kmp_task_dup_alloc: Th %p, malloc size %ld\n", thread,
4068 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, task_size);
4070 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, task_size);
4072 KMP_MEMCPY(taskdata, taskdata_src, task_size);
4074 task = KMP_TASKDATA_TO_TASK(taskdata);
4077 taskdata->td_task_id = KMP_GEN_TASK_ID();
4078 if (task->shareds != NULL) {
4079 shareds_offset = (
char *)task_src->shareds - (
char *)taskdata_src;
4080 task->shareds = &((
char *)taskdata)[shareds_offset];
4081 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
4084 taskdata->td_alloc_thread = thread;
4085 taskdata->td_parent = parent_task;
4087 taskdata->td_taskgroup = parent_task->td_taskgroup;
4090 if (taskdata->td_flags.tiedness == TASK_TIED)
4091 taskdata->td_last_tied = taskdata;
4095 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
4096 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
4097 if (parent_task->td_taskgroup)
4098 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
4101 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT)
4102 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
4106 (
"__kmp_task_dup_alloc(exit): Th %p, created task %p, parent=%p\n",
4107 thread, taskdata, taskdata->td_parent));
4109 if (UNLIKELY(ompt_enabled.enabled))
4110 __ompt_task_init(taskdata, thread->th.th_info.ds.ds_gtid);
4119 typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32);
4121 KMP_BUILD_ASSERT(
sizeof(
long) == 4 ||
sizeof(
long) == 8);
4126 class kmp_taskloop_bounds_t {
4128 const kmp_taskdata_t *taskdata;
4129 size_t lower_offset;
4130 size_t upper_offset;
4133 kmp_taskloop_bounds_t(kmp_task_t *_task, kmp_uint64 *lb, kmp_uint64 *ub)
4134 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(task)),
4135 lower_offset((char *)lb - (char *)task),
4136 upper_offset((char *)ub - (char *)task) {
4137 KMP_DEBUG_ASSERT((
char *)lb > (
char *)_task);
4138 KMP_DEBUG_ASSERT((
char *)ub > (
char *)_task);
4140 kmp_taskloop_bounds_t(kmp_task_t *_task,
const kmp_taskloop_bounds_t &bounds)
4141 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(_task)),
4142 lower_offset(bounds.lower_offset), upper_offset(bounds.upper_offset) {}
4143 size_t get_lower_offset()
const {
return lower_offset; }
4144 size_t get_upper_offset()
const {
return upper_offset; }
4145 kmp_uint64 get_lb()
const {
4147 #if defined(KMP_GOMP_COMPAT)
4149 if (!taskdata->td_flags.native) {
4150 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4153 if (taskdata->td_size_loop_bounds == 4) {
4154 kmp_int32 *lb = RCAST(kmp_int32 *, task->shareds);
4155 retval = (kmp_int64)*lb;
4157 kmp_int64 *lb = RCAST(kmp_int64 *, task->shareds);
4158 retval = (kmp_int64)*lb;
4163 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4167 kmp_uint64 get_ub()
const {
4169 #if defined(KMP_GOMP_COMPAT)
4171 if (!taskdata->td_flags.native) {
4172 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4175 if (taskdata->td_size_loop_bounds == 4) {
4176 kmp_int32 *ub = RCAST(kmp_int32 *, task->shareds) + 1;
4177 retval = (kmp_int64)*ub;
4179 kmp_int64 *ub = RCAST(kmp_int64 *, task->shareds) + 1;
4180 retval = (kmp_int64)*ub;
4184 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4188 void set_lb(kmp_uint64 lb) {
4189 #if defined(KMP_GOMP_COMPAT)
4191 if (!taskdata->td_flags.native) {
4192 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4195 if (taskdata->td_size_loop_bounds == 4) {
4196 kmp_uint32 *lower = RCAST(kmp_uint32 *, task->shareds);
4197 *lower = (kmp_uint32)lb;
4199 kmp_uint64 *lower = RCAST(kmp_uint64 *, task->shareds);
4200 *lower = (kmp_uint64)lb;
4204 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4207 void set_ub(kmp_uint64 ub) {
4208 #if defined(KMP_GOMP_COMPAT)
4210 if (!taskdata->td_flags.native) {
4211 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4214 if (taskdata->td_size_loop_bounds == 4) {
4215 kmp_uint32 *upper = RCAST(kmp_uint32 *, task->shareds) + 1;
4216 *upper = (kmp_uint32)ub;
4218 kmp_uint64 *upper = RCAST(kmp_uint64 *, task->shareds) + 1;
4219 *upper = (kmp_uint64)ub;
4223 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4244 void __kmp_taskloop_linear(
ident_t *loc,
int gtid, kmp_task_t *task,
4245 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4246 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
4247 kmp_uint64 grainsize, kmp_uint64 extras,
4248 kmp_int64 last_chunk, kmp_uint64 tc,
4254 KMP_TIME_PARTITIONED_BLOCK(OMP_taskloop_scheduling);
4255 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
4257 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4258 kmp_uint64 lower = task_bounds.get_lb();
4259 kmp_uint64 upper = task_bounds.get_ub();
4261 kmp_info_t *thread = __kmp_threads[gtid];
4262 kmp_taskdata_t *current_task = thread->th.th_current_task;
4263 kmp_task_t *next_task;
4264 kmp_int32 lastpriv = 0;
4266 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4267 (last_chunk < 0 ? last_chunk : extras));
4268 KMP_DEBUG_ASSERT(num_tasks > extras);
4269 KMP_DEBUG_ASSERT(num_tasks > 0);
4270 KA_TRACE(20, (
"__kmp_taskloop_linear: T#%d: %lld tasks, grainsize %lld, "
4271 "extras %lld, last_chunk %lld, i=%lld,%lld(%d)%lld, dup %p\n",
4272 gtid, num_tasks, grainsize, extras, last_chunk, lower, upper,
4273 ub_glob, st, task_dup));
4276 for (i = 0; i < num_tasks; ++i) {
4277 kmp_uint64 chunk_minus_1;
4279 chunk_minus_1 = grainsize - 1;
4281 chunk_minus_1 = grainsize;
4284 upper = lower + st * chunk_minus_1;
4288 if (i == num_tasks - 1) {
4291 KMP_DEBUG_ASSERT(upper == *ub);
4292 if (upper == ub_glob)
4294 }
else if (st > 0) {
4295 KMP_DEBUG_ASSERT((kmp_uint64)st > *ub - upper);
4296 if ((kmp_uint64)st > ub_glob - upper)
4299 KMP_DEBUG_ASSERT(upper + st < *ub);
4300 if (upper - ub_glob < (kmp_uint64)(-st))
4304 next_task = __kmp_task_dup_alloc(thread, task);
4305 kmp_taskdata_t *next_taskdata = KMP_TASK_TO_TASKDATA(next_task);
4306 kmp_taskloop_bounds_t next_task_bounds =
4307 kmp_taskloop_bounds_t(next_task, task_bounds);
4310 next_task_bounds.set_lb(lower);
4311 if (next_taskdata->td_flags.native) {
4312 next_task_bounds.set_ub(upper + (st > 0 ? 1 : -1));
4314 next_task_bounds.set_ub(upper);
4316 if (ptask_dup != NULL)
4318 ptask_dup(next_task, task, lastpriv);
4320 (
"__kmp_taskloop_linear: T#%d; task #%llu: task %p: lower %lld, "
4321 "upper %lld stride %lld, (offsets %p %p)\n",
4322 gtid, i, next_task, lower, upper, st,
4323 next_task_bounds.get_lower_offset(),
4324 next_task_bounds.get_upper_offset()));
4326 __kmp_omp_taskloop_task(NULL, gtid, next_task,
4329 __kmp_omp_task(gtid, next_task,
true);
4334 __kmp_task_start(gtid, task, current_task);
4336 __kmp_task_finish<false>(gtid, task, current_task);
4341 typedef struct __taskloop_params {
4348 kmp_uint64 num_tasks;
4349 kmp_uint64 grainsize;
4351 kmp_int64 last_chunk;
4353 kmp_uint64 num_t_min;
4357 } __taskloop_params_t;
4359 void __kmp_taskloop_recur(
ident_t *,
int, kmp_task_t *, kmp_uint64 *,
4360 kmp_uint64 *, kmp_int64, kmp_uint64, kmp_uint64,
4361 kmp_uint64, kmp_uint64, kmp_int64, kmp_uint64,
4369 int __kmp_taskloop_task(
int gtid,
void *ptask) {
4370 __taskloop_params_t *p =
4371 (__taskloop_params_t *)((kmp_task_t *)ptask)->shareds;
4372 kmp_task_t *task = p->task;
4373 kmp_uint64 *lb = p->lb;
4374 kmp_uint64 *ub = p->ub;
4375 void *task_dup = p->task_dup;
4377 kmp_int64 st = p->st;
4378 kmp_uint64 ub_glob = p->ub_glob;
4379 kmp_uint64 num_tasks = p->num_tasks;
4380 kmp_uint64 grainsize = p->grainsize;
4381 kmp_uint64 extras = p->extras;
4382 kmp_int64 last_chunk = p->last_chunk;
4383 kmp_uint64 tc = p->tc;
4384 kmp_uint64 num_t_min = p->num_t_min;
4386 void *codeptr_ra = p->codeptr_ra;
4389 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4390 KMP_DEBUG_ASSERT(task != NULL);
4392 (
"__kmp_taskloop_task: T#%d, task %p: %lld tasks, grainsize"
4393 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
4394 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
4397 KMP_DEBUG_ASSERT(num_tasks * 2 + 1 > num_t_min);
4398 if (num_tasks > num_t_min)
4399 __kmp_taskloop_recur(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
4400 grainsize, extras, last_chunk, tc, num_t_min,
4406 __kmp_taskloop_linear(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
4407 grainsize, extras, last_chunk, tc,
4413 KA_TRACE(40, (
"__kmp_taskloop_task(exit): T#%d\n", gtid));
4435 void __kmp_taskloop_recur(
ident_t *loc,
int gtid, kmp_task_t *task,
4436 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4437 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
4438 kmp_uint64 grainsize, kmp_uint64 extras,
4439 kmp_int64 last_chunk, kmp_uint64 tc,
4440 kmp_uint64 num_t_min,
4445 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4446 KMP_DEBUG_ASSERT(task != NULL);
4447 KMP_DEBUG_ASSERT(num_tasks > num_t_min);
4449 (
"__kmp_taskloop_recur: T#%d, task %p: %lld tasks, grainsize"
4450 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
4451 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
4453 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
4454 kmp_uint64 lower = *lb;
4455 kmp_info_t *thread = __kmp_threads[gtid];
4457 kmp_task_t *next_task;
4458 size_t lower_offset =
4459 (
char *)lb - (
char *)task;
4460 size_t upper_offset =
4461 (
char *)ub - (
char *)task;
4463 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4464 (last_chunk < 0 ? last_chunk : extras));
4465 KMP_DEBUG_ASSERT(num_tasks > extras);
4466 KMP_DEBUG_ASSERT(num_tasks > 0);
4469 kmp_uint64 lb1, ub0, tc0, tc1, ext0, ext1;
4470 kmp_int64 last_chunk0 = 0, last_chunk1 = 0;
4471 kmp_uint64 gr_size0 = grainsize;
4472 kmp_uint64 n_tsk0 = num_tasks >> 1;
4473 kmp_uint64 n_tsk1 = num_tasks - n_tsk0;
4474 if (last_chunk < 0) {
4476 last_chunk1 = last_chunk;
4477 tc0 = grainsize * n_tsk0;
4479 }
else if (n_tsk0 <= extras) {
4482 ext1 = extras - n_tsk0;
4483 tc0 = gr_size0 * n_tsk0;
4488 tc1 = grainsize * n_tsk1;
4491 ub0 = lower + st * (tc0 - 1);
4495 next_task = __kmp_task_dup_alloc(thread, task);
4497 *(kmp_uint64 *)((
char *)next_task + lower_offset) = lb1;
4498 if (ptask_dup != NULL)
4499 ptask_dup(next_task, task, 0);
4504 kmp_taskdata_t *current_task = thread->th.th_current_task;
4505 thread->th.th_current_task = taskdata->td_parent;
4506 kmp_task_t *new_task =
4507 __kmpc_omp_task_alloc(loc, gtid, 1, 3 *
sizeof(
void *),
4508 sizeof(__taskloop_params_t), &__kmp_taskloop_task);
4510 thread->th.th_current_task = current_task;
4511 __taskloop_params_t *p = (__taskloop_params_t *)new_task->shareds;
4512 p->task = next_task;
4513 p->lb = (kmp_uint64 *)((
char *)next_task + lower_offset);
4514 p->ub = (kmp_uint64 *)((
char *)next_task + upper_offset);
4515 p->task_dup = task_dup;
4517 p->ub_glob = ub_glob;
4518 p->num_tasks = n_tsk1;
4519 p->grainsize = grainsize;
4521 p->last_chunk = last_chunk1;
4523 p->num_t_min = num_t_min;
4525 p->codeptr_ra = codeptr_ra;
4530 __kmp_omp_taskloop_task(NULL, gtid, new_task, codeptr_ra);
4532 __kmp_omp_task(gtid, new_task,
true);
4536 if (n_tsk0 > num_t_min)
4537 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0, gr_size0,
4538 ext0, last_chunk0, tc0, num_t_min,
4544 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0,
4545 gr_size0, ext0, last_chunk0, tc0,
4551 KA_TRACE(40, (
"__kmp_taskloop_recur(exit): T#%d\n", gtid));
4554 static void __kmp_taskloop(
ident_t *loc,
int gtid, kmp_task_t *task,
int if_val,
4555 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4556 int nogroup,
int sched, kmp_uint64 grainsize,
4557 int modifier,
void *task_dup) {
4558 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4559 KMP_DEBUG_ASSERT(task != NULL);
4561 #if OMPT_SUPPORT && OMPT_OPTIONAL
4562 OMPT_STORE_RETURN_ADDRESS(gtid);
4564 __kmpc_taskgroup(loc, gtid);
4569 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4572 kmp_uint64 lower = task_bounds.get_lb();
4573 kmp_uint64 upper = task_bounds.get_ub();
4574 kmp_uint64 ub_glob = upper;
4575 kmp_uint64 num_tasks = 0, extras = 0;
4576 kmp_int64 last_chunk =
4578 kmp_uint64 num_tasks_min = __kmp_taskloop_min_tasks;
4579 kmp_info_t *thread = __kmp_threads[gtid];
4580 kmp_taskdata_t *current_task = thread->th.th_current_task;
4582 KA_TRACE(20, (
"__kmp_taskloop: T#%d, task %p, lb %lld, ub %lld, st %lld, "
4583 "grain %llu(%d, %d), dup %p\n",
4584 gtid, taskdata, lower, upper, st, grainsize, sched, modifier,
4589 tc = upper - lower + 1;
4590 }
else if (st < 0) {
4591 tc = (lower - upper) / (-st) + 1;
4593 tc = (upper - lower) / st + 1;
4596 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d zero-trip loop\n", gtid));
4598 __kmp_task_start(gtid, task, current_task);
4600 __kmp_task_finish<false>(gtid, task, current_task);
4604 #if OMPT_SUPPORT && OMPT_OPTIONAL
4605 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
4606 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
4607 if (ompt_enabled.ompt_callback_work) {
4608 ompt_callbacks.ompt_callback(ompt_callback_work)(
4609 ompt_work_taskloop, ompt_scope_begin, &(team_info->parallel_data),
4610 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
4614 if (num_tasks_min == 0)
4617 KMP_MIN(thread->th.th_team_nproc * 10, INITIAL_TASK_DEQUE_SIZE);
4623 grainsize = thread->th.th_team_nproc * 10;
4626 if (grainsize > tc) {
4631 num_tasks = grainsize;
4632 grainsize = tc / num_tasks;
4633 extras = tc % num_tasks;
4637 if (grainsize > tc) {
4643 num_tasks = (tc + grainsize - 1) / grainsize;
4644 last_chunk = tc - (num_tasks * grainsize);
4647 num_tasks = tc / grainsize;
4649 grainsize = tc / num_tasks;
4650 extras = tc % num_tasks;
4655 KMP_ASSERT2(0,
"unknown scheduling of taskloop");
4658 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4659 (last_chunk < 0 ? last_chunk : extras));
4660 KMP_DEBUG_ASSERT(num_tasks > extras);
4661 KMP_DEBUG_ASSERT(num_tasks > 0);
4667 taskdata->td_flags.task_serial = 1;
4668 taskdata->td_flags.tiedness = TASK_TIED;
4670 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4671 grainsize, extras, last_chunk, tc,
4673 OMPT_GET_RETURN_ADDRESS(0),
4678 }
else if (num_tasks > num_tasks_min && !taskdata->td_flags.native) {
4679 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go recursive: tc %llu, #tasks %llu"
4680 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
4681 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
4683 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4684 grainsize, extras, last_chunk, tc, num_tasks_min,
4686 OMPT_GET_RETURN_ADDRESS(0),
4690 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go linear: tc %llu, #tasks %llu"
4691 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
4692 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
4694 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4695 grainsize, extras, last_chunk, tc,
4697 OMPT_GET_RETURN_ADDRESS(0),
4702 #if OMPT_SUPPORT && OMPT_OPTIONAL
4703 if (ompt_enabled.ompt_callback_work) {
4704 ompt_callbacks.ompt_callback(ompt_callback_work)(
4705 ompt_work_taskloop, ompt_scope_end, &(team_info->parallel_data),
4706 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
4711 #if OMPT_SUPPORT && OMPT_OPTIONAL
4712 OMPT_STORE_RETURN_ADDRESS(gtid);
4714 __kmpc_end_taskgroup(loc, gtid);
4716 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d\n", gtid));
4736 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
int nogroup,
4737 int sched, kmp_uint64 grainsize,
void *task_dup) {
4738 __kmp_assert_valid_gtid(gtid);
4739 KA_TRACE(20, (
"__kmpc_taskloop(enter): T#%d\n", gtid));
4740 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
4742 KA_TRACE(20, (
"__kmpc_taskloop(exit): T#%d\n", gtid));
4763 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4764 int nogroup,
int sched, kmp_uint64 grainsize,
4765 int modifier,
void *task_dup) {
4766 __kmp_assert_valid_gtid(gtid);
4767 KA_TRACE(20, (
"__kmpc_taskloop_5(enter): T#%d\n", gtid));
4768 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
4769 modifier, task_dup);
4770 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