DPC++ Runtime
Runtime libraries for oneAPI DPC++
cg_types.hpp
Go to the documentation of this file.
1 //==---- cg_types.hpp - Auxiliary types required by command group class ----==//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #pragma once
10 
11 #include <sycl/detail/array.hpp> // for array
12 #include <sycl/detail/common.hpp> // for InitializedVal, NDLoop
13 #include <sycl/detail/helpers.hpp> // for Builder
14 #include <sycl/detail/host_profiling_info.hpp> // for HostProfilingInfo
15 #include <sycl/detail/item_base.hpp> // for id
16 #include <sycl/detail/kernel_desc.hpp> // for kernel_param_kind_t
17 #include <sycl/exception.hpp>
18 #include <sycl/group.hpp> // for group
19 #include <sycl/h_item.hpp> // for h_item
20 #include <sycl/id.hpp> // for id
21 #include <sycl/item.hpp> // for item
22 #include <sycl/kernel_handler.hpp> // for kernel_handler
23 #include <sycl/nd_item.hpp> // for nd_item
24 #include <sycl/nd_range.hpp> // for nd_range
25 #include <sycl/range.hpp> // for range, operator*
26 
27 #include <functional> // for function
28 #include <stddef.h> // for size_t
29 #include <type_traits> // for enable_if_t, false_type
30 #include <utility> // for declval
31 
32 namespace sycl {
33 inline namespace _V1 {
34 class interop_handle;
35 class handler;
36 namespace detail {
37 class HostTask;
38 
41 enum class CGType : unsigned int {
42  None = 0,
43  Kernel = 1,
44  CopyAccToPtr = 2,
45  CopyPtrToAcc = 3,
46  CopyAccToAcc = 4,
47  Barrier = 5,
48  BarrierWaitlist = 6,
49  Fill = 7,
50  UpdateHost = 8,
51  CopyUSM = 10,
52  FillUSM = 11,
53  PrefetchUSM = 12,
54  CodeplayHostTask = 14,
55  AdviseUSM = 15,
56  Copy2DUSM = 16,
57  Fill2DUSM = 17,
58  Memset2DUSM = 18,
59  CopyToDeviceGlobal = 19,
61  ReadWriteHostPipe = 21,
62  ExecCommandBuffer = 22,
63  CopyImage = 23,
64  SemaphoreWait = 24,
65  SemaphoreSignal = 25,
66  ProfilingTag = 26,
68 };
69 
70 template <typename, typename T> struct check_fn_signature {
71  static_assert(std::integral_constant<T, false>::value,
72  "Second template parameter is required to be of function type");
73 };
74 
75 template <typename F, typename RetT, typename... Args>
76 struct check_fn_signature<F, RetT(Args...)> {
77 private:
78  template <typename T>
79  static constexpr auto check(T *) -> typename std::is_same<
80  decltype(std::declval<T>().operator()(std::declval<Args>()...)),
81  RetT>::type;
82 
83  template <typename> static constexpr std::false_type check(...);
84 
85  using type = decltype(check<F>(0));
86 
87 public:
88  static constexpr bool value = type::value;
89 };
90 
91 template <typename F, typename... Args>
92 static constexpr bool check_kernel_lambda_takes_args() {
93  return check_fn_signature<std::remove_reference_t<F>, void(Args...)>::value;
94 }
95 
96 // isKernelLambdaCallableWithKernelHandlerImpl checks if LambdaArgType is void
97 // (e.g., in single_task), and based on that, calls
98 // check_kernel_lambda_takes_args with proper set of arguments. Also this type
99 // trait workarounds compilation error which happens only with msvc.
100 
101 template <
102  typename KernelType, typename LambdaArgType,
103  typename std::enable_if_t<std::is_same_v<LambdaArgType, void>> * = nullptr>
105  return check_kernel_lambda_takes_args<KernelType, kernel_handler>();
106 }
107 
108 template <
109  typename KernelType, typename LambdaArgType,
110  typename std::enable_if_t<!std::is_same_v<LambdaArgType, void>> * = nullptr>
112  return check_kernel_lambda_takes_args<KernelType, LambdaArgType,
113  kernel_handler>();
114 }
115 
116 // Type trait to find out if kernal lambda has kernel_handler argument
117 template <typename KernelType, typename LambdaArgType = void>
119  constexpr static bool value =
120  isKernelLambdaCallableWithKernelHandlerImpl<KernelType, LambdaArgType>();
121 };
122 
123 // Helpers for running kernel lambda on the host device
124 
125 template <typename KernelType>
126 typename std::enable_if_t<KernelLambdaHasKernelHandlerArgT<KernelType>::value>
127 runKernelWithoutArg(KernelType KernelName) {
128  kernel_handler KH;
129  KernelName(KH);
130 }
131 
132 template <typename KernelType>
133 typename std::enable_if_t<!KernelLambdaHasKernelHandlerArgT<KernelType>::value>
134 runKernelWithoutArg(KernelType KernelName) {
135  KernelName();
136 }
137 
138 template <typename ArgType, typename KernelType>
139 typename std::enable_if_t<
141 runKernelWithArg(KernelType KernelName, ArgType Arg) {
142  kernel_handler KH;
143  KernelName(Arg, KH);
144 }
145 
146 template <typename ArgType, typename KernelType>
147 typename std::enable_if_t<
149 runKernelWithArg(KernelType KernelName, ArgType Arg) {
150  KernelName(Arg);
151 }
152 
153 // The pure virtual class aimed to store lambda/functors of any type.
155 public:
156  // Return pointer to the lambda object.
157  // Used to extract captured variables.
158  virtual char *getPtr() = 0;
159  virtual ~HostKernelBase() = default;
160 };
161 
162 // Class which stores specific lambda object.
163 template <class KernelType, class KernelArgType, int Dims>
164 class HostKernel : public HostKernelBase {
165  using IDBuilder = sycl::detail::Builder;
166  KernelType MKernel;
167  // Allowing accessing MKernel from 'ResetHostKernelHelper' method of
168  // 'sycl::handler'
169  friend class sycl::handler;
170 
171 public:
172  HostKernel(KernelType Kernel) : MKernel(Kernel) {}
173 
174  char *getPtr() override { return reinterpret_cast<char *>(&MKernel); }
175 
176  ~HostKernel() = default;
177 };
178 
179 } // namespace detail
180 } // namespace _V1
181 } // namespace sycl
HostKernel(KernelType Kernel)
Definition: cg_types.hpp:172
char * getPtr() override
Definition: cg_types.hpp:174
Command group handler class.
Definition: handler.hpp:468
std::enable_if_t< KernelLambdaHasKernelHandlerArgT< KernelType, ArgType >::value > runKernelWithArg(KernelType KernelName, ArgType Arg)
Definition: cg_types.hpp:141
std::enable_if_t< KernelLambdaHasKernelHandlerArgT< KernelType >::value > runKernelWithoutArg(KernelType KernelName)
Definition: cg_types.hpp:127
constexpr bool isKernelLambdaCallableWithKernelHandlerImpl()
Definition: cg_types.hpp:104
CGType
Type of the command group.
Definition: cg_types.hpp:41
static constexpr bool check_kernel_lambda_takes_args()
Definition: cg_types.hpp:92
Definition: access.hpp:18