From c3902c2f6edeb7163949684d43ec53dc01b9ec3f Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Mon, 29 Aug 2016 18:51:46 -0700 Subject: [PATCH] Added args and buffer handling, but now clEnqueueNDRangeKernel is failing with the error invalid command queue. Haven't seen that one before, and cursory google suggests it's a problem with MBP's. Fun! I'll keep taking a look, and I'll try it on my windows machine here soon --- include/CL_Wrapper.h | 3 ++- kernels/minimal_kernel.c | 5 +++++ src/CL_Wrapper.cpp | 34 +++++++++++++++++++++------------- src/main.cpp | 12 ++++++++++++ 4 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 kernels/minimal_kernel.c diff --git a/include/CL_Wrapper.h b/include/CL_Wrapper.h index d549c5a..99bd728 100644 --- a/include/CL_Wrapper.h +++ b/include/CL_Wrapper.h @@ -37,7 +37,7 @@ public: int create_shared_context(); int create_command_queue(); int compile_kernel(std::string kernel_source, bool is_path, std::string kernel_name); - int set_kernel_arg(cl_kernel kernel, int index, int size, void* buffer, std::string kernel_name); + int set_kernel_arg(std::string kernel_name, int index, std::string buffer_name); int store_buffer(cl_mem, std::string buffer_name); int run_kernel(std::string kernel_name); @@ -45,6 +45,7 @@ public: cl_device_id getDeviceID(); cl_platform_id getPlatformID(); cl_context getContext(); + cl_kernel getKernel(std::string kernel_name); private: diff --git a/kernels/minimal_kernel.c b/kernels/minimal_kernel.c new file mode 100644 index 0000000..4a312ca --- /dev/null +++ b/kernels/minimal_kernel.c @@ -0,0 +1,5 @@ +__kernel void min_kern(__global char* in) +{ + int a = 10; + printf("%s\n", "this is a test string\n"); +} \ No newline at end of file diff --git a/src/CL_Wrapper.cpp b/src/CL_Wrapper.cpp index ba6b3ad..b96d3b1 100644 --- a/src/CL_Wrapper.cpp +++ b/src/CL_Wrapper.cpp @@ -157,10 +157,11 @@ int CL_Wrapper::create_command_queue(){ int CL_Wrapper::compile_kernel(std::string kernel_source, bool is_path, std::string kernel_name) { const char* source; + std::string tmp; if (is_path){ //Load in the kernel, and c stringify it - std::string tmp = read_file(kernel_source); + tmp = read_file(kernel_source); source = tmp.c_str(); } else { source = kernel_source.c_str(); @@ -208,44 +209,51 @@ int CL_Wrapper::compile_kernel(std::string kernel_source, bool is_path, std::str } int CL_Wrapper::set_kernel_arg( - cl_kernel kernel, + std::string kernel_name, int index, - int size, - void* buffer, - std::string kernel_name){ + std::string buffer_name){ + error = clSetKernelArg( + kernel_map.at(kernel_name), + index, + sizeof(cl_mem), + (void *)&buffer_map.at(buffer_name)); + if (assert(error, "clSetKernelArg")) + return -1; + return 0; +} -}; -int CL_Wrapper::store_buffer(cl_mem, std::string buffer_name){ - buffer_map.emplace(std::make_pair(buffer_name, cl_mem)); -}; +int CL_Wrapper::store_buffer(cl_mem buffer, std::string buffer_name){ + buffer_map.emplace(std::make_pair(buffer_name, buffer)); +} int CL_Wrapper::run_kernel(std::string kernel_name){ - WORKER_SIZE = 10; + const int WORKER_SIZE = 1; size_t global_work_size[1] = { WORKER_SIZE }; - cl_mem kernel = kernel_map.at(kernel_name); + cl_kernel kernel = kernel_map.at(kernel_name); error = clEnqueueNDRangeKernel( command_queue, kernel, 1, NULL, global_work_size, NULL, 0, NULL, NULL); - if (assert(error, "clCreateCommandQueue")) + if (assert(error, "clEnqueueNDRangeKernel")) return -1; -}; +} cl_device_id CL_Wrapper::getDeviceID(){ return device_id; }; cl_platform_id CL_Wrapper::getPlatformID(){ return platform_id; }; cl_context CL_Wrapper::getContext(){ return context; }; +cl_kernel CL_Wrapper::getKernel(std::string kernel_name ){ return kernel_map.at(kernel_name); } bool CL_Wrapper::assert(int error_code, std::string function_name){ diff --git a/src/main.cpp b/src/main.cpp index cdb76c4..99b32d1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -41,6 +41,18 @@ int main(){ c.create_shared_context(); c.compile_kernel("../kernels/kernel.txt", true, "hello"); + c.compile_kernel("../kernels/minimal_kernel.c", true, "min_kern"); + + std::string in = "hello!!!!!!!!!!!!!!!!!!!!!"; + cl_mem buff = clCreateBuffer( + c.getContext(), CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, + sizeof(char) * in.size(), &in, NULL); + + c.store_buffer(buff, "buffer_1"); + + //c.set_kernel_arg("min_kern", 0, "buffer_1"); + c.run_kernel("min_kern"); + };