PrevUpHomeNext

sycl::buffer, std::vector


sycl::buffer, std::vector. c++

Use std::vector object data as the data of sycl::buffer object.

Make sycl::buffer:

auto buffer = new sycl::buffer<the_value_type, the_dimensions>{
	the_data,
	sycl::range<the_dimensions>{dim1_size, dim2_size, ...}
};

Make std::vector:

auto vector = std::vector<the_value_type>{...};

How make std::vector as the_data of sycl::buffer:

auto buffer = new sycl::buffer<the_value_type, the_dimensions>{
	vector.data(),
	the_range
};

c++ example:

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

namespace my_gpu
{

class my_printer
{
public:
	void operator()(
		const std::string_view label,
		const std::vector<float> & vector,
		int height,
		int width
	) const
	{
		std::cout << label << std::endl << std::endl;
		for (int j=0; j<height; ++j)
		{
			for (int i=0; i<width; ++i)
			{
				std::cout << vector[j*width+i] << ' ';
			}
			std::cout << std::endl;
		}
		std::cout << std::endl;
	}
};

constexpr inline auto print = my_gpu::my_printer{};

}

int main()
{
	constexpr int dimy = 9, dimx = 4;
	constexpr int ldimy = 3, ldimx = 2;

// input
	auto input_vector = std::vector<float>(dimy * dimx);
	std::iota(input_vector.begin(), input_vector.end(), 1.0f);

	auto input =
		new sycl::buffer<float, 2>
		{
			input_vector.data(),
			sycl::range<2>{dimy, dimx}
		};

// output
	auto output_vector = std::vector<float>(input_vector.size());
	auto output =
		new sycl::buffer<float, 2>
		{
			output_vector.data(),
			sycl::range<2>{dimy, dimx}
		};

// sycl::queue
	auto queue = sycl::queue{sycl::gpu_selector_v};

	queue.submit(
		[&] (sycl::handler & handler)
		{
			auto input_accessor = sycl::accessor<float, 2, sycl::access_mode::read>{
				* input,
				handler,
				sycl::read_only
			};

			auto output_accessor = sycl::accessor<float, 2, sycl::access_mode::write>{
				* output,
				handler,
				sycl::write_only
			};

			handler.parallel_for<class kernel_09>(
				sycl::nd_range<2>{
					sycl::range<2>{dimy, dimx},	// global range
					sycl::range<2>{ldimy, ldimx}	// group range
				},
				[=] (sycl::nd_item<2> item)
				{
					unsigned long id0 = item.get_global_id(0);
					unsigned long id1 = item.get_global_id(1);

					const float & i = input_accessor[id0][id1];
					float & o = output_accessor[id0][id1];

					o = i/2;
				}
			);
		}
	);

	delete output;
	delete input;

	my_gpu::print("input:", input_vector, dimy, dimx);
	my_gpu::print("output:", output_vector, dimy, dimx);
}

Compile & Run:

$ dpcpp test.cpp -std=c++23 -o test
$ ./test

Output:

input:

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
21 22 23 24
25 26 27 28
29 30 31 32
33 34 35 36

output:

0.5 1 1.5 2
2.5 3 3.5 4
4.5 5 5.5 6
6.5 7 7.5 8
8.5 9 9.5 10
10.5 11 11.5 12
12.5 13 13.5 14
14.5 15 15.5 16
16.5 17 17.5 18

____

Dec 20, 2024

Back

Up

cpp/c++

c++ std::exception:

std::cout.write(err.data(), err.size());

std::cout << std::endl;

caught:

  ===================================
  #  The c++ programming language.  #
  #                                 #
  #  Join c++ Discord: yZcauUAUyC   #
  #  Deck                           #
  ===================================

Home: cppfx.xyz

K


PrevUpHomeNext