DPC++ Runtime
Runtime libraries for oneAPI Data Parallel C++
backend.hpp
Go to the documentation of this file.
1 //==---------------- backend.hpp - SYCL PI backends ------------------------==//
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 <CL/sycl/accessor.hpp>
12 #include <CL/sycl/backend.hpp>
14 #include <CL/sycl/buffer.hpp>
15 #include <CL/sycl/context.hpp>
19 #include <CL/sycl/detail/pi.h>
20 #include <CL/sycl/detail/pi.hpp>
21 #include <CL/sycl/device.hpp>
22 #include <CL/sycl/event.hpp>
23 #include <CL/sycl/exception.hpp>
25 #include <CL/sycl/platform.hpp>
26 #include <CL/sycl/queue.hpp>
27 
28 #include <type_traits>
29 
31 namespace sycl {
32 
33 namespace detail {
34 template <backend Backend, typename T> struct BackendInput {
35  // TODO replace usage of interop with specializations.
36  using type = typename interop<Backend, T>::type;
37 };
38 
39 template <backend Backend, typename T> struct BackendReturn {
40  // TODO replace usage of interop with specializations.
41  using type = typename interop<Backend, T>::type;
42 };
43 
44 // TODO each backend can have its own custom errc enumeration
45 // but the details for this are not fully specified yet
46 enum class backend_errc : unsigned int {};
47 } // namespace detail
48 
49 template <backend Backend> class backend_traits {
50 public:
51  template <class T>
53 
54  template <class T>
56 
58 };
59 
60 template <backend Backend, typename SyclType>
61 using backend_input_t =
62  typename backend_traits<Backend>::template input_type<SyclType>;
63 
64 template <backend Backend, typename SyclType>
65 using backend_return_t =
66  typename backend_traits<Backend>::template return_type<SyclType>;
67 
68 template <backend BackendName, class SyclObjectT>
69 auto get_native(const SyclObjectT &Obj)
71  // TODO use SYCL 2020 exception when implemented
72  if (Obj.get_backend() != BackendName)
73  throw runtime_error("Backends mismatch", PI_INVALID_OPERATION);
74  return Obj.template get_native<BackendName>();
75 }
76 
77 // Native handle of an accessor should be accessed through interop_handler
78 template <backend BackendName, typename DataT, int Dimensions,
79  access::mode AccessMode, access::target AccessTarget,
80  access::placeholder IsPlaceholder>
81 auto get_native(const accessor<DataT, Dimensions, AccessMode, AccessTarget,
82  IsPlaceholder> &Obj) ->
83  typename interop<BackendName, accessor<DataT, Dimensions, AccessMode,
84  AccessTarget, IsPlaceholder>>::type =
85  delete;
86 
87 namespace detail {
88 // Forward declaration
89 class kernel_bundle_impl;
90 
91 __SYCL_EXPORT platform make_platform(pi_native_handle NativeHandle,
92  backend Backend);
93 __SYCL_EXPORT device make_device(pi_native_handle NativeHandle,
94  backend Backend);
95 __SYCL_EXPORT context make_context(pi_native_handle NativeHandle,
96  const async_handler &Handler,
97  backend Backend);
98 __SYCL_EXPORT queue make_queue(pi_native_handle NativeHandle,
99  const context &TargetContext, bool KeepOwnership,
100  const async_handler &Handler, backend Backend);
101 __SYCL_EXPORT queue make_queue(pi_native_handle NativeHandle,
102  const context &TargetContext,
103  const async_handler &Handler, backend Backend);
104 __SYCL_EXPORT event make_event(pi_native_handle NativeHandle,
105  const context &TargetContext, backend Backend);
106 __SYCL_EXPORT event make_event(pi_native_handle NativeHandle,
107  const context &TargetContext, bool KeepOwnership,
108  backend Backend);
109 __SYCL_EXPORT kernel make_kernel(pi_native_handle NativeHandle,
110  const context &TargetContext, backend Backend);
111 // TODO: Unused. Remove when allowed.
112 __SYCL_EXPORT std::shared_ptr<detail::kernel_bundle_impl>
113 make_kernel_bundle(pi_native_handle NativeHandle, const context &TargetContext,
114  bundle_state State, backend Backend);
115 __SYCL_EXPORT std::shared_ptr<detail::kernel_bundle_impl>
116 make_kernel_bundle(pi_native_handle NativeHandle, const context &TargetContext,
117  bool KeepOwnership, bundle_state State, backend Backend);
118 } // namespace detail
119 
120 template <backend Backend>
121 typename std::enable_if<
122  detail::InteropFeatureSupportMap<Backend>::MakePlatform == true,
125  const typename backend_traits<Backend>::template input_type<platform>
126  &BackendObject) {
127  return detail::make_platform(
128  detail::pi::cast<pi_native_handle>(BackendObject), Backend);
129 }
130 
131 template <backend Backend>
132 typename std::enable_if<
133  detail::InteropFeatureSupportMap<Backend>::MakeDevice == true, device>::type
134 make_device(const typename backend_traits<Backend>::template input_type<device>
135  &BackendObject) {
136  return detail::make_device(detail::pi::cast<pi_native_handle>(BackendObject),
137  Backend);
138 }
139 
140 template <backend Backend>
141 typename std::enable_if<
142  detail::InteropFeatureSupportMap<Backend>::MakeContext == true,
145  const typename backend_traits<Backend>::template input_type<context>
146  &BackendObject,
147  const async_handler &Handler = {}) {
148  return detail::make_context(detail::pi::cast<pi_native_handle>(BackendObject),
149  Handler, Backend);
150 }
151 
152 template <backend Backend>
153 __SYCL_DEPRECATED("Use SYCL 2020 sycl::make_queue free function")
154 typename std::enable_if<
155  detail::InteropFeatureSupportMap<Backend>::MakeQueue == true, queue>::type
157  const typename backend_traits<Backend>::template input_type<queue>
158  &BackendObject,
159  const context &TargetContext, bool KeepOwnership,
160  const async_handler Handler = {}) {
161  return detail::make_queue(detail::pi::cast<pi_native_handle>(BackendObject),
162  TargetContext, KeepOwnership, Handler, Backend);
163 }
164 
165 template <backend Backend>
166 typename std::enable_if<
167  detail::InteropFeatureSupportMap<Backend>::MakeQueue == true, queue>::type
168 make_queue(const typename backend_traits<Backend>::template input_type<queue>
169  &BackendObject,
170  const context &TargetContext, const async_handler Handler = {}) {
171  return detail::make_queue(detail::pi::cast<pi_native_handle>(BackendObject),
172  TargetContext, false, Handler, Backend);
173 }
174 
175 template <backend Backend>
176 typename std::enable_if<
177  detail::InteropFeatureSupportMap<Backend>::MakeEvent == true, event>::type
178 make_event(const typename backend_traits<Backend>::template input_type<event>
179  &BackendObject,
180  const context &TargetContext) {
181  return detail::make_event(detail::pi::cast<pi_native_handle>(BackendObject),
182  TargetContext, Backend);
183 }
184 
185 template <backend Backend>
186 __SYCL_DEPRECATED("Use SYCL 2020 sycl::make_event free function")
187 typename std::enable_if<
188  detail::InteropFeatureSupportMap<Backend>::MakeEvent == true, event>::type
190  const typename backend_traits<Backend>::template input_type<event>
191  &BackendObject,
192  const context &TargetContext, bool KeepOwnership) {
193  return detail::make_event(detail::pi::cast<pi_native_handle>(BackendObject),
194  TargetContext, KeepOwnership, Backend);
195 }
196 
197 template <backend Backend, typename T, int Dimensions = 1,
198  typename AllocatorT = buffer_allocator>
199 typename std::enable_if<detail::InteropFeatureSupportMap<Backend>::MakeBuffer ==
200  true,
201  buffer<T, Dimensions, AllocatorT>>::type
203  buffer<T, Dimensions, AllocatorT>> &BackendObject,
204  const context &TargetContext, event AvailableEvent = {}) {
205  return buffer<T, Dimensions, AllocatorT>(
206  reinterpret_cast<cl_mem>(BackendObject), TargetContext, AvailableEvent);
207 }
208 
209 template <backend Backend>
210 kernel
211 make_kernel(const typename backend_traits<Backend>::template input_type<kernel>
212  &BackendObject,
213  const context &TargetContext) {
214  return detail::make_kernel(detail::pi::cast<pi_native_handle>(BackendObject),
215  TargetContext, Backend);
216 }
217 
218 template <backend Backend, bundle_state State>
219 typename std::enable_if<
220  detail::InteropFeatureSupportMap<Backend>::MakeKernelBundle == true,
221  kernel_bundle<State>>::type
223  kernel_bundle<State>> &BackendObject,
224  const context &TargetContext) {
225  std::shared_ptr<detail::kernel_bundle_impl> KBImpl =
227  detail::pi::cast<pi_native_handle>(BackendObject), TargetContext,
228  false, State, Backend);
229  return detail::createSyclObjFromImpl<kernel_bundle<State>>(KBImpl);
230 }
231 } // namespace sycl
232 } // __SYCL_INLINE_NAMESPACE(cl)
cl::sycl::backend
backend
Definition: backend_types.hpp:21
cl::sycl::detail::BackendInput::type
typename interop< Backend, T >::type type
Definition: backend.hpp:36
cl::sycl::make_context
std::enable_if< detail::InteropFeatureSupportMap< Backend >::MakeContext==true, context >::type make_context(const typename backend_traits< Backend >::template input_type< context > &BackendObject, const async_handler &Handler={})
Definition: backend.hpp:144
pi.h
cl::sycl::kernel_bundle
The kernel_bundle class represents collection of device images in a particular state.
Definition: kernel.hpp:26
type
cl::sycl::make_queue
std::enable_if< detail::InteropFeatureSupportMap< Backend >::MakeQueue==true, queue >::type make_queue(const typename backend_traits< Backend >::template input_type< queue > &BackendObject, const context &TargetContext, const async_handler Handler={})
Definition: backend.hpp:168
cl::sycl::backend_return_t
typename backend_traits< Backend >::template return_type< SyclType > backend_return_t
Definition: backend.hpp:66
cl::sycl::info::device
device
Definition: info_desc.hpp:47
cl::sycl::event
An event object can be used to synchronize memory transfers, enqueues of kernels and signaling barrie...
Definition: event.hpp:31
PI_INVALID_OPERATION
@ PI_INVALID_OPERATION
Definition: pi.h:84
cl::sycl::make_event
std::enable_if< detail::InteropFeatureSupportMap< Backend >::MakeEvent==true, event >::type make_event(const typename backend_traits< Backend >::template input_type< event > &BackendObject, const context &TargetContext, bool KeepOwnership)
Definition: backend.hpp:189
cl::sycl::access::placeholder
placeholder
Definition: access.hpp:43
device.hpp
__SYCL_DEPRECATED
#define __SYCL_DEPRECATED(message)
Definition: defines_elementary.hpp:43
context.hpp
event.hpp
cl::sycl::info::kernel
kernel
Definition: info_desc.hpp:219
cl::sycl::backend_traits
Definition: backend.hpp:49
cl::sycl::detail::backend_errc
backend_errc
Definition: backend.hpp:46
cl::sycl::make_kernel
kernel make_kernel(const typename backend_traits< Backend >::template input_type< kernel > &BackendObject, const context &TargetContext)
Definition: backend.hpp:211
cl::sycl::queue
Encapsulates a single SYCL queue which schedules kernels on a SYCL device.
Definition: queue.hpp:99
pi.hpp
cl::sycl::buffer
Defines a shared array that can be used by kernels in queues.
Definition: buffer.hpp:38
cl::sycl::bundle_state
bundle_state
Definition: kernel_bundle_enums.hpp:14
cl::sycl::info::queue
queue
Definition: info_desc.hpp:212
backend_traits.hpp
cl::sycl::backend_traits::input_type
typename detail::BackendInput< Backend, T >::type input_type
Definition: backend.hpp:52
export.hpp
kernel_bundle.hpp
cl::sycl::backend_traits::return_type
typename detail::BackendReturn< Backend, T >::type return_type
Definition: backend.hpp:55
cl::sycl::access::target
target
Definition: access.hpp:17
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
queue.hpp
cl::sycl::interop
Definition: backend_types.hpp:33
pi_native_handle
uintptr_t pi_native_handle
Definition: pi.h:72
accessor.hpp
backend_types.hpp
cl::sycl::info::event
event
Definition: info_desc.hpp:267
platform.hpp
exception.hpp
std
Definition: accessor.hpp:2356
cl::sycl::backend_input_t
typename backend_traits< Backend >::template input_type< SyclType > backend_input_t
Definition: backend.hpp:62
backend.hpp
cl::sycl::make_kernel_bundle
std::enable_if< detail::InteropFeatureSupportMap< Backend >::MakeKernelBundle==true, kernel_bundle< State > >::type make_kernel_bundle(const typename backend_traits< Backend >::template input_type< kernel_bundle< State >> &BackendObject, const context &TargetContext)
Definition: backend.hpp:222
cl::sycl::context
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:35
cl::sycl::make_buffer
std::enable_if< detail::InteropFeatureSupportMap< Backend >::MakeBuffer==true, buffer< T, Dimensions, AllocatorT > >::type make_buffer(const typename backend_traits< Backend >::template input_type< buffer< T, Dimensions, AllocatorT >> &BackendObject, const context &TargetContext, event AvailableEvent={})
Definition: backend.hpp:202
buffer.hpp
common.hpp
cl::sycl::make_platform
std::enable_if< detail::InteropFeatureSupportMap< Backend >::MakePlatform==true, platform >::type make_platform(const typename backend_traits< Backend >::template input_type< platform > &BackendObject)
Definition: backend.hpp:124
cl::sycl::info::platform
platform
Definition: info_desc.hpp:27
cl::sycl::detail::BackendReturn::type
typename interop< Backend, T >::type type
Definition: backend.hpp:41
cl::sycl::access::mode
mode
Definition: access.hpp:28
cl::sycl::info::context
context
Definition: info_desc.hpp:38
cl::sycl::get_native
auto get_native(const accessor< DataT, Dimensions, AccessMode, AccessTarget, IsPlaceholder > &Obj) -> typename interop< BackendName, accessor< DataT, Dimensions, AccessMode, AccessTarget, IsPlaceholder >>::type=delete
cl::sycl::async_handler
std::function< void(cl::sycl::exception_list)> async_handler
Definition: exception_list.hpp:53
cl::sycl::buffer_allocator
detail::sycl_memory_object_allocator buffer_allocator
Definition: buffer_impl.hpp:38
cl::sycl::detail::BackendReturn
Definition: backend.hpp:39
cl::sycl::detail::BackendInput
Definition: backend.hpp:34
cl::sycl::make_device
std::enable_if< detail::InteropFeatureSupportMap< Backend >::MakeDevice==true, device >::type make_device(const typename backend_traits< Backend >::template input_type< device > &BackendObject)
Definition: backend.hpp:134
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12