PrevUpHomeNext

sycl handler.copy(...), handler.fill(...)


sycl handler.copy(...), handler.fill(...) - Posted on July 21, 2024 - See https://sycl.tech - Logs Home - d0031

sycl handler.copy(...), handler.fill(...)

sycl handler.copy(...), handler.fill(...)

Copy or fill data between/to accessors, std shared-pointers, or pointers on GPU with sycl.

cpp example 1

cpp/c++ example 1: sycl handler.copy(...)

#include <sycl/sycl.hpp>
#include <vector>
#include <iostream>

int main()
{
	sycl::queue queue{sycl::gpu_selector_v};
	std::vector<float> v1{2,4,6,8,9};
	std::vector<float> v2(v1.size());
	auto buffer1 = new sycl::buffer<float, 1>{v1.data(), sycl::range<1>{v1.size()}};
	auto buffer2 = new sycl::buffer<float, 1>{v2.data(), sycl::range<1>{v1.size()}};
	queue.submit(
		[&] (sycl::handler & handler)
		{
			auto accessor1 = sycl::accessor<float, 1, sycl::access_mode::read>{* buffer1, handler, sycl::read_only};
			auto accessor2 = sycl::accessor<float, 1, sycl::access_mode::write>{* buffer2, handler, sycl::write_only};
			handler.copy(accessor1, accessor2);	// Copy on GPU
		}
	);
	delete buffer1;
	delete buffer2;	// wait
	std::copy(v2.begin(), v2.end(), std::ostream_iterator<float>{std::cout, " "});	// print
	std::cout << std::endl;
}

dpcpp prog.cpp -std=c++23 -o prog

2 4 6 8 9

cpp example 2

cpp/c++ example 2: sycl handler.fill(...)

#include <sycl/sycl.hpp>
#include <iostream>
#include <numbers>

using std::numbers::pi;

int main()
{
	std::vector<float> vector(7);
	sycl::queue queue{sycl::gpu_selector_v};
	auto buffer = new sycl::buffer<float, 1>{vector.data(), sycl::range<1>{vector.size()}};
	queue.submit(
		[&] (sycl::handler & handler)
		{
			auto accessor = sycl::accessor<float, 1, sycl::access_mode::write>{* buffer, handler, sycl::write_only};
			handler.fill(accessor, (float)pi);	// Fill on GPU
		}
	);
	delete buffer;	// wait
	std::copy(vector.begin(), vector.end(), std::ostream_iterator<float>{std::cout, " "});	// print
	std::cout << std::endl;
}

dpcpp 1.cpp -std=c++23 -o 1

3.14159 3.14159 3.14159 3.14159 3.14159 3.14159 3.14159

PrevUpHomeNext

E

U