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 <cstddef> // for size_t
12 #include <memory> // for shared_ptr, hash, opera...
13 #include <sycl/backend_types.hpp> // for backend, backend_return_t
14 #include <sycl/context.hpp> // for context
15 #include <sycl/detail/defines_elementary.hpp> // for __SYCL2020_DEPRECATED
16 #include <sycl/detail/export.hpp> // for __SYCL_EXPORT
17 #include <sycl/detail/info_desc_helpers.hpp> // for is_kernel_device_specif...
18 #include <sycl/detail/owner_less_base.hpp> // for OwnerLessBase
19 #include <sycl/detail/pi.h> // for pi_native_handle
20 #include <sycl/detail/string.hpp>
22 #include <sycl/detail/util.hpp>
23 #include <sycl/device.hpp> // for device
24 #include <sycl/kernel_bundle_enums.hpp> // for bundle_state
25 #include <sycl/range.hpp> // for range
26 #include <variant> // for hash
27 
28 namespace sycl {
29 inline namespace _V1 {
30 // Forward declaration
31 class context;
32 class queue;
33 template <backend Backend> class backend_traits;
34 template <bundle_state State> class kernel_bundle;
35 template <backend BackendName, class SyclObjectT>
36 auto get_native(const SyclObjectT &Obj)
37  -> backend_return_t<BackendName, SyclObjectT>;
38 
39 namespace detail {
40 class kernel_impl;
41 
44 class auto_name {};
45 
49 template <typename Name, typename Type> struct get_kernel_name_t {
50  using name = Name;
51 };
52 
58 template <typename Type> struct get_kernel_name_t<detail::auto_name, Type> {
59  using name = Type;
60 };
61 
62 } // namespace detail
63 
71 class __SYCL_EXPORT kernel : public detail::OwnerLessBase<kernel> {
72 public:
80 #ifdef __SYCL_INTERNAL_API
81  kernel(cl_kernel ClKernel, const context &SyclContext);
82 #endif
83 
84  kernel(const kernel &RHS) = default;
85 
86  kernel(kernel &&RHS) = default;
87 
88  kernel &operator=(const kernel &RHS) = default;
89 
90  kernel &operator=(kernel &&RHS) = default;
91 
92  bool operator==(const kernel &RHS) const { return impl == RHS.impl; }
93 
94  bool operator!=(const kernel &RHS) const { return !operator==(RHS); }
95 
102 #ifdef __SYCL_INTERNAL_API
103  cl_kernel get() const;
104 #endif
105 
112  context get_context() const;
113 
117  backend get_backend() const noexcept;
118 
122  kernel_bundle<bundle_state::executable> get_kernel_bundle() const;
123 
128  template <typename Param>
129  typename detail::is_kernel_info_desc<Param>::return_type get_info() const {
130  return detail::convert_from_abi_neutral(get_info_impl<Param>());
131  }
132 
136  template <typename Param>
138  get_backend_info() const;
139 
145  template <typename Param>
147  get_info(const device &Device) const;
148 
156  template <typename Param>
157  __SYCL2020_DEPRECATED("Use the overload without the second parameter")
158  typename detail::is_kernel_device_specific_info_desc<Param>::return_type
159  get_info(const device &Device, const range<3> &WGSize) const;
160 
161  // TODO: Revisit and align with sycl_ext_oneapi_forward_progress extension
162  // once #7598 is merged.
163  template <typename Param>
164  typename Param::return_type ext_oneapi_get_info(const queue &q) const;
165 
166 private:
168  kernel(std::shared_ptr<detail::kernel_impl> Impl);
169 
170  pi_native_handle getNative() const;
171 
172  __SYCL_DEPRECATED("Use getNative() member function")
173  pi_native_handle getNativeImpl() const;
174 
175  std::shared_ptr<detail::kernel_impl> impl;
176 
177  template <class Obj>
178  friend const decltype(Obj::impl) &
179  detail::getSyclObjImpl(const Obj &SyclObject);
180  template <class T>
181  friend T detail::createSyclObjFromImpl(decltype(T::impl) ImplObj);
182  template <backend BackendName, class SyclObjectT>
183  friend auto get_native(const SyclObjectT &Obj)
184  -> backend_return_t<BackendName, SyclObjectT>;
185  template <typename Param>
186  typename detail::ABINeutralT_t<
187  typename detail::is_kernel_info_desc<Param>::return_type>
188  get_info_impl() const;
189 };
190 } // namespace _V1
191 } // namespace sycl
192 
193 namespace std {
194 template <> struct hash<sycl::kernel> {
195  size_t operator()(const sycl::kernel &Kernel) const {
196  return hash<std::shared_ptr<sycl::detail::kernel_impl>>()(
198  }
199 };
200 } // 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:44
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:71
bool operator==(const kernel &RHS) const
Definition: kernel.hpp:92
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
bool operator!=(const kernel &RHS) const
Definition: kernel.hpp:94
kernel & operator=(kernel &&RHS)=default
Encapsulates a single SYCL queue which schedules kernels on a SYCL device.
Definition: queue.hpp:110
Defines the iteration domain of either a single work-group in a parallel dispatch,...
Definition: range.hpp:26
decltype(Obj::impl) const & getSyclObjImpl(const Obj &SyclObject)
Definition: impl_utils.hpp:31
auto convert_from_abi_neutral(ParamT &&Info)
Definition: platform.hpp:74
T createSyclObjFromImpl(decltype(T::impl) ImplObj)
Definition: impl_utils.hpp:40
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:130
typename backend_traits< Backend >::template return_type< SyclType > backend_return_t
Definition: backend.hpp:81
Definition: access.hpp:18
uintptr_t pi_native_handle
Definition: pi.h:256
_Abi const simd< _Tp, _Abi > & noexcept
Definition: simd.hpp:1324
size_t operator()(const sycl::kernel &Kernel) const
Definition: kernel.hpp:195
Helper struct to get a kernel name type based on given Name and Type types: if Name is undefined (is ...
Definition: kernel.hpp:49