Skip to content

Commit 4d69c29

Browse files
authored
[SYCL][CUDA] Fix get_native interop for device (#6649)
This patch fixes: #6635 In #6483, the implementation of `get_native` for device for the CUDA plugin was mistakenly moved to the experimental interface header, and so it was no longer available for the regular interface, causing build issues. For the CUDA plugin there is currently two interfaces for the CUDA interop, the "legacy" one which is used by projects such as oneMKL and oneDNN, and the "experimental" one, defined in the `sycl/ext/oneapi/experimental/backend/cuda.hpp` header which implements the interop as described in the CUDA backend specification proposed here: KhronosGroup/SYCL-Docs#197
1 parent 85a6833 commit 4d69c29

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

sycl/include/sycl/backend.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ struct BufferInterop<backend::opencl, DataT, Dimensions, AllocatorT> {
101101
}
102102
};
103103

104+
#if SYCL_EXT_ONEAPI_BACKEND_LEVEL_ZERO
104105
template <backend BackendName, typename DataT, int Dimensions,
105106
typename AllocatorT>
106107
auto get_native_buffer(const buffer<DataT, Dimensions, AllocatorT, void> &Obj)
@@ -115,6 +116,7 @@ auto get_native_buffer(const buffer<DataT, Dimensions, AllocatorT, void> &Obj)
115116
PI_ERROR_INVALID_OPERATION);
116117
return Obj.template getNative<BackendName>();
117118
}
119+
#endif
118120
} // namespace detail
119121

120122
template <backend BackendName, class SyclObjectT>
@@ -147,6 +149,7 @@ auto get_native(const buffer<DataT, Dimensions, AllocatorT> &Obj)
147149
return detail::get_native_buffer<BackendName>(Obj);
148150
}
149151

152+
#if SYCL_BACKEND_OPENCL
150153
template <>
151154
inline backend_return_t<backend::opencl, event>
152155
get_native<backend::opencl, event>(const event &Obj) {
@@ -164,6 +167,23 @@ get_native<backend::opencl, event>(const event &Obj) {
164167
}
165168
return ReturnValue;
166169
}
170+
#endif
171+
172+
#if SYCL_EXT_ONEAPI_BACKEND_CUDA
173+
template <>
174+
inline backend_return_t<backend::ext_oneapi_cuda, device>
175+
get_native<backend::ext_oneapi_cuda, device>(const device &Obj) {
176+
// TODO use SYCL 2020 exception when implemented
177+
if (Obj.get_backend() != backend::ext_oneapi_cuda) {
178+
throw sycl::runtime_error(errc::backend_mismatch, "Backends mismatch",
179+
PI_ERROR_INVALID_OPERATION);
180+
}
181+
// CUDA uses a 32-bit int instead of an opaque pointer like other backends,
182+
// so we need a specialization with static_cast instead of reinterpret_cast.
183+
return static_cast<backend_return_t<backend::ext_oneapi_cuda, device>>(
184+
Obj.getNative());
185+
}
186+
#endif
167187

168188
// Native handle of an accessor should be accessed through interop_handler
169189
template <backend BackendName, typename DataT, int Dimensions,

sycl/include/sycl/ext/oneapi/experimental/backend/cuda.hpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,6 @@ inline device make_device<backend::ext_oneapi_cuda>(
7373
return ext::oneapi::cuda::make_device(NativeHandle);
7474
}
7575

76-
template <>
77-
backend_return_t<backend::ext_oneapi_cuda, device>
78-
get_native<backend::ext_oneapi_cuda, device>(const device &Obj) {
79-
// TODO use SYCL 2020 exception when implemented
80-
if (Obj.get_backend() != backend::ext_oneapi_cuda) {
81-
throw sycl::runtime_error(errc::backend_mismatch, "Backends mismatch",
82-
PI_ERROR_INVALID_OPERATION);
83-
}
84-
// CUDA uses a 32-bit int instead of an opaque pointer like other backends,
85-
// so we need a specialization with static_cast instead of reinterpret_cast.
86-
return static_cast<backend_return_t<backend::ext_oneapi_cuda, device>>(
87-
Obj.getNative());
88-
}
89-
9076
// CUDA event specialization
9177
template <>
9278
inline event make_event<backend::ext_oneapi_cuda>(

sycl/test/basic_tests/interop-cuda-experimental.cpp renamed to sycl/test/basic_tests/interop-cuda.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
// REQUIRES: cuda
22
// RUN: %clangxx %fsycl-host-only -fsyntax-only -Xclang -verify -Xclang -verify-ignore-unexpected=note %s -o %t.out
33
// RUN: %clangxx %fsycl-host-only -fsyntax-only -Xclang -verify -Xclang -verify-ignore-unexpected=note -D__SYCL_INTERNAL_API %s -o %t.out
4+
//
5+
/// Also test the experimental CUDA interop interface
6+
// RUN: %clangxx %fsycl-host-only -fsyntax-only -Xclang -verify -Xclang -verify-ignore-unexpected=note -DSYCL_EXT_ONEAPI_BACKEND_CUDA_EXPERIMENTAL %s -o %t.out
7+
// RUN: %clangxx %fsycl-host-only -fsyntax-only -Xclang -verify -Xclang -verify-ignore-unexpected=note -D__SYCL_INTERNAL_API -DSYCL_EXT_ONEAPI_BACKEND_CUDA_EXPERIMENTAL %s -o %t.out
48
// expected-no-diagnostics
59

6-
// Test for experimental CUDA interop API
10+
// Test for legacy and experimental CUDA interop API
711

8-
#define SYCL_EXT_ONEAPI_BACKEND_CUDA_EXPERIMENTAL 1
12+
#ifdef SYCL_EXT_ONEAPI_BACKEND_CUDA_EXPERIMENTAL
913
#include <sycl/ext/oneapi/experimental/backend/cuda.hpp>
14+
#endif
15+
1016
#include <sycl/sycl.hpp>
1117

1218
using namespace sycl;
@@ -73,6 +79,7 @@ int main() {
7379
// behavior of these template functions is defined by the SYCL backend
7480
// specification document.
7581

82+
#ifdef SYCL_EXT_ONEAPI_BACKEND_CUDA_EXPERIMENTAL
7683
backend_input_t<backend::ext_oneapi_cuda, device> InteropDeviceInput{
7784
cu_device};
7885
device InteropDevice =
@@ -85,6 +92,7 @@ int main() {
8592
event InteropEvent = make_event<backend::ext_oneapi_cuda>(cu_event, Context);
8693

8794
queue InteropQueue = make_queue<backend::ext_oneapi_cuda>(cu_queue, Context);
95+
#endif
8896

8997
return 0;
9098
}

0 commit comments

Comments
 (0)