|
| 1 | +// RUN: %if any-device-is-level_zero %{ %{build} -isystem %sycl_include -DBUILD_FOR_L0 -o %t-l0.out %} |
| 2 | + |
| 3 | +#include <sycl/backend.hpp> |
| 4 | +#include <sycl/detail/core.hpp> |
| 5 | +#include <sycl/properties/all_properties.hpp> |
| 6 | +#include <sycl/usm.hpp> |
| 7 | +using namespace sycl; |
| 8 | + |
| 9 | +constexpr auto BACKEND = backend::ext_oneapi_level_zero; |
| 10 | +using nativeDevice = ze_device_handle_t; |
| 11 | +using nativeQueue = ze_command_queue_handle_t; |
| 12 | +using nativeEvent = ze_event_handle_t; |
| 13 | + |
| 14 | +constexpr int N = 100; |
| 15 | +constexpr int VAL = 3; |
| 16 | + |
| 17 | +int main() { |
| 18 | + |
| 19 | + assert(static_cast<bool>( |
| 20 | + std::is_same_v<backend_traits<BACKEND>::return_type<device>, |
| 21 | + nativeDevice>)); |
| 22 | + assert(static_cast<bool>( |
| 23 | + std::is_same_v<backend_traits<BACKEND>::return_type<queue>, |
| 24 | + nativeQueue>)); |
| 25 | + assert(static_cast<bool>( |
| 26 | + std::is_same_v<backend_traits<BACKEND>::return_type<event>, |
| 27 | + nativeEvent>)); |
| 28 | + |
| 29 | + device Device; |
| 30 | + backend_traits<BACKEND>::return_type<device> NativeDevice = |
| 31 | + get_native<BACKEND>(Device); |
| 32 | + // Create sycl device with a native device. |
| 33 | + auto InteropDevice = make_device<BACKEND>(NativeDevice); |
| 34 | + |
| 35 | + context Context(InteropDevice); |
| 36 | + |
| 37 | + // Create sycl queue with device created from a native device. |
| 38 | + queue Queue(InteropDevice, {sycl::property::queue::in_order()}); |
| 39 | + backend_traits<BACKEND>::return_type<queue> NativeQueue = |
| 40 | + get_native<BACKEND>(Queue); |
| 41 | + backend_traits<BACKEND>::input_type<queue> InputType(NativeQueue, Device); |
| 42 | + |
| 43 | + auto InteropQueue = make_queue<BACKEND>(InputType, Context); |
| 44 | + |
| 45 | + auto A = (int *)malloc_device(N * sizeof(int), InteropQueue); |
| 46 | + std::vector<int> vec(N, 0); |
| 47 | + |
| 48 | + auto Event = Queue.submit([&](handler &h) { |
| 49 | + h.parallel_for<class kern1>(range<1>(N), |
| 50 | + [=](id<1> item) { A[item] = VAL; }); |
| 51 | + }); |
| 52 | + |
| 53 | + backend_traits<BACKEND>::return_type<event> NativeEvent = |
| 54 | + get_native<BACKEND>(Event); |
| 55 | + backend_traits<BACKEND>::input_type<event> EventInputType; |
| 56 | + EventInputType.NativeHandle = NativeEvent; |
| 57 | + // Create sycl event with a native event. |
| 58 | + event InteropEvent = make_event<BACKEND>(EventInputType, Context); |
| 59 | + |
| 60 | + // depends_on sycl event created from a native event. |
| 61 | + auto Event2 = InteropQueue.submit([&](handler &h) { |
| 62 | + h.depends_on(InteropEvent); |
| 63 | + h.parallel_for<class kern2>(range<1>(N), [=](id<1> item) { A[item]++; }); |
| 64 | + }); |
| 65 | + |
| 66 | + auto Event3 = InteropQueue.memcpy(&vec[0], A, N * sizeof(int), Event2); |
| 67 | + Event3.wait(); |
| 68 | + |
| 69 | + if constexpr (BACKEND == backend::ext_oneapi_hip) { |
| 70 | + try { |
| 71 | + backend_traits<BACKEND>::return_type<context> NativeContext = |
| 72 | + get_native<BACKEND>(Context); |
| 73 | + } catch (sycl::exception &e) { |
| 74 | + assert(e.code() == sycl::errc::feature_not_supported); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + free(A, InteropQueue); |
| 79 | + |
| 80 | + for (const auto &val : vec) { |
| 81 | + assert(val == VAL + 1); |
| 82 | + } |
| 83 | + |
| 84 | + return 0; |
| 85 | +} |
0 commit comments