17#include "kmp_wait_release.h"
18#include "kmp_taskdeps.h"
21#include "ompt-specific.h"
24#if ENABLE_LIBOMPTARGET
25static void (*tgt_target_nowait_query)(
void **);
27void __kmp_init_target_task() {
28 *(
void **)(&tgt_target_nowait_query) = KMP_DLSYM(
"__tgt_target_nowait_query");
33static void __kmp_enable_tasking(kmp_task_team_t *task_team,
34 kmp_info_t *this_thr);
35static void __kmp_alloc_task_deque(kmp_info_t *thread,
36 kmp_thread_data_t *thread_data);
37static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
38 kmp_task_team_t *task_team);
39static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask);
41static kmp_tdg_info_t *__kmp_find_tdg(kmp_int32 tdg_id);
42int __kmp_taskloop_task(
int gtid,
void *ptask);
45#ifdef BUILD_TIED_TASK_STACK
54static void __kmp_trace_task_stack(kmp_int32 gtid,
55 kmp_thread_data_t *thread_data,
56 int threshold,
char *location) {
57 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
58 kmp_taskdata_t **stack_top = task_stack->ts_top;
59 kmp_int32 entries = task_stack->ts_entries;
60 kmp_taskdata_t *tied_task;
64 (
"__kmp_trace_task_stack(start): location = %s, gtid = %d, entries = %d, "
65 "first_block = %p, stack_top = %p \n",
66 location, gtid, entries, task_stack->ts_first_block, stack_top));
68 KMP_DEBUG_ASSERT(stack_top != NULL);
69 KMP_DEBUG_ASSERT(entries > 0);
71 while (entries != 0) {
72 KMP_DEBUG_ASSERT(stack_top != &task_stack->ts_first_block.sb_block[0]);
74 if (entries & TASK_STACK_INDEX_MASK == 0) {
75 kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(stack_top);
77 stack_block = stack_block->sb_prev;
78 stack_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE];
85 tied_task = *stack_top;
87 KMP_DEBUG_ASSERT(tied_task != NULL);
88 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
91 (
"__kmp_trace_task_stack(%s): gtid=%d, entry=%d, "
92 "stack_top=%p, tied_task=%p\n",
93 location, gtid, entries, stack_top, tied_task));
95 KMP_DEBUG_ASSERT(stack_top == &task_stack->ts_first_block.sb_block[0]);
98 (
"__kmp_trace_task_stack(exit): location = %s, gtid = %d\n",
108static void __kmp_init_task_stack(kmp_int32 gtid,
109 kmp_thread_data_t *thread_data) {
110 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
111 kmp_stack_block_t *first_block;
114 first_block = &task_stack->ts_first_block;
115 task_stack->ts_top = (kmp_taskdata_t **)first_block;
116 memset((
void *)first_block,
'\0',
117 TASK_STACK_BLOCK_SIZE *
sizeof(kmp_taskdata_t *));
120 task_stack->ts_entries = TASK_STACK_EMPTY;
121 first_block->sb_next = NULL;
122 first_block->sb_prev = NULL;
129static void __kmp_free_task_stack(kmp_int32 gtid,
130 kmp_thread_data_t *thread_data) {
131 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
132 kmp_stack_block_t *stack_block = &task_stack->ts_first_block;
134 KMP_DEBUG_ASSERT(task_stack->ts_entries == TASK_STACK_EMPTY);
136 while (stack_block != NULL) {
137 kmp_stack_block_t *next_block = (stack_block) ? stack_block->sb_next : NULL;
139 stack_block->sb_next = NULL;
140 stack_block->sb_prev = NULL;
141 if (stack_block != &task_stack->ts_first_block) {
142 __kmp_thread_free(thread,
145 stack_block = next_block;
148 task_stack->ts_entries = 0;
149 task_stack->ts_top = NULL;
158static void __kmp_push_task_stack(kmp_int32 gtid, kmp_info_t *thread,
159 kmp_taskdata_t *tied_task) {
161 kmp_thread_data_t *thread_data =
162 &thread->th.th_task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
163 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
165 if (tied_task->td_flags.team_serial || tied_task->td_flags.tasking_ser) {
169 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
170 KMP_DEBUG_ASSERT(task_stack->ts_top != NULL);
173 (
"__kmp_push_task_stack(enter): GTID: %d; THREAD: %p; TASK: %p\n",
174 gtid, thread, tied_task));
176 *(task_stack->ts_top) = tied_task;
179 task_stack->ts_top++;
180 task_stack->ts_entries++;
182 if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) {
184 kmp_stack_block_t *stack_block =
185 (kmp_stack_block_t *)(task_stack->ts_top - TASK_STACK_BLOCK_SIZE);
188 if (stack_block->sb_next !=
190 task_stack->ts_top = &stack_block->sb_next->sb_block[0];
192 kmp_stack_block_t *new_block = (kmp_stack_block_t *)__kmp_thread_calloc(
193 thread,
sizeof(kmp_stack_block_t));
195 task_stack->ts_top = &new_block->sb_block[0];
196 stack_block->sb_next = new_block;
197 new_block->sb_prev = stack_block;
198 new_block->sb_next = NULL;
202 (
"__kmp_push_task_stack(): GTID: %d; TASK: %p; Alloc new block: %p\n",
203 gtid, tied_task, new_block));
206 KA_TRACE(20, (
"__kmp_push_task_stack(exit): GTID: %d; TASK: %p\n", gtid,
217static void __kmp_pop_task_stack(kmp_int32 gtid, kmp_info_t *thread,
218 kmp_taskdata_t *ending_task) {
220 kmp_thread_data_t *thread_data =
221 &thread->th.th_task_team->tt_threads_data[__kmp_tid_from_gtid(gtid)];
222 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
223 kmp_taskdata_t *tied_task;
225 if (ending_task->td_flags.team_serial || ending_task->td_flags.tasking_ser) {
230 KMP_DEBUG_ASSERT(task_stack->ts_top != NULL);
231 KMP_DEBUG_ASSERT(task_stack->ts_entries > 0);
233 KA_TRACE(20, (
"__kmp_pop_task_stack(enter): GTID: %d; THREAD: %p\n", gtid,
237 if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) {
238 kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(task_stack->ts_top);
240 stack_block = stack_block->sb_prev;
241 task_stack->ts_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE];
245 task_stack->ts_top--;
246 task_stack->ts_entries--;
248 tied_task = *(task_stack->ts_top);
250 KMP_DEBUG_ASSERT(tied_task != NULL);
251 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
252 KMP_DEBUG_ASSERT(tied_task == ending_task);
254 KA_TRACE(20, (
"__kmp_pop_task_stack(exit): GTID: %d; TASK: %p\n", gtid,
263static bool __kmp_task_is_allowed(
int gtid,
const kmp_int32 is_constrained,
264 const kmp_taskdata_t *tasknew,
265 const kmp_taskdata_t *taskcurr) {
266 if (is_constrained && (tasknew->td_flags.tiedness == TASK_TIED)) {
270 kmp_taskdata_t *current = taskcurr->td_last_tied;
271 KMP_DEBUG_ASSERT(current != NULL);
273 if (current->td_flags.tasktype == TASK_EXPLICIT ||
274 current->td_taskwait_thread > 0) {
275 kmp_int32 level = current->td_level;
276 kmp_taskdata_t *parent = tasknew->td_parent;
277 while (parent != current && parent->td_level > level) {
279 parent = parent->td_parent;
280 KMP_DEBUG_ASSERT(parent != NULL);
282 if (parent != current)
287 kmp_depnode_t *node = tasknew->td_depnode;
289 if (!tasknew->is_taskgraph && UNLIKELY(node && (node->dn.mtx_num_locks > 0))) {
291 if (UNLIKELY(node && (node->dn.mtx_num_locks > 0))) {
293 for (
int i = 0; i < node->dn.mtx_num_locks; ++i) {
294 KMP_DEBUG_ASSERT(node->dn.mtx_locks[i] != NULL);
295 if (__kmp_test_lock(node->dn.mtx_locks[i], gtid))
298 for (
int j = i - 1; j >= 0; --j)
299 __kmp_release_lock(node->dn.mtx_locks[j], gtid);
303 node->dn.mtx_num_locks = -node->dn.mtx_num_locks;
312static void __kmp_realloc_task_deque(kmp_info_t *thread,
313 kmp_thread_data_t *thread_data) {
314 kmp_int32 size = TASK_DEQUE_SIZE(thread_data->td);
315 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == size);
316 kmp_int32 new_size = 2 * size;
318 KE_TRACE(10, (
"__kmp_realloc_task_deque: T#%d reallocating deque[from %d to "
319 "%d] for thread_data %p\n",
320 __kmp_gtid_from_thread(thread), size, new_size, thread_data));
322 kmp_taskdata_t **new_deque =
323 (kmp_taskdata_t **)__kmp_allocate(new_size *
sizeof(kmp_taskdata_t *));
326 for (i = thread_data->td.td_deque_head, j = 0; j < size;
327 i = (i + 1) & TASK_DEQUE_MASK(thread_data->td), j++)
328 new_deque[j] = thread_data->td.td_deque[i];
330 __kmp_free(thread_data->td.td_deque);
332 thread_data->td.td_deque_head = 0;
333 thread_data->td.td_deque_tail = size;
334 thread_data->td.td_deque = new_deque;
335 thread_data->td.td_deque_size = new_size;
338static kmp_task_pri_t *__kmp_alloc_task_pri_list() {
339 kmp_task_pri_t *l = (kmp_task_pri_t *)__kmp_allocate(
sizeof(kmp_task_pri_t));
340 kmp_thread_data_t *thread_data = &l->td;
341 __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
342 thread_data->td.td_deque_last_stolen = -1;
343 KE_TRACE(20, (
"__kmp_alloc_task_pri_list: T#%d allocating deque[%d] "
344 "for thread_data %p\n",
345 __kmp_get_gtid(), INITIAL_TASK_DEQUE_SIZE, thread_data));
346 thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
347 INITIAL_TASK_DEQUE_SIZE *
sizeof(kmp_taskdata_t *));
348 thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
357static kmp_thread_data_t *
358__kmp_get_priority_deque_data(kmp_task_team_t *task_team, kmp_int32 pri) {
359 kmp_thread_data_t *thread_data;
360 kmp_task_pri_t *lst = task_team->tt.tt_task_pri_list;
361 if (lst->priority == pri) {
363 thread_data = &lst->td;
364 }
else if (lst->priority < pri) {
367 kmp_task_pri_t *list = __kmp_alloc_task_pri_list();
368 thread_data = &list->td;
369 list->priority = pri;
371 task_team->tt.tt_task_pri_list = list;
373 kmp_task_pri_t *next_queue = lst->next;
374 while (next_queue && next_queue->priority > pri) {
376 next_queue = lst->next;
379 if (next_queue == NULL) {
381 kmp_task_pri_t *list = __kmp_alloc_task_pri_list();
382 thread_data = &list->td;
383 list->priority = pri;
386 }
else if (next_queue->priority == pri) {
388 thread_data = &next_queue->td;
391 kmp_task_pri_t *list = __kmp_alloc_task_pri_list();
392 thread_data = &list->td;
393 list->priority = pri;
394 list->next = next_queue;
402static kmp_int32 __kmp_push_priority_task(kmp_int32 gtid, kmp_info_t *thread,
403 kmp_taskdata_t *taskdata,
404 kmp_task_team_t *task_team,
406 kmp_thread_data_t *thread_data = NULL;
408 (
"__kmp_push_priority_task: T#%d trying to push task %p, pri %d.\n",
409 gtid, taskdata, pri));
412 kmp_task_pri_t *lst = task_team->tt.tt_task_pri_list;
413 if (UNLIKELY(lst == NULL)) {
414 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
415 if (task_team->tt.tt_task_pri_list == NULL) {
417 kmp_task_pri_t *list = __kmp_alloc_task_pri_list();
418 thread_data = &list->td;
419 list->priority = pri;
421 task_team->tt.tt_task_pri_list = list;
424 thread_data = __kmp_get_priority_deque_data(task_team, pri);
426 __kmp_release_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
428 if (lst->priority == pri) {
430 thread_data = &lst->td;
432 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
433 thread_data = __kmp_get_priority_deque_data(task_team, pri);
434 __kmp_release_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
437 KMP_DEBUG_ASSERT(thread_data);
439 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
441 if (TCR_4(thread_data->td.td_deque_ntasks) >=
442 TASK_DEQUE_SIZE(thread_data->td)) {
443 if (__kmp_enable_task_throttling &&
444 __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
445 thread->th.th_current_task)) {
446 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
447 KA_TRACE(20, (
"__kmp_push_priority_task: T#%d deque is full; returning "
448 "TASK_NOT_PUSHED for task %p\n",
450 return TASK_NOT_PUSHED;
453 __kmp_realloc_task_deque(thread, thread_data);
456 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) <
457 TASK_DEQUE_SIZE(thread_data->td));
459 thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
461 thread_data->td.td_deque_tail =
462 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
463 TCW_4(thread_data->td.td_deque_ntasks,
464 TCR_4(thread_data->td.td_deque_ntasks) + 1);
465 KMP_FSYNC_RELEASING(thread->th.th_current_task);
466 KMP_FSYNC_RELEASING(taskdata);
467 KA_TRACE(20, (
"__kmp_push_priority_task: T#%d returning "
468 "TASK_SUCCESSFULLY_PUSHED: task=%p ntasks=%d head=%u tail=%u\n",
469 gtid, taskdata, thread_data->td.td_deque_ntasks,
470 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
471 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
472 task_team->tt.tt_num_task_pri++;
473 return TASK_SUCCESSFULLY_PUSHED;
477static kmp_int32 __kmp_push_task(kmp_int32 gtid, kmp_task_t *task) {
478 kmp_info_t *thread = __kmp_threads[gtid];
479 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
484 if (UNLIKELY(taskdata->td_flags.hidden_helper &&
485 !KMP_HIDDEN_HELPER_THREAD(gtid))) {
486 kmp_int32 shadow_gtid = KMP_GTID_TO_SHADOW_GTID(gtid);
487 __kmpc_give_task(task, __kmp_tid_from_gtid(shadow_gtid));
489 __kmp_hidden_helper_worker_thread_signal();
490 return TASK_SUCCESSFULLY_PUSHED;
493 kmp_task_team_t *task_team = thread->th.th_task_team;
494 kmp_int32 tid = __kmp_tid_from_gtid(gtid);
495 kmp_thread_data_t *thread_data;
498 (
"__kmp_push_task: T#%d trying to push task %p.\n", gtid, taskdata));
500 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
503 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
504 KMP_DEBUG_USE_VAR(counter);
507 (
"__kmp_push_task: T#%d untied_count (%d) incremented for task %p\n",
508 gtid, counter, taskdata));
512 if (UNLIKELY(taskdata->td_flags.task_serial)) {
513 KA_TRACE(20, (
"__kmp_push_task: T#%d team serialized; returning "
514 "TASK_NOT_PUSHED for task %p\n",
516 return TASK_NOT_PUSHED;
521 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
522 if (UNLIKELY(!KMP_TASKING_ENABLED(task_team))) {
523 __kmp_enable_tasking(task_team, thread);
525 KMP_DEBUG_ASSERT(TCR_4(task_team->tt.tt_found_tasks) == TRUE);
526 KMP_DEBUG_ASSERT(TCR_PTR(task_team->tt.tt_threads_data) != NULL);
528 if (taskdata->td_flags.priority_specified && task->data2.priority > 0 &&
529 __kmp_max_task_priority > 0) {
530 int pri = KMP_MIN(task->data2.priority, __kmp_max_task_priority);
531 return __kmp_push_priority_task(gtid, thread, taskdata, task_team, pri);
535 thread_data = &task_team->tt.tt_threads_data[tid];
540 if (UNLIKELY(thread_data->td.td_deque == NULL)) {
541 __kmp_alloc_task_deque(thread, thread_data);
546 if (TCR_4(thread_data->td.td_deque_ntasks) >=
547 TASK_DEQUE_SIZE(thread_data->td)) {
548 if (__kmp_enable_task_throttling &&
549 __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
550 thread->th.th_current_task)) {
551 KA_TRACE(20, (
"__kmp_push_task: T#%d deque is full; returning "
552 "TASK_NOT_PUSHED for task %p\n",
554 return TASK_NOT_PUSHED;
556 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
558 if (TCR_4(thread_data->td.td_deque_ntasks) >=
559 TASK_DEQUE_SIZE(thread_data->td)) {
561 __kmp_realloc_task_deque(thread, thread_data);
567 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
569 if (TCR_4(thread_data->td.td_deque_ntasks) >=
570 TASK_DEQUE_SIZE(thread_data->td)) {
571 if (__kmp_enable_task_throttling &&
572 __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
573 thread->th.th_current_task)) {
574 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
575 KA_TRACE(20, (
"__kmp_push_task: T#%d deque is full on 2nd check; "
576 "returning TASK_NOT_PUSHED for task %p\n",
578 return TASK_NOT_PUSHED;
581 __kmp_realloc_task_deque(thread, thread_data);
586 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) <
587 TASK_DEQUE_SIZE(thread_data->td));
589 thread_data->td.td_deque[thread_data->td.td_deque_tail] =
592 thread_data->td.td_deque_tail =
593 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
594 TCW_4(thread_data->td.td_deque_ntasks,
595 TCR_4(thread_data->td.td_deque_ntasks) + 1);
596 KMP_FSYNC_RELEASING(thread->th.th_current_task);
597 KMP_FSYNC_RELEASING(taskdata);
598 KA_TRACE(20, (
"__kmp_push_task: T#%d returning TASK_SUCCESSFULLY_PUSHED: "
599 "task=%p ntasks=%d head=%u tail=%u\n",
600 gtid, taskdata, thread_data->td.td_deque_ntasks,
601 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
603 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
605 return TASK_SUCCESSFULLY_PUSHED;
612void __kmp_pop_current_task_from_thread(kmp_info_t *this_thr) {
613 KF_TRACE(10, (
"__kmp_pop_current_task_from_thread(enter): T#%d "
614 "this_thread=%p, curtask=%p, "
615 "curtask_parent=%p\n",
616 0, this_thr, this_thr->th.th_current_task,
617 this_thr->th.th_current_task->td_parent));
619 this_thr->th.th_current_task = this_thr->th.th_current_task->td_parent;
621 KF_TRACE(10, (
"__kmp_pop_current_task_from_thread(exit): T#%d "
622 "this_thread=%p, curtask=%p, "
623 "curtask_parent=%p\n",
624 0, this_thr, this_thr->th.th_current_task,
625 this_thr->th.th_current_task->td_parent));
634void __kmp_push_current_task_to_thread(kmp_info_t *this_thr, kmp_team_t *team,
638 KF_TRACE(10, (
"__kmp_push_current_task_to_thread(enter): T#%d this_thread=%p "
641 tid, this_thr, this_thr->th.th_current_task,
642 team->t.t_implicit_task_taskdata[tid].td_parent));
644 KMP_DEBUG_ASSERT(this_thr != NULL);
647 if (this_thr->th.th_current_task != &team->t.t_implicit_task_taskdata[0]) {
648 team->t.t_implicit_task_taskdata[0].td_parent =
649 this_thr->th.th_current_task;
650 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[0];
653 team->t.t_implicit_task_taskdata[tid].td_parent =
654 team->t.t_implicit_task_taskdata[0].td_parent;
655 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[tid];
658 KF_TRACE(10, (
"__kmp_push_current_task_to_thread(exit): T#%d this_thread=%p "
661 tid, this_thr, this_thr->th.th_current_task,
662 team->t.t_implicit_task_taskdata[tid].td_parent));
670static void __kmp_task_start(kmp_int32 gtid, kmp_task_t *task,
671 kmp_taskdata_t *current_task) {
672 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
673 kmp_info_t *thread = __kmp_threads[gtid];
676 (
"__kmp_task_start(enter): T#%d starting task %p: current_task=%p\n",
677 gtid, taskdata, current_task));
679 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
684 current_task->td_flags.executing = 0;
687#ifdef BUILD_TIED_TASK_STACK
688 if (taskdata->td_flags.tiedness == TASK_TIED) {
689 __kmp_push_task_stack(gtid, thread, taskdata);
694 thread->th.th_current_task = taskdata;
696 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 0 ||
697 taskdata->td_flags.tiedness == TASK_UNTIED);
698 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0 ||
699 taskdata->td_flags.tiedness == TASK_UNTIED);
700 taskdata->td_flags.started = 1;
701 taskdata->td_flags.executing = 1;
702 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
703 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
710 KA_TRACE(10, (
"__kmp_task_start(exit): T#%d task=%p\n", gtid, taskdata));
721static inline void __ompt_task_init(kmp_taskdata_t *task,
int tid) {
723 task->ompt_task_info.task_data.value = 0;
724 task->ompt_task_info.frame.exit_frame = ompt_data_none;
725 task->ompt_task_info.frame.enter_frame = ompt_data_none;
726 task->ompt_task_info.frame.exit_frame_flags =
727 ompt_frame_runtime | ompt_frame_framepointer;
728 task->ompt_task_info.frame.enter_frame_flags =
729 ompt_frame_runtime | ompt_frame_framepointer;
730 task->ompt_task_info.dispatch_chunk.start = 0;
731 task->ompt_task_info.dispatch_chunk.iterations = 0;
736static inline void __ompt_task_start(kmp_task_t *task,
737 kmp_taskdata_t *current_task,
739 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
740 ompt_task_status_t status = ompt_task_switch;
741 if (__kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded) {
742 status = ompt_task_yield;
743 __kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded = 0;
746 if (ompt_enabled.ompt_callback_task_schedule) {
747 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
748 &(current_task->ompt_task_info.task_data), status,
749 &(taskdata->ompt_task_info.task_data));
751 taskdata->ompt_task_info.scheduling_parent = current_task;
756static inline void __ompt_task_finish(kmp_task_t *task,
757 kmp_taskdata_t *resumed_task,
758 ompt_task_status_t status) {
759 if (ompt_enabled.ompt_callback_task_schedule) {
760 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
761 if (__kmp_omp_cancellation && taskdata->td_taskgroup &&
762 taskdata->td_taskgroup->cancel_request == cancel_taskgroup) {
763 status = ompt_task_cancel;
767 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
768 &(taskdata->ompt_task_info.task_data), status,
769 (resumed_task ? &(resumed_task->ompt_task_info.task_data) : NULL));
775static void __kmpc_omp_task_begin_if0_template(
ident_t *loc_ref, kmp_int32 gtid,
778 void *return_address) {
779 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
780 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
782 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(enter): T#%d loc=%p task=%p "
784 gtid, loc_ref, taskdata, current_task));
786 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
789 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
790 KMP_DEBUG_USE_VAR(counter);
791 KA_TRACE(20, (
"__kmpc_omp_task_begin_if0: T#%d untied_count (%d) "
792 "incremented for task %p\n",
793 gtid, counter, taskdata));
796 taskdata->td_flags.task_serial =
798 __kmp_task_start(gtid, task, current_task);
802 if (current_task->ompt_task_info.frame.enter_frame.ptr == NULL) {
803 current_task->ompt_task_info.frame.enter_frame.ptr =
804 taskdata->ompt_task_info.frame.exit_frame.ptr = frame_address;
805 current_task->ompt_task_info.frame.enter_frame_flags =
806 taskdata->ompt_task_info.frame.exit_frame_flags =
807 ompt_frame_application | ompt_frame_framepointer;
809 if (ompt_enabled.ompt_callback_task_create) {
810 ompt_task_info_t *parent_info = &(current_task->ompt_task_info);
811 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
812 &(parent_info->task_data), &(parent_info->frame),
813 &(taskdata->ompt_task_info.task_data),
814 TASK_TYPE_DETAILS_FORMAT(taskdata), 0, return_address);
816 __ompt_task_start(task, current_task, gtid);
820 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(exit): T#%d loc=%p task=%p,\n", gtid,
826static void __kmpc_omp_task_begin_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
829 void *return_address) {
830 __kmpc_omp_task_begin_if0_template<true>(loc_ref, gtid, task, frame_address,
847__attribute__((target(
"backchain")))
849void __kmpc_omp_task_begin_if0(
ident_t *loc_ref, kmp_int32 gtid,
852 if (UNLIKELY(ompt_enabled.enabled)) {
853 OMPT_STORE_RETURN_ADDRESS(gtid);
854 __kmpc_omp_task_begin_if0_ompt(loc_ref, gtid, task,
855 OMPT_GET_FRAME_ADDRESS(1),
856 OMPT_LOAD_RETURN_ADDRESS(gtid));
860 __kmpc_omp_task_begin_if0_template<false>(loc_ref, gtid, task, NULL, NULL);
866void __kmpc_omp_task_begin(
ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task) {
867 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
871 (
"__kmpc_omp_task_begin(enter): T#%d loc=%p task=%p current_task=%p\n",
872 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task), current_task));
874 __kmp_task_start(gtid, task, current_task);
876 KA_TRACE(10, (
"__kmpc_omp_task_begin(exit): T#%d loc=%p task=%p,\n", gtid,
877 loc_ref, KMP_TASK_TO_TASKDATA(task)));
887static void __kmp_free_task(kmp_int32 gtid, kmp_taskdata_t *taskdata,
888 kmp_info_t *thread) {
889 KA_TRACE(30, (
"__kmp_free_task: T#%d freeing data from task %p\n", gtid,
893 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
894 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0);
895 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 1);
896 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
897 KMP_DEBUG_ASSERT(taskdata->td_allocated_child_tasks == 0 ||
898 taskdata->td_flags.task_serial == 1);
899 KMP_DEBUG_ASSERT(taskdata->td_incomplete_child_tasks == 0);
900 kmp_task_t *task = KMP_TASKDATA_TO_TASK(taskdata);
902 task->data1.destructors = NULL;
903 task->data2.priority = 0;
905 taskdata->td_flags.freed = 1;
908 if (!taskdata->is_taskgraph) {
912 __kmp_fast_free(thread, taskdata);
914 __kmp_thread_free(thread, taskdata);
918 taskdata->td_flags.complete = 0;
919 taskdata->td_flags.started = 0;
920 taskdata->td_flags.freed = 0;
921 taskdata->td_flags.executing = 0;
922 taskdata->td_flags.task_serial =
923 (taskdata->td_parent->td_flags.final ||
924 taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser);
927 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
928 KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0);
930 KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1);
934 KA_TRACE(20, (
"__kmp_free_task: T#%d freed task %p\n", gtid, taskdata));
943static void __kmp_free_task_and_ancestors(kmp_int32 gtid,
944 kmp_taskdata_t *taskdata,
945 kmp_info_t *thread) {
948 kmp_int32 team_serial =
949 (taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) &&
950 !taskdata->td_flags.proxy;
951 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
953 kmp_int32 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
954 KMP_DEBUG_ASSERT(children >= 0);
957 while (children == 0) {
958 kmp_taskdata_t *parent_taskdata = taskdata->td_parent;
960 KA_TRACE(20, (
"__kmp_free_task_and_ancestors(enter): T#%d task %p complete "
961 "and freeing itself\n",
965 __kmp_free_task(gtid, taskdata, thread);
967 taskdata = parent_taskdata;
973 if (taskdata->td_flags.tasktype == TASK_IMPLICIT) {
974 if (taskdata->td_dephash) {
975 int children = KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks);
976 kmp_tasking_flags_t flags_old = taskdata->td_flags;
977 if (children == 0 && flags_old.complete == 1) {
978 kmp_tasking_flags_t flags_new = flags_old;
979 flags_new.complete = 0;
980 if (KMP_COMPARE_AND_STORE_ACQ32(
981 RCAST(kmp_int32 *, &taskdata->td_flags),
982 *RCAST(kmp_int32 *, &flags_old),
983 *RCAST(kmp_int32 *, &flags_new))) {
984 KA_TRACE(100, (
"__kmp_free_task_and_ancestors: T#%d cleans "
985 "dephash of implicit task %p\n",
988 __kmp_dephash_free_entries(thread, taskdata->td_dephash);
995 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
996 KMP_DEBUG_ASSERT(children >= 0);
1000 20, (
"__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; "
1001 "not freeing it yet\n",
1002 gtid, taskdata, children));
1013static bool __kmp_track_children_task(kmp_taskdata_t *taskdata) {
1014 kmp_tasking_flags_t flags = taskdata->td_flags;
1015 bool ret = !(flags.team_serial || flags.tasking_ser);
1016 ret = ret || flags.proxy == TASK_PROXY ||
1017 flags.detachable == TASK_DETACHABLE || flags.hidden_helper;
1019 KMP_ATOMIC_LD_ACQ(&taskdata->td_parent->td_incomplete_child_tasks) > 0;
1021 if (taskdata->td_taskgroup && taskdata->is_taskgraph)
1022 ret = ret || KMP_ATOMIC_LD_ACQ(&taskdata->td_taskgroup->count) > 0;
1037static void __kmp_task_finish(kmp_int32 gtid, kmp_task_t *task,
1038 kmp_taskdata_t *resumed_task) {
1039 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
1040 kmp_info_t *thread = __kmp_threads[gtid];
1041 kmp_task_team_t *task_team =
1042 thread->th.th_task_team;
1048 kmp_int32 children = 0;
1050 KA_TRACE(10, (
"__kmp_task_finish(enter): T#%d finishing task %p and resuming "
1052 gtid, taskdata, resumed_task));
1054 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
1057 is_taskgraph = taskdata->is_taskgraph;
1061#ifdef BUILD_TIED_TASK_STACK
1062 if (taskdata->td_flags.tiedness == TASK_TIED) {
1063 __kmp_pop_task_stack(gtid, thread, taskdata);
1067 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
1070 kmp_int32 counter = KMP_ATOMIC_DEC(&taskdata->td_untied_count) - 1;
1073 (
"__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n",
1074 gtid, counter, taskdata));
1078 if (resumed_task == NULL) {
1079 KMP_DEBUG_ASSERT(taskdata->td_flags.task_serial);
1080 resumed_task = taskdata->td_parent;
1083 thread->th.th_current_task = resumed_task;
1084 resumed_task->td_flags.executing = 1;
1085 KA_TRACE(10, (
"__kmp_task_finish(exit): T#%d partially done task %p, "
1086 "resuming task %p\n",
1087 gtid, taskdata, resumed_task));
1095 (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) ==
1096 taskdata->td_flags.task_serial);
1097 if (taskdata->td_flags.task_serial) {
1098 if (resumed_task == NULL) {
1099 resumed_task = taskdata->td_parent;
1103 KMP_DEBUG_ASSERT(resumed_task !=
1113 if (UNLIKELY(taskdata->td_flags.destructors_thunk)) {
1114 kmp_routine_entry_t destr_thunk = task->data1.destructors;
1115 KMP_ASSERT(destr_thunk);
1116 destr_thunk(gtid, task);
1119 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
1120 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 1);
1121 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
1123 bool completed =
true;
1124 if (UNLIKELY(taskdata->td_flags.detachable == TASK_DETACHABLE)) {
1125 if (taskdata->td_allow_completion_event.type ==
1126 KMP_EVENT_ALLOW_COMPLETION) {
1128 __kmp_acquire_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
1129 if (taskdata->td_allow_completion_event.type ==
1130 KMP_EVENT_ALLOW_COMPLETION) {
1132 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
1133 taskdata->td_flags.executing = 0;
1140 __ompt_task_finish(task, resumed_task, ompt_task_detach);
1146 taskdata->td_flags.proxy = TASK_PROXY;
1149 __kmp_release_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
1154 if (taskdata->td_target_data.async_handle != NULL) {
1160 __ompt_task_finish(task, resumed_task, ompt_task_switch);
1163 __kmpc_give_task(task, __kmp_tid_from_gtid(gtid));
1164 if (KMP_HIDDEN_HELPER_THREAD(gtid))
1165 __kmp_hidden_helper_worker_thread_signal();
1170 taskdata->td_flags.complete = 1;
1172 taskdata->td_flags.onced = 1;
1178 __ompt_task_finish(task, resumed_task, ompt_task_complete);
1182 if (__kmp_track_children_task(taskdata)) {
1183 __kmp_release_deps(gtid, taskdata);
1188 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks);
1189 KMP_DEBUG_ASSERT(children >= 0);
1191 if (taskdata->td_taskgroup && !taskdata->is_taskgraph)
1193 if (taskdata->td_taskgroup)
1195 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
1196 }
else if (task_team && (task_team->tt.tt_found_proxy_tasks ||
1197 task_team->tt.tt_hidden_helper_task_encountered)) {
1200 __kmp_release_deps(gtid, taskdata);
1206 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
1207 taskdata->td_flags.executing = 0;
1210 if (taskdata->td_flags.hidden_helper) {
1212 KMP_ASSERT(KMP_HIDDEN_HELPER_THREAD(gtid));
1213 KMP_ATOMIC_DEC(&__kmp_unexecuted_hidden_helper_tasks);
1218 20, (
"__kmp_task_finish: T#%d finished task %p, %d incomplete children\n",
1219 gtid, taskdata, children));
1225 thread->th.th_current_task = resumed_task;
1227 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
1231 resumed_task->td_flags.executing = 1;
1234 if (is_taskgraph && __kmp_track_children_task(taskdata) &&
1235 taskdata->td_taskgroup) {
1242 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
1247 10, (
"__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n",
1248 gtid, taskdata, resumed_task));
1254static void __kmpc_omp_task_complete_if0_template(
ident_t *loc_ref,
1257 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n",
1258 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
1259 KMP_DEBUG_ASSERT(gtid >= 0);
1261 __kmp_task_finish<ompt>(gtid, task, NULL);
1263 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n",
1264 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
1268 ompt_frame_t *ompt_frame;
1269 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
1270 ompt_frame->enter_frame = ompt_data_none;
1271 ompt_frame->enter_frame_flags =
1272 ompt_frame_runtime | ompt_frame_framepointer;
1281void __kmpc_omp_task_complete_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
1283 __kmpc_omp_task_complete_if0_template<true>(loc_ref, gtid, task);
1292void __kmpc_omp_task_complete_if0(
ident_t *loc_ref, kmp_int32 gtid,
1295 if (UNLIKELY(ompt_enabled.enabled)) {
1296 __kmpc_omp_task_complete_if0_ompt(loc_ref, gtid, task);
1300 __kmpc_omp_task_complete_if0_template<false>(loc_ref, gtid, task);
1306void __kmpc_omp_task_complete(
ident_t *loc_ref, kmp_int32 gtid,
1308 KA_TRACE(10, (
"__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n", gtid,
1309 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1311 __kmp_task_finish<false>(gtid, task,
1314 KA_TRACE(10, (
"__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n", gtid,
1315 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1331void __kmp_init_implicit_task(
ident_t *loc_ref, kmp_info_t *this_thr,
1332 kmp_team_t *team,
int tid,
int set_curr_task) {
1333 kmp_taskdata_t *task = &team->t.t_implicit_task_taskdata[tid];
1337 (
"__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n",
1338 tid, team, task, set_curr_task ?
"TRUE" :
"FALSE"));
1340 task->td_task_id = KMP_GEN_TASK_ID();
1341 task->td_team = team;
1344 task->td_ident = loc_ref;
1345 task->td_taskwait_ident = NULL;
1346 task->td_taskwait_counter = 0;
1347 task->td_taskwait_thread = 0;
1349 task->td_flags.tiedness = TASK_TIED;
1350 task->td_flags.tasktype = TASK_IMPLICIT;
1351 task->td_flags.proxy = TASK_FULL;
1354 task->td_flags.task_serial = 1;
1355 task->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1356 task->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1358 task->td_flags.started = 1;
1359 task->td_flags.executing = 1;
1360 task->td_flags.complete = 0;
1361 task->td_flags.freed = 0;
1363 task->td_flags.onced = 0;
1366 task->td_depnode = NULL;
1367 task->td_last_tied = task;
1368 task->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1370 if (set_curr_task) {
1371 KMP_ATOMIC_ST_REL(&task->td_incomplete_child_tasks, 0);
1373 KMP_ATOMIC_ST_REL(&task->td_allocated_child_tasks, 0);
1374 task->td_taskgroup = NULL;
1375 task->td_dephash = NULL;
1376 __kmp_push_current_task_to_thread(this_thr, team, tid);
1378 KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0);
1379 KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0);
1383 if (UNLIKELY(ompt_enabled.enabled))
1384 __ompt_task_init(task, tid);
1387 KF_TRACE(10, (
"__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n", tid,
1396void __kmp_finish_implicit_task(kmp_info_t *thread) {
1397 kmp_taskdata_t *task = thread->th.th_current_task;
1398 if (task->td_dephash) {
1400 task->td_flags.complete = 1;
1402 task->td_flags.onced = 1;
1404 children = KMP_ATOMIC_LD_ACQ(&task->td_incomplete_child_tasks);
1405 kmp_tasking_flags_t flags_old = task->td_flags;
1406 if (children == 0 && flags_old.complete == 1) {
1407 kmp_tasking_flags_t flags_new = flags_old;
1408 flags_new.complete = 0;
1409 if (KMP_COMPARE_AND_STORE_ACQ32(RCAST(kmp_int32 *, &task->td_flags),
1410 *RCAST(kmp_int32 *, &flags_old),
1411 *RCAST(kmp_int32 *, &flags_new))) {
1412 KA_TRACE(100, (
"__kmp_finish_implicit_task: T#%d cleans "
1413 "dephash of implicit task %p\n",
1414 thread->th.th_info.ds.ds_gtid, task));
1415 __kmp_dephash_free_entries(thread, task->td_dephash);
1425void __kmp_free_implicit_task(kmp_info_t *thread) {
1426 kmp_taskdata_t *task = thread->th.th_current_task;
1427 if (task && task->td_dephash) {
1428 __kmp_dephash_free(thread, task->td_dephash);
1429 task->td_dephash = NULL;
1435static size_t __kmp_round_up_to_val(
size_t size,
size_t val) {
1436 if (size & (val - 1)) {
1438 if (size <= KMP_SIZE_T_MAX - val) {
1457kmp_task_t *__kmp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1458 kmp_tasking_flags_t *flags,
1459 size_t sizeof_kmp_task_t,
size_t sizeof_shareds,
1460 kmp_routine_entry_t task_entry) {
1462 kmp_taskdata_t *taskdata;
1463 kmp_info_t *thread = __kmp_threads[gtid];
1464 kmp_team_t *team = thread->th.th_team;
1465 kmp_taskdata_t *parent_task = thread->th.th_current_task;
1466 size_t shareds_offset;
1468 if (UNLIKELY(!TCR_4(__kmp_init_middle)))
1469 __kmp_middle_initialize();
1471 if (flags->hidden_helper) {
1472 if (__kmp_enable_hidden_helper) {
1473 if (!TCR_4(__kmp_init_hidden_helper))
1474 __kmp_hidden_helper_initialize();
1477 flags->hidden_helper = FALSE;
1481 KA_TRACE(10, (
"__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) "
1482 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1483 gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t,
1484 sizeof_shareds, task_entry));
1486 KMP_DEBUG_ASSERT(parent_task);
1487 if (parent_task->td_flags.final) {
1488 if (flags->merged_if0) {
1493 if (flags->tiedness == TASK_UNTIED && !team->t.t_serialized) {
1497 KMP_CHECK_UPDATE(thread->th.th_task_team->tt.tt_untied_task_encountered, 1);
1503 if (UNLIKELY(flags->proxy == TASK_PROXY ||
1504 flags->detachable == TASK_DETACHABLE || flags->hidden_helper)) {
1505 if (flags->proxy == TASK_PROXY) {
1506 flags->tiedness = TASK_UNTIED;
1507 flags->merged_if0 = 1;
1511 if ((thread->th.th_task_team) == NULL) {
1514 KMP_DEBUG_ASSERT(team->t.t_serialized);
1516 (
"T#%d creating task team in __kmp_task_alloc for proxy task\n",
1518 __kmp_task_team_setup(thread, team);
1519 thread->th.th_task_team = team->t.t_task_team[thread->th.th_task_state];
1521 kmp_task_team_t *task_team = thread->th.th_task_team;
1524 if (!KMP_TASKING_ENABLED(task_team)) {
1527 (
"T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid));
1528 __kmp_enable_tasking(task_team, thread);
1529 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
1530 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
1532 if (thread_data->td.td_deque == NULL) {
1533 __kmp_alloc_task_deque(thread, thread_data);
1537 if ((flags->proxy == TASK_PROXY || flags->detachable == TASK_DETACHABLE) &&
1538 task_team->tt.tt_found_proxy_tasks == FALSE)
1539 TCW_4(task_team->tt.tt_found_proxy_tasks, TRUE);
1540 if (flags->hidden_helper &&
1541 task_team->tt.tt_hidden_helper_task_encountered == FALSE)
1542 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, TRUE);
1547 shareds_offset =
sizeof(kmp_taskdata_t) + sizeof_kmp_task_t;
1548 shareds_offset = __kmp_round_up_to_val(shareds_offset,
sizeof(
void *));
1551 KA_TRACE(30, (
"__kmp_task_alloc: T#%d First malloc size: %ld\n", gtid,
1553 KA_TRACE(30, (
"__kmp_task_alloc: T#%d Second malloc size: %ld\n", gtid,
1558 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, shareds_offset +
1561 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, shareds_offset +
1565 task = KMP_TASKDATA_TO_TASK(taskdata);
1568#if KMP_ARCH_X86 || KMP_ARCH_PPC64 || KMP_ARCH_S390X || !KMP_HAVE_QUAD
1569 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(
double) - 1)) == 0);
1570 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(
double) - 1)) == 0);
1572 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(_Quad) - 1)) == 0);
1573 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(_Quad) - 1)) == 0);
1575 if (sizeof_shareds > 0) {
1577 task->shareds = &((
char *)taskdata)[shareds_offset];
1579 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
1582 task->shareds = NULL;
1584 task->routine = task_entry;
1587 taskdata->td_task_id = KMP_GEN_TASK_ID();
1588 taskdata->td_team = thread->th.th_team;
1589 taskdata->td_alloc_thread = thread;
1590 taskdata->td_parent = parent_task;
1591 taskdata->td_level = parent_task->td_level + 1;
1592 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
1593 taskdata->td_ident = loc_ref;
1594 taskdata->td_taskwait_ident = NULL;
1595 taskdata->td_taskwait_counter = 0;
1596 taskdata->td_taskwait_thread = 0;
1597 KMP_DEBUG_ASSERT(taskdata->td_parent != NULL);
1599 if (flags->proxy == TASK_FULL)
1600 copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs);
1602 taskdata->td_flags = *flags;
1603 taskdata->td_task_team = thread->th.th_task_team;
1604 taskdata->td_size_alloc = shareds_offset + sizeof_shareds;
1605 taskdata->td_flags.tasktype = TASK_EXPLICIT;
1608 if (flags->hidden_helper) {
1609 kmp_info_t *shadow_thread = __kmp_threads[KMP_GTID_TO_SHADOW_GTID(gtid)];
1610 taskdata->td_team = shadow_thread->th.th_team;
1611 taskdata->td_task_team = shadow_thread->th.th_task_team;
1615 taskdata->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1618 taskdata->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1624 taskdata->td_flags.task_serial =
1625 (parent_task->td_flags.final || taskdata->td_flags.team_serial ||
1626 taskdata->td_flags.tasking_ser || flags->merged_if0);
1628 taskdata->td_flags.started = 0;
1629 taskdata->td_flags.executing = 0;
1630 taskdata->td_flags.complete = 0;
1631 taskdata->td_flags.freed = 0;
1633 taskdata->td_flags.onced = 0;
1635 KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0);
1637 KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1);
1638 taskdata->td_taskgroup =
1639 parent_task->td_taskgroup;
1640 taskdata->td_dephash = NULL;
1641 taskdata->td_depnode = NULL;
1642 taskdata->td_target_data.async_handle = NULL;
1643 if (flags->tiedness == TASK_UNTIED)
1644 taskdata->td_last_tied = NULL;
1646 taskdata->td_last_tied = taskdata;
1647 taskdata->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1649 if (UNLIKELY(ompt_enabled.enabled))
1650 __ompt_task_init(taskdata, gtid);
1654 if (__kmp_track_children_task(taskdata)) {
1655 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
1656 if (parent_task->td_taskgroup)
1657 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
1660 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) {
1661 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
1663 if (flags->hidden_helper) {
1664 taskdata->td_flags.task_serial = FALSE;
1666 KMP_ATOMIC_INC(&__kmp_unexecuted_hidden_helper_tasks);
1671 kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_idx);
1672 if (tdg && __kmp_tdg_is_recording(tdg->tdg_status) &&
1673 (task_entry != (kmp_routine_entry_t)__kmp_taskloop_task)) {
1674 taskdata->is_taskgraph = 1;
1675 taskdata->tdg = __kmp_global_tdgs[__kmp_curr_tdg_idx];
1676 taskdata->td_task_id = KMP_ATOMIC_INC(&__kmp_tdg_task_id);
1679 KA_TRACE(20, (
"__kmp_task_alloc(exit): T#%d created task %p parent=%p\n",
1680 gtid, taskdata, taskdata->td_parent));
1685kmp_task_t *__kmpc_omp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1686 kmp_int32 flags,
size_t sizeof_kmp_task_t,
1687 size_t sizeof_shareds,
1688 kmp_routine_entry_t task_entry) {
1690 kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
1691 __kmp_assert_valid_gtid(gtid);
1692 input_flags->native = FALSE;
1694 KA_TRACE(10, (
"__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s %s) "
1695 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1696 gtid, loc_ref, input_flags->tiedness ?
"tied " :
"untied",
1697 input_flags->proxy ?
"proxy" :
"",
1698 input_flags->detachable ?
"detachable" :
"", sizeof_kmp_task_t,
1699 sizeof_shareds, task_entry));
1701 retval = __kmp_task_alloc(loc_ref, gtid, input_flags, sizeof_kmp_task_t,
1702 sizeof_shareds, task_entry);
1704 KA_TRACE(20, (
"__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval));
1709kmp_task_t *__kmpc_omp_target_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1711 size_t sizeof_kmp_task_t,
1712 size_t sizeof_shareds,
1713 kmp_routine_entry_t task_entry,
1714 kmp_int64 device_id) {
1715 auto &input_flags =
reinterpret_cast<kmp_tasking_flags_t &
>(flags);
1717 input_flags.tiedness = TASK_UNTIED;
1718 input_flags.target = 1;
1720 if (__kmp_enable_hidden_helper)
1721 input_flags.hidden_helper = TRUE;
1723 return __kmpc_omp_task_alloc(loc_ref, gtid, flags, sizeof_kmp_task_t,
1724 sizeof_shareds, task_entry);
1742 kmp_task_t *new_task, kmp_int32 naffins,
1743 kmp_task_affinity_info_t *affin_list) {
1753__attribute__((target(
"backchain")))
1756__kmp_invoke_task(kmp_int32 gtid, kmp_task_t *task,
1757 kmp_taskdata_t *current_task) {
1758 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
1762 30, (
"__kmp_invoke_task(enter): T#%d invoking task %p, current_task=%p\n",
1763 gtid, taskdata, current_task));
1764 KMP_DEBUG_ASSERT(task);
1765 if (UNLIKELY(taskdata->td_flags.proxy == TASK_PROXY &&
1766 taskdata->td_flags.complete == 1)) {
1771 (
"__kmp_invoke_task: T#%d running bottom finish for proxy task %p\n",
1774 __kmp_bottom_half_finish_proxy(gtid, task);
1776 KA_TRACE(30, (
"__kmp_invoke_task(exit): T#%d completed bottom finish for "
1777 "proxy task %p, resuming task %p\n",
1778 gtid, taskdata, current_task));
1786 ompt_thread_info_t oldInfo;
1787 if (UNLIKELY(ompt_enabled.enabled)) {
1789 thread = __kmp_threads[gtid];
1790 oldInfo = thread->th.ompt_thread_info;
1791 thread->th.ompt_thread_info.wait_id = 0;
1792 thread->th.ompt_thread_info.state = (thread->th.th_team_serialized)
1793 ? ompt_state_work_serial
1794 : ompt_state_work_parallel;
1795 taskdata->ompt_task_info.frame.exit_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1800 if (taskdata->td_flags.proxy != TASK_PROXY) {
1801 __kmp_task_start(gtid, task, current_task);
1807 if (UNLIKELY(__kmp_omp_cancellation)) {
1808 thread = __kmp_threads[gtid];
1809 kmp_team_t *this_team = thread->th.th_team;
1810 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
1811 if ((taskgroup && taskgroup->cancel_request) ||
1812 (this_team->t.t_cancel_request == cancel_parallel)) {
1813#if OMPT_SUPPORT && OMPT_OPTIONAL
1814 ompt_data_t *task_data;
1815 if (UNLIKELY(ompt_enabled.ompt_callback_cancel)) {
1816 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, NULL, NULL);
1817 ompt_callbacks.ompt_callback(ompt_callback_cancel)(
1819 ((taskgroup && taskgroup->cancel_request) ? ompt_cancel_taskgroup
1820 : ompt_cancel_parallel) |
1821 ompt_cancel_discarded_task,
1834 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
1835 taskdata->td_last_tied = current_task->td_last_tied;
1836 KMP_DEBUG_ASSERT(taskdata->td_last_tied);
1838#if KMP_STATS_ENABLED
1840 switch (KMP_GET_THREAD_STATE()) {
1841 case FORK_JOIN_BARRIER:
1842 KMP_PUSH_PARTITIONED_TIMER(OMP_task_join_bar);
1845 KMP_PUSH_PARTITIONED_TIMER(OMP_task_plain_bar);
1848 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskyield);
1851 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskwait);
1854 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskgroup);
1857 KMP_PUSH_PARTITIONED_TIMER(OMP_task_immediate);
1864 if (UNLIKELY(ompt_enabled.enabled))
1865 __ompt_task_start(task, current_task, gtid);
1867#if OMPT_SUPPORT && OMPT_OPTIONAL
1868 if (UNLIKELY(ompt_enabled.ompt_callback_dispatch &&
1869 taskdata->ompt_task_info.dispatch_chunk.iterations > 0)) {
1870 ompt_data_t instance = ompt_data_none;
1871 instance.ptr = &(taskdata->ompt_task_info.dispatch_chunk);
1872 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
1873 ompt_callbacks.ompt_callback(ompt_callback_dispatch)(
1874 &(team_info->parallel_data), &(taskdata->ompt_task_info.task_data),
1875 ompt_dispatch_taskloop_chunk, instance);
1876 taskdata->ompt_task_info.dispatch_chunk = {0, 0};
1881 if (ompd_state & OMPD_ENABLE_BP)
1882 ompd_bp_task_begin();
1885#if USE_ITT_BUILD && USE_ITT_NOTIFY
1886 kmp_uint64 cur_time;
1887 kmp_int32 kmp_itt_count_task =
1888 __kmp_forkjoin_frames_mode == 3 && !taskdata->td_flags.task_serial &&
1889 current_task->td_flags.tasktype == TASK_IMPLICIT;
1890 if (kmp_itt_count_task) {
1891 thread = __kmp_threads[gtid];
1893 if (thread->th.th_bar_arrive_time)
1894 cur_time = __itt_get_timestamp();
1896 kmp_itt_count_task = 0;
1898 KMP_FSYNC_ACQUIRED(taskdata);
1901#if ENABLE_LIBOMPTARGET
1902 if (taskdata->td_target_data.async_handle != NULL) {
1906 KMP_ASSERT(tgt_target_nowait_query);
1907 tgt_target_nowait_query(&taskdata->td_target_data.async_handle);
1910 if (task->routine != NULL) {
1911#ifdef KMP_GOMP_COMPAT
1912 if (taskdata->td_flags.native) {
1913 ((void (*)(
void *))(*(task->routine)))(task->shareds);
1917 (*(task->routine))(gtid, task);
1920 KMP_POP_PARTITIONED_TIMER();
1922#if USE_ITT_BUILD && USE_ITT_NOTIFY
1923 if (kmp_itt_count_task) {
1925 thread->th.th_bar_arrive_time += (__itt_get_timestamp() - cur_time);
1927 KMP_FSYNC_CANCEL(taskdata);
1928 KMP_FSYNC_RELEASING(taskdata->td_parent);
1933 if (ompd_state & OMPD_ENABLE_BP)
1938 if (taskdata->td_flags.proxy != TASK_PROXY) {
1940 if (UNLIKELY(ompt_enabled.enabled)) {
1941 thread->th.ompt_thread_info = oldInfo;
1942 if (taskdata->td_flags.tiedness == TASK_TIED) {
1943 taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
1945 __kmp_task_finish<true>(gtid, task, current_task);
1948 __kmp_task_finish<false>(gtid, task, current_task);
1951 else if (UNLIKELY(ompt_enabled.enabled && taskdata->td_flags.target)) {
1952 __ompt_task_finish(task, current_task, ompt_task_switch);
1958 (
"__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n",
1959 gtid, taskdata, current_task));
1973kmp_int32 __kmpc_omp_task_parts(
ident_t *loc_ref, kmp_int32 gtid,
1974 kmp_task_t *new_task) {
1975 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1977 KA_TRACE(10, (
"__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n", gtid,
1978 loc_ref, new_taskdata));
1981 kmp_taskdata_t *parent;
1982 if (UNLIKELY(ompt_enabled.enabled)) {
1983 parent = new_taskdata->td_parent;
1984 if (ompt_enabled.ompt_callback_task_create) {
1985 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1986 &(parent->ompt_task_info.task_data), &(parent->ompt_task_info.frame),
1987 &(new_taskdata->ompt_task_info.task_data),
1988 TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1989 OMPT_GET_RETURN_ADDRESS(0));
1997 if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1999 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
2000 new_taskdata->td_flags.task_serial = 1;
2001 __kmp_invoke_task(gtid, new_task, current_task);
2006 (
"__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: "
2007 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
2008 gtid, loc_ref, new_taskdata));
2011 if (UNLIKELY(ompt_enabled.enabled)) {
2012 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
2015 return TASK_CURRENT_NOT_QUEUED;
2029kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task,
2030 bool serialize_immediate) {
2031 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
2034 if (new_taskdata->is_taskgraph &&
2035 __kmp_tdg_is_recording(new_taskdata->tdg->tdg_status)) {
2036 kmp_tdg_info_t *tdg = new_taskdata->tdg;
2038 if (new_taskdata->td_task_id >= new_taskdata->tdg->map_size) {
2039 __kmp_acquire_bootstrap_lock(&tdg->graph_lock);
2042 if (new_taskdata->td_task_id >= tdg->map_size) {
2043 kmp_uint old_size = tdg->map_size;
2044 kmp_uint new_size = old_size * 2;
2045 kmp_node_info_t *old_record = tdg->record_map;
2046 kmp_node_info_t *new_record = (kmp_node_info_t *)__kmp_allocate(
2047 new_size *
sizeof(kmp_node_info_t));
2049 KMP_MEMCPY(new_record, old_record, old_size *
sizeof(kmp_node_info_t));
2050 tdg->record_map = new_record;
2052 __kmp_free(old_record);
2054 for (kmp_int i = old_size; i < new_size; i++) {
2055 kmp_int32 *successorsList = (kmp_int32 *)__kmp_allocate(
2056 __kmp_successors_size *
sizeof(kmp_int32));
2057 new_record[i].task =
nullptr;
2058 new_record[i].successors = successorsList;
2059 new_record[i].nsuccessors = 0;
2060 new_record[i].npredecessors = 0;
2061 new_record[i].successors_size = __kmp_successors_size;
2062 KMP_ATOMIC_ST_REL(&new_record[i].npredecessors_counter, 0);
2066 tdg->map_size = new_size;
2068 __kmp_release_bootstrap_lock(&tdg->graph_lock);
2071 if (tdg->record_map[new_taskdata->td_task_id].task ==
nullptr) {
2072 tdg->record_map[new_taskdata->td_task_id].task = new_task;
2073 tdg->record_map[new_taskdata->td_task_id].parent_task =
2074 new_taskdata->td_parent;
2075 KMP_ATOMIC_INC(&tdg->num_tasks);
2082 if (new_taskdata->td_flags.proxy == TASK_PROXY ||
2083 __kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
2085 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
2086 if (serialize_immediate)
2087 new_taskdata->td_flags.task_serial = 1;
2088 __kmp_invoke_task(gtid, new_task, current_task);
2089 }
else if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME &&
2090 __kmp_wpolicy_passive) {
2091 kmp_info_t *this_thr = __kmp_threads[gtid];
2092 kmp_team_t *team = this_thr->th.th_team;
2093 kmp_int32 nthreads = this_thr->th.th_team_nproc;
2094 for (
int i = 0; i < nthreads; ++i) {
2095 kmp_info_t *thread = team->t.t_threads[i];
2096 if (thread == this_thr)
2098 if (thread->th.th_sleep_loc != NULL) {
2099 __kmp_null_resume_wrapper(thread);
2104 return TASK_CURRENT_NOT_QUEUED;
2119kmp_int32 __kmpc_omp_task(
ident_t *loc_ref, kmp_int32 gtid,
2120 kmp_task_t *new_task) {
2122 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
2124#if KMP_DEBUG || OMPT_SUPPORT
2125 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
2127 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
2129 __kmp_assert_valid_gtid(gtid);
2132 kmp_taskdata_t *parent = NULL;
2133 if (UNLIKELY(ompt_enabled.enabled)) {
2134 if (!new_taskdata->td_flags.started) {
2135 OMPT_STORE_RETURN_ADDRESS(gtid);
2136 parent = new_taskdata->td_parent;
2137 if (!parent->ompt_task_info.frame.enter_frame.ptr) {
2138 parent->ompt_task_info.frame.enter_frame.ptr =
2139 OMPT_GET_FRAME_ADDRESS(0);
2141 if (ompt_enabled.ompt_callback_task_create) {
2142 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
2143 &(parent->ompt_task_info.task_data),
2144 &(parent->ompt_task_info.frame),
2145 &(new_taskdata->ompt_task_info.task_data),
2146 TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
2147 OMPT_LOAD_RETURN_ADDRESS(gtid));
2152 __ompt_task_finish(new_task,
2153 new_taskdata->ompt_task_info.scheduling_parent,
2155 new_taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
2160 res = __kmp_omp_task(gtid, new_task,
true);
2162 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
2163 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
2164 gtid, loc_ref, new_taskdata));
2166 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
2167 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
2186kmp_int32 __kmp_omp_taskloop_task(
ident_t *loc_ref, kmp_int32 gtid,
2187 kmp_task_t *new_task,
void *codeptr_ra) {
2189 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
2191#if KMP_DEBUG || OMPT_SUPPORT
2192 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
2194 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
2198 kmp_taskdata_t *parent = NULL;
2199 if (UNLIKELY(ompt_enabled.enabled && !new_taskdata->td_flags.started)) {
2200 parent = new_taskdata->td_parent;
2201 if (!parent->ompt_task_info.frame.enter_frame.ptr)
2202 parent->ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
2203 if (ompt_enabled.ompt_callback_task_create) {
2204 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
2205 &(parent->ompt_task_info.task_data), &(parent->ompt_task_info.frame),
2206 &(new_taskdata->ompt_task_info.task_data),
2207 TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0, codeptr_ra);
2212 res = __kmp_omp_task(gtid, new_task,
true);
2214 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
2215 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
2216 gtid, loc_ref, new_taskdata));
2218 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
2219 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
2226static kmp_int32 __kmpc_omp_taskwait_template(
ident_t *loc_ref, kmp_int32 gtid,
2227 void *frame_address,
2228 void *return_address) {
2229 kmp_taskdata_t *taskdata =
nullptr;
2231 int thread_finished = FALSE;
2232 KMP_SET_THREAD_STATE_BLOCK(TASKWAIT);
2234 KA_TRACE(10, (
"__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref));
2235 KMP_DEBUG_ASSERT(gtid >= 0);
2237 if (__kmp_tasking_mode != tskm_immediate_exec) {
2238 thread = __kmp_threads[gtid];
2239 taskdata = thread->th.th_current_task;
2241#if OMPT_SUPPORT && OMPT_OPTIONAL
2242 ompt_data_t *my_task_data;
2243 ompt_data_t *my_parallel_data;
2246 my_task_data = &(taskdata->ompt_task_info.task_data);
2247 my_parallel_data = OMPT_CUR_TEAM_DATA(thread);
2249 taskdata->ompt_task_info.frame.enter_frame.ptr = frame_address;
2251 if (ompt_enabled.ompt_callback_sync_region) {
2252 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2253 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
2254 my_task_data, return_address);
2257 if (ompt_enabled.ompt_callback_sync_region_wait) {
2258 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2259 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
2260 my_task_data, return_address);
2270 taskdata->td_taskwait_counter += 1;
2271 taskdata->td_taskwait_ident = loc_ref;
2272 taskdata->td_taskwait_thread = gtid + 1;
2275 void *itt_sync_obj = NULL;
2277 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2282 !taskdata->td_flags.team_serial && !taskdata->td_flags.final;
2284 must_wait = must_wait || (thread->th.th_task_team != NULL &&
2285 thread->th.th_task_team->tt.tt_found_proxy_tasks);
2289 (__kmp_enable_hidden_helper && thread->th.th_task_team != NULL &&
2290 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered);
2293 kmp_flag_32<false, false> flag(
2294 RCAST(std::atomic<kmp_uint32> *,
2295 &(taskdata->td_incomplete_child_tasks)),
2297 while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) != 0) {
2298 flag.execute_tasks(thread, gtid, FALSE,
2299 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2300 __kmp_task_stealing_constraint);
2304 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2305 KMP_FSYNC_ACQUIRED(taskdata);
2310 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2312#if OMPT_SUPPORT && OMPT_OPTIONAL
2314 if (ompt_enabled.ompt_callback_sync_region_wait) {
2315 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2316 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
2317 my_task_data, return_address);
2319 if (ompt_enabled.ompt_callback_sync_region) {
2320 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2321 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
2322 my_task_data, return_address);
2324 taskdata->ompt_task_info.frame.enter_frame = ompt_data_none;
2329 KA_TRACE(10, (
"__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, "
2330 "returning TASK_CURRENT_NOT_QUEUED\n",
2333 return TASK_CURRENT_NOT_QUEUED;
2336#if OMPT_SUPPORT && OMPT_OPTIONAL
2338static kmp_int32 __kmpc_omp_taskwait_ompt(
ident_t *loc_ref, kmp_int32 gtid,
2339 void *frame_address,
2340 void *return_address) {
2341 return __kmpc_omp_taskwait_template<true>(loc_ref, gtid, frame_address,
2348kmp_int32 __kmpc_omp_taskwait(
ident_t *loc_ref, kmp_int32 gtid) {
2349#if OMPT_SUPPORT && OMPT_OPTIONAL
2350 if (UNLIKELY(ompt_enabled.enabled)) {
2351 OMPT_STORE_RETURN_ADDRESS(gtid);
2352 return __kmpc_omp_taskwait_ompt(loc_ref, gtid, OMPT_GET_FRAME_ADDRESS(0),
2353 OMPT_LOAD_RETURN_ADDRESS(gtid));
2356 return __kmpc_omp_taskwait_template<false>(loc_ref, gtid, NULL, NULL);
2360kmp_int32 __kmpc_omp_taskyield(
ident_t *loc_ref, kmp_int32 gtid,
int end_part) {
2361 kmp_taskdata_t *taskdata = NULL;
2363 int thread_finished = FALSE;
2366 KMP_SET_THREAD_STATE_BLOCK(TASKYIELD);
2368 KA_TRACE(10, (
"__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n",
2369 gtid, loc_ref, end_part));
2370 __kmp_assert_valid_gtid(gtid);
2372 if (__kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel) {
2373 thread = __kmp_threads[gtid];
2374 taskdata = thread->th.th_current_task;
2381 taskdata->td_taskwait_counter += 1;
2382 taskdata->td_taskwait_ident = loc_ref;
2383 taskdata->td_taskwait_thread = gtid + 1;
2386 void *itt_sync_obj = NULL;
2388 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2391 if (!taskdata->td_flags.team_serial) {
2392 kmp_task_team_t *task_team = thread->th.th_task_team;
2393 if (task_team != NULL) {
2394 if (KMP_TASKING_ENABLED(task_team)) {
2396 if (UNLIKELY(ompt_enabled.enabled))
2397 thread->th.ompt_thread_info.ompt_task_yielded = 1;
2399 __kmp_execute_tasks_32(
2400 thread, gtid, (kmp_flag_32<> *)NULL, FALSE,
2401 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2402 __kmp_task_stealing_constraint);
2404 if (UNLIKELY(ompt_enabled.enabled))
2405 thread->th.ompt_thread_info.ompt_task_yielded = 0;
2411 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2416 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2419 KA_TRACE(10, (
"__kmpc_omp_taskyield(exit): T#%d task %p resuming, "
2420 "returning TASK_CURRENT_NOT_QUEUED\n",
2423 return TASK_CURRENT_NOT_QUEUED;
2444 unsigned reserved31 : 31;
2524template <
typename T>
2525void *__kmp_task_reduction_init(
int gtid,
int num, T *data) {
2526 __kmp_assert_valid_gtid(gtid);
2527 kmp_info_t *thread = __kmp_threads[gtid];
2528 kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup;
2529 kmp_uint32 nth = thread->th.th_team_nproc;
2533 KMP_ASSERT(tg != NULL);
2534 KMP_ASSERT(data != NULL);
2535 KMP_ASSERT(num > 0);
2536 if (nth == 1 && !__kmp_enable_hidden_helper) {
2537 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n",
2541 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, taskgroup %p, #items %d\n",
2545 for (
int i = 0; i < num; ++i) {
2546 size_t size = data[i].reduce_size - 1;
2548 size += CACHE_LINE - size % CACHE_LINE;
2549 KMP_ASSERT(data[i].reduce_comb != NULL);
2552 arr[i].
flags = data[i].flags;
2556 __kmp_assign_orig<T>(arr[i], data[i]);
2557 if (!arr[i].flags.lazy_priv) {
2560 arr[i].
reduce_pend = (
char *)(arr[i].reduce_priv) + nth * size;
2561 if (arr[i].reduce_init != NULL) {
2563 for (
size_t j = 0; j < nth; ++j) {
2564 __kmp_call_init<T>(arr[i], j * size);
2571 arr[i].
reduce_priv = __kmp_allocate(nth *
sizeof(
void *));
2574 tg->reduce_data = (
void *)arr;
2575 tg->reduce_num_data = num;
2595 kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_idx);
2596 if (tdg && __kmp_tdg_is_recording(tdg->tdg_status)) {
2597 kmp_tdg_info_t *this_tdg = __kmp_global_tdgs[__kmp_curr_tdg_idx];
2598 this_tdg->rec_taskred_data =
2600 this_tdg->rec_num_taskred = num;
2601 KMP_MEMCPY(this_tdg->rec_taskred_data, data,
2622 kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_idx);
2623 if (tdg && __kmp_tdg_is_recording(tdg->tdg_status)) {
2624 kmp_tdg_info_t *this_tdg = __kmp_global_tdgs[__kmp_curr_tdg_idx];
2625 this_tdg->rec_taskred_data =
2627 this_tdg->rec_num_taskred = num;
2628 KMP_MEMCPY(this_tdg->rec_taskred_data, data,
2636template <
typename T>
2637void __kmp_task_reduction_init_copy(kmp_info_t *thr,
int num, T *data,
2638 kmp_taskgroup_t *tg,
void *reduce_data) {
2640 KA_TRACE(20, (
"__kmp_task_reduction_init_copy: Th %p, init taskgroup %p,"
2642 thr, tg, reduce_data));
2647 for (
int i = 0; i < num; ++i) {
2650 tg->reduce_data = (
void *)arr;
2651 tg->reduce_num_data = num;
2664 __kmp_assert_valid_gtid(gtid);
2665 kmp_info_t *thread = __kmp_threads[gtid];
2666 kmp_int32 nth = thread->th.th_team_nproc;
2670 kmp_taskgroup_t *tg = (kmp_taskgroup_t *)tskgrp;
2672 tg = thread->th.th_current_task->td_taskgroup;
2673 KMP_ASSERT(tg != NULL);
2676 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
2679 if ((thread->th.th_current_task->is_taskgraph) &&
2680 (!__kmp_tdg_is_recording(
2681 __kmp_global_tdgs[__kmp_curr_tdg_idx]->tdg_status))) {
2682 tg = thread->th.th_current_task->td_taskgroup;
2683 KMP_ASSERT(tg != NULL);
2684 KMP_ASSERT(tg->reduce_data != NULL);
2686 num = tg->reduce_num_data;
2690 KMP_ASSERT(data != NULL);
2691 while (tg != NULL) {
2693 num = tg->reduce_num_data;
2694 for (
int i = 0; i < num; ++i) {
2695 if (!arr[i].flags.lazy_priv) {
2696 if (data == arr[i].reduce_shar ||
2697 (data >= arr[i].reduce_priv && data < arr[i].reduce_pend))
2698 return (
char *)(arr[i].
reduce_priv) + tid * arr[i].reduce_size;
2701 void **p_priv = (
void **)(arr[i].reduce_priv);
2702 if (data == arr[i].reduce_shar)
2705 for (
int j = 0; j < nth; ++j)
2706 if (data == p_priv[j])
2710 if (p_priv[tid] == NULL) {
2712 p_priv[tid] = __kmp_allocate(arr[i].reduce_size);
2713 if (arr[i].reduce_init != NULL) {
2714 if (arr[i].reduce_orig != NULL) {
2716 p_priv[tid], arr[i].reduce_orig);
2718 ((void (*)(
void *))arr[i].
reduce_init)(p_priv[tid]);
2725 KMP_ASSERT(tg->parent);
2728 KMP_ASSERT2(0,
"Unknown task reduction item");
2734static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg) {
2735 kmp_int32 nth = th->th.th_team_nproc;
2738 __kmp_enable_hidden_helper);
2741 kmp_int32 num = tg->reduce_num_data;
2742 for (
int i = 0; i < num; ++i) {
2744 void (*f_fini)(
void *) = (
void (*)(
void *))(arr[i].
reduce_fini);
2745 void (*f_comb)(
void *,
void *) =
2747 if (!arr[i].flags.lazy_priv) {
2750 for (
int j = 0; j < nth; ++j) {
2751 void *priv_data = (
char *)pr_data + j * size;
2752 f_comb(sh_data, priv_data);
2757 void **pr_data = (
void **)(arr[i].reduce_priv);
2758 for (
int j = 0; j < nth; ++j) {
2759 if (pr_data[j] != NULL) {
2760 f_comb(sh_data, pr_data[j]);
2763 __kmp_free(pr_data[j]);
2767 __kmp_free(arr[i].reduce_priv);
2769 __kmp_thread_free(th, arr);
2770 tg->reduce_data = NULL;
2771 tg->reduce_num_data = 0;
2777static void __kmp_task_reduction_clean(kmp_info_t *th, kmp_taskgroup_t *tg) {
2778 __kmp_thread_free(th, tg->reduce_data);
2779 tg->reduce_data = NULL;
2780 tg->reduce_num_data = 0;
2783template <
typename T>
2784void *__kmp_task_reduction_modifier_init(
ident_t *loc,
int gtid,
int is_ws,
2786 __kmp_assert_valid_gtid(gtid);
2787 kmp_info_t *thr = __kmp_threads[gtid];
2788 kmp_int32 nth = thr->th.th_team_nproc;
2789 __kmpc_taskgroup(loc, gtid);
2792 (
"__kmpc_reduction_modifier_init: T#%d, tg %p, exiting nth=1\n",
2793 gtid, thr->th.th_current_task->td_taskgroup));
2794 return (
void *)thr->th.th_current_task->td_taskgroup;
2796 kmp_team_t *team = thr->th.th_team;
2798 kmp_taskgroup_t *tg;
2799 reduce_data = KMP_ATOMIC_LD_RLX(&team->t.t_tg_reduce_data[is_ws]);
2800 if (reduce_data == NULL &&
2801 __kmp_atomic_compare_store(&team->t.t_tg_reduce_data[is_ws], reduce_data,
2804 KMP_DEBUG_ASSERT(reduce_data == NULL);
2806 tg = (kmp_taskgroup_t *)__kmp_task_reduction_init<T>(gtid, num, data);
2810 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[0]) == 0);
2811 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[1]) == 0);
2812 KMP_ATOMIC_ST_REL(&team->t.t_tg_reduce_data[is_ws], reduce_data);
2815 (reduce_data = KMP_ATOMIC_LD_ACQ(&team->t.t_tg_reduce_data[is_ws])) ==
2819 KMP_DEBUG_ASSERT(reduce_data > (
void *)1);
2820 tg = thr->th.th_current_task->td_taskgroup;
2821 __kmp_task_reduction_init_copy<T>(thr, num, data, tg, reduce_data);
2843 int num,
void *data) {
2844 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2864 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2877 __kmpc_end_taskgroup(loc, gtid);
2881void __kmpc_taskgroup(
ident_t *loc,
int gtid) {
2882 __kmp_assert_valid_gtid(gtid);
2883 kmp_info_t *thread = __kmp_threads[gtid];
2884 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2885 kmp_taskgroup_t *tg_new =
2886 (kmp_taskgroup_t *)__kmp_thread_malloc(thread,
sizeof(kmp_taskgroup_t));
2887 KA_TRACE(10, (
"__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new));
2888 KMP_ATOMIC_ST_RLX(&tg_new->count, 0);
2889 KMP_ATOMIC_ST_RLX(&tg_new->cancel_request, cancel_noreq);
2890 tg_new->parent = taskdata->td_taskgroup;
2891 tg_new->reduce_data = NULL;
2892 tg_new->reduce_num_data = 0;
2893 tg_new->gomp_data = NULL;
2894 taskdata->td_taskgroup = tg_new;
2896#if OMPT_SUPPORT && OMPT_OPTIONAL
2897 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2898 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2900 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2901 kmp_team_t *team = thread->th.th_team;
2902 ompt_data_t my_task_data = taskdata->ompt_task_info.task_data;
2904 ompt_data_t my_parallel_data = team->t.ompt_team_info.parallel_data;
2906 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2907 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2908 &(my_task_data), codeptr);
2915void __kmpc_end_taskgroup(
ident_t *loc,
int gtid) {
2916 __kmp_assert_valid_gtid(gtid);
2917 kmp_info_t *thread = __kmp_threads[gtid];
2918 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2919 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
2920 int thread_finished = FALSE;
2922#if OMPT_SUPPORT && OMPT_OPTIONAL
2924 ompt_data_t my_task_data;
2925 ompt_data_t my_parallel_data;
2926 void *codeptr =
nullptr;
2927 if (UNLIKELY(ompt_enabled.enabled)) {
2928 team = thread->th.th_team;
2929 my_task_data = taskdata->ompt_task_info.task_data;
2931 my_parallel_data = team->t.ompt_team_info.parallel_data;
2932 codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2934 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2938 KA_TRACE(10, (
"__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc));
2939 KMP_DEBUG_ASSERT(taskgroup != NULL);
2940 KMP_SET_THREAD_STATE_BLOCK(TASKGROUP);
2942 if (__kmp_tasking_mode != tskm_immediate_exec) {
2944 taskdata->td_taskwait_counter += 1;
2945 taskdata->td_taskwait_ident = loc;
2946 taskdata->td_taskwait_thread = gtid + 1;
2950 void *itt_sync_obj = NULL;
2952 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2956#if OMPT_SUPPORT && OMPT_OPTIONAL
2957 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2958 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2959 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2960 &(my_task_data), codeptr);
2964 if (!taskdata->td_flags.team_serial ||
2965 (thread->th.th_task_team != NULL &&
2966 (thread->th.th_task_team->tt.tt_found_proxy_tasks ||
2967 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered))) {
2968 kmp_flag_32<false, false> flag(
2969 RCAST(std::atomic<kmp_uint32> *, &(taskgroup->count)), 0U);
2970 while (KMP_ATOMIC_LD_ACQ(&taskgroup->count) != 0) {
2971 flag.execute_tasks(thread, gtid, FALSE,
2972 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2973 __kmp_task_stealing_constraint);
2976 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2978#if OMPT_SUPPORT && OMPT_OPTIONAL
2979 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2980 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2981 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2982 &(my_task_data), codeptr);
2987 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2988 KMP_FSYNC_ACQUIRED(taskdata);
2991 KMP_DEBUG_ASSERT(taskgroup->count == 0);
2993 if (taskgroup->reduce_data != NULL &&
2994 !taskgroup->gomp_data) {
2997 kmp_team_t *t = thread->th.th_team;
3001 if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[0])) != NULL &&
3004 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[0]);
3005 if (cnt == thread->th.th_team_nproc - 1) {
3008 __kmp_task_reduction_fini(thread, taskgroup);
3011 __kmp_thread_free(thread, reduce_data);
3012 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[0], NULL);
3013 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[0], 0);
3017 __kmp_task_reduction_clean(thread, taskgroup);
3019 }
else if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[1])) !=
3023 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[1]);
3024 if (cnt == thread->th.th_team_nproc - 1) {
3026 __kmp_task_reduction_fini(thread, taskgroup);
3029 __kmp_thread_free(thread, reduce_data);
3030 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[1], NULL);
3031 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[1], 0);
3035 __kmp_task_reduction_clean(thread, taskgroup);
3039 __kmp_task_reduction_fini(thread, taskgroup);
3043 taskdata->td_taskgroup = taskgroup->parent;
3044 __kmp_thread_free(thread, taskgroup);
3046 KA_TRACE(10, (
"__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n",
3049#if OMPT_SUPPORT && OMPT_OPTIONAL
3050 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
3051 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
3052 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
3053 &(my_task_data), codeptr);
3058static kmp_task_t *__kmp_get_priority_task(kmp_int32 gtid,
3059 kmp_task_team_t *task_team,
3060 kmp_int32 is_constrained) {
3061 kmp_task_t *task = NULL;
3062 kmp_taskdata_t *taskdata;
3063 kmp_taskdata_t *current;
3064 kmp_thread_data_t *thread_data;
3065 int ntasks = task_team->tt.tt_num_task_pri;
3068 20, (
"__kmp_get_priority_task(exit #1): T#%d No tasks to get\n", gtid));
3073 if (__kmp_atomic_compare_store(&task_team->tt.tt_num_task_pri, ntasks,
3076 ntasks = task_team->tt.tt_num_task_pri;
3077 }
while (ntasks > 0);
3079 KA_TRACE(20, (
"__kmp_get_priority_task(exit #2): T#%d No tasks to get\n",
3085 kmp_task_pri_t *list = task_team->tt.tt_task_pri_list;
3087 KMP_ASSERT(list != NULL);
3088 thread_data = &list->td;
3089 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3090 deque_ntasks = thread_data->td.td_deque_ntasks;
3091 if (deque_ntasks == 0) {
3092 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3093 KA_TRACE(20, (
"__kmp_get_priority_task: T#%d No tasks to get from %p\n",
3094 __kmp_get_gtid(), thread_data));
3097 }
while (deque_ntasks == 0);
3098 KMP_DEBUG_ASSERT(deque_ntasks);
3099 int target = thread_data->td.td_deque_head;
3100 current = __kmp_threads[gtid]->th.th_current_task;
3101 taskdata = thread_data->td.td_deque[target];
3102 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3104 thread_data->td.td_deque_head =
3105 (target + 1) & TASK_DEQUE_MASK(thread_data->td);
3107 if (!task_team->tt.tt_untied_task_encountered) {
3109 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3110 KA_TRACE(20, (
"__kmp_get_priority_task(exit #3): T#%d could not get task "
3111 "from %p: task_team=%p ntasks=%d head=%u tail=%u\n",
3112 gtid, thread_data, task_team, deque_ntasks, target,
3113 thread_data->td.td_deque_tail));
3114 task_team->tt.tt_num_task_pri++;
3120 for (i = 1; i < deque_ntasks; ++i) {
3121 target = (target + 1) & TASK_DEQUE_MASK(thread_data->td);
3122 taskdata = thread_data->td.td_deque[target];
3123 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3129 if (taskdata == NULL) {
3131 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3133 10, (
"__kmp_get_priority_task(exit #4): T#%d could not get task from "
3134 "%p: task_team=%p ntasks=%d head=%u tail=%u\n",
3135 gtid, thread_data, task_team, deque_ntasks,
3136 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3137 task_team->tt.tt_num_task_pri++;
3141 for (i = i + 1; i < deque_ntasks; ++i) {
3143 target = (target + 1) & TASK_DEQUE_MASK(thread_data->td);
3144 thread_data->td.td_deque[prev] = thread_data->td.td_deque[target];
3148 thread_data->td.td_deque_tail ==
3149 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(thread_data->td)));
3150 thread_data->td.td_deque_tail = target;
3152 thread_data->td.td_deque_ntasks = deque_ntasks - 1;
3153 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3154 task = KMP_TASKDATA_TO_TASK(taskdata);
3159static kmp_task_t *__kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid,
3160 kmp_task_team_t *task_team,
3161 kmp_int32 is_constrained) {
3163 kmp_taskdata_t *taskdata;
3164 kmp_thread_data_t *thread_data;
3167 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3168 KMP_DEBUG_ASSERT(task_team->tt.tt_threads_data !=
3171 thread_data = &task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
3173 KA_TRACE(10, (
"__kmp_remove_my_task(enter): T#%d ntasks=%d head=%u tail=%u\n",
3174 gtid, thread_data->td.td_deque_ntasks,
3175 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3177 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
3179 (
"__kmp_remove_my_task(exit #1): T#%d No tasks to remove: "
3180 "ntasks=%d head=%u tail=%u\n",
3181 gtid, thread_data->td.td_deque_ntasks,
3182 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3186 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3188 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
3189 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3191 (
"__kmp_remove_my_task(exit #2): T#%d No tasks to remove: "
3192 "ntasks=%d head=%u tail=%u\n",
3193 gtid, thread_data->td.td_deque_ntasks,
3194 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3198 tail = (thread_data->td.td_deque_tail - 1) &
3199 TASK_DEQUE_MASK(thread_data->td);
3200 taskdata = thread_data->td.td_deque[tail];
3202 if (!__kmp_task_is_allowed(gtid, is_constrained, taskdata,
3203 thread->th.th_current_task)) {
3205 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3207 (
"__kmp_remove_my_task(exit #3): T#%d TSC blocks tail task: "
3208 "ntasks=%d head=%u tail=%u\n",
3209 gtid, thread_data->td.td_deque_ntasks,
3210 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3214 thread_data->td.td_deque_tail = tail;
3215 TCW_4(thread_data->td.td_deque_ntasks, thread_data->td.td_deque_ntasks - 1);
3217 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3219 KA_TRACE(10, (
"__kmp_remove_my_task(exit #4): T#%d task %p removed: "
3220 "ntasks=%d head=%u tail=%u\n",
3221 gtid, taskdata, thread_data->td.td_deque_ntasks,
3222 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3224 task = KMP_TASKDATA_TO_TASK(taskdata);
3231static kmp_task_t *__kmp_steal_task(kmp_int32 victim_tid, kmp_int32 gtid,
3232 kmp_task_team_t *task_team,
3233 std::atomic<kmp_int32> *unfinished_threads,
3234 int *thread_finished,
3235 kmp_int32 is_constrained) {
3237 kmp_taskdata_t *taskdata;
3238 kmp_taskdata_t *current;
3239 kmp_thread_data_t *victim_td, *threads_data;
3241 kmp_info_t *victim_thr;
3243 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3245 threads_data = task_team->tt.tt_threads_data;
3246 KMP_DEBUG_ASSERT(threads_data != NULL);
3247 KMP_DEBUG_ASSERT(victim_tid >= 0);
3248 KMP_DEBUG_ASSERT(victim_tid < task_team->tt.tt_nproc);
3250 victim_td = &threads_data[victim_tid];
3251 victim_thr = victim_td->td.td_thr;
3254 KA_TRACE(10, (
"__kmp_steal_task(enter): T#%d try to steal from T#%d: "
3255 "task_team=%p ntasks=%d head=%u tail=%u\n",
3256 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
3257 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
3258 victim_td->td.td_deque_tail));
3260 if (TCR_4(victim_td->td.td_deque_ntasks) == 0) {
3261 KA_TRACE(10, (
"__kmp_steal_task(exit #1): T#%d could not steal from T#%d: "
3262 "task_team=%p ntasks=%d head=%u tail=%u\n",
3263 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
3264 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
3265 victim_td->td.td_deque_tail));
3269 __kmp_acquire_bootstrap_lock(&victim_td->td.td_deque_lock);
3271 int ntasks = TCR_4(victim_td->td.td_deque_ntasks);
3274 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3275 KA_TRACE(10, (
"__kmp_steal_task(exit #2): T#%d could not steal from T#%d: "
3276 "task_team=%p ntasks=%d head=%u tail=%u\n",
3277 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3278 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3282 KMP_DEBUG_ASSERT(victim_td->td.td_deque != NULL);
3283 current = __kmp_threads[gtid]->th.th_current_task;
3284 taskdata = victim_td->td.td_deque[victim_td->td.td_deque_head];
3285 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3287 victim_td->td.td_deque_head =
3288 (victim_td->td.td_deque_head + 1) & TASK_DEQUE_MASK(victim_td->td);
3290 if (!task_team->tt.tt_untied_task_encountered) {
3292 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3293 KA_TRACE(10, (
"__kmp_steal_task(exit #3): T#%d could not steal from "
3294 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
3295 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3296 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3301 target = victim_td->td.td_deque_head;
3303 for (i = 1; i < ntasks; ++i) {
3304 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
3305 taskdata = victim_td->td.td_deque[target];
3306 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3312 if (taskdata == NULL) {
3314 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3315 KA_TRACE(10, (
"__kmp_steal_task(exit #4): T#%d could not steal from "
3316 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
3317 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3318 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3322 for (i = i + 1; i < ntasks; ++i) {
3324 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
3325 victim_td->td.td_deque[prev] = victim_td->td.td_deque[target];
3329 victim_td->td.td_deque_tail ==
3330 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(victim_td->td)));
3331 victim_td->td.td_deque_tail = target;
3333 if (*thread_finished) {
3340 KMP_ATOMIC_INC(unfinished_threads);
3343 (
"__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n",
3344 gtid, count + 1, task_team));
3345 *thread_finished = FALSE;
3347 TCW_4(victim_td->td.td_deque_ntasks, ntasks - 1);
3349 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3353 (
"__kmp_steal_task(exit #5): T#%d stole task %p from T#%d: "
3354 "task_team=%p ntasks=%d head=%u tail=%u\n",
3355 gtid, taskdata, __kmp_gtid_from_thread(victim_thr), task_team,
3356 ntasks, victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3358 task = KMP_TASKDATA_TO_TASK(taskdata);
3372static inline int __kmp_execute_tasks_template(
3373 kmp_info_t *thread, kmp_int32 gtid, C *flag,
int final_spin,
3374 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3375 kmp_int32 is_constrained) {
3376 kmp_task_team_t *task_team = thread->th.th_task_team;
3377 kmp_thread_data_t *threads_data;
3379 kmp_info_t *other_thread;
3380 kmp_taskdata_t *current_task = thread->th.th_current_task;
3381 std::atomic<kmp_int32> *unfinished_threads;
3382 kmp_int32 nthreads, victim_tid = -2, use_own_tasks = 1, new_victim = 0,
3383 tid = thread->th.th_info.ds.ds_tid;
3385 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3386 KMP_DEBUG_ASSERT(thread == __kmp_threads[gtid]);
3388 if (task_team == NULL || current_task == NULL)
3391 KA_TRACE(15, (
"__kmp_execute_tasks_template(enter): T#%d final_spin=%d "
3392 "*thread_finished=%d\n",
3393 gtid, final_spin, *thread_finished));
3395 thread->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
3396 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
3398 KMP_DEBUG_ASSERT(threads_data != NULL);
3400 nthreads = task_team->tt.tt_nproc;
3401 unfinished_threads = &(task_team->tt.tt_unfinished_threads);
3402 KMP_DEBUG_ASSERT(*unfinished_threads >= 0);
3408 if (task_team->tt.tt_num_task_pri) {
3409 task = __kmp_get_priority_task(gtid, task_team, is_constrained);
3411 if (task == NULL && use_own_tasks) {
3412 task = __kmp_remove_my_task(thread, gtid, task_team, is_constrained);
3414 if ((task == NULL) && (nthreads > 1)) {
3418 if (victim_tid == -2) {
3419 victim_tid = threads_data[tid].td.td_deque_last_stolen;
3422 other_thread = threads_data[victim_tid].td.td_thr;
3424 if (victim_tid != -1) {
3426 }
else if (!new_victim) {
3432 victim_tid = __kmp_get_random(thread) % (nthreads - 1);
3433 if (victim_tid >= tid) {
3437 other_thread = threads_data[victim_tid].td.td_thr;
3447 if ((__kmp_tasking_mode == tskm_task_teams) &&
3448 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) &&
3449 (TCR_PTR(CCAST(
void *, other_thread->th.th_sleep_loc)) !=
3452 __kmp_null_resume_wrapper(other_thread);
3466 __kmp_steal_task(victim_tid, gtid, task_team, unfinished_threads,
3467 thread_finished, is_constrained);
3470 if (threads_data[tid].td.td_deque_last_stolen != victim_tid) {
3471 threads_data[tid].td.td_deque_last_stolen = victim_tid;
3478 KMP_CHECK_UPDATE(threads_data[tid].td.td_deque_last_stolen, -1);
3487#if USE_ITT_BUILD && USE_ITT_NOTIFY
3488 if (__itt_sync_create_ptr || KMP_ITT_DEBUG) {
3489 if (itt_sync_obj == NULL) {
3491 itt_sync_obj = __kmp_itt_barrier_object(gtid, bs_forkjoin_barrier);
3493 __kmp_itt_task_starting(itt_sync_obj);
3496 __kmp_invoke_task(gtid, task, current_task);
3498 if (itt_sync_obj != NULL)
3499 __kmp_itt_task_finished(itt_sync_obj);
3506 if (flag == NULL || (!final_spin && flag->done_check())) {
3509 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3513 if (thread->th.th_task_team == NULL) {
3516 KMP_YIELD(__kmp_library == library_throughput);
3519 if (!use_own_tasks && TCR_4(threads_data[tid].td.td_deque_ntasks) != 0) {
3520 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d stolen task spawned "
3521 "other tasks, restart\n",
3532 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks) == 0) {
3536 if (!*thread_finished) {
3538 kmp_int32 count = -1 +
3540 KMP_ATOMIC_DEC(unfinished_threads);
3541 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d dec "
3542 "unfinished_threads to %d task_team=%p\n",
3543 gtid, count, task_team));
3544 *thread_finished = TRUE;
3552 if (flag != NULL && flag->done_check()) {
3555 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3563 if (thread->th.th_task_team == NULL) {
3565 (
"__kmp_execute_tasks_template: T#%d no more tasks\n", gtid));
3574 if (flag == NULL || (!final_spin && flag->done_check())) {
3576 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3583 if (nthreads == 1 &&
3584 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks))
3588 (
"__kmp_execute_tasks_template: T#%d can't find work\n", gtid));
3594template <
bool C,
bool S>
3595int __kmp_execute_tasks_32(
3596 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32<C, S> *flag,
int final_spin,
3597 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3598 kmp_int32 is_constrained) {
3599 return __kmp_execute_tasks_template(
3600 thread, gtid, flag, final_spin,
3601 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3604template <
bool C,
bool S>
3605int __kmp_execute_tasks_64(
3606 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64<C, S> *flag,
int final_spin,
3607 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3608 kmp_int32 is_constrained) {
3609 return __kmp_execute_tasks_template(
3610 thread, gtid, flag, final_spin,
3611 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3614template <
bool C,
bool S>
3615int __kmp_atomic_execute_tasks_64(
3616 kmp_info_t *thread, kmp_int32 gtid, kmp_atomic_flag_64<C, S> *flag,
3617 int final_spin,
int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3618 kmp_int32 is_constrained) {
3619 return __kmp_execute_tasks_template(
3620 thread, gtid, flag, final_spin,
3621 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3624int __kmp_execute_tasks_oncore(
3625 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag,
int final_spin,
3626 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3627 kmp_int32 is_constrained) {
3628 return __kmp_execute_tasks_template(
3629 thread, gtid, flag, final_spin,
3630 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3634__kmp_execute_tasks_32<false, false>(kmp_info_t *, kmp_int32,
3635 kmp_flag_32<false, false> *,
int,
3636 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3638template int __kmp_execute_tasks_64<false, true>(kmp_info_t *, kmp_int32,
3639 kmp_flag_64<false, true> *,
3641 int *USE_ITT_BUILD_ARG(
void *),
3644template int __kmp_execute_tasks_64<true, false>(kmp_info_t *, kmp_int32,
3645 kmp_flag_64<true, false> *,
3647 int *USE_ITT_BUILD_ARG(
void *),
3650template int __kmp_atomic_execute_tasks_64<false, true>(
3651 kmp_info_t *, kmp_int32, kmp_atomic_flag_64<false, true> *,
int,
3652 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3654template int __kmp_atomic_execute_tasks_64<true, false>(
3655 kmp_info_t *, kmp_int32, kmp_atomic_flag_64<true, false> *,
int,
3656 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3661static void __kmp_enable_tasking(kmp_task_team_t *task_team,
3662 kmp_info_t *this_thr) {
3663 kmp_thread_data_t *threads_data;
3664 int nthreads, i, is_init_thread;
3666 KA_TRACE(10, (
"__kmp_enable_tasking(enter): T#%d\n",
3667 __kmp_gtid_from_thread(this_thr)));
3669 KMP_DEBUG_ASSERT(task_team != NULL);
3670 KMP_DEBUG_ASSERT(this_thr->th.th_team != NULL);
3672 nthreads = task_team->tt.tt_nproc;
3673 KMP_DEBUG_ASSERT(nthreads > 0);
3674 KMP_DEBUG_ASSERT(nthreads == this_thr->th.th_team->t.t_nproc);
3677 is_init_thread = __kmp_realloc_task_threads_data(this_thr, task_team);
3679 if (!is_init_thread) {
3683 (
"__kmp_enable_tasking(exit): T#%d: threads array already set up.\n",
3684 __kmp_gtid_from_thread(this_thr)));
3687 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
3688 KMP_DEBUG_ASSERT(threads_data != NULL);
3690 if (__kmp_tasking_mode == tskm_task_teams &&
3691 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME)) {
3695 for (i = 0; i < nthreads; i++) {
3697 kmp_info_t *thread = threads_data[i].td.td_thr;
3699 if (i == this_thr->th.th_info.ds.ds_tid) {
3708 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3710 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d waking up thread T#%d\n",
3711 __kmp_gtid_from_thread(this_thr),
3712 __kmp_gtid_from_thread(thread)));
3713 __kmp_null_resume_wrapper(thread);
3715 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d don't wake up thread T#%d\n",
3716 __kmp_gtid_from_thread(this_thr),
3717 __kmp_gtid_from_thread(thread)));
3722 KA_TRACE(10, (
"__kmp_enable_tasking(exit): T#%d\n",
3723 __kmp_gtid_from_thread(this_thr)));
3756static kmp_task_team_t *__kmp_free_task_teams =
3759kmp_bootstrap_lock_t __kmp_task_team_lock =
3760 KMP_BOOTSTRAP_LOCK_INITIALIZER(__kmp_task_team_lock);
3767static void __kmp_alloc_task_deque(kmp_info_t *thread,
3768 kmp_thread_data_t *thread_data) {
3769 __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
3770 KMP_DEBUG_ASSERT(thread_data->td.td_deque == NULL);
3773 thread_data->td.td_deque_last_stolen = -1;
3775 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == 0);
3776 KMP_DEBUG_ASSERT(thread_data->td.td_deque_head == 0);
3777 KMP_DEBUG_ASSERT(thread_data->td.td_deque_tail == 0);
3781 (
"__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n",
3782 __kmp_gtid_from_thread(thread), INITIAL_TASK_DEQUE_SIZE, thread_data));
3786 thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
3787 INITIAL_TASK_DEQUE_SIZE *
sizeof(kmp_taskdata_t *));
3788 thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
3794static void __kmp_free_task_deque(kmp_thread_data_t *thread_data) {
3795 if (thread_data->td.td_deque != NULL) {
3796 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3797 TCW_4(thread_data->td.td_deque_ntasks, 0);
3798 __kmp_free(thread_data->td.td_deque);
3799 thread_data->td.td_deque = NULL;
3800 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3803#ifdef BUILD_TIED_TASK_STACK
3805 if (thread_data->td.td_susp_tied_tasks.ts_entries != TASK_STACK_EMPTY) {
3806 __kmp_free_task_stack(__kmp_thread_from_gtid(gtid), thread_data);
3818static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
3819 kmp_task_team_t *task_team) {
3820 kmp_thread_data_t **threads_data_p;
3821 kmp_int32 nthreads, maxthreads;
3822 int is_init_thread = FALSE;
3824 if (TCR_4(task_team->tt.tt_found_tasks)) {
3829 threads_data_p = &task_team->tt.tt_threads_data;
3830 nthreads = task_team->tt.tt_nproc;
3831 maxthreads = task_team->tt.tt_max_threads;
3836 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3838 if (!TCR_4(task_team->tt.tt_found_tasks)) {
3840 kmp_team_t *team = thread->th.th_team;
3843 is_init_thread = TRUE;
3844 if (maxthreads < nthreads) {
3846 if (*threads_data_p != NULL) {
3847 kmp_thread_data_t *old_data = *threads_data_p;
3848 kmp_thread_data_t *new_data = NULL;
3852 (
"__kmp_realloc_task_threads_data: T#%d reallocating "
3853 "threads data for task_team %p, new_size = %d, old_size = %d\n",
3854 __kmp_gtid_from_thread(thread), task_team, nthreads, maxthreads));
3859 new_data = (kmp_thread_data_t *)__kmp_allocate(
3860 nthreads *
sizeof(kmp_thread_data_t));
3862 KMP_MEMCPY_S((
void *)new_data, nthreads *
sizeof(kmp_thread_data_t),
3863 (
void *)old_data, maxthreads *
sizeof(kmp_thread_data_t));
3865#ifdef BUILD_TIED_TASK_STACK
3867 for (i = maxthreads; i < nthreads; i++) {
3868 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3869 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3873 (*threads_data_p) = new_data;
3874 __kmp_free(old_data);
3876 KE_TRACE(10, (
"__kmp_realloc_task_threads_data: T#%d allocating "
3877 "threads data for task_team %p, size = %d\n",
3878 __kmp_gtid_from_thread(thread), task_team, nthreads));
3882 *threads_data_p = (kmp_thread_data_t *)__kmp_allocate(
3883 nthreads *
sizeof(kmp_thread_data_t));
3884#ifdef BUILD_TIED_TASK_STACK
3886 for (i = 0; i < nthreads; i++) {
3887 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3888 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3892 task_team->tt.tt_max_threads = nthreads;
3895 KMP_DEBUG_ASSERT(*threads_data_p != NULL);
3899 for (i = 0; i < nthreads; i++) {
3900 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3901 thread_data->td.td_thr = team->t.t_threads[i];
3903 if (thread_data->td.td_deque_last_stolen >= nthreads) {
3907 thread_data->td.td_deque_last_stolen = -1;
3912 TCW_SYNC_4(task_team->tt.tt_found_tasks, TRUE);
3915 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3916 return is_init_thread;
3922static void __kmp_free_task_threads_data(kmp_task_team_t *task_team) {
3923 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3924 if (task_team->tt.tt_threads_data != NULL) {
3926 for (i = 0; i < task_team->tt.tt_max_threads; i++) {
3927 __kmp_free_task_deque(&task_team->tt.tt_threads_data[i]);
3929 __kmp_free(task_team->tt.tt_threads_data);
3930 task_team->tt.tt_threads_data = NULL;
3932 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3938static void __kmp_free_task_pri_list(kmp_task_team_t *task_team) {
3939 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
3940 if (task_team->tt.tt_task_pri_list != NULL) {
3941 kmp_task_pri_t *list = task_team->tt.tt_task_pri_list;
3942 while (list != NULL) {
3943 kmp_task_pri_t *next = list->next;
3944 __kmp_free_task_deque(&list->td);
3948 task_team->tt.tt_task_pri_list = NULL;
3950 __kmp_release_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
3953static inline void __kmp_task_team_init(kmp_task_team_t *task_team,
3955 int team_nth = team->t.t_nproc;
3957 if (!task_team->tt.tt_active || team_nth != task_team->tt.tt_nproc) {
3958 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3959 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3960 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
3961 TCW_4(task_team->tt.tt_nproc, team_nth);
3962 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads, team_nth);
3963 TCW_4(task_team->tt.tt_active, TRUE);
3971static kmp_task_team_t *__kmp_allocate_task_team(kmp_info_t *thread,
3973 kmp_task_team_t *task_team = NULL;
3975 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d entering; team = %p\n",
3976 (thread ? __kmp_gtid_from_thread(thread) : -1), team));
3978 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3980 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3981 if (__kmp_free_task_teams != NULL) {
3982 task_team = __kmp_free_task_teams;
3983 TCW_PTR(__kmp_free_task_teams, task_team->tt.tt_next);
3984 task_team->tt.tt_next = NULL;
3986 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3989 if (task_team == NULL) {
3990 KE_TRACE(10, (
"__kmp_allocate_task_team: T#%d allocating "
3991 "task team for team %p\n",
3992 __kmp_gtid_from_thread(thread), team));
3995 task_team = (kmp_task_team_t *)__kmp_allocate(
sizeof(kmp_task_team_t));
3996 __kmp_init_bootstrap_lock(&task_team->tt.tt_threads_lock);
3997 __kmp_init_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
3998#if USE_ITT_BUILD && USE_ITT_NOTIFY && KMP_DEBUG
4001 __itt_suppress_mark_range(
4002 __itt_suppress_range, __itt_suppress_threading_errors,
4003 &task_team->tt.tt_found_tasks,
sizeof(task_team->tt.tt_found_tasks));
4004 __itt_suppress_mark_range(__itt_suppress_range,
4005 __itt_suppress_threading_errors,
4006 CCAST(kmp_uint32 *, &task_team->tt.tt_active),
4007 sizeof(task_team->tt.tt_active));
4015 __kmp_task_team_init(task_team, team);
4017 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d exiting; task_team = %p "
4018 "unfinished_threads init'd to %d\n",
4019 (thread ? __kmp_gtid_from_thread(thread) : -1), task_team,
4020 KMP_ATOMIC_LD_RLX(&task_team->tt.tt_unfinished_threads)));
4027void __kmp_free_task_team(kmp_info_t *thread, kmp_task_team_t *task_team) {
4028 KA_TRACE(20, (
"__kmp_free_task_team: T#%d task_team = %p\n",
4029 thread ? __kmp_gtid_from_thread(thread) : -1, task_team));
4032 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
4034 KMP_DEBUG_ASSERT(task_team->tt.tt_next == NULL);
4035 task_team->tt.tt_next = __kmp_free_task_teams;
4036 TCW_PTR(__kmp_free_task_teams, task_team);
4038 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
4046void __kmp_reap_task_teams(
void) {
4047 kmp_task_team_t *task_team;
4049 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
4051 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
4052 while ((task_team = __kmp_free_task_teams) != NULL) {
4053 __kmp_free_task_teams = task_team->tt.tt_next;
4054 task_team->tt.tt_next = NULL;
4057 if (task_team->tt.tt_threads_data != NULL) {
4058 __kmp_free_task_threads_data(task_team);
4060 if (task_team->tt.tt_task_pri_list != NULL) {
4061 __kmp_free_task_pri_list(task_team);
4063 __kmp_free(task_team);
4065 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
4073void __kmp_push_task_team_node(kmp_info_t *thread, kmp_team_t *team) {
4074 KMP_DEBUG_ASSERT(team->t.t_nproc == 1);
4075 kmp_task_team_list_t *current =
4076 (kmp_task_team_list_t *)(&team->t.t_task_team[0]);
4077 kmp_task_team_list_t *node =
4078 (kmp_task_team_list_t *)__kmp_allocate(
sizeof(kmp_task_team_list_t));
4079 node->task_team = current->task_team;
4080 node->next = current->next;
4081 thread->th.th_task_team = current->task_team = NULL;
4082 current->next = node;
4086void __kmp_pop_task_team_node(kmp_info_t *thread, kmp_team_t *team) {
4087 KMP_DEBUG_ASSERT(team->t.t_nproc == 1);
4088 kmp_task_team_list_t *current =
4089 (kmp_task_team_list_t *)(&team->t.t_task_team[0]);
4090 if (current->task_team) {
4091 __kmp_free_task_team(thread, current->task_team);
4093 kmp_task_team_list_t *next = current->next;
4095 current->task_team = next->task_team;
4096 current->next = next->next;
4097 KMP_DEBUG_ASSERT(next != current);
4099 thread->th.th_task_team = current->task_team;
4106void __kmp_wait_to_unref_task_teams(
void) {
4112 KMP_INIT_YIELD(spins);
4113 KMP_INIT_BACKOFF(time);
4121 for (thread = CCAST(kmp_info_t *, __kmp_thread_pool); thread != NULL;
4122 thread = thread->th.th_next_pool) {
4126 if (TCR_PTR(thread->th.th_task_team) == NULL) {
4127 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n",
4128 __kmp_gtid_from_thread(thread)));
4133 if (!__kmp_is_thread_alive(thread, &exit_val)) {
4134 thread->th.th_task_team = NULL;
4141 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: Waiting for T#%d to "
4142 "unreference task_team\n",
4143 __kmp_gtid_from_thread(thread)));
4145 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
4148 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
4152 (
"__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n",
4153 __kmp_gtid_from_thread(thread), __kmp_gtid_from_thread(thread)));
4154 __kmp_null_resume_wrapper(thread);
4163 KMP_YIELD_OVERSUB_ELSE_SPIN(spins, time);
4169void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team) {
4170 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
4175 if (team == this_thr->th.th_serial_team ||
4176 team == this_thr->th.th_root->r.r_root_team) {
4177 KMP_DEBUG_ASSERT(team->t.t_nproc == 1);
4178 if (team->t.t_task_team[0] == NULL) {
4179 team->t.t_task_team[0] = __kmp_allocate_task_team(this_thr, team);
4181 20, (
"__kmp_task_team_setup: Primary T#%d created new task_team %p"
4182 " for serial/root team %p\n",
4183 __kmp_gtid_from_thread(this_thr), team->t.t_task_team[0], team));
4186 __kmp_task_team_init(team->t.t_task_team[0], team);
4194 if (team->t.t_task_team[this_thr->th.th_task_state] == NULL) {
4195 team->t.t_task_team[this_thr->th.th_task_state] =
4196 __kmp_allocate_task_team(this_thr, team);
4197 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d created new task_team %p"
4198 " for team %d at parity=%d\n",
4199 __kmp_gtid_from_thread(this_thr),
4200 team->t.t_task_team[this_thr->th.th_task_state], team->t.t_id,
4201 this_thr->th.th_task_state));
4210 int other_team = 1 - this_thr->th.th_task_state;
4211 KMP_DEBUG_ASSERT(other_team >= 0 && other_team < 2);
4212 if (team->t.t_task_team[other_team] == NULL) {
4213 team->t.t_task_team[other_team] = __kmp_allocate_task_team(this_thr, team);
4214 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d created second new "
4215 "task_team %p for team %d at parity=%d\n",
4216 __kmp_gtid_from_thread(this_thr),
4217 team->t.t_task_team[other_team], team->t.t_id, other_team));
4220 kmp_task_team_t *task_team = team->t.t_task_team[other_team];
4221 __kmp_task_team_init(task_team, team);
4224 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d reset next task_team "
4225 "%p for team %d at parity=%d\n",
4226 __kmp_gtid_from_thread(this_thr),
4227 team->t.t_task_team[other_team], team->t.t_id, other_team));
4234 if (this_thr == __kmp_hidden_helper_main_thread) {
4235 for (
int i = 0; i < 2; ++i) {
4236 kmp_task_team_t *task_team = team->t.t_task_team[i];
4237 if (KMP_TASKING_ENABLED(task_team)) {
4240 __kmp_enable_tasking(task_team, this_thr);
4241 for (
int j = 0; j < task_team->tt.tt_nproc; ++j) {
4242 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[j];
4243 if (thread_data->td.td_deque == NULL) {
4244 __kmp_alloc_task_deque(__kmp_hidden_helper_threads[j], thread_data);
4254void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team) {
4255 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
4256 KMP_DEBUG_ASSERT(team != this_thr->th.th_serial_team);
4257 KMP_DEBUG_ASSERT(team != this_thr->th.th_root->r.r_root_team);
4261 this_thr->th.th_task_state = (kmp_uint8)(1 - this_thr->th.th_task_state);
4265 TCW_PTR(this_thr->th.th_task_team,
4266 team->t.t_task_team[this_thr->th.th_task_state]);
4268 (
"__kmp_task_team_sync: Thread T#%d task team switched to task_team "
4269 "%p from Team #%d (parity=%d)\n",
4270 __kmp_gtid_from_thread(this_thr), this_thr->th.th_task_team,
4271 team->t.t_id, this_thr->th.th_task_state));
4280void __kmp_task_team_wait(
4281 kmp_info_t *this_thr,
4282 kmp_team_t *team USE_ITT_BUILD_ARG(
void *itt_sync_obj),
int wait) {
4283 kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state];
4285 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
4286 KMP_DEBUG_ASSERT(task_team == this_thr->th.th_task_team);
4288 if ((task_team != NULL) && KMP_TASKING_ENABLED(task_team)) {
4290 KA_TRACE(20, (
"__kmp_task_team_wait: Primary T#%d waiting for all tasks "
4291 "(for unfinished_threads to reach 0) on task_team = %p\n",
4292 __kmp_gtid_from_thread(this_thr), task_team));
4296 kmp_flag_32<false, false> flag(
4297 RCAST(std::atomic<kmp_uint32> *,
4298 &task_team->tt.tt_unfinished_threads),
4300 flag.wait(this_thr, TRUE USE_ITT_BUILD_ARG(itt_sync_obj));
4306 (
"__kmp_task_team_wait: Primary T#%d deactivating task_team %p: "
4307 "setting active to false, setting local and team's pointer to NULL\n",
4308 __kmp_gtid_from_thread(this_thr), task_team));
4309 TCW_SYNC_4(task_team->tt.tt_found_proxy_tasks, FALSE);
4310 TCW_SYNC_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
4311 KMP_CHECK_UPDATE(task_team->tt.tt_untied_task_encountered, 0);
4312 TCW_SYNC_4(task_team->tt.tt_active, FALSE);
4315 TCW_PTR(this_thr->th.th_task_team, NULL);
4324void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread,
int gtid) {
4325 std::atomic<kmp_uint32> *spin = RCAST(
4326 std::atomic<kmp_uint32> *,
4327 &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads);
4329 KMP_DEBUG_ASSERT(__kmp_tasking_mode == tskm_extra_barrier);
4332 KMP_FSYNC_SPIN_INIT(spin, NULL);
4334 kmp_flag_32<false, false> spin_flag(spin, 0U);
4335 while (!spin_flag.execute_tasks(thread, gtid, TRUE,
4336 &flag USE_ITT_BUILD_ARG(NULL), 0)) {
4339 KMP_FSYNC_SPIN_PREPARE(RCAST(
void *, spin));
4342 if (TCR_4(__kmp_global.g.g_done)) {
4343 if (__kmp_global.g.g_abort)
4344 __kmp_abort_thread();
4350 KMP_FSYNC_SPIN_ACQUIRED(RCAST(
void *, spin));
4359static bool __kmp_give_task(kmp_info_t *thread, kmp_int32 tid, kmp_task_t *task,
4361 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4362 kmp_task_team_t *task_team = taskdata->td_task_team;
4364 KA_TRACE(20, (
"__kmp_give_task: trying to give task %p to thread %d.\n",
4368 KMP_DEBUG_ASSERT(task_team != NULL);
4370 bool result =
false;
4371 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
4373 if (thread_data->td.td_deque == NULL) {
4377 (
"__kmp_give_task: thread %d has no queue while giving task %p.\n",
4382 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4383 TASK_DEQUE_SIZE(thread_data->td)) {
4386 (
"__kmp_give_task: queue is full while giving task %p to thread %d.\n",
4391 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
4394 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
4395 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4396 TASK_DEQUE_SIZE(thread_data->td)) {
4398 __kmp_realloc_task_deque(thread, thread_data);
4403 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
4405 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4406 TASK_DEQUE_SIZE(thread_data->td)) {
4407 KA_TRACE(30, (
"__kmp_give_task: queue is full while giving task %p to "
4413 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
4414 goto release_and_exit;
4416 __kmp_realloc_task_deque(thread, thread_data);
4422 thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
4424 thread_data->td.td_deque_tail =
4425 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
4426 TCW_4(thread_data->td.td_deque_ntasks,
4427 TCR_4(thread_data->td.td_deque_ntasks) + 1);
4430 KA_TRACE(30, (
"__kmp_give_task: successfully gave task %p to thread %d.\n",
4434 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
4439#define PROXY_TASK_FLAG 0x40000000
4456static void __kmp_first_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
4457 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
4458 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4459 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
4460 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
4462 taskdata->td_flags.complete = 1;
4464 taskdata->td_flags.onced = 1;
4467 if (taskdata->td_taskgroup)
4468 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
4472 KMP_ATOMIC_OR(&taskdata->td_incomplete_child_tasks, PROXY_TASK_FLAG);
4475static void __kmp_second_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
4477 kmp_int32 children = 0;
4481 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks);
4482 KMP_DEBUG_ASSERT(children >= 0);
4485 KMP_ATOMIC_AND(&taskdata->td_incomplete_child_tasks, ~PROXY_TASK_FLAG);
4488static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask) {
4489 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4490 kmp_info_t *thread = __kmp_threads[gtid];
4492 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4493 KMP_DEBUG_ASSERT(taskdata->td_flags.complete ==
4498 while ((KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) &
4499 PROXY_TASK_FLAG) > 0)
4502 __kmp_release_deps(gtid, taskdata);
4503 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
4515 KMP_DEBUG_ASSERT(ptask != NULL);
4516 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4518 10, (
"__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n",
4520 __kmp_assert_valid_gtid(gtid);
4521 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4523 __kmp_first_top_half_finish_proxy(taskdata);
4524 __kmp_second_top_half_finish_proxy(taskdata);
4525 __kmp_bottom_half_finish_proxy(gtid, ptask);
4528 (
"__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n",
4532void __kmpc_give_task(kmp_task_t *ptask, kmp_int32 start = 0) {
4533 KMP_DEBUG_ASSERT(ptask != NULL);
4534 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4538 kmp_team_t *team = taskdata->td_team;
4539 kmp_int32 nthreads = team->t.t_nproc;
4544 kmp_int32 start_k = start % nthreads;
4546 kmp_int32 k = start_k;
4550 thread = team->t.t_threads[k];
4551 k = (k + 1) % nthreads;
4557 }
while (!__kmp_give_task(thread, k, ptask, pass));
4559 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME && __kmp_wpolicy_passive) {
4561 for (
int i = 0; i < nthreads; ++i) {
4562 thread = team->t.t_threads[i];
4563 if (thread->th.th_sleep_loc != NULL) {
4564 __kmp_null_resume_wrapper(thread);
4579 KMP_DEBUG_ASSERT(ptask != NULL);
4580 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4584 (
"__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n",
4587 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4589 __kmp_first_top_half_finish_proxy(taskdata);
4591 __kmpc_give_task(ptask);
4593 __kmp_second_top_half_finish_proxy(taskdata);
4597 (
"__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n",
4601kmp_event_t *__kmpc_task_allow_completion_event(
ident_t *loc_ref,
int gtid,
4603 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(task);
4604 if (td->td_allow_completion_event.type == KMP_EVENT_UNINITIALIZED) {
4605 td->td_allow_completion_event.type = KMP_EVENT_ALLOW_COMPLETION;
4606 td->td_allow_completion_event.ed.task = task;
4607 __kmp_init_tas_lock(&td->td_allow_completion_event.lock);
4609 return &td->td_allow_completion_event;
4612void __kmp_fulfill_event(kmp_event_t *event) {
4613 if (event->type == KMP_EVENT_ALLOW_COMPLETION) {
4614 kmp_task_t *ptask = event->ed.task;
4615 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4616 bool detached =
false;
4617 int gtid = __kmp_get_gtid();
4622 __kmp_acquire_tas_lock(&event->lock, gtid);
4623 if (taskdata->td_flags.proxy == TASK_PROXY) {
4629 if (UNLIKELY(ompt_enabled.enabled))
4630 __ompt_task_finish(ptask, NULL, ompt_task_early_fulfill);
4633 event->type = KMP_EVENT_UNINITIALIZED;
4634 __kmp_release_tas_lock(&event->lock, gtid);
4640 if (UNLIKELY(ompt_enabled.enabled))
4641 __ompt_task_finish(ptask, NULL, ompt_task_late_fulfill);
4645 kmp_team_t *team = taskdata->td_team;
4646 kmp_info_t *thread = __kmp_get_thread();
4647 if (thread->th.th_team == team) {
4667kmp_task_t *__kmp_task_dup_alloc(kmp_info_t *thread, kmp_task_t *task_src
4669 ,
int taskloop_recur
4673 kmp_taskdata_t *taskdata;
4674 kmp_taskdata_t *taskdata_src = KMP_TASK_TO_TASKDATA(task_src);
4675 kmp_taskdata_t *parent_task = taskdata_src->td_parent;
4676 size_t shareds_offset;
4679 KA_TRACE(10, (
"__kmp_task_dup_alloc(enter): Th %p, source task %p\n", thread,
4681 KMP_DEBUG_ASSERT(taskdata_src->td_flags.proxy ==
4683 KMP_DEBUG_ASSERT(taskdata_src->td_flags.tasktype == TASK_EXPLICIT);
4684 task_size = taskdata_src->td_size_alloc;
4687 KA_TRACE(30, (
"__kmp_task_dup_alloc: Th %p, malloc size %ld\n", thread,
4690 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, task_size);
4692 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, task_size);
4694 KMP_MEMCPY(taskdata, taskdata_src, task_size);
4696 task = KMP_TASKDATA_TO_TASK(taskdata);
4700 if (!taskdata->is_taskgraph || taskloop_recur)
4701 taskdata->td_task_id = KMP_GEN_TASK_ID();
4702 else if (taskdata->is_taskgraph &&
4703 __kmp_tdg_is_recording(taskdata_src->tdg->tdg_status))
4704 taskdata->td_task_id = KMP_ATOMIC_INC(&__kmp_tdg_task_id);
4706 taskdata->td_task_id = KMP_GEN_TASK_ID();
4708 if (task->shareds != NULL) {
4709 shareds_offset = (
char *)task_src->shareds - (
char *)taskdata_src;
4710 task->shareds = &((
char *)taskdata)[shareds_offset];
4711 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
4714 taskdata->td_alloc_thread = thread;
4715 taskdata->td_parent = parent_task;
4717 taskdata->td_taskgroup = parent_task->td_taskgroup;
4720 if (taskdata->td_flags.tiedness == TASK_TIED)
4721 taskdata->td_last_tied = taskdata;
4725 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
4726 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
4727 if (parent_task->td_taskgroup)
4728 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
4731 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT)
4732 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
4736 (
"__kmp_task_dup_alloc(exit): Th %p, created task %p, parent=%p\n",
4737 thread, taskdata, taskdata->td_parent));
4739 if (UNLIKELY(ompt_enabled.enabled))
4740 __ompt_task_init(taskdata, thread->th.th_info.ds.ds_gtid);
4749typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32);
4751KMP_BUILD_ASSERT(
sizeof(
long) == 4 ||
sizeof(
long) == 8);
4756class kmp_taskloop_bounds_t {
4758 const kmp_taskdata_t *taskdata;
4759 size_t lower_offset;
4760 size_t upper_offset;
4763 kmp_taskloop_bounds_t(kmp_task_t *_task, kmp_uint64 *lb, kmp_uint64 *ub)
4764 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(task)),
4765 lower_offset((char *)lb - (char *)task),
4766 upper_offset((char *)ub - (char *)task) {
4767 KMP_DEBUG_ASSERT((
char *)lb > (
char *)_task);
4768 KMP_DEBUG_ASSERT((
char *)ub > (
char *)_task);
4770 kmp_taskloop_bounds_t(kmp_task_t *_task,
const kmp_taskloop_bounds_t &bounds)
4771 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(_task)),
4772 lower_offset(bounds.lower_offset), upper_offset(bounds.upper_offset) {}
4773 size_t get_lower_offset()
const {
return lower_offset; }
4774 size_t get_upper_offset()
const {
return upper_offset; }
4775 kmp_uint64 get_lb()
const {
4777#if defined(KMP_GOMP_COMPAT)
4779 if (!taskdata->td_flags.native) {
4780 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4783 if (taskdata->td_size_loop_bounds == 4) {
4784 kmp_int32 *lb = RCAST(kmp_int32 *, task->shareds);
4785 retval = (kmp_int64)*lb;
4787 kmp_int64 *lb = RCAST(kmp_int64 *, task->shareds);
4788 retval = (kmp_int64)*lb;
4793 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4797 kmp_uint64 get_ub()
const {
4799#if defined(KMP_GOMP_COMPAT)
4801 if (!taskdata->td_flags.native) {
4802 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4805 if (taskdata->td_size_loop_bounds == 4) {
4806 kmp_int32 *ub = RCAST(kmp_int32 *, task->shareds) + 1;
4807 retval = (kmp_int64)*ub;
4809 kmp_int64 *ub = RCAST(kmp_int64 *, task->shareds) + 1;
4810 retval = (kmp_int64)*ub;
4814 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4818 void set_lb(kmp_uint64 lb) {
4819#if defined(KMP_GOMP_COMPAT)
4821 if (!taskdata->td_flags.native) {
4822 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4825 if (taskdata->td_size_loop_bounds == 4) {
4826 kmp_uint32 *lower = RCAST(kmp_uint32 *, task->shareds);
4827 *lower = (kmp_uint32)lb;
4829 kmp_uint64 *lower = RCAST(kmp_uint64 *, task->shareds);
4830 *lower = (kmp_uint64)lb;
4834 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4837 void set_ub(kmp_uint64 ub) {
4838#if defined(KMP_GOMP_COMPAT)
4840 if (!taskdata->td_flags.native) {
4841 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4844 if (taskdata->td_size_loop_bounds == 4) {
4845 kmp_uint32 *upper = RCAST(kmp_uint32 *, task->shareds) + 1;
4846 *upper = (kmp_uint32)ub;
4848 kmp_uint64 *upper = RCAST(kmp_uint64 *, task->shareds) + 1;
4849 *upper = (kmp_uint64)ub;
4853 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4874void __kmp_taskloop_linear(
ident_t *loc,
int gtid, kmp_task_t *task,
4875 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4876 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
4877 kmp_uint64 grainsize, kmp_uint64 extras,
4878 kmp_int64 last_chunk, kmp_uint64 tc,
4884 KMP_TIME_PARTITIONED_BLOCK(OMP_taskloop_scheduling);
4885 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
4887 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4888 kmp_uint64 lower = task_bounds.get_lb();
4889 kmp_uint64 upper = task_bounds.get_ub();
4891 kmp_info_t *thread = __kmp_threads[gtid];
4892 kmp_taskdata_t *current_task = thread->th.th_current_task;
4893 kmp_task_t *next_task;
4894 kmp_int32 lastpriv = 0;
4896 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4897 (last_chunk < 0 ? last_chunk : extras));
4898 KMP_DEBUG_ASSERT(num_tasks > extras);
4899 KMP_DEBUG_ASSERT(num_tasks > 0);
4900 KA_TRACE(20, (
"__kmp_taskloop_linear: T#%d: %lld tasks, grainsize %lld, "
4901 "extras %lld, last_chunk %lld, i=%lld,%lld(%d)%lld, dup %p\n",
4902 gtid, num_tasks, grainsize, extras, last_chunk, lower, upper,
4903 ub_glob, st, task_dup));
4906 for (i = 0; i < num_tasks; ++i) {
4907 kmp_uint64 chunk_minus_1;
4909 chunk_minus_1 = grainsize - 1;
4911 chunk_minus_1 = grainsize;
4914 upper = lower + st * chunk_minus_1;
4918 if (i == num_tasks - 1) {
4921 KMP_DEBUG_ASSERT(upper == *ub);
4922 if (upper == ub_glob)
4924 }
else if (st > 0) {
4925 KMP_DEBUG_ASSERT((kmp_uint64)st > *ub - upper);
4926 if ((kmp_uint64)st > ub_glob - upper)
4929 KMP_DEBUG_ASSERT(upper + st < *ub);
4930 if (upper - ub_glob < (kmp_uint64)(-st))
4936 next_task = __kmp_task_dup_alloc(thread, task, 0);
4938 next_task = __kmp_task_dup_alloc(thread, task);
4941 kmp_taskdata_t *next_taskdata = KMP_TASK_TO_TASKDATA(next_task);
4942 kmp_taskloop_bounds_t next_task_bounds =
4943 kmp_taskloop_bounds_t(next_task, task_bounds);
4946 next_task_bounds.set_lb(lower);
4947 if (next_taskdata->td_flags.native) {
4948 next_task_bounds.set_ub(upper + (st > 0 ? 1 : -1));
4950 next_task_bounds.set_ub(upper);
4952 if (ptask_dup != NULL)
4954 ptask_dup(next_task, task, lastpriv);
4956 (
"__kmp_taskloop_linear: T#%d; task #%llu: task %p: lower %lld, "
4957 "upper %lld stride %lld, (offsets %p %p)\n",
4958 gtid, i, next_task, lower, upper, st,
4959 next_task_bounds.get_lower_offset(),
4960 next_task_bounds.get_upper_offset()));
4962 __kmp_omp_taskloop_task(NULL, gtid, next_task,
4965 if (ompt_enabled.ompt_callback_dispatch) {
4966 OMPT_GET_DISPATCH_CHUNK(next_taskdata->ompt_task_info.dispatch_chunk,
4971 __kmp_omp_task(gtid, next_task,
true);
4976 __kmp_task_start(gtid, task, current_task);
4978 __kmp_task_finish<false>(gtid, task, current_task);
4983typedef struct __taskloop_params {
4990 kmp_uint64 num_tasks;
4991 kmp_uint64 grainsize;
4993 kmp_int64 last_chunk;
4995 kmp_uint64 num_t_min;
4999} __taskloop_params_t;
5001void __kmp_taskloop_recur(
ident_t *,
int, kmp_task_t *, kmp_uint64 *,
5002 kmp_uint64 *, kmp_int64, kmp_uint64, kmp_uint64,
5003 kmp_uint64, kmp_uint64, kmp_int64, kmp_uint64,
5011int __kmp_taskloop_task(
int gtid,
void *ptask) {
5012 __taskloop_params_t *p =
5013 (__taskloop_params_t *)((kmp_task_t *)ptask)->shareds;
5014 kmp_task_t *task = p->task;
5015 kmp_uint64 *lb = p->lb;
5016 kmp_uint64 *ub = p->ub;
5017 void *task_dup = p->task_dup;
5019 kmp_int64 st = p->st;
5020 kmp_uint64 ub_glob = p->ub_glob;
5021 kmp_uint64 num_tasks = p->num_tasks;
5022 kmp_uint64 grainsize = p->grainsize;
5023 kmp_uint64 extras = p->extras;
5024 kmp_int64 last_chunk = p->last_chunk;
5025 kmp_uint64 tc = p->tc;
5026 kmp_uint64 num_t_min = p->num_t_min;
5028 void *codeptr_ra = p->codeptr_ra;
5031 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
5032 KMP_DEBUG_ASSERT(task != NULL);
5034 (
"__kmp_taskloop_task: T#%d, task %p: %lld tasks, grainsize"
5035 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
5036 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
5039 KMP_DEBUG_ASSERT(num_tasks * 2 + 1 > num_t_min);
5040 if (num_tasks > num_t_min)
5041 __kmp_taskloop_recur(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
5042 grainsize, extras, last_chunk, tc, num_t_min,
5048 __kmp_taskloop_linear(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
5049 grainsize, extras, last_chunk, tc,
5055 KA_TRACE(40, (
"__kmp_taskloop_task(exit): T#%d\n", gtid));
5077void __kmp_taskloop_recur(
ident_t *loc,
int gtid, kmp_task_t *task,
5078 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
5079 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
5080 kmp_uint64 grainsize, kmp_uint64 extras,
5081 kmp_int64 last_chunk, kmp_uint64 tc,
5082 kmp_uint64 num_t_min,
5087 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
5088 KMP_DEBUG_ASSERT(task != NULL);
5089 KMP_DEBUG_ASSERT(num_tasks > num_t_min);
5091 (
"__kmp_taskloop_recur: T#%d, task %p: %lld tasks, grainsize"
5092 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
5093 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
5095 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
5096 kmp_uint64 lower = *lb;
5097 kmp_info_t *thread = __kmp_threads[gtid];
5099 kmp_task_t *next_task;
5100 size_t lower_offset =
5101 (
char *)lb - (
char *)task;
5102 size_t upper_offset =
5103 (
char *)ub - (
char *)task;
5105 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
5106 (last_chunk < 0 ? last_chunk : extras));
5107 KMP_DEBUG_ASSERT(num_tasks > extras);
5108 KMP_DEBUG_ASSERT(num_tasks > 0);
5111 kmp_uint64 lb1, ub0, tc0, tc1, ext0, ext1;
5112 kmp_int64 last_chunk0 = 0, last_chunk1 = 0;
5113 kmp_uint64 gr_size0 = grainsize;
5114 kmp_uint64 n_tsk0 = num_tasks >> 1;
5115 kmp_uint64 n_tsk1 = num_tasks - n_tsk0;
5116 if (last_chunk < 0) {
5118 last_chunk1 = last_chunk;
5119 tc0 = grainsize * n_tsk0;
5121 }
else if (n_tsk0 <= extras) {
5124 ext1 = extras - n_tsk0;
5125 tc0 = gr_size0 * n_tsk0;
5130 tc1 = grainsize * n_tsk1;
5133 ub0 = lower + st * (tc0 - 1);
5138 next_task = __kmp_task_dup_alloc(thread, task,
5141 next_task = __kmp_task_dup_alloc(thread, task);
5144 *(kmp_uint64 *)((
char *)next_task + lower_offset) = lb1;
5145 if (ptask_dup != NULL)
5146 ptask_dup(next_task, task, 0);
5151 kmp_taskdata_t *current_task = thread->th.th_current_task;
5152 thread->th.th_current_task = taskdata->td_parent;
5153 kmp_task_t *new_task =
5154 __kmpc_omp_task_alloc(loc, gtid, 1, 3 *
sizeof(
void *),
5155 sizeof(__taskloop_params_t), &__kmp_taskloop_task);
5157 thread->th.th_current_task = current_task;
5158 __taskloop_params_t *p = (__taskloop_params_t *)new_task->shareds;
5159 p->task = next_task;
5160 p->lb = (kmp_uint64 *)((
char *)next_task + lower_offset);
5161 p->ub = (kmp_uint64 *)((
char *)next_task + upper_offset);
5162 p->task_dup = task_dup;
5164 p->ub_glob = ub_glob;
5165 p->num_tasks = n_tsk1;
5166 p->grainsize = grainsize;
5168 p->last_chunk = last_chunk1;
5170 p->num_t_min = num_t_min;
5172 p->codeptr_ra = codeptr_ra;
5176 kmp_taskdata_t *new_task_data = KMP_TASK_TO_TASKDATA(new_task);
5177 new_task_data->tdg = taskdata->tdg;
5178 new_task_data->is_taskgraph = 0;
5183 __kmp_omp_taskloop_task(NULL, gtid, new_task, codeptr_ra);
5185 __kmp_omp_task(gtid, new_task,
true);
5189 if (n_tsk0 > num_t_min)
5190 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0, gr_size0,
5191 ext0, last_chunk0, tc0, num_t_min,
5197 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0,
5198 gr_size0, ext0, last_chunk0, tc0,
5204 KA_TRACE(40, (
"__kmp_taskloop_recur(exit): T#%d\n", gtid));
5207static void __kmp_taskloop(
ident_t *loc,
int gtid, kmp_task_t *task,
int if_val,
5208 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
5209 int nogroup,
int sched, kmp_uint64 grainsize,
5210 int modifier,
void *task_dup) {
5211 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
5212 KMP_DEBUG_ASSERT(task != NULL);
5214#if OMPT_SUPPORT && OMPT_OPTIONAL
5215 OMPT_STORE_RETURN_ADDRESS(gtid);
5217 __kmpc_taskgroup(loc, gtid);
5221 KMP_ATOMIC_DEC(&__kmp_tdg_task_id);
5225 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
5228 kmp_uint64 lower = task_bounds.get_lb();
5229 kmp_uint64 upper = task_bounds.get_ub();
5230 kmp_uint64 ub_glob = upper;
5231 kmp_uint64 num_tasks = 0, extras = 0;
5232 kmp_int64 last_chunk =
5234 kmp_uint64 num_tasks_min = __kmp_taskloop_min_tasks;
5235 kmp_info_t *thread = __kmp_threads[gtid];
5236 kmp_taskdata_t *current_task = thread->th.th_current_task;
5238 KA_TRACE(20, (
"__kmp_taskloop: T#%d, task %p, lb %lld, ub %lld, st %lld, "
5239 "grain %llu(%d, %d), dup %p\n",
5240 gtid, taskdata, lower, upper, st, grainsize, sched, modifier,
5245 tc = upper - lower + 1;
5246 }
else if (st < 0) {
5247 tc = (lower - upper) / (-st) + 1;
5249 tc = (upper - lower) / st + 1;
5252 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d zero-trip loop\n", gtid));
5254 __kmp_task_start(gtid, task, current_task);
5256 __kmp_task_finish<false>(gtid, task, current_task);
5260#if OMPT_SUPPORT && OMPT_OPTIONAL
5261 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
5262 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
5263 if (ompt_enabled.ompt_callback_work) {
5264 ompt_callbacks.ompt_callback(ompt_callback_work)(
5265 ompt_work_taskloop, ompt_scope_begin, &(team_info->parallel_data),
5266 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
5270 if (num_tasks_min == 0)
5273 KMP_MIN(thread->th.th_team_nproc * 10, INITIAL_TASK_DEQUE_SIZE);
5279 grainsize = thread->th.th_team_nproc * 10;
5282 if (grainsize > tc) {
5287 num_tasks = grainsize;
5288 grainsize = tc / num_tasks;
5289 extras = tc % num_tasks;
5293 if (grainsize > tc) {
5299 num_tasks = (tc + grainsize - 1) / grainsize;
5300 last_chunk = tc - (num_tasks * grainsize);
5303 num_tasks = tc / grainsize;
5305 grainsize = tc / num_tasks;
5306 extras = tc % num_tasks;
5311 KMP_ASSERT2(0,
"unknown scheduling of taskloop");
5314 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
5315 (last_chunk < 0 ? last_chunk : extras));
5316 KMP_DEBUG_ASSERT(num_tasks > extras);
5317 KMP_DEBUG_ASSERT(num_tasks > 0);
5323 taskdata->td_flags.task_serial = 1;
5324 taskdata->td_flags.tiedness = TASK_TIED;
5326 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5327 grainsize, extras, last_chunk, tc,
5329 OMPT_GET_RETURN_ADDRESS(0),
5334 }
else if (num_tasks > num_tasks_min && !taskdata->td_flags.native) {
5335 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go recursive: tc %llu, #tasks %llu"
5336 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
5337 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
5339 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5340 grainsize, extras, last_chunk, tc, num_tasks_min,
5342 OMPT_GET_RETURN_ADDRESS(0),
5346 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go linear: tc %llu, #tasks %llu"
5347 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
5348 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
5350 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5351 grainsize, extras, last_chunk, tc,
5353 OMPT_GET_RETURN_ADDRESS(0),
5358#if OMPT_SUPPORT && OMPT_OPTIONAL
5359 if (ompt_enabled.ompt_callback_work) {
5360 ompt_callbacks.ompt_callback(ompt_callback_work)(
5361 ompt_work_taskloop, ompt_scope_end, &(team_info->parallel_data),
5362 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
5367#if OMPT_SUPPORT && OMPT_OPTIONAL
5368 OMPT_STORE_RETURN_ADDRESS(gtid);
5370 __kmpc_end_taskgroup(loc, gtid);
5372 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d\n", gtid));
5392 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
int nogroup,
5393 int sched, kmp_uint64 grainsize,
void *task_dup) {
5394 __kmp_assert_valid_gtid(gtid);
5395 KA_TRACE(20, (
"__kmpc_taskloop(enter): T#%d\n", gtid));
5396 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
5398 KA_TRACE(20, (
"__kmpc_taskloop(exit): T#%d\n", gtid));
5419 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
5420 int nogroup,
int sched, kmp_uint64 grainsize,
5421 int modifier,
void *task_dup) {
5422 __kmp_assert_valid_gtid(gtid);
5423 KA_TRACE(20, (
"__kmpc_taskloop_5(enter): T#%d\n", gtid));
5424 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
5425 modifier, task_dup);
5426 KA_TRACE(20, (
"__kmpc_taskloop_5(exit): T#%d\n", gtid));
5438 if (gtid == KMP_GTID_DNE)
5441 kmp_info_t *thread = __kmp_thread_from_gtid(gtid);
5442 kmp_taskdata_t *taskdata = thread->th.th_current_task;
5447 return &taskdata->td_target_data.async_handle;
5459 if (gtid == KMP_GTID_DNE)
5462 kmp_info_t *thread = __kmp_thread_from_gtid(gtid);
5463 kmp_taskdata_t *taskdata = thread->th.th_current_task;
5468 return taskdata->td_task_team != NULL;
5477static kmp_tdg_info_t *__kmp_find_tdg(kmp_int32 tdg_id) {
5478 kmp_tdg_info_t *res =
nullptr;
5479 if (__kmp_max_tdgs == 0)
5482 if (__kmp_global_tdgs == NULL)
5483 __kmp_global_tdgs = (kmp_tdg_info_t **)__kmp_allocate(
5484 sizeof(kmp_tdg_info_t *) * __kmp_max_tdgs);
5486 if ((__kmp_global_tdgs[tdg_id]) &&
5487 (__kmp_global_tdgs[tdg_id]->tdg_status != KMP_TDG_NONE))
5488 res = __kmp_global_tdgs[tdg_id];
5494void __kmp_print_tdg_dot(kmp_tdg_info_t *tdg) {
5495 kmp_int32 tdg_id = tdg->tdg_id;
5496 KA_TRACE(10, (
"__kmp_print_tdg_dot(enter): T#%d tdg_id=%d \n", gtid, tdg_id));
5499 sprintf(file_name,
"tdg_%d.dot", tdg_id);
5502 kmp_int32 num_tasks = KMP_ATOMIC_LD_RLX(&tdg->num_tasks);
5506 " subgraph cluster {\n"
5509 for (kmp_int32 i = 0; i < num_tasks; i++) {
5510 fprintf(tdg_file,
" %d[style=bold]\n", i);
5512 fprintf(tdg_file,
" }\n");
5513 for (kmp_int32 i = 0; i < num_tasks; i++) {
5514 kmp_int32 nsuccessors = tdg->record_map[i].nsuccessors;
5515 kmp_int32 *successors = tdg->record_map[i].successors;
5516 if (nsuccessors > 0) {
5517 for (kmp_int32 j = 0; j < nsuccessors; j++)
5518 fprintf(tdg_file,
" %d -> %d \n", i, successors[j]);
5521 fprintf(tdg_file,
"}");
5522 KA_TRACE(10, (
"__kmp_print_tdg_dot(exit): T#%d tdg_id=%d \n", gtid, tdg_id));
5529void __kmp_exec_tdg(kmp_int32 gtid, kmp_tdg_info_t *tdg) {
5530 KMP_DEBUG_ASSERT(tdg->tdg_status == KMP_TDG_READY);
5531 KA_TRACE(10, (
"__kmp_exec_tdg(enter): T#%d tdg_id=%d num_roots=%d\n", gtid,
5532 tdg->tdg_id, tdg->num_roots));
5533 kmp_node_info_t *this_record_map = tdg->record_map;
5534 kmp_int32 *this_root_tasks = tdg->root_tasks;
5535 kmp_int32 this_num_roots = tdg->num_roots;
5536 kmp_int32 this_num_tasks = KMP_ATOMIC_LD_RLX(&tdg->num_tasks);
5538 kmp_info_t *thread = __kmp_threads[gtid];
5539 kmp_taskdata_t *parent_task = thread->th.th_current_task;
5541 if (tdg->rec_taskred_data) {
5545 for (kmp_int32 j = 0; j < this_num_tasks; j++) {
5546 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(this_record_map[j].task);
5548 td->td_parent = parent_task;
5549 this_record_map[j].parent_task = parent_task;
5551 kmp_taskgroup_t *parent_taskgroup =
5552 this_record_map[j].parent_task->td_taskgroup;
5554 KMP_ATOMIC_ST_RLX(&this_record_map[j].npredecessors_counter,
5555 this_record_map[j].npredecessors);
5556 KMP_ATOMIC_INC(&this_record_map[j].parent_task->td_incomplete_child_tasks);
5558 if (parent_taskgroup) {
5559 KMP_ATOMIC_INC(&parent_taskgroup->count);
5561 td->td_taskgroup = parent_taskgroup;
5562 }
else if (td->td_taskgroup !=
nullptr) {
5564 td->td_taskgroup =
nullptr;
5566 if (this_record_map[j].parent_task->td_flags.tasktype == TASK_EXPLICIT)
5567 KMP_ATOMIC_INC(&this_record_map[j].parent_task->td_allocated_child_tasks);
5570 for (kmp_int32 j = 0; j < this_num_roots; ++j) {
5571 __kmp_omp_task(gtid, this_record_map[this_root_tasks[j]].task,
true);
5573 KA_TRACE(10, (
"__kmp_exec_tdg(exit): T#%d tdg_id=%d num_roots=%d\n", gtid,
5574 tdg->tdg_id, tdg->num_roots));
5582static inline void __kmp_start_record(kmp_int32 gtid,
5583 kmp_taskgraph_flags_t *flags,
5585 kmp_tdg_info_t *tdg =
5586 (kmp_tdg_info_t *)__kmp_allocate(
sizeof(kmp_tdg_info_t));
5587 __kmp_global_tdgs[__kmp_curr_tdg_idx] = tdg;
5589 tdg->tdg_id = tdg_id;
5590 tdg->map_size = INIT_MAPSIZE;
5591 tdg->num_roots = -1;
5592 tdg->root_tasks =
nullptr;
5593 tdg->tdg_status = KMP_TDG_RECORDING;
5594 tdg->rec_num_taskred = 0;
5595 tdg->rec_taskred_data =
nullptr;
5596 KMP_ATOMIC_ST_RLX(&tdg->num_tasks, 0);
5599 kmp_node_info_t *this_record_map =
5600 (kmp_node_info_t *)__kmp_allocate(INIT_MAPSIZE *
sizeof(kmp_node_info_t));
5601 for (kmp_int32 i = 0; i < INIT_MAPSIZE; i++) {
5602 kmp_int32 *successorsList =
5603 (kmp_int32 *)__kmp_allocate(__kmp_successors_size *
sizeof(kmp_int32));
5604 this_record_map[i].task =
nullptr;
5605 this_record_map[i].successors = successorsList;
5606 this_record_map[i].nsuccessors = 0;
5607 this_record_map[i].npredecessors = 0;
5608 this_record_map[i].successors_size = __kmp_successors_size;
5609 KMP_ATOMIC_ST_RLX(&this_record_map[i].npredecessors_counter, 0);
5612 __kmp_global_tdgs[__kmp_curr_tdg_idx]->record_map = this_record_map;
5622kmp_int32 __kmpc_start_record_task(
ident_t *loc_ref, kmp_int32 gtid,
5623 kmp_int32 input_flags, kmp_int32 tdg_id) {
5626 kmp_taskgraph_flags_t *flags = (kmp_taskgraph_flags_t *)&input_flags;
5628 (
"__kmpc_start_record_task(enter): T#%d loc=%p flags=%d tdg_id=%d\n",
5629 gtid, loc_ref, input_flags, tdg_id));
5631 if (__kmp_max_tdgs == 0) {
5634 (
"__kmpc_start_record_task(abandon): T#%d loc=%p flags=%d tdg_id = %d, "
5635 "__kmp_max_tdgs = 0\n",
5636 gtid, loc_ref, input_flags, tdg_id));
5640 __kmpc_taskgroup(loc_ref, gtid);
5641 if (kmp_tdg_info_t *tdg = __kmp_find_tdg(tdg_id)) {
5643 __kmp_exec_tdg(gtid, tdg);
5646 __kmp_curr_tdg_idx = tdg_id;
5647 KMP_DEBUG_ASSERT(__kmp_curr_tdg_idx < __kmp_max_tdgs);
5648 __kmp_start_record(gtid, flags, tdg_id);
5652 KA_TRACE(10, (
"__kmpc_start_record_task(exit): T#%d TDG %d starts to %s\n",
5653 gtid, tdg_id, res ?
"record" :
"execute"));
5660void __kmp_end_record(kmp_int32 gtid, kmp_tdg_info_t *tdg) {
5662 kmp_node_info_t *this_record_map = tdg->record_map;
5663 kmp_int32 this_num_tasks = KMP_ATOMIC_LD_RLX(&tdg->num_tasks);
5664 kmp_int32 *this_root_tasks =
5665 (kmp_int32 *)__kmp_allocate(this_num_tasks *
sizeof(kmp_int32));
5666 kmp_int32 this_map_size = tdg->map_size;
5667 kmp_int32 this_num_roots = 0;
5668 kmp_info_t *thread = __kmp_threads[gtid];
5670 for (kmp_int32 i = 0; i < this_num_tasks; i++) {
5671 if (this_record_map[i].npredecessors == 0) {
5672 this_root_tasks[this_num_roots++] = i;
5677 tdg->map_size = this_map_size;
5678 tdg->num_roots = this_num_roots;
5679 tdg->root_tasks = this_root_tasks;
5680 KMP_DEBUG_ASSERT(tdg->tdg_status == KMP_TDG_RECORDING);
5681 tdg->tdg_status = KMP_TDG_READY;
5683 if (thread->th.th_current_task->td_dephash) {
5684 __kmp_dephash_free(thread, thread->th.th_current_task->td_dephash);
5685 thread->th.th_current_task->td_dephash = NULL;
5689 for (kmp_int32 i = 0; i < this_num_tasks; i++) {
5690 KMP_ATOMIC_ST_RLX(&this_record_map[i].npredecessors_counter,
5691 this_record_map[i].npredecessors);
5693 KMP_ATOMIC_ST_RLX(&__kmp_tdg_task_id, 0);
5696 __kmp_print_tdg_dot(tdg);
5706void __kmpc_end_record_task(
ident_t *loc_ref, kmp_int32 gtid,
5707 kmp_int32 input_flags, kmp_int32 tdg_id) {
5708 kmp_tdg_info_t *tdg = __kmp_find_tdg(tdg_id);
5710 KA_TRACE(10, (
"__kmpc_end_record_task(enter): T#%d loc=%p finishes recording"
5711 " tdg=%d with flags=%d\n",
5712 gtid, loc_ref, tdg_id, input_flags));
5713 if (__kmp_max_tdgs) {
5715 __kmpc_end_taskgroup(loc_ref, gtid);
5716 if (__kmp_tdg_is_recording(tdg->tdg_status))
5717 __kmp_end_record(gtid, tdg);
5719 KA_TRACE(10, (
"__kmpc_end_record_task(exit): T#%d loc=%p finished recording"
5720 " tdg=%d, its status is now READY\n",
5721 gtid, loc_ref, tdg_id));
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_task_reduction_get_th_data(int gtid, void *tskgrp, void *data)
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_task_reduction_modifier_init(ident_t *loc, int gtid, int is_ws, int num, void *data)
void * __kmpc_taskred_modifier_init(ident_t *loc, int gtid, int is_ws, int num, void *data)
bool __kmpc_omp_has_task_team(kmp_int32 gtid)
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_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_task_reduction_init(int gtid, int num, void *data)
void __kmpc_proxy_task_completed(kmp_int32 gtid, kmp_task_t *ptask)
void * __kmpc_taskred_init(int gtid, int num, void *data)
void ** __kmpc_omp_get_target_async_handle_ptr(kmp_int32 gtid)
kmp_taskred_flags_t flags