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 
12 #include <CL/sycl/accessor.hpp>
17 #include <CL/sycl/detail/pi.hpp>
18 
19 #include <memory>
20 
22 namespace sycl {
23 
24 namespace detail {
25 class AccessorBaseHost;
26 class ExecCGCommand;
27 class DispatchHostTask;
28 class queue_impl;
29 class device_impl;
30 class context_impl;
31 } // namespace detail
32 
33 class queue;
34 class device;
35 class context;
36 
38 public:
39  interop_handle() = delete;
40 
43  __SYCL_EXPORT backend get_backend() const noexcept;
44 
51  template <backend Backend = backend::opencl, typename DataT, int Dims,
53  typename PropertyListT = ext::oneapi::accessor_property_list<>>
56  const {
57  static_assert(Target == access::target::device ||
58  Target == access::target::constant_buffer,
59  "The method is available only for target::device accessors");
60 #ifndef __SYCL_DEVICE_ONLY__
61  if (Backend != get_backend())
62  throw invalid_object_error("Incorrect backend argument was passed",
63  PI_ERROR_INVALID_MEM_OBJECT);
64  const auto *AccBase = static_cast<const detail::AccessorBaseHost *>(&Acc);
65  return getMemImpl<Backend, DataT, Dims>(
66  detail::getSyclObjImpl(*AccBase).get());
67 #else
68  (void)Acc;
69  // we believe this won't be ever called on device side
71 #endif
72  }
73 
86  template <backend Backend = backend::opencl>
88 #ifndef __SYCL_DEVICE_ONLY__
89  // TODO: replace the exception thrown below with the SYCL 2020 exception
90  // with the error code 'errc::backend_mismatch' when those new exceptions
91  // are ready to be used.
92  if (Backend != get_backend())
93  throw invalid_object_error("Incorrect backend argument was passed",
94  PI_ERROR_INVALID_MEM_OBJECT);
95  return reinterpret_cast<backend_return_t<Backend, queue>>(getNativeQueue());
96 #else
97  // we believe this won't be ever called on device side
98  return 0;
99 #endif
100  }
101 
107  template <backend Backend = backend::opencl>
109 #ifndef __SYCL_DEVICE_ONLY__
110  // TODO: replace the exception thrown below with the SYCL 2020 exception
111  // with the error code 'errc::backend_mismatch' when those new exceptions
112  // are ready to be used.
113  if (Backend != get_backend())
114  throw invalid_object_error("Incorrect backend argument was passed",
115  PI_ERROR_INVALID_MEM_OBJECT);
116  // C-style cast required to allow various native types
117  return (backend_return_t<Backend, device>)getNativeDevice();
118 #else
119  // we believe this won't be ever called on device side
120  return 0;
121 #endif
122  }
123 
129  template <backend Backend = backend::opencl>
131 #ifndef __SYCL_DEVICE_ONLY__
132  // TODO: replace the exception thrown below with the SYCL 2020 exception
133  // with the error code 'errc::backend_mismatch' when those new exceptions
134  // are ready to be used.
135  if (Backend != get_backend())
136  throw invalid_object_error("Incorrect backend argument was passed",
137  PI_ERROR_INVALID_MEM_OBJECT);
138  return reinterpret_cast<backend_return_t<Backend, context>>(
139  getNativeContext());
140 #else
141  // we believe this won't be ever called on device side
142  return 0;
143 #endif
144  }
145 
146 private:
147  friend class detail::ExecCGCommand;
149  using ReqToMem = std::pair<detail::Requirement *, pi_mem>;
150 
151  interop_handle(std::vector<ReqToMem> MemObjs,
152  const std::shared_ptr<detail::queue_impl> &Queue,
153  const std::shared_ptr<detail::device_impl> &Device,
154  const std::shared_ptr<detail::context_impl> &Context)
155  : MQueue(Queue), MDevice(Device), MContext(Context),
156  MMemObjs(std::move(MemObjs)) {}
157 
158  template <backend Backend, typename DataT, int Dims>
160  getMemImpl(detail::Requirement *Req) const {
161  std::vector<pi_native_handle> NativeHandles{getNativeMem(Req)};
162  return detail::BufferInterop<Backend, DataT, Dims>::GetNativeObjs(
163  NativeHandles);
164  }
165 
166  __SYCL_EXPORT pi_native_handle getNativeMem(detail::Requirement *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 } // namespace sycl
179 } // __SYCL_INLINE_NAMESPACE(cl)
cl::sycl::backend
backend
Definition: backend_types.hpp:21
cl::sycl::detail::ExecCGCommand
The exec CG command enqueues execution of kernel or explicit memory operation.
Definition: commands.hpp:539
cl::sycl::backend_return_t
typename backend_traits< Backend >::template return_type< SyclType > backend_return_t
Definition: backend.hpp:72
cl::sycl::interop_handle
Definition: interop_handle.hpp:37
cl::sycl::access::placeholder
placeholder
Definition: access.hpp:43
cl::sycl::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:130
cl::sycl::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:87
sycl
Definition: invoke_simd.hpp:68
access.hpp
pi.hpp
cl::sycl::detail::Requirement
AccessorImplHost Requirement
Definition: accessor_impl.hpp:218
cl::sycl::info::queue
queue
Definition: info_desc.hpp:229
cl::sycl::detail::AccessorImplHost
Definition: accessor_impl.hpp:74
cl::sycl::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:54
cl::sycl::accessor
Buffer accessor.
Definition: accessor.hpp:224
cl::sycl::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:108
defines.hpp
cl::sycl::access::target
target
Definition: access.hpp:17
Device
@ Device
Definition: usm_allocator.hpp:14
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
pi_native_handle
uintptr_t pi_native_handle
Definition: pi.h:98
cl::sycl::ext::oneapi::accessor_property_list
Objects of the accessor_property_list class are containers for the SYCL properties.
Definition: property_list.hpp:19
accessor_impl.hpp
accessor.hpp
cl::sycl::info::context
context
Definition: info_desc.hpp:42
cl::sycl::detail::getSyclObjImpl
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: common.hpp:204
backend_types.hpp
cl::sycl::detail::DispatchHostTask
Definition: commands.cpp:224
std
Definition: accessor.hpp:2617
cl::sycl::detail::AccessorBaseHost
Definition: accessor_impl.hpp:132
common.hpp
cl::sycl::access::mode
mode
Definition: access.hpp:28
cl::sycl::info::device
device
Definition: info_desc.hpp:53
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12