17cc577a4SJonathan Peyton /*
2de4749b7SJonathan Peyton  * kmp_tasking.cpp -- OpenMP 3.0 tasking support.
37cc577a4SJonathan Peyton  */
47cc577a4SJonathan Peyton 
57cc577a4SJonathan Peyton 
67cc577a4SJonathan Peyton //===----------------------------------------------------------------------===//
77cc577a4SJonathan Peyton //
87cc577a4SJonathan Peyton //                     The LLVM Compiler Infrastructure
97cc577a4SJonathan Peyton //
107cc577a4SJonathan Peyton // This file is dual licensed under the MIT and the University of Illinois Open
117cc577a4SJonathan Peyton // Source Licenses. See LICENSE.txt for details.
127cc577a4SJonathan Peyton //
137cc577a4SJonathan Peyton //===----------------------------------------------------------------------===//
147cc577a4SJonathan Peyton 
157cc577a4SJonathan Peyton 
167cc577a4SJonathan Peyton #include "kmp.h"
177cc577a4SJonathan Peyton #include "kmp_i18n.h"
187cc577a4SJonathan Peyton #include "kmp_itt.h"
197cc577a4SJonathan Peyton #include "kmp_stats.h"
203041982dSJonathan Peyton #include "kmp_wait_release.h"
217cc577a4SJonathan Peyton 
227cc577a4SJonathan Peyton #if OMPT_SUPPORT
237cc577a4SJonathan Peyton #include "ompt-specific.h"
247cc577a4SJonathan Peyton #endif
257cc577a4SJonathan Peyton 
267cc577a4SJonathan Peyton #include "tsan_annotations.h"
277cc577a4SJonathan Peyton 
287cc577a4SJonathan Peyton /* forward declaration */
293041982dSJonathan Peyton static void __kmp_enable_tasking(kmp_task_team_t *task_team,
303041982dSJonathan Peyton                                  kmp_info_t *this_thr);
313041982dSJonathan Peyton static void __kmp_alloc_task_deque(kmp_info_t *thread,
323041982dSJonathan Peyton                                    kmp_thread_data_t *thread_data);
333041982dSJonathan Peyton static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
343041982dSJonathan Peyton                                            kmp_task_team_t *task_team);
357cc577a4SJonathan Peyton 
367cc577a4SJonathan Peyton #ifdef OMP_45_ENABLED
377cc577a4SJonathan Peyton static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask);
387cc577a4SJonathan Peyton #endif
397cc577a4SJonathan Peyton 
407cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK
417cc577a4SJonathan Peyton 
427cc577a4SJonathan Peyton //  __kmp_trace_task_stack: print the tied tasks from the task stack in order
437cc577a4SJonathan Peyton //  from top do bottom
447cc577a4SJonathan Peyton //
457cc577a4SJonathan Peyton //  gtid: global thread identifier for thread containing stack
467cc577a4SJonathan Peyton //  thread_data: thread data for task team thread containing stack
477cc577a4SJonathan Peyton //  threshold: value above which the trace statement triggers
487cc577a4SJonathan Peyton //  location: string identifying call site of this function (for trace)
493041982dSJonathan Peyton static void __kmp_trace_task_stack(kmp_int32 gtid,
503041982dSJonathan Peyton                                    kmp_thread_data_t *thread_data,
513041982dSJonathan Peyton                                    int threshold, char *location) {
527cc577a4SJonathan Peyton   kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
537cc577a4SJonathan Peyton   kmp_taskdata_t **stack_top = task_stack->ts_top;
547cc577a4SJonathan Peyton   kmp_int32 entries = task_stack->ts_entries;
557cc577a4SJonathan Peyton   kmp_taskdata_t *tied_task;
567cc577a4SJonathan Peyton 
573041982dSJonathan Peyton   KA_TRACE(
583041982dSJonathan Peyton       threshold,
593041982dSJonathan Peyton       ("__kmp_trace_task_stack(start): location = %s, gtid = %d, entries = %d, "
607cc577a4SJonathan Peyton        "first_block = %p, stack_top = %p \n",
617cc577a4SJonathan Peyton        location, gtid, entries, task_stack->ts_first_block, stack_top));
627cc577a4SJonathan Peyton 
637cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(stack_top != NULL);
647cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(entries > 0);
657cc577a4SJonathan Peyton 
663041982dSJonathan Peyton   while (entries != 0) {
677cc577a4SJonathan Peyton     KMP_DEBUG_ASSERT(stack_top != &task_stack->ts_first_block.sb_block[0]);
687cc577a4SJonathan Peyton     // fix up ts_top if we need to pop from previous block
693041982dSJonathan Peyton     if (entries & TASK_STACK_INDEX_MASK == 0) {
707cc577a4SJonathan Peyton       kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(stack_top);
717cc577a4SJonathan Peyton 
727cc577a4SJonathan Peyton       stack_block = stack_block->sb_prev;
737cc577a4SJonathan Peyton       stack_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE];
747cc577a4SJonathan Peyton     }
757cc577a4SJonathan Peyton 
767cc577a4SJonathan Peyton     // finish bookkeeping
777cc577a4SJonathan Peyton     stack_top--;
787cc577a4SJonathan Peyton     entries--;
797cc577a4SJonathan Peyton 
807cc577a4SJonathan Peyton     tied_task = *stack_top;
817cc577a4SJonathan Peyton 
827cc577a4SJonathan Peyton     KMP_DEBUG_ASSERT(tied_task != NULL);
837cc577a4SJonathan Peyton     KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
847cc577a4SJonathan Peyton 
853041982dSJonathan Peyton     KA_TRACE(threshold,
863041982dSJonathan Peyton              ("__kmp_trace_task_stack(%s):             gtid=%d, entry=%d, "
877cc577a4SJonathan Peyton               "stack_top=%p, tied_task=%p\n",
887cc577a4SJonathan Peyton               location, gtid, entries, stack_top, tied_task));
897cc577a4SJonathan Peyton   }
907cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(stack_top == &task_stack->ts_first_block.sb_block[0]);
917cc577a4SJonathan Peyton 
923041982dSJonathan Peyton   KA_TRACE(threshold,
933041982dSJonathan Peyton            ("__kmp_trace_task_stack(exit): location = %s, gtid = %d\n",
947cc577a4SJonathan Peyton             location, gtid));
957cc577a4SJonathan Peyton }
967cc577a4SJonathan Peyton 
977cc577a4SJonathan Peyton //  __kmp_init_task_stack: initialize the task stack for the first time
987cc577a4SJonathan Peyton //  after a thread_data structure is created.
997cc577a4SJonathan Peyton //  It should not be necessary to do this again (assuming the stack works).
1007cc577a4SJonathan Peyton //
1017cc577a4SJonathan Peyton //  gtid: global thread identifier of calling thread
1027cc577a4SJonathan Peyton //  thread_data: thread data for task team thread containing stack
1033041982dSJonathan Peyton static void __kmp_init_task_stack(kmp_int32 gtid,
1043041982dSJonathan Peyton                                   kmp_thread_data_t *thread_data) {
1057cc577a4SJonathan Peyton   kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
1067cc577a4SJonathan Peyton   kmp_stack_block_t *first_block;
1077cc577a4SJonathan Peyton 
1087cc577a4SJonathan Peyton   // set up the first block of the stack
1097cc577a4SJonathan Peyton   first_block = &task_stack->ts_first_block;
1107cc577a4SJonathan Peyton   task_stack->ts_top = (kmp_taskdata_t **)first_block;
1113041982dSJonathan Peyton   memset((void *)first_block, '\0',
1123041982dSJonathan Peyton          TASK_STACK_BLOCK_SIZE * sizeof(kmp_taskdata_t *));
1137cc577a4SJonathan Peyton 
1147cc577a4SJonathan Peyton   // initialize the stack to be empty
1157cc577a4SJonathan Peyton   task_stack->ts_entries = TASK_STACK_EMPTY;
1167cc577a4SJonathan Peyton   first_block->sb_next = NULL;
1177cc577a4SJonathan Peyton   first_block->sb_prev = NULL;
1187cc577a4SJonathan Peyton }
1197cc577a4SJonathan Peyton 
1207cc577a4SJonathan Peyton //  __kmp_free_task_stack: free the task stack when thread_data is destroyed.
1217cc577a4SJonathan Peyton //
1227cc577a4SJonathan Peyton //  gtid: global thread identifier for calling thread
1237cc577a4SJonathan Peyton //  thread_data: thread info for thread containing stack
1243041982dSJonathan Peyton static void __kmp_free_task_stack(kmp_int32 gtid,
1253041982dSJonathan Peyton                                   kmp_thread_data_t *thread_data) {
1267cc577a4SJonathan Peyton   kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
1277cc577a4SJonathan Peyton   kmp_stack_block_t *stack_block = &task_stack->ts_first_block;
1287cc577a4SJonathan Peyton 
1297cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task_stack->ts_entries == TASK_STACK_EMPTY);
1307cc577a4SJonathan Peyton   // free from the second block of the stack
1317cc577a4SJonathan Peyton   while (stack_block != NULL) {
1327cc577a4SJonathan Peyton     kmp_stack_block_t *next_block = (stack_block) ? stack_block->sb_next : NULL;
1337cc577a4SJonathan Peyton 
1347cc577a4SJonathan Peyton     stack_block->sb_next = NULL;
1357cc577a4SJonathan Peyton     stack_block->sb_prev = NULL;
1367cc577a4SJonathan Peyton     if (stack_block != &task_stack->ts_first_block) {
1373041982dSJonathan Peyton       __kmp_thread_free(thread,
1383041982dSJonathan Peyton                         stack_block); // free the block, if not the first
1397cc577a4SJonathan Peyton     }
1407cc577a4SJonathan Peyton     stack_block = next_block;
1417cc577a4SJonathan Peyton   }
1427cc577a4SJonathan Peyton   // initialize the stack to be empty
1437cc577a4SJonathan Peyton   task_stack->ts_entries = 0;
1447cc577a4SJonathan Peyton   task_stack->ts_top = NULL;
1457cc577a4SJonathan Peyton }
1467cc577a4SJonathan Peyton 
1477cc577a4SJonathan Peyton //  __kmp_push_task_stack: Push the tied task onto the task stack.
1487cc577a4SJonathan Peyton //     Grow the stack if necessary by allocating another block.
1497cc577a4SJonathan Peyton //
1507cc577a4SJonathan Peyton //  gtid: global thread identifier for calling thread
1517cc577a4SJonathan Peyton //  thread: thread info for thread containing stack
1527cc577a4SJonathan Peyton //  tied_task: the task to push on the stack
1533041982dSJonathan Peyton static void __kmp_push_task_stack(kmp_int32 gtid, kmp_info_t *thread,
1543041982dSJonathan Peyton                                   kmp_taskdata_t *tied_task) {
1557cc577a4SJonathan Peyton   // GEH - need to consider what to do if tt_threads_data not allocated yet
1563041982dSJonathan Peyton   kmp_thread_data_t *thread_data =
1573041982dSJonathan Peyton       &thread->th.th_task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
1587cc577a4SJonathan Peyton   kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
1597cc577a4SJonathan Peyton 
1607cc577a4SJonathan Peyton   if (tied_task->td_flags.team_serial || tied_task->td_flags.tasking_ser) {
1617cc577a4SJonathan Peyton     return; // Don't push anything on stack if team or team tasks are serialized
1627cc577a4SJonathan Peyton   }
1637cc577a4SJonathan Peyton 
1647cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
1657cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task_stack->ts_top != NULL);
1667cc577a4SJonathan Peyton 
1673041982dSJonathan Peyton   KA_TRACE(20,
1683041982dSJonathan Peyton            ("__kmp_push_task_stack(enter): GTID: %d; THREAD: %p; TASK: %p\n",
1697cc577a4SJonathan Peyton             gtid, thread, tied_task));
1707cc577a4SJonathan Peyton   // Store entry
1717cc577a4SJonathan Peyton   *(task_stack->ts_top) = tied_task;
1727cc577a4SJonathan Peyton 
1737cc577a4SJonathan Peyton   // Do bookkeeping for next push
1747cc577a4SJonathan Peyton   task_stack->ts_top++;
1757cc577a4SJonathan Peyton   task_stack->ts_entries++;
1767cc577a4SJonathan Peyton 
1773041982dSJonathan Peyton   if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) {
1787cc577a4SJonathan Peyton     // Find beginning of this task block
1797cc577a4SJonathan Peyton     kmp_stack_block_t *stack_block =
1807cc577a4SJonathan Peyton         (kmp_stack_block_t *)(task_stack->ts_top - TASK_STACK_BLOCK_SIZE);
1817cc577a4SJonathan Peyton 
1827cc577a4SJonathan Peyton     // Check if we already have a block
1833041982dSJonathan Peyton     if (stack_block->sb_next !=
1843041982dSJonathan Peyton         NULL) { // reset ts_top to beginning of next block
1857cc577a4SJonathan Peyton       task_stack->ts_top = &stack_block->sb_next->sb_block[0];
1863041982dSJonathan Peyton     } else { // Alloc new block and link it up
1873041982dSJonathan Peyton       kmp_stack_block_t *new_block = (kmp_stack_block_t *)__kmp_thread_calloc(
1883041982dSJonathan Peyton           thread, sizeof(kmp_stack_block_t));
1897cc577a4SJonathan Peyton 
1907cc577a4SJonathan Peyton       task_stack->ts_top = &new_block->sb_block[0];
1917cc577a4SJonathan Peyton       stack_block->sb_next = new_block;
1927cc577a4SJonathan Peyton       new_block->sb_prev = stack_block;
1937cc577a4SJonathan Peyton       new_block->sb_next = NULL;
1947cc577a4SJonathan Peyton 
1953041982dSJonathan Peyton       KA_TRACE(
1963041982dSJonathan Peyton           30,
1973041982dSJonathan Peyton           ("__kmp_push_task_stack(): GTID: %d; TASK: %p; Alloc new block: %p\n",
1987cc577a4SJonathan Peyton            gtid, tied_task, new_block));
1997cc577a4SJonathan Peyton     }
2007cc577a4SJonathan Peyton   }
2013041982dSJonathan Peyton   KA_TRACE(20, ("__kmp_push_task_stack(exit): GTID: %d; TASK: %p\n", gtid,
2023041982dSJonathan Peyton                 tied_task));
2037cc577a4SJonathan Peyton }
2047cc577a4SJonathan Peyton 
2057cc577a4SJonathan Peyton //  __kmp_pop_task_stack: Pop the tied task from the task stack.  Don't return
2067cc577a4SJonathan Peyton //  the task, just check to make sure it matches the ending task passed in.
2077cc577a4SJonathan Peyton //
2087cc577a4SJonathan Peyton //  gtid: global thread identifier for the calling thread
2097cc577a4SJonathan Peyton //  thread: thread info structure containing stack
2107cc577a4SJonathan Peyton //  tied_task: the task popped off the stack
2117cc577a4SJonathan Peyton //  ending_task: the task that is ending (should match popped task)
2123041982dSJonathan Peyton static void __kmp_pop_task_stack(kmp_int32 gtid, kmp_info_t *thread,
2133041982dSJonathan Peyton                                  kmp_taskdata_t *ending_task) {
2147cc577a4SJonathan Peyton   // GEH - need to consider what to do if tt_threads_data not allocated yet
2153041982dSJonathan Peyton   kmp_thread_data_t *thread_data =
2163041982dSJonathan Peyton       &thread->th.th_task_team->tt_threads_data[__kmp_tid_from_gtid(gtid)];
2177cc577a4SJonathan Peyton   kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks;
2187cc577a4SJonathan Peyton   kmp_taskdata_t *tied_task;
2197cc577a4SJonathan Peyton 
2207cc577a4SJonathan Peyton   if (ending_task->td_flags.team_serial || ending_task->td_flags.tasking_ser) {
2213041982dSJonathan Peyton     // Don't pop anything from stack if team or team tasks are serialized
2223041982dSJonathan Peyton     return;
2237cc577a4SJonathan Peyton   }
2247cc577a4SJonathan Peyton 
2257cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task_stack->ts_top != NULL);
2267cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task_stack->ts_entries > 0);
2277cc577a4SJonathan Peyton 
2283041982dSJonathan Peyton   KA_TRACE(20, ("__kmp_pop_task_stack(enter): GTID: %d; THREAD: %p\n", gtid,
2293041982dSJonathan Peyton                 thread));
2307cc577a4SJonathan Peyton 
2317cc577a4SJonathan Peyton   // fix up ts_top if we need to pop from previous block
2323041982dSJonathan Peyton   if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) {
2333041982dSJonathan Peyton     kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(task_stack->ts_top);
2347cc577a4SJonathan Peyton 
2357cc577a4SJonathan Peyton     stack_block = stack_block->sb_prev;
2367cc577a4SJonathan Peyton     task_stack->ts_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE];
2377cc577a4SJonathan Peyton   }
2387cc577a4SJonathan Peyton 
2397cc577a4SJonathan Peyton   // finish bookkeeping
2407cc577a4SJonathan Peyton   task_stack->ts_top--;
2417cc577a4SJonathan Peyton   task_stack->ts_entries--;
2427cc577a4SJonathan Peyton 
2437cc577a4SJonathan Peyton   tied_task = *(task_stack->ts_top);
2447cc577a4SJonathan Peyton 
2457cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(tied_task != NULL);
2467cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED);
2477cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(tied_task == ending_task); // If we built the stack correctly
2487cc577a4SJonathan Peyton 
2493041982dSJonathan Peyton   KA_TRACE(20, ("__kmp_pop_task_stack(exit): GTID: %d; TASK: %p\n", gtid,
2503041982dSJonathan Peyton                 tied_task));
2517cc577a4SJonathan Peyton   return;
2527cc577a4SJonathan Peyton }
2537cc577a4SJonathan Peyton #endif /* BUILD_TIED_TASK_STACK */
2547cc577a4SJonathan Peyton 
2557cc577a4SJonathan Peyton //  __kmp_push_task: Add a task to the thread's deque
2563041982dSJonathan Peyton static kmp_int32 __kmp_push_task(kmp_int32 gtid, kmp_task_t *task) {
2577cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
2587cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
2597cc577a4SJonathan Peyton   kmp_task_team_t *task_team = thread->th.th_task_team;
2607cc577a4SJonathan Peyton   kmp_int32 tid = __kmp_tid_from_gtid(gtid);
2617cc577a4SJonathan Peyton   kmp_thread_data_t *thread_data;
2627cc577a4SJonathan Peyton 
2633041982dSJonathan Peyton   KA_TRACE(20,
2643041982dSJonathan Peyton            ("__kmp_push_task: T#%d trying to push task %p.\n", gtid, taskdata));
2657cc577a4SJonathan Peyton 
2667cc577a4SJonathan Peyton   if (taskdata->td_flags.tiedness == TASK_UNTIED) {
2673041982dSJonathan Peyton     // untied task needs to increment counter so that the task structure is not
2683041982dSJonathan Peyton     // freed prematurely
2697cc577a4SJonathan Peyton     kmp_int32 counter = 1 + KMP_TEST_THEN_INC32(&taskdata->td_untied_count);
2703041982dSJonathan Peyton     KA_TRACE(
2713041982dSJonathan Peyton         20,
2723041982dSJonathan Peyton         ("__kmp_push_task: T#%d untied_count (%d) incremented for task %p\n",
2737cc577a4SJonathan Peyton          gtid, counter, taskdata));
2747cc577a4SJonathan Peyton   }
2757cc577a4SJonathan Peyton 
2767cc577a4SJonathan Peyton   // The first check avoids building task_team thread data if serialized
2777cc577a4SJonathan Peyton   if (taskdata->td_flags.task_serial) {
2783041982dSJonathan Peyton     KA_TRACE(20, ("__kmp_push_task: T#%d team serialized; returning "
2793041982dSJonathan Peyton                   "TASK_NOT_PUSHED for task %p\n",
2807cc577a4SJonathan Peyton                   gtid, taskdata));
2817cc577a4SJonathan Peyton     return TASK_NOT_PUSHED;
2827cc577a4SJonathan Peyton   }
2837cc577a4SJonathan Peyton 
2843041982dSJonathan Peyton   // Now that serialized tasks have returned, we can assume that we are not in
2853041982dSJonathan Peyton   // immediate exec mode
2867cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
2877cc577a4SJonathan Peyton   if (!KMP_TASKING_ENABLED(task_team)) {
2887cc577a4SJonathan Peyton     __kmp_enable_tasking(task_team, thread);
2897cc577a4SJonathan Peyton   }
2907cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(TCR_4(task_team->tt.tt_found_tasks) == TRUE);
2917cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(TCR_PTR(task_team->tt.tt_threads_data) != NULL);
2927cc577a4SJonathan Peyton 
2937cc577a4SJonathan Peyton   // Find tasking deque specific to encountering thread
2947cc577a4SJonathan Peyton   thread_data = &task_team->tt.tt_threads_data[tid];
2957cc577a4SJonathan Peyton 
2967cc577a4SJonathan Peyton   // No lock needed since only owner can allocate
2977cc577a4SJonathan Peyton   if (thread_data->td.td_deque == NULL) {
2987cc577a4SJonathan Peyton     __kmp_alloc_task_deque(thread, thread_data);
2997cc577a4SJonathan Peyton   }
3007cc577a4SJonathan Peyton 
3017cc577a4SJonathan Peyton   // Check if deque is full
3023041982dSJonathan Peyton   if (TCR_4(thread_data->td.td_deque_ntasks) >=
3033041982dSJonathan Peyton       TASK_DEQUE_SIZE(thread_data->td)) {
3043041982dSJonathan Peyton     KA_TRACE(20, ("__kmp_push_task: T#%d deque is full; returning "
3053041982dSJonathan Peyton                   "TASK_NOT_PUSHED for task %p\n",
3067cc577a4SJonathan Peyton                   gtid, taskdata));
3077cc577a4SJonathan Peyton     return TASK_NOT_PUSHED;
3087cc577a4SJonathan Peyton   }
3097cc577a4SJonathan Peyton 
3107cc577a4SJonathan Peyton   // Lock the deque for the task push operation
3117cc577a4SJonathan Peyton   __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
3127cc577a4SJonathan Peyton 
3137cc577a4SJonathan Peyton #if OMP_45_ENABLED
3147cc577a4SJonathan Peyton   // Need to recheck as we can get a proxy task from a thread outside of OpenMP
3153041982dSJonathan Peyton   if (TCR_4(thread_data->td.td_deque_ntasks) >=
3163041982dSJonathan Peyton       TASK_DEQUE_SIZE(thread_data->td)) {
3177cc577a4SJonathan Peyton     __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3183041982dSJonathan Peyton     KA_TRACE(20, ("__kmp_push_task: T#%d deque is full on 2nd check; returning "
3193041982dSJonathan Peyton                   "TASK_NOT_PUSHED for task %p\n",
3207cc577a4SJonathan Peyton                   gtid, taskdata));
3217cc577a4SJonathan Peyton     return TASK_NOT_PUSHED;
3227cc577a4SJonathan Peyton   }
3237cc577a4SJonathan Peyton #else
3247cc577a4SJonathan Peyton   // Must have room since no thread can add tasks but calling thread
3253041982dSJonathan Peyton   KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) <
3263041982dSJonathan Peyton                    TASK_DEQUE_SIZE(thread_data->td));
3277cc577a4SJonathan Peyton #endif
3287cc577a4SJonathan Peyton 
3293041982dSJonathan Peyton   thread_data->td.td_deque[thread_data->td.td_deque_tail] =
3303041982dSJonathan Peyton       taskdata; // Push taskdata
3317cc577a4SJonathan Peyton   // Wrap index.
3323041982dSJonathan Peyton   thread_data->td.td_deque_tail =
3333041982dSJonathan Peyton       (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
3343041982dSJonathan Peyton   TCW_4(thread_data->td.td_deque_ntasks,
3353041982dSJonathan Peyton         TCR_4(thread_data->td.td_deque_ntasks) + 1); // Adjust task count
3367cc577a4SJonathan Peyton 
3377cc577a4SJonathan Peyton   KA_TRACE(20, ("__kmp_push_task: T#%d returning TASK_SUCCESSFULLY_PUSHED: "
3387cc577a4SJonathan Peyton                 "task=%p ntasks=%d head=%u tail=%u\n",
3397cc577a4SJonathan Peyton                 gtid, taskdata, thread_data->td.td_deque_ntasks,
3407cc577a4SJonathan Peyton                 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
3417cc577a4SJonathan Peyton 
3427cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
3437cc577a4SJonathan Peyton 
3447cc577a4SJonathan Peyton   return TASK_SUCCESSFULLY_PUSHED;
3457cc577a4SJonathan Peyton }
3467cc577a4SJonathan Peyton 
3473041982dSJonathan Peyton // __kmp_pop_current_task_from_thread: set up current task from called thread
3483041982dSJonathan Peyton // when team ends
3493041982dSJonathan Peyton //
3507cc577a4SJonathan Peyton // this_thr: thread structure to set current_task in.
3513041982dSJonathan Peyton void __kmp_pop_current_task_from_thread(kmp_info_t *this_thr) {
3523041982dSJonathan Peyton   KF_TRACE(10, ("__kmp_pop_current_task_from_thread(enter): T#%d "
3533041982dSJonathan Peyton                 "this_thread=%p, curtask=%p, "
3547cc577a4SJonathan Peyton                 "curtask_parent=%p\n",
3557cc577a4SJonathan Peyton                 0, this_thr, this_thr->th.th_current_task,
3567cc577a4SJonathan Peyton                 this_thr->th.th_current_task->td_parent));
3577cc577a4SJonathan Peyton 
3587cc577a4SJonathan Peyton   this_thr->th.th_current_task = this_thr->th.th_current_task->td_parent;
3597cc577a4SJonathan Peyton 
3603041982dSJonathan Peyton   KF_TRACE(10, ("__kmp_pop_current_task_from_thread(exit): T#%d "
3613041982dSJonathan Peyton                 "this_thread=%p, curtask=%p, "
3627cc577a4SJonathan Peyton                 "curtask_parent=%p\n",
3637cc577a4SJonathan Peyton                 0, this_thr, this_thr->th.th_current_task,
3647cc577a4SJonathan Peyton                 this_thr->th.th_current_task->td_parent));
3657cc577a4SJonathan Peyton }
3667cc577a4SJonathan Peyton 
3673041982dSJonathan Peyton // __kmp_push_current_task_to_thread: set up current task in called thread for a
3683041982dSJonathan Peyton // new team
3693041982dSJonathan Peyton //
3707cc577a4SJonathan Peyton // this_thr: thread structure to set up
3717cc577a4SJonathan Peyton // team: team for implicit task data
3727cc577a4SJonathan Peyton // tid: thread within team to set up
3733041982dSJonathan Peyton void __kmp_push_current_task_to_thread(kmp_info_t *this_thr, kmp_team_t *team,
3743041982dSJonathan Peyton                                        int tid) {
3753041982dSJonathan Peyton   // current task of the thread is a parent of the new just created implicit
3763041982dSJonathan Peyton   // tasks of new team
3773041982dSJonathan Peyton   KF_TRACE(10, ("__kmp_push_current_task_to_thread(enter): T#%d this_thread=%p "
3783041982dSJonathan Peyton                 "curtask=%p "
3797cc577a4SJonathan Peyton                 "parent_task=%p\n",
3807cc577a4SJonathan Peyton                 tid, this_thr, this_thr->th.th_current_task,
3817cc577a4SJonathan Peyton                 team->t.t_implicit_task_taskdata[tid].td_parent));
3827cc577a4SJonathan Peyton 
3837cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(this_thr != NULL);
3847cc577a4SJonathan Peyton 
3857cc577a4SJonathan Peyton   if (tid == 0) {
3867cc577a4SJonathan Peyton     if (this_thr->th.th_current_task != &team->t.t_implicit_task_taskdata[0]) {
3873041982dSJonathan Peyton       team->t.t_implicit_task_taskdata[0].td_parent =
3883041982dSJonathan Peyton           this_thr->th.th_current_task;
3897cc577a4SJonathan Peyton       this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[0];
3907cc577a4SJonathan Peyton     }
3917cc577a4SJonathan Peyton   } else {
3923041982dSJonathan Peyton     team->t.t_implicit_task_taskdata[tid].td_parent =
3933041982dSJonathan Peyton         team->t.t_implicit_task_taskdata[0].td_parent;
3947cc577a4SJonathan Peyton     this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[tid];
3957cc577a4SJonathan Peyton   }
3967cc577a4SJonathan Peyton 
3973041982dSJonathan Peyton   KF_TRACE(10, ("__kmp_push_current_task_to_thread(exit): T#%d this_thread=%p "
3983041982dSJonathan Peyton                 "curtask=%p "
3997cc577a4SJonathan Peyton                 "parent_task=%p\n",
4007cc577a4SJonathan Peyton                 tid, this_thr, this_thr->th.th_current_task,
4017cc577a4SJonathan Peyton                 team->t.t_implicit_task_taskdata[tid].td_parent));
4027cc577a4SJonathan Peyton }
4037cc577a4SJonathan Peyton 
4047cc577a4SJonathan Peyton // __kmp_task_start: bookkeeping for a task starting execution
4053041982dSJonathan Peyton //
4067cc577a4SJonathan Peyton // GTID: global thread id of calling thread
4077cc577a4SJonathan Peyton // task: task starting execution
4087cc577a4SJonathan Peyton // current_task: task suspending
4093041982dSJonathan Peyton static void __kmp_task_start(kmp_int32 gtid, kmp_task_t *task,
4103041982dSJonathan Peyton                              kmp_taskdata_t *current_task) {
4117cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4127cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
4137cc577a4SJonathan Peyton 
4143041982dSJonathan Peyton   KA_TRACE(10,
4153041982dSJonathan Peyton            ("__kmp_task_start(enter): T#%d starting task %p: current_task=%p\n",
4167cc577a4SJonathan Peyton             gtid, taskdata, current_task));
4177cc577a4SJonathan Peyton 
4187cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
4197cc577a4SJonathan Peyton 
4207cc577a4SJonathan Peyton   // mark currently executing task as suspended
4217cc577a4SJonathan Peyton   // TODO: GEH - make sure root team implicit task is initialized properly.
4227cc577a4SJonathan Peyton   // KMP_DEBUG_ASSERT( current_task -> td_flags.executing == 1 );
4237cc577a4SJonathan Peyton   current_task->td_flags.executing = 0;
4247cc577a4SJonathan Peyton 
4257cc577a4SJonathan Peyton // Add task to stack if tied
4267cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK
4273041982dSJonathan Peyton   if (taskdata->td_flags.tiedness == TASK_TIED) {
4287cc577a4SJonathan Peyton     __kmp_push_task_stack(gtid, thread, taskdata);
4297cc577a4SJonathan Peyton   }
4307cc577a4SJonathan Peyton #endif /* BUILD_TIED_TASK_STACK */
4317cc577a4SJonathan Peyton 
4327cc577a4SJonathan Peyton   // mark starting task as executing and as current task
4337cc577a4SJonathan Peyton   thread->th.th_current_task = taskdata;
4347cc577a4SJonathan Peyton 
4353041982dSJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.started == 0 ||
4363041982dSJonathan Peyton                    taskdata->td_flags.tiedness == TASK_UNTIED);
4373041982dSJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0 ||
4383041982dSJonathan Peyton                    taskdata->td_flags.tiedness == TASK_UNTIED);
4397cc577a4SJonathan Peyton   taskdata->td_flags.started = 1;
4407cc577a4SJonathan Peyton   taskdata->td_flags.executing = 1;
4417cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
4427cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
4437cc577a4SJonathan Peyton 
4447cc577a4SJonathan Peyton   // GEH TODO: shouldn't we pass some sort of location identifier here?
4457cc577a4SJonathan Peyton   // APT: yes, we will pass location here.
4467cc577a4SJonathan Peyton   // need to store current thread state (in a thread or taskdata structure)
4477cc577a4SJonathan Peyton   // before setting work_state, otherwise wrong state is set after end of task
4487cc577a4SJonathan Peyton 
4493041982dSJonathan Peyton   KA_TRACE(10, ("__kmp_task_start(exit): T#%d task=%p\n", gtid, taskdata));
4507cc577a4SJonathan Peyton 
4517cc577a4SJonathan Peyton #if OMPT_SUPPORT
4523041982dSJonathan Peyton   if (ompt_enabled && ompt_callbacks.ompt_callback(ompt_event_task_begin)) {
4537cc577a4SJonathan Peyton     kmp_taskdata_t *parent = taskdata->td_parent;
4547cc577a4SJonathan Peyton     ompt_callbacks.ompt_callback(ompt_event_task_begin)(
4557cc577a4SJonathan Peyton         parent ? parent->ompt_task_info.task_id : ompt_task_id_none,
4567cc577a4SJonathan Peyton         parent ? &(parent->ompt_task_info.frame) : NULL,
4573041982dSJonathan Peyton         taskdata->ompt_task_info.task_id, taskdata->ompt_task_info.function);
4587cc577a4SJonathan Peyton   }
4597cc577a4SJonathan Peyton #endif
4607cc577a4SJonathan Peyton #if OMP_40_ENABLED && OMPT_SUPPORT && OMPT_TRACE
4617cc577a4SJonathan Peyton   /* OMPT emit all dependences if requested by the tool */
4627cc577a4SJonathan Peyton   if (ompt_enabled && taskdata->ompt_task_info.ndeps > 0 &&
4633041982dSJonathan Peyton       ompt_callbacks.ompt_callback(ompt_event_task_dependences)) {
4647cc577a4SJonathan Peyton     ompt_callbacks.ompt_callback(ompt_event_task_dependences)(
4653041982dSJonathan Peyton         taskdata->ompt_task_info.task_id, taskdata->ompt_task_info.deps,
4663041982dSJonathan Peyton         taskdata->ompt_task_info.ndeps);
4677cc577a4SJonathan Peyton     /* We can now free the allocated memory for the dependencies */
4687cc577a4SJonathan Peyton     KMP_OMPT_DEPS_FREE(thread, taskdata->ompt_task_info.deps);
4697cc577a4SJonathan Peyton     taskdata->ompt_task_info.deps = NULL;
4707cc577a4SJonathan Peyton     taskdata->ompt_task_info.ndeps = 0;
4717cc577a4SJonathan Peyton   }
4727cc577a4SJonathan Peyton #endif /* OMP_40_ENABLED && OMPT_SUPPORT && OMPT_TRACE */
4737cc577a4SJonathan Peyton 
4747cc577a4SJonathan Peyton   return;
4757cc577a4SJonathan Peyton }
4767cc577a4SJonathan Peyton 
4773041982dSJonathan Peyton // __kmpc_omp_task_begin_if0: report that a given serialized task has started
4783041982dSJonathan Peyton // execution
4793041982dSJonathan Peyton //
4807cc577a4SJonathan Peyton // loc_ref: source location information; points to beginning of task block.
4817cc577a4SJonathan Peyton // gtid: global thread number.
4827cc577a4SJonathan Peyton // task: task thunk for the started task.
4833041982dSJonathan Peyton void __kmpc_omp_task_begin_if0(ident_t *loc_ref, kmp_int32 gtid,
4843041982dSJonathan Peyton                                kmp_task_t *task) {
4857cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
4867cc577a4SJonathan Peyton   kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
4877cc577a4SJonathan Peyton 
4883041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_begin_if0(enter): T#%d loc=%p task=%p "
4893041982dSJonathan Peyton                 "current_task=%p\n",
4907cc577a4SJonathan Peyton                 gtid, loc_ref, taskdata, current_task));
4917cc577a4SJonathan Peyton 
4927cc577a4SJonathan Peyton   if (taskdata->td_flags.tiedness == TASK_UNTIED) {
4933041982dSJonathan Peyton     // untied task needs to increment counter so that the task structure is not
4943041982dSJonathan Peyton     // freed prematurely
4957cc577a4SJonathan Peyton     kmp_int32 counter = 1 + KMP_TEST_THEN_INC32(&taskdata->td_untied_count);
4963041982dSJonathan Peyton     KA_TRACE(20, ("__kmpc_omp_task_begin_if0: T#%d untied_count (%d) "
4973041982dSJonathan Peyton                   "incremented for task %p\n",
4987cc577a4SJonathan Peyton                   gtid, counter, taskdata));
4997cc577a4SJonathan Peyton   }
5007cc577a4SJonathan Peyton 
5013041982dSJonathan Peyton   taskdata->td_flags.task_serial =
5023041982dSJonathan Peyton       1; // Execute this task immediately, not deferred.
5037cc577a4SJonathan Peyton   __kmp_task_start(gtid, task, current_task);
5047cc577a4SJonathan Peyton 
5053041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_begin_if0(exit): T#%d loc=%p task=%p,\n", gtid,
5063041982dSJonathan Peyton                 loc_ref, taskdata));
5077cc577a4SJonathan Peyton 
5087cc577a4SJonathan Peyton   return;
5097cc577a4SJonathan Peyton }
5107cc577a4SJonathan Peyton 
5117cc577a4SJonathan Peyton #ifdef TASK_UNUSED
5127cc577a4SJonathan Peyton // __kmpc_omp_task_begin: report that a given task has started execution
5137cc577a4SJonathan Peyton // NEVER GENERATED BY COMPILER, DEPRECATED!!!
5143041982dSJonathan Peyton void __kmpc_omp_task_begin(ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task) {
5157cc577a4SJonathan Peyton   kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
5167cc577a4SJonathan Peyton 
5173041982dSJonathan Peyton   KA_TRACE(
5183041982dSJonathan Peyton       10,
5193041982dSJonathan Peyton       ("__kmpc_omp_task_begin(enter): T#%d loc=%p task=%p current_task=%p\n",
5207cc577a4SJonathan Peyton        gtid, loc_ref, KMP_TASK_TO_TASKDATA(task), current_task));
5217cc577a4SJonathan Peyton 
5227cc577a4SJonathan Peyton   __kmp_task_start(gtid, task, current_task);
5237cc577a4SJonathan Peyton 
5243041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_begin(exit): T#%d loc=%p task=%p,\n", gtid,
5253041982dSJonathan Peyton                 loc_ref, KMP_TASK_TO_TASKDATA(task)));
5267cc577a4SJonathan Peyton   return;
5277cc577a4SJonathan Peyton }
5287cc577a4SJonathan Peyton #endif // TASK_UNUSED
5297cc577a4SJonathan Peyton 
5307cc577a4SJonathan Peyton // __kmp_free_task: free the current task space and the space for shareds
5313041982dSJonathan Peyton //
5327cc577a4SJonathan Peyton // gtid: Global thread ID of calling thread
5337cc577a4SJonathan Peyton // taskdata: task to free
5347cc577a4SJonathan Peyton // thread: thread data structure of caller
5353041982dSJonathan Peyton static void __kmp_free_task(kmp_int32 gtid, kmp_taskdata_t *taskdata,
5363041982dSJonathan Peyton                             kmp_info_t *thread) {
5373041982dSJonathan Peyton   KA_TRACE(30, ("__kmp_free_task: T#%d freeing data from task %p\n", gtid,
5383041982dSJonathan Peyton                 taskdata));
5397cc577a4SJonathan Peyton 
5407cc577a4SJonathan Peyton   // Check to make sure all flags and counters have the correct values
5417cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
5427cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0);
5437cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 1);
5447cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
5453041982dSJonathan Peyton   KMP_DEBUG_ASSERT(TCR_4(taskdata->td_allocated_child_tasks) == 0 ||
5463041982dSJonathan Peyton                    taskdata->td_flags.task_serial == 1);
5477cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(TCR_4(taskdata->td_incomplete_child_tasks) == 0);
5487cc577a4SJonathan Peyton 
5497cc577a4SJonathan Peyton   taskdata->td_flags.freed = 1;
5507cc577a4SJonathan Peyton   ANNOTATE_HAPPENS_BEFORE(taskdata);
5517cc577a4SJonathan Peyton // deallocate the taskdata and shared variable blocks associated with this task
5527cc577a4SJonathan Peyton #if USE_FAST_MEMORY
5537cc577a4SJonathan Peyton   __kmp_fast_free(thread, taskdata);
5547cc577a4SJonathan Peyton #else /* ! USE_FAST_MEMORY */
5557cc577a4SJonathan Peyton   __kmp_thread_free(thread, taskdata);
5567cc577a4SJonathan Peyton #endif
5577cc577a4SJonathan Peyton 
5583041982dSJonathan Peyton   KA_TRACE(20, ("__kmp_free_task: T#%d freed task %p\n", gtid, taskdata));
5597cc577a4SJonathan Peyton }
5607cc577a4SJonathan Peyton 
5613041982dSJonathan Peyton // __kmp_free_task_and_ancestors: free the current task and ancestors without
5623041982dSJonathan Peyton // children
5637cc577a4SJonathan Peyton //
5647cc577a4SJonathan Peyton // gtid: Global thread ID of calling thread
5657cc577a4SJonathan Peyton // taskdata: task to free
5667cc577a4SJonathan Peyton // thread: thread data structure of caller
5673041982dSJonathan Peyton static void __kmp_free_task_and_ancestors(kmp_int32 gtid,
5683041982dSJonathan Peyton                                           kmp_taskdata_t *taskdata,
5693041982dSJonathan Peyton                                           kmp_info_t *thread) {
5707cc577a4SJonathan Peyton #if OMP_45_ENABLED
5717cc577a4SJonathan Peyton   // Proxy tasks must always be allowed to free their parents
5727cc577a4SJonathan Peyton   // because they can be run in background even in serial mode.
5733041982dSJonathan Peyton   kmp_int32 team_serial =
5743041982dSJonathan Peyton       (taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) &&
5753041982dSJonathan Peyton       !taskdata->td_flags.proxy;
5767cc577a4SJonathan Peyton #else
5773041982dSJonathan Peyton   kmp_int32 team_serial =
5783041982dSJonathan Peyton       taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser;
5797cc577a4SJonathan Peyton #endif
5807cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
5817cc577a4SJonathan Peyton 
5825ba90c79SAndrey Churbanov   kmp_int32 children =
5835ba90c79SAndrey Churbanov       KMP_TEST_THEN_DEC32(&taskdata->td_allocated_child_tasks) - 1;
5847cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(children >= 0);
5857cc577a4SJonathan Peyton 
5867cc577a4SJonathan Peyton   // Now, go up the ancestor tree to see if any ancestors can now be freed.
5873041982dSJonathan Peyton   while (children == 0) {
5887cc577a4SJonathan Peyton     kmp_taskdata_t *parent_taskdata = taskdata->td_parent;
5897cc577a4SJonathan Peyton 
5907cc577a4SJonathan Peyton     KA_TRACE(20, ("__kmp_free_task_and_ancestors(enter): T#%d task %p complete "
5913041982dSJonathan Peyton                   "and freeing itself\n",
5923041982dSJonathan Peyton                   gtid, taskdata));
5937cc577a4SJonathan Peyton 
5947cc577a4SJonathan Peyton     // --- Deallocate my ancestor task ---
5957cc577a4SJonathan Peyton     __kmp_free_task(gtid, taskdata, thread);
5967cc577a4SJonathan Peyton 
5977cc577a4SJonathan Peyton     taskdata = parent_taskdata;
5987cc577a4SJonathan Peyton 
5993041982dSJonathan Peyton     // Stop checking ancestors at implicit task instead of walking up ancestor
6003041982dSJonathan Peyton     // tree to avoid premature deallocation of ancestors.
6017cc577a4SJonathan Peyton     if (team_serial || taskdata->td_flags.tasktype == TASK_IMPLICIT)
6027cc577a4SJonathan Peyton       return;
6037cc577a4SJonathan Peyton 
6047cc577a4SJonathan Peyton     // Predecrement simulated by "- 1" calculation
6055ba90c79SAndrey Churbanov     children = KMP_TEST_THEN_DEC32(&taskdata->td_allocated_child_tasks) - 1;
6067cc577a4SJonathan Peyton     KMP_DEBUG_ASSERT(children >= 0);
6077cc577a4SJonathan Peyton   }
6087cc577a4SJonathan Peyton 
6093041982dSJonathan Peyton   KA_TRACE(
6103041982dSJonathan Peyton       20, ("__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; "
6113041982dSJonathan Peyton            "not freeing it yet\n",
6123041982dSJonathan Peyton            gtid, taskdata, children));
6137cc577a4SJonathan Peyton }
6147cc577a4SJonathan Peyton 
6157cc577a4SJonathan Peyton // __kmp_task_finish: bookkeeping to do when a task finishes execution
6163041982dSJonathan Peyton //
6177cc577a4SJonathan Peyton // gtid: global thread ID for calling thread
6187cc577a4SJonathan Peyton // task: task to be finished
6197cc577a4SJonathan Peyton // resumed_task: task to be resumed.  (may be NULL if task is serialized)
6203041982dSJonathan Peyton static void __kmp_task_finish(kmp_int32 gtid, kmp_task_t *task,
6213041982dSJonathan Peyton                               kmp_taskdata_t *resumed_task) {
6227cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
6237cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
6243041982dSJonathan Peyton   kmp_task_team_t *task_team =
6253041982dSJonathan Peyton       thread->th.th_task_team; // might be NULL for serial teams...
6267cc577a4SJonathan Peyton   kmp_int32 children = 0;
6277cc577a4SJonathan Peyton 
6287cc577a4SJonathan Peyton #if OMPT_SUPPORT
6293041982dSJonathan Peyton   if (ompt_enabled && ompt_callbacks.ompt_callback(ompt_event_task_end)) {
6307cc577a4SJonathan Peyton     kmp_taskdata_t *parent = taskdata->td_parent;
6317cc577a4SJonathan Peyton     ompt_callbacks.ompt_callback(ompt_event_task_end)(
6327cc577a4SJonathan Peyton         taskdata->ompt_task_info.task_id);
6337cc577a4SJonathan Peyton   }
6347cc577a4SJonathan Peyton #endif
6357cc577a4SJonathan Peyton 
6363041982dSJonathan Peyton   KA_TRACE(10, ("__kmp_task_finish(enter): T#%d finishing task %p and resuming "
6373041982dSJonathan Peyton                 "task %p\n",
6387cc577a4SJonathan Peyton                 gtid, taskdata, resumed_task));
6397cc577a4SJonathan Peyton 
6407cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
6417cc577a4SJonathan Peyton 
6427cc577a4SJonathan Peyton // Pop task from stack if tied
6437cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK
6443041982dSJonathan Peyton   if (taskdata->td_flags.tiedness == TASK_TIED) {
6457cc577a4SJonathan Peyton     __kmp_pop_task_stack(gtid, thread, taskdata);
6467cc577a4SJonathan Peyton   }
6477cc577a4SJonathan Peyton #endif /* BUILD_TIED_TASK_STACK */
6487cc577a4SJonathan Peyton 
6497cc577a4SJonathan Peyton   if (taskdata->td_flags.tiedness == TASK_UNTIED) {
6503041982dSJonathan Peyton     // untied task needs to check the counter so that the task structure is not
6513041982dSJonathan Peyton     // freed prematurely
6527cc577a4SJonathan Peyton     kmp_int32 counter = KMP_TEST_THEN_DEC32(&taskdata->td_untied_count) - 1;
6533041982dSJonathan Peyton     KA_TRACE(
6543041982dSJonathan Peyton         20,
6553041982dSJonathan Peyton         ("__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n",
6567cc577a4SJonathan Peyton          gtid, counter, taskdata));
6577cc577a4SJonathan Peyton     if (counter > 0) {
6583041982dSJonathan Peyton       // untied task is not done, to be continued possibly by other thread, do
6593041982dSJonathan Peyton       // not free it now
6607cc577a4SJonathan Peyton       if (resumed_task == NULL) {
6617cc577a4SJonathan Peyton         KMP_DEBUG_ASSERT(taskdata->td_flags.task_serial);
6623041982dSJonathan Peyton         resumed_task = taskdata->td_parent; // In a serialized task, the resumed
6633041982dSJonathan Peyton         // task is the parent
6647cc577a4SJonathan Peyton       }
6657cc577a4SJonathan Peyton       thread->th.th_current_task = resumed_task; // restore current_task
6667cc577a4SJonathan Peyton       resumed_task->td_flags.executing = 1; // resume previous task
6673041982dSJonathan Peyton       KA_TRACE(10, ("__kmp_task_finish(exit): T#%d partially done task %p, "
6683041982dSJonathan Peyton                     "resuming task %p\n",
6697cc577a4SJonathan Peyton                     gtid, taskdata, resumed_task));
6707cc577a4SJonathan Peyton       return;
6717cc577a4SJonathan Peyton     }
6727cc577a4SJonathan Peyton   }
6737cc577a4SJonathan Peyton 
6747cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
6757cc577a4SJonathan Peyton   taskdata->td_flags.complete = 1; // mark the task as completed
6767cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.started == 1);
6777cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
6787cc577a4SJonathan Peyton 
6793041982dSJonathan Peyton   // Only need to keep track of count if team parallel and tasking not
6803041982dSJonathan Peyton   // serialized
6817cc577a4SJonathan Peyton   if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
6827cc577a4SJonathan Peyton     // Predecrement simulated by "- 1" calculation
6833041982dSJonathan Peyton     children =
6845ba90c79SAndrey Churbanov         KMP_TEST_THEN_DEC32(&taskdata->td_parent->td_incomplete_child_tasks) -
6853041982dSJonathan Peyton         1;
6867cc577a4SJonathan Peyton     KMP_DEBUG_ASSERT(children >= 0);
6877cc577a4SJonathan Peyton #if OMP_40_ENABLED
6887cc577a4SJonathan Peyton     if (taskdata->td_taskgroup)
6897cc577a4SJonathan Peyton       KMP_TEST_THEN_DEC32((kmp_int32 *)(&taskdata->td_taskgroup->count));
6907cc577a4SJonathan Peyton #if OMP_45_ENABLED
6917cc577a4SJonathan Peyton   }
6927cc577a4SJonathan Peyton   // if we found proxy tasks there could exist a dependency chain
6937cc577a4SJonathan Peyton   // with the proxy task as origin
6943041982dSJonathan Peyton   if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) ||
6953041982dSJonathan Peyton       (task_team && task_team->tt.tt_found_proxy_tasks)) {
6967cc577a4SJonathan Peyton #endif
6977cc577a4SJonathan Peyton     __kmp_release_deps(gtid, taskdata);
6987cc577a4SJonathan Peyton #endif
6997cc577a4SJonathan Peyton   }
7007cc577a4SJonathan Peyton 
7013041982dSJonathan Peyton   // td_flags.executing must be marked as 0 after __kmp_release_deps has been
7023041982dSJonathan Peyton   // called. Othertwise, if a task is executed immediately from the release_deps
7033041982dSJonathan Peyton   // code, the flag will be reset to 1 again by this same function
7047cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
7057cc577a4SJonathan Peyton   taskdata->td_flags.executing = 0; // suspend the finishing task
7067cc577a4SJonathan Peyton 
7073041982dSJonathan Peyton   KA_TRACE(
7083041982dSJonathan Peyton       20, ("__kmp_task_finish: T#%d finished task %p, %d incomplete children\n",
7097cc577a4SJonathan Peyton            gtid, taskdata, children));
7107cc577a4SJonathan Peyton 
7117cc577a4SJonathan Peyton #if OMP_40_ENABLED
7127cc577a4SJonathan Peyton   /* If the tasks' destructor thunk flag has been set, we need to invoke the
7133041982dSJonathan Peyton      destructor thunk that has been generated by the compiler. The code is
7143041982dSJonathan Peyton      placed here, since at this point other tasks might have been released
7157cc577a4SJonathan Peyton      hence overlapping the destructor invokations with some other work in the
7163041982dSJonathan Peyton      released tasks.  The OpenMP spec is not specific on when the destructors
7173041982dSJonathan Peyton      are invoked, so we should be free to choose. */
7187cc577a4SJonathan Peyton   if (taskdata->td_flags.destructors_thunk) {
7197cc577a4SJonathan Peyton     kmp_routine_entry_t destr_thunk = task->data1.destructors;
7207cc577a4SJonathan Peyton     KMP_ASSERT(destr_thunk);
7217cc577a4SJonathan Peyton     destr_thunk(gtid, task);
7227cc577a4SJonathan Peyton   }
7237cc577a4SJonathan Peyton #endif // OMP_40_ENABLED
7247cc577a4SJonathan Peyton 
7257cc577a4SJonathan Peyton   // bookkeeping for resuming task:
7267cc577a4SJonathan Peyton   // GEH - note tasking_ser => task_serial
7273041982dSJonathan Peyton   KMP_DEBUG_ASSERT(
7283041982dSJonathan Peyton       (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) ==
7297cc577a4SJonathan Peyton       taskdata->td_flags.task_serial);
7303041982dSJonathan Peyton   if (taskdata->td_flags.task_serial) {
7317cc577a4SJonathan Peyton     if (resumed_task == NULL) {
7323041982dSJonathan Peyton       resumed_task = taskdata->td_parent; // In a serialized task, the resumed
7333041982dSJonathan Peyton       // task is the parent
7343041982dSJonathan Peyton     } else
7357cc577a4SJonathan Peyton #if OMP_45_ENABLED
7367cc577a4SJonathan Peyton         if (!(task_team && task_team->tt.tt_found_proxy_tasks))
7377cc577a4SJonathan Peyton #endif
7387cc577a4SJonathan Peyton     {
7397cc577a4SJonathan Peyton       // verify resumed task passed in points to parent
7407cc577a4SJonathan Peyton       KMP_DEBUG_ASSERT(resumed_task == taskdata->td_parent);
7417cc577a4SJonathan Peyton     }
7423041982dSJonathan Peyton   } else {
7433041982dSJonathan Peyton     KMP_DEBUG_ASSERT(resumed_task !=
7443041982dSJonathan Peyton                      NULL); // verify that resumed task is passed as arguemnt
7457cc577a4SJonathan Peyton   }
7467cc577a4SJonathan Peyton 
7477cc577a4SJonathan Peyton   // Free this task and then ancestor tasks if they have no children.
7487cc577a4SJonathan Peyton   // Restore th_current_task first as suggested by John:
7497cc577a4SJonathan Peyton   // johnmc: if an asynchronous inquiry peers into the runtime system
7507cc577a4SJonathan Peyton   // it doesn't see the freed task as the current task.
7517cc577a4SJonathan Peyton   thread->th.th_current_task = resumed_task;
7527cc577a4SJonathan Peyton   __kmp_free_task_and_ancestors(gtid, taskdata, thread);
7537cc577a4SJonathan Peyton 
7547cc577a4SJonathan Peyton   // TODO: GEH - make sure root team implicit task is initialized properly.
7557cc577a4SJonathan Peyton   // KMP_DEBUG_ASSERT( resumed_task->td_flags.executing == 0 );
7567cc577a4SJonathan Peyton   resumed_task->td_flags.executing = 1; // resume previous task
7577cc577a4SJonathan Peyton 
7583041982dSJonathan Peyton   KA_TRACE(
7593041982dSJonathan Peyton       10, ("__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n",
7607cc577a4SJonathan Peyton            gtid, taskdata, resumed_task));
7617cc577a4SJonathan Peyton 
7627cc577a4SJonathan Peyton   return;
7637cc577a4SJonathan Peyton }
7647cc577a4SJonathan Peyton 
7657cc577a4SJonathan Peyton // __kmpc_omp_task_complete_if0: report that a task has completed execution
7663041982dSJonathan Peyton //
7677cc577a4SJonathan Peyton // loc_ref: source location information; points to end of task block.
7687cc577a4SJonathan Peyton // gtid: global thread number.
7697cc577a4SJonathan Peyton // task: task thunk for the completed task.
7703041982dSJonathan Peyton void __kmpc_omp_task_complete_if0(ident_t *loc_ref, kmp_int32 gtid,
7713041982dSJonathan Peyton                                   kmp_task_t *task) {
7727cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n",
7737cc577a4SJonathan Peyton                 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
7743041982dSJonathan Peyton   // this routine will provide task to resume
7753041982dSJonathan Peyton   __kmp_task_finish(gtid, task, NULL);
7767cc577a4SJonathan Peyton 
7777cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n",
7787cc577a4SJonathan Peyton                 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
7797cc577a4SJonathan Peyton   return;
7807cc577a4SJonathan Peyton }
7817cc577a4SJonathan Peyton 
7827cc577a4SJonathan Peyton #ifdef TASK_UNUSED
7837cc577a4SJonathan Peyton // __kmpc_omp_task_complete: report that a task has completed execution
7847cc577a4SJonathan Peyton // NEVER GENERATED BY COMPILER, DEPRECATED!!!
7853041982dSJonathan Peyton void __kmpc_omp_task_complete(ident_t *loc_ref, kmp_int32 gtid,
7863041982dSJonathan Peyton                               kmp_task_t *task) {
7873041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n", gtid,
7883041982dSJonathan Peyton                 loc_ref, KMP_TASK_TO_TASKDATA(task)));
7897cc577a4SJonathan Peyton 
7907cc577a4SJonathan Peyton   __kmp_task_finish(gtid, task, NULL); // Not sure how to find task to resume
7917cc577a4SJonathan Peyton 
7923041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n", gtid,
7933041982dSJonathan Peyton                 loc_ref, KMP_TASK_TO_TASKDATA(task)));
7947cc577a4SJonathan Peyton   return;
7957cc577a4SJonathan Peyton }
7967cc577a4SJonathan Peyton #endif // TASK_UNUSED
7977cc577a4SJonathan Peyton 
7987cc577a4SJonathan Peyton #if OMPT_SUPPORT
7993041982dSJonathan Peyton // __kmp_task_init_ompt: Initialize OMPT fields maintained by a task. This will
8003041982dSJonathan Peyton //  only be called after ompt_tool, so we already know whether ompt is enabled
8013041982dSJonathan Peyton // or not.
8023041982dSJonathan Peyton static inline void __kmp_task_init_ompt(kmp_taskdata_t *task, int tid,
8033041982dSJonathan Peyton                                         void *function) {
8047cc577a4SJonathan Peyton   if (ompt_enabled) {
8057cc577a4SJonathan Peyton     task->ompt_task_info.task_id = __ompt_task_id_new(tid);
8067cc577a4SJonathan Peyton     task->ompt_task_info.function = function;
8077cc577a4SJonathan Peyton     task->ompt_task_info.frame.exit_runtime_frame = NULL;
8087cc577a4SJonathan Peyton     task->ompt_task_info.frame.reenter_runtime_frame = NULL;
8097cc577a4SJonathan Peyton #if OMP_40_ENABLED
8107cc577a4SJonathan Peyton     task->ompt_task_info.ndeps = 0;
8117cc577a4SJonathan Peyton     task->ompt_task_info.deps = NULL;
8127cc577a4SJonathan Peyton #endif /* OMP_40_ENABLED */
8137cc577a4SJonathan Peyton   }
8147cc577a4SJonathan Peyton }
8157cc577a4SJonathan Peyton #endif
8167cc577a4SJonathan Peyton 
8173041982dSJonathan Peyton // __kmp_init_implicit_task: Initialize the appropriate fields in the implicit
8183041982dSJonathan Peyton // task for a given thread
8197cc577a4SJonathan Peyton //
8207cc577a4SJonathan Peyton // loc_ref:  reference to source location of parallel region
8217cc577a4SJonathan Peyton // this_thr:  thread data structure corresponding to implicit task
8227cc577a4SJonathan Peyton // team: team for this_thr
8237cc577a4SJonathan Peyton // tid: thread id of given thread within team
8247cc577a4SJonathan Peyton // set_curr_task: TRUE if need to push current task to thread
8253041982dSJonathan Peyton // NOTE: Routine does not set up the implicit task ICVS.  This is assumed to
8263041982dSJonathan Peyton // have already been done elsewhere.
8277cc577a4SJonathan Peyton // TODO: Get better loc_ref.  Value passed in may be NULL
8283041982dSJonathan Peyton void __kmp_init_implicit_task(ident_t *loc_ref, kmp_info_t *this_thr,
8293041982dSJonathan Peyton                               kmp_team_t *team, int tid, int set_curr_task) {
8307cc577a4SJonathan Peyton   kmp_taskdata_t *task = &team->t.t_implicit_task_taskdata[tid];
8317cc577a4SJonathan Peyton 
8323041982dSJonathan Peyton   KF_TRACE(
8333041982dSJonathan Peyton       10,
8343041982dSJonathan Peyton       ("__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n",
8357cc577a4SJonathan Peyton        tid, team, task, set_curr_task ? "TRUE" : "FALSE"));
8367cc577a4SJonathan Peyton 
8377cc577a4SJonathan Peyton   task->td_task_id = KMP_GEN_TASK_ID();
8387cc577a4SJonathan Peyton   task->td_team = team;
8393041982dSJonathan Peyton   //    task->td_parent   = NULL;  // fix for CQ230101 (broken parent task info
8403041982dSJonathan Peyton   //    in debugger)
8417cc577a4SJonathan Peyton   task->td_ident = loc_ref;
8427cc577a4SJonathan Peyton   task->td_taskwait_ident = NULL;
8437cc577a4SJonathan Peyton   task->td_taskwait_counter = 0;
8447cc577a4SJonathan Peyton   task->td_taskwait_thread = 0;
8457cc577a4SJonathan Peyton 
8467cc577a4SJonathan Peyton   task->td_flags.tiedness = TASK_TIED;
8477cc577a4SJonathan Peyton   task->td_flags.tasktype = TASK_IMPLICIT;
8487cc577a4SJonathan Peyton #if OMP_45_ENABLED
8497cc577a4SJonathan Peyton   task->td_flags.proxy = TASK_FULL;
8507cc577a4SJonathan Peyton #endif
8517cc577a4SJonathan Peyton 
8527cc577a4SJonathan Peyton   // All implicit tasks are executed immediately, not deferred
8537cc577a4SJonathan Peyton   task->td_flags.task_serial = 1;
8547cc577a4SJonathan Peyton   task->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
8557cc577a4SJonathan Peyton   task->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
8567cc577a4SJonathan Peyton 
8577cc577a4SJonathan Peyton   task->td_flags.started = 1;
8587cc577a4SJonathan Peyton   task->td_flags.executing = 1;
8597cc577a4SJonathan Peyton   task->td_flags.complete = 0;
8607cc577a4SJonathan Peyton   task->td_flags.freed = 0;
8617cc577a4SJonathan Peyton 
8627cc577a4SJonathan Peyton #if OMP_40_ENABLED
8637cc577a4SJonathan Peyton   task->td_depnode = NULL;
8647cc577a4SJonathan Peyton #endif
8657cc577a4SJonathan Peyton 
8663041982dSJonathan Peyton   if (set_curr_task) { // only do this init first time thread is created
8677cc577a4SJonathan Peyton     task->td_incomplete_child_tasks = 0;
868642688b6SJonathan Peyton     // Not used: don't need to deallocate implicit task
869642688b6SJonathan Peyton     task->td_allocated_child_tasks = 0;
8707cc577a4SJonathan Peyton #if OMP_40_ENABLED
8717cc577a4SJonathan Peyton     task->td_taskgroup = NULL; // An implicit task does not have taskgroup
8727cc577a4SJonathan Peyton     task->td_dephash = NULL;
8737cc577a4SJonathan Peyton #endif
8747cc577a4SJonathan Peyton     __kmp_push_current_task_to_thread(this_thr, team, tid);
8757cc577a4SJonathan Peyton   } else {
8767cc577a4SJonathan Peyton     KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0);
8777cc577a4SJonathan Peyton     KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0);
8787cc577a4SJonathan Peyton   }
8797cc577a4SJonathan Peyton 
8807cc577a4SJonathan Peyton #if OMPT_SUPPORT
8817cc577a4SJonathan Peyton   __kmp_task_init_ompt(task, tid, NULL);
8827cc577a4SJonathan Peyton #endif
8837cc577a4SJonathan Peyton 
8843041982dSJonathan Peyton   KF_TRACE(10, ("__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n", tid,
8853041982dSJonathan Peyton                 team, task));
8867cc577a4SJonathan Peyton }
8877cc577a4SJonathan Peyton 
8883041982dSJonathan Peyton // __kmp_finish_implicit_task: Release resources associated to implicit tasks
8893041982dSJonathan Peyton // at the end of parallel regions. Some resources are kept for reuse in the next
8903041982dSJonathan Peyton // parallel region.
8917cc577a4SJonathan Peyton //
8923041982dSJonathan Peyton // thread:  thread data structure corresponding to implicit task
8933041982dSJonathan Peyton void __kmp_finish_implicit_task(kmp_info_t *thread) {
8947cc577a4SJonathan Peyton   kmp_taskdata_t *task = thread->th.th_current_task;
8957cc577a4SJonathan Peyton   if (task->td_dephash)
8967cc577a4SJonathan Peyton     __kmp_dephash_free_entries(thread, task->td_dephash);
8977cc577a4SJonathan Peyton }
8987cc577a4SJonathan Peyton 
8993041982dSJonathan Peyton // __kmp_free_implicit_task: Release resources associated to implicit tasks
9003041982dSJonathan Peyton // when these are destroyed regions
9017cc577a4SJonathan Peyton //
9023041982dSJonathan Peyton // thread:  thread data structure corresponding to implicit task
9033041982dSJonathan Peyton void __kmp_free_implicit_task(kmp_info_t *thread) {
9047cc577a4SJonathan Peyton   kmp_taskdata_t *task = thread->th.th_current_task;
9057cc577a4SJonathan Peyton   if (task->td_dephash)
9067cc577a4SJonathan Peyton     __kmp_dephash_free(thread, task->td_dephash);
9077cc577a4SJonathan Peyton   task->td_dephash = NULL;
9087cc577a4SJonathan Peyton }
9097cc577a4SJonathan Peyton 
9103041982dSJonathan Peyton // Round up a size to a power of two specified by val: Used to insert padding
9113041982dSJonathan Peyton // between structures co-allocated using a single malloc() call
9123041982dSJonathan Peyton static size_t __kmp_round_up_to_val(size_t size, size_t val) {
9137cc577a4SJonathan Peyton   if (size & (val - 1)) {
9147cc577a4SJonathan Peyton     size &= ~(val - 1);
9157cc577a4SJonathan Peyton     if (size <= KMP_SIZE_T_MAX - val) {
9167cc577a4SJonathan Peyton       size += val; // Round up if there is no overflow.
9177cc577a4SJonathan Peyton     }; // if
9187cc577a4SJonathan Peyton   }; // if
9197cc577a4SJonathan Peyton   return size;
9207cc577a4SJonathan Peyton } // __kmp_round_up_to_va
9217cc577a4SJonathan Peyton 
9227cc577a4SJonathan Peyton // __kmp_task_alloc: Allocate the taskdata and task data structures for a task
9237cc577a4SJonathan Peyton //
9247cc577a4SJonathan Peyton // loc_ref: source location information
9257cc577a4SJonathan Peyton // gtid: global thread number.
9263041982dSJonathan Peyton // flags: include tiedness & task type (explicit vs. implicit) of the ''new''
9273041982dSJonathan Peyton // task encountered. Converted from kmp_int32 to kmp_tasking_flags_t in routine.
9283041982dSJonathan Peyton // sizeof_kmp_task_t:  Size in bytes of kmp_task_t data structure including
9293041982dSJonathan Peyton // private vars accessed in task.
9303041982dSJonathan Peyton // sizeof_shareds:  Size in bytes of array of pointers to shared vars accessed
9313041982dSJonathan Peyton // in task.
9327cc577a4SJonathan Peyton // task_entry: Pointer to task code entry point generated by compiler.
9337cc577a4SJonathan Peyton // returns: a pointer to the allocated kmp_task_t structure (task).
9343041982dSJonathan Peyton kmp_task_t *__kmp_task_alloc(ident_t *loc_ref, kmp_int32 gtid,
9353041982dSJonathan Peyton                              kmp_tasking_flags_t *flags,
9367cc577a4SJonathan Peyton                              size_t sizeof_kmp_task_t, size_t sizeof_shareds,
9373041982dSJonathan Peyton                              kmp_routine_entry_t task_entry) {
9387cc577a4SJonathan Peyton   kmp_task_t *task;
9397cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata;
9407cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
9417cc577a4SJonathan Peyton   kmp_team_t *team = thread->th.th_team;
9427cc577a4SJonathan Peyton   kmp_taskdata_t *parent_task = thread->th.th_current_task;
9437cc577a4SJonathan Peyton   size_t shareds_offset;
9447cc577a4SJonathan Peyton 
9457cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) "
9467cc577a4SJonathan Peyton                 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
9477cc577a4SJonathan Peyton                 gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t,
9487cc577a4SJonathan Peyton                 sizeof_shareds, task_entry));
9497cc577a4SJonathan Peyton 
9507cc577a4SJonathan Peyton   if (parent_task->td_flags.final) {
9517cc577a4SJonathan Peyton     if (flags->merged_if0) {
9527cc577a4SJonathan Peyton     }
9537cc577a4SJonathan Peyton     flags->final = 1;
9547cc577a4SJonathan Peyton   }
9557cc577a4SJonathan Peyton 
9567cc577a4SJonathan Peyton #if OMP_45_ENABLED
9577cc577a4SJonathan Peyton   if (flags->proxy == TASK_PROXY) {
9587cc577a4SJonathan Peyton     flags->tiedness = TASK_UNTIED;
9597cc577a4SJonathan Peyton     flags->merged_if0 = 1;
9607cc577a4SJonathan Peyton 
9613041982dSJonathan Peyton     /* are we running in a sequential parallel or tskm_immediate_exec... we need
9623041982dSJonathan Peyton        tasking support enabled */
9637cc577a4SJonathan Peyton     if ((thread->th.th_task_team) == NULL) {
9647cc577a4SJonathan Peyton       /* This should only happen if the team is serialized
9653041982dSJonathan Peyton           setup a task team and propagate it to the thread */
9667cc577a4SJonathan Peyton       KMP_DEBUG_ASSERT(team->t.t_serialized);
9673041982dSJonathan Peyton       KA_TRACE(30,
9683041982dSJonathan Peyton                ("T#%d creating task team in __kmp_task_alloc for proxy task\n",
9693041982dSJonathan Peyton                 gtid));
9703041982dSJonathan Peyton       __kmp_task_team_setup(
9713041982dSJonathan Peyton           thread, team,
9723041982dSJonathan Peyton           1); // 1 indicates setup the current team regardless of nthreads
9737cc577a4SJonathan Peyton       thread->th.th_task_team = team->t.t_task_team[thread->th.th_task_state];
9747cc577a4SJonathan Peyton     }
9757cc577a4SJonathan Peyton     kmp_task_team_t *task_team = thread->th.th_task_team;
9767cc577a4SJonathan Peyton 
9777cc577a4SJonathan Peyton     /* tasking must be enabled now as the task might not be pushed */
9787cc577a4SJonathan Peyton     if (!KMP_TASKING_ENABLED(task_team)) {
9793041982dSJonathan Peyton       KA_TRACE(
9803041982dSJonathan Peyton           30,
9813041982dSJonathan Peyton           ("T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid));
9827cc577a4SJonathan Peyton       __kmp_enable_tasking(task_team, thread);
9837cc577a4SJonathan Peyton       kmp_int32 tid = thread->th.th_info.ds.ds_tid;
9847cc577a4SJonathan Peyton       kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
9857cc577a4SJonathan Peyton       // No lock needed since only owner can allocate
9867cc577a4SJonathan Peyton       if (thread_data->td.td_deque == NULL) {
9877cc577a4SJonathan Peyton         __kmp_alloc_task_deque(thread, thread_data);
9887cc577a4SJonathan Peyton       }
9897cc577a4SJonathan Peyton     }
9907cc577a4SJonathan Peyton 
9917cc577a4SJonathan Peyton     if (task_team->tt.tt_found_proxy_tasks == FALSE)
9927cc577a4SJonathan Peyton       TCW_4(task_team->tt.tt_found_proxy_tasks, TRUE);
9937cc577a4SJonathan Peyton   }
9947cc577a4SJonathan Peyton #endif
9957cc577a4SJonathan Peyton 
9967cc577a4SJonathan Peyton   // Calculate shared structure offset including padding after kmp_task_t struct
9977cc577a4SJonathan Peyton   // to align pointers in shared struct
9987cc577a4SJonathan Peyton   shareds_offset = sizeof(kmp_taskdata_t) + sizeof_kmp_task_t;
9997cc577a4SJonathan Peyton   shareds_offset = __kmp_round_up_to_val(shareds_offset, sizeof(void *));
10007cc577a4SJonathan Peyton 
10017cc577a4SJonathan Peyton   // Allocate a kmp_taskdata_t block and a kmp_task_t block.
10023041982dSJonathan Peyton   KA_TRACE(30, ("__kmp_task_alloc: T#%d First malloc size: %ld\n", gtid,
10033041982dSJonathan Peyton                 shareds_offset));
10043041982dSJonathan Peyton   KA_TRACE(30, ("__kmp_task_alloc: T#%d Second malloc size: %ld\n", gtid,
10053041982dSJonathan Peyton                 sizeof_shareds));
10067cc577a4SJonathan Peyton 
10077cc577a4SJonathan Peyton // Avoid double allocation here by combining shareds with taskdata
10087cc577a4SJonathan Peyton #if USE_FAST_MEMORY
10093041982dSJonathan Peyton   taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, shareds_offset +
10103041982dSJonathan Peyton                                                                sizeof_shareds);
10117cc577a4SJonathan Peyton #else /* ! USE_FAST_MEMORY */
10123041982dSJonathan Peyton   taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, shareds_offset +
10133041982dSJonathan Peyton                                                                sizeof_shareds);
10147cc577a4SJonathan Peyton #endif /* USE_FAST_MEMORY */
10157cc577a4SJonathan Peyton   ANNOTATE_HAPPENS_AFTER(taskdata);
10167cc577a4SJonathan Peyton 
10177cc577a4SJonathan Peyton   task = KMP_TASKDATA_TO_TASK(taskdata);
10187cc577a4SJonathan Peyton 
10197cc577a4SJonathan Peyton // Make sure task & taskdata are aligned appropriately
10207cc577a4SJonathan Peyton #if KMP_ARCH_X86 || KMP_ARCH_PPC64 || !KMP_HAVE_QUAD
10217cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (sizeof(double) - 1)) == 0);
10227cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (sizeof(double) - 1)) == 0);
10237cc577a4SJonathan Peyton #else
10247cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (sizeof(_Quad) - 1)) == 0);
10257cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (sizeof(_Quad) - 1)) == 0);
10267cc577a4SJonathan Peyton #endif
10277cc577a4SJonathan Peyton   if (sizeof_shareds > 0) {
10287cc577a4SJonathan Peyton     // Avoid double allocation here by combining shareds with taskdata
10297cc577a4SJonathan Peyton     task->shareds = &((char *)taskdata)[shareds_offset];
10307cc577a4SJonathan Peyton     // Make sure shareds struct is aligned to pointer size
10313041982dSJonathan Peyton     KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (sizeof(void *) - 1)) ==
10323041982dSJonathan Peyton                      0);
10337cc577a4SJonathan Peyton   } else {
10347cc577a4SJonathan Peyton     task->shareds = NULL;
10357cc577a4SJonathan Peyton   }
10367cc577a4SJonathan Peyton   task->routine = task_entry;
10377cc577a4SJonathan Peyton   task->part_id = 0; // AC: Always start with 0 part id
10387cc577a4SJonathan Peyton 
10397cc577a4SJonathan Peyton   taskdata->td_task_id = KMP_GEN_TASK_ID();
10407cc577a4SJonathan Peyton   taskdata->td_team = team;
10417cc577a4SJonathan Peyton   taskdata->td_alloc_thread = thread;
10427cc577a4SJonathan Peyton   taskdata->td_parent = parent_task;
10437cc577a4SJonathan Peyton   taskdata->td_level = parent_task->td_level + 1; // increment nesting level
10447cc577a4SJonathan Peyton   taskdata->td_untied_count = 0;
10457cc577a4SJonathan Peyton   taskdata->td_ident = loc_ref;
10467cc577a4SJonathan Peyton   taskdata->td_taskwait_ident = NULL;
10477cc577a4SJonathan Peyton   taskdata->td_taskwait_counter = 0;
10487cc577a4SJonathan Peyton   taskdata->td_taskwait_thread = 0;
10497cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_parent != NULL);
10507cc577a4SJonathan Peyton #if OMP_45_ENABLED
10517cc577a4SJonathan Peyton   // avoid copying icvs for proxy tasks
10527cc577a4SJonathan Peyton   if (flags->proxy == TASK_FULL)
10537cc577a4SJonathan Peyton #endif
10547cc577a4SJonathan Peyton     copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs);
10557cc577a4SJonathan Peyton 
10567cc577a4SJonathan Peyton   taskdata->td_flags.tiedness = flags->tiedness;
10577cc577a4SJonathan Peyton   taskdata->td_flags.final = flags->final;
10587cc577a4SJonathan Peyton   taskdata->td_flags.merged_if0 = flags->merged_if0;
10597cc577a4SJonathan Peyton #if OMP_40_ENABLED
10607cc577a4SJonathan Peyton   taskdata->td_flags.destructors_thunk = flags->destructors_thunk;
10617cc577a4SJonathan Peyton #endif // OMP_40_ENABLED
10627cc577a4SJonathan Peyton #if OMP_45_ENABLED
10637cc577a4SJonathan Peyton   taskdata->td_flags.proxy = flags->proxy;
10647cc577a4SJonathan Peyton   taskdata->td_task_team = thread->th.th_task_team;
10657cc577a4SJonathan Peyton   taskdata->td_size_alloc = shareds_offset + sizeof_shareds;
10667cc577a4SJonathan Peyton #endif
10677cc577a4SJonathan Peyton   taskdata->td_flags.tasktype = TASK_EXPLICIT;
10687cc577a4SJonathan Peyton 
10697cc577a4SJonathan Peyton   // GEH - TODO: fix this to copy parent task's value of tasking_ser flag
10707cc577a4SJonathan Peyton   taskdata->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
10717cc577a4SJonathan Peyton 
10727cc577a4SJonathan Peyton   // GEH - TODO: fix this to copy parent task's value of team_serial flag
10737cc577a4SJonathan Peyton   taskdata->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
10747cc577a4SJonathan Peyton 
10753041982dSJonathan Peyton   // GEH - Note we serialize the task if the team is serialized to make sure
10763041982dSJonathan Peyton   // implicit parallel region tasks are not left until program termination to
10773041982dSJonathan Peyton   // execute. Also, it helps locality to execute immediately.
10783041982dSJonathan Peyton 
10793041982dSJonathan Peyton   taskdata->td_flags.task_serial =
10803041982dSJonathan Peyton       (parent_task->td_flags.final || taskdata->td_flags.team_serial ||
10813041982dSJonathan Peyton        taskdata->td_flags.tasking_ser);
10827cc577a4SJonathan Peyton 
10837cc577a4SJonathan Peyton   taskdata->td_flags.started = 0;
10847cc577a4SJonathan Peyton   taskdata->td_flags.executing = 0;
10857cc577a4SJonathan Peyton   taskdata->td_flags.complete = 0;
10867cc577a4SJonathan Peyton   taskdata->td_flags.freed = 0;
10877cc577a4SJonathan Peyton 
10887cc577a4SJonathan Peyton   taskdata->td_flags.native = flags->native;
10897cc577a4SJonathan Peyton 
10907cc577a4SJonathan Peyton   taskdata->td_incomplete_child_tasks = 0;
10913041982dSJonathan Peyton   taskdata->td_allocated_child_tasks = 1; // start at one because counts current
10923041982dSJonathan Peyton // task and children
10937cc577a4SJonathan Peyton #if OMP_40_ENABLED
10943041982dSJonathan Peyton   taskdata->td_taskgroup =
10953041982dSJonathan Peyton       parent_task->td_taskgroup; // task inherits taskgroup from the parent task
10967cc577a4SJonathan Peyton   taskdata->td_dephash = NULL;
10977cc577a4SJonathan Peyton   taskdata->td_depnode = NULL;
10987cc577a4SJonathan Peyton #endif
10997cc577a4SJonathan Peyton 
11003041982dSJonathan Peyton // Only need to keep track of child task counts if team parallel and tasking not
11013041982dSJonathan Peyton // serialized or if it is a proxy task
11027cc577a4SJonathan Peyton #if OMP_45_ENABLED
11033041982dSJonathan Peyton   if (flags->proxy == TASK_PROXY ||
11043041982dSJonathan Peyton       !(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser))
11057cc577a4SJonathan Peyton #else
11067cc577a4SJonathan Peyton   if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser))
11077cc577a4SJonathan Peyton #endif
11087cc577a4SJonathan Peyton   {
11095ba90c79SAndrey Churbanov     KMP_TEST_THEN_INC32(&parent_task->td_incomplete_child_tasks);
11107cc577a4SJonathan Peyton #if OMP_40_ENABLED
11117cc577a4SJonathan Peyton     if (parent_task->td_taskgroup)
11127cc577a4SJonathan Peyton       KMP_TEST_THEN_INC32((kmp_int32 *)(&parent_task->td_taskgroup->count));
11137cc577a4SJonathan Peyton #endif
11143041982dSJonathan Peyton     // Only need to keep track of allocated child tasks for explicit tasks since
11153041982dSJonathan Peyton     // implicit not deallocated
11167cc577a4SJonathan Peyton     if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) {
11175ba90c79SAndrey Churbanov       KMP_TEST_THEN_INC32(&taskdata->td_parent->td_allocated_child_tasks);
11187cc577a4SJonathan Peyton     }
11197cc577a4SJonathan Peyton   }
11207cc577a4SJonathan Peyton 
11217cc577a4SJonathan Peyton   KA_TRACE(20, ("__kmp_task_alloc(exit): T#%d created task %p parent=%p\n",
11227cc577a4SJonathan Peyton                 gtid, taskdata, taskdata->td_parent));
11237cc577a4SJonathan Peyton   ANNOTATE_HAPPENS_BEFORE(task);
11247cc577a4SJonathan Peyton 
11257cc577a4SJonathan Peyton #if OMPT_SUPPORT
11267cc577a4SJonathan Peyton   __kmp_task_init_ompt(taskdata, gtid, (void *)task_entry);
11277cc577a4SJonathan Peyton #endif
11287cc577a4SJonathan Peyton 
11297cc577a4SJonathan Peyton   return task;
11307cc577a4SJonathan Peyton }
11317cc577a4SJonathan Peyton 
11323041982dSJonathan Peyton kmp_task_t *__kmpc_omp_task_alloc(ident_t *loc_ref, kmp_int32 gtid,
11333041982dSJonathan Peyton                                   kmp_int32 flags, size_t sizeof_kmp_task_t,
11343041982dSJonathan Peyton                                   size_t sizeof_shareds,
11353041982dSJonathan Peyton                                   kmp_routine_entry_t task_entry) {
11367cc577a4SJonathan Peyton   kmp_task_t *retval;
11377cc577a4SJonathan Peyton   kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
11387cc577a4SJonathan Peyton 
11397cc577a4SJonathan Peyton   input_flags->native = FALSE;
11407cc577a4SJonathan Peyton // __kmp_task_alloc() sets up all other runtime flags
11417cc577a4SJonathan Peyton 
11427cc577a4SJonathan Peyton #if OMP_45_ENABLED
11437cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s) "
11447cc577a4SJonathan Peyton                 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
11457cc577a4SJonathan Peyton                 gtid, loc_ref, input_flags->tiedness ? "tied  " : "untied",
11463041982dSJonathan Peyton                 input_flags->proxy ? "proxy" : "", sizeof_kmp_task_t,
11473041982dSJonathan Peyton                 sizeof_shareds, task_entry));
11487cc577a4SJonathan Peyton #else
11497cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s) "
11507cc577a4SJonathan Peyton                 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
11517cc577a4SJonathan Peyton                 gtid, loc_ref, input_flags->tiedness ? "tied  " : "untied",
11527cc577a4SJonathan Peyton                 sizeof_kmp_task_t, sizeof_shareds, task_entry));
11537cc577a4SJonathan Peyton #endif
11547cc577a4SJonathan Peyton 
11557cc577a4SJonathan Peyton   retval = __kmp_task_alloc(loc_ref, gtid, input_flags, sizeof_kmp_task_t,
11567cc577a4SJonathan Peyton                             sizeof_shareds, task_entry);
11577cc577a4SJonathan Peyton 
11587cc577a4SJonathan Peyton   KA_TRACE(20, ("__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval));
11597cc577a4SJonathan Peyton 
11607cc577a4SJonathan Peyton   return retval;
11617cc577a4SJonathan Peyton }
11627cc577a4SJonathan Peyton 
11637cc577a4SJonathan Peyton //  __kmp_invoke_task: invoke the specified task
11647cc577a4SJonathan Peyton //
11657cc577a4SJonathan Peyton // gtid: global thread ID of caller
11667cc577a4SJonathan Peyton // task: the task to invoke
11677cc577a4SJonathan Peyton // current_task: the task to resume after task invokation
11683041982dSJonathan Peyton static void __kmp_invoke_task(kmp_int32 gtid, kmp_task_t *task,
11693041982dSJonathan Peyton                               kmp_taskdata_t *current_task) {
11707cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
11717cc577a4SJonathan Peyton   kmp_uint64 cur_time;
11727cc577a4SJonathan Peyton #if OMP_40_ENABLED
11737cc577a4SJonathan Peyton   int discard = 0 /* false */;
11747cc577a4SJonathan Peyton #endif
11753041982dSJonathan Peyton   KA_TRACE(
11763041982dSJonathan Peyton       30, ("__kmp_invoke_task(enter): T#%d invoking task %p, current_task=%p\n",
11777cc577a4SJonathan Peyton            gtid, taskdata, current_task));
11787cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task);
11797cc577a4SJonathan Peyton #if OMP_45_ENABLED
11807cc577a4SJonathan Peyton   if (taskdata->td_flags.proxy == TASK_PROXY &&
11813041982dSJonathan Peyton       taskdata->td_flags.complete == 1) {
11827cc577a4SJonathan Peyton     // This is a proxy task that was already completed but it needs to run
11837cc577a4SJonathan Peyton     // its bottom-half finish
11843041982dSJonathan Peyton     KA_TRACE(
11853041982dSJonathan Peyton         30,
11863041982dSJonathan Peyton         ("__kmp_invoke_task: T#%d running bottom finish for proxy task %p\n",
11877cc577a4SJonathan Peyton          gtid, taskdata));
11887cc577a4SJonathan Peyton 
11897cc577a4SJonathan Peyton     __kmp_bottom_half_finish_proxy(gtid, task);
11907cc577a4SJonathan Peyton 
11913041982dSJonathan Peyton     KA_TRACE(30, ("__kmp_invoke_task(exit): T#%d completed bottom finish for "
11923041982dSJonathan Peyton                   "proxy task %p, resuming task %p\n",
11933041982dSJonathan Peyton                   gtid, taskdata, current_task));
11947cc577a4SJonathan Peyton 
11957cc577a4SJonathan Peyton     return;
11967cc577a4SJonathan Peyton   }
11977cc577a4SJonathan Peyton #endif
11987cc577a4SJonathan Peyton 
11997cc577a4SJonathan Peyton #if USE_ITT_BUILD && USE_ITT_NOTIFY
12007cc577a4SJonathan Peyton   if (__kmp_forkjoin_frames_mode == 3) {
12013041982dSJonathan Peyton     // Get the current time stamp to measure task execution time to correct
12023041982dSJonathan Peyton     // barrier imbalance time
12037cc577a4SJonathan Peyton     cur_time = __itt_get_timestamp();
12047cc577a4SJonathan Peyton   }
12057cc577a4SJonathan Peyton #endif
12067cc577a4SJonathan Peyton 
12077cc577a4SJonathan Peyton #if OMP_45_ENABLED
12087cc577a4SJonathan Peyton   // Proxy tasks are not handled by the runtime
12097cc577a4SJonathan Peyton   if (taskdata->td_flags.proxy != TASK_PROXY) {
12107cc577a4SJonathan Peyton #endif
12117cc577a4SJonathan Peyton     ANNOTATE_HAPPENS_AFTER(task);
12127cc577a4SJonathan Peyton     __kmp_task_start(gtid, task, current_task);
12137cc577a4SJonathan Peyton #if OMP_45_ENABLED
12147cc577a4SJonathan Peyton   }
12157cc577a4SJonathan Peyton #endif
12167cc577a4SJonathan Peyton 
12177cc577a4SJonathan Peyton #if OMPT_SUPPORT
12187cc577a4SJonathan Peyton   ompt_thread_info_t oldInfo;
12197cc577a4SJonathan Peyton   kmp_info_t *thread;
12207cc577a4SJonathan Peyton   if (ompt_enabled) {
12217cc577a4SJonathan Peyton     // Store the threads states and restore them after the task
12227cc577a4SJonathan Peyton     thread = __kmp_threads[gtid];
12237cc577a4SJonathan Peyton     oldInfo = thread->th.ompt_thread_info;
12247cc577a4SJonathan Peyton     thread->th.ompt_thread_info.wait_id = 0;
12257cc577a4SJonathan Peyton     thread->th.ompt_thread_info.state = ompt_state_work_parallel;
12263041982dSJonathan Peyton     taskdata->ompt_task_info.frame.exit_runtime_frame =
12273041982dSJonathan Peyton         __builtin_frame_address(0);
12287cc577a4SJonathan Peyton   }
12297cc577a4SJonathan Peyton #endif
12307cc577a4SJonathan Peyton 
12317cc577a4SJonathan Peyton #if OMP_40_ENABLED
12327cc577a4SJonathan Peyton   // TODO: cancel tasks if the parallel region has also been cancelled
12337cc577a4SJonathan Peyton   // TODO: check if this sequence can be hoisted above __kmp_task_start
12347cc577a4SJonathan Peyton   // if cancellation has been enabled for this run ...
12357cc577a4SJonathan Peyton   if (__kmp_omp_cancellation) {
12367cc577a4SJonathan Peyton     kmp_info_t *this_thr = __kmp_threads[gtid];
12377cc577a4SJonathan Peyton     kmp_team_t *this_team = this_thr->th.th_team;
12387cc577a4SJonathan Peyton     kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
12393041982dSJonathan Peyton     if ((taskgroup && taskgroup->cancel_request) ||
12403041982dSJonathan Peyton         (this_team->t.t_cancel_request == cancel_parallel)) {
12417cc577a4SJonathan Peyton       KMP_COUNT_BLOCK(TASK_cancelled);
12427cc577a4SJonathan Peyton       // this task belongs to a task group and we need to cancel it
12437cc577a4SJonathan Peyton       discard = 1 /* true */;
12447cc577a4SJonathan Peyton     }
12457cc577a4SJonathan Peyton   }
12467cc577a4SJonathan Peyton 
12477cc577a4SJonathan Peyton   // Invoke the task routine and pass in relevant data.
12487cc577a4SJonathan Peyton   // Thunks generated by gcc take a different argument list.
12497cc577a4SJonathan Peyton   if (!discard) {
12507cc577a4SJonathan Peyton #if KMP_STATS_ENABLED
12517cc577a4SJonathan Peyton     KMP_COUNT_BLOCK(TASK_executed);
12527cc577a4SJonathan Peyton     switch (KMP_GET_THREAD_STATE()) {
12533041982dSJonathan Peyton     case FORK_JOIN_BARRIER:
12543041982dSJonathan Peyton       KMP_PUSH_PARTITIONED_TIMER(OMP_task_join_bar);
12553041982dSJonathan Peyton       break;
12563041982dSJonathan Peyton     case PLAIN_BARRIER:
12573041982dSJonathan Peyton       KMP_PUSH_PARTITIONED_TIMER(OMP_task_plain_bar);
12583041982dSJonathan Peyton       break;
12593041982dSJonathan Peyton     case TASKYIELD:
12603041982dSJonathan Peyton       KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskyield);
12613041982dSJonathan Peyton       break;
12623041982dSJonathan Peyton     case TASKWAIT:
12633041982dSJonathan Peyton       KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskwait);
12643041982dSJonathan Peyton       break;
12653041982dSJonathan Peyton     case TASKGROUP:
12663041982dSJonathan Peyton       KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskgroup);
12673041982dSJonathan Peyton       break;
12683041982dSJonathan Peyton     default:
12693041982dSJonathan Peyton       KMP_PUSH_PARTITIONED_TIMER(OMP_task_immediate);
12703041982dSJonathan Peyton       break;
12717cc577a4SJonathan Peyton     }
12727cc577a4SJonathan Peyton #endif // KMP_STATS_ENABLED
12737cc577a4SJonathan Peyton #endif // OMP_40_ENABLED
12747cc577a4SJonathan Peyton 
12757cc577a4SJonathan Peyton #if OMPT_SUPPORT && OMPT_TRACE
12767cc577a4SJonathan Peyton     /* let OMPT know that we're about to run this task */
12773041982dSJonathan Peyton     if (ompt_enabled && ompt_callbacks.ompt_callback(ompt_event_task_switch)) {
12787cc577a4SJonathan Peyton       ompt_callbacks.ompt_callback(ompt_event_task_switch)(
12797cc577a4SJonathan Peyton           current_task->ompt_task_info.task_id,
12807cc577a4SJonathan Peyton           taskdata->ompt_task_info.task_id);
12817cc577a4SJonathan Peyton     }
12827cc577a4SJonathan Peyton #endif
12837cc577a4SJonathan Peyton 
12847cc577a4SJonathan Peyton #ifdef KMP_GOMP_COMPAT
12857cc577a4SJonathan Peyton     if (taskdata->td_flags.native) {
12867cc577a4SJonathan Peyton       ((void (*)(void *))(*(task->routine)))(task->shareds);
12873041982dSJonathan Peyton     } else
12887cc577a4SJonathan Peyton #endif /* KMP_GOMP_COMPAT */
12897cc577a4SJonathan Peyton     {
12907cc577a4SJonathan Peyton       (*(task->routine))(gtid, task);
12917cc577a4SJonathan Peyton     }
12927cc577a4SJonathan Peyton     KMP_POP_PARTITIONED_TIMER();
12937cc577a4SJonathan Peyton 
12947cc577a4SJonathan Peyton #if OMPT_SUPPORT && OMPT_TRACE
12957cc577a4SJonathan Peyton     /* let OMPT know that we're returning to the callee task */
12963041982dSJonathan Peyton     if (ompt_enabled && ompt_callbacks.ompt_callback(ompt_event_task_switch)) {
12977cc577a4SJonathan Peyton       ompt_callbacks.ompt_callback(ompt_event_task_switch)(
12987cc577a4SJonathan Peyton           taskdata->ompt_task_info.task_id,
12997cc577a4SJonathan Peyton           current_task->ompt_task_info.task_id);
13007cc577a4SJonathan Peyton     }
13017cc577a4SJonathan Peyton #endif
13027cc577a4SJonathan Peyton 
13037cc577a4SJonathan Peyton #if OMP_40_ENABLED
13047cc577a4SJonathan Peyton   }
13057cc577a4SJonathan Peyton #endif // OMP_40_ENABLED
13067cc577a4SJonathan Peyton 
13077cc577a4SJonathan Peyton #if OMPT_SUPPORT
13087cc577a4SJonathan Peyton   if (ompt_enabled) {
13097cc577a4SJonathan Peyton     thread->th.ompt_thread_info = oldInfo;
13107cc577a4SJonathan Peyton     taskdata->ompt_task_info.frame.exit_runtime_frame = NULL;
13117cc577a4SJonathan Peyton   }
13127cc577a4SJonathan Peyton #endif
13137cc577a4SJonathan Peyton 
13147cc577a4SJonathan Peyton #if OMP_45_ENABLED
13157cc577a4SJonathan Peyton   // Proxy tasks are not handled by the runtime
13167cc577a4SJonathan Peyton   if (taskdata->td_flags.proxy != TASK_PROXY) {
13177cc577a4SJonathan Peyton #endif
13187cc577a4SJonathan Peyton     ANNOTATE_HAPPENS_BEFORE(taskdata->td_parent);
13197cc577a4SJonathan Peyton     __kmp_task_finish(gtid, task, current_task);
13207cc577a4SJonathan Peyton #if OMP_45_ENABLED
13217cc577a4SJonathan Peyton   }
13227cc577a4SJonathan Peyton #endif
13237cc577a4SJonathan Peyton 
13247cc577a4SJonathan Peyton #if USE_ITT_BUILD && USE_ITT_NOTIFY
13257cc577a4SJonathan Peyton   // Barrier imbalance - correct arrive time after the task finished
13267cc577a4SJonathan Peyton   if (__kmp_forkjoin_frames_mode == 3) {
13277cc577a4SJonathan Peyton     kmp_info_t *this_thr = __kmp_threads[gtid];
13287cc577a4SJonathan Peyton     if (this_thr->th.th_bar_arrive_time) {
13297cc577a4SJonathan Peyton       this_thr->th.th_bar_arrive_time += (__itt_get_timestamp() - cur_time);
13307cc577a4SJonathan Peyton     }
13317cc577a4SJonathan Peyton   }
13327cc577a4SJonathan Peyton #endif
13333041982dSJonathan Peyton   KA_TRACE(
13343041982dSJonathan Peyton       30,
13353041982dSJonathan Peyton       ("__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n",
13367cc577a4SJonathan Peyton        gtid, taskdata, current_task));
13377cc577a4SJonathan Peyton   return;
13387cc577a4SJonathan Peyton }
13397cc577a4SJonathan Peyton 
13407cc577a4SJonathan Peyton // __kmpc_omp_task_parts: Schedule a thread-switchable task for execution
13417cc577a4SJonathan Peyton //
13427cc577a4SJonathan Peyton // loc_ref: location of original task pragma (ignored)
13437cc577a4SJonathan Peyton // gtid: Global Thread ID of encountering thread
13447cc577a4SJonathan Peyton // new_task: task thunk allocated by __kmp_omp_task_alloc() for the ''new task''
13457cc577a4SJonathan Peyton // Returns:
13463041982dSJonathan Peyton //    TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to
13473041982dSJonathan Peyton //    be resumed later.
13483041982dSJonathan Peyton //    TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be
13493041982dSJonathan Peyton //    resumed later.
13503041982dSJonathan Peyton kmp_int32 __kmpc_omp_task_parts(ident_t *loc_ref, kmp_int32 gtid,
13513041982dSJonathan Peyton                                 kmp_task_t *new_task) {
13527cc577a4SJonathan Peyton   kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
13537cc577a4SJonathan Peyton 
13543041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n", gtid,
13553041982dSJonathan Peyton                 loc_ref, new_taskdata));
13567cc577a4SJonathan Peyton 
13573041982dSJonathan Peyton   /* Should we execute the new task or queue it? For now, let's just always try
13583041982dSJonathan Peyton      to queue it.  If the queue fills up, then we'll execute it.  */
13597cc577a4SJonathan Peyton 
13607cc577a4SJonathan Peyton   if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED) // if cannot defer
13617cc577a4SJonathan Peyton   { // Execute this task immediately
13627cc577a4SJonathan Peyton     kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
13637cc577a4SJonathan Peyton     new_taskdata->td_flags.task_serial = 1;
13647cc577a4SJonathan Peyton     __kmp_invoke_task(gtid, new_task, current_task);
13657cc577a4SJonathan Peyton   }
13667cc577a4SJonathan Peyton 
13673041982dSJonathan Peyton   KA_TRACE(
13683041982dSJonathan Peyton       10,
13693041982dSJonathan Peyton       ("__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: "
13703041982dSJonathan Peyton        "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
13713041982dSJonathan Peyton        gtid, loc_ref, new_taskdata));
13727cc577a4SJonathan Peyton 
13737cc577a4SJonathan Peyton   ANNOTATE_HAPPENS_BEFORE(new_task);
13747cc577a4SJonathan Peyton   return TASK_CURRENT_NOT_QUEUED;
13757cc577a4SJonathan Peyton }
13767cc577a4SJonathan Peyton 
13777cc577a4SJonathan Peyton // __kmp_omp_task: Schedule a non-thread-switchable task for execution
13783041982dSJonathan Peyton //
13797cc577a4SJonathan Peyton // gtid: Global Thread ID of encountering thread
13807cc577a4SJonathan Peyton // new_task:non-thread-switchable task thunk allocated by __kmp_omp_task_alloc()
13813041982dSJonathan Peyton // serialize_immediate: if TRUE then if the task is executed immediately its
13823041982dSJonathan Peyton // execution will be serialized
13833041982dSJonathan Peyton // Returns:
13843041982dSJonathan Peyton //    TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to
13853041982dSJonathan Peyton //    be resumed later.
13863041982dSJonathan Peyton //    TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be
13873041982dSJonathan Peyton //    resumed later.
13883041982dSJonathan Peyton kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task,
13893041982dSJonathan Peyton                          bool serialize_immediate) {
13907cc577a4SJonathan Peyton   kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
13917cc577a4SJonathan Peyton 
13927cc577a4SJonathan Peyton #if OMPT_SUPPORT
13937cc577a4SJonathan Peyton   if (ompt_enabled) {
13947cc577a4SJonathan Peyton     new_taskdata->ompt_task_info.frame.reenter_runtime_frame =
13957cc577a4SJonathan Peyton         __builtin_frame_address(1);
13967cc577a4SJonathan Peyton   }
13977cc577a4SJonathan Peyton #endif
13987cc577a4SJonathan Peyton 
13997cc577a4SJonathan Peyton /* Should we execute the new task or queue it? For now, let's just always try to
14007cc577a4SJonathan Peyton    queue it.  If the queue fills up, then we'll execute it.  */
14017cc577a4SJonathan Peyton #if OMP_45_ENABLED
14023041982dSJonathan Peyton   if (new_taskdata->td_flags.proxy == TASK_PROXY ||
14033041982dSJonathan Peyton       __kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED) // if cannot defer
14047cc577a4SJonathan Peyton #else
14057cc577a4SJonathan Peyton   if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED) // if cannot defer
14067cc577a4SJonathan Peyton #endif
14077cc577a4SJonathan Peyton   { // Execute this task immediately
14087cc577a4SJonathan Peyton     kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
14097cc577a4SJonathan Peyton     if (serialize_immediate)
14107cc577a4SJonathan Peyton       new_taskdata->td_flags.task_serial = 1;
14117cc577a4SJonathan Peyton     __kmp_invoke_task(gtid, new_task, current_task);
14127cc577a4SJonathan Peyton   }
14137cc577a4SJonathan Peyton 
14147cc577a4SJonathan Peyton #if OMPT_SUPPORT
14157cc577a4SJonathan Peyton   if (ompt_enabled) {
14167cc577a4SJonathan Peyton     new_taskdata->ompt_task_info.frame.reenter_runtime_frame = NULL;
14177cc577a4SJonathan Peyton   }
14187cc577a4SJonathan Peyton #endif
14197cc577a4SJonathan Peyton 
14207cc577a4SJonathan Peyton   ANNOTATE_HAPPENS_BEFORE(new_task);
14217cc577a4SJonathan Peyton   return TASK_CURRENT_NOT_QUEUED;
14227cc577a4SJonathan Peyton }
14237cc577a4SJonathan Peyton 
14243041982dSJonathan Peyton // __kmpc_omp_task: Wrapper around __kmp_omp_task to schedule a
14253041982dSJonathan Peyton // non-thread-switchable task from the parent thread only!
14263041982dSJonathan Peyton //
14277cc577a4SJonathan Peyton // loc_ref: location of original task pragma (ignored)
14287cc577a4SJonathan Peyton // gtid: Global Thread ID of encountering thread
14293041982dSJonathan Peyton // new_task: non-thread-switchable task thunk allocated by
14303041982dSJonathan Peyton // __kmp_omp_task_alloc()
14313041982dSJonathan Peyton // Returns:
14323041982dSJonathan Peyton //    TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to
14333041982dSJonathan Peyton //    be resumed later.
14343041982dSJonathan Peyton //    TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be
14353041982dSJonathan Peyton //    resumed later.
14363041982dSJonathan Peyton kmp_int32 __kmpc_omp_task(ident_t *loc_ref, kmp_int32 gtid,
14373041982dSJonathan Peyton                           kmp_task_t *new_task) {
14387cc577a4SJonathan Peyton   kmp_int32 res;
14397cc577a4SJonathan Peyton   KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
14407cc577a4SJonathan Peyton 
14417cc577a4SJonathan Peyton #if KMP_DEBUG
14427cc577a4SJonathan Peyton   kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
14437cc577a4SJonathan Peyton #endif
14443041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
14453041982dSJonathan Peyton                 new_taskdata));
14467cc577a4SJonathan Peyton 
14477cc577a4SJonathan Peyton   res = __kmp_omp_task(gtid, new_task, true);
14487cc577a4SJonathan Peyton 
14493041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task(exit): T#%d returning "
14503041982dSJonathan Peyton                 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
14517cc577a4SJonathan Peyton                 gtid, loc_ref, new_taskdata));
14527cc577a4SJonathan Peyton   return res;
14537cc577a4SJonathan Peyton }
14547cc577a4SJonathan Peyton 
14553041982dSJonathan Peyton // __kmpc_omp_taskwait: Wait until all tasks generated by the current task are
14563041982dSJonathan Peyton // complete
14573041982dSJonathan Peyton kmp_int32 __kmpc_omp_taskwait(ident_t *loc_ref, kmp_int32 gtid) {
14587cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata;
14597cc577a4SJonathan Peyton   kmp_info_t *thread;
14607cc577a4SJonathan Peyton   int thread_finished = FALSE;
14617cc577a4SJonathan Peyton   KMP_SET_THREAD_STATE_BLOCK(TASKWAIT);
14627cc577a4SJonathan Peyton 
14637cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref));
14647cc577a4SJonathan Peyton 
14657cc577a4SJonathan Peyton   if (__kmp_tasking_mode != tskm_immediate_exec) {
14667cc577a4SJonathan Peyton     thread = __kmp_threads[gtid];
14677cc577a4SJonathan Peyton     taskdata = thread->th.th_current_task;
14687cc577a4SJonathan Peyton #if OMPT_SUPPORT && OMPT_TRACE
14697cc577a4SJonathan Peyton     ompt_task_id_t my_task_id;
14707cc577a4SJonathan Peyton     ompt_parallel_id_t my_parallel_id;
14717cc577a4SJonathan Peyton 
14727cc577a4SJonathan Peyton     if (ompt_enabled) {
14737cc577a4SJonathan Peyton       kmp_team_t *team = thread->th.th_team;
14747cc577a4SJonathan Peyton       my_task_id = taskdata->ompt_task_info.task_id;
14757cc577a4SJonathan Peyton       my_parallel_id = team->t.ompt_team_info.parallel_id;
14767cc577a4SJonathan Peyton 
14773041982dSJonathan Peyton       taskdata->ompt_task_info.frame.reenter_runtime_frame =
14783041982dSJonathan Peyton           __builtin_frame_address(1);
14797cc577a4SJonathan Peyton       if (ompt_callbacks.ompt_callback(ompt_event_taskwait_begin)) {
14803041982dSJonathan Peyton         ompt_callbacks.ompt_callback(ompt_event_taskwait_begin)(my_parallel_id,
14813041982dSJonathan Peyton                                                                 my_task_id);
14827cc577a4SJonathan Peyton       }
14837cc577a4SJonathan Peyton     }
14847cc577a4SJonathan Peyton #endif
14857cc577a4SJonathan Peyton 
14863041982dSJonathan Peyton // Debugger: The taskwait is active. Store location and thread encountered the
14873041982dSJonathan Peyton // taskwait.
14887cc577a4SJonathan Peyton #if USE_ITT_BUILD
14897cc577a4SJonathan Peyton // Note: These values are used by ITT events as well.
14907cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
14917cc577a4SJonathan Peyton     taskdata->td_taskwait_counter += 1;
14927cc577a4SJonathan Peyton     taskdata->td_taskwait_ident = loc_ref;
14937cc577a4SJonathan Peyton     taskdata->td_taskwait_thread = gtid + 1;
14947cc577a4SJonathan Peyton 
14957cc577a4SJonathan Peyton #if USE_ITT_BUILD
14967cc577a4SJonathan Peyton     void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
14977cc577a4SJonathan Peyton     if (itt_sync_obj != NULL)
14987cc577a4SJonathan Peyton       __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
14997cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
15007cc577a4SJonathan Peyton 
15013041982dSJonathan Peyton     bool must_wait =
15023041982dSJonathan Peyton         !taskdata->td_flags.team_serial && !taskdata->td_flags.final;
15037cc577a4SJonathan Peyton 
15047cc577a4SJonathan Peyton #if OMP_45_ENABLED
15053041982dSJonathan Peyton     must_wait = must_wait || (thread->th.th_task_team != NULL &&
15063041982dSJonathan Peyton                               thread->th.th_task_team->tt.tt_found_proxy_tasks);
15077cc577a4SJonathan Peyton #endif
15083041982dSJonathan Peyton     if (must_wait) {
1509c47afcd9SAndrey Churbanov       kmp_flag_32 flag(
1510c47afcd9SAndrey Churbanov           RCAST(volatile kmp_uint32 *, &taskdata->td_incomplete_child_tasks),
1511c47afcd9SAndrey Churbanov           0U);
15127cc577a4SJonathan Peyton       while (TCR_4(taskdata->td_incomplete_child_tasks) != 0) {
15133041982dSJonathan Peyton         flag.execute_tasks(thread, gtid, FALSE,
15143041982dSJonathan Peyton                            &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
15153041982dSJonathan Peyton                            __kmp_task_stealing_constraint);
15167cc577a4SJonathan Peyton       }
15177cc577a4SJonathan Peyton     }
15187cc577a4SJonathan Peyton #if USE_ITT_BUILD
15197cc577a4SJonathan Peyton     if (itt_sync_obj != NULL)
15207cc577a4SJonathan Peyton       __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
15217cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
15227cc577a4SJonathan Peyton 
15233041982dSJonathan Peyton     // Debugger:  The taskwait is completed. Location remains, but thread is
15243041982dSJonathan Peyton     // negated.
15257cc577a4SJonathan Peyton     taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
15267cc577a4SJonathan Peyton 
15277cc577a4SJonathan Peyton #if OMPT_SUPPORT && OMPT_TRACE
15287cc577a4SJonathan Peyton     if (ompt_enabled) {
15297cc577a4SJonathan Peyton       if (ompt_callbacks.ompt_callback(ompt_event_taskwait_end)) {
15303041982dSJonathan Peyton         ompt_callbacks.ompt_callback(ompt_event_taskwait_end)(my_parallel_id,
15313041982dSJonathan Peyton                                                               my_task_id);
15327cc577a4SJonathan Peyton       }
15337cc577a4SJonathan Peyton       taskdata->ompt_task_info.frame.reenter_runtime_frame = NULL;
15347cc577a4SJonathan Peyton     }
15357cc577a4SJonathan Peyton #endif
15367cc577a4SJonathan Peyton     ANNOTATE_HAPPENS_AFTER(taskdata);
15377cc577a4SJonathan Peyton   }
15387cc577a4SJonathan Peyton 
15397cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, "
15403041982dSJonathan Peyton                 "returning TASK_CURRENT_NOT_QUEUED\n",
15413041982dSJonathan Peyton                 gtid, taskdata));
15427cc577a4SJonathan Peyton 
15437cc577a4SJonathan Peyton   return TASK_CURRENT_NOT_QUEUED;
15447cc577a4SJonathan Peyton }
15457cc577a4SJonathan Peyton 
15467cc577a4SJonathan Peyton // __kmpc_omp_taskyield: switch to a different task
15473041982dSJonathan Peyton kmp_int32 __kmpc_omp_taskyield(ident_t *loc_ref, kmp_int32 gtid, int end_part) {
15487cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata;
15497cc577a4SJonathan Peyton   kmp_info_t *thread;
15507cc577a4SJonathan Peyton   int thread_finished = FALSE;
15517cc577a4SJonathan Peyton 
15527cc577a4SJonathan Peyton   KMP_COUNT_BLOCK(OMP_TASKYIELD);
15537cc577a4SJonathan Peyton   KMP_SET_THREAD_STATE_BLOCK(TASKYIELD);
15547cc577a4SJonathan Peyton 
15557cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n",
15567cc577a4SJonathan Peyton                 gtid, loc_ref, end_part));
15577cc577a4SJonathan Peyton 
15587cc577a4SJonathan Peyton   if (__kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel) {
15597cc577a4SJonathan Peyton     thread = __kmp_threads[gtid];
15607cc577a4SJonathan Peyton     taskdata = thread->th.th_current_task;
15617cc577a4SJonathan Peyton // Should we model this as a task wait or not?
15623041982dSJonathan Peyton // Debugger: The taskwait is active. Store location and thread encountered the
15633041982dSJonathan Peyton // taskwait.
15647cc577a4SJonathan Peyton #if USE_ITT_BUILD
15657cc577a4SJonathan Peyton // Note: These values are used by ITT events as well.
15667cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
15677cc577a4SJonathan Peyton     taskdata->td_taskwait_counter += 1;
15687cc577a4SJonathan Peyton     taskdata->td_taskwait_ident = loc_ref;
15697cc577a4SJonathan Peyton     taskdata->td_taskwait_thread = gtid + 1;
15707cc577a4SJonathan Peyton 
15717cc577a4SJonathan Peyton #if USE_ITT_BUILD
15727cc577a4SJonathan Peyton     void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
15737cc577a4SJonathan Peyton     if (itt_sync_obj != NULL)
15747cc577a4SJonathan Peyton       __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
15757cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
15767cc577a4SJonathan Peyton     if (!taskdata->td_flags.team_serial) {
15777cc577a4SJonathan Peyton       kmp_task_team_t *task_team = thread->th.th_task_team;
15787cc577a4SJonathan Peyton       if (task_team != NULL) {
15797cc577a4SJonathan Peyton         if (KMP_TASKING_ENABLED(task_team)) {
15803041982dSJonathan Peyton           __kmp_execute_tasks_32(
15813041982dSJonathan Peyton               thread, gtid, NULL, FALSE,
15823041982dSJonathan Peyton               &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
15833041982dSJonathan Peyton               __kmp_task_stealing_constraint);
15847cc577a4SJonathan Peyton         }
15857cc577a4SJonathan Peyton       }
15867cc577a4SJonathan Peyton     }
15877cc577a4SJonathan Peyton #if USE_ITT_BUILD
15887cc577a4SJonathan Peyton     if (itt_sync_obj != NULL)
15897cc577a4SJonathan Peyton       __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
15907cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
15917cc577a4SJonathan Peyton 
15923041982dSJonathan Peyton     // Debugger:  The taskwait is completed. Location remains, but thread is
15933041982dSJonathan Peyton     // negated.
15947cc577a4SJonathan Peyton     taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
15957cc577a4SJonathan Peyton   }
15967cc577a4SJonathan Peyton 
15977cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_taskyield(exit): T#%d task %p resuming, "
15983041982dSJonathan Peyton                 "returning TASK_CURRENT_NOT_QUEUED\n",
15993041982dSJonathan Peyton                 gtid, taskdata));
16007cc577a4SJonathan Peyton 
16017cc577a4SJonathan Peyton   return TASK_CURRENT_NOT_QUEUED;
16027cc577a4SJonathan Peyton }
16037cc577a4SJonathan Peyton 
160472ba2109SAndrey Churbanov // TODO: change to OMP_50_ENABLED, need to change build tools for this to work
160572ba2109SAndrey Churbanov #if OMP_45_ENABLED
160672ba2109SAndrey Churbanov // Task Reduction implementation
160772ba2109SAndrey Churbanov 
160872ba2109SAndrey Churbanov typedef struct kmp_task_red_flags {
160972ba2109SAndrey Churbanov   unsigned lazy_priv : 1; // hint: (1) use lazy allocation (big objects)
161072ba2109SAndrey Churbanov   unsigned reserved31 : 31;
161172ba2109SAndrey Churbanov } kmp_task_red_flags_t;
161272ba2109SAndrey Churbanov 
161372ba2109SAndrey Churbanov // internal structure for reduction data item related info
161472ba2109SAndrey Churbanov typedef struct kmp_task_red_data {
161572ba2109SAndrey Churbanov   void *reduce_shar; // shared reduction item
161672ba2109SAndrey Churbanov   size_t reduce_size; // size of data item
161772ba2109SAndrey Churbanov   void *reduce_priv; // thread specific data
161872ba2109SAndrey Churbanov   void *reduce_pend; // end of private data for comparison op
161972ba2109SAndrey Churbanov   void *reduce_init; // data initialization routine
162072ba2109SAndrey Churbanov   void *reduce_fini; // data finalization routine
162172ba2109SAndrey Churbanov   void *reduce_comb; // data combiner routine
162272ba2109SAndrey Churbanov   kmp_task_red_flags_t flags; // flags for additional info from compiler
162372ba2109SAndrey Churbanov } kmp_task_red_data_t;
162472ba2109SAndrey Churbanov 
162572ba2109SAndrey Churbanov // structure sent us by compiler - one per reduction item
162672ba2109SAndrey Churbanov typedef struct kmp_task_red_input {
162772ba2109SAndrey Churbanov   void *reduce_shar; // shared reduction item
162872ba2109SAndrey Churbanov   size_t reduce_size; // size of data item
162972ba2109SAndrey Churbanov   void *reduce_init; // data initialization routine
163072ba2109SAndrey Churbanov   void *reduce_fini; // data finalization routine
163172ba2109SAndrey Churbanov   void *reduce_comb; // data combiner routine
163272ba2109SAndrey Churbanov   kmp_task_red_flags_t flags; // flags for additional info from compiler
163372ba2109SAndrey Churbanov } kmp_task_red_input_t;
163472ba2109SAndrey Churbanov 
163572ba2109SAndrey Churbanov /*!
163672ba2109SAndrey Churbanov @ingroup TASKING
163772ba2109SAndrey Churbanov @param gtid      Global thread ID
163872ba2109SAndrey Churbanov @param num       Number of data items to reduce
163972ba2109SAndrey Churbanov @param data      Array of data for reduction
164072ba2109SAndrey Churbanov @return The taskgroup identifier
164172ba2109SAndrey Churbanov 
164272ba2109SAndrey Churbanov Initialize task reduction for the taskgroup.
164372ba2109SAndrey Churbanov */
16443041982dSJonathan Peyton void *__kmpc_task_reduction_init(int gtid, int num, void *data) {
164572ba2109SAndrey Churbanov   kmp_info_t *thread = __kmp_threads[gtid];
164672ba2109SAndrey Churbanov   kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup;
164772ba2109SAndrey Churbanov   kmp_int32 nth = thread->th.th_team_nproc;
164872ba2109SAndrey Churbanov   kmp_task_red_input_t *input = (kmp_task_red_input_t *)data;
164972ba2109SAndrey Churbanov   kmp_task_red_data_t *arr;
165072ba2109SAndrey Churbanov 
165172ba2109SAndrey Churbanov   // check input data just in case
165272ba2109SAndrey Churbanov   KMP_ASSERT(tg != NULL);
165372ba2109SAndrey Churbanov   KMP_ASSERT(data != NULL);
165472ba2109SAndrey Churbanov   KMP_ASSERT(num > 0);
165572ba2109SAndrey Churbanov   if (nth == 1) {
165672ba2109SAndrey Churbanov     KA_TRACE(10, ("__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n",
165772ba2109SAndrey Churbanov                   gtid, tg));
165872ba2109SAndrey Churbanov     return (void *)tg;
165972ba2109SAndrey Churbanov   }
166072ba2109SAndrey Churbanov   KA_TRACE(10, ("__kmpc_task_reduction_init: T#%d, taskgroup %p, #items %d\n",
166172ba2109SAndrey Churbanov                 gtid, tg, num));
16623041982dSJonathan Peyton   arr = (kmp_task_red_data_t *)__kmp_thread_malloc(
16633041982dSJonathan Peyton       thread, num * sizeof(kmp_task_red_data_t));
166472ba2109SAndrey Churbanov   for (int i = 0; i < num; ++i) {
166572ba2109SAndrey Churbanov     void (*f_init)(void *) = (void (*)(void *))(input[i].reduce_init);
166672ba2109SAndrey Churbanov     size_t size = input[i].reduce_size - 1;
166772ba2109SAndrey Churbanov     // round the size up to cache line per thread-specific item
166872ba2109SAndrey Churbanov     size += CACHE_LINE - size % CACHE_LINE;
166972ba2109SAndrey Churbanov     KMP_ASSERT(input[i].reduce_comb != NULL); // combiner is mandatory
167072ba2109SAndrey Churbanov     arr[i].reduce_shar = input[i].reduce_shar;
167172ba2109SAndrey Churbanov     arr[i].reduce_size = size;
167272ba2109SAndrey Churbanov     arr[i].reduce_init = input[i].reduce_init;
167372ba2109SAndrey Churbanov     arr[i].reduce_fini = input[i].reduce_fini;
167472ba2109SAndrey Churbanov     arr[i].reduce_comb = input[i].reduce_comb;
167572ba2109SAndrey Churbanov     arr[i].flags = input[i].flags;
167672ba2109SAndrey Churbanov     if (!input[i].flags.lazy_priv) {
167772ba2109SAndrey Churbanov       // allocate cache-line aligned block and fill it with zeros
167872ba2109SAndrey Churbanov       arr[i].reduce_priv = __kmp_allocate(nth * size);
167972ba2109SAndrey Churbanov       arr[i].reduce_pend = (char *)(arr[i].reduce_priv) + nth * size;
168072ba2109SAndrey Churbanov       if (f_init != NULL) {
168172ba2109SAndrey Churbanov         // initialize thread-specific items
168272ba2109SAndrey Churbanov         for (int j = 0; j < nth; ++j) {
168372ba2109SAndrey Churbanov           f_init((char *)(arr[i].reduce_priv) + j * size);
168472ba2109SAndrey Churbanov         }
168572ba2109SAndrey Churbanov       }
168672ba2109SAndrey Churbanov     } else {
168772ba2109SAndrey Churbanov       // only allocate space for pointers now,
168872ba2109SAndrey Churbanov       // objects will be lazily allocated/initialized once requested
168972ba2109SAndrey Churbanov       arr[i].reduce_priv = __kmp_allocate(nth * sizeof(void *));
169072ba2109SAndrey Churbanov     }
169172ba2109SAndrey Churbanov   }
169272ba2109SAndrey Churbanov   tg->reduce_data = (void *)arr;
169372ba2109SAndrey Churbanov   tg->reduce_num_data = num;
169472ba2109SAndrey Churbanov   return (void *)tg;
169572ba2109SAndrey Churbanov }
169672ba2109SAndrey Churbanov 
169772ba2109SAndrey Churbanov /*!
169872ba2109SAndrey Churbanov @ingroup TASKING
169972ba2109SAndrey Churbanov @param gtid    Global thread ID
170072ba2109SAndrey Churbanov @param tskgrp  The taskgroup ID (optional)
170172ba2109SAndrey Churbanov @param data    Shared location of the item
170272ba2109SAndrey Churbanov @return The pointer to per-thread data
170372ba2109SAndrey Churbanov 
170472ba2109SAndrey Churbanov Get thread-specific location of data item
170572ba2109SAndrey Churbanov */
17063041982dSJonathan Peyton void *__kmpc_task_reduction_get_th_data(int gtid, void *tskgrp, void *data) {
170772ba2109SAndrey Churbanov   kmp_info_t *thread = __kmp_threads[gtid];
170872ba2109SAndrey Churbanov   kmp_int32 nth = thread->th.th_team_nproc;
170972ba2109SAndrey Churbanov   if (nth == 1)
171072ba2109SAndrey Churbanov     return data; // nothing to do
171172ba2109SAndrey Churbanov 
171272ba2109SAndrey Churbanov   kmp_taskgroup_t *tg = (kmp_taskgroup_t *)tskgrp;
171372ba2109SAndrey Churbanov   if (tg == NULL)
171472ba2109SAndrey Churbanov     tg = thread->th.th_current_task->td_taskgroup;
171572ba2109SAndrey Churbanov   KMP_ASSERT(tg != NULL);
171672ba2109SAndrey Churbanov   kmp_task_red_data_t *arr = (kmp_task_red_data_t *)(tg->reduce_data);
171772ba2109SAndrey Churbanov   kmp_int32 num = tg->reduce_num_data;
171872ba2109SAndrey Churbanov   kmp_int32 tid = thread->th.th_info.ds.ds_tid;
171972ba2109SAndrey Churbanov 
172072ba2109SAndrey Churbanov   KMP_ASSERT(data != NULL);
172172ba2109SAndrey Churbanov   while (tg != NULL) {
172272ba2109SAndrey Churbanov     for (int i = 0; i < num; ++i) {
172372ba2109SAndrey Churbanov       if (!arr[i].flags.lazy_priv) {
172472ba2109SAndrey Churbanov         if (data == arr[i].reduce_shar ||
172572ba2109SAndrey Churbanov             (data >= arr[i].reduce_priv && data < arr[i].reduce_pend))
172672ba2109SAndrey Churbanov           return (char *)(arr[i].reduce_priv) + tid * arr[i].reduce_size;
172772ba2109SAndrey Churbanov       } else {
172872ba2109SAndrey Churbanov         // check shared location first
172972ba2109SAndrey Churbanov         void **p_priv = (void **)(arr[i].reduce_priv);
173072ba2109SAndrey Churbanov         if (data == arr[i].reduce_shar)
173172ba2109SAndrey Churbanov           goto found;
173272ba2109SAndrey Churbanov         // check if we get some thread specific location as parameter
173372ba2109SAndrey Churbanov         for (int j = 0; j < nth; ++j)
173472ba2109SAndrey Churbanov           if (data == p_priv[j])
173572ba2109SAndrey Churbanov             goto found;
173672ba2109SAndrey Churbanov         continue; // not found, continue search
173772ba2109SAndrey Churbanov       found:
173872ba2109SAndrey Churbanov         if (p_priv[tid] == NULL) {
173972ba2109SAndrey Churbanov           // allocate thread specific object lazily
174072ba2109SAndrey Churbanov           void (*f_init)(void *) = (void (*)(void *))(arr[i].reduce_init);
174172ba2109SAndrey Churbanov           p_priv[tid] = __kmp_allocate(arr[i].reduce_size);
174272ba2109SAndrey Churbanov           if (f_init != NULL) {
174372ba2109SAndrey Churbanov             f_init(p_priv[tid]);
174472ba2109SAndrey Churbanov           }
174572ba2109SAndrey Churbanov         }
174672ba2109SAndrey Churbanov         return p_priv[tid];
174772ba2109SAndrey Churbanov       }
174872ba2109SAndrey Churbanov     }
174972ba2109SAndrey Churbanov     tg = tg->parent;
175072ba2109SAndrey Churbanov     arr = (kmp_task_red_data_t *)(tg->reduce_data);
175172ba2109SAndrey Churbanov     num = tg->reduce_num_data;
175272ba2109SAndrey Churbanov   }
175372ba2109SAndrey Churbanov   KMP_ASSERT2(0, "Unknown task reduction item");
175472ba2109SAndrey Churbanov   return NULL; // ERROR, this line never executed
175572ba2109SAndrey Churbanov }
175672ba2109SAndrey Churbanov 
175772ba2109SAndrey Churbanov // Finalize task reduction.
175872ba2109SAndrey Churbanov // Called from __kmpc_end_taskgroup()
17593041982dSJonathan Peyton static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg) {
176072ba2109SAndrey Churbanov   kmp_int32 nth = th->th.th_team_nproc;
176172ba2109SAndrey Churbanov   KMP_DEBUG_ASSERT(nth > 1); // should not be called if nth == 1
176272ba2109SAndrey Churbanov   kmp_task_red_data_t *arr = (kmp_task_red_data_t *)tg->reduce_data;
176372ba2109SAndrey Churbanov   kmp_int32 num = tg->reduce_num_data;
176472ba2109SAndrey Churbanov   for (int i = 0; i < num; ++i) {
176572ba2109SAndrey Churbanov     void *sh_data = arr[i].reduce_shar;
176672ba2109SAndrey Churbanov     void (*f_fini)(void *) = (void (*)(void *))(arr[i].reduce_fini);
17673041982dSJonathan Peyton     void (*f_comb)(void *, void *) =
17683041982dSJonathan Peyton         (void (*)(void *, void *))(arr[i].reduce_comb);
176972ba2109SAndrey Churbanov     if (!arr[i].flags.lazy_priv) {
177072ba2109SAndrey Churbanov       void *pr_data = arr[i].reduce_priv;
177172ba2109SAndrey Churbanov       size_t size = arr[i].reduce_size;
177272ba2109SAndrey Churbanov       for (int j = 0; j < nth; ++j) {
177372ba2109SAndrey Churbanov         void *priv_data = (char *)pr_data + j * size;
177472ba2109SAndrey Churbanov         f_comb(sh_data, priv_data); // combine results
177572ba2109SAndrey Churbanov         if (f_fini)
177672ba2109SAndrey Churbanov           f_fini(priv_data); // finalize if needed
177772ba2109SAndrey Churbanov       }
177872ba2109SAndrey Churbanov     } else {
177972ba2109SAndrey Churbanov       void **pr_data = (void **)(arr[i].reduce_priv);
178072ba2109SAndrey Churbanov       for (int j = 0; j < nth; ++j) {
178172ba2109SAndrey Churbanov         if (pr_data[j] != NULL) {
178272ba2109SAndrey Churbanov           f_comb(sh_data, pr_data[j]); // combine results
178372ba2109SAndrey Churbanov           if (f_fini)
178472ba2109SAndrey Churbanov             f_fini(pr_data[j]); // finalize if needed
178572ba2109SAndrey Churbanov           __kmp_free(pr_data[j]);
178672ba2109SAndrey Churbanov         }
178772ba2109SAndrey Churbanov       }
178872ba2109SAndrey Churbanov     }
178972ba2109SAndrey Churbanov     __kmp_free(arr[i].reduce_priv);
179072ba2109SAndrey Churbanov   }
179172ba2109SAndrey Churbanov   __kmp_thread_free(th, arr);
179272ba2109SAndrey Churbanov   tg->reduce_data = NULL;
179372ba2109SAndrey Churbanov   tg->reduce_num_data = 0;
179472ba2109SAndrey Churbanov }
179572ba2109SAndrey Churbanov #endif
17967cc577a4SJonathan Peyton 
17977cc577a4SJonathan Peyton #if OMP_40_ENABLED
17987cc577a4SJonathan Peyton // __kmpc_taskgroup: Start a new taskgroup
17993041982dSJonathan Peyton void __kmpc_taskgroup(ident_t *loc, int gtid) {
18007cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
18017cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = thread->th.th_current_task;
18027cc577a4SJonathan Peyton   kmp_taskgroup_t *tg_new =
18037cc577a4SJonathan Peyton       (kmp_taskgroup_t *)__kmp_thread_malloc(thread, sizeof(kmp_taskgroup_t));
18047cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new));
18057cc577a4SJonathan Peyton   tg_new->count = 0;
18067cc577a4SJonathan Peyton   tg_new->cancel_request = cancel_noreq;
18077cc577a4SJonathan Peyton   tg_new->parent = taskdata->td_taskgroup;
180872ba2109SAndrey Churbanov // TODO: change to OMP_50_ENABLED, need to change build tools for this to work
180972ba2109SAndrey Churbanov #if OMP_45_ENABLED
181072ba2109SAndrey Churbanov   tg_new->reduce_data = NULL;
181172ba2109SAndrey Churbanov   tg_new->reduce_num_data = 0;
181272ba2109SAndrey Churbanov #endif
18137cc577a4SJonathan Peyton   taskdata->td_taskgroup = tg_new;
18147cc577a4SJonathan Peyton }
18157cc577a4SJonathan Peyton 
18167cc577a4SJonathan Peyton // __kmpc_end_taskgroup: Wait until all tasks generated by the current task
18177cc577a4SJonathan Peyton //                       and its descendants are complete
18183041982dSJonathan Peyton void __kmpc_end_taskgroup(ident_t *loc, int gtid) {
18197cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
18207cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = thread->th.th_current_task;
18217cc577a4SJonathan Peyton   kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
18227cc577a4SJonathan Peyton   int thread_finished = FALSE;
18237cc577a4SJonathan Peyton 
18247cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc));
18257cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskgroup != NULL);
18267cc577a4SJonathan Peyton   KMP_SET_THREAD_STATE_BLOCK(TASKGROUP);
18277cc577a4SJonathan Peyton 
18287cc577a4SJonathan Peyton   if (__kmp_tasking_mode != tskm_immediate_exec) {
18297cc577a4SJonathan Peyton #if USE_ITT_BUILD
18303041982dSJonathan Peyton     // For ITT the taskgroup wait is similar to taskwait until we need to
18313041982dSJonathan Peyton     // distinguish them
18327cc577a4SJonathan Peyton     void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
18337cc577a4SJonathan Peyton     if (itt_sync_obj != NULL)
18347cc577a4SJonathan Peyton       __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
18357cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
18367cc577a4SJonathan Peyton 
18377cc577a4SJonathan Peyton #if OMP_45_ENABLED
18383041982dSJonathan Peyton     if (!taskdata->td_flags.team_serial ||
18393041982dSJonathan Peyton         (thread->th.th_task_team != NULL &&
18403041982dSJonathan Peyton          thread->th.th_task_team->tt.tt_found_proxy_tasks))
18417cc577a4SJonathan Peyton #else
18427cc577a4SJonathan Peyton     if (!taskdata->td_flags.team_serial)
18437cc577a4SJonathan Peyton #endif
18447cc577a4SJonathan Peyton     {
1845c47afcd9SAndrey Churbanov       kmp_flag_32 flag(RCAST(kmp_uint32 *, &taskgroup->count), 0U);
18467cc577a4SJonathan Peyton       while (TCR_4(taskgroup->count) != 0) {
18473041982dSJonathan Peyton         flag.execute_tasks(thread, gtid, FALSE,
18483041982dSJonathan Peyton                            &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
18493041982dSJonathan Peyton                            __kmp_task_stealing_constraint);
18507cc577a4SJonathan Peyton       }
18517cc577a4SJonathan Peyton     }
18527cc577a4SJonathan Peyton 
18537cc577a4SJonathan Peyton #if USE_ITT_BUILD
18547cc577a4SJonathan Peyton     if (itt_sync_obj != NULL)
18557cc577a4SJonathan Peyton       __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
18567cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
18577cc577a4SJonathan Peyton   }
18587cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskgroup->count == 0);
18597cc577a4SJonathan Peyton 
186072ba2109SAndrey Churbanov // TODO: change to OMP_50_ENABLED, need to change build tools for this to work
186172ba2109SAndrey Churbanov #if OMP_45_ENABLED
186272ba2109SAndrey Churbanov   if (taskgroup->reduce_data != NULL) // need to reduce?
186372ba2109SAndrey Churbanov     __kmp_task_reduction_fini(thread, taskgroup);
186472ba2109SAndrey Churbanov #endif
18657cc577a4SJonathan Peyton   // Restore parent taskgroup for the current task
18667cc577a4SJonathan Peyton   taskdata->td_taskgroup = taskgroup->parent;
18677cc577a4SJonathan Peyton   __kmp_thread_free(thread, taskgroup);
18687cc577a4SJonathan Peyton 
18693041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n",
18703041982dSJonathan Peyton                 gtid, taskdata));
18717cc577a4SJonathan Peyton   ANNOTATE_HAPPENS_AFTER(taskdata);
18727cc577a4SJonathan Peyton }
18737cc577a4SJonathan Peyton #endif
18747cc577a4SJonathan Peyton 
18757cc577a4SJonathan Peyton // __kmp_remove_my_task: remove a task from my own deque
18763041982dSJonathan Peyton static kmp_task_t *__kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid,
18773041982dSJonathan Peyton                                         kmp_task_team_t *task_team,
18783041982dSJonathan Peyton                                         kmp_int32 is_constrained) {
18797cc577a4SJonathan Peyton   kmp_task_t *task;
18807cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata;
18817cc577a4SJonathan Peyton   kmp_thread_data_t *thread_data;
18827cc577a4SJonathan Peyton   kmp_uint32 tail;
18837cc577a4SJonathan Peyton 
18847cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
18853041982dSJonathan Peyton   KMP_DEBUG_ASSERT(task_team->tt.tt_threads_data !=
18863041982dSJonathan Peyton                    NULL); // Caller should check this condition
18877cc577a4SJonathan Peyton 
18887cc577a4SJonathan Peyton   thread_data = &task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
18897cc577a4SJonathan Peyton 
18907cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmp_remove_my_task(enter): T#%d ntasks=%d head=%u tail=%u\n",
18913041982dSJonathan Peyton                 gtid, thread_data->td.td_deque_ntasks,
18923041982dSJonathan Peyton                 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
18937cc577a4SJonathan Peyton 
18947cc577a4SJonathan Peyton   if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
18953041982dSJonathan Peyton     KA_TRACE(10,
18963041982dSJonathan Peyton              ("__kmp_remove_my_task(exit #1): T#%d No tasks to remove: "
18973041982dSJonathan Peyton               "ntasks=%d head=%u tail=%u\n",
18983041982dSJonathan Peyton               gtid, thread_data->td.td_deque_ntasks,
18993041982dSJonathan Peyton               thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
19007cc577a4SJonathan Peyton     return NULL;
19017cc577a4SJonathan Peyton   }
19027cc577a4SJonathan Peyton 
19037cc577a4SJonathan Peyton   __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
19047cc577a4SJonathan Peyton 
19057cc577a4SJonathan Peyton   if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
19067cc577a4SJonathan Peyton     __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
19073041982dSJonathan Peyton     KA_TRACE(10,
19083041982dSJonathan Peyton              ("__kmp_remove_my_task(exit #2): T#%d No tasks to remove: "
19093041982dSJonathan Peyton               "ntasks=%d head=%u tail=%u\n",
19103041982dSJonathan Peyton               gtid, thread_data->td.td_deque_ntasks,
19113041982dSJonathan Peyton               thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
19127cc577a4SJonathan Peyton     return NULL;
19137cc577a4SJonathan Peyton   }
19147cc577a4SJonathan Peyton 
19153041982dSJonathan Peyton   tail = (thread_data->td.td_deque_tail - 1) &
19163041982dSJonathan Peyton          TASK_DEQUE_MASK(thread_data->td); // Wrap index.
19177cc577a4SJonathan Peyton   taskdata = thread_data->td.td_deque[tail];
19187cc577a4SJonathan Peyton 
19197cc577a4SJonathan Peyton   if (is_constrained && (taskdata->td_flags.tiedness == TASK_TIED)) {
19207cc577a4SJonathan Peyton     // we need to check if the candidate obeys task scheduling constraint:
19217cc577a4SJonathan Peyton     // only child of current task can be scheduled
19227cc577a4SJonathan Peyton     kmp_taskdata_t *current = thread->th.th_current_task;
19237cc577a4SJonathan Peyton     kmp_int32 level = current->td_level;
19247cc577a4SJonathan Peyton     kmp_taskdata_t *parent = taskdata->td_parent;
19257cc577a4SJonathan Peyton     while (parent != current && parent->td_level > level) {
19263041982dSJonathan Peyton       parent = parent->td_parent; // check generation up to the level of the
19273041982dSJonathan Peyton       // current task
19287cc577a4SJonathan Peyton       KMP_DEBUG_ASSERT(parent != NULL);
19297cc577a4SJonathan Peyton     }
19307cc577a4SJonathan Peyton     if (parent != current) {
19313041982dSJonathan Peyton       // If the tail task is not a child, then no other child can appear in the
19323041982dSJonathan Peyton       // deque.
19337cc577a4SJonathan Peyton       __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
19343041982dSJonathan Peyton       KA_TRACE(10,
19353041982dSJonathan Peyton                ("__kmp_remove_my_task(exit #2): T#%d No tasks to remove: "
19363041982dSJonathan Peyton                 "ntasks=%d head=%u tail=%u\n",
19373041982dSJonathan Peyton                 gtid, thread_data->td.td_deque_ntasks,
19383041982dSJonathan Peyton                 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
19397cc577a4SJonathan Peyton       return NULL;
19407cc577a4SJonathan Peyton     }
19417cc577a4SJonathan Peyton   }
19427cc577a4SJonathan Peyton 
19437cc577a4SJonathan Peyton   thread_data->td.td_deque_tail = tail;
19447cc577a4SJonathan Peyton   TCW_4(thread_data->td.td_deque_ntasks, thread_data->td.td_deque_ntasks - 1);
19457cc577a4SJonathan Peyton 
19467cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
19477cc577a4SJonathan Peyton 
19483041982dSJonathan Peyton   KA_TRACE(10, ("__kmp_remove_my_task(exit #2): T#%d task %p removed: "
19493041982dSJonathan Peyton                 "ntasks=%d head=%u tail=%u\n",
19503041982dSJonathan Peyton                 gtid, taskdata, thread_data->td.td_deque_ntasks,
19513041982dSJonathan Peyton                 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
19527cc577a4SJonathan Peyton 
19537cc577a4SJonathan Peyton   task = KMP_TASKDATA_TO_TASK(taskdata);
19547cc577a4SJonathan Peyton   return task;
19557cc577a4SJonathan Peyton }
19567cc577a4SJonathan Peyton 
19577cc577a4SJonathan Peyton // __kmp_steal_task: remove a task from another thread's deque
19587cc577a4SJonathan Peyton // Assume that calling thread has already checked existence of
19597cc577a4SJonathan Peyton // task_team thread_data before calling this routine.
1960c47afcd9SAndrey Churbanov static kmp_task_t *__kmp_steal_task(kmp_info_t *victim, kmp_int32 gtid,
1961c47afcd9SAndrey Churbanov                                     kmp_task_team_t *task_team,
1962c47afcd9SAndrey Churbanov                                     volatile kmp_int32 *unfinished_threads,
1963c47afcd9SAndrey Churbanov                                     int *thread_finished,
1964c47afcd9SAndrey Churbanov                                     kmp_int32 is_constrained) {
19657cc577a4SJonathan Peyton   kmp_task_t *task;
19667cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata;
19677cc577a4SJonathan Peyton   kmp_thread_data_t *victim_td, *threads_data;
19687cc577a4SJonathan Peyton   kmp_int32 victim_tid;
19697cc577a4SJonathan Peyton 
19707cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
19717cc577a4SJonathan Peyton 
19727cc577a4SJonathan Peyton   threads_data = task_team->tt.tt_threads_data;
19737cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(threads_data != NULL); // Caller should check this condition
19747cc577a4SJonathan Peyton 
19757cc577a4SJonathan Peyton   victim_tid = victim->th.th_info.ds.ds_tid;
19767cc577a4SJonathan Peyton   victim_td = &threads_data[victim_tid];
19777cc577a4SJonathan Peyton 
19783041982dSJonathan Peyton   KA_TRACE(10, ("__kmp_steal_task(enter): T#%d try to steal from T#%d: "
19793041982dSJonathan Peyton                 "task_team=%p ntasks=%d "
19807cc577a4SJonathan Peyton                 "head=%u tail=%u\n",
19813041982dSJonathan Peyton                 gtid, __kmp_gtid_from_thread(victim), task_team,
19823041982dSJonathan Peyton                 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
19833041982dSJonathan Peyton                 victim_td->td.td_deque_tail));
19847cc577a4SJonathan Peyton 
19853041982dSJonathan Peyton   if ((TCR_4(victim_td->td.td_deque_ntasks) ==
19863041982dSJonathan Peyton        0) || // Caller should not check this condition
19873041982dSJonathan Peyton       (TCR_PTR(victim->th.th_task_team) !=
19883041982dSJonathan Peyton        task_team)) // GEH: why would this happen?
19897cc577a4SJonathan Peyton   {
19903041982dSJonathan Peyton     KA_TRACE(10, ("__kmp_steal_task(exit #1): T#%d could not steal from T#%d: "
19913041982dSJonathan Peyton                   "task_team=%p "
19927cc577a4SJonathan Peyton                   "ntasks=%d head=%u tail=%u\n",
19933041982dSJonathan Peyton                   gtid, __kmp_gtid_from_thread(victim), task_team,
19943041982dSJonathan Peyton                   victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
19953041982dSJonathan Peyton                   victim_td->td.td_deque_tail));
19967cc577a4SJonathan Peyton     return NULL;
19977cc577a4SJonathan Peyton   }
19987cc577a4SJonathan Peyton 
19997cc577a4SJonathan Peyton   __kmp_acquire_bootstrap_lock(&victim_td->td.td_deque_lock);
20007cc577a4SJonathan Peyton 
20017cc577a4SJonathan Peyton   // Check again after we acquire the lock
20027cc577a4SJonathan Peyton   if ((TCR_4(victim_td->td.td_deque_ntasks) == 0) ||
20033041982dSJonathan Peyton       (TCR_PTR(victim->th.th_task_team) !=
20043041982dSJonathan Peyton        task_team)) // GEH: why would this happen?
20057cc577a4SJonathan Peyton   {
20067cc577a4SJonathan Peyton     __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
20073041982dSJonathan Peyton     KA_TRACE(10, ("__kmp_steal_task(exit #2): T#%d could not steal from T#%d: "
20083041982dSJonathan Peyton                   "task_team=%p "
20097cc577a4SJonathan Peyton                   "ntasks=%d head=%u tail=%u\n",
20103041982dSJonathan Peyton                   gtid, __kmp_gtid_from_thread(victim), task_team,
20113041982dSJonathan Peyton                   victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
20123041982dSJonathan Peyton                   victim_td->td.td_deque_tail));
20137cc577a4SJonathan Peyton     return NULL;
20147cc577a4SJonathan Peyton   }
20157cc577a4SJonathan Peyton 
20167cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(victim_td->td.td_deque != NULL);
20177cc577a4SJonathan Peyton 
20187cc577a4SJonathan Peyton   taskdata = victim_td->td.td_deque[victim_td->td.td_deque_head];
20197cc577a4SJonathan Peyton   if (is_constrained) {
20207cc577a4SJonathan Peyton     // we need to check if the candidate obeys task scheduling constraint:
20217cc577a4SJonathan Peyton     // only descendant of current task can be scheduled
20227cc577a4SJonathan Peyton     kmp_taskdata_t *current = __kmp_threads[gtid]->th.th_current_task;
20237cc577a4SJonathan Peyton     kmp_int32 level = current->td_level;
20247cc577a4SJonathan Peyton     kmp_taskdata_t *parent = taskdata->td_parent;
20257cc577a4SJonathan Peyton     while (parent != current && parent->td_level > level) {
20263041982dSJonathan Peyton       parent = parent->td_parent; // check generation up to the level of the
20273041982dSJonathan Peyton       // current task
20287cc577a4SJonathan Peyton       KMP_DEBUG_ASSERT(parent != NULL);
20297cc577a4SJonathan Peyton     }
20307cc577a4SJonathan Peyton     if (parent != current) {
20317cc577a4SJonathan Peyton       // If the head task is not a descendant of the current task then do not
20327cc577a4SJonathan Peyton       // steal it. No other task in victim's deque can be a descendant of the
20337cc577a4SJonathan Peyton       // current task.
20347cc577a4SJonathan Peyton       __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
20353041982dSJonathan Peyton       KA_TRACE(10, ("__kmp_steal_task(exit #2): T#%d could not steal from "
20363041982dSJonathan Peyton                     "T#%d: task_team=%p "
20377cc577a4SJonathan Peyton                     "ntasks=%d head=%u tail=%u\n",
20383041982dSJonathan Peyton                     gtid,
20393041982dSJonathan Peyton                     __kmp_gtid_from_thread(threads_data[victim_tid].td.td_thr),
20407cc577a4SJonathan Peyton                     task_team, victim_td->td.td_deque_ntasks,
20417cc577a4SJonathan Peyton                     victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
20427cc577a4SJonathan Peyton       return NULL;
20437cc577a4SJonathan Peyton     }
20447cc577a4SJonathan Peyton   }
20457cc577a4SJonathan Peyton   // Bump head pointer and Wrap.
20463041982dSJonathan Peyton   victim_td->td.td_deque_head =
20473041982dSJonathan Peyton       (victim_td->td.td_deque_head + 1) & TASK_DEQUE_MASK(victim_td->td);
20487cc577a4SJonathan Peyton   if (*thread_finished) {
20493041982dSJonathan Peyton     // We need to un-mark this victim as a finished victim.  This must be done
20503041982dSJonathan Peyton     // before releasing the lock, or else other threads (starting with the
20513041982dSJonathan Peyton     // master victim) might be prematurely released from the barrier!!!
2052c47afcd9SAndrey Churbanov     kmp_int32 count;
20537cc577a4SJonathan Peyton 
20545ba90c79SAndrey Churbanov     count = KMP_TEST_THEN_INC32(unfinished_threads);
20557cc577a4SJonathan Peyton 
20563041982dSJonathan Peyton     KA_TRACE(
20573041982dSJonathan Peyton         20,
20583041982dSJonathan Peyton         ("__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n",
20597cc577a4SJonathan Peyton          gtid, count + 1, task_team));
20607cc577a4SJonathan Peyton 
20617cc577a4SJonathan Peyton     *thread_finished = FALSE;
20627cc577a4SJonathan Peyton   }
20633041982dSJonathan Peyton   TCW_4(victim_td->td.td_deque_ntasks,
20643041982dSJonathan Peyton         TCR_4(victim_td->td.td_deque_ntasks) - 1);
20653041982dSJonathan Peyton 
20667cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
20677cc577a4SJonathan Peyton 
20687cc577a4SJonathan Peyton   KMP_COUNT_BLOCK(TASK_stolen);
20693041982dSJonathan Peyton   KA_TRACE(
20703041982dSJonathan Peyton       10,
20713041982dSJonathan Peyton       ("__kmp_steal_task(exit #3): T#%d stole task %p from T#%d: task_team=%p "
20727cc577a4SJonathan Peyton        "ntasks=%d head=%u tail=%u\n",
20737cc577a4SJonathan Peyton        gtid, taskdata, __kmp_gtid_from_thread(victim), task_team,
20747cc577a4SJonathan Peyton        victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
20757cc577a4SJonathan Peyton        victim_td->td.td_deque_tail));
20767cc577a4SJonathan Peyton 
20777cc577a4SJonathan Peyton   task = KMP_TASKDATA_TO_TASK(taskdata);
20787cc577a4SJonathan Peyton   return task;
20797cc577a4SJonathan Peyton }
20807cc577a4SJonathan Peyton 
20813041982dSJonathan Peyton // __kmp_execute_tasks_template: Choose and execute tasks until either the
20823041982dSJonathan Peyton // condition is statisfied (return true) or there are none left (return false).
20833041982dSJonathan Peyton //
20847cc577a4SJonathan Peyton // final_spin is TRUE if this is the spin at the release barrier.
20857cc577a4SJonathan Peyton // thread_finished indicates whether the thread is finished executing all
20867cc577a4SJonathan Peyton // the tasks it has on its deque, and is at the release barrier.
20877cc577a4SJonathan Peyton // spinner is the location on which to spin.
20887cc577a4SJonathan Peyton // spinner == NULL means only execute a single task and return.
20897cc577a4SJonathan Peyton // checker is the value to check to terminate the spin.
20907cc577a4SJonathan Peyton template <class C>
20913041982dSJonathan Peyton static inline int __kmp_execute_tasks_template(
20923041982dSJonathan Peyton     kmp_info_t *thread, kmp_int32 gtid, C *flag, int final_spin,
20933041982dSJonathan Peyton     int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj),
20943041982dSJonathan Peyton     kmp_int32 is_constrained) {
20957cc577a4SJonathan Peyton   kmp_task_team_t *task_team = thread->th.th_task_team;
20967cc577a4SJonathan Peyton   kmp_thread_data_t *threads_data;
20977cc577a4SJonathan Peyton   kmp_task_t *task;
20987cc577a4SJonathan Peyton   kmp_info_t *other_thread;
20997cc577a4SJonathan Peyton   kmp_taskdata_t *current_task = thread->th.th_current_task;
2100c47afcd9SAndrey Churbanov   volatile kmp_int32 *unfinished_threads;
21013041982dSJonathan Peyton   kmp_int32 nthreads, victim = -2, use_own_tasks = 1, new_victim = 0,
21023041982dSJonathan Peyton                       tid = thread->th.th_info.ds.ds_tid;
21037cc577a4SJonathan Peyton 
21047cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
21057cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(thread == __kmp_threads[gtid]);
21067cc577a4SJonathan Peyton 
21073041982dSJonathan Peyton   if (task_team == NULL)
21083041982dSJonathan Peyton     return FALSE;
21097cc577a4SJonathan Peyton 
21103041982dSJonathan Peyton   KA_TRACE(15, ("__kmp_execute_tasks_template(enter): T#%d final_spin=%d "
21113041982dSJonathan Peyton                 "*thread_finished=%d\n",
21127cc577a4SJonathan Peyton                 gtid, final_spin, *thread_finished));
21137cc577a4SJonathan Peyton 
2114581490e7SAndrey Churbanov   thread->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
21157cc577a4SJonathan Peyton   threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
21167cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(threads_data != NULL);
21177cc577a4SJonathan Peyton 
21187cc577a4SJonathan Peyton   nthreads = task_team->tt.tt_nproc;
21197cc577a4SJonathan Peyton   unfinished_threads = &(task_team->tt.tt_unfinished_threads);
21207cc577a4SJonathan Peyton #if OMP_45_ENABLED
21217cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(nthreads > 1 || task_team->tt.tt_found_proxy_tasks);
21227cc577a4SJonathan Peyton #else
21237cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(nthreads > 1);
21247cc577a4SJonathan Peyton #endif
2125c47afcd9SAndrey Churbanov   KMP_DEBUG_ASSERT(TCR_4(*unfinished_threads) >= 0);
21267cc577a4SJonathan Peyton 
21273041982dSJonathan Peyton   while (1) { // Outer loop keeps trying to find tasks in case of single thread
21283041982dSJonathan Peyton     // getting tasks from target constructs
21297cc577a4SJonathan Peyton     while (1) { // Inner loop to find a task and execute it
21307cc577a4SJonathan Peyton       task = NULL;
21317cc577a4SJonathan Peyton       if (use_own_tasks) { // check on own queue first
21327cc577a4SJonathan Peyton         task = __kmp_remove_my_task(thread, gtid, task_team, is_constrained);
21337cc577a4SJonathan Peyton       }
21347cc577a4SJonathan Peyton       if ((task == NULL) && (nthreads > 1)) { // Steal a task
21357cc577a4SJonathan Peyton         int asleep = 1;
21367cc577a4SJonathan Peyton         use_own_tasks = 0;
21377cc577a4SJonathan Peyton         // Try to steal from the last place I stole from successfully.
21387cc577a4SJonathan Peyton         if (victim == -2) { // haven't stolen anything yet
21397cc577a4SJonathan Peyton           victim = threads_data[tid].td.td_deque_last_stolen;
21403041982dSJonathan Peyton           if (victim !=
21413041982dSJonathan Peyton               -1) // if we have a last stolen from victim, get the thread
21427cc577a4SJonathan Peyton             other_thread = threads_data[victim].td.td_thr;
21437cc577a4SJonathan Peyton         }
21447cc577a4SJonathan Peyton         if (victim != -1) { // found last victim
21457cc577a4SJonathan Peyton           asleep = 0;
21463041982dSJonathan Peyton         } else if (!new_victim) { // no recent steals and we haven't already
21473041982dSJonathan Peyton           // used a new victim; select a random thread
21487cc577a4SJonathan Peyton           do { // Find a different thread to steal work from.
21493041982dSJonathan Peyton             // Pick a random thread. Initial plan was to cycle through all the
21503041982dSJonathan Peyton             // threads, and only return if we tried to steal from every thread,
21513041982dSJonathan Peyton             // and failed.  Arch says that's not such a great idea.
21527cc577a4SJonathan Peyton             victim = __kmp_get_random(thread) % (nthreads - 1);
21537cc577a4SJonathan Peyton             if (victim >= tid) {
21547cc577a4SJonathan Peyton               ++victim; // Adjusts random distribution to exclude self
21557cc577a4SJonathan Peyton             }
21567cc577a4SJonathan Peyton             // Found a potential victim
21577cc577a4SJonathan Peyton             other_thread = threads_data[victim].td.td_thr;
21583041982dSJonathan Peyton             // There is a slight chance that __kmp_enable_tasking() did not wake
21593041982dSJonathan Peyton             // up all threads waiting at the barrier.  If victim is sleeping,
21603041982dSJonathan Peyton             // then wake it up. Since we were going to pay the cache miss
21613041982dSJonathan Peyton             // penalty for referencing another thread's kmp_info_t struct
21623041982dSJonathan Peyton             // anyway,
21633041982dSJonathan Peyton             // the check shouldn't cost too much performance at this point. In
21643041982dSJonathan Peyton             // extra barrier mode, tasks do not sleep at the separate tasking
21653041982dSJonathan Peyton             // barrier, so this isn't a problem.
21667cc577a4SJonathan Peyton             asleep = 0;
21677cc577a4SJonathan Peyton             if ((__kmp_tasking_mode == tskm_task_teams) &&
21687cc577a4SJonathan Peyton                 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) &&
2169c47afcd9SAndrey Churbanov                 (TCR_PTR(CCAST(void *, other_thread->th.th_sleep_loc)) !=
2170c47afcd9SAndrey Churbanov                  NULL)) {
21717cc577a4SJonathan Peyton               asleep = 1;
21723041982dSJonathan Peyton               __kmp_null_resume_wrapper(__kmp_gtid_from_thread(other_thread),
21733041982dSJonathan Peyton                                         other_thread->th.th_sleep_loc);
21743041982dSJonathan Peyton               // A sleeping thread should not have any tasks on it's queue.
21753041982dSJonathan Peyton               // There is a slight possibility that it resumes, steals a task
21763041982dSJonathan Peyton               // from another thread, which spawns more tasks, all in the time
21773041982dSJonathan Peyton               // that it takes this thread to check => don't write an assertion
21783041982dSJonathan Peyton               // that the victim's queue is empty.  Try stealing from a
21793041982dSJonathan Peyton               // different thread.
21807cc577a4SJonathan Peyton             }
21817cc577a4SJonathan Peyton           } while (asleep);
21827cc577a4SJonathan Peyton         }
21837cc577a4SJonathan Peyton 
21847cc577a4SJonathan Peyton         if (!asleep) {
21857cc577a4SJonathan Peyton           // We have a victim to try to steal from
21863041982dSJonathan Peyton           task = __kmp_steal_task(other_thread, gtid, task_team,
21873041982dSJonathan Peyton                                   unfinished_threads, thread_finished,
21883041982dSJonathan Peyton                                   is_constrained);
21897cc577a4SJonathan Peyton         }
21907cc577a4SJonathan Peyton         if (task != NULL) { // set last stolen to victim
21917cc577a4SJonathan Peyton           if (threads_data[tid].td.td_deque_last_stolen != victim) {
21927cc577a4SJonathan Peyton             threads_data[tid].td.td_deque_last_stolen = victim;
21933041982dSJonathan Peyton             // The pre-refactored code did not try more than 1 successful new
21943041982dSJonathan Peyton             // vicitm, unless the last one generated more local tasks;
21953041982dSJonathan Peyton             // new_victim keeps track of this
21967cc577a4SJonathan Peyton             new_victim = 1;
21977cc577a4SJonathan Peyton           }
21983041982dSJonathan Peyton         } else { // No tasks found; unset last_stolen
21997cc577a4SJonathan Peyton           KMP_CHECK_UPDATE(threads_data[tid].td.td_deque_last_stolen, -1);
22007cc577a4SJonathan Peyton           victim = -2; // no successful victim found
22017cc577a4SJonathan Peyton         }
22027cc577a4SJonathan Peyton       }
22037cc577a4SJonathan Peyton 
22047cc577a4SJonathan Peyton       if (task == NULL) // break out of tasking loop
22057cc577a4SJonathan Peyton         break;
22067cc577a4SJonathan Peyton 
22077cc577a4SJonathan Peyton // Found a task; execute it
22087cc577a4SJonathan Peyton #if USE_ITT_BUILD && USE_ITT_NOTIFY
22097cc577a4SJonathan Peyton       if (__itt_sync_create_ptr || KMP_ITT_DEBUG) {
22103041982dSJonathan Peyton         if (itt_sync_obj == NULL) { // we are at fork barrier where we could not
22113041982dSJonathan Peyton           // get the object reliably
22127cc577a4SJonathan Peyton           itt_sync_obj = __kmp_itt_barrier_object(gtid, bs_forkjoin_barrier);
22137cc577a4SJonathan Peyton         }
22147cc577a4SJonathan Peyton         __kmp_itt_task_starting(itt_sync_obj);
22157cc577a4SJonathan Peyton       }
22167cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD && USE_ITT_NOTIFY */
22177cc577a4SJonathan Peyton       __kmp_invoke_task(gtid, task, current_task);
22187cc577a4SJonathan Peyton #if USE_ITT_BUILD
22193041982dSJonathan Peyton       if (itt_sync_obj != NULL)
22203041982dSJonathan Peyton         __kmp_itt_task_finished(itt_sync_obj);
22217cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
22223041982dSJonathan Peyton       // If this thread is only partway through the barrier and the condition is
22233041982dSJonathan Peyton       // met, then return now, so that the barrier gather/release pattern can
22243041982dSJonathan Peyton       // proceed. If this thread is in the last spin loop in the barrier,
22253041982dSJonathan Peyton       // waiting to be released, we know that the termination condition will not
22263041982dSJonathan Peyton       // be satisified, so don't waste any cycles checking it.
22277cc577a4SJonathan Peyton       if (flag == NULL || (!final_spin && flag->done_check())) {
22283041982dSJonathan Peyton         KA_TRACE(
22293041982dSJonathan Peyton             15,
22303041982dSJonathan Peyton             ("__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
22313041982dSJonathan Peyton              gtid));
22327cc577a4SJonathan Peyton         return TRUE;
22337cc577a4SJonathan Peyton       }
22347cc577a4SJonathan Peyton       if (thread->th.th_task_team == NULL) {
22357cc577a4SJonathan Peyton         break;
22367cc577a4SJonathan Peyton       }
22373041982dSJonathan Peyton       // Yield before executing next task
22383041982dSJonathan Peyton       KMP_YIELD(__kmp_library == library_throughput);
22393041982dSJonathan Peyton       // If execution of a stolen task results in more tasks being placed on our
22403041982dSJonathan Peyton       // run queue, reset use_own_tasks
22417cc577a4SJonathan Peyton       if (!use_own_tasks && TCR_4(threads_data[tid].td.td_deque_ntasks) != 0) {
22423041982dSJonathan Peyton         KA_TRACE(20, ("__kmp_execute_tasks_template: T#%d stolen task spawned "
22433041982dSJonathan Peyton                       "other tasks, restart\n",
22443041982dSJonathan Peyton                       gtid));
22457cc577a4SJonathan Peyton         use_own_tasks = 1;
22467cc577a4SJonathan Peyton         new_victim = 0;
22477cc577a4SJonathan Peyton       }
22487cc577a4SJonathan Peyton     }
22497cc577a4SJonathan Peyton 
22503041982dSJonathan Peyton // The task source has been exhausted. If in final spin loop of barrier, check
22513041982dSJonathan Peyton // if termination condition is satisfied.
22527cc577a4SJonathan Peyton #if OMP_45_ENABLED
22533041982dSJonathan Peyton     // The work queue may be empty but there might be proxy tasks still
22543041982dSJonathan Peyton     // executing
22557cc577a4SJonathan Peyton     if (final_spin && TCR_4(current_task->td_incomplete_child_tasks) == 0)
22567cc577a4SJonathan Peyton #else
22577cc577a4SJonathan Peyton     if (final_spin)
22587cc577a4SJonathan Peyton #endif
22597cc577a4SJonathan Peyton     {
22603041982dSJonathan Peyton       // First, decrement the #unfinished threads, if that has not already been
22613041982dSJonathan Peyton       // done.  This decrement might be to the spin location, and result in the
22623041982dSJonathan Peyton       // termination condition being satisfied.
22637cc577a4SJonathan Peyton       if (!*thread_finished) {
2264c47afcd9SAndrey Churbanov         kmp_int32 count;
22657cc577a4SJonathan Peyton 
22665ba90c79SAndrey Churbanov         count = KMP_TEST_THEN_DEC32(unfinished_threads) - 1;
22673041982dSJonathan Peyton         KA_TRACE(20, ("__kmp_execute_tasks_template: T#%d dec "
22683041982dSJonathan Peyton                       "unfinished_threads to %d task_team=%p\n",
22697cc577a4SJonathan Peyton                       gtid, count, task_team));
22707cc577a4SJonathan Peyton         *thread_finished = TRUE;
22717cc577a4SJonathan Peyton       }
22727cc577a4SJonathan Peyton 
22737cc577a4SJonathan Peyton       // It is now unsafe to reference thread->th.th_team !!!
22743041982dSJonathan Peyton       // Decrementing task_team->tt.tt_unfinished_threads can allow the master
22753041982dSJonathan Peyton       // thread to pass through the barrier, where it might reset each thread's
22763041982dSJonathan Peyton       // th.th_team field for the next parallel region. If we can steal more
22773041982dSJonathan Peyton       // work, we know that this has not happened yet.
22787cc577a4SJonathan Peyton       if (flag != NULL && flag->done_check()) {
22793041982dSJonathan Peyton         KA_TRACE(
22803041982dSJonathan Peyton             15,
22813041982dSJonathan Peyton             ("__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
22823041982dSJonathan Peyton              gtid));
22837cc577a4SJonathan Peyton         return TRUE;
22847cc577a4SJonathan Peyton       }
22857cc577a4SJonathan Peyton     }
22867cc577a4SJonathan Peyton 
22873041982dSJonathan Peyton     // If this thread's task team is NULL, master has recognized that there are
22883041982dSJonathan Peyton     // no more tasks; bail out
22897cc577a4SJonathan Peyton     if (thread->th.th_task_team == NULL) {
22903041982dSJonathan Peyton       KA_TRACE(15,
22913041982dSJonathan Peyton                ("__kmp_execute_tasks_template: T#%d no more tasks\n", gtid));
22927cc577a4SJonathan Peyton       return FALSE;
22937cc577a4SJonathan Peyton     }
22947cc577a4SJonathan Peyton 
22957cc577a4SJonathan Peyton #if OMP_45_ENABLED
22963041982dSJonathan Peyton     // We could be getting tasks from target constructs; if this is the only
22973041982dSJonathan Peyton     // thread, keep trying to execute tasks from own queue
22987cc577a4SJonathan Peyton     if (nthreads == 1)
22997cc577a4SJonathan Peyton       use_own_tasks = 1;
23007cc577a4SJonathan Peyton     else
23017cc577a4SJonathan Peyton #endif
23027cc577a4SJonathan Peyton     {
23033041982dSJonathan Peyton       KA_TRACE(15,
23043041982dSJonathan Peyton                ("__kmp_execute_tasks_template: T#%d can't find work\n", gtid));
23057cc577a4SJonathan Peyton       return FALSE;
23067cc577a4SJonathan Peyton     }
23077cc577a4SJonathan Peyton   }
23087cc577a4SJonathan Peyton }
23097cc577a4SJonathan Peyton 
23103041982dSJonathan Peyton int __kmp_execute_tasks_32(
23113041982dSJonathan Peyton     kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32 *flag, int final_spin,
23123041982dSJonathan Peyton     int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj),
23133041982dSJonathan Peyton     kmp_int32 is_constrained) {
23143041982dSJonathan Peyton   return __kmp_execute_tasks_template(
23153041982dSJonathan Peyton       thread, gtid, flag, final_spin,
23163041982dSJonathan Peyton       thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
23177cc577a4SJonathan Peyton }
23187cc577a4SJonathan Peyton 
23193041982dSJonathan Peyton int __kmp_execute_tasks_64(
23203041982dSJonathan Peyton     kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64 *flag, int final_spin,
23213041982dSJonathan Peyton     int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj),
23223041982dSJonathan Peyton     kmp_int32 is_constrained) {
23233041982dSJonathan Peyton   return __kmp_execute_tasks_template(
23243041982dSJonathan Peyton       thread, gtid, flag, final_spin,
23253041982dSJonathan Peyton       thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
23267cc577a4SJonathan Peyton }
23277cc577a4SJonathan Peyton 
23283041982dSJonathan Peyton int __kmp_execute_tasks_oncore(
23293041982dSJonathan Peyton     kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag, int final_spin,
23303041982dSJonathan Peyton     int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj),
23313041982dSJonathan Peyton     kmp_int32 is_constrained) {
23323041982dSJonathan Peyton   return __kmp_execute_tasks_template(
23333041982dSJonathan Peyton       thread, gtid, flag, final_spin,
23343041982dSJonathan Peyton       thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
23357cc577a4SJonathan Peyton }
23367cc577a4SJonathan Peyton 
23377cc577a4SJonathan Peyton // __kmp_enable_tasking: Allocate task team and resume threads sleeping at the
23387cc577a4SJonathan Peyton // next barrier so they can assist in executing enqueued tasks.
23397cc577a4SJonathan Peyton // First thread in allocates the task team atomically.
23403041982dSJonathan Peyton static void __kmp_enable_tasking(kmp_task_team_t *task_team,
23413041982dSJonathan Peyton                                  kmp_info_t *this_thr) {
23427cc577a4SJonathan Peyton   kmp_thread_data_t *threads_data;
23437cc577a4SJonathan Peyton   int nthreads, i, is_init_thread;
23447cc577a4SJonathan Peyton 
23457cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmp_enable_tasking(enter): T#%d\n",
23467cc577a4SJonathan Peyton                 __kmp_gtid_from_thread(this_thr)));
23477cc577a4SJonathan Peyton 
23487cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task_team != NULL);
23497cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(this_thr->th.th_team != NULL);
23507cc577a4SJonathan Peyton 
23517cc577a4SJonathan Peyton   nthreads = task_team->tt.tt_nproc;
23527cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(nthreads > 0);
23537cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(nthreads == this_thr->th.th_team->t.t_nproc);
23547cc577a4SJonathan Peyton 
23557cc577a4SJonathan Peyton   // Allocate or increase the size of threads_data if necessary
23567cc577a4SJonathan Peyton   is_init_thread = __kmp_realloc_task_threads_data(this_thr, task_team);
23577cc577a4SJonathan Peyton 
23587cc577a4SJonathan Peyton   if (!is_init_thread) {
23597cc577a4SJonathan Peyton     // Some other thread already set up the array.
23603041982dSJonathan Peyton     KA_TRACE(
23613041982dSJonathan Peyton         20,
23623041982dSJonathan Peyton         ("__kmp_enable_tasking(exit): T#%d: threads array already set up.\n",
23637cc577a4SJonathan Peyton          __kmp_gtid_from_thread(this_thr)));
23647cc577a4SJonathan Peyton     return;
23657cc577a4SJonathan Peyton   }
23667cc577a4SJonathan Peyton   threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
23677cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(threads_data != NULL);
23687cc577a4SJonathan Peyton 
23697cc577a4SJonathan Peyton   if ((__kmp_tasking_mode == tskm_task_teams) &&
23703041982dSJonathan Peyton       (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME)) {
23717cc577a4SJonathan Peyton     // Release any threads sleeping at the barrier, so that they can steal
23727cc577a4SJonathan Peyton     // tasks and execute them.  In extra barrier mode, tasks do not sleep
23737cc577a4SJonathan Peyton     // at the separate tasking barrier, so this isn't a problem.
23747cc577a4SJonathan Peyton     for (i = 0; i < nthreads; i++) {
23757cc577a4SJonathan Peyton       volatile void *sleep_loc;
23767cc577a4SJonathan Peyton       kmp_info_t *thread = threads_data[i].td.td_thr;
23777cc577a4SJonathan Peyton 
23787cc577a4SJonathan Peyton       if (i == this_thr->th.th_info.ds.ds_tid) {
23797cc577a4SJonathan Peyton         continue;
23807cc577a4SJonathan Peyton       }
23817cc577a4SJonathan Peyton       // Since we haven't locked the thread's suspend mutex lock at this
23827cc577a4SJonathan Peyton       // point, there is a small window where a thread might be putting
23837cc577a4SJonathan Peyton       // itself to sleep, but hasn't set the th_sleep_loc field yet.
23847cc577a4SJonathan Peyton       // To work around this, __kmp_execute_tasks_template() periodically checks
23853041982dSJonathan Peyton       // see if other threads are sleeping (using the same random mechanism that
23863041982dSJonathan Peyton       // is used for task stealing) and awakens them if they are.
2387c47afcd9SAndrey Churbanov       if ((sleep_loc = TCR_PTR(CCAST(void *, thread->th.th_sleep_loc))) !=
2388c47afcd9SAndrey Churbanov           NULL) {
23897cc577a4SJonathan Peyton         KF_TRACE(50, ("__kmp_enable_tasking: T#%d waking up thread T#%d\n",
23907cc577a4SJonathan Peyton                       __kmp_gtid_from_thread(this_thr),
23917cc577a4SJonathan Peyton                       __kmp_gtid_from_thread(thread)));
23927cc577a4SJonathan Peyton         __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc);
23933041982dSJonathan Peyton       } else {
23947cc577a4SJonathan Peyton         KF_TRACE(50, ("__kmp_enable_tasking: T#%d don't wake up thread T#%d\n",
23957cc577a4SJonathan Peyton                       __kmp_gtid_from_thread(this_thr),
23967cc577a4SJonathan Peyton                       __kmp_gtid_from_thread(thread)));
23977cc577a4SJonathan Peyton       }
23987cc577a4SJonathan Peyton     }
23997cc577a4SJonathan Peyton   }
24007cc577a4SJonathan Peyton 
24017cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmp_enable_tasking(exit): T#%d\n",
24027cc577a4SJonathan Peyton                 __kmp_gtid_from_thread(this_thr)));
24037cc577a4SJonathan Peyton }
24047cc577a4SJonathan Peyton 
24057cc577a4SJonathan Peyton /* // TODO: Check the comment consistency
24067cc577a4SJonathan Peyton  * Utility routines for "task teams".  A task team (kmp_task_t) is kind of
24077cc577a4SJonathan Peyton  * like a shadow of the kmp_team_t data struct, with a different lifetime.
24087cc577a4SJonathan Peyton  * After a child * thread checks into a barrier and calls __kmp_release() from
24097cc577a4SJonathan Peyton  * the particular variant of __kmp_<barrier_kind>_barrier_gather(), it can no
24107cc577a4SJonathan Peyton  * longer assume that the kmp_team_t structure is intact (at any moment, the
24117cc577a4SJonathan Peyton  * master thread may exit the barrier code and free the team data structure,
24127cc577a4SJonathan Peyton  * and return the threads to the thread pool).
24137cc577a4SJonathan Peyton  *
24147cc577a4SJonathan Peyton  * This does not work with the the tasking code, as the thread is still
24157cc577a4SJonathan Peyton  * expected to participate in the execution of any tasks that may have been
24167cc577a4SJonathan Peyton  * spawned my a member of the team, and the thread still needs access to all
24177cc577a4SJonathan Peyton  * to each thread in the team, so that it can steal work from it.
24187cc577a4SJonathan Peyton  *
24197cc577a4SJonathan Peyton  * Enter the existence of the kmp_task_team_t struct.  It employs a reference
24207cc577a4SJonathan Peyton  * counting mechanims, and is allocated by the master thread before calling
24217cc577a4SJonathan Peyton  * __kmp_<barrier_kind>_release, and then is release by the last thread to
24227cc577a4SJonathan Peyton  * exit __kmp_<barrier_kind>_release at the next barrier.  I.e. the lifetimes
24237cc577a4SJonathan Peyton  * of the kmp_task_team_t structs for consecutive barriers can overlap
24247cc577a4SJonathan Peyton  * (and will, unless the master thread is the last thread to exit the barrier
24257cc577a4SJonathan Peyton  * release phase, which is not typical).
24267cc577a4SJonathan Peyton  *
24277cc577a4SJonathan Peyton  * The existence of such a struct is useful outside the context of tasking,
24287cc577a4SJonathan Peyton  * but for now, I'm trying to keep it specific to the OMP_30_ENABLED macro,
24297cc577a4SJonathan Peyton  * so that any performance differences show up when comparing the 2.5 vs. 3.0
24307cc577a4SJonathan Peyton  * libraries.
24317cc577a4SJonathan Peyton  *
24327cc577a4SJonathan Peyton  * We currently use the existence of the threads array as an indicator that
24337cc577a4SJonathan Peyton  * tasks were spawned since the last barrier.  If the structure is to be
24347cc577a4SJonathan Peyton  * useful outside the context of tasking, then this will have to change, but
24357cc577a4SJonathan Peyton  * not settting the field minimizes the performance impact of tasking on
24367cc577a4SJonathan Peyton  * barriers, when no explicit tasks were spawned (pushed, actually).
24377cc577a4SJonathan Peyton  */
24387cc577a4SJonathan Peyton 
24393041982dSJonathan Peyton static kmp_task_team_t *__kmp_free_task_teams =
24403041982dSJonathan Peyton     NULL; // Free list for task_team data structures
24417cc577a4SJonathan Peyton // Lock for task team data structures
24423041982dSJonathan Peyton static kmp_bootstrap_lock_t __kmp_task_team_lock =
24433041982dSJonathan Peyton     KMP_BOOTSTRAP_LOCK_INITIALIZER(__kmp_task_team_lock);
24447cc577a4SJonathan Peyton 
24457cc577a4SJonathan Peyton // __kmp_alloc_task_deque:
24467cc577a4SJonathan Peyton // Allocates a task deque for a particular thread, and initialize the necessary
24477cc577a4SJonathan Peyton // data structures relating to the deque.  This only happens once per thread
24483041982dSJonathan Peyton // per task team since task teams are recycled. No lock is needed during
24493041982dSJonathan Peyton // allocation since each thread allocates its own deque.
24503041982dSJonathan Peyton static void __kmp_alloc_task_deque(kmp_info_t *thread,
24513041982dSJonathan Peyton                                    kmp_thread_data_t *thread_data) {
24527cc577a4SJonathan Peyton   __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
24537cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(thread_data->td.td_deque == NULL);
24547cc577a4SJonathan Peyton 
24557cc577a4SJonathan Peyton   // Initialize last stolen task field to "none"
24567cc577a4SJonathan Peyton   thread_data->td.td_deque_last_stolen = -1;
24577cc577a4SJonathan Peyton 
24587cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == 0);
24597cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(thread_data->td.td_deque_head == 0);
24607cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(thread_data->td.td_deque_tail == 0);
24617cc577a4SJonathan Peyton 
24623041982dSJonathan Peyton   KE_TRACE(
24633041982dSJonathan Peyton       10,
24643041982dSJonathan Peyton       ("__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n",
24657cc577a4SJonathan Peyton        __kmp_gtid_from_thread(thread), INITIAL_TASK_DEQUE_SIZE, thread_data));
24667cc577a4SJonathan Peyton   // Allocate space for task deque, and zero the deque
24677cc577a4SJonathan Peyton   // Cannot use __kmp_thread_calloc() because threads not around for
24687cc577a4SJonathan Peyton   // kmp_reap_task_team( ).
24693041982dSJonathan Peyton   thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
24703041982dSJonathan Peyton       INITIAL_TASK_DEQUE_SIZE * sizeof(kmp_taskdata_t *));
24717cc577a4SJonathan Peyton   thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
24727cc577a4SJonathan Peyton }
24737cc577a4SJonathan Peyton 
24747cc577a4SJonathan Peyton // __kmp_realloc_task_deque:
24753041982dSJonathan Peyton // Re-allocates a task deque for a particular thread, copies the content from
24763041982dSJonathan Peyton // the old deque and adjusts the necessary data structures relating to the
24773041982dSJonathan Peyton // deque. This operation must be done with a the deque_lock being held
24783041982dSJonathan Peyton static void __kmp_realloc_task_deque(kmp_info_t *thread,
24793041982dSJonathan Peyton                                      kmp_thread_data_t *thread_data) {
24807cc577a4SJonathan Peyton   kmp_int32 size = TASK_DEQUE_SIZE(thread_data->td);
24817cc577a4SJonathan Peyton   kmp_int32 new_size = 2 * size;
24827cc577a4SJonathan Peyton 
24833041982dSJonathan Peyton   KE_TRACE(10, ("__kmp_realloc_task_deque: T#%d reallocating deque[from %d to "
24843041982dSJonathan Peyton                 "%d] for thread_data %p\n",
24857cc577a4SJonathan Peyton                 __kmp_gtid_from_thread(thread), size, new_size, thread_data));
24867cc577a4SJonathan Peyton 
24873041982dSJonathan Peyton   kmp_taskdata_t **new_deque =
24883041982dSJonathan Peyton       (kmp_taskdata_t **)__kmp_allocate(new_size * sizeof(kmp_taskdata_t *));
24897cc577a4SJonathan Peyton 
24907cc577a4SJonathan Peyton   int i, j;
24913041982dSJonathan Peyton   for (i = thread_data->td.td_deque_head, j = 0; j < size;
24923041982dSJonathan Peyton        i = (i + 1) & TASK_DEQUE_MASK(thread_data->td), j++)
24937cc577a4SJonathan Peyton     new_deque[j] = thread_data->td.td_deque[i];
24947cc577a4SJonathan Peyton 
24957cc577a4SJonathan Peyton   __kmp_free(thread_data->td.td_deque);
24967cc577a4SJonathan Peyton 
24977cc577a4SJonathan Peyton   thread_data->td.td_deque_head = 0;
24987cc577a4SJonathan Peyton   thread_data->td.td_deque_tail = size;
24997cc577a4SJonathan Peyton   thread_data->td.td_deque = new_deque;
25007cc577a4SJonathan Peyton   thread_data->td.td_deque_size = new_size;
25017cc577a4SJonathan Peyton }
25027cc577a4SJonathan Peyton 
25037cc577a4SJonathan Peyton // __kmp_free_task_deque:
25043041982dSJonathan Peyton // Deallocates a task deque for a particular thread. Happens at library
25053041982dSJonathan Peyton // deallocation so don't need to reset all thread data fields.
25063041982dSJonathan Peyton static void __kmp_free_task_deque(kmp_thread_data_t *thread_data) {
25077cc577a4SJonathan Peyton   __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
25087cc577a4SJonathan Peyton 
25097cc577a4SJonathan Peyton   if (thread_data->td.td_deque != NULL) {
25107cc577a4SJonathan Peyton     TCW_4(thread_data->td.td_deque_ntasks, 0);
25117cc577a4SJonathan Peyton     __kmp_free(thread_data->td.td_deque);
25127cc577a4SJonathan Peyton     thread_data->td.td_deque = NULL;
25137cc577a4SJonathan Peyton   }
25147cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
25157cc577a4SJonathan Peyton 
25167cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK
25177cc577a4SJonathan Peyton   // GEH: Figure out what to do here for td_susp_tied_tasks
25187cc577a4SJonathan Peyton   if (thread_data->td.td_susp_tied_tasks.ts_entries != TASK_STACK_EMPTY) {
25197cc577a4SJonathan Peyton     __kmp_free_task_stack(__kmp_thread_from_gtid(gtid), thread_data);
25207cc577a4SJonathan Peyton   }
25217cc577a4SJonathan Peyton #endif // BUILD_TIED_TASK_STACK
25227cc577a4SJonathan Peyton }
25237cc577a4SJonathan Peyton 
25247cc577a4SJonathan Peyton // __kmp_realloc_task_threads_data:
25253041982dSJonathan Peyton // Allocates a threads_data array for a task team, either by allocating an
25263041982dSJonathan Peyton // initial array or enlarging an existing array.  Only the first thread to get
25273041982dSJonathan Peyton // the lock allocs or enlarges the array and re-initializes the array eleemnts.
25287cc577a4SJonathan Peyton // That thread returns "TRUE", the rest return "FALSE".
25297cc577a4SJonathan Peyton // Assumes that the new array size is given by task_team -> tt.tt_nproc.
25307cc577a4SJonathan Peyton // The current size is given by task_team -> tt.tt_max_threads.
25313041982dSJonathan Peyton static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
25323041982dSJonathan Peyton                                            kmp_task_team_t *task_team) {
25337cc577a4SJonathan Peyton   kmp_thread_data_t **threads_data_p;
25347cc577a4SJonathan Peyton   kmp_int32 nthreads, maxthreads;
25357cc577a4SJonathan Peyton   int is_init_thread = FALSE;
25367cc577a4SJonathan Peyton 
25377cc577a4SJonathan Peyton   if (TCR_4(task_team->tt.tt_found_tasks)) {
25387cc577a4SJonathan Peyton     // Already reallocated and initialized.
25397cc577a4SJonathan Peyton     return FALSE;
25407cc577a4SJonathan Peyton   }
25417cc577a4SJonathan Peyton 
25427cc577a4SJonathan Peyton   threads_data_p = &task_team->tt.tt_threads_data;
25437cc577a4SJonathan Peyton   nthreads = task_team->tt.tt_nproc;
25447cc577a4SJonathan Peyton   maxthreads = task_team->tt.tt_max_threads;
25457cc577a4SJonathan Peyton 
25463041982dSJonathan Peyton   // All threads must lock when they encounter the first task of the implicit
25473041982dSJonathan Peyton   // task region to make sure threads_data fields are (re)initialized before
25483041982dSJonathan Peyton   // used.
25497cc577a4SJonathan Peyton   __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
25507cc577a4SJonathan Peyton 
25517cc577a4SJonathan Peyton   if (!TCR_4(task_team->tt.tt_found_tasks)) {
25527cc577a4SJonathan Peyton     // first thread to enable tasking
25537cc577a4SJonathan Peyton     kmp_team_t *team = thread->th.th_team;
25547cc577a4SJonathan Peyton     int i;
25557cc577a4SJonathan Peyton 
25567cc577a4SJonathan Peyton     is_init_thread = TRUE;
25577cc577a4SJonathan Peyton     if (maxthreads < nthreads) {
25587cc577a4SJonathan Peyton 
25597cc577a4SJonathan Peyton       if (*threads_data_p != NULL) {
25607cc577a4SJonathan Peyton         kmp_thread_data_t *old_data = *threads_data_p;
25617cc577a4SJonathan Peyton         kmp_thread_data_t *new_data = NULL;
25627cc577a4SJonathan Peyton 
25633041982dSJonathan Peyton         KE_TRACE(
25643041982dSJonathan Peyton             10,
25653041982dSJonathan Peyton             ("__kmp_realloc_task_threads_data: T#%d reallocating "
25667cc577a4SJonathan Peyton              "threads data for task_team %p, new_size = %d, old_size = %d\n",
25673041982dSJonathan Peyton              __kmp_gtid_from_thread(thread), task_team, nthreads, maxthreads));
25687cc577a4SJonathan Peyton         // Reallocate threads_data to have more elements than current array
25697cc577a4SJonathan Peyton         // Cannot use __kmp_thread_realloc() because threads not around for
25707cc577a4SJonathan Peyton         // kmp_reap_task_team( ).  Note all new array entries are initialized
25717cc577a4SJonathan Peyton         // to zero by __kmp_allocate().
25723041982dSJonathan Peyton         new_data = (kmp_thread_data_t *)__kmp_allocate(
25733041982dSJonathan Peyton             nthreads * sizeof(kmp_thread_data_t));
25747cc577a4SJonathan Peyton         // copy old data to new data
25757cc577a4SJonathan Peyton         KMP_MEMCPY_S((void *)new_data, nthreads * sizeof(kmp_thread_data_t),
257671483f2dSAndrey Churbanov                      (void *)old_data, maxthreads * sizeof(kmp_thread_data_t));
25777cc577a4SJonathan Peyton 
25787cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK
25797cc577a4SJonathan Peyton         // GEH: Figure out if this is the right thing to do
25807cc577a4SJonathan Peyton         for (i = maxthreads; i < nthreads; i++) {
25817cc577a4SJonathan Peyton           kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
25827cc577a4SJonathan Peyton           __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
25837cc577a4SJonathan Peyton         }
25847cc577a4SJonathan Peyton #endif // BUILD_TIED_TASK_STACK
25857cc577a4SJonathan Peyton         // Install the new data and free the old data
25867cc577a4SJonathan Peyton         (*threads_data_p) = new_data;
25877cc577a4SJonathan Peyton         __kmp_free(old_data);
25883041982dSJonathan Peyton       } else {
25897cc577a4SJonathan Peyton         KE_TRACE(10, ("__kmp_realloc_task_threads_data: T#%d allocating "
25907cc577a4SJonathan Peyton                       "threads data for task_team %p, size = %d\n",
25917cc577a4SJonathan Peyton                       __kmp_gtid_from_thread(thread), task_team, nthreads));
25927cc577a4SJonathan Peyton         // Make the initial allocate for threads_data array, and zero entries
25937cc577a4SJonathan Peyton         // Cannot use __kmp_thread_calloc() because threads not around for
25947cc577a4SJonathan Peyton         // kmp_reap_task_team( ).
25957cc577a4SJonathan Peyton         ANNOTATE_IGNORE_WRITES_BEGIN();
25963041982dSJonathan Peyton         *threads_data_p = (kmp_thread_data_t *)__kmp_allocate(
25973041982dSJonathan Peyton             nthreads * sizeof(kmp_thread_data_t));
25987cc577a4SJonathan Peyton         ANNOTATE_IGNORE_WRITES_END();
25997cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK
26007cc577a4SJonathan Peyton         // GEH: Figure out if this is the right thing to do
26017cc577a4SJonathan Peyton         for (i = 0; i < nthreads; i++) {
26027cc577a4SJonathan Peyton           kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
26037cc577a4SJonathan Peyton           __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
26047cc577a4SJonathan Peyton         }
26057cc577a4SJonathan Peyton #endif // BUILD_TIED_TASK_STACK
26067cc577a4SJonathan Peyton       }
26077cc577a4SJonathan Peyton       task_team->tt.tt_max_threads = nthreads;
26083041982dSJonathan Peyton     } else {
26097cc577a4SJonathan Peyton       // If array has (more than) enough elements, go ahead and use it
26107cc577a4SJonathan Peyton       KMP_DEBUG_ASSERT(*threads_data_p != NULL);
26117cc577a4SJonathan Peyton     }
26127cc577a4SJonathan Peyton 
26137cc577a4SJonathan Peyton     // initialize threads_data pointers back to thread_info structures
26147cc577a4SJonathan Peyton     for (i = 0; i < nthreads; i++) {
26157cc577a4SJonathan Peyton       kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
26167cc577a4SJonathan Peyton       thread_data->td.td_thr = team->t.t_threads[i];
26177cc577a4SJonathan Peyton 
26187cc577a4SJonathan Peyton       if (thread_data->td.td_deque_last_stolen >= nthreads) {
26197cc577a4SJonathan Peyton         // The last stolen field survives across teams / barrier, and the number
26207cc577a4SJonathan Peyton         // of threads may have changed.  It's possible (likely?) that a new
26213041982dSJonathan Peyton         // parallel region will exhibit the same behavior as previous region.
26227cc577a4SJonathan Peyton         thread_data->td.td_deque_last_stolen = -1;
26237cc577a4SJonathan Peyton       }
26247cc577a4SJonathan Peyton     }
26257cc577a4SJonathan Peyton 
26267cc577a4SJonathan Peyton     KMP_MB();
26277cc577a4SJonathan Peyton     TCW_SYNC_4(task_team->tt.tt_found_tasks, TRUE);
26287cc577a4SJonathan Peyton   }
26297cc577a4SJonathan Peyton 
26307cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
26317cc577a4SJonathan Peyton   return is_init_thread;
26327cc577a4SJonathan Peyton }
26337cc577a4SJonathan Peyton 
26347cc577a4SJonathan Peyton // __kmp_free_task_threads_data:
26357cc577a4SJonathan Peyton // Deallocates a threads_data array for a task team, including any attached
26367cc577a4SJonathan Peyton // tasking deques.  Only occurs at library shutdown.
26373041982dSJonathan Peyton static void __kmp_free_task_threads_data(kmp_task_team_t *task_team) {
26387cc577a4SJonathan Peyton   __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
26397cc577a4SJonathan Peyton   if (task_team->tt.tt_threads_data != NULL) {
26407cc577a4SJonathan Peyton     int i;
26417cc577a4SJonathan Peyton     for (i = 0; i < task_team->tt.tt_max_threads; i++) {
26427cc577a4SJonathan Peyton       __kmp_free_task_deque(&task_team->tt.tt_threads_data[i]);
26437cc577a4SJonathan Peyton     }
26447cc577a4SJonathan Peyton     __kmp_free(task_team->tt.tt_threads_data);
26457cc577a4SJonathan Peyton     task_team->tt.tt_threads_data = NULL;
26467cc577a4SJonathan Peyton   }
26477cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
26487cc577a4SJonathan Peyton }
26497cc577a4SJonathan Peyton 
26507cc577a4SJonathan Peyton // __kmp_allocate_task_team:
26517cc577a4SJonathan Peyton // Allocates a task team associated with a specific team, taking it from
26523041982dSJonathan Peyton // the global task team free list if possible.  Also initializes data
26533041982dSJonathan Peyton // structures.
26543041982dSJonathan Peyton static kmp_task_team_t *__kmp_allocate_task_team(kmp_info_t *thread,
26553041982dSJonathan Peyton                                                  kmp_team_t *team) {
26567cc577a4SJonathan Peyton   kmp_task_team_t *task_team = NULL;
26577cc577a4SJonathan Peyton   int nthreads;
26587cc577a4SJonathan Peyton 
26597cc577a4SJonathan Peyton   KA_TRACE(20, ("__kmp_allocate_task_team: T#%d entering; team = %p\n",
26607cc577a4SJonathan Peyton                 (thread ? __kmp_gtid_from_thread(thread) : -1), team));
26617cc577a4SJonathan Peyton 
26627cc577a4SJonathan Peyton   if (TCR_PTR(__kmp_free_task_teams) != NULL) {
26637cc577a4SJonathan Peyton     // Take a task team from the task team pool
26647cc577a4SJonathan Peyton     __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
26657cc577a4SJonathan Peyton     if (__kmp_free_task_teams != NULL) {
26667cc577a4SJonathan Peyton       task_team = __kmp_free_task_teams;
26677cc577a4SJonathan Peyton       TCW_PTR(__kmp_free_task_teams, task_team->tt.tt_next);
26687cc577a4SJonathan Peyton       task_team->tt.tt_next = NULL;
26697cc577a4SJonathan Peyton     }
26707cc577a4SJonathan Peyton     __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
26717cc577a4SJonathan Peyton   }
26727cc577a4SJonathan Peyton 
26737cc577a4SJonathan Peyton   if (task_team == NULL) {
26747cc577a4SJonathan Peyton     KE_TRACE(10, ("__kmp_allocate_task_team: T#%d allocating "
26757cc577a4SJonathan Peyton                   "task team for team %p\n",
26767cc577a4SJonathan Peyton                   __kmp_gtid_from_thread(thread), team));
26777cc577a4SJonathan Peyton     // Allocate a new task team if one is not available.
26787cc577a4SJonathan Peyton     // Cannot use __kmp_thread_malloc() because threads not around for
26797cc577a4SJonathan Peyton     // kmp_reap_task_team( ).
26807cc577a4SJonathan Peyton     task_team = (kmp_task_team_t *)__kmp_allocate(sizeof(kmp_task_team_t));
26817cc577a4SJonathan Peyton     __kmp_init_bootstrap_lock(&task_team->tt.tt_threads_lock);
26823041982dSJonathan Peyton     // AC: __kmp_allocate zeroes returned memory
26833041982dSJonathan Peyton     // task_team -> tt.tt_threads_data = NULL;
26847cc577a4SJonathan Peyton     // task_team -> tt.tt_max_threads = 0;
26857cc577a4SJonathan Peyton     // task_team -> tt.tt_next = NULL;
26867cc577a4SJonathan Peyton   }
26877cc577a4SJonathan Peyton 
26887cc577a4SJonathan Peyton   TCW_4(task_team->tt.tt_found_tasks, FALSE);
26897cc577a4SJonathan Peyton #if OMP_45_ENABLED
26907cc577a4SJonathan Peyton   TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
26917cc577a4SJonathan Peyton #endif
26927cc577a4SJonathan Peyton   task_team->tt.tt_nproc = nthreads = team->t.t_nproc;
26937cc577a4SJonathan Peyton 
26947cc577a4SJonathan Peyton   TCW_4(task_team->tt.tt_unfinished_threads, nthreads);
26957cc577a4SJonathan Peyton   TCW_4(task_team->tt.tt_active, TRUE);
26967cc577a4SJonathan Peyton 
26973041982dSJonathan Peyton   KA_TRACE(20, ("__kmp_allocate_task_team: T#%d exiting; task_team = %p "
26983041982dSJonathan Peyton                 "unfinished_threads init'd to %d\n",
26993041982dSJonathan Peyton                 (thread ? __kmp_gtid_from_thread(thread) : -1), task_team,
27003041982dSJonathan Peyton                 task_team->tt.tt_unfinished_threads));
27017cc577a4SJonathan Peyton   return task_team;
27027cc577a4SJonathan Peyton }
27037cc577a4SJonathan Peyton 
27047cc577a4SJonathan Peyton // __kmp_free_task_team:
27057cc577a4SJonathan Peyton // Frees the task team associated with a specific thread, and adds it
27067cc577a4SJonathan Peyton // to the global task team free list.
27073041982dSJonathan Peyton void __kmp_free_task_team(kmp_info_t *thread, kmp_task_team_t *task_team) {
27087cc577a4SJonathan Peyton   KA_TRACE(20, ("__kmp_free_task_team: T#%d task_team = %p\n",
27097cc577a4SJonathan Peyton                 thread ? __kmp_gtid_from_thread(thread) : -1, task_team));
27107cc577a4SJonathan Peyton 
27117cc577a4SJonathan Peyton   // Put task team back on free list
27127cc577a4SJonathan Peyton   __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
27137cc577a4SJonathan Peyton 
27147cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task_team->tt.tt_next == NULL);
27157cc577a4SJonathan Peyton   task_team->tt.tt_next = __kmp_free_task_teams;
27167cc577a4SJonathan Peyton   TCW_PTR(__kmp_free_task_teams, task_team);
27177cc577a4SJonathan Peyton 
27187cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
27197cc577a4SJonathan Peyton }
27207cc577a4SJonathan Peyton 
27217cc577a4SJonathan Peyton // __kmp_reap_task_teams:
27227cc577a4SJonathan Peyton // Free all the task teams on the task team free list.
27237cc577a4SJonathan Peyton // Should only be done during library shutdown.
27243041982dSJonathan Peyton // Cannot do anything that needs a thread structure or gtid since they are
27253041982dSJonathan Peyton // already gone.
27263041982dSJonathan Peyton void __kmp_reap_task_teams(void) {
27277cc577a4SJonathan Peyton   kmp_task_team_t *task_team;
27287cc577a4SJonathan Peyton 
27297cc577a4SJonathan Peyton   if (TCR_PTR(__kmp_free_task_teams) != NULL) {
27307cc577a4SJonathan Peyton     // Free all task_teams on the free list
27317cc577a4SJonathan Peyton     __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
27327cc577a4SJonathan Peyton     while ((task_team = __kmp_free_task_teams) != NULL) {
27337cc577a4SJonathan Peyton       __kmp_free_task_teams = task_team->tt.tt_next;
27347cc577a4SJonathan Peyton       task_team->tt.tt_next = NULL;
27357cc577a4SJonathan Peyton 
27367cc577a4SJonathan Peyton       // Free threads_data if necessary
27377cc577a4SJonathan Peyton       if (task_team->tt.tt_threads_data != NULL) {
27387cc577a4SJonathan Peyton         __kmp_free_task_threads_data(task_team);
27397cc577a4SJonathan Peyton       }
27407cc577a4SJonathan Peyton       __kmp_free(task_team);
27417cc577a4SJonathan Peyton     }
27427cc577a4SJonathan Peyton     __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
27437cc577a4SJonathan Peyton   }
27447cc577a4SJonathan Peyton }
27457cc577a4SJonathan Peyton 
27467cc577a4SJonathan Peyton // __kmp_wait_to_unref_task_teams:
27477cc577a4SJonathan Peyton // Some threads could still be in the fork barrier release code, possibly
27487cc577a4SJonathan Peyton // trying to steal tasks.  Wait for each thread to unreference its task team.
27493041982dSJonathan Peyton void __kmp_wait_to_unref_task_teams(void) {
27507cc577a4SJonathan Peyton   kmp_info_t *thread;
27517cc577a4SJonathan Peyton   kmp_uint32 spins;
27527cc577a4SJonathan Peyton   int done;
27537cc577a4SJonathan Peyton 
27547cc577a4SJonathan Peyton   KMP_INIT_YIELD(spins);
27557cc577a4SJonathan Peyton 
27567cc577a4SJonathan Peyton   for (;;) {
27577cc577a4SJonathan Peyton     done = TRUE;
27587cc577a4SJonathan Peyton 
27597cc577a4SJonathan Peyton     // TODO: GEH - this may be is wrong because some sync would be necessary
27603041982dSJonathan Peyton     // in case threads are added to the pool during the traversal. Need to
27613041982dSJonathan Peyton     // verify that lock for thread pool is held when calling this routine.
2762c47afcd9SAndrey Churbanov     for (thread = CCAST(kmp_info_t *, __kmp_thread_pool); thread != NULL;
27633041982dSJonathan Peyton          thread = thread->th.th_next_pool) {
27647cc577a4SJonathan Peyton #if KMP_OS_WINDOWS
27657cc577a4SJonathan Peyton       DWORD exit_val;
27667cc577a4SJonathan Peyton #endif
27677cc577a4SJonathan Peyton       if (TCR_PTR(thread->th.th_task_team) == NULL) {
27687cc577a4SJonathan Peyton         KA_TRACE(10, ("__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n",
27697cc577a4SJonathan Peyton                       __kmp_gtid_from_thread(thread)));
27707cc577a4SJonathan Peyton         continue;
27717cc577a4SJonathan Peyton       }
27727cc577a4SJonathan Peyton #if KMP_OS_WINDOWS
27737cc577a4SJonathan Peyton       // TODO: GEH - add this check for Linux* OS / OS X* as well?
27747cc577a4SJonathan Peyton       if (!__kmp_is_thread_alive(thread, &exit_val)) {
27757cc577a4SJonathan Peyton         thread->th.th_task_team = NULL;
27767cc577a4SJonathan Peyton         continue;
27777cc577a4SJonathan Peyton       }
27787cc577a4SJonathan Peyton #endif
27797cc577a4SJonathan Peyton 
27807cc577a4SJonathan Peyton       done = FALSE; // Because th_task_team pointer is not NULL for this thread
27817cc577a4SJonathan Peyton 
27823041982dSJonathan Peyton       KA_TRACE(10, ("__kmp_wait_to_unref_task_team: Waiting for T#%d to "
27833041982dSJonathan Peyton                     "unreference task_team\n",
27847cc577a4SJonathan Peyton                     __kmp_gtid_from_thread(thread)));
27857cc577a4SJonathan Peyton 
27867cc577a4SJonathan Peyton       if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
27877cc577a4SJonathan Peyton         volatile void *sleep_loc;
27887cc577a4SJonathan Peyton         // If the thread is sleeping, awaken it.
2789c47afcd9SAndrey Churbanov         if ((sleep_loc = TCR_PTR(CCAST(void *, thread->th.th_sleep_loc))) !=
2790c47afcd9SAndrey Churbanov             NULL) {
27913041982dSJonathan Peyton           KA_TRACE(
27923041982dSJonathan Peyton               10,
27933041982dSJonathan Peyton               ("__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n",
27947cc577a4SJonathan Peyton                __kmp_gtid_from_thread(thread), __kmp_gtid_from_thread(thread)));
27957cc577a4SJonathan Peyton           __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc);
27967cc577a4SJonathan Peyton         }
27977cc577a4SJonathan Peyton       }
27987cc577a4SJonathan Peyton     }
27997cc577a4SJonathan Peyton     if (done) {
28007cc577a4SJonathan Peyton       break;
28017cc577a4SJonathan Peyton     }
28027cc577a4SJonathan Peyton 
28033041982dSJonathan Peyton     // If we are oversubscribed, or have waited a bit (and library mode is
28043041982dSJonathan Peyton     // throughput), yield. Pause is in the following code.
28057cc577a4SJonathan Peyton     KMP_YIELD(TCR_4(__kmp_nth) > __kmp_avail_proc);
28067cc577a4SJonathan Peyton     KMP_YIELD_SPIN(spins); // Yields only if KMP_LIBRARY=throughput
28077cc577a4SJonathan Peyton   }
28087cc577a4SJonathan Peyton }
28097cc577a4SJonathan Peyton 
28107cc577a4SJonathan Peyton // __kmp_task_team_setup:  Create a task_team for the current team, but use
28117cc577a4SJonathan Peyton // an already created, unused one if it already exists.
28123041982dSJonathan Peyton void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team, int always) {
28137cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
28147cc577a4SJonathan Peyton 
28153041982dSJonathan Peyton   // If this task_team hasn't been created yet, allocate it. It will be used in
28163041982dSJonathan Peyton   // the region after the next.
28173041982dSJonathan Peyton   // If it exists, it is the current task team and shouldn't be touched yet as
28183041982dSJonathan Peyton   // it may still be in use.
28193041982dSJonathan Peyton   if (team->t.t_task_team[this_thr->th.th_task_state] == NULL &&
28203041982dSJonathan Peyton       (always || team->t.t_nproc > 1)) {
28213041982dSJonathan Peyton     team->t.t_task_team[this_thr->th.th_task_state] =
28223041982dSJonathan Peyton         __kmp_allocate_task_team(this_thr, team);
28233041982dSJonathan Peyton     KA_TRACE(20, ("__kmp_task_team_setup: Master T#%d created new task_team %p "
28243041982dSJonathan Peyton                   "for team %d at parity=%d\n",
28253041982dSJonathan Peyton                   __kmp_gtid_from_thread(this_thr),
28263041982dSJonathan Peyton                   team->t.t_task_team[this_thr->th.th_task_state],
28273041982dSJonathan Peyton                   ((team != NULL) ? team->t.t_id : -1),
28283041982dSJonathan Peyton                   this_thr->th.th_task_state));
28297cc577a4SJonathan Peyton   }
28307cc577a4SJonathan Peyton 
28313041982dSJonathan Peyton   // After threads exit the release, they will call sync, and then point to this
28323041982dSJonathan Peyton   // other task_team; make sure it is allocated and properly initialized. As
28333041982dSJonathan Peyton   // threads spin in the barrier release phase, they will continue to use the
28343041982dSJonathan Peyton   // previous task_team struct(above), until they receive the signal to stop
28353041982dSJonathan Peyton   // checking for tasks (they can't safely reference the kmp_team_t struct,
28363041982dSJonathan Peyton   // which could be reallocated by the master thread). No task teams are formed
28373041982dSJonathan Peyton   // for serialized teams.
28387cc577a4SJonathan Peyton   if (team->t.t_nproc > 1) {
28397cc577a4SJonathan Peyton     int other_team = 1 - this_thr->th.th_task_state;
28407cc577a4SJonathan Peyton     if (team->t.t_task_team[other_team] == NULL) { // setup other team as well
28413041982dSJonathan Peyton       team->t.t_task_team[other_team] =
28423041982dSJonathan Peyton           __kmp_allocate_task_team(this_thr, team);
28433041982dSJonathan Peyton       KA_TRACE(20, ("__kmp_task_team_setup: Master T#%d created second new "
28443041982dSJonathan Peyton                     "task_team %p for team %d at parity=%d\n",
28453041982dSJonathan Peyton                     __kmp_gtid_from_thread(this_thr),
28463041982dSJonathan Peyton                     team->t.t_task_team[other_team],
28477cc577a4SJonathan Peyton                     ((team != NULL) ? team->t.t_id : -1), other_team));
28483041982dSJonathan Peyton     } else { // Leave the old task team struct in place for the upcoming region;
28493041982dSJonathan Peyton       // adjust as needed
28507cc577a4SJonathan Peyton       kmp_task_team_t *task_team = team->t.t_task_team[other_team];
28513041982dSJonathan Peyton       if (!task_team->tt.tt_active ||
28523041982dSJonathan Peyton           team->t.t_nproc != task_team->tt.tt_nproc) {
28537cc577a4SJonathan Peyton         TCW_4(task_team->tt.tt_nproc, team->t.t_nproc);
28547cc577a4SJonathan Peyton         TCW_4(task_team->tt.tt_found_tasks, FALSE);
28557cc577a4SJonathan Peyton #if OMP_45_ENABLED
28567cc577a4SJonathan Peyton         TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
28577cc577a4SJonathan Peyton #endif
28587cc577a4SJonathan Peyton         TCW_4(task_team->tt.tt_unfinished_threads, team->t.t_nproc);
28597cc577a4SJonathan Peyton         TCW_4(task_team->tt.tt_active, TRUE);
28607cc577a4SJonathan Peyton       }
28613041982dSJonathan Peyton       // if team size has changed, the first thread to enable tasking will
28623041982dSJonathan Peyton       // realloc threads_data if necessary
28633041982dSJonathan Peyton       KA_TRACE(20, ("__kmp_task_team_setup: Master T#%d reset next task_team "
28643041982dSJonathan Peyton                     "%p for team %d at parity=%d\n",
28653041982dSJonathan Peyton                     __kmp_gtid_from_thread(this_thr),
28663041982dSJonathan Peyton                     team->t.t_task_team[other_team],
28677cc577a4SJonathan Peyton                     ((team != NULL) ? team->t.t_id : -1), other_team));
28687cc577a4SJonathan Peyton     }
28697cc577a4SJonathan Peyton   }
28707cc577a4SJonathan Peyton }
28717cc577a4SJonathan Peyton 
28727cc577a4SJonathan Peyton // __kmp_task_team_sync: Propagation of task team data from team to threads
28737cc577a4SJonathan Peyton // which happens just after the release phase of a team barrier.  This may be
28747cc577a4SJonathan Peyton // called by any thread, but only for teams with # threads > 1.
28753041982dSJonathan Peyton void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team) {
28767cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
28777cc577a4SJonathan Peyton 
28783041982dSJonathan Peyton   // Toggle the th_task_state field, to switch which task_team this thread
28793041982dSJonathan Peyton   // refers to
28807cc577a4SJonathan Peyton   this_thr->th.th_task_state = 1 - this_thr->th.th_task_state;
28813041982dSJonathan Peyton   // It is now safe to propagate the task team pointer from the team struct to
28823041982dSJonathan Peyton   // the current thread.
28833041982dSJonathan Peyton   TCW_PTR(this_thr->th.th_task_team,
28843041982dSJonathan Peyton           team->t.t_task_team[this_thr->th.th_task_state]);
28853041982dSJonathan Peyton   KA_TRACE(20,
28863041982dSJonathan Peyton            ("__kmp_task_team_sync: Thread T#%d task team switched to task_team "
28873041982dSJonathan Peyton             "%p from Team #%d (parity=%d)\n",
28887cc577a4SJonathan Peyton             __kmp_gtid_from_thread(this_thr), this_thr->th.th_task_team,
28897cc577a4SJonathan Peyton             ((team != NULL) ? team->t.t_id : -1), this_thr->th.th_task_state));
28907cc577a4SJonathan Peyton }
28917cc577a4SJonathan Peyton 
28923041982dSJonathan Peyton // __kmp_task_team_wait: Master thread waits for outstanding tasks after the
28933041982dSJonathan Peyton // barrier gather phase. Only called by master thread if #threads in team > 1 or
28943041982dSJonathan Peyton // if proxy tasks were created.
28953041982dSJonathan Peyton //
28963041982dSJonathan Peyton // wait is a flag that defaults to 1 (see kmp.h), but waiting can be turned off
28973041982dSJonathan Peyton // by passing in 0 optionally as the last argument. When wait is zero, master
28983041982dSJonathan Peyton // thread does not wait for unfinished_threads to reach 0.
28993041982dSJonathan Peyton void __kmp_task_team_wait(
29003041982dSJonathan Peyton     kmp_info_t *this_thr,
29013041982dSJonathan Peyton     kmp_team_t *team USE_ITT_BUILD_ARG(void *itt_sync_obj), int wait) {
29027cc577a4SJonathan Peyton   kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state];
29037cc577a4SJonathan Peyton 
29047cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
29057cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task_team == this_thr->th.th_task_team);
29067cc577a4SJonathan Peyton 
29077cc577a4SJonathan Peyton   if ((task_team != NULL) && KMP_TASKING_ENABLED(task_team)) {
29087cc577a4SJonathan Peyton     if (wait) {
29093041982dSJonathan Peyton       KA_TRACE(20, ("__kmp_task_team_wait: Master T#%d waiting for all tasks "
29103041982dSJonathan Peyton                     "(for unfinished_threads to reach 0) on task_team = %p\n",
29117cc577a4SJonathan Peyton                     __kmp_gtid_from_thread(this_thr), task_team));
29123041982dSJonathan Peyton       // Worker threads may have dropped through to release phase, but could
29133041982dSJonathan Peyton       // still be executing tasks. Wait here for tasks to complete. To avoid
29143041982dSJonathan Peyton       // memory contention, only master thread checks termination condition.
2915c47afcd9SAndrey Churbanov       kmp_flag_32 flag(
2916c47afcd9SAndrey Churbanov           RCAST(volatile kmp_uint32 *, &task_team->tt.tt_unfinished_threads),
2917c47afcd9SAndrey Churbanov           0U);
29183041982dSJonathan Peyton       flag.wait(this_thr, TRUE USE_ITT_BUILD_ARG(itt_sync_obj));
29197cc577a4SJonathan Peyton     }
29203041982dSJonathan Peyton     // Deactivate the old task team, so that the worker threads will stop
29213041982dSJonathan Peyton     // referencing it while spinning.
29223041982dSJonathan Peyton     KA_TRACE(
29233041982dSJonathan Peyton         20,
29243041982dSJonathan Peyton         ("__kmp_task_team_wait: Master T#%d deactivating task_team %p: "
29257cc577a4SJonathan Peyton          "setting active to false, setting local and team's pointer to NULL\n",
29267cc577a4SJonathan Peyton          __kmp_gtid_from_thread(this_thr), task_team));
29277cc577a4SJonathan Peyton #if OMP_45_ENABLED
29283041982dSJonathan Peyton     KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1 ||
29293041982dSJonathan Peyton                      task_team->tt.tt_found_proxy_tasks == TRUE);
29307cc577a4SJonathan Peyton     TCW_SYNC_4(task_team->tt.tt_found_proxy_tasks, FALSE);
29317cc577a4SJonathan Peyton #else
29327cc577a4SJonathan Peyton     KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1);
29337cc577a4SJonathan Peyton #endif
29347cc577a4SJonathan Peyton     TCW_SYNC_4(task_team->tt.tt_active, FALSE);
29357cc577a4SJonathan Peyton     KMP_MB();
29367cc577a4SJonathan Peyton 
29377cc577a4SJonathan Peyton     TCW_PTR(this_thr->th.th_task_team, NULL);
29387cc577a4SJonathan Peyton   }
29397cc577a4SJonathan Peyton }
29407cc577a4SJonathan Peyton 
29417cc577a4SJonathan Peyton // __kmp_tasking_barrier:
29427cc577a4SJonathan Peyton // This routine may only called when __kmp_tasking_mode == tskm_extra_barrier.
29433041982dSJonathan Peyton // Internal function to execute all tasks prior to a regular barrier or a join
29443041982dSJonathan Peyton // barrier. It is a full barrier itself, which unfortunately turns regular
29453041982dSJonathan Peyton // barriers into double barriers and join barriers into 1 1/2 barriers.
29463041982dSJonathan Peyton void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread, int gtid) {
2947c47afcd9SAndrey Churbanov   volatile kmp_uint32 *spin = RCAST(
2948c47afcd9SAndrey Churbanov       volatile kmp_uint32 *,
2949c47afcd9SAndrey Churbanov       &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads);
29507cc577a4SJonathan Peyton   int flag = FALSE;
29517cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode == tskm_extra_barrier);
29527cc577a4SJonathan Peyton 
29537cc577a4SJonathan Peyton #if USE_ITT_BUILD
29547cc577a4SJonathan Peyton   KMP_FSYNC_SPIN_INIT(spin, (kmp_uint32 *)NULL);
29557cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
29567cc577a4SJonathan Peyton   kmp_flag_32 spin_flag(spin, 0U);
29573041982dSJonathan Peyton   while (!spin_flag.execute_tasks(thread, gtid, TRUE,
29583041982dSJonathan Peyton                                   &flag USE_ITT_BUILD_ARG(NULL), 0)) {
29597cc577a4SJonathan Peyton #if USE_ITT_BUILD
29607cc577a4SJonathan Peyton     // TODO: What about itt_sync_obj??
29615ba90c79SAndrey Churbanov     KMP_FSYNC_SPIN_PREPARE(CCAST(kmp_uint32 *, spin));
29627cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
29637cc577a4SJonathan Peyton 
29647cc577a4SJonathan Peyton     if (TCR_4(__kmp_global.g.g_done)) {
29657cc577a4SJonathan Peyton       if (__kmp_global.g.g_abort)
29667cc577a4SJonathan Peyton         __kmp_abort_thread();
29677cc577a4SJonathan Peyton       break;
29687cc577a4SJonathan Peyton     }
29697cc577a4SJonathan Peyton     KMP_YIELD(TRUE); // GH: We always yield here
29707cc577a4SJonathan Peyton   }
29717cc577a4SJonathan Peyton #if USE_ITT_BUILD
29725ba90c79SAndrey Churbanov   KMP_FSYNC_SPIN_ACQUIRED(CCAST(kmp_uint32 *, spin));
29737cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
29747cc577a4SJonathan Peyton }
29757cc577a4SJonathan Peyton 
29767cc577a4SJonathan Peyton #if OMP_45_ENABLED
29777cc577a4SJonathan Peyton 
29783041982dSJonathan Peyton // __kmp_give_task puts a task into a given thread queue if:
29793041982dSJonathan Peyton //  - the queue for that thread was created
29803041982dSJonathan Peyton //  - there's space in that queue
29813041982dSJonathan Peyton // Because of this, __kmp_push_task needs to check if there's space after
29823041982dSJonathan Peyton // getting the lock
29833041982dSJonathan Peyton static bool __kmp_give_task(kmp_info_t *thread, kmp_int32 tid, kmp_task_t *task,
29843041982dSJonathan Peyton                             kmp_int32 pass) {
29857cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
29867cc577a4SJonathan Peyton   kmp_task_team_t *task_team = taskdata->td_task_team;
29877cc577a4SJonathan Peyton 
29883041982dSJonathan Peyton   KA_TRACE(20, ("__kmp_give_task: trying to give task %p to thread %d.\n",
29893041982dSJonathan Peyton                 taskdata, tid));
29907cc577a4SJonathan Peyton 
29917cc577a4SJonathan Peyton   // If task_team is NULL something went really bad...
29927cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task_team != NULL);
29937cc577a4SJonathan Peyton 
29947cc577a4SJonathan Peyton   bool result = false;
29957cc577a4SJonathan Peyton   kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
29967cc577a4SJonathan Peyton 
29977cc577a4SJonathan Peyton   if (thread_data->td.td_deque == NULL) {
29987cc577a4SJonathan Peyton     // There's no queue in this thread, go find another one
29997cc577a4SJonathan Peyton     // We're guaranteed that at least one thread has a queue
30003041982dSJonathan Peyton     KA_TRACE(30,
30013041982dSJonathan Peyton              ("__kmp_give_task: thread %d has no queue while giving task %p.\n",
30023041982dSJonathan Peyton               tid, taskdata));
30037cc577a4SJonathan Peyton     return result;
30047cc577a4SJonathan Peyton   }
30057cc577a4SJonathan Peyton 
30063041982dSJonathan Peyton   if (TCR_4(thread_data->td.td_deque_ntasks) >=
30073041982dSJonathan Peyton       TASK_DEQUE_SIZE(thread_data->td)) {
30083041982dSJonathan Peyton     KA_TRACE(
30093041982dSJonathan Peyton         30,
30103041982dSJonathan Peyton         ("__kmp_give_task: queue is full while giving task %p to thread %d.\n",
30113041982dSJonathan Peyton          taskdata, tid));
30127cc577a4SJonathan Peyton 
30133041982dSJonathan Peyton     // if this deque is bigger than the pass ratio give a chance to another
30143041982dSJonathan Peyton     // thread
30153041982dSJonathan Peyton     if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
30163041982dSJonathan Peyton       return result;
30177cc577a4SJonathan Peyton 
30187cc577a4SJonathan Peyton     __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
30197cc577a4SJonathan Peyton     __kmp_realloc_task_deque(thread, thread_data);
30207cc577a4SJonathan Peyton 
30217cc577a4SJonathan Peyton   } else {
30227cc577a4SJonathan Peyton 
30237cc577a4SJonathan Peyton     __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
30247cc577a4SJonathan Peyton 
30253041982dSJonathan Peyton     if (TCR_4(thread_data->td.td_deque_ntasks) >=
30263041982dSJonathan Peyton         TASK_DEQUE_SIZE(thread_data->td)) {
30273041982dSJonathan Peyton       KA_TRACE(30, ("__kmp_give_task: queue is full while giving task %p to "
30283041982dSJonathan Peyton                     "thread %d.\n",
30293041982dSJonathan Peyton                     taskdata, tid));
30307cc577a4SJonathan Peyton 
30313041982dSJonathan Peyton       // if this deque is bigger than the pass ratio give a chance to another
30323041982dSJonathan Peyton       // thread
30337cc577a4SJonathan Peyton       if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
30347cc577a4SJonathan Peyton         goto release_and_exit;
30357cc577a4SJonathan Peyton 
30367cc577a4SJonathan Peyton       __kmp_realloc_task_deque(thread, thread_data);
30377cc577a4SJonathan Peyton     }
30387cc577a4SJonathan Peyton   }
30397cc577a4SJonathan Peyton 
30407cc577a4SJonathan Peyton   // lock is held here, and there is space in the deque
30417cc577a4SJonathan Peyton 
30427cc577a4SJonathan Peyton   thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
30437cc577a4SJonathan Peyton   // Wrap index.
30443041982dSJonathan Peyton   thread_data->td.td_deque_tail =
30453041982dSJonathan Peyton       (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
30463041982dSJonathan Peyton   TCW_4(thread_data->td.td_deque_ntasks,
30473041982dSJonathan Peyton         TCR_4(thread_data->td.td_deque_ntasks) + 1);
30487cc577a4SJonathan Peyton 
30497cc577a4SJonathan Peyton   result = true;
30503041982dSJonathan Peyton   KA_TRACE(30, ("__kmp_give_task: successfully gave task %p to thread %d.\n",
30513041982dSJonathan Peyton                 taskdata, tid));
30527cc577a4SJonathan Peyton 
30537cc577a4SJonathan Peyton release_and_exit:
30547cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
30557cc577a4SJonathan Peyton 
30567cc577a4SJonathan Peyton   return result;
30577cc577a4SJonathan Peyton }
30587cc577a4SJonathan Peyton 
30593041982dSJonathan Peyton /* The finish of the proxy tasks is divided in two pieces:
30607cc577a4SJonathan Peyton     - the top half is the one that can be done from a thread outside the team
30617cc577a4SJonathan Peyton     - the bottom half must be run from a them within the team
30627cc577a4SJonathan Peyton 
30633041982dSJonathan Peyton    In order to run the bottom half the task gets queued back into one of the
30643041982dSJonathan Peyton    threads of the team. Once the td_incomplete_child_task counter of the parent
30653041982dSJonathan Peyton    is decremented the threads can leave the barriers. So, the bottom half needs
30663041982dSJonathan Peyton    to be queued before the counter is decremented. The top half is therefore
30673041982dSJonathan Peyton    divided in two parts:
30687cc577a4SJonathan Peyton     - things that can be run before queuing the bottom half
30697cc577a4SJonathan Peyton     - things that must be run after queuing the bottom half
30707cc577a4SJonathan Peyton 
30713041982dSJonathan Peyton    This creates a second race as the bottom half can free the task before the
30723041982dSJonathan Peyton    second top half is executed. To avoid this we use the
30733041982dSJonathan Peyton    td_incomplete_child_task of the proxy task to synchronize the top and bottom
30743041982dSJonathan Peyton    half. */
30753041982dSJonathan Peyton static void __kmp_first_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
30767cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
30777cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
30787cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
30797cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
30807cc577a4SJonathan Peyton 
30817cc577a4SJonathan Peyton   taskdata->td_flags.complete = 1; // mark the task as completed
30827cc577a4SJonathan Peyton 
30837cc577a4SJonathan Peyton   if (taskdata->td_taskgroup)
3084c47afcd9SAndrey Churbanov     KMP_TEST_THEN_DEC32(&taskdata->td_taskgroup->count);
30857cc577a4SJonathan Peyton 
30863041982dSJonathan Peyton   // Create an imaginary children for this task so the bottom half cannot
30873041982dSJonathan Peyton   // release the task before we have completed the second top half
30887cc577a4SJonathan Peyton   TCI_4(taskdata->td_incomplete_child_tasks);
30897cc577a4SJonathan Peyton }
30907cc577a4SJonathan Peyton 
30913041982dSJonathan Peyton static void __kmp_second_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
30927cc577a4SJonathan Peyton   kmp_int32 children = 0;
30937cc577a4SJonathan Peyton 
30947cc577a4SJonathan Peyton   // Predecrement simulated by "- 1" calculation
30953041982dSJonathan Peyton   children =
30965ba90c79SAndrey Churbanov       KMP_TEST_THEN_DEC32(&taskdata->td_parent->td_incomplete_child_tasks) - 1;
30977cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(children >= 0);
30987cc577a4SJonathan Peyton 
30997cc577a4SJonathan Peyton   // Remove the imaginary children
31007cc577a4SJonathan Peyton   TCD_4(taskdata->td_incomplete_child_tasks);
31017cc577a4SJonathan Peyton }
31027cc577a4SJonathan Peyton 
31033041982dSJonathan Peyton static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask) {
31047cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
31057cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
31067cc577a4SJonathan Peyton 
31077cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
31083041982dSJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.complete ==
31093041982dSJonathan Peyton                    1); // top half must run before bottom half
31107cc577a4SJonathan Peyton 
31117cc577a4SJonathan Peyton   // We need to wait to make sure the top half is finished
31127cc577a4SJonathan Peyton   // Spinning here should be ok as this should happen quickly
31133041982dSJonathan Peyton   while (TCR_4(taskdata->td_incomplete_child_tasks) > 0)
31143041982dSJonathan Peyton     ;
31157cc577a4SJonathan Peyton 
31167cc577a4SJonathan Peyton   __kmp_release_deps(gtid, taskdata);
31177cc577a4SJonathan Peyton   __kmp_free_task_and_ancestors(gtid, taskdata, thread);
31187cc577a4SJonathan Peyton }
31197cc577a4SJonathan Peyton 
31207cc577a4SJonathan Peyton /*!
31217cc577a4SJonathan Peyton @ingroup TASKING
31227cc577a4SJonathan Peyton @param gtid Global Thread ID of encountering thread
31237cc577a4SJonathan Peyton @param ptask Task which execution is completed
31247cc577a4SJonathan Peyton 
31253041982dSJonathan Peyton Execute the completation of a proxy task from a thread of that is part of the
31263041982dSJonathan Peyton team. Run first and bottom halves directly.
31277cc577a4SJonathan Peyton */
31283041982dSJonathan Peyton void __kmpc_proxy_task_completed(kmp_int32 gtid, kmp_task_t *ptask) {
31297cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(ptask != NULL);
31307cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
31313041982dSJonathan Peyton   KA_TRACE(
31323041982dSJonathan Peyton       10, ("__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n",
31333041982dSJonathan Peyton            gtid, taskdata));
31347cc577a4SJonathan Peyton 
31357cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
31367cc577a4SJonathan Peyton 
31377cc577a4SJonathan Peyton   __kmp_first_top_half_finish_proxy(taskdata);
31387cc577a4SJonathan Peyton   __kmp_second_top_half_finish_proxy(taskdata);
31397cc577a4SJonathan Peyton   __kmp_bottom_half_finish_proxy(gtid, ptask);
31407cc577a4SJonathan Peyton 
31413041982dSJonathan Peyton   KA_TRACE(10,
31423041982dSJonathan Peyton            ("__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n",
31433041982dSJonathan Peyton             gtid, taskdata));
31447cc577a4SJonathan Peyton }
31457cc577a4SJonathan Peyton 
31467cc577a4SJonathan Peyton /*!
31477cc577a4SJonathan Peyton @ingroup TASKING
31487cc577a4SJonathan Peyton @param ptask Task which execution is completed
31497cc577a4SJonathan Peyton 
31503041982dSJonathan Peyton Execute the completation of a proxy task from a thread that could not belong to
31513041982dSJonathan Peyton the team.
31527cc577a4SJonathan Peyton */
31533041982dSJonathan Peyton void __kmpc_proxy_task_completed_ooo(kmp_task_t *ptask) {
31547cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(ptask != NULL);
31557cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
31567cc577a4SJonathan Peyton 
31573041982dSJonathan Peyton   KA_TRACE(
31583041982dSJonathan Peyton       10,
31593041982dSJonathan Peyton       ("__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n",
31603041982dSJonathan Peyton        taskdata));
31617cc577a4SJonathan Peyton 
31627cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
31637cc577a4SJonathan Peyton 
31647cc577a4SJonathan Peyton   __kmp_first_top_half_finish_proxy(taskdata);
31657cc577a4SJonathan Peyton 
31663041982dSJonathan Peyton   // Enqueue task to complete bottom half completion from a thread within the
31673041982dSJonathan Peyton   // corresponding team
31687cc577a4SJonathan Peyton   kmp_team_t *team = taskdata->td_team;
31697cc577a4SJonathan Peyton   kmp_int32 nthreads = team->t.t_nproc;
31707cc577a4SJonathan Peyton   kmp_info_t *thread;
31717cc577a4SJonathan Peyton 
31723041982dSJonathan Peyton   // This should be similar to start_k = __kmp_get_random( thread ) % nthreads
31733041982dSJonathan Peyton   // but we cannot use __kmp_get_random here
31747cc577a4SJonathan Peyton   kmp_int32 start_k = 0;
31757cc577a4SJonathan Peyton   kmp_int32 pass = 1;
31767cc577a4SJonathan Peyton   kmp_int32 k = start_k;
31777cc577a4SJonathan Peyton 
31787cc577a4SJonathan Peyton   do {
31797cc577a4SJonathan Peyton     // For now we're just linearly trying to find a thread
31807cc577a4SJonathan Peyton     thread = team->t.t_threads[k];
31817cc577a4SJonathan Peyton     k = (k + 1) % nthreads;
31827cc577a4SJonathan Peyton 
31837cc577a4SJonathan Peyton     // we did a full pass through all the threads
31843041982dSJonathan Peyton     if (k == start_k)
31853041982dSJonathan Peyton       pass = pass << 1;
31867cc577a4SJonathan Peyton 
31877cc577a4SJonathan Peyton   } while (!__kmp_give_task(thread, k, ptask, pass));
31887cc577a4SJonathan Peyton 
31897cc577a4SJonathan Peyton   __kmp_second_top_half_finish_proxy(taskdata);
31907cc577a4SJonathan Peyton 
31913041982dSJonathan Peyton   KA_TRACE(
31923041982dSJonathan Peyton       10,
31933041982dSJonathan Peyton       ("__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n",
31943041982dSJonathan Peyton        taskdata));
31957cc577a4SJonathan Peyton }
31967cc577a4SJonathan Peyton 
31973041982dSJonathan Peyton // __kmp_task_dup_alloc: Allocate the taskdata and make a copy of source task
31983041982dSJonathan Peyton // for taskloop
31997cc577a4SJonathan Peyton //
32007cc577a4SJonathan Peyton // thread:   allocating thread
32017cc577a4SJonathan Peyton // task_src: pointer to source task to be duplicated
32027cc577a4SJonathan Peyton // returns:  a pointer to the allocated kmp_task_t structure (task).
32033041982dSJonathan Peyton kmp_task_t *__kmp_task_dup_alloc(kmp_info_t *thread, kmp_task_t *task_src) {
32047cc577a4SJonathan Peyton   kmp_task_t *task;
32057cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata;
32067cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata_src;
32077cc577a4SJonathan Peyton   kmp_taskdata_t *parent_task = thread->th.th_current_task;
32087cc577a4SJonathan Peyton   size_t shareds_offset;
32097cc577a4SJonathan Peyton   size_t task_size;
32107cc577a4SJonathan Peyton 
32113041982dSJonathan Peyton   KA_TRACE(10, ("__kmp_task_dup_alloc(enter): Th %p, source task %p\n", thread,
32123041982dSJonathan Peyton                 task_src));
32137cc577a4SJonathan Peyton   taskdata_src = KMP_TASK_TO_TASKDATA(task_src);
32143041982dSJonathan Peyton   KMP_DEBUG_ASSERT(taskdata_src->td_flags.proxy ==
32153041982dSJonathan Peyton                    TASK_FULL); // it should not be proxy task
32167cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata_src->td_flags.tasktype == TASK_EXPLICIT);
32177cc577a4SJonathan Peyton   task_size = taskdata_src->td_size_alloc;
32187cc577a4SJonathan Peyton 
32197cc577a4SJonathan Peyton   // Allocate a kmp_taskdata_t block and a kmp_task_t block.
32203041982dSJonathan Peyton   KA_TRACE(30, ("__kmp_task_dup_alloc: Th %p, malloc size %ld\n", thread,
32213041982dSJonathan Peyton                 task_size));
32227cc577a4SJonathan Peyton #if USE_FAST_MEMORY
32237cc577a4SJonathan Peyton   taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, task_size);
32247cc577a4SJonathan Peyton #else
32257cc577a4SJonathan Peyton   taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, task_size);
32267cc577a4SJonathan Peyton #endif /* USE_FAST_MEMORY */
32277cc577a4SJonathan Peyton   KMP_MEMCPY(taskdata, taskdata_src, task_size);
32287cc577a4SJonathan Peyton 
32297cc577a4SJonathan Peyton   task = KMP_TASKDATA_TO_TASK(taskdata);
32307cc577a4SJonathan Peyton 
32317cc577a4SJonathan Peyton   // Initialize new task (only specific fields not affected by memcpy)
32327cc577a4SJonathan Peyton   taskdata->td_task_id = KMP_GEN_TASK_ID();
32337cc577a4SJonathan Peyton   if (task->shareds != NULL) { // need setup shareds pointer
32347cc577a4SJonathan Peyton     shareds_offset = (char *)task_src->shareds - (char *)taskdata_src;
32357cc577a4SJonathan Peyton     task->shareds = &((char *)taskdata)[shareds_offset];
32363041982dSJonathan Peyton     KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (sizeof(void *) - 1)) ==
32373041982dSJonathan Peyton                      0);
32387cc577a4SJonathan Peyton   }
32397cc577a4SJonathan Peyton   taskdata->td_alloc_thread = thread;
3240*93e17cfeSJonathan Peyton   taskdata->td_parent = parent_task;
32413041982dSJonathan Peyton   taskdata->td_taskgroup =
32423041982dSJonathan Peyton       parent_task
32433041982dSJonathan Peyton           ->td_taskgroup; // task inherits the taskgroup from the parent task
32447cc577a4SJonathan Peyton 
32453041982dSJonathan Peyton   // Only need to keep track of child task counts if team parallel and tasking
32463041982dSJonathan Peyton   // not serialized
32477cc577a4SJonathan Peyton   if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
32485ba90c79SAndrey Churbanov     KMP_TEST_THEN_INC32(&parent_task->td_incomplete_child_tasks);
32497cc577a4SJonathan Peyton     if (parent_task->td_taskgroup)
3250c47afcd9SAndrey Churbanov       KMP_TEST_THEN_INC32(&parent_task->td_taskgroup->count);
32513041982dSJonathan Peyton     // Only need to keep track of allocated child tasks for explicit tasks since
32523041982dSJonathan Peyton     // implicit not deallocated
32537cc577a4SJonathan Peyton     if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT)
32545ba90c79SAndrey Churbanov       KMP_TEST_THEN_INC32(&taskdata->td_parent->td_allocated_child_tasks);
32557cc577a4SJonathan Peyton   }
32567cc577a4SJonathan Peyton 
32573041982dSJonathan Peyton   KA_TRACE(20,
32583041982dSJonathan Peyton            ("__kmp_task_dup_alloc(exit): Th %p, created task %p, parent=%p\n",
32597cc577a4SJonathan Peyton             thread, taskdata, taskdata->td_parent));
32607cc577a4SJonathan Peyton #if OMPT_SUPPORT
32613041982dSJonathan Peyton   __kmp_task_init_ompt(taskdata, thread->th.th_info.ds.ds_gtid,
32623041982dSJonathan Peyton                        (void *)task->routine);
32637cc577a4SJonathan Peyton #endif
32647cc577a4SJonathan Peyton   return task;
32657cc577a4SJonathan Peyton }
32667cc577a4SJonathan Peyton 
32677cc577a4SJonathan Peyton // Routine optionally generated by the compiler for setting the lastprivate flag
32687cc577a4SJonathan Peyton // and calling needed constructors for private/firstprivate objects
32697cc577a4SJonathan Peyton // (used to form taskloop tasks from pattern task)
3270*93e17cfeSJonathan Peyton // Parameters: dest task, src task, lastprivate flag.
32717cc577a4SJonathan Peyton typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32);
32727cc577a4SJonathan Peyton 
32737cc577a4SJonathan Peyton // __kmp_taskloop_linear: Start tasks of the taskloop linearly
32747cc577a4SJonathan Peyton //
32757cc577a4SJonathan Peyton // loc       Source location information
32767cc577a4SJonathan Peyton // gtid      Global thread ID
3277*93e17cfeSJonathan Peyton // task      Pattern task, exposes the loop iteration range
3278*93e17cfeSJonathan Peyton // lb        Pointer to loop lower bound in task structure
3279*93e17cfeSJonathan Peyton // ub        Pointer to loop upper bound in task structure
32807cc577a4SJonathan Peyton // st        Loop stride
3281*93e17cfeSJonathan Peyton // ub_glob   Global upper bound (used for lastprivate check)
3282*93e17cfeSJonathan Peyton // num_tasks Number of tasks to execute
3283*93e17cfeSJonathan Peyton // grainsize Number of loop iterations per task
3284*93e17cfeSJonathan Peyton // extras    Number of chunks with grainsize+1 iterations
3285*93e17cfeSJonathan Peyton // tc        Iterations count
32867cc577a4SJonathan Peyton // task_dup  Tasks duplication routine
32873041982dSJonathan Peyton void __kmp_taskloop_linear(ident_t *loc, int gtid, kmp_task_t *task,
32887cc577a4SJonathan Peyton                            kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
3289*93e17cfeSJonathan Peyton                            kmp_uint64 ub_glob, kmp_uint64 num_tasks,
3290*93e17cfeSJonathan Peyton                            kmp_uint64 grainsize, kmp_uint64 extras,
3291*93e17cfeSJonathan Peyton                            kmp_uint64 tc, void *task_dup) {
32927cc577a4SJonathan Peyton   KMP_COUNT_BLOCK(OMP_TASKLOOP);
32937cc577a4SJonathan Peyton   KMP_TIME_PARTITIONED_BLOCK(OMP_taskloop_scheduling);
32947cc577a4SJonathan Peyton   p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
32957cc577a4SJonathan Peyton   kmp_uint64 lower = *lb; // compiler provides global bounds here
32967cc577a4SJonathan Peyton   kmp_uint64 upper = *ub;
3297*93e17cfeSJonathan Peyton   kmp_uint64 i;
32987cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
32997cc577a4SJonathan Peyton   kmp_taskdata_t *current_task = thread->th.th_current_task;
33007cc577a4SJonathan Peyton   kmp_task_t *next_task;
33017cc577a4SJonathan Peyton   kmp_int32 lastpriv = 0;
33023041982dSJonathan Peyton   size_t lower_offset =
33033041982dSJonathan Peyton       (char *)lb - (char *)task; // remember offset of lb in the task structure
33043041982dSJonathan Peyton   size_t upper_offset =
33053041982dSJonathan Peyton       (char *)ub - (char *)task; // remember offset of ub in the task structure
33067cc577a4SJonathan Peyton 
3307*93e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras);
3308*93e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks > extras);
3309*93e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks > 0);
3310*93e17cfeSJonathan Peyton   KA_TRACE(20, ("__kmp_taskloop_linear: T#%d: %lld tasks, grainsize %lld, "
3311*93e17cfeSJonathan Peyton                 "extras %lld, i=%lld,%lld(%d)%lld, dup %p\n", gtid, num_tasks,
3312*93e17cfeSJonathan Peyton                 grainsize, extras, lower, upper, ub_glob, st, task_dup));
3313*93e17cfeSJonathan Peyton 
3314*93e17cfeSJonathan Peyton   // Launch num_tasks tasks, assign grainsize iterations each task
3315*93e17cfeSJonathan Peyton   for (i = 0; i < num_tasks; ++i) {
3316*93e17cfeSJonathan Peyton     kmp_uint64 chunk_minus_1;
3317*93e17cfeSJonathan Peyton     if (extras == 0) {
3318*93e17cfeSJonathan Peyton       chunk_minus_1 = grainsize - 1;
3319*93e17cfeSJonathan Peyton     } else {
3320*93e17cfeSJonathan Peyton       chunk_minus_1 = grainsize;
3321*93e17cfeSJonathan Peyton       --extras; // first extras iterations get bigger chunk (grainsize+1)
3322*93e17cfeSJonathan Peyton     }
3323*93e17cfeSJonathan Peyton     upper = lower + st * chunk_minus_1;
3324*93e17cfeSJonathan Peyton     if (i == num_tasks - 1) {
3325*93e17cfeSJonathan Peyton       // schedule the last task, set lastprivate flag if needed
3326*93e17cfeSJonathan Peyton       if (st == 1) { // most common case
3327*93e17cfeSJonathan Peyton         KMP_DEBUG_ASSERT(upper == *ub);
3328*93e17cfeSJonathan Peyton         if (upper == ub_glob)
3329*93e17cfeSJonathan Peyton           lastpriv = 1;
3330*93e17cfeSJonathan Peyton       } else if (st > 0) { // positive loop stride
3331*93e17cfeSJonathan Peyton         KMP_DEBUG_ASSERT((kmp_uint64)st > *ub - upper);
3332*93e17cfeSJonathan Peyton         if ((kmp_uint64)st > ub_glob - upper)
3333*93e17cfeSJonathan Peyton           lastpriv = 1;
3334*93e17cfeSJonathan Peyton       } else {  // negative loop stride
3335*93e17cfeSJonathan Peyton         KMP_DEBUG_ASSERT(upper + st < *ub);
3336*93e17cfeSJonathan Peyton         if (upper - ub_glob < (kmp_uint64)(-st))
3337*93e17cfeSJonathan Peyton           lastpriv = 1;
3338*93e17cfeSJonathan Peyton       }
3339*93e17cfeSJonathan Peyton     }
3340*93e17cfeSJonathan Peyton     next_task = __kmp_task_dup_alloc(thread, task); // allocate new task
3341*93e17cfeSJonathan Peyton     // adjust task-specific bounds
3342*93e17cfeSJonathan Peyton     *(kmp_uint64 *)((char *)next_task + lower_offset) = lower;
3343*93e17cfeSJonathan Peyton     *(kmp_uint64 *)((char *)next_task + upper_offset) = upper;
3344*93e17cfeSJonathan Peyton     if (ptask_dup != NULL) // set lastprivate flag, construct fistprivates, etc.
3345*93e17cfeSJonathan Peyton       ptask_dup(next_task, task, lastpriv);
3346*93e17cfeSJonathan Peyton     KA_TRACE(40, ("__kmp_taskloop_linear: T#%d; task %p: lower %lld, "
3347*93e17cfeSJonathan Peyton                   "upper %lld (offsets %p %p)\n",
3348*93e17cfeSJonathan Peyton                   gtid, next_task, lower, upper, lower_offset, upper_offset));
3349*93e17cfeSJonathan Peyton     __kmp_omp_task(gtid, next_task, true); // schedule new task
3350*93e17cfeSJonathan Peyton     lower = upper + st; // adjust lower bound for the next iteration
3351*93e17cfeSJonathan Peyton   }
3352*93e17cfeSJonathan Peyton   // free the pattern task and exit
3353*93e17cfeSJonathan Peyton   __kmp_task_start(gtid, task, current_task); // make internal bookkeeping
3354*93e17cfeSJonathan Peyton   // do not execute the pattern task, just do internal bookkeeping
3355*93e17cfeSJonathan Peyton   __kmp_task_finish(gtid, task, current_task);
3356*93e17cfeSJonathan Peyton }
3357*93e17cfeSJonathan Peyton 
3358*93e17cfeSJonathan Peyton // Structure to keep taskloop parameters for auxiliary task
3359*93e17cfeSJonathan Peyton // kept in the shareds of the task structure.
3360*93e17cfeSJonathan Peyton typedef struct __taskloop_params {
3361*93e17cfeSJonathan Peyton   kmp_task_t *task;
3362*93e17cfeSJonathan Peyton   kmp_uint64 *lb;
3363*93e17cfeSJonathan Peyton   kmp_uint64 *ub;
3364*93e17cfeSJonathan Peyton   void *task_dup;
3365*93e17cfeSJonathan Peyton   kmp_int64 st;
3366*93e17cfeSJonathan Peyton   kmp_uint64 ub_glob;
3367*93e17cfeSJonathan Peyton   kmp_uint64 num_tasks;
3368*93e17cfeSJonathan Peyton   kmp_uint64 grainsize;
3369*93e17cfeSJonathan Peyton   kmp_uint64 extras;
3370*93e17cfeSJonathan Peyton   kmp_uint64 tc;
3371*93e17cfeSJonathan Peyton   kmp_uint64 num_t_min;
3372*93e17cfeSJonathan Peyton } __taskloop_params_t;
3373*93e17cfeSJonathan Peyton 
3374*93e17cfeSJonathan Peyton void __kmp_taskloop_recur(ident_t *, int, kmp_task_t *, kmp_uint64 *,
3375*93e17cfeSJonathan Peyton                           kmp_uint64 *, kmp_int64, kmp_uint64, kmp_uint64,
3376*93e17cfeSJonathan Peyton                           kmp_uint64, kmp_uint64, kmp_uint64, kmp_uint64,
3377*93e17cfeSJonathan Peyton                           void *);
3378*93e17cfeSJonathan Peyton 
3379*93e17cfeSJonathan Peyton // Execute part of the the taskloop submitted as a task.
3380*93e17cfeSJonathan Peyton int __kmp_taskloop_task(int gtid, void *ptask) {
3381*93e17cfeSJonathan Peyton   __taskloop_params_t *p = (__taskloop_params_t*)((kmp_task_t*)ptask)->shareds;
3382*93e17cfeSJonathan Peyton   kmp_task_t *task = p->task;
3383*93e17cfeSJonathan Peyton   kmp_uint64 *lb = p->lb;
3384*93e17cfeSJonathan Peyton   kmp_uint64 *ub = p->ub;
3385*93e17cfeSJonathan Peyton   void *task_dup = p->task_dup;
3386*93e17cfeSJonathan Peyton //  p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
3387*93e17cfeSJonathan Peyton   kmp_int64 st = p->st;
3388*93e17cfeSJonathan Peyton   kmp_uint64 ub_glob = p->ub_glob;
3389*93e17cfeSJonathan Peyton   kmp_uint64 num_tasks = p->num_tasks;
3390*93e17cfeSJonathan Peyton   kmp_uint64 grainsize = p->grainsize;
3391*93e17cfeSJonathan Peyton   kmp_uint64 extras = p->extras;
3392*93e17cfeSJonathan Peyton   kmp_uint64 tc = p->tc;
3393*93e17cfeSJonathan Peyton   kmp_uint64 num_t_min = p->num_t_min;
3394*93e17cfeSJonathan Peyton #if KMP_DEBUG
3395*93e17cfeSJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
3396*93e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(task != NULL);
3397*93e17cfeSJonathan Peyton   KA_TRACE(20, ("__kmp_taskloop_task: T#%d, task %p: %lld tasks, grainsize"
3398*93e17cfeSJonathan Peyton                 " %lld, extras %lld, i=%lld,%lld(%d), dup %p\n", gtid, taskdata,
3399*93e17cfeSJonathan Peyton                 num_tasks, grainsize, extras, *lb, *ub, st, task_dup));
3400*93e17cfeSJonathan Peyton #endif
3401*93e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks*2+1 > num_t_min);
3402*93e17cfeSJonathan Peyton   if (num_tasks > num_t_min)
3403*93e17cfeSJonathan Peyton     __kmp_taskloop_recur(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
3404*93e17cfeSJonathan Peyton                          grainsize, extras, tc, num_t_min, task_dup);
3405*93e17cfeSJonathan Peyton   else
3406*93e17cfeSJonathan Peyton     __kmp_taskloop_linear(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks,
3407*93e17cfeSJonathan Peyton                           grainsize, extras, tc, task_dup);
3408*93e17cfeSJonathan Peyton 
3409*93e17cfeSJonathan Peyton   KA_TRACE(40, ("__kmp_taskloop_task(exit): T#%d\n", gtid));
3410*93e17cfeSJonathan Peyton   return 0;
3411*93e17cfeSJonathan Peyton }
3412*93e17cfeSJonathan Peyton 
3413*93e17cfeSJonathan Peyton // Schedule part of the the taskloop as a task,
3414*93e17cfeSJonathan Peyton // execute the rest of the the taskloop.
3415*93e17cfeSJonathan Peyton //
3416*93e17cfeSJonathan Peyton // loc       Source location information
3417*93e17cfeSJonathan Peyton // gtid      Global thread ID
3418*93e17cfeSJonathan Peyton // task      Pattern task, exposes the loop iteration range
3419*93e17cfeSJonathan Peyton // lb        Pointer to loop lower bound in task structure
3420*93e17cfeSJonathan Peyton // ub        Pointer to loop upper bound in task structure
3421*93e17cfeSJonathan Peyton // st        Loop stride
3422*93e17cfeSJonathan Peyton // ub_glob   Global upper bound (used for lastprivate check)
3423*93e17cfeSJonathan Peyton // num_tasks Number of tasks to execute
3424*93e17cfeSJonathan Peyton // grainsize Number of loop iterations per task
3425*93e17cfeSJonathan Peyton // extras    Number of chunks with grainsize+1 iterations
3426*93e17cfeSJonathan Peyton // tc        Iterations count
3427*93e17cfeSJonathan Peyton // num_t_min Threashold to launch tasks recursively
3428*93e17cfeSJonathan Peyton // task_dup  Tasks duplication routine
3429*93e17cfeSJonathan Peyton void __kmp_taskloop_recur(ident_t *loc, int gtid, kmp_task_t *task,
3430*93e17cfeSJonathan Peyton                           kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
3431*93e17cfeSJonathan Peyton                           kmp_uint64 ub_glob, kmp_uint64 num_tasks,
3432*93e17cfeSJonathan Peyton                           kmp_uint64 grainsize, kmp_uint64 extras,
3433*93e17cfeSJonathan Peyton                           kmp_uint64 tc, kmp_uint64 num_t_min, void *task_dup) {
3434*93e17cfeSJonathan Peyton #if KMP_DEBUG
3435*93e17cfeSJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
3436*93e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(task != NULL);
3437*93e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks > num_t_min);
3438*93e17cfeSJonathan Peyton   KA_TRACE(20, ("__kmp_taskloop_recur: T#%d, task %p: %lld tasks, grainsize"
3439*93e17cfeSJonathan Peyton                 " %lld, extras %lld, i=%lld,%lld(%d), dup %p\n", gtid, taskdata,
3440*93e17cfeSJonathan Peyton                 num_tasks, grainsize, extras, *lb, *ub, st, task_dup));
3441*93e17cfeSJonathan Peyton #endif
3442*93e17cfeSJonathan Peyton   p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
3443*93e17cfeSJonathan Peyton   kmp_uint64 lower = *lb;
3444*93e17cfeSJonathan Peyton   kmp_uint64 upper = *ub;
3445*93e17cfeSJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
3446*93e17cfeSJonathan Peyton //  kmp_taskdata_t *current_task = thread->th.th_current_task;
3447*93e17cfeSJonathan Peyton   kmp_task_t *next_task;
3448*93e17cfeSJonathan Peyton   kmp_int32 lastpriv = 0;
3449*93e17cfeSJonathan Peyton   size_t lower_offset =
3450*93e17cfeSJonathan Peyton       (char *)lb - (char *)task; // remember offset of lb in the task structure
3451*93e17cfeSJonathan Peyton   size_t upper_offset =
3452*93e17cfeSJonathan Peyton       (char *)ub - (char *)task; // remember offset of ub in the task structure
3453*93e17cfeSJonathan Peyton 
3454*93e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras);
3455*93e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks > extras);
3456*93e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks > 0);
3457*93e17cfeSJonathan Peyton 
3458*93e17cfeSJonathan Peyton   // split the loop in two halves
3459*93e17cfeSJonathan Peyton   kmp_uint64 lb1, ub0, tc0, tc1, ext0, ext1;
3460*93e17cfeSJonathan Peyton   kmp_uint64 gr_size0 = grainsize;
3461*93e17cfeSJonathan Peyton   kmp_uint64 n_tsk0 = num_tasks >> 1; // num_tasks/2 to execute
3462*93e17cfeSJonathan Peyton   kmp_uint64 n_tsk1 = num_tasks - n_tsk0; // to schedule as a task
3463*93e17cfeSJonathan Peyton   if (n_tsk0 <= extras) {
3464*93e17cfeSJonathan Peyton     gr_size0++; // integrate extras into grainsize
3465*93e17cfeSJonathan Peyton     ext0 = 0; // no extra iters in 1st half
3466*93e17cfeSJonathan Peyton     ext1 = extras - n_tsk0; // remaining extras
3467*93e17cfeSJonathan Peyton     tc0 = gr_size0 * n_tsk0;
3468*93e17cfeSJonathan Peyton     tc1 = tc - tc0;
3469*93e17cfeSJonathan Peyton   } else { // n_tsk0 > extras
3470*93e17cfeSJonathan Peyton     ext1 = 0; // no extra iters in 2nd half
3471*93e17cfeSJonathan Peyton     ext0 = extras;
3472*93e17cfeSJonathan Peyton     tc1 = grainsize * n_tsk1;
3473*93e17cfeSJonathan Peyton     tc0 = tc - tc1;
3474*93e17cfeSJonathan Peyton   }
3475*93e17cfeSJonathan Peyton   ub0 = lower + st * (tc0 - 1);
3476*93e17cfeSJonathan Peyton   lb1 = ub0 + st;
3477*93e17cfeSJonathan Peyton 
3478*93e17cfeSJonathan Peyton   // create pattern task for 2nd half of the loop
3479*93e17cfeSJonathan Peyton   next_task = __kmp_task_dup_alloc(thread, task); // duplicate the task
3480*93e17cfeSJonathan Peyton   // adjust lower bound (upper bound is not changed) for the 2nd half
3481*93e17cfeSJonathan Peyton   *(kmp_uint64 *)((char *)next_task + lower_offset) = lb1;
3482*93e17cfeSJonathan Peyton   if (ptask_dup != NULL) // construct fistprivates, etc.
3483*93e17cfeSJonathan Peyton     ptask_dup(next_task, task, 0);
3484*93e17cfeSJonathan Peyton   *ub = ub0; // adjust upper bound for the 1st half
3485*93e17cfeSJonathan Peyton 
3486*93e17cfeSJonathan Peyton   // create auxiliary task for 2nd half of the loop
3487*93e17cfeSJonathan Peyton   kmp_task_t *new_task =
3488*93e17cfeSJonathan Peyton       __kmpc_omp_task_alloc(loc, gtid, 1, 3 * sizeof(void*),
3489*93e17cfeSJonathan Peyton                             sizeof(__taskloop_params_t), &__kmp_taskloop_task);
3490*93e17cfeSJonathan Peyton   __taskloop_params_t * p = (__taskloop_params_t *)new_task->shareds;
3491*93e17cfeSJonathan Peyton   p->task = next_task;
3492*93e17cfeSJonathan Peyton   p->lb = (kmp_uint64 *)((char *)next_task + lower_offset);
3493*93e17cfeSJonathan Peyton   p->ub = (kmp_uint64 *)((char *)next_task + upper_offset);
3494*93e17cfeSJonathan Peyton   p->task_dup = task_dup;
3495*93e17cfeSJonathan Peyton   p->st = st;
3496*93e17cfeSJonathan Peyton   p->ub_glob = ub_glob;
3497*93e17cfeSJonathan Peyton   p->num_tasks = n_tsk1;
3498*93e17cfeSJonathan Peyton   p->grainsize = grainsize;
3499*93e17cfeSJonathan Peyton   p->extras = ext1;
3500*93e17cfeSJonathan Peyton   p->tc = tc1;
3501*93e17cfeSJonathan Peyton   p->num_t_min = num_t_min;
3502*93e17cfeSJonathan Peyton   __kmp_omp_task(gtid, new_task, true); // schedule new task
3503*93e17cfeSJonathan Peyton 
3504*93e17cfeSJonathan Peyton   // execute the 1st half of current subrange
3505*93e17cfeSJonathan Peyton   if (n_tsk0 > num_t_min)
3506*93e17cfeSJonathan Peyton     __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0,
3507*93e17cfeSJonathan Peyton                          gr_size0, ext0, tc0, num_t_min, task_dup);
3508*93e17cfeSJonathan Peyton   else
3509*93e17cfeSJonathan Peyton     __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0,
3510*93e17cfeSJonathan Peyton                           gr_size0, ext0, tc0, task_dup);
3511*93e17cfeSJonathan Peyton 
3512*93e17cfeSJonathan Peyton   KA_TRACE(40, ("__kmpc_taskloop_recur(exit): T#%d\n", gtid));
3513*93e17cfeSJonathan Peyton }
3514*93e17cfeSJonathan Peyton 
3515*93e17cfeSJonathan Peyton /*!
3516*93e17cfeSJonathan Peyton @ingroup TASKING
3517*93e17cfeSJonathan Peyton @param loc       Source location information
3518*93e17cfeSJonathan Peyton @param gtid      Global thread ID
3519*93e17cfeSJonathan Peyton @param task      Task structure
3520*93e17cfeSJonathan Peyton @param if_val    Value of the if clause
3521*93e17cfeSJonathan Peyton @param lb        Pointer to loop lower bound in task structure
3522*93e17cfeSJonathan Peyton @param ub        Pointer to loop upper bound in task structure
3523*93e17cfeSJonathan Peyton @param st        Loop stride
3524*93e17cfeSJonathan Peyton @param nogroup   Flag, 1 if nogroup clause specified, 0 otherwise
3525*93e17cfeSJonathan Peyton @param sched     Schedule specified 0/1/2 for none/grainsize/num_tasks
3526*93e17cfeSJonathan Peyton @param grainsize Schedule value if specified
3527*93e17cfeSJonathan Peyton @param task_dup  Tasks duplication routine
3528*93e17cfeSJonathan Peyton 
3529*93e17cfeSJonathan Peyton Execute the taskloop construct.
3530*93e17cfeSJonathan Peyton */
3531*93e17cfeSJonathan Peyton void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int if_val,
3532*93e17cfeSJonathan Peyton                      kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup,
3533*93e17cfeSJonathan Peyton                      int sched, kmp_uint64 grainsize, void *task_dup) {
3534*93e17cfeSJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
3535*93e17cfeSJonathan Peyton   KMP_DEBUG_ASSERT(task != NULL);
3536*93e17cfeSJonathan Peyton 
3537*93e17cfeSJonathan Peyton   KA_TRACE(20, ("__kmpc_taskloop: T#%d, task %p, lb %lld, ub %lld, st %lld, "
3538*93e17cfeSJonathan Peyton                 "grain %llu(%d), dup %p\n", gtid, taskdata, *lb, *ub, st,
3539*93e17cfeSJonathan Peyton                 grainsize, sched, task_dup));
3540*93e17cfeSJonathan Peyton 
3541*93e17cfeSJonathan Peyton   if (nogroup == 0)
3542*93e17cfeSJonathan Peyton     __kmpc_taskgroup(loc, gtid);
3543*93e17cfeSJonathan Peyton 
3544*93e17cfeSJonathan Peyton   // =========================================================================
3545*93e17cfeSJonathan Peyton   // calculate loop parameters
3546*93e17cfeSJonathan Peyton   kmp_uint64 tc;
3547*93e17cfeSJonathan Peyton   kmp_uint64 lower = *lb; // compiler provides global bounds here
3548*93e17cfeSJonathan Peyton   kmp_uint64 upper = *ub;
3549*93e17cfeSJonathan Peyton   kmp_uint64 ub_glob = upper; // global upper used to calc lastprivate flag
3550*93e17cfeSJonathan Peyton   kmp_uint64 num_tasks = 0, extras = 0;
3551*93e17cfeSJonathan Peyton   kmp_uint64 num_tasks_min = __kmp_taskloop_min_tasks;
3552*93e17cfeSJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
3553*93e17cfeSJonathan Peyton   kmp_taskdata_t *current_task = thread->th.th_current_task;
3554*93e17cfeSJonathan Peyton 
35557cc577a4SJonathan Peyton   // compute trip count
35567cc577a4SJonathan Peyton   if (st == 1) { // most common case
35577cc577a4SJonathan Peyton     tc = upper - lower + 1;
35587cc577a4SJonathan Peyton   } else if (st < 0) {
35597cc577a4SJonathan Peyton     tc = (lower - upper) / (-st) + 1;
35607cc577a4SJonathan Peyton   } else { // st > 0
35617cc577a4SJonathan Peyton     tc = (upper - lower) / st + 1;
35627cc577a4SJonathan Peyton   }
35637cc577a4SJonathan Peyton   if (tc == 0) {
35647cc577a4SJonathan Peyton     KA_TRACE(20, ("__kmpc_taskloop(exit): T#%d zero-trip loop\n", gtid));
35657cc577a4SJonathan Peyton     // free the pattern task and exit
35667cc577a4SJonathan Peyton     __kmp_task_start(gtid, task, current_task);
35677cc577a4SJonathan Peyton     // do not execute anything for zero-trip loop
35687cc577a4SJonathan Peyton     __kmp_task_finish(gtid, task, current_task);
35697cc577a4SJonathan Peyton     return;
35707cc577a4SJonathan Peyton   }
3571*93e17cfeSJonathan Peyton   if (num_tasks_min == 0)
3572*93e17cfeSJonathan Peyton     // TODO: can we choose better default heuristic?
3573*93e17cfeSJonathan Peyton     num_tasks_min = KMP_MIN(thread->th.th_team_nproc * 10,
3574*93e17cfeSJonathan Peyton                             INITIAL_TASK_DEQUE_SIZE);
35757cc577a4SJonathan Peyton 
35767cc577a4SJonathan Peyton   // compute num_tasks/grainsize based on the input provided
35777cc577a4SJonathan Peyton   switch (sched) {
35787cc577a4SJonathan Peyton   case 0: // no schedule clause specified, we can choose the default
35797cc577a4SJonathan Peyton     // let's try to schedule (team_size*10) tasks
35807cc577a4SJonathan Peyton     grainsize = thread->th.th_team_nproc * 10;
35817cc577a4SJonathan Peyton   case 2: // num_tasks provided
35827cc577a4SJonathan Peyton     if (grainsize > tc) {
35837cc577a4SJonathan Peyton       num_tasks = tc; // too big num_tasks requested, adjust values
35847cc577a4SJonathan Peyton       grainsize = 1;
35857cc577a4SJonathan Peyton       extras = 0;
35867cc577a4SJonathan Peyton     } else {
35877cc577a4SJonathan Peyton       num_tasks = grainsize;
35887cc577a4SJonathan Peyton       grainsize = tc / num_tasks;
35897cc577a4SJonathan Peyton       extras = tc % num_tasks;
35907cc577a4SJonathan Peyton     }
35917cc577a4SJonathan Peyton     break;
35927cc577a4SJonathan Peyton   case 1: // grainsize provided
35937cc577a4SJonathan Peyton     if (grainsize > tc) {
35947cc577a4SJonathan Peyton       num_tasks = 1; // too big grainsize requested, adjust values
35957cc577a4SJonathan Peyton       grainsize = tc;
35967cc577a4SJonathan Peyton       extras = 0;
35977cc577a4SJonathan Peyton     } else {
35987cc577a4SJonathan Peyton       num_tasks = tc / grainsize;
3599*93e17cfeSJonathan Peyton       // adjust grainsize for balanced distribution of iterations
3600*93e17cfeSJonathan Peyton       grainsize = tc / num_tasks;
36017cc577a4SJonathan Peyton       extras = tc % num_tasks;
36027cc577a4SJonathan Peyton     }
36037cc577a4SJonathan Peyton     break;
36047cc577a4SJonathan Peyton   default:
36057cc577a4SJonathan Peyton     KMP_ASSERT2(0, "unknown scheduling of taskloop");
36067cc577a4SJonathan Peyton   }
36077cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras);
36087cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks > extras);
36097cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks > 0);
3610*93e17cfeSJonathan Peyton   // =========================================================================
36117cc577a4SJonathan Peyton 
36127cc577a4SJonathan Peyton   // check if clause value first
36137cc577a4SJonathan Peyton   if (if_val == 0) { // if(0) specified, mark task as serial
36147cc577a4SJonathan Peyton     taskdata->td_flags.task_serial = 1;
36157cc577a4SJonathan Peyton     taskdata->td_flags.tiedness = TASK_TIED; // AC: serial task cannot be untied
3616*93e17cfeSJonathan Peyton     // always start serial tasks linearly
3617*93e17cfeSJonathan Peyton     __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
3618*93e17cfeSJonathan Peyton                           grainsize, extras, tc, task_dup);
3619*93e17cfeSJonathan Peyton   } else if (num_tasks > num_tasks_min) {
3620*93e17cfeSJonathan Peyton     KA_TRACE(20, ("__kmpc_taskloop: T#%d, go recursive: tc %llu, #tasks %llu"
3621*93e17cfeSJonathan Peyton                   "(%lld), grain %llu, extras %llu\n", gtid, tc, num_tasks,
3622*93e17cfeSJonathan Peyton                   num_tasks_min, grainsize, extras));
3623*93e17cfeSJonathan Peyton     __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
3624*93e17cfeSJonathan Peyton                          grainsize, extras, tc, num_tasks_min, task_dup);
3625*93e17cfeSJonathan Peyton   } else {
3626*93e17cfeSJonathan Peyton     KA_TRACE(20, ("__kmpc_taskloop: T#%d, go linear: tc %llu, #tasks %llu"
3627*93e17cfeSJonathan Peyton                   "(%lld), grain %llu, extras %llu\n", gtid, tc, num_tasks,
3628*93e17cfeSJonathan Peyton                   num_tasks_min, grainsize, extras));
3629*93e17cfeSJonathan Peyton     __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks,
3630*93e17cfeSJonathan Peyton                           grainsize, extras, tc, task_dup);
36317cc577a4SJonathan Peyton   }
36327cc577a4SJonathan Peyton 
3633*93e17cfeSJonathan Peyton   if (nogroup == 0)
36347cc577a4SJonathan Peyton     __kmpc_end_taskgroup(loc, gtid);
3635*93e17cfeSJonathan Peyton   KA_TRACE(20, ("__kmpc_taskloop(exit): T#%d\n", gtid));
36367cc577a4SJonathan Peyton }
36377cc577a4SJonathan Peyton 
36387cc577a4SJonathan Peyton #endif
3639