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>
12 #include <sycl/accessor.hpp>
13 #include <sycl/backend_types.hpp>
14 #include <sycl/detail/common.hpp>
15 #include <sycl/detail/defines.hpp>
16 #include <sycl/detail/pi.hpp>
17 
18 #include <memory>
19 
20 namespace sycl {
22 
23 namespace detail {
24 class AccessorBaseHost;
25 class ExecCGCommand;
26 class DispatchHostTask;
27 class queue_impl;
28 class device_impl;
29 class context_impl;
30 } // namespace detail
31 
32 class queue;
33 class device;
34 class context;
35 
37 public:
38  interop_handle() = delete;
39 
42  __SYCL_EXPORT backend get_backend() const noexcept;
43 
50  template <backend Backend = backend::opencl, typename DataT, int Dims,
52  typename PropertyListT = ext::oneapi::accessor_property_list<>>
55  const {
56  static_assert(Target == access::target::device ||
57  Target == access::target::constant_buffer,
58  "The method is available only for target::device accessors");
59 #ifndef __SYCL_DEVICE_ONLY__
60  if (Backend != get_backend())
61  throw invalid_object_error("Incorrect backend argument was passed",
62  PI_ERROR_INVALID_MEM_OBJECT);
63  const auto *AccBase = static_cast<const detail::AccessorBaseHost *>(&Acc);
64  return getMemImpl<Backend, DataT, Dims>(
65  detail::getSyclObjImpl(*AccBase).get());
66 #else
67  (void)Acc;
68  // we believe this won't be ever called on device side
70 #endif
71  }
72 
85  template <backend Backend = backend::opencl>
87 #ifndef __SYCL_DEVICE_ONLY__
88  // TODO: replace the exception thrown below with the SYCL 2020 exception
89  // with the error code 'errc::backend_mismatch' when those new exceptions
90  // are ready to be used.
91  if (Backend != get_backend())
92  throw invalid_object_error("Incorrect backend argument was passed",
93  PI_ERROR_INVALID_MEM_OBJECT);
94  return reinterpret_cast<backend_return_t<Backend, queue>>(getNativeQueue());
95 #else
96  // we believe this won't be ever called on device side
97  return 0;
98 #endif
99  }
100 
106  template <backend Backend = backend::opencl>
108 #ifndef __SYCL_DEVICE_ONLY__
109  // TODO: replace the exception thrown below with the SYCL 2020 exception
110  // with the error code 'errc::backend_mismatch' when those new exceptions
111  // are ready to be used.
112  if (Backend != get_backend())
113  throw invalid_object_error("Incorrect backend argument was passed",
114  PI_ERROR_INVALID_MEM_OBJECT);
115  // C-style cast required to allow various native types
116  return (backend_return_t<Backend, device>)getNativeDevice();
117 #else
118  // we believe this won't be ever called on device side
119  return 0;
120 #endif
121  }
122 
128  template <backend Backend = backend::opencl>
130 #ifndef __SYCL_DEVICE_ONLY__
131  // TODO: replace the exception thrown below with the SYCL 2020 exception
132  // with the error code 'errc::backend_mismatch' when those new exceptions
133  // are ready to be used.
134  if (Backend != get_backend())
135  throw invalid_object_error("Incorrect backend argument was passed",
136  PI_ERROR_INVALID_MEM_OBJECT);
137  return reinterpret_cast<backend_return_t<Backend, context>>(
138  getNativeContext());
139 #else
140  // we believe this won't be ever called on device side
141  return 0;
142 #endif
143  }
144 
145 private:
146  friend class detail::ExecCGCommand;
148  using ReqToMem = std::pair<detail::AccessorImplHost *, pi_mem>;
149 
150  interop_handle(std::vector<ReqToMem> MemObjs,
151  const std::shared_ptr<detail::queue_impl> &Queue,
152  const std::shared_ptr<detail::device_impl> &Device,
153  const std::shared_ptr<detail::context_impl> &Context)
154  : MQueue(Queue), MDevice(Device), MContext(Context),
155  MMemObjs(std::move(MemObjs)) {}
156 
157  template <backend Backend, typename DataT, int Dims>
159  getMemImpl(detail::AccessorImplHost *Req) const {
160  std::vector<pi_native_handle> NativeHandles{getNativeMem(Req)};
161  return detail::BufferInterop<Backend, DataT, Dims>::GetNativeObjs(
162  NativeHandles);
163  }
164 
165  __SYCL_EXPORT pi_native_handle
166  getNativeMem(detail::AccessorImplHost *Req) const;
167  __SYCL_EXPORT pi_native_handle getNativeQueue() const;
168  __SYCL_EXPORT pi_native_handle getNativeDevice() const;
169  __SYCL_EXPORT pi_native_handle getNativeContext() const;
170 
171  std::shared_ptr<detail::queue_impl> MQueue;
172  std::shared_ptr<detail::device_impl> MDevice;
173  std::shared_ptr<detail::context_impl> MContext;
174 
175  std::vector<ReqToMem> MMemObjs;
176 };
177 
178 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
179 } // namespace sycl
sycl::_V1::interop_handle::get_native_context
backend_return_t< Backend, context > get_native_context() const
Returns the SYCL application interoperability native backend object associated with the context assoc...
Definition: interop_handle.hpp:129
sycl::_V1::access::mode
mode
Definition: access.hpp:30
sycl::_V1::backend
backend
Definition: backend_types.hpp:21
sycl::_V1::detail::AccessorImplHost
Definition: accessor_impl.hpp:42
__SYCL_INLINE_VER_NAMESPACE
#define __SYCL_INLINE_VER_NAMESPACE(X)
Definition: defines_elementary.hpp:11
sycl::_V1::detail::DispatchHostTask
Definition: commands.cpp:296
sycl
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
access.hpp
pi.hpp
sycl::_V1::access::placeholder
placeholder
Definition: access.hpp:45
sycl::_V1::interop_handle::get_native_device
backend_return_t< Backend, device > get_native_device() const
Returns the SYCL application interoperability native backend object associated with the device associ...
Definition: interop_handle.hpp:107
defines.hpp
common.hpp
sycl::_V1::backend_return_t
typename backend_traits< Backend >::template return_type< SyclType > backend_return_t
Definition: backend.hpp:72
sycl::_V1::ext::oneapi::accessor_property_list
Definition: property_list.hpp:18
USMAllocationForceResidencyType::Device
@ Device
sycl::_V1::access::target
target
Definition: access.hpp:18
sycl::_V1::access::target::device
@ device
pi_native_handle
uintptr_t pi_native_handle
Definition: pi.h:133
sycl::_V1::accessor
Definition: accessor.hpp:225
sycl::_V1::detail::ExecCGCommand
The exec CG command enqueues execution of kernel or explicit memory operation.
Definition: commands.hpp:601
accessor.hpp
sycl::_V1::interop_handle::get_native_queue
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...
Definition: interop_handle.hpp:86
backend_types.hpp
std
Definition: accessor.hpp:3230
sycl::_V1::interop_handle
Definition: interop_handle.hpp:36
sycl::_V1::interop_handle::get_native_mem
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,...
Definition: interop_handle.hpp:53
sycl::_V1::detail::getSyclObjImpl
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: common.hpp:300
sycl::_V1::detail::AccessorBaseHost
Definition: accessor.hpp:466