sycl::buffer - Posted on July 21, 2024 - See https://sycl.tech - Logs Home - d0032
cpp/c++ sycl::buffer - The sycl::buffer class defines a shared array of one, two or three dimensions that can be used by the SYCL kernel and has to be accessed using accessor classes. Buffers are templated on both the type of their data, and the number of dimensions that the data is stored and accessed through.
cpp/c++ examples: sycl::buffer
#include <sycl/sycl.hpp> #include <numbers> #include <iostream> #include <numeric> namespace gpu { class copy_kernel { private: sycl::accessor<float, 1, sycl::access_mode::read> __in_accessor; sycl::accessor<float, 1, sycl::access_mode::write> __out_accessor; public: copy_kernel() = delete; copy_kernel( sycl::accessor<float, 1, sycl::access_mode::read> in_accessor__, sycl::accessor<float, 1, sycl::access_mode::write> out_accessor__, sycl::handler & handler__ ): __in_accessor{in_accessor__}, __out_accessor{out_accessor__} { } public: void operator()(sycl::nd_item<1> item) const { sycl::id<1> id = item.get_global_id(); this->__out_accessor[id] = this->__in_accessor[id]; } }; } // namespace gpu int main() { std::vector<float> input(16); std::iota(input.begin(), input.end(), 1.4f); // sycl::buffer auto in_buffer = new sycl::buffer<float, 1>{input.data(), sycl::range<1>{input.size()}}; // sycl::buffer auto out_buffer = new sycl::buffer<float, 1>{sycl::range<1>{input.size()}}; sycl::queue queue{sycl::gpu_selector_v}; queue.submit( [&] (sycl::handler & handler) { auto in_accessor = sycl::accessor<float, 1, sycl::access_mode::read>{* in_buffer, handler, sycl::read_only}; // template params are auto-deduced auto out_accessor = sycl::accessor{* out_buffer, handler, sycl::write_only}; auto copy_kernel = gpu::copy_kernel{in_accessor, out_accessor, handler}; handler.parallel_for<class ken1>( sycl::nd_range<1>{ sycl::range<1>{input.size()}, // global range sycl::range<1>{input.size()/4} // local range: work group size }, copy_kernel ); } ); auto result_accessor = out_buffer->get_host_access(); std::copy(result_accessor.begin(), result_accessor.end(), std::ostream_iterator<float>{std::cout, " "}); std::cout << std::endl; delete in_buffer; delete out_buffer; }
acpp prog.cpp -std=c++23 -o prog
dpcpp prog.cpp -std=c++23 -o prog
1.4 2.4 3.4 4.4 5.4 6.4 7.4 8.4 9.4 10.4 11.4 12.4 13.4 14.4 15.4 16.4
c++ std::exception:
std::cout.write(err.data(), err.size());
std::cout << std::endl;
caught:
================================================== # The c++ programming language. # # # # Home: cppfx.xyz # # Join c++ Discord: yZcauUAUyC # # Deck # ==================================================