CL_INVALID_ARG_SIZE when setting kernel arg

I’m making an attempt to break away from CUDA and learn OpenCL. I thought an n-body simulation might be a good place to start. I’ve been using the c++ wrapper, and following the tutorial provided here to get a basic idea of how things should work.

The program loads 2 source files, one for each kernel function. Each is compiled, and built into a separate kernel. On my first attempt, they were in the same kernel. This was an attempt to fix the issue by doing something different.

typedef struct body_t {

__kernel void execute(__global struct body_t* bodies, const float G, const int n){

typedef struct body_t {

__kernel void execute(__global struct body_t* bodies, const float dt, const int n){

The buffers are allocated as such:

// create memory buffers
bGravity = new cl::Buffer(*context, CL_MEM_READ_ONLY, sizeof(float));
bBodies = new cl::Buffer(*context, CL_MEM_READ_WRITE,
            capacity * sizeof(body_t));
bTimestep = new cl::Buffer(*context, CL_MEM_READ_ONLY, sizeof(float));
bSize = new cl::Buffer(*context, CL_MEM_READ_ONLY, sizeof(int));

After the data is copied to the buffers, and it comes time to run the simulation, I set the kernel arguments as such:

fKernel->setArg(0, *bBodies);
fKernel->setArg(1, *bGravity);
fKernel->setArg(2, *bSize);

pKernel->setArg(0, *bBodies);
pKernel->setArg(1, *bTimestep);
pKernel->setArg(2, *bSize);

cl::NDRange global(capacity);
cl::NDRange local(1);

for (int step = 0; step < steps; step++) {
    queue->enqueueNDRangeKernel(*fKernel, cl::NullRange, global, local);
    queue->enqueueNDRangeKernel(*pKernel, cl::NullRange, global, local);

But on execution of the second line of the simulation function (setKernelArg(1, *bGravity)), the program terminates with CL_INVALID_ARG_SIZE. It seemed like it should be a trivial error to solve, but try as I might, I can’t seem to find anything that would cause it.

I’ve tried passing different data types, including the types provided by opencl (cl_float, etc), but the problem remains. I’m sure I’ve just done something dumb, but I’ve been banging my head against the wall for the past few days to no avail.

In my attempt to keep this post short, if there’s any critical code I neglected to include, everything can be found on the git repo here.

Leave a Reply

Your email address will not be published. Required fields are marked *