Deandre Yuself Bauswell Deandre Yuself Bauswell - 1 year ago 233
C++ Question

OpenCL HelloWorld

I've just started working in opencl and I'm currently working on what should be a relatively basic hello_world program in opencl. Unfortunately the program is not outputting the proper phrase or anything at all it instead hangs with no output.

Any idea on why that is the case?

Below is: openglsource.cpp and


#include <fstream>

int main()
std::vector<cl::Platform> platforms;

auto platform = platforms.front();
std::vector<cl::Device> devices;
platform.getDevices(CL_DEVICE_TYPE_CPU, &devices);

auto device = devices.front();

std::ifstream helloWorldFile("");
std::string src(std::istreambuf_iterator<char>(helloWorldFile), (std::istreambuf_iterator<char>()));

cl::Program::Sources sources( 1, std::make_pair(src.c_str(), src.length() + 1));

cl::Context context(device);
cl::Program program(context, sources);

auto err ="-cl-std=CL1.2");

char buf[16];
cl::Buffer memBuf(context, CL_MEM_WRITE_ONLY | CL_MEM_HOST_READ_ONLY, sizeof(buf));
cl::Kernel kernel(program, "Hello", &err);
kernel.setArg(0, memBuf);

cl::CommandQueue queue(context, device);
queue.enqueueReadBuffer(memBuf, GL_TRUE, 0, sizeof(buf), buf);

std::cout << "hello";


__kernel void HelloWorld(__global char* data)
data[0] = 'H';
data[1] = 'E';
data[2] = 'L';
data[3] = 'L';
data[4] = 'O';
data[5] = ' ';
data[6] = 'W';
data[7] = 'O';
data[8] = 'R';
data[9] = 'L';
data[10] = 'D';
data[11] = '!';
data[12] = '\n';

Answer Source

The issue was in

 cl::Kernel kernel(program, "Hello", &err);

it should be

 cl::Kernel kernel(program, "HelloWorld", &err); 

The string that goes there is not just an arbitrary name as I had inferred it should be the function that you want to call from the specified kernel. Which makes sense give that a kernel can hold multiple functions!

Such a simple fix.....I feel bad for posting!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download