DPC++ Runtime
Runtime libraries for oneAPI Data Parallel C++
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,
55  static_assert(Target == access::target::device ||
56  Target == access::target::constant_buffer,
57  "The method is available only for target::device accessors");
58 #ifndef __SYCL_DEVICE_ONLY__
59  if (Backend != get_backend())
60  throw invalid_object_error("Incorrect backend argument was passed",
62  const auto *AccBase = static_cast<const detail::AccessorBaseHost *>(&Acc);
63  return getMemImpl<Backend, DataT, Dims>(
64  detail::getSyclObjImpl(*AccBase).get());
65 #else
66  (void)Acc;
67  // we believe this won't be ever called on device side
68  return 0;
69 #endif
70  }
71 
84  template <backend Backend = backend::opencl>
86 #ifndef __SYCL_DEVICE_ONLY__
87  // TODO: replace the exception thrown below with the SYCL 2020 exception
88  // with the error code 'errc::backend_mismatch' when those new exceptions
89  // are ready to be used.
90  if (Backend != get_backend())
91  throw invalid_object_error("Incorrect backend argument was passed",
93  return reinterpret_cast<backend_return_t<Backend, queue>>(getNativeQueue());
94 #else
95  // we believe this won't be ever called on device side
96  return 0;
97 #endif
98  }
99 
105  template <backend Backend = backend::opencl>
107 #ifndef __SYCL_DEVICE_ONLY__
108  // TODO: replace the exception thrown below with the SYCL 2020 exception
109  // with the error code 'errc::backend_mismatch' when those new exceptions
110  // are ready to be used.
111  if (Backend != get_backend())
112  throw invalid_object_error("Incorrect backend argument was passed",
114  // C-style cast required to allow various native types
115  return (backend_return_t<Backend, device>)getNativeDevice();
116 #else
117  // we believe this won't be ever called on device side
118  return 0;
119 #endif
120  }
121 
127  template <backend Backend = backend::opencl>
129 #ifndef __SYCL_DEVICE_ONLY__
130  // TODO: replace the exception thrown below with the SYCL 2020 exception
131  // with the error code 'errc::backend_mismatch' when those new exceptions
132  // are ready to be used.
133  if (Backend != get_backend())
134  throw invalid_object_error("Incorrect backend argument was passed",
136  return reinterpret_cast<backend_return_t<Backend, context>>(
137  getNativeContext());
138 #else
139  // we believe this won't be ever called on device side
140  return 0;
141 #endif
142  }
143 
144 private:
145  friend class detail::ExecCGCommand;
147  using ReqToMem = std::pair<detail::Requirement *, pi_mem>;
148 
149  interop_handle(std::vector<ReqToMem> MemObjs,
150  const std::shared_ptr<detail::queue_impl> &Queue,
151  const std::shared_ptr<detail::device_impl> &Device,
152  const std::shared_ptr<detail::context_impl> &Context)
153  : MQueue(Queue), MDevice(Device), MContext(Context),
154  MMemObjs(std::move(MemObjs)) {}
155 
156  template <backend Backend, typename DataT, int Dims>
158  getMemImpl(detail::Requirement *Req) const {
159  /*
160  Do not update this cast: a C-style cast is required here.
161 
162  This function tries to cast pi_native_handle to the native handle type.
163  pi_native_handle is a typedef of uintptr_t. It is used to store opaque
164  pointers, such as cl_device, and integer handles, such as CUdevice. To
165  convert a uintptr_t to a pointer type, such as cl_device, reinterpret_cast
166  must be used. However, reinterpret_cast cannot be used to convert
167  uintptr_t to a different integer type, such as CUdevice. For this,
168  static_cast must be used. This function must employ a cast that is capable
169  of reinterpret_cast and static_cast depending on the arguments passed to
170  it. A C-style cast will achieve this. The compiler will attempt to
171  interpret it as a static_cast, and will fall back to reinterpret_cast
172  where appropriate.
173 
174  https://en.cppreference.com/w/cpp/language/reinterpret_cast
175  https://en.cppreference.com/w/cpp/language/explicit_cast
176  */
177  return (backend_return_t<Backend, buffer<DataT, Dims>>)(getNativeMem(Req));
178  }
179 
180  __SYCL_EXPORT pi_native_handle getNativeMem(detail::Requirement *Req) const;
181  __SYCL_EXPORT pi_native_handle getNativeQueue() const;
182  __SYCL_EXPORT pi_native_handle getNativeDevice() const;
183  __SYCL_EXPORT pi_native_handle getNativeContext() const;
184 
185  std::shared_ptr<detail::queue_impl> MQueue;
186  std::shared_ptr<detail::device_impl> MDevice;
187  std::shared_ptr<detail::context_impl> MContext;
188 
189  std::vector<ReqToMem> MMemObjs;
190 };
191 
192 } // namespace sycl
193 } // __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:537
cl::sycl::backend_return_t
typename backend_traits< Backend >::template return_type< SyclType > backend_return_t
Definition: backend.hpp:65
cl::sycl::interop_handle
Definition: interop_handle.hpp:37
cl::sycl::info::device
device
Definition: info_desc.hpp:50
cl::sycl::access::placeholder
placeholder
Definition: access.hpp:43
PI_INVALID_MEM_OBJECT
@ PI_INVALID_MEM_OBJECT
Definition: pi.h:99
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:128
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:85
detail
Definition: pi_opencl.cpp:86
access.hpp
cl::sycl::interop_handle::get_native_mem
backend_return_t< Backend, buffer< DataT, Dims > > get_native_mem(const accessor< DataT, Dims, Mode, Target, IsPlh > &Acc) const
Receives a SYCL accessor that has been defined as a requirement for the command group,...
Definition: interop_handle.hpp:54
pi.hpp
cl::sycl::buffer
Defines a shared array that can be used by kernels in queues.
Definition: buffer.hpp:46
cl::sycl::info::queue
queue
Definition: info_desc.hpp:223
cl::sycl::detail::Requirement
AccessorImplHost Requirement
Definition: accessor_impl.hpp:208
cl::sycl::detail::AccessorImplHost
Definition: accessor_impl.hpp:76
cl::sycl::accessor
Buffer accessor.
Definition: accessor.hpp:225
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:106
defines.hpp
cl::sycl::access::target
target
Definition: access.hpp:17
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:73
accessor_impl.hpp
accessor.hpp
cl::sycl::detail::getSyclObjImpl
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: common.hpp:198
backend_types.hpp
cl::sycl::detail::DispatchHostTask
Definition: commands.cpp:180
std
Definition: accessor.hpp:2532
cl::sycl::detail::AccessorBaseHost
Definition: accessor_impl.hpp:134
common.hpp
cl::sycl::access::mode
mode
Definition: access.hpp:28
cl::sycl::info::context
context
Definition: info_desc.hpp:41
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12