0
votes

I am trying to run vector addition code in OpenCL and I keep getting clCreateKernel:-46 error. The build program is returning no error.

vector_add_kernel.cl file below

 __kernel void vecadd(__global int *A, __global int *B, __global int *C)                        
{                                             
int idx = get_global_id(0);                      
C[idx] = A[idx] + B[idx];                         
}   

This is vector addition kernel and the code. I am struggling to fine reason why I am getting the error. I was able to compile in C but failing to work the code in C++ wrapper.

#define __CL_ENABLE_EXCEPTIONS
#include <CL/cl.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <fstream>

int main(){
    const int elements = 2048;
    size_t datasize = sizeof(int) * elements;

    int *A = (int*)malloc(datasize);
    int *B = (int*)malloc(datasize);
    int *C = (int*)malloc(datasize);

    //Initialize the input data
    int i;
    for (i = 0; i < elements; i++){
        A[i] = i;
        B[i] = i;
    }

    try{
        std::vector <cl::Platform> platforms;
        cl::Platform::get(&platforms);

        auto platform = platforms.back();
        std::vector <cl::Device> devices;
        platforms[0].getDevices(CL_DEVICE_TYPE_ALL, &devices);

        cl::Context context(devices);

        cl::CommandQueue queue = cl::CommandQueue(context, devices[0]);

        cl::Buffer bufferA = cl::Buffer(context, CL_MEM_READ_ONLY, datasize);
        cl::Buffer bufferB = cl::Buffer(context, CL_MEM_READ_ONLY, datasize);
        cl::Buffer bufferC = cl::Buffer(context, CL_MEM_WRITE_ONLY, datasize);

        queue.enqueueWriteBuffer(bufferA, CL_TRUE, 0, datasize, A);
        queue.enqueueWriteBuffer(bufferB, CL_TRUE, 0, datasize, B);

        std::ifstream sourceFile("vecadd.cl");
        std::string sourceCode(std::istreambuf_iterator <char>(sourceFile),
            (std::istreambuf_iterator <char>()));

        cl::Program::Sources source(1, std::make_pair(sourceCode.c_str(), sourceCode.length() + 1));


        cl::Program program = cl::Program(context, source);

        program.build(devices);

        cl::Kernel vecadd_kernel(program, "vecadd");

        vecadd_kernel.setArg(0, bufferA);
        vecadd_kernel.setArg(1, bufferB);
        vecadd_kernel.setArg(2, bufferC);

        cl::NDRange global(elements);
        cl::NDRange local(256);

        queue.enqueueNDRangeKernel(vecadd_kernel, cl::NullRange, global, local);

        queue.enqueueReadBuffer(bufferC, CL_TRUE, 0, datasize, C);

    }
    catch(cl::Error error){
        std::cout << error.what() << "(" << error.err() << ")" << std::endl;
    }

    return 0;

}
1

1 Answers

1
votes

In the .cpp file you declare that the kernel name is vector_add_kernel1:

cl::Kernel vecadd_kernel(program, "vector_add_kernel1");
                                  ^^^^^^^^^^^^^^^^^^^^

but in the vector_add_kernel.cl file the kernel name is vecadd:

__kernel void vecadd(__global int *A, __global int *B, __global int *C)
              ^^^^^^

Change the kernel name in the .cpp file to match the one in vector_add_kernel.cl:

cl::Kernel vecadd_kernel(program, "vecadd");
                                  ^^^^^^^^