alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
ThreadSpec.hpp
Go to the documentation of this file.
1/* Copyright 2024 René Widera
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
7#include "alpaka/Vec.hpp"
9#include "alpaka/concepts.hpp"
11
12#include <cstdint>
13#include <ostream>
14
15namespace alpaka::onHost
16{
17 /** @brief Backend-specific description of the actual block and thread launch shape.
18 *
19 * A thread specification directly describes the number of blocks and the number of threads per block that are
20 * passed to the backend. This is the closest alpaka equivalent to a CUDA grid/block launch configuration.
21 *
22 * In contrast to `alpaka::onHost::FrameSpec`, a `ThreadSpec` is not a logical frame decomposition. It is used
23 * when a kernel requires exact guarantees about the number of blocks and the size of each block.
24 *
25 * @tparam T_Executor If the executor is alpaka::exec::AnyExecutor alpaka will select a good fitting executor for
26 * the action where the ThreadSpec is used.
27 */
28 template<
29 alpaka::concepts::Vector T_NumBlocks,
30 alpaka::concepts::Vector<typename T_NumBlocks::type, T_NumBlocks::dim()> T_NumThreads,
31 alpaka::concepts::Executor T_Executor = alpaka::exec::AnyExecutor>
33 {
34 using index_type = typename T_NumBlocks::type;
35 using NumBlocksVecType = typename T_NumBlocks::UniVec;
36 using NumThreadsVecType = T_NumThreads;
37
38 private:
41
42 public:
43 constexpr ThreadSpec(
44 T_NumBlocks const& numBlocks,
45 T_NumThreads const& numThreadsPerBlock,
46 T_Executor executor = T_Executor{})
47 : m_numBlocks(numBlocks)
48 , m_numThreads(numThreadsPerBlock)
49 {
50 alpaka::unused(executor);
51 }
52
53 [[nodiscard]] static constexpr T_Executor getExecutor()
54 {
55 return T_Executor{};
56 }
57
58 [[nodiscard]] constexpr NumThreadsVecType const& getNumThreads() const noexcept
59 {
60 return m_numThreads;
61 }
62
63 [[nodiscard]] constexpr NumBlocksVecType const& getNumBlocks() const noexcept
64 {
65 return m_numBlocks;
66 }
67
68 [[nodiscard]] static consteval uint32_t dim()
69 {
70 return T_NumThreads::dim();
71 }
72 };
73
74 template<alpaka::concepts::VectorOrScalar T_NumBlocks, alpaka::concepts::VectorOrScalar T_NumThreads>
75 ThreadSpec(T_NumBlocks const&, T_NumThreads const&)
77
78 template<
82 ThreadSpec(T_NumBlocks const&, T_NumThreads const&, T_Executor)
84
85 namespace trait
86 {
87 template<typename T>
88 struct IsThreadSpec : std::false_type
89 {
90 };
91
92 template<
93 alpaka::concepts::Vector T_NumBlocks,
94 alpaka::concepts::Vector T_NumThreads,
96 struct IsThreadSpec<onHost::ThreadSpec<T_NumBlocks, T_NumThreads, T_Executor>> : std::true_type
97 {
98 };
99 } // namespace trait
100
101 template<typename T>
103
104 namespace concepts
105 {
106 /** Concept to check if a type is a ThreadSpec
107 *
108 * @tparam T Type to check
109 * @tparam T_IndexType enforce a index type of the thread specification, if not provided the type is not
110 * checked
111 * @tparam T_dim enforce a dimensionality of the thread specification, if not provided the value is not
112 * checked
113 */
114 template<typename T, typename T_IndexType = alpaka::NotRequired, uint32_t T_dim = alpaka::notRequiredDim>
117 && (std::same_as<T_IndexType, alpaka::NotRequired> || std::same_as<typename T::index_type, T_IndexType>)
118 && ((T_dim == alpaka::notRequiredDim) || (T::dim() == T_dim));
119 } // namespace concepts
120
121 std::ostream& operator<<(std::ostream& s, concepts::ThreadSpec auto const& t)
122 {
123 return s << "ThreadSpec{ blocks=" << t.getNumBlocks() << ", threads=" << t.getNumThreads() << " }";
124 }
125} // namespace alpaka::onHost
Concept to check for an executor.
Definition trait.hpp:133
Concept to check if a type is a vector or scalar variable.
Definition Vec.hpp:65
Concept to check if a type is a vector.
Definition Vec.hpp:54
Concept to check if a type is a ThreadSpec.
Functionality which is usable on the host CPU controller thread.
Definition api.hpp:40
std::ostream & operator<<(std::ostream &s, DeviceProperties const &p)
ThreadSpec(T_NumBlocks const &, T_NumThreads const &) -> ThreadSpec< alpaka::trait::getVec_t< T_NumBlocks >, alpaka::trait::getVec_t< T_NumThreads > >
constexpr bool isThreadSpec_v
typename GetVec< T >::type getVec_t
Definition Vec.hpp:948
constexpr uint32_t notRequiredDim
Definition trait.hpp:23
Backend-specific description of the actual block and thread launch shape.
typename T_NumBlocks::UniVec NumBlocksVecType
static constexpr T_Executor getExecutor()
NumBlocksVecType m_numBlocks
typename T_NumBlocks::type index_type
NumThreadsVecType m_numThreads
constexpr NumBlocksVecType const & getNumBlocks() const noexcept
constexpr ThreadSpec(T_NumBlocks const &numBlocks, T_NumThreads const &numThreadsPerBlock, T_Executor executor=T_Executor{})
constexpr NumThreadsVecType const & getNumThreads() const noexcept
static consteval uint32_t dim()