From f49ef918c430cf4c681900d75c3129d1c142a53d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Sat, 11 Oct 2025 16:55:47 +0800 Subject: [PATCH] feat: add tests in Chapter 2 --- tests/fig-2-2_simple-sycl-program.cpp | 38 +++++++++++++++++++++++++ tests/fig-2-9_selecting-host-device.cpp | 26 +++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 tests/fig-2-2_simple-sycl-program.cpp create mode 100644 tests/fig-2-9_selecting-host-device.cpp diff --git a/tests/fig-2-2_simple-sycl-program.cpp b/tests/fig-2-2_simple-sycl-program.cpp new file mode 100644 index 0000000..42429af --- /dev/null +++ b/tests/fig-2-2_simple-sycl-program.cpp @@ -0,0 +1,38 @@ +// Figure 2-2. Simple SYCL program +// from book - Data Parallel C++ +// https://link.springer.com/book/10.1007/978-1-4842-5574-2 + +#include +#include +#include + +int main() { + constexpr int size = 16; + std::array data; + + // Create queue on implementation-chosen default device + sycl::queue Q; + + // Create buffer using host allocated "data" array + sycl::buffer B{data}; + + Q.submit([&](sycl::handler& h) { + sycl::accessor A{B, h}; + + h.parallel_for(size, [=](auto& idx) { A[idx] = idx; }); + }); + + // Obtain access to buffer on the host + // Will wait for device kernel to execute to generate data + sycl::host_accessor A{B}; + + for (int i = 0; i < size; i++) { + std::cout << "data[" << i << "] = " << A[i] << "\n"; + if (A[i] != i) { + std::cerr << "unexpected data at idx " << i << std::endl; + return -1; + } + } + + return 0; +} \ No newline at end of file diff --git a/tests/fig-2-9_selecting-host-device.cpp b/tests/fig-2-9_selecting-host-device.cpp new file mode 100644 index 0000000..b9d678c --- /dev/null +++ b/tests/fig-2-9_selecting-host-device.cpp @@ -0,0 +1,26 @@ +// Figure 2-9. Selecting the host device using the host_selector class +// from book - Data Parallel C++ +// https://link.springer.com/book/10.1007/978-1-4842-5574-2 + +#include +#include + +int main() { + // Create queue to use the host device explicitly + sycl::queue Q{sycl::cpu_selector_v}; + + std::cout << "Selected device: " << Q.get_device().get_info() + << std::endl; + std::cout << " -> Vendor: " << Q.get_device().get_info() << std::endl; + std::cout << " -> Backend: " + << Q.get_device().get_platform().get_info() << std::endl; + + auto device_type = Q.get_device().get_info(); + if (device_type != sycl::info::device_type::cpu) { + std::cerr << "Error: Selected device is not a CPU device" << std::endl; + return -1; + } + + std::cout << "Device is CPU: OK" << std::endl; + return 0; +} \ No newline at end of file