Open Source Your Knowledge, Become a Contributor

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

Create Content
Previous: Device Memory using Buffers and Accessors Next: Quiz

SYCL Kernel Programming - The single_task API

Declare the vector addition kernel


By specification, an unnamed type is an invalid template argument for classes used to name SYCL kernels. Therefore, you need to at least declare the kernel class before using it.

The following examples are valid SYCL kernel names:

// class
class MyKernelClass;
// struct
struct MyKernelStruct;
// enum class
enum class MyKernelEnum : int;


Forward declare the vector_addition class functor before the int main entry point of the application.

Location in the source code:

// <<Declare vector_addition kernel function object>>

class vector_addition;

Implement the vector addition kernel


Now we need to enqueue and the vector addition kernel for execution.

A kernel that is executed on one thread using NDRange(1,1,1) can be enqueued using the cl::sycl::single_task API:

single_task<typename kernel_lambda_name>([=](){});

This is equivalent to executing a kernel on a single compute unit with a single work-group of only one work-item. Thus, we can access the values of the accessor objects directly by using the 0th index as follows:

// e.g, assign accessor element in the kernel body
acc[0] = someValue;


Complete the vector addition kernel code where the accessors of a, b, c make the vectors available for calculating c = a + b.

Location in the source code:

// <<Complete the vector addition computation>>

c_acc[0] = a_acc[0] + b_acc[0];

Run it!

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