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 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(taskdata), 0,
817 __ompt_task_start(task, current_task, gtid);
821 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(exit): T#%d loc=%p task=%p,\n", gtid,
827static void __kmpc_omp_task_begin_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
830 void *return_address) {
831 __kmpc_omp_task_begin_if0_template<true>(loc_ref, gtid, task, frame_address,
848__attribute__((target(
"backchain")))
850void __kmpc_omp_task_begin_if0(
ident_t *loc_ref, kmp_int32 gtid,
853 if (UNLIKELY(ompt_enabled.enabled)) {
854 OMPT_STORE_RETURN_ADDRESS(gtid);
855 __kmpc_omp_task_begin_if0_ompt(loc_ref, gtid, task,
856 OMPT_GET_FRAME_ADDRESS(1),
857 OMPT_LOAD_RETURN_ADDRESS(gtid));
861 __kmpc_omp_task_begin_if0_template<false>(loc_ref, gtid, task, NULL, NULL);
867void __kmpc_omp_task_begin(
ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task) {
868 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
872 (
"__kmpc_omp_task_begin(enter): T#%d loc=%p task=%p current_task=%p\n",
873 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task), current_task));
875 __kmp_task_start(gtid, task, current_task);
877 KA_TRACE(10, (
"__kmpc_omp_task_begin(exit): T#%d loc=%p task=%p,\n", gtid,
878 loc_ref, KMP_TASK_TO_TASKDATA(task)));
888static void __kmp_free_task(kmp_int32 gtid, kmp_taskdata_t *taskdata,
889 kmp_info_t *thread) {
890 KA_TRACE(30, (
"__kmp_free_task: T#%d freeing data from task %p\n", gtid,
894 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
895 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0);
896 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 1);
897 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
898 KMP_DEBUG_ASSERT(taskdata->td_allocated_child_tasks == 0 ||
899 taskdata->td_flags.task_serial == 1);
900 KMP_DEBUG_ASSERT(taskdata->td_incomplete_child_tasks == 0);
901 kmp_task_t *task = KMP_TASKDATA_TO_TASK(taskdata);
903 task->data1.destructors = NULL;
904 task->data2.priority = 0;
906 taskdata->td_flags.freed = 1;
909 if (!taskdata->is_taskgraph) {
913 __kmp_fast_free(thread, taskdata);
915 __kmp_thread_free(thread, taskdata);
919 taskdata->td_flags.complete = 0;
920 taskdata->td_flags.started = 0;
921 taskdata->td_flags.freed = 0;
922 taskdata->td_flags.executing = 0;
923 taskdata->td_flags.task_serial =
924 (taskdata->td_parent->td_flags.final ||
925 taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser);
928 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
929 KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0);
931 KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1);
935 KA_TRACE(20, (
"__kmp_free_task: T#%d freed task %p\n", gtid, taskdata));
944static void __kmp_free_task_and_ancestors(kmp_int32 gtid,
945 kmp_taskdata_t *taskdata,
946 kmp_info_t *thread) {
949 kmp_int32 team_serial =
950 (taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) &&
951 !taskdata->td_flags.proxy;
952 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
954 kmp_int32 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
955 KMP_DEBUG_ASSERT(children >= 0);
958 while (children == 0) {
959 kmp_taskdata_t *parent_taskdata = taskdata->td_parent;
961 KA_TRACE(20, (
"__kmp_free_task_and_ancestors(enter): T#%d task %p complete "
962 "and freeing itself\n",
966 __kmp_free_task(gtid, taskdata, thread);
968 taskdata = parent_taskdata;
974 if (taskdata->td_flags.tasktype == TASK_IMPLICIT) {
975 if (taskdata->td_dephash) {
976 int children = KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks);
977 kmp_tasking_flags_t flags_old = taskdata->td_flags;
978 if (children == 0 && flags_old.complete == 1) {
979 kmp_tasking_flags_t flags_new = flags_old;
980 flags_new.complete = 0;
981 if (KMP_COMPARE_AND_STORE_ACQ32(
982 RCAST(kmp_int32 *, &taskdata->td_flags),
983 *RCAST(kmp_int32 *, &flags_old),
984 *RCAST(kmp_int32 *, &flags_new))) {
985 KA_TRACE(100, (
"__kmp_free_task_and_ancestors: T#%d cleans "
986 "dephash of implicit task %p\n",
989 __kmp_dephash_free_entries(thread, taskdata->td_dephash);
996 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
997 KMP_DEBUG_ASSERT(children >= 0);
1001 20, (
"__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; "
1002 "not freeing it yet\n",
1003 gtid, taskdata, children));
1014static bool __kmp_track_children_task(kmp_taskdata_t *taskdata) {
1015 kmp_tasking_flags_t flags = taskdata->td_flags;
1016 bool ret = !(flags.team_serial || flags.tasking_ser);
1017 ret = ret || flags.proxy == TASK_PROXY ||
1018 flags.detachable == TASK_DETACHABLE || flags.hidden_helper;
1020 KMP_ATOMIC_LD_ACQ(&taskdata->td_parent->td_incomplete_child_tasks) > 0;
1022 if (taskdata->td_taskgroup && taskdata->is_taskgraph)
1023 ret = ret || KMP_ATOMIC_LD_ACQ(&taskdata->td_taskgroup->count) > 0;
1038static void __kmp_task_finish(kmp_int32 gtid, kmp_task_t *task,
1039 kmp_taskdata_t *resumed_task) {
1040 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
1041 kmp_info_t *thread = __kmp_threads[gtid];
1042 kmp_task_team_t *task_team =
1043 thread->th.th_task_team;
1049 kmp_int32 children = 0;
1051 KA_TRACE(10, (
"__kmp_task_finish(enter): T#%d finishing task %p and resuming "
1053 gtid, taskdata, resumed_task));
1055 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
1058 is_taskgraph = taskdata->is_taskgraph;
1062#ifdef BUILD_TIED_TASK_STACK
1063 if (taskdata->td_flags.tiedness == TASK_TIED) {
1064 __kmp_pop_task_stack(gtid, thread, taskdata);
1068 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
1071 kmp_int32 counter = KMP_ATOMIC_DEC(&taskdata->td_untied_count) - 1;
1074 (
"__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n",
1075 gtid, counter, taskdata));
1079 if (resumed_task == NULL) {
1080 KMP_DEBUG_ASSERT(taskdata->td_flags.task_serial);
1081 resumed_task = taskdata->td_parent;
1084 thread->th.th_current_task = resumed_task;
1085 resumed_task->td_flags.executing = 1;
1086 KA_TRACE(10, (
"__kmp_task_finish(exit): T#%d partially done task %p, "
1087 "resuming task %p\n",
1088 gtid, taskdata, resumed_task));
1096 (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) ==
1097 taskdata->td_flags.task_serial);
1098 if (taskdata->td_flags.task_serial) {
1099 if (resumed_task == NULL) {
1100 resumed_task = taskdata->td_parent;
1104 KMP_DEBUG_ASSERT(resumed_task !=
1114 if (UNLIKELY(taskdata->td_flags.destructors_thunk)) {
1115 kmp_routine_entry_t destr_thunk = task->data1.destructors;
1116 KMP_ASSERT(destr_thunk);
1117 destr_thunk(gtid, task);
1120 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
1121 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 1);
1122 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
1124 bool completed =
true;
1125 if (UNLIKELY(taskdata->td_flags.detachable == TASK_DETACHABLE)) {
1126 if (taskdata->td_allow_completion_event.type ==
1127 KMP_EVENT_ALLOW_COMPLETION) {
1129 __kmp_acquire_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
1130 if (taskdata->td_allow_completion_event.type ==
1131 KMP_EVENT_ALLOW_COMPLETION) {
1133 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
1134 taskdata->td_flags.executing = 0;
1141 __ompt_task_finish(task, resumed_task, ompt_task_detach);
1147 taskdata->td_flags.proxy = TASK_PROXY;
1150 __kmp_release_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
1155 if (taskdata->td_target_data.async_handle != NULL) {
1159 __kmpc_give_task(task, __kmp_tid_from_gtid(gtid));
1160 if (KMP_HIDDEN_HELPER_THREAD(gtid))
1161 __kmp_hidden_helper_worker_thread_signal();
1166 taskdata->td_flags.complete = 1;
1168 taskdata->td_flags.onced = 1;
1174 __ompt_task_finish(task, resumed_task, ompt_task_complete);
1178 if (__kmp_track_children_task(taskdata)) {
1179 __kmp_release_deps(gtid, taskdata);
1184 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks);
1185 KMP_DEBUG_ASSERT(children >= 0);
1187 if (taskdata->td_taskgroup && !taskdata->is_taskgraph)
1189 if (taskdata->td_taskgroup)
1191 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
1192 }
else if (task_team && (task_team->tt.tt_found_proxy_tasks ||
1193 task_team->tt.tt_hidden_helper_task_encountered)) {
1196 __kmp_release_deps(gtid, taskdata);
1202 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
1203 taskdata->td_flags.executing = 0;
1206 if (taskdata->td_flags.hidden_helper) {
1208 KMP_ASSERT(KMP_HIDDEN_HELPER_THREAD(gtid));
1209 KMP_ATOMIC_DEC(&__kmp_unexecuted_hidden_helper_tasks);
1214 20, (
"__kmp_task_finish: T#%d finished task %p, %d incomplete children\n",
1215 gtid, taskdata, children));
1221 thread->th.th_current_task = resumed_task;
1223 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
1227 resumed_task->td_flags.executing = 1;
1230 if (is_taskgraph && __kmp_track_children_task(taskdata) &&
1231 taskdata->td_taskgroup) {
1238 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
1243 10, (
"__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n",
1244 gtid, taskdata, resumed_task));
1250static void __kmpc_omp_task_complete_if0_template(
ident_t *loc_ref,
1253 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n",
1254 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
1255 KMP_DEBUG_ASSERT(gtid >= 0);
1257 __kmp_task_finish<ompt>(gtid, task, NULL);
1259 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n",
1260 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
1264 ompt_frame_t *ompt_frame;
1265 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
1266 ompt_frame->enter_frame = ompt_data_none;
1267 ompt_frame->enter_frame_flags =
1268 ompt_frame_runtime | ompt_frame_framepointer;
1277void __kmpc_omp_task_complete_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
1279 __kmpc_omp_task_complete_if0_template<true>(loc_ref, gtid, task);
1288void __kmpc_omp_task_complete_if0(
ident_t *loc_ref, kmp_int32 gtid,
1291 if (UNLIKELY(ompt_enabled.enabled)) {
1292 __kmpc_omp_task_complete_if0_ompt(loc_ref, gtid, task);
1296 __kmpc_omp_task_complete_if0_template<false>(loc_ref, gtid, task);
1302void __kmpc_omp_task_complete(
ident_t *loc_ref, kmp_int32 gtid,
1304 KA_TRACE(10, (
"__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n", gtid,
1305 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1307 __kmp_task_finish<false>(gtid, task,
1310 KA_TRACE(10, (
"__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n", gtid,
1311 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1327void __kmp_init_implicit_task(
ident_t *loc_ref, kmp_info_t *this_thr,
1328 kmp_team_t *team,
int tid,
int set_curr_task) {
1329 kmp_taskdata_t *task = &team->t.t_implicit_task_taskdata[tid];
1333 (
"__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n",
1334 tid, team, task, set_curr_task ?
"TRUE" :
"FALSE"));
1336 task->td_task_id = KMP_GEN_TASK_ID();
1337 task->td_team = team;
1340 task->td_ident = loc_ref;
1341 task->td_taskwait_ident = NULL;
1342 task->td_taskwait_counter = 0;
1343 task->td_taskwait_thread = 0;
1345 task->td_flags.tiedness = TASK_TIED;
1346 task->td_flags.tasktype = TASK_IMPLICIT;
1347 task->td_flags.proxy = TASK_FULL;
1350 task->td_flags.task_serial = 1;
1351 task->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1352 task->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1354 task->td_flags.started = 1;
1355 task->td_flags.executing = 1;
1356 task->td_flags.complete = 0;
1357 task->td_flags.freed = 0;
1359 task->td_flags.onced = 0;
1362 task->td_depnode = NULL;
1363 task->td_last_tied = task;
1364 task->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1366 if (set_curr_task) {
1367 KMP_ATOMIC_ST_REL(&task->td_incomplete_child_tasks, 0);
1369 KMP_ATOMIC_ST_REL(&task->td_allocated_child_tasks, 0);
1370 task->td_taskgroup = NULL;
1371 task->td_dephash = NULL;
1372 __kmp_push_current_task_to_thread(this_thr, team, tid);
1374 KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0);
1375 KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0);
1379 if (UNLIKELY(ompt_enabled.enabled))
1380 __ompt_task_init(task, tid);
1383 KF_TRACE(10, (
"__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n", tid,
1392void __kmp_finish_implicit_task(kmp_info_t *thread) {
1393 kmp_taskdata_t *task = thread->th.th_current_task;
1394 if (task->td_dephash) {
1396 task->td_flags.complete = 1;
1398 task->td_flags.onced = 1;
1400 children = KMP_ATOMIC_LD_ACQ(&task->td_incomplete_child_tasks);
1401 kmp_tasking_flags_t flags_old = task->td_flags;
1402 if (children == 0 && flags_old.complete == 1) {
1403 kmp_tasking_flags_t flags_new = flags_old;
1404 flags_new.complete = 0;
1405 if (KMP_COMPARE_AND_STORE_ACQ32(RCAST(kmp_int32 *, &task->td_flags),
1406 *RCAST(kmp_int32 *, &flags_old),
1407 *RCAST(kmp_int32 *, &flags_new))) {
1408 KA_TRACE(100, (
"__kmp_finish_implicit_task: T#%d cleans "
1409 "dephash of implicit task %p\n",
1410 thread->th.th_info.ds.ds_gtid, task));
1411 __kmp_dephash_free_entries(thread, task->td_dephash);
1421void __kmp_free_implicit_task(kmp_info_t *thread) {
1422 kmp_taskdata_t *task = thread->th.th_current_task;
1423 if (task && task->td_dephash) {
1424 __kmp_dephash_free(thread, task->td_dephash);
1425 task->td_dephash = NULL;
1431static size_t __kmp_round_up_to_val(
size_t size,
size_t val) {
1432 if (size & (val - 1)) {
1434 if (size <= KMP_SIZE_T_MAX - val) {
1453kmp_task_t *__kmp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1454 kmp_tasking_flags_t *flags,
1455 size_t sizeof_kmp_task_t,
size_t sizeof_shareds,
1456 kmp_routine_entry_t task_entry) {
1458 kmp_taskdata_t *taskdata;
1459 kmp_info_t *thread = __kmp_threads[gtid];
1460 kmp_team_t *team = thread->th.th_team;
1461 kmp_taskdata_t *parent_task = thread->th.th_current_task;
1462 size_t shareds_offset;
1464 if (UNLIKELY(!TCR_4(__kmp_init_middle)))
1465 __kmp_middle_initialize();
1467 if (flags->hidden_helper) {
1468 if (__kmp_enable_hidden_helper) {
1469 if (!TCR_4(__kmp_init_hidden_helper))
1470 __kmp_hidden_helper_initialize();
1473 flags->hidden_helper = FALSE;
1477 KA_TRACE(10, (
"__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) "
1478 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1479 gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t,
1480 sizeof_shareds, task_entry));
1482 KMP_DEBUG_ASSERT(parent_task);
1483 if (parent_task->td_flags.final) {
1484 if (flags->merged_if0) {
1489 if (flags->tiedness == TASK_UNTIED && !team->t.t_serialized) {
1493 KMP_CHECK_UPDATE(thread->th.th_task_team->tt.tt_untied_task_encountered, 1);
1499 if (UNLIKELY(flags->proxy == TASK_PROXY ||
1500 flags->detachable == TASK_DETACHABLE || flags->hidden_helper)) {
1501 if (flags->proxy == TASK_PROXY) {
1502 flags->tiedness = TASK_UNTIED;
1503 flags->merged_if0 = 1;
1507 if ((thread->th.th_task_team) == NULL) {
1510 KMP_DEBUG_ASSERT(team->t.t_serialized);
1512 (
"T#%d creating task team in __kmp_task_alloc for proxy task\n",
1514 __kmp_task_team_setup(thread, team);
1515 thread->th.th_task_team = team->t.t_task_team[thread->th.th_task_state];
1517 kmp_task_team_t *task_team = thread->th.th_task_team;
1520 if (!KMP_TASKING_ENABLED(task_team)) {
1523 (
"T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid));
1524 __kmp_enable_tasking(task_team, thread);
1525 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
1526 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
1528 if (thread_data->td.td_deque == NULL) {
1529 __kmp_alloc_task_deque(thread, thread_data);
1533 if ((flags->proxy == TASK_PROXY || flags->detachable == TASK_DETACHABLE) &&
1534 task_team->tt.tt_found_proxy_tasks == FALSE)
1535 TCW_4(task_team->tt.tt_found_proxy_tasks, TRUE);
1536 if (flags->hidden_helper &&
1537 task_team->tt.tt_hidden_helper_task_encountered == FALSE)
1538 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, TRUE);
1543 shareds_offset =
sizeof(kmp_taskdata_t) + sizeof_kmp_task_t;
1544 shareds_offset = __kmp_round_up_to_val(shareds_offset,
sizeof(
void *));
1547 KA_TRACE(30, (
"__kmp_task_alloc: T#%d First malloc size: %ld\n", gtid,
1549 KA_TRACE(30, (
"__kmp_task_alloc: T#%d Second malloc size: %ld\n", gtid,
1554 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, shareds_offset +
1557 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, shareds_offset +
1561 task = KMP_TASKDATA_TO_TASK(taskdata);
1564#if KMP_ARCH_X86 || KMP_ARCH_PPC64 || KMP_ARCH_S390X || !KMP_HAVE_QUAD
1565 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(
double) - 1)) == 0);
1566 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(
double) - 1)) == 0);
1568 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(_Quad) - 1)) == 0);
1569 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(_Quad) - 1)) == 0);
1571 if (sizeof_shareds > 0) {
1573 task->shareds = &((
char *)taskdata)[shareds_offset];
1575 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
1578 task->shareds = NULL;
1580 task->routine = task_entry;
1583 taskdata->td_task_id = KMP_GEN_TASK_ID();
1584 taskdata->td_team = thread->th.th_team;
1585 taskdata->td_alloc_thread = thread;
1586 taskdata->td_parent = parent_task;
1587 taskdata->td_level = parent_task->td_level + 1;
1588 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
1589 taskdata->td_ident = loc_ref;
1590 taskdata->td_taskwait_ident = NULL;
1591 taskdata->td_taskwait_counter = 0;
1592 taskdata->td_taskwait_thread = 0;
1593 KMP_DEBUG_ASSERT(taskdata->td_parent != NULL);
1595 if (flags->proxy == TASK_FULL)
1596 copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs);
1598 taskdata->td_flags = *flags;
1599 taskdata->td_task_team = thread->th.th_task_team;
1600 taskdata->td_size_alloc = shareds_offset + sizeof_shareds;
1601 taskdata->td_flags.tasktype = TASK_EXPLICIT;
1604 if (flags->hidden_helper) {
1605 kmp_info_t *shadow_thread = __kmp_threads[KMP_GTID_TO_SHADOW_GTID(gtid)];
1606 taskdata->td_team = shadow_thread->th.th_team;
1607 taskdata->td_task_team = shadow_thread->th.th_task_team;
1611 taskdata->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1614 taskdata->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1620 taskdata->td_flags.task_serial =
1621 (parent_task->td_flags.final || taskdata->td_flags.team_serial ||
1622 taskdata->td_flags.tasking_ser || flags->merged_if0);
1624 taskdata->td_flags.started = 0;
1625 taskdata->td_flags.executing = 0;
1626 taskdata->td_flags.complete = 0;
1627 taskdata->td_flags.freed = 0;
1629 taskdata->td_flags.onced = 0;
1631 KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0);
1633 KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1);
1634 taskdata->td_taskgroup =
1635 parent_task->td_taskgroup;
1636 taskdata->td_dephash = NULL;
1637 taskdata->td_depnode = NULL;
1638 taskdata->td_target_data.async_handle = NULL;
1639 if (flags->tiedness == TASK_UNTIED)
1640 taskdata->td_last_tied = NULL;
1642 taskdata->td_last_tied = taskdata;
1643 taskdata->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1645 if (UNLIKELY(ompt_enabled.enabled))
1646 __ompt_task_init(taskdata, gtid);
1650 if (__kmp_track_children_task(taskdata)) {
1651 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
1652 if (parent_task->td_taskgroup)
1653 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
1656 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) {
1657 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
1659 if (flags->hidden_helper) {
1660 taskdata->td_flags.task_serial = FALSE;
1662 KMP_ATOMIC_INC(&__kmp_unexecuted_hidden_helper_tasks);
1667 kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_idx);
1668 if (tdg && __kmp_tdg_is_recording(tdg->tdg_status) &&
1669 (task_entry != (kmp_routine_entry_t)__kmp_taskloop_task)) {
1670 taskdata->is_taskgraph = 1;
1671 taskdata->tdg = __kmp_global_tdgs[__kmp_curr_tdg_idx];
1672 taskdata->td_task_id = KMP_ATOMIC_INC(&__kmp_tdg_task_id);
1675 KA_TRACE(20, (
"__kmp_task_alloc(exit): T#%d created task %p parent=%p\n",
1676 gtid, taskdata, taskdata->td_parent));
1681kmp_task_t *__kmpc_omp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1682 kmp_int32 flags,
size_t sizeof_kmp_task_t,
1683 size_t sizeof_shareds,
1684 kmp_routine_entry_t task_entry) {
1686 kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
1687 __kmp_assert_valid_gtid(gtid);
1688 input_flags->native = FALSE;
1690 KA_TRACE(10, (
"__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s %s) "
1691 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1692 gtid, loc_ref, input_flags->tiedness ?
"tied " :
"untied",
1693 input_flags->proxy ?
"proxy" :
"",
1694 input_flags->detachable ?
"detachable" :
"", sizeof_kmp_task_t,
1695 sizeof_shareds, task_entry));
1697 retval = __kmp_task_alloc(loc_ref, gtid, input_flags, sizeof_kmp_task_t,
1698 sizeof_shareds, task_entry);
1700 KA_TRACE(20, (
"__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval));
1705kmp_task_t *__kmpc_omp_target_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1707 size_t sizeof_kmp_task_t,
1708 size_t sizeof_shareds,
1709 kmp_routine_entry_t task_entry,
1710 kmp_int64 device_id) {
1711 auto &input_flags =
reinterpret_cast<kmp_tasking_flags_t &
>(flags);
1713 input_flags.tiedness = TASK_UNTIED;
1715 if (__kmp_enable_hidden_helper)
1716 input_flags.hidden_helper = TRUE;
1718 return __kmpc_omp_task_alloc(loc_ref, gtid, flags, sizeof_kmp_task_t,
1719 sizeof_shareds, task_entry);
1737 kmp_task_t *new_task, kmp_int32 naffins,
1738 kmp_task_affinity_info_t *affin_list) {
1748__attribute__((target(
"backchain")))
1751__kmp_invoke_task(kmp_int32 gtid, kmp_task_t *task,
1752 kmp_taskdata_t *current_task) {
1753 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
1757 30, (
"__kmp_invoke_task(enter): T#%d invoking task %p, current_task=%p\n",
1758 gtid, taskdata, current_task));
1759 KMP_DEBUG_ASSERT(task);
1760 if (UNLIKELY(taskdata->td_flags.proxy == TASK_PROXY &&
1761 taskdata->td_flags.complete == 1)) {
1766 (
"__kmp_invoke_task: T#%d running bottom finish for proxy task %p\n",
1769 __kmp_bottom_half_finish_proxy(gtid, task);
1771 KA_TRACE(30, (
"__kmp_invoke_task(exit): T#%d completed bottom finish for "
1772 "proxy task %p, resuming task %p\n",
1773 gtid, taskdata, current_task));
1781 ompt_thread_info_t oldInfo;
1782 if (UNLIKELY(ompt_enabled.enabled)) {
1784 thread = __kmp_threads[gtid];
1785 oldInfo = thread->th.ompt_thread_info;
1786 thread->th.ompt_thread_info.wait_id = 0;
1787 thread->th.ompt_thread_info.state = (thread->th.th_team_serialized)
1788 ? ompt_state_work_serial
1789 : ompt_state_work_parallel;
1790 taskdata->ompt_task_info.frame.exit_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1795 if (taskdata->td_flags.proxy != TASK_PROXY) {
1796 __kmp_task_start(gtid, task, current_task);
1802 if (UNLIKELY(__kmp_omp_cancellation)) {
1803 thread = __kmp_threads[gtid];
1804 kmp_team_t *this_team = thread->th.th_team;
1805 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
1806 if ((taskgroup && taskgroup->cancel_request) ||
1807 (this_team->t.t_cancel_request == cancel_parallel)) {
1808#if OMPT_SUPPORT && OMPT_OPTIONAL
1809 ompt_data_t *task_data;
1810 if (UNLIKELY(ompt_enabled.ompt_callback_cancel)) {
1811 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, NULL, NULL);
1812 ompt_callbacks.ompt_callback(ompt_callback_cancel)(
1814 ((taskgroup && taskgroup->cancel_request) ? ompt_cancel_taskgroup
1815 : ompt_cancel_parallel) |
1816 ompt_cancel_discarded_task,
1829 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
1830 taskdata->td_last_tied = current_task->td_last_tied;
1831 KMP_DEBUG_ASSERT(taskdata->td_last_tied);
1833#if KMP_STATS_ENABLED
1835 switch (KMP_GET_THREAD_STATE()) {
1836 case FORK_JOIN_BARRIER:
1837 KMP_PUSH_PARTITIONED_TIMER(OMP_task_join_bar);
1840 KMP_PUSH_PARTITIONED_TIMER(OMP_task_plain_bar);
1843 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskyield);
1846 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskwait);
1849 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskgroup);
1852 KMP_PUSH_PARTITIONED_TIMER(OMP_task_immediate);
1859 if (UNLIKELY(ompt_enabled.enabled))
1860 __ompt_task_start(task, current_task, gtid);
1862#if OMPT_SUPPORT && OMPT_OPTIONAL
1863 if (UNLIKELY(ompt_enabled.ompt_callback_dispatch &&
1864 taskdata->ompt_task_info.dispatch_chunk.iterations > 0)) {
1865 ompt_data_t instance = ompt_data_none;
1866 instance.ptr = &(taskdata->ompt_task_info.dispatch_chunk);
1867 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
1868 ompt_callbacks.ompt_callback(ompt_callback_dispatch)(
1869 &(team_info->parallel_data), &(taskdata->ompt_task_info.task_data),
1870 ompt_dispatch_taskloop_chunk, instance);
1871 taskdata->ompt_task_info.dispatch_chunk = {0, 0};
1876 if (ompd_state & OMPD_ENABLE_BP)
1877 ompd_bp_task_begin();
1880#if USE_ITT_BUILD && USE_ITT_NOTIFY
1881 kmp_uint64 cur_time;
1882 kmp_int32 kmp_itt_count_task =
1883 __kmp_forkjoin_frames_mode == 3 && !taskdata->td_flags.task_serial &&
1884 current_task->td_flags.tasktype == TASK_IMPLICIT;
1885 if (kmp_itt_count_task) {
1886 thread = __kmp_threads[gtid];
1888 if (thread->th.th_bar_arrive_time)
1889 cur_time = __itt_get_timestamp();
1891 kmp_itt_count_task = 0;
1893 KMP_FSYNC_ACQUIRED(taskdata);
1896#if ENABLE_LIBOMPTARGET
1897 if (taskdata->td_target_data.async_handle != NULL) {
1901 KMP_ASSERT(tgt_target_nowait_query);
1902 tgt_target_nowait_query(&taskdata->td_target_data.async_handle);
1905 if (task->routine != NULL) {
1906#ifdef KMP_GOMP_COMPAT
1907 if (taskdata->td_flags.native) {
1908 ((void (*)(
void *))(*(task->routine)))(task->shareds);
1912 (*(task->routine))(gtid, task);
1915 KMP_POP_PARTITIONED_TIMER();
1917#if USE_ITT_BUILD && USE_ITT_NOTIFY
1918 if (kmp_itt_count_task) {
1920 thread->th.th_bar_arrive_time += (__itt_get_timestamp() - cur_time);
1922 KMP_FSYNC_CANCEL(taskdata);
1923 KMP_FSYNC_RELEASING(taskdata->td_parent);
1928 if (ompd_state & OMPD_ENABLE_BP)
1933 if (taskdata->td_flags.proxy != TASK_PROXY) {
1935 if (UNLIKELY(ompt_enabled.enabled)) {
1936 thread->th.ompt_thread_info = oldInfo;
1937 if (taskdata->td_flags.tiedness == TASK_TIED) {
1938 taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
1940 __kmp_task_finish<true>(gtid, task, current_task);
1943 __kmp_task_finish<false>(gtid, task, current_task);
1948 (
"__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n",
1949 gtid, taskdata, current_task));
1963kmp_int32 __kmpc_omp_task_parts(
ident_t *loc_ref, kmp_int32 gtid,
1964 kmp_task_t *new_task) {
1965 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1967 KA_TRACE(10, (
"__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n", gtid,
1968 loc_ref, new_taskdata));
1971 kmp_taskdata_t *parent;
1972 if (UNLIKELY(ompt_enabled.enabled)) {
1973 parent = new_taskdata->td_parent;
1974 if (ompt_enabled.ompt_callback_task_create) {
1975 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1976 &(parent->ompt_task_info.task_data), &(parent->ompt_task_info.frame),
1977 &(new_taskdata->ompt_task_info.task_data), ompt_task_explicit, 0,
1978 OMPT_GET_RETURN_ADDRESS(0));
1986 if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1988 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1989 new_taskdata->td_flags.task_serial = 1;
1990 __kmp_invoke_task(gtid, new_task, current_task);
1995 (
"__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: "
1996 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
1997 gtid, loc_ref, new_taskdata));
2000 if (UNLIKELY(ompt_enabled.enabled)) {
2001 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
2004 return TASK_CURRENT_NOT_QUEUED;
2018kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task,
2019 bool serialize_immediate) {
2020 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
2023 if (new_taskdata->is_taskgraph &&
2024 __kmp_tdg_is_recording(new_taskdata->tdg->tdg_status)) {
2025 kmp_tdg_info_t *tdg = new_taskdata->tdg;
2027 if (new_taskdata->td_task_id >= new_taskdata->tdg->map_size) {
2028 __kmp_acquire_bootstrap_lock(&tdg->graph_lock);
2031 if (new_taskdata->td_task_id >= tdg->map_size) {
2032 kmp_uint old_size = tdg->map_size;
2033 kmp_uint new_size = old_size * 2;
2034 kmp_node_info_t *old_record = tdg->record_map;
2035 kmp_node_info_t *new_record = (kmp_node_info_t *)__kmp_allocate(
2036 new_size *
sizeof(kmp_node_info_t));
2038 KMP_MEMCPY(new_record, old_record, old_size *
sizeof(kmp_node_info_t));
2039 tdg->record_map = new_record;
2041 __kmp_free(old_record);
2043 for (kmp_int i = old_size; i < new_size; i++) {
2044 kmp_int32 *successorsList = (kmp_int32 *)__kmp_allocate(
2045 __kmp_successors_size *
sizeof(kmp_int32));
2046 new_record[i].task =
nullptr;
2047 new_record[i].successors = successorsList;
2048 new_record[i].nsuccessors = 0;
2049 new_record[i].npredecessors = 0;
2050 new_record[i].successors_size = __kmp_successors_size;
2051 KMP_ATOMIC_ST_REL(&new_record[i].npredecessors_counter, 0);
2055 tdg->map_size = new_size;
2057 __kmp_release_bootstrap_lock(&tdg->graph_lock);
2060 if (tdg->record_map[new_taskdata->td_task_id].task ==
nullptr) {
2061 tdg->record_map[new_taskdata->td_task_id].task = new_task;
2062 tdg->record_map[new_taskdata->td_task_id].parent_task =
2063 new_taskdata->td_parent;
2064 KMP_ATOMIC_INC(&tdg->num_tasks);
2071 if (new_taskdata->td_flags.proxy == TASK_PROXY ||
2072 __kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
2074 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
2075 if (serialize_immediate)
2076 new_taskdata->td_flags.task_serial = 1;
2077 __kmp_invoke_task(gtid, new_task, current_task);
2078 }
else if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME &&
2079 __kmp_wpolicy_passive) {
2080 kmp_info_t *this_thr = __kmp_threads[gtid];
2081 kmp_team_t *team = this_thr->th.th_team;
2082 kmp_int32 nthreads = this_thr->th.th_team_nproc;
2083 for (
int i = 0; i < nthreads; ++i) {
2084 kmp_info_t *thread = team->t.t_threads[i];
2085 if (thread == this_thr)
2087 if (thread->th.th_sleep_loc != NULL) {
2088 __kmp_null_resume_wrapper(thread);
2093 return TASK_CURRENT_NOT_QUEUED;
2108kmp_int32 __kmpc_omp_task(
ident_t *loc_ref, kmp_int32 gtid,
2109 kmp_task_t *new_task) {
2111 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
2113#if KMP_DEBUG || OMPT_SUPPORT
2114 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
2116 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
2118 __kmp_assert_valid_gtid(gtid);
2121 kmp_taskdata_t *parent = NULL;
2122 if (UNLIKELY(ompt_enabled.enabled)) {
2123 if (!new_taskdata->td_flags.started) {
2124 OMPT_STORE_RETURN_ADDRESS(gtid);
2125 parent = new_taskdata->td_parent;
2126 if (!parent->ompt_task_info.frame.enter_frame.ptr) {
2127 parent->ompt_task_info.frame.enter_frame.ptr =
2128 OMPT_GET_FRAME_ADDRESS(0);
2130 if (ompt_enabled.ompt_callback_task_create) {
2131 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
2132 &(parent->ompt_task_info.task_data),
2133 &(parent->ompt_task_info.frame),
2134 &(new_taskdata->ompt_task_info.task_data),
2135 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
2136 OMPT_LOAD_RETURN_ADDRESS(gtid));
2141 __ompt_task_finish(new_task,
2142 new_taskdata->ompt_task_info.scheduling_parent,
2144 new_taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
2149 res = __kmp_omp_task(gtid, new_task,
true);
2151 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
2152 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
2153 gtid, loc_ref, new_taskdata));
2155 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
2156 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
2175kmp_int32 __kmp_omp_taskloop_task(
ident_t *loc_ref, kmp_int32 gtid,
2176 kmp_task_t *new_task,
void *codeptr_ra) {
2178 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
2180#if KMP_DEBUG || OMPT_SUPPORT
2181 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
2183 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
2187 kmp_taskdata_t *parent = NULL;
2188 if (UNLIKELY(ompt_enabled.enabled && !new_taskdata->td_flags.started)) {
2189 parent = new_taskdata->td_parent;
2190 if (!parent->ompt_task_info.frame.enter_frame.ptr)
2191 parent->ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
2192 if (ompt_enabled.ompt_callback_task_create) {
2193 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
2194 &(parent->ompt_task_info.task_data), &(parent->ompt_task_info.frame),
2195 &(new_taskdata->ompt_task_info.task_data),
2196 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
2202 res = __kmp_omp_task(gtid, new_task,
true);
2204 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
2205 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
2206 gtid, loc_ref, new_taskdata));
2208 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
2209 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
2216static kmp_int32 __kmpc_omp_taskwait_template(
ident_t *loc_ref, kmp_int32 gtid,
2217 void *frame_address,
2218 void *return_address) {
2219 kmp_taskdata_t *taskdata =
nullptr;
2221 int thread_finished = FALSE;
2222 KMP_SET_THREAD_STATE_BLOCK(TASKWAIT);
2224 KA_TRACE(10, (
"__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref));
2225 KMP_DEBUG_ASSERT(gtid >= 0);
2227 if (__kmp_tasking_mode != tskm_immediate_exec) {
2228 thread = __kmp_threads[gtid];
2229 taskdata = thread->th.th_current_task;
2231#if OMPT_SUPPORT && OMPT_OPTIONAL
2232 ompt_data_t *my_task_data;
2233 ompt_data_t *my_parallel_data;
2236 my_task_data = &(taskdata->ompt_task_info.task_data);
2237 my_parallel_data = OMPT_CUR_TEAM_DATA(thread);
2239 taskdata->ompt_task_info.frame.enter_frame.ptr = frame_address;
2241 if (ompt_enabled.ompt_callback_sync_region) {
2242 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2243 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
2244 my_task_data, return_address);
2247 if (ompt_enabled.ompt_callback_sync_region_wait) {
2248 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2249 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
2250 my_task_data, return_address);
2260 taskdata->td_taskwait_counter += 1;
2261 taskdata->td_taskwait_ident = loc_ref;
2262 taskdata->td_taskwait_thread = gtid + 1;
2265 void *itt_sync_obj = NULL;
2267 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2272 !taskdata->td_flags.team_serial && !taskdata->td_flags.final;
2274 must_wait = must_wait || (thread->th.th_task_team != NULL &&
2275 thread->th.th_task_team->tt.tt_found_proxy_tasks);
2279 (__kmp_enable_hidden_helper && thread->th.th_task_team != NULL &&
2280 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered);
2283 kmp_flag_32<false, false> flag(
2284 RCAST(std::atomic<kmp_uint32> *,
2285 &(taskdata->td_incomplete_child_tasks)),
2287 while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) != 0) {
2288 flag.execute_tasks(thread, gtid, FALSE,
2289 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2290 __kmp_task_stealing_constraint);
2294 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2295 KMP_FSYNC_ACQUIRED(taskdata);
2300 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2302#if OMPT_SUPPORT && OMPT_OPTIONAL
2304 if (ompt_enabled.ompt_callback_sync_region_wait) {
2305 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2306 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
2307 my_task_data, return_address);
2309 if (ompt_enabled.ompt_callback_sync_region) {
2310 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2311 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
2312 my_task_data, return_address);
2314 taskdata->ompt_task_info.frame.enter_frame = ompt_data_none;
2319 KA_TRACE(10, (
"__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, "
2320 "returning TASK_CURRENT_NOT_QUEUED\n",
2323 return TASK_CURRENT_NOT_QUEUED;
2326#if OMPT_SUPPORT && OMPT_OPTIONAL
2328static kmp_int32 __kmpc_omp_taskwait_ompt(
ident_t *loc_ref, kmp_int32 gtid,
2329 void *frame_address,
2330 void *return_address) {
2331 return __kmpc_omp_taskwait_template<true>(loc_ref, gtid, frame_address,
2338kmp_int32 __kmpc_omp_taskwait(
ident_t *loc_ref, kmp_int32 gtid) {
2339#if OMPT_SUPPORT && OMPT_OPTIONAL
2340 if (UNLIKELY(ompt_enabled.enabled)) {
2341 OMPT_STORE_RETURN_ADDRESS(gtid);
2342 return __kmpc_omp_taskwait_ompt(loc_ref, gtid, OMPT_GET_FRAME_ADDRESS(0),
2343 OMPT_LOAD_RETURN_ADDRESS(gtid));
2346 return __kmpc_omp_taskwait_template<false>(loc_ref, gtid, NULL, NULL);
2350kmp_int32 __kmpc_omp_taskyield(
ident_t *loc_ref, kmp_int32 gtid,
int end_part) {
2351 kmp_taskdata_t *taskdata = NULL;
2353 int thread_finished = FALSE;
2356 KMP_SET_THREAD_STATE_BLOCK(TASKYIELD);
2358 KA_TRACE(10, (
"__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n",
2359 gtid, loc_ref, end_part));
2360 __kmp_assert_valid_gtid(gtid);
2362 if (__kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel) {
2363 thread = __kmp_threads[gtid];
2364 taskdata = thread->th.th_current_task;
2371 taskdata->td_taskwait_counter += 1;
2372 taskdata->td_taskwait_ident = loc_ref;
2373 taskdata->td_taskwait_thread = gtid + 1;
2376 void *itt_sync_obj = NULL;
2378 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2381 if (!taskdata->td_flags.team_serial) {
2382 kmp_task_team_t *task_team = thread->th.th_task_team;
2383 if (task_team != NULL) {
2384 if (KMP_TASKING_ENABLED(task_team)) {
2386 if (UNLIKELY(ompt_enabled.enabled))
2387 thread->th.ompt_thread_info.ompt_task_yielded = 1;
2389 __kmp_execute_tasks_32(
2390 thread, gtid, (kmp_flag_32<> *)NULL, FALSE,
2391 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2392 __kmp_task_stealing_constraint);
2394 if (UNLIKELY(ompt_enabled.enabled))
2395 thread->th.ompt_thread_info.ompt_task_yielded = 0;
2401 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2406 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2409 KA_TRACE(10, (
"__kmpc_omp_taskyield(exit): T#%d task %p resuming, "
2410 "returning TASK_CURRENT_NOT_QUEUED\n",
2413 return TASK_CURRENT_NOT_QUEUED;
2434 unsigned reserved31 : 31;
2514template <
typename T>
2515void *__kmp_task_reduction_init(
int gtid,
int num, T *data) {
2516 __kmp_assert_valid_gtid(gtid);
2517 kmp_info_t *thread = __kmp_threads[gtid];
2518 kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup;
2519 kmp_uint32 nth = thread->th.th_team_nproc;
2523 KMP_ASSERT(tg != NULL);
2524 KMP_ASSERT(data != NULL);
2525 KMP_ASSERT(num > 0);
2526 if (nth == 1 && !__kmp_enable_hidden_helper) {
2527 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n",
2531 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, taskgroup %p, #items %d\n",
2535 for (
int i = 0; i < num; ++i) {
2536 size_t size = data[i].reduce_size - 1;
2538 size += CACHE_LINE - size % CACHE_LINE;
2539 KMP_ASSERT(data[i].reduce_comb != NULL);
2542 arr[i].
flags = data[i].flags;
2546 __kmp_assign_orig<T>(arr[i], data[i]);
2547 if (!arr[i].flags.lazy_priv) {
2550 arr[i].
reduce_pend = (
char *)(arr[i].reduce_priv) + nth * size;
2551 if (arr[i].reduce_init != NULL) {
2553 for (
size_t j = 0; j < nth; ++j) {
2554 __kmp_call_init<T>(arr[i], j * size);
2561 arr[i].
reduce_priv = __kmp_allocate(nth *
sizeof(
void *));
2564 tg->reduce_data = (
void *)arr;
2565 tg->reduce_num_data = num;
2585 kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_idx);
2586 if (tdg && __kmp_tdg_is_recording(tdg->tdg_status)) {
2587 kmp_tdg_info_t *this_tdg = __kmp_global_tdgs[__kmp_curr_tdg_idx];
2588 this_tdg->rec_taskred_data =
2590 this_tdg->rec_num_taskred = num;
2591 KMP_MEMCPY(this_tdg->rec_taskred_data, data,
2612 kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_idx);
2613 if (tdg && __kmp_tdg_is_recording(tdg->tdg_status)) {
2614 kmp_tdg_info_t *this_tdg = __kmp_global_tdgs[__kmp_curr_tdg_idx];
2615 this_tdg->rec_taskred_data =
2617 this_tdg->rec_num_taskred = num;
2618 KMP_MEMCPY(this_tdg->rec_taskred_data, data,
2626template <
typename T>
2627void __kmp_task_reduction_init_copy(kmp_info_t *thr,
int num, T *data,
2628 kmp_taskgroup_t *tg,
void *reduce_data) {
2630 KA_TRACE(20, (
"__kmp_task_reduction_init_copy: Th %p, init taskgroup %p,"
2632 thr, tg, reduce_data));
2637 for (
int i = 0; i < num; ++i) {
2640 tg->reduce_data = (
void *)arr;
2641 tg->reduce_num_data = num;
2654 __kmp_assert_valid_gtid(gtid);
2655 kmp_info_t *thread = __kmp_threads[gtid];
2656 kmp_int32 nth = thread->th.th_team_nproc;
2660 kmp_taskgroup_t *tg = (kmp_taskgroup_t *)tskgrp;
2662 tg = thread->th.th_current_task->td_taskgroup;
2663 KMP_ASSERT(tg != NULL);
2666 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
2669 if ((thread->th.th_current_task->is_taskgraph) &&
2670 (!__kmp_tdg_is_recording(
2671 __kmp_global_tdgs[__kmp_curr_tdg_idx]->tdg_status))) {
2672 tg = thread->th.th_current_task->td_taskgroup;
2673 KMP_ASSERT(tg != NULL);
2674 KMP_ASSERT(tg->reduce_data != NULL);
2676 num = tg->reduce_num_data;
2680 KMP_ASSERT(data != NULL);
2681 while (tg != NULL) {
2683 num = tg->reduce_num_data;
2684 for (
int i = 0; i < num; ++i) {
2685 if (!arr[i].flags.lazy_priv) {
2686 if (data == arr[i].reduce_shar ||
2687 (data >= arr[i].reduce_priv && data < arr[i].reduce_pend))
2688 return (
char *)(arr[i].
reduce_priv) + tid * arr[i].reduce_size;
2691 void **p_priv = (
void **)(arr[i].reduce_priv);
2692 if (data == arr[i].reduce_shar)
2695 for (
int j = 0; j < nth; ++j)
2696 if (data == p_priv[j])
2700 if (p_priv[tid] == NULL) {
2702 p_priv[tid] = __kmp_allocate(arr[i].reduce_size);
2703 if (arr[i].reduce_init != NULL) {
2704 if (arr[i].reduce_orig != NULL) {
2706 p_priv[tid], arr[i].reduce_orig);
2708 ((void (*)(
void *))arr[i].
reduce_init)(p_priv[tid]);
2715 KMP_ASSERT(tg->parent);
2718 KMP_ASSERT2(0,
"Unknown task reduction item");
2724static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg) {
2725 kmp_int32 nth = th->th.th_team_nproc;
2728 __kmp_enable_hidden_helper);
2731 kmp_int32 num = tg->reduce_num_data;
2732 for (
int i = 0; i < num; ++i) {
2734 void (*f_fini)(
void *) = (
void (*)(
void *))(arr[i].
reduce_fini);
2735 void (*f_comb)(
void *,
void *) =
2737 if (!arr[i].flags.lazy_priv) {
2740 for (
int j = 0; j < nth; ++j) {
2741 void *priv_data = (
char *)pr_data + j * size;
2742 f_comb(sh_data, priv_data);
2747 void **pr_data = (
void **)(arr[i].reduce_priv);
2748 for (
int j = 0; j < nth; ++j) {
2749 if (pr_data[j] != NULL) {
2750 f_comb(sh_data, pr_data[j]);
2753 __kmp_free(pr_data[j]);
2757 __kmp_free(arr[i].reduce_priv);
2759 __kmp_thread_free(th, arr);
2760 tg->reduce_data = NULL;
2761 tg->reduce_num_data = 0;
2767static void __kmp_task_reduction_clean(kmp_info_t *th, kmp_taskgroup_t *tg) {
2768 __kmp_thread_free(th, tg->reduce_data);
2769 tg->reduce_data = NULL;
2770 tg->reduce_num_data = 0;
2773template <
typename T>
2774void *__kmp_task_reduction_modifier_init(
ident_t *loc,
int gtid,
int is_ws,
2776 __kmp_assert_valid_gtid(gtid);
2777 kmp_info_t *thr = __kmp_threads[gtid];
2778 kmp_int32 nth = thr->th.th_team_nproc;
2779 __kmpc_taskgroup(loc, gtid);
2782 (
"__kmpc_reduction_modifier_init: T#%d, tg %p, exiting nth=1\n",
2783 gtid, thr->th.th_current_task->td_taskgroup));
2784 return (
void *)thr->th.th_current_task->td_taskgroup;
2786 kmp_team_t *team = thr->th.th_team;
2788 kmp_taskgroup_t *tg;
2789 reduce_data = KMP_ATOMIC_LD_RLX(&team->t.t_tg_reduce_data[is_ws]);
2790 if (reduce_data == NULL &&
2791 __kmp_atomic_compare_store(&team->t.t_tg_reduce_data[is_ws], reduce_data,
2794 KMP_DEBUG_ASSERT(reduce_data == NULL);
2796 tg = (kmp_taskgroup_t *)__kmp_task_reduction_init<T>(gtid, num, data);
2800 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[0]) == 0);
2801 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[1]) == 0);
2802 KMP_ATOMIC_ST_REL(&team->t.t_tg_reduce_data[is_ws], reduce_data);
2805 (reduce_data = KMP_ATOMIC_LD_ACQ(&team->t.t_tg_reduce_data[is_ws])) ==
2809 KMP_DEBUG_ASSERT(reduce_data > (
void *)1);
2810 tg = thr->th.th_current_task->td_taskgroup;
2811 __kmp_task_reduction_init_copy<T>(thr, num, data, tg, reduce_data);
2833 int num,
void *data) {
2834 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2854 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2867 __kmpc_end_taskgroup(loc, gtid);
2871void __kmpc_taskgroup(
ident_t *loc,
int gtid) {
2872 __kmp_assert_valid_gtid(gtid);
2873 kmp_info_t *thread = __kmp_threads[gtid];
2874 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2875 kmp_taskgroup_t *tg_new =
2876 (kmp_taskgroup_t *)__kmp_thread_malloc(thread,
sizeof(kmp_taskgroup_t));
2877 KA_TRACE(10, (
"__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new));
2878 KMP_ATOMIC_ST_RLX(&tg_new->count, 0);
2879 KMP_ATOMIC_ST_RLX(&tg_new->cancel_request, cancel_noreq);
2880 tg_new->parent = taskdata->td_taskgroup;
2881 tg_new->reduce_data = NULL;
2882 tg_new->reduce_num_data = 0;
2883 tg_new->gomp_data = NULL;
2884 taskdata->td_taskgroup = tg_new;
2886#if OMPT_SUPPORT && OMPT_OPTIONAL
2887 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2888 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2890 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2891 kmp_team_t *team = thread->th.th_team;
2892 ompt_data_t my_task_data = taskdata->ompt_task_info.task_data;
2894 ompt_data_t my_parallel_data = team->t.ompt_team_info.parallel_data;
2896 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2897 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2898 &(my_task_data), codeptr);
2905void __kmpc_end_taskgroup(
ident_t *loc,
int gtid) {
2906 __kmp_assert_valid_gtid(gtid);
2907 kmp_info_t *thread = __kmp_threads[gtid];
2908 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2909 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
2910 int thread_finished = FALSE;
2912#if OMPT_SUPPORT && OMPT_OPTIONAL
2914 ompt_data_t my_task_data;
2915 ompt_data_t my_parallel_data;
2916 void *codeptr =
nullptr;
2917 if (UNLIKELY(ompt_enabled.enabled)) {
2918 team = thread->th.th_team;
2919 my_task_data = taskdata->ompt_task_info.task_data;
2921 my_parallel_data = team->t.ompt_team_info.parallel_data;
2922 codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2924 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2928 KA_TRACE(10, (
"__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc));
2929 KMP_DEBUG_ASSERT(taskgroup != NULL);
2930 KMP_SET_THREAD_STATE_BLOCK(TASKGROUP);
2932 if (__kmp_tasking_mode != tskm_immediate_exec) {
2934 taskdata->td_taskwait_counter += 1;
2935 taskdata->td_taskwait_ident = loc;
2936 taskdata->td_taskwait_thread = gtid + 1;
2940 void *itt_sync_obj = NULL;
2942 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2946#if OMPT_SUPPORT && OMPT_OPTIONAL
2947 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2948 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2949 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2950 &(my_task_data), codeptr);
2954 if (!taskdata->td_flags.team_serial ||
2955 (thread->th.th_task_team != NULL &&
2956 (thread->th.th_task_team->tt.tt_found_proxy_tasks ||
2957 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered))) {
2958 kmp_flag_32<false, false> flag(
2959 RCAST(std::atomic<kmp_uint32> *, &(taskgroup->count)), 0U);
2960 while (KMP_ATOMIC_LD_ACQ(&taskgroup->count) != 0) {
2961 flag.execute_tasks(thread, gtid, FALSE,
2962 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2963 __kmp_task_stealing_constraint);
2966 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2968#if OMPT_SUPPORT && OMPT_OPTIONAL
2969 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2970 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2971 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2972 &(my_task_data), codeptr);
2977 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2978 KMP_FSYNC_ACQUIRED(taskdata);
2981 KMP_DEBUG_ASSERT(taskgroup->count == 0);
2983 if (taskgroup->reduce_data != NULL &&
2984 !taskgroup->gomp_data) {
2987 kmp_team_t *t = thread->th.th_team;
2991 if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[0])) != NULL &&
2994 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[0]);
2995 if (cnt == thread->th.th_team_nproc - 1) {
2998 __kmp_task_reduction_fini(thread, taskgroup);
3001 __kmp_thread_free(thread, reduce_data);
3002 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[0], NULL);
3003 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[0], 0);
3007 __kmp_task_reduction_clean(thread, taskgroup);
3009 }
else if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[1])) !=
3013 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[1]);
3014 if (cnt == thread->th.th_team_nproc - 1) {
3016 __kmp_task_reduction_fini(thread, taskgroup);
3019 __kmp_thread_free(thread, reduce_data);
3020 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[1], NULL);
3021 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[1], 0);
3025 __kmp_task_reduction_clean(thread, taskgroup);
3029 __kmp_task_reduction_fini(thread, taskgroup);
3033 taskdata->td_taskgroup = taskgroup->parent;
3034 __kmp_thread_free(thread, taskgroup);
3036 KA_TRACE(10, (
"__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n",
3039#if OMPT_SUPPORT && OMPT_OPTIONAL
3040 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
3041 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
3042 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
3043 &(my_task_data), codeptr);
3048static kmp_task_t *__kmp_get_priority_task(kmp_int32 gtid,
3049 kmp_task_team_t *task_team,
3050 kmp_int32 is_constrained) {
3051 kmp_task_t *task = NULL;
3052 kmp_taskdata_t *taskdata;
3053 kmp_taskdata_t *current;
3054 kmp_thread_data_t *thread_data;
3055 int ntasks = task_team->tt.tt_num_task_pri;
3058 20, (
"__kmp_get_priority_task(exit #1): T#%d No tasks to get\n", gtid));
3063 if (__kmp_atomic_compare_store(&task_team->tt.tt_num_task_pri, ntasks,
3066 ntasks = task_team->tt.tt_num_task_pri;
3067 }
while (ntasks > 0);
3069 KA_TRACE(20, (
"__kmp_get_priority_task(exit #2): T#%d No tasks to get\n",
3075 kmp_task_pri_t *list = task_team->tt.tt_task_pri_list;
3077 KMP_ASSERT(list != NULL);
3078 thread_data = &list->td;
3079 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3080 deque_ntasks = thread_data->td.td_deque_ntasks;
3081 if (deque_ntasks == 0) {
3082 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3083 KA_TRACE(20, (
"__kmp_get_priority_task: T#%d No tasks to get from %p\n",
3084 __kmp_get_gtid(), thread_data));
3087 }
while (deque_ntasks == 0);
3088 KMP_DEBUG_ASSERT(deque_ntasks);
3089 int target = thread_data->td.td_deque_head;
3090 current = __kmp_threads[gtid]->th.th_current_task;
3091 taskdata = thread_data->td.td_deque[target];
3092 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3094 thread_data->td.td_deque_head =
3095 (target + 1) & TASK_DEQUE_MASK(thread_data->td);
3097 if (!task_team->tt.tt_untied_task_encountered) {
3099 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3100 KA_TRACE(20, (
"__kmp_get_priority_task(exit #3): T#%d could not get task "
3101 "from %p: task_team=%p ntasks=%d head=%u tail=%u\n",
3102 gtid, thread_data, task_team, deque_ntasks, target,
3103 thread_data->td.td_deque_tail));
3104 task_team->tt.tt_num_task_pri++;
3110 for (i = 1; i < deque_ntasks; ++i) {
3111 target = (target + 1) & TASK_DEQUE_MASK(thread_data->td);
3112 taskdata = thread_data->td.td_deque[target];
3113 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3119 if (taskdata == NULL) {
3121 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3123 10, (
"__kmp_get_priority_task(exit #4): T#%d could not get task from "
3124 "%p: task_team=%p ntasks=%d head=%u tail=%u\n",
3125 gtid, thread_data, task_team, deque_ntasks,
3126 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3127 task_team->tt.tt_num_task_pri++;
3131 for (i = i + 1; i < deque_ntasks; ++i) {
3133 target = (target + 1) & TASK_DEQUE_MASK(thread_data->td);
3134 thread_data->td.td_deque[prev] = thread_data->td.td_deque[target];
3138 thread_data->td.td_deque_tail ==
3139 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(thread_data->td)));
3140 thread_data->td.td_deque_tail = target;
3142 thread_data->td.td_deque_ntasks = deque_ntasks - 1;
3143 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3144 task = KMP_TASKDATA_TO_TASK(taskdata);
3149static kmp_task_t *__kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid,
3150 kmp_task_team_t *task_team,
3151 kmp_int32 is_constrained) {
3153 kmp_taskdata_t *taskdata;
3154 kmp_thread_data_t *thread_data;
3157 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3158 KMP_DEBUG_ASSERT(task_team->tt.tt_threads_data !=
3161 thread_data = &task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
3163 KA_TRACE(10, (
"__kmp_remove_my_task(enter): T#%d ntasks=%d head=%u tail=%u\n",
3164 gtid, thread_data->td.td_deque_ntasks,
3165 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3167 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
3169 (
"__kmp_remove_my_task(exit #1): T#%d No tasks to remove: "
3170 "ntasks=%d head=%u tail=%u\n",
3171 gtid, thread_data->td.td_deque_ntasks,
3172 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3176 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3178 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
3179 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3181 (
"__kmp_remove_my_task(exit #2): T#%d No tasks to remove: "
3182 "ntasks=%d head=%u tail=%u\n",
3183 gtid, thread_data->td.td_deque_ntasks,
3184 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3188 tail = (thread_data->td.td_deque_tail - 1) &
3189 TASK_DEQUE_MASK(thread_data->td);
3190 taskdata = thread_data->td.td_deque[tail];
3192 if (!__kmp_task_is_allowed(gtid, is_constrained, taskdata,
3193 thread->th.th_current_task)) {
3195 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3197 (
"__kmp_remove_my_task(exit #3): T#%d TSC blocks tail task: "
3198 "ntasks=%d head=%u tail=%u\n",
3199 gtid, thread_data->td.td_deque_ntasks,
3200 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3204 thread_data->td.td_deque_tail = tail;
3205 TCW_4(thread_data->td.td_deque_ntasks, thread_data->td.td_deque_ntasks - 1);
3207 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3209 KA_TRACE(10, (
"__kmp_remove_my_task(exit #4): T#%d task %p removed: "
3210 "ntasks=%d head=%u tail=%u\n",
3211 gtid, taskdata, thread_data->td.td_deque_ntasks,
3212 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3214 task = KMP_TASKDATA_TO_TASK(taskdata);
3221static kmp_task_t *__kmp_steal_task(kmp_int32 victim_tid, kmp_int32 gtid,
3222 kmp_task_team_t *task_team,
3223 std::atomic<kmp_int32> *unfinished_threads,
3224 int *thread_finished,
3225 kmp_int32 is_constrained) {
3227 kmp_taskdata_t *taskdata;
3228 kmp_taskdata_t *current;
3229 kmp_thread_data_t *victim_td, *threads_data;
3231 kmp_info_t *victim_thr;
3233 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3235 threads_data = task_team->tt.tt_threads_data;
3236 KMP_DEBUG_ASSERT(threads_data != NULL);
3237 KMP_DEBUG_ASSERT(victim_tid >= 0);
3238 KMP_DEBUG_ASSERT(victim_tid < task_team->tt.tt_nproc);
3240 victim_td = &threads_data[victim_tid];
3241 victim_thr = victim_td->td.td_thr;
3244 KA_TRACE(10, (
"__kmp_steal_task(enter): T#%d try to steal from T#%d: "
3245 "task_team=%p ntasks=%d head=%u tail=%u\n",
3246 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
3247 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
3248 victim_td->td.td_deque_tail));
3250 if (TCR_4(victim_td->td.td_deque_ntasks) == 0) {
3251 KA_TRACE(10, (
"__kmp_steal_task(exit #1): T#%d could not steal from T#%d: "
3252 "task_team=%p ntasks=%d head=%u tail=%u\n",
3253 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
3254 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
3255 victim_td->td.td_deque_tail));
3259 __kmp_acquire_bootstrap_lock(&victim_td->td.td_deque_lock);
3261 int ntasks = TCR_4(victim_td->td.td_deque_ntasks);
3264 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3265 KA_TRACE(10, (
"__kmp_steal_task(exit #2): T#%d could not steal from T#%d: "
3266 "task_team=%p ntasks=%d head=%u tail=%u\n",
3267 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3268 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3272 KMP_DEBUG_ASSERT(victim_td->td.td_deque != NULL);
3273 current = __kmp_threads[gtid]->th.th_current_task;
3274 taskdata = victim_td->td.td_deque[victim_td->td.td_deque_head];
3275 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3277 victim_td->td.td_deque_head =
3278 (victim_td->td.td_deque_head + 1) & TASK_DEQUE_MASK(victim_td->td);
3280 if (!task_team->tt.tt_untied_task_encountered) {
3282 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3283 KA_TRACE(10, (
"__kmp_steal_task(exit #3): T#%d could not steal from "
3284 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
3285 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3286 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3291 target = victim_td->td.td_deque_head;
3293 for (i = 1; i < ntasks; ++i) {
3294 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
3295 taskdata = victim_td->td.td_deque[target];
3296 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3302 if (taskdata == NULL) {
3304 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3305 KA_TRACE(10, (
"__kmp_steal_task(exit #4): T#%d could not steal from "
3306 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
3307 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3308 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3312 for (i = i + 1; i < ntasks; ++i) {
3314 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
3315 victim_td->td.td_deque[prev] = victim_td->td.td_deque[target];
3319 victim_td->td.td_deque_tail ==
3320 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(victim_td->td)));
3321 victim_td->td.td_deque_tail = target;
3323 if (*thread_finished) {
3330 KMP_ATOMIC_INC(unfinished_threads);
3333 (
"__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n",
3334 gtid, count + 1, task_team));
3335 *thread_finished = FALSE;
3337 TCW_4(victim_td->td.td_deque_ntasks, ntasks - 1);
3339 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3343 (
"__kmp_steal_task(exit #5): T#%d stole task %p from T#%d: "
3344 "task_team=%p ntasks=%d head=%u tail=%u\n",
3345 gtid, taskdata, __kmp_gtid_from_thread(victim_thr), task_team,
3346 ntasks, victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3348 task = KMP_TASKDATA_TO_TASK(taskdata);
3362static inline int __kmp_execute_tasks_template(
3363 kmp_info_t *thread, kmp_int32 gtid, C *flag,
int final_spin,
3364 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3365 kmp_int32 is_constrained) {
3366 kmp_task_team_t *task_team = thread->th.th_task_team;
3367 kmp_thread_data_t *threads_data;
3369 kmp_info_t *other_thread;
3370 kmp_taskdata_t *current_task = thread->th.th_current_task;
3371 std::atomic<kmp_int32> *unfinished_threads;
3372 kmp_int32 nthreads, victim_tid = -2, use_own_tasks = 1, new_victim = 0,
3373 tid = thread->th.th_info.ds.ds_tid;
3375 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3376 KMP_DEBUG_ASSERT(thread == __kmp_threads[gtid]);
3378 if (task_team == NULL || current_task == NULL)
3381 KA_TRACE(15, (
"__kmp_execute_tasks_template(enter): T#%d final_spin=%d "
3382 "*thread_finished=%d\n",
3383 gtid, final_spin, *thread_finished));
3385 thread->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
3386 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
3388 KMP_DEBUG_ASSERT(threads_data != NULL);
3390 nthreads = task_team->tt.tt_nproc;
3391 unfinished_threads = &(task_team->tt.tt_unfinished_threads);
3392 KMP_DEBUG_ASSERT(*unfinished_threads >= 0);
3398 if (task_team->tt.tt_num_task_pri) {
3399 task = __kmp_get_priority_task(gtid, task_team, is_constrained);
3401 if (task == NULL && use_own_tasks) {
3402 task = __kmp_remove_my_task(thread, gtid, task_team, is_constrained);
3404 if ((task == NULL) && (nthreads > 1)) {
3408 if (victim_tid == -2) {
3409 victim_tid = threads_data[tid].td.td_deque_last_stolen;
3412 other_thread = threads_data[victim_tid].td.td_thr;
3414 if (victim_tid != -1) {
3416 }
else if (!new_victim) {
3422 victim_tid = __kmp_get_random(thread) % (nthreads - 1);
3423 if (victim_tid >= tid) {
3427 other_thread = threads_data[victim_tid].td.td_thr;
3437 if ((__kmp_tasking_mode == tskm_task_teams) &&
3438 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) &&
3439 (TCR_PTR(CCAST(
void *, other_thread->th.th_sleep_loc)) !=
3442 __kmp_null_resume_wrapper(other_thread);
3456 __kmp_steal_task(victim_tid, gtid, task_team, unfinished_threads,
3457 thread_finished, is_constrained);
3460 if (threads_data[tid].td.td_deque_last_stolen != victim_tid) {
3461 threads_data[tid].td.td_deque_last_stolen = victim_tid;
3468 KMP_CHECK_UPDATE(threads_data[tid].td.td_deque_last_stolen, -1);
3477#if USE_ITT_BUILD && USE_ITT_NOTIFY
3478 if (__itt_sync_create_ptr || KMP_ITT_DEBUG) {
3479 if (itt_sync_obj == NULL) {
3481 itt_sync_obj = __kmp_itt_barrier_object(gtid, bs_forkjoin_barrier);
3483 __kmp_itt_task_starting(itt_sync_obj);
3486 __kmp_invoke_task(gtid, task, current_task);
3488 if (itt_sync_obj != NULL)
3489 __kmp_itt_task_finished(itt_sync_obj);
3496 if (flag == NULL || (!final_spin && flag->done_check())) {
3499 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3503 if (thread->th.th_task_team == NULL) {
3506 KMP_YIELD(__kmp_library == library_throughput);
3509 if (!use_own_tasks && TCR_4(threads_data[tid].td.td_deque_ntasks) != 0) {
3510 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d stolen task spawned "
3511 "other tasks, restart\n",
3522 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks) == 0) {
3526 if (!*thread_finished) {
3528 kmp_int32 count = -1 +
3530 KMP_ATOMIC_DEC(unfinished_threads);
3531 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d dec "
3532 "unfinished_threads to %d task_team=%p\n",
3533 gtid, count, task_team));
3534 *thread_finished = TRUE;
3542 if (flag != NULL && flag->done_check()) {
3545 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3553 if (thread->th.th_task_team == NULL) {
3555 (
"__kmp_execute_tasks_template: T#%d no more tasks\n", gtid));
3564 if (flag == NULL || (!final_spin && flag->done_check())) {
3566 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3573 if (nthreads == 1 &&
3574 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks))
3578 (
"__kmp_execute_tasks_template: T#%d can't find work\n", gtid));
3584template <
bool C,
bool S>
3585int __kmp_execute_tasks_32(
3586 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32<C, S> *flag,
int final_spin,
3587 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3588 kmp_int32 is_constrained) {
3589 return __kmp_execute_tasks_template(
3590 thread, gtid, flag, final_spin,
3591 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3594template <
bool C,
bool S>
3595int __kmp_execute_tasks_64(
3596 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64<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_atomic_execute_tasks_64(
3606 kmp_info_t *thread, kmp_int32 gtid, kmp_atomic_flag_64<C, S> *flag,
3607 int final_spin,
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);
3614int __kmp_execute_tasks_oncore(
3615 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag,
int final_spin,
3616 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3617 kmp_int32 is_constrained) {
3618 return __kmp_execute_tasks_template(
3619 thread, gtid, flag, final_spin,
3620 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3624__kmp_execute_tasks_32<false, false>(kmp_info_t *, kmp_int32,
3625 kmp_flag_32<false, false> *,
int,
3626 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3628template int __kmp_execute_tasks_64<false, true>(kmp_info_t *, kmp_int32,
3629 kmp_flag_64<false, true> *,
3631 int *USE_ITT_BUILD_ARG(
void *),
3634template int __kmp_execute_tasks_64<true, false>(kmp_info_t *, kmp_int32,
3635 kmp_flag_64<true, false> *,
3637 int *USE_ITT_BUILD_ARG(
void *),
3640template int __kmp_atomic_execute_tasks_64<false, true>(
3641 kmp_info_t *, kmp_int32, kmp_atomic_flag_64<false, true> *,
int,
3642 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3644template int __kmp_atomic_execute_tasks_64<true, false>(
3645 kmp_info_t *, kmp_int32, kmp_atomic_flag_64<true, false> *,
int,
3646 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3651static void __kmp_enable_tasking(kmp_task_team_t *task_team,
3652 kmp_info_t *this_thr) {
3653 kmp_thread_data_t *threads_data;
3654 int nthreads, i, is_init_thread;
3656 KA_TRACE(10, (
"__kmp_enable_tasking(enter): T#%d\n",
3657 __kmp_gtid_from_thread(this_thr)));
3659 KMP_DEBUG_ASSERT(task_team != NULL);
3660 KMP_DEBUG_ASSERT(this_thr->th.th_team != NULL);
3662 nthreads = task_team->tt.tt_nproc;
3663 KMP_DEBUG_ASSERT(nthreads > 0);
3664 KMP_DEBUG_ASSERT(nthreads == this_thr->th.th_team->t.t_nproc);
3667 is_init_thread = __kmp_realloc_task_threads_data(this_thr, task_team);
3669 if (!is_init_thread) {
3673 (
"__kmp_enable_tasking(exit): T#%d: threads array already set up.\n",
3674 __kmp_gtid_from_thread(this_thr)));
3677 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
3678 KMP_DEBUG_ASSERT(threads_data != NULL);
3680 if (__kmp_tasking_mode == tskm_task_teams &&
3681 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME)) {
3685 for (i = 0; i < nthreads; i++) {
3687 kmp_info_t *thread = threads_data[i].td.td_thr;
3689 if (i == this_thr->th.th_info.ds.ds_tid) {
3698 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3700 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d waking up thread T#%d\n",
3701 __kmp_gtid_from_thread(this_thr),
3702 __kmp_gtid_from_thread(thread)));
3703 __kmp_null_resume_wrapper(thread);
3705 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d don't wake up thread T#%d\n",
3706 __kmp_gtid_from_thread(this_thr),
3707 __kmp_gtid_from_thread(thread)));
3712 KA_TRACE(10, (
"__kmp_enable_tasking(exit): T#%d\n",
3713 __kmp_gtid_from_thread(this_thr)));
3746static kmp_task_team_t *__kmp_free_task_teams =
3749kmp_bootstrap_lock_t __kmp_task_team_lock =
3750 KMP_BOOTSTRAP_LOCK_INITIALIZER(__kmp_task_team_lock);
3757static void __kmp_alloc_task_deque(kmp_info_t *thread,
3758 kmp_thread_data_t *thread_data) {
3759 __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
3760 KMP_DEBUG_ASSERT(thread_data->td.td_deque == NULL);
3763 thread_data->td.td_deque_last_stolen = -1;
3765 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == 0);
3766 KMP_DEBUG_ASSERT(thread_data->td.td_deque_head == 0);
3767 KMP_DEBUG_ASSERT(thread_data->td.td_deque_tail == 0);
3771 (
"__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n",
3772 __kmp_gtid_from_thread(thread), INITIAL_TASK_DEQUE_SIZE, thread_data));
3776 thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
3777 INITIAL_TASK_DEQUE_SIZE *
sizeof(kmp_taskdata_t *));
3778 thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
3784static void __kmp_free_task_deque(kmp_thread_data_t *thread_data) {
3785 if (thread_data->td.td_deque != NULL) {
3786 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3787 TCW_4(thread_data->td.td_deque_ntasks, 0);
3788 __kmp_free(thread_data->td.td_deque);
3789 thread_data->td.td_deque = NULL;
3790 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3793#ifdef BUILD_TIED_TASK_STACK
3795 if (thread_data->td.td_susp_tied_tasks.ts_entries != TASK_STACK_EMPTY) {
3796 __kmp_free_task_stack(__kmp_thread_from_gtid(gtid), thread_data);
3808static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
3809 kmp_task_team_t *task_team) {
3810 kmp_thread_data_t **threads_data_p;
3811 kmp_int32 nthreads, maxthreads;
3812 int is_init_thread = FALSE;
3814 if (TCR_4(task_team->tt.tt_found_tasks)) {
3819 threads_data_p = &task_team->tt.tt_threads_data;
3820 nthreads = task_team->tt.tt_nproc;
3821 maxthreads = task_team->tt.tt_max_threads;
3826 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3828 if (!TCR_4(task_team->tt.tt_found_tasks)) {
3830 kmp_team_t *team = thread->th.th_team;
3833 is_init_thread = TRUE;
3834 if (maxthreads < nthreads) {
3836 if (*threads_data_p != NULL) {
3837 kmp_thread_data_t *old_data = *threads_data_p;
3838 kmp_thread_data_t *new_data = NULL;
3842 (
"__kmp_realloc_task_threads_data: T#%d reallocating "
3843 "threads data for task_team %p, new_size = %d, old_size = %d\n",
3844 __kmp_gtid_from_thread(thread), task_team, nthreads, maxthreads));
3849 new_data = (kmp_thread_data_t *)__kmp_allocate(
3850 nthreads *
sizeof(kmp_thread_data_t));
3852 KMP_MEMCPY_S((
void *)new_data, nthreads *
sizeof(kmp_thread_data_t),
3853 (
void *)old_data, maxthreads *
sizeof(kmp_thread_data_t));
3855#ifdef BUILD_TIED_TASK_STACK
3857 for (i = maxthreads; i < nthreads; i++) {
3858 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3859 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3863 (*threads_data_p) = new_data;
3864 __kmp_free(old_data);
3866 KE_TRACE(10, (
"__kmp_realloc_task_threads_data: T#%d allocating "
3867 "threads data for task_team %p, size = %d\n",
3868 __kmp_gtid_from_thread(thread), task_team, nthreads));
3872 *threads_data_p = (kmp_thread_data_t *)__kmp_allocate(
3873 nthreads *
sizeof(kmp_thread_data_t));
3874#ifdef BUILD_TIED_TASK_STACK
3876 for (i = 0; i < nthreads; i++) {
3877 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3878 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3882 task_team->tt.tt_max_threads = nthreads;
3885 KMP_DEBUG_ASSERT(*threads_data_p != NULL);
3889 for (i = 0; i < nthreads; i++) {
3890 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3891 thread_data->td.td_thr = team->t.t_threads[i];
3893 if (thread_data->td.td_deque_last_stolen >= nthreads) {
3897 thread_data->td.td_deque_last_stolen = -1;
3902 TCW_SYNC_4(task_team->tt.tt_found_tasks, TRUE);
3905 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3906 return is_init_thread;
3912static void __kmp_free_task_threads_data(kmp_task_team_t *task_team) {
3913 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3914 if (task_team->tt.tt_threads_data != NULL) {
3916 for (i = 0; i < task_team->tt.tt_max_threads; i++) {
3917 __kmp_free_task_deque(&task_team->tt.tt_threads_data[i]);
3919 __kmp_free(task_team->tt.tt_threads_data);
3920 task_team->tt.tt_threads_data = NULL;
3922 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3928static void __kmp_free_task_pri_list(kmp_task_team_t *task_team) {
3929 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
3930 if (task_team->tt.tt_task_pri_list != NULL) {
3931 kmp_task_pri_t *list = task_team->tt.tt_task_pri_list;
3932 while (list != NULL) {
3933 kmp_task_pri_t *next = list->next;
3934 __kmp_free_task_deque(&list->td);
3938 task_team->tt.tt_task_pri_list = NULL;
3940 __kmp_release_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
3943static inline void __kmp_task_team_init(kmp_task_team_t *task_team,
3945 int team_nth = team->t.t_nproc;
3947 if (!task_team->tt.tt_active || team_nth != task_team->tt.tt_nproc) {
3948 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3949 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3950 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
3951 TCW_4(task_team->tt.tt_nproc, team_nth);
3952 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads, team_nth);
3953 TCW_4(task_team->tt.tt_active, TRUE);
3961static kmp_task_team_t *__kmp_allocate_task_team(kmp_info_t *thread,
3963 kmp_task_team_t *task_team = NULL;
3965 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d entering; team = %p\n",
3966 (thread ? __kmp_gtid_from_thread(thread) : -1), team));
3968 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3970 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3971 if (__kmp_free_task_teams != NULL) {
3972 task_team = __kmp_free_task_teams;
3973 TCW_PTR(__kmp_free_task_teams, task_team->tt.tt_next);
3974 task_team->tt.tt_next = NULL;
3976 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3979 if (task_team == NULL) {
3980 KE_TRACE(10, (
"__kmp_allocate_task_team: T#%d allocating "
3981 "task team for team %p\n",
3982 __kmp_gtid_from_thread(thread), team));
3985 task_team = (kmp_task_team_t *)__kmp_allocate(
sizeof(kmp_task_team_t));
3986 __kmp_init_bootstrap_lock(&task_team->tt.tt_threads_lock);
3987 __kmp_init_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
3988#if USE_ITT_BUILD && USE_ITT_NOTIFY && KMP_DEBUG
3991 __itt_suppress_mark_range(
3992 __itt_suppress_range, __itt_suppress_threading_errors,
3993 &task_team->tt.tt_found_tasks,
sizeof(task_team->tt.tt_found_tasks));
3994 __itt_suppress_mark_range(__itt_suppress_range,
3995 __itt_suppress_threading_errors,
3996 CCAST(kmp_uint32 *, &task_team->tt.tt_active),
3997 sizeof(task_team->tt.tt_active));
4005 __kmp_task_team_init(task_team, team);
4007 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d exiting; task_team = %p "
4008 "unfinished_threads init'd to %d\n",
4009 (thread ? __kmp_gtid_from_thread(thread) : -1), task_team,
4010 KMP_ATOMIC_LD_RLX(&task_team->tt.tt_unfinished_threads)));
4017void __kmp_free_task_team(kmp_info_t *thread, kmp_task_team_t *task_team) {
4018 KA_TRACE(20, (
"__kmp_free_task_team: T#%d task_team = %p\n",
4019 thread ? __kmp_gtid_from_thread(thread) : -1, task_team));
4022 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
4024 KMP_DEBUG_ASSERT(task_team->tt.tt_next == NULL);
4025 task_team->tt.tt_next = __kmp_free_task_teams;
4026 TCW_PTR(__kmp_free_task_teams, task_team);
4028 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
4036void __kmp_reap_task_teams(
void) {
4037 kmp_task_team_t *task_team;
4039 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
4041 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
4042 while ((task_team = __kmp_free_task_teams) != NULL) {
4043 __kmp_free_task_teams = task_team->tt.tt_next;
4044 task_team->tt.tt_next = NULL;
4047 if (task_team->tt.tt_threads_data != NULL) {
4048 __kmp_free_task_threads_data(task_team);
4050 if (task_team->tt.tt_task_pri_list != NULL) {
4051 __kmp_free_task_pri_list(task_team);
4053 __kmp_free(task_team);
4055 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
4063void __kmp_push_task_team_node(kmp_info_t *thread, kmp_team_t *team) {
4064 KMP_DEBUG_ASSERT(team->t.t_nproc == 1);
4065 kmp_task_team_list_t *current =
4066 (kmp_task_team_list_t *)(&team->t.t_task_team[0]);
4067 kmp_task_team_list_t *node =
4068 (kmp_task_team_list_t *)__kmp_allocate(
sizeof(kmp_task_team_list_t));
4069 node->task_team = current->task_team;
4070 node->next = current->next;
4071 thread->th.th_task_team = current->task_team = NULL;
4072 current->next = node;
4076void __kmp_pop_task_team_node(kmp_info_t *thread, kmp_team_t *team) {
4077 KMP_DEBUG_ASSERT(team->t.t_nproc == 1);
4078 kmp_task_team_list_t *current =
4079 (kmp_task_team_list_t *)(&team->t.t_task_team[0]);
4080 if (current->task_team) {
4081 __kmp_free_task_team(thread, current->task_team);
4083 kmp_task_team_list_t *next = current->next;
4085 current->task_team = next->task_team;
4086 current->next = next->next;
4087 KMP_DEBUG_ASSERT(next != current);
4089 thread->th.th_task_team = current->task_team;
4096void __kmp_wait_to_unref_task_teams(
void) {
4102 KMP_INIT_YIELD(spins);
4103 KMP_INIT_BACKOFF(time);
4111 for (thread = CCAST(kmp_info_t *, __kmp_thread_pool); thread != NULL;
4112 thread = thread->th.th_next_pool) {
4116 if (TCR_PTR(thread->th.th_task_team) == NULL) {
4117 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n",
4118 __kmp_gtid_from_thread(thread)));
4123 if (!__kmp_is_thread_alive(thread, &exit_val)) {
4124 thread->th.th_task_team = NULL;
4131 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: Waiting for T#%d to "
4132 "unreference task_team\n",
4133 __kmp_gtid_from_thread(thread)));
4135 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
4138 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
4142 (
"__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n",
4143 __kmp_gtid_from_thread(thread), __kmp_gtid_from_thread(thread)));
4144 __kmp_null_resume_wrapper(thread);
4153 KMP_YIELD_OVERSUB_ELSE_SPIN(spins, time);
4159void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team) {
4160 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
4165 if (team == this_thr->th.th_serial_team ||
4166 team == this_thr->th.th_root->r.r_root_team) {
4167 KMP_DEBUG_ASSERT(team->t.t_nproc == 1);
4168 if (team->t.t_task_team[0] == NULL) {
4169 team->t.t_task_team[0] = __kmp_allocate_task_team(this_thr, team);
4171 20, (
"__kmp_task_team_setup: Primary T#%d created new task_team %p"
4172 " for serial/root team %p\n",
4173 __kmp_gtid_from_thread(this_thr), team->t.t_task_team[0], team));
4176 __kmp_task_team_init(team->t.t_task_team[0], team);
4184 if (team->t.t_task_team[this_thr->th.th_task_state] == NULL) {
4185 team->t.t_task_team[this_thr->th.th_task_state] =
4186 __kmp_allocate_task_team(this_thr, team);
4187 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d created new task_team %p"
4188 " for team %d at parity=%d\n",
4189 __kmp_gtid_from_thread(this_thr),
4190 team->t.t_task_team[this_thr->th.th_task_state], team->t.t_id,
4191 this_thr->th.th_task_state));
4200 int other_team = 1 - this_thr->th.th_task_state;
4201 KMP_DEBUG_ASSERT(other_team >= 0 && other_team < 2);
4202 if (team->t.t_task_team[other_team] == NULL) {
4203 team->t.t_task_team[other_team] = __kmp_allocate_task_team(this_thr, team);
4204 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d created second new "
4205 "task_team %p for team %d at parity=%d\n",
4206 __kmp_gtid_from_thread(this_thr),
4207 team->t.t_task_team[other_team], team->t.t_id, other_team));
4210 kmp_task_team_t *task_team = team->t.t_task_team[other_team];
4211 __kmp_task_team_init(task_team, team);
4214 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d reset next task_team "
4215 "%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));
4224 if (this_thr == __kmp_hidden_helper_main_thread) {
4225 for (
int i = 0; i < 2; ++i) {
4226 kmp_task_team_t *task_team = team->t.t_task_team[i];
4227 if (KMP_TASKING_ENABLED(task_team)) {
4230 __kmp_enable_tasking(task_team, this_thr);
4231 for (
int j = 0; j < task_team->tt.tt_nproc; ++j) {
4232 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[j];
4233 if (thread_data->td.td_deque == NULL) {
4234 __kmp_alloc_task_deque(__kmp_hidden_helper_threads[j], thread_data);
4244void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team) {
4245 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
4246 KMP_DEBUG_ASSERT(team != this_thr->th.th_serial_team);
4247 KMP_DEBUG_ASSERT(team != this_thr->th.th_root->r.r_root_team);
4251 this_thr->th.th_task_state = (kmp_uint8)(1 - this_thr->th.th_task_state);
4255 TCW_PTR(this_thr->th.th_task_team,
4256 team->t.t_task_team[this_thr->th.th_task_state]);
4258 (
"__kmp_task_team_sync: Thread T#%d task team switched to task_team "
4259 "%p from Team #%d (parity=%d)\n",
4260 __kmp_gtid_from_thread(this_thr), this_thr->th.th_task_team,
4261 team->t.t_id, this_thr->th.th_task_state));
4270void __kmp_task_team_wait(
4271 kmp_info_t *this_thr,
4272 kmp_team_t *team USE_ITT_BUILD_ARG(
void *itt_sync_obj),
int wait) {
4273 kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state];
4275 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
4276 KMP_DEBUG_ASSERT(task_team == this_thr->th.th_task_team);
4278 if ((task_team != NULL) && KMP_TASKING_ENABLED(task_team)) {
4280 KA_TRACE(20, (
"__kmp_task_team_wait: Primary T#%d waiting for all tasks "
4281 "(for unfinished_threads to reach 0) on task_team = %p\n",
4282 __kmp_gtid_from_thread(this_thr), task_team));
4286 kmp_flag_32<false, false> flag(
4287 RCAST(std::atomic<kmp_uint32> *,
4288 &task_team->tt.tt_unfinished_threads),
4290 flag.wait(this_thr, TRUE USE_ITT_BUILD_ARG(itt_sync_obj));
4296 (
"__kmp_task_team_wait: Primary T#%d deactivating task_team %p: "
4297 "setting active to false, setting local and team's pointer to NULL\n",
4298 __kmp_gtid_from_thread(this_thr), task_team));
4299 TCW_SYNC_4(task_team->tt.tt_found_proxy_tasks, FALSE);
4300 TCW_SYNC_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
4301 KMP_CHECK_UPDATE(task_team->tt.tt_untied_task_encountered, 0);
4302 TCW_SYNC_4(task_team->tt.tt_active, FALSE);
4305 TCW_PTR(this_thr->th.th_task_team, NULL);
4314void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread,
int gtid) {
4315 std::atomic<kmp_uint32> *spin = RCAST(
4316 std::atomic<kmp_uint32> *,
4317 &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads);
4319 KMP_DEBUG_ASSERT(__kmp_tasking_mode == tskm_extra_barrier);
4322 KMP_FSYNC_SPIN_INIT(spin, NULL);
4324 kmp_flag_32<false, false> spin_flag(spin, 0U);
4325 while (!spin_flag.execute_tasks(thread, gtid, TRUE,
4326 &flag USE_ITT_BUILD_ARG(NULL), 0)) {
4329 KMP_FSYNC_SPIN_PREPARE(RCAST(
void *, spin));
4332 if (TCR_4(__kmp_global.g.g_done)) {
4333 if (__kmp_global.g.g_abort)
4334 __kmp_abort_thread();
4340 KMP_FSYNC_SPIN_ACQUIRED(RCAST(
void *, spin));
4349static bool __kmp_give_task(kmp_info_t *thread, kmp_int32 tid, kmp_task_t *task,
4351 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4352 kmp_task_team_t *task_team = taskdata->td_task_team;
4354 KA_TRACE(20, (
"__kmp_give_task: trying to give task %p to thread %d.\n",
4358 KMP_DEBUG_ASSERT(task_team != NULL);
4360 bool result =
false;
4361 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
4363 if (thread_data->td.td_deque == NULL) {
4367 (
"__kmp_give_task: thread %d has no queue while giving task %p.\n",
4372 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4373 TASK_DEQUE_SIZE(thread_data->td)) {
4376 (
"__kmp_give_task: queue is full while giving task %p to thread %d.\n",
4381 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
4384 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
4385 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4386 TASK_DEQUE_SIZE(thread_data->td)) {
4388 __kmp_realloc_task_deque(thread, thread_data);
4393 __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)) {
4397 KA_TRACE(30, (
"__kmp_give_task: queue is full while giving task %p to "
4403 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
4404 goto release_and_exit;
4406 __kmp_realloc_task_deque(thread, thread_data);
4412 thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
4414 thread_data->td.td_deque_tail =
4415 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
4416 TCW_4(thread_data->td.td_deque_ntasks,
4417 TCR_4(thread_data->td.td_deque_ntasks) + 1);
4420 KA_TRACE(30, (
"__kmp_give_task: successfully gave task %p to thread %d.\n",
4424 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
4429#define PROXY_TASK_FLAG 0x40000000
4446static void __kmp_first_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
4447 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
4448 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4449 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
4450 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
4452 taskdata->td_flags.complete = 1;
4454 taskdata->td_flags.onced = 1;
4457 if (taskdata->td_taskgroup)
4458 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
4462 KMP_ATOMIC_OR(&taskdata->td_incomplete_child_tasks, PROXY_TASK_FLAG);
4465static void __kmp_second_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
4467 kmp_int32 children = 0;
4471 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks);
4472 KMP_DEBUG_ASSERT(children >= 0);
4475 KMP_ATOMIC_AND(&taskdata->td_incomplete_child_tasks, ~PROXY_TASK_FLAG);
4478static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask) {
4479 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4480 kmp_info_t *thread = __kmp_threads[gtid];
4482 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4483 KMP_DEBUG_ASSERT(taskdata->td_flags.complete ==
4488 while ((KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) &
4489 PROXY_TASK_FLAG) > 0)
4492 __kmp_release_deps(gtid, taskdata);
4493 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
4505 KMP_DEBUG_ASSERT(ptask != NULL);
4506 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4508 10, (
"__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n",
4510 __kmp_assert_valid_gtid(gtid);
4511 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4513 __kmp_first_top_half_finish_proxy(taskdata);
4514 __kmp_second_top_half_finish_proxy(taskdata);
4515 __kmp_bottom_half_finish_proxy(gtid, ptask);
4518 (
"__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n",
4522void __kmpc_give_task(kmp_task_t *ptask, kmp_int32 start = 0) {
4523 KMP_DEBUG_ASSERT(ptask != NULL);
4524 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4528 kmp_team_t *team = taskdata->td_team;
4529 kmp_int32 nthreads = team->t.t_nproc;
4534 kmp_int32 start_k = start % nthreads;
4536 kmp_int32 k = start_k;
4540 thread = team->t.t_threads[k];
4541 k = (k + 1) % nthreads;
4547 }
while (!__kmp_give_task(thread, k, ptask, pass));
4549 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME && __kmp_wpolicy_passive) {
4551 for (
int i = 0; i < nthreads; ++i) {
4552 thread = team->t.t_threads[i];
4553 if (thread->th.th_sleep_loc != NULL) {
4554 __kmp_null_resume_wrapper(thread);
4569 KMP_DEBUG_ASSERT(ptask != NULL);
4570 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4574 (
"__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n",
4577 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4579 __kmp_first_top_half_finish_proxy(taskdata);
4581 __kmpc_give_task(ptask);
4583 __kmp_second_top_half_finish_proxy(taskdata);
4587 (
"__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n",
4591kmp_event_t *__kmpc_task_allow_completion_event(
ident_t *loc_ref,
int gtid,
4593 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(task);
4594 if (td->td_allow_completion_event.type == KMP_EVENT_UNINITIALIZED) {
4595 td->td_allow_completion_event.type = KMP_EVENT_ALLOW_COMPLETION;
4596 td->td_allow_completion_event.ed.task = task;
4597 __kmp_init_tas_lock(&td->td_allow_completion_event.lock);
4599 return &td->td_allow_completion_event;
4602void __kmp_fulfill_event(kmp_event_t *event) {
4603 if (event->type == KMP_EVENT_ALLOW_COMPLETION) {
4604 kmp_task_t *ptask = event->ed.task;
4605 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4606 bool detached =
false;
4607 int gtid = __kmp_get_gtid();
4612 __kmp_acquire_tas_lock(&event->lock, gtid);
4613 if (taskdata->td_flags.proxy == TASK_PROXY) {
4619 if (UNLIKELY(ompt_enabled.enabled))
4620 __ompt_task_finish(ptask, NULL, ompt_task_early_fulfill);
4623 event->type = KMP_EVENT_UNINITIALIZED;
4624 __kmp_release_tas_lock(&event->lock, gtid);
4630 if (UNLIKELY(ompt_enabled.enabled))
4631 __ompt_task_finish(ptask, NULL, ompt_task_late_fulfill);
4635 kmp_team_t *team = taskdata->td_team;
4636 kmp_info_t *thread = __kmp_get_thread();
4637 if (thread->th.th_team == team) {
4657kmp_task_t *__kmp_task_dup_alloc(kmp_info_t *thread, kmp_task_t *task_src
4659 ,
int taskloop_recur
4663 kmp_taskdata_t *taskdata;
4664 kmp_taskdata_t *taskdata_src = KMP_TASK_TO_TASKDATA(task_src);
4665 kmp_taskdata_t *parent_task = taskdata_src->td_parent;
4666 size_t shareds_offset;
4669 KA_TRACE(10, (
"__kmp_task_dup_alloc(enter): Th %p, source task %p\n", thread,
4671 KMP_DEBUG_ASSERT(taskdata_src->td_flags.proxy ==
4673 KMP_DEBUG_ASSERT(taskdata_src->td_flags.tasktype == TASK_EXPLICIT);
4674 task_size = taskdata_src->td_size_alloc;
4677 KA_TRACE(30, (
"__kmp_task_dup_alloc: Th %p, malloc size %ld\n", thread,
4680 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, task_size);
4682 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, task_size);
4684 KMP_MEMCPY(taskdata, taskdata_src, task_size);
4686 task = KMP_TASKDATA_TO_TASK(taskdata);
4690 if (!taskdata->is_taskgraph || taskloop_recur)
4691 taskdata->td_task_id = KMP_GEN_TASK_ID();
4692 else if (taskdata->is_taskgraph &&
4693 __kmp_tdg_is_recording(taskdata_src->tdg->tdg_status))
4694 taskdata->td_task_id = KMP_ATOMIC_INC(&__kmp_tdg_task_id);
4696 taskdata->td_task_id = KMP_GEN_TASK_ID();
4698 if (task->shareds != NULL) {
4699 shareds_offset = (
char *)task_src->shareds - (
char *)taskdata_src;
4700 task->shareds = &((
char *)taskdata)[shareds_offset];
4701 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
4704 taskdata->td_alloc_thread = thread;
4705 taskdata->td_parent = parent_task;
4707 taskdata->td_taskgroup = parent_task->td_taskgroup;
4710 if (taskdata->td_flags.tiedness == TASK_TIED)
4711 taskdata->td_last_tied = taskdata;
4715 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
4716 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
4717 if (parent_task->td_taskgroup)
4718 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
4721 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT)
4722 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
4726 (
"__kmp_task_dup_alloc(exit): Th %p, created task %p, parent=%p\n",
4727 thread, taskdata, taskdata->td_parent));
4729 if (UNLIKELY(ompt_enabled.enabled))
4730 __ompt_task_init(taskdata, thread->th.th_info.ds.ds_gtid);
4739typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32);
4741KMP_BUILD_ASSERT(
sizeof(
long) == 4 ||
sizeof(
long) == 8);
4746class kmp_taskloop_bounds_t {
4748 const kmp_taskdata_t *taskdata;
4749 size_t lower_offset;
4750 size_t upper_offset;
4753 kmp_taskloop_bounds_t(kmp_task_t *_task, kmp_uint64 *lb, kmp_uint64 *ub)
4754 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(task)),
4755 lower_offset((char *)lb - (char *)task),
4756 upper_offset((char *)ub - (char *)task) {
4757 KMP_DEBUG_ASSERT((
char *)lb > (
char *)_task);
4758 KMP_DEBUG_ASSERT((
char *)ub > (
char *)_task);
4760 kmp_taskloop_bounds_t(kmp_task_t *_task,
const kmp_taskloop_bounds_t &bounds)
4761 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(_task)),
4762 lower_offset(bounds.lower_offset), upper_offset(bounds.upper_offset) {}
4763 size_t get_lower_offset()
const {
return lower_offset; }
4764 size_t get_upper_offset()
const {
return upper_offset; }
4765 kmp_uint64 get_lb()
const {
4767#if defined(KMP_GOMP_COMPAT)
4769 if (!taskdata->td_flags.native) {
4770 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4773 if (taskdata->td_size_loop_bounds == 4) {
4774 kmp_int32 *lb = RCAST(kmp_int32 *, task->shareds);
4775 retval = (kmp_int64)*lb;
4777 kmp_int64 *lb = RCAST(kmp_int64 *, task->shareds);
4778 retval = (kmp_int64)*lb;
4783 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4787 kmp_uint64 get_ub()
const {
4789#if defined(KMP_GOMP_COMPAT)
4791 if (!taskdata->td_flags.native) {
4792 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4795 if (taskdata->td_size_loop_bounds == 4) {
4796 kmp_int32 *ub = RCAST(kmp_int32 *, task->shareds) + 1;
4797 retval = (kmp_int64)*ub;
4799 kmp_int64 *ub = RCAST(kmp_int64 *, task->shareds) + 1;
4800 retval = (kmp_int64)*ub;
4804 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4808 void set_lb(kmp_uint64 lb) {
4809#if defined(KMP_GOMP_COMPAT)
4811 if (!taskdata->td_flags.native) {
4812 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4815 if (taskdata->td_size_loop_bounds == 4) {
4816 kmp_uint32 *lower = RCAST(kmp_uint32 *, task->shareds);
4817 *lower = (kmp_uint32)lb;
4819 kmp_uint64 *lower = RCAST(kmp_uint64 *, task->shareds);
4820 *lower = (kmp_uint64)lb;
4824 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4827 void set_ub(kmp_uint64 ub) {
4828#if defined(KMP_GOMP_COMPAT)
4830 if (!taskdata->td_flags.native) {
4831 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4834 if (taskdata->td_size_loop_bounds == 4) {
4835 kmp_uint32 *upper = RCAST(kmp_uint32 *, task->shareds) + 1;
4836 *upper = (kmp_uint32)ub;
4838 kmp_uint64 *upper = RCAST(kmp_uint64 *, task->shareds) + 1;
4839 *upper = (kmp_uint64)ub;
4843 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4864void __kmp_taskloop_linear(
ident_t *loc,
int gtid, kmp_task_t *task,
4865 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4866 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
4867 kmp_uint64 grainsize, kmp_uint64 extras,
4868 kmp_int64 last_chunk, kmp_uint64 tc,
4874 KMP_TIME_PARTITIONED_BLOCK(OMP_taskloop_scheduling);
4875 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
4877 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4878 kmp_uint64 lower = task_bounds.get_lb();
4879 kmp_uint64 upper = task_bounds.get_ub();
4881 kmp_info_t *thread = __kmp_threads[gtid];
4882 kmp_taskdata_t *current_task = thread->th.th_current_task;
4883 kmp_task_t *next_task;
4884 kmp_int32 lastpriv = 0;
4886 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4887 (last_chunk < 0 ? last_chunk : extras));
4888 KMP_DEBUG_ASSERT(num_tasks > extras);
4889 KMP_DEBUG_ASSERT(num_tasks > 0);
4890 KA_TRACE(20, (
"__kmp_taskloop_linear: T#%d: %lld tasks, grainsize %lld, "
4891 "extras %lld, last_chunk %lld, i=%lld,%lld(%d)%lld, dup %p\n",
4892 gtid, num_tasks, grainsize, extras, last_chunk, lower, upper,
4893 ub_glob, st, task_dup));
4896 for (i = 0; i < num_tasks; ++i) {
4897 kmp_uint64 chunk_minus_1;
4899 chunk_minus_1 = grainsize - 1;
4901 chunk_minus_1 = grainsize;
4904 upper = lower + st * chunk_minus_1;
4908 if (i == num_tasks - 1) {
4911 KMP_DEBUG_ASSERT(upper == *ub);
4912 if (upper == ub_glob)
4914 }
else if (st > 0) {
4915 KMP_DEBUG_ASSERT((kmp_uint64)st > *ub - upper);
4916 if ((kmp_uint64)st > ub_glob - upper)
4919 KMP_DEBUG_ASSERT(upper + st < *ub);
4920 if (upper - ub_glob < (kmp_uint64)(-st))
4926 next_task = __kmp_task_dup_alloc(thread, task, 0);
4928 next_task = __kmp_task_dup_alloc(thread, task);
4931 kmp_taskdata_t *next_taskdata = KMP_TASK_TO_TASKDATA(next_task);
4932 kmp_taskloop_bounds_t next_task_bounds =
4933 kmp_taskloop_bounds_t(next_task, task_bounds);
4936 next_task_bounds.set_lb(lower);
4937 if (next_taskdata->td_flags.native) {
4938 next_task_bounds.set_ub(upper + (st > 0 ? 1 : -1));
4940 next_task_bounds.set_ub(upper);
4942 if (ptask_dup != NULL)
4944 ptask_dup(next_task, task, lastpriv);
4946 (
"__kmp_taskloop_linear: T#%d; task #%llu: task %p: lower %lld, "
4947 "upper %lld stride %lld, (offsets %p %p)\n",
4948 gtid, i, next_task, lower, upper, st,
4949 next_task_bounds.get_lower_offset(),
4950 next_task_bounds.get_upper_offset()));
4952 __kmp_omp_taskloop_task(NULL, gtid, next_task,
4955 if (ompt_enabled.ompt_callback_dispatch) {
4956 OMPT_GET_DISPATCH_CHUNK(next_taskdata->ompt_task_info.dispatch_chunk,
4961 __kmp_omp_task(gtid, next_task,
true);
4966 __kmp_task_start(gtid, task, current_task);
4968 __kmp_task_finish<false>(gtid, task, current_task);
4973typedef struct __taskloop_params {
4980 kmp_uint64 num_tasks;
4981 kmp_uint64 grainsize;
4983 kmp_int64 last_chunk;
4985 kmp_uint64 num_t_min;
4989} __taskloop_params_t;
4991void __kmp_taskloop_recur(
ident_t *,
int, kmp_task_t *, kmp_uint64 *,
4992 kmp_uint64 *, kmp_int64, kmp_uint64, kmp_uint64,
4993 kmp_uint64, kmp_uint64, kmp_int64, kmp_uint64,
5001int __kmp_taskloop_task(
int gtid,
void *ptask) {
5002 __taskloop_params_t *p =
5003 (__taskloop_params_t *)((kmp_task_t *)ptask)->shareds;
5004 kmp_task_t *task = p->task;
5005 kmp_uint64 *lb = p->lb;
5006 kmp_uint64 *ub = p->ub;
5007 void *task_dup = p->task_dup;
5009 kmp_int64 st = p->st;
5010 kmp_uint64 ub_glob = p->ub_glob;
5011 kmp_uint64 num_tasks = p->num_tasks;
5012 kmp_uint64 grainsize = p->grainsize;
5013 kmp_uint64 extras = p->extras;
5014 kmp_int64 last_chunk = p->last_chunk;
5015 kmp_uint64 tc = p->tc;
5016 kmp_uint64 num_t_min = p->num_t_min;
5018 void *codeptr_ra = p->codeptr_ra;
5021 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
5022 KMP_DEBUG_ASSERT(task != NULL);
5024 (
"__kmp_taskloop_task: T#%d, task %p: %lld tasks, grainsize"
5025 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
5026 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
5029 KMP_DEBUG_ASSERT(num_tasks * 2 + 1 > num_t_min);
5030 if (num_tasks > num_t_min)
5031 __kmp_taskloop_recur(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
5032 grainsize, extras, last_chunk, tc, num_t_min,
5038 __kmp_taskloop_linear(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
5039 grainsize, extras, last_chunk, tc,
5045 KA_TRACE(40, (
"__kmp_taskloop_task(exit): T#%d\n", gtid));
5067void __kmp_taskloop_recur(
ident_t *loc,
int gtid, kmp_task_t *task,
5068 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
5069 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
5070 kmp_uint64 grainsize, kmp_uint64 extras,
5071 kmp_int64 last_chunk, kmp_uint64 tc,
5072 kmp_uint64 num_t_min,
5077 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
5078 KMP_DEBUG_ASSERT(task != NULL);
5079 KMP_DEBUG_ASSERT(num_tasks > num_t_min);
5081 (
"__kmp_taskloop_recur: T#%d, task %p: %lld tasks, grainsize"
5082 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
5083 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
5085 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
5086 kmp_uint64 lower = *lb;
5087 kmp_info_t *thread = __kmp_threads[gtid];
5089 kmp_task_t *next_task;
5090 size_t lower_offset =
5091 (
char *)lb - (
char *)task;
5092 size_t upper_offset =
5093 (
char *)ub - (
char *)task;
5095 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
5096 (last_chunk < 0 ? last_chunk : extras));
5097 KMP_DEBUG_ASSERT(num_tasks > extras);
5098 KMP_DEBUG_ASSERT(num_tasks > 0);
5101 kmp_uint64 lb1, ub0, tc0, tc1, ext0, ext1;
5102 kmp_int64 last_chunk0 = 0, last_chunk1 = 0;
5103 kmp_uint64 gr_size0 = grainsize;
5104 kmp_uint64 n_tsk0 = num_tasks >> 1;
5105 kmp_uint64 n_tsk1 = num_tasks - n_tsk0;
5106 if (last_chunk < 0) {
5108 last_chunk1 = last_chunk;
5109 tc0 = grainsize * n_tsk0;
5111 }
else if (n_tsk0 <= extras) {
5114 ext1 = extras - n_tsk0;
5115 tc0 = gr_size0 * n_tsk0;
5120 tc1 = grainsize * n_tsk1;
5123 ub0 = lower + st * (tc0 - 1);
5128 next_task = __kmp_task_dup_alloc(thread, task,
5131 next_task = __kmp_task_dup_alloc(thread, task);
5134 *(kmp_uint64 *)((
char *)next_task + lower_offset) = lb1;
5135 if (ptask_dup != NULL)
5136 ptask_dup(next_task, task, 0);
5141 kmp_taskdata_t *current_task = thread->th.th_current_task;
5142 thread->th.th_current_task = taskdata->td_parent;
5143 kmp_task_t *new_task =
5144 __kmpc_omp_task_alloc(loc, gtid, 1, 3 *
sizeof(
void *),
5145 sizeof(__taskloop_params_t), &__kmp_taskloop_task);
5147 thread->th.th_current_task = current_task;
5148 __taskloop_params_t *p = (__taskloop_params_t *)new_task->shareds;
5149 p->task = next_task;
5150 p->lb = (kmp_uint64 *)((
char *)next_task + lower_offset);
5151 p->ub = (kmp_uint64 *)((
char *)next_task + upper_offset);
5152 p->task_dup = task_dup;
5154 p->ub_glob = ub_glob;
5155 p->num_tasks = n_tsk1;
5156 p->grainsize = grainsize;
5158 p->last_chunk = last_chunk1;
5160 p->num_t_min = num_t_min;
5162 p->codeptr_ra = codeptr_ra;
5166 kmp_taskdata_t *new_task_data = KMP_TASK_TO_TASKDATA(new_task);
5167 new_task_data->tdg = taskdata->tdg;
5168 new_task_data->is_taskgraph = 0;
5173 __kmp_omp_taskloop_task(NULL, gtid, new_task, codeptr_ra);
5175 __kmp_omp_task(gtid, new_task,
true);
5179 if (n_tsk0 > num_t_min)
5180 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0, gr_size0,
5181 ext0, last_chunk0, tc0, num_t_min,
5187 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0,
5188 gr_size0, ext0, last_chunk0, tc0,
5194 KA_TRACE(40, (
"__kmp_taskloop_recur(exit): T#%d\n", gtid));
5197static void __kmp_taskloop(
ident_t *loc,
int gtid, kmp_task_t *task,
int if_val,
5198 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
5199 int nogroup,
int sched, kmp_uint64 grainsize,
5200 int modifier,
void *task_dup) {
5201 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
5202 KMP_DEBUG_ASSERT(task != NULL);
5204#if OMPT_SUPPORT && OMPT_OPTIONAL
5205 OMPT_STORE_RETURN_ADDRESS(gtid);
5207 __kmpc_taskgroup(loc, gtid);
5211 KMP_ATOMIC_DEC(&__kmp_tdg_task_id);
5215 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
5218 kmp_uint64 lower = task_bounds.get_lb();
5219 kmp_uint64 upper = task_bounds.get_ub();
5220 kmp_uint64 ub_glob = upper;
5221 kmp_uint64 num_tasks = 0, extras = 0;
5222 kmp_int64 last_chunk =
5224 kmp_uint64 num_tasks_min = __kmp_taskloop_min_tasks;
5225 kmp_info_t *thread = __kmp_threads[gtid];
5226 kmp_taskdata_t *current_task = thread->th.th_current_task;
5228 KA_TRACE(20, (
"__kmp_taskloop: T#%d, task %p, lb %lld, ub %lld, st %lld, "
5229 "grain %llu(%d, %d), dup %p\n",
5230 gtid, taskdata, lower, upper, st, grainsize, sched, modifier,
5235 tc = upper - lower + 1;
5236 }
else if (st < 0) {
5237 tc = (lower - upper) / (-st) + 1;
5239 tc = (upper - lower) / st + 1;
5242 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d zero-trip loop\n", gtid));
5244 __kmp_task_start(gtid, task, current_task);
5246 __kmp_task_finish<false>(gtid, task, current_task);
5250#if OMPT_SUPPORT && OMPT_OPTIONAL
5251 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
5252 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
5253 if (ompt_enabled.ompt_callback_work) {
5254 ompt_callbacks.ompt_callback(ompt_callback_work)(
5255 ompt_work_taskloop, ompt_scope_begin, &(team_info->parallel_data),
5256 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
5260 if (num_tasks_min == 0)
5263 KMP_MIN(thread->th.th_team_nproc * 10, INITIAL_TASK_DEQUE_SIZE);
5269 grainsize = thread->th.th_team_nproc * 10;
5272 if (grainsize > tc) {
5277 num_tasks = grainsize;
5278 grainsize = tc / num_tasks;
5279 extras = tc % num_tasks;
5283 if (grainsize > tc) {
5289 num_tasks = (tc + grainsize - 1) / grainsize;
5290 last_chunk = tc - (num_tasks * grainsize);
5293 num_tasks = tc / grainsize;
5295 grainsize = tc / num_tasks;
5296 extras = tc % num_tasks;
5301 KMP_ASSERT2(0,
"unknown scheduling of taskloop");
5304 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
5305 (last_chunk < 0 ? last_chunk : extras));
5306 KMP_DEBUG_ASSERT(num_tasks > extras);
5307 KMP_DEBUG_ASSERT(num_tasks > 0);
5313 taskdata->td_flags.task_serial = 1;
5314 taskdata->td_flags.tiedness = TASK_TIED;
5316 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5317 grainsize, extras, last_chunk, tc,
5319 OMPT_GET_RETURN_ADDRESS(0),
5324 }
else if (num_tasks > num_tasks_min && !taskdata->td_flags.native) {
5325 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go recursive: tc %llu, #tasks %llu"
5326 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
5327 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
5329 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5330 grainsize, extras, last_chunk, tc, num_tasks_min,
5332 OMPT_GET_RETURN_ADDRESS(0),
5336 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go linear: tc %llu, #tasks %llu"
5337 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
5338 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
5340 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5341 grainsize, extras, last_chunk, tc,
5343 OMPT_GET_RETURN_ADDRESS(0),
5348#if OMPT_SUPPORT && OMPT_OPTIONAL
5349 if (ompt_enabled.ompt_callback_work) {
5350 ompt_callbacks.ompt_callback(ompt_callback_work)(
5351 ompt_work_taskloop, ompt_scope_end, &(team_info->parallel_data),
5352 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
5357#if OMPT_SUPPORT && OMPT_OPTIONAL
5358 OMPT_STORE_RETURN_ADDRESS(gtid);
5360 __kmpc_end_taskgroup(loc, gtid);
5362 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d\n", gtid));
5382 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
int nogroup,
5383 int sched, kmp_uint64 grainsize,
void *task_dup) {
5384 __kmp_assert_valid_gtid(gtid);
5385 KA_TRACE(20, (
"__kmpc_taskloop(enter): T#%d\n", gtid));
5386 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
5388 KA_TRACE(20, (
"__kmpc_taskloop(exit): T#%d\n", gtid));
5409 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
5410 int nogroup,
int sched, kmp_uint64 grainsize,
5411 int modifier,
void *task_dup) {
5412 __kmp_assert_valid_gtid(gtid);
5413 KA_TRACE(20, (
"__kmpc_taskloop_5(enter): T#%d\n", gtid));
5414 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
5415 modifier, task_dup);
5416 KA_TRACE(20, (
"__kmpc_taskloop_5(exit): T#%d\n", gtid));
5428 if (gtid == KMP_GTID_DNE)
5431 kmp_info_t *thread = __kmp_thread_from_gtid(gtid);
5432 kmp_taskdata_t *taskdata = thread->th.th_current_task;
5437 return &taskdata->td_target_data.async_handle;
5449 if (gtid == KMP_GTID_DNE)
5452 kmp_info_t *thread = __kmp_thread_from_gtid(gtid);
5453 kmp_taskdata_t *taskdata = thread->th.th_current_task;
5458 return taskdata->td_task_team != NULL;
5467static kmp_tdg_info_t *__kmp_find_tdg(kmp_int32 tdg_id) {
5468 kmp_tdg_info_t *res =
nullptr;
5469 if (__kmp_max_tdgs == 0)
5472 if (__kmp_global_tdgs == NULL)
5473 __kmp_global_tdgs = (kmp_tdg_info_t **)__kmp_allocate(
5474 sizeof(kmp_tdg_info_t *) * __kmp_max_tdgs);
5476 if ((__kmp_global_tdgs[tdg_id]) &&
5477 (__kmp_global_tdgs[tdg_id]->tdg_status != KMP_TDG_NONE))
5478 res = __kmp_global_tdgs[tdg_id];
5484void __kmp_print_tdg_dot(kmp_tdg_info_t *tdg) {
5485 kmp_int32 tdg_id = tdg->tdg_id;
5486 KA_TRACE(10, (
"__kmp_print_tdg_dot(enter): T#%d tdg_id=%d \n", gtid, tdg_id));
5489 sprintf(file_name,
"tdg_%d.dot", tdg_id);
5492 kmp_int32 num_tasks = KMP_ATOMIC_LD_RLX(&tdg->num_tasks);
5496 " subgraph cluster {\n"
5499 for (kmp_int32 i = 0; i < num_tasks; i++) {
5500 fprintf(tdg_file,
" %d[style=bold]\n", i);
5502 fprintf(tdg_file,
" }\n");
5503 for (kmp_int32 i = 0; i < num_tasks; i++) {
5504 kmp_int32 nsuccessors = tdg->record_map[i].nsuccessors;
5505 kmp_int32 *successors = tdg->record_map[i].successors;
5506 if (nsuccessors > 0) {
5507 for (kmp_int32 j = 0; j < nsuccessors; j++)
5508 fprintf(tdg_file,
" %d -> %d \n", i, successors[j]);
5511 fprintf(tdg_file,
"}");
5512 KA_TRACE(10, (
"__kmp_print_tdg_dot(exit): T#%d tdg_id=%d \n", gtid, tdg_id));
5519void __kmp_exec_tdg(kmp_int32 gtid, kmp_tdg_info_t *tdg) {
5520 KMP_DEBUG_ASSERT(tdg->tdg_status == KMP_TDG_READY);
5521 KA_TRACE(10, (
"__kmp_exec_tdg(enter): T#%d tdg_id=%d num_roots=%d\n", gtid,
5522 tdg->tdg_id, tdg->num_roots));
5523 kmp_node_info_t *this_record_map = tdg->record_map;
5524 kmp_int32 *this_root_tasks = tdg->root_tasks;
5525 kmp_int32 this_num_roots = tdg->num_roots;
5526 kmp_int32 this_num_tasks = KMP_ATOMIC_LD_RLX(&tdg->num_tasks);
5528 kmp_info_t *thread = __kmp_threads[gtid];
5529 kmp_taskdata_t *parent_task = thread->th.th_current_task;
5531 if (tdg->rec_taskred_data) {
5535 for (kmp_int32 j = 0; j < this_num_tasks; j++) {
5536 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(this_record_map[j].task);
5538 td->td_parent = parent_task;
5539 this_record_map[j].parent_task = parent_task;
5541 kmp_taskgroup_t *parent_taskgroup =
5542 this_record_map[j].parent_task->td_taskgroup;
5544 KMP_ATOMIC_ST_RLX(&this_record_map[j].npredecessors_counter,
5545 this_record_map[j].npredecessors);
5546 KMP_ATOMIC_INC(&this_record_map[j].parent_task->td_incomplete_child_tasks);
5548 if (parent_taskgroup) {
5549 KMP_ATOMIC_INC(&parent_taskgroup->count);
5551 td->td_taskgroup = parent_taskgroup;
5552 }
else if (td->td_taskgroup !=
nullptr) {
5554 td->td_taskgroup =
nullptr;
5556 if (this_record_map[j].parent_task->td_flags.tasktype == TASK_EXPLICIT)
5557 KMP_ATOMIC_INC(&this_record_map[j].parent_task->td_allocated_child_tasks);
5560 for (kmp_int32 j = 0; j < this_num_roots; ++j) {
5561 __kmp_omp_task(gtid, this_record_map[this_root_tasks[j]].task,
true);
5563 KA_TRACE(10, (
"__kmp_exec_tdg(exit): T#%d tdg_id=%d num_roots=%d\n", gtid,
5564 tdg->tdg_id, tdg->num_roots));
5572static inline void __kmp_start_record(kmp_int32 gtid,
5573 kmp_taskgraph_flags_t *flags,
5575 kmp_tdg_info_t *tdg =
5576 (kmp_tdg_info_t *)__kmp_allocate(
sizeof(kmp_tdg_info_t));
5577 __kmp_global_tdgs[__kmp_curr_tdg_idx] = tdg;
5579 tdg->tdg_id = tdg_id;
5580 tdg->map_size = INIT_MAPSIZE;
5581 tdg->num_roots = -1;
5582 tdg->root_tasks =
nullptr;
5583 tdg->tdg_status = KMP_TDG_RECORDING;
5584 tdg->rec_num_taskred = 0;
5585 tdg->rec_taskred_data =
nullptr;
5586 KMP_ATOMIC_ST_RLX(&tdg->num_tasks, 0);
5589 kmp_node_info_t *this_record_map =
5590 (kmp_node_info_t *)__kmp_allocate(INIT_MAPSIZE *
sizeof(kmp_node_info_t));
5591 for (kmp_int32 i = 0; i < INIT_MAPSIZE; i++) {
5592 kmp_int32 *successorsList =
5593 (kmp_int32 *)__kmp_allocate(__kmp_successors_size *
sizeof(kmp_int32));
5594 this_record_map[i].task =
nullptr;
5595 this_record_map[i].successors = successorsList;
5596 this_record_map[i].nsuccessors = 0;
5597 this_record_map[i].npredecessors = 0;
5598 this_record_map[i].successors_size = __kmp_successors_size;
5599 KMP_ATOMIC_ST_RLX(&this_record_map[i].npredecessors_counter, 0);
5602 __kmp_global_tdgs[__kmp_curr_tdg_idx]->record_map = this_record_map;
5612kmp_int32 __kmpc_start_record_task(
ident_t *loc_ref, kmp_int32 gtid,
5613 kmp_int32 input_flags, kmp_int32 tdg_id) {
5616 kmp_taskgraph_flags_t *flags = (kmp_taskgraph_flags_t *)&input_flags;
5618 (
"__kmpc_start_record_task(enter): T#%d loc=%p flags=%d tdg_id=%d\n",
5619 gtid, loc_ref, input_flags, tdg_id));
5621 if (__kmp_max_tdgs == 0) {
5624 (
"__kmpc_start_record_task(abandon): T#%d loc=%p flags=%d tdg_id = %d, "
5625 "__kmp_max_tdgs = 0\n",
5626 gtid, loc_ref, input_flags, tdg_id));
5630 __kmpc_taskgroup(loc_ref, gtid);
5631 if (kmp_tdg_info_t *tdg = __kmp_find_tdg(tdg_id)) {
5633 __kmp_exec_tdg(gtid, tdg);
5636 __kmp_curr_tdg_idx = tdg_id;
5637 KMP_DEBUG_ASSERT(__kmp_curr_tdg_idx < __kmp_max_tdgs);
5638 __kmp_start_record(gtid, flags, tdg_id);
5642 KA_TRACE(10, (
"__kmpc_start_record_task(exit): T#%d TDG %d starts to %s\n",
5643 gtid, tdg_id, res ?
"record" :
"execute"));
5650void __kmp_end_record(kmp_int32 gtid, kmp_tdg_info_t *tdg) {
5652 kmp_node_info_t *this_record_map = tdg->record_map;
5653 kmp_int32 this_num_tasks = KMP_ATOMIC_LD_RLX(&tdg->num_tasks);
5654 kmp_int32 *this_root_tasks =
5655 (kmp_int32 *)__kmp_allocate(this_num_tasks *
sizeof(kmp_int32));
5656 kmp_int32 this_map_size = tdg->map_size;
5657 kmp_int32 this_num_roots = 0;
5658 kmp_info_t *thread = __kmp_threads[gtid];
5660 for (kmp_int32 i = 0; i < this_num_tasks; i++) {
5661 if (this_record_map[i].npredecessors == 0) {
5662 this_root_tasks[this_num_roots++] = i;
5667 tdg->map_size = this_map_size;
5668 tdg->num_roots = this_num_roots;
5669 tdg->root_tasks = this_root_tasks;
5670 KMP_DEBUG_ASSERT(tdg->tdg_status == KMP_TDG_RECORDING);
5671 tdg->tdg_status = KMP_TDG_READY;
5673 if (thread->th.th_current_task->td_dephash) {
5674 __kmp_dephash_free(thread, thread->th.th_current_task->td_dephash);
5675 thread->th.th_current_task->td_dephash = NULL;
5679 for (kmp_int32 i = 0; i < this_num_tasks; i++) {
5680 KMP_ATOMIC_ST_RLX(&this_record_map[i].npredecessors_counter,
5681 this_record_map[i].npredecessors);
5683 KMP_ATOMIC_ST_RLX(&__kmp_tdg_task_id, 0);
5686 __kmp_print_tdg_dot(tdg);
5696void __kmpc_end_record_task(
ident_t *loc_ref, kmp_int32 gtid,
5697 kmp_int32 input_flags, kmp_int32 tdg_id) {
5698 kmp_tdg_info_t *tdg = __kmp_find_tdg(tdg_id);
5700 KA_TRACE(10, (
"__kmpc_end_record_task(enter): T#%d loc=%p finishes recording"
5701 " tdg=%d with flags=%d\n",
5702 gtid, loc_ref, tdg_id, input_flags));
5703 if (__kmp_max_tdgs) {
5705 __kmpc_end_taskgroup(loc_ref, gtid);
5706 if (__kmp_tdg_is_recording(tdg->tdg_status))
5707 __kmp_end_record(gtid, tdg);
5709 KA_TRACE(10, (
"__kmpc_end_record_task(exit): T#%d loc=%p finished recording"
5710 " tdg=%d, its status is now READY\n",
5711 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