1
0
mirror of https://github.com/fumiama/base16384-sycl.git synced 2026-06-08 20:10:24 +08:00

feat: add tests in Chapter 2

This commit is contained in:
源文雨
2025-10-11 16:55:47 +08:00
parent eb8131173e
commit f49ef918c4
2 changed files with 64 additions and 0 deletions

View File

@@ -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 <array>
#include <iostream>
#include <sycl/sycl.hpp>
int main() {
constexpr int size = 16;
std::array<int, size> 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;
}

View File

@@ -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 <iostream>
#include <sycl/sycl.hpp>
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<sycl::info::device::name>()
<< std::endl;
std::cout << " -> Vendor: " << Q.get_device().get_info<sycl::info::device::vendor>() << std::endl;
std::cout << " -> Backend: "
<< Q.get_device().get_platform().get_info<sycl::info::platform::name>() << std::endl;
auto device_type = Q.get_device().get_info<sycl::info::device::device_type>();
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;
}