PXL
direction.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright 2026 XCENA Inc.
3 
4 #pragma once
5 
6 #include <cstddef>
7 #include <pxl/v3_api_guard.hpp>
8 #include <tuple>
9 #include <type_traits>
10 #include <utility>
11 #include <vector>
12 
13 namespace pxl
14 {
15 
16 template <typename T>
17 class NDArray;
18 
19 enum class ArgDir : unsigned char
20 {
21  InOut,
22  Input,
23  Output,
24 };
25 
27 {
28  // Empty means the legacy InOut policy for every argument.
29  std::vector<ArgDir> args;
30 };
31 
32 namespace detail
33 {
34 
35 template <typename T>
37 
38 template <typename R, typename... Args>
39 struct KernelFunctionTraits<R (*)(Args...)>
40 {
41  static constexpr std::size_t arity = sizeof...(Args);
42  using parameters = std::tuple<Args...>;
43 };
44 
45 template <typename R, typename... Args>
46 struct KernelFunctionTraits<R (*)(Args...) noexcept> : KernelFunctionTraits<R (*)(Args...)>
47 {
48 };
49 
50 template <auto Func>
51 inline constexpr std::size_t KernelArityV = KernelFunctionTraits<decltype(Func)>::arity;
52 
53 template <auto Func, std::size_t Index>
54 using KernelParamT =
55  std::tuple_element_t<Index, typename KernelFunctionTraits<decltype(Func)>::parameters>;
56 
57 template <typename T, typename = void>
59 {
60  static constexpr ArgDir value = ArgDir::InOut;
61 };
62 
63 template <typename T>
65  T, std::void_t<typename T::element_type, typename T::kernel_buffer_direction>>
66 {
67 private:
68  using direction_tag = typename T::kernel_buffer_direction;
69  static constexpr bool reads = direction_tag::reads_existing_data;
70  static constexpr bool writes = direction_tag::writes_result;
71 
72 public:
73  static_assert(reads || writes, "A directional kernel buffer needs read or write access");
74  static constexpr ArgDir value = reads ? (writes ? ArgDir::InOut : ArgDir::Input)
76 };
77 
78 template <typename T>
79 inline constexpr ArgDir KernelParamDirectionV =
81 
82 template <typename T, typename = void>
83 struct IsDirectionalKernelParam : std::false_type
84 {
85 };
86 
87 template <typename T>
89  T, std::void_t<typename T::element_type, typename T::kernel_buffer_direction>>
90  : std::true_type
91 {
92 };
93 
94 template <typename T>
95 inline constexpr bool IsDirectionalKernelParamV =
97 
98 template <typename T>
100 {
101  static constexpr bool valid = false;
102  using element_type = void;
103  using pointer = void*;
104 };
105 
106 template <typename T>
108 {
109  static constexpr bool valid = true;
110  using element_type = T;
111  using pointer = T*;
112 };
113 
114 template <typename T>
116 {
117  static constexpr bool valid = true;
118  using element_type = T;
119  using pointer = T*;
120 };
121 
122 template <typename Param, typename Actual, bool = IsDirectionalKernelParamV<Param>>
123 struct KernelArgumentCompatible : std::true_type
124 {
125 };
126 
127 template <typename Param, typename Actual>
128 struct KernelArgumentCompatible<Param, Actual, true>
129 {
130 private:
131  using parameter_type = std::remove_cv_t<std::remove_reference_t<Param>>;
132  using parameter_element = typename parameter_type::element_type;
133  using direction_tag = typename parameter_type::kernel_buffer_direction;
135  using actual_element = typename actual_buffer::element_type;
136  static constexpr bool writes = direction_tag::writes_result;
137  using wire_element = std::conditional_t<writes, parameter_element,
138  std::add_const_t<parameter_element>>;
139 
140 public:
141  static constexpr bool value =
142  !std::is_reference_v<Param> &&
143  actual_buffer::valid &&
144  !std::is_void_v<std::remove_cv_t<parameter_element>> &&
145  !std::is_function_v<parameter_element> &&
146  (!writes || !std::is_const_v<parameter_element>) &&
147  std::is_same_v<std::remove_cv_t<actual_element>,
148  std::remove_cv_t<parameter_element>> &&
149  std::is_convertible_v<typename actual_buffer::pointer, wire_element*>;
150 };
151 
152 template <typename Param, typename Actual>
153 inline constexpr bool KernelArgumentCompatibleV =
155 
156 template <auto Func, typename ActualTuple, std::size_t... Indices>
157 constexpr bool KernelArgumentsCompatible(std::index_sequence<Indices...>)
158 {
161  std::tuple_element_t<Indices, ActualTuple>> &&
162  ...);
163 }
164 
165 template <auto Func, typename... Actual>
167 {
168  if constexpr (sizeof...(Actual) != KernelArityV<Func>)
169  {
170  return false;
171  }
172  else
173  {
174  return KernelArgumentsCompatible<Func, std::tuple<Actual...>>(
175  std::make_index_sequence<KernelArityV<Func>>{});
176  }
177 }
178 
179 template <auto Func, typename... Actual>
180 inline constexpr bool KernelArgumentsCompatibleV =
181  KernelArgumentsCompatible<Func, Actual...>();
182 
183 template <auto Func, typename... Actual>
185  std::enable_if_t<KernelArgumentsCompatibleV<Func, Actual...>, int>;
186 
187 template <auto Func, std::size_t... Indices>
188 ArgDirections MakeKernelDirections(std::index_sequence<Indices...>)
189 {
190  ArgDirections directions;
192  {
193  return directions;
194  }
195  directions.args = {KernelParamDirectionV<KernelParamT<Func, Indices>>...};
196  return directions;
197 }
198 
199 template <auto Func>
201 {
202  return MakeKernelDirections<Func>(std::make_index_sequence<KernelArityV<Func>>{});
203 }
204 
205 } // namespace detail
206 
207 namespace impl
208 {
209 
210 inline ArgDir directionAt(const std::vector<ArgDir>& directions, std::size_t index) noexcept
211 {
212  return index < directions.size() ? directions[index] : ArgDir::InOut;
213 }
214 
215 } // namespace impl
216 
217 } // namespace pxl
NDArray is a class that can be used in both host and device code. NDArray is automatically divided in...
Definition: ndarray.hpp:27
ArgDirections MakeKernelDirections(std::index_sequence< Indices... >)
Definition: direction.hpp:188
constexpr bool KernelArgumentCompatibleV
Definition: direction.hpp:153
constexpr ArgDir KernelParamDirectionV
Definition: direction.hpp:79
constexpr std::size_t KernelArityV
Definition: direction.hpp:51
constexpr bool IsDirectionalKernelParamV
Definition: direction.hpp:95
constexpr bool KernelArgumentsCompatibleV
Definition: direction.hpp:180
std::enable_if_t< KernelArgumentsCompatibleV< Func, Actual... >, int > EnableCompatibleKernelArguments
Definition: direction.hpp:185
constexpr bool KernelArgumentsCompatible(std::index_sequence< Indices... >)
Definition: direction.hpp:157
std::tuple_element_t< Index, typename KernelFunctionTraits< decltype(Func)>::parameters > KernelParamT
Definition: direction.hpp:55
Definition: config.hpp:13
ArgDir
Definition: direction.hpp:20
std::vector< ArgDir > args
Definition: direction.hpp:29
static constexpr bool valid
Definition: direction.hpp:101
static constexpr ArgDir value
Definition: direction.hpp:60