1 //===---- Parallelism.cpp - OpenMP GPU parallel implementation ---- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Parallel implementation in the GPU. Here is the pattern: 10 // 11 // while (not finished) { 12 // 13 // if (master) { 14 // sequential code, decide which par loop to do, or if finished 15 // __kmpc_kernel_prepare_parallel() // exec by master only 16 // } 17 // syncthreads // A 18 // __kmpc_kernel_parallel() // exec by all 19 // if (this thread is included in the parallel) { 20 // switch () for all parallel loops 21 // __kmpc_kernel_end_parallel() // exec only by threads in parallel 22 // } 23 // 24 // 25 // The reason we don't exec end_parallel for the threads not included 26 // in the parallel loop is that for each barrier in the parallel 27 // region, these non-included threads will cycle through the 28 // syncthread A. Thus they must preserve their current threadId that 29 // is larger than thread in team. 30 // 31 // To make a long story short... 32 // 33 //===----------------------------------------------------------------------===// 34 35 #include "Debug.h" 36 #include "Interface.h" 37 #include "Mapping.h" 38 #include "State.h" 39 #include "Synchronization.h" 40 #include "Types.h" 41 #include "Utils.h" 42 43 using namespace _OMP; 44 45 #pragma omp declare target 46 47 namespace { 48 49 uint32_t determineNumberOfThreads(int32_t NumThreadsClause) { 50 uint32_t NThreadsICV = 51 NumThreadsClause != -1 ? NumThreadsClause : icv::NThreads; 52 uint32_t NumThreads = mapping::getBlockSize(); 53 54 if (NThreadsICV != 0 && NThreadsICV < NumThreads) 55 NumThreads = NThreadsICV; 56 57 // Round down to a multiple of WARPSIZE since it is legal to do so in OpenMP. 58 if (NumThreads < mapping::getWarpSize()) 59 NumThreads = 1; 60 else 61 NumThreads = (NumThreads & ~((uint32_t)mapping::getWarpSize() - 1)); 62 63 return NumThreads; 64 } 65 66 // Invoke an outlined parallel function unwrapping arguments (up to 32). 67 void invokeMicrotask(int32_t global_tid, int32_t bound_tid, void *fn, 68 void **args, int64_t nargs) { 69 switch (nargs) { 70 #include "generated_microtask_cases.gen" 71 default: 72 PRINT("Too many arguments in kmp_invoke_microtask, aborting execution.\n"); 73 __builtin_trap(); 74 } 75 } 76 77 } // namespace 78 79 extern "C" { 80 81 void __kmpc_parallel_51(IdentTy *ident, int32_t, int32_t if_expr, 82 int32_t num_threads, int proc_bind, void *fn, 83 void *wrapper_fn, void **args, int64_t nargs) { 84 85 uint32_t TId = mapping::getThreadIdInBlock(); 86 // Handle the serialized case first, same for SPMD/non-SPMD. 87 if (OMP_UNLIKELY(!if_expr || icv::Level)) { 88 state::enterDataEnvironment(); 89 ++icv::Level; 90 invokeMicrotask(TId, 0, fn, args, nargs); 91 state::exitDataEnvironment(); 92 return; 93 } 94 95 uint32_t NumThreads = determineNumberOfThreads(num_threads); 96 if (mapping::isSPMDMode()) { 97 // Avoid the race between the read of the `icv::Level` above and the write 98 // below by synchronizing all threads here. 99 synchronize::threadsAligned(); 100 { 101 // Note that the order here is important. `icv::Level` has to be updated 102 // last or the other updates will cause a thread specific state to be 103 // created. 104 state::ValueRAII ParallelTeamSizeRAII(state::ParallelTeamSize, NumThreads, 105 1u, TId == 0); 106 state::ValueRAII ActiveLevelRAII(icv::ActiveLevel, 1u, 0u, TId == 0); 107 state::ValueRAII LevelRAII(icv::Level, 1u, 0u, TId == 0); 108 109 // Synchronize all threads after the main thread (TId == 0) set up the 110 // team state properly. 111 synchronize::threadsAligned(); 112 113 ASSERT(state::ParallelTeamSize == NumThreads); 114 ASSERT(icv::ActiveLevel == 1u); 115 ASSERT(icv::Level == 1u); 116 117 if (TId < NumThreads) 118 invokeMicrotask(TId, 0, fn, args, nargs); 119 120 // Synchronize all threads at the end of a parallel region. 121 synchronize::threadsAligned(); 122 } 123 124 ASSERT(state::ParallelTeamSize == 1u); 125 ASSERT(icv::ActiveLevel == 0u); 126 ASSERT(icv::Level == 0u); 127 return; 128 } 129 130 // We do *not* create a new data environment because all threads in the team 131 // that are active are now running this parallel region. They share the 132 // TeamState, which has an increase level-var and potentially active-level 133 // set, but they do not have individual ThreadStates yet. If they ever 134 // modify the ICVs beyond this point a ThreadStates will be allocated. 135 136 bool IsActiveParallelRegion = NumThreads > 1; 137 if (!IsActiveParallelRegion) { 138 state::ValueRAII LevelRAII(icv::Level, 1u, 0u, true); 139 invokeMicrotask(TId, 0, fn, args, nargs); 140 return; 141 } 142 143 void **GlobalArgs = nullptr; 144 if (nargs) { 145 __kmpc_begin_sharing_variables(&GlobalArgs, nargs); 146 #pragma unroll 147 for (int I = 0; I < nargs; I++) 148 GlobalArgs[I] = args[I]; 149 } 150 151 { 152 // Note that the order here is important. `icv::Level` has to be updated 153 // last or the other updates will cause a thread specific state to be 154 // created. 155 state::ValueRAII ParallelTeamSizeRAII(state::ParallelTeamSize, NumThreads, 156 1u, true); 157 state::ValueRAII ParallelRegionFnRAII(state::ParallelRegionFn, wrapper_fn, 158 (void *)nullptr, true); 159 state::ValueRAII ActiveLevelRAII(icv::ActiveLevel, 1u, 0u, true); 160 state::ValueRAII LevelRAII(icv::Level, 1u, 0u, true); 161 162 // Master signals work to activate workers. 163 synchronize::threads(); 164 // Master waits for workers to signal. 165 synchronize::threads(); 166 } 167 168 if (nargs) 169 __kmpc_end_sharing_variables(); 170 } 171 172 __attribute__((noinline)) bool 173 __kmpc_kernel_parallel(ParallelRegionFnTy *WorkFn) { 174 // Work function and arguments for L1 parallel region. 175 *WorkFn = state::ParallelRegionFn; 176 177 // If this is the termination signal from the master, quit early. 178 if (!*WorkFn) 179 return false; 180 181 // Set to true for workers participating in the parallel region. 182 uint32_t TId = mapping::getThreadIdInBlock(); 183 bool ThreadIsActive = TId < state::ParallelTeamSize; 184 return ThreadIsActive; 185 } 186 187 __attribute__((noinline)) void __kmpc_kernel_end_parallel() { 188 // In case we have modified an ICV for this thread before a ThreadState was 189 // created. We drop it now to not contaminate the next parallel region. 190 ASSERT(!mapping::isSPMDMode()); 191 uint32_t TId = mapping::getThreadIdInBlock(); 192 state::resetStateForThread(TId); 193 ASSERT(!mapping::isSPMDMode()); 194 } 195 196 uint16_t __kmpc_parallel_level(IdentTy *, uint32_t) { return omp_get_level(); } 197 198 int32_t __kmpc_global_thread_num(IdentTy *) { return omp_get_thread_num(); } 199 200 void __kmpc_push_num_threads(IdentTy *, int32_t, int32_t NumThreads) { 201 icv::NThreads = NumThreads; 202 } 203 204 void __kmpc_push_num_teams(IdentTy *loc, int32_t tid, int32_t num_teams, 205 int32_t thread_limit) {} 206 207 void __kmpc_push_proc_bind(IdentTy *loc, uint32_t tid, int proc_bind) {} 208 } 209 210 #pragma omp end declare target 211