16 #include "kmp_stats.h"
17 #include "kmp_wait_release.h"
18 #include "kmp_taskdeps.h"
21 #include "ompt-specific.h"
24 #if ENABLE_LIBOMPTARGET
25 static void (*tgt_target_nowait_query)(
void **);
27 void __kmp_init_target_task() {
28 *(
void **)(&tgt_target_nowait_query) = KMP_DLSYM(
"__tgt_target_nowait_query");
33 static void __kmp_enable_tasking(kmp_task_team_t *task_team,
34 kmp_info_t *this_thr);
35 static void __kmp_alloc_task_deque(kmp_info_t *thread,
36 kmp_thread_data_t *thread_data);
37 static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
38 kmp_task_team_t *task_team);
39 static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask);
41 static kmp_tdg_info_t *__kmp_find_tdg(kmp_int32 tdg_id);
42 int __kmp_taskloop_task(
int gtid,
void *ptask);
45 #ifdef BUILD_TIED_TASK_STACK
54 static 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",
108 static 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;
129 static 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;
158 static 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,
217 static 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,
263 static 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;
312 static 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;
338 static 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;
357 static 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;
402 static 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;
477 static 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;
612 void __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));
634 void __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));
670 static 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));
720 static inline void __ompt_task_start(kmp_task_t *task,
721 kmp_taskdata_t *current_task,
723 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
724 ompt_task_status_t status = ompt_task_switch;
725 if (__kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded) {
726 status = ompt_task_yield;
727 __kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded = 0;
730 if (ompt_enabled.ompt_callback_task_schedule) {
731 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
732 &(current_task->ompt_task_info.task_data), status,
733 &(taskdata->ompt_task_info.task_data));
735 taskdata->ompt_task_info.scheduling_parent = current_task;
740 static inline void __ompt_task_finish(kmp_task_t *task,
741 kmp_taskdata_t *resumed_task,
742 ompt_task_status_t status) {
743 if (ompt_enabled.ompt_callback_task_schedule) {
744 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
745 if (__kmp_omp_cancellation && taskdata->td_taskgroup &&
746 taskdata->td_taskgroup->cancel_request == cancel_taskgroup) {
747 status = ompt_task_cancel;
751 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
752 &(taskdata->ompt_task_info.task_data), status,
753 (resumed_task ? &(resumed_task->ompt_task_info.task_data) : NULL));
759 static void __kmpc_omp_task_begin_if0_template(
ident_t *loc_ref, kmp_int32 gtid,
762 void *return_address) {
763 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
764 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
766 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(enter): T#%d loc=%p task=%p "
768 gtid, loc_ref, taskdata, current_task));
770 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
773 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
774 KMP_DEBUG_USE_VAR(counter);
775 KA_TRACE(20, (
"__kmpc_omp_task_begin_if0: T#%d untied_count (%d) "
776 "incremented for task %p\n",
777 gtid, counter, taskdata));
780 taskdata->td_flags.task_serial =
782 __kmp_task_start(gtid, task, current_task);
786 if (current_task->ompt_task_info.frame.enter_frame.ptr == NULL) {
787 current_task->ompt_task_info.frame.enter_frame.ptr =
788 taskdata->ompt_task_info.frame.exit_frame.ptr = frame_address;
789 current_task->ompt_task_info.frame.enter_frame_flags =
790 taskdata->ompt_task_info.frame.exit_frame_flags =
791 OMPT_FRAME_FLAGS_APP;
793 if (ompt_enabled.ompt_callback_task_create) {
794 ompt_task_info_t *parent_info = &(current_task->ompt_task_info);
795 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
796 &(parent_info->task_data), &(parent_info->frame),
797 &(taskdata->ompt_task_info.task_data),
798 TASK_TYPE_DETAILS_FORMAT(taskdata), 0, return_address);
800 __ompt_task_start(task, current_task, gtid);
804 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(exit): T#%d loc=%p task=%p,\n", gtid,
810 static void __kmpc_omp_task_begin_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
813 void *return_address) {
814 __kmpc_omp_task_begin_if0_template<true>(loc_ref, gtid, task, frame_address,
831 __attribute__((target(
"backchain")))
833 void __kmpc_omp_task_begin_if0(
ident_t *loc_ref, kmp_int32 gtid,
836 if (UNLIKELY(ompt_enabled.enabled)) {
837 OMPT_STORE_RETURN_ADDRESS(gtid);
838 __kmpc_omp_task_begin_if0_ompt(loc_ref, gtid, task,
839 OMPT_GET_FRAME_ADDRESS(1),
840 OMPT_LOAD_RETURN_ADDRESS(gtid));
844 __kmpc_omp_task_begin_if0_template<false>(loc_ref, gtid, task, NULL, NULL);
850 void __kmpc_omp_task_begin(
ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task) {
851 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
855 (
"__kmpc_omp_task_begin(enter): T#%d loc=%p task=%p current_task=%p\n",
856 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task), current_task));
858 __kmp_task_start(gtid, task, current_task);
860 KA_TRACE(10, (
"__kmpc_omp_task_begin(exit): T#%d loc=%p task=%p,\n", gtid,
861 loc_ref, KMP_TASK_TO_TASKDATA(task)));
871 static void __kmp_free_task(kmp_int32 gtid, kmp_taskdata_t *taskdata,
872 kmp_info_t *thread) {
873 KA_TRACE(30, (
"__kmp_free_task: T#%d freeing data from task %p\n", gtid,
877 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
878 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0);
879 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 1);
880 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
881 KMP_DEBUG_ASSERT(taskdata->td_allocated_child_tasks == 0 ||
882 taskdata->td_flags.task_serial == 1);
883 KMP_DEBUG_ASSERT(taskdata->td_incomplete_child_tasks == 0);
884 kmp_task_t *task = KMP_TASKDATA_TO_TASK(taskdata);
886 task->data1.destructors = NULL;
887 task->data2.priority = 0;
889 taskdata->td_flags.freed = 1;
892 if (!taskdata->is_taskgraph) {
896 __kmp_fast_free(thread, taskdata);
898 __kmp_thread_free(thread, taskdata);
902 taskdata->td_flags.complete = 0;
903 taskdata->td_flags.started = 0;
904 taskdata->td_flags.freed = 0;
905 taskdata->td_flags.executing = 0;
906 taskdata->td_flags.task_serial =
907 (taskdata->td_parent->td_flags.final ||
908 taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser);
911 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
912 KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0);
914 KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1);
918 KA_TRACE(20, (
"__kmp_free_task: T#%d freed task %p\n", gtid, taskdata));
927 static void __kmp_free_task_and_ancestors(kmp_int32 gtid,
928 kmp_taskdata_t *taskdata,
929 kmp_info_t *thread) {
932 kmp_int32 team_serial =
933 (taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) &&
934 !taskdata->td_flags.proxy;
935 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
937 kmp_int32 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
938 KMP_DEBUG_ASSERT(children >= 0);
941 while (children == 0) {
942 kmp_taskdata_t *parent_taskdata = taskdata->td_parent;
944 KA_TRACE(20, (
"__kmp_free_task_and_ancestors(enter): T#%d task %p complete "
945 "and freeing itself\n",
949 __kmp_free_task(gtid, taskdata, thread);
951 taskdata = parent_taskdata;
957 if (taskdata->td_flags.tasktype == TASK_IMPLICIT) {
958 if (taskdata->td_dephash) {
959 int children = KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks);
960 kmp_tasking_flags_t flags_old = taskdata->td_flags;
961 if (children == 0 && flags_old.complete == 1) {
962 kmp_tasking_flags_t flags_new = flags_old;
963 flags_new.complete = 0;
964 if (KMP_COMPARE_AND_STORE_ACQ32(
965 RCAST(kmp_int32 *, &taskdata->td_flags),
966 *RCAST(kmp_int32 *, &flags_old),
967 *RCAST(kmp_int32 *, &flags_new))) {
968 KA_TRACE(100, (
"__kmp_free_task_and_ancestors: T#%d cleans "
969 "dephash of implicit task %p\n",
972 __kmp_dephash_free_entries(thread, taskdata->td_dephash);
979 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
980 KMP_DEBUG_ASSERT(children >= 0);
984 20, (
"__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; "
985 "not freeing it yet\n",
986 gtid, taskdata, children));
997 static bool __kmp_track_children_task(kmp_taskdata_t *taskdata) {
998 kmp_tasking_flags_t flags = taskdata->td_flags;
999 bool ret = !(flags.team_serial || flags.tasking_ser);
1000 ret = ret || flags.proxy == TASK_PROXY ||
1001 flags.detachable == TASK_DETACHABLE || flags.hidden_helper;
1003 KMP_ATOMIC_LD_ACQ(&taskdata->td_parent->td_incomplete_child_tasks) > 0;
1005 if (taskdata->td_taskgroup && taskdata->is_taskgraph)
1006 ret = ret || KMP_ATOMIC_LD_ACQ(&taskdata->td_taskgroup->count) > 0;
1020 template <
bool ompt>
1021 static void __kmp_task_finish(kmp_int32 gtid, kmp_task_t *task,
1022 kmp_taskdata_t *resumed_task) {
1023 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
1024 kmp_info_t *thread = __kmp_threads[gtid];
1025 kmp_task_team_t *task_team =
1026 thread->th.th_task_team;
1032 kmp_int32 children = 0;
1034 KA_TRACE(10, (
"__kmp_task_finish(enter): T#%d finishing task %p and resuming "
1036 gtid, taskdata, resumed_task));
1038 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
1041 is_taskgraph = taskdata->is_taskgraph;
1045 #ifdef BUILD_TIED_TASK_STACK
1046 if (taskdata->td_flags.tiedness == TASK_TIED) {
1047 __kmp_pop_task_stack(gtid, thread, taskdata);
1051 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
1054 kmp_int32 counter = KMP_ATOMIC_DEC(&taskdata->td_untied_count) - 1;
1057 (
"__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n",
1058 gtid, counter, taskdata));
1062 if (resumed_task == NULL) {
1063 KMP_DEBUG_ASSERT(taskdata->td_flags.task_serial);
1064 resumed_task = taskdata->td_parent;
1067 thread->th.th_current_task = resumed_task;
1068 resumed_task->td_flags.executing = 1;
1069 KA_TRACE(10, (
"__kmp_task_finish(exit): T#%d partially done task %p, "
1070 "resuming task %p\n",
1071 gtid, taskdata, resumed_task));
1079 (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) ==
1080 taskdata->td_flags.task_serial);
1081 if (taskdata->td_flags.task_serial) {
1082 if (resumed_task == NULL) {
1083 resumed_task = taskdata->td_parent;
1087 KMP_DEBUG_ASSERT(resumed_task !=
1097 if (UNLIKELY(taskdata->td_flags.destructors_thunk)) {
1098 kmp_routine_entry_t destr_thunk = task->data1.destructors;
1099 KMP_ASSERT(destr_thunk);
1100 destr_thunk(gtid, task);
1103 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
1104 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 1);
1105 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
1107 bool completed =
true;
1108 if (UNLIKELY(taskdata->td_flags.detachable == TASK_DETACHABLE)) {
1109 if (taskdata->td_allow_completion_event.type ==
1110 KMP_EVENT_ALLOW_COMPLETION) {
1112 __kmp_acquire_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
1113 if (taskdata->td_allow_completion_event.type ==
1114 KMP_EVENT_ALLOW_COMPLETION) {
1116 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
1117 taskdata->td_flags.executing = 0;
1124 __ompt_task_finish(task, resumed_task, ompt_task_detach);
1130 taskdata->td_flags.proxy = TASK_PROXY;
1133 __kmp_release_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
1138 if (taskdata->td_target_data.async_handle != NULL) {
1144 __ompt_task_finish(task, resumed_task, ompt_task_switch);
1147 __kmpc_give_task(task, __kmp_tid_from_gtid(gtid));
1148 if (KMP_HIDDEN_HELPER_THREAD(gtid))
1149 __kmp_hidden_helper_worker_thread_signal();
1154 taskdata->td_flags.complete = 1;
1156 taskdata->td_flags.onced = 1;
1162 __ompt_task_finish(task, resumed_task, ompt_task_complete);
1166 if (__kmp_track_children_task(taskdata)) {
1167 __kmp_release_deps(gtid, taskdata);
1172 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks);
1173 KMP_DEBUG_ASSERT(children >= 0);
1175 if (taskdata->td_taskgroup && !taskdata->is_taskgraph)
1177 if (taskdata->td_taskgroup)
1179 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
1180 }
else if (task_team && (task_team->tt.tt_found_proxy_tasks ||
1181 task_team->tt.tt_hidden_helper_task_encountered)) {
1184 __kmp_release_deps(gtid, taskdata);
1190 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
1191 taskdata->td_flags.executing = 0;
1194 if (taskdata->td_flags.hidden_helper) {
1196 KMP_ASSERT(KMP_HIDDEN_HELPER_THREAD(gtid));
1197 KMP_ATOMIC_DEC(&__kmp_unexecuted_hidden_helper_tasks);
1202 20, (
"__kmp_task_finish: T#%d finished task %p, %d incomplete children\n",
1203 gtid, taskdata, children));
1209 thread->th.th_current_task = resumed_task;
1211 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
1215 resumed_task->td_flags.executing = 1;
1218 if (is_taskgraph && __kmp_track_children_task(taskdata) &&
1219 taskdata->td_taskgroup) {
1226 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
1231 10, (
"__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n",
1232 gtid, taskdata, resumed_task));
1237 template <
bool ompt>
1238 static void __kmpc_omp_task_complete_if0_template(
ident_t *loc_ref,
1241 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n",
1242 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
1243 KMP_DEBUG_ASSERT(gtid >= 0);
1245 __kmp_task_finish<ompt>(gtid, task, NULL);
1247 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n",
1248 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
1252 ompt_frame_t *ompt_frame;
1253 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
1254 ompt_frame->enter_frame = ompt_data_none;
1255 ompt_frame->enter_frame_flags = OMPT_FRAME_FLAGS_RUNTIME;
1264 void __kmpc_omp_task_complete_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
1266 __kmpc_omp_task_complete_if0_template<true>(loc_ref, gtid, task);
1275 void __kmpc_omp_task_complete_if0(
ident_t *loc_ref, kmp_int32 gtid,
1278 if (UNLIKELY(ompt_enabled.enabled)) {
1279 __kmpc_omp_task_complete_if0_ompt(loc_ref, gtid, task);
1283 __kmpc_omp_task_complete_if0_template<false>(loc_ref, gtid, task);
1289 void __kmpc_omp_task_complete(
ident_t *loc_ref, kmp_int32 gtid,
1291 KA_TRACE(10, (
"__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n", gtid,
1292 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1294 __kmp_task_finish<false>(gtid, task,
1297 KA_TRACE(10, (
"__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n", gtid,
1298 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1314 void __kmp_init_implicit_task(
ident_t *loc_ref, kmp_info_t *this_thr,
1315 kmp_team_t *team,
int tid,
int set_curr_task) {
1316 kmp_taskdata_t *task = &team->t.t_implicit_task_taskdata[tid];
1320 (
"__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n",
1321 tid, team, task, set_curr_task ?
"TRUE" :
"FALSE"));
1323 task->td_task_id = KMP_GEN_TASK_ID();
1324 task->td_team = team;
1327 task->td_ident = loc_ref;
1328 task->td_taskwait_ident = NULL;
1329 task->td_taskwait_counter = 0;
1330 task->td_taskwait_thread = 0;
1332 task->td_flags.tiedness = TASK_TIED;
1333 task->td_flags.tasktype = TASK_IMPLICIT;
1334 task->td_flags.proxy = TASK_FULL;
1337 task->td_flags.task_serial = 1;
1338 task->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1339 task->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1341 task->td_flags.started = 1;
1342 task->td_flags.executing = 1;
1343 task->td_flags.complete = 0;
1344 task->td_flags.freed = 0;
1346 task->td_flags.onced = 0;
1349 task->td_depnode = NULL;
1350 task->td_last_tied = task;
1351 task->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1353 if (set_curr_task) {
1354 KMP_ATOMIC_ST_REL(&task->td_incomplete_child_tasks, 0);
1356 KMP_ATOMIC_ST_REL(&task->td_allocated_child_tasks, 0);
1357 task->td_taskgroup = NULL;
1358 task->td_dephash = NULL;
1359 __kmp_push_current_task_to_thread(this_thr, team, tid);
1361 KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0);
1362 KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0);
1366 if (UNLIKELY(ompt_enabled.enabled))
1367 __ompt_task_init(task, tid);
1370 KF_TRACE(10, (
"__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n", tid,
1379 void __kmp_finish_implicit_task(kmp_info_t *thread) {
1380 kmp_taskdata_t *task = thread->th.th_current_task;
1381 if (task->td_dephash) {
1383 task->td_flags.complete = 1;
1385 task->td_flags.onced = 1;
1387 children = KMP_ATOMIC_LD_ACQ(&task->td_incomplete_child_tasks);
1388 kmp_tasking_flags_t flags_old = task->td_flags;
1389 if (children == 0 && flags_old.complete == 1) {
1390 kmp_tasking_flags_t flags_new = flags_old;
1391 flags_new.complete = 0;
1392 if (KMP_COMPARE_AND_STORE_ACQ32(RCAST(kmp_int32 *, &task->td_flags),
1393 *RCAST(kmp_int32 *, &flags_old),
1394 *RCAST(kmp_int32 *, &flags_new))) {
1395 KA_TRACE(100, (
"__kmp_finish_implicit_task: T#%d cleans "
1396 "dephash of implicit task %p\n",
1397 thread->th.th_info.ds.ds_gtid, task));
1398 __kmp_dephash_free_entries(thread, task->td_dephash);
1408 void __kmp_free_implicit_task(kmp_info_t *thread) {
1409 kmp_taskdata_t *task = thread->th.th_current_task;
1410 if (task && task->td_dephash) {
1411 __kmp_dephash_free(thread, task->td_dephash);
1412 task->td_dephash = NULL;
1418 static size_t __kmp_round_up_to_val(
size_t size,
size_t val) {
1419 if (size & (val - 1)) {
1421 if (size <= KMP_SIZE_T_MAX - val) {
1440 kmp_task_t *__kmp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1441 kmp_tasking_flags_t *flags,
1442 size_t sizeof_kmp_task_t,
size_t sizeof_shareds,
1443 kmp_routine_entry_t task_entry) {
1445 kmp_taskdata_t *taskdata;
1446 kmp_info_t *thread = __kmp_threads[gtid];
1447 kmp_team_t *team = thread->th.th_team;
1448 kmp_taskdata_t *parent_task = thread->th.th_current_task;
1449 size_t shareds_offset;
1451 if (UNLIKELY(!TCR_4(__kmp_init_middle)))
1452 __kmp_middle_initialize();
1454 if (flags->hidden_helper) {
1455 if (__kmp_enable_hidden_helper) {
1456 if (!TCR_4(__kmp_init_hidden_helper))
1457 __kmp_hidden_helper_initialize();
1460 flags->hidden_helper = FALSE;
1464 KA_TRACE(10, (
"__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) "
1465 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1466 gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t,
1467 sizeof_shareds, task_entry));
1469 KMP_DEBUG_ASSERT(parent_task);
1470 if (parent_task->td_flags.final) {
1471 if (flags->merged_if0) {
1476 if (flags->tiedness == TASK_UNTIED && !team->t.t_serialized) {
1480 KMP_CHECK_UPDATE(thread->th.th_task_team->tt.tt_untied_task_encountered, 1);
1486 if (UNLIKELY(flags->proxy == TASK_PROXY ||
1487 flags->detachable == TASK_DETACHABLE || flags->hidden_helper)) {
1488 if (flags->proxy == TASK_PROXY) {
1489 flags->tiedness = TASK_UNTIED;
1490 flags->merged_if0 = 1;
1494 if ((thread->th.th_task_team) == NULL) {
1497 KMP_DEBUG_ASSERT(team->t.t_serialized);
1499 (
"T#%d creating task team in __kmp_task_alloc for proxy task\n",
1501 __kmp_task_team_setup(thread, team);
1502 thread->th.th_task_team = team->t.t_task_team[thread->th.th_task_state];
1504 kmp_task_team_t *task_team = thread->th.th_task_team;
1507 if (!KMP_TASKING_ENABLED(task_team)) {
1510 (
"T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid));
1511 __kmp_enable_tasking(task_team, thread);
1512 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
1513 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
1515 if (thread_data->td.td_deque == NULL) {
1516 __kmp_alloc_task_deque(thread, thread_data);
1520 if ((flags->proxy == TASK_PROXY || flags->detachable == TASK_DETACHABLE) &&
1521 task_team->tt.tt_found_proxy_tasks == FALSE)
1522 TCW_4(task_team->tt.tt_found_proxy_tasks, TRUE);
1523 if (flags->hidden_helper &&
1524 task_team->tt.tt_hidden_helper_task_encountered == FALSE)
1525 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, TRUE);
1530 shareds_offset =
sizeof(kmp_taskdata_t) + sizeof_kmp_task_t;
1531 shareds_offset = __kmp_round_up_to_val(shareds_offset,
sizeof(
void *));
1534 KA_TRACE(30, (
"__kmp_task_alloc: T#%d First malloc size: %ld\n", gtid,
1536 KA_TRACE(30, (
"__kmp_task_alloc: T#%d Second malloc size: %ld\n", gtid,
1541 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, shareds_offset +
1544 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, shareds_offset +
1548 task = KMP_TASKDATA_TO_TASK(taskdata);
1551 #if KMP_ARCH_X86 || KMP_ARCH_PPC64 || KMP_ARCH_S390X || !KMP_HAVE_QUAD
1552 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(
double) - 1)) == 0);
1553 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(
double) - 1)) == 0);
1555 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(_Quad) - 1)) == 0);
1556 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(_Quad) - 1)) == 0);
1558 if (sizeof_shareds > 0) {
1560 task->shareds = &((
char *)taskdata)[shareds_offset];
1562 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
1565 task->shareds = NULL;
1567 task->routine = task_entry;
1570 taskdata->td_task_id = KMP_GEN_TASK_ID();
1571 taskdata->td_team = thread->th.th_team;
1572 taskdata->td_alloc_thread = thread;
1573 taskdata->td_parent = parent_task;
1574 taskdata->td_level = parent_task->td_level + 1;
1575 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
1576 taskdata->td_ident = loc_ref;
1577 taskdata->td_taskwait_ident = NULL;
1578 taskdata->td_taskwait_counter = 0;
1579 taskdata->td_taskwait_thread = 0;
1580 KMP_DEBUG_ASSERT(taskdata->td_parent != NULL);
1582 if (flags->proxy == TASK_FULL)
1583 copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs);
1585 taskdata->td_flags = *flags;
1586 taskdata->td_task_team = thread->th.th_task_team;
1587 taskdata->td_size_alloc = shareds_offset + sizeof_shareds;
1588 taskdata->td_flags.tasktype = TASK_EXPLICIT;
1591 if (flags->hidden_helper) {
1592 kmp_info_t *shadow_thread = __kmp_threads[KMP_GTID_TO_SHADOW_GTID(gtid)];
1593 taskdata->td_team = shadow_thread->th.th_team;
1594 taskdata->td_task_team = shadow_thread->th.th_task_team;
1598 taskdata->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1601 taskdata->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1607 taskdata->td_flags.task_serial =
1608 (parent_task->td_flags.final || taskdata->td_flags.team_serial ||
1609 taskdata->td_flags.tasking_ser || flags->merged_if0);
1611 taskdata->td_flags.started = 0;
1612 taskdata->td_flags.executing = 0;
1613 taskdata->td_flags.complete = 0;
1614 taskdata->td_flags.freed = 0;
1616 taskdata->td_flags.onced = 0;
1618 KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0);
1620 KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1);
1621 taskdata->td_taskgroup =
1622 parent_task->td_taskgroup;
1623 taskdata->td_dephash = NULL;
1624 taskdata->td_depnode = NULL;
1625 taskdata->td_target_data.async_handle = NULL;
1626 if (flags->tiedness == TASK_UNTIED)
1627 taskdata->td_last_tied = NULL;
1629 taskdata->td_last_tied = taskdata;
1630 taskdata->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1632 if (UNLIKELY(ompt_enabled.enabled))
1633 __ompt_task_init(taskdata, gtid);
1637 if (__kmp_track_children_task(taskdata)) {
1638 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
1639 if (parent_task->td_taskgroup)
1640 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
1643 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) {
1644 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
1646 if (flags->hidden_helper) {
1647 taskdata->td_flags.task_serial = FALSE;
1649 KMP_ATOMIC_INC(&__kmp_unexecuted_hidden_helper_tasks);
1654 kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_idx);
1655 if (tdg && __kmp_tdg_is_recording(tdg->tdg_status) &&
1656 (task_entry != (kmp_routine_entry_t)__kmp_taskloop_task)) {
1657 taskdata->is_taskgraph = 1;
1658 taskdata->tdg = __kmp_global_tdgs[__kmp_curr_tdg_idx];
1659 taskdata->td_task_id = KMP_GEN_TASK_ID();
1660 taskdata->td_tdg_task_id = KMP_ATOMIC_INC(&__kmp_tdg_task_id);
1663 KA_TRACE(20, (
"__kmp_task_alloc(exit): T#%d created task %p parent=%p\n",
1664 gtid, taskdata, taskdata->td_parent));
1669 kmp_task_t *__kmpc_omp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1670 kmp_int32 flags,
size_t sizeof_kmp_task_t,
1671 size_t sizeof_shareds,
1672 kmp_routine_entry_t task_entry) {
1674 kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
1675 __kmp_assert_valid_gtid(gtid);
1676 input_flags->native = FALSE;
1678 KA_TRACE(10, (
"__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s %s) "
1679 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1680 gtid, loc_ref, input_flags->tiedness ?
"tied " :
"untied",
1681 input_flags->proxy ?
"proxy" :
"",
1682 input_flags->detachable ?
"detachable" :
"", sizeof_kmp_task_t,
1683 sizeof_shareds, task_entry));
1685 retval = __kmp_task_alloc(loc_ref, gtid, input_flags, sizeof_kmp_task_t,
1686 sizeof_shareds, task_entry);
1688 KA_TRACE(20, (
"__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval));
1693 kmp_task_t *__kmpc_omp_target_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1695 size_t sizeof_kmp_task_t,
1696 size_t sizeof_shareds,
1697 kmp_routine_entry_t task_entry,
1698 kmp_int64 device_id) {
1699 auto &input_flags =
reinterpret_cast<kmp_tasking_flags_t &
>(flags);
1701 input_flags.tiedness = TASK_UNTIED;
1702 input_flags.target = 1;
1704 if (__kmp_enable_hidden_helper)
1705 input_flags.hidden_helper = TRUE;
1707 return __kmpc_omp_task_alloc(loc_ref, gtid, flags, sizeof_kmp_task_t,
1708 sizeof_shareds, task_entry);
1726 kmp_task_t *new_task, kmp_int32 naffins,
1727 kmp_task_affinity_info_t *affin_list) {
1737 __attribute__((target(
"backchain")))
1740 __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);
1935 else if (UNLIKELY(ompt_enabled.enabled && taskdata->td_flags.target)) {
1936 __ompt_task_finish(task, current_task, ompt_task_switch);
1942 (
"__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n",
1943 gtid, taskdata, current_task));
1957 kmp_int32 __kmpc_omp_task_parts(
ident_t *loc_ref, kmp_int32 gtid,
1958 kmp_task_t *new_task) {
1959 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1961 KA_TRACE(10, (
"__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n", gtid,
1962 loc_ref, new_taskdata));
1965 kmp_taskdata_t *parent;
1966 if (UNLIKELY(ompt_enabled.enabled)) {
1967 parent = new_taskdata->td_parent;
1968 if (ompt_enabled.ompt_callback_task_create) {
1969 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1970 &(parent->ompt_task_info.task_data), &(parent->ompt_task_info.frame),
1971 &(new_taskdata->ompt_task_info.task_data),
1972 TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1973 OMPT_GET_RETURN_ADDRESS(0));
1981 if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1983 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1984 new_taskdata->td_flags.task_serial = 1;
1985 __kmp_invoke_task(gtid, new_task, current_task);
1990 (
"__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: "
1991 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
1992 gtid, loc_ref, new_taskdata));
1995 if (UNLIKELY(ompt_enabled.enabled)) {
1996 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1997 parent->ompt_task_info.frame.enter_frame_flags = OMPT_FRAME_FLAGS_RUNTIME;
2000 return TASK_CURRENT_NOT_QUEUED;
2014 kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task,
2015 bool serialize_immediate) {
2016 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
2019 if (new_taskdata->is_taskgraph &&
2020 __kmp_tdg_is_recording(new_taskdata->tdg->tdg_status)) {
2021 kmp_tdg_info_t *tdg = new_taskdata->tdg;
2023 if (new_taskdata->td_tdg_task_id >= new_taskdata->tdg->map_size) {
2024 __kmp_acquire_bootstrap_lock(&tdg->graph_lock);
2027 if (new_taskdata->td_tdg_task_id >= tdg->map_size) {
2028 kmp_uint old_size = tdg->map_size;
2029 kmp_uint new_size = old_size * 2;
2030 kmp_node_info_t *old_record = tdg->record_map;
2031 kmp_node_info_t *new_record = (kmp_node_info_t *)__kmp_allocate(
2032 new_size *
sizeof(kmp_node_info_t));
2034 KMP_MEMCPY(new_record, old_record, old_size *
sizeof(kmp_node_info_t));
2035 tdg->record_map = new_record;
2037 __kmp_free(old_record);
2039 for (kmp_int i = old_size; i < new_size; i++) {
2040 kmp_int32 *successorsList = (kmp_int32 *)__kmp_allocate(
2041 __kmp_successors_size *
sizeof(kmp_int32));
2042 new_record[i].task =
nullptr;
2043 new_record[i].successors = successorsList;
2044 new_record[i].nsuccessors = 0;
2045 new_record[i].npredecessors = 0;
2046 new_record[i].successors_size = __kmp_successors_size;
2047 KMP_ATOMIC_ST_REL(&new_record[i].npredecessors_counter, 0);
2051 tdg->map_size = new_size;
2053 __kmp_release_bootstrap_lock(&tdg->graph_lock);
2056 if (tdg->record_map[new_taskdata->td_tdg_task_id].task ==
nullptr) {
2057 tdg->record_map[new_taskdata->td_tdg_task_id].task = new_task;
2058 tdg->record_map[new_taskdata->td_tdg_task_id].parent_task =
2059 new_taskdata->td_parent;
2060 KMP_ATOMIC_INC(&tdg->num_tasks);
2067 if (new_taskdata->td_flags.proxy == TASK_PROXY ||
2068 __kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
2070 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
2071 if (serialize_immediate)
2072 new_taskdata->td_flags.task_serial = 1;
2073 __kmp_invoke_task(gtid, new_task, current_task);
2074 }
else if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME &&
2075 __kmp_wpolicy_passive) {
2076 kmp_info_t *this_thr = __kmp_threads[gtid];
2077 kmp_team_t *team = this_thr->th.th_team;
2078 kmp_int32 nthreads = this_thr->th.th_team_nproc;
2079 for (
int i = 0; i < nthreads; ++i) {
2080 kmp_info_t *thread = team->t.t_threads[i];
2081 if (thread == this_thr)
2083 if (thread->th.th_sleep_loc != NULL) {
2084 __kmp_null_resume_wrapper(thread);
2089 return TASK_CURRENT_NOT_QUEUED;
2104 kmp_int32 __kmpc_omp_task(
ident_t *loc_ref, kmp_int32 gtid,
2105 kmp_task_t *new_task) {
2107 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
2109 #if KMP_DEBUG || OMPT_SUPPORT
2110 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
2112 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
2114 __kmp_assert_valid_gtid(gtid);
2117 kmp_taskdata_t *parent = NULL;
2118 if (UNLIKELY(ompt_enabled.enabled)) {
2119 if (!new_taskdata->td_flags.started) {
2120 OMPT_STORE_RETURN_ADDRESS(gtid);
2121 parent = new_taskdata->td_parent;
2122 if (!parent->ompt_task_info.frame.enter_frame.ptr) {
2123 parent->ompt_task_info.frame.enter_frame.ptr =
2124 OMPT_GET_FRAME_ADDRESS(0);
2126 if (ompt_enabled.ompt_callback_task_create) {
2127 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
2128 &(parent->ompt_task_info.task_data),
2129 &(parent->ompt_task_info.frame),
2130 &(new_taskdata->ompt_task_info.task_data),
2131 TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
2132 OMPT_LOAD_RETURN_ADDRESS(gtid));
2137 __ompt_task_finish(new_task,
2138 new_taskdata->ompt_task_info.scheduling_parent,
2140 new_taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
2145 res = __kmp_omp_task(gtid, new_task,
true);
2147 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
2148 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
2149 gtid, loc_ref, new_taskdata));
2151 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
2152 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
2171 kmp_int32 __kmp_omp_taskloop_task(
ident_t *loc_ref, kmp_int32 gtid,
2172 kmp_task_t *new_task,
void *codeptr_ra) {
2174 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
2176 #if KMP_DEBUG || OMPT_SUPPORT
2177 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
2179 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
2183 kmp_taskdata_t *parent = NULL;
2184 if (UNLIKELY(ompt_enabled.enabled && !new_taskdata->td_flags.started)) {
2185 parent = new_taskdata->td_parent;
2186 if (!parent->ompt_task_info.frame.enter_frame.ptr)
2187 parent->ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
2188 if (ompt_enabled.ompt_callback_task_create) {
2189 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
2190 &(parent->ompt_task_info.task_data), &(parent->ompt_task_info.frame),
2191 &(new_taskdata->ompt_task_info.task_data),
2192 TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0, codeptr_ra);
2197 res = __kmp_omp_task(gtid, new_task,
true);
2199 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
2200 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
2201 gtid, loc_ref, new_taskdata));
2203 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
2204 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
2210 template <
bool ompt>
2211 static kmp_int32 __kmpc_omp_taskwait_template(
ident_t *loc_ref, kmp_int32 gtid,
2212 void *frame_address,
2213 void *return_address) {
2214 kmp_taskdata_t *taskdata =
nullptr;
2216 int thread_finished = FALSE;
2217 KMP_SET_THREAD_STATE_BLOCK(TASKWAIT);
2219 KA_TRACE(10, (
"__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref));
2220 KMP_DEBUG_ASSERT(gtid >= 0);
2222 if (__kmp_tasking_mode != tskm_immediate_exec) {
2223 thread = __kmp_threads[gtid];
2224 taskdata = thread->th.th_current_task;
2226 #if OMPT_SUPPORT && OMPT_OPTIONAL
2227 ompt_data_t *my_task_data;
2228 ompt_data_t *my_parallel_data;
2231 my_task_data = &(taskdata->ompt_task_info.task_data);
2232 my_parallel_data = OMPT_CUR_TEAM_DATA(thread);
2234 taskdata->ompt_task_info.frame.enter_frame.ptr = frame_address;
2236 if (ompt_enabled.ompt_callback_sync_region) {
2237 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2238 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
2239 my_task_data, return_address);
2242 if (ompt_enabled.ompt_callback_sync_region_wait) {
2243 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2244 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
2245 my_task_data, return_address);
2255 taskdata->td_taskwait_counter += 1;
2256 taskdata->td_taskwait_ident = loc_ref;
2257 taskdata->td_taskwait_thread = gtid + 1;
2260 void *itt_sync_obj = NULL;
2262 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2267 !taskdata->td_flags.team_serial && !taskdata->td_flags.final;
2269 must_wait = must_wait || (thread->th.th_task_team != NULL &&
2270 thread->th.th_task_team->tt.tt_found_proxy_tasks);
2274 (__kmp_enable_hidden_helper && thread->th.th_task_team != NULL &&
2275 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered);
2278 kmp_flag_32<false, false> flag(
2279 RCAST(std::atomic<kmp_uint32> *,
2280 &(taskdata->td_incomplete_child_tasks)),
2282 while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) != 0) {
2283 flag.execute_tasks(thread, gtid, FALSE,
2284 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2285 __kmp_task_stealing_constraint);
2289 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2290 KMP_FSYNC_ACQUIRED(taskdata);
2295 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2297 #if OMPT_SUPPORT && OMPT_OPTIONAL
2299 if (ompt_enabled.ompt_callback_sync_region_wait) {
2300 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2301 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
2302 my_task_data, return_address);
2304 if (ompt_enabled.ompt_callback_sync_region) {
2305 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2306 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
2307 my_task_data, return_address);
2309 taskdata->ompt_task_info.frame.enter_frame = ompt_data_none;
2314 KA_TRACE(10, (
"__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, "
2315 "returning TASK_CURRENT_NOT_QUEUED\n",
2318 return TASK_CURRENT_NOT_QUEUED;
2321 #if OMPT_SUPPORT && OMPT_OPTIONAL
2323 static kmp_int32 __kmpc_omp_taskwait_ompt(
ident_t *loc_ref, kmp_int32 gtid,
2324 void *frame_address,
2325 void *return_address) {
2326 return __kmpc_omp_taskwait_template<true>(loc_ref, gtid, frame_address,
2333 kmp_int32 __kmpc_omp_taskwait(
ident_t *loc_ref, kmp_int32 gtid) {
2334 #if OMPT_SUPPORT && OMPT_OPTIONAL
2335 if (UNLIKELY(ompt_enabled.enabled)) {
2336 OMPT_STORE_RETURN_ADDRESS(gtid);
2337 return __kmpc_omp_taskwait_ompt(loc_ref, gtid, OMPT_GET_FRAME_ADDRESS(0),
2338 OMPT_LOAD_RETURN_ADDRESS(gtid));
2341 return __kmpc_omp_taskwait_template<false>(loc_ref, gtid, NULL, NULL);
2345 kmp_int32 __kmpc_omp_taskyield(
ident_t *loc_ref, kmp_int32 gtid,
int end_part) {
2346 kmp_taskdata_t *taskdata = NULL;
2348 int thread_finished = FALSE;
2351 KMP_SET_THREAD_STATE_BLOCK(TASKYIELD);
2353 KA_TRACE(10, (
"__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n",
2354 gtid, loc_ref, end_part));
2355 __kmp_assert_valid_gtid(gtid);
2357 if (__kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel) {
2358 thread = __kmp_threads[gtid];
2359 taskdata = thread->th.th_current_task;
2366 taskdata->td_taskwait_counter += 1;
2367 taskdata->td_taskwait_ident = loc_ref;
2368 taskdata->td_taskwait_thread = gtid + 1;
2371 void *itt_sync_obj = NULL;
2373 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2376 if (!taskdata->td_flags.team_serial) {
2377 kmp_task_team_t *task_team = thread->th.th_task_team;
2378 if (task_team != NULL) {
2379 if (KMP_TASKING_ENABLED(task_team)) {
2381 if (UNLIKELY(ompt_enabled.enabled))
2382 thread->th.ompt_thread_info.ompt_task_yielded = 1;
2384 __kmp_execute_tasks_32(
2385 thread, gtid, (kmp_flag_32<> *)NULL, FALSE,
2386 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2387 __kmp_task_stealing_constraint);
2389 if (UNLIKELY(ompt_enabled.enabled))
2390 thread->th.ompt_thread_info.ompt_task_yielded = 0;
2396 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2401 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2404 KA_TRACE(10, (
"__kmpc_omp_taskyield(exit): T#%d task %p resuming, "
2405 "returning TASK_CURRENT_NOT_QUEUED\n",
2408 return TASK_CURRENT_NOT_QUEUED;
2429 unsigned reserved31 : 31;
2509 template <
typename T>
2510 void *__kmp_task_reduction_init(
int gtid,
int num, T *data) {
2511 __kmp_assert_valid_gtid(gtid);
2512 kmp_info_t *thread = __kmp_threads[gtid];
2513 kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup;
2514 kmp_uint32 nth = thread->th.th_team_nproc;
2518 KMP_ASSERT(tg != NULL);
2519 KMP_ASSERT(data != NULL);
2520 KMP_ASSERT(num > 0);
2521 if (nth == 1 && !__kmp_enable_hidden_helper) {
2522 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n",
2526 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, taskgroup %p, #items %d\n",
2530 for (
int i = 0; i < num; ++i) {
2531 size_t size = data[i].reduce_size - 1;
2533 size += CACHE_LINE - size % CACHE_LINE;
2534 KMP_ASSERT(data[i].reduce_comb != NULL);
2537 arr[i].
flags = data[i].flags;
2541 __kmp_assign_orig<T>(arr[i], data[i]);
2542 if (!arr[i].flags.lazy_priv) {
2545 arr[i].
reduce_pend = (
char *)(arr[i].reduce_priv) + nth * size;
2546 if (arr[i].reduce_init != NULL) {
2548 for (
size_t j = 0; j < nth; ++j) {
2549 __kmp_call_init<T>(arr[i], j * size);
2556 arr[i].
reduce_priv = __kmp_allocate(nth *
sizeof(
void *));
2559 tg->reduce_data = (
void *)arr;
2560 tg->reduce_num_data = num;
2580 kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_idx);
2581 if (tdg && __kmp_tdg_is_recording(tdg->tdg_status)) {
2582 kmp_tdg_info_t *this_tdg = __kmp_global_tdgs[__kmp_curr_tdg_idx];
2583 this_tdg->rec_taskred_data =
2585 this_tdg->rec_num_taskred = num;
2586 KMP_MEMCPY(this_tdg->rec_taskred_data, data,
2607 kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_idx);
2608 if (tdg && __kmp_tdg_is_recording(tdg->tdg_status)) {
2609 kmp_tdg_info_t *this_tdg = __kmp_global_tdgs[__kmp_curr_tdg_idx];
2610 this_tdg->rec_taskred_data =
2612 this_tdg->rec_num_taskred = num;
2613 KMP_MEMCPY(this_tdg->rec_taskred_data, data,
2621 template <
typename T>
2622 void __kmp_task_reduction_init_copy(kmp_info_t *thr,
int num, T *data,
2623 kmp_taskgroup_t *tg,
void *reduce_data) {
2625 KA_TRACE(20, (
"__kmp_task_reduction_init_copy: Th %p, init taskgroup %p,"
2627 thr, tg, reduce_data));
2632 for (
int i = 0; i < num; ++i) {
2635 tg->reduce_data = (
void *)arr;
2636 tg->reduce_num_data = num;
2649 __kmp_assert_valid_gtid(gtid);
2650 kmp_info_t *thread = __kmp_threads[gtid];
2651 kmp_int32 nth = thread->th.th_team_nproc;
2655 kmp_taskgroup_t *tg = (kmp_taskgroup_t *)tskgrp;
2657 tg = thread->th.th_current_task->td_taskgroup;
2658 KMP_ASSERT(tg != NULL);
2661 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
2664 if ((thread->th.th_current_task->is_taskgraph) &&
2665 (!__kmp_tdg_is_recording(
2666 __kmp_global_tdgs[__kmp_curr_tdg_idx]->tdg_status))) {
2667 tg = thread->th.th_current_task->td_taskgroup;
2668 KMP_ASSERT(tg != NULL);
2669 KMP_ASSERT(tg->reduce_data != NULL);
2671 num = tg->reduce_num_data;
2675 KMP_ASSERT(data != NULL);
2676 while (tg != NULL) {
2678 num = tg->reduce_num_data;
2679 for (
int i = 0; i < num; ++i) {
2680 if (!arr[i].flags.lazy_priv) {
2681 if (data == arr[i].reduce_shar ||
2682 (data >= arr[i].reduce_priv && data < arr[i].reduce_pend))
2683 return (
char *)(arr[i].
reduce_priv) + tid * arr[i].reduce_size;
2686 void **p_priv = (
void **)(arr[i].reduce_priv);
2687 if (data == arr[i].reduce_shar)
2690 for (
int j = 0; j < nth; ++j)
2691 if (data == p_priv[j])
2695 if (p_priv[tid] == NULL) {
2697 p_priv[tid] = __kmp_allocate(arr[i].reduce_size);
2698 if (arr[i].reduce_init != NULL) {
2699 if (arr[i].reduce_orig != NULL) {
2701 p_priv[tid], arr[i].reduce_orig);
2703 ((void (*)(
void *))arr[i].
reduce_init)(p_priv[tid]);
2710 KMP_ASSERT(tg->parent);
2713 KMP_ASSERT2(0,
"Unknown task reduction item");
2719 static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg) {
2720 kmp_int32 nth = th->th.th_team_nproc;
2723 __kmp_enable_hidden_helper);
2726 kmp_int32 num = tg->reduce_num_data;
2727 for (
int i = 0; i < num; ++i) {
2729 void (*f_fini)(
void *) = (
void (*)(
void *))(arr[i].
reduce_fini);
2730 void (*f_comb)(
void *,
void *) =
2732 if (!arr[i].flags.lazy_priv) {
2735 for (
int j = 0; j < nth; ++j) {
2736 void *priv_data = (
char *)pr_data + j * size;
2737 f_comb(sh_data, priv_data);
2742 void **pr_data = (
void **)(arr[i].reduce_priv);
2743 for (
int j = 0; j < nth; ++j) {
2744 if (pr_data[j] != NULL) {
2745 f_comb(sh_data, pr_data[j]);
2748 __kmp_free(pr_data[j]);
2752 __kmp_free(arr[i].reduce_priv);
2754 __kmp_thread_free(th, arr);
2755 tg->reduce_data = NULL;
2756 tg->reduce_num_data = 0;
2762 static void __kmp_task_reduction_clean(kmp_info_t *th, kmp_taskgroup_t *tg) {
2763 __kmp_thread_free(th, tg->reduce_data);
2764 tg->reduce_data = NULL;
2765 tg->reduce_num_data = 0;
2768 template <
typename T>
2769 void *__kmp_task_reduction_modifier_init(
ident_t *loc,
int gtid,
int is_ws,
2771 __kmp_assert_valid_gtid(gtid);
2772 kmp_info_t *thr = __kmp_threads[gtid];
2773 kmp_int32 nth = thr->th.th_team_nproc;
2774 __kmpc_taskgroup(loc, gtid);
2777 (
"__kmpc_reduction_modifier_init: T#%d, tg %p, exiting nth=1\n",
2778 gtid, thr->th.th_current_task->td_taskgroup));
2779 return (
void *)thr->th.th_current_task->td_taskgroup;
2781 kmp_team_t *team = thr->th.th_team;
2783 kmp_taskgroup_t *tg;
2784 reduce_data = KMP_ATOMIC_LD_RLX(&team->t.t_tg_reduce_data[is_ws]);
2785 if (reduce_data == NULL &&
2786 __kmp_atomic_compare_store(&team->t.t_tg_reduce_data[is_ws], reduce_data,
2789 KMP_DEBUG_ASSERT(reduce_data == NULL);
2791 tg = (kmp_taskgroup_t *)__kmp_task_reduction_init<T>(gtid, num, data);
2795 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[0]) == 0);
2796 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[1]) == 0);
2797 KMP_ATOMIC_ST_REL(&team->t.t_tg_reduce_data[is_ws], reduce_data);
2800 (reduce_data = KMP_ATOMIC_LD_ACQ(&team->t.t_tg_reduce_data[is_ws])) ==
2804 KMP_DEBUG_ASSERT(reduce_data > (
void *)1);
2805 tg = thr->th.th_current_task->td_taskgroup;
2806 __kmp_task_reduction_init_copy<T>(thr, num, data, tg, reduce_data);
2828 int num,
void *data) {
2829 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2849 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2862 __kmpc_end_taskgroup(loc, gtid);
2866 void __kmpc_taskgroup(
ident_t *loc,
int gtid) {
2867 __kmp_assert_valid_gtid(gtid);
2868 kmp_info_t *thread = __kmp_threads[gtid];
2869 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2870 kmp_taskgroup_t *tg_new =
2871 (kmp_taskgroup_t *)__kmp_thread_malloc(thread,
sizeof(kmp_taskgroup_t));
2872 KA_TRACE(10, (
"__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new));
2873 KMP_ATOMIC_ST_RLX(&tg_new->count, 0);
2874 KMP_ATOMIC_ST_RLX(&tg_new->cancel_request, cancel_noreq);
2875 tg_new->parent = taskdata->td_taskgroup;
2876 tg_new->reduce_data = NULL;
2877 tg_new->reduce_num_data = 0;
2878 tg_new->gomp_data = NULL;
2879 taskdata->td_taskgroup = tg_new;
2881 #if OMPT_SUPPORT && OMPT_OPTIONAL
2882 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2883 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2885 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2886 kmp_team_t *team = thread->th.th_team;
2887 ompt_data_t my_task_data = taskdata->ompt_task_info.task_data;
2889 ompt_data_t my_parallel_data = team->t.ompt_team_info.parallel_data;
2891 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2892 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2893 &(my_task_data), codeptr);
2900 void __kmpc_end_taskgroup(
ident_t *loc,
int gtid) {
2901 __kmp_assert_valid_gtid(gtid);
2902 kmp_info_t *thread = __kmp_threads[gtid];
2903 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2904 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
2905 int thread_finished = FALSE;
2907 #if OMPT_SUPPORT && OMPT_OPTIONAL
2909 ompt_data_t my_task_data;
2910 ompt_data_t my_parallel_data;
2911 void *codeptr =
nullptr;
2912 if (UNLIKELY(ompt_enabled.enabled)) {
2913 team = thread->th.th_team;
2914 my_task_data = taskdata->ompt_task_info.task_data;
2916 my_parallel_data = team->t.ompt_team_info.parallel_data;
2917 codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2919 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2923 KA_TRACE(10, (
"__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc));
2924 KMP_DEBUG_ASSERT(taskgroup != NULL);
2925 KMP_SET_THREAD_STATE_BLOCK(TASKGROUP);
2927 if (__kmp_tasking_mode != tskm_immediate_exec) {
2929 taskdata->td_taskwait_counter += 1;
2930 taskdata->td_taskwait_ident = loc;
2931 taskdata->td_taskwait_thread = gtid + 1;
2935 void *itt_sync_obj = NULL;
2937 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2941 #if OMPT_SUPPORT && OMPT_OPTIONAL
2942 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2943 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2944 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2945 &(my_task_data), codeptr);
2949 if (!taskdata->td_flags.team_serial ||
2950 (thread->th.th_task_team != NULL &&
2951 (thread->th.th_task_team->tt.tt_found_proxy_tasks ||
2952 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered))) {
2953 kmp_flag_32<false, false> flag(
2954 RCAST(std::atomic<kmp_uint32> *, &(taskgroup->count)), 0U);
2955 while (KMP_ATOMIC_LD_ACQ(&taskgroup->count) != 0) {
2956 flag.execute_tasks(thread, gtid, FALSE,
2957 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2958 __kmp_task_stealing_constraint);
2961 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2963 #if OMPT_SUPPORT && OMPT_OPTIONAL
2964 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2965 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2966 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2967 &(my_task_data), codeptr);
2972 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2973 KMP_FSYNC_ACQUIRED(taskdata);
2976 KMP_DEBUG_ASSERT(taskgroup->count == 0);
2978 if (taskgroup->reduce_data != NULL &&
2979 !taskgroup->gomp_data) {
2982 kmp_team_t *t = thread->th.th_team;
2986 if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[0])) != NULL &&
2989 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[0]);
2990 if (cnt == thread->th.th_team_nproc - 1) {
2993 __kmp_task_reduction_fini(thread, taskgroup);
2996 __kmp_thread_free(thread, reduce_data);
2997 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[0], NULL);
2998 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[0], 0);
3002 __kmp_task_reduction_clean(thread, taskgroup);
3004 }
else if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[1])) !=
3008 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[1]);
3009 if (cnt == thread->th.th_team_nproc - 1) {
3011 __kmp_task_reduction_fini(thread, taskgroup);
3014 __kmp_thread_free(thread, reduce_data);
3015 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[1], NULL);
3016 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[1], 0);
3020 __kmp_task_reduction_clean(thread, taskgroup);
3024 __kmp_task_reduction_fini(thread, taskgroup);
3028 taskdata->td_taskgroup = taskgroup->parent;
3029 __kmp_thread_free(thread, taskgroup);
3031 KA_TRACE(10, (
"__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n",
3034 #if OMPT_SUPPORT && OMPT_OPTIONAL
3035 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
3036 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
3037 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
3038 &(my_task_data), codeptr);
3043 static kmp_task_t *__kmp_get_priority_task(kmp_int32 gtid,
3044 kmp_task_team_t *task_team,
3045 kmp_int32 is_constrained) {
3046 kmp_task_t *task = NULL;
3047 kmp_taskdata_t *taskdata;
3048 kmp_taskdata_t *current;
3049 kmp_thread_data_t *thread_data;
3050 int ntasks = task_team->tt.tt_num_task_pri;
3053 20, (
"__kmp_get_priority_task(exit #1): T#%d No tasks to get\n", gtid));
3058 if (__kmp_atomic_compare_store(&task_team->tt.tt_num_task_pri, ntasks,
3061 ntasks = task_team->tt.tt_num_task_pri;
3062 }
while (ntasks > 0);
3064 KA_TRACE(20, (
"__kmp_get_priority_task(exit #2): T#%d No tasks to get\n",
3070 kmp_task_pri_t *list = task_team->tt.tt_task_pri_list;
3072 KMP_ASSERT(list != NULL);
3073 thread_data = &list->td;
3074 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3075 deque_ntasks = thread_data->td.td_deque_ntasks;
3076 if (deque_ntasks == 0) {
3077 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3078 KA_TRACE(20, (
"__kmp_get_priority_task: T#%d No tasks to get from %p\n",
3079 __kmp_get_gtid(), thread_data));
3082 }
while (deque_ntasks == 0);
3083 KMP_DEBUG_ASSERT(deque_ntasks);
3084 int target = thread_data->td.td_deque_head;
3085 current = __kmp_threads[gtid]->th.th_current_task;
3086 taskdata = thread_data->td.td_deque[target];
3087 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3089 thread_data->td.td_deque_head =
3090 (target + 1) & TASK_DEQUE_MASK(thread_data->td);
3092 if (!task_team->tt.tt_untied_task_encountered) {
3094 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3095 KA_TRACE(20, (
"__kmp_get_priority_task(exit #3): T#%d could not get task "
3096 "from %p: task_team=%p ntasks=%d head=%u tail=%u\n",
3097 gtid, thread_data, task_team, deque_ntasks, target,
3098 thread_data->td.td_deque_tail));
3099 task_team->tt.tt_num_task_pri++;
3105 for (i = 1; i < deque_ntasks; ++i) {
3106 target = (target + 1) & TASK_DEQUE_MASK(thread_data->td);
3107 taskdata = thread_data->td.td_deque[target];
3108 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3114 if (taskdata == NULL) {
3116 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3118 10, (
"__kmp_get_priority_task(exit #4): T#%d could not get task from "
3119 "%p: task_team=%p ntasks=%d head=%u tail=%u\n",
3120 gtid, thread_data, task_team, deque_ntasks,
3121 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3122 task_team->tt.tt_num_task_pri++;
3126 for (i = i + 1; i < deque_ntasks; ++i) {
3128 target = (target + 1) & TASK_DEQUE_MASK(thread_data->td);
3129 thread_data->td.td_deque[prev] = thread_data->td.td_deque[target];
3133 thread_data->td.td_deque_tail ==
3134 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(thread_data->td)));
3135 thread_data->td.td_deque_tail = target;
3137 thread_data->td.td_deque_ntasks = deque_ntasks - 1;
3138 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3139 task = KMP_TASKDATA_TO_TASK(taskdata);
3144 static kmp_task_t *__kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid,
3145 kmp_task_team_t *task_team,
3146 kmp_int32 is_constrained) {
3148 kmp_taskdata_t *taskdata;
3149 kmp_thread_data_t *thread_data;
3152 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3153 KMP_DEBUG_ASSERT(task_team->tt.tt_threads_data !=
3156 thread_data = &task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
3158 KA_TRACE(10, (
"__kmp_remove_my_task(enter): T#%d ntasks=%d head=%u tail=%u\n",
3159 gtid, thread_data->td.td_deque_ntasks,
3160 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3162 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
3164 (
"__kmp_remove_my_task(exit #1): T#%d No tasks to remove: "
3165 "ntasks=%d head=%u tail=%u\n",
3166 gtid, thread_data->td.td_deque_ntasks,
3167 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3171 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3173 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
3174 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3176 (
"__kmp_remove_my_task(exit #2): T#%d No tasks to remove: "
3177 "ntasks=%d head=%u tail=%u\n",
3178 gtid, thread_data->td.td_deque_ntasks,
3179 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3183 tail = (thread_data->td.td_deque_tail - 1) &
3184 TASK_DEQUE_MASK(thread_data->td);
3185 taskdata = thread_data->td.td_deque[tail];
3187 if (!__kmp_task_is_allowed(gtid, is_constrained, taskdata,
3188 thread->th.th_current_task)) {
3190 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3192 (
"__kmp_remove_my_task(exit #3): T#%d TSC blocks tail task: "
3193 "ntasks=%d head=%u tail=%u\n",
3194 gtid, thread_data->td.td_deque_ntasks,
3195 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3199 thread_data->td.td_deque_tail = tail;
3200 TCW_4(thread_data->td.td_deque_ntasks, thread_data->td.td_deque_ntasks - 1);
3202 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3204 KA_TRACE(10, (
"__kmp_remove_my_task(exit #4): T#%d task %p removed: "
3205 "ntasks=%d head=%u tail=%u\n",
3206 gtid, taskdata, thread_data->td.td_deque_ntasks,
3207 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3209 task = KMP_TASKDATA_TO_TASK(taskdata);
3216 static kmp_task_t *__kmp_steal_task(kmp_int32 victim_tid, kmp_int32 gtid,
3217 kmp_task_team_t *task_team,
3218 std::atomic<kmp_int32> *unfinished_threads,
3219 int *thread_finished,
3220 kmp_int32 is_constrained) {
3222 kmp_taskdata_t *taskdata;
3223 kmp_taskdata_t *current;
3224 kmp_thread_data_t *victim_td, *threads_data;
3226 kmp_info_t *victim_thr;
3228 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3230 threads_data = task_team->tt.tt_threads_data;
3231 KMP_DEBUG_ASSERT(threads_data != NULL);
3232 KMP_DEBUG_ASSERT(victim_tid >= 0);
3233 KMP_DEBUG_ASSERT(victim_tid < task_team->tt.tt_max_threads);
3235 victim_td = &threads_data[victim_tid];
3236 victim_thr = victim_td->td.td_thr;
3239 KA_TRACE(10, (
"__kmp_steal_task(enter): T#%d try to steal from T#%d: "
3240 "task_team=%p ntasks=%d head=%u tail=%u\n",
3241 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
3242 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
3243 victim_td->td.td_deque_tail));
3245 if (TCR_4(victim_td->td.td_deque_ntasks) == 0) {
3246 KA_TRACE(10, (
"__kmp_steal_task(exit #1): T#%d could not steal from T#%d: "
3247 "task_team=%p ntasks=%d head=%u tail=%u\n",
3248 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
3249 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
3250 victim_td->td.td_deque_tail));
3254 __kmp_acquire_bootstrap_lock(&victim_td->td.td_deque_lock);
3256 int ntasks = TCR_4(victim_td->td.td_deque_ntasks);
3259 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3260 KA_TRACE(10, (
"__kmp_steal_task(exit #2): T#%d could not steal from T#%d: "
3261 "task_team=%p ntasks=%d head=%u tail=%u\n",
3262 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3263 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3267 KMP_DEBUG_ASSERT(victim_td->td.td_deque != NULL);
3268 current = __kmp_threads[gtid]->th.th_current_task;
3269 taskdata = victim_td->td.td_deque[victim_td->td.td_deque_head];
3270 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3272 victim_td->td.td_deque_head =
3273 (victim_td->td.td_deque_head + 1) & TASK_DEQUE_MASK(victim_td->td);
3275 if (!task_team->tt.tt_untied_task_encountered) {
3277 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3278 KA_TRACE(10, (
"__kmp_steal_task(exit #3): T#%d could not steal from "
3279 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
3280 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3281 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3286 target = victim_td->td.td_deque_head;
3288 for (i = 1; i < ntasks; ++i) {
3289 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
3290 taskdata = victim_td->td.td_deque[target];
3291 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3297 if (taskdata == NULL) {
3299 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3300 KA_TRACE(10, (
"__kmp_steal_task(exit #4): T#%d could not steal from "
3301 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
3302 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3303 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3307 for (i = i + 1; i < ntasks; ++i) {
3309 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
3310 victim_td->td.td_deque[prev] = victim_td->td.td_deque[target];
3314 victim_td->td.td_deque_tail ==
3315 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(victim_td->td)));
3316 victim_td->td.td_deque_tail = target;
3318 if (*thread_finished) {
3325 KMP_ATOMIC_INC(unfinished_threads);
3328 (
"__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n",
3329 gtid, count + 1, task_team));
3330 *thread_finished = FALSE;
3332 TCW_4(victim_td->td.td_deque_ntasks, ntasks - 1);
3334 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3338 (
"__kmp_steal_task(exit #5): T#%d stole task %p from T#%d: "
3339 "task_team=%p ntasks=%d head=%u tail=%u\n",
3340 gtid, taskdata, __kmp_gtid_from_thread(victim_thr), task_team,
3341 ntasks, victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3343 task = KMP_TASKDATA_TO_TASK(taskdata);
3357 static inline int __kmp_execute_tasks_template(
3358 kmp_info_t *thread, kmp_int32 gtid, C *flag,
int final_spin,
3359 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3360 kmp_int32 is_constrained) {
3361 kmp_task_team_t *task_team = thread->th.th_task_team;
3362 kmp_thread_data_t *threads_data;
3364 kmp_info_t *other_thread;
3365 kmp_taskdata_t *current_task = thread->th.th_current_task;
3366 std::atomic<kmp_int32> *unfinished_threads;
3367 kmp_int32 nthreads, victim_tid = -2, use_own_tasks = 1, new_victim = 0,
3368 tid = thread->th.th_info.ds.ds_tid;
3370 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3371 KMP_DEBUG_ASSERT(thread == __kmp_threads[gtid]);
3373 if (task_team == NULL || current_task == NULL)
3376 KA_TRACE(15, (
"__kmp_execute_tasks_template(enter): T#%d final_spin=%d "
3377 "*thread_finished=%d\n",
3378 gtid, final_spin, *thread_finished));
3380 thread->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
3381 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
3383 KMP_DEBUG_ASSERT(threads_data != NULL);
3385 nthreads = task_team->tt.tt_nproc;
3386 unfinished_threads = &(task_team->tt.tt_unfinished_threads);
3387 KMP_DEBUG_ASSERT(*unfinished_threads >= 0);
3393 if (task_team->tt.tt_num_task_pri) {
3394 task = __kmp_get_priority_task(gtid, task_team, is_constrained);
3396 if (task == NULL && use_own_tasks) {
3397 task = __kmp_remove_my_task(thread, gtid, task_team, is_constrained);
3399 if ((task == NULL) && (nthreads > 1)) {
3403 if (victim_tid == -2) {
3404 victim_tid = threads_data[tid].td.td_deque_last_stolen;
3407 other_thread = threads_data[victim_tid].td.td_thr;
3409 if (victim_tid != -1) {
3411 }
else if (!new_victim) {
3417 victim_tid = __kmp_get_random(thread) % (nthreads - 1);
3418 if (victim_tid >= tid) {
3422 other_thread = threads_data[victim_tid].td.td_thr;
3432 if ((__kmp_tasking_mode == tskm_task_teams) &&
3433 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) &&
3434 (TCR_PTR(CCAST(
void *, other_thread->th.th_sleep_loc)) !=
3437 __kmp_null_resume_wrapper(other_thread);
3451 __kmp_steal_task(victim_tid, gtid, task_team, unfinished_threads,
3452 thread_finished, is_constrained);
3455 if (threads_data[tid].td.td_deque_last_stolen != victim_tid) {
3456 threads_data[tid].td.td_deque_last_stolen = victim_tid;
3463 KMP_CHECK_UPDATE(threads_data[tid].td.td_deque_last_stolen, -1);
3472 #if USE_ITT_BUILD && USE_ITT_NOTIFY
3473 if (__itt_sync_create_ptr || KMP_ITT_DEBUG) {
3474 if (itt_sync_obj == NULL) {
3476 itt_sync_obj = __kmp_itt_barrier_object(gtid, bs_forkjoin_barrier);
3478 __kmp_itt_task_starting(itt_sync_obj);
3481 __kmp_invoke_task(gtid, task, current_task);
3483 if (itt_sync_obj != NULL)
3484 __kmp_itt_task_finished(itt_sync_obj);
3491 if (flag == NULL || (!final_spin && flag->done_check())) {
3494 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3498 if (thread->th.th_task_team == NULL) {
3501 KMP_YIELD(__kmp_library == library_throughput);
3504 if (!use_own_tasks && TCR_4(threads_data[tid].td.td_deque_ntasks) != 0) {
3505 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d stolen task spawned "
3506 "other tasks, restart\n",
3517 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks) == 0) {
3521 if (!*thread_finished) {
3523 kmp_int32 count = -1 +
3525 KMP_ATOMIC_DEC(unfinished_threads);
3526 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d dec "
3527 "unfinished_threads to %d task_team=%p\n",
3528 gtid, count, task_team));
3529 *thread_finished = TRUE;
3537 if (flag != NULL && flag->done_check()) {
3540 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3548 if (thread->th.th_task_team == NULL) {
3550 (
"__kmp_execute_tasks_template: T#%d no more tasks\n", gtid));
3559 if (flag == NULL || (!final_spin && flag->done_check())) {
3561 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3568 if (nthreads == 1 &&
3569 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks))
3573 (
"__kmp_execute_tasks_template: T#%d can't find work\n", gtid));
3579 template <
bool C,
bool S>
3580 int __kmp_execute_tasks_32(
3581 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32<C, S> *flag,
int final_spin,
3582 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3583 kmp_int32 is_constrained) {
3584 return __kmp_execute_tasks_template(
3585 thread, gtid, flag, final_spin,
3586 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3589 template <
bool C,
bool S>
3590 int __kmp_execute_tasks_64(
3591 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64<C, S> *flag,
int final_spin,
3592 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3593 kmp_int32 is_constrained) {
3594 return __kmp_execute_tasks_template(
3595 thread, gtid, flag, final_spin,
3596 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3599 template <
bool C,
bool S>
3600 int __kmp_atomic_execute_tasks_64(
3601 kmp_info_t *thread, kmp_int32 gtid, kmp_atomic_flag_64<C, S> *flag,
3602 int final_spin,
int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3603 kmp_int32 is_constrained) {
3604 return __kmp_execute_tasks_template(
3605 thread, gtid, flag, final_spin,
3606 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3609 int __kmp_execute_tasks_oncore(
3610 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag,
int final_spin,
3611 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3612 kmp_int32 is_constrained) {
3613 return __kmp_execute_tasks_template(
3614 thread, gtid, flag, final_spin,
3615 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3619 __kmp_execute_tasks_32<false, false>(kmp_info_t *, kmp_int32,
3620 kmp_flag_32<false, false> *,
int,
3621 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3623 template int __kmp_execute_tasks_64<false, true>(kmp_info_t *, kmp_int32,
3624 kmp_flag_64<false, true> *,
3626 int *USE_ITT_BUILD_ARG(
void *),
3629 template int __kmp_execute_tasks_64<true, false>(kmp_info_t *, kmp_int32,
3630 kmp_flag_64<true, false> *,
3632 int *USE_ITT_BUILD_ARG(
void *),
3635 template int __kmp_atomic_execute_tasks_64<false, true>(
3636 kmp_info_t *, kmp_int32, kmp_atomic_flag_64<false, true> *,
int,
3637 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3639 template int __kmp_atomic_execute_tasks_64<true, false>(
3640 kmp_info_t *, kmp_int32, kmp_atomic_flag_64<true, false> *,
int,
3641 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3646 static void __kmp_enable_tasking(kmp_task_team_t *task_team,
3647 kmp_info_t *this_thr) {
3648 kmp_thread_data_t *threads_data;
3649 int nthreads, i, is_init_thread;
3651 KA_TRACE(10, (
"__kmp_enable_tasking(enter): T#%d\n",
3652 __kmp_gtid_from_thread(this_thr)));
3654 KMP_DEBUG_ASSERT(task_team != NULL);
3655 KMP_DEBUG_ASSERT(this_thr->th.th_team != NULL);
3657 nthreads = task_team->tt.tt_nproc;
3658 KMP_DEBUG_ASSERT(nthreads > 0);
3659 KMP_DEBUG_ASSERT(nthreads == this_thr->th.th_team->t.t_nproc);
3662 is_init_thread = __kmp_realloc_task_threads_data(this_thr, task_team);
3664 if (!is_init_thread) {
3668 (
"__kmp_enable_tasking(exit): T#%d: threads array already set up.\n",
3669 __kmp_gtid_from_thread(this_thr)));
3672 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
3673 KMP_DEBUG_ASSERT(threads_data != NULL);
3675 if (__kmp_tasking_mode == tskm_task_teams &&
3676 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME)) {
3680 for (i = 0; i < nthreads; i++) {
3682 kmp_info_t *thread = threads_data[i].td.td_thr;
3684 if (i == this_thr->th.th_info.ds.ds_tid) {
3693 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3695 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d waking up thread T#%d\n",
3696 __kmp_gtid_from_thread(this_thr),
3697 __kmp_gtid_from_thread(thread)));
3698 __kmp_null_resume_wrapper(thread);
3700 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d don't wake up thread T#%d\n",
3701 __kmp_gtid_from_thread(this_thr),
3702 __kmp_gtid_from_thread(thread)));
3707 KA_TRACE(10, (
"__kmp_enable_tasking(exit): T#%d\n",
3708 __kmp_gtid_from_thread(this_thr)));
3741 static kmp_task_team_t *__kmp_free_task_teams =
3744 kmp_bootstrap_lock_t __kmp_task_team_lock =
3745 KMP_BOOTSTRAP_LOCK_INITIALIZER(__kmp_task_team_lock);
3752 static void __kmp_alloc_task_deque(kmp_info_t *thread,
3753 kmp_thread_data_t *thread_data) {
3754 __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
3755 KMP_DEBUG_ASSERT(thread_data->td.td_deque == NULL);
3758 thread_data->td.td_deque_last_stolen = -1;
3760 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == 0);
3761 KMP_DEBUG_ASSERT(thread_data->td.td_deque_head == 0);
3762 KMP_DEBUG_ASSERT(thread_data->td.td_deque_tail == 0);
3766 (
"__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n",
3767 __kmp_gtid_from_thread(thread), INITIAL_TASK_DEQUE_SIZE, thread_data));
3771 thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
3772 INITIAL_TASK_DEQUE_SIZE *
sizeof(kmp_taskdata_t *));
3773 thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
3779 static void __kmp_free_task_deque(kmp_thread_data_t *thread_data) {
3780 if (thread_data->td.td_deque != NULL) {
3781 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3782 TCW_4(thread_data->td.td_deque_ntasks, 0);
3783 __kmp_free(thread_data->td.td_deque);
3784 thread_data->td.td_deque = NULL;
3785 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3788 #ifdef BUILD_TIED_TASK_STACK
3790 if (thread_data->td.td_susp_tied_tasks.ts_entries != TASK_STACK_EMPTY) {
3791 __kmp_free_task_stack(__kmp_thread_from_gtid(gtid), thread_data);
3803 static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
3804 kmp_task_team_t *task_team) {
3805 kmp_thread_data_t **threads_data_p;
3806 kmp_int32 nthreads, maxthreads;
3807 int is_init_thread = FALSE;
3809 if (TCR_4(task_team->tt.tt_found_tasks)) {
3814 threads_data_p = &task_team->tt.tt_threads_data;
3815 nthreads = task_team->tt.tt_nproc;
3816 maxthreads = task_team->tt.tt_max_threads;
3821 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3823 if (!TCR_4(task_team->tt.tt_found_tasks)) {
3825 kmp_team_t *team = thread->th.th_team;
3828 is_init_thread = TRUE;
3829 if (maxthreads < nthreads) {
3831 if (*threads_data_p != NULL) {
3832 kmp_thread_data_t *old_data = *threads_data_p;
3833 kmp_thread_data_t *new_data = NULL;
3837 (
"__kmp_realloc_task_threads_data: T#%d reallocating "
3838 "threads data for task_team %p, new_size = %d, old_size = %d\n",
3839 __kmp_gtid_from_thread(thread), task_team, nthreads, maxthreads));
3844 new_data = (kmp_thread_data_t *)__kmp_allocate(
3845 nthreads *
sizeof(kmp_thread_data_t));
3847 KMP_MEMCPY_S((
void *)new_data, nthreads *
sizeof(kmp_thread_data_t),
3848 (
void *)old_data, maxthreads *
sizeof(kmp_thread_data_t));
3850 #ifdef BUILD_TIED_TASK_STACK
3852 for (i = maxthreads; i < nthreads; i++) {
3853 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3854 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3858 (*threads_data_p) = new_data;
3859 __kmp_free(old_data);
3861 KE_TRACE(10, (
"__kmp_realloc_task_threads_data: T#%d allocating "
3862 "threads data for task_team %p, size = %d\n",
3863 __kmp_gtid_from_thread(thread), task_team, nthreads));
3867 *threads_data_p = (kmp_thread_data_t *)__kmp_allocate(
3868 nthreads *
sizeof(kmp_thread_data_t));
3869 #ifdef BUILD_TIED_TASK_STACK
3871 for (i = 0; i < nthreads; i++) {
3872 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3873 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3877 task_team->tt.tt_max_threads = nthreads;
3880 KMP_DEBUG_ASSERT(*threads_data_p != NULL);
3884 for (i = 0; i < nthreads; i++) {
3885 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3886 thread_data->td.td_thr = team->t.t_threads[i];
3888 if (thread_data->td.td_deque_last_stolen >= nthreads) {
3892 thread_data->td.td_deque_last_stolen = -1;
3897 TCW_SYNC_4(task_team->tt.tt_found_tasks, TRUE);
3900 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3901 return is_init_thread;
3907 static void __kmp_free_task_threads_data(kmp_task_team_t *task_team) {
3908 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3909 if (task_team->tt.tt_threads_data != NULL) {
3911 for (i = 0; i < task_team->tt.tt_max_threads; i++) {
3912 __kmp_free_task_deque(&task_team->tt.tt_threads_data[i]);
3914 __kmp_free(task_team->tt.tt_threads_data);
3915 task_team->tt.tt_threads_data = NULL;
3917 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3923 static void __kmp_free_task_pri_list(kmp_task_team_t *task_team) {
3924 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
3925 if (task_team->tt.tt_task_pri_list != NULL) {
3926 kmp_task_pri_t *list = task_team->tt.tt_task_pri_list;
3927 while (list != NULL) {
3928 kmp_task_pri_t *next = list->next;
3929 __kmp_free_task_deque(&list->td);
3933 task_team->tt.tt_task_pri_list = NULL;
3935 __kmp_release_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
3938 static inline void __kmp_task_team_init(kmp_task_team_t *task_team,
3940 int team_nth = team->t.t_nproc;
3942 if (!task_team->tt.tt_active || team_nth != task_team->tt.tt_nproc) {
3943 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3944 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3945 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
3946 TCW_4(task_team->tt.tt_nproc, team_nth);
3947 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads, team_nth);
3948 TCW_4(task_team->tt.tt_active, TRUE);
3956 static kmp_task_team_t *__kmp_allocate_task_team(kmp_info_t *thread,
3958 kmp_task_team_t *task_team = NULL;
3960 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d entering; team = %p\n",
3961 (thread ? __kmp_gtid_from_thread(thread) : -1), team));
3963 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3965 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3966 if (__kmp_free_task_teams != NULL) {
3967 task_team = __kmp_free_task_teams;
3968 TCW_PTR(__kmp_free_task_teams, task_team->tt.tt_next);
3969 task_team->tt.tt_next = NULL;
3971 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3974 if (task_team == NULL) {
3975 KE_TRACE(10, (
"__kmp_allocate_task_team: T#%d allocating "
3976 "task team for team %p\n",
3977 __kmp_gtid_from_thread(thread), team));
3980 task_team = (kmp_task_team_t *)__kmp_allocate(
sizeof(kmp_task_team_t));
3981 __kmp_init_bootstrap_lock(&task_team->tt.tt_threads_lock);
3982 __kmp_init_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
3983 #if USE_ITT_BUILD && USE_ITT_NOTIFY && KMP_DEBUG
3986 __itt_suppress_mark_range(
3987 __itt_suppress_range, __itt_suppress_threading_errors,
3988 &task_team->tt.tt_found_tasks,
sizeof(task_team->tt.tt_found_tasks));
3989 __itt_suppress_mark_range(__itt_suppress_range,
3990 __itt_suppress_threading_errors,
3991 CCAST(kmp_uint32 *, &task_team->tt.tt_active),
3992 sizeof(task_team->tt.tt_active));
4000 __kmp_task_team_init(task_team, team);
4002 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d exiting; task_team = %p "
4003 "unfinished_threads init'd to %d\n",
4004 (thread ? __kmp_gtid_from_thread(thread) : -1), task_team,
4005 KMP_ATOMIC_LD_RLX(&task_team->tt.tt_unfinished_threads)));
4012 void __kmp_free_task_team(kmp_info_t *thread, kmp_task_team_t *task_team) {
4013 KA_TRACE(20, (
"__kmp_free_task_team: T#%d task_team = %p\n",
4014 thread ? __kmp_gtid_from_thread(thread) : -1, task_team));
4017 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
4019 KMP_DEBUG_ASSERT(task_team->tt.tt_next == NULL);
4020 task_team->tt.tt_next = __kmp_free_task_teams;
4021 TCW_PTR(__kmp_free_task_teams, task_team);
4023 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
4031 void __kmp_reap_task_teams(
void) {
4032 kmp_task_team_t *task_team;
4034 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
4036 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
4037 while ((task_team = __kmp_free_task_teams) != NULL) {
4038 __kmp_free_task_teams = task_team->tt.tt_next;
4039 task_team->tt.tt_next = NULL;
4042 if (task_team->tt.tt_threads_data != NULL) {
4043 __kmp_free_task_threads_data(task_team);
4045 if (task_team->tt.tt_task_pri_list != NULL) {
4046 __kmp_free_task_pri_list(task_team);
4048 __kmp_free(task_team);
4050 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
4058 void __kmp_push_task_team_node(kmp_info_t *thread, kmp_team_t *team) {
4059 KMP_DEBUG_ASSERT(team->t.t_nproc == 1);
4060 kmp_task_team_list_t *current =
4061 (kmp_task_team_list_t *)(&team->t.t_task_team[0]);
4062 kmp_task_team_list_t *node =
4063 (kmp_task_team_list_t *)__kmp_allocate(
sizeof(kmp_task_team_list_t));
4064 node->task_team = current->task_team;
4065 node->next = current->next;
4066 thread->th.th_task_team = current->task_team = NULL;
4067 current->next = node;
4071 void __kmp_pop_task_team_node(kmp_info_t *thread, kmp_team_t *team) {
4072 KMP_DEBUG_ASSERT(team->t.t_nproc == 1);
4073 kmp_task_team_list_t *current =
4074 (kmp_task_team_list_t *)(&team->t.t_task_team[0]);
4075 if (current->task_team) {
4076 __kmp_free_task_team(thread, current->task_team);
4078 kmp_task_team_list_t *next = current->next;
4080 current->task_team = next->task_team;
4081 current->next = next->next;
4082 KMP_DEBUG_ASSERT(next != current);
4084 thread->th.th_task_team = current->task_team;
4091 void __kmp_wait_to_unref_task_teams(
void) {
4097 KMP_INIT_YIELD(spins);
4098 KMP_INIT_BACKOFF(time);
4106 for (thread = CCAST(kmp_info_t *, __kmp_thread_pool); thread != NULL;
4107 thread = thread->th.th_next_pool) {
4111 if (TCR_PTR(thread->th.th_task_team) == NULL) {
4112 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n",
4113 __kmp_gtid_from_thread(thread)));
4118 if (!__kmp_is_thread_alive(thread, &exit_val)) {
4119 thread->th.th_task_team = NULL;
4126 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: Waiting for T#%d to "
4127 "unreference task_team\n",
4128 __kmp_gtid_from_thread(thread)));
4130 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
4133 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
4137 (
"__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n",
4138 __kmp_gtid_from_thread(thread), __kmp_gtid_from_thread(thread)));
4139 __kmp_null_resume_wrapper(thread);
4148 KMP_YIELD_OVERSUB_ELSE_SPIN(spins, time);
4154 void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team) {
4155 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
4160 if (team == this_thr->th.th_serial_team ||
4161 team == this_thr->th.th_root->r.r_root_team) {
4162 KMP_DEBUG_ASSERT(team->t.t_nproc == 1);
4163 if (team->t.t_task_team[0] == NULL) {
4164 team->t.t_task_team[0] = __kmp_allocate_task_team(this_thr, team);
4166 20, (
"__kmp_task_team_setup: Primary T#%d created new task_team %p"
4167 " for serial/root team %p\n",
4168 __kmp_gtid_from_thread(this_thr), team->t.t_task_team[0], team));
4171 __kmp_task_team_init(team->t.t_task_team[0], team);
4179 if (team->t.t_task_team[this_thr->th.th_task_state] == NULL) {
4180 team->t.t_task_team[this_thr->th.th_task_state] =
4181 __kmp_allocate_task_team(this_thr, team);
4182 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d created new task_team %p"
4183 " for team %d at parity=%d\n",
4184 __kmp_gtid_from_thread(this_thr),
4185 team->t.t_task_team[this_thr->th.th_task_state], team->t.t_id,
4186 this_thr->th.th_task_state));
4195 int other_team = 1 - this_thr->th.th_task_state;
4196 KMP_DEBUG_ASSERT(other_team >= 0 && other_team < 2);
4197 if (team->t.t_task_team[other_team] == NULL) {
4198 team->t.t_task_team[other_team] = __kmp_allocate_task_team(this_thr, team);
4199 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d created second new "
4200 "task_team %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));
4205 kmp_task_team_t *task_team = team->t.t_task_team[other_team];
4206 __kmp_task_team_init(task_team, team);
4209 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d reset next task_team "
4210 "%p for team %d at parity=%d\n",
4211 __kmp_gtid_from_thread(this_thr),
4212 team->t.t_task_team[other_team], team->t.t_id, other_team));
4219 if (this_thr == __kmp_hidden_helper_main_thread) {
4220 for (
int i = 0; i < 2; ++i) {
4221 kmp_task_team_t *task_team = team->t.t_task_team[i];
4222 if (KMP_TASKING_ENABLED(task_team)) {
4225 __kmp_enable_tasking(task_team, this_thr);
4226 for (
int j = 0; j < task_team->tt.tt_nproc; ++j) {
4227 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[j];
4228 if (thread_data->td.td_deque == NULL) {
4229 __kmp_alloc_task_deque(__kmp_hidden_helper_threads[j], thread_data);
4239 void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team) {
4240 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
4241 KMP_DEBUG_ASSERT(team != this_thr->th.th_serial_team);
4242 KMP_DEBUG_ASSERT(team != this_thr->th.th_root->r.r_root_team);
4246 this_thr->th.th_task_state = (kmp_uint8)(1 - this_thr->th.th_task_state);
4250 TCW_PTR(this_thr->th.th_task_team,
4251 team->t.t_task_team[this_thr->th.th_task_state]);
4253 (
"__kmp_task_team_sync: Thread T#%d task team switched to task_team "
4254 "%p from Team #%d (parity=%d)\n",
4255 __kmp_gtid_from_thread(this_thr), this_thr->th.th_task_team,
4256 team->t.t_id, this_thr->th.th_task_state));
4265 void __kmp_task_team_wait(
4266 kmp_info_t *this_thr,
4267 kmp_team_t *team USE_ITT_BUILD_ARG(
void *itt_sync_obj),
int wait) {
4268 kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state];
4270 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
4271 KMP_DEBUG_ASSERT(task_team == this_thr->th.th_task_team);
4273 if ((task_team != NULL) && KMP_TASKING_ENABLED(task_team)) {
4275 KA_TRACE(20, (
"__kmp_task_team_wait: Primary T#%d waiting for all tasks "
4276 "(for unfinished_threads to reach 0) on task_team = %p\n",
4277 __kmp_gtid_from_thread(this_thr), task_team));
4281 kmp_flag_32<false, false> flag(
4282 RCAST(std::atomic<kmp_uint32> *,
4283 &task_team->tt.tt_unfinished_threads),
4285 flag.wait(this_thr, TRUE USE_ITT_BUILD_ARG(itt_sync_obj));
4291 (
"__kmp_task_team_wait: Primary T#%d deactivating task_team %p: "
4292 "setting active to false, setting local and team's pointer to NULL\n",
4293 __kmp_gtid_from_thread(this_thr), task_team));
4294 TCW_SYNC_4(task_team->tt.tt_found_proxy_tasks, FALSE);
4295 TCW_SYNC_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
4296 KMP_CHECK_UPDATE(task_team->tt.tt_untied_task_encountered, 0);
4297 TCW_SYNC_4(task_team->tt.tt_active, FALSE);
4300 TCW_PTR(this_thr->th.th_task_team, NULL);
4309 void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread,
int gtid) {
4310 std::atomic<kmp_uint32> *spin = RCAST(
4311 std::atomic<kmp_uint32> *,
4312 &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads);
4314 KMP_DEBUG_ASSERT(__kmp_tasking_mode == tskm_extra_barrier);
4317 KMP_FSYNC_SPIN_INIT(spin, NULL);
4319 kmp_flag_32<false, false> spin_flag(spin, 0U);
4320 while (!spin_flag.execute_tasks(thread, gtid, TRUE,
4321 &flag USE_ITT_BUILD_ARG(NULL), 0)) {
4324 KMP_FSYNC_SPIN_PREPARE(RCAST(
void *, spin));
4327 if (TCR_4(__kmp_global.g.g_done)) {
4328 if (__kmp_global.g.g_abort)
4329 __kmp_abort_thread();
4335 KMP_FSYNC_SPIN_ACQUIRED(RCAST(
void *, spin));
4344 static bool __kmp_give_task(kmp_info_t *thread, kmp_int32 tid, kmp_task_t *task,
4346 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4347 kmp_task_team_t *task_team = taskdata->td_task_team;
4349 KA_TRACE(20, (
"__kmp_give_task: trying to give task %p to thread %d.\n",
4353 KMP_DEBUG_ASSERT(task_team != NULL);
4355 bool result =
false;
4356 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
4358 if (thread_data->td.td_deque == NULL) {
4362 (
"__kmp_give_task: thread %d has no queue while giving task %p.\n",
4367 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4368 TASK_DEQUE_SIZE(thread_data->td)) {
4371 (
"__kmp_give_task: queue is full while giving task %p to thread %d.\n",
4376 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
4379 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
4380 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4381 TASK_DEQUE_SIZE(thread_data->td)) {
4383 __kmp_realloc_task_deque(thread, thread_data);
4388 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
4390 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4391 TASK_DEQUE_SIZE(thread_data->td)) {
4392 KA_TRACE(30, (
"__kmp_give_task: queue is full while giving task %p to "
4398 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
4399 goto release_and_exit;
4401 __kmp_realloc_task_deque(thread, thread_data);
4407 thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
4409 thread_data->td.td_deque_tail =
4410 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
4411 TCW_4(thread_data->td.td_deque_ntasks,
4412 TCR_4(thread_data->td.td_deque_ntasks) + 1);
4415 KA_TRACE(30, (
"__kmp_give_task: successfully gave task %p to thread %d.\n",
4419 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
4424 #define PROXY_TASK_FLAG 0x40000000
4441 static void __kmp_first_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
4442 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
4443 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4444 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
4445 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
4447 taskdata->td_flags.complete = 1;
4449 taskdata->td_flags.onced = 1;
4452 if (taskdata->td_taskgroup)
4453 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
4457 KMP_ATOMIC_OR(&taskdata->td_incomplete_child_tasks, PROXY_TASK_FLAG);
4460 static void __kmp_second_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
4462 kmp_int32 children = 0;
4466 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks);
4467 KMP_DEBUG_ASSERT(children >= 0);
4470 KMP_ATOMIC_AND(&taskdata->td_incomplete_child_tasks, ~PROXY_TASK_FLAG);
4473 static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask) {
4474 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4475 kmp_info_t *thread = __kmp_threads[gtid];
4477 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4478 KMP_DEBUG_ASSERT(taskdata->td_flags.complete ==
4483 while ((KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) &
4484 PROXY_TASK_FLAG) > 0)
4487 __kmp_release_deps(gtid, taskdata);
4488 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
4500 KMP_DEBUG_ASSERT(ptask != NULL);
4501 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4503 10, (
"__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n",
4505 __kmp_assert_valid_gtid(gtid);
4506 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4508 __kmp_first_top_half_finish_proxy(taskdata);
4509 __kmp_second_top_half_finish_proxy(taskdata);
4510 __kmp_bottom_half_finish_proxy(gtid, ptask);
4513 (
"__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n",
4517 void __kmpc_give_task(kmp_task_t *ptask, kmp_int32 start = 0) {
4518 KMP_DEBUG_ASSERT(ptask != NULL);
4519 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4523 kmp_team_t *team = taskdata->td_team;
4524 kmp_int32 nthreads = team->t.t_nproc;
4529 kmp_int32 start_k = start % nthreads;
4531 kmp_int32 k = start_k;
4535 thread = team->t.t_threads[k];
4536 k = (k + 1) % nthreads;
4542 }
while (!__kmp_give_task(thread, k, ptask, pass));
4544 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME && __kmp_wpolicy_passive) {
4546 for (
int i = 0; i < nthreads; ++i) {
4547 thread = team->t.t_threads[i];
4548 if (thread->th.th_sleep_loc != NULL) {
4549 __kmp_null_resume_wrapper(thread);
4564 KMP_DEBUG_ASSERT(ptask != NULL);
4565 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4569 (
"__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n",
4572 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4574 __kmp_first_top_half_finish_proxy(taskdata);
4576 __kmpc_give_task(ptask);
4578 __kmp_second_top_half_finish_proxy(taskdata);
4582 (
"__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n",
4586 kmp_event_t *__kmpc_task_allow_completion_event(
ident_t *loc_ref,
int gtid,
4588 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(task);
4589 if (td->td_allow_completion_event.type == KMP_EVENT_UNINITIALIZED) {
4590 td->td_allow_completion_event.type = KMP_EVENT_ALLOW_COMPLETION;
4591 td->td_allow_completion_event.ed.task = task;
4592 __kmp_init_tas_lock(&td->td_allow_completion_event.lock);
4594 return &td->td_allow_completion_event;
4597 void __kmp_fulfill_event(kmp_event_t *event) {
4598 if (event->type == KMP_EVENT_ALLOW_COMPLETION) {
4599 kmp_task_t *ptask = event->ed.task;
4600 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4601 bool detached =
false;
4602 int gtid = __kmp_get_gtid();
4607 __kmp_acquire_tas_lock(&event->lock, gtid);
4608 if (taskdata->td_flags.proxy == TASK_PROXY) {
4614 if (UNLIKELY(ompt_enabled.enabled))
4615 __ompt_task_finish(ptask, NULL, ompt_task_early_fulfill);
4618 event->type = KMP_EVENT_UNINITIALIZED;
4619 __kmp_release_tas_lock(&event->lock, gtid);
4625 if (UNLIKELY(ompt_enabled.enabled))
4626 __ompt_task_finish(ptask, NULL, ompt_task_late_fulfill);
4630 kmp_team_t *team = taskdata->td_team;
4631 kmp_info_t *thread = __kmp_get_thread();
4632 if (thread->th.th_team == team) {
4652 kmp_task_t *__kmp_task_dup_alloc(kmp_info_t *thread, kmp_task_t *task_src
4654 ,
int taskloop_recur
4658 kmp_taskdata_t *taskdata;
4659 kmp_taskdata_t *taskdata_src = KMP_TASK_TO_TASKDATA(task_src);
4660 kmp_taskdata_t *parent_task = taskdata_src->td_parent;
4661 size_t shareds_offset;
4664 KA_TRACE(10, (
"__kmp_task_dup_alloc(enter): Th %p, source task %p\n", thread,
4666 KMP_DEBUG_ASSERT(taskdata_src->td_flags.proxy ==
4668 KMP_DEBUG_ASSERT(taskdata_src->td_flags.tasktype == TASK_EXPLICIT);
4669 task_size = taskdata_src->td_size_alloc;
4672 KA_TRACE(30, (
"__kmp_task_dup_alloc: Th %p, malloc size %ld\n", thread,
4675 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, task_size);
4677 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, task_size);
4679 KMP_MEMCPY(taskdata, taskdata_src, task_size);
4681 task = KMP_TASKDATA_TO_TASK(taskdata);
4685 if (taskdata->is_taskgraph && !taskloop_recur &&
4686 __kmp_tdg_is_recording(taskdata_src->tdg->tdg_status))
4687 taskdata->td_tdg_task_id = KMP_ATOMIC_INC(&__kmp_tdg_task_id);
4689 taskdata->td_task_id = KMP_GEN_TASK_ID();
4690 if (task->shareds != NULL) {
4691 shareds_offset = (
char *)task_src->shareds - (
char *)taskdata_src;
4692 task->shareds = &((
char *)taskdata)[shareds_offset];
4693 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
4696 taskdata->td_alloc_thread = thread;
4697 taskdata->td_parent = parent_task;
4699 taskdata->td_taskgroup = parent_task->td_taskgroup;
4702 if (taskdata->td_flags.tiedness == TASK_TIED)
4703 taskdata->td_last_tied = taskdata;
4707 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
4708 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
4709 if (parent_task->td_taskgroup)
4710 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
4713 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT)
4714 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
4718 (
"__kmp_task_dup_alloc(exit): Th %p, created task %p, parent=%p\n",
4719 thread, taskdata, taskdata->td_parent));
4721 if (UNLIKELY(ompt_enabled.enabled))
4722 __ompt_task_init(taskdata, thread->th.th_info.ds.ds_gtid);
4731 typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32);
4733 KMP_BUILD_ASSERT(
sizeof(
long) == 4 ||
sizeof(
long) == 8);
4738 class kmp_taskloop_bounds_t {
4740 const kmp_taskdata_t *taskdata;
4741 size_t lower_offset;
4742 size_t upper_offset;
4745 kmp_taskloop_bounds_t(kmp_task_t *_task, kmp_uint64 *lb, kmp_uint64 *ub)
4746 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(task)),
4747 lower_offset((char *)lb - (char *)task),
4748 upper_offset((char *)ub - (char *)task) {
4749 KMP_DEBUG_ASSERT((
char *)lb > (
char *)_task);
4750 KMP_DEBUG_ASSERT((
char *)ub > (
char *)_task);
4752 kmp_taskloop_bounds_t(kmp_task_t *_task,
const kmp_taskloop_bounds_t &bounds)
4753 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(_task)),
4754 lower_offset(bounds.lower_offset), upper_offset(bounds.upper_offset) {}
4755 size_t get_lower_offset()
const {
return lower_offset; }
4756 size_t get_upper_offset()
const {
return upper_offset; }
4757 kmp_uint64 get_lb()
const {
4759 #if defined(KMP_GOMP_COMPAT)
4761 if (!taskdata->td_flags.native) {
4762 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4765 if (taskdata->td_size_loop_bounds == 4) {
4766 kmp_int32 *lb = RCAST(kmp_int32 *, task->shareds);
4767 retval = (kmp_int64)*lb;
4769 kmp_int64 *lb = RCAST(kmp_int64 *, task->shareds);
4770 retval = (kmp_int64)*lb;
4775 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4779 kmp_uint64 get_ub()
const {
4781 #if defined(KMP_GOMP_COMPAT)
4783 if (!taskdata->td_flags.native) {
4784 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4787 if (taskdata->td_size_loop_bounds == 4) {
4788 kmp_int32 *ub = RCAST(kmp_int32 *, task->shareds) + 1;
4789 retval = (kmp_int64)*ub;
4791 kmp_int64 *ub = RCAST(kmp_int64 *, task->shareds) + 1;
4792 retval = (kmp_int64)*ub;
4796 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4800 void set_lb(kmp_uint64 lb) {
4801 #if defined(KMP_GOMP_COMPAT)
4803 if (!taskdata->td_flags.native) {
4804 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4807 if (taskdata->td_size_loop_bounds == 4) {
4808 kmp_uint32 *lower = RCAST(kmp_uint32 *, task->shareds);
4809 *lower = (kmp_uint32)lb;
4811 kmp_uint64 *lower = RCAST(kmp_uint64 *, task->shareds);
4812 *lower = (kmp_uint64)lb;
4816 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4819 void set_ub(kmp_uint64 ub) {
4820 #if defined(KMP_GOMP_COMPAT)
4822 if (!taskdata->td_flags.native) {
4823 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4826 if (taskdata->td_size_loop_bounds == 4) {
4827 kmp_uint32 *upper = RCAST(kmp_uint32 *, task->shareds) + 1;
4828 *upper = (kmp_uint32)ub;
4830 kmp_uint64 *upper = RCAST(kmp_uint64 *, task->shareds) + 1;
4831 *upper = (kmp_uint64)ub;
4835 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4856 void __kmp_taskloop_linear(
ident_t *loc,
int gtid, kmp_task_t *task,
4857 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4858 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
4859 kmp_uint64 grainsize, kmp_uint64 extras,
4860 kmp_int64 last_chunk, kmp_uint64 tc,
4866 KMP_TIME_PARTITIONED_BLOCK(OMP_taskloop_scheduling);
4867 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
4869 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4870 kmp_uint64 lower = task_bounds.get_lb();
4871 kmp_uint64 upper = task_bounds.get_ub();
4873 kmp_info_t *thread = __kmp_threads[gtid];
4874 kmp_taskdata_t *current_task = thread->th.th_current_task;
4875 kmp_task_t *next_task;
4876 kmp_int32 lastpriv = 0;
4878 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4879 (last_chunk < 0 ? last_chunk : extras));
4880 KMP_DEBUG_ASSERT(num_tasks > extras);
4881 KMP_DEBUG_ASSERT(num_tasks > 0);
4882 KA_TRACE(20, (
"__kmp_taskloop_linear: T#%d: %lld tasks, grainsize %lld, "
4883 "extras %lld, last_chunk %lld, i=%lld,%lld(%d)%lld, dup %p\n",
4884 gtid, num_tasks, grainsize, extras, last_chunk, lower, upper,
4885 ub_glob, st, task_dup));
4888 for (i = 0; i < num_tasks; ++i) {
4889 kmp_uint64 chunk_minus_1;
4891 chunk_minus_1 = grainsize - 1;
4893 chunk_minus_1 = grainsize;
4896 upper = lower + st * chunk_minus_1;
4900 if (i == num_tasks - 1) {
4903 KMP_DEBUG_ASSERT(upper == *ub);
4904 if (upper == ub_glob)
4906 }
else if (st > 0) {
4907 KMP_DEBUG_ASSERT((kmp_uint64)st > *ub - upper);
4908 if ((kmp_uint64)st > ub_glob - upper)
4911 KMP_DEBUG_ASSERT(upper + st < *ub);
4912 if (upper - ub_glob < (kmp_uint64)(-st))
4918 next_task = __kmp_task_dup_alloc(thread, task, 0);
4920 next_task = __kmp_task_dup_alloc(thread, task);
4923 kmp_taskdata_t *next_taskdata = KMP_TASK_TO_TASKDATA(next_task);
4924 kmp_taskloop_bounds_t next_task_bounds =
4925 kmp_taskloop_bounds_t(next_task, task_bounds);
4928 next_task_bounds.set_lb(lower);
4929 if (next_taskdata->td_flags.native) {
4930 next_task_bounds.set_ub(upper + (st > 0 ? 1 : -1));
4932 next_task_bounds.set_ub(upper);
4934 if (ptask_dup != NULL)
4936 ptask_dup(next_task, task, lastpriv);
4938 (
"__kmp_taskloop_linear: T#%d; task #%llu: task %p: lower %lld, "
4939 "upper %lld stride %lld, (offsets %p %p)\n",
4940 gtid, i, next_task, lower, upper, st,
4941 next_task_bounds.get_lower_offset(),
4942 next_task_bounds.get_upper_offset()));
4944 __kmp_omp_taskloop_task(NULL, gtid, next_task,
4947 if (ompt_enabled.ompt_callback_dispatch) {
4948 OMPT_GET_DISPATCH_CHUNK(next_taskdata->ompt_task_info.dispatch_chunk,
4953 __kmp_omp_task(gtid, next_task,
true);
4958 __kmp_task_start(gtid, task, current_task);
4960 __kmp_task_finish<false>(gtid, task, current_task);
4965 typedef struct __taskloop_params {
4972 kmp_uint64 num_tasks;
4973 kmp_uint64 grainsize;
4975 kmp_int64 last_chunk;
4977 kmp_uint64 num_t_min;
4981 } __taskloop_params_t;
4983 void __kmp_taskloop_recur(
ident_t *,
int, kmp_task_t *, kmp_uint64 *,
4984 kmp_uint64 *, kmp_int64, kmp_uint64, kmp_uint64,
4985 kmp_uint64, kmp_uint64, kmp_int64, kmp_uint64,
4993 int __kmp_taskloop_task(
int gtid,
void *ptask) {
4994 __taskloop_params_t *p =
4995 (__taskloop_params_t *)((kmp_task_t *)ptask)->shareds;
4996 kmp_task_t *task = p->task;
4997 kmp_uint64 *lb = p->lb;
4998 kmp_uint64 *ub = p->ub;
4999 void *task_dup = p->task_dup;
5001 kmp_int64 st = p->st;
5002 kmp_uint64 ub_glob = p->ub_glob;
5003 kmp_uint64 num_tasks = p->num_tasks;
5004 kmp_uint64 grainsize = p->grainsize;
5005 kmp_uint64 extras = p->extras;
5006 kmp_int64 last_chunk = p->last_chunk;
5007 kmp_uint64 tc = p->tc;
5008 kmp_uint64 num_t_min = p->num_t_min;
5010 void *codeptr_ra = p->codeptr_ra;
5013 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
5014 KMP_DEBUG_ASSERT(task != NULL);
5016 (
"__kmp_taskloop_task: T#%d, task %p: %lld tasks, grainsize"
5017 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
5018 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
5021 KMP_DEBUG_ASSERT(num_tasks * 2 + 1 > num_t_min);
5022 if (num_tasks > num_t_min)
5023 __kmp_taskloop_recur(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
5024 grainsize, extras, last_chunk, tc, num_t_min,
5030 __kmp_taskloop_linear(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
5031 grainsize, extras, last_chunk, tc,
5037 KA_TRACE(40, (
"__kmp_taskloop_task(exit): T#%d\n", gtid));
5059 void __kmp_taskloop_recur(
ident_t *loc,
int gtid, kmp_task_t *task,
5060 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
5061 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
5062 kmp_uint64 grainsize, kmp_uint64 extras,
5063 kmp_int64 last_chunk, kmp_uint64 tc,
5064 kmp_uint64 num_t_min,
5069 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
5070 KMP_DEBUG_ASSERT(task != NULL);
5071 KMP_DEBUG_ASSERT(num_tasks > num_t_min);
5073 (
"__kmp_taskloop_recur: T#%d, task %p: %lld tasks, grainsize"
5074 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
5075 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
5077 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
5078 kmp_uint64 lower = *lb;
5079 kmp_info_t *thread = __kmp_threads[gtid];
5081 kmp_task_t *next_task;
5082 size_t lower_offset =
5083 (
char *)lb - (
char *)task;
5084 size_t upper_offset =
5085 (
char *)ub - (
char *)task;
5087 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
5088 (last_chunk < 0 ? last_chunk : extras));
5089 KMP_DEBUG_ASSERT(num_tasks > extras);
5090 KMP_DEBUG_ASSERT(num_tasks > 0);
5093 kmp_uint64 lb1, ub0, tc0, tc1, ext0, ext1;
5094 kmp_int64 last_chunk0 = 0, last_chunk1 = 0;
5095 kmp_uint64 gr_size0 = grainsize;
5096 kmp_uint64 n_tsk0 = num_tasks >> 1;
5097 kmp_uint64 n_tsk1 = num_tasks - n_tsk0;
5098 if (last_chunk < 0) {
5100 last_chunk1 = last_chunk;
5101 tc0 = grainsize * n_tsk0;
5103 }
else if (n_tsk0 <= extras) {
5106 ext1 = extras - n_tsk0;
5107 tc0 = gr_size0 * n_tsk0;
5112 tc1 = grainsize * n_tsk1;
5115 ub0 = lower + st * (tc0 - 1);
5120 next_task = __kmp_task_dup_alloc(thread, task,
5123 next_task = __kmp_task_dup_alloc(thread, task);
5126 *(kmp_uint64 *)((
char *)next_task + lower_offset) = lb1;
5127 if (ptask_dup != NULL)
5128 ptask_dup(next_task, task, 0);
5133 kmp_taskdata_t *current_task = thread->th.th_current_task;
5134 thread->th.th_current_task = taskdata->td_parent;
5135 kmp_task_t *new_task =
5136 __kmpc_omp_task_alloc(loc, gtid, 1, 3 *
sizeof(
void *),
5137 sizeof(__taskloop_params_t), &__kmp_taskloop_task);
5139 thread->th.th_current_task = current_task;
5140 __taskloop_params_t *p = (__taskloop_params_t *)new_task->shareds;
5141 p->task = next_task;
5142 p->lb = (kmp_uint64 *)((
char *)next_task + lower_offset);
5143 p->ub = (kmp_uint64 *)((
char *)next_task + upper_offset);
5144 p->task_dup = task_dup;
5146 p->ub_glob = ub_glob;
5147 p->num_tasks = n_tsk1;
5148 p->grainsize = grainsize;
5150 p->last_chunk = last_chunk1;
5152 p->num_t_min = num_t_min;
5154 p->codeptr_ra = codeptr_ra;
5158 kmp_taskdata_t *new_task_data = KMP_TASK_TO_TASKDATA(new_task);
5159 new_task_data->tdg = taskdata->tdg;
5160 new_task_data->is_taskgraph = 0;
5165 __kmp_omp_taskloop_task(NULL, gtid, new_task, codeptr_ra);
5167 __kmp_omp_task(gtid, new_task,
true);
5171 if (n_tsk0 > num_t_min)
5172 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0, gr_size0,
5173 ext0, last_chunk0, tc0, num_t_min,
5179 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0,
5180 gr_size0, ext0, last_chunk0, tc0,
5186 KA_TRACE(40, (
"__kmp_taskloop_recur(exit): T#%d\n", gtid));
5189 static void __kmp_taskloop(
ident_t *loc,
int gtid, kmp_task_t *task,
int if_val,
5190 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
5191 int nogroup,
int sched, kmp_uint64 grainsize,
5192 int modifier,
void *task_dup) {
5193 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
5194 KMP_DEBUG_ASSERT(task != NULL);
5196 #if OMPT_SUPPORT && OMPT_OPTIONAL
5197 OMPT_STORE_RETURN_ADDRESS(gtid);
5199 __kmpc_taskgroup(loc, gtid);
5203 KMP_ATOMIC_DEC(&__kmp_tdg_task_id);
5207 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
5210 kmp_uint64 lower = task_bounds.get_lb();
5211 kmp_uint64 upper = task_bounds.get_ub();
5212 kmp_uint64 ub_glob = upper;
5213 kmp_uint64 num_tasks = 0, extras = 0;
5214 kmp_int64 last_chunk =
5216 kmp_uint64 num_tasks_min = __kmp_taskloop_min_tasks;
5217 kmp_info_t *thread = __kmp_threads[gtid];
5218 kmp_taskdata_t *current_task = thread->th.th_current_task;
5220 KA_TRACE(20, (
"__kmp_taskloop: T#%d, task %p, lb %lld, ub %lld, st %lld, "
5221 "grain %llu(%d, %d), dup %p\n",
5222 gtid, taskdata, lower, upper, st, grainsize, sched, modifier,
5227 tc = upper - lower + 1;
5228 }
else if (st < 0) {
5229 tc = (lower - upper) / (-st) + 1;
5231 tc = (upper - lower) / st + 1;
5234 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d zero-trip loop\n", gtid));
5236 __kmp_task_start(gtid, task, current_task);
5238 __kmp_task_finish<false>(gtid, task, current_task);
5242 #if OMPT_SUPPORT && OMPT_OPTIONAL
5243 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
5244 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
5245 if (ompt_enabled.ompt_callback_work) {
5246 ompt_callbacks.ompt_callback(ompt_callback_work)(
5247 ompt_work_taskloop, ompt_scope_begin, &(team_info->parallel_data),
5248 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
5252 if (num_tasks_min == 0)
5255 KMP_MIN(thread->th.th_team_nproc * 10, INITIAL_TASK_DEQUE_SIZE);
5261 grainsize = thread->th.th_team_nproc *
static_cast<kmp_uint64
>(10);
5264 if (grainsize > tc) {
5269 num_tasks = grainsize;
5270 grainsize = tc / num_tasks;
5271 extras = tc % num_tasks;
5275 if (grainsize > tc) {
5281 num_tasks = (tc + grainsize - 1) / grainsize;
5282 last_chunk = tc - (num_tasks * grainsize);
5285 num_tasks = tc / grainsize;
5287 grainsize = tc / num_tasks;
5288 extras = tc % num_tasks;
5293 KMP_ASSERT2(0,
"unknown scheduling of taskloop");
5296 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
5297 (last_chunk < 0 ? last_chunk : extras));
5298 KMP_DEBUG_ASSERT(num_tasks > extras);
5299 KMP_DEBUG_ASSERT(num_tasks > 0);
5305 taskdata->td_flags.task_serial = 1;
5306 taskdata->td_flags.tiedness = TASK_TIED;
5308 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5309 grainsize, extras, last_chunk, tc,
5311 OMPT_GET_RETURN_ADDRESS(0),
5316 }
else if (num_tasks > num_tasks_min && !taskdata->td_flags.native) {
5317 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go recursive: tc %llu, #tasks %llu"
5318 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
5319 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
5321 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5322 grainsize, extras, last_chunk, tc, num_tasks_min,
5324 OMPT_GET_RETURN_ADDRESS(0),
5328 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go linear: tc %llu, #tasks %llu"
5329 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
5330 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
5332 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5333 grainsize, extras, last_chunk, tc,
5335 OMPT_GET_RETURN_ADDRESS(0),
5340 #if OMPT_SUPPORT && OMPT_OPTIONAL
5341 if (ompt_enabled.ompt_callback_work) {
5342 ompt_callbacks.ompt_callback(ompt_callback_work)(
5343 ompt_work_taskloop, ompt_scope_end, &(team_info->parallel_data),
5344 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
5349 #if OMPT_SUPPORT && OMPT_OPTIONAL
5350 OMPT_STORE_RETURN_ADDRESS(gtid);
5352 __kmpc_end_taskgroup(loc, gtid);
5354 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d\n", gtid));
5374 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
int nogroup,
5375 int sched, kmp_uint64 grainsize,
void *task_dup) {
5376 __kmp_assert_valid_gtid(gtid);
5377 KA_TRACE(20, (
"__kmpc_taskloop(enter): T#%d\n", gtid));
5378 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
5380 KA_TRACE(20, (
"__kmpc_taskloop(exit): T#%d\n", gtid));
5401 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
5402 int nogroup,
int sched, kmp_uint64 grainsize,
5403 int modifier,
void *task_dup) {
5404 __kmp_assert_valid_gtid(gtid);
5405 KA_TRACE(20, (
"__kmpc_taskloop_5(enter): T#%d\n", gtid));
5406 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
5407 modifier, task_dup);
5408 KA_TRACE(20, (
"__kmpc_taskloop_5(exit): T#%d\n", gtid));
5420 if (gtid == KMP_GTID_DNE)
5423 kmp_info_t *thread = __kmp_thread_from_gtid(gtid);
5424 kmp_taskdata_t *taskdata = thread->th.th_current_task;
5429 return &taskdata->td_target_data.async_handle;
5441 if (gtid == KMP_GTID_DNE)
5444 kmp_info_t *thread = __kmp_thread_from_gtid(gtid);
5445 kmp_taskdata_t *taskdata = thread->th.th_current_task;
5450 return taskdata->td_task_team != NULL;
5459 static kmp_tdg_info_t *__kmp_find_tdg(kmp_int32 tdg_id) {
5460 kmp_tdg_info_t *res =
nullptr;
5461 if (__kmp_max_tdgs == 0)
5464 if (__kmp_global_tdgs == NULL)
5465 __kmp_global_tdgs = (kmp_tdg_info_t **)__kmp_allocate(
5466 sizeof(kmp_tdg_info_t *) * __kmp_max_tdgs);
5468 if ((__kmp_global_tdgs[tdg_id]) &&
5469 (__kmp_global_tdgs[tdg_id]->tdg_status != KMP_TDG_NONE))
5470 res = __kmp_global_tdgs[tdg_id];
5477 void __kmp_print_tdg_dot(kmp_tdg_info_t *tdg, kmp_int32 gtid) {
5478 kmp_int32 tdg_id = tdg->tdg_id;
5479 KA_TRACE(10, (
"__kmp_print_tdg_dot(enter): T#%d tdg_id=%d \n", gtid, tdg_id));
5482 sprintf(file_name,
"tdg_%d.dot", tdg_id);
5485 kmp_int32 num_tasks = KMP_ATOMIC_LD_RLX(&tdg->num_tasks);
5489 " subgraph cluster {\n"
5492 for (kmp_int32 i = 0; i < num_tasks; i++) {
5493 fprintf(tdg_file,
" %d[style=bold]\n", i);
5495 fprintf(tdg_file,
" }\n");
5496 for (kmp_int32 i = 0; i < num_tasks; i++) {
5497 kmp_int32 nsuccessors = tdg->record_map[i].nsuccessors;
5498 kmp_int32 *successors = tdg->record_map[i].successors;
5499 if (nsuccessors > 0) {
5500 for (kmp_int32 j = 0; j < nsuccessors; j++)
5501 fprintf(tdg_file,
" %d -> %d \n", i, successors[j]);
5504 fprintf(tdg_file,
"}");
5505 KA_TRACE(10, (
"__kmp_print_tdg_dot(exit): T#%d tdg_id=%d \n", gtid, tdg_id));
5512 void __kmp_exec_tdg(kmp_int32 gtid, kmp_tdg_info_t *tdg) {
5513 KMP_DEBUG_ASSERT(tdg->tdg_status == KMP_TDG_READY);
5514 KA_TRACE(10, (
"__kmp_exec_tdg(enter): T#%d tdg_id=%d num_roots=%d\n", gtid,
5515 tdg->tdg_id, tdg->num_roots));
5516 kmp_node_info_t *this_record_map = tdg->record_map;
5517 kmp_int32 *this_root_tasks = tdg->root_tasks;
5518 kmp_int32 this_num_roots = tdg->num_roots;
5519 kmp_int32 this_num_tasks = KMP_ATOMIC_LD_RLX(&tdg->num_tasks);
5521 kmp_info_t *thread = __kmp_threads[gtid];
5522 kmp_taskdata_t *parent_task = thread->th.th_current_task;
5524 if (tdg->rec_taskred_data) {
5528 for (kmp_int32 j = 0; j < this_num_tasks; j++) {
5529 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(this_record_map[j].task);
5531 td->td_parent = parent_task;
5532 this_record_map[j].parent_task = parent_task;
5534 kmp_taskgroup_t *parent_taskgroup =
5535 this_record_map[j].parent_task->td_taskgroup;
5537 KMP_ATOMIC_ST_RLX(&this_record_map[j].npredecessors_counter,
5538 this_record_map[j].npredecessors);
5539 KMP_ATOMIC_INC(&this_record_map[j].parent_task->td_incomplete_child_tasks);
5541 if (parent_taskgroup) {
5542 KMP_ATOMIC_INC(&parent_taskgroup->count);
5544 td->td_taskgroup = parent_taskgroup;
5545 }
else if (td->td_taskgroup !=
nullptr) {
5547 td->td_taskgroup =
nullptr;
5549 if (this_record_map[j].parent_task->td_flags.tasktype == TASK_EXPLICIT)
5550 KMP_ATOMIC_INC(&this_record_map[j].parent_task->td_allocated_child_tasks);
5553 for (kmp_int32 j = 0; j < this_num_roots; ++j) {
5554 __kmp_omp_task(gtid, this_record_map[this_root_tasks[j]].task,
true);
5556 KA_TRACE(10, (
"__kmp_exec_tdg(exit): T#%d tdg_id=%d num_roots=%d\n", gtid,
5557 tdg->tdg_id, tdg->num_roots));
5565 static inline void __kmp_start_record(kmp_int32 gtid,
5566 kmp_taskgraph_flags_t *flags,
5568 kmp_tdg_info_t *tdg =
5569 (kmp_tdg_info_t *)__kmp_allocate(
sizeof(kmp_tdg_info_t));
5570 __kmp_global_tdgs[__kmp_curr_tdg_idx] = tdg;
5572 tdg->tdg_id = tdg_id;
5573 tdg->map_size = INIT_MAPSIZE;
5574 tdg->num_roots = -1;
5575 tdg->root_tasks =
nullptr;
5576 tdg->tdg_status = KMP_TDG_RECORDING;
5577 tdg->rec_num_taskred = 0;
5578 tdg->rec_taskred_data =
nullptr;
5579 KMP_ATOMIC_ST_RLX(&tdg->num_tasks, 0);
5582 kmp_node_info_t *this_record_map =
5583 (kmp_node_info_t *)__kmp_allocate(INIT_MAPSIZE *
sizeof(kmp_node_info_t));
5584 for (kmp_int32 i = 0; i < INIT_MAPSIZE; i++) {
5585 kmp_int32 *successorsList =
5586 (kmp_int32 *)__kmp_allocate(__kmp_successors_size *
sizeof(kmp_int32));
5587 this_record_map[i].task =
nullptr;
5588 this_record_map[i].successors = successorsList;
5589 this_record_map[i].nsuccessors = 0;
5590 this_record_map[i].npredecessors = 0;
5591 this_record_map[i].successors_size = __kmp_successors_size;
5592 KMP_ATOMIC_ST_RLX(&this_record_map[i].npredecessors_counter, 0);
5595 __kmp_global_tdgs[__kmp_curr_tdg_idx]->record_map = this_record_map;
5605 kmp_int32 __kmpc_start_record_task(
ident_t *loc_ref, kmp_int32 gtid,
5606 kmp_int32 input_flags, kmp_int32 tdg_id) {
5609 kmp_taskgraph_flags_t *flags = (kmp_taskgraph_flags_t *)&input_flags;
5611 (
"__kmpc_start_record_task(enter): T#%d loc=%p flags=%d tdg_id=%d\n",
5612 gtid, loc_ref, input_flags, tdg_id));
5614 if (__kmp_max_tdgs == 0) {
5617 (
"__kmpc_start_record_task(abandon): T#%d loc=%p flags=%d tdg_id = %d, "
5618 "__kmp_max_tdgs = 0\n",
5619 gtid, loc_ref, input_flags, tdg_id));
5623 __kmpc_taskgroup(loc_ref, gtid);
5624 if (kmp_tdg_info_t *tdg = __kmp_find_tdg(tdg_id)) {
5626 __kmp_exec_tdg(gtid, tdg);
5629 __kmp_curr_tdg_idx = tdg_id;
5630 KMP_DEBUG_ASSERT(__kmp_curr_tdg_idx < __kmp_max_tdgs);
5631 __kmp_start_record(gtid, flags, tdg_id);
5635 KA_TRACE(10, (
"__kmpc_start_record_task(exit): T#%d TDG %d starts to %s\n",
5636 gtid, tdg_id, res ?
"record" :
"execute"));
5643 void __kmp_end_record(kmp_int32 gtid, kmp_tdg_info_t *tdg) {
5645 kmp_node_info_t *this_record_map = tdg->record_map;
5646 kmp_int32 this_num_tasks = KMP_ATOMIC_LD_RLX(&tdg->num_tasks);
5647 kmp_int32 *this_root_tasks =
5648 (kmp_int32 *)__kmp_allocate(this_num_tasks *
sizeof(kmp_int32));
5649 kmp_int32 this_map_size = tdg->map_size;
5650 kmp_int32 this_num_roots = 0;
5651 kmp_info_t *thread = __kmp_threads[gtid];
5653 for (kmp_int32 i = 0; i < this_num_tasks; i++) {
5654 if (this_record_map[i].npredecessors == 0) {
5655 this_root_tasks[this_num_roots++] = i;
5660 tdg->map_size = this_map_size;
5661 tdg->num_roots = this_num_roots;
5662 tdg->root_tasks = this_root_tasks;
5663 KMP_DEBUG_ASSERT(tdg->tdg_status == KMP_TDG_RECORDING);
5664 tdg->tdg_status = KMP_TDG_READY;
5666 if (thread->th.th_current_task->td_dephash) {
5667 __kmp_dephash_free(thread, thread->th.th_current_task->td_dephash);
5668 thread->th.th_current_task->td_dephash = NULL;
5672 for (kmp_int32 i = 0; i < this_num_tasks; i++) {
5673 KMP_ATOMIC_ST_RLX(&this_record_map[i].npredecessors_counter,
5674 this_record_map[i].npredecessors);
5676 KMP_ATOMIC_ST_RLX(&__kmp_tdg_task_id, 0);
5679 __kmp_print_tdg_dot(tdg, gtid);
5689 void __kmpc_end_record_task(
ident_t *loc_ref, kmp_int32 gtid,
5690 kmp_int32 input_flags, kmp_int32 tdg_id) {
5691 kmp_tdg_info_t *tdg = __kmp_find_tdg(tdg_id);
5693 KA_TRACE(10, (
"__kmpc_end_record_task(enter): T#%d loc=%p finishes recording"
5694 " tdg=%d with flags=%d\n",
5695 gtid, loc_ref, tdg_id, input_flags));
5696 if (__kmp_max_tdgs) {
5698 __kmpc_end_taskgroup(loc_ref, gtid);
5699 if (__kmp_tdg_is_recording(tdg->tdg_status))
5700 __kmp_end_record(gtid, tdg);
5702 KA_TRACE(10, (
"__kmpc_end_record_task(exit): T#%d loc=%p finished recording"
5703 " tdg=%d, its status is now READY\n",
5704 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_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int sched, kmp_uint64 grainsize, void *task_dup)
void * __kmpc_taskred_modifier_init(ident_t *loc, int gtid, int is_ws, int num, void *data)
void * __kmpc_taskred_init(int gtid, int num, void *data)
void * __kmpc_task_reduction_init(int gtid, int num, void *data)
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_task_reduction_modifier_init(ident_t *loc, int gtid, int is_ws, int num, void *data)
void __kmpc_taskloop_5(ident_t *loc, int gtid, kmp_task_t *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int sched, kmp_uint64 grainsize, int modifier, void *task_dup)
void __kmpc_proxy_task_completed(kmp_int32 gtid, kmp_task_t *ptask)
void * __kmpc_task_reduction_get_th_data(int gtid, void *tskgrp, void *data)
void ** __kmpc_omp_get_target_async_handle_ptr(kmp_int32 gtid)
kmp_taskred_flags_t flags