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;
718 static inline void __ompt_task_start(kmp_task_t *task,
719 kmp_taskdata_t *current_task,
721 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
722 ompt_task_status_t status = ompt_task_switch;
723 if (__kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded) {
724 status = ompt_task_yield;
725 __kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded = 0;
728 if (ompt_enabled.ompt_callback_task_schedule) {
729 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
730 &(current_task->ompt_task_info.task_data), status,
731 &(taskdata->ompt_task_info.task_data));
733 taskdata->ompt_task_info.scheduling_parent = current_task;
738 static inline void __ompt_task_finish(kmp_task_t *task,
739 kmp_taskdata_t *resumed_task,
740 ompt_task_status_t status) {
741 if (ompt_enabled.ompt_callback_task_schedule) {
742 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
743 if (__kmp_omp_cancellation && taskdata->td_taskgroup &&
744 taskdata->td_taskgroup->cancel_request == cancel_taskgroup) {
745 status = ompt_task_cancel;
749 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
750 &(taskdata->ompt_task_info.task_data), status,
751 (resumed_task ? &(resumed_task->ompt_task_info.task_data) : NULL));
757 static void __kmpc_omp_task_begin_if0_template(
ident_t *loc_ref, kmp_int32 gtid,
760 void *return_address) {
761 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
762 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
764 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(enter): T#%d loc=%p task=%p "
766 gtid, loc_ref, taskdata, current_task));
768 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
771 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
772 KMP_DEBUG_USE_VAR(counter);
773 KA_TRACE(20, (
"__kmpc_omp_task_begin_if0: T#%d untied_count (%d) "
774 "incremented for task %p\n",
775 gtid, counter, taskdata));
778 taskdata->td_flags.task_serial =
780 __kmp_task_start(gtid, task, current_task);
784 if (current_task->ompt_task_info.frame.enter_frame.ptr == NULL) {
785 current_task->ompt_task_info.frame.enter_frame.ptr =
786 taskdata->ompt_task_info.frame.exit_frame.ptr = frame_address;
787 current_task->ompt_task_info.frame.enter_frame_flags =
788 taskdata->ompt_task_info.frame.exit_frame_flags =
789 ompt_frame_application | ompt_frame_framepointer;
791 if (ompt_enabled.ompt_callback_task_create) {
792 ompt_task_info_t *parent_info = &(current_task->ompt_task_info);
793 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
794 &(parent_info->task_data), &(parent_info->frame),
795 &(taskdata->ompt_task_info.task_data),
796 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(taskdata), 0,
799 __ompt_task_start(task, current_task, gtid);
803 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(exit): T#%d loc=%p task=%p,\n", gtid,
809 static void __kmpc_omp_task_begin_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
812 void *return_address) {
813 __kmpc_omp_task_begin_if0_template<true>(loc_ref, gtid, task, frame_address,
824 void __kmpc_omp_task_begin_if0(
ident_t *loc_ref, kmp_int32 gtid,
827 if (UNLIKELY(ompt_enabled.enabled)) {
828 OMPT_STORE_RETURN_ADDRESS(gtid);
829 __kmpc_omp_task_begin_if0_ompt(loc_ref, gtid, task,
830 OMPT_GET_FRAME_ADDRESS(1),
831 OMPT_LOAD_RETURN_ADDRESS(gtid));
835 __kmpc_omp_task_begin_if0_template<false>(loc_ref, gtid, task, NULL, NULL);
841 void __kmpc_omp_task_begin(
ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task) {
842 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
846 (
"__kmpc_omp_task_begin(enter): T#%d loc=%p task=%p current_task=%p\n",
847 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task), current_task));
849 __kmp_task_start(gtid, task, current_task);
851 KA_TRACE(10, (
"__kmpc_omp_task_begin(exit): T#%d loc=%p task=%p,\n", gtid,
852 loc_ref, KMP_TASK_TO_TASKDATA(task)));
862 static void __kmp_free_task(kmp_int32 gtid, kmp_taskdata_t *taskdata,
863 kmp_info_t *thread) {
864 KA_TRACE(30, (
"__kmp_free_task: T#%d freeing data from task %p\n", gtid,
868 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
869 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0);
870 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 1);
871 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
872 KMP_DEBUG_ASSERT(taskdata->td_allocated_child_tasks == 0 ||
873 taskdata->td_flags.task_serial == 1);
874 KMP_DEBUG_ASSERT(taskdata->td_incomplete_child_tasks == 0);
875 kmp_task_t *task = KMP_TASKDATA_TO_TASK(taskdata);
877 task->data1.destructors = NULL;
878 task->data2.priority = 0;
880 taskdata->td_flags.freed = 1;
883 __kmp_fast_free(thread, taskdata);
885 __kmp_thread_free(thread, taskdata);
887 KA_TRACE(20, (
"__kmp_free_task: T#%d freed task %p\n", gtid, taskdata));
896 static void __kmp_free_task_and_ancestors(kmp_int32 gtid,
897 kmp_taskdata_t *taskdata,
898 kmp_info_t *thread) {
901 kmp_int32 team_serial =
902 (taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) &&
903 !taskdata->td_flags.proxy;
904 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
906 kmp_int32 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
907 KMP_DEBUG_ASSERT(children >= 0);
910 while (children == 0) {
911 kmp_taskdata_t *parent_taskdata = taskdata->td_parent;
913 KA_TRACE(20, (
"__kmp_free_task_and_ancestors(enter): T#%d task %p complete "
914 "and freeing itself\n",
918 __kmp_free_task(gtid, taskdata, thread);
920 taskdata = parent_taskdata;
926 if (taskdata->td_flags.tasktype == TASK_IMPLICIT) {
927 if (taskdata->td_dephash) {
928 int children = KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks);
929 kmp_tasking_flags_t flags_old = taskdata->td_flags;
930 if (children == 0 && flags_old.complete == 1) {
931 kmp_tasking_flags_t flags_new = flags_old;
932 flags_new.complete = 0;
933 if (KMP_COMPARE_AND_STORE_ACQ32(
934 RCAST(kmp_int32 *, &taskdata->td_flags),
935 *RCAST(kmp_int32 *, &flags_old),
936 *RCAST(kmp_int32 *, &flags_new))) {
937 KA_TRACE(100, (
"__kmp_free_task_and_ancestors: T#%d cleans "
938 "dephash of implicit task %p\n",
941 __kmp_dephash_free_entries(thread, taskdata->td_dephash);
948 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
949 KMP_DEBUG_ASSERT(children >= 0);
953 20, (
"__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; "
954 "not freeing it yet\n",
955 gtid, taskdata, children));
966 static bool __kmp_track_children_task(kmp_taskdata_t *taskdata) {
967 kmp_tasking_flags_t flags = taskdata->td_flags;
968 bool ret = !(flags.team_serial || flags.tasking_ser);
969 ret = ret || flags.proxy == TASK_PROXY ||
970 flags.detachable == TASK_DETACHABLE || flags.hidden_helper;
972 KMP_ATOMIC_LD_ACQ(&taskdata->td_parent->td_incomplete_child_tasks) > 0;
986 static void __kmp_task_finish(kmp_int32 gtid, kmp_task_t *task,
987 kmp_taskdata_t *resumed_task) {
988 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
989 kmp_info_t *thread = __kmp_threads[gtid];
990 kmp_task_team_t *task_team =
991 thread->th.th_task_team;
993 kmp_int32 children = 0;
995 KA_TRACE(10, (
"__kmp_task_finish(enter): T#%d finishing task %p and resuming "
997 gtid, taskdata, resumed_task));
999 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
1002 #ifdef BUILD_TIED_TASK_STACK
1003 if (taskdata->td_flags.tiedness == TASK_TIED) {
1004 __kmp_pop_task_stack(gtid, thread, taskdata);
1008 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
1011 kmp_int32 counter = KMP_ATOMIC_DEC(&taskdata->td_untied_count) - 1;
1014 (
"__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n",
1015 gtid, counter, taskdata));
1019 if (resumed_task == NULL) {
1020 KMP_DEBUG_ASSERT(taskdata->td_flags.task_serial);
1021 resumed_task = taskdata->td_parent;
1024 thread->th.th_current_task = resumed_task;
1025 resumed_task->td_flags.executing = 1;
1026 KA_TRACE(10, (
"__kmp_task_finish(exit): T#%d partially done task %p, "
1027 "resuming task %p\n",
1028 gtid, taskdata, resumed_task));
1036 (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) ==
1037 taskdata->td_flags.task_serial);
1038 if (taskdata->td_flags.task_serial) {
1039 if (resumed_task == NULL) {
1040 resumed_task = taskdata->td_parent;
1044 KMP_DEBUG_ASSERT(resumed_task !=
1054 if (UNLIKELY(taskdata->td_flags.destructors_thunk)) {
1055 kmp_routine_entry_t destr_thunk = task->data1.destructors;
1056 KMP_ASSERT(destr_thunk);
1057 destr_thunk(gtid, task);
1060 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
1061 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 1);
1062 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
1064 bool detach =
false;
1065 if (UNLIKELY(taskdata->td_flags.detachable == TASK_DETACHABLE)) {
1066 if (taskdata->td_allow_completion_event.type ==
1067 KMP_EVENT_ALLOW_COMPLETION) {
1069 __kmp_acquire_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
1070 if (taskdata->td_allow_completion_event.type ==
1071 KMP_EVENT_ALLOW_COMPLETION) {
1073 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
1074 taskdata->td_flags.executing = 0;
1081 __ompt_task_finish(task, resumed_task, ompt_task_detach);
1087 taskdata->td_flags.proxy = TASK_PROXY;
1090 __kmp_release_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
1095 taskdata->td_flags.complete = 1;
1100 __ompt_task_finish(task, resumed_task, ompt_task_complete);
1104 if (__kmp_track_children_task(taskdata)) {
1105 __kmp_release_deps(gtid, taskdata);
1110 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks);
1111 KMP_DEBUG_ASSERT(children >= 0);
1112 if (taskdata->td_taskgroup)
1113 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
1114 }
else if (task_team && (task_team->tt.tt_found_proxy_tasks ||
1115 task_team->tt.tt_hidden_helper_task_encountered)) {
1118 __kmp_release_deps(gtid, taskdata);
1124 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
1125 taskdata->td_flags.executing = 0;
1129 20, (
"__kmp_task_finish: T#%d finished task %p, %d incomplete children\n",
1130 gtid, taskdata, children));
1136 thread->th.th_current_task = resumed_task;
1138 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
1142 resumed_task->td_flags.executing = 1;
1145 10, (
"__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n",
1146 gtid, taskdata, resumed_task));
1151 template <
bool ompt>
1152 static void __kmpc_omp_task_complete_if0_template(
ident_t *loc_ref,
1155 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n",
1156 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
1157 KMP_DEBUG_ASSERT(gtid >= 0);
1159 __kmp_task_finish<ompt>(gtid, task, NULL);
1161 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n",
1162 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
1166 ompt_frame_t *ompt_frame;
1167 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
1168 ompt_frame->enter_frame = ompt_data_none;
1169 ompt_frame->enter_frame_flags =
1170 ompt_frame_runtime | ompt_frame_framepointer;
1179 void __kmpc_omp_task_complete_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
1181 __kmpc_omp_task_complete_if0_template<true>(loc_ref, gtid, task);
1190 void __kmpc_omp_task_complete_if0(
ident_t *loc_ref, kmp_int32 gtid,
1193 if (UNLIKELY(ompt_enabled.enabled)) {
1194 __kmpc_omp_task_complete_if0_ompt(loc_ref, gtid, task);
1198 __kmpc_omp_task_complete_if0_template<false>(loc_ref, gtid, task);
1204 void __kmpc_omp_task_complete(
ident_t *loc_ref, kmp_int32 gtid,
1206 KA_TRACE(10, (
"__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n", gtid,
1207 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1209 __kmp_task_finish<false>(gtid, task,
1212 KA_TRACE(10, (
"__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n", gtid,
1213 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1229 void __kmp_init_implicit_task(
ident_t *loc_ref, kmp_info_t *this_thr,
1230 kmp_team_t *team,
int tid,
int set_curr_task) {
1231 kmp_taskdata_t *task = &team->t.t_implicit_task_taskdata[tid];
1235 (
"__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n",
1236 tid, team, task, set_curr_task ?
"TRUE" :
"FALSE"));
1238 task->td_task_id = KMP_GEN_TASK_ID();
1239 task->td_team = team;
1242 task->td_ident = loc_ref;
1243 task->td_taskwait_ident = NULL;
1244 task->td_taskwait_counter = 0;
1245 task->td_taskwait_thread = 0;
1247 task->td_flags.tiedness = TASK_TIED;
1248 task->td_flags.tasktype = TASK_IMPLICIT;
1249 task->td_flags.proxy = TASK_FULL;
1252 task->td_flags.task_serial = 1;
1253 task->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1254 task->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1256 task->td_flags.started = 1;
1257 task->td_flags.executing = 1;
1258 task->td_flags.complete = 0;
1259 task->td_flags.freed = 0;
1261 task->td_depnode = NULL;
1262 task->td_last_tied = task;
1263 task->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1265 if (set_curr_task) {
1266 KMP_ATOMIC_ST_REL(&task->td_incomplete_child_tasks, 0);
1268 KMP_ATOMIC_ST_REL(&task->td_allocated_child_tasks, 0);
1269 task->td_taskgroup = NULL;
1270 task->td_dephash = NULL;
1271 __kmp_push_current_task_to_thread(this_thr, team, tid);
1273 KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0);
1274 KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0);
1278 if (UNLIKELY(ompt_enabled.enabled))
1279 __ompt_task_init(task, tid);
1282 KF_TRACE(10, (
"__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n", tid,
1291 void __kmp_finish_implicit_task(kmp_info_t *thread) {
1292 kmp_taskdata_t *task = thread->th.th_current_task;
1293 if (task->td_dephash) {
1295 task->td_flags.complete = 1;
1296 children = KMP_ATOMIC_LD_ACQ(&task->td_incomplete_child_tasks);
1297 kmp_tasking_flags_t flags_old = task->td_flags;
1298 if (children == 0 && flags_old.complete == 1) {
1299 kmp_tasking_flags_t flags_new = flags_old;
1300 flags_new.complete = 0;
1301 if (KMP_COMPARE_AND_STORE_ACQ32(RCAST(kmp_int32 *, &task->td_flags),
1302 *RCAST(kmp_int32 *, &flags_old),
1303 *RCAST(kmp_int32 *, &flags_new))) {
1304 KA_TRACE(100, (
"__kmp_finish_implicit_task: T#%d cleans "
1305 "dephash of implicit task %p\n",
1306 thread->th.th_info.ds.ds_gtid, task));
1307 __kmp_dephash_free_entries(thread, task->td_dephash);
1317 void __kmp_free_implicit_task(kmp_info_t *thread) {
1318 kmp_taskdata_t *task = thread->th.th_current_task;
1319 if (task && task->td_dephash) {
1320 __kmp_dephash_free(thread, task->td_dephash);
1321 task->td_dephash = NULL;
1327 static size_t __kmp_round_up_to_val(
size_t size,
size_t val) {
1328 if (size & (val - 1)) {
1330 if (size <= KMP_SIZE_T_MAX - val) {
1349 kmp_task_t *__kmp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1350 kmp_tasking_flags_t *flags,
1351 size_t sizeof_kmp_task_t,
size_t sizeof_shareds,
1352 kmp_routine_entry_t task_entry) {
1354 kmp_taskdata_t *taskdata;
1355 kmp_info_t *thread = __kmp_threads[gtid];
1356 kmp_team_t *team = thread->th.th_team;
1357 kmp_taskdata_t *parent_task = thread->th.th_current_task;
1358 size_t shareds_offset;
1360 if (UNLIKELY(!TCR_4(__kmp_init_middle)))
1361 __kmp_middle_initialize();
1363 if (flags->hidden_helper) {
1364 if (__kmp_enable_hidden_helper) {
1365 if (!TCR_4(__kmp_init_hidden_helper))
1366 __kmp_hidden_helper_initialize();
1369 flags->hidden_helper = FALSE;
1373 KA_TRACE(10, (
"__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) "
1374 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1375 gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t,
1376 sizeof_shareds, task_entry));
1378 KMP_DEBUG_ASSERT(parent_task);
1379 if (parent_task->td_flags.final) {
1380 if (flags->merged_if0) {
1385 if (flags->tiedness == TASK_UNTIED && !team->t.t_serialized) {
1389 KMP_CHECK_UPDATE(thread->th.th_task_team->tt.tt_untied_task_encountered, 1);
1395 if (UNLIKELY(flags->proxy == TASK_PROXY ||
1396 flags->detachable == TASK_DETACHABLE || flags->hidden_helper)) {
1397 if (flags->proxy == TASK_PROXY) {
1398 flags->tiedness = TASK_UNTIED;
1399 flags->merged_if0 = 1;
1403 if ((thread->th.th_task_team) == NULL) {
1406 KMP_DEBUG_ASSERT(team->t.t_serialized);
1408 (
"T#%d creating task team in __kmp_task_alloc for proxy task\n",
1411 __kmp_task_team_setup(thread, team, 1);
1412 thread->th.th_task_team = team->t.t_task_team[thread->th.th_task_state];
1414 kmp_task_team_t *task_team = thread->th.th_task_team;
1417 if (!KMP_TASKING_ENABLED(task_team)) {
1420 (
"T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid));
1421 __kmp_enable_tasking(task_team, thread);
1422 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
1423 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
1425 if (thread_data->td.td_deque == NULL) {
1426 __kmp_alloc_task_deque(thread, thread_data);
1430 if ((flags->proxy == TASK_PROXY || flags->detachable == TASK_DETACHABLE) &&
1431 task_team->tt.tt_found_proxy_tasks == FALSE)
1432 TCW_4(task_team->tt.tt_found_proxy_tasks, TRUE);
1433 if (flags->hidden_helper &&
1434 task_team->tt.tt_hidden_helper_task_encountered == FALSE)
1435 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, TRUE);
1440 shareds_offset =
sizeof(kmp_taskdata_t) + sizeof_kmp_task_t;
1441 shareds_offset = __kmp_round_up_to_val(shareds_offset,
sizeof(
void *));
1444 KA_TRACE(30, (
"__kmp_task_alloc: T#%d First malloc size: %ld\n", gtid,
1446 KA_TRACE(30, (
"__kmp_task_alloc: T#%d Second malloc size: %ld\n", gtid,
1451 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, shareds_offset +
1454 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, shareds_offset +
1458 task = KMP_TASKDATA_TO_TASK(taskdata);
1461 #if KMP_ARCH_X86 || KMP_ARCH_PPC64 || !KMP_HAVE_QUAD
1462 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(
double) - 1)) == 0);
1463 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(
double) - 1)) == 0);
1465 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(_Quad) - 1)) == 0);
1466 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(_Quad) - 1)) == 0);
1468 if (sizeof_shareds > 0) {
1470 task->shareds = &((
char *)taskdata)[shareds_offset];
1472 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
1475 task->shareds = NULL;
1477 task->routine = task_entry;
1480 taskdata->td_task_id = KMP_GEN_TASK_ID();
1481 taskdata->td_team = thread->th.th_team;
1482 taskdata->td_alloc_thread = thread;
1483 taskdata->td_parent = parent_task;
1484 taskdata->td_level = parent_task->td_level + 1;
1485 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
1486 taskdata->td_ident = loc_ref;
1487 taskdata->td_taskwait_ident = NULL;
1488 taskdata->td_taskwait_counter = 0;
1489 taskdata->td_taskwait_thread = 0;
1490 KMP_DEBUG_ASSERT(taskdata->td_parent != NULL);
1492 if (flags->proxy == TASK_FULL)
1493 copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs);
1495 taskdata->td_flags = *flags;
1496 taskdata->td_task_team = thread->th.th_task_team;
1497 taskdata->td_size_alloc = shareds_offset + sizeof_shareds;
1498 taskdata->td_flags.tasktype = TASK_EXPLICIT;
1501 if (flags->hidden_helper) {
1502 kmp_info_t *shadow_thread = __kmp_threads[KMP_GTID_TO_SHADOW_GTID(gtid)];
1503 taskdata->td_team = shadow_thread->th.th_team;
1504 taskdata->td_task_team = shadow_thread->th.th_task_team;
1508 taskdata->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1511 taskdata->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1517 taskdata->td_flags.task_serial =
1518 (parent_task->td_flags.final || taskdata->td_flags.team_serial ||
1519 taskdata->td_flags.tasking_ser || flags->merged_if0);
1521 taskdata->td_flags.started = 0;
1522 taskdata->td_flags.executing = 0;
1523 taskdata->td_flags.complete = 0;
1524 taskdata->td_flags.freed = 0;
1526 KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0);
1528 KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1);
1529 taskdata->td_taskgroup =
1530 parent_task->td_taskgroup;
1531 taskdata->td_dephash = NULL;
1532 taskdata->td_depnode = NULL;
1533 if (flags->tiedness == TASK_UNTIED)
1534 taskdata->td_last_tied = NULL;
1536 taskdata->td_last_tied = taskdata;
1537 taskdata->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1539 if (UNLIKELY(ompt_enabled.enabled))
1540 __ompt_task_init(taskdata, gtid);
1544 if (__kmp_track_children_task(taskdata)) {
1545 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
1546 if (parent_task->td_taskgroup)
1547 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
1550 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) {
1551 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
1553 if (flags->hidden_helper) {
1554 taskdata->td_flags.task_serial = FALSE;
1556 KMP_ATOMIC_INC(&__kmp_unexecuted_hidden_helper_tasks);
1560 KA_TRACE(20, (
"__kmp_task_alloc(exit): T#%d created task %p parent=%p\n",
1561 gtid, taskdata, taskdata->td_parent));
1566 kmp_task_t *__kmpc_omp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1567 kmp_int32 flags,
size_t sizeof_kmp_task_t,
1568 size_t sizeof_shareds,
1569 kmp_routine_entry_t task_entry) {
1571 kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
1572 __kmp_assert_valid_gtid(gtid);
1573 input_flags->native = FALSE;
1575 KA_TRACE(10, (
"__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s %s) "
1576 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1577 gtid, loc_ref, input_flags->tiedness ?
"tied " :
"untied",
1578 input_flags->proxy ?
"proxy" :
"",
1579 input_flags->detachable ?
"detachable" :
"", sizeof_kmp_task_t,
1580 sizeof_shareds, task_entry));
1582 retval = __kmp_task_alloc(loc_ref, gtid, input_flags, sizeof_kmp_task_t,
1583 sizeof_shareds, task_entry);
1585 KA_TRACE(20, (
"__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval));
1590 kmp_task_t *__kmpc_omp_target_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1592 size_t sizeof_kmp_task_t,
1593 size_t sizeof_shareds,
1594 kmp_routine_entry_t task_entry,
1595 kmp_int64 device_id) {
1596 auto &input_flags =
reinterpret_cast<kmp_tasking_flags_t &
>(flags);
1598 input_flags.tiedness = TASK_UNTIED;
1600 if (__kmp_enable_hidden_helper)
1601 input_flags.hidden_helper = TRUE;
1603 return __kmpc_omp_task_alloc(loc_ref, gtid, flags, sizeof_kmp_task_t,
1604 sizeof_shareds, task_entry);
1622 kmp_task_t *new_task, kmp_int32 naffins,
1623 kmp_task_affinity_info_t *affin_list) {
1632 static void __kmp_invoke_task(kmp_int32 gtid, kmp_task_t *task,
1633 kmp_taskdata_t *current_task) {
1634 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
1638 30, (
"__kmp_invoke_task(enter): T#%d invoking task %p, current_task=%p\n",
1639 gtid, taskdata, current_task));
1640 KMP_DEBUG_ASSERT(task);
1641 if (UNLIKELY(taskdata->td_flags.proxy == TASK_PROXY &&
1642 taskdata->td_flags.complete == 1)) {
1647 (
"__kmp_invoke_task: T#%d running bottom finish for proxy task %p\n",
1650 __kmp_bottom_half_finish_proxy(gtid, task);
1652 KA_TRACE(30, (
"__kmp_invoke_task(exit): T#%d completed bottom finish for "
1653 "proxy task %p, resuming task %p\n",
1654 gtid, taskdata, current_task));
1662 ompt_thread_info_t oldInfo;
1663 if (UNLIKELY(ompt_enabled.enabled)) {
1665 thread = __kmp_threads[gtid];
1666 oldInfo = thread->th.ompt_thread_info;
1667 thread->th.ompt_thread_info.wait_id = 0;
1668 thread->th.ompt_thread_info.state = (thread->th.th_team_serialized)
1669 ? ompt_state_work_serial
1670 : ompt_state_work_parallel;
1671 taskdata->ompt_task_info.frame.exit_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1676 if (taskdata->td_flags.hidden_helper) {
1678 KMP_ASSERT(KMP_HIDDEN_HELPER_THREAD(gtid));
1679 KMP_ATOMIC_DEC(&__kmp_unexecuted_hidden_helper_tasks);
1683 if (taskdata->td_flags.proxy != TASK_PROXY) {
1684 __kmp_task_start(gtid, task, current_task);
1690 if (UNLIKELY(__kmp_omp_cancellation)) {
1691 thread = __kmp_threads[gtid];
1692 kmp_team_t *this_team = thread->th.th_team;
1693 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
1694 if ((taskgroup && taskgroup->cancel_request) ||
1695 (this_team->t.t_cancel_request == cancel_parallel)) {
1696 #if OMPT_SUPPORT && OMPT_OPTIONAL
1697 ompt_data_t *task_data;
1698 if (UNLIKELY(ompt_enabled.ompt_callback_cancel)) {
1699 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, NULL, NULL);
1700 ompt_callbacks.ompt_callback(ompt_callback_cancel)(
1702 ((taskgroup && taskgroup->cancel_request) ? ompt_cancel_taskgroup
1703 : ompt_cancel_parallel) |
1704 ompt_cancel_discarded_task,
1717 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
1718 taskdata->td_last_tied = current_task->td_last_tied;
1719 KMP_DEBUG_ASSERT(taskdata->td_last_tied);
1721 #if KMP_STATS_ENABLED
1723 switch (KMP_GET_THREAD_STATE()) {
1724 case FORK_JOIN_BARRIER:
1725 KMP_PUSH_PARTITIONED_TIMER(OMP_task_join_bar);
1728 KMP_PUSH_PARTITIONED_TIMER(OMP_task_plain_bar);
1731 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskyield);
1734 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskwait);
1737 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskgroup);
1740 KMP_PUSH_PARTITIONED_TIMER(OMP_task_immediate);
1747 if (UNLIKELY(ompt_enabled.enabled))
1748 __ompt_task_start(task, current_task, gtid);
1752 if (ompd_state & OMPD_ENABLE_BP)
1753 ompd_bp_task_begin();
1756 #if USE_ITT_BUILD && USE_ITT_NOTIFY
1757 kmp_uint64 cur_time;
1758 kmp_int32 kmp_itt_count_task =
1759 __kmp_forkjoin_frames_mode == 3 && !taskdata->td_flags.task_serial &&
1760 current_task->td_flags.tasktype == TASK_IMPLICIT;
1761 if (kmp_itt_count_task) {
1762 thread = __kmp_threads[gtid];
1764 if (thread->th.th_bar_arrive_time)
1765 cur_time = __itt_get_timestamp();
1767 kmp_itt_count_task = 0;
1769 KMP_FSYNC_ACQUIRED(taskdata);
1772 if (task->routine != NULL) {
1773 #ifdef KMP_GOMP_COMPAT
1774 if (taskdata->td_flags.native) {
1775 ((void (*)(
void *))(*(task->routine)))(task->shareds);
1779 (*(task->routine))(gtid, task);
1782 KMP_POP_PARTITIONED_TIMER();
1784 #if USE_ITT_BUILD && USE_ITT_NOTIFY
1785 if (kmp_itt_count_task) {
1787 thread->th.th_bar_arrive_time += (__itt_get_timestamp() - cur_time);
1789 KMP_FSYNC_CANCEL(taskdata);
1790 KMP_FSYNC_RELEASING(taskdata->td_parent);
1795 if (ompd_state & OMPD_ENABLE_BP)
1800 if (taskdata->td_flags.proxy != TASK_PROXY) {
1802 if (UNLIKELY(ompt_enabled.enabled)) {
1803 thread->th.ompt_thread_info = oldInfo;
1804 if (taskdata->td_flags.tiedness == TASK_TIED) {
1805 taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
1807 __kmp_task_finish<true>(gtid, task, current_task);
1810 __kmp_task_finish<false>(gtid, task, current_task);
1815 (
"__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n",
1816 gtid, taskdata, current_task));
1830 kmp_int32 __kmpc_omp_task_parts(
ident_t *loc_ref, kmp_int32 gtid,
1831 kmp_task_t *new_task) {
1832 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1834 KA_TRACE(10, (
"__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n", gtid,
1835 loc_ref, new_taskdata));
1838 kmp_taskdata_t *parent;
1839 if (UNLIKELY(ompt_enabled.enabled)) {
1840 parent = new_taskdata->td_parent;
1841 if (ompt_enabled.ompt_callback_task_create) {
1842 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1843 &(parent->ompt_task_info.task_data), &(parent->ompt_task_info.frame),
1844 &(new_taskdata->ompt_task_info.task_data), ompt_task_explicit, 0,
1845 OMPT_GET_RETURN_ADDRESS(0));
1853 if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1855 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1856 new_taskdata->td_flags.task_serial = 1;
1857 __kmp_invoke_task(gtid, new_task, current_task);
1862 (
"__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: "
1863 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
1864 gtid, loc_ref, new_taskdata));
1867 if (UNLIKELY(ompt_enabled.enabled)) {
1868 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1871 return TASK_CURRENT_NOT_QUEUED;
1885 kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task,
1886 bool serialize_immediate) {
1887 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1891 if (new_taskdata->td_flags.proxy == TASK_PROXY ||
1892 __kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1894 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1895 if (serialize_immediate)
1896 new_taskdata->td_flags.task_serial = 1;
1897 __kmp_invoke_task(gtid, new_task, current_task);
1900 return TASK_CURRENT_NOT_QUEUED;
1915 kmp_int32 __kmpc_omp_task(
ident_t *loc_ref, kmp_int32 gtid,
1916 kmp_task_t *new_task) {
1918 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1920 #if KMP_DEBUG || OMPT_SUPPORT
1921 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1923 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1925 __kmp_assert_valid_gtid(gtid);
1928 kmp_taskdata_t *parent = NULL;
1929 if (UNLIKELY(ompt_enabled.enabled)) {
1930 if (!new_taskdata->td_flags.started) {
1931 OMPT_STORE_RETURN_ADDRESS(gtid);
1932 parent = new_taskdata->td_parent;
1933 if (!parent->ompt_task_info.frame.enter_frame.ptr) {
1934 parent->ompt_task_info.frame.enter_frame.ptr =
1935 OMPT_GET_FRAME_ADDRESS(0);
1937 if (ompt_enabled.ompt_callback_task_create) {
1938 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1939 &(parent->ompt_task_info.task_data),
1940 &(parent->ompt_task_info.frame),
1941 &(new_taskdata->ompt_task_info.task_data),
1942 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1943 OMPT_LOAD_RETURN_ADDRESS(gtid));
1948 __ompt_task_finish(new_task,
1949 new_taskdata->ompt_task_info.scheduling_parent,
1951 new_taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
1956 res = __kmp_omp_task(gtid, new_task,
true);
1958 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
1959 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1960 gtid, loc_ref, new_taskdata));
1962 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
1963 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1982 kmp_int32 __kmp_omp_taskloop_task(
ident_t *loc_ref, kmp_int32 gtid,
1983 kmp_task_t *new_task,
void *codeptr_ra) {
1985 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1987 #if KMP_DEBUG || OMPT_SUPPORT
1988 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1990 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1994 kmp_taskdata_t *parent = NULL;
1995 if (UNLIKELY(ompt_enabled.enabled && !new_taskdata->td_flags.started)) {
1996 parent = new_taskdata->td_parent;
1997 if (!parent->ompt_task_info.frame.enter_frame.ptr)
1998 parent->ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1999 if (ompt_enabled.ompt_callback_task_create) {
2000 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
2001 &(parent->ompt_task_info.task_data), &(parent->ompt_task_info.frame),
2002 &(new_taskdata->ompt_task_info.task_data),
2003 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
2009 res = __kmp_omp_task(gtid, new_task,
true);
2011 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning "
2012 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
2013 gtid, loc_ref, new_taskdata));
2015 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
2016 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
2022 template <
bool ompt>
2023 static kmp_int32 __kmpc_omp_taskwait_template(
ident_t *loc_ref, kmp_int32 gtid,
2024 void *frame_address,
2025 void *return_address) {
2026 kmp_taskdata_t *taskdata =
nullptr;
2028 int thread_finished = FALSE;
2029 KMP_SET_THREAD_STATE_BLOCK(TASKWAIT);
2031 KA_TRACE(10, (
"__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref));
2032 KMP_DEBUG_ASSERT(gtid >= 0);
2034 if (__kmp_tasking_mode != tskm_immediate_exec) {
2035 thread = __kmp_threads[gtid];
2036 taskdata = thread->th.th_current_task;
2038 #if OMPT_SUPPORT && OMPT_OPTIONAL
2039 ompt_data_t *my_task_data;
2040 ompt_data_t *my_parallel_data;
2043 my_task_data = &(taskdata->ompt_task_info.task_data);
2044 my_parallel_data = OMPT_CUR_TEAM_DATA(thread);
2046 taskdata->ompt_task_info.frame.enter_frame.ptr = frame_address;
2048 if (ompt_enabled.ompt_callback_sync_region) {
2049 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2050 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
2051 my_task_data, return_address);
2054 if (ompt_enabled.ompt_callback_sync_region_wait) {
2055 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2056 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
2057 my_task_data, return_address);
2067 taskdata->td_taskwait_counter += 1;
2068 taskdata->td_taskwait_ident = loc_ref;
2069 taskdata->td_taskwait_thread = gtid + 1;
2072 void *itt_sync_obj = NULL;
2074 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2079 !taskdata->td_flags.team_serial && !taskdata->td_flags.final;
2081 must_wait = must_wait || (thread->th.th_task_team != NULL &&
2082 thread->th.th_task_team->tt.tt_found_proxy_tasks);
2086 (__kmp_enable_hidden_helper && thread->th.th_task_team != NULL &&
2087 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered);
2090 kmp_flag_32<false, false> flag(
2091 RCAST(std::atomic<kmp_uint32> *,
2092 &(taskdata->td_incomplete_child_tasks)),
2094 while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) != 0) {
2095 flag.execute_tasks(thread, gtid, FALSE,
2096 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2097 __kmp_task_stealing_constraint);
2101 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2102 KMP_FSYNC_ACQUIRED(taskdata);
2107 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2109 #if OMPT_SUPPORT && OMPT_OPTIONAL
2111 if (ompt_enabled.ompt_callback_sync_region_wait) {
2112 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2113 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
2114 my_task_data, return_address);
2116 if (ompt_enabled.ompt_callback_sync_region) {
2117 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2118 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
2119 my_task_data, return_address);
2121 taskdata->ompt_task_info.frame.enter_frame = ompt_data_none;
2127 KA_TRACE(10, (
"__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, "
2128 "returning TASK_CURRENT_NOT_QUEUED\n",
2131 return TASK_CURRENT_NOT_QUEUED;
2134 #if OMPT_SUPPORT && OMPT_OPTIONAL
2136 static kmp_int32 __kmpc_omp_taskwait_ompt(
ident_t *loc_ref, kmp_int32 gtid,
2137 void *frame_address,
2138 void *return_address) {
2139 return __kmpc_omp_taskwait_template<true>(loc_ref, gtid, frame_address,
2146 kmp_int32 __kmpc_omp_taskwait(
ident_t *loc_ref, kmp_int32 gtid) {
2147 #if OMPT_SUPPORT && OMPT_OPTIONAL
2148 if (UNLIKELY(ompt_enabled.enabled)) {
2149 OMPT_STORE_RETURN_ADDRESS(gtid);
2150 return __kmpc_omp_taskwait_ompt(loc_ref, gtid, OMPT_GET_FRAME_ADDRESS(0),
2151 OMPT_LOAD_RETURN_ADDRESS(gtid));
2154 return __kmpc_omp_taskwait_template<false>(loc_ref, gtid, NULL, NULL);
2158 kmp_int32 __kmpc_omp_taskyield(
ident_t *loc_ref, kmp_int32 gtid,
int end_part) {
2159 kmp_taskdata_t *taskdata = NULL;
2161 int thread_finished = FALSE;
2164 KMP_SET_THREAD_STATE_BLOCK(TASKYIELD);
2166 KA_TRACE(10, (
"__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n",
2167 gtid, loc_ref, end_part));
2168 __kmp_assert_valid_gtid(gtid);
2170 if (__kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel) {
2171 thread = __kmp_threads[gtid];
2172 taskdata = thread->th.th_current_task;
2179 taskdata->td_taskwait_counter += 1;
2180 taskdata->td_taskwait_ident = loc_ref;
2181 taskdata->td_taskwait_thread = gtid + 1;
2184 void *itt_sync_obj = NULL;
2186 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2189 if (!taskdata->td_flags.team_serial) {
2190 kmp_task_team_t *task_team = thread->th.th_task_team;
2191 if (task_team != NULL) {
2192 if (KMP_TASKING_ENABLED(task_team)) {
2194 if (UNLIKELY(ompt_enabled.enabled))
2195 thread->th.ompt_thread_info.ompt_task_yielded = 1;
2197 __kmp_execute_tasks_32(
2198 thread, gtid, (kmp_flag_32<> *)NULL, FALSE,
2199 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2200 __kmp_task_stealing_constraint);
2202 if (UNLIKELY(ompt_enabled.enabled))
2203 thread->th.ompt_thread_info.ompt_task_yielded = 0;
2209 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2214 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2217 KA_TRACE(10, (
"__kmpc_omp_taskyield(exit): T#%d task %p resuming, "
2218 "returning TASK_CURRENT_NOT_QUEUED\n",
2221 return TASK_CURRENT_NOT_QUEUED;
2242 unsigned reserved31 : 31;
2322 template <
typename T>
2323 void *__kmp_task_reduction_init(
int gtid,
int num, T *data) {
2324 __kmp_assert_valid_gtid(gtid);
2325 kmp_info_t *thread = __kmp_threads[gtid];
2326 kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup;
2327 kmp_uint32 nth = thread->th.th_team_nproc;
2331 KMP_ASSERT(tg != NULL);
2332 KMP_ASSERT(data != NULL);
2333 KMP_ASSERT(num > 0);
2335 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n",
2339 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, taskgroup %p, #items %d\n",
2343 for (
int i = 0; i < num; ++i) {
2344 size_t size = data[i].reduce_size - 1;
2346 size += CACHE_LINE - size % CACHE_LINE;
2347 KMP_ASSERT(data[i].reduce_comb != NULL);
2350 arr[i].
flags = data[i].flags;
2354 __kmp_assign_orig<T>(arr[i], data[i]);
2355 if (!arr[i].flags.lazy_priv) {
2358 arr[i].
reduce_pend = (
char *)(arr[i].reduce_priv) + nth * size;
2359 if (arr[i].reduce_init != NULL) {
2361 for (
size_t j = 0; j < nth; ++j) {
2362 __kmp_call_init<T>(arr[i], j * size);
2369 arr[i].
reduce_priv = __kmp_allocate(nth *
sizeof(
void *));
2372 tg->reduce_data = (
void *)arr;
2373 tg->reduce_num_data = num;
2412 template <
typename T>
2413 void __kmp_task_reduction_init_copy(kmp_info_t *thr,
int num, T *data,
2414 kmp_taskgroup_t *tg,
void *reduce_data) {
2416 KA_TRACE(20, (
"__kmp_task_reduction_init_copy: Th %p, init taskgroup %p,"
2418 thr, tg, reduce_data));
2423 for (
int i = 0; i < num; ++i) {
2426 tg->reduce_data = (
void *)arr;
2427 tg->reduce_num_data = num;
2440 __kmp_assert_valid_gtid(gtid);
2441 kmp_info_t *thread = __kmp_threads[gtid];
2442 kmp_int32 nth = thread->th.th_team_nproc;
2446 kmp_taskgroup_t *tg = (kmp_taskgroup_t *)tskgrp;
2448 tg = thread->th.th_current_task->td_taskgroup;
2449 KMP_ASSERT(tg != NULL);
2451 kmp_int32 num = tg->reduce_num_data;
2452 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
2454 KMP_ASSERT(data != NULL);
2455 while (tg != NULL) {
2456 for (
int i = 0; i < num; ++i) {
2457 if (!arr[i].flags.lazy_priv) {
2458 if (data == arr[i].reduce_shar ||
2459 (data >= arr[i].reduce_priv && data < arr[i].reduce_pend))
2460 return (
char *)(arr[i].
reduce_priv) + tid * arr[i].reduce_size;
2463 void **p_priv = (
void **)(arr[i].reduce_priv);
2464 if (data == arr[i].reduce_shar)
2467 for (
int j = 0; j < nth; ++j)
2468 if (data == p_priv[j])
2472 if (p_priv[tid] == NULL) {
2474 p_priv[tid] = __kmp_allocate(arr[i].reduce_size);
2475 if (arr[i].reduce_init != NULL) {
2476 if (arr[i].reduce_orig != NULL) {
2478 p_priv[tid], arr[i].reduce_orig);
2480 ((void (*)(
void *))arr[i].
reduce_init)(p_priv[tid]);
2489 num = tg->reduce_num_data;
2491 KMP_ASSERT2(0,
"Unknown task reduction item");
2497 static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg) {
2498 kmp_int32 nth = th->th.th_team_nproc;
2499 KMP_DEBUG_ASSERT(nth > 1);
2501 kmp_int32 num = tg->reduce_num_data;
2502 for (
int i = 0; i < num; ++i) {
2504 void (*f_fini)(
void *) = (
void (*)(
void *))(arr[i].
reduce_fini);
2505 void (*f_comb)(
void *,
void *) =
2507 if (!arr[i].flags.lazy_priv) {
2510 for (
int j = 0; j < nth; ++j) {
2511 void *priv_data = (
char *)pr_data + j * size;
2512 f_comb(sh_data, priv_data);
2517 void **pr_data = (
void **)(arr[i].reduce_priv);
2518 for (
int j = 0; j < nth; ++j) {
2519 if (pr_data[j] != NULL) {
2520 f_comb(sh_data, pr_data[j]);
2523 __kmp_free(pr_data[j]);
2527 __kmp_free(arr[i].reduce_priv);
2529 __kmp_thread_free(th, arr);
2530 tg->reduce_data = NULL;
2531 tg->reduce_num_data = 0;
2537 static void __kmp_task_reduction_clean(kmp_info_t *th, kmp_taskgroup_t *tg) {
2538 __kmp_thread_free(th, tg->reduce_data);
2539 tg->reduce_data = NULL;
2540 tg->reduce_num_data = 0;
2543 template <
typename T>
2544 void *__kmp_task_reduction_modifier_init(
ident_t *loc,
int gtid,
int is_ws,
2546 __kmp_assert_valid_gtid(gtid);
2547 kmp_info_t *thr = __kmp_threads[gtid];
2548 kmp_int32 nth = thr->th.th_team_nproc;
2549 __kmpc_taskgroup(loc, gtid);
2552 (
"__kmpc_reduction_modifier_init: T#%d, tg %p, exiting nth=1\n",
2553 gtid, thr->th.th_current_task->td_taskgroup));
2554 return (
void *)thr->th.th_current_task->td_taskgroup;
2556 kmp_team_t *team = thr->th.th_team;
2558 kmp_taskgroup_t *tg;
2559 reduce_data = KMP_ATOMIC_LD_RLX(&team->t.t_tg_reduce_data[is_ws]);
2560 if (reduce_data == NULL &&
2561 __kmp_atomic_compare_store(&team->t.t_tg_reduce_data[is_ws], reduce_data,
2564 KMP_DEBUG_ASSERT(reduce_data == NULL);
2566 tg = (kmp_taskgroup_t *)__kmp_task_reduction_init<T>(gtid, num, data);
2570 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[0]) == 0);
2571 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[1]) == 0);
2572 KMP_ATOMIC_ST_REL(&team->t.t_tg_reduce_data[is_ws], reduce_data);
2575 (reduce_data = KMP_ATOMIC_LD_ACQ(&team->t.t_tg_reduce_data[is_ws])) ==
2579 KMP_DEBUG_ASSERT(reduce_data > (
void *)1);
2580 tg = thr->th.th_current_task->td_taskgroup;
2581 __kmp_task_reduction_init_copy<T>(thr, num, data, tg, reduce_data);
2603 int num,
void *data) {
2604 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2624 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2637 __kmpc_end_taskgroup(loc, gtid);
2641 void __kmpc_taskgroup(
ident_t *loc,
int gtid) {
2642 __kmp_assert_valid_gtid(gtid);
2643 kmp_info_t *thread = __kmp_threads[gtid];
2644 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2645 kmp_taskgroup_t *tg_new =
2646 (kmp_taskgroup_t *)__kmp_thread_malloc(thread,
sizeof(kmp_taskgroup_t));
2647 KA_TRACE(10, (
"__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new));
2648 KMP_ATOMIC_ST_RLX(&tg_new->count, 0);
2649 KMP_ATOMIC_ST_RLX(&tg_new->cancel_request, cancel_noreq);
2650 tg_new->parent = taskdata->td_taskgroup;
2651 tg_new->reduce_data = NULL;
2652 tg_new->reduce_num_data = 0;
2653 tg_new->gomp_data = NULL;
2654 taskdata->td_taskgroup = tg_new;
2656 #if OMPT_SUPPORT && OMPT_OPTIONAL
2657 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2658 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2660 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2661 kmp_team_t *team = thread->th.th_team;
2662 ompt_data_t my_task_data = taskdata->ompt_task_info.task_data;
2664 ompt_data_t my_parallel_data = team->t.ompt_team_info.parallel_data;
2666 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2667 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2668 &(my_task_data), codeptr);
2675 void __kmpc_end_taskgroup(
ident_t *loc,
int gtid) {
2676 __kmp_assert_valid_gtid(gtid);
2677 kmp_info_t *thread = __kmp_threads[gtid];
2678 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2679 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
2680 int thread_finished = FALSE;
2682 #if OMPT_SUPPORT && OMPT_OPTIONAL
2684 ompt_data_t my_task_data;
2685 ompt_data_t my_parallel_data;
2686 void *codeptr =
nullptr;
2687 if (UNLIKELY(ompt_enabled.enabled)) {
2688 team = thread->th.th_team;
2689 my_task_data = taskdata->ompt_task_info.task_data;
2691 my_parallel_data = team->t.ompt_team_info.parallel_data;
2692 codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2694 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2698 KA_TRACE(10, (
"__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc));
2699 KMP_DEBUG_ASSERT(taskgroup != NULL);
2700 KMP_SET_THREAD_STATE_BLOCK(TASKGROUP);
2702 if (__kmp_tasking_mode != tskm_immediate_exec) {
2704 taskdata->td_taskwait_counter += 1;
2705 taskdata->td_taskwait_ident = loc;
2706 taskdata->td_taskwait_thread = gtid + 1;
2710 void *itt_sync_obj = NULL;
2712 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2716 #if OMPT_SUPPORT && OMPT_OPTIONAL
2717 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2718 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2719 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2720 &(my_task_data), codeptr);
2724 if (!taskdata->td_flags.team_serial ||
2725 (thread->th.th_task_team != NULL &&
2726 (thread->th.th_task_team->tt.tt_found_proxy_tasks ||
2727 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered))) {
2728 kmp_flag_32<false, false> flag(
2729 RCAST(std::atomic<kmp_uint32> *, &(taskgroup->count)), 0U);
2730 while (KMP_ATOMIC_LD_ACQ(&taskgroup->count) != 0) {
2731 flag.execute_tasks(thread, gtid, FALSE,
2732 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2733 __kmp_task_stealing_constraint);
2736 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2738 #if OMPT_SUPPORT && OMPT_OPTIONAL
2739 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2740 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2741 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2742 &(my_task_data), codeptr);
2747 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2748 KMP_FSYNC_ACQUIRED(taskdata);
2751 KMP_DEBUG_ASSERT(taskgroup->count == 0);
2753 if (taskgroup->reduce_data != NULL &&
2754 !taskgroup->gomp_data) {
2757 kmp_team_t *t = thread->th.th_team;
2761 if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[0])) != NULL &&
2764 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[0]);
2765 if (cnt == thread->th.th_team_nproc - 1) {
2768 __kmp_task_reduction_fini(thread, taskgroup);
2771 __kmp_thread_free(thread, reduce_data);
2772 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[0], NULL);
2773 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[0], 0);
2777 __kmp_task_reduction_clean(thread, taskgroup);
2779 }
else if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[1])) !=
2783 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[1]);
2784 if (cnt == thread->th.th_team_nproc - 1) {
2786 __kmp_task_reduction_fini(thread, taskgroup);
2789 __kmp_thread_free(thread, reduce_data);
2790 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[1], NULL);
2791 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[1], 0);
2795 __kmp_task_reduction_clean(thread, taskgroup);
2799 __kmp_task_reduction_fini(thread, taskgroup);
2803 taskdata->td_taskgroup = taskgroup->parent;
2804 __kmp_thread_free(thread, taskgroup);
2806 KA_TRACE(10, (
"__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n",
2809 #if OMPT_SUPPORT && OMPT_OPTIONAL
2810 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2811 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2812 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2813 &(my_task_data), codeptr);
2818 static kmp_task_t *__kmp_get_priority_task(kmp_int32 gtid,
2819 kmp_task_team_t *task_team,
2820 kmp_int32 is_constrained) {
2821 kmp_task_t *task = NULL;
2822 kmp_taskdata_t *taskdata;
2823 kmp_taskdata_t *current;
2824 kmp_thread_data_t *thread_data;
2825 int ntasks = task_team->tt.tt_num_task_pri;
2828 20, (
"__kmp_get_priority_task(exit #1): T#%d No tasks to get\n", gtid));
2833 if (__kmp_atomic_compare_store(&task_team->tt.tt_num_task_pri, ntasks,
2836 }
while (ntasks > 0);
2838 KA_TRACE(20, (
"__kmp_get_priority_task(exit #2): T#%d No tasks to get\n",
2844 kmp_task_pri_t *list = task_team->tt.tt_task_pri_list;
2846 KMP_ASSERT(list != NULL);
2847 thread_data = &list->td;
2848 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
2849 deque_ntasks = thread_data->td.td_deque_ntasks;
2850 if (deque_ntasks == 0) {
2851 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2852 KA_TRACE(20, (
"__kmp_get_priority_task: T#%d No tasks to get from %p\n",
2853 __kmp_get_gtid(), thread_data));
2856 }
while (deque_ntasks == 0);
2857 KMP_DEBUG_ASSERT(deque_ntasks);
2858 int target = thread_data->td.td_deque_head;
2859 current = __kmp_threads[gtid]->th.th_current_task;
2860 taskdata = thread_data->td.td_deque[target];
2861 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
2863 thread_data->td.td_deque_head =
2864 (target + 1) & TASK_DEQUE_MASK(thread_data->td);
2866 if (!task_team->tt.tt_untied_task_encountered) {
2868 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2869 KA_TRACE(20, (
"__kmp_get_priority_task(exit #3): T#%d could not get task "
2870 "from %p: task_team=%p ntasks=%d head=%u tail=%u\n",
2871 gtid, thread_data, task_team, deque_ntasks, target,
2872 thread_data->td.td_deque_tail));
2873 task_team->tt.tt_num_task_pri++;
2879 for (i = 1; i < deque_ntasks; ++i) {
2880 target = (target + 1) & TASK_DEQUE_MASK(thread_data->td);
2881 taskdata = thread_data->td.td_deque[target];
2882 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
2888 if (taskdata == NULL) {
2890 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2892 10, (
"__kmp_get_priority_task(exit #4): T#%d could not get task from "
2893 "%p: task_team=%p ntasks=%d head=%u tail=%u\n",
2894 gtid, thread_data, task_team, deque_ntasks,
2895 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2896 task_team->tt.tt_num_task_pri++;
2900 for (i = i + 1; i < deque_ntasks; ++i) {
2902 target = (target + 1) & TASK_DEQUE_MASK(thread_data->td);
2903 thread_data->td.td_deque[prev] = thread_data->td.td_deque[target];
2907 thread_data->td.td_deque_tail ==
2908 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(thread_data->td)));
2909 thread_data->td.td_deque_tail = target;
2911 thread_data->td.td_deque_ntasks = deque_ntasks - 1;
2912 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2913 task = KMP_TASKDATA_TO_TASK(taskdata);
2918 static kmp_task_t *__kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid,
2919 kmp_task_team_t *task_team,
2920 kmp_int32 is_constrained) {
2922 kmp_taskdata_t *taskdata;
2923 kmp_thread_data_t *thread_data;
2926 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2927 KMP_DEBUG_ASSERT(task_team->tt.tt_threads_data !=
2930 thread_data = &task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
2932 KA_TRACE(10, (
"__kmp_remove_my_task(enter): T#%d ntasks=%d head=%u tail=%u\n",
2933 gtid, thread_data->td.td_deque_ntasks,
2934 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2936 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2938 (
"__kmp_remove_my_task(exit #1): T#%d No tasks to remove: "
2939 "ntasks=%d head=%u tail=%u\n",
2940 gtid, thread_data->td.td_deque_ntasks,
2941 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2945 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
2947 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2948 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2950 (
"__kmp_remove_my_task(exit #2): T#%d No tasks to remove: "
2951 "ntasks=%d head=%u tail=%u\n",
2952 gtid, thread_data->td.td_deque_ntasks,
2953 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2957 tail = (thread_data->td.td_deque_tail - 1) &
2958 TASK_DEQUE_MASK(thread_data->td);
2959 taskdata = thread_data->td.td_deque[tail];
2961 if (!__kmp_task_is_allowed(gtid, is_constrained, taskdata,
2962 thread->th.th_current_task)) {
2964 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2966 (
"__kmp_remove_my_task(exit #3): T#%d TSC blocks tail task: "
2967 "ntasks=%d head=%u tail=%u\n",
2968 gtid, thread_data->td.td_deque_ntasks,
2969 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2973 thread_data->td.td_deque_tail = tail;
2974 TCW_4(thread_data->td.td_deque_ntasks, thread_data->td.td_deque_ntasks - 1);
2976 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2978 KA_TRACE(10, (
"__kmp_remove_my_task(exit #4): T#%d task %p removed: "
2979 "ntasks=%d head=%u tail=%u\n",
2980 gtid, taskdata, thread_data->td.td_deque_ntasks,
2981 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2983 task = KMP_TASKDATA_TO_TASK(taskdata);
2990 static kmp_task_t *__kmp_steal_task(kmp_info_t *victim_thr, kmp_int32 gtid,
2991 kmp_task_team_t *task_team,
2992 std::atomic<kmp_int32> *unfinished_threads,
2993 int *thread_finished,
2994 kmp_int32 is_constrained) {
2996 kmp_taskdata_t *taskdata;
2997 kmp_taskdata_t *current;
2998 kmp_thread_data_t *victim_td, *threads_data;
3000 kmp_int32 victim_tid;
3002 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3004 threads_data = task_team->tt.tt_threads_data;
3005 KMP_DEBUG_ASSERT(threads_data != NULL);
3007 victim_tid = victim_thr->th.th_info.ds.ds_tid;
3008 victim_td = &threads_data[victim_tid];
3010 KA_TRACE(10, (
"__kmp_steal_task(enter): T#%d try to steal from T#%d: "
3011 "task_team=%p ntasks=%d head=%u tail=%u\n",
3012 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
3013 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
3014 victim_td->td.td_deque_tail));
3016 if (TCR_4(victim_td->td.td_deque_ntasks) == 0) {
3017 KA_TRACE(10, (
"__kmp_steal_task(exit #1): T#%d could not steal from T#%d: "
3018 "task_team=%p ntasks=%d head=%u tail=%u\n",
3019 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
3020 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
3021 victim_td->td.td_deque_tail));
3025 __kmp_acquire_bootstrap_lock(&victim_td->td.td_deque_lock);
3027 int ntasks = TCR_4(victim_td->td.td_deque_ntasks);
3030 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3031 KA_TRACE(10, (
"__kmp_steal_task(exit #2): 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, ntasks,
3034 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3038 KMP_DEBUG_ASSERT(victim_td->td.td_deque != NULL);
3039 current = __kmp_threads[gtid]->th.th_current_task;
3040 taskdata = victim_td->td.td_deque[victim_td->td.td_deque_head];
3041 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3043 victim_td->td.td_deque_head =
3044 (victim_td->td.td_deque_head + 1) & TASK_DEQUE_MASK(victim_td->td);
3046 if (!task_team->tt.tt_untied_task_encountered) {
3048 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3049 KA_TRACE(10, (
"__kmp_steal_task(exit #3): T#%d could not steal from "
3050 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
3051 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3052 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3057 target = victim_td->td.td_deque_head;
3059 for (i = 1; i < ntasks; ++i) {
3060 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
3061 taskdata = victim_td->td.td_deque[target];
3062 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3068 if (taskdata == NULL) {
3070 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3071 KA_TRACE(10, (
"__kmp_steal_task(exit #4): T#%d could not steal from "
3072 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
3073 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3074 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3078 for (i = i + 1; i < ntasks; ++i) {
3080 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
3081 victim_td->td.td_deque[prev] = victim_td->td.td_deque[target];
3085 victim_td->td.td_deque_tail ==
3086 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(victim_td->td)));
3087 victim_td->td.td_deque_tail = target;
3089 if (*thread_finished) {
3096 KMP_ATOMIC_INC(unfinished_threads);
3099 (
"__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n",
3100 gtid, count + 1, task_team));
3101 *thread_finished = FALSE;
3103 TCW_4(victim_td->td.td_deque_ntasks, ntasks - 1);
3105 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3109 (
"__kmp_steal_task(exit #5): T#%d stole task %p from T#%d: "
3110 "task_team=%p ntasks=%d head=%u tail=%u\n",
3111 gtid, taskdata, __kmp_gtid_from_thread(victim_thr), task_team,
3112 ntasks, victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3114 task = KMP_TASKDATA_TO_TASK(taskdata);
3128 static inline int __kmp_execute_tasks_template(
3129 kmp_info_t *thread, kmp_int32 gtid, C *flag,
int final_spin,
3130 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3131 kmp_int32 is_constrained) {
3132 kmp_task_team_t *task_team = thread->th.th_task_team;
3133 kmp_thread_data_t *threads_data;
3135 kmp_info_t *other_thread;
3136 kmp_taskdata_t *current_task = thread->th.th_current_task;
3137 std::atomic<kmp_int32> *unfinished_threads;
3138 kmp_int32 nthreads, victim_tid = -2, use_own_tasks = 1, new_victim = 0,
3139 tid = thread->th.th_info.ds.ds_tid;
3141 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3142 KMP_DEBUG_ASSERT(thread == __kmp_threads[gtid]);
3144 if (task_team == NULL || current_task == NULL)
3147 KA_TRACE(15, (
"__kmp_execute_tasks_template(enter): T#%d final_spin=%d "
3148 "*thread_finished=%d\n",
3149 gtid, final_spin, *thread_finished));
3151 thread->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
3152 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
3154 KMP_DEBUG_ASSERT(threads_data != NULL);
3156 nthreads = task_team->tt.tt_nproc;
3157 unfinished_threads = &(task_team->tt.tt_unfinished_threads);
3158 KMP_DEBUG_ASSERT(nthreads > 1 || task_team->tt.tt_found_proxy_tasks ||
3159 task_team->tt.tt_hidden_helper_task_encountered);
3160 KMP_DEBUG_ASSERT(*unfinished_threads >= 0);
3166 if (task_team->tt.tt_num_task_pri) {
3167 task = __kmp_get_priority_task(gtid, task_team, is_constrained);
3169 if (task == NULL && use_own_tasks) {
3170 task = __kmp_remove_my_task(thread, gtid, task_team, is_constrained);
3172 if ((task == NULL) && (nthreads > 1)) {
3176 if (victim_tid == -2) {
3177 victim_tid = threads_data[tid].td.td_deque_last_stolen;
3180 other_thread = threads_data[victim_tid].td.td_thr;
3182 if (victim_tid != -1) {
3184 }
else if (!new_victim) {
3190 victim_tid = __kmp_get_random(thread) % (nthreads - 1);
3191 if (victim_tid >= tid) {
3195 other_thread = threads_data[victim_tid].td.td_thr;
3205 if ((__kmp_tasking_mode == tskm_task_teams) &&
3206 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) &&
3207 (TCR_PTR(CCAST(
void *, other_thread->th.th_sleep_loc)) !=
3210 __kmp_null_resume_wrapper(other_thread);
3223 task = __kmp_steal_task(other_thread, gtid, task_team,
3224 unfinished_threads, thread_finished,
3228 if (threads_data[tid].td.td_deque_last_stolen != victim_tid) {
3229 threads_data[tid].td.td_deque_last_stolen = victim_tid;
3236 KMP_CHECK_UPDATE(threads_data[tid].td.td_deque_last_stolen, -1);
3245 #if USE_ITT_BUILD && USE_ITT_NOTIFY
3246 if (__itt_sync_create_ptr || KMP_ITT_DEBUG) {
3247 if (itt_sync_obj == NULL) {
3249 itt_sync_obj = __kmp_itt_barrier_object(gtid, bs_forkjoin_barrier);
3251 __kmp_itt_task_starting(itt_sync_obj);
3254 __kmp_invoke_task(gtid, task, current_task);
3256 if (itt_sync_obj != NULL)
3257 __kmp_itt_task_finished(itt_sync_obj);
3264 if (flag == NULL || (!final_spin && flag->done_check())) {
3267 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3271 if (thread->th.th_task_team == NULL) {
3274 KMP_YIELD(__kmp_library == library_throughput);
3277 if (!use_own_tasks && TCR_4(threads_data[tid].td.td_deque_ntasks) != 0) {
3278 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d stolen task spawned "
3279 "other tasks, restart\n",
3290 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks) == 0) {
3294 if (!*thread_finished) {
3296 kmp_int32 count = -1 +
3298 KMP_ATOMIC_DEC(unfinished_threads);
3299 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d dec "
3300 "unfinished_threads to %d task_team=%p\n",
3301 gtid, count, task_team));
3302 *thread_finished = TRUE;
3310 if (flag != NULL && flag->done_check()) {
3313 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3321 if (thread->th.th_task_team == NULL) {
3323 (
"__kmp_execute_tasks_template: T#%d no more tasks\n", gtid));
3332 if (flag == NULL || (!final_spin && flag->done_check())) {
3334 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3341 if (nthreads == 1 &&
3342 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks))
3346 (
"__kmp_execute_tasks_template: T#%d can't find work\n", gtid));
3352 template <
bool C,
bool S>
3353 int __kmp_execute_tasks_32(
3354 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32<C, S> *flag,
int final_spin,
3355 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3356 kmp_int32 is_constrained) {
3357 return __kmp_execute_tasks_template(
3358 thread, gtid, flag, final_spin,
3359 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3362 template <
bool C,
bool S>
3363 int __kmp_execute_tasks_64(
3364 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64<C, S> *flag,
int final_spin,
3365 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3366 kmp_int32 is_constrained) {
3367 return __kmp_execute_tasks_template(
3368 thread, gtid, flag, final_spin,
3369 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3372 template <
bool C,
bool S>
3373 int __kmp_atomic_execute_tasks_64(
3374 kmp_info_t *thread, kmp_int32 gtid, kmp_atomic_flag_64<C, S> *flag,
3375 int final_spin,
int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3376 kmp_int32 is_constrained) {
3377 return __kmp_execute_tasks_template(
3378 thread, gtid, flag, final_spin,
3379 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3382 int __kmp_execute_tasks_oncore(
3383 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag,
int final_spin,
3384 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3385 kmp_int32 is_constrained) {
3386 return __kmp_execute_tasks_template(
3387 thread, gtid, flag, final_spin,
3388 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3392 __kmp_execute_tasks_32<false, false>(kmp_info_t *, kmp_int32,
3393 kmp_flag_32<false, false> *,
int,
3394 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3396 template int __kmp_execute_tasks_64<false, true>(kmp_info_t *, kmp_int32,
3397 kmp_flag_64<false, true> *,
3399 int *USE_ITT_BUILD_ARG(
void *),
3402 template int __kmp_execute_tasks_64<true, false>(kmp_info_t *, kmp_int32,
3403 kmp_flag_64<true, false> *,
3405 int *USE_ITT_BUILD_ARG(
void *),
3408 template int __kmp_atomic_execute_tasks_64<false, true>(
3409 kmp_info_t *, kmp_int32, kmp_atomic_flag_64<false, true> *,
int,
3410 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3412 template int __kmp_atomic_execute_tasks_64<true, false>(
3413 kmp_info_t *, kmp_int32, kmp_atomic_flag_64<true, false> *,
int,
3414 int *USE_ITT_BUILD_ARG(
void *), kmp_int32);
3419 static void __kmp_enable_tasking(kmp_task_team_t *task_team,
3420 kmp_info_t *this_thr) {
3421 kmp_thread_data_t *threads_data;
3422 int nthreads, i, is_init_thread;
3424 KA_TRACE(10, (
"__kmp_enable_tasking(enter): T#%d\n",
3425 __kmp_gtid_from_thread(this_thr)));
3427 KMP_DEBUG_ASSERT(task_team != NULL);
3428 KMP_DEBUG_ASSERT(this_thr->th.th_team != NULL);
3430 nthreads = task_team->tt.tt_nproc;
3431 KMP_DEBUG_ASSERT(nthreads > 0);
3432 KMP_DEBUG_ASSERT(nthreads == this_thr->th.th_team->t.t_nproc);
3435 is_init_thread = __kmp_realloc_task_threads_data(this_thr, task_team);
3437 if (!is_init_thread) {
3441 (
"__kmp_enable_tasking(exit): T#%d: threads array already set up.\n",
3442 __kmp_gtid_from_thread(this_thr)));
3445 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
3446 KMP_DEBUG_ASSERT(threads_data != NULL);
3448 if (__kmp_tasking_mode == tskm_task_teams &&
3449 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME)) {
3453 for (i = 0; i < nthreads; i++) {
3455 kmp_info_t *thread = threads_data[i].td.td_thr;
3457 if (i == this_thr->th.th_info.ds.ds_tid) {
3466 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3468 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d waking up thread T#%d\n",
3469 __kmp_gtid_from_thread(this_thr),
3470 __kmp_gtid_from_thread(thread)));
3471 __kmp_null_resume_wrapper(thread);
3473 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d don't wake up thread T#%d\n",
3474 __kmp_gtid_from_thread(this_thr),
3475 __kmp_gtid_from_thread(thread)));
3480 KA_TRACE(10, (
"__kmp_enable_tasking(exit): T#%d\n",
3481 __kmp_gtid_from_thread(this_thr)));
3514 static kmp_task_team_t *__kmp_free_task_teams =
3517 kmp_bootstrap_lock_t __kmp_task_team_lock =
3518 KMP_BOOTSTRAP_LOCK_INITIALIZER(__kmp_task_team_lock);
3525 static void __kmp_alloc_task_deque(kmp_info_t *thread,
3526 kmp_thread_data_t *thread_data) {
3527 __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
3528 KMP_DEBUG_ASSERT(thread_data->td.td_deque == NULL);
3531 thread_data->td.td_deque_last_stolen = -1;
3533 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == 0);
3534 KMP_DEBUG_ASSERT(thread_data->td.td_deque_head == 0);
3535 KMP_DEBUG_ASSERT(thread_data->td.td_deque_tail == 0);
3539 (
"__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n",
3540 __kmp_gtid_from_thread(thread), INITIAL_TASK_DEQUE_SIZE, thread_data));
3544 thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
3545 INITIAL_TASK_DEQUE_SIZE *
sizeof(kmp_taskdata_t *));
3546 thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
3552 static void __kmp_free_task_deque(kmp_thread_data_t *thread_data) {
3553 if (thread_data->td.td_deque != NULL) {
3554 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3555 TCW_4(thread_data->td.td_deque_ntasks, 0);
3556 __kmp_free(thread_data->td.td_deque);
3557 thread_data->td.td_deque = NULL;
3558 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3561 #ifdef BUILD_TIED_TASK_STACK
3563 if (thread_data->td.td_susp_tied_tasks.ts_entries != TASK_STACK_EMPTY) {
3564 __kmp_free_task_stack(__kmp_thread_from_gtid(gtid), thread_data);
3576 static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
3577 kmp_task_team_t *task_team) {
3578 kmp_thread_data_t **threads_data_p;
3579 kmp_int32 nthreads, maxthreads;
3580 int is_init_thread = FALSE;
3582 if (TCR_4(task_team->tt.tt_found_tasks)) {
3587 threads_data_p = &task_team->tt.tt_threads_data;
3588 nthreads = task_team->tt.tt_nproc;
3589 maxthreads = task_team->tt.tt_max_threads;
3594 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3596 if (!TCR_4(task_team->tt.tt_found_tasks)) {
3598 kmp_team_t *team = thread->th.th_team;
3601 is_init_thread = TRUE;
3602 if (maxthreads < nthreads) {
3604 if (*threads_data_p != NULL) {
3605 kmp_thread_data_t *old_data = *threads_data_p;
3606 kmp_thread_data_t *new_data = NULL;
3610 (
"__kmp_realloc_task_threads_data: T#%d reallocating "
3611 "threads data for task_team %p, new_size = %d, old_size = %d\n",
3612 __kmp_gtid_from_thread(thread), task_team, nthreads, maxthreads));
3617 new_data = (kmp_thread_data_t *)__kmp_allocate(
3618 nthreads *
sizeof(kmp_thread_data_t));
3620 KMP_MEMCPY_S((
void *)new_data, nthreads *
sizeof(kmp_thread_data_t),
3621 (
void *)old_data, maxthreads *
sizeof(kmp_thread_data_t));
3623 #ifdef BUILD_TIED_TASK_STACK
3625 for (i = maxthreads; i < nthreads; i++) {
3626 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3627 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3631 (*threads_data_p) = new_data;
3632 __kmp_free(old_data);
3634 KE_TRACE(10, (
"__kmp_realloc_task_threads_data: T#%d allocating "
3635 "threads data for task_team %p, size = %d\n",
3636 __kmp_gtid_from_thread(thread), task_team, nthreads));
3640 *threads_data_p = (kmp_thread_data_t *)__kmp_allocate(
3641 nthreads *
sizeof(kmp_thread_data_t));
3642 #ifdef BUILD_TIED_TASK_STACK
3644 for (i = 0; i < nthreads; i++) {
3645 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3646 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3650 task_team->tt.tt_max_threads = nthreads;
3653 KMP_DEBUG_ASSERT(*threads_data_p != NULL);
3657 for (i = 0; i < nthreads; i++) {
3658 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3659 thread_data->td.td_thr = team->t.t_threads[i];
3661 if (thread_data->td.td_deque_last_stolen >= nthreads) {
3665 thread_data->td.td_deque_last_stolen = -1;
3670 TCW_SYNC_4(task_team->tt.tt_found_tasks, TRUE);
3673 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3674 return is_init_thread;
3680 static void __kmp_free_task_threads_data(kmp_task_team_t *task_team) {
3681 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3682 if (task_team->tt.tt_threads_data != NULL) {
3684 for (i = 0; i < task_team->tt.tt_max_threads; i++) {
3685 __kmp_free_task_deque(&task_team->tt.tt_threads_data[i]);
3687 __kmp_free(task_team->tt.tt_threads_data);
3688 task_team->tt.tt_threads_data = NULL;
3690 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3696 static void __kmp_free_task_pri_list(kmp_task_team_t *task_team) {
3697 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
3698 if (task_team->tt.tt_task_pri_list != NULL) {
3699 kmp_task_pri_t *list = task_team->tt.tt_task_pri_list;
3700 while (list != NULL) {
3701 kmp_task_pri_t *next = list->next;
3702 __kmp_free_task_deque(&list->td);
3706 task_team->tt.tt_task_pri_list = NULL;
3708 __kmp_release_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
3715 static kmp_task_team_t *__kmp_allocate_task_team(kmp_info_t *thread,
3717 kmp_task_team_t *task_team = NULL;
3720 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d entering; team = %p\n",
3721 (thread ? __kmp_gtid_from_thread(thread) : -1), team));
3723 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3725 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3726 if (__kmp_free_task_teams != NULL) {
3727 task_team = __kmp_free_task_teams;
3728 TCW_PTR(__kmp_free_task_teams, task_team->tt.tt_next);
3729 task_team->tt.tt_next = NULL;
3731 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3734 if (task_team == NULL) {
3735 KE_TRACE(10, (
"__kmp_allocate_task_team: T#%d allocating "
3736 "task team for team %p\n",
3737 __kmp_gtid_from_thread(thread), team));
3740 task_team = (kmp_task_team_t *)__kmp_allocate(
sizeof(kmp_task_team_t));
3741 __kmp_init_bootstrap_lock(&task_team->tt.tt_threads_lock);
3742 __kmp_init_bootstrap_lock(&task_team->tt.tt_task_pri_lock);
3743 #if USE_ITT_BUILD && USE_ITT_NOTIFY && KMP_DEBUG
3746 __itt_suppress_mark_range(
3747 __itt_suppress_range, __itt_suppress_threading_errors,
3748 &task_team->tt.tt_found_tasks,
sizeof(task_team->tt.tt_found_tasks));
3749 __itt_suppress_mark_range(__itt_suppress_range,
3750 __itt_suppress_threading_errors,
3751 CCAST(kmp_uint32 *, &task_team->tt.tt_active),
3752 sizeof(task_team->tt.tt_active));
3760 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3761 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3762 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
3763 task_team->tt.tt_nproc = nthreads = team->t.t_nproc;
3765 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads, nthreads);
3766 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
3767 TCW_4(task_team->tt.tt_active, TRUE);
3769 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d exiting; task_team = %p "
3770 "unfinished_threads init'd to %d\n",
3771 (thread ? __kmp_gtid_from_thread(thread) : -1), task_team,
3772 KMP_ATOMIC_LD_RLX(&task_team->tt.tt_unfinished_threads)));
3779 void __kmp_free_task_team(kmp_info_t *thread, kmp_task_team_t *task_team) {
3780 KA_TRACE(20, (
"__kmp_free_task_team: T#%d task_team = %p\n",
3781 thread ? __kmp_gtid_from_thread(thread) : -1, task_team));
3784 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3786 KMP_DEBUG_ASSERT(task_team->tt.tt_next == NULL);
3787 task_team->tt.tt_next = __kmp_free_task_teams;
3788 TCW_PTR(__kmp_free_task_teams, task_team);
3790 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3798 void __kmp_reap_task_teams(
void) {
3799 kmp_task_team_t *task_team;
3801 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3803 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3804 while ((task_team = __kmp_free_task_teams) != NULL) {
3805 __kmp_free_task_teams = task_team->tt.tt_next;
3806 task_team->tt.tt_next = NULL;
3809 if (task_team->tt.tt_threads_data != NULL) {
3810 __kmp_free_task_threads_data(task_team);
3812 if (task_team->tt.tt_task_pri_list != NULL) {
3813 __kmp_free_task_pri_list(task_team);
3815 __kmp_free(task_team);
3817 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3824 void __kmp_wait_to_unref_task_teams(
void) {
3830 KMP_INIT_YIELD(spins);
3831 KMP_INIT_BACKOFF(time);
3839 for (thread = CCAST(kmp_info_t *, __kmp_thread_pool); thread != NULL;
3840 thread = thread->th.th_next_pool) {
3844 if (TCR_PTR(thread->th.th_task_team) == NULL) {
3845 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n",
3846 __kmp_gtid_from_thread(thread)));
3851 if (!__kmp_is_thread_alive(thread, &exit_val)) {
3852 thread->th.th_task_team = NULL;
3859 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: Waiting for T#%d to "
3860 "unreference task_team\n",
3861 __kmp_gtid_from_thread(thread)));
3863 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
3866 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3870 (
"__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n",
3871 __kmp_gtid_from_thread(thread), __kmp_gtid_from_thread(thread)));
3872 __kmp_null_resume_wrapper(thread);
3881 KMP_YIELD_OVERSUB_ELSE_SPIN(spins, time);
3887 void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team,
int always) {
3888 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3894 if (team->t.t_task_team[this_thr->th.th_task_state] == NULL &&
3895 (always || team->t.t_nproc > 1)) {
3896 team->t.t_task_team[this_thr->th.th_task_state] =
3897 __kmp_allocate_task_team(this_thr, team);
3898 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d created new task_team %p"
3899 " for team %d at parity=%d\n",
3900 __kmp_gtid_from_thread(this_thr),
3901 team->t.t_task_team[this_thr->th.th_task_state], team->t.t_id,
3902 this_thr->th.th_task_state));
3912 if (team->t.t_nproc > 1) {
3913 int other_team = 1 - this_thr->th.th_task_state;
3914 KMP_DEBUG_ASSERT(other_team >= 0 && other_team < 2);
3915 if (team->t.t_task_team[other_team] == NULL) {
3916 team->t.t_task_team[other_team] =
3917 __kmp_allocate_task_team(this_thr, team);
3918 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d created second new "
3919 "task_team %p for team %d at parity=%d\n",
3920 __kmp_gtid_from_thread(this_thr),
3921 team->t.t_task_team[other_team], team->t.t_id, other_team));
3924 kmp_task_team_t *task_team = team->t.t_task_team[other_team];
3925 if (!task_team->tt.tt_active ||
3926 team->t.t_nproc != task_team->tt.tt_nproc) {
3927 TCW_4(task_team->tt.tt_nproc, team->t.t_nproc);
3928 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3929 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3930 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
3931 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads,
3933 TCW_4(task_team->tt.tt_active, TRUE);
3937 KA_TRACE(20, (
"__kmp_task_team_setup: Primary T#%d reset next task_team "
3938 "%p for team %d at parity=%d\n",
3939 __kmp_gtid_from_thread(this_thr),
3940 team->t.t_task_team[other_team], team->t.t_id, other_team));
3948 if (this_thr == __kmp_hidden_helper_main_thread) {
3949 for (
int i = 0; i < 2; ++i) {
3950 kmp_task_team_t *task_team = team->t.t_task_team[i];
3951 if (KMP_TASKING_ENABLED(task_team)) {
3954 __kmp_enable_tasking(task_team, this_thr);
3955 for (
int j = 0; j < task_team->tt.tt_nproc; ++j) {
3956 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[j];
3957 if (thread_data->td.td_deque == NULL) {
3958 __kmp_alloc_task_deque(__kmp_hidden_helper_threads[j], thread_data);
3968 void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team) {
3969 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3973 this_thr->th.th_task_state = (kmp_uint8)(1 - this_thr->th.th_task_state);
3977 TCW_PTR(this_thr->th.th_task_team,
3978 team->t.t_task_team[this_thr->th.th_task_state]);
3980 (
"__kmp_task_team_sync: Thread T#%d task team switched to task_team "
3981 "%p from Team #%d (parity=%d)\n",
3982 __kmp_gtid_from_thread(this_thr), this_thr->th.th_task_team,
3983 team->t.t_id, this_thr->th.th_task_state));
3993 void __kmp_task_team_wait(
3994 kmp_info_t *this_thr,
3995 kmp_team_t *team USE_ITT_BUILD_ARG(
void *itt_sync_obj),
int wait) {
3996 kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state];
3998 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3999 KMP_DEBUG_ASSERT(task_team == this_thr->th.th_task_team);
4001 if ((task_team != NULL) && KMP_TASKING_ENABLED(task_team)) {
4003 KA_TRACE(20, (
"__kmp_task_team_wait: Primary T#%d waiting for all tasks "
4004 "(for unfinished_threads to reach 0) on task_team = %p\n",
4005 __kmp_gtid_from_thread(this_thr), task_team));
4009 kmp_flag_32<false, false> flag(
4010 RCAST(std::atomic<kmp_uint32> *,
4011 &task_team->tt.tt_unfinished_threads),
4013 flag.wait(this_thr, TRUE USE_ITT_BUILD_ARG(itt_sync_obj));
4019 (
"__kmp_task_team_wait: Primary T#%d deactivating task_team %p: "
4020 "setting active to false, setting local and team's pointer to NULL\n",
4021 __kmp_gtid_from_thread(this_thr), task_team));
4022 KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1 ||
4023 task_team->tt.tt_found_proxy_tasks == TRUE ||
4024 task_team->tt.tt_hidden_helper_task_encountered == TRUE);
4025 TCW_SYNC_4(task_team->tt.tt_found_proxy_tasks, FALSE);
4026 TCW_SYNC_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE);
4027 KMP_CHECK_UPDATE(task_team->tt.tt_untied_task_encountered, 0);
4028 TCW_SYNC_4(task_team->tt.tt_active, FALSE);
4031 TCW_PTR(this_thr->th.th_task_team, NULL);
4040 void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread,
int gtid) {
4041 std::atomic<kmp_uint32> *spin = RCAST(
4042 std::atomic<kmp_uint32> *,
4043 &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads);
4045 KMP_DEBUG_ASSERT(__kmp_tasking_mode == tskm_extra_barrier);
4048 KMP_FSYNC_SPIN_INIT(spin, NULL);
4050 kmp_flag_32<false, false> spin_flag(spin, 0U);
4051 while (!spin_flag.execute_tasks(thread, gtid, TRUE,
4052 &flag USE_ITT_BUILD_ARG(NULL), 0)) {
4055 KMP_FSYNC_SPIN_PREPARE(RCAST(
void *, spin));
4058 if (TCR_4(__kmp_global.g.g_done)) {
4059 if (__kmp_global.g.g_abort)
4060 __kmp_abort_thread();
4066 KMP_FSYNC_SPIN_ACQUIRED(RCAST(
void *, spin));
4075 static bool __kmp_give_task(kmp_info_t *thread, kmp_int32 tid, kmp_task_t *task,
4077 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4078 kmp_task_team_t *task_team = taskdata->td_task_team;
4080 KA_TRACE(20, (
"__kmp_give_task: trying to give task %p to thread %d.\n",
4084 KMP_DEBUG_ASSERT(task_team != NULL);
4086 bool result =
false;
4087 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
4089 if (thread_data->td.td_deque == NULL) {
4093 (
"__kmp_give_task: thread %d has no queue while giving task %p.\n",
4098 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4099 TASK_DEQUE_SIZE(thread_data->td)) {
4102 (
"__kmp_give_task: queue is full while giving task %p to thread %d.\n",
4107 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
4110 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
4111 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4112 TASK_DEQUE_SIZE(thread_data->td)) {
4114 __kmp_realloc_task_deque(thread, thread_data);
4119 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
4121 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4122 TASK_DEQUE_SIZE(thread_data->td)) {
4123 KA_TRACE(30, (
"__kmp_give_task: queue is full while giving task %p to "
4129 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
4130 goto release_and_exit;
4132 __kmp_realloc_task_deque(thread, thread_data);
4138 thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
4140 thread_data->td.td_deque_tail =
4141 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
4142 TCW_4(thread_data->td.td_deque_ntasks,
4143 TCR_4(thread_data->td.td_deque_ntasks) + 1);
4146 KA_TRACE(30, (
"__kmp_give_task: successfully gave task %p to thread %d.\n",
4150 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
4155 #define PROXY_TASK_FLAG 0x40000000
4172 static void __kmp_first_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
4173 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
4174 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4175 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
4176 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
4178 taskdata->td_flags.complete = 1;
4180 if (taskdata->td_taskgroup)
4181 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
4185 KMP_ATOMIC_OR(&taskdata->td_incomplete_child_tasks, PROXY_TASK_FLAG);
4188 static void __kmp_second_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
4190 kmp_int32 children = 0;
4194 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks);
4195 KMP_DEBUG_ASSERT(children >= 0);
4198 KMP_ATOMIC_AND(&taskdata->td_incomplete_child_tasks, ~PROXY_TASK_FLAG);
4201 static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask) {
4202 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4203 kmp_info_t *thread = __kmp_threads[gtid];
4205 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4206 KMP_DEBUG_ASSERT(taskdata->td_flags.complete ==
4211 while ((KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) &
4212 PROXY_TASK_FLAG) > 0)
4215 __kmp_release_deps(gtid, taskdata);
4216 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
4228 KMP_DEBUG_ASSERT(ptask != NULL);
4229 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4231 10, (
"__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n",
4233 __kmp_assert_valid_gtid(gtid);
4234 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4236 __kmp_first_top_half_finish_proxy(taskdata);
4237 __kmp_second_top_half_finish_proxy(taskdata);
4238 __kmp_bottom_half_finish_proxy(gtid, ptask);
4241 (
"__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n",
4245 void __kmpc_give_task(kmp_task_t *ptask, kmp_int32 start = 0) {
4246 KMP_DEBUG_ASSERT(ptask != NULL);
4247 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4251 kmp_team_t *team = taskdata->td_team;
4252 kmp_int32 nthreads = team->t.t_nproc;
4257 kmp_int32 start_k = start % nthreads;
4259 kmp_int32 k = start_k;
4263 thread = team->t.t_threads[k];
4264 k = (k + 1) % nthreads;
4270 }
while (!__kmp_give_task(thread, k, ptask, pass));
4281 KMP_DEBUG_ASSERT(ptask != NULL);
4282 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4286 (
"__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n",
4289 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
4291 __kmp_first_top_half_finish_proxy(taskdata);
4293 __kmpc_give_task(ptask);
4295 __kmp_second_top_half_finish_proxy(taskdata);
4299 (
"__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n",
4303 kmp_event_t *__kmpc_task_allow_completion_event(
ident_t *loc_ref,
int gtid,
4305 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(task);
4306 if (td->td_allow_completion_event.type == KMP_EVENT_UNINITIALIZED) {
4307 td->td_allow_completion_event.type = KMP_EVENT_ALLOW_COMPLETION;
4308 td->td_allow_completion_event.ed.task = task;
4309 __kmp_init_tas_lock(&td->td_allow_completion_event.lock);
4311 return &td->td_allow_completion_event;
4314 void __kmp_fulfill_event(kmp_event_t *event) {
4315 if (event->type == KMP_EVENT_ALLOW_COMPLETION) {
4316 kmp_task_t *ptask = event->ed.task;
4317 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
4318 bool detached =
false;
4319 int gtid = __kmp_get_gtid();
4324 __kmp_acquire_tas_lock(&event->lock, gtid);
4325 if (taskdata->td_flags.proxy == TASK_PROXY) {
4331 if (UNLIKELY(ompt_enabled.enabled))
4332 __ompt_task_finish(ptask, NULL, ompt_task_early_fulfill);
4335 event->type = KMP_EVENT_UNINITIALIZED;
4336 __kmp_release_tas_lock(&event->lock, gtid);
4342 if (UNLIKELY(ompt_enabled.enabled))
4343 __ompt_task_finish(ptask, NULL, ompt_task_late_fulfill);
4347 kmp_team_t *team = taskdata->td_team;
4348 kmp_info_t *thread = __kmp_get_thread();
4349 if (thread->th.th_team == team) {
4367 kmp_task_t *__kmp_task_dup_alloc(kmp_info_t *thread, kmp_task_t *task_src) {
4369 kmp_taskdata_t *taskdata;
4370 kmp_taskdata_t *taskdata_src = KMP_TASK_TO_TASKDATA(task_src);
4371 kmp_taskdata_t *parent_task = taskdata_src->td_parent;
4372 size_t shareds_offset;
4375 KA_TRACE(10, (
"__kmp_task_dup_alloc(enter): Th %p, source task %p\n", thread,
4377 KMP_DEBUG_ASSERT(taskdata_src->td_flags.proxy ==
4379 KMP_DEBUG_ASSERT(taskdata_src->td_flags.tasktype == TASK_EXPLICIT);
4380 task_size = taskdata_src->td_size_alloc;
4383 KA_TRACE(30, (
"__kmp_task_dup_alloc: Th %p, malloc size %ld\n", thread,
4386 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, task_size);
4388 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, task_size);
4390 KMP_MEMCPY(taskdata, taskdata_src, task_size);
4392 task = KMP_TASKDATA_TO_TASK(taskdata);
4395 taskdata->td_task_id = KMP_GEN_TASK_ID();
4396 if (task->shareds != NULL) {
4397 shareds_offset = (
char *)task_src->shareds - (
char *)taskdata_src;
4398 task->shareds = &((
char *)taskdata)[shareds_offset];
4399 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
4402 taskdata->td_alloc_thread = thread;
4403 taskdata->td_parent = parent_task;
4405 taskdata->td_taskgroup = parent_task->td_taskgroup;
4408 if (taskdata->td_flags.tiedness == TASK_TIED)
4409 taskdata->td_last_tied = taskdata;
4413 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
4414 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
4415 if (parent_task->td_taskgroup)
4416 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
4419 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT)
4420 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
4424 (
"__kmp_task_dup_alloc(exit): Th %p, created task %p, parent=%p\n",
4425 thread, taskdata, taskdata->td_parent));
4427 if (UNLIKELY(ompt_enabled.enabled))
4428 __ompt_task_init(taskdata, thread->th.th_info.ds.ds_gtid);
4437 typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32);
4439 KMP_BUILD_ASSERT(
sizeof(
long) == 4 ||
sizeof(
long) == 8);
4444 class kmp_taskloop_bounds_t {
4446 const kmp_taskdata_t *taskdata;
4447 size_t lower_offset;
4448 size_t upper_offset;
4451 kmp_taskloop_bounds_t(kmp_task_t *_task, kmp_uint64 *lb, kmp_uint64 *ub)
4452 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(task)),
4453 lower_offset((char *)lb - (char *)task),
4454 upper_offset((char *)ub - (char *)task) {
4455 KMP_DEBUG_ASSERT((
char *)lb > (
char *)_task);
4456 KMP_DEBUG_ASSERT((
char *)ub > (
char *)_task);
4458 kmp_taskloop_bounds_t(kmp_task_t *_task,
const kmp_taskloop_bounds_t &bounds)
4459 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(_task)),
4460 lower_offset(bounds.lower_offset), upper_offset(bounds.upper_offset) {}
4461 size_t get_lower_offset()
const {
return lower_offset; }
4462 size_t get_upper_offset()
const {
return upper_offset; }
4463 kmp_uint64 get_lb()
const {
4465 #if defined(KMP_GOMP_COMPAT)
4467 if (!taskdata->td_flags.native) {
4468 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4471 if (taskdata->td_size_loop_bounds == 4) {
4472 kmp_int32 *lb = RCAST(kmp_int32 *, task->shareds);
4473 retval = (kmp_int64)*lb;
4475 kmp_int64 *lb = RCAST(kmp_int64 *, task->shareds);
4476 retval = (kmp_int64)*lb;
4481 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4485 kmp_uint64 get_ub()
const {
4487 #if defined(KMP_GOMP_COMPAT)
4489 if (!taskdata->td_flags.native) {
4490 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4493 if (taskdata->td_size_loop_bounds == 4) {
4494 kmp_int32 *ub = RCAST(kmp_int32 *, task->shareds) + 1;
4495 retval = (kmp_int64)*ub;
4497 kmp_int64 *ub = RCAST(kmp_int64 *, task->shareds) + 1;
4498 retval = (kmp_int64)*ub;
4502 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4506 void set_lb(kmp_uint64 lb) {
4507 #if defined(KMP_GOMP_COMPAT)
4509 if (!taskdata->td_flags.native) {
4510 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4513 if (taskdata->td_size_loop_bounds == 4) {
4514 kmp_uint32 *lower = RCAST(kmp_uint32 *, task->shareds);
4515 *lower = (kmp_uint32)lb;
4517 kmp_uint64 *lower = RCAST(kmp_uint64 *, task->shareds);
4518 *lower = (kmp_uint64)lb;
4522 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4525 void set_ub(kmp_uint64 ub) {
4526 #if defined(KMP_GOMP_COMPAT)
4528 if (!taskdata->td_flags.native) {
4529 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4532 if (taskdata->td_size_loop_bounds == 4) {
4533 kmp_uint32 *upper = RCAST(kmp_uint32 *, task->shareds) + 1;
4534 *upper = (kmp_uint32)ub;
4536 kmp_uint64 *upper = RCAST(kmp_uint64 *, task->shareds) + 1;
4537 *upper = (kmp_uint64)ub;
4541 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4562 void __kmp_taskloop_linear(
ident_t *loc,
int gtid, kmp_task_t *task,
4563 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4564 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
4565 kmp_uint64 grainsize, kmp_uint64 extras,
4566 kmp_int64 last_chunk, kmp_uint64 tc,
4572 KMP_TIME_PARTITIONED_BLOCK(OMP_taskloop_scheduling);
4573 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
4575 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4576 kmp_uint64 lower = task_bounds.get_lb();
4577 kmp_uint64 upper = task_bounds.get_ub();
4579 kmp_info_t *thread = __kmp_threads[gtid];
4580 kmp_taskdata_t *current_task = thread->th.th_current_task;
4581 kmp_task_t *next_task;
4582 kmp_int32 lastpriv = 0;
4584 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4585 (last_chunk < 0 ? last_chunk : extras));
4586 KMP_DEBUG_ASSERT(num_tasks > extras);
4587 KMP_DEBUG_ASSERT(num_tasks > 0);
4588 KA_TRACE(20, (
"__kmp_taskloop_linear: T#%d: %lld tasks, grainsize %lld, "
4589 "extras %lld, last_chunk %lld, i=%lld,%lld(%d)%lld, dup %p\n",
4590 gtid, num_tasks, grainsize, extras, last_chunk, lower, upper,
4591 ub_glob, st, task_dup));
4594 for (i = 0; i < num_tasks; ++i) {
4595 kmp_uint64 chunk_minus_1;
4597 chunk_minus_1 = grainsize - 1;
4599 chunk_minus_1 = grainsize;
4602 upper = lower + st * chunk_minus_1;
4606 if (i == num_tasks - 1) {
4609 KMP_DEBUG_ASSERT(upper == *ub);
4610 if (upper == ub_glob)
4612 }
else if (st > 0) {
4613 KMP_DEBUG_ASSERT((kmp_uint64)st > *ub - upper);
4614 if ((kmp_uint64)st > ub_glob - upper)
4617 KMP_DEBUG_ASSERT(upper + st < *ub);
4618 if (upper - ub_glob < (kmp_uint64)(-st))
4622 next_task = __kmp_task_dup_alloc(thread, task);
4623 kmp_taskdata_t *next_taskdata = KMP_TASK_TO_TASKDATA(next_task);
4624 kmp_taskloop_bounds_t next_task_bounds =
4625 kmp_taskloop_bounds_t(next_task, task_bounds);
4628 next_task_bounds.set_lb(lower);
4629 if (next_taskdata->td_flags.native) {
4630 next_task_bounds.set_ub(upper + (st > 0 ? 1 : -1));
4632 next_task_bounds.set_ub(upper);
4634 if (ptask_dup != NULL)
4636 ptask_dup(next_task, task, lastpriv);
4638 (
"__kmp_taskloop_linear: T#%d; task #%llu: task %p: lower %lld, "
4639 "upper %lld stride %lld, (offsets %p %p)\n",
4640 gtid, i, next_task, lower, upper, st,
4641 next_task_bounds.get_lower_offset(),
4642 next_task_bounds.get_upper_offset()));
4644 __kmp_omp_taskloop_task(NULL, gtid, next_task,
4647 __kmp_omp_task(gtid, next_task,
true);
4652 __kmp_task_start(gtid, task, current_task);
4654 __kmp_task_finish<false>(gtid, task, current_task);
4659 typedef struct __taskloop_params {
4666 kmp_uint64 num_tasks;
4667 kmp_uint64 grainsize;
4669 kmp_int64 last_chunk;
4671 kmp_uint64 num_t_min;
4675 } __taskloop_params_t;
4677 void __kmp_taskloop_recur(
ident_t *,
int, kmp_task_t *, kmp_uint64 *,
4678 kmp_uint64 *, kmp_int64, kmp_uint64, kmp_uint64,
4679 kmp_uint64, kmp_uint64, kmp_int64, kmp_uint64,
4687 int __kmp_taskloop_task(
int gtid,
void *ptask) {
4688 __taskloop_params_t *p =
4689 (__taskloop_params_t *)((kmp_task_t *)ptask)->shareds;
4690 kmp_task_t *task = p->task;
4691 kmp_uint64 *lb = p->lb;
4692 kmp_uint64 *ub = p->ub;
4693 void *task_dup = p->task_dup;
4695 kmp_int64 st = p->st;
4696 kmp_uint64 ub_glob = p->ub_glob;
4697 kmp_uint64 num_tasks = p->num_tasks;
4698 kmp_uint64 grainsize = p->grainsize;
4699 kmp_uint64 extras = p->extras;
4700 kmp_int64 last_chunk = p->last_chunk;
4701 kmp_uint64 tc = p->tc;
4702 kmp_uint64 num_t_min = p->num_t_min;
4704 void *codeptr_ra = p->codeptr_ra;
4707 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4708 KMP_DEBUG_ASSERT(task != NULL);
4710 (
"__kmp_taskloop_task: T#%d, task %p: %lld tasks, grainsize"
4711 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
4712 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
4715 KMP_DEBUG_ASSERT(num_tasks * 2 + 1 > num_t_min);
4716 if (num_tasks > num_t_min)
4717 __kmp_taskloop_recur(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
4718 grainsize, extras, last_chunk, tc, num_t_min,
4724 __kmp_taskloop_linear(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
4725 grainsize, extras, last_chunk, tc,
4731 KA_TRACE(40, (
"__kmp_taskloop_task(exit): T#%d\n", gtid));
4753 void __kmp_taskloop_recur(
ident_t *loc,
int gtid, kmp_task_t *task,
4754 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4755 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
4756 kmp_uint64 grainsize, kmp_uint64 extras,
4757 kmp_int64 last_chunk, kmp_uint64 tc,
4758 kmp_uint64 num_t_min,
4763 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4764 KMP_DEBUG_ASSERT(task != NULL);
4765 KMP_DEBUG_ASSERT(num_tasks > num_t_min);
4767 (
"__kmp_taskloop_recur: T#%d, task %p: %lld tasks, grainsize"
4768 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
4769 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
4771 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
4772 kmp_uint64 lower = *lb;
4773 kmp_info_t *thread = __kmp_threads[gtid];
4775 kmp_task_t *next_task;
4776 size_t lower_offset =
4777 (
char *)lb - (
char *)task;
4778 size_t upper_offset =
4779 (
char *)ub - (
char *)task;
4781 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4782 (last_chunk < 0 ? last_chunk : extras));
4783 KMP_DEBUG_ASSERT(num_tasks > extras);
4784 KMP_DEBUG_ASSERT(num_tasks > 0);
4787 kmp_uint64 lb1, ub0, tc0, tc1, ext0, ext1;
4788 kmp_int64 last_chunk0 = 0, last_chunk1 = 0;
4789 kmp_uint64 gr_size0 = grainsize;
4790 kmp_uint64 n_tsk0 = num_tasks >> 1;
4791 kmp_uint64 n_tsk1 = num_tasks - n_tsk0;
4792 if (last_chunk < 0) {
4794 last_chunk1 = last_chunk;
4795 tc0 = grainsize * n_tsk0;
4797 }
else if (n_tsk0 <= extras) {
4800 ext1 = extras - n_tsk0;
4801 tc0 = gr_size0 * n_tsk0;
4806 tc1 = grainsize * n_tsk1;
4809 ub0 = lower + st * (tc0 - 1);
4813 next_task = __kmp_task_dup_alloc(thread, task);
4815 *(kmp_uint64 *)((
char *)next_task + lower_offset) = lb1;
4816 if (ptask_dup != NULL)
4817 ptask_dup(next_task, task, 0);
4822 kmp_taskdata_t *current_task = thread->th.th_current_task;
4823 thread->th.th_current_task = taskdata->td_parent;
4824 kmp_task_t *new_task =
4825 __kmpc_omp_task_alloc(loc, gtid, 1, 3 *
sizeof(
void *),
4826 sizeof(__taskloop_params_t), &__kmp_taskloop_task);
4828 thread->th.th_current_task = current_task;
4829 __taskloop_params_t *p = (__taskloop_params_t *)new_task->shareds;
4830 p->task = next_task;
4831 p->lb = (kmp_uint64 *)((
char *)next_task + lower_offset);
4832 p->ub = (kmp_uint64 *)((
char *)next_task + upper_offset);
4833 p->task_dup = task_dup;
4835 p->ub_glob = ub_glob;
4836 p->num_tasks = n_tsk1;
4837 p->grainsize = grainsize;
4839 p->last_chunk = last_chunk1;
4841 p->num_t_min = num_t_min;
4843 p->codeptr_ra = codeptr_ra;
4848 __kmp_omp_taskloop_task(NULL, gtid, new_task, codeptr_ra);
4850 __kmp_omp_task(gtid, new_task,
true);
4854 if (n_tsk0 > num_t_min)
4855 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0, gr_size0,
4856 ext0, last_chunk0, tc0, num_t_min,
4862 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0,
4863 gr_size0, ext0, last_chunk0, tc0,
4869 KA_TRACE(40, (
"__kmp_taskloop_recur(exit): T#%d\n", gtid));
4872 static void __kmp_taskloop(
ident_t *loc,
int gtid, kmp_task_t *task,
int if_val,
4873 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4874 int nogroup,
int sched, kmp_uint64 grainsize,
4875 int modifier,
void *task_dup) {
4876 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4877 KMP_DEBUG_ASSERT(task != NULL);
4879 #if OMPT_SUPPORT && OMPT_OPTIONAL
4880 OMPT_STORE_RETURN_ADDRESS(gtid);
4882 __kmpc_taskgroup(loc, gtid);
4887 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4890 kmp_uint64 lower = task_bounds.get_lb();
4891 kmp_uint64 upper = task_bounds.get_ub();
4892 kmp_uint64 ub_glob = upper;
4893 kmp_uint64 num_tasks = 0, extras = 0;
4894 kmp_int64 last_chunk =
4896 kmp_uint64 num_tasks_min = __kmp_taskloop_min_tasks;
4897 kmp_info_t *thread = __kmp_threads[gtid];
4898 kmp_taskdata_t *current_task = thread->th.th_current_task;
4900 KA_TRACE(20, (
"__kmp_taskloop: T#%d, task %p, lb %lld, ub %lld, st %lld, "
4901 "grain %llu(%d, %d), dup %p\n",
4902 gtid, taskdata, lower, upper, st, grainsize, sched, modifier,
4907 tc = upper - lower + 1;
4908 }
else if (st < 0) {
4909 tc = (lower - upper) / (-st) + 1;
4911 tc = (upper - lower) / st + 1;
4914 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d zero-trip loop\n", gtid));
4916 __kmp_task_start(gtid, task, current_task);
4918 __kmp_task_finish<false>(gtid, task, current_task);
4922 #if OMPT_SUPPORT && OMPT_OPTIONAL
4923 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
4924 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
4925 if (ompt_enabled.ompt_callback_work) {
4926 ompt_callbacks.ompt_callback(ompt_callback_work)(
4927 ompt_work_taskloop, ompt_scope_begin, &(team_info->parallel_data),
4928 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
4932 if (num_tasks_min == 0)
4935 KMP_MIN(thread->th.th_team_nproc * 10, INITIAL_TASK_DEQUE_SIZE);
4941 grainsize = thread->th.th_team_nproc * 10;
4944 if (grainsize > tc) {
4949 num_tasks = grainsize;
4950 grainsize = tc / num_tasks;
4951 extras = tc % num_tasks;
4955 if (grainsize > tc) {
4961 num_tasks = (tc + grainsize - 1) / grainsize;
4962 last_chunk = tc - (num_tasks * grainsize);
4965 num_tasks = tc / grainsize;
4967 grainsize = tc / num_tasks;
4968 extras = tc % num_tasks;
4973 KMP_ASSERT2(0,
"unknown scheduling of taskloop");
4976 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4977 (last_chunk < 0 ? last_chunk : extras));
4978 KMP_DEBUG_ASSERT(num_tasks > extras);
4979 KMP_DEBUG_ASSERT(num_tasks > 0);
4985 taskdata->td_flags.task_serial = 1;
4986 taskdata->td_flags.tiedness = TASK_TIED;
4988 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4989 grainsize, extras, last_chunk, tc,
4991 OMPT_GET_RETURN_ADDRESS(0),
4996 }
else if (num_tasks > num_tasks_min && !taskdata->td_flags.native) {
4997 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go recursive: tc %llu, #tasks %llu"
4998 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
4999 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
5001 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5002 grainsize, extras, last_chunk, tc, num_tasks_min,
5004 OMPT_GET_RETURN_ADDRESS(0),
5008 KA_TRACE(20, (
"__kmp_taskloop: T#%d, go linear: tc %llu, #tasks %llu"
5009 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
5010 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
5012 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5013 grainsize, extras, last_chunk, tc,
5015 OMPT_GET_RETURN_ADDRESS(0),
5020 #if OMPT_SUPPORT && OMPT_OPTIONAL
5021 if (ompt_enabled.ompt_callback_work) {
5022 ompt_callbacks.ompt_callback(ompt_callback_work)(
5023 ompt_work_taskloop, ompt_scope_end, &(team_info->parallel_data),
5024 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
5029 #if OMPT_SUPPORT && OMPT_OPTIONAL
5030 OMPT_STORE_RETURN_ADDRESS(gtid);
5032 __kmpc_end_taskgroup(loc, gtid);
5034 KA_TRACE(20, (
"__kmp_taskloop(exit): T#%d\n", gtid));
5054 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
int nogroup,
5055 int sched, kmp_uint64 grainsize,
void *task_dup) {
5056 __kmp_assert_valid_gtid(gtid);
5057 KA_TRACE(20, (
"__kmpc_taskloop(enter): T#%d\n", gtid));
5058 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
5060 KA_TRACE(20, (
"__kmpc_taskloop(exit): T#%d\n", gtid));
5081 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
5082 int nogroup,
int sched, kmp_uint64 grainsize,
5083 int modifier,
void *task_dup) {
5084 __kmp_assert_valid_gtid(gtid);
5085 KA_TRACE(20, (
"__kmpc_taskloop_5(enter): T#%d\n", gtid));
5086 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
5087 modifier, task_dup);
5088 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