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