1 //===--------- Mapping.h - OpenMP device runtime mapping helpers -- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 //===----------------------------------------------------------------------===//
12 #ifndef OMPTARGET_MAPPING_H
13 #define OMPTARGET_MAPPING_H
15 #include "DeviceTypes.h"
27 #pragma omp begin declare target device_type(nohost)
29 inline constexpr uint32_t MaxThreadsPerTeam
= 1024;
31 #pragma omp end declare target
33 /// Initialize the mapping machinery.
34 void init(bool IsSPMD
);
36 /// Return true if the kernel is executed in SPMD mode.
39 /// Return true if the kernel is executed in generic mode.
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
);
49 /// Return true if this thread is the initial thread in parallel level 0.
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
);
56 /// Return true if the executing thread has the lowest Id of the active threads
58 bool isLeaderInWarp();
60 /// Return a mask describing all active threads in the warp.
61 LaneMaskTy
activemask();
63 /// Return a mask describing all threads with a smaller Id in the warp.
64 LaneMaskTy
lanemaskLT();
66 /// Return a mask describing all threads with a larget Id in the warp.
67 LaneMaskTy
lanemaskGT();
69 /// Return the thread Id in the warp, in [0, getWarpSize()).
70 uint32_t getThreadIdInWarp();
72 /// Return the warp size, thus number of threads in the warp.
73 uint32_t getWarpSize();
75 /// Return the warp id in the block, in [0, getNumberOfWarpsInBlock()]
76 uint32_t getWarpIdInBlock();
78 /// Return the number of warps in the block.
79 uint32_t getNumberOfWarpsInBlock();
81 /// Return the thread Id in the block, in [0, getNumberOfThreadsInBlock(Dim)).
82 uint32_t getThreadIdInBlock(int32_t Dim
= DIM_X
);
84 /// Return the block size, thus number of threads in the block.
85 uint32_t getNumberOfThreadsInBlock(int32_t Dim
= DIM_X
);
87 /// Return the block Id in the kernel, in [0, getNumberOfBlocksInKernel(Dim)).
88 uint32_t getBlockIdInKernel(int32_t Dim
= DIM_X
);
90 /// Return the number of blocks in the kernel.
91 uint32_t getNumberOfBlocksInKernel(int32_t Dim
= DIM_X
);
93 /// Return the kernel size, thus number of threads in the kernel.
94 uint32_t getNumberOfThreadsInKernel();
96 /// Return the maximal number of threads in the block usable for a team (=
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
);
105 /// Return the number of processing elements on the device.
106 uint32_t getNumberOfProcessorElements();
108 } // namespace mapping