1 //===--------- Mapping.h - OpenMP device runtime mapping helpers -- 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 // 10 //===----------------------------------------------------------------------===// 11 12 #ifndef OMPTARGET_MAPPING_H 13 #define OMPTARGET_MAPPING_H 14 15 #include "Types.h" 16 17 namespace _OMP { 18 19 namespace mapping { 20 21 #pragma omp begin declare target device_type(nohost) 22 23 inline constexpr uint32_t MaxThreadsPerTeam = 1024; 24 25 #pragma omp end declare target 26 27 /// Initialize the mapping machinery. 28 void init(bool IsSPMD); 29 30 /// Return true if the kernel is executed in SPMD mode. 31 bool isSPMDMode(); 32 33 /// Return true if the kernel is executed in generic mode. 34 bool isGenericMode(); 35 36 /// Return true if the executing thread is the main thread in generic mode. 37 /// These functions will lookup state and it is required that that is OK for the 38 /// thread and location. See also `isInitialThreadInLevel0` for a stateless 39 /// alternative for certain situations, e.g. during initialization. 40 bool isMainThreadInGenericMode(); 41 bool isMainThreadInGenericMode(bool IsSPMD); 42 43 /// Return true if this thread is the initial thread in parallel level 0. 44 /// 45 /// The thread for which this returns true should be used for single threaded 46 /// initialization tasks. We pick a special thread to ensure there are no 47 /// races between the initialization and the first read of initialized state. 48 bool isInitialThreadInLevel0(bool IsSPMD); 49 50 /// Return true if the executing thread has the lowest Id of the active threads 51 /// in the warp. 52 bool isLeaderInWarp(); 53 54 /// Return a mask describing all active threads in the warp. 55 LaneMaskTy activemask(); 56 57 /// Return a mask describing all threads with a smaller Id in the warp. 58 LaneMaskTy lanemaskLT(); 59 60 /// Return a mask describing all threads with a larget Id in the warp. 61 LaneMaskTy lanemaskGT(); 62 63 /// Return the thread Id in the warp, in [0, getWarpSize()). 64 uint32_t getThreadIdInWarp(); 65 66 /// Return the thread Id in the block, in [0, getBlockSize()). 67 uint32_t getThreadIdInBlock(); 68 69 /// Return the warp id in the block. 70 uint32_t getWarpId(); 71 72 /// Return the warp size, thus number of threads in the warp. 73 uint32_t getWarpSize(); 74 75 /// Return the number of warps in the block. 76 uint32_t getNumberOfWarpsInBlock(); 77 78 /// Return the block Id in the kernel, in [0, getKernelSize()). 79 uint32_t getBlockId(); 80 81 /// Return the block size, thus number of threads in the block. 82 /// 83 /// Note: The version taking \p IsSPMD mode explicitly can be used during the 84 /// initialization of the target region, that is before `mapping::isSPMDMode()` 85 /// can be called by any thread other than the main one. 86 uint32_t getBlockSize(); 87 uint32_t getBlockSize(bool IsSPMD); 88 89 /// Return the number of blocks in the kernel. 90 uint32_t getNumberOfBlocks(); 91 92 /// Return the kernel size, thus number of threads in the kernel. 93 uint32_t getKernelSize(); 94 95 /// Return the number of processing elements on the device. 96 uint32_t getNumberOfProcessorElements(); 97 98 } // namespace mapping 99 100 } // namespace _OMP 101 102 #endif 103