alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
WorkerGroup.hpp
Go to the documentation of this file.
1/* Copyright 2024 Andrea Bocci, René Widera
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
10#include "alpaka/tag.hpp"
11
12#include <cstdint>
13
14namespace alpaka::onAcc
15{
16 template<bool T_multiDimensional = true>
17 struct MultiDimensional : std::bool_constant<T_multiDimensional>
18 {
19 };
20
22
23 template<
24 typename T_ThreadIdxOrOrigin,
25 typename T_NumThreadsOrUnit,
26 typename T_MultiDimensional = MultiDimensional<true>>
28 {
29 /** WorkerGroup constructor
30 *
31 * @param threadIdxOrOrigin the index of the thread or onAcc::origin
32 * @param numThreadsOrUnit the number of threads or the onAcc::unit
33 * @param multiDimensional keep the dimensionality for both input parameters, if 'linearized' is used the
34 * workgroup will be reduced to a one dimensional group.
35 */
36 constexpr WorkerGroup(
37 T_ThreadIdxOrOrigin threadIdxOrOrigin,
38 T_NumThreadsOrUnit numThreadsOrUnit,
39 T_MultiDimensional = MultiDimensional<true>{})
40 : m_threadIdxOrOrigin{threadIdxOrOrigin}
41 , m_numThreadsOrUnit{numThreadsOrUnit}
42 {
43 }
44
45 constexpr auto size(auto const& acc) const
46 {
47 return getThreadSpace(acc).size();
48 }
49
50 constexpr auto idx(auto const& acc) const
51 {
52 return getThreadSpace(acc).idx();
53 }
54
55 private:
56 template<typename T_ThreadGroup, typename T_ThreadIdxOrOriginRange>
57 friend struct DomainSpec;
58
59 /** get the thread configuration
60 *
61 * Implementation specialization for vectors.
62 */
63 constexpr auto getThreadSpace([[maybe_unused]] auto const& acc) const
65 {
66 if constexpr(T_MultiDimensional::value == false)
67 return ThreadSpace{
68 Vec{linearize(m_numThreadsOrUnit, m_threadIdxOrOrigin)},
69 Vec{m_numThreadsOrUnit.product()}};
70 else
71 return ThreadSpace{m_threadIdxOrOrigin, m_numThreadsOrUnit};
72 }
73
74 /** get the thread configuration
75 *
76 * Implementation specialization for lazy evaluated acc properties based on an origin and unit.
77 */
78 constexpr auto getThreadSpace(auto const& acc) const
80 {
81 auto const idx
82 = internalCompute::GetIdxWithin::Op<ALPAKA_TYPEOF(acc), T_ThreadIdxOrOrigin, T_NumThreadsOrUnit>{}(
83 acc,
84 m_threadIdxOrOrigin,
85 m_numThreadsOrUnit);
86 auto const extent
87 = internalCompute::GetExtentsOf::Op<ALPAKA_TYPEOF(acc), T_ThreadIdxOrOrigin, T_NumThreadsOrUnit>{}(
88 acc,
89 m_threadIdxOrOrigin,
90 m_numThreadsOrUnit);
91
92 if constexpr(T_MultiDimensional::value == false)
93 return ThreadSpace{Vec{linearize(extent, idx)}, Vec{extent.product()}};
94 else
95 return ThreadSpace{idx, extent};
96 }
97
98 private:
99 T_ThreadIdxOrOrigin m_threadIdxOrOrigin;
100 T_NumThreadsOrUnit m_numThreadsOrUnit;
101 };
102
103 namespace worker
104 {
105 constexpr auto threadsInGrid = WorkerGroup{origin::grid, unit::threads};
106 constexpr auto blocksInGrid = WorkerGroup{origin::grid, unit::blocks};
107 constexpr auto threadsInBlock = WorkerGroup{origin::block, unit::threads};
108
109 /** Representation of all threads in a warp as a linearized worker group */
110 constexpr auto linearThreadsInWarp = WorkerGroup{origin::warp, unit::threads};
111 constexpr auto linearThreadsInBlock = WorkerGroup{origin::block, unit::threads, linearized};
112 constexpr auto linearThreadsInGrid = WorkerGroup{origin::grid, unit::threads, linearized};
113
114 /** Representation of all warps in a thread block as a linearized worker group
115 *
116 * @attention If the number of threads in a block is not a multiple of the warp size you can have partial
117 * warps.
118 */
119 constexpr auto linearWarpsInBlock = WorkerGroup{origin::block, unit::warps};
120 /** Representation of all warps in the grid as a linearized worker group
121 *
122 * @attention Since a thread block is not required to have as many threads a warp has, you can not assume that
123 * number of warps * warp size is the total number of threads.
124 */
125 constexpr auto linearWarpsInGrid = WorkerGroup{origin::grid, unit::warps};
126
127 constexpr auto linearBlocksInGrid = WorkerGroup{origin::grid, unit::blocks, linearized};
128
129 /** Represent the identity of the executor thread.
130 *
131 * All threads are in the same worker group.
132 * If used with onAcc::makeIdxMap(), any thread is getting all indices of the range.
133 */
134 constexpr auto allThreads = WorkerGroup{origin::thread, unit::threads};
135 } // namespace worker
136
137} // namespace alpaka::onAcc
#define ALPAKA_TYPEOF(...)
Get the type of instance.
Definition common.hpp:154
constexpr auto blocksInGrid
constexpr auto threadsInGrid
constexpr auto linearWarpsInGrid
Representation of all warps in the grid as a linearized worker group.
constexpr auto linearThreadsInGrid
constexpr auto linearBlocksInGrid
constexpr auto allThreads
Represent the identity of the executor thread.
constexpr auto linearThreadsInBlock
constexpr auto linearWarpsInBlock
Representation of all warps in a thread block as a linearized worker group.
constexpr auto linearThreadsInWarp
Representation of all threads in a warp as a linearized worker group.
constexpr auto threadsInBlock
functionality which is usable on the accelerator compute device from within a kernel.
Definition executor.hpp:38
constexpr bool isUnit_v
Definition tag.hpp:91
constexpr bool isOrigin_v
Definition tag.hpp:88
constexpr auto linearized
constexpr T_IntegralType linearize(Vec< T_IntegralType, T_dim - 1u, T_Storage > const &dim, Vec< T_IntegralType, T_dim, T_OtherStorage > const &idx)
Give the linear index of an N-dimensional index within an N-dimensional index space.
Definition Vec.hpp:839
ALPAKA_FN_HOST_ACC Vec(T_1, T_Args...) -> Vec< T_1, uint32_t(sizeof...(T_Args)+1u), ArrayStorage< T_1, uint32_t(sizeof...(T_Args)+1u)> >
constexpr bool isVector_v
Definition Vec.hpp:39
constexpr WorkerGroup(T_ThreadIdxOrOrigin threadIdxOrOrigin, T_NumThreadsOrUnit numThreadsOrUnit, T_MultiDimensional=MultiDimensional< true >{})
WorkerGroup constructor.
constexpr auto size(auto const &acc) const
constexpr auto idx(auto const &acc) const