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