Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Device Memory using Buffers and Accessors

Setup Device Memory - Buffers

Description

In order to handle data on the device memory we need to create SYCL buffers.

The constructor for buffers for a raw data pointer to the data to be managed is the following:

cl::sycl::buffer<typename T, int dimensions>(
    T* ptr, 
    cl::sycl::range<dimensions>(int range_size)
);

Task

Create buffers for the 2 input vectors as well as for the output one.

Location in the source code:

// <<Setup device memory>>
Hint

// buffer for input 1
sycl::buffer<sycl::float4, 1> buf_a(&a, cl::sycl::range<1>{1});

Access Device Memory - Accessors

Description

SYCL has the concept of accessors which are used to access request control over the device memory from the buffer objects.

The different access modes are encapsulated in the following enum class:

enum class mode { 
  read,
  write, 
  read_write, 
  discard_wirte, 
  discard_read_write,
  atomic
};

that lives in the access namespace.

At this stage we are only going to focus on the more common ones: read, write, and read_write.

A SYCL accessor can be created from a buffer by using the following construct:

// e.g., read access to buffer memory
auto sycl_acc = sycl_buf.get_access<cl::sycl::access::mode::read>(cgh)

where cgh is an instance of the SYCL command group handler object cl::sycl::handler. Accessors are required to be created inside the command group for which they will be used.

Task

Request access from the buffers for access to the data on the device.

  • Read access from the input buffers
  • Write access from the output buffer

Location in the source code:

// <<Request device memory access>>
Hint

// read accessor for the memory in `buf_a`
auto a_acc = buf_a.get_access<sycl::access::mode::read>(cgh);

Run it!

Hello World from SYCL
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content