DPC++ Runtime
Runtime libraries for oneAPI DPC++
cuda.hpp
Go to the documentation of this file.
1 //==--------- cuda.hpp - SYCL CUDA backend ---------------------------------==//
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/backend.hpp>
12 #include <sycl/context.hpp>
14 
15 #include <vector>
16 
17 namespace sycl {
19 namespace ext::oneapi::cuda {
20 
21 // Implementation of ext_oneapi_cuda::make<device>
22 inline __SYCL_EXPORT device make_device(pi_native_handle NativeHandle) {
23  return sycl::detail::make_device(NativeHandle, backend::ext_oneapi_cuda);
24 }
25 
26 // Implementation of cuda::has_native_event
27 inline __SYCL_EXPORT bool has_native_event(event sycl_event) {
28  if (sycl_event.get_backend() == backend::ext_oneapi_cuda)
29  return get_native<backend::ext_oneapi_cuda>(sycl_event) != nullptr;
30 
31  return false;
32 }
33 
34 } // namespace ext::oneapi::cuda
35 
36 // CUDA context specialization
37 template <>
38 inline auto get_native<backend::ext_oneapi_cuda, context>(const context &C)
40  // create a vector to be returned
42 
43  // get the native CUDA context from the SYCL object
44  auto native = reinterpret_cast<
46  C.getNative());
47  ret.push_back(native);
48 
49  return ret;
50 }
51 
52 // Specialisation of interop_handles get_native_context
53 template <>
54 inline backend_return_t<backend::ext_oneapi_cuda, context>
55 interop_handle::get_native_context<backend::ext_oneapi_cuda>() const {
56 #ifndef __SYCL_DEVICE_ONLY__
57  return std::vector{reinterpret_cast<CUcontext>(getNativeContext())};
58 #else
59  // we believe this won't be ever called on device side
60  return {};
61 #endif
62 }
63 
64 // CUDA device specialization
65 template <>
66 inline device make_device<backend::ext_oneapi_cuda>(
68  auto devs = device::get_devices(info::device_type::gpu);
69  for (auto &dev : devs) {
70  if (dev.get_backend() == backend::ext_oneapi_cuda &&
71  BackendObject == get_native<backend::ext_oneapi_cuda>(dev)) {
72  return dev;
73  }
74  }
75  pi_native_handle NativeHandle = static_cast<pi_native_handle>(BackendObject);
76  return ext::oneapi::cuda::make_device(NativeHandle);
77 }
78 
79 // CUDA event specialization
80 template <>
81 inline event make_event<backend::ext_oneapi_cuda>(
83  const context &TargetContext) {
84  return detail::make_event(detail::pi::cast<pi_native_handle>(BackendObject),
85  TargetContext, true,
86  /*Backend*/ backend::ext_oneapi_cuda);
87 }
88 
89 // CUDA queue specialization
90 template <>
91 inline queue make_queue<backend::ext_oneapi_cuda>(
93  const context &TargetContext, const async_handler Handler) {
94  return detail::make_queue(detail::pi::cast<pi_native_handle>(BackendObject),
95  TargetContext, nullptr, true, Handler,
96  /*Backend*/ backend::ext_oneapi_cuda);
97 }
98 
99 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
100 } // namespace sycl
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:41
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:49
An event object can be used to synchronize memory transfers, enqueues of kernels and signaling barrie...
Definition: event.hpp:40
backend get_backend() const noexcept
Returns the backend associated with this platform.
Definition: event.cpp:104
Encapsulates a single SYCL queue which schedules kernels on a SYCL device.
Definition: queue.hpp:88
struct CUctx_st * CUcontext
#define __SYCL_INLINE_VER_NAMESPACE(X)
event make_event(pi_native_handle NativeHandle, const context &TargetContext, backend Backend)
Definition: backend.cpp:112
device make_device(pi_native_handle NativeHandle)
Definition: cuda.hpp:22
bool has_native_event(event sycl_event)
Definition: cuda.hpp:27
queue make_queue(const context &Context, pi_native_handle InteropHandle)
Definition: opencl.cpp:45
typename backend_traits< Backend >::template return_type< SyclType > backend_return_t
Definition: backend.hpp:72
std::function< void(sycl::exception_list)> async_handler
typename backend_traits< Backend >::template input_type< SyclType > backend_input_t
Definition: backend.hpp:68
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
uintptr_t pi_native_handle
Definition: pi.h:128