17cc577a4SJonathan Peyton /*
2de4749b7SJonathan Peyton  * kmp_tasking.cpp -- OpenMP 3.0 tasking support.
37cc577a4SJonathan Peyton  */
47cc577a4SJonathan Peyton 
57cc577a4SJonathan Peyton //===----------------------------------------------------------------------===//
67cc577a4SJonathan Peyton //
757b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
857b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
957b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
107cc577a4SJonathan Peyton //
117cc577a4SJonathan Peyton //===----------------------------------------------------------------------===//
127cc577a4SJonathan Peyton 
137cc577a4SJonathan Peyton #include "kmp.h"
147cc577a4SJonathan Peyton #include "kmp_i18n.h"
157cc577a4SJonathan Peyton #include "kmp_itt.h"
167cc577a4SJonathan Peyton #include "kmp_stats.h"
173041982dSJonathan Peyton #include "kmp_wait_release.h"
18cf27e31bSJonathan Peyton #include "kmp_taskdeps.h"
197cc577a4SJonathan Peyton 
207cc577a4SJonathan Peyton #if OMPT_SUPPORT
217cc577a4SJonathan Peyton #include "ompt-specific.h"
227cc577a4SJonathan Peyton #endif
237cc577a4SJonathan Peyton 
247cc577a4SJonathan Peyton #include "tsan_annotations.h"
257cc577a4SJonathan Peyton 
267cc577a4SJonathan Peyton /* forward declaration */
273041982dSJonathan Peyton static void __kmp_enable_tasking(kmp_task_team_t *task_team,
283041982dSJonathan Peyton                                  kmp_info_t *this_thr);
293041982dSJonathan Peyton static void __kmp_alloc_task_deque(kmp_info_t *thread,
303041982dSJonathan Peyton                                    kmp_thread_data_t *thread_data);
313041982dSJonathan Peyton static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
323041982dSJonathan Peyton                                            kmp_task_team_t *task_team);
337cc577a4SJonathan Peyton 
34c3344345SAndrey Churbanov #if OMP_45_ENABLED
357cc577a4SJonathan Peyton static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask);
367cc577a4SJonathan Peyton #endif
377cc577a4SJonathan Peyton 
387cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK
397cc577a4SJonathan Peyton 
407cc577a4SJonathan Peyton //  __kmp_trace_task_stack: print the tied tasks from the task stack in order
417cc577a4SJonathan Peyton //  from top do bottom
427cc577a4SJonathan Peyton //
437cc577a4SJonathan Peyton //  gtid: global thread identifier for thread containing stack
447cc577a4SJonathan Peyton //  thread_data: thread data for task team thread containing stack
457cc577a4SJonathan Peyton //  threshold: value above which the trace statement triggers
467cc577a4SJonathan Peyton //  location: string identifying call site of this function (for trace)
473041982dSJonathan Peyton static void __kmp_trace_task_stack(kmp_int32 gtid,
483041982dSJonathan Peyton                                    kmp_thread_data_t *thread_data,
493041982dSJonathan Peyton                                    int threshold, char *location) {
507cc577a4SJonathan Peyton   kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
517cc577a4SJonathan Peyton   kmp_taskdata_t **stack_top = task_stack->ts_top;
527cc577a4SJonathan Peyton   kmp_int32 entries = task_stack->ts_entries;
537cc577a4SJonathan Peyton   kmp_taskdata_t *tied_task;
547cc577a4SJonathan Peyton 
553041982dSJonathan Peyton   KA_TRACE(
563041982dSJonathan Peyton       threshold,
573041982dSJonathan Peyton       ("__kmp_trace_task_stack(start): location = %s, gtid = %d, entries = %d, "
587cc577a4SJonathan Peyton        "first_block = %p, stack_top = %p \n",
597cc577a4SJonathan Peyton        location, gtid, entries, task_stack->ts_first_block, stack_top));
607cc577a4SJonathan Peyton 
617cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(stack_top != NULL);
627cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(entries > 0);
637cc577a4SJonathan Peyton 
643041982dSJonathan Peyton   while (entries != 0) {
657cc577a4SJonathan Peyton     KMP_DEBUG_ASSERT(stack_top != &task_stack->ts_first_block.sb_block[0]);
667cc577a4SJonathan Peyton     // fix up ts_top if we need to pop from previous block
673041982dSJonathan Peyton     if (entries & TASK_STACK_INDEX_MASK == 0) {
687cc577a4SJonathan Peyton       kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(stack_top);
697cc577a4SJonathan Peyton 
707cc577a4SJonathan Peyton       stack_block = stack_block->sb_prev;
717cc577a4SJonathan Peyton       stack_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE];
727cc577a4SJonathan Peyton     }
737cc577a4SJonathan Peyton 
747cc577a4SJonathan Peyton     // finish bookkeeping
757cc577a4SJonathan Peyton     stack_top--;
767cc577a4SJonathan Peyton     entries--;
777cc577a4SJonathan Peyton 
787cc577a4SJonathan Peyton     tied_task = *stack_top;
797cc577a4SJonathan Peyton 
807cc577a4SJonathan Peyton     KMP_DEBUG_ASSERT(tied_task != NULL);
817cc577a4SJonathan Peyton     KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
827cc577a4SJonathan Peyton 
833041982dSJonathan Peyton     KA_TRACE(threshold,
843041982dSJonathan Peyton              ("__kmp_trace_task_stack(%s):             gtid=%d, entry=%d, "
857cc577a4SJonathan Peyton               "stack_top=%p, tied_task=%p\n",
867cc577a4SJonathan Peyton               location, gtid, entries, stack_top, tied_task));
877cc577a4SJonathan Peyton   }
887cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(stack_top == &task_stack->ts_first_block.sb_block[0]);
897cc577a4SJonathan Peyton 
903041982dSJonathan Peyton   KA_TRACE(threshold,
913041982dSJonathan Peyton            ("__kmp_trace_task_stack(exit): location = %s, gtid = %d\n",
927cc577a4SJonathan Peyton             location, gtid));
937cc577a4SJonathan Peyton }
947cc577a4SJonathan Peyton 
957cc577a4SJonathan Peyton //  __kmp_init_task_stack: initialize the task stack for the first time
967cc577a4SJonathan Peyton //  after a thread_data structure is created.
977cc577a4SJonathan Peyton //  It should not be necessary to do this again (assuming the stack works).
987cc577a4SJonathan Peyton //
997cc577a4SJonathan Peyton //  gtid: global thread identifier of calling thread
1007cc577a4SJonathan Peyton //  thread_data: thread data for task team thread containing stack
1013041982dSJonathan Peyton static void __kmp_init_task_stack(kmp_int32 gtid,
1023041982dSJonathan Peyton                                   kmp_thread_data_t *thread_data) {
1037cc577a4SJonathan Peyton   kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
1047cc577a4SJonathan Peyton   kmp_stack_block_t *first_block;
1057cc577a4SJonathan Peyton 
1067cc577a4SJonathan Peyton   // set up the first block of the stack
1077cc577a4SJonathan Peyton   first_block = &task_stack->ts_first_block;
1087cc577a4SJonathan Peyton   task_stack->ts_top = (kmp_taskdata_t **)first_block;
1093041982dSJonathan Peyton   memset((void *)first_block, '\0',
1103041982dSJonathan Peyton          TASK_STACK_BLOCK_SIZE * sizeof(kmp_taskdata_t *));
1117cc577a4SJonathan Peyton 
1127cc577a4SJonathan Peyton   // initialize the stack to be empty
1137cc577a4SJonathan Peyton   task_stack->ts_entries = TASK_STACK_EMPTY;
1147cc577a4SJonathan Peyton   first_block->sb_next = NULL;
1157cc577a4SJonathan Peyton   first_block->sb_prev = NULL;
1167cc577a4SJonathan Peyton }
1177cc577a4SJonathan Peyton 
1187cc577a4SJonathan Peyton //  __kmp_free_task_stack: free the task stack when thread_data is destroyed.
1197cc577a4SJonathan Peyton //
1207cc577a4SJonathan Peyton //  gtid: global thread identifier for calling thread
1217cc577a4SJonathan Peyton //  thread_data: thread info for thread containing stack
1223041982dSJonathan Peyton static void __kmp_free_task_stack(kmp_int32 gtid,
1233041982dSJonathan Peyton                                   kmp_thread_data_t *thread_data) {
1247cc577a4SJonathan Peyton   kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
1257cc577a4SJonathan Peyton   kmp_stack_block_t *stack_block = &task_stack->ts_first_block;
1267cc577a4SJonathan Peyton 
1277cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task_stack->ts_entries == TASK_STACK_EMPTY);
1287cc577a4SJonathan Peyton   // free from the second block of the stack
1297cc577a4SJonathan Peyton   while (stack_block != NULL) {
1307cc577a4SJonathan Peyton     kmp_stack_block_t *next_block = (stack_block) ? stack_block->sb_next : NULL;
1317cc577a4SJonathan Peyton 
1327cc577a4SJonathan Peyton     stack_block->sb_next = NULL;
1337cc577a4SJonathan Peyton     stack_block->sb_prev = NULL;
1347cc577a4SJonathan Peyton     if (stack_block != &task_stack->ts_first_block) {
1353041982dSJonathan Peyton       __kmp_thread_free(thread,
1363041982dSJonathan Peyton                         stack_block); // free the block, if not the first
1377cc577a4SJonathan Peyton     }
1387cc577a4SJonathan Peyton     stack_block = next_block;
1397cc577a4SJonathan Peyton   }
1407cc577a4SJonathan Peyton   // initialize the stack to be empty
1417cc577a4SJonathan Peyton   task_stack->ts_entries = 0;
1427cc577a4SJonathan Peyton   task_stack->ts_top = NULL;
1437cc577a4SJonathan Peyton }
1447cc577a4SJonathan Peyton 
1457cc577a4SJonathan Peyton //  __kmp_push_task_stack: Push the tied task onto the task stack.
1467cc577a4SJonathan Peyton //     Grow the stack if necessary by allocating another block.
1477cc577a4SJonathan Peyton //
1487cc577a4SJonathan Peyton //  gtid: global thread identifier for calling thread
1497cc577a4SJonathan Peyton //  thread: thread info for thread containing stack
1507cc577a4SJonathan Peyton //  tied_task: the task to push on the stack
1513041982dSJonathan Peyton static void __kmp_push_task_stack(kmp_int32 gtid, kmp_info_t *thread,
1523041982dSJonathan Peyton                                   kmp_taskdata_t *tied_task) {
1537cc577a4SJonathan Peyton   // GEH - need to consider what to do if tt_threads_data not allocated yet
1543041982dSJonathan Peyton   kmp_thread_data_t *thread_data =
1553041982dSJonathan Peyton       &thread->th.th_task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
1567cc577a4SJonathan Peyton   kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
1577cc577a4SJonathan Peyton 
1587cc577a4SJonathan Peyton   if (tied_task->td_flags.team_serial || tied_task->td_flags.tasking_ser) {
1597cc577a4SJonathan Peyton     return; // Don't push anything on stack if team or team tasks are serialized
1607cc577a4SJonathan Peyton   }
1617cc577a4SJonathan Peyton 
1627cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
1637cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task_stack->ts_top != NULL);
1647cc577a4SJonathan Peyton 
1653041982dSJonathan Peyton   KA_TRACE(20,
1663041982dSJonathan Peyton            ("__kmp_push_task_stack(enter): GTID: %d; THREAD: %p; TASK: %p\n",
1677cc577a4SJonathan Peyton             gtid, thread, tied_task));
1687cc577a4SJonathan Peyton   // Store entry
1697cc577a4SJonathan Peyton   *(task_stack->ts_top) = tied_task;
1707cc577a4SJonathan Peyton 
1717cc577a4SJonathan Peyton   // Do bookkeeping for next push
1727cc577a4SJonathan Peyton   task_stack->ts_top++;
1737cc577a4SJonathan Peyton   task_stack->ts_entries++;
1747cc577a4SJonathan Peyton 
1753041982dSJonathan Peyton   if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) {
1767cc577a4SJonathan Peyton     // Find beginning of this task block
1777cc577a4SJonathan Peyton     kmp_stack_block_t *stack_block =
1787cc577a4SJonathan Peyton         (kmp_stack_block_t *)(task_stack->ts_top - TASK_STACK_BLOCK_SIZE);
1797cc577a4SJonathan Peyton 
1807cc577a4SJonathan Peyton     // Check if we already have a block
1813041982dSJonathan Peyton     if (stack_block->sb_next !=
1823041982dSJonathan Peyton         NULL) { // reset ts_top to beginning of next block
1837cc577a4SJonathan Peyton       task_stack->ts_top = &stack_block->sb_next->sb_block[0];
1843041982dSJonathan Peyton     } else { // Alloc new block and link it up
1853041982dSJonathan Peyton       kmp_stack_block_t *new_block = (kmp_stack_block_t *)__kmp_thread_calloc(
1863041982dSJonathan Peyton           thread, sizeof(kmp_stack_block_t));
1877cc577a4SJonathan Peyton 
1887cc577a4SJonathan Peyton       task_stack->ts_top = &new_block->sb_block[0];
1897cc577a4SJonathan Peyton       stack_block->sb_next = new_block;
1907cc577a4SJonathan Peyton       new_block->sb_prev = stack_block;
1917cc577a4SJonathan Peyton       new_block->sb_next = NULL;
1927cc577a4SJonathan Peyton 
1933041982dSJonathan Peyton       KA_TRACE(
1943041982dSJonathan Peyton           30,
1953041982dSJonathan Peyton           ("__kmp_push_task_stack(): GTID: %d; TASK: %p; Alloc new block: %p\n",
1967cc577a4SJonathan Peyton            gtid, tied_task, new_block));
1977cc577a4SJonathan Peyton     }
1987cc577a4SJonathan Peyton   }
1993041982dSJonathan Peyton   KA_TRACE(20, ("__kmp_push_task_stack(exit): GTID: %d; TASK: %p\n", gtid,
2003041982dSJonathan Peyton                 tied_task));
2017cc577a4SJonathan Peyton }
2027cc577a4SJonathan Peyton 
2037cc577a4SJonathan Peyton //  __kmp_pop_task_stack: Pop the tied task from the task stack.  Don't return
2047cc577a4SJonathan Peyton //  the task, just check to make sure it matches the ending task passed in.
2057cc577a4SJonathan Peyton //
2067cc577a4SJonathan Peyton //  gtid: global thread identifier for the calling thread
2077cc577a4SJonathan Peyton //  thread: thread info structure containing stack
2087cc577a4SJonathan Peyton //  tied_task: the task popped off the stack
2097cc577a4SJonathan Peyton //  ending_task: the task that is ending (should match popped task)
2103041982dSJonathan Peyton static void __kmp_pop_task_stack(kmp_int32 gtid, kmp_info_t *thread,
2113041982dSJonathan Peyton                                  kmp_taskdata_t *ending_task) {
2127cc577a4SJonathan Peyton   // GEH - need to consider what to do if tt_threads_data not allocated yet
2133041982dSJonathan Peyton   kmp_thread_data_t *thread_data =
2143041982dSJonathan Peyton       &thread->th.th_task_team->tt_threads_data[__kmp_tid_from_gtid(gtid)];
2157cc577a4SJonathan Peyton   kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
2167cc577a4SJonathan Peyton   kmp_taskdata_t *tied_task;
2177cc577a4SJonathan Peyton 
2187cc577a4SJonathan Peyton   if (ending_task->td_flags.team_serial || ending_task->td_flags.tasking_ser) {
2193041982dSJonathan Peyton     // Don't pop anything from stack if team or team tasks are serialized
2203041982dSJonathan Peyton     return;
2217cc577a4SJonathan Peyton   }
2227cc577a4SJonathan Peyton 
2237cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task_stack->ts_top != NULL);
2247cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task_stack->ts_entries > 0);
2257cc577a4SJonathan Peyton 
2263041982dSJonathan Peyton   KA_TRACE(20, ("__kmp_pop_task_stack(enter): GTID: %d; THREAD: %p\n", gtid,
2273041982dSJonathan Peyton                 thread));
2287cc577a4SJonathan Peyton 
2297cc577a4SJonathan Peyton   // fix up ts_top if we need to pop from previous block
2303041982dSJonathan Peyton   if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) {
2313041982dSJonathan Peyton     kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(task_stack->ts_top);
2327cc577a4SJonathan Peyton 
2337cc577a4SJonathan Peyton     stack_block = stack_block->sb_prev;
2347cc577a4SJonathan Peyton     task_stack->ts_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE];
2357cc577a4SJonathan Peyton   }
2367cc577a4SJonathan Peyton 
2377cc577a4SJonathan Peyton   // finish bookkeeping
2387cc577a4SJonathan Peyton   task_stack->ts_top--;
2397cc577a4SJonathan Peyton   task_stack->ts_entries--;
2407cc577a4SJonathan Peyton 
2417cc577a4SJonathan Peyton   tied_task = *(task_stack->ts_top);
2427cc577a4SJonathan Peyton 
2437cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(tied_task != NULL);
2447cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
2457cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(tied_task == ending_task); // If we built the stack correctly
2467cc577a4SJonathan Peyton 
2473041982dSJonathan Peyton   KA_TRACE(20, ("__kmp_pop_task_stack(exit): GTID: %d; TASK: %p\n", gtid,
2483041982dSJonathan Peyton                 tied_task));
2497cc577a4SJonathan Peyton   return;
2507cc577a4SJonathan Peyton }
2517cc577a4SJonathan Peyton #endif /* BUILD_TIED_TASK_STACK */
2527cc577a4SJonathan Peyton 
253c3344345SAndrey Churbanov // returns 1 if new task is allowed to execute, 0 otherwise
254c3344345SAndrey Churbanov // checks Task Scheduling constraint (if requested) and
255c3344345SAndrey Churbanov // mutexinoutset dependencies if any
256c3344345SAndrey Churbanov static bool __kmp_task_is_allowed(int gtid, const kmp_int32 is_constrained,
257c3344345SAndrey Churbanov                                   const kmp_taskdata_t *tasknew,
258c3344345SAndrey Churbanov                                   const kmp_taskdata_t *taskcurr) {
259c3344345SAndrey Churbanov   if (is_constrained && (tasknew->td_flags.tiedness == TASK_TIED)) {
260c3344345SAndrey Churbanov     // Check if the candidate obeys the Task Scheduling Constraints (TSC)
261c3344345SAndrey Churbanov     // only descendant of all deferred tied tasks can be scheduled, checking
262c3344345SAndrey Churbanov     // the last one is enough, as it in turn is the descendant of all others
263c3344345SAndrey Churbanov     kmp_taskdata_t *current = taskcurr->td_last_tied;
264c3344345SAndrey Churbanov     KMP_DEBUG_ASSERT(current != NULL);
265c3344345SAndrey Churbanov     // check if the task is not suspended on barrier
266c3344345SAndrey Churbanov     if (current->td_flags.tasktype == TASK_EXPLICIT ||
267c3344345SAndrey Churbanov         current->td_taskwait_thread > 0) { // <= 0 on barrier
268c3344345SAndrey Churbanov       kmp_int32 level = current->td_level;
269c3344345SAndrey Churbanov       kmp_taskdata_t *parent = tasknew->td_parent;
270c3344345SAndrey Churbanov       while (parent != current && parent->td_level > level) {
271c3344345SAndrey Churbanov         // check generation up to the level of the current task
272c3344345SAndrey Churbanov         parent = parent->td_parent;
273c3344345SAndrey Churbanov         KMP_DEBUG_ASSERT(parent != NULL);
274c3344345SAndrey Churbanov       }
275c3344345SAndrey Churbanov       if (parent != current)
276c3344345SAndrey Churbanov         return false;
277c3344345SAndrey Churbanov     }
278c3344345SAndrey Churbanov   }
279c3344345SAndrey Churbanov   // Check mutexinoutset dependencies, acquire locks
280c3344345SAndrey Churbanov   kmp_depnode_t *node = tasknew->td_depnode;
281c3344345SAndrey Churbanov   if (node && (node->dn.mtx_num_locks > 0)) {
282c3344345SAndrey Churbanov     for (int i = 0; i < node->dn.mtx_num_locks; ++i) {
283c3344345SAndrey Churbanov       KMP_DEBUG_ASSERT(node->dn.mtx_locks[i] != NULL);
284c3344345SAndrey Churbanov       if (__kmp_test_lock(node->dn.mtx_locks[i], gtid))
285c3344345SAndrey Churbanov         continue;
286c3344345SAndrey Churbanov       // could not get the lock, release previous locks
287c3344345SAndrey Churbanov       for (int j = i - 1; j >= 0; --j)
288c3344345SAndrey Churbanov         __kmp_release_lock(node->dn.mtx_locks[j], gtid);
289c3344345SAndrey Churbanov       return false;
290c3344345SAndrey Churbanov     }
291c3344345SAndrey Churbanov     // negative num_locks means all locks acquired successfully
292c3344345SAndrey Churbanov     node->dn.mtx_num_locks = -node->dn.mtx_num_locks;
293c3344345SAndrey Churbanov   }
294c3344345SAndrey Churbanov   return true;
295c3344345SAndrey Churbanov }
296c3344345SAndrey Churbanov 
297c3344345SAndrey Churbanov // __kmp_realloc_task_deque:
298c3344345SAndrey Churbanov // Re-allocates a task deque for a particular thread, copies the content from
299c3344345SAndrey Churbanov // the old deque and adjusts the necessary data structures relating to the
300c3344345SAndrey Churbanov // deque. This operation must be done with the deque_lock being held
301c3344345SAndrey Churbanov static void __kmp_realloc_task_deque(kmp_info_t *thread,
302c3344345SAndrey Churbanov                                      kmp_thread_data_t *thread_data) {
303c3344345SAndrey Churbanov   kmp_int32 size = TASK_DEQUE_SIZE(thread_data->td);
304c3344345SAndrey Churbanov   kmp_int32 new_size = 2 * size;
305c3344345SAndrey Churbanov 
306c3344345SAndrey Churbanov   KE_TRACE(10, ("__kmp_realloc_task_deque: T#%d reallocating deque[from %d to "
307c3344345SAndrey Churbanov                 "%d] for thread_data %p\n",
308c3344345SAndrey Churbanov                 __kmp_gtid_from_thread(thread), size, new_size, thread_data));
309c3344345SAndrey Churbanov 
310c3344345SAndrey Churbanov   kmp_taskdata_t **new_deque =
311c3344345SAndrey Churbanov       (kmp_taskdata_t **)__kmp_allocate(new_size * sizeof(kmp_taskdata_t *));
312c3344345SAndrey Churbanov 
313c3344345SAndrey Churbanov   int i, j;
314c3344345SAndrey Churbanov   for (i = thread_data->td.td_deque_head, j = 0; j < size;
315c3344345SAndrey Churbanov        i = (i + 1) & TASK_DEQUE_MASK(thread_data->td), j++)
316c3344345SAndrey Churbanov     new_deque[j] = thread_data->td.td_deque[i];
317c3344345SAndrey Churbanov 
318c3344345SAndrey Churbanov   __kmp_free(thread_data->td.td_deque);
319c3344345SAndrey Churbanov 
320c3344345SAndrey Churbanov   thread_data->td.td_deque_head = 0;
321c3344345SAndrey Churbanov   thread_data->td.td_deque_tail = size;
322c3344345SAndrey Churbanov   thread_data->td.td_deque = new_deque;
323c3344345SAndrey Churbanov   thread_data->td.td_deque_size = new_size;
324c3344345SAndrey Churbanov }
325c3344345SAndrey Churbanov 
3267cc577a4SJonathan Peyton //  __kmp_push_task: Add a task to the thread's deque
3273041982dSJonathan Peyton static kmp_int32 __kmp_push_task(kmp_int32 gtid, kmp_task_t *task) {
3287cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
3297cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
3307cc577a4SJonathan Peyton   kmp_task_team_t *task_team = thread->th.th_task_team;
3317cc577a4SJonathan Peyton   kmp_int32 tid = __kmp_tid_from_gtid(gtid);
3327cc577a4SJonathan Peyton   kmp_thread_data_t *thread_data;
3337cc577a4SJonathan Peyton 
3343041982dSJonathan Peyton   KA_TRACE(20,
3353041982dSJonathan Peyton            ("__kmp_push_task: T#%d trying to push task %p.\n", gtid, taskdata));
3367cc577a4SJonathan Peyton 
3377cc577a4SJonathan Peyton   if (taskdata->td_flags.tiedness == TASK_UNTIED) {
3383041982dSJonathan Peyton     // untied task needs to increment counter so that the task structure is not
3393041982dSJonathan Peyton     // freed prematurely
34037e2ef54SJonathan Peyton     kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
341baad3f60SJonathan Peyton     KMP_DEBUG_USE_VAR(counter);
3423041982dSJonathan Peyton     KA_TRACE(
3433041982dSJonathan Peyton         20,
3443041982dSJonathan Peyton         ("__kmp_push_task: T#%d untied_count (%d) incremented for task %p\n",
3457cc577a4SJonathan Peyton          gtid, counter, taskdata));
3467cc577a4SJonathan Peyton   }
3477cc577a4SJonathan Peyton 
3487cc577a4SJonathan Peyton   // The first check avoids building task_team thread data if serialized
3497cc577a4SJonathan Peyton   if (taskdata->td_flags.task_serial) {
3503041982dSJonathan Peyton     KA_TRACE(20, ("__kmp_push_task: T#%d team serialized; returning "
3513041982dSJonathan Peyton                   "TASK_NOT_PUSHED for task %p\n",
3527cc577a4SJonathan Peyton                   gtid, taskdata));
3537cc577a4SJonathan Peyton     return TASK_NOT_PUSHED;
3547cc577a4SJonathan Peyton   }
3557cc577a4SJonathan Peyton 
3563041982dSJonathan Peyton   // Now that serialized tasks have returned, we can assume that we are not in
3573041982dSJonathan Peyton   // immediate exec mode
3587cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
3597cc577a4SJonathan Peyton   if (!KMP_TASKING_ENABLED(task_team)) {
3607cc577a4SJonathan Peyton     __kmp_enable_tasking(task_team, thread);
3617cc577a4SJonathan Peyton   }
3627cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(TCR_4(task_team->tt.tt_found_tasks) == TRUE);
3637cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(TCR_PTR(task_team->tt.tt_threads_data) != NULL);
3647cc577a4SJonathan Peyton 
3657cc577a4SJonathan Peyton   // Find tasking deque specific to encountering thread
3667cc577a4SJonathan Peyton   thread_data = &task_team->tt.tt_threads_data[tid];
3677cc577a4SJonathan Peyton 
3687cc577a4SJonathan Peyton   // No lock needed since only owner can allocate
3697cc577a4SJonathan Peyton   if (thread_data->td.td_deque == NULL) {
3707cc577a4SJonathan Peyton     __kmp_alloc_task_deque(thread, thread_data);
3717cc577a4SJonathan Peyton   }
3727cc577a4SJonathan Peyton 
373c3344345SAndrey Churbanov   int locked = 0;
3747cc577a4SJonathan Peyton   // Check if deque is full
3753041982dSJonathan Peyton   if (TCR_4(thread_data->td.td_deque_ntasks) >=
3763041982dSJonathan Peyton       TASK_DEQUE_SIZE(thread_data->td)) {
377*a23806e6SAndrey Churbanov     if (__kmp_enable_task_throttling &&
378*a23806e6SAndrey Churbanov         __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
379c3344345SAndrey Churbanov                               thread->th.th_current_task)) {
3803041982dSJonathan Peyton       KA_TRACE(20, ("__kmp_push_task: T#%d deque is full; returning "
3813041982dSJonathan Peyton                     "TASK_NOT_PUSHED for task %p\n",
3827cc577a4SJonathan Peyton                     gtid, taskdata));
3837cc577a4SJonathan Peyton       return TASK_NOT_PUSHED;
384c3344345SAndrey Churbanov     } else {
3857cc577a4SJonathan Peyton       __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
386c3344345SAndrey Churbanov       locked = 1;
387c3344345SAndrey Churbanov       // expand deque to push the task which is not allowed to execute
388c3344345SAndrey Churbanov       __kmp_realloc_task_deque(thread, thread_data);
389c3344345SAndrey Churbanov     }
390c3344345SAndrey Churbanov   }
391c3344345SAndrey Churbanov   // Lock the deque for the task push operation
392c3344345SAndrey Churbanov   if (!locked) {
393c3344345SAndrey Churbanov     __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3947cc577a4SJonathan Peyton #if OMP_45_ENABLED
395c3344345SAndrey Churbanov     // Need to recheck as we can get a proxy task from thread outside of OpenMP
3963041982dSJonathan Peyton     if (TCR_4(thread_data->td.td_deque_ntasks) >=
3973041982dSJonathan Peyton         TASK_DEQUE_SIZE(thread_data->td)) {
398*a23806e6SAndrey Churbanov       if (__kmp_enable_task_throttling &&
399*a23806e6SAndrey Churbanov           __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
400c3344345SAndrey Churbanov                                 thread->th.th_current_task)) {
4017cc577a4SJonathan Peyton         __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
402c3344345SAndrey Churbanov         KA_TRACE(20, ("__kmp_push_task: T#%d deque is full on 2nd check; "
403c3344345SAndrey Churbanov                       "returning TASK_NOT_PUSHED for task %p\n",
4047cc577a4SJonathan Peyton                       gtid, taskdata));
4057cc577a4SJonathan Peyton         return TASK_NOT_PUSHED;
406c3344345SAndrey Churbanov       } else {
407c3344345SAndrey Churbanov         // expand deque to push the task which is not allowed to execute
408c3344345SAndrey Churbanov         __kmp_realloc_task_deque(thread, thread_data);
4097cc577a4SJonathan Peyton       }
410c3344345SAndrey Churbanov     }
411c3344345SAndrey Churbanov #endif
412c3344345SAndrey Churbanov   }
4137cc577a4SJonathan Peyton   // Must have room since no thread can add tasks but calling thread
4143041982dSJonathan Peyton   KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) <
4153041982dSJonathan Peyton                    TASK_DEQUE_SIZE(thread_data->td));
4167cc577a4SJonathan Peyton 
4173041982dSJonathan Peyton   thread_data->td.td_deque[thread_data->td.td_deque_tail] =
4183041982dSJonathan Peyton       taskdata; // Push taskdata
4197cc577a4SJonathan Peyton   // Wrap index.
4203041982dSJonathan Peyton   thread_data->td.td_deque_tail =
4213041982dSJonathan Peyton       (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
4223041982dSJonathan Peyton   TCW_4(thread_data->td.td_deque_ntasks,
4233041982dSJonathan Peyton         TCR_4(thread_data->td.td_deque_ntasks) + 1); // Adjust task count
4247cc577a4SJonathan Peyton 
4257cc577a4SJonathan Peyton   KA_TRACE(20, ("__kmp_push_task: T#%d returning TASK_SUCCESSFULLY_PUSHED: "
4267cc577a4SJonathan Peyton                 "task=%p ntasks=%d head=%u tail=%u\n",
4277cc577a4SJonathan Peyton                 gtid, taskdata, thread_data->td.td_deque_ntasks,
4287cc577a4SJonathan Peyton                 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
4297cc577a4SJonathan Peyton 
4307cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
4317cc577a4SJonathan Peyton 
4327cc577a4SJonathan Peyton   return TASK_SUCCESSFULLY_PUSHED;
4337cc577a4SJonathan Peyton }
4347cc577a4SJonathan Peyton 
4353041982dSJonathan Peyton // __kmp_pop_current_task_from_thread: set up current task from called thread
4363041982dSJonathan Peyton // when team ends
4373041982dSJonathan Peyton //
4387cc577a4SJonathan Peyton // this_thr: thread structure to set current_task in.
4393041982dSJonathan Peyton void __kmp_pop_current_task_from_thread(kmp_info_t *this_thr) {
4403041982dSJonathan Peyton   KF_TRACE(10, ("__kmp_pop_current_task_from_thread(enter): T#%d "
4413041982dSJonathan Peyton                 "this_thread=%p, curtask=%p, "
4427cc577a4SJonathan Peyton                 "curtask_parent=%p\n",
4437cc577a4SJonathan Peyton                 0, this_thr, this_thr->th.th_current_task,
4447cc577a4SJonathan Peyton                 this_thr->th.th_current_task->td_parent));
4457cc577a4SJonathan Peyton 
4467cc577a4SJonathan Peyton   this_thr->th.th_current_task = this_thr->th.th_current_task->td_parent;
4477cc577a4SJonathan Peyton 
4483041982dSJonathan Peyton   KF_TRACE(10, ("__kmp_pop_current_task_from_thread(exit): T#%d "
4493041982dSJonathan Peyton                 "this_thread=%p, curtask=%p, "
4507cc577a4SJonathan Peyton                 "curtask_parent=%p\n",
4517cc577a4SJonathan Peyton                 0, this_thr, this_thr->th.th_current_task,
4527cc577a4SJonathan Peyton                 this_thr->th.th_current_task->td_parent));
4537cc577a4SJonathan Peyton }
4547cc577a4SJonathan Peyton 
4553041982dSJonathan Peyton // __kmp_push_current_task_to_thread: set up current task in called thread for a
4563041982dSJonathan Peyton // new team
4573041982dSJonathan Peyton //
4587cc577a4SJonathan Peyton // this_thr: thread structure to set up
4597cc577a4SJonathan Peyton // team: team for implicit task data
4607cc577a4SJonathan Peyton // tid: thread within team to set up
4613041982dSJonathan Peyton void __kmp_push_current_task_to_thread(kmp_info_t *this_thr, kmp_team_t *team,
4623041982dSJonathan Peyton                                        int tid) {
4633041982dSJonathan Peyton   // current task of the thread is a parent of the new just created implicit
4643041982dSJonathan Peyton   // tasks of new team
4653041982dSJonathan Peyton   KF_TRACE(10, ("__kmp_push_current_task_to_thread(enter): T#%d this_thread=%p "
4663041982dSJonathan Peyton                 "curtask=%p "
4677cc577a4SJonathan Peyton                 "parent_task=%p\n",
4687cc577a4SJonathan Peyton                 tid, this_thr, this_thr->th.th_current_task,
4697cc577a4SJonathan Peyton                 team->t.t_implicit_task_taskdata[tid].td_parent));
4707cc577a4SJonathan Peyton 
4717cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(this_thr != NULL);
4727cc577a4SJonathan Peyton 
4737cc577a4SJonathan Peyton   if (tid == 0) {
4747cc577a4SJonathan Peyton     if (this_thr->th.th_current_task != &team->t.t_implicit_task_taskdata[0]) {
4753041982dSJonathan Peyton       team->t.t_implicit_task_taskdata[0].td_parent =
4763041982dSJonathan Peyton           this_thr->th.th_current_task;
4777cc577a4SJonathan Peyton       this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[0];
4787cc577a4SJonathan Peyton     }
4797cc577a4SJonathan Peyton   } else {
4803041982dSJonathan Peyton     team->t.t_implicit_task_taskdata[tid].td_parent =
4813041982dSJonathan Peyton         team->t.t_implicit_task_taskdata[0].td_parent;
4827cc577a4SJonathan Peyton     this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[tid];
4837cc577a4SJonathan Peyton   }
4847cc577a4SJonathan Peyton 
4853041982dSJonathan Peyton   KF_TRACE(10, ("__kmp_push_current_task_to_thread(exit): T#%d this_thread=%p "
4863041982dSJonathan Peyton                 "curtask=%p "
4877cc577a4SJonathan Peyton                 "parent_task=%p\n",
4887cc577a4SJonathan Peyton                 tid, this_thr, this_thr->th.th_current_task,
4897cc577a4SJonathan Peyton                 team->t.t_implicit_task_taskdata[tid].td_parent));
4907cc577a4SJonathan Peyton }
4917cc577a4SJonathan Peyton 
4927cc577a4SJonathan Peyton // __kmp_task_start: bookkeeping for a task starting execution
4933041982dSJonathan Peyton //
4947cc577a4SJonathan Peyton // GTID: global thread id of calling thread
4957cc577a4SJonathan Peyton // task: task starting execution
4967cc577a4SJonathan Peyton // current_task: task suspending
4973041982dSJonathan Peyton static void __kmp_task_start(kmp_int32 gtid, kmp_task_t *task,
4983041982dSJonathan Peyton                              kmp_taskdata_t *current_task) {
4997cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
5007cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
5017cc577a4SJonathan Peyton 
5023041982dSJonathan Peyton   KA_TRACE(10,
5033041982dSJonathan Peyton            ("__kmp_task_start(enter): T#%d starting task %p: current_task=%p\n",
5047cc577a4SJonathan Peyton             gtid, taskdata, current_task));
5057cc577a4SJonathan Peyton 
5067cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
5077cc577a4SJonathan Peyton 
5087cc577a4SJonathan Peyton   // mark currently executing task as suspended
5097cc577a4SJonathan Peyton   // TODO: GEH - make sure root team implicit task is initialized properly.
5107cc577a4SJonathan Peyton   // KMP_DEBUG_ASSERT( current_task -> td_flags.executing == 1 );
5117cc577a4SJonathan Peyton   current_task->td_flags.executing = 0;
5127cc577a4SJonathan Peyton 
5137cc577a4SJonathan Peyton // Add task to stack if tied
5147cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK
5153041982dSJonathan Peyton   if (taskdata->td_flags.tiedness == TASK_TIED) {
5167cc577a4SJonathan Peyton     __kmp_push_task_stack(gtid, thread, taskdata);
5177cc577a4SJonathan Peyton   }
5187cc577a4SJonathan Peyton #endif /* BUILD_TIED_TASK_STACK */
5197cc577a4SJonathan Peyton 
5207cc577a4SJonathan Peyton   // mark starting task as executing and as current task
5217cc577a4SJonathan Peyton   thread->th.th_current_task = taskdata;
5227cc577a4SJonathan Peyton 
5233041982dSJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.started == 0 ||
5243041982dSJonathan Peyton                    taskdata->td_flags.tiedness == TASK_UNTIED);
5253041982dSJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0 ||
5263041982dSJonathan Peyton                    taskdata->td_flags.tiedness == TASK_UNTIED);
5277cc577a4SJonathan Peyton   taskdata->td_flags.started = 1;
5287cc577a4SJonathan Peyton   taskdata->td_flags.executing = 1;
5297cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
5307cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
5317cc577a4SJonathan Peyton 
5327cc577a4SJonathan Peyton   // GEH TODO: shouldn't we pass some sort of location identifier here?
5337cc577a4SJonathan Peyton   // APT: yes, we will pass location here.
5347cc577a4SJonathan Peyton   // need to store current thread state (in a thread or taskdata structure)
5357cc577a4SJonathan Peyton   // before setting work_state, otherwise wrong state is set after end of task
5367cc577a4SJonathan Peyton 
5373041982dSJonathan Peyton   KA_TRACE(10, ("__kmp_task_start(exit): T#%d task=%p\n", gtid, taskdata));
5387cc577a4SJonathan Peyton 
5397cc577a4SJonathan Peyton   return;
5407cc577a4SJonathan Peyton }
5417cc577a4SJonathan Peyton 
54282e94a59SJoachim Protze #if OMPT_SUPPORT
54382e94a59SJoachim Protze //------------------------------------------------------------------------------
54482e94a59SJoachim Protze // __ompt_task_init:
54582e94a59SJoachim Protze //   Initialize OMPT fields maintained by a task. This will only be called after
54682e94a59SJoachim Protze //   ompt_start_tool, so we already know whether ompt is enabled or not.
54782e94a59SJoachim Protze 
54882e94a59SJoachim Protze static inline void __ompt_task_init(kmp_taskdata_t *task, int tid) {
54982e94a59SJoachim Protze   // The calls to __ompt_task_init already have the ompt_enabled condition.
55082e94a59SJoachim Protze   task->ompt_task_info.task_data.value = 0;
5510e0d6cddSJoachim Protze   task->ompt_task_info.frame.exit_frame = ompt_data_none;
5520e0d6cddSJoachim Protze   task->ompt_task_info.frame.enter_frame = ompt_data_none;
5530e0d6cddSJoachim Protze   task->ompt_task_info.frame.exit_frame_flags = ompt_frame_runtime | ompt_frame_framepointer;
5540e0d6cddSJoachim Protze   task->ompt_task_info.frame.enter_frame_flags = ompt_frame_runtime | ompt_frame_framepointer;
55582e94a59SJoachim Protze #if OMP_40_ENABLED
55682e94a59SJoachim Protze   task->ompt_task_info.ndeps = 0;
55782e94a59SJoachim Protze   task->ompt_task_info.deps = NULL;
55882e94a59SJoachim Protze #endif /* OMP_40_ENABLED */
55982e94a59SJoachim Protze }
56082e94a59SJoachim Protze 
56182e94a59SJoachim Protze // __ompt_task_start:
56282e94a59SJoachim Protze //   Build and trigger task-begin event
56382e94a59SJoachim Protze static inline void __ompt_task_start(kmp_task_t *task,
56482e94a59SJoachim Protze                                      kmp_taskdata_t *current_task,
56582e94a59SJoachim Protze                                      kmp_int32 gtid) {
56682e94a59SJoachim Protze   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
567489cdb78SJoachim Protze   ompt_task_status_t status = ompt_task_switch;
56882e94a59SJoachim Protze   if (__kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded) {
56982e94a59SJoachim Protze     status = ompt_task_yield;
57082e94a59SJoachim Protze     __kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded = 0;
57182e94a59SJoachim Protze   }
57282e94a59SJoachim Protze   /* let OMPT know that we're about to run this task */
57382e94a59SJoachim Protze   if (ompt_enabled.ompt_callback_task_schedule) {
57482e94a59SJoachim Protze     ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
57582e94a59SJoachim Protze         &(current_task->ompt_task_info.task_data), status,
57682e94a59SJoachim Protze         &(taskdata->ompt_task_info.task_data));
57782e94a59SJoachim Protze   }
57882e94a59SJoachim Protze   taskdata->ompt_task_info.scheduling_parent = current_task;
57982e94a59SJoachim Protze }
58082e94a59SJoachim Protze 
58182e94a59SJoachim Protze // __ompt_task_finish:
58282e94a59SJoachim Protze //   Build and trigger final task-schedule event
58386ed6aa6SJoachim Protze static inline void
58486ed6aa6SJoachim Protze __ompt_task_finish(kmp_task_t *task, kmp_taskdata_t *resumed_task,
58586ed6aa6SJoachim Protze                    ompt_task_status_t status = ompt_task_complete) {
58682e94a59SJoachim Protze   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
58782e94a59SJoachim Protze   if (__kmp_omp_cancellation && taskdata->td_taskgroup &&
58882e94a59SJoachim Protze       taskdata->td_taskgroup->cancel_request == cancel_taskgroup) {
58982e94a59SJoachim Protze     status = ompt_task_cancel;
59082e94a59SJoachim Protze   }
59182e94a59SJoachim Protze 
59282e94a59SJoachim Protze   /* let OMPT know that we're returning to the callee task */
59382e94a59SJoachim Protze   if (ompt_enabled.ompt_callback_task_schedule) {
59482e94a59SJoachim Protze     ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
59582e94a59SJoachim Protze         &(taskdata->ompt_task_info.task_data), status,
59682e94a59SJoachim Protze         &((resumed_task ? resumed_task
59782e94a59SJoachim Protze                         : (taskdata->ompt_task_info.scheduling_parent
59882e94a59SJoachim Protze                                ? taskdata->ompt_task_info.scheduling_parent
59982e94a59SJoachim Protze                                : taskdata->td_parent))
60082e94a59SJoachim Protze               ->ompt_task_info.task_data));
60182e94a59SJoachim Protze   }
60282e94a59SJoachim Protze }
60382e94a59SJoachim Protze #endif
60482e94a59SJoachim Protze 
60582e94a59SJoachim Protze template <bool ompt>
60682e94a59SJoachim Protze static void __kmpc_omp_task_begin_if0_template(ident_t *loc_ref, kmp_int32 gtid,
60782e94a59SJoachim Protze                                                kmp_task_t *task,
60882e94a59SJoachim Protze                                                void *frame_address,
60982e94a59SJoachim Protze                                                void *return_address) {
6107cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
6117cc577a4SJonathan Peyton   kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
6127cc577a4SJonathan Peyton 
6133041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_begin_if0(enter): T#%d loc=%p task=%p "
6143041982dSJonathan Peyton                 "current_task=%p\n",
6157cc577a4SJonathan Peyton                 gtid, loc_ref, taskdata, current_task));
6167cc577a4SJonathan Peyton 
6177cc577a4SJonathan Peyton   if (taskdata->td_flags.tiedness == TASK_UNTIED) {
6183041982dSJonathan Peyton     // untied task needs to increment counter so that the task structure is not
6193041982dSJonathan Peyton     // freed prematurely
62037e2ef54SJonathan Peyton     kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
621baad3f60SJonathan Peyton     KMP_DEBUG_USE_VAR(counter);
6223041982dSJonathan Peyton     KA_TRACE(20, ("__kmpc_omp_task_begin_if0: T#%d untied_count (%d) "
6233041982dSJonathan Peyton                   "incremented for task %p\n",
6247cc577a4SJonathan Peyton                   gtid, counter, taskdata));
6257cc577a4SJonathan Peyton   }
6267cc577a4SJonathan Peyton 
6273041982dSJonathan Peyton   taskdata->td_flags.task_serial =
6283041982dSJonathan Peyton       1; // Execute this task immediately, not deferred.
6297cc577a4SJonathan Peyton   __kmp_task_start(gtid, task, current_task);
6307cc577a4SJonathan Peyton 
63182e94a59SJoachim Protze #if OMPT_SUPPORT
63282e94a59SJoachim Protze   if (ompt) {
6330e0d6cddSJoachim Protze     if (current_task->ompt_task_info.frame.enter_frame.ptr == NULL) {
6340e0d6cddSJoachim Protze       current_task->ompt_task_info.frame.enter_frame.ptr =
6350e0d6cddSJoachim Protze           taskdata->ompt_task_info.frame.exit_frame.ptr = frame_address;
6360e0d6cddSJoachim Protze       current_task->ompt_task_info.frame.enter_frame_flags =
6370e0d6cddSJoachim Protze           taskdata->ompt_task_info.frame.exit_frame_flags = ompt_frame_application | ompt_frame_framepointer;
63882e94a59SJoachim Protze     }
63982e94a59SJoachim Protze     if (ompt_enabled.ompt_callback_task_create) {
64082e94a59SJoachim Protze       ompt_task_info_t *parent_info = &(current_task->ompt_task_info);
64182e94a59SJoachim Protze       ompt_callbacks.ompt_callback(ompt_callback_task_create)(
64282e94a59SJoachim Protze           &(parent_info->task_data), &(parent_info->frame),
64382e94a59SJoachim Protze           &(taskdata->ompt_task_info.task_data),
64482e94a59SJoachim Protze           ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(taskdata), 0,
64582e94a59SJoachim Protze           return_address);
64682e94a59SJoachim Protze     }
64782e94a59SJoachim Protze     __ompt_task_start(task, current_task, gtid);
64882e94a59SJoachim Protze   }
64982e94a59SJoachim Protze #endif // OMPT_SUPPORT
65082e94a59SJoachim Protze 
6513041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_begin_if0(exit): T#%d loc=%p task=%p,\n", gtid,
6523041982dSJonathan Peyton                 loc_ref, taskdata));
65382e94a59SJoachim Protze }
6547cc577a4SJonathan Peyton 
65582e94a59SJoachim Protze #if OMPT_SUPPORT
65682e94a59SJoachim Protze OMPT_NOINLINE
65782e94a59SJoachim Protze static void __kmpc_omp_task_begin_if0_ompt(ident_t *loc_ref, kmp_int32 gtid,
65882e94a59SJoachim Protze                                            kmp_task_t *task,
65982e94a59SJoachim Protze                                            void *frame_address,
66082e94a59SJoachim Protze                                            void *return_address) {
661f0a1c65fSJonas Hahnfeld   __kmpc_omp_task_begin_if0_template<true>(loc_ref, gtid, task, frame_address,
662f0a1c65fSJonas Hahnfeld                                            return_address);
66382e94a59SJoachim Protze }
66482e94a59SJoachim Protze #endif // OMPT_SUPPORT
66582e94a59SJoachim Protze 
66682e94a59SJoachim Protze // __kmpc_omp_task_begin_if0: report that a given serialized task has started
66782e94a59SJoachim Protze // execution
66882e94a59SJoachim Protze //
66982e94a59SJoachim Protze // loc_ref: source location information; points to beginning of task block.
67082e94a59SJoachim Protze // gtid: global thread number.
67182e94a59SJoachim Protze // task: task thunk for the started task.
67282e94a59SJoachim Protze void __kmpc_omp_task_begin_if0(ident_t *loc_ref, kmp_int32 gtid,
67382e94a59SJoachim Protze                                kmp_task_t *task) {
67482e94a59SJoachim Protze #if OMPT_SUPPORT
67582e94a59SJoachim Protze   if (UNLIKELY(ompt_enabled.enabled)) {
67682e94a59SJoachim Protze     OMPT_STORE_RETURN_ADDRESS(gtid);
67782e94a59SJoachim Protze     __kmpc_omp_task_begin_if0_ompt(loc_ref, gtid, task,
67882e94a59SJoachim Protze                                    OMPT_GET_FRAME_ADDRESS(1),
67982e94a59SJoachim Protze                                    OMPT_LOAD_RETURN_ADDRESS(gtid));
6807cc577a4SJonathan Peyton     return;
6817cc577a4SJonathan Peyton   }
68282e94a59SJoachim Protze #endif
683f0a1c65fSJonas Hahnfeld   __kmpc_omp_task_begin_if0_template<false>(loc_ref, gtid, task, NULL, NULL);
68482e94a59SJoachim Protze }
6857cc577a4SJonathan Peyton 
6867cc577a4SJonathan Peyton #ifdef TASK_UNUSED
6877cc577a4SJonathan Peyton // __kmpc_omp_task_begin: report that a given task has started execution
6887cc577a4SJonathan Peyton // NEVER GENERATED BY COMPILER, DEPRECATED!!!
6893041982dSJonathan Peyton void __kmpc_omp_task_begin(ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task) {
6907cc577a4SJonathan Peyton   kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
6917cc577a4SJonathan Peyton 
6923041982dSJonathan Peyton   KA_TRACE(
6933041982dSJonathan Peyton       10,
6943041982dSJonathan Peyton       ("__kmpc_omp_task_begin(enter): T#%d loc=%p task=%p current_task=%p\n",
6957cc577a4SJonathan Peyton        gtid, loc_ref, KMP_TASK_TO_TASKDATA(task), current_task));
6967cc577a4SJonathan Peyton 
6977cc577a4SJonathan Peyton   __kmp_task_start(gtid, task, current_task);
6987cc577a4SJonathan Peyton 
6993041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_begin(exit): T#%d loc=%p task=%p,\n", gtid,
7003041982dSJonathan Peyton                 loc_ref, KMP_TASK_TO_TASKDATA(task)));
7017cc577a4SJonathan Peyton   return;
7027cc577a4SJonathan Peyton }
7037cc577a4SJonathan Peyton #endif // TASK_UNUSED
7047cc577a4SJonathan Peyton 
7057cc577a4SJonathan Peyton // __kmp_free_task: free the current task space and the space for shareds
7063041982dSJonathan Peyton //
7077cc577a4SJonathan Peyton // gtid: Global thread ID of calling thread
7087cc577a4SJonathan Peyton // taskdata: task to free
7097cc577a4SJonathan Peyton // thread: thread data structure of caller
7103041982dSJonathan Peyton static void __kmp_free_task(kmp_int32 gtid, kmp_taskdata_t *taskdata,
7113041982dSJonathan Peyton                             kmp_info_t *thread) {
7123041982dSJonathan Peyton   KA_TRACE(30, ("__kmp_free_task: T#%d freeing data from task %p\n", gtid,
7133041982dSJonathan Peyton                 taskdata));
7147cc577a4SJonathan Peyton 
7157cc577a4SJonathan Peyton   // Check to make sure all flags and counters have the correct values
7167cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
7177cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0);
7187cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 1);
7197cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
72037e2ef54SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_allocated_child_tasks == 0 ||
7213041982dSJonathan Peyton                    taskdata->td_flags.task_serial == 1);
72237e2ef54SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_incomplete_child_tasks == 0);
7237cc577a4SJonathan Peyton 
7247cc577a4SJonathan Peyton   taskdata->td_flags.freed = 1;
7257cc577a4SJonathan Peyton   ANNOTATE_HAPPENS_BEFORE(taskdata);
7267cc577a4SJonathan Peyton // deallocate the taskdata and shared variable blocks associated with this task
7277cc577a4SJonathan Peyton #if USE_FAST_MEMORY
7287cc577a4SJonathan Peyton   __kmp_fast_free(thread, taskdata);
7297cc577a4SJonathan Peyton #else /* ! USE_FAST_MEMORY */
7307cc577a4SJonathan Peyton   __kmp_thread_free(thread, taskdata);
7317cc577a4SJonathan Peyton #endif
7327cc577a4SJonathan Peyton 
7333041982dSJonathan Peyton   KA_TRACE(20, ("__kmp_free_task: T#%d freed task %p\n", gtid, taskdata));
7347cc577a4SJonathan Peyton }
7357cc577a4SJonathan Peyton 
7363041982dSJonathan Peyton // __kmp_free_task_and_ancestors: free the current task and ancestors without
7373041982dSJonathan Peyton // children
7387cc577a4SJonathan Peyton //
7397cc577a4SJonathan Peyton // gtid: Global thread ID of calling thread
7407cc577a4SJonathan Peyton // taskdata: task to free
7417cc577a4SJonathan Peyton // thread: thread data structure of caller
7423041982dSJonathan Peyton static void __kmp_free_task_and_ancestors(kmp_int32 gtid,
7433041982dSJonathan Peyton                                           kmp_taskdata_t *taskdata,
7443041982dSJonathan Peyton                                           kmp_info_t *thread) {
7457cc577a4SJonathan Peyton #if OMP_45_ENABLED
7467cc577a4SJonathan Peyton   // Proxy tasks must always be allowed to free their parents
7477cc577a4SJonathan Peyton   // because they can be run in background even in serial mode.
7483041982dSJonathan Peyton   kmp_int32 team_serial =
7493041982dSJonathan Peyton       (taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) &&
7503041982dSJonathan Peyton       !taskdata->td_flags.proxy;
7517cc577a4SJonathan Peyton #else
7523041982dSJonathan Peyton   kmp_int32 team_serial =
7533041982dSJonathan Peyton       taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser;
7547cc577a4SJonathan Peyton #endif
7557cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
7567cc577a4SJonathan Peyton 
75737e2ef54SJonathan Peyton   kmp_int32 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
7587cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(children >= 0);
7597cc577a4SJonathan Peyton 
7607cc577a4SJonathan Peyton   // Now, go up the ancestor tree to see if any ancestors can now be freed.
7613041982dSJonathan Peyton   while (children == 0) {
7627cc577a4SJonathan Peyton     kmp_taskdata_t *parent_taskdata = taskdata->td_parent;
7637cc577a4SJonathan Peyton 
7647cc577a4SJonathan Peyton     KA_TRACE(20, ("__kmp_free_task_and_ancestors(enter): T#%d task %p complete "
7653041982dSJonathan Peyton                   "and freeing itself\n",
7663041982dSJonathan Peyton                   gtid, taskdata));
7677cc577a4SJonathan Peyton 
7687cc577a4SJonathan Peyton     // --- Deallocate my ancestor task ---
7697cc577a4SJonathan Peyton     __kmp_free_task(gtid, taskdata, thread);
7707cc577a4SJonathan Peyton 
7717cc577a4SJonathan Peyton     taskdata = parent_taskdata;
7727cc577a4SJonathan Peyton 
773c3344345SAndrey Churbanov     if (team_serial)
774c3344345SAndrey Churbanov       return;
7753041982dSJonathan Peyton     // Stop checking ancestors at implicit task instead of walking up ancestor
7763041982dSJonathan Peyton     // tree to avoid premature deallocation of ancestors.
777c3344345SAndrey Churbanov     if (taskdata->td_flags.tasktype == TASK_IMPLICIT) {
778c3344345SAndrey Churbanov       if (taskdata->td_dephash) { // do we need to cleanup dephash?
779c3344345SAndrey Churbanov         int children = KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks);
780c3344345SAndrey Churbanov         kmp_tasking_flags_t flags_old = taskdata->td_flags;
781c3344345SAndrey Churbanov         if (children == 0 && flags_old.complete == 1) {
782c3344345SAndrey Churbanov           kmp_tasking_flags_t flags_new = flags_old;
783c3344345SAndrey Churbanov           flags_new.complete = 0;
784c3344345SAndrey Churbanov           if (KMP_COMPARE_AND_STORE_ACQ32(
785c3344345SAndrey Churbanov                   RCAST(kmp_int32 *, &taskdata->td_flags),
786c3344345SAndrey Churbanov                   *RCAST(kmp_int32 *, &flags_old),
787c3344345SAndrey Churbanov                   *RCAST(kmp_int32 *, &flags_new))) {
788c3344345SAndrey Churbanov             KA_TRACE(100, ("__kmp_free_task_and_ancestors: T#%d cleans "
789c3344345SAndrey Churbanov                            "dephash of implicit task %p\n",
790c3344345SAndrey Churbanov                            gtid, taskdata));
791c3344345SAndrey Churbanov             // cleanup dephash of finished implicit task
792c3344345SAndrey Churbanov             __kmp_dephash_free_entries(thread, taskdata->td_dephash);
793c3344345SAndrey Churbanov           }
794c3344345SAndrey Churbanov         }
795c3344345SAndrey Churbanov       }
7967cc577a4SJonathan Peyton       return;
797c3344345SAndrey Churbanov     }
7987cc577a4SJonathan Peyton     // Predecrement simulated by "- 1" calculation
79937e2ef54SJonathan Peyton     children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
8007cc577a4SJonathan Peyton     KMP_DEBUG_ASSERT(children >= 0);
8017cc577a4SJonathan Peyton   }
8027cc577a4SJonathan Peyton 
8033041982dSJonathan Peyton   KA_TRACE(
8043041982dSJonathan Peyton       20, ("__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; "
8053041982dSJonathan Peyton            "not freeing it yet\n",
8063041982dSJonathan Peyton            gtid, taskdata, children));
8077cc577a4SJonathan Peyton }
8087cc577a4SJonathan Peyton 
8097cc577a4SJonathan Peyton // __kmp_task_finish: bookkeeping to do when a task finishes execution
8103041982dSJonathan Peyton //
8117cc577a4SJonathan Peyton // gtid: global thread ID for calling thread
8127cc577a4SJonathan Peyton // task: task to be finished
8137cc577a4SJonathan Peyton // resumed_task: task to be resumed.  (may be NULL if task is serialized)
81486ed6aa6SJoachim Protze template <bool ompt>
8153041982dSJonathan Peyton static void __kmp_task_finish(kmp_int32 gtid, kmp_task_t *task,
8163041982dSJonathan Peyton                               kmp_taskdata_t *resumed_task) {
8177cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
8187cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
819781a0896SRoman Lebedev #if OMP_45_ENABLED
8203041982dSJonathan Peyton   kmp_task_team_t *task_team =
8213041982dSJonathan Peyton       thread->th.th_task_team; // might be NULL for serial teams...
822781a0896SRoman Lebedev #endif // OMP_45_ENABLED
8237cc577a4SJonathan Peyton   kmp_int32 children = 0;
8247cc577a4SJonathan Peyton 
8253041982dSJonathan Peyton   KA_TRACE(10, ("__kmp_task_finish(enter): T#%d finishing task %p and resuming "
8263041982dSJonathan Peyton                 "task %p\n",
8277cc577a4SJonathan Peyton                 gtid, taskdata, resumed_task));
8287cc577a4SJonathan Peyton 
8297cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
8307cc577a4SJonathan Peyton 
8317cc577a4SJonathan Peyton // Pop task from stack if tied
8327cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK
8333041982dSJonathan Peyton   if (taskdata->td_flags.tiedness == TASK_TIED) {
8347cc577a4SJonathan Peyton     __kmp_pop_task_stack(gtid, thread, taskdata);
8357cc577a4SJonathan Peyton   }
8367cc577a4SJonathan Peyton #endif /* BUILD_TIED_TASK_STACK */
8377cc577a4SJonathan Peyton 
8387cc577a4SJonathan Peyton   if (taskdata->td_flags.tiedness == TASK_UNTIED) {
8393041982dSJonathan Peyton     // untied task needs to check the counter so that the task structure is not
8403041982dSJonathan Peyton     // freed prematurely
84137e2ef54SJonathan Peyton     kmp_int32 counter = KMP_ATOMIC_DEC(&taskdata->td_untied_count) - 1;
8423041982dSJonathan Peyton     KA_TRACE(
8433041982dSJonathan Peyton         20,
8443041982dSJonathan Peyton         ("__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n",
8457cc577a4SJonathan Peyton          gtid, counter, taskdata));
8467cc577a4SJonathan Peyton     if (counter > 0) {
8473041982dSJonathan Peyton       // untied task is not done, to be continued possibly by other thread, do
8483041982dSJonathan Peyton       // not free it now
8497cc577a4SJonathan Peyton       if (resumed_task == NULL) {
8507cc577a4SJonathan Peyton         KMP_DEBUG_ASSERT(taskdata->td_flags.task_serial);
8513041982dSJonathan Peyton         resumed_task = taskdata->td_parent; // In a serialized task, the resumed
8523041982dSJonathan Peyton         // task is the parent
8537cc577a4SJonathan Peyton       }
8547cc577a4SJonathan Peyton       thread->th.th_current_task = resumed_task; // restore current_task
8557cc577a4SJonathan Peyton       resumed_task->td_flags.executing = 1; // resume previous task
8563041982dSJonathan Peyton       KA_TRACE(10, ("__kmp_task_finish(exit): T#%d partially done task %p, "
8573041982dSJonathan Peyton                     "resuming task %p\n",
8587cc577a4SJonathan Peyton                     gtid, taskdata, resumed_task));
8597cc577a4SJonathan Peyton       return;
8607cc577a4SJonathan Peyton     }
8617cc577a4SJonathan Peyton   }
86286ed6aa6SJoachim Protze #if OMPT_SUPPORT
86386ed6aa6SJoachim Protze   if (ompt)
86486ed6aa6SJoachim Protze     __ompt_task_finish(task, resumed_task);
86586ed6aa6SJoachim Protze #endif
8667cc577a4SJonathan Peyton 
867c3344345SAndrey Churbanov   // Check mutexinoutset dependencies, release locks
868c3344345SAndrey Churbanov   kmp_depnode_t *node = taskdata->td_depnode;
869c3344345SAndrey Churbanov   if (node && (node->dn.mtx_num_locks < 0)) {
870c3344345SAndrey Churbanov     // negative num_locks means all locks were acquired
871c3344345SAndrey Churbanov     node->dn.mtx_num_locks = -node->dn.mtx_num_locks;
872c3344345SAndrey Churbanov     for (int i = node->dn.mtx_num_locks - 1; i >= 0; --i) {
873c3344345SAndrey Churbanov       KMP_DEBUG_ASSERT(node->dn.mtx_locks[i] != NULL);
874c3344345SAndrey Churbanov       __kmp_release_lock(node->dn.mtx_locks[i], gtid);
875c3344345SAndrey Churbanov     }
876c3344345SAndrey Churbanov   }
877c3344345SAndrey Churbanov 
8787cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
879405037c4SAndrey Churbanov   bool detach = false;
880405037c4SAndrey Churbanov #if OMP_50_ENABLED
881405037c4SAndrey Churbanov   if (taskdata->td_flags.detachable == TASK_DETACHABLE) {
882405037c4SAndrey Churbanov     if (taskdata->td_allow_completion_event.type ==
883405037c4SAndrey Churbanov         KMP_EVENT_ALLOW_COMPLETION) {
884405037c4SAndrey Churbanov       // event hasn't been fulfilled yet. Try to detach task.
885405037c4SAndrey Churbanov       __kmp_acquire_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
886405037c4SAndrey Churbanov       if (taskdata->td_allow_completion_event.type ==
887405037c4SAndrey Churbanov           KMP_EVENT_ALLOW_COMPLETION) {
888405037c4SAndrey Churbanov         taskdata->td_flags.proxy = TASK_PROXY; // proxify!
889405037c4SAndrey Churbanov         detach = true;
890405037c4SAndrey Churbanov       }
891405037c4SAndrey Churbanov       __kmp_release_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
892405037c4SAndrey Churbanov     }
893405037c4SAndrey Churbanov   }
894405037c4SAndrey Churbanov #endif
8957cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.started == 1);
8967cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
8977cc577a4SJonathan Peyton 
898405037c4SAndrey Churbanov   if (!detach) {
899405037c4SAndrey Churbanov     taskdata->td_flags.complete = 1; // mark the task as completed
900405037c4SAndrey Churbanov 
9013041982dSJonathan Peyton     // Only need to keep track of count if team parallel and tasking not
9023041982dSJonathan Peyton     // serialized
9037cc577a4SJonathan Peyton     if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
9047cc577a4SJonathan Peyton       // Predecrement simulated by "- 1" calculation
9053041982dSJonathan Peyton       children =
90637e2ef54SJonathan Peyton           KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks) - 1;
9077cc577a4SJonathan Peyton       KMP_DEBUG_ASSERT(children >= 0);
9087cc577a4SJonathan Peyton #if OMP_40_ENABLED
9097cc577a4SJonathan Peyton       if (taskdata->td_taskgroup)
91037e2ef54SJonathan Peyton         KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
911cf27e31bSJonathan Peyton       __kmp_release_deps(gtid, taskdata);
9127cc577a4SJonathan Peyton #if OMP_45_ENABLED
913cf27e31bSJonathan Peyton     } else if (task_team && task_team->tt.tt_found_proxy_tasks) {
9147cc577a4SJonathan Peyton       // if we found proxy tasks there could exist a dependency chain
9157cc577a4SJonathan Peyton       // with the proxy task as origin
9167cc577a4SJonathan Peyton       __kmp_release_deps(gtid, taskdata);
917cf27e31bSJonathan Peyton #endif // OMP_45_ENABLED
918cf27e31bSJonathan Peyton #endif // OMP_40_ENABLED
9197cc577a4SJonathan Peyton     }
920405037c4SAndrey Churbanov   }
9217cc577a4SJonathan Peyton 
9223041982dSJonathan Peyton   // td_flags.executing must be marked as 0 after __kmp_release_deps has been
9233041982dSJonathan Peyton   // called. Othertwise, if a task is executed immediately from the release_deps
9243041982dSJonathan Peyton   // code, the flag will be reset to 1 again by this same function
9257cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
9267cc577a4SJonathan Peyton   taskdata->td_flags.executing = 0; // suspend the finishing task
9277cc577a4SJonathan Peyton 
9283041982dSJonathan Peyton   KA_TRACE(
9293041982dSJonathan Peyton       20, ("__kmp_task_finish: T#%d finished task %p, %d incomplete children\n",
9307cc577a4SJonathan Peyton            gtid, taskdata, children));
9317cc577a4SJonathan Peyton 
9327cc577a4SJonathan Peyton #if OMP_40_ENABLED
9337cc577a4SJonathan Peyton   /* If the tasks' destructor thunk flag has been set, we need to invoke the
9343041982dSJonathan Peyton      destructor thunk that has been generated by the compiler. The code is
9353041982dSJonathan Peyton      placed here, since at this point other tasks might have been released
9367cc577a4SJonathan Peyton      hence overlapping the destructor invokations with some other work in the
9373041982dSJonathan Peyton      released tasks.  The OpenMP spec is not specific on when the destructors
9383041982dSJonathan Peyton      are invoked, so we should be free to choose. */
9397cc577a4SJonathan Peyton   if (taskdata->td_flags.destructors_thunk) {
9407cc577a4SJonathan Peyton     kmp_routine_entry_t destr_thunk = task->data1.destructors;
9417cc577a4SJonathan Peyton     KMP_ASSERT(destr_thunk);
9427cc577a4SJonathan Peyton     destr_thunk(gtid, task);
9437cc577a4SJonathan Peyton   }
9447cc577a4SJonathan Peyton #endif // OMP_40_ENABLED
9457cc577a4SJonathan Peyton 
9467cc577a4SJonathan Peyton   // bookkeeping for resuming task:
9477cc577a4SJonathan Peyton   // GEH - note tasking_ser => task_serial
9483041982dSJonathan Peyton   KMP_DEBUG_ASSERT(
9493041982dSJonathan Peyton       (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) ==
9507cc577a4SJonathan Peyton       taskdata->td_flags.task_serial);
9513041982dSJonathan Peyton   if (taskdata->td_flags.task_serial) {
9527cc577a4SJonathan Peyton     if (resumed_task == NULL) {
9533041982dSJonathan Peyton       resumed_task = taskdata->td_parent; // In a serialized task, the resumed
9543041982dSJonathan Peyton       // task is the parent
9557cc577a4SJonathan Peyton     }
9563041982dSJonathan Peyton   } else {
9573041982dSJonathan Peyton     KMP_DEBUG_ASSERT(resumed_task !=
9583041982dSJonathan Peyton                      NULL); // verify that resumed task is passed as arguemnt
9597cc577a4SJonathan Peyton   }
9607cc577a4SJonathan Peyton 
9617cc577a4SJonathan Peyton   // Free this task and then ancestor tasks if they have no children.
9627cc577a4SJonathan Peyton   // Restore th_current_task first as suggested by John:
9637cc577a4SJonathan Peyton   // johnmc: if an asynchronous inquiry peers into the runtime system
9647cc577a4SJonathan Peyton   // it doesn't see the freed task as the current task.
9657cc577a4SJonathan Peyton   thread->th.th_current_task = resumed_task;
966405037c4SAndrey Churbanov   if (!detach)
9677cc577a4SJonathan Peyton     __kmp_free_task_and_ancestors(gtid, taskdata, thread);
9687cc577a4SJonathan Peyton 
9697cc577a4SJonathan Peyton   // TODO: GEH - make sure root team implicit task is initialized properly.
9707cc577a4SJonathan Peyton   // KMP_DEBUG_ASSERT( resumed_task->td_flags.executing == 0 );
9717cc577a4SJonathan Peyton   resumed_task->td_flags.executing = 1; // resume previous task
9727cc577a4SJonathan Peyton 
9733041982dSJonathan Peyton   KA_TRACE(
9743041982dSJonathan Peyton       10, ("__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n",
9757cc577a4SJonathan Peyton            gtid, taskdata, resumed_task));
9767cc577a4SJonathan Peyton 
9777cc577a4SJonathan Peyton   return;
9787cc577a4SJonathan Peyton }
9797cc577a4SJonathan Peyton 
98082e94a59SJoachim Protze template <bool ompt>
98182e94a59SJoachim Protze static void __kmpc_omp_task_complete_if0_template(ident_t *loc_ref,
98282e94a59SJoachim Protze                                                   kmp_int32 gtid,
9833041982dSJonathan Peyton                                                   kmp_task_t *task) {
9847cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n",
9857cc577a4SJonathan Peyton                 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
9863041982dSJonathan Peyton   // this routine will provide task to resume
98786ed6aa6SJoachim Protze   __kmp_task_finish<ompt>(gtid, task, NULL);
9887cc577a4SJonathan Peyton 
9897cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n",
9907cc577a4SJonathan Peyton                 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
99182e94a59SJoachim Protze 
99282e94a59SJoachim Protze #if OMPT_SUPPORT
99382e94a59SJoachim Protze   if (ompt) {
9940e0d6cddSJoachim Protze     ompt_frame_t *ompt_frame;
99582e94a59SJoachim Protze     __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
9960e0d6cddSJoachim Protze     ompt_frame->enter_frame = ompt_data_none;
9970e0d6cddSJoachim Protze     ompt_frame->enter_frame_flags = ompt_frame_runtime | ompt_frame_framepointer;
99882e94a59SJoachim Protze   }
99982e94a59SJoachim Protze #endif
100082e94a59SJoachim Protze 
10017cc577a4SJonathan Peyton   return;
10027cc577a4SJonathan Peyton }
10037cc577a4SJonathan Peyton 
100482e94a59SJoachim Protze #if OMPT_SUPPORT
100582e94a59SJoachim Protze OMPT_NOINLINE
100682e94a59SJoachim Protze void __kmpc_omp_task_complete_if0_ompt(ident_t *loc_ref, kmp_int32 gtid,
100782e94a59SJoachim Protze                                        kmp_task_t *task) {
100882e94a59SJoachim Protze   __kmpc_omp_task_complete_if0_template<true>(loc_ref, gtid, task);
100982e94a59SJoachim Protze }
101082e94a59SJoachim Protze #endif // OMPT_SUPPORT
101182e94a59SJoachim Protze 
101282e94a59SJoachim Protze // __kmpc_omp_task_complete_if0: report that a task has completed execution
101382e94a59SJoachim Protze //
101482e94a59SJoachim Protze // loc_ref: source location information; points to end of task block.
101582e94a59SJoachim Protze // gtid: global thread number.
101682e94a59SJoachim Protze // task: task thunk for the completed task.
101782e94a59SJoachim Protze void __kmpc_omp_task_complete_if0(ident_t *loc_ref, kmp_int32 gtid,
101882e94a59SJoachim Protze                                   kmp_task_t *task) {
101982e94a59SJoachim Protze #if OMPT_SUPPORT
102082e94a59SJoachim Protze   if (UNLIKELY(ompt_enabled.enabled)) {
102182e94a59SJoachim Protze     __kmpc_omp_task_complete_if0_ompt(loc_ref, gtid, task);
102282e94a59SJoachim Protze     return;
102382e94a59SJoachim Protze   }
102482e94a59SJoachim Protze #endif
102582e94a59SJoachim Protze   __kmpc_omp_task_complete_if0_template<false>(loc_ref, gtid, task);
102682e94a59SJoachim Protze }
102782e94a59SJoachim Protze 
10287cc577a4SJonathan Peyton #ifdef TASK_UNUSED
10297cc577a4SJonathan Peyton // __kmpc_omp_task_complete: report that a task has completed execution
10307cc577a4SJonathan Peyton // NEVER GENERATED BY COMPILER, DEPRECATED!!!
10313041982dSJonathan Peyton void __kmpc_omp_task_complete(ident_t *loc_ref, kmp_int32 gtid,
10323041982dSJonathan Peyton                               kmp_task_t *task) {
10333041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n", gtid,
10343041982dSJonathan Peyton                 loc_ref, KMP_TASK_TO_TASKDATA(task)));
10357cc577a4SJonathan Peyton 
103686ed6aa6SJoachim Protze   __kmp_task_finish<false>(gtid, task,
103786ed6aa6SJoachim Protze                            NULL); // Not sure how to find task to resume
10387cc577a4SJonathan Peyton 
10393041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n", gtid,
10403041982dSJonathan Peyton                 loc_ref, KMP_TASK_TO_TASKDATA(task)));
10417cc577a4SJonathan Peyton   return;
10427cc577a4SJonathan Peyton }
10437cc577a4SJonathan Peyton #endif // TASK_UNUSED
10447cc577a4SJonathan Peyton 
10453041982dSJonathan Peyton // __kmp_init_implicit_task: Initialize the appropriate fields in the implicit
10463041982dSJonathan Peyton // task for a given thread
10477cc577a4SJonathan Peyton //
10487cc577a4SJonathan Peyton // loc_ref:  reference to source location of parallel region
10497cc577a4SJonathan Peyton // this_thr:  thread data structure corresponding to implicit task
10507cc577a4SJonathan Peyton // team: team for this_thr
10517cc577a4SJonathan Peyton // tid: thread id of given thread within team
10527cc577a4SJonathan Peyton // set_curr_task: TRUE if need to push current task to thread
10533041982dSJonathan Peyton // NOTE: Routine does not set up the implicit task ICVS.  This is assumed to
10543041982dSJonathan Peyton // have already been done elsewhere.
10557cc577a4SJonathan Peyton // TODO: Get better loc_ref.  Value passed in may be NULL
10563041982dSJonathan Peyton void __kmp_init_implicit_task(ident_t *loc_ref, kmp_info_t *this_thr,
10573041982dSJonathan Peyton                               kmp_team_t *team, int tid, int set_curr_task) {
10587cc577a4SJonathan Peyton   kmp_taskdata_t *task = &team->t.t_implicit_task_taskdata[tid];
10597cc577a4SJonathan Peyton 
10603041982dSJonathan Peyton   KF_TRACE(
10613041982dSJonathan Peyton       10,
10623041982dSJonathan Peyton       ("__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n",
10637cc577a4SJonathan Peyton        tid, team, task, set_curr_task ? "TRUE" : "FALSE"));
10647cc577a4SJonathan Peyton 
10657cc577a4SJonathan Peyton   task->td_task_id = KMP_GEN_TASK_ID();
10667cc577a4SJonathan Peyton   task->td_team = team;
10673041982dSJonathan Peyton   //    task->td_parent   = NULL;  // fix for CQ230101 (broken parent task info
10683041982dSJonathan Peyton   //    in debugger)
10697cc577a4SJonathan Peyton   task->td_ident = loc_ref;
10707cc577a4SJonathan Peyton   task->td_taskwait_ident = NULL;
10717cc577a4SJonathan Peyton   task->td_taskwait_counter = 0;
10727cc577a4SJonathan Peyton   task->td_taskwait_thread = 0;
10737cc577a4SJonathan Peyton 
10747cc577a4SJonathan Peyton   task->td_flags.tiedness = TASK_TIED;
10757cc577a4SJonathan Peyton   task->td_flags.tasktype = TASK_IMPLICIT;
10767cc577a4SJonathan Peyton #if OMP_45_ENABLED
10777cc577a4SJonathan Peyton   task->td_flags.proxy = TASK_FULL;
10787cc577a4SJonathan Peyton #endif
10797cc577a4SJonathan Peyton 
10807cc577a4SJonathan Peyton   // All implicit tasks are executed immediately, not deferred
10817cc577a4SJonathan Peyton   task->td_flags.task_serial = 1;
10827cc577a4SJonathan Peyton   task->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
10837cc577a4SJonathan Peyton   task->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
10847cc577a4SJonathan Peyton 
10857cc577a4SJonathan Peyton   task->td_flags.started = 1;
10867cc577a4SJonathan Peyton   task->td_flags.executing = 1;
10877cc577a4SJonathan Peyton   task->td_flags.complete = 0;
10887cc577a4SJonathan Peyton   task->td_flags.freed = 0;
10897cc577a4SJonathan Peyton 
10907cc577a4SJonathan Peyton #if OMP_40_ENABLED
10917cc577a4SJonathan Peyton   task->td_depnode = NULL;
10927cc577a4SJonathan Peyton #endif
1093a756cb24SAndrey Churbanov   task->td_last_tied = task;
1094405037c4SAndrey Churbanov #if OMP_50_ENABLED
1095405037c4SAndrey Churbanov   task->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1096405037c4SAndrey Churbanov #endif
10977cc577a4SJonathan Peyton 
10983041982dSJonathan Peyton   if (set_curr_task) { // only do this init first time thread is created
109937e2ef54SJonathan Peyton     KMP_ATOMIC_ST_REL(&task->td_incomplete_child_tasks, 0);
1100642688b6SJonathan Peyton     // Not used: don't need to deallocate implicit task
110137e2ef54SJonathan Peyton     KMP_ATOMIC_ST_REL(&task->td_allocated_child_tasks, 0);
11027cc577a4SJonathan Peyton #if OMP_40_ENABLED
11037cc577a4SJonathan Peyton     task->td_taskgroup = NULL; // An implicit task does not have taskgroup
11047cc577a4SJonathan Peyton     task->td_dephash = NULL;
11057cc577a4SJonathan Peyton #endif
11067cc577a4SJonathan Peyton     __kmp_push_current_task_to_thread(this_thr, team, tid);
11077cc577a4SJonathan Peyton   } else {
11087cc577a4SJonathan Peyton     KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0);
11097cc577a4SJonathan Peyton     KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0);
11107cc577a4SJonathan Peyton   }
11117cc577a4SJonathan Peyton 
11127cc577a4SJonathan Peyton #if OMPT_SUPPORT
1113f0a1c65fSJonas Hahnfeld   if (UNLIKELY(ompt_enabled.enabled))
111482e94a59SJoachim Protze     __ompt_task_init(task, tid);
11157cc577a4SJonathan Peyton #endif
11167cc577a4SJonathan Peyton 
11173041982dSJonathan Peyton   KF_TRACE(10, ("__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n", tid,
11183041982dSJonathan Peyton                 team, task));
11197cc577a4SJonathan Peyton }
11207cc577a4SJonathan Peyton 
11213041982dSJonathan Peyton // __kmp_finish_implicit_task: Release resources associated to implicit tasks
11223041982dSJonathan Peyton // at the end of parallel regions. Some resources are kept for reuse in the next
11233041982dSJonathan Peyton // parallel region.
11247cc577a4SJonathan Peyton //
11253041982dSJonathan Peyton // thread:  thread data structure corresponding to implicit task
11263041982dSJonathan Peyton void __kmp_finish_implicit_task(kmp_info_t *thread) {
11277cc577a4SJonathan Peyton   kmp_taskdata_t *task = thread->th.th_current_task;
1128c3344345SAndrey Churbanov   if (task->td_dephash) {
1129c3344345SAndrey Churbanov     int children;
1130c3344345SAndrey Churbanov     task->td_flags.complete = 1;
1131c3344345SAndrey Churbanov     children = KMP_ATOMIC_LD_ACQ(&task->td_incomplete_child_tasks);
1132c3344345SAndrey Churbanov     kmp_tasking_flags_t flags_old = task->td_flags;
1133c3344345SAndrey Churbanov     if (children == 0 && flags_old.complete == 1) {
1134c3344345SAndrey Churbanov       kmp_tasking_flags_t flags_new = flags_old;
1135c3344345SAndrey Churbanov       flags_new.complete = 0;
1136c3344345SAndrey Churbanov       if (KMP_COMPARE_AND_STORE_ACQ32(RCAST(kmp_int32 *, &task->td_flags),
1137c3344345SAndrey Churbanov                                       *RCAST(kmp_int32 *, &flags_old),
1138c3344345SAndrey Churbanov                                       *RCAST(kmp_int32 *, &flags_new))) {
1139c3344345SAndrey Churbanov         KA_TRACE(100, ("__kmp_finish_implicit_task: T#%d cleans "
1140c3344345SAndrey Churbanov                        "dephash of implicit task %p\n",
1141c3344345SAndrey Churbanov                        thread->th.th_info.ds.ds_gtid, task));
11427cc577a4SJonathan Peyton         __kmp_dephash_free_entries(thread, task->td_dephash);
11437cc577a4SJonathan Peyton       }
1144c3344345SAndrey Churbanov     }
1145c3344345SAndrey Churbanov   }
1146c3344345SAndrey Churbanov }
11477cc577a4SJonathan Peyton 
11483041982dSJonathan Peyton // __kmp_free_implicit_task: Release resources associated to implicit tasks
11493041982dSJonathan Peyton // when these are destroyed regions
11507cc577a4SJonathan Peyton //
11513041982dSJonathan Peyton // thread:  thread data structure corresponding to implicit task
11523041982dSJonathan Peyton void __kmp_free_implicit_task(kmp_info_t *thread) {
11537cc577a4SJonathan Peyton   kmp_taskdata_t *task = thread->th.th_current_task;
1154bff8ded9SJonathan Peyton   if (task && task->td_dephash) {
11557cc577a4SJonathan Peyton     __kmp_dephash_free(thread, task->td_dephash);
11567cc577a4SJonathan Peyton     task->td_dephash = NULL;
11577cc577a4SJonathan Peyton   }
1158bff8ded9SJonathan Peyton }
11597cc577a4SJonathan Peyton 
11603041982dSJonathan Peyton // Round up a size to a power of two specified by val: Used to insert padding
11613041982dSJonathan Peyton // between structures co-allocated using a single malloc() call
11623041982dSJonathan Peyton static size_t __kmp_round_up_to_val(size_t size, size_t val) {
11637cc577a4SJonathan Peyton   if (size & (val - 1)) {
11647cc577a4SJonathan Peyton     size &= ~(val - 1);
11657cc577a4SJonathan Peyton     if (size <= KMP_SIZE_T_MAX - val) {
11667cc577a4SJonathan Peyton       size += val; // Round up if there is no overflow.
1167bd3a7633SJonathan Peyton     }
1168bd3a7633SJonathan Peyton   }
11697cc577a4SJonathan Peyton   return size;
11707cc577a4SJonathan Peyton } // __kmp_round_up_to_va
11717cc577a4SJonathan Peyton 
11727cc577a4SJonathan Peyton // __kmp_task_alloc: Allocate the taskdata and task data structures for a task
11737cc577a4SJonathan Peyton //
11747cc577a4SJonathan Peyton // loc_ref: source location information
11757cc577a4SJonathan Peyton // gtid: global thread number.
11763041982dSJonathan Peyton // flags: include tiedness & task type (explicit vs. implicit) of the ''new''
11773041982dSJonathan Peyton // task encountered. Converted from kmp_int32 to kmp_tasking_flags_t in routine.
11783041982dSJonathan Peyton // sizeof_kmp_task_t:  Size in bytes of kmp_task_t data structure including
11793041982dSJonathan Peyton // private vars accessed in task.
11803041982dSJonathan Peyton // sizeof_shareds:  Size in bytes of array of pointers to shared vars accessed
11813041982dSJonathan Peyton // in task.
11827cc577a4SJonathan Peyton // task_entry: Pointer to task code entry point generated by compiler.
11837cc577a4SJonathan Peyton // returns: a pointer to the allocated kmp_task_t structure (task).
11843041982dSJonathan Peyton kmp_task_t *__kmp_task_alloc(ident_t *loc_ref, kmp_int32 gtid,
11853041982dSJonathan Peyton                              kmp_tasking_flags_t *flags,
11867cc577a4SJonathan Peyton                              size_t sizeof_kmp_task_t, size_t sizeof_shareds,
11873041982dSJonathan Peyton                              kmp_routine_entry_t task_entry) {
11887cc577a4SJonathan Peyton   kmp_task_t *task;
11897cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata;
11907cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
11917cc577a4SJonathan Peyton   kmp_team_t *team = thread->th.th_team;
11927cc577a4SJonathan Peyton   kmp_taskdata_t *parent_task = thread->th.th_current_task;
11937cc577a4SJonathan Peyton   size_t shareds_offset;
11947cc577a4SJonathan Peyton 
119528226e7dSJonathan Peyton   if (!TCR_4(__kmp_init_middle))
119628226e7dSJonathan Peyton     __kmp_middle_initialize();
119728226e7dSJonathan Peyton 
11987cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) "
11997cc577a4SJonathan Peyton                 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
12007cc577a4SJonathan Peyton                 gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t,
12017cc577a4SJonathan Peyton                 sizeof_shareds, task_entry));
12027cc577a4SJonathan Peyton 
12037cc577a4SJonathan Peyton   if (parent_task->td_flags.final) {
12047cc577a4SJonathan Peyton     if (flags->merged_if0) {
12057cc577a4SJonathan Peyton     }
12067cc577a4SJonathan Peyton     flags->final = 1;
12077cc577a4SJonathan Peyton   }
1208a756cb24SAndrey Churbanov   if (flags->tiedness == TASK_UNTIED && !team->t.t_serialized) {
1209a756cb24SAndrey Churbanov     // Untied task encountered causes the TSC algorithm to check entire deque of
1210a756cb24SAndrey Churbanov     // the victim thread. If no untied task encountered, then checking the head
1211a756cb24SAndrey Churbanov     // of the deque should be enough.
1212a756cb24SAndrey Churbanov     KMP_CHECK_UPDATE(thread->th.th_task_team->tt.tt_untied_task_encountered, 1);
1213a756cb24SAndrey Churbanov   }
12147cc577a4SJonathan Peyton 
1215405037c4SAndrey Churbanov #if OMP_50_ENABLED
1216405037c4SAndrey Churbanov   // Detachable tasks are not proxy tasks yet but could be in the future. Doing
1217405037c4SAndrey Churbanov   // the tasking setup
1218405037c4SAndrey Churbanov   // when that happens is too late.
1219405037c4SAndrey Churbanov   if (flags->proxy == TASK_PROXY || flags->detachable == TASK_DETACHABLE) {
1220405037c4SAndrey Churbanov #endif
12217cc577a4SJonathan Peyton #if OMP_45_ENABLED
12227cc577a4SJonathan Peyton     if (flags->proxy == TASK_PROXY) {
12237cc577a4SJonathan Peyton       flags->tiedness = TASK_UNTIED;
12247cc577a4SJonathan Peyton       flags->merged_if0 = 1;
1225405037c4SAndrey Churbanov #if OMP_50_ENABLED
1226405037c4SAndrey Churbanov     }
1227405037c4SAndrey Churbanov #endif
12283041982dSJonathan Peyton     /* are we running in a sequential parallel or tskm_immediate_exec... we need
12293041982dSJonathan Peyton        tasking support enabled */
12307cc577a4SJonathan Peyton     if ((thread->th.th_task_team) == NULL) {
12317cc577a4SJonathan Peyton       /* This should only happen if the team is serialized
12323041982dSJonathan Peyton           setup a task team and propagate it to the thread */
12337cc577a4SJonathan Peyton       KMP_DEBUG_ASSERT(team->t.t_serialized);
12343041982dSJonathan Peyton       KA_TRACE(30,
12353041982dSJonathan Peyton                ("T#%d creating task team in __kmp_task_alloc for proxy task\n",
12363041982dSJonathan Peyton                 gtid));
12373041982dSJonathan Peyton       __kmp_task_team_setup(
12383041982dSJonathan Peyton           thread, team,
12393041982dSJonathan Peyton           1); // 1 indicates setup the current team regardless of nthreads
12407cc577a4SJonathan Peyton       thread->th.th_task_team = team->t.t_task_team[thread->th.th_task_state];
12417cc577a4SJonathan Peyton     }
12427cc577a4SJonathan Peyton     kmp_task_team_t *task_team = thread->th.th_task_team;
12437cc577a4SJonathan Peyton 
12447cc577a4SJonathan Peyton     /* tasking must be enabled now as the task might not be pushed */
12457cc577a4SJonathan Peyton     if (!KMP_TASKING_ENABLED(task_team)) {
12463041982dSJonathan Peyton       KA_TRACE(
12473041982dSJonathan Peyton           30,
12483041982dSJonathan Peyton           ("T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid));
12497cc577a4SJonathan Peyton       __kmp_enable_tasking(task_team, thread);
12507cc577a4SJonathan Peyton       kmp_int32 tid = thread->th.th_info.ds.ds_tid;
12517cc577a4SJonathan Peyton       kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
12527cc577a4SJonathan Peyton       // No lock needed since only owner can allocate
12537cc577a4SJonathan Peyton       if (thread_data->td.td_deque == NULL) {
12547cc577a4SJonathan Peyton         __kmp_alloc_task_deque(thread, thread_data);
12557cc577a4SJonathan Peyton       }
12567cc577a4SJonathan Peyton     }
12577cc577a4SJonathan Peyton 
12587cc577a4SJonathan Peyton     if (task_team->tt.tt_found_proxy_tasks == FALSE)
12597cc577a4SJonathan Peyton       TCW_4(task_team->tt.tt_found_proxy_tasks, TRUE);
12607cc577a4SJonathan Peyton   }
12617cc577a4SJonathan Peyton #endif
12627cc577a4SJonathan Peyton 
12637cc577a4SJonathan Peyton   // Calculate shared structure offset including padding after kmp_task_t struct
12647cc577a4SJonathan Peyton   // to align pointers in shared struct
12657cc577a4SJonathan Peyton   shareds_offset = sizeof(kmp_taskdata_t) + sizeof_kmp_task_t;
12667cc577a4SJonathan Peyton   shareds_offset = __kmp_round_up_to_val(shareds_offset, sizeof(void *));
12677cc577a4SJonathan Peyton 
12687cc577a4SJonathan Peyton   // Allocate a kmp_taskdata_t block and a kmp_task_t block.
12693041982dSJonathan Peyton   KA_TRACE(30, ("__kmp_task_alloc: T#%d First malloc size: %ld\n", gtid,
12703041982dSJonathan Peyton                 shareds_offset));
12713041982dSJonathan Peyton   KA_TRACE(30, ("__kmp_task_alloc: T#%d Second malloc size: %ld\n", gtid,
12723041982dSJonathan Peyton                 sizeof_shareds));
12737cc577a4SJonathan Peyton 
12747cc577a4SJonathan Peyton // Avoid double allocation here by combining shareds with taskdata
12757cc577a4SJonathan Peyton #if USE_FAST_MEMORY
12763041982dSJonathan Peyton   taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, shareds_offset +
12773041982dSJonathan Peyton                                                                sizeof_shareds);
12787cc577a4SJonathan Peyton #else /* ! USE_FAST_MEMORY */
12793041982dSJonathan Peyton   taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, shareds_offset +
12803041982dSJonathan Peyton                                                                sizeof_shareds);
12817cc577a4SJonathan Peyton #endif /* USE_FAST_MEMORY */
12827cc577a4SJonathan Peyton   ANNOTATE_HAPPENS_AFTER(taskdata);
12837cc577a4SJonathan Peyton 
12847cc577a4SJonathan Peyton   task = KMP_TASKDATA_TO_TASK(taskdata);
12857cc577a4SJonathan Peyton 
12867cc577a4SJonathan Peyton // Make sure task & taskdata are aligned appropriately
12877cc577a4SJonathan Peyton #if KMP_ARCH_X86 || KMP_ARCH_PPC64 || !KMP_HAVE_QUAD
12887cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (sizeof(double) - 1)) == 0);
12897cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (sizeof(double) - 1)) == 0);
12907cc577a4SJonathan Peyton #else
12917cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (sizeof(_Quad) - 1)) == 0);
12927cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (sizeof(_Quad) - 1)) == 0);
12937cc577a4SJonathan Peyton #endif
12947cc577a4SJonathan Peyton   if (sizeof_shareds > 0) {
12957cc577a4SJonathan Peyton     // Avoid double allocation here by combining shareds with taskdata
12967cc577a4SJonathan Peyton     task->shareds = &((char *)taskdata)[shareds_offset];
12977cc577a4SJonathan Peyton     // Make sure shareds struct is aligned to pointer size
12983041982dSJonathan Peyton     KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (sizeof(void *) - 1)) ==
12993041982dSJonathan Peyton                      0);
13007cc577a4SJonathan Peyton   } else {
13017cc577a4SJonathan Peyton     task->shareds = NULL;
13027cc577a4SJonathan Peyton   }
13037cc577a4SJonathan Peyton   task->routine = task_entry;
13047cc577a4SJonathan Peyton   task->part_id = 0; // AC: Always start with 0 part id
13057cc577a4SJonathan Peyton 
13067cc577a4SJonathan Peyton   taskdata->td_task_id = KMP_GEN_TASK_ID();
13077cc577a4SJonathan Peyton   taskdata->td_team = team;
13087cc577a4SJonathan Peyton   taskdata->td_alloc_thread = thread;
13097cc577a4SJonathan Peyton   taskdata->td_parent = parent_task;
13107cc577a4SJonathan Peyton   taskdata->td_level = parent_task->td_level + 1; // increment nesting level
131137e2ef54SJonathan Peyton   KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
13127cc577a4SJonathan Peyton   taskdata->td_ident = loc_ref;
13137cc577a4SJonathan Peyton   taskdata->td_taskwait_ident = NULL;
13147cc577a4SJonathan Peyton   taskdata->td_taskwait_counter = 0;
13157cc577a4SJonathan Peyton   taskdata->td_taskwait_thread = 0;
13167cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_parent != NULL);
13177cc577a4SJonathan Peyton #if OMP_45_ENABLED
13187cc577a4SJonathan Peyton   // avoid copying icvs for proxy tasks
13197cc577a4SJonathan Peyton   if (flags->proxy == TASK_FULL)
13207cc577a4SJonathan Peyton #endif
13217cc577a4SJonathan Peyton     copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs);
13227cc577a4SJonathan Peyton 
13237cc577a4SJonathan Peyton   taskdata->td_flags.tiedness = flags->tiedness;
13247cc577a4SJonathan Peyton   taskdata->td_flags.final = flags->final;
13257cc577a4SJonathan Peyton   taskdata->td_flags.merged_if0 = flags->merged_if0;
13267cc577a4SJonathan Peyton #if OMP_40_ENABLED
13277cc577a4SJonathan Peyton   taskdata->td_flags.destructors_thunk = flags->destructors_thunk;
13287cc577a4SJonathan Peyton #endif // OMP_40_ENABLED
13297cc577a4SJonathan Peyton #if OMP_45_ENABLED
13307cc577a4SJonathan Peyton   taskdata->td_flags.proxy = flags->proxy;
1331405037c4SAndrey Churbanov #if OMP_50_ENABLED
1332405037c4SAndrey Churbanov   taskdata->td_flags.detachable = flags->detachable;
1333405037c4SAndrey Churbanov #endif
13347cc577a4SJonathan Peyton   taskdata->td_task_team = thread->th.th_task_team;
13357cc577a4SJonathan Peyton   taskdata->td_size_alloc = shareds_offset + sizeof_shareds;
13367cc577a4SJonathan Peyton #endif
13377cc577a4SJonathan Peyton   taskdata->td_flags.tasktype = TASK_EXPLICIT;
13387cc577a4SJonathan Peyton 
13397cc577a4SJonathan Peyton   // GEH - TODO: fix this to copy parent task's value of tasking_ser flag
13407cc577a4SJonathan Peyton   taskdata->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
13417cc577a4SJonathan Peyton 
13427cc577a4SJonathan Peyton   // GEH - TODO: fix this to copy parent task's value of team_serial flag
13437cc577a4SJonathan Peyton   taskdata->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
13447cc577a4SJonathan Peyton 
13453041982dSJonathan Peyton   // GEH - Note we serialize the task if the team is serialized to make sure
13463041982dSJonathan Peyton   // implicit parallel region tasks are not left until program termination to
13473041982dSJonathan Peyton   // execute. Also, it helps locality to execute immediately.
13483041982dSJonathan Peyton 
13493041982dSJonathan Peyton   taskdata->td_flags.task_serial =
13503041982dSJonathan Peyton       (parent_task->td_flags.final || taskdata->td_flags.team_serial ||
13513041982dSJonathan Peyton        taskdata->td_flags.tasking_ser);
13527cc577a4SJonathan Peyton 
13537cc577a4SJonathan Peyton   taskdata->td_flags.started = 0;
13547cc577a4SJonathan Peyton   taskdata->td_flags.executing = 0;
13557cc577a4SJonathan Peyton   taskdata->td_flags.complete = 0;
13567cc577a4SJonathan Peyton   taskdata->td_flags.freed = 0;
13577cc577a4SJonathan Peyton 
13587cc577a4SJonathan Peyton   taskdata->td_flags.native = flags->native;
13597cc577a4SJonathan Peyton 
136037e2ef54SJonathan Peyton   KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0);
136137e2ef54SJonathan Peyton   // start at one because counts current task and children
136237e2ef54SJonathan Peyton   KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1);
13637cc577a4SJonathan Peyton #if OMP_40_ENABLED
13643041982dSJonathan Peyton   taskdata->td_taskgroup =
13653041982dSJonathan Peyton       parent_task->td_taskgroup; // task inherits taskgroup from the parent task
13667cc577a4SJonathan Peyton   taskdata->td_dephash = NULL;
13677cc577a4SJonathan Peyton   taskdata->td_depnode = NULL;
13687cc577a4SJonathan Peyton #endif
1369a756cb24SAndrey Churbanov   if (flags->tiedness == TASK_UNTIED)
1370a756cb24SAndrey Churbanov     taskdata->td_last_tied = NULL; // will be set when the task is scheduled
1371a756cb24SAndrey Churbanov   else
1372a756cb24SAndrey Churbanov     taskdata->td_last_tied = taskdata;
1373405037c4SAndrey Churbanov #if OMP_50_ENABLED
1374405037c4SAndrey Churbanov   taskdata->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1375405037c4SAndrey Churbanov #endif
137686ed6aa6SJoachim Protze #if OMPT_SUPPORT
137786ed6aa6SJoachim Protze   if (UNLIKELY(ompt_enabled.enabled))
137886ed6aa6SJoachim Protze     __ompt_task_init(taskdata, gtid);
137986ed6aa6SJoachim Protze #endif
13803041982dSJonathan Peyton // Only need to keep track of child task counts if team parallel and tasking not
1381405037c4SAndrey Churbanov // serialized or if it is a proxy or detachable task
13827cc577a4SJonathan Peyton #if OMP_45_ENABLED
13833041982dSJonathan Peyton   if (flags->proxy == TASK_PROXY ||
1384405037c4SAndrey Churbanov #if OMP_50_ENABLED
1385405037c4SAndrey Churbanov       flags->detachable == TASK_DETACHABLE ||
1386405037c4SAndrey Churbanov #endif
13873041982dSJonathan Peyton       !(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser))
13887cc577a4SJonathan Peyton #else
13897cc577a4SJonathan Peyton   if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser))
13907cc577a4SJonathan Peyton #endif
13917cc577a4SJonathan Peyton   {
139237e2ef54SJonathan Peyton     KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
13937cc577a4SJonathan Peyton #if OMP_40_ENABLED
13947cc577a4SJonathan Peyton     if (parent_task->td_taskgroup)
139537e2ef54SJonathan Peyton       KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
13967cc577a4SJonathan Peyton #endif
13973041982dSJonathan Peyton     // Only need to keep track of allocated child tasks for explicit tasks since
13983041982dSJonathan Peyton     // implicit not deallocated
13997cc577a4SJonathan Peyton     if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) {
140037e2ef54SJonathan Peyton       KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
14017cc577a4SJonathan Peyton     }
14027cc577a4SJonathan Peyton   }
14037cc577a4SJonathan Peyton 
14047cc577a4SJonathan Peyton   KA_TRACE(20, ("__kmp_task_alloc(exit): T#%d created task %p parent=%p\n",
14057cc577a4SJonathan Peyton                 gtid, taskdata, taskdata->td_parent));
14067cc577a4SJonathan Peyton   ANNOTATE_HAPPENS_BEFORE(task);
14077cc577a4SJonathan Peyton 
14087cc577a4SJonathan Peyton   return task;
14097cc577a4SJonathan Peyton }
14107cc577a4SJonathan Peyton 
14113041982dSJonathan Peyton kmp_task_t *__kmpc_omp_task_alloc(ident_t *loc_ref, kmp_int32 gtid,
14123041982dSJonathan Peyton                                   kmp_int32 flags, size_t sizeof_kmp_task_t,
14133041982dSJonathan Peyton                                   size_t sizeof_shareds,
14143041982dSJonathan Peyton                                   kmp_routine_entry_t task_entry) {
14157cc577a4SJonathan Peyton   kmp_task_t *retval;
14167cc577a4SJonathan Peyton   kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
14177cc577a4SJonathan Peyton 
14187cc577a4SJonathan Peyton   input_flags->native = FALSE;
14197cc577a4SJonathan Peyton // __kmp_task_alloc() sets up all other runtime flags
14207cc577a4SJonathan Peyton 
14217cc577a4SJonathan Peyton #if OMP_45_ENABLED
1422405037c4SAndrey Churbanov #if OMP_50_ENABLED
1423405037c4SAndrey Churbanov   KA_TRACE(10, ("__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s %s) "
1424405037c4SAndrey Churbanov                 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1425405037c4SAndrey Churbanov                 gtid, loc_ref, input_flags->tiedness ? "tied  " : "untied",
1426405037c4SAndrey Churbanov                 input_flags->proxy ? "proxy" : "",
1427405037c4SAndrey Churbanov                 input_flags->detachable ? "detachable" : "", sizeof_kmp_task_t,
1428405037c4SAndrey Churbanov                 sizeof_shareds, task_entry));
1429405037c4SAndrey Churbanov #else
14307cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s) "
14317cc577a4SJonathan Peyton                 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
14327cc577a4SJonathan Peyton                 gtid, loc_ref, input_flags->tiedness ? "tied  " : "untied",
14333041982dSJonathan Peyton                 input_flags->proxy ? "proxy" : "", sizeof_kmp_task_t,
14343041982dSJonathan Peyton                 sizeof_shareds, task_entry));
1435405037c4SAndrey Churbanov #endif
14367cc577a4SJonathan Peyton #else
14377cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s) "
14387cc577a4SJonathan Peyton                 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
14397cc577a4SJonathan Peyton                 gtid, loc_ref, input_flags->tiedness ? "tied  " : "untied",
14407cc577a4SJonathan Peyton                 sizeof_kmp_task_t, sizeof_shareds, task_entry));
14417cc577a4SJonathan Peyton #endif
14427cc577a4SJonathan Peyton 
14437cc577a4SJonathan Peyton   retval = __kmp_task_alloc(loc_ref, gtid, input_flags, sizeof_kmp_task_t,
14447cc577a4SJonathan Peyton                             sizeof_shareds, task_entry);
14457cc577a4SJonathan Peyton 
14467cc577a4SJonathan Peyton   KA_TRACE(20, ("__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval));
14477cc577a4SJonathan Peyton 
14487cc577a4SJonathan Peyton   return retval;
14497cc577a4SJonathan Peyton }
14507cc577a4SJonathan Peyton 
1451b48e44a6SGheorghe-Teodor Bercea kmp_task_t *__kmpc_omp_target_task_alloc(ident_t *loc_ref, kmp_int32 gtid,
1452b48e44a6SGheorghe-Teodor Bercea                                          kmp_int32 flags,
1453b48e44a6SGheorghe-Teodor Bercea                                          size_t sizeof_kmp_task_t,
1454b48e44a6SGheorghe-Teodor Bercea                                          size_t sizeof_shareds,
1455b48e44a6SGheorghe-Teodor Bercea                                          kmp_routine_entry_t task_entry,
1456b48e44a6SGheorghe-Teodor Bercea                                          kmp_int64 device_id) {
1457b48e44a6SGheorghe-Teodor Bercea   return __kmpc_omp_task_alloc(loc_ref, gtid, flags, sizeof_kmp_task_t,
1458b48e44a6SGheorghe-Teodor Bercea                                sizeof_shareds, task_entry);
1459b48e44a6SGheorghe-Teodor Bercea }
1460b48e44a6SGheorghe-Teodor Bercea 
1461b04f7d68SJonathan Peyton #if OMP_50_ENABLED
1462b04f7d68SJonathan Peyton /*!
1463b04f7d68SJonathan Peyton @ingroup TASKING
1464b04f7d68SJonathan Peyton @param loc_ref location of the original task directive
1465b04f7d68SJonathan Peyton @param gtid Global Thread ID of encountering thread
1466b04f7d68SJonathan Peyton @param new_task task thunk allocated by __kmpc_omp_task_alloc() for the ''new
1467b04f7d68SJonathan Peyton task''
1468b04f7d68SJonathan Peyton @param naffins Number of affinity items
1469b04f7d68SJonathan Peyton @param affin_list List of affinity items
1470b04f7d68SJonathan Peyton @return Returns non-zero if registering affinity information was not successful.
1471b04f7d68SJonathan Peyton  Returns 0 if registration was successful
1472b04f7d68SJonathan Peyton This entry registers the affinity information attached to a task with the task
1473b04f7d68SJonathan Peyton thunk structure kmp_taskdata_t.
1474b04f7d68SJonathan Peyton */
1475b04f7d68SJonathan Peyton kmp_int32
1476b04f7d68SJonathan Peyton __kmpc_omp_reg_task_with_affinity(ident_t *loc_ref, kmp_int32 gtid,
1477b04f7d68SJonathan Peyton                                   kmp_task_t *new_task, kmp_int32 naffins,
1478b04f7d68SJonathan Peyton                                   kmp_task_affinity_info_t *affin_list) {
1479b04f7d68SJonathan Peyton   return 0;
1480b04f7d68SJonathan Peyton }
1481b04f7d68SJonathan Peyton #endif
1482b04f7d68SJonathan Peyton 
14837cc577a4SJonathan Peyton //  __kmp_invoke_task: invoke the specified task
14847cc577a4SJonathan Peyton //
14857cc577a4SJonathan Peyton // gtid: global thread ID of caller
14867cc577a4SJonathan Peyton // task: the task to invoke
14877cc577a4SJonathan Peyton // current_task: the task to resume after task invokation
14883041982dSJonathan Peyton static void __kmp_invoke_task(kmp_int32 gtid, kmp_task_t *task,
14893041982dSJonathan Peyton                               kmp_taskdata_t *current_task) {
14907cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
1491b1b221c8SJonathan Peyton   kmp_info_t *thread;
14927cc577a4SJonathan Peyton #if OMP_40_ENABLED
14937cc577a4SJonathan Peyton   int discard = 0 /* false */;
14947cc577a4SJonathan Peyton #endif
14953041982dSJonathan Peyton   KA_TRACE(
14963041982dSJonathan Peyton       30, ("__kmp_invoke_task(enter): T#%d invoking task %p, current_task=%p\n",
14977cc577a4SJonathan Peyton            gtid, taskdata, current_task));
14987cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task);
14997cc577a4SJonathan Peyton #if OMP_45_ENABLED
15007cc577a4SJonathan Peyton   if (taskdata->td_flags.proxy == TASK_PROXY &&
15013041982dSJonathan Peyton       taskdata->td_flags.complete == 1) {
15027cc577a4SJonathan Peyton     // This is a proxy task that was already completed but it needs to run
15037cc577a4SJonathan Peyton     // its bottom-half finish
15043041982dSJonathan Peyton     KA_TRACE(
15053041982dSJonathan Peyton         30,
15063041982dSJonathan Peyton         ("__kmp_invoke_task: T#%d running bottom finish for proxy task %p\n",
15077cc577a4SJonathan Peyton          gtid, taskdata));
15087cc577a4SJonathan Peyton 
15097cc577a4SJonathan Peyton     __kmp_bottom_half_finish_proxy(gtid, task);
15107cc577a4SJonathan Peyton 
15113041982dSJonathan Peyton     KA_TRACE(30, ("__kmp_invoke_task(exit): T#%d completed bottom finish for "
15123041982dSJonathan Peyton                   "proxy task %p, resuming task %p\n",
15133041982dSJonathan Peyton                   gtid, taskdata, current_task));
15147cc577a4SJonathan Peyton 
15157cc577a4SJonathan Peyton     return;
15167cc577a4SJonathan Peyton   }
15177cc577a4SJonathan Peyton #endif
15187cc577a4SJonathan Peyton 
15197cc577a4SJonathan Peyton #if OMPT_SUPPORT
152086ed6aa6SJoachim Protze   // For untied tasks, the first task executed only calls __kmpc_omp_task and
152186ed6aa6SJoachim Protze   // does not execute code.
15227cc577a4SJonathan Peyton   ompt_thread_info_t oldInfo;
1523f0a1c65fSJonas Hahnfeld   if (UNLIKELY(ompt_enabled.enabled)) {
15247cc577a4SJonathan Peyton     // Store the threads states and restore them after the task
15257cc577a4SJonathan Peyton     thread = __kmp_threads[gtid];
15267cc577a4SJonathan Peyton     oldInfo = thread->th.ompt_thread_info;
15277cc577a4SJonathan Peyton     thread->th.ompt_thread_info.wait_id = 0;
152882e94a59SJoachim Protze     thread->th.ompt_thread_info.state = (thread->th.th_team_serialized)
15290e0d6cddSJoachim Protze                                             ? ompt_state_work_serial
15300e0d6cddSJoachim Protze                                             : ompt_state_work_parallel;
15310e0d6cddSJoachim Protze     taskdata->ompt_task_info.frame.exit_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
15327cc577a4SJonathan Peyton   }
15337cc577a4SJonathan Peyton #endif
15347cc577a4SJonathan Peyton 
153586ed6aa6SJoachim Protze #if OMP_45_ENABLED
153686ed6aa6SJoachim Protze   // Proxy tasks are not handled by the runtime
153786ed6aa6SJoachim Protze   if (taskdata->td_flags.proxy != TASK_PROXY) {
153886ed6aa6SJoachim Protze #endif
153986ed6aa6SJoachim Protze     ANNOTATE_HAPPENS_AFTER(task);
154086ed6aa6SJoachim Protze     __kmp_task_start(gtid, task, current_task); // OMPT only if not discarded
154186ed6aa6SJoachim Protze #if OMP_45_ENABLED
154286ed6aa6SJoachim Protze   }
154386ed6aa6SJoachim Protze #endif
154486ed6aa6SJoachim Protze 
15457cc577a4SJonathan Peyton #if OMP_40_ENABLED
15467cc577a4SJonathan Peyton   // TODO: cancel tasks if the parallel region has also been cancelled
15477cc577a4SJonathan Peyton   // TODO: check if this sequence can be hoisted above __kmp_task_start
15487cc577a4SJonathan Peyton   // if cancellation has been enabled for this run ...
15497cc577a4SJonathan Peyton   if (__kmp_omp_cancellation) {
1550b1b221c8SJonathan Peyton     thread = __kmp_threads[gtid];
1551b1b221c8SJonathan Peyton     kmp_team_t *this_team = thread->th.th_team;
15527cc577a4SJonathan Peyton     kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
15533041982dSJonathan Peyton     if ((taskgroup && taskgroup->cancel_request) ||
15543041982dSJonathan Peyton         (this_team->t.t_cancel_request == cancel_parallel)) {
155582e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
155682e94a59SJoachim Protze       ompt_data_t *task_data;
1557f0a1c65fSJonas Hahnfeld       if (UNLIKELY(ompt_enabled.ompt_callback_cancel)) {
155882e94a59SJoachim Protze         __ompt_get_task_info_internal(0, NULL, &task_data, NULL, NULL, NULL);
155982e94a59SJoachim Protze         ompt_callbacks.ompt_callback(ompt_callback_cancel)(
156082e94a59SJoachim Protze             task_data,
156182e94a59SJoachim Protze             ((taskgroup && taskgroup->cancel_request) ? ompt_cancel_taskgroup
156282e94a59SJoachim Protze                                                       : ompt_cancel_parallel) |
156382e94a59SJoachim Protze                 ompt_cancel_discarded_task,
156482e94a59SJoachim Protze             NULL);
156582e94a59SJoachim Protze       }
156682e94a59SJoachim Protze #endif
15677cc577a4SJonathan Peyton       KMP_COUNT_BLOCK(TASK_cancelled);
15687cc577a4SJonathan Peyton       // this task belongs to a task group and we need to cancel it
15697cc577a4SJonathan Peyton       discard = 1 /* true */;
15707cc577a4SJonathan Peyton     }
15717cc577a4SJonathan Peyton   }
15727cc577a4SJonathan Peyton 
15737cc577a4SJonathan Peyton   // Invoke the task routine and pass in relevant data.
15747cc577a4SJonathan Peyton   // Thunks generated by gcc take a different argument list.
15757cc577a4SJonathan Peyton   if (!discard) {
1576a756cb24SAndrey Churbanov     if (taskdata->td_flags.tiedness == TASK_UNTIED) {
1577a756cb24SAndrey Churbanov       taskdata->td_last_tied = current_task->td_last_tied;
1578a756cb24SAndrey Churbanov       KMP_DEBUG_ASSERT(taskdata->td_last_tied);
1579a756cb24SAndrey Churbanov     }
15807cc577a4SJonathan Peyton #if KMP_STATS_ENABLED
15817cc577a4SJonathan Peyton     KMP_COUNT_BLOCK(TASK_executed);
15827cc577a4SJonathan Peyton     switch (KMP_GET_THREAD_STATE()) {
15833041982dSJonathan Peyton     case FORK_JOIN_BARRIER:
15843041982dSJonathan Peyton       KMP_PUSH_PARTITIONED_TIMER(OMP_task_join_bar);
15853041982dSJonathan Peyton       break;
15863041982dSJonathan Peyton     case PLAIN_BARRIER:
15873041982dSJonathan Peyton       KMP_PUSH_PARTITIONED_TIMER(OMP_task_plain_bar);
15883041982dSJonathan Peyton       break;
15893041982dSJonathan Peyton     case TASKYIELD:
15903041982dSJonathan Peyton       KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskyield);
15913041982dSJonathan Peyton       break;
15923041982dSJonathan Peyton     case TASKWAIT:
15933041982dSJonathan Peyton       KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskwait);
15943041982dSJonathan Peyton       break;
15953041982dSJonathan Peyton     case TASKGROUP:
15963041982dSJonathan Peyton       KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskgroup);
15973041982dSJonathan Peyton       break;
15983041982dSJonathan Peyton     default:
15993041982dSJonathan Peyton       KMP_PUSH_PARTITIONED_TIMER(OMP_task_immediate);
16003041982dSJonathan Peyton       break;
16017cc577a4SJonathan Peyton     }
16027cc577a4SJonathan Peyton #endif // KMP_STATS_ENABLED
16037cc577a4SJonathan Peyton #endif // OMP_40_ENABLED
16047cc577a4SJonathan Peyton 
160582e94a59SJoachim Protze // OMPT task begin
160682e94a59SJoachim Protze #if OMPT_SUPPORT
1607f0a1c65fSJonas Hahnfeld     if (UNLIKELY(ompt_enabled.enabled))
160882e94a59SJoachim Protze       __ompt_task_start(task, current_task, gtid);
16097cc577a4SJonathan Peyton #endif
16107cc577a4SJonathan Peyton 
1611b1b221c8SJonathan Peyton #if USE_ITT_BUILD && USE_ITT_NOTIFY
1612b1b221c8SJonathan Peyton     kmp_uint64 cur_time;
1613b1b221c8SJonathan Peyton     kmp_int32 kmp_itt_count_task =
1614b1b221c8SJonathan Peyton         __kmp_forkjoin_frames_mode == 3 && !taskdata->td_flags.task_serial &&
1615b1b221c8SJonathan Peyton         current_task->td_flags.tasktype == TASK_IMPLICIT;
1616b1b221c8SJonathan Peyton     if (kmp_itt_count_task) {
1617b1b221c8SJonathan Peyton       thread = __kmp_threads[gtid];
1618b1b221c8SJonathan Peyton       // Time outer level explicit task on barrier for adjusting imbalance time
1619b1b221c8SJonathan Peyton       if (thread->th.th_bar_arrive_time)
1620b1b221c8SJonathan Peyton         cur_time = __itt_get_timestamp();
1621b1b221c8SJonathan Peyton       else
1622b1b221c8SJonathan Peyton         kmp_itt_count_task = 0; // thread is not on a barrier - skip timing
1623b1b221c8SJonathan Peyton     }
1624b1b221c8SJonathan Peyton #endif
1625b1b221c8SJonathan Peyton 
16267cc577a4SJonathan Peyton #ifdef KMP_GOMP_COMPAT
16277cc577a4SJonathan Peyton     if (taskdata->td_flags.native) {
16287cc577a4SJonathan Peyton       ((void (*)(void *))(*(task->routine)))(task->shareds);
16293041982dSJonathan Peyton     } else
16307cc577a4SJonathan Peyton #endif /* KMP_GOMP_COMPAT */
16317cc577a4SJonathan Peyton     {
16327cc577a4SJonathan Peyton       (*(task->routine))(gtid, task);
16337cc577a4SJonathan Peyton     }
16347cc577a4SJonathan Peyton     KMP_POP_PARTITIONED_TIMER();
16357cc577a4SJonathan Peyton 
1636b1b221c8SJonathan Peyton #if USE_ITT_BUILD && USE_ITT_NOTIFY
1637b1b221c8SJonathan Peyton     if (kmp_itt_count_task) {
1638b1b221c8SJonathan Peyton       // Barrier imbalance - adjust arrive time with the task duration
1639b1b221c8SJonathan Peyton       thread->th.th_bar_arrive_time += (__itt_get_timestamp() - cur_time);
1640b1b221c8SJonathan Peyton     }
1641b1b221c8SJonathan Peyton #endif
1642b1b221c8SJonathan Peyton 
16437cc577a4SJonathan Peyton #if OMP_40_ENABLED
16447cc577a4SJonathan Peyton   }
16457cc577a4SJonathan Peyton #endif // OMP_40_ENABLED
16467cc577a4SJonathan Peyton 
16477cc577a4SJonathan Peyton 
16487cc577a4SJonathan Peyton #if OMP_45_ENABLED
16497cc577a4SJonathan Peyton   // Proxy tasks are not handled by the runtime
16507cc577a4SJonathan Peyton   if (taskdata->td_flags.proxy != TASK_PROXY) {
16517cc577a4SJonathan Peyton #endif
16527cc577a4SJonathan Peyton     ANNOTATE_HAPPENS_BEFORE(taskdata->td_parent);
165386ed6aa6SJoachim Protze #if OMPT_SUPPORT
165486ed6aa6SJoachim Protze     if (UNLIKELY(ompt_enabled.enabled)) {
165586ed6aa6SJoachim Protze       thread->th.ompt_thread_info = oldInfo;
165686ed6aa6SJoachim Protze       if (taskdata->td_flags.tiedness == TASK_TIED) {
16570e0d6cddSJoachim Protze         taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
165886ed6aa6SJoachim Protze       }
165986ed6aa6SJoachim Protze       __kmp_task_finish<true>(gtid, task, current_task);
166086ed6aa6SJoachim Protze     } else
166186ed6aa6SJoachim Protze #endif
166286ed6aa6SJoachim Protze       __kmp_task_finish<false>(gtid, task, current_task);
16637cc577a4SJonathan Peyton #if OMP_45_ENABLED
16647cc577a4SJonathan Peyton   }
16657cc577a4SJonathan Peyton #endif
16667cc577a4SJonathan Peyton 
16673041982dSJonathan Peyton   KA_TRACE(
16683041982dSJonathan Peyton       30,
16693041982dSJonathan Peyton       ("__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n",
16707cc577a4SJonathan Peyton        gtid, taskdata, current_task));
16717cc577a4SJonathan Peyton   return;
16727cc577a4SJonathan Peyton }
16737cc577a4SJonathan Peyton 
16747cc577a4SJonathan Peyton // __kmpc_omp_task_parts: Schedule a thread-switchable task for execution
16757cc577a4SJonathan Peyton //
16767cc577a4SJonathan Peyton // loc_ref: location of original task pragma (ignored)
16777cc577a4SJonathan Peyton // gtid: Global Thread ID of encountering thread
16787cc577a4SJonathan Peyton // new_task: task thunk allocated by __kmp_omp_task_alloc() for the ''new task''
16797cc577a4SJonathan Peyton // Returns:
16803041982dSJonathan Peyton //    TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to
16813041982dSJonathan Peyton //    be resumed later.
16823041982dSJonathan Peyton //    TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be
16833041982dSJonathan Peyton //    resumed later.
16843041982dSJonathan Peyton kmp_int32 __kmpc_omp_task_parts(ident_t *loc_ref, kmp_int32 gtid,
16853041982dSJonathan Peyton                                 kmp_task_t *new_task) {
16867cc577a4SJonathan Peyton   kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
16877cc577a4SJonathan Peyton 
16883041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n", gtid,
16893041982dSJonathan Peyton                 loc_ref, new_taskdata));
16907cc577a4SJonathan Peyton 
169182e94a59SJoachim Protze #if OMPT_SUPPORT
169282e94a59SJoachim Protze   kmp_taskdata_t *parent;
1693f0a1c65fSJonas Hahnfeld   if (UNLIKELY(ompt_enabled.enabled)) {
169482e94a59SJoachim Protze     parent = new_taskdata->td_parent;
169582e94a59SJoachim Protze     if (ompt_enabled.ompt_callback_task_create) {
169682e94a59SJoachim Protze       ompt_data_t task_data = ompt_data_none;
169782e94a59SJoachim Protze       ompt_callbacks.ompt_callback(ompt_callback_task_create)(
169882e94a59SJoachim Protze           parent ? &(parent->ompt_task_info.task_data) : &task_data,
169982e94a59SJoachim Protze           parent ? &(parent->ompt_task_info.frame) : NULL,
170082e94a59SJoachim Protze           &(new_taskdata->ompt_task_info.task_data), ompt_task_explicit, 0,
170182e94a59SJoachim Protze           OMPT_GET_RETURN_ADDRESS(0));
170282e94a59SJoachim Protze     }
170382e94a59SJoachim Protze   }
170482e94a59SJoachim Protze #endif
170582e94a59SJoachim Protze 
17063041982dSJonathan Peyton   /* Should we execute the new task or queue it? For now, let's just always try
17073041982dSJonathan Peyton      to queue it.  If the queue fills up, then we'll execute it.  */
17087cc577a4SJonathan Peyton 
17097cc577a4SJonathan Peyton   if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED) // if cannot defer
17107cc577a4SJonathan Peyton   { // Execute this task immediately
17117cc577a4SJonathan Peyton     kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
17127cc577a4SJonathan Peyton     new_taskdata->td_flags.task_serial = 1;
17137cc577a4SJonathan Peyton     __kmp_invoke_task(gtid, new_task, current_task);
17147cc577a4SJonathan Peyton   }
17157cc577a4SJonathan Peyton 
17163041982dSJonathan Peyton   KA_TRACE(
17173041982dSJonathan Peyton       10,
17183041982dSJonathan Peyton       ("__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: "
17193041982dSJonathan Peyton        "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
17203041982dSJonathan Peyton        gtid, loc_ref, new_taskdata));
17217cc577a4SJonathan Peyton 
17227cc577a4SJonathan Peyton   ANNOTATE_HAPPENS_BEFORE(new_task);
172382e94a59SJoachim Protze #if OMPT_SUPPORT
1724f0a1c65fSJonas Hahnfeld   if (UNLIKELY(ompt_enabled.enabled)) {
17250e0d6cddSJoachim Protze     parent->ompt_task_info.frame.enter_frame = ompt_data_none;
172682e94a59SJoachim Protze   }
172782e94a59SJoachim Protze #endif
17287cc577a4SJonathan Peyton   return TASK_CURRENT_NOT_QUEUED;
17297cc577a4SJonathan Peyton }
17307cc577a4SJonathan Peyton 
17317cc577a4SJonathan Peyton // __kmp_omp_task: Schedule a non-thread-switchable task for execution
17323041982dSJonathan Peyton //
17337cc577a4SJonathan Peyton // gtid: Global Thread ID of encountering thread
17347cc577a4SJonathan Peyton // new_task:non-thread-switchable task thunk allocated by __kmp_omp_task_alloc()
17353041982dSJonathan Peyton // serialize_immediate: if TRUE then if the task is executed immediately its
17363041982dSJonathan Peyton // execution will be serialized
17373041982dSJonathan Peyton // Returns:
17383041982dSJonathan Peyton //    TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to
17393041982dSJonathan Peyton //    be resumed later.
17403041982dSJonathan Peyton //    TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be
17413041982dSJonathan Peyton //    resumed later.
17423041982dSJonathan Peyton kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task,
17433041982dSJonathan Peyton                          bool serialize_immediate) {
17447cc577a4SJonathan Peyton   kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
17457cc577a4SJonathan Peyton 
17467cc577a4SJonathan Peyton /* Should we execute the new task or queue it? For now, let's just always try to
17477cc577a4SJonathan Peyton    queue it.  If the queue fills up, then we'll execute it.  */
17487cc577a4SJonathan Peyton #if OMP_45_ENABLED
17493041982dSJonathan Peyton   if (new_taskdata->td_flags.proxy == TASK_PROXY ||
17503041982dSJonathan Peyton       __kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED) // if cannot defer
17517cc577a4SJonathan Peyton #else
17527cc577a4SJonathan Peyton   if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED) // if cannot defer
17537cc577a4SJonathan Peyton #endif
17547cc577a4SJonathan Peyton   { // Execute this task immediately
17557cc577a4SJonathan Peyton     kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
17567cc577a4SJonathan Peyton     if (serialize_immediate)
17577cc577a4SJonathan Peyton       new_taskdata->td_flags.task_serial = 1;
17587cc577a4SJonathan Peyton     __kmp_invoke_task(gtid, new_task, current_task);
17597cc577a4SJonathan Peyton   }
17607cc577a4SJonathan Peyton 
17617cc577a4SJonathan Peyton   ANNOTATE_HAPPENS_BEFORE(new_task);
17627cc577a4SJonathan Peyton   return TASK_CURRENT_NOT_QUEUED;
17637cc577a4SJonathan Peyton }
17647cc577a4SJonathan Peyton 
17653041982dSJonathan Peyton // __kmpc_omp_task: Wrapper around __kmp_omp_task to schedule a
17663041982dSJonathan Peyton // non-thread-switchable task from the parent thread only!
17673041982dSJonathan Peyton //
17687cc577a4SJonathan Peyton // loc_ref: location of original task pragma (ignored)
17697cc577a4SJonathan Peyton // gtid: Global Thread ID of encountering thread
17703041982dSJonathan Peyton // new_task: non-thread-switchable task thunk allocated by
17713041982dSJonathan Peyton // __kmp_omp_task_alloc()
17723041982dSJonathan Peyton // Returns:
17733041982dSJonathan Peyton //    TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to
17743041982dSJonathan Peyton //    be resumed later.
17753041982dSJonathan Peyton //    TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be
17763041982dSJonathan Peyton //    resumed later.
17773041982dSJonathan Peyton kmp_int32 __kmpc_omp_task(ident_t *loc_ref, kmp_int32 gtid,
17783041982dSJonathan Peyton                           kmp_task_t *new_task) {
17797cc577a4SJonathan Peyton   kmp_int32 res;
17807cc577a4SJonathan Peyton   KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
17817cc577a4SJonathan Peyton 
178282e94a59SJoachim Protze #if KMP_DEBUG || OMPT_SUPPORT
17837cc577a4SJonathan Peyton   kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
17847cc577a4SJonathan Peyton #endif
17853041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
17863041982dSJonathan Peyton                 new_taskdata));
17877cc577a4SJonathan Peyton 
178882e94a59SJoachim Protze #if OMPT_SUPPORT
178982e94a59SJoachim Protze   kmp_taskdata_t *parent = NULL;
179086ed6aa6SJoachim Protze   if (UNLIKELY(ompt_enabled.enabled)) {
179186ed6aa6SJoachim Protze     if (!new_taskdata->td_flags.started) {
179282e94a59SJoachim Protze       OMPT_STORE_RETURN_ADDRESS(gtid);
179382e94a59SJoachim Protze       parent = new_taskdata->td_parent;
17940e0d6cddSJoachim Protze       if (!parent->ompt_task_info.frame.enter_frame.ptr) {
17950e0d6cddSJoachim Protze         parent->ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
179686ed6aa6SJoachim Protze       }
179782e94a59SJoachim Protze       if (ompt_enabled.ompt_callback_task_create) {
179882e94a59SJoachim Protze         ompt_data_t task_data = ompt_data_none;
179982e94a59SJoachim Protze         ompt_callbacks.ompt_callback(ompt_callback_task_create)(
180082e94a59SJoachim Protze             parent ? &(parent->ompt_task_info.task_data) : &task_data,
180182e94a59SJoachim Protze             parent ? &(parent->ompt_task_info.frame) : NULL,
180282e94a59SJoachim Protze             &(new_taskdata->ompt_task_info.task_data),
180382e94a59SJoachim Protze             ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
180482e94a59SJoachim Protze             OMPT_LOAD_RETURN_ADDRESS(gtid));
180582e94a59SJoachim Protze       }
180686ed6aa6SJoachim Protze     } else {
180786ed6aa6SJoachim Protze       // We are scheduling the continuation of an UNTIED task.
180886ed6aa6SJoachim Protze       // Scheduling back to the parent task.
180986ed6aa6SJoachim Protze       __ompt_task_finish(new_task,
181086ed6aa6SJoachim Protze                          new_taskdata->ompt_task_info.scheduling_parent,
1811489cdb78SJoachim Protze                          ompt_task_switch);
18120e0d6cddSJoachim Protze       new_taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
181386ed6aa6SJoachim Protze     }
181482e94a59SJoachim Protze   }
181582e94a59SJoachim Protze #endif
181682e94a59SJoachim Protze 
18177cc577a4SJonathan Peyton   res = __kmp_omp_task(gtid, new_task, true);
18187cc577a4SJonathan Peyton 
18193041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task(exit): T#%d returning "
18203041982dSJonathan Peyton                 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
18217cc577a4SJonathan Peyton                 gtid, loc_ref, new_taskdata));
182282e94a59SJoachim Protze #if OMPT_SUPPORT
1823f0a1c65fSJonas Hahnfeld   if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
18240e0d6cddSJoachim Protze     parent->ompt_task_info.frame.enter_frame = ompt_data_none;
182582e94a59SJoachim Protze   }
182682e94a59SJoachim Protze #endif
18277cc577a4SJonathan Peyton   return res;
18287cc577a4SJonathan Peyton }
18297cc577a4SJonathan Peyton 
1830cdaefac5SJoachim Protze // __kmp_omp_taskloop_task: Wrapper around __kmp_omp_task to schedule
1831cdaefac5SJoachim Protze // a taskloop task with the correct OMPT return address
1832cdaefac5SJoachim Protze //
1833cdaefac5SJoachim Protze // loc_ref: location of original task pragma (ignored)
1834cdaefac5SJoachim Protze // gtid: Global Thread ID of encountering thread
1835cdaefac5SJoachim Protze // new_task: non-thread-switchable task thunk allocated by
1836cdaefac5SJoachim Protze // __kmp_omp_task_alloc()
1837cdaefac5SJoachim Protze // codeptr_ra: return address for OMPT callback
1838cdaefac5SJoachim Protze // Returns:
1839cdaefac5SJoachim Protze //    TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to
1840cdaefac5SJoachim Protze //    be resumed later.
1841cdaefac5SJoachim Protze //    TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be
1842cdaefac5SJoachim Protze //    resumed later.
1843cdaefac5SJoachim Protze kmp_int32 __kmp_omp_taskloop_task(ident_t *loc_ref, kmp_int32 gtid,
1844cdaefac5SJoachim Protze                                   kmp_task_t *new_task, void *codeptr_ra) {
1845cdaefac5SJoachim Protze   kmp_int32 res;
1846cdaefac5SJoachim Protze   KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1847cdaefac5SJoachim Protze 
1848cdaefac5SJoachim Protze #if KMP_DEBUG || OMPT_SUPPORT
1849cdaefac5SJoachim Protze   kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1850cdaefac5SJoachim Protze #endif
1851cdaefac5SJoachim Protze   KA_TRACE(10, ("__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1852cdaefac5SJoachim Protze                 new_taskdata));
1853cdaefac5SJoachim Protze 
1854cdaefac5SJoachim Protze #if OMPT_SUPPORT
1855cdaefac5SJoachim Protze   kmp_taskdata_t *parent = NULL;
1856cdaefac5SJoachim Protze   if (UNLIKELY(ompt_enabled.enabled && !new_taskdata->td_flags.started)) {
1857cdaefac5SJoachim Protze     parent = new_taskdata->td_parent;
18580e0d6cddSJoachim Protze     if (!parent->ompt_task_info.frame.enter_frame.ptr)
18590e0d6cddSJoachim Protze       parent->ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1860cdaefac5SJoachim Protze     if (ompt_enabled.ompt_callback_task_create) {
1861cdaefac5SJoachim Protze       ompt_data_t task_data = ompt_data_none;
1862cdaefac5SJoachim Protze       ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1863cdaefac5SJoachim Protze           parent ? &(parent->ompt_task_info.task_data) : &task_data,
1864cdaefac5SJoachim Protze           parent ? &(parent->ompt_task_info.frame) : NULL,
1865cdaefac5SJoachim Protze           &(new_taskdata->ompt_task_info.task_data),
1866cdaefac5SJoachim Protze           ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1867cdaefac5SJoachim Protze           codeptr_ra);
1868cdaefac5SJoachim Protze     }
1869cdaefac5SJoachim Protze   }
1870cdaefac5SJoachim Protze #endif
1871cdaefac5SJoachim Protze 
1872cdaefac5SJoachim Protze   res = __kmp_omp_task(gtid, new_task, true);
1873cdaefac5SJoachim Protze 
1874cdaefac5SJoachim Protze   KA_TRACE(10, ("__kmpc_omp_task(exit): T#%d returning "
1875cdaefac5SJoachim Protze                 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1876cdaefac5SJoachim Protze                 gtid, loc_ref, new_taskdata));
1877cdaefac5SJoachim Protze #if OMPT_SUPPORT
1878cdaefac5SJoachim Protze   if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
18790e0d6cddSJoachim Protze     parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1880cdaefac5SJoachim Protze   }
1881cdaefac5SJoachim Protze #endif
1882cdaefac5SJoachim Protze   return res;
1883cdaefac5SJoachim Protze }
1884cdaefac5SJoachim Protze 
188582e94a59SJoachim Protze template <bool ompt>
188682e94a59SJoachim Protze static kmp_int32 __kmpc_omp_taskwait_template(ident_t *loc_ref, kmp_int32 gtid,
188782e94a59SJoachim Protze                                               void *frame_address,
188882e94a59SJoachim Protze                                               void *return_address) {
18897cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata;
18907cc577a4SJonathan Peyton   kmp_info_t *thread;
18917cc577a4SJonathan Peyton   int thread_finished = FALSE;
18927cc577a4SJonathan Peyton   KMP_SET_THREAD_STATE_BLOCK(TASKWAIT);
18937cc577a4SJonathan Peyton 
18947cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref));
18957cc577a4SJonathan Peyton 
18967cc577a4SJonathan Peyton   if (__kmp_tasking_mode != tskm_immediate_exec) {
18977cc577a4SJonathan Peyton     thread = __kmp_threads[gtid];
18987cc577a4SJonathan Peyton     taskdata = thread->th.th_current_task;
18997cc577a4SJonathan Peyton 
190082e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
190182e94a59SJoachim Protze     ompt_data_t *my_task_data;
190282e94a59SJoachim Protze     ompt_data_t *my_parallel_data;
19037cc577a4SJonathan Peyton 
190482e94a59SJoachim Protze     if (ompt) {
190582e94a59SJoachim Protze       my_task_data = &(taskdata->ompt_task_info.task_data);
190682e94a59SJoachim Protze       my_parallel_data = OMPT_CUR_TEAM_DATA(thread);
190782e94a59SJoachim Protze 
19080e0d6cddSJoachim Protze       taskdata->ompt_task_info.frame.enter_frame.ptr = frame_address;
190982e94a59SJoachim Protze 
191082e94a59SJoachim Protze       if (ompt_enabled.ompt_callback_sync_region) {
191182e94a59SJoachim Protze         ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
191282e94a59SJoachim Protze             ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
191382e94a59SJoachim Protze             my_task_data, return_address);
191482e94a59SJoachim Protze       }
191582e94a59SJoachim Protze 
191682e94a59SJoachim Protze       if (ompt_enabled.ompt_callback_sync_region_wait) {
191782e94a59SJoachim Protze         ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
191882e94a59SJoachim Protze             ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
191982e94a59SJoachim Protze             my_task_data, return_address);
19207cc577a4SJonathan Peyton       }
19217cc577a4SJonathan Peyton     }
192282e94a59SJoachim Protze #endif // OMPT_SUPPORT && OMPT_OPTIONAL
19237cc577a4SJonathan Peyton 
19243041982dSJonathan Peyton // Debugger: The taskwait is active. Store location and thread encountered the
19253041982dSJonathan Peyton // taskwait.
19267cc577a4SJonathan Peyton #if USE_ITT_BUILD
19277cc577a4SJonathan Peyton // Note: These values are used by ITT events as well.
19287cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
19297cc577a4SJonathan Peyton     taskdata->td_taskwait_counter += 1;
19307cc577a4SJonathan Peyton     taskdata->td_taskwait_ident = loc_ref;
19317cc577a4SJonathan Peyton     taskdata->td_taskwait_thread = gtid + 1;
19327cc577a4SJonathan Peyton 
19337cc577a4SJonathan Peyton #if USE_ITT_BUILD
19347cc577a4SJonathan Peyton     void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
19357cc577a4SJonathan Peyton     if (itt_sync_obj != NULL)
19367cc577a4SJonathan Peyton       __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
19377cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
19387cc577a4SJonathan Peyton 
19393041982dSJonathan Peyton     bool must_wait =
19403041982dSJonathan Peyton         !taskdata->td_flags.team_serial && !taskdata->td_flags.final;
19417cc577a4SJonathan Peyton 
19427cc577a4SJonathan Peyton #if OMP_45_ENABLED
19433041982dSJonathan Peyton     must_wait = must_wait || (thread->th.th_task_team != NULL &&
19443041982dSJonathan Peyton                               thread->th.th_task_team->tt.tt_found_proxy_tasks);
19457cc577a4SJonathan Peyton #endif
19463041982dSJonathan Peyton     if (must_wait) {
194737e2ef54SJonathan Peyton       kmp_flag_32 flag(RCAST(std::atomic<kmp_uint32> *,
194837e2ef54SJonathan Peyton                              &(taskdata->td_incomplete_child_tasks)),
1949c47afcd9SAndrey Churbanov                        0U);
195037e2ef54SJonathan Peyton       while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) != 0) {
19513041982dSJonathan Peyton         flag.execute_tasks(thread, gtid, FALSE,
19523041982dSJonathan Peyton                            &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
19533041982dSJonathan Peyton                            __kmp_task_stealing_constraint);
19547cc577a4SJonathan Peyton       }
19557cc577a4SJonathan Peyton     }
19567cc577a4SJonathan Peyton #if USE_ITT_BUILD
19577cc577a4SJonathan Peyton     if (itt_sync_obj != NULL)
19587cc577a4SJonathan Peyton       __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
19597cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
19607cc577a4SJonathan Peyton 
19613041982dSJonathan Peyton     // Debugger:  The taskwait is completed. Location remains, but thread is
19623041982dSJonathan Peyton     // negated.
19637cc577a4SJonathan Peyton     taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
19647cc577a4SJonathan Peyton 
196582e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
196682e94a59SJoachim Protze     if (ompt) {
196782e94a59SJoachim Protze       if (ompt_enabled.ompt_callback_sync_region_wait) {
196882e94a59SJoachim Protze         ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
196982e94a59SJoachim Protze             ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
197082e94a59SJoachim Protze             my_task_data, return_address);
197182e94a59SJoachim Protze       }
197282e94a59SJoachim Protze       if (ompt_enabled.ompt_callback_sync_region) {
197382e94a59SJoachim Protze         ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
197482e94a59SJoachim Protze             ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
197582e94a59SJoachim Protze             my_task_data, return_address);
19767cc577a4SJonathan Peyton       }
19770e0d6cddSJoachim Protze       taskdata->ompt_task_info.frame.enter_frame = ompt_data_none;
19787cc577a4SJonathan Peyton     }
197982e94a59SJoachim Protze #endif // OMPT_SUPPORT && OMPT_OPTIONAL
198082e94a59SJoachim Protze 
19817cc577a4SJonathan Peyton     ANNOTATE_HAPPENS_AFTER(taskdata);
19827cc577a4SJonathan Peyton   }
19837cc577a4SJonathan Peyton 
19847cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, "
19853041982dSJonathan Peyton                 "returning TASK_CURRENT_NOT_QUEUED\n",
19863041982dSJonathan Peyton                 gtid, taskdata));
19877cc577a4SJonathan Peyton 
19887cc577a4SJonathan Peyton   return TASK_CURRENT_NOT_QUEUED;
19897cc577a4SJonathan Peyton }
19907cc577a4SJonathan Peyton 
1991489cdb78SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
199282e94a59SJoachim Protze OMPT_NOINLINE
199382e94a59SJoachim Protze static kmp_int32 __kmpc_omp_taskwait_ompt(ident_t *loc_ref, kmp_int32 gtid,
199482e94a59SJoachim Protze                                           void *frame_address,
199582e94a59SJoachim Protze                                           void *return_address) {
199682e94a59SJoachim Protze   return __kmpc_omp_taskwait_template<true>(loc_ref, gtid, frame_address,
199782e94a59SJoachim Protze                                             return_address);
199882e94a59SJoachim Protze }
1999489cdb78SJoachim Protze #endif // OMPT_SUPPORT && OMPT_OPTIONAL
200082e94a59SJoachim Protze 
200182e94a59SJoachim Protze // __kmpc_omp_taskwait: Wait until all tasks generated by the current task are
200282e94a59SJoachim Protze // complete
200382e94a59SJoachim Protze kmp_int32 __kmpc_omp_taskwait(ident_t *loc_ref, kmp_int32 gtid) {
200482e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
200582e94a59SJoachim Protze   if (UNLIKELY(ompt_enabled.enabled)) {
200682e94a59SJoachim Protze     OMPT_STORE_RETURN_ADDRESS(gtid);
20070e0d6cddSJoachim Protze     return __kmpc_omp_taskwait_ompt(loc_ref, gtid, OMPT_GET_FRAME_ADDRESS(0),
200882e94a59SJoachim Protze                                     OMPT_LOAD_RETURN_ADDRESS(gtid));
200982e94a59SJoachim Protze   }
201082e94a59SJoachim Protze #endif
201182e94a59SJoachim Protze   return __kmpc_omp_taskwait_template<false>(loc_ref, gtid, NULL, NULL);
201282e94a59SJoachim Protze }
201382e94a59SJoachim Protze 
20147cc577a4SJonathan Peyton // __kmpc_omp_taskyield: switch to a different task
20153041982dSJonathan Peyton kmp_int32 __kmpc_omp_taskyield(ident_t *loc_ref, kmp_int32 gtid, int end_part) {
20167cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata;
20177cc577a4SJonathan Peyton   kmp_info_t *thread;
20187cc577a4SJonathan Peyton   int thread_finished = FALSE;
20197cc577a4SJonathan Peyton 
20207cc577a4SJonathan Peyton   KMP_COUNT_BLOCK(OMP_TASKYIELD);
20217cc577a4SJonathan Peyton   KMP_SET_THREAD_STATE_BLOCK(TASKYIELD);
20227cc577a4SJonathan Peyton 
20237cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n",
20247cc577a4SJonathan Peyton                 gtid, loc_ref, end_part));
20257cc577a4SJonathan Peyton 
20267cc577a4SJonathan Peyton   if (__kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel) {
20277cc577a4SJonathan Peyton     thread = __kmp_threads[gtid];
20287cc577a4SJonathan Peyton     taskdata = thread->th.th_current_task;
20297cc577a4SJonathan Peyton // Should we model this as a task wait or not?
20303041982dSJonathan Peyton // Debugger: The taskwait is active. Store location and thread encountered the
20313041982dSJonathan Peyton // taskwait.
20327cc577a4SJonathan Peyton #if USE_ITT_BUILD
20337cc577a4SJonathan Peyton // Note: These values are used by ITT events as well.
20347cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
20357cc577a4SJonathan Peyton     taskdata->td_taskwait_counter += 1;
20367cc577a4SJonathan Peyton     taskdata->td_taskwait_ident = loc_ref;
20377cc577a4SJonathan Peyton     taskdata->td_taskwait_thread = gtid + 1;
20387cc577a4SJonathan Peyton 
20397cc577a4SJonathan Peyton #if USE_ITT_BUILD
20407cc577a4SJonathan Peyton     void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
20417cc577a4SJonathan Peyton     if (itt_sync_obj != NULL)
20427cc577a4SJonathan Peyton       __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
20437cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
20447cc577a4SJonathan Peyton     if (!taskdata->td_flags.team_serial) {
20457cc577a4SJonathan Peyton       kmp_task_team_t *task_team = thread->th.th_task_team;
20467cc577a4SJonathan Peyton       if (task_team != NULL) {
20477cc577a4SJonathan Peyton         if (KMP_TASKING_ENABLED(task_team)) {
204882e94a59SJoachim Protze #if OMPT_SUPPORT
2049f0a1c65fSJonas Hahnfeld           if (UNLIKELY(ompt_enabled.enabled))
205082e94a59SJoachim Protze             thread->th.ompt_thread_info.ompt_task_yielded = 1;
205182e94a59SJoachim Protze #endif
20523041982dSJonathan Peyton           __kmp_execute_tasks_32(
20533041982dSJonathan Peyton               thread, gtid, NULL, FALSE,
20543041982dSJonathan Peyton               &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
20553041982dSJonathan Peyton               __kmp_task_stealing_constraint);
205682e94a59SJoachim Protze #if OMPT_SUPPORT
2057f0a1c65fSJonas Hahnfeld           if (UNLIKELY(ompt_enabled.enabled))
205882e94a59SJoachim Protze             thread->th.ompt_thread_info.ompt_task_yielded = 0;
205982e94a59SJoachim Protze #endif
20607cc577a4SJonathan Peyton         }
20617cc577a4SJonathan Peyton       }
20627cc577a4SJonathan Peyton     }
20637cc577a4SJonathan Peyton #if USE_ITT_BUILD
20647cc577a4SJonathan Peyton     if (itt_sync_obj != NULL)
20657cc577a4SJonathan Peyton       __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
20667cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
20677cc577a4SJonathan Peyton 
20683041982dSJonathan Peyton     // Debugger:  The taskwait is completed. Location remains, but thread is
20693041982dSJonathan Peyton     // negated.
20707cc577a4SJonathan Peyton     taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
20717cc577a4SJonathan Peyton   }
20727cc577a4SJonathan Peyton 
20737cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_taskyield(exit): T#%d task %p resuming, "
20743041982dSJonathan Peyton                 "returning TASK_CURRENT_NOT_QUEUED\n",
20753041982dSJonathan Peyton                 gtid, taskdata));
20767cc577a4SJonathan Peyton 
20777cc577a4SJonathan Peyton   return TASK_CURRENT_NOT_QUEUED;
20787cc577a4SJonathan Peyton }
20797cc577a4SJonathan Peyton 
208083e360a4SJonathan Peyton #if OMP_50_ENABLED
208172ba2109SAndrey Churbanov // Task Reduction implementation
2082a8426ac8SJonathan Peyton //
2083a8426ac8SJonathan Peyton // Note: initial implementation didn't take into account the possibility
2084a8426ac8SJonathan Peyton // to specify omp_orig for initializer of the UDR (user defined reduction).
2085a8426ac8SJonathan Peyton // Corrected implementation takes into account the omp_orig object.
2086a8426ac8SJonathan Peyton // Compiler is free to use old implementation if omp_orig is not specified.
208772ba2109SAndrey Churbanov 
2088a8426ac8SJonathan Peyton /*!
2089a8426ac8SJonathan Peyton @ingroup BASIC_TYPES
2090a8426ac8SJonathan Peyton @{
2091a8426ac8SJonathan Peyton */
2092a8426ac8SJonathan Peyton 
2093a8426ac8SJonathan Peyton /*!
2094a8426ac8SJonathan Peyton Flags for special info per task reduction item.
2095a8426ac8SJonathan Peyton */
2096a8426ac8SJonathan Peyton typedef struct kmp_taskred_flags {
2097a8426ac8SJonathan Peyton   /*! 1 - use lazy alloc/init (e.g. big objects, #tasks < #threads) */
2098a8426ac8SJonathan Peyton   unsigned lazy_priv : 1;
209972ba2109SAndrey Churbanov   unsigned reserved31 : 31;
2100a8426ac8SJonathan Peyton } kmp_taskred_flags_t;
210172ba2109SAndrey Churbanov 
2102a8426ac8SJonathan Peyton /*!
2103a8426ac8SJonathan Peyton Internal struct for reduction data item related info set up by compiler.
2104a8426ac8SJonathan Peyton */
210572ba2109SAndrey Churbanov typedef struct kmp_task_red_input {
2106a8426ac8SJonathan Peyton   void *reduce_shar; /**< shared between tasks item to reduce into */
2107a8426ac8SJonathan Peyton   size_t reduce_size; /**< size of data item in bytes */
2108a8426ac8SJonathan Peyton   // three compiler-generated routines (init, fini are optional):
2109a8426ac8SJonathan Peyton   void *reduce_init; /**< data initialization routine (single parameter) */
2110a8426ac8SJonathan Peyton   void *reduce_fini; /**< data finalization routine */
2111a8426ac8SJonathan Peyton   void *reduce_comb; /**< data combiner routine */
2112a8426ac8SJonathan Peyton   kmp_taskred_flags_t flags; /**< flags for additional info from compiler */
211372ba2109SAndrey Churbanov } kmp_task_red_input_t;
211472ba2109SAndrey Churbanov 
211572ba2109SAndrey Churbanov /*!
2116a8426ac8SJonathan Peyton Internal struct for reduction data item related info saved by the library.
211772ba2109SAndrey Churbanov */
2118a8426ac8SJonathan Peyton typedef struct kmp_taskred_data {
2119a8426ac8SJonathan Peyton   void *reduce_shar; /**< shared between tasks item to reduce into */
2120a8426ac8SJonathan Peyton   size_t reduce_size; /**< size of data item */
2121a8426ac8SJonathan Peyton   kmp_taskred_flags_t flags; /**< flags for additional info from compiler */
2122a8426ac8SJonathan Peyton   void *reduce_priv; /**< array of thread specific items */
2123a8426ac8SJonathan Peyton   void *reduce_pend; /**< end of private data for faster comparison op */
2124a8426ac8SJonathan Peyton   // three compiler-generated routines (init, fini are optional):
2125a8426ac8SJonathan Peyton   void *reduce_comb; /**< data combiner routine */
2126a8426ac8SJonathan Peyton   void *reduce_init; /**< data initialization routine (two parameters) */
2127a8426ac8SJonathan Peyton   void *reduce_fini; /**< data finalization routine */
2128a8426ac8SJonathan Peyton   void *reduce_orig; /**< original item (can be used in UDR initializer) */
2129a8426ac8SJonathan Peyton } kmp_taskred_data_t;
2130a8426ac8SJonathan Peyton 
2131a8426ac8SJonathan Peyton /*!
2132a8426ac8SJonathan Peyton Internal struct for reduction data item related info set up by compiler.
2133a8426ac8SJonathan Peyton 
2134a8426ac8SJonathan Peyton New interface: added reduce_orig field to provide omp_orig for UDR initializer.
2135a8426ac8SJonathan Peyton */
2136a8426ac8SJonathan Peyton typedef struct kmp_taskred_input {
2137a8426ac8SJonathan Peyton   void *reduce_shar; /**< shared between tasks item to reduce into */
2138a8426ac8SJonathan Peyton   void *reduce_orig; /**< original reduction item used for initialization */
2139a8426ac8SJonathan Peyton   size_t reduce_size; /**< size of data item */
2140a8426ac8SJonathan Peyton   // three compiler-generated routines (init, fini are optional):
2141a8426ac8SJonathan Peyton   void *reduce_init; /**< data initialization routine (two parameters) */
2142a8426ac8SJonathan Peyton   void *reduce_fini; /**< data finalization routine */
2143a8426ac8SJonathan Peyton   void *reduce_comb; /**< data combiner routine */
2144a8426ac8SJonathan Peyton   kmp_taskred_flags_t flags; /**< flags for additional info from compiler */
2145a8426ac8SJonathan Peyton } kmp_taskred_input_t;
2146a8426ac8SJonathan Peyton /*!
2147a8426ac8SJonathan Peyton @}
2148a8426ac8SJonathan Peyton */
2149a8426ac8SJonathan Peyton 
2150a8426ac8SJonathan Peyton template <typename T> void __kmp_assign_orig(kmp_taskred_data_t &item, T &src);
2151a8426ac8SJonathan Peyton template <>
2152a8426ac8SJonathan Peyton void __kmp_assign_orig<kmp_task_red_input_t>(kmp_taskred_data_t &item,
2153a8426ac8SJonathan Peyton                                              kmp_task_red_input_t &src) {
2154a8426ac8SJonathan Peyton   item.reduce_orig = NULL;
2155a8426ac8SJonathan Peyton }
2156a8426ac8SJonathan Peyton template <>
2157a8426ac8SJonathan Peyton void __kmp_assign_orig<kmp_taskred_input_t>(kmp_taskred_data_t &item,
2158a8426ac8SJonathan Peyton                                             kmp_taskred_input_t &src) {
2159a8426ac8SJonathan Peyton   if (src.reduce_orig != NULL) {
2160a8426ac8SJonathan Peyton     item.reduce_orig = src.reduce_orig;
2161a8426ac8SJonathan Peyton   } else {
2162a8426ac8SJonathan Peyton     item.reduce_orig = src.reduce_shar;
2163a8426ac8SJonathan Peyton   } // non-NULL reduce_orig means new interface used
2164a8426ac8SJonathan Peyton }
2165a8426ac8SJonathan Peyton 
2166a8426ac8SJonathan Peyton template <typename T> void __kmp_call_init(kmp_taskred_data_t &item, int j);
2167a8426ac8SJonathan Peyton template <>
2168a8426ac8SJonathan Peyton void __kmp_call_init<kmp_task_red_input_t>(kmp_taskred_data_t &item,
2169a8426ac8SJonathan Peyton                                            int offset) {
2170a8426ac8SJonathan Peyton   ((void (*)(void *))item.reduce_init)((char *)(item.reduce_priv) + offset);
2171a8426ac8SJonathan Peyton }
2172a8426ac8SJonathan Peyton template <>
2173a8426ac8SJonathan Peyton void __kmp_call_init<kmp_taskred_input_t>(kmp_taskred_data_t &item,
2174a8426ac8SJonathan Peyton                                           int offset) {
2175a8426ac8SJonathan Peyton   ((void (*)(void *, void *))item.reduce_init)(
2176a8426ac8SJonathan Peyton       (char *)(item.reduce_priv) + offset, item.reduce_orig);
2177a8426ac8SJonathan Peyton }
2178a8426ac8SJonathan Peyton 
2179a8426ac8SJonathan Peyton template <typename T>
2180a8426ac8SJonathan Peyton void *__kmp_task_reduction_init(int gtid, int num, T *data) {
218172ba2109SAndrey Churbanov   kmp_info_t *thread = __kmp_threads[gtid];
218272ba2109SAndrey Churbanov   kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup;
218372ba2109SAndrey Churbanov   kmp_int32 nth = thread->th.th_team_nproc;
2184a8426ac8SJonathan Peyton   kmp_taskred_data_t *arr;
218572ba2109SAndrey Churbanov 
218672ba2109SAndrey Churbanov   // check input data just in case
218772ba2109SAndrey Churbanov   KMP_ASSERT(tg != NULL);
218872ba2109SAndrey Churbanov   KMP_ASSERT(data != NULL);
218972ba2109SAndrey Churbanov   KMP_ASSERT(num > 0);
219072ba2109SAndrey Churbanov   if (nth == 1) {
219172ba2109SAndrey Churbanov     KA_TRACE(10, ("__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n",
219272ba2109SAndrey Churbanov                   gtid, tg));
219372ba2109SAndrey Churbanov     return (void *)tg;
219472ba2109SAndrey Churbanov   }
219572ba2109SAndrey Churbanov   KA_TRACE(10, ("__kmpc_task_reduction_init: T#%d, taskgroup %p, #items %d\n",
219672ba2109SAndrey Churbanov                 gtid, tg, num));
2197a8426ac8SJonathan Peyton   arr = (kmp_taskred_data_t *)__kmp_thread_malloc(
2198a8426ac8SJonathan Peyton       thread, num * sizeof(kmp_taskred_data_t));
219972ba2109SAndrey Churbanov   for (int i = 0; i < num; ++i) {
2200a8426ac8SJonathan Peyton     size_t size = data[i].reduce_size - 1;
220172ba2109SAndrey Churbanov     // round the size up to cache line per thread-specific item
220272ba2109SAndrey Churbanov     size += CACHE_LINE - size % CACHE_LINE;
2203a8426ac8SJonathan Peyton     KMP_ASSERT(data[i].reduce_comb != NULL); // combiner is mandatory
2204a8426ac8SJonathan Peyton     arr[i].reduce_shar = data[i].reduce_shar;
220572ba2109SAndrey Churbanov     arr[i].reduce_size = size;
2206a8426ac8SJonathan Peyton     arr[i].flags = data[i].flags;
2207a8426ac8SJonathan Peyton     arr[i].reduce_comb = data[i].reduce_comb;
2208a8426ac8SJonathan Peyton     arr[i].reduce_init = data[i].reduce_init;
2209a8426ac8SJonathan Peyton     arr[i].reduce_fini = data[i].reduce_fini;
2210a8426ac8SJonathan Peyton     __kmp_assign_orig<T>(arr[i], data[i]);
2211a8426ac8SJonathan Peyton     if (!arr[i].flags.lazy_priv) {
221272ba2109SAndrey Churbanov       // allocate cache-line aligned block and fill it with zeros
221372ba2109SAndrey Churbanov       arr[i].reduce_priv = __kmp_allocate(nth * size);
221472ba2109SAndrey Churbanov       arr[i].reduce_pend = (char *)(arr[i].reduce_priv) + nth * size;
2215a8426ac8SJonathan Peyton       if (arr[i].reduce_init != NULL) {
2216a8426ac8SJonathan Peyton         // initialize all thread-specific items
221772ba2109SAndrey Churbanov         for (int j = 0; j < nth; ++j) {
2218a8426ac8SJonathan Peyton           __kmp_call_init<T>(arr[i], j * size);
221972ba2109SAndrey Churbanov         }
222072ba2109SAndrey Churbanov       }
222172ba2109SAndrey Churbanov     } else {
222272ba2109SAndrey Churbanov       // only allocate space for pointers now,
2223a8426ac8SJonathan Peyton       // objects will be lazily allocated/initialized if/when requested
2224a8426ac8SJonathan Peyton       // note that __kmp_allocate zeroes the allocated memory
222572ba2109SAndrey Churbanov       arr[i].reduce_priv = __kmp_allocate(nth * sizeof(void *));
222672ba2109SAndrey Churbanov     }
222772ba2109SAndrey Churbanov   }
222872ba2109SAndrey Churbanov   tg->reduce_data = (void *)arr;
222972ba2109SAndrey Churbanov   tg->reduce_num_data = num;
223072ba2109SAndrey Churbanov   return (void *)tg;
223172ba2109SAndrey Churbanov }
223272ba2109SAndrey Churbanov 
223372ba2109SAndrey Churbanov /*!
223472ba2109SAndrey Churbanov @ingroup TASKING
223572ba2109SAndrey Churbanov @param gtid      Global thread ID
2236a8426ac8SJonathan Peyton @param num       Number of data items to reduce
2237a8426ac8SJonathan Peyton @param data      Array of data for reduction
2238a8426ac8SJonathan Peyton @return The taskgroup identifier
2239a8426ac8SJonathan Peyton 
2240a8426ac8SJonathan Peyton Initialize task reduction for the taskgroup.
2241a8426ac8SJonathan Peyton 
2242a8426ac8SJonathan Peyton Note: this entry supposes the optional compiler-generated initializer routine
2243a8426ac8SJonathan Peyton has single parameter - pointer to object to be initialized. That means
2244a8426ac8SJonathan Peyton the reduction either does not use omp_orig object, or the omp_orig is accessible
2245a8426ac8SJonathan Peyton without help of the runtime library.
2246a8426ac8SJonathan Peyton */
2247a8426ac8SJonathan Peyton void *__kmpc_task_reduction_init(int gtid, int num, void *data) {
2248a8426ac8SJonathan Peyton   return __kmp_task_reduction_init(gtid, num, (kmp_task_red_input_t *)data);
2249a8426ac8SJonathan Peyton }
2250a8426ac8SJonathan Peyton 
2251a8426ac8SJonathan Peyton /*!
2252a8426ac8SJonathan Peyton @ingroup TASKING
2253a8426ac8SJonathan Peyton @param gtid      Global thread ID
2254a8426ac8SJonathan Peyton @param num       Number of data items to reduce
2255a8426ac8SJonathan Peyton @param data      Array of data for reduction
2256a8426ac8SJonathan Peyton @return The taskgroup identifier
2257a8426ac8SJonathan Peyton 
2258a8426ac8SJonathan Peyton Initialize task reduction for the taskgroup.
2259a8426ac8SJonathan Peyton 
2260a8426ac8SJonathan Peyton Note: this entry supposes the optional compiler-generated initializer routine
2261a8426ac8SJonathan Peyton has two parameters, pointer to object to be initialized and pointer to omp_orig
2262a8426ac8SJonathan Peyton */
2263a8426ac8SJonathan Peyton void *__kmpc_taskred_init(int gtid, int num, void *data) {
2264a8426ac8SJonathan Peyton   return __kmp_task_reduction_init(gtid, num, (kmp_taskred_input_t *)data);
2265a8426ac8SJonathan Peyton }
2266a8426ac8SJonathan Peyton 
2267a8426ac8SJonathan Peyton // Copy task reduction data (except for shared pointers).
2268a8426ac8SJonathan Peyton template <typename T>
2269a8426ac8SJonathan Peyton void __kmp_task_reduction_init_copy(kmp_info_t *thr, int num, T *data,
2270a8426ac8SJonathan Peyton                                     kmp_taskgroup_t *tg, void *reduce_data) {
2271a8426ac8SJonathan Peyton   kmp_taskred_data_t *arr;
2272a8426ac8SJonathan Peyton   KA_TRACE(20, ("__kmp_task_reduction_init_copy: Th %p, init taskgroup %p,"
2273a8426ac8SJonathan Peyton                 " from data %p\n",
2274a8426ac8SJonathan Peyton                 thr, tg, reduce_data));
2275a8426ac8SJonathan Peyton   arr = (kmp_taskred_data_t *)__kmp_thread_malloc(
2276a8426ac8SJonathan Peyton       thr, num * sizeof(kmp_taskred_data_t));
2277a8426ac8SJonathan Peyton   // threads will share private copies, thunk routines, sizes, flags, etc.:
2278a8426ac8SJonathan Peyton   KMP_MEMCPY(arr, reduce_data, num * sizeof(kmp_taskred_data_t));
2279a8426ac8SJonathan Peyton   for (int i = 0; i < num; ++i) {
2280a8426ac8SJonathan Peyton     arr[i].reduce_shar = data[i].reduce_shar; // init unique shared pointers
2281a8426ac8SJonathan Peyton   }
2282a8426ac8SJonathan Peyton   tg->reduce_data = (void *)arr;
2283a8426ac8SJonathan Peyton   tg->reduce_num_data = num;
2284a8426ac8SJonathan Peyton }
2285a8426ac8SJonathan Peyton 
2286a8426ac8SJonathan Peyton /*!
2287a8426ac8SJonathan Peyton @ingroup TASKING
2288a8426ac8SJonathan Peyton @param gtid    Global thread ID
228972ba2109SAndrey Churbanov @param tskgrp  The taskgroup ID (optional)
229072ba2109SAndrey Churbanov @param data    Shared location of the item
229172ba2109SAndrey Churbanov @return The pointer to per-thread data
229272ba2109SAndrey Churbanov 
229372ba2109SAndrey Churbanov Get thread-specific location of data item
229472ba2109SAndrey Churbanov */
22953041982dSJonathan Peyton void *__kmpc_task_reduction_get_th_data(int gtid, void *tskgrp, void *data) {
229672ba2109SAndrey Churbanov   kmp_info_t *thread = __kmp_threads[gtid];
229772ba2109SAndrey Churbanov   kmp_int32 nth = thread->th.th_team_nproc;
229872ba2109SAndrey Churbanov   if (nth == 1)
229972ba2109SAndrey Churbanov     return data; // nothing to do
230072ba2109SAndrey Churbanov 
230172ba2109SAndrey Churbanov   kmp_taskgroup_t *tg = (kmp_taskgroup_t *)tskgrp;
230272ba2109SAndrey Churbanov   if (tg == NULL)
230372ba2109SAndrey Churbanov     tg = thread->th.th_current_task->td_taskgroup;
230472ba2109SAndrey Churbanov   KMP_ASSERT(tg != NULL);
2305a8426ac8SJonathan Peyton   kmp_taskred_data_t *arr = (kmp_taskred_data_t *)(tg->reduce_data);
230672ba2109SAndrey Churbanov   kmp_int32 num = tg->reduce_num_data;
230772ba2109SAndrey Churbanov   kmp_int32 tid = thread->th.th_info.ds.ds_tid;
230872ba2109SAndrey Churbanov 
230972ba2109SAndrey Churbanov   KMP_ASSERT(data != NULL);
231072ba2109SAndrey Churbanov   while (tg != NULL) {
231172ba2109SAndrey Churbanov     for (int i = 0; i < num; ++i) {
231272ba2109SAndrey Churbanov       if (!arr[i].flags.lazy_priv) {
231372ba2109SAndrey Churbanov         if (data == arr[i].reduce_shar ||
231472ba2109SAndrey Churbanov             (data >= arr[i].reduce_priv && data < arr[i].reduce_pend))
231572ba2109SAndrey Churbanov           return (char *)(arr[i].reduce_priv) + tid * arr[i].reduce_size;
231672ba2109SAndrey Churbanov       } else {
231772ba2109SAndrey Churbanov         // check shared location first
231872ba2109SAndrey Churbanov         void **p_priv = (void **)(arr[i].reduce_priv);
231972ba2109SAndrey Churbanov         if (data == arr[i].reduce_shar)
232072ba2109SAndrey Churbanov           goto found;
232172ba2109SAndrey Churbanov         // check if we get some thread specific location as parameter
232272ba2109SAndrey Churbanov         for (int j = 0; j < nth; ++j)
232372ba2109SAndrey Churbanov           if (data == p_priv[j])
232472ba2109SAndrey Churbanov             goto found;
232572ba2109SAndrey Churbanov         continue; // not found, continue search
232672ba2109SAndrey Churbanov       found:
232772ba2109SAndrey Churbanov         if (p_priv[tid] == NULL) {
232872ba2109SAndrey Churbanov           // allocate thread specific object lazily
232972ba2109SAndrey Churbanov           p_priv[tid] = __kmp_allocate(arr[i].reduce_size);
2330a8426ac8SJonathan Peyton           if (arr[i].reduce_init != NULL) {
2331a8426ac8SJonathan Peyton             if (arr[i].reduce_orig != NULL) { // new interface
2332a8426ac8SJonathan Peyton               ((void (*)(void *, void *))arr[i].reduce_init)(
2333a8426ac8SJonathan Peyton                   p_priv[tid], arr[i].reduce_orig);
2334a8426ac8SJonathan Peyton             } else { // old interface (single parameter)
2335a8426ac8SJonathan Peyton               ((void (*)(void *))arr[i].reduce_init)(p_priv[tid]);
2336a8426ac8SJonathan Peyton             }
233772ba2109SAndrey Churbanov           }
233872ba2109SAndrey Churbanov         }
233972ba2109SAndrey Churbanov         return p_priv[tid];
234072ba2109SAndrey Churbanov       }
234172ba2109SAndrey Churbanov     }
234272ba2109SAndrey Churbanov     tg = tg->parent;
2343a8426ac8SJonathan Peyton     arr = (kmp_taskred_data_t *)(tg->reduce_data);
234472ba2109SAndrey Churbanov     num = tg->reduce_num_data;
234572ba2109SAndrey Churbanov   }
234672ba2109SAndrey Churbanov   KMP_ASSERT2(0, "Unknown task reduction item");
234772ba2109SAndrey Churbanov   return NULL; // ERROR, this line never executed
234872ba2109SAndrey Churbanov }
234972ba2109SAndrey Churbanov 
235072ba2109SAndrey Churbanov // Finalize task reduction.
235172ba2109SAndrey Churbanov // Called from __kmpc_end_taskgroup()
23523041982dSJonathan Peyton static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg) {
235372ba2109SAndrey Churbanov   kmp_int32 nth = th->th.th_team_nproc;
235472ba2109SAndrey Churbanov   KMP_DEBUG_ASSERT(nth > 1); // should not be called if nth == 1
2355a8426ac8SJonathan Peyton   kmp_taskred_data_t *arr = (kmp_taskred_data_t *)tg->reduce_data;
235672ba2109SAndrey Churbanov   kmp_int32 num = tg->reduce_num_data;
235772ba2109SAndrey Churbanov   for (int i = 0; i < num; ++i) {
235872ba2109SAndrey Churbanov     void *sh_data = arr[i].reduce_shar;
235972ba2109SAndrey Churbanov     void (*f_fini)(void *) = (void (*)(void *))(arr[i].reduce_fini);
23603041982dSJonathan Peyton     void (*f_comb)(void *, void *) =
23613041982dSJonathan Peyton         (void (*)(void *, void *))(arr[i].reduce_comb);
236272ba2109SAndrey Churbanov     if (!arr[i].flags.lazy_priv) {
236372ba2109SAndrey Churbanov       void *pr_data = arr[i].reduce_priv;
236472ba2109SAndrey Churbanov       size_t size = arr[i].reduce_size;
236572ba2109SAndrey Churbanov       for (int j = 0; j < nth; ++j) {
236672ba2109SAndrey Churbanov         void *priv_data = (char *)pr_data + j * size;
236772ba2109SAndrey Churbanov         f_comb(sh_data, priv_data); // combine results
236872ba2109SAndrey Churbanov         if (f_fini)
236972ba2109SAndrey Churbanov           f_fini(priv_data); // finalize if needed
237072ba2109SAndrey Churbanov       }
237172ba2109SAndrey Churbanov     } else {
237272ba2109SAndrey Churbanov       void **pr_data = (void **)(arr[i].reduce_priv);
237372ba2109SAndrey Churbanov       for (int j = 0; j < nth; ++j) {
237472ba2109SAndrey Churbanov         if (pr_data[j] != NULL) {
237572ba2109SAndrey Churbanov           f_comb(sh_data, pr_data[j]); // combine results
237672ba2109SAndrey Churbanov           if (f_fini)
237772ba2109SAndrey Churbanov             f_fini(pr_data[j]); // finalize if needed
237872ba2109SAndrey Churbanov           __kmp_free(pr_data[j]);
237972ba2109SAndrey Churbanov         }
238072ba2109SAndrey Churbanov       }
238172ba2109SAndrey Churbanov     }
238272ba2109SAndrey Churbanov     __kmp_free(arr[i].reduce_priv);
238372ba2109SAndrey Churbanov   }
238472ba2109SAndrey Churbanov   __kmp_thread_free(th, arr);
238572ba2109SAndrey Churbanov   tg->reduce_data = NULL;
238672ba2109SAndrey Churbanov   tg->reduce_num_data = 0;
238772ba2109SAndrey Churbanov }
2388a8426ac8SJonathan Peyton 
2389a8426ac8SJonathan Peyton // Cleanup task reduction data for parallel or worksharing,
2390a8426ac8SJonathan Peyton // do not touch task private data other threads still working with.
2391a8426ac8SJonathan Peyton // Called from __kmpc_end_taskgroup()
2392a8426ac8SJonathan Peyton static void __kmp_task_reduction_clean(kmp_info_t *th, kmp_taskgroup_t *tg) {
2393a8426ac8SJonathan Peyton   __kmp_thread_free(th, tg->reduce_data);
2394a8426ac8SJonathan Peyton   tg->reduce_data = NULL;
2395a8426ac8SJonathan Peyton   tg->reduce_num_data = 0;
2396a8426ac8SJonathan Peyton }
2397a8426ac8SJonathan Peyton 
2398a8426ac8SJonathan Peyton template <typename T>
2399a8426ac8SJonathan Peyton void *__kmp_task_reduction_modifier_init(ident_t *loc, int gtid, int is_ws,
2400a8426ac8SJonathan Peyton                                          int num, T *data) {
2401a8426ac8SJonathan Peyton   kmp_info_t *thr = __kmp_threads[gtid];
2402a8426ac8SJonathan Peyton   kmp_int32 nth = thr->th.th_team_nproc;
2403a8426ac8SJonathan Peyton   __kmpc_taskgroup(loc, gtid); // form new taskgroup first
2404a8426ac8SJonathan Peyton   if (nth == 1) {
2405a8426ac8SJonathan Peyton     KA_TRACE(10,
2406a8426ac8SJonathan Peyton              ("__kmpc_reduction_modifier_init: T#%d, tg %p, exiting nth=1\n",
2407a8426ac8SJonathan Peyton               gtid, thr->th.th_current_task->td_taskgroup));
2408a8426ac8SJonathan Peyton     return (void *)thr->th.th_current_task->td_taskgroup;
2409a8426ac8SJonathan Peyton   }
2410a8426ac8SJonathan Peyton   kmp_team_t *team = thr->th.th_team;
2411a8426ac8SJonathan Peyton   void *reduce_data;
2412a8426ac8SJonathan Peyton   kmp_taskgroup_t *tg;
2413a8426ac8SJonathan Peyton   reduce_data = KMP_ATOMIC_LD_RLX(&team->t.t_tg_reduce_data[is_ws]);
2414a8426ac8SJonathan Peyton   if (reduce_data == NULL &&
2415a8426ac8SJonathan Peyton       __kmp_atomic_compare_store(&team->t.t_tg_reduce_data[is_ws], reduce_data,
2416a8426ac8SJonathan Peyton                                  (void *)1)) {
2417a8426ac8SJonathan Peyton     // single thread enters this block to initialize common reduction data
2418a8426ac8SJonathan Peyton     KMP_DEBUG_ASSERT(reduce_data == NULL);
2419a8426ac8SJonathan Peyton     // first initialize own data, then make a copy other threads can use
2420a8426ac8SJonathan Peyton     tg = (kmp_taskgroup_t *)__kmp_task_reduction_init<T>(gtid, num, data);
2421a8426ac8SJonathan Peyton     reduce_data = __kmp_thread_malloc(thr, num * sizeof(kmp_taskred_data_t));
2422a8426ac8SJonathan Peyton     KMP_MEMCPY(reduce_data, tg->reduce_data, num * sizeof(kmp_taskred_data_t));
2423a8426ac8SJonathan Peyton     // fini counters should be 0 at this point
2424a8426ac8SJonathan Peyton     KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[0]) == 0);
2425a8426ac8SJonathan Peyton     KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[1]) == 0);
2426a8426ac8SJonathan Peyton     KMP_ATOMIC_ST_REL(&team->t.t_tg_reduce_data[is_ws], reduce_data);
2427a8426ac8SJonathan Peyton   } else {
2428a8426ac8SJonathan Peyton     while (
2429a8426ac8SJonathan Peyton         (reduce_data = KMP_ATOMIC_LD_ACQ(&team->t.t_tg_reduce_data[is_ws])) ==
2430a8426ac8SJonathan Peyton         (void *)1) { // wait for task reduction initialization
2431a8426ac8SJonathan Peyton       KMP_CPU_PAUSE();
2432a8426ac8SJonathan Peyton     }
2433a8426ac8SJonathan Peyton     KMP_DEBUG_ASSERT(reduce_data > (void *)1); // should be valid pointer here
2434a8426ac8SJonathan Peyton     tg = thr->th.th_current_task->td_taskgroup;
2435a8426ac8SJonathan Peyton     __kmp_task_reduction_init_copy<T>(thr, num, data, tg, reduce_data);
2436a8426ac8SJonathan Peyton   }
2437a8426ac8SJonathan Peyton   return tg;
2438a8426ac8SJonathan Peyton }
2439a8426ac8SJonathan Peyton 
2440a8426ac8SJonathan Peyton /*!
2441a8426ac8SJonathan Peyton @ingroup TASKING
2442a8426ac8SJonathan Peyton @param loc       Source location info
2443a8426ac8SJonathan Peyton @param gtid      Global thread ID
2444a8426ac8SJonathan Peyton @param is_ws     Is 1 if the reduction is for worksharing, 0 otherwise
2445a8426ac8SJonathan Peyton @param num       Number of data items to reduce
2446a8426ac8SJonathan Peyton @param data      Array of data for reduction
2447a8426ac8SJonathan Peyton @return The taskgroup identifier
2448a8426ac8SJonathan Peyton 
2449a8426ac8SJonathan Peyton Initialize task reduction for a parallel or worksharing.
2450a8426ac8SJonathan Peyton 
2451a8426ac8SJonathan Peyton Note: this entry supposes the optional compiler-generated initializer routine
2452a8426ac8SJonathan Peyton has single parameter - pointer to object to be initialized. That means
2453a8426ac8SJonathan Peyton the reduction either does not use omp_orig object, or the omp_orig is accessible
2454a8426ac8SJonathan Peyton without help of the runtime library.
2455a8426ac8SJonathan Peyton */
2456a8426ac8SJonathan Peyton void *__kmpc_task_reduction_modifier_init(ident_t *loc, int gtid, int is_ws,
2457a8426ac8SJonathan Peyton                                           int num, void *data) {
2458a8426ac8SJonathan Peyton   return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2459a8426ac8SJonathan Peyton                                             (kmp_task_red_input_t *)data);
2460a8426ac8SJonathan Peyton }
2461a8426ac8SJonathan Peyton 
2462a8426ac8SJonathan Peyton /*!
2463a8426ac8SJonathan Peyton @ingroup TASKING
2464a8426ac8SJonathan Peyton @param loc       Source location info
2465a8426ac8SJonathan Peyton @param gtid      Global thread ID
2466a8426ac8SJonathan Peyton @param is_ws     Is 1 if the reduction is for worksharing, 0 otherwise
2467a8426ac8SJonathan Peyton @param num       Number of data items to reduce
2468a8426ac8SJonathan Peyton @param data      Array of data for reduction
2469a8426ac8SJonathan Peyton @return The taskgroup identifier
2470a8426ac8SJonathan Peyton 
2471a8426ac8SJonathan Peyton Initialize task reduction for a parallel or worksharing.
2472a8426ac8SJonathan Peyton 
2473a8426ac8SJonathan Peyton Note: this entry supposes the optional compiler-generated initializer routine
2474a8426ac8SJonathan Peyton has two parameters, pointer to object to be initialized and pointer to omp_orig
2475a8426ac8SJonathan Peyton */
2476a8426ac8SJonathan Peyton void *__kmpc_taskred_modifier_init(ident_t *loc, int gtid, int is_ws, int num,
2477a8426ac8SJonathan Peyton                                    void *data) {
2478a8426ac8SJonathan Peyton   return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2479a8426ac8SJonathan Peyton                                             (kmp_taskred_input_t *)data);
2480a8426ac8SJonathan Peyton }
2481a8426ac8SJonathan Peyton 
2482a8426ac8SJonathan Peyton /*!
2483a8426ac8SJonathan Peyton @ingroup TASKING
2484a8426ac8SJonathan Peyton @param loc       Source location info
2485a8426ac8SJonathan Peyton @param gtid      Global thread ID
2486a8426ac8SJonathan Peyton @param is_ws     Is 1 if the reduction is for worksharing, 0 otherwise
2487a8426ac8SJonathan Peyton 
2488a8426ac8SJonathan Peyton Finalize task reduction for a parallel or worksharing.
2489a8426ac8SJonathan Peyton */
2490a8426ac8SJonathan Peyton void __kmpc_task_reduction_modifier_fini(ident_t *loc, int gtid, int is_ws) {
2491a8426ac8SJonathan Peyton   __kmpc_end_taskgroup(loc, gtid);
2492a8426ac8SJonathan Peyton }
249372ba2109SAndrey Churbanov #endif
24947cc577a4SJonathan Peyton 
24957cc577a4SJonathan Peyton #if OMP_40_ENABLED
24967cc577a4SJonathan Peyton // __kmpc_taskgroup: Start a new taskgroup
24973041982dSJonathan Peyton void __kmpc_taskgroup(ident_t *loc, int gtid) {
24987cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
24997cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = thread->th.th_current_task;
25007cc577a4SJonathan Peyton   kmp_taskgroup_t *tg_new =
25017cc577a4SJonathan Peyton       (kmp_taskgroup_t *)__kmp_thread_malloc(thread, sizeof(kmp_taskgroup_t));
25027cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new));
250337e2ef54SJonathan Peyton   KMP_ATOMIC_ST_RLX(&tg_new->count, 0);
250437e2ef54SJonathan Peyton   KMP_ATOMIC_ST_RLX(&tg_new->cancel_request, cancel_noreq);
25057cc577a4SJonathan Peyton   tg_new->parent = taskdata->td_taskgroup;
250683e360a4SJonathan Peyton #if OMP_50_ENABLED
250772ba2109SAndrey Churbanov   tg_new->reduce_data = NULL;
250872ba2109SAndrey Churbanov   tg_new->reduce_num_data = 0;
250972ba2109SAndrey Churbanov #endif
25107cc577a4SJonathan Peyton   taskdata->td_taskgroup = tg_new;
251182e94a59SJoachim Protze 
251282e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
2513f0a1c65fSJonas Hahnfeld   if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
251482e94a59SJoachim Protze     void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
251582e94a59SJoachim Protze     if (!codeptr)
251682e94a59SJoachim Protze       codeptr = OMPT_GET_RETURN_ADDRESS(0);
251782e94a59SJoachim Protze     kmp_team_t *team = thread->th.th_team;
251882e94a59SJoachim Protze     ompt_data_t my_task_data = taskdata->ompt_task_info.task_data;
251982e94a59SJoachim Protze     // FIXME: I think this is wrong for lwt!
252082e94a59SJoachim Protze     ompt_data_t my_parallel_data = team->t.ompt_team_info.parallel_data;
252182e94a59SJoachim Protze 
252282e94a59SJoachim Protze     ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
252382e94a59SJoachim Protze         ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
252482e94a59SJoachim Protze         &(my_task_data), codeptr);
252582e94a59SJoachim Protze   }
252682e94a59SJoachim Protze #endif
25277cc577a4SJonathan Peyton }
25287cc577a4SJonathan Peyton 
25297cc577a4SJonathan Peyton // __kmpc_end_taskgroup: Wait until all tasks generated by the current task
25307cc577a4SJonathan Peyton //                       and its descendants are complete
25313041982dSJonathan Peyton void __kmpc_end_taskgroup(ident_t *loc, int gtid) {
25327cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
25337cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = thread->th.th_current_task;
25347cc577a4SJonathan Peyton   kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
25357cc577a4SJonathan Peyton   int thread_finished = FALSE;
25367cc577a4SJonathan Peyton 
253782e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
253882e94a59SJoachim Protze   kmp_team_t *team;
253982e94a59SJoachim Protze   ompt_data_t my_task_data;
254082e94a59SJoachim Protze   ompt_data_t my_parallel_data;
254182e94a59SJoachim Protze   void *codeptr;
2542f0a1c65fSJonas Hahnfeld   if (UNLIKELY(ompt_enabled.enabled)) {
254382e94a59SJoachim Protze     team = thread->th.th_team;
254482e94a59SJoachim Protze     my_task_data = taskdata->ompt_task_info.task_data;
254582e94a59SJoachim Protze     // FIXME: I think this is wrong for lwt!
254682e94a59SJoachim Protze     my_parallel_data = team->t.ompt_team_info.parallel_data;
254782e94a59SJoachim Protze     codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
254882e94a59SJoachim Protze     if (!codeptr)
254982e94a59SJoachim Protze       codeptr = OMPT_GET_RETURN_ADDRESS(0);
255082e94a59SJoachim Protze   }
255182e94a59SJoachim Protze #endif
255282e94a59SJoachim Protze 
25537cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc));
25547cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskgroup != NULL);
25557cc577a4SJonathan Peyton   KMP_SET_THREAD_STATE_BLOCK(TASKGROUP);
25567cc577a4SJonathan Peyton 
25577cc577a4SJonathan Peyton   if (__kmp_tasking_mode != tskm_immediate_exec) {
2558a756cb24SAndrey Churbanov     // mark task as waiting not on a barrier
2559a756cb24SAndrey Churbanov     taskdata->td_taskwait_counter += 1;
2560a756cb24SAndrey Churbanov     taskdata->td_taskwait_ident = loc;
2561a756cb24SAndrey Churbanov     taskdata->td_taskwait_thread = gtid + 1;
25627cc577a4SJonathan Peyton #if USE_ITT_BUILD
25633041982dSJonathan Peyton     // For ITT the taskgroup wait is similar to taskwait until we need to
25643041982dSJonathan Peyton     // distinguish them
25657cc577a4SJonathan Peyton     void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
25667cc577a4SJonathan Peyton     if (itt_sync_obj != NULL)
25677cc577a4SJonathan Peyton       __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
25687cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
25697cc577a4SJonathan Peyton 
257082e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
2571f0a1c65fSJonas Hahnfeld     if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
257282e94a59SJoachim Protze       ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
257382e94a59SJoachim Protze           ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
257482e94a59SJoachim Protze           &(my_task_data), codeptr);
257582e94a59SJoachim Protze     }
257682e94a59SJoachim Protze #endif
257782e94a59SJoachim Protze 
25787cc577a4SJonathan Peyton #if OMP_45_ENABLED
25793041982dSJonathan Peyton     if (!taskdata->td_flags.team_serial ||
25803041982dSJonathan Peyton         (thread->th.th_task_team != NULL &&
25813041982dSJonathan Peyton          thread->th.th_task_team->tt.tt_found_proxy_tasks))
25827cc577a4SJonathan Peyton #else
25837cc577a4SJonathan Peyton     if (!taskdata->td_flags.team_serial)
25847cc577a4SJonathan Peyton #endif
25857cc577a4SJonathan Peyton     {
258637e2ef54SJonathan Peyton       kmp_flag_32 flag(RCAST(std::atomic<kmp_uint32> *, &(taskgroup->count)),
258737e2ef54SJonathan Peyton                        0U);
258837e2ef54SJonathan Peyton       while (KMP_ATOMIC_LD_ACQ(&taskgroup->count) != 0) {
25893041982dSJonathan Peyton         flag.execute_tasks(thread, gtid, FALSE,
25903041982dSJonathan Peyton                            &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
25913041982dSJonathan Peyton                            __kmp_task_stealing_constraint);
25927cc577a4SJonathan Peyton       }
25937cc577a4SJonathan Peyton     }
2594a756cb24SAndrey Churbanov     taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread; // end waiting
25957cc577a4SJonathan Peyton 
259682e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
2597f0a1c65fSJonas Hahnfeld     if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
259882e94a59SJoachim Protze       ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
259982e94a59SJoachim Protze           ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
260082e94a59SJoachim Protze           &(my_task_data), codeptr);
260182e94a59SJoachim Protze     }
260282e94a59SJoachim Protze #endif
260382e94a59SJoachim Protze 
26047cc577a4SJonathan Peyton #if USE_ITT_BUILD
26057cc577a4SJonathan Peyton     if (itt_sync_obj != NULL)
26067cc577a4SJonathan Peyton       __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
26077cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
26087cc577a4SJonathan Peyton   }
26097cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskgroup->count == 0);
26107cc577a4SJonathan Peyton 
261183e360a4SJonathan Peyton #if OMP_50_ENABLED
2612a8426ac8SJonathan Peyton   if (taskgroup->reduce_data != NULL) { // need to reduce?
2613a8426ac8SJonathan Peyton     int cnt;
2614a8426ac8SJonathan Peyton     void *reduce_data;
2615a8426ac8SJonathan Peyton     kmp_team_t *t = thread->th.th_team;
2616a8426ac8SJonathan Peyton     kmp_taskred_data_t *arr = (kmp_taskred_data_t *)taskgroup->reduce_data;
2617a8426ac8SJonathan Peyton     // check if <priv> data of the first reduction variable shared for the team
2618a8426ac8SJonathan Peyton     void *priv0 = arr[0].reduce_priv;
2619a8426ac8SJonathan Peyton     if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[0])) != NULL &&
2620a8426ac8SJonathan Peyton         ((kmp_taskred_data_t *)reduce_data)[0].reduce_priv == priv0) {
2621a8426ac8SJonathan Peyton       // finishing task reduction on parallel
2622a8426ac8SJonathan Peyton       cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[0]);
2623a8426ac8SJonathan Peyton       if (cnt == thread->th.th_team_nproc - 1) {
2624a8426ac8SJonathan Peyton         // we are the last thread passing __kmpc_reduction_modifier_fini()
2625a8426ac8SJonathan Peyton         // finalize task reduction:
262672ba2109SAndrey Churbanov         __kmp_task_reduction_fini(thread, taskgroup);
2627a8426ac8SJonathan Peyton         // cleanup fields in the team structure:
2628a8426ac8SJonathan Peyton         // TODO: is relaxed store enough here (whole barrier should follow)?
2629a8426ac8SJonathan Peyton         __kmp_thread_free(thread, reduce_data);
2630a8426ac8SJonathan Peyton         KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[0], NULL);
2631a8426ac8SJonathan Peyton         KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[0], 0);
2632a8426ac8SJonathan Peyton       } else {
2633a8426ac8SJonathan Peyton         // we are not the last thread passing __kmpc_reduction_modifier_fini(),
2634a8426ac8SJonathan Peyton         // so do not finalize reduction, just clean own copy of the data
2635a8426ac8SJonathan Peyton         __kmp_task_reduction_clean(thread, taskgroup);
2636a8426ac8SJonathan Peyton       }
2637a8426ac8SJonathan Peyton     } else if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[1])) !=
2638a8426ac8SJonathan Peyton                    NULL &&
2639a8426ac8SJonathan Peyton                ((kmp_taskred_data_t *)reduce_data)[0].reduce_priv == priv0) {
2640a8426ac8SJonathan Peyton       // finishing task reduction on worksharing
2641a8426ac8SJonathan Peyton       cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[1]);
2642a8426ac8SJonathan Peyton       if (cnt == thread->th.th_team_nproc - 1) {
2643a8426ac8SJonathan Peyton         // we are the last thread passing __kmpc_reduction_modifier_fini()
2644a8426ac8SJonathan Peyton         __kmp_task_reduction_fini(thread, taskgroup);
2645a8426ac8SJonathan Peyton         // cleanup fields in team structure:
2646a8426ac8SJonathan Peyton         // TODO: is relaxed store enough here (whole barrier should follow)?
2647a8426ac8SJonathan Peyton         __kmp_thread_free(thread, reduce_data);
2648a8426ac8SJonathan Peyton         KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[1], NULL);
2649a8426ac8SJonathan Peyton         KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[1], 0);
2650a8426ac8SJonathan Peyton       } else {
2651a8426ac8SJonathan Peyton         // we are not the last thread passing __kmpc_reduction_modifier_fini(),
2652a8426ac8SJonathan Peyton         // so do not finalize reduction, just clean own copy of the data
2653a8426ac8SJonathan Peyton         __kmp_task_reduction_clean(thread, taskgroup);
2654a8426ac8SJonathan Peyton       }
2655a8426ac8SJonathan Peyton     } else {
2656a8426ac8SJonathan Peyton       // finishing task reduction on taskgroup
2657a8426ac8SJonathan Peyton       __kmp_task_reduction_fini(thread, taskgroup);
2658a8426ac8SJonathan Peyton     }
2659a8426ac8SJonathan Peyton   }
266072ba2109SAndrey Churbanov #endif
26617cc577a4SJonathan Peyton   // Restore parent taskgroup for the current task
26627cc577a4SJonathan Peyton   taskdata->td_taskgroup = taskgroup->parent;
26637cc577a4SJonathan Peyton   __kmp_thread_free(thread, taskgroup);
26647cc577a4SJonathan Peyton 
26653041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n",
26663041982dSJonathan Peyton                 gtid, taskdata));
26677cc577a4SJonathan Peyton   ANNOTATE_HAPPENS_AFTER(taskdata);
266882e94a59SJoachim Protze 
266982e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
2670f0a1c65fSJonas Hahnfeld   if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
267182e94a59SJoachim Protze     ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
267282e94a59SJoachim Protze         ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
267382e94a59SJoachim Protze         &(my_task_data), codeptr);
267482e94a59SJoachim Protze   }
267582e94a59SJoachim Protze #endif
26767cc577a4SJonathan Peyton }
26777cc577a4SJonathan Peyton #endif
26787cc577a4SJonathan Peyton 
26797cc577a4SJonathan Peyton // __kmp_remove_my_task: remove a task from my own deque
26803041982dSJonathan Peyton static kmp_task_t *__kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid,
26813041982dSJonathan Peyton                                         kmp_task_team_t *task_team,
26823041982dSJonathan Peyton                                         kmp_int32 is_constrained) {
26837cc577a4SJonathan Peyton   kmp_task_t *task;
26847cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata;
26857cc577a4SJonathan Peyton   kmp_thread_data_t *thread_data;
26867cc577a4SJonathan Peyton   kmp_uint32 tail;
26877cc577a4SJonathan Peyton 
26887cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
26893041982dSJonathan Peyton   KMP_DEBUG_ASSERT(task_team->tt.tt_threads_data !=
26903041982dSJonathan Peyton                    NULL); // Caller should check this condition
26917cc577a4SJonathan Peyton 
26927cc577a4SJonathan Peyton   thread_data = &task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
26937cc577a4SJonathan Peyton 
26947cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmp_remove_my_task(enter): T#%d ntasks=%d head=%u tail=%u\n",
26953041982dSJonathan Peyton                 gtid, thread_data->td.td_deque_ntasks,
26963041982dSJonathan Peyton                 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
26977cc577a4SJonathan Peyton 
26987cc577a4SJonathan Peyton   if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
26993041982dSJonathan Peyton     KA_TRACE(10,
27003041982dSJonathan Peyton              ("__kmp_remove_my_task(exit #1): T#%d No tasks to remove: "
27013041982dSJonathan Peyton               "ntasks=%d head=%u tail=%u\n",
27023041982dSJonathan Peyton               gtid, thread_data->td.td_deque_ntasks,
27033041982dSJonathan Peyton               thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
27047cc577a4SJonathan Peyton     return NULL;
27057cc577a4SJonathan Peyton   }
27067cc577a4SJonathan Peyton 
27077cc577a4SJonathan Peyton   __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
27087cc577a4SJonathan Peyton 
27097cc577a4SJonathan Peyton   if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
27107cc577a4SJonathan Peyton     __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
27113041982dSJonathan Peyton     KA_TRACE(10,
27123041982dSJonathan Peyton              ("__kmp_remove_my_task(exit #2): T#%d No tasks to remove: "
27133041982dSJonathan Peyton               "ntasks=%d head=%u tail=%u\n",
27143041982dSJonathan Peyton               gtid, thread_data->td.td_deque_ntasks,
27153041982dSJonathan Peyton               thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
27167cc577a4SJonathan Peyton     return NULL;
27177cc577a4SJonathan Peyton   }
27187cc577a4SJonathan Peyton 
27193041982dSJonathan Peyton   tail = (thread_data->td.td_deque_tail - 1) &
27203041982dSJonathan Peyton          TASK_DEQUE_MASK(thread_data->td); // Wrap index.
27217cc577a4SJonathan Peyton   taskdata = thread_data->td.td_deque[tail];
27227cc577a4SJonathan Peyton 
2723c3344345SAndrey Churbanov   if (!__kmp_task_is_allowed(gtid, is_constrained, taskdata,
2724c3344345SAndrey Churbanov                              thread->th.th_current_task)) {
2725a756cb24SAndrey Churbanov     // The TSC does not allow to steal victim task
27267cc577a4SJonathan Peyton     __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2727c3344345SAndrey Churbanov     KA_TRACE(10,
2728c3344345SAndrey Churbanov              ("__kmp_remove_my_task(exit #3): T#%d TSC blocks tail task: "
27293041982dSJonathan Peyton               "ntasks=%d head=%u tail=%u\n",
27303041982dSJonathan Peyton               gtid, thread_data->td.td_deque_ntasks,
2731c3344345SAndrey Churbanov               thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
27327cc577a4SJonathan Peyton     return NULL;
27337cc577a4SJonathan Peyton   }
27347cc577a4SJonathan Peyton 
27357cc577a4SJonathan Peyton   thread_data->td.td_deque_tail = tail;
27367cc577a4SJonathan Peyton   TCW_4(thread_data->td.td_deque_ntasks, thread_data->td.td_deque_ntasks - 1);
27377cc577a4SJonathan Peyton 
27387cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
27397cc577a4SJonathan Peyton 
2740c3344345SAndrey Churbanov   KA_TRACE(10, ("__kmp_remove_my_task(exit #4): T#%d task %p removed: "
27413041982dSJonathan Peyton                 "ntasks=%d head=%u tail=%u\n",
27423041982dSJonathan Peyton                 gtid, taskdata, thread_data->td.td_deque_ntasks,
27433041982dSJonathan Peyton                 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
27447cc577a4SJonathan Peyton 
27457cc577a4SJonathan Peyton   task = KMP_TASKDATA_TO_TASK(taskdata);
27467cc577a4SJonathan Peyton   return task;
27477cc577a4SJonathan Peyton }
27487cc577a4SJonathan Peyton 
27497cc577a4SJonathan Peyton // __kmp_steal_task: remove a task from another thread's deque
27507cc577a4SJonathan Peyton // Assume that calling thread has already checked existence of
27517cc577a4SJonathan Peyton // task_team thread_data before calling this routine.
2752a756cb24SAndrey Churbanov static kmp_task_t *__kmp_steal_task(kmp_info_t *victim_thr, kmp_int32 gtid,
2753c47afcd9SAndrey Churbanov                                     kmp_task_team_t *task_team,
275437e2ef54SJonathan Peyton                                     std::atomic<kmp_int32> *unfinished_threads,
2755c47afcd9SAndrey Churbanov                                     int *thread_finished,
2756c47afcd9SAndrey Churbanov                                     kmp_int32 is_constrained) {
27577cc577a4SJonathan Peyton   kmp_task_t *task;
27587cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata;
2759a756cb24SAndrey Churbanov   kmp_taskdata_t *current;
27607cc577a4SJonathan Peyton   kmp_thread_data_t *victim_td, *threads_data;
2761c3344345SAndrey Churbanov   kmp_int32 target;
27627cc577a4SJonathan Peyton   kmp_int32 victim_tid;
27637cc577a4SJonathan Peyton 
27647cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
27657cc577a4SJonathan Peyton 
27667cc577a4SJonathan Peyton   threads_data = task_team->tt.tt_threads_data;
27677cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(threads_data != NULL); // Caller should check this condition
27687cc577a4SJonathan Peyton 
2769a756cb24SAndrey Churbanov   victim_tid = victim_thr->th.th_info.ds.ds_tid;
27707cc577a4SJonathan Peyton   victim_td = &threads_data[victim_tid];
27717cc577a4SJonathan Peyton 
27723041982dSJonathan Peyton   KA_TRACE(10, ("__kmp_steal_task(enter): T#%d try to steal from T#%d: "
2773a756cb24SAndrey Churbanov                 "task_team=%p ntasks=%d head=%u tail=%u\n",
2774a756cb24SAndrey Churbanov                 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
27753041982dSJonathan Peyton                 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
27763041982dSJonathan Peyton                 victim_td->td.td_deque_tail));
27777cc577a4SJonathan Peyton 
2778a756cb24SAndrey Churbanov   if (TCR_4(victim_td->td.td_deque_ntasks) == 0) {
27793041982dSJonathan Peyton     KA_TRACE(10, ("__kmp_steal_task(exit #1): T#%d could not steal from T#%d: "
2780a756cb24SAndrey Churbanov                   "task_team=%p ntasks=%d head=%u tail=%u\n",
2781a756cb24SAndrey Churbanov                   gtid, __kmp_gtid_from_thread(victim_thr), task_team,
27823041982dSJonathan Peyton                   victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
27833041982dSJonathan Peyton                   victim_td->td.td_deque_tail));
27847cc577a4SJonathan Peyton     return NULL;
27857cc577a4SJonathan Peyton   }
27867cc577a4SJonathan Peyton 
27877cc577a4SJonathan Peyton   __kmp_acquire_bootstrap_lock(&victim_td->td.td_deque_lock);
27887cc577a4SJonathan Peyton 
2789a756cb24SAndrey Churbanov   int ntasks = TCR_4(victim_td->td.td_deque_ntasks);
27907cc577a4SJonathan Peyton   // Check again after we acquire the lock
2791a756cb24SAndrey Churbanov   if (ntasks == 0) {
27927cc577a4SJonathan Peyton     __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
27933041982dSJonathan Peyton     KA_TRACE(10, ("__kmp_steal_task(exit #2): T#%d could not steal from T#%d: "
2794a756cb24SAndrey Churbanov                   "task_team=%p ntasks=%d head=%u tail=%u\n",
2795a756cb24SAndrey Churbanov                   gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2796a756cb24SAndrey Churbanov                   victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
27977cc577a4SJonathan Peyton     return NULL;
27987cc577a4SJonathan Peyton   }
27997cc577a4SJonathan Peyton 
28007cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(victim_td->td.td_deque != NULL);
2801c3344345SAndrey Churbanov   current = __kmp_threads[gtid]->th.th_current_task;
28027cc577a4SJonathan Peyton   taskdata = victim_td->td.td_deque[victim_td->td.td_deque_head];
2803c3344345SAndrey Churbanov   if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
2804c3344345SAndrey Churbanov     // Bump head pointer and Wrap.
2805c3344345SAndrey Churbanov     victim_td->td.td_deque_head =
2806c3344345SAndrey Churbanov         (victim_td->td.td_deque_head + 1) & TASK_DEQUE_MASK(victim_td->td);
2807c3344345SAndrey Churbanov   } else {
2808a756cb24SAndrey Churbanov     if (!task_team->tt.tt_untied_task_encountered) {
2809a756cb24SAndrey Churbanov       // The TSC does not allow to steal victim task
28107cc577a4SJonathan Peyton       __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2811c3344345SAndrey Churbanov       KA_TRACE(10, ("__kmp_steal_task(exit #3): T#%d could not steal from "
2812a756cb24SAndrey Churbanov                     "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
2813a756cb24SAndrey Churbanov                     gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
28147cc577a4SJonathan Peyton                     victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
28157cc577a4SJonathan Peyton       return NULL;
28167cc577a4SJonathan Peyton     }
2817a756cb24SAndrey Churbanov     int i;
2818a756cb24SAndrey Churbanov     // walk through victim's deque trying to steal any task
2819a756cb24SAndrey Churbanov     target = victim_td->td.td_deque_head;
2820c3344345SAndrey Churbanov     taskdata = NULL;
2821a756cb24SAndrey Churbanov     for (i = 1; i < ntasks; ++i) {
2822a756cb24SAndrey Churbanov       target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
2823a756cb24SAndrey Churbanov       taskdata = victim_td->td.td_deque[target];
2824c3344345SAndrey Churbanov       if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
2825c3344345SAndrey Churbanov         break; // found victim task
2826c3344345SAndrey Churbanov       } else {
2827a756cb24SAndrey Churbanov         taskdata = NULL;
2828a756cb24SAndrey Churbanov       }
2829a756cb24SAndrey Churbanov     }
2830a756cb24SAndrey Churbanov     if (taskdata == NULL) {
2831a756cb24SAndrey Churbanov       // No appropriate candidate to steal found
2832a756cb24SAndrey Churbanov       __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2833a756cb24SAndrey Churbanov       KA_TRACE(10, ("__kmp_steal_task(exit #4): T#%d could not steal from "
2834a756cb24SAndrey Churbanov                     "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
2835a756cb24SAndrey Churbanov                     gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2836a756cb24SAndrey Churbanov                     victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2837a756cb24SAndrey Churbanov       return NULL;
2838a756cb24SAndrey Churbanov     }
2839a756cb24SAndrey Churbanov     int prev = target;
2840a756cb24SAndrey Churbanov     for (i = i + 1; i < ntasks; ++i) {
2841a756cb24SAndrey Churbanov       // shift remaining tasks in the deque left by 1
2842a756cb24SAndrey Churbanov       target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
2843a756cb24SAndrey Churbanov       victim_td->td.td_deque[prev] = victim_td->td.td_deque[target];
2844a756cb24SAndrey Churbanov       prev = target;
2845a756cb24SAndrey Churbanov     }
2846baad3f60SJonathan Peyton     KMP_DEBUG_ASSERT(
2847baad3f60SJonathan Peyton         victim_td->td.td_deque_tail ==
2848baad3f60SJonathan Peyton         (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(victim_td->td)));
2849a756cb24SAndrey Churbanov     victim_td->td.td_deque_tail = target; // tail -= 1 (wrapped))
2850a756cb24SAndrey Churbanov   }
28517cc577a4SJonathan Peyton   if (*thread_finished) {
28523041982dSJonathan Peyton     // We need to un-mark this victim as a finished victim.  This must be done
28533041982dSJonathan Peyton     // before releasing the lock, or else other threads (starting with the
28543041982dSJonathan Peyton     // master victim) might be prematurely released from the barrier!!!
2855c47afcd9SAndrey Churbanov     kmp_int32 count;
28567cc577a4SJonathan Peyton 
285737e2ef54SJonathan Peyton     count = KMP_ATOMIC_INC(unfinished_threads);
28587cc577a4SJonathan Peyton 
28593041982dSJonathan Peyton     KA_TRACE(
28603041982dSJonathan Peyton         20,
28613041982dSJonathan Peyton         ("__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n",
28627cc577a4SJonathan Peyton          gtid, count + 1, task_team));
28637cc577a4SJonathan Peyton 
28647cc577a4SJonathan Peyton     *thread_finished = FALSE;
28657cc577a4SJonathan Peyton   }
2866a756cb24SAndrey Churbanov   TCW_4(victim_td->td.td_deque_ntasks, ntasks - 1);
28673041982dSJonathan Peyton 
28687cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
28697cc577a4SJonathan Peyton 
28707cc577a4SJonathan Peyton   KMP_COUNT_BLOCK(TASK_stolen);
2871a756cb24SAndrey Churbanov   KA_TRACE(10,
2872a756cb24SAndrey Churbanov            ("__kmp_steal_task(exit #5): T#%d stole task %p from T#%d: "
2873a756cb24SAndrey Churbanov             "task_team=%p ntasks=%d head=%u tail=%u\n",
2874a756cb24SAndrey Churbanov             gtid, taskdata, __kmp_gtid_from_thread(victim_thr), task_team,
2875a756cb24SAndrey Churbanov             ntasks, victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
28767cc577a4SJonathan Peyton 
28777cc577a4SJonathan Peyton   task = KMP_TASKDATA_TO_TASK(taskdata);
28787cc577a4SJonathan Peyton   return task;
28797cc577a4SJonathan Peyton }
28807cc577a4SJonathan Peyton 
28813041982dSJonathan Peyton // __kmp_execute_tasks_template: Choose and execute tasks until either the
28823041982dSJonathan Peyton // condition is statisfied (return true) or there are none left (return false).
28833041982dSJonathan Peyton //
28847cc577a4SJonathan Peyton // final_spin is TRUE if this is the spin at the release barrier.
28857cc577a4SJonathan Peyton // thread_finished indicates whether the thread is finished executing all
28867cc577a4SJonathan Peyton // the tasks it has on its deque, and is at the release barrier.
28877cc577a4SJonathan Peyton // spinner is the location on which to spin.
28887cc577a4SJonathan Peyton // spinner == NULL means only execute a single task and return.
28897cc577a4SJonathan Peyton // checker is the value to check to terminate the spin.
28907cc577a4SJonathan Peyton template <class C>
28913041982dSJonathan Peyton static inline int __kmp_execute_tasks_template(
28923041982dSJonathan Peyton     kmp_info_t *thread, kmp_int32 gtid, C *flag, int final_spin,
28933041982dSJonathan Peyton     int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj),
28943041982dSJonathan Peyton     kmp_int32 is_constrained) {
28957cc577a4SJonathan Peyton   kmp_task_team_t *task_team = thread->th.th_task_team;
28967cc577a4SJonathan Peyton   kmp_thread_data_t *threads_data;
28977cc577a4SJonathan Peyton   kmp_task_t *task;
28987cc577a4SJonathan Peyton   kmp_info_t *other_thread;
28997cc577a4SJonathan Peyton   kmp_taskdata_t *current_task = thread->th.th_current_task;
290037e2ef54SJonathan Peyton   std::atomic<kmp_int32> *unfinished_threads;
2901a756cb24SAndrey Churbanov   kmp_int32 nthreads, victim_tid = -2, use_own_tasks = 1, new_victim = 0,
29023041982dSJonathan Peyton                       tid = thread->th.th_info.ds.ds_tid;
29037cc577a4SJonathan Peyton 
29047cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
29057cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(thread == __kmp_threads[gtid]);
29067cc577a4SJonathan Peyton 
2907a4a9c48cSJonathan Peyton   if (task_team == NULL || current_task == NULL)
29083041982dSJonathan Peyton     return FALSE;
29097cc577a4SJonathan Peyton 
29103041982dSJonathan Peyton   KA_TRACE(15, ("__kmp_execute_tasks_template(enter): T#%d final_spin=%d "
29113041982dSJonathan Peyton                 "*thread_finished=%d\n",
29127cc577a4SJonathan Peyton                 gtid, final_spin, *thread_finished));
29137cc577a4SJonathan Peyton 
2914581490e7SAndrey Churbanov   thread->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
29157cc577a4SJonathan Peyton   threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
29167cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(threads_data != NULL);
29177cc577a4SJonathan Peyton 
29187cc577a4SJonathan Peyton   nthreads = task_team->tt.tt_nproc;
29197cc577a4SJonathan Peyton   unfinished_threads = &(task_team->tt.tt_unfinished_threads);
29207cc577a4SJonathan Peyton #if OMP_45_ENABLED
29217cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(nthreads > 1 || task_team->tt.tt_found_proxy_tasks);
29227cc577a4SJonathan Peyton #else
29237cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(nthreads > 1);
29247cc577a4SJonathan Peyton #endif
292537e2ef54SJonathan Peyton   KMP_DEBUG_ASSERT(*unfinished_threads >= 0);
29267cc577a4SJonathan Peyton 
29273041982dSJonathan Peyton   while (1) { // Outer loop keeps trying to find tasks in case of single thread
29283041982dSJonathan Peyton     // getting tasks from target constructs
29297cc577a4SJonathan Peyton     while (1) { // Inner loop to find a task and execute it
29307cc577a4SJonathan Peyton       task = NULL;
29317cc577a4SJonathan Peyton       if (use_own_tasks) { // check on own queue first
29327cc577a4SJonathan Peyton         task = __kmp_remove_my_task(thread, gtid, task_team, is_constrained);
29337cc577a4SJonathan Peyton       }
29347cc577a4SJonathan Peyton       if ((task == NULL) && (nthreads > 1)) { // Steal a task
29357cc577a4SJonathan Peyton         int asleep = 1;
29367cc577a4SJonathan Peyton         use_own_tasks = 0;
29377cc577a4SJonathan Peyton         // Try to steal from the last place I stole from successfully.
2938a756cb24SAndrey Churbanov         if (victim_tid == -2) { // haven't stolen anything yet
2939a756cb24SAndrey Churbanov           victim_tid = threads_data[tid].td.td_deque_last_stolen;
2940a756cb24SAndrey Churbanov           if (victim_tid !=
29413041982dSJonathan Peyton               -1) // if we have a last stolen from victim, get the thread
2942a756cb24SAndrey Churbanov             other_thread = threads_data[victim_tid].td.td_thr;
29437cc577a4SJonathan Peyton         }
2944a756cb24SAndrey Churbanov         if (victim_tid != -1) { // found last victim
29457cc577a4SJonathan Peyton           asleep = 0;
29463041982dSJonathan Peyton         } else if (!new_victim) { // no recent steals and we haven't already
29473041982dSJonathan Peyton           // used a new victim; select a random thread
29487cc577a4SJonathan Peyton           do { // Find a different thread to steal work from.
29493041982dSJonathan Peyton             // Pick a random thread. Initial plan was to cycle through all the
29503041982dSJonathan Peyton             // threads, and only return if we tried to steal from every thread,
29513041982dSJonathan Peyton             // and failed.  Arch says that's not such a great idea.
2952a756cb24SAndrey Churbanov             victim_tid = __kmp_get_random(thread) % (nthreads - 1);
2953a756cb24SAndrey Churbanov             if (victim_tid >= tid) {
2954a756cb24SAndrey Churbanov               ++victim_tid; // Adjusts random distribution to exclude self
29557cc577a4SJonathan Peyton             }
29567cc577a4SJonathan Peyton             // Found a potential victim
2957a756cb24SAndrey Churbanov             other_thread = threads_data[victim_tid].td.td_thr;
29583041982dSJonathan Peyton             // There is a slight chance that __kmp_enable_tasking() did not wake
29593041982dSJonathan Peyton             // up all threads waiting at the barrier.  If victim is sleeping,
29603041982dSJonathan Peyton             // then wake it up. Since we were going to pay the cache miss
29613041982dSJonathan Peyton             // penalty for referencing another thread's kmp_info_t struct
29623041982dSJonathan Peyton             // anyway,
29633041982dSJonathan Peyton             // the check shouldn't cost too much performance at this point. In
29643041982dSJonathan Peyton             // extra barrier mode, tasks do not sleep at the separate tasking
29653041982dSJonathan Peyton             // barrier, so this isn't a problem.
29667cc577a4SJonathan Peyton             asleep = 0;
29677cc577a4SJonathan Peyton             if ((__kmp_tasking_mode == tskm_task_teams) &&
29687cc577a4SJonathan Peyton                 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) &&
2969c47afcd9SAndrey Churbanov                 (TCR_PTR(CCAST(void *, other_thread->th.th_sleep_loc)) !=
2970c47afcd9SAndrey Churbanov                  NULL)) {
29717cc577a4SJonathan Peyton               asleep = 1;
29723041982dSJonathan Peyton               __kmp_null_resume_wrapper(__kmp_gtid_from_thread(other_thread),
29733041982dSJonathan Peyton                                         other_thread->th.th_sleep_loc);
29743041982dSJonathan Peyton               // A sleeping thread should not have any tasks on it's queue.
29753041982dSJonathan Peyton               // There is a slight possibility that it resumes, steals a task
29763041982dSJonathan Peyton               // from another thread, which spawns more tasks, all in the time
29773041982dSJonathan Peyton               // that it takes this thread to check => don't write an assertion
29783041982dSJonathan Peyton               // that the victim's queue is empty.  Try stealing from a
29793041982dSJonathan Peyton               // different thread.
29807cc577a4SJonathan Peyton             }
29817cc577a4SJonathan Peyton           } while (asleep);
29827cc577a4SJonathan Peyton         }
29837cc577a4SJonathan Peyton 
29847cc577a4SJonathan Peyton         if (!asleep) {
29857cc577a4SJonathan Peyton           // We have a victim to try to steal from
29863041982dSJonathan Peyton           task = __kmp_steal_task(other_thread, gtid, task_team,
29873041982dSJonathan Peyton                                   unfinished_threads, thread_finished,
29883041982dSJonathan Peyton                                   is_constrained);
29897cc577a4SJonathan Peyton         }
29907cc577a4SJonathan Peyton         if (task != NULL) { // set last stolen to victim
2991a756cb24SAndrey Churbanov           if (threads_data[tid].td.td_deque_last_stolen != victim_tid) {
2992a756cb24SAndrey Churbanov             threads_data[tid].td.td_deque_last_stolen = victim_tid;
29933041982dSJonathan Peyton             // The pre-refactored code did not try more than 1 successful new
29943041982dSJonathan Peyton             // vicitm, unless the last one generated more local tasks;
29953041982dSJonathan Peyton             // new_victim keeps track of this
29967cc577a4SJonathan Peyton             new_victim = 1;
29977cc577a4SJonathan Peyton           }
29983041982dSJonathan Peyton         } else { // No tasks found; unset last_stolen
29997cc577a4SJonathan Peyton           KMP_CHECK_UPDATE(threads_data[tid].td.td_deque_last_stolen, -1);
3000a756cb24SAndrey Churbanov           victim_tid = -2; // no successful victim found
30017cc577a4SJonathan Peyton         }
30027cc577a4SJonathan Peyton       }
30037cc577a4SJonathan Peyton 
30047cc577a4SJonathan Peyton       if (task == NULL) // break out of tasking loop
30057cc577a4SJonathan Peyton         break;
30067cc577a4SJonathan Peyton 
30077cc577a4SJonathan Peyton // Found a task; execute it
30087cc577a4SJonathan Peyton #if USE_ITT_BUILD && USE_ITT_NOTIFY
30097cc577a4SJonathan Peyton       if (__itt_sync_create_ptr || KMP_ITT_DEBUG) {
30103041982dSJonathan Peyton         if (itt_sync_obj == NULL) { // we are at fork barrier where we could not
30113041982dSJonathan Peyton           // get the object reliably
30127cc577a4SJonathan Peyton           itt_sync_obj = __kmp_itt_barrier_object(gtid, bs_forkjoin_barrier);
30137cc577a4SJonathan Peyton         }
30147cc577a4SJonathan Peyton         __kmp_itt_task_starting(itt_sync_obj);
30157cc577a4SJonathan Peyton       }
30167cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD && USE_ITT_NOTIFY */
30177cc577a4SJonathan Peyton       __kmp_invoke_task(gtid, task, current_task);
30187cc577a4SJonathan Peyton #if USE_ITT_BUILD
30193041982dSJonathan Peyton       if (itt_sync_obj != NULL)
30203041982dSJonathan Peyton         __kmp_itt_task_finished(itt_sync_obj);
30217cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
30223041982dSJonathan Peyton       // If this thread is only partway through the barrier and the condition is
30233041982dSJonathan Peyton       // met, then return now, so that the barrier gather/release pattern can
30243041982dSJonathan Peyton       // proceed. If this thread is in the last spin loop in the barrier,
30253041982dSJonathan Peyton       // waiting to be released, we know that the termination condition will not
30263041982dSJonathan Peyton       // be satisified, so don't waste any cycles checking it.
30277cc577a4SJonathan Peyton       if (flag == NULL || (!final_spin && flag->done_check())) {
30283041982dSJonathan Peyton         KA_TRACE(
30293041982dSJonathan Peyton             15,
30303041982dSJonathan Peyton             ("__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
30313041982dSJonathan Peyton              gtid));
30327cc577a4SJonathan Peyton         return TRUE;
30337cc577a4SJonathan Peyton       }
30347cc577a4SJonathan Peyton       if (thread->th.th_task_team == NULL) {
30357cc577a4SJonathan Peyton         break;
30367cc577a4SJonathan Peyton       }
3037e47d32f1SJonathan Peyton       KMP_YIELD(__kmp_library == library_throughput); // Yield before next task
30383041982dSJonathan Peyton       // If execution of a stolen task results in more tasks being placed on our
30393041982dSJonathan Peyton       // run queue, reset use_own_tasks
30407cc577a4SJonathan Peyton       if (!use_own_tasks && TCR_4(threads_data[tid].td.td_deque_ntasks) != 0) {
30413041982dSJonathan Peyton         KA_TRACE(20, ("__kmp_execute_tasks_template: T#%d stolen task spawned "
30423041982dSJonathan Peyton                       "other tasks, restart\n",
30433041982dSJonathan Peyton                       gtid));
30447cc577a4SJonathan Peyton         use_own_tasks = 1;
30457cc577a4SJonathan Peyton         new_victim = 0;
30467cc577a4SJonathan Peyton       }
30477cc577a4SJonathan Peyton     }
30487cc577a4SJonathan Peyton 
30493041982dSJonathan Peyton // The task source has been exhausted. If in final spin loop of barrier, check
30503041982dSJonathan Peyton // if termination condition is satisfied.
30517cc577a4SJonathan Peyton #if OMP_45_ENABLED
30523041982dSJonathan Peyton     // The work queue may be empty but there might be proxy tasks still
30533041982dSJonathan Peyton     // executing
305437e2ef54SJonathan Peyton     if (final_spin &&
305537e2ef54SJonathan Peyton         KMP_ATOMIC_LD_ACQ(&current_task->td_incomplete_child_tasks) == 0)
30567cc577a4SJonathan Peyton #else
30577cc577a4SJonathan Peyton     if (final_spin)
30587cc577a4SJonathan Peyton #endif
30597cc577a4SJonathan Peyton     {
30603041982dSJonathan Peyton       // First, decrement the #unfinished threads, if that has not already been
30613041982dSJonathan Peyton       // done.  This decrement might be to the spin location, and result in the
30623041982dSJonathan Peyton       // termination condition being satisfied.
30637cc577a4SJonathan Peyton       if (!*thread_finished) {
3064c47afcd9SAndrey Churbanov         kmp_int32 count;
30657cc577a4SJonathan Peyton 
306637e2ef54SJonathan Peyton         count = KMP_ATOMIC_DEC(unfinished_threads) - 1;
30673041982dSJonathan Peyton         KA_TRACE(20, ("__kmp_execute_tasks_template: T#%d dec "
30683041982dSJonathan Peyton                       "unfinished_threads to %d task_team=%p\n",
30697cc577a4SJonathan Peyton                       gtid, count, task_team));
30707cc577a4SJonathan Peyton         *thread_finished = TRUE;
30717cc577a4SJonathan Peyton       }
30727cc577a4SJonathan Peyton 
30737cc577a4SJonathan Peyton       // It is now unsafe to reference thread->th.th_team !!!
30743041982dSJonathan Peyton       // Decrementing task_team->tt.tt_unfinished_threads can allow the master
30753041982dSJonathan Peyton       // thread to pass through the barrier, where it might reset each thread's
30763041982dSJonathan Peyton       // th.th_team field for the next parallel region. If we can steal more
30773041982dSJonathan Peyton       // work, we know that this has not happened yet.
30787cc577a4SJonathan Peyton       if (flag != NULL && flag->done_check()) {
30793041982dSJonathan Peyton         KA_TRACE(
30803041982dSJonathan Peyton             15,
30813041982dSJonathan Peyton             ("__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
30823041982dSJonathan Peyton              gtid));
30837cc577a4SJonathan Peyton         return TRUE;
30847cc577a4SJonathan Peyton       }
30857cc577a4SJonathan Peyton     }
30867cc577a4SJonathan Peyton 
30873041982dSJonathan Peyton     // If this thread's task team is NULL, master has recognized that there are
30883041982dSJonathan Peyton     // no more tasks; bail out
30897cc577a4SJonathan Peyton     if (thread->th.th_task_team == NULL) {
30903041982dSJonathan Peyton       KA_TRACE(15,
30913041982dSJonathan Peyton                ("__kmp_execute_tasks_template: T#%d no more tasks\n", gtid));
30927cc577a4SJonathan Peyton       return FALSE;
30937cc577a4SJonathan Peyton     }
30947cc577a4SJonathan Peyton 
30957cc577a4SJonathan Peyton #if OMP_45_ENABLED
30963041982dSJonathan Peyton     // We could be getting tasks from target constructs; if this is the only
30973041982dSJonathan Peyton     // thread, keep trying to execute tasks from own queue
30987cc577a4SJonathan Peyton     if (nthreads == 1)
30997cc577a4SJonathan Peyton       use_own_tasks = 1;
31007cc577a4SJonathan Peyton     else
31017cc577a4SJonathan Peyton #endif
31027cc577a4SJonathan Peyton     {
31033041982dSJonathan Peyton       KA_TRACE(15,
31043041982dSJonathan Peyton                ("__kmp_execute_tasks_template: T#%d can't find work\n", gtid));
31057cc577a4SJonathan Peyton       return FALSE;
31067cc577a4SJonathan Peyton     }
31077cc577a4SJonathan Peyton   }
31087cc577a4SJonathan Peyton }
31097cc577a4SJonathan Peyton 
31103041982dSJonathan Peyton int __kmp_execute_tasks_32(
31113041982dSJonathan Peyton     kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32 *flag, int final_spin,
31123041982dSJonathan Peyton     int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj),
31133041982dSJonathan Peyton     kmp_int32 is_constrained) {
31143041982dSJonathan Peyton   return __kmp_execute_tasks_template(
31153041982dSJonathan Peyton       thread, gtid, flag, final_spin,
31163041982dSJonathan Peyton       thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
31177cc577a4SJonathan Peyton }
31187cc577a4SJonathan Peyton 
31193041982dSJonathan Peyton int __kmp_execute_tasks_64(
31203041982dSJonathan Peyton     kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64 *flag, int final_spin,
31213041982dSJonathan Peyton     int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj),
31223041982dSJonathan Peyton     kmp_int32 is_constrained) {
31233041982dSJonathan Peyton   return __kmp_execute_tasks_template(
31243041982dSJonathan Peyton       thread, gtid, flag, final_spin,
31253041982dSJonathan Peyton       thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
31267cc577a4SJonathan Peyton }
31277cc577a4SJonathan Peyton 
31283041982dSJonathan Peyton int __kmp_execute_tasks_oncore(
31293041982dSJonathan Peyton     kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag, int final_spin,
31303041982dSJonathan Peyton     int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj),
31313041982dSJonathan Peyton     kmp_int32 is_constrained) {
31323041982dSJonathan Peyton   return __kmp_execute_tasks_template(
31333041982dSJonathan Peyton       thread, gtid, flag, final_spin,
31343041982dSJonathan Peyton       thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
31357cc577a4SJonathan Peyton }
31367cc577a4SJonathan Peyton 
31377cc577a4SJonathan Peyton // __kmp_enable_tasking: Allocate task team and resume threads sleeping at the
31387cc577a4SJonathan Peyton // next barrier so they can assist in executing enqueued tasks.
31397cc577a4SJonathan Peyton // First thread in allocates the task team atomically.
31403041982dSJonathan Peyton static void __kmp_enable_tasking(kmp_task_team_t *task_team,
31413041982dSJonathan Peyton                                  kmp_info_t *this_thr) {
31427cc577a4SJonathan Peyton   kmp_thread_data_t *threads_data;
31437cc577a4SJonathan Peyton   int nthreads, i, is_init_thread;
31447cc577a4SJonathan Peyton 
31457cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmp_enable_tasking(enter): T#%d\n",
31467cc577a4SJonathan Peyton                 __kmp_gtid_from_thread(this_thr)));
31477cc577a4SJonathan Peyton 
31487cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task_team != NULL);
31497cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(this_thr->th.th_team != NULL);
31507cc577a4SJonathan Peyton 
31517cc577a4SJonathan Peyton   nthreads = task_team->tt.tt_nproc;
31527cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(nthreads > 0);
31537cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(nthreads == this_thr->th.th_team->t.t_nproc);
31547cc577a4SJonathan Peyton 
31557cc577a4SJonathan Peyton   // Allocate or increase the size of threads_data if necessary
31567cc577a4SJonathan Peyton   is_init_thread = __kmp_realloc_task_threads_data(this_thr, task_team);
31577cc577a4SJonathan Peyton 
31587cc577a4SJonathan Peyton   if (!is_init_thread) {
31597cc577a4SJonathan Peyton     // Some other thread already set up the array.
31603041982dSJonathan Peyton     KA_TRACE(
31613041982dSJonathan Peyton         20,
31623041982dSJonathan Peyton         ("__kmp_enable_tasking(exit): T#%d: threads array already set up.\n",
31637cc577a4SJonathan Peyton          __kmp_gtid_from_thread(this_thr)));
31647cc577a4SJonathan Peyton     return;
31657cc577a4SJonathan Peyton   }
31667cc577a4SJonathan Peyton   threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
31677cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(threads_data != NULL);
31687cc577a4SJonathan Peyton 
31699b8bb323SJonathan Peyton   if (__kmp_tasking_mode == tskm_task_teams &&
31703041982dSJonathan Peyton       (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME)) {
31717cc577a4SJonathan Peyton     // Release any threads sleeping at the barrier, so that they can steal
31727cc577a4SJonathan Peyton     // tasks and execute them.  In extra barrier mode, tasks do not sleep
31737cc577a4SJonathan Peyton     // at the separate tasking barrier, so this isn't a problem.
31747cc577a4SJonathan Peyton     for (i = 0; i < nthreads; i++) {
31757cc577a4SJonathan Peyton       volatile void *sleep_loc;
31767cc577a4SJonathan Peyton       kmp_info_t *thread = threads_data[i].td.td_thr;
31777cc577a4SJonathan Peyton 
31787cc577a4SJonathan Peyton       if (i == this_thr->th.th_info.ds.ds_tid) {
31797cc577a4SJonathan Peyton         continue;
31807cc577a4SJonathan Peyton       }
31817cc577a4SJonathan Peyton       // Since we haven't locked the thread's suspend mutex lock at this
31827cc577a4SJonathan Peyton       // point, there is a small window where a thread might be putting
31837cc577a4SJonathan Peyton       // itself to sleep, but hasn't set the th_sleep_loc field yet.
31847cc577a4SJonathan Peyton       // To work around this, __kmp_execute_tasks_template() periodically checks
31853041982dSJonathan Peyton       // see if other threads are sleeping (using the same random mechanism that
31863041982dSJonathan Peyton       // is used for task stealing) and awakens them if they are.
3187c47afcd9SAndrey Churbanov       if ((sleep_loc = TCR_PTR(CCAST(void *, thread->th.th_sleep_loc))) !=
3188c47afcd9SAndrey Churbanov           NULL) {
31897cc577a4SJonathan Peyton         KF_TRACE(50, ("__kmp_enable_tasking: T#%d waking up thread T#%d\n",
31907cc577a4SJonathan Peyton                       __kmp_gtid_from_thread(this_thr),
31917cc577a4SJonathan Peyton                       __kmp_gtid_from_thread(thread)));
31927cc577a4SJonathan Peyton         __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc);
31933041982dSJonathan Peyton       } else {
31947cc577a4SJonathan Peyton         KF_TRACE(50, ("__kmp_enable_tasking: T#%d don't wake up thread T#%d\n",
31957cc577a4SJonathan Peyton                       __kmp_gtid_from_thread(this_thr),
31967cc577a4SJonathan Peyton                       __kmp_gtid_from_thread(thread)));
31977cc577a4SJonathan Peyton       }
31987cc577a4SJonathan Peyton     }
31997cc577a4SJonathan Peyton   }
32007cc577a4SJonathan Peyton 
32017cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmp_enable_tasking(exit): T#%d\n",
32027cc577a4SJonathan Peyton                 __kmp_gtid_from_thread(this_thr)));
32037cc577a4SJonathan Peyton }
32047cc577a4SJonathan Peyton 
32057cc577a4SJonathan Peyton /* // TODO: Check the comment consistency
32067cc577a4SJonathan Peyton  * Utility routines for "task teams".  A task team (kmp_task_t) is kind of
32077cc577a4SJonathan Peyton  * like a shadow of the kmp_team_t data struct, with a different lifetime.
32087cc577a4SJonathan Peyton  * After a child * thread checks into a barrier and calls __kmp_release() from
32097cc577a4SJonathan Peyton  * the particular variant of __kmp_<barrier_kind>_barrier_gather(), it can no
32107cc577a4SJonathan Peyton  * longer assume that the kmp_team_t structure is intact (at any moment, the
32117cc577a4SJonathan Peyton  * master thread may exit the barrier code and free the team data structure,
32127cc577a4SJonathan Peyton  * and return the threads to the thread pool).
32137cc577a4SJonathan Peyton  *
32147cc577a4SJonathan Peyton  * This does not work with the the tasking code, as the thread is still
32157cc577a4SJonathan Peyton  * expected to participate in the execution of any tasks that may have been
32167cc577a4SJonathan Peyton  * spawned my a member of the team, and the thread still needs access to all
32177cc577a4SJonathan Peyton  * to each thread in the team, so that it can steal work from it.
32187cc577a4SJonathan Peyton  *
32197cc577a4SJonathan Peyton  * Enter the existence of the kmp_task_team_t struct.  It employs a reference
32207cc577a4SJonathan Peyton  * counting mechanims, and is allocated by the master thread before calling
32217cc577a4SJonathan Peyton  * __kmp_<barrier_kind>_release, and then is release by the last thread to
32227cc577a4SJonathan Peyton  * exit __kmp_<barrier_kind>_release at the next barrier.  I.e. the lifetimes
32237cc577a4SJonathan Peyton  * of the kmp_task_team_t structs for consecutive barriers can overlap
32247cc577a4SJonathan Peyton  * (and will, unless the master thread is the last thread to exit the barrier
32257cc577a4SJonathan Peyton  * release phase, which is not typical).
32267cc577a4SJonathan Peyton  *
32277cc577a4SJonathan Peyton  * The existence of such a struct is useful outside the context of tasking,
32287cc577a4SJonathan Peyton  * but for now, I'm trying to keep it specific to the OMP_30_ENABLED macro,
32297cc577a4SJonathan Peyton  * so that any performance differences show up when comparing the 2.5 vs. 3.0
32307cc577a4SJonathan Peyton  * libraries.
32317cc577a4SJonathan Peyton  *
32327cc577a4SJonathan Peyton  * We currently use the existence of the threads array as an indicator that
32337cc577a4SJonathan Peyton  * tasks were spawned since the last barrier.  If the structure is to be
32347cc577a4SJonathan Peyton  * useful outside the context of tasking, then this will have to change, but
32357cc577a4SJonathan Peyton  * not settting the field minimizes the performance impact of tasking on
32367cc577a4SJonathan Peyton  * barriers, when no explicit tasks were spawned (pushed, actually).
32377cc577a4SJonathan Peyton  */
32387cc577a4SJonathan Peyton 
32393041982dSJonathan Peyton static kmp_task_team_t *__kmp_free_task_teams =
32403041982dSJonathan Peyton     NULL; // Free list for task_team data structures
32417cc577a4SJonathan Peyton // Lock for task team data structures
3242eaa9e40cSJonathan Peyton kmp_bootstrap_lock_t __kmp_task_team_lock =
32433041982dSJonathan Peyton     KMP_BOOTSTRAP_LOCK_INITIALIZER(__kmp_task_team_lock);
32447cc577a4SJonathan Peyton 
32457cc577a4SJonathan Peyton // __kmp_alloc_task_deque:
32467cc577a4SJonathan Peyton // Allocates a task deque for a particular thread, and initialize the necessary
32477cc577a4SJonathan Peyton // data structures relating to the deque.  This only happens once per thread
32483041982dSJonathan Peyton // per task team since task teams are recycled. No lock is needed during
32493041982dSJonathan Peyton // allocation since each thread allocates its own deque.
32503041982dSJonathan Peyton static void __kmp_alloc_task_deque(kmp_info_t *thread,
32513041982dSJonathan Peyton                                    kmp_thread_data_t *thread_data) {
32527cc577a4SJonathan Peyton   __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
32537cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(thread_data->td.td_deque == NULL);
32547cc577a4SJonathan Peyton 
32557cc577a4SJonathan Peyton   // Initialize last stolen task field to "none"
32567cc577a4SJonathan Peyton   thread_data->td.td_deque_last_stolen = -1;
32577cc577a4SJonathan Peyton 
32587cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == 0);
32597cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(thread_data->td.td_deque_head == 0);
32607cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(thread_data->td.td_deque_tail == 0);
32617cc577a4SJonathan Peyton 
32623041982dSJonathan Peyton   KE_TRACE(
32633041982dSJonathan Peyton       10,
32643041982dSJonathan Peyton       ("__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n",
32657cc577a4SJonathan Peyton        __kmp_gtid_from_thread(thread), INITIAL_TASK_DEQUE_SIZE, thread_data));
32667cc577a4SJonathan Peyton   // Allocate space for task deque, and zero the deque
32677cc577a4SJonathan Peyton   // Cannot use __kmp_thread_calloc() because threads not around for
32687cc577a4SJonathan Peyton   // kmp_reap_task_team( ).
32693041982dSJonathan Peyton   thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
32703041982dSJonathan Peyton       INITIAL_TASK_DEQUE_SIZE * sizeof(kmp_taskdata_t *));
32717cc577a4SJonathan Peyton   thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
32727cc577a4SJonathan Peyton }
32737cc577a4SJonathan Peyton 
32747cc577a4SJonathan Peyton // __kmp_free_task_deque:
32753041982dSJonathan Peyton // Deallocates a task deque for a particular thread. Happens at library
32763041982dSJonathan Peyton // deallocation so don't need to reset all thread data fields.
32773041982dSJonathan Peyton static void __kmp_free_task_deque(kmp_thread_data_t *thread_data) {
32787cc577a4SJonathan Peyton   if (thread_data->td.td_deque != NULL) {
32791b536724SJonathan Peyton     __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
32807cc577a4SJonathan Peyton     TCW_4(thread_data->td.td_deque_ntasks, 0);
32817cc577a4SJonathan Peyton     __kmp_free(thread_data->td.td_deque);
32827cc577a4SJonathan Peyton     thread_data->td.td_deque = NULL;
32837cc577a4SJonathan Peyton     __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
32841b536724SJonathan Peyton   }
32857cc577a4SJonathan Peyton 
32867cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK
32877cc577a4SJonathan Peyton   // GEH: Figure out what to do here for td_susp_tied_tasks
32887cc577a4SJonathan Peyton   if (thread_data->td.td_susp_tied_tasks.ts_entries != TASK_STACK_EMPTY) {
32897cc577a4SJonathan Peyton     __kmp_free_task_stack(__kmp_thread_from_gtid(gtid), thread_data);
32907cc577a4SJonathan Peyton   }
32917cc577a4SJonathan Peyton #endif // BUILD_TIED_TASK_STACK
32927cc577a4SJonathan Peyton }
32937cc577a4SJonathan Peyton 
32947cc577a4SJonathan Peyton // __kmp_realloc_task_threads_data:
32953041982dSJonathan Peyton // Allocates a threads_data array for a task team, either by allocating an
32963041982dSJonathan Peyton // initial array or enlarging an existing array.  Only the first thread to get
32973041982dSJonathan Peyton // the lock allocs or enlarges the array and re-initializes the array eleemnts.
32987cc577a4SJonathan Peyton // That thread returns "TRUE", the rest return "FALSE".
32997cc577a4SJonathan Peyton // Assumes that the new array size is given by task_team -> tt.tt_nproc.
33007cc577a4SJonathan Peyton // The current size is given by task_team -> tt.tt_max_threads.
33013041982dSJonathan Peyton static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
33023041982dSJonathan Peyton                                            kmp_task_team_t *task_team) {
33037cc577a4SJonathan Peyton   kmp_thread_data_t **threads_data_p;
33047cc577a4SJonathan Peyton   kmp_int32 nthreads, maxthreads;
33057cc577a4SJonathan Peyton   int is_init_thread = FALSE;
33067cc577a4SJonathan Peyton 
33077cc577a4SJonathan Peyton   if (TCR_4(task_team->tt.tt_found_tasks)) {
33087cc577a4SJonathan Peyton     // Already reallocated and initialized.
33097cc577a4SJonathan Peyton     return FALSE;
33107cc577a4SJonathan Peyton   }
33117cc577a4SJonathan Peyton 
33127cc577a4SJonathan Peyton   threads_data_p = &task_team->tt.tt_threads_data;
33137cc577a4SJonathan Peyton   nthreads = task_team->tt.tt_nproc;
33147cc577a4SJonathan Peyton   maxthreads = task_team->tt.tt_max_threads;
33157cc577a4SJonathan Peyton 
33163041982dSJonathan Peyton   // All threads must lock when they encounter the first task of the implicit
33173041982dSJonathan Peyton   // task region to make sure threads_data fields are (re)initialized before
33183041982dSJonathan Peyton   // used.
33197cc577a4SJonathan Peyton   __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
33207cc577a4SJonathan Peyton 
33217cc577a4SJonathan Peyton   if (!TCR_4(task_team->tt.tt_found_tasks)) {
33227cc577a4SJonathan Peyton     // first thread to enable tasking
33237cc577a4SJonathan Peyton     kmp_team_t *team = thread->th.th_team;
33247cc577a4SJonathan Peyton     int i;
33257cc577a4SJonathan Peyton 
33267cc577a4SJonathan Peyton     is_init_thread = TRUE;
33277cc577a4SJonathan Peyton     if (maxthreads < nthreads) {
33287cc577a4SJonathan Peyton 
33297cc577a4SJonathan Peyton       if (*threads_data_p != NULL) {
33307cc577a4SJonathan Peyton         kmp_thread_data_t *old_data = *threads_data_p;
33317cc577a4SJonathan Peyton         kmp_thread_data_t *new_data = NULL;
33327cc577a4SJonathan Peyton 
33333041982dSJonathan Peyton         KE_TRACE(
33343041982dSJonathan Peyton             10,
33353041982dSJonathan Peyton             ("__kmp_realloc_task_threads_data: T#%d reallocating "
33367cc577a4SJonathan Peyton              "threads data for task_team %p, new_size = %d, old_size = %d\n",
33373041982dSJonathan Peyton              __kmp_gtid_from_thread(thread), task_team, nthreads, maxthreads));
33387cc577a4SJonathan Peyton         // Reallocate threads_data to have more elements than current array
33397cc577a4SJonathan Peyton         // Cannot use __kmp_thread_realloc() because threads not around for
33407cc577a4SJonathan Peyton         // kmp_reap_task_team( ).  Note all new array entries are initialized
33417cc577a4SJonathan Peyton         // to zero by __kmp_allocate().
33423041982dSJonathan Peyton         new_data = (kmp_thread_data_t *)__kmp_allocate(
33433041982dSJonathan Peyton             nthreads * sizeof(kmp_thread_data_t));
33447cc577a4SJonathan Peyton         // copy old data to new data
33457cc577a4SJonathan Peyton         KMP_MEMCPY_S((void *)new_data, nthreads * sizeof(kmp_thread_data_t),
334671483f2dSAndrey Churbanov                      (void *)old_data, maxthreads * sizeof(kmp_thread_data_t));
33477cc577a4SJonathan Peyton 
33487cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK
33497cc577a4SJonathan Peyton         // GEH: Figure out if this is the right thing to do
33507cc577a4SJonathan Peyton         for (i = maxthreads; i < nthreads; i++) {
33517cc577a4SJonathan Peyton           kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
33527cc577a4SJonathan Peyton           __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
33537cc577a4SJonathan Peyton         }
33547cc577a4SJonathan Peyton #endif // BUILD_TIED_TASK_STACK
33557cc577a4SJonathan Peyton         // Install the new data and free the old data
33567cc577a4SJonathan Peyton         (*threads_data_p) = new_data;
33577cc577a4SJonathan Peyton         __kmp_free(old_data);
33583041982dSJonathan Peyton       } else {
33597cc577a4SJonathan Peyton         KE_TRACE(10, ("__kmp_realloc_task_threads_data: T#%d allocating "
33607cc577a4SJonathan Peyton                       "threads data for task_team %p, size = %d\n",
33617cc577a4SJonathan Peyton                       __kmp_gtid_from_thread(thread), task_team, nthreads));
33627cc577a4SJonathan Peyton         // Make the initial allocate for threads_data array, and zero entries
33637cc577a4SJonathan Peyton         // Cannot use __kmp_thread_calloc() because threads not around for
33647cc577a4SJonathan Peyton         // kmp_reap_task_team( ).
33657cc577a4SJonathan Peyton         ANNOTATE_IGNORE_WRITES_BEGIN();
33663041982dSJonathan Peyton         *threads_data_p = (kmp_thread_data_t *)__kmp_allocate(
33673041982dSJonathan Peyton             nthreads * sizeof(kmp_thread_data_t));
33687cc577a4SJonathan Peyton         ANNOTATE_IGNORE_WRITES_END();
33697cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK
33707cc577a4SJonathan Peyton         // GEH: Figure out if this is the right thing to do
33717cc577a4SJonathan Peyton         for (i = 0; i < nthreads; i++) {
33727cc577a4SJonathan Peyton           kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
33737cc577a4SJonathan Peyton           __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
33747cc577a4SJonathan Peyton         }
33757cc577a4SJonathan Peyton #endif // BUILD_TIED_TASK_STACK
33767cc577a4SJonathan Peyton       }
33777cc577a4SJonathan Peyton       task_team->tt.tt_max_threads = nthreads;
33783041982dSJonathan Peyton     } else {
33797cc577a4SJonathan Peyton       // If array has (more than) enough elements, go ahead and use it
33807cc577a4SJonathan Peyton       KMP_DEBUG_ASSERT(*threads_data_p != NULL);
33817cc577a4SJonathan Peyton     }
33827cc577a4SJonathan Peyton 
33837cc577a4SJonathan Peyton     // initialize threads_data pointers back to thread_info structures
33847cc577a4SJonathan Peyton     for (i = 0; i < nthreads; i++) {
33857cc577a4SJonathan Peyton       kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
33867cc577a4SJonathan Peyton       thread_data->td.td_thr = team->t.t_threads[i];
33877cc577a4SJonathan Peyton 
33887cc577a4SJonathan Peyton       if (thread_data->td.td_deque_last_stolen >= nthreads) {
33897cc577a4SJonathan Peyton         // The last stolen field survives across teams / barrier, and the number
33907cc577a4SJonathan Peyton         // of threads may have changed.  It's possible (likely?) that a new
33913041982dSJonathan Peyton         // parallel region will exhibit the same behavior as previous region.
33927cc577a4SJonathan Peyton         thread_data->td.td_deque_last_stolen = -1;
33937cc577a4SJonathan Peyton       }
33947cc577a4SJonathan Peyton     }
33957cc577a4SJonathan Peyton 
33967cc577a4SJonathan Peyton     KMP_MB();
33977cc577a4SJonathan Peyton     TCW_SYNC_4(task_team->tt.tt_found_tasks, TRUE);
33987cc577a4SJonathan Peyton   }
33997cc577a4SJonathan Peyton 
34007cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
34017cc577a4SJonathan Peyton   return is_init_thread;
34027cc577a4SJonathan Peyton }
34037cc577a4SJonathan Peyton 
34047cc577a4SJonathan Peyton // __kmp_free_task_threads_data:
34057cc577a4SJonathan Peyton // Deallocates a threads_data array for a task team, including any attached
34067cc577a4SJonathan Peyton // tasking deques.  Only occurs at library shutdown.
34073041982dSJonathan Peyton static void __kmp_free_task_threads_data(kmp_task_team_t *task_team) {
34087cc577a4SJonathan Peyton   __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
34097cc577a4SJonathan Peyton   if (task_team->tt.tt_threads_data != NULL) {
34107cc577a4SJonathan Peyton     int i;
34117cc577a4SJonathan Peyton     for (i = 0; i < task_team->tt.tt_max_threads; i++) {
34127cc577a4SJonathan Peyton       __kmp_free_task_deque(&task_team->tt.tt_threads_data[i]);
34137cc577a4SJonathan Peyton     }
34147cc577a4SJonathan Peyton     __kmp_free(task_team->tt.tt_threads_data);
34157cc577a4SJonathan Peyton     task_team->tt.tt_threads_data = NULL;
34167cc577a4SJonathan Peyton   }
34177cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
34187cc577a4SJonathan Peyton }
34197cc577a4SJonathan Peyton 
34207cc577a4SJonathan Peyton // __kmp_allocate_task_team:
34217cc577a4SJonathan Peyton // Allocates a task team associated with a specific team, taking it from
34223041982dSJonathan Peyton // the global task team free list if possible.  Also initializes data
34233041982dSJonathan Peyton // structures.
34243041982dSJonathan Peyton static kmp_task_team_t *__kmp_allocate_task_team(kmp_info_t *thread,
34253041982dSJonathan Peyton                                                  kmp_team_t *team) {
34267cc577a4SJonathan Peyton   kmp_task_team_t *task_team = NULL;
34277cc577a4SJonathan Peyton   int nthreads;
34287cc577a4SJonathan Peyton 
34297cc577a4SJonathan Peyton   KA_TRACE(20, ("__kmp_allocate_task_team: T#%d entering; team = %p\n",
34307cc577a4SJonathan Peyton                 (thread ? __kmp_gtid_from_thread(thread) : -1), team));
34317cc577a4SJonathan Peyton 
34327cc577a4SJonathan Peyton   if (TCR_PTR(__kmp_free_task_teams) != NULL) {
34337cc577a4SJonathan Peyton     // Take a task team from the task team pool
34347cc577a4SJonathan Peyton     __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
34357cc577a4SJonathan Peyton     if (__kmp_free_task_teams != NULL) {
34367cc577a4SJonathan Peyton       task_team = __kmp_free_task_teams;
34377cc577a4SJonathan Peyton       TCW_PTR(__kmp_free_task_teams, task_team->tt.tt_next);
34387cc577a4SJonathan Peyton       task_team->tt.tt_next = NULL;
34397cc577a4SJonathan Peyton     }
34407cc577a4SJonathan Peyton     __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
34417cc577a4SJonathan Peyton   }
34427cc577a4SJonathan Peyton 
34437cc577a4SJonathan Peyton   if (task_team == NULL) {
34447cc577a4SJonathan Peyton     KE_TRACE(10, ("__kmp_allocate_task_team: T#%d allocating "
34457cc577a4SJonathan Peyton                   "task team for team %p\n",
34467cc577a4SJonathan Peyton                   __kmp_gtid_from_thread(thread), team));
34477cc577a4SJonathan Peyton     // Allocate a new task team if one is not available.
34487cc577a4SJonathan Peyton     // Cannot use __kmp_thread_malloc() because threads not around for
34497cc577a4SJonathan Peyton     // kmp_reap_task_team( ).
34507cc577a4SJonathan Peyton     task_team = (kmp_task_team_t *)__kmp_allocate(sizeof(kmp_task_team_t));
34517cc577a4SJonathan Peyton     __kmp_init_bootstrap_lock(&task_team->tt.tt_threads_lock);
34523041982dSJonathan Peyton     // AC: __kmp_allocate zeroes returned memory
34533041982dSJonathan Peyton     // task_team -> tt.tt_threads_data = NULL;
34547cc577a4SJonathan Peyton     // task_team -> tt.tt_max_threads = 0;
34557cc577a4SJonathan Peyton     // task_team -> tt.tt_next = NULL;
34567cc577a4SJonathan Peyton   }
34577cc577a4SJonathan Peyton 
34587cc577a4SJonathan Peyton   TCW_4(task_team->tt.tt_found_tasks, FALSE);
34597cc577a4SJonathan Peyton #if OMP_45_ENABLED
34607cc577a4SJonathan Peyton   TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
34617cc577a4SJonathan Peyton #endif
34627cc577a4SJonathan Peyton   task_team->tt.tt_nproc = nthreads = team->t.t_nproc;
34637cc577a4SJonathan Peyton 
346437e2ef54SJonathan Peyton   KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads, nthreads);
34657cc577a4SJonathan Peyton   TCW_4(task_team->tt.tt_active, TRUE);
34667cc577a4SJonathan Peyton 
34673041982dSJonathan Peyton   KA_TRACE(20, ("__kmp_allocate_task_team: T#%d exiting; task_team = %p "
34683041982dSJonathan Peyton                 "unfinished_threads init'd to %d\n",
34693041982dSJonathan Peyton                 (thread ? __kmp_gtid_from_thread(thread) : -1), task_team,
347037e2ef54SJonathan Peyton                 KMP_ATOMIC_LD_RLX(&task_team->tt.tt_unfinished_threads)));
34717cc577a4SJonathan Peyton   return task_team;
34727cc577a4SJonathan Peyton }
34737cc577a4SJonathan Peyton 
34747cc577a4SJonathan Peyton // __kmp_free_task_team:
34757cc577a4SJonathan Peyton // Frees the task team associated with a specific thread, and adds it
34767cc577a4SJonathan Peyton // to the global task team free list.
34773041982dSJonathan Peyton void __kmp_free_task_team(kmp_info_t *thread, kmp_task_team_t *task_team) {
34787cc577a4SJonathan Peyton   KA_TRACE(20, ("__kmp_free_task_team: T#%d task_team = %p\n",
34797cc577a4SJonathan Peyton                 thread ? __kmp_gtid_from_thread(thread) : -1, task_team));
34807cc577a4SJonathan Peyton 
34817cc577a4SJonathan Peyton   // Put task team back on free list
34827cc577a4SJonathan Peyton   __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
34837cc577a4SJonathan Peyton 
34847cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task_team->tt.tt_next == NULL);
34857cc577a4SJonathan Peyton   task_team->tt.tt_next = __kmp_free_task_teams;
34867cc577a4SJonathan Peyton   TCW_PTR(__kmp_free_task_teams, task_team);
34877cc577a4SJonathan Peyton 
34887cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
34897cc577a4SJonathan Peyton }
34907cc577a4SJonathan Peyton 
34917cc577a4SJonathan Peyton // __kmp_reap_task_teams:
34927cc577a4SJonathan Peyton // Free all the task teams on the task team free list.
34937cc577a4SJonathan Peyton // Should only be done during library shutdown.
34943041982dSJonathan Peyton // Cannot do anything that needs a thread structure or gtid since they are
34953041982dSJonathan Peyton // already gone.
34963041982dSJonathan Peyton void __kmp_reap_task_teams(void) {
34977cc577a4SJonathan Peyton   kmp_task_team_t *task_team;
34987cc577a4SJonathan Peyton 
34997cc577a4SJonathan Peyton   if (TCR_PTR(__kmp_free_task_teams) != NULL) {
35007cc577a4SJonathan Peyton     // Free all task_teams on the free list
35017cc577a4SJonathan Peyton     __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
35027cc577a4SJonathan Peyton     while ((task_team = __kmp_free_task_teams) != NULL) {
35037cc577a4SJonathan Peyton       __kmp_free_task_teams = task_team->tt.tt_next;
35047cc577a4SJonathan Peyton       task_team->tt.tt_next = NULL;
35057cc577a4SJonathan Peyton 
35067cc577a4SJonathan Peyton       // Free threads_data if necessary
35077cc577a4SJonathan Peyton       if (task_team->tt.tt_threads_data != NULL) {
35087cc577a4SJonathan Peyton         __kmp_free_task_threads_data(task_team);
35097cc577a4SJonathan Peyton       }
35107cc577a4SJonathan Peyton       __kmp_free(task_team);
35117cc577a4SJonathan Peyton     }
35127cc577a4SJonathan Peyton     __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
35137cc577a4SJonathan Peyton   }
35147cc577a4SJonathan Peyton }
35157cc577a4SJonathan Peyton 
35167cc577a4SJonathan Peyton // __kmp_wait_to_unref_task_teams:
35177cc577a4SJonathan Peyton // Some threads could still be in the fork barrier release code, possibly
35187cc577a4SJonathan Peyton // trying to steal tasks.  Wait for each thread to unreference its task team.
35193041982dSJonathan Peyton void __kmp_wait_to_unref_task_teams(void) {
35207cc577a4SJonathan Peyton   kmp_info_t *thread;
35217cc577a4SJonathan Peyton   kmp_uint32 spins;
35227cc577a4SJonathan Peyton   int done;
35237cc577a4SJonathan Peyton 
35247cc577a4SJonathan Peyton   KMP_INIT_YIELD(spins);
35257cc577a4SJonathan Peyton 
35267cc577a4SJonathan Peyton   for (;;) {
35277cc577a4SJonathan Peyton     done = TRUE;
35287cc577a4SJonathan Peyton 
35297cc577a4SJonathan Peyton     // TODO: GEH - this may be is wrong because some sync would be necessary
35303041982dSJonathan Peyton     // in case threads are added to the pool during the traversal. Need to
35313041982dSJonathan Peyton     // verify that lock for thread pool is held when calling this routine.
3532c47afcd9SAndrey Churbanov     for (thread = CCAST(kmp_info_t *, __kmp_thread_pool); thread != NULL;
35333041982dSJonathan Peyton          thread = thread->th.th_next_pool) {
35347cc577a4SJonathan Peyton #if KMP_OS_WINDOWS
35357cc577a4SJonathan Peyton       DWORD exit_val;
35367cc577a4SJonathan Peyton #endif
35377cc577a4SJonathan Peyton       if (TCR_PTR(thread->th.th_task_team) == NULL) {
35387cc577a4SJonathan Peyton         KA_TRACE(10, ("__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n",
35397cc577a4SJonathan Peyton                       __kmp_gtid_from_thread(thread)));
35407cc577a4SJonathan Peyton         continue;
35417cc577a4SJonathan Peyton       }
35427cc577a4SJonathan Peyton #if KMP_OS_WINDOWS
35437cc577a4SJonathan Peyton       // TODO: GEH - add this check for Linux* OS / OS X* as well?
35447cc577a4SJonathan Peyton       if (!__kmp_is_thread_alive(thread, &exit_val)) {
35457cc577a4SJonathan Peyton         thread->th.th_task_team = NULL;
35467cc577a4SJonathan Peyton         continue;
35477cc577a4SJonathan Peyton       }
35487cc577a4SJonathan Peyton #endif
35497cc577a4SJonathan Peyton 
35507cc577a4SJonathan Peyton       done = FALSE; // Because th_task_team pointer is not NULL for this thread
35517cc577a4SJonathan Peyton 
35523041982dSJonathan Peyton       KA_TRACE(10, ("__kmp_wait_to_unref_task_team: Waiting for T#%d to "
35533041982dSJonathan Peyton                     "unreference task_team\n",
35547cc577a4SJonathan Peyton                     __kmp_gtid_from_thread(thread)));
35557cc577a4SJonathan Peyton 
35567cc577a4SJonathan Peyton       if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
35577cc577a4SJonathan Peyton         volatile void *sleep_loc;
35587cc577a4SJonathan Peyton         // If the thread is sleeping, awaken it.
3559c47afcd9SAndrey Churbanov         if ((sleep_loc = TCR_PTR(CCAST(void *, thread->th.th_sleep_loc))) !=
3560c47afcd9SAndrey Churbanov             NULL) {
35613041982dSJonathan Peyton           KA_TRACE(
35623041982dSJonathan Peyton               10,
35633041982dSJonathan Peyton               ("__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n",
35647cc577a4SJonathan Peyton                __kmp_gtid_from_thread(thread), __kmp_gtid_from_thread(thread)));
35657cc577a4SJonathan Peyton           __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc);
35667cc577a4SJonathan Peyton         }
35677cc577a4SJonathan Peyton       }
35687cc577a4SJonathan Peyton     }
35697cc577a4SJonathan Peyton     if (done) {
35707cc577a4SJonathan Peyton       break;
35717cc577a4SJonathan Peyton     }
35727cc577a4SJonathan Peyton 
3573e47d32f1SJonathan Peyton     // If oversubscribed or have waited a bit, yield.
3574e47d32f1SJonathan Peyton     KMP_YIELD_OVERSUB_ELSE_SPIN(spins);
35757cc577a4SJonathan Peyton   }
35767cc577a4SJonathan Peyton }
35777cc577a4SJonathan Peyton 
35787cc577a4SJonathan Peyton // __kmp_task_team_setup:  Create a task_team for the current team, but use
35797cc577a4SJonathan Peyton // an already created, unused one if it already exists.
35803041982dSJonathan Peyton void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team, int always) {
35817cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
35827cc577a4SJonathan Peyton 
35833041982dSJonathan Peyton   // If this task_team hasn't been created yet, allocate it. It will be used in
35843041982dSJonathan Peyton   // the region after the next.
35853041982dSJonathan Peyton   // If it exists, it is the current task team and shouldn't be touched yet as
35863041982dSJonathan Peyton   // it may still be in use.
35873041982dSJonathan Peyton   if (team->t.t_task_team[this_thr->th.th_task_state] == NULL &&
35883041982dSJonathan Peyton       (always || team->t.t_nproc > 1)) {
35893041982dSJonathan Peyton     team->t.t_task_team[this_thr->th.th_task_state] =
35903041982dSJonathan Peyton         __kmp_allocate_task_team(this_thr, team);
35913041982dSJonathan Peyton     KA_TRACE(20, ("__kmp_task_team_setup: Master T#%d created new task_team %p "
35923041982dSJonathan Peyton                   "for team %d at parity=%d\n",
35933041982dSJonathan Peyton                   __kmp_gtid_from_thread(this_thr),
35943041982dSJonathan Peyton                   team->t.t_task_team[this_thr->th.th_task_state],
35953041982dSJonathan Peyton                   ((team != NULL) ? team->t.t_id : -1),
35963041982dSJonathan Peyton                   this_thr->th.th_task_state));
35977cc577a4SJonathan Peyton   }
35987cc577a4SJonathan Peyton 
35993041982dSJonathan Peyton   // After threads exit the release, they will call sync, and then point to this
36003041982dSJonathan Peyton   // other task_team; make sure it is allocated and properly initialized. As
36013041982dSJonathan Peyton   // threads spin in the barrier release phase, they will continue to use the
36023041982dSJonathan Peyton   // previous task_team struct(above), until they receive the signal to stop
36033041982dSJonathan Peyton   // checking for tasks (they can't safely reference the kmp_team_t struct,
36043041982dSJonathan Peyton   // which could be reallocated by the master thread). No task teams are formed
36053041982dSJonathan Peyton   // for serialized teams.
36067cc577a4SJonathan Peyton   if (team->t.t_nproc > 1) {
36077cc577a4SJonathan Peyton     int other_team = 1 - this_thr->th.th_task_state;
36087cc577a4SJonathan Peyton     if (team->t.t_task_team[other_team] == NULL) { // setup other team as well
36093041982dSJonathan Peyton       team->t.t_task_team[other_team] =
36103041982dSJonathan Peyton           __kmp_allocate_task_team(this_thr, team);
36113041982dSJonathan Peyton       KA_TRACE(20, ("__kmp_task_team_setup: Master T#%d created second new "
36123041982dSJonathan Peyton                     "task_team %p for team %d at parity=%d\n",
36133041982dSJonathan Peyton                     __kmp_gtid_from_thread(this_thr),
36143041982dSJonathan Peyton                     team->t.t_task_team[other_team],
36157cc577a4SJonathan Peyton                     ((team != NULL) ? team->t.t_id : -1), other_team));
36163041982dSJonathan Peyton     } else { // Leave the old task team struct in place for the upcoming region;
36173041982dSJonathan Peyton       // adjust as needed
36187cc577a4SJonathan Peyton       kmp_task_team_t *task_team = team->t.t_task_team[other_team];
36193041982dSJonathan Peyton       if (!task_team->tt.tt_active ||
36203041982dSJonathan Peyton           team->t.t_nproc != task_team->tt.tt_nproc) {
36217cc577a4SJonathan Peyton         TCW_4(task_team->tt.tt_nproc, team->t.t_nproc);
36227cc577a4SJonathan Peyton         TCW_4(task_team->tt.tt_found_tasks, FALSE);
36237cc577a4SJonathan Peyton #if OMP_45_ENABLED
36247cc577a4SJonathan Peyton         TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
36257cc577a4SJonathan Peyton #endif
362637e2ef54SJonathan Peyton         KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads,
362737e2ef54SJonathan Peyton                           team->t.t_nproc);
36287cc577a4SJonathan Peyton         TCW_4(task_team->tt.tt_active, TRUE);
36297cc577a4SJonathan Peyton       }
36303041982dSJonathan Peyton       // if team size has changed, the first thread to enable tasking will
36313041982dSJonathan Peyton       // realloc threads_data if necessary
36323041982dSJonathan Peyton       KA_TRACE(20, ("__kmp_task_team_setup: Master T#%d reset next task_team "
36333041982dSJonathan Peyton                     "%p for team %d at parity=%d\n",
36343041982dSJonathan Peyton                     __kmp_gtid_from_thread(this_thr),
36353041982dSJonathan Peyton                     team->t.t_task_team[other_team],
36367cc577a4SJonathan Peyton                     ((team != NULL) ? team->t.t_id : -1), other_team));
36377cc577a4SJonathan Peyton     }
36387cc577a4SJonathan Peyton   }
36397cc577a4SJonathan Peyton }
36407cc577a4SJonathan Peyton 
36417cc577a4SJonathan Peyton // __kmp_task_team_sync: Propagation of task team data from team to threads
36427cc577a4SJonathan Peyton // which happens just after the release phase of a team barrier.  This may be
36437cc577a4SJonathan Peyton // called by any thread, but only for teams with # threads > 1.
36443041982dSJonathan Peyton void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team) {
36457cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
36467cc577a4SJonathan Peyton 
36473041982dSJonathan Peyton   // Toggle the th_task_state field, to switch which task_team this thread
36483041982dSJonathan Peyton   // refers to
36497cc577a4SJonathan Peyton   this_thr->th.th_task_state = 1 - this_thr->th.th_task_state;
36503041982dSJonathan Peyton   // It is now safe to propagate the task team pointer from the team struct to
36513041982dSJonathan Peyton   // the current thread.
36523041982dSJonathan Peyton   TCW_PTR(this_thr->th.th_task_team,
36533041982dSJonathan Peyton           team->t.t_task_team[this_thr->th.th_task_state]);
36543041982dSJonathan Peyton   KA_TRACE(20,
36553041982dSJonathan Peyton            ("__kmp_task_team_sync: Thread T#%d task team switched to task_team "
36563041982dSJonathan Peyton             "%p from Team #%d (parity=%d)\n",
36577cc577a4SJonathan Peyton             __kmp_gtid_from_thread(this_thr), this_thr->th.th_task_team,
36587cc577a4SJonathan Peyton             ((team != NULL) ? team->t.t_id : -1), this_thr->th.th_task_state));
36597cc577a4SJonathan Peyton }
36607cc577a4SJonathan Peyton 
36613041982dSJonathan Peyton // __kmp_task_team_wait: Master thread waits for outstanding tasks after the
36623041982dSJonathan Peyton // barrier gather phase. Only called by master thread if #threads in team > 1 or
36633041982dSJonathan Peyton // if proxy tasks were created.
36643041982dSJonathan Peyton //
36653041982dSJonathan Peyton // wait is a flag that defaults to 1 (see kmp.h), but waiting can be turned off
36663041982dSJonathan Peyton // by passing in 0 optionally as the last argument. When wait is zero, master
36673041982dSJonathan Peyton // thread does not wait for unfinished_threads to reach 0.
36683041982dSJonathan Peyton void __kmp_task_team_wait(
36693041982dSJonathan Peyton     kmp_info_t *this_thr,
36703041982dSJonathan Peyton     kmp_team_t *team USE_ITT_BUILD_ARG(void *itt_sync_obj), int wait) {
36717cc577a4SJonathan Peyton   kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state];
36727cc577a4SJonathan Peyton 
36737cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
36747cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task_team == this_thr->th.th_task_team);
36757cc577a4SJonathan Peyton 
36767cc577a4SJonathan Peyton   if ((task_team != NULL) && KMP_TASKING_ENABLED(task_team)) {
36777cc577a4SJonathan Peyton     if (wait) {
36783041982dSJonathan Peyton       KA_TRACE(20, ("__kmp_task_team_wait: Master T#%d waiting for all tasks "
36793041982dSJonathan Peyton                     "(for unfinished_threads to reach 0) on task_team = %p\n",
36807cc577a4SJonathan Peyton                     __kmp_gtid_from_thread(this_thr), task_team));
36813041982dSJonathan Peyton       // Worker threads may have dropped through to release phase, but could
36823041982dSJonathan Peyton       // still be executing tasks. Wait here for tasks to complete. To avoid
36833041982dSJonathan Peyton       // memory contention, only master thread checks termination condition.
368437e2ef54SJonathan Peyton       kmp_flag_32 flag(RCAST(std::atomic<kmp_uint32> *,
368537e2ef54SJonathan Peyton                              &task_team->tt.tt_unfinished_threads),
3686c47afcd9SAndrey Churbanov                        0U);
36873041982dSJonathan Peyton       flag.wait(this_thr, TRUE USE_ITT_BUILD_ARG(itt_sync_obj));
36887cc577a4SJonathan Peyton     }
36893041982dSJonathan Peyton     // Deactivate the old task team, so that the worker threads will stop
36903041982dSJonathan Peyton     // referencing it while spinning.
36913041982dSJonathan Peyton     KA_TRACE(
36923041982dSJonathan Peyton         20,
36933041982dSJonathan Peyton         ("__kmp_task_team_wait: Master T#%d deactivating task_team %p: "
36947cc577a4SJonathan Peyton          "setting active to false, setting local and team's pointer to NULL\n",
36957cc577a4SJonathan Peyton          __kmp_gtid_from_thread(this_thr), task_team));
36967cc577a4SJonathan Peyton #if OMP_45_ENABLED
36973041982dSJonathan Peyton     KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1 ||
36983041982dSJonathan Peyton                      task_team->tt.tt_found_proxy_tasks == TRUE);
36997cc577a4SJonathan Peyton     TCW_SYNC_4(task_team->tt.tt_found_proxy_tasks, FALSE);
37007cc577a4SJonathan Peyton #else
37017cc577a4SJonathan Peyton     KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1);
37027cc577a4SJonathan Peyton #endif
3703a756cb24SAndrey Churbanov     KMP_CHECK_UPDATE(task_team->tt.tt_untied_task_encountered, 0);
37047cc577a4SJonathan Peyton     TCW_SYNC_4(task_team->tt.tt_active, FALSE);
37057cc577a4SJonathan Peyton     KMP_MB();
37067cc577a4SJonathan Peyton 
37077cc577a4SJonathan Peyton     TCW_PTR(this_thr->th.th_task_team, NULL);
37087cc577a4SJonathan Peyton   }
37097cc577a4SJonathan Peyton }
37107cc577a4SJonathan Peyton 
37117cc577a4SJonathan Peyton // __kmp_tasking_barrier:
37127cc577a4SJonathan Peyton // This routine may only called when __kmp_tasking_mode == tskm_extra_barrier.
37133041982dSJonathan Peyton // Internal function to execute all tasks prior to a regular barrier or a join
37143041982dSJonathan Peyton // barrier. It is a full barrier itself, which unfortunately turns regular
37153041982dSJonathan Peyton // barriers into double barriers and join barriers into 1 1/2 barriers.
37163041982dSJonathan Peyton void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread, int gtid) {
371737e2ef54SJonathan Peyton   std::atomic<kmp_uint32> *spin = RCAST(
371837e2ef54SJonathan Peyton       std::atomic<kmp_uint32> *,
3719c47afcd9SAndrey Churbanov       &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads);
37207cc577a4SJonathan Peyton   int flag = FALSE;
37217cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode == tskm_extra_barrier);
37227cc577a4SJonathan Peyton 
37237cc577a4SJonathan Peyton #if USE_ITT_BUILD
372437e2ef54SJonathan Peyton   KMP_FSYNC_SPIN_INIT(spin, NULL);
37257cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
37267cc577a4SJonathan Peyton   kmp_flag_32 spin_flag(spin, 0U);
37273041982dSJonathan Peyton   while (!spin_flag.execute_tasks(thread, gtid, TRUE,
37283041982dSJonathan Peyton                                   &flag USE_ITT_BUILD_ARG(NULL), 0)) {
37297cc577a4SJonathan Peyton #if USE_ITT_BUILD
37307cc577a4SJonathan Peyton     // TODO: What about itt_sync_obj??
373137e2ef54SJonathan Peyton     KMP_FSYNC_SPIN_PREPARE(RCAST(void *, spin));
37327cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
37337cc577a4SJonathan Peyton 
37347cc577a4SJonathan Peyton     if (TCR_4(__kmp_global.g.g_done)) {
37357cc577a4SJonathan Peyton       if (__kmp_global.g.g_abort)
37367cc577a4SJonathan Peyton         __kmp_abort_thread();
37377cc577a4SJonathan Peyton       break;
37387cc577a4SJonathan Peyton     }
3739e47d32f1SJonathan Peyton     KMP_YIELD(TRUE);
37407cc577a4SJonathan Peyton   }
37417cc577a4SJonathan Peyton #if USE_ITT_BUILD
374237e2ef54SJonathan Peyton   KMP_FSYNC_SPIN_ACQUIRED(RCAST(void *, spin));
37437cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
37447cc577a4SJonathan Peyton }
37457cc577a4SJonathan Peyton 
37467cc577a4SJonathan Peyton #if OMP_45_ENABLED
37477cc577a4SJonathan Peyton 
37483041982dSJonathan Peyton // __kmp_give_task puts a task into a given thread queue if:
37493041982dSJonathan Peyton //  - the queue for that thread was created
37503041982dSJonathan Peyton //  - there's space in that queue
37513041982dSJonathan Peyton // Because of this, __kmp_push_task needs to check if there's space after
37523041982dSJonathan Peyton // getting the lock
37533041982dSJonathan Peyton static bool __kmp_give_task(kmp_info_t *thread, kmp_int32 tid, kmp_task_t *task,
37543041982dSJonathan Peyton                             kmp_int32 pass) {
37557cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
37567cc577a4SJonathan Peyton   kmp_task_team_t *task_team = taskdata->td_task_team;
37577cc577a4SJonathan Peyton 
37583041982dSJonathan Peyton   KA_TRACE(20, ("__kmp_give_task: trying to give task %p to thread %d.\n",
37593041982dSJonathan Peyton                 taskdata, tid));
37607cc577a4SJonathan Peyton 
37617cc577a4SJonathan Peyton   // If task_team is NULL something went really bad...
37627cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task_team != NULL);
37637cc577a4SJonathan Peyton 
37647cc577a4SJonathan Peyton   bool result = false;
37657cc577a4SJonathan Peyton   kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
37667cc577a4SJonathan Peyton 
37677cc577a4SJonathan Peyton   if (thread_data->td.td_deque == NULL) {
37687cc577a4SJonathan Peyton     // There's no queue in this thread, go find another one
37697cc577a4SJonathan Peyton     // We're guaranteed that at least one thread has a queue
37703041982dSJonathan Peyton     KA_TRACE(30,
37713041982dSJonathan Peyton              ("__kmp_give_task: thread %d has no queue while giving task %p.\n",
37723041982dSJonathan Peyton               tid, taskdata));
37737cc577a4SJonathan Peyton     return result;
37747cc577a4SJonathan Peyton   }
37757cc577a4SJonathan Peyton 
37763041982dSJonathan Peyton   if (TCR_4(thread_data->td.td_deque_ntasks) >=
37773041982dSJonathan Peyton       TASK_DEQUE_SIZE(thread_data->td)) {
37783041982dSJonathan Peyton     KA_TRACE(
37793041982dSJonathan Peyton         30,
37803041982dSJonathan Peyton         ("__kmp_give_task: queue is full while giving task %p to thread %d.\n",
37813041982dSJonathan Peyton          taskdata, tid));
37827cc577a4SJonathan Peyton 
37833041982dSJonathan Peyton     // if this deque is bigger than the pass ratio give a chance to another
37843041982dSJonathan Peyton     // thread
37853041982dSJonathan Peyton     if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
37863041982dSJonathan Peyton       return result;
37877cc577a4SJonathan Peyton 
37887cc577a4SJonathan Peyton     __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
37897cc577a4SJonathan Peyton     __kmp_realloc_task_deque(thread, thread_data);
37907cc577a4SJonathan Peyton 
37917cc577a4SJonathan Peyton   } else {
37927cc577a4SJonathan Peyton 
37937cc577a4SJonathan Peyton     __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
37947cc577a4SJonathan Peyton 
37953041982dSJonathan Peyton     if (TCR_4(thread_data->td.td_deque_ntasks) >=
37963041982dSJonathan Peyton         TASK_DEQUE_SIZE(thread_data->td)) {
37973041982dSJonathan Peyton       KA_TRACE(30, ("__kmp_give_task: queue is full while giving task %p to "
37983041982dSJonathan Peyton                     "thread %d.\n",
37993041982dSJonathan Peyton                     taskdata, tid));
38007cc577a4SJonathan Peyton 
38013041982dSJonathan Peyton       // if this deque is bigger than the pass ratio give a chance to another
38023041982dSJonathan Peyton       // thread
38037cc577a4SJonathan Peyton       if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
38047cc577a4SJonathan Peyton         goto release_and_exit;
38057cc577a4SJonathan Peyton 
38067cc577a4SJonathan Peyton       __kmp_realloc_task_deque(thread, thread_data);
38077cc577a4SJonathan Peyton     }
38087cc577a4SJonathan Peyton   }
38097cc577a4SJonathan Peyton 
38107cc577a4SJonathan Peyton   // lock is held here, and there is space in the deque
38117cc577a4SJonathan Peyton 
38127cc577a4SJonathan Peyton   thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
38137cc577a4SJonathan Peyton   // Wrap index.
38143041982dSJonathan Peyton   thread_data->td.td_deque_tail =
38153041982dSJonathan Peyton       (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
38163041982dSJonathan Peyton   TCW_4(thread_data->td.td_deque_ntasks,
38173041982dSJonathan Peyton         TCR_4(thread_data->td.td_deque_ntasks) + 1);
38187cc577a4SJonathan Peyton 
38197cc577a4SJonathan Peyton   result = true;
38203041982dSJonathan Peyton   KA_TRACE(30, ("__kmp_give_task: successfully gave task %p to thread %d.\n",
38213041982dSJonathan Peyton                 taskdata, tid));
38227cc577a4SJonathan Peyton 
38237cc577a4SJonathan Peyton release_and_exit:
38247cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
38257cc577a4SJonathan Peyton 
38267cc577a4SJonathan Peyton   return result;
38277cc577a4SJonathan Peyton }
38287cc577a4SJonathan Peyton 
38293041982dSJonathan Peyton /* The finish of the proxy tasks is divided in two pieces:
38307cc577a4SJonathan Peyton     - the top half is the one that can be done from a thread outside the team
3831c3344345SAndrey Churbanov     - the bottom half must be run from a thread within the team
38327cc577a4SJonathan Peyton 
38333041982dSJonathan Peyton    In order to run the bottom half the task gets queued back into one of the
38343041982dSJonathan Peyton    threads of the team. Once the td_incomplete_child_task counter of the parent
38353041982dSJonathan Peyton    is decremented the threads can leave the barriers. So, the bottom half needs
38363041982dSJonathan Peyton    to be queued before the counter is decremented. The top half is therefore
38373041982dSJonathan Peyton    divided in two parts:
38387cc577a4SJonathan Peyton     - things that can be run before queuing the bottom half
38397cc577a4SJonathan Peyton     - things that must be run after queuing the bottom half
38407cc577a4SJonathan Peyton 
38413041982dSJonathan Peyton    This creates a second race as the bottom half can free the task before the
38423041982dSJonathan Peyton    second top half is executed. To avoid this we use the
38433041982dSJonathan Peyton    td_incomplete_child_task of the proxy task to synchronize the top and bottom
38443041982dSJonathan Peyton    half. */
38453041982dSJonathan Peyton static void __kmp_first_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
38467cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
38477cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
38487cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
38497cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
38507cc577a4SJonathan Peyton 
38517cc577a4SJonathan Peyton   taskdata->td_flags.complete = 1; // mark the task as completed
38527cc577a4SJonathan Peyton 
38537cc577a4SJonathan Peyton   if (taskdata->td_taskgroup)
385437e2ef54SJonathan Peyton     KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
38557cc577a4SJonathan Peyton 
38563041982dSJonathan Peyton   // Create an imaginary children for this task so the bottom half cannot
38573041982dSJonathan Peyton   // release the task before we have completed the second top half
385837e2ef54SJonathan Peyton   KMP_ATOMIC_INC(&taskdata->td_incomplete_child_tasks);
38597cc577a4SJonathan Peyton }
38607cc577a4SJonathan Peyton 
38613041982dSJonathan Peyton static void __kmp_second_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
38627cc577a4SJonathan Peyton   kmp_int32 children = 0;
38637cc577a4SJonathan Peyton 
38647cc577a4SJonathan Peyton   // Predecrement simulated by "- 1" calculation
38653041982dSJonathan Peyton   children =
386637e2ef54SJonathan Peyton       KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks) - 1;
38677cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(children >= 0);
38687cc577a4SJonathan Peyton 
38697cc577a4SJonathan Peyton   // Remove the imaginary children
387037e2ef54SJonathan Peyton   KMP_ATOMIC_DEC(&taskdata->td_incomplete_child_tasks);
38717cc577a4SJonathan Peyton }
38727cc577a4SJonathan Peyton 
38733041982dSJonathan Peyton static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask) {
38747cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
38757cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
38767cc577a4SJonathan Peyton 
38777cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
38783041982dSJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.complete ==
38793041982dSJonathan Peyton                    1); // top half must run before bottom half
38807cc577a4SJonathan Peyton 
38817cc577a4SJonathan Peyton   // We need to wait to make sure the top half is finished
38827cc577a4SJonathan Peyton   // Spinning here should be ok as this should happen quickly
388337e2ef54SJonathan Peyton   while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) > 0)
38843041982dSJonathan Peyton     ;
38857cc577a4SJonathan Peyton 
38867cc577a4SJonathan Peyton   __kmp_release_deps(gtid, taskdata);
38877cc577a4SJonathan Peyton   __kmp_free_task_and_ancestors(gtid, taskdata, thread);
38887cc577a4SJonathan Peyton }
38897cc577a4SJonathan Peyton 
38907cc577a4SJonathan Peyton /*!
38917cc577a4SJonathan Peyton @ingroup TASKING
38927cc577a4SJonathan Peyton @param gtid Global Thread ID of encountering thread
38937cc577a4SJonathan Peyton @param ptask Task which execution is completed
38947cc577a4SJonathan Peyton 
38953041982dSJonathan Peyton Execute the completation of a proxy task from a thread of that is part of the
38963041982dSJonathan Peyton team. Run first and bottom halves directly.
38977cc577a4SJonathan Peyton */
38983041982dSJonathan Peyton void __kmpc_proxy_task_completed(kmp_int32 gtid, kmp_task_t *ptask) {
38997cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(ptask != NULL);
39007cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
39013041982dSJonathan Peyton   KA_TRACE(
39023041982dSJonathan Peyton       10, ("__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n",
39033041982dSJonathan Peyton            gtid, taskdata));
39047cc577a4SJonathan Peyton 
39057cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
39067cc577a4SJonathan Peyton 
39077cc577a4SJonathan Peyton   __kmp_first_top_half_finish_proxy(taskdata);
39087cc577a4SJonathan Peyton   __kmp_second_top_half_finish_proxy(taskdata);
39097cc577a4SJonathan Peyton   __kmp_bottom_half_finish_proxy(gtid, ptask);
39107cc577a4SJonathan Peyton 
39113041982dSJonathan Peyton   KA_TRACE(10,
39123041982dSJonathan Peyton            ("__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n",
39133041982dSJonathan Peyton             gtid, taskdata));
39147cc577a4SJonathan Peyton }
39157cc577a4SJonathan Peyton 
39167cc577a4SJonathan Peyton /*!
39177cc577a4SJonathan Peyton @ingroup TASKING
39187cc577a4SJonathan Peyton @param ptask Task which execution is completed
39197cc577a4SJonathan Peyton 
39203041982dSJonathan Peyton Execute the completation of a proxy task from a thread that could not belong to
39213041982dSJonathan Peyton the team.
39227cc577a4SJonathan Peyton */
39233041982dSJonathan Peyton void __kmpc_proxy_task_completed_ooo(kmp_task_t *ptask) {
39247cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(ptask != NULL);
39257cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
39267cc577a4SJonathan Peyton 
39273041982dSJonathan Peyton   KA_TRACE(
39283041982dSJonathan Peyton       10,
39293041982dSJonathan Peyton       ("__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n",
39303041982dSJonathan Peyton        taskdata));
39317cc577a4SJonathan Peyton 
39327cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
39337cc577a4SJonathan Peyton 
39347cc577a4SJonathan Peyton   __kmp_first_top_half_finish_proxy(taskdata);
39357cc577a4SJonathan Peyton 
39363041982dSJonathan Peyton   // Enqueue task to complete bottom half completion from a thread within the
39373041982dSJonathan Peyton   // corresponding team
39387cc577a4SJonathan Peyton   kmp_team_t *team = taskdata->td_team;
39397cc577a4SJonathan Peyton   kmp_int32 nthreads = team->t.t_nproc;
39407cc577a4SJonathan Peyton   kmp_info_t *thread;
39417cc577a4SJonathan Peyton 
39423041982dSJonathan Peyton   // This should be similar to start_k = __kmp_get_random( thread ) % nthreads
39433041982dSJonathan Peyton   // but we cannot use __kmp_get_random here
39447cc577a4SJonathan Peyton   kmp_int32 start_k = 0;
39457cc577a4SJonathan Peyton   kmp_int32 pass = 1;
39467cc577a4SJonathan Peyton   kmp_int32 k = start_k;
39477cc577a4SJonathan Peyton 
39487cc577a4SJonathan Peyton   do {
39497cc577a4SJonathan Peyton     // For now we're just linearly trying to find a thread
39507cc577a4SJonathan Peyton     thread = team->t.t_threads[k];
39517cc577a4SJonathan Peyton     k = (k + 1) % nthreads;
39527cc577a4SJonathan Peyton 
39537cc577a4SJonathan Peyton     // we did a full pass through all the threads
39543041982dSJonathan Peyton     if (k == start_k)
39553041982dSJonathan Peyton       pass = pass << 1;
39567cc577a4SJonathan Peyton 
39577cc577a4SJonathan Peyton   } while (!__kmp_give_task(thread, k, ptask, pass));
39587cc577a4SJonathan Peyton 
39597cc577a4SJonathan Peyton   __kmp_second_top_half_finish_proxy(taskdata);
39607cc577a4SJonathan Peyton 
39613041982dSJonathan Peyton   KA_TRACE(
39623041982dSJonathan Peyton       10,
39633041982dSJonathan Peyton       ("__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n",
39643041982dSJonathan Peyton        taskdata));
39657cc577a4SJonathan Peyton }
39667cc577a4SJonathan Peyton 
3967405037c4SAndrey Churbanov #if OMP_50_ENABLED
3968405037c4SAndrey Churbanov kmp_event_t *__kmpc_task_allow_completion_event(ident_t *loc_ref, int gtid,
3969405037c4SAndrey Churbanov                                                 kmp_task_t *task) {
3970405037c4SAndrey Churbanov   kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(task);
3971405037c4SAndrey Churbanov   if (td->td_allow_completion_event.type == KMP_EVENT_UNINITIALIZED) {
3972405037c4SAndrey Churbanov     td->td_allow_completion_event.type = KMP_EVENT_ALLOW_COMPLETION;
3973405037c4SAndrey Churbanov     td->td_allow_completion_event.ed.task = task;
3974405037c4SAndrey Churbanov     __kmp_init_tas_lock(&td->td_allow_completion_event.lock);
3975405037c4SAndrey Churbanov   }
3976405037c4SAndrey Churbanov   return &td->td_allow_completion_event;
3977405037c4SAndrey Churbanov }
3978405037c4SAndrey Churbanov 
3979405037c4SAndrey Churbanov void __kmp_fulfill_event(kmp_event_t *event) {
3980405037c4SAndrey Churbanov   if (event->type == KMP_EVENT_ALLOW_COMPLETION) {
3981405037c4SAndrey Churbanov     kmp_task_t *ptask = event->ed.task;
3982405037c4SAndrey Churbanov     kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3983405037c4SAndrey Churbanov     bool detached = false;
3984405037c4SAndrey Churbanov     int gtid = __kmp_get_gtid();
3985405037c4SAndrey Churbanov 
3986405037c4SAndrey Churbanov     if (taskdata->td_flags.proxy == TASK_PROXY) {
3987405037c4SAndrey Churbanov       // The associated task code completed before this call and detached.
3988405037c4SAndrey Churbanov       detached = true;
3989405037c4SAndrey Churbanov       event->type = KMP_EVENT_UNINITIALIZED;
3990405037c4SAndrey Churbanov     } else {
3991405037c4SAndrey Churbanov       // The associated task has not completed but could be completing at this
3992405037c4SAndrey Churbanov       // point.
3993405037c4SAndrey Churbanov       // We need to take the lock to avoid races
3994405037c4SAndrey Churbanov       __kmp_acquire_tas_lock(&event->lock, gtid);
3995405037c4SAndrey Churbanov       if (taskdata->td_flags.proxy == TASK_PROXY)
3996405037c4SAndrey Churbanov         detached = true;
3997405037c4SAndrey Churbanov       event->type = KMP_EVENT_UNINITIALIZED;
3998405037c4SAndrey Churbanov       __kmp_release_tas_lock(&event->lock, gtid);
3999405037c4SAndrey Churbanov     }
4000405037c4SAndrey Churbanov 
4001405037c4SAndrey Churbanov     if (detached) {
4002405037c4SAndrey Churbanov       // If the task detached complete the proxy task
4003405037c4SAndrey Churbanov       if (gtid >= 0) {
4004405037c4SAndrey Churbanov         kmp_team_t *team = taskdata->td_team;
4005405037c4SAndrey Churbanov         kmp_info_t *thread = __kmp_get_thread();
4006405037c4SAndrey Churbanov         if (thread->th.th_team == team) {
4007405037c4SAndrey Churbanov           __kmpc_proxy_task_completed(gtid, ptask);
4008405037c4SAndrey Churbanov           return;
4009405037c4SAndrey Churbanov         }
4010405037c4SAndrey Churbanov       }
4011405037c4SAndrey Churbanov 
4012405037c4SAndrey Churbanov       // fallback
4013405037c4SAndrey Churbanov       __kmpc_proxy_task_completed_ooo(ptask);
4014405037c4SAndrey Churbanov     }
4015405037c4SAndrey Churbanov   }
4016405037c4SAndrey Churbanov }
4017405037c4SAndrey Churbanov #endif
4018405037c4SAndrey Churbanov 
40193041982dSJonathan Peyton // __kmp_task_dup_alloc: Allocate the taskdata and make a copy of source task
40203041982dSJonathan Peyton // for taskloop
40217cc577a4SJonathan Peyton //
40227cc577a4SJonathan Peyton // thread:   allocating thread
40237cc577a4SJonathan Peyton // task_src: pointer to source task to be duplicated
40247cc577a4SJonathan Peyton // returns:  a pointer to the allocated kmp_task_t structure (task).
40253041982dSJonathan Peyton kmp_task_t *__kmp_task_dup_alloc(kmp_info_t *thread, kmp_task_t *task_src) {
40267cc577a4SJonathan Peyton   kmp_task_t *task;
40277cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata;
40287cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata_src;
40297cc577a4SJonathan Peyton   kmp_taskdata_t *parent_task = thread->th.th_current_task;
40307cc577a4SJonathan Peyton   size_t shareds_offset;
40317cc577a4SJonathan Peyton   size_t task_size;
40327cc577a4SJonathan Peyton 
40333041982dSJonathan Peyton   KA_TRACE(10, ("__kmp_task_dup_alloc(enter): Th %p, source task %p\n", thread,
40343041982dSJonathan Peyton                 task_src));
40357cc577a4SJonathan Peyton   taskdata_src = KMP_TASK_TO_TASKDATA(task_src);
40363041982dSJonathan Peyton   KMP_DEBUG_ASSERT(taskdata_src->td_flags.proxy ==
40373041982dSJonathan Peyton                    TASK_FULL); // it should not be proxy task
40387cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata_src->td_flags.tasktype == TASK_EXPLICIT);
40397cc577a4SJonathan Peyton   task_size = taskdata_src->td_size_alloc;
40407cc577a4SJonathan Peyton 
40417cc577a4SJonathan Peyton   // Allocate a kmp_taskdata_t block and a kmp_task_t block.
40423041982dSJonathan Peyton   KA_TRACE(30, ("__kmp_task_dup_alloc: Th %p, malloc size %ld\n", thread,
40433041982dSJonathan Peyton                 task_size));
40447cc577a4SJonathan Peyton #if USE_FAST_MEMORY
40457cc577a4SJonathan Peyton   taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, task_size);
40467cc577a4SJonathan Peyton #else
40477cc577a4SJonathan Peyton   taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, task_size);
40487cc577a4SJonathan Peyton #endif /* USE_FAST_MEMORY */
40497cc577a4SJonathan Peyton   KMP_MEMCPY(taskdata, taskdata_src, task_size);
40507cc577a4SJonathan Peyton 
40517cc577a4SJonathan Peyton   task = KMP_TASKDATA_TO_TASK(taskdata);
40527cc577a4SJonathan Peyton 
40537cc577a4SJonathan Peyton   // Initialize new task (only specific fields not affected by memcpy)
40547cc577a4SJonathan Peyton   taskdata->td_task_id = KMP_GEN_TASK_ID();
40557cc577a4SJonathan Peyton   if (task->shareds != NULL) { // need setup shareds pointer
40567cc577a4SJonathan Peyton     shareds_offset = (char *)task_src->shareds - (char *)taskdata_src;
40577cc577a4SJonathan Peyton     task->shareds = &((char *)taskdata)[shareds_offset];
40583041982dSJonathan Peyton     KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (sizeof(void *) - 1)) ==
40593041982dSJonathan Peyton                      0);
40607cc577a4SJonathan Peyton   }
40617cc577a4SJonathan Peyton   taskdata->td_alloc_thread = thread;
406293e17cfeSJonathan Peyton   taskdata->td_parent = parent_task;
40633041982dSJonathan Peyton   taskdata->td_taskgroup =
40643041982dSJonathan Peyton       parent_task
40653041982dSJonathan Peyton           ->td_taskgroup; // task inherits the taskgroup from the parent task
40667cc577a4SJonathan Peyton 
40673041982dSJonathan Peyton   // Only need to keep track of child task counts if team parallel and tasking
40683041982dSJonathan Peyton   // not serialized
40697cc577a4SJonathan Peyton   if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
407037e2ef54SJonathan Peyton     KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
40717cc577a4SJonathan Peyton     if (parent_task->td_taskgroup)
407237e2ef54SJonathan Peyton       KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
40733041982dSJonathan Peyton     // Only need to keep track of allocated child tasks for explicit tasks since
40743041982dSJonathan Peyton     // implicit not deallocated
40757cc577a4SJonathan Peyton     if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT)
407637e2ef54SJonathan Peyton       KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
40777cc577a4SJonathan Peyton   }
40787cc577a4SJonathan Peyton 
40793041982dSJonathan Peyton   KA_TRACE(20,
40803041982dSJonathan Peyton            ("__kmp_task_dup_alloc(exit): Th %p, created task %p, parent=%p\n",
40817cc577a4SJonathan Peyton             thread, taskdata, taskdata->td_parent));
40827cc577a4SJonathan Peyton #if OMPT_SUPPORT
4083f0a1c65fSJonas Hahnfeld   if (UNLIKELY(ompt_enabled.enabled))
408482e94a59SJoachim Protze     __ompt_task_init(taskdata, thread->th.th_info.ds.ds_gtid);
40857cc577a4SJonathan Peyton #endif
40867cc577a4SJonathan Peyton   return task;
40877cc577a4SJonathan Peyton }
40887cc577a4SJonathan Peyton 
40897cc577a4SJonathan Peyton // Routine optionally generated by the compiler for setting the lastprivate flag
40907cc577a4SJonathan Peyton // and calling needed constructors for private/firstprivate objects
40917cc577a4SJonathan Peyton // (used to form taskloop tasks from pattern task)
409293e17cfeSJonathan Peyton // Parameters: dest task, src task, lastprivate flag.
40937cc577a4SJonathan Peyton typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32);
40947cc577a4SJonathan Peyton 
409527a677fcSJonathan Peyton KMP_BUILD_ASSERT(sizeof(long) == 4 || sizeof(long) == 8);
409627a677fcSJonathan Peyton 
409727a677fcSJonathan Peyton // class to encapsulate manipulating loop bounds in a taskloop task.
409827a677fcSJonathan Peyton // this abstracts away the Intel vs GOMP taskloop interface for setting/getting
409927a677fcSJonathan Peyton // the loop bound variables.
410027a677fcSJonathan Peyton class kmp_taskloop_bounds_t {
410127a677fcSJonathan Peyton   kmp_task_t *task;
410227a677fcSJonathan Peyton   const kmp_taskdata_t *taskdata;
410327a677fcSJonathan Peyton   size_t lower_offset;
410427a677fcSJonathan Peyton   size_t upper_offset;
410527a677fcSJonathan Peyton 
410627a677fcSJonathan Peyton public:
410727a677fcSJonathan Peyton   kmp_taskloop_bounds_t(kmp_task_t *_task, kmp_uint64 *lb, kmp_uint64 *ub)
410827a677fcSJonathan Peyton       : task(_task), taskdata(KMP_TASK_TO_TASKDATA(task)),
410927a677fcSJonathan Peyton         lower_offset((char *)lb - (char *)task),
411027a677fcSJonathan Peyton         upper_offset((char *)ub - (char *)task) {
411127a677fcSJonathan Peyton     KMP_DEBUG_ASSERT((char *)lb > (char *)_task);
411227a677fcSJonathan Peyton     KMP_DEBUG_ASSERT((char *)ub > (char *)_task);
411327a677fcSJonathan Peyton   }
411427a677fcSJonathan Peyton   kmp_taskloop_bounds_t(kmp_task_t *_task, const kmp_taskloop_bounds_t &bounds)
411527a677fcSJonathan Peyton       : task(_task), taskdata(KMP_TASK_TO_TASKDATA(_task)),
411627a677fcSJonathan Peyton         lower_offset(bounds.lower_offset), upper_offset(bounds.upper_offset) {}
411727a677fcSJonathan Peyton   size_t get_lower_offset() const { return lower_offset; }
411827a677fcSJonathan Peyton   size_t get_upper_offset() const { return upper_offset; }
411927a677fcSJonathan Peyton   kmp_uint64 get_lb() const {
412027a677fcSJonathan Peyton     kmp_int64 retval;
412127a677fcSJonathan Peyton #if defined(KMP_GOMP_COMPAT)
412227a677fcSJonathan Peyton     // Intel task just returns the lower bound normally
412327a677fcSJonathan Peyton     if (!taskdata->td_flags.native) {
412427a677fcSJonathan Peyton       retval = *(kmp_int64 *)((char *)task + lower_offset);
412527a677fcSJonathan Peyton     } else {
412627a677fcSJonathan Peyton       // GOMP task has to take into account the sizeof(long)
412727a677fcSJonathan Peyton       if (taskdata->td_size_loop_bounds == 4) {
412827a677fcSJonathan Peyton         kmp_int32 *lb = RCAST(kmp_int32 *, task->shareds);
412927a677fcSJonathan Peyton         retval = (kmp_int64)*lb;
413027a677fcSJonathan Peyton       } else {
413127a677fcSJonathan Peyton         kmp_int64 *lb = RCAST(kmp_int64 *, task->shareds);
413227a677fcSJonathan Peyton         retval = (kmp_int64)*lb;
413327a677fcSJonathan Peyton       }
413427a677fcSJonathan Peyton     }
413527a677fcSJonathan Peyton #else
413627a677fcSJonathan Peyton     retval = *(kmp_int64 *)((char *)task + lower_offset);
413727a677fcSJonathan Peyton #endif // defined(KMP_GOMP_COMPAT)
413827a677fcSJonathan Peyton     return retval;
413927a677fcSJonathan Peyton   }
414027a677fcSJonathan Peyton   kmp_uint64 get_ub() const {
414127a677fcSJonathan Peyton     kmp_int64 retval;
414227a677fcSJonathan Peyton #if defined(KMP_GOMP_COMPAT)
414327a677fcSJonathan Peyton     // Intel task just returns the upper bound normally
414427a677fcSJonathan Peyton     if (!taskdata->td_flags.native) {
414527a677fcSJonathan Peyton       retval = *(kmp_int64 *)((char *)task + upper_offset);
414627a677fcSJonathan Peyton     } else {
414727a677fcSJonathan Peyton       // GOMP task has to take into account the sizeof(long)
414827a677fcSJonathan Peyton       if (taskdata->td_size_loop_bounds == 4) {
414927a677fcSJonathan Peyton         kmp_int32 *ub = RCAST(kmp_int32 *, task->shareds) + 1;
415027a677fcSJonathan Peyton         retval = (kmp_int64)*ub;
415127a677fcSJonathan Peyton       } else {
415227a677fcSJonathan Peyton         kmp_int64 *ub = RCAST(kmp_int64 *, task->shareds) + 1;
415327a677fcSJonathan Peyton         retval = (kmp_int64)*ub;
415427a677fcSJonathan Peyton       }
415527a677fcSJonathan Peyton     }
415627a677fcSJonathan Peyton #else
415727a677fcSJonathan Peyton     retval = *(kmp_int64 *)((char *)task + upper_offset);
415827a677fcSJonathan Peyton #endif // defined(KMP_GOMP_COMPAT)
415927a677fcSJonathan Peyton     return retval;
416027a677fcSJonathan Peyton   }
416127a677fcSJonathan Peyton   void set_lb(kmp_uint64 lb) {
416227a677fcSJonathan Peyton #if defined(KMP_GOMP_COMPAT)
416327a677fcSJonathan Peyton     // Intel task just sets the lower bound normally
416427a677fcSJonathan Peyton     if (!taskdata->td_flags.native) {
416527a677fcSJonathan Peyton       *(kmp_uint64 *)((char *)task + lower_offset) = lb;
416627a677fcSJonathan Peyton     } else {
416727a677fcSJonathan Peyton       // GOMP task has to take into account the sizeof(long)
416827a677fcSJonathan Peyton       if (taskdata->td_size_loop_bounds == 4) {
416927a677fcSJonathan Peyton         kmp_uint32 *lower = RCAST(kmp_uint32 *, task->shareds);
417027a677fcSJonathan Peyton         *lower = (kmp_uint32)lb;
417127a677fcSJonathan Peyton       } else {
417227a677fcSJonathan Peyton         kmp_uint64 *lower = RCAST(kmp_uint64 *, task->shareds);
417327a677fcSJonathan Peyton         *lower = (kmp_uint64)lb;
417427a677fcSJonathan Peyton       }
417527a677fcSJonathan Peyton     }
417627a677fcSJonathan Peyton #else
417727a677fcSJonathan Peyton     *(kmp_uint64 *)((char *)task + lower_offset) = lb;
417827a677fcSJonathan Peyton #endif // defined(KMP_GOMP_COMPAT)
417927a677fcSJonathan Peyton   }
418027a677fcSJonathan Peyton   void set_ub(kmp_uint64 ub) {
418127a677fcSJonathan Peyton #if defined(KMP_GOMP_COMPAT)
418227a677fcSJonathan Peyton     // Intel task just sets the upper bound normally
418327a677fcSJonathan Peyton     if (!taskdata->td_flags.native) {
418427a677fcSJonathan Peyton       *(kmp_uint64 *)((char *)task + upper_offset) = ub;
418527a677fcSJonathan Peyton     } else {
418627a677fcSJonathan Peyton       // GOMP task has to take into account the sizeof(long)
418727a677fcSJonathan Peyton       if (taskdata->td_size_loop_bounds == 4) {
418827a677fcSJonathan Peyton         kmp_uint32 *upper = RCAST(kmp_uint32 *, task->shareds) + 1;
418927a677fcSJonathan Peyton         *upper = (kmp_uint32)ub;
419027a677fcSJonathan Peyton       } else {
419127a677fcSJonathan Peyton         kmp_uint64 *upper = RCAST(kmp_uint64 *, task->shareds) + 1;
419227a677fcSJonathan Peyton         *upper = (kmp_uint64)ub;
419327a677fcSJonathan Peyton       }
419427a677fcSJonathan Peyton     }
419527a677fcSJonathan Peyton #else
419627a677fcSJonathan Peyton     *(kmp_uint64 *)((char *)task + upper_offset) = ub;
419727a677fcSJonathan Peyton #endif // defined(KMP_GOMP_COMPAT)
419827a677fcSJonathan Peyton   }
419927a677fcSJonathan Peyton };
420027a677fcSJonathan Peyton 
42017cc577a4SJonathan Peyton // __kmp_taskloop_linear: Start tasks of the taskloop linearly
42027cc577a4SJonathan Peyton //
42037cc577a4SJonathan Peyton // loc        Source location information
42047cc577a4SJonathan Peyton // gtid       Global thread ID
420593e17cfeSJonathan Peyton // task       Pattern task, exposes the loop iteration range
420693e17cfeSJonathan Peyton // lb         Pointer to loop lower bound in task structure
420793e17cfeSJonathan Peyton // ub         Pointer to loop upper bound in task structure
42087cc577a4SJonathan Peyton // st         Loop stride
420993e17cfeSJonathan Peyton // ub_glob    Global upper bound (used for lastprivate check)
421093e17cfeSJonathan Peyton // num_tasks  Number of tasks to execute
421193e17cfeSJonathan Peyton // grainsize  Number of loop iterations per task
421293e17cfeSJonathan Peyton // extras     Number of chunks with grainsize+1 iterations
421393e17cfeSJonathan Peyton // tc         Iterations count
42147cc577a4SJonathan Peyton // task_dup   Tasks duplication routine
4215cdaefac5SJoachim Protze // codeptr_ra Return address for OMPT events
42163041982dSJonathan Peyton void __kmp_taskloop_linear(ident_t *loc, int gtid, kmp_task_t *task,
42177cc577a4SJonathan Peyton                            kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
421893e17cfeSJonathan Peyton                            kmp_uint64 ub_glob, kmp_uint64 num_tasks,
421993e17cfeSJonathan Peyton                            kmp_uint64 grainsize, kmp_uint64 extras,
4220cdaefac5SJoachim Protze                            kmp_uint64 tc,
4221cdaefac5SJoachim Protze #if OMPT_SUPPORT
4222cdaefac5SJoachim Protze                            void *codeptr_ra,
4223cdaefac5SJoachim Protze #endif
4224cdaefac5SJoachim Protze                            void *task_dup) {
42257cc577a4SJonathan Peyton   KMP_COUNT_BLOCK(OMP_TASKLOOP);
42267cc577a4SJonathan Peyton   KMP_TIME_PARTITIONED_BLOCK(OMP_taskloop_scheduling);
42277cc577a4SJonathan Peyton   p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
422827a677fcSJonathan Peyton   // compiler provides global bounds here
422927a677fcSJonathan Peyton   kmp_taskloop_bounds_t task_bounds(task, lb, ub);
423027a677fcSJonathan Peyton   kmp_uint64 lower = task_bounds.get_lb();
423127a677fcSJonathan Peyton   kmp_uint64 upper = task_bounds.get_ub();
423293e17cfeSJonathan Peyton   kmp_uint64 i;
42337cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
42347cc577a4SJonathan Peyton   kmp_taskdata_t *current_task = thread->th.th_current_task;
42357cc577a4SJonathan Peyton   kmp_task_t *next_task;
42367cc577a4SJonathan Peyton   kmp_int32 lastpriv = 0;
42377cc577a4SJonathan Peyton 
423893e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras);
423993e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks > extras);
424093e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks > 0);
424193e17cfeSJonathan Peyton   KA_TRACE(20, ("__kmp_taskloop_linear: T#%d: %lld tasks, grainsize %lld, "
4242d74d8902SJonathan Peyton                 "extras %lld, i=%lld,%lld(%d)%lld, dup %p\n",
4243d74d8902SJonathan Peyton                 gtid, num_tasks, grainsize, extras, lower, upper, ub_glob, st,
4244d74d8902SJonathan Peyton                 task_dup));
424593e17cfeSJonathan Peyton 
424693e17cfeSJonathan Peyton   // Launch num_tasks tasks, assign grainsize iterations each task
424793e17cfeSJonathan Peyton   for (i = 0; i < num_tasks; ++i) {
424893e17cfeSJonathan Peyton     kmp_uint64 chunk_minus_1;
424993e17cfeSJonathan Peyton     if (extras == 0) {
425093e17cfeSJonathan Peyton       chunk_minus_1 = grainsize - 1;
425193e17cfeSJonathan Peyton     } else {
425293e17cfeSJonathan Peyton       chunk_minus_1 = grainsize;
425393e17cfeSJonathan Peyton       --extras; // first extras iterations get bigger chunk (grainsize+1)
425493e17cfeSJonathan Peyton     }
425593e17cfeSJonathan Peyton     upper = lower + st * chunk_minus_1;
425693e17cfeSJonathan Peyton     if (i == num_tasks - 1) {
425793e17cfeSJonathan Peyton       // schedule the last task, set lastprivate flag if needed
425893e17cfeSJonathan Peyton       if (st == 1) { // most common case
425993e17cfeSJonathan Peyton         KMP_DEBUG_ASSERT(upper == *ub);
426093e17cfeSJonathan Peyton         if (upper == ub_glob)
426193e17cfeSJonathan Peyton           lastpriv = 1;
426293e17cfeSJonathan Peyton       } else if (st > 0) { // positive loop stride
426393e17cfeSJonathan Peyton         KMP_DEBUG_ASSERT((kmp_uint64)st > *ub - upper);
426493e17cfeSJonathan Peyton         if ((kmp_uint64)st > ub_glob - upper)
426593e17cfeSJonathan Peyton           lastpriv = 1;
426693e17cfeSJonathan Peyton       } else { // negative loop stride
426793e17cfeSJonathan Peyton         KMP_DEBUG_ASSERT(upper + st < *ub);
426893e17cfeSJonathan Peyton         if (upper - ub_glob < (kmp_uint64)(-st))
426993e17cfeSJonathan Peyton           lastpriv = 1;
427093e17cfeSJonathan Peyton       }
427193e17cfeSJonathan Peyton     }
427293e17cfeSJonathan Peyton     next_task = __kmp_task_dup_alloc(thread, task); // allocate new task
427327a677fcSJonathan Peyton     kmp_taskdata_t *next_taskdata = KMP_TASK_TO_TASKDATA(next_task);
427427a677fcSJonathan Peyton     kmp_taskloop_bounds_t next_task_bounds =
427527a677fcSJonathan Peyton         kmp_taskloop_bounds_t(next_task, task_bounds);
427627a677fcSJonathan Peyton 
427793e17cfeSJonathan Peyton     // adjust task-specific bounds
427827a677fcSJonathan Peyton     next_task_bounds.set_lb(lower);
427927a677fcSJonathan Peyton     if (next_taskdata->td_flags.native) {
428027a677fcSJonathan Peyton       next_task_bounds.set_ub(upper + (st > 0 ? 1 : -1));
428127a677fcSJonathan Peyton     } else {
428227a677fcSJonathan Peyton       next_task_bounds.set_ub(upper);
428327a677fcSJonathan Peyton     }
428493e17cfeSJonathan Peyton     if (ptask_dup != NULL) // set lastprivate flag, construct fistprivates, etc.
428593e17cfeSJonathan Peyton       ptask_dup(next_task, task, lastpriv);
428627a677fcSJonathan Peyton     KA_TRACE(40,
428727a677fcSJonathan Peyton              ("__kmp_taskloop_linear: T#%d; task #%llu: task %p: lower %lld, "
428827a677fcSJonathan Peyton               "upper %lld stride %lld, (offsets %p %p)\n",
428927a677fcSJonathan Peyton               gtid, i, next_task, lower, upper, st,
429027a677fcSJonathan Peyton               next_task_bounds.get_lower_offset(),
429127a677fcSJonathan Peyton               next_task_bounds.get_upper_offset()));
4292cdaefac5SJoachim Protze #if OMPT_SUPPORT
4293cdaefac5SJoachim Protze     __kmp_omp_taskloop_task(NULL, gtid, next_task,
4294cdaefac5SJoachim Protze                            codeptr_ra); // schedule new task
4295cdaefac5SJoachim Protze #else
429693e17cfeSJonathan Peyton     __kmp_omp_task(gtid, next_task, true); // schedule new task
4297cdaefac5SJoachim Protze #endif
429893e17cfeSJonathan Peyton     lower = upper + st; // adjust lower bound for the next iteration
429993e17cfeSJonathan Peyton   }
430093e17cfeSJonathan Peyton   // free the pattern task and exit
430193e17cfeSJonathan Peyton   __kmp_task_start(gtid, task, current_task); // make internal bookkeeping
430293e17cfeSJonathan Peyton   // do not execute the pattern task, just do internal bookkeeping
430386ed6aa6SJoachim Protze   __kmp_task_finish<false>(gtid, task, current_task);
430493e17cfeSJonathan Peyton }
430593e17cfeSJonathan Peyton 
430693e17cfeSJonathan Peyton // Structure to keep taskloop parameters for auxiliary task
430793e17cfeSJonathan Peyton // kept in the shareds of the task structure.
430893e17cfeSJonathan Peyton typedef struct __taskloop_params {
430993e17cfeSJonathan Peyton   kmp_task_t *task;
431093e17cfeSJonathan Peyton   kmp_uint64 *lb;
431193e17cfeSJonathan Peyton   kmp_uint64 *ub;
431293e17cfeSJonathan Peyton   void *task_dup;
431393e17cfeSJonathan Peyton   kmp_int64 st;
431493e17cfeSJonathan Peyton   kmp_uint64 ub_glob;
431593e17cfeSJonathan Peyton   kmp_uint64 num_tasks;
431693e17cfeSJonathan Peyton   kmp_uint64 grainsize;
431793e17cfeSJonathan Peyton   kmp_uint64 extras;
431893e17cfeSJonathan Peyton   kmp_uint64 tc;
431993e17cfeSJonathan Peyton   kmp_uint64 num_t_min;
4320cdaefac5SJoachim Protze #if OMPT_SUPPORT
4321cdaefac5SJoachim Protze   void *codeptr_ra;
4322cdaefac5SJoachim Protze #endif
432393e17cfeSJonathan Peyton } __taskloop_params_t;
432493e17cfeSJonathan Peyton 
432593e17cfeSJonathan Peyton void __kmp_taskloop_recur(ident_t *, int, kmp_task_t *, kmp_uint64 *,
432693e17cfeSJonathan Peyton                           kmp_uint64 *, kmp_int64, kmp_uint64, kmp_uint64,
432793e17cfeSJonathan Peyton                           kmp_uint64, kmp_uint64, kmp_uint64, kmp_uint64,
4328cdaefac5SJoachim Protze #if OMPT_SUPPORT
4329cdaefac5SJoachim Protze                           void *,
4330cdaefac5SJoachim Protze #endif
433193e17cfeSJonathan Peyton                           void *);
433293e17cfeSJonathan Peyton 
433393e17cfeSJonathan Peyton // Execute part of the the taskloop submitted as a task.
433493e17cfeSJonathan Peyton int __kmp_taskloop_task(int gtid, void *ptask) {
4335d74d8902SJonathan Peyton   __taskloop_params_t *p =
4336d74d8902SJonathan Peyton       (__taskloop_params_t *)((kmp_task_t *)ptask)->shareds;
433793e17cfeSJonathan Peyton   kmp_task_t *task = p->task;
433893e17cfeSJonathan Peyton   kmp_uint64 *lb = p->lb;
433993e17cfeSJonathan Peyton   kmp_uint64 *ub = p->ub;
434093e17cfeSJonathan Peyton   void *task_dup = p->task_dup;
434193e17cfeSJonathan Peyton   //  p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
434293e17cfeSJonathan Peyton   kmp_int64 st = p->st;
434393e17cfeSJonathan Peyton   kmp_uint64 ub_glob = p->ub_glob;
434493e17cfeSJonathan Peyton   kmp_uint64 num_tasks = p->num_tasks;
434593e17cfeSJonathan Peyton   kmp_uint64 grainsize = p->grainsize;
434693e17cfeSJonathan Peyton   kmp_uint64 extras = p->extras;
434793e17cfeSJonathan Peyton   kmp_uint64 tc = p->tc;
434893e17cfeSJonathan Peyton   kmp_uint64 num_t_min = p->num_t_min;
4349cdaefac5SJoachim Protze #if OMPT_SUPPORT
4350cdaefac5SJoachim Protze   void *codeptr_ra = p->codeptr_ra;
4351cdaefac5SJoachim Protze #endif
435293e17cfeSJonathan Peyton #if KMP_DEBUG
435393e17cfeSJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
435493e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(task != NULL);
435593e17cfeSJonathan Peyton   KA_TRACE(20, ("__kmp_taskloop_task: T#%d, task %p: %lld tasks, grainsize"
4356d74d8902SJonathan Peyton                 " %lld, extras %lld, i=%lld,%lld(%d), dup %p\n",
4357d74d8902SJonathan Peyton                 gtid, taskdata, num_tasks, grainsize, extras, *lb, *ub, st,
4358d74d8902SJonathan Peyton                 task_dup));
435993e17cfeSJonathan Peyton #endif
436093e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks * 2 + 1 > num_t_min);
436193e17cfeSJonathan Peyton   if (num_tasks > num_t_min)
436293e17cfeSJonathan Peyton     __kmp_taskloop_recur(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
4363cdaefac5SJoachim Protze                          grainsize, extras, tc, num_t_min,
4364cdaefac5SJoachim Protze #if OMPT_SUPPORT
4365cdaefac5SJoachim Protze                          codeptr_ra,
4366cdaefac5SJoachim Protze #endif
4367cdaefac5SJoachim Protze                          task_dup);
436893e17cfeSJonathan Peyton   else
436993e17cfeSJonathan Peyton     __kmp_taskloop_linear(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
4370cdaefac5SJoachim Protze                           grainsize, extras, tc,
4371cdaefac5SJoachim Protze #if OMPT_SUPPORT
4372cdaefac5SJoachim Protze                           codeptr_ra,
4373cdaefac5SJoachim Protze #endif
4374cdaefac5SJoachim Protze                           task_dup);
437593e17cfeSJonathan Peyton 
437693e17cfeSJonathan Peyton   KA_TRACE(40, ("__kmp_taskloop_task(exit): T#%d\n", gtid));
437793e17cfeSJonathan Peyton   return 0;
437893e17cfeSJonathan Peyton }
437993e17cfeSJonathan Peyton 
438093e17cfeSJonathan Peyton // Schedule part of the the taskloop as a task,
438193e17cfeSJonathan Peyton // execute the rest of the the taskloop.
438293e17cfeSJonathan Peyton //
438393e17cfeSJonathan Peyton // loc        Source location information
438493e17cfeSJonathan Peyton // gtid       Global thread ID
438593e17cfeSJonathan Peyton // task       Pattern task, exposes the loop iteration range
438693e17cfeSJonathan Peyton // lb         Pointer to loop lower bound in task structure
438793e17cfeSJonathan Peyton // ub         Pointer to loop upper bound in task structure
438893e17cfeSJonathan Peyton // st         Loop stride
438993e17cfeSJonathan Peyton // ub_glob    Global upper bound (used for lastprivate check)
439093e17cfeSJonathan Peyton // num_tasks  Number of tasks to execute
439193e17cfeSJonathan Peyton // grainsize  Number of loop iterations per task
439293e17cfeSJonathan Peyton // extras     Number of chunks with grainsize+1 iterations
439393e17cfeSJonathan Peyton // tc         Iterations count
439493e17cfeSJonathan Peyton // num_t_min  Threashold to launch tasks recursively
439593e17cfeSJonathan Peyton // task_dup   Tasks duplication routine
4396cdaefac5SJoachim Protze // codeptr_ra Return address for OMPT events
439793e17cfeSJonathan Peyton void __kmp_taskloop_recur(ident_t *loc, int gtid, kmp_task_t *task,
439893e17cfeSJonathan Peyton                           kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
439993e17cfeSJonathan Peyton                           kmp_uint64 ub_glob, kmp_uint64 num_tasks,
440093e17cfeSJonathan Peyton                           kmp_uint64 grainsize, kmp_uint64 extras,
4401cdaefac5SJoachim Protze                           kmp_uint64 tc, kmp_uint64 num_t_min,
4402cdaefac5SJoachim Protze #if OMPT_SUPPORT
4403cdaefac5SJoachim Protze                           void *codeptr_ra,
4404cdaefac5SJoachim Protze #endif
4405cdaefac5SJoachim Protze                           void *task_dup) {
440693e17cfeSJonathan Peyton #if KMP_DEBUG
440793e17cfeSJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
440893e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(task != NULL);
440993e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks > num_t_min);
441093e17cfeSJonathan Peyton   KA_TRACE(20, ("__kmp_taskloop_recur: T#%d, task %p: %lld tasks, grainsize"
4411d74d8902SJonathan Peyton                 " %lld, extras %lld, i=%lld,%lld(%d), dup %p\n",
4412d74d8902SJonathan Peyton                 gtid, taskdata, num_tasks, grainsize, extras, *lb, *ub, st,
4413d74d8902SJonathan Peyton                 task_dup));
441493e17cfeSJonathan Peyton #endif
441593e17cfeSJonathan Peyton   p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
441693e17cfeSJonathan Peyton   kmp_uint64 lower = *lb;
441793e17cfeSJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
441893e17cfeSJonathan Peyton   //  kmp_taskdata_t *current_task = thread->th.th_current_task;
441993e17cfeSJonathan Peyton   kmp_task_t *next_task;
442093e17cfeSJonathan Peyton   size_t lower_offset =
442193e17cfeSJonathan Peyton       (char *)lb - (char *)task; // remember offset of lb in the task structure
442293e17cfeSJonathan Peyton   size_t upper_offset =
442393e17cfeSJonathan Peyton       (char *)ub - (char *)task; // remember offset of ub in the task structure
442493e17cfeSJonathan Peyton 
442593e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras);
442693e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks > extras);
442793e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks > 0);
442893e17cfeSJonathan Peyton 
442993e17cfeSJonathan Peyton   // split the loop in two halves
443093e17cfeSJonathan Peyton   kmp_uint64 lb1, ub0, tc0, tc1, ext0, ext1;
443193e17cfeSJonathan Peyton   kmp_uint64 gr_size0 = grainsize;
443293e17cfeSJonathan Peyton   kmp_uint64 n_tsk0 = num_tasks >> 1; // num_tasks/2 to execute
443393e17cfeSJonathan Peyton   kmp_uint64 n_tsk1 = num_tasks - n_tsk0; // to schedule as a task
443493e17cfeSJonathan Peyton   if (n_tsk0 <= extras) {
443593e17cfeSJonathan Peyton     gr_size0++; // integrate extras into grainsize
443693e17cfeSJonathan Peyton     ext0 = 0; // no extra iters in 1st half
443793e17cfeSJonathan Peyton     ext1 = extras - n_tsk0; // remaining extras
443893e17cfeSJonathan Peyton     tc0 = gr_size0 * n_tsk0;
443993e17cfeSJonathan Peyton     tc1 = tc - tc0;
444093e17cfeSJonathan Peyton   } else { // n_tsk0 > extras
444193e17cfeSJonathan Peyton     ext1 = 0; // no extra iters in 2nd half
444293e17cfeSJonathan Peyton     ext0 = extras;
444393e17cfeSJonathan Peyton     tc1 = grainsize * n_tsk1;
444493e17cfeSJonathan Peyton     tc0 = tc - tc1;
444593e17cfeSJonathan Peyton   }
444693e17cfeSJonathan Peyton   ub0 = lower + st * (tc0 - 1);
444793e17cfeSJonathan Peyton   lb1 = ub0 + st;
444893e17cfeSJonathan Peyton 
444993e17cfeSJonathan Peyton   // create pattern task for 2nd half of the loop
445093e17cfeSJonathan Peyton   next_task = __kmp_task_dup_alloc(thread, task); // duplicate the task
445193e17cfeSJonathan Peyton   // adjust lower bound (upper bound is not changed) for the 2nd half
445293e17cfeSJonathan Peyton   *(kmp_uint64 *)((char *)next_task + lower_offset) = lb1;
445393e17cfeSJonathan Peyton   if (ptask_dup != NULL) // construct fistprivates, etc.
445493e17cfeSJonathan Peyton     ptask_dup(next_task, task, 0);
445593e17cfeSJonathan Peyton   *ub = ub0; // adjust upper bound for the 1st half
445693e17cfeSJonathan Peyton 
445793e17cfeSJonathan Peyton   // create auxiliary task for 2nd half of the loop
445893e17cfeSJonathan Peyton   kmp_task_t *new_task =
445993e17cfeSJonathan Peyton       __kmpc_omp_task_alloc(loc, gtid, 1, 3 * sizeof(void *),
446093e17cfeSJonathan Peyton                             sizeof(__taskloop_params_t), &__kmp_taskloop_task);
446193e17cfeSJonathan Peyton   __taskloop_params_t *p = (__taskloop_params_t *)new_task->shareds;
446293e17cfeSJonathan Peyton   p->task = next_task;
446393e17cfeSJonathan Peyton   p->lb = (kmp_uint64 *)((char *)next_task + lower_offset);
446493e17cfeSJonathan Peyton   p->ub = (kmp_uint64 *)((char *)next_task + upper_offset);
446593e17cfeSJonathan Peyton   p->task_dup = task_dup;
446693e17cfeSJonathan Peyton   p->st = st;
446793e17cfeSJonathan Peyton   p->ub_glob = ub_glob;
446893e17cfeSJonathan Peyton   p->num_tasks = n_tsk1;
446993e17cfeSJonathan Peyton   p->grainsize = grainsize;
447093e17cfeSJonathan Peyton   p->extras = ext1;
447193e17cfeSJonathan Peyton   p->tc = tc1;
447293e17cfeSJonathan Peyton   p->num_t_min = num_t_min;
4473cdaefac5SJoachim Protze #if OMPT_SUPPORT
4474cdaefac5SJoachim Protze   p->codeptr_ra = codeptr_ra;
4475cdaefac5SJoachim Protze #endif
4476cdaefac5SJoachim Protze 
4477cdaefac5SJoachim Protze #if OMPT_SUPPORT
4478cdaefac5SJoachim Protze   // schedule new task with correct return address for OMPT events
4479cdaefac5SJoachim Protze   __kmp_omp_taskloop_task(NULL, gtid, new_task, codeptr_ra);
4480cdaefac5SJoachim Protze #else
4481f729df82SGheorghe-Teodor Bercea   __kmp_omp_task(gtid, new_task, true); // schedule new task
4482cdaefac5SJoachim Protze #endif
448393e17cfeSJonathan Peyton 
448493e17cfeSJonathan Peyton   // execute the 1st half of current subrange
448593e17cfeSJonathan Peyton   if (n_tsk0 > num_t_min)
4486d74d8902SJonathan Peyton     __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0, gr_size0,
4487cdaefac5SJoachim Protze                          ext0, tc0, num_t_min,
4488cdaefac5SJoachim Protze #if OMPT_SUPPORT
4489cdaefac5SJoachim Protze                          codeptr_ra,
4490cdaefac5SJoachim Protze #endif
4491cdaefac5SJoachim Protze                          task_dup);
449293e17cfeSJonathan Peyton   else
449393e17cfeSJonathan Peyton     __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0,
4494cdaefac5SJoachim Protze                           gr_size0, ext0, tc0,
4495cdaefac5SJoachim Protze #if OMPT_SUPPORT
4496cdaefac5SJoachim Protze                           codeptr_ra,
4497cdaefac5SJoachim Protze #endif
4498cdaefac5SJoachim Protze                           task_dup);
449993e17cfeSJonathan Peyton 
450093e17cfeSJonathan Peyton   KA_TRACE(40, ("__kmpc_taskloop_recur(exit): T#%d\n", gtid));
450193e17cfeSJonathan Peyton }
450293e17cfeSJonathan Peyton 
450393e17cfeSJonathan Peyton /*!
450493e17cfeSJonathan Peyton @ingroup TASKING
450593e17cfeSJonathan Peyton @param loc       Source location information
450693e17cfeSJonathan Peyton @param gtid      Global thread ID
450793e17cfeSJonathan Peyton @param task      Task structure
450893e17cfeSJonathan Peyton @param if_val    Value of the if clause
450993e17cfeSJonathan Peyton @param lb        Pointer to loop lower bound in task structure
451093e17cfeSJonathan Peyton @param ub        Pointer to loop upper bound in task structure
451193e17cfeSJonathan Peyton @param st        Loop stride
4512b7a8ab34SAndrey Churbanov @param nogroup   Flag, 1 if no taskgroup needs to be added, 0 otherwise
451393e17cfeSJonathan Peyton @param sched     Schedule specified 0/1/2 for none/grainsize/num_tasks
451493e17cfeSJonathan Peyton @param grainsize Schedule value if specified
451593e17cfeSJonathan Peyton @param task_dup  Tasks duplication routine
451693e17cfeSJonathan Peyton 
451793e17cfeSJonathan Peyton Execute the taskloop construct.
451893e17cfeSJonathan Peyton */
451993e17cfeSJonathan Peyton void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int if_val,
452093e17cfeSJonathan Peyton                      kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup,
452193e17cfeSJonathan Peyton                      int sched, kmp_uint64 grainsize, void *task_dup) {
452293e17cfeSJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
452393e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(task != NULL);
452493e17cfeSJonathan Peyton 
452582e94a59SJoachim Protze   if (nogroup == 0) {
452682e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
452782e94a59SJoachim Protze     OMPT_STORE_RETURN_ADDRESS(gtid);
452882e94a59SJoachim Protze #endif
452993e17cfeSJonathan Peyton     __kmpc_taskgroup(loc, gtid);
453082e94a59SJoachim Protze   }
453193e17cfeSJonathan Peyton 
453293e17cfeSJonathan Peyton   // =========================================================================
453393e17cfeSJonathan Peyton   // calculate loop parameters
453427a677fcSJonathan Peyton   kmp_taskloop_bounds_t task_bounds(task, lb, ub);
453593e17cfeSJonathan Peyton   kmp_uint64 tc;
453627a677fcSJonathan Peyton   // compiler provides global bounds here
453727a677fcSJonathan Peyton   kmp_uint64 lower = task_bounds.get_lb();
453827a677fcSJonathan Peyton   kmp_uint64 upper = task_bounds.get_ub();
453993e17cfeSJonathan Peyton   kmp_uint64 ub_glob = upper; // global upper used to calc lastprivate flag
454093e17cfeSJonathan Peyton   kmp_uint64 num_tasks = 0, extras = 0;
454193e17cfeSJonathan Peyton   kmp_uint64 num_tasks_min = __kmp_taskloop_min_tasks;
454293e17cfeSJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
454393e17cfeSJonathan Peyton   kmp_taskdata_t *current_task = thread->th.th_current_task;
454493e17cfeSJonathan Peyton 
454527a677fcSJonathan Peyton   KA_TRACE(20, ("__kmpc_taskloop: T#%d, task %p, lb %lld, ub %lld, st %lld, "
454627a677fcSJonathan Peyton                 "grain %llu(%d), dup %p\n",
454727a677fcSJonathan Peyton                 gtid, taskdata, lower, upper, st, grainsize, sched, task_dup));
454827a677fcSJonathan Peyton 
45497cc577a4SJonathan Peyton   // compute trip count
45507cc577a4SJonathan Peyton   if (st == 1) { // most common case
45517cc577a4SJonathan Peyton     tc = upper - lower + 1;
45527cc577a4SJonathan Peyton   } else if (st < 0) {
45537cc577a4SJonathan Peyton     tc = (lower - upper) / (-st) + 1;
45547cc577a4SJonathan Peyton   } else { // st > 0
45557cc577a4SJonathan Peyton     tc = (upper - lower) / st + 1;
45567cc577a4SJonathan Peyton   }
45577cc577a4SJonathan Peyton   if (tc == 0) {
45587cc577a4SJonathan Peyton     KA_TRACE(20, ("__kmpc_taskloop(exit): T#%d zero-trip loop\n", gtid));
45597cc577a4SJonathan Peyton     // free the pattern task and exit
45607cc577a4SJonathan Peyton     __kmp_task_start(gtid, task, current_task);
45617cc577a4SJonathan Peyton     // do not execute anything for zero-trip loop
456286ed6aa6SJoachim Protze     __kmp_task_finish<false>(gtid, task, current_task);
45637cc577a4SJonathan Peyton     return;
45647cc577a4SJonathan Peyton   }
4565cdaefac5SJoachim Protze 
4566cdaefac5SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
4567cdaefac5SJoachim Protze   ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
4568cdaefac5SJoachim Protze   ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
4569cdaefac5SJoachim Protze   if (ompt_enabled.ompt_callback_work) {
4570cdaefac5SJoachim Protze     ompt_callbacks.ompt_callback(ompt_callback_work)(
4571cdaefac5SJoachim Protze         ompt_work_taskloop, ompt_scope_begin, &(team_info->parallel_data),
4572cdaefac5SJoachim Protze         &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
4573cdaefac5SJoachim Protze   }
4574cdaefac5SJoachim Protze #endif
4575cdaefac5SJoachim Protze 
457693e17cfeSJonathan Peyton   if (num_tasks_min == 0)
457793e17cfeSJonathan Peyton     // TODO: can we choose better default heuristic?
4578d74d8902SJonathan Peyton     num_tasks_min =
4579d74d8902SJonathan Peyton         KMP_MIN(thread->th.th_team_nproc * 10, INITIAL_TASK_DEQUE_SIZE);
45807cc577a4SJonathan Peyton 
45817cc577a4SJonathan Peyton   // compute num_tasks/grainsize based on the input provided
45827cc577a4SJonathan Peyton   switch (sched) {
45837cc577a4SJonathan Peyton   case 0: // no schedule clause specified, we can choose the default
45847cc577a4SJonathan Peyton     // let's try to schedule (team_size*10) tasks
45857cc577a4SJonathan Peyton     grainsize = thread->th.th_team_nproc * 10;
45860c599c38SJoachim Protze     KMP_FALLTHROUGH();
45877cc577a4SJonathan Peyton   case 2: // num_tasks provided
45887cc577a4SJonathan Peyton     if (grainsize > tc) {
45897cc577a4SJonathan Peyton       num_tasks = tc; // too big num_tasks requested, adjust values
45907cc577a4SJonathan Peyton       grainsize = 1;
45917cc577a4SJonathan Peyton       extras = 0;
45927cc577a4SJonathan Peyton     } else {
45937cc577a4SJonathan Peyton       num_tasks = grainsize;
45947cc577a4SJonathan Peyton       grainsize = tc / num_tasks;
45957cc577a4SJonathan Peyton       extras = tc % num_tasks;
45967cc577a4SJonathan Peyton     }
45977cc577a4SJonathan Peyton     break;
45987cc577a4SJonathan Peyton   case 1: // grainsize provided
45997cc577a4SJonathan Peyton     if (grainsize > tc) {
46007cc577a4SJonathan Peyton       num_tasks = 1; // too big grainsize requested, adjust values
46017cc577a4SJonathan Peyton       grainsize = tc;
46027cc577a4SJonathan Peyton       extras = 0;
46037cc577a4SJonathan Peyton     } else {
46047cc577a4SJonathan Peyton       num_tasks = tc / grainsize;
460593e17cfeSJonathan Peyton       // adjust grainsize for balanced distribution of iterations
460693e17cfeSJonathan Peyton       grainsize = tc / num_tasks;
46077cc577a4SJonathan Peyton       extras = tc % num_tasks;
46087cc577a4SJonathan Peyton     }
46097cc577a4SJonathan Peyton     break;
46107cc577a4SJonathan Peyton   default:
46117cc577a4SJonathan Peyton     KMP_ASSERT2(0, "unknown scheduling of taskloop");
46127cc577a4SJonathan Peyton   }
46137cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras);
46147cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks > extras);
46157cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks > 0);
461693e17cfeSJonathan Peyton   // =========================================================================
46177cc577a4SJonathan Peyton 
46187cc577a4SJonathan Peyton   // check if clause value first
461927a677fcSJonathan Peyton   // Also require GOMP_taskloop to reduce to linear (taskdata->td_flags.native)
46207cc577a4SJonathan Peyton   if (if_val == 0) { // if(0) specified, mark task as serial
46217cc577a4SJonathan Peyton     taskdata->td_flags.task_serial = 1;
46227cc577a4SJonathan Peyton     taskdata->td_flags.tiedness = TASK_TIED; // AC: serial task cannot be untied
462393e17cfeSJonathan Peyton     // always start serial tasks linearly
462493e17cfeSJonathan Peyton     __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4625cdaefac5SJoachim Protze                           grainsize, extras, tc,
4626cdaefac5SJoachim Protze #if OMPT_SUPPORT
4627cdaefac5SJoachim Protze                           OMPT_GET_RETURN_ADDRESS(0),
4628cdaefac5SJoachim Protze #endif
4629cdaefac5SJoachim Protze                           task_dup);
463027a677fcSJonathan Peyton     // !taskdata->td_flags.native => currently force linear spawning of tasks
463127a677fcSJonathan Peyton     // for GOMP_taskloop
463227a677fcSJonathan Peyton   } else if (num_tasks > num_tasks_min && !taskdata->td_flags.native) {
463393e17cfeSJonathan Peyton     KA_TRACE(20, ("__kmpc_taskloop: T#%d, go recursive: tc %llu, #tasks %llu"
4634d74d8902SJonathan Peyton                   "(%lld), grain %llu, extras %llu\n",
4635d74d8902SJonathan Peyton                   gtid, tc, num_tasks, num_tasks_min, grainsize, extras));
463693e17cfeSJonathan Peyton     __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4637cdaefac5SJoachim Protze                          grainsize, extras, tc, num_tasks_min,
4638cdaefac5SJoachim Protze #if OMPT_SUPPORT
4639cdaefac5SJoachim Protze                          OMPT_GET_RETURN_ADDRESS(0),
4640cdaefac5SJoachim Protze #endif
4641cdaefac5SJoachim Protze                          task_dup);
464293e17cfeSJonathan Peyton   } else {
464393e17cfeSJonathan Peyton     KA_TRACE(20, ("__kmpc_taskloop: T#%d, go linear: tc %llu, #tasks %llu"
4644d74d8902SJonathan Peyton                   "(%lld), grain %llu, extras %llu\n",
4645d74d8902SJonathan Peyton                   gtid, tc, num_tasks, num_tasks_min, grainsize, extras));
464693e17cfeSJonathan Peyton     __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4647cdaefac5SJoachim Protze                           grainsize, extras, tc,
4648cdaefac5SJoachim Protze #if OMPT_SUPPORT
4649cdaefac5SJoachim Protze                           OMPT_GET_RETURN_ADDRESS(0),
4650cdaefac5SJoachim Protze #endif
4651cdaefac5SJoachim Protze                           task_dup);
46527cc577a4SJonathan Peyton   }
46537cc577a4SJonathan Peyton 
4654cdaefac5SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
4655cdaefac5SJoachim Protze   if (ompt_enabled.ompt_callback_work) {
4656cdaefac5SJoachim Protze     ompt_callbacks.ompt_callback(ompt_callback_work)(
4657cdaefac5SJoachim Protze         ompt_work_taskloop, ompt_scope_end, &(team_info->parallel_data),
4658cdaefac5SJoachim Protze         &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
4659cdaefac5SJoachim Protze   }
4660cdaefac5SJoachim Protze #endif
4661cdaefac5SJoachim Protze 
466282e94a59SJoachim Protze   if (nogroup == 0) {
466382e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
466482e94a59SJoachim Protze     OMPT_STORE_RETURN_ADDRESS(gtid);
466582e94a59SJoachim Protze #endif
46667cc577a4SJonathan Peyton     __kmpc_end_taskgroup(loc, gtid);
466782e94a59SJoachim Protze   }
466893e17cfeSJonathan Peyton   KA_TRACE(20, ("__kmpc_taskloop(exit): T#%d\n", gtid));
46697cc577a4SJonathan Peyton }
46707cc577a4SJonathan Peyton 
46717cc577a4SJonathan Peyton #endif
4672