16 #include "kmp_stats.h"
17 #include "kmp_wait_release.h"
18 #include "kmp_taskdeps.h"
21 #include "ompt-specific.h"
25 static void __kmp_enable_tasking(kmp_task_team_t *task_team,
26 kmp_info_t *this_thr);
27 static void __kmp_alloc_task_deque(kmp_info_t *thread,
28 kmp_thread_data_t *thread_data);
29 static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
30 kmp_task_team_t *task_team);
31 static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask);
33 #ifdef BUILD_TIED_TASK_STACK
42 static void __kmp_trace_task_stack(kmp_int32 gtid,
43 kmp_thread_data_t *thread_data,
44 int threshold,
char *location) {
45 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
46 kmp_taskdata_t **stack_top = task_stack->ts_top;
47 kmp_int32 entries = task_stack->ts_entries;
48 kmp_taskdata_t *tied_task;
52 (
"__kmp_trace_task_stack(start): location = %s, gtid = %d, entries = %d, "
53 "first_block = %p, stack_top = %p \n",
54 location, gtid, entries, task_stack->ts_first_block, stack_top));
56 KMP_DEBUG_ASSERT(stack_top != NULL);
57 KMP_DEBUG_ASSERT(entries > 0);
59 while (entries != 0) {
60 KMP_DEBUG_ASSERT(stack_top != &task_stack->ts_first_block.sb_block[0]);
62 if (entries & TASK_STACK_INDEX_MASK == 0) {
63 kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(stack_top);
65 stack_block = stack_block->sb_prev;
66 stack_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE];
73 tied_task = *stack_top;
75 KMP_DEBUG_ASSERT(tied_task != NULL);
76 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
79 (
"__kmp_trace_task_stack(%s): gtid=%d, entry=%d, "
80 "stack_top=%p, tied_task=%p\n",
81 location, gtid, entries, stack_top, tied_task));
83 KMP_DEBUG_ASSERT(stack_top == &task_stack->ts_first_block.sb_block[0]);
86 (
"__kmp_trace_task_stack(exit): location = %s, gtid = %d\n",
96 static void __kmp_init_task_stack(kmp_int32 gtid,
97 kmp_thread_data_t *thread_data) {
98 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
99 kmp_stack_block_t *first_block;
102 first_block = &task_stack->ts_first_block;
103 task_stack->ts_top = (kmp_taskdata_t **)first_block;
104 memset((
void *)first_block,
'\0',
105 TASK_STACK_BLOCK_SIZE *
sizeof(kmp_taskdata_t *));
108 task_stack->ts_entries = TASK_STACK_EMPTY;
109 first_block->sb_next = NULL;
110 first_block->sb_prev = NULL;
117 static void __kmp_free_task_stack(kmp_int32 gtid,
118 kmp_thread_data_t *thread_data) {
119 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
120 kmp_stack_block_t *stack_block = &task_stack->ts_first_block;
122 KMP_DEBUG_ASSERT(task_stack->ts_entries == TASK_STACK_EMPTY);
124 while (stack_block != NULL) {
125 kmp_stack_block_t *next_block = (stack_block) ? stack_block->sb_next : NULL;
127 stack_block->sb_next = NULL;
128 stack_block->sb_prev = NULL;
129 if (stack_block != &task_stack->ts_first_block) {
130 __kmp_thread_free(thread,
133 stack_block = next_block;
136 task_stack->ts_entries = 0;
137 task_stack->ts_top = NULL;
146 static void __kmp_push_task_stack(kmp_int32 gtid, kmp_info_t *thread,
147 kmp_taskdata_t *tied_task) {
149 kmp_thread_data_t *thread_data =
150 &thread->th.th_task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
151 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
153 if (tied_task->td_flags.team_serial || tied_task->td_flags.tasking_ser) {
157 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
158 KMP_DEBUG_ASSERT(task_stack->ts_top != NULL);
161 (
"__kmp_push_task_stack(enter): GTID: %d; THREAD: %p; TASK: %p\n",
162 gtid, thread, tied_task));
164 *(task_stack->ts_top) = tied_task;
167 task_stack->ts_top++;
168 task_stack->ts_entries++;
170 if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) {
172 kmp_stack_block_t *stack_block =
173 (kmp_stack_block_t *)(task_stack->ts_top - TASK_STACK_BLOCK_SIZE);
176 if (stack_block->sb_next !=
178 task_stack->ts_top = &stack_block->sb_next->sb_block[0];
180 kmp_stack_block_t *new_block = (kmp_stack_block_t *)__kmp_thread_calloc(
181 thread,
sizeof(kmp_stack_block_t));
183 task_stack->ts_top = &new_block->sb_block[0];
184 stack_block->sb_next = new_block;
185 new_block->sb_prev = stack_block;
186 new_block->sb_next = NULL;
190 (
"__kmp_push_task_stack(): GTID: %d; TASK: %p; Alloc new block: %p\n",
191 gtid, tied_task, new_block));
194 KA_TRACE(20, (
"__kmp_push_task_stack(exit): GTID: %d; TASK: %p\n", gtid,
205 static void __kmp_pop_task_stack(kmp_int32 gtid, kmp_info_t *thread,
206 kmp_taskdata_t *ending_task) {
208 kmp_thread_data_t *thread_data =
209 &thread->th.th_task_team->tt_threads_data[__kmp_tid_from_gtid(gtid)];
210 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
211 kmp_taskdata_t *tied_task;
213 if (ending_task->td_flags.team_serial || ending_task->td_flags.tasking_ser) {
218 KMP_DEBUG_ASSERT(task_stack->ts_top != NULL);
219 KMP_DEBUG_ASSERT(task_stack->ts_entries > 0);
221 KA_TRACE(20, (
"__kmp_pop_task_stack(enter): GTID: %d; THREAD: %p\n", gtid,
225 if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) {
226 kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(task_stack->ts_top);
228 stack_block = stack_block->sb_prev;
229 task_stack->ts_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE];
233 task_stack->ts_top--;
234 task_stack->ts_entries--;
236 tied_task = *(task_stack->ts_top);
238 KMP_DEBUG_ASSERT(tied_task != NULL);
239 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
240 KMP_DEBUG_ASSERT(tied_task == ending_task);
242 KA_TRACE(20, (
"__kmp_pop_task_stack(exit): GTID: %d; TASK: %p\n", gtid,
251 static bool __kmp_task_is_allowed(
int gtid,
const kmp_int32 is_constrained,
252 const kmp_taskdata_t *tasknew,
253 const kmp_taskdata_t *taskcurr) {
254 if (is_constrained && (tasknew->td_flags.tiedness == TASK_TIED)) {
258 kmp_taskdata_t *current = taskcurr->td_last_tied;
259 KMP_DEBUG_ASSERT(current != NULL);
261 if (current->td_flags.tasktype == TASK_EXPLICIT ||
262 current->td_taskwait_thread > 0) {
263 kmp_int32 level = current->td_level;
264 kmp_taskdata_t *parent = tasknew->td_parent;
265 while (parent != current && parent->td_level > level) {
267 parent = parent->td_parent;
268 KMP_DEBUG_ASSERT(parent != NULL);
270 if (parent != current)
275 kmp_depnode_t *node = tasknew->td_depnode;
276 if (UNLIKELY(node && (node->dn.mtx_num_locks > 0))) {
277 for (
int i = 0; i < node->dn.mtx_num_locks; ++i) {
278 KMP_DEBUG_ASSERT(node->dn.mtx_locks[i] != NULL);
279 if (__kmp_test_lock(node->dn.mtx_locks[i], gtid))
282 for (
int j = i - 1; j >= 0; --j)
283 __kmp_release_lock(node->dn.mtx_locks[j], gtid);
287 node->dn.mtx_num_locks = -node->dn.mtx_num_locks;
296 static void __kmp_realloc_task_deque(kmp_info_t *thread,
297 kmp_thread_data_t *thread_data) {
298 kmp_int32 size = TASK_DEQUE_SIZE(thread_data->td);
299 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == size);
300 kmp_int32 new_size = 2 * size;
302 KE_TRACE(10, (
"__kmp_realloc_task_deque: T#%d reallocating deque[from %d to "
303 "%d] for thread_data %p\n",
304 __kmp_gtid_from_thread(thread), size, new_size, thread_data));
306 kmp_taskdata_t **new_deque =
307 (kmp_taskdata_t **)__kmp_allocate(new_size *
sizeof(kmp_taskdata_t *));
310 for (i = thread_data->td.td_deque_head, j = 0; j < size;
311 i = (i + 1) & TASK_DEQUE_MASK(thread_data->td), j++)
312 new_deque[j] = thread_data->td.td_deque[i];
314 __kmp_free(thread_data->td.td_deque);
316 thread_data->td.td_deque_head = 0;
317 thread_data->td.td_deque_tail = size;
318 thread_data->td.td_deque = new_deque;
319 thread_data->td.td_deque_size = new_size;
322 static kmp_task_pri_t *__kmp_alloc_task_pri_list() {
323 kmp_task_pri_t *l = (kmp_task_pri_t *)__kmp_allocate(
sizeof(kmp_task_pri_t));
324 kmp_thread_data_t *thread_data = &l->td;
325 __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
326 thread_data->td.td_deque_last_stolen = -1;
327 KE_TRACE(20, (
"__kmp_alloc_task_pri_list: T#%d allocating deque[%d] "
328 "for thread_data %p\n",
329 __kmp_get_gtid(), INITIAL_TASK_DEQUE_SIZE, thread_data));
330 thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
331 INITIAL_TASK_DEQUE_SIZE *
sizeof(kmp_taskdata_t *));
332 thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
341 static kmp_thread_data_t *
342 __kmp_get_priority_deque_data(kmp_task_team_t *task_team, kmp_int32 pri) {
343 kmp_thread_data_t *thread_data;
344 kmp_task_pri_t *lst = task_team->tt.tt_task_pri_list;
345 if (lst->priority == pri) {
347 thread_data = &lst->td;
348 }
else if (lst->priority < pri) {
351 kmp_task_pri_t *list = __kmp_alloc_task_pri_list();
352 thread_data = &list->td;
353 list->priority = pri;
355 task_team->tt.tt_task_pri_list = list;
357 kmp_task_pri_t *next_queue = lst->next;
358 while (next_queue && next_queue->priority > pri) {
360 next_queue = lst->next;
363 if (next_queue == NULL) {
365 kmp_task_pri_t *list = __kmp_alloc_task_pri_list();
366 thread_data = &list->td;
367 list->priority = pri;
370 }
else if (next_queue->priority == pri) {
372 thread_data = &next_queue->td;
375 kmp_task_pri_t *list = __kmp_alloc_task_pri_list();
376 thread_data = &list->td;
377 list->priority = pri;
378 list->next = next_queue;
386 static kmp_int32 __kmp_push_priority_task(kmp_int32 gtid, kmp_info_t *thread,
387 kmp_taskdata_t *taskdata,
388 kmp_task_team_t *task_team,
390 kmp_thread_data_t *thread_data = NULL;
392 (
"__kmp_push_priority_task: T#%d trying to push task %p, pri %d.\n",
393 gtid, taskdata, pri));
396 kmp_task_pri_t *lst = task_team->tt.tt_task_pri_list;
397 if (UNLIKELY(lst == NULL)) {
398 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
399 if (task_team->tt.tt_task_pri_list == NULL) {
401 kmp_task_pri_t *list = __kmp_alloc_task_pri_list();
402 thread_data = &list->td;
403 list->priority = pri;
405 task_team->tt.tt_task_pri_list = list;
408 thread_data = __kmp_get_priority_deque_data(task_team, pri);
410 __kmp_release_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
412 if (lst->priority == pri) {
414 thread_data = &lst->td;
416 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
417 thread_data = __kmp_get_priority_deque_data(task_team, pri);
418 __kmp_release_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
421 KMP_DEBUG_ASSERT(thread_data);
423 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
425 if (TCR_4(thread_data->td.td_deque_ntasks) >=
426 TASK_DEQUE_SIZE(thread_data->td)) {
427 if (__kmp_enable_task_throttling &&
428 __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
429 thread->th.th_current_task)) {
430 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
431 KA_TRACE(20, (
"__kmp_push_priority_task: T#%d deque is full; returning "
432 "TASK_NOT_PUSHED for task %p\n",
434 return TASK_NOT_PUSHED;
437 __kmp_realloc_task_deque(thread, thread_data);
440 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) <
441 TASK_DEQUE_SIZE(thread_data->td));
443 thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
445 thread_data->td.td_deque_tail =
446 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
447 TCW_4(thread_data->td.td_deque_ntasks,
448 TCR_4(thread_data->td.td_deque_ntasks) + 1);
449 KMP_FSYNC_RELEASING(thread->th.th_current_task);
450 KMP_FSYNC_RELEASING(taskdata);
451 KA_TRACE(20, (
"__kmp_push_priority_task: T#%d returning "
452 "TASK_SUCCESSFULLY_PUSHED: task=%p ntasks=%d head=%u tail=%u\n",
453 gtid, taskdata, thread_data->td.td_deque_ntasks,
454 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
455 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
456 task_team->tt.tt_num_task_pri++;
457 return TASK_SUCCESSFULLY_PUSHED;
461 static kmp_int32 __kmp_push_task(kmp_int32 gtid, kmp_task_t *task) {
462 kmp_info_t *thread = __kmp_threads[gtid];
463 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
468 if (UNLIKELY(taskdata->td_flags.hidden_helper &&
469 !KMP_HIDDEN_HELPER_THREAD(gtid))) {
470 kmp_int32 shadow_gtid = KMP_GTID_TO_SHADOW_GTID(gtid);
471 __kmpc_give_task(task, __kmp_tid_from_gtid(shadow_gtid));
473 __kmp_hidden_helper_worker_thread_signal();
474 return TASK_SUCCESSFULLY_PUSHED;
477 kmp_task_team_t *task_team = thread->th.th_task_team;
478 kmp_int32 tid = __kmp_tid_from_gtid(gtid);
479 kmp_thread_data_t *thread_data;
482 (
"__kmp_push_task: T#%d trying to push task %p.\n", gtid, taskdata));
484 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
487 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
488 KMP_DEBUG_USE_VAR(counter);
491 (
"__kmp_push_task: T#%d untied_count (%d) incremented for task %p\n",
492 gtid, counter, taskdata));
496 if (UNLIKELY(taskdata->td_flags.task_serial)) {
497 KA_TRACE(20, (
"__kmp_push_task: T#%d team serialized; returning "
498 "TASK_NOT_PUSHED for task %p\n",
500 return TASK_NOT_PUSHED;
505 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
506 if (UNLIKELY(!KMP_TASKING_ENABLED(task_team))) {
507 __kmp_enable_tasking(task_team, thread);
509 KMP_DEBUG_ASSERT(TCR_4(task_team->tt.tt_found_tasks) == TRUE);
510 KMP_DEBUG_ASSERT(TCR_PTR(task_team->tt.tt_threads_data) != NULL);
512 if (taskdata->td_flags.priority_specified && task->data2.priority > 0 &&
513 __kmp_max_task_priority > 0) {
514 int pri = KMP_MIN(task->data2.priority, __kmp_max_task_priority);
515 return __kmp_push_priority_task(gtid, thread, taskdata, task_team, pri);
519 thread_data = &task_team->tt.tt_threads_data[tid];
524 if (UNLIKELY(thread_data->td.td_deque == NULL)) {
525 __kmp_alloc_task_deque(thread, thread_data);
530 if (TCR_4(thread_data->td.td_deque_ntasks) >=
531 TASK_DEQUE_SIZE(thread_data->td)) {
532 if (__kmp_enable_task_throttling &&
533 __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
534 thread->th.th_current_task)) {
535 KA_TRACE(20, (
"__kmp_push_task: T#%d deque is full; returning "
536 "TASK_NOT_PUSHED for task %p\n",
538 return TASK_NOT_PUSHED;
540 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
542 if (TCR_4(thread_data->td.td_deque_ntasks) >=
543 TASK_DEQUE_SIZE(thread_data->td)) {
545 __kmp_realloc_task_deque(thread, thread_data);
551 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
553 if (TCR_4(thread_data->td.td_deque_ntasks) >=
554 TASK_DEQUE_SIZE(thread_data->td)) {
555 if (__kmp_enable_task_throttling &&
556 __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
557 thread->th.th_current_task)) {
558 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
559 KA_TRACE(20, (
"__kmp_push_task: T#%d deque is full on 2nd check; "
560 "returning TASK_NOT_PUSHED for task %p\n",
562 return TASK_NOT_PUSHED;
565 __kmp_realloc_task_deque(thread, thread_data);
570 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) <
571 TASK_DEQUE_SIZE(thread_data->td));
573 thread_data->td.td_deque[thread_data->td.td_deque_tail] =
576 thread_data->td.td_deque_tail =
577 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
578 TCW_4(thread_data->td.td_deque_ntasks,
579 TCR_4(thread_data->td.td_deque_ntasks) + 1);
580 KMP_FSYNC_RELEASING(thread->th.th_current_task);
581 KMP_FSYNC_RELEASING(taskdata);
582 KA_TRACE(20, (
"__kmp_push_task: T#%d returning TASK_SUCCESSFULLY_PUSHED: "
583 "task=%p ntasks=%d head=%u tail=%u\n",
584 gtid, taskdata, thread_data->td.td_deque_ntasks,
585 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
587 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
589 return TASK_SUCCESSFULLY_PUSHED;
596 void __kmp_pop_current_task_from_thread(kmp_info_t *this_thr) {
597 KF_TRACE(10, (
"__kmp_pop_current_task_from_thread(enter): T#%d "
598 "this_thread=%p, curtask=%p, "
599 "curtask_parent=%p\n",
600 0, this_thr, this_thr->th.th_current_task,
601 this_thr->th.th_current_task->td_parent));
603 this_thr->th.th_current_task = this_thr->th.th_current_task->td_parent;
605 KF_TRACE(10, (
"__kmp_pop_current_task_from_thread(exit): T#%d "
606 "this_thread=%p, curtask=%p, "
607 "curtask_parent=%p\n",
608 0, this_thr, this_thr->th.th_current_task,
609 this_thr->th.th_current_task->td_parent));
618 void __kmp_push_current_task_to_thread(kmp_info_t *this_thr, kmp_team_t *team,
622 KF_TRACE(10, (
"__kmp_push_current_task_to_thread(enter): T#%d this_thread=%p "
625 tid, this_thr, this_thr->th.th_current_task,
626 team->t.t_implicit_task_taskdata[tid].td_parent));
628 KMP_DEBUG_ASSERT(this_thr != NULL);
631 if (this_thr->th.th_current_task != &team->t.t_implicit_task_taskdata[0]) {
632 team->t.t_implicit_task_taskdata[0].td_parent =
633 this_thr->th.th_current_task;
634 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[0];
637 team->t.t_implicit_task_taskdata[tid].td_parent =
638 team->t.t_implicit_task_taskdata[0].td_parent;
639 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[tid];
642 KF_TRACE(10, (
"__kmp_push_current_task_to_thread(exit): T#%d this_thread=%p "
645 tid, this_thr, this_thr->th.th_current_task,
646 team->t.t_implicit_task_taskdata[tid].td_parent));
654 static void __kmp_task_start(kmp_int32 gtid, kmp_task_t *task,
655 kmp_taskdata_t *current_task) {
656 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
657 kmp_info_t *thread = __kmp_threads[gtid];
660 (
"__kmp_task_start(enter): T#%d starting task %p: current_task=%p\n",
661 gtid, taskdata, current_task));
663 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
668 current_task->td_flags.executing = 0;
671 #ifdef BUILD_TIED_TASK_STACK
672 if (taskdata->td_flags.tiedness == TASK_TIED) {
673 __kmp_push_task_stack(gtid, thread, taskdata);
678 thread->th.th_current_task = taskdata;
680 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 0 ||
681 taskdata->td_flags.tiedness == TASK_UNTIED);
682 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0 ||
683 taskdata->td_flags.tiedness == TASK_UNTIED);
684 taskdata->td_flags.started = 1;
685 taskdata->td_flags.executing = 1;
686 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
687 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
694 KA_TRACE(10, (
"__kmp_task_start(exit): T#%d task=%p\n", gtid, taskdata));
705 static inline void __ompt_task_init(kmp_taskdata_t *task,
int tid) {
707 task->ompt_task_info.task_data.value = 0;
708 task->ompt_task_info.frame.exit_frame = ompt_data_none;
709 task->ompt_task_info.frame.enter_frame = ompt_data_none;
710 task->ompt_task_info.frame.exit_frame_flags =
711 ompt_frame_runtime | ompt_frame_framepointer;
712 task->ompt_task_info.frame.enter_frame_flags =
713 ompt_frame_runtime | ompt_frame_framepointer;
714 task->ompt_task_info.dispatch_chunk.start = 0;
715 task->ompt_task_info.dispatch_chunk.iterations = 0;
720 static inline void __ompt_task_start(kmp_task_t *task,
721 kmp_taskdata_t *current_task,
723 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
724 ompt_task_status_t status = ompt_task_switch;
725 if (__kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded) {
726 status = ompt_task_yield;
727 __kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded = 0;
730 if (ompt_enabled.ompt_callback_task_schedule) {
731 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
732 &(current_task->ompt_task_info.task_data), status,
733 &(taskdata->ompt_task_info.task_data));
735 taskdata->ompt_task_info.scheduling_parent = current_task;
740 static inline void __ompt_task_finish(kmp_task_t *task,
741 kmp_taskdata_t *resumed_task,
742 ompt_task_status_t status) {
743 if (ompt_enabled.ompt_callback_task_schedule) {
744 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
745 if (__kmp_omp_cancellation && taskdata->td_taskgroup &&
746 taskdata->td_taskgroup->cancel_request == cancel_taskgroup) {
747 status = ompt_task_cancel;
751 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
752 &(taskdata->ompt_task_info.task_data), status,
753 (resumed_task ? &(resumed_task->ompt_task_info.task_data) : NULL));
759 static void __kmpc_omp_task_begin_if0_template(
ident_t *loc_ref, kmp_int32 gtid,
762 void *return_address) {
763 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
764 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
766 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(enter): T#%d loc=%p task=%p "
768 gtid, loc_ref, taskdata, current_task));
770 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
773 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
774 KMP_DEBUG_USE_VAR(counter);
775 KA_TRACE(20, (
"__kmpc_omp_task_begin_if0: T#%d untied_count (%d) "
776 "incremented for task %p\n",
777 gtid, counter, taskdata));
780 taskdata->td_flags.task_serial =
782 __kmp_task_start(gtid, task, current_task);
786 if (current_task->ompt_task_info.frame.enter_frame.ptr == NULL) {
787 current_task->ompt_task_info.frame.enter_frame.ptr =
788 taskdata->ompt_task_info.frame.exit_frame.ptr = frame_address;
789 current_task->ompt_task_info.frame.enter_frame_flags =
790 taskdata->ompt_task_info.frame.exit_frame_flags =
791 ompt_frame_application | ompt_frame_framepointer;
793 if (ompt_enabled.ompt_callback_task_create) {
794 ompt_task_info_t *parent_info = &(current_task->ompt_task_info);
795 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
796 &(parent_info->task_data), &(parent_info->frame),
797 &(taskdata->ompt_task_info.task_data),
798 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(taskdata), 0,
801 __ompt_task_start(task, current_task, gtid);
805 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(exit): T#%d loc=%p task=%p,\n", gtid,
811 static void __kmpc_omp_task_begin_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
814 void *return_address) {
815 __kmpc_omp_task_begin_if0_template<true>(loc_ref, gtid, task, frame_address,
826 void __kmpc_omp_task_begin_if0(
ident_t *loc_ref, kmp_int32 gtid,
829 if (UNLIKELY(ompt_enabled.enabled)) {
830 OMPT_STORE_RETURN_ADDRESS(gtid);
831 __kmpc_omp_task_begin_if0_ompt(loc_ref, gtid, task,
832 OMPT_GET_FRAME_ADDRESS(1),
833 OMPT_LOAD_RETURN_ADDRESS(gtid));
837 __kmpc_omp_task_begin_if0_template<false>(loc_ref, gtid, task, NULL, NULL);
843 void __kmpc_omp_task_begin(
ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task) {
844 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
848 (
"__kmpc_omp_task_begin(enter): T#%d loc=%p task=%p current_task=%p\n",
849 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task), current_task));
851 __kmp_task_start(gtid, task, current_task);
853 KA_TRACE(10, (
"__kmpc_omp_task_begin(exit): T#%d loc=%p task=%p,\n", gtid,
854 loc_ref, KMP_TASK_TO_TASKDATA(task)));
864 static void __kmp_free_task(kmp_int32 gtid, kmp_taskdata_t *taskdata,
865 kmp_info_t *thread) {
866 KA_TRACE(30, (
"__kmp_free_task: T#%d freeing data from task %p\n", gtid,
870 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
871 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0);
872 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 1);
873 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
874 KMP_DEBUG_ASSERT(taskdata->td_allocated_child_tasks == 0 ||
875 taskdata->td_flags.task_serial == 1);
876 KMP_DEBUG_ASSERT(taskdata->td_incomplete_child_tasks == 0);
877 kmp_task_t *task = KMP_TASKDATA_TO_TASK(taskdata);
879 task->data1.destructors = NULL;
880 task->data2.priority = 0;
882 taskdata->td_flags.freed = 1;
885 __kmp_fast_free(thread, taskdata);
887 __kmp_thread_free(thread, taskdata);
889 KA_TRACE(20, (
"__kmp_free_task: T#%d freed task %p\n", gtid, taskdata));
898 static void __kmp_free_task_and_ancestors(kmp_int32 gtid,
899 kmp_taskdata_t *taskdata,
900 kmp_info_t *thread) {
903 kmp_int32 team_serial =
904 (taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) &&
905 !taskdata->td_flags.proxy;
906 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
908 kmp_int32 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
909 KMP_DEBUG_ASSERT(children >= 0);
912 while (children == 0) {
913 kmp_taskdata_t *parent_taskdata = taskdata->td_parent;
915 KA_TRACE(20, (
"__kmp_free_task_and_ancestors(enter): T#%d task %p complete "
916 "and freeing itself\n",
920 __kmp_free_task(gtid, taskdata, thread);
922 taskdata = parent_taskdata;
928 if (taskdata->td_flags.tasktype == TASK_IMPLICIT) {
929 if (taskdata->td_dephash) {
930 int children = KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks);
931 kmp_tasking_flags_t flags_old = taskdata->td_flags;
932 if (children == 0 && flags_old.complete == 1) {
933 kmp_tasking_flags_t flags_new = flags_old;
934 flags_new.complete = 0;
935 if (KMP_COMPARE_AND_STORE_ACQ32(
936 RCAST(kmp_int32 *, &taskdata->td_flags),
937 *RCAST(kmp_int32 *, &flags_old),
938 *RCAST(kmp_int32 *, &flags_new))) {
939 KA_TRACE(100, (
"__kmp_free_task_and_ancestors: T#%d cleans "
940 "dephash of implicit task %p\n",
943 __kmp_dephash_free_entries(thread, taskdata->td_dephash);
950 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
951 KMP_DEBUG_ASSERT(children >= 0);
955 20, (
"__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; "
956 "not freeing it yet\n",
957 gtid, taskdata, children));
968 static bool __kmp_track_children_task(kmp_taskdata_t *taskdata) {
969 kmp_tasking_flags_t flags = taskdata->td_flags;
970 bool ret = !(flags.team_serial || flags.tasking_ser);
971 ret = ret || flags.proxy == TASK_PROXY ||
972 flags.detachable == TASK_DETACHABLE || flags.hidden_helper;
974 KMP_ATOMIC_LD_ACQ(&taskdata->td_parent->td_incomplete_child_tasks) > 0;
988 static void __kmp_task_finish(kmp_int32 gtid, kmp_task_t *task,
989 kmp_taskdata_t *resumed_task) {
990 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
991 kmp_info_t *thread = __kmp_threads[gtid];
992 kmp_task_team_t *task_team =
993 thread->th.th_task_team;
995 kmp_int32 children = 0;
997 KA_TRACE(10, (
"__kmp_task_finish(enter): T#%d finishing task %p and resuming "
999 gtid, taskdata, resumed_task));
1001 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
1004 #ifdef BUILD_TIED_TASK_STACK
1005 if (taskdata->td_flags.tiedness == TASK_TIED) {
1006 __kmp_pop_task_stack(gtid, thread, taskdata);
1010 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
1013 kmp_int32 counter = KMP_ATOMIC_DEC(&taskdata->td_untied_count) - 1;
1016 (
"__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n",
1017 gtid, counter, taskdata));
1021 if (resumed_task == NULL) {
1022 KMP_DEBUG_ASSERT(taskdata->td_flags.task_serial);
1023 resumed_task = taskdata->td_parent;
1026 thread->th.th_current_task = resumed_task;
1027 resumed_task->td_flags.executing = 1;
1028 KA_TRACE(10, (
"__kmp_task_finish(exit): T#%d partially done task %p, "
1029 "resuming task %p\n",
1030 gtid, taskdata, resumed_task));
1038 (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) ==
1039 taskdata->td_flags.task_serial);
1040 if (taskdata->td_flags.task_serial) {
1041 if (resumed_task == NULL) {
1042 resumed_task = taskdata->td_parent;
1046 KMP_DEBUG_ASSERT(resumed_task !=
1056 if (UNLIKELY(taskdata->td_flags.destructors_thunk)) {
1057 kmp_routine_entry_t destr_thunk = task->data1.destructors;
1058 KMP_ASSERT(destr_thunk);
1059 destr_thunk(gtid, task);
1062 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
1063 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 1);
1064 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
1066 bool detach =
false;
1067 if (UNLIKELY(taskdata->td_flags.detachable == TASK_DETACHABLE)) {
1068 if (taskdata->td_allow_completion_event.type ==
1069 KMP_EVENT_ALLOW_COMPLETION) {
1071 __kmp_acquire_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
1072 if (taskdata->td_allow_completion_event.type ==
1073 KMP_EVENT_ALLOW_COMPLETION) {
1075 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
1076 taskdata->td_flags.executing = 0;
1083 __ompt_task_finish(task, resumed_task, ompt_task_detach);
1089 taskdata->td_flags.proxy = TASK_PROXY;
1092 __kmp_release_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
1097 taskdata->td_flags.complete = 1;
1102 __ompt_task_finish(task, resumed_task, ompt_task_complete);
1106 if (__kmp_track_children_task(taskdata)) {
1107 __kmp_release_deps(gtid, taskdata);
1112 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks);
1113 KMP_DEBUG_ASSERT(children >= 0);
1114 if (taskdata->td_taskgroup)
1115 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
1116 }
else if (task_team && (task_team->tt.tt_found_proxy_tasks ||
1117 task_team->tt.tt_hidden_helper_task_encountered)) {
1120 __kmp_release_deps(gtid, taskdata);
1126 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
1127 taskdata->td_flags.executing = 0;
1131 20, (
"__kmp_task_finish: T#%d finished task %p, %d incomplete children\n",
1132 gtid, taskdata, children));
1138 thread->th.th_current_task = resumed_task;
1140 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
1144 resumed_task->td_flags.executing = 1;
1147 10, (
"__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n",
1148 gtid, taskdata, resumed_task));
1153 template <
bool ompt>
1154 static void __kmpc_omp_task_complete_if0_template(
ident_t *loc_ref,
1157 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n",
1158 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
1159 KMP_DEBUG_ASSERT(gtid >= 0);
1161 __kmp_task_finish<ompt>(gtid, task, NULL);
1163 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n",
1164 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
1168 ompt_frame_t *ompt_frame;
1169 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
1170 ompt_frame->enter_frame = ompt_data_none;
1171 ompt_frame->enter_frame_flags =
1172 ompt_frame_runtime | ompt_frame_framepointer;
1181 void __kmpc_omp_task_complete_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
1183 __kmpc_omp_task_complete_if0_template<true>(loc_ref, gtid, task);
1192 void __kmpc_omp_task_complete_if0(
ident_t *loc_ref, kmp_int32 gtid,
1195 if (UNLIKELY(ompt_enabled.enabled)) {
1196 __kmpc_omp_task_complete_if0_ompt(loc_ref, gtid, task);
1200 __kmpc_omp_task_complete_if0_template<false>(loc_ref, gtid, task);
1206 void __kmpc_omp_task_complete(
ident_t *loc_ref, kmp_int32 gtid,
1208 KA_TRACE(10, (
"__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n", gtid,
1209 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1211 __kmp_task_finish<false>(gtid, task,
1214 KA_TRACE(10, (
"__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n", gtid,
1215 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1231 void __kmp_init_implicit_task(
ident_t *loc_ref, kmp_info_t *this_thr,
1232 kmp_team_t *team,
int tid,
int set_curr_task) {
1233 kmp_taskdata_t *task = &team->t.t_implicit_task_taskdata[tid];
1237 (
"__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n",
1238 tid, team, task, set_curr_task ?
"TRUE" :
"FALSE"));
1240 task->td_task_id = KMP_GEN_TASK_ID();
1241 task->td_team = team;
1244 task->td_ident = loc_ref;
1245 task->td_taskwait_ident = NULL;
1246 task->td_taskwait_counter = 0;
1247 task->td_taskwait_thread = 0;
1249 task->td_flags.tiedness = TASK_TIED;
1250 task->td_flags.tasktype = TASK_IMPLICIT;
1251 task->td_flags.proxy = TASK_FULL;
1254 task->td_flags.task_serial = 1;
1255 task->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1256 task->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1258 task->td_flags.started = 1;
1259 task->td_flags.executing = 1;
1260 task->td_flags.complete = 0;
1261 task->td_flags.freed = 0;
1263 task->td_depnode = NULL;
1264 task->td_last_tied = task;
1265 task->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1267 if (set_curr_task) {
1268 KMP_ATOMIC_ST_REL(&task->td_incomplete_child_tasks, 0);
1270 KMP_ATOMIC_ST_REL(&task->td_allocated_child_tasks, 0);
1271 task->td_taskgroup = NULL;
1272 task->td_dephash = NULL;
1273 __kmp_push_current_task_to_thread(this_thr, team, tid);
1275 KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0);
1276 KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0);
1280 if (UNLIKELY(ompt_enabled.enabled))
1281 __ompt_task_init(task, tid);
1284 KF_TRACE(10, (
"__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n", tid,
1293 void __kmp_finish_implicit_task(kmp_info_t *thread) {
1294 kmp_taskdata_t *task = thread->th.th_current_task;
1295 if (task->td_dephash) {
1297 task->td_flags.complete = 1;
1298 children = KMP_ATOMIC_LD_ACQ(&task->td_incomplete_child_tasks);
1299 kmp_tasking_flags_t flags_old = task->td_flags;
1300 if (children == 0 && flags_old.complete == 1) {
1301 kmp_tasking_flags_t flags_new = flags_old;
1302 flags_new.complete = 0;
1303 if (KMP_COMPARE_AND_STORE_ACQ32(RCAST(kmp_int32 *, &task->td_flags),
1304 *RCAST(kmp_int32 *, &flags_old),
1305 *RCAST(kmp_int32 *, &flags_new))) {
1306 KA_TRACE(100, (
"__kmp_finish_implicit_task: T#%d cleans "
1307 "dephash of implicit task %p\n",
1308 thread->th.th_info.ds.ds_gtid, task));
1309 __kmp_dephash_free_entries(thread, task->td_dephash);
1319 void __kmp_free_implicit_task(kmp_info_t *thread) {
1320 kmp_taskdata_t *task = thread->th.th_current_task;
1321 if (task && task->td_dephash) {
1322 __kmp_dephash_free(thread, task->td_dephash);
1323 task->td_dephash = NULL;
1329 static size_t __kmp_round_up_to_val(
size_t size,
size_t val) {
1330 if (size & (val - 1)) {
1332 if (size <= KMP_SIZE_T_MAX - val) {
1351 kmp_task_t *__kmp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1352 kmp_tasking_flags_t *flags,
1353 size_t sizeof_kmp_task_t,
size_t sizeof_shareds,
1354 kmp_routine_entry_t task_entry) {
1356 kmp_taskdata_t *taskdata;
1357 kmp_info_t *thread = __kmp_threads[gtid];
1358 kmp_team_t *team = thread->th.th_team;
1359 kmp_taskdata_t *parent_task = thread->th.th_current_task;
1360 size_t shareds_offset;
1362 if (UNLIKELY(!TCR_4(__kmp_init_middle)))
1363 __kmp_middle_initialize();
1365 if (flags->hidden_helper) {
1366 if (__kmp_enable_hidden_helper) {
1367 if (!TCR_4(__kmp_init_hidden_helper))
1368 __kmp_hidden_helper_initialize();
1371 flags->hidden_helper = FALSE;
1375 KA_TRACE(10, (
"__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) "
1376 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1377 gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t,
1378 sizeof_shareds, task_entry));
1380 KMP_DEBUG_ASSERT(parent_task);
1381 if (parent_task->td_flags.final) {
1382 if (flags->merged_if0) {
1387 if (flags->tiedness == TASK_UNTIED && !team->t.t_serialized) {
1391 KMP_CHECK_UPDATE(thread->th.th_task_team->tt.tt_untied_task_encountered, 1);
1397 if (UNLIKELY(flags->proxy == TASK_PROXY ||
1398 flags->detachable == TASK_DETACHABLE || flags->hidden_helper)) {
1399 if (flags->proxy == TASK_PROXY) {
1400 flags->tiedness = TASK_UNTIED;
1401 flags->merged_if0 = 1;
1405 if ((thread->th.th_task_team) == NULL) {
1408 KMP_DEBUG_ASSERT(team->t.t_serialized);
1410 (
"T#%d creating task team in __kmp_task_alloc for proxy task\n",
1413 __kmp_task_team_setup(thread, team, 1);
1414 thread->th.th_task_team = team->t.t_task_team[thread->th.th_task_state];
1416 kmp_task_team_t *task_team = thread->th.th_task_team;
1419 if (!KMP_TASKING_ENABLED(task_team)) {
1422 (
"T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid));
1423 __kmp_enable_tasking(task_team, thread);
1424 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
1425 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
1427 if (thread_data->td.td_deque == NULL) {
1428 __kmp_alloc_task_deque(thread, thread_data);
1432 if ((flags->proxy == TASK_PROXY || flags->detachable == TASK_DETACHABLE) &&
1433 task_team->tt.tt_found_proxy_tasks == FALSE)
1434 TCW_4(task_team->tt.tt_found_proxy_tasks, TRUE);
1435 if (flags->hidden_helper &&
1436 task_team->tt.tt_hidden_helper_task_encountered == FALSE)
1437 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, TRUE);
1442 shareds_offset =
sizeof(kmp_taskdata_t) + sizeof_kmp_task_t;
1443 shareds_offset = __kmp_round_up_to_val(shareds_offset,
sizeof(
void *));
1446 KA_TRACE(30, (
"__kmp_task_alloc: T#%d First malloc size: %ld\n", gtid,
1448 KA_TRACE(30, (
"__kmp_task_alloc: T#%d Second malloc size: %ld\n", gtid,
1453 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, shareds_offset +
1456 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, shareds_offset +
1460 task = KMP_TASKDATA_TO_TASK(taskdata);
1463 #if KMP_ARCH_X86 || KMP_ARCH_PPC64 || !KMP_HAVE_QUAD
1464 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(
double) - 1)) == 0);
1465 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(
double) - 1)) == 0);
1467 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(_Quad) - 1)) == 0);
1468 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(_Quad) - 1)) == 0);
1470 if (sizeof_shareds > 0) {
1472 task->shareds = &((
char *)taskdata)[shareds_offset];
1474 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
1477 task->shareds = NULL;
1479 task->routine = task_entry;
1482 taskdata->td_task_id = KMP_GEN_TASK_ID();
1483 taskdata->td_team = thread->th.th_team;
1484 taskdata->td_alloc_thread = thread;
1485 taskdata->td_parent = parent_task;
1486 taskdata->td_level = parent_task->td_level + 1;
1487 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
1488 taskdata->td_ident = loc_ref;
1489 taskdata->td_taskwait_ident = NULL;
1490 taskdata->td_taskwait_counter = 0;
1491 taskdata->td_taskwait_thread = 0;
1492 KMP_DEBUG_ASSERT(taskdata->td_parent != NULL);
1494 if (flags->proxy == TASK_FULL)
1495 copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs);
1497 taskdata->td_flags = *flags;
1498 taskdata->td_task_team = thread->th.th_task_team;
1499 taskdata->td_size_alloc = shareds_offset + sizeof_shareds;
1500 taskdata->td_flags.tasktype = TASK_EXPLICIT;
1503 if (flags->hidden_helper) {
1504 kmp_info_t *shadow_thread = __kmp_threads[KMP_GTID_TO_SHADOW_GTID(gtid)];
1505 taskdata->td_team = shadow_thread->th.th_team;
1506 taskdata->td_task_team = shadow_thread->th.th_task_team;
1510 taskdata->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1513 taskdata->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1519 taskdata->td_flags.task_serial =
1520 (parent_task->td_flags.final || taskdata->td_flags.team_serial ||
1521 taskdata->td_flags.tasking_ser || flags->merged_if0);
1523 taskdata->td_flags.started = 0;
1524 taskdata->td_flags.executing = 0;
1525 taskdata->td_flags.complete = 0;
1526 taskdata->td_flags.freed = 0;
1528 KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0);
1530 KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1);
1531 taskdata->td_taskgroup =
1532 parent_task->td_taskgroup;
1533 taskdata->td_dephash = NULL;
1534 taskdata->td_depnode = NULL;
1535 if (flags->tiedness == TASK_UNTIED)
1536 taskdata->td_last_tied = NULL;
1538 taskdata->td_last_tied = taskdata;
1539 taskdata->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1541 if (UNLIKELY(ompt_enabled.enabled))
1542 __ompt_task_init(taskdata, gtid);
1546 if (__kmp_track_children_task(taskdata)) {
1547 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
1548 if (parent_task->td_taskgroup)
1549 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
1552 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) {
1553 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
1555 if (flags->hidden_helper) {
1556 taskdata->td_flags.task_serial = FALSE;
1558 KMP_ATOMIC_INC(&__kmp_unexecuted_hidden_helper_tasks);
1562 KA_TRACE(20, (
"__kmp_task_alloc(exit): T#%d created task %p parent=%p\n",
1563 gtid, taskdata, taskdata->td_parent));
1568 kmp_task_t *__kmpc_omp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1569 kmp_int32 flags,
size_t sizeof_kmp_task_t,
1570 size_t sizeof_shareds,
1571 kmp_routine_entry_t task_entry) {
1573 kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
1574 __kmp_assert_valid_gtid(gtid);
1575 input_flags->native = FALSE;
1577 KA_TRACE(10, (
"__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s %s) "
1578 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1579 gtid, loc_ref, input_flags->tiedness ?
"tied " :
"untied",
1580 input_flags->proxy ?
"proxy" :
"",
1581 input_flags->detachable ?
"detachable" :
"", sizeof_kmp_task_t,
1582 sizeof_shareds, task_entry));
1584 retval = __kmp_task_alloc(loc_ref, gtid, input_flags, sizeof_kmp_task_t,
1585 sizeof_shareds, task_entry);
1587 KA_TRACE(20, (
"__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval));
1592 kmp_task_t *__kmpc_omp_target_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1594 size_t sizeof_kmp_task_t,
1595 size_t sizeof_shareds,
1596 kmp_routine_entry_t task_entry,
1597 kmp_int64 device_id) {
1598 auto &input_flags =
reinterpret_cast<kmp_tasking_flags_t &
>(flags);
1600 input_flags.tiedness = TASK_UNTIED;
1602 if (__kmp_enable_hidden_helper)
1603 input_flags.hidden_helper = TRUE;
1605 return __kmpc_omp_task_alloc(loc_ref, gtid, flags, sizeof_kmp_task_t,
1606 sizeof_shareds, task_entry);
1624 kmp_task_t *new_task, kmp_int32 naffins,
1625 kmp_task_affinity_info_t *affin_list) {
1634 static void __kmp_invoke_task(kmp_int32 gtid, kmp_task_t *task,
1635 kmp_taskdata_t *current_task) {
1636 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
1640 30, (
"__kmp_invoke_task(enter): T#%d invoking task %p, current_task=%p\n",
1641 gtid, taskdata, current_task));
1642 KMP_DEBUG_ASSERT(task);
1643 if (UNLIKELY(taskdata->td_flags.proxy == TASK_PROXY &&
1644 taskdata->td_flags.complete == 1)) {
1649 (
"__kmp_invoke_task: T#%d running bottom finish for proxy task %p\n",
1652 __kmp_bottom_half_finish_proxy(gtid, task);
1654 KA_TRACE(30, (
"__kmp_invoke_task(exit): T#%d completed bottom finish for "
1655 "proxy task %p, resuming task %p\n",
1656 gtid, taskdata, current_task));
1664 ompt_thread_info_t oldInfo;
1665 if (UNLIKELY(ompt_enabled.enabled)) {
1667 thread = __kmp_threads[gtid];
1668 oldInfo = thread->th.ompt_thread_info;
1669 thread->th.ompt_thread_info.wait_id = 0;
1670 thread->th.ompt_thread_info.state = (thread->th.th_team_serialized)
1671 ? ompt_state_work_serial
1672 : ompt_state_work_parallel;
1673 taskdata->ompt_task_info.frame.exit_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1678 if (taskdata->td_flags.hidden_helper) {
1680 KMP_ASSERT(KMP_HIDDEN_HELPER_THREAD(gtid));
1681 KMP_ATOMIC_DEC(&__kmp_unexecuted_hidden_helper_tasks);
1685 if (taskdata->td_flags.proxy != TASK_PROXY) {
1686 __kmp_task_start(gtid, task, current_task);
1692 if (UNLIKELY(__kmp_omp_cancellation)) {
1693 thread = __kmp_threads[gtid];
1694 kmp_team_t *this_team = thread->th.th_team;
1695 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
1696 if ((taskgroup && taskgroup->cancel_request) ||
1697 (this_team->t.t_cancel_request == cancel_parallel)) {
1698 #if OMPT_SUPPORT && OMPT_OPTIONAL
1699 ompt_data_t *task_data;
1700 if (UNLIKELY(ompt_enabled.ompt_callback_cancel)) {
1701 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, NULL, NULL);
1702 ompt_callbacks.ompt_callback(ompt_callback_cancel)(
1704 ((taskgroup && taskgroup->cancel_request) ? ompt_cancel_taskgroup
1705 : ompt_cancel_parallel) |
1706 ompt_cancel_discarded_task,
1719 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
1720 taskdata->td_last_tied = current_task->td_last_tied;
1721 KMP_DEBUG_ASSERT(taskdata->td_last_tied);
1723 #if KMP_STATS_ENABLED
1725 switch (KMP_GET_THREAD_STATE()) {
1726 case FORK_JOIN_BARRIER:
1727 KMP_PUSH_PARTITIONED_TIMER(OMP_task_join_bar);
1730 KMP_PUSH_PARTITIONED_TIMER(OMP_task_plain_bar);
1733 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskyield);
1736 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskwait);
1739 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskgroup);
1742 KMP_PUSH_PARTITIONED_TIMER(OMP_task_immediate);
1749 if (UNLIKELY(ompt_enabled.enabled))
1750 __ompt_task_start(task, current_task, gtid);
1752 #if OMPT_SUPPORT && OMPT_OPTIONAL
1753 if (UNLIKELY(ompt_enabled.ompt_callback_dispatch &&
1754 taskdata->ompt_task_info.dispatch_chunk.iterations > 0)) {
1755 ompt_data_t instance = ompt_data_none;
1756 instance.ptr = &(taskdata->ompt_task_info.dispatch_chunk);
1757 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
1758 ompt_callbacks.ompt_callback(ompt_callback_dispatch)(
1759 &(team_info->parallel_data), &(taskdata->ompt_task_info.task_data),
1760 ompt_dispatch_taskloop_chunk, instance);
1761 taskdata->ompt_task_info.dispatch_chunk = {0, 0};
1766 if (ompd_state & OMPD_ENABLE_BP)
1767 ompd_bp_task_begin();
1770 #if USE_ITT_BUILD && USE_ITT_NOTIFY
1771 kmp_uint64 cur_time;
1772 kmp_int32 kmp_itt_count_task =
1773 __kmp_forkjoin_frames_mode == 3 && !taskdata->td_flags.task_serial &&
1774 current_task->td_flags.tasktype == TASK_IMPLICIT;
1775 if (kmp_itt_count_task) {
1776 thread = __kmp_threads[gtid];
1778 if (thread->th.th_bar_arrive_time)
1779 cur_time = __itt_get_timestamp();
1781 kmp_itt_count_task = 0;
1783 KMP_FSYNC_ACQUIRED(taskdata);
1786 if (task->routine != NULL) {
1787 #ifdef KMP_GOMP_COMPAT
1788 if (taskdata->td_flags.native) {
1789 ((void (*)(
void *))(*(task->routine)))(task->shareds);
1793 (*(task->routine))(gtid, task);
1796 KMP_POP_PARTITIONED_TIMER();
1798 #if USE_ITT_BUILD && USE_ITT_NOTIFY
1799 if (kmp_itt_count_task) {
1801 thread->th.th_bar_arrive_time += (__itt_get_timestamp() - cur_time);
1803 KMP_FSYNC_CANCEL(taskdata);
1804 KMP_FSYNC_RELEASING(taskdata->td_parent);
1809 if (ompd_state & OMPD_ENABLE_BP)
1814 if (taskdata->td_flags.proxy != TASK_PROXY) {
1816 if (UNLIKELY(ompt_enabled.enabled)) {
1817 thread->th.ompt_thread_info = oldInfo;
1818 if (taskdata->td_flags.tiedness == TASK_TIED) {
1819 taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
1821 __kmp_task_finish<true>(gtid, task, current_task);
1824 __kmp_task_finish<false>(gtid, task, current_task);
1829 (
"__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n",
1830 gtid, taskdata, current_task));
1844 kmp_int32 __kmpc_omp_task_parts(
ident_t *loc_ref, kmp_int32 gtid,
1845 kmp_task_t *new_task) {
1846 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1848 KA_TRACE(10, (
"__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n", gtid,
1849 loc_ref, new_taskdata));
1852 kmp_taskdata_t *parent;
1853 if (UNLIKELY(ompt_enabled.enabled)) {
1854 parent = new_taskdata->td_parent;
1855 if (ompt_enabled.ompt_callback_task_create) {
1856 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1857 &(parent->ompt_task_info.task_data), &(parent->ompt_task_info.frame),
1858 &(new_taskdata->ompt_task_info.task_data), ompt_task_explicit, 0,
1859 OMPT_GET_RETURN_ADDRESS(0));
1867 if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1869 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1870 new_taskdata->td_flags.task_serial = 1;
1871 __kmp_invoke_task(gtid, new_task, current_task);
1876 (
"__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: "
1877 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
1878 gtid, loc_ref, new_taskdata));
1881 if (UNLIKELY(ompt_enabled.enabled)) {
1882 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1885 return TASK_CURRENT_NOT_QUEUED;
1899 kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task,
1900 bool serialize_immediate) {
1901 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1905 if (new_taskdata->td_flags.proxy == TASK_PROXY ||
1906 __kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1908 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1909 if (serialize_immediate)
1910 new_taskdata->td_flags.task_serial = 1;
1911 __kmp_invoke_task(gtid, new_task, current_task);
1914 return TASK_CURRENT_NOT_QUEUED;
1929 kmp_int32 __kmpc_omp_task(
ident_t *loc_ref, kmp_int32 gtid,
1930 kmp_task_t *new_task) {
1932 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1934 #if KMP_DEBUG || OMPT_SUPPORT
1935 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1937 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1939 __kmp_assert_valid_gtid(gtid);
1942 kmp_taskdata_t *parent = NULL;
1943 if (UNLIKELY(ompt_enabled.enabled)) {
1944 if (!new_taskdata->td_flags.started) {
1945 OMPT_STORE_RETURN_ADDRESS(gtid);
1946 parent = new_taskdata->td_parent;
1947 if (!parent->ompt_task_info.frame.enter_frame.ptr) {
1948 parent->ompt_task_info.frame.enter_frame.ptr =
1949 OMPT_GET_FRAME_ADDRESS(0);
1951 if (ompt_enabled.ompt_callback_task_create) {
1952 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1953 &(parent->ompt_task_info.task_data),
1954 &(parent->ompt_task_info.frame),
1955 &(new_taskdata->ompt_task_info.task_data),
1956 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1957 OMPT_LOAD_RETURN_ADDRESS(gtid));
1962 __ompt_task_finish(new_task,
1963 new_taskdata->ompt_task_info.scheduling_parent,
1965 new_taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
1970 res = __kmp_omp_task(gtid, new_task,
true);
1972 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
1973 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1974 gtid, loc_ref, new_taskdata));
1976 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
1977 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1996 kmp_int32 __kmp_omp_taskloop_task(
ident_t *loc_ref, kmp_int32 gtid,
1997 kmp_task_t *new_task,
void *codeptr_ra) {
1999 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
2001 #if KMP_DEBUG || OMPT_SUPPORT
2002 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
2004 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
2008 kmp_taskdata_t *parent = NULL;
2009 if (UNLIKELY(ompt_enabled.enabled && !new_taskdata->td_flags.started)) {
2010 parent = new_taskdata->td_parent;
2011 if (!parent->ompt_task_info.frame.enter_frame.ptr)
2012 parent->ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
2013 if (ompt_enabled.ompt_callback_task_create) {
2014 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
2015 &(parent->ompt_task_info.task_data), &(parent->ompt_task_info.frame),
2016 &(new_taskdata->ompt_task_info.task_data),
2017 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
2023 res = __kmp_omp_task(gtid, new_task,
true);
2025 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
2026 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
2027 gtid, loc_ref, new_taskdata));
2029 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
2030 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
2036 template <
bool ompt>
2037 static kmp_int32 __kmpc_omp_taskwait_template(
ident_t *loc_ref, kmp_int32 gtid,
2038 void *frame_address,
2039 void *return_address) {
2040 kmp_taskdata_t *taskdata =
nullptr;
2042 int thread_finished = FALSE;
2043 KMP_SET_THREAD_STATE_BLOCK(TASKWAIT);
2045 KA_TRACE(10, (
"__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref));
2046 KMP_DEBUG_ASSERT(gtid >= 0);
2048 if (__kmp_tasking_mode != tskm_immediate_exec) {
2049 thread = __kmp_threads[gtid];
2050 taskdata = thread->th.th_current_task;
2052 #if OMPT_SUPPORT && OMPT_OPTIONAL
2053 ompt_data_t *my_task_data;
2054 ompt_data_t *my_parallel_data;
2057 my_task_data = &(taskdata->ompt_task_info.task_data);
2058 my_parallel_data = OMPT_CUR_TEAM_DATA(thread);
2060 taskdata->ompt_task_info.frame.enter_frame.ptr = frame_address;
2062 if (ompt_enabled.ompt_callback_sync_region) {
2063 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2064 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
2065 my_task_data, return_address);
2068 if (ompt_enabled.ompt_callback_sync_region_wait) {
2069 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2070 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
2071 my_task_data, return_address);
2081 taskdata->td_taskwait_counter += 1;
2082 taskdata->td_taskwait_ident = loc_ref;
2083 taskdata->td_taskwait_thread = gtid + 1;
2086 void *itt_sync_obj = NULL;
2088 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2093 !taskdata->td_flags.team_serial && !taskdata->td_flags.final;
2095 must_wait = must_wait || (thread->th.th_task_team != NULL &&
2096 thread->th.th_task_team->tt.tt_found_proxy_tasks);
2100 (__kmp_enable_hidden_helper && thread->th.th_task_team != NULL &&
2101 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered);
2104 kmp_flag_32<false, false> flag(
2105 RCAST(std::atomic<kmp_uint32> *,
2106 &(taskdata->td_incomplete_child_tasks)),
2108 while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) != 0) {
2109 flag.execute_tasks(thread, gtid, FALSE,
2110 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2111 __kmp_task_stealing_constraint);
2115 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2116 KMP_FSYNC_ACQUIRED(taskdata);
2121 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2123 #if OMPT_SUPPORT && OMPT_OPTIONAL
2125 if (ompt_enabled.ompt_callback_sync_region_wait) {
2126 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2127 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
2128 my_task_data, return_address);
2130 if (ompt_enabled.ompt_callback_sync_region) {
2131 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2132 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
2133 my_task_data, return_address);
2135 taskdata->ompt_task_info.frame.enter_frame = ompt_data_none;
2141 KA_TRACE(10, (
"__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, "
2142 "returning TASK_CURRENT_NOT_QUEUED\n",
2145 return TASK_CURRENT_NOT_QUEUED;
2148 #if OMPT_SUPPORT && OMPT_OPTIONAL
2150 static kmp_int32 __kmpc_omp_taskwait_ompt(
ident_t *loc_ref, kmp_int32 gtid,
2151 void *frame_address,
2152 void *return_address) {
2153 return __kmpc_omp_taskwait_template<true>(loc_ref, gtid, frame_address,
2160 kmp_int32 __kmpc_omp_taskwait(
ident_t *loc_ref, kmp_int32 gtid) {
2161 #if OMPT_SUPPORT && OMPT_OPTIONAL
2162 if (UNLIKELY(ompt_enabled.enabled)) {
2163 OMPT_STORE_RETURN_ADDRESS(gtid);
2164 return __kmpc_omp_taskwait_ompt(loc_ref, gtid, OMPT_GET_FRAME_ADDRESS(0),
2165 OMPT_LOAD_RETURN_ADDRESS(gtid));
2168 return __kmpc_omp_taskwait_template<false>(loc_ref, gtid, NULL, NULL);
2172 kmp_int32 __kmpc_omp_taskyield(
ident_t *loc_ref, kmp_int32 gtid,
int end_part) {
2173 kmp_taskdata_t *taskdata = NULL;
2175 int thread_finished = FALSE;
2178 KMP_SET_THREAD_STATE_BLOCK(TASKYIELD);
2180 KA_TRACE(10, (
"__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n",
2181 gtid, loc_ref, end_part));
2182 __kmp_assert_valid_gtid(gtid);
2184 if (__kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel) {
2185 thread = __kmp_threads[gtid];
2186 taskdata = thread->th.th_current_task;
2193 taskdata->td_taskwait_counter += 1;
2194 taskdata->td_taskwait_ident = loc_ref;
2195 taskdata->td_taskwait_thread = gtid + 1;
2198 void *itt_sync_obj = NULL;
2200 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2203 if (!taskdata->td_flags.team_serial) {
2204 kmp_task_team_t *task_team = thread->th.th_task_team;
2205 if (task_team != NULL) {
2206 if (KMP_TASKING_ENABLED(task_team)) {
2208 if (UNLIKELY(ompt_enabled.enabled))
2209 thread->th.ompt_thread_info.ompt_task_yielded = 1;
2211 __kmp_execute_tasks_32(
2212 thread, gtid, (kmp_flag_32<> *)NULL, FALSE,
2213 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2214 __kmp_task_stealing_constraint);
2216 if (UNLIKELY(ompt_enabled.enabled))
2217 thread->th.ompt_thread_info.ompt_task_yielded = 0;
2223 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2228 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2231 KA_TRACE(10, (
"__kmpc_omp_taskyield(exit): T#%d task %p resuming, "
2232 "returning TASK_CURRENT_NOT_QUEUED\n",
2235 return TASK_CURRENT_NOT_QUEUED;
2256 unsigned reserved31 : 31;
2336 template <
typename T>
2337 void *__kmp_task_reduction_init(
int gtid,
int num, T *data) {
2338 __kmp_assert_valid_gtid(gtid);
2339 kmp_info_t *thread = __kmp_threads[gtid];
2340 kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup;
2341 kmp_uint32 nth = thread->th.th_team_nproc;
2345 KMP_ASSERT(tg != NULL);
2346 KMP_ASSERT(data != NULL);
2347 KMP_ASSERT(num > 0);
2349 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n",
2353 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, taskgroup %p, #items %d\n",
2357 for (
int i = 0; i < num; ++i) {
2358 size_t size = data[i].reduce_size - 1;
2360 size += CACHE_LINE - size % CACHE_LINE;
2361 KMP_ASSERT(data[i].reduce_comb != NULL);
2364 arr[i].
flags = data[i].flags;
2368 __kmp_assign_orig<T>(arr[i], data[i]);
2369 if (!arr[i].flags.lazy_priv) {
2372 arr[i].
reduce_pend = (
char *)(arr[i].reduce_priv) + nth * size;
2373 if (arr[i].reduce_init != NULL) {
2375 for (
size_t j = 0; j < nth; ++j) {
2376 __kmp_call_init<T>(arr[i], j * size);
2383 arr[i].
reduce_priv = __kmp_allocate(nth *
sizeof(
void *));
2386 tg->reduce_data = (
void *)arr;
2387 tg->reduce_num_data = num;
2426 template <
typename T>
2427 void __kmp_task_reduction_init_copy(kmp_info_t *thr,
int num, T *data,
2428 kmp_taskgroup_t *tg,
void *reduce_data) {
2430 KA_TRACE(20, (
"__kmp_task_reduction_init_copy: Th %p, init taskgroup %p,"
2432 thr, tg, reduce_data));
2437 for (
int i = 0; i < num; ++i) {
2440 tg->reduce_data = (
void *)arr;
2441 tg->reduce_num_data = num;
2454 __kmp_assert_valid_gtid(gtid);
2455 kmp_info_t *thread = __kmp_threads[gtid];
2456 kmp_int32 nth = thread->th.th_team_nproc;
2460 kmp_taskgroup_t *tg = (kmp_taskgroup_t *)tskgrp;
2462 tg = thread->th.th_current_task->td_taskgroup;
2463 KMP_ASSERT(tg != NULL);
2465 kmp_int32 num = tg->reduce_num_data;
2466 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
2468 KMP_ASSERT(data != NULL);
2469 while (tg != NULL) {
2470 for (
int i = 0; i < num; ++i) {
2471 if (!arr[i].flags.lazy_priv) {
2472 if (data == arr[i].reduce_shar ||
2473 (data >= arr[i].reduce_priv && data < arr[i].reduce_pend))
2474 return (
char *)(arr[i].
reduce_priv) + tid * arr[i].reduce_size;
2477 void **p_priv = (
void **)(arr[i].reduce_priv);
2478 if (data == arr[i].reduce_shar)
2481 for (
int j = 0; j < nth; ++j)
2482 if (data == p_priv[j])
2486 if (p_priv[tid] == NULL) {
2488 p_priv[tid] = __kmp_allocate(arr[i].reduce_size);
2489 if (arr[i].reduce_init != NULL) {
2490 if (arr[i].reduce_orig != NULL) {
2492 p_priv[tid], arr[i].reduce_orig);
2494 ((void (*)(
void *))arr[i].
reduce_init)(p_priv[tid]);
2503 num = tg->reduce_num_data;
2505 KMP_ASSERT2(0,
"Unknown task reduction item");
2511 static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg) {
2512 kmp_int32 nth = th->th.th_team_nproc;
2513 KMP_DEBUG_ASSERT(nth > 1);
2515 kmp_int32 num = tg->reduce_num_data;
2516 for (
int i = 0; i < num; ++i) {
2518 void (*f_fini)(
void *) = (
void (*)(
void *))(arr[i].
reduce_fini);
2519 void (*f_comb)(
void *,
void *) =
2521 if (!arr[i].flags.lazy_priv) {
2524 for (
int j = 0; j < nth; ++j) {
2525 void *priv_data = (
char *)pr_data + j * size;
2526 f_comb(sh_data, priv_data);
2531 void **pr_data = (
void **)(arr[i].reduce_priv);
2532 for (
int j = 0; j < nth; ++j) {
2533 if (pr_data[j] != NULL) {
2534 f_comb(sh_data, pr_data[j]);
2537 __kmp_free(pr_data[j]);
2541 __kmp_free(arr[i].reduce_priv);
2543 __kmp_thread_free(th, arr);
2544 tg->reduce_data = NULL;
2545 tg->reduce_num_data = 0;
2551 static void __kmp_task_reduction_clean(kmp_info_t *th, kmp_taskgroup_t *tg) {
2552 __kmp_thread_free(th, tg->reduce_data);
2553 tg->reduce_data = NULL;
2554 tg->reduce_num_data = 0;
2557 template <
typename T>
2558 void *__kmp_task_reduction_modifier_init(
ident_t *loc,
int gtid,
int is_ws,
2560 __kmp_assert_valid_gtid(gtid);
2561 kmp_info_t *thr = __kmp_threads[gtid];
2562 kmp_int32 nth = thr->th.th_team_nproc;
2563 __kmpc_taskgroup(loc, gtid);
2566 (
"__kmpc_reduction_modifier_init: T#%d, tg %p, exiting nth=1\n",
2567 gtid, thr->th.th_current_task->td_taskgroup));
2568 return (
void *)thr->th.th_current_task->td_taskgroup;
2570 kmp_team_t *team = thr->th.th_team;
2572 kmp_taskgroup_t *tg;
2573 reduce_data = KMP_ATOMIC_LD_RLX(&team->t.t_tg_reduce_data[is_ws]);
2574 if (reduce_data == NULL &&
2575 __kmp_atomic_compare_store(&team->t.t_tg_reduce_data[is_ws], reduce_data,
2578 KMP_DEBUG_ASSERT(reduce_data == NULL);
2580 tg = (kmp_taskgroup_t *)__kmp_task_reduction_init<T>(gtid, num, data);
2584 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[0]) == 0);
2585 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[1]) == 0);
2586 KMP_ATOMIC_ST_REL(&team->t.t_tg_reduce_data[is_ws], reduce_data);
2589 (reduce_data = KMP_ATOMIC_LD_ACQ(&team->t.t_tg_reduce_data[is_ws])) ==
2593 KMP_DEBUG_ASSERT(reduce_data > (
void *)1);
2594 tg = thr->th.th_current_task->td_taskgroup;
2595 __kmp_task_reduction_init_copy<T>(thr, num, data, tg, reduce_data);
2617 int num,
void *data) {
2618 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2638 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2651 __kmpc_end_taskgroup(loc, gtid);
2655 void __kmpc_taskgroup(
ident_t *loc,
int gtid) {
2656 __kmp_assert_valid_gtid(gtid);
2657 kmp_info_t *thread = __kmp_threads[gtid];
2658 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2659 kmp_taskgroup_t *tg_new =
2660 (kmp_taskgroup_t *)__kmp_thread_malloc(thread,
sizeof(kmp_taskgroup_t));
2661 KA_TRACE(10, (
"__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new));
2662 KMP_ATOMIC_ST_RLX(&tg_new->count, 0);
2663 KMP_ATOMIC_ST_RLX(&tg_new->cancel_request, cancel_noreq);
2664 tg_new->parent = taskdata->td_taskgroup;
2665 tg_new->reduce_data = NULL;
2666 tg_new->reduce_num_data = 0;
2667 tg_new->gomp_data = NULL;
2668 taskdata->td_taskgroup = tg_new;
2670 #if OMPT_SUPPORT && OMPT_OPTIONAL
2671 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2672 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2674 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2675 kmp_team_t *team = thread->th.th_team;
2676 ompt_data_t my_task_data = taskdata->ompt_task_info.task_data;
2678 ompt_data_t my_parallel_data = team->t.ompt_team_info.parallel_data;
2680 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2681 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2682 &(my_task_data), codeptr);
2689 void __kmpc_end_taskgroup(
ident_t *loc,
int gtid) {
2690 __kmp_assert_valid_gtid(gtid);
2691 kmp_info_t *thread = __kmp_threads[gtid];
2692 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2693 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
2694 int thread_finished = FALSE;
2696 #if OMPT_SUPPORT && OMPT_OPTIONAL
2698 ompt_data_t my_task_data;
2699 ompt_data_t my_parallel_data;
2700 void *codeptr =
nullptr;
2701 if (UNLIKELY(ompt_enabled.enabled)) {
2702 team = thread->th.th_team;
2703 my_task_data = taskdata->ompt_task_info.task_data;
2705 my_parallel_data = team->t.ompt_team_info.parallel_data;
2706 codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2708 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2712 KA_TRACE(10, (
"__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc));
2713 KMP_DEBUG_ASSERT(taskgroup != NULL);
2714 KMP_SET_THREAD_STATE_BLOCK(TASKGROUP);
2716 if (__kmp_tasking_mode != tskm_immediate_exec) {
2718 taskdata->td_taskwait_counter += 1;
2719 taskdata->td_taskwait_ident = loc;
2720 taskdata->td_taskwait_thread = gtid + 1;
2724 void *itt_sync_obj = NULL;
2726 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2730 #if OMPT_SUPPORT && OMPT_OPTIONAL
2731 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2732 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2733 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2734 &(my_task_data), codeptr);
2738 if (!taskdata->td_flags.team_serial ||
2739 (thread->th.th_task_team != NULL &&
2740 (thread->th.th_task_team->tt.tt_found_proxy_tasks ||
2741 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered))) {
2742 kmp_flag_32<false, false> flag(
2743 RCAST(std::atomic<kmp_uint32> *, &(taskgroup->count)), 0U);
2744 while (KMP_ATOMIC_LD_ACQ(&taskgroup->count) != 0) {
2745 flag.execute_tasks(thread, gtid, FALSE,
2746 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2747 __kmp_task_stealing_constraint);
2750 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2752 #if OMPT_SUPPORT && OMPT_OPTIONAL
2753 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2754 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2755 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2756 &(my_task_data), codeptr);
2761 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2762 KMP_FSYNC_ACQUIRED(taskdata);
2765 KMP_DEBUG_ASSERT(taskgroup->count == 0);
2767 if (taskgroup->reduce_data != NULL &&
2768 !taskgroup->gomp_data) {
2771 kmp_team_t *t = thread->th.th_team;
2775 if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[0])) != NULL &&
2778 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[0]);
2779 if (cnt == thread->th.th_team_nproc - 1) {
2782 __kmp_task_reduction_fini(thread, taskgroup);
2785 __kmp_thread_free(thread, reduce_data);
2786 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[0], NULL);
2787 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[0], 0);
2791 __kmp_task_reduction_clean(thread, taskgroup);
2793 }
else if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[1])) !=
2797 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[1]);
2798 if (cnt == thread->th.th_team_nproc - 1) {
2800 __kmp_task_reduction_fini(thread, taskgroup);
2803 __kmp_thread_free(thread, reduce_data);
2804 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[1], NULL);
2805 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[1], 0);
2809 __kmp_task_reduction_clean(thread, taskgroup);
2813 __kmp_task_reduction_fini(thread, taskgroup);
2817 taskdata->td_taskgroup = taskgroup->parent;
2818 __kmp_thread_free(thread, taskgroup);
2820 KA_TRACE(10, (
"__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n",
2823 #if OMPT_SUPPORT && OMPT_OPTIONAL
2824 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2825 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2826 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2827 &(my_task_data), codeptr);
2832 static kmp_task_t *__kmp_get_priority_task(kmp_int32 gtid,
2833 kmp_task_team_t *task_team,
2834 kmp_int32 is_constrained) {
2835 kmp_task_t *task = NULL;
2836 kmp_taskdata_t *taskdata;
2837 kmp_taskdata_t *current;
2838 kmp_thread_data_t *thread_data;
2839 int ntasks = task_team->tt.tt_num_task_pri;
2842 20, (
"__kmp_get_priority_task(exit #1): T#%d No tasks to get\n", gtid));
2847 if (__kmp_atomic_compare_store(&task_team->tt.tt_num_task_pri, ntasks,
2850 }
while (ntasks > 0);
2852 KA_TRACE(20, (
"__kmp_get_priority_task(exit #2): T#%d No tasks to get\n",
2858 kmp_task_pri_t *list = task_team->tt.tt_task_pri_list;
2860 KMP_ASSERT(list != NULL);
2861 thread_data = &list->td;
2862 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
2863 deque_ntasks = thread_data->td.td_deque_ntasks;
2864 if (deque_ntasks == 0) {
2865 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2866 KA_TRACE(20, (
"__kmp_get_priority_task: T#%d No tasks to get from %p\n",
2867 __kmp_get_gtid(), thread_data));
2870 }
while (deque_ntasks == 0);
2871 KMP_DEBUG_ASSERT(deque_ntasks);
2872 int target = thread_data->td.td_deque_head;
2873 current = __kmp_threads[gtid]->th.th_current_task;
2874 taskdata = thread_data->td.td_deque[target];
2875 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
2877 thread_data->td.td_deque_head =
2878 (target + 1) & TASK_DEQUE_MASK(thread_data->td);
2880 if (!task_team->tt.tt_untied_task_encountered) {
2882 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2883 KA_TRACE(20, (
"__kmp_get_priority_task(exit #3): T#%d could not get task "
2884 "from %p: task_team=%p ntasks=%d head=%u tail=%u\n",
2885 gtid, thread_data, task_team, deque_ntasks, target,
2886 thread_data->td.td_deque_tail));
2887 task_team->tt.tt_num_task_pri++;
2893 for (i = 1; i < deque_ntasks; ++i) {
2894 target = (target + 1) & TASK_DEQUE_MASK(thread_data->td);
2895 taskdata = thread_data->td.td_deque[target];
2896 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
2902 if (taskdata == NULL) {
2904 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2906 10, (
"__kmp_get_priority_task(exit #4): T#%d could not get task from "
2907 "%p: task_team=%p ntasks=%d head=%u tail=%u\n",
2908 gtid, thread_data, task_team, deque_ntasks,
2909 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2910 task_team->tt.tt_num_task_pri++;
2914 for (i = i + 1; i < deque_ntasks; ++i) {
2916 target = (target + 1) & TASK_DEQUE_MASK(thread_data->td);
2917 thread_data->td.td_deque[prev] = thread_data->td.td_deque[target];
2921 thread_data->td.td_deque_tail ==
2922 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(thread_data->td)));
2923 thread_data->td.td_deque_tail = target;
2925 thread_data->td.td_deque_ntasks = deque_ntasks - 1;
2926 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2927 task = KMP_TASKDATA_TO_TASK(taskdata);
2932 static kmp_task_t *__kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid,
2933 kmp_task_team_t *task_team,
2934 kmp_int32 is_constrained) {
2936 kmp_taskdata_t *taskdata;
2937 kmp_thread_data_t *thread_data;
2940 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2941 KMP_DEBUG_ASSERT(task_team->tt.tt_threads_data !=
2944 thread_data = &task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
2946 KA_TRACE(10, (
"__kmp_remove_my_task(enter): T#%d ntasks=%d head=%u tail=%u\n",
2947 gtid, thread_data->td.td_deque_ntasks,
2948 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2950 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2952 (
"__kmp_remove_my_task(exit #1): T#%d No tasks to remove: "
2953 "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));
2959 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
2961 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2962 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2964 (
"__kmp_remove_my_task(exit #2): T#%d No tasks to remove: "
2965 "ntasks=%d head=%u tail=%u\n",
2966 gtid, thread_data->td.td_deque_ntasks,
2967 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2971 tail = (thread_data->td.td_deque_tail - 1) &
2972 TASK_DEQUE_MASK(thread_data->td);
2973 taskdata = thread_data->td.td_deque[tail];
2975 if (!__kmp_task_is_allowed(gtid, is_constrained, taskdata,
2976 thread->th.th_current_task)) {
2978 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2980 (
"__kmp_remove_my_task(exit #3): T#%d TSC blocks tail task: "
2981 "ntasks=%d head=%u tail=%u\n",
2982 gtid, thread_data->td.td_deque_ntasks,
2983 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2987 thread_data->td.td_deque_tail = tail;
2988 TCW_4(thread_data->td.td_deque_ntasks, thread_data->td.td_deque_ntasks - 1);
2990 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2992 KA_TRACE(10, (
"__kmp_remove_my_task(exit #4): T#%d task %p removed: "
2993 "ntasks=%d head=%u tail=%u\n",
2994 gtid, taskdata, thread_data->td.td_deque_ntasks,
2995 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2997 task = KMP_TASKDATA_TO_TASK(taskdata);
3004 static kmp_task_t *__kmp_steal_task(kmp_info_t *victim_thr, kmp_int32 gtid,
3005 kmp_task_team_t *task_team,
3006 std::atomic<kmp_int32> *unfinished_threads,
3007 int *thread_finished,
3008 kmp_int32 is_constrained) {
3010 kmp_taskdata_t *taskdata;
3011 kmp_taskdata_t *current;
3012 kmp_thread_data_t *victim_td, *threads_data;
3014 kmp_int32 victim_tid;
3016 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3018 threads_data = task_team->tt.tt_threads_data;
3019 KMP_DEBUG_ASSERT(threads_data != NULL);
3021 victim_tid = victim_thr->th.th_info.ds.ds_tid;
3022 victim_td = &threads_data[victim_tid];
3024 KA_TRACE(10, (
"__kmp_steal_task(enter): T#%d try to steal from T#%d: "
3025 "task_team=%p ntasks=%d head=%u tail=%u\n",
3026 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
3027 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
3028 victim_td->td.td_deque_tail));
3030 if (TCR_4(victim_td->td.td_deque_ntasks) == 0) {
3031 KA_TRACE(10, (
"__kmp_steal_task(exit #1): T#%d could not steal from T#%d: "
3032 "task_team=%p ntasks=%d head=%u tail=%u\n",
3033 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
3034 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
3035 victim_td->td.td_deque_tail));
3039 __kmp_acquire_bootstrap_lock(&victim_td->td.td_deque_lock);
3041 int ntasks = TCR_4(victim_td->td.td_deque_ntasks);
3044 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3045 KA_TRACE(10, (
"__kmp_steal_task(exit #2): T#%d could not steal from T#%d: "
3046 "task_team=%p ntasks=%d head=%u tail=%u\n",
3047 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3048 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3052 KMP_DEBUG_ASSERT(victim_td->td.td_deque != NULL);
3053 current = __kmp_threads[gtid]->th.th_current_task;
3054 taskdata = victim_td->td.td_deque[victim_td->td.td_deque_head];
3055 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3057 victim_td->td.td_deque_head =
3058 (victim_td->td.td_deque_head + 1) & TASK_DEQUE_MASK(victim_td->td);
3060 if (!task_team->tt.tt_untied_task_encountered) {
3062 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3063 KA_TRACE(10, (
"__kmp_steal_task(exit #3): T#%d could not steal from "
3064 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
3065 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3066 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3071 target = victim_td->td.td_deque_head;
3073 for (i = 1; i < ntasks; ++i) {
3074 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
3075 taskdata = victim_td->td.td_deque[target];
3076 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3082 if (taskdata == NULL) {
3084 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3085 KA_TRACE(10, (
"__kmp_steal_task(exit #4): T#%d could not steal from "
3086 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
3087 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3088 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3092 for (i = i + 1; i < ntasks; ++i) {
3094 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
3095 victim_td->td.td_deque[prev] = victim_td->td.td_deque[target];
3099 victim_td->td.td_deque_tail ==
3100 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(victim_td->td)));
3101 victim_td->td.td_deque_tail = target;
3103 if (*thread_finished) {
3110 KMP_ATOMIC_INC(unfinished_threads);
3113 (
"__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n",
3114 gtid, count + 1, task_team));
3115 *thread_finished = FALSE;
3117 TCW_4(victim_td->td.td_deque_ntasks, ntasks - 1);
3119 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3123 (
"__kmp_steal_task(exit #5): T#%d stole task %p from T#%d: "
3124 "task_team=%p ntasks=%d head=%u tail=%u\n",
3125 gtid, taskdata, __kmp_gtid_from_thread(victim_thr), task_team,
3126 ntasks, victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3128 task = KMP_TASKDATA_TO_TASK(taskdata);
3142 static inline int __kmp_execute_tasks_template(
3143 kmp_info_t *thread, kmp_int32 gtid, C *flag,
int final_spin,
3144 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3145 kmp_int32 is_constrained) {
3146 kmp_task_team_t *task_team = thread->th.th_task_team;
3147 kmp_thread_data_t *threads_data;
3149 kmp_info_t *other_thread;
3150 kmp_taskdata_t *current_task = thread->th.th_current_task;
3151 std::atomic<kmp_int32> *unfinished_threads;
3152 kmp_int32 nthreads, victim_tid = -2, use_own_tasks = 1, new_victim = 0,
3153 tid = thread->th.th_info.ds.ds_tid;
3155 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3156 KMP_DEBUG_ASSERT(thread == __kmp_threads[gtid]);
3158 if (task_team == NULL || current_task == NULL)
3161 KA_TRACE(15, (
"__kmp_execute_tasks_template(enter): T#%d final_spin=%d "
3162 "*thread_finished=%d\n",
3163 gtid, final_spin, *thread_finished));
3165 thread->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
3166 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
3168 KMP_DEBUG_ASSERT(threads_data != NULL);
3170 nthreads = task_team->tt.tt_nproc;
3171 unfinished_threads = &(task_team->tt.tt_unfinished_threads);
3172 KMP_DEBUG_ASSERT(nthreads > 1 || task_team->tt.tt_found_proxy_tasks ||
3173 task_team->tt.tt_hidden_helper_task_encountered);
3174 KMP_DEBUG_ASSERT(*unfinished_threads >= 0);
3180 if (task_team->tt.tt_num_task_pri) {
3181 task = __kmp_get_priority_task(gtid, task_team, is_constrained);
3183 if (task == NULL && use_own_tasks) {
3184 task = __kmp_remove_my_task(thread, gtid, task_team, is_constrained);
3186 if ((task == NULL) && (nthreads > 1)) {
3190 if (victim_tid == -2) {
3191 victim_tid = threads_data[tid].td.td_deque_last_stolen;
3194 other_thread = threads_data[victim_tid].td.td_thr;
3196 if (victim_tid != -1) {
3198 }
else if (!new_victim) {
3204 victim_tid = __kmp_get_random(thread) % (nthreads - 1);
3205 if (victim_tid >= tid) {
3209 other_thread = threads_data[victim_tid].td.td_thr;
3219 if ((__kmp_tasking_mode == tskm_task_teams) &&
3220 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) &&
3221 (TCR_PTR(CCAST(
void *, other_thread->th.th_sleep_loc)) !=
3224 __kmp_null_resume_wrapper(other_thread);
3237 task = __kmp_steal_task(other_thread, gtid, task_team,
3238 unfinished_threads, thread_finished,
3242 if (threads_data[tid].td.td_deque_last_stolen != victim_tid) {
3243 threads_data[tid].td.td_deque_last_stolen = victim_tid;
3250 KMP_CHECK_UPDATE(threads_data[tid].td.td_deque_last_stolen, -1);
3259 #if USE_ITT_BUILD && USE_ITT_NOTIFY
3260 if (__itt_sync_create_ptr || KMP_ITT_DEBUG) {
3261 if (itt_sync_obj == NULL) {
3263 itt_sync_obj = __kmp_itt_barrier_object(gtid, bs_forkjoin_barrier);
3265 __kmp_itt_task_starting(itt_sync_obj);
3268 __kmp_invoke_task(gtid, task, current_task);
3270 if (itt_sync_obj != NULL)
3271 __kmp_itt_task_finished(itt_sync_obj);
3278 if (flag == NULL || (!final_spin && flag->done_check())) {
3281 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3285 if (thread->th.th_task_team == NULL) {
3288 KMP_YIELD(__kmp_library == library_throughput);
3291 if (!use_own_tasks && TCR_4(threads_data[tid].td.td_deque_ntasks) != 0) {
3292 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d stolen task spawned "
3293 "other tasks, restart\n",
3304 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks) == 0) {
3308 if (!*thread_finished) {
3310 kmp_int32 count = -1 +
3312 KMP_ATOMIC_DEC(unfinished_threads);
3313 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d dec "
3314 "unfinished_threads to %d task_team=%p\n",
3315 gtid, count, task_team));
3316 *thread_finished = TRUE;
3324 if (flag != NULL && flag->done_check()) {
3327 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3335 if (thread->th.th_task_team == NULL) {
3337 (
"__kmp_execute_tasks_template: T#%d no more tasks\n", gtid));
3346 if (flag == NULL || (!final_spin && flag->done_check())) {
3348 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3355 if (nthreads == 1 &&
3356 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks))
3360 (
"__kmp_execute_tasks_template: T#%d can't find work\n", gtid));
3366 template <
bool C,
bool S>
3367 int __kmp_execute_tasks_32(
3368 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32<C, S> *flag,
int final_spin,
3369 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3370 kmp_int32 is_constrained) {
3371 return __kmp_execute_tasks_template(
3372 thread, gtid, flag, final_spin,
3373 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3376 template <
bool C,
bool S>
3377 int __kmp_execute_tasks_64(
3378 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64<C, S> *flag,
int final_spin,
3379 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3380 kmp_int32 is_constrained) {
3381 return __kmp_execute_tasks_template(
3382 thread, gtid, flag, final_spin,
3383 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3386 template <
bool C,
bool S>
3387 int __kmp_atomic_execute_tasks_64(
3388 kmp_info_t *thread, kmp_int32 gtid, kmp_atomic_flag_64<C, S> *flag,
3389 int final_spin,
int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3390 kmp_int32 is_constrained) {
3391 return __kmp_execute_tasks_template(
3392 thread, gtid, flag, final_spin,
3393 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3396 int __kmp_execute_tasks_oncore(
3397 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag,
int final_spin,
3398 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3399 kmp_int32 is_constrained) {
3400 return __kmp_execute_tasks_template(
3401 thread, gtid, flag, final_spin,
3402 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3406 __kmp_execute_tasks_32<false, false>(kmp_info_t *, kmp_int32,
3407 kmp_flag_32<false, false> *,
int,
3408 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3410 template int __kmp_execute_tasks_64<false, true>(kmp_info_t *, kmp_int32,
3411 kmp_flag_64<false, true> *,
3413 int *USE_ITT_BUILD_ARG(
void *),
3416 template int __kmp_execute_tasks_64<true, false>(kmp_info_t *, kmp_int32,
3417 kmp_flag_64<true, false> *,
3419 int *USE_ITT_BUILD_ARG(
void *),
3422 template int __kmp_atomic_execute_tasks_64<false, true>(
3423 kmp_info_t *, kmp_int32, kmp_atomic_flag_64<false, true> *,
int,
3424 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3426 template int __kmp_atomic_execute_tasks_64<true, false>(
3427 kmp_info_t *, kmp_int32, kmp_atomic_flag_64<true, false> *,
int,
3428 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3433 static void __kmp_enable_tasking(kmp_task_team_t *task_team,
3434 kmp_info_t *this_thr) {
3435 kmp_thread_data_t *threads_data;
3436 int nthreads, i, is_init_thread;
3438 KA_TRACE(10, (
"__kmp_enable_tasking(enter): T#%d\n",
3439 __kmp_gtid_from_thread(this_thr)));
3441 KMP_DEBUG_ASSERT(task_team != NULL);
3442 KMP_DEBUG_ASSERT(this_thr->th.th_team != NULL);
3444 nthreads = task_team->tt.tt_nproc;
3445 KMP_DEBUG_ASSERT(nthreads > 0);
3446 KMP_DEBUG_ASSERT(nthreads == this_thr->th.th_team->t.t_nproc);
3449 is_init_thread = __kmp_realloc_task_threads_data(this_thr, task_team);
3451 if (!is_init_thread) {
3455 (
"__kmp_enable_tasking(exit): T#%d: threads array already set up.\n",
3456 __kmp_gtid_from_thread(this_thr)));
3459 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
3460 KMP_DEBUG_ASSERT(threads_data != NULL);
3462 if (__kmp_tasking_mode == tskm_task_teams &&
3463 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME)) {
3467 for (i = 0; i < nthreads; i++) {
3469 kmp_info_t *thread = threads_data[i].td.td_thr;
3471 if (i == this_thr->th.th_info.ds.ds_tid) {
3480 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3482 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d waking up thread T#%d\n",
3483 __kmp_gtid_from_thread(this_thr),
3484 __kmp_gtid_from_thread(thread)));
3485 __kmp_null_resume_wrapper(thread);
3487 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d don't wake up thread T#%d\n",
3488 __kmp_gtid_from_thread(this_thr),
3489 __kmp_gtid_from_thread(thread)));
3494 KA_TRACE(10, (
"__kmp_enable_tasking(exit): T#%d\n",
3495 __kmp_gtid_from_thread(this_thr)));
3528 static kmp_task_team_t *__kmp_free_task_teams =
3531 kmp_bootstrap_lock_t __kmp_task_team_lock =
3532 KMP_BOOTSTRAP_LOCK_INITIALIZER(__kmp_task_team_lock);
3539 static void __kmp_alloc_task_deque(kmp_info_t *thread,
3540 kmp_thread_data_t *thread_data) {
3541 __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
3542 KMP_DEBUG_ASSERT(thread_data->td.td_deque == NULL);
3545 thread_data->td.td_deque_last_stolen = -1;
3547 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == 0);
3548 KMP_DEBUG_ASSERT(thread_data->td.td_deque_head == 0);
3549 KMP_DEBUG_ASSERT(thread_data->td.td_deque_tail == 0);
3553 (
"__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n",
3554 __kmp_gtid_from_thread(thread), INITIAL_TASK_DEQUE_SIZE, thread_data));
3558 thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
3559 INITIAL_TASK_DEQUE_SIZE *
sizeof(kmp_taskdata_t *));
3560 thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
3566 static void __kmp_free_task_deque(kmp_thread_data_t *thread_data) {
3567 if (thread_data->td.td_deque != NULL) {
3568 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3569 TCW_4(thread_data->td.td_deque_ntasks, 0);
3570 __kmp_free(thread_data->td.td_deque);
3571 thread_data->td.td_deque = NULL;
3572 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3575 #ifdef BUILD_TIED_TASK_STACK
3577 if (thread_data->td.td_susp_tied_tasks.ts_entries != TASK_STACK_EMPTY) {
3578 __kmp_free_task_stack(__kmp_thread_from_gtid(gtid), thread_data);
3590 static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
3591 kmp_task_team_t *task_team) {
3592 kmp_thread_data_t **threads_data_p;
3593 kmp_int32 nthreads, maxthreads;
3594 int is_init_thread = FALSE;
3596 if (TCR_4(task_team->tt.tt_found_tasks)) {
3601 threads_data_p = &task_team->tt.tt_threads_data;
3602 nthreads = task_team->tt.tt_nproc;
3603 maxthreads = task_team->tt.tt_max_threads;
3608 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3610 if (!TCR_4(task_team->tt.tt_found_tasks)) {
3612 kmp_team_t *team = thread->th.th_team;
3615 is_init_thread = TRUE;
3616 if (maxthreads < nthreads) {
3618 if (*threads_data_p != NULL) {
3619 kmp_thread_data_t *old_data = *threads_data_p;
3620 kmp_thread_data_t *new_data = NULL;
3624 (
"__kmp_realloc_task_threads_data: T#%d reallocating "
3625 "threads data for task_team %p, new_size = %d, old_size = %d\n",
3626 __kmp_gtid_from_thread(thread), task_team, nthreads, maxthreads));
3631 new_data = (kmp_thread_data_t *)__kmp_allocate(
3632 nthreads *
sizeof(kmp_thread_data_t));
3634 KMP_MEMCPY_S((
void *)new_data, nthreads *
sizeof(kmp_thread_data_t),
3635 (
void *)old_data, maxthreads *
sizeof(kmp_thread_data_t));
3637 #ifdef BUILD_TIED_TASK_STACK
3639 for (i = maxthreads; i < nthreads; i++) {
3640 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3641 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3645 (*threads_data_p) = new_data;
3646 __kmp_free(old_data);
3648 KE_TRACE(10, (
"__kmp_realloc_task_threads_data: T#%d allocating "
3649 "threads data for task_team %p, size = %d\n",
3650 __kmp_gtid_from_thread(thread), task_team, nthreads));
3654 *threads_data_p = (kmp_thread_data_t *)__kmp_allocate(
3655 nthreads *
sizeof(kmp_thread_data_t));
3656 #ifdef BUILD_TIED_TASK_STACK
3658 for (i = 0; i < nthreads; i++) {
3659 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3660 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3664 task_team->tt.tt_max_threads = nthreads;
3667 KMP_DEBUG_ASSERT(*threads_data_p != NULL);
3671 for (i = 0; i < nthreads; i++) {
3672 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3673 thread_data->td.td_thr = team->t.t_threads[i];
3675 if (thread_data->td.td_deque_last_stolen >= nthreads) {
3679 thread_data->td.td_deque_last_stolen = -1;
3684 TCW_SYNC_4(task_team->tt.tt_found_tasks, TRUE);
3687 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3688 return is_init_thread;
3694 static void __kmp_free_task_threads_data(kmp_task_team_t *task_team) {
3695 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3696 if (task_team->tt.tt_threads_data != NULL) {
3698 for (i = 0; i < task_team->tt.tt_max_threads; i++) {
3699 __kmp_free_task_deque(&task_team->tt.tt_threads_data[i]);
3701 __kmp_free(task_team->tt.tt_threads_data);
3702 task_team->tt.tt_threads_data = NULL;
3704 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3710 static void __kmp_free_task_pri_list(kmp_task_team_t *task_team) {
3711 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
3712 if (task_team->tt.tt_task_pri_list != NULL) {
3713 kmp_task_pri_t *list = task_team->tt.tt_task_pri_list;
3714 while (list != NULL) {
3715 kmp_task_pri_t *next = list->next;
3716 __kmp_free_task_deque(&list->td);
3720 task_team->tt.tt_task_pri_list = NULL;
3722 __kmp_release_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
3729 static kmp_task_team_t *__kmp_allocate_task_team(kmp_info_t *thread,
3731 kmp_task_team_t *task_team = NULL;
3734 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d entering; team = %p\n",
3735 (thread ? __kmp_gtid_from_thread(thread) : -1), team));
3737 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3739 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3740 if (__kmp_free_task_teams != NULL) {
3741 task_team = __kmp_free_task_teams;
3742 TCW_PTR(__kmp_free_task_teams, task_team->tt.tt_next);
3743 task_team->tt.tt_next = NULL;
3745 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3748 if (task_team == NULL) {
3749 KE_TRACE(10, (
"__kmp_allocate_task_team: T#%d allocating "
3750 "task team for team %p\n",
3751 __kmp_gtid_from_thread(thread), team));
3754 task_team = (kmp_task_team_t *)__kmp_allocate(
sizeof(kmp_task_team_t));
3755 __kmp_init_bootstrap_lock(&task_team->tt.tt_threads_lock);
3756 __kmp_init_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
3757 #if USE_ITT_BUILD && USE_ITT_NOTIFY && KMP_DEBUG
3760 __itt_suppress_mark_range(
3761 __itt_suppress_range, __itt_suppress_threading_errors,
3762 &task_team->tt.tt_found_tasks,
sizeof(task_team->tt.tt_found_tasks));
3763 __itt_suppress_mark_range(__itt_suppress_range,
3764 __itt_suppress_threading_errors,
3765 CCAST(kmp_uint32 *, &task_team->tt.tt_active),
3766 sizeof(task_team->tt.tt_active));
3774 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3775 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3776 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
3777 task_team->tt.tt_nproc = nthreads = team->t.t_nproc;
3779 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads, nthreads);
3780 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
3781 TCW_4(task_team->tt.tt_active, TRUE);
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)));
3793 void __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);
3812 void __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);
3838 void __kmp_wait_to_unref_task_teams(
void) {
3844 KMP_INIT_YIELD(spins);
3845 KMP_INIT_BACKOFF(time);
3853 for (thread = CCAST(kmp_info_t *, __kmp_thread_pool); thread != NULL;
3854 thread = thread->th.th_next_pool) {
3858 if (TCR_PTR(thread->th.th_task_team) == NULL) {
3859 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n",
3860 __kmp_gtid_from_thread(thread)));
3865 if (!__kmp_is_thread_alive(thread, &exit_val)) {
3866 thread->th.th_task_team = NULL;
3873 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: Waiting for T#%d to "
3874 "unreference task_team\n",
3875 __kmp_gtid_from_thread(thread)));
3877 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
3880 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3884 (
"__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n",
3885 __kmp_gtid_from_thread(thread), __kmp_gtid_from_thread(thread)));
3886 __kmp_null_resume_wrapper(thread);
3895 KMP_YIELD_OVERSUB_ELSE_SPIN(spins, time);
3901 void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team,
int always) {
3902 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3908 if (team->t.t_task_team[this_thr->th.th_task_state] == NULL &&
3909 (always || team->t.t_nproc > 1)) {
3910 team->t.t_task_team[this_thr->th.th_task_state] =
3911 __kmp_allocate_task_team(this_thr, team);
3912 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d created new task_team %p"
3913 " for team %d at parity=%d\n",
3914 __kmp_gtid_from_thread(this_thr),
3915 team->t.t_task_team[this_thr->th.th_task_state], team->t.t_id,
3916 this_thr->th.th_task_state));
3926 if (team->t.t_nproc > 1) {
3927 int other_team = 1 - this_thr->th.th_task_state;
3928 KMP_DEBUG_ASSERT(other_team >= 0 && other_team < 2);
3929 if (team->t.t_task_team[other_team] == NULL) {
3930 team->t.t_task_team[other_team] =
3931 __kmp_allocate_task_team(this_thr, team);
3932 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d created second new "
3933 "task_team %p for team %d at parity=%d\n",
3934 __kmp_gtid_from_thread(this_thr),
3935 team->t.t_task_team[other_team], team->t.t_id, other_team));
3938 kmp_task_team_t *task_team = team->t.t_task_team[other_team];
3939 if (!task_team->tt.tt_active ||
3940 team->t.t_nproc != task_team->tt.tt_nproc) {
3941 TCW_4(task_team->tt.tt_nproc, team->t.t_nproc);
3942 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3943 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3944 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
3945 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads,
3947 TCW_4(task_team->tt.tt_active, TRUE);
3951 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d reset next task_team "
3952 "%p for team %d at parity=%d\n",
3953 __kmp_gtid_from_thread(this_thr),
3954 team->t.t_task_team[other_team], team->t.t_id, other_team));
3962 if (this_thr == __kmp_hidden_helper_main_thread) {
3963 for (
int i = 0; i < 2; ++i) {
3964 kmp_task_team_t *task_team = team->t.t_task_team[i];
3965 if (KMP_TASKING_ENABLED(task_team)) {
3968 __kmp_enable_tasking(task_team, this_thr);
3969 for (
int j = 0; j < task_team->tt.tt_nproc; ++j) {
3970 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[j];
3971 if (thread_data->td.td_deque == NULL) {
3972 __kmp_alloc_task_deque(__kmp_hidden_helper_threads[j], thread_data);
3982 void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team) {
3983 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3987 this_thr->th.th_task_state = (kmp_uint8)(1 - this_thr->th.th_task_state);
3991 TCW_PTR(this_thr->th.th_task_team,
3992 team->t.t_task_team[this_thr->th.th_task_state]);
3994 (
"__kmp_task_team_sync: Thread T#%d task team switched to task_team "
3995 "%p from Team #%d (parity=%d)\n",
3996 __kmp_gtid_from_thread(this_thr), this_thr->th.th_task_team,
3997 team->t.t_id, this_thr->th.th_task_state));
4007 void __kmp_task_team_wait(
4008 kmp_info_t *this_thr,
4009 kmp_team_t *team USE_ITT_BUILD_ARG(
void *itt_sync_obj),
int wait) {
4010 kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state];
4012 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
4013 KMP_DEBUG_ASSERT(task_team == this_thr->th.th_task_team);
4015 if ((task_team != NULL) && KMP_TASKING_ENABLED(task_team)) {
4017 KA_TRACE(20, (
"__kmp_task_team_wait: Primary T#%d waiting for all tasks "
4018 "(for unfinished_threads to reach 0) on task_team = %p\n",
4019 __kmp_gtid_from_thread(this_thr), task_team));
4023 kmp_flag_32<false, false> flag(
4024 RCAST(std::atomic<kmp_uint32> *,
4025 &task_team->tt.tt_unfinished_threads),
4027 flag.wait(this_thr, TRUE USE_ITT_BUILD_ARG(itt_sync_obj));
4033 (
"__kmp_task_team_wait: Primary T#%d deactivating task_team %p: "
4034 "setting active to false, setting local and team's pointer to NULL\n",
4035 __kmp_gtid_from_thread(this_thr), task_team));
4036 KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1 ||
4037 task_team->tt.tt_found_proxy_tasks == TRUE ||
4038 task_team->tt.tt_hidden_helper_task_encountered == TRUE);
4039 TCW_SYNC_4(task_team->tt.tt_found_proxy_tasks, FALSE);
4040 TCW_SYNC_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
4041 KMP_CHECK_UPDATE(task_team->tt.tt_untied_task_encountered, 0);
4042 TCW_SYNC_4(task_team->tt.tt_active, FALSE);
4045 TCW_PTR(this_thr->th.th_task_team, NULL);
4054 void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread,
int gtid) {
4055 std::atomic<kmp_uint32> *spin = RCAST(
4056 std::atomic<kmp_uint32> *,
4057 &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads);
4059 KMP_DEBUG_ASSERT(__kmp_tasking_mode == tskm_extra_barrier);
4062 KMP_FSYNC_SPIN_INIT(spin, NULL);
4064 kmp_flag_32<false, false> spin_flag(spin, 0U);
4065 while (!spin_flag.execute_tasks(thread, gtid, TRUE,
4066 &flag USE_ITT_BUILD_ARG(NULL), 0)) {
4069 KMP_FSYNC_SPIN_PREPARE(RCAST(
void *, spin));
4072 if (TCR_4(__kmp_global.g.g_done)) {
4073 if (__kmp_global.g.g_abort)
4074 __kmp_abort_thread();
4080 KMP_FSYNC_SPIN_ACQUIRED(RCAST(
void *, spin));
4089 static bool __kmp_give_task(kmp_info_t *thread, kmp_int32 tid, kmp_task_t *task,
4091 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4092 kmp_task_team_t *task_team = taskdata->td_task_team;
4094 KA_TRACE(20, (
"__kmp_give_task: trying to give task %p to thread %d.\n",
4098 KMP_DEBUG_ASSERT(task_team != NULL);
4100 bool result =
false;
4101 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
4103 if (thread_data->td.td_deque == NULL) {
4107 (
"__kmp_give_task: thread %d has no queue while giving task %p.\n",
4112 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4113 TASK_DEQUE_SIZE(thread_data->td)) {
4116 (
"__kmp_give_task: queue is full while giving task %p to thread %d.\n",
4121 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
4124 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
4125 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4126 TASK_DEQUE_SIZE(thread_data->td)) {
4128 __kmp_realloc_task_deque(thread, thread_data);
4133 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
4135 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4136 TASK_DEQUE_SIZE(thread_data->td)) {
4137 KA_TRACE(30, (
"__kmp_give_task: queue is full while giving task %p to "
4143 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
4144 goto release_and_exit;
4146 __kmp_realloc_task_deque(thread, thread_data);
4152 thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
4154 thread_data->td.td_deque_tail =
4155 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
4156 TCW_4(thread_data->td.td_deque_ntasks,
4157 TCR_4(thread_data->td.td_deque_ntasks) + 1);
4160 KA_TRACE(30, (
"__kmp_give_task: successfully gave task %p to thread %d.\n",
4164 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
4169 #define PROXY_TASK_FLAG 0x40000000
4186 static void __kmp_first_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
4187 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
4188 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4189 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
4190 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
4192 taskdata->td_flags.complete = 1;
4194 if (taskdata->td_taskgroup)
4195 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
4199 KMP_ATOMIC_OR(&taskdata->td_incomplete_child_tasks, PROXY_TASK_FLAG);
4202 static void __kmp_second_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
4204 kmp_int32 children = 0;
4208 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks);
4209 KMP_DEBUG_ASSERT(children >= 0);
4212 KMP_ATOMIC_AND(&taskdata->td_incomplete_child_tasks, ~PROXY_TASK_FLAG);
4215 static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask) {
4216 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4217 kmp_info_t *thread = __kmp_threads[gtid];
4219 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4220 KMP_DEBUG_ASSERT(taskdata->td_flags.complete ==
4225 while ((KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) &
4226 PROXY_TASK_FLAG) > 0)
4229 __kmp_release_deps(gtid, taskdata);
4230 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
4242 KMP_DEBUG_ASSERT(ptask != NULL);
4243 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4245 10, (
"__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n",
4247 __kmp_assert_valid_gtid(gtid);
4248 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4250 __kmp_first_top_half_finish_proxy(taskdata);
4251 __kmp_second_top_half_finish_proxy(taskdata);
4252 __kmp_bottom_half_finish_proxy(gtid, ptask);
4255 (
"__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n",
4259 void __kmpc_give_task(kmp_task_t *ptask, kmp_int32 start = 0) {
4260 KMP_DEBUG_ASSERT(ptask != NULL);
4261 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4265 kmp_team_t *team = taskdata->td_team;
4266 kmp_int32 nthreads = team->t.t_nproc;
4271 kmp_int32 start_k = start % nthreads;
4273 kmp_int32 k = start_k;
4277 thread = team->t.t_threads[k];
4278 k = (k + 1) % nthreads;
4284 }
while (!__kmp_give_task(thread, k, ptask, pass));
4295 KMP_DEBUG_ASSERT(ptask != NULL);
4296 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4300 (
"__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n",
4303 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4305 __kmp_first_top_half_finish_proxy(taskdata);
4307 __kmpc_give_task(ptask);
4309 __kmp_second_top_half_finish_proxy(taskdata);
4313 (
"__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n",
4317 kmp_event_t *__kmpc_task_allow_completion_event(
ident_t *loc_ref,
int gtid,
4319 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(task);
4320 if (td->td_allow_completion_event.type == KMP_EVENT_UNINITIALIZED) {
4321 td->td_allow_completion_event.type = KMP_EVENT_ALLOW_COMPLETION;
4322 td->td_allow_completion_event.ed.task = task;
4323 __kmp_init_tas_lock(&td->td_allow_completion_event.lock);
4325 return &td->td_allow_completion_event;
4328 void __kmp_fulfill_event(kmp_event_t *event) {
4329 if (event->type == KMP_EVENT_ALLOW_COMPLETION) {
4330 kmp_task_t *ptask = event->ed.task;
4331 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4332 bool detached =
false;
4333 int gtid = __kmp_get_gtid();
4338 __kmp_acquire_tas_lock(&event->lock, gtid);
4339 if (taskdata->td_flags.proxy == TASK_PROXY) {
4345 if (UNLIKELY(ompt_enabled.enabled))
4346 __ompt_task_finish(ptask, NULL, ompt_task_early_fulfill);
4349 event->type = KMP_EVENT_UNINITIALIZED;
4350 __kmp_release_tas_lock(&event->lock, gtid);
4356 if (UNLIKELY(ompt_enabled.enabled))
4357 __ompt_task_finish(ptask, NULL, ompt_task_late_fulfill);
4361 kmp_team_t *team = taskdata->td_team;
4362 kmp_info_t *thread = __kmp_get_thread();
4363 if (thread->th.th_team == team) {
4381 kmp_task_t *__kmp_task_dup_alloc(kmp_info_t *thread, kmp_task_t *task_src) {
4383 kmp_taskdata_t *taskdata;
4384 kmp_taskdata_t *taskdata_src = KMP_TASK_TO_TASKDATA(task_src);
4385 kmp_taskdata_t *parent_task = taskdata_src->td_parent;
4386 size_t shareds_offset;
4389 KA_TRACE(10, (
"__kmp_task_dup_alloc(enter): Th %p, source task %p\n", thread,
4391 KMP_DEBUG_ASSERT(taskdata_src->td_flags.proxy ==
4393 KMP_DEBUG_ASSERT(taskdata_src->td_flags.tasktype == TASK_EXPLICIT);
4394 task_size = taskdata_src->td_size_alloc;
4397 KA_TRACE(30, (
"__kmp_task_dup_alloc: Th %p, malloc size %ld\n", thread,
4400 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, task_size);
4402 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, task_size);
4404 KMP_MEMCPY(taskdata, taskdata_src, task_size);
4406 task = KMP_TASKDATA_TO_TASK(taskdata);
4409 taskdata->td_task_id = KMP_GEN_TASK_ID();
4410 if (task->shareds != NULL) {
4411 shareds_offset = (
char *)task_src->shareds - (
char *)taskdata_src;
4412 task->shareds = &((
char *)taskdata)[shareds_offset];
4413 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
4416 taskdata->td_alloc_thread = thread;
4417 taskdata->td_parent = parent_task;
4419 taskdata->td_taskgroup = parent_task->td_taskgroup;
4422 if (taskdata->td_flags.tiedness == TASK_TIED)
4423 taskdata->td_last_tied = taskdata;
4427 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
4428 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
4429 if (parent_task->td_taskgroup)
4430 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
4433 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT)
4434 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
4438 (
"__kmp_task_dup_alloc(exit): Th %p, created task %p, parent=%p\n",
4439 thread, taskdata, taskdata->td_parent));
4441 if (UNLIKELY(ompt_enabled.enabled))
4442 __ompt_task_init(taskdata, thread->th.th_info.ds.ds_gtid);
4451 typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32);
4453 KMP_BUILD_ASSERT(
sizeof(
long) == 4 ||
sizeof(
long) == 8);
4458 class kmp_taskloop_bounds_t {
4460 const kmp_taskdata_t *taskdata;
4461 size_t lower_offset;
4462 size_t upper_offset;
4465 kmp_taskloop_bounds_t(kmp_task_t *_task, kmp_uint64 *lb, kmp_uint64 *ub)
4466 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(task)),
4467 lower_offset((char *)lb - (char *)task),
4468 upper_offset((char *)ub - (char *)task) {
4469 KMP_DEBUG_ASSERT((
char *)lb > (
char *)_task);
4470 KMP_DEBUG_ASSERT((
char *)ub > (
char *)_task);
4472 kmp_taskloop_bounds_t(kmp_task_t *_task,
const kmp_taskloop_bounds_t &bounds)
4473 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(_task)),
4474 lower_offset(bounds.lower_offset), upper_offset(bounds.upper_offset) {}
4475 size_t get_lower_offset()
const {
return lower_offset; }
4476 size_t get_upper_offset()
const {
return upper_offset; }
4477 kmp_uint64 get_lb()
const {
4479 #if defined(KMP_GOMP_COMPAT)
4481 if (!taskdata->td_flags.native) {
4482 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4485 if (taskdata->td_size_loop_bounds == 4) {
4486 kmp_int32 *lb = RCAST(kmp_int32 *, task->shareds);
4487 retval = (kmp_int64)*lb;
4489 kmp_int64 *lb = RCAST(kmp_int64 *, task->shareds);
4490 retval = (kmp_int64)*lb;
4495 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4499 kmp_uint64 get_ub()
const {
4501 #if defined(KMP_GOMP_COMPAT)
4503 if (!taskdata->td_flags.native) {
4504 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4507 if (taskdata->td_size_loop_bounds == 4) {
4508 kmp_int32 *ub = RCAST(kmp_int32 *, task->shareds) + 1;
4509 retval = (kmp_int64)*ub;
4511 kmp_int64 *ub = RCAST(kmp_int64 *, task->shareds) + 1;
4512 retval = (kmp_int64)*ub;
4516 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4520 void set_lb(kmp_uint64 lb) {
4521 #if defined(KMP_GOMP_COMPAT)
4523 if (!taskdata->td_flags.native) {
4524 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4527 if (taskdata->td_size_loop_bounds == 4) {
4528 kmp_uint32 *lower = RCAST(kmp_uint32 *, task->shareds);
4529 *lower = (kmp_uint32)lb;
4531 kmp_uint64 *lower = RCAST(kmp_uint64 *, task->shareds);
4532 *lower = (kmp_uint64)lb;
4536 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4539 void set_ub(kmp_uint64 ub) {
4540 #if defined(KMP_GOMP_COMPAT)
4542 if (!taskdata->td_flags.native) {
4543 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4546 if (taskdata->td_size_loop_bounds == 4) {
4547 kmp_uint32 *upper = RCAST(kmp_uint32 *, task->shareds) + 1;
4548 *upper = (kmp_uint32)ub;
4550 kmp_uint64 *upper = RCAST(kmp_uint64 *, task->shareds) + 1;
4551 *upper = (kmp_uint64)ub;
4555 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4576 void __kmp_taskloop_linear(
ident_t *loc,
int gtid, kmp_task_t *task,
4577 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4578 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
4579 kmp_uint64 grainsize, kmp_uint64 extras,
4580 kmp_int64 last_chunk, kmp_uint64 tc,
4586 KMP_TIME_PARTITIONED_BLOCK(OMP_taskloop_scheduling);
4587 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
4589 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4590 kmp_uint64 lower = task_bounds.get_lb();
4591 kmp_uint64 upper = task_bounds.get_ub();
4593 kmp_info_t *thread = __kmp_threads[gtid];
4594 kmp_taskdata_t *current_task = thread->th.th_current_task;
4595 kmp_task_t *next_task;
4596 kmp_int32 lastpriv = 0;
4598 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4599 (last_chunk < 0 ? last_chunk : extras));
4600 KMP_DEBUG_ASSERT(num_tasks > extras);
4601 KMP_DEBUG_ASSERT(num_tasks > 0);
4602 KA_TRACE(20, (
"__kmp_taskloop_linear: T#%d: %lld tasks, grainsize %lld, "
4603 "extras %lld, last_chunk %lld, i=%lld,%lld(%d)%lld, dup %p\n",
4604 gtid, num_tasks, grainsize, extras, last_chunk, lower, upper,
4605 ub_glob, st, task_dup));
4608 for (i = 0; i < num_tasks; ++i) {
4609 kmp_uint64 chunk_minus_1;
4611 chunk_minus_1 = grainsize - 1;
4613 chunk_minus_1 = grainsize;
4616 upper = lower + st * chunk_minus_1;
4620 if (i == num_tasks - 1) {
4623 KMP_DEBUG_ASSERT(upper == *ub);
4624 if (upper == ub_glob)
4626 }
else if (st > 0) {
4627 KMP_DEBUG_ASSERT((kmp_uint64)st > *ub - upper);
4628 if ((kmp_uint64)st > ub_glob - upper)
4631 KMP_DEBUG_ASSERT(upper + st < *ub);
4632 if (upper - ub_glob < (kmp_uint64)(-st))
4636 next_task = __kmp_task_dup_alloc(thread, task);
4637 kmp_taskdata_t *next_taskdata = KMP_TASK_TO_TASKDATA(next_task);
4638 kmp_taskloop_bounds_t next_task_bounds =
4639 kmp_taskloop_bounds_t(next_task, task_bounds);
4642 next_task_bounds.set_lb(lower);
4643 if (next_taskdata->td_flags.native) {
4644 next_task_bounds.set_ub(upper + (st > 0 ? 1 : -1));
4646 next_task_bounds.set_ub(upper);
4648 if (ptask_dup != NULL)
4650 ptask_dup(next_task, task, lastpriv);
4652 (
"__kmp_taskloop_linear: T#%d; task #%llu: task %p: lower %lld, "
4653 "upper %lld stride %lld, (offsets %p %p)\n",
4654 gtid, i, next_task, lower, upper, st,
4655 next_task_bounds.get_lower_offset(),
4656 next_task_bounds.get_upper_offset()));
4658 __kmp_omp_taskloop_task(NULL, gtid, next_task,
4661 if (ompt_enabled.ompt_callback_dispatch) {
4662 OMPT_GET_DISPATCH_CHUNK(next_taskdata->ompt_task_info.dispatch_chunk,
4667 __kmp_omp_task(gtid, next_task,
true);
4672 __kmp_task_start(gtid, task, current_task);
4674 __kmp_task_finish<false>(gtid, task, current_task);
4679 typedef struct __taskloop_params {
4686 kmp_uint64 num_tasks;
4687 kmp_uint64 grainsize;
4689 kmp_int64 last_chunk;
4691 kmp_uint64 num_t_min;
4695 } __taskloop_params_t;
4697 void __kmp_taskloop_recur(
ident_t *,
int, kmp_task_t *, kmp_uint64 *,
4698 kmp_uint64 *, kmp_int64, kmp_uint64, kmp_uint64,
4699 kmp_uint64, kmp_uint64, kmp_int64, kmp_uint64,
4707 int __kmp_taskloop_task(
int gtid,
void *ptask) {
4708 __taskloop_params_t *p =
4709 (__taskloop_params_t *)((kmp_task_t *)ptask)->shareds;
4710 kmp_task_t *task = p->task;
4711 kmp_uint64 *lb = p->lb;
4712 kmp_uint64 *ub = p->ub;
4713 void *task_dup = p->task_dup;
4715 kmp_int64 st = p->st;
4716 kmp_uint64 ub_glob = p->ub_glob;
4717 kmp_uint64 num_tasks = p->num_tasks;
4718 kmp_uint64 grainsize = p->grainsize;
4719 kmp_uint64 extras = p->extras;
4720 kmp_int64 last_chunk = p->last_chunk;
4721 kmp_uint64 tc = p->tc;
4722 kmp_uint64 num_t_min = p->num_t_min;
4724 void *codeptr_ra = p->codeptr_ra;
4727 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4728 KMP_DEBUG_ASSERT(task != NULL);
4730 (
"__kmp_taskloop_task: T#%d, task %p: %lld tasks, grainsize"
4731 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
4732 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
4735 KMP_DEBUG_ASSERT(num_tasks * 2 + 1 > num_t_min);
4736 if (num_tasks > num_t_min)
4737 __kmp_taskloop_recur(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
4738 grainsize, extras, last_chunk, tc, num_t_min,
4744 __kmp_taskloop_linear(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
4745 grainsize, extras, last_chunk, tc,
4751 KA_TRACE(40, (
"__kmp_taskloop_task(exit): T#%d\n", gtid));
4773 void __kmp_taskloop_recur(
ident_t *loc,
int gtid, kmp_task_t *task,
4774 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4775 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
4776 kmp_uint64 grainsize, kmp_uint64 extras,
4777 kmp_int64 last_chunk, kmp_uint64 tc,
4778 kmp_uint64 num_t_min,
4783 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4784 KMP_DEBUG_ASSERT(task != NULL);
4785 KMP_DEBUG_ASSERT(num_tasks > num_t_min);
4787 (
"__kmp_taskloop_recur: T#%d, task %p: %lld tasks, grainsize"
4788 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
4789 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
4791 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
4792 kmp_uint64 lower = *lb;
4793 kmp_info_t *thread = __kmp_threads[gtid];
4795 kmp_task_t *next_task;
4796 size_t lower_offset =
4797 (
char *)lb - (
char *)task;
4798 size_t upper_offset =
4799 (
char *)ub - (
char *)task;
4801 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4802 (last_chunk < 0 ? last_chunk : extras));
4803 KMP_DEBUG_ASSERT(num_tasks > extras);
4804 KMP_DEBUG_ASSERT(num_tasks > 0);
4807 kmp_uint64 lb1, ub0, tc0, tc1, ext0, ext1;
4808 kmp_int64 last_chunk0 = 0, last_chunk1 = 0;
4809 kmp_uint64 gr_size0 = grainsize;
4810 kmp_uint64 n_tsk0 = num_tasks >> 1;
4811 kmp_uint64 n_tsk1 = num_tasks - n_tsk0;
4812 if (last_chunk < 0) {
4814 last_chunk1 = last_chunk;
4815 tc0 = grainsize * n_tsk0;
4817 }
else if (n_tsk0 <= extras) {
4820 ext1 = extras - n_tsk0;
4821 tc0 = gr_size0 * n_tsk0;
4826 tc1 = grainsize * n_tsk1;
4829 ub0 = lower + st * (tc0 - 1);
4833 next_task = __kmp_task_dup_alloc(thread, task);
4835 *(kmp_uint64 *)((
char *)next_task + lower_offset) = lb1;
4836 if (ptask_dup != NULL)
4837 ptask_dup(next_task, task, 0);
4842 kmp_taskdata_t *current_task = thread->th.th_current_task;
4843 thread->th.th_current_task = taskdata->td_parent;
4844 kmp_task_t *new_task =
4845 __kmpc_omp_task_alloc(loc, gtid, 1, 3 *
sizeof(
void *),
4846 sizeof(__taskloop_params_t), &__kmp_taskloop_task);
4848 thread->th.th_current_task = current_task;
4849 __taskloop_params_t *p = (__taskloop_params_t *)new_task->shareds;
4850 p->task = next_task;
4851 p->lb = (kmp_uint64 *)((
char *)next_task + lower_offset);
4852 p->ub = (kmp_uint64 *)((
char *)next_task + upper_offset);
4853 p->task_dup = task_dup;
4855 p->ub_glob = ub_glob;
4856 p->num_tasks = n_tsk1;
4857 p->grainsize = grainsize;
4859 p->last_chunk = last_chunk1;
4861 p->num_t_min = num_t_min;
4863 p->codeptr_ra = codeptr_ra;
4868 __kmp_omp_taskloop_task(NULL, gtid, new_task, codeptr_ra);
4870 __kmp_omp_task(gtid, new_task,
true);
4874 if (n_tsk0 > num_t_min)
4875 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0, gr_size0,
4876 ext0, last_chunk0, tc0, num_t_min,
4882 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0,
4883 gr_size0, ext0, last_chunk0, tc0,
4889 KA_TRACE(40, (
"__kmp_taskloop_recur(exit): T#%d\n", gtid));
4892 static void __kmp_taskloop(
ident_t *loc,
int gtid, kmp_task_t *task,
int if_val,
4893 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4894 int nogroup,
int sched, kmp_uint64 grainsize,
4895 int modifier,
void *task_dup) {
4896 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4897 KMP_DEBUG_ASSERT(task != NULL);
4899 #if OMPT_SUPPORT && OMPT_OPTIONAL
4900 OMPT_STORE_RETURN_ADDRESS(gtid);
4902 __kmpc_taskgroup(loc, gtid);
4907 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4910 kmp_uint64 lower = task_bounds.get_lb();
4911 kmp_uint64 upper = task_bounds.get_ub();
4912 kmp_uint64 ub_glob = upper;
4913 kmp_uint64 num_tasks = 0, extras = 0;
4914 kmp_int64 last_chunk =
4916 kmp_uint64 num_tasks_min = __kmp_taskloop_min_tasks;
4917 kmp_info_t *thread = __kmp_threads[gtid];
4918 kmp_taskdata_t *current_task = thread->th.th_current_task;
4920 KA_TRACE(20, (
"__kmp_taskloop: T#%d, task %p, lb %lld, ub %lld, st %lld, "
4921 "grain %llu(%d, %d), dup %p\n",
4922 gtid, taskdata, lower, upper, st, grainsize, sched, modifier,
4927 tc = upper - lower + 1;
4928 }
else if (st < 0) {
4929 tc = (lower - upper) / (-st) + 1;
4931 tc = (upper - lower) / st + 1;
4934 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d zero-trip loop\n", gtid));
4936 __kmp_task_start(gtid, task, current_task);
4938 __kmp_task_finish<false>(gtid, task, current_task);
4942 #if OMPT_SUPPORT && OMPT_OPTIONAL
4943 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
4944 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
4945 if (ompt_enabled.ompt_callback_work) {
4946 ompt_callbacks.ompt_callback(ompt_callback_work)(
4947 ompt_work_taskloop, ompt_scope_begin, &(team_info->parallel_data),
4948 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
4952 if (num_tasks_min == 0)
4955 KMP_MIN(thread->th.th_team_nproc * 10, INITIAL_TASK_DEQUE_SIZE);
4961 grainsize = thread->th.th_team_nproc * 10;
4964 if (grainsize > tc) {
4969 num_tasks = grainsize;
4970 grainsize = tc / num_tasks;
4971 extras = tc % num_tasks;
4975 if (grainsize > tc) {
4981 num_tasks = (tc + grainsize - 1) / grainsize;
4982 last_chunk = tc - (num_tasks * grainsize);
4985 num_tasks = tc / grainsize;
4987 grainsize = tc / num_tasks;
4988 extras = tc % num_tasks;
4993 KMP_ASSERT2(0,
"unknown scheduling of taskloop");
4996 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4997 (last_chunk < 0 ? last_chunk : extras));
4998 KMP_DEBUG_ASSERT(num_tasks > extras);
4999 KMP_DEBUG_ASSERT(num_tasks > 0);
5005 taskdata->td_flags.task_serial = 1;
5006 taskdata->td_flags.tiedness = TASK_TIED;
5008 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5009 grainsize, extras, last_chunk, tc,
5011 OMPT_GET_RETURN_ADDRESS(0),
5016 }
else if (num_tasks > num_tasks_min && !taskdata->td_flags.native) {
5017 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go recursive: tc %llu, #tasks %llu"
5018 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
5019 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
5021 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5022 grainsize, extras, last_chunk, tc, num_tasks_min,
5024 OMPT_GET_RETURN_ADDRESS(0),
5028 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go linear: tc %llu, #tasks %llu"
5029 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
5030 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
5032 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5033 grainsize, extras, last_chunk, tc,
5035 OMPT_GET_RETURN_ADDRESS(0),
5040 #if OMPT_SUPPORT && OMPT_OPTIONAL
5041 if (ompt_enabled.ompt_callback_work) {
5042 ompt_callbacks.ompt_callback(ompt_callback_work)(
5043 ompt_work_taskloop, ompt_scope_end, &(team_info->parallel_data),
5044 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
5049 #if OMPT_SUPPORT && OMPT_OPTIONAL
5050 OMPT_STORE_RETURN_ADDRESS(gtid);
5052 __kmpc_end_taskgroup(loc, gtid);
5054 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d\n", gtid));
5074 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
int nogroup,
5075 int sched, kmp_uint64 grainsize,
void *task_dup) {
5076 __kmp_assert_valid_gtid(gtid);
5077 KA_TRACE(20, (
"__kmpc_taskloop(enter): T#%d\n", gtid));
5078 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
5080 KA_TRACE(20, (
"__kmpc_taskloop(exit): T#%d\n", gtid));
5101 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
5102 int nogroup,
int sched, kmp_uint64 grainsize,
5103 int modifier,
void *task_dup) {
5104 __kmp_assert_valid_gtid(gtid);
5105 KA_TRACE(20, (
"__kmpc_taskloop_5(enter): T#%d\n", gtid));
5106 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
5107 modifier, task_dup);
5108 KA_TRACE(20, (
"__kmpc_taskloop_5(exit): T#%d\n", gtid));
struct kmp_taskred_data kmp_taskred_data_t
struct kmp_task_red_input kmp_task_red_input_t
struct kmp_taskred_flags kmp_taskred_flags_t
struct kmp_taskred_input kmp_taskred_input_t
#define KMP_COUNT_BLOCK(name)
Increments specified counter (name).
void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int sched, kmp_uint64 grainsize, void *task_dup)
void * __kmpc_taskred_modifier_init(ident_t *loc, int gtid, int is_ws, int num, void *data)
void * __kmpc_taskred_init(int gtid, int num, void *data)
void * __kmpc_task_reduction_init(int gtid, int num, void *data)
void __kmpc_proxy_task_completed_ooo(kmp_task_t *ptask)
void __kmpc_task_reduction_modifier_fini(ident_t *loc, int gtid, int is_ws)
kmp_int32 __kmpc_omp_reg_task_with_affinity(ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *new_task, kmp_int32 naffins, kmp_task_affinity_info_t *affin_list)
void * __kmpc_task_reduction_modifier_init(ident_t *loc, int gtid, int is_ws, int num, void *data)
void __kmpc_taskloop_5(ident_t *loc, int gtid, kmp_task_t *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int sched, kmp_uint64 grainsize, int modifier, void *task_dup)
void __kmpc_proxy_task_completed(kmp_int32 gtid, kmp_task_t *ptask)
void * __kmpc_task_reduction_get_th_data(int gtid, void *tskgrp, void *data)
kmp_taskred_flags_t flags