alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
KernelBundle.hpp
Go to the documentation of this file.
1/* Copyright 2023 René Widera, Mehmet Yusufoglu
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
7#include "alpaka/apply.hpp"
12#include "alpaka/trait.hpp"
13#include "alpaka/utility.hpp"
14
15#include <tuple>
16#include <type_traits>
17
18namespace alpaka
19{
20 namespace onHost
21 {
22 /** Provides an instance of an object which can be used within the compute kernel*/
24 {
25 template<typename T_Any>
26 struct Op
27 {
28 /** @return @attention returns a reference to the original data */
29 auto const& operator()(auto const& any) const
30 {
31 return any;
32 }
33
34 auto& operator()(auto& any) const
35 {
36 return any;
37 }
38 };
39 };
40
41 /** Provides an instance of an object which can be used within the compute kernel
42 *
43 * @return compute kernel compatible object if MakeAccessibleOnAcc is specialized else the identity
44 */
45 inline decltype(auto) makeAccessibleOnAcc(auto&& any)
46 {
48 }
49 } // namespace onHost
50
51 //! \brief The class used to bind kernel function object and arguments together. Once an instance of this class
52 //! is created, arguments are not needed to be separately given to functions who need kernel function and
53 //! arguments.
54 //! \tparam TKernelFn The kernel function object type.
55 //! \tparam TArgs Kernel function object
56 //! invocation argument types as a parameter pack.
57 template<typename TKernelFn, typename... TArgs>
59 {
60 public:
61 //! The function object type
62 using KernelFn = std::decay_t<TKernelFn>;
63 //! Tuple type to encapsulate kernel function argument types and argument values
64 using ArgTuple = std::conditional_t<
65 sizeof...(TArgs) == 0,
66 std::tuple<>,
68
69 // Constructor
70 constexpr KernelBundle(KernelFn const& kernelFn) : m_kernelFn{kernelFn}, m_args(std::tuple<>{})
71 {
72 static_assert(
74 "Kernel functor must be trivially copyable or specialize trait::IsKernelTriviallyCopyable<>!");
75 }
76
77 // Constructor
78 constexpr KernelBundle(KernelFn const& kernelFn, auto&&... args)
79 : m_kernelFn{kernelFn}
80 , m_args(onHost::makeAccessibleOnAcc(ALPAKA_FORWARD(args))...)
81 {
82 static_assert(
84 "Kernel functor must be trivially copyable or specialize trait::IsKernelTriviallyCopyable<>!");
85 static_assert(
88 && ...),
89 "All kernel arguments must be trivially copyable or specialize "
90 "trait::IsKernelArgumentTriviallyCopyable<>!");
91 }
92
93 constexpr KernelBundle(KernelBundle const& b) = default;
94 constexpr KernelBundle& operator=(KernelBundle const&) = default;
95
96 /** allow move assignment and constriction
97 *
98 * @attention if the functor or the arguments contains non movable types the move operators can be
99 * inaccessible.
100 *
101 * @{
102 */
103 constexpr KernelBundle(KernelBundle&& b) = default;
104 constexpr KernelBundle& operator=(KernelBundle&&) = default;
105
106 /** @} */
107
108 template<typename TAcc>
109 requires(
111 && std::is_invocable_v<
112 std::remove_const_t<KernelFn>,
113 TAcc,
115 constexpr auto operator()(TAcc const& acc) const
116 {
117 static_assert(
118 std::is_invocable_v<
119 std::add_const_t<KernelFn>,
120 TAcc,
122 "the operator() function of a kernel must be marked const");
123 static_assert(
124 std::same_as<
125 void,
126 std::invoke_result_t<
127 std::add_const_t<KernelFn>,
128 TAcc,
130 "the return type of the operator() function of a kernel must be void");
132 /* It is required to take the arguments as const reference.
133 * The reason is that these arguments are shared between threads in a block. If the user like to mutate
134 * these he should use a non const copy in the kernel function signature. This is the reason why we can
135 * not keep const correctness for buffers and view within the copy-constructor of these.
136 */
137 [&](alpaka::concepts::KernelArg auto const&... args) constexpr { m_kernelFn(acc, args...); },
138 m_args);
139 }
140
142 // Store the argument types without const and reference
144 };
145
146 //! \brief User defined deduction guide with trailing return type. For CTAD during the construction.
147 //! \tparam TKernelFn The kernel function object type.
148 //! \tparam TArgs Kernel function object argument types as a parameter pack.
149 //! \param kernelFn The kernel object
150 //! \param args The kernel invocation arguments.
151
152 //! \return Kernel function bundle. An instance of KernelBundle which consists the kernel function object and its
153 //! arguments.
154 template<typename TKernelFn, typename... TArgs>
155 ALPAKA_FN_HOST KernelBundle(TKernelFn const&, TArgs&&...) -> KernelBundle<TKernelFn, TArgs...>;
156
157 namespace trait
158 {
159 template<typename T>
160 struct IsKernelBundle : std::integral_constant<bool, isSpecializationOf_v<T, KernelBundle>>
161 {
162 };
163 } // namespace trait
164
165 template<typename T>
167
168} // namespace alpaka
169
170namespace alpaka::concepts
171{
172 /** Concept to check if a type is a KernelBundle
173 *
174 * @tparam T Type to check
175 */
176 template<typename T>
178} // namespace alpaka::concepts
The class used to bind kernel function object and arguments together. Once an instance of this class ...
std::conditional_t< sizeof...(TArgs)==0, std::tuple<>, alpaka::Tuple< remove_restrict_t< ALPAKA_TYPEOF(onHost::makeAccessibleOnAcc(std::declval< TArgs >()))>... > > ArgTuple
Tuple type to encapsulate kernel function argument types and argument values.
constexpr KernelBundle(KernelBundle &&b)=default
allow move assignment and constriction
constexpr KernelBundle & operator=(KernelBundle &&)=default
allow move assignment and constriction
constexpr KernelBundle(KernelFn const &kernelFn, auto &&... args)
constexpr KernelBundle(KernelFn const &kernelFn)
constexpr KernelBundle(KernelBundle const &b)=default
std::decay_t< TKernelFn > KernelFn
The function object type.
constexpr KernelBundle & operator=(KernelBundle const &)=default
#define ALPAKA_FN_HOST
All functions that can be used on an accelerator have to be attributed with ALPAKA_FN_ACC or ALPAKA_F...
Definition common.hpp:33
#define ALPAKA_TYPEOF(...)
Get the type of instance.
Definition common.hpp:154
#define ALPAKA_FORWARD(instance)
Perfectly forward an instance as argument.
Definition common.hpp:148
Concept to check for a kernel argument object.
Definition trait.hpp:143
Concept to check if a type is a KernelBundle.
Concept to check for a kernel function object.
Definition trait.hpp:135
Functionality which is usable on the host CPU controller thread.
Definition api.hpp:40
decltype(auto) makeAccessibleOnAcc(auto &&any)
Provides an instance of an object which can be used within the compute kernel.
main alpaka namespace.
Definition alpaka.hpp:76
typename remove_restrict< T >::type remove_restrict_t
Helper to remove restrict from a type.
ALPAKA_FN_HOST KernelBundle(TKernelFn const &, TArgs &&...) -> KernelBundle< TKernelFn, TArgs... >
User defined deduction guide with trailing return type. For CTAD during the construction.
constexpr bool isKernelBundle_v
ALPAKA_FN_INLINE constexpr decltype(auto) apply(T_Func &&func, T_TupleLike &&tuple)
Applies a function to the elements of a tuple-like object.
Definition apply.hpp:35
STL namespace.
basic tuple implementation
Definition Tuple.hpp:52
auto const & operator()(auto const &any) const
Provides an instance of an object which can be used within the compute kernel.