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