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 
582*c47afcd9SAndrey Churbanov   kmp_int32 children = KMP_TEST_THEN_DEC32(CCAST(
583*c47afcd9SAndrey Churbanov                            kmp_int32 *, &taskdata->td_allocated_child_tasks)) -
5843041982dSJonathan Peyton                        1;
5857cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(children >= 0);
5867cc577a4SJonathan Peyton 
5877cc577a4SJonathan Peyton   // Now, go up the ancestor tree to see if any ancestors can now be freed.
5883041982dSJonathan Peyton   while (children == 0) {
5897cc577a4SJonathan Peyton     kmp_taskdata_t *parent_taskdata = taskdata->td_parent;
5907cc577a4SJonathan Peyton 
5917cc577a4SJonathan Peyton     KA_TRACE(20, ("__kmp_free_task_and_ancestors(enter): T#%d task %p complete "
5923041982dSJonathan Peyton                   "and freeing itself\n",
5933041982dSJonathan Peyton                   gtid, taskdata));
5947cc577a4SJonathan Peyton 
5957cc577a4SJonathan Peyton     // --- Deallocate my ancestor task ---
5967cc577a4SJonathan Peyton     __kmp_free_task(gtid, taskdata, thread);
5977cc577a4SJonathan Peyton 
5987cc577a4SJonathan Peyton     taskdata = parent_taskdata;
5997cc577a4SJonathan Peyton 
6003041982dSJonathan Peyton     // Stop checking ancestors at implicit task instead of walking up ancestor
6013041982dSJonathan Peyton     // tree to avoid premature deallocation of ancestors.
6027cc577a4SJonathan Peyton     if (team_serial || taskdata->td_flags.tasktype == TASK_IMPLICIT)
6037cc577a4SJonathan Peyton       return;
6047cc577a4SJonathan Peyton 
6057cc577a4SJonathan Peyton     // Predecrement simulated by "- 1" calculation
6063041982dSJonathan Peyton     children = KMP_TEST_THEN_DEC32(
607*c47afcd9SAndrey Churbanov                    CCAST(kmp_int32 *, &taskdata->td_allocated_child_tasks)) -
6083041982dSJonathan Peyton                1;
6097cc577a4SJonathan Peyton     KMP_DEBUG_ASSERT(children >= 0);
6107cc577a4SJonathan Peyton   }
6117cc577a4SJonathan Peyton 
6123041982dSJonathan Peyton   KA_TRACE(
6133041982dSJonathan Peyton       20, ("__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; "
6143041982dSJonathan Peyton            "not freeing it yet\n",
6153041982dSJonathan Peyton            gtid, taskdata, children));
6167cc577a4SJonathan Peyton }
6177cc577a4SJonathan Peyton 
6187cc577a4SJonathan Peyton // __kmp_task_finish: bookkeeping to do when a task finishes execution
6193041982dSJonathan Peyton //
6207cc577a4SJonathan Peyton // gtid: global thread ID for calling thread
6217cc577a4SJonathan Peyton // task: task to be finished
6227cc577a4SJonathan Peyton // resumed_task: task to be resumed.  (may be NULL if task is serialized)
6233041982dSJonathan Peyton static void __kmp_task_finish(kmp_int32 gtid, kmp_task_t *task,
6243041982dSJonathan Peyton                               kmp_taskdata_t *resumed_task) {
6257cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
6267cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
6273041982dSJonathan Peyton   kmp_task_team_t *task_team =
6283041982dSJonathan Peyton       thread->th.th_task_team; // might be NULL for serial teams...
6297cc577a4SJonathan Peyton   kmp_int32 children = 0;
6307cc577a4SJonathan Peyton 
6317cc577a4SJonathan Peyton #if OMPT_SUPPORT
6323041982dSJonathan Peyton   if (ompt_enabled && ompt_callbacks.ompt_callback(ompt_event_task_end)) {
6337cc577a4SJonathan Peyton     kmp_taskdata_t *parent = taskdata->td_parent;
6347cc577a4SJonathan Peyton     ompt_callbacks.ompt_callback(ompt_event_task_end)(
6357cc577a4SJonathan Peyton         taskdata->ompt_task_info.task_id);
6367cc577a4SJonathan Peyton   }
6377cc577a4SJonathan Peyton #endif
6387cc577a4SJonathan Peyton 
6393041982dSJonathan Peyton   KA_TRACE(10, ("__kmp_task_finish(enter): T#%d finishing task %p and resuming "
6403041982dSJonathan Peyton                 "task %p\n",
6417cc577a4SJonathan Peyton                 gtid, taskdata, resumed_task));
6427cc577a4SJonathan Peyton 
6437cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
6447cc577a4SJonathan Peyton 
6457cc577a4SJonathan Peyton // Pop task from stack if tied
6467cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK
6473041982dSJonathan Peyton   if (taskdata->td_flags.tiedness == TASK_TIED) {
6487cc577a4SJonathan Peyton     __kmp_pop_task_stack(gtid, thread, taskdata);
6497cc577a4SJonathan Peyton   }
6507cc577a4SJonathan Peyton #endif /* BUILD_TIED_TASK_STACK */
6517cc577a4SJonathan Peyton 
6527cc577a4SJonathan Peyton   if (taskdata->td_flags.tiedness == TASK_UNTIED) {
6533041982dSJonathan Peyton     // untied task needs to check the counter so that the task structure is not
6543041982dSJonathan Peyton     // freed prematurely
6557cc577a4SJonathan Peyton     kmp_int32 counter = KMP_TEST_THEN_DEC32(&taskdata->td_untied_count) - 1;
6563041982dSJonathan Peyton     KA_TRACE(
6573041982dSJonathan Peyton         20,
6583041982dSJonathan Peyton         ("__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n",
6597cc577a4SJonathan Peyton          gtid, counter, taskdata));
6607cc577a4SJonathan Peyton     if (counter > 0) {
6613041982dSJonathan Peyton       // untied task is not done, to be continued possibly by other thread, do
6623041982dSJonathan Peyton       // not free it now
6637cc577a4SJonathan Peyton       if (resumed_task == NULL) {
6647cc577a4SJonathan Peyton         KMP_DEBUG_ASSERT(taskdata->td_flags.task_serial);
6653041982dSJonathan Peyton         resumed_task = taskdata->td_parent; // In a serialized task, the resumed
6663041982dSJonathan Peyton         // task is the parent
6677cc577a4SJonathan Peyton       }
6687cc577a4SJonathan Peyton       thread->th.th_current_task = resumed_task; // restore current_task
6697cc577a4SJonathan Peyton       resumed_task->td_flags.executing = 1; // resume previous task
6703041982dSJonathan Peyton       KA_TRACE(10, ("__kmp_task_finish(exit): T#%d partially done task %p, "
6713041982dSJonathan Peyton                     "resuming task %p\n",
6727cc577a4SJonathan Peyton                     gtid, taskdata, resumed_task));
6737cc577a4SJonathan Peyton       return;
6747cc577a4SJonathan Peyton     }
6757cc577a4SJonathan Peyton   }
6767cc577a4SJonathan Peyton 
6777cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
6787cc577a4SJonathan Peyton   taskdata->td_flags.complete = 1; // mark the task as completed
6797cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.started == 1);
6807cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
6817cc577a4SJonathan Peyton 
6823041982dSJonathan Peyton   // Only need to keep track of count if team parallel and tasking not
6833041982dSJonathan Peyton   // serialized
6847cc577a4SJonathan Peyton   if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
6857cc577a4SJonathan Peyton     // Predecrement simulated by "- 1" calculation
6863041982dSJonathan Peyton     children =
687*c47afcd9SAndrey Churbanov         KMP_TEST_THEN_DEC32(CCAST(
688*c47afcd9SAndrey Churbanov             kmp_int32 *, &taskdata->td_parent->td_incomplete_child_tasks)) -
6893041982dSJonathan Peyton         1;
6907cc577a4SJonathan Peyton     KMP_DEBUG_ASSERT(children >= 0);
6917cc577a4SJonathan Peyton #if OMP_40_ENABLED
6927cc577a4SJonathan Peyton     if (taskdata->td_taskgroup)
6937cc577a4SJonathan Peyton       KMP_TEST_THEN_DEC32((kmp_int32 *)(&taskdata->td_taskgroup->count));
6947cc577a4SJonathan Peyton #if OMP_45_ENABLED
6957cc577a4SJonathan Peyton   }
6967cc577a4SJonathan Peyton   // if we found proxy tasks there could exist a dependency chain
6977cc577a4SJonathan Peyton   // with the proxy task as origin
6983041982dSJonathan Peyton   if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) ||
6993041982dSJonathan Peyton       (task_team && task_team->tt.tt_found_proxy_tasks)) {
7007cc577a4SJonathan Peyton #endif
7017cc577a4SJonathan Peyton     __kmp_release_deps(gtid, taskdata);
7027cc577a4SJonathan Peyton #endif
7037cc577a4SJonathan Peyton   }
7047cc577a4SJonathan Peyton 
7053041982dSJonathan Peyton   // td_flags.executing must be marked as 0 after __kmp_release_deps has been
7063041982dSJonathan Peyton   // called. Othertwise, if a task is executed immediately from the release_deps
7073041982dSJonathan Peyton   // code, the flag will be reset to 1 again by this same function
7087cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1);
7097cc577a4SJonathan Peyton   taskdata->td_flags.executing = 0; // suspend the finishing task
7107cc577a4SJonathan Peyton 
7113041982dSJonathan Peyton   KA_TRACE(
7123041982dSJonathan Peyton       20, ("__kmp_task_finish: T#%d finished task %p, %d incomplete children\n",
7137cc577a4SJonathan Peyton            gtid, taskdata, children));
7147cc577a4SJonathan Peyton 
7157cc577a4SJonathan Peyton #if OMP_40_ENABLED
7167cc577a4SJonathan Peyton   /* If the tasks' destructor thunk flag has been set, we need to invoke the
7173041982dSJonathan Peyton      destructor thunk that has been generated by the compiler. The code is
7183041982dSJonathan Peyton      placed here, since at this point other tasks might have been released
7197cc577a4SJonathan Peyton      hence overlapping the destructor invokations with some other work in the
7203041982dSJonathan Peyton      released tasks.  The OpenMP spec is not specific on when the destructors
7213041982dSJonathan Peyton      are invoked, so we should be free to choose. */
7227cc577a4SJonathan Peyton   if (taskdata->td_flags.destructors_thunk) {
7237cc577a4SJonathan Peyton     kmp_routine_entry_t destr_thunk = task->data1.destructors;
7247cc577a4SJonathan Peyton     KMP_ASSERT(destr_thunk);
7257cc577a4SJonathan Peyton     destr_thunk(gtid, task);
7267cc577a4SJonathan Peyton   }
7277cc577a4SJonathan Peyton #endif // OMP_40_ENABLED
7287cc577a4SJonathan Peyton 
7297cc577a4SJonathan Peyton   // bookkeeping for resuming task:
7307cc577a4SJonathan Peyton   // GEH - note tasking_ser => task_serial
7313041982dSJonathan Peyton   KMP_DEBUG_ASSERT(
7323041982dSJonathan Peyton       (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) ==
7337cc577a4SJonathan Peyton       taskdata->td_flags.task_serial);
7343041982dSJonathan Peyton   if (taskdata->td_flags.task_serial) {
7357cc577a4SJonathan Peyton     if (resumed_task == NULL) {
7363041982dSJonathan Peyton       resumed_task = taskdata->td_parent; // In a serialized task, the resumed
7373041982dSJonathan Peyton       // task is the parent
7383041982dSJonathan Peyton     } else
7397cc577a4SJonathan Peyton #if OMP_45_ENABLED
7407cc577a4SJonathan Peyton         if (!(task_team && task_team->tt.tt_found_proxy_tasks))
7417cc577a4SJonathan Peyton #endif
7427cc577a4SJonathan Peyton     {
7437cc577a4SJonathan Peyton       // verify resumed task passed in points to parent
7447cc577a4SJonathan Peyton       KMP_DEBUG_ASSERT(resumed_task == taskdata->td_parent);
7457cc577a4SJonathan Peyton     }
7463041982dSJonathan Peyton   } else {
7473041982dSJonathan Peyton     KMP_DEBUG_ASSERT(resumed_task !=
7483041982dSJonathan Peyton                      NULL); // verify that resumed task is passed as arguemnt
7497cc577a4SJonathan Peyton   }
7507cc577a4SJonathan Peyton 
7517cc577a4SJonathan Peyton   // Free this task and then ancestor tasks if they have no children.
7527cc577a4SJonathan Peyton   // Restore th_current_task first as suggested by John:
7537cc577a4SJonathan Peyton   // johnmc: if an asynchronous inquiry peers into the runtime system
7547cc577a4SJonathan Peyton   // it doesn't see the freed task as the current task.
7557cc577a4SJonathan Peyton   thread->th.th_current_task = resumed_task;
7567cc577a4SJonathan Peyton   __kmp_free_task_and_ancestors(gtid, taskdata, thread);
7577cc577a4SJonathan Peyton 
7587cc577a4SJonathan Peyton   // TODO: GEH - make sure root team implicit task is initialized properly.
7597cc577a4SJonathan Peyton   // KMP_DEBUG_ASSERT( resumed_task->td_flags.executing == 0 );
7607cc577a4SJonathan Peyton   resumed_task->td_flags.executing = 1; // resume previous task
7617cc577a4SJonathan Peyton 
7623041982dSJonathan Peyton   KA_TRACE(
7633041982dSJonathan Peyton       10, ("__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n",
7647cc577a4SJonathan Peyton            gtid, taskdata, resumed_task));
7657cc577a4SJonathan Peyton 
7667cc577a4SJonathan Peyton   return;
7677cc577a4SJonathan Peyton }
7687cc577a4SJonathan Peyton 
7697cc577a4SJonathan Peyton // __kmpc_omp_task_complete_if0: report that a task has completed execution
7703041982dSJonathan Peyton //
7717cc577a4SJonathan Peyton // loc_ref: source location information; points to end of task block.
7727cc577a4SJonathan Peyton // gtid: global thread number.
7737cc577a4SJonathan Peyton // task: task thunk for the completed task.
7743041982dSJonathan Peyton void __kmpc_omp_task_complete_if0(ident_t *loc_ref, kmp_int32 gtid,
7753041982dSJonathan Peyton                                   kmp_task_t *task) {
7767cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n",
7777cc577a4SJonathan Peyton                 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
7783041982dSJonathan Peyton   // this routine will provide task to resume
7793041982dSJonathan Peyton   __kmp_task_finish(gtid, task, NULL);
7807cc577a4SJonathan Peyton 
7817cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n",
7827cc577a4SJonathan Peyton                 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task)));
7837cc577a4SJonathan Peyton   return;
7847cc577a4SJonathan Peyton }
7857cc577a4SJonathan Peyton 
7867cc577a4SJonathan Peyton #ifdef TASK_UNUSED
7877cc577a4SJonathan Peyton // __kmpc_omp_task_complete: report that a task has completed execution
7887cc577a4SJonathan Peyton // NEVER GENERATED BY COMPILER, DEPRECATED!!!
7893041982dSJonathan Peyton void __kmpc_omp_task_complete(ident_t *loc_ref, kmp_int32 gtid,
7903041982dSJonathan Peyton                               kmp_task_t *task) {
7913041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n", gtid,
7923041982dSJonathan Peyton                 loc_ref, KMP_TASK_TO_TASKDATA(task)));
7937cc577a4SJonathan Peyton 
7947cc577a4SJonathan Peyton   __kmp_task_finish(gtid, task, NULL); // Not sure how to find task to resume
7957cc577a4SJonathan Peyton 
7963041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n", gtid,
7973041982dSJonathan Peyton                 loc_ref, KMP_TASK_TO_TASKDATA(task)));
7987cc577a4SJonathan Peyton   return;
7997cc577a4SJonathan Peyton }
8007cc577a4SJonathan Peyton #endif // TASK_UNUSED
8017cc577a4SJonathan Peyton 
8027cc577a4SJonathan Peyton #if OMPT_SUPPORT
8033041982dSJonathan Peyton // __kmp_task_init_ompt: Initialize OMPT fields maintained by a task. This will
8043041982dSJonathan Peyton //  only be called after ompt_tool, so we already know whether ompt is enabled
8053041982dSJonathan Peyton // or not.
8063041982dSJonathan Peyton static inline void __kmp_task_init_ompt(kmp_taskdata_t *task, int tid,
8073041982dSJonathan Peyton                                         void *function) {
8087cc577a4SJonathan Peyton   if (ompt_enabled) {
8097cc577a4SJonathan Peyton     task->ompt_task_info.task_id = __ompt_task_id_new(tid);
8107cc577a4SJonathan Peyton     task->ompt_task_info.function = function;
8117cc577a4SJonathan Peyton     task->ompt_task_info.frame.exit_runtime_frame = NULL;
8127cc577a4SJonathan Peyton     task->ompt_task_info.frame.reenter_runtime_frame = NULL;
8137cc577a4SJonathan Peyton #if OMP_40_ENABLED
8147cc577a4SJonathan Peyton     task->ompt_task_info.ndeps = 0;
8157cc577a4SJonathan Peyton     task->ompt_task_info.deps = NULL;
8167cc577a4SJonathan Peyton #endif /* OMP_40_ENABLED */
8177cc577a4SJonathan Peyton   }
8187cc577a4SJonathan Peyton }
8197cc577a4SJonathan Peyton #endif
8207cc577a4SJonathan Peyton 
8213041982dSJonathan Peyton // __kmp_init_implicit_task: Initialize the appropriate fields in the implicit
8223041982dSJonathan Peyton // task for a given thread
8237cc577a4SJonathan Peyton //
8247cc577a4SJonathan Peyton // loc_ref:  reference to source location of parallel region
8257cc577a4SJonathan Peyton // this_thr:  thread data structure corresponding to implicit task
8267cc577a4SJonathan Peyton // team: team for this_thr
8277cc577a4SJonathan Peyton // tid: thread id of given thread within team
8287cc577a4SJonathan Peyton // set_curr_task: TRUE if need to push current task to thread
8293041982dSJonathan Peyton // NOTE: Routine does not set up the implicit task ICVS.  This is assumed to
8303041982dSJonathan Peyton // have already been done elsewhere.
8317cc577a4SJonathan Peyton // TODO: Get better loc_ref.  Value passed in may be NULL
8323041982dSJonathan Peyton void __kmp_init_implicit_task(ident_t *loc_ref, kmp_info_t *this_thr,
8333041982dSJonathan Peyton                               kmp_team_t *team, int tid, int set_curr_task) {
8347cc577a4SJonathan Peyton   kmp_taskdata_t *task = &team->t.t_implicit_task_taskdata[tid];
8357cc577a4SJonathan Peyton 
8363041982dSJonathan Peyton   KF_TRACE(
8373041982dSJonathan Peyton       10,
8383041982dSJonathan Peyton       ("__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n",
8397cc577a4SJonathan Peyton        tid, team, task, set_curr_task ? "TRUE" : "FALSE"));
8407cc577a4SJonathan Peyton 
8417cc577a4SJonathan Peyton   task->td_task_id = KMP_GEN_TASK_ID();
8427cc577a4SJonathan Peyton   task->td_team = team;
8433041982dSJonathan Peyton   //    task->td_parent   = NULL;  // fix for CQ230101 (broken parent task info
8443041982dSJonathan Peyton   //    in debugger)
8457cc577a4SJonathan Peyton   task->td_ident = loc_ref;
8467cc577a4SJonathan Peyton   task->td_taskwait_ident = NULL;
8477cc577a4SJonathan Peyton   task->td_taskwait_counter = 0;
8487cc577a4SJonathan Peyton   task->td_taskwait_thread = 0;
8497cc577a4SJonathan Peyton 
8507cc577a4SJonathan Peyton   task->td_flags.tiedness = TASK_TIED;
8517cc577a4SJonathan Peyton   task->td_flags.tasktype = TASK_IMPLICIT;
8527cc577a4SJonathan Peyton #if OMP_45_ENABLED
8537cc577a4SJonathan Peyton   task->td_flags.proxy = TASK_FULL;
8547cc577a4SJonathan Peyton #endif
8557cc577a4SJonathan Peyton 
8567cc577a4SJonathan Peyton   // All implicit tasks are executed immediately, not deferred
8577cc577a4SJonathan Peyton   task->td_flags.task_serial = 1;
8587cc577a4SJonathan Peyton   task->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
8597cc577a4SJonathan Peyton   task->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
8607cc577a4SJonathan Peyton 
8617cc577a4SJonathan Peyton   task->td_flags.started = 1;
8627cc577a4SJonathan Peyton   task->td_flags.executing = 1;
8637cc577a4SJonathan Peyton   task->td_flags.complete = 0;
8647cc577a4SJonathan Peyton   task->td_flags.freed = 0;
8657cc577a4SJonathan Peyton 
8667cc577a4SJonathan Peyton #if OMP_40_ENABLED
8677cc577a4SJonathan Peyton   task->td_depnode = NULL;
8687cc577a4SJonathan Peyton #endif
8697cc577a4SJonathan Peyton 
8703041982dSJonathan Peyton   if (set_curr_task) { // only do this init first time thread is created
8717cc577a4SJonathan Peyton     task->td_incomplete_child_tasks = 0;
872642688b6SJonathan Peyton     // Not used: don't need to deallocate implicit task
873642688b6SJonathan Peyton     task->td_allocated_child_tasks = 0;
8747cc577a4SJonathan Peyton #if OMP_40_ENABLED
8757cc577a4SJonathan Peyton     task->td_taskgroup = NULL; // An implicit task does not have taskgroup
8767cc577a4SJonathan Peyton     task->td_dephash = NULL;
8777cc577a4SJonathan Peyton #endif
8787cc577a4SJonathan Peyton     __kmp_push_current_task_to_thread(this_thr, team, tid);
8797cc577a4SJonathan Peyton   } else {
8807cc577a4SJonathan Peyton     KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0);
8817cc577a4SJonathan Peyton     KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0);
8827cc577a4SJonathan Peyton   }
8837cc577a4SJonathan Peyton 
8847cc577a4SJonathan Peyton #if OMPT_SUPPORT
8857cc577a4SJonathan Peyton   __kmp_task_init_ompt(task, tid, NULL);
8867cc577a4SJonathan Peyton #endif
8877cc577a4SJonathan Peyton 
8883041982dSJonathan Peyton   KF_TRACE(10, ("__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n", tid,
8893041982dSJonathan Peyton                 team, task));
8907cc577a4SJonathan Peyton }
8917cc577a4SJonathan Peyton 
8923041982dSJonathan Peyton // __kmp_finish_implicit_task: Release resources associated to implicit tasks
8933041982dSJonathan Peyton // at the end of parallel regions. Some resources are kept for reuse in the next
8943041982dSJonathan Peyton // parallel region.
8957cc577a4SJonathan Peyton //
8963041982dSJonathan Peyton // thread:  thread data structure corresponding to implicit task
8973041982dSJonathan Peyton void __kmp_finish_implicit_task(kmp_info_t *thread) {
8987cc577a4SJonathan Peyton   kmp_taskdata_t *task = thread->th.th_current_task;
8997cc577a4SJonathan Peyton   if (task->td_dephash)
9007cc577a4SJonathan Peyton     __kmp_dephash_free_entries(thread, task->td_dephash);
9017cc577a4SJonathan Peyton }
9027cc577a4SJonathan Peyton 
9033041982dSJonathan Peyton // __kmp_free_implicit_task: Release resources associated to implicit tasks
9043041982dSJonathan Peyton // when these are destroyed regions
9057cc577a4SJonathan Peyton //
9063041982dSJonathan Peyton // thread:  thread data structure corresponding to implicit task
9073041982dSJonathan Peyton void __kmp_free_implicit_task(kmp_info_t *thread) {
9087cc577a4SJonathan Peyton   kmp_taskdata_t *task = thread->th.th_current_task;
9097cc577a4SJonathan Peyton   if (task->td_dephash)
9107cc577a4SJonathan Peyton     __kmp_dephash_free(thread, task->td_dephash);
9117cc577a4SJonathan Peyton   task->td_dephash = NULL;
9127cc577a4SJonathan Peyton }
9137cc577a4SJonathan Peyton 
9143041982dSJonathan Peyton // Round up a size to a power of two specified by val: Used to insert padding
9153041982dSJonathan Peyton // between structures co-allocated using a single malloc() call
9163041982dSJonathan Peyton static size_t __kmp_round_up_to_val(size_t size, size_t val) {
9177cc577a4SJonathan Peyton   if (size & (val - 1)) {
9187cc577a4SJonathan Peyton     size &= ~(val - 1);
9197cc577a4SJonathan Peyton     if (size <= KMP_SIZE_T_MAX - val) {
9207cc577a4SJonathan Peyton       size += val; // Round up if there is no overflow.
9217cc577a4SJonathan Peyton     }; // if
9227cc577a4SJonathan Peyton   }; // if
9237cc577a4SJonathan Peyton   return size;
9247cc577a4SJonathan Peyton } // __kmp_round_up_to_va
9257cc577a4SJonathan Peyton 
9267cc577a4SJonathan Peyton // __kmp_task_alloc: Allocate the taskdata and task data structures for a task
9277cc577a4SJonathan Peyton //
9287cc577a4SJonathan Peyton // loc_ref: source location information
9297cc577a4SJonathan Peyton // gtid: global thread number.
9303041982dSJonathan Peyton // flags: include tiedness & task type (explicit vs. implicit) of the ''new''
9313041982dSJonathan Peyton // task encountered. Converted from kmp_int32 to kmp_tasking_flags_t in routine.
9323041982dSJonathan Peyton // sizeof_kmp_task_t:  Size in bytes of kmp_task_t data structure including
9333041982dSJonathan Peyton // private vars accessed in task.
9343041982dSJonathan Peyton // sizeof_shareds:  Size in bytes of array of pointers to shared vars accessed
9353041982dSJonathan Peyton // in task.
9367cc577a4SJonathan Peyton // task_entry: Pointer to task code entry point generated by compiler.
9377cc577a4SJonathan Peyton // returns: a pointer to the allocated kmp_task_t structure (task).
9383041982dSJonathan Peyton kmp_task_t *__kmp_task_alloc(ident_t *loc_ref, kmp_int32 gtid,
9393041982dSJonathan Peyton                              kmp_tasking_flags_t *flags,
9407cc577a4SJonathan Peyton                              size_t sizeof_kmp_task_t, size_t sizeof_shareds,
9413041982dSJonathan Peyton                              kmp_routine_entry_t task_entry) {
9427cc577a4SJonathan Peyton   kmp_task_t *task;
9437cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata;
9447cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
9457cc577a4SJonathan Peyton   kmp_team_t *team = thread->th.th_team;
9467cc577a4SJonathan Peyton   kmp_taskdata_t *parent_task = thread->th.th_current_task;
9477cc577a4SJonathan Peyton   size_t shareds_offset;
9487cc577a4SJonathan Peyton 
9497cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) "
9507cc577a4SJonathan Peyton                 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
9517cc577a4SJonathan Peyton                 gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t,
9527cc577a4SJonathan Peyton                 sizeof_shareds, task_entry));
9537cc577a4SJonathan Peyton 
9547cc577a4SJonathan Peyton   if (parent_task->td_flags.final) {
9557cc577a4SJonathan Peyton     if (flags->merged_if0) {
9567cc577a4SJonathan Peyton     }
9577cc577a4SJonathan Peyton     flags->final = 1;
9587cc577a4SJonathan Peyton   }
9597cc577a4SJonathan Peyton 
9607cc577a4SJonathan Peyton #if OMP_45_ENABLED
9617cc577a4SJonathan Peyton   if (flags->proxy == TASK_PROXY) {
9627cc577a4SJonathan Peyton     flags->tiedness = TASK_UNTIED;
9637cc577a4SJonathan Peyton     flags->merged_if0 = 1;
9647cc577a4SJonathan Peyton 
9653041982dSJonathan Peyton     /* are we running in a sequential parallel or tskm_immediate_exec... we need
9663041982dSJonathan Peyton        tasking support enabled */
9677cc577a4SJonathan Peyton     if ((thread->th.th_task_team) == NULL) {
9687cc577a4SJonathan Peyton       /* This should only happen if the team is serialized
9693041982dSJonathan Peyton           setup a task team and propagate it to the thread */
9707cc577a4SJonathan Peyton       KMP_DEBUG_ASSERT(team->t.t_serialized);
9713041982dSJonathan Peyton       KA_TRACE(30,
9723041982dSJonathan Peyton                ("T#%d creating task team in __kmp_task_alloc for proxy task\n",
9733041982dSJonathan Peyton                 gtid));
9743041982dSJonathan Peyton       __kmp_task_team_setup(
9753041982dSJonathan Peyton           thread, team,
9763041982dSJonathan Peyton           1); // 1 indicates setup the current team regardless of nthreads
9777cc577a4SJonathan Peyton       thread->th.th_task_team = team->t.t_task_team[thread->th.th_task_state];
9787cc577a4SJonathan Peyton     }
9797cc577a4SJonathan Peyton     kmp_task_team_t *task_team = thread->th.th_task_team;
9807cc577a4SJonathan Peyton 
9817cc577a4SJonathan Peyton     /* tasking must be enabled now as the task might not be pushed */
9827cc577a4SJonathan Peyton     if (!KMP_TASKING_ENABLED(task_team)) {
9833041982dSJonathan Peyton       KA_TRACE(
9843041982dSJonathan Peyton           30,
9853041982dSJonathan Peyton           ("T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid));
9867cc577a4SJonathan Peyton       __kmp_enable_tasking(task_team, thread);
9877cc577a4SJonathan Peyton       kmp_int32 tid = thread->th.th_info.ds.ds_tid;
9887cc577a4SJonathan Peyton       kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
9897cc577a4SJonathan Peyton       // No lock needed since only owner can allocate
9907cc577a4SJonathan Peyton       if (thread_data->td.td_deque == NULL) {
9917cc577a4SJonathan Peyton         __kmp_alloc_task_deque(thread, thread_data);
9927cc577a4SJonathan Peyton       }
9937cc577a4SJonathan Peyton     }
9947cc577a4SJonathan Peyton 
9957cc577a4SJonathan Peyton     if (task_team->tt.tt_found_proxy_tasks == FALSE)
9967cc577a4SJonathan Peyton       TCW_4(task_team->tt.tt_found_proxy_tasks, TRUE);
9977cc577a4SJonathan Peyton   }
9987cc577a4SJonathan Peyton #endif
9997cc577a4SJonathan Peyton 
10007cc577a4SJonathan Peyton   // Calculate shared structure offset including padding after kmp_task_t struct
10017cc577a4SJonathan Peyton   // to align pointers in shared struct
10027cc577a4SJonathan Peyton   shareds_offset = sizeof(kmp_taskdata_t) + sizeof_kmp_task_t;
10037cc577a4SJonathan Peyton   shareds_offset = __kmp_round_up_to_val(shareds_offset, sizeof(void *));
10047cc577a4SJonathan Peyton 
10057cc577a4SJonathan Peyton   // Allocate a kmp_taskdata_t block and a kmp_task_t block.
10063041982dSJonathan Peyton   KA_TRACE(30, ("__kmp_task_alloc: T#%d First malloc size: %ld\n", gtid,
10073041982dSJonathan Peyton                 shareds_offset));
10083041982dSJonathan Peyton   KA_TRACE(30, ("__kmp_task_alloc: T#%d Second malloc size: %ld\n", gtid,
10093041982dSJonathan Peyton                 sizeof_shareds));
10107cc577a4SJonathan Peyton 
10117cc577a4SJonathan Peyton // Avoid double allocation here by combining shareds with taskdata
10127cc577a4SJonathan Peyton #if USE_FAST_MEMORY
10133041982dSJonathan Peyton   taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, shareds_offset +
10143041982dSJonathan Peyton                                                                sizeof_shareds);
10157cc577a4SJonathan Peyton #else /* ! USE_FAST_MEMORY */
10163041982dSJonathan Peyton   taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, shareds_offset +
10173041982dSJonathan Peyton                                                                sizeof_shareds);
10187cc577a4SJonathan Peyton #endif /* USE_FAST_MEMORY */
10197cc577a4SJonathan Peyton   ANNOTATE_HAPPENS_AFTER(taskdata);
10207cc577a4SJonathan Peyton 
10217cc577a4SJonathan Peyton   task = KMP_TASKDATA_TO_TASK(taskdata);
10227cc577a4SJonathan Peyton 
10237cc577a4SJonathan Peyton // Make sure task & taskdata are aligned appropriately
10247cc577a4SJonathan Peyton #if KMP_ARCH_X86 || KMP_ARCH_PPC64 || !KMP_HAVE_QUAD
10257cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (sizeof(double) - 1)) == 0);
10267cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (sizeof(double) - 1)) == 0);
10277cc577a4SJonathan Peyton #else
10287cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (sizeof(_Quad) - 1)) == 0);
10297cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (sizeof(_Quad) - 1)) == 0);
10307cc577a4SJonathan Peyton #endif
10317cc577a4SJonathan Peyton   if (sizeof_shareds > 0) {
10327cc577a4SJonathan Peyton     // Avoid double allocation here by combining shareds with taskdata
10337cc577a4SJonathan Peyton     task->shareds = &((char *)taskdata)[shareds_offset];
10347cc577a4SJonathan Peyton     // Make sure shareds struct is aligned to pointer size
10353041982dSJonathan Peyton     KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (sizeof(void *) - 1)) ==
10363041982dSJonathan Peyton                      0);
10377cc577a4SJonathan Peyton   } else {
10387cc577a4SJonathan Peyton     task->shareds = NULL;
10397cc577a4SJonathan Peyton   }
10407cc577a4SJonathan Peyton   task->routine = task_entry;
10417cc577a4SJonathan Peyton   task->part_id = 0; // AC: Always start with 0 part id
10427cc577a4SJonathan Peyton 
10437cc577a4SJonathan Peyton   taskdata->td_task_id = KMP_GEN_TASK_ID();
10447cc577a4SJonathan Peyton   taskdata->td_team = team;
10457cc577a4SJonathan Peyton   taskdata->td_alloc_thread = thread;
10467cc577a4SJonathan Peyton   taskdata->td_parent = parent_task;
10477cc577a4SJonathan Peyton   taskdata->td_level = parent_task->td_level + 1; // increment nesting level
10487cc577a4SJonathan Peyton   taskdata->td_untied_count = 0;
10497cc577a4SJonathan Peyton   taskdata->td_ident = loc_ref;
10507cc577a4SJonathan Peyton   taskdata->td_taskwait_ident = NULL;
10517cc577a4SJonathan Peyton   taskdata->td_taskwait_counter = 0;
10527cc577a4SJonathan Peyton   taskdata->td_taskwait_thread = 0;
10537cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_parent != NULL);
10547cc577a4SJonathan Peyton #if OMP_45_ENABLED
10557cc577a4SJonathan Peyton   // avoid copying icvs for proxy tasks
10567cc577a4SJonathan Peyton   if (flags->proxy == TASK_FULL)
10577cc577a4SJonathan Peyton #endif
10587cc577a4SJonathan Peyton     copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs);
10597cc577a4SJonathan Peyton 
10607cc577a4SJonathan Peyton   taskdata->td_flags.tiedness = flags->tiedness;
10617cc577a4SJonathan Peyton   taskdata->td_flags.final = flags->final;
10627cc577a4SJonathan Peyton   taskdata->td_flags.merged_if0 = flags->merged_if0;
10637cc577a4SJonathan Peyton #if OMP_40_ENABLED
10647cc577a4SJonathan Peyton   taskdata->td_flags.destructors_thunk = flags->destructors_thunk;
10657cc577a4SJonathan Peyton #endif // OMP_40_ENABLED
10667cc577a4SJonathan Peyton #if OMP_45_ENABLED
10677cc577a4SJonathan Peyton   taskdata->td_flags.proxy = flags->proxy;
10687cc577a4SJonathan Peyton   taskdata->td_task_team = thread->th.th_task_team;
10697cc577a4SJonathan Peyton   taskdata->td_size_alloc = shareds_offset + sizeof_shareds;
10707cc577a4SJonathan Peyton #endif
10717cc577a4SJonathan Peyton   taskdata->td_flags.tasktype = TASK_EXPLICIT;
10727cc577a4SJonathan Peyton 
10737cc577a4SJonathan Peyton   // GEH - TODO: fix this to copy parent task's value of tasking_ser flag
10747cc577a4SJonathan Peyton   taskdata->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec);
10757cc577a4SJonathan Peyton 
10767cc577a4SJonathan Peyton   // GEH - TODO: fix this to copy parent task's value of team_serial flag
10777cc577a4SJonathan Peyton   taskdata->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0;
10787cc577a4SJonathan Peyton 
10793041982dSJonathan Peyton   // GEH - Note we serialize the task if the team is serialized to make sure
10803041982dSJonathan Peyton   // implicit parallel region tasks are not left until program termination to
10813041982dSJonathan Peyton   // execute. Also, it helps locality to execute immediately.
10823041982dSJonathan Peyton 
10833041982dSJonathan Peyton   taskdata->td_flags.task_serial =
10843041982dSJonathan Peyton       (parent_task->td_flags.final || taskdata->td_flags.team_serial ||
10853041982dSJonathan Peyton        taskdata->td_flags.tasking_ser);
10867cc577a4SJonathan Peyton 
10877cc577a4SJonathan Peyton   taskdata->td_flags.started = 0;
10887cc577a4SJonathan Peyton   taskdata->td_flags.executing = 0;
10897cc577a4SJonathan Peyton   taskdata->td_flags.complete = 0;
10907cc577a4SJonathan Peyton   taskdata->td_flags.freed = 0;
10917cc577a4SJonathan Peyton 
10927cc577a4SJonathan Peyton   taskdata->td_flags.native = flags->native;
10937cc577a4SJonathan Peyton 
10947cc577a4SJonathan Peyton   taskdata->td_incomplete_child_tasks = 0;
10953041982dSJonathan Peyton   taskdata->td_allocated_child_tasks = 1; // start at one because counts current
10963041982dSJonathan Peyton // task and children
10977cc577a4SJonathan Peyton #if OMP_40_ENABLED
10983041982dSJonathan Peyton   taskdata->td_taskgroup =
10993041982dSJonathan Peyton       parent_task->td_taskgroup; // task inherits taskgroup from the parent task
11007cc577a4SJonathan Peyton   taskdata->td_dephash = NULL;
11017cc577a4SJonathan Peyton   taskdata->td_depnode = NULL;
11027cc577a4SJonathan Peyton #endif
11037cc577a4SJonathan Peyton 
11043041982dSJonathan Peyton // Only need to keep track of child task counts if team parallel and tasking not
11053041982dSJonathan Peyton // serialized or if it is a proxy task
11067cc577a4SJonathan Peyton #if OMP_45_ENABLED
11073041982dSJonathan Peyton   if (flags->proxy == TASK_PROXY ||
11083041982dSJonathan Peyton       !(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser))
11097cc577a4SJonathan Peyton #else
11107cc577a4SJonathan Peyton   if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser))
11117cc577a4SJonathan Peyton #endif
11127cc577a4SJonathan Peyton   {
1113*c47afcd9SAndrey Churbanov     KMP_TEST_THEN_INC32(
1114*c47afcd9SAndrey Churbanov         CCAST(kmp_int32 *, &parent_task->td_incomplete_child_tasks));
11157cc577a4SJonathan Peyton #if OMP_40_ENABLED
11167cc577a4SJonathan Peyton     if (parent_task->td_taskgroup)
11177cc577a4SJonathan Peyton       KMP_TEST_THEN_INC32((kmp_int32 *)(&parent_task->td_taskgroup->count));
11187cc577a4SJonathan Peyton #endif
11193041982dSJonathan Peyton     // Only need to keep track of allocated child tasks for explicit tasks since
11203041982dSJonathan Peyton     // implicit not deallocated
11217cc577a4SJonathan Peyton     if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) {
11223041982dSJonathan Peyton       KMP_TEST_THEN_INC32(
1123*c47afcd9SAndrey Churbanov           CCAST(kmp_int32 *, &taskdata->td_parent->td_allocated_child_tasks));
11247cc577a4SJonathan Peyton     }
11257cc577a4SJonathan Peyton   }
11267cc577a4SJonathan Peyton 
11277cc577a4SJonathan Peyton   KA_TRACE(20, ("__kmp_task_alloc(exit): T#%d created task %p parent=%p\n",
11287cc577a4SJonathan Peyton                 gtid, taskdata, taskdata->td_parent));
11297cc577a4SJonathan Peyton   ANNOTATE_HAPPENS_BEFORE(task);
11307cc577a4SJonathan Peyton 
11317cc577a4SJonathan Peyton #if OMPT_SUPPORT
11327cc577a4SJonathan Peyton   __kmp_task_init_ompt(taskdata, gtid, (void *)task_entry);
11337cc577a4SJonathan Peyton #endif
11347cc577a4SJonathan Peyton 
11357cc577a4SJonathan Peyton   return task;
11367cc577a4SJonathan Peyton }
11377cc577a4SJonathan Peyton 
11383041982dSJonathan Peyton kmp_task_t *__kmpc_omp_task_alloc(ident_t *loc_ref, kmp_int32 gtid,
11393041982dSJonathan Peyton                                   kmp_int32 flags, size_t sizeof_kmp_task_t,
11403041982dSJonathan Peyton                                   size_t sizeof_shareds,
11413041982dSJonathan Peyton                                   kmp_routine_entry_t task_entry) {
11427cc577a4SJonathan Peyton   kmp_task_t *retval;
11437cc577a4SJonathan Peyton   kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
11447cc577a4SJonathan Peyton 
11457cc577a4SJonathan Peyton   input_flags->native = FALSE;
11467cc577a4SJonathan Peyton // __kmp_task_alloc() sets up all other runtime flags
11477cc577a4SJonathan Peyton 
11487cc577a4SJonathan Peyton #if OMP_45_ENABLED
11497cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s) "
11507cc577a4SJonathan Peyton                 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
11517cc577a4SJonathan Peyton                 gtid, loc_ref, input_flags->tiedness ? "tied  " : "untied",
11523041982dSJonathan Peyton                 input_flags->proxy ? "proxy" : "", sizeof_kmp_task_t,
11533041982dSJonathan Peyton                 sizeof_shareds, task_entry));
11547cc577a4SJonathan Peyton #else
11557cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s) "
11567cc577a4SJonathan Peyton                 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n",
11577cc577a4SJonathan Peyton                 gtid, loc_ref, input_flags->tiedness ? "tied  " : "untied",
11587cc577a4SJonathan Peyton                 sizeof_kmp_task_t, sizeof_shareds, task_entry));
11597cc577a4SJonathan Peyton #endif
11607cc577a4SJonathan Peyton 
11617cc577a4SJonathan Peyton   retval = __kmp_task_alloc(loc_ref, gtid, input_flags, sizeof_kmp_task_t,
11627cc577a4SJonathan Peyton                             sizeof_shareds, task_entry);
11637cc577a4SJonathan Peyton 
11647cc577a4SJonathan Peyton   KA_TRACE(20, ("__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval));
11657cc577a4SJonathan Peyton 
11667cc577a4SJonathan Peyton   return retval;
11677cc577a4SJonathan Peyton }
11687cc577a4SJonathan Peyton 
11697cc577a4SJonathan Peyton //  __kmp_invoke_task: invoke the specified task
11707cc577a4SJonathan Peyton //
11717cc577a4SJonathan Peyton // gtid: global thread ID of caller
11727cc577a4SJonathan Peyton // task: the task to invoke
11737cc577a4SJonathan Peyton // current_task: the task to resume after task invokation
11743041982dSJonathan Peyton static void __kmp_invoke_task(kmp_int32 gtid, kmp_task_t *task,
11753041982dSJonathan Peyton                               kmp_taskdata_t *current_task) {
11767cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
11777cc577a4SJonathan Peyton   kmp_uint64 cur_time;
11787cc577a4SJonathan Peyton #if OMP_40_ENABLED
11797cc577a4SJonathan Peyton   int discard = 0 /* false */;
11807cc577a4SJonathan Peyton #endif
11813041982dSJonathan Peyton   KA_TRACE(
11823041982dSJonathan Peyton       30, ("__kmp_invoke_task(enter): T#%d invoking task %p, current_task=%p\n",
11837cc577a4SJonathan Peyton            gtid, taskdata, current_task));
11847cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task);
11857cc577a4SJonathan Peyton #if OMP_45_ENABLED
11867cc577a4SJonathan Peyton   if (taskdata->td_flags.proxy == TASK_PROXY &&
11873041982dSJonathan Peyton       taskdata->td_flags.complete == 1) {
11887cc577a4SJonathan Peyton     // This is a proxy task that was already completed but it needs to run
11897cc577a4SJonathan Peyton     // its bottom-half finish
11903041982dSJonathan Peyton     KA_TRACE(
11913041982dSJonathan Peyton         30,
11923041982dSJonathan Peyton         ("__kmp_invoke_task: T#%d running bottom finish for proxy task %p\n",
11937cc577a4SJonathan Peyton          gtid, taskdata));
11947cc577a4SJonathan Peyton 
11957cc577a4SJonathan Peyton     __kmp_bottom_half_finish_proxy(gtid, task);
11967cc577a4SJonathan Peyton 
11973041982dSJonathan Peyton     KA_TRACE(30, ("__kmp_invoke_task(exit): T#%d completed bottom finish for "
11983041982dSJonathan Peyton                   "proxy task %p, resuming task %p\n",
11993041982dSJonathan Peyton                   gtid, taskdata, current_task));
12007cc577a4SJonathan Peyton 
12017cc577a4SJonathan Peyton     return;
12027cc577a4SJonathan Peyton   }
12037cc577a4SJonathan Peyton #endif
12047cc577a4SJonathan Peyton 
12057cc577a4SJonathan Peyton #if USE_ITT_BUILD && USE_ITT_NOTIFY
12067cc577a4SJonathan Peyton   if (__kmp_forkjoin_frames_mode == 3) {
12073041982dSJonathan Peyton     // Get the current time stamp to measure task execution time to correct
12083041982dSJonathan Peyton     // barrier imbalance time
12097cc577a4SJonathan Peyton     cur_time = __itt_get_timestamp();
12107cc577a4SJonathan Peyton   }
12117cc577a4SJonathan Peyton #endif
12127cc577a4SJonathan Peyton 
12137cc577a4SJonathan Peyton #if OMP_45_ENABLED
12147cc577a4SJonathan Peyton   // Proxy tasks are not handled by the runtime
12157cc577a4SJonathan Peyton   if (taskdata->td_flags.proxy != TASK_PROXY) {
12167cc577a4SJonathan Peyton #endif
12177cc577a4SJonathan Peyton     ANNOTATE_HAPPENS_AFTER(task);
12187cc577a4SJonathan Peyton     __kmp_task_start(gtid, task, current_task);
12197cc577a4SJonathan Peyton #if OMP_45_ENABLED
12207cc577a4SJonathan Peyton   }
12217cc577a4SJonathan Peyton #endif
12227cc577a4SJonathan Peyton 
12237cc577a4SJonathan Peyton #if OMPT_SUPPORT
12247cc577a4SJonathan Peyton   ompt_thread_info_t oldInfo;
12257cc577a4SJonathan Peyton   kmp_info_t *thread;
12267cc577a4SJonathan Peyton   if (ompt_enabled) {
12277cc577a4SJonathan Peyton     // Store the threads states and restore them after the task
12287cc577a4SJonathan Peyton     thread = __kmp_threads[gtid];
12297cc577a4SJonathan Peyton     oldInfo = thread->th.ompt_thread_info;
12307cc577a4SJonathan Peyton     thread->th.ompt_thread_info.wait_id = 0;
12317cc577a4SJonathan Peyton     thread->th.ompt_thread_info.state = ompt_state_work_parallel;
12323041982dSJonathan Peyton     taskdata->ompt_task_info.frame.exit_runtime_frame =
12333041982dSJonathan Peyton         __builtin_frame_address(0);
12347cc577a4SJonathan Peyton   }
12357cc577a4SJonathan Peyton #endif
12367cc577a4SJonathan Peyton 
12377cc577a4SJonathan Peyton #if OMP_40_ENABLED
12387cc577a4SJonathan Peyton   // TODO: cancel tasks if the parallel region has also been cancelled
12397cc577a4SJonathan Peyton   // TODO: check if this sequence can be hoisted above __kmp_task_start
12407cc577a4SJonathan Peyton   // if cancellation has been enabled for this run ...
12417cc577a4SJonathan Peyton   if (__kmp_omp_cancellation) {
12427cc577a4SJonathan Peyton     kmp_info_t *this_thr = __kmp_threads[gtid];
12437cc577a4SJonathan Peyton     kmp_team_t *this_team = this_thr->th.th_team;
12447cc577a4SJonathan Peyton     kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
12453041982dSJonathan Peyton     if ((taskgroup && taskgroup->cancel_request) ||
12463041982dSJonathan Peyton         (this_team->t.t_cancel_request == cancel_parallel)) {
12477cc577a4SJonathan Peyton       KMP_COUNT_BLOCK(TASK_cancelled);
12487cc577a4SJonathan Peyton       // this task belongs to a task group and we need to cancel it
12497cc577a4SJonathan Peyton       discard = 1 /* true */;
12507cc577a4SJonathan Peyton     }
12517cc577a4SJonathan Peyton   }
12527cc577a4SJonathan Peyton 
12537cc577a4SJonathan Peyton   // Invoke the task routine and pass in relevant data.
12547cc577a4SJonathan Peyton   // Thunks generated by gcc take a different argument list.
12557cc577a4SJonathan Peyton   if (!discard) {
12567cc577a4SJonathan Peyton #if KMP_STATS_ENABLED
12577cc577a4SJonathan Peyton     KMP_COUNT_BLOCK(TASK_executed);
12587cc577a4SJonathan Peyton     switch (KMP_GET_THREAD_STATE()) {
12593041982dSJonathan Peyton     case FORK_JOIN_BARRIER:
12603041982dSJonathan Peyton       KMP_PUSH_PARTITIONED_TIMER(OMP_task_join_bar);
12613041982dSJonathan Peyton       break;
12623041982dSJonathan Peyton     case PLAIN_BARRIER:
12633041982dSJonathan Peyton       KMP_PUSH_PARTITIONED_TIMER(OMP_task_plain_bar);
12643041982dSJonathan Peyton       break;
12653041982dSJonathan Peyton     case TASKYIELD:
12663041982dSJonathan Peyton       KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskyield);
12673041982dSJonathan Peyton       break;
12683041982dSJonathan Peyton     case TASKWAIT:
12693041982dSJonathan Peyton       KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskwait);
12703041982dSJonathan Peyton       break;
12713041982dSJonathan Peyton     case TASKGROUP:
12723041982dSJonathan Peyton       KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskgroup);
12733041982dSJonathan Peyton       break;
12743041982dSJonathan Peyton     default:
12753041982dSJonathan Peyton       KMP_PUSH_PARTITIONED_TIMER(OMP_task_immediate);
12763041982dSJonathan Peyton       break;
12777cc577a4SJonathan Peyton     }
12787cc577a4SJonathan Peyton #endif // KMP_STATS_ENABLED
12797cc577a4SJonathan Peyton #endif // OMP_40_ENABLED
12807cc577a4SJonathan Peyton 
12817cc577a4SJonathan Peyton #if OMPT_SUPPORT && OMPT_TRACE
12827cc577a4SJonathan Peyton     /* let OMPT know that we're about to run this task */
12833041982dSJonathan Peyton     if (ompt_enabled && ompt_callbacks.ompt_callback(ompt_event_task_switch)) {
12847cc577a4SJonathan Peyton       ompt_callbacks.ompt_callback(ompt_event_task_switch)(
12857cc577a4SJonathan Peyton           current_task->ompt_task_info.task_id,
12867cc577a4SJonathan Peyton           taskdata->ompt_task_info.task_id);
12877cc577a4SJonathan Peyton     }
12887cc577a4SJonathan Peyton #endif
12897cc577a4SJonathan Peyton 
12907cc577a4SJonathan Peyton #ifdef KMP_GOMP_COMPAT
12917cc577a4SJonathan Peyton     if (taskdata->td_flags.native) {
12927cc577a4SJonathan Peyton       ((void (*)(void *))(*(task->routine)))(task->shareds);
12933041982dSJonathan Peyton     } else
12947cc577a4SJonathan Peyton #endif /* KMP_GOMP_COMPAT */
12957cc577a4SJonathan Peyton     {
12967cc577a4SJonathan Peyton       (*(task->routine))(gtid, task);
12977cc577a4SJonathan Peyton     }
12987cc577a4SJonathan Peyton     KMP_POP_PARTITIONED_TIMER();
12997cc577a4SJonathan Peyton 
13007cc577a4SJonathan Peyton #if OMPT_SUPPORT && OMPT_TRACE
13017cc577a4SJonathan Peyton     /* let OMPT know that we're returning to the callee task */
13023041982dSJonathan Peyton     if (ompt_enabled && ompt_callbacks.ompt_callback(ompt_event_task_switch)) {
13037cc577a4SJonathan Peyton       ompt_callbacks.ompt_callback(ompt_event_task_switch)(
13047cc577a4SJonathan Peyton           taskdata->ompt_task_info.task_id,
13057cc577a4SJonathan Peyton           current_task->ompt_task_info.task_id);
13067cc577a4SJonathan Peyton     }
13077cc577a4SJonathan Peyton #endif
13087cc577a4SJonathan Peyton 
13097cc577a4SJonathan Peyton #if OMP_40_ENABLED
13107cc577a4SJonathan Peyton   }
13117cc577a4SJonathan Peyton #endif // OMP_40_ENABLED
13127cc577a4SJonathan Peyton 
13137cc577a4SJonathan Peyton #if OMPT_SUPPORT
13147cc577a4SJonathan Peyton   if (ompt_enabled) {
13157cc577a4SJonathan Peyton     thread->th.ompt_thread_info = oldInfo;
13167cc577a4SJonathan Peyton     taskdata->ompt_task_info.frame.exit_runtime_frame = NULL;
13177cc577a4SJonathan Peyton   }
13187cc577a4SJonathan Peyton #endif
13197cc577a4SJonathan Peyton 
13207cc577a4SJonathan Peyton #if OMP_45_ENABLED
13217cc577a4SJonathan Peyton   // Proxy tasks are not handled by the runtime
13227cc577a4SJonathan Peyton   if (taskdata->td_flags.proxy != TASK_PROXY) {
13237cc577a4SJonathan Peyton #endif
13247cc577a4SJonathan Peyton     ANNOTATE_HAPPENS_BEFORE(taskdata->td_parent);
13257cc577a4SJonathan Peyton     __kmp_task_finish(gtid, task, current_task);
13267cc577a4SJonathan Peyton #if OMP_45_ENABLED
13277cc577a4SJonathan Peyton   }
13287cc577a4SJonathan Peyton #endif
13297cc577a4SJonathan Peyton 
13307cc577a4SJonathan Peyton #if USE_ITT_BUILD && USE_ITT_NOTIFY
13317cc577a4SJonathan Peyton   // Barrier imbalance - correct arrive time after the task finished
13327cc577a4SJonathan Peyton   if (__kmp_forkjoin_frames_mode == 3) {
13337cc577a4SJonathan Peyton     kmp_info_t *this_thr = __kmp_threads[gtid];
13347cc577a4SJonathan Peyton     if (this_thr->th.th_bar_arrive_time) {
13357cc577a4SJonathan Peyton       this_thr->th.th_bar_arrive_time += (__itt_get_timestamp() - cur_time);
13367cc577a4SJonathan Peyton     }
13377cc577a4SJonathan Peyton   }
13387cc577a4SJonathan Peyton #endif
13393041982dSJonathan Peyton   KA_TRACE(
13403041982dSJonathan Peyton       30,
13413041982dSJonathan Peyton       ("__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n",
13427cc577a4SJonathan Peyton        gtid, taskdata, current_task));
13437cc577a4SJonathan Peyton   return;
13447cc577a4SJonathan Peyton }
13457cc577a4SJonathan Peyton 
13467cc577a4SJonathan Peyton // __kmpc_omp_task_parts: Schedule a thread-switchable task for execution
13477cc577a4SJonathan Peyton //
13487cc577a4SJonathan Peyton // loc_ref: location of original task pragma (ignored)
13497cc577a4SJonathan Peyton // gtid: Global Thread ID of encountering thread
13507cc577a4SJonathan Peyton // new_task: task thunk allocated by __kmp_omp_task_alloc() for the ''new task''
13517cc577a4SJonathan Peyton // Returns:
13523041982dSJonathan Peyton //    TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to
13533041982dSJonathan Peyton //    be resumed later.
13543041982dSJonathan Peyton //    TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be
13553041982dSJonathan Peyton //    resumed later.
13563041982dSJonathan Peyton kmp_int32 __kmpc_omp_task_parts(ident_t *loc_ref, kmp_int32 gtid,
13573041982dSJonathan Peyton                                 kmp_task_t *new_task) {
13587cc577a4SJonathan Peyton   kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
13597cc577a4SJonathan Peyton 
13603041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n", gtid,
13613041982dSJonathan Peyton                 loc_ref, new_taskdata));
13627cc577a4SJonathan Peyton 
13633041982dSJonathan Peyton   /* Should we execute the new task or queue it? For now, let's just always try
13643041982dSJonathan Peyton      to queue it.  If the queue fills up, then we'll execute it.  */
13657cc577a4SJonathan Peyton 
13667cc577a4SJonathan Peyton   if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED) // if cannot defer
13677cc577a4SJonathan Peyton   { // Execute this task immediately
13687cc577a4SJonathan Peyton     kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
13697cc577a4SJonathan Peyton     new_taskdata->td_flags.task_serial = 1;
13707cc577a4SJonathan Peyton     __kmp_invoke_task(gtid, new_task, current_task);
13717cc577a4SJonathan Peyton   }
13727cc577a4SJonathan Peyton 
13733041982dSJonathan Peyton   KA_TRACE(
13743041982dSJonathan Peyton       10,
13753041982dSJonathan Peyton       ("__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: "
13763041982dSJonathan Peyton        "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n",
13773041982dSJonathan Peyton        gtid, loc_ref, new_taskdata));
13787cc577a4SJonathan Peyton 
13797cc577a4SJonathan Peyton   ANNOTATE_HAPPENS_BEFORE(new_task);
13807cc577a4SJonathan Peyton   return TASK_CURRENT_NOT_QUEUED;
13817cc577a4SJonathan Peyton }
13827cc577a4SJonathan Peyton 
13837cc577a4SJonathan Peyton // __kmp_omp_task: Schedule a non-thread-switchable task for execution
13843041982dSJonathan Peyton //
13857cc577a4SJonathan Peyton // gtid: Global Thread ID of encountering thread
13867cc577a4SJonathan Peyton // new_task:non-thread-switchable task thunk allocated by __kmp_omp_task_alloc()
13873041982dSJonathan Peyton // serialize_immediate: if TRUE then if the task is executed immediately its
13883041982dSJonathan Peyton // execution will be serialized
13893041982dSJonathan Peyton // Returns:
13903041982dSJonathan Peyton //    TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to
13913041982dSJonathan Peyton //    be resumed later.
13923041982dSJonathan Peyton //    TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be
13933041982dSJonathan Peyton //    resumed later.
13943041982dSJonathan Peyton kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task,
13953041982dSJonathan Peyton                          bool serialize_immediate) {
13967cc577a4SJonathan Peyton   kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
13977cc577a4SJonathan Peyton 
13987cc577a4SJonathan Peyton #if OMPT_SUPPORT
13997cc577a4SJonathan Peyton   if (ompt_enabled) {
14007cc577a4SJonathan Peyton     new_taskdata->ompt_task_info.frame.reenter_runtime_frame =
14017cc577a4SJonathan Peyton         __builtin_frame_address(1);
14027cc577a4SJonathan Peyton   }
14037cc577a4SJonathan Peyton #endif
14047cc577a4SJonathan Peyton 
14057cc577a4SJonathan Peyton /* Should we execute the new task or queue it? For now, let's just always try to
14067cc577a4SJonathan Peyton    queue it.  If the queue fills up, then we'll execute it.  */
14077cc577a4SJonathan Peyton #if OMP_45_ENABLED
14083041982dSJonathan Peyton   if (new_taskdata->td_flags.proxy == TASK_PROXY ||
14093041982dSJonathan Peyton       __kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED) // if cannot defer
14107cc577a4SJonathan Peyton #else
14117cc577a4SJonathan Peyton   if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED) // if cannot defer
14127cc577a4SJonathan Peyton #endif
14137cc577a4SJonathan Peyton   { // Execute this task immediately
14147cc577a4SJonathan Peyton     kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task;
14157cc577a4SJonathan Peyton     if (serialize_immediate)
14167cc577a4SJonathan Peyton       new_taskdata->td_flags.task_serial = 1;
14177cc577a4SJonathan Peyton     __kmp_invoke_task(gtid, new_task, current_task);
14187cc577a4SJonathan Peyton   }
14197cc577a4SJonathan Peyton 
14207cc577a4SJonathan Peyton #if OMPT_SUPPORT
14217cc577a4SJonathan Peyton   if (ompt_enabled) {
14227cc577a4SJonathan Peyton     new_taskdata->ompt_task_info.frame.reenter_runtime_frame = NULL;
14237cc577a4SJonathan Peyton   }
14247cc577a4SJonathan Peyton #endif
14257cc577a4SJonathan Peyton 
14267cc577a4SJonathan Peyton   ANNOTATE_HAPPENS_BEFORE(new_task);
14277cc577a4SJonathan Peyton   return TASK_CURRENT_NOT_QUEUED;
14287cc577a4SJonathan Peyton }
14297cc577a4SJonathan Peyton 
14303041982dSJonathan Peyton // __kmpc_omp_task: Wrapper around __kmp_omp_task to schedule a
14313041982dSJonathan Peyton // non-thread-switchable task from the parent thread only!
14323041982dSJonathan Peyton //
14337cc577a4SJonathan Peyton // loc_ref: location of original task pragma (ignored)
14347cc577a4SJonathan Peyton // gtid: Global Thread ID of encountering thread
14353041982dSJonathan Peyton // new_task: non-thread-switchable task thunk allocated by
14363041982dSJonathan Peyton // __kmp_omp_task_alloc()
14373041982dSJonathan Peyton // Returns:
14383041982dSJonathan Peyton //    TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to
14393041982dSJonathan Peyton //    be resumed later.
14403041982dSJonathan Peyton //    TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be
14413041982dSJonathan Peyton //    resumed later.
14423041982dSJonathan Peyton kmp_int32 __kmpc_omp_task(ident_t *loc_ref, kmp_int32 gtid,
14433041982dSJonathan Peyton                           kmp_task_t *new_task) {
14447cc577a4SJonathan Peyton   kmp_int32 res;
14457cc577a4SJonathan Peyton   KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK);
14467cc577a4SJonathan Peyton 
14477cc577a4SJonathan Peyton #if KMP_DEBUG
14487cc577a4SJonathan Peyton   kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task);
14497cc577a4SJonathan Peyton #endif
14503041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref,
14513041982dSJonathan Peyton                 new_taskdata));
14527cc577a4SJonathan Peyton 
14537cc577a4SJonathan Peyton   res = __kmp_omp_task(gtid, new_task, true);
14547cc577a4SJonathan Peyton 
14553041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_task(exit): T#%d returning "
14563041982dSJonathan Peyton                 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n",
14577cc577a4SJonathan Peyton                 gtid, loc_ref, new_taskdata));
14587cc577a4SJonathan Peyton   return res;
14597cc577a4SJonathan Peyton }
14607cc577a4SJonathan Peyton 
14613041982dSJonathan Peyton // __kmpc_omp_taskwait: Wait until all tasks generated by the current task are
14623041982dSJonathan Peyton // complete
14633041982dSJonathan Peyton kmp_int32 __kmpc_omp_taskwait(ident_t *loc_ref, kmp_int32 gtid) {
14647cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata;
14657cc577a4SJonathan Peyton   kmp_info_t *thread;
14667cc577a4SJonathan Peyton   int thread_finished = FALSE;
14677cc577a4SJonathan Peyton   KMP_SET_THREAD_STATE_BLOCK(TASKWAIT);
14687cc577a4SJonathan Peyton 
14697cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref));
14707cc577a4SJonathan Peyton 
14717cc577a4SJonathan Peyton   if (__kmp_tasking_mode != tskm_immediate_exec) {
14727cc577a4SJonathan Peyton     thread = __kmp_threads[gtid];
14737cc577a4SJonathan Peyton     taskdata = thread->th.th_current_task;
14747cc577a4SJonathan Peyton #if OMPT_SUPPORT && OMPT_TRACE
14757cc577a4SJonathan Peyton     ompt_task_id_t my_task_id;
14767cc577a4SJonathan Peyton     ompt_parallel_id_t my_parallel_id;
14777cc577a4SJonathan Peyton 
14787cc577a4SJonathan Peyton     if (ompt_enabled) {
14797cc577a4SJonathan Peyton       kmp_team_t *team = thread->th.th_team;
14807cc577a4SJonathan Peyton       my_task_id = taskdata->ompt_task_info.task_id;
14817cc577a4SJonathan Peyton       my_parallel_id = team->t.ompt_team_info.parallel_id;
14827cc577a4SJonathan Peyton 
14833041982dSJonathan Peyton       taskdata->ompt_task_info.frame.reenter_runtime_frame =
14843041982dSJonathan Peyton           __builtin_frame_address(1);
14857cc577a4SJonathan Peyton       if (ompt_callbacks.ompt_callback(ompt_event_taskwait_begin)) {
14863041982dSJonathan Peyton         ompt_callbacks.ompt_callback(ompt_event_taskwait_begin)(my_parallel_id,
14873041982dSJonathan Peyton                                                                 my_task_id);
14887cc577a4SJonathan Peyton       }
14897cc577a4SJonathan Peyton     }
14907cc577a4SJonathan Peyton #endif
14917cc577a4SJonathan Peyton 
14923041982dSJonathan Peyton // Debugger: The taskwait is active. Store location and thread encountered the
14933041982dSJonathan Peyton // taskwait.
14947cc577a4SJonathan Peyton #if USE_ITT_BUILD
14957cc577a4SJonathan Peyton // Note: These values are used by ITT events as well.
14967cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
14977cc577a4SJonathan Peyton     taskdata->td_taskwait_counter += 1;
14987cc577a4SJonathan Peyton     taskdata->td_taskwait_ident = loc_ref;
14997cc577a4SJonathan Peyton     taskdata->td_taskwait_thread = gtid + 1;
15007cc577a4SJonathan Peyton 
15017cc577a4SJonathan Peyton #if USE_ITT_BUILD
15027cc577a4SJonathan Peyton     void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
15037cc577a4SJonathan Peyton     if (itt_sync_obj != NULL)
15047cc577a4SJonathan Peyton       __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
15057cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
15067cc577a4SJonathan Peyton 
15073041982dSJonathan Peyton     bool must_wait =
15083041982dSJonathan Peyton         !taskdata->td_flags.team_serial && !taskdata->td_flags.final;
15097cc577a4SJonathan Peyton 
15107cc577a4SJonathan Peyton #if OMP_45_ENABLED
15113041982dSJonathan Peyton     must_wait = must_wait || (thread->th.th_task_team != NULL &&
15123041982dSJonathan Peyton                               thread->th.th_task_team->tt.tt_found_proxy_tasks);
15137cc577a4SJonathan Peyton #endif
15143041982dSJonathan Peyton     if (must_wait) {
1515*c47afcd9SAndrey Churbanov       kmp_flag_32 flag(
1516*c47afcd9SAndrey Churbanov           RCAST(volatile kmp_uint32 *, &taskdata->td_incomplete_child_tasks),
1517*c47afcd9SAndrey Churbanov           0U);
15187cc577a4SJonathan Peyton       while (TCR_4(taskdata->td_incomplete_child_tasks) != 0) {
15193041982dSJonathan Peyton         flag.execute_tasks(thread, gtid, FALSE,
15203041982dSJonathan Peyton                            &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
15213041982dSJonathan Peyton                            __kmp_task_stealing_constraint);
15227cc577a4SJonathan Peyton       }
15237cc577a4SJonathan Peyton     }
15247cc577a4SJonathan Peyton #if USE_ITT_BUILD
15257cc577a4SJonathan Peyton     if (itt_sync_obj != NULL)
15267cc577a4SJonathan Peyton       __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
15277cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
15287cc577a4SJonathan Peyton 
15293041982dSJonathan Peyton     // Debugger:  The taskwait is completed. Location remains, but thread is
15303041982dSJonathan Peyton     // negated.
15317cc577a4SJonathan Peyton     taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
15327cc577a4SJonathan Peyton 
15337cc577a4SJonathan Peyton #if OMPT_SUPPORT && OMPT_TRACE
15347cc577a4SJonathan Peyton     if (ompt_enabled) {
15357cc577a4SJonathan Peyton       if (ompt_callbacks.ompt_callback(ompt_event_taskwait_end)) {
15363041982dSJonathan Peyton         ompt_callbacks.ompt_callback(ompt_event_taskwait_end)(my_parallel_id,
15373041982dSJonathan Peyton                                                               my_task_id);
15387cc577a4SJonathan Peyton       }
15397cc577a4SJonathan Peyton       taskdata->ompt_task_info.frame.reenter_runtime_frame = NULL;
15407cc577a4SJonathan Peyton     }
15417cc577a4SJonathan Peyton #endif
15427cc577a4SJonathan Peyton     ANNOTATE_HAPPENS_AFTER(taskdata);
15437cc577a4SJonathan Peyton   }
15447cc577a4SJonathan Peyton 
15457cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, "
15463041982dSJonathan Peyton                 "returning TASK_CURRENT_NOT_QUEUED\n",
15473041982dSJonathan Peyton                 gtid, taskdata));
15487cc577a4SJonathan Peyton 
15497cc577a4SJonathan Peyton   return TASK_CURRENT_NOT_QUEUED;
15507cc577a4SJonathan Peyton }
15517cc577a4SJonathan Peyton 
15527cc577a4SJonathan Peyton // __kmpc_omp_taskyield: switch to a different task
15533041982dSJonathan Peyton kmp_int32 __kmpc_omp_taskyield(ident_t *loc_ref, kmp_int32 gtid, int end_part) {
15547cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata;
15557cc577a4SJonathan Peyton   kmp_info_t *thread;
15567cc577a4SJonathan Peyton   int thread_finished = FALSE;
15577cc577a4SJonathan Peyton 
15587cc577a4SJonathan Peyton   KMP_COUNT_BLOCK(OMP_TASKYIELD);
15597cc577a4SJonathan Peyton   KMP_SET_THREAD_STATE_BLOCK(TASKYIELD);
15607cc577a4SJonathan Peyton 
15617cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n",
15627cc577a4SJonathan Peyton                 gtid, loc_ref, end_part));
15637cc577a4SJonathan Peyton 
15647cc577a4SJonathan Peyton   if (__kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel) {
15657cc577a4SJonathan Peyton     thread = __kmp_threads[gtid];
15667cc577a4SJonathan Peyton     taskdata = thread->th.th_current_task;
15677cc577a4SJonathan Peyton // Should we model this as a task wait or not?
15683041982dSJonathan Peyton // Debugger: The taskwait is active. Store location and thread encountered the
15693041982dSJonathan Peyton // taskwait.
15707cc577a4SJonathan Peyton #if USE_ITT_BUILD
15717cc577a4SJonathan Peyton // Note: These values are used by ITT events as well.
15727cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
15737cc577a4SJonathan Peyton     taskdata->td_taskwait_counter += 1;
15747cc577a4SJonathan Peyton     taskdata->td_taskwait_ident = loc_ref;
15757cc577a4SJonathan Peyton     taskdata->td_taskwait_thread = gtid + 1;
15767cc577a4SJonathan Peyton 
15777cc577a4SJonathan Peyton #if USE_ITT_BUILD
15787cc577a4SJonathan Peyton     void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
15797cc577a4SJonathan Peyton     if (itt_sync_obj != NULL)
15807cc577a4SJonathan Peyton       __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
15817cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
15827cc577a4SJonathan Peyton     if (!taskdata->td_flags.team_serial) {
15837cc577a4SJonathan Peyton       kmp_task_team_t *task_team = thread->th.th_task_team;
15847cc577a4SJonathan Peyton       if (task_team != NULL) {
15857cc577a4SJonathan Peyton         if (KMP_TASKING_ENABLED(task_team)) {
15863041982dSJonathan Peyton           __kmp_execute_tasks_32(
15873041982dSJonathan Peyton               thread, gtid, NULL, FALSE,
15883041982dSJonathan Peyton               &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
15893041982dSJonathan Peyton               __kmp_task_stealing_constraint);
15907cc577a4SJonathan Peyton         }
15917cc577a4SJonathan Peyton       }
15927cc577a4SJonathan Peyton     }
15937cc577a4SJonathan Peyton #if USE_ITT_BUILD
15947cc577a4SJonathan Peyton     if (itt_sync_obj != NULL)
15957cc577a4SJonathan Peyton       __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
15967cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
15977cc577a4SJonathan Peyton 
15983041982dSJonathan Peyton     // Debugger:  The taskwait is completed. Location remains, but thread is
15993041982dSJonathan Peyton     // negated.
16007cc577a4SJonathan Peyton     taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread;
16017cc577a4SJonathan Peyton   }
16027cc577a4SJonathan Peyton 
16037cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_omp_taskyield(exit): T#%d task %p resuming, "
16043041982dSJonathan Peyton                 "returning TASK_CURRENT_NOT_QUEUED\n",
16053041982dSJonathan Peyton                 gtid, taskdata));
16067cc577a4SJonathan Peyton 
16077cc577a4SJonathan Peyton   return TASK_CURRENT_NOT_QUEUED;
16087cc577a4SJonathan Peyton }
16097cc577a4SJonathan Peyton 
161072ba2109SAndrey Churbanov // TODO: change to OMP_50_ENABLED, need to change build tools for this to work
161172ba2109SAndrey Churbanov #if OMP_45_ENABLED
161272ba2109SAndrey Churbanov // Task Reduction implementation
161372ba2109SAndrey Churbanov 
161472ba2109SAndrey Churbanov typedef struct kmp_task_red_flags {
161572ba2109SAndrey Churbanov   unsigned lazy_priv : 1; // hint: (1) use lazy allocation (big objects)
161672ba2109SAndrey Churbanov   unsigned reserved31 : 31;
161772ba2109SAndrey Churbanov } kmp_task_red_flags_t;
161872ba2109SAndrey Churbanov 
161972ba2109SAndrey Churbanov // internal structure for reduction data item related info
162072ba2109SAndrey Churbanov typedef struct kmp_task_red_data {
162172ba2109SAndrey Churbanov   void *reduce_shar; // shared reduction item
162272ba2109SAndrey Churbanov   size_t reduce_size; // size of data item
162372ba2109SAndrey Churbanov   void *reduce_priv; // thread specific data
162472ba2109SAndrey Churbanov   void *reduce_pend; // end of private data for comparison op
162572ba2109SAndrey Churbanov   void *reduce_init; // data initialization routine
162672ba2109SAndrey Churbanov   void *reduce_fini; // data finalization routine
162772ba2109SAndrey Churbanov   void *reduce_comb; // data combiner routine
162872ba2109SAndrey Churbanov   kmp_task_red_flags_t flags; // flags for additional info from compiler
162972ba2109SAndrey Churbanov } kmp_task_red_data_t;
163072ba2109SAndrey Churbanov 
163172ba2109SAndrey Churbanov // structure sent us by compiler - one per reduction item
163272ba2109SAndrey Churbanov typedef struct kmp_task_red_input {
163372ba2109SAndrey Churbanov   void *reduce_shar; // shared reduction item
163472ba2109SAndrey Churbanov   size_t reduce_size; // size of data item
163572ba2109SAndrey Churbanov   void *reduce_init; // data initialization routine
163672ba2109SAndrey Churbanov   void *reduce_fini; // data finalization routine
163772ba2109SAndrey Churbanov   void *reduce_comb; // data combiner routine
163872ba2109SAndrey Churbanov   kmp_task_red_flags_t flags; // flags for additional info from compiler
163972ba2109SAndrey Churbanov } kmp_task_red_input_t;
164072ba2109SAndrey Churbanov 
164172ba2109SAndrey Churbanov /*!
164272ba2109SAndrey Churbanov @ingroup TASKING
164372ba2109SAndrey Churbanov @param gtid      Global thread ID
164472ba2109SAndrey Churbanov @param num       Number of data items to reduce
164572ba2109SAndrey Churbanov @param data      Array of data for reduction
164672ba2109SAndrey Churbanov @return The taskgroup identifier
164772ba2109SAndrey Churbanov 
164872ba2109SAndrey Churbanov Initialize task reduction for the taskgroup.
164972ba2109SAndrey Churbanov */
16503041982dSJonathan Peyton void *__kmpc_task_reduction_init(int gtid, int num, void *data) {
165172ba2109SAndrey Churbanov   kmp_info_t *thread = __kmp_threads[gtid];
165272ba2109SAndrey Churbanov   kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup;
165372ba2109SAndrey Churbanov   kmp_int32 nth = thread->th.th_team_nproc;
165472ba2109SAndrey Churbanov   kmp_task_red_input_t *input = (kmp_task_red_input_t *)data;
165572ba2109SAndrey Churbanov   kmp_task_red_data_t *arr;
165672ba2109SAndrey Churbanov 
165772ba2109SAndrey Churbanov   // check input data just in case
165872ba2109SAndrey Churbanov   KMP_ASSERT(tg != NULL);
165972ba2109SAndrey Churbanov   KMP_ASSERT(data != NULL);
166072ba2109SAndrey Churbanov   KMP_ASSERT(num > 0);
166172ba2109SAndrey Churbanov   if (nth == 1) {
166272ba2109SAndrey Churbanov     KA_TRACE(10, ("__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n",
166372ba2109SAndrey Churbanov                   gtid, tg));
166472ba2109SAndrey Churbanov     return (void *)tg;
166572ba2109SAndrey Churbanov   }
166672ba2109SAndrey Churbanov   KA_TRACE(10, ("__kmpc_task_reduction_init: T#%d, taskgroup %p, #items %d\n",
166772ba2109SAndrey Churbanov                 gtid, tg, num));
16683041982dSJonathan Peyton   arr = (kmp_task_red_data_t *)__kmp_thread_malloc(
16693041982dSJonathan Peyton       thread, num * sizeof(kmp_task_red_data_t));
167072ba2109SAndrey Churbanov   for (int i = 0; i < num; ++i) {
167172ba2109SAndrey Churbanov     void (*f_init)(void *) = (void (*)(void *))(input[i].reduce_init);
167272ba2109SAndrey Churbanov     size_t size = input[i].reduce_size - 1;
167372ba2109SAndrey Churbanov     // round the size up to cache line per thread-specific item
167472ba2109SAndrey Churbanov     size += CACHE_LINE - size % CACHE_LINE;
167572ba2109SAndrey Churbanov     KMP_ASSERT(input[i].reduce_comb != NULL); // combiner is mandatory
167672ba2109SAndrey Churbanov     arr[i].reduce_shar = input[i].reduce_shar;
167772ba2109SAndrey Churbanov     arr[i].reduce_size = size;
167872ba2109SAndrey Churbanov     arr[i].reduce_init = input[i].reduce_init;
167972ba2109SAndrey Churbanov     arr[i].reduce_fini = input[i].reduce_fini;
168072ba2109SAndrey Churbanov     arr[i].reduce_comb = input[i].reduce_comb;
168172ba2109SAndrey Churbanov     arr[i].flags = input[i].flags;
168272ba2109SAndrey Churbanov     if (!input[i].flags.lazy_priv) {
168372ba2109SAndrey Churbanov       // allocate cache-line aligned block and fill it with zeros
168472ba2109SAndrey Churbanov       arr[i].reduce_priv = __kmp_allocate(nth * size);
168572ba2109SAndrey Churbanov       arr[i].reduce_pend = (char *)(arr[i].reduce_priv) + nth * size;
168672ba2109SAndrey Churbanov       if (f_init != NULL) {
168772ba2109SAndrey Churbanov         // initialize thread-specific items
168872ba2109SAndrey Churbanov         for (int j = 0; j < nth; ++j) {
168972ba2109SAndrey Churbanov           f_init((char *)(arr[i].reduce_priv) + j * size);
169072ba2109SAndrey Churbanov         }
169172ba2109SAndrey Churbanov       }
169272ba2109SAndrey Churbanov     } else {
169372ba2109SAndrey Churbanov       // only allocate space for pointers now,
169472ba2109SAndrey Churbanov       // objects will be lazily allocated/initialized once requested
169572ba2109SAndrey Churbanov       arr[i].reduce_priv = __kmp_allocate(nth * sizeof(void *));
169672ba2109SAndrey Churbanov     }
169772ba2109SAndrey Churbanov   }
169872ba2109SAndrey Churbanov   tg->reduce_data = (void *)arr;
169972ba2109SAndrey Churbanov   tg->reduce_num_data = num;
170072ba2109SAndrey Churbanov   return (void *)tg;
170172ba2109SAndrey Churbanov }
170272ba2109SAndrey Churbanov 
170372ba2109SAndrey Churbanov /*!
170472ba2109SAndrey Churbanov @ingroup TASKING
170572ba2109SAndrey Churbanov @param gtid    Global thread ID
170672ba2109SAndrey Churbanov @param tskgrp  The taskgroup ID (optional)
170772ba2109SAndrey Churbanov @param data    Shared location of the item
170872ba2109SAndrey Churbanov @return The pointer to per-thread data
170972ba2109SAndrey Churbanov 
171072ba2109SAndrey Churbanov Get thread-specific location of data item
171172ba2109SAndrey Churbanov */
17123041982dSJonathan Peyton void *__kmpc_task_reduction_get_th_data(int gtid, void *tskgrp, void *data) {
171372ba2109SAndrey Churbanov   kmp_info_t *thread = __kmp_threads[gtid];
171472ba2109SAndrey Churbanov   kmp_int32 nth = thread->th.th_team_nproc;
171572ba2109SAndrey Churbanov   if (nth == 1)
171672ba2109SAndrey Churbanov     return data; // nothing to do
171772ba2109SAndrey Churbanov 
171872ba2109SAndrey Churbanov   kmp_taskgroup_t *tg = (kmp_taskgroup_t *)tskgrp;
171972ba2109SAndrey Churbanov   if (tg == NULL)
172072ba2109SAndrey Churbanov     tg = thread->th.th_current_task->td_taskgroup;
172172ba2109SAndrey Churbanov   KMP_ASSERT(tg != NULL);
172272ba2109SAndrey Churbanov   kmp_task_red_data_t *arr = (kmp_task_red_data_t *)(tg->reduce_data);
172372ba2109SAndrey Churbanov   kmp_int32 num = tg->reduce_num_data;
172472ba2109SAndrey Churbanov   kmp_int32 tid = thread->th.th_info.ds.ds_tid;
172572ba2109SAndrey Churbanov 
172672ba2109SAndrey Churbanov   KMP_ASSERT(data != NULL);
172772ba2109SAndrey Churbanov   while (tg != NULL) {
172872ba2109SAndrey Churbanov     for (int i = 0; i < num; ++i) {
172972ba2109SAndrey Churbanov       if (!arr[i].flags.lazy_priv) {
173072ba2109SAndrey Churbanov         if (data == arr[i].reduce_shar ||
173172ba2109SAndrey Churbanov             (data >= arr[i].reduce_priv && data < arr[i].reduce_pend))
173272ba2109SAndrey Churbanov           return (char *)(arr[i].reduce_priv) + tid * arr[i].reduce_size;
173372ba2109SAndrey Churbanov       } else {
173472ba2109SAndrey Churbanov         // check shared location first
173572ba2109SAndrey Churbanov         void **p_priv = (void **)(arr[i].reduce_priv);
173672ba2109SAndrey Churbanov         if (data == arr[i].reduce_shar)
173772ba2109SAndrey Churbanov           goto found;
173872ba2109SAndrey Churbanov         // check if we get some thread specific location as parameter
173972ba2109SAndrey Churbanov         for (int j = 0; j < nth; ++j)
174072ba2109SAndrey Churbanov           if (data == p_priv[j])
174172ba2109SAndrey Churbanov             goto found;
174272ba2109SAndrey Churbanov         continue; // not found, continue search
174372ba2109SAndrey Churbanov       found:
174472ba2109SAndrey Churbanov         if (p_priv[tid] == NULL) {
174572ba2109SAndrey Churbanov           // allocate thread specific object lazily
174672ba2109SAndrey Churbanov           void (*f_init)(void *) = (void (*)(void *))(arr[i].reduce_init);
174772ba2109SAndrey Churbanov           p_priv[tid] = __kmp_allocate(arr[i].reduce_size);
174872ba2109SAndrey Churbanov           if (f_init != NULL) {
174972ba2109SAndrey Churbanov             f_init(p_priv[tid]);
175072ba2109SAndrey Churbanov           }
175172ba2109SAndrey Churbanov         }
175272ba2109SAndrey Churbanov         return p_priv[tid];
175372ba2109SAndrey Churbanov       }
175472ba2109SAndrey Churbanov     }
175572ba2109SAndrey Churbanov     tg = tg->parent;
175672ba2109SAndrey Churbanov     arr = (kmp_task_red_data_t *)(tg->reduce_data);
175772ba2109SAndrey Churbanov     num = tg->reduce_num_data;
175872ba2109SAndrey Churbanov   }
175972ba2109SAndrey Churbanov   KMP_ASSERT2(0, "Unknown task reduction item");
176072ba2109SAndrey Churbanov   return NULL; // ERROR, this line never executed
176172ba2109SAndrey Churbanov }
176272ba2109SAndrey Churbanov 
176372ba2109SAndrey Churbanov // Finalize task reduction.
176472ba2109SAndrey Churbanov // Called from __kmpc_end_taskgroup()
17653041982dSJonathan Peyton static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg) {
176672ba2109SAndrey Churbanov   kmp_int32 nth = th->th.th_team_nproc;
176772ba2109SAndrey Churbanov   KMP_DEBUG_ASSERT(nth > 1); // should not be called if nth == 1
176872ba2109SAndrey Churbanov   kmp_task_red_data_t *arr = (kmp_task_red_data_t *)tg->reduce_data;
176972ba2109SAndrey Churbanov   kmp_int32 num = tg->reduce_num_data;
177072ba2109SAndrey Churbanov   for (int i = 0; i < num; ++i) {
177172ba2109SAndrey Churbanov     void *sh_data = arr[i].reduce_shar;
177272ba2109SAndrey Churbanov     void (*f_fini)(void *) = (void (*)(void *))(arr[i].reduce_fini);
17733041982dSJonathan Peyton     void (*f_comb)(void *, void *) =
17743041982dSJonathan Peyton         (void (*)(void *, void *))(arr[i].reduce_comb);
177572ba2109SAndrey Churbanov     if (!arr[i].flags.lazy_priv) {
177672ba2109SAndrey Churbanov       void *pr_data = arr[i].reduce_priv;
177772ba2109SAndrey Churbanov       size_t size = arr[i].reduce_size;
177872ba2109SAndrey Churbanov       for (int j = 0; j < nth; ++j) {
177972ba2109SAndrey Churbanov         void *priv_data = (char *)pr_data + j * size;
178072ba2109SAndrey Churbanov         f_comb(sh_data, priv_data); // combine results
178172ba2109SAndrey Churbanov         if (f_fini)
178272ba2109SAndrey Churbanov           f_fini(priv_data); // finalize if needed
178372ba2109SAndrey Churbanov       }
178472ba2109SAndrey Churbanov     } else {
178572ba2109SAndrey Churbanov       void **pr_data = (void **)(arr[i].reduce_priv);
178672ba2109SAndrey Churbanov       for (int j = 0; j < nth; ++j) {
178772ba2109SAndrey Churbanov         if (pr_data[j] != NULL) {
178872ba2109SAndrey Churbanov           f_comb(sh_data, pr_data[j]); // combine results
178972ba2109SAndrey Churbanov           if (f_fini)
179072ba2109SAndrey Churbanov             f_fini(pr_data[j]); // finalize if needed
179172ba2109SAndrey Churbanov           __kmp_free(pr_data[j]);
179272ba2109SAndrey Churbanov         }
179372ba2109SAndrey Churbanov       }
179472ba2109SAndrey Churbanov     }
179572ba2109SAndrey Churbanov     __kmp_free(arr[i].reduce_priv);
179672ba2109SAndrey Churbanov   }
179772ba2109SAndrey Churbanov   __kmp_thread_free(th, arr);
179872ba2109SAndrey Churbanov   tg->reduce_data = NULL;
179972ba2109SAndrey Churbanov   tg->reduce_num_data = 0;
180072ba2109SAndrey Churbanov }
180172ba2109SAndrey Churbanov #endif
18027cc577a4SJonathan Peyton 
18037cc577a4SJonathan Peyton #if OMP_40_ENABLED
18047cc577a4SJonathan Peyton // __kmpc_taskgroup: Start a new taskgroup
18053041982dSJonathan Peyton void __kmpc_taskgroup(ident_t *loc, int gtid) {
18067cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
18077cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = thread->th.th_current_task;
18087cc577a4SJonathan Peyton   kmp_taskgroup_t *tg_new =
18097cc577a4SJonathan Peyton       (kmp_taskgroup_t *)__kmp_thread_malloc(thread, sizeof(kmp_taskgroup_t));
18107cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new));
18117cc577a4SJonathan Peyton   tg_new->count = 0;
18127cc577a4SJonathan Peyton   tg_new->cancel_request = cancel_noreq;
18137cc577a4SJonathan Peyton   tg_new->parent = taskdata->td_taskgroup;
181472ba2109SAndrey Churbanov // TODO: change to OMP_50_ENABLED, need to change build tools for this to work
181572ba2109SAndrey Churbanov #if OMP_45_ENABLED
181672ba2109SAndrey Churbanov   tg_new->reduce_data = NULL;
181772ba2109SAndrey Churbanov   tg_new->reduce_num_data = 0;
181872ba2109SAndrey Churbanov #endif
18197cc577a4SJonathan Peyton   taskdata->td_taskgroup = tg_new;
18207cc577a4SJonathan Peyton }
18217cc577a4SJonathan Peyton 
18227cc577a4SJonathan Peyton // __kmpc_end_taskgroup: Wait until all tasks generated by the current task
18237cc577a4SJonathan Peyton //                       and its descendants are complete
18243041982dSJonathan Peyton void __kmpc_end_taskgroup(ident_t *loc, int gtid) {
18257cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
18267cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = thread->th.th_current_task;
18277cc577a4SJonathan Peyton   kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup;
18287cc577a4SJonathan Peyton   int thread_finished = FALSE;
18297cc577a4SJonathan Peyton 
18307cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc));
18317cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskgroup != NULL);
18327cc577a4SJonathan Peyton   KMP_SET_THREAD_STATE_BLOCK(TASKGROUP);
18337cc577a4SJonathan Peyton 
18347cc577a4SJonathan Peyton   if (__kmp_tasking_mode != tskm_immediate_exec) {
18357cc577a4SJonathan Peyton #if USE_ITT_BUILD
18363041982dSJonathan Peyton     // For ITT the taskgroup wait is similar to taskwait until we need to
18373041982dSJonathan Peyton     // distinguish them
18387cc577a4SJonathan Peyton     void *itt_sync_obj = __kmp_itt_taskwait_object(gtid);
18397cc577a4SJonathan Peyton     if (itt_sync_obj != NULL)
18407cc577a4SJonathan Peyton       __kmp_itt_taskwait_starting(gtid, itt_sync_obj);
18417cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
18427cc577a4SJonathan Peyton 
18437cc577a4SJonathan Peyton #if OMP_45_ENABLED
18443041982dSJonathan Peyton     if (!taskdata->td_flags.team_serial ||
18453041982dSJonathan Peyton         (thread->th.th_task_team != NULL &&
18463041982dSJonathan Peyton          thread->th.th_task_team->tt.tt_found_proxy_tasks))
18477cc577a4SJonathan Peyton #else
18487cc577a4SJonathan Peyton     if (!taskdata->td_flags.team_serial)
18497cc577a4SJonathan Peyton #endif
18507cc577a4SJonathan Peyton     {
1851*c47afcd9SAndrey Churbanov       kmp_flag_32 flag(RCAST(kmp_uint32 *, &taskgroup->count), 0U);
18527cc577a4SJonathan Peyton       while (TCR_4(taskgroup->count) != 0) {
18533041982dSJonathan Peyton         flag.execute_tasks(thread, gtid, FALSE,
18543041982dSJonathan Peyton                            &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj),
18553041982dSJonathan Peyton                            __kmp_task_stealing_constraint);
18567cc577a4SJonathan Peyton       }
18577cc577a4SJonathan Peyton     }
18587cc577a4SJonathan Peyton 
18597cc577a4SJonathan Peyton #if USE_ITT_BUILD
18607cc577a4SJonathan Peyton     if (itt_sync_obj != NULL)
18617cc577a4SJonathan Peyton       __kmp_itt_taskwait_finished(gtid, itt_sync_obj);
18627cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
18637cc577a4SJonathan Peyton   }
18647cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskgroup->count == 0);
18657cc577a4SJonathan Peyton 
186672ba2109SAndrey Churbanov // TODO: change to OMP_50_ENABLED, need to change build tools for this to work
186772ba2109SAndrey Churbanov #if OMP_45_ENABLED
186872ba2109SAndrey Churbanov   if (taskgroup->reduce_data != NULL) // need to reduce?
186972ba2109SAndrey Churbanov     __kmp_task_reduction_fini(thread, taskgroup);
187072ba2109SAndrey Churbanov #endif
18717cc577a4SJonathan Peyton   // Restore parent taskgroup for the current task
18727cc577a4SJonathan Peyton   taskdata->td_taskgroup = taskgroup->parent;
18737cc577a4SJonathan Peyton   __kmp_thread_free(thread, taskgroup);
18747cc577a4SJonathan Peyton 
18753041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n",
18763041982dSJonathan Peyton                 gtid, taskdata));
18777cc577a4SJonathan Peyton   ANNOTATE_HAPPENS_AFTER(taskdata);
18787cc577a4SJonathan Peyton }
18797cc577a4SJonathan Peyton #endif
18807cc577a4SJonathan Peyton 
18817cc577a4SJonathan Peyton // __kmp_remove_my_task: remove a task from my own deque
18823041982dSJonathan Peyton static kmp_task_t *__kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid,
18833041982dSJonathan Peyton                                         kmp_task_team_t *task_team,
18843041982dSJonathan Peyton                                         kmp_int32 is_constrained) {
18857cc577a4SJonathan Peyton   kmp_task_t *task;
18867cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata;
18877cc577a4SJonathan Peyton   kmp_thread_data_t *thread_data;
18887cc577a4SJonathan Peyton   kmp_uint32 tail;
18897cc577a4SJonathan Peyton 
18907cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
18913041982dSJonathan Peyton   KMP_DEBUG_ASSERT(task_team->tt.tt_threads_data !=
18923041982dSJonathan Peyton                    NULL); // Caller should check this condition
18937cc577a4SJonathan Peyton 
18947cc577a4SJonathan Peyton   thread_data = &task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)];
18957cc577a4SJonathan Peyton 
18967cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmp_remove_my_task(enter): T#%d ntasks=%d head=%u tail=%u\n",
18973041982dSJonathan Peyton                 gtid, thread_data->td.td_deque_ntasks,
18983041982dSJonathan Peyton                 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
18997cc577a4SJonathan Peyton 
19007cc577a4SJonathan Peyton   if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
19013041982dSJonathan Peyton     KA_TRACE(10,
19023041982dSJonathan Peyton              ("__kmp_remove_my_task(exit #1): T#%d No tasks to remove: "
19033041982dSJonathan Peyton               "ntasks=%d head=%u tail=%u\n",
19043041982dSJonathan Peyton               gtid, thread_data->td.td_deque_ntasks,
19053041982dSJonathan Peyton               thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
19067cc577a4SJonathan Peyton     return NULL;
19077cc577a4SJonathan Peyton   }
19087cc577a4SJonathan Peyton 
19097cc577a4SJonathan Peyton   __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
19107cc577a4SJonathan Peyton 
19117cc577a4SJonathan Peyton   if (TCR_4(thread_data->td.td_deque_ntasks) == 0) {
19127cc577a4SJonathan Peyton     __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
19133041982dSJonathan Peyton     KA_TRACE(10,
19143041982dSJonathan Peyton              ("__kmp_remove_my_task(exit #2): T#%d No tasks to remove: "
19153041982dSJonathan Peyton               "ntasks=%d head=%u tail=%u\n",
19163041982dSJonathan Peyton               gtid, thread_data->td.td_deque_ntasks,
19173041982dSJonathan Peyton               thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
19187cc577a4SJonathan Peyton     return NULL;
19197cc577a4SJonathan Peyton   }
19207cc577a4SJonathan Peyton 
19213041982dSJonathan Peyton   tail = (thread_data->td.td_deque_tail - 1) &
19223041982dSJonathan Peyton          TASK_DEQUE_MASK(thread_data->td); // Wrap index.
19237cc577a4SJonathan Peyton   taskdata = thread_data->td.td_deque[tail];
19247cc577a4SJonathan Peyton 
19257cc577a4SJonathan Peyton   if (is_constrained && (taskdata->td_flags.tiedness == TASK_TIED)) {
19267cc577a4SJonathan Peyton     // we need to check if the candidate obeys task scheduling constraint:
19277cc577a4SJonathan Peyton     // only child of current task can be scheduled
19287cc577a4SJonathan Peyton     kmp_taskdata_t *current = thread->th.th_current_task;
19297cc577a4SJonathan Peyton     kmp_int32 level = current->td_level;
19307cc577a4SJonathan Peyton     kmp_taskdata_t *parent = taskdata->td_parent;
19317cc577a4SJonathan Peyton     while (parent != current && parent->td_level > level) {
19323041982dSJonathan Peyton       parent = parent->td_parent; // check generation up to the level of the
19333041982dSJonathan Peyton       // current task
19347cc577a4SJonathan Peyton       KMP_DEBUG_ASSERT(parent != NULL);
19357cc577a4SJonathan Peyton     }
19367cc577a4SJonathan Peyton     if (parent != current) {
19373041982dSJonathan Peyton       // If the tail task is not a child, then no other child can appear in the
19383041982dSJonathan Peyton       // deque.
19397cc577a4SJonathan Peyton       __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
19403041982dSJonathan Peyton       KA_TRACE(10,
19413041982dSJonathan Peyton                ("__kmp_remove_my_task(exit #2): T#%d No tasks to remove: "
19423041982dSJonathan Peyton                 "ntasks=%d head=%u tail=%u\n",
19433041982dSJonathan Peyton                 gtid, thread_data->td.td_deque_ntasks,
19443041982dSJonathan Peyton                 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
19457cc577a4SJonathan Peyton       return NULL;
19467cc577a4SJonathan Peyton     }
19477cc577a4SJonathan Peyton   }
19487cc577a4SJonathan Peyton 
19497cc577a4SJonathan Peyton   thread_data->td.td_deque_tail = tail;
19507cc577a4SJonathan Peyton   TCW_4(thread_data->td.td_deque_ntasks, thread_data->td.td_deque_ntasks - 1);
19517cc577a4SJonathan Peyton 
19527cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
19537cc577a4SJonathan Peyton 
19543041982dSJonathan Peyton   KA_TRACE(10, ("__kmp_remove_my_task(exit #2): T#%d task %p removed: "
19553041982dSJonathan Peyton                 "ntasks=%d head=%u tail=%u\n",
19563041982dSJonathan Peyton                 gtid, taskdata, thread_data->td.td_deque_ntasks,
19573041982dSJonathan Peyton                 thread_data->td.td_deque_head, thread_data->td.td_deque_tail));
19587cc577a4SJonathan Peyton 
19597cc577a4SJonathan Peyton   task = KMP_TASKDATA_TO_TASK(taskdata);
19607cc577a4SJonathan Peyton   return task;
19617cc577a4SJonathan Peyton }
19627cc577a4SJonathan Peyton 
19637cc577a4SJonathan Peyton // __kmp_steal_task: remove a task from another thread's deque
19647cc577a4SJonathan Peyton // Assume that calling thread has already checked existence of
19657cc577a4SJonathan Peyton // task_team thread_data before calling this routine.
1966*c47afcd9SAndrey Churbanov static kmp_task_t *__kmp_steal_task(kmp_info_t *victim, kmp_int32 gtid,
1967*c47afcd9SAndrey Churbanov                                     kmp_task_team_t *task_team,
1968*c47afcd9SAndrey Churbanov                                     volatile kmp_int32 *unfinished_threads,
1969*c47afcd9SAndrey Churbanov                                     int *thread_finished,
1970*c47afcd9SAndrey Churbanov                                     kmp_int32 is_constrained) {
19717cc577a4SJonathan Peyton   kmp_task_t *task;
19727cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata;
19737cc577a4SJonathan Peyton   kmp_thread_data_t *victim_td, *threads_data;
19747cc577a4SJonathan Peyton   kmp_int32 victim_tid;
19757cc577a4SJonathan Peyton 
19767cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
19777cc577a4SJonathan Peyton 
19787cc577a4SJonathan Peyton   threads_data = task_team->tt.tt_threads_data;
19797cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(threads_data != NULL); // Caller should check this condition
19807cc577a4SJonathan Peyton 
19817cc577a4SJonathan Peyton   victim_tid = victim->th.th_info.ds.ds_tid;
19827cc577a4SJonathan Peyton   victim_td = &threads_data[victim_tid];
19837cc577a4SJonathan Peyton 
19843041982dSJonathan Peyton   KA_TRACE(10, ("__kmp_steal_task(enter): T#%d try to steal from T#%d: "
19853041982dSJonathan Peyton                 "task_team=%p ntasks=%d "
19867cc577a4SJonathan Peyton                 "head=%u tail=%u\n",
19873041982dSJonathan Peyton                 gtid, __kmp_gtid_from_thread(victim), task_team,
19883041982dSJonathan Peyton                 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
19893041982dSJonathan Peyton                 victim_td->td.td_deque_tail));
19907cc577a4SJonathan Peyton 
19913041982dSJonathan Peyton   if ((TCR_4(victim_td->td.td_deque_ntasks) ==
19923041982dSJonathan Peyton        0) || // Caller should not check this condition
19933041982dSJonathan Peyton       (TCR_PTR(victim->th.th_task_team) !=
19943041982dSJonathan Peyton        task_team)) // GEH: why would this happen?
19957cc577a4SJonathan Peyton   {
19963041982dSJonathan Peyton     KA_TRACE(10, ("__kmp_steal_task(exit #1): T#%d could not steal from T#%d: "
19973041982dSJonathan Peyton                   "task_team=%p "
19987cc577a4SJonathan Peyton                   "ntasks=%d head=%u tail=%u\n",
19993041982dSJonathan Peyton                   gtid, __kmp_gtid_from_thread(victim), task_team,
20003041982dSJonathan Peyton                   victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
20013041982dSJonathan Peyton                   victim_td->td.td_deque_tail));
20027cc577a4SJonathan Peyton     return NULL;
20037cc577a4SJonathan Peyton   }
20047cc577a4SJonathan Peyton 
20057cc577a4SJonathan Peyton   __kmp_acquire_bootstrap_lock(&victim_td->td.td_deque_lock);
20067cc577a4SJonathan Peyton 
20077cc577a4SJonathan Peyton   // Check again after we acquire the lock
20087cc577a4SJonathan Peyton   if ((TCR_4(victim_td->td.td_deque_ntasks) == 0) ||
20093041982dSJonathan Peyton       (TCR_PTR(victim->th.th_task_team) !=
20103041982dSJonathan Peyton        task_team)) // GEH: why would this happen?
20117cc577a4SJonathan Peyton   {
20127cc577a4SJonathan Peyton     __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
20133041982dSJonathan Peyton     KA_TRACE(10, ("__kmp_steal_task(exit #2): T#%d could not steal from T#%d: "
20143041982dSJonathan Peyton                   "task_team=%p "
20157cc577a4SJonathan Peyton                   "ntasks=%d head=%u tail=%u\n",
20163041982dSJonathan Peyton                   gtid, __kmp_gtid_from_thread(victim), task_team,
20173041982dSJonathan Peyton                   victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
20183041982dSJonathan Peyton                   victim_td->td.td_deque_tail));
20197cc577a4SJonathan Peyton     return NULL;
20207cc577a4SJonathan Peyton   }
20217cc577a4SJonathan Peyton 
20227cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(victim_td->td.td_deque != NULL);
20237cc577a4SJonathan Peyton 
20247cc577a4SJonathan Peyton   taskdata = victim_td->td.td_deque[victim_td->td.td_deque_head];
20257cc577a4SJonathan Peyton   if (is_constrained) {
20267cc577a4SJonathan Peyton     // we need to check if the candidate obeys task scheduling constraint:
20277cc577a4SJonathan Peyton     // only descendant of current task can be scheduled
20287cc577a4SJonathan Peyton     kmp_taskdata_t *current = __kmp_threads[gtid]->th.th_current_task;
20297cc577a4SJonathan Peyton     kmp_int32 level = current->td_level;
20307cc577a4SJonathan Peyton     kmp_taskdata_t *parent = taskdata->td_parent;
20317cc577a4SJonathan Peyton     while (parent != current && parent->td_level > level) {
20323041982dSJonathan Peyton       parent = parent->td_parent; // check generation up to the level of the
20333041982dSJonathan Peyton       // current task
20347cc577a4SJonathan Peyton       KMP_DEBUG_ASSERT(parent != NULL);
20357cc577a4SJonathan Peyton     }
20367cc577a4SJonathan Peyton     if (parent != current) {
20377cc577a4SJonathan Peyton       // If the head task is not a descendant of the current task then do not
20387cc577a4SJonathan Peyton       // steal it. No other task in victim's deque can be a descendant of the
20397cc577a4SJonathan Peyton       // current task.
20407cc577a4SJonathan Peyton       __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
20413041982dSJonathan Peyton       KA_TRACE(10, ("__kmp_steal_task(exit #2): T#%d could not steal from "
20423041982dSJonathan Peyton                     "T#%d: task_team=%p "
20437cc577a4SJonathan Peyton                     "ntasks=%d head=%u tail=%u\n",
20443041982dSJonathan Peyton                     gtid,
20453041982dSJonathan Peyton                     __kmp_gtid_from_thread(threads_data[victim_tid].td.td_thr),
20467cc577a4SJonathan Peyton                     task_team, victim_td->td.td_deque_ntasks,
20477cc577a4SJonathan Peyton                     victim_td->td.td_deque_head, victim_td->td.td_deque_tail));
20487cc577a4SJonathan Peyton       return NULL;
20497cc577a4SJonathan Peyton     }
20507cc577a4SJonathan Peyton   }
20517cc577a4SJonathan Peyton   // Bump head pointer and Wrap.
20523041982dSJonathan Peyton   victim_td->td.td_deque_head =
20533041982dSJonathan Peyton       (victim_td->td.td_deque_head + 1) & TASK_DEQUE_MASK(victim_td->td);
20547cc577a4SJonathan Peyton   if (*thread_finished) {
20553041982dSJonathan Peyton     // We need to un-mark this victim as a finished victim.  This must be done
20563041982dSJonathan Peyton     // before releasing the lock, or else other threads (starting with the
20573041982dSJonathan Peyton     // master victim) might be prematurely released from the barrier!!!
2058*c47afcd9SAndrey Churbanov     kmp_int32 count;
20597cc577a4SJonathan Peyton 
2060*c47afcd9SAndrey Churbanov     count = KMP_TEST_THEN_INC32(CCAST(kmp_int32 *, unfinished_threads));
20617cc577a4SJonathan Peyton 
20623041982dSJonathan Peyton     KA_TRACE(
20633041982dSJonathan Peyton         20,
20643041982dSJonathan Peyton         ("__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n",
20657cc577a4SJonathan Peyton          gtid, count + 1, task_team));
20667cc577a4SJonathan Peyton 
20677cc577a4SJonathan Peyton     *thread_finished = FALSE;
20687cc577a4SJonathan Peyton   }
20693041982dSJonathan Peyton   TCW_4(victim_td->td.td_deque_ntasks,
20703041982dSJonathan Peyton         TCR_4(victim_td->td.td_deque_ntasks) - 1);
20713041982dSJonathan Peyton 
20727cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock);
20737cc577a4SJonathan Peyton 
20747cc577a4SJonathan Peyton   KMP_COUNT_BLOCK(TASK_stolen);
20753041982dSJonathan Peyton   KA_TRACE(
20763041982dSJonathan Peyton       10,
20773041982dSJonathan Peyton       ("__kmp_steal_task(exit #3): T#%d stole task %p from T#%d: task_team=%p "
20787cc577a4SJonathan Peyton        "ntasks=%d head=%u tail=%u\n",
20797cc577a4SJonathan Peyton        gtid, taskdata, __kmp_gtid_from_thread(victim), task_team,
20807cc577a4SJonathan Peyton        victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head,
20817cc577a4SJonathan Peyton        victim_td->td.td_deque_tail));
20827cc577a4SJonathan Peyton 
20837cc577a4SJonathan Peyton   task = KMP_TASKDATA_TO_TASK(taskdata);
20847cc577a4SJonathan Peyton   return task;
20857cc577a4SJonathan Peyton }
20867cc577a4SJonathan Peyton 
20873041982dSJonathan Peyton // __kmp_execute_tasks_template: Choose and execute tasks until either the
20883041982dSJonathan Peyton // condition is statisfied (return true) or there are none left (return false).
20893041982dSJonathan Peyton //
20907cc577a4SJonathan Peyton // final_spin is TRUE if this is the spin at the release barrier.
20917cc577a4SJonathan Peyton // thread_finished indicates whether the thread is finished executing all
20927cc577a4SJonathan Peyton // the tasks it has on its deque, and is at the release barrier.
20937cc577a4SJonathan Peyton // spinner is the location on which to spin.
20947cc577a4SJonathan Peyton // spinner == NULL means only execute a single task and return.
20957cc577a4SJonathan Peyton // checker is the value to check to terminate the spin.
20967cc577a4SJonathan Peyton template <class C>
20973041982dSJonathan Peyton static inline int __kmp_execute_tasks_template(
20983041982dSJonathan Peyton     kmp_info_t *thread, kmp_int32 gtid, C *flag, int final_spin,
20993041982dSJonathan Peyton     int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj),
21003041982dSJonathan Peyton     kmp_int32 is_constrained) {
21017cc577a4SJonathan Peyton   kmp_task_team_t *task_team = thread->th.th_task_team;
21027cc577a4SJonathan Peyton   kmp_thread_data_t *threads_data;
21037cc577a4SJonathan Peyton   kmp_task_t *task;
21047cc577a4SJonathan Peyton   kmp_info_t *other_thread;
21057cc577a4SJonathan Peyton   kmp_taskdata_t *current_task = thread->th.th_current_task;
2106*c47afcd9SAndrey Churbanov   volatile kmp_int32 *unfinished_threads;
21073041982dSJonathan Peyton   kmp_int32 nthreads, victim = -2, use_own_tasks = 1, new_victim = 0,
21083041982dSJonathan Peyton                       tid = thread->th.th_info.ds.ds_tid;
21097cc577a4SJonathan Peyton 
21107cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
21117cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(thread == __kmp_threads[gtid]);
21127cc577a4SJonathan Peyton 
21133041982dSJonathan Peyton   if (task_team == NULL)
21143041982dSJonathan Peyton     return FALSE;
21157cc577a4SJonathan Peyton 
21163041982dSJonathan Peyton   KA_TRACE(15, ("__kmp_execute_tasks_template(enter): T#%d final_spin=%d "
21173041982dSJonathan Peyton                 "*thread_finished=%d\n",
21187cc577a4SJonathan Peyton                 gtid, final_spin, *thread_finished));
21197cc577a4SJonathan Peyton 
2120581490e7SAndrey Churbanov   thread->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
21217cc577a4SJonathan Peyton   threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
21227cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(threads_data != NULL);
21237cc577a4SJonathan Peyton 
21247cc577a4SJonathan Peyton   nthreads = task_team->tt.tt_nproc;
21257cc577a4SJonathan Peyton   unfinished_threads = &(task_team->tt.tt_unfinished_threads);
21267cc577a4SJonathan Peyton #if OMP_45_ENABLED
21277cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(nthreads > 1 || task_team->tt.tt_found_proxy_tasks);
21287cc577a4SJonathan Peyton #else
21297cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(nthreads > 1);
21307cc577a4SJonathan Peyton #endif
2131*c47afcd9SAndrey Churbanov   KMP_DEBUG_ASSERT(TCR_4(*unfinished_threads) >= 0);
21327cc577a4SJonathan Peyton 
21333041982dSJonathan Peyton   while (1) { // Outer loop keeps trying to find tasks in case of single thread
21343041982dSJonathan Peyton     // getting tasks from target constructs
21357cc577a4SJonathan Peyton     while (1) { // Inner loop to find a task and execute it
21367cc577a4SJonathan Peyton       task = NULL;
21377cc577a4SJonathan Peyton       if (use_own_tasks) { // check on own queue first
21387cc577a4SJonathan Peyton         task = __kmp_remove_my_task(thread, gtid, task_team, is_constrained);
21397cc577a4SJonathan Peyton       }
21407cc577a4SJonathan Peyton       if ((task == NULL) && (nthreads > 1)) { // Steal a task
21417cc577a4SJonathan Peyton         int asleep = 1;
21427cc577a4SJonathan Peyton         use_own_tasks = 0;
21437cc577a4SJonathan Peyton         // Try to steal from the last place I stole from successfully.
21447cc577a4SJonathan Peyton         if (victim == -2) { // haven't stolen anything yet
21457cc577a4SJonathan Peyton           victim = threads_data[tid].td.td_deque_last_stolen;
21463041982dSJonathan Peyton           if (victim !=
21473041982dSJonathan Peyton               -1) // if we have a last stolen from victim, get the thread
21487cc577a4SJonathan Peyton             other_thread = threads_data[victim].td.td_thr;
21497cc577a4SJonathan Peyton         }
21507cc577a4SJonathan Peyton         if (victim != -1) { // found last victim
21517cc577a4SJonathan Peyton           asleep = 0;
21523041982dSJonathan Peyton         } else if (!new_victim) { // no recent steals and we haven't already
21533041982dSJonathan Peyton           // used a new victim; select a random thread
21547cc577a4SJonathan Peyton           do { // Find a different thread to steal work from.
21553041982dSJonathan Peyton             // Pick a random thread. Initial plan was to cycle through all the
21563041982dSJonathan Peyton             // threads, and only return if we tried to steal from every thread,
21573041982dSJonathan Peyton             // and failed.  Arch says that's not such a great idea.
21587cc577a4SJonathan Peyton             victim = __kmp_get_random(thread) % (nthreads - 1);
21597cc577a4SJonathan Peyton             if (victim >= tid) {
21607cc577a4SJonathan Peyton               ++victim; // Adjusts random distribution to exclude self
21617cc577a4SJonathan Peyton             }
21627cc577a4SJonathan Peyton             // Found a potential victim
21637cc577a4SJonathan Peyton             other_thread = threads_data[victim].td.td_thr;
21643041982dSJonathan Peyton             // There is a slight chance that __kmp_enable_tasking() did not wake
21653041982dSJonathan Peyton             // up all threads waiting at the barrier.  If victim is sleeping,
21663041982dSJonathan Peyton             // then wake it up. Since we were going to pay the cache miss
21673041982dSJonathan Peyton             // penalty for referencing another thread's kmp_info_t struct
21683041982dSJonathan Peyton             // anyway,
21693041982dSJonathan Peyton             // the check shouldn't cost too much performance at this point. In
21703041982dSJonathan Peyton             // extra barrier mode, tasks do not sleep at the separate tasking
21713041982dSJonathan Peyton             // barrier, so this isn't a problem.
21727cc577a4SJonathan Peyton             asleep = 0;
21737cc577a4SJonathan Peyton             if ((__kmp_tasking_mode == tskm_task_teams) &&
21747cc577a4SJonathan Peyton                 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) &&
2175*c47afcd9SAndrey Churbanov                 (TCR_PTR(CCAST(void *, other_thread->th.th_sleep_loc)) !=
2176*c47afcd9SAndrey Churbanov                  NULL)) {
21777cc577a4SJonathan Peyton               asleep = 1;
21783041982dSJonathan Peyton               __kmp_null_resume_wrapper(__kmp_gtid_from_thread(other_thread),
21793041982dSJonathan Peyton                                         other_thread->th.th_sleep_loc);
21803041982dSJonathan Peyton               // A sleeping thread should not have any tasks on it's queue.
21813041982dSJonathan Peyton               // There is a slight possibility that it resumes, steals a task
21823041982dSJonathan Peyton               // from another thread, which spawns more tasks, all in the time
21833041982dSJonathan Peyton               // that it takes this thread to check => don't write an assertion
21843041982dSJonathan Peyton               // that the victim's queue is empty.  Try stealing from a
21853041982dSJonathan Peyton               // different thread.
21867cc577a4SJonathan Peyton             }
21877cc577a4SJonathan Peyton           } while (asleep);
21887cc577a4SJonathan Peyton         }
21897cc577a4SJonathan Peyton 
21907cc577a4SJonathan Peyton         if (!asleep) {
21917cc577a4SJonathan Peyton           // We have a victim to try to steal from
21923041982dSJonathan Peyton           task = __kmp_steal_task(other_thread, gtid, task_team,
21933041982dSJonathan Peyton                                   unfinished_threads, thread_finished,
21943041982dSJonathan Peyton                                   is_constrained);
21957cc577a4SJonathan Peyton         }
21967cc577a4SJonathan Peyton         if (task != NULL) { // set last stolen to victim
21977cc577a4SJonathan Peyton           if (threads_data[tid].td.td_deque_last_stolen != victim) {
21987cc577a4SJonathan Peyton             threads_data[tid].td.td_deque_last_stolen = victim;
21993041982dSJonathan Peyton             // The pre-refactored code did not try more than 1 successful new
22003041982dSJonathan Peyton             // vicitm, unless the last one generated more local tasks;
22013041982dSJonathan Peyton             // new_victim keeps track of this
22027cc577a4SJonathan Peyton             new_victim = 1;
22037cc577a4SJonathan Peyton           }
22043041982dSJonathan Peyton         } else { // No tasks found; unset last_stolen
22057cc577a4SJonathan Peyton           KMP_CHECK_UPDATE(threads_data[tid].td.td_deque_last_stolen, -1);
22067cc577a4SJonathan Peyton           victim = -2; // no successful victim found
22077cc577a4SJonathan Peyton         }
22087cc577a4SJonathan Peyton       }
22097cc577a4SJonathan Peyton 
22107cc577a4SJonathan Peyton       if (task == NULL) // break out of tasking loop
22117cc577a4SJonathan Peyton         break;
22127cc577a4SJonathan Peyton 
22137cc577a4SJonathan Peyton // Found a task; execute it
22147cc577a4SJonathan Peyton #if USE_ITT_BUILD && USE_ITT_NOTIFY
22157cc577a4SJonathan Peyton       if (__itt_sync_create_ptr || KMP_ITT_DEBUG) {
22163041982dSJonathan Peyton         if (itt_sync_obj == NULL) { // we are at fork barrier where we could not
22173041982dSJonathan Peyton           // get the object reliably
22187cc577a4SJonathan Peyton           itt_sync_obj = __kmp_itt_barrier_object(gtid, bs_forkjoin_barrier);
22197cc577a4SJonathan Peyton         }
22207cc577a4SJonathan Peyton         __kmp_itt_task_starting(itt_sync_obj);
22217cc577a4SJonathan Peyton       }
22227cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD && USE_ITT_NOTIFY */
22237cc577a4SJonathan Peyton       __kmp_invoke_task(gtid, task, current_task);
22247cc577a4SJonathan Peyton #if USE_ITT_BUILD
22253041982dSJonathan Peyton       if (itt_sync_obj != NULL)
22263041982dSJonathan Peyton         __kmp_itt_task_finished(itt_sync_obj);
22277cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
22283041982dSJonathan Peyton       // If this thread is only partway through the barrier and the condition is
22293041982dSJonathan Peyton       // met, then return now, so that the barrier gather/release pattern can
22303041982dSJonathan Peyton       // proceed. If this thread is in the last spin loop in the barrier,
22313041982dSJonathan Peyton       // waiting to be released, we know that the termination condition will not
22323041982dSJonathan Peyton       // be satisified, so don't waste any cycles checking it.
22337cc577a4SJonathan Peyton       if (flag == NULL || (!final_spin && flag->done_check())) {
22343041982dSJonathan Peyton         KA_TRACE(
22353041982dSJonathan Peyton             15,
22363041982dSJonathan Peyton             ("__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
22373041982dSJonathan Peyton              gtid));
22387cc577a4SJonathan Peyton         return TRUE;
22397cc577a4SJonathan Peyton       }
22407cc577a4SJonathan Peyton       if (thread->th.th_task_team == NULL) {
22417cc577a4SJonathan Peyton         break;
22427cc577a4SJonathan Peyton       }
22433041982dSJonathan Peyton       // Yield before executing next task
22443041982dSJonathan Peyton       KMP_YIELD(__kmp_library == library_throughput);
22453041982dSJonathan Peyton       // If execution of a stolen task results in more tasks being placed on our
22463041982dSJonathan Peyton       // run queue, reset use_own_tasks
22477cc577a4SJonathan Peyton       if (!use_own_tasks && TCR_4(threads_data[tid].td.td_deque_ntasks) != 0) {
22483041982dSJonathan Peyton         KA_TRACE(20, ("__kmp_execute_tasks_template: T#%d stolen task spawned "
22493041982dSJonathan Peyton                       "other tasks, restart\n",
22503041982dSJonathan Peyton                       gtid));
22517cc577a4SJonathan Peyton         use_own_tasks = 1;
22527cc577a4SJonathan Peyton         new_victim = 0;
22537cc577a4SJonathan Peyton       }
22547cc577a4SJonathan Peyton     }
22557cc577a4SJonathan Peyton 
22563041982dSJonathan Peyton // The task source has been exhausted. If in final spin loop of barrier, check
22573041982dSJonathan Peyton // if termination condition is satisfied.
22587cc577a4SJonathan Peyton #if OMP_45_ENABLED
22593041982dSJonathan Peyton     // The work queue may be empty but there might be proxy tasks still
22603041982dSJonathan Peyton     // executing
22617cc577a4SJonathan Peyton     if (final_spin && TCR_4(current_task->td_incomplete_child_tasks) == 0)
22627cc577a4SJonathan Peyton #else
22637cc577a4SJonathan Peyton     if (final_spin)
22647cc577a4SJonathan Peyton #endif
22657cc577a4SJonathan Peyton     {
22663041982dSJonathan Peyton       // First, decrement the #unfinished threads, if that has not already been
22673041982dSJonathan Peyton       // done.  This decrement might be to the spin location, and result in the
22683041982dSJonathan Peyton       // termination condition being satisfied.
22697cc577a4SJonathan Peyton       if (!*thread_finished) {
2270*c47afcd9SAndrey Churbanov         kmp_int32 count;
22717cc577a4SJonathan Peyton 
2272*c47afcd9SAndrey Churbanov         count = KMP_TEST_THEN_DEC32(CCAST(kmp_int32 *, unfinished_threads)) - 1;
22733041982dSJonathan Peyton         KA_TRACE(20, ("__kmp_execute_tasks_template: T#%d dec "
22743041982dSJonathan Peyton                       "unfinished_threads to %d task_team=%p\n",
22757cc577a4SJonathan Peyton                       gtid, count, task_team));
22767cc577a4SJonathan Peyton         *thread_finished = TRUE;
22777cc577a4SJonathan Peyton       }
22787cc577a4SJonathan Peyton 
22797cc577a4SJonathan Peyton       // It is now unsafe to reference thread->th.th_team !!!
22803041982dSJonathan Peyton       // Decrementing task_team->tt.tt_unfinished_threads can allow the master
22813041982dSJonathan Peyton       // thread to pass through the barrier, where it might reset each thread's
22823041982dSJonathan Peyton       // th.th_team field for the next parallel region. If we can steal more
22833041982dSJonathan Peyton       // work, we know that this has not happened yet.
22847cc577a4SJonathan Peyton       if (flag != NULL && flag->done_check()) {
22853041982dSJonathan Peyton         KA_TRACE(
22863041982dSJonathan Peyton             15,
22873041982dSJonathan Peyton             ("__kmp_execute_tasks_template: T#%d spin condition satisfied\n",
22883041982dSJonathan Peyton              gtid));
22897cc577a4SJonathan Peyton         return TRUE;
22907cc577a4SJonathan Peyton       }
22917cc577a4SJonathan Peyton     }
22927cc577a4SJonathan Peyton 
22933041982dSJonathan Peyton     // If this thread's task team is NULL, master has recognized that there are
22943041982dSJonathan Peyton     // no more tasks; bail out
22957cc577a4SJonathan Peyton     if (thread->th.th_task_team == NULL) {
22963041982dSJonathan Peyton       KA_TRACE(15,
22973041982dSJonathan Peyton                ("__kmp_execute_tasks_template: T#%d no more tasks\n", gtid));
22987cc577a4SJonathan Peyton       return FALSE;
22997cc577a4SJonathan Peyton     }
23007cc577a4SJonathan Peyton 
23017cc577a4SJonathan Peyton #if OMP_45_ENABLED
23023041982dSJonathan Peyton     // We could be getting tasks from target constructs; if this is the only
23033041982dSJonathan Peyton     // thread, keep trying to execute tasks from own queue
23047cc577a4SJonathan Peyton     if (nthreads == 1)
23057cc577a4SJonathan Peyton       use_own_tasks = 1;
23067cc577a4SJonathan Peyton     else
23077cc577a4SJonathan Peyton #endif
23087cc577a4SJonathan Peyton     {
23093041982dSJonathan Peyton       KA_TRACE(15,
23103041982dSJonathan Peyton                ("__kmp_execute_tasks_template: T#%d can't find work\n", gtid));
23117cc577a4SJonathan Peyton       return FALSE;
23127cc577a4SJonathan Peyton     }
23137cc577a4SJonathan Peyton   }
23147cc577a4SJonathan Peyton }
23157cc577a4SJonathan Peyton 
23163041982dSJonathan Peyton int __kmp_execute_tasks_32(
23173041982dSJonathan Peyton     kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32 *flag, int final_spin,
23183041982dSJonathan Peyton     int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj),
23193041982dSJonathan Peyton     kmp_int32 is_constrained) {
23203041982dSJonathan Peyton   return __kmp_execute_tasks_template(
23213041982dSJonathan Peyton       thread, gtid, flag, final_spin,
23223041982dSJonathan Peyton       thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
23237cc577a4SJonathan Peyton }
23247cc577a4SJonathan Peyton 
23253041982dSJonathan Peyton int __kmp_execute_tasks_64(
23263041982dSJonathan Peyton     kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64 *flag, int final_spin,
23273041982dSJonathan Peyton     int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj),
23283041982dSJonathan Peyton     kmp_int32 is_constrained) {
23293041982dSJonathan Peyton   return __kmp_execute_tasks_template(
23303041982dSJonathan Peyton       thread, gtid, flag, final_spin,
23313041982dSJonathan Peyton       thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
23327cc577a4SJonathan Peyton }
23337cc577a4SJonathan Peyton 
23343041982dSJonathan Peyton int __kmp_execute_tasks_oncore(
23353041982dSJonathan Peyton     kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag, int final_spin,
23363041982dSJonathan Peyton     int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj),
23373041982dSJonathan Peyton     kmp_int32 is_constrained) {
23383041982dSJonathan Peyton   return __kmp_execute_tasks_template(
23393041982dSJonathan Peyton       thread, gtid, flag, final_spin,
23403041982dSJonathan Peyton       thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
23417cc577a4SJonathan Peyton }
23427cc577a4SJonathan Peyton 
23437cc577a4SJonathan Peyton // __kmp_enable_tasking: Allocate task team and resume threads sleeping at the
23447cc577a4SJonathan Peyton // next barrier so they can assist in executing enqueued tasks.
23457cc577a4SJonathan Peyton // First thread in allocates the task team atomically.
23463041982dSJonathan Peyton static void __kmp_enable_tasking(kmp_task_team_t *task_team,
23473041982dSJonathan Peyton                                  kmp_info_t *this_thr) {
23487cc577a4SJonathan Peyton   kmp_thread_data_t *threads_data;
23497cc577a4SJonathan Peyton   int nthreads, i, is_init_thread;
23507cc577a4SJonathan Peyton 
23517cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmp_enable_tasking(enter): T#%d\n",
23527cc577a4SJonathan Peyton                 __kmp_gtid_from_thread(this_thr)));
23537cc577a4SJonathan Peyton 
23547cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task_team != NULL);
23557cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(this_thr->th.th_team != NULL);
23567cc577a4SJonathan Peyton 
23577cc577a4SJonathan Peyton   nthreads = task_team->tt.tt_nproc;
23587cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(nthreads > 0);
23597cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(nthreads == this_thr->th.th_team->t.t_nproc);
23607cc577a4SJonathan Peyton 
23617cc577a4SJonathan Peyton   // Allocate or increase the size of threads_data if necessary
23627cc577a4SJonathan Peyton   is_init_thread = __kmp_realloc_task_threads_data(this_thr, task_team);
23637cc577a4SJonathan Peyton 
23647cc577a4SJonathan Peyton   if (!is_init_thread) {
23657cc577a4SJonathan Peyton     // Some other thread already set up the array.
23663041982dSJonathan Peyton     KA_TRACE(
23673041982dSJonathan Peyton         20,
23683041982dSJonathan Peyton         ("__kmp_enable_tasking(exit): T#%d: threads array already set up.\n",
23697cc577a4SJonathan Peyton          __kmp_gtid_from_thread(this_thr)));
23707cc577a4SJonathan Peyton     return;
23717cc577a4SJonathan Peyton   }
23727cc577a4SJonathan Peyton   threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data);
23737cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(threads_data != NULL);
23747cc577a4SJonathan Peyton 
23757cc577a4SJonathan Peyton   if ((__kmp_tasking_mode == tskm_task_teams) &&
23763041982dSJonathan Peyton       (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME)) {
23777cc577a4SJonathan Peyton     // Release any threads sleeping at the barrier, so that they can steal
23787cc577a4SJonathan Peyton     // tasks and execute them.  In extra barrier mode, tasks do not sleep
23797cc577a4SJonathan Peyton     // at the separate tasking barrier, so this isn't a problem.
23807cc577a4SJonathan Peyton     for (i = 0; i < nthreads; i++) {
23817cc577a4SJonathan Peyton       volatile void *sleep_loc;
23827cc577a4SJonathan Peyton       kmp_info_t *thread = threads_data[i].td.td_thr;
23837cc577a4SJonathan Peyton 
23847cc577a4SJonathan Peyton       if (i == this_thr->th.th_info.ds.ds_tid) {
23857cc577a4SJonathan Peyton         continue;
23867cc577a4SJonathan Peyton       }
23877cc577a4SJonathan Peyton       // Since we haven't locked the thread's suspend mutex lock at this
23887cc577a4SJonathan Peyton       // point, there is a small window where a thread might be putting
23897cc577a4SJonathan Peyton       // itself to sleep, but hasn't set the th_sleep_loc field yet.
23907cc577a4SJonathan Peyton       // To work around this, __kmp_execute_tasks_template() periodically checks
23913041982dSJonathan Peyton       // see if other threads are sleeping (using the same random mechanism that
23923041982dSJonathan Peyton       // is used for task stealing) and awakens them if they are.
2393*c47afcd9SAndrey Churbanov       if ((sleep_loc = TCR_PTR(CCAST(void *, thread->th.th_sleep_loc))) !=
2394*c47afcd9SAndrey Churbanov           NULL) {
23957cc577a4SJonathan Peyton         KF_TRACE(50, ("__kmp_enable_tasking: T#%d waking up thread T#%d\n",
23967cc577a4SJonathan Peyton                       __kmp_gtid_from_thread(this_thr),
23977cc577a4SJonathan Peyton                       __kmp_gtid_from_thread(thread)));
23987cc577a4SJonathan Peyton         __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc);
23993041982dSJonathan Peyton       } else {
24007cc577a4SJonathan Peyton         KF_TRACE(50, ("__kmp_enable_tasking: T#%d don't wake up thread T#%d\n",
24017cc577a4SJonathan Peyton                       __kmp_gtid_from_thread(this_thr),
24027cc577a4SJonathan Peyton                       __kmp_gtid_from_thread(thread)));
24037cc577a4SJonathan Peyton       }
24047cc577a4SJonathan Peyton     }
24057cc577a4SJonathan Peyton   }
24067cc577a4SJonathan Peyton 
24077cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmp_enable_tasking(exit): T#%d\n",
24087cc577a4SJonathan Peyton                 __kmp_gtid_from_thread(this_thr)));
24097cc577a4SJonathan Peyton }
24107cc577a4SJonathan Peyton 
24117cc577a4SJonathan Peyton /* // TODO: Check the comment consistency
24127cc577a4SJonathan Peyton  * Utility routines for "task teams".  A task team (kmp_task_t) is kind of
24137cc577a4SJonathan Peyton  * like a shadow of the kmp_team_t data struct, with a different lifetime.
24147cc577a4SJonathan Peyton  * After a child * thread checks into a barrier and calls __kmp_release() from
24157cc577a4SJonathan Peyton  * the particular variant of __kmp_<barrier_kind>_barrier_gather(), it can no
24167cc577a4SJonathan Peyton  * longer assume that the kmp_team_t structure is intact (at any moment, the
24177cc577a4SJonathan Peyton  * master thread may exit the barrier code and free the team data structure,
24187cc577a4SJonathan Peyton  * and return the threads to the thread pool).
24197cc577a4SJonathan Peyton  *
24207cc577a4SJonathan Peyton  * This does not work with the the tasking code, as the thread is still
24217cc577a4SJonathan Peyton  * expected to participate in the execution of any tasks that may have been
24227cc577a4SJonathan Peyton  * spawned my a member of the team, and the thread still needs access to all
24237cc577a4SJonathan Peyton  * to each thread in the team, so that it can steal work from it.
24247cc577a4SJonathan Peyton  *
24257cc577a4SJonathan Peyton  * Enter the existence of the kmp_task_team_t struct.  It employs a reference
24267cc577a4SJonathan Peyton  * counting mechanims, and is allocated by the master thread before calling
24277cc577a4SJonathan Peyton  * __kmp_<barrier_kind>_release, and then is release by the last thread to
24287cc577a4SJonathan Peyton  * exit __kmp_<barrier_kind>_release at the next barrier.  I.e. the lifetimes
24297cc577a4SJonathan Peyton  * of the kmp_task_team_t structs for consecutive barriers can overlap
24307cc577a4SJonathan Peyton  * (and will, unless the master thread is the last thread to exit the barrier
24317cc577a4SJonathan Peyton  * release phase, which is not typical).
24327cc577a4SJonathan Peyton  *
24337cc577a4SJonathan Peyton  * The existence of such a struct is useful outside the context of tasking,
24347cc577a4SJonathan Peyton  * but for now, I'm trying to keep it specific to the OMP_30_ENABLED macro,
24357cc577a4SJonathan Peyton  * so that any performance differences show up when comparing the 2.5 vs. 3.0
24367cc577a4SJonathan Peyton  * libraries.
24377cc577a4SJonathan Peyton  *
24387cc577a4SJonathan Peyton  * We currently use the existence of the threads array as an indicator that
24397cc577a4SJonathan Peyton  * tasks were spawned since the last barrier.  If the structure is to be
24407cc577a4SJonathan Peyton  * useful outside the context of tasking, then this will have to change, but
24417cc577a4SJonathan Peyton  * not settting the field minimizes the performance impact of tasking on
24427cc577a4SJonathan Peyton  * barriers, when no explicit tasks were spawned (pushed, actually).
24437cc577a4SJonathan Peyton  */
24447cc577a4SJonathan Peyton 
24453041982dSJonathan Peyton static kmp_task_team_t *__kmp_free_task_teams =
24463041982dSJonathan Peyton     NULL; // Free list for task_team data structures
24477cc577a4SJonathan Peyton // Lock for task team data structures
24483041982dSJonathan Peyton static kmp_bootstrap_lock_t __kmp_task_team_lock =
24493041982dSJonathan Peyton     KMP_BOOTSTRAP_LOCK_INITIALIZER(__kmp_task_team_lock);
24507cc577a4SJonathan Peyton 
24517cc577a4SJonathan Peyton // __kmp_alloc_task_deque:
24527cc577a4SJonathan Peyton // Allocates a task deque for a particular thread, and initialize the necessary
24537cc577a4SJonathan Peyton // data structures relating to the deque.  This only happens once per thread
24543041982dSJonathan Peyton // per task team since task teams are recycled. No lock is needed during
24553041982dSJonathan Peyton // allocation since each thread allocates its own deque.
24563041982dSJonathan Peyton static void __kmp_alloc_task_deque(kmp_info_t *thread,
24573041982dSJonathan Peyton                                    kmp_thread_data_t *thread_data) {
24587cc577a4SJonathan Peyton   __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock);
24597cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(thread_data->td.td_deque == NULL);
24607cc577a4SJonathan Peyton 
24617cc577a4SJonathan Peyton   // Initialize last stolen task field to "none"
24627cc577a4SJonathan Peyton   thread_data->td.td_deque_last_stolen = -1;
24637cc577a4SJonathan Peyton 
24647cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == 0);
24657cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(thread_data->td.td_deque_head == 0);
24667cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(thread_data->td.td_deque_tail == 0);
24677cc577a4SJonathan Peyton 
24683041982dSJonathan Peyton   KE_TRACE(
24693041982dSJonathan Peyton       10,
24703041982dSJonathan Peyton       ("__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n",
24717cc577a4SJonathan Peyton        __kmp_gtid_from_thread(thread), INITIAL_TASK_DEQUE_SIZE, thread_data));
24727cc577a4SJonathan Peyton   // Allocate space for task deque, and zero the deque
24737cc577a4SJonathan Peyton   // Cannot use __kmp_thread_calloc() because threads not around for
24747cc577a4SJonathan Peyton   // kmp_reap_task_team( ).
24753041982dSJonathan Peyton   thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate(
24763041982dSJonathan Peyton       INITIAL_TASK_DEQUE_SIZE * sizeof(kmp_taskdata_t *));
24777cc577a4SJonathan Peyton   thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE;
24787cc577a4SJonathan Peyton }
24797cc577a4SJonathan Peyton 
24807cc577a4SJonathan Peyton // __kmp_realloc_task_deque:
24813041982dSJonathan Peyton // Re-allocates a task deque for a particular thread, copies the content from
24823041982dSJonathan Peyton // the old deque and adjusts the necessary data structures relating to the
24833041982dSJonathan Peyton // deque. This operation must be done with a the deque_lock being held
24843041982dSJonathan Peyton static void __kmp_realloc_task_deque(kmp_info_t *thread,
24853041982dSJonathan Peyton                                      kmp_thread_data_t *thread_data) {
24867cc577a4SJonathan Peyton   kmp_int32 size = TASK_DEQUE_SIZE(thread_data->td);
24877cc577a4SJonathan Peyton   kmp_int32 new_size = 2 * size;
24887cc577a4SJonathan Peyton 
24893041982dSJonathan Peyton   KE_TRACE(10, ("__kmp_realloc_task_deque: T#%d reallocating deque[from %d to "
24903041982dSJonathan Peyton                 "%d] for thread_data %p\n",
24917cc577a4SJonathan Peyton                 __kmp_gtid_from_thread(thread), size, new_size, thread_data));
24927cc577a4SJonathan Peyton 
24933041982dSJonathan Peyton   kmp_taskdata_t **new_deque =
24943041982dSJonathan Peyton       (kmp_taskdata_t **)__kmp_allocate(new_size * sizeof(kmp_taskdata_t *));
24957cc577a4SJonathan Peyton 
24967cc577a4SJonathan Peyton   int i, j;
24973041982dSJonathan Peyton   for (i = thread_data->td.td_deque_head, j = 0; j < size;
24983041982dSJonathan Peyton        i = (i + 1) & TASK_DEQUE_MASK(thread_data->td), j++)
24997cc577a4SJonathan Peyton     new_deque[j] = thread_data->td.td_deque[i];
25007cc577a4SJonathan Peyton 
25017cc577a4SJonathan Peyton   __kmp_free(thread_data->td.td_deque);
25027cc577a4SJonathan Peyton 
25037cc577a4SJonathan Peyton   thread_data->td.td_deque_head = 0;
25047cc577a4SJonathan Peyton   thread_data->td.td_deque_tail = size;
25057cc577a4SJonathan Peyton   thread_data->td.td_deque = new_deque;
25067cc577a4SJonathan Peyton   thread_data->td.td_deque_size = new_size;
25077cc577a4SJonathan Peyton }
25087cc577a4SJonathan Peyton 
25097cc577a4SJonathan Peyton // __kmp_free_task_deque:
25103041982dSJonathan Peyton // Deallocates a task deque for a particular thread. Happens at library
25113041982dSJonathan Peyton // deallocation so don't need to reset all thread data fields.
25123041982dSJonathan Peyton static void __kmp_free_task_deque(kmp_thread_data_t *thread_data) {
25137cc577a4SJonathan Peyton   __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
25147cc577a4SJonathan Peyton 
25157cc577a4SJonathan Peyton   if (thread_data->td.td_deque != NULL) {
25167cc577a4SJonathan Peyton     TCW_4(thread_data->td.td_deque_ntasks, 0);
25177cc577a4SJonathan Peyton     __kmp_free(thread_data->td.td_deque);
25187cc577a4SJonathan Peyton     thread_data->td.td_deque = NULL;
25197cc577a4SJonathan Peyton   }
25207cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
25217cc577a4SJonathan Peyton 
25227cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK
25237cc577a4SJonathan Peyton   // GEH: Figure out what to do here for td_susp_tied_tasks
25247cc577a4SJonathan Peyton   if (thread_data->td.td_susp_tied_tasks.ts_entries != TASK_STACK_EMPTY) {
25257cc577a4SJonathan Peyton     __kmp_free_task_stack(__kmp_thread_from_gtid(gtid), thread_data);
25267cc577a4SJonathan Peyton   }
25277cc577a4SJonathan Peyton #endif // BUILD_TIED_TASK_STACK
25287cc577a4SJonathan Peyton }
25297cc577a4SJonathan Peyton 
25307cc577a4SJonathan Peyton // __kmp_realloc_task_threads_data:
25313041982dSJonathan Peyton // Allocates a threads_data array for a task team, either by allocating an
25323041982dSJonathan Peyton // initial array or enlarging an existing array.  Only the first thread to get
25333041982dSJonathan Peyton // the lock allocs or enlarges the array and re-initializes the array eleemnts.
25347cc577a4SJonathan Peyton // That thread returns "TRUE", the rest return "FALSE".
25357cc577a4SJonathan Peyton // Assumes that the new array size is given by task_team -> tt.tt_nproc.
25367cc577a4SJonathan Peyton // The current size is given by task_team -> tt.tt_max_threads.
25373041982dSJonathan Peyton static int __kmp_realloc_task_threads_data(kmp_info_t *thread,
25383041982dSJonathan Peyton                                            kmp_task_team_t *task_team) {
25397cc577a4SJonathan Peyton   kmp_thread_data_t **threads_data_p;
25407cc577a4SJonathan Peyton   kmp_int32 nthreads, maxthreads;
25417cc577a4SJonathan Peyton   int is_init_thread = FALSE;
25427cc577a4SJonathan Peyton 
25437cc577a4SJonathan Peyton   if (TCR_4(task_team->tt.tt_found_tasks)) {
25447cc577a4SJonathan Peyton     // Already reallocated and initialized.
25457cc577a4SJonathan Peyton     return FALSE;
25467cc577a4SJonathan Peyton   }
25477cc577a4SJonathan Peyton 
25487cc577a4SJonathan Peyton   threads_data_p = &task_team->tt.tt_threads_data;
25497cc577a4SJonathan Peyton   nthreads = task_team->tt.tt_nproc;
25507cc577a4SJonathan Peyton   maxthreads = task_team->tt.tt_max_threads;
25517cc577a4SJonathan Peyton 
25523041982dSJonathan Peyton   // All threads must lock when they encounter the first task of the implicit
25533041982dSJonathan Peyton   // task region to make sure threads_data fields are (re)initialized before
25543041982dSJonathan Peyton   // used.
25557cc577a4SJonathan Peyton   __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
25567cc577a4SJonathan Peyton 
25577cc577a4SJonathan Peyton   if (!TCR_4(task_team->tt.tt_found_tasks)) {
25587cc577a4SJonathan Peyton     // first thread to enable tasking
25597cc577a4SJonathan Peyton     kmp_team_t *team = thread->th.th_team;
25607cc577a4SJonathan Peyton     int i;
25617cc577a4SJonathan Peyton 
25627cc577a4SJonathan Peyton     is_init_thread = TRUE;
25637cc577a4SJonathan Peyton     if (maxthreads < nthreads) {
25647cc577a4SJonathan Peyton 
25657cc577a4SJonathan Peyton       if (*threads_data_p != NULL) {
25667cc577a4SJonathan Peyton         kmp_thread_data_t *old_data = *threads_data_p;
25677cc577a4SJonathan Peyton         kmp_thread_data_t *new_data = NULL;
25687cc577a4SJonathan Peyton 
25693041982dSJonathan Peyton         KE_TRACE(
25703041982dSJonathan Peyton             10,
25713041982dSJonathan Peyton             ("__kmp_realloc_task_threads_data: T#%d reallocating "
25727cc577a4SJonathan Peyton              "threads data for task_team %p, new_size = %d, old_size = %d\n",
25733041982dSJonathan Peyton              __kmp_gtid_from_thread(thread), task_team, nthreads, maxthreads));
25747cc577a4SJonathan Peyton         // Reallocate threads_data to have more elements than current array
25757cc577a4SJonathan Peyton         // Cannot use __kmp_thread_realloc() because threads not around for
25767cc577a4SJonathan Peyton         // kmp_reap_task_team( ).  Note all new array entries are initialized
25777cc577a4SJonathan Peyton         // to zero by __kmp_allocate().
25783041982dSJonathan Peyton         new_data = (kmp_thread_data_t *)__kmp_allocate(
25793041982dSJonathan Peyton             nthreads * sizeof(kmp_thread_data_t));
25807cc577a4SJonathan Peyton         // copy old data to new data
25817cc577a4SJonathan Peyton         KMP_MEMCPY_S((void *)new_data, nthreads * sizeof(kmp_thread_data_t),
25823041982dSJonathan Peyton                      (void *)old_data, maxthreads * sizeof(kmp_taskdata_t *));
25837cc577a4SJonathan Peyton 
25847cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK
25857cc577a4SJonathan Peyton         // GEH: Figure out if this is the right thing to do
25867cc577a4SJonathan Peyton         for (i = maxthreads; i < nthreads; i++) {
25877cc577a4SJonathan Peyton           kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
25887cc577a4SJonathan Peyton           __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
25897cc577a4SJonathan Peyton         }
25907cc577a4SJonathan Peyton #endif // BUILD_TIED_TASK_STACK
25917cc577a4SJonathan Peyton         // Install the new data and free the old data
25927cc577a4SJonathan Peyton         (*threads_data_p) = new_data;
25937cc577a4SJonathan Peyton         __kmp_free(old_data);
25943041982dSJonathan Peyton       } else {
25957cc577a4SJonathan Peyton         KE_TRACE(10, ("__kmp_realloc_task_threads_data: T#%d allocating "
25967cc577a4SJonathan Peyton                       "threads data for task_team %p, size = %d\n",
25977cc577a4SJonathan Peyton                       __kmp_gtid_from_thread(thread), task_team, nthreads));
25987cc577a4SJonathan Peyton         // Make the initial allocate for threads_data array, and zero entries
25997cc577a4SJonathan Peyton         // Cannot use __kmp_thread_calloc() because threads not around for
26007cc577a4SJonathan Peyton         // kmp_reap_task_team( ).
26017cc577a4SJonathan Peyton         ANNOTATE_IGNORE_WRITES_BEGIN();
26023041982dSJonathan Peyton         *threads_data_p = (kmp_thread_data_t *)__kmp_allocate(
26033041982dSJonathan Peyton             nthreads * sizeof(kmp_thread_data_t));
26047cc577a4SJonathan Peyton         ANNOTATE_IGNORE_WRITES_END();
26057cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK
26067cc577a4SJonathan Peyton         // GEH: Figure out if this is the right thing to do
26077cc577a4SJonathan Peyton         for (i = 0; i < nthreads; i++) {
26087cc577a4SJonathan Peyton           kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
26097cc577a4SJonathan Peyton           __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data);
26107cc577a4SJonathan Peyton         }
26117cc577a4SJonathan Peyton #endif // BUILD_TIED_TASK_STACK
26127cc577a4SJonathan Peyton       }
26137cc577a4SJonathan Peyton       task_team->tt.tt_max_threads = nthreads;
26143041982dSJonathan Peyton     } else {
26157cc577a4SJonathan Peyton       // If array has (more than) enough elements, go ahead and use it
26167cc577a4SJonathan Peyton       KMP_DEBUG_ASSERT(*threads_data_p != NULL);
26177cc577a4SJonathan Peyton     }
26187cc577a4SJonathan Peyton 
26197cc577a4SJonathan Peyton     // initialize threads_data pointers back to thread_info structures
26207cc577a4SJonathan Peyton     for (i = 0; i < nthreads; i++) {
26217cc577a4SJonathan Peyton       kmp_thread_data_t *thread_data = &(*threads_data_p)[i];
26227cc577a4SJonathan Peyton       thread_data->td.td_thr = team->t.t_threads[i];
26237cc577a4SJonathan Peyton 
26247cc577a4SJonathan Peyton       if (thread_data->td.td_deque_last_stolen >= nthreads) {
26257cc577a4SJonathan Peyton         // The last stolen field survives across teams / barrier, and the number
26267cc577a4SJonathan Peyton         // of threads may have changed.  It's possible (likely?) that a new
26273041982dSJonathan Peyton         // parallel region will exhibit the same behavior as previous region.
26287cc577a4SJonathan Peyton         thread_data->td.td_deque_last_stolen = -1;
26297cc577a4SJonathan Peyton       }
26307cc577a4SJonathan Peyton     }
26317cc577a4SJonathan Peyton 
26327cc577a4SJonathan Peyton     KMP_MB();
26337cc577a4SJonathan Peyton     TCW_SYNC_4(task_team->tt.tt_found_tasks, TRUE);
26347cc577a4SJonathan Peyton   }
26357cc577a4SJonathan Peyton 
26367cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
26377cc577a4SJonathan Peyton   return is_init_thread;
26387cc577a4SJonathan Peyton }
26397cc577a4SJonathan Peyton 
26407cc577a4SJonathan Peyton // __kmp_free_task_threads_data:
26417cc577a4SJonathan Peyton // Deallocates a threads_data array for a task team, including any attached
26427cc577a4SJonathan Peyton // tasking deques.  Only occurs at library shutdown.
26433041982dSJonathan Peyton static void __kmp_free_task_threads_data(kmp_task_team_t *task_team) {
26447cc577a4SJonathan Peyton   __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock);
26457cc577a4SJonathan Peyton   if (task_team->tt.tt_threads_data != NULL) {
26467cc577a4SJonathan Peyton     int i;
26477cc577a4SJonathan Peyton     for (i = 0; i < task_team->tt.tt_max_threads; i++) {
26487cc577a4SJonathan Peyton       __kmp_free_task_deque(&task_team->tt.tt_threads_data[i]);
26497cc577a4SJonathan Peyton     }
26507cc577a4SJonathan Peyton     __kmp_free(task_team->tt.tt_threads_data);
26517cc577a4SJonathan Peyton     task_team->tt.tt_threads_data = NULL;
26527cc577a4SJonathan Peyton   }
26537cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock);
26547cc577a4SJonathan Peyton }
26557cc577a4SJonathan Peyton 
26567cc577a4SJonathan Peyton // __kmp_allocate_task_team:
26577cc577a4SJonathan Peyton // Allocates a task team associated with a specific team, taking it from
26583041982dSJonathan Peyton // the global task team free list if possible.  Also initializes data
26593041982dSJonathan Peyton // structures.
26603041982dSJonathan Peyton static kmp_task_team_t *__kmp_allocate_task_team(kmp_info_t *thread,
26613041982dSJonathan Peyton                                                  kmp_team_t *team) {
26627cc577a4SJonathan Peyton   kmp_task_team_t *task_team = NULL;
26637cc577a4SJonathan Peyton   int nthreads;
26647cc577a4SJonathan Peyton 
26657cc577a4SJonathan Peyton   KA_TRACE(20, ("__kmp_allocate_task_team: T#%d entering; team = %p\n",
26667cc577a4SJonathan Peyton                 (thread ? __kmp_gtid_from_thread(thread) : -1), team));
26677cc577a4SJonathan Peyton 
26687cc577a4SJonathan Peyton   if (TCR_PTR(__kmp_free_task_teams) != NULL) {
26697cc577a4SJonathan Peyton     // Take a task team from the task team pool
26707cc577a4SJonathan Peyton     __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
26717cc577a4SJonathan Peyton     if (__kmp_free_task_teams != NULL) {
26727cc577a4SJonathan Peyton       task_team = __kmp_free_task_teams;
26737cc577a4SJonathan Peyton       TCW_PTR(__kmp_free_task_teams, task_team->tt.tt_next);
26747cc577a4SJonathan Peyton       task_team->tt.tt_next = NULL;
26757cc577a4SJonathan Peyton     }
26767cc577a4SJonathan Peyton     __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
26777cc577a4SJonathan Peyton   }
26787cc577a4SJonathan Peyton 
26797cc577a4SJonathan Peyton   if (task_team == NULL) {
26807cc577a4SJonathan Peyton     KE_TRACE(10, ("__kmp_allocate_task_team: T#%d allocating "
26817cc577a4SJonathan Peyton                   "task team for team %p\n",
26827cc577a4SJonathan Peyton                   __kmp_gtid_from_thread(thread), team));
26837cc577a4SJonathan Peyton     // Allocate a new task team if one is not available.
26847cc577a4SJonathan Peyton     // Cannot use __kmp_thread_malloc() because threads not around for
26857cc577a4SJonathan Peyton     // kmp_reap_task_team( ).
26867cc577a4SJonathan Peyton     task_team = (kmp_task_team_t *)__kmp_allocate(sizeof(kmp_task_team_t));
26877cc577a4SJonathan Peyton     __kmp_init_bootstrap_lock(&task_team->tt.tt_threads_lock);
26883041982dSJonathan Peyton     // AC: __kmp_allocate zeroes returned memory
26893041982dSJonathan Peyton     // task_team -> tt.tt_threads_data = NULL;
26907cc577a4SJonathan Peyton     // task_team -> tt.tt_max_threads = 0;
26917cc577a4SJonathan Peyton     // task_team -> tt.tt_next = NULL;
26927cc577a4SJonathan Peyton   }
26937cc577a4SJonathan Peyton 
26947cc577a4SJonathan Peyton   TCW_4(task_team->tt.tt_found_tasks, FALSE);
26957cc577a4SJonathan Peyton #if OMP_45_ENABLED
26967cc577a4SJonathan Peyton   TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
26977cc577a4SJonathan Peyton #endif
26987cc577a4SJonathan Peyton   task_team->tt.tt_nproc = nthreads = team->t.t_nproc;
26997cc577a4SJonathan Peyton 
27007cc577a4SJonathan Peyton   TCW_4(task_team->tt.tt_unfinished_threads, nthreads);
27017cc577a4SJonathan Peyton   TCW_4(task_team->tt.tt_active, TRUE);
27027cc577a4SJonathan Peyton 
27033041982dSJonathan Peyton   KA_TRACE(20, ("__kmp_allocate_task_team: T#%d exiting; task_team = %p "
27043041982dSJonathan Peyton                 "unfinished_threads init'd to %d\n",
27053041982dSJonathan Peyton                 (thread ? __kmp_gtid_from_thread(thread) : -1), task_team,
27063041982dSJonathan Peyton                 task_team->tt.tt_unfinished_threads));
27077cc577a4SJonathan Peyton   return task_team;
27087cc577a4SJonathan Peyton }
27097cc577a4SJonathan Peyton 
27107cc577a4SJonathan Peyton // __kmp_free_task_team:
27117cc577a4SJonathan Peyton // Frees the task team associated with a specific thread, and adds it
27127cc577a4SJonathan Peyton // to the global task team free list.
27133041982dSJonathan Peyton void __kmp_free_task_team(kmp_info_t *thread, kmp_task_team_t *task_team) {
27147cc577a4SJonathan Peyton   KA_TRACE(20, ("__kmp_free_task_team: T#%d task_team = %p\n",
27157cc577a4SJonathan Peyton                 thread ? __kmp_gtid_from_thread(thread) : -1, task_team));
27167cc577a4SJonathan Peyton 
27177cc577a4SJonathan Peyton   // Put task team back on free list
27187cc577a4SJonathan Peyton   __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
27197cc577a4SJonathan Peyton 
27207cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task_team->tt.tt_next == NULL);
27217cc577a4SJonathan Peyton   task_team->tt.tt_next = __kmp_free_task_teams;
27227cc577a4SJonathan Peyton   TCW_PTR(__kmp_free_task_teams, task_team);
27237cc577a4SJonathan Peyton 
27247cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
27257cc577a4SJonathan Peyton }
27267cc577a4SJonathan Peyton 
27277cc577a4SJonathan Peyton // __kmp_reap_task_teams:
27287cc577a4SJonathan Peyton // Free all the task teams on the task team free list.
27297cc577a4SJonathan Peyton // Should only be done during library shutdown.
27303041982dSJonathan Peyton // Cannot do anything that needs a thread structure or gtid since they are
27313041982dSJonathan Peyton // already gone.
27323041982dSJonathan Peyton void __kmp_reap_task_teams(void) {
27337cc577a4SJonathan Peyton   kmp_task_team_t *task_team;
27347cc577a4SJonathan Peyton 
27357cc577a4SJonathan Peyton   if (TCR_PTR(__kmp_free_task_teams) != NULL) {
27367cc577a4SJonathan Peyton     // Free all task_teams on the free list
27377cc577a4SJonathan Peyton     __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock);
27387cc577a4SJonathan Peyton     while ((task_team = __kmp_free_task_teams) != NULL) {
27397cc577a4SJonathan Peyton       __kmp_free_task_teams = task_team->tt.tt_next;
27407cc577a4SJonathan Peyton       task_team->tt.tt_next = NULL;
27417cc577a4SJonathan Peyton 
27427cc577a4SJonathan Peyton       // Free threads_data if necessary
27437cc577a4SJonathan Peyton       if (task_team->tt.tt_threads_data != NULL) {
27447cc577a4SJonathan Peyton         __kmp_free_task_threads_data(task_team);
27457cc577a4SJonathan Peyton       }
27467cc577a4SJonathan Peyton       __kmp_free(task_team);
27477cc577a4SJonathan Peyton     }
27487cc577a4SJonathan Peyton     __kmp_release_bootstrap_lock(&__kmp_task_team_lock);
27497cc577a4SJonathan Peyton   }
27507cc577a4SJonathan Peyton }
27517cc577a4SJonathan Peyton 
27527cc577a4SJonathan Peyton // __kmp_wait_to_unref_task_teams:
27537cc577a4SJonathan Peyton // Some threads could still be in the fork barrier release code, possibly
27547cc577a4SJonathan Peyton // trying to steal tasks.  Wait for each thread to unreference its task team.
27553041982dSJonathan Peyton void __kmp_wait_to_unref_task_teams(void) {
27567cc577a4SJonathan Peyton   kmp_info_t *thread;
27577cc577a4SJonathan Peyton   kmp_uint32 spins;
27587cc577a4SJonathan Peyton   int done;
27597cc577a4SJonathan Peyton 
27607cc577a4SJonathan Peyton   KMP_INIT_YIELD(spins);
27617cc577a4SJonathan Peyton 
27627cc577a4SJonathan Peyton   for (;;) {
27637cc577a4SJonathan Peyton     done = TRUE;
27647cc577a4SJonathan Peyton 
27657cc577a4SJonathan Peyton     // TODO: GEH - this may be is wrong because some sync would be necessary
27663041982dSJonathan Peyton     // in case threads are added to the pool during the traversal. Need to
27673041982dSJonathan Peyton     // verify that lock for thread pool is held when calling this routine.
2768*c47afcd9SAndrey Churbanov     for (thread = CCAST(kmp_info_t *, __kmp_thread_pool); thread != NULL;
27693041982dSJonathan Peyton          thread = thread->th.th_next_pool) {
27707cc577a4SJonathan Peyton #if KMP_OS_WINDOWS
27717cc577a4SJonathan Peyton       DWORD exit_val;
27727cc577a4SJonathan Peyton #endif
27737cc577a4SJonathan Peyton       if (TCR_PTR(thread->th.th_task_team) == NULL) {
27747cc577a4SJonathan Peyton         KA_TRACE(10, ("__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n",
27757cc577a4SJonathan Peyton                       __kmp_gtid_from_thread(thread)));
27767cc577a4SJonathan Peyton         continue;
27777cc577a4SJonathan Peyton       }
27787cc577a4SJonathan Peyton #if KMP_OS_WINDOWS
27797cc577a4SJonathan Peyton       // TODO: GEH - add this check for Linux* OS / OS X* as well?
27807cc577a4SJonathan Peyton       if (!__kmp_is_thread_alive(thread, &exit_val)) {
27817cc577a4SJonathan Peyton         thread->th.th_task_team = NULL;
27827cc577a4SJonathan Peyton         continue;
27837cc577a4SJonathan Peyton       }
27847cc577a4SJonathan Peyton #endif
27857cc577a4SJonathan Peyton 
27867cc577a4SJonathan Peyton       done = FALSE; // Because th_task_team pointer is not NULL for this thread
27877cc577a4SJonathan Peyton 
27883041982dSJonathan Peyton       KA_TRACE(10, ("__kmp_wait_to_unref_task_team: Waiting for T#%d to "
27893041982dSJonathan Peyton                     "unreference task_team\n",
27907cc577a4SJonathan Peyton                     __kmp_gtid_from_thread(thread)));
27917cc577a4SJonathan Peyton 
27927cc577a4SJonathan Peyton       if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
27937cc577a4SJonathan Peyton         volatile void *sleep_loc;
27947cc577a4SJonathan Peyton         // If the thread is sleeping, awaken it.
2795*c47afcd9SAndrey Churbanov         if ((sleep_loc = TCR_PTR(CCAST(void *, thread->th.th_sleep_loc))) !=
2796*c47afcd9SAndrey Churbanov             NULL) {
27973041982dSJonathan Peyton           KA_TRACE(
27983041982dSJonathan Peyton               10,
27993041982dSJonathan Peyton               ("__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n",
28007cc577a4SJonathan Peyton                __kmp_gtid_from_thread(thread), __kmp_gtid_from_thread(thread)));
28017cc577a4SJonathan Peyton           __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc);
28027cc577a4SJonathan Peyton         }
28037cc577a4SJonathan Peyton       }
28047cc577a4SJonathan Peyton     }
28057cc577a4SJonathan Peyton     if (done) {
28067cc577a4SJonathan Peyton       break;
28077cc577a4SJonathan Peyton     }
28087cc577a4SJonathan Peyton 
28093041982dSJonathan Peyton     // If we are oversubscribed, or have waited a bit (and library mode is
28103041982dSJonathan Peyton     // throughput), yield. Pause is in the following code.
28117cc577a4SJonathan Peyton     KMP_YIELD(TCR_4(__kmp_nth) > __kmp_avail_proc);
28127cc577a4SJonathan Peyton     KMP_YIELD_SPIN(spins); // Yields only if KMP_LIBRARY=throughput
28137cc577a4SJonathan Peyton   }
28147cc577a4SJonathan Peyton }
28157cc577a4SJonathan Peyton 
28167cc577a4SJonathan Peyton // __kmp_task_team_setup:  Create a task_team for the current team, but use
28177cc577a4SJonathan Peyton // an already created, unused one if it already exists.
28183041982dSJonathan Peyton void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team, int always) {
28197cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
28207cc577a4SJonathan Peyton 
28213041982dSJonathan Peyton   // If this task_team hasn't been created yet, allocate it. It will be used in
28223041982dSJonathan Peyton   // the region after the next.
28233041982dSJonathan Peyton   // If it exists, it is the current task team and shouldn't be touched yet as
28243041982dSJonathan Peyton   // it may still be in use.
28253041982dSJonathan Peyton   if (team->t.t_task_team[this_thr->th.th_task_state] == NULL &&
28263041982dSJonathan Peyton       (always || team->t.t_nproc > 1)) {
28273041982dSJonathan Peyton     team->t.t_task_team[this_thr->th.th_task_state] =
28283041982dSJonathan Peyton         __kmp_allocate_task_team(this_thr, team);
28293041982dSJonathan Peyton     KA_TRACE(20, ("__kmp_task_team_setup: Master T#%d created new task_team %p "
28303041982dSJonathan Peyton                   "for team %d at parity=%d\n",
28313041982dSJonathan Peyton                   __kmp_gtid_from_thread(this_thr),
28323041982dSJonathan Peyton                   team->t.t_task_team[this_thr->th.th_task_state],
28333041982dSJonathan Peyton                   ((team != NULL) ? team->t.t_id : -1),
28343041982dSJonathan Peyton                   this_thr->th.th_task_state));
28357cc577a4SJonathan Peyton   }
28367cc577a4SJonathan Peyton 
28373041982dSJonathan Peyton   // After threads exit the release, they will call sync, and then point to this
28383041982dSJonathan Peyton   // other task_team; make sure it is allocated and properly initialized. As
28393041982dSJonathan Peyton   // threads spin in the barrier release phase, they will continue to use the
28403041982dSJonathan Peyton   // previous task_team struct(above), until they receive the signal to stop
28413041982dSJonathan Peyton   // checking for tasks (they can't safely reference the kmp_team_t struct,
28423041982dSJonathan Peyton   // which could be reallocated by the master thread). No task teams are formed
28433041982dSJonathan Peyton   // for serialized teams.
28447cc577a4SJonathan Peyton   if (team->t.t_nproc > 1) {
28457cc577a4SJonathan Peyton     int other_team = 1 - this_thr->th.th_task_state;
28467cc577a4SJonathan Peyton     if (team->t.t_task_team[other_team] == NULL) { // setup other team as well
28473041982dSJonathan Peyton       team->t.t_task_team[other_team] =
28483041982dSJonathan Peyton           __kmp_allocate_task_team(this_thr, team);
28493041982dSJonathan Peyton       KA_TRACE(20, ("__kmp_task_team_setup: Master T#%d created second new "
28503041982dSJonathan Peyton                     "task_team %p for team %d at parity=%d\n",
28513041982dSJonathan Peyton                     __kmp_gtid_from_thread(this_thr),
28523041982dSJonathan Peyton                     team->t.t_task_team[other_team],
28537cc577a4SJonathan Peyton                     ((team != NULL) ? team->t.t_id : -1), other_team));
28543041982dSJonathan Peyton     } else { // Leave the old task team struct in place for the upcoming region;
28553041982dSJonathan Peyton       // adjust as needed
28567cc577a4SJonathan Peyton       kmp_task_team_t *task_team = team->t.t_task_team[other_team];
28573041982dSJonathan Peyton       if (!task_team->tt.tt_active ||
28583041982dSJonathan Peyton           team->t.t_nproc != task_team->tt.tt_nproc) {
28597cc577a4SJonathan Peyton         TCW_4(task_team->tt.tt_nproc, team->t.t_nproc);
28607cc577a4SJonathan Peyton         TCW_4(task_team->tt.tt_found_tasks, FALSE);
28617cc577a4SJonathan Peyton #if OMP_45_ENABLED
28627cc577a4SJonathan Peyton         TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE);
28637cc577a4SJonathan Peyton #endif
28647cc577a4SJonathan Peyton         TCW_4(task_team->tt.tt_unfinished_threads, team->t.t_nproc);
28657cc577a4SJonathan Peyton         TCW_4(task_team->tt.tt_active, TRUE);
28667cc577a4SJonathan Peyton       }
28673041982dSJonathan Peyton       // if team size has changed, the first thread to enable tasking will
28683041982dSJonathan Peyton       // realloc threads_data if necessary
28693041982dSJonathan Peyton       KA_TRACE(20, ("__kmp_task_team_setup: Master T#%d reset next task_team "
28703041982dSJonathan Peyton                     "%p for team %d at parity=%d\n",
28713041982dSJonathan Peyton                     __kmp_gtid_from_thread(this_thr),
28723041982dSJonathan Peyton                     team->t.t_task_team[other_team],
28737cc577a4SJonathan Peyton                     ((team != NULL) ? team->t.t_id : -1), other_team));
28747cc577a4SJonathan Peyton     }
28757cc577a4SJonathan Peyton   }
28767cc577a4SJonathan Peyton }
28777cc577a4SJonathan Peyton 
28787cc577a4SJonathan Peyton // __kmp_task_team_sync: Propagation of task team data from team to threads
28797cc577a4SJonathan Peyton // which happens just after the release phase of a team barrier.  This may be
28807cc577a4SJonathan Peyton // called by any thread, but only for teams with # threads > 1.
28813041982dSJonathan Peyton void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team) {
28827cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
28837cc577a4SJonathan Peyton 
28843041982dSJonathan Peyton   // Toggle the th_task_state field, to switch which task_team this thread
28853041982dSJonathan Peyton   // refers to
28867cc577a4SJonathan Peyton   this_thr->th.th_task_state = 1 - this_thr->th.th_task_state;
28873041982dSJonathan Peyton   // It is now safe to propagate the task team pointer from the team struct to
28883041982dSJonathan Peyton   // the current thread.
28893041982dSJonathan Peyton   TCW_PTR(this_thr->th.th_task_team,
28903041982dSJonathan Peyton           team->t.t_task_team[this_thr->th.th_task_state]);
28913041982dSJonathan Peyton   KA_TRACE(20,
28923041982dSJonathan Peyton            ("__kmp_task_team_sync: Thread T#%d task team switched to task_team "
28933041982dSJonathan Peyton             "%p from Team #%d (parity=%d)\n",
28947cc577a4SJonathan Peyton             __kmp_gtid_from_thread(this_thr), this_thr->th.th_task_team,
28957cc577a4SJonathan Peyton             ((team != NULL) ? team->t.t_id : -1), this_thr->th.th_task_state));
28967cc577a4SJonathan Peyton }
28977cc577a4SJonathan Peyton 
28983041982dSJonathan Peyton // __kmp_task_team_wait: Master thread waits for outstanding tasks after the
28993041982dSJonathan Peyton // barrier gather phase. Only called by master thread if #threads in team > 1 or
29003041982dSJonathan Peyton // if proxy tasks were created.
29013041982dSJonathan Peyton //
29023041982dSJonathan Peyton // wait is a flag that defaults to 1 (see kmp.h), but waiting can be turned off
29033041982dSJonathan Peyton // by passing in 0 optionally as the last argument. When wait is zero, master
29043041982dSJonathan Peyton // thread does not wait for unfinished_threads to reach 0.
29053041982dSJonathan Peyton void __kmp_task_team_wait(
29063041982dSJonathan Peyton     kmp_info_t *this_thr,
29073041982dSJonathan Peyton     kmp_team_t *team USE_ITT_BUILD_ARG(void *itt_sync_obj), int wait) {
29087cc577a4SJonathan Peyton   kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state];
29097cc577a4SJonathan Peyton 
29107cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);
29117cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task_team == this_thr->th.th_task_team);
29127cc577a4SJonathan Peyton 
29137cc577a4SJonathan Peyton   if ((task_team != NULL) && KMP_TASKING_ENABLED(task_team)) {
29147cc577a4SJonathan Peyton     if (wait) {
29153041982dSJonathan Peyton       KA_TRACE(20, ("__kmp_task_team_wait: Master T#%d waiting for all tasks "
29163041982dSJonathan Peyton                     "(for unfinished_threads to reach 0) on task_team = %p\n",
29177cc577a4SJonathan Peyton                     __kmp_gtid_from_thread(this_thr), task_team));
29183041982dSJonathan Peyton       // Worker threads may have dropped through to release phase, but could
29193041982dSJonathan Peyton       // still be executing tasks. Wait here for tasks to complete. To avoid
29203041982dSJonathan Peyton       // memory contention, only master thread checks termination condition.
2921*c47afcd9SAndrey Churbanov       kmp_flag_32 flag(
2922*c47afcd9SAndrey Churbanov           RCAST(volatile kmp_uint32 *, &task_team->tt.tt_unfinished_threads),
2923*c47afcd9SAndrey Churbanov           0U);
29243041982dSJonathan Peyton       flag.wait(this_thr, TRUE USE_ITT_BUILD_ARG(itt_sync_obj));
29257cc577a4SJonathan Peyton     }
29263041982dSJonathan Peyton     // Deactivate the old task team, so that the worker threads will stop
29273041982dSJonathan Peyton     // referencing it while spinning.
29283041982dSJonathan Peyton     KA_TRACE(
29293041982dSJonathan Peyton         20,
29303041982dSJonathan Peyton         ("__kmp_task_team_wait: Master T#%d deactivating task_team %p: "
29317cc577a4SJonathan Peyton          "setting active to false, setting local and team's pointer to NULL\n",
29327cc577a4SJonathan Peyton          __kmp_gtid_from_thread(this_thr), task_team));
29337cc577a4SJonathan Peyton #if OMP_45_ENABLED
29343041982dSJonathan Peyton     KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1 ||
29353041982dSJonathan Peyton                      task_team->tt.tt_found_proxy_tasks == TRUE);
29367cc577a4SJonathan Peyton     TCW_SYNC_4(task_team->tt.tt_found_proxy_tasks, FALSE);
29377cc577a4SJonathan Peyton #else
29387cc577a4SJonathan Peyton     KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1);
29397cc577a4SJonathan Peyton #endif
29407cc577a4SJonathan Peyton     TCW_SYNC_4(task_team->tt.tt_active, FALSE);
29417cc577a4SJonathan Peyton     KMP_MB();
29427cc577a4SJonathan Peyton 
29437cc577a4SJonathan Peyton     TCW_PTR(this_thr->th.th_task_team, NULL);
29447cc577a4SJonathan Peyton   }
29457cc577a4SJonathan Peyton }
29467cc577a4SJonathan Peyton 
29477cc577a4SJonathan Peyton // __kmp_tasking_barrier:
29487cc577a4SJonathan Peyton // This routine may only called when __kmp_tasking_mode == tskm_extra_barrier.
29493041982dSJonathan Peyton // Internal function to execute all tasks prior to a regular barrier or a join
29503041982dSJonathan Peyton // barrier. It is a full barrier itself, which unfortunately turns regular
29513041982dSJonathan Peyton // barriers into double barriers and join barriers into 1 1/2 barriers.
29523041982dSJonathan Peyton void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread, int gtid) {
2953*c47afcd9SAndrey Churbanov   volatile kmp_uint32 *spin = RCAST(
2954*c47afcd9SAndrey Churbanov       volatile kmp_uint32 *,
2955*c47afcd9SAndrey Churbanov       &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads);
29567cc577a4SJonathan Peyton   int flag = FALSE;
29577cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(__kmp_tasking_mode == tskm_extra_barrier);
29587cc577a4SJonathan Peyton 
29597cc577a4SJonathan Peyton #if USE_ITT_BUILD
29607cc577a4SJonathan Peyton   KMP_FSYNC_SPIN_INIT(spin, (kmp_uint32 *)NULL);
29617cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
29627cc577a4SJonathan Peyton   kmp_flag_32 spin_flag(spin, 0U);
29633041982dSJonathan Peyton   while (!spin_flag.execute_tasks(thread, gtid, TRUE,
29643041982dSJonathan Peyton                                   &flag USE_ITT_BUILD_ARG(NULL), 0)) {
29657cc577a4SJonathan Peyton #if USE_ITT_BUILD
29667cc577a4SJonathan Peyton     // TODO: What about itt_sync_obj??
2967*c47afcd9SAndrey Churbanov     KMP_FSYNC_SPIN_PREPARE(CCAST(void *, RCAST(volatile void *, spin)));
29687cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
29697cc577a4SJonathan Peyton 
29707cc577a4SJonathan Peyton     if (TCR_4(__kmp_global.g.g_done)) {
29717cc577a4SJonathan Peyton       if (__kmp_global.g.g_abort)
29727cc577a4SJonathan Peyton         __kmp_abort_thread();
29737cc577a4SJonathan Peyton       break;
29747cc577a4SJonathan Peyton     }
29757cc577a4SJonathan Peyton     KMP_YIELD(TRUE); // GH: We always yield here
29767cc577a4SJonathan Peyton   }
29777cc577a4SJonathan Peyton #if USE_ITT_BUILD
2978*c47afcd9SAndrey Churbanov   KMP_FSYNC_SPIN_ACQUIRED(CCAST(void *, RCAST(volatile void *, spin)));
29797cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */
29807cc577a4SJonathan Peyton }
29817cc577a4SJonathan Peyton 
29827cc577a4SJonathan Peyton #if OMP_45_ENABLED
29837cc577a4SJonathan Peyton 
29843041982dSJonathan Peyton // __kmp_give_task puts a task into a given thread queue if:
29853041982dSJonathan Peyton //  - the queue for that thread was created
29863041982dSJonathan Peyton //  - there's space in that queue
29873041982dSJonathan Peyton // Because of this, __kmp_push_task needs to check if there's space after
29883041982dSJonathan Peyton // getting the lock
29893041982dSJonathan Peyton static bool __kmp_give_task(kmp_info_t *thread, kmp_int32 tid, kmp_task_t *task,
29903041982dSJonathan Peyton                             kmp_int32 pass) {
29917cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
29927cc577a4SJonathan Peyton   kmp_task_team_t *task_team = taskdata->td_task_team;
29937cc577a4SJonathan Peyton 
29943041982dSJonathan Peyton   KA_TRACE(20, ("__kmp_give_task: trying to give task %p to thread %d.\n",
29953041982dSJonathan Peyton                 taskdata, tid));
29967cc577a4SJonathan Peyton 
29977cc577a4SJonathan Peyton   // If task_team is NULL something went really bad...
29987cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task_team != NULL);
29997cc577a4SJonathan Peyton 
30007cc577a4SJonathan Peyton   bool result = false;
30017cc577a4SJonathan Peyton   kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid];
30027cc577a4SJonathan Peyton 
30037cc577a4SJonathan Peyton   if (thread_data->td.td_deque == NULL) {
30047cc577a4SJonathan Peyton     // There's no queue in this thread, go find another one
30057cc577a4SJonathan Peyton     // We're guaranteed that at least one thread has a queue
30063041982dSJonathan Peyton     KA_TRACE(30,
30073041982dSJonathan Peyton              ("__kmp_give_task: thread %d has no queue while giving task %p.\n",
30083041982dSJonathan Peyton               tid, taskdata));
30097cc577a4SJonathan Peyton     return result;
30107cc577a4SJonathan Peyton   }
30117cc577a4SJonathan Peyton 
30123041982dSJonathan Peyton   if (TCR_4(thread_data->td.td_deque_ntasks) >=
30133041982dSJonathan Peyton       TASK_DEQUE_SIZE(thread_data->td)) {
30143041982dSJonathan Peyton     KA_TRACE(
30153041982dSJonathan Peyton         30,
30163041982dSJonathan Peyton         ("__kmp_give_task: queue is full while giving task %p to thread %d.\n",
30173041982dSJonathan Peyton          taskdata, tid));
30187cc577a4SJonathan Peyton 
30193041982dSJonathan Peyton     // if this deque is bigger than the pass ratio give a chance to another
30203041982dSJonathan Peyton     // thread
30213041982dSJonathan Peyton     if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
30223041982dSJonathan Peyton       return result;
30237cc577a4SJonathan Peyton 
30247cc577a4SJonathan Peyton     __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
30257cc577a4SJonathan Peyton     __kmp_realloc_task_deque(thread, thread_data);
30267cc577a4SJonathan Peyton 
30277cc577a4SJonathan Peyton   } else {
30287cc577a4SJonathan Peyton 
30297cc577a4SJonathan Peyton     __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
30307cc577a4SJonathan Peyton 
30313041982dSJonathan Peyton     if (TCR_4(thread_data->td.td_deque_ntasks) >=
30323041982dSJonathan Peyton         TASK_DEQUE_SIZE(thread_data->td)) {
30333041982dSJonathan Peyton       KA_TRACE(30, ("__kmp_give_task: queue is full while giving task %p to "
30343041982dSJonathan Peyton                     "thread %d.\n",
30353041982dSJonathan Peyton                     taskdata, tid));
30367cc577a4SJonathan Peyton 
30373041982dSJonathan Peyton       // if this deque is bigger than the pass ratio give a chance to another
30383041982dSJonathan Peyton       // thread
30397cc577a4SJonathan Peyton       if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass)
30407cc577a4SJonathan Peyton         goto release_and_exit;
30417cc577a4SJonathan Peyton 
30427cc577a4SJonathan Peyton       __kmp_realloc_task_deque(thread, thread_data);
30437cc577a4SJonathan Peyton     }
30447cc577a4SJonathan Peyton   }
30457cc577a4SJonathan Peyton 
30467cc577a4SJonathan Peyton   // lock is held here, and there is space in the deque
30477cc577a4SJonathan Peyton 
30487cc577a4SJonathan Peyton   thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata;
30497cc577a4SJonathan Peyton   // Wrap index.
30503041982dSJonathan Peyton   thread_data->td.td_deque_tail =
30513041982dSJonathan Peyton       (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td);
30523041982dSJonathan Peyton   TCW_4(thread_data->td.td_deque_ntasks,
30533041982dSJonathan Peyton         TCR_4(thread_data->td.td_deque_ntasks) + 1);
30547cc577a4SJonathan Peyton 
30557cc577a4SJonathan Peyton   result = true;
30563041982dSJonathan Peyton   KA_TRACE(30, ("__kmp_give_task: successfully gave task %p to thread %d.\n",
30573041982dSJonathan Peyton                 taskdata, tid));
30587cc577a4SJonathan Peyton 
30597cc577a4SJonathan Peyton release_and_exit:
30607cc577a4SJonathan Peyton   __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
30617cc577a4SJonathan Peyton 
30627cc577a4SJonathan Peyton   return result;
30637cc577a4SJonathan Peyton }
30647cc577a4SJonathan Peyton 
30653041982dSJonathan Peyton /* The finish of the proxy tasks is divided in two pieces:
30667cc577a4SJonathan Peyton     - the top half is the one that can be done from a thread outside the team
30677cc577a4SJonathan Peyton     - the bottom half must be run from a them within the team
30687cc577a4SJonathan Peyton 
30693041982dSJonathan Peyton    In order to run the bottom half the task gets queued back into one of the
30703041982dSJonathan Peyton    threads of the team. Once the td_incomplete_child_task counter of the parent
30713041982dSJonathan Peyton    is decremented the threads can leave the barriers. So, the bottom half needs
30723041982dSJonathan Peyton    to be queued before the counter is decremented. The top half is therefore
30733041982dSJonathan Peyton    divided in two parts:
30747cc577a4SJonathan Peyton     - things that can be run before queuing the bottom half
30757cc577a4SJonathan Peyton     - things that must be run after queuing the bottom half
30767cc577a4SJonathan Peyton 
30773041982dSJonathan Peyton    This creates a second race as the bottom half can free the task before the
30783041982dSJonathan Peyton    second top half is executed. To avoid this we use the
30793041982dSJonathan Peyton    td_incomplete_child_task of the proxy task to synchronize the top and bottom
30803041982dSJonathan Peyton    half. */
30813041982dSJonathan Peyton static void __kmp_first_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
30827cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT);
30837cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
30847cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0);
30857cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0);
30867cc577a4SJonathan Peyton 
30877cc577a4SJonathan Peyton   taskdata->td_flags.complete = 1; // mark the task as completed
30887cc577a4SJonathan Peyton 
30897cc577a4SJonathan Peyton   if (taskdata->td_taskgroup)
3090*c47afcd9SAndrey Churbanov     KMP_TEST_THEN_DEC32(&taskdata->td_taskgroup->count);
30917cc577a4SJonathan Peyton 
30923041982dSJonathan Peyton   // Create an imaginary children for this task so the bottom half cannot
30933041982dSJonathan Peyton   // release the task before we have completed the second top half
30947cc577a4SJonathan Peyton   TCI_4(taskdata->td_incomplete_child_tasks);
30957cc577a4SJonathan Peyton }
30967cc577a4SJonathan Peyton 
30973041982dSJonathan Peyton static void __kmp_second_top_half_finish_proxy(kmp_taskdata_t *taskdata) {
30987cc577a4SJonathan Peyton   kmp_int32 children = 0;
30997cc577a4SJonathan Peyton 
31007cc577a4SJonathan Peyton   // Predecrement simulated by "- 1" calculation
31013041982dSJonathan Peyton   children =
31023041982dSJonathan Peyton       KMP_TEST_THEN_DEC32(
3103*c47afcd9SAndrey Churbanov           CCAST(kmp_int32 *, &taskdata->td_parent->td_incomplete_child_tasks)) -
31043041982dSJonathan Peyton       1;
31057cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(children >= 0);
31067cc577a4SJonathan Peyton 
31077cc577a4SJonathan Peyton   // Remove the imaginary children
31087cc577a4SJonathan Peyton   TCD_4(taskdata->td_incomplete_child_tasks);
31097cc577a4SJonathan Peyton }
31107cc577a4SJonathan Peyton 
31113041982dSJonathan Peyton static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask) {
31127cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
31137cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
31147cc577a4SJonathan Peyton 
31157cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
31163041982dSJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.complete ==
31173041982dSJonathan Peyton                    1); // top half must run before bottom half
31187cc577a4SJonathan Peyton 
31197cc577a4SJonathan Peyton   // We need to wait to make sure the top half is finished
31207cc577a4SJonathan Peyton   // Spinning here should be ok as this should happen quickly
31213041982dSJonathan Peyton   while (TCR_4(taskdata->td_incomplete_child_tasks) > 0)
31223041982dSJonathan Peyton     ;
31237cc577a4SJonathan Peyton 
31247cc577a4SJonathan Peyton   __kmp_release_deps(gtid, taskdata);
31257cc577a4SJonathan Peyton   __kmp_free_task_and_ancestors(gtid, taskdata, thread);
31267cc577a4SJonathan Peyton }
31277cc577a4SJonathan Peyton 
31287cc577a4SJonathan Peyton /*!
31297cc577a4SJonathan Peyton @ingroup TASKING
31307cc577a4SJonathan Peyton @param gtid Global Thread ID of encountering thread
31317cc577a4SJonathan Peyton @param ptask Task which execution is completed
31327cc577a4SJonathan Peyton 
31333041982dSJonathan Peyton Execute the completation of a proxy task from a thread of that is part of the
31343041982dSJonathan Peyton team. Run first and bottom halves directly.
31357cc577a4SJonathan Peyton */
31363041982dSJonathan Peyton void __kmpc_proxy_task_completed(kmp_int32 gtid, kmp_task_t *ptask) {
31377cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(ptask != NULL);
31387cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
31393041982dSJonathan Peyton   KA_TRACE(
31403041982dSJonathan Peyton       10, ("__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n",
31413041982dSJonathan Peyton            gtid, taskdata));
31427cc577a4SJonathan Peyton 
31437cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
31447cc577a4SJonathan Peyton 
31457cc577a4SJonathan Peyton   __kmp_first_top_half_finish_proxy(taskdata);
31467cc577a4SJonathan Peyton   __kmp_second_top_half_finish_proxy(taskdata);
31477cc577a4SJonathan Peyton   __kmp_bottom_half_finish_proxy(gtid, ptask);
31487cc577a4SJonathan Peyton 
31493041982dSJonathan Peyton   KA_TRACE(10,
31503041982dSJonathan Peyton            ("__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n",
31513041982dSJonathan Peyton             gtid, taskdata));
31527cc577a4SJonathan Peyton }
31537cc577a4SJonathan Peyton 
31547cc577a4SJonathan Peyton /*!
31557cc577a4SJonathan Peyton @ingroup TASKING
31567cc577a4SJonathan Peyton @param ptask Task which execution is completed
31577cc577a4SJonathan Peyton 
31583041982dSJonathan Peyton Execute the completation of a proxy task from a thread that could not belong to
31593041982dSJonathan Peyton the team.
31607cc577a4SJonathan Peyton */
31613041982dSJonathan Peyton void __kmpc_proxy_task_completed_ooo(kmp_task_t *ptask) {
31627cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(ptask != NULL);
31637cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask);
31647cc577a4SJonathan Peyton 
31653041982dSJonathan Peyton   KA_TRACE(
31663041982dSJonathan Peyton       10,
31673041982dSJonathan Peyton       ("__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n",
31683041982dSJonathan Peyton        taskdata));
31697cc577a4SJonathan Peyton 
31707cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY);
31717cc577a4SJonathan Peyton 
31727cc577a4SJonathan Peyton   __kmp_first_top_half_finish_proxy(taskdata);
31737cc577a4SJonathan Peyton 
31743041982dSJonathan Peyton   // Enqueue task to complete bottom half completion from a thread within the
31753041982dSJonathan Peyton   // corresponding team
31767cc577a4SJonathan Peyton   kmp_team_t *team = taskdata->td_team;
31777cc577a4SJonathan Peyton   kmp_int32 nthreads = team->t.t_nproc;
31787cc577a4SJonathan Peyton   kmp_info_t *thread;
31797cc577a4SJonathan Peyton 
31803041982dSJonathan Peyton   // This should be similar to start_k = __kmp_get_random( thread ) % nthreads
31813041982dSJonathan Peyton   // but we cannot use __kmp_get_random here
31827cc577a4SJonathan Peyton   kmp_int32 start_k = 0;
31837cc577a4SJonathan Peyton   kmp_int32 pass = 1;
31847cc577a4SJonathan Peyton   kmp_int32 k = start_k;
31857cc577a4SJonathan Peyton 
31867cc577a4SJonathan Peyton   do {
31877cc577a4SJonathan Peyton     // For now we're just linearly trying to find a thread
31887cc577a4SJonathan Peyton     thread = team->t.t_threads[k];
31897cc577a4SJonathan Peyton     k = (k + 1) % nthreads;
31907cc577a4SJonathan Peyton 
31917cc577a4SJonathan Peyton     // we did a full pass through all the threads
31923041982dSJonathan Peyton     if (k == start_k)
31933041982dSJonathan Peyton       pass = pass << 1;
31947cc577a4SJonathan Peyton 
31957cc577a4SJonathan Peyton   } while (!__kmp_give_task(thread, k, ptask, pass));
31967cc577a4SJonathan Peyton 
31977cc577a4SJonathan Peyton   __kmp_second_top_half_finish_proxy(taskdata);
31987cc577a4SJonathan Peyton 
31993041982dSJonathan Peyton   KA_TRACE(
32003041982dSJonathan Peyton       10,
32013041982dSJonathan Peyton       ("__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n",
32023041982dSJonathan Peyton        taskdata));
32037cc577a4SJonathan Peyton }
32047cc577a4SJonathan Peyton 
32053041982dSJonathan Peyton // __kmp_task_dup_alloc: Allocate the taskdata and make a copy of source task
32063041982dSJonathan Peyton // for taskloop
32077cc577a4SJonathan Peyton //
32087cc577a4SJonathan Peyton // thread:   allocating thread
32097cc577a4SJonathan Peyton // task_src: pointer to source task to be duplicated
32107cc577a4SJonathan Peyton // returns:  a pointer to the allocated kmp_task_t structure (task).
32113041982dSJonathan Peyton kmp_task_t *__kmp_task_dup_alloc(kmp_info_t *thread, kmp_task_t *task_src) {
32127cc577a4SJonathan Peyton   kmp_task_t *task;
32137cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata;
32147cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata_src;
32157cc577a4SJonathan Peyton   kmp_taskdata_t *parent_task = thread->th.th_current_task;
32167cc577a4SJonathan Peyton   size_t shareds_offset;
32177cc577a4SJonathan Peyton   size_t task_size;
32187cc577a4SJonathan Peyton 
32193041982dSJonathan Peyton   KA_TRACE(10, ("__kmp_task_dup_alloc(enter): Th %p, source task %p\n", thread,
32203041982dSJonathan Peyton                 task_src));
32217cc577a4SJonathan Peyton   taskdata_src = KMP_TASK_TO_TASKDATA(task_src);
32223041982dSJonathan Peyton   KMP_DEBUG_ASSERT(taskdata_src->td_flags.proxy ==
32233041982dSJonathan Peyton                    TASK_FULL); // it should not be proxy task
32247cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(taskdata_src->td_flags.tasktype == TASK_EXPLICIT);
32257cc577a4SJonathan Peyton   task_size = taskdata_src->td_size_alloc;
32267cc577a4SJonathan Peyton 
32277cc577a4SJonathan Peyton   // Allocate a kmp_taskdata_t block and a kmp_task_t block.
32283041982dSJonathan Peyton   KA_TRACE(30, ("__kmp_task_dup_alloc: Th %p, malloc size %ld\n", thread,
32293041982dSJonathan Peyton                 task_size));
32307cc577a4SJonathan Peyton #if USE_FAST_MEMORY
32317cc577a4SJonathan Peyton   taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, task_size);
32327cc577a4SJonathan Peyton #else
32337cc577a4SJonathan Peyton   taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, task_size);
32347cc577a4SJonathan Peyton #endif /* USE_FAST_MEMORY */
32357cc577a4SJonathan Peyton   KMP_MEMCPY(taskdata, taskdata_src, task_size);
32367cc577a4SJonathan Peyton 
32377cc577a4SJonathan Peyton   task = KMP_TASKDATA_TO_TASK(taskdata);
32387cc577a4SJonathan Peyton 
32397cc577a4SJonathan Peyton   // Initialize new task (only specific fields not affected by memcpy)
32407cc577a4SJonathan Peyton   taskdata->td_task_id = KMP_GEN_TASK_ID();
32417cc577a4SJonathan Peyton   if (task->shareds != NULL) { // need setup shareds pointer
32427cc577a4SJonathan Peyton     shareds_offset = (char *)task_src->shareds - (char *)taskdata_src;
32437cc577a4SJonathan Peyton     task->shareds = &((char *)taskdata)[shareds_offset];
32443041982dSJonathan Peyton     KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (sizeof(void *) - 1)) ==
32453041982dSJonathan Peyton                      0);
32467cc577a4SJonathan Peyton   }
32477cc577a4SJonathan Peyton   taskdata->td_alloc_thread = thread;
32483041982dSJonathan Peyton   taskdata->td_taskgroup =
32493041982dSJonathan Peyton       parent_task
32503041982dSJonathan Peyton           ->td_taskgroup; // task inherits the taskgroup from the parent task
32517cc577a4SJonathan Peyton 
32523041982dSJonathan Peyton   // Only need to keep track of child task counts if team parallel and tasking
32533041982dSJonathan Peyton   // not serialized
32547cc577a4SJonathan Peyton   if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) {
3255*c47afcd9SAndrey Churbanov     KMP_TEST_THEN_INC32(
3256*c47afcd9SAndrey Churbanov         CCAST(kmp_int32 *, &parent_task->td_incomplete_child_tasks));
32577cc577a4SJonathan Peyton     if (parent_task->td_taskgroup)
3258*c47afcd9SAndrey Churbanov       KMP_TEST_THEN_INC32(&parent_task->td_taskgroup->count);
32593041982dSJonathan Peyton     // Only need to keep track of allocated child tasks for explicit tasks since
32603041982dSJonathan Peyton     // implicit not deallocated
32617cc577a4SJonathan Peyton     if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT)
32623041982dSJonathan Peyton       KMP_TEST_THEN_INC32(
3263*c47afcd9SAndrey Churbanov           CCAST(kmp_int32 *, &taskdata->td_parent->td_allocated_child_tasks));
32647cc577a4SJonathan Peyton   }
32657cc577a4SJonathan Peyton 
32663041982dSJonathan Peyton   KA_TRACE(20,
32673041982dSJonathan Peyton            ("__kmp_task_dup_alloc(exit): Th %p, created task %p, parent=%p\n",
32687cc577a4SJonathan Peyton             thread, taskdata, taskdata->td_parent));
32697cc577a4SJonathan Peyton #if OMPT_SUPPORT
32703041982dSJonathan Peyton   __kmp_task_init_ompt(taskdata, thread->th.th_info.ds.ds_gtid,
32713041982dSJonathan Peyton                        (void *)task->routine);
32727cc577a4SJonathan Peyton #endif
32737cc577a4SJonathan Peyton   return task;
32747cc577a4SJonathan Peyton }
32757cc577a4SJonathan Peyton 
32767cc577a4SJonathan Peyton // Routine optionally generated by th ecompiler for setting the lastprivate flag
32777cc577a4SJonathan Peyton // and calling needed constructors for private/firstprivate objects
32787cc577a4SJonathan Peyton // (used to form taskloop tasks from pattern task)
32797cc577a4SJonathan Peyton typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32);
32807cc577a4SJonathan Peyton 
32817cc577a4SJonathan Peyton // __kmp_taskloop_linear: Start tasks of the taskloop linearly
32827cc577a4SJonathan Peyton //
32837cc577a4SJonathan Peyton // loc       Source location information
32847cc577a4SJonathan Peyton // gtid      Global thread ID
32857cc577a4SJonathan Peyton // task      Task with whole loop iteration range
32867cc577a4SJonathan Peyton // lb        Pointer to loop lower bound
32877cc577a4SJonathan Peyton // ub        Pointer to loop upper bound
32887cc577a4SJonathan Peyton // st        Loop stride
32897cc577a4SJonathan Peyton // sched     Schedule specified 0/1/2 for none/grainsize/num_tasks
32907cc577a4SJonathan Peyton // grainsize Schedule value if specified
32917cc577a4SJonathan Peyton // task_dup  Tasks duplication routine
32923041982dSJonathan Peyton void __kmp_taskloop_linear(ident_t *loc, int gtid, kmp_task_t *task,
32937cc577a4SJonathan Peyton                            kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
32943041982dSJonathan Peyton                            int sched, kmp_uint64 grainsize, void *task_dup) {
32957cc577a4SJonathan Peyton   KMP_COUNT_BLOCK(OMP_TASKLOOP);
32967cc577a4SJonathan Peyton   KMP_TIME_PARTITIONED_BLOCK(OMP_taskloop_scheduling);
32977cc577a4SJonathan Peyton   p_task_dup_t ptask_dup = (p_task_dup_t)task_dup;
32987cc577a4SJonathan Peyton   kmp_uint64 tc;
32997cc577a4SJonathan Peyton   kmp_uint64 lower = *lb; // compiler provides global bounds here
33007cc577a4SJonathan Peyton   kmp_uint64 upper = *ub;
33017cc577a4SJonathan Peyton   kmp_uint64 i, num_tasks = 0, extras = 0;
33027cc577a4SJonathan Peyton   kmp_info_t *thread = __kmp_threads[gtid];
33037cc577a4SJonathan Peyton   kmp_taskdata_t *current_task = thread->th.th_current_task;
33047cc577a4SJonathan Peyton   kmp_task_t *next_task;
33057cc577a4SJonathan Peyton   kmp_int32 lastpriv = 0;
33063041982dSJonathan Peyton   size_t lower_offset =
33073041982dSJonathan Peyton       (char *)lb - (char *)task; // remember offset of lb in the task structure
33083041982dSJonathan Peyton   size_t upper_offset =
33093041982dSJonathan Peyton       (char *)ub - (char *)task; // remember offset of ub in the task structure
33107cc577a4SJonathan Peyton 
33117cc577a4SJonathan Peyton   // compute trip count
33127cc577a4SJonathan Peyton   if (st == 1) { // most common case
33137cc577a4SJonathan Peyton     tc = upper - lower + 1;
33147cc577a4SJonathan Peyton   } else if (st < 0) {
33157cc577a4SJonathan Peyton     tc = (lower - upper) / (-st) + 1;
33167cc577a4SJonathan Peyton   } else { // st > 0
33177cc577a4SJonathan Peyton     tc = (upper - lower) / st + 1;
33187cc577a4SJonathan Peyton   }
33197cc577a4SJonathan Peyton   if (tc == 0) {
33207cc577a4SJonathan Peyton     KA_TRACE(20, ("__kmpc_taskloop(exit): T#%d zero-trip loop\n", gtid));
33217cc577a4SJonathan Peyton     // free the pattern task and exit
33227cc577a4SJonathan Peyton     __kmp_task_start(gtid, task, current_task);
33237cc577a4SJonathan Peyton     // do not execute anything for zero-trip loop
33247cc577a4SJonathan Peyton     __kmp_task_finish(gtid, task, current_task);
33257cc577a4SJonathan Peyton     return;
33267cc577a4SJonathan Peyton   }
33277cc577a4SJonathan Peyton 
33287cc577a4SJonathan Peyton   // compute num_tasks/grainsize based on the input provided
33297cc577a4SJonathan Peyton   switch (sched) {
33307cc577a4SJonathan Peyton   case 0: // no schedule clause specified, we can choose the default
33317cc577a4SJonathan Peyton     // let's try to schedule (team_size*10) tasks
33327cc577a4SJonathan Peyton     grainsize = thread->th.th_team_nproc * 10;
33337cc577a4SJonathan Peyton   case 2: // num_tasks provided
33347cc577a4SJonathan Peyton     if (grainsize > tc) {
33357cc577a4SJonathan Peyton       num_tasks = tc; // too big num_tasks requested, adjust values
33367cc577a4SJonathan Peyton       grainsize = 1;
33377cc577a4SJonathan Peyton       extras = 0;
33387cc577a4SJonathan Peyton     } else {
33397cc577a4SJonathan Peyton       num_tasks = grainsize;
33407cc577a4SJonathan Peyton       grainsize = tc / num_tasks;
33417cc577a4SJonathan Peyton       extras = tc % num_tasks;
33427cc577a4SJonathan Peyton     }
33437cc577a4SJonathan Peyton     break;
33447cc577a4SJonathan Peyton   case 1: // grainsize provided
33457cc577a4SJonathan Peyton     if (grainsize > tc) {
33467cc577a4SJonathan Peyton       num_tasks = 1; // too big grainsize requested, adjust values
33477cc577a4SJonathan Peyton       grainsize = tc;
33487cc577a4SJonathan Peyton       extras = 0;
33497cc577a4SJonathan Peyton     } else {
33507cc577a4SJonathan Peyton       num_tasks = tc / grainsize;
33513041982dSJonathan Peyton       grainsize =
33523041982dSJonathan Peyton           tc /
33533041982dSJonathan Peyton           num_tasks; // adjust grainsize for balanced distribution of iterations
33547cc577a4SJonathan Peyton       extras = tc % num_tasks;
33557cc577a4SJonathan Peyton     }
33567cc577a4SJonathan Peyton     break;
33577cc577a4SJonathan Peyton   default:
33587cc577a4SJonathan Peyton     KMP_ASSERT2(0, "unknown scheduling of taskloop");
33597cc577a4SJonathan Peyton   }
33607cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras);
33617cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks > extras);
33627cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(num_tasks > 0);
33633041982dSJonathan Peyton   KA_TRACE(20, ("__kmpc_taskloop: T#%d will launch: num_tasks %lld, grainsize "
33643041982dSJonathan Peyton                 "%lld, extras %lld\n",
33657cc577a4SJonathan Peyton                 gtid, num_tasks, grainsize, extras));
33667cc577a4SJonathan Peyton 
33677cc577a4SJonathan Peyton   // Main loop, launch num_tasks tasks, assign grainsize iterations each task
33687cc577a4SJonathan Peyton   for (i = 0; i < num_tasks; ++i) {
33697cc577a4SJonathan Peyton     kmp_uint64 chunk_minus_1;
33707cc577a4SJonathan Peyton     if (extras == 0) {
33717cc577a4SJonathan Peyton       chunk_minus_1 = grainsize - 1;
33727cc577a4SJonathan Peyton     } else {
33737cc577a4SJonathan Peyton       chunk_minus_1 = grainsize;
33747cc577a4SJonathan Peyton       --extras; // first extras iterations get bigger chunk (grainsize+1)
33757cc577a4SJonathan Peyton     }
33767cc577a4SJonathan Peyton     upper = lower + st * chunk_minus_1;
33777cc577a4SJonathan Peyton     if (i == num_tasks - 1) {
33787cc577a4SJonathan Peyton       // schedule the last task, set lastprivate flag
33797cc577a4SJonathan Peyton       lastpriv = 1;
33807cc577a4SJonathan Peyton #if KMP_DEBUG
33817cc577a4SJonathan Peyton       if (st == 1)
33827cc577a4SJonathan Peyton         KMP_DEBUG_ASSERT(upper == *ub);
33837cc577a4SJonathan Peyton       else if (st > 0)
33847cc577a4SJonathan Peyton         KMP_DEBUG_ASSERT(upper + st > *ub);
33857cc577a4SJonathan Peyton       else
33867cc577a4SJonathan Peyton         KMP_DEBUG_ASSERT(upper + st < *ub);
33877cc577a4SJonathan Peyton #endif
33887cc577a4SJonathan Peyton     }
33897cc577a4SJonathan Peyton     next_task = __kmp_task_dup_alloc(thread, task); // allocate new task
33903041982dSJonathan Peyton     *(kmp_uint64 *)((char *)next_task + lower_offset) =
33913041982dSJonathan Peyton         lower; // adjust task-specific bounds
33927cc577a4SJonathan Peyton     *(kmp_uint64 *)((char *)next_task + upper_offset) = upper;
33937cc577a4SJonathan Peyton     if (ptask_dup != NULL)
33943041982dSJonathan Peyton       ptask_dup(next_task, task,
33953041982dSJonathan Peyton                 lastpriv); // set lastprivate flag, construct fistprivates, etc.
33963041982dSJonathan Peyton     KA_TRACE(20, ("__kmpc_taskloop: T#%d schedule task %p: lower %lld, upper "
33973041982dSJonathan Peyton                   "%lld (offsets %p %p)\n",
33987cc577a4SJonathan Peyton                   gtid, next_task, lower, upper, lower_offset, upper_offset));
33997cc577a4SJonathan Peyton     __kmp_omp_task(gtid, next_task, true); // schedule new task
34007cc577a4SJonathan Peyton     lower = upper + st; // adjust lower bound for the next iteration
34017cc577a4SJonathan Peyton   }
34027cc577a4SJonathan Peyton   // free the pattern task and exit
34037cc577a4SJonathan Peyton   __kmp_task_start(gtid, task, current_task);
34047cc577a4SJonathan Peyton   // do not execute the pattern task, just do bookkeeping
34057cc577a4SJonathan Peyton   __kmp_task_finish(gtid, task, current_task);
34067cc577a4SJonathan Peyton }
34077cc577a4SJonathan Peyton 
34087cc577a4SJonathan Peyton /*!
34097cc577a4SJonathan Peyton @ingroup TASKING
34107cc577a4SJonathan Peyton @param loc       Source location information
34117cc577a4SJonathan Peyton @param gtid      Global thread ID
34127cc577a4SJonathan Peyton @param task      Task structure
34137cc577a4SJonathan Peyton @param if_val    Value of the if clause
34147cc577a4SJonathan Peyton @param lb        Pointer to loop lower bound
34157cc577a4SJonathan Peyton @param ub        Pointer to loop upper bound
34167cc577a4SJonathan Peyton @param st        Loop stride
34177cc577a4SJonathan Peyton @param nogroup   Flag, 1 if nogroup clause specified, 0 otherwise
34187cc577a4SJonathan Peyton @param sched     Schedule specified 0/1/2 for none/grainsize/num_tasks
34197cc577a4SJonathan Peyton @param grainsize Schedule value if specified
34207cc577a4SJonathan Peyton @param task_dup  Tasks duplication routine
34217cc577a4SJonathan Peyton 
34227cc577a4SJonathan Peyton Execute the taskloop construct.
34237cc577a4SJonathan Peyton */
34243041982dSJonathan Peyton void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int if_val,
34253041982dSJonathan Peyton                      kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup,
34263041982dSJonathan Peyton                      int sched, kmp_uint64 grainsize, void *task_dup) {
34277cc577a4SJonathan Peyton   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
34287cc577a4SJonathan Peyton   KMP_DEBUG_ASSERT(task != NULL);
34297cc577a4SJonathan Peyton 
34303041982dSJonathan Peyton   KA_TRACE(10, ("__kmpc_taskloop(enter): T#%d, pattern task %p, lb %lld ub "
34313041982dSJonathan Peyton                 "%lld st %lld, grain %llu(%d)\n",
34327cc577a4SJonathan Peyton                 gtid, taskdata, *lb, *ub, st, grainsize, sched));
34337cc577a4SJonathan Peyton 
34347cc577a4SJonathan Peyton   // check if clause value first
34357cc577a4SJonathan Peyton   if (if_val == 0) { // if(0) specified, mark task as serial
34367cc577a4SJonathan Peyton     taskdata->td_flags.task_serial = 1;
34377cc577a4SJonathan Peyton     taskdata->td_flags.tiedness = TASK_TIED; // AC: serial task cannot be untied
34387cc577a4SJonathan Peyton   }
34397cc577a4SJonathan Peyton   if (nogroup == 0) {
34407cc577a4SJonathan Peyton     __kmpc_taskgroup(loc, gtid);
34417cc577a4SJonathan Peyton   }
34427cc577a4SJonathan Peyton 
34437cc577a4SJonathan Peyton   if (1 /* AC: use some heuristic here to choose task scheduling method */) {
34443041982dSJonathan Peyton     __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, sched, grainsize,
34453041982dSJonathan Peyton                           task_dup);
34467cc577a4SJonathan Peyton   }
34477cc577a4SJonathan Peyton 
34487cc577a4SJonathan Peyton   if (nogroup == 0) {
34497cc577a4SJonathan Peyton     __kmpc_end_taskgroup(loc, gtid);
34507cc577a4SJonathan Peyton   }
34517cc577a4SJonathan Peyton   KA_TRACE(10, ("__kmpc_taskloop(exit): T#%d\n", gtid));
34527cc577a4SJonathan Peyton }
34537cc577a4SJonathan Peyton 
34547cc577a4SJonathan Peyton #endif
3455