16 #include "kmp_stats.h"
17 #include "kmp_wait_release.h"
18 #include "kmp_taskdeps.h"
21 #include "ompt-specific.h"
25 static void __kmp_enable_tasking(kmp_task_team_t *task_team,
26 kmp_info_t *this_thr);
27 static void __kmp_alloc_task_deque(kmp_info_t *thread,
28 kmp_thread_data_t *thread_data);
29 static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
30 kmp_task_team_t *task_team);
31 static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask);
33 #ifdef BUILD_TIED_TASK_STACK
42 static void __kmp_trace_task_stack(kmp_int32 gtid,
43 kmp_thread_data_t *thread_data,
44 int threshold,
char *location) {
45 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
46 kmp_taskdata_t **stack_top = task_stack->ts_top;
47 kmp_int32 entries = task_stack->ts_entries;
48 kmp_taskdata_t *tied_task;
52 (
"__kmp_trace_task_stack(start): location = %s, gtid = %d, entries = %d, "
53 "first_block = %p, stack_top = %p \n",
54 location, gtid, entries, task_stack->ts_first_block, stack_top));
56 KMP_DEBUG_ASSERT(stack_top != NULL);
57 KMP_DEBUG_ASSERT(entries > 0);
59 while (entries != 0) {
60 KMP_DEBUG_ASSERT(stack_top != &task_stack->ts_first_block.sb_block[0]);
62 if (entries & TASK_STACK_INDEX_MASK == 0) {
63 kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(stack_top);
65 stack_block = stack_block->sb_prev;
66 stack_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE];
73 tied_task = *stack_top;
75 KMP_DEBUG_ASSERT(tied_task != NULL);
76 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
79 (
"__kmp_trace_task_stack(%s): gtid=%d, entry=%d, "
80 "stack_top=%p, tied_task=%p\n",
81 location, gtid, entries, stack_top, tied_task));
83 KMP_DEBUG_ASSERT(stack_top == &task_stack->ts_first_block.sb_block[0]);
86 (
"__kmp_trace_task_stack(exit): location = %s, gtid = %d\n",
96 static void __kmp_init_task_stack(kmp_int32 gtid,
97 kmp_thread_data_t *thread_data) {
98 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
99 kmp_stack_block_t *first_block;
102 first_block = &task_stack->ts_first_block;
103 task_stack->ts_top = (kmp_taskdata_t **)first_block;
104 memset((
void *)first_block,
'\0',
105 TASK_STACK_BLOCK_SIZE *
sizeof(kmp_taskdata_t *));
108 task_stack->ts_entries = TASK_STACK_EMPTY;
109 first_block->sb_next = NULL;
110 first_block->sb_prev = NULL;
117 static void __kmp_free_task_stack(kmp_int32 gtid,
118 kmp_thread_data_t *thread_data) {
119 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
120 kmp_stack_block_t *stack_block = &task_stack->ts_first_block;
122 KMP_DEBUG_ASSERT(task_stack->ts_entries == TASK_STACK_EMPTY);
124 while (stack_block != NULL) {
125 kmp_stack_block_t *next_block = (stack_block) ? stack_block->sb_next : NULL;
127 stack_block->sb_next = NULL;
128 stack_block->sb_prev = NULL;
129 if (stack_block != &task_stack->ts_first_block) {
130 __kmp_thread_free(thread,
133 stack_block = next_block;
136 task_stack->ts_entries = 0;
137 task_stack->ts_top = NULL;
146 static void __kmp_push_task_stack(kmp_int32 gtid, kmp_info_t *thread,
147 kmp_taskdata_t *tied_task) {
149 kmp_thread_data_t *thread_data =
150 &thread->th.th_task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
151 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
153 if (tied_task->td_flags.team_serial || tied_task->td_flags.tasking_ser) {
157 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
158 KMP_DEBUG_ASSERT(task_stack->ts_top != NULL);
161 (
"__kmp_push_task_stack(enter): GTID: %d; THREAD: %p; TASK: %p\n",
162 gtid, thread, tied_task));
164 *(task_stack->ts_top) = tied_task;
167 task_stack->ts_top++;
168 task_stack->ts_entries++;
170 if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) {
172 kmp_stack_block_t *stack_block =
173 (kmp_stack_block_t *)(task_stack->ts_top - TASK_STACK_BLOCK_SIZE);
176 if (stack_block->sb_next !=
178 task_stack->ts_top = &stack_block->sb_next->sb_block[0];
180 kmp_stack_block_t *new_block = (kmp_stack_block_t *)__kmp_thread_calloc(
181 thread,
sizeof(kmp_stack_block_t));
183 task_stack->ts_top = &new_block->sb_block[0];
184 stack_block->sb_next = new_block;
185 new_block->sb_prev = stack_block;
186 new_block->sb_next = NULL;
190 (
"__kmp_push_task_stack(): GTID: %d; TASK: %p; Alloc new block: %p\n",
191 gtid, tied_task, new_block));
194 KA_TRACE(20, (
"__kmp_push_task_stack(exit): GTID: %d; TASK: %p\n", gtid,
205 static void __kmp_pop_task_stack(kmp_int32 gtid, kmp_info_t *thread,
206 kmp_taskdata_t *ending_task) {
208 kmp_thread_data_t *thread_data =
209 &thread->th.th_task_team->tt_threads_data[__kmp_tid_from_gtid(gtid)];
210 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
211 kmp_taskdata_t *tied_task;
213 if (ending_task->td_flags.team_serial || ending_task->td_flags.tasking_ser) {
218 KMP_DEBUG_ASSERT(task_stack->ts_top != NULL);
219 KMP_DEBUG_ASSERT(task_stack->ts_entries > 0);
221 KA_TRACE(20, (
"__kmp_pop_task_stack(enter): GTID: %d; THREAD: %p\n", gtid,
225 if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) {
226 kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(task_stack->ts_top);
228 stack_block = stack_block->sb_prev;
229 task_stack->ts_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE];
233 task_stack->ts_top--;
234 task_stack->ts_entries--;
236 tied_task = *(task_stack->ts_top);
238 KMP_DEBUG_ASSERT(tied_task != NULL);
239 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
240 KMP_DEBUG_ASSERT(tied_task == ending_task);
242 KA_TRACE(20, (
"__kmp_pop_task_stack(exit): GTID: %d; TASK: %p\n", gtid,
251 static bool __kmp_task_is_allowed(
int gtid,
const kmp_int32 is_constrained,
252 const kmp_taskdata_t *tasknew,
253 const kmp_taskdata_t *taskcurr) {
254 if (is_constrained && (tasknew->td_flags.tiedness == TASK_TIED)) {
258 kmp_taskdata_t *current = taskcurr->td_last_tied;
259 KMP_DEBUG_ASSERT(current != NULL);
261 if (current->td_flags.tasktype == TASK_EXPLICIT ||
262 current->td_taskwait_thread > 0) {
263 kmp_int32 level = current->td_level;
264 kmp_taskdata_t *parent = tasknew->td_parent;
265 while (parent != current && parent->td_level > level) {
267 parent = parent->td_parent;
268 KMP_DEBUG_ASSERT(parent != NULL);
270 if (parent != current)
275 kmp_depnode_t *node = tasknew->td_depnode;
276 if (UNLIKELY(node && (node->dn.mtx_num_locks > 0))) {
277 for (
int i = 0; i < node->dn.mtx_num_locks; ++i) {
278 KMP_DEBUG_ASSERT(node->dn.mtx_locks[i] != NULL);
279 if (__kmp_test_lock(node->dn.mtx_locks[i], gtid))
282 for (
int j = i - 1; j >= 0; --j)
283 __kmp_release_lock(node->dn.mtx_locks[j], gtid);
287 node->dn.mtx_num_locks = -node->dn.mtx_num_locks;
296 static void __kmp_realloc_task_deque(kmp_info_t *thread,
297 kmp_thread_data_t *thread_data) {
298 kmp_int32 size = TASK_DEQUE_SIZE(thread_data->td);
299 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == size);
300 kmp_int32 new_size = 2 * size;
302 KE_TRACE(10, (
"__kmp_realloc_task_deque: T#%d reallocating deque[from %d to "
303 "%d] for thread_data %p\n",
304 __kmp_gtid_from_thread(thread), size, new_size, thread_data));
306 kmp_taskdata_t **new_deque =
307 (kmp_taskdata_t **)__kmp_allocate(new_size *
sizeof(kmp_taskdata_t *));
310 for (i = thread_data->td.td_deque_head, j = 0; j < size;
311 i = (i + 1) & TASK_DEQUE_MASK(thread_data->td), j++)
312 new_deque[j] = thread_data->td.td_deque[i];
314 __kmp_free(thread_data->td.td_deque);
316 thread_data->td.td_deque_head = 0;
317 thread_data->td.td_deque_tail = size;
318 thread_data->td.td_deque = new_deque;
319 thread_data->td.td_deque_size = new_size;
323 static kmp_int32 __kmp_push_task(kmp_int32 gtid, kmp_task_t *task) {
324 kmp_info_t *thread = __kmp_threads[gtid];
325 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
330 if (UNLIKELY(taskdata->td_flags.hidden_helper &&
331 !KMP_HIDDEN_HELPER_THREAD(gtid))) {
332 kmp_int32 shadow_gtid = KMP_GTID_TO_SHADOW_GTID(gtid);
333 __kmpc_give_task(task, __kmp_tid_from_gtid(shadow_gtid));
335 __kmp_hidden_helper_worker_thread_signal();
336 return TASK_SUCCESSFULLY_PUSHED;
339 kmp_task_team_t *task_team = thread->th.th_task_team;
340 kmp_int32 tid = __kmp_tid_from_gtid(gtid);
341 kmp_thread_data_t *thread_data;
344 (
"__kmp_push_task: T#%d trying to push task %p.\n", gtid, taskdata));
346 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
349 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
350 KMP_DEBUG_USE_VAR(counter);
353 (
"__kmp_push_task: T#%d untied_count (%d) incremented for task %p\n",
354 gtid, counter, taskdata));
358 if (UNLIKELY(taskdata->td_flags.task_serial)) {
359 KA_TRACE(20, (
"__kmp_push_task: T#%d team serialized; returning "
360 "TASK_NOT_PUSHED for task %p\n",
362 return TASK_NOT_PUSHED;
367 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
368 if (UNLIKELY(!KMP_TASKING_ENABLED(task_team))) {
369 __kmp_enable_tasking(task_team, thread);
371 KMP_DEBUG_ASSERT(TCR_4(task_team->tt.tt_found_tasks) == TRUE);
372 KMP_DEBUG_ASSERT(TCR_PTR(task_team->tt.tt_threads_data) != NULL);
375 thread_data = &task_team->tt.tt_threads_data[tid];
380 if (UNLIKELY(thread_data->td.td_deque == NULL)) {
381 __kmp_alloc_task_deque(thread, thread_data);
386 if (TCR_4(thread_data->td.td_deque_ntasks) >=
387 TASK_DEQUE_SIZE(thread_data->td)) {
388 if (__kmp_enable_task_throttling &&
389 __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
390 thread->th.th_current_task)) {
391 KA_TRACE(20, (
"__kmp_push_task: T#%d deque is full; returning "
392 "TASK_NOT_PUSHED for task %p\n",
394 return TASK_NOT_PUSHED;
396 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
398 if (TCR_4(thread_data->td.td_deque_ntasks) >=
399 TASK_DEQUE_SIZE(thread_data->td)) {
401 __kmp_realloc_task_deque(thread, thread_data);
407 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
409 if (TCR_4(thread_data->td.td_deque_ntasks) >=
410 TASK_DEQUE_SIZE(thread_data->td)) {
411 if (__kmp_enable_task_throttling &&
412 __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
413 thread->th.th_current_task)) {
414 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
415 KA_TRACE(20, (
"__kmp_push_task: T#%d deque is full on 2nd check; "
416 "returning TASK_NOT_PUSHED for task %p\n",
418 return TASK_NOT_PUSHED;
421 __kmp_realloc_task_deque(thread, thread_data);
426 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) <
427 TASK_DEQUE_SIZE(thread_data->td));
429 thread_data->td.td_deque[thread_data->td.td_deque_tail] =
432 thread_data->td.td_deque_tail =
433 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
434 TCW_4(thread_data->td.td_deque_ntasks,
435 TCR_4(thread_data->td.td_deque_ntasks) + 1);
436 KMP_FSYNC_RELEASING(thread->th.th_current_task);
437 KMP_FSYNC_RELEASING(taskdata);
438 KA_TRACE(20, (
"__kmp_push_task: T#%d returning TASK_SUCCESSFULLY_PUSHED: "
439 "task=%p ntasks=%d head=%u tail=%u\n",
440 gtid, taskdata, thread_data->td.td_deque_ntasks,
441 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
443 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
445 return TASK_SUCCESSFULLY_PUSHED;
452 void __kmp_pop_current_task_from_thread(kmp_info_t *this_thr) {
453 KF_TRACE(10, (
"__kmp_pop_current_task_from_thread(enter): T#%d "
454 "this_thread=%p, curtask=%p, "
455 "curtask_parent=%p\n",
456 0, this_thr, this_thr->th.th_current_task,
457 this_thr->th.th_current_task->td_parent));
459 this_thr->th.th_current_task = this_thr->th.th_current_task->td_parent;
461 KF_TRACE(10, (
"__kmp_pop_current_task_from_thread(exit): T#%d "
462 "this_thread=%p, curtask=%p, "
463 "curtask_parent=%p\n",
464 0, this_thr, this_thr->th.th_current_task,
465 this_thr->th.th_current_task->td_parent));
474 void __kmp_push_current_task_to_thread(kmp_info_t *this_thr, kmp_team_t *team,
478 KF_TRACE(10, (
"__kmp_push_current_task_to_thread(enter): T#%d this_thread=%p "
481 tid, this_thr, this_thr->th.th_current_task,
482 team->t.t_implicit_task_taskdata[tid].td_parent));
484 KMP_DEBUG_ASSERT(this_thr != NULL);
487 if (this_thr->th.th_current_task != &team->t.t_implicit_task_taskdata[0]) {
488 team->t.t_implicit_task_taskdata[0].td_parent =
489 this_thr->th.th_current_task;
490 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[0];
493 team->t.t_implicit_task_taskdata[tid].td_parent =
494 team->t.t_implicit_task_taskdata[0].td_parent;
495 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[tid];
498 KF_TRACE(10, (
"__kmp_push_current_task_to_thread(exit): T#%d this_thread=%p "
501 tid, this_thr, this_thr->th.th_current_task,
502 team->t.t_implicit_task_taskdata[tid].td_parent));
510 static void __kmp_task_start(kmp_int32 gtid, kmp_task_t *task,
511 kmp_taskdata_t *current_task) {
512 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
513 kmp_info_t *thread = __kmp_threads[gtid];
516 (
"__kmp_task_start(enter): T#%d starting task %p: current_task=%p\n",
517 gtid, taskdata, current_task));
519 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
524 current_task->td_flags.executing = 0;
527 #ifdef BUILD_TIED_TASK_STACK
528 if (taskdata->td_flags.tiedness == TASK_TIED) {
529 __kmp_push_task_stack(gtid, thread, taskdata);
534 thread->th.th_current_task = taskdata;
536 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 0 ||
537 taskdata->td_flags.tiedness == TASK_UNTIED);
538 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0 ||
539 taskdata->td_flags.tiedness == TASK_UNTIED);
540 taskdata->td_flags.started = 1;
541 taskdata->td_flags.executing = 1;
542 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
543 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
550 KA_TRACE(10, (
"__kmp_task_start(exit): T#%d task=%p\n", gtid, taskdata));
561 static inline void __ompt_task_init(kmp_taskdata_t *task,
int tid) {
563 task->ompt_task_info.task_data.value = 0;
564 task->ompt_task_info.frame.exit_frame = ompt_data_none;
565 task->ompt_task_info.frame.enter_frame = ompt_data_none;
566 task->ompt_task_info.frame.exit_frame_flags =
567 ompt_frame_runtime | ompt_frame_framepointer;
568 task->ompt_task_info.frame.enter_frame_flags =
569 ompt_frame_runtime | ompt_frame_framepointer;
574 static inline void __ompt_task_start(kmp_task_t *task,
575 kmp_taskdata_t *current_task,
577 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
578 ompt_task_status_t status = ompt_task_switch;
579 if (__kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded) {
580 status = ompt_task_yield;
581 __kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded = 0;
584 if (ompt_enabled.ompt_callback_task_schedule) {
585 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
586 &(current_task->ompt_task_info.task_data), status,
587 &(taskdata->ompt_task_info.task_data));
589 taskdata->ompt_task_info.scheduling_parent = current_task;
594 static inline void __ompt_task_finish(kmp_task_t *task,
595 kmp_taskdata_t *resumed_task,
596 ompt_task_status_t status) {
597 if (ompt_enabled.ompt_callback_task_schedule) {
598 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
599 if (__kmp_omp_cancellation && taskdata->td_taskgroup &&
600 taskdata->td_taskgroup->cancel_request == cancel_taskgroup) {
601 status = ompt_task_cancel;
605 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
606 &(taskdata->ompt_task_info.task_data), status,
607 (resumed_task ? &(resumed_task->ompt_task_info.task_data) : NULL));
613 static void __kmpc_omp_task_begin_if0_template(
ident_t *loc_ref, kmp_int32 gtid,
616 void *return_address) {
617 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
618 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
620 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(enter): T#%d loc=%p task=%p "
622 gtid, loc_ref, taskdata, current_task));
624 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
627 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
628 KMP_DEBUG_USE_VAR(counter);
629 KA_TRACE(20, (
"__kmpc_omp_task_begin_if0: T#%d untied_count (%d) "
630 "incremented for task %p\n",
631 gtid, counter, taskdata));
634 taskdata->td_flags.task_serial =
636 __kmp_task_start(gtid, task, current_task);
640 if (current_task->ompt_task_info.frame.enter_frame.ptr == NULL) {
641 current_task->ompt_task_info.frame.enter_frame.ptr =
642 taskdata->ompt_task_info.frame.exit_frame.ptr = frame_address;
643 current_task->ompt_task_info.frame.enter_frame_flags =
644 taskdata->ompt_task_info.frame.exit_frame_flags =
645 ompt_frame_application | ompt_frame_framepointer;
647 if (ompt_enabled.ompt_callback_task_create) {
648 ompt_task_info_t *parent_info = &(current_task->ompt_task_info);
649 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
650 &(parent_info->task_data), &(parent_info->frame),
651 &(taskdata->ompt_task_info.task_data),
652 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(taskdata), 0,
655 __ompt_task_start(task, current_task, gtid);
659 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(exit): T#%d loc=%p task=%p,\n", gtid,
665 static void __kmpc_omp_task_begin_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
668 void *return_address) {
669 __kmpc_omp_task_begin_if0_template<true>(loc_ref, gtid, task, frame_address,
680 void __kmpc_omp_task_begin_if0(
ident_t *loc_ref, kmp_int32 gtid,
683 if (UNLIKELY(ompt_enabled.enabled)) {
684 OMPT_STORE_RETURN_ADDRESS(gtid);
685 __kmpc_omp_task_begin_if0_ompt(loc_ref, gtid, task,
686 OMPT_GET_FRAME_ADDRESS(1),
687 OMPT_LOAD_RETURN_ADDRESS(gtid));
691 __kmpc_omp_task_begin_if0_template<false>(loc_ref, gtid, task, NULL, NULL);
697 void __kmpc_omp_task_begin(
ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task) {
698 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
702 (
"__kmpc_omp_task_begin(enter): T#%d loc=%p task=%p current_task=%p\n",
703 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task), current_task));
705 __kmp_task_start(gtid, task, current_task);
707 KA_TRACE(10, (
"__kmpc_omp_task_begin(exit): T#%d loc=%p task=%p,\n", gtid,
708 loc_ref, KMP_TASK_TO_TASKDATA(task)));
718 static void __kmp_free_task(kmp_int32 gtid, kmp_taskdata_t *taskdata,
719 kmp_info_t *thread) {
720 KA_TRACE(30, (
"__kmp_free_task: T#%d freeing data from task %p\n", gtid,
724 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
725 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0);
726 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 1);
727 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
728 KMP_DEBUG_ASSERT(taskdata->td_allocated_child_tasks == 0 ||
729 taskdata->td_flags.task_serial == 1);
730 KMP_DEBUG_ASSERT(taskdata->td_incomplete_child_tasks == 0);
732 taskdata->td_flags.freed = 1;
735 __kmp_fast_free(thread, taskdata);
737 __kmp_thread_free(thread, taskdata);
739 KA_TRACE(20, (
"__kmp_free_task: T#%d freed task %p\n", gtid, taskdata));
748 static void __kmp_free_task_and_ancestors(kmp_int32 gtid,
749 kmp_taskdata_t *taskdata,
750 kmp_info_t *thread) {
753 kmp_int32 team_serial =
754 (taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) &&
755 !taskdata->td_flags.proxy;
756 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
758 kmp_int32 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
759 KMP_DEBUG_ASSERT(children >= 0);
762 while (children == 0) {
763 kmp_taskdata_t *parent_taskdata = taskdata->td_parent;
765 KA_TRACE(20, (
"__kmp_free_task_and_ancestors(enter): T#%d task %p complete "
766 "and freeing itself\n",
770 __kmp_free_task(gtid, taskdata, thread);
772 taskdata = parent_taskdata;
778 if (taskdata->td_flags.tasktype == TASK_IMPLICIT) {
779 if (taskdata->td_dephash) {
780 int children = KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks);
781 kmp_tasking_flags_t flags_old = taskdata->td_flags;
782 if (children == 0 && flags_old.complete == 1) {
783 kmp_tasking_flags_t flags_new = flags_old;
784 flags_new.complete = 0;
785 if (KMP_COMPARE_AND_STORE_ACQ32(
786 RCAST(kmp_int32 *, &taskdata->td_flags),
787 *RCAST(kmp_int32 *, &flags_old),
788 *RCAST(kmp_int32 *, &flags_new))) {
789 KA_TRACE(100, (
"__kmp_free_task_and_ancestors: T#%d cleans "
790 "dephash of implicit task %p\n",
793 __kmp_dephash_free_entries(thread, taskdata->td_dephash);
800 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
801 KMP_DEBUG_ASSERT(children >= 0);
805 20, (
"__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; "
806 "not freeing it yet\n",
807 gtid, taskdata, children));
818 static bool __kmp_track_children_task(kmp_taskdata_t *taskdata) {
819 kmp_tasking_flags_t flags = taskdata->td_flags;
820 bool ret = !(flags.team_serial || flags.tasking_ser);
821 ret = ret || flags.proxy == TASK_PROXY ||
822 flags.detachable == TASK_DETACHABLE || flags.hidden_helper;
824 KMP_ATOMIC_LD_ACQ(&taskdata->td_parent->td_incomplete_child_tasks) > 0;
838 static void __kmp_task_finish(kmp_int32 gtid, kmp_task_t *task,
839 kmp_taskdata_t *resumed_task) {
840 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
841 kmp_info_t *thread = __kmp_threads[gtid];
842 kmp_task_team_t *task_team =
843 thread->th.th_task_team;
845 kmp_int32 children = 0;
847 KA_TRACE(10, (
"__kmp_task_finish(enter): T#%d finishing task %p and resuming "
849 gtid, taskdata, resumed_task));
851 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
854 #ifdef BUILD_TIED_TASK_STACK
855 if (taskdata->td_flags.tiedness == TASK_TIED) {
856 __kmp_pop_task_stack(gtid, thread, taskdata);
860 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
863 kmp_int32 counter = KMP_ATOMIC_DEC(&taskdata->td_untied_count) - 1;
866 (
"__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n",
867 gtid, counter, taskdata));
871 if (resumed_task == NULL) {
872 KMP_DEBUG_ASSERT(taskdata->td_flags.task_serial);
873 resumed_task = taskdata->td_parent;
876 thread->th.th_current_task = resumed_task;
877 resumed_task->td_flags.executing = 1;
878 KA_TRACE(10, (
"__kmp_task_finish(exit): T#%d partially done task %p, "
879 "resuming task %p\n",
880 gtid, taskdata, resumed_task));
888 (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) ==
889 taskdata->td_flags.task_serial);
890 if (taskdata->td_flags.task_serial) {
891 if (resumed_task == NULL) {
892 resumed_task = taskdata->td_parent;
896 KMP_DEBUG_ASSERT(resumed_task !=
906 if (UNLIKELY(taskdata->td_flags.destructors_thunk)) {
907 kmp_routine_entry_t destr_thunk = task->data1.destructors;
908 KMP_ASSERT(destr_thunk);
909 destr_thunk(gtid, task);
912 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
913 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 1);
914 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
917 if (UNLIKELY(taskdata->td_flags.detachable == TASK_DETACHABLE)) {
918 if (taskdata->td_allow_completion_event.type ==
919 KMP_EVENT_ALLOW_COMPLETION) {
921 __kmp_acquire_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
922 if (taskdata->td_allow_completion_event.type ==
923 KMP_EVENT_ALLOW_COMPLETION) {
925 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
926 taskdata->td_flags.executing = 0;
933 __ompt_task_finish(task, resumed_task, ompt_task_detach);
939 taskdata->td_flags.proxy = TASK_PROXY;
942 __kmp_release_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
947 taskdata->td_flags.complete = 1;
952 __ompt_task_finish(task, resumed_task, ompt_task_complete);
956 if (__kmp_track_children_task(taskdata)) {
957 __kmp_release_deps(gtid, taskdata);
962 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks);
963 KMP_DEBUG_ASSERT(children >= 0);
964 if (taskdata->td_taskgroup)
965 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
966 }
else if (task_team && (task_team->tt.tt_found_proxy_tasks ||
967 task_team->tt.tt_hidden_helper_task_encountered)) {
970 __kmp_release_deps(gtid, taskdata);
976 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
977 taskdata->td_flags.executing = 0;
981 20, (
"__kmp_task_finish: T#%d finished task %p, %d incomplete children\n",
982 gtid, taskdata, children));
988 thread->th.th_current_task = resumed_task;
990 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
994 resumed_task->td_flags.executing = 1;
997 10, (
"__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n",
998 gtid, taskdata, resumed_task));
1003 template <
bool ompt>
1004 static void __kmpc_omp_task_complete_if0_template(
ident_t *loc_ref,
1007 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n",
1008 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
1009 KMP_DEBUG_ASSERT(gtid >= 0);
1011 __kmp_task_finish<ompt>(gtid, task, NULL);
1013 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n",
1014 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
1018 ompt_frame_t *ompt_frame;
1019 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
1020 ompt_frame->enter_frame = ompt_data_none;
1021 ompt_frame->enter_frame_flags =
1022 ompt_frame_runtime | ompt_frame_framepointer;
1031 void __kmpc_omp_task_complete_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
1033 __kmpc_omp_task_complete_if0_template<true>(loc_ref, gtid, task);
1042 void __kmpc_omp_task_complete_if0(
ident_t *loc_ref, kmp_int32 gtid,
1045 if (UNLIKELY(ompt_enabled.enabled)) {
1046 __kmpc_omp_task_complete_if0_ompt(loc_ref, gtid, task);
1050 __kmpc_omp_task_complete_if0_template<false>(loc_ref, gtid, task);
1056 void __kmpc_omp_task_complete(
ident_t *loc_ref, kmp_int32 gtid,
1058 KA_TRACE(10, (
"__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n", gtid,
1059 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1061 __kmp_task_finish<false>(gtid, task,
1064 KA_TRACE(10, (
"__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n", gtid,
1065 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1081 void __kmp_init_implicit_task(
ident_t *loc_ref, kmp_info_t *this_thr,
1082 kmp_team_t *team,
int tid,
int set_curr_task) {
1083 kmp_taskdata_t *task = &team->t.t_implicit_task_taskdata[tid];
1087 (
"__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n",
1088 tid, team, task, set_curr_task ?
"TRUE" :
"FALSE"));
1090 task->td_task_id = KMP_GEN_TASK_ID();
1091 task->td_team = team;
1094 task->td_ident = loc_ref;
1095 task->td_taskwait_ident = NULL;
1096 task->td_taskwait_counter = 0;
1097 task->td_taskwait_thread = 0;
1099 task->td_flags.tiedness = TASK_TIED;
1100 task->td_flags.tasktype = TASK_IMPLICIT;
1101 task->td_flags.proxy = TASK_FULL;
1104 task->td_flags.task_serial = 1;
1105 task->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1106 task->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1108 task->td_flags.started = 1;
1109 task->td_flags.executing = 1;
1110 task->td_flags.complete = 0;
1111 task->td_flags.freed = 0;
1113 task->td_depnode = NULL;
1114 task->td_last_tied = task;
1115 task->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1117 if (set_curr_task) {
1118 KMP_ATOMIC_ST_REL(&task->td_incomplete_child_tasks, 0);
1120 KMP_ATOMIC_ST_REL(&task->td_allocated_child_tasks, 0);
1121 task->td_taskgroup = NULL;
1122 task->td_dephash = NULL;
1123 __kmp_push_current_task_to_thread(this_thr, team, tid);
1125 KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0);
1126 KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0);
1130 if (UNLIKELY(ompt_enabled.enabled))
1131 __ompt_task_init(task, tid);
1134 KF_TRACE(10, (
"__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n", tid,
1143 void __kmp_finish_implicit_task(kmp_info_t *thread) {
1144 kmp_taskdata_t *task = thread->th.th_current_task;
1145 if (task->td_dephash) {
1147 task->td_flags.complete = 1;
1148 children = KMP_ATOMIC_LD_ACQ(&task->td_incomplete_child_tasks);
1149 kmp_tasking_flags_t flags_old = task->td_flags;
1150 if (children == 0 && flags_old.complete == 1) {
1151 kmp_tasking_flags_t flags_new = flags_old;
1152 flags_new.complete = 0;
1153 if (KMP_COMPARE_AND_STORE_ACQ32(RCAST(kmp_int32 *, &task->td_flags),
1154 *RCAST(kmp_int32 *, &flags_old),
1155 *RCAST(kmp_int32 *, &flags_new))) {
1156 KA_TRACE(100, (
"__kmp_finish_implicit_task: T#%d cleans "
1157 "dephash of implicit task %p\n",
1158 thread->th.th_info.ds.ds_gtid, task));
1159 __kmp_dephash_free_entries(thread, task->td_dephash);
1169 void __kmp_free_implicit_task(kmp_info_t *thread) {
1170 kmp_taskdata_t *task = thread->th.th_current_task;
1171 if (task && task->td_dephash) {
1172 __kmp_dephash_free(thread, task->td_dephash);
1173 task->td_dephash = NULL;
1179 static size_t __kmp_round_up_to_val(
size_t size,
size_t val) {
1180 if (size & (val - 1)) {
1182 if (size <= KMP_SIZE_T_MAX - val) {
1201 kmp_task_t *__kmp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1202 kmp_tasking_flags_t *flags,
1203 size_t sizeof_kmp_task_t,
size_t sizeof_shareds,
1204 kmp_routine_entry_t task_entry) {
1206 kmp_taskdata_t *taskdata;
1207 kmp_info_t *thread = __kmp_threads[gtid];
1208 kmp_team_t *team = thread->th.th_team;
1209 kmp_taskdata_t *parent_task = thread->th.th_current_task;
1210 size_t shareds_offset;
1212 if (UNLIKELY(!TCR_4(__kmp_init_middle)))
1213 __kmp_middle_initialize();
1215 if (flags->hidden_helper) {
1216 if (__kmp_enable_hidden_helper) {
1217 if (!TCR_4(__kmp_init_hidden_helper))
1218 __kmp_hidden_helper_initialize();
1221 flags->hidden_helper = FALSE;
1225 KA_TRACE(10, (
"__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) "
1226 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1227 gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t,
1228 sizeof_shareds, task_entry));
1230 KMP_DEBUG_ASSERT(parent_task);
1231 if (parent_task->td_flags.final) {
1232 if (flags->merged_if0) {
1237 if (flags->tiedness == TASK_UNTIED && !team->t.t_serialized) {
1241 KMP_CHECK_UPDATE(thread->th.th_task_team->tt.tt_untied_task_encountered, 1);
1247 if (UNLIKELY(flags->proxy == TASK_PROXY ||
1248 flags->detachable == TASK_DETACHABLE || flags->hidden_helper)) {
1249 if (flags->proxy == TASK_PROXY) {
1250 flags->tiedness = TASK_UNTIED;
1251 flags->merged_if0 = 1;
1255 if ((thread->th.th_task_team) == NULL) {
1258 KMP_DEBUG_ASSERT(team->t.t_serialized);
1260 (
"T#%d creating task team in __kmp_task_alloc for proxy task\n",
1263 __kmp_task_team_setup(thread, team, 1);
1264 thread->th.th_task_team = team->t.t_task_team[thread->th.th_task_state];
1266 kmp_task_team_t *task_team = thread->th.th_task_team;
1269 if (!KMP_TASKING_ENABLED(task_team)) {
1272 (
"T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid));
1273 __kmp_enable_tasking(task_team, thread);
1274 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
1275 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
1277 if (thread_data->td.td_deque == NULL) {
1278 __kmp_alloc_task_deque(thread, thread_data);
1282 if ((flags->proxy == TASK_PROXY || flags->detachable == TASK_DETACHABLE) &&
1283 task_team->tt.tt_found_proxy_tasks == FALSE)
1284 TCW_4(task_team->tt.tt_found_proxy_tasks, TRUE);
1285 if (flags->hidden_helper &&
1286 task_team->tt.tt_hidden_helper_task_encountered == FALSE)
1287 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, TRUE);
1292 shareds_offset =
sizeof(kmp_taskdata_t) + sizeof_kmp_task_t;
1293 shareds_offset = __kmp_round_up_to_val(shareds_offset,
sizeof(
void *));
1296 KA_TRACE(30, (
"__kmp_task_alloc: T#%d First malloc size: %ld\n", gtid,
1298 KA_TRACE(30, (
"__kmp_task_alloc: T#%d Second malloc size: %ld\n", gtid,
1303 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, shareds_offset +
1306 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, shareds_offset +
1310 task = KMP_TASKDATA_TO_TASK(taskdata);
1313 #if KMP_ARCH_X86 || KMP_ARCH_PPC64 || !KMP_HAVE_QUAD
1314 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(
double) - 1)) == 0);
1315 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(
double) - 1)) == 0);
1317 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(_Quad) - 1)) == 0);
1318 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(_Quad) - 1)) == 0);
1320 if (sizeof_shareds > 0) {
1322 task->shareds = &((
char *)taskdata)[shareds_offset];
1324 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
1327 task->shareds = NULL;
1329 task->routine = task_entry;
1332 taskdata->td_task_id = KMP_GEN_TASK_ID();
1333 taskdata->td_team = thread->th.th_team;
1334 taskdata->td_alloc_thread = thread;
1335 taskdata->td_parent = parent_task;
1336 taskdata->td_level = parent_task->td_level + 1;
1337 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
1338 taskdata->td_ident = loc_ref;
1339 taskdata->td_taskwait_ident = NULL;
1340 taskdata->td_taskwait_counter = 0;
1341 taskdata->td_taskwait_thread = 0;
1342 KMP_DEBUG_ASSERT(taskdata->td_parent != NULL);
1344 if (flags->proxy == TASK_FULL)
1345 copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs);
1347 taskdata->td_flags = *flags;
1348 taskdata->td_task_team = thread->th.th_task_team;
1349 taskdata->td_size_alloc = shareds_offset + sizeof_shareds;
1350 taskdata->td_flags.tasktype = TASK_EXPLICIT;
1353 if (flags->hidden_helper) {
1354 kmp_info_t *shadow_thread = __kmp_threads[KMP_GTID_TO_SHADOW_GTID(gtid)];
1355 taskdata->td_team = shadow_thread->th.th_team;
1356 taskdata->td_task_team = shadow_thread->th.th_task_team;
1360 taskdata->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1363 taskdata->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1369 taskdata->td_flags.task_serial =
1370 (parent_task->td_flags.final || taskdata->td_flags.team_serial ||
1371 taskdata->td_flags.tasking_ser || flags->merged_if0);
1373 taskdata->td_flags.started = 0;
1374 taskdata->td_flags.executing = 0;
1375 taskdata->td_flags.complete = 0;
1376 taskdata->td_flags.freed = 0;
1378 KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0);
1380 KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1);
1381 taskdata->td_taskgroup =
1382 parent_task->td_taskgroup;
1383 taskdata->td_dephash = NULL;
1384 taskdata->td_depnode = NULL;
1385 if (flags->tiedness == TASK_UNTIED)
1386 taskdata->td_last_tied = NULL;
1388 taskdata->td_last_tied = taskdata;
1389 taskdata->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1391 if (UNLIKELY(ompt_enabled.enabled))
1392 __ompt_task_init(taskdata, gtid);
1396 if (__kmp_track_children_task(taskdata)) {
1397 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
1398 if (parent_task->td_taskgroup)
1399 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
1402 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) {
1403 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
1405 if (flags->hidden_helper) {
1406 taskdata->td_flags.task_serial = FALSE;
1408 KMP_ATOMIC_INC(&__kmp_unexecuted_hidden_helper_tasks);
1412 KA_TRACE(20, (
"__kmp_task_alloc(exit): T#%d created task %p parent=%p\n",
1413 gtid, taskdata, taskdata->td_parent));
1418 kmp_task_t *__kmpc_omp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1419 kmp_int32 flags,
size_t sizeof_kmp_task_t,
1420 size_t sizeof_shareds,
1421 kmp_routine_entry_t task_entry) {
1423 kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
1424 __kmp_assert_valid_gtid(gtid);
1425 input_flags->native = FALSE;
1427 KA_TRACE(10, (
"__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s %s) "
1428 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1429 gtid, loc_ref, input_flags->tiedness ?
"tied " :
"untied",
1430 input_flags->proxy ?
"proxy" :
"",
1431 input_flags->detachable ?
"detachable" :
"", sizeof_kmp_task_t,
1432 sizeof_shareds, task_entry));
1434 retval = __kmp_task_alloc(loc_ref, gtid, input_flags, sizeof_kmp_task_t,
1435 sizeof_shareds, task_entry);
1437 KA_TRACE(20, (
"__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval));
1442 kmp_task_t *__kmpc_omp_target_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1444 size_t sizeof_kmp_task_t,
1445 size_t sizeof_shareds,
1446 kmp_routine_entry_t task_entry,
1447 kmp_int64 device_id) {
1448 auto &input_flags =
reinterpret_cast<kmp_tasking_flags_t &
>(flags);
1450 input_flags.tiedness = TASK_UNTIED;
1452 if (__kmp_enable_hidden_helper)
1453 input_flags.hidden_helper = TRUE;
1455 return __kmpc_omp_task_alloc(loc_ref, gtid, flags, sizeof_kmp_task_t,
1456 sizeof_shareds, task_entry);
1474 kmp_task_t *new_task, kmp_int32 naffins,
1475 kmp_task_affinity_info_t *affin_list) {
1484 static void __kmp_invoke_task(kmp_int32 gtid, kmp_task_t *task,
1485 kmp_taskdata_t *current_task) {
1486 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
1490 30, (
"__kmp_invoke_task(enter): T#%d invoking task %p, current_task=%p\n",
1491 gtid, taskdata, current_task));
1492 KMP_DEBUG_ASSERT(task);
1493 if (UNLIKELY(taskdata->td_flags.proxy == TASK_PROXY &&
1494 taskdata->td_flags.complete == 1)) {
1499 (
"__kmp_invoke_task: T#%d running bottom finish for proxy task %p\n",
1502 __kmp_bottom_half_finish_proxy(gtid, task);
1504 KA_TRACE(30, (
"__kmp_invoke_task(exit): T#%d completed bottom finish for "
1505 "proxy task %p, resuming task %p\n",
1506 gtid, taskdata, current_task));
1514 ompt_thread_info_t oldInfo;
1515 if (UNLIKELY(ompt_enabled.enabled)) {
1517 thread = __kmp_threads[gtid];
1518 oldInfo = thread->th.ompt_thread_info;
1519 thread->th.ompt_thread_info.wait_id = 0;
1520 thread->th.ompt_thread_info.state = (thread->th.th_team_serialized)
1521 ? ompt_state_work_serial
1522 : ompt_state_work_parallel;
1523 taskdata->ompt_task_info.frame.exit_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1528 if (taskdata->td_flags.hidden_helper) {
1530 KMP_ASSERT(KMP_HIDDEN_HELPER_THREAD(gtid));
1531 KMP_ATOMIC_DEC(&__kmp_unexecuted_hidden_helper_tasks);
1535 if (taskdata->td_flags.proxy != TASK_PROXY) {
1536 __kmp_task_start(gtid, task, current_task);
1542 if (UNLIKELY(__kmp_omp_cancellation)) {
1543 thread = __kmp_threads[gtid];
1544 kmp_team_t *this_team = thread->th.th_team;
1545 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
1546 if ((taskgroup && taskgroup->cancel_request) ||
1547 (this_team->t.t_cancel_request == cancel_parallel)) {
1548 #if OMPT_SUPPORT && OMPT_OPTIONAL
1549 ompt_data_t *task_data;
1550 if (UNLIKELY(ompt_enabled.ompt_callback_cancel)) {
1551 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, NULL, NULL);
1552 ompt_callbacks.ompt_callback(ompt_callback_cancel)(
1554 ((taskgroup && taskgroup->cancel_request) ? ompt_cancel_taskgroup
1555 : ompt_cancel_parallel) |
1556 ompt_cancel_discarded_task,
1569 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
1570 taskdata->td_last_tied = current_task->td_last_tied;
1571 KMP_DEBUG_ASSERT(taskdata->td_last_tied);
1573 #if KMP_STATS_ENABLED
1575 switch (KMP_GET_THREAD_STATE()) {
1576 case FORK_JOIN_BARRIER:
1577 KMP_PUSH_PARTITIONED_TIMER(OMP_task_join_bar);
1580 KMP_PUSH_PARTITIONED_TIMER(OMP_task_plain_bar);
1583 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskyield);
1586 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskwait);
1589 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskgroup);
1592 KMP_PUSH_PARTITIONED_TIMER(OMP_task_immediate);
1599 if (UNLIKELY(ompt_enabled.enabled))
1600 __ompt_task_start(task, current_task, gtid);
1604 if (ompd_state & OMPD_ENABLE_BP)
1605 ompd_bp_task_begin();
1608 #if USE_ITT_BUILD && USE_ITT_NOTIFY
1609 kmp_uint64 cur_time;
1610 kmp_int32 kmp_itt_count_task =
1611 __kmp_forkjoin_frames_mode == 3 && !taskdata->td_flags.task_serial &&
1612 current_task->td_flags.tasktype == TASK_IMPLICIT;
1613 if (kmp_itt_count_task) {
1614 thread = __kmp_threads[gtid];
1616 if (thread->th.th_bar_arrive_time)
1617 cur_time = __itt_get_timestamp();
1619 kmp_itt_count_task = 0;
1621 KMP_FSYNC_ACQUIRED(taskdata);
1624 #ifdef KMP_GOMP_COMPAT
1625 if (taskdata->td_flags.native) {
1626 ((void (*)(
void *))(*(task->routine)))(task->shareds);
1630 (*(task->routine))(gtid, task);
1632 KMP_POP_PARTITIONED_TIMER();
1634 #if USE_ITT_BUILD && USE_ITT_NOTIFY
1635 if (kmp_itt_count_task) {
1637 thread->th.th_bar_arrive_time += (__itt_get_timestamp() - cur_time);
1639 KMP_FSYNC_CANCEL(taskdata);
1640 KMP_FSYNC_RELEASING(taskdata->td_parent);
1645 if (ompd_state & OMPD_ENABLE_BP)
1650 if (taskdata->td_flags.proxy != TASK_PROXY) {
1652 if (UNLIKELY(ompt_enabled.enabled)) {
1653 thread->th.ompt_thread_info = oldInfo;
1654 if (taskdata->td_flags.tiedness == TASK_TIED) {
1655 taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
1657 __kmp_task_finish<true>(gtid, task, current_task);
1660 __kmp_task_finish<false>(gtid, task, current_task);
1665 (
"__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n",
1666 gtid, taskdata, current_task));
1680 kmp_int32 __kmpc_omp_task_parts(
ident_t *loc_ref, kmp_int32 gtid,
1681 kmp_task_t *new_task) {
1682 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1684 KA_TRACE(10, (
"__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n", gtid,
1685 loc_ref, new_taskdata));
1688 kmp_taskdata_t *parent;
1689 if (UNLIKELY(ompt_enabled.enabled)) {
1690 parent = new_taskdata->td_parent;
1691 if (ompt_enabled.ompt_callback_task_create) {
1692 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1693 &(parent->ompt_task_info.task_data), &(parent->ompt_task_info.frame),
1694 &(new_taskdata->ompt_task_info.task_data), ompt_task_explicit, 0,
1695 OMPT_GET_RETURN_ADDRESS(0));
1703 if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1705 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1706 new_taskdata->td_flags.task_serial = 1;
1707 __kmp_invoke_task(gtid, new_task, current_task);
1712 (
"__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: "
1713 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
1714 gtid, loc_ref, new_taskdata));
1717 if (UNLIKELY(ompt_enabled.enabled)) {
1718 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1721 return TASK_CURRENT_NOT_QUEUED;
1735 kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task,
1736 bool serialize_immediate) {
1737 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1741 if (new_taskdata->td_flags.proxy == TASK_PROXY ||
1742 __kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1744 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1745 if (serialize_immediate)
1746 new_taskdata->td_flags.task_serial = 1;
1747 __kmp_invoke_task(gtid, new_task, current_task);
1750 return TASK_CURRENT_NOT_QUEUED;
1765 kmp_int32 __kmpc_omp_task(
ident_t *loc_ref, kmp_int32 gtid,
1766 kmp_task_t *new_task) {
1768 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1770 #if KMP_DEBUG || OMPT_SUPPORT
1771 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1773 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1775 __kmp_assert_valid_gtid(gtid);
1778 kmp_taskdata_t *parent = NULL;
1779 if (UNLIKELY(ompt_enabled.enabled)) {
1780 if (!new_taskdata->td_flags.started) {
1781 OMPT_STORE_RETURN_ADDRESS(gtid);
1782 parent = new_taskdata->td_parent;
1783 if (!parent->ompt_task_info.frame.enter_frame.ptr) {
1784 parent->ompt_task_info.frame.enter_frame.ptr =
1785 OMPT_GET_FRAME_ADDRESS(0);
1787 if (ompt_enabled.ompt_callback_task_create) {
1788 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1789 &(parent->ompt_task_info.task_data),
1790 &(parent->ompt_task_info.frame),
1791 &(new_taskdata->ompt_task_info.task_data),
1792 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1793 OMPT_LOAD_RETURN_ADDRESS(gtid));
1798 __ompt_task_finish(new_task,
1799 new_taskdata->ompt_task_info.scheduling_parent,
1801 new_taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
1806 res = __kmp_omp_task(gtid, new_task,
true);
1808 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
1809 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1810 gtid, loc_ref, new_taskdata));
1812 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
1813 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1832 kmp_int32 __kmp_omp_taskloop_task(
ident_t *loc_ref, kmp_int32 gtid,
1833 kmp_task_t *new_task,
void *codeptr_ra) {
1835 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1837 #if KMP_DEBUG || OMPT_SUPPORT
1838 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1840 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1844 kmp_taskdata_t *parent = NULL;
1845 if (UNLIKELY(ompt_enabled.enabled && !new_taskdata->td_flags.started)) {
1846 parent = new_taskdata->td_parent;
1847 if (!parent->ompt_task_info.frame.enter_frame.ptr)
1848 parent->ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1849 if (ompt_enabled.ompt_callback_task_create) {
1850 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1851 &(parent->ompt_task_info.task_data), &(parent->ompt_task_info.frame),
1852 &(new_taskdata->ompt_task_info.task_data),
1853 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1859 res = __kmp_omp_task(gtid, new_task,
true);
1861 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
1862 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1863 gtid, loc_ref, new_taskdata));
1865 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
1866 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1872 template <
bool ompt>
1873 static kmp_int32 __kmpc_omp_taskwait_template(
ident_t *loc_ref, kmp_int32 gtid,
1874 void *frame_address,
1875 void *return_address) {
1876 kmp_taskdata_t *taskdata =
nullptr;
1878 int thread_finished = FALSE;
1879 KMP_SET_THREAD_STATE_BLOCK(TASKWAIT);
1881 KA_TRACE(10, (
"__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref));
1882 KMP_DEBUG_ASSERT(gtid >= 0);
1884 if (__kmp_tasking_mode != tskm_immediate_exec) {
1885 thread = __kmp_threads[gtid];
1886 taskdata = thread->th.th_current_task;
1888 #if OMPT_SUPPORT && OMPT_OPTIONAL
1889 ompt_data_t *my_task_data;
1890 ompt_data_t *my_parallel_data;
1893 my_task_data = &(taskdata->ompt_task_info.task_data);
1894 my_parallel_data = OMPT_CUR_TEAM_DATA(thread);
1896 taskdata->ompt_task_info.frame.enter_frame.ptr = frame_address;
1898 if (ompt_enabled.ompt_callback_sync_region) {
1899 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
1900 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
1901 my_task_data, return_address);
1904 if (ompt_enabled.ompt_callback_sync_region_wait) {
1905 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
1906 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
1907 my_task_data, return_address);
1917 taskdata->td_taskwait_counter += 1;
1918 taskdata->td_taskwait_ident = loc_ref;
1919 taskdata->td_taskwait_thread = gtid + 1;
1922 void *itt_sync_obj = NULL;
1924 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
1929 !taskdata->td_flags.team_serial && !taskdata->td_flags.final;
1931 must_wait = must_wait || (thread->th.th_task_team != NULL &&
1932 thread->th.th_task_team->tt.tt_found_proxy_tasks);
1936 (__kmp_enable_hidden_helper && thread->th.th_task_team != NULL &&
1937 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered);
1940 kmp_flag_32<false, false> flag(
1941 RCAST(std::atomic<kmp_uint32> *,
1942 &(taskdata->td_incomplete_child_tasks)),
1944 while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) != 0) {
1945 flag.execute_tasks(thread, gtid, FALSE,
1946 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
1947 __kmp_task_stealing_constraint);
1951 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
1952 KMP_FSYNC_ACQUIRED(taskdata);
1957 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
1959 #if OMPT_SUPPORT && OMPT_OPTIONAL
1961 if (ompt_enabled.ompt_callback_sync_region_wait) {
1962 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
1963 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
1964 my_task_data, return_address);
1966 if (ompt_enabled.ompt_callback_sync_region) {
1967 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
1968 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
1969 my_task_data, return_address);
1971 taskdata->ompt_task_info.frame.enter_frame = ompt_data_none;
1977 KA_TRACE(10, (
"__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, "
1978 "returning TASK_CURRENT_NOT_QUEUED\n",
1981 return TASK_CURRENT_NOT_QUEUED;
1984 #if OMPT_SUPPORT && OMPT_OPTIONAL
1986 static kmp_int32 __kmpc_omp_taskwait_ompt(
ident_t *loc_ref, kmp_int32 gtid,
1987 void *frame_address,
1988 void *return_address) {
1989 return __kmpc_omp_taskwait_template<true>(loc_ref, gtid, frame_address,
1996 kmp_int32 __kmpc_omp_taskwait(
ident_t *loc_ref, kmp_int32 gtid) {
1997 #if OMPT_SUPPORT && OMPT_OPTIONAL
1998 if (UNLIKELY(ompt_enabled.enabled)) {
1999 OMPT_STORE_RETURN_ADDRESS(gtid);
2000 return __kmpc_omp_taskwait_ompt(loc_ref, gtid, OMPT_GET_FRAME_ADDRESS(0),
2001 OMPT_LOAD_RETURN_ADDRESS(gtid));
2004 return __kmpc_omp_taskwait_template<false>(loc_ref, gtid, NULL, NULL);
2008 kmp_int32 __kmpc_omp_taskyield(
ident_t *loc_ref, kmp_int32 gtid,
int end_part) {
2009 kmp_taskdata_t *taskdata = NULL;
2011 int thread_finished = FALSE;
2014 KMP_SET_THREAD_STATE_BLOCK(TASKYIELD);
2016 KA_TRACE(10, (
"__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n",
2017 gtid, loc_ref, end_part));
2018 __kmp_assert_valid_gtid(gtid);
2020 if (__kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel) {
2021 thread = __kmp_threads[gtid];
2022 taskdata = thread->th.th_current_task;
2029 taskdata->td_taskwait_counter += 1;
2030 taskdata->td_taskwait_ident = loc_ref;
2031 taskdata->td_taskwait_thread = gtid + 1;
2034 void *itt_sync_obj = NULL;
2036 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2039 if (!taskdata->td_flags.team_serial) {
2040 kmp_task_team_t *task_team = thread->th.th_task_team;
2041 if (task_team != NULL) {
2042 if (KMP_TASKING_ENABLED(task_team)) {
2044 if (UNLIKELY(ompt_enabled.enabled))
2045 thread->th.ompt_thread_info.ompt_task_yielded = 1;
2047 __kmp_execute_tasks_32(
2048 thread, gtid, (kmp_flag_32<> *)NULL, FALSE,
2049 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2050 __kmp_task_stealing_constraint);
2052 if (UNLIKELY(ompt_enabled.enabled))
2053 thread->th.ompt_thread_info.ompt_task_yielded = 0;
2059 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2064 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2067 KA_TRACE(10, (
"__kmpc_omp_taskyield(exit): T#%d task %p resuming, "
2068 "returning TASK_CURRENT_NOT_QUEUED\n",
2071 return TASK_CURRENT_NOT_QUEUED;
2092 unsigned reserved31 : 31;
2172 template <
typename T>
2173 void *__kmp_task_reduction_init(
int gtid,
int num, T *data) {
2174 __kmp_assert_valid_gtid(gtid);
2175 kmp_info_t *thread = __kmp_threads[gtid];
2176 kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup;
2177 kmp_uint32 nth = thread->th.th_team_nproc;
2181 KMP_ASSERT(tg != NULL);
2182 KMP_ASSERT(data != NULL);
2183 KMP_ASSERT(num > 0);
2185 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n",
2189 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, taskgroup %p, #items %d\n",
2193 for (
int i = 0; i < num; ++i) {
2194 size_t size = data[i].reduce_size - 1;
2196 size += CACHE_LINE - size % CACHE_LINE;
2197 KMP_ASSERT(data[i].reduce_comb != NULL);
2200 arr[i].
flags = data[i].flags;
2204 __kmp_assign_orig<T>(arr[i], data[i]);
2205 if (!arr[i].flags.lazy_priv) {
2208 arr[i].
reduce_pend = (
char *)(arr[i].reduce_priv) + nth * size;
2209 if (arr[i].reduce_init != NULL) {
2211 for (
size_t j = 0; j < nth; ++j) {
2212 __kmp_call_init<T>(arr[i], j * size);
2219 arr[i].
reduce_priv = __kmp_allocate(nth *
sizeof(
void *));
2222 tg->reduce_data = (
void *)arr;
2223 tg->reduce_num_data = num;
2262 template <
typename T>
2263 void __kmp_task_reduction_init_copy(kmp_info_t *thr,
int num, T *data,
2264 kmp_taskgroup_t *tg,
void *reduce_data) {
2266 KA_TRACE(20, (
"__kmp_task_reduction_init_copy: Th %p, init taskgroup %p,"
2268 thr, tg, reduce_data));
2273 for (
int i = 0; i < num; ++i) {
2276 tg->reduce_data = (
void *)arr;
2277 tg->reduce_num_data = num;
2290 __kmp_assert_valid_gtid(gtid);
2291 kmp_info_t *thread = __kmp_threads[gtid];
2292 kmp_int32 nth = thread->th.th_team_nproc;
2296 kmp_taskgroup_t *tg = (kmp_taskgroup_t *)tskgrp;
2298 tg = thread->th.th_current_task->td_taskgroup;
2299 KMP_ASSERT(tg != NULL);
2301 kmp_int32 num = tg->reduce_num_data;
2302 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
2304 KMP_ASSERT(data != NULL);
2305 while (tg != NULL) {
2306 for (
int i = 0; i < num; ++i) {
2307 if (!arr[i].flags.lazy_priv) {
2308 if (data == arr[i].reduce_shar ||
2309 (data >= arr[i].reduce_priv && data < arr[i].reduce_pend))
2310 return (
char *)(arr[i].
reduce_priv) + tid * arr[i].reduce_size;
2313 void **p_priv = (
void **)(arr[i].reduce_priv);
2314 if (data == arr[i].reduce_shar)
2317 for (
int j = 0; j < nth; ++j)
2318 if (data == p_priv[j])
2322 if (p_priv[tid] == NULL) {
2324 p_priv[tid] = __kmp_allocate(arr[i].reduce_size);
2325 if (arr[i].reduce_init != NULL) {
2326 if (arr[i].reduce_orig != NULL) {
2328 p_priv[tid], arr[i].reduce_orig);
2330 ((void (*)(
void *))arr[i].
reduce_init)(p_priv[tid]);
2339 num = tg->reduce_num_data;
2341 KMP_ASSERT2(0,
"Unknown task reduction item");
2347 static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg) {
2348 kmp_int32 nth = th->th.th_team_nproc;
2349 KMP_DEBUG_ASSERT(nth > 1);
2351 kmp_int32 num = tg->reduce_num_data;
2352 for (
int i = 0; i < num; ++i) {
2354 void (*f_fini)(
void *) = (
void (*)(
void *))(arr[i].
reduce_fini);
2355 void (*f_comb)(
void *,
void *) =
2357 if (!arr[i].flags.lazy_priv) {
2360 for (
int j = 0; j < nth; ++j) {
2361 void *priv_data = (
char *)pr_data + j * size;
2362 f_comb(sh_data, priv_data);
2367 void **pr_data = (
void **)(arr[i].reduce_priv);
2368 for (
int j = 0; j < nth; ++j) {
2369 if (pr_data[j] != NULL) {
2370 f_comb(sh_data, pr_data[j]);
2373 __kmp_free(pr_data[j]);
2377 __kmp_free(arr[i].reduce_priv);
2379 __kmp_thread_free(th, arr);
2380 tg->reduce_data = NULL;
2381 tg->reduce_num_data = 0;
2387 static void __kmp_task_reduction_clean(kmp_info_t *th, kmp_taskgroup_t *tg) {
2388 __kmp_thread_free(th, tg->reduce_data);
2389 tg->reduce_data = NULL;
2390 tg->reduce_num_data = 0;
2393 template <
typename T>
2394 void *__kmp_task_reduction_modifier_init(
ident_t *loc,
int gtid,
int is_ws,
2396 __kmp_assert_valid_gtid(gtid);
2397 kmp_info_t *thr = __kmp_threads[gtid];
2398 kmp_int32 nth = thr->th.th_team_nproc;
2399 __kmpc_taskgroup(loc, gtid);
2402 (
"__kmpc_reduction_modifier_init: T#%d, tg %p, exiting nth=1\n",
2403 gtid, thr->th.th_current_task->td_taskgroup));
2404 return (
void *)thr->th.th_current_task->td_taskgroup;
2406 kmp_team_t *team = thr->th.th_team;
2408 kmp_taskgroup_t *tg;
2409 reduce_data = KMP_ATOMIC_LD_RLX(&team->t.t_tg_reduce_data[is_ws]);
2410 if (reduce_data == NULL &&
2411 __kmp_atomic_compare_store(&team->t.t_tg_reduce_data[is_ws], reduce_data,
2414 KMP_DEBUG_ASSERT(reduce_data == NULL);
2416 tg = (kmp_taskgroup_t *)__kmp_task_reduction_init<T>(gtid, num, data);
2420 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[0]) == 0);
2421 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[1]) == 0);
2422 KMP_ATOMIC_ST_REL(&team->t.t_tg_reduce_data[is_ws], reduce_data);
2425 (reduce_data = KMP_ATOMIC_LD_ACQ(&team->t.t_tg_reduce_data[is_ws])) ==
2429 KMP_DEBUG_ASSERT(reduce_data > (
void *)1);
2430 tg = thr->th.th_current_task->td_taskgroup;
2431 __kmp_task_reduction_init_copy<T>(thr, num, data, tg, reduce_data);
2453 int num,
void *data) {
2454 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2474 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2487 __kmpc_end_taskgroup(loc, gtid);
2491 void __kmpc_taskgroup(
ident_t *loc,
int gtid) {
2492 __kmp_assert_valid_gtid(gtid);
2493 kmp_info_t *thread = __kmp_threads[gtid];
2494 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2495 kmp_taskgroup_t *tg_new =
2496 (kmp_taskgroup_t *)__kmp_thread_malloc(thread,
sizeof(kmp_taskgroup_t));
2497 KA_TRACE(10, (
"__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new));
2498 KMP_ATOMIC_ST_RLX(&tg_new->count, 0);
2499 KMP_ATOMIC_ST_RLX(&tg_new->cancel_request, cancel_noreq);
2500 tg_new->parent = taskdata->td_taskgroup;
2501 tg_new->reduce_data = NULL;
2502 tg_new->reduce_num_data = 0;
2503 tg_new->gomp_data = NULL;
2504 taskdata->td_taskgroup = tg_new;
2506 #if OMPT_SUPPORT && OMPT_OPTIONAL
2507 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2508 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2510 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2511 kmp_team_t *team = thread->th.th_team;
2512 ompt_data_t my_task_data = taskdata->ompt_task_info.task_data;
2514 ompt_data_t my_parallel_data = team->t.ompt_team_info.parallel_data;
2516 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2517 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2518 &(my_task_data), codeptr);
2525 void __kmpc_end_taskgroup(
ident_t *loc,
int gtid) {
2526 __kmp_assert_valid_gtid(gtid);
2527 kmp_info_t *thread = __kmp_threads[gtid];
2528 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2529 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
2530 int thread_finished = FALSE;
2532 #if OMPT_SUPPORT && OMPT_OPTIONAL
2534 ompt_data_t my_task_data;
2535 ompt_data_t my_parallel_data;
2536 void *codeptr =
nullptr;
2537 if (UNLIKELY(ompt_enabled.enabled)) {
2538 team = thread->th.th_team;
2539 my_task_data = taskdata->ompt_task_info.task_data;
2541 my_parallel_data = team->t.ompt_team_info.parallel_data;
2542 codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2544 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2548 KA_TRACE(10, (
"__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc));
2549 KMP_DEBUG_ASSERT(taskgroup != NULL);
2550 KMP_SET_THREAD_STATE_BLOCK(TASKGROUP);
2552 if (__kmp_tasking_mode != tskm_immediate_exec) {
2554 taskdata->td_taskwait_counter += 1;
2555 taskdata->td_taskwait_ident = loc;
2556 taskdata->td_taskwait_thread = gtid + 1;
2560 void *itt_sync_obj = NULL;
2562 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2566 #if OMPT_SUPPORT && OMPT_OPTIONAL
2567 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2568 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2569 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2570 &(my_task_data), codeptr);
2574 if (!taskdata->td_flags.team_serial ||
2575 (thread->th.th_task_team != NULL &&
2576 (thread->th.th_task_team->tt.tt_found_proxy_tasks ||
2577 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered))) {
2578 kmp_flag_32<false, false> flag(
2579 RCAST(std::atomic<kmp_uint32> *, &(taskgroup->count)), 0U);
2580 while (KMP_ATOMIC_LD_ACQ(&taskgroup->count) != 0) {
2581 flag.execute_tasks(thread, gtid, FALSE,
2582 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2583 __kmp_task_stealing_constraint);
2586 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2588 #if OMPT_SUPPORT && OMPT_OPTIONAL
2589 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2590 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2591 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2592 &(my_task_data), codeptr);
2597 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2598 KMP_FSYNC_ACQUIRED(taskdata);
2601 KMP_DEBUG_ASSERT(taskgroup->count == 0);
2603 if (taskgroup->reduce_data != NULL &&
2604 !taskgroup->gomp_data) {
2607 kmp_team_t *t = thread->th.th_team;
2611 if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[0])) != NULL &&
2614 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[0]);
2615 if (cnt == thread->th.th_team_nproc - 1) {
2618 __kmp_task_reduction_fini(thread, taskgroup);
2621 __kmp_thread_free(thread, reduce_data);
2622 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[0], NULL);
2623 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[0], 0);
2627 __kmp_task_reduction_clean(thread, taskgroup);
2629 }
else if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[1])) !=
2633 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[1]);
2634 if (cnt == thread->th.th_team_nproc - 1) {
2636 __kmp_task_reduction_fini(thread, taskgroup);
2639 __kmp_thread_free(thread, reduce_data);
2640 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[1], NULL);
2641 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[1], 0);
2645 __kmp_task_reduction_clean(thread, taskgroup);
2649 __kmp_task_reduction_fini(thread, taskgroup);
2653 taskdata->td_taskgroup = taskgroup->parent;
2654 __kmp_thread_free(thread, taskgroup);
2656 KA_TRACE(10, (
"__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n",
2659 #if OMPT_SUPPORT && OMPT_OPTIONAL
2660 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2661 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2662 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2663 &(my_task_data), codeptr);
2669 static kmp_task_t *__kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid,
2670 kmp_task_team_t *task_team,
2671 kmp_int32 is_constrained) {
2673 kmp_taskdata_t *taskdata;
2674 kmp_thread_data_t *thread_data;
2677 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2678 KMP_DEBUG_ASSERT(task_team->tt.tt_threads_data !=
2681 thread_data = &task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
2683 KA_TRACE(10, (
"__kmp_remove_my_task(enter): T#%d ntasks=%d head=%u tail=%u\n",
2684 gtid, thread_data->td.td_deque_ntasks,
2685 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2687 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2689 (
"__kmp_remove_my_task(exit #1): T#%d No tasks to remove: "
2690 "ntasks=%d head=%u tail=%u\n",
2691 gtid, thread_data->td.td_deque_ntasks,
2692 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2696 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
2698 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2699 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2701 (
"__kmp_remove_my_task(exit #2): T#%d No tasks to remove: "
2702 "ntasks=%d head=%u tail=%u\n",
2703 gtid, thread_data->td.td_deque_ntasks,
2704 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2708 tail = (thread_data->td.td_deque_tail - 1) &
2709 TASK_DEQUE_MASK(thread_data->td);
2710 taskdata = thread_data->td.td_deque[tail];
2712 if (!__kmp_task_is_allowed(gtid, is_constrained, taskdata,
2713 thread->th.th_current_task)) {
2715 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2717 (
"__kmp_remove_my_task(exit #3): T#%d TSC blocks tail task: "
2718 "ntasks=%d head=%u tail=%u\n",
2719 gtid, thread_data->td.td_deque_ntasks,
2720 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2724 thread_data->td.td_deque_tail = tail;
2725 TCW_4(thread_data->td.td_deque_ntasks, thread_data->td.td_deque_ntasks - 1);
2727 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2729 KA_TRACE(10, (
"__kmp_remove_my_task(exit #4): T#%d task %p removed: "
2730 "ntasks=%d head=%u tail=%u\n",
2731 gtid, taskdata, thread_data->td.td_deque_ntasks,
2732 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2734 task = KMP_TASKDATA_TO_TASK(taskdata);
2741 static kmp_task_t *__kmp_steal_task(kmp_info_t *victim_thr, kmp_int32 gtid,
2742 kmp_task_team_t *task_team,
2743 std::atomic<kmp_int32> *unfinished_threads,
2744 int *thread_finished,
2745 kmp_int32 is_constrained) {
2747 kmp_taskdata_t *taskdata;
2748 kmp_taskdata_t *current;
2749 kmp_thread_data_t *victim_td, *threads_data;
2751 kmp_int32 victim_tid;
2753 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2755 threads_data = task_team->tt.tt_threads_data;
2756 KMP_DEBUG_ASSERT(threads_data != NULL);
2758 victim_tid = victim_thr->th.th_info.ds.ds_tid;
2759 victim_td = &threads_data[victim_tid];
2761 KA_TRACE(10, (
"__kmp_steal_task(enter): T#%d try to steal from T#%d: "
2762 "task_team=%p ntasks=%d head=%u tail=%u\n",
2763 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
2764 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
2765 victim_td->td.td_deque_tail));
2767 if (TCR_4(victim_td->td.td_deque_ntasks) == 0) {
2768 KA_TRACE(10, (
"__kmp_steal_task(exit #1): T#%d could not steal from T#%d: "
2769 "task_team=%p ntasks=%d head=%u tail=%u\n",
2770 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
2771 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
2772 victim_td->td.td_deque_tail));
2776 __kmp_acquire_bootstrap_lock(&victim_td->td.td_deque_lock);
2778 int ntasks = TCR_4(victim_td->td.td_deque_ntasks);
2781 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2782 KA_TRACE(10, (
"__kmp_steal_task(exit #2): T#%d could not steal from T#%d: "
2783 "task_team=%p ntasks=%d head=%u tail=%u\n",
2784 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2785 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2789 KMP_DEBUG_ASSERT(victim_td->td.td_deque != NULL);
2790 current = __kmp_threads[gtid]->th.th_current_task;
2791 taskdata = victim_td->td.td_deque[victim_td->td.td_deque_head];
2792 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
2794 victim_td->td.td_deque_head =
2795 (victim_td->td.td_deque_head + 1) & TASK_DEQUE_MASK(victim_td->td);
2797 if (!task_team->tt.tt_untied_task_encountered) {
2799 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2800 KA_TRACE(10, (
"__kmp_steal_task(exit #3): T#%d could not steal from "
2801 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
2802 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2803 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2808 target = victim_td->td.td_deque_head;
2810 for (i = 1; i < ntasks; ++i) {
2811 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
2812 taskdata = victim_td->td.td_deque[target];
2813 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
2819 if (taskdata == NULL) {
2821 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2822 KA_TRACE(10, (
"__kmp_steal_task(exit #4): T#%d could not steal from "
2823 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
2824 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2825 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2829 for (i = i + 1; i < ntasks; ++i) {
2831 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
2832 victim_td->td.td_deque[prev] = victim_td->td.td_deque[target];
2836 victim_td->td.td_deque_tail ==
2837 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(victim_td->td)));
2838 victim_td->td.td_deque_tail = target;
2840 if (*thread_finished) {
2847 KMP_ATOMIC_INC(unfinished_threads);
2850 (
"__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n",
2851 gtid, count + 1, task_team));
2852 *thread_finished = FALSE;
2854 TCW_4(victim_td->td.td_deque_ntasks, ntasks - 1);
2856 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2860 (
"__kmp_steal_task(exit #5): T#%d stole task %p from T#%d: "
2861 "task_team=%p ntasks=%d head=%u tail=%u\n",
2862 gtid, taskdata, __kmp_gtid_from_thread(victim_thr), task_team,
2863 ntasks, victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2865 task = KMP_TASKDATA_TO_TASK(taskdata);
2879 static inline int __kmp_execute_tasks_template(
2880 kmp_info_t *thread, kmp_int32 gtid, C *flag,
int final_spin,
2881 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
2882 kmp_int32 is_constrained) {
2883 kmp_task_team_t *task_team = thread->th.th_task_team;
2884 kmp_thread_data_t *threads_data;
2886 kmp_info_t *other_thread;
2887 kmp_taskdata_t *current_task = thread->th.th_current_task;
2888 std::atomic<kmp_int32> *unfinished_threads;
2889 kmp_int32 nthreads, victim_tid = -2, use_own_tasks = 1, new_victim = 0,
2890 tid = thread->th.th_info.ds.ds_tid;
2892 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2893 KMP_DEBUG_ASSERT(thread == __kmp_threads[gtid]);
2895 if (task_team == NULL || current_task == NULL)
2898 KA_TRACE(15, (
"__kmp_execute_tasks_template(enter): T#%d final_spin=%d "
2899 "*thread_finished=%d\n",
2900 gtid, final_spin, *thread_finished));
2902 thread->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
2903 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
2905 KMP_DEBUG_ASSERT(threads_data != NULL);
2907 nthreads = task_team->tt.tt_nproc;
2908 unfinished_threads = &(task_team->tt.tt_unfinished_threads);
2909 KMP_DEBUG_ASSERT(nthreads > 1 || task_team->tt.tt_found_proxy_tasks ||
2910 task_team->tt.tt_hidden_helper_task_encountered);
2911 KMP_DEBUG_ASSERT(*unfinished_threads >= 0);
2917 if (use_own_tasks) {
2918 task = __kmp_remove_my_task(thread, gtid, task_team, is_constrained);
2920 if ((task == NULL) && (nthreads > 1)) {
2924 if (victim_tid == -2) {
2925 victim_tid = threads_data[tid].td.td_deque_last_stolen;
2928 other_thread = threads_data[victim_tid].td.td_thr;
2930 if (victim_tid != -1) {
2932 }
else if (!new_victim) {
2938 victim_tid = __kmp_get_random(thread) % (nthreads - 1);
2939 if (victim_tid >= tid) {
2943 other_thread = threads_data[victim_tid].td.td_thr;
2953 if ((__kmp_tasking_mode == tskm_task_teams) &&
2954 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) &&
2955 (TCR_PTR(CCAST(
void *, other_thread->th.th_sleep_loc)) !=
2958 __kmp_null_resume_wrapper(other_thread);
2971 task = __kmp_steal_task(other_thread, gtid, task_team,
2972 unfinished_threads, thread_finished,
2976 if (threads_data[tid].td.td_deque_last_stolen != victim_tid) {
2977 threads_data[tid].td.td_deque_last_stolen = victim_tid;
2984 KMP_CHECK_UPDATE(threads_data[tid].td.td_deque_last_stolen, -1);
2993 #if USE_ITT_BUILD && USE_ITT_NOTIFY
2994 if (__itt_sync_create_ptr || KMP_ITT_DEBUG) {
2995 if (itt_sync_obj == NULL) {
2997 itt_sync_obj = __kmp_itt_barrier_object(gtid, bs_forkjoin_barrier);
2999 __kmp_itt_task_starting(itt_sync_obj);
3002 __kmp_invoke_task(gtid, task, current_task);
3004 if (itt_sync_obj != NULL)
3005 __kmp_itt_task_finished(itt_sync_obj);
3012 if (flag == NULL || (!final_spin && flag->done_check())) {
3015 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3019 if (thread->th.th_task_team == NULL) {
3022 KMP_YIELD(__kmp_library == library_throughput);
3025 if (!use_own_tasks && TCR_4(threads_data[tid].td.td_deque_ntasks) != 0) {
3026 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d stolen task spawned "
3027 "other tasks, restart\n",
3038 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks) == 0) {
3042 if (!*thread_finished) {
3044 kmp_int32 count = -1 +
3046 KMP_ATOMIC_DEC(unfinished_threads);
3047 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d dec "
3048 "unfinished_threads to %d task_team=%p\n",
3049 gtid, count, task_team));
3050 *thread_finished = TRUE;
3058 if (flag != NULL && flag->done_check()) {
3061 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3069 if (thread->th.th_task_team == NULL) {
3071 (
"__kmp_execute_tasks_template: T#%d no more tasks\n", gtid));
3077 if (nthreads == 1 &&
3078 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks))
3082 (
"__kmp_execute_tasks_template: T#%d can't find work\n", gtid));
3088 template <
bool C,
bool S>
3089 int __kmp_execute_tasks_32(
3090 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32<C, S> *flag,
int final_spin,
3091 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3092 kmp_int32 is_constrained) {
3093 return __kmp_execute_tasks_template(
3094 thread, gtid, flag, final_spin,
3095 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3098 template <
bool C,
bool S>
3099 int __kmp_execute_tasks_64(
3100 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64<C, S> *flag,
int final_spin,
3101 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3102 kmp_int32 is_constrained) {
3103 return __kmp_execute_tasks_template(
3104 thread, gtid, flag, final_spin,
3105 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3108 template <
bool C,
bool S>
3109 int __kmp_atomic_execute_tasks_64(
3110 kmp_info_t *thread, kmp_int32 gtid, kmp_atomic_flag_64<C, S> *flag,
3111 int final_spin,
int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3112 kmp_int32 is_constrained) {
3113 return __kmp_execute_tasks_template(
3114 thread, gtid, flag, final_spin,
3115 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3118 int __kmp_execute_tasks_oncore(
3119 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag,
int final_spin,
3120 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3121 kmp_int32 is_constrained) {
3122 return __kmp_execute_tasks_template(
3123 thread, gtid, flag, final_spin,
3124 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3128 __kmp_execute_tasks_32<false, false>(kmp_info_t *, kmp_int32,
3129 kmp_flag_32<false, false> *,
int,
3130 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3132 template int __kmp_execute_tasks_64<false, true>(kmp_info_t *, kmp_int32,
3133 kmp_flag_64<false, true> *,
3135 int *USE_ITT_BUILD_ARG(
void *),
3138 template int __kmp_execute_tasks_64<true, false>(kmp_info_t *, kmp_int32,
3139 kmp_flag_64<true, false> *,
3141 int *USE_ITT_BUILD_ARG(
void *),
3144 template int __kmp_atomic_execute_tasks_64<false, true>(
3145 kmp_info_t *, kmp_int32, kmp_atomic_flag_64<false, true> *,
int,
3146 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3148 template int __kmp_atomic_execute_tasks_64<true, false>(
3149 kmp_info_t *, kmp_int32, kmp_atomic_flag_64<true, false> *,
int,
3150 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3155 static void __kmp_enable_tasking(kmp_task_team_t *task_team,
3156 kmp_info_t *this_thr) {
3157 kmp_thread_data_t *threads_data;
3158 int nthreads, i, is_init_thread;
3160 KA_TRACE(10, (
"__kmp_enable_tasking(enter): T#%d\n",
3161 __kmp_gtid_from_thread(this_thr)));
3163 KMP_DEBUG_ASSERT(task_team != NULL);
3164 KMP_DEBUG_ASSERT(this_thr->th.th_team != NULL);
3166 nthreads = task_team->tt.tt_nproc;
3167 KMP_DEBUG_ASSERT(nthreads > 0);
3168 KMP_DEBUG_ASSERT(nthreads == this_thr->th.th_team->t.t_nproc);
3171 is_init_thread = __kmp_realloc_task_threads_data(this_thr, task_team);
3173 if (!is_init_thread) {
3177 (
"__kmp_enable_tasking(exit): T#%d: threads array already set up.\n",
3178 __kmp_gtid_from_thread(this_thr)));
3181 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
3182 KMP_DEBUG_ASSERT(threads_data != NULL);
3184 if (__kmp_tasking_mode == tskm_task_teams &&
3185 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME)) {
3189 for (i = 0; i < nthreads; i++) {
3191 kmp_info_t *thread = threads_data[i].td.td_thr;
3193 if (i == this_thr->th.th_info.ds.ds_tid) {
3202 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3204 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d waking up thread T#%d\n",
3205 __kmp_gtid_from_thread(this_thr),
3206 __kmp_gtid_from_thread(thread)));
3207 __kmp_null_resume_wrapper(thread);
3209 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d don't wake up thread T#%d\n",
3210 __kmp_gtid_from_thread(this_thr),
3211 __kmp_gtid_from_thread(thread)));
3216 KA_TRACE(10, (
"__kmp_enable_tasking(exit): T#%d\n",
3217 __kmp_gtid_from_thread(this_thr)));
3250 static kmp_task_team_t *__kmp_free_task_teams =
3253 kmp_bootstrap_lock_t __kmp_task_team_lock =
3254 KMP_BOOTSTRAP_LOCK_INITIALIZER(__kmp_task_team_lock);
3261 static void __kmp_alloc_task_deque(kmp_info_t *thread,
3262 kmp_thread_data_t *thread_data) {
3263 __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
3264 KMP_DEBUG_ASSERT(thread_data->td.td_deque == NULL);
3267 thread_data->td.td_deque_last_stolen = -1;
3269 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == 0);
3270 KMP_DEBUG_ASSERT(thread_data->td.td_deque_head == 0);
3271 KMP_DEBUG_ASSERT(thread_data->td.td_deque_tail == 0);
3275 (
"__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n",
3276 __kmp_gtid_from_thread(thread), INITIAL_TASK_DEQUE_SIZE, thread_data));
3280 thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
3281 INITIAL_TASK_DEQUE_SIZE *
sizeof(kmp_taskdata_t *));
3282 thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
3288 static void __kmp_free_task_deque(kmp_thread_data_t *thread_data) {
3289 if (thread_data->td.td_deque != NULL) {
3290 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3291 TCW_4(thread_data->td.td_deque_ntasks, 0);
3292 __kmp_free(thread_data->td.td_deque);
3293 thread_data->td.td_deque = NULL;
3294 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3297 #ifdef BUILD_TIED_TASK_STACK
3299 if (thread_data->td.td_susp_tied_tasks.ts_entries != TASK_STACK_EMPTY) {
3300 __kmp_free_task_stack(__kmp_thread_from_gtid(gtid), thread_data);
3312 static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
3313 kmp_task_team_t *task_team) {
3314 kmp_thread_data_t **threads_data_p;
3315 kmp_int32 nthreads, maxthreads;
3316 int is_init_thread = FALSE;
3318 if (TCR_4(task_team->tt.tt_found_tasks)) {
3323 threads_data_p = &task_team->tt.tt_threads_data;
3324 nthreads = task_team->tt.tt_nproc;
3325 maxthreads = task_team->tt.tt_max_threads;
3330 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3332 if (!TCR_4(task_team->tt.tt_found_tasks)) {
3334 kmp_team_t *team = thread->th.th_team;
3337 is_init_thread = TRUE;
3338 if (maxthreads < nthreads) {
3340 if (*threads_data_p != NULL) {
3341 kmp_thread_data_t *old_data = *threads_data_p;
3342 kmp_thread_data_t *new_data = NULL;
3346 (
"__kmp_realloc_task_threads_data: T#%d reallocating "
3347 "threads data for task_team %p, new_size = %d, old_size = %d\n",
3348 __kmp_gtid_from_thread(thread), task_team, nthreads, maxthreads));
3353 new_data = (kmp_thread_data_t *)__kmp_allocate(
3354 nthreads *
sizeof(kmp_thread_data_t));
3356 KMP_MEMCPY_S((
void *)new_data, nthreads *
sizeof(kmp_thread_data_t),
3357 (
void *)old_data, maxthreads *
sizeof(kmp_thread_data_t));
3359 #ifdef BUILD_TIED_TASK_STACK
3361 for (i = maxthreads; i < nthreads; i++) {
3362 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3363 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3367 (*threads_data_p) = new_data;
3368 __kmp_free(old_data);
3370 KE_TRACE(10, (
"__kmp_realloc_task_threads_data: T#%d allocating "
3371 "threads data for task_team %p, size = %d\n",
3372 __kmp_gtid_from_thread(thread), task_team, nthreads));
3376 *threads_data_p = (kmp_thread_data_t *)__kmp_allocate(
3377 nthreads *
sizeof(kmp_thread_data_t));
3378 #ifdef BUILD_TIED_TASK_STACK
3380 for (i = 0; i < nthreads; i++) {
3381 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3382 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3386 task_team->tt.tt_max_threads = nthreads;
3389 KMP_DEBUG_ASSERT(*threads_data_p != NULL);
3393 for (i = 0; i < nthreads; i++) {
3394 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3395 thread_data->td.td_thr = team->t.t_threads[i];
3397 if (thread_data->td.td_deque_last_stolen >= nthreads) {
3401 thread_data->td.td_deque_last_stolen = -1;
3406 TCW_SYNC_4(task_team->tt.tt_found_tasks, TRUE);
3409 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3410 return is_init_thread;
3416 static void __kmp_free_task_threads_data(kmp_task_team_t *task_team) {
3417 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3418 if (task_team->tt.tt_threads_data != NULL) {
3420 for (i = 0; i < task_team->tt.tt_max_threads; i++) {
3421 __kmp_free_task_deque(&task_team->tt.tt_threads_data[i]);
3423 __kmp_free(task_team->tt.tt_threads_data);
3424 task_team->tt.tt_threads_data = NULL;
3426 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3433 static kmp_task_team_t *__kmp_allocate_task_team(kmp_info_t *thread,
3435 kmp_task_team_t *task_team = NULL;
3438 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d entering; team = %p\n",
3439 (thread ? __kmp_gtid_from_thread(thread) : -1), team));
3441 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3443 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3444 if (__kmp_free_task_teams != NULL) {
3445 task_team = __kmp_free_task_teams;
3446 TCW_PTR(__kmp_free_task_teams, task_team->tt.tt_next);
3447 task_team->tt.tt_next = NULL;
3449 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3452 if (task_team == NULL) {
3453 KE_TRACE(10, (
"__kmp_allocate_task_team: T#%d allocating "
3454 "task team for team %p\n",
3455 __kmp_gtid_from_thread(thread), team));
3458 task_team = (kmp_task_team_t *)__kmp_allocate(
sizeof(kmp_task_team_t));
3459 __kmp_init_bootstrap_lock(&task_team->tt.tt_threads_lock);
3460 #if USE_ITT_BUILD && USE_ITT_NOTIFY && KMP_DEBUG
3463 __itt_suppress_mark_range(
3464 __itt_suppress_range, __itt_suppress_threading_errors,
3465 &task_team->tt.tt_found_tasks,
sizeof(task_team->tt.tt_found_tasks));
3466 __itt_suppress_mark_range(__itt_suppress_range,
3467 __itt_suppress_threading_errors,
3468 CCAST(kmp_uint32 *, &task_team->tt.tt_active),
3469 sizeof(task_team->tt.tt_active));
3477 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3478 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3479 task_team->tt.tt_nproc = nthreads = team->t.t_nproc;
3481 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads, nthreads);
3482 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
3483 TCW_4(task_team->tt.tt_active, TRUE);
3485 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d exiting; task_team = %p "
3486 "unfinished_threads init'd to %d\n",
3487 (thread ? __kmp_gtid_from_thread(thread) : -1), task_team,
3488 KMP_ATOMIC_LD_RLX(&task_team->tt.tt_unfinished_threads)));
3495 void __kmp_free_task_team(kmp_info_t *thread, kmp_task_team_t *task_team) {
3496 KA_TRACE(20, (
"__kmp_free_task_team: T#%d task_team = %p\n",
3497 thread ? __kmp_gtid_from_thread(thread) : -1, task_team));
3500 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3502 KMP_DEBUG_ASSERT(task_team->tt.tt_next == NULL);
3503 task_team->tt.tt_next = __kmp_free_task_teams;
3504 TCW_PTR(__kmp_free_task_teams, task_team);
3506 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3514 void __kmp_reap_task_teams(
void) {
3515 kmp_task_team_t *task_team;
3517 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3519 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3520 while ((task_team = __kmp_free_task_teams) != NULL) {
3521 __kmp_free_task_teams = task_team->tt.tt_next;
3522 task_team->tt.tt_next = NULL;
3525 if (task_team->tt.tt_threads_data != NULL) {
3526 __kmp_free_task_threads_data(task_team);
3528 __kmp_free(task_team);
3530 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3537 void __kmp_wait_to_unref_task_teams(
void) {
3542 KMP_INIT_YIELD(spins);
3550 for (thread = CCAST(kmp_info_t *, __kmp_thread_pool); thread != NULL;
3551 thread = thread->th.th_next_pool) {
3555 if (TCR_PTR(thread->th.th_task_team) == NULL) {
3556 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n",
3557 __kmp_gtid_from_thread(thread)));
3562 if (!__kmp_is_thread_alive(thread, &exit_val)) {
3563 thread->th.th_task_team = NULL;
3570 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: Waiting for T#%d to "
3571 "unreference task_team\n",
3572 __kmp_gtid_from_thread(thread)));
3574 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
3577 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3581 (
"__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n",
3582 __kmp_gtid_from_thread(thread), __kmp_gtid_from_thread(thread)));
3583 __kmp_null_resume_wrapper(thread);
3592 KMP_YIELD_OVERSUB_ELSE_SPIN(spins);
3598 void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team,
int always) {
3599 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3605 if (team->t.t_task_team[this_thr->th.th_task_state] == NULL &&
3606 (always || team->t.t_nproc > 1)) {
3607 team->t.t_task_team[this_thr->th.th_task_state] =
3608 __kmp_allocate_task_team(this_thr, team);
3609 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d created new task_team %p"
3610 " for team %d at parity=%d\n",
3611 __kmp_gtid_from_thread(this_thr),
3612 team->t.t_task_team[this_thr->th.th_task_state], team->t.t_id,
3613 this_thr->th.th_task_state));
3623 if (team->t.t_nproc > 1) {
3624 int other_team = 1 - this_thr->th.th_task_state;
3625 KMP_DEBUG_ASSERT(other_team >= 0 && other_team < 2);
3626 if (team->t.t_task_team[other_team] == NULL) {
3627 team->t.t_task_team[other_team] =
3628 __kmp_allocate_task_team(this_thr, team);
3629 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d created second new "
3630 "task_team %p for team %d at parity=%d\n",
3631 __kmp_gtid_from_thread(this_thr),
3632 team->t.t_task_team[other_team], team->t.t_id, other_team));
3635 kmp_task_team_t *task_team = team->t.t_task_team[other_team];
3636 if (!task_team->tt.tt_active ||
3637 team->t.t_nproc != task_team->tt.tt_nproc) {
3638 TCW_4(task_team->tt.tt_nproc, team->t.t_nproc);
3639 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3640 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3641 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads,
3643 TCW_4(task_team->tt.tt_active, TRUE);
3647 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d reset next task_team "
3648 "%p for team %d at parity=%d\n",
3649 __kmp_gtid_from_thread(this_thr),
3650 team->t.t_task_team[other_team], team->t.t_id, other_team));
3658 if (this_thr == __kmp_hidden_helper_main_thread) {
3659 for (
int i = 0; i < 2; ++i) {
3660 kmp_task_team_t *task_team = team->t.t_task_team[i];
3661 if (KMP_TASKING_ENABLED(task_team)) {
3664 __kmp_enable_tasking(task_team, this_thr);
3665 for (
int j = 0; j < task_team->tt.tt_nproc; ++j) {
3666 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[j];
3667 if (thread_data->td.td_deque == NULL) {
3668 __kmp_alloc_task_deque(__kmp_hidden_helper_threads[j], thread_data);
3678 void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team) {
3679 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3683 this_thr->th.th_task_state = (kmp_uint8)(1 - this_thr->th.th_task_state);
3687 TCW_PTR(this_thr->th.th_task_team,
3688 team->t.t_task_team[this_thr->th.th_task_state]);
3690 (
"__kmp_task_team_sync: Thread T#%d task team switched to task_team "
3691 "%p from Team #%d (parity=%d)\n",
3692 __kmp_gtid_from_thread(this_thr), this_thr->th.th_task_team,
3693 team->t.t_id, this_thr->th.th_task_state));
3703 void __kmp_task_team_wait(
3704 kmp_info_t *this_thr,
3705 kmp_team_t *team USE_ITT_BUILD_ARG(
void *itt_sync_obj),
int wait) {
3706 kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state];
3708 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3709 KMP_DEBUG_ASSERT(task_team == this_thr->th.th_task_team);
3711 if ((task_team != NULL) && KMP_TASKING_ENABLED(task_team)) {
3713 KA_TRACE(20, (
"__kmp_task_team_wait: Primary T#%d waiting for all tasks "
3714 "(for unfinished_threads to reach 0) on task_team = %p\n",
3715 __kmp_gtid_from_thread(this_thr), task_team));
3719 kmp_flag_32<false, false> flag(
3720 RCAST(std::atomic<kmp_uint32> *,
3721 &task_team->tt.tt_unfinished_threads),
3723 flag.wait(this_thr, TRUE USE_ITT_BUILD_ARG(itt_sync_obj));
3729 (
"__kmp_task_team_wait: Primary T#%d deactivating task_team %p: "
3730 "setting active to false, setting local and team's pointer to NULL\n",
3731 __kmp_gtid_from_thread(this_thr), task_team));
3732 KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1 ||
3733 task_team->tt.tt_found_proxy_tasks == TRUE);
3734 TCW_SYNC_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3735 KMP_CHECK_UPDATE(task_team->tt.tt_untied_task_encountered, 0);
3736 TCW_SYNC_4(task_team->tt.tt_active, FALSE);
3739 TCW_PTR(this_thr->th.th_task_team, NULL);
3748 void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread,
int gtid) {
3749 std::atomic<kmp_uint32> *spin = RCAST(
3750 std::atomic<kmp_uint32> *,
3751 &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads);
3753 KMP_DEBUG_ASSERT(__kmp_tasking_mode == tskm_extra_barrier);
3756 KMP_FSYNC_SPIN_INIT(spin, NULL);
3758 kmp_flag_32<false, false> spin_flag(spin, 0U);
3759 while (!spin_flag.execute_tasks(thread, gtid, TRUE,
3760 &flag USE_ITT_BUILD_ARG(NULL), 0)) {
3763 KMP_FSYNC_SPIN_PREPARE(RCAST(
void *, spin));
3766 if (TCR_4(__kmp_global.g.g_done)) {
3767 if (__kmp_global.g.g_abort)
3768 __kmp_abort_thread();
3774 KMP_FSYNC_SPIN_ACQUIRED(RCAST(
void *, spin));
3783 static bool __kmp_give_task(kmp_info_t *thread, kmp_int32 tid, kmp_task_t *task,
3785 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
3786 kmp_task_team_t *task_team = taskdata->td_task_team;
3788 KA_TRACE(20, (
"__kmp_give_task: trying to give task %p to thread %d.\n",
3792 KMP_DEBUG_ASSERT(task_team != NULL);
3794 bool result =
false;
3795 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
3797 if (thread_data->td.td_deque == NULL) {
3801 (
"__kmp_give_task: thread %d has no queue while giving task %p.\n",
3806 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3807 TASK_DEQUE_SIZE(thread_data->td)) {
3810 (
"__kmp_give_task: queue is full while giving task %p to thread %d.\n",
3815 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
3818 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3819 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3820 TASK_DEQUE_SIZE(thread_data->td)) {
3822 __kmp_realloc_task_deque(thread, thread_data);
3827 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3829 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3830 TASK_DEQUE_SIZE(thread_data->td)) {
3831 KA_TRACE(30, (
"__kmp_give_task: queue is full while giving task %p to "
3837 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
3838 goto release_and_exit;
3840 __kmp_realloc_task_deque(thread, thread_data);
3846 thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
3848 thread_data->td.td_deque_tail =
3849 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
3850 TCW_4(thread_data->td.td_deque_ntasks,
3851 TCR_4(thread_data->td.td_deque_ntasks) + 1);
3854 KA_TRACE(30, (
"__kmp_give_task: successfully gave task %p to thread %d.\n",
3858 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3863 #define PROXY_TASK_FLAG 0x40000000
3880 static void __kmp_first_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
3881 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
3882 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3883 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
3884 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
3886 taskdata->td_flags.complete = 1;
3888 if (taskdata->td_taskgroup)
3889 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
3893 KMP_ATOMIC_OR(&taskdata->td_incomplete_child_tasks, PROXY_TASK_FLAG);
3896 static void __kmp_second_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
3898 kmp_int32 children = 0;
3902 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks);
3903 KMP_DEBUG_ASSERT(children >= 0);
3906 KMP_ATOMIC_AND(&taskdata->td_incomplete_child_tasks, ~PROXY_TASK_FLAG);
3909 static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask) {
3910 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3911 kmp_info_t *thread = __kmp_threads[gtid];
3913 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3914 KMP_DEBUG_ASSERT(taskdata->td_flags.complete ==
3919 while ((KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) &
3920 PROXY_TASK_FLAG) > 0)
3923 __kmp_release_deps(gtid, taskdata);
3924 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
3936 KMP_DEBUG_ASSERT(ptask != NULL);
3937 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3939 10, (
"__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n",
3941 __kmp_assert_valid_gtid(gtid);
3942 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3944 __kmp_first_top_half_finish_proxy(taskdata);
3945 __kmp_second_top_half_finish_proxy(taskdata);
3946 __kmp_bottom_half_finish_proxy(gtid, ptask);
3949 (
"__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n",
3953 void __kmpc_give_task(kmp_task_t *ptask, kmp_int32 start = 0) {
3954 KMP_DEBUG_ASSERT(ptask != NULL);
3955 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3959 kmp_team_t *team = taskdata->td_team;
3960 kmp_int32 nthreads = team->t.t_nproc;
3965 kmp_int32 start_k = start % nthreads;
3967 kmp_int32 k = start_k;
3971 thread = team->t.t_threads[k];
3972 k = (k + 1) % nthreads;
3978 }
while (!__kmp_give_task(thread, k, ptask, pass));
3989 KMP_DEBUG_ASSERT(ptask != NULL);
3990 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3994 (
"__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n",
3997 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3999 __kmp_first_top_half_finish_proxy(taskdata);
4001 __kmpc_give_task(ptask);
4003 __kmp_second_top_half_finish_proxy(taskdata);
4007 (
"__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n",
4011 kmp_event_t *__kmpc_task_allow_completion_event(
ident_t *loc_ref,
int gtid,
4013 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(task);
4014 if (td->td_allow_completion_event.type == KMP_EVENT_UNINITIALIZED) {
4015 td->td_allow_completion_event.type = KMP_EVENT_ALLOW_COMPLETION;
4016 td->td_allow_completion_event.ed.task = task;
4017 __kmp_init_tas_lock(&td->td_allow_completion_event.lock);
4019 return &td->td_allow_completion_event;
4022 void __kmp_fulfill_event(kmp_event_t *event) {
4023 if (event->type == KMP_EVENT_ALLOW_COMPLETION) {
4024 kmp_task_t *ptask = event->ed.task;
4025 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4026 bool detached =
false;
4027 int gtid = __kmp_get_gtid();
4032 __kmp_acquire_tas_lock(&event->lock, gtid);
4033 if (taskdata->td_flags.proxy == TASK_PROXY) {
4039 if (UNLIKELY(ompt_enabled.enabled))
4040 __ompt_task_finish(ptask, NULL, ompt_task_early_fulfill);
4043 event->type = KMP_EVENT_UNINITIALIZED;
4044 __kmp_release_tas_lock(&event->lock, gtid);
4050 if (UNLIKELY(ompt_enabled.enabled))
4051 __ompt_task_finish(ptask, NULL, ompt_task_late_fulfill);
4055 kmp_team_t *team = taskdata->td_team;
4056 kmp_info_t *thread = __kmp_get_thread();
4057 if (thread->th.th_team == team) {
4075 kmp_task_t *__kmp_task_dup_alloc(kmp_info_t *thread, kmp_task_t *task_src) {
4077 kmp_taskdata_t *taskdata;
4078 kmp_taskdata_t *taskdata_src = KMP_TASK_TO_TASKDATA(task_src);
4079 kmp_taskdata_t *parent_task = taskdata_src->td_parent;
4080 size_t shareds_offset;
4083 KA_TRACE(10, (
"__kmp_task_dup_alloc(enter): Th %p, source task %p\n", thread,
4085 KMP_DEBUG_ASSERT(taskdata_src->td_flags.proxy ==
4087 KMP_DEBUG_ASSERT(taskdata_src->td_flags.tasktype == TASK_EXPLICIT);
4088 task_size = taskdata_src->td_size_alloc;
4091 KA_TRACE(30, (
"__kmp_task_dup_alloc: Th %p, malloc size %ld\n", thread,
4094 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, task_size);
4096 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, task_size);
4098 KMP_MEMCPY(taskdata, taskdata_src, task_size);
4100 task = KMP_TASKDATA_TO_TASK(taskdata);
4103 taskdata->td_task_id = KMP_GEN_TASK_ID();
4104 if (task->shareds != NULL) {
4105 shareds_offset = (
char *)task_src->shareds - (
char *)taskdata_src;
4106 task->shareds = &((
char *)taskdata)[shareds_offset];
4107 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
4110 taskdata->td_alloc_thread = thread;
4111 taskdata->td_parent = parent_task;
4113 taskdata->td_taskgroup = parent_task->td_taskgroup;
4116 if (taskdata->td_flags.tiedness == TASK_TIED)
4117 taskdata->td_last_tied = taskdata;
4121 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
4122 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
4123 if (parent_task->td_taskgroup)
4124 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
4127 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT)
4128 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
4132 (
"__kmp_task_dup_alloc(exit): Th %p, created task %p, parent=%p\n",
4133 thread, taskdata, taskdata->td_parent));
4135 if (UNLIKELY(ompt_enabled.enabled))
4136 __ompt_task_init(taskdata, thread->th.th_info.ds.ds_gtid);
4145 typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32);
4147 KMP_BUILD_ASSERT(
sizeof(
long) == 4 ||
sizeof(
long) == 8);
4152 class kmp_taskloop_bounds_t {
4154 const kmp_taskdata_t *taskdata;
4155 size_t lower_offset;
4156 size_t upper_offset;
4159 kmp_taskloop_bounds_t(kmp_task_t *_task, kmp_uint64 *lb, kmp_uint64 *ub)
4160 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(task)),
4161 lower_offset((char *)lb - (char *)task),
4162 upper_offset((char *)ub - (char *)task) {
4163 KMP_DEBUG_ASSERT((
char *)lb > (
char *)_task);
4164 KMP_DEBUG_ASSERT((
char *)ub > (
char *)_task);
4166 kmp_taskloop_bounds_t(kmp_task_t *_task,
const kmp_taskloop_bounds_t &bounds)
4167 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(_task)),
4168 lower_offset(bounds.lower_offset), upper_offset(bounds.upper_offset) {}
4169 size_t get_lower_offset()
const {
return lower_offset; }
4170 size_t get_upper_offset()
const {
return upper_offset; }
4171 kmp_uint64 get_lb()
const {
4173 #if defined(KMP_GOMP_COMPAT)
4175 if (!taskdata->td_flags.native) {
4176 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4179 if (taskdata->td_size_loop_bounds == 4) {
4180 kmp_int32 *lb = RCAST(kmp_int32 *, task->shareds);
4181 retval = (kmp_int64)*lb;
4183 kmp_int64 *lb = RCAST(kmp_int64 *, task->shareds);
4184 retval = (kmp_int64)*lb;
4189 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4193 kmp_uint64 get_ub()
const {
4195 #if defined(KMP_GOMP_COMPAT)
4197 if (!taskdata->td_flags.native) {
4198 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4201 if (taskdata->td_size_loop_bounds == 4) {
4202 kmp_int32 *ub = RCAST(kmp_int32 *, task->shareds) + 1;
4203 retval = (kmp_int64)*ub;
4205 kmp_int64 *ub = RCAST(kmp_int64 *, task->shareds) + 1;
4206 retval = (kmp_int64)*ub;
4210 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4214 void set_lb(kmp_uint64 lb) {
4215 #if defined(KMP_GOMP_COMPAT)
4217 if (!taskdata->td_flags.native) {
4218 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4221 if (taskdata->td_size_loop_bounds == 4) {
4222 kmp_uint32 *lower = RCAST(kmp_uint32 *, task->shareds);
4223 *lower = (kmp_uint32)lb;
4225 kmp_uint64 *lower = RCAST(kmp_uint64 *, task->shareds);
4226 *lower = (kmp_uint64)lb;
4230 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4233 void set_ub(kmp_uint64 ub) {
4234 #if defined(KMP_GOMP_COMPAT)
4236 if (!taskdata->td_flags.native) {
4237 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4240 if (taskdata->td_size_loop_bounds == 4) {
4241 kmp_uint32 *upper = RCAST(kmp_uint32 *, task->shareds) + 1;
4242 *upper = (kmp_uint32)ub;
4244 kmp_uint64 *upper = RCAST(kmp_uint64 *, task->shareds) + 1;
4245 *upper = (kmp_uint64)ub;
4249 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4270 void __kmp_taskloop_linear(
ident_t *loc,
int gtid, kmp_task_t *task,
4271 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4272 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
4273 kmp_uint64 grainsize, kmp_uint64 extras,
4274 kmp_int64 last_chunk, kmp_uint64 tc,
4280 KMP_TIME_PARTITIONED_BLOCK(OMP_taskloop_scheduling);
4281 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
4283 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4284 kmp_uint64 lower = task_bounds.get_lb();
4285 kmp_uint64 upper = task_bounds.get_ub();
4287 kmp_info_t *thread = __kmp_threads[gtid];
4288 kmp_taskdata_t *current_task = thread->th.th_current_task;
4289 kmp_task_t *next_task;
4290 kmp_int32 lastpriv = 0;
4292 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4293 (last_chunk < 0 ? last_chunk : extras));
4294 KMP_DEBUG_ASSERT(num_tasks > extras);
4295 KMP_DEBUG_ASSERT(num_tasks > 0);
4296 KA_TRACE(20, (
"__kmp_taskloop_linear: T#%d: %lld tasks, grainsize %lld, "
4297 "extras %lld, last_chunk %lld, i=%lld,%lld(%d)%lld, dup %p\n",
4298 gtid, num_tasks, grainsize, extras, last_chunk, lower, upper,
4299 ub_glob, st, task_dup));
4302 for (i = 0; i < num_tasks; ++i) {
4303 kmp_uint64 chunk_minus_1;
4305 chunk_minus_1 = grainsize - 1;
4307 chunk_minus_1 = grainsize;
4310 upper = lower + st * chunk_minus_1;
4314 if (i == num_tasks - 1) {
4317 KMP_DEBUG_ASSERT(upper == *ub);
4318 if (upper == ub_glob)
4320 }
else if (st > 0) {
4321 KMP_DEBUG_ASSERT((kmp_uint64)st > *ub - upper);
4322 if ((kmp_uint64)st > ub_glob - upper)
4325 KMP_DEBUG_ASSERT(upper + st < *ub);
4326 if (upper - ub_glob < (kmp_uint64)(-st))
4330 next_task = __kmp_task_dup_alloc(thread, task);
4331 kmp_taskdata_t *next_taskdata = KMP_TASK_TO_TASKDATA(next_task);
4332 kmp_taskloop_bounds_t next_task_bounds =
4333 kmp_taskloop_bounds_t(next_task, task_bounds);
4336 next_task_bounds.set_lb(lower);
4337 if (next_taskdata->td_flags.native) {
4338 next_task_bounds.set_ub(upper + (st > 0 ? 1 : -1));
4340 next_task_bounds.set_ub(upper);
4342 if (ptask_dup != NULL)
4344 ptask_dup(next_task, task, lastpriv);
4346 (
"__kmp_taskloop_linear: T#%d; task #%llu: task %p: lower %lld, "
4347 "upper %lld stride %lld, (offsets %p %p)\n",
4348 gtid, i, next_task, lower, upper, st,
4349 next_task_bounds.get_lower_offset(),
4350 next_task_bounds.get_upper_offset()));
4352 __kmp_omp_taskloop_task(NULL, gtid, next_task,
4355 __kmp_omp_task(gtid, next_task,
true);
4360 __kmp_task_start(gtid, task, current_task);
4362 __kmp_task_finish<false>(gtid, task, current_task);
4367 typedef struct __taskloop_params {
4374 kmp_uint64 num_tasks;
4375 kmp_uint64 grainsize;
4377 kmp_int64 last_chunk;
4379 kmp_uint64 num_t_min;
4383 } __taskloop_params_t;
4385 void __kmp_taskloop_recur(
ident_t *,
int, kmp_task_t *, kmp_uint64 *,
4386 kmp_uint64 *, kmp_int64, kmp_uint64, kmp_uint64,
4387 kmp_uint64, kmp_uint64, kmp_int64, kmp_uint64,
4395 int __kmp_taskloop_task(
int gtid,
void *ptask) {
4396 __taskloop_params_t *p =
4397 (__taskloop_params_t *)((kmp_task_t *)ptask)->shareds;
4398 kmp_task_t *task = p->task;
4399 kmp_uint64 *lb = p->lb;
4400 kmp_uint64 *ub = p->ub;
4401 void *task_dup = p->task_dup;
4403 kmp_int64 st = p->st;
4404 kmp_uint64 ub_glob = p->ub_glob;
4405 kmp_uint64 num_tasks = p->num_tasks;
4406 kmp_uint64 grainsize = p->grainsize;
4407 kmp_uint64 extras = p->extras;
4408 kmp_int64 last_chunk = p->last_chunk;
4409 kmp_uint64 tc = p->tc;
4410 kmp_uint64 num_t_min = p->num_t_min;
4412 void *codeptr_ra = p->codeptr_ra;
4415 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4416 KMP_DEBUG_ASSERT(task != NULL);
4418 (
"__kmp_taskloop_task: T#%d, task %p: %lld tasks, grainsize"
4419 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
4420 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
4423 KMP_DEBUG_ASSERT(num_tasks * 2 + 1 > num_t_min);
4424 if (num_tasks > num_t_min)
4425 __kmp_taskloop_recur(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
4426 grainsize, extras, last_chunk, tc, num_t_min,
4432 __kmp_taskloop_linear(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
4433 grainsize, extras, last_chunk, tc,
4439 KA_TRACE(40, (
"__kmp_taskloop_task(exit): T#%d\n", gtid));
4461 void __kmp_taskloop_recur(
ident_t *loc,
int gtid, kmp_task_t *task,
4462 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4463 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
4464 kmp_uint64 grainsize, kmp_uint64 extras,
4465 kmp_int64 last_chunk, kmp_uint64 tc,
4466 kmp_uint64 num_t_min,
4471 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4472 KMP_DEBUG_ASSERT(task != NULL);
4473 KMP_DEBUG_ASSERT(num_tasks > num_t_min);
4475 (
"__kmp_taskloop_recur: T#%d, task %p: %lld tasks, grainsize"
4476 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
4477 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
4479 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
4480 kmp_uint64 lower = *lb;
4481 kmp_info_t *thread = __kmp_threads[gtid];
4483 kmp_task_t *next_task;
4484 size_t lower_offset =
4485 (
char *)lb - (
char *)task;
4486 size_t upper_offset =
4487 (
char *)ub - (
char *)task;
4489 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4490 (last_chunk < 0 ? last_chunk : extras));
4491 KMP_DEBUG_ASSERT(num_tasks > extras);
4492 KMP_DEBUG_ASSERT(num_tasks > 0);
4495 kmp_uint64 lb1, ub0, tc0, tc1, ext0, ext1;
4496 kmp_int64 last_chunk0 = 0, last_chunk1 = 0;
4497 kmp_uint64 gr_size0 = grainsize;
4498 kmp_uint64 n_tsk0 = num_tasks >> 1;
4499 kmp_uint64 n_tsk1 = num_tasks - n_tsk0;
4500 if (last_chunk < 0) {
4502 last_chunk1 = last_chunk;
4503 tc0 = grainsize * n_tsk0;
4505 }
else if (n_tsk0 <= extras) {
4508 ext1 = extras - n_tsk0;
4509 tc0 = gr_size0 * n_tsk0;
4514 tc1 = grainsize * n_tsk1;
4517 ub0 = lower + st * (tc0 - 1);
4521 next_task = __kmp_task_dup_alloc(thread, task);
4523 *(kmp_uint64 *)((
char *)next_task + lower_offset) = lb1;
4524 if (ptask_dup != NULL)
4525 ptask_dup(next_task, task, 0);
4530 kmp_taskdata_t *current_task = thread->th.th_current_task;
4531 thread->th.th_current_task = taskdata->td_parent;
4532 kmp_task_t *new_task =
4533 __kmpc_omp_task_alloc(loc, gtid, 1, 3 *
sizeof(
void *),
4534 sizeof(__taskloop_params_t), &__kmp_taskloop_task);
4536 thread->th.th_current_task = current_task;
4537 __taskloop_params_t *p = (__taskloop_params_t *)new_task->shareds;
4538 p->task = next_task;
4539 p->lb = (kmp_uint64 *)((
char *)next_task + lower_offset);
4540 p->ub = (kmp_uint64 *)((
char *)next_task + upper_offset);
4541 p->task_dup = task_dup;
4543 p->ub_glob = ub_glob;
4544 p->num_tasks = n_tsk1;
4545 p->grainsize = grainsize;
4547 p->last_chunk = last_chunk1;
4549 p->num_t_min = num_t_min;
4551 p->codeptr_ra = codeptr_ra;
4556 __kmp_omp_taskloop_task(NULL, gtid, new_task, codeptr_ra);
4558 __kmp_omp_task(gtid, new_task,
true);
4562 if (n_tsk0 > num_t_min)
4563 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0, gr_size0,
4564 ext0, last_chunk0, tc0, num_t_min,
4570 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0,
4571 gr_size0, ext0, last_chunk0, tc0,
4577 KA_TRACE(40, (
"__kmp_taskloop_recur(exit): T#%d\n", gtid));
4580 static void __kmp_taskloop(
ident_t *loc,
int gtid, kmp_task_t *task,
int if_val,
4581 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4582 int nogroup,
int sched, kmp_uint64 grainsize,
4583 int modifier,
void *task_dup) {
4584 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4585 KMP_DEBUG_ASSERT(task != NULL);
4587 #if OMPT_SUPPORT && OMPT_OPTIONAL
4588 OMPT_STORE_RETURN_ADDRESS(gtid);
4590 __kmpc_taskgroup(loc, gtid);
4595 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4598 kmp_uint64 lower = task_bounds.get_lb();
4599 kmp_uint64 upper = task_bounds.get_ub();
4600 kmp_uint64 ub_glob = upper;
4601 kmp_uint64 num_tasks = 0, extras = 0;
4602 kmp_int64 last_chunk =
4604 kmp_uint64 num_tasks_min = __kmp_taskloop_min_tasks;
4605 kmp_info_t *thread = __kmp_threads[gtid];
4606 kmp_taskdata_t *current_task = thread->th.th_current_task;
4608 KA_TRACE(20, (
"__kmp_taskloop: T#%d, task %p, lb %lld, ub %lld, st %lld, "
4609 "grain %llu(%d, %d), dup %p\n",
4610 gtid, taskdata, lower, upper, st, grainsize, sched, modifier,
4615 tc = upper - lower + 1;
4616 }
else if (st < 0) {
4617 tc = (lower - upper) / (-st) + 1;
4619 tc = (upper - lower) / st + 1;
4622 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d zero-trip loop\n", gtid));
4624 __kmp_task_start(gtid, task, current_task);
4626 __kmp_task_finish<false>(gtid, task, current_task);
4630 #if OMPT_SUPPORT && OMPT_OPTIONAL
4631 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
4632 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
4633 if (ompt_enabled.ompt_callback_work) {
4634 ompt_callbacks.ompt_callback(ompt_callback_work)(
4635 ompt_work_taskloop, ompt_scope_begin, &(team_info->parallel_data),
4636 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
4640 if (num_tasks_min == 0)
4643 KMP_MIN(thread->th.th_team_nproc * 10, INITIAL_TASK_DEQUE_SIZE);
4649 grainsize = thread->th.th_team_nproc * 10;
4652 if (grainsize > tc) {
4657 num_tasks = grainsize;
4658 grainsize = tc / num_tasks;
4659 extras = tc % num_tasks;
4663 if (grainsize > tc) {
4669 num_tasks = (tc + grainsize - 1) / grainsize;
4670 last_chunk = tc - (num_tasks * grainsize);
4673 num_tasks = tc / grainsize;
4675 grainsize = tc / num_tasks;
4676 extras = tc % num_tasks;
4681 KMP_ASSERT2(0,
"unknown scheduling of taskloop");
4684 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4685 (last_chunk < 0 ? last_chunk : extras));
4686 KMP_DEBUG_ASSERT(num_tasks > extras);
4687 KMP_DEBUG_ASSERT(num_tasks > 0);
4693 taskdata->td_flags.task_serial = 1;
4694 taskdata->td_flags.tiedness = TASK_TIED;
4696 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4697 grainsize, extras, last_chunk, tc,
4699 OMPT_GET_RETURN_ADDRESS(0),
4704 }
else if (num_tasks > num_tasks_min && !taskdata->td_flags.native) {
4705 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go recursive: tc %llu, #tasks %llu"
4706 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
4707 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
4709 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4710 grainsize, extras, last_chunk, tc, num_tasks_min,
4712 OMPT_GET_RETURN_ADDRESS(0),
4716 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go linear: tc %llu, #tasks %llu"
4717 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
4718 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
4720 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4721 grainsize, extras, last_chunk, tc,
4723 OMPT_GET_RETURN_ADDRESS(0),
4728 #if OMPT_SUPPORT && OMPT_OPTIONAL
4729 if (ompt_enabled.ompt_callback_work) {
4730 ompt_callbacks.ompt_callback(ompt_callback_work)(
4731 ompt_work_taskloop, ompt_scope_end, &(team_info->parallel_data),
4732 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
4737 #if OMPT_SUPPORT && OMPT_OPTIONAL
4738 OMPT_STORE_RETURN_ADDRESS(gtid);
4740 __kmpc_end_taskgroup(loc, gtid);
4742 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d\n", gtid));
4762 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
int nogroup,
4763 int sched, kmp_uint64 grainsize,
void *task_dup) {
4764 __kmp_assert_valid_gtid(gtid);
4765 KA_TRACE(20, (
"__kmpc_taskloop(enter): T#%d\n", gtid));
4766 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
4768 KA_TRACE(20, (
"__kmpc_taskloop(exit): T#%d\n", gtid));
4789 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4790 int nogroup,
int sched, kmp_uint64 grainsize,
4791 int modifier,
void *task_dup) {
4792 __kmp_assert_valid_gtid(gtid);
4793 KA_TRACE(20, (
"__kmpc_taskloop_5(enter): T#%d\n", gtid));
4794 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
4795 modifier, task_dup);
4796 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