15

In this tutorial

There are 2 methods to run the kernel, and another one mentioned in the comments:

1.

cl::KernelFunctor simple_add(cl::Kernel(program,"simple_add"),queue,cl::NullRange,cl::NDRange(10),cl::NullRange);
simple_add(buffer_A,buffer_B,buffer_C);

However, I found out, that KernelFunctor has gone.

So I tried the alternative way:

2.

cl::Kernel kernel_add=cl::Kernel(program,"simple_add");
kernel_add.setArg(0,buffer_A);
kernel_add.setArg(1,buffer_B);
kernel_add.setArg(2,buffer_C);
queue.enqueueNDRangeKernel(kernel_add,cl::NullRange,cl::NDRange(10),cl::NullRange);
queue.finish();

It compiles and runs succussfully.

However, there is a 3rd option in the comments:

3.

cl::make_kernel simple_add(cl::Kernel(program,"simple_add"));
cl::EnqueueArgs eargs(queue,cl::NullRange,cl::NDRange(10),cl::NullRange);
simple_add(eargs, buffer_A,buffer_B,buffer_C).wait();

Which does not compile, I think the make_kernel needs template arguments. I'm new to OpenCl, and didn't manage to fix the code.

My question is:

1. How should I modify the 3. code to compile?

2. Which way is better and why? 2. vs. 3.?

otisonoza
  • 1,334
  • 2
  • 14
  • 32

1 Answers1

4

You can check the OpenCL C++ Bindings Specification for a detailed description of the cl::make_kernel API (in section 3.6.1), which includes an example of usage.

In your case, you could write something like this to create the kernel functor:

auto simple_add = cl::make_kernel<cl::Buffer&, cl::Buffer&, cl::Buffer&>(program, "simple_add");

Your second question is primarily opinion based, and so is difficult to answer. One could argue that the kernel functor approach is simpler, as it allows you to 'call' the kernel almost as if it were just a function and pass the arguments in a familiar manner. The alternative approach (option 2 in your question) is more explicit about setting arguments and enqueuing the kernel, but more closely represents how you would write the same code using the OpenCL C API. Which method you use is entirely down to personal preference.

jprice
  • 9,755
  • 1
  • 28
  • 32
  • just a note on make_kernel, this feature like KernelFunctor is more likely to change eventually than setArg. If you have a goal of making the codebase last longer while using functors, wrapping setArg functionality into your own functor class might be useful. – That Realty Programmer Guy Mar 17 '15 at 14:14