DPC++ Runtime
Runtime libraries for oneAPI DPC++
interop_handle.hpp
Go to the documentation of this file.
1 //==------------ interop_handle.hpp --- SYCL interop handle ----------------==//
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/access/access.hpp> // for target, mode, place...
12 #include <sycl/accessor.hpp> // for AccessorBaseHost
13 #include <sycl/backend_types.hpp> // for backend, backend_re...
14 #include <sycl/context.hpp> // for context
15 #include <sycl/detail/export.hpp> // for __SYCL_EXPORT
16 #include <sycl/detail/helpers.hpp> // for context_impl
17 #include <sycl/detail/impl_utils.hpp> // for getSyclObjImpl
18 #include <sycl/detail/pi.h> // for _pi_mem, pi_native_...
19 #include <sycl/device.hpp> // for device, device_impl
20 #include <sycl/exception.hpp> // for invalid_object_error
21 #include <sycl/exception_list.hpp> // for queue_impl
22 #include <sycl/ext/oneapi/accessor_property_list.hpp> // for accessor_property_list
23 #include <sycl/image.hpp> // for image
24 #include <sycl/properties/buffer_properties.hpp> // for buffer
25 
26 #include <memory> // for shared_ptr
27 #include <stdint.h> // for int32_t
28 #include <type_traits> // for enable_if_t
29 #include <utility> // for move, pair
30 #include <vector> // for vector
31 
32 namespace sycl {
33 inline namespace _V1 {
34 
35 namespace detail {
36 class AccessorBaseHost;
37 class ExecCGCommand;
38 class DispatchHostTask;
39 class queue_impl;
40 class device_impl;
41 class context_impl;
42 } // namespace detail
43 
44 class queue;
45 class device;
46 class context;
47 
49 public:
50  interop_handle() = delete;
51 
54  __SYCL_EXPORT backend get_backend() const noexcept;
55 
62  template <backend Backend = backend::opencl, typename DataT, int Dims,
63  access::mode Mode, access::target Target, access::placeholder IsPlh,
64  typename PropertyListT = ext::oneapi::accessor_property_list<>>
65  std::enable_if_t<Target != access::target::image,
66  backend_return_t<Backend, buffer<DataT, Dims>>>
67  get_native_mem(const accessor<DataT, Dims, Mode, Target, IsPlh, PropertyListT>
68  &Acc) const {
69  static_assert(Target == access::target::device ||
70  Target == access::target::constant_buffer,
71  "The method is available only for target::device accessors");
72 #ifndef __SYCL_DEVICE_ONLY__
73  if (Backend != get_backend())
74  throw invalid_object_error("Incorrect backend argument was passed",
75  PI_ERROR_INVALID_MEM_OBJECT);
76  const auto *AccBase = static_cast<const detail::AccessorBaseHost *>(&Acc);
77  return getMemImpl<Backend, DataT, Dims>(
78  detail::getSyclObjImpl(*AccBase).get());
79 #else
80  (void)Acc;
81  // we believe this won't be ever called on device side
83 #endif
84  }
85 
92  template <backend Backend = backend::opencl, typename DataT, int Dims,
95  const detail::image_accessor<DataT, Dims, Mode,
96  /*access::target::image*/ Target,
97  IsPlh /*, PropertyListT */> &Acc) const {
98 #ifndef __SYCL_DEVICE_ONLY__
99  if (Backend != get_backend())
100  throw invalid_object_error("Incorrect backend argument was passed",
101  PI_ERROR_INVALID_MEM_OBJECT);
102  const auto *AccBase = static_cast<const detail::AccessorBaseHost *>(&Acc);
103  return getMemImpl<Backend, Dims>(detail::getSyclObjImpl(*AccBase).get());
104 #else
105  (void)Acc;
106  // we believe this won't be ever called on device side
108 #endif
109  }
110 
123  template <backend Backend = backend::opencl>
125 #ifndef __SYCL_DEVICE_ONLY__
126  // TODO: replace the exception thrown below with the SYCL 2020 exception
127  // with the error code 'errc::backend_mismatch' when those new exceptions
128  // are ready to be used.
129  if (Backend != get_backend())
130  throw invalid_object_error("Incorrect backend argument was passed",
131  PI_ERROR_INVALID_MEM_OBJECT);
132  int32_t NativeHandleDesc;
133  return reinterpret_cast<backend_return_t<Backend, queue>>(
134  getNativeQueue(NativeHandleDesc));
135 #else
136  // we believe this won't be ever called on device side
137  return 0;
138 #endif
139  }
140 
146  template <backend Backend = backend::opencl>
148 #ifndef __SYCL_DEVICE_ONLY__
149  // TODO: replace the exception thrown below with the SYCL 2020 exception
150  // with the error code 'errc::backend_mismatch' when those new exceptions
151  // are ready to be used.
152  if (Backend != get_backend())
153  throw invalid_object_error("Incorrect backend argument was passed",
154  PI_ERROR_INVALID_MEM_OBJECT);
155  // C-style cast required to allow various native types
156  return (backend_return_t<Backend, device>)getNativeDevice();
157 #else
158  // we believe this won't be ever called on device side
159  return 0;
160 #endif
161  }
162 
168  template <backend Backend = backend::opencl>
170 #ifndef __SYCL_DEVICE_ONLY__
171  // TODO: replace the exception thrown below with the SYCL 2020 exception
172  // with the error code 'errc::backend_mismatch' when those new exceptions
173  // are ready to be used.
174  if (Backend != get_backend())
175  throw invalid_object_error("Incorrect backend argument was passed",
176  PI_ERROR_INVALID_MEM_OBJECT);
177  return reinterpret_cast<backend_return_t<Backend, context>>(
178  getNativeContext());
179 #else
180  // we believe this won't be ever called on device side
181  return 0;
182 #endif
183  }
184 
185 private:
186  friend class detail::ExecCGCommand;
188  using ReqToMem = std::pair<detail::AccessorImplHost *, pi_mem>;
189 
190  interop_handle(std::vector<ReqToMem> MemObjs,
191  const std::shared_ptr<detail::queue_impl> &Queue,
192  const std::shared_ptr<detail::device_impl> &Device,
193  const std::shared_ptr<detail::context_impl> &Context)
194  : MQueue(Queue), MDevice(Device), MContext(Context),
195  MMemObjs(std::move(MemObjs)) {}
196 
197  template <backend Backend, typename DataT, int Dims>
199  getMemImpl(detail::AccessorImplHost *Req) const {
200  std::vector<pi_native_handle> NativeHandles{getNativeMem(Req)};
202  NativeHandles);
203  }
204 
205  template <backend Backend, int Dims>
206  backend_return_t<Backend, image<Dims>>
207  getMemImpl(detail::AccessorImplHost *Req) const {
208  using image_return_t = backend_return_t<Backend, image<Dims>>;
209  return reinterpret_cast<image_return_t>(getNativeMem(Req));
210  }
211 
212  __SYCL_EXPORT pi_native_handle
213  getNativeMem(detail::AccessorImplHost *Req) const;
214  __SYCL_EXPORT pi_native_handle
215  getNativeQueue(int32_t &NativeHandleDesc) const;
216  __SYCL_EXPORT pi_native_handle getNativeDevice() const;
217  __SYCL_EXPORT pi_native_handle getNativeContext() const;
218 
219  std::shared_ptr<detail::queue_impl> MQueue;
220  std::shared_ptr<detail::device_impl> MDevice;
221  std::shared_ptr<detail::context_impl> MContext;
222 
223  std::vector<ReqToMem> MMemObjs;
224 };
225 
226 } // namespace _V1
227 } // namespace sycl
The file contains implementations of accessor class.
Defines a shared array that can be used by kernels in queues.
Definition: buffer.hpp:169
The exec CG command enqueues execution of kernel or explicit memory operation.
Definition: commands.hpp:647
Defines a shared image data.
Definition: image.hpp:443
backend_return_t< Backend, queue > get_native_queue() const
Returns an underlying native backend object associated with teh queue that the host task was submitte...
std::enable_if_t< Target !=access::target::image, backend_return_t< Backend, buffer< DataT, Dims > > > get_native_mem(const accessor< DataT, Dims, Mode, Target, IsPlh, PropertyListT > &Acc) const
Receives a SYCL accessor that has been defined as a requirement for the command group,...
backend get_backend() const noexcept
Returns a backend associated with the queue associated with this interop_handle.
backend_return_t< Backend, context > get_native_context() const
Returns the SYCL application interoperability native backend object associated with the context assoc...
backend_return_t< Backend, image< Dims > > get_native_mem(const detail::image_accessor< DataT, Dims, Mode, Target, IsPlh > &Acc) const
Receives a SYCL accessor that has been defined as a requirement for the command group,...
backend_return_t< Backend, device > get_native_device() const
Returns the SYCL application interoperability native backend object associated with the device associ...
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: impl_utils.hpp:30
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:209
_Abi const simd< _Tp, _Abi > & noexcept
Definition: simd.hpp:1324
static ReturnType GetNativeObjs(const std::vector< pi_native_handle > &Handle)
Definition: backend.hpp:95