Mikey

OpenCL on GPU

Discussion created by Mikey on Dec 9, 2009
Latest reply on Dec 10, 2009 by genaganna

Hi guys.

I'm trying to run simple kernel on my GPU, however it doesn't work as it should. I want to get 'hello world' string in outH but instead I get "". If I change:

kernel.bind(queue, cl::NDRange(hw.length()+1), cl::NDRange(1)) ().wait();

to:

kernel.bind(queue, cl::NDRange(hw.length()+1), cl::NDRange(hw.length()+1)) ().wait();

it gives me 'H'. 

As I understand I need hw.length()+1 work-items, each one for diffrent letter + '\0' character. So global == hw.length()+1. 

Another thing I don't get is that when I change device to CPU it works. It works in both cases! It works even if I change buffer to be READ_ONLY...

 

// use exceptions instead of return codes #define __CL_ENABLE_EXCEPTIONS #include <CL/cl.hpp> #include <cstdio> #include <cstdlib> #include <fstream> #include <iostream> #include <string> const std::string hw("Hello World\n"); int main() { char *outH; try { // get context / GPU cl::Context context(CL_DEVICE_TYPE_GPU); std::vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>(); std::ifstream file("kernel.cl"); std::string prog(std::istreambuf_iterator<char>(file), (std::istreambuf_iterator<char>())); cl::Program::Sources source(1, std::make_pair(prog.c_str(), prog.length()+1)); cl::Program program(context, source); program.build(devices); cl::Kernel kernel(program, "hello"); // buffer for output outH = new char[hw.length() + 1]; memset(outH, 0, hw.length() + 1); cl::Buffer outCL(context, CL_MEM_WRITE_ONLY, hw.length()+1, NULL, NULL); kernel.setArg(0, outCL); cl::CommandQueue queue(context, devices[0]); // i'm not sure of those ranges kernel.bind(queue, cl::NDRange(hw.length()+1), cl::NDRange(1)) ().wait(); queue.enqueueReadBuffer(outCL, CL_TRUE, 0, hw.length()+1, outH); std::cout << outH; } catch (cl::Error err) { std::cerr << "ERROR: " << err.what() << "(" << err.err() << ")" << std::endl; } delete [] outH; return EXIT_SUCCESS; } /// now kernel file: const char hw[] = "Hello World\n"; __kernel void hello(__global char * out) { size_t tid = get_global_id(0); out[tid] = hw[tid]; }

Outcomes