DPC++ Runtime
Runtime libraries for oneAPI DPC++
kernel.hpp
Go to the documentation of this file.
1 //==--------------- kernel.hpp --- SYCL kernel -----------------------------==//
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/backend_types.hpp> // for backend, backend_return_t
12 #include <sycl/context.hpp> // for context
13 #include <sycl/detail/defines_elementary.hpp> // for __SYCL2020_DEPRECATED
14 #include <sycl/detail/export.hpp> // for __SYCL_EXPORT
15 #include <sycl/detail/info_desc_helpers.hpp> // for is_kernel_device_specif...
16 #include <sycl/detail/owner_less_base.hpp> // for OwnerLessBase
17 #include <sycl/detail/pi.h> // for pi_native_handle
18 #include <sycl/detail/string.hpp>
20 #include <sycl/detail/util.hpp>
21 #include <sycl/device.hpp> // for device
22 #include <sycl/kernel_bundle_enums.hpp> // for bundle_state
23 #include <sycl/range.hpp> // for range
24 
25 #include <cstddef> // for size_t
26 #include <memory> // for shared_ptr, hash, opera...
27 #include <variant> // for hash
28 
29 namespace sycl {
30 inline namespace _V1 {
31 // Forward declaration
32 class context;
33 class queue;
34 template <backend Backend> class backend_traits;
35 template <bundle_state State> class kernel_bundle;
36 template <backend BackendName, class SyclObjectT>
37 auto get_native(const SyclObjectT &Obj)
38  -> backend_return_t<BackendName, SyclObjectT>;
39 
40 namespace detail {
41 class kernel_impl;
42 
45 class auto_name {};
46 
50 template <typename Name, typename Type> struct get_kernel_name_t {
51  using name = Name;
52  static_assert(
53  !std::is_same_v<Name, auto_name>,
54  "No kernel name provided without -fsycl-unnamed-lambda enabled!");
55 };
56 
57 #ifdef __SYCL_UNNAMED_LAMBDA__
63 template <typename Type> struct get_kernel_name_t<detail::auto_name, Type> {
64  using name = Type;
65 };
66 #endif // __SYCL_UNNAMED_LAMBDA__
67 
68 } // namespace detail
69 
77 class __SYCL_EXPORT kernel : public detail::OwnerLessBase<kernel> {
78 public:
86 #ifdef __SYCL_INTERNAL_API
87  kernel(cl_kernel ClKernel, const context &SyclContext);
88 #endif
89 
90  kernel(const kernel &RHS) = default;
91 
92  kernel(kernel &&RHS) = default;
93 
94  kernel &operator=(const kernel &RHS) = default;
95 
96  kernel &operator=(kernel &&RHS) = default;
97 
98  bool operator==(const kernel &RHS) const { return impl == RHS.impl; }
99 
100  bool operator!=(const kernel &RHS) const { return !operator==(RHS); }
101 
109 #ifdef __SYCL_INTERNAL_API
110  cl_kernel get() const;
111 #endif
112 
117  "is_host() is deprecated as the host device is no longer supported.")
118  bool is_host() const;
119 
126  context get_context() const;
127 
131  backend get_backend() const noexcept;
132 
136  kernel_bundle<bundle_state::executable> get_kernel_bundle() const;
137 
142  template <typename Param>
143  typename detail::is_kernel_info_desc<Param>::return_type get_info() const {
144  return detail::convert_from_abi_neutral(get_info_impl<Param>());
145  }
146 
150  template <typename Param>
152  get_backend_info() const;
153 
159  template <typename Param>
161  get_info(const device &Device) const;
162 
170  template <typename Param>
171  __SYCL2020_DEPRECATED("Use the overload without the second parameter")
172  typename detail::is_kernel_device_specific_info_desc<Param>::return_type
173  get_info(const device &Device, const range<3> &WGSize) const;
174 
175  // TODO: Revisit and align with sycl_ext_oneapi_forward_progress extension
176  // once #7598 is merged.
177  template <typename Param>
178  typename Param::return_type ext_oneapi_get_info(const queue &q) const;
179 
180 private:
182  kernel(std::shared_ptr<detail::kernel_impl> Impl);
183 
184  pi_native_handle getNative() const;
185 
186  __SYCL_DEPRECATED("Use getNative() member function")
187  pi_native_handle getNativeImpl() const;
188 
189  std::shared_ptr<detail::kernel_impl> impl;
190 
191  template <class Obj>
192  friend decltype(Obj::impl) detail::getSyclObjImpl(const Obj &SyclObject);
193  template <class T>
194  friend T detail::createSyclObjFromImpl(decltype(T::impl) ImplObj);
195  template <backend BackendName, class SyclObjectT>
196  friend auto get_native(const SyclObjectT &Obj)
197  -> backend_return_t<BackendName, SyclObjectT>;
198  template <typename Param>
199  typename detail::ABINeutralT_t<
200  typename detail::is_kernel_info_desc<Param>::return_type>
201  get_info_impl() const;
202 };
203 } // namespace _V1
204 } // namespace sycl
205 
206 namespace std {
207 template <> struct hash<sycl::kernel> {
208  size_t operator()(const sycl::kernel &Kernel) const {
209  return hash<std::shared_ptr<sycl::detail::kernel_impl>>()(
211  }
212 };
213 } // namespace std
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:50
This class is the default KernelName template parameter type for kernel invocation APIs such as singl...
Definition: kernel.hpp:45
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:64
The kernel_bundle class represents collection of device images in a particular state.
Provides an abstraction of a SYCL kernel.
Definition: kernel.hpp:77
bool operator==(const kernel &RHS) const
Definition: kernel.hpp:98
kernel(kernel &&RHS)=default
kernel(const kernel &RHS)=default
Constructs a SYCL kernel instance from an OpenCL cl_kernel.
kernel & operator=(const kernel &RHS)=default
__SYCL2020_DEPRECATED("is_host() is deprecated as the host device is no longer supported.") bool is_host() const
Get a valid OpenCL kernel handle.
bool operator!=(const kernel &RHS) const
Definition: kernel.hpp:100
kernel & operator=(kernel &&RHS)=default
Encapsulates a single SYCL queue which schedules kernels on a SYCL device.
Definition: queue.hpp:111
Defines the iteration domain of either a single work-group in a parallel dispatch,...
Definition: range.hpp:26
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: impl_utils.hpp:30
auto convert_from_abi_neutral(ParamT &&Info)
Definition: platform.hpp:74
T createSyclObjFromImpl(decltype(T::impl) ImplObj)
Definition: impl_utils.hpp:48
typename ABINeutralT< T >::type ABINeutralT_t
Definition: util.hpp:85
kernel_bundle(kernel_bundle< State > &&) -> kernel_bundle< State >
signed char __SYCL2020_DEPRECATED
Definition: aliases.hpp:94
kernel_bundle< State > get_kernel_bundle(const context &Ctx, const std::vector< device > &Devs)
A kernel bundle in state State which contains all of the kernels in the application which are compati...
pointer get() const
Definition: multi_ptr.hpp:544
auto get_native(const SyclObjectT &Obj) -> backend_return_t< BackendName, SyclObjectT >
Definition: backend.hpp:136
typename backend_traits< Backend >::template return_type< SyclType > backend_return_t
Definition: backend.hpp:87
Definition: access.hpp:18
uintptr_t pi_native_handle
Definition: pi.h:217
_Abi const simd< _Tp, _Abi > & noexcept
Definition: simd.hpp:1324
size_t operator()(const sycl::kernel &Kernel) const
Definition: kernel.hpp:208
Helper struct to get a kernel name type based on given Name and Type types: if Name is undefined (is ...
Definition: kernel.hpp:50