LLVM OpenMP 19.0.0git
kmp_tasking.cpp
Go to the documentation of this file.
1/*
2 * kmp_tasking.cpp -- OpenMP 3.0 tasking support.
3 */
4
5//===----------------------------------------------------------------------===//
6//
7// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
8// See https://llvm.org/LICENSE.txt for license information.
9// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
10//
11//===----------------------------------------------------------------------===//
12
13#include "kmp.h"
14#include "kmp_i18n.h"
15#include "kmp_itt.h"
16#include "kmp_stats.h"
17#include "kmp_wait_release.h"
18#include "kmp_taskdeps.h"
19
20#if OMPT_SUPPORT
21#include "ompt-specific.h"
22#endif
23
24#if ENABLE_LIBOMPTARGET
25static void (*tgt_target_nowait_query)(void **);
26
27void __kmp_init_target_task() {
28 *(void **)(&tgt_target_nowait_query) = KMP_DLSYM("__tgt_target_nowait_query");
29}
30#endif
31
32/* forward declaration */
33static void __kmp_enable_tasking(kmp_task_team_t *task_team,
34 kmp_info_t *this_thr);
35static void __kmp_alloc_task_deque(kmp_info_t *thread,
36 kmp_thread_data_t *thread_data);
38 kmp_task_team_t *task_team);
40#if OMPX_TASKGRAPH
41static kmp_tdg_info_t *__kmp_find_tdg(kmp_int32 tdg_id);
42int __kmp_taskloop_task(int gtid, void *ptask);
43#endif
44
45#ifdef BUILD_TIED_TASK_STACK
46
47// __kmp_trace_task_stack: print the tied tasks from the task stack in order
48// from top do bottom
49//
50// gtid: global thread identifier for thread containing stack
51// thread_data: thread data for task team thread containing stack
52// threshold: value above which the trace statement triggers
53// location: string identifying call site of this function (for trace)
54static void __kmp_trace_task_stack(kmp_int32 gtid,
55 kmp_thread_data_t *thread_data,
56 int threshold, char *location) {
57 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
58 kmp_taskdata_t **stack_top = task_stack->ts_top;
59 kmp_int32 entries = task_stack->ts_entries;
60 kmp_taskdata_t *tied_task;
61
63 threshold,
64 ("__kmp_trace_task_stack(start): location = %s, gtid = %d, entries = %d, "
65 "first_block = %p, stack_top = %p \n",
66 location, gtid, entries, task_stack->ts_first_block, stack_top));
67
68 KMP_DEBUG_ASSERT(stack_top != NULL);
69 KMP_DEBUG_ASSERT(entries > 0);
70
71 while (entries != 0) {
72 KMP_DEBUG_ASSERT(stack_top != &task_stack->ts_first_block.sb_block[0]);
73 // fix up ts_top if we need to pop from previous block
74 if (entries & TASK_STACK_INDEX_MASK == 0) {
75 kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(stack_top);
76
77 stack_block = stack_block->sb_prev;
78 stack_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE];
79 }
80
81 // finish bookkeeping
82 stack_top--;
83 entries--;
84
85 tied_task = *stack_top;
86
87 KMP_DEBUG_ASSERT(tied_task != NULL);
89
90 KA_TRACE(threshold,
91 ("__kmp_trace_task_stack(%s): gtid=%d, entry=%d, "
92 "stack_top=%p, tied_task=%p\n",
93 location, gtid, entries, stack_top, tied_task));
94 }
95 KMP_DEBUG_ASSERT(stack_top == &task_stack->ts_first_block.sb_block[0]);
96
97 KA_TRACE(threshold,
98 ("__kmp_trace_task_stack(exit): location = %s, gtid = %d\n",
99 location, gtid));
100}
101
102// __kmp_init_task_stack: initialize the task stack for the first time
103// after a thread_data structure is created.
104// It should not be necessary to do this again (assuming the stack works).
105//
106// gtid: global thread identifier of calling thread
107// thread_data: thread data for task team thread containing stack
108static void __kmp_init_task_stack(kmp_int32 gtid,
109 kmp_thread_data_t *thread_data) {
110 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
111 kmp_stack_block_t *first_block;
112
113 // set up the first block of the stack
114 first_block = &task_stack->ts_first_block;
115 task_stack->ts_top = (kmp_taskdata_t **)first_block;
116 memset((void *)first_block, '\0',
117 TASK_STACK_BLOCK_SIZE * sizeof(kmp_taskdata_t *));
118
119 // initialize the stack to be empty
120 task_stack->ts_entries = TASK_STACK_EMPTY;
121 first_block->sb_next = NULL;
122 first_block->sb_prev = NULL;
123}
124
125// __kmp_free_task_stack: free the task stack when thread_data is destroyed.
126//
127// gtid: global thread identifier for calling thread
128// thread_data: thread info for thread containing stack
129static void __kmp_free_task_stack(kmp_int32 gtid,
130 kmp_thread_data_t *thread_data) {
131 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
132 kmp_stack_block_t *stack_block = &task_stack->ts_first_block;
133
134 KMP_DEBUG_ASSERT(task_stack->ts_entries == TASK_STACK_EMPTY);
135 // free from the second block of the stack
136 while (stack_block != NULL) {
137 kmp_stack_block_t *next_block = (stack_block) ? stack_block->sb_next : NULL;
138
139 stack_block->sb_next = NULL;
140 stack_block->sb_prev = NULL;
141 if (stack_block != &task_stack->ts_first_block) {
142 __kmp_thread_free(thread,
143 stack_block); // free the block, if not the first
144 }
145 stack_block = next_block;
146 }
147 // initialize the stack to be empty
148 task_stack->ts_entries = 0;
149 task_stack->ts_top = NULL;
150}
151
152// __kmp_push_task_stack: Push the tied task onto the task stack.
153// Grow the stack if necessary by allocating another block.
154//
155// gtid: global thread identifier for calling thread
156// thread: thread info for thread containing stack
157// tied_task: the task to push on the stack
158static void __kmp_push_task_stack(kmp_int32 gtid, kmp_info_t *thread,
159 kmp_taskdata_t *tied_task) {
160 // GEH - need to consider what to do if tt_threads_data not allocated yet
161 kmp_thread_data_t *thread_data =
162 &thread->th.th_task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
163 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
164
165 if (tied_task->td_flags.team_serial || tied_task->td_flags.tasking_ser) {
166 return; // Don't push anything on stack if team or team tasks are serialized
167 }
168
170 KMP_DEBUG_ASSERT(task_stack->ts_top != NULL);
171
172 KA_TRACE(20,
173 ("__kmp_push_task_stack(enter): GTID: %d; THREAD: %p; TASK: %p\n",
174 gtid, thread, tied_task));
175 // Store entry
176 *(task_stack->ts_top) = tied_task;
177
178 // Do bookkeeping for next push
179 task_stack->ts_top++;
180 task_stack->ts_entries++;
181
182 if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) {
183 // Find beginning of this task block
184 kmp_stack_block_t *stack_block =
185 (kmp_stack_block_t *)(task_stack->ts_top - TASK_STACK_BLOCK_SIZE);
186
187 // Check if we already have a block
188 if (stack_block->sb_next !=
189 NULL) { // reset ts_top to beginning of next block
190 task_stack->ts_top = &stack_block->sb_next->sb_block[0];
191 } else { // Alloc new block and link it up
192 kmp_stack_block_t *new_block = (kmp_stack_block_t *)__kmp_thread_calloc(
193 thread, sizeof(kmp_stack_block_t));
194
195 task_stack->ts_top = &new_block->sb_block[0];
196 stack_block->sb_next = new_block;
197 new_block->sb_prev = stack_block;
198 new_block->sb_next = NULL;
199
200 KA_TRACE(
201 30,
202 ("__kmp_push_task_stack(): GTID: %d; TASK: %p; Alloc new block: %p\n",
203 gtid, tied_task, new_block));
204 }
205 }
206 KA_TRACE(20, ("__kmp_push_task_stack(exit): GTID: %d; TASK: %p\n", gtid,
207 tied_task));
208}
209
210// __kmp_pop_task_stack: Pop the tied task from the task stack. Don't return
211// the task, just check to make sure it matches the ending task passed in.
212//
213// gtid: global thread identifier for the calling thread
214// thread: thread info structure containing stack
215// tied_task: the task popped off the stack
216// ending_task: the task that is ending (should match popped task)
217static void __kmp_pop_task_stack(kmp_int32 gtid, kmp_info_t *thread,
218 kmp_taskdata_t *ending_task) {
219 // GEH - need to consider what to do if tt_threads_data not allocated yet
220 kmp_thread_data_t *thread_data =
221 &thread->th.th_task_team->tt_threads_data[__kmp_tid_from_gtid(gtid)];
222 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
223 kmp_taskdata_t *tied_task;
224
225 if (ending_task->td_flags.team_serial || ending_task->td_flags.tasking_ser) {
226 // Don't pop anything from stack if team or team tasks are serialized
227 return;
228 }
229
230 KMP_DEBUG_ASSERT(task_stack->ts_top != NULL);
231 KMP_DEBUG_ASSERT(task_stack->ts_entries > 0);
232
233 KA_TRACE(20, ("__kmp_pop_task_stack(enter): GTID: %d; THREAD: %p\n", gtid,
234 thread));
235
236 // fix up ts_top if we need to pop from previous block
237 if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) {
238 kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(task_stack->ts_top);
239
240 stack_block = stack_block->sb_prev;
241 task_stack->ts_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE];
242 }
243
244 // finish bookkeeping
245 task_stack->ts_top--;
246 task_stack->ts_entries--;
247
248 tied_task = *(task_stack->ts_top);
249
250 KMP_DEBUG_ASSERT(tied_task != NULL);
252 KMP_DEBUG_ASSERT(tied_task == ending_task); // If we built the stack correctly
253
254 KA_TRACE(20, ("__kmp_pop_task_stack(exit): GTID: %d; TASK: %p\n", gtid,
255 tied_task));
256 return;
257}
258#endif /* BUILD_TIED_TASK_STACK */
259
260// returns 1 if new task is allowed to execute, 0 otherwise
261// checks Task Scheduling constraint (if requested) and
262// mutexinoutset dependencies if any
263static bool __kmp_task_is_allowed(int gtid, const kmp_int32 is_constrained,
264 const kmp_taskdata_t *tasknew,
265 const kmp_taskdata_t *taskcurr) {
266 if (is_constrained && (tasknew->td_flags.tiedness == TASK_TIED)) {
267 // Check if the candidate obeys the Task Scheduling Constraints (TSC)
268 // only descendant of all deferred tied tasks can be scheduled, checking
269 // the last one is enough, as it in turn is the descendant of all others
270 kmp_taskdata_t *current = taskcurr->td_last_tied;
271 KMP_DEBUG_ASSERT(current != NULL);
272 // check if the task is not suspended on barrier
273 if (current->td_flags.tasktype == TASK_EXPLICIT ||
274 current->td_taskwait_thread > 0) { // <= 0 on barrier
275 kmp_int32 level = current->td_level;
276 kmp_taskdata_t *parent = tasknew->td_parent;
277 while (parent != current && parent->td_level > level) {
278 // check generation up to the level of the current task
279 parent = parent->td_parent;
280 KMP_DEBUG_ASSERT(parent != NULL);
281 }
282 if (parent != current)
283 return false;
284 }
285 }
286 // Check mutexinoutset dependencies, acquire locks
287 kmp_depnode_t *node = tasknew->td_depnode;
288#if OMPX_TASKGRAPH
289 if (!tasknew->is_taskgraph && UNLIKELY(node && (node->dn.mtx_num_locks > 0))) {
290#else
291 if (UNLIKELY(node && (node->dn.mtx_num_locks > 0))) {
292#endif
293 for (int i = 0; i < node->dn.mtx_num_locks; ++i) {
294 KMP_DEBUG_ASSERT(node->dn.mtx_locks[i] != NULL);
295 if (__kmp_test_lock(node->dn.mtx_locks[i], gtid))
296 continue;
297 // could not get the lock, release previous locks
298 for (int j = i - 1; j >= 0; --j)
299 __kmp_release_lock(node->dn.mtx_locks[j], gtid);
300 return false;
301 }
302 // negative num_locks means all locks acquired successfully
303 node->dn.mtx_num_locks = -node->dn.mtx_num_locks;
304 }
305 return true;
306}
307
308// __kmp_realloc_task_deque:
309// Re-allocates a task deque for a particular thread, copies the content from
310// the old deque and adjusts the necessary data structures relating to the
311// deque. This operation must be done with the deque_lock being held
313 kmp_thread_data_t *thread_data) {
314 kmp_int32 size = TASK_DEQUE_SIZE(thread_data->td);
315 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == size);
316 kmp_int32 new_size = 2 * size;
317
318 KE_TRACE(10, ("__kmp_realloc_task_deque: T#%d reallocating deque[from %d to "
319 "%d] for thread_data %p\n",
320 __kmp_gtid_from_thread(thread), size, new_size, thread_data));
321
322 kmp_taskdata_t **new_deque =
324
325 int i, j;
326 for (i = thread_data->td.td_deque_head, j = 0; j < size;
327 i = (i + 1) & TASK_DEQUE_MASK(thread_data->td), j++)
328 new_deque[j] = thread_data->td.td_deque[i];
329
330 __kmp_free(thread_data->td.td_deque);
331
332 thread_data->td.td_deque_head = 0;
333 thread_data->td.td_deque_tail = size;
334 thread_data->td.td_deque = new_deque;
335 thread_data->td.td_deque_size = new_size;
336}
337
340 kmp_thread_data_t *thread_data = &l->td;
341 __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
342 thread_data->td.td_deque_last_stolen = -1;
343 KE_TRACE(20, ("__kmp_alloc_task_pri_list: T#%d allocating deque[%d] "
344 "for thread_data %p\n",
345 __kmp_get_gtid(), INITIAL_TASK_DEQUE_SIZE, thread_data));
346 thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
348 thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
349 return l;
350}
351
352// The function finds the deque of priority tasks with given priority, or
353// allocates a new deque and put it into sorted (high -> low) list of deques.
354// Deques of non-default priority tasks are shared between all threads in team,
355// as opposed to per-thread deques of tasks with default priority.
356// The function is called under the lock task_team->tt.tt_task_pri_lock.
357static kmp_thread_data_t *
359 kmp_thread_data_t *thread_data;
360 kmp_task_pri_t *lst = task_team->tt.tt_task_pri_list;
361 if (lst->priority == pri) {
362 // Found queue of tasks with given priority.
363 thread_data = &lst->td;
364 } else if (lst->priority < pri) {
365 // All current priority queues contain tasks with lower priority.
366 // Allocate new one for given priority tasks.
368 thread_data = &list->td;
369 list->priority = pri;
370 list->next = lst;
371 task_team->tt.tt_task_pri_list = list;
372 } else { // task_team->tt.tt_task_pri_list->priority > pri
373 kmp_task_pri_t *next_queue = lst->next;
374 while (next_queue && next_queue->priority > pri) {
375 lst = next_queue;
376 next_queue = lst->next;
377 }
378 // lst->priority > pri && (next == NULL || pri >= next->priority)
379 if (next_queue == NULL) {
380 // No queue with pri priority, need to allocate new one.
382 thread_data = &list->td;
383 list->priority = pri;
384 list->next = NULL;
385 lst->next = list;
386 } else if (next_queue->priority == pri) {
387 // Found queue of tasks with given priority.
388 thread_data = &next_queue->td;
389 } else { // lst->priority > pri > next->priority
390 // insert newly allocated between existed queues
392 thread_data = &list->td;
393 list->priority = pri;
394 list->next = next_queue;
395 lst->next = list;
396 }
397 }
398 return thread_data;
399}
400
401// __kmp_push_priority_task: Add a task to the team's priority task deque
403 kmp_taskdata_t *taskdata,
404 kmp_task_team_t *task_team,
405 kmp_int32 pri) {
406 kmp_thread_data_t *thread_data = NULL;
407 KA_TRACE(20,
408 ("__kmp_push_priority_task: T#%d trying to push task %p, pri %d.\n",
409 gtid, taskdata, pri));
410
411 // Find task queue specific to priority value
412 kmp_task_pri_t *lst = task_team->tt.tt_task_pri_list;
413 if (UNLIKELY(lst == NULL)) {
415 if (task_team->tt.tt_task_pri_list == NULL) {
416 // List of queues is still empty, allocate one.
418 thread_data = &list->td;
419 list->priority = pri;
420 list->next = NULL;
421 task_team->tt.tt_task_pri_list = list;
422 } else {
423 // Other thread initialized a queue. Check if it fits and get thread_data.
424 thread_data = __kmp_get_priority_deque_data(task_team, pri);
425 }
427 } else {
428 if (lst->priority == pri) {
429 // Found queue of tasks with given priority.
430 thread_data = &lst->td;
431 } else {
433 thread_data = __kmp_get_priority_deque_data(task_team, pri);
435 }
436 }
437 KMP_DEBUG_ASSERT(thread_data);
438
439 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
440 // Check if deque is full
441 if (TCR_4(thread_data->td.td_deque_ntasks) >=
442 TASK_DEQUE_SIZE(thread_data->td)) {
445 thread->th.th_current_task)) {
446 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
447 KA_TRACE(20, ("__kmp_push_priority_task: T#%d deque is full; returning "
448 "TASK_NOT_PUSHED for task %p\n",
449 gtid, taskdata));
450 return TASK_NOT_PUSHED;
451 } else {
452 // expand deque to push the task which is not allowed to execute
453 __kmp_realloc_task_deque(thread, thread_data);
454 }
455 }
456 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) <
457 TASK_DEQUE_SIZE(thread_data->td));
458 // Push taskdata.
459 thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
460 // Wrap index.
461 thread_data->td.td_deque_tail =
462 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
463 TCW_4(thread_data->td.td_deque_ntasks,
464 TCR_4(thread_data->td.td_deque_ntasks) + 1); // Adjust task count
465 KMP_FSYNC_RELEASING(thread->th.th_current_task); // releasing self
466 KMP_FSYNC_RELEASING(taskdata); // releasing child
467 KA_TRACE(20, ("__kmp_push_priority_task: T#%d returning "
468 "TASK_SUCCESSFULLY_PUSHED: task=%p ntasks=%d head=%u tail=%u\n",
469 gtid, taskdata, thread_data->td.td_deque_ntasks,
470 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
471 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
472 task_team->tt.tt_num_task_pri++; // atomic inc
474}
475
476// __kmp_push_task: Add a task to the thread's deque
478 kmp_info_t *thread = __kmp_threads[gtid];
480
481 // If we encounter a hidden helper task, and the current thread is not a
482 // hidden helper thread, we have to give the task to any hidden helper thread
483 // starting from its shadow one.
484 if (UNLIKELY(taskdata->td_flags.hidden_helper &&
485 !KMP_HIDDEN_HELPER_THREAD(gtid))) {
486 kmp_int32 shadow_gtid = KMP_GTID_TO_SHADOW_GTID(gtid);
488 // Signal the hidden helper threads.
491 }
492
493 kmp_task_team_t *task_team = thread->th.th_task_team;
494 kmp_int32 tid = __kmp_tid_from_gtid(gtid);
495 kmp_thread_data_t *thread_data;
496
497 KA_TRACE(20,
498 ("__kmp_push_task: T#%d trying to push task %p.\n", gtid, taskdata));
499
500 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
501 // untied task needs to increment counter so that the task structure is not
502 // freed prematurely
505 KA_TRACE(
506 20,
507 ("__kmp_push_task: T#%d untied_count (%d) incremented for task %p\n",
508 gtid, counter, taskdata));
509 }
510
511 // The first check avoids building task_team thread data if serialized
512 if (UNLIKELY(taskdata->td_flags.task_serial)) {
513 KA_TRACE(20, ("__kmp_push_task: T#%d team serialized; returning "
514 "TASK_NOT_PUSHED for task %p\n",
515 gtid, taskdata));
516 return TASK_NOT_PUSHED;
517 }
518
519 // Now that serialized tasks have returned, we can assume that we are not in
520 // immediate exec mode
522 if (UNLIKELY(!KMP_TASKING_ENABLED(task_team))) {
523 __kmp_enable_tasking(task_team, thread);
524 }
526 KMP_DEBUG_ASSERT(TCR_PTR(task_team->tt.tt_threads_data) != NULL);
527
528 if (taskdata->td_flags.priority_specified && task->data2.priority > 0 &&
530 int pri = KMP_MIN(task->data2.priority, __kmp_max_task_priority);
531 return __kmp_push_priority_task(gtid, thread, taskdata, task_team, pri);
532 }
533
534 // Find tasking deque specific to encountering thread
535 thread_data = &task_team->tt.tt_threads_data[tid];
536
537 // No lock needed since only owner can allocate. If the task is hidden_helper,
538 // we don't need it either because we have initialized the dequeue for hidden
539 // helper thread data.
540 if (UNLIKELY(thread_data->td.td_deque == NULL)) {
541 __kmp_alloc_task_deque(thread, thread_data);
542 }
543
544 int locked = 0;
545 // Check if deque is full
546 if (TCR_4(thread_data->td.td_deque_ntasks) >=
547 TASK_DEQUE_SIZE(thread_data->td)) {
550 thread->th.th_current_task)) {
551 KA_TRACE(20, ("__kmp_push_task: T#%d deque is full; returning "
552 "TASK_NOT_PUSHED for task %p\n",
553 gtid, taskdata));
554 return TASK_NOT_PUSHED;
555 } else {
556 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
557 locked = 1;
558 if (TCR_4(thread_data->td.td_deque_ntasks) >=
559 TASK_DEQUE_SIZE(thread_data->td)) {
560 // expand deque to push the task which is not allowed to execute
561 __kmp_realloc_task_deque(thread, thread_data);
562 }
563 }
564 }
565 // Lock the deque for the task push operation
566 if (!locked) {
567 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
568 // Need to recheck as we can get a proxy task from thread outside of OpenMP
569 if (TCR_4(thread_data->td.td_deque_ntasks) >=
570 TASK_DEQUE_SIZE(thread_data->td)) {
573 thread->th.th_current_task)) {
574 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
575 KA_TRACE(20, ("__kmp_push_task: T#%d deque is full on 2nd check; "
576 "returning TASK_NOT_PUSHED for task %p\n",
577 gtid, taskdata));
578 return TASK_NOT_PUSHED;
579 } else {
580 // expand deque to push the task which is not allowed to execute
581 __kmp_realloc_task_deque(thread, thread_data);
582 }
583 }
584 }
585 // Must have room since no thread can add tasks but calling thread
586 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) <
587 TASK_DEQUE_SIZE(thread_data->td));
588
589 thread_data->td.td_deque[thread_data->td.td_deque_tail] =
590 taskdata; // Push taskdata
591 // Wrap index.
592 thread_data->td.td_deque_tail =
593 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
594 TCW_4(thread_data->td.td_deque_ntasks,
595 TCR_4(thread_data->td.td_deque_ntasks) + 1); // Adjust task count
596 KMP_FSYNC_RELEASING(thread->th.th_current_task); // releasing self
597 KMP_FSYNC_RELEASING(taskdata); // releasing child
598 KA_TRACE(20, ("__kmp_push_task: T#%d returning TASK_SUCCESSFULLY_PUSHED: "
599 "task=%p ntasks=%d head=%u tail=%u\n",
600 gtid, taskdata, thread_data->td.td_deque_ntasks,
601 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
602
603 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
604
606}
607
608// __kmp_pop_current_task_from_thread: set up current task from called thread
609// when team ends
610//
611// this_thr: thread structure to set current_task in.
613 KF_TRACE(10, ("__kmp_pop_current_task_from_thread(enter): T#%d "
614 "this_thread=%p, curtask=%p, "
615 "curtask_parent=%p\n",
616 0, this_thr, this_thr->th.th_current_task,
617 this_thr->th.th_current_task->td_parent));
618
619 this_thr->th.th_current_task = this_thr->th.th_current_task->td_parent;
620
621 KF_TRACE(10, ("__kmp_pop_current_task_from_thread(exit): T#%d "
622 "this_thread=%p, curtask=%p, "
623 "curtask_parent=%p\n",
624 0, this_thr, this_thr->th.th_current_task,
625 this_thr->th.th_current_task->td_parent));
626}
627
628// __kmp_push_current_task_to_thread: set up current task in called thread for a
629// new team
630//
631// this_thr: thread structure to set up
632// team: team for implicit task data
633// tid: thread within team to set up
635 int tid) {
636 // current task of the thread is a parent of the new just created implicit
637 // tasks of new team
638 KF_TRACE(10, ("__kmp_push_current_task_to_thread(enter): T#%d this_thread=%p "
639 "curtask=%p "
640 "parent_task=%p\n",
641 tid, this_thr, this_thr->th.th_current_task,
642 team->t.t_implicit_task_taskdata[tid].td_parent));
643
644 KMP_DEBUG_ASSERT(this_thr != NULL);
645
646 if (tid == 0) {
647 if (this_thr->th.th_current_task != &team->t.t_implicit_task_taskdata[0]) {
648 team->t.t_implicit_task_taskdata[0].td_parent =
649 this_thr->th.th_current_task;
650 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[0];
651 }
652 } else {
653 team->t.t_implicit_task_taskdata[tid].td_parent =
654 team->t.t_implicit_task_taskdata[0].td_parent;
655 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[tid];
656 }
657
658 KF_TRACE(10, ("__kmp_push_current_task_to_thread(exit): T#%d this_thread=%p "
659 "curtask=%p "
660 "parent_task=%p\n",
661 tid, this_thr, this_thr->th.th_current_task,
662 team->t.t_implicit_task_taskdata[tid].td_parent));
663}
664
665// __kmp_task_start: bookkeeping for a task starting execution
666//
667// GTID: global thread id of calling thread
668// task: task starting execution
669// current_task: task suspending
671 kmp_taskdata_t *current_task) {
673 kmp_info_t *thread = __kmp_threads[gtid];
674
675 KA_TRACE(10,
676 ("__kmp_task_start(enter): T#%d starting task %p: current_task=%p\n",
677 gtid, taskdata, current_task));
678
680
681 // mark currently executing task as suspended
682 // TODO: GEH - make sure root team implicit task is initialized properly.
683 // KMP_DEBUG_ASSERT( current_task -> td_flags.executing == 1 );
684 current_task->td_flags.executing = 0;
685
686// Add task to stack if tied
687#ifdef BUILD_TIED_TASK_STACK
688 if (taskdata->td_flags.tiedness == TASK_TIED) {
689 __kmp_push_task_stack(gtid, thread, taskdata);
690 }
691#endif /* BUILD_TIED_TASK_STACK */
692
693 // mark starting task as executing and as current task
694 thread->th.th_current_task = taskdata;
695
696 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 0 ||
697 taskdata->td_flags.tiedness == TASK_UNTIED);
698 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0 ||
699 taskdata->td_flags.tiedness == TASK_UNTIED);
700 taskdata->td_flags.started = 1;
701 taskdata->td_flags.executing = 1;
702 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
703 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
704
705 // GEH TODO: shouldn't we pass some sort of location identifier here?
706 // APT: yes, we will pass location here.
707 // need to store current thread state (in a thread or taskdata structure)
708 // before setting work_state, otherwise wrong state is set after end of task
709
710 KA_TRACE(10, ("__kmp_task_start(exit): T#%d task=%p\n", gtid, taskdata));
711
712 return;
713}
714
715#if OMPT_SUPPORT
716//------------------------------------------------------------------------------
717// __ompt_task_init:
718// Initialize OMPT fields maintained by a task. This will only be called after
719// ompt_start_tool, so we already know whether ompt is enabled or not.
720
721static inline void __ompt_task_init(kmp_taskdata_t *task, int tid) {
722 // The calls to __ompt_task_init already have the ompt_enabled condition.
723 task->ompt_task_info.task_data.value = 0;
724 task->ompt_task_info.frame.exit_frame = ompt_data_none;
725 task->ompt_task_info.frame.enter_frame = ompt_data_none;
726 task->ompt_task_info.frame.exit_frame_flags =
727 ompt_frame_runtime | ompt_frame_framepointer;
728 task->ompt_task_info.frame.enter_frame_flags =
729 ompt_frame_runtime | ompt_frame_framepointer;
730 task->ompt_task_info.dispatch_chunk.start = 0;
731 task->ompt_task_info.dispatch_chunk.iterations = 0;
732}
733
734// __ompt_task_start:
735// Build and trigger task-begin event
736static inline void __ompt_task_start(kmp_task_t *task,
737 kmp_taskdata_t *current_task,
738 kmp_int32 gtid) {
740 ompt_task_status_t status = ompt_task_switch;
741 if (__kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded) {
742 status = ompt_task_yield;
743 __kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded = 0;
744 }
745 /* let OMPT know that we're about to run this task */
746 if (ompt_enabled.ompt_callback_task_schedule) {
747 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
748 &(current_task->ompt_task_info.task_data), status,
749 &(taskdata->ompt_task_info.task_data));
750 }
751 taskdata->ompt_task_info.scheduling_parent = current_task;
752}
753
754// __ompt_task_finish:
755// Build and trigger final task-schedule event
756static inline void __ompt_task_finish(kmp_task_t *task,
757 kmp_taskdata_t *resumed_task,
758 ompt_task_status_t status) {
759 if (ompt_enabled.ompt_callback_task_schedule) {
761 if (__kmp_omp_cancellation && taskdata->td_taskgroup &&
763 status = ompt_task_cancel;
764 }
765
766 /* let OMPT know that we're returning to the callee task */
767 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
768 &(taskdata->ompt_task_info.task_data), status,
769 (resumed_task ? &(resumed_task->ompt_task_info.task_data) : NULL));
770 }
771}
772#endif
773
774template <bool ompt>
777 void *frame_address,
778 void *return_address) {
780 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
781
782 KA_TRACE(10, ("__kmpc_omp_task_begin_if0(enter): T#%d loc=%p task=%p "
783 "current_task=%p\n",
784 gtid, loc_ref, taskdata, current_task));
785
786 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
787 // untied task needs to increment counter so that the task structure is not
788 // freed prematurely
791 KA_TRACE(20, ("__kmpc_omp_task_begin_if0: T#%d untied_count (%d) "
792 "incremented for task %p\n",
793 gtid, counter, taskdata));
794 }
795
796 taskdata->td_flags.task_serial =
797 1; // Execute this task immediately, not deferred.
798 __kmp_task_start(gtid, task, current_task);
799
800#if OMPT_SUPPORT
801 if (ompt) {
802 if (current_task->ompt_task_info.frame.enter_frame.ptr == NULL) {
803 current_task->ompt_task_info.frame.enter_frame.ptr =
804 taskdata->ompt_task_info.frame.exit_frame.ptr = frame_address;
805 current_task->ompt_task_info.frame.enter_frame_flags =
806 taskdata->ompt_task_info.frame.exit_frame_flags =
807 ompt_frame_application | ompt_frame_framepointer;
808 }
809 if (ompt_enabled.ompt_callback_task_create) {
810 ompt_task_info_t *parent_info = &(current_task->ompt_task_info);
811 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
812 &(parent_info->task_data), &(parent_info->frame),
813 &(taskdata->ompt_task_info.task_data),
814 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(taskdata), 0,
815 return_address);
816 }
817 __ompt_task_start(task, current_task, gtid);
818 }
819#endif // OMPT_SUPPORT
820
821 KA_TRACE(10, ("__kmpc_omp_task_begin_if0(exit): T#%d loc=%p task=%p,\n", gtid,
822 loc_ref, taskdata));
823}
824
825#if OMPT_SUPPORT
827static void __kmpc_omp_task_begin_if0_ompt(ident_t *loc_ref, kmp_int32 gtid,
829 void *frame_address,
830 void *return_address) {
831 __kmpc_omp_task_begin_if0_template<true>(loc_ref, gtid, task, frame_address,
832 return_address);
833}
834#endif // OMPT_SUPPORT
835
836// __kmpc_omp_task_begin_if0: report that a given serialized task has started
837// execution
838//
839// loc_ref: source location information; points to beginning of task block.
840// gtid: global thread number.
841// task: task thunk for the started task.
842#ifdef __s390x__
843// This is required for OMPT_GET_FRAME_ADDRESS(1) to compile on s390x.
844// In order for it to work correctly, the caller also needs to be compiled with
845// backchain. If a caller is compiled without backchain,
846// OMPT_GET_FRAME_ADDRESS(1) will produce an incorrect value, but will not
847// crash.
848__attribute__((target("backchain")))
849#endif
851 kmp_task_t *task) {
852#if OMPT_SUPPORT
854 OMPT_STORE_RETURN_ADDRESS(gtid);
855 __kmpc_omp_task_begin_if0_ompt(loc_ref, gtid, task,
857 OMPT_LOAD_RETURN_ADDRESS(gtid));
858 return;
859 }
860#endif
861 __kmpc_omp_task_begin_if0_template<false>(loc_ref, gtid, task, NULL, NULL);
862}
863
864#ifdef TASK_UNUSED
865// __kmpc_omp_task_begin: report that a given task has started execution
866// NEVER GENERATED BY COMPILER, DEPRECATED!!!
867void __kmpc_omp_task_begin(ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task) {
868 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
869
870 KA_TRACE(
871 10,
872 ("__kmpc_omp_task_begin(enter): T#%d loc=%p task=%p current_task=%p\n",
873 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task), current_task));
874
875 __kmp_task_start(gtid, task, current_task);
876
877 KA_TRACE(10, ("__kmpc_omp_task_begin(exit): T#%d loc=%p task=%p,\n", gtid,
878 loc_ref, KMP_TASK_TO_TASKDATA(task)));
879 return;
880}
881#endif // TASK_UNUSED
882
883// __kmp_free_task: free the current task space and the space for shareds
884//
885// gtid: Global thread ID of calling thread
886// taskdata: task to free
887// thread: thread data structure of caller
888static void __kmp_free_task(kmp_int32 gtid, kmp_taskdata_t *taskdata,
889 kmp_info_t *thread) {
890 KA_TRACE(30, ("__kmp_free_task: T#%d freeing data from task %p\n", gtid,
891 taskdata));
892
893 // Check to make sure all flags and counters have the correct values
895 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0);
896 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 1);
897 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
899 taskdata->td_flags.task_serial == 1);
902 // Clear data to not be re-used later by mistake.
903 task->data1.destructors = NULL;
904 task->data2.priority = 0;
905
906 taskdata->td_flags.freed = 1;
907#if OMPX_TASKGRAPH
908 // do not free tasks in taskgraph
909 if (!taskdata->is_taskgraph) {
910#endif
911// deallocate the taskdata and shared variable blocks associated with this task
912#if USE_FAST_MEMORY
913 __kmp_fast_free(thread, taskdata);
914#else /* ! USE_FAST_MEMORY */
915 __kmp_thread_free(thread, taskdata);
916#endif
917#if OMPX_TASKGRAPH
918 } else {
919 taskdata->td_flags.complete = 0;
920 taskdata->td_flags.started = 0;
921 taskdata->td_flags.freed = 0;
922 taskdata->td_flags.executing = 0;
923 taskdata->td_flags.task_serial =
924 (taskdata->td_parent->td_flags.final ||
925 taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser);
926
927 // taskdata->td_allow_completion_event.pending_events_count = 1;
928 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
930 // start at one because counts current task and children
932 }
933#endif
934
935 KA_TRACE(20, ("__kmp_free_task: T#%d freed task %p\n", gtid, taskdata));
936}
937
938// __kmp_free_task_and_ancestors: free the current task and ancestors without
939// children
940//
941// gtid: Global thread ID of calling thread
942// taskdata: task to free
943// thread: thread data structure of caller
945 kmp_taskdata_t *taskdata,
946 kmp_info_t *thread) {
947 // Proxy tasks must always be allowed to free their parents
948 // because they can be run in background even in serial mode.
949 kmp_int32 team_serial =
950 (taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) &&
951 !taskdata->td_flags.proxy;
953
954 kmp_int32 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
955 KMP_DEBUG_ASSERT(children >= 0);
956
957 // Now, go up the ancestor tree to see if any ancestors can now be freed.
958 while (children == 0) {
959 kmp_taskdata_t *parent_taskdata = taskdata->td_parent;
960
961 KA_TRACE(20, ("__kmp_free_task_and_ancestors(enter): T#%d task %p complete "
962 "and freeing itself\n",
963 gtid, taskdata));
964
965 // --- Deallocate my ancestor task ---
966 __kmp_free_task(gtid, taskdata, thread);
967
968 taskdata = parent_taskdata;
969
970 if (team_serial)
971 return;
972 // Stop checking ancestors at implicit task instead of walking up ancestor
973 // tree to avoid premature deallocation of ancestors.
974 if (taskdata->td_flags.tasktype == TASK_IMPLICIT) {
975 if (taskdata->td_dephash) { // do we need to cleanup dephash?
976 int children = KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks);
977 kmp_tasking_flags_t flags_old = taskdata->td_flags;
978 if (children == 0 && flags_old.complete == 1) {
979 kmp_tasking_flags_t flags_new = flags_old;
980 flags_new.complete = 0;
982 RCAST(kmp_int32 *, &taskdata->td_flags),
983 *RCAST(kmp_int32 *, &flags_old),
984 *RCAST(kmp_int32 *, &flags_new))) {
985 KA_TRACE(100, ("__kmp_free_task_and_ancestors: T#%d cleans "
986 "dephash of implicit task %p\n",
987 gtid, taskdata));
988 // cleanup dephash of finished implicit task
989 __kmp_dephash_free_entries(thread, taskdata->td_dephash);
990 }
991 }
992 }
993 return;
994 }
995 // Predecrement simulated by "- 1" calculation
996 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
997 KMP_DEBUG_ASSERT(children >= 0);
998 }
999
1000 KA_TRACE(
1001 20, ("__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; "
1002 "not freeing it yet\n",
1003 gtid, taskdata, children));
1004}
1005
1006// Only need to keep track of child task counts if any of the following:
1007// 1. team parallel and tasking not serialized;
1008// 2. it is a proxy or detachable or hidden helper task
1009// 3. the children counter of its parent task is greater than 0.
1010// The reason for the 3rd one is for serialized team that found detached task,
1011// hidden helper task, T. In this case, the execution of T is still deferred,
1012// and it is also possible that a regular task depends on T. In this case, if we
1013// don't track the children, task synchronization will be broken.
1015 kmp_tasking_flags_t flags = taskdata->td_flags;
1016 bool ret = !(flags.team_serial || flags.tasking_ser);
1017 ret = ret || flags.proxy == TASK_PROXY ||
1018 flags.detachable == TASK_DETACHABLE || flags.hidden_helper;
1019 ret = ret ||
1021#if OMPX_TASKGRAPH
1022 if (taskdata->td_taskgroup && taskdata->is_taskgraph)
1023 ret = ret || KMP_ATOMIC_LD_ACQ(&taskdata->td_taskgroup->count) > 0;
1024#endif
1025 return ret;
1026}
1027
1028// __kmp_task_finish: bookkeeping to do when a task finishes execution
1029//
1030// gtid: global thread ID for calling thread
1031// task: task to be finished
1032// resumed_task: task to be resumed. (may be NULL if task is serialized)
1033//
1034// template<ompt>: effectively ompt_enabled.enabled!=0
1035// the version with ompt=false is inlined, allowing to optimize away all ompt
1036// code in this case
1037template <bool ompt>
1039 kmp_taskdata_t *resumed_task) {
1041 kmp_info_t *thread = __kmp_threads[gtid];
1042 kmp_task_team_t *task_team =
1043 thread->th.th_task_team; // might be NULL for serial teams...
1044#if OMPX_TASKGRAPH
1045 // to avoid seg fault when we need to access taskdata->td_flags after free when using vanilla taskloop
1046 bool is_taskgraph;
1047#endif
1048#if KMP_DEBUG
1049 kmp_int32 children = 0;
1050#endif
1051 KA_TRACE(10, ("__kmp_task_finish(enter): T#%d finishing task %p and resuming "
1052 "task %p\n",
1053 gtid, taskdata, resumed_task));
1054
1056
1057#if OMPX_TASKGRAPH
1058 is_taskgraph = taskdata->is_taskgraph;
1059#endif
1060
1061// Pop task from stack if tied
1062#ifdef BUILD_TIED_TASK_STACK
1063 if (taskdata->td_flags.tiedness == TASK_TIED) {
1064 __kmp_pop_task_stack(gtid, thread, taskdata);
1065 }
1066#endif /* BUILD_TIED_TASK_STACK */
1067
1068 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) {
1069 // untied task needs to check the counter so that the task structure is not
1070 // freed prematurely
1072 KA_TRACE(
1073 20,
1074 ("__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n",
1075 gtid, counter, taskdata));
1076 if (counter > 0) {
1077 // untied task is not done, to be continued possibly by other thread, do
1078 // not free it now
1079 if (resumed_task == NULL) {
1081 resumed_task = taskdata->td_parent; // In a serialized task, the resumed
1082 // task is the parent
1083 }
1084 thread->th.th_current_task = resumed_task; // restore current_task
1085 resumed_task->td_flags.executing = 1; // resume previous task
1086 KA_TRACE(10, ("__kmp_task_finish(exit): T#%d partially done task %p, "
1087 "resuming task %p\n",
1088 gtid, taskdata, resumed_task));
1089 return;
1090 }
1091 }
1092
1093 // bookkeeping for resuming task:
1094 // GEH - note tasking_ser => task_serial
1096 (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) ==
1097 taskdata->td_flags.task_serial);
1098 if (taskdata->td_flags.task_serial) {
1099 if (resumed_task == NULL) {
1100 resumed_task = taskdata->td_parent; // In a serialized task, the resumed
1101 // task is the parent
1102 }
1103 } else {
1104 KMP_DEBUG_ASSERT(resumed_task !=
1105 NULL); // verify that resumed task is passed as argument
1106 }
1107
1108 /* If the tasks' destructor thunk flag has been set, we need to invoke the
1109 destructor thunk that has been generated by the compiler. The code is
1110 placed here, since at this point other tasks might have been released
1111 hence overlapping the destructor invocations with some other work in the
1112 released tasks. The OpenMP spec is not specific on when the destructors
1113 are invoked, so we should be free to choose. */
1114 if (UNLIKELY(taskdata->td_flags.destructors_thunk)) {
1115 kmp_routine_entry_t destr_thunk = task->data1.destructors;
1116 KMP_ASSERT(destr_thunk);
1117 destr_thunk(gtid, task);
1118 }
1119
1120 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
1121 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 1);
1122 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
1123
1124 bool completed = true;
1125 if (UNLIKELY(taskdata->td_flags.detachable == TASK_DETACHABLE)) {
1126 if (taskdata->td_allow_completion_event.type ==
1128 // event hasn't been fulfilled yet. Try to detach task.
1130 if (taskdata->td_allow_completion_event.type ==
1132 // task finished execution
1133 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
1134 taskdata->td_flags.executing = 0; // suspend the finishing task
1135
1136#if OMPT_SUPPORT
1137 // For a detached task, which is not completed, we switch back
1138 // the omp_fulfill_event signals completion
1139 // locking is necessary to avoid a race with ompt_task_late_fulfill
1140 if (ompt)
1141 __ompt_task_finish(task, resumed_task, ompt_task_detach);
1142#endif
1143
1144 // no access to taskdata after this point!
1145 // __kmp_fulfill_event might free taskdata at any time from now
1146
1147 taskdata->td_flags.proxy = TASK_PROXY; // proxify!
1148 completed = false;
1149 }
1151 }
1152 }
1153
1154 // Tasks with valid target async handles must be re-enqueued.
1155 if (taskdata->td_target_data.async_handle != NULL) {
1156 // Note: no need to translate gtid to its shadow. If the current thread is a
1157 // hidden helper one, then the gtid is already correct. Otherwise, hidden
1158 // helper threads are disabled, and gtid refers to a OpenMP thread.
1160 if (KMP_HIDDEN_HELPER_THREAD(gtid))
1162 completed = false;
1163 }
1164
1165 if (completed) {
1166 taskdata->td_flags.complete = 1; // mark the task as completed
1167#if OMPX_TASKGRAPH
1168 taskdata->td_flags.onced = 1; // mark the task as ran once already
1169#endif
1170
1171#if OMPT_SUPPORT
1172 // This is not a detached task, we are done here
1173 if (ompt)
1174 __ompt_task_finish(task, resumed_task, ompt_task_complete);
1175#endif
1176 // TODO: What would be the balance between the conditions in the function
1177 // and an atomic operation?
1178 if (__kmp_track_children_task(taskdata)) {
1179 __kmp_release_deps(gtid, taskdata);
1180 // Predecrement simulated by "- 1" calculation
1181#if KMP_DEBUG
1182 children = -1 +
1183#endif
1185 KMP_DEBUG_ASSERT(children >= 0);
1186#if OMPX_TASKGRAPH
1187 if (taskdata->td_taskgroup && !taskdata->is_taskgraph)
1188#else
1189 if (taskdata->td_taskgroup)
1190#endif
1191 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
1192 } else if (task_team && (task_team->tt.tt_found_proxy_tasks ||
1194 // if we found proxy or hidden helper tasks there could exist a dependency
1195 // chain with the proxy task as origin
1196 __kmp_release_deps(gtid, taskdata);
1197 }
1198 // td_flags.executing must be marked as 0 after __kmp_release_deps has been
1199 // called. Othertwise, if a task is executed immediately from the
1200 // release_deps code, the flag will be reset to 1 again by this same
1201 // function
1202 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
1203 taskdata->td_flags.executing = 0; // suspend the finishing task
1204
1205 // Decrement the counter of hidden helper tasks to be executed.
1206 if (taskdata->td_flags.hidden_helper) {
1207 // Hidden helper tasks can only be executed by hidden helper threads.
1210 }
1211 }
1212
1213 KA_TRACE(
1214 20, ("__kmp_task_finish: T#%d finished task %p, %d incomplete children\n",
1215 gtid, taskdata, children));
1216
1217 // Free this task and then ancestor tasks if they have no children.
1218 // Restore th_current_task first as suggested by John:
1219 // johnmc: if an asynchronous inquiry peers into the runtime system
1220 // it doesn't see the freed task as the current task.
1221 thread->th.th_current_task = resumed_task;
1222 if (completed)
1223 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
1224
1225 // TODO: GEH - make sure root team implicit task is initialized properly.
1226 // KMP_DEBUG_ASSERT( resumed_task->td_flags.executing == 0 );
1227 resumed_task->td_flags.executing = 1; // resume previous task
1228
1229#if OMPX_TASKGRAPH
1230 if (is_taskgraph && __kmp_track_children_task(taskdata) &&
1231 taskdata->td_taskgroup) {
1232 // TDG: we only release taskgroup barrier here because
1233 // free_task_and_ancestors will call
1234 // __kmp_free_task, which resets all task parameters such as
1235 // taskdata->started, etc. If we release the barrier earlier, these
1236 // parameters could be read before being reset. This is not an issue for
1237 // non-TDG implementation because we never reuse a task(data) structure
1238 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
1239 }
1240#endif
1241
1242 KA_TRACE(
1243 10, ("__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n",
1244 gtid, taskdata, resumed_task));
1245
1246 return;
1247}
1248
1249template <bool ompt>
1251 kmp_int32 gtid,
1252 kmp_task_t *task) {
1253 KA_TRACE(10, ("__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n",
1254 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
1255 KMP_DEBUG_ASSERT(gtid >= 0);
1256 // this routine will provide task to resume
1257 __kmp_task_finish<ompt>(gtid, task, NULL);
1258
1259 KA_TRACE(10, ("__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n",
1260 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
1261
1262#if OMPT_SUPPORT
1263 if (ompt) {
1264 ompt_frame_t *ompt_frame;
1265 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
1266 ompt_frame->enter_frame = ompt_data_none;
1267 ompt_frame->enter_frame_flags =
1268 ompt_frame_runtime | ompt_frame_framepointer;
1269 }
1270#endif
1271
1272 return;
1273}
1274
1275#if OMPT_SUPPORT
1277void __kmpc_omp_task_complete_if0_ompt(ident_t *loc_ref, kmp_int32 gtid,
1278 kmp_task_t *task) {
1279 __kmpc_omp_task_complete_if0_template<true>(loc_ref, gtid, task);
1280}
1281#endif // OMPT_SUPPORT
1282
1283// __kmpc_omp_task_complete_if0: report that a task has completed execution
1284//
1285// loc_ref: source location information; points to end of task block.
1286// gtid: global thread number.
1287// task: task thunk for the completed task.
1289 kmp_task_t *task) {
1290#if OMPT_SUPPORT
1292 __kmpc_omp_task_complete_if0_ompt(loc_ref, gtid, task);
1293 return;
1294 }
1295#endif
1296 __kmpc_omp_task_complete_if0_template<false>(loc_ref, gtid, task);
1297}
1298
1299#ifdef TASK_UNUSED
1300// __kmpc_omp_task_complete: report that a task has completed execution
1301// NEVER GENERATED BY COMPILER, DEPRECATED!!!
1302void __kmpc_omp_task_complete(ident_t *loc_ref, kmp_int32 gtid,
1303 kmp_task_t *task) {
1304 KA_TRACE(10, ("__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n", gtid,
1305 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1306
1307 __kmp_task_finish<false>(gtid, task,
1308 NULL); // Not sure how to find task to resume
1309
1310 KA_TRACE(10, ("__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n", gtid,
1311 loc_ref, KMP_TASK_TO_TASKDATA(task)));
1312 return;
1313}
1314#endif // TASK_UNUSED
1315
1316// __kmp_init_implicit_task: Initialize the appropriate fields in the implicit
1317// task for a given thread
1318//
1319// loc_ref: reference to source location of parallel region
1320// this_thr: thread data structure corresponding to implicit task
1321// team: team for this_thr
1322// tid: thread id of given thread within team
1323// set_curr_task: TRUE if need to push current task to thread
1324// NOTE: Routine does not set up the implicit task ICVS. This is assumed to
1325// have already been done elsewhere.
1326// TODO: Get better loc_ref. Value passed in may be NULL
1328 kmp_team_t *team, int tid, int set_curr_task) {
1329 kmp_taskdata_t *task = &team->t.t_implicit_task_taskdata[tid];
1330
1331 KF_TRACE(
1332 10,
1333 ("__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n",
1334 tid, team, task, set_curr_task ? "TRUE" : "FALSE"));
1335
1336 task->td_task_id = KMP_GEN_TASK_ID();
1337 task->td_team = team;
1338 // task->td_parent = NULL; // fix for CQ230101 (broken parent task info
1339 // in debugger)
1340 task->td_ident = loc_ref;
1341 task->td_taskwait_ident = NULL;
1342 task->td_taskwait_counter = 0;
1343 task->td_taskwait_thread = 0;
1344
1345 task->td_flags.tiedness = TASK_TIED;
1346 task->td_flags.tasktype = TASK_IMPLICIT;
1347 task->td_flags.proxy = TASK_FULL;
1348
1349 // All implicit tasks are executed immediately, not deferred
1350 task->td_flags.task_serial = 1;
1351 task->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
1352 task->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1353
1354 task->td_flags.started = 1;
1355 task->td_flags.executing = 1;
1356 task->td_flags.complete = 0;
1357 task->td_flags.freed = 0;
1358#if OMPX_TASKGRAPH
1359 task->td_flags.onced = 0;
1360#endif
1361
1362 task->td_depnode = NULL;
1363 task->td_last_tied = task;
1364 task->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1365
1366 if (set_curr_task) { // only do this init first time thread is created
1367 KMP_ATOMIC_ST_REL(&task->td_incomplete_child_tasks, 0);
1368 // Not used: don't need to deallocate implicit task
1369 KMP_ATOMIC_ST_REL(&task->td_allocated_child_tasks, 0);
1370 task->td_taskgroup = NULL; // An implicit task does not have taskgroup
1371 task->td_dephash = NULL;
1372 __kmp_push_current_task_to_thread(this_thr, team, tid);
1373 } else {
1374 KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0);
1375 KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0);
1376 }
1377
1378#if OMPT_SUPPORT
1380 __ompt_task_init(task, tid);
1381#endif
1382
1383 KF_TRACE(10, ("__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n", tid,
1384 team, task));
1385}
1386
1387// __kmp_finish_implicit_task: Release resources associated to implicit tasks
1388// at the end of parallel regions. Some resources are kept for reuse in the next
1389// parallel region.
1390//
1391// thread: thread data structure corresponding to implicit task
1393 kmp_taskdata_t *task = thread->th.th_current_task;
1394 if (task->td_dephash) {
1395 int children;
1396 task->td_flags.complete = 1;
1397#if OMPX_TASKGRAPH
1398 task->td_flags.onced = 1;
1399#endif
1400 children = KMP_ATOMIC_LD_ACQ(&task->td_incomplete_child_tasks);
1401 kmp_tasking_flags_t flags_old = task->td_flags;
1402 if (children == 0 && flags_old.complete == 1) {
1403 kmp_tasking_flags_t flags_new = flags_old;
1404 flags_new.complete = 0;
1406 *RCAST(kmp_int32 *, &flags_old),
1407 *RCAST(kmp_int32 *, &flags_new))) {
1408 KA_TRACE(100, ("__kmp_finish_implicit_task: T#%d cleans "
1409 "dephash of implicit task %p\n",
1410 thread->th.th_info.ds.ds_gtid, task));
1411 __kmp_dephash_free_entries(thread, task->td_dephash);
1412 }
1413 }
1414 }
1415}
1416
1417// __kmp_free_implicit_task: Release resources associated to implicit tasks
1418// when these are destroyed regions
1419//
1420// thread: thread data structure corresponding to implicit task
1422 kmp_taskdata_t *task = thread->th.th_current_task;
1423 if (task && task->td_dephash) {
1424 __kmp_dephash_free(thread, task->td_dephash);
1425 task->td_dephash = NULL;
1426 }
1427}
1428
1429// Round up a size to a power of two specified by val: Used to insert padding
1430// between structures co-allocated using a single malloc() call
1431static size_t __kmp_round_up_to_val(size_t size, size_t val) {
1432 if (size & (val - 1)) {
1433 size &= ~(val - 1);
1434 if (size <= KMP_SIZE_T_MAX - val) {
1435 size += val; // Round up if there is no overflow.
1436 }
1437 }
1438 return size;
1439} // __kmp_round_up_to_va
1440
1441// __kmp_task_alloc: Allocate the taskdata and task data structures for a task
1442//
1443// loc_ref: source location information
1444// gtid: global thread number.
1445// flags: include tiedness & task type (explicit vs. implicit) of the ''new''
1446// task encountered. Converted from kmp_int32 to kmp_tasking_flags_t in routine.
1447// sizeof_kmp_task_t: Size in bytes of kmp_task_t data structure including
1448// private vars accessed in task.
1449// sizeof_shareds: Size in bytes of array of pointers to shared vars accessed
1450// in task.
1451// task_entry: Pointer to task code entry point generated by compiler.
1452// returns: a pointer to the allocated kmp_task_t structure (task).
1454 kmp_tasking_flags_t *flags,
1455 size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1458 kmp_taskdata_t *taskdata;
1459 kmp_info_t *thread = __kmp_threads[gtid];
1460 kmp_team_t *team = thread->th.th_team;
1461 kmp_taskdata_t *parent_task = thread->th.th_current_task;
1462 size_t shareds_offset;
1463
1466
1467 if (flags->hidden_helper) {
1471 } else {
1472 // If the hidden helper task is not enabled, reset the flag to FALSE.
1473 flags->hidden_helper = FALSE;
1474 }
1475 }
1476
1477 KA_TRACE(10, ("__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) "
1478 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1479 gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t,
1480 sizeof_shareds, task_entry));
1481
1482 KMP_DEBUG_ASSERT(parent_task);
1483 if (parent_task->td_flags.final) {
1484 if (flags->merged_if0) {
1485 }
1486 flags->final = 1;
1487 }
1488
1489 if (flags->tiedness == TASK_UNTIED && !team->t.t_serialized) {
1490 // Untied task encountered causes the TSC algorithm to check entire deque of
1491 // the victim thread. If no untied task encountered, then checking the head
1492 // of the deque should be enough.
1493 KMP_CHECK_UPDATE(thread->th.th_task_team->tt.tt_untied_task_encountered, 1);
1494 }
1495
1496 // Detachable tasks are not proxy tasks yet but could be in the future. Doing
1497 // the tasking setup
1498 // when that happens is too late.
1499 if (UNLIKELY(flags->proxy == TASK_PROXY ||
1500 flags->detachable == TASK_DETACHABLE || flags->hidden_helper)) {
1501 if (flags->proxy == TASK_PROXY) {
1502 flags->tiedness = TASK_UNTIED;
1503 flags->merged_if0 = 1;
1504 }
1505 /* are we running in a sequential parallel or tskm_immediate_exec... we need
1506 tasking support enabled */
1507 if ((thread->th.th_task_team) == NULL) {
1508 /* This should only happen if the team is serialized
1509 setup a task team and propagate it to the thread */
1510 KMP_DEBUG_ASSERT(team->t.t_serialized);
1511 KA_TRACE(30,
1512 ("T#%d creating task team in __kmp_task_alloc for proxy task\n",
1513 gtid));
1514 __kmp_task_team_setup(thread, team);
1515 thread->th.th_task_team = team->t.t_task_team[thread->th.th_task_state];
1516 }
1517 kmp_task_team_t *task_team = thread->th.th_task_team;
1518
1519 /* tasking must be enabled now as the task might not be pushed */
1520 if (!KMP_TASKING_ENABLED(task_team)) {
1521 KA_TRACE(
1522 30,
1523 ("T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid));
1524 __kmp_enable_tasking(task_team, thread);
1525 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
1526 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
1527 // No lock needed since only owner can allocate
1528 if (thread_data->td.td_deque == NULL) {
1529 __kmp_alloc_task_deque(thread, thread_data);
1530 }
1531 }
1532
1533 if ((flags->proxy == TASK_PROXY || flags->detachable == TASK_DETACHABLE) &&
1534 task_team->tt.tt_found_proxy_tasks == FALSE)
1535 TCW_4(task_team->tt.tt_found_proxy_tasks, TRUE);
1536 if (flags->hidden_helper &&
1539 }
1540
1541 // Calculate shared structure offset including padding after kmp_task_t struct
1542 // to align pointers in shared struct
1543 shareds_offset = sizeof(kmp_taskdata_t) + sizeof_kmp_task_t;
1544 shareds_offset = __kmp_round_up_to_val(shareds_offset, sizeof(void *));
1545
1546 // Allocate a kmp_taskdata_t block and a kmp_task_t block.
1547 KA_TRACE(30, ("__kmp_task_alloc: T#%d First malloc size: %ld\n", gtid,
1548 shareds_offset));
1549 KA_TRACE(30, ("__kmp_task_alloc: T#%d Second malloc size: %ld\n", gtid,
1550 sizeof_shareds));
1551
1552 // Avoid double allocation here by combining shareds with taskdata
1553#if USE_FAST_MEMORY
1554 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, shareds_offset +
1555 sizeof_shareds);
1556#else /* ! USE_FAST_MEMORY */
1557 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, shareds_offset +
1558 sizeof_shareds);
1559#endif /* USE_FAST_MEMORY */
1560
1561 task = KMP_TASKDATA_TO_TASK(taskdata);
1562
1563// Make sure task & taskdata are aligned appropriately
1564#if KMP_ARCH_X86 || KMP_ARCH_PPC64 || KMP_ARCH_S390X || !KMP_HAVE_QUAD
1565 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (sizeof(double) - 1)) == 0);
1566 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (sizeof(double) - 1)) == 0);
1567#else
1568 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (sizeof(_Quad) - 1)) == 0);
1569 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (sizeof(_Quad) - 1)) == 0);
1570#endif
1571 if (sizeof_shareds > 0) {
1572 // Avoid double allocation here by combining shareds with taskdata
1573 task->shareds = &((char *)taskdata)[shareds_offset];
1574 // Make sure shareds struct is aligned to pointer size
1575 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (sizeof(void *) - 1)) ==
1576 0);
1577 } else {
1578 task->shareds = NULL;
1579 }
1581 task->part_id = 0; // AC: Always start with 0 part id
1582
1583 taskdata->td_task_id = KMP_GEN_TASK_ID();
1584 taskdata->td_team = thread->th.th_team;
1585 taskdata->td_alloc_thread = thread;
1586 taskdata->td_parent = parent_task;
1587 taskdata->td_level = parent_task->td_level + 1; // increment nesting level
1588 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
1589 taskdata->td_ident = loc_ref;
1590 taskdata->td_taskwait_ident = NULL;
1591 taskdata->td_taskwait_counter = 0;
1592 taskdata->td_taskwait_thread = 0;
1593 KMP_DEBUG_ASSERT(taskdata->td_parent != NULL);
1594 // avoid copying icvs for proxy tasks
1595 if (flags->proxy == TASK_FULL)
1596 copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs);
1597
1598 taskdata->td_flags = *flags;
1599 taskdata->td_task_team = thread->th.th_task_team;
1600 taskdata->td_size_alloc = shareds_offset + sizeof_shareds;
1601 taskdata->td_flags.tasktype = TASK_EXPLICIT;
1602 // If it is hidden helper task, we need to set the team and task team
1603 // correspondingly.
1604 if (flags->hidden_helper) {
1605 kmp_info_t *shadow_thread = __kmp_threads[KMP_GTID_TO_SHADOW_GTID(gtid)];
1606 taskdata->td_team = shadow_thread->th.th_team;
1607 taskdata->td_task_team = shadow_thread->th.th_task_team;
1608 }
1609
1610 // GEH - TODO: fix this to copy parent task's value of tasking_ser flag
1612
1613 // GEH - TODO: fix this to copy parent task's value of team_serial flag
1614 taskdata->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
1615
1616 // GEH - Note we serialize the task if the team is serialized to make sure
1617 // implicit parallel region tasks are not left until program termination to
1618 // execute. Also, it helps locality to execute immediately.
1619
1620 taskdata->td_flags.task_serial =
1621 (parent_task->td_flags.final || taskdata->td_flags.team_serial ||
1622 taskdata->td_flags.tasking_ser || flags->merged_if0);
1623
1624 taskdata->td_flags.started = 0;
1625 taskdata->td_flags.executing = 0;
1626 taskdata->td_flags.complete = 0;
1627 taskdata->td_flags.freed = 0;
1628#if OMPX_TASKGRAPH
1629 taskdata->td_flags.onced = 0;
1630#endif
1632 // start at one because counts current task and children
1634 taskdata->td_taskgroup =
1635 parent_task->td_taskgroup; // task inherits taskgroup from the parent task
1636 taskdata->td_dephash = NULL;
1637 taskdata->td_depnode = NULL;
1638 taskdata->td_target_data.async_handle = NULL;
1639 if (flags->tiedness == TASK_UNTIED)
1640 taskdata->td_last_tied = NULL; // will be set when the task is scheduled
1641 else
1642 taskdata->td_last_tied = taskdata;
1644#if OMPT_SUPPORT
1646 __ompt_task_init(taskdata, gtid);
1647#endif
1648 // TODO: What would be the balance between the conditions in the function and
1649 // an atomic operation?
1650 if (__kmp_track_children_task(taskdata)) {
1652 if (parent_task->td_taskgroup)
1653 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
1654 // Only need to keep track of allocated child tasks for explicit tasks since
1655 // implicit not deallocated
1656 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) {
1658 }
1659 if (flags->hidden_helper) {
1660 taskdata->td_flags.task_serial = FALSE;
1661 // Increment the number of hidden helper tasks to be executed
1663 }
1664 }
1665
1666#if OMPX_TASKGRAPH
1667 kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_idx);
1668 if (tdg && __kmp_tdg_is_recording(tdg->tdg_status) &&
1670 taskdata->is_taskgraph = 1;
1671 taskdata->tdg = __kmp_global_tdgs[__kmp_curr_tdg_idx];
1672 taskdata->td_task_id = KMP_ATOMIC_INC(&__kmp_tdg_task_id);
1673 }
1674#endif
1675 KA_TRACE(20, ("__kmp_task_alloc(exit): T#%d created task %p parent=%p\n",
1676 gtid, taskdata, taskdata->td_parent));
1677
1678 return task;
1679}
1680
1682 kmp_int32 flags, size_t sizeof_kmp_task_t,
1683 size_t sizeof_shareds,
1685 kmp_task_t *retval;
1686 kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
1688 input_flags->native = FALSE;
1689 // __kmp_task_alloc() sets up all other runtime flags
1690 KA_TRACE(10, ("__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s %s) "
1691 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1692 gtid, loc_ref, input_flags->tiedness ? "tied " : "untied",
1693 input_flags->proxy ? "proxy" : "",
1694 input_flags->detachable ? "detachable" : "", sizeof_kmp_task_t,
1695 sizeof_shareds, task_entry));
1696
1697 retval = __kmp_task_alloc(loc_ref, gtid, input_flags, sizeof_kmp_task_t,
1698 sizeof_shareds, task_entry);
1699
1700 KA_TRACE(20, ("__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval));
1701
1702 return retval;
1703}
1704
1706 kmp_int32 flags,
1707 size_t sizeof_kmp_task_t,
1708 size_t sizeof_shareds,
1710 kmp_int64 device_id) {
1711 auto &input_flags = reinterpret_cast<kmp_tasking_flags_t &>(flags);
1712 // target task is untied defined in the specification
1713 input_flags.tiedness = TASK_UNTIED;
1714
1716 input_flags.hidden_helper = TRUE;
1717
1718 return __kmpc_omp_task_alloc(loc_ref, gtid, flags, sizeof_kmp_task_t,
1719 sizeof_shareds, task_entry);
1720}
1721
1722/*!
1723@ingroup TASKING
1724@param loc_ref location of the original task directive
1725@param gtid Global Thread ID of encountering thread
1726@param new_task task thunk allocated by __kmpc_omp_task_alloc() for the ''new
1727task''
1728@param naffins Number of affinity items
1729@param affin_list List of affinity items
1730@return Returns non-zero if registering affinity information was not successful.
1731 Returns 0 if registration was successful
1732This entry registers the affinity information attached to a task with the task
1733thunk structure kmp_taskdata_t.
1734*/
1737 kmp_task_t *new_task, kmp_int32 naffins,
1738 kmp_task_affinity_info_t *affin_list) {
1739 return 0;
1740}
1741
1742// __kmp_invoke_task: invoke the specified task
1743//
1744// gtid: global thread ID of caller
1745// task: the task to invoke
1746// current_task: the task to resume after task invocation
1747#ifdef __s390x__
1748__attribute__((target("backchain")))
1749#endif
1750static void
1752 kmp_taskdata_t *current_task) {
1754 kmp_info_t *thread;
1755 int discard = 0 /* false */;
1756 KA_TRACE(
1757 30, ("__kmp_invoke_task(enter): T#%d invoking task %p, current_task=%p\n",
1758 gtid, taskdata, current_task));
1760 if (UNLIKELY(taskdata->td_flags.proxy == TASK_PROXY &&
1761 taskdata->td_flags.complete == 1)) {
1762 // This is a proxy task that was already completed but it needs to run
1763 // its bottom-half finish
1764 KA_TRACE(
1765 30,
1766 ("__kmp_invoke_task: T#%d running bottom finish for proxy task %p\n",
1767 gtid, taskdata));
1768
1770
1771 KA_TRACE(30, ("__kmp_invoke_task(exit): T#%d completed bottom finish for "
1772 "proxy task %p, resuming task %p\n",
1773 gtid, taskdata, current_task));
1774
1775 return;
1776 }
1777
1778#if OMPT_SUPPORT
1779 // For untied tasks, the first task executed only calls __kmpc_omp_task and
1780 // does not execute code.
1781 ompt_thread_info_t oldInfo;
1783 // Store the threads states and restore them after the task
1784 thread = __kmp_threads[gtid];
1785 oldInfo = thread->th.ompt_thread_info;
1786 thread->th.ompt_thread_info.wait_id = 0;
1787 thread->th.ompt_thread_info.state = (thread->th.th_team_serialized)
1788 ? ompt_state_work_serial
1789 : ompt_state_work_parallel;
1790 taskdata->ompt_task_info.frame.exit_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1791 }
1792#endif
1793
1794 // Proxy tasks are not handled by the runtime
1795 if (taskdata->td_flags.proxy != TASK_PROXY) {
1796 __kmp_task_start(gtid, task, current_task); // OMPT only if not discarded
1797 }
1798
1799 // TODO: cancel tasks if the parallel region has also been cancelled
1800 // TODO: check if this sequence can be hoisted above __kmp_task_start
1801 // if cancellation has been enabled for this run ...
1803 thread = __kmp_threads[gtid];
1804 kmp_team_t *this_team = thread->th.th_team;
1805 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
1806 if ((taskgroup && taskgroup->cancel_request) ||
1807 (this_team->t.t_cancel_request == cancel_parallel)) {
1808#if OMPT_SUPPORT && OMPT_OPTIONAL
1809 ompt_data_t *task_data;
1810 if (UNLIKELY(ompt_enabled.ompt_callback_cancel)) {
1811 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, NULL, NULL);
1812 ompt_callbacks.ompt_callback(ompt_callback_cancel)(
1813 task_data,
1814 ((taskgroup && taskgroup->cancel_request) ? ompt_cancel_taskgroup
1815 : ompt_cancel_parallel) |
1816 ompt_cancel_discarded_task,
1817 NULL);
1818 }
1819#endif
1820 KMP_COUNT_BLOCK(TASK_cancelled);
1821 // this task belongs to a task group and we need to cancel it
1822 discard = 1 /* true */;
1823 }
1824 }
1825
1826 // Invoke the task routine and pass in relevant data.
1827 // Thunks generated by gcc take a different argument list.
1828 if (!discard) {
1829 if (taskdata->td_flags.tiedness == TASK_UNTIED) {
1830 taskdata->td_last_tied = current_task->td_last_tied;
1831 KMP_DEBUG_ASSERT(taskdata->td_last_tied);
1832 }
1833#if KMP_STATS_ENABLED
1834 KMP_COUNT_BLOCK(TASK_executed);
1835 switch (KMP_GET_THREAD_STATE()) {
1836 case FORK_JOIN_BARRIER:
1837 KMP_PUSH_PARTITIONED_TIMER(OMP_task_join_bar);
1838 break;
1839 case PLAIN_BARRIER:
1840 KMP_PUSH_PARTITIONED_TIMER(OMP_task_plain_bar);
1841 break;
1842 case TASKYIELD:
1843 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskyield);
1844 break;
1845 case TASKWAIT:
1846 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskwait);
1847 break;
1848 case TASKGROUP:
1849 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskgroup);
1850 break;
1851 default:
1852 KMP_PUSH_PARTITIONED_TIMER(OMP_task_immediate);
1853 break;
1854 }
1855#endif // KMP_STATS_ENABLED
1856
1857// OMPT task begin
1858#if OMPT_SUPPORT
1860 __ompt_task_start(task, current_task, gtid);
1861#endif
1862#if OMPT_SUPPORT && OMPT_OPTIONAL
1863 if (UNLIKELY(ompt_enabled.ompt_callback_dispatch &&
1864 taskdata->ompt_task_info.dispatch_chunk.iterations > 0)) {
1865 ompt_data_t instance = ompt_data_none;
1866 instance.ptr = &(taskdata->ompt_task_info.dispatch_chunk);
1867 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
1868 ompt_callbacks.ompt_callback(ompt_callback_dispatch)(
1869 &(team_info->parallel_data), &(taskdata->ompt_task_info.task_data),
1870 ompt_dispatch_taskloop_chunk, instance);
1871 taskdata->ompt_task_info.dispatch_chunk = {0, 0};
1872 }
1873#endif // OMPT_SUPPORT && OMPT_OPTIONAL
1874
1875#if OMPD_SUPPORT
1876 if (ompd_state & OMPD_ENABLE_BP)
1877 ompd_bp_task_begin();
1878#endif
1879
1880#if USE_ITT_BUILD && USE_ITT_NOTIFY
1881 kmp_uint64 cur_time;
1882 kmp_int32 kmp_itt_count_task =
1883 __kmp_forkjoin_frames_mode == 3 && !taskdata->td_flags.task_serial &&
1884 current_task->td_flags.tasktype == TASK_IMPLICIT;
1885 if (kmp_itt_count_task) {
1886 thread = __kmp_threads[gtid];
1887 // Time outer level explicit task on barrier for adjusting imbalance time
1888 if (thread->th.th_bar_arrive_time)
1889 cur_time = __itt_get_timestamp();
1890 else
1891 kmp_itt_count_task = 0; // thread is not on a barrier - skip timing
1892 }
1893 KMP_FSYNC_ACQUIRED(taskdata); // acquired self (new task)
1894#endif
1895
1896#if ENABLE_LIBOMPTARGET
1897 if (taskdata->td_target_data.async_handle != NULL) {
1898 // If we have a valid target async handle, that means that we have already
1899 // executed the task routine once. We must query for the handle completion
1900 // instead of re-executing the routine.
1901 KMP_ASSERT(tgt_target_nowait_query);
1902 tgt_target_nowait_query(&taskdata->td_target_data.async_handle);
1903 } else
1904#endif
1905 if (task->routine != NULL) {
1906#ifdef KMP_GOMP_COMPAT
1907 if (taskdata->td_flags.native) {
1908 ((void (*)(void *))(*(task->routine)))(task->shareds);
1909 } else
1910#endif /* KMP_GOMP_COMPAT */
1911 {
1912 (*(task->routine))(gtid, task);
1913 }
1914 }
1916
1917#if USE_ITT_BUILD && USE_ITT_NOTIFY
1918 if (kmp_itt_count_task) {
1919 // Barrier imbalance - adjust arrive time with the task duration
1920 thread->th.th_bar_arrive_time += (__itt_get_timestamp() - cur_time);
1921 }
1922 KMP_FSYNC_CANCEL(taskdata); // destroy self (just executed)
1923 KMP_FSYNC_RELEASING(taskdata->td_parent); // releasing parent
1924#endif
1925 }
1926
1927#if OMPD_SUPPORT
1928 if (ompd_state & OMPD_ENABLE_BP)
1929 ompd_bp_task_end();
1930#endif
1931
1932 // Proxy tasks are not handled by the runtime
1933 if (taskdata->td_flags.proxy != TASK_PROXY) {
1934#if OMPT_SUPPORT
1936 thread->th.ompt_thread_info = oldInfo;
1937 if (taskdata->td_flags.tiedness == TASK_TIED) {
1938 taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
1939 }
1940 __kmp_task_finish<true>(gtid, task, current_task);
1941 } else
1942#endif
1943 __kmp_task_finish<false>(gtid, task, current_task);
1944 }
1945
1946 KA_TRACE(
1947 30,
1948 ("__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n",
1949 gtid, taskdata, current_task));
1950 return;
1951}
1952
1953// __kmpc_omp_task_parts: Schedule a thread-switchable task for execution
1954//
1955// loc_ref: location of original task pragma (ignored)
1956// gtid: Global Thread ID of encountering thread
1957// new_task: task thunk allocated by __kmp_omp_task_alloc() for the ''new task''
1958// Returns:
1959// TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to
1960// be resumed later.
1961// TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be
1962// resumed later.
1964 kmp_task_t *new_task) {
1965 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1966
1967 KA_TRACE(10, ("__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n", gtid,
1968 loc_ref, new_taskdata));
1969
1970#if OMPT_SUPPORT
1973 parent = new_taskdata->td_parent;
1974 if (ompt_enabled.ompt_callback_task_create) {
1975 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1976 &(parent->ompt_task_info.task_data), &(parent->ompt_task_info.frame),
1977 &(new_taskdata->ompt_task_info.task_data), ompt_task_explicit, 0,
1979 }
1980 }
1981#endif
1982
1983 /* Should we execute the new task or queue it? For now, let's just always try
1984 to queue it. If the queue fills up, then we'll execute it. */
1985
1986 if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED) // if cannot defer
1987 { // Execute this task immediately
1988 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
1989 new_taskdata->td_flags.task_serial = 1;
1990 __kmp_invoke_task(gtid, new_task, current_task);
1991 }
1992
1993 KA_TRACE(
1994 10,
1995 ("__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: "
1996 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
1997 gtid, loc_ref, new_taskdata));
1998
1999#if OMPT_SUPPORT
2001 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
2002 }
2003#endif
2005}
2006
2007// __kmp_omp_task: Schedule a non-thread-switchable task for execution
2008//
2009// gtid: Global Thread ID of encountering thread
2010// new_task:non-thread-switchable task thunk allocated by __kmp_omp_task_alloc()
2011// serialize_immediate: if TRUE then if the task is executed immediately its
2012// execution will be serialized
2013// Returns:
2014// TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to
2015// be resumed later.
2016// TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be
2017// resumed later.
2019 bool serialize_immediate) {
2020 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
2021
2022#if OMPX_TASKGRAPH
2023 if (new_taskdata->is_taskgraph &&
2024 __kmp_tdg_is_recording(new_taskdata->tdg->tdg_status)) {
2025 kmp_tdg_info_t *tdg = new_taskdata->tdg;
2026 // extend the record_map if needed
2027 if (new_taskdata->td_task_id >= new_taskdata->tdg->map_size) {
2028 __kmp_acquire_bootstrap_lock(&tdg->graph_lock);
2029 // map_size could have been updated by another thread if recursive
2030 // taskloop
2031 if (new_taskdata->td_task_id >= tdg->map_size) {
2032 kmp_uint old_size = tdg->map_size;
2033 kmp_uint new_size = old_size * 2;
2034 kmp_node_info_t *old_record = tdg->record_map;
2035 kmp_node_info_t *new_record = (kmp_node_info_t *)__kmp_allocate(
2036 new_size * sizeof(kmp_node_info_t));
2037
2038 KMP_MEMCPY(new_record, old_record, old_size * sizeof(kmp_node_info_t));
2039 tdg->record_map = new_record;
2040
2041 __kmp_free(old_record);
2042
2043 for (kmp_int i = old_size; i < new_size; i++) {
2044 kmp_int32 *successorsList = (kmp_int32 *)__kmp_allocate(
2045 __kmp_successors_size * sizeof(kmp_int32));
2046 new_record[i].task = nullptr;
2047 new_record[i].successors = successorsList;
2048 new_record[i].nsuccessors = 0;
2049 new_record[i].npredecessors = 0;
2050 new_record[i].successors_size = __kmp_successors_size;
2051 KMP_ATOMIC_ST_REL(&new_record[i].npredecessors_counter, 0);
2052 }
2053 // update the size at the end, so that we avoid other
2054 // threads use old_record while map_size is already updated
2055 tdg->map_size = new_size;
2056 }
2057 __kmp_release_bootstrap_lock(&tdg->graph_lock);
2058 }
2059 // record a task
2060 if (tdg->record_map[new_taskdata->td_task_id].task == nullptr) {
2061 tdg->record_map[new_taskdata->td_task_id].task = new_task;
2062 tdg->record_map[new_taskdata->td_task_id].parent_task =
2063 new_taskdata->td_parent;
2064 KMP_ATOMIC_INC(&tdg->num_tasks);
2065 }
2066 }
2067#endif
2068
2069 /* Should we execute the new task or queue it? For now, let's just always try
2070 to queue it. If the queue fills up, then we'll execute it. */
2071 if (new_taskdata->td_flags.proxy == TASK_PROXY ||
2072 __kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED) // if cannot defer
2073 { // Execute this task immediately
2074 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
2075 if (serialize_immediate)
2076 new_taskdata->td_flags.task_serial = 1;
2077 __kmp_invoke_task(gtid, new_task, current_task);
2080 kmp_info_t *this_thr = __kmp_threads[gtid];
2081 kmp_team_t *team = this_thr->th.th_team;
2082 kmp_int32 nthreads = this_thr->th.th_team_nproc;
2083 for (int i = 0; i < nthreads; ++i) {
2084 kmp_info_t *thread = team->t.t_threads[i];
2085 if (thread == this_thr)
2086 continue;
2087 if (thread->th.th_sleep_loc != NULL) {
2089 break; // awake one thread at a time
2090 }
2091 }
2092 }
2094}
2095
2096// __kmpc_omp_task: Wrapper around __kmp_omp_task to schedule a
2097// non-thread-switchable task from the parent thread only!
2098//
2099// loc_ref: location of original task pragma (ignored)
2100// gtid: Global Thread ID of encountering thread
2101// new_task: non-thread-switchable task thunk allocated by
2102// __kmp_omp_task_alloc()
2103// Returns:
2104// TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to
2105// be resumed later.
2106// TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be
2107// resumed later.
2109 kmp_task_t *new_task) {
2110 kmp_int32 res;
2111 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
2112
2113#if KMP_DEBUG || OMPT_SUPPORT
2114 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
2115#endif
2116 KA_TRACE(10, ("__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
2117 new_taskdata));
2119
2120#if OMPT_SUPPORT
2121 kmp_taskdata_t *parent = NULL;
2123 if (!new_taskdata->td_flags.started) {
2124 OMPT_STORE_RETURN_ADDRESS(gtid);
2125 parent = new_taskdata->td_parent;
2126 if (!parent->ompt_task_info.frame.enter_frame.ptr) {
2127 parent->ompt_task_info.frame.enter_frame.ptr =
2129 }
2130 if (ompt_enabled.ompt_callback_task_create) {
2131 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
2132 &(parent->ompt_task_info.task_data),
2133 &(parent->ompt_task_info.frame),
2134 &(new_taskdata->ompt_task_info.task_data),
2135 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
2136 OMPT_LOAD_RETURN_ADDRESS(gtid));
2137 }
2138 } else {
2139 // We are scheduling the continuation of an UNTIED task.
2140 // Scheduling back to the parent task.
2141 __ompt_task_finish(new_task,
2142 new_taskdata->ompt_task_info.scheduling_parent,
2143 ompt_task_switch);
2144 new_taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
2145 }
2146 }
2147#endif
2148
2149 res = __kmp_omp_task(gtid, new_task, true);
2150
2151 KA_TRACE(10, ("__kmpc_omp_task(exit): T#%d returning "
2152 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
2153 gtid, loc_ref, new_taskdata));
2154#if OMPT_SUPPORT
2155 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
2156 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
2157 }
2158#endif
2159 return res;
2160}
2161
2162// __kmp_omp_taskloop_task: Wrapper around __kmp_omp_task to schedule
2163// a taskloop task with the correct OMPT return address
2164//
2165// loc_ref: location of original task pragma (ignored)
2166// gtid: Global Thread ID of encountering thread
2167// new_task: non-thread-switchable task thunk allocated by
2168// __kmp_omp_task_alloc()
2169// codeptr_ra: return address for OMPT callback
2170// Returns:
2171// TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to
2172// be resumed later.
2173// TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be
2174// resumed later.
2176 kmp_task_t *new_task, void *codeptr_ra) {
2177 kmp_int32 res;
2178 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
2179
2180#if KMP_DEBUG || OMPT_SUPPORT
2181 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
2182#endif
2183 KA_TRACE(10, ("__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
2184 new_taskdata));
2185
2186#if OMPT_SUPPORT
2187 kmp_taskdata_t *parent = NULL;
2188 if (UNLIKELY(ompt_enabled.enabled && !new_taskdata->td_flags.started)) {
2189 parent = new_taskdata->td_parent;
2190 if (!parent->ompt_task_info.frame.enter_frame.ptr)
2191 parent->ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
2192 if (ompt_enabled.ompt_callback_task_create) {
2193 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
2194 &(parent->ompt_task_info.task_data), &(parent->ompt_task_info.frame),
2195 &(new_taskdata->ompt_task_info.task_data),
2196 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
2197 codeptr_ra);
2198 }
2199 }
2200#endif
2201
2202 res = __kmp_omp_task(gtid, new_task, true);
2203
2204 KA_TRACE(10, ("__kmpc_omp_task(exit): T#%d returning "
2205 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
2206 gtid, loc_ref, new_taskdata));
2207#if OMPT_SUPPORT
2208 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
2209 parent->ompt_task_info.frame.enter_frame = ompt_data_none;
2210 }
2211#endif
2212 return res;
2213}
2214
2215template <bool ompt>
2217 void *frame_address,
2218 void *return_address) {
2219 kmp_taskdata_t *taskdata = nullptr;
2220 kmp_info_t *thread;
2221 int thread_finished = FALSE;
2223
2224 KA_TRACE(10, ("__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref));
2225 KMP_DEBUG_ASSERT(gtid >= 0);
2226
2228 thread = __kmp_threads[gtid];
2229 taskdata = thread->th.th_current_task;
2230
2231#if OMPT_SUPPORT && OMPT_OPTIONAL
2232 ompt_data_t *my_task_data;
2233 ompt_data_t *my_parallel_data;
2234
2235 if (ompt) {
2236 my_task_data = &(taskdata->ompt_task_info.task_data);
2237 my_parallel_data = OMPT_CUR_TEAM_DATA(thread);
2238
2239 taskdata->ompt_task_info.frame.enter_frame.ptr = frame_address;
2240
2241 if (ompt_enabled.ompt_callback_sync_region) {
2242 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2243 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
2244 my_task_data, return_address);
2245 }
2246
2247 if (ompt_enabled.ompt_callback_sync_region_wait) {
2248 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2249 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
2250 my_task_data, return_address);
2251 }
2252 }
2253#endif // OMPT_SUPPORT && OMPT_OPTIONAL
2254
2255// Debugger: The taskwait is active. Store location and thread encountered the
2256// taskwait.
2257#if USE_ITT_BUILD
2258// Note: These values are used by ITT events as well.
2259#endif /* USE_ITT_BUILD */
2260 taskdata->td_taskwait_counter += 1;
2261 taskdata->td_taskwait_ident = loc_ref;
2262 taskdata->td_taskwait_thread = gtid + 1;
2263
2264#if USE_ITT_BUILD
2265 void *itt_sync_obj = NULL;
2266#if USE_ITT_NOTIFY
2267 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2268#endif /* USE_ITT_NOTIFY */
2269#endif /* USE_ITT_BUILD */
2270
2271 bool must_wait =
2272 !taskdata->td_flags.team_serial && !taskdata->td_flags.final;
2273
2274 must_wait = must_wait || (thread->th.th_task_team != NULL &&
2275 thread->th.th_task_team->tt.tt_found_proxy_tasks);
2276 // If hidden helper thread is encountered, we must enable wait here.
2277 must_wait =
2278 must_wait ||
2279 (__kmp_enable_hidden_helper && thread->th.th_task_team != NULL &&
2280 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered);
2281
2282 if (must_wait) {
2284 RCAST(std::atomic<kmp_uint32> *,
2285 &(taskdata->td_incomplete_child_tasks)),
2286 0U);
2287 while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) != 0) {
2288 flag.execute_tasks(thread, gtid, FALSE,
2289 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2291 }
2292 }
2293#if USE_ITT_BUILD
2294 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2295 KMP_FSYNC_ACQUIRED(taskdata); // acquire self - sync with children
2296#endif /* USE_ITT_BUILD */
2297
2298 // Debugger: The taskwait is completed. Location remains, but thread is
2299 // negated.
2300 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2301
2302#if OMPT_SUPPORT && OMPT_OPTIONAL
2303 if (ompt) {
2304 if (ompt_enabled.ompt_callback_sync_region_wait) {
2305 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2306 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
2307 my_task_data, return_address);
2308 }
2309 if (ompt_enabled.ompt_callback_sync_region) {
2310 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2311 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
2312 my_task_data, return_address);
2313 }
2314 taskdata->ompt_task_info.frame.enter_frame = ompt_data_none;
2315 }
2316#endif // OMPT_SUPPORT && OMPT_OPTIONAL
2317 }
2318
2319 KA_TRACE(10, ("__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, "
2320 "returning TASK_CURRENT_NOT_QUEUED\n",
2321 gtid, taskdata));
2322
2324}
2325
2326#if OMPT_SUPPORT && OMPT_OPTIONAL
2328static kmp_int32 __kmpc_omp_taskwait_ompt(ident_t *loc_ref, kmp_int32 gtid,
2329 void *frame_address,
2330 void *return_address) {
2331 return __kmpc_omp_taskwait_template<true>(loc_ref, gtid, frame_address,
2332 return_address);
2333}
2334#endif // OMPT_SUPPORT && OMPT_OPTIONAL
2335
2336// __kmpc_omp_taskwait: Wait until all tasks generated by the current task are
2337// complete
2339#if OMPT_SUPPORT && OMPT_OPTIONAL
2341 OMPT_STORE_RETURN_ADDRESS(gtid);
2342 return __kmpc_omp_taskwait_ompt(loc_ref, gtid, OMPT_GET_FRAME_ADDRESS(0),
2343 OMPT_LOAD_RETURN_ADDRESS(gtid));
2344 }
2345#endif
2346 return __kmpc_omp_taskwait_template<false>(loc_ref, gtid, NULL, NULL);
2347}
2348
2349// __kmpc_omp_taskyield: switch to a different task
2350kmp_int32 __kmpc_omp_taskyield(ident_t *loc_ref, kmp_int32 gtid, int end_part) {
2351 kmp_taskdata_t *taskdata = NULL;
2352 kmp_info_t *thread;
2353 int thread_finished = FALSE;
2354
2355 KMP_COUNT_BLOCK(OMP_TASKYIELD);
2356 KMP_SET_THREAD_STATE_BLOCK(TASKYIELD);
2357
2358 KA_TRACE(10, ("__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n",
2359 gtid, loc_ref, end_part));
2361
2363 thread = __kmp_threads[gtid];
2364 taskdata = thread->th.th_current_task;
2365// Should we model this as a task wait or not?
2366// Debugger: The taskwait is active. Store location and thread encountered the
2367// taskwait.
2368#if USE_ITT_BUILD
2369// Note: These values are used by ITT events as well.
2370#endif /* USE_ITT_BUILD */
2371 taskdata->td_taskwait_counter += 1;
2372 taskdata->td_taskwait_ident = loc_ref;
2373 taskdata->td_taskwait_thread = gtid + 1;
2374
2375#if USE_ITT_BUILD
2376 void *itt_sync_obj = NULL;
2377#if USE_ITT_NOTIFY
2378 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2379#endif /* USE_ITT_NOTIFY */
2380#endif /* USE_ITT_BUILD */
2381 if (!taskdata->td_flags.team_serial) {
2382 kmp_task_team_t *task_team = thread->th.th_task_team;
2383 if (task_team != NULL) {
2384 if (KMP_TASKING_ENABLED(task_team)) {
2385#if OMPT_SUPPORT
2387 thread->th.ompt_thread_info.ompt_task_yielded = 1;
2388#endif
2390 thread, gtid, (kmp_flag_32<> *)NULL, FALSE,
2391 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2393#if OMPT_SUPPORT
2395 thread->th.ompt_thread_info.ompt_task_yielded = 0;
2396#endif
2397 }
2398 }
2399 }
2400#if USE_ITT_BUILD
2401 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2402#endif /* USE_ITT_BUILD */
2403
2404 // Debugger: The taskwait is completed. Location remains, but thread is
2405 // negated.
2406 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
2407 }
2408
2409 KA_TRACE(10, ("__kmpc_omp_taskyield(exit): T#%d task %p resuming, "
2410 "returning TASK_CURRENT_NOT_QUEUED\n",
2411 gtid, taskdata));
2412
2414}
2415
2416// Task Reduction implementation
2417//
2418// Note: initial implementation didn't take into account the possibility
2419// to specify omp_orig for initializer of the UDR (user defined reduction).
2420// Corrected implementation takes into account the omp_orig object.
2421// Compiler is free to use old implementation if omp_orig is not specified.
2422
2423/*!
2424@ingroup BASIC_TYPES
2425@{
2426*/
2427
2428/*!
2429Flags for special info per task reduction item.
2430*/
2431typedef struct kmp_taskred_flags {
2432 /*! 1 - use lazy alloc/init (e.g. big objects, num tasks < num threads) */
2433 unsigned lazy_priv : 1;
2434 unsigned reserved31 : 31;
2436
2437/*!
2438Internal struct for reduction data item related info set up by compiler.
2439*/
2440typedef struct kmp_task_red_input {
2441 void *reduce_shar; /**< shared between tasks item to reduce into */
2442 size_t reduce_size; /**< size of data item in bytes */
2443 // three compiler-generated routines (init, fini are optional):
2444 void *reduce_init; /**< data initialization routine (single parameter) */
2445 void *reduce_fini; /**< data finalization routine */
2446 void *reduce_comb; /**< data combiner routine */
2447 kmp_taskred_flags_t flags; /**< flags for additional info from compiler */
2449
2450/*!
2451Internal struct for reduction data item related info saved by the library.
2452*/
2453typedef struct kmp_taskred_data {
2454 void *reduce_shar; /**< shared between tasks item to reduce into */
2455 size_t reduce_size; /**< size of data item */
2456 kmp_taskred_flags_t flags; /**< flags for additional info from compiler */
2457 void *reduce_priv; /**< array of thread specific items */
2458 void *reduce_pend; /**< end of private data for faster comparison op */
2459 // three compiler-generated routines (init, fini are optional):
2460 void *reduce_comb; /**< data combiner routine */
2461 void *reduce_init; /**< data initialization routine (two parameters) */
2462 void *reduce_fini; /**< data finalization routine */
2463 void *reduce_orig; /**< original item (can be used in UDR initializer) */
2465
2466/*!
2467Internal struct for reduction data item related info set up by compiler.
2468
2469New interface: added reduce_orig field to provide omp_orig for UDR initializer.
2470*/
2471typedef struct kmp_taskred_input {
2472 void *reduce_shar; /**< shared between tasks item to reduce into */
2473 void *reduce_orig; /**< original reduction item used for initialization */
2474 size_t reduce_size; /**< size of data item */
2475 // three compiler-generated routines (init, fini are optional):
2476 void *reduce_init; /**< data initialization routine (two parameters) */
2477 void *reduce_fini; /**< data finalization routine */
2478 void *reduce_comb; /**< data combiner routine */
2479 kmp_taskred_flags_t flags; /**< flags for additional info from compiler */
2481/*!
2482@}
2483*/
2484
2485template <typename T> void __kmp_assign_orig(kmp_taskred_data_t &item, T &src);
2486template <>
2488 kmp_task_red_input_t &src) {
2489 item.reduce_orig = NULL;
2490}
2491template <>
2493 kmp_taskred_input_t &src) {
2494 if (src.reduce_orig != NULL) {
2495 item.reduce_orig = src.reduce_orig;
2496 } else {
2497 item.reduce_orig = src.reduce_shar;
2498 } // non-NULL reduce_orig means new interface used
2499}
2500
2501template <typename T> void __kmp_call_init(kmp_taskred_data_t &item, size_t j);
2502template <>
2504 size_t offset) {
2505 ((void (*)(void *))item.reduce_init)((char *)(item.reduce_priv) + offset);
2506}
2507template <>
2509 size_t offset) {
2510 ((void (*)(void *, void *))item.reduce_init)(
2511 (char *)(item.reduce_priv) + offset, item.reduce_orig);
2512}
2513
2514template <typename T>
2515void *__kmp_task_reduction_init(int gtid, int num, T *data) {
2517 kmp_info_t *thread = __kmp_threads[gtid];
2518 kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup;
2519 kmp_uint32 nth = thread->th.th_team_nproc;
2521
2522 // check input data just in case
2523 KMP_ASSERT(tg != NULL);
2524 KMP_ASSERT(data != NULL);
2525 KMP_ASSERT(num > 0);
2526 if (nth == 1 && !__kmp_enable_hidden_helper) {
2527 KA_TRACE(10, ("__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n",
2528 gtid, tg));
2529 return (void *)tg;
2530 }
2531 KA_TRACE(10, ("__kmpc_task_reduction_init: T#%d, taskgroup %p, #items %d\n",
2532 gtid, tg, num));
2534 thread, num * sizeof(kmp_taskred_data_t));
2535 for (int i = 0; i < num; ++i) {
2536 size_t size = data[i].reduce_size - 1;
2537 // round the size up to cache line per thread-specific item
2539 KMP_ASSERT(data[i].reduce_comb != NULL); // combiner is mandatory
2540 arr[i].reduce_shar = data[i].reduce_shar;
2541 arr[i].reduce_size = size;
2542 arr[i].flags = data[i].flags;
2543 arr[i].reduce_comb = data[i].reduce_comb;
2544 arr[i].reduce_init = data[i].reduce_init;
2545 arr[i].reduce_fini = data[i].reduce_fini;
2546 __kmp_assign_orig<T>(arr[i], data[i]);
2547 if (!arr[i].flags.lazy_priv) {
2548 // allocate cache-line aligned block and fill it with zeros
2549 arr[i].reduce_priv = __kmp_allocate(nth * size);
2550 arr[i].reduce_pend = (char *)(arr[i].reduce_priv) + nth * size;
2551 if (arr[i].reduce_init != NULL) {
2552 // initialize all thread-specific items
2553 for (size_t j = 0; j < nth; ++j) {
2554 __kmp_call_init<T>(arr[i], j * size);
2555 }
2556 }
2557 } else {
2558 // only allocate space for pointers now,
2559 // objects will be lazily allocated/initialized if/when requested
2560 // note that __kmp_allocate zeroes the allocated memory
2561 arr[i].reduce_priv = __kmp_allocate(nth * sizeof(void *));
2562 }
2563 }
2564 tg->reduce_data = (void *)arr;
2565 tg->reduce_num_data = num;
2566 return (void *)tg;
2567}
2568
2569/*!
2570@ingroup TASKING
2571@param gtid Global thread ID
2572@param num Number of data items to reduce
2573@param data Array of data for reduction
2574@return The taskgroup identifier
2575
2576Initialize task reduction for the taskgroup.
2577
2578Note: this entry supposes the optional compiler-generated initializer routine
2579has single parameter - pointer to object to be initialized. That means
2580the reduction either does not use omp_orig object, or the omp_orig is accessible
2581without help of the runtime library.
2582*/
2583void *__kmpc_task_reduction_init(int gtid, int num, void *data) {
2584#if OMPX_TASKGRAPH
2585 kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_idx);
2586 if (tdg && __kmp_tdg_is_recording(tdg->tdg_status)) {
2587 kmp_tdg_info_t *this_tdg = __kmp_global_tdgs[__kmp_curr_tdg_idx];
2588 this_tdg->rec_taskred_data =
2589 __kmp_allocate(sizeof(kmp_task_red_input_t) * num);
2590 this_tdg->rec_num_taskred = num;
2591 KMP_MEMCPY(this_tdg->rec_taskred_data, data,
2592 sizeof(kmp_task_red_input_t) * num);
2593 }
2594#endif
2596}
2597
2598/*!
2599@ingroup TASKING
2600@param gtid Global thread ID
2601@param num Number of data items to reduce
2602@param data Array of data for reduction
2603@return The taskgroup identifier
2604
2605Initialize task reduction for the taskgroup.
2606
2607Note: this entry supposes the optional compiler-generated initializer routine
2608has two parameters, pointer to object to be initialized and pointer to omp_orig
2609*/
2610void *__kmpc_taskred_init(int gtid, int num, void *data) {
2611#if OMPX_TASKGRAPH
2612 kmp_tdg_info_t *tdg = __kmp_find_tdg(__kmp_curr_tdg_idx);
2613 if (tdg && __kmp_tdg_is_recording(tdg->tdg_status)) {
2614 kmp_tdg_info_t *this_tdg = __kmp_global_tdgs[__kmp_curr_tdg_idx];
2615 this_tdg->rec_taskred_data =
2616 __kmp_allocate(sizeof(kmp_task_red_input_t) * num);
2617 this_tdg->rec_num_taskred = num;
2618 KMP_MEMCPY(this_tdg->rec_taskred_data, data,
2619 sizeof(kmp_task_red_input_t) * num);
2620 }
2621#endif
2623}
2624
2625// Copy task reduction data (except for shared pointers).
2626template <typename T>
2628 kmp_taskgroup_t *tg, void *reduce_data) {
2630 KA_TRACE(20, ("__kmp_task_reduction_init_copy: Th %p, init taskgroup %p,"
2631 " from data %p\n",
2632 thr, tg, reduce_data));
2634 thr, num * sizeof(kmp_taskred_data_t));
2635 // threads will share private copies, thunk routines, sizes, flags, etc.:
2636 KMP_MEMCPY(arr, reduce_data, num * sizeof(kmp_taskred_data_t));
2637 for (int i = 0; i < num; ++i) {
2638 arr[i].reduce_shar = data[i].reduce_shar; // init unique shared pointers
2639 }
2640 tg->reduce_data = (void *)arr;
2641 tg->reduce_num_data = num;
2642}
2643
2644/*!
2645@ingroup TASKING
2646@param gtid Global thread ID
2647@param tskgrp The taskgroup ID (optional)
2648@param data Shared location of the item
2649@return The pointer to per-thread data
2650
2651Get thread-specific location of data item
2652*/
2653void *__kmpc_task_reduction_get_th_data(int gtid, void *tskgrp, void *data) {
2655 kmp_info_t *thread = __kmp_threads[gtid];
2656 kmp_int32 nth = thread->th.th_team_nproc;
2657 if (nth == 1)
2658 return data; // nothing to do
2659
2660 kmp_taskgroup_t *tg = (kmp_taskgroup_t *)tskgrp;
2661 if (tg == NULL)
2662 tg = thread->th.th_current_task->td_taskgroup;
2663 KMP_ASSERT(tg != NULL);
2665 kmp_int32 num;
2666 kmp_int32 tid = thread->th.th_info.ds.ds_tid;
2667
2668#if OMPX_TASKGRAPH
2669 if ((thread->th.th_current_task->is_taskgraph) &&
2670 (!__kmp_tdg_is_recording(
2671 __kmp_global_tdgs[__kmp_curr_tdg_idx]->tdg_status))) {
2672 tg = thread->th.th_current_task->td_taskgroup;
2673 KMP_ASSERT(tg != NULL);
2674 KMP_ASSERT(tg->reduce_data != NULL);
2676 num = tg->reduce_num_data;
2677 }
2678#endif
2679
2680 KMP_ASSERT(data != NULL);
2681 while (tg != NULL) {
2683 num = tg->reduce_num_data;
2684 for (int i = 0; i < num; ++i) {
2685 if (!arr[i].flags.lazy_priv) {
2686 if (data == arr[i].reduce_shar ||
2687 (data >= arr[i].reduce_priv && data < arr[i].reduce_pend))
2688 return (char *)(arr[i].reduce_priv) + tid * arr[i].reduce_size;
2689 } else {
2690 // check shared location first
2691 void **p_priv = (void **)(arr[i].reduce_priv);
2692 if (data == arr[i].reduce_shar)
2693 goto found;
2694 // check if we get some thread specific location as parameter
2695 for (int j = 0; j < nth; ++j)
2696 if (data == p_priv[j])
2697 goto found;
2698 continue; // not found, continue search
2699 found:
2700 if (p_priv[tid] == NULL) {
2701 // allocate thread specific object lazily
2702 p_priv[tid] = __kmp_allocate(arr[i].reduce_size);
2703 if (arr[i].reduce_init != NULL) {
2704 if (arr[i].reduce_orig != NULL) { // new interface
2705 ((void (*)(void *, void *))arr[i].reduce_init)(
2706 p_priv[tid], arr[i].reduce_orig);
2707 } else { // old interface (single parameter)
2708 ((void (*)(void *))arr[i].reduce_init)(p_priv[tid]);
2709 }
2710 }
2711 }
2712 return p_priv[tid];
2713 }
2714 }
2715 KMP_ASSERT(tg->parent);
2716 tg = tg->parent;
2717 }
2718 KMP_ASSERT2(0, "Unknown task reduction item");
2719 return NULL; // ERROR, this line never executed
2720}
2721
2722// Finalize task reduction.
2723// Called from __kmpc_end_taskgroup()
2725 kmp_int32 nth = th->th.th_team_nproc;
2727 nth > 1 ||
2728 __kmp_enable_hidden_helper); // should not be called if nth == 1 unless we
2729 // are using hidden helper threads
2731 kmp_int32 num = tg->reduce_num_data;
2732 for (int i = 0; i < num; ++i) {
2733 void *sh_data = arr[i].reduce_shar;
2734 void (*f_fini)(void *) = (void (*)(void *))(arr[i].reduce_fini);
2735 void (*f_comb)(void *, void *) =
2736 (void (*)(void *, void *))(arr[i].reduce_comb);
2737 if (!arr[i].flags.lazy_priv) {
2738 void *pr_data = arr[i].reduce_priv;
2739 size_t size = arr[i].reduce_size;
2740 for (int j = 0; j < nth; ++j) {
2741 void *priv_data = (char *)pr_data + j * size;
2742 f_comb(sh_data, priv_data); // combine results
2743 if (f_fini)
2744 f_fini(priv_data); // finalize if needed
2745 }
2746 } else {
2747 void **pr_data = (void **)(arr[i].reduce_priv);
2748 for (int j = 0; j < nth; ++j) {
2749 if (pr_data[j] != NULL) {
2750 f_comb(sh_data, pr_data[j]); // combine results
2751 if (f_fini)
2752 f_fini(pr_data[j]); // finalize if needed
2753 __kmp_free(pr_data[j]);
2754 }
2755 }
2756 }
2757 __kmp_free(arr[i].reduce_priv);
2758 }
2760 tg->reduce_data = NULL;
2761 tg->reduce_num_data = 0;
2762}
2763
2764// Cleanup task reduction data for parallel or worksharing,
2765// do not touch task private data other threads still working with.
2766// Called from __kmpc_end_taskgroup()
2769 tg->reduce_data = NULL;
2770 tg->reduce_num_data = 0;
2771}
2772
2773template <typename T>
2775 int num, T *data) {
2777 kmp_info_t *thr = __kmp_threads[gtid];
2778 kmp_int32 nth = thr->th.th_team_nproc;
2779 __kmpc_taskgroup(loc, gtid); // form new taskgroup first
2780 if (nth == 1) {
2781 KA_TRACE(10,
2782 ("__kmpc_reduction_modifier_init: T#%d, tg %p, exiting nth=1\n",
2783 gtid, thr->th.th_current_task->td_taskgroup));
2784 return (void *)thr->th.th_current_task->td_taskgroup;
2785 }
2786 kmp_team_t *team = thr->th.th_team;
2787 void *reduce_data;
2788 kmp_taskgroup_t *tg;
2789 reduce_data = KMP_ATOMIC_LD_RLX(&team->t.t_tg_reduce_data[is_ws]);
2790 if (reduce_data == NULL &&
2791 __kmp_atomic_compare_store(&team->t.t_tg_reduce_data[is_ws], reduce_data,
2792 (void *)1)) {
2793 // single thread enters this block to initialize common reduction data
2794 KMP_DEBUG_ASSERT(reduce_data == NULL);
2795 // first initialize own data, then make a copy other threads can use
2796 tg = (kmp_taskgroup_t *)__kmp_task_reduction_init<T>(gtid, num, data);
2797 reduce_data = __kmp_thread_malloc(thr, num * sizeof(kmp_taskred_data_t));
2798 KMP_MEMCPY(reduce_data, tg->reduce_data, num * sizeof(kmp_taskred_data_t));
2799 // fini counters should be 0 at this point
2800 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[0]) == 0);
2801 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[1]) == 0);
2802 KMP_ATOMIC_ST_REL(&team->t.t_tg_reduce_data[is_ws], reduce_data);
2803 } else {
2804 while (
2805 (reduce_data = KMP_ATOMIC_LD_ACQ(&team->t.t_tg_reduce_data[is_ws])) ==
2806 (void *)1) { // wait for task reduction initialization
2807 KMP_CPU_PAUSE();
2808 }
2809 KMP_DEBUG_ASSERT(reduce_data > (void *)1); // should be valid pointer here
2810 tg = thr->th.th_current_task->td_taskgroup;
2811 __kmp_task_reduction_init_copy<T>(thr, num, data, tg, reduce_data);
2812 }
2813 return tg;
2814}
2815
2816/*!
2817@ingroup TASKING
2818@param loc Source location info
2819@param gtid Global thread ID
2820@param is_ws Is 1 if the reduction is for worksharing, 0 otherwise
2821@param num Number of data items to reduce
2822@param data Array of data for reduction
2823@return The taskgroup identifier
2824
2825Initialize task reduction for a parallel or worksharing.
2826
2827Note: this entry supposes the optional compiler-generated initializer routine
2828has single parameter - pointer to object to be initialized. That means
2829the reduction either does not use omp_orig object, or the omp_orig is accessible
2830without help of the runtime library.
2831*/
2833 int num, void *data) {
2834 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2836}
2837
2838/*!
2839@ingroup TASKING
2840@param loc Source location info
2841@param gtid Global thread ID
2842@param is_ws Is 1 if the reduction is for worksharing, 0 otherwise
2843@param num Number of data items to reduce
2844@param data Array of data for reduction
2845@return The taskgroup identifier
2846
2847Initialize task reduction for a parallel or worksharing.
2848
2849Note: this entry supposes the optional compiler-generated initializer routine
2850has two parameters, pointer to object to be initialized and pointer to omp_orig
2851*/
2852void *__kmpc_taskred_modifier_init(ident_t *loc, int gtid, int is_ws, int num,
2853 void *data) {
2854 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2856}
2857
2858/*!
2859@ingroup TASKING
2860@param loc Source location info
2861@param gtid Global thread ID
2862@param is_ws Is 1 if the reduction is for worksharing, 0 otherwise
2863
2864Finalize task reduction for a parallel or worksharing.
2865*/
2868}
2869
2870// __kmpc_taskgroup: Start a new taskgroup
2871void __kmpc_taskgroup(ident_t *loc, int gtid) {
2873 kmp_info_t *thread = __kmp_threads[gtid];
2874 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2875 kmp_taskgroup_t *tg_new =
2877 KA_TRACE(10, ("__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new));
2878 KMP_ATOMIC_ST_RLX(&tg_new->count, 0);
2880 tg_new->parent = taskdata->td_taskgroup;
2881 tg_new->reduce_data = NULL;
2882 tg_new->reduce_num_data = 0;
2883 tg_new->gomp_data = NULL;
2884 taskdata->td_taskgroup = tg_new;
2885
2886#if OMPT_SUPPORT && OMPT_OPTIONAL
2887 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
2888 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2889 if (!codeptr)
2890 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2891 kmp_team_t *team = thread->th.th_team;
2892 ompt_data_t my_task_data = taskdata->ompt_task_info.task_data;
2893 // FIXME: I think this is wrong for lwt!
2894 ompt_data_t my_parallel_data = team->t.ompt_team_info.parallel_data;
2895
2896 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
2897 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2898 &(my_task_data), codeptr);
2899 }
2900#endif
2901}
2902
2903// __kmpc_end_taskgroup: Wait until all tasks generated by the current task
2904// and its descendants are complete
2907 kmp_info_t *thread = __kmp_threads[gtid];
2908 kmp_taskdata_t *taskdata = thread->th.th_current_task;
2909 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
2910 int thread_finished = FALSE;
2911
2912#if OMPT_SUPPORT && OMPT_OPTIONAL
2913 kmp_team_t *team;
2914 ompt_data_t my_task_data;
2915 ompt_data_t my_parallel_data;
2916 void *codeptr = nullptr;
2918 team = thread->th.th_team;
2919 my_task_data = taskdata->ompt_task_info.task_data;
2920 // FIXME: I think this is wrong for lwt!
2921 my_parallel_data = team->t.ompt_team_info.parallel_data;
2922 codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2923 if (!codeptr)
2924 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2925 }
2926#endif
2927
2928 KA_TRACE(10, ("__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc));
2929 KMP_DEBUG_ASSERT(taskgroup != NULL);
2930 KMP_SET_THREAD_STATE_BLOCK(TASKGROUP);
2931
2933 // mark task as waiting not on a barrier
2934 taskdata->td_taskwait_counter += 1;
2935 taskdata->td_taskwait_ident = loc;
2936 taskdata->td_taskwait_thread = gtid + 1;
2937#if USE_ITT_BUILD
2938 // For ITT the taskgroup wait is similar to taskwait until we need to
2939 // distinguish them
2940 void *itt_sync_obj = NULL;
2941#if USE_ITT_NOTIFY
2942 KMP_ITT_TASKWAIT_STARTING(itt_sync_obj);
2943#endif /* USE_ITT_NOTIFY */
2944#endif /* USE_ITT_BUILD */
2945
2946#if OMPT_SUPPORT && OMPT_OPTIONAL
2947 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2948 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2949 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
2950 &(my_task_data), codeptr);
2951 }
2952#endif
2953
2954 if (!taskdata->td_flags.team_serial ||
2955 (thread->th.th_task_team != NULL &&
2956 (thread->th.th_task_team->tt.tt_found_proxy_tasks ||
2957 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered))) {
2959 RCAST(std::atomic<kmp_uint32> *, &(taskgroup->count)), 0U);
2960 while (KMP_ATOMIC_LD_ACQ(&taskgroup->count) != 0) {
2961 flag.execute_tasks(thread, gtid, FALSE,
2962 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
2964 }
2965 }
2966 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread; // end waiting
2967
2968#if OMPT_SUPPORT && OMPT_OPTIONAL
2969 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
2970 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
2971 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
2972 &(my_task_data), codeptr);
2973 }
2974#endif
2975
2976#if USE_ITT_BUILD
2977 KMP_ITT_TASKWAIT_FINISHED(itt_sync_obj);
2978 KMP_FSYNC_ACQUIRED(taskdata); // acquire self - sync with descendants
2979#endif /* USE_ITT_BUILD */
2980 }
2981 KMP_DEBUG_ASSERT(taskgroup->count == 0);
2982
2983 if (taskgroup->reduce_data != NULL &&
2984 !taskgroup->gomp_data) { // need to reduce?
2985 int cnt;
2986 void *reduce_data;
2987 kmp_team_t *t = thread->th.th_team;
2989 // check if <priv> data of the first reduction variable shared for the team
2990 void *priv0 = arr[0].reduce_priv;
2991 if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[0])) != NULL &&
2992 ((kmp_taskred_data_t *)reduce_data)[0].reduce_priv == priv0) {
2993 // finishing task reduction on parallel
2994 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[0]);
2995 if (cnt == thread->th.th_team_nproc - 1) {
2996 // we are the last thread passing __kmpc_reduction_modifier_fini()
2997 // finalize task reduction:
2998 __kmp_task_reduction_fini(thread, taskgroup);
2999 // cleanup fields in the team structure:
3000 // TODO: is relaxed store enough here (whole barrier should follow)?
3001 __kmp_thread_free(thread, reduce_data);
3002 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[0], NULL);
3003 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[0], 0);
3004 } else {
3005 // we are not the last thread passing __kmpc_reduction_modifier_fini(),
3006 // so do not finalize reduction, just clean own copy of the data
3007 __kmp_task_reduction_clean(thread, taskgroup);
3008 }
3009 } else if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[1])) !=
3010 NULL &&
3011 ((kmp_taskred_data_t *)reduce_data)[0].reduce_priv == priv0) {
3012 // finishing task reduction on worksharing
3013 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[1]);
3014 if (cnt == thread->th.th_team_nproc - 1) {
3015 // we are the last thread passing __kmpc_reduction_modifier_fini()
3016 __kmp_task_reduction_fini(thread, taskgroup);
3017 // cleanup fields in team structure:
3018 // TODO: is relaxed store enough here (whole barrier should follow)?
3019 __kmp_thread_free(thread, reduce_data);
3020 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[1], NULL);
3021 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[1], 0);
3022 } else {
3023 // we are not the last thread passing __kmpc_reduction_modifier_fini(),
3024 // so do not finalize reduction, just clean own copy of the data
3025 __kmp_task_reduction_clean(thread, taskgroup);
3026 }
3027 } else {
3028 // finishing task reduction on taskgroup
3029 __kmp_task_reduction_fini(thread, taskgroup);
3030 }
3031 }
3032 // Restore parent taskgroup for the current task
3033 taskdata->td_taskgroup = taskgroup->parent;
3034 __kmp_thread_free(thread, taskgroup);
3035
3036 KA_TRACE(10, ("__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n",
3037 gtid, taskdata));
3038
3039#if OMPT_SUPPORT && OMPT_OPTIONAL
3040 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
3041 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
3042 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
3043 &(my_task_data), codeptr);
3044 }
3045#endif
3046}
3047
3049 kmp_task_team_t *task_team,
3050 kmp_int32 is_constrained) {
3051 kmp_task_t *task = NULL;
3052 kmp_taskdata_t *taskdata;
3053 kmp_taskdata_t *current;
3054 kmp_thread_data_t *thread_data;
3055 int ntasks = task_team->tt.tt_num_task_pri;
3056 if (ntasks == 0) {
3057 KA_TRACE(
3058 20, ("__kmp_get_priority_task(exit #1): T#%d No tasks to get\n", gtid));
3059 return NULL;
3060 }
3061 do {
3062 // decrement num_tasks to "reserve" one task to get for execution
3063 if (__kmp_atomic_compare_store(&task_team->tt.tt_num_task_pri, ntasks,
3064 ntasks - 1))
3065 break;
3066 ntasks = task_team->tt.tt_num_task_pri;
3067 } while (ntasks > 0);
3068 if (ntasks == 0) {
3069 KA_TRACE(20, ("__kmp_get_priority_task(exit #2): T#%d No tasks to get\n",
3070 __kmp_get_gtid()));
3071 return NULL;
3072 }
3073 // We got a "ticket" to get a "reserved" priority task
3074 int deque_ntasks;
3075 kmp_task_pri_t *list = task_team->tt.tt_task_pri_list;
3076 do {
3077 KMP_ASSERT(list != NULL);
3078 thread_data = &list->td;
3079 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3080 deque_ntasks = thread_data->td.td_deque_ntasks;
3081 if (deque_ntasks == 0) {
3082 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3083 KA_TRACE(20, ("__kmp_get_priority_task: T#%d No tasks to get from %p\n",
3084 __kmp_get_gtid(), thread_data));
3085 list = list->next;
3086 }
3087 } while (deque_ntasks == 0);
3088 KMP_DEBUG_ASSERT(deque_ntasks);
3089 int target = thread_data->td.td_deque_head;
3090 current = __kmp_threads[gtid]->th.th_current_task;
3091 taskdata = thread_data->td.td_deque[target];
3092 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3093 // Bump head pointer and Wrap.
3094 thread_data->td.td_deque_head =
3095 (target + 1) & TASK_DEQUE_MASK(thread_data->td);
3096 } else {
3097 if (!task_team->tt.tt_untied_task_encountered) {
3098 // The TSC does not allow to steal victim task
3099 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3100 KA_TRACE(20, ("__kmp_get_priority_task(exit #3): T#%d could not get task "
3101 "from %p: task_team=%p ntasks=%d head=%u tail=%u\n",
3102 gtid, thread_data, task_team, deque_ntasks, target,
3103 thread_data->td.td_deque_tail));
3104 task_team->tt.tt_num_task_pri++; // atomic inc, restore value
3105 return NULL;
3106 }
3107 int i;
3108 // walk through the deque trying to steal any task
3109 taskdata = NULL;
3110 for (i = 1; i < deque_ntasks; ++i) {
3111 target = (target + 1) & TASK_DEQUE_MASK(thread_data->td);
3112 taskdata = thread_data->td.td_deque[target];
3113 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3114 break; // found task to execute
3115 } else {
3116 taskdata = NULL;
3117 }
3118 }
3119 if (taskdata == NULL) {
3120 // No appropriate candidate found to execute
3121 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3122 KA_TRACE(
3123 10, ("__kmp_get_priority_task(exit #4): T#%d could not get task from "
3124 "%p: task_team=%p ntasks=%d head=%u tail=%u\n",
3125 gtid, thread_data, task_team, deque_ntasks,
3126 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3127 task_team->tt.tt_num_task_pri++; // atomic inc, restore value
3128 return NULL;
3129 }
3130 int prev = target;
3131 for (i = i + 1; i < deque_ntasks; ++i) {
3132 // shift remaining tasks in the deque left by 1
3133 target = (target + 1) & TASK_DEQUE_MASK(thread_data->td);
3134 thread_data->td.td_deque[prev] = thread_data->td.td_deque[target];
3135 prev = target;
3136 }
3138 thread_data->td.td_deque_tail ==
3139 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(thread_data->td)));
3140 thread_data->td.td_deque_tail = target; // tail -= 1 (wrapped))
3141 }
3142 thread_data->td.td_deque_ntasks = deque_ntasks - 1;
3143 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3144 task = KMP_TASKDATA_TO_TASK(taskdata);
3145 return task;
3146}
3147
3148// __kmp_remove_my_task: remove a task from my own deque
3150 kmp_task_team_t *task_team,
3151 kmp_int32 is_constrained) {
3153 kmp_taskdata_t *taskdata;
3154 kmp_thread_data_t *thread_data;
3156
3158 KMP_DEBUG_ASSERT(task_team->tt.tt_threads_data !=
3159 NULL); // Caller should check this condition
3160
3161 thread_data = &task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
3162
3163 KA_TRACE(10, ("__kmp_remove_my_task(enter): T#%d ntasks=%d head=%u tail=%u\n",
3164 gtid, thread_data->td.td_deque_ntasks,
3165 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3166
3167 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
3168 KA_TRACE(10,
3169 ("__kmp_remove_my_task(exit #1): T#%d No tasks to remove: "
3170 "ntasks=%d head=%u tail=%u\n",
3171 gtid, thread_data->td.td_deque_ntasks,
3172 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3173 return NULL;
3174 }
3175
3176 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3177
3178 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
3179 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3180 KA_TRACE(10,
3181 ("__kmp_remove_my_task(exit #2): T#%d No tasks to remove: "
3182 "ntasks=%d head=%u tail=%u\n",
3183 gtid, thread_data->td.td_deque_ntasks,
3184 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3185 return NULL;
3186 }
3187
3188 tail = (thread_data->td.td_deque_tail - 1) &
3189 TASK_DEQUE_MASK(thread_data->td); // Wrap index.
3190 taskdata = thread_data->td.td_deque[tail];
3191
3192 if (!__kmp_task_is_allowed(gtid, is_constrained, taskdata,
3193 thread->th.th_current_task)) {
3194 // The TSC does not allow to steal victim task
3195 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3196 KA_TRACE(10,
3197 ("__kmp_remove_my_task(exit #3): T#%d TSC blocks tail task: "
3198 "ntasks=%d head=%u tail=%u\n",
3199 gtid, thread_data->td.td_deque_ntasks,
3200 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3201 return NULL;
3202 }
3203
3204 thread_data->td.td_deque_tail = tail;
3205 TCW_4(thread_data->td.td_deque_ntasks, thread_data->td.td_deque_ntasks - 1);
3206
3207 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3208
3209 KA_TRACE(10, ("__kmp_remove_my_task(exit #4): T#%d task %p removed: "
3210 "ntasks=%d head=%u tail=%u\n",
3211 gtid, taskdata, thread_data->td.td_deque_ntasks,
3212 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3213
3214 task = KMP_TASKDATA_TO_TASK(taskdata);
3215 return task;
3216}
3217
3218// __kmp_steal_task: remove a task from another thread's deque
3219// Assume that calling thread has already checked existence of
3220// task_team thread_data before calling this routine.
3222 kmp_task_team_t *task_team,
3223 std::atomic<kmp_int32> *unfinished_threads,
3224 int *thread_finished,
3225 kmp_int32 is_constrained) {
3227 kmp_taskdata_t *taskdata;
3228 kmp_taskdata_t *current;
3229 kmp_thread_data_t *victim_td, *threads_data;
3231 kmp_info_t *victim_thr;
3232
3234
3235 threads_data = task_team->tt.tt_threads_data;
3236 KMP_DEBUG_ASSERT(threads_data != NULL); // Caller should check this condition
3237 KMP_DEBUG_ASSERT(victim_tid >= 0);
3238 KMP_DEBUG_ASSERT(victim_tid < task_team->tt.tt_nproc);
3239
3240 victim_td = &threads_data[victim_tid];
3241 victim_thr = victim_td->td.td_thr;
3242 (void)victim_thr; // Use in TRACE messages which aren't always enabled.
3243
3244 KA_TRACE(10, ("__kmp_steal_task(enter): T#%d try to steal from T#%d: "
3245 "task_team=%p ntasks=%d head=%u tail=%u\n",
3246 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
3247 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
3248 victim_td->td.td_deque_tail));
3249
3250 if (TCR_4(victim_td->td.td_deque_ntasks) == 0) {
3251 KA_TRACE(10, ("__kmp_steal_task(exit #1): T#%d could not steal from T#%d: "
3252 "task_team=%p ntasks=%d head=%u tail=%u\n",
3253 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
3254 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
3255 victim_td->td.td_deque_tail));
3256 return NULL;
3257 }
3258
3259 __kmp_acquire_bootstrap_lock(&victim_td->td.td_deque_lock);
3260
3261 int ntasks = TCR_4(victim_td->td.td_deque_ntasks);
3262 // Check again after we acquire the lock
3263 if (ntasks == 0) {
3264 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3265 KA_TRACE(10, ("__kmp_steal_task(exit #2): T#%d could not steal from T#%d: "
3266 "task_team=%p ntasks=%d head=%u tail=%u\n",
3267 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3268 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3269 return NULL;
3270 }
3271
3272 KMP_DEBUG_ASSERT(victim_td->td.td_deque != NULL);
3273 current = __kmp_threads[gtid]->th.th_current_task;
3274 taskdata = victim_td->td.td_deque[victim_td->td.td_deque_head];
3275 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3276 // Bump head pointer and Wrap.
3277 victim_td->td.td_deque_head =
3278 (victim_td->td.td_deque_head + 1) & TASK_DEQUE_MASK(victim_td->td);
3279 } else {
3280 if (!task_team->tt.tt_untied_task_encountered) {
3281 // The TSC does not allow to steal victim task
3282 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3283 KA_TRACE(10, ("__kmp_steal_task(exit #3): T#%d could not steal from "
3284 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
3285 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3286 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3287 return NULL;
3288 }
3289 int i;
3290 // walk through victim's deque trying to steal any task
3291 target = victim_td->td.td_deque_head;
3292 taskdata = NULL;
3293 for (i = 1; i < ntasks; ++i) {
3294 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
3295 taskdata = victim_td->td.td_deque[target];
3296 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
3297 break; // found victim task
3298 } else {
3299 taskdata = NULL;
3300 }
3301 }
3302 if (taskdata == NULL) {
3303 // No appropriate candidate to steal found
3304 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3305 KA_TRACE(10, ("__kmp_steal_task(exit #4): T#%d could not steal from "
3306 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
3307 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
3308 victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3309 return NULL;
3310 }
3311 int prev = target;
3312 for (i = i + 1; i < ntasks; ++i) {
3313 // shift remaining tasks in the deque left by 1
3314 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
3315 victim_td->td.td_deque[prev] = victim_td->td.td_deque[target];
3316 prev = target;
3317 }
3319 victim_td->td.td_deque_tail ==
3320 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(victim_td->td)));
3321 victim_td->td.td_deque_tail = target; // tail -= 1 (wrapped))
3322 }
3323 if (*thread_finished) {
3324 // We need to un-mark this victim as a finished victim. This must be done
3325 // before releasing the lock, or else other threads (starting with the
3326 // primary thread victim) might be prematurely released from the barrier!!!
3327#if KMP_DEBUG
3329#endif
3330 KMP_ATOMIC_INC(unfinished_threads);
3331 KA_TRACE(
3332 20,
3333 ("__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n",
3334 gtid, count + 1, task_team));
3335 *thread_finished = FALSE;
3336 }
3337 TCW_4(victim_td->td.td_deque_ntasks, ntasks - 1);
3338
3339 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
3340
3341 KMP_COUNT_BLOCK(TASK_stolen);
3342 KA_TRACE(10,
3343 ("__kmp_steal_task(exit #5): T#%d stole task %p from T#%d: "
3344 "task_team=%p ntasks=%d head=%u tail=%u\n",
3345 gtid, taskdata, __kmp_gtid_from_thread(victim_thr), task_team,
3346 ntasks, victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
3347
3348 task = KMP_TASKDATA_TO_TASK(taskdata);
3349 return task;
3350}
3351
3352// __kmp_execute_tasks_template: Choose and execute tasks until either the
3353// condition is statisfied (return true) or there are none left (return false).
3354//
3355// final_spin is TRUE if this is the spin at the release barrier.
3356// thread_finished indicates whether the thread is finished executing all
3357// the tasks it has on its deque, and is at the release barrier.
3358// spinner is the location on which to spin.
3359// spinner == NULL means only execute a single task and return.
3360// checker is the value to check to terminate the spin.
3361template <class C>
3363 kmp_info_t *thread, kmp_int32 gtid, C *flag, int final_spin,
3364 int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj),
3365 kmp_int32 is_constrained) {
3366 kmp_task_team_t *task_team = thread->th.th_task_team;
3367 kmp_thread_data_t *threads_data;
3369 kmp_info_t *other_thread;
3370 kmp_taskdata_t *current_task = thread->th.th_current_task;
3371 std::atomic<kmp_int32> *unfinished_threads;
3372 kmp_int32 nthreads, victim_tid = -2, use_own_tasks = 1, new_victim = 0,
3373 tid = thread->th.th_info.ds.ds_tid;
3374
3376 KMP_DEBUG_ASSERT(thread == __kmp_threads[gtid]);
3377
3378 if (task_team == NULL || current_task == NULL)
3379 return FALSE;
3380
3381 KA_TRACE(15, ("__kmp_execute_tasks_template(enter): T#%d final_spin=%d "
3382 "*thread_finished=%d\n",
3383 gtid, final_spin, *thread_finished));
3384
3385 thread->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
3386 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
3387
3388 KMP_DEBUG_ASSERT(threads_data != NULL);
3389
3390 nthreads = task_team->tt.tt_nproc;
3391 unfinished_threads = &(task_team->tt.tt_unfinished_threads);
3392 KMP_DEBUG_ASSERT(*unfinished_threads >= 0);
3393
3394 while (1) { // Outer loop keeps trying to find tasks in case of single thread
3395 // getting tasks from target constructs
3396 while (1) { // Inner loop to find a task and execute it
3397 task = NULL;
3398 if (task_team->tt.tt_num_task_pri) { // get priority task first
3399 task = __kmp_get_priority_task(gtid, task_team, is_constrained);
3400 }
3401 if (task == NULL && use_own_tasks) { // check own queue next
3402 task = __kmp_remove_my_task(thread, gtid, task_team, is_constrained);
3403 }
3404 if ((task == NULL) && (nthreads > 1)) { // Steal a task finally
3405 int asleep = 1;
3406 use_own_tasks = 0;
3407 // Try to steal from the last place I stole from successfully.
3408 if (victim_tid == -2) { // haven't stolen anything yet
3409 victim_tid = threads_data[tid].td.td_deque_last_stolen;
3410 if (victim_tid !=
3411 -1) // if we have a last stolen from victim, get the thread
3412 other_thread = threads_data[victim_tid].td.td_thr;
3413 }
3414 if (victim_tid != -1) { // found last victim
3415 asleep = 0;
3416 } else if (!new_victim) { // no recent steals and we haven't already
3417 // used a new victim; select a random thread
3418 do { // Find a different thread to steal work from.
3419 // Pick a random thread. Initial plan was to cycle through all the
3420 // threads, and only return if we tried to steal from every thread,
3421 // and failed. Arch says that's not such a great idea.
3422 victim_tid = __kmp_get_random(thread) % (nthreads - 1);
3423 if (victim_tid >= tid) {
3424 ++victim_tid; // Adjusts random distribution to exclude self
3425 }
3426 // Found a potential victim
3427 other_thread = threads_data[victim_tid].td.td_thr;
3428 // There is a slight chance that __kmp_enable_tasking() did not wake
3429 // up all threads waiting at the barrier. If victim is sleeping,
3430 // then wake it up. Since we were going to pay the cache miss
3431 // penalty for referencing another thread's kmp_info_t struct
3432 // anyway,
3433 // the check shouldn't cost too much performance at this point. In
3434 // extra barrier mode, tasks do not sleep at the separate tasking
3435 // barrier, so this isn't a problem.
3436 asleep = 0;
3439 (TCR_PTR(CCAST(void *, other_thread->th.th_sleep_loc)) !=
3440 NULL)) {
3441 asleep = 1;
3442 __kmp_null_resume_wrapper(other_thread);
3443 // A sleeping thread should not have any tasks on it's queue.
3444 // There is a slight possibility that it resumes, steals a task
3445 // from another thread, which spawns more tasks, all in the time
3446 // that it takes this thread to check => don't write an assertion
3447 // that the victim's queue is empty. Try stealing from a
3448 // different thread.
3449 }
3450 } while (asleep);
3451 }
3452
3453 if (!asleep) {
3454 // We have a victim to try to steal from
3455 task =
3456 __kmp_steal_task(victim_tid, gtid, task_team, unfinished_threads,
3457 thread_finished, is_constrained);
3458 }
3459 if (task != NULL) { // set last stolen to victim
3460 if (threads_data[tid].td.td_deque_last_stolen != victim_tid) {
3461 threads_data[tid].td.td_deque_last_stolen = victim_tid;
3462 // The pre-refactored code did not try more than 1 successful new
3463 // vicitm, unless the last one generated more local tasks;
3464 // new_victim keeps track of this
3465 new_victim = 1;
3466 }
3467 } else { // No tasks found; unset last_stolen
3468 KMP_CHECK_UPDATE(threads_data[tid].td.td_deque_last_stolen, -1);
3469 victim_tid = -2; // no successful victim found
3470 }
3471 }
3472
3473 if (task == NULL)
3474 break; // break out of tasking loop
3475
3476// Found a task; execute it
3477#if USE_ITT_BUILD && USE_ITT_NOTIFY
3478 if (__itt_sync_create_ptr || KMP_ITT_DEBUG) {
3479 if (itt_sync_obj == NULL) { // we are at fork barrier where we could not
3480 // get the object reliably
3481 itt_sync_obj = __kmp_itt_barrier_object(gtid, bs_forkjoin_barrier);
3482 }
3483 __kmp_itt_task_starting(itt_sync_obj);
3484 }
3485#endif /* USE_ITT_BUILD && USE_ITT_NOTIFY */
3486 __kmp_invoke_task(gtid, task, current_task);
3487#if USE_ITT_BUILD
3488 if (itt_sync_obj != NULL)
3489 __kmp_itt_task_finished(itt_sync_obj);
3490#endif /* USE_ITT_BUILD */
3491 // If this thread is only partway through the barrier and the condition is
3492 // met, then return now, so that the barrier gather/release pattern can
3493 // proceed. If this thread is in the last spin loop in the barrier,
3494 // waiting to be released, we know that the termination condition will not
3495 // be satisfied, so don't waste any cycles checking it.
3496 if (flag == NULL || (!final_spin && flag->done_check())) {
3497 KA_TRACE(
3498 15,
3499 ("__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3500 gtid));
3501 return TRUE;
3502 }
3503 if (thread->th.th_task_team == NULL) {
3504 break;
3505 }
3506 KMP_YIELD(__kmp_library == library_throughput); // Yield before next task
3507 // If execution of a stolen task results in more tasks being placed on our
3508 // run queue, reset use_own_tasks
3509 if (!use_own_tasks && TCR_4(threads_data[tid].td.td_deque_ntasks) != 0) {
3510 KA_TRACE(20, ("__kmp_execute_tasks_template: T#%d stolen task spawned "
3511 "other tasks, restart\n",
3512 gtid));
3513 use_own_tasks = 1;
3514 new_victim = 0;
3515 }
3516 }
3517
3518 // The task source has been exhausted. If in final spin loop of barrier,
3519 // check if termination condition is satisfied. The work queue may be empty
3520 // but there might be proxy tasks still executing.
3521 if (final_spin &&
3522 KMP_ATOMIC_LD_ACQ(&current_task->td_incomplete_child_tasks) == 0) {
3523 // First, decrement the #unfinished threads, if that has not already been
3524 // done. This decrement might be to the spin location, and result in the
3525 // termination condition being satisfied.
3526 if (!*thread_finished) {
3527#if KMP_DEBUG
3528 kmp_int32 count = -1 +
3529#endif
3530 KMP_ATOMIC_DEC(unfinished_threads);
3531 KA_TRACE(20, ("__kmp_execute_tasks_template: T#%d dec "
3532 "unfinished_threads to %d task_team=%p\n",
3533 gtid, count, task_team));
3534 *thread_finished = TRUE;
3535 }
3536
3537 // It is now unsafe to reference thread->th.th_team !!!
3538 // Decrementing task_team->tt.tt_unfinished_threads can allow the primary
3539 // thread to pass through the barrier, where it might reset each thread's
3540 // th.th_team field for the next parallel region. If we can steal more
3541 // work, we know that this has not happened yet.
3542 if (flag != NULL && flag->done_check()) {
3543 KA_TRACE(
3544 15,
3545 ("__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3546 gtid));
3547 return TRUE;
3548 }
3549 }
3550
3551 // If this thread's task team is NULL, primary thread has recognized that
3552 // there are no more tasks; bail out
3553 if (thread->th.th_task_team == NULL) {
3554 KA_TRACE(15,
3555 ("__kmp_execute_tasks_template: T#%d no more tasks\n", gtid));
3556 return FALSE;
3557 }
3558
3559 // Check the flag again to see if it has already done in case to be trapped
3560 // into infinite loop when a if0 task depends on a hidden helper task
3561 // outside any parallel region. Detached tasks are not impacted in this case
3562 // because the only thread executing this function has to execute the proxy
3563 // task so it is in another code path that has the same check.
3564 if (flag == NULL || (!final_spin && flag->done_check())) {
3565 KA_TRACE(15,
3566 ("__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
3567 gtid));
3568 return TRUE;
3569 }
3570
3571 // We could be getting tasks from target constructs; if this is the only
3572 // thread, keep trying to execute tasks from own queue
3573 if (nthreads == 1 &&
3575 use_own_tasks = 1;
3576 else {
3577 KA_TRACE(15,
3578 ("__kmp_execute_tasks_template: T#%d can't find work\n", gtid));
3579 return FALSE;
3580 }
3581 }
3582}
3583
3584template <bool C, bool S>
3586 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32<C, S> *flag, int final_spin,
3587 int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj),
3588 kmp_int32 is_constrained) {
3590 thread, gtid, flag, final_spin,
3591 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3592}
3593
3594template <bool C, bool S>
3596 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64<C, S> *flag, int final_spin,
3597 int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj),
3598 kmp_int32 is_constrained) {
3600 thread, gtid, flag, final_spin,
3601 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3602}
3603
3604template <bool C, bool S>
3607 int final_spin, int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj),
3608 kmp_int32 is_constrained) {
3610 thread, gtid, flag, final_spin,
3611 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3612}
3613
3615 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag, int final_spin,
3616 int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj),
3617 kmp_int32 is_constrained) {
3619 thread, gtid, flag, final_spin,
3620 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
3621}
3622
3623template int
3626 int *USE_ITT_BUILD_ARG(void *), kmp_int32);
3627
3630 int,
3631 int *USE_ITT_BUILD_ARG(void *),
3632 kmp_int32);
3633
3636 int,
3637 int *USE_ITT_BUILD_ARG(void *),
3638 kmp_int32);
3639
3642 int *USE_ITT_BUILD_ARG(void *), kmp_int32);
3643
3646 int *USE_ITT_BUILD_ARG(void *), kmp_int32);
3647
3648// __kmp_enable_tasking: Allocate task team and resume threads sleeping at the
3649// next barrier so they can assist in executing enqueued tasks.
3650// First thread in allocates the task team atomically.
3652 kmp_info_t *this_thr) {
3653 kmp_thread_data_t *threads_data;
3654 int nthreads, i, is_init_thread;
3655
3656 KA_TRACE(10, ("__kmp_enable_tasking(enter): T#%d\n",
3657 __kmp_gtid_from_thread(this_thr)));
3658
3659 KMP_DEBUG_ASSERT(task_team != NULL);
3660 KMP_DEBUG_ASSERT(this_thr->th.th_team != NULL);
3661
3662 nthreads = task_team->tt.tt_nproc;
3663 KMP_DEBUG_ASSERT(nthreads > 0);
3664 KMP_DEBUG_ASSERT(nthreads == this_thr->th.th_team->t.t_nproc);
3665
3666 // Allocate or increase the size of threads_data if necessary
3667 is_init_thread = __kmp_realloc_task_threads_data(this_thr, task_team);
3668
3669 if (!is_init_thread) {
3670 // Some other thread already set up the array.
3671 KA_TRACE(
3672 20,
3673 ("__kmp_enable_tasking(exit): T#%d: threads array already set up.\n",
3674 __kmp_gtid_from_thread(this_thr)));
3675 return;
3676 }
3677 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
3678 KMP_DEBUG_ASSERT(threads_data != NULL);
3679
3682 // Release any threads sleeping at the barrier, so that they can steal
3683 // tasks and execute them. In extra barrier mode, tasks do not sleep
3684 // at the separate tasking barrier, so this isn't a problem.
3685 for (i = 0; i < nthreads; i++) {
3686 void *sleep_loc;
3687 kmp_info_t *thread = threads_data[i].td.td_thr;
3688
3689 if (i == this_thr->th.th_info.ds.ds_tid) {
3690 continue;
3691 }
3692 // Since we haven't locked the thread's suspend mutex lock at this
3693 // point, there is a small window where a thread might be putting
3694 // itself to sleep, but hasn't set the th_sleep_loc field yet.
3695 // To work around this, __kmp_execute_tasks_template() periodically checks
3696 // see if other threads are sleeping (using the same random mechanism that
3697 // is used for task stealing) and awakens them if they are.
3698 if ((sleep_loc = TCR_PTR(CCAST(void *, thread->th.th_sleep_loc))) !=
3699 NULL) {
3700 KF_TRACE(50, ("__kmp_enable_tasking: T#%d waking up thread T#%d\n",
3701 __kmp_gtid_from_thread(this_thr),
3702 __kmp_gtid_from_thread(thread)));
3704 } else {
3705 KF_TRACE(50, ("__kmp_enable_tasking: T#%d don't wake up thread T#%d\n",
3706 __kmp_gtid_from_thread(this_thr),
3707 __kmp_gtid_from_thread(thread)));
3708 }
3709 }
3710 }
3711
3712 KA_TRACE(10, ("__kmp_enable_tasking(exit): T#%d\n",
3713 __kmp_gtid_from_thread(this_thr)));
3714}
3715
3716/* // TODO: Check the comment consistency
3717 * Utility routines for "task teams". A task team (kmp_task_t) is kind of
3718 * like a shadow of the kmp_team_t data struct, with a different lifetime.
3719 * After a child * thread checks into a barrier and calls __kmp_release() from
3720 * the particular variant of __kmp_<barrier_kind>_barrier_gather(), it can no
3721 * longer assume that the kmp_team_t structure is intact (at any moment, the
3722 * primary thread may exit the barrier code and free the team data structure,
3723 * and return the threads to the thread pool).
3724 *
3725 * This does not work with the tasking code, as the thread is still
3726 * expected to participate in the execution of any tasks that may have been
3727 * spawned my a member of the team, and the thread still needs access to all
3728 * to each thread in the team, so that it can steal work from it.
3729 *
3730 * Enter the existence of the kmp_task_team_t struct. It employs a reference
3731 * counting mechanism, and is allocated by the primary thread before calling
3732 * __kmp_<barrier_kind>_release, and then is release by the last thread to
3733 * exit __kmp_<barrier_kind>_release at the next barrier. I.e. the lifetimes
3734 * of the kmp_task_team_t structs for consecutive barriers can overlap
3735 * (and will, unless the primary thread is the last thread to exit the barrier
3736 * release phase, which is not typical). The existence of such a struct is
3737 * useful outside the context of tasking.
3738 *
3739 * We currently use the existence of the threads array as an indicator that
3740 * tasks were spawned since the last barrier. If the structure is to be
3741 * useful outside the context of tasking, then this will have to change, but
3742 * not setting the field minimizes the performance impact of tasking on
3743 * barriers, when no explicit tasks were spawned (pushed, actually).
3744 */
3745
3747 NULL; // Free list for task_team data structures
3748// Lock for task team data structures
3751
3752// __kmp_alloc_task_deque:
3753// Allocates a task deque for a particular thread, and initialize the necessary
3754// data structures relating to the deque. This only happens once per thread
3755// per task team since task teams are recycled. No lock is needed during
3756// allocation since each thread allocates its own deque.
3758 kmp_thread_data_t *thread_data) {
3759 __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
3760 KMP_DEBUG_ASSERT(thread_data->td.td_deque == NULL);
3761
3762 // Initialize last stolen task field to "none"
3763 thread_data->td.td_deque_last_stolen = -1;
3764
3765 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == 0);
3766 KMP_DEBUG_ASSERT(thread_data->td.td_deque_head == 0);
3767 KMP_DEBUG_ASSERT(thread_data->td.td_deque_tail == 0);
3768
3769 KE_TRACE(
3770 10,
3771 ("__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n",
3772 __kmp_gtid_from_thread(thread), INITIAL_TASK_DEQUE_SIZE, thread_data));
3773 // Allocate space for task deque, and zero the deque
3774 // Cannot use __kmp_thread_calloc() because threads not around for
3775 // kmp_reap_task_team( ).
3776 thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
3778 thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
3779}
3780
3781// __kmp_free_task_deque:
3782// Deallocates a task deque for a particular thread. Happens at library
3783// deallocation so don't need to reset all thread data fields.
3784static void __kmp_free_task_deque(kmp_thread_data_t *thread_data) {
3785 if (thread_data->td.td_deque != NULL) {
3786 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3787 TCW_4(thread_data->td.td_deque_ntasks, 0);
3788 __kmp_free(thread_data->td.td_deque);
3789 thread_data->td.td_deque = NULL;
3790 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3791 }
3792
3793#ifdef BUILD_TIED_TASK_STACK
3794 // GEH: Figure out what to do here for td_susp_tied_tasks
3795 if (thread_data->td.td_susp_tied_tasks.ts_entries != TASK_STACK_EMPTY) {
3796 __kmp_free_task_stack(__kmp_thread_from_gtid(gtid), thread_data);
3797 }
3798#endif // BUILD_TIED_TASK_STACK
3799}
3800
3801// __kmp_realloc_task_threads_data:
3802// Allocates a threads_data array for a task team, either by allocating an
3803// initial array or enlarging an existing array. Only the first thread to get
3804// the lock allocs or enlarges the array and re-initializes the array elements.
3805// That thread returns "TRUE", the rest return "FALSE".
3806// Assumes that the new array size is given by task_team -> tt.tt_nproc.
3807// The current size is given by task_team -> tt.tt_max_threads.
3809 kmp_task_team_t *task_team) {
3810 kmp_thread_data_t **threads_data_p;
3811 kmp_int32 nthreads, maxthreads;
3812 int is_init_thread = FALSE;
3813
3814 if (TCR_4(task_team->tt.tt_found_tasks)) {
3815 // Already reallocated and initialized.
3816 return FALSE;
3817 }
3818
3819 threads_data_p = &task_team->tt.tt_threads_data;
3820 nthreads = task_team->tt.tt_nproc;
3821 maxthreads = task_team->tt.tt_max_threads;
3822
3823 // All threads must lock when they encounter the first task of the implicit
3824 // task region to make sure threads_data fields are (re)initialized before
3825 // used.
3827
3828 if (!TCR_4(task_team->tt.tt_found_tasks)) {
3829 // first thread to enable tasking
3830 kmp_team_t *team = thread->th.th_team;
3831 int i;
3832
3833 is_init_thread = TRUE;
3834 if (maxthreads < nthreads) {
3835
3836 if (*threads_data_p != NULL) {
3837 kmp_thread_data_t *old_data = *threads_data_p;
3838 kmp_thread_data_t *new_data = NULL;
3839
3840 KE_TRACE(
3841 10,
3842 ("__kmp_realloc_task_threads_data: T#%d reallocating "
3843 "threads data for task_team %p, new_size = %d, old_size = %d\n",
3844 __kmp_gtid_from_thread(thread), task_team, nthreads, maxthreads));
3845 // Reallocate threads_data to have more elements than current array
3846 // Cannot use __kmp_thread_realloc() because threads not around for
3847 // kmp_reap_task_team( ). Note all new array entries are initialized
3848 // to zero by __kmp_allocate().
3849 new_data = (kmp_thread_data_t *)__kmp_allocate(
3850 nthreads * sizeof(kmp_thread_data_t));
3851 // copy old data to new data
3852 KMP_MEMCPY_S((void *)new_data, nthreads * sizeof(kmp_thread_data_t),
3853 (void *)old_data, maxthreads * sizeof(kmp_thread_data_t));
3854
3855#ifdef BUILD_TIED_TASK_STACK
3856 // GEH: Figure out if this is the right thing to do
3857 for (i = maxthreads; i < nthreads; i++) {
3858 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3859 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3860 }
3861#endif // BUILD_TIED_TASK_STACK
3862 // Install the new data and free the old data
3863 (*threads_data_p) = new_data;
3864 __kmp_free(old_data);
3865 } else {
3866 KE_TRACE(10, ("__kmp_realloc_task_threads_data: T#%d allocating "
3867 "threads data for task_team %p, size = %d\n",
3868 __kmp_gtid_from_thread(thread), task_team, nthreads));
3869 // Make the initial allocate for threads_data array, and zero entries
3870 // Cannot use __kmp_thread_calloc() because threads not around for
3871 // kmp_reap_task_team( ).
3872 *threads_data_p = (kmp_thread_data_t *)__kmp_allocate(
3873 nthreads * sizeof(kmp_thread_data_t));
3874#ifdef BUILD_TIED_TASK_STACK
3875 // GEH: Figure out if this is the right thing to do
3876 for (i = 0; i < nthreads; i++) {
3877 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3878 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
3879 }
3880#endif // BUILD_TIED_TASK_STACK
3881 }
3882 task_team->tt.tt_max_threads = nthreads;
3883 } else {
3884 // If array has (more than) enough elements, go ahead and use it
3885 KMP_DEBUG_ASSERT(*threads_data_p != NULL);
3886 }
3887
3888 // initialize threads_data pointers back to thread_info structures
3889 for (i = 0; i < nthreads; i++) {
3890 kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
3891 thread_data->td.td_thr = team->t.t_threads[i];
3892
3893 if (thread_data->td.td_deque_last_stolen >= nthreads) {
3894 // The last stolen field survives across teams / barrier, and the number
3895 // of threads may have changed. It's possible (likely?) that a new
3896 // parallel region will exhibit the same behavior as previous region.
3897 thread_data->td.td_deque_last_stolen = -1;
3898 }
3899 }
3900
3901 KMP_MB();
3902 TCW_SYNC_4(task_team->tt.tt_found_tasks, TRUE);
3903 }
3904
3906 return is_init_thread;
3907}
3908
3909// __kmp_free_task_threads_data:
3910// Deallocates a threads_data array for a task team, including any attached
3911// tasking deques. Only occurs at library shutdown.
3914 if (task_team->tt.tt_threads_data != NULL) {
3915 int i;
3916 for (i = 0; i < task_team->tt.tt_max_threads; i++) {
3918 }
3919 __kmp_free(task_team->tt.tt_threads_data);
3920 task_team->tt.tt_threads_data = NULL;
3921 }
3923}
3924
3925// __kmp_free_task_pri_list:
3926// Deallocates tasking deques used for priority tasks.
3927// Only occurs at library shutdown.
3930 if (task_team->tt.tt_task_pri_list != NULL) {
3931 kmp_task_pri_t *list = task_team->tt.tt_task_pri_list;
3932 while (list != NULL) {
3933 kmp_task_pri_t *next = list->next;
3934 __kmp_free_task_deque(&list->td);
3935 __kmp_free(list);
3936 list = next;
3937 }
3938 task_team->tt.tt_task_pri_list = NULL;
3939 }
3941}
3942
3943static inline void __kmp_task_team_init(kmp_task_team_t *task_team,
3944 kmp_team_t *team) {
3945 int team_nth = team->t.t_nproc;
3946 // Only need to init if task team is isn't active or team size changed
3947 if (!task_team->tt.tt_active || team_nth != task_team->tt.tt_nproc) {
3948 TCW_4(task_team->tt.tt_found_tasks, FALSE);
3949 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
3951 TCW_4(task_team->tt.tt_nproc, team_nth);
3952 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads, team_nth);
3953 TCW_4(task_team->tt.tt_active, TRUE);
3954 }
3955}
3956
3957// __kmp_allocate_task_team:
3958// Allocates a task team associated with a specific team, taking it from
3959// the global task team free list if possible. Also initializes data
3960// structures.
3962 kmp_team_t *team) {
3963 kmp_task_team_t *task_team = NULL;
3964
3965 KA_TRACE(20, ("__kmp_allocate_task_team: T#%d entering; team = %p\n",
3966 (thread ? __kmp_gtid_from_thread(thread) : -1), team));
3967
3968 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
3969 // Take a task team from the task team pool
3971 if (__kmp_free_task_teams != NULL) {
3972 task_team = __kmp_free_task_teams;
3974 task_team->tt.tt_next = NULL;
3975 }
3977 }
3978
3979 if (task_team == NULL) {
3980 KE_TRACE(10, ("__kmp_allocate_task_team: T#%d allocating "
3981 "task team for team %p\n",
3982 __kmp_gtid_from_thread(thread), team));
3983 // Allocate a new task team if one is not available. Cannot use
3984 // __kmp_thread_malloc because threads not around for kmp_reap_task_team.
3985 task_team = (kmp_task_team_t *)__kmp_allocate(sizeof(kmp_task_team_t));
3988#if USE_ITT_BUILD && USE_ITT_NOTIFY && KMP_DEBUG
3989 // suppress race conditions detection on synchronization flags in debug mode
3990 // this helps to analyze library internals eliminating false positives
3991 __itt_suppress_mark_range(
3992 __itt_suppress_range, __itt_suppress_threading_errors,
3993 &task_team->tt.tt_found_tasks, sizeof(task_team->tt.tt_found_tasks));
3994 __itt_suppress_mark_range(__itt_suppress_range,
3995 __itt_suppress_threading_errors,
3996 CCAST(kmp_uint32 *, &task_team->tt.tt_active),
3997 sizeof(task_team->tt.tt_active));
3998#endif /* USE_ITT_BUILD && USE_ITT_NOTIFY && KMP_DEBUG */
3999 // Note: __kmp_allocate zeroes returned memory, othewise we would need:
4000 // task_team->tt.tt_threads_data = NULL;
4001 // task_team->tt.tt_max_threads = 0;
4002 // task_team->tt.tt_next = NULL;
4003 }
4004
4005 __kmp_task_team_init(task_team, team);
4006
4007 KA_TRACE(20, ("__kmp_allocate_task_team: T#%d exiting; task_team = %p "
4008 "unfinished_threads init'd to %d\n",
4009 (thread ? __kmp_gtid_from_thread(thread) : -1), task_team,
4011 return task_team;
4012}
4013
4014// __kmp_free_task_team:
4015// Frees the task team associated with a specific thread, and adds it
4016// to the global task team free list.
4018 KA_TRACE(20, ("__kmp_free_task_team: T#%d task_team = %p\n",
4019 thread ? __kmp_gtid_from_thread(thread) : -1, task_team));
4020
4021 // Put task team back on free list
4023
4024 KMP_DEBUG_ASSERT(task_team->tt.tt_next == NULL);
4025 task_team->tt.tt_next = __kmp_free_task_teams;
4026 TCW_PTR(__kmp_free_task_teams, task_team);
4027
4029}
4030
4031// __kmp_reap_task_teams:
4032// Free all the task teams on the task team free list.
4033// Should only be done during library shutdown.
4034// Cannot do anything that needs a thread structure or gtid since they are
4035// already gone.
4037 kmp_task_team_t *task_team;
4038
4039 if (TCR_PTR(__kmp_free_task_teams) != NULL) {
4040 // Free all task_teams on the free list
4042 while ((task_team = __kmp_free_task_teams) != NULL) {
4043 __kmp_free_task_teams = task_team->tt.tt_next;
4044 task_team->tt.tt_next = NULL;
4045
4046 // Free threads_data if necessary
4047 if (task_team->tt.tt_threads_data != NULL) {
4049 }
4050 if (task_team->tt.tt_task_pri_list != NULL) {
4051 __kmp_free_task_pri_list(task_team);
4052 }
4053 __kmp_free(task_team);
4054 }
4056 }
4057}
4058
4059// View the array of two task team pointers as a pair of pointers:
4060// 1) a single task_team pointer
4061// 2) next pointer for stack
4062// Serial teams can create a stack of task teams for nested serial teams.
4064 KMP_DEBUG_ASSERT(team->t.t_nproc == 1);
4065 kmp_task_team_list_t *current =
4066 (kmp_task_team_list_t *)(&team->t.t_task_team[0]);
4067 kmp_task_team_list_t *node =
4069 node->task_team = current->task_team;
4070 node->next = current->next;
4071 thread->th.th_task_team = current->task_team = NULL;
4072 current->next = node;
4073}
4074
4075// Serial team pops a task team off the stack
4077 KMP_DEBUG_ASSERT(team->t.t_nproc == 1);
4078 kmp_task_team_list_t *current =
4079 (kmp_task_team_list_t *)(&team->t.t_task_team[0]);
4080 if (current->task_team) {
4081 __kmp_free_task_team(thread, current->task_team);
4082 }
4083 kmp_task_team_list_t *next = current->next;
4084 if (next) {
4085 current->task_team = next->task_team;
4086 current->next = next->next;
4087 KMP_DEBUG_ASSERT(next != current);
4088 __kmp_free(next);
4089 thread->th.th_task_team = current->task_team;
4090 }
4091}
4092
4093// __kmp_wait_to_unref_task_teams:
4094// Some threads could still be in the fork barrier release code, possibly
4095// trying to steal tasks. Wait for each thread to unreference its task team.
4097 kmp_info_t *thread;
4098 kmp_uint32 spins;
4099 kmp_uint64 time;
4100 int done;
4101
4102 KMP_INIT_YIELD(spins);
4103 KMP_INIT_BACKOFF(time);
4104
4105 for (;;) {
4106 done = TRUE;
4107
4108 // TODO: GEH - this may be is wrong because some sync would be necessary
4109 // in case threads are added to the pool during the traversal. Need to
4110 // verify that lock for thread pool is held when calling this routine.
4111 for (thread = CCAST(kmp_info_t *, __kmp_thread_pool); thread != NULL;
4112 thread = thread->th.th_next_pool) {
4113#if KMP_OS_WINDOWS
4114 DWORD exit_val;
4115#endif
4116 if (TCR_PTR(thread->th.th_task_team) == NULL) {
4117 KA_TRACE(10, ("__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n",
4118 __kmp_gtid_from_thread(thread)));
4119 continue;
4120 }
4121#if KMP_OS_WINDOWS
4122 // TODO: GEH - add this check for Linux* OS / OS X* as well?
4123 if (!__kmp_is_thread_alive(thread, &exit_val)) {
4124 thread->th.th_task_team = NULL;
4125 continue;
4126 }
4127#endif
4128
4129 done = FALSE; // Because th_task_team pointer is not NULL for this thread
4130
4131 KA_TRACE(10, ("__kmp_wait_to_unref_task_team: Waiting for T#%d to "
4132 "unreference task_team\n",
4133 __kmp_gtid_from_thread(thread)));
4134
4136 void *sleep_loc;
4137 // If the thread is sleeping, awaken it.
4138 if ((sleep_loc = TCR_PTR(CCAST(void *, thread->th.th_sleep_loc))) !=
4139 NULL) {
4140 KA_TRACE(
4141 10,
4142 ("__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n",
4145 }
4146 }
4147 }
4148 if (done) {
4149 break;
4150 }
4151
4152 // If oversubscribed or have waited a bit, yield.
4153 KMP_YIELD_OVERSUB_ELSE_SPIN(spins, time);
4154 }
4155}
4156
4157// __kmp_task_team_setup: Create a task_team for the current team, but use
4158// an already created, unused one if it already exists.
4161
4162 // For the serial and root teams, setup the first task team pointer to point
4163 // to task team. The other pointer is a stack of task teams from previous
4164 // serial levels.
4165 if (team == this_thr->th.th_serial_team ||
4166 team == this_thr->th.th_root->r.r_root_team) {
4167 KMP_DEBUG_ASSERT(team->t.t_nproc == 1);
4168 if (team->t.t_task_team[0] == NULL) {
4169 team->t.t_task_team[0] = __kmp_allocate_task_team(this_thr, team);
4170 KA_TRACE(
4171 20, ("__kmp_task_team_setup: Primary T#%d created new task_team %p"
4172 " for serial/root team %p\n",
4173 __kmp_gtid_from_thread(this_thr), team->t.t_task_team[0], team));
4174
4175 } else
4176 __kmp_task_team_init(team->t.t_task_team[0], team);
4177 return;
4178 }
4179
4180 // If this task_team hasn't been created yet, allocate it. It will be used in
4181 // the region after the next.
4182 // If it exists, it is the current task team and shouldn't be touched yet as
4183 // it may still be in use.
4184 if (team->t.t_task_team[this_thr->th.th_task_state] == NULL) {
4185 team->t.t_task_team[this_thr->th.th_task_state] =
4186 __kmp_allocate_task_team(this_thr, team);
4187 KA_TRACE(20, ("__kmp_task_team_setup: Primary T#%d created new task_team %p"
4188 " for team %d at parity=%d\n",
4189 __kmp_gtid_from_thread(this_thr),
4190 team->t.t_task_team[this_thr->th.th_task_state], team->t.t_id,
4191 this_thr->th.th_task_state));
4192 }
4193
4194 // After threads exit the release, they will call sync, and then point to this
4195 // other task_team; make sure it is allocated and properly initialized. As
4196 // threads spin in the barrier release phase, they will continue to use the
4197 // previous task_team struct(above), until they receive the signal to stop
4198 // checking for tasks (they can't safely reference the kmp_team_t struct,
4199 // which could be reallocated by the primary thread).
4200 int other_team = 1 - this_thr->th.th_task_state;
4201 KMP_DEBUG_ASSERT(other_team >= 0 && other_team < 2);
4202 if (team->t.t_task_team[other_team] == NULL) { // setup other team as well
4203 team->t.t_task_team[other_team] = __kmp_allocate_task_team(this_thr, team);
4204 KA_TRACE(20, ("__kmp_task_team_setup: Primary T#%d created second new "
4205 "task_team %p for team %d at parity=%d\n",
4206 __kmp_gtid_from_thread(this_thr),
4207 team->t.t_task_team[other_team], team->t.t_id, other_team));
4208 } else { // Leave the old task team struct in place for the upcoming region;
4209 // adjust as needed
4210 kmp_task_team_t *task_team = team->t.t_task_team[other_team];
4211 __kmp_task_team_init(task_team, team);
4212 // if team size has changed, the first thread to enable tasking will
4213 // realloc threads_data if necessary
4214 KA_TRACE(20, ("__kmp_task_team_setup: Primary T#%d reset next task_team "
4215 "%p for team %d at parity=%d\n",
4216 __kmp_gtid_from_thread(this_thr),
4217 team->t.t_task_team[other_team], team->t.t_id, other_team));
4218 }
4219
4220 // For regular thread, task enabling should be called when the task is going
4221 // to be pushed to a dequeue. However, for the hidden helper thread, we need
4222 // it ahead of time so that some operations can be performed without race
4223 // condition.
4224 if (this_thr == __kmp_hidden_helper_main_thread) {
4225 for (int i = 0; i < 2; ++i) {
4226 kmp_task_team_t *task_team = team->t.t_task_team[i];
4227 if (KMP_TASKING_ENABLED(task_team)) {
4228 continue;
4229 }
4230 __kmp_enable_tasking(task_team, this_thr);
4231 for (int j = 0; j < task_team->tt.tt_nproc; ++j) {
4232 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[j];
4233 if (thread_data->td.td_deque == NULL) {
4235 }
4236 }
4237 }
4238 }
4239}
4240
4241// __kmp_task_team_sync: Propagation of task team data from team to threads
4242// which happens just after the release phase of a team barrier. This may be
4243// called by any thread. This is not called for serial or root teams.
4246 KMP_DEBUG_ASSERT(team != this_thr->th.th_serial_team);
4247 KMP_DEBUG_ASSERT(team != this_thr->th.th_root->r.r_root_team);
4248
4249 // Toggle the th_task_state field, to switch which task_team this thread
4250 // refers to
4251 this_thr->th.th_task_state = (kmp_uint8)(1 - this_thr->th.th_task_state);
4252
4253 // It is now safe to propagate the task team pointer from the team struct to
4254 // the current thread.
4255 TCW_PTR(this_thr->th.th_task_team,
4256 team->t.t_task_team[this_thr->th.th_task_state]);
4257 KA_TRACE(20,
4258 ("__kmp_task_team_sync: Thread T#%d task team switched to task_team "
4259 "%p from Team #%d (parity=%d)\n",
4260 __kmp_gtid_from_thread(this_thr), this_thr->th.th_task_team,
4261 team->t.t_id, this_thr->th.th_task_state));
4262}
4263
4264// __kmp_task_team_wait: Primary thread waits for outstanding tasks after the
4265// barrier gather phase. Only called by the primary thread.
4266//
4267// wait is a flag that defaults to 1 (see kmp.h), but waiting can be turned off
4268// by passing in 0 optionally as the last argument. When wait is zero, primary
4269// thread does not wait for unfinished_threads to reach 0.
4271 kmp_info_t *this_thr,
4272 kmp_team_t *team USE_ITT_BUILD_ARG(void *itt_sync_obj), int wait) {
4273 kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state];
4274
4276 KMP_DEBUG_ASSERT(task_team == this_thr->th.th_task_team);
4277
4278 if ((task_team != NULL) && KMP_TASKING_ENABLED(task_team)) {
4279 if (wait) {
4280 KA_TRACE(20, ("__kmp_task_team_wait: Primary T#%d waiting for all tasks "
4281 "(for unfinished_threads to reach 0) on task_team = %p\n",
4282 __kmp_gtid_from_thread(this_thr), task_team));
4283 // Worker threads may have dropped through to release phase, but could
4284 // still be executing tasks. Wait here for tasks to complete. To avoid
4285 // memory contention, only primary thread checks termination condition.
4287 RCAST(std::atomic<kmp_uint32> *,
4288 &task_team->tt.tt_unfinished_threads),
4289 0U);
4290 flag.wait(this_thr, TRUE USE_ITT_BUILD_ARG(itt_sync_obj));
4291 }
4292 // Deactivate the old task team, so that the worker threads will stop
4293 // referencing it while spinning.
4294 KA_TRACE(
4295 20,
4296 ("__kmp_task_team_wait: Primary T#%d deactivating task_team %p: "
4297 "setting active to false, setting local and team's pointer to NULL\n",
4298 __kmp_gtid_from_thread(this_thr), task_team));
4302 TCW_SYNC_4(task_team->tt.tt_active, FALSE);
4303 KMP_MB();
4304
4305 TCW_PTR(this_thr->th.th_task_team, NULL);
4306 }
4307}
4308
4309// __kmp_tasking_barrier:
4310// This routine is called only when __kmp_tasking_mode == tskm_extra_barrier.
4311// Internal function to execute all tasks prior to a regular barrier or a join
4312// barrier. It is a full barrier itself, which unfortunately turns regular
4313// barriers into double barriers and join barriers into 1 1/2 barriers.
4314void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread, int gtid) {
4315 std::atomic<kmp_uint32> *spin = RCAST(
4316 std::atomic<kmp_uint32> *,
4317 &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads);
4318 int flag = FALSE;
4320
4321#if USE_ITT_BUILD
4322 KMP_FSYNC_SPIN_INIT(spin, NULL);
4323#endif /* USE_ITT_BUILD */
4324 kmp_flag_32<false, false> spin_flag(spin, 0U);
4325 while (!spin_flag.execute_tasks(thread, gtid, TRUE,
4326 &flag USE_ITT_BUILD_ARG(NULL), 0)) {
4327#if USE_ITT_BUILD
4328 // TODO: What about itt_sync_obj??
4329 KMP_FSYNC_SPIN_PREPARE(RCAST(void *, spin));
4330#endif /* USE_ITT_BUILD */
4331
4332 if (TCR_4(__kmp_global.g.g_done)) {
4333 if (__kmp_global.g.g_abort)
4335 break;
4336 }
4337 KMP_YIELD(TRUE);
4338 }
4339#if USE_ITT_BUILD
4340 KMP_FSYNC_SPIN_ACQUIRED(RCAST(void *, spin));
4341#endif /* USE_ITT_BUILD */
4342}
4343
4344// __kmp_give_task puts a task into a given thread queue if:
4345// - the queue for that thread was created
4346// - there's space in that queue
4347// Because of this, __kmp_push_task needs to check if there's space after
4348// getting the lock
4350 kmp_int32 pass) {
4352 kmp_task_team_t *task_team = taskdata->td_task_team;
4353
4354 KA_TRACE(20, ("__kmp_give_task: trying to give task %p to thread %d.\n",
4355 taskdata, tid));
4356
4357 // If task_team is NULL something went really bad...
4358 KMP_DEBUG_ASSERT(task_team != NULL);
4359
4360 bool result = false;
4361 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
4362
4363 if (thread_data->td.td_deque == NULL) {
4364 // There's no queue in this thread, go find another one
4365 // We're guaranteed that at least one thread has a queue
4366 KA_TRACE(30,
4367 ("__kmp_give_task: thread %d has no queue while giving task %p.\n",
4368 tid, taskdata));
4369 return result;
4370 }
4371
4372 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4373 TASK_DEQUE_SIZE(thread_data->td)) {
4374 KA_TRACE(
4375 30,
4376 ("__kmp_give_task: queue is full while giving task %p to thread %d.\n",
4377 taskdata, tid));
4378
4379 // if this deque is bigger than the pass ratio give a chance to another
4380 // thread
4381 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
4382 return result;
4383
4384 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
4385 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4386 TASK_DEQUE_SIZE(thread_data->td)) {
4387 // expand deque to push the task which is not allowed to execute
4388 __kmp_realloc_task_deque(thread, thread_data);
4389 }
4390
4391 } else {
4392
4393 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
4394
4395 if (TCR_4(thread_data->td.td_deque_ntasks) >=
4396 TASK_DEQUE_SIZE(thread_data->td)) {
4397 KA_TRACE(30, ("__kmp_give_task: queue is full while giving task %p to "
4398 "thread %d.\n",
4399 taskdata, tid));
4400
4401 // if this deque is bigger than the pass ratio give a chance to another
4402 // thread
4403 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
4404 goto release_and_exit;
4405
4406 __kmp_realloc_task_deque(thread, thread_data);
4407 }
4408 }
4409
4410 // lock is held here, and there is space in the deque
4411
4412 thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
4413 // Wrap index.
4414 thread_data->td.td_deque_tail =
4415 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
4416 TCW_4(thread_data->td.td_deque_ntasks,
4417 TCR_4(thread_data->td.td_deque_ntasks) + 1);
4418
4419 result = true;
4420 KA_TRACE(30, ("__kmp_give_task: successfully gave task %p to thread %d.\n",
4421 taskdata, tid));
4422
4423release_and_exit:
4424 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
4425
4426 return result;
4427}
4428
4429#define PROXY_TASK_FLAG 0x40000000
4430/* The finish of the proxy tasks is divided in two pieces:
4431 - the top half is the one that can be done from a thread outside the team
4432 - the bottom half must be run from a thread within the team
4433
4434 In order to run the bottom half the task gets queued back into one of the
4435 threads of the team. Once the td_incomplete_child_task counter of the parent
4436 is decremented the threads can leave the barriers. So, the bottom half needs
4437 to be queued before the counter is decremented. The top half is therefore
4438 divided in two parts:
4439 - things that can be run before queuing the bottom half
4440 - things that must be run after queuing the bottom half
4441
4442 This creates a second race as the bottom half can free the task before the
4443 second top half is executed. To avoid this we use the
4444 td_incomplete_child_task of the proxy task to synchronize the top and bottom
4445 half. */
4449 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
4450 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
4451
4452 taskdata->td_flags.complete = 1; // mark the task as completed
4453#if OMPX_TASKGRAPH
4454 taskdata->td_flags.onced = 1;
4455#endif
4456
4457 if (taskdata->td_taskgroup)
4458 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
4459
4460 // Create an imaginary children for this task so the bottom half cannot
4461 // release the task before we have completed the second top half
4463}
4464
4466#if KMP_DEBUG
4467 kmp_int32 children = 0;
4468 // Predecrement simulated by "- 1" calculation
4469 children = -1 +
4470#endif
4472 KMP_DEBUG_ASSERT(children >= 0);
4473
4474 // Remove the imaginary children
4476}
4477
4480 kmp_info_t *thread = __kmp_threads[gtid];
4481
4484 1); // top half must run before bottom half
4485
4486 // We need to wait to make sure the top half is finished
4487 // Spinning here should be ok as this should happen quickly
4488 while ((KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) &
4489 PROXY_TASK_FLAG) > 0)
4490 ;
4491
4492 __kmp_release_deps(gtid, taskdata);
4493 __kmp_free_task_and_ancestors(gtid, taskdata, thread);
4494}
4495
4496/*!
4497@ingroup TASKING
4498@param gtid Global Thread ID of encountering thread
4499@param ptask Task which execution is completed
4500
4501Execute the completion of a proxy task from a thread of that is part of the
4502team. Run first and bottom halves directly.
4503*/
4505 KMP_DEBUG_ASSERT(ptask != NULL);
4507 KA_TRACE(
4508 10, ("__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n",
4509 gtid, taskdata));
4512
4516
4517 KA_TRACE(10,
4518 ("__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n",
4519 gtid, taskdata));
4520}
4521
4523 KMP_DEBUG_ASSERT(ptask != NULL);
4525
4526 // Enqueue task to complete bottom half completion from a thread within the
4527 // corresponding team
4528 kmp_team_t *team = taskdata->td_team;
4529 kmp_int32 nthreads = team->t.t_nproc;
4530 kmp_info_t *thread;
4531
4532 // This should be similar to start_k = __kmp_get_random( thread ) % nthreads
4533 // but we cannot use __kmp_get_random here
4534 kmp_int32 start_k = start % nthreads;
4535 kmp_int32 pass = 1;
4536 kmp_int32 k = start_k;
4537
4538 do {
4539 // For now we're just linearly trying to find a thread
4540 thread = team->t.t_threads[k];
4541 k = (k + 1) % nthreads;
4542
4543 // we did a full pass through all the threads
4544 if (k == start_k)
4545 pass = pass << 1;
4546
4547 } while (!__kmp_give_task(thread, k, ptask, pass));
4548
4550 // awake at least one thread to execute given task
4551 for (int i = 0; i < nthreads; ++i) {
4552 thread = team->t.t_threads[i];
4553 if (thread->th.th_sleep_loc != NULL) {
4555 break;
4556 }
4557 }
4558 }
4559}
4560
4561/*!
4562@ingroup TASKING
4563@param ptask Task which execution is completed
4564
4565Execute the completion of a proxy task from a thread that could not belong to
4566the team.
4567*/
4569 KMP_DEBUG_ASSERT(ptask != NULL);
4571
4572 KA_TRACE(
4573 10,
4574 ("__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n",
4575 taskdata));
4576
4578
4580
4582
4584
4585 KA_TRACE(
4586 10,
4587 ("__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n",
4588 taskdata));
4589}
4590
4592 kmp_task_t *task) {
4598 }
4599 return &td->td_allow_completion_event;
4600}
4601
4603 if (event->type == KMP_EVENT_ALLOW_COMPLETION) {
4604 kmp_task_t *ptask = event->ed.task;
4606 bool detached = false;
4607 int gtid = __kmp_get_gtid();
4608
4609 // The associated task might have completed or could be completing at this
4610 // point.
4611 // We need to take the lock to avoid races
4612 __kmp_acquire_tas_lock(&event->lock, gtid);
4613 if (taskdata->td_flags.proxy == TASK_PROXY) {
4614 detached = true;
4615 } else {
4616#if OMPT_SUPPORT
4617 // The OMPT event must occur under mutual exclusion,
4618 // otherwise the tool might access ptask after free
4620 __ompt_task_finish(ptask, NULL, ompt_task_early_fulfill);
4621#endif
4622 }
4624 __kmp_release_tas_lock(&event->lock, gtid);
4625
4626 if (detached) {
4627#if OMPT_SUPPORT
4628 // We free ptask afterwards and know the task is finished,
4629 // so locking is not necessary
4631 __ompt_task_finish(ptask, NULL, ompt_task_late_fulfill);
4632#endif
4633 // If the task detached complete the proxy task
4634 if (gtid >= 0) {
4635 kmp_team_t *team = taskdata->td_team;
4636 kmp_info_t *thread = __kmp_get_thread();
4637 if (thread->th.th_team == team) {
4639 return;
4640 }
4641 }
4642
4643 // fallback
4645 }
4646 }
4647}
4648
4649// __kmp_task_dup_alloc: Allocate the taskdata and make a copy of source task
4650// for taskloop
4651//
4652// thread: allocating thread
4653// task_src: pointer to source task to be duplicated
4654// taskloop_recur: used only when dealing with taskgraph,
4655// indicating whether we need to update task->td_task_id
4656// returns: a pointer to the allocated kmp_task_t structure (task).
4658#if OMPX_TASKGRAPH
4659 , int taskloop_recur
4660#endif
4661) {
4663 kmp_taskdata_t *taskdata;
4664 kmp_taskdata_t *taskdata_src = KMP_TASK_TO_TASKDATA(task_src);
4665 kmp_taskdata_t *parent_task = taskdata_src->td_parent; // same parent task
4666 size_t shareds_offset;
4667 size_t task_size;
4668
4669 KA_TRACE(10, ("__kmp_task_dup_alloc(enter): Th %p, source task %p\n", thread,
4670 task_src));
4671 KMP_DEBUG_ASSERT(taskdata_src->td_flags.proxy ==
4672 TASK_FULL); // it should not be proxy task
4674 task_size = taskdata_src->td_size_alloc;
4675
4676 // Allocate a kmp_taskdata_t block and a kmp_task_t block.
4677 KA_TRACE(30, ("__kmp_task_dup_alloc: Th %p, malloc size %ld\n", thread,
4678 task_size));
4679#if USE_FAST_MEMORY
4680 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, task_size);
4681#else
4682 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, task_size);
4683#endif /* USE_FAST_MEMORY */
4684 KMP_MEMCPY(taskdata, taskdata_src, task_size);
4685
4686 task = KMP_TASKDATA_TO_TASK(taskdata);
4687
4688 // Initialize new task (only specific fields not affected by memcpy)
4689#if OMPX_TASKGRAPH
4690 if (!taskdata->is_taskgraph || taskloop_recur)
4691 taskdata->td_task_id = KMP_GEN_TASK_ID();
4692 else if (taskdata->is_taskgraph &&
4693 __kmp_tdg_is_recording(taskdata_src->tdg->tdg_status))
4694 taskdata->td_task_id = KMP_ATOMIC_INC(&__kmp_tdg_task_id);
4695#else
4696 taskdata->td_task_id = KMP_GEN_TASK_ID();
4697#endif
4698 if (task->shareds != NULL) { // need setup shareds pointer
4699 shareds_offset = (char *)task_src->shareds - (char *)taskdata_src;
4700 task->shareds = &((char *)taskdata)[shareds_offset];
4701 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (sizeof(void *) - 1)) ==
4702 0);
4703 }
4704 taskdata->td_alloc_thread = thread;
4705 taskdata->td_parent = parent_task;
4706 // task inherits the taskgroup from the parent task
4707 taskdata->td_taskgroup = parent_task->td_taskgroup;
4708 // tied task needs to initialize the td_last_tied at creation,
4709 // untied one does this when it is scheduled for execution
4710 if (taskdata->td_flags.tiedness == TASK_TIED)
4711 taskdata->td_last_tied = taskdata;
4712
4713 // Only need to keep track of child task counts if team parallel and tasking
4714 // not serialized
4715 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
4717 if (parent_task->td_taskgroup)
4718 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
4719 // Only need to keep track of allocated child tasks for explicit tasks since
4720 // implicit not deallocated
4721 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT)
4723 }
4724
4725 KA_TRACE(20,
4726 ("__kmp_task_dup_alloc(exit): Th %p, created task %p, parent=%p\n",
4727 thread, taskdata, taskdata->td_parent));
4728#if OMPT_SUPPORT
4730 __ompt_task_init(taskdata, thread->th.th_info.ds.ds_gtid);
4731#endif
4732 return task;
4733}
4734
4735// Routine optionally generated by the compiler for setting the lastprivate flag
4736// and calling needed constructors for private/firstprivate objects
4737// (used to form taskloop tasks from pattern task)
4738// Parameters: dest task, src task, lastprivate flag.
4740
4741KMP_BUILD_ASSERT(sizeof(long) == 4 || sizeof(long) == 8);
4742
4743// class to encapsulate manipulating loop bounds in a taskloop task.
4744// this abstracts away the Intel vs GOMP taskloop interface for setting/getting
4745// the loop bound variables.
4748 const kmp_taskdata_t *taskdata;
4749 size_t lower_offset;
4750 size_t upper_offset;
4751
4752public:
4754 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(task)),
4755 lower_offset((char *)lb - (char *)task),
4756 upper_offset((char *)ub - (char *)task) {
4757 KMP_DEBUG_ASSERT((char *)lb > (char *)_task);
4758 KMP_DEBUG_ASSERT((char *)ub > (char *)_task);
4759 }
4761 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(_task)),
4762 lower_offset(bounds.lower_offset), upper_offset(bounds.upper_offset) {}
4763 size_t get_lower_offset() const { return lower_offset; }
4764 size_t get_upper_offset() const { return upper_offset; }
4766 kmp_int64 retval;
4767#if defined(KMP_GOMP_COMPAT)
4768 // Intel task just returns the lower bound normally
4769 if (!taskdata->td_flags.native) {
4770 retval = *(kmp_int64 *)((char *)task + lower_offset);
4771 } else {
4772 // GOMP task has to take into account the sizeof(long)
4773 if (taskdata->td_size_loop_bounds == 4) {
4775 retval = (kmp_int64)*lb;
4776 } else {
4778 retval = (kmp_int64)*lb;
4779 }
4780 }
4781#else
4782 (void)taskdata;
4783 retval = *(kmp_int64 *)((char *)task + lower_offset);
4784#endif // defined(KMP_GOMP_COMPAT)
4785 return retval;
4786 }
4788 kmp_int64 retval;
4789#if defined(KMP_GOMP_COMPAT)
4790 // Intel task just returns the upper bound normally
4791 if (!taskdata->td_flags.native) {
4792 retval = *(kmp_int64 *)((char *)task + upper_offset);
4793 } else {
4794 // GOMP task has to take into account the sizeof(long)
4795 if (taskdata->td_size_loop_bounds == 4) {
4796 kmp_int32 *ub = RCAST(kmp_int32 *, task->shareds) + 1;
4797 retval = (kmp_int64)*ub;
4798 } else {
4799 kmp_int64 *ub = RCAST(kmp_int64 *, task->shareds) + 1;
4800 retval = (kmp_int64)*ub;
4801 }
4802 }
4803#else
4804 retval = *(kmp_int64 *)((char *)task + upper_offset);
4805#endif // defined(KMP_GOMP_COMPAT)
4806 return retval;
4807 }
4809#if defined(KMP_GOMP_COMPAT)
4810 // Intel task just sets the lower bound normally
4811 if (!taskdata->td_flags.native) {
4812 *(kmp_uint64 *)((char *)task + lower_offset) = lb;
4813 } else {
4814 // GOMP task has to take into account the sizeof(long)
4815 if (taskdata->td_size_loop_bounds == 4) {
4816 kmp_uint32 *lower = RCAST(kmp_uint32 *, task->shareds);
4817 *lower = (kmp_uint32)lb;
4818 } else {
4819 kmp_uint64 *lower = RCAST(kmp_uint64 *, task->shareds);
4820 *lower = (kmp_uint64)lb;
4821 }
4822 }
4823#else
4824 *(kmp_uint64 *)((char *)task + lower_offset) = lb;
4825#endif // defined(KMP_GOMP_COMPAT)
4826 }
4828#if defined(KMP_GOMP_COMPAT)
4829 // Intel task just sets the upper bound normally
4830 if (!taskdata->td_flags.native) {
4831 *(kmp_uint64 *)((char *)task + upper_offset) = ub;
4832 } else {
4833 // GOMP task has to take into account the sizeof(long)
4834 if (taskdata->td_size_loop_bounds == 4) {
4835 kmp_uint32 *upper = RCAST(kmp_uint32 *, task->shareds) + 1;
4836 *upper = (kmp_uint32)ub;
4837 } else {
4838 kmp_uint64 *upper = RCAST(kmp_uint64 *, task->shareds) + 1;
4839 *upper = (kmp_uint64)ub;
4840 }
4841 }
4842#else
4843 *(kmp_uint64 *)((char *)task + upper_offset) = ub;
4844#endif // defined(KMP_GOMP_COMPAT)
4845 }
4846};
4847
4848// __kmp_taskloop_linear: Start tasks of the taskloop linearly
4849//
4850// loc Source location information
4851// gtid Global thread ID
4852// task Pattern task, exposes the loop iteration range
4853// lb Pointer to loop lower bound in task structure
4854// ub Pointer to loop upper bound in task structure
4855// st Loop stride
4856// ub_glob Global upper bound (used for lastprivate check)
4857// num_tasks Number of tasks to execute
4858// grainsize Number of loop iterations per task
4859// extras Number of chunks with grainsize+1 iterations
4860// last_chunk Reduction of grainsize for last task
4861// tc Iterations count
4862// task_dup Tasks duplication routine
4863// codeptr_ra Return address for OMPT events
4865 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
4866 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
4867 kmp_uint64 grainsize, kmp_uint64 extras,
4868 kmp_int64 last_chunk, kmp_uint64 tc,
4869#if OMPT_SUPPORT
4870 void *codeptr_ra,
4871#endif
4872 void *task_dup) {
4873 KMP_COUNT_BLOCK(OMP_TASKLOOP);
4874 KMP_TIME_PARTITIONED_BLOCK(OMP_taskloop_scheduling);
4875 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
4876 // compiler provides global bounds here
4877 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
4878 kmp_uint64 lower = task_bounds.get_lb();
4879 kmp_uint64 upper = task_bounds.get_ub();
4880 kmp_uint64 i;
4881 kmp_info_t *thread = __kmp_threads[gtid];
4882 kmp_taskdata_t *current_task = thread->th.th_current_task;
4883 kmp_task_t *next_task;
4884 kmp_int32 lastpriv = 0;
4885
4886 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
4887 (last_chunk < 0 ? last_chunk : extras));
4888 KMP_DEBUG_ASSERT(num_tasks > extras);
4889 KMP_DEBUG_ASSERT(num_tasks > 0);
4890 KA_TRACE(20, ("__kmp_taskloop_linear: T#%d: %lld tasks, grainsize %lld, "
4891 "extras %lld, last_chunk %lld, i=%lld,%lld(%d)%lld, dup %p\n",
4892 gtid, num_tasks, grainsize, extras, last_chunk, lower, upper,
4893 ub_glob, st, task_dup));
4894
4895 // Launch num_tasks tasks, assign grainsize iterations each task
4896 for (i = 0; i < num_tasks; ++i) {
4897 kmp_uint64 chunk_minus_1;
4898 if (extras == 0) {
4899 chunk_minus_1 = grainsize - 1;
4900 } else {
4901 chunk_minus_1 = grainsize;
4902 --extras; // first extras iterations get bigger chunk (grainsize+1)
4903 }
4904 upper = lower + st * chunk_minus_1;
4905 if (upper > *ub) {
4906 upper = *ub;
4907 }
4908 if (i == num_tasks - 1) {
4909 // schedule the last task, set lastprivate flag if needed
4910 if (st == 1) { // most common case
4911 KMP_DEBUG_ASSERT(upper == *ub);
4912 if (upper == ub_glob)
4913 lastpriv = 1;
4914 } else if (st > 0) { // positive loop stride
4915 KMP_DEBUG_ASSERT((kmp_uint64)st > *ub - upper);
4916 if ((kmp_uint64)st > ub_glob - upper)
4917 lastpriv = 1;
4918 } else { // negative loop stride
4919 KMP_DEBUG_ASSERT(upper + st < *ub);
4920 if (upper - ub_glob < (kmp_uint64)(-st))
4921 lastpriv = 1;
4922 }
4923 }
4924
4925#if OMPX_TASKGRAPH
4926 next_task = __kmp_task_dup_alloc(thread, task, /* taskloop_recur */ 0);
4927#else
4928 next_task = __kmp_task_dup_alloc(thread, task); // allocate new task
4929#endif
4930
4931 kmp_taskdata_t *next_taskdata = KMP_TASK_TO_TASKDATA(next_task);
4932 kmp_taskloop_bounds_t next_task_bounds =
4933 kmp_taskloop_bounds_t(next_task, task_bounds);
4934
4935 // adjust task-specific bounds
4936 next_task_bounds.set_lb(lower);
4937 if (next_taskdata->td_flags.native) {
4938 next_task_bounds.set_ub(upper + (st > 0 ? 1 : -1));
4939 } else {
4940 next_task_bounds.set_ub(upper);
4941 }
4942 if (ptask_dup != NULL) // set lastprivate flag, construct firstprivates,
4943 // etc.
4944 ptask_dup(next_task, task, lastpriv);
4945 KA_TRACE(40,
4946 ("__kmp_taskloop_linear: T#%d; task #%llu: task %p: lower %lld, "
4947 "upper %lld stride %lld, (offsets %p %p)\n",
4948 gtid, i, next_task, lower, upper, st,
4949 next_task_bounds.get_lower_offset(),
4950 next_task_bounds.get_upper_offset()));
4951#if OMPT_SUPPORT
4952 __kmp_omp_taskloop_task(NULL, gtid, next_task,
4953 codeptr_ra); // schedule new task
4954#if OMPT_OPTIONAL
4955 if (ompt_enabled.ompt_callback_dispatch) {
4956 OMPT_GET_DISPATCH_CHUNK(next_taskdata->ompt_task_info.dispatch_chunk,
4957 lower, upper, st);
4958 }
4959#endif // OMPT_OPTIONAL
4960#else
4961 __kmp_omp_task(gtid, next_task, true); // schedule new task
4962#endif
4963 lower = upper + st; // adjust lower bound for the next iteration
4964 }
4965 // free the pattern task and exit
4966 __kmp_task_start(gtid, task, current_task); // make internal bookkeeping
4967 // do not execute the pattern task, just do internal bookkeeping
4968 __kmp_task_finish<false>(gtid, task, current_task);
4969}
4970
4971// Structure to keep taskloop parameters for auxiliary task
4972// kept in the shareds of the task structure.
4973typedef struct __taskloop_params {
4986#if OMPT_SUPPORT
4987 void *codeptr_ra;
4988#endif
4990
4994 kmp_uint64,
4995#if OMPT_SUPPORT
4996 void *,
4997#endif
4998 void *);
4999
5000// Execute part of the taskloop submitted as a task.
5001int __kmp_taskloop_task(int gtid, void *ptask) {
5003 (__taskloop_params_t *)((kmp_task_t *)ptask)->shareds;
5004 kmp_task_t *task = p->task;
5005 kmp_uint64 *lb = p->lb;
5006 kmp_uint64 *ub = p->ub;
5007 void *task_dup = p->task_dup;
5008 // p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
5009 kmp_int64 st = p->st;
5010 kmp_uint64 ub_glob = p->ub_glob;
5011 kmp_uint64 num_tasks = p->num_tasks;
5012 kmp_uint64 grainsize = p->grainsize;
5013 kmp_uint64 extras = p->extras;
5014 kmp_int64 last_chunk = p->last_chunk;
5015 kmp_uint64 tc = p->tc;
5016 kmp_uint64 num_t_min = p->num_t_min;
5017#if OMPT_SUPPORT
5018 void *codeptr_ra = p->codeptr_ra;
5019#endif
5020#if KMP_DEBUG
5022 KMP_DEBUG_ASSERT(task != NULL);
5023 KA_TRACE(20,
5024 ("__kmp_taskloop_task: T#%d, task %p: %lld tasks, grainsize"
5025 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
5026 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
5027 st, task_dup));
5028#endif
5029 KMP_DEBUG_ASSERT(num_tasks * 2 + 1 > num_t_min);
5030 if (num_tasks > num_t_min)
5031 __kmp_taskloop_recur(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
5032 grainsize, extras, last_chunk, tc, num_t_min,
5033#if OMPT_SUPPORT
5034 codeptr_ra,
5035#endif
5036 task_dup);
5037 else
5038 __kmp_taskloop_linear(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
5039 grainsize, extras, last_chunk, tc,
5040#if OMPT_SUPPORT
5041 codeptr_ra,
5042#endif
5043 task_dup);
5044
5045 KA_TRACE(40, ("__kmp_taskloop_task(exit): T#%d\n", gtid));
5046 return 0;
5047}
5048
5049// Schedule part of the taskloop as a task,
5050// execute the rest of the taskloop.
5051//
5052// loc Source location information
5053// gtid Global thread ID
5054// task Pattern task, exposes the loop iteration range
5055// lb Pointer to loop lower bound in task structure
5056// ub Pointer to loop upper bound in task structure
5057// st Loop stride
5058// ub_glob Global upper bound (used for lastprivate check)
5059// num_tasks Number of tasks to execute
5060// grainsize Number of loop iterations per task
5061// extras Number of chunks with grainsize+1 iterations
5062// last_chunk Reduction of grainsize for last task
5063// tc Iterations count
5064// num_t_min Threshold to launch tasks recursively
5065// task_dup Tasks duplication routine
5066// codeptr_ra Return address for OMPT events
5068 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
5069 kmp_uint64 ub_glob, kmp_uint64 num_tasks,
5070 kmp_uint64 grainsize, kmp_uint64 extras,
5071 kmp_int64 last_chunk, kmp_uint64 tc,
5072 kmp_uint64 num_t_min,
5073#if OMPT_SUPPORT
5074 void *codeptr_ra,
5075#endif
5076 void *task_dup) {
5078 KMP_DEBUG_ASSERT(task != NULL);
5079 KMP_DEBUG_ASSERT(num_tasks > num_t_min);
5080 KA_TRACE(20,
5081 ("__kmp_taskloop_recur: T#%d, task %p: %lld tasks, grainsize"
5082 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n",
5083 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub,
5084 st, task_dup));
5085 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
5086 kmp_uint64 lower = *lb;
5087 kmp_info_t *thread = __kmp_threads[gtid];
5088 // kmp_taskdata_t *current_task = thread->th.th_current_task;
5089 kmp_task_t *next_task;
5090 size_t lower_offset =
5091 (char *)lb - (char *)task; // remember offset of lb in the task structure
5092 size_t upper_offset =
5093 (char *)ub - (char *)task; // remember offset of ub in the task structure
5094
5095 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
5096 (last_chunk < 0 ? last_chunk : extras));
5097 KMP_DEBUG_ASSERT(num_tasks > extras);
5098 KMP_DEBUG_ASSERT(num_tasks > 0);
5099
5100 // split the loop in two halves
5101 kmp_uint64 lb1, ub0, tc0, tc1, ext0, ext1;
5102 kmp_int64 last_chunk0 = 0, last_chunk1 = 0;
5103 kmp_uint64 gr_size0 = grainsize;
5104 kmp_uint64 n_tsk0 = num_tasks >> 1; // num_tasks/2 to execute
5105 kmp_uint64 n_tsk1 = num_tasks - n_tsk0; // to schedule as a task
5106 if (last_chunk < 0) {
5107 ext0 = ext1 = 0;
5108 last_chunk1 = last_chunk;
5109 tc0 = grainsize * n_tsk0;
5110 tc1 = tc - tc0;
5111 } else if (n_tsk0 <= extras) {
5112 gr_size0++; // integrate extras into grainsize
5113 ext0 = 0; // no extra iters in 1st half
5114 ext1 = extras - n_tsk0; // remaining extras
5115 tc0 = gr_size0 * n_tsk0;
5116 tc1 = tc - tc0;
5117 } else { // n_tsk0 > extras
5118 ext1 = 0; // no extra iters in 2nd half
5119 ext0 = extras;
5120 tc1 = grainsize * n_tsk1;
5121 tc0 = tc - tc1;
5122 }
5123 ub0 = lower + st * (tc0 - 1);
5124 lb1 = ub0 + st;
5125
5126 // create pattern task for 2nd half of the loop
5127#if OMPX_TASKGRAPH
5128 next_task = __kmp_task_dup_alloc(thread, task,
5129 /* taskloop_recur */ 1);
5130#else
5131 next_task = __kmp_task_dup_alloc(thread, task); // duplicate the task
5132#endif
5133 // adjust lower bound (upper bound is not changed) for the 2nd half
5134 *(kmp_uint64 *)((char *)next_task + lower_offset) = lb1;
5135 if (ptask_dup != NULL) // construct firstprivates, etc.
5136 ptask_dup(next_task, task, 0);
5137 *ub = ub0; // adjust upper bound for the 1st half
5138
5139 // create auxiliary task for 2nd half of the loop
5140 // make sure new task has same parent task as the pattern task
5141 kmp_taskdata_t *current_task = thread->th.th_current_task;
5142 thread->th.th_current_task = taskdata->td_parent;
5143 kmp_task_t *new_task =
5144 __kmpc_omp_task_alloc(loc, gtid, 1, 3 * sizeof(void *),
5146 // restore current task
5147 thread->th.th_current_task = current_task;
5149 p->task = next_task;
5150 p->lb = (kmp_uint64 *)((char *)next_task + lower_offset);
5151 p->ub = (kmp_uint64 *)((char *)next_task + upper_offset);
5152 p->task_dup = task_dup;
5153 p->st = st;
5154 p->ub_glob = ub_glob;
5155 p->num_tasks = n_tsk1;
5156 p->grainsize = grainsize;
5157 p->extras = ext1;
5158 p->last_chunk = last_chunk1;
5159 p->tc = tc1;
5160 p->num_t_min = num_t_min;
5161#if OMPT_SUPPORT
5162 p->codeptr_ra = codeptr_ra;
5163#endif
5164
5165#if OMPX_TASKGRAPH
5166 kmp_taskdata_t *new_task_data = KMP_TASK_TO_TASKDATA(new_task);
5167 new_task_data->tdg = taskdata->tdg;
5168 new_task_data->is_taskgraph = 0;
5169#endif
5170
5171#if OMPT_SUPPORT
5172 // schedule new task with correct return address for OMPT events
5173 __kmp_omp_taskloop_task(NULL, gtid, new_task, codeptr_ra);
5174#else
5175 __kmp_omp_task(gtid, new_task, true); // schedule new task
5176#endif
5177
5178 // execute the 1st half of current subrange
5179 if (n_tsk0 > num_t_min)
5180 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0, gr_size0,
5181 ext0, last_chunk0, tc0, num_t_min,
5182#if OMPT_SUPPORT
5183 codeptr_ra,
5184#endif
5185 task_dup);
5186 else
5187 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0,
5188 gr_size0, ext0, last_chunk0, tc0,
5189#if OMPT_SUPPORT
5190 codeptr_ra,
5191#endif
5192 task_dup);
5193
5194 KA_TRACE(40, ("__kmp_taskloop_recur(exit): T#%d\n", gtid));
5195}
5196
5197static void __kmp_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int if_val,
5198 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
5199 int nogroup, int sched, kmp_uint64 grainsize,
5200 int modifier, void *task_dup) {
5202 KMP_DEBUG_ASSERT(task != NULL);
5203 if (nogroup == 0) {
5204#if OMPT_SUPPORT && OMPT_OPTIONAL
5205 OMPT_STORE_RETURN_ADDRESS(gtid);
5206#endif
5207 __kmpc_taskgroup(loc, gtid);
5208 }
5209
5210#if OMPX_TASKGRAPH
5211 KMP_ATOMIC_DEC(&__kmp_tdg_task_id);
5212#endif
5213 // =========================================================================
5214 // calculate loop parameters
5215 kmp_taskloop_bounds_t task_bounds(task, lb, ub);
5216 kmp_uint64 tc;
5217 // compiler provides global bounds here
5218 kmp_uint64 lower = task_bounds.get_lb();
5219 kmp_uint64 upper = task_bounds.get_ub();
5220 kmp_uint64 ub_glob = upper; // global upper used to calc lastprivate flag
5221 kmp_uint64 num_tasks = 0, extras = 0;
5222 kmp_int64 last_chunk =
5223 0; // reduce grainsize of last task by last_chunk in strict mode
5224 kmp_uint64 num_tasks_min = __kmp_taskloop_min_tasks;
5225 kmp_info_t *thread = __kmp_threads[gtid];
5226 kmp_taskdata_t *current_task = thread->th.th_current_task;
5227
5228 KA_TRACE(20, ("__kmp_taskloop: T#%d, task %p, lb %lld, ub %lld, st %lld, "
5229 "grain %llu(%d, %d), dup %p\n",
5230 gtid, taskdata, lower, upper, st, grainsize, sched, modifier,
5231 task_dup));
5232
5233 // compute trip count
5234 if (st == 1) { // most common case
5235 tc = upper - lower + 1;
5236 } else if (st < 0) {
5237 tc = (lower - upper) / (-st) + 1;
5238 } else { // st > 0
5239 tc = (upper - lower) / st + 1;
5240 }
5241 if (tc == 0) {
5242 KA_TRACE(20, ("__kmp_taskloop(exit): T#%d zero-trip loop\n", gtid));
5243 // free the pattern task and exit
5244 __kmp_task_start(gtid, task, current_task);
5245 // do not execute anything for zero-trip loop
5246 __kmp_task_finish<false>(gtid, task, current_task);
5247 return;
5248 }
5249
5250#if OMPT_SUPPORT && OMPT_OPTIONAL
5251 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
5253 if (ompt_enabled.ompt_callback_work) {
5254 ompt_callbacks.ompt_callback(ompt_callback_work)(
5255 ompt_work_taskloop, ompt_scope_begin, &(team_info->parallel_data),
5256 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
5257 }
5258#endif
5259
5260 if (num_tasks_min == 0)
5261 // TODO: can we choose better default heuristic?
5262 num_tasks_min =
5263 KMP_MIN(thread->th.th_team_nproc * 10, INITIAL_TASK_DEQUE_SIZE);
5264
5265 // compute num_tasks/grainsize based on the input provided
5266 switch (sched) {
5267 case 0: // no schedule clause specified, we can choose the default
5268 // let's try to schedule (team_size*10) tasks
5269 grainsize = thread->th.th_team_nproc * 10;
5271 case 2: // num_tasks provided
5272 if (grainsize > tc) {
5273 num_tasks = tc; // too big num_tasks requested, adjust values
5274 grainsize = 1;
5275 extras = 0;
5276 } else {
5277 num_tasks = grainsize;
5278 grainsize = tc / num_tasks;
5279 extras = tc % num_tasks;
5280 }
5281 break;
5282 case 1: // grainsize provided
5283 if (grainsize > tc) {
5284 num_tasks = 1;
5285 grainsize = tc; // too big grainsize requested, adjust values
5286 extras = 0;
5287 } else {
5288 if (modifier) {
5289 num_tasks = (tc + grainsize - 1) / grainsize;
5290 last_chunk = tc - (num_tasks * grainsize);
5291 extras = 0;
5292 } else {
5293 num_tasks = tc / grainsize;
5294 // adjust grainsize for balanced distribution of iterations
5295 grainsize = tc / num_tasks;
5296 extras = tc % num_tasks;
5297 }
5298 }
5299 break;
5300 default:
5301 KMP_ASSERT2(0, "unknown scheduling of taskloop");
5302 }
5303
5304 KMP_DEBUG_ASSERT(tc == num_tasks * grainsize +
5305 (last_chunk < 0 ? last_chunk : extras));
5306 KMP_DEBUG_ASSERT(num_tasks > extras);
5307 KMP_DEBUG_ASSERT(num_tasks > 0);
5308 // =========================================================================
5309
5310 // check if clause value first
5311 // Also require GOMP_taskloop to reduce to linear (taskdata->td_flags.native)
5312 if (if_val == 0) { // if(0) specified, mark task as serial
5313 taskdata->td_flags.task_serial = 1;
5314 taskdata->td_flags.tiedness = TASK_TIED; // AC: serial task cannot be untied
5315 // always start serial tasks linearly
5316 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5317 grainsize, extras, last_chunk, tc,
5318#if OMPT_SUPPORT
5320#endif
5321 task_dup);
5322 // !taskdata->td_flags.native => currently force linear spawning of tasks
5323 // for GOMP_taskloop
5324 } else if (num_tasks > num_tasks_min && !taskdata->td_flags.native) {
5325 KA_TRACE(20, ("__kmp_taskloop: T#%d, go recursive: tc %llu, #tasks %llu"
5326 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
5327 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
5328 last_chunk));
5329 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5330 grainsize, extras, last_chunk, tc, num_tasks_min,
5331#if OMPT_SUPPORT
5333#endif
5334 task_dup);
5335 } else {
5336 KA_TRACE(20, ("__kmp_taskloop: T#%d, go linear: tc %llu, #tasks %llu"
5337 "(%lld), grain %llu, extras %llu, last_chunk %lld\n",
5338 gtid, tc, num_tasks, num_tasks_min, grainsize, extras,
5339 last_chunk));
5340 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
5341 grainsize, extras, last_chunk, tc,
5342#if OMPT_SUPPORT
5344#endif
5345 task_dup);
5346 }
5347
5348#if OMPT_SUPPORT && OMPT_OPTIONAL
5349 if (ompt_enabled.ompt_callback_work) {
5350 ompt_callbacks.ompt_callback(ompt_callback_work)(
5351 ompt_work_taskloop, ompt_scope_end, &(team_info->parallel_data),
5352 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
5353 }
5354#endif
5355
5356 if (nogroup == 0) {
5357#if OMPT_SUPPORT && OMPT_OPTIONAL
5358 OMPT_STORE_RETURN_ADDRESS(gtid);
5359#endif
5361 }
5362 KA_TRACE(20, ("__kmp_taskloop(exit): T#%d\n", gtid));
5363}
5364
5365/*!
5366@ingroup TASKING
5367@param loc Source location information
5368@param gtid Global thread ID
5369@param task Task structure
5370@param if_val Value of the if clause
5371@param lb Pointer to loop lower bound in task structure
5372@param ub Pointer to loop upper bound in task structure
5373@param st Loop stride
5374@param nogroup Flag, 1 if nogroup clause specified, 0 otherwise
5375@param sched Schedule specified 0/1/2 for none/grainsize/num_tasks
5376@param grainsize Schedule value if specified
5377@param task_dup Tasks duplication routine
5378
5379Execute the taskloop construct.
5380*/
5381void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int if_val,
5382 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup,
5383 int sched, kmp_uint64 grainsize, void *task_dup) {
5385 KA_TRACE(20, ("__kmpc_taskloop(enter): T#%d\n", gtid));
5386 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
5387 0, task_dup);
5388 KA_TRACE(20, ("__kmpc_taskloop(exit): T#%d\n", gtid));
5389}
5390
5391/*!
5392@ingroup TASKING
5393@param loc Source location information
5394@param gtid Global thread ID
5395@param task Task structure
5396@param if_val Value of the if clause
5397@param lb Pointer to loop lower bound in task structure
5398@param ub Pointer to loop upper bound in task structure
5399@param st Loop stride
5400@param nogroup Flag, 1 if nogroup clause specified, 0 otherwise
5401@param sched Schedule specified 0/1/2 for none/grainsize/num_tasks
5402@param grainsize Schedule value if specified
5403@param modifier Modifier 'strict' for sched, 1 if present, 0 otherwise
5404@param task_dup Tasks duplication routine
5405
5406Execute the taskloop construct.
5407*/
5408void __kmpc_taskloop_5(ident_t *loc, int gtid, kmp_task_t *task, int if_val,
5409 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
5410 int nogroup, int sched, kmp_uint64 grainsize,
5411 int modifier, void *task_dup) {
5413 KA_TRACE(20, ("__kmpc_taskloop_5(enter): T#%d\n", gtid));
5414 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize,
5415 modifier, task_dup);
5416 KA_TRACE(20, ("__kmpc_taskloop_5(exit): T#%d\n", gtid));
5417}
5418
5419/*!
5420@ingroup TASKING
5421@param gtid Global Thread ID of current thread
5422@return Returns a pointer to the thread's current task async handle. If no task
5423is present or gtid is invalid, returns NULL.
5424
5425Acqurires a pointer to the target async handle from the current task.
5426*/
5428 if (gtid == KMP_GTID_DNE)
5429 return NULL;
5430
5431 kmp_info_t *thread = __kmp_thread_from_gtid(gtid);
5432 kmp_taskdata_t *taskdata = thread->th.th_current_task;
5433
5434 if (!taskdata)
5435 return NULL;
5436
5437 return &taskdata->td_target_data.async_handle;
5438}
5439
5440/*!
5441@ingroup TASKING
5442@param gtid Global Thread ID of current thread
5443@return Returns TRUE if the current task being executed of the given thread has
5444a task team allocated to it. Otherwise, returns FALSE.
5445
5446Checks if the current thread has a task team.
5447*/
5449 if (gtid == KMP_GTID_DNE)
5450 return FALSE;
5451
5452 kmp_info_t *thread = __kmp_thread_from_gtid(gtid);
5453 kmp_taskdata_t *taskdata = thread->th.th_current_task;
5454
5455 if (!taskdata)
5456 return FALSE;
5457
5458 return taskdata->td_task_team != NULL;
5459}
5460
5461#if OMPX_TASKGRAPH
5462// __kmp_find_tdg: identify a TDG through its ID
5463// gtid: Global Thread ID
5464// tdg_id: ID of the TDG
5465// returns: If a TDG corresponding to this ID is found and not
5466// its initial state, return the pointer to it, otherwise nullptr
5467static kmp_tdg_info_t *__kmp_find_tdg(kmp_int32 tdg_id) {
5468 kmp_tdg_info_t *res = nullptr;
5469 if (__kmp_max_tdgs == 0)
5470 return res;
5471
5472 if (__kmp_global_tdgs == NULL)
5473 __kmp_global_tdgs = (kmp_tdg_info_t **)__kmp_allocate(
5474 sizeof(kmp_tdg_info_t *) * __kmp_max_tdgs);
5475
5476 if ((__kmp_global_tdgs[tdg_id]) &&
5477 (__kmp_global_tdgs[tdg_id]->tdg_status != KMP_TDG_NONE))
5478 res = __kmp_global_tdgs[tdg_id];
5479 return res;
5480}
5481
5482// __kmp_print_tdg_dot: prints the TDG to a dot file
5483// tdg: ID of the TDG
5484void __kmp_print_tdg_dot(kmp_tdg_info_t *tdg) {
5485 kmp_int32 tdg_id = tdg->tdg_id;
5486 KA_TRACE(10, ("__kmp_print_tdg_dot(enter): T#%d tdg_id=%d \n", gtid, tdg_id));
5487
5488 char file_name[20];
5489 sprintf(file_name, "tdg_%d.dot", tdg_id);
5490 kmp_safe_raii_file_t tdg_file(file_name, "w");
5491
5492 kmp_int32 num_tasks = KMP_ATOMIC_LD_RLX(&tdg->num_tasks);
5493 fprintf(tdg_file,
5494 "digraph TDG {\n"
5495 " compound=true\n"
5496 " subgraph cluster {\n"
5497 " label=TDG_%d\n",
5498 tdg_id);
5499 for (kmp_int32 i = 0; i < num_tasks; i++) {
5500 fprintf(tdg_file, " %d[style=bold]\n", i);
5501 }
5502 fprintf(tdg_file, " }\n");
5503 for (kmp_int32 i = 0; i < num_tasks; i++) {
5504 kmp_int32 nsuccessors = tdg->record_map[i].nsuccessors;
5505 kmp_int32 *successors = tdg->record_map[i].successors;
5506 if (nsuccessors > 0) {
5507 for (kmp_int32 j = 0; j < nsuccessors; j++)
5508 fprintf(tdg_file, " %d -> %d \n", i, successors[j]);
5509 }
5510 }
5511 fprintf(tdg_file, "}");
5512 KA_TRACE(10, ("__kmp_print_tdg_dot(exit): T#%d tdg_id=%d \n", gtid, tdg_id));
5513}
5514
5515// __kmp_start_record: launch the execution of a previous
5516// recorded TDG
5517// gtid: Global Thread ID
5518// tdg: ID of the TDG
5519void __kmp_exec_tdg(kmp_int32 gtid, kmp_tdg_info_t *tdg) {
5520 KMP_DEBUG_ASSERT(tdg->tdg_status == KMP_TDG_READY);
5521 KA_TRACE(10, ("__kmp_exec_tdg(enter): T#%d tdg_id=%d num_roots=%d\n", gtid,
5522 tdg->tdg_id, tdg->num_roots));
5523 kmp_node_info_t *this_record_map = tdg->record_map;
5524 kmp_int32 *this_root_tasks = tdg->root_tasks;
5525 kmp_int32 this_num_roots = tdg->num_roots;
5526 kmp_int32 this_num_tasks = KMP_ATOMIC_LD_RLX(&tdg->num_tasks);
5527
5528 kmp_info_t *thread = __kmp_threads[gtid];
5529 kmp_taskdata_t *parent_task = thread->th.th_current_task;
5530
5531 if (tdg->rec_taskred_data) {
5532 __kmpc_taskred_init(gtid, tdg->rec_num_taskred, tdg->rec_taskred_data);
5533 }
5534
5535 for (kmp_int32 j = 0; j < this_num_tasks; j++) {
5536 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(this_record_map[j].task);
5537
5538 td->td_parent = parent_task;
5539 this_record_map[j].parent_task = parent_task;
5540
5541 kmp_taskgroup_t *parent_taskgroup =
5542 this_record_map[j].parent_task->td_taskgroup;
5543
5544 KMP_ATOMIC_ST_RLX(&this_record_map[j].npredecessors_counter,
5545 this_record_map[j].npredecessors);
5546 KMP_ATOMIC_INC(&this_record_map[j].parent_task->td_incomplete_child_tasks);
5547
5548 if (parent_taskgroup) {
5549 KMP_ATOMIC_INC(&parent_taskgroup->count);
5550 // The taskgroup is different so we must update it
5551 td->td_taskgroup = parent_taskgroup;
5552 } else if (td->td_taskgroup != nullptr) {
5553 // If the parent doesnt have a taskgroup, remove it from the task
5554 td->td_taskgroup = nullptr;
5555 }
5556 if (this_record_map[j].parent_task->td_flags.tasktype == TASK_EXPLICIT)
5557 KMP_ATOMIC_INC(&this_record_map[j].parent_task->td_allocated_child_tasks);
5558 }
5559
5560 for (kmp_int32 j = 0; j < this_num_roots; ++j) {
5561 __kmp_omp_task(gtid, this_record_map[this_root_tasks[j]].task, true);
5562 }
5563 KA_TRACE(10, ("__kmp_exec_tdg(exit): T#%d tdg_id=%d num_roots=%d\n", gtid,
5564 tdg->tdg_id, tdg->num_roots));
5565}
5566
5567// __kmp_start_record: set up a TDG structure and turn the
5568// recording flag to true
5569// gtid: Global Thread ID of the encountering thread
5570// input_flags: Flags associated with the TDG
5571// tdg_id: ID of the TDG to record
5572static inline void __kmp_start_record(kmp_int32 gtid,
5573 kmp_taskgraph_flags_t *flags,
5574 kmp_int32 tdg_id) {
5575 kmp_tdg_info_t *tdg =
5576 (kmp_tdg_info_t *)__kmp_allocate(sizeof(kmp_tdg_info_t));
5577 __kmp_global_tdgs[__kmp_curr_tdg_idx] = tdg;
5578 // Initializing the TDG structure
5579 tdg->tdg_id = tdg_id;
5580 tdg->map_size = INIT_MAPSIZE;
5581 tdg->num_roots = -1;
5582 tdg->root_tasks = nullptr;
5583 tdg->tdg_status = KMP_TDG_RECORDING;
5584 tdg->rec_num_taskred = 0;
5585 tdg->rec_taskred_data = nullptr;
5586 KMP_ATOMIC_ST_RLX(&tdg->num_tasks, 0);
5587
5588 // Initializing the list of nodes in this TDG
5589 kmp_node_info_t *this_record_map =
5590 (kmp_node_info_t *)__kmp_allocate(INIT_MAPSIZE * sizeof(kmp_node_info_t));
5591 for (kmp_int32 i = 0; i < INIT_MAPSIZE; i++) {
5592 kmp_int32 *successorsList =
5593 (kmp_int32 *)__kmp_allocate(__kmp_successors_size * sizeof(kmp_int32));
5594 this_record_map[i].task = nullptr;
5595 this_record_map[i].successors = successorsList;
5596 this_record_map[i].nsuccessors = 0;
5597 this_record_map[i].npredecessors = 0;
5598 this_record_map[i].successors_size = __kmp_successors_size;
5599 KMP_ATOMIC_ST_RLX(&this_record_map[i].npredecessors_counter, 0);
5600 }
5601
5602 __kmp_global_tdgs[__kmp_curr_tdg_idx]->record_map = this_record_map;
5603}
5604
5605// __kmpc_start_record_task: Wrapper around __kmp_start_record to mark
5606// the beginning of the record process of a task region
5607// loc_ref: Location of TDG, not used yet
5608// gtid: Global Thread ID of the encountering thread
5609// input_flags: Flags associated with the TDG
5610// tdg_id: ID of the TDG to record, for now, incremental integer
5611// returns: 1 if we record, otherwise, 0
5612kmp_int32 __kmpc_start_record_task(ident_t *loc_ref, kmp_int32 gtid,
5613 kmp_int32 input_flags, kmp_int32 tdg_id) {
5614
5615 kmp_int32 res;
5616 kmp_taskgraph_flags_t *flags = (kmp_taskgraph_flags_t *)&input_flags;
5617 KA_TRACE(10,
5618 ("__kmpc_start_record_task(enter): T#%d loc=%p flags=%d tdg_id=%d\n",
5619 gtid, loc_ref, input_flags, tdg_id));
5620
5621 if (__kmp_max_tdgs == 0) {
5622 KA_TRACE(
5623 10,
5624 ("__kmpc_start_record_task(abandon): T#%d loc=%p flags=%d tdg_id = %d, "
5625 "__kmp_max_tdgs = 0\n",
5626 gtid, loc_ref, input_flags, tdg_id));
5627 return 1;
5628 }
5629
5630 __kmpc_taskgroup(loc_ref, gtid);
5631 if (kmp_tdg_info_t *tdg = __kmp_find_tdg(tdg_id)) {
5632 // TODO: use re_record flag
5633 __kmp_exec_tdg(gtid, tdg);
5634 res = 0;
5635 } else {
5636 __kmp_curr_tdg_idx = tdg_id;
5637 KMP_DEBUG_ASSERT(__kmp_curr_tdg_idx < __kmp_max_tdgs);
5638 __kmp_start_record(gtid, flags, tdg_id);
5639 __kmp_num_tdg++;
5640 res = 1;
5641 }
5642 KA_TRACE(10, ("__kmpc_start_record_task(exit): T#%d TDG %d starts to %s\n",
5643 gtid, tdg_id, res ? "record" : "execute"));
5644 return res;
5645}
5646
5647// __kmp_end_record: set up a TDG after recording it
5648// gtid: Global thread ID
5649// tdg: Pointer to the TDG
5650void __kmp_end_record(kmp_int32 gtid, kmp_tdg_info_t *tdg) {
5651 // Store roots
5652 kmp_node_info_t *this_record_map = tdg->record_map;
5653 kmp_int32 this_num_tasks = KMP_ATOMIC_LD_RLX(&tdg->num_tasks);
5654 kmp_int32 *this_root_tasks =
5655 (kmp_int32 *)__kmp_allocate(this_num_tasks * sizeof(kmp_int32));
5656 kmp_int32 this_map_size = tdg->map_size;
5657 kmp_int32 this_num_roots = 0;
5658 kmp_info_t *thread = __kmp_threads[gtid];
5659
5660 for (kmp_int32 i = 0; i < this_num_tasks; i++) {
5661 if (this_record_map[i].npredecessors == 0) {
5662 this_root_tasks[this_num_roots++] = i;
5663 }
5664 }
5665
5666 // Update with roots info and mapsize
5667 tdg->map_size = this_map_size;
5668 tdg->num_roots = this_num_roots;
5669 tdg->root_tasks = this_root_tasks;
5670 KMP_DEBUG_ASSERT(tdg->tdg_status == KMP_TDG_RECORDING);
5671 tdg->tdg_status = KMP_TDG_READY;
5672
5673 if (thread->th.th_current_task->td_dephash) {
5674 __kmp_dephash_free(thread, thread->th.th_current_task->td_dephash);
5675 thread->th.th_current_task->td_dephash = NULL;
5676 }
5677
5678 // Reset predecessor counter
5679 for (kmp_int32 i = 0; i < this_num_tasks; i++) {
5680 KMP_ATOMIC_ST_RLX(&this_record_map[i].npredecessors_counter,
5681 this_record_map[i].npredecessors);
5682 }
5683 KMP_ATOMIC_ST_RLX(&__kmp_tdg_task_id, 0);
5684
5685 if (__kmp_tdg_dot)
5686 __kmp_print_tdg_dot(tdg);
5687}
5688
5689// __kmpc_end_record_task: wrapper around __kmp_end_record to mark
5690// the end of recording phase
5691//
5692// loc_ref: Source location information
5693// gtid: Global thread ID
5694// input_flags: Flags attached to the graph
5695// tdg_id: ID of the TDG just finished recording
5696void __kmpc_end_record_task(ident_t *loc_ref, kmp_int32 gtid,
5697 kmp_int32 input_flags, kmp_int32 tdg_id) {
5698 kmp_tdg_info_t *tdg = __kmp_find_tdg(tdg_id);
5699
5700 KA_TRACE(10, ("__kmpc_end_record_task(enter): T#%d loc=%p finishes recording"
5701 " tdg=%d with flags=%d\n",
5702 gtid, loc_ref, tdg_id, input_flags));
5703 if (__kmp_max_tdgs) {
5704 // TODO: use input_flags->nowait
5705 __kmpc_end_taskgroup(loc_ref, gtid);
5706 if (__kmp_tdg_is_recording(tdg->tdg_status))
5707 __kmp_end_record(gtid, tdg);
5708 }
5709 KA_TRACE(10, ("__kmpc_end_record_task(exit): T#%d loc=%p finished recording"
5710 " tdg=%d, its status is now READY\n",
5711 gtid, loc_ref, tdg_id));
5712}
5713#endif
void * target(void *task)
uint8_t kmp_uint8
int task_entry(kmp_int32 gtid, kmp_task_t *task)
struct task * ptask
int result[2]
int execute_tasks(kmp_info_t *this_thr, kmp_int32 gtid, int final_spin, int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj), kmp_int32 is_constrained)
This class safely opens and closes a C-style FILE* object using RAII semantics.
Definition: kmp.h:4689
kmp_uint64 get_ub() const
void set_ub(kmp_uint64 ub)
size_t get_lower_offset() const
void set_lb(kmp_uint64 lb)
size_t get_upper_offset() const
kmp_taskloop_bounds_t(kmp_task_t *_task, const kmp_taskloop_bounds_t &bounds)
kmp_taskloop_bounds_t(kmp_task_t *_task, kmp_uint64 *lb, kmp_uint64 *ub)
kmp_uint64 get_lb() const
kmp_int32(*)(kmp_int32, void *) kmp_routine_entry_t
Definition: common.h:11
int64_t kmp_int64
Definition: common.h:10
struct kmp_taskred_data kmp_taskred_data_t
Internal struct for reduction data item related info saved by the library.
struct kmp_task_red_input kmp_task_red_input_t
Internal struct for reduction data item related info set up by compiler.
struct kmp_taskred_flags kmp_taskred_flags_t
Flags for special info per task reduction item.
struct kmp_taskred_input kmp_taskred_input_t
Internal struct for reduction data item related info set up by compiler.
void * __kmpc_task_reduction_get_th_data(int gtid, void *tskgrp, void *data)
void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int sched, kmp_uint64 grainsize, void *task_dup)
void * __kmpc_task_reduction_modifier_init(ident_t *loc, int gtid, int is_ws, int num, void *data)
void * __kmpc_taskred_modifier_init(ident_t *loc, int gtid, int is_ws, int num, void *data)
bool __kmpc_omp_has_task_team(kmp_int32 gtid)
void __kmpc_proxy_task_completed_ooo(kmp_task_t *ptask)
void __kmpc_task_reduction_modifier_fini(ident_t *loc, int gtid, int is_ws)
kmp_int32 __kmpc_omp_reg_task_with_affinity(ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *new_task, kmp_int32 naffins, kmp_task_affinity_info_t *affin_list)
void __kmpc_taskloop_5(ident_t *loc, int gtid, kmp_task_t *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int sched, kmp_uint64 grainsize, int modifier, void *task_dup)
void * __kmpc_task_reduction_init(int gtid, int num, void *data)
void __kmpc_proxy_task_completed(kmp_int32 gtid, kmp_task_t *ptask)
void * __kmpc_taskred_init(int gtid, int num, void *data)
void ** __kmpc_omp_get_target_async_handle_ptr(kmp_int32 gtid)
void
Definition: ittnotify.h:3324
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_id __itt_string_handle __itt_metadata_type size_t void * data
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t new_size
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance * instance
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t count
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_id __itt_string_handle __itt_metadata_type size_t void ITT_FORMAT p const __itt_domain __itt_id __itt_string_handle const wchar_t size_t ITT_FORMAT lu const __itt_domain __itt_id __itt_relation __itt_id ITT_FORMAT p const wchar_t int ITT_FORMAT __itt_group_mark d __itt_event event
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id parent
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t size
void const char const char int ITT_FORMAT __itt_group_sync p
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_id __itt_string_handle __itt_metadata_type size_t void ITT_FORMAT p const __itt_domain __itt_id __itt_string_handle const wchar_t size_t ITT_FORMAT lu const __itt_domain __itt_id __itt_relation __itt_id tail
#define TASK_UNTIED
Definition: kmp.h:49
#define TASK_NOT_PUSHED
Definition: kmp.h:46
#define __kmp_free(ptr)
Definition: kmp.h:3756
kmp_info_t * __kmp_hidden_helper_main_thread
#define TASK_PROXY
Definition: kmp.h:52
#define KMP_CPU_PAUSE()
Definition: kmp.h:1564
kmp_global_t __kmp_global
Definition: kmp_global.cpp:467
#define TASK_EXPLICIT
Definition: kmp.h:50
void __kmp_hidden_helper_worker_thread_signal()
#define KMP_YIELD_OVERSUB_ELSE_SPIN(count, time)
Definition: kmp.h:1640
#define KMP_MAX_BLOCKTIME
Definition: kmp.h:1228
#define INITIAL_TASK_DEQUE_SIZE
Definition: kmp.h:2817
#define KMP_TASKDATA_TO_TASK(taskdata)
Definition: kmp.h:2437
#define KMP_NOT_SAFE_TO_REAP
Definition: kmp.h:2118
#define TASK_DEQUE_MASK(td)
Definition: kmp.h:2820
unsigned short __kmp_get_random(kmp_info_t *thread)
#define TASK_FULL
Definition: kmp.h:53
kmp_tasking_mode_t __kmp_tasking_mode
Definition: kmp_global.cpp:299
void __kmp_abort_thread(void)
int __kmp_dflt_blocktime
Definition: kmp_global.cpp:158
volatile kmp_info_t * __kmp_thread_pool
Definition: kmp_global.cpp:458
#define KMP_GEN_TASK_ID()
Definition: kmp.h:3680
int __kmp_omp_cancellation
Definition: kmp_global.cpp:215
#define TASK_DEQUE_SIZE(td)
Definition: kmp.h:2819
#define KMP_GTID_TO_SHADOW_GTID(gtid)
Definition: kmp.h:4589
#define __kmp_get_thread()
Definition: kmp.h:3604
#define TASK_CURRENT_NOT_QUEUED
Definition: kmp.h:34
static int __kmp_tid_from_gtid(int gtid)
Definition: kmp.h:3619
#define KMP_MIN(x, y)
Definition: kmp.h:320
volatile int __kmp_init_hidden_helper
Definition: kmp_global.cpp:50
@ KMP_EVENT_UNINITIALIZED
Definition: kmp.h:2588
@ KMP_EVENT_ALLOW_COMPLETION
Definition: kmp.h:2589
volatile int __kmp_init_middle
Definition: kmp_global.cpp:48
#define TASK_DETACHABLE
Definition: kmp.h:54
@ cancel_parallel
Definition: kmp.h:999
@ cancel_taskgroup
Definition: kmp.h:1002
@ cancel_noreq
Definition: kmp.h:998
#define KMP_CHECK_UPDATE(a, b)
Definition: kmp.h:2353
#define TASK_IMPLICIT
Definition: kmp.h:51
#define KMP_TASK_TO_TASKDATA(task)
Definition: kmp.h:2436
union KMP_ALIGN_CACHE kmp_thread_data kmp_thread_data_t
#define TASK_SUCCESSFULLY_PUSHED
Definition: kmp.h:47
#define TASK_TIED
Definition: kmp.h:48
#define __kmp_thread_malloc(th, size)
Definition: kmp.h:3776
void __kmp_middle_initialize(void)
static void copy_icvs(kmp_internal_control_t *dst, kmp_internal_control_t *src)
Definition: kmp.h:2184
#define KMP_TASKING_ENABLED(task_team)
Definition: kmp.h:2441
kmp_info_t ** __kmp_threads
Definition: kmp_global.cpp:450
#define KMP_HIDDEN_HELPER_THREAD(gtid)
Definition: kmp.h:4575
int __kmp_enable_task_throttling
Definition: kmp_global.cpp:356
int __kmp_task_stealing_constraint
Definition: kmp_global.cpp:355
#define KMP_INIT_YIELD(count)
Definition: kmp.h:1567
#define KMP_INIT_BACKOFF(time)
Definition: kmp.h:1570
#define KMP_YIELD(cond)
Definition: kmp.h:1582
volatile int __kmp_init_parallel
Definition: kmp_global.cpp:49
kmp_int32 __kmp_enable_hidden_helper
#define __kmp_allocate(size)
Definition: kmp.h:3754
#define TRUE
Definition: kmp.h:1324
enum library_type __kmp_library
Definition: kmp_global.cpp:143
#define FALSE
Definition: kmp.h:1323
@ tskm_extra_barrier
Definition: kmp.h:2418
@ tskm_task_teams
Definition: kmp.h:2419
@ tskm_immediate_exec
Definition: kmp.h:2417
#define UNLIKELY(x)
Definition: kmp.h:159
kmp_uint64 __kmp_taskloop_min_tasks
Definition: kmp_global.cpp:301
std::atomic< kmp_int32 > __kmp_unexecuted_hidden_helper_tasks
kmp_info_t ** __kmp_hidden_helper_threads
#define __kmp_thread_calloc(th, nelem, elsize)
Definition: kmp.h:3778
bool __kmp_wpolicy_passive
Definition: kmp_global.cpp:160
@ bs_forkjoin_barrier
Definition: kmp.h:2134
void __kmp_hidden_helper_initialize()
#define __kmp_get_gtid()
Definition: kmp.h:3600
kmp_int32 __kmp_max_task_priority
Definition: kmp_global.cpp:300
static void __kmp_assert_valid_gtid(kmp_int32 gtid)
Definition: kmp.h:3644
static kmp_info_t * __kmp_thread_from_gtid(int gtid)
Definition: kmp.h:3634
static int __kmp_gtid_from_thread(const kmp_info_t *thr)
Definition: kmp.h:3629
@ library_throughput
Definition: kmp.h:521
struct kmp_taskdata kmp_taskdata_t
Definition: kmp.h:272
#define KMP_GTID_DNE
Definition: kmp.h:1027
union KMP_ALIGN_CACHE kmp_info kmp_info_t
#define __kmp_thread_free(th, ptr)
Definition: kmp.h:3782
char
KMP_ARCH_X86 KMP_ARCH_X86 KMP_ARCH_X86 KMP_ARCH_X86 KMP_ARCH_X86 KMP_ARCH_X86 KMP_ARCH_X86 KMP_ARCH_X86 KMP_ARCH_X86<<, 2i, 1, KMP_ARCH_X86) ATOMIC_CMPXCHG(fixed2, shr, kmp_int16, 16, > KMP_ARCH_X86 KMP_ARCH_X86 kmp_uint32
#define KE_TRACE(d, x)
Definition: kmp_debug.h:161
#define KA_TRACE(d, x)
Definition: kmp_debug.h:157
#define KMP_DEBUG_USE_VAR(x)
Definition: kmp_debug.h:63
#define KMP_ASSERT(cond)
Definition: kmp_debug.h:59
#define KMP_BUILD_ASSERT(expr)
Definition: kmp_debug.h:26
#define KF_TRACE(d, x)
Definition: kmp_debug.h:162
#define KMP_DEBUG_ASSERT(cond)
Definition: kmp_debug.h:61
#define KMP_ASSERT2(cond, msg)
Definition: kmp_debug.h:60
unsigned long long kmp_uint64
static volatile kmp_i18n_cat_status_t status
Definition: kmp_i18n.cpp:48
#define KMP_FSYNC_RELEASING(obj)
Definition: kmp_itt.h:335
#define KMP_FSYNC_ACQUIRED(obj)
Definition: kmp_itt.h:334
#define KMP_FSYNC_SPIN_ACQUIRED(obj)
Definition: kmp_itt.h:339
#define KMP_FSYNC_CANCEL(obj)
Definition: kmp_itt.h:333
#define KMP_FSYNC_SPIN_PREPARE(obj)
Definition: kmp_itt.h:338
#define USE_ITT_BUILD_ARG(x)
Definition: kmp_itt.h:346
#define KMP_FSYNC_SPIN_INIT(obj, spin)
Definition: kmp_itt.h:337
int __kmp_acquire_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid)
Definition: kmp_lock.cpp:118
void __kmp_init_tas_lock(kmp_tas_lock_t *lck)
Definition: kmp_lock.cpp:186
int __kmp_release_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid)
Definition: kmp_lock.cpp:157
static void __kmp_release_bootstrap_lock(kmp_bootstrap_lock_t *lck)
Definition: kmp_lock.h:535
static int __kmp_test_lock(kmp_lock_t *lck, kmp_int32 gtid)
Definition: kmp_lock.h:563
static int __kmp_acquire_bootstrap_lock(kmp_bootstrap_lock_t *lck)
Definition: kmp_lock.h:527
static void __kmp_release_lock(kmp_lock_t *lck, kmp_int32 gtid)
Definition: kmp_lock.h:567
static void __kmp_init_bootstrap_lock(kmp_bootstrap_lock_t *lck)
Definition: kmp_lock.h:539
#define KMP_BOOTSTRAP_LOCK_INITIALIZER(lock)
Definition: kmp_lock.h:523
#define TCW_PTR(a, b)
Definition: kmp_os.h:1165
#define KMP_ATOMIC_AND(p, v)
Definition: kmp_os.h:1265
#define KMP_SIZE_T_MAX
Definition: kmp_os.h:195
#define KMP_ATOMIC_ST_REL(p, v)
Definition: kmp_os.h:1259
bool __kmp_atomic_compare_store(std::atomic< T > *p, T expected, T desired)
Definition: kmp_os.h:1274
#define TCR_PTR(a)
Definition: kmp_os.h:1164
#define RCAST(type, var)
Definition: kmp_os.h:291
#define CACHE_LINE
Definition: kmp_os.h:339
#define KMP_ATOMIC_LD_ACQ(p)
Definition: kmp_os.h:1257
#define TCW_SYNC_4(a, b)
Definition: kmp_os.h:1144
#define KMP_ATOMIC_ST_RLX(p, v)
Definition: kmp_os.h:1260
#define CCAST(type, var)
Definition: kmp_os.h:290
#define KMP_MB()
Definition: kmp_os.h:1064
kmp_int32 kmp_int
Definition: kmp_os.h:214
#define TCR_4(a)
Definition: kmp_os.h:1135
#define KMP_FALLTHROUGH()
Definition: kmp_os.h:363
#define KMP_ATOMIC_DEC(p)
Definition: kmp_os.h:1268
#define KMP_ATOMIC_LD_RLX(p)
Definition: kmp_os.h:1258
#define KMP_COMPARE_AND_STORE_ACQ32(p, cv, sv)
Definition: kmp_os.h:813
#define TCW_4(a, b)
Definition: kmp_os.h:1136
unsigned long kmp_uintptr_t
Definition: kmp_os.h:205
#define KMP_DLSYM(name)
Definition: kmp_os.h:1300
#define KMP_ATOMIC_OR(p, v)
Definition: kmp_os.h:1266
#define KMP_ATOMIC_INC(p)
Definition: kmp_os.h:1267
#define KMP_MEMCPY
#define KMP_MEMCPY_S(dst, bsz, src, cnt)
Functions for collecting statistics.
#define KMP_PUSH_PARTITIONED_TIMER(name)
Definition: kmp_stats.h:1014
#define KMP_GET_THREAD_STATE()
Definition: kmp_stats.h:1017
#define KMP_POP_PARTITIONED_TIMER()
Definition: kmp_stats.h:1015
#define KMP_SET_THREAD_STATE_BLOCK(state_name)
Definition: kmp_stats.h:1018
#define KMP_TIME_PARTITIONED_BLOCK(name)
Definition: kmp_stats.h:1013
#define KMP_COUNT_BLOCK(n)
Definition: kmp_stats.h:1001
#define i
Definition: kmp_stub.cpp:87
static void __kmp_dephash_free(kmp_info_t *thread, kmp_dephash_t *h)
Definition: kmp_taskdeps.h:83
static void __kmp_dephash_free_entries(kmp_info_t *thread, kmp_dephash_t *h)
Definition: kmp_taskdeps.h:56
static void __kmp_release_deps(kmp_int32 gtid, kmp_taskdata_t *task)
Definition: kmp_taskdeps.h:94
void __kmp_free_task_team(kmp_info_t *thread, kmp_task_team_t *task_team)
template int __kmp_atomic_execute_tasks_64< true, false >(kmp_info_t *, kmp_int32, kmp_atomic_flag_64< true, false > *, int, int *USE_ITT_BUILD_ARG(void *), kmp_int32)
void __kmp_call_init(kmp_taskred_data_t &item, size_t j)
void __kmp_call_init< kmp_task_red_input_t >(kmp_taskred_data_t &item, size_t offset)
#define PROXY_TASK_FLAG
void(* p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32)
void * __kmp_task_reduction_modifier_init(ident_t *loc, int gtid, int is_ws, int num, T *data)
static void __kmp_task_reduction_clean(kmp_info_t *th, kmp_taskgroup_t *tg)
static void __kmpc_omp_task_complete_if0_template(ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task)
kmp_task_t * __kmpc_omp_task_alloc(ident_t *loc_ref, kmp_int32 gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, kmp_routine_entry_t task_entry)
void __kmp_reap_task_teams(void)
kmp_int32 __kmpc_omp_taskyield(ident_t *loc_ref, kmp_int32 gtid, int end_part)
kmp_int32 __kmpc_omp_task(ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *new_task)
static void __kmp_task_start(kmp_int32 gtid, kmp_task_t *task, kmp_taskdata_t *current_task)
void __kmp_pop_task_team_node(kmp_info_t *thread, kmp_team_t *team)
int __kmp_execute_tasks_64(kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64< C, S > *flag, int final_spin, int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj), kmp_int32 is_constrained)
void __kmp_wait_to_unref_task_teams(void)
static kmp_task_team_t * __kmp_allocate_task_team(kmp_info_t *thread, kmp_team_t *team)
void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team)
void __kmp_task_reduction_init_copy(kmp_info_t *thr, int num, T *data, kmp_taskgroup_t *tg, void *reduce_data)
void __kmpc_omp_task_complete_if0(ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task)
static void __kmp_realloc_task_deque(kmp_info_t *thread, kmp_thread_data_t *thread_data)
static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask)
void __kmpc_end_taskgroup(ident_t *loc, int gtid)
void __kmpc_omp_task_begin_if0(ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task)
static void __kmp_first_top_half_finish_proxy(kmp_taskdata_t *taskdata)
static int __kmp_execute_tasks_template(kmp_info_t *thread, kmp_int32 gtid, C *flag, int final_spin, int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj), kmp_int32 is_constrained)
kmp_bootstrap_lock_t __kmp_task_team_lock
static void __kmp_free_task_and_ancestors(kmp_int32 gtid, kmp_taskdata_t *taskdata, kmp_info_t *thread)
static void __kmp_task_finish(kmp_int32 gtid, kmp_task_t *task, kmp_taskdata_t *resumed_task)
static void __kmp_invoke_task(kmp_int32 gtid, kmp_task_t *task, kmp_taskdata_t *current_task)
static kmp_thread_data_t * __kmp_get_priority_deque_data(kmp_task_team_t *task_team, kmp_int32 pri)
template int __kmp_execute_tasks_64< false, true >(kmp_info_t *, kmp_int32, kmp_flag_64< false, true > *, int, int *USE_ITT_BUILD_ARG(void *), kmp_int32)
void __kmp_finish_implicit_task(kmp_info_t *thread)
template int __kmp_execute_tasks_32< false, false >(kmp_info_t *, kmp_int32, kmp_flag_32< false, false > *, int, int *USE_ITT_BUILD_ARG(void *), kmp_int32)
struct __taskloop_params __taskloop_params_t
kmp_event_t * __kmpc_task_allow_completion_event(ident_t *loc_ref, int gtid, kmp_task_t *task)
static void __kmp_free_task_threads_data(kmp_task_team_t *task_team)
void __kmp_assign_orig(kmp_taskred_data_t &item, T &src)
static kmp_int32 __kmpc_omp_taskwait_template(ident_t *loc_ref, kmp_int32 gtid, void *frame_address, void *return_address)
static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg)
static void __kmp_free_task(kmp_int32 gtid, kmp_taskdata_t *taskdata, kmp_info_t *thread)
template int __kmp_execute_tasks_64< true, false >(kmp_info_t *, kmp_int32, kmp_flag_64< true, false > *, int, int *USE_ITT_BUILD_ARG(void *), kmp_int32)
int __kmp_atomic_execute_tasks_64(kmp_info_t *thread, kmp_int32 gtid, kmp_atomic_flag_64< C, S > *flag, int final_spin, int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj), kmp_int32 is_constrained)
void __kmp_task_team_wait(kmp_info_t *this_thr, kmp_team_t *team USE_ITT_BUILD_ARG(void *itt_sync_obj), int wait)
kmp_int32 __kmpc_omp_task_parts(ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *new_task)
static kmp_task_pri_t * __kmp_alloc_task_pri_list()
static void __kmp_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, int modifier, void *task_dup)
kmp_int32 __kmp_omp_taskloop_task(ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *new_task, void *codeptr_ra)
void __kmp_init_implicit_task(ident_t *loc_ref, kmp_info_t *this_thr, kmp_team_t *team, int tid, int set_curr_task)
static kmp_int32 __kmp_push_task(kmp_int32 gtid, kmp_task_t *task)
static kmp_task_t * __kmp_steal_task(kmp_int32 victim_tid, kmp_int32 gtid, kmp_task_team_t *task_team, std::atomic< kmp_int32 > *unfinished_threads, int *thread_finished, kmp_int32 is_constrained)
void * __kmp_task_reduction_init(int gtid, int num, T *data)
static void __kmp_enable_tasking(kmp_task_team_t *task_team, kmp_info_t *this_thr)
static bool __kmp_give_task(kmp_info_t *thread, kmp_int32 tid, kmp_task_t *task, kmp_int32 pass)
int __kmp_execute_tasks_32(kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32< C, S > *flag, int final_spin, int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj), kmp_int32 is_constrained)
static kmp_task_t * __kmp_get_priority_task(kmp_int32 gtid, kmp_task_team_t *task_team, kmp_int32 is_constrained)
void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread, int gtid)
void __kmp_free_implicit_task(kmp_info_t *thread)
int __kmp_execute_tasks_oncore(kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag, int final_spin, int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj), kmp_int32 is_constrained)
static size_t __kmp_round_up_to_val(size_t size, size_t val)
kmp_task_t * __kmpc_omp_target_task_alloc(ident_t *loc_ref, kmp_int32 gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, kmp_routine_entry_t task_entry, kmp_int64 device_id)
void __kmp_fulfill_event(kmp_event_t *event)
void __kmp_assign_orig< kmp_taskred_input_t >(kmp_taskred_data_t &item, kmp_taskred_input_t &src)
static void __kmpc_omp_task_begin_if0_template(ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task, void *frame_address, void *return_address)
void __kmp_assign_orig< kmp_task_red_input_t >(kmp_taskred_data_t &item, kmp_task_red_input_t &src)
static int __kmp_realloc_task_threads_data(kmp_info_t *thread, kmp_task_team_t *task_team)
template int __kmp_atomic_execute_tasks_64< false, true >(kmp_info_t *, kmp_int32, kmp_atomic_flag_64< false, true > *, int, int *USE_ITT_BUILD_ARG(void *), kmp_int32)
static kmp_task_team_t * __kmp_free_task_teams
kmp_task_t * __kmp_task_dup_alloc(kmp_info_t *thread, kmp_task_t *task_src)
kmp_task_t * __kmp_task_alloc(ident_t *loc_ref, kmp_int32 gtid, kmp_tasking_flags_t *flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, kmp_routine_entry_t task_entry)
kmp_int32 __kmpc_omp_taskwait(ident_t *loc_ref, kmp_int32 gtid)
int __kmp_taskloop_task(int gtid, void *ptask)
void __kmp_push_current_task_to_thread(kmp_info_t *this_thr, kmp_team_t *team, int tid)
void __kmp_push_task_team_node(kmp_info_t *thread, kmp_team_t *team)
static void __kmp_second_top_half_finish_proxy(kmp_taskdata_t *taskdata)
static void __kmp_free_task_pri_list(kmp_task_team_t *task_team)
static bool __kmp_task_is_allowed(int gtid, const kmp_int32 is_constrained, const kmp_taskdata_t *tasknew, const kmp_taskdata_t *taskcurr)
static kmp_task_t * __kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid, kmp_task_team_t *task_team, kmp_int32 is_constrained)
void __kmpc_taskgroup(ident_t *loc, int gtid)
kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task, bool serialize_immediate)
static void __kmp_task_team_init(kmp_task_team_t *task_team, kmp_team_t *team)
void __kmp_pop_current_task_from_thread(kmp_info_t *this_thr)
void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team)
static void __kmp_alloc_task_deque(kmp_info_t *thread, kmp_thread_data_t *thread_data)
static void __kmp_free_task_deque(kmp_thread_data_t *thread_data)
static kmp_int32 __kmp_push_priority_task(kmp_int32 gtid, kmp_info_t *thread, kmp_taskdata_t *taskdata, kmp_task_team_t *task_team, kmp_int32 pri)
void __kmpc_give_task(kmp_task_t *ptask, kmp_int32 start=0)
void __kmp_call_init< kmp_taskred_input_t >(kmp_taskred_data_t &item, size_t offset)
void __kmp_taskloop_linear(ident_t *loc, int gtid, kmp_task_t *task, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, kmp_uint64 ub_glob, kmp_uint64 num_tasks, kmp_uint64 grainsize, kmp_uint64 extras, kmp_int64 last_chunk, kmp_uint64 tc, void *task_dup)
void __kmp_taskloop_recur(ident_t *, int, kmp_task_t *, kmp_uint64 *, kmp_uint64 *, kmp_int64, kmp_uint64, kmp_uint64, kmp_uint64, kmp_uint64, kmp_int64, kmp_uint64, kmp_uint64, void *)
static bool __kmp_track_children_task(kmp_taskdata_t *taskdata)
int counter
Definition: kmp_taskloop.c:13
static void __kmp_null_resume_wrapper(kmp_info_t *thr)
__attribute__((noinline))
int32_t kmp_int32
int arr[N][N][N]
#define C
#define res
if(ret)
ompt_callbacks_active_t ompt_enabled
return ret
ompt_callbacks_internal_t ompt_callbacks
#define OMPT_NOINLINE
#define OMPT_GET_RETURN_ADDRESS(level)
#define TASK_TYPE_DETAILS_FORMAT(info)
Definition: ompt-internal.h:47
#define OMPT_GET_FRAME_ADDRESS(level)
ompt_team_info_t * __ompt_get_teaminfo(int depth, int *size)
int __ompt_get_task_info_internal(int ancestor_level, int *type, ompt_data_t **task_data, ompt_frame_t **task_frame, ompt_data_t **parallel_data, int *thread_num)
ompt_task_info_t * __ompt_get_task_info_object(int depth)
static id loc
volatile int flag
kmp_uint64 num_tasks
kmp_uint64 grainsize
kmp_uint64 num_t_min
kmp_task_t * task
kmp_lock_t * mtx_locks[MAX_MTX_DEPS]
Definition: kmp.h:2542
kmp_int32 mtx_num_locks
Definition: kmp.h:2543
kmp_int32 tt_found_proxy_tasks
Definition: kmp.h:2853
KMP_ALIGN_CACHE std::atomic< kmp_int32 > tt_unfinished_threads
Definition: kmp.h:2861
kmp_int32 tt_max_threads
Definition: kmp.h:2852
kmp_int32 tt_nproc
Definition: kmp.h:2851
kmp_bootstrap_lock_t tt_task_pri_lock
Definition: kmp.h:2841
std::atomic< kmp_int32 > tt_num_task_pri
Definition: kmp.h:2855
kmp_bootstrap_lock_t tt_threads_lock
Definition: kmp.h:2837
kmp_int32 tt_untied_task_encountered
Definition: kmp.h:2854
kmp_task_pri_t * tt_task_pri_list
Definition: kmp.h:2842
kmp_int32 tt_hidden_helper_task_encountered
Definition: kmp.h:2858
kmp_thread_data_t * tt_threads_data
Definition: kmp.h:2846
KMP_ALIGN_CACHE volatile kmp_uint32 tt_active
Definition: kmp.h:2865
kmp_task_team_t * tt_next
Definition: kmp.h:2844
kmp_int32 tt_found_tasks
Definition: kmp.h:2848
kmp_tas_lock_t lock
Definition: kmp.h:2594
union kmp_event_t::@12 ed
kmp_task_t * task
Definition: kmp.h:2596
kmp_event_type_t type
Definition: kmp.h:2593
void * async_handle
Definition: kmp.h:2741
kmp_int32 priority
Definition: kmp.h:2830
kmp_task_pri * next
Definition: kmp.h:2831
kmp_thread_data_t td
Definition: kmp.h:2829
Internal struct for reduction data item related info set up by compiler.
void * reduce_shar
shared between tasks item to reduce into
void * reduce_fini
data finalization routine
kmp_taskred_flags_t flags
flags for additional info from compiler
size_t reduce_size
size of data item in bytes
void * reduce_init
data initialization routine (single parameter)
void * reduce_comb
data combiner routine
kmp_task_team_list_t * next
Definition: kmp.h:2876
kmp_task_team_t * task_team
Definition: kmp.h:2875
Definition: kmp.h:2463
void * shareds
pointer to block of pointers to shared vars
Definition: kmp.h:2464
kmp_uint32 td_taskwait_counter
Definition: kmp.h:2756
ident_t * td_taskwait_ident
Definition: kmp.h:2755
kmp_int32 td_level
Definition: kmp.h:2751
kmp_team_t * td_team
Definition: kmp.h:2747
kmp_task_team_t * td_task_team
Definition: kmp.h:2771
kmp_dephash_t * td_dephash
Definition: kmp.h:2768
kmp_taskdata_t * td_parent
Definition: kmp.h:2750
std::atomic< kmp_int32 > td_incomplete_child_tasks
Definition: kmp.h:2764
std::atomic< kmp_int32 > td_untied_count
Definition: kmp.h:2752
kmp_taskgroup_t * td_taskgroup
Definition: kmp.h:2766
kmp_int32 td_task_id
Definition: kmp.h:2745
kmp_info_p * td_alloc_thread
Definition: kmp.h:2748
ident_t * td_ident
Definition: kmp.h:2753
kmp_depnode_t * td_depnode
Definition: kmp.h:2770
kmp_int32 td_taskwait_thread
Definition: kmp.h:2757
kmp_tasking_flags_t td_flags
Definition: kmp.h:2746
kmp_taskdata_t * td_last_tied
Definition: kmp.h:2777
KMP_ALIGN_CACHE kmp_internal_control_t td_icvs
Definition: kmp.h:2759
kmp_event_t td_allow_completion_event
Definition: kmp.h:2782
size_t td_size_alloc
Definition: kmp.h:2772
kmp_target_data_t td_target_data
Definition: kmp.h:2790
KMP_ALIGN_CACHE std::atomic< kmp_int32 > td_allocated_child_tasks
Definition: kmp.h:2761
std::atomic< kmp_int32 > cancel_request
Definition: kmp.h:2482
uintptr_t * gomp_data
Definition: kmp.h:2487
std::atomic< kmp_int32 > count
Definition: kmp.h:2480
void * reduce_data
Definition: kmp.h:2485
struct kmp_taskgroup * parent
Definition: kmp.h:2483
kmp_int32 reduce_num_data
Definition: kmp.h:2486
unsigned priority_specified
Definition: kmp.h:2711
unsigned detachable
Definition: kmp.h:2713
unsigned task_serial
Definition: kmp.h:2719
unsigned merged_if0
Definition: kmp.h:2705
unsigned complete
Definition: kmp.h:2728
unsigned freed
Definition: kmp.h:2729
unsigned executing
Definition: kmp.h:2727
unsigned tasking_ser
Definition: kmp.h:2720
unsigned team_serial
Definition: kmp.h:2722
unsigned native
Definition: kmp.h:2730
unsigned tiedness
Definition: kmp.h:2703
unsigned started
Definition: kmp.h:2726
unsigned destructors_thunk
Definition: kmp.h:2707
unsigned proxy
Definition: kmp.h:2709
unsigned tasktype
Definition: kmp.h:2718
unsigned final
Definition: kmp.h:2704
unsigned hidden_helper
Definition: kmp.h:2714
Internal struct for reduction data item related info saved by the library.
void * reduce_init
data initialization routine (two parameters)
void * reduce_priv
array of thread specific items
void * reduce_pend
end of private data for faster comparison op
void * reduce_comb
data combiner routine
kmp_taskred_flags_t flags
flags for additional info from compiler
void * reduce_fini
data finalization routine
size_t reduce_size
size of data item
void * reduce_shar
shared between tasks item to reduce into
void * reduce_orig
original item (can be used in UDR initializer)
Flags for special info per task reduction item.
unsigned lazy_priv
1 - use lazy alloc/init (e.g.
Internal struct for reduction data item related info set up by compiler.
void * reduce_shar
shared between tasks item to reduce into
void * reduce_fini
data finalization routine
void * reduce_init
data initialization routine (two parameters)
void * reduce_comb
data combiner routine
size_t reduce_size
size of data item
void * reduce_orig
original reduction item used for initialization
kmp_taskred_flags_t flags
flags for additional info from compiler
ompt_data_t task_data
Definition: ompt-internal.h:57
ompt_frame_t frame
Definition: ompt-internal.h:56
ompt_data_t parallel_data
Definition: ompt-internal.h:64
ompt_wait_id_t wait_id
Definition: ompt-internal.h:82
int(* routine)(int, struct task *)
int th
Definition: kmp_taskloop.c:38
void ** shareds
kmp_base_depnode_t dn
Definition: kmp.h:2555
kmp_base_task_team_t tt
Definition: kmp.h:2869
Definition: kmp.h:3215
kmp_base_team_t t
Definition: kmp.h:3216
int __kmp_is_thread_alive(kmp_info_t *th, DWORD *exit_val)