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)) {
377c3344345SAndrey Churbanov     if (__kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
378c3344345SAndrey Churbanov                               thread->th.th_current_task)) {
3793041982dSJonathan Peyton       KA_TRACE(20, ("__kmp_push_task: T#%d deque is full; returning "
3803041982dSJonathan Peyton                     "TASK_NOT_PUSHED for task %p\n",
3817cc577a4SJonathan Peyton                     gtid, taskdata));
3827cc577a4SJonathan Peyton       return TASK_NOT_PUSHED;
383c3344345SAndrey Churbanov     } else {
3847cc577a4SJonathan Peyton       __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
385c3344345SAndrey Churbanov       locked = 1;
386c3344345SAndrey Churbanov       // expand deque to push the task which is not allowed to execute
387c3344345SAndrey Churbanov       __kmp_realloc_task_deque(thread, thread_data);
388c3344345SAndrey Churbanov     }
389c3344345SAndrey Churbanov   }
390c3344345SAndrey Churbanov   // Lock the deque for the task push operation
391c3344345SAndrey Churbanov   if (!locked) {
392c3344345SAndrey Churbanov     __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3937cc577a4SJonathan Peyton #if OMP_45_ENABLED
394c3344345SAndrey Churbanov     // Need to recheck as we can get a proxy task from thread outside of OpenMP
3953041982dSJonathan Peyton     if (TCR_4(thread_data->td.td_deque_ntasks) >=
3963041982dSJonathan Peyton         TASK_DEQUE_SIZE(thread_data->td)) {
397c3344345SAndrey Churbanov       if (__kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
398c3344345SAndrey Churbanov                                 thread->th.th_current_task)) {
3997cc577a4SJonathan Peyton         __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
400c3344345SAndrey Churbanov         KA_TRACE(20, ("__kmp_push_task: T#%d deque is full on 2nd check; "
401c3344345SAndrey Churbanov                       "returning TASK_NOT_PUSHED for task %p\n",
4027cc577a4SJonathan Peyton                       gtid, taskdata));
4037cc577a4SJonathan Peyton         return TASK_NOT_PUSHED;
404c3344345SAndrey Churbanov       } else {
405c3344345SAndrey Churbanov         // expand deque to push the task which is not allowed to execute
406c3344345SAndrey Churbanov         __kmp_realloc_task_deque(thread, thread_data);
4077cc577a4SJonathan Peyton       }
408c3344345SAndrey Churbanov     }
409c3344345SAndrey Churbanov #endif
410c3344345SAndrey Churbanov   }
4117cc577a4SJonathan Peyton   // Must have room since no thread can add tasks but calling thread
4123041982dSJonathan Peyton   KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) <
4133041982dSJonathan Peyton                    TASK_DEQUE_SIZE(thread_data->td));
4147cc577a4SJonathan Peyton 
4153041982dSJonathan Peyton   thread_data->td.td_deque[thread_data->td.td_deque_tail] =
4163041982dSJonathan Peyton       taskdata; // Push taskdata
4177cc577a4SJonathan Peyton   // Wrap index.
4183041982dSJonathan Peyton   thread_data->td.td_deque_tail =
4193041982dSJonathan Peyton       (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
4203041982dSJonathan Peyton   TCW_4(thread_data->td.td_deque_ntasks,
4213041982dSJonathan Peyton         TCR_4(thread_data->td.td_deque_ntasks) + 1); // Adjust task count
4227cc577a4SJonathan Peyton 
4237cc577a4SJonathan Peyton   KA_TRACE(20, ("__kmp_push_task: T#%d returning TASK_SUCCESSFULLY_PUSHED: "
4247cc577a4SJonathan Peyton                 "task=%p ntasks=%d head=%u tail=%u\n",
4257cc577a4SJonathan Peyton                 gtid, taskdata, thread_data->td.td_deque_ntasks,
4267cc577a4SJonathan Peyton                 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
4277cc577a4SJonathan Peyton 
4287cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
4297cc577a4SJonathan Peyton 
4307cc577a4SJonathan Peyton   return TASK_SUCCESSFULLY_PUSHED;
4317cc577a4SJonathan Peyton }
4327cc577a4SJonathan Peyton 
4333041982dSJonathan Peyton // __kmp_pop_current_task_from_thread: set up current task from called thread
4343041982dSJonathan Peyton // when team ends
4353041982dSJonathan Peyton //
4367cc577a4SJonathan Peyton // this_thr: thread structure to set current_task in.
4373041982dSJonathan Peyton void __kmp_pop_current_task_from_thread(kmp_info_t *this_thr) {
4383041982dSJonathan Peyton   KF_TRACE(10, ("__kmp_pop_current_task_from_thread(enter): T#%d "
4393041982dSJonathan Peyton                 "this_thread=%p, curtask=%p, "
4407cc577a4SJonathan Peyton                 "curtask_parent=%p\n",
4417cc577a4SJonathan Peyton                 0, this_thr, this_thr->th.th_current_task,
4427cc577a4SJonathan Peyton                 this_thr->th.th_current_task->td_parent));
4437cc577a4SJonathan Peyton 
4447cc577a4SJonathan Peyton   this_thr->th.th_current_task = this_thr->th.th_current_task->td_parent;
4457cc577a4SJonathan Peyton 
4463041982dSJonathan Peyton   KF_TRACE(10, ("__kmp_pop_current_task_from_thread(exit): T#%d "
4473041982dSJonathan Peyton                 "this_thread=%p, curtask=%p, "
4487cc577a4SJonathan Peyton                 "curtask_parent=%p\n",
4497cc577a4SJonathan Peyton                 0, this_thr, this_thr->th.th_current_task,
4507cc577a4SJonathan Peyton                 this_thr->th.th_current_task->td_parent));
4517cc577a4SJonathan Peyton }
4527cc577a4SJonathan Peyton 
4533041982dSJonathan Peyton // __kmp_push_current_task_to_thread: set up current task in called thread for a
4543041982dSJonathan Peyton // new team
4553041982dSJonathan Peyton //
4567cc577a4SJonathan Peyton // this_thr: thread structure to set up
4577cc577a4SJonathan Peyton // team: team for implicit task data
4587cc577a4SJonathan Peyton // tid: thread within team to set up
4593041982dSJonathan Peyton void __kmp_push_current_task_to_thread(kmp_info_t *this_thr, kmp_team_t *team,
4603041982dSJonathan Peyton                                        int tid) {
4613041982dSJonathan Peyton   // current task of the thread is a parent of the new just created implicit
4623041982dSJonathan Peyton   // tasks of new team
4633041982dSJonathan Peyton   KF_TRACE(10, ("__kmp_push_current_task_to_thread(enter): T#%d this_thread=%p "
4643041982dSJonathan Peyton                 "curtask=%p "
4657cc577a4SJonathan Peyton                 "parent_task=%p\n",
4667cc577a4SJonathan Peyton                 tid, this_thr, this_thr->th.th_current_task,
4677cc577a4SJonathan Peyton                 team->t.t_implicit_task_taskdata[tid].td_parent));
4687cc577a4SJonathan Peyton 
4697cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(this_thr != NULL);
4707cc577a4SJonathan Peyton 
4717cc577a4SJonathan Peyton   if (tid == 0) {
4727cc577a4SJonathan Peyton     if (this_thr->th.th_current_task != &team->t.t_implicit_task_taskdata[0]) {
4733041982dSJonathan Peyton       team->t.t_implicit_task_taskdata[0].td_parent =
4743041982dSJonathan Peyton           this_thr->th.th_current_task;
4757cc577a4SJonathan Peyton       this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[0];
4767cc577a4SJonathan Peyton     }
4777cc577a4SJonathan Peyton   } else {
4783041982dSJonathan Peyton     team->t.t_implicit_task_taskdata[tid].td_parent =
4793041982dSJonathan Peyton         team->t.t_implicit_task_taskdata[0].td_parent;
4807cc577a4SJonathan Peyton     this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[tid];
4817cc577a4SJonathan Peyton   }
4827cc577a4SJonathan Peyton 
4833041982dSJonathan Peyton   KF_TRACE(10, ("__kmp_push_current_task_to_thread(exit): T#%d this_thread=%p "
4843041982dSJonathan Peyton                 "curtask=%p "
4857cc577a4SJonathan Peyton                 "parent_task=%p\n",
4867cc577a4SJonathan Peyton                 tid, this_thr, this_thr->th.th_current_task,
4877cc577a4SJonathan Peyton                 team->t.t_implicit_task_taskdata[tid].td_parent));
4887cc577a4SJonathan Peyton }
4897cc577a4SJonathan Peyton 
4907cc577a4SJonathan Peyton // __kmp_task_start: bookkeeping for a task starting execution
4913041982dSJonathan Peyton //
4927cc577a4SJonathan Peyton // GTID: global thread id of calling thread
4937cc577a4SJonathan Peyton // task: task starting execution
4947cc577a4SJonathan Peyton // current_task: task suspending
4953041982dSJonathan Peyton static void __kmp_task_start(kmp_int32 gtid, kmp_task_t *task,
4963041982dSJonathan Peyton                              kmp_taskdata_t *current_task) {
4977cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4987cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
4997cc577a4SJonathan Peyton 
5003041982dSJonathan Peyton   KA_TRACE(10,
5013041982dSJonathan Peyton            ("__kmp_task_start(enter): T#%d starting task %p: current_task=%p\n",
5027cc577a4SJonathan Peyton             gtid, taskdata, current_task));
5037cc577a4SJonathan Peyton 
5047cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
5057cc577a4SJonathan Peyton 
5067cc577a4SJonathan Peyton   // mark currently executing task as suspended
5077cc577a4SJonathan Peyton   // TODO: GEH - make sure root team implicit task is initialized properly.
5087cc577a4SJonathan Peyton   // KMP_DEBUG_ASSERT( current_task -> td_flags.executing == 1 );
5097cc577a4SJonathan Peyton   current_task->td_flags.executing = 0;
5107cc577a4SJonathan Peyton 
5117cc577a4SJonathan Peyton // Add task to stack if tied
5127cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK
5133041982dSJonathan Peyton   if (taskdata->td_flags.tiedness == TASK_TIED) {
5147cc577a4SJonathan Peyton     __kmp_push_task_stack(gtid, thread, taskdata);
5157cc577a4SJonathan Peyton   }
5167cc577a4SJonathan Peyton #endif /* BUILD_TIED_TASK_STACK */
5177cc577a4SJonathan Peyton 
5187cc577a4SJonathan Peyton   // mark starting task as executing and as current task
5197cc577a4SJonathan Peyton   thread->th.th_current_task = taskdata;
5207cc577a4SJonathan Peyton 
5213041982dSJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.started == 0 ||
5223041982dSJonathan Peyton                    taskdata->td_flags.tiedness == TASK_UNTIED);
5233041982dSJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0 ||
5243041982dSJonathan Peyton                    taskdata->td_flags.tiedness == TASK_UNTIED);
5257cc577a4SJonathan Peyton   taskdata->td_flags.started = 1;
5267cc577a4SJonathan Peyton   taskdata->td_flags.executing = 1;
5277cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
5287cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
5297cc577a4SJonathan Peyton 
5307cc577a4SJonathan Peyton   // GEH TODO: shouldn't we pass some sort of location identifier here?
5317cc577a4SJonathan Peyton   // APT: yes, we will pass location here.
5327cc577a4SJonathan Peyton   // need to store current thread state (in a thread or taskdata structure)
5337cc577a4SJonathan Peyton   // before setting work_state, otherwise wrong state is set after end of task
5347cc577a4SJonathan Peyton 
5353041982dSJonathan Peyton   KA_TRACE(10, ("__kmp_task_start(exit): T#%d task=%p\n", gtid, taskdata));
5367cc577a4SJonathan Peyton 
5377cc577a4SJonathan Peyton   return;
5387cc577a4SJonathan Peyton }
5397cc577a4SJonathan Peyton 
54082e94a59SJoachim Protze #if OMPT_SUPPORT
54182e94a59SJoachim Protze //------------------------------------------------------------------------------
54282e94a59SJoachim Protze // __ompt_task_init:
54382e94a59SJoachim Protze //   Initialize OMPT fields maintained by a task. This will only be called after
54482e94a59SJoachim Protze //   ompt_start_tool, so we already know whether ompt is enabled or not.
54582e94a59SJoachim Protze 
54682e94a59SJoachim Protze static inline void __ompt_task_init(kmp_taskdata_t *task, int tid) {
54782e94a59SJoachim Protze   // The calls to __ompt_task_init already have the ompt_enabled condition.
54882e94a59SJoachim Protze   task->ompt_task_info.task_data.value = 0;
5490e0d6cddSJoachim Protze   task->ompt_task_info.frame.exit_frame = ompt_data_none;
5500e0d6cddSJoachim Protze   task->ompt_task_info.frame.enter_frame = ompt_data_none;
5510e0d6cddSJoachim Protze   task->ompt_task_info.frame.exit_frame_flags = ompt_frame_runtime | ompt_frame_framepointer;
5520e0d6cddSJoachim Protze   task->ompt_task_info.frame.enter_frame_flags = ompt_frame_runtime | ompt_frame_framepointer;
55382e94a59SJoachim Protze #if OMP_40_ENABLED
55482e94a59SJoachim Protze   task->ompt_task_info.ndeps = 0;
55582e94a59SJoachim Protze   task->ompt_task_info.deps = NULL;
55682e94a59SJoachim Protze #endif /* OMP_40_ENABLED */
55782e94a59SJoachim Protze }
55882e94a59SJoachim Protze 
55982e94a59SJoachim Protze // __ompt_task_start:
56082e94a59SJoachim Protze //   Build and trigger task-begin event
56182e94a59SJoachim Protze static inline void __ompt_task_start(kmp_task_t *task,
56282e94a59SJoachim Protze                                      kmp_taskdata_t *current_task,
56382e94a59SJoachim Protze                                      kmp_int32 gtid) {
56482e94a59SJoachim Protze   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
565489cdb78SJoachim Protze   ompt_task_status_t status = ompt_task_switch;
56682e94a59SJoachim Protze   if (__kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded) {
56782e94a59SJoachim Protze     status = ompt_task_yield;
56882e94a59SJoachim Protze     __kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded = 0;
56982e94a59SJoachim Protze   }
57082e94a59SJoachim Protze   /* let OMPT know that we're about to run this task */
57182e94a59SJoachim Protze   if (ompt_enabled.ompt_callback_task_schedule) {
57282e94a59SJoachim Protze     ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
57382e94a59SJoachim Protze         &(current_task->ompt_task_info.task_data), status,
57482e94a59SJoachim Protze         &(taskdata->ompt_task_info.task_data));
57582e94a59SJoachim Protze   }
57682e94a59SJoachim Protze   taskdata->ompt_task_info.scheduling_parent = current_task;
57782e94a59SJoachim Protze }
57882e94a59SJoachim Protze 
57982e94a59SJoachim Protze // __ompt_task_finish:
58082e94a59SJoachim Protze //   Build and trigger final task-schedule event
58186ed6aa6SJoachim Protze static inline void
58286ed6aa6SJoachim Protze __ompt_task_finish(kmp_task_t *task, kmp_taskdata_t *resumed_task,
58386ed6aa6SJoachim Protze                    ompt_task_status_t status = ompt_task_complete) {
58482e94a59SJoachim Protze   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
58582e94a59SJoachim Protze   if (__kmp_omp_cancellation && taskdata->td_taskgroup &&
58682e94a59SJoachim Protze       taskdata->td_taskgroup->cancel_request == cancel_taskgroup) {
58782e94a59SJoachim Protze     status = ompt_task_cancel;
58882e94a59SJoachim Protze   }
58982e94a59SJoachim Protze 
59082e94a59SJoachim Protze   /* let OMPT know that we're returning to the callee task */
59182e94a59SJoachim Protze   if (ompt_enabled.ompt_callback_task_schedule) {
59282e94a59SJoachim Protze     ompt_callbacks.ompt_callback(ompt_callback_task_schedule)(
59382e94a59SJoachim Protze         &(taskdata->ompt_task_info.task_data), status,
59482e94a59SJoachim Protze         &((resumed_task ? resumed_task
59582e94a59SJoachim Protze                         : (taskdata->ompt_task_info.scheduling_parent
59682e94a59SJoachim Protze                                ? taskdata->ompt_task_info.scheduling_parent
59782e94a59SJoachim Protze                                : taskdata->td_parent))
59882e94a59SJoachim Protze               ->ompt_task_info.task_data));
59982e94a59SJoachim Protze   }
60082e94a59SJoachim Protze }
60182e94a59SJoachim Protze #endif
60282e94a59SJoachim Protze 
60382e94a59SJoachim Protze template <bool ompt>
60482e94a59SJoachim Protze static void __kmpc_omp_task_begin_if0_template(ident_t *loc_ref, kmp_int32 gtid,
60582e94a59SJoachim Protze                                                kmp_task_t *task,
60682e94a59SJoachim Protze                                                void *frame_address,
60782e94a59SJoachim Protze                                                void *return_address) {
6087cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
6097cc577a4SJonathan Peyton   kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
6107cc577a4SJonathan Peyton 
6113041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_begin_if0(enter): T#%d loc=%p task=%p "
6123041982dSJonathan Peyton                 "current_task=%p\n",
6137cc577a4SJonathan Peyton                 gtid, loc_ref, taskdata, current_task));
6147cc577a4SJonathan Peyton 
6157cc577a4SJonathan Peyton   if (taskdata->td_flags.tiedness == TASK_UNTIED) {
6163041982dSJonathan Peyton     // untied task needs to increment counter so that the task structure is not
6173041982dSJonathan Peyton     // freed prematurely
61837e2ef54SJonathan Peyton     kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count);
619baad3f60SJonathan Peyton     KMP_DEBUG_USE_VAR(counter);
6203041982dSJonathan Peyton     KA_TRACE(20, ("__kmpc_omp_task_begin_if0: T#%d untied_count (%d) "
6213041982dSJonathan Peyton                   "incremented for task %p\n",
6227cc577a4SJonathan Peyton                   gtid, counter, taskdata));
6237cc577a4SJonathan Peyton   }
6247cc577a4SJonathan Peyton 
6253041982dSJonathan Peyton   taskdata->td_flags.task_serial =
6263041982dSJonathan Peyton       1; // Execute this task immediately, not deferred.
6277cc577a4SJonathan Peyton   __kmp_task_start(gtid, task, current_task);
6287cc577a4SJonathan Peyton 
62982e94a59SJoachim Protze #if OMPT_SUPPORT
63082e94a59SJoachim Protze   if (ompt) {
6310e0d6cddSJoachim Protze     if (current_task->ompt_task_info.frame.enter_frame.ptr == NULL) {
6320e0d6cddSJoachim Protze       current_task->ompt_task_info.frame.enter_frame.ptr =
6330e0d6cddSJoachim Protze           taskdata->ompt_task_info.frame.exit_frame.ptr = frame_address;
6340e0d6cddSJoachim Protze       current_task->ompt_task_info.frame.enter_frame_flags =
6350e0d6cddSJoachim Protze           taskdata->ompt_task_info.frame.exit_frame_flags = ompt_frame_application | ompt_frame_framepointer;
63682e94a59SJoachim Protze     }
63782e94a59SJoachim Protze     if (ompt_enabled.ompt_callback_task_create) {
63882e94a59SJoachim Protze       ompt_task_info_t *parent_info = &(current_task->ompt_task_info);
63982e94a59SJoachim Protze       ompt_callbacks.ompt_callback(ompt_callback_task_create)(
64082e94a59SJoachim Protze           &(parent_info->task_data), &(parent_info->frame),
64182e94a59SJoachim Protze           &(taskdata->ompt_task_info.task_data),
64282e94a59SJoachim Protze           ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(taskdata), 0,
64382e94a59SJoachim Protze           return_address);
64482e94a59SJoachim Protze     }
64582e94a59SJoachim Protze     __ompt_task_start(task, current_task, gtid);
64682e94a59SJoachim Protze   }
64782e94a59SJoachim Protze #endif // OMPT_SUPPORT
64882e94a59SJoachim Protze 
6493041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_begin_if0(exit): T#%d loc=%p task=%p,\n", gtid,
6503041982dSJonathan Peyton                 loc_ref, taskdata));
65182e94a59SJoachim Protze }
6527cc577a4SJonathan Peyton 
65382e94a59SJoachim Protze #if OMPT_SUPPORT
65482e94a59SJoachim Protze OMPT_NOINLINE
65582e94a59SJoachim Protze static void __kmpc_omp_task_begin_if0_ompt(ident_t *loc_ref, kmp_int32 gtid,
65682e94a59SJoachim Protze                                            kmp_task_t *task,
65782e94a59SJoachim Protze                                            void *frame_address,
65882e94a59SJoachim Protze                                            void *return_address) {
659f0a1c65fSJonas Hahnfeld   __kmpc_omp_task_begin_if0_template<true>(loc_ref, gtid, task, frame_address,
660f0a1c65fSJonas Hahnfeld                                            return_address);
66182e94a59SJoachim Protze }
66282e94a59SJoachim Protze #endif // OMPT_SUPPORT
66382e94a59SJoachim Protze 
66482e94a59SJoachim Protze // __kmpc_omp_task_begin_if0: report that a given serialized task has started
66582e94a59SJoachim Protze // execution
66682e94a59SJoachim Protze //
66782e94a59SJoachim Protze // loc_ref: source location information; points to beginning of task block.
66882e94a59SJoachim Protze // gtid: global thread number.
66982e94a59SJoachim Protze // task: task thunk for the started task.
67082e94a59SJoachim Protze void __kmpc_omp_task_begin_if0(ident_t *loc_ref, kmp_int32 gtid,
67182e94a59SJoachim Protze                                kmp_task_t *task) {
67282e94a59SJoachim Protze #if OMPT_SUPPORT
67382e94a59SJoachim Protze   if (UNLIKELY(ompt_enabled.enabled)) {
67482e94a59SJoachim Protze     OMPT_STORE_RETURN_ADDRESS(gtid);
67582e94a59SJoachim Protze     __kmpc_omp_task_begin_if0_ompt(loc_ref, gtid, task,
67682e94a59SJoachim Protze                                    OMPT_GET_FRAME_ADDRESS(1),
67782e94a59SJoachim Protze                                    OMPT_LOAD_RETURN_ADDRESS(gtid));
6787cc577a4SJonathan Peyton     return;
6797cc577a4SJonathan Peyton   }
68082e94a59SJoachim Protze #endif
681f0a1c65fSJonas Hahnfeld   __kmpc_omp_task_begin_if0_template<false>(loc_ref, gtid, task, NULL, NULL);
68282e94a59SJoachim Protze }
6837cc577a4SJonathan Peyton 
6847cc577a4SJonathan Peyton #ifdef TASK_UNUSED
6857cc577a4SJonathan Peyton // __kmpc_omp_task_begin: report that a given task has started execution
6867cc577a4SJonathan Peyton // NEVER GENERATED BY COMPILER, DEPRECATED!!!
6873041982dSJonathan Peyton void __kmpc_omp_task_begin(ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task) {
6887cc577a4SJonathan Peyton   kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
6897cc577a4SJonathan Peyton 
6903041982dSJonathan Peyton   KA_TRACE(
6913041982dSJonathan Peyton       10,
6923041982dSJonathan Peyton       ("__kmpc_omp_task_begin(enter): T#%d loc=%p task=%p current_task=%p\n",
6937cc577a4SJonathan Peyton        gtid, loc_ref, KMP_TASK_TO_TASKDATA(task), current_task));
6947cc577a4SJonathan Peyton 
6957cc577a4SJonathan Peyton   __kmp_task_start(gtid, task, current_task);
6967cc577a4SJonathan Peyton 
6973041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_begin(exit): T#%d loc=%p task=%p,\n", gtid,
6983041982dSJonathan Peyton                 loc_ref, KMP_TASK_TO_TASKDATA(task)));
6997cc577a4SJonathan Peyton   return;
7007cc577a4SJonathan Peyton }
7017cc577a4SJonathan Peyton #endif // TASK_UNUSED
7027cc577a4SJonathan Peyton 
7037cc577a4SJonathan Peyton // __kmp_free_task: free the current task space and the space for shareds
7043041982dSJonathan Peyton //
7057cc577a4SJonathan Peyton // gtid: Global thread ID of calling thread
7067cc577a4SJonathan Peyton // taskdata: task to free
7077cc577a4SJonathan Peyton // thread: thread data structure of caller
7083041982dSJonathan Peyton static void __kmp_free_task(kmp_int32 gtid, kmp_taskdata_t *taskdata,
7093041982dSJonathan Peyton                             kmp_info_t *thread) {
7103041982dSJonathan Peyton   KA_TRACE(30, ("__kmp_free_task: T#%d freeing data from task %p\n", gtid,
7113041982dSJonathan Peyton                 taskdata));
7127cc577a4SJonathan Peyton 
7137cc577a4SJonathan Peyton   // Check to make sure all flags and counters have the correct values
7147cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
7157cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0);
7167cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 1);
7177cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
71837e2ef54SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_allocated_child_tasks == 0 ||
7193041982dSJonathan Peyton                    taskdata->td_flags.task_serial == 1);
72037e2ef54SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_incomplete_child_tasks == 0);
7217cc577a4SJonathan Peyton 
7227cc577a4SJonathan Peyton   taskdata->td_flags.freed = 1;
7237cc577a4SJonathan Peyton   ANNOTATE_HAPPENS_BEFORE(taskdata);
7247cc577a4SJonathan Peyton // deallocate the taskdata and shared variable blocks associated with this task
7257cc577a4SJonathan Peyton #if USE_FAST_MEMORY
7267cc577a4SJonathan Peyton   __kmp_fast_free(thread, taskdata);
7277cc577a4SJonathan Peyton #else /* ! USE_FAST_MEMORY */
7287cc577a4SJonathan Peyton   __kmp_thread_free(thread, taskdata);
7297cc577a4SJonathan Peyton #endif
7307cc577a4SJonathan Peyton 
7313041982dSJonathan Peyton   KA_TRACE(20, ("__kmp_free_task: T#%d freed task %p\n", gtid, taskdata));
7327cc577a4SJonathan Peyton }
7337cc577a4SJonathan Peyton 
7343041982dSJonathan Peyton // __kmp_free_task_and_ancestors: free the current task and ancestors without
7353041982dSJonathan Peyton // children
7367cc577a4SJonathan Peyton //
7377cc577a4SJonathan Peyton // gtid: Global thread ID of calling thread
7387cc577a4SJonathan Peyton // taskdata: task to free
7397cc577a4SJonathan Peyton // thread: thread data structure of caller
7403041982dSJonathan Peyton static void __kmp_free_task_and_ancestors(kmp_int32 gtid,
7413041982dSJonathan Peyton                                           kmp_taskdata_t *taskdata,
7423041982dSJonathan Peyton                                           kmp_info_t *thread) {
7437cc577a4SJonathan Peyton #if OMP_45_ENABLED
7447cc577a4SJonathan Peyton   // Proxy tasks must always be allowed to free their parents
7457cc577a4SJonathan Peyton   // because they can be run in background even in serial mode.
7463041982dSJonathan Peyton   kmp_int32 team_serial =
7473041982dSJonathan Peyton       (taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) &&
7483041982dSJonathan Peyton       !taskdata->td_flags.proxy;
7497cc577a4SJonathan Peyton #else
7503041982dSJonathan Peyton   kmp_int32 team_serial =
7513041982dSJonathan Peyton       taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser;
7527cc577a4SJonathan Peyton #endif
7537cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
7547cc577a4SJonathan Peyton 
75537e2ef54SJonathan Peyton   kmp_int32 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
7567cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(children >= 0);
7577cc577a4SJonathan Peyton 
7587cc577a4SJonathan Peyton   // Now, go up the ancestor tree to see if any ancestors can now be freed.
7593041982dSJonathan Peyton   while (children == 0) {
7607cc577a4SJonathan Peyton     kmp_taskdata_t *parent_taskdata = taskdata->td_parent;
7617cc577a4SJonathan Peyton 
7627cc577a4SJonathan Peyton     KA_TRACE(20, ("__kmp_free_task_and_ancestors(enter): T#%d task %p complete "
7633041982dSJonathan Peyton                   "and freeing itself\n",
7643041982dSJonathan Peyton                   gtid, taskdata));
7657cc577a4SJonathan Peyton 
7667cc577a4SJonathan Peyton     // --- Deallocate my ancestor task ---
7677cc577a4SJonathan Peyton     __kmp_free_task(gtid, taskdata, thread);
7687cc577a4SJonathan Peyton 
7697cc577a4SJonathan Peyton     taskdata = parent_taskdata;
7707cc577a4SJonathan Peyton 
771c3344345SAndrey Churbanov     if (team_serial)
772c3344345SAndrey Churbanov       return;
7733041982dSJonathan Peyton     // Stop checking ancestors at implicit task instead of walking up ancestor
7743041982dSJonathan Peyton     // tree to avoid premature deallocation of ancestors.
775c3344345SAndrey Churbanov     if (taskdata->td_flags.tasktype == TASK_IMPLICIT) {
776c3344345SAndrey Churbanov       if (taskdata->td_dephash) { // do we need to cleanup dephash?
777c3344345SAndrey Churbanov         int children = KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks);
778c3344345SAndrey Churbanov         kmp_tasking_flags_t flags_old = taskdata->td_flags;
779c3344345SAndrey Churbanov         if (children == 0 && flags_old.complete == 1) {
780c3344345SAndrey Churbanov           kmp_tasking_flags_t flags_new = flags_old;
781c3344345SAndrey Churbanov           flags_new.complete = 0;
782c3344345SAndrey Churbanov           if (KMP_COMPARE_AND_STORE_ACQ32(
783c3344345SAndrey Churbanov                   RCAST(kmp_int32 *, &taskdata->td_flags),
784c3344345SAndrey Churbanov                   *RCAST(kmp_int32 *, &flags_old),
785c3344345SAndrey Churbanov                   *RCAST(kmp_int32 *, &flags_new))) {
786c3344345SAndrey Churbanov             KA_TRACE(100, ("__kmp_free_task_and_ancestors: T#%d cleans "
787c3344345SAndrey Churbanov                            "dephash of implicit task %p\n",
788c3344345SAndrey Churbanov                            gtid, taskdata));
789c3344345SAndrey Churbanov             // cleanup dephash of finished implicit task
790c3344345SAndrey Churbanov             __kmp_dephash_free_entries(thread, taskdata->td_dephash);
791c3344345SAndrey Churbanov           }
792c3344345SAndrey Churbanov         }
793c3344345SAndrey Churbanov       }
7947cc577a4SJonathan Peyton       return;
795c3344345SAndrey Churbanov     }
7967cc577a4SJonathan Peyton     // Predecrement simulated by "- 1" calculation
79737e2ef54SJonathan Peyton     children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1;
7987cc577a4SJonathan Peyton     KMP_DEBUG_ASSERT(children >= 0);
7997cc577a4SJonathan Peyton   }
8007cc577a4SJonathan Peyton 
8013041982dSJonathan Peyton   KA_TRACE(
8023041982dSJonathan Peyton       20, ("__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; "
8033041982dSJonathan Peyton            "not freeing it yet\n",
8043041982dSJonathan Peyton            gtid, taskdata, children));
8057cc577a4SJonathan Peyton }
8067cc577a4SJonathan Peyton 
8077cc577a4SJonathan Peyton // __kmp_task_finish: bookkeeping to do when a task finishes execution
8083041982dSJonathan Peyton //
8097cc577a4SJonathan Peyton // gtid: global thread ID for calling thread
8107cc577a4SJonathan Peyton // task: task to be finished
8117cc577a4SJonathan Peyton // resumed_task: task to be resumed.  (may be NULL if task is serialized)
81286ed6aa6SJoachim Protze template <bool ompt>
8133041982dSJonathan Peyton static void __kmp_task_finish(kmp_int32 gtid, kmp_task_t *task,
8143041982dSJonathan Peyton                               kmp_taskdata_t *resumed_task) {
8157cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
8167cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
817781a0896SRoman Lebedev #if OMP_45_ENABLED
8183041982dSJonathan Peyton   kmp_task_team_t *task_team =
8193041982dSJonathan Peyton       thread->th.th_task_team; // might be NULL for serial teams...
820781a0896SRoman Lebedev #endif // OMP_45_ENABLED
8217cc577a4SJonathan Peyton   kmp_int32 children = 0;
8227cc577a4SJonathan Peyton 
8233041982dSJonathan Peyton   KA_TRACE(10, ("__kmp_task_finish(enter): T#%d finishing task %p and resuming "
8243041982dSJonathan Peyton                 "task %p\n",
8257cc577a4SJonathan Peyton                 gtid, taskdata, resumed_task));
8267cc577a4SJonathan Peyton 
8277cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
8287cc577a4SJonathan Peyton 
8297cc577a4SJonathan Peyton // Pop task from stack if tied
8307cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK
8313041982dSJonathan Peyton   if (taskdata->td_flags.tiedness == TASK_TIED) {
8327cc577a4SJonathan Peyton     __kmp_pop_task_stack(gtid, thread, taskdata);
8337cc577a4SJonathan Peyton   }
8347cc577a4SJonathan Peyton #endif /* BUILD_TIED_TASK_STACK */
8357cc577a4SJonathan Peyton 
8367cc577a4SJonathan Peyton   if (taskdata->td_flags.tiedness == TASK_UNTIED) {
8373041982dSJonathan Peyton     // untied task needs to check the counter so that the task structure is not
8383041982dSJonathan Peyton     // freed prematurely
83937e2ef54SJonathan Peyton     kmp_int32 counter = KMP_ATOMIC_DEC(&taskdata->td_untied_count) - 1;
8403041982dSJonathan Peyton     KA_TRACE(
8413041982dSJonathan Peyton         20,
8423041982dSJonathan Peyton         ("__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n",
8437cc577a4SJonathan Peyton          gtid, counter, taskdata));
8447cc577a4SJonathan Peyton     if (counter > 0) {
8453041982dSJonathan Peyton       // untied task is not done, to be continued possibly by other thread, do
8463041982dSJonathan Peyton       // not free it now
8477cc577a4SJonathan Peyton       if (resumed_task == NULL) {
8487cc577a4SJonathan Peyton         KMP_DEBUG_ASSERT(taskdata->td_flags.task_serial);
8493041982dSJonathan Peyton         resumed_task = taskdata->td_parent; // In a serialized task, the resumed
8503041982dSJonathan Peyton         // task is the parent
8517cc577a4SJonathan Peyton       }
8527cc577a4SJonathan Peyton       thread->th.th_current_task = resumed_task; // restore current_task
8537cc577a4SJonathan Peyton       resumed_task->td_flags.executing = 1; // resume previous task
8543041982dSJonathan Peyton       KA_TRACE(10, ("__kmp_task_finish(exit): T#%d partially done task %p, "
8553041982dSJonathan Peyton                     "resuming task %p\n",
8567cc577a4SJonathan Peyton                     gtid, taskdata, resumed_task));
8577cc577a4SJonathan Peyton       return;
8587cc577a4SJonathan Peyton     }
8597cc577a4SJonathan Peyton   }
86086ed6aa6SJoachim Protze #if OMPT_SUPPORT
86186ed6aa6SJoachim Protze   if (ompt)
86286ed6aa6SJoachim Protze     __ompt_task_finish(task, resumed_task);
86386ed6aa6SJoachim Protze #endif
8647cc577a4SJonathan Peyton 
865c3344345SAndrey Churbanov   // Check mutexinoutset dependencies, release locks
866c3344345SAndrey Churbanov   kmp_depnode_t *node = taskdata->td_depnode;
867c3344345SAndrey Churbanov   if (node && (node->dn.mtx_num_locks < 0)) {
868c3344345SAndrey Churbanov     // negative num_locks means all locks were acquired
869c3344345SAndrey Churbanov     node->dn.mtx_num_locks = -node->dn.mtx_num_locks;
870c3344345SAndrey Churbanov     for (int i = node->dn.mtx_num_locks - 1; i >= 0; --i) {
871c3344345SAndrey Churbanov       KMP_DEBUG_ASSERT(node->dn.mtx_locks[i] != NULL);
872c3344345SAndrey Churbanov       __kmp_release_lock(node->dn.mtx_locks[i], gtid);
873c3344345SAndrey Churbanov     }
874c3344345SAndrey Churbanov   }
875c3344345SAndrey Churbanov 
8767cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
877*405037c4SAndrey Churbanov   bool detach = false;
878*405037c4SAndrey Churbanov #if OMP_50_ENABLED
879*405037c4SAndrey Churbanov   if (taskdata->td_flags.detachable == TASK_DETACHABLE) {
880*405037c4SAndrey Churbanov     if (taskdata->td_allow_completion_event.type ==
881*405037c4SAndrey Churbanov         KMP_EVENT_ALLOW_COMPLETION) {
882*405037c4SAndrey Churbanov       // event hasn't been fulfilled yet. Try to detach task.
883*405037c4SAndrey Churbanov       __kmp_acquire_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
884*405037c4SAndrey Churbanov       if (taskdata->td_allow_completion_event.type ==
885*405037c4SAndrey Churbanov           KMP_EVENT_ALLOW_COMPLETION) {
886*405037c4SAndrey Churbanov         taskdata->td_flags.proxy = TASK_PROXY; // proxify!
887*405037c4SAndrey Churbanov         detach = true;
888*405037c4SAndrey Churbanov       }
889*405037c4SAndrey Churbanov       __kmp_release_tas_lock(&taskdata->td_allow_completion_event.lock, gtid);
890*405037c4SAndrey Churbanov     }
891*405037c4SAndrey Churbanov   }
892*405037c4SAndrey Churbanov #endif
8937cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.started == 1);
8947cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
8957cc577a4SJonathan Peyton 
896*405037c4SAndrey Churbanov   if (!detach) {
897*405037c4SAndrey Churbanov     taskdata->td_flags.complete = 1; // mark the task as completed
898*405037c4SAndrey Churbanov 
8993041982dSJonathan Peyton     // Only need to keep track of count if team parallel and tasking not
9003041982dSJonathan Peyton     // serialized
9017cc577a4SJonathan Peyton     if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
9027cc577a4SJonathan Peyton       // Predecrement simulated by "- 1" calculation
9033041982dSJonathan Peyton       children =
90437e2ef54SJonathan Peyton           KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks) - 1;
9057cc577a4SJonathan Peyton       KMP_DEBUG_ASSERT(children >= 0);
9067cc577a4SJonathan Peyton #if OMP_40_ENABLED
9077cc577a4SJonathan Peyton       if (taskdata->td_taskgroup)
90837e2ef54SJonathan Peyton         KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
909cf27e31bSJonathan Peyton       __kmp_release_deps(gtid, taskdata);
9107cc577a4SJonathan Peyton #if OMP_45_ENABLED
911cf27e31bSJonathan Peyton     } else if (task_team && task_team->tt.tt_found_proxy_tasks) {
9127cc577a4SJonathan Peyton       // if we found proxy tasks there could exist a dependency chain
9137cc577a4SJonathan Peyton       // with the proxy task as origin
9147cc577a4SJonathan Peyton       __kmp_release_deps(gtid, taskdata);
915cf27e31bSJonathan Peyton #endif // OMP_45_ENABLED
916cf27e31bSJonathan Peyton #endif // OMP_40_ENABLED
9177cc577a4SJonathan Peyton     }
918*405037c4SAndrey Churbanov   }
9197cc577a4SJonathan Peyton 
9203041982dSJonathan Peyton   // td_flags.executing must be marked as 0 after __kmp_release_deps has been
9213041982dSJonathan Peyton   // called. Othertwise, if a task is executed immediately from the release_deps
9223041982dSJonathan Peyton   // code, the flag will be reset to 1 again by this same function
9237cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
9247cc577a4SJonathan Peyton   taskdata->td_flags.executing = 0; // suspend the finishing task
9257cc577a4SJonathan Peyton 
9263041982dSJonathan Peyton   KA_TRACE(
9273041982dSJonathan Peyton       20, ("__kmp_task_finish: T#%d finished task %p, %d incomplete children\n",
9287cc577a4SJonathan Peyton            gtid, taskdata, children));
9297cc577a4SJonathan Peyton 
9307cc577a4SJonathan Peyton #if OMP_40_ENABLED
9317cc577a4SJonathan Peyton   /* If the tasks' destructor thunk flag has been set, we need to invoke the
9323041982dSJonathan Peyton      destructor thunk that has been generated by the compiler. The code is
9333041982dSJonathan Peyton      placed here, since at this point other tasks might have been released
9347cc577a4SJonathan Peyton      hence overlapping the destructor invokations with some other work in the
9353041982dSJonathan Peyton      released tasks.  The OpenMP spec is not specific on when the destructors
9363041982dSJonathan Peyton      are invoked, so we should be free to choose. */
9377cc577a4SJonathan Peyton   if (taskdata->td_flags.destructors_thunk) {
9387cc577a4SJonathan Peyton     kmp_routine_entry_t destr_thunk = task->data1.destructors;
9397cc577a4SJonathan Peyton     KMP_ASSERT(destr_thunk);
9407cc577a4SJonathan Peyton     destr_thunk(gtid, task);
9417cc577a4SJonathan Peyton   }
9427cc577a4SJonathan Peyton #endif // OMP_40_ENABLED
9437cc577a4SJonathan Peyton 
9447cc577a4SJonathan Peyton   // bookkeeping for resuming task:
9457cc577a4SJonathan Peyton   // GEH - note tasking_ser => task_serial
9463041982dSJonathan Peyton   KMP_DEBUG_ASSERT(
9473041982dSJonathan Peyton       (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) ==
9487cc577a4SJonathan Peyton       taskdata->td_flags.task_serial);
9493041982dSJonathan Peyton   if (taskdata->td_flags.task_serial) {
9507cc577a4SJonathan Peyton     if (resumed_task == NULL) {
9513041982dSJonathan Peyton       resumed_task = taskdata->td_parent; // In a serialized task, the resumed
9523041982dSJonathan Peyton       // task is the parent
9537cc577a4SJonathan Peyton     }
9543041982dSJonathan Peyton   } else {
9553041982dSJonathan Peyton     KMP_DEBUG_ASSERT(resumed_task !=
9563041982dSJonathan Peyton                      NULL); // verify that resumed task is passed as arguemnt
9577cc577a4SJonathan Peyton   }
9587cc577a4SJonathan Peyton 
9597cc577a4SJonathan Peyton   // Free this task and then ancestor tasks if they have no children.
9607cc577a4SJonathan Peyton   // Restore th_current_task first as suggested by John:
9617cc577a4SJonathan Peyton   // johnmc: if an asynchronous inquiry peers into the runtime system
9627cc577a4SJonathan Peyton   // it doesn't see the freed task as the current task.
9637cc577a4SJonathan Peyton   thread->th.th_current_task = resumed_task;
964*405037c4SAndrey Churbanov   if (!detach)
9657cc577a4SJonathan Peyton     __kmp_free_task_and_ancestors(gtid, taskdata, thread);
9667cc577a4SJonathan Peyton 
9677cc577a4SJonathan Peyton   // TODO: GEH - make sure root team implicit task is initialized properly.
9687cc577a4SJonathan Peyton   // KMP_DEBUG_ASSERT( resumed_task->td_flags.executing == 0 );
9697cc577a4SJonathan Peyton   resumed_task->td_flags.executing = 1; // resume previous task
9707cc577a4SJonathan Peyton 
9713041982dSJonathan Peyton   KA_TRACE(
9723041982dSJonathan Peyton       10, ("__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n",
9737cc577a4SJonathan Peyton            gtid, taskdata, resumed_task));
9747cc577a4SJonathan Peyton 
9757cc577a4SJonathan Peyton   return;
9767cc577a4SJonathan Peyton }
9777cc577a4SJonathan Peyton 
97882e94a59SJoachim Protze template <bool ompt>
97982e94a59SJoachim Protze static void __kmpc_omp_task_complete_if0_template(ident_t *loc_ref,
98082e94a59SJoachim Protze                                                   kmp_int32 gtid,
9813041982dSJonathan Peyton                                                   kmp_task_t *task) {
9827cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n",
9837cc577a4SJonathan Peyton                 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
9843041982dSJonathan Peyton   // this routine will provide task to resume
98586ed6aa6SJoachim Protze   __kmp_task_finish<ompt>(gtid, task, NULL);
9867cc577a4SJonathan Peyton 
9877cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n",
9887cc577a4SJonathan Peyton                 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
98982e94a59SJoachim Protze 
99082e94a59SJoachim Protze #if OMPT_SUPPORT
99182e94a59SJoachim Protze   if (ompt) {
9920e0d6cddSJoachim Protze     ompt_frame_t *ompt_frame;
99382e94a59SJoachim Protze     __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
9940e0d6cddSJoachim Protze     ompt_frame->enter_frame = ompt_data_none;
9950e0d6cddSJoachim Protze     ompt_frame->enter_frame_flags = ompt_frame_runtime | ompt_frame_framepointer;
99682e94a59SJoachim Protze   }
99782e94a59SJoachim Protze #endif
99882e94a59SJoachim Protze 
9997cc577a4SJonathan Peyton   return;
10007cc577a4SJonathan Peyton }
10017cc577a4SJonathan Peyton 
100282e94a59SJoachim Protze #if OMPT_SUPPORT
100382e94a59SJoachim Protze OMPT_NOINLINE
100482e94a59SJoachim Protze void __kmpc_omp_task_complete_if0_ompt(ident_t *loc_ref, kmp_int32 gtid,
100582e94a59SJoachim Protze                                        kmp_task_t *task) {
100682e94a59SJoachim Protze   __kmpc_omp_task_complete_if0_template<true>(loc_ref, gtid, task);
100782e94a59SJoachim Protze }
100882e94a59SJoachim Protze #endif // OMPT_SUPPORT
100982e94a59SJoachim Protze 
101082e94a59SJoachim Protze // __kmpc_omp_task_complete_if0: report that a task has completed execution
101182e94a59SJoachim Protze //
101282e94a59SJoachim Protze // loc_ref: source location information; points to end of task block.
101382e94a59SJoachim Protze // gtid: global thread number.
101482e94a59SJoachim Protze // task: task thunk for the completed task.
101582e94a59SJoachim Protze void __kmpc_omp_task_complete_if0(ident_t *loc_ref, kmp_int32 gtid,
101682e94a59SJoachim Protze                                   kmp_task_t *task) {
101782e94a59SJoachim Protze #if OMPT_SUPPORT
101882e94a59SJoachim Protze   if (UNLIKELY(ompt_enabled.enabled)) {
101982e94a59SJoachim Protze     __kmpc_omp_task_complete_if0_ompt(loc_ref, gtid, task);
102082e94a59SJoachim Protze     return;
102182e94a59SJoachim Protze   }
102282e94a59SJoachim Protze #endif
102382e94a59SJoachim Protze   __kmpc_omp_task_complete_if0_template<false>(loc_ref, gtid, task);
102482e94a59SJoachim Protze }
102582e94a59SJoachim Protze 
10267cc577a4SJonathan Peyton #ifdef TASK_UNUSED
10277cc577a4SJonathan Peyton // __kmpc_omp_task_complete: report that a task has completed execution
10287cc577a4SJonathan Peyton // NEVER GENERATED BY COMPILER, DEPRECATED!!!
10293041982dSJonathan Peyton void __kmpc_omp_task_complete(ident_t *loc_ref, kmp_int32 gtid,
10303041982dSJonathan Peyton                               kmp_task_t *task) {
10313041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n", gtid,
10323041982dSJonathan Peyton                 loc_ref, KMP_TASK_TO_TASKDATA(task)));
10337cc577a4SJonathan Peyton 
103486ed6aa6SJoachim Protze   __kmp_task_finish<false>(gtid, task,
103586ed6aa6SJoachim Protze                            NULL); // Not sure how to find task to resume
10367cc577a4SJonathan Peyton 
10373041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n", gtid,
10383041982dSJonathan Peyton                 loc_ref, KMP_TASK_TO_TASKDATA(task)));
10397cc577a4SJonathan Peyton   return;
10407cc577a4SJonathan Peyton }
10417cc577a4SJonathan Peyton #endif // TASK_UNUSED
10427cc577a4SJonathan Peyton 
10433041982dSJonathan Peyton // __kmp_init_implicit_task: Initialize the appropriate fields in the implicit
10443041982dSJonathan Peyton // task for a given thread
10457cc577a4SJonathan Peyton //
10467cc577a4SJonathan Peyton // loc_ref:  reference to source location of parallel region
10477cc577a4SJonathan Peyton // this_thr:  thread data structure corresponding to implicit task
10487cc577a4SJonathan Peyton // team: team for this_thr
10497cc577a4SJonathan Peyton // tid: thread id of given thread within team
10507cc577a4SJonathan Peyton // set_curr_task: TRUE if need to push current task to thread
10513041982dSJonathan Peyton // NOTE: Routine does not set up the implicit task ICVS.  This is assumed to
10523041982dSJonathan Peyton // have already been done elsewhere.
10537cc577a4SJonathan Peyton // TODO: Get better loc_ref.  Value passed in may be NULL
10543041982dSJonathan Peyton void __kmp_init_implicit_task(ident_t *loc_ref, kmp_info_t *this_thr,
10553041982dSJonathan Peyton                               kmp_team_t *team, int tid, int set_curr_task) {
10567cc577a4SJonathan Peyton   kmp_taskdata_t *task = &team->t.t_implicit_task_taskdata[tid];
10577cc577a4SJonathan Peyton 
10583041982dSJonathan Peyton   KF_TRACE(
10593041982dSJonathan Peyton       10,
10603041982dSJonathan Peyton       ("__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n",
10617cc577a4SJonathan Peyton        tid, team, task, set_curr_task ? "TRUE" : "FALSE"));
10627cc577a4SJonathan Peyton 
10637cc577a4SJonathan Peyton   task->td_task_id = KMP_GEN_TASK_ID();
10647cc577a4SJonathan Peyton   task->td_team = team;
10653041982dSJonathan Peyton   //    task->td_parent   = NULL;  // fix for CQ230101 (broken parent task info
10663041982dSJonathan Peyton   //    in debugger)
10677cc577a4SJonathan Peyton   task->td_ident = loc_ref;
10687cc577a4SJonathan Peyton   task->td_taskwait_ident = NULL;
10697cc577a4SJonathan Peyton   task->td_taskwait_counter = 0;
10707cc577a4SJonathan Peyton   task->td_taskwait_thread = 0;
10717cc577a4SJonathan Peyton 
10727cc577a4SJonathan Peyton   task->td_flags.tiedness = TASK_TIED;
10737cc577a4SJonathan Peyton   task->td_flags.tasktype = TASK_IMPLICIT;
10747cc577a4SJonathan Peyton #if OMP_45_ENABLED
10757cc577a4SJonathan Peyton   task->td_flags.proxy = TASK_FULL;
10767cc577a4SJonathan Peyton #endif
10777cc577a4SJonathan Peyton 
10787cc577a4SJonathan Peyton   // All implicit tasks are executed immediately, not deferred
10797cc577a4SJonathan Peyton   task->td_flags.task_serial = 1;
10807cc577a4SJonathan Peyton   task->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
10817cc577a4SJonathan Peyton   task->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
10827cc577a4SJonathan Peyton 
10837cc577a4SJonathan Peyton   task->td_flags.started = 1;
10847cc577a4SJonathan Peyton   task->td_flags.executing = 1;
10857cc577a4SJonathan Peyton   task->td_flags.complete = 0;
10867cc577a4SJonathan Peyton   task->td_flags.freed = 0;
10877cc577a4SJonathan Peyton 
10887cc577a4SJonathan Peyton #if OMP_40_ENABLED
10897cc577a4SJonathan Peyton   task->td_depnode = NULL;
10907cc577a4SJonathan Peyton #endif
1091a756cb24SAndrey Churbanov   task->td_last_tied = task;
1092*405037c4SAndrey Churbanov #if OMP_50_ENABLED
1093*405037c4SAndrey Churbanov   task->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1094*405037c4SAndrey Churbanov #endif
10957cc577a4SJonathan Peyton 
10963041982dSJonathan Peyton   if (set_curr_task) { // only do this init first time thread is created
109737e2ef54SJonathan Peyton     KMP_ATOMIC_ST_REL(&task->td_incomplete_child_tasks, 0);
1098642688b6SJonathan Peyton     // Not used: don't need to deallocate implicit task
109937e2ef54SJonathan Peyton     KMP_ATOMIC_ST_REL(&task->td_allocated_child_tasks, 0);
11007cc577a4SJonathan Peyton #if OMP_40_ENABLED
11017cc577a4SJonathan Peyton     task->td_taskgroup = NULL; // An implicit task does not have taskgroup
11027cc577a4SJonathan Peyton     task->td_dephash = NULL;
11037cc577a4SJonathan Peyton #endif
11047cc577a4SJonathan Peyton     __kmp_push_current_task_to_thread(this_thr, team, tid);
11057cc577a4SJonathan Peyton   } else {
11067cc577a4SJonathan Peyton     KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0);
11077cc577a4SJonathan Peyton     KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0);
11087cc577a4SJonathan Peyton   }
11097cc577a4SJonathan Peyton 
11107cc577a4SJonathan Peyton #if OMPT_SUPPORT
1111f0a1c65fSJonas Hahnfeld   if (UNLIKELY(ompt_enabled.enabled))
111282e94a59SJoachim Protze     __ompt_task_init(task, tid);
11137cc577a4SJonathan Peyton #endif
11147cc577a4SJonathan Peyton 
11153041982dSJonathan Peyton   KF_TRACE(10, ("__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n", tid,
11163041982dSJonathan Peyton                 team, task));
11177cc577a4SJonathan Peyton }
11187cc577a4SJonathan Peyton 
11193041982dSJonathan Peyton // __kmp_finish_implicit_task: Release resources associated to implicit tasks
11203041982dSJonathan Peyton // at the end of parallel regions. Some resources are kept for reuse in the next
11213041982dSJonathan Peyton // parallel region.
11227cc577a4SJonathan Peyton //
11233041982dSJonathan Peyton // thread:  thread data structure corresponding to implicit task
11243041982dSJonathan Peyton void __kmp_finish_implicit_task(kmp_info_t *thread) {
11257cc577a4SJonathan Peyton   kmp_taskdata_t *task = thread->th.th_current_task;
1126c3344345SAndrey Churbanov   if (task->td_dephash) {
1127c3344345SAndrey Churbanov     int children;
1128c3344345SAndrey Churbanov     task->td_flags.complete = 1;
1129c3344345SAndrey Churbanov     children = KMP_ATOMIC_LD_ACQ(&task->td_incomplete_child_tasks);
1130c3344345SAndrey Churbanov     kmp_tasking_flags_t flags_old = task->td_flags;
1131c3344345SAndrey Churbanov     if (children == 0 && flags_old.complete == 1) {
1132c3344345SAndrey Churbanov       kmp_tasking_flags_t flags_new = flags_old;
1133c3344345SAndrey Churbanov       flags_new.complete = 0;
1134c3344345SAndrey Churbanov       if (KMP_COMPARE_AND_STORE_ACQ32(RCAST(kmp_int32 *, &task->td_flags),
1135c3344345SAndrey Churbanov                                       *RCAST(kmp_int32 *, &flags_old),
1136c3344345SAndrey Churbanov                                       *RCAST(kmp_int32 *, &flags_new))) {
1137c3344345SAndrey Churbanov         KA_TRACE(100, ("__kmp_finish_implicit_task: T#%d cleans "
1138c3344345SAndrey Churbanov                        "dephash of implicit task %p\n",
1139c3344345SAndrey Churbanov                        thread->th.th_info.ds.ds_gtid, task));
11407cc577a4SJonathan Peyton         __kmp_dephash_free_entries(thread, task->td_dephash);
11417cc577a4SJonathan Peyton       }
1142c3344345SAndrey Churbanov     }
1143c3344345SAndrey Churbanov   }
1144c3344345SAndrey Churbanov }
11457cc577a4SJonathan Peyton 
11463041982dSJonathan Peyton // __kmp_free_implicit_task: Release resources associated to implicit tasks
11473041982dSJonathan Peyton // when these are destroyed regions
11487cc577a4SJonathan Peyton //
11493041982dSJonathan Peyton // thread:  thread data structure corresponding to implicit task
11503041982dSJonathan Peyton void __kmp_free_implicit_task(kmp_info_t *thread) {
11517cc577a4SJonathan Peyton   kmp_taskdata_t *task = thread->th.th_current_task;
1152bff8ded9SJonathan Peyton   if (task && task->td_dephash) {
11537cc577a4SJonathan Peyton     __kmp_dephash_free(thread, task->td_dephash);
11547cc577a4SJonathan Peyton     task->td_dephash = NULL;
11557cc577a4SJonathan Peyton   }
1156bff8ded9SJonathan Peyton }
11577cc577a4SJonathan Peyton 
11583041982dSJonathan Peyton // Round up a size to a power of two specified by val: Used to insert padding
11593041982dSJonathan Peyton // between structures co-allocated using a single malloc() call
11603041982dSJonathan Peyton static size_t __kmp_round_up_to_val(size_t size, size_t val) {
11617cc577a4SJonathan Peyton   if (size & (val - 1)) {
11627cc577a4SJonathan Peyton     size &= ~(val - 1);
11637cc577a4SJonathan Peyton     if (size <= KMP_SIZE_T_MAX - val) {
11647cc577a4SJonathan Peyton       size += val; // Round up if there is no overflow.
1165bd3a7633SJonathan Peyton     }
1166bd3a7633SJonathan Peyton   }
11677cc577a4SJonathan Peyton   return size;
11687cc577a4SJonathan Peyton } // __kmp_round_up_to_va
11697cc577a4SJonathan Peyton 
11707cc577a4SJonathan Peyton // __kmp_task_alloc: Allocate the taskdata and task data structures for a task
11717cc577a4SJonathan Peyton //
11727cc577a4SJonathan Peyton // loc_ref: source location information
11737cc577a4SJonathan Peyton // gtid: global thread number.
11743041982dSJonathan Peyton // flags: include tiedness & task type (explicit vs. implicit) of the ''new''
11753041982dSJonathan Peyton // task encountered. Converted from kmp_int32 to kmp_tasking_flags_t in routine.
11763041982dSJonathan Peyton // sizeof_kmp_task_t:  Size in bytes of kmp_task_t data structure including
11773041982dSJonathan Peyton // private vars accessed in task.
11783041982dSJonathan Peyton // sizeof_shareds:  Size in bytes of array of pointers to shared vars accessed
11793041982dSJonathan Peyton // in task.
11807cc577a4SJonathan Peyton // task_entry: Pointer to task code entry point generated by compiler.
11817cc577a4SJonathan Peyton // returns: a pointer to the allocated kmp_task_t structure (task).
11823041982dSJonathan Peyton kmp_task_t *__kmp_task_alloc(ident_t *loc_ref, kmp_int32 gtid,
11833041982dSJonathan Peyton                              kmp_tasking_flags_t *flags,
11847cc577a4SJonathan Peyton                              size_t sizeof_kmp_task_t, size_t sizeof_shareds,
11853041982dSJonathan Peyton                              kmp_routine_entry_t task_entry) {
11867cc577a4SJonathan Peyton   kmp_task_t *task;
11877cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata;
11887cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
11897cc577a4SJonathan Peyton   kmp_team_t *team = thread->th.th_team;
11907cc577a4SJonathan Peyton   kmp_taskdata_t *parent_task = thread->th.th_current_task;
11917cc577a4SJonathan Peyton   size_t shareds_offset;
11927cc577a4SJonathan Peyton 
119328226e7dSJonathan Peyton   if (!TCR_4(__kmp_init_middle))
119428226e7dSJonathan Peyton     __kmp_middle_initialize();
119528226e7dSJonathan Peyton 
11967cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) "
11977cc577a4SJonathan Peyton                 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
11987cc577a4SJonathan Peyton                 gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t,
11997cc577a4SJonathan Peyton                 sizeof_shareds, task_entry));
12007cc577a4SJonathan Peyton 
12017cc577a4SJonathan Peyton   if (parent_task->td_flags.final) {
12027cc577a4SJonathan Peyton     if (flags->merged_if0) {
12037cc577a4SJonathan Peyton     }
12047cc577a4SJonathan Peyton     flags->final = 1;
12057cc577a4SJonathan Peyton   }
1206a756cb24SAndrey Churbanov   if (flags->tiedness == TASK_UNTIED && !team->t.t_serialized) {
1207a756cb24SAndrey Churbanov     // Untied task encountered causes the TSC algorithm to check entire deque of
1208a756cb24SAndrey Churbanov     // the victim thread. If no untied task encountered, then checking the head
1209a756cb24SAndrey Churbanov     // of the deque should be enough.
1210a756cb24SAndrey Churbanov     KMP_CHECK_UPDATE(thread->th.th_task_team->tt.tt_untied_task_encountered, 1);
1211a756cb24SAndrey Churbanov   }
12127cc577a4SJonathan Peyton 
1213*405037c4SAndrey Churbanov #if OMP_50_ENABLED
1214*405037c4SAndrey Churbanov   // Detachable tasks are not proxy tasks yet but could be in the future. Doing
1215*405037c4SAndrey Churbanov   // the tasking setup
1216*405037c4SAndrey Churbanov   // when that happens is too late.
1217*405037c4SAndrey Churbanov   if (flags->proxy == TASK_PROXY || flags->detachable == TASK_DETACHABLE) {
1218*405037c4SAndrey Churbanov #endif
12197cc577a4SJonathan Peyton #if OMP_45_ENABLED
12207cc577a4SJonathan Peyton     if (flags->proxy == TASK_PROXY) {
12217cc577a4SJonathan Peyton       flags->tiedness = TASK_UNTIED;
12227cc577a4SJonathan Peyton       flags->merged_if0 = 1;
1223*405037c4SAndrey Churbanov #if OMP_50_ENABLED
1224*405037c4SAndrey Churbanov     }
1225*405037c4SAndrey Churbanov #endif
12263041982dSJonathan Peyton     /* are we running in a sequential parallel or tskm_immediate_exec... we need
12273041982dSJonathan Peyton        tasking support enabled */
12287cc577a4SJonathan Peyton     if ((thread->th.th_task_team) == NULL) {
12297cc577a4SJonathan Peyton       /* This should only happen if the team is serialized
12303041982dSJonathan Peyton           setup a task team and propagate it to the thread */
12317cc577a4SJonathan Peyton       KMP_DEBUG_ASSERT(team->t.t_serialized);
12323041982dSJonathan Peyton       KA_TRACE(30,
12333041982dSJonathan Peyton                ("T#%d creating task team in __kmp_task_alloc for proxy task\n",
12343041982dSJonathan Peyton                 gtid));
12353041982dSJonathan Peyton       __kmp_task_team_setup(
12363041982dSJonathan Peyton           thread, team,
12373041982dSJonathan Peyton           1); // 1 indicates setup the current team regardless of nthreads
12387cc577a4SJonathan Peyton       thread->th.th_task_team = team->t.t_task_team[thread->th.th_task_state];
12397cc577a4SJonathan Peyton     }
12407cc577a4SJonathan Peyton     kmp_task_team_t *task_team = thread->th.th_task_team;
12417cc577a4SJonathan Peyton 
12427cc577a4SJonathan Peyton     /* tasking must be enabled now as the task might not be pushed */
12437cc577a4SJonathan Peyton     if (!KMP_TASKING_ENABLED(task_team)) {
12443041982dSJonathan Peyton       KA_TRACE(
12453041982dSJonathan Peyton           30,
12463041982dSJonathan Peyton           ("T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid));
12477cc577a4SJonathan Peyton       __kmp_enable_tasking(task_team, thread);
12487cc577a4SJonathan Peyton       kmp_int32 tid = thread->th.th_info.ds.ds_tid;
12497cc577a4SJonathan Peyton       kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
12507cc577a4SJonathan Peyton       // No lock needed since only owner can allocate
12517cc577a4SJonathan Peyton       if (thread_data->td.td_deque == NULL) {
12527cc577a4SJonathan Peyton         __kmp_alloc_task_deque(thread, thread_data);
12537cc577a4SJonathan Peyton       }
12547cc577a4SJonathan Peyton     }
12557cc577a4SJonathan Peyton 
12567cc577a4SJonathan Peyton     if (task_team->tt.tt_found_proxy_tasks == FALSE)
12577cc577a4SJonathan Peyton       TCW_4(task_team->tt.tt_found_proxy_tasks, TRUE);
12587cc577a4SJonathan Peyton   }
12597cc577a4SJonathan Peyton #endif
12607cc577a4SJonathan Peyton 
12617cc577a4SJonathan Peyton   // Calculate shared structure offset including padding after kmp_task_t struct
12627cc577a4SJonathan Peyton   // to align pointers in shared struct
12637cc577a4SJonathan Peyton   shareds_offset = sizeof(kmp_taskdata_t) + sizeof_kmp_task_t;
12647cc577a4SJonathan Peyton   shareds_offset = __kmp_round_up_to_val(shareds_offset, sizeof(void *));
12657cc577a4SJonathan Peyton 
12667cc577a4SJonathan Peyton   // Allocate a kmp_taskdata_t block and a kmp_task_t block.
12673041982dSJonathan Peyton   KA_TRACE(30, ("__kmp_task_alloc: T#%d First malloc size: %ld\n", gtid,
12683041982dSJonathan Peyton                 shareds_offset));
12693041982dSJonathan Peyton   KA_TRACE(30, ("__kmp_task_alloc: T#%d Second malloc size: %ld\n", gtid,
12703041982dSJonathan Peyton                 sizeof_shareds));
12717cc577a4SJonathan Peyton 
12727cc577a4SJonathan Peyton // Avoid double allocation here by combining shareds with taskdata
12737cc577a4SJonathan Peyton #if USE_FAST_MEMORY
12743041982dSJonathan Peyton   taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, shareds_offset +
12753041982dSJonathan Peyton                                                                sizeof_shareds);
12767cc577a4SJonathan Peyton #else /* ! USE_FAST_MEMORY */
12773041982dSJonathan Peyton   taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, shareds_offset +
12783041982dSJonathan Peyton                                                                sizeof_shareds);
12797cc577a4SJonathan Peyton #endif /* USE_FAST_MEMORY */
12807cc577a4SJonathan Peyton   ANNOTATE_HAPPENS_AFTER(taskdata);
12817cc577a4SJonathan Peyton 
12827cc577a4SJonathan Peyton   task = KMP_TASKDATA_TO_TASK(taskdata);
12837cc577a4SJonathan Peyton 
12847cc577a4SJonathan Peyton // Make sure task & taskdata are aligned appropriately
12857cc577a4SJonathan Peyton #if KMP_ARCH_X86 || KMP_ARCH_PPC64 || !KMP_HAVE_QUAD
12867cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (sizeof(double) - 1)) == 0);
12877cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (sizeof(double) - 1)) == 0);
12887cc577a4SJonathan Peyton #else
12897cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (sizeof(_Quad) - 1)) == 0);
12907cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (sizeof(_Quad) - 1)) == 0);
12917cc577a4SJonathan Peyton #endif
12927cc577a4SJonathan Peyton   if (sizeof_shareds > 0) {
12937cc577a4SJonathan Peyton     // Avoid double allocation here by combining shareds with taskdata
12947cc577a4SJonathan Peyton     task->shareds = &((char *)taskdata)[shareds_offset];
12957cc577a4SJonathan Peyton     // Make sure shareds struct is aligned to pointer size
12963041982dSJonathan Peyton     KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (sizeof(void *) - 1)) ==
12973041982dSJonathan Peyton                      0);
12987cc577a4SJonathan Peyton   } else {
12997cc577a4SJonathan Peyton     task->shareds = NULL;
13007cc577a4SJonathan Peyton   }
13017cc577a4SJonathan Peyton   task->routine = task_entry;
13027cc577a4SJonathan Peyton   task->part_id = 0; // AC: Always start with 0 part id
13037cc577a4SJonathan Peyton 
13047cc577a4SJonathan Peyton   taskdata->td_task_id = KMP_GEN_TASK_ID();
13057cc577a4SJonathan Peyton   taskdata->td_team = team;
13067cc577a4SJonathan Peyton   taskdata->td_alloc_thread = thread;
13077cc577a4SJonathan Peyton   taskdata->td_parent = parent_task;
13087cc577a4SJonathan Peyton   taskdata->td_level = parent_task->td_level + 1; // increment nesting level
130937e2ef54SJonathan Peyton   KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0);
13107cc577a4SJonathan Peyton   taskdata->td_ident = loc_ref;
13117cc577a4SJonathan Peyton   taskdata->td_taskwait_ident = NULL;
13127cc577a4SJonathan Peyton   taskdata->td_taskwait_counter = 0;
13137cc577a4SJonathan Peyton   taskdata->td_taskwait_thread = 0;
13147cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_parent != NULL);
13157cc577a4SJonathan Peyton #if OMP_45_ENABLED
13167cc577a4SJonathan Peyton   // avoid copying icvs for proxy tasks
13177cc577a4SJonathan Peyton   if (flags->proxy == TASK_FULL)
13187cc577a4SJonathan Peyton #endif
13197cc577a4SJonathan Peyton     copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs);
13207cc577a4SJonathan Peyton 
13217cc577a4SJonathan Peyton   taskdata->td_flags.tiedness = flags->tiedness;
13227cc577a4SJonathan Peyton   taskdata->td_flags.final = flags->final;
13237cc577a4SJonathan Peyton   taskdata->td_flags.merged_if0 = flags->merged_if0;
13247cc577a4SJonathan Peyton #if OMP_40_ENABLED
13257cc577a4SJonathan Peyton   taskdata->td_flags.destructors_thunk = flags->destructors_thunk;
13267cc577a4SJonathan Peyton #endif // OMP_40_ENABLED
13277cc577a4SJonathan Peyton #if OMP_45_ENABLED
13287cc577a4SJonathan Peyton   taskdata->td_flags.proxy = flags->proxy;
1329*405037c4SAndrey Churbanov #if OMP_50_ENABLED
1330*405037c4SAndrey Churbanov   taskdata->td_flags.detachable = flags->detachable;
1331*405037c4SAndrey Churbanov #endif
13327cc577a4SJonathan Peyton   taskdata->td_task_team = thread->th.th_task_team;
13337cc577a4SJonathan Peyton   taskdata->td_size_alloc = shareds_offset + sizeof_shareds;
13347cc577a4SJonathan Peyton #endif
13357cc577a4SJonathan Peyton   taskdata->td_flags.tasktype = TASK_EXPLICIT;
13367cc577a4SJonathan Peyton 
13377cc577a4SJonathan Peyton   // GEH - TODO: fix this to copy parent task's value of tasking_ser flag
13387cc577a4SJonathan Peyton   taskdata->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
13397cc577a4SJonathan Peyton 
13407cc577a4SJonathan Peyton   // GEH - TODO: fix this to copy parent task's value of team_serial flag
13417cc577a4SJonathan Peyton   taskdata->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
13427cc577a4SJonathan Peyton 
13433041982dSJonathan Peyton   // GEH - Note we serialize the task if the team is serialized to make sure
13443041982dSJonathan Peyton   // implicit parallel region tasks are not left until program termination to
13453041982dSJonathan Peyton   // execute. Also, it helps locality to execute immediately.
13463041982dSJonathan Peyton 
13473041982dSJonathan Peyton   taskdata->td_flags.task_serial =
13483041982dSJonathan Peyton       (parent_task->td_flags.final || taskdata->td_flags.team_serial ||
13493041982dSJonathan Peyton        taskdata->td_flags.tasking_ser);
13507cc577a4SJonathan Peyton 
13517cc577a4SJonathan Peyton   taskdata->td_flags.started = 0;
13527cc577a4SJonathan Peyton   taskdata->td_flags.executing = 0;
13537cc577a4SJonathan Peyton   taskdata->td_flags.complete = 0;
13547cc577a4SJonathan Peyton   taskdata->td_flags.freed = 0;
13557cc577a4SJonathan Peyton 
13567cc577a4SJonathan Peyton   taskdata->td_flags.native = flags->native;
13577cc577a4SJonathan Peyton 
135837e2ef54SJonathan Peyton   KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0);
135937e2ef54SJonathan Peyton   // start at one because counts current task and children
136037e2ef54SJonathan Peyton   KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1);
13617cc577a4SJonathan Peyton #if OMP_40_ENABLED
13623041982dSJonathan Peyton   taskdata->td_taskgroup =
13633041982dSJonathan Peyton       parent_task->td_taskgroup; // task inherits taskgroup from the parent task
13647cc577a4SJonathan Peyton   taskdata->td_dephash = NULL;
13657cc577a4SJonathan Peyton   taskdata->td_depnode = NULL;
13667cc577a4SJonathan Peyton #endif
1367a756cb24SAndrey Churbanov   if (flags->tiedness == TASK_UNTIED)
1368a756cb24SAndrey Churbanov     taskdata->td_last_tied = NULL; // will be set when the task is scheduled
1369a756cb24SAndrey Churbanov   else
1370a756cb24SAndrey Churbanov     taskdata->td_last_tied = taskdata;
1371*405037c4SAndrey Churbanov #if OMP_50_ENABLED
1372*405037c4SAndrey Churbanov   taskdata->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED;
1373*405037c4SAndrey Churbanov #endif
137486ed6aa6SJoachim Protze #if OMPT_SUPPORT
137586ed6aa6SJoachim Protze   if (UNLIKELY(ompt_enabled.enabled))
137686ed6aa6SJoachim Protze     __ompt_task_init(taskdata, gtid);
137786ed6aa6SJoachim Protze #endif
13783041982dSJonathan Peyton // Only need to keep track of child task counts if team parallel and tasking not
1379*405037c4SAndrey Churbanov // serialized or if it is a proxy or detachable task
13807cc577a4SJonathan Peyton #if OMP_45_ENABLED
13813041982dSJonathan Peyton   if (flags->proxy == TASK_PROXY ||
1382*405037c4SAndrey Churbanov #if OMP_50_ENABLED
1383*405037c4SAndrey Churbanov       flags->detachable == TASK_DETACHABLE ||
1384*405037c4SAndrey Churbanov #endif
13853041982dSJonathan Peyton       !(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser))
13867cc577a4SJonathan Peyton #else
13877cc577a4SJonathan Peyton   if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser))
13887cc577a4SJonathan Peyton #endif
13897cc577a4SJonathan Peyton   {
139037e2ef54SJonathan Peyton     KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
13917cc577a4SJonathan Peyton #if OMP_40_ENABLED
13927cc577a4SJonathan Peyton     if (parent_task->td_taskgroup)
139337e2ef54SJonathan Peyton       KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
13947cc577a4SJonathan Peyton #endif
13953041982dSJonathan Peyton     // Only need to keep track of allocated child tasks for explicit tasks since
13963041982dSJonathan Peyton     // implicit not deallocated
13977cc577a4SJonathan Peyton     if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) {
139837e2ef54SJonathan Peyton       KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
13997cc577a4SJonathan Peyton     }
14007cc577a4SJonathan Peyton   }
14017cc577a4SJonathan Peyton 
14027cc577a4SJonathan Peyton   KA_TRACE(20, ("__kmp_task_alloc(exit): T#%d created task %p parent=%p\n",
14037cc577a4SJonathan Peyton                 gtid, taskdata, taskdata->td_parent));
14047cc577a4SJonathan Peyton   ANNOTATE_HAPPENS_BEFORE(task);
14057cc577a4SJonathan Peyton 
14067cc577a4SJonathan Peyton   return task;
14077cc577a4SJonathan Peyton }
14087cc577a4SJonathan Peyton 
14093041982dSJonathan Peyton kmp_task_t *__kmpc_omp_task_alloc(ident_t *loc_ref, kmp_int32 gtid,
14103041982dSJonathan Peyton                                   kmp_int32 flags, size_t sizeof_kmp_task_t,
14113041982dSJonathan Peyton                                   size_t sizeof_shareds,
14123041982dSJonathan Peyton                                   kmp_routine_entry_t task_entry) {
14137cc577a4SJonathan Peyton   kmp_task_t *retval;
14147cc577a4SJonathan Peyton   kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
14157cc577a4SJonathan Peyton 
14167cc577a4SJonathan Peyton   input_flags->native = FALSE;
14177cc577a4SJonathan Peyton // __kmp_task_alloc() sets up all other runtime flags
14187cc577a4SJonathan Peyton 
14197cc577a4SJonathan Peyton #if OMP_45_ENABLED
1420*405037c4SAndrey Churbanov #if OMP_50_ENABLED
1421*405037c4SAndrey Churbanov   KA_TRACE(10, ("__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s %s) "
1422*405037c4SAndrey Churbanov                 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
1423*405037c4SAndrey Churbanov                 gtid, loc_ref, input_flags->tiedness ? "tied  " : "untied",
1424*405037c4SAndrey Churbanov                 input_flags->proxy ? "proxy" : "",
1425*405037c4SAndrey Churbanov                 input_flags->detachable ? "detachable" : "", sizeof_kmp_task_t,
1426*405037c4SAndrey Churbanov                 sizeof_shareds, task_entry));
1427*405037c4SAndrey Churbanov #else
14287cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s) "
14297cc577a4SJonathan Peyton                 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
14307cc577a4SJonathan Peyton                 gtid, loc_ref, input_flags->tiedness ? "tied  " : "untied",
14313041982dSJonathan Peyton                 input_flags->proxy ? "proxy" : "", sizeof_kmp_task_t,
14323041982dSJonathan Peyton                 sizeof_shareds, task_entry));
1433*405037c4SAndrey Churbanov #endif
14347cc577a4SJonathan Peyton #else
14357cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s) "
14367cc577a4SJonathan Peyton                 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
14377cc577a4SJonathan Peyton                 gtid, loc_ref, input_flags->tiedness ? "tied  " : "untied",
14387cc577a4SJonathan Peyton                 sizeof_kmp_task_t, sizeof_shareds, task_entry));
14397cc577a4SJonathan Peyton #endif
14407cc577a4SJonathan Peyton 
14417cc577a4SJonathan Peyton   retval = __kmp_task_alloc(loc_ref, gtid, input_flags, sizeof_kmp_task_t,
14427cc577a4SJonathan Peyton                             sizeof_shareds, task_entry);
14437cc577a4SJonathan Peyton 
14447cc577a4SJonathan Peyton   KA_TRACE(20, ("__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval));
14457cc577a4SJonathan Peyton 
14467cc577a4SJonathan Peyton   return retval;
14477cc577a4SJonathan Peyton }
14487cc577a4SJonathan Peyton 
1449b48e44a6SGheorghe-Teodor Bercea kmp_task_t *__kmpc_omp_target_task_alloc(ident_t *loc_ref, kmp_int32 gtid,
1450b48e44a6SGheorghe-Teodor Bercea                                          kmp_int32 flags,
1451b48e44a6SGheorghe-Teodor Bercea                                          size_t sizeof_kmp_task_t,
1452b48e44a6SGheorghe-Teodor Bercea                                          size_t sizeof_shareds,
1453b48e44a6SGheorghe-Teodor Bercea                                          kmp_routine_entry_t task_entry,
1454b48e44a6SGheorghe-Teodor Bercea                                          kmp_int64 device_id) {
1455b48e44a6SGheorghe-Teodor Bercea   return __kmpc_omp_task_alloc(loc_ref, gtid, flags, sizeof_kmp_task_t,
1456b48e44a6SGheorghe-Teodor Bercea                                sizeof_shareds, task_entry);
1457b48e44a6SGheorghe-Teodor Bercea }
1458b48e44a6SGheorghe-Teodor Bercea 
1459b04f7d68SJonathan Peyton #if OMP_50_ENABLED
1460b04f7d68SJonathan Peyton /*!
1461b04f7d68SJonathan Peyton @ingroup TASKING
1462b04f7d68SJonathan Peyton @param loc_ref location of the original task directive
1463b04f7d68SJonathan Peyton @param gtid Global Thread ID of encountering thread
1464b04f7d68SJonathan Peyton @param new_task task thunk allocated by __kmpc_omp_task_alloc() for the ''new
1465b04f7d68SJonathan Peyton task''
1466b04f7d68SJonathan Peyton @param naffins Number of affinity items
1467b04f7d68SJonathan Peyton @param affin_list List of affinity items
1468b04f7d68SJonathan Peyton @return Returns non-zero if registering affinity information was not successful.
1469b04f7d68SJonathan Peyton  Returns 0 if registration was successful
1470b04f7d68SJonathan Peyton This entry registers the affinity information attached to a task with the task
1471b04f7d68SJonathan Peyton thunk structure kmp_taskdata_t.
1472b04f7d68SJonathan Peyton */
1473b04f7d68SJonathan Peyton kmp_int32
1474b04f7d68SJonathan Peyton __kmpc_omp_reg_task_with_affinity(ident_t *loc_ref, kmp_int32 gtid,
1475b04f7d68SJonathan Peyton                                   kmp_task_t *new_task, kmp_int32 naffins,
1476b04f7d68SJonathan Peyton                                   kmp_task_affinity_info_t *affin_list) {
1477b04f7d68SJonathan Peyton   return 0;
1478b04f7d68SJonathan Peyton }
1479b04f7d68SJonathan Peyton #endif
1480b04f7d68SJonathan Peyton 
14817cc577a4SJonathan Peyton //  __kmp_invoke_task: invoke the specified task
14827cc577a4SJonathan Peyton //
14837cc577a4SJonathan Peyton // gtid: global thread ID of caller
14847cc577a4SJonathan Peyton // task: the task to invoke
14857cc577a4SJonathan Peyton // current_task: the task to resume after task invokation
14863041982dSJonathan Peyton static void __kmp_invoke_task(kmp_int32 gtid, kmp_task_t *task,
14873041982dSJonathan Peyton                               kmp_taskdata_t *current_task) {
14887cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
1489b1b221c8SJonathan Peyton   kmp_info_t *thread;
14907cc577a4SJonathan Peyton #if OMP_40_ENABLED
14917cc577a4SJonathan Peyton   int discard = 0 /* false */;
14927cc577a4SJonathan Peyton #endif
14933041982dSJonathan Peyton   KA_TRACE(
14943041982dSJonathan Peyton       30, ("__kmp_invoke_task(enter): T#%d invoking task %p, current_task=%p\n",
14957cc577a4SJonathan Peyton            gtid, taskdata, current_task));
14967cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task);
14977cc577a4SJonathan Peyton #if OMP_45_ENABLED
14987cc577a4SJonathan Peyton   if (taskdata->td_flags.proxy == TASK_PROXY &&
14993041982dSJonathan Peyton       taskdata->td_flags.complete == 1) {
15007cc577a4SJonathan Peyton     // This is a proxy task that was already completed but it needs to run
15017cc577a4SJonathan Peyton     // its bottom-half finish
15023041982dSJonathan Peyton     KA_TRACE(
15033041982dSJonathan Peyton         30,
15043041982dSJonathan Peyton         ("__kmp_invoke_task: T#%d running bottom finish for proxy task %p\n",
15057cc577a4SJonathan Peyton          gtid, taskdata));
15067cc577a4SJonathan Peyton 
15077cc577a4SJonathan Peyton     __kmp_bottom_half_finish_proxy(gtid, task);
15087cc577a4SJonathan Peyton 
15093041982dSJonathan Peyton     KA_TRACE(30, ("__kmp_invoke_task(exit): T#%d completed bottom finish for "
15103041982dSJonathan Peyton                   "proxy task %p, resuming task %p\n",
15113041982dSJonathan Peyton                   gtid, taskdata, current_task));
15127cc577a4SJonathan Peyton 
15137cc577a4SJonathan Peyton     return;
15147cc577a4SJonathan Peyton   }
15157cc577a4SJonathan Peyton #endif
15167cc577a4SJonathan Peyton 
15177cc577a4SJonathan Peyton #if OMPT_SUPPORT
151886ed6aa6SJoachim Protze   // For untied tasks, the first task executed only calls __kmpc_omp_task and
151986ed6aa6SJoachim Protze   // does not execute code.
15207cc577a4SJonathan Peyton   ompt_thread_info_t oldInfo;
1521f0a1c65fSJonas Hahnfeld   if (UNLIKELY(ompt_enabled.enabled)) {
15227cc577a4SJonathan Peyton     // Store the threads states and restore them after the task
15237cc577a4SJonathan Peyton     thread = __kmp_threads[gtid];
15247cc577a4SJonathan Peyton     oldInfo = thread->th.ompt_thread_info;
15257cc577a4SJonathan Peyton     thread->th.ompt_thread_info.wait_id = 0;
152682e94a59SJoachim Protze     thread->th.ompt_thread_info.state = (thread->th.th_team_serialized)
15270e0d6cddSJoachim Protze                                             ? ompt_state_work_serial
15280e0d6cddSJoachim Protze                                             : ompt_state_work_parallel;
15290e0d6cddSJoachim Protze     taskdata->ompt_task_info.frame.exit_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
15307cc577a4SJonathan Peyton   }
15317cc577a4SJonathan Peyton #endif
15327cc577a4SJonathan Peyton 
153386ed6aa6SJoachim Protze #if OMP_45_ENABLED
153486ed6aa6SJoachim Protze   // Proxy tasks are not handled by the runtime
153586ed6aa6SJoachim Protze   if (taskdata->td_flags.proxy != TASK_PROXY) {
153686ed6aa6SJoachim Protze #endif
153786ed6aa6SJoachim Protze     ANNOTATE_HAPPENS_AFTER(task);
153886ed6aa6SJoachim Protze     __kmp_task_start(gtid, task, current_task); // OMPT only if not discarded
153986ed6aa6SJoachim Protze #if OMP_45_ENABLED
154086ed6aa6SJoachim Protze   }
154186ed6aa6SJoachim Protze #endif
154286ed6aa6SJoachim Protze 
15437cc577a4SJonathan Peyton #if OMP_40_ENABLED
15447cc577a4SJonathan Peyton   // TODO: cancel tasks if the parallel region has also been cancelled
15457cc577a4SJonathan Peyton   // TODO: check if this sequence can be hoisted above __kmp_task_start
15467cc577a4SJonathan Peyton   // if cancellation has been enabled for this run ...
15477cc577a4SJonathan Peyton   if (__kmp_omp_cancellation) {
1548b1b221c8SJonathan Peyton     thread = __kmp_threads[gtid];
1549b1b221c8SJonathan Peyton     kmp_team_t *this_team = thread->th.th_team;
15507cc577a4SJonathan Peyton     kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
15513041982dSJonathan Peyton     if ((taskgroup && taskgroup->cancel_request) ||
15523041982dSJonathan Peyton         (this_team->t.t_cancel_request == cancel_parallel)) {
155382e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
155482e94a59SJoachim Protze       ompt_data_t *task_data;
1555f0a1c65fSJonas Hahnfeld       if (UNLIKELY(ompt_enabled.ompt_callback_cancel)) {
155682e94a59SJoachim Protze         __ompt_get_task_info_internal(0, NULL, &task_data, NULL, NULL, NULL);
155782e94a59SJoachim Protze         ompt_callbacks.ompt_callback(ompt_callback_cancel)(
155882e94a59SJoachim Protze             task_data,
155982e94a59SJoachim Protze             ((taskgroup && taskgroup->cancel_request) ? ompt_cancel_taskgroup
156082e94a59SJoachim Protze                                                       : ompt_cancel_parallel) |
156182e94a59SJoachim Protze                 ompt_cancel_discarded_task,
156282e94a59SJoachim Protze             NULL);
156382e94a59SJoachim Protze       }
156482e94a59SJoachim Protze #endif
15657cc577a4SJonathan Peyton       KMP_COUNT_BLOCK(TASK_cancelled);
15667cc577a4SJonathan Peyton       // this task belongs to a task group and we need to cancel it
15677cc577a4SJonathan Peyton       discard = 1 /* true */;
15687cc577a4SJonathan Peyton     }
15697cc577a4SJonathan Peyton   }
15707cc577a4SJonathan Peyton 
15717cc577a4SJonathan Peyton   // Invoke the task routine and pass in relevant data.
15727cc577a4SJonathan Peyton   // Thunks generated by gcc take a different argument list.
15737cc577a4SJonathan Peyton   if (!discard) {
1574a756cb24SAndrey Churbanov     if (taskdata->td_flags.tiedness == TASK_UNTIED) {
1575a756cb24SAndrey Churbanov       taskdata->td_last_tied = current_task->td_last_tied;
1576a756cb24SAndrey Churbanov       KMP_DEBUG_ASSERT(taskdata->td_last_tied);
1577a756cb24SAndrey Churbanov     }
15787cc577a4SJonathan Peyton #if KMP_STATS_ENABLED
15797cc577a4SJonathan Peyton     KMP_COUNT_BLOCK(TASK_executed);
15807cc577a4SJonathan Peyton     switch (KMP_GET_THREAD_STATE()) {
15813041982dSJonathan Peyton     case FORK_JOIN_BARRIER:
15823041982dSJonathan Peyton       KMP_PUSH_PARTITIONED_TIMER(OMP_task_join_bar);
15833041982dSJonathan Peyton       break;
15843041982dSJonathan Peyton     case PLAIN_BARRIER:
15853041982dSJonathan Peyton       KMP_PUSH_PARTITIONED_TIMER(OMP_task_plain_bar);
15863041982dSJonathan Peyton       break;
15873041982dSJonathan Peyton     case TASKYIELD:
15883041982dSJonathan Peyton       KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskyield);
15893041982dSJonathan Peyton       break;
15903041982dSJonathan Peyton     case TASKWAIT:
15913041982dSJonathan Peyton       KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskwait);
15923041982dSJonathan Peyton       break;
15933041982dSJonathan Peyton     case TASKGROUP:
15943041982dSJonathan Peyton       KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskgroup);
15953041982dSJonathan Peyton       break;
15963041982dSJonathan Peyton     default:
15973041982dSJonathan Peyton       KMP_PUSH_PARTITIONED_TIMER(OMP_task_immediate);
15983041982dSJonathan Peyton       break;
15997cc577a4SJonathan Peyton     }
16007cc577a4SJonathan Peyton #endif // KMP_STATS_ENABLED
16017cc577a4SJonathan Peyton #endif // OMP_40_ENABLED
16027cc577a4SJonathan Peyton 
160382e94a59SJoachim Protze // OMPT task begin
160482e94a59SJoachim Protze #if OMPT_SUPPORT
1605f0a1c65fSJonas Hahnfeld     if (UNLIKELY(ompt_enabled.enabled))
160682e94a59SJoachim Protze       __ompt_task_start(task, current_task, gtid);
16077cc577a4SJonathan Peyton #endif
16087cc577a4SJonathan Peyton 
1609b1b221c8SJonathan Peyton #if USE_ITT_BUILD && USE_ITT_NOTIFY
1610b1b221c8SJonathan Peyton     kmp_uint64 cur_time;
1611b1b221c8SJonathan Peyton     kmp_int32 kmp_itt_count_task =
1612b1b221c8SJonathan Peyton         __kmp_forkjoin_frames_mode == 3 && !taskdata->td_flags.task_serial &&
1613b1b221c8SJonathan Peyton         current_task->td_flags.tasktype == TASK_IMPLICIT;
1614b1b221c8SJonathan Peyton     if (kmp_itt_count_task) {
1615b1b221c8SJonathan Peyton       thread = __kmp_threads[gtid];
1616b1b221c8SJonathan Peyton       // Time outer level explicit task on barrier for adjusting imbalance time
1617b1b221c8SJonathan Peyton       if (thread->th.th_bar_arrive_time)
1618b1b221c8SJonathan Peyton         cur_time = __itt_get_timestamp();
1619b1b221c8SJonathan Peyton       else
1620b1b221c8SJonathan Peyton         kmp_itt_count_task = 0; // thread is not on a barrier - skip timing
1621b1b221c8SJonathan Peyton     }
1622b1b221c8SJonathan Peyton #endif
1623b1b221c8SJonathan Peyton 
16247cc577a4SJonathan Peyton #ifdef KMP_GOMP_COMPAT
16257cc577a4SJonathan Peyton     if (taskdata->td_flags.native) {
16267cc577a4SJonathan Peyton       ((void (*)(void *))(*(task->routine)))(task->shareds);
16273041982dSJonathan Peyton     } else
16287cc577a4SJonathan Peyton #endif /* KMP_GOMP_COMPAT */
16297cc577a4SJonathan Peyton     {
16307cc577a4SJonathan Peyton       (*(task->routine))(gtid, task);
16317cc577a4SJonathan Peyton     }
16327cc577a4SJonathan Peyton     KMP_POP_PARTITIONED_TIMER();
16337cc577a4SJonathan Peyton 
1634b1b221c8SJonathan Peyton #if USE_ITT_BUILD && USE_ITT_NOTIFY
1635b1b221c8SJonathan Peyton     if (kmp_itt_count_task) {
1636b1b221c8SJonathan Peyton       // Barrier imbalance - adjust arrive time with the task duration
1637b1b221c8SJonathan Peyton       thread->th.th_bar_arrive_time += (__itt_get_timestamp() - cur_time);
1638b1b221c8SJonathan Peyton     }
1639b1b221c8SJonathan Peyton #endif
1640b1b221c8SJonathan Peyton 
16417cc577a4SJonathan Peyton #if OMP_40_ENABLED
16427cc577a4SJonathan Peyton   }
16437cc577a4SJonathan Peyton #endif // OMP_40_ENABLED
16447cc577a4SJonathan Peyton 
16457cc577a4SJonathan Peyton 
16467cc577a4SJonathan Peyton #if OMP_45_ENABLED
16477cc577a4SJonathan Peyton   // Proxy tasks are not handled by the runtime
16487cc577a4SJonathan Peyton   if (taskdata->td_flags.proxy != TASK_PROXY) {
16497cc577a4SJonathan Peyton #endif
16507cc577a4SJonathan Peyton     ANNOTATE_HAPPENS_BEFORE(taskdata->td_parent);
165186ed6aa6SJoachim Protze #if OMPT_SUPPORT
165286ed6aa6SJoachim Protze     if (UNLIKELY(ompt_enabled.enabled)) {
165386ed6aa6SJoachim Protze       thread->th.ompt_thread_info = oldInfo;
165486ed6aa6SJoachim Protze       if (taskdata->td_flags.tiedness == TASK_TIED) {
16550e0d6cddSJoachim Protze         taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
165686ed6aa6SJoachim Protze       }
165786ed6aa6SJoachim Protze       __kmp_task_finish<true>(gtid, task, current_task);
165886ed6aa6SJoachim Protze     } else
165986ed6aa6SJoachim Protze #endif
166086ed6aa6SJoachim Protze       __kmp_task_finish<false>(gtid, task, current_task);
16617cc577a4SJonathan Peyton #if OMP_45_ENABLED
16627cc577a4SJonathan Peyton   }
16637cc577a4SJonathan Peyton #endif
16647cc577a4SJonathan Peyton 
16653041982dSJonathan Peyton   KA_TRACE(
16663041982dSJonathan Peyton       30,
16673041982dSJonathan Peyton       ("__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n",
16687cc577a4SJonathan Peyton        gtid, taskdata, current_task));
16697cc577a4SJonathan Peyton   return;
16707cc577a4SJonathan Peyton }
16717cc577a4SJonathan Peyton 
16727cc577a4SJonathan Peyton // __kmpc_omp_task_parts: Schedule a thread-switchable task for execution
16737cc577a4SJonathan Peyton //
16747cc577a4SJonathan Peyton // loc_ref: location of original task pragma (ignored)
16757cc577a4SJonathan Peyton // gtid: Global Thread ID of encountering thread
16767cc577a4SJonathan Peyton // new_task: task thunk allocated by __kmp_omp_task_alloc() for the ''new task''
16777cc577a4SJonathan Peyton // Returns:
16783041982dSJonathan Peyton //    TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to
16793041982dSJonathan Peyton //    be resumed later.
16803041982dSJonathan Peyton //    TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be
16813041982dSJonathan Peyton //    resumed later.
16823041982dSJonathan Peyton kmp_int32 __kmpc_omp_task_parts(ident_t *loc_ref, kmp_int32 gtid,
16833041982dSJonathan Peyton                                 kmp_task_t *new_task) {
16847cc577a4SJonathan Peyton   kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
16857cc577a4SJonathan Peyton 
16863041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n", gtid,
16873041982dSJonathan Peyton                 loc_ref, new_taskdata));
16887cc577a4SJonathan Peyton 
168982e94a59SJoachim Protze #if OMPT_SUPPORT
169082e94a59SJoachim Protze   kmp_taskdata_t *parent;
1691f0a1c65fSJonas Hahnfeld   if (UNLIKELY(ompt_enabled.enabled)) {
169282e94a59SJoachim Protze     parent = new_taskdata->td_parent;
169382e94a59SJoachim Protze     if (ompt_enabled.ompt_callback_task_create) {
169482e94a59SJoachim Protze       ompt_data_t task_data = ompt_data_none;
169582e94a59SJoachim Protze       ompt_callbacks.ompt_callback(ompt_callback_task_create)(
169682e94a59SJoachim Protze           parent ? &(parent->ompt_task_info.task_data) : &task_data,
169782e94a59SJoachim Protze           parent ? &(parent->ompt_task_info.frame) : NULL,
169882e94a59SJoachim Protze           &(new_taskdata->ompt_task_info.task_data), ompt_task_explicit, 0,
169982e94a59SJoachim Protze           OMPT_GET_RETURN_ADDRESS(0));
170082e94a59SJoachim Protze     }
170182e94a59SJoachim Protze   }
170282e94a59SJoachim Protze #endif
170382e94a59SJoachim Protze 
17043041982dSJonathan Peyton   /* Should we execute the new task or queue it? For now, let's just always try
17053041982dSJonathan Peyton      to queue it.  If the queue fills up, then we'll execute it.  */
17067cc577a4SJonathan Peyton 
17077cc577a4SJonathan Peyton   if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED) // if cannot defer
17087cc577a4SJonathan Peyton   { // Execute this task immediately
17097cc577a4SJonathan Peyton     kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
17107cc577a4SJonathan Peyton     new_taskdata->td_flags.task_serial = 1;
17117cc577a4SJonathan Peyton     __kmp_invoke_task(gtid, new_task, current_task);
17127cc577a4SJonathan Peyton   }
17137cc577a4SJonathan Peyton 
17143041982dSJonathan Peyton   KA_TRACE(
17153041982dSJonathan Peyton       10,
17163041982dSJonathan Peyton       ("__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: "
17173041982dSJonathan Peyton        "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
17183041982dSJonathan Peyton        gtid, loc_ref, new_taskdata));
17197cc577a4SJonathan Peyton 
17207cc577a4SJonathan Peyton   ANNOTATE_HAPPENS_BEFORE(new_task);
172182e94a59SJoachim Protze #if OMPT_SUPPORT
1722f0a1c65fSJonas Hahnfeld   if (UNLIKELY(ompt_enabled.enabled)) {
17230e0d6cddSJoachim Protze     parent->ompt_task_info.frame.enter_frame = ompt_data_none;
172482e94a59SJoachim Protze   }
172582e94a59SJoachim Protze #endif
17267cc577a4SJonathan Peyton   return TASK_CURRENT_NOT_QUEUED;
17277cc577a4SJonathan Peyton }
17287cc577a4SJonathan Peyton 
17297cc577a4SJonathan Peyton // __kmp_omp_task: Schedule a non-thread-switchable task for execution
17303041982dSJonathan Peyton //
17317cc577a4SJonathan Peyton // gtid: Global Thread ID of encountering thread
17327cc577a4SJonathan Peyton // new_task:non-thread-switchable task thunk allocated by __kmp_omp_task_alloc()
17333041982dSJonathan Peyton // serialize_immediate: if TRUE then if the task is executed immediately its
17343041982dSJonathan Peyton // execution will be serialized
17353041982dSJonathan Peyton // Returns:
17363041982dSJonathan Peyton //    TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to
17373041982dSJonathan Peyton //    be resumed later.
17383041982dSJonathan Peyton //    TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be
17393041982dSJonathan Peyton //    resumed later.
17403041982dSJonathan Peyton kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task,
17413041982dSJonathan Peyton                          bool serialize_immediate) {
17427cc577a4SJonathan Peyton   kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
17437cc577a4SJonathan Peyton 
17447cc577a4SJonathan Peyton /* Should we execute the new task or queue it? For now, let's just always try to
17457cc577a4SJonathan Peyton    queue it.  If the queue fills up, then we'll execute it.  */
17467cc577a4SJonathan Peyton #if OMP_45_ENABLED
17473041982dSJonathan Peyton   if (new_taskdata->td_flags.proxy == TASK_PROXY ||
17483041982dSJonathan Peyton       __kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED) // if cannot defer
17497cc577a4SJonathan Peyton #else
17507cc577a4SJonathan Peyton   if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED) // if cannot defer
17517cc577a4SJonathan Peyton #endif
17527cc577a4SJonathan Peyton   { // Execute this task immediately
17537cc577a4SJonathan Peyton     kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
17547cc577a4SJonathan Peyton     if (serialize_immediate)
17557cc577a4SJonathan Peyton       new_taskdata->td_flags.task_serial = 1;
17567cc577a4SJonathan Peyton     __kmp_invoke_task(gtid, new_task, current_task);
17577cc577a4SJonathan Peyton   }
17587cc577a4SJonathan Peyton 
17597cc577a4SJonathan Peyton   ANNOTATE_HAPPENS_BEFORE(new_task);
17607cc577a4SJonathan Peyton   return TASK_CURRENT_NOT_QUEUED;
17617cc577a4SJonathan Peyton }
17627cc577a4SJonathan Peyton 
17633041982dSJonathan Peyton // __kmpc_omp_task: Wrapper around __kmp_omp_task to schedule a
17643041982dSJonathan Peyton // non-thread-switchable task from the parent thread only!
17653041982dSJonathan Peyton //
17667cc577a4SJonathan Peyton // loc_ref: location of original task pragma (ignored)
17677cc577a4SJonathan Peyton // gtid: Global Thread ID of encountering thread
17683041982dSJonathan Peyton // new_task: non-thread-switchable task thunk allocated by
17693041982dSJonathan Peyton // __kmp_omp_task_alloc()
17703041982dSJonathan Peyton // Returns:
17713041982dSJonathan Peyton //    TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to
17723041982dSJonathan Peyton //    be resumed later.
17733041982dSJonathan Peyton //    TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be
17743041982dSJonathan Peyton //    resumed later.
17753041982dSJonathan Peyton kmp_int32 __kmpc_omp_task(ident_t *loc_ref, kmp_int32 gtid,
17763041982dSJonathan Peyton                           kmp_task_t *new_task) {
17777cc577a4SJonathan Peyton   kmp_int32 res;
17787cc577a4SJonathan Peyton   KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
17797cc577a4SJonathan Peyton 
178082e94a59SJoachim Protze #if KMP_DEBUG || OMPT_SUPPORT
17817cc577a4SJonathan Peyton   kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
17827cc577a4SJonathan Peyton #endif
17833041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
17843041982dSJonathan Peyton                 new_taskdata));
17857cc577a4SJonathan Peyton 
178682e94a59SJoachim Protze #if OMPT_SUPPORT
178782e94a59SJoachim Protze   kmp_taskdata_t *parent = NULL;
178886ed6aa6SJoachim Protze   if (UNLIKELY(ompt_enabled.enabled)) {
178986ed6aa6SJoachim Protze     if (!new_taskdata->td_flags.started) {
179082e94a59SJoachim Protze       OMPT_STORE_RETURN_ADDRESS(gtid);
179182e94a59SJoachim Protze       parent = new_taskdata->td_parent;
17920e0d6cddSJoachim Protze       if (!parent->ompt_task_info.frame.enter_frame.ptr) {
17930e0d6cddSJoachim Protze         parent->ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
179486ed6aa6SJoachim Protze       }
179582e94a59SJoachim Protze       if (ompt_enabled.ompt_callback_task_create) {
179682e94a59SJoachim Protze         ompt_data_t task_data = ompt_data_none;
179782e94a59SJoachim Protze         ompt_callbacks.ompt_callback(ompt_callback_task_create)(
179882e94a59SJoachim Protze             parent ? &(parent->ompt_task_info.task_data) : &task_data,
179982e94a59SJoachim Protze             parent ? &(parent->ompt_task_info.frame) : NULL,
180082e94a59SJoachim Protze             &(new_taskdata->ompt_task_info.task_data),
180182e94a59SJoachim Protze             ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
180282e94a59SJoachim Protze             OMPT_LOAD_RETURN_ADDRESS(gtid));
180382e94a59SJoachim Protze       }
180486ed6aa6SJoachim Protze     } else {
180586ed6aa6SJoachim Protze       // We are scheduling the continuation of an UNTIED task.
180686ed6aa6SJoachim Protze       // Scheduling back to the parent task.
180786ed6aa6SJoachim Protze       __ompt_task_finish(new_task,
180886ed6aa6SJoachim Protze                          new_taskdata->ompt_task_info.scheduling_parent,
1809489cdb78SJoachim Protze                          ompt_task_switch);
18100e0d6cddSJoachim Protze       new_taskdata->ompt_task_info.frame.exit_frame = ompt_data_none;
181186ed6aa6SJoachim Protze     }
181282e94a59SJoachim Protze   }
181382e94a59SJoachim Protze #endif
181482e94a59SJoachim Protze 
18157cc577a4SJonathan Peyton   res = __kmp_omp_task(gtid, new_task, true);
18167cc577a4SJonathan Peyton 
18173041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task(exit): T#%d returning "
18183041982dSJonathan Peyton                 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
18197cc577a4SJonathan Peyton                 gtid, loc_ref, new_taskdata));
182082e94a59SJoachim Protze #if OMPT_SUPPORT
1821f0a1c65fSJonas Hahnfeld   if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
18220e0d6cddSJoachim Protze     parent->ompt_task_info.frame.enter_frame = ompt_data_none;
182382e94a59SJoachim Protze   }
182482e94a59SJoachim Protze #endif
18257cc577a4SJonathan Peyton   return res;
18267cc577a4SJonathan Peyton }
18277cc577a4SJonathan Peyton 
1828cdaefac5SJoachim Protze // __kmp_omp_taskloop_task: Wrapper around __kmp_omp_task to schedule
1829cdaefac5SJoachim Protze // a taskloop task with the correct OMPT return address
1830cdaefac5SJoachim Protze //
1831cdaefac5SJoachim Protze // loc_ref: location of original task pragma (ignored)
1832cdaefac5SJoachim Protze // gtid: Global Thread ID of encountering thread
1833cdaefac5SJoachim Protze // new_task: non-thread-switchable task thunk allocated by
1834cdaefac5SJoachim Protze // __kmp_omp_task_alloc()
1835cdaefac5SJoachim Protze // codeptr_ra: return address for OMPT callback
1836cdaefac5SJoachim Protze // Returns:
1837cdaefac5SJoachim Protze //    TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to
1838cdaefac5SJoachim Protze //    be resumed later.
1839cdaefac5SJoachim Protze //    TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be
1840cdaefac5SJoachim Protze //    resumed later.
1841cdaefac5SJoachim Protze kmp_int32 __kmp_omp_taskloop_task(ident_t *loc_ref, kmp_int32 gtid,
1842cdaefac5SJoachim Protze                                   kmp_task_t *new_task, void *codeptr_ra) {
1843cdaefac5SJoachim Protze   kmp_int32 res;
1844cdaefac5SJoachim Protze   KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
1845cdaefac5SJoachim Protze 
1846cdaefac5SJoachim Protze #if KMP_DEBUG || OMPT_SUPPORT
1847cdaefac5SJoachim Protze   kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
1848cdaefac5SJoachim Protze #endif
1849cdaefac5SJoachim Protze   KA_TRACE(10, ("__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
1850cdaefac5SJoachim Protze                 new_taskdata));
1851cdaefac5SJoachim Protze 
1852cdaefac5SJoachim Protze #if OMPT_SUPPORT
1853cdaefac5SJoachim Protze   kmp_taskdata_t *parent = NULL;
1854cdaefac5SJoachim Protze   if (UNLIKELY(ompt_enabled.enabled && !new_taskdata->td_flags.started)) {
1855cdaefac5SJoachim Protze     parent = new_taskdata->td_parent;
18560e0d6cddSJoachim Protze     if (!parent->ompt_task_info.frame.enter_frame.ptr)
18570e0d6cddSJoachim Protze       parent->ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1858cdaefac5SJoachim Protze     if (ompt_enabled.ompt_callback_task_create) {
1859cdaefac5SJoachim Protze       ompt_data_t task_data = ompt_data_none;
1860cdaefac5SJoachim Protze       ompt_callbacks.ompt_callback(ompt_callback_task_create)(
1861cdaefac5SJoachim Protze           parent ? &(parent->ompt_task_info.task_data) : &task_data,
1862cdaefac5SJoachim Protze           parent ? &(parent->ompt_task_info.frame) : NULL,
1863cdaefac5SJoachim Protze           &(new_taskdata->ompt_task_info.task_data),
1864cdaefac5SJoachim Protze           ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0,
1865cdaefac5SJoachim Protze           codeptr_ra);
1866cdaefac5SJoachim Protze     }
1867cdaefac5SJoachim Protze   }
1868cdaefac5SJoachim Protze #endif
1869cdaefac5SJoachim Protze 
1870cdaefac5SJoachim Protze   res = __kmp_omp_task(gtid, new_task, true);
1871cdaefac5SJoachim Protze 
1872cdaefac5SJoachim Protze   KA_TRACE(10, ("__kmpc_omp_task(exit): T#%d returning "
1873cdaefac5SJoachim Protze                 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
1874cdaefac5SJoachim Protze                 gtid, loc_ref, new_taskdata));
1875cdaefac5SJoachim Protze #if OMPT_SUPPORT
1876cdaefac5SJoachim Protze   if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) {
18770e0d6cddSJoachim Protze     parent->ompt_task_info.frame.enter_frame = ompt_data_none;
1878cdaefac5SJoachim Protze   }
1879cdaefac5SJoachim Protze #endif
1880cdaefac5SJoachim Protze   return res;
1881cdaefac5SJoachim Protze }
1882cdaefac5SJoachim Protze 
188382e94a59SJoachim Protze template <bool ompt>
188482e94a59SJoachim Protze static kmp_int32 __kmpc_omp_taskwait_template(ident_t *loc_ref, kmp_int32 gtid,
188582e94a59SJoachim Protze                                               void *frame_address,
188682e94a59SJoachim Protze                                               void *return_address) {
18877cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata;
18887cc577a4SJonathan Peyton   kmp_info_t *thread;
18897cc577a4SJonathan Peyton   int thread_finished = FALSE;
18907cc577a4SJonathan Peyton   KMP_SET_THREAD_STATE_BLOCK(TASKWAIT);
18917cc577a4SJonathan Peyton 
18927cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref));
18937cc577a4SJonathan Peyton 
18947cc577a4SJonathan Peyton   if (__kmp_tasking_mode != tskm_immediate_exec) {
18957cc577a4SJonathan Peyton     thread = __kmp_threads[gtid];
18967cc577a4SJonathan Peyton     taskdata = thread->th.th_current_task;
18977cc577a4SJonathan Peyton 
189882e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
189982e94a59SJoachim Protze     ompt_data_t *my_task_data;
190082e94a59SJoachim Protze     ompt_data_t *my_parallel_data;
19017cc577a4SJonathan Peyton 
190282e94a59SJoachim Protze     if (ompt) {
190382e94a59SJoachim Protze       my_task_data = &(taskdata->ompt_task_info.task_data);
190482e94a59SJoachim Protze       my_parallel_data = OMPT_CUR_TEAM_DATA(thread);
190582e94a59SJoachim Protze 
19060e0d6cddSJoachim Protze       taskdata->ompt_task_info.frame.enter_frame.ptr = frame_address;
190782e94a59SJoachim Protze 
190882e94a59SJoachim Protze       if (ompt_enabled.ompt_callback_sync_region) {
190982e94a59SJoachim Protze         ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
191082e94a59SJoachim Protze             ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
191182e94a59SJoachim Protze             my_task_data, return_address);
191282e94a59SJoachim Protze       }
191382e94a59SJoachim Protze 
191482e94a59SJoachim Protze       if (ompt_enabled.ompt_callback_sync_region_wait) {
191582e94a59SJoachim Protze         ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
191682e94a59SJoachim Protze             ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data,
191782e94a59SJoachim Protze             my_task_data, return_address);
19187cc577a4SJonathan Peyton       }
19197cc577a4SJonathan Peyton     }
192082e94a59SJoachim Protze #endif // OMPT_SUPPORT && OMPT_OPTIONAL
19217cc577a4SJonathan Peyton 
19223041982dSJonathan Peyton // Debugger: The taskwait is active. Store location and thread encountered the
19233041982dSJonathan Peyton // taskwait.
19247cc577a4SJonathan Peyton #if USE_ITT_BUILD
19257cc577a4SJonathan Peyton // Note: These values are used by ITT events as well.
19267cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
19277cc577a4SJonathan Peyton     taskdata->td_taskwait_counter += 1;
19287cc577a4SJonathan Peyton     taskdata->td_taskwait_ident = loc_ref;
19297cc577a4SJonathan Peyton     taskdata->td_taskwait_thread = gtid + 1;
19307cc577a4SJonathan Peyton 
19317cc577a4SJonathan Peyton #if USE_ITT_BUILD
19327cc577a4SJonathan Peyton     void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
19337cc577a4SJonathan Peyton     if (itt_sync_obj != NULL)
19347cc577a4SJonathan Peyton       __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
19357cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
19367cc577a4SJonathan Peyton 
19373041982dSJonathan Peyton     bool must_wait =
19383041982dSJonathan Peyton         !taskdata->td_flags.team_serial && !taskdata->td_flags.final;
19397cc577a4SJonathan Peyton 
19407cc577a4SJonathan Peyton #if OMP_45_ENABLED
19413041982dSJonathan Peyton     must_wait = must_wait || (thread->th.th_task_team != NULL &&
19423041982dSJonathan Peyton                               thread->th.th_task_team->tt.tt_found_proxy_tasks);
19437cc577a4SJonathan Peyton #endif
19443041982dSJonathan Peyton     if (must_wait) {
194537e2ef54SJonathan Peyton       kmp_flag_32 flag(RCAST(std::atomic<kmp_uint32> *,
194637e2ef54SJonathan Peyton                              &(taskdata->td_incomplete_child_tasks)),
1947c47afcd9SAndrey Churbanov                        0U);
194837e2ef54SJonathan Peyton       while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) != 0) {
19493041982dSJonathan Peyton         flag.execute_tasks(thread, gtid, FALSE,
19503041982dSJonathan Peyton                            &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
19513041982dSJonathan Peyton                            __kmp_task_stealing_constraint);
19527cc577a4SJonathan Peyton       }
19537cc577a4SJonathan Peyton     }
19547cc577a4SJonathan Peyton #if USE_ITT_BUILD
19557cc577a4SJonathan Peyton     if (itt_sync_obj != NULL)
19567cc577a4SJonathan Peyton       __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
19577cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
19587cc577a4SJonathan Peyton 
19593041982dSJonathan Peyton     // Debugger:  The taskwait is completed. Location remains, but thread is
19603041982dSJonathan Peyton     // negated.
19617cc577a4SJonathan Peyton     taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
19627cc577a4SJonathan Peyton 
196382e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
196482e94a59SJoachim Protze     if (ompt) {
196582e94a59SJoachim Protze       if (ompt_enabled.ompt_callback_sync_region_wait) {
196682e94a59SJoachim Protze         ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
196782e94a59SJoachim Protze             ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
196882e94a59SJoachim Protze             my_task_data, return_address);
196982e94a59SJoachim Protze       }
197082e94a59SJoachim Protze       if (ompt_enabled.ompt_callback_sync_region) {
197182e94a59SJoachim Protze         ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
197282e94a59SJoachim Protze             ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data,
197382e94a59SJoachim Protze             my_task_data, return_address);
19747cc577a4SJonathan Peyton       }
19750e0d6cddSJoachim Protze       taskdata->ompt_task_info.frame.enter_frame = ompt_data_none;
19767cc577a4SJonathan Peyton     }
197782e94a59SJoachim Protze #endif // OMPT_SUPPORT && OMPT_OPTIONAL
197882e94a59SJoachim Protze 
19797cc577a4SJonathan Peyton     ANNOTATE_HAPPENS_AFTER(taskdata);
19807cc577a4SJonathan Peyton   }
19817cc577a4SJonathan Peyton 
19827cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, "
19833041982dSJonathan Peyton                 "returning TASK_CURRENT_NOT_QUEUED\n",
19843041982dSJonathan Peyton                 gtid, taskdata));
19857cc577a4SJonathan Peyton 
19867cc577a4SJonathan Peyton   return TASK_CURRENT_NOT_QUEUED;
19877cc577a4SJonathan Peyton }
19887cc577a4SJonathan Peyton 
1989489cdb78SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
199082e94a59SJoachim Protze OMPT_NOINLINE
199182e94a59SJoachim Protze static kmp_int32 __kmpc_omp_taskwait_ompt(ident_t *loc_ref, kmp_int32 gtid,
199282e94a59SJoachim Protze                                           void *frame_address,
199382e94a59SJoachim Protze                                           void *return_address) {
199482e94a59SJoachim Protze   return __kmpc_omp_taskwait_template<true>(loc_ref, gtid, frame_address,
199582e94a59SJoachim Protze                                             return_address);
199682e94a59SJoachim Protze }
1997489cdb78SJoachim Protze #endif // OMPT_SUPPORT && OMPT_OPTIONAL
199882e94a59SJoachim Protze 
199982e94a59SJoachim Protze // __kmpc_omp_taskwait: Wait until all tasks generated by the current task are
200082e94a59SJoachim Protze // complete
200182e94a59SJoachim Protze kmp_int32 __kmpc_omp_taskwait(ident_t *loc_ref, kmp_int32 gtid) {
200282e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
200382e94a59SJoachim Protze   if (UNLIKELY(ompt_enabled.enabled)) {
200482e94a59SJoachim Protze     OMPT_STORE_RETURN_ADDRESS(gtid);
20050e0d6cddSJoachim Protze     return __kmpc_omp_taskwait_ompt(loc_ref, gtid, OMPT_GET_FRAME_ADDRESS(0),
200682e94a59SJoachim Protze                                     OMPT_LOAD_RETURN_ADDRESS(gtid));
200782e94a59SJoachim Protze   }
200882e94a59SJoachim Protze #endif
200982e94a59SJoachim Protze   return __kmpc_omp_taskwait_template<false>(loc_ref, gtid, NULL, NULL);
201082e94a59SJoachim Protze }
201182e94a59SJoachim Protze 
20127cc577a4SJonathan Peyton // __kmpc_omp_taskyield: switch to a different task
20133041982dSJonathan Peyton kmp_int32 __kmpc_omp_taskyield(ident_t *loc_ref, kmp_int32 gtid, int end_part) {
20147cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata;
20157cc577a4SJonathan Peyton   kmp_info_t *thread;
20167cc577a4SJonathan Peyton   int thread_finished = FALSE;
20177cc577a4SJonathan Peyton 
20187cc577a4SJonathan Peyton   KMP_COUNT_BLOCK(OMP_TASKYIELD);
20197cc577a4SJonathan Peyton   KMP_SET_THREAD_STATE_BLOCK(TASKYIELD);
20207cc577a4SJonathan Peyton 
20217cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n",
20227cc577a4SJonathan Peyton                 gtid, loc_ref, end_part));
20237cc577a4SJonathan Peyton 
20247cc577a4SJonathan Peyton   if (__kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel) {
20257cc577a4SJonathan Peyton     thread = __kmp_threads[gtid];
20267cc577a4SJonathan Peyton     taskdata = thread->th.th_current_task;
20277cc577a4SJonathan Peyton // Should we model this as a task wait or not?
20283041982dSJonathan Peyton // Debugger: The taskwait is active. Store location and thread encountered the
20293041982dSJonathan Peyton // taskwait.
20307cc577a4SJonathan Peyton #if USE_ITT_BUILD
20317cc577a4SJonathan Peyton // Note: These values are used by ITT events as well.
20327cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
20337cc577a4SJonathan Peyton     taskdata->td_taskwait_counter += 1;
20347cc577a4SJonathan Peyton     taskdata->td_taskwait_ident = loc_ref;
20357cc577a4SJonathan Peyton     taskdata->td_taskwait_thread = gtid + 1;
20367cc577a4SJonathan Peyton 
20377cc577a4SJonathan Peyton #if USE_ITT_BUILD
20387cc577a4SJonathan Peyton     void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
20397cc577a4SJonathan Peyton     if (itt_sync_obj != NULL)
20407cc577a4SJonathan Peyton       __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
20417cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
20427cc577a4SJonathan Peyton     if (!taskdata->td_flags.team_serial) {
20437cc577a4SJonathan Peyton       kmp_task_team_t *task_team = thread->th.th_task_team;
20447cc577a4SJonathan Peyton       if (task_team != NULL) {
20457cc577a4SJonathan Peyton         if (KMP_TASKING_ENABLED(task_team)) {
204682e94a59SJoachim Protze #if OMPT_SUPPORT
2047f0a1c65fSJonas Hahnfeld           if (UNLIKELY(ompt_enabled.enabled))
204882e94a59SJoachim Protze             thread->th.ompt_thread_info.ompt_task_yielded = 1;
204982e94a59SJoachim Protze #endif
20503041982dSJonathan Peyton           __kmp_execute_tasks_32(
20513041982dSJonathan Peyton               thread, gtid, NULL, FALSE,
20523041982dSJonathan Peyton               &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
20533041982dSJonathan Peyton               __kmp_task_stealing_constraint);
205482e94a59SJoachim Protze #if OMPT_SUPPORT
2055f0a1c65fSJonas Hahnfeld           if (UNLIKELY(ompt_enabled.enabled))
205682e94a59SJoachim Protze             thread->th.ompt_thread_info.ompt_task_yielded = 0;
205782e94a59SJoachim Protze #endif
20587cc577a4SJonathan Peyton         }
20597cc577a4SJonathan Peyton       }
20607cc577a4SJonathan Peyton     }
20617cc577a4SJonathan Peyton #if USE_ITT_BUILD
20627cc577a4SJonathan Peyton     if (itt_sync_obj != NULL)
20637cc577a4SJonathan Peyton       __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
20647cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
20657cc577a4SJonathan Peyton 
20663041982dSJonathan Peyton     // Debugger:  The taskwait is completed. Location remains, but thread is
20673041982dSJonathan Peyton     // negated.
20687cc577a4SJonathan Peyton     taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
20697cc577a4SJonathan Peyton   }
20707cc577a4SJonathan Peyton 
20717cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_taskyield(exit): T#%d task %p resuming, "
20723041982dSJonathan Peyton                 "returning TASK_CURRENT_NOT_QUEUED\n",
20733041982dSJonathan Peyton                 gtid, taskdata));
20747cc577a4SJonathan Peyton 
20757cc577a4SJonathan Peyton   return TASK_CURRENT_NOT_QUEUED;
20767cc577a4SJonathan Peyton }
20777cc577a4SJonathan Peyton 
207883e360a4SJonathan Peyton #if OMP_50_ENABLED
207972ba2109SAndrey Churbanov // Task Reduction implementation
2080a8426ac8SJonathan Peyton //
2081a8426ac8SJonathan Peyton // Note: initial implementation didn't take into account the possibility
2082a8426ac8SJonathan Peyton // to specify omp_orig for initializer of the UDR (user defined reduction).
2083a8426ac8SJonathan Peyton // Corrected implementation takes into account the omp_orig object.
2084a8426ac8SJonathan Peyton // Compiler is free to use old implementation if omp_orig is not specified.
208572ba2109SAndrey Churbanov 
2086a8426ac8SJonathan Peyton /*!
2087a8426ac8SJonathan Peyton @ingroup BASIC_TYPES
2088a8426ac8SJonathan Peyton @{
2089a8426ac8SJonathan Peyton */
2090a8426ac8SJonathan Peyton 
2091a8426ac8SJonathan Peyton /*!
2092a8426ac8SJonathan Peyton Flags for special info per task reduction item.
2093a8426ac8SJonathan Peyton */
2094a8426ac8SJonathan Peyton typedef struct kmp_taskred_flags {
2095a8426ac8SJonathan Peyton   /*! 1 - use lazy alloc/init (e.g. big objects, #tasks < #threads) */
2096a8426ac8SJonathan Peyton   unsigned lazy_priv : 1;
209772ba2109SAndrey Churbanov   unsigned reserved31 : 31;
2098a8426ac8SJonathan Peyton } kmp_taskred_flags_t;
209972ba2109SAndrey Churbanov 
2100a8426ac8SJonathan Peyton /*!
2101a8426ac8SJonathan Peyton Internal struct for reduction data item related info set up by compiler.
2102a8426ac8SJonathan Peyton */
210372ba2109SAndrey Churbanov typedef struct kmp_task_red_input {
2104a8426ac8SJonathan Peyton   void *reduce_shar; /**< shared between tasks item to reduce into */
2105a8426ac8SJonathan Peyton   size_t reduce_size; /**< size of data item in bytes */
2106a8426ac8SJonathan Peyton   // three compiler-generated routines (init, fini are optional):
2107a8426ac8SJonathan Peyton   void *reduce_init; /**< data initialization routine (single parameter) */
2108a8426ac8SJonathan Peyton   void *reduce_fini; /**< data finalization routine */
2109a8426ac8SJonathan Peyton   void *reduce_comb; /**< data combiner routine */
2110a8426ac8SJonathan Peyton   kmp_taskred_flags_t flags; /**< flags for additional info from compiler */
211172ba2109SAndrey Churbanov } kmp_task_red_input_t;
211272ba2109SAndrey Churbanov 
211372ba2109SAndrey Churbanov /*!
2114a8426ac8SJonathan Peyton Internal struct for reduction data item related info saved by the library.
211572ba2109SAndrey Churbanov */
2116a8426ac8SJonathan Peyton typedef struct kmp_taskred_data {
2117a8426ac8SJonathan Peyton   void *reduce_shar; /**< shared between tasks item to reduce into */
2118a8426ac8SJonathan Peyton   size_t reduce_size; /**< size of data item */
2119a8426ac8SJonathan Peyton   kmp_taskred_flags_t flags; /**< flags for additional info from compiler */
2120a8426ac8SJonathan Peyton   void *reduce_priv; /**< array of thread specific items */
2121a8426ac8SJonathan Peyton   void *reduce_pend; /**< end of private data for faster comparison op */
2122a8426ac8SJonathan Peyton   // three compiler-generated routines (init, fini are optional):
2123a8426ac8SJonathan Peyton   void *reduce_comb; /**< data combiner routine */
2124a8426ac8SJonathan Peyton   void *reduce_init; /**< data initialization routine (two parameters) */
2125a8426ac8SJonathan Peyton   void *reduce_fini; /**< data finalization routine */
2126a8426ac8SJonathan Peyton   void *reduce_orig; /**< original item (can be used in UDR initializer) */
2127a8426ac8SJonathan Peyton } kmp_taskred_data_t;
2128a8426ac8SJonathan Peyton 
2129a8426ac8SJonathan Peyton /*!
2130a8426ac8SJonathan Peyton Internal struct for reduction data item related info set up by compiler.
2131a8426ac8SJonathan Peyton 
2132a8426ac8SJonathan Peyton New interface: added reduce_orig field to provide omp_orig for UDR initializer.
2133a8426ac8SJonathan Peyton */
2134a8426ac8SJonathan Peyton typedef struct kmp_taskred_input {
2135a8426ac8SJonathan Peyton   void *reduce_shar; /**< shared between tasks item to reduce into */
2136a8426ac8SJonathan Peyton   void *reduce_orig; /**< original reduction item used for initialization */
2137a8426ac8SJonathan Peyton   size_t reduce_size; /**< size of data item */
2138a8426ac8SJonathan Peyton   // three compiler-generated routines (init, fini are optional):
2139a8426ac8SJonathan Peyton   void *reduce_init; /**< data initialization routine (two parameters) */
2140a8426ac8SJonathan Peyton   void *reduce_fini; /**< data finalization routine */
2141a8426ac8SJonathan Peyton   void *reduce_comb; /**< data combiner routine */
2142a8426ac8SJonathan Peyton   kmp_taskred_flags_t flags; /**< flags for additional info from compiler */
2143a8426ac8SJonathan Peyton } kmp_taskred_input_t;
2144a8426ac8SJonathan Peyton /*!
2145a8426ac8SJonathan Peyton @}
2146a8426ac8SJonathan Peyton */
2147a8426ac8SJonathan Peyton 
2148a8426ac8SJonathan Peyton template <typename T> void __kmp_assign_orig(kmp_taskred_data_t &item, T &src);
2149a8426ac8SJonathan Peyton template <>
2150a8426ac8SJonathan Peyton void __kmp_assign_orig<kmp_task_red_input_t>(kmp_taskred_data_t &item,
2151a8426ac8SJonathan Peyton                                              kmp_task_red_input_t &src) {
2152a8426ac8SJonathan Peyton   item.reduce_orig = NULL;
2153a8426ac8SJonathan Peyton }
2154a8426ac8SJonathan Peyton template <>
2155a8426ac8SJonathan Peyton void __kmp_assign_orig<kmp_taskred_input_t>(kmp_taskred_data_t &item,
2156a8426ac8SJonathan Peyton                                             kmp_taskred_input_t &src) {
2157a8426ac8SJonathan Peyton   if (src.reduce_orig != NULL) {
2158a8426ac8SJonathan Peyton     item.reduce_orig = src.reduce_orig;
2159a8426ac8SJonathan Peyton   } else {
2160a8426ac8SJonathan Peyton     item.reduce_orig = src.reduce_shar;
2161a8426ac8SJonathan Peyton   } // non-NULL reduce_orig means new interface used
2162a8426ac8SJonathan Peyton }
2163a8426ac8SJonathan Peyton 
2164a8426ac8SJonathan Peyton template <typename T> void __kmp_call_init(kmp_taskred_data_t &item, int j);
2165a8426ac8SJonathan Peyton template <>
2166a8426ac8SJonathan Peyton void __kmp_call_init<kmp_task_red_input_t>(kmp_taskred_data_t &item,
2167a8426ac8SJonathan Peyton                                            int offset) {
2168a8426ac8SJonathan Peyton   ((void (*)(void *))item.reduce_init)((char *)(item.reduce_priv) + offset);
2169a8426ac8SJonathan Peyton }
2170a8426ac8SJonathan Peyton template <>
2171a8426ac8SJonathan Peyton void __kmp_call_init<kmp_taskred_input_t>(kmp_taskred_data_t &item,
2172a8426ac8SJonathan Peyton                                           int offset) {
2173a8426ac8SJonathan Peyton   ((void (*)(void *, void *))item.reduce_init)(
2174a8426ac8SJonathan Peyton       (char *)(item.reduce_priv) + offset, item.reduce_orig);
2175a8426ac8SJonathan Peyton }
2176a8426ac8SJonathan Peyton 
2177a8426ac8SJonathan Peyton template <typename T>
2178a8426ac8SJonathan Peyton void *__kmp_task_reduction_init(int gtid, int num, T *data) {
217972ba2109SAndrey Churbanov   kmp_info_t *thread = __kmp_threads[gtid];
218072ba2109SAndrey Churbanov   kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup;
218172ba2109SAndrey Churbanov   kmp_int32 nth = thread->th.th_team_nproc;
2182a8426ac8SJonathan Peyton   kmp_taskred_data_t *arr;
218372ba2109SAndrey Churbanov 
218472ba2109SAndrey Churbanov   // check input data just in case
218572ba2109SAndrey Churbanov   KMP_ASSERT(tg != NULL);
218672ba2109SAndrey Churbanov   KMP_ASSERT(data != NULL);
218772ba2109SAndrey Churbanov   KMP_ASSERT(num > 0);
218872ba2109SAndrey Churbanov   if (nth == 1) {
218972ba2109SAndrey Churbanov     KA_TRACE(10, ("__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n",
219072ba2109SAndrey Churbanov                   gtid, tg));
219172ba2109SAndrey Churbanov     return (void *)tg;
219272ba2109SAndrey Churbanov   }
219372ba2109SAndrey Churbanov   KA_TRACE(10, ("__kmpc_task_reduction_init: T#%d, taskgroup %p, #items %d\n",
219472ba2109SAndrey Churbanov                 gtid, tg, num));
2195a8426ac8SJonathan Peyton   arr = (kmp_taskred_data_t *)__kmp_thread_malloc(
2196a8426ac8SJonathan Peyton       thread, num * sizeof(kmp_taskred_data_t));
219772ba2109SAndrey Churbanov   for (int i = 0; i < num; ++i) {
2198a8426ac8SJonathan Peyton     size_t size = data[i].reduce_size - 1;
219972ba2109SAndrey Churbanov     // round the size up to cache line per thread-specific item
220072ba2109SAndrey Churbanov     size += CACHE_LINE - size % CACHE_LINE;
2201a8426ac8SJonathan Peyton     KMP_ASSERT(data[i].reduce_comb != NULL); // combiner is mandatory
2202a8426ac8SJonathan Peyton     arr[i].reduce_shar = data[i].reduce_shar;
220372ba2109SAndrey Churbanov     arr[i].reduce_size = size;
2204a8426ac8SJonathan Peyton     arr[i].flags = data[i].flags;
2205a8426ac8SJonathan Peyton     arr[i].reduce_comb = data[i].reduce_comb;
2206a8426ac8SJonathan Peyton     arr[i].reduce_init = data[i].reduce_init;
2207a8426ac8SJonathan Peyton     arr[i].reduce_fini = data[i].reduce_fini;
2208a8426ac8SJonathan Peyton     __kmp_assign_orig<T>(arr[i], data[i]);
2209a8426ac8SJonathan Peyton     if (!arr[i].flags.lazy_priv) {
221072ba2109SAndrey Churbanov       // allocate cache-line aligned block and fill it with zeros
221172ba2109SAndrey Churbanov       arr[i].reduce_priv = __kmp_allocate(nth * size);
221272ba2109SAndrey Churbanov       arr[i].reduce_pend = (char *)(arr[i].reduce_priv) + nth * size;
2213a8426ac8SJonathan Peyton       if (arr[i].reduce_init != NULL) {
2214a8426ac8SJonathan Peyton         // initialize all thread-specific items
221572ba2109SAndrey Churbanov         for (int j = 0; j < nth; ++j) {
2216a8426ac8SJonathan Peyton           __kmp_call_init<T>(arr[i], j * size);
221772ba2109SAndrey Churbanov         }
221872ba2109SAndrey Churbanov       }
221972ba2109SAndrey Churbanov     } else {
222072ba2109SAndrey Churbanov       // only allocate space for pointers now,
2221a8426ac8SJonathan Peyton       // objects will be lazily allocated/initialized if/when requested
2222a8426ac8SJonathan Peyton       // note that __kmp_allocate zeroes the allocated memory
222372ba2109SAndrey Churbanov       arr[i].reduce_priv = __kmp_allocate(nth * sizeof(void *));
222472ba2109SAndrey Churbanov     }
222572ba2109SAndrey Churbanov   }
222672ba2109SAndrey Churbanov   tg->reduce_data = (void *)arr;
222772ba2109SAndrey Churbanov   tg->reduce_num_data = num;
222872ba2109SAndrey Churbanov   return (void *)tg;
222972ba2109SAndrey Churbanov }
223072ba2109SAndrey Churbanov 
223172ba2109SAndrey Churbanov /*!
223272ba2109SAndrey Churbanov @ingroup TASKING
223372ba2109SAndrey Churbanov @param gtid      Global thread ID
2234a8426ac8SJonathan Peyton @param num       Number of data items to reduce
2235a8426ac8SJonathan Peyton @param data      Array of data for reduction
2236a8426ac8SJonathan Peyton @return The taskgroup identifier
2237a8426ac8SJonathan Peyton 
2238a8426ac8SJonathan Peyton Initialize task reduction for the taskgroup.
2239a8426ac8SJonathan Peyton 
2240a8426ac8SJonathan Peyton Note: this entry supposes the optional compiler-generated initializer routine
2241a8426ac8SJonathan Peyton has single parameter - pointer to object to be initialized. That means
2242a8426ac8SJonathan Peyton the reduction either does not use omp_orig object, or the omp_orig is accessible
2243a8426ac8SJonathan Peyton without help of the runtime library.
2244a8426ac8SJonathan Peyton */
2245a8426ac8SJonathan Peyton void *__kmpc_task_reduction_init(int gtid, int num, void *data) {
2246a8426ac8SJonathan Peyton   return __kmp_task_reduction_init(gtid, num, (kmp_task_red_input_t *)data);
2247a8426ac8SJonathan Peyton }
2248a8426ac8SJonathan Peyton 
2249a8426ac8SJonathan Peyton /*!
2250a8426ac8SJonathan Peyton @ingroup TASKING
2251a8426ac8SJonathan Peyton @param gtid      Global thread ID
2252a8426ac8SJonathan Peyton @param num       Number of data items to reduce
2253a8426ac8SJonathan Peyton @param data      Array of data for reduction
2254a8426ac8SJonathan Peyton @return The taskgroup identifier
2255a8426ac8SJonathan Peyton 
2256a8426ac8SJonathan Peyton Initialize task reduction for the taskgroup.
2257a8426ac8SJonathan Peyton 
2258a8426ac8SJonathan Peyton Note: this entry supposes the optional compiler-generated initializer routine
2259a8426ac8SJonathan Peyton has two parameters, pointer to object to be initialized and pointer to omp_orig
2260a8426ac8SJonathan Peyton */
2261a8426ac8SJonathan Peyton void *__kmpc_taskred_init(int gtid, int num, void *data) {
2262a8426ac8SJonathan Peyton   return __kmp_task_reduction_init(gtid, num, (kmp_taskred_input_t *)data);
2263a8426ac8SJonathan Peyton }
2264a8426ac8SJonathan Peyton 
2265a8426ac8SJonathan Peyton // Copy task reduction data (except for shared pointers).
2266a8426ac8SJonathan Peyton template <typename T>
2267a8426ac8SJonathan Peyton void __kmp_task_reduction_init_copy(kmp_info_t *thr, int num, T *data,
2268a8426ac8SJonathan Peyton                                     kmp_taskgroup_t *tg, void *reduce_data) {
2269a8426ac8SJonathan Peyton   kmp_taskred_data_t *arr;
2270a8426ac8SJonathan Peyton   KA_TRACE(20, ("__kmp_task_reduction_init_copy: Th %p, init taskgroup %p,"
2271a8426ac8SJonathan Peyton                 " from data %p\n",
2272a8426ac8SJonathan Peyton                 thr, tg, reduce_data));
2273a8426ac8SJonathan Peyton   arr = (kmp_taskred_data_t *)__kmp_thread_malloc(
2274a8426ac8SJonathan Peyton       thr, num * sizeof(kmp_taskred_data_t));
2275a8426ac8SJonathan Peyton   // threads will share private copies, thunk routines, sizes, flags, etc.:
2276a8426ac8SJonathan Peyton   KMP_MEMCPY(arr, reduce_data, num * sizeof(kmp_taskred_data_t));
2277a8426ac8SJonathan Peyton   for (int i = 0; i < num; ++i) {
2278a8426ac8SJonathan Peyton     arr[i].reduce_shar = data[i].reduce_shar; // init unique shared pointers
2279a8426ac8SJonathan Peyton   }
2280a8426ac8SJonathan Peyton   tg->reduce_data = (void *)arr;
2281a8426ac8SJonathan Peyton   tg->reduce_num_data = num;
2282a8426ac8SJonathan Peyton }
2283a8426ac8SJonathan Peyton 
2284a8426ac8SJonathan Peyton /*!
2285a8426ac8SJonathan Peyton @ingroup TASKING
2286a8426ac8SJonathan Peyton @param gtid    Global thread ID
228772ba2109SAndrey Churbanov @param tskgrp  The taskgroup ID (optional)
228872ba2109SAndrey Churbanov @param data    Shared location of the item
228972ba2109SAndrey Churbanov @return The pointer to per-thread data
229072ba2109SAndrey Churbanov 
229172ba2109SAndrey Churbanov Get thread-specific location of data item
229272ba2109SAndrey Churbanov */
22933041982dSJonathan Peyton void *__kmpc_task_reduction_get_th_data(int gtid, void *tskgrp, void *data) {
229472ba2109SAndrey Churbanov   kmp_info_t *thread = __kmp_threads[gtid];
229572ba2109SAndrey Churbanov   kmp_int32 nth = thread->th.th_team_nproc;
229672ba2109SAndrey Churbanov   if (nth == 1)
229772ba2109SAndrey Churbanov     return data; // nothing to do
229872ba2109SAndrey Churbanov 
229972ba2109SAndrey Churbanov   kmp_taskgroup_t *tg = (kmp_taskgroup_t *)tskgrp;
230072ba2109SAndrey Churbanov   if (tg == NULL)
230172ba2109SAndrey Churbanov     tg = thread->th.th_current_task->td_taskgroup;
230272ba2109SAndrey Churbanov   KMP_ASSERT(tg != NULL);
2303a8426ac8SJonathan Peyton   kmp_taskred_data_t *arr = (kmp_taskred_data_t *)(tg->reduce_data);
230472ba2109SAndrey Churbanov   kmp_int32 num = tg->reduce_num_data;
230572ba2109SAndrey Churbanov   kmp_int32 tid = thread->th.th_info.ds.ds_tid;
230672ba2109SAndrey Churbanov 
230772ba2109SAndrey Churbanov   KMP_ASSERT(data != NULL);
230872ba2109SAndrey Churbanov   while (tg != NULL) {
230972ba2109SAndrey Churbanov     for (int i = 0; i < num; ++i) {
231072ba2109SAndrey Churbanov       if (!arr[i].flags.lazy_priv) {
231172ba2109SAndrey Churbanov         if (data == arr[i].reduce_shar ||
231272ba2109SAndrey Churbanov             (data >= arr[i].reduce_priv && data < arr[i].reduce_pend))
231372ba2109SAndrey Churbanov           return (char *)(arr[i].reduce_priv) + tid * arr[i].reduce_size;
231472ba2109SAndrey Churbanov       } else {
231572ba2109SAndrey Churbanov         // check shared location first
231672ba2109SAndrey Churbanov         void **p_priv = (void **)(arr[i].reduce_priv);
231772ba2109SAndrey Churbanov         if (data == arr[i].reduce_shar)
231872ba2109SAndrey Churbanov           goto found;
231972ba2109SAndrey Churbanov         // check if we get some thread specific location as parameter
232072ba2109SAndrey Churbanov         for (int j = 0; j < nth; ++j)
232172ba2109SAndrey Churbanov           if (data == p_priv[j])
232272ba2109SAndrey Churbanov             goto found;
232372ba2109SAndrey Churbanov         continue; // not found, continue search
232472ba2109SAndrey Churbanov       found:
232572ba2109SAndrey Churbanov         if (p_priv[tid] == NULL) {
232672ba2109SAndrey Churbanov           // allocate thread specific object lazily
232772ba2109SAndrey Churbanov           p_priv[tid] = __kmp_allocate(arr[i].reduce_size);
2328a8426ac8SJonathan Peyton           if (arr[i].reduce_init != NULL) {
2329a8426ac8SJonathan Peyton             if (arr[i].reduce_orig != NULL) { // new interface
2330a8426ac8SJonathan Peyton               ((void (*)(void *, void *))arr[i].reduce_init)(
2331a8426ac8SJonathan Peyton                   p_priv[tid], arr[i].reduce_orig);
2332a8426ac8SJonathan Peyton             } else { // old interface (single parameter)
2333a8426ac8SJonathan Peyton               ((void (*)(void *))arr[i].reduce_init)(p_priv[tid]);
2334a8426ac8SJonathan Peyton             }
233572ba2109SAndrey Churbanov           }
233672ba2109SAndrey Churbanov         }
233772ba2109SAndrey Churbanov         return p_priv[tid];
233872ba2109SAndrey Churbanov       }
233972ba2109SAndrey Churbanov     }
234072ba2109SAndrey Churbanov     tg = tg->parent;
2341a8426ac8SJonathan Peyton     arr = (kmp_taskred_data_t *)(tg->reduce_data);
234272ba2109SAndrey Churbanov     num = tg->reduce_num_data;
234372ba2109SAndrey Churbanov   }
234472ba2109SAndrey Churbanov   KMP_ASSERT2(0, "Unknown task reduction item");
234572ba2109SAndrey Churbanov   return NULL; // ERROR, this line never executed
234672ba2109SAndrey Churbanov }
234772ba2109SAndrey Churbanov 
234872ba2109SAndrey Churbanov // Finalize task reduction.
234972ba2109SAndrey Churbanov // Called from __kmpc_end_taskgroup()
23503041982dSJonathan Peyton static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg) {
235172ba2109SAndrey Churbanov   kmp_int32 nth = th->th.th_team_nproc;
235272ba2109SAndrey Churbanov   KMP_DEBUG_ASSERT(nth > 1); // should not be called if nth == 1
2353a8426ac8SJonathan Peyton   kmp_taskred_data_t *arr = (kmp_taskred_data_t *)tg->reduce_data;
235472ba2109SAndrey Churbanov   kmp_int32 num = tg->reduce_num_data;
235572ba2109SAndrey Churbanov   for (int i = 0; i < num; ++i) {
235672ba2109SAndrey Churbanov     void *sh_data = arr[i].reduce_shar;
235772ba2109SAndrey Churbanov     void (*f_fini)(void *) = (void (*)(void *))(arr[i].reduce_fini);
23583041982dSJonathan Peyton     void (*f_comb)(void *, void *) =
23593041982dSJonathan Peyton         (void (*)(void *, void *))(arr[i].reduce_comb);
236072ba2109SAndrey Churbanov     if (!arr[i].flags.lazy_priv) {
236172ba2109SAndrey Churbanov       void *pr_data = arr[i].reduce_priv;
236272ba2109SAndrey Churbanov       size_t size = arr[i].reduce_size;
236372ba2109SAndrey Churbanov       for (int j = 0; j < nth; ++j) {
236472ba2109SAndrey Churbanov         void *priv_data = (char *)pr_data + j * size;
236572ba2109SAndrey Churbanov         f_comb(sh_data, priv_data); // combine results
236672ba2109SAndrey Churbanov         if (f_fini)
236772ba2109SAndrey Churbanov           f_fini(priv_data); // finalize if needed
236872ba2109SAndrey Churbanov       }
236972ba2109SAndrey Churbanov     } else {
237072ba2109SAndrey Churbanov       void **pr_data = (void **)(arr[i].reduce_priv);
237172ba2109SAndrey Churbanov       for (int j = 0; j < nth; ++j) {
237272ba2109SAndrey Churbanov         if (pr_data[j] != NULL) {
237372ba2109SAndrey Churbanov           f_comb(sh_data, pr_data[j]); // combine results
237472ba2109SAndrey Churbanov           if (f_fini)
237572ba2109SAndrey Churbanov             f_fini(pr_data[j]); // finalize if needed
237672ba2109SAndrey Churbanov           __kmp_free(pr_data[j]);
237772ba2109SAndrey Churbanov         }
237872ba2109SAndrey Churbanov       }
237972ba2109SAndrey Churbanov     }
238072ba2109SAndrey Churbanov     __kmp_free(arr[i].reduce_priv);
238172ba2109SAndrey Churbanov   }
238272ba2109SAndrey Churbanov   __kmp_thread_free(th, arr);
238372ba2109SAndrey Churbanov   tg->reduce_data = NULL;
238472ba2109SAndrey Churbanov   tg->reduce_num_data = 0;
238572ba2109SAndrey Churbanov }
2386a8426ac8SJonathan Peyton 
2387a8426ac8SJonathan Peyton // Cleanup task reduction data for parallel or worksharing,
2388a8426ac8SJonathan Peyton // do not touch task private data other threads still working with.
2389a8426ac8SJonathan Peyton // Called from __kmpc_end_taskgroup()
2390a8426ac8SJonathan Peyton static void __kmp_task_reduction_clean(kmp_info_t *th, kmp_taskgroup_t *tg) {
2391a8426ac8SJonathan Peyton   __kmp_thread_free(th, tg->reduce_data);
2392a8426ac8SJonathan Peyton   tg->reduce_data = NULL;
2393a8426ac8SJonathan Peyton   tg->reduce_num_data = 0;
2394a8426ac8SJonathan Peyton }
2395a8426ac8SJonathan Peyton 
2396a8426ac8SJonathan Peyton template <typename T>
2397a8426ac8SJonathan Peyton void *__kmp_task_reduction_modifier_init(ident_t *loc, int gtid, int is_ws,
2398a8426ac8SJonathan Peyton                                          int num, T *data) {
2399a8426ac8SJonathan Peyton   kmp_info_t *thr = __kmp_threads[gtid];
2400a8426ac8SJonathan Peyton   kmp_int32 nth = thr->th.th_team_nproc;
2401a8426ac8SJonathan Peyton   __kmpc_taskgroup(loc, gtid); // form new taskgroup first
2402a8426ac8SJonathan Peyton   if (nth == 1) {
2403a8426ac8SJonathan Peyton     KA_TRACE(10,
2404a8426ac8SJonathan Peyton              ("__kmpc_reduction_modifier_init: T#%d, tg %p, exiting nth=1\n",
2405a8426ac8SJonathan Peyton               gtid, thr->th.th_current_task->td_taskgroup));
2406a8426ac8SJonathan Peyton     return (void *)thr->th.th_current_task->td_taskgroup;
2407a8426ac8SJonathan Peyton   }
2408a8426ac8SJonathan Peyton   kmp_team_t *team = thr->th.th_team;
2409a8426ac8SJonathan Peyton   void *reduce_data;
2410a8426ac8SJonathan Peyton   kmp_taskgroup_t *tg;
2411a8426ac8SJonathan Peyton   reduce_data = KMP_ATOMIC_LD_RLX(&team->t.t_tg_reduce_data[is_ws]);
2412a8426ac8SJonathan Peyton   if (reduce_data == NULL &&
2413a8426ac8SJonathan Peyton       __kmp_atomic_compare_store(&team->t.t_tg_reduce_data[is_ws], reduce_data,
2414a8426ac8SJonathan Peyton                                  (void *)1)) {
2415a8426ac8SJonathan Peyton     // single thread enters this block to initialize common reduction data
2416a8426ac8SJonathan Peyton     KMP_DEBUG_ASSERT(reduce_data == NULL);
2417a8426ac8SJonathan Peyton     // first initialize own data, then make a copy other threads can use
2418a8426ac8SJonathan Peyton     tg = (kmp_taskgroup_t *)__kmp_task_reduction_init<T>(gtid, num, data);
2419a8426ac8SJonathan Peyton     reduce_data = __kmp_thread_malloc(thr, num * sizeof(kmp_taskred_data_t));
2420a8426ac8SJonathan Peyton     KMP_MEMCPY(reduce_data, tg->reduce_data, num * sizeof(kmp_taskred_data_t));
2421a8426ac8SJonathan Peyton     // fini counters should be 0 at this point
2422a8426ac8SJonathan Peyton     KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[0]) == 0);
2423a8426ac8SJonathan Peyton     KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[1]) == 0);
2424a8426ac8SJonathan Peyton     KMP_ATOMIC_ST_REL(&team->t.t_tg_reduce_data[is_ws], reduce_data);
2425a8426ac8SJonathan Peyton   } else {
2426a8426ac8SJonathan Peyton     while (
2427a8426ac8SJonathan Peyton         (reduce_data = KMP_ATOMIC_LD_ACQ(&team->t.t_tg_reduce_data[is_ws])) ==
2428a8426ac8SJonathan Peyton         (void *)1) { // wait for task reduction initialization
2429a8426ac8SJonathan Peyton       KMP_CPU_PAUSE();
2430a8426ac8SJonathan Peyton     }
2431a8426ac8SJonathan Peyton     KMP_DEBUG_ASSERT(reduce_data > (void *)1); // should be valid pointer here
2432a8426ac8SJonathan Peyton     tg = thr->th.th_current_task->td_taskgroup;
2433a8426ac8SJonathan Peyton     __kmp_task_reduction_init_copy<T>(thr, num, data, tg, reduce_data);
2434a8426ac8SJonathan Peyton   }
2435a8426ac8SJonathan Peyton   return tg;
2436a8426ac8SJonathan Peyton }
2437a8426ac8SJonathan Peyton 
2438a8426ac8SJonathan Peyton /*!
2439a8426ac8SJonathan Peyton @ingroup TASKING
2440a8426ac8SJonathan Peyton @param loc       Source location info
2441a8426ac8SJonathan Peyton @param gtid      Global thread ID
2442a8426ac8SJonathan Peyton @param is_ws     Is 1 if the reduction is for worksharing, 0 otherwise
2443a8426ac8SJonathan Peyton @param num       Number of data items to reduce
2444a8426ac8SJonathan Peyton @param data      Array of data for reduction
2445a8426ac8SJonathan Peyton @return The taskgroup identifier
2446a8426ac8SJonathan Peyton 
2447a8426ac8SJonathan Peyton Initialize task reduction for a parallel or worksharing.
2448a8426ac8SJonathan Peyton 
2449a8426ac8SJonathan Peyton Note: this entry supposes the optional compiler-generated initializer routine
2450a8426ac8SJonathan Peyton has single parameter - pointer to object to be initialized. That means
2451a8426ac8SJonathan Peyton the reduction either does not use omp_orig object, or the omp_orig is accessible
2452a8426ac8SJonathan Peyton without help of the runtime library.
2453a8426ac8SJonathan Peyton */
2454a8426ac8SJonathan Peyton void *__kmpc_task_reduction_modifier_init(ident_t *loc, int gtid, int is_ws,
2455a8426ac8SJonathan Peyton                                           int num, void *data) {
2456a8426ac8SJonathan Peyton   return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2457a8426ac8SJonathan Peyton                                             (kmp_task_red_input_t *)data);
2458a8426ac8SJonathan Peyton }
2459a8426ac8SJonathan Peyton 
2460a8426ac8SJonathan Peyton /*!
2461a8426ac8SJonathan Peyton @ingroup TASKING
2462a8426ac8SJonathan Peyton @param loc       Source location info
2463a8426ac8SJonathan Peyton @param gtid      Global thread ID
2464a8426ac8SJonathan Peyton @param is_ws     Is 1 if the reduction is for worksharing, 0 otherwise
2465a8426ac8SJonathan Peyton @param num       Number of data items to reduce
2466a8426ac8SJonathan Peyton @param data      Array of data for reduction
2467a8426ac8SJonathan Peyton @return The taskgroup identifier
2468a8426ac8SJonathan Peyton 
2469a8426ac8SJonathan Peyton Initialize task reduction for a parallel or worksharing.
2470a8426ac8SJonathan Peyton 
2471a8426ac8SJonathan Peyton Note: this entry supposes the optional compiler-generated initializer routine
2472a8426ac8SJonathan Peyton has two parameters, pointer to object to be initialized and pointer to omp_orig
2473a8426ac8SJonathan Peyton */
2474a8426ac8SJonathan Peyton void *__kmpc_taskred_modifier_init(ident_t *loc, int gtid, int is_ws, int num,
2475a8426ac8SJonathan Peyton                                    void *data) {
2476a8426ac8SJonathan Peyton   return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num,
2477a8426ac8SJonathan Peyton                                             (kmp_taskred_input_t *)data);
2478a8426ac8SJonathan Peyton }
2479a8426ac8SJonathan Peyton 
2480a8426ac8SJonathan Peyton /*!
2481a8426ac8SJonathan Peyton @ingroup TASKING
2482a8426ac8SJonathan Peyton @param loc       Source location info
2483a8426ac8SJonathan Peyton @param gtid      Global thread ID
2484a8426ac8SJonathan Peyton @param is_ws     Is 1 if the reduction is for worksharing, 0 otherwise
2485a8426ac8SJonathan Peyton 
2486a8426ac8SJonathan Peyton Finalize task reduction for a parallel or worksharing.
2487a8426ac8SJonathan Peyton */
2488a8426ac8SJonathan Peyton void __kmpc_task_reduction_modifier_fini(ident_t *loc, int gtid, int is_ws) {
2489a8426ac8SJonathan Peyton   __kmpc_end_taskgroup(loc, gtid);
2490a8426ac8SJonathan Peyton }
249172ba2109SAndrey Churbanov #endif
24927cc577a4SJonathan Peyton 
24937cc577a4SJonathan Peyton #if OMP_40_ENABLED
24947cc577a4SJonathan Peyton // __kmpc_taskgroup: Start a new taskgroup
24953041982dSJonathan Peyton void __kmpc_taskgroup(ident_t *loc, int gtid) {
24967cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
24977cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = thread->th.th_current_task;
24987cc577a4SJonathan Peyton   kmp_taskgroup_t *tg_new =
24997cc577a4SJonathan Peyton       (kmp_taskgroup_t *)__kmp_thread_malloc(thread, sizeof(kmp_taskgroup_t));
25007cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new));
250137e2ef54SJonathan Peyton   KMP_ATOMIC_ST_RLX(&tg_new->count, 0);
250237e2ef54SJonathan Peyton   KMP_ATOMIC_ST_RLX(&tg_new->cancel_request, cancel_noreq);
25037cc577a4SJonathan Peyton   tg_new->parent = taskdata->td_taskgroup;
250483e360a4SJonathan Peyton #if OMP_50_ENABLED
250572ba2109SAndrey Churbanov   tg_new->reduce_data = NULL;
250672ba2109SAndrey Churbanov   tg_new->reduce_num_data = 0;
250772ba2109SAndrey Churbanov #endif
25087cc577a4SJonathan Peyton   taskdata->td_taskgroup = tg_new;
250982e94a59SJoachim Protze 
251082e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
2511f0a1c65fSJonas Hahnfeld   if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
251282e94a59SJoachim Protze     void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
251382e94a59SJoachim Protze     if (!codeptr)
251482e94a59SJoachim Protze       codeptr = OMPT_GET_RETURN_ADDRESS(0);
251582e94a59SJoachim Protze     kmp_team_t *team = thread->th.th_team;
251682e94a59SJoachim Protze     ompt_data_t my_task_data = taskdata->ompt_task_info.task_data;
251782e94a59SJoachim Protze     // FIXME: I think this is wrong for lwt!
251882e94a59SJoachim Protze     ompt_data_t my_parallel_data = team->t.ompt_team_info.parallel_data;
251982e94a59SJoachim Protze 
252082e94a59SJoachim Protze     ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
252182e94a59SJoachim Protze         ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
252282e94a59SJoachim Protze         &(my_task_data), codeptr);
252382e94a59SJoachim Protze   }
252482e94a59SJoachim Protze #endif
25257cc577a4SJonathan Peyton }
25267cc577a4SJonathan Peyton 
25277cc577a4SJonathan Peyton // __kmpc_end_taskgroup: Wait until all tasks generated by the current task
25287cc577a4SJonathan Peyton //                       and its descendants are complete
25293041982dSJonathan Peyton void __kmpc_end_taskgroup(ident_t *loc, int gtid) {
25307cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
25317cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = thread->th.th_current_task;
25327cc577a4SJonathan Peyton   kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
25337cc577a4SJonathan Peyton   int thread_finished = FALSE;
25347cc577a4SJonathan Peyton 
253582e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
253682e94a59SJoachim Protze   kmp_team_t *team;
253782e94a59SJoachim Protze   ompt_data_t my_task_data;
253882e94a59SJoachim Protze   ompt_data_t my_parallel_data;
253982e94a59SJoachim Protze   void *codeptr;
2540f0a1c65fSJonas Hahnfeld   if (UNLIKELY(ompt_enabled.enabled)) {
254182e94a59SJoachim Protze     team = thread->th.th_team;
254282e94a59SJoachim Protze     my_task_data = taskdata->ompt_task_info.task_data;
254382e94a59SJoachim Protze     // FIXME: I think this is wrong for lwt!
254482e94a59SJoachim Protze     my_parallel_data = team->t.ompt_team_info.parallel_data;
254582e94a59SJoachim Protze     codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
254682e94a59SJoachim Protze     if (!codeptr)
254782e94a59SJoachim Protze       codeptr = OMPT_GET_RETURN_ADDRESS(0);
254882e94a59SJoachim Protze   }
254982e94a59SJoachim Protze #endif
255082e94a59SJoachim Protze 
25517cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc));
25527cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskgroup != NULL);
25537cc577a4SJonathan Peyton   KMP_SET_THREAD_STATE_BLOCK(TASKGROUP);
25547cc577a4SJonathan Peyton 
25557cc577a4SJonathan Peyton   if (__kmp_tasking_mode != tskm_immediate_exec) {
2556a756cb24SAndrey Churbanov     // mark task as waiting not on a barrier
2557a756cb24SAndrey Churbanov     taskdata->td_taskwait_counter += 1;
2558a756cb24SAndrey Churbanov     taskdata->td_taskwait_ident = loc;
2559a756cb24SAndrey Churbanov     taskdata->td_taskwait_thread = gtid + 1;
25607cc577a4SJonathan Peyton #if USE_ITT_BUILD
25613041982dSJonathan Peyton     // For ITT the taskgroup wait is similar to taskwait until we need to
25623041982dSJonathan Peyton     // distinguish them
25637cc577a4SJonathan Peyton     void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
25647cc577a4SJonathan Peyton     if (itt_sync_obj != NULL)
25657cc577a4SJonathan Peyton       __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
25667cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
25677cc577a4SJonathan Peyton 
256882e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
2569f0a1c65fSJonas Hahnfeld     if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
257082e94a59SJoachim Protze       ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
257182e94a59SJoachim Protze           ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data),
257282e94a59SJoachim Protze           &(my_task_data), codeptr);
257382e94a59SJoachim Protze     }
257482e94a59SJoachim Protze #endif
257582e94a59SJoachim Protze 
25767cc577a4SJonathan Peyton #if OMP_45_ENABLED
25773041982dSJonathan Peyton     if (!taskdata->td_flags.team_serial ||
25783041982dSJonathan Peyton         (thread->th.th_task_team != NULL &&
25793041982dSJonathan Peyton          thread->th.th_task_team->tt.tt_found_proxy_tasks))
25807cc577a4SJonathan Peyton #else
25817cc577a4SJonathan Peyton     if (!taskdata->td_flags.team_serial)
25827cc577a4SJonathan Peyton #endif
25837cc577a4SJonathan Peyton     {
258437e2ef54SJonathan Peyton       kmp_flag_32 flag(RCAST(std::atomic<kmp_uint32> *, &(taskgroup->count)),
258537e2ef54SJonathan Peyton                        0U);
258637e2ef54SJonathan Peyton       while (KMP_ATOMIC_LD_ACQ(&taskgroup->count) != 0) {
25873041982dSJonathan Peyton         flag.execute_tasks(thread, gtid, FALSE,
25883041982dSJonathan Peyton                            &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
25893041982dSJonathan Peyton                            __kmp_task_stealing_constraint);
25907cc577a4SJonathan Peyton       }
25917cc577a4SJonathan Peyton     }
2592a756cb24SAndrey Churbanov     taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread; // end waiting
25937cc577a4SJonathan Peyton 
259482e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
2595f0a1c65fSJonas Hahnfeld     if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) {
259682e94a59SJoachim Protze       ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
259782e94a59SJoachim Protze           ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
259882e94a59SJoachim Protze           &(my_task_data), codeptr);
259982e94a59SJoachim Protze     }
260082e94a59SJoachim Protze #endif
260182e94a59SJoachim Protze 
26027cc577a4SJonathan Peyton #if USE_ITT_BUILD
26037cc577a4SJonathan Peyton     if (itt_sync_obj != NULL)
26047cc577a4SJonathan Peyton       __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
26057cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
26067cc577a4SJonathan Peyton   }
26077cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskgroup->count == 0);
26087cc577a4SJonathan Peyton 
260983e360a4SJonathan Peyton #if OMP_50_ENABLED
2610a8426ac8SJonathan Peyton   if (taskgroup->reduce_data != NULL) { // need to reduce?
2611a8426ac8SJonathan Peyton     int cnt;
2612a8426ac8SJonathan Peyton     void *reduce_data;
2613a8426ac8SJonathan Peyton     kmp_team_t *t = thread->th.th_team;
2614a8426ac8SJonathan Peyton     kmp_taskred_data_t *arr = (kmp_taskred_data_t *)taskgroup->reduce_data;
2615a8426ac8SJonathan Peyton     // check if <priv> data of the first reduction variable shared for the team
2616a8426ac8SJonathan Peyton     void *priv0 = arr[0].reduce_priv;
2617a8426ac8SJonathan Peyton     if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[0])) != NULL &&
2618a8426ac8SJonathan Peyton         ((kmp_taskred_data_t *)reduce_data)[0].reduce_priv == priv0) {
2619a8426ac8SJonathan Peyton       // finishing task reduction on parallel
2620a8426ac8SJonathan Peyton       cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[0]);
2621a8426ac8SJonathan Peyton       if (cnt == thread->th.th_team_nproc - 1) {
2622a8426ac8SJonathan Peyton         // we are the last thread passing __kmpc_reduction_modifier_fini()
2623a8426ac8SJonathan Peyton         // finalize task reduction:
262472ba2109SAndrey Churbanov         __kmp_task_reduction_fini(thread, taskgroup);
2625a8426ac8SJonathan Peyton         // cleanup fields in the team structure:
2626a8426ac8SJonathan Peyton         // TODO: is relaxed store enough here (whole barrier should follow)?
2627a8426ac8SJonathan Peyton         __kmp_thread_free(thread, reduce_data);
2628a8426ac8SJonathan Peyton         KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[0], NULL);
2629a8426ac8SJonathan Peyton         KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[0], 0);
2630a8426ac8SJonathan Peyton       } else {
2631a8426ac8SJonathan Peyton         // we are not the last thread passing __kmpc_reduction_modifier_fini(),
2632a8426ac8SJonathan Peyton         // so do not finalize reduction, just clean own copy of the data
2633a8426ac8SJonathan Peyton         __kmp_task_reduction_clean(thread, taskgroup);
2634a8426ac8SJonathan Peyton       }
2635a8426ac8SJonathan Peyton     } else if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[1])) !=
2636a8426ac8SJonathan Peyton                    NULL &&
2637a8426ac8SJonathan Peyton                ((kmp_taskred_data_t *)reduce_data)[0].reduce_priv == priv0) {
2638a8426ac8SJonathan Peyton       // finishing task reduction on worksharing
2639a8426ac8SJonathan Peyton       cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[1]);
2640a8426ac8SJonathan Peyton       if (cnt == thread->th.th_team_nproc - 1) {
2641a8426ac8SJonathan Peyton         // we are the last thread passing __kmpc_reduction_modifier_fini()
2642a8426ac8SJonathan Peyton         __kmp_task_reduction_fini(thread, taskgroup);
2643a8426ac8SJonathan Peyton         // cleanup fields in team structure:
2644a8426ac8SJonathan Peyton         // TODO: is relaxed store enough here (whole barrier should follow)?
2645a8426ac8SJonathan Peyton         __kmp_thread_free(thread, reduce_data);
2646a8426ac8SJonathan Peyton         KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[1], NULL);
2647a8426ac8SJonathan Peyton         KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[1], 0);
2648a8426ac8SJonathan Peyton       } else {
2649a8426ac8SJonathan Peyton         // we are not the last thread passing __kmpc_reduction_modifier_fini(),
2650a8426ac8SJonathan Peyton         // so do not finalize reduction, just clean own copy of the data
2651a8426ac8SJonathan Peyton         __kmp_task_reduction_clean(thread, taskgroup);
2652a8426ac8SJonathan Peyton       }
2653a8426ac8SJonathan Peyton     } else {
2654a8426ac8SJonathan Peyton       // finishing task reduction on taskgroup
2655a8426ac8SJonathan Peyton       __kmp_task_reduction_fini(thread, taskgroup);
2656a8426ac8SJonathan Peyton     }
2657a8426ac8SJonathan Peyton   }
265872ba2109SAndrey Churbanov #endif
26597cc577a4SJonathan Peyton   // Restore parent taskgroup for the current task
26607cc577a4SJonathan Peyton   taskdata->td_taskgroup = taskgroup->parent;
26617cc577a4SJonathan Peyton   __kmp_thread_free(thread, taskgroup);
26627cc577a4SJonathan Peyton 
26633041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n",
26643041982dSJonathan Peyton                 gtid, taskdata));
26657cc577a4SJonathan Peyton   ANNOTATE_HAPPENS_AFTER(taskdata);
266682e94a59SJoachim Protze 
266782e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
2668f0a1c65fSJonas Hahnfeld   if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) {
266982e94a59SJoachim Protze     ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
267082e94a59SJoachim Protze         ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data),
267182e94a59SJoachim Protze         &(my_task_data), codeptr);
267282e94a59SJoachim Protze   }
267382e94a59SJoachim Protze #endif
26747cc577a4SJonathan Peyton }
26757cc577a4SJonathan Peyton #endif
26767cc577a4SJonathan Peyton 
26777cc577a4SJonathan Peyton // __kmp_remove_my_task: remove a task from my own deque
26783041982dSJonathan Peyton static kmp_task_t *__kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid,
26793041982dSJonathan Peyton                                         kmp_task_team_t *task_team,
26803041982dSJonathan Peyton                                         kmp_int32 is_constrained) {
26817cc577a4SJonathan Peyton   kmp_task_t *task;
26827cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata;
26837cc577a4SJonathan Peyton   kmp_thread_data_t *thread_data;
26847cc577a4SJonathan Peyton   kmp_uint32 tail;
26857cc577a4SJonathan Peyton 
26867cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
26873041982dSJonathan Peyton   KMP_DEBUG_ASSERT(task_team->tt.tt_threads_data !=
26883041982dSJonathan Peyton                    NULL); // Caller should check this condition
26897cc577a4SJonathan Peyton 
26907cc577a4SJonathan Peyton   thread_data = &task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
26917cc577a4SJonathan Peyton 
26927cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmp_remove_my_task(enter): T#%d ntasks=%d head=%u tail=%u\n",
26933041982dSJonathan Peyton                 gtid, thread_data->td.td_deque_ntasks,
26943041982dSJonathan Peyton                 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
26957cc577a4SJonathan Peyton 
26967cc577a4SJonathan Peyton   if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
26973041982dSJonathan Peyton     KA_TRACE(10,
26983041982dSJonathan Peyton              ("__kmp_remove_my_task(exit #1): T#%d No tasks to remove: "
26993041982dSJonathan Peyton               "ntasks=%d head=%u tail=%u\n",
27003041982dSJonathan Peyton               gtid, thread_data->td.td_deque_ntasks,
27013041982dSJonathan Peyton               thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
27027cc577a4SJonathan Peyton     return NULL;
27037cc577a4SJonathan Peyton   }
27047cc577a4SJonathan Peyton 
27057cc577a4SJonathan Peyton   __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
27067cc577a4SJonathan Peyton 
27077cc577a4SJonathan Peyton   if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
27087cc577a4SJonathan Peyton     __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
27093041982dSJonathan Peyton     KA_TRACE(10,
27103041982dSJonathan Peyton              ("__kmp_remove_my_task(exit #2): T#%d No tasks to remove: "
27113041982dSJonathan Peyton               "ntasks=%d head=%u tail=%u\n",
27123041982dSJonathan Peyton               gtid, thread_data->td.td_deque_ntasks,
27133041982dSJonathan Peyton               thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
27147cc577a4SJonathan Peyton     return NULL;
27157cc577a4SJonathan Peyton   }
27167cc577a4SJonathan Peyton 
27173041982dSJonathan Peyton   tail = (thread_data->td.td_deque_tail - 1) &
27183041982dSJonathan Peyton          TASK_DEQUE_MASK(thread_data->td); // Wrap index.
27197cc577a4SJonathan Peyton   taskdata = thread_data->td.td_deque[tail];
27207cc577a4SJonathan Peyton 
2721c3344345SAndrey Churbanov   if (!__kmp_task_is_allowed(gtid, is_constrained, taskdata,
2722c3344345SAndrey Churbanov                              thread->th.th_current_task)) {
2723a756cb24SAndrey Churbanov     // The TSC does not allow to steal victim task
27247cc577a4SJonathan Peyton     __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
2725c3344345SAndrey Churbanov     KA_TRACE(10,
2726c3344345SAndrey Churbanov              ("__kmp_remove_my_task(exit #3): T#%d TSC blocks tail task: "
27273041982dSJonathan Peyton               "ntasks=%d head=%u tail=%u\n",
27283041982dSJonathan Peyton               gtid, thread_data->td.td_deque_ntasks,
2729c3344345SAndrey Churbanov               thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
27307cc577a4SJonathan Peyton     return NULL;
27317cc577a4SJonathan Peyton   }
27327cc577a4SJonathan Peyton 
27337cc577a4SJonathan Peyton   thread_data->td.td_deque_tail = tail;
27347cc577a4SJonathan Peyton   TCW_4(thread_data->td.td_deque_ntasks, thread_data->td.td_deque_ntasks - 1);
27357cc577a4SJonathan Peyton 
27367cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
27377cc577a4SJonathan Peyton 
2738c3344345SAndrey Churbanov   KA_TRACE(10, ("__kmp_remove_my_task(exit #4): T#%d task %p removed: "
27393041982dSJonathan Peyton                 "ntasks=%d head=%u tail=%u\n",
27403041982dSJonathan Peyton                 gtid, taskdata, thread_data->td.td_deque_ntasks,
27413041982dSJonathan Peyton                 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
27427cc577a4SJonathan Peyton 
27437cc577a4SJonathan Peyton   task = KMP_TASKDATA_TO_TASK(taskdata);
27447cc577a4SJonathan Peyton   return task;
27457cc577a4SJonathan Peyton }
27467cc577a4SJonathan Peyton 
27477cc577a4SJonathan Peyton // __kmp_steal_task: remove a task from another thread's deque
27487cc577a4SJonathan Peyton // Assume that calling thread has already checked existence of
27497cc577a4SJonathan Peyton // task_team thread_data before calling this routine.
2750a756cb24SAndrey Churbanov static kmp_task_t *__kmp_steal_task(kmp_info_t *victim_thr, kmp_int32 gtid,
2751c47afcd9SAndrey Churbanov                                     kmp_task_team_t *task_team,
275237e2ef54SJonathan Peyton                                     std::atomic<kmp_int32> *unfinished_threads,
2753c47afcd9SAndrey Churbanov                                     int *thread_finished,
2754c47afcd9SAndrey Churbanov                                     kmp_int32 is_constrained) {
27557cc577a4SJonathan Peyton   kmp_task_t *task;
27567cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata;
2757a756cb24SAndrey Churbanov   kmp_taskdata_t *current;
27587cc577a4SJonathan Peyton   kmp_thread_data_t *victim_td, *threads_data;
2759c3344345SAndrey Churbanov   kmp_int32 target;
27607cc577a4SJonathan Peyton   kmp_int32 victim_tid;
27617cc577a4SJonathan Peyton 
27627cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
27637cc577a4SJonathan Peyton 
27647cc577a4SJonathan Peyton   threads_data = task_team->tt.tt_threads_data;
27657cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(threads_data != NULL); // Caller should check this condition
27667cc577a4SJonathan Peyton 
2767a756cb24SAndrey Churbanov   victim_tid = victim_thr->th.th_info.ds.ds_tid;
27687cc577a4SJonathan Peyton   victim_td = &threads_data[victim_tid];
27697cc577a4SJonathan Peyton 
27703041982dSJonathan Peyton   KA_TRACE(10, ("__kmp_steal_task(enter): T#%d try to steal from T#%d: "
2771a756cb24SAndrey Churbanov                 "task_team=%p ntasks=%d head=%u tail=%u\n",
2772a756cb24SAndrey Churbanov                 gtid, __kmp_gtid_from_thread(victim_thr), task_team,
27733041982dSJonathan Peyton                 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
27743041982dSJonathan Peyton                 victim_td->td.td_deque_tail));
27757cc577a4SJonathan Peyton 
2776a756cb24SAndrey Churbanov   if (TCR_4(victim_td->td.td_deque_ntasks) == 0) {
27773041982dSJonathan Peyton     KA_TRACE(10, ("__kmp_steal_task(exit #1): T#%d could not steal from T#%d: "
2778a756cb24SAndrey Churbanov                   "task_team=%p ntasks=%d head=%u tail=%u\n",
2779a756cb24SAndrey Churbanov                   gtid, __kmp_gtid_from_thread(victim_thr), task_team,
27803041982dSJonathan Peyton                   victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
27813041982dSJonathan Peyton                   victim_td->td.td_deque_tail));
27827cc577a4SJonathan Peyton     return NULL;
27837cc577a4SJonathan Peyton   }
27847cc577a4SJonathan Peyton 
27857cc577a4SJonathan Peyton   __kmp_acquire_bootstrap_lock(&victim_td->td.td_deque_lock);
27867cc577a4SJonathan Peyton 
2787a756cb24SAndrey Churbanov   int ntasks = TCR_4(victim_td->td.td_deque_ntasks);
27887cc577a4SJonathan Peyton   // Check again after we acquire the lock
2789a756cb24SAndrey Churbanov   if (ntasks == 0) {
27907cc577a4SJonathan Peyton     __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
27913041982dSJonathan Peyton     KA_TRACE(10, ("__kmp_steal_task(exit #2): T#%d could not steal from T#%d: "
2792a756cb24SAndrey Churbanov                   "task_team=%p ntasks=%d head=%u tail=%u\n",
2793a756cb24SAndrey Churbanov                   gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2794a756cb24SAndrey Churbanov                   victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
27957cc577a4SJonathan Peyton     return NULL;
27967cc577a4SJonathan Peyton   }
27977cc577a4SJonathan Peyton 
27987cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(victim_td->td.td_deque != NULL);
2799c3344345SAndrey Churbanov   current = __kmp_threads[gtid]->th.th_current_task;
28007cc577a4SJonathan Peyton   taskdata = victim_td->td.td_deque[victim_td->td.td_deque_head];
2801c3344345SAndrey Churbanov   if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
2802c3344345SAndrey Churbanov     // Bump head pointer and Wrap.
2803c3344345SAndrey Churbanov     victim_td->td.td_deque_head =
2804c3344345SAndrey Churbanov         (victim_td->td.td_deque_head + 1) & TASK_DEQUE_MASK(victim_td->td);
2805c3344345SAndrey Churbanov   } else {
2806a756cb24SAndrey Churbanov     if (!task_team->tt.tt_untied_task_encountered) {
2807a756cb24SAndrey Churbanov       // The TSC does not allow to steal victim task
28087cc577a4SJonathan Peyton       __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2809c3344345SAndrey Churbanov       KA_TRACE(10, ("__kmp_steal_task(exit #3): T#%d could not steal from "
2810a756cb24SAndrey Churbanov                     "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
2811a756cb24SAndrey Churbanov                     gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
28127cc577a4SJonathan Peyton                     victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
28137cc577a4SJonathan Peyton       return NULL;
28147cc577a4SJonathan Peyton     }
2815a756cb24SAndrey Churbanov     int i;
2816a756cb24SAndrey Churbanov     // walk through victim's deque trying to steal any task
2817a756cb24SAndrey Churbanov     target = victim_td->td.td_deque_head;
2818c3344345SAndrey Churbanov     taskdata = NULL;
2819a756cb24SAndrey Churbanov     for (i = 1; i < ntasks; ++i) {
2820a756cb24SAndrey Churbanov       target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
2821a756cb24SAndrey Churbanov       taskdata = victim_td->td.td_deque[target];
2822c3344345SAndrey Churbanov       if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) {
2823c3344345SAndrey Churbanov         break; // found victim task
2824c3344345SAndrey Churbanov       } else {
2825a756cb24SAndrey Churbanov         taskdata = NULL;
2826a756cb24SAndrey Churbanov       }
2827a756cb24SAndrey Churbanov     }
2828a756cb24SAndrey Churbanov     if (taskdata == NULL) {
2829a756cb24SAndrey Churbanov       // No appropriate candidate to steal found
2830a756cb24SAndrey Churbanov       __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
2831a756cb24SAndrey Churbanov       KA_TRACE(10, ("__kmp_steal_task(exit #4): T#%d could not steal from "
2832a756cb24SAndrey Churbanov                     "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n",
2833a756cb24SAndrey Churbanov                     gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks,
2834a756cb24SAndrey Churbanov                     victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
2835a756cb24SAndrey Churbanov       return NULL;
2836a756cb24SAndrey Churbanov     }
2837a756cb24SAndrey Churbanov     int prev = target;
2838a756cb24SAndrey Churbanov     for (i = i + 1; i < ntasks; ++i) {
2839a756cb24SAndrey Churbanov       // shift remaining tasks in the deque left by 1
2840a756cb24SAndrey Churbanov       target = (target + 1) & TASK_DEQUE_MASK(victim_td->td);
2841a756cb24SAndrey Churbanov       victim_td->td.td_deque[prev] = victim_td->td.td_deque[target];
2842a756cb24SAndrey Churbanov       prev = target;
2843a756cb24SAndrey Churbanov     }
2844baad3f60SJonathan Peyton     KMP_DEBUG_ASSERT(
2845baad3f60SJonathan Peyton         victim_td->td.td_deque_tail ==
2846baad3f60SJonathan Peyton         (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(victim_td->td)));
2847a756cb24SAndrey Churbanov     victim_td->td.td_deque_tail = target; // tail -= 1 (wrapped))
2848a756cb24SAndrey Churbanov   }
28497cc577a4SJonathan Peyton   if (*thread_finished) {
28503041982dSJonathan Peyton     // We need to un-mark this victim as a finished victim.  This must be done
28513041982dSJonathan Peyton     // before releasing the lock, or else other threads (starting with the
28523041982dSJonathan Peyton     // master victim) might be prematurely released from the barrier!!!
2853c47afcd9SAndrey Churbanov     kmp_int32 count;
28547cc577a4SJonathan Peyton 
285537e2ef54SJonathan Peyton     count = KMP_ATOMIC_INC(unfinished_threads);
28567cc577a4SJonathan Peyton 
28573041982dSJonathan Peyton     KA_TRACE(
28583041982dSJonathan Peyton         20,
28593041982dSJonathan Peyton         ("__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n",
28607cc577a4SJonathan Peyton          gtid, count + 1, task_team));
28617cc577a4SJonathan Peyton 
28627cc577a4SJonathan Peyton     *thread_finished = FALSE;
28637cc577a4SJonathan Peyton   }
2864a756cb24SAndrey Churbanov   TCW_4(victim_td->td.td_deque_ntasks, ntasks - 1);
28653041982dSJonathan Peyton 
28667cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
28677cc577a4SJonathan Peyton 
28687cc577a4SJonathan Peyton   KMP_COUNT_BLOCK(TASK_stolen);
2869a756cb24SAndrey Churbanov   KA_TRACE(10,
2870a756cb24SAndrey Churbanov            ("__kmp_steal_task(exit #5): T#%d stole task %p from T#%d: "
2871a756cb24SAndrey Churbanov             "task_team=%p ntasks=%d head=%u tail=%u\n",
2872a756cb24SAndrey Churbanov             gtid, taskdata, __kmp_gtid_from_thread(victim_thr), task_team,
2873a756cb24SAndrey Churbanov             ntasks, victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
28747cc577a4SJonathan Peyton 
28757cc577a4SJonathan Peyton   task = KMP_TASKDATA_TO_TASK(taskdata);
28767cc577a4SJonathan Peyton   return task;
28777cc577a4SJonathan Peyton }
28787cc577a4SJonathan Peyton 
28793041982dSJonathan Peyton // __kmp_execute_tasks_template: Choose and execute tasks until either the
28803041982dSJonathan Peyton // condition is statisfied (return true) or there are none left (return false).
28813041982dSJonathan Peyton //
28827cc577a4SJonathan Peyton // final_spin is TRUE if this is the spin at the release barrier.
28837cc577a4SJonathan Peyton // thread_finished indicates whether the thread is finished executing all
28847cc577a4SJonathan Peyton // the tasks it has on its deque, and is at the release barrier.
28857cc577a4SJonathan Peyton // spinner is the location on which to spin.
28867cc577a4SJonathan Peyton // spinner == NULL means only execute a single task and return.
28877cc577a4SJonathan Peyton // checker is the value to check to terminate the spin.
28887cc577a4SJonathan Peyton template <class C>
28893041982dSJonathan Peyton static inline int __kmp_execute_tasks_template(
28903041982dSJonathan Peyton     kmp_info_t *thread, kmp_int32 gtid, C *flag, int final_spin,
28913041982dSJonathan Peyton     int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj),
28923041982dSJonathan Peyton     kmp_int32 is_constrained) {
28937cc577a4SJonathan Peyton   kmp_task_team_t *task_team = thread->th.th_task_team;
28947cc577a4SJonathan Peyton   kmp_thread_data_t *threads_data;
28957cc577a4SJonathan Peyton   kmp_task_t *task;
28967cc577a4SJonathan Peyton   kmp_info_t *other_thread;
28977cc577a4SJonathan Peyton   kmp_taskdata_t *current_task = thread->th.th_current_task;
289837e2ef54SJonathan Peyton   std::atomic<kmp_int32> *unfinished_threads;
2899a756cb24SAndrey Churbanov   kmp_int32 nthreads, victim_tid = -2, use_own_tasks = 1, new_victim = 0,
29003041982dSJonathan Peyton                       tid = thread->th.th_info.ds.ds_tid;
29017cc577a4SJonathan Peyton 
29027cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
29037cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(thread == __kmp_threads[gtid]);
29047cc577a4SJonathan Peyton 
2905a4a9c48cSJonathan Peyton   if (task_team == NULL || current_task == NULL)
29063041982dSJonathan Peyton     return FALSE;
29077cc577a4SJonathan Peyton 
29083041982dSJonathan Peyton   KA_TRACE(15, ("__kmp_execute_tasks_template(enter): T#%d final_spin=%d "
29093041982dSJonathan Peyton                 "*thread_finished=%d\n",
29107cc577a4SJonathan Peyton                 gtid, final_spin, *thread_finished));
29117cc577a4SJonathan Peyton 
2912581490e7SAndrey Churbanov   thread->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
29137cc577a4SJonathan Peyton   threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
29147cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(threads_data != NULL);
29157cc577a4SJonathan Peyton 
29167cc577a4SJonathan Peyton   nthreads = task_team->tt.tt_nproc;
29177cc577a4SJonathan Peyton   unfinished_threads = &(task_team->tt.tt_unfinished_threads);
29187cc577a4SJonathan Peyton #if OMP_45_ENABLED
29197cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(nthreads > 1 || task_team->tt.tt_found_proxy_tasks);
29207cc577a4SJonathan Peyton #else
29217cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(nthreads > 1);
29227cc577a4SJonathan Peyton #endif
292337e2ef54SJonathan Peyton   KMP_DEBUG_ASSERT(*unfinished_threads >= 0);
29247cc577a4SJonathan Peyton 
29253041982dSJonathan Peyton   while (1) { // Outer loop keeps trying to find tasks in case of single thread
29263041982dSJonathan Peyton     // getting tasks from target constructs
29277cc577a4SJonathan Peyton     while (1) { // Inner loop to find a task and execute it
29287cc577a4SJonathan Peyton       task = NULL;
29297cc577a4SJonathan Peyton       if (use_own_tasks) { // check on own queue first
29307cc577a4SJonathan Peyton         task = __kmp_remove_my_task(thread, gtid, task_team, is_constrained);
29317cc577a4SJonathan Peyton       }
29327cc577a4SJonathan Peyton       if ((task == NULL) && (nthreads > 1)) { // Steal a task
29337cc577a4SJonathan Peyton         int asleep = 1;
29347cc577a4SJonathan Peyton         use_own_tasks = 0;
29357cc577a4SJonathan Peyton         // Try to steal from the last place I stole from successfully.
2936a756cb24SAndrey Churbanov         if (victim_tid == -2) { // haven't stolen anything yet
2937a756cb24SAndrey Churbanov           victim_tid = threads_data[tid].td.td_deque_last_stolen;
2938a756cb24SAndrey Churbanov           if (victim_tid !=
29393041982dSJonathan Peyton               -1) // if we have a last stolen from victim, get the thread
2940a756cb24SAndrey Churbanov             other_thread = threads_data[victim_tid].td.td_thr;
29417cc577a4SJonathan Peyton         }
2942a756cb24SAndrey Churbanov         if (victim_tid != -1) { // found last victim
29437cc577a4SJonathan Peyton           asleep = 0;
29443041982dSJonathan Peyton         } else if (!new_victim) { // no recent steals and we haven't already
29453041982dSJonathan Peyton           // used a new victim; select a random thread
29467cc577a4SJonathan Peyton           do { // Find a different thread to steal work from.
29473041982dSJonathan Peyton             // Pick a random thread. Initial plan was to cycle through all the
29483041982dSJonathan Peyton             // threads, and only return if we tried to steal from every thread,
29493041982dSJonathan Peyton             // and failed.  Arch says that's not such a great idea.
2950a756cb24SAndrey Churbanov             victim_tid = __kmp_get_random(thread) % (nthreads - 1);
2951a756cb24SAndrey Churbanov             if (victim_tid >= tid) {
2952a756cb24SAndrey Churbanov               ++victim_tid; // Adjusts random distribution to exclude self
29537cc577a4SJonathan Peyton             }
29547cc577a4SJonathan Peyton             // Found a potential victim
2955a756cb24SAndrey Churbanov             other_thread = threads_data[victim_tid].td.td_thr;
29563041982dSJonathan Peyton             // There is a slight chance that __kmp_enable_tasking() did not wake
29573041982dSJonathan Peyton             // up all threads waiting at the barrier.  If victim is sleeping,
29583041982dSJonathan Peyton             // then wake it up. Since we were going to pay the cache miss
29593041982dSJonathan Peyton             // penalty for referencing another thread's kmp_info_t struct
29603041982dSJonathan Peyton             // anyway,
29613041982dSJonathan Peyton             // the check shouldn't cost too much performance at this point. In
29623041982dSJonathan Peyton             // extra barrier mode, tasks do not sleep at the separate tasking
29633041982dSJonathan Peyton             // barrier, so this isn't a problem.
29647cc577a4SJonathan Peyton             asleep = 0;
29657cc577a4SJonathan Peyton             if ((__kmp_tasking_mode == tskm_task_teams) &&
29667cc577a4SJonathan Peyton                 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) &&
2967c47afcd9SAndrey Churbanov                 (TCR_PTR(CCAST(void *, other_thread->th.th_sleep_loc)) !=
2968c47afcd9SAndrey Churbanov                  NULL)) {
29697cc577a4SJonathan Peyton               asleep = 1;
29703041982dSJonathan Peyton               __kmp_null_resume_wrapper(__kmp_gtid_from_thread(other_thread),
29713041982dSJonathan Peyton                                         other_thread->th.th_sleep_loc);
29723041982dSJonathan Peyton               // A sleeping thread should not have any tasks on it's queue.
29733041982dSJonathan Peyton               // There is a slight possibility that it resumes, steals a task
29743041982dSJonathan Peyton               // from another thread, which spawns more tasks, all in the time
29753041982dSJonathan Peyton               // that it takes this thread to check => don't write an assertion
29763041982dSJonathan Peyton               // that the victim's queue is empty.  Try stealing from a
29773041982dSJonathan Peyton               // different thread.
29787cc577a4SJonathan Peyton             }
29797cc577a4SJonathan Peyton           } while (asleep);
29807cc577a4SJonathan Peyton         }
29817cc577a4SJonathan Peyton 
29827cc577a4SJonathan Peyton         if (!asleep) {
29837cc577a4SJonathan Peyton           // We have a victim to try to steal from
29843041982dSJonathan Peyton           task = __kmp_steal_task(other_thread, gtid, task_team,
29853041982dSJonathan Peyton                                   unfinished_threads, thread_finished,
29863041982dSJonathan Peyton                                   is_constrained);
29877cc577a4SJonathan Peyton         }
29887cc577a4SJonathan Peyton         if (task != NULL) { // set last stolen to victim
2989a756cb24SAndrey Churbanov           if (threads_data[tid].td.td_deque_last_stolen != victim_tid) {
2990a756cb24SAndrey Churbanov             threads_data[tid].td.td_deque_last_stolen = victim_tid;
29913041982dSJonathan Peyton             // The pre-refactored code did not try more than 1 successful new
29923041982dSJonathan Peyton             // vicitm, unless the last one generated more local tasks;
29933041982dSJonathan Peyton             // new_victim keeps track of this
29947cc577a4SJonathan Peyton             new_victim = 1;
29957cc577a4SJonathan Peyton           }
29963041982dSJonathan Peyton         } else { // No tasks found; unset last_stolen
29977cc577a4SJonathan Peyton           KMP_CHECK_UPDATE(threads_data[tid].td.td_deque_last_stolen, -1);
2998a756cb24SAndrey Churbanov           victim_tid = -2; // no successful victim found
29997cc577a4SJonathan Peyton         }
30007cc577a4SJonathan Peyton       }
30017cc577a4SJonathan Peyton 
30027cc577a4SJonathan Peyton       if (task == NULL) // break out of tasking loop
30037cc577a4SJonathan Peyton         break;
30047cc577a4SJonathan Peyton 
30057cc577a4SJonathan Peyton // Found a task; execute it
30067cc577a4SJonathan Peyton #if USE_ITT_BUILD && USE_ITT_NOTIFY
30077cc577a4SJonathan Peyton       if (__itt_sync_create_ptr || KMP_ITT_DEBUG) {
30083041982dSJonathan Peyton         if (itt_sync_obj == NULL) { // we are at fork barrier where we could not
30093041982dSJonathan Peyton           // get the object reliably
30107cc577a4SJonathan Peyton           itt_sync_obj = __kmp_itt_barrier_object(gtid, bs_forkjoin_barrier);
30117cc577a4SJonathan Peyton         }
30127cc577a4SJonathan Peyton         __kmp_itt_task_starting(itt_sync_obj);
30137cc577a4SJonathan Peyton       }
30147cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD && USE_ITT_NOTIFY */
30157cc577a4SJonathan Peyton       __kmp_invoke_task(gtid, task, current_task);
30167cc577a4SJonathan Peyton #if USE_ITT_BUILD
30173041982dSJonathan Peyton       if (itt_sync_obj != NULL)
30183041982dSJonathan Peyton         __kmp_itt_task_finished(itt_sync_obj);
30197cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
30203041982dSJonathan Peyton       // If this thread is only partway through the barrier and the condition is
30213041982dSJonathan Peyton       // met, then return now, so that the barrier gather/release pattern can
30223041982dSJonathan Peyton       // proceed. If this thread is in the last spin loop in the barrier,
30233041982dSJonathan Peyton       // waiting to be released, we know that the termination condition will not
30243041982dSJonathan Peyton       // be satisified, so don't waste any cycles checking it.
30257cc577a4SJonathan Peyton       if (flag == NULL || (!final_spin && flag->done_check())) {
30263041982dSJonathan Peyton         KA_TRACE(
30273041982dSJonathan Peyton             15,
30283041982dSJonathan Peyton             ("__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
30293041982dSJonathan Peyton              gtid));
30307cc577a4SJonathan Peyton         return TRUE;
30317cc577a4SJonathan Peyton       }
30327cc577a4SJonathan Peyton       if (thread->th.th_task_team == NULL) {
30337cc577a4SJonathan Peyton         break;
30347cc577a4SJonathan Peyton       }
3035e47d32f1SJonathan Peyton       KMP_YIELD(__kmp_library == library_throughput); // Yield before next task
30363041982dSJonathan Peyton       // If execution of a stolen task results in more tasks being placed on our
30373041982dSJonathan Peyton       // run queue, reset use_own_tasks
30387cc577a4SJonathan Peyton       if (!use_own_tasks && TCR_4(threads_data[tid].td.td_deque_ntasks) != 0) {
30393041982dSJonathan Peyton         KA_TRACE(20, ("__kmp_execute_tasks_template: T#%d stolen task spawned "
30403041982dSJonathan Peyton                       "other tasks, restart\n",
30413041982dSJonathan Peyton                       gtid));
30427cc577a4SJonathan Peyton         use_own_tasks = 1;
30437cc577a4SJonathan Peyton         new_victim = 0;
30447cc577a4SJonathan Peyton       }
30457cc577a4SJonathan Peyton     }
30467cc577a4SJonathan Peyton 
30473041982dSJonathan Peyton // The task source has been exhausted. If in final spin loop of barrier, check
30483041982dSJonathan Peyton // if termination condition is satisfied.
30497cc577a4SJonathan Peyton #if OMP_45_ENABLED
30503041982dSJonathan Peyton     // The work queue may be empty but there might be proxy tasks still
30513041982dSJonathan Peyton     // executing
305237e2ef54SJonathan Peyton     if (final_spin &&
305337e2ef54SJonathan Peyton         KMP_ATOMIC_LD_ACQ(&current_task->td_incomplete_child_tasks) == 0)
30547cc577a4SJonathan Peyton #else
30557cc577a4SJonathan Peyton     if (final_spin)
30567cc577a4SJonathan Peyton #endif
30577cc577a4SJonathan Peyton     {
30583041982dSJonathan Peyton       // First, decrement the #unfinished threads, if that has not already been
30593041982dSJonathan Peyton       // done.  This decrement might be to the spin location, and result in the
30603041982dSJonathan Peyton       // termination condition being satisfied.
30617cc577a4SJonathan Peyton       if (!*thread_finished) {
3062c47afcd9SAndrey Churbanov         kmp_int32 count;
30637cc577a4SJonathan Peyton 
306437e2ef54SJonathan Peyton         count = KMP_ATOMIC_DEC(unfinished_threads) - 1;
30653041982dSJonathan Peyton         KA_TRACE(20, ("__kmp_execute_tasks_template: T#%d dec "
30663041982dSJonathan Peyton                       "unfinished_threads to %d task_team=%p\n",
30677cc577a4SJonathan Peyton                       gtid, count, task_team));
30687cc577a4SJonathan Peyton         *thread_finished = TRUE;
30697cc577a4SJonathan Peyton       }
30707cc577a4SJonathan Peyton 
30717cc577a4SJonathan Peyton       // It is now unsafe to reference thread->th.th_team !!!
30723041982dSJonathan Peyton       // Decrementing task_team->tt.tt_unfinished_threads can allow the master
30733041982dSJonathan Peyton       // thread to pass through the barrier, where it might reset each thread's
30743041982dSJonathan Peyton       // th.th_team field for the next parallel region. If we can steal more
30753041982dSJonathan Peyton       // work, we know that this has not happened yet.
30767cc577a4SJonathan Peyton       if (flag != NULL && flag->done_check()) {
30773041982dSJonathan Peyton         KA_TRACE(
30783041982dSJonathan Peyton             15,
30793041982dSJonathan Peyton             ("__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
30803041982dSJonathan Peyton              gtid));
30817cc577a4SJonathan Peyton         return TRUE;
30827cc577a4SJonathan Peyton       }
30837cc577a4SJonathan Peyton     }
30847cc577a4SJonathan Peyton 
30853041982dSJonathan Peyton     // If this thread's task team is NULL, master has recognized that there are
30863041982dSJonathan Peyton     // no more tasks; bail out
30877cc577a4SJonathan Peyton     if (thread->th.th_task_team == NULL) {
30883041982dSJonathan Peyton       KA_TRACE(15,
30893041982dSJonathan Peyton                ("__kmp_execute_tasks_template: T#%d no more tasks\n", gtid));
30907cc577a4SJonathan Peyton       return FALSE;
30917cc577a4SJonathan Peyton     }
30927cc577a4SJonathan Peyton 
30937cc577a4SJonathan Peyton #if OMP_45_ENABLED
30943041982dSJonathan Peyton     // We could be getting tasks from target constructs; if this is the only
30953041982dSJonathan Peyton     // thread, keep trying to execute tasks from own queue
30967cc577a4SJonathan Peyton     if (nthreads == 1)
30977cc577a4SJonathan Peyton       use_own_tasks = 1;
30987cc577a4SJonathan Peyton     else
30997cc577a4SJonathan Peyton #endif
31007cc577a4SJonathan Peyton     {
31013041982dSJonathan Peyton       KA_TRACE(15,
31023041982dSJonathan Peyton                ("__kmp_execute_tasks_template: T#%d can't find work\n", gtid));
31037cc577a4SJonathan Peyton       return FALSE;
31047cc577a4SJonathan Peyton     }
31057cc577a4SJonathan Peyton   }
31067cc577a4SJonathan Peyton }
31077cc577a4SJonathan Peyton 
31083041982dSJonathan Peyton int __kmp_execute_tasks_32(
31093041982dSJonathan Peyton     kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32 *flag, int final_spin,
31103041982dSJonathan Peyton     int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj),
31113041982dSJonathan Peyton     kmp_int32 is_constrained) {
31123041982dSJonathan Peyton   return __kmp_execute_tasks_template(
31133041982dSJonathan Peyton       thread, gtid, flag, final_spin,
31143041982dSJonathan Peyton       thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
31157cc577a4SJonathan Peyton }
31167cc577a4SJonathan Peyton 
31173041982dSJonathan Peyton int __kmp_execute_tasks_64(
31183041982dSJonathan Peyton     kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64 *flag, int final_spin,
31193041982dSJonathan Peyton     int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj),
31203041982dSJonathan Peyton     kmp_int32 is_constrained) {
31213041982dSJonathan Peyton   return __kmp_execute_tasks_template(
31223041982dSJonathan Peyton       thread, gtid, flag, final_spin,
31233041982dSJonathan Peyton       thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
31247cc577a4SJonathan Peyton }
31257cc577a4SJonathan Peyton 
31263041982dSJonathan Peyton int __kmp_execute_tasks_oncore(
31273041982dSJonathan Peyton     kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag, int final_spin,
31283041982dSJonathan Peyton     int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj),
31293041982dSJonathan Peyton     kmp_int32 is_constrained) {
31303041982dSJonathan Peyton   return __kmp_execute_tasks_template(
31313041982dSJonathan Peyton       thread, gtid, flag, final_spin,
31323041982dSJonathan Peyton       thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
31337cc577a4SJonathan Peyton }
31347cc577a4SJonathan Peyton 
31357cc577a4SJonathan Peyton // __kmp_enable_tasking: Allocate task team and resume threads sleeping at the
31367cc577a4SJonathan Peyton // next barrier so they can assist in executing enqueued tasks.
31377cc577a4SJonathan Peyton // First thread in allocates the task team atomically.
31383041982dSJonathan Peyton static void __kmp_enable_tasking(kmp_task_team_t *task_team,
31393041982dSJonathan Peyton                                  kmp_info_t *this_thr) {
31407cc577a4SJonathan Peyton   kmp_thread_data_t *threads_data;
31417cc577a4SJonathan Peyton   int nthreads, i, is_init_thread;
31427cc577a4SJonathan Peyton 
31437cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmp_enable_tasking(enter): T#%d\n",
31447cc577a4SJonathan Peyton                 __kmp_gtid_from_thread(this_thr)));
31457cc577a4SJonathan Peyton 
31467cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task_team != NULL);
31477cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(this_thr->th.th_team != NULL);
31487cc577a4SJonathan Peyton 
31497cc577a4SJonathan Peyton   nthreads = task_team->tt.tt_nproc;
31507cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(nthreads > 0);
31517cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(nthreads == this_thr->th.th_team->t.t_nproc);
31527cc577a4SJonathan Peyton 
31537cc577a4SJonathan Peyton   // Allocate or increase the size of threads_data if necessary
31547cc577a4SJonathan Peyton   is_init_thread = __kmp_realloc_task_threads_data(this_thr, task_team);
31557cc577a4SJonathan Peyton 
31567cc577a4SJonathan Peyton   if (!is_init_thread) {
31577cc577a4SJonathan Peyton     // Some other thread already set up the array.
31583041982dSJonathan Peyton     KA_TRACE(
31593041982dSJonathan Peyton         20,
31603041982dSJonathan Peyton         ("__kmp_enable_tasking(exit): T#%d: threads array already set up.\n",
31617cc577a4SJonathan Peyton          __kmp_gtid_from_thread(this_thr)));
31627cc577a4SJonathan Peyton     return;
31637cc577a4SJonathan Peyton   }
31647cc577a4SJonathan Peyton   threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
31657cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(threads_data != NULL);
31667cc577a4SJonathan Peyton 
31679b8bb323SJonathan Peyton   if (__kmp_tasking_mode == tskm_task_teams &&
31683041982dSJonathan Peyton       (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME)) {
31697cc577a4SJonathan Peyton     // Release any threads sleeping at the barrier, so that they can steal
31707cc577a4SJonathan Peyton     // tasks and execute them.  In extra barrier mode, tasks do not sleep
31717cc577a4SJonathan Peyton     // at the separate tasking barrier, so this isn't a problem.
31727cc577a4SJonathan Peyton     for (i = 0; i < nthreads; i++) {
31737cc577a4SJonathan Peyton       volatile void *sleep_loc;
31747cc577a4SJonathan Peyton       kmp_info_t *thread = threads_data[i].td.td_thr;
31757cc577a4SJonathan Peyton 
31767cc577a4SJonathan Peyton       if (i == this_thr->th.th_info.ds.ds_tid) {
31777cc577a4SJonathan Peyton         continue;
31787cc577a4SJonathan Peyton       }
31797cc577a4SJonathan Peyton       // Since we haven't locked the thread's suspend mutex lock at this
31807cc577a4SJonathan Peyton       // point, there is a small window where a thread might be putting
31817cc577a4SJonathan Peyton       // itself to sleep, but hasn't set the th_sleep_loc field yet.
31827cc577a4SJonathan Peyton       // To work around this, __kmp_execute_tasks_template() periodically checks
31833041982dSJonathan Peyton       // see if other threads are sleeping (using the same random mechanism that
31843041982dSJonathan Peyton       // is used for task stealing) and awakens them if they are.
3185c47afcd9SAndrey Churbanov       if ((sleep_loc = TCR_PTR(CCAST(void *, thread->th.th_sleep_loc))) !=
3186c47afcd9SAndrey Churbanov           NULL) {
31877cc577a4SJonathan Peyton         KF_TRACE(50, ("__kmp_enable_tasking: T#%d waking up thread T#%d\n",
31887cc577a4SJonathan Peyton                       __kmp_gtid_from_thread(this_thr),
31897cc577a4SJonathan Peyton                       __kmp_gtid_from_thread(thread)));
31907cc577a4SJonathan Peyton         __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc);
31913041982dSJonathan Peyton       } else {
31927cc577a4SJonathan Peyton         KF_TRACE(50, ("__kmp_enable_tasking: T#%d don't wake up thread T#%d\n",
31937cc577a4SJonathan Peyton                       __kmp_gtid_from_thread(this_thr),
31947cc577a4SJonathan Peyton                       __kmp_gtid_from_thread(thread)));
31957cc577a4SJonathan Peyton       }
31967cc577a4SJonathan Peyton     }
31977cc577a4SJonathan Peyton   }
31987cc577a4SJonathan Peyton 
31997cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmp_enable_tasking(exit): T#%d\n",
32007cc577a4SJonathan Peyton                 __kmp_gtid_from_thread(this_thr)));
32017cc577a4SJonathan Peyton }
32027cc577a4SJonathan Peyton 
32037cc577a4SJonathan Peyton /* // TODO: Check the comment consistency
32047cc577a4SJonathan Peyton  * Utility routines for "task teams".  A task team (kmp_task_t) is kind of
32057cc577a4SJonathan Peyton  * like a shadow of the kmp_team_t data struct, with a different lifetime.
32067cc577a4SJonathan Peyton  * After a child * thread checks into a barrier and calls __kmp_release() from
32077cc577a4SJonathan Peyton  * the particular variant of __kmp_<barrier_kind>_barrier_gather(), it can no
32087cc577a4SJonathan Peyton  * longer assume that the kmp_team_t structure is intact (at any moment, the
32097cc577a4SJonathan Peyton  * master thread may exit the barrier code and free the team data structure,
32107cc577a4SJonathan Peyton  * and return the threads to the thread pool).
32117cc577a4SJonathan Peyton  *
32127cc577a4SJonathan Peyton  * This does not work with the the tasking code, as the thread is still
32137cc577a4SJonathan Peyton  * expected to participate in the execution of any tasks that may have been
32147cc577a4SJonathan Peyton  * spawned my a member of the team, and the thread still needs access to all
32157cc577a4SJonathan Peyton  * to each thread in the team, so that it can steal work from it.
32167cc577a4SJonathan Peyton  *
32177cc577a4SJonathan Peyton  * Enter the existence of the kmp_task_team_t struct.  It employs a reference
32187cc577a4SJonathan Peyton  * counting mechanims, and is allocated by the master thread before calling
32197cc577a4SJonathan Peyton  * __kmp_<barrier_kind>_release, and then is release by the last thread to
32207cc577a4SJonathan Peyton  * exit __kmp_<barrier_kind>_release at the next barrier.  I.e. the lifetimes
32217cc577a4SJonathan Peyton  * of the kmp_task_team_t structs for consecutive barriers can overlap
32227cc577a4SJonathan Peyton  * (and will, unless the master thread is the last thread to exit the barrier
32237cc577a4SJonathan Peyton  * release phase, which is not typical).
32247cc577a4SJonathan Peyton  *
32257cc577a4SJonathan Peyton  * The existence of such a struct is useful outside the context of tasking,
32267cc577a4SJonathan Peyton  * but for now, I'm trying to keep it specific to the OMP_30_ENABLED macro,
32277cc577a4SJonathan Peyton  * so that any performance differences show up when comparing the 2.5 vs. 3.0
32287cc577a4SJonathan Peyton  * libraries.
32297cc577a4SJonathan Peyton  *
32307cc577a4SJonathan Peyton  * We currently use the existence of the threads array as an indicator that
32317cc577a4SJonathan Peyton  * tasks were spawned since the last barrier.  If the structure is to be
32327cc577a4SJonathan Peyton  * useful outside the context of tasking, then this will have to change, but
32337cc577a4SJonathan Peyton  * not settting the field minimizes the performance impact of tasking on
32347cc577a4SJonathan Peyton  * barriers, when no explicit tasks were spawned (pushed, actually).
32357cc577a4SJonathan Peyton  */
32367cc577a4SJonathan Peyton 
32373041982dSJonathan Peyton static kmp_task_team_t *__kmp_free_task_teams =
32383041982dSJonathan Peyton     NULL; // Free list for task_team data structures
32397cc577a4SJonathan Peyton // Lock for task team data structures
3240eaa9e40cSJonathan Peyton kmp_bootstrap_lock_t __kmp_task_team_lock =
32413041982dSJonathan Peyton     KMP_BOOTSTRAP_LOCK_INITIALIZER(__kmp_task_team_lock);
32427cc577a4SJonathan Peyton 
32437cc577a4SJonathan Peyton // __kmp_alloc_task_deque:
32447cc577a4SJonathan Peyton // Allocates a task deque for a particular thread, and initialize the necessary
32457cc577a4SJonathan Peyton // data structures relating to the deque.  This only happens once per thread
32463041982dSJonathan Peyton // per task team since task teams are recycled. No lock is needed during
32473041982dSJonathan Peyton // allocation since each thread allocates its own deque.
32483041982dSJonathan Peyton static void __kmp_alloc_task_deque(kmp_info_t *thread,
32493041982dSJonathan Peyton                                    kmp_thread_data_t *thread_data) {
32507cc577a4SJonathan Peyton   __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
32517cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(thread_data->td.td_deque == NULL);
32527cc577a4SJonathan Peyton 
32537cc577a4SJonathan Peyton   // Initialize last stolen task field to "none"
32547cc577a4SJonathan Peyton   thread_data->td.td_deque_last_stolen = -1;
32557cc577a4SJonathan Peyton 
32567cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == 0);
32577cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(thread_data->td.td_deque_head == 0);
32587cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(thread_data->td.td_deque_tail == 0);
32597cc577a4SJonathan Peyton 
32603041982dSJonathan Peyton   KE_TRACE(
32613041982dSJonathan Peyton       10,
32623041982dSJonathan Peyton       ("__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n",
32637cc577a4SJonathan Peyton        __kmp_gtid_from_thread(thread), INITIAL_TASK_DEQUE_SIZE, thread_data));
32647cc577a4SJonathan Peyton   // Allocate space for task deque, and zero the deque
32657cc577a4SJonathan Peyton   // Cannot use __kmp_thread_calloc() because threads not around for
32667cc577a4SJonathan Peyton   // kmp_reap_task_team( ).
32673041982dSJonathan Peyton   thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
32683041982dSJonathan Peyton       INITIAL_TASK_DEQUE_SIZE * sizeof(kmp_taskdata_t *));
32697cc577a4SJonathan Peyton   thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
32707cc577a4SJonathan Peyton }
32717cc577a4SJonathan Peyton 
32727cc577a4SJonathan Peyton // __kmp_free_task_deque:
32733041982dSJonathan Peyton // Deallocates a task deque for a particular thread. Happens at library
32743041982dSJonathan Peyton // deallocation so don't need to reset all thread data fields.
32753041982dSJonathan Peyton static void __kmp_free_task_deque(kmp_thread_data_t *thread_data) {
32767cc577a4SJonathan Peyton   if (thread_data->td.td_deque != NULL) {
32771b536724SJonathan Peyton     __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
32787cc577a4SJonathan Peyton     TCW_4(thread_data->td.td_deque_ntasks, 0);
32797cc577a4SJonathan Peyton     __kmp_free(thread_data->td.td_deque);
32807cc577a4SJonathan Peyton     thread_data->td.td_deque = NULL;
32817cc577a4SJonathan Peyton     __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
32821b536724SJonathan Peyton   }
32837cc577a4SJonathan Peyton 
32847cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK
32857cc577a4SJonathan Peyton   // GEH: Figure out what to do here for td_susp_tied_tasks
32867cc577a4SJonathan Peyton   if (thread_data->td.td_susp_tied_tasks.ts_entries != TASK_STACK_EMPTY) {
32877cc577a4SJonathan Peyton     __kmp_free_task_stack(__kmp_thread_from_gtid(gtid), thread_data);
32887cc577a4SJonathan Peyton   }
32897cc577a4SJonathan Peyton #endif // BUILD_TIED_TASK_STACK
32907cc577a4SJonathan Peyton }
32917cc577a4SJonathan Peyton 
32927cc577a4SJonathan Peyton // __kmp_realloc_task_threads_data:
32933041982dSJonathan Peyton // Allocates a threads_data array for a task team, either by allocating an
32943041982dSJonathan Peyton // initial array or enlarging an existing array.  Only the first thread to get
32953041982dSJonathan Peyton // the lock allocs or enlarges the array and re-initializes the array eleemnts.
32967cc577a4SJonathan Peyton // That thread returns "TRUE", the rest return "FALSE".
32977cc577a4SJonathan Peyton // Assumes that the new array size is given by task_team -> tt.tt_nproc.
32987cc577a4SJonathan Peyton // The current size is given by task_team -> tt.tt_max_threads.
32993041982dSJonathan Peyton static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
33003041982dSJonathan Peyton                                            kmp_task_team_t *task_team) {
33017cc577a4SJonathan Peyton   kmp_thread_data_t **threads_data_p;
33027cc577a4SJonathan Peyton   kmp_int32 nthreads, maxthreads;
33037cc577a4SJonathan Peyton   int is_init_thread = FALSE;
33047cc577a4SJonathan Peyton 
33057cc577a4SJonathan Peyton   if (TCR_4(task_team->tt.tt_found_tasks)) {
33067cc577a4SJonathan Peyton     // Already reallocated and initialized.
33077cc577a4SJonathan Peyton     return FALSE;
33087cc577a4SJonathan Peyton   }
33097cc577a4SJonathan Peyton 
33107cc577a4SJonathan Peyton   threads_data_p = &task_team->tt.tt_threads_data;
33117cc577a4SJonathan Peyton   nthreads = task_team->tt.tt_nproc;
33127cc577a4SJonathan Peyton   maxthreads = task_team->tt.tt_max_threads;
33137cc577a4SJonathan Peyton 
33143041982dSJonathan Peyton   // All threads must lock when they encounter the first task of the implicit
33153041982dSJonathan Peyton   // task region to make sure threads_data fields are (re)initialized before
33163041982dSJonathan Peyton   // used.
33177cc577a4SJonathan Peyton   __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
33187cc577a4SJonathan Peyton 
33197cc577a4SJonathan Peyton   if (!TCR_4(task_team->tt.tt_found_tasks)) {
33207cc577a4SJonathan Peyton     // first thread to enable tasking
33217cc577a4SJonathan Peyton     kmp_team_t *team = thread->th.th_team;
33227cc577a4SJonathan Peyton     int i;
33237cc577a4SJonathan Peyton 
33247cc577a4SJonathan Peyton     is_init_thread = TRUE;
33257cc577a4SJonathan Peyton     if (maxthreads < nthreads) {
33267cc577a4SJonathan Peyton 
33277cc577a4SJonathan Peyton       if (*threads_data_p != NULL) {
33287cc577a4SJonathan Peyton         kmp_thread_data_t *old_data = *threads_data_p;
33297cc577a4SJonathan Peyton         kmp_thread_data_t *new_data = NULL;
33307cc577a4SJonathan Peyton 
33313041982dSJonathan Peyton         KE_TRACE(
33323041982dSJonathan Peyton             10,
33333041982dSJonathan Peyton             ("__kmp_realloc_task_threads_data: T#%d reallocating "
33347cc577a4SJonathan Peyton              "threads data for task_team %p, new_size = %d, old_size = %d\n",
33353041982dSJonathan Peyton              __kmp_gtid_from_thread(thread), task_team, nthreads, maxthreads));
33367cc577a4SJonathan Peyton         // Reallocate threads_data to have more elements than current array
33377cc577a4SJonathan Peyton         // Cannot use __kmp_thread_realloc() because threads not around for
33387cc577a4SJonathan Peyton         // kmp_reap_task_team( ).  Note all new array entries are initialized
33397cc577a4SJonathan Peyton         // to zero by __kmp_allocate().
33403041982dSJonathan Peyton         new_data = (kmp_thread_data_t *)__kmp_allocate(
33413041982dSJonathan Peyton             nthreads * sizeof(kmp_thread_data_t));
33427cc577a4SJonathan Peyton         // copy old data to new data
33437cc577a4SJonathan Peyton         KMP_MEMCPY_S((void *)new_data, nthreads * sizeof(kmp_thread_data_t),
334471483f2dSAndrey Churbanov                      (void *)old_data, maxthreads * sizeof(kmp_thread_data_t));
33457cc577a4SJonathan Peyton 
33467cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK
33477cc577a4SJonathan Peyton         // GEH: Figure out if this is the right thing to do
33487cc577a4SJonathan Peyton         for (i = maxthreads; i < nthreads; i++) {
33497cc577a4SJonathan Peyton           kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
33507cc577a4SJonathan Peyton           __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
33517cc577a4SJonathan Peyton         }
33527cc577a4SJonathan Peyton #endif // BUILD_TIED_TASK_STACK
33537cc577a4SJonathan Peyton         // Install the new data and free the old data
33547cc577a4SJonathan Peyton         (*threads_data_p) = new_data;
33557cc577a4SJonathan Peyton         __kmp_free(old_data);
33563041982dSJonathan Peyton       } else {
33577cc577a4SJonathan Peyton         KE_TRACE(10, ("__kmp_realloc_task_threads_data: T#%d allocating "
33587cc577a4SJonathan Peyton                       "threads data for task_team %p, size = %d\n",
33597cc577a4SJonathan Peyton                       __kmp_gtid_from_thread(thread), task_team, nthreads));
33607cc577a4SJonathan Peyton         // Make the initial allocate for threads_data array, and zero entries
33617cc577a4SJonathan Peyton         // Cannot use __kmp_thread_calloc() because threads not around for
33627cc577a4SJonathan Peyton         // kmp_reap_task_team( ).
33637cc577a4SJonathan Peyton         ANNOTATE_IGNORE_WRITES_BEGIN();
33643041982dSJonathan Peyton         *threads_data_p = (kmp_thread_data_t *)__kmp_allocate(
33653041982dSJonathan Peyton             nthreads * sizeof(kmp_thread_data_t));
33667cc577a4SJonathan Peyton         ANNOTATE_IGNORE_WRITES_END();
33677cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK
33687cc577a4SJonathan Peyton         // GEH: Figure out if this is the right thing to do
33697cc577a4SJonathan Peyton         for (i = 0; i < nthreads; i++) {
33707cc577a4SJonathan Peyton           kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
33717cc577a4SJonathan Peyton           __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
33727cc577a4SJonathan Peyton         }
33737cc577a4SJonathan Peyton #endif // BUILD_TIED_TASK_STACK
33747cc577a4SJonathan Peyton       }
33757cc577a4SJonathan Peyton       task_team->tt.tt_max_threads = nthreads;
33763041982dSJonathan Peyton     } else {
33777cc577a4SJonathan Peyton       // If array has (more than) enough elements, go ahead and use it
33787cc577a4SJonathan Peyton       KMP_DEBUG_ASSERT(*threads_data_p != NULL);
33797cc577a4SJonathan Peyton     }
33807cc577a4SJonathan Peyton 
33817cc577a4SJonathan Peyton     // initialize threads_data pointers back to thread_info structures
33827cc577a4SJonathan Peyton     for (i = 0; i < nthreads; i++) {
33837cc577a4SJonathan Peyton       kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
33847cc577a4SJonathan Peyton       thread_data->td.td_thr = team->t.t_threads[i];
33857cc577a4SJonathan Peyton 
33867cc577a4SJonathan Peyton       if (thread_data->td.td_deque_last_stolen >= nthreads) {
33877cc577a4SJonathan Peyton         // The last stolen field survives across teams / barrier, and the number
33887cc577a4SJonathan Peyton         // of threads may have changed.  It's possible (likely?) that a new
33893041982dSJonathan Peyton         // parallel region will exhibit the same behavior as previous region.
33907cc577a4SJonathan Peyton         thread_data->td.td_deque_last_stolen = -1;
33917cc577a4SJonathan Peyton       }
33927cc577a4SJonathan Peyton     }
33937cc577a4SJonathan Peyton 
33947cc577a4SJonathan Peyton     KMP_MB();
33957cc577a4SJonathan Peyton     TCW_SYNC_4(task_team->tt.tt_found_tasks, TRUE);
33967cc577a4SJonathan Peyton   }
33977cc577a4SJonathan Peyton 
33987cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
33997cc577a4SJonathan Peyton   return is_init_thread;
34007cc577a4SJonathan Peyton }
34017cc577a4SJonathan Peyton 
34027cc577a4SJonathan Peyton // __kmp_free_task_threads_data:
34037cc577a4SJonathan Peyton // Deallocates a threads_data array for a task team, including any attached
34047cc577a4SJonathan Peyton // tasking deques.  Only occurs at library shutdown.
34053041982dSJonathan Peyton static void __kmp_free_task_threads_data(kmp_task_team_t *task_team) {
34067cc577a4SJonathan Peyton   __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
34077cc577a4SJonathan Peyton   if (task_team->tt.tt_threads_data != NULL) {
34087cc577a4SJonathan Peyton     int i;
34097cc577a4SJonathan Peyton     for (i = 0; i < task_team->tt.tt_max_threads; i++) {
34107cc577a4SJonathan Peyton       __kmp_free_task_deque(&task_team->tt.tt_threads_data[i]);
34117cc577a4SJonathan Peyton     }
34127cc577a4SJonathan Peyton     __kmp_free(task_team->tt.tt_threads_data);
34137cc577a4SJonathan Peyton     task_team->tt.tt_threads_data = NULL;
34147cc577a4SJonathan Peyton   }
34157cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
34167cc577a4SJonathan Peyton }
34177cc577a4SJonathan Peyton 
34187cc577a4SJonathan Peyton // __kmp_allocate_task_team:
34197cc577a4SJonathan Peyton // Allocates a task team associated with a specific team, taking it from
34203041982dSJonathan Peyton // the global task team free list if possible.  Also initializes data
34213041982dSJonathan Peyton // structures.
34223041982dSJonathan Peyton static kmp_task_team_t *__kmp_allocate_task_team(kmp_info_t *thread,
34233041982dSJonathan Peyton                                                  kmp_team_t *team) {
34247cc577a4SJonathan Peyton   kmp_task_team_t *task_team = NULL;
34257cc577a4SJonathan Peyton   int nthreads;
34267cc577a4SJonathan Peyton 
34277cc577a4SJonathan Peyton   KA_TRACE(20, ("__kmp_allocate_task_team: T#%d entering; team = %p\n",
34287cc577a4SJonathan Peyton                 (thread ? __kmp_gtid_from_thread(thread) : -1), team));
34297cc577a4SJonathan Peyton 
34307cc577a4SJonathan Peyton   if (TCR_PTR(__kmp_free_task_teams) != NULL) {
34317cc577a4SJonathan Peyton     // Take a task team from the task team pool
34327cc577a4SJonathan Peyton     __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
34337cc577a4SJonathan Peyton     if (__kmp_free_task_teams != NULL) {
34347cc577a4SJonathan Peyton       task_team = __kmp_free_task_teams;
34357cc577a4SJonathan Peyton       TCW_PTR(__kmp_free_task_teams, task_team->tt.tt_next);
34367cc577a4SJonathan Peyton       task_team->tt.tt_next = NULL;
34377cc577a4SJonathan Peyton     }
34387cc577a4SJonathan Peyton     __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
34397cc577a4SJonathan Peyton   }
34407cc577a4SJonathan Peyton 
34417cc577a4SJonathan Peyton   if (task_team == NULL) {
34427cc577a4SJonathan Peyton     KE_TRACE(10, ("__kmp_allocate_task_team: T#%d allocating "
34437cc577a4SJonathan Peyton                   "task team for team %p\n",
34447cc577a4SJonathan Peyton                   __kmp_gtid_from_thread(thread), team));
34457cc577a4SJonathan Peyton     // Allocate a new task team if one is not available.
34467cc577a4SJonathan Peyton     // Cannot use __kmp_thread_malloc() because threads not around for
34477cc577a4SJonathan Peyton     // kmp_reap_task_team( ).
34487cc577a4SJonathan Peyton     task_team = (kmp_task_team_t *)__kmp_allocate(sizeof(kmp_task_team_t));
34497cc577a4SJonathan Peyton     __kmp_init_bootstrap_lock(&task_team->tt.tt_threads_lock);
34503041982dSJonathan Peyton     // AC: __kmp_allocate zeroes returned memory
34513041982dSJonathan Peyton     // task_team -> tt.tt_threads_data = NULL;
34527cc577a4SJonathan Peyton     // task_team -> tt.tt_max_threads = 0;
34537cc577a4SJonathan Peyton     // task_team -> tt.tt_next = NULL;
34547cc577a4SJonathan Peyton   }
34557cc577a4SJonathan Peyton 
34567cc577a4SJonathan Peyton   TCW_4(task_team->tt.tt_found_tasks, FALSE);
34577cc577a4SJonathan Peyton #if OMP_45_ENABLED
34587cc577a4SJonathan Peyton   TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
34597cc577a4SJonathan Peyton #endif
34607cc577a4SJonathan Peyton   task_team->tt.tt_nproc = nthreads = team->t.t_nproc;
34617cc577a4SJonathan Peyton 
346237e2ef54SJonathan Peyton   KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads, nthreads);
34637cc577a4SJonathan Peyton   TCW_4(task_team->tt.tt_active, TRUE);
34647cc577a4SJonathan Peyton 
34653041982dSJonathan Peyton   KA_TRACE(20, ("__kmp_allocate_task_team: T#%d exiting; task_team = %p "
34663041982dSJonathan Peyton                 "unfinished_threads init'd to %d\n",
34673041982dSJonathan Peyton                 (thread ? __kmp_gtid_from_thread(thread) : -1), task_team,
346837e2ef54SJonathan Peyton                 KMP_ATOMIC_LD_RLX(&task_team->tt.tt_unfinished_threads)));
34697cc577a4SJonathan Peyton   return task_team;
34707cc577a4SJonathan Peyton }
34717cc577a4SJonathan Peyton 
34727cc577a4SJonathan Peyton // __kmp_free_task_team:
34737cc577a4SJonathan Peyton // Frees the task team associated with a specific thread, and adds it
34747cc577a4SJonathan Peyton // to the global task team free list.
34753041982dSJonathan Peyton void __kmp_free_task_team(kmp_info_t *thread, kmp_task_team_t *task_team) {
34767cc577a4SJonathan Peyton   KA_TRACE(20, ("__kmp_free_task_team: T#%d task_team = %p\n",
34777cc577a4SJonathan Peyton                 thread ? __kmp_gtid_from_thread(thread) : -1, task_team));
34787cc577a4SJonathan Peyton 
34797cc577a4SJonathan Peyton   // Put task team back on free list
34807cc577a4SJonathan Peyton   __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
34817cc577a4SJonathan Peyton 
34827cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task_team->tt.tt_next == NULL);
34837cc577a4SJonathan Peyton   task_team->tt.tt_next = __kmp_free_task_teams;
34847cc577a4SJonathan Peyton   TCW_PTR(__kmp_free_task_teams, task_team);
34857cc577a4SJonathan Peyton 
34867cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
34877cc577a4SJonathan Peyton }
34887cc577a4SJonathan Peyton 
34897cc577a4SJonathan Peyton // __kmp_reap_task_teams:
34907cc577a4SJonathan Peyton // Free all the task teams on the task team free list.
34917cc577a4SJonathan Peyton // Should only be done during library shutdown.
34923041982dSJonathan Peyton // Cannot do anything that needs a thread structure or gtid since they are
34933041982dSJonathan Peyton // already gone.
34943041982dSJonathan Peyton void __kmp_reap_task_teams(void) {
34957cc577a4SJonathan Peyton   kmp_task_team_t *task_team;
34967cc577a4SJonathan Peyton 
34977cc577a4SJonathan Peyton   if (TCR_PTR(__kmp_free_task_teams) != NULL) {
34987cc577a4SJonathan Peyton     // Free all task_teams on the free list
34997cc577a4SJonathan Peyton     __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
35007cc577a4SJonathan Peyton     while ((task_team = __kmp_free_task_teams) != NULL) {
35017cc577a4SJonathan Peyton       __kmp_free_task_teams = task_team->tt.tt_next;
35027cc577a4SJonathan Peyton       task_team->tt.tt_next = NULL;
35037cc577a4SJonathan Peyton 
35047cc577a4SJonathan Peyton       // Free threads_data if necessary
35057cc577a4SJonathan Peyton       if (task_team->tt.tt_threads_data != NULL) {
35067cc577a4SJonathan Peyton         __kmp_free_task_threads_data(task_team);
35077cc577a4SJonathan Peyton       }
35087cc577a4SJonathan Peyton       __kmp_free(task_team);
35097cc577a4SJonathan Peyton     }
35107cc577a4SJonathan Peyton     __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
35117cc577a4SJonathan Peyton   }
35127cc577a4SJonathan Peyton }
35137cc577a4SJonathan Peyton 
35147cc577a4SJonathan Peyton // __kmp_wait_to_unref_task_teams:
35157cc577a4SJonathan Peyton // Some threads could still be in the fork barrier release code, possibly
35167cc577a4SJonathan Peyton // trying to steal tasks.  Wait for each thread to unreference its task team.
35173041982dSJonathan Peyton void __kmp_wait_to_unref_task_teams(void) {
35187cc577a4SJonathan Peyton   kmp_info_t *thread;
35197cc577a4SJonathan Peyton   kmp_uint32 spins;
35207cc577a4SJonathan Peyton   int done;
35217cc577a4SJonathan Peyton 
35227cc577a4SJonathan Peyton   KMP_INIT_YIELD(spins);
35237cc577a4SJonathan Peyton 
35247cc577a4SJonathan Peyton   for (;;) {
35257cc577a4SJonathan Peyton     done = TRUE;
35267cc577a4SJonathan Peyton 
35277cc577a4SJonathan Peyton     // TODO: GEH - this may be is wrong because some sync would be necessary
35283041982dSJonathan Peyton     // in case threads are added to the pool during the traversal. Need to
35293041982dSJonathan Peyton     // verify that lock for thread pool is held when calling this routine.
3530c47afcd9SAndrey Churbanov     for (thread = CCAST(kmp_info_t *, __kmp_thread_pool); thread != NULL;
35313041982dSJonathan Peyton          thread = thread->th.th_next_pool) {
35327cc577a4SJonathan Peyton #if KMP_OS_WINDOWS
35337cc577a4SJonathan Peyton       DWORD exit_val;
35347cc577a4SJonathan Peyton #endif
35357cc577a4SJonathan Peyton       if (TCR_PTR(thread->th.th_task_team) == NULL) {
35367cc577a4SJonathan Peyton         KA_TRACE(10, ("__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n",
35377cc577a4SJonathan Peyton                       __kmp_gtid_from_thread(thread)));
35387cc577a4SJonathan Peyton         continue;
35397cc577a4SJonathan Peyton       }
35407cc577a4SJonathan Peyton #if KMP_OS_WINDOWS
35417cc577a4SJonathan Peyton       // TODO: GEH - add this check for Linux* OS / OS X* as well?
35427cc577a4SJonathan Peyton       if (!__kmp_is_thread_alive(thread, &exit_val)) {
35437cc577a4SJonathan Peyton         thread->th.th_task_team = NULL;
35447cc577a4SJonathan Peyton         continue;
35457cc577a4SJonathan Peyton       }
35467cc577a4SJonathan Peyton #endif
35477cc577a4SJonathan Peyton 
35487cc577a4SJonathan Peyton       done = FALSE; // Because th_task_team pointer is not NULL for this thread
35497cc577a4SJonathan Peyton 
35503041982dSJonathan Peyton       KA_TRACE(10, ("__kmp_wait_to_unref_task_team: Waiting for T#%d to "
35513041982dSJonathan Peyton                     "unreference task_team\n",
35527cc577a4SJonathan Peyton                     __kmp_gtid_from_thread(thread)));
35537cc577a4SJonathan Peyton 
35547cc577a4SJonathan Peyton       if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
35557cc577a4SJonathan Peyton         volatile void *sleep_loc;
35567cc577a4SJonathan Peyton         // If the thread is sleeping, awaken it.
3557c47afcd9SAndrey Churbanov         if ((sleep_loc = TCR_PTR(CCAST(void *, thread->th.th_sleep_loc))) !=
3558c47afcd9SAndrey Churbanov             NULL) {
35593041982dSJonathan Peyton           KA_TRACE(
35603041982dSJonathan Peyton               10,
35613041982dSJonathan Peyton               ("__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n",
35627cc577a4SJonathan Peyton                __kmp_gtid_from_thread(thread), __kmp_gtid_from_thread(thread)));
35637cc577a4SJonathan Peyton           __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc);
35647cc577a4SJonathan Peyton         }
35657cc577a4SJonathan Peyton       }
35667cc577a4SJonathan Peyton     }
35677cc577a4SJonathan Peyton     if (done) {
35687cc577a4SJonathan Peyton       break;
35697cc577a4SJonathan Peyton     }
35707cc577a4SJonathan Peyton 
3571e47d32f1SJonathan Peyton     // If oversubscribed or have waited a bit, yield.
3572e47d32f1SJonathan Peyton     KMP_YIELD_OVERSUB_ELSE_SPIN(spins);
35737cc577a4SJonathan Peyton   }
35747cc577a4SJonathan Peyton }
35757cc577a4SJonathan Peyton 
35767cc577a4SJonathan Peyton // __kmp_task_team_setup:  Create a task_team for the current team, but use
35777cc577a4SJonathan Peyton // an already created, unused one if it already exists.
35783041982dSJonathan Peyton void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team, int always) {
35797cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
35807cc577a4SJonathan Peyton 
35813041982dSJonathan Peyton   // If this task_team hasn't been created yet, allocate it. It will be used in
35823041982dSJonathan Peyton   // the region after the next.
35833041982dSJonathan Peyton   // If it exists, it is the current task team and shouldn't be touched yet as
35843041982dSJonathan Peyton   // it may still be in use.
35853041982dSJonathan Peyton   if (team->t.t_task_team[this_thr->th.th_task_state] == NULL &&
35863041982dSJonathan Peyton       (always || team->t.t_nproc > 1)) {
35873041982dSJonathan Peyton     team->t.t_task_team[this_thr->th.th_task_state] =
35883041982dSJonathan Peyton         __kmp_allocate_task_team(this_thr, team);
35893041982dSJonathan Peyton     KA_TRACE(20, ("__kmp_task_team_setup: Master T#%d created new task_team %p "
35903041982dSJonathan Peyton                   "for team %d at parity=%d\n",
35913041982dSJonathan Peyton                   __kmp_gtid_from_thread(this_thr),
35923041982dSJonathan Peyton                   team->t.t_task_team[this_thr->th.th_task_state],
35933041982dSJonathan Peyton                   ((team != NULL) ? team->t.t_id : -1),
35943041982dSJonathan Peyton                   this_thr->th.th_task_state));
35957cc577a4SJonathan Peyton   }
35967cc577a4SJonathan Peyton 
35973041982dSJonathan Peyton   // After threads exit the release, they will call sync, and then point to this
35983041982dSJonathan Peyton   // other task_team; make sure it is allocated and properly initialized. As
35993041982dSJonathan Peyton   // threads spin in the barrier release phase, they will continue to use the
36003041982dSJonathan Peyton   // previous task_team struct(above), until they receive the signal to stop
36013041982dSJonathan Peyton   // checking for tasks (they can't safely reference the kmp_team_t struct,
36023041982dSJonathan Peyton   // which could be reallocated by the master thread). No task teams are formed
36033041982dSJonathan Peyton   // for serialized teams.
36047cc577a4SJonathan Peyton   if (team->t.t_nproc > 1) {
36057cc577a4SJonathan Peyton     int other_team = 1 - this_thr->th.th_task_state;
36067cc577a4SJonathan Peyton     if (team->t.t_task_team[other_team] == NULL) { // setup other team as well
36073041982dSJonathan Peyton       team->t.t_task_team[other_team] =
36083041982dSJonathan Peyton           __kmp_allocate_task_team(this_thr, team);
36093041982dSJonathan Peyton       KA_TRACE(20, ("__kmp_task_team_setup: Master T#%d created second new "
36103041982dSJonathan Peyton                     "task_team %p for team %d at parity=%d\n",
36113041982dSJonathan Peyton                     __kmp_gtid_from_thread(this_thr),
36123041982dSJonathan Peyton                     team->t.t_task_team[other_team],
36137cc577a4SJonathan Peyton                     ((team != NULL) ? team->t.t_id : -1), other_team));
36143041982dSJonathan Peyton     } else { // Leave the old task team struct in place for the upcoming region;
36153041982dSJonathan Peyton       // adjust as needed
36167cc577a4SJonathan Peyton       kmp_task_team_t *task_team = team->t.t_task_team[other_team];
36173041982dSJonathan Peyton       if (!task_team->tt.tt_active ||
36183041982dSJonathan Peyton           team->t.t_nproc != task_team->tt.tt_nproc) {
36197cc577a4SJonathan Peyton         TCW_4(task_team->tt.tt_nproc, team->t.t_nproc);
36207cc577a4SJonathan Peyton         TCW_4(task_team->tt.tt_found_tasks, FALSE);
36217cc577a4SJonathan Peyton #if OMP_45_ENABLED
36227cc577a4SJonathan Peyton         TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
36237cc577a4SJonathan Peyton #endif
362437e2ef54SJonathan Peyton         KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads,
362537e2ef54SJonathan Peyton                           team->t.t_nproc);
36267cc577a4SJonathan Peyton         TCW_4(task_team->tt.tt_active, TRUE);
36277cc577a4SJonathan Peyton       }
36283041982dSJonathan Peyton       // if team size has changed, the first thread to enable tasking will
36293041982dSJonathan Peyton       // realloc threads_data if necessary
36303041982dSJonathan Peyton       KA_TRACE(20, ("__kmp_task_team_setup: Master T#%d reset next task_team "
36313041982dSJonathan Peyton                     "%p for team %d at parity=%d\n",
36323041982dSJonathan Peyton                     __kmp_gtid_from_thread(this_thr),
36333041982dSJonathan Peyton                     team->t.t_task_team[other_team],
36347cc577a4SJonathan Peyton                     ((team != NULL) ? team->t.t_id : -1), other_team));
36357cc577a4SJonathan Peyton     }
36367cc577a4SJonathan Peyton   }
36377cc577a4SJonathan Peyton }
36387cc577a4SJonathan Peyton 
36397cc577a4SJonathan Peyton // __kmp_task_team_sync: Propagation of task team data from team to threads
36407cc577a4SJonathan Peyton // which happens just after the release phase of a team barrier.  This may be
36417cc577a4SJonathan Peyton // called by any thread, but only for teams with # threads > 1.
36423041982dSJonathan Peyton void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team) {
36437cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
36447cc577a4SJonathan Peyton 
36453041982dSJonathan Peyton   // Toggle the th_task_state field, to switch which task_team this thread
36463041982dSJonathan Peyton   // refers to
36477cc577a4SJonathan Peyton   this_thr->th.th_task_state = 1 - this_thr->th.th_task_state;
36483041982dSJonathan Peyton   // It is now safe to propagate the task team pointer from the team struct to
36493041982dSJonathan Peyton   // the current thread.
36503041982dSJonathan Peyton   TCW_PTR(this_thr->th.th_task_team,
36513041982dSJonathan Peyton           team->t.t_task_team[this_thr->th.th_task_state]);
36523041982dSJonathan Peyton   KA_TRACE(20,
36533041982dSJonathan Peyton            ("__kmp_task_team_sync: Thread T#%d task team switched to task_team "
36543041982dSJonathan Peyton             "%p from Team #%d (parity=%d)\n",
36557cc577a4SJonathan Peyton             __kmp_gtid_from_thread(this_thr), this_thr->th.th_task_team,
36567cc577a4SJonathan Peyton             ((team != NULL) ? team->t.t_id : -1), this_thr->th.th_task_state));
36577cc577a4SJonathan Peyton }
36587cc577a4SJonathan Peyton 
36593041982dSJonathan Peyton // __kmp_task_team_wait: Master thread waits for outstanding tasks after the
36603041982dSJonathan Peyton // barrier gather phase. Only called by master thread if #threads in team > 1 or
36613041982dSJonathan Peyton // if proxy tasks were created.
36623041982dSJonathan Peyton //
36633041982dSJonathan Peyton // wait is a flag that defaults to 1 (see kmp.h), but waiting can be turned off
36643041982dSJonathan Peyton // by passing in 0 optionally as the last argument. When wait is zero, master
36653041982dSJonathan Peyton // thread does not wait for unfinished_threads to reach 0.
36663041982dSJonathan Peyton void __kmp_task_team_wait(
36673041982dSJonathan Peyton     kmp_info_t *this_thr,
36683041982dSJonathan Peyton     kmp_team_t *team USE_ITT_BUILD_ARG(void *itt_sync_obj), int wait) {
36697cc577a4SJonathan Peyton   kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state];
36707cc577a4SJonathan Peyton 
36717cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
36727cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task_team == this_thr->th.th_task_team);
36737cc577a4SJonathan Peyton 
36747cc577a4SJonathan Peyton   if ((task_team != NULL) && KMP_TASKING_ENABLED(task_team)) {
36757cc577a4SJonathan Peyton     if (wait) {
36763041982dSJonathan Peyton       KA_TRACE(20, ("__kmp_task_team_wait: Master T#%d waiting for all tasks "
36773041982dSJonathan Peyton                     "(for unfinished_threads to reach 0) on task_team = %p\n",
36787cc577a4SJonathan Peyton                     __kmp_gtid_from_thread(this_thr), task_team));
36793041982dSJonathan Peyton       // Worker threads may have dropped through to release phase, but could
36803041982dSJonathan Peyton       // still be executing tasks. Wait here for tasks to complete. To avoid
36813041982dSJonathan Peyton       // memory contention, only master thread checks termination condition.
368237e2ef54SJonathan Peyton       kmp_flag_32 flag(RCAST(std::atomic<kmp_uint32> *,
368337e2ef54SJonathan Peyton                              &task_team->tt.tt_unfinished_threads),
3684c47afcd9SAndrey Churbanov                        0U);
36853041982dSJonathan Peyton       flag.wait(this_thr, TRUE USE_ITT_BUILD_ARG(itt_sync_obj));
36867cc577a4SJonathan Peyton     }
36873041982dSJonathan Peyton     // Deactivate the old task team, so that the worker threads will stop
36883041982dSJonathan Peyton     // referencing it while spinning.
36893041982dSJonathan Peyton     KA_TRACE(
36903041982dSJonathan Peyton         20,
36913041982dSJonathan Peyton         ("__kmp_task_team_wait: Master T#%d deactivating task_team %p: "
36927cc577a4SJonathan Peyton          "setting active to false, setting local and team's pointer to NULL\n",
36937cc577a4SJonathan Peyton          __kmp_gtid_from_thread(this_thr), task_team));
36947cc577a4SJonathan Peyton #if OMP_45_ENABLED
36953041982dSJonathan Peyton     KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1 ||
36963041982dSJonathan Peyton                      task_team->tt.tt_found_proxy_tasks == TRUE);
36977cc577a4SJonathan Peyton     TCW_SYNC_4(task_team->tt.tt_found_proxy_tasks, FALSE);
36987cc577a4SJonathan Peyton #else
36997cc577a4SJonathan Peyton     KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1);
37007cc577a4SJonathan Peyton #endif
3701a756cb24SAndrey Churbanov     KMP_CHECK_UPDATE(task_team->tt.tt_untied_task_encountered, 0);
37027cc577a4SJonathan Peyton     TCW_SYNC_4(task_team->tt.tt_active, FALSE);
37037cc577a4SJonathan Peyton     KMP_MB();
37047cc577a4SJonathan Peyton 
37057cc577a4SJonathan Peyton     TCW_PTR(this_thr->th.th_task_team, NULL);
37067cc577a4SJonathan Peyton   }
37077cc577a4SJonathan Peyton }
37087cc577a4SJonathan Peyton 
37097cc577a4SJonathan Peyton // __kmp_tasking_barrier:
37107cc577a4SJonathan Peyton // This routine may only called when __kmp_tasking_mode == tskm_extra_barrier.
37113041982dSJonathan Peyton // Internal function to execute all tasks prior to a regular barrier or a join
37123041982dSJonathan Peyton // barrier. It is a full barrier itself, which unfortunately turns regular
37133041982dSJonathan Peyton // barriers into double barriers and join barriers into 1 1/2 barriers.
37143041982dSJonathan Peyton void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread, int gtid) {
371537e2ef54SJonathan Peyton   std::atomic<kmp_uint32> *spin = RCAST(
371637e2ef54SJonathan Peyton       std::atomic<kmp_uint32> *,
3717c47afcd9SAndrey Churbanov       &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads);
37187cc577a4SJonathan Peyton   int flag = FALSE;
37197cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode == tskm_extra_barrier);
37207cc577a4SJonathan Peyton 
37217cc577a4SJonathan Peyton #if USE_ITT_BUILD
372237e2ef54SJonathan Peyton   KMP_FSYNC_SPIN_INIT(spin, NULL);
37237cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
37247cc577a4SJonathan Peyton   kmp_flag_32 spin_flag(spin, 0U);
37253041982dSJonathan Peyton   while (!spin_flag.execute_tasks(thread, gtid, TRUE,
37263041982dSJonathan Peyton                                   &flag USE_ITT_BUILD_ARG(NULL), 0)) {
37277cc577a4SJonathan Peyton #if USE_ITT_BUILD
37287cc577a4SJonathan Peyton     // TODO: What about itt_sync_obj??
372937e2ef54SJonathan Peyton     KMP_FSYNC_SPIN_PREPARE(RCAST(void *, spin));
37307cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
37317cc577a4SJonathan Peyton 
37327cc577a4SJonathan Peyton     if (TCR_4(__kmp_global.g.g_done)) {
37337cc577a4SJonathan Peyton       if (__kmp_global.g.g_abort)
37347cc577a4SJonathan Peyton         __kmp_abort_thread();
37357cc577a4SJonathan Peyton       break;
37367cc577a4SJonathan Peyton     }
3737e47d32f1SJonathan Peyton     KMP_YIELD(TRUE);
37387cc577a4SJonathan Peyton   }
37397cc577a4SJonathan Peyton #if USE_ITT_BUILD
374037e2ef54SJonathan Peyton   KMP_FSYNC_SPIN_ACQUIRED(RCAST(void *, spin));
37417cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
37427cc577a4SJonathan Peyton }
37437cc577a4SJonathan Peyton 
37447cc577a4SJonathan Peyton #if OMP_45_ENABLED
37457cc577a4SJonathan Peyton 
37463041982dSJonathan Peyton // __kmp_give_task puts a task into a given thread queue if:
37473041982dSJonathan Peyton //  - the queue for that thread was created
37483041982dSJonathan Peyton //  - there's space in that queue
37493041982dSJonathan Peyton // Because of this, __kmp_push_task needs to check if there's space after
37503041982dSJonathan Peyton // getting the lock
37513041982dSJonathan Peyton static bool __kmp_give_task(kmp_info_t *thread, kmp_int32 tid, kmp_task_t *task,
37523041982dSJonathan Peyton                             kmp_int32 pass) {
37537cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
37547cc577a4SJonathan Peyton   kmp_task_team_t *task_team = taskdata->td_task_team;
37557cc577a4SJonathan Peyton 
37563041982dSJonathan Peyton   KA_TRACE(20, ("__kmp_give_task: trying to give task %p to thread %d.\n",
37573041982dSJonathan Peyton                 taskdata, tid));
37587cc577a4SJonathan Peyton 
37597cc577a4SJonathan Peyton   // If task_team is NULL something went really bad...
37607cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task_team != NULL);
37617cc577a4SJonathan Peyton 
37627cc577a4SJonathan Peyton   bool result = false;
37637cc577a4SJonathan Peyton   kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
37647cc577a4SJonathan Peyton 
37657cc577a4SJonathan Peyton   if (thread_data->td.td_deque == NULL) {
37667cc577a4SJonathan Peyton     // There's no queue in this thread, go find another one
37677cc577a4SJonathan Peyton     // We're guaranteed that at least one thread has a queue
37683041982dSJonathan Peyton     KA_TRACE(30,
37693041982dSJonathan Peyton              ("__kmp_give_task: thread %d has no queue while giving task %p.\n",
37703041982dSJonathan Peyton               tid, taskdata));
37717cc577a4SJonathan Peyton     return result;
37727cc577a4SJonathan Peyton   }
37737cc577a4SJonathan Peyton 
37743041982dSJonathan Peyton   if (TCR_4(thread_data->td.td_deque_ntasks) >=
37753041982dSJonathan Peyton       TASK_DEQUE_SIZE(thread_data->td)) {
37763041982dSJonathan Peyton     KA_TRACE(
37773041982dSJonathan Peyton         30,
37783041982dSJonathan Peyton         ("__kmp_give_task: queue is full while giving task %p to thread %d.\n",
37793041982dSJonathan Peyton          taskdata, tid));
37807cc577a4SJonathan Peyton 
37813041982dSJonathan Peyton     // if this deque is bigger than the pass ratio give a chance to another
37823041982dSJonathan Peyton     // thread
37833041982dSJonathan Peyton     if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
37843041982dSJonathan Peyton       return result;
37857cc577a4SJonathan Peyton 
37867cc577a4SJonathan Peyton     __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
37877cc577a4SJonathan Peyton     __kmp_realloc_task_deque(thread, thread_data);
37887cc577a4SJonathan Peyton 
37897cc577a4SJonathan Peyton   } else {
37907cc577a4SJonathan Peyton 
37917cc577a4SJonathan Peyton     __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
37927cc577a4SJonathan Peyton 
37933041982dSJonathan Peyton     if (TCR_4(thread_data->td.td_deque_ntasks) >=
37943041982dSJonathan Peyton         TASK_DEQUE_SIZE(thread_data->td)) {
37953041982dSJonathan Peyton       KA_TRACE(30, ("__kmp_give_task: queue is full while giving task %p to "
37963041982dSJonathan Peyton                     "thread %d.\n",
37973041982dSJonathan Peyton                     taskdata, tid));
37987cc577a4SJonathan Peyton 
37993041982dSJonathan Peyton       // if this deque is bigger than the pass ratio give a chance to another
38003041982dSJonathan Peyton       // thread
38017cc577a4SJonathan Peyton       if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
38027cc577a4SJonathan Peyton         goto release_and_exit;
38037cc577a4SJonathan Peyton 
38047cc577a4SJonathan Peyton       __kmp_realloc_task_deque(thread, thread_data);
38057cc577a4SJonathan Peyton     }
38067cc577a4SJonathan Peyton   }
38077cc577a4SJonathan Peyton 
38087cc577a4SJonathan Peyton   // lock is held here, and there is space in the deque
38097cc577a4SJonathan Peyton 
38107cc577a4SJonathan Peyton   thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
38117cc577a4SJonathan Peyton   // Wrap index.
38123041982dSJonathan Peyton   thread_data->td.td_deque_tail =
38133041982dSJonathan Peyton       (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
38143041982dSJonathan Peyton   TCW_4(thread_data->td.td_deque_ntasks,
38153041982dSJonathan Peyton         TCR_4(thread_data->td.td_deque_ntasks) + 1);
38167cc577a4SJonathan Peyton 
38177cc577a4SJonathan Peyton   result = true;
38183041982dSJonathan Peyton   KA_TRACE(30, ("__kmp_give_task: successfully gave task %p to thread %d.\n",
38193041982dSJonathan Peyton                 taskdata, tid));
38207cc577a4SJonathan Peyton 
38217cc577a4SJonathan Peyton release_and_exit:
38227cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
38237cc577a4SJonathan Peyton 
38247cc577a4SJonathan Peyton   return result;
38257cc577a4SJonathan Peyton }
38267cc577a4SJonathan Peyton 
38273041982dSJonathan Peyton /* The finish of the proxy tasks is divided in two pieces:
38287cc577a4SJonathan Peyton     - the top half is the one that can be done from a thread outside the team
3829c3344345SAndrey Churbanov     - the bottom half must be run from a thread within the team
38307cc577a4SJonathan Peyton 
38313041982dSJonathan Peyton    In order to run the bottom half the task gets queued back into one of the
38323041982dSJonathan Peyton    threads of the team. Once the td_incomplete_child_task counter of the parent
38333041982dSJonathan Peyton    is decremented the threads can leave the barriers. So, the bottom half needs
38343041982dSJonathan Peyton    to be queued before the counter is decremented. The top half is therefore
38353041982dSJonathan Peyton    divided in two parts:
38367cc577a4SJonathan Peyton     - things that can be run before queuing the bottom half
38377cc577a4SJonathan Peyton     - things that must be run after queuing the bottom half
38387cc577a4SJonathan Peyton 
38393041982dSJonathan Peyton    This creates a second race as the bottom half can free the task before the
38403041982dSJonathan Peyton    second top half is executed. To avoid this we use the
38413041982dSJonathan Peyton    td_incomplete_child_task of the proxy task to synchronize the top and bottom
38423041982dSJonathan Peyton    half. */
38433041982dSJonathan Peyton static void __kmp_first_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
38447cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
38457cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
38467cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
38477cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
38487cc577a4SJonathan Peyton 
38497cc577a4SJonathan Peyton   taskdata->td_flags.complete = 1; // mark the task as completed
38507cc577a4SJonathan Peyton 
38517cc577a4SJonathan Peyton   if (taskdata->td_taskgroup)
385237e2ef54SJonathan Peyton     KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
38537cc577a4SJonathan Peyton 
38543041982dSJonathan Peyton   // Create an imaginary children for this task so the bottom half cannot
38553041982dSJonathan Peyton   // release the task before we have completed the second top half
385637e2ef54SJonathan Peyton   KMP_ATOMIC_INC(&taskdata->td_incomplete_child_tasks);
38577cc577a4SJonathan Peyton }
38587cc577a4SJonathan Peyton 
38593041982dSJonathan Peyton static void __kmp_second_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
38607cc577a4SJonathan Peyton   kmp_int32 children = 0;
38617cc577a4SJonathan Peyton 
38627cc577a4SJonathan Peyton   // Predecrement simulated by "- 1" calculation
38633041982dSJonathan Peyton   children =
386437e2ef54SJonathan Peyton       KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks) - 1;
38657cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(children >= 0);
38667cc577a4SJonathan Peyton 
38677cc577a4SJonathan Peyton   // Remove the imaginary children
386837e2ef54SJonathan Peyton   KMP_ATOMIC_DEC(&taskdata->td_incomplete_child_tasks);
38697cc577a4SJonathan Peyton }
38707cc577a4SJonathan Peyton 
38713041982dSJonathan Peyton static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask) {
38727cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
38737cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
38747cc577a4SJonathan Peyton 
38757cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
38763041982dSJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.complete ==
38773041982dSJonathan Peyton                    1); // top half must run before bottom half
38787cc577a4SJonathan Peyton 
38797cc577a4SJonathan Peyton   // We need to wait to make sure the top half is finished
38807cc577a4SJonathan Peyton   // Spinning here should be ok as this should happen quickly
388137e2ef54SJonathan Peyton   while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) > 0)
38823041982dSJonathan Peyton     ;
38837cc577a4SJonathan Peyton 
38847cc577a4SJonathan Peyton   __kmp_release_deps(gtid, taskdata);
38857cc577a4SJonathan Peyton   __kmp_free_task_and_ancestors(gtid, taskdata, thread);
38867cc577a4SJonathan Peyton }
38877cc577a4SJonathan Peyton 
38887cc577a4SJonathan Peyton /*!
38897cc577a4SJonathan Peyton @ingroup TASKING
38907cc577a4SJonathan Peyton @param gtid Global Thread ID of encountering thread
38917cc577a4SJonathan Peyton @param ptask Task which execution is completed
38927cc577a4SJonathan Peyton 
38933041982dSJonathan Peyton Execute the completation of a proxy task from a thread of that is part of the
38943041982dSJonathan Peyton team. Run first and bottom halves directly.
38957cc577a4SJonathan Peyton */
38963041982dSJonathan Peyton void __kmpc_proxy_task_completed(kmp_int32 gtid, kmp_task_t *ptask) {
38977cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(ptask != NULL);
38987cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
38993041982dSJonathan Peyton   KA_TRACE(
39003041982dSJonathan Peyton       10, ("__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n",
39013041982dSJonathan Peyton            gtid, taskdata));
39027cc577a4SJonathan Peyton 
39037cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
39047cc577a4SJonathan Peyton 
39057cc577a4SJonathan Peyton   __kmp_first_top_half_finish_proxy(taskdata);
39067cc577a4SJonathan Peyton   __kmp_second_top_half_finish_proxy(taskdata);
39077cc577a4SJonathan Peyton   __kmp_bottom_half_finish_proxy(gtid, ptask);
39087cc577a4SJonathan Peyton 
39093041982dSJonathan Peyton   KA_TRACE(10,
39103041982dSJonathan Peyton            ("__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n",
39113041982dSJonathan Peyton             gtid, taskdata));
39127cc577a4SJonathan Peyton }
39137cc577a4SJonathan Peyton 
39147cc577a4SJonathan Peyton /*!
39157cc577a4SJonathan Peyton @ingroup TASKING
39167cc577a4SJonathan Peyton @param ptask Task which execution is completed
39177cc577a4SJonathan Peyton 
39183041982dSJonathan Peyton Execute the completation of a proxy task from a thread that could not belong to
39193041982dSJonathan Peyton the team.
39207cc577a4SJonathan Peyton */
39213041982dSJonathan Peyton void __kmpc_proxy_task_completed_ooo(kmp_task_t *ptask) {
39227cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(ptask != NULL);
39237cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
39247cc577a4SJonathan Peyton 
39253041982dSJonathan Peyton   KA_TRACE(
39263041982dSJonathan Peyton       10,
39273041982dSJonathan Peyton       ("__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n",
39283041982dSJonathan Peyton        taskdata));
39297cc577a4SJonathan Peyton 
39307cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
39317cc577a4SJonathan Peyton 
39327cc577a4SJonathan Peyton   __kmp_first_top_half_finish_proxy(taskdata);
39337cc577a4SJonathan Peyton 
39343041982dSJonathan Peyton   // Enqueue task to complete bottom half completion from a thread within the
39353041982dSJonathan Peyton   // corresponding team
39367cc577a4SJonathan Peyton   kmp_team_t *team = taskdata->td_team;
39377cc577a4SJonathan Peyton   kmp_int32 nthreads = team->t.t_nproc;
39387cc577a4SJonathan Peyton   kmp_info_t *thread;
39397cc577a4SJonathan Peyton 
39403041982dSJonathan Peyton   // This should be similar to start_k = __kmp_get_random( thread ) % nthreads
39413041982dSJonathan Peyton   // but we cannot use __kmp_get_random here
39427cc577a4SJonathan Peyton   kmp_int32 start_k = 0;
39437cc577a4SJonathan Peyton   kmp_int32 pass = 1;
39447cc577a4SJonathan Peyton   kmp_int32 k = start_k;
39457cc577a4SJonathan Peyton 
39467cc577a4SJonathan Peyton   do {
39477cc577a4SJonathan Peyton     // For now we're just linearly trying to find a thread
39487cc577a4SJonathan Peyton     thread = team->t.t_threads[k];
39497cc577a4SJonathan Peyton     k = (k + 1) % nthreads;
39507cc577a4SJonathan Peyton 
39517cc577a4SJonathan Peyton     // we did a full pass through all the threads
39523041982dSJonathan Peyton     if (k == start_k)
39533041982dSJonathan Peyton       pass = pass << 1;
39547cc577a4SJonathan Peyton 
39557cc577a4SJonathan Peyton   } while (!__kmp_give_task(thread, k, ptask, pass));
39567cc577a4SJonathan Peyton 
39577cc577a4SJonathan Peyton   __kmp_second_top_half_finish_proxy(taskdata);
39587cc577a4SJonathan Peyton 
39593041982dSJonathan Peyton   KA_TRACE(
39603041982dSJonathan Peyton       10,
39613041982dSJonathan Peyton       ("__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n",
39623041982dSJonathan Peyton        taskdata));
39637cc577a4SJonathan Peyton }
39647cc577a4SJonathan Peyton 
3965*405037c4SAndrey Churbanov #if OMP_50_ENABLED
3966*405037c4SAndrey Churbanov kmp_event_t *__kmpc_task_allow_completion_event(ident_t *loc_ref, int gtid,
3967*405037c4SAndrey Churbanov                                                 kmp_task_t *task) {
3968*405037c4SAndrey Churbanov   kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(task);
3969*405037c4SAndrey Churbanov   if (td->td_allow_completion_event.type == KMP_EVENT_UNINITIALIZED) {
3970*405037c4SAndrey Churbanov     td->td_allow_completion_event.type = KMP_EVENT_ALLOW_COMPLETION;
3971*405037c4SAndrey Churbanov     td->td_allow_completion_event.ed.task = task;
3972*405037c4SAndrey Churbanov     __kmp_init_tas_lock(&td->td_allow_completion_event.lock);
3973*405037c4SAndrey Churbanov   }
3974*405037c4SAndrey Churbanov   return &td->td_allow_completion_event;
3975*405037c4SAndrey Churbanov }
3976*405037c4SAndrey Churbanov 
3977*405037c4SAndrey Churbanov void __kmp_fulfill_event(kmp_event_t *event) {
3978*405037c4SAndrey Churbanov   if (event->type == KMP_EVENT_ALLOW_COMPLETION) {
3979*405037c4SAndrey Churbanov     kmp_task_t *ptask = event->ed.task;
3980*405037c4SAndrey Churbanov     kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
3981*405037c4SAndrey Churbanov     bool detached = false;
3982*405037c4SAndrey Churbanov     int gtid = __kmp_get_gtid();
3983*405037c4SAndrey Churbanov 
3984*405037c4SAndrey Churbanov     if (taskdata->td_flags.proxy == TASK_PROXY) {
3985*405037c4SAndrey Churbanov       // The associated task code completed before this call and detached.
3986*405037c4SAndrey Churbanov       detached = true;
3987*405037c4SAndrey Churbanov       event->type = KMP_EVENT_UNINITIALIZED;
3988*405037c4SAndrey Churbanov     } else {
3989*405037c4SAndrey Churbanov       // The associated task has not completed but could be completing at this
3990*405037c4SAndrey Churbanov       // point.
3991*405037c4SAndrey Churbanov       // We need to take the lock to avoid races
3992*405037c4SAndrey Churbanov       __kmp_acquire_tas_lock(&event->lock, gtid);
3993*405037c4SAndrey Churbanov       if (taskdata->td_flags.proxy == TASK_PROXY)
3994*405037c4SAndrey Churbanov         detached = true;
3995*405037c4SAndrey Churbanov       event->type = KMP_EVENT_UNINITIALIZED;
3996*405037c4SAndrey Churbanov       __kmp_release_tas_lock(&event->lock, gtid);
3997*405037c4SAndrey Churbanov     }
3998*405037c4SAndrey Churbanov 
3999*405037c4SAndrey Churbanov     if (detached) {
4000*405037c4SAndrey Churbanov       // If the task detached complete the proxy task
4001*405037c4SAndrey Churbanov       if (gtid >= 0) {
4002*405037c4SAndrey Churbanov         kmp_team_t *team = taskdata->td_team;
4003*405037c4SAndrey Churbanov         kmp_info_t *thread = __kmp_get_thread();
4004*405037c4SAndrey Churbanov         if (thread->th.th_team == team) {
4005*405037c4SAndrey Churbanov           __kmpc_proxy_task_completed(gtid, ptask);
4006*405037c4SAndrey Churbanov           return;
4007*405037c4SAndrey Churbanov         }
4008*405037c4SAndrey Churbanov       }
4009*405037c4SAndrey Churbanov 
4010*405037c4SAndrey Churbanov       // fallback
4011*405037c4SAndrey Churbanov       __kmpc_proxy_task_completed_ooo(ptask);
4012*405037c4SAndrey Churbanov     }
4013*405037c4SAndrey Churbanov   }
4014*405037c4SAndrey Churbanov }
4015*405037c4SAndrey Churbanov #endif
4016*405037c4SAndrey Churbanov 
40173041982dSJonathan Peyton // __kmp_task_dup_alloc: Allocate the taskdata and make a copy of source task
40183041982dSJonathan Peyton // for taskloop
40197cc577a4SJonathan Peyton //
40207cc577a4SJonathan Peyton // thread:   allocating thread
40217cc577a4SJonathan Peyton // task_src: pointer to source task to be duplicated
40227cc577a4SJonathan Peyton // returns:  a pointer to the allocated kmp_task_t structure (task).
40233041982dSJonathan Peyton kmp_task_t *__kmp_task_dup_alloc(kmp_info_t *thread, kmp_task_t *task_src) {
40247cc577a4SJonathan Peyton   kmp_task_t *task;
40257cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata;
40267cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata_src;
40277cc577a4SJonathan Peyton   kmp_taskdata_t *parent_task = thread->th.th_current_task;
40287cc577a4SJonathan Peyton   size_t shareds_offset;
40297cc577a4SJonathan Peyton   size_t task_size;
40307cc577a4SJonathan Peyton 
40313041982dSJonathan Peyton   KA_TRACE(10, ("__kmp_task_dup_alloc(enter): Th %p, source task %p\n", thread,
40323041982dSJonathan Peyton                 task_src));
40337cc577a4SJonathan Peyton   taskdata_src = KMP_TASK_TO_TASKDATA(task_src);
40343041982dSJonathan Peyton   KMP_DEBUG_ASSERT(taskdata_src->td_flags.proxy ==
40353041982dSJonathan Peyton                    TASK_FULL); // it should not be proxy task
40367cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata_src->td_flags.tasktype == TASK_EXPLICIT);
40377cc577a4SJonathan Peyton   task_size = taskdata_src->td_size_alloc;
40387cc577a4SJonathan Peyton 
40397cc577a4SJonathan Peyton   // Allocate a kmp_taskdata_t block and a kmp_task_t block.
40403041982dSJonathan Peyton   KA_TRACE(30, ("__kmp_task_dup_alloc: Th %p, malloc size %ld\n", thread,
40413041982dSJonathan Peyton                 task_size));
40427cc577a4SJonathan Peyton #if USE_FAST_MEMORY
40437cc577a4SJonathan Peyton   taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, task_size);
40447cc577a4SJonathan Peyton #else
40457cc577a4SJonathan Peyton   taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, task_size);
40467cc577a4SJonathan Peyton #endif /* USE_FAST_MEMORY */
40477cc577a4SJonathan Peyton   KMP_MEMCPY(taskdata, taskdata_src, task_size);
40487cc577a4SJonathan Peyton 
40497cc577a4SJonathan Peyton   task = KMP_TASKDATA_TO_TASK(taskdata);
40507cc577a4SJonathan Peyton 
40517cc577a4SJonathan Peyton   // Initialize new task (only specific fields not affected by memcpy)
40527cc577a4SJonathan Peyton   taskdata->td_task_id = KMP_GEN_TASK_ID();
40537cc577a4SJonathan Peyton   if (task->shareds != NULL) { // need setup shareds pointer
40547cc577a4SJonathan Peyton     shareds_offset = (char *)task_src->shareds - (char *)taskdata_src;
40557cc577a4SJonathan Peyton     task->shareds = &((char *)taskdata)[shareds_offset];
40563041982dSJonathan Peyton     KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (sizeof(void *) - 1)) ==
40573041982dSJonathan Peyton                      0);
40587cc577a4SJonathan Peyton   }
40597cc577a4SJonathan Peyton   taskdata->td_alloc_thread = thread;
406093e17cfeSJonathan Peyton   taskdata->td_parent = parent_task;
40613041982dSJonathan Peyton   taskdata->td_taskgroup =
40623041982dSJonathan Peyton       parent_task
40633041982dSJonathan Peyton           ->td_taskgroup; // task inherits the taskgroup from the parent task
40647cc577a4SJonathan Peyton 
40653041982dSJonathan Peyton   // Only need to keep track of child task counts if team parallel and tasking
40663041982dSJonathan Peyton   // not serialized
40677cc577a4SJonathan Peyton   if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
406837e2ef54SJonathan Peyton     KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks);
40697cc577a4SJonathan Peyton     if (parent_task->td_taskgroup)
407037e2ef54SJonathan Peyton       KMP_ATOMIC_INC(&parent_task->td_taskgroup->count);
40713041982dSJonathan Peyton     // Only need to keep track of allocated child tasks for explicit tasks since
40723041982dSJonathan Peyton     // implicit not deallocated
40737cc577a4SJonathan Peyton     if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT)
407437e2ef54SJonathan Peyton       KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks);
40757cc577a4SJonathan Peyton   }
40767cc577a4SJonathan Peyton 
40773041982dSJonathan Peyton   KA_TRACE(20,
40783041982dSJonathan Peyton            ("__kmp_task_dup_alloc(exit): Th %p, created task %p, parent=%p\n",
40797cc577a4SJonathan Peyton             thread, taskdata, taskdata->td_parent));
40807cc577a4SJonathan Peyton #if OMPT_SUPPORT
4081f0a1c65fSJonas Hahnfeld   if (UNLIKELY(ompt_enabled.enabled))
408282e94a59SJoachim Protze     __ompt_task_init(taskdata, thread->th.th_info.ds.ds_gtid);
40837cc577a4SJonathan Peyton #endif
40847cc577a4SJonathan Peyton   return task;
40857cc577a4SJonathan Peyton }
40867cc577a4SJonathan Peyton 
40877cc577a4SJonathan Peyton // Routine optionally generated by the compiler for setting the lastprivate flag
40887cc577a4SJonathan Peyton // and calling needed constructors for private/firstprivate objects
40897cc577a4SJonathan Peyton // (used to form taskloop tasks from pattern task)
409093e17cfeSJonathan Peyton // Parameters: dest task, src task, lastprivate flag.
40917cc577a4SJonathan Peyton typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32);
40927cc577a4SJonathan Peyton 
409327a677fcSJonathan Peyton KMP_BUILD_ASSERT(sizeof(long) == 4 || sizeof(long) == 8);
409427a677fcSJonathan Peyton 
409527a677fcSJonathan Peyton // class to encapsulate manipulating loop bounds in a taskloop task.
409627a677fcSJonathan Peyton // this abstracts away the Intel vs GOMP taskloop interface for setting/getting
409727a677fcSJonathan Peyton // the loop bound variables.
409827a677fcSJonathan Peyton class kmp_taskloop_bounds_t {
409927a677fcSJonathan Peyton   kmp_task_t *task;
410027a677fcSJonathan Peyton   const kmp_taskdata_t *taskdata;
410127a677fcSJonathan Peyton   size_t lower_offset;
410227a677fcSJonathan Peyton   size_t upper_offset;
410327a677fcSJonathan Peyton 
410427a677fcSJonathan Peyton public:
410527a677fcSJonathan Peyton   kmp_taskloop_bounds_t(kmp_task_t *_task, kmp_uint64 *lb, kmp_uint64 *ub)
410627a677fcSJonathan Peyton       : task(_task), taskdata(KMP_TASK_TO_TASKDATA(task)),
410727a677fcSJonathan Peyton         lower_offset((char *)lb - (char *)task),
410827a677fcSJonathan Peyton         upper_offset((char *)ub - (char *)task) {
410927a677fcSJonathan Peyton     KMP_DEBUG_ASSERT((char *)lb > (char *)_task);
411027a677fcSJonathan Peyton     KMP_DEBUG_ASSERT((char *)ub > (char *)_task);
411127a677fcSJonathan Peyton   }
411227a677fcSJonathan Peyton   kmp_taskloop_bounds_t(kmp_task_t *_task, const kmp_taskloop_bounds_t &bounds)
411327a677fcSJonathan Peyton       : task(_task), taskdata(KMP_TASK_TO_TASKDATA(_task)),
411427a677fcSJonathan Peyton         lower_offset(bounds.lower_offset), upper_offset(bounds.upper_offset) {}
411527a677fcSJonathan Peyton   size_t get_lower_offset() const { return lower_offset; }
411627a677fcSJonathan Peyton   size_t get_upper_offset() const { return upper_offset; }
411727a677fcSJonathan Peyton   kmp_uint64 get_lb() const {
411827a677fcSJonathan Peyton     kmp_int64 retval;
411927a677fcSJonathan Peyton #if defined(KMP_GOMP_COMPAT)
412027a677fcSJonathan Peyton     // Intel task just returns the lower bound normally
412127a677fcSJonathan Peyton     if (!taskdata->td_flags.native) {
412227a677fcSJonathan Peyton       retval = *(kmp_int64 *)((char *)task + lower_offset);
412327a677fcSJonathan Peyton     } else {
412427a677fcSJonathan Peyton       // GOMP task has to take into account the sizeof(long)
412527a677fcSJonathan Peyton       if (taskdata->td_size_loop_bounds == 4) {
412627a677fcSJonathan Peyton         kmp_int32 *lb = RCAST(kmp_int32 *, task->shareds);
412727a677fcSJonathan Peyton         retval = (kmp_int64)*lb;
412827a677fcSJonathan Peyton       } else {
412927a677fcSJonathan Peyton         kmp_int64 *lb = RCAST(kmp_int64 *, task->shareds);
413027a677fcSJonathan Peyton         retval = (kmp_int64)*lb;
413127a677fcSJonathan Peyton       }
413227a677fcSJonathan Peyton     }
413327a677fcSJonathan Peyton #else
413427a677fcSJonathan Peyton     retval = *(kmp_int64 *)((char *)task + lower_offset);
413527a677fcSJonathan Peyton #endif // defined(KMP_GOMP_COMPAT)
413627a677fcSJonathan Peyton     return retval;
413727a677fcSJonathan Peyton   }
413827a677fcSJonathan Peyton   kmp_uint64 get_ub() const {
413927a677fcSJonathan Peyton     kmp_int64 retval;
414027a677fcSJonathan Peyton #if defined(KMP_GOMP_COMPAT)
414127a677fcSJonathan Peyton     // Intel task just returns the upper bound normally
414227a677fcSJonathan Peyton     if (!taskdata->td_flags.native) {
414327a677fcSJonathan Peyton       retval = *(kmp_int64 *)((char *)task + upper_offset);
414427a677fcSJonathan Peyton     } else {
414527a677fcSJonathan Peyton       // GOMP task has to take into account the sizeof(long)
414627a677fcSJonathan Peyton       if (taskdata->td_size_loop_bounds == 4) {
414727a677fcSJonathan Peyton         kmp_int32 *ub = RCAST(kmp_int32 *, task->shareds) + 1;
414827a677fcSJonathan Peyton         retval = (kmp_int64)*ub;
414927a677fcSJonathan Peyton       } else {
415027a677fcSJonathan Peyton         kmp_int64 *ub = RCAST(kmp_int64 *, task->shareds) + 1;
415127a677fcSJonathan Peyton         retval = (kmp_int64)*ub;
415227a677fcSJonathan Peyton       }
415327a677fcSJonathan Peyton     }
415427a677fcSJonathan Peyton #else
415527a677fcSJonathan Peyton     retval = *(kmp_int64 *)((char *)task + upper_offset);
415627a677fcSJonathan Peyton #endif // defined(KMP_GOMP_COMPAT)
415727a677fcSJonathan Peyton     return retval;
415827a677fcSJonathan Peyton   }
415927a677fcSJonathan Peyton   void set_lb(kmp_uint64 lb) {
416027a677fcSJonathan Peyton #if defined(KMP_GOMP_COMPAT)
416127a677fcSJonathan Peyton     // Intel task just sets the lower bound normally
416227a677fcSJonathan Peyton     if (!taskdata->td_flags.native) {
416327a677fcSJonathan Peyton       *(kmp_uint64 *)((char *)task + lower_offset) = lb;
416427a677fcSJonathan Peyton     } else {
416527a677fcSJonathan Peyton       // GOMP task has to take into account the sizeof(long)
416627a677fcSJonathan Peyton       if (taskdata->td_size_loop_bounds == 4) {
416727a677fcSJonathan Peyton         kmp_uint32 *lower = RCAST(kmp_uint32 *, task->shareds);
416827a677fcSJonathan Peyton         *lower = (kmp_uint32)lb;
416927a677fcSJonathan Peyton       } else {
417027a677fcSJonathan Peyton         kmp_uint64 *lower = RCAST(kmp_uint64 *, task->shareds);
417127a677fcSJonathan Peyton         *lower = (kmp_uint64)lb;
417227a677fcSJonathan Peyton       }
417327a677fcSJonathan Peyton     }
417427a677fcSJonathan Peyton #else
417527a677fcSJonathan Peyton     *(kmp_uint64 *)((char *)task + lower_offset) = lb;
417627a677fcSJonathan Peyton #endif // defined(KMP_GOMP_COMPAT)
417727a677fcSJonathan Peyton   }
417827a677fcSJonathan Peyton   void set_ub(kmp_uint64 ub) {
417927a677fcSJonathan Peyton #if defined(KMP_GOMP_COMPAT)
418027a677fcSJonathan Peyton     // Intel task just sets the upper bound normally
418127a677fcSJonathan Peyton     if (!taskdata->td_flags.native) {
418227a677fcSJonathan Peyton       *(kmp_uint64 *)((char *)task + upper_offset) = ub;
418327a677fcSJonathan Peyton     } else {
418427a677fcSJonathan Peyton       // GOMP task has to take into account the sizeof(long)
418527a677fcSJonathan Peyton       if (taskdata->td_size_loop_bounds == 4) {
418627a677fcSJonathan Peyton         kmp_uint32 *upper = RCAST(kmp_uint32 *, task->shareds) + 1;
418727a677fcSJonathan Peyton         *upper = (kmp_uint32)ub;
418827a677fcSJonathan Peyton       } else {
418927a677fcSJonathan Peyton         kmp_uint64 *upper = RCAST(kmp_uint64 *, task->shareds) + 1;
419027a677fcSJonathan Peyton         *upper = (kmp_uint64)ub;
419127a677fcSJonathan Peyton       }
419227a677fcSJonathan Peyton     }
419327a677fcSJonathan Peyton #else
419427a677fcSJonathan Peyton     *(kmp_uint64 *)((char *)task + upper_offset) = ub;
419527a677fcSJonathan Peyton #endif // defined(KMP_GOMP_COMPAT)
419627a677fcSJonathan Peyton   }
419727a677fcSJonathan Peyton };
419827a677fcSJonathan Peyton 
41997cc577a4SJonathan Peyton // __kmp_taskloop_linear: Start tasks of the taskloop linearly
42007cc577a4SJonathan Peyton //
42017cc577a4SJonathan Peyton // loc        Source location information
42027cc577a4SJonathan Peyton // gtid       Global thread ID
420393e17cfeSJonathan Peyton // task       Pattern task, exposes the loop iteration range
420493e17cfeSJonathan Peyton // lb         Pointer to loop lower bound in task structure
420593e17cfeSJonathan Peyton // ub         Pointer to loop upper bound in task structure
42067cc577a4SJonathan Peyton // st         Loop stride
420793e17cfeSJonathan Peyton // ub_glob    Global upper bound (used for lastprivate check)
420893e17cfeSJonathan Peyton // num_tasks  Number of tasks to execute
420993e17cfeSJonathan Peyton // grainsize  Number of loop iterations per task
421093e17cfeSJonathan Peyton // extras     Number of chunks with grainsize+1 iterations
421193e17cfeSJonathan Peyton // tc         Iterations count
42127cc577a4SJonathan Peyton // task_dup   Tasks duplication routine
4213cdaefac5SJoachim Protze // codeptr_ra Return address for OMPT events
42143041982dSJonathan Peyton void __kmp_taskloop_linear(ident_t *loc, int gtid, kmp_task_t *task,
42157cc577a4SJonathan Peyton                            kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
421693e17cfeSJonathan Peyton                            kmp_uint64 ub_glob, kmp_uint64 num_tasks,
421793e17cfeSJonathan Peyton                            kmp_uint64 grainsize, kmp_uint64 extras,
4218cdaefac5SJoachim Protze                            kmp_uint64 tc,
4219cdaefac5SJoachim Protze #if OMPT_SUPPORT
4220cdaefac5SJoachim Protze                            void *codeptr_ra,
4221cdaefac5SJoachim Protze #endif
4222cdaefac5SJoachim Protze                            void *task_dup) {
42237cc577a4SJonathan Peyton   KMP_COUNT_BLOCK(OMP_TASKLOOP);
42247cc577a4SJonathan Peyton   KMP_TIME_PARTITIONED_BLOCK(OMP_taskloop_scheduling);
42257cc577a4SJonathan Peyton   p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
422627a677fcSJonathan Peyton   // compiler provides global bounds here
422727a677fcSJonathan Peyton   kmp_taskloop_bounds_t task_bounds(task, lb, ub);
422827a677fcSJonathan Peyton   kmp_uint64 lower = task_bounds.get_lb();
422927a677fcSJonathan Peyton   kmp_uint64 upper = task_bounds.get_ub();
423093e17cfeSJonathan Peyton   kmp_uint64 i;
42317cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
42327cc577a4SJonathan Peyton   kmp_taskdata_t *current_task = thread->th.th_current_task;
42337cc577a4SJonathan Peyton   kmp_task_t *next_task;
42347cc577a4SJonathan Peyton   kmp_int32 lastpriv = 0;
42357cc577a4SJonathan Peyton 
423693e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras);
423793e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks > extras);
423893e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks > 0);
423993e17cfeSJonathan Peyton   KA_TRACE(20, ("__kmp_taskloop_linear: T#%d: %lld tasks, grainsize %lld, "
4240d74d8902SJonathan Peyton                 "extras %lld, i=%lld,%lld(%d)%lld, dup %p\n",
4241d74d8902SJonathan Peyton                 gtid, num_tasks, grainsize, extras, lower, upper, ub_glob, st,
4242d74d8902SJonathan Peyton                 task_dup));
424393e17cfeSJonathan Peyton 
424493e17cfeSJonathan Peyton   // Launch num_tasks tasks, assign grainsize iterations each task
424593e17cfeSJonathan Peyton   for (i = 0; i < num_tasks; ++i) {
424693e17cfeSJonathan Peyton     kmp_uint64 chunk_minus_1;
424793e17cfeSJonathan Peyton     if (extras == 0) {
424893e17cfeSJonathan Peyton       chunk_minus_1 = grainsize - 1;
424993e17cfeSJonathan Peyton     } else {
425093e17cfeSJonathan Peyton       chunk_minus_1 = grainsize;
425193e17cfeSJonathan Peyton       --extras; // first extras iterations get bigger chunk (grainsize+1)
425293e17cfeSJonathan Peyton     }
425393e17cfeSJonathan Peyton     upper = lower + st * chunk_minus_1;
425493e17cfeSJonathan Peyton     if (i == num_tasks - 1) {
425593e17cfeSJonathan Peyton       // schedule the last task, set lastprivate flag if needed
425693e17cfeSJonathan Peyton       if (st == 1) { // most common case
425793e17cfeSJonathan Peyton         KMP_DEBUG_ASSERT(upper == *ub);
425893e17cfeSJonathan Peyton         if (upper == ub_glob)
425993e17cfeSJonathan Peyton           lastpriv = 1;
426093e17cfeSJonathan Peyton       } else if (st > 0) { // positive loop stride
426193e17cfeSJonathan Peyton         KMP_DEBUG_ASSERT((kmp_uint64)st > *ub - upper);
426293e17cfeSJonathan Peyton         if ((kmp_uint64)st > ub_glob - upper)
426393e17cfeSJonathan Peyton           lastpriv = 1;
426493e17cfeSJonathan Peyton       } else { // negative loop stride
426593e17cfeSJonathan Peyton         KMP_DEBUG_ASSERT(upper + st < *ub);
426693e17cfeSJonathan Peyton         if (upper - ub_glob < (kmp_uint64)(-st))
426793e17cfeSJonathan Peyton           lastpriv = 1;
426893e17cfeSJonathan Peyton       }
426993e17cfeSJonathan Peyton     }
427093e17cfeSJonathan Peyton     next_task = __kmp_task_dup_alloc(thread, task); // allocate new task
427127a677fcSJonathan Peyton     kmp_taskdata_t *next_taskdata = KMP_TASK_TO_TASKDATA(next_task);
427227a677fcSJonathan Peyton     kmp_taskloop_bounds_t next_task_bounds =
427327a677fcSJonathan Peyton         kmp_taskloop_bounds_t(next_task, task_bounds);
427427a677fcSJonathan Peyton 
427593e17cfeSJonathan Peyton     // adjust task-specific bounds
427627a677fcSJonathan Peyton     next_task_bounds.set_lb(lower);
427727a677fcSJonathan Peyton     if (next_taskdata->td_flags.native) {
427827a677fcSJonathan Peyton       next_task_bounds.set_ub(upper + (st > 0 ? 1 : -1));
427927a677fcSJonathan Peyton     } else {
428027a677fcSJonathan Peyton       next_task_bounds.set_ub(upper);
428127a677fcSJonathan Peyton     }
428293e17cfeSJonathan Peyton     if (ptask_dup != NULL) // set lastprivate flag, construct fistprivates, etc.
428393e17cfeSJonathan Peyton       ptask_dup(next_task, task, lastpriv);
428427a677fcSJonathan Peyton     KA_TRACE(40,
428527a677fcSJonathan Peyton              ("__kmp_taskloop_linear: T#%d; task #%llu: task %p: lower %lld, "
428627a677fcSJonathan Peyton               "upper %lld stride %lld, (offsets %p %p)\n",
428727a677fcSJonathan Peyton               gtid, i, next_task, lower, upper, st,
428827a677fcSJonathan Peyton               next_task_bounds.get_lower_offset(),
428927a677fcSJonathan Peyton               next_task_bounds.get_upper_offset()));
4290cdaefac5SJoachim Protze #if OMPT_SUPPORT
4291cdaefac5SJoachim Protze     __kmp_omp_taskloop_task(NULL, gtid, next_task,
4292cdaefac5SJoachim Protze                            codeptr_ra); // schedule new task
4293cdaefac5SJoachim Protze #else
429493e17cfeSJonathan Peyton     __kmp_omp_task(gtid, next_task, true); // schedule new task
4295cdaefac5SJoachim Protze #endif
429693e17cfeSJonathan Peyton     lower = upper + st; // adjust lower bound for the next iteration
429793e17cfeSJonathan Peyton   }
429893e17cfeSJonathan Peyton   // free the pattern task and exit
429993e17cfeSJonathan Peyton   __kmp_task_start(gtid, task, current_task); // make internal bookkeeping
430093e17cfeSJonathan Peyton   // do not execute the pattern task, just do internal bookkeeping
430186ed6aa6SJoachim Protze   __kmp_task_finish<false>(gtid, task, current_task);
430293e17cfeSJonathan Peyton }
430393e17cfeSJonathan Peyton 
430493e17cfeSJonathan Peyton // Structure to keep taskloop parameters for auxiliary task
430593e17cfeSJonathan Peyton // kept in the shareds of the task structure.
430693e17cfeSJonathan Peyton typedef struct __taskloop_params {
430793e17cfeSJonathan Peyton   kmp_task_t *task;
430893e17cfeSJonathan Peyton   kmp_uint64 *lb;
430993e17cfeSJonathan Peyton   kmp_uint64 *ub;
431093e17cfeSJonathan Peyton   void *task_dup;
431193e17cfeSJonathan Peyton   kmp_int64 st;
431293e17cfeSJonathan Peyton   kmp_uint64 ub_glob;
431393e17cfeSJonathan Peyton   kmp_uint64 num_tasks;
431493e17cfeSJonathan Peyton   kmp_uint64 grainsize;
431593e17cfeSJonathan Peyton   kmp_uint64 extras;
431693e17cfeSJonathan Peyton   kmp_uint64 tc;
431793e17cfeSJonathan Peyton   kmp_uint64 num_t_min;
4318cdaefac5SJoachim Protze #if OMPT_SUPPORT
4319cdaefac5SJoachim Protze   void *codeptr_ra;
4320cdaefac5SJoachim Protze #endif
432193e17cfeSJonathan Peyton } __taskloop_params_t;
432293e17cfeSJonathan Peyton 
432393e17cfeSJonathan Peyton void __kmp_taskloop_recur(ident_t *, int, kmp_task_t *, kmp_uint64 *,
432493e17cfeSJonathan Peyton                           kmp_uint64 *, kmp_int64, kmp_uint64, kmp_uint64,
432593e17cfeSJonathan Peyton                           kmp_uint64, kmp_uint64, kmp_uint64, kmp_uint64,
4326cdaefac5SJoachim Protze #if OMPT_SUPPORT
4327cdaefac5SJoachim Protze                           void *,
4328cdaefac5SJoachim Protze #endif
432993e17cfeSJonathan Peyton                           void *);
433093e17cfeSJonathan Peyton 
433193e17cfeSJonathan Peyton // Execute part of the the taskloop submitted as a task.
433293e17cfeSJonathan Peyton int __kmp_taskloop_task(int gtid, void *ptask) {
4333d74d8902SJonathan Peyton   __taskloop_params_t *p =
4334d74d8902SJonathan Peyton       (__taskloop_params_t *)((kmp_task_t *)ptask)->shareds;
433593e17cfeSJonathan Peyton   kmp_task_t *task = p->task;
433693e17cfeSJonathan Peyton   kmp_uint64 *lb = p->lb;
433793e17cfeSJonathan Peyton   kmp_uint64 *ub = p->ub;
433893e17cfeSJonathan Peyton   void *task_dup = p->task_dup;
433993e17cfeSJonathan Peyton   //  p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
434093e17cfeSJonathan Peyton   kmp_int64 st = p->st;
434193e17cfeSJonathan Peyton   kmp_uint64 ub_glob = p->ub_glob;
434293e17cfeSJonathan Peyton   kmp_uint64 num_tasks = p->num_tasks;
434393e17cfeSJonathan Peyton   kmp_uint64 grainsize = p->grainsize;
434493e17cfeSJonathan Peyton   kmp_uint64 extras = p->extras;
434593e17cfeSJonathan Peyton   kmp_uint64 tc = p->tc;
434693e17cfeSJonathan Peyton   kmp_uint64 num_t_min = p->num_t_min;
4347cdaefac5SJoachim Protze #if OMPT_SUPPORT
4348cdaefac5SJoachim Protze   void *codeptr_ra = p->codeptr_ra;
4349cdaefac5SJoachim Protze #endif
435093e17cfeSJonathan Peyton #if KMP_DEBUG
435193e17cfeSJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
435293e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(task != NULL);
435393e17cfeSJonathan Peyton   KA_TRACE(20, ("__kmp_taskloop_task: T#%d, task %p: %lld tasks, grainsize"
4354d74d8902SJonathan Peyton                 " %lld, extras %lld, i=%lld,%lld(%d), dup %p\n",
4355d74d8902SJonathan Peyton                 gtid, taskdata, num_tasks, grainsize, extras, *lb, *ub, st,
4356d74d8902SJonathan Peyton                 task_dup));
435793e17cfeSJonathan Peyton #endif
435893e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks * 2 + 1 > num_t_min);
435993e17cfeSJonathan Peyton   if (num_tasks > num_t_min)
436093e17cfeSJonathan Peyton     __kmp_taskloop_recur(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
4361cdaefac5SJoachim Protze                          grainsize, extras, tc, num_t_min,
4362cdaefac5SJoachim Protze #if OMPT_SUPPORT
4363cdaefac5SJoachim Protze                          codeptr_ra,
4364cdaefac5SJoachim Protze #endif
4365cdaefac5SJoachim Protze                          task_dup);
436693e17cfeSJonathan Peyton   else
436793e17cfeSJonathan Peyton     __kmp_taskloop_linear(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
4368cdaefac5SJoachim Protze                           grainsize, extras, tc,
4369cdaefac5SJoachim Protze #if OMPT_SUPPORT
4370cdaefac5SJoachim Protze                           codeptr_ra,
4371cdaefac5SJoachim Protze #endif
4372cdaefac5SJoachim Protze                           task_dup);
437393e17cfeSJonathan Peyton 
437493e17cfeSJonathan Peyton   KA_TRACE(40, ("__kmp_taskloop_task(exit): T#%d\n", gtid));
437593e17cfeSJonathan Peyton   return 0;
437693e17cfeSJonathan Peyton }
437793e17cfeSJonathan Peyton 
437893e17cfeSJonathan Peyton // Schedule part of the the taskloop as a task,
437993e17cfeSJonathan Peyton // execute the rest of the the taskloop.
438093e17cfeSJonathan Peyton //
438193e17cfeSJonathan Peyton // loc        Source location information
438293e17cfeSJonathan Peyton // gtid       Global thread ID
438393e17cfeSJonathan Peyton // task       Pattern task, exposes the loop iteration range
438493e17cfeSJonathan Peyton // lb         Pointer to loop lower bound in task structure
438593e17cfeSJonathan Peyton // ub         Pointer to loop upper bound in task structure
438693e17cfeSJonathan Peyton // st         Loop stride
438793e17cfeSJonathan Peyton // ub_glob    Global upper bound (used for lastprivate check)
438893e17cfeSJonathan Peyton // num_tasks  Number of tasks to execute
438993e17cfeSJonathan Peyton // grainsize  Number of loop iterations per task
439093e17cfeSJonathan Peyton // extras     Number of chunks with grainsize+1 iterations
439193e17cfeSJonathan Peyton // tc         Iterations count
439293e17cfeSJonathan Peyton // num_t_min  Threashold to launch tasks recursively
439393e17cfeSJonathan Peyton // task_dup   Tasks duplication routine
4394cdaefac5SJoachim Protze // codeptr_ra Return address for OMPT events
439593e17cfeSJonathan Peyton void __kmp_taskloop_recur(ident_t *loc, int gtid, kmp_task_t *task,
439693e17cfeSJonathan Peyton                           kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
439793e17cfeSJonathan Peyton                           kmp_uint64 ub_glob, kmp_uint64 num_tasks,
439893e17cfeSJonathan Peyton                           kmp_uint64 grainsize, kmp_uint64 extras,
4399cdaefac5SJoachim Protze                           kmp_uint64 tc, kmp_uint64 num_t_min,
4400cdaefac5SJoachim Protze #if OMPT_SUPPORT
4401cdaefac5SJoachim Protze                           void *codeptr_ra,
4402cdaefac5SJoachim Protze #endif
4403cdaefac5SJoachim Protze                           void *task_dup) {
440493e17cfeSJonathan Peyton #if KMP_DEBUG
440593e17cfeSJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
440693e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(task != NULL);
440793e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks > num_t_min);
440893e17cfeSJonathan Peyton   KA_TRACE(20, ("__kmp_taskloop_recur: T#%d, task %p: %lld tasks, grainsize"
4409d74d8902SJonathan Peyton                 " %lld, extras %lld, i=%lld,%lld(%d), dup %p\n",
4410d74d8902SJonathan Peyton                 gtid, taskdata, num_tasks, grainsize, extras, *lb, *ub, st,
4411d74d8902SJonathan Peyton                 task_dup));
441293e17cfeSJonathan Peyton #endif
441393e17cfeSJonathan Peyton   p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
441493e17cfeSJonathan Peyton   kmp_uint64 lower = *lb;
441593e17cfeSJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
441693e17cfeSJonathan Peyton   //  kmp_taskdata_t *current_task = thread->th.th_current_task;
441793e17cfeSJonathan Peyton   kmp_task_t *next_task;
441893e17cfeSJonathan Peyton   size_t lower_offset =
441993e17cfeSJonathan Peyton       (char *)lb - (char *)task; // remember offset of lb in the task structure
442093e17cfeSJonathan Peyton   size_t upper_offset =
442193e17cfeSJonathan Peyton       (char *)ub - (char *)task; // remember offset of ub in the task structure
442293e17cfeSJonathan Peyton 
442393e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras);
442493e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks > extras);
442593e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks > 0);
442693e17cfeSJonathan Peyton 
442793e17cfeSJonathan Peyton   // split the loop in two halves
442893e17cfeSJonathan Peyton   kmp_uint64 lb1, ub0, tc0, tc1, ext0, ext1;
442993e17cfeSJonathan Peyton   kmp_uint64 gr_size0 = grainsize;
443093e17cfeSJonathan Peyton   kmp_uint64 n_tsk0 = num_tasks >> 1; // num_tasks/2 to execute
443193e17cfeSJonathan Peyton   kmp_uint64 n_tsk1 = num_tasks - n_tsk0; // to schedule as a task
443293e17cfeSJonathan Peyton   if (n_tsk0 <= extras) {
443393e17cfeSJonathan Peyton     gr_size0++; // integrate extras into grainsize
443493e17cfeSJonathan Peyton     ext0 = 0; // no extra iters in 1st half
443593e17cfeSJonathan Peyton     ext1 = extras - n_tsk0; // remaining extras
443693e17cfeSJonathan Peyton     tc0 = gr_size0 * n_tsk0;
443793e17cfeSJonathan Peyton     tc1 = tc - tc0;
443893e17cfeSJonathan Peyton   } else { // n_tsk0 > extras
443993e17cfeSJonathan Peyton     ext1 = 0; // no extra iters in 2nd half
444093e17cfeSJonathan Peyton     ext0 = extras;
444193e17cfeSJonathan Peyton     tc1 = grainsize * n_tsk1;
444293e17cfeSJonathan Peyton     tc0 = tc - tc1;
444393e17cfeSJonathan Peyton   }
444493e17cfeSJonathan Peyton   ub0 = lower + st * (tc0 - 1);
444593e17cfeSJonathan Peyton   lb1 = ub0 + st;
444693e17cfeSJonathan Peyton 
444793e17cfeSJonathan Peyton   // create pattern task for 2nd half of the loop
444893e17cfeSJonathan Peyton   next_task = __kmp_task_dup_alloc(thread, task); // duplicate the task
444993e17cfeSJonathan Peyton   // adjust lower bound (upper bound is not changed) for the 2nd half
445093e17cfeSJonathan Peyton   *(kmp_uint64 *)((char *)next_task + lower_offset) = lb1;
445193e17cfeSJonathan Peyton   if (ptask_dup != NULL) // construct fistprivates, etc.
445293e17cfeSJonathan Peyton     ptask_dup(next_task, task, 0);
445393e17cfeSJonathan Peyton   *ub = ub0; // adjust upper bound for the 1st half
445493e17cfeSJonathan Peyton 
445593e17cfeSJonathan Peyton   // create auxiliary task for 2nd half of the loop
445693e17cfeSJonathan Peyton   kmp_task_t *new_task =
445793e17cfeSJonathan Peyton       __kmpc_omp_task_alloc(loc, gtid, 1, 3 * sizeof(void *),
445893e17cfeSJonathan Peyton                             sizeof(__taskloop_params_t), &__kmp_taskloop_task);
445993e17cfeSJonathan Peyton   __taskloop_params_t *p = (__taskloop_params_t *)new_task->shareds;
446093e17cfeSJonathan Peyton   p->task = next_task;
446193e17cfeSJonathan Peyton   p->lb = (kmp_uint64 *)((char *)next_task + lower_offset);
446293e17cfeSJonathan Peyton   p->ub = (kmp_uint64 *)((char *)next_task + upper_offset);
446393e17cfeSJonathan Peyton   p->task_dup = task_dup;
446493e17cfeSJonathan Peyton   p->st = st;
446593e17cfeSJonathan Peyton   p->ub_glob = ub_glob;
446693e17cfeSJonathan Peyton   p->num_tasks = n_tsk1;
446793e17cfeSJonathan Peyton   p->grainsize = grainsize;
446893e17cfeSJonathan Peyton   p->extras = ext1;
446993e17cfeSJonathan Peyton   p->tc = tc1;
447093e17cfeSJonathan Peyton   p->num_t_min = num_t_min;
4471cdaefac5SJoachim Protze #if OMPT_SUPPORT
4472cdaefac5SJoachim Protze   p->codeptr_ra = codeptr_ra;
4473cdaefac5SJoachim Protze #endif
4474cdaefac5SJoachim Protze 
4475cdaefac5SJoachim Protze #if OMPT_SUPPORT
4476cdaefac5SJoachim Protze   // schedule new task with correct return address for OMPT events
4477cdaefac5SJoachim Protze   __kmp_omp_taskloop_task(NULL, gtid, new_task, codeptr_ra);
4478cdaefac5SJoachim Protze #else
4479f729df82SGheorghe-Teodor Bercea   __kmp_omp_task(gtid, new_task, true); // schedule new task
4480cdaefac5SJoachim Protze #endif
448193e17cfeSJonathan Peyton 
448293e17cfeSJonathan Peyton   // execute the 1st half of current subrange
448393e17cfeSJonathan Peyton   if (n_tsk0 > num_t_min)
4484d74d8902SJonathan Peyton     __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0, gr_size0,
4485cdaefac5SJoachim Protze                          ext0, tc0, num_t_min,
4486cdaefac5SJoachim Protze #if OMPT_SUPPORT
4487cdaefac5SJoachim Protze                          codeptr_ra,
4488cdaefac5SJoachim Protze #endif
4489cdaefac5SJoachim Protze                          task_dup);
449093e17cfeSJonathan Peyton   else
449193e17cfeSJonathan Peyton     __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0,
4492cdaefac5SJoachim Protze                           gr_size0, ext0, tc0,
4493cdaefac5SJoachim Protze #if OMPT_SUPPORT
4494cdaefac5SJoachim Protze                           codeptr_ra,
4495cdaefac5SJoachim Protze #endif
4496cdaefac5SJoachim Protze                           task_dup);
449793e17cfeSJonathan Peyton 
449893e17cfeSJonathan Peyton   KA_TRACE(40, ("__kmpc_taskloop_recur(exit): T#%d\n", gtid));
449993e17cfeSJonathan Peyton }
450093e17cfeSJonathan Peyton 
450193e17cfeSJonathan Peyton /*!
450293e17cfeSJonathan Peyton @ingroup TASKING
450393e17cfeSJonathan Peyton @param loc       Source location information
450493e17cfeSJonathan Peyton @param gtid      Global thread ID
450593e17cfeSJonathan Peyton @param task      Task structure
450693e17cfeSJonathan Peyton @param if_val    Value of the if clause
450793e17cfeSJonathan Peyton @param lb        Pointer to loop lower bound in task structure
450893e17cfeSJonathan Peyton @param ub        Pointer to loop upper bound in task structure
450993e17cfeSJonathan Peyton @param st        Loop stride
4510b7a8ab34SAndrey Churbanov @param nogroup   Flag, 1 if no taskgroup needs to be added, 0 otherwise
451193e17cfeSJonathan Peyton @param sched     Schedule specified 0/1/2 for none/grainsize/num_tasks
451293e17cfeSJonathan Peyton @param grainsize Schedule value if specified
451393e17cfeSJonathan Peyton @param task_dup  Tasks duplication routine
451493e17cfeSJonathan Peyton 
451593e17cfeSJonathan Peyton Execute the taskloop construct.
451693e17cfeSJonathan Peyton */
451793e17cfeSJonathan Peyton void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int if_val,
451893e17cfeSJonathan Peyton                      kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup,
451993e17cfeSJonathan Peyton                      int sched, kmp_uint64 grainsize, void *task_dup) {
452093e17cfeSJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
452193e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(task != NULL);
452293e17cfeSJonathan Peyton 
452382e94a59SJoachim Protze   if (nogroup == 0) {
452482e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
452582e94a59SJoachim Protze     OMPT_STORE_RETURN_ADDRESS(gtid);
452682e94a59SJoachim Protze #endif
452793e17cfeSJonathan Peyton     __kmpc_taskgroup(loc, gtid);
452882e94a59SJoachim Protze   }
452993e17cfeSJonathan Peyton 
453093e17cfeSJonathan Peyton   // =========================================================================
453193e17cfeSJonathan Peyton   // calculate loop parameters
453227a677fcSJonathan Peyton   kmp_taskloop_bounds_t task_bounds(task, lb, ub);
453393e17cfeSJonathan Peyton   kmp_uint64 tc;
453427a677fcSJonathan Peyton   // compiler provides global bounds here
453527a677fcSJonathan Peyton   kmp_uint64 lower = task_bounds.get_lb();
453627a677fcSJonathan Peyton   kmp_uint64 upper = task_bounds.get_ub();
453793e17cfeSJonathan Peyton   kmp_uint64 ub_glob = upper; // global upper used to calc lastprivate flag
453893e17cfeSJonathan Peyton   kmp_uint64 num_tasks = 0, extras = 0;
453993e17cfeSJonathan Peyton   kmp_uint64 num_tasks_min = __kmp_taskloop_min_tasks;
454093e17cfeSJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
454193e17cfeSJonathan Peyton   kmp_taskdata_t *current_task = thread->th.th_current_task;
454293e17cfeSJonathan Peyton 
454327a677fcSJonathan Peyton   KA_TRACE(20, ("__kmpc_taskloop: T#%d, task %p, lb %lld, ub %lld, st %lld, "
454427a677fcSJonathan Peyton                 "grain %llu(%d), dup %p\n",
454527a677fcSJonathan Peyton                 gtid, taskdata, lower, upper, st, grainsize, sched, task_dup));
454627a677fcSJonathan Peyton 
45477cc577a4SJonathan Peyton   // compute trip count
45487cc577a4SJonathan Peyton   if (st == 1) { // most common case
45497cc577a4SJonathan Peyton     tc = upper - lower + 1;
45507cc577a4SJonathan Peyton   } else if (st < 0) {
45517cc577a4SJonathan Peyton     tc = (lower - upper) / (-st) + 1;
45527cc577a4SJonathan Peyton   } else { // st > 0
45537cc577a4SJonathan Peyton     tc = (upper - lower) / st + 1;
45547cc577a4SJonathan Peyton   }
45557cc577a4SJonathan Peyton   if (tc == 0) {
45567cc577a4SJonathan Peyton     KA_TRACE(20, ("__kmpc_taskloop(exit): T#%d zero-trip loop\n", gtid));
45577cc577a4SJonathan Peyton     // free the pattern task and exit
45587cc577a4SJonathan Peyton     __kmp_task_start(gtid, task, current_task);
45597cc577a4SJonathan Peyton     // do not execute anything for zero-trip loop
456086ed6aa6SJoachim Protze     __kmp_task_finish<false>(gtid, task, current_task);
45617cc577a4SJonathan Peyton     return;
45627cc577a4SJonathan Peyton   }
4563cdaefac5SJoachim Protze 
4564cdaefac5SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
4565cdaefac5SJoachim Protze   ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
4566cdaefac5SJoachim Protze   ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
4567cdaefac5SJoachim Protze   if (ompt_enabled.ompt_callback_work) {
4568cdaefac5SJoachim Protze     ompt_callbacks.ompt_callback(ompt_callback_work)(
4569cdaefac5SJoachim Protze         ompt_work_taskloop, ompt_scope_begin, &(team_info->parallel_data),
4570cdaefac5SJoachim Protze         &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
4571cdaefac5SJoachim Protze   }
4572cdaefac5SJoachim Protze #endif
4573cdaefac5SJoachim Protze 
457493e17cfeSJonathan Peyton   if (num_tasks_min == 0)
457593e17cfeSJonathan Peyton     // TODO: can we choose better default heuristic?
4576d74d8902SJonathan Peyton     num_tasks_min =
4577d74d8902SJonathan Peyton         KMP_MIN(thread->th.th_team_nproc * 10, INITIAL_TASK_DEQUE_SIZE);
45787cc577a4SJonathan Peyton 
45797cc577a4SJonathan Peyton   // compute num_tasks/grainsize based on the input provided
45807cc577a4SJonathan Peyton   switch (sched) {
45817cc577a4SJonathan Peyton   case 0: // no schedule clause specified, we can choose the default
45827cc577a4SJonathan Peyton     // let's try to schedule (team_size*10) tasks
45837cc577a4SJonathan Peyton     grainsize = thread->th.th_team_nproc * 10;
45840c599c38SJoachim Protze     KMP_FALLTHROUGH();
45857cc577a4SJonathan Peyton   case 2: // num_tasks provided
45867cc577a4SJonathan Peyton     if (grainsize > tc) {
45877cc577a4SJonathan Peyton       num_tasks = tc; // too big num_tasks requested, adjust values
45887cc577a4SJonathan Peyton       grainsize = 1;
45897cc577a4SJonathan Peyton       extras = 0;
45907cc577a4SJonathan Peyton     } else {
45917cc577a4SJonathan Peyton       num_tasks = grainsize;
45927cc577a4SJonathan Peyton       grainsize = tc / num_tasks;
45937cc577a4SJonathan Peyton       extras = tc % num_tasks;
45947cc577a4SJonathan Peyton     }
45957cc577a4SJonathan Peyton     break;
45967cc577a4SJonathan Peyton   case 1: // grainsize provided
45977cc577a4SJonathan Peyton     if (grainsize > tc) {
45987cc577a4SJonathan Peyton       num_tasks = 1; // too big grainsize requested, adjust values
45997cc577a4SJonathan Peyton       grainsize = tc;
46007cc577a4SJonathan Peyton       extras = 0;
46017cc577a4SJonathan Peyton     } else {
46027cc577a4SJonathan Peyton       num_tasks = tc / grainsize;
460393e17cfeSJonathan Peyton       // adjust grainsize for balanced distribution of iterations
460493e17cfeSJonathan Peyton       grainsize = tc / num_tasks;
46057cc577a4SJonathan Peyton       extras = tc % num_tasks;
46067cc577a4SJonathan Peyton     }
46077cc577a4SJonathan Peyton     break;
46087cc577a4SJonathan Peyton   default:
46097cc577a4SJonathan Peyton     KMP_ASSERT2(0, "unknown scheduling of taskloop");
46107cc577a4SJonathan Peyton   }
46117cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras);
46127cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks > extras);
46137cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks > 0);
461493e17cfeSJonathan Peyton   // =========================================================================
46157cc577a4SJonathan Peyton 
46167cc577a4SJonathan Peyton   // check if clause value first
461727a677fcSJonathan Peyton   // Also require GOMP_taskloop to reduce to linear (taskdata->td_flags.native)
46187cc577a4SJonathan Peyton   if (if_val == 0) { // if(0) specified, mark task as serial
46197cc577a4SJonathan Peyton     taskdata->td_flags.task_serial = 1;
46207cc577a4SJonathan Peyton     taskdata->td_flags.tiedness = TASK_TIED; // AC: serial task cannot be untied
462193e17cfeSJonathan Peyton     // always start serial tasks linearly
462293e17cfeSJonathan Peyton     __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4623cdaefac5SJoachim Protze                           grainsize, extras, tc,
4624cdaefac5SJoachim Protze #if OMPT_SUPPORT
4625cdaefac5SJoachim Protze                           OMPT_GET_RETURN_ADDRESS(0),
4626cdaefac5SJoachim Protze #endif
4627cdaefac5SJoachim Protze                           task_dup);
462827a677fcSJonathan Peyton     // !taskdata->td_flags.native => currently force linear spawning of tasks
462927a677fcSJonathan Peyton     // for GOMP_taskloop
463027a677fcSJonathan Peyton   } else if (num_tasks > num_tasks_min && !taskdata->td_flags.native) {
463193e17cfeSJonathan Peyton     KA_TRACE(20, ("__kmpc_taskloop: T#%d, go recursive: tc %llu, #tasks %llu"
4632d74d8902SJonathan Peyton                   "(%lld), grain %llu, extras %llu\n",
4633d74d8902SJonathan Peyton                   gtid, tc, num_tasks, num_tasks_min, grainsize, extras));
463493e17cfeSJonathan Peyton     __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4635cdaefac5SJoachim Protze                          grainsize, extras, tc, num_tasks_min,
4636cdaefac5SJoachim Protze #if OMPT_SUPPORT
4637cdaefac5SJoachim Protze                          OMPT_GET_RETURN_ADDRESS(0),
4638cdaefac5SJoachim Protze #endif
4639cdaefac5SJoachim Protze                          task_dup);
464093e17cfeSJonathan Peyton   } else {
464193e17cfeSJonathan Peyton     KA_TRACE(20, ("__kmpc_taskloop: T#%d, go linear: tc %llu, #tasks %llu"
4642d74d8902SJonathan Peyton                   "(%lld), grain %llu, extras %llu\n",
4643d74d8902SJonathan Peyton                   gtid, tc, num_tasks, num_tasks_min, grainsize, extras));
464493e17cfeSJonathan Peyton     __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
4645cdaefac5SJoachim Protze                           grainsize, extras, tc,
4646cdaefac5SJoachim Protze #if OMPT_SUPPORT
4647cdaefac5SJoachim Protze                           OMPT_GET_RETURN_ADDRESS(0),
4648cdaefac5SJoachim Protze #endif
4649cdaefac5SJoachim Protze                           task_dup);
46507cc577a4SJonathan Peyton   }
46517cc577a4SJonathan Peyton 
4652cdaefac5SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
4653cdaefac5SJoachim Protze   if (ompt_enabled.ompt_callback_work) {
4654cdaefac5SJoachim Protze     ompt_callbacks.ompt_callback(ompt_callback_work)(
4655cdaefac5SJoachim Protze         ompt_work_taskloop, ompt_scope_end, &(team_info->parallel_data),
4656cdaefac5SJoachim Protze         &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0));
4657cdaefac5SJoachim Protze   }
4658cdaefac5SJoachim Protze #endif
4659cdaefac5SJoachim Protze 
466082e94a59SJoachim Protze   if (nogroup == 0) {
466182e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL
466282e94a59SJoachim Protze     OMPT_STORE_RETURN_ADDRESS(gtid);
466382e94a59SJoachim Protze #endif
46647cc577a4SJonathan Peyton     __kmpc_end_taskgroup(loc, gtid);
466582e94a59SJoachim Protze   }
466693e17cfeSJonathan Peyton   KA_TRACE(20, ("__kmpc_taskloop(exit): T#%d\n", gtid));
46677cc577a4SJonathan Peyton }
46687cc577a4SJonathan Peyton 
46697cc577a4SJonathan Peyton #endif
4670