17#include "kmp_wait_release.h"
18#include "kmp_taskdeps.h"
21#include "ompt-specific.h"
24#if ENABLE_LIBOMPTARGET
25static void (*tgt_target_nowait_query)(
void **);
27void __kmp_init_target_task() {
28 *(
void **)(&tgt_target_nowait_query) = KMP_DLSYM(
"__tgt_target_nowait_query");
33static void __kmp_enable_tasking(kmp_task_team_t *task_team,
34 kmp_info_t *this_thr);
35static void __kmp_alloc_task_deque(kmp_info_t *thread,
36 kmp_thread_data_t *thread_data);
37static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
38 kmp_task_team_t *task_team);
39static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask);
41static kmp_tdg_info_t *__kmp_find_tdg(kmp_int32 tdg_id);
42int __kmp_taskloop_task(
int gtid,
void *ptask);
45#ifdef BUILD_TIED_TASK_STACK
54static void __kmp_trace_task_stack(kmp_int32 gtid,
55 kmp_thread_data_t *thread_data,
56 int threshold,
char *location) {
57 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
58 kmp_taskdata_t **stack_top = task_stack->ts_top;
59 kmp_int32 entries = task_stack->ts_entries;
60 kmp_taskdata_t *tied_task;
64 (
"__kmp_trace_task_stack(start): location = %s, gtid = %d, entries = %d, "
65 "first_block = %p, stack_top = %p \n",
66 location, gtid, entries, task_stack->ts_first_block, stack_top));
68 KMP_DEBUG_ASSERT(stack_top != NULL);
69 KMP_DEBUG_ASSERT(entries > 0);
71 while (entries != 0) {
72 KMP_DEBUG_ASSERT(stack_top != &task_stack->ts_first_block.sb_block[0]);
74 if (entries & TASK_STACK_INDEX_MASK == 0) {
75 kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(stack_top);
77 stack_block = stack_block->sb_prev;
78 stack_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE];
85 tied_task = *stack_top;
87 KMP_DEBUG_ASSERT(tied_task != NULL);
88 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
91 (
"__kmp_trace_task_stack(%s): gtid=%d, entry=%d, "
92 "stack_top=%p, tied_task=%p\n",
93 location, gtid, entries, stack_top, tied_task));
95 KMP_DEBUG_ASSERT(stack_top == &task_stack->ts_first_block.sb_block[0]);
98 (
"__kmp_trace_task_stack(exit): location = %s, gtid = %d\n",
108static void __kmp_init_task_stack(kmp_int32 gtid,
109 kmp_thread_data_t *thread_data) {
110 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
111 kmp_stack_block_t *first_block;
114 first_block = &task_stack->ts_first_block;
115 task_stack->ts_top = (kmp_taskdata_t **)first_block;
116 memset((
void *)first_block,
'\0',
117 TASK_STACK_BLOCK_SIZE *
sizeof(kmp_taskdata_t *));
120 task_stack->ts_entries = TASK_STACK_EMPTY;
121 first_block->sb_next = NULL;
122 first_block->sb_prev = NULL;
129static void __kmp_free_task_stack(kmp_int32 gtid,
130 kmp_thread_data_t *thread_data) {
131 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
132 kmp_stack_block_t *stack_block = &task_stack->ts_first_block;
134 KMP_DEBUG_ASSERT(task_stack->ts_entries == TASK_STACK_EMPTY);
136 while (stack_block != NULL) {
137 kmp_stack_block_t *next_block = (stack_block) ? stack_block->sb_next : NULL;
139 stack_block->sb_next = NULL;
140 stack_block->sb_prev = NULL;
141 if (stack_block != &task_stack->ts_first_block) {
142 __kmp_thread_free(thread,
145 stack_block = next_block;
148 task_stack->ts_entries = 0;
149 task_stack->ts_top = NULL;
158static void __kmp_push_task_stack(kmp_int32 gtid, kmp_info_t *thread,
159 kmp_taskdata_t *tied_task) {
161 kmp_thread_data_t *thread_data =
162 &thread->th.th_task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
163 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
165 if (tied_task->td_flags.team_serial || tied_task->td_flags.tasking_ser) {
169 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
170 KMP_DEBUG_ASSERT(task_stack->ts_top != NULL);
173 (
"__kmp_push_task_stack(enter): GTID: %d; THREAD: %p; TASK: %p\n",
174 gtid, thread, tied_task));
176 *(task_stack->ts_top) = tied_task;
179 task_stack->ts_top++;
180 task_stack->ts_entries++;
182 if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) {
184 kmp_stack_block_t *stack_block =
185 (kmp_stack_block_t *)(task_stack->ts_top - TASK_STACK_BLOCK_SIZE);
188 if (stack_block->sb_next !=
190 task_stack->ts_top = &stack_block->sb_next->sb_block[0];
192 kmp_stack_block_t *new_block = (kmp_stack_block_t *)__kmp_thread_calloc(
193 thread,
sizeof(kmp_stack_block_t));
195 task_stack->ts_top = &new_block->sb_block[0];
196 stack_block->sb_next = new_block;
197 new_block->sb_prev = stack_block;
198 new_block->sb_next = NULL;
202 (
"__kmp_push_task_stack(): GTID: %d; TASK: %p; Alloc new block: %p\n",
203 gtid, tied_task, new_block));
206 KA_TRACE(20, (
"__kmp_push_task_stack(exit): GTID: %d; TASK: %p\n", gtid,
217static void __kmp_pop_task_stack(kmp_int32 gtid, kmp_info_t *thread,
218 kmp_taskdata_t *ending_task) {
220 kmp_thread_data_t *thread_data =
221 &thread->th.th_task_team->tt_threads_data[__kmp_tid_from_gtid(gtid)];
222 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
223 kmp_taskdata_t *tied_task;
225 if (ending_task->td_flags.team_serial || ending_task->td_flags.tasking_ser) {
230 KMP_DEBUG_ASSERT(task_stack->ts_top != NULL);
231 KMP_DEBUG_ASSERT(task_stack->ts_entries > 0);
233 KA_TRACE(20, (
"__kmp_pop_task_stack(enter): GTID: %d; THREAD: %p\n", gtid,
237 if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) {
238 kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(task_stack->ts_top);
240 stack_block = stack_block->sb_prev;
241 task_stack->ts_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE];
245 task_stack->ts_top--;
246 task_stack->ts_entries--;
248 tied_task = *(task_stack->ts_top);
250 KMP_DEBUG_ASSERT(tied_task != NULL);
251 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
252 KMP_DEBUG_ASSERT(tied_task == ending_task);
254 KA_TRACE(20, (
"__kmp_pop_task_stack(exit): GTID: %d; TASK: %p\n", gtid,
263static bool __kmp_task_is_allowed(
int gtid,
const kmp_int32 is_constrained,
264 const kmp_taskdata_t *tasknew,
265 const kmp_taskdata_t *taskcurr) {
266 if (is_constrained && (tasknew->td_flags.tiedness == TASK_TIED)) {
270 kmp_taskdata_t *current = taskcurr->td_last_tied;
271 KMP_DEBUG_ASSERT(current != NULL);
273 if (current->td_flags.tasktype == TASK_EXPLICIT ||
274 current->td_taskwait_thread > 0) {
275 kmp_int32 level = current->td_level;
276 kmp_taskdata_t *parent = tasknew->td_parent;
277 while (parent != current && parent->td_level > level) {
279 parent = parent->td_parent;
280 KMP_DEBUG_ASSERT(parent != NULL);
282 if (parent != current)
287 kmp_depnode_t *node = tasknew->td_depnode;
289 if (!tasknew->is_taskgraph && UNLIKELY(node && (node->dn.mtx_num_locks > 0))) {
291 if (UNLIKELY(node && (node->dn.mtx_num_locks > 0))) {
293 for (
int i = 0; i < node->dn.mtx_num_locks; ++i) {
294 KMP_DEBUG_ASSERT(node->dn.mtx_locks[i] != NULL);
295 if (__kmp_test_lock(node->dn.mtx_locks[i], gtid))
298 for (
int j = i - 1; j >= 0; --j)
299 __kmp_release_lock(node->dn.mtx_locks[j], gtid);
303 node->dn.mtx_num_locks = -node->dn.mtx_num_locks;
312static void __kmp_realloc_task_deque(kmp_info_t *thread,
313 kmp_thread_data_t *thread_data) {
314 kmp_int32 size = TASK_DEQUE_SIZE(thread_data->td);
315 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == size);
316 kmp_int32 new_size = 2 * size;
318 KE_TRACE(10, (
"__kmp_realloc_task_deque: T#%d reallocating deque[from %d to "
319 "%d] for thread_data %p\n",
320 __kmp_gtid_from_thread(thread), size, new_size, thread_data));
322 kmp_taskdata_t **new_deque =
323 (kmp_taskdata_t **)__kmp_allocate(new_size *
sizeof(kmp_taskdata_t *));
326 for (i = thread_data->td.td_deque_head, j = 0; j < size;
327 i = (i + 1) & TASK_DEQUE_MASK(thread_data->td), j++)
328 new_deque[j] = thread_data->td.td_deque[i];
330 __kmp_free(thread_data->td.td_deque);
332 thread_data->td.td_deque_head = 0;
333 thread_data->td.td_deque_tail = size;
334 thread_data->td.td_deque = new_deque;
335 thread_data->td.td_deque_size = new_size;
338static kmp_task_pri_t *__kmp_alloc_task_pri_list() {
339 kmp_task_pri_t *l = (kmp_task_pri_t *)__kmp_allocate(
sizeof(kmp_task_pri_t));
340 kmp_thread_data_t *thread_data = &l->td;
341 __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
342 thread_data->td.td_deque_last_stolen = -1;
343 KE_TRACE(20, (
"__kmp_alloc_task_pri_list: T#%d allocating deque[%d] "
344 "for thread_data %p\n",
345 __kmp_get_gtid(), INITIAL_TASK_DEQUE_SIZE, thread_data));
346 thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
347 INITIAL_TASK_DEQUE_SIZE *
sizeof(kmp_taskdata_t *));
348 thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
357static kmp_thread_data_t *
358__kmp_get_priority_deque_data(kmp_task_team_t *task_team, kmp_int32 pri) {
359 kmp_thread_data_t *thread_data;
360 kmp_task_pri_t *lst = task_team->tt.tt_task_pri_list;
361 if (lst->priority == pri) {
363 thread_data = &lst->td;
364 }
else if (lst->priority < pri) {
367 kmp_task_pri_t *list = __kmp_alloc_task_pri_list();
368 thread_data = &list->td;
369 list->priority = pri;
371 task_team->tt.tt_task_pri_list = list;
373 kmp_task_pri_t *next_queue = lst->next;
374 while (next_queue && next_queue->priority > pri) {
376 next_queue = lst->next;
379 if (next_queue == NULL) {
381 kmp_task_pri_t *list = __kmp_alloc_task_pri_list();
382 thread_data = &list->td;
383 list->priority = pri;
386 }
else if (next_queue->priority == pri) {
388 thread_data = &next_queue->td;
391 kmp_task_pri_t *list = __kmp_alloc_task_pri_list();
392 thread_data = &list->td;
393 list->priority = pri;
394 list->next = next_queue;
402static kmp_int32 __kmp_push_priority_task(kmp_int32 gtid, kmp_info_t *thread,
403 kmp_taskdata_t *taskdata,
404 kmp_task_team_t *task_team,
406 kmp_thread_data_t *thread_data = NULL;
408 (
"__kmp_push_priority_task: T#%d trying to push task %p, pri %d.\n",
409 gtid, taskdata, pri));
412 kmp_task_pri_t *lst = task_team->tt.tt_task_pri_list;
413 if (UNLIKELY(lst == NULL)) {
414 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
415 if (task_team->tt.tt_task_pri_list == NULL) {
417 kmp_task_pri_t *list = __kmp_alloc_task_pri_list();
418 thread_data = &list->td;
419 list->priority = pri;
421 task_team->tt.tt_task_pri_list = list;
424 thread_data = __kmp_get_priority_deque_data(task_team, pri);
426 __kmp_release_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
428 if (lst->priority == pri) {
430 thread_data = &lst->td;
432 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
433 thread_data = __kmp_get_priority_deque_data(task_team, pri);
434 __kmp_release_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
437 KMP_DEBUG_ASSERT(thread_data);
439 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
441 if (TCR_4(thread_data->td.td_deque_ntasks) >=
442 TASK_DEQUE_SIZE(thread_data->td)) {
443 if (__kmp_enable_task_throttling &&
444 __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
445 thread->th.th_current_task)) {
446 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
447 KA_TRACE(20, (
"__kmp_push_priority_task: T#%d deque is full; returning "
448 "TASK_NOT_PUSHED for task %p\n",
450 return TASK_NOT_PUSHED;
453 __kmp_realloc_task_deque(thread, thread_data);
456 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) <
457 TASK_DEQUE_SIZE(thread_data->td));
459 thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
461 thread_data->td.td_deque_tail =
462 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
463 TCW_4(thread_data->td.td_deque_ntasks,
464 TCR_4(thread_data->td.td_deque_ntasks) + 1);
465 KMP_FSYNC_RELEASING(thread->th.th_current_task);
466 KMP_FSYNC_RELEASING(taskdata);
467 KA_TRACE(20, (
"__kmp_push_priority_task: T#%d returning "
468 "TASK_SUCCESSFULLY_PUSHED: task=%p ntasks=%d head=%u tail=%u\n",
469 gtid, taskdata, thread_data->td.td_deque_ntasks,
470 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
471 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
472 task_team->tt.tt_num_task_pri++;
473 return TASK_SUCCESSFULLY_PUSHED;
477static kmp_int32 __kmp_push_task(kmp_int32 gtid, kmp_task_t *task) {
478 kmp_info_t *thread = __kmp_threads[gtid];
479 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
484 if (UNLIKELY(taskdata->td_flags.hidden_helper &&
485 !KMP_HIDDEN_HELPER_THREAD(gtid))) {
486 kmp_int32 shadow_gtid = KMP_GTID_TO_SHADOW_GTID(gtid);
487 __kmpc_give_task(task, __kmp_tid_from_gtid(shadow_gtid));
489 __kmp_hidden_helper_worker_thread_signal();
490 return TASK_SUCCESSFULLY_PUSHED;
493 kmp_task_team_t *task_team = thread->th.th_task_team;
494 kmp_int32 tid = __kmp_tid_from_gtid(gtid);
495 kmp_thread_data_t *thread_data;
498 (
"__kmp_push_task: T#%d trying to push task %p.\n", gtid, taskdata));
500 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
503 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
504 KMP_DEBUG_USE_VAR(counter);
507 (
"__kmp_push_task: T#%d untied_count (%d) incremented for task %p\n",
508 gtid, counter, taskdata));
512 if (UNLIKELY(taskdata->td_flags.task_serial)) {
513 KA_TRACE(20, (
"__kmp_push_task: T#%d team serialized; returning "
514 "TASK_NOT_PUSHED for task %p\n",
516 return TASK_NOT_PUSHED;
521 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
522 if (UNLIKELY(!KMP_TASKING_ENABLED(task_team))) {
523 __kmp_enable_tasking(task_team, thread);
525 KMP_DEBUG_ASSERT(TCR_4(task_team->tt.tt_found_tasks) == TRUE);
526 KMP_DEBUG_ASSERT(TCR_PTR(task_team->tt.tt_threads_data) != NULL);
528 if (taskdata->td_flags.priority_specified && task->data2.priority > 0 &&
529 __kmp_max_task_priority > 0) {
530 int pri = KMP_MIN(task->data2.priority, __kmp_max_task_priority);
531 return __kmp_push_priority_task(gtid, thread, taskdata, task_team, pri);
535 thread_data = &task_team->tt.tt_threads_data[tid];
540 if (UNLIKELY(thread_data->td.td_deque == NULL)) {
541 __kmp_alloc_task_deque(thread, thread_data);
546 if (TCR_4(thread_data->td.td_deque_ntasks) >=
547 TASK_DEQUE_SIZE(thread_data->td)) {
548 if (__kmp_enable_task_throttling &&
549 __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
550 thread->th.th_current_task)) {
551 KA_TRACE(20, (
"__kmp_push_task: T#%d deque is full; returning "
552 "TASK_NOT_PUSHED for task %p\n",
554 return TASK_NOT_PUSHED;
556 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
558 if (TCR_4(thread_data->td.td_deque_ntasks) >=
559 TASK_DEQUE_SIZE(thread_data->td)) {
561 __kmp_realloc_task_deque(thread, thread_data);
567 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
569 if (TCR_4(thread_data->td.td_deque_ntasks) >=
570 TASK_DEQUE_SIZE(thread_data->td)) {
571 if (__kmp_enable_task_throttling &&
572 __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
573 thread->th.th_current_task)) {
574 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
575 KA_TRACE(20, (
"__kmp_push_task: T#%d deque is full on 2nd check; "
576 "returning TASK_NOT_PUSHED for task %p\n",
578 return TASK_NOT_PUSHED;
581 __kmp_realloc_task_deque(thread, thread_data);
586 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) <
587 TASK_DEQUE_SIZE(thread_data->td));
589 thread_data->td.td_deque[thread_data->td.td_deque_tail] =
592 thread_data->td.td_deque_tail =
593 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
594 TCW_4(thread_data->td.td_deque_ntasks,
595 TCR_4(thread_data->td.td_deque_ntasks) + 1);
596 KMP_FSYNC_RELEASING(thread->th.th_current_task);
597 KMP_FSYNC_RELEASING(taskdata);
598 KA_TRACE(20, (
"__kmp_push_task: T#%d returning TASK_SUCCESSFULLY_PUSHED: "
599 "task=%p ntasks=%d head=%u tail=%u\n",
600 gtid, taskdata, thread_data->td.td_deque_ntasks,
601 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
603 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
605 return TASK_SUCCESSFULLY_PUSHED;
612void __kmp_pop_current_task_from_thread(kmp_info_t *this_thr) {
613 KF_TRACE(10, (
"__kmp_pop_current_task_from_thread(enter): T#%d "
614 "this_thread=%p, curtask=%p, "
615 "curtask_parent=%p\n",
616 0, this_thr, this_thr->th.th_current_task,
617 this_thr->th.th_current_task->td_parent));
619 this_thr->th.th_current_task = this_thr->th.th_current_task->td_parent;
621 KF_TRACE(10, (
"__kmp_pop_current_task_from_thread(exit): T#%d "
622 "this_thread=%p, curtask=%p, "
623 "curtask_parent=%p\n",
624 0, this_thr, this_thr->th.th_current_task,
625 this_thr->th.th_current_task->td_parent));
634void __kmp_push_current_task_to_thread(kmp_info_t *this_thr, kmp_team_t *team,
638 KF_TRACE(10, (
"__kmp_push_current_task_to_thread(enter): T#%d this_thread=%p "
641 tid, this_thr, this_thr->th.th_current_task,
642 team->t.t_implicit_task_taskdata[tid].td_parent));
644 KMP_DEBUG_ASSERT(this_thr != NULL);
647 if (this_thr->th.th_current_task != &team->t.t_implicit_task_taskdata[0]) {
648 team->t.t_implicit_task_taskdata[0].td_parent =
649 this_thr->th.th_current_task;
650 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[0];
653 team->t.t_implicit_task_taskdata[tid].td_parent =
654 team->t.t_implicit_task_taskdata[0].td_parent;
655 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[tid];
658 KF_TRACE(10, (
"__kmp_push_current_task_to_thread(exit): T#%d this_thread=%p "
661 tid, this_thr, this_thr->th.th_current_task,
662 team->t.t_implicit_task_taskdata[tid].td_parent));
670static void __kmp_task_start(kmp_int32 gtid, kmp_task_t *task,
671 kmp_taskdata_t *current_task) {
672 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
673 kmp_info_t *thread = __kmp_threads[gtid];
676 (
"__kmp_task_start(enter): T#%d starting task %p: current_task=%p\n",
677 gtid, taskdata, current_task));
679 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
684 current_task->td_flags.executing = 0;
687#ifdef BUILD_TIED_TASK_STACK
688 if (taskdata->td_flags.tiedness == TASK_TIED) {
689 __kmp_push_task_stack(gtid, thread, taskdata);
694 thread->th.th_current_task = taskdata;
696 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 0 ||
697 taskdata->td_flags.tiedness == TASK_UNTIED);
698 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0 ||
699 taskdata->td_flags.tiedness == TASK_UNTIED);
700 taskdata->td_flags.started = 1;
701 taskdata->td_flags.executing = 1;
702 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
703 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
710 KA_TRACE(10, (
"__kmp_task_start(exit): T#%d task=%p\n", gtid, taskdata));
721static inline void __ompt_task_init(kmp_taskdata_t *task,
int tid) {
723 task->ompt_task_info.task_data.value = 0;
724 task->ompt_task_info.frame.exit_frame = ompt_data_none;
725 task->ompt_task_info.frame.enter_frame = ompt_data_none;
726 task->ompt_task_info.frame.exit_frame_flags =
727 ompt_frame_runtime | ompt_frame_framepointer;
728 task->ompt_task_info.frame.enter_frame_flags =
729 ompt_frame_runtime | ompt_frame_framepointer;
730 task->ompt_task_info.dispatch_chunk.start = 0;
731 task->ompt_task_info.dispatch_chunk.iterations = 0;
736static inline void __ompt_task_start(kmp_task_t *task,
737 kmp_taskdata_t *current_task,
739 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
740 ompt_task_status_t status = ompt_task_switch;
741 if (__kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded) {
742 status = ompt_task_yield;
743 __kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded = 0;
746 if (ompt_enabled.ompt_callback_task_schedule) {
747 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
748 &(current_task->ompt_task_info.task_data), status,
749 &(taskdata->ompt_task_info.task_data));
751 taskdata->ompt_task_info.scheduling_parent = current_task;
756static inline void __ompt_task_finish(kmp_task_t *task,
757 kmp_taskdata_t *resumed_task,
758 ompt_task_status_t status) {
759 if (ompt_enabled.ompt_callback_task_schedule) {
760 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
761 if (__kmp_omp_cancellation && taskdata->td_taskgroup &&
762 taskdata->td_taskgroup->cancel_request == cancel_taskgroup) {
763 status = ompt_task_cancel;
767 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
768 &(taskdata->ompt_task_info.task_data), status,
769 (resumed_task ? &(resumed_task->ompt_task_info.task_data) : NULL));
775static void __kmpc_omp_task_begin_if0_template(
ident_t *loc_ref, kmp_int32 gtid,
778 void *return_address) {
779 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
780 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
782 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(enter): T#%d loc=%p task=%p "
784 gtid, loc_ref, taskdata, current_task));
786 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
789 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
790 KMP_DEBUG_USE_VAR(counter);
791 KA_TRACE(20, (
"__kmpc_omp_task_begin_if0: T#%d untied_count (%d) "
792 "incremented for task %p\n",
793 gtid, counter, taskdata));
796 taskdata->td_flags.task_serial =
798 __kmp_task_start(gtid, task, current_task);
802 if (current_task->ompt_task_info.frame.enter_frame.ptr == NULL) {
803 current_task->ompt_task_info.frame.enter_frame.ptr =
804 taskdata->ompt_task_info.frame.exit_frame.ptr = frame_address;
805 current_task->ompt_task_info.frame.enter_frame_flags =
806 taskdata->ompt_task_info.frame.exit_frame_flags =
807 ompt_frame_application | ompt_frame_framepointer;
809 if (ompt_enabled.ompt_callback_task_create) {
810 ompt_task_info_t *parent_info = &(current_task->ompt_task_info);
811 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
812 &(parent_info->task_data), &(parent_info->frame),
813 &(taskdata->ompt_task_info.task_data),
814 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(taskdata), 0,
817 __ompt_task_start(task, current_task, gtid);
821 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(exit): T#%d loc=%p task=%p,\n", gtid,
827static void __kmpc_omp_task_begin_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
830 void *return_address) {
831 __kmpc_omp_task_begin_if0_template<true>(loc_ref, gtid, task, frame_address,
842void __kmpc_omp_task_begin_if0(
ident_t *loc_ref, kmp_int32 gtid,
845 if (UNLIKELY(ompt_enabled.enabled)) {
846 OMPT_STORE_RETURN_ADDRESS(gtid);
847 __kmpc_omp_task_begin_if0_ompt(loc_ref, gtid, task,
848 OMPT_GET_FRAME_ADDRESS(1),
849 OMPT_LOAD_RETURN_ADDRESS(gtid));
853 __kmpc_omp_task_begin_if0_template<false>(loc_ref, gtid, task, NULL, NULL);
859void __kmpc_omp_task_begin(
ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task) {
860 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
864 (
"__kmpc_omp_task_begin(enter): T#%d loc=%p task=%p current_task=%p\n",
865 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task), current_task));
867 __kmp_task_start(gtid, task, current_task);
869 KA_TRACE(10, (
"__kmpc_omp_task_begin(exit): T#%d loc=%p task=%p,\n", gtid,
870 loc_ref, KMP_TASK_TO_TASKDATA(task)));
880static void __kmp_free_task(kmp_int32 gtid, kmp_taskdata_t *taskdata,
881 kmp_info_t *thread) {
882 KA_TRACE(30, (
"__kmp_free_task: T#%d freeing data from task %p\n", gtid,
886 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
887 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0);
888 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 1);
889 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
890 KMP_DEBUG_ASSERT(taskdata->td_allocated_child_tasks == 0 ||
891 taskdata->td_flags.task_serial == 1);
892 KMP_DEBUG_ASSERT(taskdata->td_incomplete_child_tasks == 0);
893 kmp_task_t *task = KMP_TASKDATA_TO_TASK(taskdata);
895 task->data1.destructors = NULL;
896 task->data2.priority = 0;
898 taskdata->td_flags.freed = 1;
901 if (!taskdata->is_taskgraph) {
905 __kmp_fast_free(thread, taskdata);
907 __kmp_thread_free(thread, taskdata);
911 taskdata->td_flags.complete = 0;
912 taskdata->td_flags.started = 0;
913 taskdata->td_flags.freed = 0;
914 taskdata->td_flags.executing = 0;
915 taskdata->td_flags.task_serial =
916 (taskdata->td_parent->td_flags.final ||
917 taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser);
920 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
921 KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0);
923 KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1);
927 KA_TRACE(20, (
"__kmp_free_task: T#%d freed task %p\n", gtid, taskdata));
936static void __kmp_free_task_and_ancestors(kmp_int32 gtid,
937 kmp_taskdata_t *taskdata,
938 kmp_info_t *thread) {
941 kmp_int32 team_serial =
942 (taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) &&
943 !taskdata->td_flags.proxy;
944 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
946 kmp_int32 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
947 KMP_DEBUG_ASSERT(children >= 0);
950 while (children == 0) {
951 kmp_taskdata_t *parent_taskdata = taskdata->td_parent;
953 KA_TRACE(20, (
"__kmp_free_task_and_ancestors(enter): T#%d task %p complete "
954 "and freeing itself\n",
958 __kmp_free_task(gtid, taskdata, thread);
960 taskdata = parent_taskdata;
966 if (taskdata->td_flags.tasktype == TASK_IMPLICIT) {
967 if (taskdata->td_dephash) {
968 int children = KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks);
969 kmp_tasking_flags_t flags_old = taskdata->td_flags;
970 if (children == 0 && flags_old.complete == 1) {
971 kmp_tasking_flags_t flags_new = flags_old;
972 flags_new.complete = 0;
973 if (KMP_COMPARE_AND_STORE_ACQ32(
974 RCAST(kmp_int32 *, &taskdata->td_flags),
975 *RCAST(kmp_int32 *, &flags_old),
976 *RCAST(kmp_int32 *, &flags_new))) {
977 KA_TRACE(100, (
"__kmp_free_task_and_ancestors: T#%d cleans "
978 "dephash of implicit task %p\n",
981 __kmp_dephash_free_entries(thread, taskdata->td_dephash);
988 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
989 KMP_DEBUG_ASSERT(children >= 0);
993 20, (
"__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; "
994 "not freeing it yet\n",
995 gtid, taskdata, children));
1006static bool __kmp_track_children_task(kmp_taskdata_t *taskdata) {
1007 kmp_tasking_flags_t flags = taskdata->td_flags;
1008 bool ret = !(flags.team_serial || flags.tasking_ser);
1009 ret = ret || flags.proxy == TASK_PROXY ||
1010 flags.detachable == TASK_DETACHABLE || flags.hidden_helper;
1012 KMP_ATOMIC_LD_ACQ(&taskdata->td_parent->td_incomplete_child_tasks) > 0;
1014 if (taskdata->td_taskgroup && taskdata->is_taskgraph)
1015 ret = ret || KMP_ATOMIC_LD_ACQ(&taskdata->td_taskgroup->count) > 0;
1030static void __kmp_task_finish(kmp_int32 gtid, kmp_task_t *task,
1031 kmp_taskdata_t *resumed_task) {
1032 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
1033 kmp_info_t *thread = __kmp_threads[gtid];
1034 kmp_task_team_t *task_team =
1035 thread->th.th_task_team;
1041 kmp_int32 children = 0;
1043 KA_TRACE(10, (
"__kmp_task_finish(enter): T#%d finishing task %p and resuming "
1045 gtid, taskdata, resumed_task));
1047 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
1050 is_taskgraph = taskdata->is_taskgraph;
1054#ifdef BUILD_TIED_TASK_STACK
1055 if (taskdata->td_flags.tiedness == TASK_TIED) {
1056 __kmp_pop_task_stack(gtid, thread, taskdata);
1060 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
1063 kmp_int32 counter = KMP_ATOMIC_DEC(&taskdata->td_untied_count) - 1;
1066 (
"__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n",
1067 gtid, counter, taskdata));
1071 if (resumed_task == NULL) {
1072 KMP_DEBUG_ASSERT(taskdata->td_flags.task_serial);
1073 resumed_task = taskdata->td_parent;
1076 thread->th.th_current_task = resumed_task;
1077 resumed_task->td_flags.executing = 1;
1078 KA_TRACE(10, (
"__kmp_task_finish(exit): T#%d partially done task %p, "
1079 "resuming task %p\n",
1080 gtid, taskdata, resumed_task));
1088 (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) ==
1089 taskdata->td_flags.task_serial);
1090 if (taskdata->td_flags.task_serial) {
1091 if (resumed_task == NULL) {
1092 resumed_task = taskdata->td_parent;
1096 KMP_DEBUG_ASSERT(resumed_task !=
1106 if (UNLIKELY(taskdata->td_flags.destructors_thunk)) {
1107 kmp_routine_entry_t destr_thunk = task->data1.destructors;
1108 KMP_ASSERT(destr_thunk);
1109 destr_thunk(gtid, task);
1112 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
1113 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 1);
1114 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
1116 bool completed =
true;
1117 if (UNLIKELY(taskdata->td_flags.detachable == TASK_DETACHABLE)) {
1118 if (taskdata->td_allow_completion_event.type ==
1119 KMP_EVENT_ALLOW_COMPLETION) {
1121 __kmp_acquire_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
1122 if (taskdata->td_allow_completion_event.type ==
1123 KMP_EVENT_ALLOW_COMPLETION) {
1125 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
1126 taskdata->td_flags.executing = 0;
1133 __ompt_task_finish(task, resumed_task, ompt_task_detach);
1139 taskdata->td_flags.proxy = TASK_PROXY;
1142 __kmp_release_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
1147 if (taskdata->td_target_data.async_handle != NULL) {
1151 __kmpc_give_task(task, __kmp_tid_from_gtid(gtid));
1152 if (KMP_HIDDEN_HELPER_THREAD(gtid))
1153 __kmp_hidden_helper_worker_thread_signal();
1158 taskdata->td_flags.complete = 1;
1160 taskdata->td_flags.onced = 1;
1166 __ompt_task_finish(task, resumed_task, ompt_task_complete);
1170 if (__kmp_track_children_task(taskdata)) {
1171 __kmp_release_deps(gtid, taskdata);
1176 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks);
1177 KMP_DEBUG_ASSERT(children >= 0);
1179 if (taskdata->td_taskgroup && !taskdata->is_taskgraph)
1181 if (taskdata->td_taskgroup)
1183 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
1184 }
else if (task_team && (task_team->tt.tt_found_proxy_tasks ||
1185 task_team->tt.tt_hidden_helper_task_encountered)) {
1188 __kmp_release_deps(gtid, taskdata);
1194 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
1195 taskdata->td_flags.executing = 0;
1198 if (taskdata->td_flags.hidden_helper) {
1200 KMP_ASSERT(KMP_HIDDEN_HELPER_THREAD(gtid));
1201 KMP_ATOMIC_DEC(&__kmp_unexecuted_hidden_helper_tasks);
1206 20, (
"__kmp_task_finish: T#%d finished task %p, %d incomplete children\n",
1207 gtid, taskdata, children));
1213 thread->th.th_current_task = resumed_task;
1215 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
1219 resumed_task->td_flags.executing = 1;
1222 if (is_taskgraph && __kmp_track_children_task(taskdata) &&
1223 taskdata->td_taskgroup) {
1230 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
1235 10, (
"__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n",
1236 gtid, taskdata, resumed_task));
1242static void __kmpc_omp_task_complete_if0_template(
ident_t *loc_ref,
1245 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n",
1246 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
1247 KMP_DEBUG_ASSERT(gtid >= 0);
1249 __kmp_task_finish<ompt>(gtid, task, NULL);
1251 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n",
1252 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
1256 ompt_frame_t *ompt_frame;
1257 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
1258 ompt_frame->enter_frame = ompt_data_none;
1259 ompt_frame->enter_frame_flags =
1260 ompt_frame_runtime | ompt_frame_framepointer;
1269void __kmpc_omp_task_complete_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
1271 __kmpc_omp_task_complete_if0_template<true>(loc_ref, gtid, task);
1280void __kmpc_omp_task_complete_if0(
ident_t *loc_ref, kmp_int32 gtid,
1283 if (UNLIKELY(ompt_enabled.enabled)) {
1284 __kmpc_omp_task_complete_if0_ompt(loc_ref, gtid, task);
1288 __kmpc_omp_task_complete_if0_template<false>(loc_ref, gtid, task);
1294void __kmpc_omp_task_complete(
ident_t *loc_ref, kmp_int32 gtid,
1296 KA_TRACE(10, (
"__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n", gtid,
1297 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1299 __kmp_task_finish<false>(gtid, task,
1302 KA_TRACE(10, (
"__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n", gtid,
1303 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1319void __kmp_init_implicit_task(
ident_t *loc_ref, kmp_info_t *this_thr,
1320 kmp_team_t *team,
int tid,
int set_curr_task) {
1321 kmp_taskdata_t *task = &team->t.t_implicit_task_taskdata[tid];
1325 (
"__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n",
1326 tid, team, task, set_curr_task ?
"TRUE" :
"FALSE"));
1328 task->td_task_id = KMP_GEN_TASK_ID();
1329 task->td_team = team;
1332 task->td_ident = loc_ref;
1333 task->td_taskwait_ident = NULL;
1334 task->td_taskwait_counter = 0;
1335 task->td_taskwait_thread = 0;
1337 task->td_flags.tiedness = TASK_TIED;
1338 task->td_flags.tasktype = TASK_IMPLICIT;
1339 task->td_flags.proxy = TASK_FULL;
1342 task->td_flags.task_serial = 1;
1343 task->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1344 task->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1346 task->td_flags.started = 1;
1347 task->td_flags.executing = 1;
1348 task->td_flags.complete = 0;
1349 task->td_flags.freed = 0;
1351 task->td_flags.onced = 0;
1354 task->td_depnode = NULL;
1355 task->td_last_tied = task;
1356 task->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1358 if (set_curr_task) {
1359 KMP_ATOMIC_ST_REL(&task->td_incomplete_child_tasks, 0);
1361 KMP_ATOMIC_ST_REL(&task->td_allocated_child_tasks, 0);
1362 task->td_taskgroup = NULL;
1363 task->td_dephash = NULL;
1364 __kmp_push_current_task_to_thread(this_thr, team, tid);
1366 KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0);
1367 KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0);
1371 if (UNLIKELY(ompt_enabled.enabled))
1372 __ompt_task_init(task, tid);
1375 KF_TRACE(10, (
"__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n", tid,
1384void __kmp_finish_implicit_task(kmp_info_t *thread) {
1385 kmp_taskdata_t *task = thread->th.th_current_task;
1386 if (task->td_dephash) {
1388 task->td_flags.complete = 1;
1390 task->td_flags.onced = 1;
1392 children = KMP_ATOMIC_LD_ACQ(&task->td_incomplete_child_tasks);
1393 kmp_tasking_flags_t flags_old = task->td_flags;
1394 if (children == 0 && flags_old.complete == 1) {
1395 kmp_tasking_flags_t flags_new = flags_old;
1396 flags_new.complete = 0;
1397 if (KMP_COMPARE_AND_STORE_ACQ32(RCAST(kmp_int32 *, &task->td_flags),
1398 *RCAST(kmp_int32 *, &flags_old),
1399 *RCAST(kmp_int32 *, &flags_new))) {
1400 KA_TRACE(100, (
"__kmp_finish_implicit_task: T#%d cleans "
1401 "dephash of implicit task %p\n",
1402 thread->th.th_info.ds.ds_gtid, task));
1403 __kmp_dephash_free_entries(thread, task->td_dephash);
1413void __kmp_free_implicit_task(kmp_info_t *thread) {
1414 kmp_taskdata_t *task = thread->th.th_current_task;
1415 if (task && task->td_dephash) {
1416 __kmp_dephash_free(thread, task->td_dephash);
1417 task->td_dephash = NULL;
1423static size_t __kmp_round_up_to_val(
size_t size,
size_t val) {
1424 if (size & (val - 1)) {
1426 if (size <= KMP_SIZE_T_MAX - val) {
1445kmp_task_t *__kmp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1446 kmp_tasking_flags_t *flags,
1447 size_t sizeof_kmp_task_t,
size_t sizeof_shareds,
1448 kmp_routine_entry_t task_entry) {
1450 kmp_taskdata_t *taskdata;
1451 kmp_info_t *thread = __kmp_threads[gtid];
1452 kmp_team_t *team = thread->th.th_team;
1453 kmp_taskdata_t *parent_task = thread->th.th_current_task;
1454 size_t shareds_offset;
1456 if (UNLIKELY(!TCR_4(__kmp_init_middle)))
1457 __kmp_middle_initialize();
1459 if (flags->hidden_helper) {
1460 if (__kmp_enable_hidden_helper) {
1461 if (!TCR_4(__kmp_init_hidden_helper))
1462 __kmp_hidden_helper_initialize();
1465 flags->hidden_helper = FALSE;
1469 KA_TRACE(10, (
"__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) "
1470 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1471 gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t,
1472 sizeof_shareds, task_entry));
1474 KMP_DEBUG_ASSERT(parent_task);
1475 if (parent_task->td_flags.final) {
1476 if (flags->merged_if0) {
1481 if (flags->tiedness == TASK_UNTIED && !team->t.t_serialized) {
1485 KMP_CHECK_UPDATE(thread->th.th_task_team->tt.tt_untied_task_encountered, 1);
1491 if (UNLIKELY(flags->proxy == TASK_PROXY ||
1492 flags->detachable == TASK_DETACHABLE || flags->hidden_helper)) {
1493 if (flags->proxy == TASK_PROXY) {
1494 flags->tiedness = TASK_UNTIED;
1495 flags->merged_if0 = 1;
1499 if ((thread->th.th_task_team) == NULL) {
1502 KMP_DEBUG_ASSERT(team->t.t_serialized);
1504 (
"T#%d creating task team in __kmp_task_alloc for proxy task\n",
1507 __kmp_task_team_setup(thread, team, 1);
1508 thread->th.th_task_team = team->t.t_task_team[thread->th.th_task_state];
1510 kmp_task_team_t *task_team = thread->th.th_task_team;
1513 if (!KMP_TASKING_ENABLED(task_team)) {
1516 (
"T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid));
1517 __kmp_enable_tasking(task_team, thread);
1518 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
1519 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
1521 if (thread_data->td.td_deque == NULL) {
1522 __kmp_alloc_task_deque(thread, thread_data);
1526 if ((flags->proxy == TASK_PROXY || flags->detachable == TASK_DETACHABLE) &&
1527 task_team->tt.tt_found_proxy_tasks == FALSE)
1528 TCW_4(task_team->tt.tt_found_proxy_tasks, TRUE);
1529 if (flags->hidden_helper &&
1530 task_team->tt.tt_hidden_helper_task_encountered == FALSE)
1531 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, TRUE);
1536 shareds_offset =
sizeof(kmp_taskdata_t) + sizeof_kmp_task_t;
1537 shareds_offset = __kmp_round_up_to_val(shareds_offset,
sizeof(
void *));
1540 KA_TRACE(30, (
"__kmp_task_alloc: T#%d First malloc size: %ld\n", gtid,
1542 KA_TRACE(30, (
"__kmp_task_alloc: T#%d Second malloc size: %ld\n", gtid,
1547 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, shareds_offset +
1550 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, shareds_offset +
1554 task = KMP_TASKDATA_TO_TASK(taskdata);
1557#if KMP_ARCH_X86 || KMP_ARCH_PPC64 || !KMP_HAVE_QUAD
1558 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(
double) - 1)) == 0);
1559 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(
double) - 1)) == 0);
1561 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(_Quad) - 1)) == 0);
1562 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(_Quad) - 1)) == 0);
1564 if (sizeof_shareds > 0) {
1566 task->shareds = &((
char *)taskdata)[shareds_offset];
1568 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
1571 task->shareds = NULL;
1573 task->routine = task_entry;
1576 taskdata->td_task_id = KMP_GEN_TASK_ID();
1577 taskdata->td_team = thread->th.th_team;
1578 taskdata->td_alloc_thread = thread;
1579 taskdata->td_parent = parent_task;
1580 taskdata->td_level = parent_task->td_level + 1;
1581 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
1582 taskdata->td_ident = loc_ref;
1583 taskdata->td_taskwait_ident = NULL;
1584 taskdata->td_taskwait_counter = 0;
1585 taskdata->td_taskwait_thread = 0;
1586 KMP_DEBUG_ASSERT(taskdata->td_parent != NULL);
1588 if (flags->proxy == TASK_FULL)
1589 copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs);
1591 taskdata->td_flags = *flags;
1592 taskdata->td_task_team = thread->th.th_task_team;
1593 taskdata->td_size_alloc = shareds_offset + sizeof_shareds;
1594 taskdata->td_flags.tasktype = TASK_EXPLICIT;
1597 if (flags->hidden_helper) {
1598 kmp_info_t *shadow_thread = __kmp_threads[KMP_GTID_TO_SHADOW_GTID(gtid)];
1599 taskdata->td_team = shadow_thread->th.th_team;
1600 taskdata->td_task_team = shadow_thread->th.th_task_team;
1604 taskdata->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1607 taskdata->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1613 taskdata->td_flags.task_serial =
1614 (parent_task->td_flags.final || taskdata->td_flags.team_serial ||
1615 taskdata->td_flags.tasking_ser || flags->merged_if0);
1617 taskdata->td_flags.started = 0;
1618 taskdata->td_flags.executing = 0;
1619 taskdata->td_flags.complete = 0;
1620 taskdata->td_flags.freed = 0;
1622 taskdata->td_flags.onced = 0;
1624 KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0);
1626 KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1);
1627 taskdata->td_taskgroup =
1628 parent_task->td_taskgroup;
1629 taskdata->td_dephash = NULL;
1630 taskdata->td_depnode = NULL;
1631 taskdata->td_target_data.async_handle = NULL;
1632 if (flags->tiedness == TASK_UNTIED)
1633 taskdata->td_last_tied = NULL;
1635 taskdata->td_last_tied = taskdata;
1636 taskdata->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1638 if (UNLIKELY(ompt_enabled.enabled))
1639 __ompt_task_init(taskdata, gtid);
1643 if (__kmp_track_children_task(taskdata)) {
1644 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
1645 if (parent_task->td_taskgroup)
1646 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
1649 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) {
1650 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
1652 if (flags->hidden_helper) {
1653 taskdata->td_flags.task_serial = FALSE;
1655 KMP_ATOMIC_INC(&__kmp_unexecuted_hidden_helper_tasks);
1660 kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_idx);
1661 if (tdg && __kmp_tdg_is_recording(tdg->tdg_status) &&
1662 (task_entry != (kmp_routine_entry_t)__kmp_taskloop_task)) {
1663 taskdata->is_taskgraph = 1;
1664 taskdata->tdg = __kmp_global_tdgs[__kmp_curr_tdg_idx];
1665 taskdata->td_task_id = KMP_ATOMIC_INC(&__kmp_tdg_task_id);
1668 KA_TRACE(20, (
"__kmp_task_alloc(exit): T#%d created task %p parent=%p\n",
1669 gtid, taskdata, taskdata->td_parent));
1674kmp_task_t *__kmpc_omp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1675 kmp_int32 flags,
size_t sizeof_kmp_task_t,
1676 size_t sizeof_shareds,
1677 kmp_routine_entry_t task_entry) {
1679 kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
1680 __kmp_assert_valid_gtid(gtid);
1681 input_flags->native = FALSE;
1683 KA_TRACE(10, (
"__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s %s) "
1684 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1685 gtid, loc_ref, input_flags->tiedness ?
"tied " :
"untied",
1686 input_flags->proxy ?
"proxy" :
"",
1687 input_flags->detachable ?
"detachable" :
"", sizeof_kmp_task_t,
1688 sizeof_shareds, task_entry));
1690 retval = __kmp_task_alloc(loc_ref, gtid, input_flags, sizeof_kmp_task_t,
1691 sizeof_shareds, task_entry);
1693 KA_TRACE(20, (
"__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval));
1698kmp_task_t *__kmpc_omp_target_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1700 size_t sizeof_kmp_task_t,
1701 size_t sizeof_shareds,
1702 kmp_routine_entry_t task_entry,
1703 kmp_int64 device_id) {
1704 auto &input_flags =
reinterpret_cast<kmp_tasking_flags_t &
>(flags);
1706 input_flags.tiedness = TASK_UNTIED;
1708 if (__kmp_enable_hidden_helper)
1709 input_flags.hidden_helper = TRUE;
1711 return __kmpc_omp_task_alloc(loc_ref, gtid, flags, sizeof_kmp_task_t,
1712 sizeof_shareds, task_entry);
1730 kmp_task_t *new_task, kmp_int32 naffins,
1731 kmp_task_affinity_info_t *affin_list) {
1740static void __kmp_invoke_task(kmp_int32 gtid, kmp_task_t *task,
1741 kmp_taskdata_t *current_task) {
1742 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
1746 30, (
"__kmp_invoke_task(enter): T#%d invoking task %p, current_task=%p\n",
1747 gtid, taskdata, current_task));
1748 KMP_DEBUG_ASSERT(task);
1749 if (UNLIKELY(taskdata->td_flags.proxy == TASK_PROXY &&
1750 taskdata->td_flags.complete == 1)) {
1755 (
"__kmp_invoke_task: T#%d running bottom finish for proxy task %p\n",
1758 __kmp_bottom_half_finish_proxy(gtid, task);
1760 KA_TRACE(30, (
"__kmp_invoke_task(exit): T#%d completed bottom finish for "
1761 "proxy task %p, resuming task %p\n",
1762 gtid, taskdata, current_task));
1770 ompt_thread_info_t oldInfo;
1771 if (UNLIKELY(ompt_enabled.enabled)) {
1773 thread = __kmp_threads[gtid];
1774 oldInfo = thread->th.ompt_thread_info;
1775 thread->th.ompt_thread_info.wait_id = 0;
1776 thread->th.ompt_thread_info.state = (thread->th.th_team_serialized)
1777 ? ompt_state_work_serial
1778 : ompt_state_work_parallel;
1779 taskdata->ompt_task_info.frame.exit_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1784 if (taskdata->td_flags.proxy != TASK_PROXY) {
1785 __kmp_task_start(gtid, task, current_task);
1791 if (UNLIKELY(__kmp_omp_cancellation)) {
1792 thread = __kmp_threads[gtid];
1793 kmp_team_t *this_team = thread->th.th_team;
1794 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
1795 if ((taskgroup && taskgroup->cancel_request) ||
1796 (this_team->t.t_cancel_request == cancel_parallel)) {
1797#if OMPT_SUPPORT && OMPT_OPTIONAL
1798 ompt_data_t *task_data;
1799 if (UNLIKELY(ompt_enabled.ompt_callback_cancel)) {
1800 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, NULL, NULL);
1801 ompt_callbacks.ompt_callback(ompt_callback_cancel)(
1803 ((taskgroup && taskgroup->cancel_request) ? ompt_cancel_taskgroup
1804 : ompt_cancel_parallel) |
1805 ompt_cancel_discarded_task,
1818 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
1819 taskdata->td_last_tied = current_task->td_last_tied;
1820 KMP_DEBUG_ASSERT(taskdata->td_last_tied);
1822#if KMP_STATS_ENABLED
1824 switch (KMP_GET_THREAD_STATE()) {
1825 case FORK_JOIN_BARRIER:
1826 KMP_PUSH_PARTITIONED_TIMER(OMP_task_join_bar);
1829 KMP_PUSH_PARTITIONED_TIMER(OMP_task_plain_bar);
1832 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskyield);
1835 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskwait);
1838 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskgroup);
1841 KMP_PUSH_PARTITIONED_TIMER(OMP_task_immediate);
1848 if (UNLIKELY(ompt_enabled.enabled))
1849 __ompt_task_start(task, current_task, gtid);
1851#if OMPT_SUPPORT && OMPT_OPTIONAL
1852 if (UNLIKELY(ompt_enabled.ompt_callback_dispatch &&
1853 taskdata->ompt_task_info.dispatch_chunk.iterations > 0)) {
1854 ompt_data_t instance = ompt_data_none;
1855 instance.ptr = &(taskdata->ompt_task_info.dispatch_chunk);
1856 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
1857 ompt_callbacks.ompt_callback(ompt_callback_dispatch)(
1858 &(team_info->parallel_data), &(taskdata->ompt_task_info.task_data),
1859 ompt_dispatch_taskloop_chunk, instance);
1860 taskdata->ompt_task_info.dispatch_chunk = {0, 0};
1865 if (ompd_state & OMPD_ENABLE_BP)
1866 ompd_bp_task_begin();
1869#if USE_ITT_BUILD && USE_ITT_NOTIFY
1870 kmp_uint64 cur_time;
1871 kmp_int32 kmp_itt_count_task =
1872 __kmp_forkjoin_frames_mode == 3 && !taskdata->td_flags.task_serial &&
1873 current_task->td_flags.tasktype == TASK_IMPLICIT;
1874 if (kmp_itt_count_task) {
1875 thread = __kmp_threads[gtid];
1877 if (thread->th.th_bar_arrive_time)
1878 cur_time = __itt_get_timestamp();
1880 kmp_itt_count_task = 0;
1882 KMP_FSYNC_ACQUIRED(taskdata);
1885#if ENABLE_LIBOMPTARGET
1886 if (taskdata->td_target_data.async_handle != NULL) {
1890 KMP_ASSERT(tgt_target_nowait_query);
1891 tgt_target_nowait_query(&taskdata->td_target_data.async_handle);
1894 if (task->routine != NULL) {
1895#ifdef KMP_GOMP_COMPAT
1896 if (taskdata->td_flags.native) {
1897 ((void (*)(
void *))(*(task->routine)))(task->shareds);
1901 (*(task->routine))(gtid, task);
1904 KMP_POP_PARTITIONED_TIMER();
1906#if USE_ITT_BUILD && USE_ITT_NOTIFY
1907 if (kmp_itt_count_task) {
1909 thread->th.th_bar_arrive_time += (__itt_get_timestamp() - cur_time);
1911 KMP_FSYNC_CANCEL(taskdata);
1912 KMP_FSYNC_RELEASING(taskdata->td_parent);
1917 if (ompd_state & OMPD_ENABLE_BP)
1922 if (taskdata->td_flags.proxy != TASK_PROXY) {
1924 if (UNLIKELY(ompt_enabled.enabled)) {
1925 thread->th.ompt_thread_info = oldInfo;
1926 if (taskdata->td_flags.tiedness == TASK_TIED) {
1927 taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
1929 __kmp_task_finish<true>(gtid, task, current_task);
1932 __kmp_task_finish<false>(gtid, task, current_task);
1937 (
"__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n",
1938 gtid, taskdata, current_task));
1952kmp_int32 __kmpc_omp_task_parts(
ident_t *loc_ref, kmp_int32 gtid,
1953 kmp_task_t *new_task) {
1954 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1956 KA_TRACE(10, (
"__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n", gtid,
1957 loc_ref, new_taskdata));
1960 kmp_taskdata_t *parent;
1961 if (UNLIKELY(ompt_enabled.enabled)) {
1962 parent = new_taskdata->td_parent;
1963 if (ompt_enabled.ompt_callback_task_create) {
1964 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1965 &(parent->ompt_task_info.task_data), &(parent->ompt_task_info.frame),
1966 &(new_taskdata->ompt_task_info.task_data), ompt_task_explicit, 0,
1967 OMPT_GET_RETURN_ADDRESS(0));
1975 if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1977 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1978 new_taskdata->td_flags.task_serial = 1;
1979 __kmp_invoke_task(gtid, new_task, current_task);
1984 (
"__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: "
1985 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
1986 gtid, loc_ref, new_taskdata));
1989 if (UNLIKELY(ompt_enabled.enabled)) {
1990 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1993 return TASK_CURRENT_NOT_QUEUED;
2007kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task,
2008 bool serialize_immediate) {
2009 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
2012 if (new_taskdata->is_taskgraph &&
2013 __kmp_tdg_is_recording(new_taskdata->tdg->tdg_status)) {
2014 kmp_tdg_info_t *tdg = new_taskdata->tdg;
2016 if (new_taskdata->td_task_id >= new_taskdata->tdg->map_size) {
2017 __kmp_acquire_bootstrap_lock(&tdg->graph_lock);
2020 if (new_taskdata->td_task_id >= tdg->map_size) {
2021 kmp_uint old_size = tdg->map_size;
2022 kmp_uint new_size = old_size * 2;
2023 kmp_node_info_t *old_record = tdg->record_map;
2024 kmp_node_info_t *new_record = (kmp_node_info_t *)__kmp_allocate(
2025 new_size *
sizeof(kmp_node_info_t));
2027 KMP_MEMCPY(new_record, old_record, old_size *
sizeof(kmp_node_info_t));
2028 tdg->record_map = new_record;
2030 __kmp_free(old_record);
2032 for (kmp_int i = old_size; i < new_size; i++) {
2033 kmp_int32 *successorsList = (kmp_int32 *)__kmp_allocate(
2034 __kmp_successors_size *
sizeof(kmp_int32));
2035 new_record[i].task =
nullptr;
2036 new_record[i].successors = successorsList;
2037 new_record[i].nsuccessors = 0;
2038 new_record[i].npredecessors = 0;
2039 new_record[i].successors_size = __kmp_successors_size;
2040 KMP_ATOMIC_ST_REL(&new_record[i].npredecessors_counter, 0);
2044 tdg->map_size = new_size;
2046 __kmp_release_bootstrap_lock(&tdg->graph_lock);
2049 if (tdg->record_map[new_taskdata->td_task_id].task ==
nullptr) {
2050 tdg->record_map[new_taskdata->td_task_id].task = new_task;
2051 tdg->record_map[new_taskdata->td_task_id].parent_task =
2052 new_taskdata->td_parent;
2053 KMP_ATOMIC_INC(&tdg->num_tasks);
2060 if (new_taskdata->td_flags.proxy == TASK_PROXY ||
2061 __kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
2063 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
2064 if (serialize_immediate)
2065 new_taskdata->td_flags.task_serial = 1;
2066 __kmp_invoke_task(gtid, new_task, current_task);
2067 }
else if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME &&
2068 __kmp_wpolicy_passive) {
2069 kmp_info_t *this_thr = __kmp_threads[gtid];
2070 kmp_team_t *team = this_thr->th.th_team;
2071 kmp_int32 nthreads = this_thr->th.th_team_nproc;
2072 for (
int i = 0; i < nthreads; ++i) {
2073 kmp_info_t *thread = team->t.t_threads[i];
2074 if (thread == this_thr)
2076 if (thread->th.th_sleep_loc != NULL) {
2077 __kmp_null_resume_wrapper(thread);
2082 return TASK_CURRENT_NOT_QUEUED;
2097kmp_int32 __kmpc_omp_task(
ident_t *loc_ref, kmp_int32 gtid,
2098 kmp_task_t *new_task) {
2100 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
2102#if KMP_DEBUG || OMPT_SUPPORT
2103 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
2105 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
2107 __kmp_assert_valid_gtid(gtid);
2110 kmp_taskdata_t *parent = NULL;
2111 if (UNLIKELY(ompt_enabled.enabled)) {
2112 if (!new_taskdata->td_flags.started) {
2113 OMPT_STORE_RETURN_ADDRESS(gtid);
2114 parent = new_taskdata->td_parent;
2115 if (!parent->ompt_task_info.frame.enter_frame.ptr) {
2116 parent->ompt_task_info.frame.enter_frame.ptr =
2117 OMPT_GET_FRAME_ADDRESS(0);
2119 if (ompt_enabled.ompt_callback_task_create) {
2120 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
2121 &(parent->ompt_task_info.task_data),
2122 &(parent->ompt_task_info.frame),
2123 &(new_taskdata->ompt_task_info.task_data),
2124 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
2125 OMPT_LOAD_RETURN_ADDRESS(gtid));
2130 __ompt_task_finish(new_task,
2131 new_taskdata->ompt_task_info.scheduling_parent,
2133 new_taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
2138 res = __kmp_omp_task(gtid, new_task,
true);
2140 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
2141 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
2142 gtid, loc_ref, new_taskdata));
2144 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
2145 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
2164kmp_int32 __kmp_omp_taskloop_task(
ident_t *loc_ref, kmp_int32 gtid,
2165 kmp_task_t *new_task,
void *codeptr_ra) {
2167 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
2169#if KMP_DEBUG || OMPT_SUPPORT
2170 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
2172 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
2176 kmp_taskdata_t *parent = NULL;
2177 if (UNLIKELY(ompt_enabled.enabled && !new_taskdata->td_flags.started)) {
2178 parent = new_taskdata->td_parent;
2179 if (!parent->ompt_task_info.frame.enter_frame.ptr)
2180 parent->ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
2181 if (ompt_enabled.ompt_callback_task_create) {
2182 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
2183 &(parent->ompt_task_info.task_data), &(parent->ompt_task_info.frame),
2184 &(new_taskdata->ompt_task_info.task_data),
2185 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
2191 res = __kmp_omp_task(gtid, new_task,
true);
2193 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
2194 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
2195 gtid, loc_ref, new_taskdata));
2197 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
2198 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
2205static kmp_int32 __kmpc_omp_taskwait_template(
ident_t *loc_ref, kmp_int32 gtid,
2206 void *frame_address,
2207 void *return_address) {
2208 kmp_taskdata_t *taskdata =
nullptr;
2210 int thread_finished = FALSE;
2211 KMP_SET_THREAD_STATE_BLOCK(TASKWAIT);
2213 KA_TRACE(10, (
"__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref));
2214 KMP_DEBUG_ASSERT(gtid >= 0);
2216 if (__kmp_tasking_mode != tskm_immediate_exec) {
2217 thread = __kmp_threads[gtid];
2218 taskdata = thread->th.th_current_task;
2220#if OMPT_SUPPORT && OMPT_OPTIONAL
2221 ompt_data_t *my_task_data;
2222 ompt_data_t *my_parallel_data;
2225 my_task_data = &(taskdata->ompt_task_info.task_data);
2226 my_parallel_data = OMPT_CUR_TEAM_DATA(thread);
2228 taskdata->ompt_task_info.frame.enter_frame.ptr = frame_address;
2230 if (ompt_enabled.ompt_callback_sync_region) {
2231 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2232 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
2233 my_task_data, return_address);
2236 if (ompt_enabled.ompt_callback_sync_region_wait) {
2237 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2238 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
2239 my_task_data, return_address);
2249 taskdata->td_taskwait_counter += 1;
2250 taskdata->td_taskwait_ident = loc_ref;
2251 taskdata->td_taskwait_thread = gtid + 1;
2254 void *itt_sync_obj = NULL;
2256 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2261 !taskdata->td_flags.team_serial && !taskdata->td_flags.final;
2263 must_wait = must_wait || (thread->th.th_task_team != NULL &&
2264 thread->th.th_task_team->tt.tt_found_proxy_tasks);
2268 (__kmp_enable_hidden_helper && thread->th.th_task_team != NULL &&
2269 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered);
2272 kmp_flag_32<false, false> flag(
2273 RCAST(std::atomic<kmp_uint32> *,
2274 &(taskdata->td_incomplete_child_tasks)),
2276 while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) != 0) {
2277 flag.execute_tasks(thread, gtid, FALSE,
2278 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2279 __kmp_task_stealing_constraint);
2283 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2284 KMP_FSYNC_ACQUIRED(taskdata);
2289 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2291#if OMPT_SUPPORT && OMPT_OPTIONAL
2293 if (ompt_enabled.ompt_callback_sync_region_wait) {
2294 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2295 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
2296 my_task_data, return_address);
2298 if (ompt_enabled.ompt_callback_sync_region) {
2299 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2300 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
2301 my_task_data, return_address);
2303 taskdata->ompt_task_info.frame.enter_frame = ompt_data_none;
2308 KA_TRACE(10, (
"__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, "
2309 "returning TASK_CURRENT_NOT_QUEUED\n",
2312 return TASK_CURRENT_NOT_QUEUED;
2315#if OMPT_SUPPORT && OMPT_OPTIONAL
2317static kmp_int32 __kmpc_omp_taskwait_ompt(
ident_t *loc_ref, kmp_int32 gtid,
2318 void *frame_address,
2319 void *return_address) {
2320 return __kmpc_omp_taskwait_template<true>(loc_ref, gtid, frame_address,
2327kmp_int32 __kmpc_omp_taskwait(
ident_t *loc_ref, kmp_int32 gtid) {
2328#if OMPT_SUPPORT && OMPT_OPTIONAL
2329 if (UNLIKELY(ompt_enabled.enabled)) {
2330 OMPT_STORE_RETURN_ADDRESS(gtid);
2331 return __kmpc_omp_taskwait_ompt(loc_ref, gtid, OMPT_GET_FRAME_ADDRESS(0),
2332 OMPT_LOAD_RETURN_ADDRESS(gtid));
2335 return __kmpc_omp_taskwait_template<false>(loc_ref, gtid, NULL, NULL);
2339kmp_int32 __kmpc_omp_taskyield(
ident_t *loc_ref, kmp_int32 gtid,
int end_part) {
2340 kmp_taskdata_t *taskdata = NULL;
2342 int thread_finished = FALSE;
2345 KMP_SET_THREAD_STATE_BLOCK(TASKYIELD);
2347 KA_TRACE(10, (
"__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n",
2348 gtid, loc_ref, end_part));
2349 __kmp_assert_valid_gtid(gtid);
2351 if (__kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel) {
2352 thread = __kmp_threads[gtid];
2353 taskdata = thread->th.th_current_task;
2360 taskdata->td_taskwait_counter += 1;
2361 taskdata->td_taskwait_ident = loc_ref;
2362 taskdata->td_taskwait_thread = gtid + 1;
2365 void *itt_sync_obj = NULL;
2367 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2370 if (!taskdata->td_flags.team_serial) {
2371 kmp_task_team_t *task_team = thread->th.th_task_team;
2372 if (task_team != NULL) {
2373 if (KMP_TASKING_ENABLED(task_team)) {
2375 if (UNLIKELY(ompt_enabled.enabled))
2376 thread->th.ompt_thread_info.ompt_task_yielded = 1;
2378 __kmp_execute_tasks_32(
2379 thread, gtid, (kmp_flag_32<> *)NULL, FALSE,
2380 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2381 __kmp_task_stealing_constraint);
2383 if (UNLIKELY(ompt_enabled.enabled))
2384 thread->th.ompt_thread_info.ompt_task_yielded = 0;
2390 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2395 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2398 KA_TRACE(10, (
"__kmpc_omp_taskyield(exit): T#%d task %p resuming, "
2399 "returning TASK_CURRENT_NOT_QUEUED\n",
2402 return TASK_CURRENT_NOT_QUEUED;
2423 unsigned reserved31 : 31;
2503template <
typename T>
2504void *__kmp_task_reduction_init(
int gtid,
int num, T *data) {
2505 __kmp_assert_valid_gtid(gtid);
2506 kmp_info_t *thread = __kmp_threads[gtid];
2507 kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup;
2508 kmp_uint32 nth = thread->th.th_team_nproc;
2512 KMP_ASSERT(tg != NULL);
2513 KMP_ASSERT(data != NULL);
2514 KMP_ASSERT(num > 0);
2516 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n",
2520 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, taskgroup %p, #items %d\n",
2524 for (
int i = 0; i < num; ++i) {
2525 size_t size = data[i].reduce_size - 1;
2527 size += CACHE_LINE - size % CACHE_LINE;
2528 KMP_ASSERT(data[i].reduce_comb != NULL);
2531 arr[i].
flags = data[i].flags;
2535 __kmp_assign_orig<T>(arr[i], data[i]);
2536 if (!arr[i].flags.lazy_priv) {
2539 arr[i].
reduce_pend = (
char *)(arr[i].reduce_priv) + nth * size;
2540 if (arr[i].reduce_init != NULL) {
2542 for (
size_t j = 0; j < nth; ++j) {
2543 __kmp_call_init<T>(arr[i], j * size);
2550 arr[i].
reduce_priv = __kmp_allocate(nth *
sizeof(
void *));
2553 tg->reduce_data = (
void *)arr;
2554 tg->reduce_num_data = num;
2574 kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_idx);
2575 if (tdg && __kmp_tdg_is_recording(tdg->tdg_status)) {
2576 kmp_tdg_info_t *this_tdg = __kmp_global_tdgs[__kmp_curr_tdg_idx];
2577 this_tdg->rec_taskred_data =
2579 this_tdg->rec_num_taskred = num;
2580 KMP_MEMCPY(this_tdg->rec_taskred_data, data,
2601 kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_idx);
2602 if (tdg && __kmp_tdg_is_recording(tdg->tdg_status)) {
2603 kmp_tdg_info_t *this_tdg = __kmp_global_tdgs[__kmp_curr_tdg_idx];
2604 this_tdg->rec_taskred_data =
2606 this_tdg->rec_num_taskred = num;
2607 KMP_MEMCPY(this_tdg->rec_taskred_data, data,
2615template <
typename T>
2616void __kmp_task_reduction_init_copy(kmp_info_t *thr,
int num, T *data,
2617 kmp_taskgroup_t *tg,
void *reduce_data) {
2619 KA_TRACE(20, (
"__kmp_task_reduction_init_copy: Th %p, init taskgroup %p,"
2621 thr, tg, reduce_data));
2626 for (
int i = 0; i < num; ++i) {
2629 tg->reduce_data = (
void *)arr;
2630 tg->reduce_num_data = num;
2643 __kmp_assert_valid_gtid(gtid);
2644 kmp_info_t *thread = __kmp_threads[gtid];
2645 kmp_int32 nth = thread->th.th_team_nproc;
2649 kmp_taskgroup_t *tg = (kmp_taskgroup_t *)tskgrp;
2651 tg = thread->th.th_current_task->td_taskgroup;
2652 KMP_ASSERT(tg != NULL);
2654 kmp_int32 num = tg->reduce_num_data;
2655 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
2658 if ((thread->th.th_current_task->is_taskgraph) &&
2659 (!__kmp_tdg_is_recording(
2660 __kmp_global_tdgs[__kmp_curr_tdg_idx]->tdg_status))) {
2661 tg = thread->th.th_current_task->td_taskgroup;
2662 KMP_ASSERT(tg != NULL);
2663 KMP_ASSERT(tg->reduce_data != NULL);
2665 num = tg->reduce_num_data;
2669 KMP_ASSERT(data != NULL);
2670 while (tg != NULL) {
2671 for (
int i = 0; i < num; ++i) {
2672 if (!arr[i].flags.lazy_priv) {
2673 if (data == arr[i].reduce_shar ||
2674 (data >= arr[i].reduce_priv && data < arr[i].reduce_pend))
2675 return (
char *)(arr[i].
reduce_priv) + tid * arr[i].reduce_size;
2678 void **p_priv = (
void **)(arr[i].reduce_priv);
2679 if (data == arr[i].reduce_shar)
2682 for (
int j = 0; j < nth; ++j)
2683 if (data == p_priv[j])
2687 if (p_priv[tid] == NULL) {
2689 p_priv[tid] = __kmp_allocate(arr[i].reduce_size);
2690 if (arr[i].reduce_init != NULL) {
2691 if (arr[i].reduce_orig != NULL) {
2693 p_priv[tid], arr[i].reduce_orig);
2695 ((void (*)(
void *))arr[i].
reduce_init)(p_priv[tid]);
2704 num = tg->reduce_num_data;
2706 KMP_ASSERT2(0,
"Unknown task reduction item");
2712static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg) {
2713 kmp_int32 nth = th->th.th_team_nproc;
2714 KMP_DEBUG_ASSERT(nth > 1);
2716 kmp_int32 num = tg->reduce_num_data;
2717 for (
int i = 0; i < num; ++i) {
2719 void (*f_fini)(
void *) = (
void (*)(
void *))(arr[i].
reduce_fini);
2720 void (*f_comb)(
void *,
void *) =
2722 if (!arr[i].flags.lazy_priv) {
2725 for (
int j = 0; j < nth; ++j) {
2726 void *priv_data = (
char *)pr_data + j * size;
2727 f_comb(sh_data, priv_data);
2732 void **pr_data = (
void **)(arr[i].reduce_priv);
2733 for (
int j = 0; j < nth; ++j) {
2734 if (pr_data[j] != NULL) {
2735 f_comb(sh_data, pr_data[j]);
2738 __kmp_free(pr_data[j]);
2742 __kmp_free(arr[i].reduce_priv);
2744 __kmp_thread_free(th, arr);
2745 tg->reduce_data = NULL;
2746 tg->reduce_num_data = 0;
2752static void __kmp_task_reduction_clean(kmp_info_t *th, kmp_taskgroup_t *tg) {
2753 __kmp_thread_free(th, tg->reduce_data);
2754 tg->reduce_data = NULL;
2755 tg->reduce_num_data = 0;
2758template <
typename T>
2759void *__kmp_task_reduction_modifier_init(
ident_t *loc,
int gtid,
int is_ws,
2761 __kmp_assert_valid_gtid(gtid);
2762 kmp_info_t *thr = __kmp_threads[gtid];
2763 kmp_int32 nth = thr->th.th_team_nproc;
2764 __kmpc_taskgroup(loc, gtid);
2767 (
"__kmpc_reduction_modifier_init: T#%d, tg %p, exiting nth=1\n",
2768 gtid, thr->th.th_current_task->td_taskgroup));
2769 return (
void *)thr->th.th_current_task->td_taskgroup;
2771 kmp_team_t *team = thr->th.th_team;
2773 kmp_taskgroup_t *tg;
2774 reduce_data = KMP_ATOMIC_LD_RLX(&team->t.t_tg_reduce_data[is_ws]);
2775 if (reduce_data == NULL &&
2776 __kmp_atomic_compare_store(&team->t.t_tg_reduce_data[is_ws], reduce_data,
2779 KMP_DEBUG_ASSERT(reduce_data == NULL);
2781 tg = (kmp_taskgroup_t *)__kmp_task_reduction_init<T>(gtid, num, data);
2785 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[0]) == 0);
2786 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[1]) == 0);
2787 KMP_ATOMIC_ST_REL(&team->t.t_tg_reduce_data[is_ws], reduce_data);
2790 (reduce_data = KMP_ATOMIC_LD_ACQ(&team->t.t_tg_reduce_data[is_ws])) ==
2794 KMP_DEBUG_ASSERT(reduce_data > (
void *)1);
2795 tg = thr->th.th_current_task->td_taskgroup;
2796 __kmp_task_reduction_init_copy<T>(thr, num, data, tg, reduce_data);
2818 int num,
void *data) {
2819 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2839 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2852 __kmpc_end_taskgroup(loc, gtid);
2856void __kmpc_taskgroup(
ident_t *loc,
int gtid) {
2857 __kmp_assert_valid_gtid(gtid);
2858 kmp_info_t *thread = __kmp_threads[gtid];
2859 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2860 kmp_taskgroup_t *tg_new =
2861 (kmp_taskgroup_t *)__kmp_thread_malloc(thread,
sizeof(kmp_taskgroup_t));
2862 KA_TRACE(10, (
"__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new));
2863 KMP_ATOMIC_ST_RLX(&tg_new->count, 0);
2864 KMP_ATOMIC_ST_RLX(&tg_new->cancel_request, cancel_noreq);
2865 tg_new->parent = taskdata->td_taskgroup;
2866 tg_new->reduce_data = NULL;
2867 tg_new->reduce_num_data = 0;
2868 tg_new->gomp_data = NULL;
2869 taskdata->td_taskgroup = tg_new;
2871#if OMPT_SUPPORT && OMPT_OPTIONAL
2872 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2873 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2875 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2876 kmp_team_t *team = thread->th.th_team;
2877 ompt_data_t my_task_data = taskdata->ompt_task_info.task_data;
2879 ompt_data_t my_parallel_data = team->t.ompt_team_info.parallel_data;
2881 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2882 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2883 &(my_task_data), codeptr);
2890void __kmpc_end_taskgroup(
ident_t *loc,
int gtid) {
2891 __kmp_assert_valid_gtid(gtid);
2892 kmp_info_t *thread = __kmp_threads[gtid];
2893 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2894 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
2895 int thread_finished = FALSE;
2897#if OMPT_SUPPORT && OMPT_OPTIONAL
2899 ompt_data_t my_task_data;
2900 ompt_data_t my_parallel_data;
2901 void *codeptr =
nullptr;
2902 if (UNLIKELY(ompt_enabled.enabled)) {
2903 team = thread->th.th_team;
2904 my_task_data = taskdata->ompt_task_info.task_data;
2906 my_parallel_data = team->t.ompt_team_info.parallel_data;
2907 codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2909 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2913 KA_TRACE(10, (
"__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc));
2914 KMP_DEBUG_ASSERT(taskgroup != NULL);
2915 KMP_SET_THREAD_STATE_BLOCK(TASKGROUP);
2917 if (__kmp_tasking_mode != tskm_immediate_exec) {
2919 taskdata->td_taskwait_counter += 1;
2920 taskdata->td_taskwait_ident = loc;
2921 taskdata->td_taskwait_thread = gtid + 1;
2925 void *itt_sync_obj = NULL;
2927 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2931#if OMPT_SUPPORT && OMPT_OPTIONAL
2932 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2933 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2934 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2935 &(my_task_data), codeptr);
2939 if (!taskdata->td_flags.team_serial ||
2940 (thread->th.th_task_team != NULL &&
2941 (thread->th.th_task_team->tt.tt_found_proxy_tasks ||
2942 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered))) {
2943 kmp_flag_32<false, false> flag(
2944 RCAST(std::atomic<kmp_uint32> *, &(taskgroup->count)), 0U);
2945 while (KMP_ATOMIC_LD_ACQ(&taskgroup->count) != 0) {
2946 flag.execute_tasks(thread, gtid, FALSE,
2947 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2948 __kmp_task_stealing_constraint);
2951 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2953#if OMPT_SUPPORT && OMPT_OPTIONAL
2954 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2955 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2956 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2957 &(my_task_data), codeptr);
2962 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2963 KMP_FSYNC_ACQUIRED(taskdata);
2966 KMP_DEBUG_ASSERT(taskgroup->count == 0);
2968 if (taskgroup->reduce_data != NULL &&
2969 !taskgroup->gomp_data) {
2972 kmp_team_t *t = thread->th.th_team;
2976 if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[0])) != NULL &&
2979 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[0]);
2980 if (cnt == thread->th.th_team_nproc - 1) {
2983 __kmp_task_reduction_fini(thread, taskgroup);
2986 __kmp_thread_free(thread, reduce_data);
2987 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[0], NULL);
2988 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[0], 0);
2992 __kmp_task_reduction_clean(thread, taskgroup);
2994 }
else if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[1])) !=
2998 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[1]);
2999 if (cnt == thread->th.th_team_nproc - 1) {
3001 __kmp_task_reduction_fini(thread, taskgroup);
3004 __kmp_thread_free(thread, reduce_data);
3005 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[1], NULL);
3006 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[1], 0);
3010 __kmp_task_reduction_clean(thread, taskgroup);
3014 __kmp_task_reduction_fini(thread, taskgroup);
3018 taskdata->td_taskgroup = taskgroup->parent;
3019 __kmp_thread_free(thread, taskgroup);
3021 KA_TRACE(10, (
"__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n",
3024#if OMPT_SUPPORT && OMPT_OPTIONAL
3025 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
3026 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
3027 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
3028 &(my_task_data), codeptr);
3033static kmp_task_t *__kmp_get_priority_task(kmp_int32 gtid,
3034 kmp_task_team_t *task_team,
3035 kmp_int32 is_constrained) {
3036 kmp_task_t *task = NULL;
3037 kmp_taskdata_t *taskdata;
3038 kmp_taskdata_t *current;
3039 kmp_thread_data_t *thread_data;
3040 int ntasks = task_team->tt.tt_num_task_pri;
3043 20, (
"__kmp_get_priority_task(exit #1): T#%d No tasks to get\n", gtid));
3048 if (__kmp_atomic_compare_store(&task_team->tt.tt_num_task_pri, ntasks,
3051 ntasks = task_team->tt.tt_num_task_pri;
3052 }
while (ntasks > 0);
3054 KA_TRACE(20, (
"__kmp_get_priority_task(exit #2): T#%d No tasks to get\n",
3060 kmp_task_pri_t *list = task_team->tt.tt_task_pri_list;
3062 KMP_ASSERT(list != NULL);
3063 thread_data = &list->td;
3064 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3065 deque_ntasks = thread_data->td.td_deque_ntasks;
3066 if (deque_ntasks == 0) {
3067 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3068 KA_TRACE(20, (
"__kmp_get_priority_task: T#%d No tasks to get from %p\n",
3069 __kmp_get_gtid(), thread_data));
3072 }
while (deque_ntasks == 0);
3073 KMP_DEBUG_ASSERT(deque_ntasks);
3074 int target = thread_data->td.td_deque_head;
3075 current = __kmp_threads[gtid]->th.th_current_task;
3076 taskdata = thread_data->td.td_deque[target];
3077 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3079 thread_data->td.td_deque_head =
3080 (target + 1) & TASK_DEQUE_MASK(thread_data->td);
3082 if (!task_team->tt.tt_untied_task_encountered) {
3084 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3085 KA_TRACE(20, (
"__kmp_get_priority_task(exit #3): T#%d could not get task "
3086 "from %p: task_team=%p ntasks=%d head=%u tail=%u\n",
3087 gtid, thread_data, task_team, deque_ntasks, target,
3088 thread_data->td.td_deque_tail));
3089 task_team->tt.tt_num_task_pri++;
3095 for (i = 1; i < deque_ntasks; ++i) {
3096 target = (target + 1) & TASK_DEQUE_MASK(thread_data->td);
3097 taskdata = thread_data->td.td_deque[target];
3098 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3104 if (taskdata == NULL) {
3106 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3108 10, (
"__kmp_get_priority_task(exit #4): T#%d could not get task from "
3109 "%p: task_team=%p ntasks=%d head=%u tail=%u\n",
3110 gtid, thread_data, task_team, deque_ntasks,
3111 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3112 task_team->tt.tt_num_task_pri++;
3116 for (i = i + 1; i < deque_ntasks; ++i) {
3118 target = (target + 1) & TASK_DEQUE_MASK(thread_data->td);
3119 thread_data->td.td_deque[prev] = thread_data->td.td_deque[target];
3123 thread_data->td.td_deque_tail ==
3124 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(thread_data->td)));
3125 thread_data->td.td_deque_tail = target;
3127 thread_data->td.td_deque_ntasks = deque_ntasks - 1;
3128 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3129 task = KMP_TASKDATA_TO_TASK(taskdata);
3134static kmp_task_t *__kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid,
3135 kmp_task_team_t *task_team,
3136 kmp_int32 is_constrained) {
3138 kmp_taskdata_t *taskdata;
3139 kmp_thread_data_t *thread_data;
3142 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3143 KMP_DEBUG_ASSERT(task_team->tt.tt_threads_data !=
3146 thread_data = &task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
3148 KA_TRACE(10, (
"__kmp_remove_my_task(enter): T#%d ntasks=%d head=%u tail=%u\n",
3149 gtid, thread_data->td.td_deque_ntasks,
3150 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3152 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
3154 (
"__kmp_remove_my_task(exit #1): T#%d No tasks to remove: "
3155 "ntasks=%d head=%u tail=%u\n",
3156 gtid, thread_data->td.td_deque_ntasks,
3157 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3161 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3163 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
3164 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3166 (
"__kmp_remove_my_task(exit #2): 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 tail = (thread_data->td.td_deque_tail - 1) &
3174 TASK_DEQUE_MASK(thread_data->td);
3175 taskdata = thread_data->td.td_deque[tail];
3177 if (!__kmp_task_is_allowed(gtid, is_constrained, taskdata,
3178 thread->th.th_current_task)) {
3180 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3182 (
"__kmp_remove_my_task(exit #3): T#%d TSC blocks tail task: "
3183 "ntasks=%d head=%u tail=%u\n",
3184 gtid, thread_data->td.td_deque_ntasks,
3185 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3189 thread_data->td.td_deque_tail = tail;
3190 TCW_4(thread_data->td.td_deque_ntasks, thread_data->td.td_deque_ntasks - 1);
3192 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3194 KA_TRACE(10, (
"__kmp_remove_my_task(exit #4): T#%d task %p removed: "
3195 "ntasks=%d head=%u tail=%u\n",
3196 gtid, taskdata, thread_data->td.td_deque_ntasks,
3197 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3199 task = KMP_TASKDATA_TO_TASK(taskdata);
3206static kmp_task_t *__kmp_steal_task(kmp_info_t *victim_thr, kmp_int32 gtid,
3207 kmp_task_team_t *task_team,
3208 std::atomic<kmp_int32> *unfinished_threads,
3209 int *thread_finished,
3210 kmp_int32 is_constrained) {
3212 kmp_taskdata_t *taskdata;
3213 kmp_taskdata_t *current;
3214 kmp_thread_data_t *victim_td, *threads_data;
3216 kmp_int32 victim_tid;
3218 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3220 threads_data = task_team->tt.tt_threads_data;
3221 KMP_DEBUG_ASSERT(threads_data != NULL);
3223 victim_tid = victim_thr->th.th_info.ds.ds_tid;
3224 victim_td = &threads_data[victim_tid];
3226 KA_TRACE(10, (
"__kmp_steal_task(enter): T#%d try to steal from T#%d: "
3227 "task_team=%p ntasks=%d head=%u tail=%u\n",
3228 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
3229 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
3230 victim_td->td.td_deque_tail));
3232 if (TCR_4(victim_td->td.td_deque_ntasks) == 0) {
3233 KA_TRACE(10, (
"__kmp_steal_task(exit #1): T#%d could not steal from T#%d: "
3234 "task_team=%p ntasks=%d head=%u tail=%u\n",
3235 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
3236 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
3237 victim_td->td.td_deque_tail));
3241 __kmp_acquire_bootstrap_lock(&victim_td->td.td_deque_lock);
3243 int ntasks = TCR_4(victim_td->td.td_deque_ntasks);
3246 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3247 KA_TRACE(10, (
"__kmp_steal_task(exit #2): T#%d could not steal from T#%d: "
3248 "task_team=%p ntasks=%d head=%u tail=%u\n",
3249 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3250 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3254 KMP_DEBUG_ASSERT(victim_td->td.td_deque != NULL);
3255 current = __kmp_threads[gtid]->th.th_current_task;
3256 taskdata = victim_td->td.td_deque[victim_td->td.td_deque_head];
3257 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3259 victim_td->td.td_deque_head =
3260 (victim_td->td.td_deque_head + 1) & TASK_DEQUE_MASK(victim_td->td);
3262 if (!task_team->tt.tt_untied_task_encountered) {
3264 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3265 KA_TRACE(10, (
"__kmp_steal_task(exit #3): T#%d could not steal from "
3266 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
3267 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3268 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3273 target = victim_td->td.td_deque_head;
3275 for (i = 1; i < ntasks; ++i) {
3276 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
3277 taskdata = victim_td->td.td_deque[target];
3278 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3284 if (taskdata == NULL) {
3286 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3287 KA_TRACE(10, (
"__kmp_steal_task(exit #4): T#%d could not steal from "
3288 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
3289 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3290 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3294 for (i = i + 1; i < ntasks; ++i) {
3296 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
3297 victim_td->td.td_deque[prev] = victim_td->td.td_deque[target];
3301 victim_td->td.td_deque_tail ==
3302 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(victim_td->td)));
3303 victim_td->td.td_deque_tail = target;
3305 if (*thread_finished) {
3312 KMP_ATOMIC_INC(unfinished_threads);
3315 (
"__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n",
3316 gtid, count + 1, task_team));
3317 *thread_finished = FALSE;
3319 TCW_4(victim_td->td.td_deque_ntasks, ntasks - 1);
3321 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3325 (
"__kmp_steal_task(exit #5): T#%d stole task %p from T#%d: "
3326 "task_team=%p ntasks=%d head=%u tail=%u\n",
3327 gtid, taskdata, __kmp_gtid_from_thread(victim_thr), task_team,
3328 ntasks, victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3330 task = KMP_TASKDATA_TO_TASK(taskdata);
3344static inline int __kmp_execute_tasks_template(
3345 kmp_info_t *thread, kmp_int32 gtid, C *flag,
int final_spin,
3346 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3347 kmp_int32 is_constrained) {
3348 kmp_task_team_t *task_team = thread->th.th_task_team;
3349 kmp_thread_data_t *threads_data;
3351 kmp_info_t *other_thread;
3352 kmp_taskdata_t *current_task = thread->th.th_current_task;
3353 std::atomic<kmp_int32> *unfinished_threads;
3354 kmp_int32 nthreads, victim_tid = -2, use_own_tasks = 1, new_victim = 0,
3355 tid = thread->th.th_info.ds.ds_tid;
3357 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3358 KMP_DEBUG_ASSERT(thread == __kmp_threads[gtid]);
3360 if (task_team == NULL || current_task == NULL)
3363 KA_TRACE(15, (
"__kmp_execute_tasks_template(enter): T#%d final_spin=%d "
3364 "*thread_finished=%d\n",
3365 gtid, final_spin, *thread_finished));
3367 thread->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
3368 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
3370 KMP_DEBUG_ASSERT(threads_data != NULL);
3372 nthreads = task_team->tt.tt_nproc;
3373 unfinished_threads = &(task_team->tt.tt_unfinished_threads);
3374 KMP_DEBUG_ASSERT(nthreads > 1 || task_team->tt.tt_found_proxy_tasks ||
3375 task_team->tt.tt_hidden_helper_task_encountered);
3376 KMP_DEBUG_ASSERT(*unfinished_threads >= 0);
3382 if (task_team->tt.tt_num_task_pri) {
3383 task = __kmp_get_priority_task(gtid, task_team, is_constrained);
3385 if (task == NULL && use_own_tasks) {
3386 task = __kmp_remove_my_task(thread, gtid, task_team, is_constrained);
3388 if ((task == NULL) && (nthreads > 1)) {
3392 if (victim_tid == -2) {
3393 victim_tid = threads_data[tid].td.td_deque_last_stolen;
3396 other_thread = threads_data[victim_tid].td.td_thr;
3398 if (victim_tid != -1) {
3400 }
else if (!new_victim) {
3406 victim_tid = __kmp_get_random(thread) % (nthreads - 1);
3407 if (victim_tid >= tid) {
3411 other_thread = threads_data[victim_tid].td.td_thr;
3421 if ((__kmp_tasking_mode == tskm_task_teams) &&
3422 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) &&
3423 (TCR_PTR(CCAST(
void *, other_thread->th.th_sleep_loc)) !=
3426 __kmp_null_resume_wrapper(other_thread);
3439 task = __kmp_steal_task(other_thread, gtid, task_team,
3440 unfinished_threads, thread_finished,
3444 if (threads_data[tid].td.td_deque_last_stolen != victim_tid) {
3445 threads_data[tid].td.td_deque_last_stolen = victim_tid;
3452 KMP_CHECK_UPDATE(threads_data[tid].td.td_deque_last_stolen, -1);
3461#if USE_ITT_BUILD && USE_ITT_NOTIFY
3462 if (__itt_sync_create_ptr || KMP_ITT_DEBUG) {
3463 if (itt_sync_obj == NULL) {
3465 itt_sync_obj = __kmp_itt_barrier_object(gtid, bs_forkjoin_barrier);
3467 __kmp_itt_task_starting(itt_sync_obj);
3470 __kmp_invoke_task(gtid, task, current_task);
3472 if (itt_sync_obj != NULL)
3473 __kmp_itt_task_finished(itt_sync_obj);
3480 if (flag == NULL || (!final_spin && flag->done_check())) {
3483 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3487 if (thread->th.th_task_team == NULL) {
3490 KMP_YIELD(__kmp_library == library_throughput);
3493 if (!use_own_tasks && TCR_4(threads_data[tid].td.td_deque_ntasks) != 0) {
3494 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d stolen task spawned "
3495 "other tasks, restart\n",
3506 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks) == 0) {
3510 if (!*thread_finished) {
3512 kmp_int32 count = -1 +
3514 KMP_ATOMIC_DEC(unfinished_threads);
3515 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d dec "
3516 "unfinished_threads to %d task_team=%p\n",
3517 gtid, count, task_team));
3518 *thread_finished = TRUE;
3526 if (flag != NULL && flag->done_check()) {
3529 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3537 if (thread->th.th_task_team == NULL) {
3539 (
"__kmp_execute_tasks_template: T#%d no more tasks\n", gtid));
3548 if (flag == NULL || (!final_spin && flag->done_check())) {
3550 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3557 if (nthreads == 1 &&
3558 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks))
3562 (
"__kmp_execute_tasks_template: T#%d can't find work\n", gtid));
3568template <
bool C,
bool S>
3569int __kmp_execute_tasks_32(
3570 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32<C, S> *flag,
int final_spin,
3571 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3572 kmp_int32 is_constrained) {
3573 return __kmp_execute_tasks_template(
3574 thread, gtid, flag, final_spin,
3575 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3578template <
bool C,
bool S>
3579int __kmp_execute_tasks_64(
3580 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64<C, S> *flag,
int final_spin,
3581 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3582 kmp_int32 is_constrained) {
3583 return __kmp_execute_tasks_template(
3584 thread, gtid, flag, final_spin,
3585 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3588template <
bool C,
bool S>
3589int __kmp_atomic_execute_tasks_64(
3590 kmp_info_t *thread, kmp_int32 gtid, kmp_atomic_flag_64<C, S> *flag,
3591 int final_spin,
int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3592 kmp_int32 is_constrained) {
3593 return __kmp_execute_tasks_template(
3594 thread, gtid, flag, final_spin,
3595 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3598int __kmp_execute_tasks_oncore(
3599 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag,
int final_spin,
3600 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3601 kmp_int32 is_constrained) {
3602 return __kmp_execute_tasks_template(
3603 thread, gtid, flag, final_spin,
3604 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3608__kmp_execute_tasks_32<false, false>(kmp_info_t *, kmp_int32,
3609 kmp_flag_32<false, false> *,
int,
3610 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3612template int __kmp_execute_tasks_64<false, true>(kmp_info_t *, kmp_int32,
3613 kmp_flag_64<false, true> *,
3615 int *USE_ITT_BUILD_ARG(
void *),
3618template int __kmp_execute_tasks_64<true, false>(kmp_info_t *, kmp_int32,
3619 kmp_flag_64<true, false> *,
3621 int *USE_ITT_BUILD_ARG(
void *),
3624template int __kmp_atomic_execute_tasks_64<false, true>(
3625 kmp_info_t *, kmp_int32, kmp_atomic_flag_64<false, true> *,
int,
3626 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3628template int __kmp_atomic_execute_tasks_64<true, false>(
3629 kmp_info_t *, kmp_int32, kmp_atomic_flag_64<true, false> *,
int,
3630 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3635static void __kmp_enable_tasking(kmp_task_team_t *task_team,
3636 kmp_info_t *this_thr) {
3637 kmp_thread_data_t *threads_data;
3638 int nthreads, i, is_init_thread;
3640 KA_TRACE(10, (
"__kmp_enable_tasking(enter): T#%d\n",
3641 __kmp_gtid_from_thread(this_thr)));
3643 KMP_DEBUG_ASSERT(task_team != NULL);
3644 KMP_DEBUG_ASSERT(this_thr->th.th_team != NULL);
3646 nthreads = task_team->tt.tt_nproc;
3647 KMP_DEBUG_ASSERT(nthreads > 0);
3648 KMP_DEBUG_ASSERT(nthreads == this_thr->th.th_team->t.t_nproc);
3651 is_init_thread = __kmp_realloc_task_threads_data(this_thr, task_team);
3653 if (!is_init_thread) {
3657 (
"__kmp_enable_tasking(exit): T#%d: threads array already set up.\n",
3658 __kmp_gtid_from_thread(this_thr)));
3661 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
3662 KMP_DEBUG_ASSERT(threads_data != NULL);
3664 if (__kmp_tasking_mode == tskm_task_teams &&
3665 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME)) {
3669 for (i = 0; i < nthreads; i++) {
3671 kmp_info_t *thread = threads_data[i].td.td_thr;
3673 if (i == this_thr->th.th_info.ds.ds_tid) {
3682 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3684 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d waking up thread T#%d\n",
3685 __kmp_gtid_from_thread(this_thr),
3686 __kmp_gtid_from_thread(thread)));
3687 __kmp_null_resume_wrapper(thread);
3689 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d don't wake up thread T#%d\n",
3690 __kmp_gtid_from_thread(this_thr),
3691 __kmp_gtid_from_thread(thread)));
3696 KA_TRACE(10, (
"__kmp_enable_tasking(exit): T#%d\n",
3697 __kmp_gtid_from_thread(this_thr)));
3730static kmp_task_team_t *__kmp_free_task_teams =
3733kmp_bootstrap_lock_t __kmp_task_team_lock =
3734 KMP_BOOTSTRAP_LOCK_INITIALIZER(__kmp_task_team_lock);
3741static void __kmp_alloc_task_deque(kmp_info_t *thread,
3742 kmp_thread_data_t *thread_data) {
3743 __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
3744 KMP_DEBUG_ASSERT(thread_data->td.td_deque == NULL);
3747 thread_data->td.td_deque_last_stolen = -1;
3749 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == 0);
3750 KMP_DEBUG_ASSERT(thread_data->td.td_deque_head == 0);
3751 KMP_DEBUG_ASSERT(thread_data->td.td_deque_tail == 0);
3755 (
"__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n",
3756 __kmp_gtid_from_thread(thread), INITIAL_TASK_DEQUE_SIZE, thread_data));
3760 thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
3761 INITIAL_TASK_DEQUE_SIZE *
sizeof(kmp_taskdata_t *));
3762 thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
3768static void __kmp_free_task_deque(kmp_thread_data_t *thread_data) {
3769 if (thread_data->td.td_deque != NULL) {
3770 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3771 TCW_4(thread_data->td.td_deque_ntasks, 0);
3772 __kmp_free(thread_data->td.td_deque);
3773 thread_data->td.td_deque = NULL;
3774 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3777#ifdef BUILD_TIED_TASK_STACK
3779 if (thread_data->td.td_susp_tied_tasks.ts_entries != TASK_STACK_EMPTY) {
3780 __kmp_free_task_stack(__kmp_thread_from_gtid(gtid), thread_data);
3792static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
3793 kmp_task_team_t *task_team) {
3794 kmp_thread_data_t **threads_data_p;
3795 kmp_int32 nthreads, maxthreads;
3796 int is_init_thread = FALSE;
3798 if (TCR_4(task_team->tt.tt_found_tasks)) {
3803 threads_data_p = &task_team->tt.tt_threads_data;
3804 nthreads = task_team->tt.tt_nproc;
3805 maxthreads = task_team->tt.tt_max_threads;
3810 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3812 if (!TCR_4(task_team->tt.tt_found_tasks)) {
3814 kmp_team_t *team = thread->th.th_team;
3817 is_init_thread = TRUE;
3818 if (maxthreads < nthreads) {
3820 if (*threads_data_p != NULL) {
3821 kmp_thread_data_t *old_data = *threads_data_p;
3822 kmp_thread_data_t *new_data = NULL;
3826 (
"__kmp_realloc_task_threads_data: T#%d reallocating "
3827 "threads data for task_team %p, new_size = %d, old_size = %d\n",
3828 __kmp_gtid_from_thread(thread), task_team, nthreads, maxthreads));
3833 new_data = (kmp_thread_data_t *)__kmp_allocate(
3834 nthreads *
sizeof(kmp_thread_data_t));
3836 KMP_MEMCPY_S((
void *)new_data, nthreads *
sizeof(kmp_thread_data_t),
3837 (
void *)old_data, maxthreads *
sizeof(kmp_thread_data_t));
3839#ifdef BUILD_TIED_TASK_STACK
3841 for (i = maxthreads; i < nthreads; i++) {
3842 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3843 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3847 (*threads_data_p) = new_data;
3848 __kmp_free(old_data);
3850 KE_TRACE(10, (
"__kmp_realloc_task_threads_data: T#%d allocating "
3851 "threads data for task_team %p, size = %d\n",
3852 __kmp_gtid_from_thread(thread), task_team, nthreads));
3856 *threads_data_p = (kmp_thread_data_t *)__kmp_allocate(
3857 nthreads *
sizeof(kmp_thread_data_t));
3858#ifdef BUILD_TIED_TASK_STACK
3860 for (i = 0; i < nthreads; i++) {
3861 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3862 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3866 task_team->tt.tt_max_threads = nthreads;
3869 KMP_DEBUG_ASSERT(*threads_data_p != NULL);
3873 for (i = 0; i < nthreads; i++) {
3874 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3875 thread_data->td.td_thr = team->t.t_threads[i];
3877 if (thread_data->td.td_deque_last_stolen >= nthreads) {
3881 thread_data->td.td_deque_last_stolen = -1;
3886 TCW_SYNC_4(task_team->tt.tt_found_tasks, TRUE);
3889 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3890 return is_init_thread;
3896static void __kmp_free_task_threads_data(kmp_task_team_t *task_team) {
3897 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3898 if (task_team->tt.tt_threads_data != NULL) {
3900 for (i = 0; i < task_team->tt.tt_max_threads; i++) {
3901 __kmp_free_task_deque(&task_team->tt.tt_threads_data[i]);
3903 __kmp_free(task_team->tt.tt_threads_data);
3904 task_team->tt.tt_threads_data = NULL;
3906 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3912static void __kmp_free_task_pri_list(kmp_task_team_t *task_team) {
3913 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
3914 if (task_team->tt.tt_task_pri_list != NULL) {
3915 kmp_task_pri_t *list = task_team->tt.tt_task_pri_list;
3916 while (list != NULL) {
3917 kmp_task_pri_t *next = list->next;
3918 __kmp_free_task_deque(&list->td);
3922 task_team->tt.tt_task_pri_list = NULL;
3924 __kmp_release_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
3931static kmp_task_team_t *__kmp_allocate_task_team(kmp_info_t *thread,
3933 kmp_task_team_t *task_team = NULL;
3936 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d entering; team = %p\n",
3937 (thread ? __kmp_gtid_from_thread(thread) : -1), team));
3939 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3941 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3942 if (__kmp_free_task_teams != NULL) {
3943 task_team = __kmp_free_task_teams;
3944 TCW_PTR(__kmp_free_task_teams, task_team->tt.tt_next);
3945 task_team->tt.tt_next = NULL;
3947 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3950 if (task_team == NULL) {
3951 KE_TRACE(10, (
"__kmp_allocate_task_team: T#%d allocating "
3952 "task team for team %p\n",
3953 __kmp_gtid_from_thread(thread), team));
3956 task_team = (kmp_task_team_t *)__kmp_allocate(
sizeof(kmp_task_team_t));
3957 __kmp_init_bootstrap_lock(&task_team->tt.tt_threads_lock);
3958 __kmp_init_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
3959#if USE_ITT_BUILD && USE_ITT_NOTIFY && KMP_DEBUG
3962 __itt_suppress_mark_range(
3963 __itt_suppress_range, __itt_suppress_threading_errors,
3964 &task_team->tt.tt_found_tasks,
sizeof(task_team->tt.tt_found_tasks));
3965 __itt_suppress_mark_range(__itt_suppress_range,
3966 __itt_suppress_threading_errors,
3967 CCAST(kmp_uint32 *, &task_team->tt.tt_active),
3968 sizeof(task_team->tt.tt_active));
3976 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3977 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3978 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
3979 task_team->tt.tt_nproc = nthreads = team->t.t_nproc;
3981 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads, nthreads);
3982 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
3983 TCW_4(task_team->tt.tt_active, TRUE);
3985 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d exiting; task_team = %p "
3986 "unfinished_threads init'd to %d\n",
3987 (thread ? __kmp_gtid_from_thread(thread) : -1), task_team,
3988 KMP_ATOMIC_LD_RLX(&task_team->tt.tt_unfinished_threads)));
3995void __kmp_free_task_team(kmp_info_t *thread, kmp_task_team_t *task_team) {
3996 KA_TRACE(20, (
"__kmp_free_task_team: T#%d task_team = %p\n",
3997 thread ? __kmp_gtid_from_thread(thread) : -1, task_team));
4000 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
4002 KMP_DEBUG_ASSERT(task_team->tt.tt_next == NULL);
4003 task_team->tt.tt_next = __kmp_free_task_teams;
4004 TCW_PTR(__kmp_free_task_teams, task_team);
4006 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
4014void __kmp_reap_task_teams(
void) {
4015 kmp_task_team_t *task_team;
4017 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
4019 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
4020 while ((task_team = __kmp_free_task_teams) != NULL) {
4021 __kmp_free_task_teams = task_team->tt.tt_next;
4022 task_team->tt.tt_next = NULL;
4025 if (task_team->tt.tt_threads_data != NULL) {
4026 __kmp_free_task_threads_data(task_team);
4028 if (task_team->tt.tt_task_pri_list != NULL) {
4029 __kmp_free_task_pri_list(task_team);
4031 __kmp_free(task_team);
4033 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
4040void __kmp_wait_to_unref_task_teams(
void) {
4046 KMP_INIT_YIELD(spins);
4047 KMP_INIT_BACKOFF(time);
4055 for (thread = CCAST(kmp_info_t *, __kmp_thread_pool); thread != NULL;
4056 thread = thread->th.th_next_pool) {
4060 if (TCR_PTR(thread->th.th_task_team) == NULL) {
4061 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n",
4062 __kmp_gtid_from_thread(thread)));
4067 if (!__kmp_is_thread_alive(thread, &exit_val)) {
4068 thread->th.th_task_team = NULL;
4075 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: Waiting for T#%d to "
4076 "unreference task_team\n",
4077 __kmp_gtid_from_thread(thread)));
4079 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
4082 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
4086 (
"__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n",
4087 __kmp_gtid_from_thread(thread), __kmp_gtid_from_thread(thread)));
4088 __kmp_null_resume_wrapper(thread);
4097 KMP_YIELD_OVERSUB_ELSE_SPIN(spins, time);
4101void __kmp_shift_task_state_stack(kmp_info_t *this_thr, kmp_uint8 value) {
4103 if (this_thr->th.th_task_state_top + 1 >=
4104 this_thr->th.th_task_state_stack_sz) {
4105 kmp_uint32 new_size = 2 * this_thr->th.th_task_state_stack_sz;
4106 kmp_uint8 *old_stack, *new_stack;
4108 new_stack = (kmp_uint8 *)__kmp_allocate(new_size);
4109 for (i = 0; i <= this_thr->th.th_task_state_top; ++i) {
4110 new_stack[i] = this_thr->th.th_task_state_memo_stack[i];
4113 for (; i < this_thr->th.th_task_state_stack_sz; ++i) {
4114 new_stack[i + 1] = this_thr->th.th_task_state_memo_stack[i];
4116 for (i = this_thr->th.th_task_state_stack_sz; i < new_size;
4120 old_stack = this_thr->th.th_task_state_memo_stack;
4121 this_thr->th.th_task_state_memo_stack = new_stack;
4122 this_thr->th.th_task_state_stack_sz = new_size;
4123 __kmp_free(old_stack);
4129 .th_task_state_memo_stack[this_thr->th.th_task_state_stack_sz];
4131 for (i = this_thr->th.th_task_state_stack_sz - 1;
4132 i > this_thr->th.th_task_state_top; i--, end--)
4135 this_thr->th.th_task_state_memo_stack[this_thr->th.th_task_state_top + 1] =
4141void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team,
int always) {
4142 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
4148 if (team->t.t_task_team[this_thr->th.th_task_state] == NULL &&
4149 (always || team->t.t_nproc > 1)) {
4150 team->t.t_task_team[this_thr->th.th_task_state] =
4151 __kmp_allocate_task_team(this_thr, team);
4152 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d created new task_team %p"
4153 " for team %d at parity=%d\n",
4154 __kmp_gtid_from_thread(this_thr),
4155 team->t.t_task_team[this_thr->th.th_task_state], team->t.t_id,
4156 this_thr->th.th_task_state));
4158 if (this_thr->th.th_task_state == 1 && always && team->t.t_nproc == 1) {
4160 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d needs to shift stack"
4161 " for team %d at parity=%d\n",
4162 __kmp_gtid_from_thread(this_thr), team->t.t_id,
4163 this_thr->th.th_task_state));
4164 __kmp_shift_task_state_stack(this_thr, this_thr->th.th_task_state);
4174 if (team->t.t_nproc > 1) {
4175 int other_team = 1 - this_thr->th.th_task_state;
4176 KMP_DEBUG_ASSERT(other_team >= 0 && other_team < 2);
4177 if (team->t.t_task_team[other_team] == NULL) {
4178 team->t.t_task_team[other_team] =
4179 __kmp_allocate_task_team(this_thr, team);
4180 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d created second new "
4181 "task_team %p for team %d at parity=%d\n",
4182 __kmp_gtid_from_thread(this_thr),
4183 team->t.t_task_team[other_team], team->t.t_id, other_team));
4186 kmp_task_team_t *task_team = team->t.t_task_team[other_team];
4187 if (!task_team->tt.tt_active ||
4188 team->t.t_nproc != task_team->tt.tt_nproc) {
4189 TCW_4(task_team->tt.tt_nproc, team->t.t_nproc);
4190 TCW_4(task_team->tt.tt_found_tasks, FALSE);
4191 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
4192 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
4193 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads,
4195 TCW_4(task_team->tt.tt_active, TRUE);
4199 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d reset next task_team "
4200 "%p for team %d at parity=%d\n",
4201 __kmp_gtid_from_thread(this_thr),
4202 team->t.t_task_team[other_team], team->t.t_id, other_team));
4210 if (this_thr == __kmp_hidden_helper_main_thread) {
4211 for (
int i = 0; i < 2; ++i) {
4212 kmp_task_team_t *task_team = team->t.t_task_team[i];
4213 if (KMP_TASKING_ENABLED(task_team)) {
4216 __kmp_enable_tasking(task_team, this_thr);
4217 for (
int j = 0; j < task_team->tt.tt_nproc; ++j) {
4218 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[j];
4219 if (thread_data->td.td_deque == NULL) {
4220 __kmp_alloc_task_deque(__kmp_hidden_helper_threads[j], thread_data);
4230void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team) {
4231 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
4235 this_thr->th.th_task_state = (kmp_uint8)(1 - this_thr->th.th_task_state);
4239 TCW_PTR(this_thr->th.th_task_team,
4240 team->t.t_task_team[this_thr->th.th_task_state]);
4242 (
"__kmp_task_team_sync: Thread T#%d task team switched to task_team "
4243 "%p from Team #%d (parity=%d)\n",
4244 __kmp_gtid_from_thread(this_thr), this_thr->th.th_task_team,
4245 team->t.t_id, this_thr->th.th_task_state));
4255void __kmp_task_team_wait(
4256 kmp_info_t *this_thr,
4257 kmp_team_t *team USE_ITT_BUILD_ARG(
void *itt_sync_obj),
int wait) {
4258 kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state];
4260 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
4261 KMP_DEBUG_ASSERT(task_team == this_thr->th.th_task_team);
4263 if ((task_team != NULL) && KMP_TASKING_ENABLED(task_team)) {
4265 KA_TRACE(20, (
"__kmp_task_team_wait: Primary T#%d waiting for all tasks "
4266 "(for unfinished_threads to reach 0) on task_team = %p\n",
4267 __kmp_gtid_from_thread(this_thr), task_team));
4271 kmp_flag_32<false, false> flag(
4272 RCAST(std::atomic<kmp_uint32> *,
4273 &task_team->tt.tt_unfinished_threads),
4275 flag.wait(this_thr, TRUE USE_ITT_BUILD_ARG(itt_sync_obj));
4281 (
"__kmp_task_team_wait: Primary T#%d deactivating task_team %p: "
4282 "setting active to false, setting local and team's pointer to NULL\n",
4283 __kmp_gtid_from_thread(this_thr), task_team));
4284 KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1 ||
4285 task_team->tt.tt_found_proxy_tasks == TRUE ||
4286 task_team->tt.tt_hidden_helper_task_encountered == TRUE);
4287 TCW_SYNC_4(task_team->tt.tt_found_proxy_tasks, FALSE);
4288 TCW_SYNC_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
4289 KMP_CHECK_UPDATE(task_team->tt.tt_untied_task_encountered, 0);
4290 TCW_SYNC_4(task_team->tt.tt_active, FALSE);
4293 TCW_PTR(this_thr->th.th_task_team, NULL);
4302void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread,
int gtid) {
4303 std::atomic<kmp_uint32> *spin = RCAST(
4304 std::atomic<kmp_uint32> *,
4305 &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads);
4307 KMP_DEBUG_ASSERT(__kmp_tasking_mode == tskm_extra_barrier);
4310 KMP_FSYNC_SPIN_INIT(spin, NULL);
4312 kmp_flag_32<false, false> spin_flag(spin, 0U);
4313 while (!spin_flag.execute_tasks(thread, gtid, TRUE,
4314 &flag USE_ITT_BUILD_ARG(NULL), 0)) {
4317 KMP_FSYNC_SPIN_PREPARE(RCAST(
void *, spin));
4320 if (TCR_4(__kmp_global.g.g_done)) {
4321 if (__kmp_global.g.g_abort)
4322 __kmp_abort_thread();
4328 KMP_FSYNC_SPIN_ACQUIRED(RCAST(
void *, spin));
4337static bool __kmp_give_task(kmp_info_t *thread, kmp_int32 tid, kmp_task_t *task,
4339 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4340 kmp_task_team_t *task_team = taskdata->td_task_team;
4342 KA_TRACE(20, (
"__kmp_give_task: trying to give task %p to thread %d.\n",
4346 KMP_DEBUG_ASSERT(task_team != NULL);
4348 bool result =
false;
4349 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
4351 if (thread_data->td.td_deque == NULL) {
4355 (
"__kmp_give_task: thread %d has no queue while giving task %p.\n",
4360 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4361 TASK_DEQUE_SIZE(thread_data->td)) {
4364 (
"__kmp_give_task: queue is full while giving task %p to thread %d.\n",
4369 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
4372 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
4373 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4374 TASK_DEQUE_SIZE(thread_data->td)) {
4376 __kmp_realloc_task_deque(thread, thread_data);
4381 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
4383 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4384 TASK_DEQUE_SIZE(thread_data->td)) {
4385 KA_TRACE(30, (
"__kmp_give_task: queue is full while giving task %p to "
4391 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
4392 goto release_and_exit;
4394 __kmp_realloc_task_deque(thread, thread_data);
4400 thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
4402 thread_data->td.td_deque_tail =
4403 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
4404 TCW_4(thread_data->td.td_deque_ntasks,
4405 TCR_4(thread_data->td.td_deque_ntasks) + 1);
4408 KA_TRACE(30, (
"__kmp_give_task: successfully gave task %p to thread %d.\n",
4412 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
4417#define PROXY_TASK_FLAG 0x40000000
4434static void __kmp_first_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
4435 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
4436 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4437 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
4438 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
4440 taskdata->td_flags.complete = 1;
4442 taskdata->td_flags.onced = 1;
4445 if (taskdata->td_taskgroup)
4446 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
4450 KMP_ATOMIC_OR(&taskdata->td_incomplete_child_tasks, PROXY_TASK_FLAG);
4453static void __kmp_second_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
4455 kmp_int32 children = 0;
4459 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks);
4460 KMP_DEBUG_ASSERT(children >= 0);
4463 KMP_ATOMIC_AND(&taskdata->td_incomplete_child_tasks, ~PROXY_TASK_FLAG);
4466static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask) {
4467 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4468 kmp_info_t *thread = __kmp_threads[gtid];
4470 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4471 KMP_DEBUG_ASSERT(taskdata->td_flags.complete ==
4476 while ((KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) &
4477 PROXY_TASK_FLAG) > 0)
4480 __kmp_release_deps(gtid, taskdata);
4481 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
4493 KMP_DEBUG_ASSERT(ptask != NULL);
4494 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4496 10, (
"__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n",
4498 __kmp_assert_valid_gtid(gtid);
4499 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4501 __kmp_first_top_half_finish_proxy(taskdata);
4502 __kmp_second_top_half_finish_proxy(taskdata);
4503 __kmp_bottom_half_finish_proxy(gtid, ptask);
4506 (
"__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n",
4510void __kmpc_give_task(kmp_task_t *ptask, kmp_int32 start = 0) {
4511 KMP_DEBUG_ASSERT(ptask != NULL);
4512 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4516 kmp_team_t *team = taskdata->td_team;
4517 kmp_int32 nthreads = team->t.t_nproc;
4522 kmp_int32 start_k = start % nthreads;
4524 kmp_int32 k = start_k;
4528 thread = team->t.t_threads[k];
4529 k = (k + 1) % nthreads;
4535 }
while (!__kmp_give_task(thread, k, ptask, pass));
4537 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME && __kmp_wpolicy_passive) {
4539 for (
int i = 0; i < nthreads; ++i) {
4540 thread = team->t.t_threads[i];
4541 if (thread->th.th_sleep_loc != NULL) {
4542 __kmp_null_resume_wrapper(thread);
4557 KMP_DEBUG_ASSERT(ptask != NULL);
4558 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4562 (
"__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n",
4565 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4567 __kmp_first_top_half_finish_proxy(taskdata);
4569 __kmpc_give_task(ptask);
4571 __kmp_second_top_half_finish_proxy(taskdata);
4575 (
"__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n",
4579kmp_event_t *__kmpc_task_allow_completion_event(
ident_t *loc_ref,
int gtid,
4581 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(task);
4582 if (td->td_allow_completion_event.type == KMP_EVENT_UNINITIALIZED) {
4583 td->td_allow_completion_event.type = KMP_EVENT_ALLOW_COMPLETION;
4584 td->td_allow_completion_event.ed.task = task;
4585 __kmp_init_tas_lock(&td->td_allow_completion_event.lock);
4587 return &td->td_allow_completion_event;
4590void __kmp_fulfill_event(kmp_event_t *event) {
4591 if (event->type == KMP_EVENT_ALLOW_COMPLETION) {
4592 kmp_task_t *ptask = event->ed.task;
4593 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4594 bool detached =
false;
4595 int gtid = __kmp_get_gtid();
4600 __kmp_acquire_tas_lock(&event->lock, gtid);
4601 if (taskdata->td_flags.proxy == TASK_PROXY) {
4607 if (UNLIKELY(ompt_enabled.enabled))
4608 __ompt_task_finish(ptask, NULL, ompt_task_early_fulfill);
4611 event->type = KMP_EVENT_UNINITIALIZED;
4612 __kmp_release_tas_lock(&event->lock, gtid);
4618 if (UNLIKELY(ompt_enabled.enabled))
4619 __ompt_task_finish(ptask, NULL, ompt_task_late_fulfill);
4623 kmp_team_t *team = taskdata->td_team;
4624 kmp_info_t *thread = __kmp_get_thread();
4625 if (thread->th.th_team == team) {
4645kmp_task_t *__kmp_task_dup_alloc(kmp_info_t *thread, kmp_task_t *task_src
4647 ,
int taskloop_recur
4651 kmp_taskdata_t *taskdata;
4652 kmp_taskdata_t *taskdata_src = KMP_TASK_TO_TASKDATA(task_src);
4653 kmp_taskdata_t *parent_task = taskdata_src->td_parent;
4654 size_t shareds_offset;
4657 KA_TRACE(10, (
"__kmp_task_dup_alloc(enter): Th %p, source task %p\n", thread,
4659 KMP_DEBUG_ASSERT(taskdata_src->td_flags.proxy ==
4661 KMP_DEBUG_ASSERT(taskdata_src->td_flags.tasktype == TASK_EXPLICIT);
4662 task_size = taskdata_src->td_size_alloc;
4665 KA_TRACE(30, (
"__kmp_task_dup_alloc: Th %p, malloc size %ld\n", thread,
4668 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, task_size);
4670 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, task_size);
4672 KMP_MEMCPY(taskdata, taskdata_src, task_size);
4674 task = KMP_TASKDATA_TO_TASK(taskdata);
4678 if (!taskdata->is_taskgraph || taskloop_recur)
4679 taskdata->td_task_id = KMP_GEN_TASK_ID();
4680 else if (taskdata->is_taskgraph &&
4681 __kmp_tdg_is_recording(taskdata_src->tdg->tdg_status))
4682 taskdata->td_task_id = KMP_ATOMIC_INC(&__kmp_tdg_task_id);
4684 taskdata->td_task_id = KMP_GEN_TASK_ID();
4686 if (task->shareds != NULL) {
4687 shareds_offset = (
char *)task_src->shareds - (
char *)taskdata_src;
4688 task->shareds = &((
char *)taskdata)[shareds_offset];
4689 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
4692 taskdata->td_alloc_thread = thread;
4693 taskdata->td_parent = parent_task;
4695 taskdata->td_taskgroup = parent_task->td_taskgroup;
4698 if (taskdata->td_flags.tiedness == TASK_TIED)
4699 taskdata->td_last_tied = taskdata;
4703 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
4704 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
4705 if (parent_task->td_taskgroup)
4706 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
4709 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT)
4710 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
4714 (
"__kmp_task_dup_alloc(exit): Th %p, created task %p, parent=%p\n",
4715 thread, taskdata, taskdata->td_parent));
4717 if (UNLIKELY(ompt_enabled.enabled))
4718 __ompt_task_init(taskdata, thread->th.th_info.ds.ds_gtid);
4727typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32);
4729KMP_BUILD_ASSERT(
sizeof(
long) == 4 ||
sizeof(
long) == 8);
4734class kmp_taskloop_bounds_t {
4736 const kmp_taskdata_t *taskdata;
4737 size_t lower_offset;
4738 size_t upper_offset;
4741 kmp_taskloop_bounds_t(kmp_task_t *_task, kmp_uint64 *lb, kmp_uint64 *ub)
4742 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(task)),
4743 lower_offset((char *)lb - (char *)task),
4744 upper_offset((char *)ub - (char *)task) {
4745 KMP_DEBUG_ASSERT((
char *)lb > (
char *)_task);
4746 KMP_DEBUG_ASSERT((
char *)ub > (
char *)_task);
4748 kmp_taskloop_bounds_t(kmp_task_t *_task,
const kmp_taskloop_bounds_t &bounds)
4749 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(_task)),
4750 lower_offset(bounds.lower_offset), upper_offset(bounds.upper_offset) {}
4751 size_t get_lower_offset()
const {
return lower_offset; }
4752 size_t get_upper_offset()
const {
return upper_offset; }
4753 kmp_uint64 get_lb()
const {
4755#if defined(KMP_GOMP_COMPAT)
4757 if (!taskdata->td_flags.native) {
4758 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4761 if (taskdata->td_size_loop_bounds == 4) {
4762 kmp_int32 *lb = RCAST(kmp_int32 *, task->shareds);
4763 retval = (kmp_int64)*lb;
4765 kmp_int64 *lb = RCAST(kmp_int64 *, task->shareds);
4766 retval = (kmp_int64)*lb;
4771 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4775 kmp_uint64 get_ub()
const {
4777#if defined(KMP_GOMP_COMPAT)
4779 if (!taskdata->td_flags.native) {
4780 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4783 if (taskdata->td_size_loop_bounds == 4) {
4784 kmp_int32 *ub = RCAST(kmp_int32 *, task->shareds) + 1;
4785 retval = (kmp_int64)*ub;
4787 kmp_int64 *ub = RCAST(kmp_int64 *, task->shareds) + 1;
4788 retval = (kmp_int64)*ub;
4792 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4796 void set_lb(kmp_uint64 lb) {
4797#if defined(KMP_GOMP_COMPAT)
4799 if (!taskdata->td_flags.native) {
4800 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4803 if (taskdata->td_size_loop_bounds == 4) {
4804 kmp_uint32 *lower = RCAST(kmp_uint32 *, task->shareds);
4805 *lower = (kmp_uint32)lb;
4807 kmp_uint64 *lower = RCAST(kmp_uint64 *, task->shareds);
4808 *lower = (kmp_uint64)lb;
4812 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4815 void set_ub(kmp_uint64 ub) {
4816#if defined(KMP_GOMP_COMPAT)
4818 if (!taskdata->td_flags.native) {
4819 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4822 if (taskdata->td_size_loop_bounds == 4) {
4823 kmp_uint32 *upper = RCAST(kmp_uint32 *, task->shareds) + 1;
4824 *upper = (kmp_uint32)ub;
4826 kmp_uint64 *upper = RCAST(kmp_uint64 *, task->shareds) + 1;
4827 *upper = (kmp_uint64)ub;
4831 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4852void __kmp_taskloop_linear(
ident_t *loc,
int gtid, kmp_task_t *task,
4853 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4854 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
4855 kmp_uint64 grainsize, kmp_uint64 extras,
4856 kmp_int64 last_chunk, kmp_uint64 tc,
4862 KMP_TIME_PARTITIONED_BLOCK(OMP_taskloop_scheduling);
4863 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
4865 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4866 kmp_uint64 lower = task_bounds.get_lb();
4867 kmp_uint64 upper = task_bounds.get_ub();
4869 kmp_info_t *thread = __kmp_threads[gtid];
4870 kmp_taskdata_t *current_task = thread->th.th_current_task;
4871 kmp_task_t *next_task;
4872 kmp_int32 lastpriv = 0;
4874 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4875 (last_chunk < 0 ? last_chunk : extras));
4876 KMP_DEBUG_ASSERT(num_tasks > extras);
4877 KMP_DEBUG_ASSERT(num_tasks > 0);
4878 KA_TRACE(20, (
"__kmp_taskloop_linear: T#%d: %lld tasks, grainsize %lld, "
4879 "extras %lld, last_chunk %lld, i=%lld,%lld(%d)%lld, dup %p\n",
4880 gtid, num_tasks, grainsize, extras, last_chunk, lower, upper,
4881 ub_glob, st, task_dup));
4884 for (i = 0; i < num_tasks; ++i) {
4885 kmp_uint64 chunk_minus_1;
4887 chunk_minus_1 = grainsize - 1;
4889 chunk_minus_1 = grainsize;
4892 upper = lower + st * chunk_minus_1;
4896 if (i == num_tasks - 1) {
4899 KMP_DEBUG_ASSERT(upper == *ub);
4900 if (upper == ub_glob)
4902 }
else if (st > 0) {
4903 KMP_DEBUG_ASSERT((kmp_uint64)st > *ub - upper);
4904 if ((kmp_uint64)st > ub_glob - upper)
4907 KMP_DEBUG_ASSERT(upper + st < *ub);
4908 if (upper - ub_glob < (kmp_uint64)(-st))
4914 next_task = __kmp_task_dup_alloc(thread, task, 0);
4916 next_task = __kmp_task_dup_alloc(thread, task);
4919 kmp_taskdata_t *next_taskdata = KMP_TASK_TO_TASKDATA(next_task);
4920 kmp_taskloop_bounds_t next_task_bounds =
4921 kmp_taskloop_bounds_t(next_task, task_bounds);
4924 next_task_bounds.set_lb(lower);
4925 if (next_taskdata->td_flags.native) {
4926 next_task_bounds.set_ub(upper + (st > 0 ? 1 : -1));
4928 next_task_bounds.set_ub(upper);
4930 if (ptask_dup != NULL)
4932 ptask_dup(next_task, task, lastpriv);
4934 (
"__kmp_taskloop_linear: T#%d; task #%llu: task %p: lower %lld, "
4935 "upper %lld stride %lld, (offsets %p %p)\n",
4936 gtid, i, next_task, lower, upper, st,
4937 next_task_bounds.get_lower_offset(),
4938 next_task_bounds.get_upper_offset()));
4940 __kmp_omp_taskloop_task(NULL, gtid, next_task,
4943 if (ompt_enabled.ompt_callback_dispatch) {
4944 OMPT_GET_DISPATCH_CHUNK(next_taskdata->ompt_task_info.dispatch_chunk,
4949 __kmp_omp_task(gtid, next_task,
true);
4954 __kmp_task_start(gtid, task, current_task);
4956 __kmp_task_finish<false>(gtid, task, current_task);
4961typedef struct __taskloop_params {
4968 kmp_uint64 num_tasks;
4969 kmp_uint64 grainsize;
4971 kmp_int64 last_chunk;
4973 kmp_uint64 num_t_min;
4977} __taskloop_params_t;
4979void __kmp_taskloop_recur(
ident_t *,
int, kmp_task_t *, kmp_uint64 *,
4980 kmp_uint64 *, kmp_int64, kmp_uint64, kmp_uint64,
4981 kmp_uint64, kmp_uint64, kmp_int64, kmp_uint64,
4989int __kmp_taskloop_task(
int gtid,
void *ptask) {
4990 __taskloop_params_t *p =
4991 (__taskloop_params_t *)((kmp_task_t *)ptask)->shareds;
4992 kmp_task_t *task = p->task;
4993 kmp_uint64 *lb = p->lb;
4994 kmp_uint64 *ub = p->ub;
4995 void *task_dup = p->task_dup;
4997 kmp_int64 st = p->st;
4998 kmp_uint64 ub_glob = p->ub_glob;
4999 kmp_uint64 num_tasks = p->num_tasks;
5000 kmp_uint64 grainsize = p->grainsize;
5001 kmp_uint64 extras = p->extras;
5002 kmp_int64 last_chunk = p->last_chunk;
5003 kmp_uint64 tc = p->tc;
5004 kmp_uint64 num_t_min = p->num_t_min;
5006 void *codeptr_ra = p->codeptr_ra;
5009 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
5010 KMP_DEBUG_ASSERT(task != NULL);
5012 (
"__kmp_taskloop_task: T#%d, task %p: %lld tasks, grainsize"
5013 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
5014 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
5017 KMP_DEBUG_ASSERT(num_tasks * 2 + 1 > num_t_min);
5018 if (num_tasks > num_t_min)
5019 __kmp_taskloop_recur(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
5020 grainsize, extras, last_chunk, tc, num_t_min,
5026 __kmp_taskloop_linear(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
5027 grainsize, extras, last_chunk, tc,
5033 KA_TRACE(40, (
"__kmp_taskloop_task(exit): T#%d\n", gtid));
5055void __kmp_taskloop_recur(
ident_t *loc,
int gtid, kmp_task_t *task,
5056 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
5057 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
5058 kmp_uint64 grainsize, kmp_uint64 extras,
5059 kmp_int64 last_chunk, kmp_uint64 tc,
5060 kmp_uint64 num_t_min,
5065 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
5066 KMP_DEBUG_ASSERT(task != NULL);
5067 KMP_DEBUG_ASSERT(num_tasks > num_t_min);
5069 (
"__kmp_taskloop_recur: T#%d, task %p: %lld tasks, grainsize"
5070 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
5071 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
5073 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
5074 kmp_uint64 lower = *lb;
5075 kmp_info_t *thread = __kmp_threads[gtid];
5077 kmp_task_t *next_task;
5078 size_t lower_offset =
5079 (
char *)lb - (
char *)task;
5080 size_t upper_offset =
5081 (
char *)ub - (
char *)task;
5083 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
5084 (last_chunk < 0 ? last_chunk : extras));
5085 KMP_DEBUG_ASSERT(num_tasks > extras);
5086 KMP_DEBUG_ASSERT(num_tasks > 0);
5089 kmp_uint64 lb1, ub0, tc0, tc1, ext0, ext1;
5090 kmp_int64 last_chunk0 = 0, last_chunk1 = 0;
5091 kmp_uint64 gr_size0 = grainsize;
5092 kmp_uint64 n_tsk0 = num_tasks >> 1;
5093 kmp_uint64 n_tsk1 = num_tasks - n_tsk0;
5094 if (last_chunk < 0) {
5096 last_chunk1 = last_chunk;
5097 tc0 = grainsize * n_tsk0;
5099 }
else if (n_tsk0 <= extras) {
5102 ext1 = extras - n_tsk0;
5103 tc0 = gr_size0 * n_tsk0;
5108 tc1 = grainsize * n_tsk1;
5111 ub0 = lower + st * (tc0 - 1);
5116 next_task = __kmp_task_dup_alloc(thread, task,
5119 next_task = __kmp_task_dup_alloc(thread, task);
5122 *(kmp_uint64 *)((
char *)next_task + lower_offset) = lb1;
5123 if (ptask_dup != NULL)
5124 ptask_dup(next_task, task, 0);
5129 kmp_taskdata_t *current_task = thread->th.th_current_task;
5130 thread->th.th_current_task = taskdata->td_parent;
5131 kmp_task_t *new_task =
5132 __kmpc_omp_task_alloc(loc, gtid, 1, 3 *
sizeof(
void *),
5133 sizeof(__taskloop_params_t), &__kmp_taskloop_task);
5135 thread->th.th_current_task = current_task;
5136 __taskloop_params_t *p = (__taskloop_params_t *)new_task->shareds;
5137 p->task = next_task;
5138 p->lb = (kmp_uint64 *)((
char *)next_task + lower_offset);
5139 p->ub = (kmp_uint64 *)((
char *)next_task + upper_offset);
5140 p->task_dup = task_dup;
5142 p->ub_glob = ub_glob;
5143 p->num_tasks = n_tsk1;
5144 p->grainsize = grainsize;
5146 p->last_chunk = last_chunk1;
5148 p->num_t_min = num_t_min;
5150 p->codeptr_ra = codeptr_ra;
5154 kmp_taskdata_t *new_task_data = KMP_TASK_TO_TASKDATA(new_task);
5155 new_task_data->tdg = taskdata->tdg;
5156 new_task_data->is_taskgraph = 0;
5161 __kmp_omp_taskloop_task(NULL, gtid, new_task, codeptr_ra);
5163 __kmp_omp_task(gtid, new_task,
true);
5167 if (n_tsk0 > num_t_min)
5168 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0, gr_size0,
5169 ext0, last_chunk0, tc0, num_t_min,
5175 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0,
5176 gr_size0, ext0, last_chunk0, tc0,
5182 KA_TRACE(40, (
"__kmp_taskloop_recur(exit): T#%d\n", gtid));
5185static void __kmp_taskloop(
ident_t *loc,
int gtid, kmp_task_t *task,
int if_val,
5186 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
5187 int nogroup,
int sched, kmp_uint64 grainsize,
5188 int modifier,
void *task_dup) {
5189 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
5190 KMP_DEBUG_ASSERT(task != NULL);
5192#if OMPT_SUPPORT && OMPT_OPTIONAL
5193 OMPT_STORE_RETURN_ADDRESS(gtid);
5195 __kmpc_taskgroup(loc, gtid);
5199 KMP_ATOMIC_DEC(&__kmp_tdg_task_id);
5203 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
5206 kmp_uint64 lower = task_bounds.get_lb();
5207 kmp_uint64 upper = task_bounds.get_ub();
5208 kmp_uint64 ub_glob = upper;
5209 kmp_uint64 num_tasks = 0, extras = 0;
5210 kmp_int64 last_chunk =
5212 kmp_uint64 num_tasks_min = __kmp_taskloop_min_tasks;
5213 kmp_info_t *thread = __kmp_threads[gtid];
5214 kmp_taskdata_t *current_task = thread->th.th_current_task;
5216 KA_TRACE(20, (
"__kmp_taskloop: T#%d, task %p, lb %lld, ub %lld, st %lld, "
5217 "grain %llu(%d, %d), dup %p\n",
5218 gtid, taskdata, lower, upper, st, grainsize, sched, modifier,
5223 tc = upper - lower + 1;
5224 }
else if (st < 0) {
5225 tc = (lower - upper) / (-st) + 1;
5227 tc = (upper - lower) / st + 1;
5230 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d zero-trip loop\n", gtid));
5232 __kmp_task_start(gtid, task, current_task);
5234 __kmp_task_finish<false>(gtid, task, current_task);
5238#if OMPT_SUPPORT && OMPT_OPTIONAL
5239 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
5240 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
5241 if (ompt_enabled.ompt_callback_work) {
5242 ompt_callbacks.ompt_callback(ompt_callback_work)(
5243 ompt_work_taskloop, ompt_scope_begin, &(team_info->parallel_data),
5244 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
5248 if (num_tasks_min == 0)
5251 KMP_MIN(thread->th.th_team_nproc * 10, INITIAL_TASK_DEQUE_SIZE);
5257 grainsize = thread->th.th_team_nproc * 10;
5260 if (grainsize > tc) {
5265 num_tasks = grainsize;
5266 grainsize = tc / num_tasks;
5267 extras = tc % num_tasks;
5271 if (grainsize > tc) {
5277 num_tasks = (tc + grainsize - 1) / grainsize;
5278 last_chunk = tc - (num_tasks * grainsize);
5281 num_tasks = tc / grainsize;
5283 grainsize = tc / num_tasks;
5284 extras = tc % num_tasks;
5289 KMP_ASSERT2(0,
"unknown scheduling of taskloop");
5292 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
5293 (last_chunk < 0 ? last_chunk : extras));
5294 KMP_DEBUG_ASSERT(num_tasks > extras);
5295 KMP_DEBUG_ASSERT(num_tasks > 0);
5301 taskdata->td_flags.task_serial = 1;
5302 taskdata->td_flags.tiedness = TASK_TIED;
5304 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5305 grainsize, extras, last_chunk, tc,
5307 OMPT_GET_RETURN_ADDRESS(0),
5312 }
else if (num_tasks > num_tasks_min && !taskdata->td_flags.native) {
5313 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go recursive: tc %llu, #tasks %llu"
5314 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
5315 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
5317 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5318 grainsize, extras, last_chunk, tc, num_tasks_min,
5320 OMPT_GET_RETURN_ADDRESS(0),
5324 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go linear: tc %llu, #tasks %llu"
5325 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
5326 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
5328 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5329 grainsize, extras, last_chunk, tc,
5331 OMPT_GET_RETURN_ADDRESS(0),
5336#if OMPT_SUPPORT && OMPT_OPTIONAL
5337 if (ompt_enabled.ompt_callback_work) {
5338 ompt_callbacks.ompt_callback(ompt_callback_work)(
5339 ompt_work_taskloop, ompt_scope_end, &(team_info->parallel_data),
5340 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
5345#if OMPT_SUPPORT && OMPT_OPTIONAL
5346 OMPT_STORE_RETURN_ADDRESS(gtid);
5348 __kmpc_end_taskgroup(loc, gtid);
5350 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d\n", gtid));
5370 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
int nogroup,
5371 int sched, kmp_uint64 grainsize,
void *task_dup) {
5372 __kmp_assert_valid_gtid(gtid);
5373 KA_TRACE(20, (
"__kmpc_taskloop(enter): T#%d\n", gtid));
5374 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
5376 KA_TRACE(20, (
"__kmpc_taskloop(exit): T#%d\n", gtid));
5397 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
5398 int nogroup,
int sched, kmp_uint64 grainsize,
5399 int modifier,
void *task_dup) {
5400 __kmp_assert_valid_gtid(gtid);
5401 KA_TRACE(20, (
"__kmpc_taskloop_5(enter): T#%d\n", gtid));
5402 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
5403 modifier, task_dup);
5404 KA_TRACE(20, (
"__kmpc_taskloop_5(exit): T#%d\n", gtid));
5416 if (gtid == KMP_GTID_DNE)
5419 kmp_info_t *thread = __kmp_thread_from_gtid(gtid);
5420 kmp_taskdata_t *taskdata = thread->th.th_current_task;
5425 return &taskdata->td_target_data.async_handle;
5437 if (gtid == KMP_GTID_DNE)
5440 kmp_info_t *thread = __kmp_thread_from_gtid(gtid);
5441 kmp_taskdata_t *taskdata = thread->th.th_current_task;
5446 return taskdata->td_task_team != NULL;
5455static kmp_tdg_info_t *__kmp_find_tdg(kmp_int32 tdg_id) {
5456 kmp_tdg_info_t *res =
nullptr;
5457 if (__kmp_max_tdgs == 0)
5460 if (__kmp_global_tdgs == NULL)
5461 __kmp_global_tdgs = (kmp_tdg_info_t **)__kmp_allocate(
5462 sizeof(kmp_tdg_info_t *) * __kmp_max_tdgs);
5464 if ((__kmp_global_tdgs[tdg_id]) &&
5465 (__kmp_global_tdgs[tdg_id]->tdg_status != KMP_TDG_NONE))
5466 res = __kmp_global_tdgs[tdg_id];
5472void __kmp_print_tdg_dot(kmp_tdg_info_t *tdg) {
5473 kmp_int32 tdg_id = tdg->tdg_id;
5474 KA_TRACE(10, (
"__kmp_print_tdg_dot(enter): T#%d tdg_id=%d \n", gtid, tdg_id));
5477 sprintf(file_name,
"tdg_%d.dot", tdg_id);
5480 kmp_int32 num_tasks = KMP_ATOMIC_LD_RLX(&tdg->num_tasks);
5484 " subgraph cluster {\n"
5487 for (kmp_int32 i = 0; i < num_tasks; i++) {
5488 fprintf(tdg_file,
" %d[style=bold]\n", i);
5490 fprintf(tdg_file,
" }\n");
5491 for (kmp_int32 i = 0; i < num_tasks; i++) {
5492 kmp_int32 nsuccessors = tdg->record_map[i].nsuccessors;
5493 kmp_int32 *successors = tdg->record_map[i].successors;
5494 if (nsuccessors > 0) {
5495 for (kmp_int32 j = 0; j < nsuccessors; j++)
5496 fprintf(tdg_file,
" %d -> %d \n", i, successors[j]);
5499 fprintf(tdg_file,
"}");
5500 KA_TRACE(10, (
"__kmp_print_tdg_dot(exit): T#%d tdg_id=%d \n", gtid, tdg_id));
5507void __kmp_exec_tdg(kmp_int32 gtid, kmp_tdg_info_t *tdg) {
5508 KMP_DEBUG_ASSERT(tdg->tdg_status == KMP_TDG_READY);
5509 KA_TRACE(10, (
"__kmp_exec_tdg(enter): T#%d tdg_id=%d num_roots=%d\n", gtid,
5510 tdg->tdg_id, tdg->num_roots));
5511 kmp_node_info_t *this_record_map = tdg->record_map;
5512 kmp_int32 *this_root_tasks = tdg->root_tasks;
5513 kmp_int32 this_num_roots = tdg->num_roots;
5514 kmp_int32 this_num_tasks = KMP_ATOMIC_LD_RLX(&tdg->num_tasks);
5516 kmp_info_t *thread = __kmp_threads[gtid];
5517 kmp_taskdata_t *parent_task = thread->th.th_current_task;
5519 if (tdg->rec_taskred_data) {
5523 for (kmp_int32 j = 0; j < this_num_tasks; j++) {
5524 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(this_record_map[j].task);
5526 td->td_parent = parent_task;
5527 this_record_map[j].parent_task = parent_task;
5529 kmp_taskgroup_t *parent_taskgroup =
5530 this_record_map[j].parent_task->td_taskgroup;
5532 KMP_ATOMIC_ST_RLX(&this_record_map[j].npredecessors_counter,
5533 this_record_map[j].npredecessors);
5534 KMP_ATOMIC_INC(&this_record_map[j].parent_task->td_incomplete_child_tasks);
5536 if (parent_taskgroup) {
5537 KMP_ATOMIC_INC(&parent_taskgroup->count);
5539 td->td_taskgroup = parent_taskgroup;
5540 }
else if (td->td_taskgroup !=
nullptr) {
5542 td->td_taskgroup =
nullptr;
5544 if (this_record_map[j].parent_task->td_flags.tasktype == TASK_EXPLICIT)
5545 KMP_ATOMIC_INC(&this_record_map[j].parent_task->td_allocated_child_tasks);
5548 for (kmp_int32 j = 0; j < this_num_roots; ++j) {
5549 __kmp_omp_task(gtid, this_record_map[this_root_tasks[j]].task,
true);
5551 KA_TRACE(10, (
"__kmp_exec_tdg(exit): T#%d tdg_id=%d num_roots=%d\n", gtid,
5552 tdg->tdg_id, tdg->num_roots));
5560static inline void __kmp_start_record(kmp_int32 gtid,
5561 kmp_taskgraph_flags_t *flags,
5563 kmp_tdg_info_t *tdg =
5564 (kmp_tdg_info_t *)__kmp_allocate(
sizeof(kmp_tdg_info_t));
5565 __kmp_global_tdgs[__kmp_curr_tdg_idx] = tdg;
5567 tdg->tdg_id = tdg_id;
5568 tdg->map_size = INIT_MAPSIZE;
5569 tdg->num_roots = -1;
5570 tdg->root_tasks =
nullptr;
5571 tdg->tdg_status = KMP_TDG_RECORDING;
5572 tdg->rec_num_taskred = 0;
5573 tdg->rec_taskred_data =
nullptr;
5574 KMP_ATOMIC_ST_RLX(&tdg->num_tasks, 0);
5577 kmp_node_info_t *this_record_map =
5578 (kmp_node_info_t *)__kmp_allocate(INIT_MAPSIZE *
sizeof(kmp_node_info_t));
5579 for (kmp_int32 i = 0; i < INIT_MAPSIZE; i++) {
5580 kmp_int32 *successorsList =
5581 (kmp_int32 *)__kmp_allocate(__kmp_successors_size *
sizeof(kmp_int32));
5582 this_record_map[i].task =
nullptr;
5583 this_record_map[i].successors = successorsList;
5584 this_record_map[i].nsuccessors = 0;
5585 this_record_map[i].npredecessors = 0;
5586 this_record_map[i].successors_size = __kmp_successors_size;
5587 KMP_ATOMIC_ST_RLX(&this_record_map[i].npredecessors_counter, 0);
5590 __kmp_global_tdgs[__kmp_curr_tdg_idx]->record_map = this_record_map;
5600kmp_int32 __kmpc_start_record_task(
ident_t *loc_ref, kmp_int32 gtid,
5601 kmp_int32 input_flags, kmp_int32 tdg_id) {
5604 kmp_taskgraph_flags_t *flags = (kmp_taskgraph_flags_t *)&input_flags;
5606 (
"__kmpc_start_record_task(enter): T#%d loc=%p flags=%d tdg_id=%d\n",
5607 gtid, loc_ref, input_flags, tdg_id));
5609 if (__kmp_max_tdgs == 0) {
5612 (
"__kmpc_start_record_task(abandon): T#%d loc=%p flags=%d tdg_id = %d, "
5613 "__kmp_max_tdgs = 0\n",
5614 gtid, loc_ref, input_flags, tdg_id));
5618 __kmpc_taskgroup(loc_ref, gtid);
5619 if (kmp_tdg_info_t *tdg = __kmp_find_tdg(tdg_id)) {
5621 __kmp_exec_tdg(gtid, tdg);
5624 __kmp_curr_tdg_idx = tdg_id;
5625 KMP_DEBUG_ASSERT(__kmp_curr_tdg_idx < __kmp_max_tdgs);
5626 __kmp_start_record(gtid, flags, tdg_id);
5630 KA_TRACE(10, (
"__kmpc_start_record_task(exit): T#%d TDG %d starts to %s\n",
5631 gtid, tdg_id, res ?
"record" :
"execute"));
5638void __kmp_end_record(kmp_int32 gtid, kmp_tdg_info_t *tdg) {
5640 kmp_node_info_t *this_record_map = tdg->record_map;
5641 kmp_int32 this_num_tasks = KMP_ATOMIC_LD_RLX(&tdg->num_tasks);
5642 kmp_int32 *this_root_tasks =
5643 (kmp_int32 *)__kmp_allocate(this_num_tasks *
sizeof(kmp_int32));
5644 kmp_int32 this_map_size = tdg->map_size;
5645 kmp_int32 this_num_roots = 0;
5646 kmp_info_t *thread = __kmp_threads[gtid];
5648 for (kmp_int32 i = 0; i < this_num_tasks; i++) {
5649 if (this_record_map[i].npredecessors == 0) {
5650 this_root_tasks[this_num_roots++] = i;
5655 tdg->map_size = this_map_size;
5656 tdg->num_roots = this_num_roots;
5657 tdg->root_tasks = this_root_tasks;
5658 KMP_DEBUG_ASSERT(tdg->tdg_status == KMP_TDG_RECORDING);
5659 tdg->tdg_status = KMP_TDG_READY;
5661 if (thread->th.th_current_task->td_dephash) {
5662 __kmp_dephash_free(thread, thread->th.th_current_task->td_dephash);
5663 thread->th.th_current_task->td_dephash = NULL;
5667 for (kmp_int32 i = 0; i < this_num_tasks; i++) {
5668 KMP_ATOMIC_ST_RLX(&this_record_map[i].npredecessors_counter,
5669 this_record_map[i].npredecessors);
5671 KMP_ATOMIC_ST_RLX(&__kmp_tdg_task_id, 0);
5674 __kmp_print_tdg_dot(tdg);
5684void __kmpc_end_record_task(
ident_t *loc_ref, kmp_int32 gtid,
5685 kmp_int32 input_flags, kmp_int32 tdg_id) {
5686 kmp_tdg_info_t *tdg = __kmp_find_tdg(tdg_id);
5688 KA_TRACE(10, (
"__kmpc_end_record_task(enter): T#%d loc=%p finishes recording"
5689 " tdg=%d with flags=%d\n",
5690 gtid, loc_ref, tdg_id, input_flags));
5691 if (__kmp_max_tdgs) {
5693 __kmpc_end_taskgroup(loc_ref, gtid);
5694 if (__kmp_tdg_is_recording(tdg->tdg_status))
5695 __kmp_end_record(gtid, tdg);
5697 KA_TRACE(10, (
"__kmpc_end_record_task(exit): T#%d loc=%p finished recording"
5698 " tdg=%d, its status is now READY\n",
5699 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