16 #include "kmp_stats.h" 17 #include "kmp_wait_release.h" 18 #include "kmp_taskdeps.h" 21 #include "ompt-specific.h" 24 #include "tsan_annotations.h" 27 static void __kmp_enable_tasking(kmp_task_team_t *task_team,
28 kmp_info_t *this_thr);
29 static void __kmp_alloc_task_deque(kmp_info_t *thread,
30 kmp_thread_data_t *thread_data);
31 static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
32 kmp_task_team_t *task_team);
33 static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask);
35 #ifdef BUILD_TIED_TASK_STACK 44 static void __kmp_trace_task_stack(kmp_int32 gtid,
45 kmp_thread_data_t *thread_data,
46 int threshold,
char *location) {
47 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
48 kmp_taskdata_t **stack_top = task_stack->ts_top;
49 kmp_int32 entries = task_stack->ts_entries;
50 kmp_taskdata_t *tied_task;
54 (
"__kmp_trace_task_stack(start): location = %s, gtid = %d, entries = %d, " 55 "first_block = %p, stack_top = %p \n",
56 location, gtid, entries, task_stack->ts_first_block, stack_top));
58 KMP_DEBUG_ASSERT(stack_top != NULL);
59 KMP_DEBUG_ASSERT(entries > 0);
61 while (entries != 0) {
62 KMP_DEBUG_ASSERT(stack_top != &task_stack->ts_first_block.sb_block[0]);
64 if (entries & TASK_STACK_INDEX_MASK == 0) {
65 kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(stack_top);
67 stack_block = stack_block->sb_prev;
68 stack_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE];
75 tied_task = *stack_top;
77 KMP_DEBUG_ASSERT(tied_task != NULL);
78 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
81 (
"__kmp_trace_task_stack(%s): gtid=%d, entry=%d, " 82 "stack_top=%p, tied_task=%p\n",
83 location, gtid, entries, stack_top, tied_task));
85 KMP_DEBUG_ASSERT(stack_top == &task_stack->ts_first_block.sb_block[0]);
88 (
"__kmp_trace_task_stack(exit): location = %s, gtid = %d\n",
98 static void __kmp_init_task_stack(kmp_int32 gtid,
99 kmp_thread_data_t *thread_data) {
100 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
101 kmp_stack_block_t *first_block;
104 first_block = &task_stack->ts_first_block;
105 task_stack->ts_top = (kmp_taskdata_t **)first_block;
106 memset((
void *)first_block,
'\0',
107 TASK_STACK_BLOCK_SIZE *
sizeof(kmp_taskdata_t *));
110 task_stack->ts_entries = TASK_STACK_EMPTY;
111 first_block->sb_next = NULL;
112 first_block->sb_prev = NULL;
119 static void __kmp_free_task_stack(kmp_int32 gtid,
120 kmp_thread_data_t *thread_data) {
121 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
122 kmp_stack_block_t *stack_block = &task_stack->ts_first_block;
124 KMP_DEBUG_ASSERT(task_stack->ts_entries == TASK_STACK_EMPTY);
126 while (stack_block != NULL) {
127 kmp_stack_block_t *next_block = (stack_block) ? stack_block->sb_next : NULL;
129 stack_block->sb_next = NULL;
130 stack_block->sb_prev = NULL;
131 if (stack_block != &task_stack->ts_first_block) {
132 __kmp_thread_free(thread,
135 stack_block = next_block;
138 task_stack->ts_entries = 0;
139 task_stack->ts_top = NULL;
148 static void __kmp_push_task_stack(kmp_int32 gtid, kmp_info_t *thread,
149 kmp_taskdata_t *tied_task) {
151 kmp_thread_data_t *thread_data =
152 &thread->th.th_task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
153 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
155 if (tied_task->td_flags.team_serial || tied_task->td_flags.tasking_ser) {
159 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
160 KMP_DEBUG_ASSERT(task_stack->ts_top != NULL);
163 (
"__kmp_push_task_stack(enter): GTID: %d; THREAD: %p; TASK: %p\n",
164 gtid, thread, tied_task));
166 *(task_stack->ts_top) = tied_task;
169 task_stack->ts_top++;
170 task_stack->ts_entries++;
172 if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) {
174 kmp_stack_block_t *stack_block =
175 (kmp_stack_block_t *)(task_stack->ts_top - TASK_STACK_BLOCK_SIZE);
178 if (stack_block->sb_next !=
180 task_stack->ts_top = &stack_block->sb_next->sb_block[0];
182 kmp_stack_block_t *new_block = (kmp_stack_block_t *)__kmp_thread_calloc(
183 thread,
sizeof(kmp_stack_block_t));
185 task_stack->ts_top = &new_block->sb_block[0];
186 stack_block->sb_next = new_block;
187 new_block->sb_prev = stack_block;
188 new_block->sb_next = NULL;
192 (
"__kmp_push_task_stack(): GTID: %d; TASK: %p; Alloc new block: %p\n",
193 gtid, tied_task, new_block));
196 KA_TRACE(20, (
"__kmp_push_task_stack(exit): GTID: %d; TASK: %p\n", gtid,
207 static void __kmp_pop_task_stack(kmp_int32 gtid, kmp_info_t *thread,
208 kmp_taskdata_t *ending_task) {
210 kmp_thread_data_t *thread_data =
211 &thread->th.th_task_team->tt_threads_data[__kmp_tid_from_gtid(gtid)];
212 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
213 kmp_taskdata_t *tied_task;
215 if (ending_task->td_flags.team_serial || ending_task->td_flags.tasking_ser) {
220 KMP_DEBUG_ASSERT(task_stack->ts_top != NULL);
221 KMP_DEBUG_ASSERT(task_stack->ts_entries > 0);
223 KA_TRACE(20, (
"__kmp_pop_task_stack(enter): GTID: %d; THREAD: %p\n", gtid,
227 if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) {
228 kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(task_stack->ts_top);
230 stack_block = stack_block->sb_prev;
231 task_stack->ts_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE];
235 task_stack->ts_top--;
236 task_stack->ts_entries--;
238 tied_task = *(task_stack->ts_top);
240 KMP_DEBUG_ASSERT(tied_task != NULL);
241 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
242 KMP_DEBUG_ASSERT(tied_task == ending_task);
244 KA_TRACE(20, (
"__kmp_pop_task_stack(exit): GTID: %d; TASK: %p\n", gtid,
253 static bool __kmp_task_is_allowed(
int gtid,
const kmp_int32 is_constrained,
254 const kmp_taskdata_t *tasknew,
255 const kmp_taskdata_t *taskcurr) {
256 if (is_constrained && (tasknew->td_flags.tiedness == TASK_TIED)) {
260 kmp_taskdata_t *current = taskcurr->td_last_tied;
261 KMP_DEBUG_ASSERT(current != NULL);
263 if (current->td_flags.tasktype == TASK_EXPLICIT ||
264 current->td_taskwait_thread > 0) {
265 kmp_int32 level = current->td_level;
266 kmp_taskdata_t *parent = tasknew->td_parent;
267 while (parent != current && parent->td_level > level) {
269 parent = parent->td_parent;
270 KMP_DEBUG_ASSERT(parent != NULL);
272 if (parent != current)
277 kmp_depnode_t *node = tasknew->td_depnode;
278 if (node && (node->dn.mtx_num_locks > 0)) {
279 for (
int i = 0; i < node->dn.mtx_num_locks; ++i) {
280 KMP_DEBUG_ASSERT(node->dn.mtx_locks[i] != NULL);
281 if (__kmp_test_lock(node->dn.mtx_locks[i], gtid))
284 for (
int j = i - 1; j >= 0; --j)
285 __kmp_release_lock(node->dn.mtx_locks[j], gtid);
289 node->dn.mtx_num_locks = -node->dn.mtx_num_locks;
298 static void __kmp_realloc_task_deque(kmp_info_t *thread,
299 kmp_thread_data_t *thread_data) {
300 kmp_int32 size = TASK_DEQUE_SIZE(thread_data->td);
301 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == size);
302 kmp_int32 new_size = 2 * size;
304 KE_TRACE(10, (
"__kmp_realloc_task_deque: T#%d reallocating deque[from %d to " 305 "%d] for thread_data %p\n",
306 __kmp_gtid_from_thread(thread), size, new_size, thread_data));
308 kmp_taskdata_t **new_deque =
309 (kmp_taskdata_t **)__kmp_allocate(new_size *
sizeof(kmp_taskdata_t *));
312 for (i = thread_data->td.td_deque_head, j = 0; j < size;
313 i = (i + 1) & TASK_DEQUE_MASK(thread_data->td), j++)
314 new_deque[j] = thread_data->td.td_deque[i];
316 __kmp_free(thread_data->td.td_deque);
318 thread_data->td.td_deque_head = 0;
319 thread_data->td.td_deque_tail = size;
320 thread_data->td.td_deque = new_deque;
321 thread_data->td.td_deque_size = new_size;
325 static kmp_int32 __kmp_push_task(kmp_int32 gtid, kmp_task_t *task) {
326 kmp_info_t *thread = __kmp_threads[gtid];
327 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
328 kmp_task_team_t *task_team = thread->th.th_task_team;
329 kmp_int32 tid = __kmp_tid_from_gtid(gtid);
330 kmp_thread_data_t *thread_data;
333 (
"__kmp_push_task: T#%d trying to push task %p.\n", gtid, taskdata));
335 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
338 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
339 KMP_DEBUG_USE_VAR(counter);
342 (
"__kmp_push_task: T#%d untied_count (%d) incremented for task %p\n",
343 gtid, counter, taskdata));
347 if (taskdata->td_flags.task_serial) {
348 KA_TRACE(20, (
"__kmp_push_task: T#%d team serialized; returning " 349 "TASK_NOT_PUSHED for task %p\n",
351 return TASK_NOT_PUSHED;
356 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
357 if (!KMP_TASKING_ENABLED(task_team)) {
358 __kmp_enable_tasking(task_team, thread);
360 KMP_DEBUG_ASSERT(TCR_4(task_team->tt.tt_found_tasks) == TRUE);
361 KMP_DEBUG_ASSERT(TCR_PTR(task_team->tt.tt_threads_data) != NULL);
364 thread_data = &task_team->tt.tt_threads_data[tid];
367 if (thread_data->td.td_deque == NULL) {
368 __kmp_alloc_task_deque(thread, thread_data);
373 if (TCR_4(thread_data->td.td_deque_ntasks) >=
374 TASK_DEQUE_SIZE(thread_data->td)) {
375 if (__kmp_enable_task_throttling &&
376 __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
377 thread->th.th_current_task)) {
378 KA_TRACE(20, (
"__kmp_push_task: T#%d deque is full; returning " 379 "TASK_NOT_PUSHED for task %p\n",
381 return TASK_NOT_PUSHED;
383 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
385 if (TCR_4(thread_data->td.td_deque_ntasks) >=
386 TASK_DEQUE_SIZE(thread_data->td)) {
388 __kmp_realloc_task_deque(thread, thread_data);
394 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
396 if (TCR_4(thread_data->td.td_deque_ntasks) >=
397 TASK_DEQUE_SIZE(thread_data->td)) {
398 if (__kmp_enable_task_throttling &&
399 __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
400 thread->th.th_current_task)) {
401 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
402 KA_TRACE(20, (
"__kmp_push_task: T#%d deque is full on 2nd check; " 403 "returning TASK_NOT_PUSHED for task %p\n",
405 return TASK_NOT_PUSHED;
408 __kmp_realloc_task_deque(thread, thread_data);
413 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) <
414 TASK_DEQUE_SIZE(thread_data->td));
416 thread_data->td.td_deque[thread_data->td.td_deque_tail] =
419 thread_data->td.td_deque_tail =
420 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
421 TCW_4(thread_data->td.td_deque_ntasks,
422 TCR_4(thread_data->td.td_deque_ntasks) + 1);
424 KA_TRACE(20, (
"__kmp_push_task: T#%d returning TASK_SUCCESSFULLY_PUSHED: " 425 "task=%p ntasks=%d head=%u tail=%u\n",
426 gtid, taskdata, thread_data->td.td_deque_ntasks,
427 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
429 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
431 return TASK_SUCCESSFULLY_PUSHED;
438 void __kmp_pop_current_task_from_thread(kmp_info_t *this_thr) {
439 KF_TRACE(10, (
"__kmp_pop_current_task_from_thread(enter): T#%d " 440 "this_thread=%p, curtask=%p, " 441 "curtask_parent=%p\n",
442 0, this_thr, this_thr->th.th_current_task,
443 this_thr->th.th_current_task->td_parent));
445 this_thr->th.th_current_task = this_thr->th.th_current_task->td_parent;
447 KF_TRACE(10, (
"__kmp_pop_current_task_from_thread(exit): T#%d " 448 "this_thread=%p, curtask=%p, " 449 "curtask_parent=%p\n",
450 0, this_thr, this_thr->th.th_current_task,
451 this_thr->th.th_current_task->td_parent));
460 void __kmp_push_current_task_to_thread(kmp_info_t *this_thr, kmp_team_t *team,
464 KF_TRACE(10, (
"__kmp_push_current_task_to_thread(enter): T#%d this_thread=%p " 467 tid, this_thr, this_thr->th.th_current_task,
468 team->t.t_implicit_task_taskdata[tid].td_parent));
470 KMP_DEBUG_ASSERT(this_thr != NULL);
473 if (this_thr->th.th_current_task != &team->t.t_implicit_task_taskdata[0]) {
474 team->t.t_implicit_task_taskdata[0].td_parent =
475 this_thr->th.th_current_task;
476 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[0];
479 team->t.t_implicit_task_taskdata[tid].td_parent =
480 team->t.t_implicit_task_taskdata[0].td_parent;
481 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[tid];
484 KF_TRACE(10, (
"__kmp_push_current_task_to_thread(exit): T#%d this_thread=%p " 487 tid, this_thr, this_thr->th.th_current_task,
488 team->t.t_implicit_task_taskdata[tid].td_parent));
496 static void __kmp_task_start(kmp_int32 gtid, kmp_task_t *task,
497 kmp_taskdata_t *current_task) {
498 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
499 kmp_info_t *thread = __kmp_threads[gtid];
502 (
"__kmp_task_start(enter): T#%d starting task %p: current_task=%p\n",
503 gtid, taskdata, current_task));
505 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
510 current_task->td_flags.executing = 0;
513 #ifdef BUILD_TIED_TASK_STACK 514 if (taskdata->td_flags.tiedness == TASK_TIED) {
515 __kmp_push_task_stack(gtid, thread, taskdata);
520 thread->th.th_current_task = taskdata;
522 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 0 ||
523 taskdata->td_flags.tiedness == TASK_UNTIED);
524 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0 ||
525 taskdata->td_flags.tiedness == TASK_UNTIED);
526 taskdata->td_flags.started = 1;
527 taskdata->td_flags.executing = 1;
528 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
529 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
536 KA_TRACE(10, (
"__kmp_task_start(exit): T#%d task=%p\n", gtid, taskdata));
547 static inline void __ompt_task_init(kmp_taskdata_t *task,
int tid) {
549 task->ompt_task_info.task_data.value = 0;
550 task->ompt_task_info.frame.exit_frame = ompt_data_none;
551 task->ompt_task_info.frame.enter_frame = ompt_data_none;
552 task->ompt_task_info.frame.exit_frame_flags = ompt_frame_runtime | ompt_frame_framepointer;
553 task->ompt_task_info.frame.enter_frame_flags = ompt_frame_runtime | ompt_frame_framepointer;
558 static inline void __ompt_task_start(kmp_task_t *task,
559 kmp_taskdata_t *current_task,
561 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
562 ompt_task_status_t status = ompt_task_switch;
563 if (__kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded) {
564 status = ompt_task_yield;
565 __kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded = 0;
568 if (ompt_enabled.ompt_callback_task_schedule) {
569 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
570 &(current_task->ompt_task_info.task_data), status,
571 &(taskdata->ompt_task_info.task_data));
573 taskdata->ompt_task_info.scheduling_parent = current_task;
578 static inline void __ompt_task_finish(kmp_task_t *task,
579 kmp_taskdata_t *resumed_task,
580 ompt_task_status_t status) {
581 if (ompt_enabled.ompt_callback_task_schedule) {
582 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
583 if (__kmp_omp_cancellation && taskdata->td_taskgroup &&
584 taskdata->td_taskgroup->cancel_request == cancel_taskgroup) {
585 status = ompt_task_cancel;
589 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
590 &(taskdata->ompt_task_info.task_data), status,
591 (resumed_task ? &(resumed_task->ompt_task_info.task_data) : NULL));
597 static void __kmpc_omp_task_begin_if0_template(
ident_t *loc_ref, kmp_int32 gtid,
600 void *return_address) {
601 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
602 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
604 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(enter): T#%d loc=%p task=%p " 606 gtid, loc_ref, taskdata, current_task));
608 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
611 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
612 KMP_DEBUG_USE_VAR(counter);
613 KA_TRACE(20, (
"__kmpc_omp_task_begin_if0: T#%d untied_count (%d) " 614 "incremented for task %p\n",
615 gtid, counter, taskdata));
618 taskdata->td_flags.task_serial =
620 __kmp_task_start(gtid, task, current_task);
624 if (current_task->ompt_task_info.frame.enter_frame.ptr == NULL) {
625 current_task->ompt_task_info.frame.enter_frame.ptr =
626 taskdata->ompt_task_info.frame.exit_frame.ptr = frame_address;
627 current_task->ompt_task_info.frame.enter_frame_flags =
628 taskdata->ompt_task_info.frame.exit_frame_flags = ompt_frame_application | ompt_frame_framepointer;
630 if (ompt_enabled.ompt_callback_task_create) {
631 ompt_task_info_t *parent_info = &(current_task->ompt_task_info);
632 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
633 &(parent_info->task_data), &(parent_info->frame),
634 &(taskdata->ompt_task_info.task_data),
635 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(taskdata), 0,
638 __ompt_task_start(task, current_task, gtid);
640 #endif // OMPT_SUPPORT 642 KA_TRACE(10, (
"__kmpc_omp_task_begin_if0(exit): T#%d loc=%p task=%p,\n", gtid,
648 static void __kmpc_omp_task_begin_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
651 void *return_address) {
652 __kmpc_omp_task_begin_if0_template<true>(loc_ref, gtid, task, frame_address,
655 #endif // OMPT_SUPPORT 663 void __kmpc_omp_task_begin_if0(
ident_t *loc_ref, kmp_int32 gtid,
666 if (UNLIKELY(ompt_enabled.enabled)) {
667 OMPT_STORE_RETURN_ADDRESS(gtid);
668 __kmpc_omp_task_begin_if0_ompt(loc_ref, gtid, task,
669 OMPT_GET_FRAME_ADDRESS(1),
670 OMPT_LOAD_RETURN_ADDRESS(gtid));
674 __kmpc_omp_task_begin_if0_template<false>(loc_ref, gtid, task, NULL, NULL);
680 void __kmpc_omp_task_begin(
ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task) {
681 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
685 (
"__kmpc_omp_task_begin(enter): T#%d loc=%p task=%p current_task=%p\n",
686 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task), current_task));
688 __kmp_task_start(gtid, task, current_task);
690 KA_TRACE(10, (
"__kmpc_omp_task_begin(exit): T#%d loc=%p task=%p,\n", gtid,
691 loc_ref, KMP_TASK_TO_TASKDATA(task)));
694 #endif // TASK_UNUSED 701 static void __kmp_free_task(kmp_int32 gtid, kmp_taskdata_t *taskdata,
702 kmp_info_t *thread) {
703 KA_TRACE(30, (
"__kmp_free_task: T#%d freeing data from task %p\n", gtid,
707 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
708 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0);
709 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 1);
710 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
711 KMP_DEBUG_ASSERT(taskdata->td_allocated_child_tasks == 0 ||
712 taskdata->td_flags.task_serial == 1);
713 KMP_DEBUG_ASSERT(taskdata->td_incomplete_child_tasks == 0);
715 taskdata->td_flags.freed = 1;
716 ANNOTATE_HAPPENS_BEFORE(taskdata);
719 __kmp_fast_free(thread, taskdata);
721 __kmp_thread_free(thread, taskdata);
724 KA_TRACE(20, (
"__kmp_free_task: T#%d freed task %p\n", gtid, taskdata));
733 static void __kmp_free_task_and_ancestors(kmp_int32 gtid,
734 kmp_taskdata_t *taskdata,
735 kmp_info_t *thread) {
738 kmp_int32 team_serial =
739 (taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) &&
740 !taskdata->td_flags.proxy;
741 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
743 kmp_int32 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
744 KMP_DEBUG_ASSERT(children >= 0);
747 while (children == 0) {
748 kmp_taskdata_t *parent_taskdata = taskdata->td_parent;
750 KA_TRACE(20, (
"__kmp_free_task_and_ancestors(enter): T#%d task %p complete " 751 "and freeing itself\n",
755 __kmp_free_task(gtid, taskdata, thread);
757 taskdata = parent_taskdata;
763 if (taskdata->td_flags.tasktype == TASK_IMPLICIT) {
764 if (taskdata->td_dephash) {
765 int children = KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks);
766 kmp_tasking_flags_t flags_old = taskdata->td_flags;
767 if (children == 0 && flags_old.complete == 1) {
768 kmp_tasking_flags_t flags_new = flags_old;
769 flags_new.complete = 0;
770 if (KMP_COMPARE_AND_STORE_ACQ32(
771 RCAST(kmp_int32 *, &taskdata->td_flags),
772 *RCAST(kmp_int32 *, &flags_old),
773 *RCAST(kmp_int32 *, &flags_new))) {
774 KA_TRACE(100, (
"__kmp_free_task_and_ancestors: T#%d cleans " 775 "dephash of implicit task %p\n",
778 __kmp_dephash_free_entries(thread, taskdata->td_dephash);
785 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
786 KMP_DEBUG_ASSERT(children >= 0);
790 20, (
"__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; " 791 "not freeing it yet\n",
792 gtid, taskdata, children));
805 static void __kmp_task_finish(kmp_int32 gtid, kmp_task_t *task,
806 kmp_taskdata_t *resumed_task) {
807 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
808 kmp_info_t *thread = __kmp_threads[gtid];
809 kmp_task_team_t *task_team =
810 thread->th.th_task_team;
811 kmp_int32 children = 0;
813 KA_TRACE(10, (
"__kmp_task_finish(enter): T#%d finishing task %p and resuming " 815 gtid, taskdata, resumed_task));
817 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
820 #ifdef BUILD_TIED_TASK_STACK 821 if (taskdata->td_flags.tiedness == TASK_TIED) {
822 __kmp_pop_task_stack(gtid, thread, taskdata);
826 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
829 kmp_int32 counter = KMP_ATOMIC_DEC(&taskdata->td_untied_count) - 1;
832 (
"__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n",
833 gtid, counter, taskdata));
837 if (resumed_task == NULL) {
838 KMP_DEBUG_ASSERT(taskdata->td_flags.task_serial);
839 resumed_task = taskdata->td_parent;
842 thread->th.th_current_task = resumed_task;
843 resumed_task->td_flags.executing = 1;
844 KA_TRACE(10, (
"__kmp_task_finish(exit): T#%d partially done task %p, " 845 "resuming task %p\n",
846 gtid, taskdata, resumed_task));
852 kmp_depnode_t *node = taskdata->td_depnode;
853 if (node && (node->dn.mtx_num_locks < 0)) {
855 node->dn.mtx_num_locks = -node->dn.mtx_num_locks;
856 for (
int i = node->dn.mtx_num_locks - 1; i >= 0; --i) {
857 KMP_DEBUG_ASSERT(node->dn.mtx_locks[i] != NULL);
858 __kmp_release_lock(node->dn.mtx_locks[i], gtid);
865 (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) ==
866 taskdata->td_flags.task_serial);
867 if (taskdata->td_flags.task_serial) {
868 if (resumed_task == NULL) {
869 resumed_task = taskdata->td_parent;
873 KMP_DEBUG_ASSERT(resumed_task !=
883 if (taskdata->td_flags.destructors_thunk) {
884 kmp_routine_entry_t destr_thunk = task->data1.destructors;
885 KMP_ASSERT(destr_thunk);
886 destr_thunk(gtid, task);
889 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
890 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 1);
891 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
894 if (taskdata->td_flags.detachable == TASK_DETACHABLE) {
895 if (taskdata->td_allow_completion_event.type ==
896 KMP_EVENT_ALLOW_COMPLETION) {
898 __kmp_acquire_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
899 if (taskdata->td_allow_completion_event.type ==
900 KMP_EVENT_ALLOW_COMPLETION) {
902 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
903 taskdata->td_flags.executing = 0;
910 __ompt_task_finish(task, resumed_task, ompt_task_detach);
916 taskdata->td_flags.proxy = TASK_PROXY;
919 __kmp_release_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
924 taskdata->td_flags.complete = 1;
929 __ompt_task_finish(task, resumed_task, ompt_task_complete);
934 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) ||
935 taskdata->td_flags.detachable == TASK_DETACHABLE) {
938 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks) - 1;
939 KMP_DEBUG_ASSERT(children >= 0);
940 if (taskdata->td_taskgroup)
941 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
942 __kmp_release_deps(gtid, taskdata);
943 }
else if (task_team && task_team->tt.tt_found_proxy_tasks) {
946 __kmp_release_deps(gtid, taskdata);
952 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
953 taskdata->td_flags.executing = 0;
958 20, (
"__kmp_task_finish: T#%d finished task %p, %d incomplete children\n",
959 gtid, taskdata, children));
965 thread->th.th_current_task = resumed_task;
967 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
971 resumed_task->td_flags.executing = 1;
974 10, (
"__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n",
975 gtid, taskdata, resumed_task));
981 static void __kmpc_omp_task_complete_if0_template(
ident_t *loc_ref,
984 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n",
985 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
987 __kmp_task_finish<ompt>(gtid, task, NULL);
989 KA_TRACE(10, (
"__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n",
990 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
994 ompt_frame_t *ompt_frame;
995 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
996 ompt_frame->enter_frame = ompt_data_none;
997 ompt_frame->enter_frame_flags = ompt_frame_runtime | ompt_frame_framepointer;
1006 void __kmpc_omp_task_complete_if0_ompt(
ident_t *loc_ref, kmp_int32 gtid,
1008 __kmpc_omp_task_complete_if0_template<true>(loc_ref, gtid, task);
1010 #endif // OMPT_SUPPORT 1017 void __kmpc_omp_task_complete_if0(
ident_t *loc_ref, kmp_int32 gtid,
1020 if (UNLIKELY(ompt_enabled.enabled)) {
1021 __kmpc_omp_task_complete_if0_ompt(loc_ref, gtid, task);
1025 __kmpc_omp_task_complete_if0_template<false>(loc_ref, gtid, task);
1031 void __kmpc_omp_task_complete(
ident_t *loc_ref, kmp_int32 gtid,
1033 KA_TRACE(10, (
"__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n", gtid,
1034 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1036 __kmp_task_finish<false>(gtid, task,
1039 KA_TRACE(10, (
"__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n", gtid,
1040 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1043 #endif // TASK_UNUSED 1056 void __kmp_init_implicit_task(
ident_t *loc_ref, kmp_info_t *this_thr,
1057 kmp_team_t *team,
int tid,
int set_curr_task) {
1058 kmp_taskdata_t *task = &team->t.t_implicit_task_taskdata[tid];
1062 (
"__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n",
1063 tid, team, task, set_curr_task ?
"TRUE" :
"FALSE"));
1065 task->td_task_id = KMP_GEN_TASK_ID();
1066 task->td_team = team;
1069 task->td_ident = loc_ref;
1070 task->td_taskwait_ident = NULL;
1071 task->td_taskwait_counter = 0;
1072 task->td_taskwait_thread = 0;
1074 task->td_flags.tiedness = TASK_TIED;
1075 task->td_flags.tasktype = TASK_IMPLICIT;
1076 task->td_flags.proxy = TASK_FULL;
1079 task->td_flags.task_serial = 1;
1080 task->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1081 task->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1083 task->td_flags.started = 1;
1084 task->td_flags.executing = 1;
1085 task->td_flags.complete = 0;
1086 task->td_flags.freed = 0;
1088 task->td_depnode = NULL;
1089 task->td_last_tied = task;
1090 task->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1092 if (set_curr_task) {
1093 KMP_ATOMIC_ST_REL(&task->td_incomplete_child_tasks, 0);
1095 KMP_ATOMIC_ST_REL(&task->td_allocated_child_tasks, 0);
1096 task->td_taskgroup = NULL;
1097 task->td_dephash = NULL;
1098 __kmp_push_current_task_to_thread(this_thr, team, tid);
1100 KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0);
1101 KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0);
1105 if (UNLIKELY(ompt_enabled.enabled))
1106 __ompt_task_init(task, tid);
1109 KF_TRACE(10, (
"__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n", tid,
1118 void __kmp_finish_implicit_task(kmp_info_t *thread) {
1119 kmp_taskdata_t *task = thread->th.th_current_task;
1120 if (task->td_dephash) {
1122 task->td_flags.complete = 1;
1123 children = KMP_ATOMIC_LD_ACQ(&task->td_incomplete_child_tasks);
1124 kmp_tasking_flags_t flags_old = task->td_flags;
1125 if (children == 0 && flags_old.complete == 1) {
1126 kmp_tasking_flags_t flags_new = flags_old;
1127 flags_new.complete = 0;
1128 if (KMP_COMPARE_AND_STORE_ACQ32(RCAST(kmp_int32 *, &task->td_flags),
1129 *RCAST(kmp_int32 *, &flags_old),
1130 *RCAST(kmp_int32 *, &flags_new))) {
1131 KA_TRACE(100, (
"__kmp_finish_implicit_task: T#%d cleans " 1132 "dephash of implicit task %p\n",
1133 thread->th.th_info.ds.ds_gtid, task));
1134 __kmp_dephash_free_entries(thread, task->td_dephash);
1144 void __kmp_free_implicit_task(kmp_info_t *thread) {
1145 kmp_taskdata_t *task = thread->th.th_current_task;
1146 if (task && task->td_dephash) {
1147 __kmp_dephash_free(thread, task->td_dephash);
1148 task->td_dephash = NULL;
1154 static size_t __kmp_round_up_to_val(
size_t size,
size_t val) {
1155 if (size & (val - 1)) {
1157 if (size <= KMP_SIZE_T_MAX - val) {
1176 kmp_task_t *__kmp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1177 kmp_tasking_flags_t *flags,
1178 size_t sizeof_kmp_task_t,
size_t sizeof_shareds,
1179 kmp_routine_entry_t task_entry) {
1181 kmp_taskdata_t *taskdata;
1182 kmp_info_t *thread = __kmp_threads[gtid];
1183 kmp_team_t *team = thread->th.th_team;
1184 kmp_taskdata_t *parent_task = thread->th.th_current_task;
1185 size_t shareds_offset;
1187 if (!TCR_4(__kmp_init_middle))
1188 __kmp_middle_initialize();
1190 KA_TRACE(10, (
"__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) " 1191 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1192 gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t,
1193 sizeof_shareds, task_entry));
1195 if (parent_task->td_flags.final) {
1196 if (flags->merged_if0) {
1200 if (flags->tiedness == TASK_UNTIED && !team->t.t_serialized) {
1204 KMP_CHECK_UPDATE(thread->th.th_task_team->tt.tt_untied_task_encountered, 1);
1210 if (flags->proxy == TASK_PROXY || flags->detachable == TASK_DETACHABLE) {
1211 if (flags->proxy == TASK_PROXY) {
1212 flags->tiedness = TASK_UNTIED;
1213 flags->merged_if0 = 1;
1217 if ((thread->th.th_task_team) == NULL) {
1220 KMP_DEBUG_ASSERT(team->t.t_serialized);
1222 (
"T#%d creating task team in __kmp_task_alloc for proxy task\n",
1224 __kmp_task_team_setup(
1227 thread->th.th_task_team = team->t.t_task_team[thread->th.th_task_state];
1229 kmp_task_team_t *task_team = thread->th.th_task_team;
1232 if (!KMP_TASKING_ENABLED(task_team)) {
1235 (
"T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid));
1236 __kmp_enable_tasking(task_team, thread);
1237 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
1238 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
1240 if (thread_data->td.td_deque == NULL) {
1241 __kmp_alloc_task_deque(thread, thread_data);
1245 if (task_team->tt.tt_found_proxy_tasks == FALSE)
1246 TCW_4(task_team->tt.tt_found_proxy_tasks, TRUE);
1251 shareds_offset =
sizeof(kmp_taskdata_t) + sizeof_kmp_task_t;
1252 shareds_offset = __kmp_round_up_to_val(shareds_offset,
sizeof(
void *));
1255 KA_TRACE(30, (
"__kmp_task_alloc: T#%d First malloc size: %ld\n", gtid,
1257 KA_TRACE(30, (
"__kmp_task_alloc: T#%d Second malloc size: %ld\n", gtid,
1262 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, shareds_offset +
1265 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, shareds_offset +
1268 ANNOTATE_HAPPENS_AFTER(taskdata);
1270 task = KMP_TASKDATA_TO_TASK(taskdata);
1273 #if KMP_ARCH_X86 || KMP_ARCH_PPC64 || !KMP_HAVE_QUAD 1274 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(
double) - 1)) == 0);
1275 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(
double) - 1)) == 0);
1277 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (
sizeof(_Quad) - 1)) == 0);
1278 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (
sizeof(_Quad) - 1)) == 0);
1280 if (sizeof_shareds > 0) {
1282 task->shareds = &((
char *)taskdata)[shareds_offset];
1284 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
1287 task->shareds = NULL;
1289 task->routine = task_entry;
1292 taskdata->td_task_id = KMP_GEN_TASK_ID();
1293 taskdata->td_team = team;
1294 taskdata->td_alloc_thread = thread;
1295 taskdata->td_parent = parent_task;
1296 taskdata->td_level = parent_task->td_level + 1;
1297 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
1298 taskdata->td_ident = loc_ref;
1299 taskdata->td_taskwait_ident = NULL;
1300 taskdata->td_taskwait_counter = 0;
1301 taskdata->td_taskwait_thread = 0;
1302 KMP_DEBUG_ASSERT(taskdata->td_parent != NULL);
1304 if (flags->proxy == TASK_FULL)
1305 copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs);
1307 taskdata->td_flags.tiedness = flags->tiedness;
1308 taskdata->td_flags.final = flags->final;
1309 taskdata->td_flags.merged_if0 = flags->merged_if0;
1310 taskdata->td_flags.destructors_thunk = flags->destructors_thunk;
1311 taskdata->td_flags.proxy = flags->proxy;
1312 taskdata->td_flags.detachable = flags->detachable;
1313 taskdata->td_task_team = thread->th.th_task_team;
1314 taskdata->td_size_alloc = shareds_offset + sizeof_shareds;
1315 taskdata->td_flags.tasktype = TASK_EXPLICIT;
1318 taskdata->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1321 taskdata->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1327 taskdata->td_flags.task_serial =
1328 (parent_task->td_flags.final || taskdata->td_flags.team_serial ||
1329 taskdata->td_flags.tasking_ser || flags->merged_if0);
1331 taskdata->td_flags.started = 0;
1332 taskdata->td_flags.executing = 0;
1333 taskdata->td_flags.complete = 0;
1334 taskdata->td_flags.freed = 0;
1336 taskdata->td_flags.native = flags->native;
1338 KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0);
1340 KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1);
1341 taskdata->td_taskgroup =
1342 parent_task->td_taskgroup;
1343 taskdata->td_dephash = NULL;
1344 taskdata->td_depnode = NULL;
1345 if (flags->tiedness == TASK_UNTIED)
1346 taskdata->td_last_tied = NULL;
1348 taskdata->td_last_tied = taskdata;
1349 taskdata->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1351 if (UNLIKELY(ompt_enabled.enabled))
1352 __ompt_task_init(taskdata, gtid);
1356 if (flags->proxy == TASK_PROXY ||
1357 flags->detachable == TASK_DETACHABLE ||
1358 !(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser))
1360 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
1361 if (parent_task->td_taskgroup)
1362 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
1365 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) {
1366 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
1370 KA_TRACE(20, (
"__kmp_task_alloc(exit): T#%d created task %p parent=%p\n",
1371 gtid, taskdata, taskdata->td_parent));
1372 ANNOTATE_HAPPENS_BEFORE(task);
1377 kmp_task_t *__kmpc_omp_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1378 kmp_int32 flags,
size_t sizeof_kmp_task_t,
1379 size_t sizeof_shareds,
1380 kmp_routine_entry_t task_entry) {
1382 kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
1384 input_flags->native = FALSE;
1387 KA_TRACE(10, (
"__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s %s) " 1388 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1389 gtid, loc_ref, input_flags->tiedness ?
"tied " :
"untied",
1390 input_flags->proxy ?
"proxy" :
"",
1391 input_flags->detachable ?
"detachable" :
"", sizeof_kmp_task_t,
1392 sizeof_shareds, task_entry));
1394 retval = __kmp_task_alloc(loc_ref, gtid, input_flags, sizeof_kmp_task_t,
1395 sizeof_shareds, task_entry);
1397 KA_TRACE(20, (
"__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval));
1402 kmp_task_t *__kmpc_omp_target_task_alloc(
ident_t *loc_ref, kmp_int32 gtid,
1404 size_t sizeof_kmp_task_t,
1405 size_t sizeof_shareds,
1406 kmp_routine_entry_t task_entry,
1407 kmp_int64 device_id) {
1408 return __kmpc_omp_task_alloc(loc_ref, gtid, flags, sizeof_kmp_task_t,
1409 sizeof_shareds, task_entry);
1427 kmp_task_t *new_task, kmp_int32 naffins,
1428 kmp_task_affinity_info_t *affin_list) {
1437 static void __kmp_invoke_task(kmp_int32 gtid, kmp_task_t *task,
1438 kmp_taskdata_t *current_task) {
1439 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
1443 30, (
"__kmp_invoke_task(enter): T#%d invoking task %p, current_task=%p\n",
1444 gtid, taskdata, current_task));
1445 KMP_DEBUG_ASSERT(task);
1446 if (taskdata->td_flags.proxy == TASK_PROXY &&
1447 taskdata->td_flags.complete == 1) {
1452 (
"__kmp_invoke_task: T#%d running bottom finish for proxy task %p\n",
1455 __kmp_bottom_half_finish_proxy(gtid, task);
1457 KA_TRACE(30, (
"__kmp_invoke_task(exit): T#%d completed bottom finish for " 1458 "proxy task %p, resuming task %p\n",
1459 gtid, taskdata, current_task));
1467 ompt_thread_info_t oldInfo;
1468 if (UNLIKELY(ompt_enabled.enabled)) {
1470 thread = __kmp_threads[gtid];
1471 oldInfo = thread->th.ompt_thread_info;
1472 thread->th.ompt_thread_info.wait_id = 0;
1473 thread->th.ompt_thread_info.state = (thread->th.th_team_serialized)
1474 ? ompt_state_work_serial
1475 : ompt_state_work_parallel;
1476 taskdata->ompt_task_info.frame.exit_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1481 if (taskdata->td_flags.proxy != TASK_PROXY) {
1482 ANNOTATE_HAPPENS_AFTER(task);
1483 __kmp_task_start(gtid, task, current_task);
1489 if (__kmp_omp_cancellation) {
1490 thread = __kmp_threads[gtid];
1491 kmp_team_t *this_team = thread->th.th_team;
1492 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
1493 if ((taskgroup && taskgroup->cancel_request) ||
1494 (this_team->t.t_cancel_request == cancel_parallel)) {
1495 #if OMPT_SUPPORT && OMPT_OPTIONAL 1496 ompt_data_t *task_data;
1497 if (UNLIKELY(ompt_enabled.ompt_callback_cancel)) {
1498 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, NULL, NULL);
1499 ompt_callbacks.ompt_callback(ompt_callback_cancel)(
1501 ((taskgroup && taskgroup->cancel_request) ? ompt_cancel_taskgroup
1502 : ompt_cancel_parallel) |
1503 ompt_cancel_discarded_task,
1516 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
1517 taskdata->td_last_tied = current_task->td_last_tied;
1518 KMP_DEBUG_ASSERT(taskdata->td_last_tied);
1520 #if KMP_STATS_ENABLED 1522 switch (KMP_GET_THREAD_STATE()) {
1523 case FORK_JOIN_BARRIER:
1524 KMP_PUSH_PARTITIONED_TIMER(OMP_task_join_bar);
1527 KMP_PUSH_PARTITIONED_TIMER(OMP_task_plain_bar);
1530 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskyield);
1533 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskwait);
1536 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskgroup);
1539 KMP_PUSH_PARTITIONED_TIMER(OMP_task_immediate);
1542 #endif // KMP_STATS_ENABLED 1546 if (UNLIKELY(ompt_enabled.enabled))
1547 __ompt_task_start(task, current_task, gtid);
1550 #if USE_ITT_BUILD && USE_ITT_NOTIFY 1551 kmp_uint64 cur_time;
1552 kmp_int32 kmp_itt_count_task =
1553 __kmp_forkjoin_frames_mode == 3 && !taskdata->td_flags.task_serial &&
1554 current_task->td_flags.tasktype == TASK_IMPLICIT;
1555 if (kmp_itt_count_task) {
1556 thread = __kmp_threads[gtid];
1558 if (thread->th.th_bar_arrive_time)
1559 cur_time = __itt_get_timestamp();
1561 kmp_itt_count_task = 0;
1565 #ifdef KMP_GOMP_COMPAT 1566 if (taskdata->td_flags.native) {
1567 ((void (*)(
void *))(*(task->routine)))(task->shareds);
1571 (*(task->routine))(gtid, task);
1573 KMP_POP_PARTITIONED_TIMER();
1575 #if USE_ITT_BUILD && USE_ITT_NOTIFY 1576 if (kmp_itt_count_task) {
1578 thread->th.th_bar_arrive_time += (__itt_get_timestamp() - cur_time);
1586 if (taskdata->td_flags.proxy != TASK_PROXY) {
1587 ANNOTATE_HAPPENS_BEFORE(taskdata->td_parent);
1589 if (UNLIKELY(ompt_enabled.enabled)) {
1590 thread->th.ompt_thread_info = oldInfo;
1591 if (taskdata->td_flags.tiedness == TASK_TIED) {
1592 taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
1594 __kmp_task_finish<true>(gtid, task, current_task);
1597 __kmp_task_finish<false>(gtid, task, current_task);
1602 (
"__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n",
1603 gtid, taskdata, current_task));
1617 kmp_int32 __kmpc_omp_task_parts(
ident_t *loc_ref, kmp_int32 gtid,
1618 kmp_task_t *new_task) {
1619 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1621 KA_TRACE(10, (
"__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n", gtid,
1622 loc_ref, new_taskdata));
1625 kmp_taskdata_t *parent;
1626 if (UNLIKELY(ompt_enabled.enabled)) {
1627 parent = new_taskdata->td_parent;
1628 if (ompt_enabled.ompt_callback_task_create) {
1629 ompt_data_t task_data = ompt_data_none;
1630 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1631 parent ? &(parent->ompt_task_info.task_data) : &task_data,
1632 parent ? &(parent->ompt_task_info.frame) : NULL,
1633 &(new_taskdata->ompt_task_info.task_data), ompt_task_explicit, 0,
1634 OMPT_GET_RETURN_ADDRESS(0));
1642 if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1644 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1645 new_taskdata->td_flags.task_serial = 1;
1646 __kmp_invoke_task(gtid, new_task, current_task);
1651 (
"__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: " 1652 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
1653 gtid, loc_ref, new_taskdata));
1655 ANNOTATE_HAPPENS_BEFORE(new_task);
1657 if (UNLIKELY(ompt_enabled.enabled)) {
1658 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1661 return TASK_CURRENT_NOT_QUEUED;
1675 kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task,
1676 bool serialize_immediate) {
1677 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1681 if (new_taskdata->td_flags.proxy == TASK_PROXY ||
1682 __kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED)
1684 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1685 if (serialize_immediate)
1686 new_taskdata->td_flags.task_serial = 1;
1687 __kmp_invoke_task(gtid, new_task, current_task);
1690 ANNOTATE_HAPPENS_BEFORE(new_task);
1691 return TASK_CURRENT_NOT_QUEUED;
1706 kmp_int32 __kmpc_omp_task(
ident_t *loc_ref, kmp_int32 gtid,
1707 kmp_task_t *new_task) {
1709 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1711 #if KMP_DEBUG || OMPT_SUPPORT 1712 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1714 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1718 kmp_taskdata_t *parent = NULL;
1719 if (UNLIKELY(ompt_enabled.enabled)) {
1720 if (!new_taskdata->td_flags.started) {
1721 OMPT_STORE_RETURN_ADDRESS(gtid);
1722 parent = new_taskdata->td_parent;
1723 if (!parent->ompt_task_info.frame.enter_frame.ptr) {
1724 parent->ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1726 if (ompt_enabled.ompt_callback_task_create) {
1727 ompt_data_t task_data = ompt_data_none;
1728 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1729 parent ? &(parent->ompt_task_info.task_data) : &task_data,
1730 parent ? &(parent->ompt_task_info.frame) : NULL,
1731 &(new_taskdata->ompt_task_info.task_data),
1732 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1733 OMPT_LOAD_RETURN_ADDRESS(gtid));
1738 __ompt_task_finish(new_task,
1739 new_taskdata->ompt_task_info.scheduling_parent,
1741 new_taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
1746 res = __kmp_omp_task(gtid, new_task,
true);
1748 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning " 1749 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1750 gtid, loc_ref, new_taskdata));
1752 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
1753 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1772 kmp_int32 __kmp_omp_taskloop_task(
ident_t *loc_ref, kmp_int32 gtid,
1773 kmp_task_t *new_task,
void *codeptr_ra) {
1775 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1777 #if KMP_DEBUG || OMPT_SUPPORT 1778 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1780 KA_TRACE(10, (
"__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1784 kmp_taskdata_t *parent = NULL;
1785 if (UNLIKELY(ompt_enabled.enabled && !new_taskdata->td_flags.started)) {
1786 parent = new_taskdata->td_parent;
1787 if (!parent->ompt_task_info.frame.enter_frame.ptr)
1788 parent->ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1789 if (ompt_enabled.ompt_callback_task_create) {
1790 ompt_data_t task_data = ompt_data_none;
1791 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1792 parent ? &(parent->ompt_task_info.task_data) : &task_data,
1793 parent ? &(parent->ompt_task_info.frame) : NULL,
1794 &(new_taskdata->ompt_task_info.task_data),
1795 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1801 res = __kmp_omp_task(gtid, new_task,
true);
1803 KA_TRACE(10, (
"__kmpc_omp_task(exit): T#%d returning " 1804 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1805 gtid, loc_ref, new_taskdata));
1807 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
1808 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1814 template <
bool ompt>
1815 static kmp_int32 __kmpc_omp_taskwait_template(
ident_t *loc_ref, kmp_int32 gtid,
1816 void *frame_address,
1817 void *return_address) {
1818 kmp_taskdata_t *taskdata;
1820 int thread_finished = FALSE;
1821 KMP_SET_THREAD_STATE_BLOCK(TASKWAIT);
1823 KA_TRACE(10, (
"__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref));
1825 if (__kmp_tasking_mode != tskm_immediate_exec) {
1826 thread = __kmp_threads[gtid];
1827 taskdata = thread->th.th_current_task;
1829 #if OMPT_SUPPORT && OMPT_OPTIONAL 1830 ompt_data_t *my_task_data;
1831 ompt_data_t *my_parallel_data;
1834 my_task_data = &(taskdata->ompt_task_info.task_data);
1835 my_parallel_data = OMPT_CUR_TEAM_DATA(thread);
1837 taskdata->ompt_task_info.frame.enter_frame.ptr = frame_address;
1839 if (ompt_enabled.ompt_callback_sync_region) {
1840 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
1841 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
1842 my_task_data, return_address);
1845 if (ompt_enabled.ompt_callback_sync_region_wait) {
1846 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
1847 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
1848 my_task_data, return_address);
1851 #endif // OMPT_SUPPORT && OMPT_OPTIONAL 1858 taskdata->td_taskwait_counter += 1;
1859 taskdata->td_taskwait_ident = loc_ref;
1860 taskdata->td_taskwait_thread = gtid + 1;
1863 void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
1864 if (itt_sync_obj != NULL)
1865 __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
1869 !taskdata->td_flags.team_serial && !taskdata->td_flags.final;
1871 must_wait = must_wait || (thread->th.th_task_team != NULL &&
1872 thread->th.th_task_team->tt.tt_found_proxy_tasks);
1874 kmp_flag_32 flag(RCAST(std::atomic<kmp_uint32> *,
1875 &(taskdata->td_incomplete_child_tasks)),
1877 while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) != 0) {
1878 flag.execute_tasks(thread, gtid, FALSE,
1879 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
1880 __kmp_task_stealing_constraint);
1884 if (itt_sync_obj != NULL)
1885 __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
1890 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
1892 #if OMPT_SUPPORT && OMPT_OPTIONAL 1894 if (ompt_enabled.ompt_callback_sync_region_wait) {
1895 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
1896 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
1897 my_task_data, return_address);
1899 if (ompt_enabled.ompt_callback_sync_region) {
1900 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
1901 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
1902 my_task_data, return_address);
1904 taskdata->ompt_task_info.frame.enter_frame = ompt_data_none;
1906 #endif // OMPT_SUPPORT && OMPT_OPTIONAL 1908 ANNOTATE_HAPPENS_AFTER(taskdata);
1911 KA_TRACE(10, (
"__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, " 1912 "returning TASK_CURRENT_NOT_QUEUED\n",
1915 return TASK_CURRENT_NOT_QUEUED;
1918 #if OMPT_SUPPORT && OMPT_OPTIONAL 1920 static kmp_int32 __kmpc_omp_taskwait_ompt(
ident_t *loc_ref, kmp_int32 gtid,
1921 void *frame_address,
1922 void *return_address) {
1923 return __kmpc_omp_taskwait_template<true>(loc_ref, gtid, frame_address,
1926 #endif // OMPT_SUPPORT && OMPT_OPTIONAL 1930 kmp_int32 __kmpc_omp_taskwait(
ident_t *loc_ref, kmp_int32 gtid) {
1931 #if OMPT_SUPPORT && OMPT_OPTIONAL 1932 if (UNLIKELY(ompt_enabled.enabled)) {
1933 OMPT_STORE_RETURN_ADDRESS(gtid);
1934 return __kmpc_omp_taskwait_ompt(loc_ref, gtid, OMPT_GET_FRAME_ADDRESS(0),
1935 OMPT_LOAD_RETURN_ADDRESS(gtid));
1938 return __kmpc_omp_taskwait_template<false>(loc_ref, gtid, NULL, NULL);
1942 kmp_int32 __kmpc_omp_taskyield(
ident_t *loc_ref, kmp_int32 gtid,
int end_part) {
1943 kmp_taskdata_t *taskdata;
1945 int thread_finished = FALSE;
1948 KMP_SET_THREAD_STATE_BLOCK(TASKYIELD);
1950 KA_TRACE(10, (
"__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n",
1951 gtid, loc_ref, end_part));
1953 if (__kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel) {
1954 thread = __kmp_threads[gtid];
1955 taskdata = thread->th.th_current_task;
1962 taskdata->td_taskwait_counter += 1;
1963 taskdata->td_taskwait_ident = loc_ref;
1964 taskdata->td_taskwait_thread = gtid + 1;
1967 void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
1968 if (itt_sync_obj != NULL)
1969 __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
1971 if (!taskdata->td_flags.team_serial) {
1972 kmp_task_team_t *task_team = thread->th.th_task_team;
1973 if (task_team != NULL) {
1974 if (KMP_TASKING_ENABLED(task_team)) {
1976 if (UNLIKELY(ompt_enabled.enabled))
1977 thread->th.ompt_thread_info.ompt_task_yielded = 1;
1979 __kmp_execute_tasks_32(
1980 thread, gtid, NULL, FALSE,
1981 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
1982 __kmp_task_stealing_constraint);
1984 if (UNLIKELY(ompt_enabled.enabled))
1985 thread->th.ompt_thread_info.ompt_task_yielded = 0;
1991 if (itt_sync_obj != NULL)
1992 __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
1997 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2000 KA_TRACE(10, (
"__kmpc_omp_taskyield(exit): T#%d task %p resuming, " 2001 "returning TASK_CURRENT_NOT_QUEUED\n",
2004 return TASK_CURRENT_NOT_QUEUED;
2025 unsigned reserved31 : 31;
2080 item.reduce_orig = NULL;
2085 if (src.reduce_orig != NULL) {
2086 item.reduce_orig = src.reduce_orig;
2088 item.reduce_orig = src.reduce_shar;
2096 ((void (*)(
void *))item.reduce_init)((
char *)(item.reduce_priv) + offset);
2101 ((void (*)(
void *,
void *))item.reduce_init)(
2102 (
char *)(item.reduce_priv) + offset, item.reduce_orig);
2105 template <
typename T>
2106 void *__kmp_task_reduction_init(
int gtid,
int num, T *data) {
2107 kmp_info_t *thread = __kmp_threads[gtid];
2108 kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup;
2109 kmp_int32 nth = thread->th.th_team_nproc;
2113 KMP_ASSERT(tg != NULL);
2114 KMP_ASSERT(data != NULL);
2115 KMP_ASSERT(num > 0);
2117 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n",
2121 KA_TRACE(10, (
"__kmpc_task_reduction_init: T#%d, taskgroup %p, #items %d\n",
2125 for (
int i = 0; i < num; ++i) {
2126 size_t size = data[i].reduce_size - 1;
2128 size += CACHE_LINE - size % CACHE_LINE;
2129 KMP_ASSERT(data[i].reduce_comb != NULL);
2132 arr[i].
flags = data[i].flags;
2136 __kmp_assign_orig<T>(arr[i], data[i]);
2137 if (!arr[i].flags.lazy_priv) {
2139 arr[i].reduce_priv = __kmp_allocate(nth * size);
2140 arr[i].reduce_pend = (
char *)(arr[i].reduce_priv) + nth * size;
2141 if (arr[i].reduce_init != NULL) {
2143 for (
int j = 0; j < nth; ++j) {
2144 __kmp_call_init<T>(arr[i], j * size);
2151 arr[i].reduce_priv = __kmp_allocate(nth *
sizeof(
void *));
2154 tg->reduce_data = (
void *)arr;
2155 tg->reduce_num_data = num;
2194 template <
typename T>
2195 void __kmp_task_reduction_init_copy(kmp_info_t *thr,
int num, T *data,
2196 kmp_taskgroup_t *tg,
void *reduce_data) {
2198 KA_TRACE(20, (
"__kmp_task_reduction_init_copy: Th %p, init taskgroup %p," 2200 thr, tg, reduce_data));
2205 for (
int i = 0; i < num; ++i) {
2208 tg->reduce_data = (
void *)arr;
2209 tg->reduce_num_data = num;
2222 kmp_info_t *thread = __kmp_threads[gtid];
2223 kmp_int32 nth = thread->th.th_team_nproc;
2227 kmp_taskgroup_t *tg = (kmp_taskgroup_t *)tskgrp;
2229 tg = thread->th.th_current_task->td_taskgroup;
2230 KMP_ASSERT(tg != NULL);
2232 kmp_int32 num = tg->reduce_num_data;
2233 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
2235 KMP_ASSERT(data != NULL);
2236 while (tg != NULL) {
2237 for (
int i = 0; i < num; ++i) {
2238 if (!arr[i].flags.lazy_priv) {
2239 if (data == arr[i].reduce_shar ||
2240 (data >= arr[i].reduce_priv && data < arr[i].reduce_pend))
2241 return (
char *)(arr[i].
reduce_priv) + tid * arr[i].reduce_size;
2244 void **p_priv = (
void **)(arr[i].reduce_priv);
2245 if (data == arr[i].reduce_shar)
2248 for (
int j = 0; j < nth; ++j)
2249 if (data == p_priv[j])
2253 if (p_priv[tid] == NULL) {
2255 p_priv[tid] = __kmp_allocate(arr[i].reduce_size);
2256 if (arr[i].reduce_init != NULL) {
2257 if (arr[i].reduce_orig != NULL) {
2259 p_priv[tid], arr[i].reduce_orig);
2261 ((void (*)(
void *))arr[i].
reduce_init)(p_priv[tid]);
2270 num = tg->reduce_num_data;
2272 KMP_ASSERT2(0,
"Unknown task reduction item");
2278 static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg) {
2279 kmp_int32 nth = th->th.th_team_nproc;
2280 KMP_DEBUG_ASSERT(nth > 1);
2282 kmp_int32 num = tg->reduce_num_data;
2283 for (
int i = 0; i < num; ++i) {
2285 void (*f_fini)(
void *) = (
void (*)(
void *))(arr[i].
reduce_fini);
2286 void (*f_comb)(
void *,
void *) =
2288 if (!arr[i].flags.lazy_priv) {
2291 for (
int j = 0; j < nth; ++j) {
2292 void *priv_data = (
char *)pr_data + j * size;
2293 f_comb(sh_data, priv_data);
2298 void **pr_data = (
void **)(arr[i].reduce_priv);
2299 for (
int j = 0; j < nth; ++j) {
2300 if (pr_data[j] != NULL) {
2301 f_comb(sh_data, pr_data[j]);
2304 __kmp_free(pr_data[j]);
2308 __kmp_free(arr[i].reduce_priv);
2310 __kmp_thread_free(th, arr);
2311 tg->reduce_data = NULL;
2312 tg->reduce_num_data = 0;
2318 static void __kmp_task_reduction_clean(kmp_info_t *th, kmp_taskgroup_t *tg) {
2319 __kmp_thread_free(th, tg->reduce_data);
2320 tg->reduce_data = NULL;
2321 tg->reduce_num_data = 0;
2324 template <
typename T>
2325 void *__kmp_task_reduction_modifier_init(
ident_t *loc,
int gtid,
int is_ws,
2327 kmp_info_t *thr = __kmp_threads[gtid];
2328 kmp_int32 nth = thr->th.th_team_nproc;
2329 __kmpc_taskgroup(loc, gtid);
2332 (
"__kmpc_reduction_modifier_init: T#%d, tg %p, exiting nth=1\n",
2333 gtid, thr->th.th_current_task->td_taskgroup));
2334 return (
void *)thr->th.th_current_task->td_taskgroup;
2336 kmp_team_t *team = thr->th.th_team;
2338 kmp_taskgroup_t *tg;
2339 reduce_data = KMP_ATOMIC_LD_RLX(&team->t.t_tg_reduce_data[is_ws]);
2340 if (reduce_data == NULL &&
2341 __kmp_atomic_compare_store(&team->t.t_tg_reduce_data[is_ws], reduce_data,
2344 KMP_DEBUG_ASSERT(reduce_data == NULL);
2346 tg = (kmp_taskgroup_t *)__kmp_task_reduction_init<T>(gtid, num, data);
2350 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[0]) == 0);
2351 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[1]) == 0);
2352 KMP_ATOMIC_ST_REL(&team->t.t_tg_reduce_data[is_ws], reduce_data);
2355 (reduce_data = KMP_ATOMIC_LD_ACQ(&team->t.t_tg_reduce_data[is_ws])) ==
2359 KMP_DEBUG_ASSERT(reduce_data > (
void *)1);
2360 tg = thr->th.th_current_task->td_taskgroup;
2361 __kmp_task_reduction_init_copy<T>(thr, num, data, tg, reduce_data);
2383 int num,
void *data) {
2384 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2404 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2417 __kmpc_end_taskgroup(loc, gtid);
2421 void __kmpc_taskgroup(
ident_t *loc,
int gtid) {
2422 kmp_info_t *thread = __kmp_threads[gtid];
2423 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2424 kmp_taskgroup_t *tg_new =
2425 (kmp_taskgroup_t *)__kmp_thread_malloc(thread,
sizeof(kmp_taskgroup_t));
2426 KA_TRACE(10, (
"__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new));
2427 KMP_ATOMIC_ST_RLX(&tg_new->count, 0);
2428 KMP_ATOMIC_ST_RLX(&tg_new->cancel_request, cancel_noreq);
2429 tg_new->parent = taskdata->td_taskgroup;
2430 tg_new->reduce_data = NULL;
2431 tg_new->reduce_num_data = 0;
2432 taskdata->td_taskgroup = tg_new;
2434 #if OMPT_SUPPORT && OMPT_OPTIONAL 2435 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2436 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2438 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2439 kmp_team_t *team = thread->th.th_team;
2440 ompt_data_t my_task_data = taskdata->ompt_task_info.task_data;
2442 ompt_data_t my_parallel_data = team->t.ompt_team_info.parallel_data;
2444 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2445 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2446 &(my_task_data), codeptr);
2453 void __kmpc_end_taskgroup(
ident_t *loc,
int gtid) {
2454 kmp_info_t *thread = __kmp_threads[gtid];
2455 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2456 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
2457 int thread_finished = FALSE;
2459 #if OMPT_SUPPORT && OMPT_OPTIONAL 2461 ompt_data_t my_task_data;
2462 ompt_data_t my_parallel_data;
2464 if (UNLIKELY(ompt_enabled.enabled)) {
2465 team = thread->th.th_team;
2466 my_task_data = taskdata->ompt_task_info.task_data;
2468 my_parallel_data = team->t.ompt_team_info.parallel_data;
2469 codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2471 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2475 KA_TRACE(10, (
"__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc));
2476 KMP_DEBUG_ASSERT(taskgroup != NULL);
2477 KMP_SET_THREAD_STATE_BLOCK(TASKGROUP);
2479 if (__kmp_tasking_mode != tskm_immediate_exec) {
2481 taskdata->td_taskwait_counter += 1;
2482 taskdata->td_taskwait_ident = loc;
2483 taskdata->td_taskwait_thread = gtid + 1;
2487 void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
2488 if (itt_sync_obj != NULL)
2489 __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
2492 #if OMPT_SUPPORT && OMPT_OPTIONAL 2493 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2494 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2495 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2496 &(my_task_data), codeptr);
2500 if (!taskdata->td_flags.team_serial ||
2501 (thread->th.th_task_team != NULL &&
2502 thread->th.th_task_team->tt.tt_found_proxy_tasks)) {
2503 kmp_flag_32 flag(RCAST(std::atomic<kmp_uint32> *, &(taskgroup->count)),
2505 while (KMP_ATOMIC_LD_ACQ(&taskgroup->count) != 0) {
2506 flag.execute_tasks(thread, gtid, FALSE,
2507 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2508 __kmp_task_stealing_constraint);
2511 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2513 #if OMPT_SUPPORT && OMPT_OPTIONAL 2514 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2515 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2516 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2517 &(my_task_data), codeptr);
2522 if (itt_sync_obj != NULL)
2523 __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
2526 KMP_DEBUG_ASSERT(taskgroup->count == 0);
2528 if (taskgroup->reduce_data != NULL) {
2531 kmp_team_t *t = thread->th.th_team;
2535 if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[0])) != NULL &&
2538 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[0]);
2539 if (cnt == thread->th.th_team_nproc - 1) {
2542 __kmp_task_reduction_fini(thread, taskgroup);
2545 __kmp_thread_free(thread, reduce_data);
2546 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[0], NULL);
2547 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[0], 0);
2551 __kmp_task_reduction_clean(thread, taskgroup);
2553 }
else if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[1])) !=
2557 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[1]);
2558 if (cnt == thread->th.th_team_nproc - 1) {
2560 __kmp_task_reduction_fini(thread, taskgroup);
2563 __kmp_thread_free(thread, reduce_data);
2564 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[1], NULL);
2565 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[1], 0);
2569 __kmp_task_reduction_clean(thread, taskgroup);
2573 __kmp_task_reduction_fini(thread, taskgroup);
2577 taskdata->td_taskgroup = taskgroup->parent;
2578 __kmp_thread_free(thread, taskgroup);
2580 KA_TRACE(10, (
"__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n",
2582 ANNOTATE_HAPPENS_AFTER(taskdata);
2584 #if OMPT_SUPPORT && OMPT_OPTIONAL 2585 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2586 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2587 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2588 &(my_task_data), codeptr);
2594 static kmp_task_t *__kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid,
2595 kmp_task_team_t *task_team,
2596 kmp_int32 is_constrained) {
2598 kmp_taskdata_t *taskdata;
2599 kmp_thread_data_t *thread_data;
2602 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2603 KMP_DEBUG_ASSERT(task_team->tt.tt_threads_data !=
2606 thread_data = &task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
2608 KA_TRACE(10, (
"__kmp_remove_my_task(enter): T#%d ntasks=%d head=%u tail=%u\n",
2609 gtid, thread_data->td.td_deque_ntasks,
2610 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2612 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2614 (
"__kmp_remove_my_task(exit #1): T#%d No tasks to remove: " 2615 "ntasks=%d head=%u tail=%u\n",
2616 gtid, thread_data->td.td_deque_ntasks,
2617 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2621 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
2623 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
2624 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2626 (
"__kmp_remove_my_task(exit #2): T#%d No tasks to remove: " 2627 "ntasks=%d head=%u tail=%u\n",
2628 gtid, thread_data->td.td_deque_ntasks,
2629 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2633 tail = (thread_data->td.td_deque_tail - 1) &
2634 TASK_DEQUE_MASK(thread_data->td);
2635 taskdata = thread_data->td.td_deque[tail];
2637 if (!__kmp_task_is_allowed(gtid, is_constrained, taskdata,
2638 thread->th.th_current_task)) {
2640 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2642 (
"__kmp_remove_my_task(exit #3): T#%d TSC blocks tail task: " 2643 "ntasks=%d head=%u tail=%u\n",
2644 gtid, thread_data->td.td_deque_ntasks,
2645 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2649 thread_data->td.td_deque_tail = tail;
2650 TCW_4(thread_data->td.td_deque_ntasks, thread_data->td.td_deque_ntasks - 1);
2652 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2654 KA_TRACE(10, (
"__kmp_remove_my_task(exit #4): T#%d task %p removed: " 2655 "ntasks=%d head=%u tail=%u\n",
2656 gtid, taskdata, thread_data->td.td_deque_ntasks,
2657 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
2659 task = KMP_TASKDATA_TO_TASK(taskdata);
2666 static kmp_task_t *__kmp_steal_task(kmp_info_t *victim_thr, kmp_int32 gtid,
2667 kmp_task_team_t *task_team,
2668 std::atomic<kmp_int32> *unfinished_threads,
2669 int *thread_finished,
2670 kmp_int32 is_constrained) {
2672 kmp_taskdata_t *taskdata;
2673 kmp_taskdata_t *current;
2674 kmp_thread_data_t *victim_td, *threads_data;
2676 kmp_int32 victim_tid;
2678 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2680 threads_data = task_team->tt.tt_threads_data;
2681 KMP_DEBUG_ASSERT(threads_data != NULL);
2683 victim_tid = victim_thr->th.th_info.ds.ds_tid;
2684 victim_td = &threads_data[victim_tid];
2686 KA_TRACE(10, (
"__kmp_steal_task(enter): T#%d try to steal from T#%d: " 2687 "task_team=%p ntasks=%d head=%u tail=%u\n",
2688 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
2689 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
2690 victim_td->td.td_deque_tail));
2692 if (TCR_4(victim_td->td.td_deque_ntasks) == 0) {
2693 KA_TRACE(10, (
"__kmp_steal_task(exit #1): T#%d could not steal from T#%d: " 2694 "task_team=%p ntasks=%d head=%u tail=%u\n",
2695 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
2696 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
2697 victim_td->td.td_deque_tail));
2701 __kmp_acquire_bootstrap_lock(&victim_td->td.td_deque_lock);
2703 int ntasks = TCR_4(victim_td->td.td_deque_ntasks);
2706 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2707 KA_TRACE(10, (
"__kmp_steal_task(exit #2): T#%d could not steal from T#%d: " 2708 "task_team=%p ntasks=%d head=%u tail=%u\n",
2709 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2710 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2714 KMP_DEBUG_ASSERT(victim_td->td.td_deque != NULL);
2715 current = __kmp_threads[gtid]->th.th_current_task;
2716 taskdata = victim_td->td.td_deque[victim_td->td.td_deque_head];
2717 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
2719 victim_td->td.td_deque_head =
2720 (victim_td->td.td_deque_head + 1) & TASK_DEQUE_MASK(victim_td->td);
2722 if (!task_team->tt.tt_untied_task_encountered) {
2724 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2725 KA_TRACE(10, (
"__kmp_steal_task(exit #3): T#%d could not steal from " 2726 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
2727 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2728 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2733 target = victim_td->td.td_deque_head;
2735 for (i = 1; i < ntasks; ++i) {
2736 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
2737 taskdata = victim_td->td.td_deque[target];
2738 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
2744 if (taskdata == NULL) {
2746 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2747 KA_TRACE(10, (
"__kmp_steal_task(exit #4): T#%d could not steal from " 2748 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
2749 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2750 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2754 for (i = i + 1; i < ntasks; ++i) {
2756 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
2757 victim_td->td.td_deque[prev] = victim_td->td.td_deque[target];
2761 victim_td->td.td_deque_tail ==
2762 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(victim_td->td)));
2763 victim_td->td.td_deque_tail = target;
2765 if (*thread_finished) {
2771 count = KMP_ATOMIC_INC(unfinished_threads);
2775 (
"__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n",
2776 gtid, count + 1, task_team));
2778 *thread_finished = FALSE;
2780 TCW_4(victim_td->td.td_deque_ntasks, ntasks - 1);
2782 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2786 (
"__kmp_steal_task(exit #5): T#%d stole task %p from T#%d: " 2787 "task_team=%p ntasks=%d head=%u tail=%u\n",
2788 gtid, taskdata, __kmp_gtid_from_thread(victim_thr), task_team,
2789 ntasks, victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2791 task = KMP_TASKDATA_TO_TASK(taskdata);
2805 static inline int __kmp_execute_tasks_template(
2806 kmp_info_t *thread, kmp_int32 gtid, C *flag,
int final_spin,
2807 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
2808 kmp_int32 is_constrained) {
2809 kmp_task_team_t *task_team = thread->th.th_task_team;
2810 kmp_thread_data_t *threads_data;
2812 kmp_info_t *other_thread;
2813 kmp_taskdata_t *current_task = thread->th.th_current_task;
2814 std::atomic<kmp_int32> *unfinished_threads;
2815 kmp_int32 nthreads, victim_tid = -2, use_own_tasks = 1, new_victim = 0,
2816 tid = thread->th.th_info.ds.ds_tid;
2818 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2819 KMP_DEBUG_ASSERT(thread == __kmp_threads[gtid]);
2821 if (task_team == NULL || current_task == NULL)
2824 KA_TRACE(15, (
"__kmp_execute_tasks_template(enter): T#%d final_spin=%d " 2825 "*thread_finished=%d\n",
2826 gtid, final_spin, *thread_finished));
2828 thread->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
2829 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
2830 KMP_DEBUG_ASSERT(threads_data != NULL);
2832 nthreads = task_team->tt.tt_nproc;
2833 unfinished_threads = &(task_team->tt.tt_unfinished_threads);
2834 KMP_DEBUG_ASSERT(nthreads > 1 || task_team->tt.tt_found_proxy_tasks);
2835 KMP_DEBUG_ASSERT(*unfinished_threads >= 0);
2841 if (use_own_tasks) {
2842 task = __kmp_remove_my_task(thread, gtid, task_team, is_constrained);
2844 if ((task == NULL) && (nthreads > 1)) {
2848 if (victim_tid == -2) {
2849 victim_tid = threads_data[tid].td.td_deque_last_stolen;
2852 other_thread = threads_data[victim_tid].td.td_thr;
2854 if (victim_tid != -1) {
2856 }
else if (!new_victim) {
2862 victim_tid = __kmp_get_random(thread) % (nthreads - 1);
2863 if (victim_tid >= tid) {
2867 other_thread = threads_data[victim_tid].td.td_thr;
2877 if ((__kmp_tasking_mode == tskm_task_teams) &&
2878 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) &&
2879 (TCR_PTR(CCAST(
void *, other_thread->th.th_sleep_loc)) !=
2882 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(other_thread),
2883 other_thread->th.th_sleep_loc);
2896 task = __kmp_steal_task(other_thread, gtid, task_team,
2897 unfinished_threads, thread_finished,
2901 if (threads_data[tid].td.td_deque_last_stolen != victim_tid) {
2902 threads_data[tid].td.td_deque_last_stolen = victim_tid;
2909 KMP_CHECK_UPDATE(threads_data[tid].td.td_deque_last_stolen, -1);
2918 #if USE_ITT_BUILD && USE_ITT_NOTIFY 2919 if (__itt_sync_create_ptr || KMP_ITT_DEBUG) {
2920 if (itt_sync_obj == NULL) {
2922 itt_sync_obj = __kmp_itt_barrier_object(gtid, bs_forkjoin_barrier);
2924 __kmp_itt_task_starting(itt_sync_obj);
2927 __kmp_invoke_task(gtid, task, current_task);
2929 if (itt_sync_obj != NULL)
2930 __kmp_itt_task_finished(itt_sync_obj);
2937 if (flag == NULL || (!final_spin && flag->done_check())) {
2940 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
2944 if (thread->th.th_task_team == NULL) {
2947 KMP_YIELD(__kmp_library == library_throughput);
2950 if (!use_own_tasks && TCR_4(threads_data[tid].td.td_deque_ntasks) != 0) {
2951 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d stolen task spawned " 2952 "other tasks, restart\n",
2963 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks) == 0) {
2967 if (!*thread_finished) {
2970 count = KMP_ATOMIC_DEC(unfinished_threads) - 1;
2971 KA_TRACE(20, (
"__kmp_execute_tasks_template: T#%d dec " 2972 "unfinished_threads to %d task_team=%p\n",
2973 gtid, count, task_team));
2974 *thread_finished = TRUE;
2982 if (flag != NULL && flag->done_check()) {
2985 (
"__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
2993 if (thread->th.th_task_team == NULL) {
2995 (
"__kmp_execute_tasks_template: T#%d no more tasks\n", gtid));
3005 (
"__kmp_execute_tasks_template: T#%d can't find work\n", gtid));
3011 int __kmp_execute_tasks_32(
3012 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32 *flag,
int final_spin,
3013 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3014 kmp_int32 is_constrained) {
3015 return __kmp_execute_tasks_template(
3016 thread, gtid, flag, final_spin,
3017 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3020 int __kmp_execute_tasks_64(
3021 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64 *flag,
int final_spin,
3022 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3023 kmp_int32 is_constrained) {
3024 return __kmp_execute_tasks_template(
3025 thread, gtid, flag, final_spin,
3026 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3029 int __kmp_execute_tasks_oncore(
3030 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag,
int final_spin,
3031 int *thread_finished USE_ITT_BUILD_ARG(
void *itt_sync_obj),
3032 kmp_int32 is_constrained) {
3033 return __kmp_execute_tasks_template(
3034 thread, gtid, flag, final_spin,
3035 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3041 static void __kmp_enable_tasking(kmp_task_team_t *task_team,
3042 kmp_info_t *this_thr) {
3043 kmp_thread_data_t *threads_data;
3044 int nthreads, i, is_init_thread;
3046 KA_TRACE(10, (
"__kmp_enable_tasking(enter): T#%d\n",
3047 __kmp_gtid_from_thread(this_thr)));
3049 KMP_DEBUG_ASSERT(task_team != NULL);
3050 KMP_DEBUG_ASSERT(this_thr->th.th_team != NULL);
3052 nthreads = task_team->tt.tt_nproc;
3053 KMP_DEBUG_ASSERT(nthreads > 0);
3054 KMP_DEBUG_ASSERT(nthreads == this_thr->th.th_team->t.t_nproc);
3057 is_init_thread = __kmp_realloc_task_threads_data(this_thr, task_team);
3059 if (!is_init_thread) {
3063 (
"__kmp_enable_tasking(exit): T#%d: threads array already set up.\n",
3064 __kmp_gtid_from_thread(this_thr)));
3067 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
3068 KMP_DEBUG_ASSERT(threads_data != NULL);
3070 if (__kmp_tasking_mode == tskm_task_teams &&
3071 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME)) {
3075 for (i = 0; i < nthreads; i++) {
3076 volatile void *sleep_loc;
3077 kmp_info_t *thread = threads_data[i].td.td_thr;
3079 if (i == this_thr->th.th_info.ds.ds_tid) {
3088 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3090 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d waking up thread T#%d\n",
3091 __kmp_gtid_from_thread(this_thr),
3092 __kmp_gtid_from_thread(thread)));
3093 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc);
3095 KF_TRACE(50, (
"__kmp_enable_tasking: T#%d don't wake up thread T#%d\n",
3096 __kmp_gtid_from_thread(this_thr),
3097 __kmp_gtid_from_thread(thread)));
3102 KA_TRACE(10, (
"__kmp_enable_tasking(exit): T#%d\n",
3103 __kmp_gtid_from_thread(this_thr)));
3136 static kmp_task_team_t *__kmp_free_task_teams =
3139 kmp_bootstrap_lock_t __kmp_task_team_lock =
3140 KMP_BOOTSTRAP_LOCK_INITIALIZER(__kmp_task_team_lock);
3147 static void __kmp_alloc_task_deque(kmp_info_t *thread,
3148 kmp_thread_data_t *thread_data) {
3149 __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
3150 KMP_DEBUG_ASSERT(thread_data->td.td_deque == NULL);
3153 thread_data->td.td_deque_last_stolen = -1;
3155 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == 0);
3156 KMP_DEBUG_ASSERT(thread_data->td.td_deque_head == 0);
3157 KMP_DEBUG_ASSERT(thread_data->td.td_deque_tail == 0);
3161 (
"__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n",
3162 __kmp_gtid_from_thread(thread), INITIAL_TASK_DEQUE_SIZE, thread_data));
3166 thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
3167 INITIAL_TASK_DEQUE_SIZE *
sizeof(kmp_taskdata_t *));
3168 thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
3174 static void __kmp_free_task_deque(kmp_thread_data_t *thread_data) {
3175 if (thread_data->td.td_deque != NULL) {
3176 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3177 TCW_4(thread_data->td.td_deque_ntasks, 0);
3178 __kmp_free(thread_data->td.td_deque);
3179 thread_data->td.td_deque = NULL;
3180 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3183 #ifdef BUILD_TIED_TASK_STACK 3185 if (thread_data->td.td_susp_tied_tasks.ts_entries != TASK_STACK_EMPTY) {
3186 __kmp_free_task_stack(__kmp_thread_from_gtid(gtid), thread_data);
3188 #endif // BUILD_TIED_TASK_STACK 3198 static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
3199 kmp_task_team_t *task_team) {
3200 kmp_thread_data_t **threads_data_p;
3201 kmp_int32 nthreads, maxthreads;
3202 int is_init_thread = FALSE;
3204 if (TCR_4(task_team->tt.tt_found_tasks)) {
3209 threads_data_p = &task_team->tt.tt_threads_data;
3210 nthreads = task_team->tt.tt_nproc;
3211 maxthreads = task_team->tt.tt_max_threads;
3216 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3218 if (!TCR_4(task_team->tt.tt_found_tasks)) {
3220 kmp_team_t *team = thread->th.th_team;
3223 is_init_thread = TRUE;
3224 if (maxthreads < nthreads) {
3226 if (*threads_data_p != NULL) {
3227 kmp_thread_data_t *old_data = *threads_data_p;
3228 kmp_thread_data_t *new_data = NULL;
3232 (
"__kmp_realloc_task_threads_data: T#%d reallocating " 3233 "threads data for task_team %p, new_size = %d, old_size = %d\n",
3234 __kmp_gtid_from_thread(thread), task_team, nthreads, maxthreads));
3239 new_data = (kmp_thread_data_t *)__kmp_allocate(
3240 nthreads *
sizeof(kmp_thread_data_t));
3242 KMP_MEMCPY_S((
void *)new_data, nthreads *
sizeof(kmp_thread_data_t),
3243 (
void *)old_data, maxthreads *
sizeof(kmp_thread_data_t));
3245 #ifdef BUILD_TIED_TASK_STACK 3247 for (i = maxthreads; i < nthreads; i++) {
3248 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3249 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3251 #endif // BUILD_TIED_TASK_STACK 3253 (*threads_data_p) = new_data;
3254 __kmp_free(old_data);
3256 KE_TRACE(10, (
"__kmp_realloc_task_threads_data: T#%d allocating " 3257 "threads data for task_team %p, size = %d\n",
3258 __kmp_gtid_from_thread(thread), task_team, nthreads));
3262 ANNOTATE_IGNORE_WRITES_BEGIN();
3263 *threads_data_p = (kmp_thread_data_t *)__kmp_allocate(
3264 nthreads *
sizeof(kmp_thread_data_t));
3265 ANNOTATE_IGNORE_WRITES_END();
3266 #ifdef BUILD_TIED_TASK_STACK 3268 for (i = 0; i < nthreads; i++) {
3269 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3270 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3272 #endif // BUILD_TIED_TASK_STACK 3274 task_team->tt.tt_max_threads = nthreads;
3277 KMP_DEBUG_ASSERT(*threads_data_p != NULL);
3281 for (i = 0; i < nthreads; i++) {
3282 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3283 thread_data->td.td_thr = team->t.t_threads[i];
3285 if (thread_data->td.td_deque_last_stolen >= nthreads) {
3289 thread_data->td.td_deque_last_stolen = -1;
3294 TCW_SYNC_4(task_team->tt.tt_found_tasks, TRUE);
3297 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3298 return is_init_thread;
3304 static void __kmp_free_task_threads_data(kmp_task_team_t *task_team) {
3305 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
3306 if (task_team->tt.tt_threads_data != NULL) {
3308 for (i = 0; i < task_team->tt.tt_max_threads; i++) {
3309 __kmp_free_task_deque(&task_team->tt.tt_threads_data[i]);
3311 __kmp_free(task_team->tt.tt_threads_data);
3312 task_team->tt.tt_threads_data = NULL;
3314 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
3321 static kmp_task_team_t *__kmp_allocate_task_team(kmp_info_t *thread,
3323 kmp_task_team_t *task_team = NULL;
3326 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d entering; team = %p\n",
3327 (thread ? __kmp_gtid_from_thread(thread) : -1), team));
3329 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3331 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3332 if (__kmp_free_task_teams != NULL) {
3333 task_team = __kmp_free_task_teams;
3334 TCW_PTR(__kmp_free_task_teams, task_team->tt.tt_next);
3335 task_team->tt.tt_next = NULL;
3337 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3340 if (task_team == NULL) {
3341 KE_TRACE(10, (
"__kmp_allocate_task_team: T#%d allocating " 3342 "task team for team %p\n",
3343 __kmp_gtid_from_thread(thread), team));
3347 task_team = (kmp_task_team_t *)__kmp_allocate(
sizeof(kmp_task_team_t));
3348 __kmp_init_bootstrap_lock(&task_team->tt.tt_threads_lock);
3355 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3356 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3357 task_team->tt.tt_nproc = nthreads = team->t.t_nproc;
3359 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads, nthreads);
3360 TCW_4(task_team->tt.tt_active, TRUE);
3362 KA_TRACE(20, (
"__kmp_allocate_task_team: T#%d exiting; task_team = %p " 3363 "unfinished_threads init'd to %d\n",
3364 (thread ? __kmp_gtid_from_thread(thread) : -1), task_team,
3365 KMP_ATOMIC_LD_RLX(&task_team->tt.tt_unfinished_threads)));
3372 void __kmp_free_task_team(kmp_info_t *thread, kmp_task_team_t *task_team) {
3373 KA_TRACE(20, (
"__kmp_free_task_team: T#%d task_team = %p\n",
3374 thread ? __kmp_gtid_from_thread(thread) : -1, task_team));
3377 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3379 KMP_DEBUG_ASSERT(task_team->tt.tt_next == NULL);
3380 task_team->tt.tt_next = __kmp_free_task_teams;
3381 TCW_PTR(__kmp_free_task_teams, task_team);
3383 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3391 void __kmp_reap_task_teams(
void) {
3392 kmp_task_team_t *task_team;
3394 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3396 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
3397 while ((task_team = __kmp_free_task_teams) != NULL) {
3398 __kmp_free_task_teams = task_team->tt.tt_next;
3399 task_team->tt.tt_next = NULL;
3402 if (task_team->tt.tt_threads_data != NULL) {
3403 __kmp_free_task_threads_data(task_team);
3405 __kmp_free(task_team);
3407 __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
3414 void __kmp_wait_to_unref_task_teams(
void) {
3419 KMP_INIT_YIELD(spins);
3427 for (thread = CCAST(kmp_info_t *, __kmp_thread_pool); thread != NULL;
3428 thread = thread->th.th_next_pool) {
3432 if (TCR_PTR(thread->th.th_task_team) == NULL) {
3433 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n",
3434 __kmp_gtid_from_thread(thread)));
3439 if (!__kmp_is_thread_alive(thread, &exit_val)) {
3440 thread->th.th_task_team = NULL;
3447 KA_TRACE(10, (
"__kmp_wait_to_unref_task_team: Waiting for T#%d to " 3448 "unreference task_team\n",
3449 __kmp_gtid_from_thread(thread)));
3451 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
3452 volatile void *sleep_loc;
3454 if ((sleep_loc = TCR_PTR(CCAST(
void *, thread->th.th_sleep_loc))) !=
3458 (
"__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n",
3459 __kmp_gtid_from_thread(thread), __kmp_gtid_from_thread(thread)));
3460 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc);
3469 KMP_YIELD_OVERSUB_ELSE_SPIN(spins);
3475 void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team,
int always) {
3476 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3482 if (team->t.t_task_team[this_thr->th.th_task_state] == NULL &&
3483 (always || team->t.t_nproc > 1)) {
3484 team->t.t_task_team[this_thr->th.th_task_state] =
3485 __kmp_allocate_task_team(this_thr, team);
3486 KA_TRACE(20, (
"__kmp_task_team_setup: Master T#%d created new task_team %p " 3487 "for team %d at parity=%d\n",
3488 __kmp_gtid_from_thread(this_thr),
3489 team->t.t_task_team[this_thr->th.th_task_state],
3490 ((team != NULL) ? team->t.t_id : -1),
3491 this_thr->th.th_task_state));
3501 if (team->t.t_nproc > 1) {
3502 int other_team = 1 - this_thr->th.th_task_state;
3503 if (team->t.t_task_team[other_team] == NULL) {
3504 team->t.t_task_team[other_team] =
3505 __kmp_allocate_task_team(this_thr, team);
3506 KA_TRACE(20, (
"__kmp_task_team_setup: Master T#%d created second new " 3507 "task_team %p for team %d at parity=%d\n",
3508 __kmp_gtid_from_thread(this_thr),
3509 team->t.t_task_team[other_team],
3510 ((team != NULL) ? team->t.t_id : -1), other_team));
3513 kmp_task_team_t *task_team = team->t.t_task_team[other_team];
3514 if (!task_team->tt.tt_active ||
3515 team->t.t_nproc != task_team->tt.tt_nproc) {
3516 TCW_4(task_team->tt.tt_nproc, team->t.t_nproc);
3517 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3518 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3519 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads,
3521 TCW_4(task_team->tt.tt_active, TRUE);
3525 KA_TRACE(20, (
"__kmp_task_team_setup: Master T#%d reset next task_team " 3526 "%p for team %d at parity=%d\n",
3527 __kmp_gtid_from_thread(this_thr),
3528 team->t.t_task_team[other_team],
3529 ((team != NULL) ? team->t.t_id : -1), other_team));
3537 void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team) {
3538 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3542 this_thr->th.th_task_state = 1 - this_thr->th.th_task_state;
3545 TCW_PTR(this_thr->th.th_task_team,
3546 team->t.t_task_team[this_thr->th.th_task_state]);
3548 (
"__kmp_task_team_sync: Thread T#%d task team switched to task_team " 3549 "%p from Team #%d (parity=%d)\n",
3550 __kmp_gtid_from_thread(this_thr), this_thr->th.th_task_team,
3551 ((team != NULL) ? team->t.t_id : -1), this_thr->th.th_task_state));
3561 void __kmp_task_team_wait(
3562 kmp_info_t *this_thr,
3563 kmp_team_t *team USE_ITT_BUILD_ARG(
void *itt_sync_obj),
int wait) {
3564 kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state];
3566 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3567 KMP_DEBUG_ASSERT(task_team == this_thr->th.th_task_team);
3569 if ((task_team != NULL) && KMP_TASKING_ENABLED(task_team)) {
3571 KA_TRACE(20, (
"__kmp_task_team_wait: Master T#%d waiting for all tasks " 3572 "(for unfinished_threads to reach 0) on task_team = %p\n",
3573 __kmp_gtid_from_thread(this_thr), task_team));
3577 kmp_flag_32 flag(RCAST(std::atomic<kmp_uint32> *,
3578 &task_team->tt.tt_unfinished_threads),
3580 flag.wait(this_thr, TRUE USE_ITT_BUILD_ARG(itt_sync_obj));
3586 (
"__kmp_task_team_wait: Master T#%d deactivating task_team %p: " 3587 "setting active to false, setting local and team's pointer to NULL\n",
3588 __kmp_gtid_from_thread(this_thr), task_team));
3589 KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1 ||
3590 task_team->tt.tt_found_proxy_tasks == TRUE);
3591 TCW_SYNC_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3592 KMP_CHECK_UPDATE(task_team->tt.tt_untied_task_encountered, 0);
3593 TCW_SYNC_4(task_team->tt.tt_active, FALSE);
3596 TCW_PTR(this_thr->th.th_task_team, NULL);
3605 void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread,
int gtid) {
3606 std::atomic<kmp_uint32> *spin = RCAST(
3607 std::atomic<kmp_uint32> *,
3608 &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads);
3610 KMP_DEBUG_ASSERT(__kmp_tasking_mode == tskm_extra_barrier);
3613 KMP_FSYNC_SPIN_INIT(spin, NULL);
3615 kmp_flag_32 spin_flag(spin, 0U);
3616 while (!spin_flag.execute_tasks(thread, gtid, TRUE,
3617 &flag USE_ITT_BUILD_ARG(NULL), 0)) {
3620 KMP_FSYNC_SPIN_PREPARE(RCAST(
void *, spin));
3623 if (TCR_4(__kmp_global.g.g_done)) {
3624 if (__kmp_global.g.g_abort)
3625 __kmp_abort_thread();
3631 KMP_FSYNC_SPIN_ACQUIRED(RCAST(
void *, spin));
3640 static bool __kmp_give_task(kmp_info_t *thread, kmp_int32 tid, kmp_task_t *task,
3642 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
3643 kmp_task_team_t *task_team = taskdata->td_task_team;
3645 KA_TRACE(20, (
"__kmp_give_task: trying to give task %p to thread %d.\n",
3649 KMP_DEBUG_ASSERT(task_team != NULL);
3651 bool result =
false;
3652 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
3654 if (thread_data->td.td_deque == NULL) {
3658 (
"__kmp_give_task: thread %d has no queue while giving task %p.\n",
3663 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3664 TASK_DEQUE_SIZE(thread_data->td)) {
3667 (
"__kmp_give_task: queue is full while giving task %p to thread %d.\n",
3672 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
3675 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3676 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3677 TASK_DEQUE_SIZE(thread_data->td)) {
3679 __kmp_realloc_task_deque(thread, thread_data);
3684 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3686 if (TCR_4(thread_data->td.td_deque_ntasks) >=
3687 TASK_DEQUE_SIZE(thread_data->td)) {
3688 KA_TRACE(30, (
"__kmp_give_task: queue is full while giving task %p to " 3694 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
3695 goto release_and_exit;
3697 __kmp_realloc_task_deque(thread, thread_data);
3703 thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
3705 thread_data->td.td_deque_tail =
3706 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
3707 TCW_4(thread_data->td.td_deque_ntasks,
3708 TCR_4(thread_data->td.td_deque_ntasks) + 1);
3711 KA_TRACE(30, (
"__kmp_give_task: successfully gave task %p to thread %d.\n",
3715 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3736 static void __kmp_first_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
3737 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
3738 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3739 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
3740 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
3742 taskdata->td_flags.complete = 1;
3744 if (taskdata->td_taskgroup)
3745 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
3749 KMP_ATOMIC_INC(&taskdata->td_incomplete_child_tasks);
3752 static void __kmp_second_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
3753 kmp_int32 children = 0;
3757 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks) - 1;
3758 KMP_DEBUG_ASSERT(children >= 0);
3761 KMP_ATOMIC_DEC(&taskdata->td_incomplete_child_tasks);
3764 static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask) {
3765 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3766 kmp_info_t *thread = __kmp_threads[gtid];
3768 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3769 KMP_DEBUG_ASSERT(taskdata->td_flags.complete ==
3774 while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) > 0)
3777 __kmp_release_deps(gtid, taskdata);
3778 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
3790 KMP_DEBUG_ASSERT(ptask != NULL);
3791 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3793 10, (
"__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n",
3796 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3798 __kmp_first_top_half_finish_proxy(taskdata);
3799 __kmp_second_top_half_finish_proxy(taskdata);
3800 __kmp_bottom_half_finish_proxy(gtid, ptask);
3803 (
"__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n",
3815 KMP_DEBUG_ASSERT(ptask != NULL);
3816 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3820 (
"__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n",
3823 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
3825 __kmp_first_top_half_finish_proxy(taskdata);
3829 kmp_team_t *team = taskdata->td_team;
3830 kmp_int32 nthreads = team->t.t_nproc;
3835 kmp_int32 start_k = 0;
3837 kmp_int32 k = start_k;
3841 thread = team->t.t_threads[k];
3842 k = (k + 1) % nthreads;
3848 }
while (!__kmp_give_task(thread, k, ptask, pass));
3850 __kmp_second_top_half_finish_proxy(taskdata);
3854 (
"__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n",
3858 kmp_event_t *__kmpc_task_allow_completion_event(
ident_t *loc_ref,
int gtid,
3860 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(task);
3861 if (td->td_allow_completion_event.type == KMP_EVENT_UNINITIALIZED) {
3862 td->td_allow_completion_event.type = KMP_EVENT_ALLOW_COMPLETION;
3863 td->td_allow_completion_event.ed.task = task;
3864 __kmp_init_tas_lock(&td->td_allow_completion_event.lock);
3866 return &td->td_allow_completion_event;
3869 void __kmp_fulfill_event(kmp_event_t *event) {
3870 if (event->type == KMP_EVENT_ALLOW_COMPLETION) {
3871 kmp_task_t *ptask =
event->ed.task;
3872 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3873 bool detached =
false;
3874 int gtid = __kmp_get_gtid();
3879 __kmp_acquire_tas_lock(&event->lock, gtid);
3880 if (taskdata->td_flags.proxy == TASK_PROXY) {
3886 if (UNLIKELY(ompt_enabled.enabled))
3887 __ompt_task_finish(ptask, NULL, ompt_task_early_fulfill);
3890 event->type = KMP_EVENT_UNINITIALIZED;
3891 __kmp_release_tas_lock(&event->lock, gtid);
3897 if (UNLIKELY(ompt_enabled.enabled))
3898 __ompt_task_finish(ptask, NULL, ompt_task_late_fulfill);
3902 kmp_team_t *team = taskdata->td_team;
3903 kmp_info_t *thread = __kmp_get_thread();
3904 if (thread->th.th_team == team) {
3922 kmp_task_t *__kmp_task_dup_alloc(kmp_info_t *thread, kmp_task_t *task_src) {
3924 kmp_taskdata_t *taskdata;
3925 kmp_taskdata_t *taskdata_src = KMP_TASK_TO_TASKDATA(task_src);
3926 kmp_taskdata_t *parent_task = taskdata_src->td_parent;
3927 size_t shareds_offset;
3930 KA_TRACE(10, (
"__kmp_task_dup_alloc(enter): Th %p, source task %p\n", thread,
3932 KMP_DEBUG_ASSERT(taskdata_src->td_flags.proxy ==
3934 KMP_DEBUG_ASSERT(taskdata_src->td_flags.tasktype == TASK_EXPLICIT);
3935 task_size = taskdata_src->td_size_alloc;
3938 KA_TRACE(30, (
"__kmp_task_dup_alloc: Th %p, malloc size %ld\n", thread,
3941 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, task_size);
3943 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, task_size);
3945 KMP_MEMCPY(taskdata, taskdata_src, task_size);
3947 task = KMP_TASKDATA_TO_TASK(taskdata);
3950 taskdata->td_task_id = KMP_GEN_TASK_ID();
3951 if (task->shareds != NULL) {
3952 shareds_offset = (
char *)task_src->shareds - (
char *)taskdata_src;
3953 task->shareds = &((
char *)taskdata)[shareds_offset];
3954 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (
sizeof(
void *) - 1)) ==
3957 taskdata->td_alloc_thread = thread;
3958 taskdata->td_parent = parent_task;
3960 taskdata->td_taskgroup = parent_task->td_taskgroup;
3963 if (taskdata->td_flags.tiedness == TASK_TIED)
3964 taskdata->td_last_tied = taskdata;
3968 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
3969 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
3970 if (parent_task->td_taskgroup)
3971 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
3974 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT)
3975 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
3979 (
"__kmp_task_dup_alloc(exit): Th %p, created task %p, parent=%p\n",
3980 thread, taskdata, taskdata->td_parent));
3982 if (UNLIKELY(ompt_enabled.enabled))
3983 __ompt_task_init(taskdata, thread->th.th_info.ds.ds_gtid);
3992 typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32);
3994 KMP_BUILD_ASSERT(
sizeof(
long) == 4 ||
sizeof(
long) == 8);
3999 class kmp_taskloop_bounds_t {
4001 const kmp_taskdata_t *taskdata;
4002 size_t lower_offset;
4003 size_t upper_offset;
4006 kmp_taskloop_bounds_t(kmp_task_t *_task, kmp_uint64 *lb, kmp_uint64 *ub)
4007 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(task)),
4008 lower_offset((
char *)lb - (
char *)task),
4009 upper_offset((
char *)ub - (
char *)task) {
4010 KMP_DEBUG_ASSERT((
char *)lb > (
char *)_task);
4011 KMP_DEBUG_ASSERT((
char *)ub > (
char *)_task);
4013 kmp_taskloop_bounds_t(kmp_task_t *_task,
const kmp_taskloop_bounds_t &bounds)
4014 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(_task)),
4015 lower_offset(bounds.lower_offset), upper_offset(bounds.upper_offset) {}
4016 size_t get_lower_offset()
const {
return lower_offset; }
4017 size_t get_upper_offset()
const {
return upper_offset; }
4018 kmp_uint64 get_lb()
const {
4020 #if defined(KMP_GOMP_COMPAT) 4022 if (!taskdata->td_flags.native) {
4023 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4026 if (taskdata->td_size_loop_bounds == 4) {
4027 kmp_int32 *lb = RCAST(kmp_int32 *, task->shareds);
4028 retval = (kmp_int64)*lb;
4030 kmp_int64 *lb = RCAST(kmp_int64 *, task->shareds);
4031 retval = (kmp_int64)*lb;
4035 retval = *(kmp_int64 *)((
char *)task + lower_offset);
4036 #endif // defined(KMP_GOMP_COMPAT) 4039 kmp_uint64 get_ub()
const {
4041 #if defined(KMP_GOMP_COMPAT) 4043 if (!taskdata->td_flags.native) {
4044 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4047 if (taskdata->td_size_loop_bounds == 4) {
4048 kmp_int32 *ub = RCAST(kmp_int32 *, task->shareds) + 1;
4049 retval = (kmp_int64)*ub;
4051 kmp_int64 *ub = RCAST(kmp_int64 *, task->shareds) + 1;
4052 retval = (kmp_int64)*ub;
4056 retval = *(kmp_int64 *)((
char *)task + upper_offset);
4057 #endif // defined(KMP_GOMP_COMPAT) 4060 void set_lb(kmp_uint64 lb) {
4061 #if defined(KMP_GOMP_COMPAT) 4063 if (!taskdata->td_flags.native) {
4064 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4067 if (taskdata->td_size_loop_bounds == 4) {
4068 kmp_uint32 *lower = RCAST(kmp_uint32 *, task->shareds);
4069 *lower = (kmp_uint32)lb;
4071 kmp_uint64 *lower = RCAST(kmp_uint64 *, task->shareds);
4072 *lower = (kmp_uint64)lb;
4076 *(kmp_uint64 *)((
char *)task + lower_offset) = lb;
4077 #endif // defined(KMP_GOMP_COMPAT) 4079 void set_ub(kmp_uint64 ub) {
4080 #if defined(KMP_GOMP_COMPAT) 4082 if (!taskdata->td_flags.native) {
4083 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4086 if (taskdata->td_size_loop_bounds == 4) {
4087 kmp_uint32 *upper = RCAST(kmp_uint32 *, task->shareds) + 1;
4088 *upper = (kmp_uint32)ub;
4090 kmp_uint64 *upper = RCAST(kmp_uint64 *, task->shareds) + 1;
4091 *upper = (kmp_uint64)ub;
4095 *(kmp_uint64 *)((
char *)task + upper_offset) = ub;
4096 #endif // defined(KMP_GOMP_COMPAT) 4115 void __kmp_taskloop_linear(
ident_t *loc,
int gtid, kmp_task_t *task,
4116 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4117 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
4118 kmp_uint64 grainsize, kmp_uint64 extras,
4125 KMP_TIME_PARTITIONED_BLOCK(OMP_taskloop_scheduling);
4126 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
4128 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4129 kmp_uint64 lower = task_bounds.get_lb();
4130 kmp_uint64 upper = task_bounds.get_ub();
4132 kmp_info_t *thread = __kmp_threads[gtid];
4133 kmp_taskdata_t *current_task = thread->th.th_current_task;
4134 kmp_task_t *next_task;
4135 kmp_int32 lastpriv = 0;
4137 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras);
4138 KMP_DEBUG_ASSERT(num_tasks > extras);
4139 KMP_DEBUG_ASSERT(num_tasks > 0);
4140 KA_TRACE(20, (
"__kmp_taskloop_linear: T#%d: %lld tasks, grainsize %lld, " 4141 "extras %lld, i=%lld,%lld(%d)%lld, dup %p\n",
4142 gtid, num_tasks, grainsize, extras, lower, upper, ub_glob, st,
4146 for (i = 0; i < num_tasks; ++i) {
4147 kmp_uint64 chunk_minus_1;
4149 chunk_minus_1 = grainsize - 1;
4151 chunk_minus_1 = grainsize;
4154 upper = lower + st * chunk_minus_1;
4155 if (i == num_tasks - 1) {
4158 KMP_DEBUG_ASSERT(upper == *ub);
4159 if (upper == ub_glob)
4161 }
else if (st > 0) {
4162 KMP_DEBUG_ASSERT((kmp_uint64)st > *ub - upper);
4163 if ((kmp_uint64)st > ub_glob - upper)
4166 KMP_DEBUG_ASSERT(upper + st < *ub);
4167 if (upper - ub_glob < (kmp_uint64)(-st))
4171 next_task = __kmp_task_dup_alloc(thread, task);
4172 kmp_taskdata_t *next_taskdata = KMP_TASK_TO_TASKDATA(next_task);
4173 kmp_taskloop_bounds_t next_task_bounds =
4174 kmp_taskloop_bounds_t(next_task, task_bounds);
4177 next_task_bounds.set_lb(lower);
4178 if (next_taskdata->td_flags.native) {
4179 next_task_bounds.set_ub(upper + (st > 0 ? 1 : -1));
4181 next_task_bounds.set_ub(upper);
4183 if (ptask_dup != NULL)
4185 ptask_dup(next_task, task, lastpriv);
4187 (
"__kmp_taskloop_linear: T#%d; task #%llu: task %p: lower %lld, " 4188 "upper %lld stride %lld, (offsets %p %p)\n",
4189 gtid, i, next_task, lower, upper, st,
4190 next_task_bounds.get_lower_offset(),
4191 next_task_bounds.get_upper_offset()));
4193 __kmp_omp_taskloop_task(NULL, gtid, next_task,
4196 __kmp_omp_task(gtid, next_task,
true);
4201 __kmp_task_start(gtid, task, current_task);
4203 __kmp_task_finish<false>(gtid, task, current_task);
4208 typedef struct __taskloop_params {
4215 kmp_uint64 num_tasks;
4216 kmp_uint64 grainsize;
4219 kmp_uint64 num_t_min;
4223 } __taskloop_params_t;
4225 void __kmp_taskloop_recur(
ident_t *,
int, kmp_task_t *, kmp_uint64 *,
4226 kmp_uint64 *, kmp_int64, kmp_uint64, kmp_uint64,
4227 kmp_uint64, kmp_uint64, kmp_uint64, kmp_uint64,
4234 int __kmp_taskloop_task(
int gtid,
void *ptask) {
4235 __taskloop_params_t *p =
4236 (__taskloop_params_t *)((kmp_task_t *)ptask)->shareds;
4237 kmp_task_t *task = p->task;
4238 kmp_uint64 *lb = p->lb;
4239 kmp_uint64 *ub = p->ub;
4240 void *task_dup = p->task_dup;
4242 kmp_int64 st = p->st;
4243 kmp_uint64 ub_glob = p->ub_glob;
4244 kmp_uint64 num_tasks = p->num_tasks;
4245 kmp_uint64 grainsize = p->grainsize;
4246 kmp_uint64 extras = p->extras;
4247 kmp_uint64 tc = p->tc;
4248 kmp_uint64 num_t_min = p->num_t_min;
4250 void *codeptr_ra = p->codeptr_ra;
4253 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4254 KMP_DEBUG_ASSERT(task != NULL);
4255 KA_TRACE(20, (
"__kmp_taskloop_task: T#%d, task %p: %lld tasks, grainsize" 4256 " %lld, extras %lld, i=%lld,%lld(%d), dup %p\n",
4257 gtid, taskdata, num_tasks, grainsize, extras, *lb, *ub, st,
4260 KMP_DEBUG_ASSERT(num_tasks * 2 + 1 > num_t_min);
4261 if (num_tasks > num_t_min)
4262 __kmp_taskloop_recur(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
4263 grainsize, extras, tc, num_t_min,
4269 __kmp_taskloop_linear(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
4270 grainsize, extras, tc,
4276 KA_TRACE(40, (
"__kmp_taskloop_task(exit): T#%d\n", gtid));
4297 void __kmp_taskloop_recur(
ident_t *loc,
int gtid, kmp_task_t *task,
4298 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4299 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
4300 kmp_uint64 grainsize, kmp_uint64 extras,
4301 kmp_uint64 tc, kmp_uint64 num_t_min,
4306 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4307 KMP_DEBUG_ASSERT(task != NULL);
4308 KMP_DEBUG_ASSERT(num_tasks > num_t_min);
4309 KA_TRACE(20, (
"__kmp_taskloop_recur: T#%d, task %p: %lld tasks, grainsize" 4310 " %lld, extras %lld, i=%lld,%lld(%d), dup %p\n",
4311 gtid, taskdata, num_tasks, grainsize, extras, *lb, *ub, st,
4313 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
4314 kmp_uint64 lower = *lb;
4315 kmp_info_t *thread = __kmp_threads[gtid];
4317 kmp_task_t *next_task;
4318 size_t lower_offset =
4319 (
char *)lb - (
char *)task;
4320 size_t upper_offset =
4321 (
char *)ub - (
char *)task;
4323 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras);
4324 KMP_DEBUG_ASSERT(num_tasks > extras);
4325 KMP_DEBUG_ASSERT(num_tasks > 0);
4328 kmp_uint64 lb1, ub0, tc0, tc1, ext0, ext1;
4329 kmp_uint64 gr_size0 = grainsize;
4330 kmp_uint64 n_tsk0 = num_tasks >> 1;
4331 kmp_uint64 n_tsk1 = num_tasks - n_tsk0;
4332 if (n_tsk0 <= extras) {
4335 ext1 = extras - n_tsk0;
4336 tc0 = gr_size0 * n_tsk0;
4341 tc1 = grainsize * n_tsk1;
4344 ub0 = lower + st * (tc0 - 1);
4348 next_task = __kmp_task_dup_alloc(thread, task);
4350 *(kmp_uint64 *)((
char *)next_task + lower_offset) = lb1;
4351 if (ptask_dup != NULL)
4352 ptask_dup(next_task, task, 0);
4357 kmp_taskdata_t *current_task = thread->th.th_current_task;
4358 thread->th.th_current_task = taskdata->td_parent;
4359 kmp_task_t *new_task =
4360 __kmpc_omp_task_alloc(loc, gtid, 1, 3 *
sizeof(
void *),
4361 sizeof(__taskloop_params_t), &__kmp_taskloop_task);
4363 thread->th.th_current_task = current_task;
4364 __taskloop_params_t *p = (__taskloop_params_t *)new_task->shareds;
4365 p->task = next_task;
4366 p->lb = (kmp_uint64 *)((
char *)next_task + lower_offset);
4367 p->ub = (kmp_uint64 *)((
char *)next_task + upper_offset);
4368 p->task_dup = task_dup;
4370 p->ub_glob = ub_glob;
4371 p->num_tasks = n_tsk1;
4372 p->grainsize = grainsize;
4375 p->num_t_min = num_t_min;
4377 p->codeptr_ra = codeptr_ra;
4382 __kmp_omp_taskloop_task(NULL, gtid, new_task, codeptr_ra);
4384 __kmp_omp_task(gtid, new_task,
true);
4388 if (n_tsk0 > num_t_min)
4389 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0, gr_size0,
4390 ext0, tc0, num_t_min,
4396 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0,
4397 gr_size0, ext0, tc0,
4403 KA_TRACE(40, (
"__kmpc_taskloop_recur(exit): T#%d\n", gtid));
4423 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
int nogroup,
4424 int sched, kmp_uint64 grainsize,
void *task_dup) {
4425 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4426 KMP_DEBUG_ASSERT(task != NULL);
4429 #if OMPT_SUPPORT && OMPT_OPTIONAL 4430 OMPT_STORE_RETURN_ADDRESS(gtid);
4432 __kmpc_taskgroup(loc, gtid);
4437 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4440 kmp_uint64 lower = task_bounds.get_lb();
4441 kmp_uint64 upper = task_bounds.get_ub();
4442 kmp_uint64 ub_glob = upper;
4443 kmp_uint64 num_tasks = 0, extras = 0;
4444 kmp_uint64 num_tasks_min = __kmp_taskloop_min_tasks;
4445 kmp_info_t *thread = __kmp_threads[gtid];
4446 kmp_taskdata_t *current_task = thread->th.th_current_task;
4448 KA_TRACE(20, (
"__kmpc_taskloop: T#%d, task %p, lb %lld, ub %lld, st %lld, " 4449 "grain %llu(%d), dup %p\n",
4450 gtid, taskdata, lower, upper, st, grainsize, sched, task_dup));
4454 tc = upper - lower + 1;
4455 }
else if (st < 0) {
4456 tc = (lower - upper) / (-st) + 1;
4458 tc = (upper - lower) / st + 1;
4461 KA_TRACE(20, (
"__kmpc_taskloop(exit): T#%d zero-trip loop\n", gtid));
4463 __kmp_task_start(gtid, task, current_task);
4465 __kmp_task_finish<false>(gtid, task, current_task);
4469 #if OMPT_SUPPORT && OMPT_OPTIONAL 4470 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
4471 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
4472 if (ompt_enabled.ompt_callback_work) {
4473 ompt_callbacks.ompt_callback(ompt_callback_work)(
4474 ompt_work_taskloop, ompt_scope_begin, &(team_info->parallel_data),
4475 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
4479 if (num_tasks_min == 0)
4482 KMP_MIN(thread->th.th_team_nproc * 10, INITIAL_TASK_DEQUE_SIZE);
4488 grainsize = thread->th.th_team_nproc * 10;
4491 if (grainsize > tc) {
4496 num_tasks = grainsize;
4497 grainsize = tc / num_tasks;
4498 extras = tc % num_tasks;
4502 if (grainsize > tc) {
4507 num_tasks = tc / grainsize;
4509 grainsize = tc / num_tasks;
4510 extras = tc % num_tasks;
4514 KMP_ASSERT2(0,
"unknown scheduling of taskloop");
4516 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras);
4517 KMP_DEBUG_ASSERT(num_tasks > extras);
4518 KMP_DEBUG_ASSERT(num_tasks > 0);
4524 taskdata->td_flags.task_serial = 1;
4525 taskdata->td_flags.tiedness = TASK_TIED;
4527 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4528 grainsize, extras, tc,
4530 OMPT_GET_RETURN_ADDRESS(0),
4535 }
else if (num_tasks > num_tasks_min && !taskdata->td_flags.native) {
4536 KA_TRACE(20, (
"__kmpc_taskloop: T#%d, go recursive: tc %llu, #tasks %llu" 4537 "(%lld), grain %llu, extras %llu\n",
4538 gtid, tc, num_tasks, num_tasks_min, grainsize, extras));
4539 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4540 grainsize, extras, tc, num_tasks_min,
4542 OMPT_GET_RETURN_ADDRESS(0),
4546 KA_TRACE(20, (
"__kmpc_taskloop: T#%d, go linear: tc %llu, #tasks %llu" 4547 "(%lld), grain %llu, extras %llu\n",
4548 gtid, tc, num_tasks, num_tasks_min, grainsize, extras));
4549 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4550 grainsize, extras, tc,
4552 OMPT_GET_RETURN_ADDRESS(0),
4557 #if OMPT_SUPPORT && OMPT_OPTIONAL 4558 if (ompt_enabled.ompt_callback_work) {
4559 ompt_callbacks.ompt_callback(ompt_callback_work)(
4560 ompt_work_taskloop, ompt_scope_end, &(team_info->parallel_data),
4561 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
4566 #if OMPT_SUPPORT && OMPT_OPTIONAL 4567 OMPT_STORE_RETURN_ADDRESS(gtid);
4569 __kmpc_end_taskgroup(loc, gtid);
4571 KA_TRACE(20, (
"__kmpc_taskloop(exit): T#%d\n", gtid));
void __kmpc_task_reduction_modifier_fini(ident_t *loc, int gtid, int is_ws)
void * __kmpc_task_reduction_modifier_init(ident_t *loc, int gtid, int is_ws, int num, void *data)
void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int sched, kmp_uint64 grainsize, void *task_dup)
struct kmp_taskred_input kmp_taskred_input_t
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_taskred_modifier_init(ident_t *loc, int gtid, int is_ws, int num, void *data)
struct kmp_taskred_data kmp_taskred_data_t
void __kmpc_proxy_task_completed_ooo(kmp_task_t *ptask)
#define KMP_COUNT_BLOCK(name)
Increments specified counter (name).
void * __kmpc_taskred_init(int gtid, int num, void *data)
void * __kmpc_task_reduction_get_th_data(int gtid, void *tskgrp, void *data)
void __kmpc_proxy_task_completed(kmp_int32 gtid, kmp_task_t *ptask)
struct kmp_taskred_flags kmp_taskred_flags_t
void * __kmpc_task_reduction_init(int gtid, int num, void *data)
kmp_taskred_flags_t flags
struct kmp_task_red_input kmp_task_red_input_t