|
| 1 | +/** TRACCC library, part of the ACTS project (R&D line) |
| 2 | + * |
| 3 | + * (c) 2023 CERN for the benefit of the ACTS project |
| 4 | + * |
| 5 | + * Mozilla Public License Version 2.0 |
| 6 | + */ |
| 7 | + |
| 8 | +// SYCL include(s). |
| 9 | +#include <CL/sycl.hpp> |
| 10 | + |
| 11 | +// VecMem include(s). |
| 12 | +#include <vecmem/containers/data/vector_buffer.hpp> |
| 13 | +#include <vecmem/containers/device_vector.hpp> |
| 14 | +#include <vecmem/memory/host_memory_resource.hpp> |
| 15 | +#include <vecmem/memory/sycl/device_memory_resource.hpp> |
| 16 | +#include <vecmem/utils/sycl/copy.hpp> |
| 17 | + |
| 18 | +// DPL include(s). |
| 19 | +#include <oneapi/dpl/algorithm> |
| 20 | +#include <oneapi/dpl/execution> |
| 21 | + |
| 22 | +// GTest include(s). |
| 23 | +#include <gtest/gtest.h> |
| 24 | + |
| 25 | +namespace { |
| 26 | + |
| 27 | +// Simple asynchronous handler function |
| 28 | +auto handle_async_error = [](::sycl::exception_list elist) { |
| 29 | + for (auto& e : elist) { |
| 30 | + try { |
| 31 | + std::rethrow_exception(e); |
| 32 | + } catch (::sycl::exception& e) { |
| 33 | + std::cout << "ASYNC EXCEPTION!!\n"; |
| 34 | + std::cout << e.what() << "\n"; |
| 35 | + } |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +::sycl::queue q(handle_async_error); |
| 40 | +vecmem::sycl::copy copy{&q}; |
| 41 | +vecmem::host_memory_resource host_resource; |
| 42 | +vecmem::sycl::device_memory_resource device_resource; |
| 43 | + |
| 44 | +} // namespace |
| 45 | + |
| 46 | +TEST(dpl, sort) { |
| 47 | + |
| 48 | + vecmem::vector<unsigned int> host_vector{{3, 2, 1, 8, 4}, &host_resource}; |
| 49 | + |
| 50 | + auto host_buffer = vecmem::get_data(host_vector); |
| 51 | + auto device_buffer = copy.to(vecmem::get_data(host_vector), device_resource, |
| 52 | + vecmem::copy::type::host_to_device); |
| 53 | + |
| 54 | + vecmem::device_vector<unsigned int> device_vector(device_buffer); |
| 55 | + |
| 56 | + oneapi::dpl::sort(oneapi::dpl::execution::dpcpp_default, |
| 57 | + device_vector.begin(), device_vector.end()); |
| 58 | + |
| 59 | + copy(device_buffer, host_buffer, vecmem::copy::type::device_to_host); |
| 60 | + |
| 61 | + ASSERT_EQ(host_vector[0], 1); |
| 62 | + ASSERT_EQ(host_vector[1], 2); |
| 63 | + ASSERT_EQ(host_vector[2], 3); |
| 64 | + ASSERT_EQ(host_vector[3], 4); |
| 65 | + ASSERT_EQ(host_vector[4], 8); |
| 66 | +} |
| 67 | + |
| 68 | +TEST(dpl, scan) { |
| 69 | + |
| 70 | + vecmem::vector<unsigned int> host_vector{{3, 2, 1, 8, 4}, &host_resource}; |
| 71 | + |
| 72 | + auto host_buffer = vecmem::get_data(host_vector); |
| 73 | + auto device_buffer = copy.to(vecmem::get_data(host_vector), device_resource, |
| 74 | + vecmem::copy::type::host_to_device); |
| 75 | + |
| 76 | + vecmem::device_vector<unsigned int> device_vector(device_buffer); |
| 77 | + |
| 78 | + oneapi::dpl::inclusive_scan(oneapi::dpl::execution::dpcpp_default, |
| 79 | + device_vector.begin(), device_vector.end(), |
| 80 | + device_vector.begin()); |
| 81 | + |
| 82 | + copy(device_buffer, host_buffer, vecmem::copy::type::device_to_host); |
| 83 | + |
| 84 | + ASSERT_EQ(host_vector[0], 3); |
| 85 | + ASSERT_EQ(host_vector[1], 5); |
| 86 | + ASSERT_EQ(host_vector[2], 6); |
| 87 | + ASSERT_EQ(host_vector[3], 14); |
| 88 | + ASSERT_EQ(host_vector[4], 18); |
| 89 | +} |
| 90 | + |
| 91 | +TEST(dpl, fill) { |
| 92 | + |
| 93 | + vecmem::vector<unsigned int> host_vector{{1, 1, 1, 1, 1, 1, 1}, |
| 94 | + &host_resource}; |
| 95 | + |
| 96 | + auto host_buffer = vecmem::get_data(host_vector); |
| 97 | + auto device_buffer = copy.to(vecmem::get_data(host_vector), device_resource, |
| 98 | + vecmem::copy::type::host_to_device); |
| 99 | + |
| 100 | + vecmem::device_vector<unsigned int> device_vector(device_buffer); |
| 101 | + |
| 102 | + oneapi::dpl::fill(oneapi::dpl::execution::dpcpp_default, |
| 103 | + device_vector.begin(), device_vector.end(), 112); |
| 104 | + |
| 105 | + copy(device_buffer, host_buffer, vecmem::copy::type::device_to_host); |
| 106 | + |
| 107 | + ASSERT_EQ(host_vector[0], 112); |
| 108 | + ASSERT_EQ(host_vector[1], 112); |
| 109 | + ASSERT_EQ(host_vector[2], 112); |
| 110 | + ASSERT_EQ(host_vector[3], 112); |
| 111 | + ASSERT_EQ(host_vector[4], 112); |
| 112 | + ASSERT_EQ(host_vector[5], 112); |
| 113 | + ASSERT_EQ(host_vector[6], 112); |
| 114 | +} |
0 commit comments