0

Consider the following example:

std::string kernelCode =
    "void kernel copy(global int* image, global int* result)"
    "{"
        "result[get_global_id(0)] = foo(get_global_id(0));"
    "}";

sources.push_back({kernelCode.c_str(), kernelCode.length()});

cl::Program program(context, sources);

if (program.build({defaultDevice}) != CL_SUCCESS)
{
    std::cerr << "Error while building kernel: " <<
        program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(defaultDevice) << std::endl;
    exit(1);
}
else
{
    qDebug() << "Kernel compiled succesfully\n";
}

How do I use or define a function like:

int foo(int id)
{
   //...
}

to use in the kernel?

1
  • 2
    If you develop a lot of code in OpenCL you should consider putting you kernel source in .cl files. This will allow you to use debugging tools and help with the legibility of your code. Commented Feb 10, 2014 at 20:12

1 Answer 1

1

Source can contain multiple functions, so simply prepend it:

std::string kernelCode =
"int foo(int id)"
"{"
"    return id + 1;"
"}"
"void kernel copy(global int* image, global int* result)"
"{"
"    result[get_global_id(0)] = foo(get_global_id(0));"
"}";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.