17#include "kmp_wait_release.h"
18#include "kmp_taskdeps.h"
21#include "ompt-specific.h"
24#if ENABLE_LIBOMPTARGET
25static void (*tgt_target_nowait_query)(
void **);
27void __kmp_init_target_task() {
28 *(
void **)(&tgt_target_nowait_query) = KMP_DLSYM(
"__tgt_target_nowait_query");
33static void __kmp_enable_tasking(kmp_task_team_t *task_team,
34 kmp_info_t *this_thr);
35static void __kmp_alloc_task_deque(kmp_info_t *thread,
36 kmp_thread_data_t *thread_data);
37static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
38 kmp_task_team_t *task_team);
39static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask);
41static kmp_tdg_info_t *__kmp_find_tdg(kmp_int32 tdg_id);
42int __kmp_taskloop_task(
int gtid,
void *ptask);
48static bool __kmp_task_is_allowed(
int gtid,
const kmp_int32 is_constrained,
49 const kmp_taskdata_t *tasknew,
50 const kmp_taskdata_t *taskcurr) {
51 if (is_constrained && (tasknew->td_flags.tiedness == TASK_TIED)) {
55 kmp_taskdata_t *current = taskcurr->td_last_tied;
56 KMP_DEBUG_ASSERT(current != NULL);
58 if (current->td_flags.tasktype == TASK_EXPLICIT ||
59 current->td_taskwait_thread > 0) {
60 kmp_int32 level = current->td_level;
61 kmp_taskdata_t *parent = tasknew->td_parent;
62 while (parent != current && parent->td_level > level) {
64 parent = parent->td_parent;
65 KMP_DEBUG_ASSERT(parent != NULL);
67 if (parent != current)
72 kmp_depnode_t *node = tasknew->td_depnode;
74 if (!tasknew->is_taskgraph && UNLIKELY(node && (node->dn.mtx_num_locks > 0))) {
76 if (UNLIKELY(node && (node->dn.mtx_num_locks > 0))) {
78 for (
int i = 0; i < node->dn.mtx_num_locks; ++i) {
79 KMP_DEBUG_ASSERT(node->dn.mtx_locks[i] != NULL);
80 if (__kmp_test_lock(node->dn.mtx_locks[i], gtid))
83 for (
int j = i - 1; j >= 0; --j)
84 __kmp_release_lock(node->dn.mtx_locks[j], gtid);
88 node->dn.mtx_num_locks = -node->dn.mtx_num_locks;
97static void __kmp_realloc_task_deque(kmp_info_t *thread,
98 kmp_thread_data_t *thread_data) {
99 kmp_int32 size = TASK_DEQUE_SIZE(thread_data->td);
100 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == size);
101 kmp_int32 new_size = 2 * size;
103 KE_TRACE(10, (
"__kmp_realloc_task_deque: T#%d reallocating deque[from %d to "
104 "%d] for thread_data %p\n",
105 __kmp_gtid_from_thread(thread), size, new_size, thread_data));
107 kmp_taskdata_t **new_deque =
108 (kmp_taskdata_t **)__kmp_allocate(new_size *
sizeof(kmp_taskdata_t *));
111 for (i = thread_data->td.td_deque_head, j = 0; j < size;
112 i = (i + 1) & TASK_DEQUE_MASK(thread_data->td), j++)
113 new_deque[j] = thread_data->td.td_deque[i];
115 __kmp_free(thread_data->td.td_deque);
117 thread_data->td.td_deque_head = 0;
118 thread_data->td.td_deque_tail = size;
119 thread_data->td.td_deque = new_deque;
120 thread_data->td.td_deque_size = new_size;
123static kmp_task_pri_t *__kmp_alloc_task_pri_list() {
124 kmp_task_pri_t *l = (kmp_task_pri_t *)__kmp_allocate(
sizeof(kmp_task_pri_t));
125 kmp_thread_data_t *thread_data = &l->td;
126 __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
127 thread_data->td.td_deque_last_stolen = -1;
128 KE_TRACE(20, (
"__kmp_alloc_task_pri_list: T#%d allocating deque[%d] "
129 "for thread_data %p\n",
130 __kmp_get_gtid(), INITIAL_TASK_DEQUE_SIZE, thread_data));
131 thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
132 INITIAL_TASK_DEQUE_SIZE *
sizeof(kmp_taskdata_t *));
133 thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
142static kmp_thread_data_t *
143__kmp_get_priority_deque_data(kmp_task_team_t *task_team, kmp_int32 pri) {
144 kmp_thread_data_t *thread_data;
145 kmp_task_pri_t *lst = task_team->tt.tt_task_pri_list;
146 if (lst->priority == pri) {
148 thread_data = &lst->td;
149 }
else if (lst->priority < pri) {
152 kmp_task_pri_t *list = __kmp_alloc_task_pri_list();
153 thread_data = &list->td;
154 list->priority = pri;
156 task_team->tt.tt_task_pri_list = list;
158 kmp_task_pri_t *next_queue = lst->next;
159 while (next_queue && next_queue->priority > pri) {
161 next_queue = lst->next;
164 if (next_queue == NULL) {
166 kmp_task_pri_t *list = __kmp_alloc_task_pri_list();
167 thread_data = &list->td;
168 list->priority = pri;
171 }
else if (next_queue->priority == pri) {
173 thread_data = &next_queue->td;
176 kmp_task_pri_t *list = __kmp_alloc_task_pri_list();
177 thread_data = &list->td;
178 list->priority = pri;
179 list->next = next_queue;
187static kmp_int32 __kmp_push_priority_task(kmp_int32 gtid, kmp_info_t *thread,
188 kmp_taskdata_t *taskdata,
189 kmp_task_team_t *task_team,
191 kmp_thread_data_t *thread_data = NULL;
193 (
"__kmp_push_priority_task: T#%d trying to push task %p, pri %d.\n",
194 gtid, taskdata, pri));
197 kmp_task_pri_t *lst = task_team->tt.tt_task_pri_list;
198 if (UNLIKELY(lst == NULL)) {
199 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
200 if (task_team->tt.tt_task_pri_list == NULL) {
202 kmp_task_pri_t *list = __kmp_alloc_task_pri_list();
203 thread_data = &list->td;
204 list->priority = pri;
206 task_team->tt.tt_task_pri_list = list;
209 thread_data = __kmp_get_priority_deque_data(task_team, pri);
211 __kmp_release_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
213 if (lst->priority == pri) {
215 thread_data = &lst->td;
217 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
218 thread_data = __kmp_get_priority_deque_data(task_team, pri);
219 __kmp_release_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
222 KMP_DEBUG_ASSERT(thread_data);
224 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
226 if (TCR_4(thread_data->td.td_deque_ntasks) >=
227 TASK_DEQUE_SIZE(thread_data->td)) {
228 if (__kmp_enable_task_throttling &&
229 __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
230 thread->th.th_current_task)) {
231 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
232 KA_TRACE(20, (
"__kmp_push_priority_task: T#%d deque is full; returning "
233 "TASK_NOT_PUSHED for task %p\n",
235 return TASK_NOT_PUSHED;
238 __kmp_realloc_task_deque(thread, thread_data);
241 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) <
242 TASK_DEQUE_SIZE(thread_data->td));
244 thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
246 thread_data->td.td_deque_tail =
247 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
248 TCW_4(thread_data->td.td_deque_ntasks,
249 TCR_4(thread_data->td.td_deque_ntasks) + 1);
250 KMP_FSYNC_RELEASING(thread->th.th_current_task);
251 KMP_FSYNC_RELEASING(taskdata);
252 KA_TRACE(20, (
"__kmp_push_priority_task: T#%d returning "
253 "TASK_SUCCESSFULLY_PUSHED: task=%p ntasks=%d head=%u tail=%u\n",
254 gtid, taskdata, thread_data->td.td_deque_ntasks,
255 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
256 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
257 task_team->tt.tt_num_task_pri++;
258 return TASK_SUCCESSFULLY_PUSHED;
262static kmp_int32 __kmp_push_task(kmp_int32 gtid, kmp_task_t *task) {
263 kmp_info_t *thread = __kmp_threads[gtid];
264 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
269 if (UNLIKELY(taskdata->td_flags.hidden_helper &&
270 !KMP_HIDDEN_HELPER_THREAD(gtid))) {
271 kmp_int32 shadow_gtid = KMP_GTID_TO_SHADOW_GTID(gtid);
272 __kmpc_give_task(task, __kmp_tid_from_gtid(shadow_gtid));
274 __kmp_hidden_helper_worker_thread_signal();
275 return TASK_SUCCESSFULLY_PUSHED;
278 kmp_task_team_t *task_team = thread->th.th_task_team;
279 kmp_int32 tid = __kmp_tid_from_gtid(gtid);
280 kmp_thread_data_t *thread_data;
283 (
"__kmp_push_task: T#%d trying to push task %p.\n", gtid, taskdata));
285 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
288 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
289 KMP_DEBUG_USE_VAR(counter);
292 (
"__kmp_push_task: T#%d untied_count (%d) incremented for task %p\n",
293 gtid, counter, taskdata));
297 if (UNLIKELY(taskdata->td_flags.task_serial)) {
298 KA_TRACE(20, (
"__kmp_push_task: T#%d team serialized; returning "
299 "TASK_NOT_PUSHED for task %p\n",
301 return TASK_NOT_PUSHED;
306 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
307 if (UNLIKELY(!KMP_TASKING_ENABLED(task_team))) {
308 __kmp_enable_tasking(task_team, thread);
310 KMP_DEBUG_ASSERT(TCR_4(task_team->tt.tt_found_tasks) == TRUE);
311 KMP_DEBUG_ASSERT(TCR_PTR(task_team->tt.tt_threads_data) != NULL);
313 if (taskdata->td_flags.priority_specified && task->data2.priority > 0 &&
314 __kmp_max_task_priority > 0) {
315 int pri = KMP_MIN(task->data2.priority, __kmp_max_task_priority);
316 return __kmp_push_priority_task(gtid, thread, taskdata, task_team, pri);
320 thread_data = &task_team->tt.tt_threads_data[tid];
325 if (UNLIKELY(thread_data->td.td_deque == NULL)) {
326 __kmp_alloc_task_deque(thread, thread_data);
331 if (TCR_4(thread_data->td.td_deque_ntasks) >=
332 TASK_DEQUE_SIZE(thread_data->td)) {
333 if (__kmp_enable_task_throttling &&
334 __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
335 thread->th.th_current_task)) {
336 KA_TRACE(20, (
"__kmp_push_task: T#%d deque is full; returning "
337 "TASK_NOT_PUSHED for task %p\n",
339 return TASK_NOT_PUSHED;
341 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
343 if (TCR_4(thread_data->td.td_deque_ntasks) >=
344 TASK_DEQUE_SIZE(thread_data->td)) {
346 __kmp_realloc_task_deque(thread, thread_data);
352 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
354 if (TCR_4(thread_data->td.td_deque_ntasks) >=
355 TASK_DEQUE_SIZE(thread_data->td)) {
356 if (__kmp_enable_task_throttling &&
357 __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
358 thread->th.th_current_task)) {
359 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
360 KA_TRACE(20, (
"__kmp_push_task: T#%d deque is full on 2nd check; "
361 "returning TASK_NOT_PUSHED for task %p\n",
363 return TASK_NOT_PUSHED;
366 __kmp_realloc_task_deque(thread, thread_data);
371 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) <
372 TASK_DEQUE_SIZE(thread_data->td));
374 thread_data->td.td_deque[thread_data->td.td_deque_tail] =
377 thread_data->td.td_deque_tail =
378 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
379 TCW_4(thread_data->td.td_deque_ntasks,
380 TCR_4(thread_data->td.td_deque_ntasks) + 1);
381 KMP_FSYNC_RELEASING(thread->th.th_current_task);
382 KMP_FSYNC_RELEASING(taskdata);
383 KA_TRACE(20, (
"__kmp_push_task: T#%d returning TASK_SUCCESSFULLY_PUSHED: "
384 "task=%p ntasks=%d head=%u tail=%u\n",
385 gtid, taskdata, thread_data->td.td_deque_ntasks,
386 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
388 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
390 return TASK_SUCCESSFULLY_PUSHED;
397void __kmp_pop_current_task_from_thread(kmp_info_t *this_thr) {
398 KF_TRACE(10, (
"__kmp_pop_current_task_from_thread(enter): T#%d "
399 "this_thread=%p, curtask=%p, "
400 "curtask_parent=%p\n",
401 0, this_thr, this_thr->th.th_current_task,
402 this_thr->th.th_current_task->td_parent));
404 this_thr->th.th_current_task = this_thr->th.th_current_task->td_parent;
406 KF_TRACE(10, (
"__kmp_pop_current_task_from_thread(exit): T#%d "
407 "this_thread=%p, curtask=%p, "
408 "curtask_parent=%p\n",
409 0, this_thr, this_thr->th.th_current_task,
410 this_thr->th.th_current_task->td_parent));
419void __kmp_push_current_task_to_thread(kmp_info_t *this_thr, kmp_team_t *team,
423 KF_TRACE(10, (
"__kmp_push_current_task_to_thread(enter): T#%d this_thread=%p "
426 tid, this_thr, this_thr->th.th_current_task,
427 team->t.t_implicit_task_taskdata[tid].td_parent));
429 KMP_DEBUG_ASSERT(this_thr != NULL);
432 if (this_thr->th.th_current_task != &team->t.t_implicit_task_taskdata[0]) {
433 team->t.t_implicit_task_taskdata[0].td_parent =
434 this_thr->th.th_current_task;
435 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[0];
438 team->t.t_implicit_task_taskdata[tid].td_parent =
439 team->t.t_implicit_task_taskdata[0].td_parent;
440 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[tid];
443 KF_TRACE(10, (
"__kmp_push_current_task_to_thread(exit): T#%d this_thread=%p "
446 tid, this_thr, this_thr->th.th_current_task,
447 team->t.t_implicit_task_taskdata[tid].td_parent));
455static void __kmp_task_start(kmp_int32 gtid, kmp_task_t *task,
456 kmp_taskdata_t *current_task) {
457 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
458 kmp_info_t *thread = __kmp_threads[gtid];
461 (
"__kmp_task_start(enter): T#%d starting task %p: current_task=%p\n",
462 gtid, taskdata, current_task));
464 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
469 current_task->td_flags.executing = 0;
472 thread->th.th_current_task = taskdata;
474 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 0 ||
475 taskdata->td_flags.tiedness == TASK_UNTIED);
476 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0 ||
477 taskdata->td_flags.tiedness == TASK_UNTIED);
478 taskdata->td_flags.started = 1;
479 taskdata->td_flags.executing = 1;
480 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
481 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
488 KA_TRACE(10, (
"__kmp_task_start(exit): T#%d task=%p\n", gtid, taskdata));
498static inline void __ompt_task_start(kmp_task_t *task,
499 kmp_taskdata_t *current_task,
501 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
502 ompt_task_status_t status = ompt_task_switch;
503 if (__kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded) {
504 status = ompt_task_yield;
505 __kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded = 0;
508 if (ompt_enabled.ompt_callback_task_schedule) {
509 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
510 &(current_task->ompt_task_info.task_data), status,
511 &(taskdata->ompt_task_info.task_data));
513 taskdata->ompt_task_info.scheduling_parent = current_task;
518static inline void __ompt_task_finish(kmp_task_t *task,
519 kmp_taskdata_t *resumed_task,
520 ompt_task_status_t status) {
521 if (ompt_enabled.ompt_callback_task_schedule) {
522 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
523 if (__kmp_omp_cancellation && taskdata->td_taskgroup &&
524 taskdata->td_taskgroup->cancel_request == cancel_taskgroup) {
525 status = ompt_task_cancel;
529 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
530 &(taskdata->ompt_task_info.task_data), status,
531 (resumed_task ? &(resumed_task->ompt_task_info.task_data) : NULL));
537static void __kmpc_omp_task_begin_if0_template(
ident_t *loc_ref, kmp_int32 gtid,
540 void *return_address) {
541 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
542 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
544 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(enter): T#%d loc=%p task=%p "
546 gtid, loc_ref, taskdata, current_task));
548 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
551 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
552 KMP_DEBUG_USE_VAR(counter);
553 KA_TRACE(20, (
"__kmpc_omp_task_begin_if0: T#%d untied_count (%d) "
554 "incremented for task %p\n",
555 gtid, counter, taskdata));
558 taskdata->td_flags.task_serial =
560 __kmp_task_start(gtid, task, current_task);
564 if (current_task->ompt_task_info.frame.enter_frame.ptr == NULL) {
565 current_task->ompt_task_info.frame.enter_frame.ptr =
566 taskdata->ompt_task_info.frame.exit_frame.ptr = frame_address;
567 current_task->ompt_task_info.frame.enter_frame_flags =
568 taskdata->ompt_task_info.frame.exit_frame_flags =
569 OMPT_FRAME_FLAGS_APP;
571 if (ompt_enabled.ompt_callback_task_create) {
572 ompt_task_info_t *parent_info = &(current_task->ompt_task_info);
573 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
574 &(parent_info->task_data), &(parent_info->frame),
575 &(taskdata->ompt_task_info.task_data),
576 TASK_TYPE_DETAILS_FORMAT(taskdata), 0, return_address);
578 __ompt_task_start(task, current_task, gtid);
582 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(exit): T#%d loc=%p task=%p,\n", gtid,
588static void __kmpc_omp_task_begin_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
591 void *return_address) {
592 __kmpc_omp_task_begin_if0_template<true>(loc_ref, gtid, task, frame_address,
609__attribute__((target(
"backchain")))
611void __kmpc_omp_task_begin_if0(
ident_t *loc_ref, kmp_int32 gtid,
614 if (UNLIKELY(ompt_enabled.enabled)) {
615 OMPT_STORE_RETURN_ADDRESS(gtid);
616 __kmpc_omp_task_begin_if0_ompt(loc_ref, gtid, task,
617 OMPT_GET_FRAME_ADDRESS(1),
618 OMPT_LOAD_RETURN_ADDRESS(gtid));
622 __kmpc_omp_task_begin_if0_template<false>(loc_ref, gtid, task, NULL, NULL);
628void __kmpc_omp_task_begin(
ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task) {
629 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
633 (
"__kmpc_omp_task_begin(enter): T#%d loc=%p task=%p current_task=%p\n",
634 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task), current_task));
636 __kmp_task_start(gtid, task, current_task);
638 KA_TRACE(10, (
"__kmpc_omp_task_begin(exit): T#%d loc=%p task=%p,\n", gtid,
639 loc_ref, KMP_TASK_TO_TASKDATA(task)));
649static void __kmp_free_task(kmp_int32 gtid, kmp_taskdata_t *taskdata,
650 kmp_info_t *thread) {
651 KA_TRACE(30, (
"__kmp_free_task: T#%d freeing data from task %p\n", gtid,
655 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
656 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0);
657 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 1);
658 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
659 KMP_DEBUG_ASSERT(taskdata->td_allocated_child_tasks == 0 ||
660 taskdata->td_flags.task_serial == 1);
661 KMP_DEBUG_ASSERT(taskdata->td_incomplete_child_tasks == 0);
662 kmp_task_t *task = KMP_TASKDATA_TO_TASK(taskdata);
664 task->data1.destructors = NULL;
665 task->data2.priority = 0;
667 taskdata->td_flags.freed = 1;
670 if (!taskdata->is_taskgraph) {
674 __kmp_fast_free(thread, taskdata);
676 __kmp_thread_free(thread, taskdata);
680 taskdata->td_flags.complete = 0;
681 taskdata->td_flags.started = 0;
682 taskdata->td_flags.freed = 0;
683 taskdata->td_flags.executing = 0;
684 taskdata->td_flags.task_serial =
685 (taskdata->td_parent->td_flags.final ||
686 taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser);
689 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
690 KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0);
692 KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1);
696 KA_TRACE(20, (
"__kmp_free_task: T#%d freed task %p\n", gtid, taskdata));
705static void __kmp_free_task_and_ancestors(kmp_int32 gtid,
706 kmp_taskdata_t *taskdata,
707 kmp_info_t *thread) {
710 kmp_int32 team_serial =
711 (taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) &&
712 !taskdata->td_flags.proxy;
713 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
715 kmp_int32 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
716 KMP_DEBUG_ASSERT(children >= 0);
719 while (children == 0) {
720 kmp_taskdata_t *parent_taskdata = taskdata->td_parent;
722 KA_TRACE(20, (
"__kmp_free_task_and_ancestors(enter): T#%d task %p complete "
723 "and freeing itself\n",
727 __kmp_free_task(gtid, taskdata, thread);
729 taskdata = parent_taskdata;
735 if (taskdata->td_flags.tasktype == TASK_IMPLICIT) {
736 if (taskdata->td_dephash) {
737 int children = KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks);
738 kmp_tasking_flags_t flags_old = taskdata->td_flags;
739 if (children == 0 && flags_old.complete == 1) {
740 kmp_tasking_flags_t flags_new = flags_old;
741 flags_new.complete = 0;
742 if (KMP_COMPARE_AND_STORE_ACQ32(
743 RCAST(kmp_int32 *, &taskdata->td_flags),
744 *RCAST(kmp_int32 *, &flags_old),
745 *RCAST(kmp_int32 *, &flags_new))) {
746 KA_TRACE(100, (
"__kmp_free_task_and_ancestors: T#%d cleans "
747 "dephash of implicit task %p\n",
750 __kmp_dephash_free_entries(thread, taskdata->td_dephash);
757 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
758 KMP_DEBUG_ASSERT(children >= 0);
762 20, (
"__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; "
763 "not freeing it yet\n",
764 gtid, taskdata, children));
775static bool __kmp_track_children_task(kmp_taskdata_t *taskdata) {
776 kmp_tasking_flags_t flags = taskdata->td_flags;
777 bool ret = !(flags.team_serial || flags.tasking_ser);
778 ret = ret || flags.proxy == TASK_PROXY ||
779 flags.detachable == TASK_DETACHABLE || flags.hidden_helper;
781 KMP_ATOMIC_LD_ACQ(&taskdata->td_parent->td_incomplete_child_tasks) > 0;
783 if (taskdata->td_taskgroup && taskdata->is_taskgraph)
784 ret = ret || KMP_ATOMIC_LD_ACQ(&taskdata->td_taskgroup->count) > 0;
799static void __kmp_task_finish(kmp_int32 gtid, kmp_task_t *task,
800 kmp_taskdata_t *resumed_task) {
801 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
802 kmp_info_t *thread = __kmp_threads[gtid];
803 kmp_task_team_t *task_team =
804 thread->th.th_task_team;
810 kmp_int32 children = 0;
812 KA_TRACE(10, (
"__kmp_task_finish(enter): T#%d finishing task %p and resuming "
814 gtid, taskdata, resumed_task));
816 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
819 is_taskgraph = taskdata->is_taskgraph;
822 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
825 kmp_int32 counter = KMP_ATOMIC_DEC(&taskdata->td_untied_count) - 1;
828 (
"__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n",
829 gtid, counter, taskdata));
833 if (resumed_task == NULL) {
834 KMP_DEBUG_ASSERT(taskdata->td_flags.task_serial);
835 resumed_task = taskdata->td_parent;
838 thread->th.th_current_task = resumed_task;
839 resumed_task->td_flags.executing = 1;
840 KA_TRACE(10, (
"__kmp_task_finish(exit): T#%d partially done task %p, "
841 "resuming task %p\n",
842 gtid, taskdata, resumed_task));
850 (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) ==
851 taskdata->td_flags.task_serial);
852 if (taskdata->td_flags.task_serial) {
853 if (resumed_task == NULL) {
854 resumed_task = taskdata->td_parent;
858 KMP_DEBUG_ASSERT(resumed_task !=
868 if (UNLIKELY(taskdata->td_flags.destructors_thunk)) {
869 kmp_routine_entry_t destr_thunk = task->data1.destructors;
870 KMP_ASSERT(destr_thunk);
871 destr_thunk(gtid, task);
874 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
875 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 1);
876 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
878 bool completed =
true;
879 if (UNLIKELY(taskdata->td_flags.detachable == TASK_DETACHABLE)) {
880 if (taskdata->td_allow_completion_event.type ==
881 KMP_EVENT_ALLOW_COMPLETION) {
883 __kmp_acquire_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
884 if (taskdata->td_allow_completion_event.type ==
885 KMP_EVENT_ALLOW_COMPLETION) {
887 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
888 taskdata->td_flags.executing = 0;
895 __ompt_task_finish(task, resumed_task, ompt_task_detach);
901 taskdata->td_flags.proxy = TASK_PROXY;
904 __kmp_release_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
909 if (taskdata->td_target_data.async_handle != NULL) {
915 __ompt_task_finish(task, resumed_task, ompt_task_switch);
918 __kmpc_give_task(task, __kmp_tid_from_gtid(gtid));
919 if (KMP_HIDDEN_HELPER_THREAD(gtid))
920 __kmp_hidden_helper_worker_thread_signal();
925 taskdata->td_flags.complete = 1;
927 taskdata->td_flags.onced = 1;
933 __ompt_task_finish(task, resumed_task, ompt_task_complete);
937 if (__kmp_track_children_task(taskdata)) {
938 __kmp_release_deps(gtid, taskdata);
943 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks);
944 KMP_DEBUG_ASSERT(children >= 0);
946 if (taskdata->td_taskgroup && !taskdata->is_taskgraph)
948 if (taskdata->td_taskgroup)
950 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
951 }
else if (task_team && (task_team->tt.tt_found_proxy_tasks ||
952 task_team->tt.tt_hidden_helper_task_encountered)) {
955 __kmp_release_deps(gtid, taskdata);
961 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
962 taskdata->td_flags.executing = 0;
965 if (taskdata->td_flags.hidden_helper) {
967 KMP_ASSERT(KMP_HIDDEN_HELPER_THREAD(gtid));
968 KMP_ATOMIC_DEC(&__kmp_unexecuted_hidden_helper_tasks);
973 20, (
"__kmp_task_finish: T#%d finished task %p, %d incomplete children\n",
974 gtid, taskdata, children));
980 thread->th.th_current_task = resumed_task;
982 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
986 resumed_task->td_flags.executing = 1;
989 if (is_taskgraph && __kmp_track_children_task(taskdata) &&
990 taskdata->td_taskgroup) {
997 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
1002 10, (
"__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n",
1003 gtid, taskdata, resumed_task));
1009static void __kmpc_omp_task_complete_if0_template(
ident_t *loc_ref,
1012 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n",
1013 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
1014 KMP_DEBUG_ASSERT(gtid >= 0);
1016 __kmp_task_finish<ompt>(gtid, task, NULL);
1018 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n",
1019 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
1023 ompt_frame_t *ompt_frame;
1024 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
1025 ompt_frame->enter_frame = ompt_data_none;
1026 ompt_frame->enter_frame_flags = OMPT_FRAME_FLAGS_RUNTIME;
1035void __kmpc_omp_task_complete_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
1037 __kmpc_omp_task_complete_if0_template<true>(loc_ref, gtid, task);
1046void __kmpc_omp_task_complete_if0(
ident_t *loc_ref, kmp_int32 gtid,
1049 if (UNLIKELY(ompt_enabled.enabled)) {
1050 __kmpc_omp_task_complete_if0_ompt(loc_ref, gtid, task);
1054 __kmpc_omp_task_complete_if0_template<false>(loc_ref, gtid, task);
1060void __kmpc_omp_task_complete(
ident_t *loc_ref, kmp_int32 gtid,
1062 KA_TRACE(10, (
"__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n", gtid,
1063 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1065 __kmp_task_finish<false>(gtid, task,
1068 KA_TRACE(10, (
"__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n", gtid,
1069 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1085void __kmp_init_implicit_task(
ident_t *loc_ref, kmp_info_t *this_thr,
1086 kmp_team_t *team,
int tid,
int set_curr_task) {
1087 kmp_taskdata_t *task = &team->t.t_implicit_task_taskdata[tid];
1091 (
"__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n",
1092 tid, team, task, set_curr_task ?
"TRUE" :
"FALSE"));
1094 task->td_task_id = KMP_GEN_TASK_ID();
1095 task->td_team = team;
1098 task->td_ident = loc_ref;
1099 task->td_taskwait_ident = NULL;
1100 task->td_taskwait_counter = 0;
1101 task->td_taskwait_thread = 0;
1103 task->td_flags.tiedness = TASK_TIED;
1104 task->td_flags.tasktype = TASK_IMPLICIT;
1105 task->td_flags.proxy = TASK_FULL;
1108 task->td_flags.task_serial = 1;
1109 task->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1110 task->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1112 task->td_flags.started = 1;
1113 task->td_flags.executing = 1;
1114 task->td_flags.complete = 0;
1115 task->td_flags.freed = 0;
1117 task->td_flags.onced = 0;
1120 task->td_depnode = NULL;
1121 task->td_last_tied = task;
1122 task->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1124 if (set_curr_task) {
1125 KMP_ATOMIC_ST_REL(&task->td_incomplete_child_tasks, 0);
1127 KMP_ATOMIC_ST_REL(&task->td_allocated_child_tasks, 0);
1128 task->td_taskgroup = NULL;
1129 task->td_dephash = NULL;
1130 __kmp_push_current_task_to_thread(this_thr, team, tid);
1132 KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0);
1133 KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0);
1137 if (UNLIKELY(ompt_enabled.enabled))
1138 __ompt_task_init(task, tid);
1141 KF_TRACE(10, (
"__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n", tid,
1150void __kmp_finish_implicit_task(kmp_info_t *thread) {
1151 kmp_taskdata_t *task = thread->th.th_current_task;
1152#if ENABLE_LIBOMPTARGET
1155 if (UNLIKELY(kmp_target_sync_cb != NULL))
1156 (*kmp_target_sync_cb)(NULL, thread->th.th_info.ds.ds_gtid,
1157 KMP_TASKDATA_TO_TASK(task), NULL);
1159 if (task->td_dephash) {
1161 task->td_flags.complete = 1;
1163 task->td_flags.onced = 1;
1165 children = KMP_ATOMIC_LD_ACQ(&task->td_incomplete_child_tasks);
1166 kmp_tasking_flags_t flags_old = task->td_flags;
1167 if (children == 0 && flags_old.complete == 1) {
1168 kmp_tasking_flags_t flags_new = flags_old;
1169 flags_new.complete = 0;
1170 if (KMP_COMPARE_AND_STORE_ACQ32(RCAST(kmp_int32 *, &task->td_flags),
1171 *RCAST(kmp_int32 *, &flags_old),
1172 *RCAST(kmp_int32 *, &flags_new))) {
1173 KA_TRACE(100, (
"__kmp_finish_implicit_task: T#%d cleans "
1174 "dephash of implicit task %p\n",
1175 thread->th.th_info.ds.ds_gtid, task));
1176 __kmp_dephash_free_entries(thread, task->td_dephash);
1186void __kmp_free_implicit_task(kmp_info_t *thread) {
1187 kmp_taskdata_t *task = thread->th.th_current_task;
1188 if (task && task->td_dephash) {
1189 __kmp_dephash_free(thread, task->td_dephash);
1190 task->td_dephash = NULL;
1196static size_t __kmp_round_up_to_val(
size_t size,
size_t val) {
1197 if (size & (val - 1)) {
1199 if (size <= KMP_SIZE_T_MAX - val) {
1218kmp_task_t *__kmp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1219 kmp_tasking_flags_t *flags,
1220 size_t sizeof_kmp_task_t,
size_t sizeof_shareds,
1221 kmp_routine_entry_t task_entry) {
1223 kmp_taskdata_t *taskdata;
1224 kmp_info_t *thread = __kmp_threads[gtid];
1225 kmp_team_t *team = thread->th.th_team;
1226 kmp_taskdata_t *parent_task = thread->th.th_current_task;
1227 size_t shareds_offset;
1229 if (UNLIKELY(!TCR_4(__kmp_init_middle)))
1230 __kmp_middle_initialize();
1232 if (flags->hidden_helper) {
1233 if (__kmp_enable_hidden_helper) {
1234 if (!TCR_4(__kmp_init_hidden_helper))
1235 __kmp_hidden_helper_initialize();
1238 flags->hidden_helper = FALSE;
1242 KA_TRACE(10, (
"__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) "
1243 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1244 gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t,
1245 sizeof_shareds, task_entry));
1247 KMP_DEBUG_ASSERT(parent_task);
1248 if (parent_task->td_flags.final) {
1249 if (flags->merged_if0) {
1254 if (flags->tiedness == TASK_UNTIED && !team->t.t_serialized) {
1258 KMP_CHECK_UPDATE(thread->th.th_task_team->tt.tt_untied_task_encountered, 1);
1264 if (UNLIKELY(flags->proxy == TASK_PROXY ||
1265 flags->detachable == TASK_DETACHABLE || flags->hidden_helper)) {
1266 if (flags->proxy == TASK_PROXY) {
1267 flags->tiedness = TASK_UNTIED;
1268 flags->merged_if0 = 1;
1272 if ((thread->th.th_task_team) == NULL) {
1275 KMP_DEBUG_ASSERT(team->t.t_serialized);
1277 (
"T#%d creating task team in __kmp_task_alloc for proxy task\n",
1279 __kmp_task_team_setup(thread, team);
1280 thread->th.th_task_team = team->t.t_task_team[thread->th.th_task_state];
1282 kmp_task_team_t *task_team = thread->th.th_task_team;
1285 if (!KMP_TASKING_ENABLED(task_team)) {
1288 (
"T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid));
1289 __kmp_enable_tasking(task_team, thread);
1290 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
1291 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
1293 if (thread_data->td.td_deque == NULL) {
1294 __kmp_alloc_task_deque(thread, thread_data);
1298 if ((flags->proxy == TASK_PROXY || flags->detachable == TASK_DETACHABLE) &&
1299 task_team->tt.tt_found_proxy_tasks == FALSE)
1300 TCW_4(task_team->tt.tt_found_proxy_tasks, TRUE);
1301 if (flags->hidden_helper &&
1302 task_team->tt.tt_hidden_helper_task_encountered == FALSE)
1303 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, TRUE);
1308 shareds_offset =
sizeof(kmp_taskdata_t) + sizeof_kmp_task_t;
1309 shareds_offset = __kmp_round_up_to_val(shareds_offset,
sizeof(kmp_uint64));
1312 KA_TRACE(30, (
"__kmp_task_alloc: T#%d First malloc size: %ld\n", gtid,
1314 KA_TRACE(30, (
"__kmp_task_alloc: T#%d Second malloc size: %ld\n", gtid,
1319 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, shareds_offset +
1322 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, shareds_offset +
1326 task = KMP_TASKDATA_TO_TASK(taskdata);
1329#if KMP_ARCH_X86 || KMP_ARCH_PPC64 || KMP_ARCH_S390X || !KMP_HAVE_QUAD
1330 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(
double) - 1)) == 0);
1331 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(
double) - 1)) == 0);
1333 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(_Quad) - 1)) == 0);
1334 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(_Quad) - 1)) == 0);
1336 if (sizeof_shareds > 0) {
1338 task->shareds = &((
char *)taskdata)[shareds_offset];
1340 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
1343 task->shareds = NULL;
1345 task->routine = task_entry;
1348 taskdata->td_task_id = KMP_GEN_TASK_ID();
1349 taskdata->td_team = thread->th.th_team;
1350 taskdata->td_alloc_thread = thread;
1351 taskdata->td_parent = parent_task;
1352 taskdata->td_level = parent_task->td_level + 1;
1353 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
1354 taskdata->td_ident = loc_ref;
1355 taskdata->td_taskwait_ident = NULL;
1356 taskdata->td_taskwait_counter = 0;
1357 taskdata->td_taskwait_thread = 0;
1358 KMP_DEBUG_ASSERT(taskdata->td_parent != NULL);
1360 if (flags->proxy == TASK_FULL)
1361 copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs);
1363 taskdata->td_flags = *flags;
1364 taskdata->td_task_team = thread->th.th_task_team;
1365 taskdata->td_size_alloc = shareds_offset + sizeof_shareds;
1366 taskdata->td_flags.tasktype = TASK_EXPLICIT;
1369 if (flags->hidden_helper) {
1370 kmp_info_t *shadow_thread = __kmp_threads[KMP_GTID_TO_SHADOW_GTID(gtid)];
1371 taskdata->td_team = shadow_thread->th.th_team;
1372 taskdata->td_task_team = shadow_thread->th.th_task_team;
1376 taskdata->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1379 taskdata->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1385 taskdata->td_flags.task_serial =
1386 (parent_task->td_flags.final || taskdata->td_flags.team_serial ||
1387 taskdata->td_flags.tasking_ser || flags->merged_if0);
1389 taskdata->td_flags.started = 0;
1390 taskdata->td_flags.executing = 0;
1391 taskdata->td_flags.complete = 0;
1392 taskdata->td_flags.freed = 0;
1394 taskdata->td_flags.onced = 0;
1395 taskdata->is_taskgraph = 0;
1396 taskdata->tdg =
nullptr;
1398 KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0);
1400 KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1);
1401 taskdata->td_taskgroup =
1402 parent_task->td_taskgroup;
1403 taskdata->td_dephash = NULL;
1404 taskdata->td_depnode = NULL;
1405 taskdata->td_target_data.async_handle = NULL;
1406 if (flags->tiedness == TASK_UNTIED)
1407 taskdata->td_last_tied = NULL;
1409 taskdata->td_last_tied = taskdata;
1410 taskdata->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1412 if (UNLIKELY(ompt_enabled.enabled))
1413 __ompt_task_init(taskdata, gtid);
1417 if (__kmp_track_children_task(taskdata)) {
1418 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
1419 if (parent_task->td_taskgroup)
1420 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
1423 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) {
1424 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
1426 if (flags->hidden_helper) {
1427 taskdata->td_flags.task_serial = FALSE;
1429 KMP_ATOMIC_INC(&__kmp_unexecuted_hidden_helper_tasks);
1434 kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_idx);
1435 if (tdg && __kmp_tdg_is_recording(tdg->tdg_status) &&
1436 (task_entry != (kmp_routine_entry_t)__kmp_taskloop_task)) {
1437 taskdata->is_taskgraph = 1;
1438 taskdata->tdg = __kmp_global_tdgs[__kmp_curr_tdg_idx];
1439 taskdata->td_task_id = KMP_GEN_TASK_ID();
1440 taskdata->td_tdg_task_id = KMP_ATOMIC_INC(&__kmp_tdg_task_id);
1443 KA_TRACE(20, (
"__kmp_task_alloc(exit): T#%d created task %p parent=%p\n",
1444 gtid, taskdata, taskdata->td_parent));
1449kmp_task_t *__kmpc_omp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1450 kmp_int32 flags,
size_t sizeof_kmp_task_t,
1451 size_t sizeof_shareds,
1452 kmp_routine_entry_t task_entry) {
1454 kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
1455 __kmp_assert_valid_gtid(gtid);
1456 input_flags->native = FALSE;
1458 KA_TRACE(10, (
"__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s %s) "
1459 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1460 gtid, loc_ref, input_flags->tiedness ?
"tied " :
"untied",
1461 input_flags->proxy ?
"proxy" :
"",
1462 input_flags->detachable ?
"detachable" :
"", sizeof_kmp_task_t,
1463 sizeof_shareds, task_entry));
1465 retval = __kmp_task_alloc(loc_ref, gtid, input_flags, sizeof_kmp_task_t,
1466 sizeof_shareds, task_entry);
1468 KA_TRACE(20, (
"__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval));
1473kmp_task_t *__kmpc_omp_target_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1475 size_t sizeof_kmp_task_t,
1476 size_t sizeof_shareds,
1477 kmp_routine_entry_t task_entry,
1478 kmp_int64 device_id) {
1479 auto &input_flags =
reinterpret_cast<kmp_tasking_flags_t &
>(flags);
1481 input_flags.tiedness = TASK_UNTIED;
1482 input_flags.target = 1;
1484 if (__kmp_enable_hidden_helper)
1485 input_flags.hidden_helper = TRUE;
1487 return __kmpc_omp_task_alloc(loc_ref, gtid, flags, sizeof_kmp_task_t,
1488 sizeof_shareds, task_entry);
1506 kmp_task_t *new_task, kmp_int32 naffins,
1507 kmp_task_affinity_info_t *affin_list) {
1517__attribute__((target(
"backchain")))
1520__kmp_invoke_task(kmp_int32 gtid, kmp_task_t *task,
1521 kmp_taskdata_t *current_task) {
1522 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
1526 30, (
"__kmp_invoke_task(enter): T#%d invoking task %p, current_task=%p\n",
1527 gtid, taskdata, current_task));
1528 KMP_DEBUG_ASSERT(task);
1529 if (UNLIKELY(taskdata->td_flags.proxy == TASK_PROXY &&
1530 taskdata->td_flags.complete == 1)) {
1535 (
"__kmp_invoke_task: T#%d running bottom finish for proxy task %p\n",
1538 __kmp_bottom_half_finish_proxy(gtid, task);
1540 KA_TRACE(30, (
"__kmp_invoke_task(exit): T#%d completed bottom finish for "
1541 "proxy task %p, resuming task %p\n",
1542 gtid, taskdata, current_task));
1550 ompt_thread_info_t oldInfo;
1551 if (UNLIKELY(ompt_enabled.enabled)) {
1553 thread = __kmp_threads[gtid];
1554 oldInfo = thread->th.ompt_thread_info;
1555 thread->th.ompt_thread_info.wait_id = 0;
1556 thread->th.ompt_thread_info.state = (thread->th.th_team_serialized)
1557 ? ompt_state_work_serial
1558 : ompt_state_work_parallel;
1559 taskdata->ompt_task_info.frame.exit_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1564 if (taskdata->td_flags.proxy != TASK_PROXY) {
1565 __kmp_task_start(gtid, task, current_task);
1571 if (UNLIKELY(__kmp_omp_cancellation)) {
1572 thread = __kmp_threads[gtid];
1573 kmp_team_t *this_team = thread->th.th_team;
1574 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
1575 if ((taskgroup && taskgroup->cancel_request) ||
1576 (this_team->t.t_cancel_request == cancel_parallel)) {
1577#if OMPT_SUPPORT && OMPT_OPTIONAL
1578 ompt_data_t *task_data;
1579 if (UNLIKELY(ompt_enabled.ompt_callback_cancel)) {
1580 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, NULL, NULL);
1581 ompt_callbacks.ompt_callback(ompt_callback_cancel)(
1583 ((taskgroup && taskgroup->cancel_request) ? ompt_cancel_taskgroup
1584 : ompt_cancel_parallel) |
1585 ompt_cancel_discarded_task,
1598 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
1599 taskdata->td_last_tied = current_task->td_last_tied;
1600 KMP_DEBUG_ASSERT(taskdata->td_last_tied);
1602#if KMP_STATS_ENABLED
1604 switch (KMP_GET_THREAD_STATE()) {
1605 case FORK_JOIN_BARRIER:
1606 KMP_PUSH_PARTITIONED_TIMER(OMP_task_join_bar);
1609 KMP_PUSH_PARTITIONED_TIMER(OMP_task_plain_bar);
1612 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskyield);
1615 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskwait);
1618 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskgroup);
1621 KMP_PUSH_PARTITIONED_TIMER(OMP_task_immediate);
1628 if (UNLIKELY(ompt_enabled.enabled))
1629 __ompt_task_start(task, current_task, gtid);
1631#if OMPT_SUPPORT && OMPT_OPTIONAL
1632 if (UNLIKELY(ompt_enabled.ompt_callback_dispatch &&
1633 taskdata->ompt_task_info.dispatch_chunk.iterations > 0)) {
1634 ompt_data_t instance = ompt_data_none;
1635 instance.ptr = &(taskdata->ompt_task_info.dispatch_chunk);
1636 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
1637 ompt_callbacks.ompt_callback(ompt_callback_dispatch)(
1638 &(team_info->parallel_data), &(taskdata->ompt_task_info.task_data),
1639 ompt_dispatch_taskloop_chunk, instance);
1640 taskdata->ompt_task_info.dispatch_chunk = {0, 0};
1645 if (ompd_state & OMPD_ENABLE_BP)
1646 ompd_bp_task_begin();
1649#if USE_ITT_BUILD && USE_ITT_NOTIFY
1650 kmp_uint64 cur_time;
1651 kmp_int32 kmp_itt_count_task =
1652 __kmp_forkjoin_frames_mode == 3 && !taskdata->td_flags.task_serial &&
1653 current_task->td_flags.tasktype == TASK_IMPLICIT;
1654 if (kmp_itt_count_task) {
1655 thread = __kmp_threads[gtid];
1657 if (thread->th.th_bar_arrive_time)
1658 cur_time = __itt_get_timestamp();
1660 kmp_itt_count_task = 0;
1662 KMP_FSYNC_ACQUIRED(taskdata);
1665#if ENABLE_LIBOMPTARGET
1666 if (taskdata->td_target_data.async_handle != NULL) {
1670 KMP_ASSERT(tgt_target_nowait_query);
1671 tgt_target_nowait_query(&taskdata->td_target_data.async_handle);
1674 if (task->routine != NULL) {
1675#ifdef KMP_GOMP_COMPAT
1676 if (taskdata->td_flags.native) {
1677 ((void (*)(
void *))(*(task->routine)))(task->shareds);
1681 (*(task->routine))(gtid, task);
1684 KMP_POP_PARTITIONED_TIMER();
1686#if USE_ITT_BUILD && USE_ITT_NOTIFY
1687 if (kmp_itt_count_task) {
1689 thread->th.th_bar_arrive_time += (__itt_get_timestamp() - cur_time);
1691 KMP_FSYNC_CANCEL(taskdata);
1692 KMP_FSYNC_RELEASING(taskdata->td_parent);
1697 if (ompd_state & OMPD_ENABLE_BP)
1702 if (taskdata->td_flags.proxy != TASK_PROXY) {
1704 if (UNLIKELY(ompt_enabled.enabled)) {
1705 thread->th.ompt_thread_info = oldInfo;
1706 if (taskdata->td_flags.tiedness == TASK_TIED) {
1707 taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
1709 __kmp_task_finish<true>(gtid, task, current_task);
1712 __kmp_task_finish<false>(gtid, task, current_task);
1715 else if (UNLIKELY(ompt_enabled.enabled && taskdata->td_flags.target)) {
1716 __ompt_task_finish(task, current_task, ompt_task_switch);
1722 (
"__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n",
1723 gtid, taskdata, current_task));
1737kmp_int32 __kmpc_omp_task_parts(
ident_t *loc_ref, kmp_int32 gtid,
1738 kmp_task_t *new_task) {
1739 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1741 KA_TRACE(10, (
"__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n", gtid,
1742 loc_ref, new_taskdata));
1745 kmp_taskdata_t *parent;
1746 if (UNLIKELY(ompt_enabled.enabled)) {
1747 parent = new_taskdata->td_parent;
1748 if (ompt_enabled.ompt_callback_task_create) {
1749 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1750 &(parent->ompt_task_info.task_data), &(parent->ompt_task_info.frame),
1751 &(new_taskdata->ompt_task_info.task_data),
1752 TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1753 OMPT_GET_RETURN_ADDRESS(0));
1761 if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1763 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1764 new_taskdata->td_flags.task_serial = 1;
1765 __kmp_invoke_task(gtid, new_task, current_task);
1770 (
"__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: "
1771 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
1772 gtid, loc_ref, new_taskdata));
1775 if (UNLIKELY(ompt_enabled.enabled)) {
1776 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1777 parent->ompt_task_info.frame.enter_frame_flags = OMPT_FRAME_FLAGS_RUNTIME;
1780 return TASK_CURRENT_NOT_QUEUED;
1794kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task,
1795 bool serialize_immediate) {
1796 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1799 if (new_taskdata->is_taskgraph &&
1800 __kmp_tdg_is_recording(new_taskdata->tdg->tdg_status)) {
1801 kmp_tdg_info_t *tdg = new_taskdata->tdg;
1803 if (new_taskdata->td_tdg_task_id >= new_taskdata->tdg->map_size) {
1804 __kmp_acquire_bootstrap_lock(&tdg->graph_lock);
1807 if (new_taskdata->td_tdg_task_id >= tdg->map_size) {
1808 kmp_uint old_size = tdg->map_size;
1809 kmp_uint new_size = old_size * 2;
1810 kmp_node_info_t *old_record = tdg->record_map;
1811 kmp_node_info_t *new_record = (kmp_node_info_t *)__kmp_allocate(
1812 new_size *
sizeof(kmp_node_info_t));
1814 KMP_MEMCPY(new_record, old_record, old_size *
sizeof(kmp_node_info_t));
1815 tdg->record_map = new_record;
1817 __kmp_free(old_record);
1819 for (kmp_int i = old_size; i < new_size; i++) {
1820 kmp_int32 *successorsList = (kmp_int32 *)__kmp_allocate(
1821 __kmp_successors_size *
sizeof(kmp_int32));
1822 new_record[i].task =
nullptr;
1823 new_record[i].successors = successorsList;
1824 new_record[i].nsuccessors = 0;
1825 new_record[i].npredecessors = 0;
1826 new_record[i].successors_size = __kmp_successors_size;
1827 KMP_ATOMIC_ST_REL(&new_record[i].npredecessors_counter, 0);
1831 tdg->map_size = new_size;
1833 __kmp_release_bootstrap_lock(&tdg->graph_lock);
1836 if (tdg->record_map[new_taskdata->td_tdg_task_id].task ==
nullptr) {
1837 tdg->record_map[new_taskdata->td_tdg_task_id].task = new_task;
1838 tdg->record_map[new_taskdata->td_tdg_task_id].parent_task =
1839 new_taskdata->td_parent;
1840 KMP_ATOMIC_INC(&tdg->num_tasks);
1847 if (new_taskdata->td_flags.proxy == TASK_PROXY ||
1848 __kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1850 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1851 if (serialize_immediate)
1852 new_taskdata->td_flags.task_serial = 1;
1853 __kmp_invoke_task(gtid, new_task, current_task);
1854 }
else if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME &&
1855 __kmp_wpolicy_passive) {
1856 kmp_info_t *this_thr = __kmp_threads[gtid];
1857 kmp_team_t *team = this_thr->th.th_team;
1858 kmp_int32 nthreads = this_thr->th.th_team_nproc;
1859 for (
int i = 0; i < nthreads; ++i) {
1860 kmp_info_t *thread = team->t.t_threads[i];
1861 if (thread == this_thr)
1863 if (thread->th.th_sleep_loc != NULL) {
1864 __kmp_null_resume_wrapper(thread);
1869 return TASK_CURRENT_NOT_QUEUED;
1884kmp_int32 __kmpc_omp_task(
ident_t *loc_ref, kmp_int32 gtid,
1885 kmp_task_t *new_task) {
1887 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1889#if KMP_DEBUG || OMPT_SUPPORT
1890 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1892 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1894 __kmp_assert_valid_gtid(gtid);
1897 kmp_taskdata_t *parent = NULL;
1898 if (UNLIKELY(ompt_enabled.enabled)) {
1899 if (!new_taskdata->td_flags.started) {
1900 OMPT_STORE_RETURN_ADDRESS(gtid);
1901 parent = new_taskdata->td_parent;
1902 if (!parent->ompt_task_info.frame.enter_frame.ptr) {
1903 parent->ompt_task_info.frame.enter_frame.ptr =
1904 OMPT_GET_FRAME_ADDRESS(0);
1906 if (ompt_enabled.ompt_callback_task_create) {
1907 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1908 &(parent->ompt_task_info.task_data),
1909 &(parent->ompt_task_info.frame),
1910 &(new_taskdata->ompt_task_info.task_data),
1911 TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1912 OMPT_LOAD_RETURN_ADDRESS(gtid));
1917 __ompt_task_finish(new_task,
1918 new_taskdata->ompt_task_info.scheduling_parent,
1920 new_taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
1925 res = __kmp_omp_task(gtid, new_task,
true);
1927 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
1928 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1929 gtid, loc_ref, new_taskdata));
1931 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
1932 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1951kmp_int32 __kmp_omp_taskloop_task(
ident_t *loc_ref, kmp_int32 gtid,
1952 kmp_task_t *new_task,
void *codeptr_ra) {
1954 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1956#if KMP_DEBUG || OMPT_SUPPORT
1957 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1959 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1963 kmp_taskdata_t *parent = NULL;
1964 if (UNLIKELY(ompt_enabled.enabled && !new_taskdata->td_flags.started)) {
1965 parent = new_taskdata->td_parent;
1966 if (!parent->ompt_task_info.frame.enter_frame.ptr)
1967 parent->ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
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, codeptr_ra);
1977 res = __kmp_omp_task(gtid, new_task,
true);
1979 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
1980 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1981 gtid, loc_ref, new_taskdata));
1983 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
1984 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1991static kmp_int32 __kmpc_omp_taskwait_template(
ident_t *loc_ref, kmp_int32 gtid,
1992 void *frame_address,
1993 void *return_address) {
1994 kmp_taskdata_t *taskdata =
nullptr;
1996 int thread_finished = FALSE;
1997 KMP_SET_THREAD_STATE_BLOCK(TASKWAIT);
1999 KA_TRACE(10, (
"__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref));
2000 KMP_DEBUG_ASSERT(gtid >= 0);
2002 if (__kmp_tasking_mode != tskm_immediate_exec) {
2003 thread = __kmp_threads[gtid];
2004 taskdata = thread->th.th_current_task;
2006#if OMPT_SUPPORT && OMPT_OPTIONAL
2007 ompt_data_t *my_task_data;
2008 ompt_data_t *my_parallel_data;
2011 my_task_data = &(taskdata->ompt_task_info.task_data);
2012 my_parallel_data = OMPT_CUR_TEAM_DATA(thread);
2014 taskdata->ompt_task_info.frame.enter_frame.ptr = frame_address;
2016 if (ompt_enabled.ompt_callback_sync_region) {
2017 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2018 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
2019 my_task_data, return_address);
2022 if (ompt_enabled.ompt_callback_sync_region_wait) {
2023 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2024 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
2025 my_task_data, return_address);
2030#if ENABLE_LIBOMPTARGET
2033 if (UNLIKELY(kmp_target_sync_cb))
2034 (*kmp_target_sync_cb)(loc_ref, gtid, KMP_TASKDATA_TO_TASK(taskdata),
2043 taskdata->td_taskwait_counter += 1;
2044 taskdata->td_taskwait_ident = loc_ref;
2045 taskdata->td_taskwait_thread = gtid + 1;
2048 void *itt_sync_obj = NULL;
2050 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2055 !taskdata->td_flags.team_serial && !taskdata->td_flags.final;
2057 must_wait = must_wait || (thread->th.th_task_team != NULL &&
2058 thread->th.th_task_team->tt.tt_found_proxy_tasks);
2062 (__kmp_enable_hidden_helper && thread->th.th_task_team != NULL &&
2063 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered);
2066 kmp_flag_32<false, false> flag(
2067 RCAST(std::atomic<kmp_uint32> *,
2068 &(taskdata->td_incomplete_child_tasks)),
2070 while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) != 0) {
2071 flag.execute_tasks(thread, gtid, FALSE,
2072 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2073 __kmp_task_stealing_constraint);
2077 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2078 KMP_FSYNC_ACQUIRED(taskdata);
2083 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2085#if OMPT_SUPPORT && OMPT_OPTIONAL
2087 if (ompt_enabled.ompt_callback_sync_region_wait) {
2088 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2089 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
2090 my_task_data, return_address);
2092 if (ompt_enabled.ompt_callback_sync_region) {
2093 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2094 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
2095 my_task_data, return_address);
2097 taskdata->ompt_task_info.frame.enter_frame = ompt_data_none;
2102 KA_TRACE(10, (
"__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, "
2103 "returning TASK_CURRENT_NOT_QUEUED\n",
2106 return TASK_CURRENT_NOT_QUEUED;
2109#if OMPT_SUPPORT && OMPT_OPTIONAL
2111static kmp_int32 __kmpc_omp_taskwait_ompt(
ident_t *loc_ref, kmp_int32 gtid,
2112 void *frame_address,
2113 void *return_address) {
2114 return __kmpc_omp_taskwait_template<true>(loc_ref, gtid, frame_address,
2121kmp_int32 __kmpc_omp_taskwait(
ident_t *loc_ref, kmp_int32 gtid) {
2122#if OMPT_SUPPORT && OMPT_OPTIONAL
2123 if (UNLIKELY(ompt_enabled.enabled)) {
2124 OMPT_STORE_RETURN_ADDRESS(gtid);
2125 return __kmpc_omp_taskwait_ompt(loc_ref, gtid, OMPT_GET_FRAME_ADDRESS(0),
2126 OMPT_LOAD_RETURN_ADDRESS(gtid));
2129 return __kmpc_omp_taskwait_template<false>(loc_ref, gtid, NULL, NULL);
2133kmp_int32 __kmpc_omp_taskyield(
ident_t *loc_ref, kmp_int32 gtid,
int end_part) {
2134 kmp_taskdata_t *taskdata = NULL;
2136 int thread_finished = FALSE;
2139 KMP_SET_THREAD_STATE_BLOCK(TASKYIELD);
2141 KA_TRACE(10, (
"__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n",
2142 gtid, loc_ref, end_part));
2143 __kmp_assert_valid_gtid(gtid);
2145 if (__kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel) {
2146 thread = __kmp_threads[gtid];
2147 taskdata = thread->th.th_current_task;
2154 taskdata->td_taskwait_counter += 1;
2155 taskdata->td_taskwait_ident = loc_ref;
2156 taskdata->td_taskwait_thread = gtid + 1;
2159 void *itt_sync_obj = NULL;
2161 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2164 if (!taskdata->td_flags.team_serial) {
2165 kmp_task_team_t *task_team = thread->th.th_task_team;
2166 if (task_team != NULL) {
2167 if (KMP_TASKING_ENABLED(task_team)) {
2169 if (UNLIKELY(ompt_enabled.enabled))
2170 thread->th.ompt_thread_info.ompt_task_yielded = 1;
2172 __kmp_execute_tasks_32(
2173 thread, gtid, (kmp_flag_32<> *)NULL, FALSE,
2174 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2175 __kmp_task_stealing_constraint);
2177 if (UNLIKELY(ompt_enabled.enabled))
2178 thread->th.ompt_thread_info.ompt_task_yielded = 0;
2184 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2189 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2192 KA_TRACE(10, (
"__kmpc_omp_taskyield(exit): T#%d task %p resuming, "
2193 "returning TASK_CURRENT_NOT_QUEUED\n",
2196 return TASK_CURRENT_NOT_QUEUED;
2217 unsigned reserved31 : 31;
2297template <
typename T>
2298void *__kmp_task_reduction_init(
int gtid,
int num, T *data) {
2299 __kmp_assert_valid_gtid(gtid);
2300 kmp_info_t *thread = __kmp_threads[gtid];
2301 kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup;
2302 kmp_uint32 nth = thread->th.th_team_nproc;
2306 KMP_ASSERT(tg != NULL);
2307 KMP_ASSERT(data != NULL);
2308 KMP_ASSERT(num > 0);
2309 if (nth == 1 && !__kmp_enable_hidden_helper) {
2310 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n",
2314 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, taskgroup %p, #items %d\n",
2318 for (
int i = 0; i < num; ++i) {
2319 size_t size = data[i].reduce_size - 1;
2321 size += CACHE_LINE - size % CACHE_LINE;
2322 KMP_ASSERT(data[i].reduce_comb != NULL);
2325 arr[i].
flags = data[i].flags;
2329 __kmp_assign_orig<T>(arr[i], data[i]);
2330 if (!arr[i].flags.lazy_priv) {
2333 arr[i].
reduce_pend = (
char *)(arr[i].reduce_priv) + nth * size;
2334 if (arr[i].reduce_init != NULL) {
2336 for (
size_t j = 0; j < nth; ++j) {
2337 __kmp_call_init<T>(arr[i], j * size);
2344 arr[i].
reduce_priv = __kmp_allocate(nth *
sizeof(
void *));
2347 tg->reduce_data = (
void *)arr;
2348 tg->reduce_num_data = num;
2368 kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_idx);
2369 if (tdg && __kmp_tdg_is_recording(tdg->tdg_status)) {
2370 kmp_tdg_info_t *this_tdg = __kmp_global_tdgs[__kmp_curr_tdg_idx];
2371 this_tdg->rec_taskred_data =
2373 this_tdg->rec_num_taskred = num;
2374 KMP_MEMCPY(this_tdg->rec_taskred_data, data,
2395 kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_idx);
2396 if (tdg && __kmp_tdg_is_recording(tdg->tdg_status)) {
2397 kmp_tdg_info_t *this_tdg = __kmp_global_tdgs[__kmp_curr_tdg_idx];
2398 this_tdg->rec_taskred_data =
2400 this_tdg->rec_num_taskred = num;
2401 KMP_MEMCPY(this_tdg->rec_taskred_data, data,
2409template <
typename T>
2410void __kmp_task_reduction_init_copy(kmp_info_t *thr,
int num, T *data,
2411 kmp_taskgroup_t *tg,
void *reduce_data) {
2413 KA_TRACE(20, (
"__kmp_task_reduction_init_copy: Th %p, init taskgroup %p,"
2415 thr, tg, reduce_data));
2420 for (
int i = 0; i < num; ++i) {
2423 tg->reduce_data = (
void *)arr;
2424 tg->reduce_num_data = num;
2437 __kmp_assert_valid_gtid(gtid);
2438 kmp_info_t *thread = __kmp_threads[gtid];
2439 kmp_int32 nth = thread->th.th_team_nproc;
2443 kmp_taskgroup_t *tg = (kmp_taskgroup_t *)tskgrp;
2445 tg = thread->th.th_current_task->td_taskgroup;
2446 KMP_ASSERT(tg != NULL);
2449 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
2452 if ((thread->th.th_current_task->is_taskgraph) &&
2453 (!__kmp_tdg_is_recording(
2454 __kmp_global_tdgs[__kmp_curr_tdg_idx]->tdg_status))) {
2455 tg = thread->th.th_current_task->td_taskgroup;
2456 KMP_ASSERT(tg != NULL);
2457 KMP_ASSERT(tg->reduce_data != NULL);
2459 num = tg->reduce_num_data;
2463 KMP_ASSERT(data != NULL);
2464 while (tg != NULL) {
2466 num = tg->reduce_num_data;
2467 for (
int i = 0; i < num; ++i) {
2468 if (!arr[i].flags.lazy_priv) {
2469 if (data == arr[i].reduce_shar ||
2470 (data >= arr[i].reduce_priv && data < arr[i].reduce_pend))
2471 return (
char *)(arr[i].
reduce_priv) + tid * arr[i].reduce_size;
2474 void **p_priv = (
void **)(arr[i].reduce_priv);
2475 if (data == arr[i].reduce_shar)
2478 for (
int j = 0; j < nth; ++j)
2479 if (data == p_priv[j])
2483 if (p_priv[tid] == NULL) {
2485 p_priv[tid] = __kmp_allocate(arr[i].reduce_size);
2486 if (arr[i].reduce_init != NULL) {
2487 if (arr[i].reduce_orig != NULL) {
2488 ((void (*)(
void *,
void *))arr[i].reduce_init)(
2491 ((void (*)(
void *))arr[i].reduce_init)(p_priv[tid]);
2498 KMP_ASSERT(tg->parent);
2501 KMP_ASSERT2(0,
"Unknown task reduction item");
2507static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg) {
2508 kmp_int32 nth = th->th.th_team_nproc;
2511 __kmp_enable_hidden_helper);
2514 kmp_int32 num = tg->reduce_num_data;
2515 for (
int i = 0; i < num; ++i) {
2517 void (*f_fini)(
void *) = (
void (*)(
void *))(arr[i].
reduce_fini);
2518 void (*f_comb)(
void *,
void *) =
2520 if (!arr[i].flags.lazy_priv) {
2523 for (
int j = 0; j < nth; ++j) {
2524 void *priv_data = (
char *)pr_data + j * size;
2525 f_comb(sh_data, priv_data);
2530 void **pr_data = (
void **)(arr[i].reduce_priv);
2531 for (
int j = 0; j < nth; ++j) {
2532 if (pr_data[j] != NULL) {
2533 f_comb(sh_data, pr_data[j]);
2536 __kmp_free(pr_data[j]);
2540 __kmp_free(arr[i].reduce_priv);
2542 __kmp_thread_free(th, arr);
2543 tg->reduce_data = NULL;
2544 tg->reduce_num_data = 0;
2550static void __kmp_task_reduction_clean(kmp_info_t *th, kmp_taskgroup_t *tg) {
2551 __kmp_thread_free(th, tg->reduce_data);
2552 tg->reduce_data = NULL;
2553 tg->reduce_num_data = 0;
2556template <
typename T>
2557void *__kmp_task_reduction_modifier_init(
ident_t *loc,
int gtid,
int is_ws,
2559 __kmp_assert_valid_gtid(gtid);
2560 kmp_info_t *thr = __kmp_threads[gtid];
2561 kmp_int32 nth = thr->th.th_team_nproc;
2562 __kmpc_taskgroup(loc, gtid);
2565 (
"__kmpc_reduction_modifier_init: T#%d, tg %p, exiting nth=1\n",
2566 gtid, thr->th.th_current_task->td_taskgroup));
2567 return (
void *)thr->th.th_current_task->td_taskgroup;
2569 kmp_team_t *team = thr->th.th_team;
2571 kmp_taskgroup_t *tg;
2572 reduce_data = KMP_ATOMIC_LD_RLX(&team->t.t_tg_reduce_data[is_ws]);
2573 if (reduce_data == NULL &&
2574 __kmp_atomic_compare_store(&team->t.t_tg_reduce_data[is_ws], reduce_data,
2577 KMP_DEBUG_ASSERT(reduce_data == NULL);
2579 tg = (kmp_taskgroup_t *)__kmp_task_reduction_init<T>(gtid, num, data);
2583 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[0]) == 0);
2584 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[1]) == 0);
2585 KMP_ATOMIC_ST_REL(&team->t.t_tg_reduce_data[is_ws], reduce_data);
2588 (reduce_data = KMP_ATOMIC_LD_ACQ(&team->t.t_tg_reduce_data[is_ws])) ==
2592 KMP_DEBUG_ASSERT(reduce_data > (
void *)1);
2593 tg = thr->th.th_current_task->td_taskgroup;
2594 __kmp_task_reduction_init_copy<T>(thr, num, data, tg, reduce_data);
2616 int num,
void *data) {
2617 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2637 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2650 __kmpc_end_taskgroup(loc, gtid);
2654void __kmpc_taskgroup(
ident_t *loc,
int gtid) {
2655 __kmp_assert_valid_gtid(gtid);
2656 kmp_info_t *thread = __kmp_threads[gtid];
2657 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2658 kmp_taskgroup_t *tg_new =
2659 (kmp_taskgroup_t *)__kmp_thread_malloc(thread,
sizeof(kmp_taskgroup_t));
2660 KA_TRACE(10, (
"__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new));
2661 KMP_ATOMIC_ST_RLX(&tg_new->count, 0);
2662 KMP_ATOMIC_ST_RLX(&tg_new->cancel_request, cancel_noreq);
2663 tg_new->parent = taskdata->td_taskgroup;
2664 tg_new->reduce_data = NULL;
2665 tg_new->reduce_num_data = 0;
2666 tg_new->gomp_data = NULL;
2667 taskdata->td_taskgroup = tg_new;
2669#if OMPT_SUPPORT && OMPT_OPTIONAL
2670 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2671 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2673 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2674 kmp_team_t *team = thread->th.th_team;
2675 ompt_data_t my_task_data = taskdata->ompt_task_info.task_data;
2677 ompt_data_t my_parallel_data = team->t.ompt_team_info.parallel_data;
2679 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2680 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2681 &(my_task_data), codeptr);
2688void __kmpc_end_taskgroup(
ident_t *loc,
int gtid) {
2689 __kmp_assert_valid_gtid(gtid);
2690 kmp_info_t *thread = __kmp_threads[gtid];
2691 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2692 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
2693 int thread_finished = FALSE;
2695#if OMPT_SUPPORT && OMPT_OPTIONAL
2697 ompt_data_t my_task_data;
2698 ompt_data_t my_parallel_data;
2699 void *codeptr =
nullptr;
2700 if (UNLIKELY(ompt_enabled.enabled)) {
2701 team = thread->th.th_team;
2702 my_task_data = taskdata->ompt_task_info.task_data;
2704 my_parallel_data = team->t.ompt_team_info.parallel_data;
2705 codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2707 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2711 KA_TRACE(10, (
"__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc));
2712 KMP_DEBUG_ASSERT(taskgroup != NULL);
2713 KMP_SET_THREAD_STATE_BLOCK(TASKGROUP);
2715 if (__kmp_tasking_mode != tskm_immediate_exec) {
2717 taskdata->td_taskwait_counter += 1;
2718 taskdata->td_taskwait_ident = loc;
2719 taskdata->td_taskwait_thread = gtid + 1;
2723 void *itt_sync_obj = NULL;
2725 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2729#if OMPT_SUPPORT && OMPT_OPTIONAL
2730 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2731 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2732 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2733 &(my_task_data), codeptr);
2737#if ENABLE_LIBOMPTARGET
2740 if (UNLIKELY(kmp_target_sync_cb))
2741 (*kmp_target_sync_cb)(loc, gtid, KMP_TASKDATA_TO_TASK(taskdata), NULL);
2744 if (!taskdata->td_flags.team_serial ||
2745 (thread->th.th_task_team != NULL &&
2746 (thread->th.th_task_team->tt.tt_found_proxy_tasks ||
2747 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered))) {
2748 kmp_flag_32<false, false> flag(
2749 RCAST(std::atomic<kmp_uint32> *, &(taskgroup->count)), 0U);
2750 while (KMP_ATOMIC_LD_ACQ(&taskgroup->count) != 0) {
2751 flag.execute_tasks(thread, gtid, FALSE,
2752 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2753 __kmp_task_stealing_constraint);
2756 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2758#if OMPT_SUPPORT && OMPT_OPTIONAL
2759 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2760 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2761 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2762 &(my_task_data), codeptr);
2767 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2768 KMP_FSYNC_ACQUIRED(taskdata);
2771 KMP_DEBUG_ASSERT(taskgroup->count == 0);
2773 if (taskgroup->reduce_data != NULL &&
2774 !taskgroup->gomp_data) {
2777 kmp_team_t *t = thread->th.th_team;
2781 if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[0])) != NULL &&
2784 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[0]);
2785 if (cnt == thread->th.th_team_nproc - 1) {
2788 __kmp_task_reduction_fini(thread, taskgroup);
2791 __kmp_thread_free(thread, reduce_data);
2792 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[0], NULL);
2793 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[0], 0);
2797 __kmp_task_reduction_clean(thread, taskgroup);
2799 }
else if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[1])) !=
2803 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[1]);
2804 if (cnt == thread->th.th_team_nproc - 1) {
2806 __kmp_task_reduction_fini(thread, taskgroup);
2809 __kmp_thread_free(thread, reduce_data);
2810 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[1], NULL);
2811 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[1], 0);
2815 __kmp_task_reduction_clean(thread, taskgroup);
2819 __kmp_task_reduction_fini(thread, taskgroup);
2823 taskdata->td_taskgroup = taskgroup->parent;
2824 __kmp_thread_free(thread, taskgroup);
2826 KA_TRACE(10, (
"__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n",
2829#if OMPT_SUPPORT && OMPT_OPTIONAL
2830 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2831 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2832 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2833 &(my_task_data), codeptr);
2838static kmp_task_t *__kmp_get_priority_task(kmp_int32 gtid,
2839 kmp_task_team_t *task_team,
2840 kmp_int32 is_constrained) {
2841 kmp_task_t *task = NULL;
2842 kmp_taskdata_t *taskdata;
2843 kmp_taskdata_t *current;
2844 kmp_thread_data_t *thread_data;
2845 int ntasks = task_team->tt.tt_num_task_pri;
2848 20, (
"__kmp_get_priority_task(exit #1): T#%d No tasks to get\n", gtid));
2853 if (__kmp_atomic_compare_store(&task_team->tt.tt_num_task_pri, ntasks,
2856 ntasks = task_team->tt.tt_num_task_pri;
2857 }
while (ntasks > 0);
2859 KA_TRACE(20, (
"__kmp_get_priority_task(exit #2): T#%d No tasks to get\n",
2865 kmp_task_pri_t *list = task_team->tt.tt_task_pri_list;
2867 KMP_ASSERT(list != NULL);
2868 thread_data = &list->td;
2869 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
2870 deque_ntasks = thread_data->td.td_deque_ntasks;
2871 if (deque_ntasks == 0) {
2872 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2873 KA_TRACE(20, (
"__kmp_get_priority_task: T#%d No tasks to get from %p\n",
2874 __kmp_get_gtid(), thread_data));
2877 }
while (deque_ntasks == 0);
2878 KMP_DEBUG_ASSERT(deque_ntasks);
2879 int target = thread_data->td.td_deque_head;
2880 current = __kmp_threads[gtid]->th.th_current_task;
2881 taskdata = thread_data->td.td_deque[target];
2882 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
2884 thread_data->td.td_deque_head =
2885 (target + 1) & TASK_DEQUE_MASK(thread_data->td);
2887 if (!task_team->tt.tt_untied_task_encountered) {
2889 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2890 KA_TRACE(20, (
"__kmp_get_priority_task(exit #3): T#%d could not get task "
2891 "from %p: task_team=%p ntasks=%d head=%u tail=%u\n",
2892 gtid, thread_data, task_team, deque_ntasks, target,
2893 thread_data->td.td_deque_tail));
2894 task_team->tt.tt_num_task_pri++;
2900 for (i = 1; i < deque_ntasks; ++i) {
2901 target = (target + 1) & TASK_DEQUE_MASK(thread_data->td);
2902 taskdata = thread_data->td.td_deque[target];
2903 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
2909 if (taskdata == NULL) {
2911 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2913 10, (
"__kmp_get_priority_task(exit #4): T#%d could not get task from "
2914 "%p: task_team=%p ntasks=%d head=%u tail=%u\n",
2915 gtid, thread_data, task_team, deque_ntasks,
2916 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2917 task_team->tt.tt_num_task_pri++;
2921 for (i = i + 1; i < deque_ntasks; ++i) {
2923 target = (target + 1) & TASK_DEQUE_MASK(thread_data->td);
2924 thread_data->td.td_deque[prev] = thread_data->td.td_deque[target];
2928 thread_data->td.td_deque_tail ==
2929 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(thread_data->td)));
2930 thread_data->td.td_deque_tail = target;
2932 thread_data->td.td_deque_ntasks = deque_ntasks - 1;
2933 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2934 task = KMP_TASKDATA_TO_TASK(taskdata);
2939static kmp_task_t *__kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid,
2940 kmp_task_team_t *task_team,
2941 kmp_int32 is_constrained) {
2943 kmp_taskdata_t *taskdata;
2944 kmp_thread_data_t *thread_data;
2947 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2948 KMP_DEBUG_ASSERT(task_team->tt.tt_threads_data !=
2951 thread_data = &task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
2953 KA_TRACE(10, (
"__kmp_remove_my_task(enter): T#%d ntasks=%d head=%u tail=%u\n",
2954 gtid, thread_data->td.td_deque_ntasks,
2955 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2957 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2959 (
"__kmp_remove_my_task(exit #1): T#%d No tasks to remove: "
2960 "ntasks=%d head=%u tail=%u\n",
2961 gtid, thread_data->td.td_deque_ntasks,
2962 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2966 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
2968 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2969 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2971 (
"__kmp_remove_my_task(exit #2): T#%d No tasks to remove: "
2972 "ntasks=%d head=%u tail=%u\n",
2973 gtid, thread_data->td.td_deque_ntasks,
2974 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2978 tail = (thread_data->td.td_deque_tail - 1) &
2979 TASK_DEQUE_MASK(thread_data->td);
2980 taskdata = thread_data->td.td_deque[tail];
2982 if (!__kmp_task_is_allowed(gtid, is_constrained, taskdata,
2983 thread->th.th_current_task)) {
2985 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2987 (
"__kmp_remove_my_task(exit #3): T#%d TSC blocks tail task: "
2988 "ntasks=%d head=%u tail=%u\n",
2989 gtid, thread_data->td.td_deque_ntasks,
2990 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2994 thread_data->td.td_deque_tail = tail;
2995 TCW_4(thread_data->td.td_deque_ntasks, thread_data->td.td_deque_ntasks - 1);
2997 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2999 KA_TRACE(10, (
"__kmp_remove_my_task(exit #4): T#%d task %p removed: "
3000 "ntasks=%d head=%u tail=%u\n",
3001 gtid, taskdata, thread_data->td.td_deque_ntasks,
3002 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3004 task = KMP_TASKDATA_TO_TASK(taskdata);
3011static kmp_task_t *__kmp_steal_task(kmp_int32 victim_tid, kmp_int32 gtid,
3012 kmp_task_team_t *task_team,
3013 std::atomic<kmp_int32> *unfinished_threads,
3014 int *thread_finished,
3015 kmp_int32 is_constrained) {
3017 kmp_taskdata_t *taskdata;
3018 kmp_taskdata_t *current;
3019 kmp_thread_data_t *victim_td, *threads_data;
3021 kmp_info_t *victim_thr;
3023 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3025 threads_data = task_team->tt.tt_threads_data;
3026 KMP_DEBUG_ASSERT(threads_data != NULL);
3027 KMP_DEBUG_ASSERT(victim_tid >= 0);
3028 KMP_DEBUG_ASSERT(victim_tid < task_team->tt.tt_max_threads);
3030 victim_td = &threads_data[victim_tid];
3031 victim_thr = victim_td->td.td_thr;
3034 KA_TRACE(10, (
"__kmp_steal_task(enter): T#%d try to steal from T#%d: "
3035 "task_team=%p ntasks=%d head=%u tail=%u\n",
3036 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
3037 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
3038 victim_td->td.td_deque_tail));
3040 if (TCR_4(victim_td->td.td_deque_ntasks) == 0) {
3041 KA_TRACE(10, (
"__kmp_steal_task(exit #1): T#%d could not steal from T#%d: "
3042 "task_team=%p ntasks=%d head=%u tail=%u\n",
3043 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
3044 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
3045 victim_td->td.td_deque_tail));
3049 __kmp_acquire_bootstrap_lock(&victim_td->td.td_deque_lock);
3051 int ntasks = TCR_4(victim_td->td.td_deque_ntasks);
3054 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3055 KA_TRACE(10, (
"__kmp_steal_task(exit #2): T#%d could not steal from T#%d: "
3056 "task_team=%p ntasks=%d head=%u tail=%u\n",
3057 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3058 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3062 KMP_DEBUG_ASSERT(victim_td->td.td_deque != NULL);
3063 current = __kmp_threads[gtid]->th.th_current_task;
3064 taskdata = victim_td->td.td_deque[victim_td->td.td_deque_head];
3065 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3067 victim_td->td.td_deque_head =
3068 (victim_td->td.td_deque_head + 1) & TASK_DEQUE_MASK(victim_td->td);
3070 if (!task_team->tt.tt_untied_task_encountered) {
3072 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3073 KA_TRACE(10, (
"__kmp_steal_task(exit #3): T#%d could not steal from "
3074 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
3075 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3076 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3081 target = victim_td->td.td_deque_head;
3083 for (i = 1; i < ntasks; ++i) {
3084 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
3085 taskdata = victim_td->td.td_deque[target];
3086 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3092 if (taskdata == NULL) {
3094 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3095 KA_TRACE(10, (
"__kmp_steal_task(exit #4): T#%d could not steal from "
3096 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
3097 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3098 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3102 for (i = i + 1; i < ntasks; ++i) {
3104 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
3105 victim_td->td.td_deque[prev] = victim_td->td.td_deque[target];
3109 victim_td->td.td_deque_tail ==
3110 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(victim_td->td)));
3111 victim_td->td.td_deque_tail = target;
3113 if (*thread_finished) {
3120 KMP_ATOMIC_INC(unfinished_threads);
3123 (
"__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n",
3124 gtid, count + 1, task_team));
3125 *thread_finished = FALSE;
3127 TCW_4(victim_td->td.td_deque_ntasks, ntasks - 1);
3129 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3133 (
"__kmp_steal_task(exit #5): T#%d stole task %p from T#%d: "
3134 "task_team=%p ntasks=%d head=%u tail=%u\n",
3135 gtid, taskdata, __kmp_gtid_from_thread(victim_thr), task_team,
3136 ntasks, victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3138 task = KMP_TASKDATA_TO_TASK(taskdata);
3152static inline int __kmp_execute_tasks_template(
3153 kmp_info_t *thread, kmp_int32 gtid, C *flag,
int final_spin,
3154 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3155 kmp_int32 is_constrained) {
3156 kmp_task_team_t *task_team = thread->th.th_task_team;
3157 kmp_thread_data_t *threads_data;
3159 kmp_info_t *other_thread;
3160 kmp_taskdata_t *current_task = thread->th.th_current_task;
3161 std::atomic<kmp_int32> *unfinished_threads;
3162 kmp_int32 nthreads, victim_tid = -2, use_own_tasks = 1, new_victim = 0,
3163 tid = thread->th.th_info.ds.ds_tid;
3165 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3166 KMP_DEBUG_ASSERT(thread == __kmp_threads[gtid]);
3168 if (task_team == NULL || current_task == NULL)
3171 KA_TRACE(15, (
"__kmp_execute_tasks_template(enter): T#%d final_spin=%d "
3172 "*thread_finished=%d\n",
3173 gtid, final_spin, *thread_finished));
3175 thread->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
3176 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
3178 KMP_DEBUG_ASSERT(threads_data != NULL);
3180 nthreads = task_team->tt.tt_nproc;
3181 unfinished_threads = &(task_team->tt.tt_unfinished_threads);
3182 KMP_DEBUG_ASSERT(*unfinished_threads >= 0);
3187#if ENABLE_LIBOMPTARGET
3189 if (UNLIKELY(kmp_target_sync_cb))
3190 (*kmp_target_sync_cb)(NULL, gtid, KMP_TASKDATA_TO_TASK(current_task),
3195 if (task_team->tt.tt_num_task_pri) {
3196 task = __kmp_get_priority_task(gtid, task_team, is_constrained);
3198 if (task == NULL && use_own_tasks) {
3199 task = __kmp_remove_my_task(thread, gtid, task_team, is_constrained);
3201 if ((task == NULL) && (nthreads > 1)) {
3205 if (victim_tid == -2) {
3206 victim_tid = threads_data[tid].td.td_deque_last_stolen;
3209 other_thread = threads_data[victim_tid].td.td_thr;
3211 if (victim_tid != -1) {
3213 }
else if (!new_victim) {
3219 victim_tid = __kmp_get_random(thread) % (nthreads - 1);
3220 if (victim_tid >= tid) {
3224 other_thread = threads_data[victim_tid].td.td_thr;
3234 if ((__kmp_tasking_mode == tskm_task_teams) &&
3235 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) &&
3236 (TCR_PTR(CCAST(
void *, other_thread->th.th_sleep_loc)) !=
3239 __kmp_null_resume_wrapper(other_thread);
3253 __kmp_steal_task(victim_tid, gtid, task_team, unfinished_threads,
3254 thread_finished, is_constrained);
3257 if (threads_data[tid].td.td_deque_last_stolen != victim_tid) {
3258 threads_data[tid].td.td_deque_last_stolen = victim_tid;
3265 KMP_CHECK_UPDATE(threads_data[tid].td.td_deque_last_stolen, -1);
3274#if USE_ITT_BUILD && USE_ITT_NOTIFY
3275 if (__itt_sync_create_ptr || KMP_ITT_DEBUG) {
3276 if (itt_sync_obj == NULL) {
3278 itt_sync_obj = __kmp_itt_barrier_object(gtid, bs_forkjoin_barrier);
3280 __kmp_itt_task_starting(itt_sync_obj);
3283 __kmp_invoke_task(gtid, task, current_task);
3285 if (itt_sync_obj != NULL)
3286 __kmp_itt_task_finished(itt_sync_obj);
3293 if (flag == NULL || (!final_spin && flag->done_check())) {
3296 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3300 if (thread->th.th_task_team == NULL) {
3303 KMP_YIELD(__kmp_library == library_throughput);
3306 if (!use_own_tasks && TCR_4(threads_data[tid].td.td_deque_ntasks) != 0) {
3307 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d stolen task spawned "
3308 "other tasks, restart\n",
3319 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks) == 0) {
3323 if (!*thread_finished) {
3325 kmp_int32 count = -1 +
3327 KMP_ATOMIC_DEC(unfinished_threads);
3328 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d dec "
3329 "unfinished_threads to %d task_team=%p\n",
3330 gtid, count, task_team));
3331 *thread_finished = TRUE;
3339 if (flag != NULL && flag->done_check()) {
3342 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3350 if (thread->th.th_task_team == NULL) {
3352 (
"__kmp_execute_tasks_template: T#%d no more tasks\n", gtid));
3361 if (flag == NULL || (!final_spin && flag->done_check())) {
3363 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3370 if (nthreads == 1 &&
3371 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks))
3375 (
"__kmp_execute_tasks_template: T#%d can't find work\n", gtid));
3381template <
bool C,
bool S>
3382int __kmp_execute_tasks_32(
3383 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32<C, S> *flag,
int final_spin,
3384 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3385 kmp_int32 is_constrained) {
3386 return __kmp_execute_tasks_template(
3387 thread, gtid, flag, final_spin,
3388 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3391template <
bool C,
bool S>
3392int __kmp_execute_tasks_64(
3393 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64<C, S> *flag,
int final_spin,
3394 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3395 kmp_int32 is_constrained) {
3396 return __kmp_execute_tasks_template(
3397 thread, gtid, flag, final_spin,
3398 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3401template <
bool C,
bool S>
3402int __kmp_atomic_execute_tasks_64(
3403 kmp_info_t *thread, kmp_int32 gtid, kmp_atomic_flag_64<C, S> *flag,
3404 int final_spin,
int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3405 kmp_int32 is_constrained) {
3406 return __kmp_execute_tasks_template(
3407 thread, gtid, flag, final_spin,
3408 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3411int __kmp_execute_tasks_oncore(
3412 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag,
int final_spin,
3413 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3414 kmp_int32 is_constrained) {
3415 return __kmp_execute_tasks_template(
3416 thread, gtid, flag, final_spin,
3417 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3421__kmp_execute_tasks_32<false, false>(kmp_info_t *, kmp_int32,
3422 kmp_flag_32<false, false> *,
int,
3423 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3425template int __kmp_execute_tasks_64<false, true>(kmp_info_t *, kmp_int32,
3426 kmp_flag_64<false, true> *,
3428 int *USE_ITT_BUILD_ARG(
void *),
3431template int __kmp_execute_tasks_64<true, false>(kmp_info_t *, kmp_int32,
3432 kmp_flag_64<true, false> *,
3434 int *USE_ITT_BUILD_ARG(
void *),
3437template int __kmp_atomic_execute_tasks_64<false, true>(
3438 kmp_info_t *, kmp_int32, kmp_atomic_flag_64<false, true> *,
int,
3439 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3441template int __kmp_atomic_execute_tasks_64<true, false>(
3442 kmp_info_t *, kmp_int32, kmp_atomic_flag_64<true, false> *,
int,
3443 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3448static void __kmp_enable_tasking(kmp_task_team_t *task_team,
3449 kmp_info_t *this_thr) {
3450 kmp_thread_data_t *threads_data;
3451 int nthreads, i, is_init_thread;
3453 KA_TRACE(10, (
"__kmp_enable_tasking(enter): T#%d\n",
3454 __kmp_gtid_from_thread(this_thr)));
3456 KMP_DEBUG_ASSERT(task_team != NULL);
3457 KMP_DEBUG_ASSERT(this_thr->th.th_team != NULL);
3459 nthreads = task_team->tt.tt_nproc;
3460 KMP_DEBUG_ASSERT(nthreads > 0);
3461 KMP_DEBUG_ASSERT(nthreads == this_thr->th.th_team->t.t_nproc);
3464 is_init_thread = __kmp_realloc_task_threads_data(this_thr, task_team);
3466 if (!is_init_thread) {
3470 (
"__kmp_enable_tasking(exit): T#%d: threads array already set up.\n",
3471 __kmp_gtid_from_thread(this_thr)));
3474 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
3475 KMP_DEBUG_ASSERT(threads_data != NULL);
3477 if (__kmp_tasking_mode == tskm_task_teams &&
3478 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME)) {
3482 for (i = 0; i < nthreads; i++) {
3484 kmp_info_t *thread = threads_data[i].td.td_thr;
3486 if (i == this_thr->th.th_info.ds.ds_tid) {
3495 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3497 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d waking up thread T#%d\n",
3498 __kmp_gtid_from_thread(this_thr),
3499 __kmp_gtid_from_thread(thread)));
3500 __kmp_null_resume_wrapper(thread);
3502 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d don't wake up thread T#%d\n",
3503 __kmp_gtid_from_thread(this_thr),
3504 __kmp_gtid_from_thread(thread)));
3509 KA_TRACE(10, (
"__kmp_enable_tasking(exit): T#%d\n",
3510 __kmp_gtid_from_thread(this_thr)));
3543static kmp_task_team_t *__kmp_free_task_teams =
3546kmp_bootstrap_lock_t __kmp_task_team_lock =
3547 KMP_BOOTSTRAP_LOCK_INITIALIZER(__kmp_task_team_lock);
3554static void __kmp_alloc_task_deque(kmp_info_t *thread,
3555 kmp_thread_data_t *thread_data) {
3556 __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
3557 KMP_DEBUG_ASSERT(thread_data->td.td_deque == NULL);
3560 thread_data->td.td_deque_last_stolen = -1;
3562 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == 0);
3563 KMP_DEBUG_ASSERT(thread_data->td.td_deque_head == 0);
3564 KMP_DEBUG_ASSERT(thread_data->td.td_deque_tail == 0);
3568 (
"__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n",
3569 __kmp_gtid_from_thread(thread), INITIAL_TASK_DEQUE_SIZE, thread_data));
3573 thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
3574 INITIAL_TASK_DEQUE_SIZE *
sizeof(kmp_taskdata_t *));
3575 thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
3581static void __kmp_free_task_deque(kmp_thread_data_t *thread_data) {
3582 if (thread_data->td.td_deque != NULL) {
3583 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3584 TCW_4(thread_data->td.td_deque_ntasks, 0);
3585 __kmp_free(thread_data->td.td_deque);
3586 thread_data->td.td_deque = NULL;
3587 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3598static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
3599 kmp_task_team_t *task_team) {
3600 kmp_thread_data_t **threads_data_p;
3601 kmp_int32 nthreads, maxthreads;
3602 int is_init_thread = FALSE;
3604 if (TCR_4(task_team->tt.tt_found_tasks)) {
3609 threads_data_p = &task_team->tt.tt_threads_data;
3610 nthreads = task_team->tt.tt_nproc;
3611 maxthreads = task_team->tt.tt_max_threads;
3616 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3618 if (!TCR_4(task_team->tt.tt_found_tasks)) {
3620 kmp_team_t *team = thread->th.th_team;
3623 is_init_thread = TRUE;
3624 if (maxthreads < nthreads) {
3626 if (*threads_data_p != NULL) {
3627 kmp_thread_data_t *old_data = *threads_data_p;
3628 kmp_thread_data_t *new_data = NULL;
3632 (
"__kmp_realloc_task_threads_data: T#%d reallocating "
3633 "threads data for task_team %p, new_size = %d, old_size = %d\n",
3634 __kmp_gtid_from_thread(thread), task_team, nthreads, maxthreads));
3639 new_data = (kmp_thread_data_t *)__kmp_allocate(
3640 nthreads *
sizeof(kmp_thread_data_t));
3642 KMP_MEMCPY_S((
void *)new_data, nthreads *
sizeof(kmp_thread_data_t),
3643 (
void *)old_data, maxthreads *
sizeof(kmp_thread_data_t));
3646 (*threads_data_p) = new_data;
3647 __kmp_free(old_data);
3649 KE_TRACE(10, (
"__kmp_realloc_task_threads_data: T#%d allocating "
3650 "threads data for task_team %p, size = %d\n",
3651 __kmp_gtid_from_thread(thread), task_team, nthreads));
3655 *threads_data_p = (kmp_thread_data_t *)__kmp_allocate(
3656 nthreads *
sizeof(kmp_thread_data_t));
3658 task_team->tt.tt_max_threads = nthreads;
3661 KMP_DEBUG_ASSERT(*threads_data_p != NULL);
3665 for (i = 0; i < nthreads; i++) {
3666 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3667 thread_data->td.td_thr = team->t.t_threads[i];
3669 if (thread_data->td.td_deque_last_stolen >= nthreads) {
3673 thread_data->td.td_deque_last_stolen = -1;
3678 TCW_SYNC_4(task_team->tt.tt_found_tasks, TRUE);
3681 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3682 return is_init_thread;
3688static void __kmp_free_task_threads_data(kmp_task_team_t *task_team) {
3689 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3690 if (task_team->tt.tt_threads_data != NULL) {
3692 for (i = 0; i < task_team->tt.tt_max_threads; i++) {
3693 __kmp_free_task_deque(&task_team->tt.tt_threads_data[i]);
3695 __kmp_free(task_team->tt.tt_threads_data);
3696 task_team->tt.tt_threads_data = NULL;
3698 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3704static void __kmp_free_task_pri_list(kmp_task_team_t *task_team) {
3705 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
3706 if (task_team->tt.tt_task_pri_list != NULL) {
3707 kmp_task_pri_t *list = task_team->tt.tt_task_pri_list;
3708 while (list != NULL) {
3709 kmp_task_pri_t *next = list->next;
3710 __kmp_free_task_deque(&list->td);
3714 task_team->tt.tt_task_pri_list = NULL;
3716 __kmp_release_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
3719static inline void __kmp_task_team_init(kmp_task_team_t *task_team,
3721 int team_nth = team->t.t_nproc;
3723 if (!task_team->tt.tt_active || team_nth != task_team->tt.tt_nproc) {
3724 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3725 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3726 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
3727 TCW_4(task_team->tt.tt_nproc, team_nth);
3728 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads, team_nth);
3729 TCW_4(task_team->tt.tt_active, TRUE);
3737static kmp_task_team_t *__kmp_allocate_task_team(kmp_info_t *thread,
3739 kmp_task_team_t *task_team = NULL;
3741 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d entering; team = %p\n",
3742 (thread ? __kmp_gtid_from_thread(thread) : -1), team));
3744 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3746 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3747 if (__kmp_free_task_teams != NULL) {
3748 task_team = __kmp_free_task_teams;
3749 TCW_PTR(__kmp_free_task_teams, task_team->tt.tt_next);
3750 task_team->tt.tt_next = NULL;
3752 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3755 if (task_team == NULL) {
3756 KE_TRACE(10, (
"__kmp_allocate_task_team: T#%d allocating "
3757 "task team for team %p\n",
3758 __kmp_gtid_from_thread(thread), team));
3761 task_team = (kmp_task_team_t *)__kmp_allocate(
sizeof(kmp_task_team_t));
3762 __kmp_init_bootstrap_lock(&task_team->tt.tt_threads_lock);
3763 __kmp_init_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
3764#if USE_ITT_BUILD && USE_ITT_NOTIFY && KMP_DEBUG
3767 __itt_suppress_mark_range(
3768 __itt_suppress_range, __itt_suppress_threading_errors,
3769 &task_team->tt.tt_found_tasks,
sizeof(task_team->tt.tt_found_tasks));
3770 __itt_suppress_mark_range(__itt_suppress_range,
3771 __itt_suppress_threading_errors,
3772 CCAST(kmp_uint32 *, &task_team->tt.tt_active),
3773 sizeof(task_team->tt.tt_active));
3781 __kmp_task_team_init(task_team, team);
3783 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d exiting; task_team = %p "
3784 "unfinished_threads init'd to %d\n",
3785 (thread ? __kmp_gtid_from_thread(thread) : -1), task_team,
3786 KMP_ATOMIC_LD_RLX(&task_team->tt.tt_unfinished_threads)));
3793void __kmp_free_task_team(kmp_info_t *thread, kmp_task_team_t *task_team) {
3794 KA_TRACE(20, (
"__kmp_free_task_team: T#%d task_team = %p\n",
3795 thread ? __kmp_gtid_from_thread(thread) : -1, task_team));
3798 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3800 KMP_DEBUG_ASSERT(task_team->tt.tt_next == NULL);
3801 task_team->tt.tt_next = __kmp_free_task_teams;
3802 TCW_PTR(__kmp_free_task_teams, task_team);
3804 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3812void __kmp_reap_task_teams(
void) {
3813 kmp_task_team_t *task_team;
3815 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3817 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3818 while ((task_team = __kmp_free_task_teams) != NULL) {
3819 __kmp_free_task_teams = task_team->tt.tt_next;
3820 task_team->tt.tt_next = NULL;
3823 if (task_team->tt.tt_threads_data != NULL) {
3824 __kmp_free_task_threads_data(task_team);
3826 if (task_team->tt.tt_task_pri_list != NULL) {
3827 __kmp_free_task_pri_list(task_team);
3829 __kmp_free(task_team);
3831 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3839void __kmp_push_task_team_node(kmp_info_t *thread, kmp_team_t *team) {
3840 KMP_DEBUG_ASSERT(team->t.t_nproc == 1);
3841 kmp_task_team_list_t *current =
3842 (kmp_task_team_list_t *)(&team->t.t_task_team[0]);
3843 kmp_task_team_list_t *node =
3844 (kmp_task_team_list_t *)__kmp_allocate(
sizeof(kmp_task_team_list_t));
3845 node->task_team = current->task_team;
3846 node->next = current->next;
3847 thread->th.th_task_team = current->task_team = NULL;
3848 current->next = node;
3852void __kmp_pop_task_team_node(kmp_info_t *thread, kmp_team_t *team) {
3853 KMP_DEBUG_ASSERT(team->t.t_nproc == 1);
3854 kmp_task_team_list_t *current =
3855 (kmp_task_team_list_t *)(&team->t.t_task_team[0]);
3856 if (current->task_team) {
3857 __kmp_free_task_team(thread, current->task_team);
3859 kmp_task_team_list_t *next = current->next;
3861 current->task_team = next->task_team;
3862 current->next = next->next;
3863 KMP_DEBUG_ASSERT(next != current);
3865 thread->th.th_task_team = current->task_team;
3872void __kmp_wait_to_unref_task_teams(
void) {
3878 KMP_INIT_YIELD(spins);
3879 KMP_INIT_BACKOFF(time);
3887 for (thread = CCAST(kmp_info_t *, __kmp_thread_pool); thread != NULL;
3888 thread = thread->th.th_next_pool) {
3892 if (TCR_PTR(thread->th.th_task_team) == NULL) {
3893 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n",
3894 __kmp_gtid_from_thread(thread)));
3899 if (!__kmp_is_thread_alive(thread, &exit_val)) {
3900 thread->th.th_task_team = NULL;
3907 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: Waiting for T#%d to "
3908 "unreference task_team\n",
3909 __kmp_gtid_from_thread(thread)));
3911 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
3914 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3918 (
"__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n",
3919 __kmp_gtid_from_thread(thread), __kmp_gtid_from_thread(thread)));
3920 __kmp_null_resume_wrapper(thread);
3929 KMP_YIELD_OVERSUB_ELSE_SPIN(spins, time);
3935void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team) {
3936 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3941 if (team == this_thr->th.th_serial_team ||
3942 team == this_thr->th.th_root->r.r_root_team) {
3943 KMP_DEBUG_ASSERT(team->t.t_nproc == 1);
3944 if (team->t.t_task_team[0] == NULL) {
3945 team->t.t_task_team[0] = __kmp_allocate_task_team(this_thr, team);
3947 20, (
"__kmp_task_team_setup: Primary T#%d created new task_team %p"
3948 " for serial/root team %p\n",
3949 __kmp_gtid_from_thread(this_thr), team->t.t_task_team[0], team));
3952 __kmp_task_team_init(team->t.t_task_team[0], team);
3960 if (team->t.t_task_team[this_thr->th.th_task_state] == NULL) {
3961 team->t.t_task_team[this_thr->th.th_task_state] =
3962 __kmp_allocate_task_team(this_thr, team);
3963 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d created new task_team %p"
3964 " for team %d at parity=%d\n",
3965 __kmp_gtid_from_thread(this_thr),
3966 team->t.t_task_team[this_thr->th.th_task_state], team->t.t_id,
3967 this_thr->th.th_task_state));
3976 int other_team = 1 - this_thr->th.th_task_state;
3977 KMP_DEBUG_ASSERT(other_team >= 0 && other_team < 2);
3978 if (team->t.t_task_team[other_team] == NULL) {
3979 team->t.t_task_team[other_team] = __kmp_allocate_task_team(this_thr, team);
3980 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d created second new "
3981 "task_team %p for team %d at parity=%d\n",
3982 __kmp_gtid_from_thread(this_thr),
3983 team->t.t_task_team[other_team], team->t.t_id, other_team));
3986 kmp_task_team_t *task_team = team->t.t_task_team[other_team];
3987 __kmp_task_team_init(task_team, team);
3990 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d reset next task_team "
3991 "%p for team %d at parity=%d\n",
3992 __kmp_gtid_from_thread(this_thr),
3993 team->t.t_task_team[other_team], team->t.t_id, other_team));
4000 if (this_thr == __kmp_hidden_helper_main_thread) {
4001 for (
int i = 0; i < 2; ++i) {
4002 kmp_task_team_t *task_team = team->t.t_task_team[i];
4003 if (KMP_TASKING_ENABLED(task_team)) {
4006 __kmp_enable_tasking(task_team, this_thr);
4007 for (
int j = 0; j < task_team->tt.tt_nproc; ++j) {
4008 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[j];
4009 if (thread_data->td.td_deque == NULL) {
4010 __kmp_alloc_task_deque(__kmp_hidden_helper_threads[j], thread_data);
4020void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team) {
4021 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
4022 KMP_DEBUG_ASSERT(team != this_thr->th.th_serial_team);
4023 KMP_DEBUG_ASSERT(team != this_thr->th.th_root->r.r_root_team);
4027 this_thr->th.th_task_state = (kmp_uint8)(1 - this_thr->th.th_task_state);
4031 TCW_PTR(this_thr->th.th_task_team,
4032 team->t.t_task_team[this_thr->th.th_task_state]);
4034 (
"__kmp_task_team_sync: Thread T#%d task team switched to task_team "
4035 "%p from Team #%d (parity=%d)\n",
4036 __kmp_gtid_from_thread(this_thr), this_thr->th.th_task_team,
4037 team->t.t_id, this_thr->th.th_task_state));
4046void __kmp_task_team_wait(
4047 kmp_info_t *this_thr,
4048 kmp_team_t *team USE_ITT_BUILD_ARG(
void *itt_sync_obj),
int wait) {
4049 kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state];
4051 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
4052 KMP_DEBUG_ASSERT(task_team == this_thr->th.th_task_team);
4054 if ((task_team != NULL) && KMP_TASKING_ENABLED(task_team)) {
4056 KA_TRACE(20, (
"__kmp_task_team_wait: Primary T#%d waiting for all tasks "
4057 "(for unfinished_threads to reach 0) on task_team = %p\n",
4058 __kmp_gtid_from_thread(this_thr), task_team));
4062 kmp_flag_32<false, false> flag(
4063 RCAST(std::atomic<kmp_uint32> *,
4064 &task_team->tt.tt_unfinished_threads),
4066 flag.wait(this_thr, TRUE USE_ITT_BUILD_ARG(itt_sync_obj));
4072 (
"__kmp_task_team_wait: Primary T#%d deactivating task_team %p: "
4073 "setting active to false, setting local and team's pointer to NULL\n",
4074 __kmp_gtid_from_thread(this_thr), task_team));
4075 TCW_SYNC_4(task_team->tt.tt_found_proxy_tasks, FALSE);
4076 TCW_SYNC_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
4077 KMP_CHECK_UPDATE(task_team->tt.tt_untied_task_encountered, 0);
4078 TCW_SYNC_4(task_team->tt.tt_active, FALSE);
4081 TCW_PTR(this_thr->th.th_task_team, NULL);
4090void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread,
int gtid) {
4091 std::atomic<kmp_uint32> *spin = RCAST(
4092 std::atomic<kmp_uint32> *,
4093 &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads);
4095 KMP_DEBUG_ASSERT(__kmp_tasking_mode == tskm_extra_barrier);
4098 KMP_FSYNC_SPIN_INIT(spin, NULL);
4100 kmp_flag_32<false, false> spin_flag(spin, 0U);
4101 while (!spin_flag.execute_tasks(thread, gtid, TRUE,
4102 &flag USE_ITT_BUILD_ARG(NULL), 0)) {
4105 KMP_FSYNC_SPIN_PREPARE(RCAST(
void *, spin));
4108 if (TCR_4(__kmp_global.g.g_done)) {
4109 if (__kmp_global.g.g_abort)
4110 __kmp_abort_thread();
4116 KMP_FSYNC_SPIN_ACQUIRED(RCAST(
void *, spin));
4125static bool __kmp_give_task(kmp_info_t *thread, kmp_int32 tid, kmp_task_t *task,
4127 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4128 kmp_task_team_t *task_team = taskdata->td_task_team;
4130 KA_TRACE(20, (
"__kmp_give_task: trying to give task %p to thread %d.\n",
4134 KMP_DEBUG_ASSERT(task_team != NULL);
4136 bool result =
false;
4137 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
4139 if (thread_data->td.td_deque == NULL) {
4143 (
"__kmp_give_task: thread %d has no queue while giving task %p.\n",
4148 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4149 TASK_DEQUE_SIZE(thread_data->td)) {
4152 (
"__kmp_give_task: queue is full while giving task %p to thread %d.\n",
4157 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
4160 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
4161 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4162 TASK_DEQUE_SIZE(thread_data->td)) {
4164 __kmp_realloc_task_deque(thread, thread_data);
4169 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
4171 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4172 TASK_DEQUE_SIZE(thread_data->td)) {
4173 KA_TRACE(30, (
"__kmp_give_task: queue is full while giving task %p to "
4179 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
4180 goto release_and_exit;
4182 __kmp_realloc_task_deque(thread, thread_data);
4188 thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
4190 thread_data->td.td_deque_tail =
4191 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
4192 TCW_4(thread_data->td.td_deque_ntasks,
4193 TCR_4(thread_data->td.td_deque_ntasks) + 1);
4196 KA_TRACE(30, (
"__kmp_give_task: successfully gave task %p to thread %d.\n",
4200 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
4205#define PROXY_TASK_FLAG 0x40000000
4222static void __kmp_first_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
4223 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
4224 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4225 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
4226 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
4228 taskdata->td_flags.complete = 1;
4230 taskdata->td_flags.onced = 1;
4233 if (taskdata->td_taskgroup)
4234 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
4238 KMP_ATOMIC_OR(&taskdata->td_incomplete_child_tasks, PROXY_TASK_FLAG);
4241static void __kmp_second_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
4243 kmp_int32 children = 0;
4247 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks);
4248 KMP_DEBUG_ASSERT(children >= 0);
4251 KMP_ATOMIC_AND(&taskdata->td_incomplete_child_tasks, ~PROXY_TASK_FLAG);
4254static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask) {
4255 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4256 kmp_info_t *thread = __kmp_threads[gtid];
4258 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4259 KMP_DEBUG_ASSERT(taskdata->td_flags.complete ==
4264 while ((KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) &
4265 PROXY_TASK_FLAG) > 0)
4268 __kmp_release_deps(gtid, taskdata);
4269 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
4281 KMP_DEBUG_ASSERT(ptask != NULL);
4282 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4284 10, (
"__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n",
4286 __kmp_assert_valid_gtid(gtid);
4287 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4289 __kmp_first_top_half_finish_proxy(taskdata);
4290 __kmp_second_top_half_finish_proxy(taskdata);
4291 __kmp_bottom_half_finish_proxy(gtid, ptask);
4294 (
"__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n",
4298void __kmpc_give_task(kmp_task_t *ptask, kmp_int32 start = 0) {
4299 KMP_DEBUG_ASSERT(ptask != NULL);
4300 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4304 kmp_team_t *team = taskdata->td_team;
4305 kmp_int32 nthreads = team->t.t_nproc;
4310 kmp_int32 start_k = start % nthreads;
4312 kmp_int32 k = start_k;
4316 thread = team->t.t_threads[k];
4317 k = (k + 1) % nthreads;
4323 }
while (!__kmp_give_task(thread, k, ptask, pass));
4325 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME && __kmp_wpolicy_passive) {
4327 for (
int i = 0; i < nthreads; ++i) {
4328 thread = team->t.t_threads[i];
4329 if (thread->th.th_sleep_loc != NULL) {
4330 __kmp_null_resume_wrapper(thread);
4345 KMP_DEBUG_ASSERT(ptask != NULL);
4346 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4350 (
"__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n",
4353 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4355 __kmp_first_top_half_finish_proxy(taskdata);
4357 __kmpc_give_task(ptask);
4359 __kmp_second_top_half_finish_proxy(taskdata);
4363 (
"__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n",
4367kmp_event_t *__kmpc_task_allow_completion_event(
ident_t *loc_ref,
int gtid,
4369 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(task);
4370 if (td->td_allow_completion_event.type == KMP_EVENT_UNINITIALIZED) {
4371 td->td_allow_completion_event.type = KMP_EVENT_ALLOW_COMPLETION;
4372 td->td_allow_completion_event.ed.task = task;
4373 __kmp_init_tas_lock(&td->td_allow_completion_event.lock);
4375 return &td->td_allow_completion_event;
4378void __kmp_fulfill_event(kmp_event_t *event) {
4379 if (event->type == KMP_EVENT_ALLOW_COMPLETION) {
4380 kmp_task_t *ptask = event->ed.task;
4381 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4382 bool detached =
false;
4383 int gtid = __kmp_get_gtid();
4388 __kmp_acquire_tas_lock(&event->lock, gtid);
4389 if (taskdata->td_flags.proxy == TASK_PROXY) {
4395 if (UNLIKELY(ompt_enabled.enabled))
4396 __ompt_task_finish(ptask, NULL, ompt_task_early_fulfill);
4399 event->type = KMP_EVENT_UNINITIALIZED;
4400 __kmp_release_tas_lock(&event->lock, gtid);
4406 if (UNLIKELY(ompt_enabled.enabled))
4407 __ompt_task_finish(ptask, NULL, ompt_task_late_fulfill);
4411 kmp_team_t *team = taskdata->td_team;
4412 kmp_info_t *thread = __kmp_get_thread();
4413 if (thread->th.th_team == team) {
4433kmp_task_t *__kmp_task_dup_alloc(kmp_info_t *thread, kmp_task_t *task_src
4435 ,
int taskloop_recur
4439 kmp_taskdata_t *taskdata;
4440 kmp_taskdata_t *taskdata_src = KMP_TASK_TO_TASKDATA(task_src);
4441 kmp_taskdata_t *parent_task = taskdata_src->td_parent;
4442 size_t shareds_offset;
4445 KA_TRACE(10, (
"__kmp_task_dup_alloc(enter): Th %p, source task %p\n", thread,
4447 KMP_DEBUG_ASSERT(taskdata_src->td_flags.proxy ==
4449 KMP_DEBUG_ASSERT(taskdata_src->td_flags.tasktype == TASK_EXPLICIT);
4450 task_size = taskdata_src->td_size_alloc;
4453 KA_TRACE(30, (
"__kmp_task_dup_alloc: Th %p, malloc size %ld\n", thread,
4456 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, task_size);
4458 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, task_size);
4460 KMP_MEMCPY(taskdata, taskdata_src, task_size);
4462 task = KMP_TASKDATA_TO_TASK(taskdata);
4466 if (taskdata->is_taskgraph && !taskloop_recur &&
4467 __kmp_tdg_is_recording(taskdata_src->tdg->tdg_status))
4468 taskdata->td_tdg_task_id = KMP_ATOMIC_INC(&__kmp_tdg_task_id);
4470 taskdata->td_task_id = KMP_GEN_TASK_ID();
4471 if (task->shareds != NULL) {
4472 shareds_offset = (
char *)task_src->shareds - (
char *)taskdata_src;
4473 task->shareds = &((
char *)taskdata)[shareds_offset];
4474 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
4477 taskdata->td_alloc_thread = thread;
4478 taskdata->td_parent = parent_task;
4480 taskdata->td_taskgroup = parent_task->td_taskgroup;
4483 if (taskdata->td_flags.tiedness == TASK_TIED)
4484 taskdata->td_last_tied = taskdata;
4488 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
4489 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
4490 if (parent_task->td_taskgroup)
4491 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
4494 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT)
4495 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
4499 (
"__kmp_task_dup_alloc(exit): Th %p, created task %p, parent=%p\n",
4500 thread, taskdata, taskdata->td_parent));
4502 if (UNLIKELY(ompt_enabled.enabled))
4503 __ompt_task_init(taskdata, thread->th.th_info.ds.ds_gtid);
4512typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32);
4514KMP_BUILD_ASSERT(
sizeof(
long) == 4 ||
sizeof(
long) == 8);
4519class kmp_taskloop_bounds_t {
4521 const kmp_taskdata_t *taskdata;
4522 size_t lower_offset;
4523 size_t upper_offset;
4526 kmp_taskloop_bounds_t(kmp_task_t *_task, kmp_uint64 *lb, kmp_uint64 *ub)
4527 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(task)),
4528 lower_offset((char *)lb - (char *)task),
4529 upper_offset((char *)ub - (char *)task) {
4530 KMP_DEBUG_ASSERT((
char *)lb > (
char *)_task);
4531 KMP_DEBUG_ASSERT((
char *)ub > (
char *)_task);
4533 kmp_taskloop_bounds_t(kmp_task_t *_task,
const kmp_taskloop_bounds_t &bounds)
4534 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(_task)),
4535 lower_offset(bounds.lower_offset), upper_offset(bounds.upper_offset) {}
4536 size_t get_lower_offset()
const {
return lower_offset; }
4537 size_t get_upper_offset()
const {
return upper_offset; }
4538 kmp_uint64 get_lb()
const {
4540#if defined(KMP_GOMP_COMPAT)
4542 if (!taskdata->td_flags.native) {
4543 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4546 if (taskdata->td_size_loop_bounds == 4) {
4547 kmp_int32 *lb = RCAST(kmp_int32 *, task->shareds);
4548 retval = (kmp_int64)*lb;
4550 kmp_int64 *lb = RCAST(kmp_int64 *, task->shareds);
4551 retval = (kmp_int64)*lb;
4556 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4560 kmp_uint64 get_ub()
const {
4562#if defined(KMP_GOMP_COMPAT)
4564 if (!taskdata->td_flags.native) {
4565 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4568 if (taskdata->td_size_loop_bounds == 4) {
4569 kmp_int32 *ub = RCAST(kmp_int32 *, task->shareds) + 1;
4570 retval = (kmp_int64)*ub;
4572 kmp_int64 *ub = RCAST(kmp_int64 *, task->shareds) + 1;
4573 retval = (kmp_int64)*ub;
4577 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4581 void set_lb(kmp_uint64 lb) {
4582#if defined(KMP_GOMP_COMPAT)
4584 if (!taskdata->td_flags.native) {
4585 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4588 if (taskdata->td_size_loop_bounds == 4) {
4589 kmp_uint32 *lower = RCAST(kmp_uint32 *, task->shareds);
4590 *lower = (kmp_uint32)lb;
4592 kmp_uint64 *lower = RCAST(kmp_uint64 *, task->shareds);
4593 *lower = (kmp_uint64)lb;
4597 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4600 void set_ub(kmp_uint64 ub) {
4601#if defined(KMP_GOMP_COMPAT)
4603 if (!taskdata->td_flags.native) {
4604 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4607 if (taskdata->td_size_loop_bounds == 4) {
4608 kmp_uint32 *upper = RCAST(kmp_uint32 *, task->shareds) + 1;
4609 *upper = (kmp_uint32)ub;
4611 kmp_uint64 *upper = RCAST(kmp_uint64 *, task->shareds) + 1;
4612 *upper = (kmp_uint64)ub;
4616 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4637void __kmp_taskloop_linear(
ident_t *loc,
int gtid, kmp_task_t *task,
4638 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4639 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
4640 kmp_uint64 grainsize, kmp_uint64 extras,
4641 kmp_int64 last_chunk, kmp_uint64 tc,
4647 KMP_TIME_PARTITIONED_BLOCK(OMP_taskloop_scheduling);
4648 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
4650 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4651 kmp_uint64 lower = task_bounds.get_lb();
4652 kmp_uint64 upper = task_bounds.get_ub();
4654 kmp_info_t *thread = __kmp_threads[gtid];
4655 kmp_taskdata_t *current_task = thread->th.th_current_task;
4656 kmp_task_t *next_task;
4657 kmp_int32 lastpriv = 0;
4659 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4660 (last_chunk < 0 ? last_chunk : extras));
4661 KMP_DEBUG_ASSERT(num_tasks > extras);
4662 KMP_DEBUG_ASSERT(num_tasks > 0);
4663 KA_TRACE(20, (
"__kmp_taskloop_linear: T#%d: %lld tasks, grainsize %lld, "
4664 "extras %lld, last_chunk %lld, i=%lld,%lld(%d)%lld, dup %p\n",
4665 gtid, num_tasks, grainsize, extras, last_chunk, lower, upper,
4666 ub_glob, st, task_dup));
4669 for (i = 0; i < num_tasks; ++i) {
4670 kmp_uint64 chunk_minus_1;
4672 chunk_minus_1 = grainsize - 1;
4674 chunk_minus_1 = grainsize;
4677 upper = lower + st * chunk_minus_1;
4681 if (i == num_tasks - 1) {
4684 KMP_DEBUG_ASSERT(upper == *ub);
4685 if (upper == ub_glob)
4687 }
else if (st > 0) {
4688 KMP_DEBUG_ASSERT((kmp_uint64)st > *ub - upper);
4689 if ((kmp_uint64)st > ub_glob - upper)
4692 KMP_DEBUG_ASSERT(upper + st < *ub);
4693 if (upper - ub_glob < (kmp_uint64)(-st))
4699 next_task = __kmp_task_dup_alloc(thread, task, 0);
4701 next_task = __kmp_task_dup_alloc(thread, task);
4704 kmp_taskdata_t *next_taskdata = KMP_TASK_TO_TASKDATA(next_task);
4705 kmp_taskloop_bounds_t next_task_bounds =
4706 kmp_taskloop_bounds_t(next_task, task_bounds);
4709 next_task_bounds.set_lb(lower);
4710 if (next_taskdata->td_flags.native) {
4711 next_task_bounds.set_ub(upper + (st > 0 ? 1 : -1));
4713 next_task_bounds.set_ub(upper);
4715 if (ptask_dup != NULL)
4717 ptask_dup(next_task, task, lastpriv);
4719 (
"__kmp_taskloop_linear: T#%d; task #%llu: task %p: lower %lld, "
4720 "upper %lld stride %lld, (offsets %p %p)\n",
4721 gtid, i, next_task, lower, upper, st,
4722 next_task_bounds.get_lower_offset(),
4723 next_task_bounds.get_upper_offset()));
4725 __kmp_omp_taskloop_task(NULL, gtid, next_task,
4728 if (ompt_enabled.ompt_callback_dispatch) {
4729 OMPT_GET_DISPATCH_CHUNK(next_taskdata->ompt_task_info.dispatch_chunk,
4734 __kmp_omp_task(gtid, next_task,
true);
4739 __kmp_task_start(gtid, task, current_task);
4741 __kmp_task_finish<false>(gtid, task, current_task);
4746typedef struct __taskloop_params {
4753 kmp_uint64 num_tasks;
4754 kmp_uint64 grainsize;
4756 kmp_int64 last_chunk;
4758 kmp_uint64 num_t_min;
4762} __taskloop_params_t;
4764void __kmp_taskloop_recur(
ident_t *,
int, kmp_task_t *, kmp_uint64 *,
4765 kmp_uint64 *, kmp_int64, kmp_uint64, kmp_uint64,
4766 kmp_uint64, kmp_uint64, kmp_int64, kmp_uint64,
4774int __kmp_taskloop_task(
int gtid,
void *ptask) {
4775 __taskloop_params_t *p =
4776 (__taskloop_params_t *)((kmp_task_t *)ptask)->shareds;
4777 kmp_task_t *task = p->task;
4778 kmp_uint64 *lb = p->lb;
4779 kmp_uint64 *ub = p->ub;
4780 void *task_dup = p->task_dup;
4782 kmp_int64 st = p->st;
4783 kmp_uint64 ub_glob = p->ub_glob;
4784 kmp_uint64 num_tasks = p->num_tasks;
4785 kmp_uint64 grainsize = p->grainsize;
4786 kmp_uint64 extras = p->extras;
4787 kmp_int64 last_chunk = p->last_chunk;
4788 kmp_uint64 tc = p->tc;
4789 kmp_uint64 num_t_min = p->num_t_min;
4791 void *codeptr_ra = p->codeptr_ra;
4794 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4795 KMP_DEBUG_ASSERT(task != NULL);
4797 (
"__kmp_taskloop_task: T#%d, task %p: %lld tasks, grainsize"
4798 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
4799 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
4802 KMP_DEBUG_ASSERT(num_tasks * 2 + 1 > num_t_min);
4803 if (num_tasks > num_t_min)
4804 __kmp_taskloop_recur(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
4805 grainsize, extras, last_chunk, tc, num_t_min,
4811 __kmp_taskloop_linear(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
4812 grainsize, extras, last_chunk, tc,
4818 KA_TRACE(40, (
"__kmp_taskloop_task(exit): T#%d\n", gtid));
4840void __kmp_taskloop_recur(
ident_t *loc,
int gtid, kmp_task_t *task,
4841 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4842 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
4843 kmp_uint64 grainsize, kmp_uint64 extras,
4844 kmp_int64 last_chunk, kmp_uint64 tc,
4845 kmp_uint64 num_t_min,
4850 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4851 KMP_DEBUG_ASSERT(task != NULL);
4852 KMP_DEBUG_ASSERT(num_tasks > num_t_min);
4854 (
"__kmp_taskloop_recur: T#%d, task %p: %lld tasks, grainsize"
4855 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
4856 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
4858 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
4859 kmp_uint64 lower = *lb;
4860 kmp_info_t *thread = __kmp_threads[gtid];
4862 kmp_task_t *next_task;
4863 size_t lower_offset =
4864 (
char *)lb - (
char *)task;
4865 size_t upper_offset =
4866 (
char *)ub - (
char *)task;
4868 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4869 (last_chunk < 0 ? last_chunk : extras));
4870 KMP_DEBUG_ASSERT(num_tasks > extras);
4871 KMP_DEBUG_ASSERT(num_tasks > 0);
4874 kmp_uint64 lb1, ub0, tc0, tc1, ext0, ext1;
4875 kmp_int64 last_chunk0 = 0, last_chunk1 = 0;
4876 kmp_uint64 gr_size0 = grainsize;
4877 kmp_uint64 n_tsk0 = num_tasks >> 1;
4878 kmp_uint64 n_tsk1 = num_tasks - n_tsk0;
4879 if (last_chunk < 0) {
4881 last_chunk1 = last_chunk;
4882 tc0 = grainsize * n_tsk0;
4884 }
else if (n_tsk0 <= extras) {
4887 ext1 = extras - n_tsk0;
4888 tc0 = gr_size0 * n_tsk0;
4893 tc1 = grainsize * n_tsk1;
4896 ub0 = lower + st * (tc0 - 1);
4901 next_task = __kmp_task_dup_alloc(thread, task,
4904 next_task = __kmp_task_dup_alloc(thread, task);
4907 *(kmp_uint64 *)((
char *)next_task + lower_offset) = lb1;
4908 if (ptask_dup != NULL)
4909 ptask_dup(next_task, task, 0);
4914 kmp_taskdata_t *current_task = thread->th.th_current_task;
4915 thread->th.th_current_task = taskdata->td_parent;
4916 kmp_task_t *new_task =
4917 __kmpc_omp_task_alloc(loc, gtid, 1, 3 *
sizeof(
void *),
4918 sizeof(__taskloop_params_t), &__kmp_taskloop_task);
4920 thread->th.th_current_task = current_task;
4921 __taskloop_params_t *p = (__taskloop_params_t *)new_task->shareds;
4922 p->task = next_task;
4923 p->lb = (kmp_uint64 *)((
char *)next_task + lower_offset);
4924 p->ub = (kmp_uint64 *)((
char *)next_task + upper_offset);
4925 p->task_dup = task_dup;
4927 p->ub_glob = ub_glob;
4928 p->num_tasks = n_tsk1;
4929 p->grainsize = grainsize;
4931 p->last_chunk = last_chunk1;
4933 p->num_t_min = num_t_min;
4935 p->codeptr_ra = codeptr_ra;
4939 kmp_taskdata_t *new_task_data = KMP_TASK_TO_TASKDATA(new_task);
4940 new_task_data->tdg = taskdata->tdg;
4941 new_task_data->is_taskgraph = 0;
4946 __kmp_omp_taskloop_task(NULL, gtid, new_task, codeptr_ra);
4948 __kmp_omp_task(gtid, new_task,
true);
4952 if (n_tsk0 > num_t_min)
4953 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0, gr_size0,
4954 ext0, last_chunk0, tc0, num_t_min,
4960 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0,
4961 gr_size0, ext0, last_chunk0, tc0,
4967 KA_TRACE(40, (
"__kmp_taskloop_recur(exit): T#%d\n", gtid));
4970static void __kmp_taskloop(
ident_t *loc,
int gtid, kmp_task_t *task,
int if_val,
4971 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4972 int nogroup,
int sched, kmp_uint64 grainsize,
4973 int modifier,
void *task_dup) {
4974 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4975 KMP_DEBUG_ASSERT(task != NULL);
4977#if OMPT_SUPPORT && OMPT_OPTIONAL
4978 OMPT_STORE_RETURN_ADDRESS(gtid);
4980 __kmpc_taskgroup(loc, gtid);
4984 KMP_ATOMIC_DEC(&__kmp_tdg_task_id);
4988 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4991 kmp_uint64 lower = task_bounds.get_lb();
4992 kmp_uint64 upper = task_bounds.get_ub();
4993 kmp_uint64 ub_glob = upper;
4994 kmp_uint64 num_tasks = 0, extras = 0;
4995 kmp_int64 last_chunk =
4997 kmp_uint64 num_tasks_min = __kmp_taskloop_min_tasks;
4998 kmp_info_t *thread = __kmp_threads[gtid];
4999 kmp_taskdata_t *current_task = thread->th.th_current_task;
5001 KA_TRACE(20, (
"__kmp_taskloop: T#%d, task %p, lb %lld, ub %lld, st %lld, "
5002 "grain %llu(%d, %d), dup %p\n",
5003 gtid, taskdata, lower, upper, st, grainsize, sched, modifier,
5008 tc = upper - lower + 1;
5009 }
else if (st < 0) {
5010 tc = (lower - upper) / (-st) + 1;
5012 tc = (upper - lower) / st + 1;
5015 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d zero-trip loop\n", gtid));
5017 __kmp_task_start(gtid, task, current_task);
5019 __kmp_task_finish<false>(gtid, task, current_task);
5023#if OMPT_SUPPORT && OMPT_OPTIONAL
5024 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
5025 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
5026 if (ompt_enabled.ompt_callback_work) {
5027 ompt_callbacks.ompt_callback(ompt_callback_work)(
5028 ompt_work_taskloop, ompt_scope_begin, &(team_info->parallel_data),
5029 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
5033 if (num_tasks_min == 0)
5036 KMP_MIN(thread->th.th_team_nproc * 10, INITIAL_TASK_DEQUE_SIZE);
5042 grainsize = thread->th.th_team_nproc *
static_cast<kmp_uint64
>(10);
5045 if (grainsize > tc) {
5050 num_tasks = grainsize;
5051 grainsize = tc / num_tasks;
5052 extras = tc % num_tasks;
5056 if (grainsize > tc) {
5062 num_tasks = (tc + grainsize - 1) / grainsize;
5063 last_chunk = tc - (num_tasks * grainsize);
5066 num_tasks = tc / grainsize;
5068 grainsize = tc / num_tasks;
5069 extras = tc % num_tasks;
5074 KMP_ASSERT2(0,
"unknown scheduling of taskloop");
5077 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
5078 (last_chunk < 0 ? last_chunk : extras));
5079 KMP_DEBUG_ASSERT(num_tasks > extras);
5080 KMP_DEBUG_ASSERT(num_tasks > 0);
5086 taskdata->td_flags.task_serial = 1;
5087 taskdata->td_flags.tiedness = TASK_TIED;
5089 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5090 grainsize, extras, last_chunk, tc,
5092 OMPT_GET_RETURN_ADDRESS(0),
5097 }
else if (num_tasks > num_tasks_min && !taskdata->td_flags.native) {
5098 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go recursive: tc %llu, #tasks %llu"
5099 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
5100 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
5102 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5103 grainsize, extras, last_chunk, tc, num_tasks_min,
5105 OMPT_GET_RETURN_ADDRESS(0),
5109 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go linear: tc %llu, #tasks %llu"
5110 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
5111 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
5113 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5114 grainsize, extras, last_chunk, tc,
5116 OMPT_GET_RETURN_ADDRESS(0),
5121#if OMPT_SUPPORT && OMPT_OPTIONAL
5122 if (ompt_enabled.ompt_callback_work) {
5123 ompt_callbacks.ompt_callback(ompt_callback_work)(
5124 ompt_work_taskloop, ompt_scope_end, &(team_info->parallel_data),
5125 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
5130#if OMPT_SUPPORT && OMPT_OPTIONAL
5131 OMPT_STORE_RETURN_ADDRESS(gtid);
5133 __kmpc_end_taskgroup(loc, gtid);
5135 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d\n", gtid));
5155 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
int nogroup,
5156 int sched, kmp_uint64 grainsize,
void *task_dup) {
5157 __kmp_assert_valid_gtid(gtid);
5158 KA_TRACE(20, (
"__kmpc_taskloop(enter): T#%d\n", gtid));
5159 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
5161 KA_TRACE(20, (
"__kmpc_taskloop(exit): T#%d\n", gtid));
5182 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
5183 int nogroup,
int sched, kmp_uint64 grainsize,
5184 int modifier,
void *task_dup) {
5185 __kmp_assert_valid_gtid(gtid);
5186 KA_TRACE(20, (
"__kmpc_taskloop_5(enter): T#%d\n", gtid));
5187 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
5188 modifier, task_dup);
5189 KA_TRACE(20, (
"__kmpc_taskloop_5(exit): T#%d\n", gtid));
5201 if (gtid == KMP_GTID_DNE)
5204 kmp_info_t *thread = __kmp_thread_from_gtid(gtid);
5205 kmp_taskdata_t *taskdata = thread->th.th_current_task;
5210 return &taskdata->td_target_data.async_handle;
5222 if (gtid == KMP_GTID_DNE)
5225 kmp_info_t *thread = __kmp_thread_from_gtid(gtid);
5226 kmp_taskdata_t *taskdata = thread->th.th_current_task;
5231 return taskdata->td_task_team != NULL;
5239static kmp_tdg_info_t *__kmp_find_tdg(kmp_int32 tdg_id) {
5240 kmp_tdg_info_t *res =
nullptr;
5241 if (__kmp_max_tdgs == 0)
5244 if (__kmp_global_tdgs == NULL)
5245 __kmp_global_tdgs = (kmp_tdg_info_t **)__kmp_allocate(
5246 sizeof(kmp_tdg_info_t *) * __kmp_max_tdgs);
5248 if ((__kmp_global_tdgs[tdg_id]) &&
5249 (__kmp_global_tdgs[tdg_id]->tdg_status != KMP_TDG_NONE))
5250 res = __kmp_global_tdgs[tdg_id];
5257void __kmp_print_tdg_dot(kmp_tdg_info_t *tdg, kmp_int32 gtid) {
5258 kmp_int32 tdg_id = tdg->tdg_id;
5259 KA_TRACE(10, (
"__kmp_print_tdg_dot(enter): T#%d tdg_id=%d \n", gtid, tdg_id));
5262 sprintf(file_name,
"tdg_%d.dot", tdg_id);
5265 kmp_int32 num_tasks = KMP_ATOMIC_LD_RLX(&tdg->num_tasks);
5269 " subgraph cluster {\n"
5272 for (kmp_int32 i = 0; i < num_tasks; i++) {
5273 fprintf(tdg_file,
" %d[style=bold]\n", i);
5275 fprintf(tdg_file,
" }\n");
5276 for (kmp_int32 i = 0; i < num_tasks; i++) {
5277 kmp_int32 nsuccessors = tdg->record_map[i].nsuccessors;
5278 kmp_int32 *successors = tdg->record_map[i].successors;
5279 if (nsuccessors > 0) {
5280 for (kmp_int32 j = 0; j < nsuccessors; j++)
5281 fprintf(tdg_file,
" %d -> %d \n", i, successors[j]);
5284 fprintf(tdg_file,
"}");
5285 KA_TRACE(10, (
"__kmp_print_tdg_dot(exit): T#%d tdg_id=%d \n", gtid, tdg_id));
5292void __kmp_exec_tdg(kmp_int32 gtid, kmp_tdg_info_t *tdg) {
5293 KMP_DEBUG_ASSERT(tdg->tdg_status == KMP_TDG_READY);
5294 KA_TRACE(10, (
"__kmp_exec_tdg(enter): T#%d tdg_id=%d num_roots=%d\n", gtid,
5295 tdg->tdg_id, tdg->num_roots));
5296 kmp_node_info_t *this_record_map = tdg->record_map;
5297 kmp_int32 *this_root_tasks = tdg->root_tasks;
5298 kmp_int32 this_num_roots = tdg->num_roots;
5299 kmp_int32 this_num_tasks = KMP_ATOMIC_LD_RLX(&tdg->num_tasks);
5301 kmp_info_t *thread = __kmp_threads[gtid];
5302 kmp_taskdata_t *parent_task = thread->th.th_current_task;
5304 if (tdg->rec_taskred_data) {
5308 for (kmp_int32 j = 0; j < this_num_tasks; j++) {
5309 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(this_record_map[j].task);
5311 td->td_parent = parent_task;
5312 this_record_map[j].parent_task = parent_task;
5314 kmp_taskgroup_t *parent_taskgroup =
5315 this_record_map[j].parent_task->td_taskgroup;
5317 KMP_ATOMIC_ST_RLX(&this_record_map[j].npredecessors_counter,
5318 this_record_map[j].npredecessors);
5319 KMP_ATOMIC_INC(&this_record_map[j].parent_task->td_incomplete_child_tasks);
5321 if (parent_taskgroup) {
5322 KMP_ATOMIC_INC(&parent_taskgroup->count);
5324 td->td_taskgroup = parent_taskgroup;
5325 }
else if (td->td_taskgroup !=
nullptr) {
5327 td->td_taskgroup =
nullptr;
5329 if (this_record_map[j].parent_task->td_flags.tasktype == TASK_EXPLICIT)
5330 KMP_ATOMIC_INC(&this_record_map[j].parent_task->td_allocated_child_tasks);
5333 for (kmp_int32 j = 0; j < this_num_roots; ++j) {
5334 __kmp_omp_task(gtid, this_record_map[this_root_tasks[j]].task,
true);
5336 KA_TRACE(10, (
"__kmp_exec_tdg(exit): T#%d tdg_id=%d num_roots=%d\n", gtid,
5337 tdg->tdg_id, tdg->num_roots));
5345static inline void __kmp_start_record(kmp_int32 gtid,
5346 kmp_taskgraph_flags_t *flags,
5348 kmp_tdg_info_t *tdg =
5349 (kmp_tdg_info_t *)__kmp_allocate(
sizeof(kmp_tdg_info_t));
5350 __kmp_global_tdgs[__kmp_curr_tdg_idx] = tdg;
5352 tdg->tdg_id = tdg_id;
5353 tdg->map_size = INIT_MAPSIZE;
5354 tdg->num_roots = -1;
5355 tdg->root_tasks =
nullptr;
5356 tdg->tdg_status = KMP_TDG_RECORDING;
5357 tdg->rec_num_taskred = 0;
5358 tdg->rec_taskred_data =
nullptr;
5359 KMP_ATOMIC_ST_RLX(&tdg->num_tasks, 0);
5362 kmp_node_info_t *this_record_map =
5363 (kmp_node_info_t *)__kmp_allocate(INIT_MAPSIZE *
sizeof(kmp_node_info_t));
5364 for (kmp_int32 i = 0; i < INIT_MAPSIZE; i++) {
5365 kmp_int32 *successorsList =
5366 (kmp_int32 *)__kmp_allocate(__kmp_successors_size *
sizeof(kmp_int32));
5367 this_record_map[i].task =
nullptr;
5368 this_record_map[i].successors = successorsList;
5369 this_record_map[i].nsuccessors = 0;
5370 this_record_map[i].npredecessors = 0;
5371 this_record_map[i].successors_size = __kmp_successors_size;
5372 KMP_ATOMIC_ST_RLX(&this_record_map[i].npredecessors_counter, 0);
5375 __kmp_global_tdgs[__kmp_curr_tdg_idx]->record_map = this_record_map;
5385kmp_int32 __kmpc_start_record_task(
ident_t *loc_ref, kmp_int32 gtid,
5386 kmp_int32 input_flags, kmp_int32 tdg_id) {
5389 kmp_taskgraph_flags_t *flags = (kmp_taskgraph_flags_t *)&input_flags;
5391 (
"__kmpc_start_record_task(enter): T#%d loc=%p flags=%d tdg_id=%d\n",
5392 gtid, loc_ref, input_flags, tdg_id));
5394 if (__kmp_max_tdgs == 0) {
5397 (
"__kmpc_start_record_task(abandon): T#%d loc=%p flags=%d tdg_id = %d, "
5398 "__kmp_max_tdgs = 0\n",
5399 gtid, loc_ref, input_flags, tdg_id));
5403 __kmpc_taskgroup(loc_ref, gtid);
5404 if (kmp_tdg_info_t *tdg = __kmp_find_tdg(tdg_id)) {
5406 __kmp_exec_tdg(gtid, tdg);
5409 __kmp_curr_tdg_idx = tdg_id;
5410 KMP_DEBUG_ASSERT(__kmp_curr_tdg_idx < __kmp_max_tdgs);
5411 __kmp_start_record(gtid, flags, tdg_id);
5415 KA_TRACE(10, (
"__kmpc_start_record_task(exit): T#%d TDG %d starts to %s\n",
5416 gtid, tdg_id, res ?
"record" :
"execute"));
5423void __kmp_end_record(kmp_int32 gtid, kmp_tdg_info_t *tdg) {
5425 kmp_node_info_t *this_record_map = tdg->record_map;
5426 kmp_int32 this_num_tasks = KMP_ATOMIC_LD_RLX(&tdg->num_tasks);
5427 kmp_int32 *this_root_tasks =
5428 (kmp_int32 *)__kmp_allocate(this_num_tasks *
sizeof(kmp_int32));
5429 kmp_int32 this_map_size = tdg->map_size;
5430 kmp_int32 this_num_roots = 0;
5431 kmp_info_t *thread = __kmp_threads[gtid];
5433 for (kmp_int32 i = 0; i < this_num_tasks; i++) {
5434 if (this_record_map[i].npredecessors == 0) {
5435 this_root_tasks[this_num_roots++] = i;
5440 tdg->map_size = this_map_size;
5441 tdg->num_roots = this_num_roots;
5442 tdg->root_tasks = this_root_tasks;
5443 KMP_DEBUG_ASSERT(tdg->tdg_status == KMP_TDG_RECORDING);
5444 tdg->tdg_status = KMP_TDG_READY;
5446 if (thread->th.th_current_task->td_dephash) {
5447 __kmp_dephash_free(thread, thread->th.th_current_task->td_dephash);
5448 thread->th.th_current_task->td_dephash = NULL;
5452 for (kmp_int32 i = 0; i < this_num_tasks; i++) {
5453 KMP_ATOMIC_ST_RLX(&this_record_map[i].npredecessors_counter,
5454 this_record_map[i].npredecessors);
5456 KMP_ATOMIC_ST_RLX(&__kmp_tdg_task_id, 0);
5459 __kmp_print_tdg_dot(tdg, gtid);
5469void __kmpc_end_record_task(
ident_t *loc_ref, kmp_int32 gtid,
5470 kmp_int32 input_flags, kmp_int32 tdg_id) {
5471 kmp_tdg_info_t *tdg = __kmp_find_tdg(tdg_id);
5473 KA_TRACE(10, (
"__kmpc_end_record_task(enter): T#%d loc=%p finishes recording"
5474 " tdg=%d with flags=%d\n",
5475 gtid, loc_ref, tdg_id, input_flags));
5476 if (__kmp_max_tdgs) {
5478 __kmpc_end_taskgroup(loc_ref, gtid);
5479 if (__kmp_tdg_is_recording(tdg->tdg_status))
5480 __kmp_end_record(gtid, tdg);
5482 KA_TRACE(10, (
"__kmpc_end_record_task(exit): T#%d loc=%p finished recording"
5483 " tdg=%d, its status is now READY\n",
5484 gtid, loc_ref, tdg_id));
struct kmp_taskred_data kmp_taskred_data_t
struct kmp_task_red_input kmp_task_red_input_t
struct kmp_taskred_flags kmp_taskred_flags_t
struct kmp_taskred_input kmp_taskred_input_t
#define KMP_COUNT_BLOCK(name)
Increments specified counter (name).
void * __kmpc_task_reduction_get_th_data(int gtid, void *tskgrp, void *data)
void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int sched, kmp_uint64 grainsize, void *task_dup)
void * __kmpc_task_reduction_modifier_init(ident_t *loc, int gtid, int is_ws, int num, void *data)
void * __kmpc_taskred_modifier_init(ident_t *loc, int gtid, int is_ws, int num, void *data)
bool __kmpc_omp_has_task_team(kmp_int32 gtid)
void __kmpc_proxy_task_completed_ooo(kmp_task_t *ptask)
void __kmpc_task_reduction_modifier_fini(ident_t *loc, int gtid, int is_ws)
kmp_int32 __kmpc_omp_reg_task_with_affinity(ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *new_task, kmp_int32 naffins, kmp_task_affinity_info_t *affin_list)
void __kmpc_taskloop_5(ident_t *loc, int gtid, kmp_task_t *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int sched, kmp_uint64 grainsize, int modifier, void *task_dup)
void * __kmpc_task_reduction_init(int gtid, int num, void *data)
void __kmpc_proxy_task_completed(kmp_int32 gtid, kmp_task_t *ptask)
void * __kmpc_taskred_init(int gtid, int num, void *data)
void ** __kmpc_omp_get_target_async_handle_ptr(kmp_int32 gtid)
kmp_taskred_flags_t flags