17cc577a4SJonathan Peyton /* 2de4749b7SJonathan Peyton * kmp_tasking.cpp -- OpenMP 3.0 tasking support. 37cc577a4SJonathan Peyton */ 47cc577a4SJonathan Peyton 57cc577a4SJonathan Peyton //===----------------------------------------------------------------------===// 67cc577a4SJonathan Peyton // 77cc577a4SJonathan Peyton // The LLVM Compiler Infrastructure 87cc577a4SJonathan Peyton // 97cc577a4SJonathan Peyton // This file is dual licensed under the MIT and the University of Illinois Open 107cc577a4SJonathan Peyton // Source Licenses. See LICENSE.txt for details. 117cc577a4SJonathan Peyton // 127cc577a4SJonathan Peyton //===----------------------------------------------------------------------===// 137cc577a4SJonathan Peyton 147cc577a4SJonathan Peyton #include "kmp.h" 157cc577a4SJonathan Peyton #include "kmp_i18n.h" 167cc577a4SJonathan Peyton #include "kmp_itt.h" 177cc577a4SJonathan Peyton #include "kmp_stats.h" 183041982dSJonathan Peyton #include "kmp_wait_release.h" 197cc577a4SJonathan Peyton 207cc577a4SJonathan Peyton #if OMPT_SUPPORT 217cc577a4SJonathan Peyton #include "ompt-specific.h" 227cc577a4SJonathan Peyton #endif 237cc577a4SJonathan Peyton 247cc577a4SJonathan Peyton #include "tsan_annotations.h" 257cc577a4SJonathan Peyton 267cc577a4SJonathan Peyton /* forward declaration */ 273041982dSJonathan Peyton static void __kmp_enable_tasking(kmp_task_team_t *task_team, 283041982dSJonathan Peyton kmp_info_t *this_thr); 293041982dSJonathan Peyton static void __kmp_alloc_task_deque(kmp_info_t *thread, 303041982dSJonathan Peyton kmp_thread_data_t *thread_data); 313041982dSJonathan Peyton static int __kmp_realloc_task_threads_data(kmp_info_t *thread, 323041982dSJonathan Peyton kmp_task_team_t *task_team); 337cc577a4SJonathan Peyton 347cc577a4SJonathan Peyton #ifdef OMP_45_ENABLED 357cc577a4SJonathan Peyton static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask); 367cc577a4SJonathan Peyton #endif 377cc577a4SJonathan Peyton 387cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK 397cc577a4SJonathan Peyton 407cc577a4SJonathan Peyton // __kmp_trace_task_stack: print the tied tasks from the task stack in order 417cc577a4SJonathan Peyton // from top do bottom 427cc577a4SJonathan Peyton // 437cc577a4SJonathan Peyton // gtid: global thread identifier for thread containing stack 447cc577a4SJonathan Peyton // thread_data: thread data for task team thread containing stack 457cc577a4SJonathan Peyton // threshold: value above which the trace statement triggers 467cc577a4SJonathan Peyton // location: string identifying call site of this function (for trace) 473041982dSJonathan Peyton static void __kmp_trace_task_stack(kmp_int32 gtid, 483041982dSJonathan Peyton kmp_thread_data_t *thread_data, 493041982dSJonathan Peyton int threshold, char *location) { 507cc577a4SJonathan Peyton kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks; 517cc577a4SJonathan Peyton kmp_taskdata_t **stack_top = task_stack->ts_top; 527cc577a4SJonathan Peyton kmp_int32 entries = task_stack->ts_entries; 537cc577a4SJonathan Peyton kmp_taskdata_t *tied_task; 547cc577a4SJonathan Peyton 553041982dSJonathan Peyton KA_TRACE( 563041982dSJonathan Peyton threshold, 573041982dSJonathan Peyton ("__kmp_trace_task_stack(start): location = %s, gtid = %d, entries = %d, " 587cc577a4SJonathan Peyton "first_block = %p, stack_top = %p \n", 597cc577a4SJonathan Peyton location, gtid, entries, task_stack->ts_first_block, stack_top)); 607cc577a4SJonathan Peyton 617cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(stack_top != NULL); 627cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(entries > 0); 637cc577a4SJonathan Peyton 643041982dSJonathan Peyton while (entries != 0) { 657cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(stack_top != &task_stack->ts_first_block.sb_block[0]); 667cc577a4SJonathan Peyton // fix up ts_top if we need to pop from previous block 673041982dSJonathan Peyton if (entries & TASK_STACK_INDEX_MASK == 0) { 687cc577a4SJonathan Peyton kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(stack_top); 697cc577a4SJonathan Peyton 707cc577a4SJonathan Peyton stack_block = stack_block->sb_prev; 717cc577a4SJonathan Peyton stack_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE]; 727cc577a4SJonathan Peyton } 737cc577a4SJonathan Peyton 747cc577a4SJonathan Peyton // finish bookkeeping 757cc577a4SJonathan Peyton stack_top--; 767cc577a4SJonathan Peyton entries--; 777cc577a4SJonathan Peyton 787cc577a4SJonathan Peyton tied_task = *stack_top; 797cc577a4SJonathan Peyton 807cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(tied_task != NULL); 817cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED); 827cc577a4SJonathan Peyton 833041982dSJonathan Peyton KA_TRACE(threshold, 843041982dSJonathan Peyton ("__kmp_trace_task_stack(%s): gtid=%d, entry=%d, " 857cc577a4SJonathan Peyton "stack_top=%p, tied_task=%p\n", 867cc577a4SJonathan Peyton location, gtid, entries, stack_top, tied_task)); 877cc577a4SJonathan Peyton } 887cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(stack_top == &task_stack->ts_first_block.sb_block[0]); 897cc577a4SJonathan Peyton 903041982dSJonathan Peyton KA_TRACE(threshold, 913041982dSJonathan Peyton ("__kmp_trace_task_stack(exit): location = %s, gtid = %d\n", 927cc577a4SJonathan Peyton location, gtid)); 937cc577a4SJonathan Peyton } 947cc577a4SJonathan Peyton 957cc577a4SJonathan Peyton // __kmp_init_task_stack: initialize the task stack for the first time 967cc577a4SJonathan Peyton // after a thread_data structure is created. 977cc577a4SJonathan Peyton // It should not be necessary to do this again (assuming the stack works). 987cc577a4SJonathan Peyton // 997cc577a4SJonathan Peyton // gtid: global thread identifier of calling thread 1007cc577a4SJonathan Peyton // thread_data: thread data for task team thread containing stack 1013041982dSJonathan Peyton static void __kmp_init_task_stack(kmp_int32 gtid, 1023041982dSJonathan Peyton kmp_thread_data_t *thread_data) { 1037cc577a4SJonathan Peyton kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks; 1047cc577a4SJonathan Peyton kmp_stack_block_t *first_block; 1057cc577a4SJonathan Peyton 1067cc577a4SJonathan Peyton // set up the first block of the stack 1077cc577a4SJonathan Peyton first_block = &task_stack->ts_first_block; 1087cc577a4SJonathan Peyton task_stack->ts_top = (kmp_taskdata_t **)first_block; 1093041982dSJonathan Peyton memset((void *)first_block, '\0', 1103041982dSJonathan Peyton TASK_STACK_BLOCK_SIZE * sizeof(kmp_taskdata_t *)); 1117cc577a4SJonathan Peyton 1127cc577a4SJonathan Peyton // initialize the stack to be empty 1137cc577a4SJonathan Peyton task_stack->ts_entries = TASK_STACK_EMPTY; 1147cc577a4SJonathan Peyton first_block->sb_next = NULL; 1157cc577a4SJonathan Peyton first_block->sb_prev = NULL; 1167cc577a4SJonathan Peyton } 1177cc577a4SJonathan Peyton 1187cc577a4SJonathan Peyton // __kmp_free_task_stack: free the task stack when thread_data is destroyed. 1197cc577a4SJonathan Peyton // 1207cc577a4SJonathan Peyton // gtid: global thread identifier for calling thread 1217cc577a4SJonathan Peyton // thread_data: thread info for thread containing stack 1223041982dSJonathan Peyton static void __kmp_free_task_stack(kmp_int32 gtid, 1233041982dSJonathan Peyton kmp_thread_data_t *thread_data) { 1247cc577a4SJonathan Peyton kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks; 1257cc577a4SJonathan Peyton kmp_stack_block_t *stack_block = &task_stack->ts_first_block; 1267cc577a4SJonathan Peyton 1277cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(task_stack->ts_entries == TASK_STACK_EMPTY); 1287cc577a4SJonathan Peyton // free from the second block of the stack 1297cc577a4SJonathan Peyton while (stack_block != NULL) { 1307cc577a4SJonathan Peyton kmp_stack_block_t *next_block = (stack_block) ? stack_block->sb_next : NULL; 1317cc577a4SJonathan Peyton 1327cc577a4SJonathan Peyton stack_block->sb_next = NULL; 1337cc577a4SJonathan Peyton stack_block->sb_prev = NULL; 1347cc577a4SJonathan Peyton if (stack_block != &task_stack->ts_first_block) { 1353041982dSJonathan Peyton __kmp_thread_free(thread, 1363041982dSJonathan Peyton stack_block); // free the block, if not the first 1377cc577a4SJonathan Peyton } 1387cc577a4SJonathan Peyton stack_block = next_block; 1397cc577a4SJonathan Peyton } 1407cc577a4SJonathan Peyton // initialize the stack to be empty 1417cc577a4SJonathan Peyton task_stack->ts_entries = 0; 1427cc577a4SJonathan Peyton task_stack->ts_top = NULL; 1437cc577a4SJonathan Peyton } 1447cc577a4SJonathan Peyton 1457cc577a4SJonathan Peyton // __kmp_push_task_stack: Push the tied task onto the task stack. 1467cc577a4SJonathan Peyton // Grow the stack if necessary by allocating another block. 1477cc577a4SJonathan Peyton // 1487cc577a4SJonathan Peyton // gtid: global thread identifier for calling thread 1497cc577a4SJonathan Peyton // thread: thread info for thread containing stack 1507cc577a4SJonathan Peyton // tied_task: the task to push on the stack 1513041982dSJonathan Peyton static void __kmp_push_task_stack(kmp_int32 gtid, kmp_info_t *thread, 1523041982dSJonathan Peyton kmp_taskdata_t *tied_task) { 1537cc577a4SJonathan Peyton // GEH - need to consider what to do if tt_threads_data not allocated yet 1543041982dSJonathan Peyton kmp_thread_data_t *thread_data = 1553041982dSJonathan Peyton &thread->th.th_task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)]; 1567cc577a4SJonathan Peyton kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks; 1577cc577a4SJonathan Peyton 1587cc577a4SJonathan Peyton if (tied_task->td_flags.team_serial || tied_task->td_flags.tasking_ser) { 1597cc577a4SJonathan Peyton return; // Don't push anything on stack if team or team tasks are serialized 1607cc577a4SJonathan Peyton } 1617cc577a4SJonathan Peyton 1627cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED); 1637cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(task_stack->ts_top != NULL); 1647cc577a4SJonathan Peyton 1653041982dSJonathan Peyton KA_TRACE(20, 1663041982dSJonathan Peyton ("__kmp_push_task_stack(enter): GTID: %d; THREAD: %p; TASK: %p\n", 1677cc577a4SJonathan Peyton gtid, thread, tied_task)); 1687cc577a4SJonathan Peyton // Store entry 1697cc577a4SJonathan Peyton *(task_stack->ts_top) = tied_task; 1707cc577a4SJonathan Peyton 1717cc577a4SJonathan Peyton // Do bookkeeping for next push 1727cc577a4SJonathan Peyton task_stack->ts_top++; 1737cc577a4SJonathan Peyton task_stack->ts_entries++; 1747cc577a4SJonathan Peyton 1753041982dSJonathan Peyton if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) { 1767cc577a4SJonathan Peyton // Find beginning of this task block 1777cc577a4SJonathan Peyton kmp_stack_block_t *stack_block = 1787cc577a4SJonathan Peyton (kmp_stack_block_t *)(task_stack->ts_top - TASK_STACK_BLOCK_SIZE); 1797cc577a4SJonathan Peyton 1807cc577a4SJonathan Peyton // Check if we already have a block 1813041982dSJonathan Peyton if (stack_block->sb_next != 1823041982dSJonathan Peyton NULL) { // reset ts_top to beginning of next block 1837cc577a4SJonathan Peyton task_stack->ts_top = &stack_block->sb_next->sb_block[0]; 1843041982dSJonathan Peyton } else { // Alloc new block and link it up 1853041982dSJonathan Peyton kmp_stack_block_t *new_block = (kmp_stack_block_t *)__kmp_thread_calloc( 1863041982dSJonathan Peyton thread, sizeof(kmp_stack_block_t)); 1877cc577a4SJonathan Peyton 1887cc577a4SJonathan Peyton task_stack->ts_top = &new_block->sb_block[0]; 1897cc577a4SJonathan Peyton stack_block->sb_next = new_block; 1907cc577a4SJonathan Peyton new_block->sb_prev = stack_block; 1917cc577a4SJonathan Peyton new_block->sb_next = NULL; 1927cc577a4SJonathan Peyton 1933041982dSJonathan Peyton KA_TRACE( 1943041982dSJonathan Peyton 30, 1953041982dSJonathan Peyton ("__kmp_push_task_stack(): GTID: %d; TASK: %p; Alloc new block: %p\n", 1967cc577a4SJonathan Peyton gtid, tied_task, new_block)); 1977cc577a4SJonathan Peyton } 1987cc577a4SJonathan Peyton } 1993041982dSJonathan Peyton KA_TRACE(20, ("__kmp_push_task_stack(exit): GTID: %d; TASK: %p\n", gtid, 2003041982dSJonathan Peyton tied_task)); 2017cc577a4SJonathan Peyton } 2027cc577a4SJonathan Peyton 2037cc577a4SJonathan Peyton // __kmp_pop_task_stack: Pop the tied task from the task stack. Don't return 2047cc577a4SJonathan Peyton // the task, just check to make sure it matches the ending task passed in. 2057cc577a4SJonathan Peyton // 2067cc577a4SJonathan Peyton // gtid: global thread identifier for the calling thread 2077cc577a4SJonathan Peyton // thread: thread info structure containing stack 2087cc577a4SJonathan Peyton // tied_task: the task popped off the stack 2097cc577a4SJonathan Peyton // ending_task: the task that is ending (should match popped task) 2103041982dSJonathan Peyton static void __kmp_pop_task_stack(kmp_int32 gtid, kmp_info_t *thread, 2113041982dSJonathan Peyton kmp_taskdata_t *ending_task) { 2127cc577a4SJonathan Peyton // GEH - need to consider what to do if tt_threads_data not allocated yet 2133041982dSJonathan Peyton kmp_thread_data_t *thread_data = 2143041982dSJonathan Peyton &thread->th.th_task_team->tt_threads_data[__kmp_tid_from_gtid(gtid)]; 2157cc577a4SJonathan Peyton kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks; 2167cc577a4SJonathan Peyton kmp_taskdata_t *tied_task; 2177cc577a4SJonathan Peyton 2187cc577a4SJonathan Peyton if (ending_task->td_flags.team_serial || ending_task->td_flags.tasking_ser) { 2193041982dSJonathan Peyton // Don't pop anything from stack if team or team tasks are serialized 2203041982dSJonathan Peyton return; 2217cc577a4SJonathan Peyton } 2227cc577a4SJonathan Peyton 2237cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(task_stack->ts_top != NULL); 2247cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(task_stack->ts_entries > 0); 2257cc577a4SJonathan Peyton 2263041982dSJonathan Peyton KA_TRACE(20, ("__kmp_pop_task_stack(enter): GTID: %d; THREAD: %p\n", gtid, 2273041982dSJonathan Peyton thread)); 2287cc577a4SJonathan Peyton 2297cc577a4SJonathan Peyton // fix up ts_top if we need to pop from previous block 2303041982dSJonathan Peyton if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) { 2313041982dSJonathan Peyton kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(task_stack->ts_top); 2327cc577a4SJonathan Peyton 2337cc577a4SJonathan Peyton stack_block = stack_block->sb_prev; 2347cc577a4SJonathan Peyton task_stack->ts_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE]; 2357cc577a4SJonathan Peyton } 2367cc577a4SJonathan Peyton 2377cc577a4SJonathan Peyton // finish bookkeeping 2387cc577a4SJonathan Peyton task_stack->ts_top--; 2397cc577a4SJonathan Peyton task_stack->ts_entries--; 2407cc577a4SJonathan Peyton 2417cc577a4SJonathan Peyton tied_task = *(task_stack->ts_top); 2427cc577a4SJonathan Peyton 2437cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(tied_task != NULL); 2447cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED); 2457cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(tied_task == ending_task); // If we built the stack correctly 2467cc577a4SJonathan Peyton 2473041982dSJonathan Peyton KA_TRACE(20, ("__kmp_pop_task_stack(exit): GTID: %d; TASK: %p\n", gtid, 2483041982dSJonathan Peyton tied_task)); 2497cc577a4SJonathan Peyton return; 2507cc577a4SJonathan Peyton } 2517cc577a4SJonathan Peyton #endif /* BUILD_TIED_TASK_STACK */ 2527cc577a4SJonathan Peyton 2537cc577a4SJonathan Peyton // __kmp_push_task: Add a task to the thread's deque 2543041982dSJonathan Peyton static kmp_int32 __kmp_push_task(kmp_int32 gtid, kmp_task_t *task) { 2557cc577a4SJonathan Peyton kmp_info_t *thread = __kmp_threads[gtid]; 2567cc577a4SJonathan Peyton kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task); 2577cc577a4SJonathan Peyton kmp_task_team_t *task_team = thread->th.th_task_team; 2587cc577a4SJonathan Peyton kmp_int32 tid = __kmp_tid_from_gtid(gtid); 2597cc577a4SJonathan Peyton kmp_thread_data_t *thread_data; 2607cc577a4SJonathan Peyton 2613041982dSJonathan Peyton KA_TRACE(20, 2623041982dSJonathan Peyton ("__kmp_push_task: T#%d trying to push task %p.\n", gtid, taskdata)); 2637cc577a4SJonathan Peyton 2647cc577a4SJonathan Peyton if (taskdata->td_flags.tiedness == TASK_UNTIED) { 2653041982dSJonathan Peyton // untied task needs to increment counter so that the task structure is not 2663041982dSJonathan Peyton // freed prematurely 2677cc577a4SJonathan Peyton kmp_int32 counter = 1 + KMP_TEST_THEN_INC32(&taskdata->td_untied_count); 2683041982dSJonathan Peyton KA_TRACE( 2693041982dSJonathan Peyton 20, 2703041982dSJonathan Peyton ("__kmp_push_task: T#%d untied_count (%d) incremented for task %p\n", 2717cc577a4SJonathan Peyton gtid, counter, taskdata)); 2727cc577a4SJonathan Peyton } 2737cc577a4SJonathan Peyton 2747cc577a4SJonathan Peyton // The first check avoids building task_team thread data if serialized 2757cc577a4SJonathan Peyton if (taskdata->td_flags.task_serial) { 2763041982dSJonathan Peyton KA_TRACE(20, ("__kmp_push_task: T#%d team serialized; returning " 2773041982dSJonathan Peyton "TASK_NOT_PUSHED for task %p\n", 2787cc577a4SJonathan Peyton gtid, taskdata)); 2797cc577a4SJonathan Peyton return TASK_NOT_PUSHED; 2807cc577a4SJonathan Peyton } 2817cc577a4SJonathan Peyton 2823041982dSJonathan Peyton // Now that serialized tasks have returned, we can assume that we are not in 2833041982dSJonathan Peyton // immediate exec mode 2847cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec); 2857cc577a4SJonathan Peyton if (!KMP_TASKING_ENABLED(task_team)) { 2867cc577a4SJonathan Peyton __kmp_enable_tasking(task_team, thread); 2877cc577a4SJonathan Peyton } 2887cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(TCR_4(task_team->tt.tt_found_tasks) == TRUE); 2897cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(TCR_PTR(task_team->tt.tt_threads_data) != NULL); 2907cc577a4SJonathan Peyton 2917cc577a4SJonathan Peyton // Find tasking deque specific to encountering thread 2927cc577a4SJonathan Peyton thread_data = &task_team->tt.tt_threads_data[tid]; 2937cc577a4SJonathan Peyton 2947cc577a4SJonathan Peyton // No lock needed since only owner can allocate 2957cc577a4SJonathan Peyton if (thread_data->td.td_deque == NULL) { 2967cc577a4SJonathan Peyton __kmp_alloc_task_deque(thread, thread_data); 2977cc577a4SJonathan Peyton } 2987cc577a4SJonathan Peyton 2997cc577a4SJonathan Peyton // Check if deque is full 3003041982dSJonathan Peyton if (TCR_4(thread_data->td.td_deque_ntasks) >= 3013041982dSJonathan Peyton TASK_DEQUE_SIZE(thread_data->td)) { 3023041982dSJonathan Peyton KA_TRACE(20, ("__kmp_push_task: T#%d deque is full; returning " 3033041982dSJonathan Peyton "TASK_NOT_PUSHED for task %p\n", 3047cc577a4SJonathan Peyton gtid, taskdata)); 3057cc577a4SJonathan Peyton return TASK_NOT_PUSHED; 3067cc577a4SJonathan Peyton } 3077cc577a4SJonathan Peyton 3087cc577a4SJonathan Peyton // Lock the deque for the task push operation 3097cc577a4SJonathan Peyton __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock); 3107cc577a4SJonathan Peyton 3117cc577a4SJonathan Peyton #if OMP_45_ENABLED 3127cc577a4SJonathan Peyton // Need to recheck as we can get a proxy task from a thread outside of OpenMP 3133041982dSJonathan Peyton if (TCR_4(thread_data->td.td_deque_ntasks) >= 3143041982dSJonathan Peyton TASK_DEQUE_SIZE(thread_data->td)) { 3157cc577a4SJonathan Peyton __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock); 3163041982dSJonathan Peyton KA_TRACE(20, ("__kmp_push_task: T#%d deque is full on 2nd check; returning " 3173041982dSJonathan Peyton "TASK_NOT_PUSHED for task %p\n", 3187cc577a4SJonathan Peyton gtid, taskdata)); 3197cc577a4SJonathan Peyton return TASK_NOT_PUSHED; 3207cc577a4SJonathan Peyton } 3217cc577a4SJonathan Peyton #else 3227cc577a4SJonathan Peyton // Must have room since no thread can add tasks but calling thread 3233041982dSJonathan Peyton KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) < 3243041982dSJonathan Peyton TASK_DEQUE_SIZE(thread_data->td)); 3257cc577a4SJonathan Peyton #endif 3267cc577a4SJonathan Peyton 3273041982dSJonathan Peyton thread_data->td.td_deque[thread_data->td.td_deque_tail] = 3283041982dSJonathan Peyton taskdata; // Push taskdata 3297cc577a4SJonathan Peyton // Wrap index. 3303041982dSJonathan Peyton thread_data->td.td_deque_tail = 3313041982dSJonathan Peyton (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td); 3323041982dSJonathan Peyton TCW_4(thread_data->td.td_deque_ntasks, 3333041982dSJonathan Peyton TCR_4(thread_data->td.td_deque_ntasks) + 1); // Adjust task count 3347cc577a4SJonathan Peyton 3357cc577a4SJonathan Peyton KA_TRACE(20, ("__kmp_push_task: T#%d returning TASK_SUCCESSFULLY_PUSHED: " 3367cc577a4SJonathan Peyton "task=%p ntasks=%d head=%u tail=%u\n", 3377cc577a4SJonathan Peyton gtid, taskdata, thread_data->td.td_deque_ntasks, 3387cc577a4SJonathan Peyton thread_data->td.td_deque_head, thread_data->td.td_deque_tail)); 3397cc577a4SJonathan Peyton 3407cc577a4SJonathan Peyton __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock); 3417cc577a4SJonathan Peyton 3427cc577a4SJonathan Peyton return TASK_SUCCESSFULLY_PUSHED; 3437cc577a4SJonathan Peyton } 3447cc577a4SJonathan Peyton 3453041982dSJonathan Peyton // __kmp_pop_current_task_from_thread: set up current task from called thread 3463041982dSJonathan Peyton // when team ends 3473041982dSJonathan Peyton // 3487cc577a4SJonathan Peyton // this_thr: thread structure to set current_task in. 3493041982dSJonathan Peyton void __kmp_pop_current_task_from_thread(kmp_info_t *this_thr) { 3503041982dSJonathan Peyton KF_TRACE(10, ("__kmp_pop_current_task_from_thread(enter): T#%d " 3513041982dSJonathan Peyton "this_thread=%p, curtask=%p, " 3527cc577a4SJonathan Peyton "curtask_parent=%p\n", 3537cc577a4SJonathan Peyton 0, this_thr, this_thr->th.th_current_task, 3547cc577a4SJonathan Peyton this_thr->th.th_current_task->td_parent)); 3557cc577a4SJonathan Peyton 3567cc577a4SJonathan Peyton this_thr->th.th_current_task = this_thr->th.th_current_task->td_parent; 3577cc577a4SJonathan Peyton 3583041982dSJonathan Peyton KF_TRACE(10, ("__kmp_pop_current_task_from_thread(exit): T#%d " 3593041982dSJonathan Peyton "this_thread=%p, curtask=%p, " 3607cc577a4SJonathan Peyton "curtask_parent=%p\n", 3617cc577a4SJonathan Peyton 0, this_thr, this_thr->th.th_current_task, 3627cc577a4SJonathan Peyton this_thr->th.th_current_task->td_parent)); 3637cc577a4SJonathan Peyton } 3647cc577a4SJonathan Peyton 3653041982dSJonathan Peyton // __kmp_push_current_task_to_thread: set up current task in called thread for a 3663041982dSJonathan Peyton // new team 3673041982dSJonathan Peyton // 3687cc577a4SJonathan Peyton // this_thr: thread structure to set up 3697cc577a4SJonathan Peyton // team: team for implicit task data 3707cc577a4SJonathan Peyton // tid: thread within team to set up 3713041982dSJonathan Peyton void __kmp_push_current_task_to_thread(kmp_info_t *this_thr, kmp_team_t *team, 3723041982dSJonathan Peyton int tid) { 3733041982dSJonathan Peyton // current task of the thread is a parent of the new just created implicit 3743041982dSJonathan Peyton // tasks of new team 3753041982dSJonathan Peyton KF_TRACE(10, ("__kmp_push_current_task_to_thread(enter): T#%d this_thread=%p " 3763041982dSJonathan Peyton "curtask=%p " 3777cc577a4SJonathan Peyton "parent_task=%p\n", 3787cc577a4SJonathan Peyton tid, this_thr, this_thr->th.th_current_task, 3797cc577a4SJonathan Peyton team->t.t_implicit_task_taskdata[tid].td_parent)); 3807cc577a4SJonathan Peyton 3817cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(this_thr != NULL); 3827cc577a4SJonathan Peyton 3837cc577a4SJonathan Peyton if (tid == 0) { 3847cc577a4SJonathan Peyton if (this_thr->th.th_current_task != &team->t.t_implicit_task_taskdata[0]) { 3853041982dSJonathan Peyton team->t.t_implicit_task_taskdata[0].td_parent = 3863041982dSJonathan Peyton this_thr->th.th_current_task; 3877cc577a4SJonathan Peyton this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[0]; 3887cc577a4SJonathan Peyton } 3897cc577a4SJonathan Peyton } else { 3903041982dSJonathan Peyton team->t.t_implicit_task_taskdata[tid].td_parent = 3913041982dSJonathan Peyton team->t.t_implicit_task_taskdata[0].td_parent; 3927cc577a4SJonathan Peyton this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[tid]; 3937cc577a4SJonathan Peyton } 3947cc577a4SJonathan Peyton 3953041982dSJonathan Peyton KF_TRACE(10, ("__kmp_push_current_task_to_thread(exit): T#%d this_thread=%p " 3963041982dSJonathan Peyton "curtask=%p " 3977cc577a4SJonathan Peyton "parent_task=%p\n", 3987cc577a4SJonathan Peyton tid, this_thr, this_thr->th.th_current_task, 3997cc577a4SJonathan Peyton team->t.t_implicit_task_taskdata[tid].td_parent)); 4007cc577a4SJonathan Peyton } 4017cc577a4SJonathan Peyton 4027cc577a4SJonathan Peyton // __kmp_task_start: bookkeeping for a task starting execution 4033041982dSJonathan Peyton // 4047cc577a4SJonathan Peyton // GTID: global thread id of calling thread 4057cc577a4SJonathan Peyton // task: task starting execution 4067cc577a4SJonathan Peyton // current_task: task suspending 4073041982dSJonathan Peyton static void __kmp_task_start(kmp_int32 gtid, kmp_task_t *task, 4083041982dSJonathan Peyton kmp_taskdata_t *current_task) { 4097cc577a4SJonathan Peyton kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task); 4107cc577a4SJonathan Peyton kmp_info_t *thread = __kmp_threads[gtid]; 4117cc577a4SJonathan Peyton 4123041982dSJonathan Peyton KA_TRACE(10, 4133041982dSJonathan Peyton ("__kmp_task_start(enter): T#%d starting task %p: current_task=%p\n", 4147cc577a4SJonathan Peyton gtid, taskdata, current_task)); 4157cc577a4SJonathan Peyton 4167cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT); 4177cc577a4SJonathan Peyton 4187cc577a4SJonathan Peyton // mark currently executing task as suspended 4197cc577a4SJonathan Peyton // TODO: GEH - make sure root team implicit task is initialized properly. 4207cc577a4SJonathan Peyton // KMP_DEBUG_ASSERT( current_task -> td_flags.executing == 1 ); 4217cc577a4SJonathan Peyton current_task->td_flags.executing = 0; 4227cc577a4SJonathan Peyton 4237cc577a4SJonathan Peyton // Add task to stack if tied 4247cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK 4253041982dSJonathan Peyton if (taskdata->td_flags.tiedness == TASK_TIED) { 4267cc577a4SJonathan Peyton __kmp_push_task_stack(gtid, thread, taskdata); 4277cc577a4SJonathan Peyton } 4287cc577a4SJonathan Peyton #endif /* BUILD_TIED_TASK_STACK */ 4297cc577a4SJonathan Peyton 4307cc577a4SJonathan Peyton // mark starting task as executing and as current task 4317cc577a4SJonathan Peyton thread->th.th_current_task = taskdata; 4327cc577a4SJonathan Peyton 4333041982dSJonathan Peyton KMP_DEBUG_ASSERT(taskdata->td_flags.started == 0 || 4343041982dSJonathan Peyton taskdata->td_flags.tiedness == TASK_UNTIED); 4353041982dSJonathan Peyton KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0 || 4363041982dSJonathan Peyton taskdata->td_flags.tiedness == TASK_UNTIED); 4377cc577a4SJonathan Peyton taskdata->td_flags.started = 1; 4387cc577a4SJonathan Peyton taskdata->td_flags.executing = 1; 4397cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0); 4407cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0); 4417cc577a4SJonathan Peyton 4427cc577a4SJonathan Peyton // GEH TODO: shouldn't we pass some sort of location identifier here? 4437cc577a4SJonathan Peyton // APT: yes, we will pass location here. 4447cc577a4SJonathan Peyton // need to store current thread state (in a thread or taskdata structure) 4457cc577a4SJonathan Peyton // before setting work_state, otherwise wrong state is set after end of task 4467cc577a4SJonathan Peyton 4473041982dSJonathan Peyton KA_TRACE(10, ("__kmp_task_start(exit): T#%d task=%p\n", gtid, taskdata)); 4487cc577a4SJonathan Peyton 4497cc577a4SJonathan Peyton return; 4507cc577a4SJonathan Peyton } 4517cc577a4SJonathan Peyton 452*82e94a59SJoachim Protze #if OMPT_SUPPORT 453*82e94a59SJoachim Protze //------------------------------------------------------------------------------ 454*82e94a59SJoachim Protze // __ompt_task_init: 455*82e94a59SJoachim Protze // Initialize OMPT fields maintained by a task. This will only be called after 456*82e94a59SJoachim Protze // ompt_start_tool, so we already know whether ompt is enabled or not. 457*82e94a59SJoachim Protze 458*82e94a59SJoachim Protze static inline void __ompt_task_init(kmp_taskdata_t *task, int tid) { 459*82e94a59SJoachim Protze // The calls to __ompt_task_init already have the ompt_enabled condition. 460*82e94a59SJoachim Protze task->ompt_task_info.task_data.value = 0; 461*82e94a59SJoachim Protze task->ompt_task_info.frame.exit_runtime_frame = NULL; 462*82e94a59SJoachim Protze task->ompt_task_info.frame.reenter_runtime_frame = NULL; 463*82e94a59SJoachim Protze #if OMP_40_ENABLED 464*82e94a59SJoachim Protze task->ompt_task_info.ndeps = 0; 465*82e94a59SJoachim Protze task->ompt_task_info.deps = NULL; 466*82e94a59SJoachim Protze #endif /* OMP_40_ENABLED */ 467*82e94a59SJoachim Protze } 468*82e94a59SJoachim Protze 469*82e94a59SJoachim Protze // __ompt_task_start: 470*82e94a59SJoachim Protze // Build and trigger task-begin event 471*82e94a59SJoachim Protze static inline void __ompt_task_start(kmp_task_t *task, 472*82e94a59SJoachim Protze kmp_taskdata_t *current_task, 473*82e94a59SJoachim Protze kmp_int32 gtid) { 474*82e94a59SJoachim Protze kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task); 475*82e94a59SJoachim Protze ompt_task_status_t status = ompt_task_others; 476*82e94a59SJoachim Protze if (__kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded) { 477*82e94a59SJoachim Protze status = ompt_task_yield; 478*82e94a59SJoachim Protze __kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded = 0; 479*82e94a59SJoachim Protze } 480*82e94a59SJoachim Protze /* let OMPT know that we're about to run this task */ 481*82e94a59SJoachim Protze if (ompt_enabled.ompt_callback_task_schedule) { 482*82e94a59SJoachim Protze ompt_callbacks.ompt_callback(ompt_callback_task_schedule)( 483*82e94a59SJoachim Protze &(current_task->ompt_task_info.task_data), status, 484*82e94a59SJoachim Protze &(taskdata->ompt_task_info.task_data)); 485*82e94a59SJoachim Protze } 486*82e94a59SJoachim Protze taskdata->ompt_task_info.scheduling_parent = current_task; 487*82e94a59SJoachim Protze } 488*82e94a59SJoachim Protze 489*82e94a59SJoachim Protze // __ompt_task_finish: 490*82e94a59SJoachim Protze // Build and trigger final task-schedule event 491*82e94a59SJoachim Protze static inline void __ompt_task_finish(kmp_task_t *task, 492*82e94a59SJoachim Protze kmp_taskdata_t *resumed_task) { 493*82e94a59SJoachim Protze kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task); 494*82e94a59SJoachim Protze ompt_task_status_t status = ompt_task_complete; 495*82e94a59SJoachim Protze if (taskdata->td_flags.tiedness == TASK_UNTIED && 496*82e94a59SJoachim Protze KMP_TEST_THEN_ADD32(&(taskdata->td_untied_count), 0) > 1) 497*82e94a59SJoachim Protze status = ompt_task_others; 498*82e94a59SJoachim Protze if (__kmp_omp_cancellation && taskdata->td_taskgroup && 499*82e94a59SJoachim Protze taskdata->td_taskgroup->cancel_request == cancel_taskgroup) { 500*82e94a59SJoachim Protze status = ompt_task_cancel; 501*82e94a59SJoachim Protze } 502*82e94a59SJoachim Protze 503*82e94a59SJoachim Protze /* let OMPT know that we're returning to the callee task */ 504*82e94a59SJoachim Protze if (ompt_enabled.ompt_callback_task_schedule) { 505*82e94a59SJoachim Protze ompt_callbacks.ompt_callback(ompt_callback_task_schedule)( 506*82e94a59SJoachim Protze &(taskdata->ompt_task_info.task_data), status, 507*82e94a59SJoachim Protze &((resumed_task ? resumed_task 508*82e94a59SJoachim Protze : (taskdata->ompt_task_info.scheduling_parent 509*82e94a59SJoachim Protze ? taskdata->ompt_task_info.scheduling_parent 510*82e94a59SJoachim Protze : taskdata->td_parent)) 511*82e94a59SJoachim Protze ->ompt_task_info.task_data)); 512*82e94a59SJoachim Protze } 513*82e94a59SJoachim Protze } 514*82e94a59SJoachim Protze #endif 515*82e94a59SJoachim Protze 516*82e94a59SJoachim Protze template <bool ompt> 517*82e94a59SJoachim Protze static void __kmpc_omp_task_begin_if0_template(ident_t *loc_ref, kmp_int32 gtid, 518*82e94a59SJoachim Protze kmp_task_t *task, 519*82e94a59SJoachim Protze void *frame_address, 520*82e94a59SJoachim Protze void *return_address) { 5217cc577a4SJonathan Peyton kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task); 5227cc577a4SJonathan Peyton kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task; 5237cc577a4SJonathan Peyton 5243041982dSJonathan Peyton KA_TRACE(10, ("__kmpc_omp_task_begin_if0(enter): T#%d loc=%p task=%p " 5253041982dSJonathan Peyton "current_task=%p\n", 5267cc577a4SJonathan Peyton gtid, loc_ref, taskdata, current_task)); 5277cc577a4SJonathan Peyton 5287cc577a4SJonathan Peyton if (taskdata->td_flags.tiedness == TASK_UNTIED) { 5293041982dSJonathan Peyton // untied task needs to increment counter so that the task structure is not 5303041982dSJonathan Peyton // freed prematurely 5317cc577a4SJonathan Peyton kmp_int32 counter = 1 + KMP_TEST_THEN_INC32(&taskdata->td_untied_count); 5323041982dSJonathan Peyton KA_TRACE(20, ("__kmpc_omp_task_begin_if0: T#%d untied_count (%d) " 5333041982dSJonathan Peyton "incremented for task %p\n", 5347cc577a4SJonathan Peyton gtid, counter, taskdata)); 5357cc577a4SJonathan Peyton } 5367cc577a4SJonathan Peyton 5373041982dSJonathan Peyton taskdata->td_flags.task_serial = 5383041982dSJonathan Peyton 1; // Execute this task immediately, not deferred. 5397cc577a4SJonathan Peyton __kmp_task_start(gtid, task, current_task); 5407cc577a4SJonathan Peyton 541*82e94a59SJoachim Protze #if OMPT_SUPPORT 542*82e94a59SJoachim Protze if (ompt) { 543*82e94a59SJoachim Protze if (current_task->ompt_task_info.frame.reenter_runtime_frame == NULL) { 544*82e94a59SJoachim Protze current_task->ompt_task_info.frame.reenter_runtime_frame = 545*82e94a59SJoachim Protze taskdata->ompt_task_info.frame.exit_runtime_frame = frame_address; 546*82e94a59SJoachim Protze } 547*82e94a59SJoachim Protze if (ompt_enabled.ompt_callback_task_create) { 548*82e94a59SJoachim Protze ompt_task_info_t *parent_info = &(current_task->ompt_task_info); 549*82e94a59SJoachim Protze ompt_callbacks.ompt_callback(ompt_callback_task_create)( 550*82e94a59SJoachim Protze &(parent_info->task_data), &(parent_info->frame), 551*82e94a59SJoachim Protze &(taskdata->ompt_task_info.task_data), 552*82e94a59SJoachim Protze ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(taskdata), 0, 553*82e94a59SJoachim Protze return_address); 554*82e94a59SJoachim Protze } 555*82e94a59SJoachim Protze __ompt_task_start(task, current_task, gtid); 556*82e94a59SJoachim Protze } 557*82e94a59SJoachim Protze #endif // OMPT_SUPPORT 558*82e94a59SJoachim Protze 5593041982dSJonathan Peyton KA_TRACE(10, ("__kmpc_omp_task_begin_if0(exit): T#%d loc=%p task=%p,\n", gtid, 5603041982dSJonathan Peyton loc_ref, taskdata)); 561*82e94a59SJoachim Protze } 5627cc577a4SJonathan Peyton 563*82e94a59SJoachim Protze #if OMPT_SUPPORT 564*82e94a59SJoachim Protze OMPT_NOINLINE 565*82e94a59SJoachim Protze static void __kmpc_omp_task_begin_if0_ompt(ident_t *loc_ref, kmp_int32 gtid, 566*82e94a59SJoachim Protze kmp_task_t *task, 567*82e94a59SJoachim Protze void *frame_address, 568*82e94a59SJoachim Protze void *return_address) { 569*82e94a59SJoachim Protze __kmpc_omp_task_begin_if0_template<true>(loc_ref, gtid, task, frame_address, 570*82e94a59SJoachim Protze return_address); 571*82e94a59SJoachim Protze } 572*82e94a59SJoachim Protze #endif // OMPT_SUPPORT 573*82e94a59SJoachim Protze 574*82e94a59SJoachim Protze // __kmpc_omp_task_begin_if0: report that a given serialized task has started 575*82e94a59SJoachim Protze // execution 576*82e94a59SJoachim Protze // 577*82e94a59SJoachim Protze // loc_ref: source location information; points to beginning of task block. 578*82e94a59SJoachim Protze // gtid: global thread number. 579*82e94a59SJoachim Protze // task: task thunk for the started task. 580*82e94a59SJoachim Protze void __kmpc_omp_task_begin_if0(ident_t *loc_ref, kmp_int32 gtid, 581*82e94a59SJoachim Protze kmp_task_t *task) { 582*82e94a59SJoachim Protze #if OMPT_SUPPORT 583*82e94a59SJoachim Protze if (UNLIKELY(ompt_enabled.enabled)) { 584*82e94a59SJoachim Protze OMPT_STORE_RETURN_ADDRESS(gtid); 585*82e94a59SJoachim Protze __kmpc_omp_task_begin_if0_ompt(loc_ref, gtid, task, 586*82e94a59SJoachim Protze OMPT_GET_FRAME_ADDRESS(1), 587*82e94a59SJoachim Protze OMPT_LOAD_RETURN_ADDRESS(gtid)); 5887cc577a4SJonathan Peyton return; 5897cc577a4SJonathan Peyton } 590*82e94a59SJoachim Protze #endif 591*82e94a59SJoachim Protze __kmpc_omp_task_begin_if0_template<false>(loc_ref, gtid, task, NULL, NULL); 592*82e94a59SJoachim Protze } 5937cc577a4SJonathan Peyton 5947cc577a4SJonathan Peyton #ifdef TASK_UNUSED 5957cc577a4SJonathan Peyton // __kmpc_omp_task_begin: report that a given task has started execution 5967cc577a4SJonathan Peyton // NEVER GENERATED BY COMPILER, DEPRECATED!!! 5973041982dSJonathan Peyton void __kmpc_omp_task_begin(ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task) { 5987cc577a4SJonathan Peyton kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task; 5997cc577a4SJonathan Peyton 6003041982dSJonathan Peyton KA_TRACE( 6013041982dSJonathan Peyton 10, 6023041982dSJonathan Peyton ("__kmpc_omp_task_begin(enter): T#%d loc=%p task=%p current_task=%p\n", 6037cc577a4SJonathan Peyton gtid, loc_ref, KMP_TASK_TO_TASKDATA(task), current_task)); 6047cc577a4SJonathan Peyton 6057cc577a4SJonathan Peyton __kmp_task_start(gtid, task, current_task); 6067cc577a4SJonathan Peyton 6073041982dSJonathan Peyton KA_TRACE(10, ("__kmpc_omp_task_begin(exit): T#%d loc=%p task=%p,\n", gtid, 6083041982dSJonathan Peyton loc_ref, KMP_TASK_TO_TASKDATA(task))); 6097cc577a4SJonathan Peyton return; 6107cc577a4SJonathan Peyton } 6117cc577a4SJonathan Peyton #endif // TASK_UNUSED 6127cc577a4SJonathan Peyton 6137cc577a4SJonathan Peyton // __kmp_free_task: free the current task space and the space for shareds 6143041982dSJonathan Peyton // 6157cc577a4SJonathan Peyton // gtid: Global thread ID of calling thread 6167cc577a4SJonathan Peyton // taskdata: task to free 6177cc577a4SJonathan Peyton // thread: thread data structure of caller 6183041982dSJonathan Peyton static void __kmp_free_task(kmp_int32 gtid, kmp_taskdata_t *taskdata, 6193041982dSJonathan Peyton kmp_info_t *thread) { 6203041982dSJonathan Peyton KA_TRACE(30, ("__kmp_free_task: T#%d freeing data from task %p\n", gtid, 6213041982dSJonathan Peyton taskdata)); 6227cc577a4SJonathan Peyton 6237cc577a4SJonathan Peyton // Check to make sure all flags and counters have the correct values 6247cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT); 6257cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0); 6267cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 1); 6277cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0); 6283041982dSJonathan Peyton KMP_DEBUG_ASSERT(TCR_4(taskdata->td_allocated_child_tasks) == 0 || 6293041982dSJonathan Peyton taskdata->td_flags.task_serial == 1); 6307cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(TCR_4(taskdata->td_incomplete_child_tasks) == 0); 6317cc577a4SJonathan Peyton 6327cc577a4SJonathan Peyton taskdata->td_flags.freed = 1; 6337cc577a4SJonathan Peyton ANNOTATE_HAPPENS_BEFORE(taskdata); 6347cc577a4SJonathan Peyton // deallocate the taskdata and shared variable blocks associated with this task 6357cc577a4SJonathan Peyton #if USE_FAST_MEMORY 6367cc577a4SJonathan Peyton __kmp_fast_free(thread, taskdata); 6377cc577a4SJonathan Peyton #else /* ! USE_FAST_MEMORY */ 6387cc577a4SJonathan Peyton __kmp_thread_free(thread, taskdata); 6397cc577a4SJonathan Peyton #endif 6407cc577a4SJonathan Peyton 6413041982dSJonathan Peyton KA_TRACE(20, ("__kmp_free_task: T#%d freed task %p\n", gtid, taskdata)); 6427cc577a4SJonathan Peyton } 6437cc577a4SJonathan Peyton 6443041982dSJonathan Peyton // __kmp_free_task_and_ancestors: free the current task and ancestors without 6453041982dSJonathan Peyton // children 6467cc577a4SJonathan Peyton // 6477cc577a4SJonathan Peyton // gtid: Global thread ID of calling thread 6487cc577a4SJonathan Peyton // taskdata: task to free 6497cc577a4SJonathan Peyton // thread: thread data structure of caller 6503041982dSJonathan Peyton static void __kmp_free_task_and_ancestors(kmp_int32 gtid, 6513041982dSJonathan Peyton kmp_taskdata_t *taskdata, 6523041982dSJonathan Peyton kmp_info_t *thread) { 6537cc577a4SJonathan Peyton #if OMP_45_ENABLED 6547cc577a4SJonathan Peyton // Proxy tasks must always be allowed to free their parents 6557cc577a4SJonathan Peyton // because they can be run in background even in serial mode. 6563041982dSJonathan Peyton kmp_int32 team_serial = 6573041982dSJonathan Peyton (taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) && 6583041982dSJonathan Peyton !taskdata->td_flags.proxy; 6597cc577a4SJonathan Peyton #else 6603041982dSJonathan Peyton kmp_int32 team_serial = 6613041982dSJonathan Peyton taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser; 6627cc577a4SJonathan Peyton #endif 6637cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT); 6647cc577a4SJonathan Peyton 6655ba90c79SAndrey Churbanov kmp_int32 children = 6665ba90c79SAndrey Churbanov KMP_TEST_THEN_DEC32(&taskdata->td_allocated_child_tasks) - 1; 6677cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(children >= 0); 6687cc577a4SJonathan Peyton 6697cc577a4SJonathan Peyton // Now, go up the ancestor tree to see if any ancestors can now be freed. 6703041982dSJonathan Peyton while (children == 0) { 6717cc577a4SJonathan Peyton kmp_taskdata_t *parent_taskdata = taskdata->td_parent; 6727cc577a4SJonathan Peyton 6737cc577a4SJonathan Peyton KA_TRACE(20, ("__kmp_free_task_and_ancestors(enter): T#%d task %p complete " 6743041982dSJonathan Peyton "and freeing itself\n", 6753041982dSJonathan Peyton gtid, taskdata)); 6767cc577a4SJonathan Peyton 6777cc577a4SJonathan Peyton // --- Deallocate my ancestor task --- 6787cc577a4SJonathan Peyton __kmp_free_task(gtid, taskdata, thread); 6797cc577a4SJonathan Peyton 6807cc577a4SJonathan Peyton taskdata = parent_taskdata; 6817cc577a4SJonathan Peyton 6823041982dSJonathan Peyton // Stop checking ancestors at implicit task instead of walking up ancestor 6833041982dSJonathan Peyton // tree to avoid premature deallocation of ancestors. 6847cc577a4SJonathan Peyton if (team_serial || taskdata->td_flags.tasktype == TASK_IMPLICIT) 6857cc577a4SJonathan Peyton return; 6867cc577a4SJonathan Peyton 6877cc577a4SJonathan Peyton // Predecrement simulated by "- 1" calculation 6885ba90c79SAndrey Churbanov children = KMP_TEST_THEN_DEC32(&taskdata->td_allocated_child_tasks) - 1; 6897cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(children >= 0); 6907cc577a4SJonathan Peyton } 6917cc577a4SJonathan Peyton 6923041982dSJonathan Peyton KA_TRACE( 6933041982dSJonathan Peyton 20, ("__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; " 6943041982dSJonathan Peyton "not freeing it yet\n", 6953041982dSJonathan Peyton gtid, taskdata, children)); 6967cc577a4SJonathan Peyton } 6977cc577a4SJonathan Peyton 6987cc577a4SJonathan Peyton // __kmp_task_finish: bookkeeping to do when a task finishes execution 6993041982dSJonathan Peyton // 7007cc577a4SJonathan Peyton // gtid: global thread ID for calling thread 7017cc577a4SJonathan Peyton // task: task to be finished 7027cc577a4SJonathan Peyton // resumed_task: task to be resumed. (may be NULL if task is serialized) 7033041982dSJonathan Peyton static void __kmp_task_finish(kmp_int32 gtid, kmp_task_t *task, 7043041982dSJonathan Peyton kmp_taskdata_t *resumed_task) { 7057cc577a4SJonathan Peyton kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task); 7067cc577a4SJonathan Peyton kmp_info_t *thread = __kmp_threads[gtid]; 7073041982dSJonathan Peyton kmp_task_team_t *task_team = 7083041982dSJonathan Peyton thread->th.th_task_team; // might be NULL for serial teams... 7097cc577a4SJonathan Peyton kmp_int32 children = 0; 7107cc577a4SJonathan Peyton 7113041982dSJonathan Peyton KA_TRACE(10, ("__kmp_task_finish(enter): T#%d finishing task %p and resuming " 7123041982dSJonathan Peyton "task %p\n", 7137cc577a4SJonathan Peyton gtid, taskdata, resumed_task)); 7147cc577a4SJonathan Peyton 7157cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT); 7167cc577a4SJonathan Peyton 7177cc577a4SJonathan Peyton // Pop task from stack if tied 7187cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK 7193041982dSJonathan Peyton if (taskdata->td_flags.tiedness == TASK_TIED) { 7207cc577a4SJonathan Peyton __kmp_pop_task_stack(gtid, thread, taskdata); 7217cc577a4SJonathan Peyton } 7227cc577a4SJonathan Peyton #endif /* BUILD_TIED_TASK_STACK */ 7237cc577a4SJonathan Peyton 7247cc577a4SJonathan Peyton if (taskdata->td_flags.tiedness == TASK_UNTIED) { 7253041982dSJonathan Peyton // untied task needs to check the counter so that the task structure is not 7263041982dSJonathan Peyton // freed prematurely 7277cc577a4SJonathan Peyton kmp_int32 counter = KMP_TEST_THEN_DEC32(&taskdata->td_untied_count) - 1; 7283041982dSJonathan Peyton KA_TRACE( 7293041982dSJonathan Peyton 20, 7303041982dSJonathan Peyton ("__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n", 7317cc577a4SJonathan Peyton gtid, counter, taskdata)); 7327cc577a4SJonathan Peyton if (counter > 0) { 7333041982dSJonathan Peyton // untied task is not done, to be continued possibly by other thread, do 7343041982dSJonathan Peyton // not free it now 7357cc577a4SJonathan Peyton if (resumed_task == NULL) { 7367cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(taskdata->td_flags.task_serial); 7373041982dSJonathan Peyton resumed_task = taskdata->td_parent; // In a serialized task, the resumed 7383041982dSJonathan Peyton // task is the parent 7397cc577a4SJonathan Peyton } 7407cc577a4SJonathan Peyton thread->th.th_current_task = resumed_task; // restore current_task 7417cc577a4SJonathan Peyton resumed_task->td_flags.executing = 1; // resume previous task 7423041982dSJonathan Peyton KA_TRACE(10, ("__kmp_task_finish(exit): T#%d partially done task %p, " 7433041982dSJonathan Peyton "resuming task %p\n", 7447cc577a4SJonathan Peyton gtid, taskdata, resumed_task)); 7457cc577a4SJonathan Peyton return; 7467cc577a4SJonathan Peyton } 7477cc577a4SJonathan Peyton } 7487cc577a4SJonathan Peyton 7497cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0); 7507cc577a4SJonathan Peyton taskdata->td_flags.complete = 1; // mark the task as completed 7517cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(taskdata->td_flags.started == 1); 7527cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0); 7537cc577a4SJonathan Peyton 7543041982dSJonathan Peyton // Only need to keep track of count if team parallel and tasking not 7553041982dSJonathan Peyton // serialized 7567cc577a4SJonathan Peyton if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) { 7577cc577a4SJonathan Peyton // Predecrement simulated by "- 1" calculation 7583041982dSJonathan Peyton children = 7595ba90c79SAndrey Churbanov KMP_TEST_THEN_DEC32(&taskdata->td_parent->td_incomplete_child_tasks) - 7603041982dSJonathan Peyton 1; 7617cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(children >= 0); 7627cc577a4SJonathan Peyton #if OMP_40_ENABLED 7637cc577a4SJonathan Peyton if (taskdata->td_taskgroup) 7647cc577a4SJonathan Peyton KMP_TEST_THEN_DEC32((kmp_int32 *)(&taskdata->td_taskgroup->count)); 7657cc577a4SJonathan Peyton #if OMP_45_ENABLED 7667cc577a4SJonathan Peyton } 7677cc577a4SJonathan Peyton // if we found proxy tasks there could exist a dependency chain 7687cc577a4SJonathan Peyton // with the proxy task as origin 7693041982dSJonathan Peyton if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) || 7703041982dSJonathan Peyton (task_team && task_team->tt.tt_found_proxy_tasks)) { 7717cc577a4SJonathan Peyton #endif 7727cc577a4SJonathan Peyton __kmp_release_deps(gtid, taskdata); 7737cc577a4SJonathan Peyton #endif 7747cc577a4SJonathan Peyton } 7757cc577a4SJonathan Peyton 7763041982dSJonathan Peyton // td_flags.executing must be marked as 0 after __kmp_release_deps has been 7773041982dSJonathan Peyton // called. Othertwise, if a task is executed immediately from the release_deps 7783041982dSJonathan Peyton // code, the flag will be reset to 1 again by this same function 7797cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1); 7807cc577a4SJonathan Peyton taskdata->td_flags.executing = 0; // suspend the finishing task 7817cc577a4SJonathan Peyton 7823041982dSJonathan Peyton KA_TRACE( 7833041982dSJonathan Peyton 20, ("__kmp_task_finish: T#%d finished task %p, %d incomplete children\n", 7847cc577a4SJonathan Peyton gtid, taskdata, children)); 7857cc577a4SJonathan Peyton 7867cc577a4SJonathan Peyton #if OMP_40_ENABLED 7877cc577a4SJonathan Peyton /* If the tasks' destructor thunk flag has been set, we need to invoke the 7883041982dSJonathan Peyton destructor thunk that has been generated by the compiler. The code is 7893041982dSJonathan Peyton placed here, since at this point other tasks might have been released 7907cc577a4SJonathan Peyton hence overlapping the destructor invokations with some other work in the 7913041982dSJonathan Peyton released tasks. The OpenMP spec is not specific on when the destructors 7923041982dSJonathan Peyton are invoked, so we should be free to choose. */ 7937cc577a4SJonathan Peyton if (taskdata->td_flags.destructors_thunk) { 7947cc577a4SJonathan Peyton kmp_routine_entry_t destr_thunk = task->data1.destructors; 7957cc577a4SJonathan Peyton KMP_ASSERT(destr_thunk); 7967cc577a4SJonathan Peyton destr_thunk(gtid, task); 7977cc577a4SJonathan Peyton } 7987cc577a4SJonathan Peyton #endif // OMP_40_ENABLED 7997cc577a4SJonathan Peyton 8007cc577a4SJonathan Peyton // bookkeeping for resuming task: 8017cc577a4SJonathan Peyton // GEH - note tasking_ser => task_serial 8023041982dSJonathan Peyton KMP_DEBUG_ASSERT( 8033041982dSJonathan Peyton (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) == 8047cc577a4SJonathan Peyton taskdata->td_flags.task_serial); 8053041982dSJonathan Peyton if (taskdata->td_flags.task_serial) { 8067cc577a4SJonathan Peyton if (resumed_task == NULL) { 8073041982dSJonathan Peyton resumed_task = taskdata->td_parent; // In a serialized task, the resumed 8083041982dSJonathan Peyton // task is the parent 8093041982dSJonathan Peyton } else 8107cc577a4SJonathan Peyton #if OMP_45_ENABLED 8117cc577a4SJonathan Peyton if (!(task_team && task_team->tt.tt_found_proxy_tasks)) 8127cc577a4SJonathan Peyton #endif 8137cc577a4SJonathan Peyton { 8147cc577a4SJonathan Peyton // verify resumed task passed in points to parent 8157cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(resumed_task == taskdata->td_parent); 8167cc577a4SJonathan Peyton } 8173041982dSJonathan Peyton } else { 8183041982dSJonathan Peyton KMP_DEBUG_ASSERT(resumed_task != 8193041982dSJonathan Peyton NULL); // verify that resumed task is passed as arguemnt 8207cc577a4SJonathan Peyton } 8217cc577a4SJonathan Peyton 8227cc577a4SJonathan Peyton // Free this task and then ancestor tasks if they have no children. 8237cc577a4SJonathan Peyton // Restore th_current_task first as suggested by John: 8247cc577a4SJonathan Peyton // johnmc: if an asynchronous inquiry peers into the runtime system 8257cc577a4SJonathan Peyton // it doesn't see the freed task as the current task. 8267cc577a4SJonathan Peyton thread->th.th_current_task = resumed_task; 8277cc577a4SJonathan Peyton __kmp_free_task_and_ancestors(gtid, taskdata, thread); 8287cc577a4SJonathan Peyton 8297cc577a4SJonathan Peyton // TODO: GEH - make sure root team implicit task is initialized properly. 8307cc577a4SJonathan Peyton // KMP_DEBUG_ASSERT( resumed_task->td_flags.executing == 0 ); 8317cc577a4SJonathan Peyton resumed_task->td_flags.executing = 1; // resume previous task 8327cc577a4SJonathan Peyton 8333041982dSJonathan Peyton KA_TRACE( 8343041982dSJonathan Peyton 10, ("__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n", 8357cc577a4SJonathan Peyton gtid, taskdata, resumed_task)); 8367cc577a4SJonathan Peyton 8377cc577a4SJonathan Peyton return; 8387cc577a4SJonathan Peyton } 8397cc577a4SJonathan Peyton 840*82e94a59SJoachim Protze template <bool ompt> 841*82e94a59SJoachim Protze static void __kmpc_omp_task_complete_if0_template(ident_t *loc_ref, 842*82e94a59SJoachim Protze kmp_int32 gtid, 8433041982dSJonathan Peyton kmp_task_t *task) { 8447cc577a4SJonathan Peyton KA_TRACE(10, ("__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n", 8457cc577a4SJonathan Peyton gtid, loc_ref, KMP_TASK_TO_TASKDATA(task))); 8463041982dSJonathan Peyton // this routine will provide task to resume 8473041982dSJonathan Peyton __kmp_task_finish(gtid, task, NULL); 8487cc577a4SJonathan Peyton 8497cc577a4SJonathan Peyton KA_TRACE(10, ("__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n", 8507cc577a4SJonathan Peyton gtid, loc_ref, KMP_TASK_TO_TASKDATA(task))); 851*82e94a59SJoachim Protze 852*82e94a59SJoachim Protze #if OMPT_SUPPORT 853*82e94a59SJoachim Protze if (ompt) { 854*82e94a59SJoachim Protze __ompt_task_finish(task, NULL); 855*82e94a59SJoachim Protze ompt_frame_t *ompt_frame; 856*82e94a59SJoachim Protze __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL); 857*82e94a59SJoachim Protze ompt_frame->reenter_runtime_frame = NULL; 858*82e94a59SJoachim Protze } 859*82e94a59SJoachim Protze #endif 860*82e94a59SJoachim Protze 8617cc577a4SJonathan Peyton return; 8627cc577a4SJonathan Peyton } 8637cc577a4SJonathan Peyton 864*82e94a59SJoachim Protze #if OMPT_SUPPORT 865*82e94a59SJoachim Protze OMPT_NOINLINE 866*82e94a59SJoachim Protze void __kmpc_omp_task_complete_if0_ompt(ident_t *loc_ref, kmp_int32 gtid, 867*82e94a59SJoachim Protze kmp_task_t *task) { 868*82e94a59SJoachim Protze __kmpc_omp_task_complete_if0_template<true>(loc_ref, gtid, task); 869*82e94a59SJoachim Protze } 870*82e94a59SJoachim Protze #endif // OMPT_SUPPORT 871*82e94a59SJoachim Protze 872*82e94a59SJoachim Protze // __kmpc_omp_task_complete_if0: report that a task has completed execution 873*82e94a59SJoachim Protze // 874*82e94a59SJoachim Protze // loc_ref: source location information; points to end of task block. 875*82e94a59SJoachim Protze // gtid: global thread number. 876*82e94a59SJoachim Protze // task: task thunk for the completed task. 877*82e94a59SJoachim Protze void __kmpc_omp_task_complete_if0(ident_t *loc_ref, kmp_int32 gtid, 878*82e94a59SJoachim Protze kmp_task_t *task) { 879*82e94a59SJoachim Protze #if OMPT_SUPPORT 880*82e94a59SJoachim Protze if (UNLIKELY(ompt_enabled.enabled)) { 881*82e94a59SJoachim Protze __kmpc_omp_task_complete_if0_ompt(loc_ref, gtid, task); 882*82e94a59SJoachim Protze return; 883*82e94a59SJoachim Protze } 884*82e94a59SJoachim Protze #endif 885*82e94a59SJoachim Protze __kmpc_omp_task_complete_if0_template<false>(loc_ref, gtid, task); 886*82e94a59SJoachim Protze } 887*82e94a59SJoachim Protze 8887cc577a4SJonathan Peyton #ifdef TASK_UNUSED 8897cc577a4SJonathan Peyton // __kmpc_omp_task_complete: report that a task has completed execution 8907cc577a4SJonathan Peyton // NEVER GENERATED BY COMPILER, DEPRECATED!!! 8913041982dSJonathan Peyton void __kmpc_omp_task_complete(ident_t *loc_ref, kmp_int32 gtid, 8923041982dSJonathan Peyton kmp_task_t *task) { 8933041982dSJonathan Peyton KA_TRACE(10, ("__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n", gtid, 8943041982dSJonathan Peyton loc_ref, KMP_TASK_TO_TASKDATA(task))); 8957cc577a4SJonathan Peyton 8967cc577a4SJonathan Peyton __kmp_task_finish(gtid, task, NULL); // Not sure how to find task to resume 8977cc577a4SJonathan Peyton 8983041982dSJonathan Peyton KA_TRACE(10, ("__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n", gtid, 8993041982dSJonathan Peyton loc_ref, KMP_TASK_TO_TASKDATA(task))); 9007cc577a4SJonathan Peyton return; 9017cc577a4SJonathan Peyton } 9027cc577a4SJonathan Peyton #endif // TASK_UNUSED 9037cc577a4SJonathan Peyton 9043041982dSJonathan Peyton // __kmp_init_implicit_task: Initialize the appropriate fields in the implicit 9053041982dSJonathan Peyton // task for a given thread 9067cc577a4SJonathan Peyton // 9077cc577a4SJonathan Peyton // loc_ref: reference to source location of parallel region 9087cc577a4SJonathan Peyton // this_thr: thread data structure corresponding to implicit task 9097cc577a4SJonathan Peyton // team: team for this_thr 9107cc577a4SJonathan Peyton // tid: thread id of given thread within team 9117cc577a4SJonathan Peyton // set_curr_task: TRUE if need to push current task to thread 9123041982dSJonathan Peyton // NOTE: Routine does not set up the implicit task ICVS. This is assumed to 9133041982dSJonathan Peyton // have already been done elsewhere. 9147cc577a4SJonathan Peyton // TODO: Get better loc_ref. Value passed in may be NULL 9153041982dSJonathan Peyton void __kmp_init_implicit_task(ident_t *loc_ref, kmp_info_t *this_thr, 9163041982dSJonathan Peyton kmp_team_t *team, int tid, int set_curr_task) { 9177cc577a4SJonathan Peyton kmp_taskdata_t *task = &team->t.t_implicit_task_taskdata[tid]; 9187cc577a4SJonathan Peyton 9193041982dSJonathan Peyton KF_TRACE( 9203041982dSJonathan Peyton 10, 9213041982dSJonathan Peyton ("__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n", 9227cc577a4SJonathan Peyton tid, team, task, set_curr_task ? "TRUE" : "FALSE")); 9237cc577a4SJonathan Peyton 9247cc577a4SJonathan Peyton task->td_task_id = KMP_GEN_TASK_ID(); 9257cc577a4SJonathan Peyton task->td_team = team; 9263041982dSJonathan Peyton // task->td_parent = NULL; // fix for CQ230101 (broken parent task info 9273041982dSJonathan Peyton // in debugger) 9287cc577a4SJonathan Peyton task->td_ident = loc_ref; 9297cc577a4SJonathan Peyton task->td_taskwait_ident = NULL; 9307cc577a4SJonathan Peyton task->td_taskwait_counter = 0; 9317cc577a4SJonathan Peyton task->td_taskwait_thread = 0; 9327cc577a4SJonathan Peyton 9337cc577a4SJonathan Peyton task->td_flags.tiedness = TASK_TIED; 9347cc577a4SJonathan Peyton task->td_flags.tasktype = TASK_IMPLICIT; 9357cc577a4SJonathan Peyton #if OMP_45_ENABLED 9367cc577a4SJonathan Peyton task->td_flags.proxy = TASK_FULL; 9377cc577a4SJonathan Peyton #endif 9387cc577a4SJonathan Peyton 9397cc577a4SJonathan Peyton // All implicit tasks are executed immediately, not deferred 9407cc577a4SJonathan Peyton task->td_flags.task_serial = 1; 9417cc577a4SJonathan Peyton task->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec); 9427cc577a4SJonathan Peyton task->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0; 9437cc577a4SJonathan Peyton 9447cc577a4SJonathan Peyton task->td_flags.started = 1; 9457cc577a4SJonathan Peyton task->td_flags.executing = 1; 9467cc577a4SJonathan Peyton task->td_flags.complete = 0; 9477cc577a4SJonathan Peyton task->td_flags.freed = 0; 9487cc577a4SJonathan Peyton 9497cc577a4SJonathan Peyton #if OMP_40_ENABLED 9507cc577a4SJonathan Peyton task->td_depnode = NULL; 9517cc577a4SJonathan Peyton #endif 9527cc577a4SJonathan Peyton 9533041982dSJonathan Peyton if (set_curr_task) { // only do this init first time thread is created 9547cc577a4SJonathan Peyton task->td_incomplete_child_tasks = 0; 955642688b6SJonathan Peyton // Not used: don't need to deallocate implicit task 956642688b6SJonathan Peyton task->td_allocated_child_tasks = 0; 9577cc577a4SJonathan Peyton #if OMP_40_ENABLED 9587cc577a4SJonathan Peyton task->td_taskgroup = NULL; // An implicit task does not have taskgroup 9597cc577a4SJonathan Peyton task->td_dephash = NULL; 9607cc577a4SJonathan Peyton #endif 9617cc577a4SJonathan Peyton __kmp_push_current_task_to_thread(this_thr, team, tid); 9627cc577a4SJonathan Peyton } else { 9637cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0); 9647cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0); 9657cc577a4SJonathan Peyton } 9667cc577a4SJonathan Peyton 9677cc577a4SJonathan Peyton #if OMPT_SUPPORT 968*82e94a59SJoachim Protze if (UNLIKELY(ompt_enabled.enabled)) 969*82e94a59SJoachim Protze __ompt_task_init(task, tid); 9707cc577a4SJonathan Peyton #endif 9717cc577a4SJonathan Peyton 9723041982dSJonathan Peyton KF_TRACE(10, ("__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n", tid, 9733041982dSJonathan Peyton team, task)); 9747cc577a4SJonathan Peyton } 9757cc577a4SJonathan Peyton 9763041982dSJonathan Peyton // __kmp_finish_implicit_task: Release resources associated to implicit tasks 9773041982dSJonathan Peyton // at the end of parallel regions. Some resources are kept for reuse in the next 9783041982dSJonathan Peyton // parallel region. 9797cc577a4SJonathan Peyton // 9803041982dSJonathan Peyton // thread: thread data structure corresponding to implicit task 9813041982dSJonathan Peyton void __kmp_finish_implicit_task(kmp_info_t *thread) { 9827cc577a4SJonathan Peyton kmp_taskdata_t *task = thread->th.th_current_task; 9837cc577a4SJonathan Peyton if (task->td_dephash) 9847cc577a4SJonathan Peyton __kmp_dephash_free_entries(thread, task->td_dephash); 9857cc577a4SJonathan Peyton } 9867cc577a4SJonathan Peyton 9873041982dSJonathan Peyton // __kmp_free_implicit_task: Release resources associated to implicit tasks 9883041982dSJonathan Peyton // when these are destroyed regions 9897cc577a4SJonathan Peyton // 9903041982dSJonathan Peyton // thread: thread data structure corresponding to implicit task 9913041982dSJonathan Peyton void __kmp_free_implicit_task(kmp_info_t *thread) { 9927cc577a4SJonathan Peyton kmp_taskdata_t *task = thread->th.th_current_task; 9937cc577a4SJonathan Peyton if (task->td_dephash) 9947cc577a4SJonathan Peyton __kmp_dephash_free(thread, task->td_dephash); 9957cc577a4SJonathan Peyton task->td_dephash = NULL; 9967cc577a4SJonathan Peyton } 9977cc577a4SJonathan Peyton 9983041982dSJonathan Peyton // Round up a size to a power of two specified by val: Used to insert padding 9993041982dSJonathan Peyton // between structures co-allocated using a single malloc() call 10003041982dSJonathan Peyton static size_t __kmp_round_up_to_val(size_t size, size_t val) { 10017cc577a4SJonathan Peyton if (size & (val - 1)) { 10027cc577a4SJonathan Peyton size &= ~(val - 1); 10037cc577a4SJonathan Peyton if (size <= KMP_SIZE_T_MAX - val) { 10047cc577a4SJonathan Peyton size += val; // Round up if there is no overflow. 1005bd3a7633SJonathan Peyton } 1006bd3a7633SJonathan Peyton } 10077cc577a4SJonathan Peyton return size; 10087cc577a4SJonathan Peyton } // __kmp_round_up_to_va 10097cc577a4SJonathan Peyton 10107cc577a4SJonathan Peyton // __kmp_task_alloc: Allocate the taskdata and task data structures for a task 10117cc577a4SJonathan Peyton // 10127cc577a4SJonathan Peyton // loc_ref: source location information 10137cc577a4SJonathan Peyton // gtid: global thread number. 10143041982dSJonathan Peyton // flags: include tiedness & task type (explicit vs. implicit) of the ''new'' 10153041982dSJonathan Peyton // task encountered. Converted from kmp_int32 to kmp_tasking_flags_t in routine. 10163041982dSJonathan Peyton // sizeof_kmp_task_t: Size in bytes of kmp_task_t data structure including 10173041982dSJonathan Peyton // private vars accessed in task. 10183041982dSJonathan Peyton // sizeof_shareds: Size in bytes of array of pointers to shared vars accessed 10193041982dSJonathan Peyton // in task. 10207cc577a4SJonathan Peyton // task_entry: Pointer to task code entry point generated by compiler. 10217cc577a4SJonathan Peyton // returns: a pointer to the allocated kmp_task_t structure (task). 10223041982dSJonathan Peyton kmp_task_t *__kmp_task_alloc(ident_t *loc_ref, kmp_int32 gtid, 10233041982dSJonathan Peyton kmp_tasking_flags_t *flags, 10247cc577a4SJonathan Peyton size_t sizeof_kmp_task_t, size_t sizeof_shareds, 10253041982dSJonathan Peyton kmp_routine_entry_t task_entry) { 10267cc577a4SJonathan Peyton kmp_task_t *task; 10277cc577a4SJonathan Peyton kmp_taskdata_t *taskdata; 10287cc577a4SJonathan Peyton kmp_info_t *thread = __kmp_threads[gtid]; 10297cc577a4SJonathan Peyton kmp_team_t *team = thread->th.th_team; 10307cc577a4SJonathan Peyton kmp_taskdata_t *parent_task = thread->th.th_current_task; 10317cc577a4SJonathan Peyton size_t shareds_offset; 10327cc577a4SJonathan Peyton 10337cc577a4SJonathan Peyton KA_TRACE(10, ("__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) " 10347cc577a4SJonathan Peyton "sizeof_task=%ld sizeof_shared=%ld entry=%p\n", 10357cc577a4SJonathan Peyton gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t, 10367cc577a4SJonathan Peyton sizeof_shareds, task_entry)); 10377cc577a4SJonathan Peyton 10387cc577a4SJonathan Peyton if (parent_task->td_flags.final) { 10397cc577a4SJonathan Peyton if (flags->merged_if0) { 10407cc577a4SJonathan Peyton } 10417cc577a4SJonathan Peyton flags->final = 1; 10427cc577a4SJonathan Peyton } 10437cc577a4SJonathan Peyton 10447cc577a4SJonathan Peyton #if OMP_45_ENABLED 10457cc577a4SJonathan Peyton if (flags->proxy == TASK_PROXY) { 10467cc577a4SJonathan Peyton flags->tiedness = TASK_UNTIED; 10477cc577a4SJonathan Peyton flags->merged_if0 = 1; 10487cc577a4SJonathan Peyton 10493041982dSJonathan Peyton /* are we running in a sequential parallel or tskm_immediate_exec... we need 10503041982dSJonathan Peyton tasking support enabled */ 10517cc577a4SJonathan Peyton if ((thread->th.th_task_team) == NULL) { 10527cc577a4SJonathan Peyton /* This should only happen if the team is serialized 10533041982dSJonathan Peyton setup a task team and propagate it to the thread */ 10547cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(team->t.t_serialized); 10553041982dSJonathan Peyton KA_TRACE(30, 10563041982dSJonathan Peyton ("T#%d creating task team in __kmp_task_alloc for proxy task\n", 10573041982dSJonathan Peyton gtid)); 10583041982dSJonathan Peyton __kmp_task_team_setup( 10593041982dSJonathan Peyton thread, team, 10603041982dSJonathan Peyton 1); // 1 indicates setup the current team regardless of nthreads 10617cc577a4SJonathan Peyton thread->th.th_task_team = team->t.t_task_team[thread->th.th_task_state]; 10627cc577a4SJonathan Peyton } 10637cc577a4SJonathan Peyton kmp_task_team_t *task_team = thread->th.th_task_team; 10647cc577a4SJonathan Peyton 10657cc577a4SJonathan Peyton /* tasking must be enabled now as the task might not be pushed */ 10667cc577a4SJonathan Peyton if (!KMP_TASKING_ENABLED(task_team)) { 10673041982dSJonathan Peyton KA_TRACE( 10683041982dSJonathan Peyton 30, 10693041982dSJonathan Peyton ("T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid)); 10707cc577a4SJonathan Peyton __kmp_enable_tasking(task_team, thread); 10717cc577a4SJonathan Peyton kmp_int32 tid = thread->th.th_info.ds.ds_tid; 10727cc577a4SJonathan Peyton kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid]; 10737cc577a4SJonathan Peyton // No lock needed since only owner can allocate 10747cc577a4SJonathan Peyton if (thread_data->td.td_deque == NULL) { 10757cc577a4SJonathan Peyton __kmp_alloc_task_deque(thread, thread_data); 10767cc577a4SJonathan Peyton } 10777cc577a4SJonathan Peyton } 10787cc577a4SJonathan Peyton 10797cc577a4SJonathan Peyton if (task_team->tt.tt_found_proxy_tasks == FALSE) 10807cc577a4SJonathan Peyton TCW_4(task_team->tt.tt_found_proxy_tasks, TRUE); 10817cc577a4SJonathan Peyton } 10827cc577a4SJonathan Peyton #endif 10837cc577a4SJonathan Peyton 10847cc577a4SJonathan Peyton // Calculate shared structure offset including padding after kmp_task_t struct 10857cc577a4SJonathan Peyton // to align pointers in shared struct 10867cc577a4SJonathan Peyton shareds_offset = sizeof(kmp_taskdata_t) + sizeof_kmp_task_t; 10877cc577a4SJonathan Peyton shareds_offset = __kmp_round_up_to_val(shareds_offset, sizeof(void *)); 10887cc577a4SJonathan Peyton 10897cc577a4SJonathan Peyton // Allocate a kmp_taskdata_t block and a kmp_task_t block. 10903041982dSJonathan Peyton KA_TRACE(30, ("__kmp_task_alloc: T#%d First malloc size: %ld\n", gtid, 10913041982dSJonathan Peyton shareds_offset)); 10923041982dSJonathan Peyton KA_TRACE(30, ("__kmp_task_alloc: T#%d Second malloc size: %ld\n", gtid, 10933041982dSJonathan Peyton sizeof_shareds)); 10947cc577a4SJonathan Peyton 10957cc577a4SJonathan Peyton // Avoid double allocation here by combining shareds with taskdata 10967cc577a4SJonathan Peyton #if USE_FAST_MEMORY 10973041982dSJonathan Peyton taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, shareds_offset + 10983041982dSJonathan Peyton sizeof_shareds); 10997cc577a4SJonathan Peyton #else /* ! USE_FAST_MEMORY */ 11003041982dSJonathan Peyton taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, shareds_offset + 11013041982dSJonathan Peyton sizeof_shareds); 11027cc577a4SJonathan Peyton #endif /* USE_FAST_MEMORY */ 11037cc577a4SJonathan Peyton ANNOTATE_HAPPENS_AFTER(taskdata); 11047cc577a4SJonathan Peyton 11057cc577a4SJonathan Peyton task = KMP_TASKDATA_TO_TASK(taskdata); 11067cc577a4SJonathan Peyton 11077cc577a4SJonathan Peyton // Make sure task & taskdata are aligned appropriately 11087cc577a4SJonathan Peyton #if KMP_ARCH_X86 || KMP_ARCH_PPC64 || !KMP_HAVE_QUAD 11097cc577a4SJonathan Peyton KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (sizeof(double) - 1)) == 0); 11107cc577a4SJonathan Peyton KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (sizeof(double) - 1)) == 0); 11117cc577a4SJonathan Peyton #else 11127cc577a4SJonathan Peyton KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (sizeof(_Quad) - 1)) == 0); 11137cc577a4SJonathan Peyton KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (sizeof(_Quad) - 1)) == 0); 11147cc577a4SJonathan Peyton #endif 11157cc577a4SJonathan Peyton if (sizeof_shareds > 0) { 11167cc577a4SJonathan Peyton // Avoid double allocation here by combining shareds with taskdata 11177cc577a4SJonathan Peyton task->shareds = &((char *)taskdata)[shareds_offset]; 11187cc577a4SJonathan Peyton // Make sure shareds struct is aligned to pointer size 11193041982dSJonathan Peyton KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (sizeof(void *) - 1)) == 11203041982dSJonathan Peyton 0); 11217cc577a4SJonathan Peyton } else { 11227cc577a4SJonathan Peyton task->shareds = NULL; 11237cc577a4SJonathan Peyton } 11247cc577a4SJonathan Peyton task->routine = task_entry; 11257cc577a4SJonathan Peyton task->part_id = 0; // AC: Always start with 0 part id 11267cc577a4SJonathan Peyton 11277cc577a4SJonathan Peyton taskdata->td_task_id = KMP_GEN_TASK_ID(); 11287cc577a4SJonathan Peyton taskdata->td_team = team; 11297cc577a4SJonathan Peyton taskdata->td_alloc_thread = thread; 11307cc577a4SJonathan Peyton taskdata->td_parent = parent_task; 11317cc577a4SJonathan Peyton taskdata->td_level = parent_task->td_level + 1; // increment nesting level 11327cc577a4SJonathan Peyton taskdata->td_untied_count = 0; 11337cc577a4SJonathan Peyton taskdata->td_ident = loc_ref; 11347cc577a4SJonathan Peyton taskdata->td_taskwait_ident = NULL; 11357cc577a4SJonathan Peyton taskdata->td_taskwait_counter = 0; 11367cc577a4SJonathan Peyton taskdata->td_taskwait_thread = 0; 11377cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(taskdata->td_parent != NULL); 11387cc577a4SJonathan Peyton #if OMP_45_ENABLED 11397cc577a4SJonathan Peyton // avoid copying icvs for proxy tasks 11407cc577a4SJonathan Peyton if (flags->proxy == TASK_FULL) 11417cc577a4SJonathan Peyton #endif 11427cc577a4SJonathan Peyton copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs); 11437cc577a4SJonathan Peyton 11447cc577a4SJonathan Peyton taskdata->td_flags.tiedness = flags->tiedness; 11457cc577a4SJonathan Peyton taskdata->td_flags.final = flags->final; 11467cc577a4SJonathan Peyton taskdata->td_flags.merged_if0 = flags->merged_if0; 11477cc577a4SJonathan Peyton #if OMP_40_ENABLED 11487cc577a4SJonathan Peyton taskdata->td_flags.destructors_thunk = flags->destructors_thunk; 11497cc577a4SJonathan Peyton #endif // OMP_40_ENABLED 11507cc577a4SJonathan Peyton #if OMP_45_ENABLED 11517cc577a4SJonathan Peyton taskdata->td_flags.proxy = flags->proxy; 11527cc577a4SJonathan Peyton taskdata->td_task_team = thread->th.th_task_team; 11537cc577a4SJonathan Peyton taskdata->td_size_alloc = shareds_offset + sizeof_shareds; 11547cc577a4SJonathan Peyton #endif 11557cc577a4SJonathan Peyton taskdata->td_flags.tasktype = TASK_EXPLICIT; 11567cc577a4SJonathan Peyton 11577cc577a4SJonathan Peyton // GEH - TODO: fix this to copy parent task's value of tasking_ser flag 11587cc577a4SJonathan Peyton taskdata->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec); 11597cc577a4SJonathan Peyton 11607cc577a4SJonathan Peyton // GEH - TODO: fix this to copy parent task's value of team_serial flag 11617cc577a4SJonathan Peyton taskdata->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0; 11627cc577a4SJonathan Peyton 11633041982dSJonathan Peyton // GEH - Note we serialize the task if the team is serialized to make sure 11643041982dSJonathan Peyton // implicit parallel region tasks are not left until program termination to 11653041982dSJonathan Peyton // execute. Also, it helps locality to execute immediately. 11663041982dSJonathan Peyton 11673041982dSJonathan Peyton taskdata->td_flags.task_serial = 11683041982dSJonathan Peyton (parent_task->td_flags.final || taskdata->td_flags.team_serial || 11693041982dSJonathan Peyton taskdata->td_flags.tasking_ser); 11707cc577a4SJonathan Peyton 11717cc577a4SJonathan Peyton taskdata->td_flags.started = 0; 11727cc577a4SJonathan Peyton taskdata->td_flags.executing = 0; 11737cc577a4SJonathan Peyton taskdata->td_flags.complete = 0; 11747cc577a4SJonathan Peyton taskdata->td_flags.freed = 0; 11757cc577a4SJonathan Peyton 11767cc577a4SJonathan Peyton taskdata->td_flags.native = flags->native; 11777cc577a4SJonathan Peyton 11787cc577a4SJonathan Peyton taskdata->td_incomplete_child_tasks = 0; 11793041982dSJonathan Peyton taskdata->td_allocated_child_tasks = 1; // start at one because counts current 11803041982dSJonathan Peyton // task and children 11817cc577a4SJonathan Peyton #if OMP_40_ENABLED 11823041982dSJonathan Peyton taskdata->td_taskgroup = 11833041982dSJonathan Peyton parent_task->td_taskgroup; // task inherits taskgroup from the parent task 11847cc577a4SJonathan Peyton taskdata->td_dephash = NULL; 11857cc577a4SJonathan Peyton taskdata->td_depnode = NULL; 11867cc577a4SJonathan Peyton #endif 11877cc577a4SJonathan Peyton 11883041982dSJonathan Peyton // Only need to keep track of child task counts if team parallel and tasking not 11893041982dSJonathan Peyton // serialized or if it is a proxy task 11907cc577a4SJonathan Peyton #if OMP_45_ENABLED 11913041982dSJonathan Peyton if (flags->proxy == TASK_PROXY || 11923041982dSJonathan Peyton !(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) 11937cc577a4SJonathan Peyton #else 11947cc577a4SJonathan Peyton if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) 11957cc577a4SJonathan Peyton #endif 11967cc577a4SJonathan Peyton { 11975ba90c79SAndrey Churbanov KMP_TEST_THEN_INC32(&parent_task->td_incomplete_child_tasks); 11987cc577a4SJonathan Peyton #if OMP_40_ENABLED 11997cc577a4SJonathan Peyton if (parent_task->td_taskgroup) 12007cc577a4SJonathan Peyton KMP_TEST_THEN_INC32((kmp_int32 *)(&parent_task->td_taskgroup->count)); 12017cc577a4SJonathan Peyton #endif 12023041982dSJonathan Peyton // Only need to keep track of allocated child tasks for explicit tasks since 12033041982dSJonathan Peyton // implicit not deallocated 12047cc577a4SJonathan Peyton if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) { 12055ba90c79SAndrey Churbanov KMP_TEST_THEN_INC32(&taskdata->td_parent->td_allocated_child_tasks); 12067cc577a4SJonathan Peyton } 12077cc577a4SJonathan Peyton } 12087cc577a4SJonathan Peyton 12097cc577a4SJonathan Peyton KA_TRACE(20, ("__kmp_task_alloc(exit): T#%d created task %p parent=%p\n", 12107cc577a4SJonathan Peyton gtid, taskdata, taskdata->td_parent)); 12117cc577a4SJonathan Peyton ANNOTATE_HAPPENS_BEFORE(task); 12127cc577a4SJonathan Peyton 12137cc577a4SJonathan Peyton #if OMPT_SUPPORT 1214*82e94a59SJoachim Protze if (UNLIKELY(ompt_enabled.enabled)) 1215*82e94a59SJoachim Protze __ompt_task_init(taskdata, gtid); 12167cc577a4SJonathan Peyton #endif 12177cc577a4SJonathan Peyton 12187cc577a4SJonathan Peyton return task; 12197cc577a4SJonathan Peyton } 12207cc577a4SJonathan Peyton 12213041982dSJonathan Peyton kmp_task_t *__kmpc_omp_task_alloc(ident_t *loc_ref, kmp_int32 gtid, 12223041982dSJonathan Peyton kmp_int32 flags, size_t sizeof_kmp_task_t, 12233041982dSJonathan Peyton size_t sizeof_shareds, 12243041982dSJonathan Peyton kmp_routine_entry_t task_entry) { 12257cc577a4SJonathan Peyton kmp_task_t *retval; 12267cc577a4SJonathan Peyton kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags; 12277cc577a4SJonathan Peyton 12287cc577a4SJonathan Peyton input_flags->native = FALSE; 12297cc577a4SJonathan Peyton // __kmp_task_alloc() sets up all other runtime flags 12307cc577a4SJonathan Peyton 12317cc577a4SJonathan Peyton #if OMP_45_ENABLED 12327cc577a4SJonathan Peyton KA_TRACE(10, ("__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s) " 12337cc577a4SJonathan Peyton "sizeof_task=%ld sizeof_shared=%ld entry=%p\n", 12347cc577a4SJonathan Peyton gtid, loc_ref, input_flags->tiedness ? "tied " : "untied", 12353041982dSJonathan Peyton input_flags->proxy ? "proxy" : "", sizeof_kmp_task_t, 12363041982dSJonathan Peyton sizeof_shareds, task_entry)); 12377cc577a4SJonathan Peyton #else 12387cc577a4SJonathan Peyton KA_TRACE(10, ("__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s) " 12397cc577a4SJonathan Peyton "sizeof_task=%ld sizeof_shared=%ld entry=%p\n", 12407cc577a4SJonathan Peyton gtid, loc_ref, input_flags->tiedness ? "tied " : "untied", 12417cc577a4SJonathan Peyton sizeof_kmp_task_t, sizeof_shareds, task_entry)); 12427cc577a4SJonathan Peyton #endif 12437cc577a4SJonathan Peyton 12447cc577a4SJonathan Peyton retval = __kmp_task_alloc(loc_ref, gtid, input_flags, sizeof_kmp_task_t, 12457cc577a4SJonathan Peyton sizeof_shareds, task_entry); 12467cc577a4SJonathan Peyton 12477cc577a4SJonathan Peyton KA_TRACE(20, ("__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval)); 12487cc577a4SJonathan Peyton 12497cc577a4SJonathan Peyton return retval; 12507cc577a4SJonathan Peyton } 12517cc577a4SJonathan Peyton 12527cc577a4SJonathan Peyton // __kmp_invoke_task: invoke the specified task 12537cc577a4SJonathan Peyton // 12547cc577a4SJonathan Peyton // gtid: global thread ID of caller 12557cc577a4SJonathan Peyton // task: the task to invoke 12567cc577a4SJonathan Peyton // current_task: the task to resume after task invokation 12573041982dSJonathan Peyton static void __kmp_invoke_task(kmp_int32 gtid, kmp_task_t *task, 12583041982dSJonathan Peyton kmp_taskdata_t *current_task) { 12597cc577a4SJonathan Peyton kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task); 12607cc577a4SJonathan Peyton kmp_uint64 cur_time; 12617cc577a4SJonathan Peyton #if OMP_40_ENABLED 12627cc577a4SJonathan Peyton int discard = 0 /* false */; 12637cc577a4SJonathan Peyton #endif 12643041982dSJonathan Peyton KA_TRACE( 12653041982dSJonathan Peyton 30, ("__kmp_invoke_task(enter): T#%d invoking task %p, current_task=%p\n", 12667cc577a4SJonathan Peyton gtid, taskdata, current_task)); 12677cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(task); 12687cc577a4SJonathan Peyton #if OMP_45_ENABLED 12697cc577a4SJonathan Peyton if (taskdata->td_flags.proxy == TASK_PROXY && 12703041982dSJonathan Peyton taskdata->td_flags.complete == 1) { 12717cc577a4SJonathan Peyton // This is a proxy task that was already completed but it needs to run 12727cc577a4SJonathan Peyton // its bottom-half finish 12733041982dSJonathan Peyton KA_TRACE( 12743041982dSJonathan Peyton 30, 12753041982dSJonathan Peyton ("__kmp_invoke_task: T#%d running bottom finish for proxy task %p\n", 12767cc577a4SJonathan Peyton gtid, taskdata)); 12777cc577a4SJonathan Peyton 12787cc577a4SJonathan Peyton __kmp_bottom_half_finish_proxy(gtid, task); 12797cc577a4SJonathan Peyton 12803041982dSJonathan Peyton KA_TRACE(30, ("__kmp_invoke_task(exit): T#%d completed bottom finish for " 12813041982dSJonathan Peyton "proxy task %p, resuming task %p\n", 12823041982dSJonathan Peyton gtid, taskdata, current_task)); 12837cc577a4SJonathan Peyton 12847cc577a4SJonathan Peyton return; 12857cc577a4SJonathan Peyton } 12867cc577a4SJonathan Peyton #endif 12877cc577a4SJonathan Peyton 12887cc577a4SJonathan Peyton #if USE_ITT_BUILD && USE_ITT_NOTIFY 12897cc577a4SJonathan Peyton if (__kmp_forkjoin_frames_mode == 3) { 12903041982dSJonathan Peyton // Get the current time stamp to measure task execution time to correct 12913041982dSJonathan Peyton // barrier imbalance time 12927cc577a4SJonathan Peyton cur_time = __itt_get_timestamp(); 12937cc577a4SJonathan Peyton } 12947cc577a4SJonathan Peyton #endif 12957cc577a4SJonathan Peyton 12967cc577a4SJonathan Peyton #if OMP_45_ENABLED 12977cc577a4SJonathan Peyton // Proxy tasks are not handled by the runtime 12987cc577a4SJonathan Peyton if (taskdata->td_flags.proxy != TASK_PROXY) { 12997cc577a4SJonathan Peyton #endif 13007cc577a4SJonathan Peyton ANNOTATE_HAPPENS_AFTER(task); 1301*82e94a59SJoachim Protze __kmp_task_start(gtid, task, current_task); // OMPT only if not discarded 13027cc577a4SJonathan Peyton #if OMP_45_ENABLED 13037cc577a4SJonathan Peyton } 13047cc577a4SJonathan Peyton #endif 13057cc577a4SJonathan Peyton 13067cc577a4SJonathan Peyton #if OMPT_SUPPORT 13077cc577a4SJonathan Peyton ompt_thread_info_t oldInfo; 13087cc577a4SJonathan Peyton kmp_info_t *thread; 1309*82e94a59SJoachim Protze if (UNLIKELY(ompt_enabled.enabled)) { 13107cc577a4SJonathan Peyton // Store the threads states and restore them after the task 13117cc577a4SJonathan Peyton thread = __kmp_threads[gtid]; 13127cc577a4SJonathan Peyton oldInfo = thread->th.ompt_thread_info; 13137cc577a4SJonathan Peyton thread->th.ompt_thread_info.wait_id = 0; 1314*82e94a59SJoachim Protze thread->th.ompt_thread_info.state = (thread->th.th_team_serialized) 1315*82e94a59SJoachim Protze ? omp_state_work_serial 1316*82e94a59SJoachim Protze : omp_state_work_parallel; 13173041982dSJonathan Peyton taskdata->ompt_task_info.frame.exit_runtime_frame = 1318*82e94a59SJoachim Protze OMPT_GET_FRAME_ADDRESS(0); 13197cc577a4SJonathan Peyton } 13207cc577a4SJonathan Peyton #endif 13217cc577a4SJonathan Peyton 13227cc577a4SJonathan Peyton #if OMP_40_ENABLED 13237cc577a4SJonathan Peyton // TODO: cancel tasks if the parallel region has also been cancelled 13247cc577a4SJonathan Peyton // TODO: check if this sequence can be hoisted above __kmp_task_start 13257cc577a4SJonathan Peyton // if cancellation has been enabled for this run ... 13267cc577a4SJonathan Peyton if (__kmp_omp_cancellation) { 13277cc577a4SJonathan Peyton kmp_info_t *this_thr = __kmp_threads[gtid]; 13287cc577a4SJonathan Peyton kmp_team_t *this_team = this_thr->th.th_team; 13297cc577a4SJonathan Peyton kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup; 13303041982dSJonathan Peyton if ((taskgroup && taskgroup->cancel_request) || 13313041982dSJonathan Peyton (this_team->t.t_cancel_request == cancel_parallel)) { 1332*82e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL 1333*82e94a59SJoachim Protze ompt_data_t *task_data; 1334*82e94a59SJoachim Protze if (UNLIKELY(ompt_enabled.ompt_callback_cancel)) { 1335*82e94a59SJoachim Protze __ompt_get_task_info_internal(0, NULL, &task_data, NULL, NULL, NULL); 1336*82e94a59SJoachim Protze ompt_callbacks.ompt_callback(ompt_callback_cancel)( 1337*82e94a59SJoachim Protze task_data, 1338*82e94a59SJoachim Protze ((taskgroup && taskgroup->cancel_request) ? ompt_cancel_taskgroup 1339*82e94a59SJoachim Protze : ompt_cancel_parallel) | 1340*82e94a59SJoachim Protze ompt_cancel_discarded_task, 1341*82e94a59SJoachim Protze NULL); 1342*82e94a59SJoachim Protze } 1343*82e94a59SJoachim Protze #endif 13447cc577a4SJonathan Peyton KMP_COUNT_BLOCK(TASK_cancelled); 13457cc577a4SJonathan Peyton // this task belongs to a task group and we need to cancel it 13467cc577a4SJonathan Peyton discard = 1 /* true */; 13477cc577a4SJonathan Peyton } 13487cc577a4SJonathan Peyton } 13497cc577a4SJonathan Peyton 13507cc577a4SJonathan Peyton // Invoke the task routine and pass in relevant data. 13517cc577a4SJonathan Peyton // Thunks generated by gcc take a different argument list. 13527cc577a4SJonathan Peyton if (!discard) { 13537cc577a4SJonathan Peyton #if KMP_STATS_ENABLED 13547cc577a4SJonathan Peyton KMP_COUNT_BLOCK(TASK_executed); 13557cc577a4SJonathan Peyton switch (KMP_GET_THREAD_STATE()) { 13563041982dSJonathan Peyton case FORK_JOIN_BARRIER: 13573041982dSJonathan Peyton KMP_PUSH_PARTITIONED_TIMER(OMP_task_join_bar); 13583041982dSJonathan Peyton break; 13593041982dSJonathan Peyton case PLAIN_BARRIER: 13603041982dSJonathan Peyton KMP_PUSH_PARTITIONED_TIMER(OMP_task_plain_bar); 13613041982dSJonathan Peyton break; 13623041982dSJonathan Peyton case TASKYIELD: 13633041982dSJonathan Peyton KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskyield); 13643041982dSJonathan Peyton break; 13653041982dSJonathan Peyton case TASKWAIT: 13663041982dSJonathan Peyton KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskwait); 13673041982dSJonathan Peyton break; 13683041982dSJonathan Peyton case TASKGROUP: 13693041982dSJonathan Peyton KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskgroup); 13703041982dSJonathan Peyton break; 13713041982dSJonathan Peyton default: 13723041982dSJonathan Peyton KMP_PUSH_PARTITIONED_TIMER(OMP_task_immediate); 13733041982dSJonathan Peyton break; 13747cc577a4SJonathan Peyton } 13757cc577a4SJonathan Peyton #endif // KMP_STATS_ENABLED 13767cc577a4SJonathan Peyton #endif // OMP_40_ENABLED 13777cc577a4SJonathan Peyton 1378*82e94a59SJoachim Protze // OMPT task begin 1379*82e94a59SJoachim Protze #if OMPT_SUPPORT 1380*82e94a59SJoachim Protze if (UNLIKELY(ompt_enabled.enabled)) 1381*82e94a59SJoachim Protze __ompt_task_start(task, current_task, gtid); 13827cc577a4SJonathan Peyton #endif 13837cc577a4SJonathan Peyton 13847cc577a4SJonathan Peyton #ifdef KMP_GOMP_COMPAT 13857cc577a4SJonathan Peyton if (taskdata->td_flags.native) { 13867cc577a4SJonathan Peyton ((void (*)(void *))(*(task->routine)))(task->shareds); 13873041982dSJonathan Peyton } else 13887cc577a4SJonathan Peyton #endif /* KMP_GOMP_COMPAT */ 13897cc577a4SJonathan Peyton { 13907cc577a4SJonathan Peyton (*(task->routine))(gtid, task); 13917cc577a4SJonathan Peyton } 13927cc577a4SJonathan Peyton KMP_POP_PARTITIONED_TIMER(); 13937cc577a4SJonathan Peyton 1394*82e94a59SJoachim Protze #if OMPT_SUPPORT 1395*82e94a59SJoachim Protze if (UNLIKELY(ompt_enabled.enabled)) 1396*82e94a59SJoachim Protze __ompt_task_finish(task, current_task); 13977cc577a4SJonathan Peyton #endif 13987cc577a4SJonathan Peyton #if OMP_40_ENABLED 13997cc577a4SJonathan Peyton } 14007cc577a4SJonathan Peyton #endif // OMP_40_ENABLED 14017cc577a4SJonathan Peyton 14027cc577a4SJonathan Peyton #if OMPT_SUPPORT 1403*82e94a59SJoachim Protze if (UNLIKELY(ompt_enabled.enabled)) { 14047cc577a4SJonathan Peyton thread->th.ompt_thread_info = oldInfo; 14057cc577a4SJonathan Peyton taskdata->ompt_task_info.frame.exit_runtime_frame = NULL; 14067cc577a4SJonathan Peyton } 14077cc577a4SJonathan Peyton #endif 14087cc577a4SJonathan Peyton 14097cc577a4SJonathan Peyton #if OMP_45_ENABLED 14107cc577a4SJonathan Peyton // Proxy tasks are not handled by the runtime 14117cc577a4SJonathan Peyton if (taskdata->td_flags.proxy != TASK_PROXY) { 14127cc577a4SJonathan Peyton #endif 14137cc577a4SJonathan Peyton ANNOTATE_HAPPENS_BEFORE(taskdata->td_parent); 1414*82e94a59SJoachim Protze __kmp_task_finish(gtid, task, current_task); // OMPT only if not discarded 14157cc577a4SJonathan Peyton #if OMP_45_ENABLED 14167cc577a4SJonathan Peyton } 14177cc577a4SJonathan Peyton #endif 14187cc577a4SJonathan Peyton 14197cc577a4SJonathan Peyton #if USE_ITT_BUILD && USE_ITT_NOTIFY 14207cc577a4SJonathan Peyton // Barrier imbalance - correct arrive time after the task finished 14217cc577a4SJonathan Peyton if (__kmp_forkjoin_frames_mode == 3) { 14227cc577a4SJonathan Peyton kmp_info_t *this_thr = __kmp_threads[gtid]; 14237cc577a4SJonathan Peyton if (this_thr->th.th_bar_arrive_time) { 14247cc577a4SJonathan Peyton this_thr->th.th_bar_arrive_time += (__itt_get_timestamp() - cur_time); 14257cc577a4SJonathan Peyton } 14267cc577a4SJonathan Peyton } 14277cc577a4SJonathan Peyton #endif 14283041982dSJonathan Peyton KA_TRACE( 14293041982dSJonathan Peyton 30, 14303041982dSJonathan Peyton ("__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n", 14317cc577a4SJonathan Peyton gtid, taskdata, current_task)); 14327cc577a4SJonathan Peyton return; 14337cc577a4SJonathan Peyton } 14347cc577a4SJonathan Peyton 14357cc577a4SJonathan Peyton // __kmpc_omp_task_parts: Schedule a thread-switchable task for execution 14367cc577a4SJonathan Peyton // 14377cc577a4SJonathan Peyton // loc_ref: location of original task pragma (ignored) 14387cc577a4SJonathan Peyton // gtid: Global Thread ID of encountering thread 14397cc577a4SJonathan Peyton // new_task: task thunk allocated by __kmp_omp_task_alloc() for the ''new task'' 14407cc577a4SJonathan Peyton // Returns: 14413041982dSJonathan Peyton // TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to 14423041982dSJonathan Peyton // be resumed later. 14433041982dSJonathan Peyton // TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be 14443041982dSJonathan Peyton // resumed later. 14453041982dSJonathan Peyton kmp_int32 __kmpc_omp_task_parts(ident_t *loc_ref, kmp_int32 gtid, 14463041982dSJonathan Peyton kmp_task_t *new_task) { 14477cc577a4SJonathan Peyton kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task); 14487cc577a4SJonathan Peyton 14493041982dSJonathan Peyton KA_TRACE(10, ("__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n", gtid, 14503041982dSJonathan Peyton loc_ref, new_taskdata)); 14517cc577a4SJonathan Peyton 1452*82e94a59SJoachim Protze #if OMPT_SUPPORT 1453*82e94a59SJoachim Protze kmp_taskdata_t *parent; 1454*82e94a59SJoachim Protze if (UNLIKELY(ompt_enabled.enabled)) { 1455*82e94a59SJoachim Protze parent = new_taskdata->td_parent; 1456*82e94a59SJoachim Protze if (ompt_enabled.ompt_callback_task_create) { 1457*82e94a59SJoachim Protze ompt_data_t task_data = ompt_data_none; 1458*82e94a59SJoachim Protze ompt_callbacks.ompt_callback(ompt_callback_task_create)( 1459*82e94a59SJoachim Protze parent ? &(parent->ompt_task_info.task_data) : &task_data, 1460*82e94a59SJoachim Protze parent ? &(parent->ompt_task_info.frame) : NULL, 1461*82e94a59SJoachim Protze &(new_taskdata->ompt_task_info.task_data), ompt_task_explicit, 0, 1462*82e94a59SJoachim Protze OMPT_GET_RETURN_ADDRESS(0)); 1463*82e94a59SJoachim Protze } 1464*82e94a59SJoachim Protze } 1465*82e94a59SJoachim Protze #endif 1466*82e94a59SJoachim Protze 14673041982dSJonathan Peyton /* Should we execute the new task or queue it? For now, let's just always try 14683041982dSJonathan Peyton to queue it. If the queue fills up, then we'll execute it. */ 14697cc577a4SJonathan Peyton 14707cc577a4SJonathan Peyton if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED) // if cannot defer 14717cc577a4SJonathan Peyton { // Execute this task immediately 14727cc577a4SJonathan Peyton kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task; 14737cc577a4SJonathan Peyton new_taskdata->td_flags.task_serial = 1; 14747cc577a4SJonathan Peyton __kmp_invoke_task(gtid, new_task, current_task); 14757cc577a4SJonathan Peyton } 14767cc577a4SJonathan Peyton 14773041982dSJonathan Peyton KA_TRACE( 14783041982dSJonathan Peyton 10, 14793041982dSJonathan Peyton ("__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: " 14803041982dSJonathan Peyton "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n", 14813041982dSJonathan Peyton gtid, loc_ref, new_taskdata)); 14827cc577a4SJonathan Peyton 14837cc577a4SJonathan Peyton ANNOTATE_HAPPENS_BEFORE(new_task); 1484*82e94a59SJoachim Protze #if OMPT_SUPPORT 1485*82e94a59SJoachim Protze if (UNLIKELY(ompt_enabled.enabled)) { 1486*82e94a59SJoachim Protze parent->ompt_task_info.frame.reenter_runtime_frame = NULL; 1487*82e94a59SJoachim Protze } 1488*82e94a59SJoachim Protze #endif 14897cc577a4SJonathan Peyton return TASK_CURRENT_NOT_QUEUED; 14907cc577a4SJonathan Peyton } 14917cc577a4SJonathan Peyton 14927cc577a4SJonathan Peyton // __kmp_omp_task: Schedule a non-thread-switchable task for execution 14933041982dSJonathan Peyton // 14947cc577a4SJonathan Peyton // gtid: Global Thread ID of encountering thread 14957cc577a4SJonathan Peyton // new_task:non-thread-switchable task thunk allocated by __kmp_omp_task_alloc() 14963041982dSJonathan Peyton // serialize_immediate: if TRUE then if the task is executed immediately its 14973041982dSJonathan Peyton // execution will be serialized 14983041982dSJonathan Peyton // Returns: 14993041982dSJonathan Peyton // TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to 15003041982dSJonathan Peyton // be resumed later. 15013041982dSJonathan Peyton // TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be 15023041982dSJonathan Peyton // resumed later. 15033041982dSJonathan Peyton kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task, 15043041982dSJonathan Peyton bool serialize_immediate) { 15057cc577a4SJonathan Peyton kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task); 15067cc577a4SJonathan Peyton 15077cc577a4SJonathan Peyton /* Should we execute the new task or queue it? For now, let's just always try to 15087cc577a4SJonathan Peyton queue it. If the queue fills up, then we'll execute it. */ 15097cc577a4SJonathan Peyton #if OMP_45_ENABLED 15103041982dSJonathan Peyton if (new_taskdata->td_flags.proxy == TASK_PROXY || 15113041982dSJonathan Peyton __kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED) // if cannot defer 15127cc577a4SJonathan Peyton #else 15137cc577a4SJonathan Peyton if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED) // if cannot defer 15147cc577a4SJonathan Peyton #endif 15157cc577a4SJonathan Peyton { // Execute this task immediately 15167cc577a4SJonathan Peyton kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task; 15177cc577a4SJonathan Peyton if (serialize_immediate) 15187cc577a4SJonathan Peyton new_taskdata->td_flags.task_serial = 1; 15197cc577a4SJonathan Peyton __kmp_invoke_task(gtid, new_task, current_task); 15207cc577a4SJonathan Peyton } 15217cc577a4SJonathan Peyton 15227cc577a4SJonathan Peyton ANNOTATE_HAPPENS_BEFORE(new_task); 15237cc577a4SJonathan Peyton return TASK_CURRENT_NOT_QUEUED; 15247cc577a4SJonathan Peyton } 15257cc577a4SJonathan Peyton 15263041982dSJonathan Peyton // __kmpc_omp_task: Wrapper around __kmp_omp_task to schedule a 15273041982dSJonathan Peyton // non-thread-switchable task from the parent thread only! 15283041982dSJonathan Peyton // 15297cc577a4SJonathan Peyton // loc_ref: location of original task pragma (ignored) 15307cc577a4SJonathan Peyton // gtid: Global Thread ID of encountering thread 15313041982dSJonathan Peyton // new_task: non-thread-switchable task thunk allocated by 15323041982dSJonathan Peyton // __kmp_omp_task_alloc() 15333041982dSJonathan Peyton // Returns: 15343041982dSJonathan Peyton // TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to 15353041982dSJonathan Peyton // be resumed later. 15363041982dSJonathan Peyton // TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be 15373041982dSJonathan Peyton // resumed later. 15383041982dSJonathan Peyton kmp_int32 __kmpc_omp_task(ident_t *loc_ref, kmp_int32 gtid, 15393041982dSJonathan Peyton kmp_task_t *new_task) { 15407cc577a4SJonathan Peyton kmp_int32 res; 15417cc577a4SJonathan Peyton KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK); 15427cc577a4SJonathan Peyton 1543*82e94a59SJoachim Protze #if KMP_DEBUG || OMPT_SUPPORT 15447cc577a4SJonathan Peyton kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task); 15457cc577a4SJonathan Peyton #endif 15463041982dSJonathan Peyton KA_TRACE(10, ("__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref, 15473041982dSJonathan Peyton new_taskdata)); 15487cc577a4SJonathan Peyton 1549*82e94a59SJoachim Protze #if OMPT_SUPPORT 1550*82e94a59SJoachim Protze kmp_taskdata_t *parent = NULL; 1551*82e94a59SJoachim Protze if (UNLIKELY(ompt_enabled.enabled && !new_taskdata->td_flags.started)) { 1552*82e94a59SJoachim Protze OMPT_STORE_RETURN_ADDRESS(gtid); 1553*82e94a59SJoachim Protze parent = new_taskdata->td_parent; 1554*82e94a59SJoachim Protze if (!parent->ompt_task_info.frame.reenter_runtime_frame) 1555*82e94a59SJoachim Protze parent->ompt_task_info.frame.reenter_runtime_frame = 1556*82e94a59SJoachim Protze OMPT_GET_FRAME_ADDRESS(1); 1557*82e94a59SJoachim Protze if (ompt_enabled.ompt_callback_task_create) { 1558*82e94a59SJoachim Protze ompt_data_t task_data = ompt_data_none; 1559*82e94a59SJoachim Protze ompt_callbacks.ompt_callback(ompt_callback_task_create)( 1560*82e94a59SJoachim Protze parent ? &(parent->ompt_task_info.task_data) : &task_data, 1561*82e94a59SJoachim Protze parent ? &(parent->ompt_task_info.frame) : NULL, 1562*82e94a59SJoachim Protze &(new_taskdata->ompt_task_info.task_data), 1563*82e94a59SJoachim Protze ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0, 1564*82e94a59SJoachim Protze OMPT_LOAD_RETURN_ADDRESS(gtid)); 1565*82e94a59SJoachim Protze } 1566*82e94a59SJoachim Protze } 1567*82e94a59SJoachim Protze #endif 1568*82e94a59SJoachim Protze 15697cc577a4SJonathan Peyton res = __kmp_omp_task(gtid, new_task, true); 15707cc577a4SJonathan Peyton 15713041982dSJonathan Peyton KA_TRACE(10, ("__kmpc_omp_task(exit): T#%d returning " 15723041982dSJonathan Peyton "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n", 15737cc577a4SJonathan Peyton gtid, loc_ref, new_taskdata)); 1574*82e94a59SJoachim Protze #if OMPT_SUPPORT 1575*82e94a59SJoachim Protze if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) { 1576*82e94a59SJoachim Protze parent->ompt_task_info.frame.reenter_runtime_frame = NULL; 1577*82e94a59SJoachim Protze } 1578*82e94a59SJoachim Protze #endif 15797cc577a4SJonathan Peyton return res; 15807cc577a4SJonathan Peyton } 15817cc577a4SJonathan Peyton 1582*82e94a59SJoachim Protze template <bool ompt> 1583*82e94a59SJoachim Protze static kmp_int32 __kmpc_omp_taskwait_template(ident_t *loc_ref, kmp_int32 gtid, 1584*82e94a59SJoachim Protze void *frame_address, 1585*82e94a59SJoachim Protze void *return_address) { 15867cc577a4SJonathan Peyton kmp_taskdata_t *taskdata; 15877cc577a4SJonathan Peyton kmp_info_t *thread; 15887cc577a4SJonathan Peyton int thread_finished = FALSE; 15897cc577a4SJonathan Peyton KMP_SET_THREAD_STATE_BLOCK(TASKWAIT); 15907cc577a4SJonathan Peyton 15917cc577a4SJonathan Peyton KA_TRACE(10, ("__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref)); 15927cc577a4SJonathan Peyton 15937cc577a4SJonathan Peyton if (__kmp_tasking_mode != tskm_immediate_exec) { 15947cc577a4SJonathan Peyton thread = __kmp_threads[gtid]; 15957cc577a4SJonathan Peyton taskdata = thread->th.th_current_task; 15967cc577a4SJonathan Peyton 1597*82e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL 1598*82e94a59SJoachim Protze ompt_data_t *my_task_data; 1599*82e94a59SJoachim Protze ompt_data_t *my_parallel_data; 16007cc577a4SJonathan Peyton 1601*82e94a59SJoachim Protze if (ompt) { 1602*82e94a59SJoachim Protze my_task_data = &(taskdata->ompt_task_info.task_data); 1603*82e94a59SJoachim Protze my_parallel_data = OMPT_CUR_TEAM_DATA(thread); 1604*82e94a59SJoachim Protze 1605*82e94a59SJoachim Protze taskdata->ompt_task_info.frame.reenter_runtime_frame = frame_address; 1606*82e94a59SJoachim Protze 1607*82e94a59SJoachim Protze if (ompt_enabled.ompt_callback_sync_region) { 1608*82e94a59SJoachim Protze ompt_callbacks.ompt_callback(ompt_callback_sync_region)( 1609*82e94a59SJoachim Protze ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data, 1610*82e94a59SJoachim Protze my_task_data, return_address); 1611*82e94a59SJoachim Protze } 1612*82e94a59SJoachim Protze 1613*82e94a59SJoachim Protze if (ompt_enabled.ompt_callback_sync_region_wait) { 1614*82e94a59SJoachim Protze ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)( 1615*82e94a59SJoachim Protze ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data, 1616*82e94a59SJoachim Protze my_task_data, return_address); 16177cc577a4SJonathan Peyton } 16187cc577a4SJonathan Peyton } 1619*82e94a59SJoachim Protze #endif // OMPT_SUPPORT && OMPT_OPTIONAL 16207cc577a4SJonathan Peyton 16213041982dSJonathan Peyton // Debugger: The taskwait is active. Store location and thread encountered the 16223041982dSJonathan Peyton // taskwait. 16237cc577a4SJonathan Peyton #if USE_ITT_BUILD 16247cc577a4SJonathan Peyton // Note: These values are used by ITT events as well. 16257cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */ 16267cc577a4SJonathan Peyton taskdata->td_taskwait_counter += 1; 16277cc577a4SJonathan Peyton taskdata->td_taskwait_ident = loc_ref; 16287cc577a4SJonathan Peyton taskdata->td_taskwait_thread = gtid + 1; 16297cc577a4SJonathan Peyton 16307cc577a4SJonathan Peyton #if USE_ITT_BUILD 16317cc577a4SJonathan Peyton void *itt_sync_obj = __kmp_itt_taskwait_object(gtid); 16327cc577a4SJonathan Peyton if (itt_sync_obj != NULL) 16337cc577a4SJonathan Peyton __kmp_itt_taskwait_starting(gtid, itt_sync_obj); 16347cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */ 16357cc577a4SJonathan Peyton 16363041982dSJonathan Peyton bool must_wait = 16373041982dSJonathan Peyton !taskdata->td_flags.team_serial && !taskdata->td_flags.final; 16387cc577a4SJonathan Peyton 16397cc577a4SJonathan Peyton #if OMP_45_ENABLED 16403041982dSJonathan Peyton must_wait = must_wait || (thread->th.th_task_team != NULL && 16413041982dSJonathan Peyton thread->th.th_task_team->tt.tt_found_proxy_tasks); 16427cc577a4SJonathan Peyton #endif 16433041982dSJonathan Peyton if (must_wait) { 1644c47afcd9SAndrey Churbanov kmp_flag_32 flag( 1645c47afcd9SAndrey Churbanov RCAST(volatile kmp_uint32 *, &taskdata->td_incomplete_child_tasks), 1646c47afcd9SAndrey Churbanov 0U); 16477cc577a4SJonathan Peyton while (TCR_4(taskdata->td_incomplete_child_tasks) != 0) { 16483041982dSJonathan Peyton flag.execute_tasks(thread, gtid, FALSE, 16493041982dSJonathan Peyton &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), 16503041982dSJonathan Peyton __kmp_task_stealing_constraint); 16517cc577a4SJonathan Peyton } 16527cc577a4SJonathan Peyton } 16537cc577a4SJonathan Peyton #if USE_ITT_BUILD 16547cc577a4SJonathan Peyton if (itt_sync_obj != NULL) 16557cc577a4SJonathan Peyton __kmp_itt_taskwait_finished(gtid, itt_sync_obj); 16567cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */ 16577cc577a4SJonathan Peyton 16583041982dSJonathan Peyton // Debugger: The taskwait is completed. Location remains, but thread is 16593041982dSJonathan Peyton // negated. 16607cc577a4SJonathan Peyton taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread; 16617cc577a4SJonathan Peyton 1662*82e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL 1663*82e94a59SJoachim Protze if (ompt) { 1664*82e94a59SJoachim Protze if (ompt_enabled.ompt_callback_sync_region_wait) { 1665*82e94a59SJoachim Protze ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)( 1666*82e94a59SJoachim Protze ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data, 1667*82e94a59SJoachim Protze my_task_data, return_address); 1668*82e94a59SJoachim Protze } 1669*82e94a59SJoachim Protze if (ompt_enabled.ompt_callback_sync_region) { 1670*82e94a59SJoachim Protze ompt_callbacks.ompt_callback(ompt_callback_sync_region)( 1671*82e94a59SJoachim Protze ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data, 1672*82e94a59SJoachim Protze my_task_data, return_address); 16737cc577a4SJonathan Peyton } 16747cc577a4SJonathan Peyton taskdata->ompt_task_info.frame.reenter_runtime_frame = NULL; 16757cc577a4SJonathan Peyton } 1676*82e94a59SJoachim Protze #endif // OMPT_SUPPORT && OMPT_OPTIONAL 1677*82e94a59SJoachim Protze 16787cc577a4SJonathan Peyton ANNOTATE_HAPPENS_AFTER(taskdata); 16797cc577a4SJonathan Peyton } 16807cc577a4SJonathan Peyton 16817cc577a4SJonathan Peyton KA_TRACE(10, ("__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, " 16823041982dSJonathan Peyton "returning TASK_CURRENT_NOT_QUEUED\n", 16833041982dSJonathan Peyton gtid, taskdata)); 16847cc577a4SJonathan Peyton 16857cc577a4SJonathan Peyton return TASK_CURRENT_NOT_QUEUED; 16867cc577a4SJonathan Peyton } 16877cc577a4SJonathan Peyton 1688*82e94a59SJoachim Protze #if OMPT_SUPPORT 1689*82e94a59SJoachim Protze OMPT_NOINLINE 1690*82e94a59SJoachim Protze static kmp_int32 __kmpc_omp_taskwait_ompt(ident_t *loc_ref, kmp_int32 gtid, 1691*82e94a59SJoachim Protze void *frame_address, 1692*82e94a59SJoachim Protze void *return_address) { 1693*82e94a59SJoachim Protze return __kmpc_omp_taskwait_template<true>(loc_ref, gtid, frame_address, 1694*82e94a59SJoachim Protze return_address); 1695*82e94a59SJoachim Protze } 1696*82e94a59SJoachim Protze #endif // OMPT_SUPPORT 1697*82e94a59SJoachim Protze 1698*82e94a59SJoachim Protze // __kmpc_omp_taskwait: Wait until all tasks generated by the current task are 1699*82e94a59SJoachim Protze // complete 1700*82e94a59SJoachim Protze kmp_int32 __kmpc_omp_taskwait(ident_t *loc_ref, kmp_int32 gtid) { 1701*82e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL 1702*82e94a59SJoachim Protze if (UNLIKELY(ompt_enabled.enabled)) { 1703*82e94a59SJoachim Protze OMPT_STORE_RETURN_ADDRESS(gtid); 1704*82e94a59SJoachim Protze return __kmpc_omp_taskwait_ompt(loc_ref, gtid, OMPT_GET_FRAME_ADDRESS(1), 1705*82e94a59SJoachim Protze OMPT_LOAD_RETURN_ADDRESS(gtid)); 1706*82e94a59SJoachim Protze } 1707*82e94a59SJoachim Protze #endif 1708*82e94a59SJoachim Protze return __kmpc_omp_taskwait_template<false>(loc_ref, gtid, NULL, NULL); 1709*82e94a59SJoachim Protze } 1710*82e94a59SJoachim Protze 17117cc577a4SJonathan Peyton // __kmpc_omp_taskyield: switch to a different task 17123041982dSJonathan Peyton kmp_int32 __kmpc_omp_taskyield(ident_t *loc_ref, kmp_int32 gtid, int end_part) { 17137cc577a4SJonathan Peyton kmp_taskdata_t *taskdata; 17147cc577a4SJonathan Peyton kmp_info_t *thread; 17157cc577a4SJonathan Peyton int thread_finished = FALSE; 17167cc577a4SJonathan Peyton 17177cc577a4SJonathan Peyton KMP_COUNT_BLOCK(OMP_TASKYIELD); 17187cc577a4SJonathan Peyton KMP_SET_THREAD_STATE_BLOCK(TASKYIELD); 17197cc577a4SJonathan Peyton 17207cc577a4SJonathan Peyton KA_TRACE(10, ("__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n", 17217cc577a4SJonathan Peyton gtid, loc_ref, end_part)); 17227cc577a4SJonathan Peyton 17237cc577a4SJonathan Peyton if (__kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel) { 17247cc577a4SJonathan Peyton thread = __kmp_threads[gtid]; 17257cc577a4SJonathan Peyton taskdata = thread->th.th_current_task; 17267cc577a4SJonathan Peyton // Should we model this as a task wait or not? 17273041982dSJonathan Peyton // Debugger: The taskwait is active. Store location and thread encountered the 17283041982dSJonathan Peyton // taskwait. 17297cc577a4SJonathan Peyton #if USE_ITT_BUILD 17307cc577a4SJonathan Peyton // Note: These values are used by ITT events as well. 17317cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */ 17327cc577a4SJonathan Peyton taskdata->td_taskwait_counter += 1; 17337cc577a4SJonathan Peyton taskdata->td_taskwait_ident = loc_ref; 17347cc577a4SJonathan Peyton taskdata->td_taskwait_thread = gtid + 1; 17357cc577a4SJonathan Peyton 17367cc577a4SJonathan Peyton #if USE_ITT_BUILD 17377cc577a4SJonathan Peyton void *itt_sync_obj = __kmp_itt_taskwait_object(gtid); 17387cc577a4SJonathan Peyton if (itt_sync_obj != NULL) 17397cc577a4SJonathan Peyton __kmp_itt_taskwait_starting(gtid, itt_sync_obj); 17407cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */ 17417cc577a4SJonathan Peyton if (!taskdata->td_flags.team_serial) { 17427cc577a4SJonathan Peyton kmp_task_team_t *task_team = thread->th.th_task_team; 17437cc577a4SJonathan Peyton if (task_team != NULL) { 17447cc577a4SJonathan Peyton if (KMP_TASKING_ENABLED(task_team)) { 1745*82e94a59SJoachim Protze #if OMPT_SUPPORT 1746*82e94a59SJoachim Protze if (UNLIKELY(ompt_enabled.enabled)) 1747*82e94a59SJoachim Protze thread->th.ompt_thread_info.ompt_task_yielded = 1; 1748*82e94a59SJoachim Protze #endif 17493041982dSJonathan Peyton __kmp_execute_tasks_32( 17503041982dSJonathan Peyton thread, gtid, NULL, FALSE, 17513041982dSJonathan Peyton &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), 17523041982dSJonathan Peyton __kmp_task_stealing_constraint); 1753*82e94a59SJoachim Protze #if OMPT_SUPPORT 1754*82e94a59SJoachim Protze if (UNLIKELY(ompt_enabled.enabled)) 1755*82e94a59SJoachim Protze thread->th.ompt_thread_info.ompt_task_yielded = 0; 1756*82e94a59SJoachim Protze #endif 17577cc577a4SJonathan Peyton } 17587cc577a4SJonathan Peyton } 17597cc577a4SJonathan Peyton } 17607cc577a4SJonathan Peyton #if USE_ITT_BUILD 17617cc577a4SJonathan Peyton if (itt_sync_obj != NULL) 17627cc577a4SJonathan Peyton __kmp_itt_taskwait_finished(gtid, itt_sync_obj); 17637cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */ 17647cc577a4SJonathan Peyton 17653041982dSJonathan Peyton // Debugger: The taskwait is completed. Location remains, but thread is 17663041982dSJonathan Peyton // negated. 17677cc577a4SJonathan Peyton taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread; 17687cc577a4SJonathan Peyton } 17697cc577a4SJonathan Peyton 17707cc577a4SJonathan Peyton KA_TRACE(10, ("__kmpc_omp_taskyield(exit): T#%d task %p resuming, " 17713041982dSJonathan Peyton "returning TASK_CURRENT_NOT_QUEUED\n", 17723041982dSJonathan Peyton gtid, taskdata)); 17737cc577a4SJonathan Peyton 17747cc577a4SJonathan Peyton return TASK_CURRENT_NOT_QUEUED; 17757cc577a4SJonathan Peyton } 17767cc577a4SJonathan Peyton 177772ba2109SAndrey Churbanov // TODO: change to OMP_50_ENABLED, need to change build tools for this to work 177872ba2109SAndrey Churbanov #if OMP_45_ENABLED 177972ba2109SAndrey Churbanov // Task Reduction implementation 178072ba2109SAndrey Churbanov 178172ba2109SAndrey Churbanov typedef struct kmp_task_red_flags { 178272ba2109SAndrey Churbanov unsigned lazy_priv : 1; // hint: (1) use lazy allocation (big objects) 178372ba2109SAndrey Churbanov unsigned reserved31 : 31; 178472ba2109SAndrey Churbanov } kmp_task_red_flags_t; 178572ba2109SAndrey Churbanov 178672ba2109SAndrey Churbanov // internal structure for reduction data item related info 178772ba2109SAndrey Churbanov typedef struct kmp_task_red_data { 178872ba2109SAndrey Churbanov void *reduce_shar; // shared reduction item 178972ba2109SAndrey Churbanov size_t reduce_size; // size of data item 179072ba2109SAndrey Churbanov void *reduce_priv; // thread specific data 179172ba2109SAndrey Churbanov void *reduce_pend; // end of private data for comparison op 179272ba2109SAndrey Churbanov void *reduce_init; // data initialization routine 179372ba2109SAndrey Churbanov void *reduce_fini; // data finalization routine 179472ba2109SAndrey Churbanov void *reduce_comb; // data combiner routine 179572ba2109SAndrey Churbanov kmp_task_red_flags_t flags; // flags for additional info from compiler 179672ba2109SAndrey Churbanov } kmp_task_red_data_t; 179772ba2109SAndrey Churbanov 179872ba2109SAndrey Churbanov // structure sent us by compiler - one per reduction item 179972ba2109SAndrey Churbanov typedef struct kmp_task_red_input { 180072ba2109SAndrey Churbanov void *reduce_shar; // shared reduction item 180172ba2109SAndrey Churbanov size_t reduce_size; // size of data item 180272ba2109SAndrey Churbanov void *reduce_init; // data initialization routine 180372ba2109SAndrey Churbanov void *reduce_fini; // data finalization routine 180472ba2109SAndrey Churbanov void *reduce_comb; // data combiner routine 180572ba2109SAndrey Churbanov kmp_task_red_flags_t flags; // flags for additional info from compiler 180672ba2109SAndrey Churbanov } kmp_task_red_input_t; 180772ba2109SAndrey Churbanov 180872ba2109SAndrey Churbanov /*! 180972ba2109SAndrey Churbanov @ingroup TASKING 181072ba2109SAndrey Churbanov @param gtid Global thread ID 181172ba2109SAndrey Churbanov @param num Number of data items to reduce 181272ba2109SAndrey Churbanov @param data Array of data for reduction 181372ba2109SAndrey Churbanov @return The taskgroup identifier 181472ba2109SAndrey Churbanov 181572ba2109SAndrey Churbanov Initialize task reduction for the taskgroup. 181672ba2109SAndrey Churbanov */ 18173041982dSJonathan Peyton void *__kmpc_task_reduction_init(int gtid, int num, void *data) { 181872ba2109SAndrey Churbanov kmp_info_t *thread = __kmp_threads[gtid]; 181972ba2109SAndrey Churbanov kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup; 182072ba2109SAndrey Churbanov kmp_int32 nth = thread->th.th_team_nproc; 182172ba2109SAndrey Churbanov kmp_task_red_input_t *input = (kmp_task_red_input_t *)data; 182272ba2109SAndrey Churbanov kmp_task_red_data_t *arr; 182372ba2109SAndrey Churbanov 182472ba2109SAndrey Churbanov // check input data just in case 182572ba2109SAndrey Churbanov KMP_ASSERT(tg != NULL); 182672ba2109SAndrey Churbanov KMP_ASSERT(data != NULL); 182772ba2109SAndrey Churbanov KMP_ASSERT(num > 0); 182872ba2109SAndrey Churbanov if (nth == 1) { 182972ba2109SAndrey Churbanov KA_TRACE(10, ("__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n", 183072ba2109SAndrey Churbanov gtid, tg)); 183172ba2109SAndrey Churbanov return (void *)tg; 183272ba2109SAndrey Churbanov } 183372ba2109SAndrey Churbanov KA_TRACE(10, ("__kmpc_task_reduction_init: T#%d, taskgroup %p, #items %d\n", 183472ba2109SAndrey Churbanov gtid, tg, num)); 18353041982dSJonathan Peyton arr = (kmp_task_red_data_t *)__kmp_thread_malloc( 18363041982dSJonathan Peyton thread, num * sizeof(kmp_task_red_data_t)); 183772ba2109SAndrey Churbanov for (int i = 0; i < num; ++i) { 183872ba2109SAndrey Churbanov void (*f_init)(void *) = (void (*)(void *))(input[i].reduce_init); 183972ba2109SAndrey Churbanov size_t size = input[i].reduce_size - 1; 184072ba2109SAndrey Churbanov // round the size up to cache line per thread-specific item 184172ba2109SAndrey Churbanov size += CACHE_LINE - size % CACHE_LINE; 184272ba2109SAndrey Churbanov KMP_ASSERT(input[i].reduce_comb != NULL); // combiner is mandatory 184372ba2109SAndrey Churbanov arr[i].reduce_shar = input[i].reduce_shar; 184472ba2109SAndrey Churbanov arr[i].reduce_size = size; 184572ba2109SAndrey Churbanov arr[i].reduce_init = input[i].reduce_init; 184672ba2109SAndrey Churbanov arr[i].reduce_fini = input[i].reduce_fini; 184772ba2109SAndrey Churbanov arr[i].reduce_comb = input[i].reduce_comb; 184872ba2109SAndrey Churbanov arr[i].flags = input[i].flags; 184972ba2109SAndrey Churbanov if (!input[i].flags.lazy_priv) { 185072ba2109SAndrey Churbanov // allocate cache-line aligned block and fill it with zeros 185172ba2109SAndrey Churbanov arr[i].reduce_priv = __kmp_allocate(nth * size); 185272ba2109SAndrey Churbanov arr[i].reduce_pend = (char *)(arr[i].reduce_priv) + nth * size; 185372ba2109SAndrey Churbanov if (f_init != NULL) { 185472ba2109SAndrey Churbanov // initialize thread-specific items 185572ba2109SAndrey Churbanov for (int j = 0; j < nth; ++j) { 185672ba2109SAndrey Churbanov f_init((char *)(arr[i].reduce_priv) + j * size); 185772ba2109SAndrey Churbanov } 185872ba2109SAndrey Churbanov } 185972ba2109SAndrey Churbanov } else { 186072ba2109SAndrey Churbanov // only allocate space for pointers now, 186172ba2109SAndrey Churbanov // objects will be lazily allocated/initialized once requested 186272ba2109SAndrey Churbanov arr[i].reduce_priv = __kmp_allocate(nth * sizeof(void *)); 186372ba2109SAndrey Churbanov } 186472ba2109SAndrey Churbanov } 186572ba2109SAndrey Churbanov tg->reduce_data = (void *)arr; 186672ba2109SAndrey Churbanov tg->reduce_num_data = num; 186772ba2109SAndrey Churbanov return (void *)tg; 186872ba2109SAndrey Churbanov } 186972ba2109SAndrey Churbanov 187072ba2109SAndrey Churbanov /*! 187172ba2109SAndrey Churbanov @ingroup TASKING 187272ba2109SAndrey Churbanov @param gtid Global thread ID 187372ba2109SAndrey Churbanov @param tskgrp The taskgroup ID (optional) 187472ba2109SAndrey Churbanov @param data Shared location of the item 187572ba2109SAndrey Churbanov @return The pointer to per-thread data 187672ba2109SAndrey Churbanov 187772ba2109SAndrey Churbanov Get thread-specific location of data item 187872ba2109SAndrey Churbanov */ 18793041982dSJonathan Peyton void *__kmpc_task_reduction_get_th_data(int gtid, void *tskgrp, void *data) { 188072ba2109SAndrey Churbanov kmp_info_t *thread = __kmp_threads[gtid]; 188172ba2109SAndrey Churbanov kmp_int32 nth = thread->th.th_team_nproc; 188272ba2109SAndrey Churbanov if (nth == 1) 188372ba2109SAndrey Churbanov return data; // nothing to do 188472ba2109SAndrey Churbanov 188572ba2109SAndrey Churbanov kmp_taskgroup_t *tg = (kmp_taskgroup_t *)tskgrp; 188672ba2109SAndrey Churbanov if (tg == NULL) 188772ba2109SAndrey Churbanov tg = thread->th.th_current_task->td_taskgroup; 188872ba2109SAndrey Churbanov KMP_ASSERT(tg != NULL); 188972ba2109SAndrey Churbanov kmp_task_red_data_t *arr = (kmp_task_red_data_t *)(tg->reduce_data); 189072ba2109SAndrey Churbanov kmp_int32 num = tg->reduce_num_data; 189172ba2109SAndrey Churbanov kmp_int32 tid = thread->th.th_info.ds.ds_tid; 189272ba2109SAndrey Churbanov 189372ba2109SAndrey Churbanov KMP_ASSERT(data != NULL); 189472ba2109SAndrey Churbanov while (tg != NULL) { 189572ba2109SAndrey Churbanov for (int i = 0; i < num; ++i) { 189672ba2109SAndrey Churbanov if (!arr[i].flags.lazy_priv) { 189772ba2109SAndrey Churbanov if (data == arr[i].reduce_shar || 189872ba2109SAndrey Churbanov (data >= arr[i].reduce_priv && data < arr[i].reduce_pend)) 189972ba2109SAndrey Churbanov return (char *)(arr[i].reduce_priv) + tid * arr[i].reduce_size; 190072ba2109SAndrey Churbanov } else { 190172ba2109SAndrey Churbanov // check shared location first 190272ba2109SAndrey Churbanov void **p_priv = (void **)(arr[i].reduce_priv); 190372ba2109SAndrey Churbanov if (data == arr[i].reduce_shar) 190472ba2109SAndrey Churbanov goto found; 190572ba2109SAndrey Churbanov // check if we get some thread specific location as parameter 190672ba2109SAndrey Churbanov for (int j = 0; j < nth; ++j) 190772ba2109SAndrey Churbanov if (data == p_priv[j]) 190872ba2109SAndrey Churbanov goto found; 190972ba2109SAndrey Churbanov continue; // not found, continue search 191072ba2109SAndrey Churbanov found: 191172ba2109SAndrey Churbanov if (p_priv[tid] == NULL) { 191272ba2109SAndrey Churbanov // allocate thread specific object lazily 191372ba2109SAndrey Churbanov void (*f_init)(void *) = (void (*)(void *))(arr[i].reduce_init); 191472ba2109SAndrey Churbanov p_priv[tid] = __kmp_allocate(arr[i].reduce_size); 191572ba2109SAndrey Churbanov if (f_init != NULL) { 191672ba2109SAndrey Churbanov f_init(p_priv[tid]); 191772ba2109SAndrey Churbanov } 191872ba2109SAndrey Churbanov } 191972ba2109SAndrey Churbanov return p_priv[tid]; 192072ba2109SAndrey Churbanov } 192172ba2109SAndrey Churbanov } 192272ba2109SAndrey Churbanov tg = tg->parent; 192372ba2109SAndrey Churbanov arr = (kmp_task_red_data_t *)(tg->reduce_data); 192472ba2109SAndrey Churbanov num = tg->reduce_num_data; 192572ba2109SAndrey Churbanov } 192672ba2109SAndrey Churbanov KMP_ASSERT2(0, "Unknown task reduction item"); 192772ba2109SAndrey Churbanov return NULL; // ERROR, this line never executed 192872ba2109SAndrey Churbanov } 192972ba2109SAndrey Churbanov 193072ba2109SAndrey Churbanov // Finalize task reduction. 193172ba2109SAndrey Churbanov // Called from __kmpc_end_taskgroup() 19323041982dSJonathan Peyton static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg) { 193372ba2109SAndrey Churbanov kmp_int32 nth = th->th.th_team_nproc; 193472ba2109SAndrey Churbanov KMP_DEBUG_ASSERT(nth > 1); // should not be called if nth == 1 193572ba2109SAndrey Churbanov kmp_task_red_data_t *arr = (kmp_task_red_data_t *)tg->reduce_data; 193672ba2109SAndrey Churbanov kmp_int32 num = tg->reduce_num_data; 193772ba2109SAndrey Churbanov for (int i = 0; i < num; ++i) { 193872ba2109SAndrey Churbanov void *sh_data = arr[i].reduce_shar; 193972ba2109SAndrey Churbanov void (*f_fini)(void *) = (void (*)(void *))(arr[i].reduce_fini); 19403041982dSJonathan Peyton void (*f_comb)(void *, void *) = 19413041982dSJonathan Peyton (void (*)(void *, void *))(arr[i].reduce_comb); 194272ba2109SAndrey Churbanov if (!arr[i].flags.lazy_priv) { 194372ba2109SAndrey Churbanov void *pr_data = arr[i].reduce_priv; 194472ba2109SAndrey Churbanov size_t size = arr[i].reduce_size; 194572ba2109SAndrey Churbanov for (int j = 0; j < nth; ++j) { 194672ba2109SAndrey Churbanov void *priv_data = (char *)pr_data + j * size; 194772ba2109SAndrey Churbanov f_comb(sh_data, priv_data); // combine results 194872ba2109SAndrey Churbanov if (f_fini) 194972ba2109SAndrey Churbanov f_fini(priv_data); // finalize if needed 195072ba2109SAndrey Churbanov } 195172ba2109SAndrey Churbanov } else { 195272ba2109SAndrey Churbanov void **pr_data = (void **)(arr[i].reduce_priv); 195372ba2109SAndrey Churbanov for (int j = 0; j < nth; ++j) { 195472ba2109SAndrey Churbanov if (pr_data[j] != NULL) { 195572ba2109SAndrey Churbanov f_comb(sh_data, pr_data[j]); // combine results 195672ba2109SAndrey Churbanov if (f_fini) 195772ba2109SAndrey Churbanov f_fini(pr_data[j]); // finalize if needed 195872ba2109SAndrey Churbanov __kmp_free(pr_data[j]); 195972ba2109SAndrey Churbanov } 196072ba2109SAndrey Churbanov } 196172ba2109SAndrey Churbanov } 196272ba2109SAndrey Churbanov __kmp_free(arr[i].reduce_priv); 196372ba2109SAndrey Churbanov } 196472ba2109SAndrey Churbanov __kmp_thread_free(th, arr); 196572ba2109SAndrey Churbanov tg->reduce_data = NULL; 196672ba2109SAndrey Churbanov tg->reduce_num_data = 0; 196772ba2109SAndrey Churbanov } 196872ba2109SAndrey Churbanov #endif 19697cc577a4SJonathan Peyton 19707cc577a4SJonathan Peyton #if OMP_40_ENABLED 19717cc577a4SJonathan Peyton // __kmpc_taskgroup: Start a new taskgroup 19723041982dSJonathan Peyton void __kmpc_taskgroup(ident_t *loc, int gtid) { 19737cc577a4SJonathan Peyton kmp_info_t *thread = __kmp_threads[gtid]; 19747cc577a4SJonathan Peyton kmp_taskdata_t *taskdata = thread->th.th_current_task; 19757cc577a4SJonathan Peyton kmp_taskgroup_t *tg_new = 19767cc577a4SJonathan Peyton (kmp_taskgroup_t *)__kmp_thread_malloc(thread, sizeof(kmp_taskgroup_t)); 19777cc577a4SJonathan Peyton KA_TRACE(10, ("__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new)); 19787cc577a4SJonathan Peyton tg_new->count = 0; 19797cc577a4SJonathan Peyton tg_new->cancel_request = cancel_noreq; 19807cc577a4SJonathan Peyton tg_new->parent = taskdata->td_taskgroup; 198172ba2109SAndrey Churbanov // TODO: change to OMP_50_ENABLED, need to change build tools for this to work 198272ba2109SAndrey Churbanov #if OMP_45_ENABLED 198372ba2109SAndrey Churbanov tg_new->reduce_data = NULL; 198472ba2109SAndrey Churbanov tg_new->reduce_num_data = 0; 198572ba2109SAndrey Churbanov #endif 19867cc577a4SJonathan Peyton taskdata->td_taskgroup = tg_new; 1987*82e94a59SJoachim Protze 1988*82e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL 1989*82e94a59SJoachim Protze if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) { 1990*82e94a59SJoachim Protze void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid); 1991*82e94a59SJoachim Protze if (!codeptr) 1992*82e94a59SJoachim Protze codeptr = OMPT_GET_RETURN_ADDRESS(0); 1993*82e94a59SJoachim Protze kmp_team_t *team = thread->th.th_team; 1994*82e94a59SJoachim Protze ompt_data_t my_task_data = taskdata->ompt_task_info.task_data; 1995*82e94a59SJoachim Protze // FIXME: I think this is wrong for lwt! 1996*82e94a59SJoachim Protze ompt_data_t my_parallel_data = team->t.ompt_team_info.parallel_data; 1997*82e94a59SJoachim Protze 1998*82e94a59SJoachim Protze ompt_callbacks.ompt_callback(ompt_callback_sync_region)( 1999*82e94a59SJoachim Protze ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data), 2000*82e94a59SJoachim Protze &(my_task_data), codeptr); 2001*82e94a59SJoachim Protze } 2002*82e94a59SJoachim Protze #endif 20037cc577a4SJonathan Peyton } 20047cc577a4SJonathan Peyton 20057cc577a4SJonathan Peyton // __kmpc_end_taskgroup: Wait until all tasks generated by the current task 20067cc577a4SJonathan Peyton // and its descendants are complete 20073041982dSJonathan Peyton void __kmpc_end_taskgroup(ident_t *loc, int gtid) { 20087cc577a4SJonathan Peyton kmp_info_t *thread = __kmp_threads[gtid]; 20097cc577a4SJonathan Peyton kmp_taskdata_t *taskdata = thread->th.th_current_task; 20107cc577a4SJonathan Peyton kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup; 20117cc577a4SJonathan Peyton int thread_finished = FALSE; 20127cc577a4SJonathan Peyton 2013*82e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL 2014*82e94a59SJoachim Protze kmp_team_t *team; 2015*82e94a59SJoachim Protze ompt_data_t my_task_data; 2016*82e94a59SJoachim Protze ompt_data_t my_parallel_data; 2017*82e94a59SJoachim Protze void *codeptr; 2018*82e94a59SJoachim Protze if (UNLIKELY(ompt_enabled.enabled)) { 2019*82e94a59SJoachim Protze team = thread->th.th_team; 2020*82e94a59SJoachim Protze my_task_data = taskdata->ompt_task_info.task_data; 2021*82e94a59SJoachim Protze // FIXME: I think this is wrong for lwt! 2022*82e94a59SJoachim Protze my_parallel_data = team->t.ompt_team_info.parallel_data; 2023*82e94a59SJoachim Protze codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid); 2024*82e94a59SJoachim Protze if (!codeptr) 2025*82e94a59SJoachim Protze codeptr = OMPT_GET_RETURN_ADDRESS(0); 2026*82e94a59SJoachim Protze } 2027*82e94a59SJoachim Protze #endif 2028*82e94a59SJoachim Protze 20297cc577a4SJonathan Peyton KA_TRACE(10, ("__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc)); 20307cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(taskgroup != NULL); 20317cc577a4SJonathan Peyton KMP_SET_THREAD_STATE_BLOCK(TASKGROUP); 20327cc577a4SJonathan Peyton 20337cc577a4SJonathan Peyton if (__kmp_tasking_mode != tskm_immediate_exec) { 20347cc577a4SJonathan Peyton #if USE_ITT_BUILD 20353041982dSJonathan Peyton // For ITT the taskgroup wait is similar to taskwait until we need to 20363041982dSJonathan Peyton // distinguish them 20377cc577a4SJonathan Peyton void *itt_sync_obj = __kmp_itt_taskwait_object(gtid); 20387cc577a4SJonathan Peyton if (itt_sync_obj != NULL) 20397cc577a4SJonathan Peyton __kmp_itt_taskwait_starting(gtid, itt_sync_obj); 20407cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */ 20417cc577a4SJonathan Peyton 2042*82e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL 2043*82e94a59SJoachim Protze if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) { 2044*82e94a59SJoachim Protze ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)( 2045*82e94a59SJoachim Protze ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data), 2046*82e94a59SJoachim Protze &(my_task_data), codeptr); 2047*82e94a59SJoachim Protze } 2048*82e94a59SJoachim Protze #endif 2049*82e94a59SJoachim Protze 20507cc577a4SJonathan Peyton #if OMP_45_ENABLED 20513041982dSJonathan Peyton if (!taskdata->td_flags.team_serial || 20523041982dSJonathan Peyton (thread->th.th_task_team != NULL && 20533041982dSJonathan Peyton thread->th.th_task_team->tt.tt_found_proxy_tasks)) 20547cc577a4SJonathan Peyton #else 20557cc577a4SJonathan Peyton if (!taskdata->td_flags.team_serial) 20567cc577a4SJonathan Peyton #endif 20577cc577a4SJonathan Peyton { 2058c47afcd9SAndrey Churbanov kmp_flag_32 flag(RCAST(kmp_uint32 *, &taskgroup->count), 0U); 20597cc577a4SJonathan Peyton while (TCR_4(taskgroup->count) != 0) { 20603041982dSJonathan Peyton flag.execute_tasks(thread, gtid, FALSE, 20613041982dSJonathan Peyton &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), 20623041982dSJonathan Peyton __kmp_task_stealing_constraint); 20637cc577a4SJonathan Peyton } 20647cc577a4SJonathan Peyton } 20657cc577a4SJonathan Peyton 2066*82e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL 2067*82e94a59SJoachim Protze if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) { 2068*82e94a59SJoachim Protze ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)( 2069*82e94a59SJoachim Protze ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data), 2070*82e94a59SJoachim Protze &(my_task_data), codeptr); 2071*82e94a59SJoachim Protze } 2072*82e94a59SJoachim Protze #endif 2073*82e94a59SJoachim Protze 20747cc577a4SJonathan Peyton #if USE_ITT_BUILD 20757cc577a4SJonathan Peyton if (itt_sync_obj != NULL) 20767cc577a4SJonathan Peyton __kmp_itt_taskwait_finished(gtid, itt_sync_obj); 20777cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */ 20787cc577a4SJonathan Peyton } 20797cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(taskgroup->count == 0); 20807cc577a4SJonathan Peyton 208172ba2109SAndrey Churbanov // TODO: change to OMP_50_ENABLED, need to change build tools for this to work 208272ba2109SAndrey Churbanov #if OMP_45_ENABLED 208372ba2109SAndrey Churbanov if (taskgroup->reduce_data != NULL) // need to reduce? 208472ba2109SAndrey Churbanov __kmp_task_reduction_fini(thread, taskgroup); 208572ba2109SAndrey Churbanov #endif 20867cc577a4SJonathan Peyton // Restore parent taskgroup for the current task 20877cc577a4SJonathan Peyton taskdata->td_taskgroup = taskgroup->parent; 20887cc577a4SJonathan Peyton __kmp_thread_free(thread, taskgroup); 20897cc577a4SJonathan Peyton 20903041982dSJonathan Peyton KA_TRACE(10, ("__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n", 20913041982dSJonathan Peyton gtid, taskdata)); 20927cc577a4SJonathan Peyton ANNOTATE_HAPPENS_AFTER(taskdata); 2093*82e94a59SJoachim Protze 2094*82e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL 2095*82e94a59SJoachim Protze if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) { 2096*82e94a59SJoachim Protze ompt_callbacks.ompt_callback(ompt_callback_sync_region)( 2097*82e94a59SJoachim Protze ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data), 2098*82e94a59SJoachim Protze &(my_task_data), codeptr); 2099*82e94a59SJoachim Protze } 2100*82e94a59SJoachim Protze #endif 21017cc577a4SJonathan Peyton } 21027cc577a4SJonathan Peyton #endif 21037cc577a4SJonathan Peyton 21047cc577a4SJonathan Peyton // __kmp_remove_my_task: remove a task from my own deque 21053041982dSJonathan Peyton static kmp_task_t *__kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid, 21063041982dSJonathan Peyton kmp_task_team_t *task_team, 21073041982dSJonathan Peyton kmp_int32 is_constrained) { 21087cc577a4SJonathan Peyton kmp_task_t *task; 21097cc577a4SJonathan Peyton kmp_taskdata_t *taskdata; 21107cc577a4SJonathan Peyton kmp_thread_data_t *thread_data; 21117cc577a4SJonathan Peyton kmp_uint32 tail; 21127cc577a4SJonathan Peyton 21137cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec); 21143041982dSJonathan Peyton KMP_DEBUG_ASSERT(task_team->tt.tt_threads_data != 21153041982dSJonathan Peyton NULL); // Caller should check this condition 21167cc577a4SJonathan Peyton 21177cc577a4SJonathan Peyton thread_data = &task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)]; 21187cc577a4SJonathan Peyton 21197cc577a4SJonathan Peyton KA_TRACE(10, ("__kmp_remove_my_task(enter): T#%d ntasks=%d head=%u tail=%u\n", 21203041982dSJonathan Peyton gtid, thread_data->td.td_deque_ntasks, 21213041982dSJonathan Peyton thread_data->td.td_deque_head, thread_data->td.td_deque_tail)); 21227cc577a4SJonathan Peyton 21237cc577a4SJonathan Peyton if (TCR_4(thread_data->td.td_deque_ntasks) == 0) { 21243041982dSJonathan Peyton KA_TRACE(10, 21253041982dSJonathan Peyton ("__kmp_remove_my_task(exit #1): T#%d No tasks to remove: " 21263041982dSJonathan Peyton "ntasks=%d head=%u tail=%u\n", 21273041982dSJonathan Peyton gtid, thread_data->td.td_deque_ntasks, 21283041982dSJonathan Peyton thread_data->td.td_deque_head, thread_data->td.td_deque_tail)); 21297cc577a4SJonathan Peyton return NULL; 21307cc577a4SJonathan Peyton } 21317cc577a4SJonathan Peyton 21327cc577a4SJonathan Peyton __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock); 21337cc577a4SJonathan Peyton 21347cc577a4SJonathan Peyton if (TCR_4(thread_data->td.td_deque_ntasks) == 0) { 21357cc577a4SJonathan Peyton __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock); 21363041982dSJonathan Peyton KA_TRACE(10, 21373041982dSJonathan Peyton ("__kmp_remove_my_task(exit #2): T#%d No tasks to remove: " 21383041982dSJonathan Peyton "ntasks=%d head=%u tail=%u\n", 21393041982dSJonathan Peyton gtid, thread_data->td.td_deque_ntasks, 21403041982dSJonathan Peyton thread_data->td.td_deque_head, thread_data->td.td_deque_tail)); 21417cc577a4SJonathan Peyton return NULL; 21427cc577a4SJonathan Peyton } 21437cc577a4SJonathan Peyton 21443041982dSJonathan Peyton tail = (thread_data->td.td_deque_tail - 1) & 21453041982dSJonathan Peyton TASK_DEQUE_MASK(thread_data->td); // Wrap index. 21467cc577a4SJonathan Peyton taskdata = thread_data->td.td_deque[tail]; 21477cc577a4SJonathan Peyton 21487cc577a4SJonathan Peyton if (is_constrained && (taskdata->td_flags.tiedness == TASK_TIED)) { 21497cc577a4SJonathan Peyton // we need to check if the candidate obeys task scheduling constraint: 21507cc577a4SJonathan Peyton // only child of current task can be scheduled 21517cc577a4SJonathan Peyton kmp_taskdata_t *current = thread->th.th_current_task; 21527cc577a4SJonathan Peyton kmp_int32 level = current->td_level; 21537cc577a4SJonathan Peyton kmp_taskdata_t *parent = taskdata->td_parent; 21547cc577a4SJonathan Peyton while (parent != current && parent->td_level > level) { 21553041982dSJonathan Peyton parent = parent->td_parent; // check generation up to the level of the 21563041982dSJonathan Peyton // current task 21577cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(parent != NULL); 21587cc577a4SJonathan Peyton } 21597cc577a4SJonathan Peyton if (parent != current) { 21603041982dSJonathan Peyton // If the tail task is not a child, then no other child can appear in the 21613041982dSJonathan Peyton // deque. 21627cc577a4SJonathan Peyton __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock); 21633041982dSJonathan Peyton KA_TRACE(10, 21643041982dSJonathan Peyton ("__kmp_remove_my_task(exit #2): T#%d No tasks to remove: " 21653041982dSJonathan Peyton "ntasks=%d head=%u tail=%u\n", 21663041982dSJonathan Peyton gtid, thread_data->td.td_deque_ntasks, 21673041982dSJonathan Peyton thread_data->td.td_deque_head, thread_data->td.td_deque_tail)); 21687cc577a4SJonathan Peyton return NULL; 21697cc577a4SJonathan Peyton } 21707cc577a4SJonathan Peyton } 21717cc577a4SJonathan Peyton 21727cc577a4SJonathan Peyton thread_data->td.td_deque_tail = tail; 21737cc577a4SJonathan Peyton TCW_4(thread_data->td.td_deque_ntasks, thread_data->td.td_deque_ntasks - 1); 21747cc577a4SJonathan Peyton 21757cc577a4SJonathan Peyton __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock); 21767cc577a4SJonathan Peyton 21773041982dSJonathan Peyton KA_TRACE(10, ("__kmp_remove_my_task(exit #2): T#%d task %p removed: " 21783041982dSJonathan Peyton "ntasks=%d head=%u tail=%u\n", 21793041982dSJonathan Peyton gtid, taskdata, thread_data->td.td_deque_ntasks, 21803041982dSJonathan Peyton thread_data->td.td_deque_head, thread_data->td.td_deque_tail)); 21817cc577a4SJonathan Peyton 21827cc577a4SJonathan Peyton task = KMP_TASKDATA_TO_TASK(taskdata); 21837cc577a4SJonathan Peyton return task; 21847cc577a4SJonathan Peyton } 21857cc577a4SJonathan Peyton 21867cc577a4SJonathan Peyton // __kmp_steal_task: remove a task from another thread's deque 21877cc577a4SJonathan Peyton // Assume that calling thread has already checked existence of 21887cc577a4SJonathan Peyton // task_team thread_data before calling this routine. 2189c47afcd9SAndrey Churbanov static kmp_task_t *__kmp_steal_task(kmp_info_t *victim, kmp_int32 gtid, 2190c47afcd9SAndrey Churbanov kmp_task_team_t *task_team, 2191c47afcd9SAndrey Churbanov volatile kmp_int32 *unfinished_threads, 2192c47afcd9SAndrey Churbanov int *thread_finished, 2193c47afcd9SAndrey Churbanov kmp_int32 is_constrained) { 21947cc577a4SJonathan Peyton kmp_task_t *task; 21957cc577a4SJonathan Peyton kmp_taskdata_t *taskdata; 21967cc577a4SJonathan Peyton kmp_thread_data_t *victim_td, *threads_data; 21977cc577a4SJonathan Peyton kmp_int32 victim_tid; 21987cc577a4SJonathan Peyton 21997cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec); 22007cc577a4SJonathan Peyton 22017cc577a4SJonathan Peyton threads_data = task_team->tt.tt_threads_data; 22027cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(threads_data != NULL); // Caller should check this condition 22037cc577a4SJonathan Peyton 22047cc577a4SJonathan Peyton victim_tid = victim->th.th_info.ds.ds_tid; 22057cc577a4SJonathan Peyton victim_td = &threads_data[victim_tid]; 22067cc577a4SJonathan Peyton 22073041982dSJonathan Peyton KA_TRACE(10, ("__kmp_steal_task(enter): T#%d try to steal from T#%d: " 22083041982dSJonathan Peyton "task_team=%p ntasks=%d " 22097cc577a4SJonathan Peyton "head=%u tail=%u\n", 22103041982dSJonathan Peyton gtid, __kmp_gtid_from_thread(victim), task_team, 22113041982dSJonathan Peyton victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head, 22123041982dSJonathan Peyton victim_td->td.td_deque_tail)); 22137cc577a4SJonathan Peyton 22143041982dSJonathan Peyton if ((TCR_4(victim_td->td.td_deque_ntasks) == 22153041982dSJonathan Peyton 0) || // Caller should not check this condition 22163041982dSJonathan Peyton (TCR_PTR(victim->th.th_task_team) != 22173041982dSJonathan Peyton task_team)) // GEH: why would this happen? 22187cc577a4SJonathan Peyton { 22193041982dSJonathan Peyton KA_TRACE(10, ("__kmp_steal_task(exit #1): T#%d could not steal from T#%d: " 22203041982dSJonathan Peyton "task_team=%p " 22217cc577a4SJonathan Peyton "ntasks=%d head=%u tail=%u\n", 22223041982dSJonathan Peyton gtid, __kmp_gtid_from_thread(victim), task_team, 22233041982dSJonathan Peyton victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head, 22243041982dSJonathan Peyton victim_td->td.td_deque_tail)); 22257cc577a4SJonathan Peyton return NULL; 22267cc577a4SJonathan Peyton } 22277cc577a4SJonathan Peyton 22287cc577a4SJonathan Peyton __kmp_acquire_bootstrap_lock(&victim_td->td.td_deque_lock); 22297cc577a4SJonathan Peyton 22307cc577a4SJonathan Peyton // Check again after we acquire the lock 22317cc577a4SJonathan Peyton if ((TCR_4(victim_td->td.td_deque_ntasks) == 0) || 22323041982dSJonathan Peyton (TCR_PTR(victim->th.th_task_team) != 22333041982dSJonathan Peyton task_team)) // GEH: why would this happen? 22347cc577a4SJonathan Peyton { 22357cc577a4SJonathan Peyton __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock); 22363041982dSJonathan Peyton KA_TRACE(10, ("__kmp_steal_task(exit #2): T#%d could not steal from T#%d: " 22373041982dSJonathan Peyton "task_team=%p " 22387cc577a4SJonathan Peyton "ntasks=%d head=%u tail=%u\n", 22393041982dSJonathan Peyton gtid, __kmp_gtid_from_thread(victim), task_team, 22403041982dSJonathan Peyton victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head, 22413041982dSJonathan Peyton victim_td->td.td_deque_tail)); 22427cc577a4SJonathan Peyton return NULL; 22437cc577a4SJonathan Peyton } 22447cc577a4SJonathan Peyton 22457cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(victim_td->td.td_deque != NULL); 22467cc577a4SJonathan Peyton 22477cc577a4SJonathan Peyton taskdata = victim_td->td.td_deque[victim_td->td.td_deque_head]; 22487cc577a4SJonathan Peyton if (is_constrained) { 22497cc577a4SJonathan Peyton // we need to check if the candidate obeys task scheduling constraint: 22507cc577a4SJonathan Peyton // only descendant of current task can be scheduled 22517cc577a4SJonathan Peyton kmp_taskdata_t *current = __kmp_threads[gtid]->th.th_current_task; 22527cc577a4SJonathan Peyton kmp_int32 level = current->td_level; 22537cc577a4SJonathan Peyton kmp_taskdata_t *parent = taskdata->td_parent; 22547cc577a4SJonathan Peyton while (parent != current && parent->td_level > level) { 22553041982dSJonathan Peyton parent = parent->td_parent; // check generation up to the level of the 22563041982dSJonathan Peyton // current task 22577cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(parent != NULL); 22587cc577a4SJonathan Peyton } 22597cc577a4SJonathan Peyton if (parent != current) { 22607cc577a4SJonathan Peyton // If the head task is not a descendant of the current task then do not 22617cc577a4SJonathan Peyton // steal it. No other task in victim's deque can be a descendant of the 22627cc577a4SJonathan Peyton // current task. 22637cc577a4SJonathan Peyton __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock); 22643041982dSJonathan Peyton KA_TRACE(10, ("__kmp_steal_task(exit #2): T#%d could not steal from " 22653041982dSJonathan Peyton "T#%d: task_team=%p " 22667cc577a4SJonathan Peyton "ntasks=%d head=%u tail=%u\n", 22673041982dSJonathan Peyton gtid, 22683041982dSJonathan Peyton __kmp_gtid_from_thread(threads_data[victim_tid].td.td_thr), 22697cc577a4SJonathan Peyton task_team, victim_td->td.td_deque_ntasks, 22707cc577a4SJonathan Peyton victim_td->td.td_deque_head, victim_td->td.td_deque_tail)); 22717cc577a4SJonathan Peyton return NULL; 22727cc577a4SJonathan Peyton } 22737cc577a4SJonathan Peyton } 22747cc577a4SJonathan Peyton // Bump head pointer and Wrap. 22753041982dSJonathan Peyton victim_td->td.td_deque_head = 22763041982dSJonathan Peyton (victim_td->td.td_deque_head + 1) & TASK_DEQUE_MASK(victim_td->td); 22777cc577a4SJonathan Peyton if (*thread_finished) { 22783041982dSJonathan Peyton // We need to un-mark this victim as a finished victim. This must be done 22793041982dSJonathan Peyton // before releasing the lock, or else other threads (starting with the 22803041982dSJonathan Peyton // master victim) might be prematurely released from the barrier!!! 2281c47afcd9SAndrey Churbanov kmp_int32 count; 22827cc577a4SJonathan Peyton 22835ba90c79SAndrey Churbanov count = KMP_TEST_THEN_INC32(unfinished_threads); 22847cc577a4SJonathan Peyton 22853041982dSJonathan Peyton KA_TRACE( 22863041982dSJonathan Peyton 20, 22873041982dSJonathan Peyton ("__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n", 22887cc577a4SJonathan Peyton gtid, count + 1, task_team)); 22897cc577a4SJonathan Peyton 22907cc577a4SJonathan Peyton *thread_finished = FALSE; 22917cc577a4SJonathan Peyton } 22923041982dSJonathan Peyton TCW_4(victim_td->td.td_deque_ntasks, 22933041982dSJonathan Peyton TCR_4(victim_td->td.td_deque_ntasks) - 1); 22943041982dSJonathan Peyton 22957cc577a4SJonathan Peyton __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock); 22967cc577a4SJonathan Peyton 22977cc577a4SJonathan Peyton KMP_COUNT_BLOCK(TASK_stolen); 22983041982dSJonathan Peyton KA_TRACE( 22993041982dSJonathan Peyton 10, 23003041982dSJonathan Peyton ("__kmp_steal_task(exit #3): T#%d stole task %p from T#%d: task_team=%p " 23017cc577a4SJonathan Peyton "ntasks=%d head=%u tail=%u\n", 23027cc577a4SJonathan Peyton gtid, taskdata, __kmp_gtid_from_thread(victim), task_team, 23037cc577a4SJonathan Peyton victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head, 23047cc577a4SJonathan Peyton victim_td->td.td_deque_tail)); 23057cc577a4SJonathan Peyton 23067cc577a4SJonathan Peyton task = KMP_TASKDATA_TO_TASK(taskdata); 23077cc577a4SJonathan Peyton return task; 23087cc577a4SJonathan Peyton } 23097cc577a4SJonathan Peyton 23103041982dSJonathan Peyton // __kmp_execute_tasks_template: Choose and execute tasks until either the 23113041982dSJonathan Peyton // condition is statisfied (return true) or there are none left (return false). 23123041982dSJonathan Peyton // 23137cc577a4SJonathan Peyton // final_spin is TRUE if this is the spin at the release barrier. 23147cc577a4SJonathan Peyton // thread_finished indicates whether the thread is finished executing all 23157cc577a4SJonathan Peyton // the tasks it has on its deque, and is at the release barrier. 23167cc577a4SJonathan Peyton // spinner is the location on which to spin. 23177cc577a4SJonathan Peyton // spinner == NULL means only execute a single task and return. 23187cc577a4SJonathan Peyton // checker is the value to check to terminate the spin. 23197cc577a4SJonathan Peyton template <class C> 23203041982dSJonathan Peyton static inline int __kmp_execute_tasks_template( 23213041982dSJonathan Peyton kmp_info_t *thread, kmp_int32 gtid, C *flag, int final_spin, 23223041982dSJonathan Peyton int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj), 23233041982dSJonathan Peyton kmp_int32 is_constrained) { 23247cc577a4SJonathan Peyton kmp_task_team_t *task_team = thread->th.th_task_team; 23257cc577a4SJonathan Peyton kmp_thread_data_t *threads_data; 23267cc577a4SJonathan Peyton kmp_task_t *task; 23277cc577a4SJonathan Peyton kmp_info_t *other_thread; 23287cc577a4SJonathan Peyton kmp_taskdata_t *current_task = thread->th.th_current_task; 2329c47afcd9SAndrey Churbanov volatile kmp_int32 *unfinished_threads; 23303041982dSJonathan Peyton kmp_int32 nthreads, victim = -2, use_own_tasks = 1, new_victim = 0, 23313041982dSJonathan Peyton tid = thread->th.th_info.ds.ds_tid; 23327cc577a4SJonathan Peyton 23337cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec); 23347cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(thread == __kmp_threads[gtid]); 23357cc577a4SJonathan Peyton 23363041982dSJonathan Peyton if (task_team == NULL) 23373041982dSJonathan Peyton return FALSE; 23387cc577a4SJonathan Peyton 23393041982dSJonathan Peyton KA_TRACE(15, ("__kmp_execute_tasks_template(enter): T#%d final_spin=%d " 23403041982dSJonathan Peyton "*thread_finished=%d\n", 23417cc577a4SJonathan Peyton gtid, final_spin, *thread_finished)); 23427cc577a4SJonathan Peyton 2343581490e7SAndrey Churbanov thread->th.th_reap_state = KMP_NOT_SAFE_TO_REAP; 23447cc577a4SJonathan Peyton threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data); 23457cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(threads_data != NULL); 23467cc577a4SJonathan Peyton 23477cc577a4SJonathan Peyton nthreads = task_team->tt.tt_nproc; 23487cc577a4SJonathan Peyton unfinished_threads = &(task_team->tt.tt_unfinished_threads); 23497cc577a4SJonathan Peyton #if OMP_45_ENABLED 23507cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(nthreads > 1 || task_team->tt.tt_found_proxy_tasks); 23517cc577a4SJonathan Peyton #else 23527cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(nthreads > 1); 23537cc577a4SJonathan Peyton #endif 2354c47afcd9SAndrey Churbanov KMP_DEBUG_ASSERT(TCR_4(*unfinished_threads) >= 0); 23557cc577a4SJonathan Peyton 23563041982dSJonathan Peyton while (1) { // Outer loop keeps trying to find tasks in case of single thread 23573041982dSJonathan Peyton // getting tasks from target constructs 23587cc577a4SJonathan Peyton while (1) { // Inner loop to find a task and execute it 23597cc577a4SJonathan Peyton task = NULL; 23607cc577a4SJonathan Peyton if (use_own_tasks) { // check on own queue first 23617cc577a4SJonathan Peyton task = __kmp_remove_my_task(thread, gtid, task_team, is_constrained); 23627cc577a4SJonathan Peyton } 23637cc577a4SJonathan Peyton if ((task == NULL) && (nthreads > 1)) { // Steal a task 23647cc577a4SJonathan Peyton int asleep = 1; 23657cc577a4SJonathan Peyton use_own_tasks = 0; 23667cc577a4SJonathan Peyton // Try to steal from the last place I stole from successfully. 23677cc577a4SJonathan Peyton if (victim == -2) { // haven't stolen anything yet 23687cc577a4SJonathan Peyton victim = threads_data[tid].td.td_deque_last_stolen; 23693041982dSJonathan Peyton if (victim != 23703041982dSJonathan Peyton -1) // if we have a last stolen from victim, get the thread 23717cc577a4SJonathan Peyton other_thread = threads_data[victim].td.td_thr; 23727cc577a4SJonathan Peyton } 23737cc577a4SJonathan Peyton if (victim != -1) { // found last victim 23747cc577a4SJonathan Peyton asleep = 0; 23753041982dSJonathan Peyton } else if (!new_victim) { // no recent steals and we haven't already 23763041982dSJonathan Peyton // used a new victim; select a random thread 23777cc577a4SJonathan Peyton do { // Find a different thread to steal work from. 23783041982dSJonathan Peyton // Pick a random thread. Initial plan was to cycle through all the 23793041982dSJonathan Peyton // threads, and only return if we tried to steal from every thread, 23803041982dSJonathan Peyton // and failed. Arch says that's not such a great idea. 23817cc577a4SJonathan Peyton victim = __kmp_get_random(thread) % (nthreads - 1); 23827cc577a4SJonathan Peyton if (victim >= tid) { 23837cc577a4SJonathan Peyton ++victim; // Adjusts random distribution to exclude self 23847cc577a4SJonathan Peyton } 23857cc577a4SJonathan Peyton // Found a potential victim 23867cc577a4SJonathan Peyton other_thread = threads_data[victim].td.td_thr; 23873041982dSJonathan Peyton // There is a slight chance that __kmp_enable_tasking() did not wake 23883041982dSJonathan Peyton // up all threads waiting at the barrier. If victim is sleeping, 23893041982dSJonathan Peyton // then wake it up. Since we were going to pay the cache miss 23903041982dSJonathan Peyton // penalty for referencing another thread's kmp_info_t struct 23913041982dSJonathan Peyton // anyway, 23923041982dSJonathan Peyton // the check shouldn't cost too much performance at this point. In 23933041982dSJonathan Peyton // extra barrier mode, tasks do not sleep at the separate tasking 23943041982dSJonathan Peyton // barrier, so this isn't a problem. 23957cc577a4SJonathan Peyton asleep = 0; 23967cc577a4SJonathan Peyton if ((__kmp_tasking_mode == tskm_task_teams) && 23977cc577a4SJonathan Peyton (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) && 2398c47afcd9SAndrey Churbanov (TCR_PTR(CCAST(void *, other_thread->th.th_sleep_loc)) != 2399c47afcd9SAndrey Churbanov NULL)) { 24007cc577a4SJonathan Peyton asleep = 1; 24013041982dSJonathan Peyton __kmp_null_resume_wrapper(__kmp_gtid_from_thread(other_thread), 24023041982dSJonathan Peyton other_thread->th.th_sleep_loc); 24033041982dSJonathan Peyton // A sleeping thread should not have any tasks on it's queue. 24043041982dSJonathan Peyton // There is a slight possibility that it resumes, steals a task 24053041982dSJonathan Peyton // from another thread, which spawns more tasks, all in the time 24063041982dSJonathan Peyton // that it takes this thread to check => don't write an assertion 24073041982dSJonathan Peyton // that the victim's queue is empty. Try stealing from a 24083041982dSJonathan Peyton // different thread. 24097cc577a4SJonathan Peyton } 24107cc577a4SJonathan Peyton } while (asleep); 24117cc577a4SJonathan Peyton } 24127cc577a4SJonathan Peyton 24137cc577a4SJonathan Peyton if (!asleep) { 24147cc577a4SJonathan Peyton // We have a victim to try to steal from 24153041982dSJonathan Peyton task = __kmp_steal_task(other_thread, gtid, task_team, 24163041982dSJonathan Peyton unfinished_threads, thread_finished, 24173041982dSJonathan Peyton is_constrained); 24187cc577a4SJonathan Peyton } 24197cc577a4SJonathan Peyton if (task != NULL) { // set last stolen to victim 24207cc577a4SJonathan Peyton if (threads_data[tid].td.td_deque_last_stolen != victim) { 24217cc577a4SJonathan Peyton threads_data[tid].td.td_deque_last_stolen = victim; 24223041982dSJonathan Peyton // The pre-refactored code did not try more than 1 successful new 24233041982dSJonathan Peyton // vicitm, unless the last one generated more local tasks; 24243041982dSJonathan Peyton // new_victim keeps track of this 24257cc577a4SJonathan Peyton new_victim = 1; 24267cc577a4SJonathan Peyton } 24273041982dSJonathan Peyton } else { // No tasks found; unset last_stolen 24287cc577a4SJonathan Peyton KMP_CHECK_UPDATE(threads_data[tid].td.td_deque_last_stolen, -1); 24297cc577a4SJonathan Peyton victim = -2; // no successful victim found 24307cc577a4SJonathan Peyton } 24317cc577a4SJonathan Peyton } 24327cc577a4SJonathan Peyton 24337cc577a4SJonathan Peyton if (task == NULL) // break out of tasking loop 24347cc577a4SJonathan Peyton break; 24357cc577a4SJonathan Peyton 24367cc577a4SJonathan Peyton // Found a task; execute it 24377cc577a4SJonathan Peyton #if USE_ITT_BUILD && USE_ITT_NOTIFY 24387cc577a4SJonathan Peyton if (__itt_sync_create_ptr || KMP_ITT_DEBUG) { 24393041982dSJonathan Peyton if (itt_sync_obj == NULL) { // we are at fork barrier where we could not 24403041982dSJonathan Peyton // get the object reliably 24417cc577a4SJonathan Peyton itt_sync_obj = __kmp_itt_barrier_object(gtid, bs_forkjoin_barrier); 24427cc577a4SJonathan Peyton } 24437cc577a4SJonathan Peyton __kmp_itt_task_starting(itt_sync_obj); 24447cc577a4SJonathan Peyton } 24457cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD && USE_ITT_NOTIFY */ 24467cc577a4SJonathan Peyton __kmp_invoke_task(gtid, task, current_task); 24477cc577a4SJonathan Peyton #if USE_ITT_BUILD 24483041982dSJonathan Peyton if (itt_sync_obj != NULL) 24493041982dSJonathan Peyton __kmp_itt_task_finished(itt_sync_obj); 24507cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */ 24513041982dSJonathan Peyton // If this thread is only partway through the barrier and the condition is 24523041982dSJonathan Peyton // met, then return now, so that the barrier gather/release pattern can 24533041982dSJonathan Peyton // proceed. If this thread is in the last spin loop in the barrier, 24543041982dSJonathan Peyton // waiting to be released, we know that the termination condition will not 24553041982dSJonathan Peyton // be satisified, so don't waste any cycles checking it. 24567cc577a4SJonathan Peyton if (flag == NULL || (!final_spin && flag->done_check())) { 24573041982dSJonathan Peyton KA_TRACE( 24583041982dSJonathan Peyton 15, 24593041982dSJonathan Peyton ("__kmp_execute_tasks_template: T#%d spin condition satisfied\n", 24603041982dSJonathan Peyton gtid)); 24617cc577a4SJonathan Peyton return TRUE; 24627cc577a4SJonathan Peyton } 24637cc577a4SJonathan Peyton if (thread->th.th_task_team == NULL) { 24647cc577a4SJonathan Peyton break; 24657cc577a4SJonathan Peyton } 24663041982dSJonathan Peyton // Yield before executing next task 24673041982dSJonathan Peyton KMP_YIELD(__kmp_library == library_throughput); 24683041982dSJonathan Peyton // If execution of a stolen task results in more tasks being placed on our 24693041982dSJonathan Peyton // run queue, reset use_own_tasks 24707cc577a4SJonathan Peyton if (!use_own_tasks && TCR_4(threads_data[tid].td.td_deque_ntasks) != 0) { 24713041982dSJonathan Peyton KA_TRACE(20, ("__kmp_execute_tasks_template: T#%d stolen task spawned " 24723041982dSJonathan Peyton "other tasks, restart\n", 24733041982dSJonathan Peyton gtid)); 24747cc577a4SJonathan Peyton use_own_tasks = 1; 24757cc577a4SJonathan Peyton new_victim = 0; 24767cc577a4SJonathan Peyton } 24777cc577a4SJonathan Peyton } 24787cc577a4SJonathan Peyton 24793041982dSJonathan Peyton // The task source has been exhausted. If in final spin loop of barrier, check 24803041982dSJonathan Peyton // if termination condition is satisfied. 24817cc577a4SJonathan Peyton #if OMP_45_ENABLED 24823041982dSJonathan Peyton // The work queue may be empty but there might be proxy tasks still 24833041982dSJonathan Peyton // executing 24847cc577a4SJonathan Peyton if (final_spin && TCR_4(current_task->td_incomplete_child_tasks) == 0) 24857cc577a4SJonathan Peyton #else 24867cc577a4SJonathan Peyton if (final_spin) 24877cc577a4SJonathan Peyton #endif 24887cc577a4SJonathan Peyton { 24893041982dSJonathan Peyton // First, decrement the #unfinished threads, if that has not already been 24903041982dSJonathan Peyton // done. This decrement might be to the spin location, and result in the 24913041982dSJonathan Peyton // termination condition being satisfied. 24927cc577a4SJonathan Peyton if (!*thread_finished) { 2493c47afcd9SAndrey Churbanov kmp_int32 count; 24947cc577a4SJonathan Peyton 24955ba90c79SAndrey Churbanov count = KMP_TEST_THEN_DEC32(unfinished_threads) - 1; 24963041982dSJonathan Peyton KA_TRACE(20, ("__kmp_execute_tasks_template: T#%d dec " 24973041982dSJonathan Peyton "unfinished_threads to %d task_team=%p\n", 24987cc577a4SJonathan Peyton gtid, count, task_team)); 24997cc577a4SJonathan Peyton *thread_finished = TRUE; 25007cc577a4SJonathan Peyton } 25017cc577a4SJonathan Peyton 25027cc577a4SJonathan Peyton // It is now unsafe to reference thread->th.th_team !!! 25033041982dSJonathan Peyton // Decrementing task_team->tt.tt_unfinished_threads can allow the master 25043041982dSJonathan Peyton // thread to pass through the barrier, where it might reset each thread's 25053041982dSJonathan Peyton // th.th_team field for the next parallel region. If we can steal more 25063041982dSJonathan Peyton // work, we know that this has not happened yet. 25077cc577a4SJonathan Peyton if (flag != NULL && flag->done_check()) { 25083041982dSJonathan Peyton KA_TRACE( 25093041982dSJonathan Peyton 15, 25103041982dSJonathan Peyton ("__kmp_execute_tasks_template: T#%d spin condition satisfied\n", 25113041982dSJonathan Peyton gtid)); 25127cc577a4SJonathan Peyton return TRUE; 25137cc577a4SJonathan Peyton } 25147cc577a4SJonathan Peyton } 25157cc577a4SJonathan Peyton 25163041982dSJonathan Peyton // If this thread's task team is NULL, master has recognized that there are 25173041982dSJonathan Peyton // no more tasks; bail out 25187cc577a4SJonathan Peyton if (thread->th.th_task_team == NULL) { 25193041982dSJonathan Peyton KA_TRACE(15, 25203041982dSJonathan Peyton ("__kmp_execute_tasks_template: T#%d no more tasks\n", gtid)); 25217cc577a4SJonathan Peyton return FALSE; 25227cc577a4SJonathan Peyton } 25237cc577a4SJonathan Peyton 25247cc577a4SJonathan Peyton #if OMP_45_ENABLED 25253041982dSJonathan Peyton // We could be getting tasks from target constructs; if this is the only 25263041982dSJonathan Peyton // thread, keep trying to execute tasks from own queue 25277cc577a4SJonathan Peyton if (nthreads == 1) 25287cc577a4SJonathan Peyton use_own_tasks = 1; 25297cc577a4SJonathan Peyton else 25307cc577a4SJonathan Peyton #endif 25317cc577a4SJonathan Peyton { 25323041982dSJonathan Peyton KA_TRACE(15, 25333041982dSJonathan Peyton ("__kmp_execute_tasks_template: T#%d can't find work\n", gtid)); 25347cc577a4SJonathan Peyton return FALSE; 25357cc577a4SJonathan Peyton } 25367cc577a4SJonathan Peyton } 25377cc577a4SJonathan Peyton } 25387cc577a4SJonathan Peyton 25393041982dSJonathan Peyton int __kmp_execute_tasks_32( 25403041982dSJonathan Peyton kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32 *flag, int final_spin, 25413041982dSJonathan Peyton int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj), 25423041982dSJonathan Peyton kmp_int32 is_constrained) { 25433041982dSJonathan Peyton return __kmp_execute_tasks_template( 25443041982dSJonathan Peyton thread, gtid, flag, final_spin, 25453041982dSJonathan Peyton thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained); 25467cc577a4SJonathan Peyton } 25477cc577a4SJonathan Peyton 25483041982dSJonathan Peyton int __kmp_execute_tasks_64( 25493041982dSJonathan Peyton kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64 *flag, int final_spin, 25503041982dSJonathan Peyton int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj), 25513041982dSJonathan Peyton kmp_int32 is_constrained) { 25523041982dSJonathan Peyton return __kmp_execute_tasks_template( 25533041982dSJonathan Peyton thread, gtid, flag, final_spin, 25543041982dSJonathan Peyton thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained); 25557cc577a4SJonathan Peyton } 25567cc577a4SJonathan Peyton 25573041982dSJonathan Peyton int __kmp_execute_tasks_oncore( 25583041982dSJonathan Peyton kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag, int final_spin, 25593041982dSJonathan Peyton int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj), 25603041982dSJonathan Peyton kmp_int32 is_constrained) { 25613041982dSJonathan Peyton return __kmp_execute_tasks_template( 25623041982dSJonathan Peyton thread, gtid, flag, final_spin, 25633041982dSJonathan Peyton thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained); 25647cc577a4SJonathan Peyton } 25657cc577a4SJonathan Peyton 25667cc577a4SJonathan Peyton // __kmp_enable_tasking: Allocate task team and resume threads sleeping at the 25677cc577a4SJonathan Peyton // next barrier so they can assist in executing enqueued tasks. 25687cc577a4SJonathan Peyton // First thread in allocates the task team atomically. 25693041982dSJonathan Peyton static void __kmp_enable_tasking(kmp_task_team_t *task_team, 25703041982dSJonathan Peyton kmp_info_t *this_thr) { 25717cc577a4SJonathan Peyton kmp_thread_data_t *threads_data; 25727cc577a4SJonathan Peyton int nthreads, i, is_init_thread; 25737cc577a4SJonathan Peyton 25747cc577a4SJonathan Peyton KA_TRACE(10, ("__kmp_enable_tasking(enter): T#%d\n", 25757cc577a4SJonathan Peyton __kmp_gtid_from_thread(this_thr))); 25767cc577a4SJonathan Peyton 25777cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(task_team != NULL); 25787cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(this_thr->th.th_team != NULL); 25797cc577a4SJonathan Peyton 25807cc577a4SJonathan Peyton nthreads = task_team->tt.tt_nproc; 25817cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(nthreads > 0); 25827cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(nthreads == this_thr->th.th_team->t.t_nproc); 25837cc577a4SJonathan Peyton 25847cc577a4SJonathan Peyton // Allocate or increase the size of threads_data if necessary 25857cc577a4SJonathan Peyton is_init_thread = __kmp_realloc_task_threads_data(this_thr, task_team); 25867cc577a4SJonathan Peyton 25877cc577a4SJonathan Peyton if (!is_init_thread) { 25887cc577a4SJonathan Peyton // Some other thread already set up the array. 25893041982dSJonathan Peyton KA_TRACE( 25903041982dSJonathan Peyton 20, 25913041982dSJonathan Peyton ("__kmp_enable_tasking(exit): T#%d: threads array already set up.\n", 25927cc577a4SJonathan Peyton __kmp_gtid_from_thread(this_thr))); 25937cc577a4SJonathan Peyton return; 25947cc577a4SJonathan Peyton } 25957cc577a4SJonathan Peyton threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data); 25967cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(threads_data != NULL); 25977cc577a4SJonathan Peyton 25987cc577a4SJonathan Peyton if ((__kmp_tasking_mode == tskm_task_teams) && 25993041982dSJonathan Peyton (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME)) { 26007cc577a4SJonathan Peyton // Release any threads sleeping at the barrier, so that they can steal 26017cc577a4SJonathan Peyton // tasks and execute them. In extra barrier mode, tasks do not sleep 26027cc577a4SJonathan Peyton // at the separate tasking barrier, so this isn't a problem. 26037cc577a4SJonathan Peyton for (i = 0; i < nthreads; i++) { 26047cc577a4SJonathan Peyton volatile void *sleep_loc; 26057cc577a4SJonathan Peyton kmp_info_t *thread = threads_data[i].td.td_thr; 26067cc577a4SJonathan Peyton 26077cc577a4SJonathan Peyton if (i == this_thr->th.th_info.ds.ds_tid) { 26087cc577a4SJonathan Peyton continue; 26097cc577a4SJonathan Peyton } 26107cc577a4SJonathan Peyton // Since we haven't locked the thread's suspend mutex lock at this 26117cc577a4SJonathan Peyton // point, there is a small window where a thread might be putting 26127cc577a4SJonathan Peyton // itself to sleep, but hasn't set the th_sleep_loc field yet. 26137cc577a4SJonathan Peyton // To work around this, __kmp_execute_tasks_template() periodically checks 26143041982dSJonathan Peyton // see if other threads are sleeping (using the same random mechanism that 26153041982dSJonathan Peyton // is used for task stealing) and awakens them if they are. 2616c47afcd9SAndrey Churbanov if ((sleep_loc = TCR_PTR(CCAST(void *, thread->th.th_sleep_loc))) != 2617c47afcd9SAndrey Churbanov NULL) { 26187cc577a4SJonathan Peyton KF_TRACE(50, ("__kmp_enable_tasking: T#%d waking up thread T#%d\n", 26197cc577a4SJonathan Peyton __kmp_gtid_from_thread(this_thr), 26207cc577a4SJonathan Peyton __kmp_gtid_from_thread(thread))); 26217cc577a4SJonathan Peyton __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc); 26223041982dSJonathan Peyton } else { 26237cc577a4SJonathan Peyton KF_TRACE(50, ("__kmp_enable_tasking: T#%d don't wake up thread T#%d\n", 26247cc577a4SJonathan Peyton __kmp_gtid_from_thread(this_thr), 26257cc577a4SJonathan Peyton __kmp_gtid_from_thread(thread))); 26267cc577a4SJonathan Peyton } 26277cc577a4SJonathan Peyton } 26287cc577a4SJonathan Peyton } 26297cc577a4SJonathan Peyton 26307cc577a4SJonathan Peyton KA_TRACE(10, ("__kmp_enable_tasking(exit): T#%d\n", 26317cc577a4SJonathan Peyton __kmp_gtid_from_thread(this_thr))); 26327cc577a4SJonathan Peyton } 26337cc577a4SJonathan Peyton 26347cc577a4SJonathan Peyton /* // TODO: Check the comment consistency 26357cc577a4SJonathan Peyton * Utility routines for "task teams". A task team (kmp_task_t) is kind of 26367cc577a4SJonathan Peyton * like a shadow of the kmp_team_t data struct, with a different lifetime. 26377cc577a4SJonathan Peyton * After a child * thread checks into a barrier and calls __kmp_release() from 26387cc577a4SJonathan Peyton * the particular variant of __kmp_<barrier_kind>_barrier_gather(), it can no 26397cc577a4SJonathan Peyton * longer assume that the kmp_team_t structure is intact (at any moment, the 26407cc577a4SJonathan Peyton * master thread may exit the barrier code and free the team data structure, 26417cc577a4SJonathan Peyton * and return the threads to the thread pool). 26427cc577a4SJonathan Peyton * 26437cc577a4SJonathan Peyton * This does not work with the the tasking code, as the thread is still 26447cc577a4SJonathan Peyton * expected to participate in the execution of any tasks that may have been 26457cc577a4SJonathan Peyton * spawned my a member of the team, and the thread still needs access to all 26467cc577a4SJonathan Peyton * to each thread in the team, so that it can steal work from it. 26477cc577a4SJonathan Peyton * 26487cc577a4SJonathan Peyton * Enter the existence of the kmp_task_team_t struct. It employs a reference 26497cc577a4SJonathan Peyton * counting mechanims, and is allocated by the master thread before calling 26507cc577a4SJonathan Peyton * __kmp_<barrier_kind>_release, and then is release by the last thread to 26517cc577a4SJonathan Peyton * exit __kmp_<barrier_kind>_release at the next barrier. I.e. the lifetimes 26527cc577a4SJonathan Peyton * of the kmp_task_team_t structs for consecutive barriers can overlap 26537cc577a4SJonathan Peyton * (and will, unless the master thread is the last thread to exit the barrier 26547cc577a4SJonathan Peyton * release phase, which is not typical). 26557cc577a4SJonathan Peyton * 26567cc577a4SJonathan Peyton * The existence of such a struct is useful outside the context of tasking, 26577cc577a4SJonathan Peyton * but for now, I'm trying to keep it specific to the OMP_30_ENABLED macro, 26587cc577a4SJonathan Peyton * so that any performance differences show up when comparing the 2.5 vs. 3.0 26597cc577a4SJonathan Peyton * libraries. 26607cc577a4SJonathan Peyton * 26617cc577a4SJonathan Peyton * We currently use the existence of the threads array as an indicator that 26627cc577a4SJonathan Peyton * tasks were spawned since the last barrier. If the structure is to be 26637cc577a4SJonathan Peyton * useful outside the context of tasking, then this will have to change, but 26647cc577a4SJonathan Peyton * not settting the field minimizes the performance impact of tasking on 26657cc577a4SJonathan Peyton * barriers, when no explicit tasks were spawned (pushed, actually). 26667cc577a4SJonathan Peyton */ 26677cc577a4SJonathan Peyton 26683041982dSJonathan Peyton static kmp_task_team_t *__kmp_free_task_teams = 26693041982dSJonathan Peyton NULL; // Free list for task_team data structures 26707cc577a4SJonathan Peyton // Lock for task team data structures 26713041982dSJonathan Peyton static kmp_bootstrap_lock_t __kmp_task_team_lock = 26723041982dSJonathan Peyton KMP_BOOTSTRAP_LOCK_INITIALIZER(__kmp_task_team_lock); 26737cc577a4SJonathan Peyton 26747cc577a4SJonathan Peyton // __kmp_alloc_task_deque: 26757cc577a4SJonathan Peyton // Allocates a task deque for a particular thread, and initialize the necessary 26767cc577a4SJonathan Peyton // data structures relating to the deque. This only happens once per thread 26773041982dSJonathan Peyton // per task team since task teams are recycled. No lock is needed during 26783041982dSJonathan Peyton // allocation since each thread allocates its own deque. 26793041982dSJonathan Peyton static void __kmp_alloc_task_deque(kmp_info_t *thread, 26803041982dSJonathan Peyton kmp_thread_data_t *thread_data) { 26817cc577a4SJonathan Peyton __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock); 26827cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(thread_data->td.td_deque == NULL); 26837cc577a4SJonathan Peyton 26847cc577a4SJonathan Peyton // Initialize last stolen task field to "none" 26857cc577a4SJonathan Peyton thread_data->td.td_deque_last_stolen = -1; 26867cc577a4SJonathan Peyton 26877cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == 0); 26887cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(thread_data->td.td_deque_head == 0); 26897cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(thread_data->td.td_deque_tail == 0); 26907cc577a4SJonathan Peyton 26913041982dSJonathan Peyton KE_TRACE( 26923041982dSJonathan Peyton 10, 26933041982dSJonathan Peyton ("__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n", 26947cc577a4SJonathan Peyton __kmp_gtid_from_thread(thread), INITIAL_TASK_DEQUE_SIZE, thread_data)); 26957cc577a4SJonathan Peyton // Allocate space for task deque, and zero the deque 26967cc577a4SJonathan Peyton // Cannot use __kmp_thread_calloc() because threads not around for 26977cc577a4SJonathan Peyton // kmp_reap_task_team( ). 26983041982dSJonathan Peyton thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate( 26993041982dSJonathan Peyton INITIAL_TASK_DEQUE_SIZE * sizeof(kmp_taskdata_t *)); 27007cc577a4SJonathan Peyton thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE; 27017cc577a4SJonathan Peyton } 27027cc577a4SJonathan Peyton 27037cc577a4SJonathan Peyton // __kmp_realloc_task_deque: 27043041982dSJonathan Peyton // Re-allocates a task deque for a particular thread, copies the content from 27053041982dSJonathan Peyton // the old deque and adjusts the necessary data structures relating to the 27063041982dSJonathan Peyton // deque. This operation must be done with a the deque_lock being held 27073041982dSJonathan Peyton static void __kmp_realloc_task_deque(kmp_info_t *thread, 27083041982dSJonathan Peyton kmp_thread_data_t *thread_data) { 27097cc577a4SJonathan Peyton kmp_int32 size = TASK_DEQUE_SIZE(thread_data->td); 27107cc577a4SJonathan Peyton kmp_int32 new_size = 2 * size; 27117cc577a4SJonathan Peyton 27123041982dSJonathan Peyton KE_TRACE(10, ("__kmp_realloc_task_deque: T#%d reallocating deque[from %d to " 27133041982dSJonathan Peyton "%d] for thread_data %p\n", 27147cc577a4SJonathan Peyton __kmp_gtid_from_thread(thread), size, new_size, thread_data)); 27157cc577a4SJonathan Peyton 27163041982dSJonathan Peyton kmp_taskdata_t **new_deque = 27173041982dSJonathan Peyton (kmp_taskdata_t **)__kmp_allocate(new_size * sizeof(kmp_taskdata_t *)); 27187cc577a4SJonathan Peyton 27197cc577a4SJonathan Peyton int i, j; 27203041982dSJonathan Peyton for (i = thread_data->td.td_deque_head, j = 0; j < size; 27213041982dSJonathan Peyton i = (i + 1) & TASK_DEQUE_MASK(thread_data->td), j++) 27227cc577a4SJonathan Peyton new_deque[j] = thread_data->td.td_deque[i]; 27237cc577a4SJonathan Peyton 27247cc577a4SJonathan Peyton __kmp_free(thread_data->td.td_deque); 27257cc577a4SJonathan Peyton 27267cc577a4SJonathan Peyton thread_data->td.td_deque_head = 0; 27277cc577a4SJonathan Peyton thread_data->td.td_deque_tail = size; 27287cc577a4SJonathan Peyton thread_data->td.td_deque = new_deque; 27297cc577a4SJonathan Peyton thread_data->td.td_deque_size = new_size; 27307cc577a4SJonathan Peyton } 27317cc577a4SJonathan Peyton 27327cc577a4SJonathan Peyton // __kmp_free_task_deque: 27333041982dSJonathan Peyton // Deallocates a task deque for a particular thread. Happens at library 27343041982dSJonathan Peyton // deallocation so don't need to reset all thread data fields. 27353041982dSJonathan Peyton static void __kmp_free_task_deque(kmp_thread_data_t *thread_data) { 27367cc577a4SJonathan Peyton if (thread_data->td.td_deque != NULL) { 27371b536724SJonathan Peyton __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock); 27387cc577a4SJonathan Peyton TCW_4(thread_data->td.td_deque_ntasks, 0); 27397cc577a4SJonathan Peyton __kmp_free(thread_data->td.td_deque); 27407cc577a4SJonathan Peyton thread_data->td.td_deque = NULL; 27417cc577a4SJonathan Peyton __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock); 27421b536724SJonathan Peyton } 27437cc577a4SJonathan Peyton 27447cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK 27457cc577a4SJonathan Peyton // GEH: Figure out what to do here for td_susp_tied_tasks 27467cc577a4SJonathan Peyton if (thread_data->td.td_susp_tied_tasks.ts_entries != TASK_STACK_EMPTY) { 27477cc577a4SJonathan Peyton __kmp_free_task_stack(__kmp_thread_from_gtid(gtid), thread_data); 27487cc577a4SJonathan Peyton } 27497cc577a4SJonathan Peyton #endif // BUILD_TIED_TASK_STACK 27507cc577a4SJonathan Peyton } 27517cc577a4SJonathan Peyton 27527cc577a4SJonathan Peyton // __kmp_realloc_task_threads_data: 27533041982dSJonathan Peyton // Allocates a threads_data array for a task team, either by allocating an 27543041982dSJonathan Peyton // initial array or enlarging an existing array. Only the first thread to get 27553041982dSJonathan Peyton // the lock allocs or enlarges the array and re-initializes the array eleemnts. 27567cc577a4SJonathan Peyton // That thread returns "TRUE", the rest return "FALSE". 27577cc577a4SJonathan Peyton // Assumes that the new array size is given by task_team -> tt.tt_nproc. 27587cc577a4SJonathan Peyton // The current size is given by task_team -> tt.tt_max_threads. 27593041982dSJonathan Peyton static int __kmp_realloc_task_threads_data(kmp_info_t *thread, 27603041982dSJonathan Peyton kmp_task_team_t *task_team) { 27617cc577a4SJonathan Peyton kmp_thread_data_t **threads_data_p; 27627cc577a4SJonathan Peyton kmp_int32 nthreads, maxthreads; 27637cc577a4SJonathan Peyton int is_init_thread = FALSE; 27647cc577a4SJonathan Peyton 27657cc577a4SJonathan Peyton if (TCR_4(task_team->tt.tt_found_tasks)) { 27667cc577a4SJonathan Peyton // Already reallocated and initialized. 27677cc577a4SJonathan Peyton return FALSE; 27687cc577a4SJonathan Peyton } 27697cc577a4SJonathan Peyton 27707cc577a4SJonathan Peyton threads_data_p = &task_team->tt.tt_threads_data; 27717cc577a4SJonathan Peyton nthreads = task_team->tt.tt_nproc; 27727cc577a4SJonathan Peyton maxthreads = task_team->tt.tt_max_threads; 27737cc577a4SJonathan Peyton 27743041982dSJonathan Peyton // All threads must lock when they encounter the first task of the implicit 27753041982dSJonathan Peyton // task region to make sure threads_data fields are (re)initialized before 27763041982dSJonathan Peyton // used. 27777cc577a4SJonathan Peyton __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock); 27787cc577a4SJonathan Peyton 27797cc577a4SJonathan Peyton if (!TCR_4(task_team->tt.tt_found_tasks)) { 27807cc577a4SJonathan Peyton // first thread to enable tasking 27817cc577a4SJonathan Peyton kmp_team_t *team = thread->th.th_team; 27827cc577a4SJonathan Peyton int i; 27837cc577a4SJonathan Peyton 27847cc577a4SJonathan Peyton is_init_thread = TRUE; 27857cc577a4SJonathan Peyton if (maxthreads < nthreads) { 27867cc577a4SJonathan Peyton 27877cc577a4SJonathan Peyton if (*threads_data_p != NULL) { 27887cc577a4SJonathan Peyton kmp_thread_data_t *old_data = *threads_data_p; 27897cc577a4SJonathan Peyton kmp_thread_data_t *new_data = NULL; 27907cc577a4SJonathan Peyton 27913041982dSJonathan Peyton KE_TRACE( 27923041982dSJonathan Peyton 10, 27933041982dSJonathan Peyton ("__kmp_realloc_task_threads_data: T#%d reallocating " 27947cc577a4SJonathan Peyton "threads data for task_team %p, new_size = %d, old_size = %d\n", 27953041982dSJonathan Peyton __kmp_gtid_from_thread(thread), task_team, nthreads, maxthreads)); 27967cc577a4SJonathan Peyton // Reallocate threads_data to have more elements than current array 27977cc577a4SJonathan Peyton // Cannot use __kmp_thread_realloc() because threads not around for 27987cc577a4SJonathan Peyton // kmp_reap_task_team( ). Note all new array entries are initialized 27997cc577a4SJonathan Peyton // to zero by __kmp_allocate(). 28003041982dSJonathan Peyton new_data = (kmp_thread_data_t *)__kmp_allocate( 28013041982dSJonathan Peyton nthreads * sizeof(kmp_thread_data_t)); 28027cc577a4SJonathan Peyton // copy old data to new data 28037cc577a4SJonathan Peyton KMP_MEMCPY_S((void *)new_data, nthreads * sizeof(kmp_thread_data_t), 280471483f2dSAndrey Churbanov (void *)old_data, maxthreads * sizeof(kmp_thread_data_t)); 28057cc577a4SJonathan Peyton 28067cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK 28077cc577a4SJonathan Peyton // GEH: Figure out if this is the right thing to do 28087cc577a4SJonathan Peyton for (i = maxthreads; i < nthreads; i++) { 28097cc577a4SJonathan Peyton kmp_thread_data_t *thread_data = &(*threads_data_p)[i]; 28107cc577a4SJonathan Peyton __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data); 28117cc577a4SJonathan Peyton } 28127cc577a4SJonathan Peyton #endif // BUILD_TIED_TASK_STACK 28137cc577a4SJonathan Peyton // Install the new data and free the old data 28147cc577a4SJonathan Peyton (*threads_data_p) = new_data; 28157cc577a4SJonathan Peyton __kmp_free(old_data); 28163041982dSJonathan Peyton } else { 28177cc577a4SJonathan Peyton KE_TRACE(10, ("__kmp_realloc_task_threads_data: T#%d allocating " 28187cc577a4SJonathan Peyton "threads data for task_team %p, size = %d\n", 28197cc577a4SJonathan Peyton __kmp_gtid_from_thread(thread), task_team, nthreads)); 28207cc577a4SJonathan Peyton // Make the initial allocate for threads_data array, and zero entries 28217cc577a4SJonathan Peyton // Cannot use __kmp_thread_calloc() because threads not around for 28227cc577a4SJonathan Peyton // kmp_reap_task_team( ). 28237cc577a4SJonathan Peyton ANNOTATE_IGNORE_WRITES_BEGIN(); 28243041982dSJonathan Peyton *threads_data_p = (kmp_thread_data_t *)__kmp_allocate( 28253041982dSJonathan Peyton nthreads * sizeof(kmp_thread_data_t)); 28267cc577a4SJonathan Peyton ANNOTATE_IGNORE_WRITES_END(); 28277cc577a4SJonathan Peyton #ifdef BUILD_TIED_TASK_STACK 28287cc577a4SJonathan Peyton // GEH: Figure out if this is the right thing to do 28297cc577a4SJonathan Peyton for (i = 0; i < nthreads; i++) { 28307cc577a4SJonathan Peyton kmp_thread_data_t *thread_data = &(*threads_data_p)[i]; 28317cc577a4SJonathan Peyton __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data); 28327cc577a4SJonathan Peyton } 28337cc577a4SJonathan Peyton #endif // BUILD_TIED_TASK_STACK 28347cc577a4SJonathan Peyton } 28357cc577a4SJonathan Peyton task_team->tt.tt_max_threads = nthreads; 28363041982dSJonathan Peyton } else { 28377cc577a4SJonathan Peyton // If array has (more than) enough elements, go ahead and use it 28387cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(*threads_data_p != NULL); 28397cc577a4SJonathan Peyton } 28407cc577a4SJonathan Peyton 28417cc577a4SJonathan Peyton // initialize threads_data pointers back to thread_info structures 28427cc577a4SJonathan Peyton for (i = 0; i < nthreads; i++) { 28437cc577a4SJonathan Peyton kmp_thread_data_t *thread_data = &(*threads_data_p)[i]; 28447cc577a4SJonathan Peyton thread_data->td.td_thr = team->t.t_threads[i]; 28457cc577a4SJonathan Peyton 28467cc577a4SJonathan Peyton if (thread_data->td.td_deque_last_stolen >= nthreads) { 28477cc577a4SJonathan Peyton // The last stolen field survives across teams / barrier, and the number 28487cc577a4SJonathan Peyton // of threads may have changed. It's possible (likely?) that a new 28493041982dSJonathan Peyton // parallel region will exhibit the same behavior as previous region. 28507cc577a4SJonathan Peyton thread_data->td.td_deque_last_stolen = -1; 28517cc577a4SJonathan Peyton } 28527cc577a4SJonathan Peyton } 28537cc577a4SJonathan Peyton 28547cc577a4SJonathan Peyton KMP_MB(); 28557cc577a4SJonathan Peyton TCW_SYNC_4(task_team->tt.tt_found_tasks, TRUE); 28567cc577a4SJonathan Peyton } 28577cc577a4SJonathan Peyton 28587cc577a4SJonathan Peyton __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock); 28597cc577a4SJonathan Peyton return is_init_thread; 28607cc577a4SJonathan Peyton } 28617cc577a4SJonathan Peyton 28627cc577a4SJonathan Peyton // __kmp_free_task_threads_data: 28637cc577a4SJonathan Peyton // Deallocates a threads_data array for a task team, including any attached 28647cc577a4SJonathan Peyton // tasking deques. Only occurs at library shutdown. 28653041982dSJonathan Peyton static void __kmp_free_task_threads_data(kmp_task_team_t *task_team) { 28667cc577a4SJonathan Peyton __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock); 28677cc577a4SJonathan Peyton if (task_team->tt.tt_threads_data != NULL) { 28687cc577a4SJonathan Peyton int i; 28697cc577a4SJonathan Peyton for (i = 0; i < task_team->tt.tt_max_threads; i++) { 28707cc577a4SJonathan Peyton __kmp_free_task_deque(&task_team->tt.tt_threads_data[i]); 28717cc577a4SJonathan Peyton } 28727cc577a4SJonathan Peyton __kmp_free(task_team->tt.tt_threads_data); 28737cc577a4SJonathan Peyton task_team->tt.tt_threads_data = NULL; 28747cc577a4SJonathan Peyton } 28757cc577a4SJonathan Peyton __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock); 28767cc577a4SJonathan Peyton } 28777cc577a4SJonathan Peyton 28787cc577a4SJonathan Peyton // __kmp_allocate_task_team: 28797cc577a4SJonathan Peyton // Allocates a task team associated with a specific team, taking it from 28803041982dSJonathan Peyton // the global task team free list if possible. Also initializes data 28813041982dSJonathan Peyton // structures. 28823041982dSJonathan Peyton static kmp_task_team_t *__kmp_allocate_task_team(kmp_info_t *thread, 28833041982dSJonathan Peyton kmp_team_t *team) { 28847cc577a4SJonathan Peyton kmp_task_team_t *task_team = NULL; 28857cc577a4SJonathan Peyton int nthreads; 28867cc577a4SJonathan Peyton 28877cc577a4SJonathan Peyton KA_TRACE(20, ("__kmp_allocate_task_team: T#%d entering; team = %p\n", 28887cc577a4SJonathan Peyton (thread ? __kmp_gtid_from_thread(thread) : -1), team)); 28897cc577a4SJonathan Peyton 28907cc577a4SJonathan Peyton if (TCR_PTR(__kmp_free_task_teams) != NULL) { 28917cc577a4SJonathan Peyton // Take a task team from the task team pool 28927cc577a4SJonathan Peyton __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock); 28937cc577a4SJonathan Peyton if (__kmp_free_task_teams != NULL) { 28947cc577a4SJonathan Peyton task_team = __kmp_free_task_teams; 28957cc577a4SJonathan Peyton TCW_PTR(__kmp_free_task_teams, task_team->tt.tt_next); 28967cc577a4SJonathan Peyton task_team->tt.tt_next = NULL; 28977cc577a4SJonathan Peyton } 28987cc577a4SJonathan Peyton __kmp_release_bootstrap_lock(&__kmp_task_team_lock); 28997cc577a4SJonathan Peyton } 29007cc577a4SJonathan Peyton 29017cc577a4SJonathan Peyton if (task_team == NULL) { 29027cc577a4SJonathan Peyton KE_TRACE(10, ("__kmp_allocate_task_team: T#%d allocating " 29037cc577a4SJonathan Peyton "task team for team %p\n", 29047cc577a4SJonathan Peyton __kmp_gtid_from_thread(thread), team)); 29057cc577a4SJonathan Peyton // Allocate a new task team if one is not available. 29067cc577a4SJonathan Peyton // Cannot use __kmp_thread_malloc() because threads not around for 29077cc577a4SJonathan Peyton // kmp_reap_task_team( ). 29087cc577a4SJonathan Peyton task_team = (kmp_task_team_t *)__kmp_allocate(sizeof(kmp_task_team_t)); 29097cc577a4SJonathan Peyton __kmp_init_bootstrap_lock(&task_team->tt.tt_threads_lock); 29103041982dSJonathan Peyton // AC: __kmp_allocate zeroes returned memory 29113041982dSJonathan Peyton // task_team -> tt.tt_threads_data = NULL; 29127cc577a4SJonathan Peyton // task_team -> tt.tt_max_threads = 0; 29137cc577a4SJonathan Peyton // task_team -> tt.tt_next = NULL; 29147cc577a4SJonathan Peyton } 29157cc577a4SJonathan Peyton 29167cc577a4SJonathan Peyton TCW_4(task_team->tt.tt_found_tasks, FALSE); 29177cc577a4SJonathan Peyton #if OMP_45_ENABLED 29187cc577a4SJonathan Peyton TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE); 29197cc577a4SJonathan Peyton #endif 29207cc577a4SJonathan Peyton task_team->tt.tt_nproc = nthreads = team->t.t_nproc; 29217cc577a4SJonathan Peyton 29227cc577a4SJonathan Peyton TCW_4(task_team->tt.tt_unfinished_threads, nthreads); 29237cc577a4SJonathan Peyton TCW_4(task_team->tt.tt_active, TRUE); 29247cc577a4SJonathan Peyton 29253041982dSJonathan Peyton KA_TRACE(20, ("__kmp_allocate_task_team: T#%d exiting; task_team = %p " 29263041982dSJonathan Peyton "unfinished_threads init'd to %d\n", 29273041982dSJonathan Peyton (thread ? __kmp_gtid_from_thread(thread) : -1), task_team, 29283041982dSJonathan Peyton task_team->tt.tt_unfinished_threads)); 29297cc577a4SJonathan Peyton return task_team; 29307cc577a4SJonathan Peyton } 29317cc577a4SJonathan Peyton 29327cc577a4SJonathan Peyton // __kmp_free_task_team: 29337cc577a4SJonathan Peyton // Frees the task team associated with a specific thread, and adds it 29347cc577a4SJonathan Peyton // to the global task team free list. 29353041982dSJonathan Peyton void __kmp_free_task_team(kmp_info_t *thread, kmp_task_team_t *task_team) { 29367cc577a4SJonathan Peyton KA_TRACE(20, ("__kmp_free_task_team: T#%d task_team = %p\n", 29377cc577a4SJonathan Peyton thread ? __kmp_gtid_from_thread(thread) : -1, task_team)); 29387cc577a4SJonathan Peyton 29397cc577a4SJonathan Peyton // Put task team back on free list 29407cc577a4SJonathan Peyton __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock); 29417cc577a4SJonathan Peyton 29427cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(task_team->tt.tt_next == NULL); 29437cc577a4SJonathan Peyton task_team->tt.tt_next = __kmp_free_task_teams; 29447cc577a4SJonathan Peyton TCW_PTR(__kmp_free_task_teams, task_team); 29457cc577a4SJonathan Peyton 29467cc577a4SJonathan Peyton __kmp_release_bootstrap_lock(&__kmp_task_team_lock); 29477cc577a4SJonathan Peyton } 29487cc577a4SJonathan Peyton 29497cc577a4SJonathan Peyton // __kmp_reap_task_teams: 29507cc577a4SJonathan Peyton // Free all the task teams on the task team free list. 29517cc577a4SJonathan Peyton // Should only be done during library shutdown. 29523041982dSJonathan Peyton // Cannot do anything that needs a thread structure or gtid since they are 29533041982dSJonathan Peyton // already gone. 29543041982dSJonathan Peyton void __kmp_reap_task_teams(void) { 29557cc577a4SJonathan Peyton kmp_task_team_t *task_team; 29567cc577a4SJonathan Peyton 29577cc577a4SJonathan Peyton if (TCR_PTR(__kmp_free_task_teams) != NULL) { 29587cc577a4SJonathan Peyton // Free all task_teams on the free list 29597cc577a4SJonathan Peyton __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock); 29607cc577a4SJonathan Peyton while ((task_team = __kmp_free_task_teams) != NULL) { 29617cc577a4SJonathan Peyton __kmp_free_task_teams = task_team->tt.tt_next; 29627cc577a4SJonathan Peyton task_team->tt.tt_next = NULL; 29637cc577a4SJonathan Peyton 29647cc577a4SJonathan Peyton // Free threads_data if necessary 29657cc577a4SJonathan Peyton if (task_team->tt.tt_threads_data != NULL) { 29667cc577a4SJonathan Peyton __kmp_free_task_threads_data(task_team); 29677cc577a4SJonathan Peyton } 29687cc577a4SJonathan Peyton __kmp_free(task_team); 29697cc577a4SJonathan Peyton } 29707cc577a4SJonathan Peyton __kmp_release_bootstrap_lock(&__kmp_task_team_lock); 29717cc577a4SJonathan Peyton } 29727cc577a4SJonathan Peyton } 29737cc577a4SJonathan Peyton 29747cc577a4SJonathan Peyton // __kmp_wait_to_unref_task_teams: 29757cc577a4SJonathan Peyton // Some threads could still be in the fork barrier release code, possibly 29767cc577a4SJonathan Peyton // trying to steal tasks. Wait for each thread to unreference its task team. 29773041982dSJonathan Peyton void __kmp_wait_to_unref_task_teams(void) { 29787cc577a4SJonathan Peyton kmp_info_t *thread; 29797cc577a4SJonathan Peyton kmp_uint32 spins; 29807cc577a4SJonathan Peyton int done; 29817cc577a4SJonathan Peyton 29827cc577a4SJonathan Peyton KMP_INIT_YIELD(spins); 29837cc577a4SJonathan Peyton 29847cc577a4SJonathan Peyton for (;;) { 29857cc577a4SJonathan Peyton done = TRUE; 29867cc577a4SJonathan Peyton 29877cc577a4SJonathan Peyton // TODO: GEH - this may be is wrong because some sync would be necessary 29883041982dSJonathan Peyton // in case threads are added to the pool during the traversal. Need to 29893041982dSJonathan Peyton // verify that lock for thread pool is held when calling this routine. 2990c47afcd9SAndrey Churbanov for (thread = CCAST(kmp_info_t *, __kmp_thread_pool); thread != NULL; 29913041982dSJonathan Peyton thread = thread->th.th_next_pool) { 29927cc577a4SJonathan Peyton #if KMP_OS_WINDOWS 29937cc577a4SJonathan Peyton DWORD exit_val; 29947cc577a4SJonathan Peyton #endif 29957cc577a4SJonathan Peyton if (TCR_PTR(thread->th.th_task_team) == NULL) { 29967cc577a4SJonathan Peyton KA_TRACE(10, ("__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n", 29977cc577a4SJonathan Peyton __kmp_gtid_from_thread(thread))); 29987cc577a4SJonathan Peyton continue; 29997cc577a4SJonathan Peyton } 30007cc577a4SJonathan Peyton #if KMP_OS_WINDOWS 30017cc577a4SJonathan Peyton // TODO: GEH - add this check for Linux* OS / OS X* as well? 30027cc577a4SJonathan Peyton if (!__kmp_is_thread_alive(thread, &exit_val)) { 30037cc577a4SJonathan Peyton thread->th.th_task_team = NULL; 30047cc577a4SJonathan Peyton continue; 30057cc577a4SJonathan Peyton } 30067cc577a4SJonathan Peyton #endif 30077cc577a4SJonathan Peyton 30087cc577a4SJonathan Peyton done = FALSE; // Because th_task_team pointer is not NULL for this thread 30097cc577a4SJonathan Peyton 30103041982dSJonathan Peyton KA_TRACE(10, ("__kmp_wait_to_unref_task_team: Waiting for T#%d to " 30113041982dSJonathan Peyton "unreference task_team\n", 30127cc577a4SJonathan Peyton __kmp_gtid_from_thread(thread))); 30137cc577a4SJonathan Peyton 30147cc577a4SJonathan Peyton if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) { 30157cc577a4SJonathan Peyton volatile void *sleep_loc; 30167cc577a4SJonathan Peyton // If the thread is sleeping, awaken it. 3017c47afcd9SAndrey Churbanov if ((sleep_loc = TCR_PTR(CCAST(void *, thread->th.th_sleep_loc))) != 3018c47afcd9SAndrey Churbanov NULL) { 30193041982dSJonathan Peyton KA_TRACE( 30203041982dSJonathan Peyton 10, 30213041982dSJonathan Peyton ("__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n", 30227cc577a4SJonathan Peyton __kmp_gtid_from_thread(thread), __kmp_gtid_from_thread(thread))); 30237cc577a4SJonathan Peyton __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc); 30247cc577a4SJonathan Peyton } 30257cc577a4SJonathan Peyton } 30267cc577a4SJonathan Peyton } 30277cc577a4SJonathan Peyton if (done) { 30287cc577a4SJonathan Peyton break; 30297cc577a4SJonathan Peyton } 30307cc577a4SJonathan Peyton 30313041982dSJonathan Peyton // If we are oversubscribed, or have waited a bit (and library mode is 30323041982dSJonathan Peyton // throughput), yield. Pause is in the following code. 30337cc577a4SJonathan Peyton KMP_YIELD(TCR_4(__kmp_nth) > __kmp_avail_proc); 30347cc577a4SJonathan Peyton KMP_YIELD_SPIN(spins); // Yields only if KMP_LIBRARY=throughput 30357cc577a4SJonathan Peyton } 30367cc577a4SJonathan Peyton } 30377cc577a4SJonathan Peyton 30387cc577a4SJonathan Peyton // __kmp_task_team_setup: Create a task_team for the current team, but use 30397cc577a4SJonathan Peyton // an already created, unused one if it already exists. 30403041982dSJonathan Peyton void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team, int always) { 30417cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec); 30427cc577a4SJonathan Peyton 30433041982dSJonathan Peyton // If this task_team hasn't been created yet, allocate it. It will be used in 30443041982dSJonathan Peyton // the region after the next. 30453041982dSJonathan Peyton // If it exists, it is the current task team and shouldn't be touched yet as 30463041982dSJonathan Peyton // it may still be in use. 30473041982dSJonathan Peyton if (team->t.t_task_team[this_thr->th.th_task_state] == NULL && 30483041982dSJonathan Peyton (always || team->t.t_nproc > 1)) { 30493041982dSJonathan Peyton team->t.t_task_team[this_thr->th.th_task_state] = 30503041982dSJonathan Peyton __kmp_allocate_task_team(this_thr, team); 30513041982dSJonathan Peyton KA_TRACE(20, ("__kmp_task_team_setup: Master T#%d created new task_team %p " 30523041982dSJonathan Peyton "for team %d at parity=%d\n", 30533041982dSJonathan Peyton __kmp_gtid_from_thread(this_thr), 30543041982dSJonathan Peyton team->t.t_task_team[this_thr->th.th_task_state], 30553041982dSJonathan Peyton ((team != NULL) ? team->t.t_id : -1), 30563041982dSJonathan Peyton this_thr->th.th_task_state)); 30577cc577a4SJonathan Peyton } 30587cc577a4SJonathan Peyton 30593041982dSJonathan Peyton // After threads exit the release, they will call sync, and then point to this 30603041982dSJonathan Peyton // other task_team; make sure it is allocated and properly initialized. As 30613041982dSJonathan Peyton // threads spin in the barrier release phase, they will continue to use the 30623041982dSJonathan Peyton // previous task_team struct(above), until they receive the signal to stop 30633041982dSJonathan Peyton // checking for tasks (they can't safely reference the kmp_team_t struct, 30643041982dSJonathan Peyton // which could be reallocated by the master thread). No task teams are formed 30653041982dSJonathan Peyton // for serialized teams. 30667cc577a4SJonathan Peyton if (team->t.t_nproc > 1) { 30677cc577a4SJonathan Peyton int other_team = 1 - this_thr->th.th_task_state; 30687cc577a4SJonathan Peyton if (team->t.t_task_team[other_team] == NULL) { // setup other team as well 30693041982dSJonathan Peyton team->t.t_task_team[other_team] = 30703041982dSJonathan Peyton __kmp_allocate_task_team(this_thr, team); 30713041982dSJonathan Peyton KA_TRACE(20, ("__kmp_task_team_setup: Master T#%d created second new " 30723041982dSJonathan Peyton "task_team %p for team %d at parity=%d\n", 30733041982dSJonathan Peyton __kmp_gtid_from_thread(this_thr), 30743041982dSJonathan Peyton team->t.t_task_team[other_team], 30757cc577a4SJonathan Peyton ((team != NULL) ? team->t.t_id : -1), other_team)); 30763041982dSJonathan Peyton } else { // Leave the old task team struct in place for the upcoming region; 30773041982dSJonathan Peyton // adjust as needed 30787cc577a4SJonathan Peyton kmp_task_team_t *task_team = team->t.t_task_team[other_team]; 30793041982dSJonathan Peyton if (!task_team->tt.tt_active || 30803041982dSJonathan Peyton team->t.t_nproc != task_team->tt.tt_nproc) { 30817cc577a4SJonathan Peyton TCW_4(task_team->tt.tt_nproc, team->t.t_nproc); 30827cc577a4SJonathan Peyton TCW_4(task_team->tt.tt_found_tasks, FALSE); 30837cc577a4SJonathan Peyton #if OMP_45_ENABLED 30847cc577a4SJonathan Peyton TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE); 30857cc577a4SJonathan Peyton #endif 30867cc577a4SJonathan Peyton TCW_4(task_team->tt.tt_unfinished_threads, team->t.t_nproc); 30877cc577a4SJonathan Peyton TCW_4(task_team->tt.tt_active, TRUE); 30887cc577a4SJonathan Peyton } 30893041982dSJonathan Peyton // if team size has changed, the first thread to enable tasking will 30903041982dSJonathan Peyton // realloc threads_data if necessary 30913041982dSJonathan Peyton KA_TRACE(20, ("__kmp_task_team_setup: Master T#%d reset next task_team " 30923041982dSJonathan Peyton "%p for team %d at parity=%d\n", 30933041982dSJonathan Peyton __kmp_gtid_from_thread(this_thr), 30943041982dSJonathan Peyton team->t.t_task_team[other_team], 30957cc577a4SJonathan Peyton ((team != NULL) ? team->t.t_id : -1), other_team)); 30967cc577a4SJonathan Peyton } 30977cc577a4SJonathan Peyton } 30987cc577a4SJonathan Peyton } 30997cc577a4SJonathan Peyton 31007cc577a4SJonathan Peyton // __kmp_task_team_sync: Propagation of task team data from team to threads 31017cc577a4SJonathan Peyton // which happens just after the release phase of a team barrier. This may be 31027cc577a4SJonathan Peyton // called by any thread, but only for teams with # threads > 1. 31033041982dSJonathan Peyton void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team) { 31047cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec); 31057cc577a4SJonathan Peyton 31063041982dSJonathan Peyton // Toggle the th_task_state field, to switch which task_team this thread 31073041982dSJonathan Peyton // refers to 31087cc577a4SJonathan Peyton this_thr->th.th_task_state = 1 - this_thr->th.th_task_state; 31093041982dSJonathan Peyton // It is now safe to propagate the task team pointer from the team struct to 31103041982dSJonathan Peyton // the current thread. 31113041982dSJonathan Peyton TCW_PTR(this_thr->th.th_task_team, 31123041982dSJonathan Peyton team->t.t_task_team[this_thr->th.th_task_state]); 31133041982dSJonathan Peyton KA_TRACE(20, 31143041982dSJonathan Peyton ("__kmp_task_team_sync: Thread T#%d task team switched to task_team " 31153041982dSJonathan Peyton "%p from Team #%d (parity=%d)\n", 31167cc577a4SJonathan Peyton __kmp_gtid_from_thread(this_thr), this_thr->th.th_task_team, 31177cc577a4SJonathan Peyton ((team != NULL) ? team->t.t_id : -1), this_thr->th.th_task_state)); 31187cc577a4SJonathan Peyton } 31197cc577a4SJonathan Peyton 31203041982dSJonathan Peyton // __kmp_task_team_wait: Master thread waits for outstanding tasks after the 31213041982dSJonathan Peyton // barrier gather phase. Only called by master thread if #threads in team > 1 or 31223041982dSJonathan Peyton // if proxy tasks were created. 31233041982dSJonathan Peyton // 31243041982dSJonathan Peyton // wait is a flag that defaults to 1 (see kmp.h), but waiting can be turned off 31253041982dSJonathan Peyton // by passing in 0 optionally as the last argument. When wait is zero, master 31263041982dSJonathan Peyton // thread does not wait for unfinished_threads to reach 0. 31273041982dSJonathan Peyton void __kmp_task_team_wait( 31283041982dSJonathan Peyton kmp_info_t *this_thr, 31293041982dSJonathan Peyton kmp_team_t *team USE_ITT_BUILD_ARG(void *itt_sync_obj), int wait) { 31307cc577a4SJonathan Peyton kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state]; 31317cc577a4SJonathan Peyton 31327cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec); 31337cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(task_team == this_thr->th.th_task_team); 31347cc577a4SJonathan Peyton 31357cc577a4SJonathan Peyton if ((task_team != NULL) && KMP_TASKING_ENABLED(task_team)) { 31367cc577a4SJonathan Peyton if (wait) { 31373041982dSJonathan Peyton KA_TRACE(20, ("__kmp_task_team_wait: Master T#%d waiting for all tasks " 31383041982dSJonathan Peyton "(for unfinished_threads to reach 0) on task_team = %p\n", 31397cc577a4SJonathan Peyton __kmp_gtid_from_thread(this_thr), task_team)); 31403041982dSJonathan Peyton // Worker threads may have dropped through to release phase, but could 31413041982dSJonathan Peyton // still be executing tasks. Wait here for tasks to complete. To avoid 31423041982dSJonathan Peyton // memory contention, only master thread checks termination condition. 3143c47afcd9SAndrey Churbanov kmp_flag_32 flag( 3144c47afcd9SAndrey Churbanov RCAST(volatile kmp_uint32 *, &task_team->tt.tt_unfinished_threads), 3145c47afcd9SAndrey Churbanov 0U); 31463041982dSJonathan Peyton flag.wait(this_thr, TRUE USE_ITT_BUILD_ARG(itt_sync_obj)); 31477cc577a4SJonathan Peyton } 31483041982dSJonathan Peyton // Deactivate the old task team, so that the worker threads will stop 31493041982dSJonathan Peyton // referencing it while spinning. 31503041982dSJonathan Peyton KA_TRACE( 31513041982dSJonathan Peyton 20, 31523041982dSJonathan Peyton ("__kmp_task_team_wait: Master T#%d deactivating task_team %p: " 31537cc577a4SJonathan Peyton "setting active to false, setting local and team's pointer to NULL\n", 31547cc577a4SJonathan Peyton __kmp_gtid_from_thread(this_thr), task_team)); 31557cc577a4SJonathan Peyton #if OMP_45_ENABLED 31563041982dSJonathan Peyton KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1 || 31573041982dSJonathan Peyton task_team->tt.tt_found_proxy_tasks == TRUE); 31587cc577a4SJonathan Peyton TCW_SYNC_4(task_team->tt.tt_found_proxy_tasks, FALSE); 31597cc577a4SJonathan Peyton #else 31607cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1); 31617cc577a4SJonathan Peyton #endif 31627cc577a4SJonathan Peyton TCW_SYNC_4(task_team->tt.tt_active, FALSE); 31637cc577a4SJonathan Peyton KMP_MB(); 31647cc577a4SJonathan Peyton 31657cc577a4SJonathan Peyton TCW_PTR(this_thr->th.th_task_team, NULL); 31667cc577a4SJonathan Peyton } 31677cc577a4SJonathan Peyton } 31687cc577a4SJonathan Peyton 31697cc577a4SJonathan Peyton // __kmp_tasking_barrier: 31707cc577a4SJonathan Peyton // This routine may only called when __kmp_tasking_mode == tskm_extra_barrier. 31713041982dSJonathan Peyton // Internal function to execute all tasks prior to a regular barrier or a join 31723041982dSJonathan Peyton // barrier. It is a full barrier itself, which unfortunately turns regular 31733041982dSJonathan Peyton // barriers into double barriers and join barriers into 1 1/2 barriers. 31743041982dSJonathan Peyton void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread, int gtid) { 3175c47afcd9SAndrey Churbanov volatile kmp_uint32 *spin = RCAST( 3176c47afcd9SAndrey Churbanov volatile kmp_uint32 *, 3177c47afcd9SAndrey Churbanov &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads); 31787cc577a4SJonathan Peyton int flag = FALSE; 31797cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(__kmp_tasking_mode == tskm_extra_barrier); 31807cc577a4SJonathan Peyton 31817cc577a4SJonathan Peyton #if USE_ITT_BUILD 31827cc577a4SJonathan Peyton KMP_FSYNC_SPIN_INIT(spin, (kmp_uint32 *)NULL); 31837cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */ 31847cc577a4SJonathan Peyton kmp_flag_32 spin_flag(spin, 0U); 31853041982dSJonathan Peyton while (!spin_flag.execute_tasks(thread, gtid, TRUE, 31863041982dSJonathan Peyton &flag USE_ITT_BUILD_ARG(NULL), 0)) { 31877cc577a4SJonathan Peyton #if USE_ITT_BUILD 31887cc577a4SJonathan Peyton // TODO: What about itt_sync_obj?? 31895ba90c79SAndrey Churbanov KMP_FSYNC_SPIN_PREPARE(CCAST(kmp_uint32 *, spin)); 31907cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */ 31917cc577a4SJonathan Peyton 31927cc577a4SJonathan Peyton if (TCR_4(__kmp_global.g.g_done)) { 31937cc577a4SJonathan Peyton if (__kmp_global.g.g_abort) 31947cc577a4SJonathan Peyton __kmp_abort_thread(); 31957cc577a4SJonathan Peyton break; 31967cc577a4SJonathan Peyton } 31977cc577a4SJonathan Peyton KMP_YIELD(TRUE); // GH: We always yield here 31987cc577a4SJonathan Peyton } 31997cc577a4SJonathan Peyton #if USE_ITT_BUILD 32005ba90c79SAndrey Churbanov KMP_FSYNC_SPIN_ACQUIRED(CCAST(kmp_uint32 *, spin)); 32017cc577a4SJonathan Peyton #endif /* USE_ITT_BUILD */ 32027cc577a4SJonathan Peyton } 32037cc577a4SJonathan Peyton 32047cc577a4SJonathan Peyton #if OMP_45_ENABLED 32057cc577a4SJonathan Peyton 32063041982dSJonathan Peyton // __kmp_give_task puts a task into a given thread queue if: 32073041982dSJonathan Peyton // - the queue for that thread was created 32083041982dSJonathan Peyton // - there's space in that queue 32093041982dSJonathan Peyton // Because of this, __kmp_push_task needs to check if there's space after 32103041982dSJonathan Peyton // getting the lock 32113041982dSJonathan Peyton static bool __kmp_give_task(kmp_info_t *thread, kmp_int32 tid, kmp_task_t *task, 32123041982dSJonathan Peyton kmp_int32 pass) { 32137cc577a4SJonathan Peyton kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task); 32147cc577a4SJonathan Peyton kmp_task_team_t *task_team = taskdata->td_task_team; 32157cc577a4SJonathan Peyton 32163041982dSJonathan Peyton KA_TRACE(20, ("__kmp_give_task: trying to give task %p to thread %d.\n", 32173041982dSJonathan Peyton taskdata, tid)); 32187cc577a4SJonathan Peyton 32197cc577a4SJonathan Peyton // If task_team is NULL something went really bad... 32207cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(task_team != NULL); 32217cc577a4SJonathan Peyton 32227cc577a4SJonathan Peyton bool result = false; 32237cc577a4SJonathan Peyton kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid]; 32247cc577a4SJonathan Peyton 32257cc577a4SJonathan Peyton if (thread_data->td.td_deque == NULL) { 32267cc577a4SJonathan Peyton // There's no queue in this thread, go find another one 32277cc577a4SJonathan Peyton // We're guaranteed that at least one thread has a queue 32283041982dSJonathan Peyton KA_TRACE(30, 32293041982dSJonathan Peyton ("__kmp_give_task: thread %d has no queue while giving task %p.\n", 32303041982dSJonathan Peyton tid, taskdata)); 32317cc577a4SJonathan Peyton return result; 32327cc577a4SJonathan Peyton } 32337cc577a4SJonathan Peyton 32343041982dSJonathan Peyton if (TCR_4(thread_data->td.td_deque_ntasks) >= 32353041982dSJonathan Peyton TASK_DEQUE_SIZE(thread_data->td)) { 32363041982dSJonathan Peyton KA_TRACE( 32373041982dSJonathan Peyton 30, 32383041982dSJonathan Peyton ("__kmp_give_task: queue is full while giving task %p to thread %d.\n", 32393041982dSJonathan Peyton taskdata, tid)); 32407cc577a4SJonathan Peyton 32413041982dSJonathan Peyton // if this deque is bigger than the pass ratio give a chance to another 32423041982dSJonathan Peyton // thread 32433041982dSJonathan Peyton if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass) 32443041982dSJonathan Peyton return result; 32457cc577a4SJonathan Peyton 32467cc577a4SJonathan Peyton __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock); 32477cc577a4SJonathan Peyton __kmp_realloc_task_deque(thread, thread_data); 32487cc577a4SJonathan Peyton 32497cc577a4SJonathan Peyton } else { 32507cc577a4SJonathan Peyton 32517cc577a4SJonathan Peyton __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock); 32527cc577a4SJonathan Peyton 32533041982dSJonathan Peyton if (TCR_4(thread_data->td.td_deque_ntasks) >= 32543041982dSJonathan Peyton TASK_DEQUE_SIZE(thread_data->td)) { 32553041982dSJonathan Peyton KA_TRACE(30, ("__kmp_give_task: queue is full while giving task %p to " 32563041982dSJonathan Peyton "thread %d.\n", 32573041982dSJonathan Peyton taskdata, tid)); 32587cc577a4SJonathan Peyton 32593041982dSJonathan Peyton // if this deque is bigger than the pass ratio give a chance to another 32603041982dSJonathan Peyton // thread 32617cc577a4SJonathan Peyton if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass) 32627cc577a4SJonathan Peyton goto release_and_exit; 32637cc577a4SJonathan Peyton 32647cc577a4SJonathan Peyton __kmp_realloc_task_deque(thread, thread_data); 32657cc577a4SJonathan Peyton } 32667cc577a4SJonathan Peyton } 32677cc577a4SJonathan Peyton 32687cc577a4SJonathan Peyton // lock is held here, and there is space in the deque 32697cc577a4SJonathan Peyton 32707cc577a4SJonathan Peyton thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata; 32717cc577a4SJonathan Peyton // Wrap index. 32723041982dSJonathan Peyton thread_data->td.td_deque_tail = 32733041982dSJonathan Peyton (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td); 32743041982dSJonathan Peyton TCW_4(thread_data->td.td_deque_ntasks, 32753041982dSJonathan Peyton TCR_4(thread_data->td.td_deque_ntasks) + 1); 32767cc577a4SJonathan Peyton 32777cc577a4SJonathan Peyton result = true; 32783041982dSJonathan Peyton KA_TRACE(30, ("__kmp_give_task: successfully gave task %p to thread %d.\n", 32793041982dSJonathan Peyton taskdata, tid)); 32807cc577a4SJonathan Peyton 32817cc577a4SJonathan Peyton release_and_exit: 32827cc577a4SJonathan Peyton __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock); 32837cc577a4SJonathan Peyton 32847cc577a4SJonathan Peyton return result; 32857cc577a4SJonathan Peyton } 32867cc577a4SJonathan Peyton 32873041982dSJonathan Peyton /* The finish of the proxy tasks is divided in two pieces: 32887cc577a4SJonathan Peyton - the top half is the one that can be done from a thread outside the team 32897cc577a4SJonathan Peyton - the bottom half must be run from a them within the team 32907cc577a4SJonathan Peyton 32913041982dSJonathan Peyton In order to run the bottom half the task gets queued back into one of the 32923041982dSJonathan Peyton threads of the team. Once the td_incomplete_child_task counter of the parent 32933041982dSJonathan Peyton is decremented the threads can leave the barriers. So, the bottom half needs 32943041982dSJonathan Peyton to be queued before the counter is decremented. The top half is therefore 32953041982dSJonathan Peyton divided in two parts: 32967cc577a4SJonathan Peyton - things that can be run before queuing the bottom half 32977cc577a4SJonathan Peyton - things that must be run after queuing the bottom half 32987cc577a4SJonathan Peyton 32993041982dSJonathan Peyton This creates a second race as the bottom half can free the task before the 33003041982dSJonathan Peyton second top half is executed. To avoid this we use the 33013041982dSJonathan Peyton td_incomplete_child_task of the proxy task to synchronize the top and bottom 33023041982dSJonathan Peyton half. */ 33033041982dSJonathan Peyton static void __kmp_first_top_half_finish_proxy(kmp_taskdata_t *taskdata) { 33047cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT); 33057cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY); 33067cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0); 33077cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0); 33087cc577a4SJonathan Peyton 33097cc577a4SJonathan Peyton taskdata->td_flags.complete = 1; // mark the task as completed 33107cc577a4SJonathan Peyton 33117cc577a4SJonathan Peyton if (taskdata->td_taskgroup) 3312c47afcd9SAndrey Churbanov KMP_TEST_THEN_DEC32(&taskdata->td_taskgroup->count); 33137cc577a4SJonathan Peyton 33143041982dSJonathan Peyton // Create an imaginary children for this task so the bottom half cannot 33153041982dSJonathan Peyton // release the task before we have completed the second top half 33167cc577a4SJonathan Peyton TCI_4(taskdata->td_incomplete_child_tasks); 33177cc577a4SJonathan Peyton } 33187cc577a4SJonathan Peyton 33193041982dSJonathan Peyton static void __kmp_second_top_half_finish_proxy(kmp_taskdata_t *taskdata) { 33207cc577a4SJonathan Peyton kmp_int32 children = 0; 33217cc577a4SJonathan Peyton 33227cc577a4SJonathan Peyton // Predecrement simulated by "- 1" calculation 33233041982dSJonathan Peyton children = 33245ba90c79SAndrey Churbanov KMP_TEST_THEN_DEC32(&taskdata->td_parent->td_incomplete_child_tasks) - 1; 33257cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(children >= 0); 33267cc577a4SJonathan Peyton 33277cc577a4SJonathan Peyton // Remove the imaginary children 33287cc577a4SJonathan Peyton TCD_4(taskdata->td_incomplete_child_tasks); 33297cc577a4SJonathan Peyton } 33307cc577a4SJonathan Peyton 33313041982dSJonathan Peyton static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask) { 33327cc577a4SJonathan Peyton kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask); 33337cc577a4SJonathan Peyton kmp_info_t *thread = __kmp_threads[gtid]; 33347cc577a4SJonathan Peyton 33357cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY); 33363041982dSJonathan Peyton KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 33373041982dSJonathan Peyton 1); // top half must run before bottom half 33387cc577a4SJonathan Peyton 33397cc577a4SJonathan Peyton // We need to wait to make sure the top half is finished 33407cc577a4SJonathan Peyton // Spinning here should be ok as this should happen quickly 33413041982dSJonathan Peyton while (TCR_4(taskdata->td_incomplete_child_tasks) > 0) 33423041982dSJonathan Peyton ; 33437cc577a4SJonathan Peyton 33447cc577a4SJonathan Peyton __kmp_release_deps(gtid, taskdata); 33457cc577a4SJonathan Peyton __kmp_free_task_and_ancestors(gtid, taskdata, thread); 33467cc577a4SJonathan Peyton } 33477cc577a4SJonathan Peyton 33487cc577a4SJonathan Peyton /*! 33497cc577a4SJonathan Peyton @ingroup TASKING 33507cc577a4SJonathan Peyton @param gtid Global Thread ID of encountering thread 33517cc577a4SJonathan Peyton @param ptask Task which execution is completed 33527cc577a4SJonathan Peyton 33533041982dSJonathan Peyton Execute the completation of a proxy task from a thread of that is part of the 33543041982dSJonathan Peyton team. Run first and bottom halves directly. 33557cc577a4SJonathan Peyton */ 33563041982dSJonathan Peyton void __kmpc_proxy_task_completed(kmp_int32 gtid, kmp_task_t *ptask) { 33577cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(ptask != NULL); 33587cc577a4SJonathan Peyton kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask); 33593041982dSJonathan Peyton KA_TRACE( 33603041982dSJonathan Peyton 10, ("__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n", 33613041982dSJonathan Peyton gtid, taskdata)); 33627cc577a4SJonathan Peyton 33637cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY); 33647cc577a4SJonathan Peyton 33657cc577a4SJonathan Peyton __kmp_first_top_half_finish_proxy(taskdata); 33667cc577a4SJonathan Peyton __kmp_second_top_half_finish_proxy(taskdata); 33677cc577a4SJonathan Peyton __kmp_bottom_half_finish_proxy(gtid, ptask); 33687cc577a4SJonathan Peyton 33693041982dSJonathan Peyton KA_TRACE(10, 33703041982dSJonathan Peyton ("__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n", 33713041982dSJonathan Peyton gtid, taskdata)); 33727cc577a4SJonathan Peyton } 33737cc577a4SJonathan Peyton 33747cc577a4SJonathan Peyton /*! 33757cc577a4SJonathan Peyton @ingroup TASKING 33767cc577a4SJonathan Peyton @param ptask Task which execution is completed 33777cc577a4SJonathan Peyton 33783041982dSJonathan Peyton Execute the completation of a proxy task from a thread that could not belong to 33793041982dSJonathan Peyton the team. 33807cc577a4SJonathan Peyton */ 33813041982dSJonathan Peyton void __kmpc_proxy_task_completed_ooo(kmp_task_t *ptask) { 33827cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(ptask != NULL); 33837cc577a4SJonathan Peyton kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask); 33847cc577a4SJonathan Peyton 33853041982dSJonathan Peyton KA_TRACE( 33863041982dSJonathan Peyton 10, 33873041982dSJonathan Peyton ("__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n", 33883041982dSJonathan Peyton taskdata)); 33897cc577a4SJonathan Peyton 33907cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY); 33917cc577a4SJonathan Peyton 33927cc577a4SJonathan Peyton __kmp_first_top_half_finish_proxy(taskdata); 33937cc577a4SJonathan Peyton 33943041982dSJonathan Peyton // Enqueue task to complete bottom half completion from a thread within the 33953041982dSJonathan Peyton // corresponding team 33967cc577a4SJonathan Peyton kmp_team_t *team = taskdata->td_team; 33977cc577a4SJonathan Peyton kmp_int32 nthreads = team->t.t_nproc; 33987cc577a4SJonathan Peyton kmp_info_t *thread; 33997cc577a4SJonathan Peyton 34003041982dSJonathan Peyton // This should be similar to start_k = __kmp_get_random( thread ) % nthreads 34013041982dSJonathan Peyton // but we cannot use __kmp_get_random here 34027cc577a4SJonathan Peyton kmp_int32 start_k = 0; 34037cc577a4SJonathan Peyton kmp_int32 pass = 1; 34047cc577a4SJonathan Peyton kmp_int32 k = start_k; 34057cc577a4SJonathan Peyton 34067cc577a4SJonathan Peyton do { 34077cc577a4SJonathan Peyton // For now we're just linearly trying to find a thread 34087cc577a4SJonathan Peyton thread = team->t.t_threads[k]; 34097cc577a4SJonathan Peyton k = (k + 1) % nthreads; 34107cc577a4SJonathan Peyton 34117cc577a4SJonathan Peyton // we did a full pass through all the threads 34123041982dSJonathan Peyton if (k == start_k) 34133041982dSJonathan Peyton pass = pass << 1; 34147cc577a4SJonathan Peyton 34157cc577a4SJonathan Peyton } while (!__kmp_give_task(thread, k, ptask, pass)); 34167cc577a4SJonathan Peyton 34177cc577a4SJonathan Peyton __kmp_second_top_half_finish_proxy(taskdata); 34187cc577a4SJonathan Peyton 34193041982dSJonathan Peyton KA_TRACE( 34203041982dSJonathan Peyton 10, 34213041982dSJonathan Peyton ("__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n", 34223041982dSJonathan Peyton taskdata)); 34237cc577a4SJonathan Peyton } 34247cc577a4SJonathan Peyton 34253041982dSJonathan Peyton // __kmp_task_dup_alloc: Allocate the taskdata and make a copy of source task 34263041982dSJonathan Peyton // for taskloop 34277cc577a4SJonathan Peyton // 34287cc577a4SJonathan Peyton // thread: allocating thread 34297cc577a4SJonathan Peyton // task_src: pointer to source task to be duplicated 34307cc577a4SJonathan Peyton // returns: a pointer to the allocated kmp_task_t structure (task). 34313041982dSJonathan Peyton kmp_task_t *__kmp_task_dup_alloc(kmp_info_t *thread, kmp_task_t *task_src) { 34327cc577a4SJonathan Peyton kmp_task_t *task; 34337cc577a4SJonathan Peyton kmp_taskdata_t *taskdata; 34347cc577a4SJonathan Peyton kmp_taskdata_t *taskdata_src; 34357cc577a4SJonathan Peyton kmp_taskdata_t *parent_task = thread->th.th_current_task; 34367cc577a4SJonathan Peyton size_t shareds_offset; 34377cc577a4SJonathan Peyton size_t task_size; 34387cc577a4SJonathan Peyton 34393041982dSJonathan Peyton KA_TRACE(10, ("__kmp_task_dup_alloc(enter): Th %p, source task %p\n", thread, 34403041982dSJonathan Peyton task_src)); 34417cc577a4SJonathan Peyton taskdata_src = KMP_TASK_TO_TASKDATA(task_src); 34423041982dSJonathan Peyton KMP_DEBUG_ASSERT(taskdata_src->td_flags.proxy == 34433041982dSJonathan Peyton TASK_FULL); // it should not be proxy task 34447cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(taskdata_src->td_flags.tasktype == TASK_EXPLICIT); 34457cc577a4SJonathan Peyton task_size = taskdata_src->td_size_alloc; 34467cc577a4SJonathan Peyton 34477cc577a4SJonathan Peyton // Allocate a kmp_taskdata_t block and a kmp_task_t block. 34483041982dSJonathan Peyton KA_TRACE(30, ("__kmp_task_dup_alloc: Th %p, malloc size %ld\n", thread, 34493041982dSJonathan Peyton task_size)); 34507cc577a4SJonathan Peyton #if USE_FAST_MEMORY 34517cc577a4SJonathan Peyton taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, task_size); 34527cc577a4SJonathan Peyton #else 34537cc577a4SJonathan Peyton taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, task_size); 34547cc577a4SJonathan Peyton #endif /* USE_FAST_MEMORY */ 34557cc577a4SJonathan Peyton KMP_MEMCPY(taskdata, taskdata_src, task_size); 34567cc577a4SJonathan Peyton 34577cc577a4SJonathan Peyton task = KMP_TASKDATA_TO_TASK(taskdata); 34587cc577a4SJonathan Peyton 34597cc577a4SJonathan Peyton // Initialize new task (only specific fields not affected by memcpy) 34607cc577a4SJonathan Peyton taskdata->td_task_id = KMP_GEN_TASK_ID(); 34617cc577a4SJonathan Peyton if (task->shareds != NULL) { // need setup shareds pointer 34627cc577a4SJonathan Peyton shareds_offset = (char *)task_src->shareds - (char *)taskdata_src; 34637cc577a4SJonathan Peyton task->shareds = &((char *)taskdata)[shareds_offset]; 34643041982dSJonathan Peyton KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (sizeof(void *) - 1)) == 34653041982dSJonathan Peyton 0); 34667cc577a4SJonathan Peyton } 34677cc577a4SJonathan Peyton taskdata->td_alloc_thread = thread; 346893e17cfeSJonathan Peyton taskdata->td_parent = parent_task; 34693041982dSJonathan Peyton taskdata->td_taskgroup = 34703041982dSJonathan Peyton parent_task 34713041982dSJonathan Peyton ->td_taskgroup; // task inherits the taskgroup from the parent task 34727cc577a4SJonathan Peyton 34733041982dSJonathan Peyton // Only need to keep track of child task counts if team parallel and tasking 34743041982dSJonathan Peyton // not serialized 34757cc577a4SJonathan Peyton if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) { 34765ba90c79SAndrey Churbanov KMP_TEST_THEN_INC32(&parent_task->td_incomplete_child_tasks); 34777cc577a4SJonathan Peyton if (parent_task->td_taskgroup) 3478c47afcd9SAndrey Churbanov KMP_TEST_THEN_INC32(&parent_task->td_taskgroup->count); 34793041982dSJonathan Peyton // Only need to keep track of allocated child tasks for explicit tasks since 34803041982dSJonathan Peyton // implicit not deallocated 34817cc577a4SJonathan Peyton if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) 34825ba90c79SAndrey Churbanov KMP_TEST_THEN_INC32(&taskdata->td_parent->td_allocated_child_tasks); 34837cc577a4SJonathan Peyton } 34847cc577a4SJonathan Peyton 34853041982dSJonathan Peyton KA_TRACE(20, 34863041982dSJonathan Peyton ("__kmp_task_dup_alloc(exit): Th %p, created task %p, parent=%p\n", 34877cc577a4SJonathan Peyton thread, taskdata, taskdata->td_parent)); 34887cc577a4SJonathan Peyton #if OMPT_SUPPORT 3489*82e94a59SJoachim Protze if (UNLIKELY(ompt_enabled.enabled)) 3490*82e94a59SJoachim Protze __ompt_task_init(taskdata, thread->th.th_info.ds.ds_gtid); 34917cc577a4SJonathan Peyton #endif 34927cc577a4SJonathan Peyton return task; 34937cc577a4SJonathan Peyton } 34947cc577a4SJonathan Peyton 34957cc577a4SJonathan Peyton // Routine optionally generated by the compiler for setting the lastprivate flag 34967cc577a4SJonathan Peyton // and calling needed constructors for private/firstprivate objects 34977cc577a4SJonathan Peyton // (used to form taskloop tasks from pattern task) 349893e17cfeSJonathan Peyton // Parameters: dest task, src task, lastprivate flag. 34997cc577a4SJonathan Peyton typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32); 35007cc577a4SJonathan Peyton 35017cc577a4SJonathan Peyton // __kmp_taskloop_linear: Start tasks of the taskloop linearly 35027cc577a4SJonathan Peyton // 35037cc577a4SJonathan Peyton // loc Source location information 35047cc577a4SJonathan Peyton // gtid Global thread ID 350593e17cfeSJonathan Peyton // task Pattern task, exposes the loop iteration range 350693e17cfeSJonathan Peyton // lb Pointer to loop lower bound in task structure 350793e17cfeSJonathan Peyton // ub Pointer to loop upper bound in task structure 35087cc577a4SJonathan Peyton // st Loop stride 350993e17cfeSJonathan Peyton // ub_glob Global upper bound (used for lastprivate check) 351093e17cfeSJonathan Peyton // num_tasks Number of tasks to execute 351193e17cfeSJonathan Peyton // grainsize Number of loop iterations per task 351293e17cfeSJonathan Peyton // extras Number of chunks with grainsize+1 iterations 351393e17cfeSJonathan Peyton // tc Iterations count 35147cc577a4SJonathan Peyton // task_dup Tasks duplication routine 35153041982dSJonathan Peyton void __kmp_taskloop_linear(ident_t *loc, int gtid, kmp_task_t *task, 35167cc577a4SJonathan Peyton kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, 351793e17cfeSJonathan Peyton kmp_uint64 ub_glob, kmp_uint64 num_tasks, 351893e17cfeSJonathan Peyton kmp_uint64 grainsize, kmp_uint64 extras, 351993e17cfeSJonathan Peyton kmp_uint64 tc, void *task_dup) { 35207cc577a4SJonathan Peyton KMP_COUNT_BLOCK(OMP_TASKLOOP); 35217cc577a4SJonathan Peyton KMP_TIME_PARTITIONED_BLOCK(OMP_taskloop_scheduling); 35227cc577a4SJonathan Peyton p_task_dup_t ptask_dup = (p_task_dup_t)task_dup; 35237cc577a4SJonathan Peyton kmp_uint64 lower = *lb; // compiler provides global bounds here 35247cc577a4SJonathan Peyton kmp_uint64 upper = *ub; 352593e17cfeSJonathan Peyton kmp_uint64 i; 35267cc577a4SJonathan Peyton kmp_info_t *thread = __kmp_threads[gtid]; 35277cc577a4SJonathan Peyton kmp_taskdata_t *current_task = thread->th.th_current_task; 35287cc577a4SJonathan Peyton kmp_task_t *next_task; 35297cc577a4SJonathan Peyton kmp_int32 lastpriv = 0; 35303041982dSJonathan Peyton size_t lower_offset = 35313041982dSJonathan Peyton (char *)lb - (char *)task; // remember offset of lb in the task structure 35323041982dSJonathan Peyton size_t upper_offset = 35333041982dSJonathan Peyton (char *)ub - (char *)task; // remember offset of ub in the task structure 35347cc577a4SJonathan Peyton 353593e17cfeSJonathan Peyton KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras); 353693e17cfeSJonathan Peyton KMP_DEBUG_ASSERT(num_tasks > extras); 353793e17cfeSJonathan Peyton KMP_DEBUG_ASSERT(num_tasks > 0); 353893e17cfeSJonathan Peyton KA_TRACE(20, ("__kmp_taskloop_linear: T#%d: %lld tasks, grainsize %lld, " 3539d74d8902SJonathan Peyton "extras %lld, i=%lld,%lld(%d)%lld, dup %p\n", 3540d74d8902SJonathan Peyton gtid, num_tasks, grainsize, extras, lower, upper, ub_glob, st, 3541d74d8902SJonathan Peyton task_dup)); 354293e17cfeSJonathan Peyton 354393e17cfeSJonathan Peyton // Launch num_tasks tasks, assign grainsize iterations each task 354493e17cfeSJonathan Peyton for (i = 0; i < num_tasks; ++i) { 354593e17cfeSJonathan Peyton kmp_uint64 chunk_minus_1; 354693e17cfeSJonathan Peyton if (extras == 0) { 354793e17cfeSJonathan Peyton chunk_minus_1 = grainsize - 1; 354893e17cfeSJonathan Peyton } else { 354993e17cfeSJonathan Peyton chunk_minus_1 = grainsize; 355093e17cfeSJonathan Peyton --extras; // first extras iterations get bigger chunk (grainsize+1) 355193e17cfeSJonathan Peyton } 355293e17cfeSJonathan Peyton upper = lower + st * chunk_minus_1; 355393e17cfeSJonathan Peyton if (i == num_tasks - 1) { 355493e17cfeSJonathan Peyton // schedule the last task, set lastprivate flag if needed 355593e17cfeSJonathan Peyton if (st == 1) { // most common case 355693e17cfeSJonathan Peyton KMP_DEBUG_ASSERT(upper == *ub); 355793e17cfeSJonathan Peyton if (upper == ub_glob) 355893e17cfeSJonathan Peyton lastpriv = 1; 355993e17cfeSJonathan Peyton } else if (st > 0) { // positive loop stride 356093e17cfeSJonathan Peyton KMP_DEBUG_ASSERT((kmp_uint64)st > *ub - upper); 356193e17cfeSJonathan Peyton if ((kmp_uint64)st > ub_glob - upper) 356293e17cfeSJonathan Peyton lastpriv = 1; 356393e17cfeSJonathan Peyton } else { // negative loop stride 356493e17cfeSJonathan Peyton KMP_DEBUG_ASSERT(upper + st < *ub); 356593e17cfeSJonathan Peyton if (upper - ub_glob < (kmp_uint64)(-st)) 356693e17cfeSJonathan Peyton lastpriv = 1; 356793e17cfeSJonathan Peyton } 356893e17cfeSJonathan Peyton } 356993e17cfeSJonathan Peyton next_task = __kmp_task_dup_alloc(thread, task); // allocate new task 357093e17cfeSJonathan Peyton // adjust task-specific bounds 357193e17cfeSJonathan Peyton *(kmp_uint64 *)((char *)next_task + lower_offset) = lower; 357293e17cfeSJonathan Peyton *(kmp_uint64 *)((char *)next_task + upper_offset) = upper; 357393e17cfeSJonathan Peyton if (ptask_dup != NULL) // set lastprivate flag, construct fistprivates, etc. 357493e17cfeSJonathan Peyton ptask_dup(next_task, task, lastpriv); 357593e17cfeSJonathan Peyton KA_TRACE(40, ("__kmp_taskloop_linear: T#%d; task %p: lower %lld, " 357693e17cfeSJonathan Peyton "upper %lld (offsets %p %p)\n", 357793e17cfeSJonathan Peyton gtid, next_task, lower, upper, lower_offset, upper_offset)); 357893e17cfeSJonathan Peyton __kmp_omp_task(gtid, next_task, true); // schedule new task 357993e17cfeSJonathan Peyton lower = upper + st; // adjust lower bound for the next iteration 358093e17cfeSJonathan Peyton } 358193e17cfeSJonathan Peyton // free the pattern task and exit 358293e17cfeSJonathan Peyton __kmp_task_start(gtid, task, current_task); // make internal bookkeeping 358393e17cfeSJonathan Peyton // do not execute the pattern task, just do internal bookkeeping 358493e17cfeSJonathan Peyton __kmp_task_finish(gtid, task, current_task); 358593e17cfeSJonathan Peyton } 358693e17cfeSJonathan Peyton 358793e17cfeSJonathan Peyton // Structure to keep taskloop parameters for auxiliary task 358893e17cfeSJonathan Peyton // kept in the shareds of the task structure. 358993e17cfeSJonathan Peyton typedef struct __taskloop_params { 359093e17cfeSJonathan Peyton kmp_task_t *task; 359193e17cfeSJonathan Peyton kmp_uint64 *lb; 359293e17cfeSJonathan Peyton kmp_uint64 *ub; 359393e17cfeSJonathan Peyton void *task_dup; 359493e17cfeSJonathan Peyton kmp_int64 st; 359593e17cfeSJonathan Peyton kmp_uint64 ub_glob; 359693e17cfeSJonathan Peyton kmp_uint64 num_tasks; 359793e17cfeSJonathan Peyton kmp_uint64 grainsize; 359893e17cfeSJonathan Peyton kmp_uint64 extras; 359993e17cfeSJonathan Peyton kmp_uint64 tc; 360093e17cfeSJonathan Peyton kmp_uint64 num_t_min; 360193e17cfeSJonathan Peyton } __taskloop_params_t; 360293e17cfeSJonathan Peyton 360393e17cfeSJonathan Peyton void __kmp_taskloop_recur(ident_t *, int, kmp_task_t *, kmp_uint64 *, 360493e17cfeSJonathan Peyton kmp_uint64 *, kmp_int64, kmp_uint64, kmp_uint64, 360593e17cfeSJonathan Peyton kmp_uint64, kmp_uint64, kmp_uint64, kmp_uint64, 360693e17cfeSJonathan Peyton void *); 360793e17cfeSJonathan Peyton 360893e17cfeSJonathan Peyton // Execute part of the the taskloop submitted as a task. 360993e17cfeSJonathan Peyton int __kmp_taskloop_task(int gtid, void *ptask) { 3610d74d8902SJonathan Peyton __taskloop_params_t *p = 3611d74d8902SJonathan Peyton (__taskloop_params_t *)((kmp_task_t *)ptask)->shareds; 361293e17cfeSJonathan Peyton kmp_task_t *task = p->task; 361393e17cfeSJonathan Peyton kmp_uint64 *lb = p->lb; 361493e17cfeSJonathan Peyton kmp_uint64 *ub = p->ub; 361593e17cfeSJonathan Peyton void *task_dup = p->task_dup; 361693e17cfeSJonathan Peyton // p_task_dup_t ptask_dup = (p_task_dup_t)task_dup; 361793e17cfeSJonathan Peyton kmp_int64 st = p->st; 361893e17cfeSJonathan Peyton kmp_uint64 ub_glob = p->ub_glob; 361993e17cfeSJonathan Peyton kmp_uint64 num_tasks = p->num_tasks; 362093e17cfeSJonathan Peyton kmp_uint64 grainsize = p->grainsize; 362193e17cfeSJonathan Peyton kmp_uint64 extras = p->extras; 362293e17cfeSJonathan Peyton kmp_uint64 tc = p->tc; 362393e17cfeSJonathan Peyton kmp_uint64 num_t_min = p->num_t_min; 362493e17cfeSJonathan Peyton #if KMP_DEBUG 362593e17cfeSJonathan Peyton kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task); 362693e17cfeSJonathan Peyton KMP_DEBUG_ASSERT(task != NULL); 362793e17cfeSJonathan Peyton KA_TRACE(20, ("__kmp_taskloop_task: T#%d, task %p: %lld tasks, grainsize" 3628d74d8902SJonathan Peyton " %lld, extras %lld, i=%lld,%lld(%d), dup %p\n", 3629d74d8902SJonathan Peyton gtid, taskdata, num_tasks, grainsize, extras, *lb, *ub, st, 3630d74d8902SJonathan Peyton task_dup)); 363193e17cfeSJonathan Peyton #endif 363293e17cfeSJonathan Peyton KMP_DEBUG_ASSERT(num_tasks * 2 + 1 > num_t_min); 363393e17cfeSJonathan Peyton if (num_tasks > num_t_min) 363493e17cfeSJonathan Peyton __kmp_taskloop_recur(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks, 363593e17cfeSJonathan Peyton grainsize, extras, tc, num_t_min, task_dup); 363693e17cfeSJonathan Peyton else 363793e17cfeSJonathan Peyton __kmp_taskloop_linear(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks, 363893e17cfeSJonathan Peyton grainsize, extras, tc, task_dup); 363993e17cfeSJonathan Peyton 364093e17cfeSJonathan Peyton KA_TRACE(40, ("__kmp_taskloop_task(exit): T#%d\n", gtid)); 364193e17cfeSJonathan Peyton return 0; 364293e17cfeSJonathan Peyton } 364393e17cfeSJonathan Peyton 364493e17cfeSJonathan Peyton // Schedule part of the the taskloop as a task, 364593e17cfeSJonathan Peyton // execute the rest of the the taskloop. 364693e17cfeSJonathan Peyton // 364793e17cfeSJonathan Peyton // loc Source location information 364893e17cfeSJonathan Peyton // gtid Global thread ID 364993e17cfeSJonathan Peyton // task Pattern task, exposes the loop iteration range 365093e17cfeSJonathan Peyton // lb Pointer to loop lower bound in task structure 365193e17cfeSJonathan Peyton // ub Pointer to loop upper bound in task structure 365293e17cfeSJonathan Peyton // st Loop stride 365393e17cfeSJonathan Peyton // ub_glob Global upper bound (used for lastprivate check) 365493e17cfeSJonathan Peyton // num_tasks Number of tasks to execute 365593e17cfeSJonathan Peyton // grainsize Number of loop iterations per task 365693e17cfeSJonathan Peyton // extras Number of chunks with grainsize+1 iterations 365793e17cfeSJonathan Peyton // tc Iterations count 365893e17cfeSJonathan Peyton // num_t_min Threashold to launch tasks recursively 365993e17cfeSJonathan Peyton // task_dup Tasks duplication routine 366093e17cfeSJonathan Peyton void __kmp_taskloop_recur(ident_t *loc, int gtid, kmp_task_t *task, 366193e17cfeSJonathan Peyton kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, 366293e17cfeSJonathan Peyton kmp_uint64 ub_glob, kmp_uint64 num_tasks, 366393e17cfeSJonathan Peyton kmp_uint64 grainsize, kmp_uint64 extras, 366493e17cfeSJonathan Peyton kmp_uint64 tc, kmp_uint64 num_t_min, void *task_dup) { 366593e17cfeSJonathan Peyton #if KMP_DEBUG 366693e17cfeSJonathan Peyton kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task); 366793e17cfeSJonathan Peyton KMP_DEBUG_ASSERT(task != NULL); 366893e17cfeSJonathan Peyton KMP_DEBUG_ASSERT(num_tasks > num_t_min); 366993e17cfeSJonathan Peyton KA_TRACE(20, ("__kmp_taskloop_recur: T#%d, task %p: %lld tasks, grainsize" 3670d74d8902SJonathan Peyton " %lld, extras %lld, i=%lld,%lld(%d), dup %p\n", 3671d74d8902SJonathan Peyton gtid, taskdata, num_tasks, grainsize, extras, *lb, *ub, st, 3672d74d8902SJonathan Peyton task_dup)); 367393e17cfeSJonathan Peyton #endif 367493e17cfeSJonathan Peyton p_task_dup_t ptask_dup = (p_task_dup_t)task_dup; 367593e17cfeSJonathan Peyton kmp_uint64 lower = *lb; 367693e17cfeSJonathan Peyton kmp_uint64 upper = *ub; 367793e17cfeSJonathan Peyton kmp_info_t *thread = __kmp_threads[gtid]; 367893e17cfeSJonathan Peyton // kmp_taskdata_t *current_task = thread->th.th_current_task; 367993e17cfeSJonathan Peyton kmp_task_t *next_task; 368093e17cfeSJonathan Peyton kmp_int32 lastpriv = 0; 368193e17cfeSJonathan Peyton size_t lower_offset = 368293e17cfeSJonathan Peyton (char *)lb - (char *)task; // remember offset of lb in the task structure 368393e17cfeSJonathan Peyton size_t upper_offset = 368493e17cfeSJonathan Peyton (char *)ub - (char *)task; // remember offset of ub in the task structure 368593e17cfeSJonathan Peyton 368693e17cfeSJonathan Peyton KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras); 368793e17cfeSJonathan Peyton KMP_DEBUG_ASSERT(num_tasks > extras); 368893e17cfeSJonathan Peyton KMP_DEBUG_ASSERT(num_tasks > 0); 368993e17cfeSJonathan Peyton 369093e17cfeSJonathan Peyton // split the loop in two halves 369193e17cfeSJonathan Peyton kmp_uint64 lb1, ub0, tc0, tc1, ext0, ext1; 369293e17cfeSJonathan Peyton kmp_uint64 gr_size0 = grainsize; 369393e17cfeSJonathan Peyton kmp_uint64 n_tsk0 = num_tasks >> 1; // num_tasks/2 to execute 369493e17cfeSJonathan Peyton kmp_uint64 n_tsk1 = num_tasks - n_tsk0; // to schedule as a task 369593e17cfeSJonathan Peyton if (n_tsk0 <= extras) { 369693e17cfeSJonathan Peyton gr_size0++; // integrate extras into grainsize 369793e17cfeSJonathan Peyton ext0 = 0; // no extra iters in 1st half 369893e17cfeSJonathan Peyton ext1 = extras - n_tsk0; // remaining extras 369993e17cfeSJonathan Peyton tc0 = gr_size0 * n_tsk0; 370093e17cfeSJonathan Peyton tc1 = tc - tc0; 370193e17cfeSJonathan Peyton } else { // n_tsk0 > extras 370293e17cfeSJonathan Peyton ext1 = 0; // no extra iters in 2nd half 370393e17cfeSJonathan Peyton ext0 = extras; 370493e17cfeSJonathan Peyton tc1 = grainsize * n_tsk1; 370593e17cfeSJonathan Peyton tc0 = tc - tc1; 370693e17cfeSJonathan Peyton } 370793e17cfeSJonathan Peyton ub0 = lower + st * (tc0 - 1); 370893e17cfeSJonathan Peyton lb1 = ub0 + st; 370993e17cfeSJonathan Peyton 371093e17cfeSJonathan Peyton // create pattern task for 2nd half of the loop 371193e17cfeSJonathan Peyton next_task = __kmp_task_dup_alloc(thread, task); // duplicate the task 371293e17cfeSJonathan Peyton // adjust lower bound (upper bound is not changed) for the 2nd half 371393e17cfeSJonathan Peyton *(kmp_uint64 *)((char *)next_task + lower_offset) = lb1; 371493e17cfeSJonathan Peyton if (ptask_dup != NULL) // construct fistprivates, etc. 371593e17cfeSJonathan Peyton ptask_dup(next_task, task, 0); 371693e17cfeSJonathan Peyton *ub = ub0; // adjust upper bound for the 1st half 371793e17cfeSJonathan Peyton 371893e17cfeSJonathan Peyton // create auxiliary task for 2nd half of the loop 371993e17cfeSJonathan Peyton kmp_task_t *new_task = 372093e17cfeSJonathan Peyton __kmpc_omp_task_alloc(loc, gtid, 1, 3 * sizeof(void *), 372193e17cfeSJonathan Peyton sizeof(__taskloop_params_t), &__kmp_taskloop_task); 372293e17cfeSJonathan Peyton __taskloop_params_t *p = (__taskloop_params_t *)new_task->shareds; 372393e17cfeSJonathan Peyton p->task = next_task; 372493e17cfeSJonathan Peyton p->lb = (kmp_uint64 *)((char *)next_task + lower_offset); 372593e17cfeSJonathan Peyton p->ub = (kmp_uint64 *)((char *)next_task + upper_offset); 372693e17cfeSJonathan Peyton p->task_dup = task_dup; 372793e17cfeSJonathan Peyton p->st = st; 372893e17cfeSJonathan Peyton p->ub_glob = ub_glob; 372993e17cfeSJonathan Peyton p->num_tasks = n_tsk1; 373093e17cfeSJonathan Peyton p->grainsize = grainsize; 373193e17cfeSJonathan Peyton p->extras = ext1; 373293e17cfeSJonathan Peyton p->tc = tc1; 373393e17cfeSJonathan Peyton p->num_t_min = num_t_min; 373493e17cfeSJonathan Peyton __kmp_omp_task(gtid, new_task, true); // schedule new task 373593e17cfeSJonathan Peyton 373693e17cfeSJonathan Peyton // execute the 1st half of current subrange 373793e17cfeSJonathan Peyton if (n_tsk0 > num_t_min) 3738d74d8902SJonathan Peyton __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0, gr_size0, 3739d74d8902SJonathan Peyton ext0, tc0, num_t_min, task_dup); 374093e17cfeSJonathan Peyton else 374193e17cfeSJonathan Peyton __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0, 374293e17cfeSJonathan Peyton gr_size0, ext0, tc0, task_dup); 374393e17cfeSJonathan Peyton 374493e17cfeSJonathan Peyton KA_TRACE(40, ("__kmpc_taskloop_recur(exit): T#%d\n", gtid)); 374593e17cfeSJonathan Peyton } 374693e17cfeSJonathan Peyton 374793e17cfeSJonathan Peyton /*! 374893e17cfeSJonathan Peyton @ingroup TASKING 374993e17cfeSJonathan Peyton @param loc Source location information 375093e17cfeSJonathan Peyton @param gtid Global thread ID 375193e17cfeSJonathan Peyton @param task Task structure 375293e17cfeSJonathan Peyton @param if_val Value of the if clause 375393e17cfeSJonathan Peyton @param lb Pointer to loop lower bound in task structure 375493e17cfeSJonathan Peyton @param ub Pointer to loop upper bound in task structure 375593e17cfeSJonathan Peyton @param st Loop stride 375693e17cfeSJonathan Peyton @param nogroup Flag, 1 if nogroup clause specified, 0 otherwise 375793e17cfeSJonathan Peyton @param sched Schedule specified 0/1/2 for none/grainsize/num_tasks 375893e17cfeSJonathan Peyton @param grainsize Schedule value if specified 375993e17cfeSJonathan Peyton @param task_dup Tasks duplication routine 376093e17cfeSJonathan Peyton 376193e17cfeSJonathan Peyton Execute the taskloop construct. 376293e17cfeSJonathan Peyton */ 376393e17cfeSJonathan Peyton void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int if_val, 376493e17cfeSJonathan Peyton kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, 376593e17cfeSJonathan Peyton int sched, kmp_uint64 grainsize, void *task_dup) { 376693e17cfeSJonathan Peyton kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task); 376793e17cfeSJonathan Peyton KMP_DEBUG_ASSERT(task != NULL); 376893e17cfeSJonathan Peyton 376993e17cfeSJonathan Peyton KA_TRACE(20, ("__kmpc_taskloop: T#%d, task %p, lb %lld, ub %lld, st %lld, " 3770d74d8902SJonathan Peyton "grain %llu(%d), dup %p\n", 3771d74d8902SJonathan Peyton gtid, taskdata, *lb, *ub, st, grainsize, sched, task_dup)); 377293e17cfeSJonathan Peyton 3773*82e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL 3774*82e94a59SJoachim Protze ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL); 3775*82e94a59SJoachim Protze ompt_task_info_t *task_info = __ompt_get_task_info_object(0); 3776*82e94a59SJoachim Protze if (ompt_enabled.ompt_callback_work) { 3777*82e94a59SJoachim Protze ompt_callbacks.ompt_callback(ompt_callback_work)( 3778*82e94a59SJoachim Protze ompt_work_taskloop, ompt_scope_begin, &(team_info->parallel_data), 3779*82e94a59SJoachim Protze &(task_info->task_data), 0, OMPT_GET_RETURN_ADDRESS(0)); 3780*82e94a59SJoachim Protze } 3781*82e94a59SJoachim Protze #endif 3782*82e94a59SJoachim Protze 3783*82e94a59SJoachim Protze if (nogroup == 0) { 3784*82e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL 3785*82e94a59SJoachim Protze OMPT_STORE_RETURN_ADDRESS(gtid); 3786*82e94a59SJoachim Protze #endif 378793e17cfeSJonathan Peyton __kmpc_taskgroup(loc, gtid); 3788*82e94a59SJoachim Protze } 378993e17cfeSJonathan Peyton 379093e17cfeSJonathan Peyton // ========================================================================= 379193e17cfeSJonathan Peyton // calculate loop parameters 379293e17cfeSJonathan Peyton kmp_uint64 tc; 379393e17cfeSJonathan Peyton kmp_uint64 lower = *lb; // compiler provides global bounds here 379493e17cfeSJonathan Peyton kmp_uint64 upper = *ub; 379593e17cfeSJonathan Peyton kmp_uint64 ub_glob = upper; // global upper used to calc lastprivate flag 379693e17cfeSJonathan Peyton kmp_uint64 num_tasks = 0, extras = 0; 379793e17cfeSJonathan Peyton kmp_uint64 num_tasks_min = __kmp_taskloop_min_tasks; 379893e17cfeSJonathan Peyton kmp_info_t *thread = __kmp_threads[gtid]; 379993e17cfeSJonathan Peyton kmp_taskdata_t *current_task = thread->th.th_current_task; 380093e17cfeSJonathan Peyton 38017cc577a4SJonathan Peyton // compute trip count 38027cc577a4SJonathan Peyton if (st == 1) { // most common case 38037cc577a4SJonathan Peyton tc = upper - lower + 1; 38047cc577a4SJonathan Peyton } else if (st < 0) { 38057cc577a4SJonathan Peyton tc = (lower - upper) / (-st) + 1; 38067cc577a4SJonathan Peyton } else { // st > 0 38077cc577a4SJonathan Peyton tc = (upper - lower) / st + 1; 38087cc577a4SJonathan Peyton } 38097cc577a4SJonathan Peyton if (tc == 0) { 38107cc577a4SJonathan Peyton KA_TRACE(20, ("__kmpc_taskloop(exit): T#%d zero-trip loop\n", gtid)); 38117cc577a4SJonathan Peyton // free the pattern task and exit 38127cc577a4SJonathan Peyton __kmp_task_start(gtid, task, current_task); 38137cc577a4SJonathan Peyton // do not execute anything for zero-trip loop 38147cc577a4SJonathan Peyton __kmp_task_finish(gtid, task, current_task); 38157cc577a4SJonathan Peyton return; 38167cc577a4SJonathan Peyton } 381793e17cfeSJonathan Peyton if (num_tasks_min == 0) 381893e17cfeSJonathan Peyton // TODO: can we choose better default heuristic? 3819d74d8902SJonathan Peyton num_tasks_min = 3820d74d8902SJonathan Peyton KMP_MIN(thread->th.th_team_nproc * 10, INITIAL_TASK_DEQUE_SIZE); 38217cc577a4SJonathan Peyton 38227cc577a4SJonathan Peyton // compute num_tasks/grainsize based on the input provided 38237cc577a4SJonathan Peyton switch (sched) { 38247cc577a4SJonathan Peyton case 0: // no schedule clause specified, we can choose the default 38257cc577a4SJonathan Peyton // let's try to schedule (team_size*10) tasks 38267cc577a4SJonathan Peyton grainsize = thread->th.th_team_nproc * 10; 38277cc577a4SJonathan Peyton case 2: // num_tasks provided 38287cc577a4SJonathan Peyton if (grainsize > tc) { 38297cc577a4SJonathan Peyton num_tasks = tc; // too big num_tasks requested, adjust values 38307cc577a4SJonathan Peyton grainsize = 1; 38317cc577a4SJonathan Peyton extras = 0; 38327cc577a4SJonathan Peyton } else { 38337cc577a4SJonathan Peyton num_tasks = grainsize; 38347cc577a4SJonathan Peyton grainsize = tc / num_tasks; 38357cc577a4SJonathan Peyton extras = tc % num_tasks; 38367cc577a4SJonathan Peyton } 38377cc577a4SJonathan Peyton break; 38387cc577a4SJonathan Peyton case 1: // grainsize provided 38397cc577a4SJonathan Peyton if (grainsize > tc) { 38407cc577a4SJonathan Peyton num_tasks = 1; // too big grainsize requested, adjust values 38417cc577a4SJonathan Peyton grainsize = tc; 38427cc577a4SJonathan Peyton extras = 0; 38437cc577a4SJonathan Peyton } else { 38447cc577a4SJonathan Peyton num_tasks = tc / grainsize; 384593e17cfeSJonathan Peyton // adjust grainsize for balanced distribution of iterations 384693e17cfeSJonathan Peyton grainsize = tc / num_tasks; 38477cc577a4SJonathan Peyton extras = tc % num_tasks; 38487cc577a4SJonathan Peyton } 38497cc577a4SJonathan Peyton break; 38507cc577a4SJonathan Peyton default: 38517cc577a4SJonathan Peyton KMP_ASSERT2(0, "unknown scheduling of taskloop"); 38527cc577a4SJonathan Peyton } 38537cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(tc == num_tasks * grainsize + extras); 38547cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(num_tasks > extras); 38557cc577a4SJonathan Peyton KMP_DEBUG_ASSERT(num_tasks > 0); 385693e17cfeSJonathan Peyton // ========================================================================= 38577cc577a4SJonathan Peyton 38587cc577a4SJonathan Peyton // check if clause value first 38597cc577a4SJonathan Peyton if (if_val == 0) { // if(0) specified, mark task as serial 38607cc577a4SJonathan Peyton taskdata->td_flags.task_serial = 1; 38617cc577a4SJonathan Peyton taskdata->td_flags.tiedness = TASK_TIED; // AC: serial task cannot be untied 3862*82e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL 3863*82e94a59SJoachim Protze OMPT_STORE_RETURN_ADDRESS(gtid); 3864*82e94a59SJoachim Protze #endif 386593e17cfeSJonathan Peyton // always start serial tasks linearly 386693e17cfeSJonathan Peyton __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks, 386793e17cfeSJonathan Peyton grainsize, extras, tc, task_dup); 386893e17cfeSJonathan Peyton } else if (num_tasks > num_tasks_min) { 386993e17cfeSJonathan Peyton KA_TRACE(20, ("__kmpc_taskloop: T#%d, go recursive: tc %llu, #tasks %llu" 3870d74d8902SJonathan Peyton "(%lld), grain %llu, extras %llu\n", 3871d74d8902SJonathan Peyton gtid, tc, num_tasks, num_tasks_min, grainsize, extras)); 3872*82e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL 3873*82e94a59SJoachim Protze OMPT_STORE_RETURN_ADDRESS(gtid); 3874*82e94a59SJoachim Protze #endif 387593e17cfeSJonathan Peyton __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, num_tasks, 387693e17cfeSJonathan Peyton grainsize, extras, tc, num_tasks_min, task_dup); 387793e17cfeSJonathan Peyton } else { 387893e17cfeSJonathan Peyton KA_TRACE(20, ("__kmpc_taskloop: T#%d, go linear: tc %llu, #tasks %llu" 3879d74d8902SJonathan Peyton "(%lld), grain %llu, extras %llu\n", 3880d74d8902SJonathan Peyton gtid, tc, num_tasks, num_tasks_min, grainsize, extras)); 3881*82e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL 3882*82e94a59SJoachim Protze OMPT_STORE_RETURN_ADDRESS(gtid); 3883*82e94a59SJoachim Protze #endif 388493e17cfeSJonathan Peyton __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks, 388593e17cfeSJonathan Peyton grainsize, extras, tc, task_dup); 38867cc577a4SJonathan Peyton } 38877cc577a4SJonathan Peyton 3888*82e94a59SJoachim Protze if (nogroup == 0) { 3889*82e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL 3890*82e94a59SJoachim Protze OMPT_STORE_RETURN_ADDRESS(gtid); 3891*82e94a59SJoachim Protze #endif 38927cc577a4SJonathan Peyton __kmpc_end_taskgroup(loc, gtid); 3893*82e94a59SJoachim Protze } 3894*82e94a59SJoachim Protze #if OMPT_SUPPORT && OMPT_OPTIONAL 3895*82e94a59SJoachim Protze if (ompt_enabled.ompt_callback_work) { 3896*82e94a59SJoachim Protze ompt_callbacks.ompt_callback(ompt_callback_work)( 3897*82e94a59SJoachim Protze ompt_work_taskloop, ompt_scope_end, &(team_info->parallel_data), 3898*82e94a59SJoachim Protze &(task_info->task_data), 0, OMPT_GET_RETURN_ADDRESS(0)); 3899*82e94a59SJoachim Protze } 3900*82e94a59SJoachim Protze #endif 390193e17cfeSJonathan Peyton KA_TRACE(20, ("__kmpc_taskloop(exit): T#%d\n", gtid)); 39027cc577a4SJonathan Peyton } 39037cc577a4SJonathan Peyton 39047cc577a4SJonathan Peyton #endif 3905