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>
13 #include <sycl/interop_handle.hpp>
14 
16 
17 #include <vector>
18 
19 namespace sycl {
20 inline namespace _V1 {
21 namespace ext::oneapi::cuda {
22 
23 // Implementation of ext_oneapi_cuda::make<device>
24 inline __SYCL_EXPORT device make_device(ur_native_handle_t NativeHandle) {
26 }
27 
28 // Implementation of cuda::has_native_event
29 inline __SYCL_EXPORT bool has_native_event(event sycl_event) {
30  if (sycl_event.get_backend() == backend::ext_oneapi_cuda)
31  return get_native<backend::ext_oneapi_cuda>(sycl_event) != nullptr;
32 
33  return false;
34 }
35 
36 } // namespace ext::oneapi::cuda
37 
38 // CUDA context specialization
39 template <>
40 inline auto get_native<backend::ext_oneapi_cuda, context>(const context &C)
42  // create a vector to be returned
44 
45  // get the native CUDA context from the SYCL object
46  auto native = reinterpret_cast<
48  C.getNative());
49  ret.push_back(native);
50 
51  return ret;
52 }
53 
54 // Specialisation of interop_handles get_native_context
55 template <>
56 inline backend_return_t<backend::ext_oneapi_cuda, context>
57 interop_handle::get_native_context<backend::ext_oneapi_cuda>() const {
58 #ifndef __SYCL_DEVICE_ONLY__
59  return std::vector{reinterpret_cast<CUcontext>(getNativeContext())};
60 #else
61  // we believe this won't be ever called on device side
62  return {};
63 #endif
64 }
65 
66 // CUDA device specialization
67 template <>
68 inline device make_device<backend::ext_oneapi_cuda>(
71  for (auto &dev : devs) {
72  if (dev.get_backend() == backend::ext_oneapi_cuda &&
73  BackendObject == get_native<backend::ext_oneapi_cuda>(dev)) {
74  return dev;
75  }
76  }
77  ur_native_handle_t NativeHandle =
78  static_cast<ur_native_handle_t>(BackendObject);
79  return ext::oneapi::cuda::make_device(NativeHandle);
80 }
81 
82 // CUDA event specialization
83 template <>
84 inline event make_event<backend::ext_oneapi_cuda>(
86  const context &TargetContext) {
87  return detail::make_event(reinterpret_cast<ur_native_handle_t>(BackendObject),
88  TargetContext, true,
89  /*Backend*/ backend::ext_oneapi_cuda);
90 }
91 
92 // CUDA queue specialization
93 template <>
94 inline queue make_queue<backend::ext_oneapi_cuda>(
96  const context &TargetContext, const async_handler Handler) {
97  int32_t nativeHandleDesc = 0;
98  const property_list &PropList{};
99  return detail::make_queue(reinterpret_cast<ur_native_handle_t>(BackendObject),
100  nativeHandleDesc, TargetContext, nullptr, true,
101  PropList, Handler,
102  /*Backend*/ backend::ext_oneapi_cuda);
103 }
104 
105 } // namespace _V1
106 } // namespace sycl
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:50
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:64
static std::vector< device > get_devices(info::device_type deviceType=info::device_type::all)
Query available SYCL devices.
Definition: device.cpp:53
An event object can be used to synchronize memory transfers, enqueues of kernels and signaling barrie...
Definition: event.hpp:44
backend get_backend() const noexcept
Returns the backend associated with this platform.
Definition: event.cpp:120
Objects of the property_list class are containers for the SYCL properties.
Encapsulates a single SYCL queue which schedules kernels on a SYCL device.
Definition: queue.hpp:110
struct CUctx_st * CUcontext
queue make_queue(ur_native_handle_t NativeHandle, int32_t nativeHandleDesc, const context &TargetContext, const device *TargetDevice, bool KeepOwnership, const property_list &PropList, const async_handler &Handler, backend Backend)
Definition: backend.cpp:116
event make_event(ur_native_handle_t NativeHandle, const context &TargetContext, backend Backend)
Definition: backend.cpp:161
device make_device(ur_native_handle_t NativeHandle)
Definition: cuda.hpp:24
bool has_native_event(event sycl_event)
Definition: cuda.hpp:29
typename backend_traits< Backend >::template return_type< SyclType > backend_return_t
Definition: backend.hpp:87
std::function< void(sycl::exception_list)> async_handler
typename backend_traits< Backend >::template input_type< SyclType > backend_input_t
Definition: backend.hpp:83
Definition: access.hpp:18