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));
720static inline void __ompt_task_start(kmp_task_t *task,
721 kmp_taskdata_t *current_task,
723 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
724 ompt_task_status_t status = ompt_task_switch;
725 if (__kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded) {
726 status = ompt_task_yield;
727 __kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded = 0;
730 if (ompt_enabled.ompt_callback_task_schedule) {
731 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
732 &(current_task->ompt_task_info.task_data), status,
733 &(taskdata->ompt_task_info.task_data));
735 taskdata->ompt_task_info.scheduling_parent = current_task;
740static inline void __ompt_task_finish(kmp_task_t *task,
741 kmp_taskdata_t *resumed_task,
742 ompt_task_status_t status) {
743 if (ompt_enabled.ompt_callback_task_schedule) {
744 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
745 if (__kmp_omp_cancellation && taskdata->td_taskgroup &&
746 taskdata->td_taskgroup->cancel_request == cancel_taskgroup) {
747 status = ompt_task_cancel;
751 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
752 &(taskdata->ompt_task_info.task_data), status,
753 (resumed_task ? &(resumed_task->ompt_task_info.task_data) : NULL));
759static void __kmpc_omp_task_begin_if0_template(
ident_t *loc_ref, kmp_int32 gtid,
762 void *return_address) {
763 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
764 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
766 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(enter): T#%d loc=%p task=%p "
768 gtid, loc_ref, taskdata, current_task));
770 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
773 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
774 KMP_DEBUG_USE_VAR(counter);
775 KA_TRACE(20, (
"__kmpc_omp_task_begin_if0: T#%d untied_count (%d) "
776 "incremented for task %p\n",
777 gtid, counter, taskdata));
780 taskdata->td_flags.task_serial =
782 __kmp_task_start(gtid, task, current_task);
786 if (current_task->ompt_task_info.frame.enter_frame.ptr == NULL) {
787 current_task->ompt_task_info.frame.enter_frame.ptr =
788 taskdata->ompt_task_info.frame.exit_frame.ptr = frame_address;
789 current_task->ompt_task_info.frame.enter_frame_flags =
790 taskdata->ompt_task_info.frame.exit_frame_flags =
791 OMPT_FRAME_FLAGS_APP;
793 if (ompt_enabled.ompt_callback_task_create) {
794 ompt_task_info_t *parent_info = &(current_task->ompt_task_info);
795 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
796 &(parent_info->task_data), &(parent_info->frame),
797 &(taskdata->ompt_task_info.task_data),
798 TASK_TYPE_DETAILS_FORMAT(taskdata), 0, return_address);
800 __ompt_task_start(task, current_task, gtid);
804 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(exit): T#%d loc=%p task=%p,\n", gtid,
810static void __kmpc_omp_task_begin_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
813 void *return_address) {
814 __kmpc_omp_task_begin_if0_template<true>(loc_ref, gtid, task, frame_address,
831__attribute__((target(
"backchain")))
833void __kmpc_omp_task_begin_if0(
ident_t *loc_ref, kmp_int32 gtid,
836 if (UNLIKELY(ompt_enabled.enabled)) {
837 OMPT_STORE_RETURN_ADDRESS(gtid);
838 __kmpc_omp_task_begin_if0_ompt(loc_ref, gtid, task,
839 OMPT_GET_FRAME_ADDRESS(1),
840 OMPT_LOAD_RETURN_ADDRESS(gtid));
844 __kmpc_omp_task_begin_if0_template<false>(loc_ref, gtid, task, NULL, NULL);
850void __kmpc_omp_task_begin(
ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task) {
851 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
855 (
"__kmpc_omp_task_begin(enter): T#%d loc=%p task=%p current_task=%p\n",
856 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task), current_task));
858 __kmp_task_start(gtid, task, current_task);
860 KA_TRACE(10, (
"__kmpc_omp_task_begin(exit): T#%d loc=%p task=%p,\n", gtid,
861 loc_ref, KMP_TASK_TO_TASKDATA(task)));
871static void __kmp_free_task(kmp_int32 gtid, kmp_taskdata_t *taskdata,
872 kmp_info_t *thread) {
873 KA_TRACE(30, (
"__kmp_free_task: T#%d freeing data from task %p\n", gtid,
877 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
878 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0);
879 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 1);
880 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
881 KMP_DEBUG_ASSERT(taskdata->td_allocated_child_tasks == 0 ||
882 taskdata->td_flags.task_serial == 1);
883 KMP_DEBUG_ASSERT(taskdata->td_incomplete_child_tasks == 0);
884 kmp_task_t *task = KMP_TASKDATA_TO_TASK(taskdata);
886 task->data1.destructors = NULL;
887 task->data2.priority = 0;
889 taskdata->td_flags.freed = 1;
892 if (!taskdata->is_taskgraph) {
896 __kmp_fast_free(thread, taskdata);
898 __kmp_thread_free(thread, taskdata);
902 taskdata->td_flags.complete = 0;
903 taskdata->td_flags.started = 0;
904 taskdata->td_flags.freed = 0;
905 taskdata->td_flags.executing = 0;
906 taskdata->td_flags.task_serial =
907 (taskdata->td_parent->td_flags.final ||
908 taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser);
911 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
912 KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0);
914 KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1);
918 KA_TRACE(20, (
"__kmp_free_task: T#%d freed task %p\n", gtid, taskdata));
927static void __kmp_free_task_and_ancestors(kmp_int32 gtid,
928 kmp_taskdata_t *taskdata,
929 kmp_info_t *thread) {
932 kmp_int32 team_serial =
933 (taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) &&
934 !taskdata->td_flags.proxy;
935 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
937 kmp_int32 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
938 KMP_DEBUG_ASSERT(children >= 0);
941 while (children == 0) {
942 kmp_taskdata_t *parent_taskdata = taskdata->td_parent;
944 KA_TRACE(20, (
"__kmp_free_task_and_ancestors(enter): T#%d task %p complete "
945 "and freeing itself\n",
949 __kmp_free_task(gtid, taskdata, thread);
951 taskdata = parent_taskdata;
957 if (taskdata->td_flags.tasktype == TASK_IMPLICIT) {
958 if (taskdata->td_dephash) {
959 int children = KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks);
960 kmp_tasking_flags_t flags_old = taskdata->td_flags;
961 if (children == 0 && flags_old.complete == 1) {
962 kmp_tasking_flags_t flags_new = flags_old;
963 flags_new.complete = 0;
964 if (KMP_COMPARE_AND_STORE_ACQ32(
965 RCAST(kmp_int32 *, &taskdata->td_flags),
966 *RCAST(kmp_int32 *, &flags_old),
967 *RCAST(kmp_int32 *, &flags_new))) {
968 KA_TRACE(100, (
"__kmp_free_task_and_ancestors: T#%d cleans "
969 "dephash of implicit task %p\n",
972 __kmp_dephash_free_entries(thread, taskdata->td_dephash);
979 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
980 KMP_DEBUG_ASSERT(children >= 0);
984 20, (
"__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; "
985 "not freeing it yet\n",
986 gtid, taskdata, children));
997static bool __kmp_track_children_task(kmp_taskdata_t *taskdata) {
998 kmp_tasking_flags_t flags = taskdata->td_flags;
999 bool ret = !(flags.team_serial || flags.tasking_ser);
1000 ret = ret || flags.proxy == TASK_PROXY ||
1001 flags.detachable == TASK_DETACHABLE || flags.hidden_helper;
1003 KMP_ATOMIC_LD_ACQ(&taskdata->td_parent->td_incomplete_child_tasks) > 0;
1005 if (taskdata->td_taskgroup && taskdata->is_taskgraph)
1006 ret = ret || KMP_ATOMIC_LD_ACQ(&taskdata->td_taskgroup->count) > 0;
1021static void __kmp_task_finish(kmp_int32 gtid, kmp_task_t *task,
1022 kmp_taskdata_t *resumed_task) {
1023 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
1024 kmp_info_t *thread = __kmp_threads[gtid];
1025 kmp_task_team_t *task_team =
1026 thread->th.th_task_team;
1032 kmp_int32 children = 0;
1034 KA_TRACE(10, (
"__kmp_task_finish(enter): T#%d finishing task %p and resuming "
1036 gtid, taskdata, resumed_task));
1038 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
1041 is_taskgraph = taskdata->is_taskgraph;
1045#ifdef BUILD_TIED_TASK_STACK
1046 if (taskdata->td_flags.tiedness == TASK_TIED) {
1047 __kmp_pop_task_stack(gtid, thread, taskdata);
1051 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
1054 kmp_int32 counter = KMP_ATOMIC_DEC(&taskdata->td_untied_count) - 1;
1057 (
"__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n",
1058 gtid, counter, taskdata));
1062 if (resumed_task == NULL) {
1063 KMP_DEBUG_ASSERT(taskdata->td_flags.task_serial);
1064 resumed_task = taskdata->td_parent;
1067 thread->th.th_current_task = resumed_task;
1068 resumed_task->td_flags.executing = 1;
1069 KA_TRACE(10, (
"__kmp_task_finish(exit): T#%d partially done task %p, "
1070 "resuming task %p\n",
1071 gtid, taskdata, resumed_task));
1079 (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) ==
1080 taskdata->td_flags.task_serial);
1081 if (taskdata->td_flags.task_serial) {
1082 if (resumed_task == NULL) {
1083 resumed_task = taskdata->td_parent;
1087 KMP_DEBUG_ASSERT(resumed_task !=
1097 if (UNLIKELY(taskdata->td_flags.destructors_thunk)) {
1098 kmp_routine_entry_t destr_thunk = task->data1.destructors;
1099 KMP_ASSERT(destr_thunk);
1100 destr_thunk(gtid, task);
1103 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
1104 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 1);
1105 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
1107 bool completed =
true;
1108 if (UNLIKELY(taskdata->td_flags.detachable == TASK_DETACHABLE)) {
1109 if (taskdata->td_allow_completion_event.type ==
1110 KMP_EVENT_ALLOW_COMPLETION) {
1112 __kmp_acquire_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
1113 if (taskdata->td_allow_completion_event.type ==
1114 KMP_EVENT_ALLOW_COMPLETION) {
1116 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
1117 taskdata->td_flags.executing = 0;
1124 __ompt_task_finish(task, resumed_task, ompt_task_detach);
1130 taskdata->td_flags.proxy = TASK_PROXY;
1133 __kmp_release_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
1138 if (taskdata->td_target_data.async_handle != NULL) {
1144 __ompt_task_finish(task, resumed_task, ompt_task_switch);
1147 __kmpc_give_task(task, __kmp_tid_from_gtid(gtid));
1148 if (KMP_HIDDEN_HELPER_THREAD(gtid))
1149 __kmp_hidden_helper_worker_thread_signal();
1154 taskdata->td_flags.complete = 1;
1156 taskdata->td_flags.onced = 1;
1162 __ompt_task_finish(task, resumed_task, ompt_task_complete);
1166 if (__kmp_track_children_task(taskdata)) {
1167 __kmp_release_deps(gtid, taskdata);
1172 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks);
1173 KMP_DEBUG_ASSERT(children >= 0);
1175 if (taskdata->td_taskgroup && !taskdata->is_taskgraph)
1177 if (taskdata->td_taskgroup)
1179 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
1180 }
else if (task_team && (task_team->tt.tt_found_proxy_tasks ||
1181 task_team->tt.tt_hidden_helper_task_encountered)) {
1184 __kmp_release_deps(gtid, taskdata);
1190 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
1191 taskdata->td_flags.executing = 0;
1194 if (taskdata->td_flags.hidden_helper) {
1196 KMP_ASSERT(KMP_HIDDEN_HELPER_THREAD(gtid));
1197 KMP_ATOMIC_DEC(&__kmp_unexecuted_hidden_helper_tasks);
1202 20, (
"__kmp_task_finish: T#%d finished task %p, %d incomplete children\n",
1203 gtid, taskdata, children));
1209 thread->th.th_current_task = resumed_task;
1211 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
1215 resumed_task->td_flags.executing = 1;
1218 if (is_taskgraph && __kmp_track_children_task(taskdata) &&
1219 taskdata->td_taskgroup) {
1226 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
1231 10, (
"__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n",
1232 gtid, taskdata, resumed_task));
1238static void __kmpc_omp_task_complete_if0_template(
ident_t *loc_ref,
1241 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n",
1242 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
1243 KMP_DEBUG_ASSERT(gtid >= 0);
1245 __kmp_task_finish<ompt>(gtid, task, NULL);
1247 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n",
1248 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
1252 ompt_frame_t *ompt_frame;
1253 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
1254 ompt_frame->enter_frame = ompt_data_none;
1255 ompt_frame->enter_frame_flags = OMPT_FRAME_FLAGS_RUNTIME;
1264void __kmpc_omp_task_complete_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
1266 __kmpc_omp_task_complete_if0_template<true>(loc_ref, gtid, task);
1275void __kmpc_omp_task_complete_if0(
ident_t *loc_ref, kmp_int32 gtid,
1278 if (UNLIKELY(ompt_enabled.enabled)) {
1279 __kmpc_omp_task_complete_if0_ompt(loc_ref, gtid, task);
1283 __kmpc_omp_task_complete_if0_template<false>(loc_ref, gtid, task);
1289void __kmpc_omp_task_complete(
ident_t *loc_ref, kmp_int32 gtid,
1291 KA_TRACE(10, (
"__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n", gtid,
1292 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1294 __kmp_task_finish<false>(gtid, task,
1297 KA_TRACE(10, (
"__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n", gtid,
1298 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1314void __kmp_init_implicit_task(
ident_t *loc_ref, kmp_info_t *this_thr,
1315 kmp_team_t *team,
int tid,
int set_curr_task) {
1316 kmp_taskdata_t *task = &team->t.t_implicit_task_taskdata[tid];
1320 (
"__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n",
1321 tid, team, task, set_curr_task ?
"TRUE" :
"FALSE"));
1323 task->td_task_id = KMP_GEN_TASK_ID();
1324 task->td_team = team;
1327 task->td_ident = loc_ref;
1328 task->td_taskwait_ident = NULL;
1329 task->td_taskwait_counter = 0;
1330 task->td_taskwait_thread = 0;
1332 task->td_flags.tiedness = TASK_TIED;
1333 task->td_flags.tasktype = TASK_IMPLICIT;
1334 task->td_flags.proxy = TASK_FULL;
1337 task->td_flags.task_serial = 1;
1338 task->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1339 task->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1341 task->td_flags.started = 1;
1342 task->td_flags.executing = 1;
1343 task->td_flags.complete = 0;
1344 task->td_flags.freed = 0;
1346 task->td_flags.onced = 0;
1349 task->td_depnode = NULL;
1350 task->td_last_tied = task;
1351 task->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1353 if (set_curr_task) {
1354 KMP_ATOMIC_ST_REL(&task->td_incomplete_child_tasks, 0);
1356 KMP_ATOMIC_ST_REL(&task->td_allocated_child_tasks, 0);
1357 task->td_taskgroup = NULL;
1358 task->td_dephash = NULL;
1359 __kmp_push_current_task_to_thread(this_thr, team, tid);
1361 KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0);
1362 KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0);
1366 if (UNLIKELY(ompt_enabled.enabled))
1367 __ompt_task_init(task, tid);
1370 KF_TRACE(10, (
"__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n", tid,
1379void __kmp_finish_implicit_task(kmp_info_t *thread) {
1380 kmp_taskdata_t *task = thread->th.th_current_task;
1381 if (task->td_dephash) {
1383 task->td_flags.complete = 1;
1385 task->td_flags.onced = 1;
1387 children = KMP_ATOMIC_LD_ACQ(&task->td_incomplete_child_tasks);
1388 kmp_tasking_flags_t flags_old = task->td_flags;
1389 if (children == 0 && flags_old.complete == 1) {
1390 kmp_tasking_flags_t flags_new = flags_old;
1391 flags_new.complete = 0;
1392 if (KMP_COMPARE_AND_STORE_ACQ32(RCAST(kmp_int32 *, &task->td_flags),
1393 *RCAST(kmp_int32 *, &flags_old),
1394 *RCAST(kmp_int32 *, &flags_new))) {
1395 KA_TRACE(100, (
"__kmp_finish_implicit_task: T#%d cleans "
1396 "dephash of implicit task %p\n",
1397 thread->th.th_info.ds.ds_gtid, task));
1398 __kmp_dephash_free_entries(thread, task->td_dephash);
1408void __kmp_free_implicit_task(kmp_info_t *thread) {
1409 kmp_taskdata_t *task = thread->th.th_current_task;
1410 if (task && task->td_dephash) {
1411 __kmp_dephash_free(thread, task->td_dephash);
1412 task->td_dephash = NULL;
1418static size_t __kmp_round_up_to_val(
size_t size,
size_t val) {
1419 if (size & (val - 1)) {
1421 if (size <= KMP_SIZE_T_MAX - val) {
1440kmp_task_t *__kmp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1441 kmp_tasking_flags_t *flags,
1442 size_t sizeof_kmp_task_t,
size_t sizeof_shareds,
1443 kmp_routine_entry_t task_entry) {
1445 kmp_taskdata_t *taskdata;
1446 kmp_info_t *thread = __kmp_threads[gtid];
1447 kmp_team_t *team = thread->th.th_team;
1448 kmp_taskdata_t *parent_task = thread->th.th_current_task;
1449 size_t shareds_offset;
1451 if (UNLIKELY(!TCR_4(__kmp_init_middle)))
1452 __kmp_middle_initialize();
1454 if (flags->hidden_helper) {
1455 if (__kmp_enable_hidden_helper) {
1456 if (!TCR_4(__kmp_init_hidden_helper))
1457 __kmp_hidden_helper_initialize();
1460 flags->hidden_helper = FALSE;
1464 KA_TRACE(10, (
"__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) "
1465 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1466 gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t,
1467 sizeof_shareds, task_entry));
1469 KMP_DEBUG_ASSERT(parent_task);
1470 if (parent_task->td_flags.final) {
1471 if (flags->merged_if0) {
1476 if (flags->tiedness == TASK_UNTIED && !team->t.t_serialized) {
1480 KMP_CHECK_UPDATE(thread->th.th_task_team->tt.tt_untied_task_encountered, 1);
1486 if (UNLIKELY(flags->proxy == TASK_PROXY ||
1487 flags->detachable == TASK_DETACHABLE || flags->hidden_helper)) {
1488 if (flags->proxy == TASK_PROXY) {
1489 flags->tiedness = TASK_UNTIED;
1490 flags->merged_if0 = 1;
1494 if ((thread->th.th_task_team) == NULL) {
1497 KMP_DEBUG_ASSERT(team->t.t_serialized);
1499 (
"T#%d creating task team in __kmp_task_alloc for proxy task\n",
1501 __kmp_task_team_setup(thread, team);
1502 thread->th.th_task_team = team->t.t_task_team[thread->th.th_task_state];
1504 kmp_task_team_t *task_team = thread->th.th_task_team;
1507 if (!KMP_TASKING_ENABLED(task_team)) {
1510 (
"T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid));
1511 __kmp_enable_tasking(task_team, thread);
1512 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
1513 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
1515 if (thread_data->td.td_deque == NULL) {
1516 __kmp_alloc_task_deque(thread, thread_data);
1520 if ((flags->proxy == TASK_PROXY || flags->detachable == TASK_DETACHABLE) &&
1521 task_team->tt.tt_found_proxy_tasks == FALSE)
1522 TCW_4(task_team->tt.tt_found_proxy_tasks, TRUE);
1523 if (flags->hidden_helper &&
1524 task_team->tt.tt_hidden_helper_task_encountered == FALSE)
1525 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, TRUE);
1530 shareds_offset =
sizeof(kmp_taskdata_t) + sizeof_kmp_task_t;
1531 shareds_offset = __kmp_round_up_to_val(shareds_offset,
sizeof(
void *));
1534 KA_TRACE(30, (
"__kmp_task_alloc: T#%d First malloc size: %ld\n", gtid,
1536 KA_TRACE(30, (
"__kmp_task_alloc: T#%d Second malloc size: %ld\n", gtid,
1541 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, shareds_offset +
1544 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, shareds_offset +
1548 task = KMP_TASKDATA_TO_TASK(taskdata);
1551#if KMP_ARCH_X86 || KMP_ARCH_PPC64 || KMP_ARCH_S390X || !KMP_HAVE_QUAD
1552 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(
double) - 1)) == 0);
1553 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(
double) - 1)) == 0);
1555 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(_Quad) - 1)) == 0);
1556 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(_Quad) - 1)) == 0);
1558 if (sizeof_shareds > 0) {
1560 task->shareds = &((
char *)taskdata)[shareds_offset];
1562 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
1565 task->shareds = NULL;
1567 task->routine = task_entry;
1570 taskdata->td_task_id = KMP_GEN_TASK_ID();
1571 taskdata->td_team = thread->th.th_team;
1572 taskdata->td_alloc_thread = thread;
1573 taskdata->td_parent = parent_task;
1574 taskdata->td_level = parent_task->td_level + 1;
1575 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
1576 taskdata->td_ident = loc_ref;
1577 taskdata->td_taskwait_ident = NULL;
1578 taskdata->td_taskwait_counter = 0;
1579 taskdata->td_taskwait_thread = 0;
1580 KMP_DEBUG_ASSERT(taskdata->td_parent != NULL);
1582 if (flags->proxy == TASK_FULL)
1583 copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs);
1585 taskdata->td_flags = *flags;
1586 taskdata->td_task_team = thread->th.th_task_team;
1587 taskdata->td_size_alloc = shareds_offset + sizeof_shareds;
1588 taskdata->td_flags.tasktype = TASK_EXPLICIT;
1591 if (flags->hidden_helper) {
1592 kmp_info_t *shadow_thread = __kmp_threads[KMP_GTID_TO_SHADOW_GTID(gtid)];
1593 taskdata->td_team = shadow_thread->th.th_team;
1594 taskdata->td_task_team = shadow_thread->th.th_task_team;
1598 taskdata->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1601 taskdata->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1607 taskdata->td_flags.task_serial =
1608 (parent_task->td_flags.final || taskdata->td_flags.team_serial ||
1609 taskdata->td_flags.tasking_ser || flags->merged_if0);
1611 taskdata->td_flags.started = 0;
1612 taskdata->td_flags.executing = 0;
1613 taskdata->td_flags.complete = 0;
1614 taskdata->td_flags.freed = 0;
1616 taskdata->td_flags.onced = 0;
1617 taskdata->is_taskgraph = 0;
1618 taskdata->tdg =
nullptr;
1620 KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0);
1622 KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1);
1623 taskdata->td_taskgroup =
1624 parent_task->td_taskgroup;
1625 taskdata->td_dephash = NULL;
1626 taskdata->td_depnode = NULL;
1627 taskdata->td_target_data.async_handle = NULL;
1628 if (flags->tiedness == TASK_UNTIED)
1629 taskdata->td_last_tied = NULL;
1631 taskdata->td_last_tied = taskdata;
1632 taskdata->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1634 if (UNLIKELY(ompt_enabled.enabled))
1635 __ompt_task_init(taskdata, gtid);
1639 if (__kmp_track_children_task(taskdata)) {
1640 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
1641 if (parent_task->td_taskgroup)
1642 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
1645 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) {
1646 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
1648 if (flags->hidden_helper) {
1649 taskdata->td_flags.task_serial = FALSE;
1651 KMP_ATOMIC_INC(&__kmp_unexecuted_hidden_helper_tasks);
1656 kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_idx);
1657 if (tdg && __kmp_tdg_is_recording(tdg->tdg_status) &&
1658 (task_entry != (kmp_routine_entry_t)__kmp_taskloop_task)) {
1659 taskdata->is_taskgraph = 1;
1660 taskdata->tdg = __kmp_global_tdgs[__kmp_curr_tdg_idx];
1661 taskdata->td_task_id = KMP_GEN_TASK_ID();
1662 taskdata->td_tdg_task_id = KMP_ATOMIC_INC(&__kmp_tdg_task_id);
1665 KA_TRACE(20, (
"__kmp_task_alloc(exit): T#%d created task %p parent=%p\n",
1666 gtid, taskdata, taskdata->td_parent));
1671kmp_task_t *__kmpc_omp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1672 kmp_int32 flags,
size_t sizeof_kmp_task_t,
1673 size_t sizeof_shareds,
1674 kmp_routine_entry_t task_entry) {
1676 kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
1677 __kmp_assert_valid_gtid(gtid);
1678 input_flags->native = FALSE;
1680 KA_TRACE(10, (
"__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s %s) "
1681 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1682 gtid, loc_ref, input_flags->tiedness ?
"tied " :
"untied",
1683 input_flags->proxy ?
"proxy" :
"",
1684 input_flags->detachable ?
"detachable" :
"", sizeof_kmp_task_t,
1685 sizeof_shareds, task_entry));
1687 retval = __kmp_task_alloc(loc_ref, gtid, input_flags, sizeof_kmp_task_t,
1688 sizeof_shareds, task_entry);
1690 KA_TRACE(20, (
"__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval));
1695kmp_task_t *__kmpc_omp_target_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1697 size_t sizeof_kmp_task_t,
1698 size_t sizeof_shareds,
1699 kmp_routine_entry_t task_entry,
1700 kmp_int64 device_id) {
1701 auto &input_flags =
reinterpret_cast<kmp_tasking_flags_t &
>(flags);
1703 input_flags.tiedness = TASK_UNTIED;
1704 input_flags.target = 1;
1706 if (__kmp_enable_hidden_helper)
1707 input_flags.hidden_helper = TRUE;
1709 return __kmpc_omp_task_alloc(loc_ref, gtid, flags, sizeof_kmp_task_t,
1710 sizeof_shareds, task_entry);
1728 kmp_task_t *new_task, kmp_int32 naffins,
1729 kmp_task_affinity_info_t *affin_list) {
1739__attribute__((target(
"backchain")))
1742__kmp_invoke_task(kmp_int32 gtid, kmp_task_t *task,
1743 kmp_taskdata_t *current_task) {
1744 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
1748 30, (
"__kmp_invoke_task(enter): T#%d invoking task %p, current_task=%p\n",
1749 gtid, taskdata, current_task));
1750 KMP_DEBUG_ASSERT(task);
1751 if (UNLIKELY(taskdata->td_flags.proxy == TASK_PROXY &&
1752 taskdata->td_flags.complete == 1)) {
1757 (
"__kmp_invoke_task: T#%d running bottom finish for proxy task %p\n",
1760 __kmp_bottom_half_finish_proxy(gtid, task);
1762 KA_TRACE(30, (
"__kmp_invoke_task(exit): T#%d completed bottom finish for "
1763 "proxy task %p, resuming task %p\n",
1764 gtid, taskdata, current_task));
1772 ompt_thread_info_t oldInfo;
1773 if (UNLIKELY(ompt_enabled.enabled)) {
1775 thread = __kmp_threads[gtid];
1776 oldInfo = thread->th.ompt_thread_info;
1777 thread->th.ompt_thread_info.wait_id = 0;
1778 thread->th.ompt_thread_info.state = (thread->th.th_team_serialized)
1779 ? ompt_state_work_serial
1780 : ompt_state_work_parallel;
1781 taskdata->ompt_task_info.frame.exit_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1786 if (taskdata->td_flags.proxy != TASK_PROXY) {
1787 __kmp_task_start(gtid, task, current_task);
1793 if (UNLIKELY(__kmp_omp_cancellation)) {
1794 thread = __kmp_threads[gtid];
1795 kmp_team_t *this_team = thread->th.th_team;
1796 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
1797 if ((taskgroup && taskgroup->cancel_request) ||
1798 (this_team->t.t_cancel_request == cancel_parallel)) {
1799#if OMPT_SUPPORT && OMPT_OPTIONAL
1800 ompt_data_t *task_data;
1801 if (UNLIKELY(ompt_enabled.ompt_callback_cancel)) {
1802 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, NULL, NULL);
1803 ompt_callbacks.ompt_callback(ompt_callback_cancel)(
1805 ((taskgroup && taskgroup->cancel_request) ? ompt_cancel_taskgroup
1806 : ompt_cancel_parallel) |
1807 ompt_cancel_discarded_task,
1820 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
1821 taskdata->td_last_tied = current_task->td_last_tied;
1822 KMP_DEBUG_ASSERT(taskdata->td_last_tied);
1824#if KMP_STATS_ENABLED
1826 switch (KMP_GET_THREAD_STATE()) {
1827 case FORK_JOIN_BARRIER:
1828 KMP_PUSH_PARTITIONED_TIMER(OMP_task_join_bar);
1831 KMP_PUSH_PARTITIONED_TIMER(OMP_task_plain_bar);
1834 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskyield);
1837 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskwait);
1840 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskgroup);
1843 KMP_PUSH_PARTITIONED_TIMER(OMP_task_immediate);
1850 if (UNLIKELY(ompt_enabled.enabled))
1851 __ompt_task_start(task, current_task, gtid);
1853#if OMPT_SUPPORT && OMPT_OPTIONAL
1854 if (UNLIKELY(ompt_enabled.ompt_callback_dispatch &&
1855 taskdata->ompt_task_info.dispatch_chunk.iterations > 0)) {
1856 ompt_data_t instance = ompt_data_none;
1857 instance.ptr = &(taskdata->ompt_task_info.dispatch_chunk);
1858 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
1859 ompt_callbacks.ompt_callback(ompt_callback_dispatch)(
1860 &(team_info->parallel_data), &(taskdata->ompt_task_info.task_data),
1861 ompt_dispatch_taskloop_chunk, instance);
1862 taskdata->ompt_task_info.dispatch_chunk = {0, 0};
1867 if (ompd_state & OMPD_ENABLE_BP)
1868 ompd_bp_task_begin();
1871#if USE_ITT_BUILD && USE_ITT_NOTIFY
1872 kmp_uint64 cur_time;
1873 kmp_int32 kmp_itt_count_task =
1874 __kmp_forkjoin_frames_mode == 3 && !taskdata->td_flags.task_serial &&
1875 current_task->td_flags.tasktype == TASK_IMPLICIT;
1876 if (kmp_itt_count_task) {
1877 thread = __kmp_threads[gtid];
1879 if (thread->th.th_bar_arrive_time)
1880 cur_time = __itt_get_timestamp();
1882 kmp_itt_count_task = 0;
1884 KMP_FSYNC_ACQUIRED(taskdata);
1887#if ENABLE_LIBOMPTARGET
1888 if (taskdata->td_target_data.async_handle != NULL) {
1892 KMP_ASSERT(tgt_target_nowait_query);
1893 tgt_target_nowait_query(&taskdata->td_target_data.async_handle);
1896 if (task->routine != NULL) {
1897#ifdef KMP_GOMP_COMPAT
1898 if (taskdata->td_flags.native) {
1899 ((void (*)(
void *))(*(task->routine)))(task->shareds);
1903 (*(task->routine))(gtid, task);
1906 KMP_POP_PARTITIONED_TIMER();
1908#if USE_ITT_BUILD && USE_ITT_NOTIFY
1909 if (kmp_itt_count_task) {
1911 thread->th.th_bar_arrive_time += (__itt_get_timestamp() - cur_time);
1913 KMP_FSYNC_CANCEL(taskdata);
1914 KMP_FSYNC_RELEASING(taskdata->td_parent);
1919 if (ompd_state & OMPD_ENABLE_BP)
1924 if (taskdata->td_flags.proxy != TASK_PROXY) {
1926 if (UNLIKELY(ompt_enabled.enabled)) {
1927 thread->th.ompt_thread_info = oldInfo;
1928 if (taskdata->td_flags.tiedness == TASK_TIED) {
1929 taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
1931 __kmp_task_finish<true>(gtid, task, current_task);
1934 __kmp_task_finish<false>(gtid, task, current_task);
1937 else if (UNLIKELY(ompt_enabled.enabled && taskdata->td_flags.target)) {
1938 __ompt_task_finish(task, current_task, ompt_task_switch);
1944 (
"__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n",
1945 gtid, taskdata, current_task));
1959kmp_int32 __kmpc_omp_task_parts(
ident_t *loc_ref, kmp_int32 gtid,
1960 kmp_task_t *new_task) {
1961 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1963 KA_TRACE(10, (
"__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n", gtid,
1964 loc_ref, new_taskdata));
1967 kmp_taskdata_t *parent;
1968 if (UNLIKELY(ompt_enabled.enabled)) {
1969 parent = new_taskdata->td_parent;
1970 if (ompt_enabled.ompt_callback_task_create) {
1971 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1972 &(parent->ompt_task_info.task_data), &(parent->ompt_task_info.frame),
1973 &(new_taskdata->ompt_task_info.task_data),
1974 TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1975 OMPT_GET_RETURN_ADDRESS(0));
1983 if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1985 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1986 new_taskdata->td_flags.task_serial = 1;
1987 __kmp_invoke_task(gtid, new_task, current_task);
1992 (
"__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: "
1993 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
1994 gtid, loc_ref, new_taskdata));
1997 if (UNLIKELY(ompt_enabled.enabled)) {
1998 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1999 parent->ompt_task_info.frame.enter_frame_flags = OMPT_FRAME_FLAGS_RUNTIME;
2002 return TASK_CURRENT_NOT_QUEUED;
2016kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task,
2017 bool serialize_immediate) {
2018 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
2021 if (new_taskdata->is_taskgraph &&
2022 __kmp_tdg_is_recording(new_taskdata->tdg->tdg_status)) {
2023 kmp_tdg_info_t *tdg = new_taskdata->tdg;
2025 if (new_taskdata->td_tdg_task_id >= new_taskdata->tdg->map_size) {
2026 __kmp_acquire_bootstrap_lock(&tdg->graph_lock);
2029 if (new_taskdata->td_tdg_task_id >= tdg->map_size) {
2030 kmp_uint old_size = tdg->map_size;
2031 kmp_uint new_size = old_size * 2;
2032 kmp_node_info_t *old_record = tdg->record_map;
2033 kmp_node_info_t *new_record = (kmp_node_info_t *)__kmp_allocate(
2034 new_size *
sizeof(kmp_node_info_t));
2036 KMP_MEMCPY(new_record, old_record, old_size *
sizeof(kmp_node_info_t));
2037 tdg->record_map = new_record;
2039 __kmp_free(old_record);
2041 for (kmp_int i = old_size; i < new_size; i++) {
2042 kmp_int32 *successorsList = (kmp_int32 *)__kmp_allocate(
2043 __kmp_successors_size *
sizeof(kmp_int32));
2044 new_record[i].task =
nullptr;
2045 new_record[i].successors = successorsList;
2046 new_record[i].nsuccessors = 0;
2047 new_record[i].npredecessors = 0;
2048 new_record[i].successors_size = __kmp_successors_size;
2049 KMP_ATOMIC_ST_REL(&new_record[i].npredecessors_counter, 0);
2053 tdg->map_size = new_size;
2055 __kmp_release_bootstrap_lock(&tdg->graph_lock);
2058 if (tdg->record_map[new_taskdata->td_tdg_task_id].task ==
nullptr) {
2059 tdg->record_map[new_taskdata->td_tdg_task_id].task = new_task;
2060 tdg->record_map[new_taskdata->td_tdg_task_id].parent_task =
2061 new_taskdata->td_parent;
2062 KMP_ATOMIC_INC(&tdg->num_tasks);
2069 if (new_taskdata->td_flags.proxy == TASK_PROXY ||
2070 __kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
2072 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
2073 if (serialize_immediate)
2074 new_taskdata->td_flags.task_serial = 1;
2075 __kmp_invoke_task(gtid, new_task, current_task);
2076 }
else if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME &&
2077 __kmp_wpolicy_passive) {
2078 kmp_info_t *this_thr = __kmp_threads[gtid];
2079 kmp_team_t *team = this_thr->th.th_team;
2080 kmp_int32 nthreads = this_thr->th.th_team_nproc;
2081 for (
int i = 0; i < nthreads; ++i) {
2082 kmp_info_t *thread = team->t.t_threads[i];
2083 if (thread == this_thr)
2085 if (thread->th.th_sleep_loc != NULL) {
2086 __kmp_null_resume_wrapper(thread);
2091 return TASK_CURRENT_NOT_QUEUED;
2106kmp_int32 __kmpc_omp_task(
ident_t *loc_ref, kmp_int32 gtid,
2107 kmp_task_t *new_task) {
2109 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
2111#if KMP_DEBUG || OMPT_SUPPORT
2112 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
2114 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
2116 __kmp_assert_valid_gtid(gtid);
2119 kmp_taskdata_t *parent = NULL;
2120 if (UNLIKELY(ompt_enabled.enabled)) {
2121 if (!new_taskdata->td_flags.started) {
2122 OMPT_STORE_RETURN_ADDRESS(gtid);
2123 parent = new_taskdata->td_parent;
2124 if (!parent->ompt_task_info.frame.enter_frame.ptr) {
2125 parent->ompt_task_info.frame.enter_frame.ptr =
2126 OMPT_GET_FRAME_ADDRESS(0);
2128 if (ompt_enabled.ompt_callback_task_create) {
2129 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
2130 &(parent->ompt_task_info.task_data),
2131 &(parent->ompt_task_info.frame),
2132 &(new_taskdata->ompt_task_info.task_data),
2133 TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
2134 OMPT_LOAD_RETURN_ADDRESS(gtid));
2139 __ompt_task_finish(new_task,
2140 new_taskdata->ompt_task_info.scheduling_parent,
2142 new_taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
2147 res = __kmp_omp_task(gtid, new_task,
true);
2149 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
2150 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
2151 gtid, loc_ref, new_taskdata));
2153 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
2154 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
2173kmp_int32 __kmp_omp_taskloop_task(
ident_t *loc_ref, kmp_int32 gtid,
2174 kmp_task_t *new_task,
void *codeptr_ra) {
2176 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
2178#if KMP_DEBUG || OMPT_SUPPORT
2179 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
2181 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
2185 kmp_taskdata_t *parent = NULL;
2186 if (UNLIKELY(ompt_enabled.enabled && !new_taskdata->td_flags.started)) {
2187 parent = new_taskdata->td_parent;
2188 if (!parent->ompt_task_info.frame.enter_frame.ptr)
2189 parent->ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
2190 if (ompt_enabled.ompt_callback_task_create) {
2191 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
2192 &(parent->ompt_task_info.task_data), &(parent->ompt_task_info.frame),
2193 &(new_taskdata->ompt_task_info.task_data),
2194 TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0, codeptr_ra);
2199 res = __kmp_omp_task(gtid, new_task,
true);
2201 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
2202 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
2203 gtid, loc_ref, new_taskdata));
2205 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
2206 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
2213static kmp_int32 __kmpc_omp_taskwait_template(
ident_t *loc_ref, kmp_int32 gtid,
2214 void *frame_address,
2215 void *return_address) {
2216 kmp_taskdata_t *taskdata =
nullptr;
2218 int thread_finished = FALSE;
2219 KMP_SET_THREAD_STATE_BLOCK(TASKWAIT);
2221 KA_TRACE(10, (
"__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref));
2222 KMP_DEBUG_ASSERT(gtid >= 0);
2224 if (__kmp_tasking_mode != tskm_immediate_exec) {
2225 thread = __kmp_threads[gtid];
2226 taskdata = thread->th.th_current_task;
2228#if OMPT_SUPPORT && OMPT_OPTIONAL
2229 ompt_data_t *my_task_data;
2230 ompt_data_t *my_parallel_data;
2233 my_task_data = &(taskdata->ompt_task_info.task_data);
2234 my_parallel_data = OMPT_CUR_TEAM_DATA(thread);
2236 taskdata->ompt_task_info.frame.enter_frame.ptr = frame_address;
2238 if (ompt_enabled.ompt_callback_sync_region) {
2239 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2240 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
2241 my_task_data, return_address);
2244 if (ompt_enabled.ompt_callback_sync_region_wait) {
2245 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2246 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
2247 my_task_data, return_address);
2257 taskdata->td_taskwait_counter += 1;
2258 taskdata->td_taskwait_ident = loc_ref;
2259 taskdata->td_taskwait_thread = gtid + 1;
2262 void *itt_sync_obj = NULL;
2264 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2269 !taskdata->td_flags.team_serial && !taskdata->td_flags.final;
2271 must_wait = must_wait || (thread->th.th_task_team != NULL &&
2272 thread->th.th_task_team->tt.tt_found_proxy_tasks);
2276 (__kmp_enable_hidden_helper && thread->th.th_task_team != NULL &&
2277 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered);
2280 kmp_flag_32<false, false> flag(
2281 RCAST(std::atomic<kmp_uint32> *,
2282 &(taskdata->td_incomplete_child_tasks)),
2284 while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) != 0) {
2285 flag.execute_tasks(thread, gtid, FALSE,
2286 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2287 __kmp_task_stealing_constraint);
2291 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2292 KMP_FSYNC_ACQUIRED(taskdata);
2297 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2299#if OMPT_SUPPORT && OMPT_OPTIONAL
2301 if (ompt_enabled.ompt_callback_sync_region_wait) {
2302 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2303 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
2304 my_task_data, return_address);
2306 if (ompt_enabled.ompt_callback_sync_region) {
2307 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2308 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
2309 my_task_data, return_address);
2311 taskdata->ompt_task_info.frame.enter_frame = ompt_data_none;
2316 KA_TRACE(10, (
"__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, "
2317 "returning TASK_CURRENT_NOT_QUEUED\n",
2320 return TASK_CURRENT_NOT_QUEUED;
2323#if OMPT_SUPPORT && OMPT_OPTIONAL
2325static kmp_int32 __kmpc_omp_taskwait_ompt(
ident_t *loc_ref, kmp_int32 gtid,
2326 void *frame_address,
2327 void *return_address) {
2328 return __kmpc_omp_taskwait_template<true>(loc_ref, gtid, frame_address,
2335kmp_int32 __kmpc_omp_taskwait(
ident_t *loc_ref, kmp_int32 gtid) {
2336#if OMPT_SUPPORT && OMPT_OPTIONAL
2337 if (UNLIKELY(ompt_enabled.enabled)) {
2338 OMPT_STORE_RETURN_ADDRESS(gtid);
2339 return __kmpc_omp_taskwait_ompt(loc_ref, gtid, OMPT_GET_FRAME_ADDRESS(0),
2340 OMPT_LOAD_RETURN_ADDRESS(gtid));
2343 return __kmpc_omp_taskwait_template<false>(loc_ref, gtid, NULL, NULL);
2347kmp_int32 __kmpc_omp_taskyield(
ident_t *loc_ref, kmp_int32 gtid,
int end_part) {
2348 kmp_taskdata_t *taskdata = NULL;
2350 int thread_finished = FALSE;
2353 KMP_SET_THREAD_STATE_BLOCK(TASKYIELD);
2355 KA_TRACE(10, (
"__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n",
2356 gtid, loc_ref, end_part));
2357 __kmp_assert_valid_gtid(gtid);
2359 if (__kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel) {
2360 thread = __kmp_threads[gtid];
2361 taskdata = thread->th.th_current_task;
2368 taskdata->td_taskwait_counter += 1;
2369 taskdata->td_taskwait_ident = loc_ref;
2370 taskdata->td_taskwait_thread = gtid + 1;
2373 void *itt_sync_obj = NULL;
2375 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2378 if (!taskdata->td_flags.team_serial) {
2379 kmp_task_team_t *task_team = thread->th.th_task_team;
2380 if (task_team != NULL) {
2381 if (KMP_TASKING_ENABLED(task_team)) {
2383 if (UNLIKELY(ompt_enabled.enabled))
2384 thread->th.ompt_thread_info.ompt_task_yielded = 1;
2386 __kmp_execute_tasks_32(
2387 thread, gtid, (kmp_flag_32<> *)NULL, FALSE,
2388 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2389 __kmp_task_stealing_constraint);
2391 if (UNLIKELY(ompt_enabled.enabled))
2392 thread->th.ompt_thread_info.ompt_task_yielded = 0;
2398 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2403 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2406 KA_TRACE(10, (
"__kmpc_omp_taskyield(exit): T#%d task %p resuming, "
2407 "returning TASK_CURRENT_NOT_QUEUED\n",
2410 return TASK_CURRENT_NOT_QUEUED;
2431 unsigned reserved31 : 31;
2511template <
typename T>
2512void *__kmp_task_reduction_init(
int gtid,
int num, T *data) {
2513 __kmp_assert_valid_gtid(gtid);
2514 kmp_info_t *thread = __kmp_threads[gtid];
2515 kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup;
2516 kmp_uint32 nth = thread->th.th_team_nproc;
2520 KMP_ASSERT(tg != NULL);
2521 KMP_ASSERT(data != NULL);
2522 KMP_ASSERT(num > 0);
2523 if (nth == 1 && !__kmp_enable_hidden_helper) {
2524 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n",
2528 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, taskgroup %p, #items %d\n",
2532 for (
int i = 0; i < num; ++i) {
2533 size_t size = data[i].reduce_size - 1;
2535 size += CACHE_LINE - size % CACHE_LINE;
2536 KMP_ASSERT(data[i].reduce_comb != NULL);
2539 arr[i].
flags = data[i].flags;
2543 __kmp_assign_orig<T>(arr[i], data[i]);
2544 if (!arr[i].flags.lazy_priv) {
2547 arr[i].
reduce_pend = (
char *)(arr[i].reduce_priv) + nth * size;
2548 if (arr[i].reduce_init != NULL) {
2550 for (
size_t j = 0; j < nth; ++j) {
2551 __kmp_call_init<T>(arr[i], j * size);
2558 arr[i].
reduce_priv = __kmp_allocate(nth *
sizeof(
void *));
2561 tg->reduce_data = (
void *)arr;
2562 tg->reduce_num_data = num;
2582 kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_idx);
2583 if (tdg && __kmp_tdg_is_recording(tdg->tdg_status)) {
2584 kmp_tdg_info_t *this_tdg = __kmp_global_tdgs[__kmp_curr_tdg_idx];
2585 this_tdg->rec_taskred_data =
2587 this_tdg->rec_num_taskred = num;
2588 KMP_MEMCPY(this_tdg->rec_taskred_data, data,
2609 kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_idx);
2610 if (tdg && __kmp_tdg_is_recording(tdg->tdg_status)) {
2611 kmp_tdg_info_t *this_tdg = __kmp_global_tdgs[__kmp_curr_tdg_idx];
2612 this_tdg->rec_taskred_data =
2614 this_tdg->rec_num_taskred = num;
2615 KMP_MEMCPY(this_tdg->rec_taskred_data, data,
2623template <
typename T>
2624void __kmp_task_reduction_init_copy(kmp_info_t *thr,
int num, T *data,
2625 kmp_taskgroup_t *tg,
void *reduce_data) {
2627 KA_TRACE(20, (
"__kmp_task_reduction_init_copy: Th %p, init taskgroup %p,"
2629 thr, tg, reduce_data));
2634 for (
int i = 0; i < num; ++i) {
2637 tg->reduce_data = (
void *)arr;
2638 tg->reduce_num_data = num;
2651 __kmp_assert_valid_gtid(gtid);
2652 kmp_info_t *thread = __kmp_threads[gtid];
2653 kmp_int32 nth = thread->th.th_team_nproc;
2657 kmp_taskgroup_t *tg = (kmp_taskgroup_t *)tskgrp;
2659 tg = thread->th.th_current_task->td_taskgroup;
2660 KMP_ASSERT(tg != NULL);
2663 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
2666 if ((thread->th.th_current_task->is_taskgraph) &&
2667 (!__kmp_tdg_is_recording(
2668 __kmp_global_tdgs[__kmp_curr_tdg_idx]->tdg_status))) {
2669 tg = thread->th.th_current_task->td_taskgroup;
2670 KMP_ASSERT(tg != NULL);
2671 KMP_ASSERT(tg->reduce_data != NULL);
2673 num = tg->reduce_num_data;
2677 KMP_ASSERT(data != NULL);
2678 while (tg != NULL) {
2680 num = tg->reduce_num_data;
2681 for (
int i = 0; i < num; ++i) {
2682 if (!arr[i].flags.lazy_priv) {
2683 if (data == arr[i].reduce_shar ||
2684 (data >= arr[i].reduce_priv && data < arr[i].reduce_pend))
2685 return (
char *)(arr[i].
reduce_priv) + tid * arr[i].reduce_size;
2688 void **p_priv = (
void **)(arr[i].reduce_priv);
2689 if (data == arr[i].reduce_shar)
2692 for (
int j = 0; j < nth; ++j)
2693 if (data == p_priv[j])
2697 if (p_priv[tid] == NULL) {
2699 p_priv[tid] = __kmp_allocate(arr[i].reduce_size);
2700 if (arr[i].reduce_init != NULL) {
2701 if (arr[i].reduce_orig != NULL) {
2703 p_priv[tid], arr[i].reduce_orig);
2705 ((void (*)(
void *))arr[i].
reduce_init)(p_priv[tid]);
2712 KMP_ASSERT(tg->parent);
2715 KMP_ASSERT2(0,
"Unknown task reduction item");
2721static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg) {
2722 kmp_int32 nth = th->th.th_team_nproc;
2725 __kmp_enable_hidden_helper);
2728 kmp_int32 num = tg->reduce_num_data;
2729 for (
int i = 0; i < num; ++i) {
2731 void (*f_fini)(
void *) = (
void (*)(
void *))(arr[i].
reduce_fini);
2732 void (*f_comb)(
void *,
void *) =
2734 if (!arr[i].flags.lazy_priv) {
2737 for (
int j = 0; j < nth; ++j) {
2738 void *priv_data = (
char *)pr_data + j * size;
2739 f_comb(sh_data, priv_data);
2744 void **pr_data = (
void **)(arr[i].reduce_priv);
2745 for (
int j = 0; j < nth; ++j) {
2746 if (pr_data[j] != NULL) {
2747 f_comb(sh_data, pr_data[j]);
2750 __kmp_free(pr_data[j]);
2754 __kmp_free(arr[i].reduce_priv);
2756 __kmp_thread_free(th, arr);
2757 tg->reduce_data = NULL;
2758 tg->reduce_num_data = 0;
2764static void __kmp_task_reduction_clean(kmp_info_t *th, kmp_taskgroup_t *tg) {
2765 __kmp_thread_free(th, tg->reduce_data);
2766 tg->reduce_data = NULL;
2767 tg->reduce_num_data = 0;
2770template <
typename T>
2771void *__kmp_task_reduction_modifier_init(
ident_t *loc,
int gtid,
int is_ws,
2773 __kmp_assert_valid_gtid(gtid);
2774 kmp_info_t *thr = __kmp_threads[gtid];
2775 kmp_int32 nth = thr->th.th_team_nproc;
2776 __kmpc_taskgroup(loc, gtid);
2779 (
"__kmpc_reduction_modifier_init: T#%d, tg %p, exiting nth=1\n",
2780 gtid, thr->th.th_current_task->td_taskgroup));
2781 return (
void *)thr->th.th_current_task->td_taskgroup;
2783 kmp_team_t *team = thr->th.th_team;
2785 kmp_taskgroup_t *tg;
2786 reduce_data = KMP_ATOMIC_LD_RLX(&team->t.t_tg_reduce_data[is_ws]);
2787 if (reduce_data == NULL &&
2788 __kmp_atomic_compare_store(&team->t.t_tg_reduce_data[is_ws], reduce_data,
2791 KMP_DEBUG_ASSERT(reduce_data == NULL);
2793 tg = (kmp_taskgroup_t *)__kmp_task_reduction_init<T>(gtid, num, data);
2797 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[0]) == 0);
2798 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[1]) == 0);
2799 KMP_ATOMIC_ST_REL(&team->t.t_tg_reduce_data[is_ws], reduce_data);
2802 (reduce_data = KMP_ATOMIC_LD_ACQ(&team->t.t_tg_reduce_data[is_ws])) ==
2806 KMP_DEBUG_ASSERT(reduce_data > (
void *)1);
2807 tg = thr->th.th_current_task->td_taskgroup;
2808 __kmp_task_reduction_init_copy<T>(thr, num, data, tg, reduce_data);
2830 int num,
void *data) {
2831 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2851 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2864 __kmpc_end_taskgroup(loc, gtid);
2868void __kmpc_taskgroup(
ident_t *loc,
int gtid) {
2869 __kmp_assert_valid_gtid(gtid);
2870 kmp_info_t *thread = __kmp_threads[gtid];
2871 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2872 kmp_taskgroup_t *tg_new =
2873 (kmp_taskgroup_t *)__kmp_thread_malloc(thread,
sizeof(kmp_taskgroup_t));
2874 KA_TRACE(10, (
"__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new));
2875 KMP_ATOMIC_ST_RLX(&tg_new->count, 0);
2876 KMP_ATOMIC_ST_RLX(&tg_new->cancel_request, cancel_noreq);
2877 tg_new->parent = taskdata->td_taskgroup;
2878 tg_new->reduce_data = NULL;
2879 tg_new->reduce_num_data = 0;
2880 tg_new->gomp_data = NULL;
2881 taskdata->td_taskgroup = tg_new;
2883#if OMPT_SUPPORT && OMPT_OPTIONAL
2884 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2885 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2887 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2888 kmp_team_t *team = thread->th.th_team;
2889 ompt_data_t my_task_data = taskdata->ompt_task_info.task_data;
2891 ompt_data_t my_parallel_data = team->t.ompt_team_info.parallel_data;
2893 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2894 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2895 &(my_task_data), codeptr);
2902void __kmpc_end_taskgroup(
ident_t *loc,
int gtid) {
2903 __kmp_assert_valid_gtid(gtid);
2904 kmp_info_t *thread = __kmp_threads[gtid];
2905 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2906 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
2907 int thread_finished = FALSE;
2909#if OMPT_SUPPORT && OMPT_OPTIONAL
2911 ompt_data_t my_task_data;
2912 ompt_data_t my_parallel_data;
2913 void *codeptr =
nullptr;
2914 if (UNLIKELY(ompt_enabled.enabled)) {
2915 team = thread->th.th_team;
2916 my_task_data = taskdata->ompt_task_info.task_data;
2918 my_parallel_data = team->t.ompt_team_info.parallel_data;
2919 codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2921 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2925 KA_TRACE(10, (
"__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc));
2926 KMP_DEBUG_ASSERT(taskgroup != NULL);
2927 KMP_SET_THREAD_STATE_BLOCK(TASKGROUP);
2929 if (__kmp_tasking_mode != tskm_immediate_exec) {
2931 taskdata->td_taskwait_counter += 1;
2932 taskdata->td_taskwait_ident = loc;
2933 taskdata->td_taskwait_thread = gtid + 1;
2937 void *itt_sync_obj = NULL;
2939 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2943#if OMPT_SUPPORT && OMPT_OPTIONAL
2944 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2945 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2946 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2947 &(my_task_data), codeptr);
2951 if (!taskdata->td_flags.team_serial ||
2952 (thread->th.th_task_team != NULL &&
2953 (thread->th.th_task_team->tt.tt_found_proxy_tasks ||
2954 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered))) {
2955 kmp_flag_32<false, false> flag(
2956 RCAST(std::atomic<kmp_uint32> *, &(taskgroup->count)), 0U);
2957 while (KMP_ATOMIC_LD_ACQ(&taskgroup->count) != 0) {
2958 flag.execute_tasks(thread, gtid, FALSE,
2959 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2960 __kmp_task_stealing_constraint);
2963 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2965#if OMPT_SUPPORT && OMPT_OPTIONAL
2966 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2967 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2968 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2969 &(my_task_data), codeptr);
2974 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2975 KMP_FSYNC_ACQUIRED(taskdata);
2978 KMP_DEBUG_ASSERT(taskgroup->count == 0);
2980 if (taskgroup->reduce_data != NULL &&
2981 !taskgroup->gomp_data) {
2984 kmp_team_t *t = thread->th.th_team;
2988 if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[0])) != NULL &&
2991 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[0]);
2992 if (cnt == thread->th.th_team_nproc - 1) {
2995 __kmp_task_reduction_fini(thread, taskgroup);
2998 __kmp_thread_free(thread, reduce_data);
2999 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[0], NULL);
3000 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[0], 0);
3004 __kmp_task_reduction_clean(thread, taskgroup);
3006 }
else if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[1])) !=
3010 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[1]);
3011 if (cnt == thread->th.th_team_nproc - 1) {
3013 __kmp_task_reduction_fini(thread, taskgroup);
3016 __kmp_thread_free(thread, reduce_data);
3017 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[1], NULL);
3018 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[1], 0);
3022 __kmp_task_reduction_clean(thread, taskgroup);
3026 __kmp_task_reduction_fini(thread, taskgroup);
3030 taskdata->td_taskgroup = taskgroup->parent;
3031 __kmp_thread_free(thread, taskgroup);
3033 KA_TRACE(10, (
"__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n",
3036#if OMPT_SUPPORT && OMPT_OPTIONAL
3037 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
3038 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
3039 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
3040 &(my_task_data), codeptr);
3045static kmp_task_t *__kmp_get_priority_task(kmp_int32 gtid,
3046 kmp_task_team_t *task_team,
3047 kmp_int32 is_constrained) {
3048 kmp_task_t *task = NULL;
3049 kmp_taskdata_t *taskdata;
3050 kmp_taskdata_t *current;
3051 kmp_thread_data_t *thread_data;
3052 int ntasks = task_team->tt.tt_num_task_pri;
3055 20, (
"__kmp_get_priority_task(exit #1): T#%d No tasks to get\n", gtid));
3060 if (__kmp_atomic_compare_store(&task_team->tt.tt_num_task_pri, ntasks,
3063 ntasks = task_team->tt.tt_num_task_pri;
3064 }
while (ntasks > 0);
3066 KA_TRACE(20, (
"__kmp_get_priority_task(exit #2): T#%d No tasks to get\n",
3072 kmp_task_pri_t *list = task_team->tt.tt_task_pri_list;
3074 KMP_ASSERT(list != NULL);
3075 thread_data = &list->td;
3076 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3077 deque_ntasks = thread_data->td.td_deque_ntasks;
3078 if (deque_ntasks == 0) {
3079 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3080 KA_TRACE(20, (
"__kmp_get_priority_task: T#%d No tasks to get from %p\n",
3081 __kmp_get_gtid(), thread_data));
3084 }
while (deque_ntasks == 0);
3085 KMP_DEBUG_ASSERT(deque_ntasks);
3086 int target = thread_data->td.td_deque_head;
3087 current = __kmp_threads[gtid]->th.th_current_task;
3088 taskdata = thread_data->td.td_deque[target];
3089 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3091 thread_data->td.td_deque_head =
3092 (target + 1) & TASK_DEQUE_MASK(thread_data->td);
3094 if (!task_team->tt.tt_untied_task_encountered) {
3096 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3097 KA_TRACE(20, (
"__kmp_get_priority_task(exit #3): T#%d could not get task "
3098 "from %p: task_team=%p ntasks=%d head=%u tail=%u\n",
3099 gtid, thread_data, task_team, deque_ntasks, target,
3100 thread_data->td.td_deque_tail));
3101 task_team->tt.tt_num_task_pri++;
3107 for (i = 1; i < deque_ntasks; ++i) {
3108 target = (target + 1) & TASK_DEQUE_MASK(thread_data->td);
3109 taskdata = thread_data->td.td_deque[target];
3110 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3116 if (taskdata == NULL) {
3118 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3120 10, (
"__kmp_get_priority_task(exit #4): T#%d could not get task from "
3121 "%p: task_team=%p ntasks=%d head=%u tail=%u\n",
3122 gtid, thread_data, task_team, deque_ntasks,
3123 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3124 task_team->tt.tt_num_task_pri++;
3128 for (i = i + 1; i < deque_ntasks; ++i) {
3130 target = (target + 1) & TASK_DEQUE_MASK(thread_data->td);
3131 thread_data->td.td_deque[prev] = thread_data->td.td_deque[target];
3135 thread_data->td.td_deque_tail ==
3136 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(thread_data->td)));
3137 thread_data->td.td_deque_tail = target;
3139 thread_data->td.td_deque_ntasks = deque_ntasks - 1;
3140 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3141 task = KMP_TASKDATA_TO_TASK(taskdata);
3146static kmp_task_t *__kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid,
3147 kmp_task_team_t *task_team,
3148 kmp_int32 is_constrained) {
3150 kmp_taskdata_t *taskdata;
3151 kmp_thread_data_t *thread_data;
3154 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3155 KMP_DEBUG_ASSERT(task_team->tt.tt_threads_data !=
3158 thread_data = &task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
3160 KA_TRACE(10, (
"__kmp_remove_my_task(enter): T#%d ntasks=%d head=%u tail=%u\n",
3161 gtid, thread_data->td.td_deque_ntasks,
3162 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3164 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
3166 (
"__kmp_remove_my_task(exit #1): T#%d No tasks to remove: "
3167 "ntasks=%d head=%u tail=%u\n",
3168 gtid, thread_data->td.td_deque_ntasks,
3169 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3173 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3175 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
3176 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3178 (
"__kmp_remove_my_task(exit #2): T#%d No tasks to remove: "
3179 "ntasks=%d head=%u tail=%u\n",
3180 gtid, thread_data->td.td_deque_ntasks,
3181 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3185 tail = (thread_data->td.td_deque_tail - 1) &
3186 TASK_DEQUE_MASK(thread_data->td);
3187 taskdata = thread_data->td.td_deque[tail];
3189 if (!__kmp_task_is_allowed(gtid, is_constrained, taskdata,
3190 thread->th.th_current_task)) {
3192 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3194 (
"__kmp_remove_my_task(exit #3): T#%d TSC blocks tail task: "
3195 "ntasks=%d head=%u tail=%u\n",
3196 gtid, thread_data->td.td_deque_ntasks,
3197 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3201 thread_data->td.td_deque_tail = tail;
3202 TCW_4(thread_data->td.td_deque_ntasks, thread_data->td.td_deque_ntasks - 1);
3204 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3206 KA_TRACE(10, (
"__kmp_remove_my_task(exit #4): T#%d task %p removed: "
3207 "ntasks=%d head=%u tail=%u\n",
3208 gtid, taskdata, thread_data->td.td_deque_ntasks,
3209 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3211 task = KMP_TASKDATA_TO_TASK(taskdata);
3218static kmp_task_t *__kmp_steal_task(kmp_int32 victim_tid, kmp_int32 gtid,
3219 kmp_task_team_t *task_team,
3220 std::atomic<kmp_int32> *unfinished_threads,
3221 int *thread_finished,
3222 kmp_int32 is_constrained) {
3224 kmp_taskdata_t *taskdata;
3225 kmp_taskdata_t *current;
3226 kmp_thread_data_t *victim_td, *threads_data;
3228 kmp_info_t *victim_thr;
3230 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3232 threads_data = task_team->tt.tt_threads_data;
3233 KMP_DEBUG_ASSERT(threads_data != NULL);
3234 KMP_DEBUG_ASSERT(victim_tid >= 0);
3235 KMP_DEBUG_ASSERT(victim_tid < task_team->tt.tt_max_threads);
3237 victim_td = &threads_data[victim_tid];
3238 victim_thr = victim_td->td.td_thr;
3241 KA_TRACE(10, (
"__kmp_steal_task(enter): T#%d try to steal from T#%d: "
3242 "task_team=%p ntasks=%d head=%u tail=%u\n",
3243 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
3244 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
3245 victim_td->td.td_deque_tail));
3247 if (TCR_4(victim_td->td.td_deque_ntasks) == 0) {
3248 KA_TRACE(10, (
"__kmp_steal_task(exit #1): T#%d could not steal from T#%d: "
3249 "task_team=%p ntasks=%d head=%u tail=%u\n",
3250 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
3251 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
3252 victim_td->td.td_deque_tail));
3256 __kmp_acquire_bootstrap_lock(&victim_td->td.td_deque_lock);
3258 int ntasks = TCR_4(victim_td->td.td_deque_ntasks);
3261 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3262 KA_TRACE(10, (
"__kmp_steal_task(exit #2): T#%d could not steal from T#%d: "
3263 "task_team=%p ntasks=%d head=%u tail=%u\n",
3264 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3265 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3269 KMP_DEBUG_ASSERT(victim_td->td.td_deque != NULL);
3270 current = __kmp_threads[gtid]->th.th_current_task;
3271 taskdata = victim_td->td.td_deque[victim_td->td.td_deque_head];
3272 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3274 victim_td->td.td_deque_head =
3275 (victim_td->td.td_deque_head + 1) & TASK_DEQUE_MASK(victim_td->td);
3277 if (!task_team->tt.tt_untied_task_encountered) {
3279 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3280 KA_TRACE(10, (
"__kmp_steal_task(exit #3): T#%d could not steal from "
3281 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
3282 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3283 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3288 target = victim_td->td.td_deque_head;
3290 for (i = 1; i < ntasks; ++i) {
3291 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
3292 taskdata = victim_td->td.td_deque[target];
3293 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3299 if (taskdata == NULL) {
3301 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3302 KA_TRACE(10, (
"__kmp_steal_task(exit #4): T#%d could not steal from "
3303 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
3304 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3305 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3309 for (i = i + 1; i < ntasks; ++i) {
3311 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
3312 victim_td->td.td_deque[prev] = victim_td->td.td_deque[target];
3316 victim_td->td.td_deque_tail ==
3317 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(victim_td->td)));
3318 victim_td->td.td_deque_tail = target;
3320 if (*thread_finished) {
3327 KMP_ATOMIC_INC(unfinished_threads);
3330 (
"__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n",
3331 gtid, count + 1, task_team));
3332 *thread_finished = FALSE;
3334 TCW_4(victim_td->td.td_deque_ntasks, ntasks - 1);
3336 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3340 (
"__kmp_steal_task(exit #5): T#%d stole task %p from T#%d: "
3341 "task_team=%p ntasks=%d head=%u tail=%u\n",
3342 gtid, taskdata, __kmp_gtid_from_thread(victim_thr), task_team,
3343 ntasks, victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3345 task = KMP_TASKDATA_TO_TASK(taskdata);
3359static inline int __kmp_execute_tasks_template(
3360 kmp_info_t *thread, kmp_int32 gtid, C *flag,
int final_spin,
3361 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3362 kmp_int32 is_constrained) {
3363 kmp_task_team_t *task_team = thread->th.th_task_team;
3364 kmp_thread_data_t *threads_data;
3366 kmp_info_t *other_thread;
3367 kmp_taskdata_t *current_task = thread->th.th_current_task;
3368 std::atomic<kmp_int32> *unfinished_threads;
3369 kmp_int32 nthreads, victim_tid = -2, use_own_tasks = 1, new_victim = 0,
3370 tid = thread->th.th_info.ds.ds_tid;
3372 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3373 KMP_DEBUG_ASSERT(thread == __kmp_threads[gtid]);
3375 if (task_team == NULL || current_task == NULL)
3378 KA_TRACE(15, (
"__kmp_execute_tasks_template(enter): T#%d final_spin=%d "
3379 "*thread_finished=%d\n",
3380 gtid, final_spin, *thread_finished));
3382 thread->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
3383 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
3385 KMP_DEBUG_ASSERT(threads_data != NULL);
3387 nthreads = task_team->tt.tt_nproc;
3388 unfinished_threads = &(task_team->tt.tt_unfinished_threads);
3389 KMP_DEBUG_ASSERT(*unfinished_threads >= 0);
3395 if (task_team->tt.tt_num_task_pri) {
3396 task = __kmp_get_priority_task(gtid, task_team, is_constrained);
3398 if (task == NULL && use_own_tasks) {
3399 task = __kmp_remove_my_task(thread, gtid, task_team, is_constrained);
3401 if ((task == NULL) && (nthreads > 1)) {
3405 if (victim_tid == -2) {
3406 victim_tid = threads_data[tid].td.td_deque_last_stolen;
3409 other_thread = threads_data[victim_tid].td.td_thr;
3411 if (victim_tid != -1) {
3413 }
else if (!new_victim) {
3419 victim_tid = __kmp_get_random(thread) % (nthreads - 1);
3420 if (victim_tid >= tid) {
3424 other_thread = threads_data[victim_tid].td.td_thr;
3434 if ((__kmp_tasking_mode == tskm_task_teams) &&
3435 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) &&
3436 (TCR_PTR(CCAST(
void *, other_thread->th.th_sleep_loc)) !=
3439 __kmp_null_resume_wrapper(other_thread);
3453 __kmp_steal_task(victim_tid, gtid, task_team, unfinished_threads,
3454 thread_finished, is_constrained);
3457 if (threads_data[tid].td.td_deque_last_stolen != victim_tid) {
3458 threads_data[tid].td.td_deque_last_stolen = victim_tid;
3465 KMP_CHECK_UPDATE(threads_data[tid].td.td_deque_last_stolen, -1);
3474#if USE_ITT_BUILD && USE_ITT_NOTIFY
3475 if (__itt_sync_create_ptr || KMP_ITT_DEBUG) {
3476 if (itt_sync_obj == NULL) {
3478 itt_sync_obj = __kmp_itt_barrier_object(gtid, bs_forkjoin_barrier);
3480 __kmp_itt_task_starting(itt_sync_obj);
3483 __kmp_invoke_task(gtid, task, current_task);
3485 if (itt_sync_obj != NULL)
3486 __kmp_itt_task_finished(itt_sync_obj);
3493 if (flag == NULL || (!final_spin && flag->done_check())) {
3496 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3500 if (thread->th.th_task_team == NULL) {
3503 KMP_YIELD(__kmp_library == library_throughput);
3506 if (!use_own_tasks && TCR_4(threads_data[tid].td.td_deque_ntasks) != 0) {
3507 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d stolen task spawned "
3508 "other tasks, restart\n",
3519 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks) == 0) {
3523 if (!*thread_finished) {
3525 kmp_int32 count = -1 +
3527 KMP_ATOMIC_DEC(unfinished_threads);
3528 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d dec "
3529 "unfinished_threads to %d task_team=%p\n",
3530 gtid, count, task_team));
3531 *thread_finished = TRUE;
3539 if (flag != NULL && flag->done_check()) {
3542 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3550 if (thread->th.th_task_team == NULL) {
3552 (
"__kmp_execute_tasks_template: T#%d no more tasks\n", gtid));
3561 if (flag == NULL || (!final_spin && flag->done_check())) {
3563 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3570 if (nthreads == 1 &&
3571 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks))
3575 (
"__kmp_execute_tasks_template: T#%d can't find work\n", gtid));
3581template <
bool C,
bool S>
3582int __kmp_execute_tasks_32(
3583 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32<C, S> *flag,
int final_spin,
3584 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3585 kmp_int32 is_constrained) {
3586 return __kmp_execute_tasks_template(
3587 thread, gtid, flag, final_spin,
3588 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3591template <
bool C,
bool S>
3592int __kmp_execute_tasks_64(
3593 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64<C, S> *flag,
int final_spin,
3594 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3595 kmp_int32 is_constrained) {
3596 return __kmp_execute_tasks_template(
3597 thread, gtid, flag, final_spin,
3598 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3601template <
bool C,
bool S>
3602int __kmp_atomic_execute_tasks_64(
3603 kmp_info_t *thread, kmp_int32 gtid, kmp_atomic_flag_64<C, S> *flag,
3604 int final_spin,
int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3605 kmp_int32 is_constrained) {
3606 return __kmp_execute_tasks_template(
3607 thread, gtid, flag, final_spin,
3608 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3611int __kmp_execute_tasks_oncore(
3612 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag,
int final_spin,
3613 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3614 kmp_int32 is_constrained) {
3615 return __kmp_execute_tasks_template(
3616 thread, gtid, flag, final_spin,
3617 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3621__kmp_execute_tasks_32<false, false>(kmp_info_t *, kmp_int32,
3622 kmp_flag_32<false, false> *,
int,
3623 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3625template int __kmp_execute_tasks_64<false, true>(kmp_info_t *, kmp_int32,
3626 kmp_flag_64<false, true> *,
3628 int *USE_ITT_BUILD_ARG(
void *),
3631template int __kmp_execute_tasks_64<true, false>(kmp_info_t *, kmp_int32,
3632 kmp_flag_64<true, false> *,
3634 int *USE_ITT_BUILD_ARG(
void *),
3637template int __kmp_atomic_execute_tasks_64<false, true>(
3638 kmp_info_t *, kmp_int32, kmp_atomic_flag_64<false, true> *,
int,
3639 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3641template int __kmp_atomic_execute_tasks_64<true, false>(
3642 kmp_info_t *, kmp_int32, kmp_atomic_flag_64<true, false> *,
int,
3643 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3648static void __kmp_enable_tasking(kmp_task_team_t *task_team,
3649 kmp_info_t *this_thr) {
3650 kmp_thread_data_t *threads_data;
3651 int nthreads, i, is_init_thread;
3653 KA_TRACE(10, (
"__kmp_enable_tasking(enter): T#%d\n",
3654 __kmp_gtid_from_thread(this_thr)));
3656 KMP_DEBUG_ASSERT(task_team != NULL);
3657 KMP_DEBUG_ASSERT(this_thr->th.th_team != NULL);
3659 nthreads = task_team->tt.tt_nproc;
3660 KMP_DEBUG_ASSERT(nthreads > 0);
3661 KMP_DEBUG_ASSERT(nthreads == this_thr->th.th_team->t.t_nproc);
3664 is_init_thread = __kmp_realloc_task_threads_data(this_thr, task_team);
3666 if (!is_init_thread) {
3670 (
"__kmp_enable_tasking(exit): T#%d: threads array already set up.\n",
3671 __kmp_gtid_from_thread(this_thr)));
3674 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
3675 KMP_DEBUG_ASSERT(threads_data != NULL);
3677 if (__kmp_tasking_mode == tskm_task_teams &&
3678 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME)) {
3682 for (i = 0; i < nthreads; i++) {
3684 kmp_info_t *thread = threads_data[i].td.td_thr;
3686 if (i == this_thr->th.th_info.ds.ds_tid) {
3695 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3697 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d waking up thread T#%d\n",
3698 __kmp_gtid_from_thread(this_thr),
3699 __kmp_gtid_from_thread(thread)));
3700 __kmp_null_resume_wrapper(thread);
3702 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d don't wake up thread T#%d\n",
3703 __kmp_gtid_from_thread(this_thr),
3704 __kmp_gtid_from_thread(thread)));
3709 KA_TRACE(10, (
"__kmp_enable_tasking(exit): T#%d\n",
3710 __kmp_gtid_from_thread(this_thr)));
3743static kmp_task_team_t *__kmp_free_task_teams =
3746kmp_bootstrap_lock_t __kmp_task_team_lock =
3747 KMP_BOOTSTRAP_LOCK_INITIALIZER(__kmp_task_team_lock);
3754static void __kmp_alloc_task_deque(kmp_info_t *thread,
3755 kmp_thread_data_t *thread_data) {
3756 __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
3757 KMP_DEBUG_ASSERT(thread_data->td.td_deque == NULL);
3760 thread_data->td.td_deque_last_stolen = -1;
3762 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == 0);
3763 KMP_DEBUG_ASSERT(thread_data->td.td_deque_head == 0);
3764 KMP_DEBUG_ASSERT(thread_data->td.td_deque_tail == 0);
3768 (
"__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n",
3769 __kmp_gtid_from_thread(thread), INITIAL_TASK_DEQUE_SIZE, thread_data));
3773 thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
3774 INITIAL_TASK_DEQUE_SIZE *
sizeof(kmp_taskdata_t *));
3775 thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
3781static void __kmp_free_task_deque(kmp_thread_data_t *thread_data) {
3782 if (thread_data->td.td_deque != NULL) {
3783 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3784 TCW_4(thread_data->td.td_deque_ntasks, 0);
3785 __kmp_free(thread_data->td.td_deque);
3786 thread_data->td.td_deque = NULL;
3787 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3790#ifdef BUILD_TIED_TASK_STACK
3792 if (thread_data->td.td_susp_tied_tasks.ts_entries != TASK_STACK_EMPTY) {
3793 __kmp_free_task_stack(__kmp_thread_from_gtid(gtid), thread_data);
3805static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
3806 kmp_task_team_t *task_team) {
3807 kmp_thread_data_t **threads_data_p;
3808 kmp_int32 nthreads, maxthreads;
3809 int is_init_thread = FALSE;
3811 if (TCR_4(task_team->tt.tt_found_tasks)) {
3816 threads_data_p = &task_team->tt.tt_threads_data;
3817 nthreads = task_team->tt.tt_nproc;
3818 maxthreads = task_team->tt.tt_max_threads;
3823 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3825 if (!TCR_4(task_team->tt.tt_found_tasks)) {
3827 kmp_team_t *team = thread->th.th_team;
3830 is_init_thread = TRUE;
3831 if (maxthreads < nthreads) {
3833 if (*threads_data_p != NULL) {
3834 kmp_thread_data_t *old_data = *threads_data_p;
3835 kmp_thread_data_t *new_data = NULL;
3839 (
"__kmp_realloc_task_threads_data: T#%d reallocating "
3840 "threads data for task_team %p, new_size = %d, old_size = %d\n",
3841 __kmp_gtid_from_thread(thread), task_team, nthreads, maxthreads));
3846 new_data = (kmp_thread_data_t *)__kmp_allocate(
3847 nthreads *
sizeof(kmp_thread_data_t));
3849 KMP_MEMCPY_S((
void *)new_data, nthreads *
sizeof(kmp_thread_data_t),
3850 (
void *)old_data, maxthreads *
sizeof(kmp_thread_data_t));
3852#ifdef BUILD_TIED_TASK_STACK
3854 for (i = maxthreads; i < nthreads; i++) {
3855 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3856 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3860 (*threads_data_p) = new_data;
3861 __kmp_free(old_data);
3863 KE_TRACE(10, (
"__kmp_realloc_task_threads_data: T#%d allocating "
3864 "threads data for task_team %p, size = %d\n",
3865 __kmp_gtid_from_thread(thread), task_team, nthreads));
3869 *threads_data_p = (kmp_thread_data_t *)__kmp_allocate(
3870 nthreads *
sizeof(kmp_thread_data_t));
3871#ifdef BUILD_TIED_TASK_STACK
3873 for (i = 0; i < nthreads; i++) {
3874 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3875 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3879 task_team->tt.tt_max_threads = nthreads;
3882 KMP_DEBUG_ASSERT(*threads_data_p != NULL);
3886 for (i = 0; i < nthreads; i++) {
3887 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3888 thread_data->td.td_thr = team->t.t_threads[i];
3890 if (thread_data->td.td_deque_last_stolen >= nthreads) {
3894 thread_data->td.td_deque_last_stolen = -1;
3899 TCW_SYNC_4(task_team->tt.tt_found_tasks, TRUE);
3902 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3903 return is_init_thread;
3909static void __kmp_free_task_threads_data(kmp_task_team_t *task_team) {
3910 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3911 if (task_team->tt.tt_threads_data != NULL) {
3913 for (i = 0; i < task_team->tt.tt_max_threads; i++) {
3914 __kmp_free_task_deque(&task_team->tt.tt_threads_data[i]);
3916 __kmp_free(task_team->tt.tt_threads_data);
3917 task_team->tt.tt_threads_data = NULL;
3919 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3925static void __kmp_free_task_pri_list(kmp_task_team_t *task_team) {
3926 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
3927 if (task_team->tt.tt_task_pri_list != NULL) {
3928 kmp_task_pri_t *list = task_team->tt.tt_task_pri_list;
3929 while (list != NULL) {
3930 kmp_task_pri_t *next = list->next;
3931 __kmp_free_task_deque(&list->td);
3935 task_team->tt.tt_task_pri_list = NULL;
3937 __kmp_release_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
3940static inline void __kmp_task_team_init(kmp_task_team_t *task_team,
3942 int team_nth = team->t.t_nproc;
3944 if (!task_team->tt.tt_active || team_nth != task_team->tt.tt_nproc) {
3945 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3946 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3947 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
3948 TCW_4(task_team->tt.tt_nproc, team_nth);
3949 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads, team_nth);
3950 TCW_4(task_team->tt.tt_active, TRUE);
3958static kmp_task_team_t *__kmp_allocate_task_team(kmp_info_t *thread,
3960 kmp_task_team_t *task_team = NULL;
3962 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d entering; team = %p\n",
3963 (thread ? __kmp_gtid_from_thread(thread) : -1), team));
3965 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3967 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3968 if (__kmp_free_task_teams != NULL) {
3969 task_team = __kmp_free_task_teams;
3970 TCW_PTR(__kmp_free_task_teams, task_team->tt.tt_next);
3971 task_team->tt.tt_next = NULL;
3973 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3976 if (task_team == NULL) {
3977 KE_TRACE(10, (
"__kmp_allocate_task_team: T#%d allocating "
3978 "task team for team %p\n",
3979 __kmp_gtid_from_thread(thread), team));
3982 task_team = (kmp_task_team_t *)__kmp_allocate(
sizeof(kmp_task_team_t));
3983 __kmp_init_bootstrap_lock(&task_team->tt.tt_threads_lock);
3984 __kmp_init_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
3985#if USE_ITT_BUILD && USE_ITT_NOTIFY && KMP_DEBUG
3988 __itt_suppress_mark_range(
3989 __itt_suppress_range, __itt_suppress_threading_errors,
3990 &task_team->tt.tt_found_tasks,
sizeof(task_team->tt.tt_found_tasks));
3991 __itt_suppress_mark_range(__itt_suppress_range,
3992 __itt_suppress_threading_errors,
3993 CCAST(kmp_uint32 *, &task_team->tt.tt_active),
3994 sizeof(task_team->tt.tt_active));
4002 __kmp_task_team_init(task_team, team);
4004 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d exiting; task_team = %p "
4005 "unfinished_threads init'd to %d\n",
4006 (thread ? __kmp_gtid_from_thread(thread) : -1), task_team,
4007 KMP_ATOMIC_LD_RLX(&task_team->tt.tt_unfinished_threads)));
4014void __kmp_free_task_team(kmp_info_t *thread, kmp_task_team_t *task_team) {
4015 KA_TRACE(20, (
"__kmp_free_task_team: T#%d task_team = %p\n",
4016 thread ? __kmp_gtid_from_thread(thread) : -1, task_team));
4019 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
4021 KMP_DEBUG_ASSERT(task_team->tt.tt_next == NULL);
4022 task_team->tt.tt_next = __kmp_free_task_teams;
4023 TCW_PTR(__kmp_free_task_teams, task_team);
4025 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
4033void __kmp_reap_task_teams(
void) {
4034 kmp_task_team_t *task_team;
4036 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
4038 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
4039 while ((task_team = __kmp_free_task_teams) != NULL) {
4040 __kmp_free_task_teams = task_team->tt.tt_next;
4041 task_team->tt.tt_next = NULL;
4044 if (task_team->tt.tt_threads_data != NULL) {
4045 __kmp_free_task_threads_data(task_team);
4047 if (task_team->tt.tt_task_pri_list != NULL) {
4048 __kmp_free_task_pri_list(task_team);
4050 __kmp_free(task_team);
4052 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
4060void __kmp_push_task_team_node(kmp_info_t *thread, kmp_team_t *team) {
4061 KMP_DEBUG_ASSERT(team->t.t_nproc == 1);
4062 kmp_task_team_list_t *current =
4063 (kmp_task_team_list_t *)(&team->t.t_task_team[0]);
4064 kmp_task_team_list_t *node =
4065 (kmp_task_team_list_t *)__kmp_allocate(
sizeof(kmp_task_team_list_t));
4066 node->task_team = current->task_team;
4067 node->next = current->next;
4068 thread->th.th_task_team = current->task_team = NULL;
4069 current->next = node;
4073void __kmp_pop_task_team_node(kmp_info_t *thread, kmp_team_t *team) {
4074 KMP_DEBUG_ASSERT(team->t.t_nproc == 1);
4075 kmp_task_team_list_t *current =
4076 (kmp_task_team_list_t *)(&team->t.t_task_team[0]);
4077 if (current->task_team) {
4078 __kmp_free_task_team(thread, current->task_team);
4080 kmp_task_team_list_t *next = current->next;
4082 current->task_team = next->task_team;
4083 current->next = next->next;
4084 KMP_DEBUG_ASSERT(next != current);
4086 thread->th.th_task_team = current->task_team;
4093void __kmp_wait_to_unref_task_teams(
void) {
4099 KMP_INIT_YIELD(spins);
4100 KMP_INIT_BACKOFF(time);
4108 for (thread = CCAST(kmp_info_t *, __kmp_thread_pool); thread != NULL;
4109 thread = thread->th.th_next_pool) {
4113 if (TCR_PTR(thread->th.th_task_team) == NULL) {
4114 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n",
4115 __kmp_gtid_from_thread(thread)));
4120 if (!__kmp_is_thread_alive(thread, &exit_val)) {
4121 thread->th.th_task_team = NULL;
4128 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: Waiting for T#%d to "
4129 "unreference task_team\n",
4130 __kmp_gtid_from_thread(thread)));
4132 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
4135 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
4139 (
"__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n",
4140 __kmp_gtid_from_thread(thread), __kmp_gtid_from_thread(thread)));
4141 __kmp_null_resume_wrapper(thread);
4150 KMP_YIELD_OVERSUB_ELSE_SPIN(spins, time);
4156void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team) {
4157 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
4162 if (team == this_thr->th.th_serial_team ||
4163 team == this_thr->th.th_root->r.r_root_team) {
4164 KMP_DEBUG_ASSERT(team->t.t_nproc == 1);
4165 if (team->t.t_task_team[0] == NULL) {
4166 team->t.t_task_team[0] = __kmp_allocate_task_team(this_thr, team);
4168 20, (
"__kmp_task_team_setup: Primary T#%d created new task_team %p"
4169 " for serial/root team %p\n",
4170 __kmp_gtid_from_thread(this_thr), team->t.t_task_team[0], team));
4173 __kmp_task_team_init(team->t.t_task_team[0], team);
4181 if (team->t.t_task_team[this_thr->th.th_task_state] == NULL) {
4182 team->t.t_task_team[this_thr->th.th_task_state] =
4183 __kmp_allocate_task_team(this_thr, team);
4184 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d created new task_team %p"
4185 " for team %d at parity=%d\n",
4186 __kmp_gtid_from_thread(this_thr),
4187 team->t.t_task_team[this_thr->th.th_task_state], team->t.t_id,
4188 this_thr->th.th_task_state));
4197 int other_team = 1 - this_thr->th.th_task_state;
4198 KMP_DEBUG_ASSERT(other_team >= 0 && other_team < 2);
4199 if (team->t.t_task_team[other_team] == NULL) {
4200 team->t.t_task_team[other_team] = __kmp_allocate_task_team(this_thr, team);
4201 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d created second new "
4202 "task_team %p for team %d at parity=%d\n",
4203 __kmp_gtid_from_thread(this_thr),
4204 team->t.t_task_team[other_team], team->t.t_id, other_team));
4207 kmp_task_team_t *task_team = team->t.t_task_team[other_team];
4208 __kmp_task_team_init(task_team, team);
4211 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d reset next task_team "
4212 "%p for team %d at parity=%d\n",
4213 __kmp_gtid_from_thread(this_thr),
4214 team->t.t_task_team[other_team], team->t.t_id, other_team));
4221 if (this_thr == __kmp_hidden_helper_main_thread) {
4222 for (
int i = 0; i < 2; ++i) {
4223 kmp_task_team_t *task_team = team->t.t_task_team[i];
4224 if (KMP_TASKING_ENABLED(task_team)) {
4227 __kmp_enable_tasking(task_team, this_thr);
4228 for (
int j = 0; j < task_team->tt.tt_nproc; ++j) {
4229 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[j];
4230 if (thread_data->td.td_deque == NULL) {
4231 __kmp_alloc_task_deque(__kmp_hidden_helper_threads[j], thread_data);
4241void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team) {
4242 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
4243 KMP_DEBUG_ASSERT(team != this_thr->th.th_serial_team);
4244 KMP_DEBUG_ASSERT(team != this_thr->th.th_root->r.r_root_team);
4248 this_thr->th.th_task_state = (kmp_uint8)(1 - this_thr->th.th_task_state);
4252 TCW_PTR(this_thr->th.th_task_team,
4253 team->t.t_task_team[this_thr->th.th_task_state]);
4255 (
"__kmp_task_team_sync: Thread T#%d task team switched to task_team "
4256 "%p from Team #%d (parity=%d)\n",
4257 __kmp_gtid_from_thread(this_thr), this_thr->th.th_task_team,
4258 team->t.t_id, this_thr->th.th_task_state));
4267void __kmp_task_team_wait(
4268 kmp_info_t *this_thr,
4269 kmp_team_t *team USE_ITT_BUILD_ARG(
void *itt_sync_obj),
int wait) {
4270 kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state];
4272 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
4273 KMP_DEBUG_ASSERT(task_team == this_thr->th.th_task_team);
4275 if ((task_team != NULL) && KMP_TASKING_ENABLED(task_team)) {
4277 KA_TRACE(20, (
"__kmp_task_team_wait: Primary T#%d waiting for all tasks "
4278 "(for unfinished_threads to reach 0) on task_team = %p\n",
4279 __kmp_gtid_from_thread(this_thr), task_team));
4283 kmp_flag_32<false, false> flag(
4284 RCAST(std::atomic<kmp_uint32> *,
4285 &task_team->tt.tt_unfinished_threads),
4287 flag.wait(this_thr, TRUE USE_ITT_BUILD_ARG(itt_sync_obj));
4293 (
"__kmp_task_team_wait: Primary T#%d deactivating task_team %p: "
4294 "setting active to false, setting local and team's pointer to NULL\n",
4295 __kmp_gtid_from_thread(this_thr), task_team));
4296 TCW_SYNC_4(task_team->tt.tt_found_proxy_tasks, FALSE);
4297 TCW_SYNC_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
4298 KMP_CHECK_UPDATE(task_team->tt.tt_untied_task_encountered, 0);
4299 TCW_SYNC_4(task_team->tt.tt_active, FALSE);
4302 TCW_PTR(this_thr->th.th_task_team, NULL);
4311void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread,
int gtid) {
4312 std::atomic<kmp_uint32> *spin = RCAST(
4313 std::atomic<kmp_uint32> *,
4314 &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads);
4316 KMP_DEBUG_ASSERT(__kmp_tasking_mode == tskm_extra_barrier);
4319 KMP_FSYNC_SPIN_INIT(spin, NULL);
4321 kmp_flag_32<false, false> spin_flag(spin, 0U);
4322 while (!spin_flag.execute_tasks(thread, gtid, TRUE,
4323 &flag USE_ITT_BUILD_ARG(NULL), 0)) {
4326 KMP_FSYNC_SPIN_PREPARE(RCAST(
void *, spin));
4329 if (TCR_4(__kmp_global.g.g_done)) {
4330 if (__kmp_global.g.g_abort)
4331 __kmp_abort_thread();
4337 KMP_FSYNC_SPIN_ACQUIRED(RCAST(
void *, spin));
4346static bool __kmp_give_task(kmp_info_t *thread, kmp_int32 tid, kmp_task_t *task,
4348 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4349 kmp_task_team_t *task_team = taskdata->td_task_team;
4351 KA_TRACE(20, (
"__kmp_give_task: trying to give task %p to thread %d.\n",
4355 KMP_DEBUG_ASSERT(task_team != NULL);
4357 bool result =
false;
4358 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
4360 if (thread_data->td.td_deque == NULL) {
4364 (
"__kmp_give_task: thread %d has no queue while giving task %p.\n",
4369 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4370 TASK_DEQUE_SIZE(thread_data->td)) {
4373 (
"__kmp_give_task: queue is full while giving task %p to thread %d.\n",
4378 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
4381 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
4382 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4383 TASK_DEQUE_SIZE(thread_data->td)) {
4385 __kmp_realloc_task_deque(thread, thread_data);
4390 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
4392 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4393 TASK_DEQUE_SIZE(thread_data->td)) {
4394 KA_TRACE(30, (
"__kmp_give_task: queue is full while giving task %p to "
4400 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
4401 goto release_and_exit;
4403 __kmp_realloc_task_deque(thread, thread_data);
4409 thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
4411 thread_data->td.td_deque_tail =
4412 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
4413 TCW_4(thread_data->td.td_deque_ntasks,
4414 TCR_4(thread_data->td.td_deque_ntasks) + 1);
4417 KA_TRACE(30, (
"__kmp_give_task: successfully gave task %p to thread %d.\n",
4421 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
4426#define PROXY_TASK_FLAG 0x40000000
4443static void __kmp_first_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
4444 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
4445 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4446 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
4447 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
4449 taskdata->td_flags.complete = 1;
4451 taskdata->td_flags.onced = 1;
4454 if (taskdata->td_taskgroup)
4455 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
4459 KMP_ATOMIC_OR(&taskdata->td_incomplete_child_tasks, PROXY_TASK_FLAG);
4462static void __kmp_second_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
4464 kmp_int32 children = 0;
4468 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks);
4469 KMP_DEBUG_ASSERT(children >= 0);
4472 KMP_ATOMIC_AND(&taskdata->td_incomplete_child_tasks, ~PROXY_TASK_FLAG);
4475static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask) {
4476 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4477 kmp_info_t *thread = __kmp_threads[gtid];
4479 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4480 KMP_DEBUG_ASSERT(taskdata->td_flags.complete ==
4485 while ((KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) &
4486 PROXY_TASK_FLAG) > 0)
4489 __kmp_release_deps(gtid, taskdata);
4490 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
4502 KMP_DEBUG_ASSERT(ptask != NULL);
4503 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4505 10, (
"__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n",
4507 __kmp_assert_valid_gtid(gtid);
4508 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4510 __kmp_first_top_half_finish_proxy(taskdata);
4511 __kmp_second_top_half_finish_proxy(taskdata);
4512 __kmp_bottom_half_finish_proxy(gtid, ptask);
4515 (
"__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n",
4519void __kmpc_give_task(kmp_task_t *ptask, kmp_int32 start = 0) {
4520 KMP_DEBUG_ASSERT(ptask != NULL);
4521 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4525 kmp_team_t *team = taskdata->td_team;
4526 kmp_int32 nthreads = team->t.t_nproc;
4531 kmp_int32 start_k = start % nthreads;
4533 kmp_int32 k = start_k;
4537 thread = team->t.t_threads[k];
4538 k = (k + 1) % nthreads;
4544 }
while (!__kmp_give_task(thread, k, ptask, pass));
4546 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME && __kmp_wpolicy_passive) {
4548 for (
int i = 0; i < nthreads; ++i) {
4549 thread = team->t.t_threads[i];
4550 if (thread->th.th_sleep_loc != NULL) {
4551 __kmp_null_resume_wrapper(thread);
4566 KMP_DEBUG_ASSERT(ptask != NULL);
4567 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4571 (
"__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n",
4574 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4576 __kmp_first_top_half_finish_proxy(taskdata);
4578 __kmpc_give_task(ptask);
4580 __kmp_second_top_half_finish_proxy(taskdata);
4584 (
"__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n",
4588kmp_event_t *__kmpc_task_allow_completion_event(
ident_t *loc_ref,
int gtid,
4590 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(task);
4591 if (td->td_allow_completion_event.type == KMP_EVENT_UNINITIALIZED) {
4592 td->td_allow_completion_event.type = KMP_EVENT_ALLOW_COMPLETION;
4593 td->td_allow_completion_event.ed.task = task;
4594 __kmp_init_tas_lock(&td->td_allow_completion_event.lock);
4596 return &td->td_allow_completion_event;
4599void __kmp_fulfill_event(kmp_event_t *event) {
4600 if (event->type == KMP_EVENT_ALLOW_COMPLETION) {
4601 kmp_task_t *ptask = event->ed.task;
4602 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4603 bool detached =
false;
4604 int gtid = __kmp_get_gtid();
4609 __kmp_acquire_tas_lock(&event->lock, gtid);
4610 if (taskdata->td_flags.proxy == TASK_PROXY) {
4616 if (UNLIKELY(ompt_enabled.enabled))
4617 __ompt_task_finish(ptask, NULL, ompt_task_early_fulfill);
4620 event->type = KMP_EVENT_UNINITIALIZED;
4621 __kmp_release_tas_lock(&event->lock, gtid);
4627 if (UNLIKELY(ompt_enabled.enabled))
4628 __ompt_task_finish(ptask, NULL, ompt_task_late_fulfill);
4632 kmp_team_t *team = taskdata->td_team;
4633 kmp_info_t *thread = __kmp_get_thread();
4634 if (thread->th.th_team == team) {
4654kmp_task_t *__kmp_task_dup_alloc(kmp_info_t *thread, kmp_task_t *task_src
4656 ,
int taskloop_recur
4660 kmp_taskdata_t *taskdata;
4661 kmp_taskdata_t *taskdata_src = KMP_TASK_TO_TASKDATA(task_src);
4662 kmp_taskdata_t *parent_task = taskdata_src->td_parent;
4663 size_t shareds_offset;
4666 KA_TRACE(10, (
"__kmp_task_dup_alloc(enter): Th %p, source task %p\n", thread,
4668 KMP_DEBUG_ASSERT(taskdata_src->td_flags.proxy ==
4670 KMP_DEBUG_ASSERT(taskdata_src->td_flags.tasktype == TASK_EXPLICIT);
4671 task_size = taskdata_src->td_size_alloc;
4674 KA_TRACE(30, (
"__kmp_task_dup_alloc: Th %p, malloc size %ld\n", thread,
4677 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, task_size);
4679 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, task_size);
4681 KMP_MEMCPY(taskdata, taskdata_src, task_size);
4683 task = KMP_TASKDATA_TO_TASK(taskdata);
4687 if (taskdata->is_taskgraph && !taskloop_recur &&
4688 __kmp_tdg_is_recording(taskdata_src->tdg->tdg_status))
4689 taskdata->td_tdg_task_id = KMP_ATOMIC_INC(&__kmp_tdg_task_id);
4691 taskdata->td_task_id = KMP_GEN_TASK_ID();
4692 if (task->shareds != NULL) {
4693 shareds_offset = (
char *)task_src->shareds - (
char *)taskdata_src;
4694 task->shareds = &((
char *)taskdata)[shareds_offset];
4695 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
4698 taskdata->td_alloc_thread = thread;
4699 taskdata->td_parent = parent_task;
4701 taskdata->td_taskgroup = parent_task->td_taskgroup;
4704 if (taskdata->td_flags.tiedness == TASK_TIED)
4705 taskdata->td_last_tied = taskdata;
4709 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
4710 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
4711 if (parent_task->td_taskgroup)
4712 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
4715 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT)
4716 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
4720 (
"__kmp_task_dup_alloc(exit): Th %p, created task %p, parent=%p\n",
4721 thread, taskdata, taskdata->td_parent));
4723 if (UNLIKELY(ompt_enabled.enabled))
4724 __ompt_task_init(taskdata, thread->th.th_info.ds.ds_gtid);
4733typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32);
4735KMP_BUILD_ASSERT(
sizeof(
long) == 4 ||
sizeof(
long) == 8);
4740class kmp_taskloop_bounds_t {
4742 const kmp_taskdata_t *taskdata;
4743 size_t lower_offset;
4744 size_t upper_offset;
4747 kmp_taskloop_bounds_t(kmp_task_t *_task, kmp_uint64 *lb, kmp_uint64 *ub)
4748 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(task)),
4749 lower_offset((char *)lb - (char *)task),
4750 upper_offset((char *)ub - (char *)task) {
4751 KMP_DEBUG_ASSERT((
char *)lb > (
char *)_task);
4752 KMP_DEBUG_ASSERT((
char *)ub > (
char *)_task);
4754 kmp_taskloop_bounds_t(kmp_task_t *_task,
const kmp_taskloop_bounds_t &bounds)
4755 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(_task)),
4756 lower_offset(bounds.lower_offset), upper_offset(bounds.upper_offset) {}
4757 size_t get_lower_offset()
const {
return lower_offset; }
4758 size_t get_upper_offset()
const {
return upper_offset; }
4759 kmp_uint64 get_lb()
const {
4761#if defined(KMP_GOMP_COMPAT)
4763 if (!taskdata->td_flags.native) {
4764 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4767 if (taskdata->td_size_loop_bounds == 4) {
4768 kmp_int32 *lb = RCAST(kmp_int32 *, task->shareds);
4769 retval = (kmp_int64)*lb;
4771 kmp_int64 *lb = RCAST(kmp_int64 *, task->shareds);
4772 retval = (kmp_int64)*lb;
4777 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4781 kmp_uint64 get_ub()
const {
4783#if defined(KMP_GOMP_COMPAT)
4785 if (!taskdata->td_flags.native) {
4786 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4789 if (taskdata->td_size_loop_bounds == 4) {
4790 kmp_int32 *ub = RCAST(kmp_int32 *, task->shareds) + 1;
4791 retval = (kmp_int64)*ub;
4793 kmp_int64 *ub = RCAST(kmp_int64 *, task->shareds) + 1;
4794 retval = (kmp_int64)*ub;
4798 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4802 void set_lb(kmp_uint64 lb) {
4803#if defined(KMP_GOMP_COMPAT)
4805 if (!taskdata->td_flags.native) {
4806 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4809 if (taskdata->td_size_loop_bounds == 4) {
4810 kmp_uint32 *lower = RCAST(kmp_uint32 *, task->shareds);
4811 *lower = (kmp_uint32)lb;
4813 kmp_uint64 *lower = RCAST(kmp_uint64 *, task->shareds);
4814 *lower = (kmp_uint64)lb;
4818 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4821 void set_ub(kmp_uint64 ub) {
4822#if defined(KMP_GOMP_COMPAT)
4824 if (!taskdata->td_flags.native) {
4825 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4828 if (taskdata->td_size_loop_bounds == 4) {
4829 kmp_uint32 *upper = RCAST(kmp_uint32 *, task->shareds) + 1;
4830 *upper = (kmp_uint32)ub;
4832 kmp_uint64 *upper = RCAST(kmp_uint64 *, task->shareds) + 1;
4833 *upper = (kmp_uint64)ub;
4837 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4858void __kmp_taskloop_linear(
ident_t *loc,
int gtid, kmp_task_t *task,
4859 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4860 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
4861 kmp_uint64 grainsize, kmp_uint64 extras,
4862 kmp_int64 last_chunk, kmp_uint64 tc,
4868 KMP_TIME_PARTITIONED_BLOCK(OMP_taskloop_scheduling);
4869 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
4871 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4872 kmp_uint64 lower = task_bounds.get_lb();
4873 kmp_uint64 upper = task_bounds.get_ub();
4875 kmp_info_t *thread = __kmp_threads[gtid];
4876 kmp_taskdata_t *current_task = thread->th.th_current_task;
4877 kmp_task_t *next_task;
4878 kmp_int32 lastpriv = 0;
4880 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4881 (last_chunk < 0 ? last_chunk : extras));
4882 KMP_DEBUG_ASSERT(num_tasks > extras);
4883 KMP_DEBUG_ASSERT(num_tasks > 0);
4884 KA_TRACE(20, (
"__kmp_taskloop_linear: T#%d: %lld tasks, grainsize %lld, "
4885 "extras %lld, last_chunk %lld, i=%lld,%lld(%d)%lld, dup %p\n",
4886 gtid, num_tasks, grainsize, extras, last_chunk, lower, upper,
4887 ub_glob, st, task_dup));
4890 for (i = 0; i < num_tasks; ++i) {
4891 kmp_uint64 chunk_minus_1;
4893 chunk_minus_1 = grainsize - 1;
4895 chunk_minus_1 = grainsize;
4898 upper = lower + st * chunk_minus_1;
4902 if (i == num_tasks - 1) {
4905 KMP_DEBUG_ASSERT(upper == *ub);
4906 if (upper == ub_glob)
4908 }
else if (st > 0) {
4909 KMP_DEBUG_ASSERT((kmp_uint64)st > *ub - upper);
4910 if ((kmp_uint64)st > ub_glob - upper)
4913 KMP_DEBUG_ASSERT(upper + st < *ub);
4914 if (upper - ub_glob < (kmp_uint64)(-st))
4920 next_task = __kmp_task_dup_alloc(thread, task, 0);
4922 next_task = __kmp_task_dup_alloc(thread, task);
4925 kmp_taskdata_t *next_taskdata = KMP_TASK_TO_TASKDATA(next_task);
4926 kmp_taskloop_bounds_t next_task_bounds =
4927 kmp_taskloop_bounds_t(next_task, task_bounds);
4930 next_task_bounds.set_lb(lower);
4931 if (next_taskdata->td_flags.native) {
4932 next_task_bounds.set_ub(upper + (st > 0 ? 1 : -1));
4934 next_task_bounds.set_ub(upper);
4936 if (ptask_dup != NULL)
4938 ptask_dup(next_task, task, lastpriv);
4940 (
"__kmp_taskloop_linear: T#%d; task #%llu: task %p: lower %lld, "
4941 "upper %lld stride %lld, (offsets %p %p)\n",
4942 gtid, i, next_task, lower, upper, st,
4943 next_task_bounds.get_lower_offset(),
4944 next_task_bounds.get_upper_offset()));
4946 __kmp_omp_taskloop_task(NULL, gtid, next_task,
4949 if (ompt_enabled.ompt_callback_dispatch) {
4950 OMPT_GET_DISPATCH_CHUNK(next_taskdata->ompt_task_info.dispatch_chunk,
4955 __kmp_omp_task(gtid, next_task,
true);
4960 __kmp_task_start(gtid, task, current_task);
4962 __kmp_task_finish<false>(gtid, task, current_task);
4967typedef struct __taskloop_params {
4974 kmp_uint64 num_tasks;
4975 kmp_uint64 grainsize;
4977 kmp_int64 last_chunk;
4979 kmp_uint64 num_t_min;
4983} __taskloop_params_t;
4985void __kmp_taskloop_recur(
ident_t *,
int, kmp_task_t *, kmp_uint64 *,
4986 kmp_uint64 *, kmp_int64, kmp_uint64, kmp_uint64,
4987 kmp_uint64, kmp_uint64, kmp_int64, kmp_uint64,
4995int __kmp_taskloop_task(
int gtid,
void *ptask) {
4996 __taskloop_params_t *p =
4997 (__taskloop_params_t *)((kmp_task_t *)ptask)->shareds;
4998 kmp_task_t *task = p->task;
4999 kmp_uint64 *lb = p->lb;
5000 kmp_uint64 *ub = p->ub;
5001 void *task_dup = p->task_dup;
5003 kmp_int64 st = p->st;
5004 kmp_uint64 ub_glob = p->ub_glob;
5005 kmp_uint64 num_tasks = p->num_tasks;
5006 kmp_uint64 grainsize = p->grainsize;
5007 kmp_uint64 extras = p->extras;
5008 kmp_int64 last_chunk = p->last_chunk;
5009 kmp_uint64 tc = p->tc;
5010 kmp_uint64 num_t_min = p->num_t_min;
5012 void *codeptr_ra = p->codeptr_ra;
5015 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
5016 KMP_DEBUG_ASSERT(task != NULL);
5018 (
"__kmp_taskloop_task: T#%d, task %p: %lld tasks, grainsize"
5019 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
5020 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
5023 KMP_DEBUG_ASSERT(num_tasks * 2 + 1 > num_t_min);
5024 if (num_tasks > num_t_min)
5025 __kmp_taskloop_recur(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
5026 grainsize, extras, last_chunk, tc, num_t_min,
5032 __kmp_taskloop_linear(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
5033 grainsize, extras, last_chunk, tc,
5039 KA_TRACE(40, (
"__kmp_taskloop_task(exit): T#%d\n", gtid));
5061void __kmp_taskloop_recur(
ident_t *loc,
int gtid, kmp_task_t *task,
5062 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
5063 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
5064 kmp_uint64 grainsize, kmp_uint64 extras,
5065 kmp_int64 last_chunk, kmp_uint64 tc,
5066 kmp_uint64 num_t_min,
5071 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
5072 KMP_DEBUG_ASSERT(task != NULL);
5073 KMP_DEBUG_ASSERT(num_tasks > num_t_min);
5075 (
"__kmp_taskloop_recur: T#%d, task %p: %lld tasks, grainsize"
5076 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
5077 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
5079 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
5080 kmp_uint64 lower = *lb;
5081 kmp_info_t *thread = __kmp_threads[gtid];
5083 kmp_task_t *next_task;
5084 size_t lower_offset =
5085 (
char *)lb - (
char *)task;
5086 size_t upper_offset =
5087 (
char *)ub - (
char *)task;
5089 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
5090 (last_chunk < 0 ? last_chunk : extras));
5091 KMP_DEBUG_ASSERT(num_tasks > extras);
5092 KMP_DEBUG_ASSERT(num_tasks > 0);
5095 kmp_uint64 lb1, ub0, tc0, tc1, ext0, ext1;
5096 kmp_int64 last_chunk0 = 0, last_chunk1 = 0;
5097 kmp_uint64 gr_size0 = grainsize;
5098 kmp_uint64 n_tsk0 = num_tasks >> 1;
5099 kmp_uint64 n_tsk1 = num_tasks - n_tsk0;
5100 if (last_chunk < 0) {
5102 last_chunk1 = last_chunk;
5103 tc0 = grainsize * n_tsk0;
5105 }
else if (n_tsk0 <= extras) {
5108 ext1 = extras - n_tsk0;
5109 tc0 = gr_size0 * n_tsk0;
5114 tc1 = grainsize * n_tsk1;
5117 ub0 = lower + st * (tc0 - 1);
5122 next_task = __kmp_task_dup_alloc(thread, task,
5125 next_task = __kmp_task_dup_alloc(thread, task);
5128 *(kmp_uint64 *)((
char *)next_task + lower_offset) = lb1;
5129 if (ptask_dup != NULL)
5130 ptask_dup(next_task, task, 0);
5135 kmp_taskdata_t *current_task = thread->th.th_current_task;
5136 thread->th.th_current_task = taskdata->td_parent;
5137 kmp_task_t *new_task =
5138 __kmpc_omp_task_alloc(loc, gtid, 1, 3 *
sizeof(
void *),
5139 sizeof(__taskloop_params_t), &__kmp_taskloop_task);
5141 thread->th.th_current_task = current_task;
5142 __taskloop_params_t *p = (__taskloop_params_t *)new_task->shareds;
5143 p->task = next_task;
5144 p->lb = (kmp_uint64 *)((
char *)next_task + lower_offset);
5145 p->ub = (kmp_uint64 *)((
char *)next_task + upper_offset);
5146 p->task_dup = task_dup;
5148 p->ub_glob = ub_glob;
5149 p->num_tasks = n_tsk1;
5150 p->grainsize = grainsize;
5152 p->last_chunk = last_chunk1;
5154 p->num_t_min = num_t_min;
5156 p->codeptr_ra = codeptr_ra;
5160 kmp_taskdata_t *new_task_data = KMP_TASK_TO_TASKDATA(new_task);
5161 new_task_data->tdg = taskdata->tdg;
5162 new_task_data->is_taskgraph = 0;
5167 __kmp_omp_taskloop_task(NULL, gtid, new_task, codeptr_ra);
5169 __kmp_omp_task(gtid, new_task,
true);
5173 if (n_tsk0 > num_t_min)
5174 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0, gr_size0,
5175 ext0, last_chunk0, tc0, num_t_min,
5181 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0,
5182 gr_size0, ext0, last_chunk0, tc0,
5188 KA_TRACE(40, (
"__kmp_taskloop_recur(exit): T#%d\n", gtid));
5191static void __kmp_taskloop(
ident_t *loc,
int gtid, kmp_task_t *task,
int if_val,
5192 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
5193 int nogroup,
int sched, kmp_uint64 grainsize,
5194 int modifier,
void *task_dup) {
5195 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
5196 KMP_DEBUG_ASSERT(task != NULL);
5198#if OMPT_SUPPORT && OMPT_OPTIONAL
5199 OMPT_STORE_RETURN_ADDRESS(gtid);
5201 __kmpc_taskgroup(loc, gtid);
5205 KMP_ATOMIC_DEC(&__kmp_tdg_task_id);
5209 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
5212 kmp_uint64 lower = task_bounds.get_lb();
5213 kmp_uint64 upper = task_bounds.get_ub();
5214 kmp_uint64 ub_glob = upper;
5215 kmp_uint64 num_tasks = 0, extras = 0;
5216 kmp_int64 last_chunk =
5218 kmp_uint64 num_tasks_min = __kmp_taskloop_min_tasks;
5219 kmp_info_t *thread = __kmp_threads[gtid];
5220 kmp_taskdata_t *current_task = thread->th.th_current_task;
5222 KA_TRACE(20, (
"__kmp_taskloop: T#%d, task %p, lb %lld, ub %lld, st %lld, "
5223 "grain %llu(%d, %d), dup %p\n",
5224 gtid, taskdata, lower, upper, st, grainsize, sched, modifier,
5229 tc = upper - lower + 1;
5230 }
else if (st < 0) {
5231 tc = (lower - upper) / (-st) + 1;
5233 tc = (upper - lower) / st + 1;
5236 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d zero-trip loop\n", gtid));
5238 __kmp_task_start(gtid, task, current_task);
5240 __kmp_task_finish<false>(gtid, task, current_task);
5244#if OMPT_SUPPORT && OMPT_OPTIONAL
5245 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
5246 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
5247 if (ompt_enabled.ompt_callback_work) {
5248 ompt_callbacks.ompt_callback(ompt_callback_work)(
5249 ompt_work_taskloop, ompt_scope_begin, &(team_info->parallel_data),
5250 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
5254 if (num_tasks_min == 0)
5257 KMP_MIN(thread->th.th_team_nproc * 10, INITIAL_TASK_DEQUE_SIZE);
5263 grainsize = thread->th.th_team_nproc *
static_cast<kmp_uint64
>(10);
5266 if (grainsize > tc) {
5271 num_tasks = grainsize;
5272 grainsize = tc / num_tasks;
5273 extras = tc % num_tasks;
5277 if (grainsize > tc) {
5283 num_tasks = (tc + grainsize - 1) / grainsize;
5284 last_chunk = tc - (num_tasks * grainsize);
5287 num_tasks = tc / grainsize;
5289 grainsize = tc / num_tasks;
5290 extras = tc % num_tasks;
5295 KMP_ASSERT2(0,
"unknown scheduling of taskloop");
5298 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
5299 (last_chunk < 0 ? last_chunk : extras));
5300 KMP_DEBUG_ASSERT(num_tasks > extras);
5301 KMP_DEBUG_ASSERT(num_tasks > 0);
5307 taskdata->td_flags.task_serial = 1;
5308 taskdata->td_flags.tiedness = TASK_TIED;
5310 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5311 grainsize, extras, last_chunk, tc,
5313 OMPT_GET_RETURN_ADDRESS(0),
5318 }
else if (num_tasks > num_tasks_min && !taskdata->td_flags.native) {
5319 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go recursive: tc %llu, #tasks %llu"
5320 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
5321 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
5323 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5324 grainsize, extras, last_chunk, tc, num_tasks_min,
5326 OMPT_GET_RETURN_ADDRESS(0),
5330 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go linear: tc %llu, #tasks %llu"
5331 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
5332 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
5334 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5335 grainsize, extras, last_chunk, tc,
5337 OMPT_GET_RETURN_ADDRESS(0),
5342#if OMPT_SUPPORT && OMPT_OPTIONAL
5343 if (ompt_enabled.ompt_callback_work) {
5344 ompt_callbacks.ompt_callback(ompt_callback_work)(
5345 ompt_work_taskloop, ompt_scope_end, &(team_info->parallel_data),
5346 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
5351#if OMPT_SUPPORT && OMPT_OPTIONAL
5352 OMPT_STORE_RETURN_ADDRESS(gtid);
5354 __kmpc_end_taskgroup(loc, gtid);
5356 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d\n", gtid));
5376 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
int nogroup,
5377 int sched, kmp_uint64 grainsize,
void *task_dup) {
5378 __kmp_assert_valid_gtid(gtid);
5379 KA_TRACE(20, (
"__kmpc_taskloop(enter): T#%d\n", gtid));
5380 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
5382 KA_TRACE(20, (
"__kmpc_taskloop(exit): T#%d\n", gtid));
5403 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
5404 int nogroup,
int sched, kmp_uint64 grainsize,
5405 int modifier,
void *task_dup) {
5406 __kmp_assert_valid_gtid(gtid);
5407 KA_TRACE(20, (
"__kmpc_taskloop_5(enter): T#%d\n", gtid));
5408 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
5409 modifier, task_dup);
5410 KA_TRACE(20, (
"__kmpc_taskloop_5(exit): T#%d\n", gtid));
5422 if (gtid == KMP_GTID_DNE)
5425 kmp_info_t *thread = __kmp_thread_from_gtid(gtid);
5426 kmp_taskdata_t *taskdata = thread->th.th_current_task;
5431 return &taskdata->td_target_data.async_handle;
5443 if (gtid == KMP_GTID_DNE)
5446 kmp_info_t *thread = __kmp_thread_from_gtid(gtid);
5447 kmp_taskdata_t *taskdata = thread->th.th_current_task;
5452 return taskdata->td_task_team != NULL;
5460static kmp_tdg_info_t *__kmp_find_tdg(kmp_int32 tdg_id) {
5461 kmp_tdg_info_t *res =
nullptr;
5462 if (__kmp_max_tdgs == 0)
5465 if (__kmp_global_tdgs == NULL)
5466 __kmp_global_tdgs = (kmp_tdg_info_t **)__kmp_allocate(
5467 sizeof(kmp_tdg_info_t *) * __kmp_max_tdgs);
5469 if ((__kmp_global_tdgs[tdg_id]) &&
5470 (__kmp_global_tdgs[tdg_id]->tdg_status != KMP_TDG_NONE))
5471 res = __kmp_global_tdgs[tdg_id];
5478void __kmp_print_tdg_dot(kmp_tdg_info_t *tdg, kmp_int32 gtid) {
5479 kmp_int32 tdg_id = tdg->tdg_id;
5480 KA_TRACE(10, (
"__kmp_print_tdg_dot(enter): T#%d tdg_id=%d \n", gtid, tdg_id));
5483 sprintf(file_name,
"tdg_%d.dot", tdg_id);
5486 kmp_int32 num_tasks = KMP_ATOMIC_LD_RLX(&tdg->num_tasks);
5490 " subgraph cluster {\n"
5493 for (kmp_int32 i = 0; i < num_tasks; i++) {
5494 fprintf(tdg_file,
" %d[style=bold]\n", i);
5496 fprintf(tdg_file,
" }\n");
5497 for (kmp_int32 i = 0; i < num_tasks; i++) {
5498 kmp_int32 nsuccessors = tdg->record_map[i].nsuccessors;
5499 kmp_int32 *successors = tdg->record_map[i].successors;
5500 if (nsuccessors > 0) {
5501 for (kmp_int32 j = 0; j < nsuccessors; j++)
5502 fprintf(tdg_file,
" %d -> %d \n", i, successors[j]);
5505 fprintf(tdg_file,
"}");
5506 KA_TRACE(10, (
"__kmp_print_tdg_dot(exit): T#%d tdg_id=%d \n", gtid, tdg_id));
5513void __kmp_exec_tdg(kmp_int32 gtid, kmp_tdg_info_t *tdg) {
5514 KMP_DEBUG_ASSERT(tdg->tdg_status == KMP_TDG_READY);
5515 KA_TRACE(10, (
"__kmp_exec_tdg(enter): T#%d tdg_id=%d num_roots=%d\n", gtid,
5516 tdg->tdg_id, tdg->num_roots));
5517 kmp_node_info_t *this_record_map = tdg->record_map;
5518 kmp_int32 *this_root_tasks = tdg->root_tasks;
5519 kmp_int32 this_num_roots = tdg->num_roots;
5520 kmp_int32 this_num_tasks = KMP_ATOMIC_LD_RLX(&tdg->num_tasks);
5522 kmp_info_t *thread = __kmp_threads[gtid];
5523 kmp_taskdata_t *parent_task = thread->th.th_current_task;
5525 if (tdg->rec_taskred_data) {
5529 for (kmp_int32 j = 0; j < this_num_tasks; j++) {
5530 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(this_record_map[j].task);
5532 td->td_parent = parent_task;
5533 this_record_map[j].parent_task = parent_task;
5535 kmp_taskgroup_t *parent_taskgroup =
5536 this_record_map[j].parent_task->td_taskgroup;
5538 KMP_ATOMIC_ST_RLX(&this_record_map[j].npredecessors_counter,
5539 this_record_map[j].npredecessors);
5540 KMP_ATOMIC_INC(&this_record_map[j].parent_task->td_incomplete_child_tasks);
5542 if (parent_taskgroup) {
5543 KMP_ATOMIC_INC(&parent_taskgroup->count);
5545 td->td_taskgroup = parent_taskgroup;
5546 }
else if (td->td_taskgroup !=
nullptr) {
5548 td->td_taskgroup =
nullptr;
5550 if (this_record_map[j].parent_task->td_flags.tasktype == TASK_EXPLICIT)
5551 KMP_ATOMIC_INC(&this_record_map[j].parent_task->td_allocated_child_tasks);
5554 for (kmp_int32 j = 0; j < this_num_roots; ++j) {
5555 __kmp_omp_task(gtid, this_record_map[this_root_tasks[j]].task,
true);
5557 KA_TRACE(10, (
"__kmp_exec_tdg(exit): T#%d tdg_id=%d num_roots=%d\n", gtid,
5558 tdg->tdg_id, tdg->num_roots));
5566static inline void __kmp_start_record(kmp_int32 gtid,
5567 kmp_taskgraph_flags_t *flags,
5569 kmp_tdg_info_t *tdg =
5570 (kmp_tdg_info_t *)__kmp_allocate(
sizeof(kmp_tdg_info_t));
5571 __kmp_global_tdgs[__kmp_curr_tdg_idx] = tdg;
5573 tdg->tdg_id = tdg_id;
5574 tdg->map_size = INIT_MAPSIZE;
5575 tdg->num_roots = -1;
5576 tdg->root_tasks =
nullptr;
5577 tdg->tdg_status = KMP_TDG_RECORDING;
5578 tdg->rec_num_taskred = 0;
5579 tdg->rec_taskred_data =
nullptr;
5580 KMP_ATOMIC_ST_RLX(&tdg->num_tasks, 0);
5583 kmp_node_info_t *this_record_map =
5584 (kmp_node_info_t *)__kmp_allocate(INIT_MAPSIZE *
sizeof(kmp_node_info_t));
5585 for (kmp_int32 i = 0; i < INIT_MAPSIZE; i++) {
5586 kmp_int32 *successorsList =
5587 (kmp_int32 *)__kmp_allocate(__kmp_successors_size *
sizeof(kmp_int32));
5588 this_record_map[i].task =
nullptr;
5589 this_record_map[i].successors = successorsList;
5590 this_record_map[i].nsuccessors = 0;
5591 this_record_map[i].npredecessors = 0;
5592 this_record_map[i].successors_size = __kmp_successors_size;
5593 KMP_ATOMIC_ST_RLX(&this_record_map[i].npredecessors_counter, 0);
5596 __kmp_global_tdgs[__kmp_curr_tdg_idx]->record_map = this_record_map;
5606kmp_int32 __kmpc_start_record_task(
ident_t *loc_ref, kmp_int32 gtid,
5607 kmp_int32 input_flags, kmp_int32 tdg_id) {
5610 kmp_taskgraph_flags_t *flags = (kmp_taskgraph_flags_t *)&input_flags;
5612 (
"__kmpc_start_record_task(enter): T#%d loc=%p flags=%d tdg_id=%d\n",
5613 gtid, loc_ref, input_flags, tdg_id));
5615 if (__kmp_max_tdgs == 0) {
5618 (
"__kmpc_start_record_task(abandon): T#%d loc=%p flags=%d tdg_id = %d, "
5619 "__kmp_max_tdgs = 0\n",
5620 gtid, loc_ref, input_flags, tdg_id));
5624 __kmpc_taskgroup(loc_ref, gtid);
5625 if (kmp_tdg_info_t *tdg = __kmp_find_tdg(tdg_id)) {
5627 __kmp_exec_tdg(gtid, tdg);
5630 __kmp_curr_tdg_idx = tdg_id;
5631 KMP_DEBUG_ASSERT(__kmp_curr_tdg_idx < __kmp_max_tdgs);
5632 __kmp_start_record(gtid, flags, tdg_id);
5636 KA_TRACE(10, (
"__kmpc_start_record_task(exit): T#%d TDG %d starts to %s\n",
5637 gtid, tdg_id, res ?
"record" :
"execute"));
5644void __kmp_end_record(kmp_int32 gtid, kmp_tdg_info_t *tdg) {
5646 kmp_node_info_t *this_record_map = tdg->record_map;
5647 kmp_int32 this_num_tasks = KMP_ATOMIC_LD_RLX(&tdg->num_tasks);
5648 kmp_int32 *this_root_tasks =
5649 (kmp_int32 *)__kmp_allocate(this_num_tasks *
sizeof(kmp_int32));
5650 kmp_int32 this_map_size = tdg->map_size;
5651 kmp_int32 this_num_roots = 0;
5652 kmp_info_t *thread = __kmp_threads[gtid];
5654 for (kmp_int32 i = 0; i < this_num_tasks; i++) {
5655 if (this_record_map[i].npredecessors == 0) {
5656 this_root_tasks[this_num_roots++] = i;
5661 tdg->map_size = this_map_size;
5662 tdg->num_roots = this_num_roots;
5663 tdg->root_tasks = this_root_tasks;
5664 KMP_DEBUG_ASSERT(tdg->tdg_status == KMP_TDG_RECORDING);
5665 tdg->tdg_status = KMP_TDG_READY;
5667 if (thread->th.th_current_task->td_dephash) {
5668 __kmp_dephash_free(thread, thread->th.th_current_task->td_dephash);
5669 thread->th.th_current_task->td_dephash = NULL;
5673 for (kmp_int32 i = 0; i < this_num_tasks; i++) {
5674 KMP_ATOMIC_ST_RLX(&this_record_map[i].npredecessors_counter,
5675 this_record_map[i].npredecessors);
5677 KMP_ATOMIC_ST_RLX(&__kmp_tdg_task_id, 0);
5680 __kmp_print_tdg_dot(tdg, gtid);
5690void __kmpc_end_record_task(
ident_t *loc_ref, kmp_int32 gtid,
5691 kmp_int32 input_flags, kmp_int32 tdg_id) {
5692 kmp_tdg_info_t *tdg = __kmp_find_tdg(tdg_id);
5694 KA_TRACE(10, (
"__kmpc_end_record_task(enter): T#%d loc=%p finishes recording"
5695 " tdg=%d with flags=%d\n",
5696 gtid, loc_ref, tdg_id, input_flags));
5697 if (__kmp_max_tdgs) {
5699 __kmpc_end_taskgroup(loc_ref, gtid);
5700 if (__kmp_tdg_is_recording(tdg->tdg_status))
5701 __kmp_end_record(gtid, tdg);
5703 KA_TRACE(10, (
"__kmpc_end_record_task(exit): T#%d loc=%p finished recording"
5704 " tdg=%d, its status is now READY\n",
5705 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