diff --git a/libs/main/opencl/piopencl.h b/libs/main/opencl/piopencl.h index 5c531054..a9704821 100644 --- a/libs/main/opencl/piopencl.h +++ b/libs/main/opencl/piopencl.h @@ -133,10 +133,12 @@ public: friend class Kernel; public: ~Context(); + void * handle(); + void * queue(); static Context * create(const DeviceList & dl); static Context * create(const Device & d) {return create(DeviceList() << d);} static Context * create(const PIString & part_name); - Program * createProgram(const PIString & source, PIString * error = 0); + Program * createProgram(const PIString & source, const PIStringList & args = PIStringList(), PIString * error = 0); template Buffer * createBuffer(PIOpenCL::Direction dir, PIVector & container) { T def = T(); return createBuffer(dir, &container, Buffer::cVector , PIByteArray(&def, sizeof(T)), container.size()); @@ -173,10 +175,14 @@ public: friend class Kernel; public: ~Buffer(); + void * handle(); bool resize(uint new_elements); void clear(); void copyToContainer(); + void copyTo(void * data); void copyFromContainer(); + void copyFrom(void * data); + uint elementsCount() const {return elements;} private: enum Container { cNone, @@ -204,6 +210,7 @@ public: friend class Buffer; public: ~Program(); + Context * context() const {return context_;} const PIString & sourceCode() const {return source_;} Kernel * kernel(int index = 0) const {return kernels_[index];} const PIVector & kernels() const {return kernels_;} @@ -222,6 +229,7 @@ public: friend class Program; friend class Buffer; public: + Program * program() const {return program_;} bool execute(); void setExecuteRange(int size) {setExecuteRanges(PIVector() << size);} void setExecuteRanges(const PIVector & ranges); diff --git a/libs/opencl/piopencl.cpp b/libs/opencl/piopencl.cpp index 40ea7e19..5ac9dad8 100644 --- a/libs/opencl/piopencl.cpp +++ b/libs/opencl/piopencl.cpp @@ -1,4 +1,5 @@ #include "piopencl.h" +#include "piresources.h" #define CL_USE_DEPRECATED_OPENCL_1_2_APIS #define CL_USE_DEPRECATED_OPENCL_2_0_APIS #ifdef MAC_OS @@ -12,6 +13,7 @@ PRIVATE_DEFINITION_START(PIOpenCL::Context) cl_context context; cl_command_queue queue; PIVector devices; + PIString complex_src; PRIVATE_DEFINITION_END(PIOpenCL::Context) @@ -139,6 +141,7 @@ void PIOpenCL::Initializer::init() { PIOpenCL::Context::Context() { + PRIVATE->complex_src = PIResources::get("3rd/clcomplex.h") + "\n"; zero(); } @@ -155,6 +158,16 @@ PIOpenCL::Context::~Context() { } +void * PIOpenCL::Context::handle() { + return PRIVATE->context; +} + + +void * PIOpenCL::Context::queue() { + return PRIVATE->queue; +} + + void PIOpenCL::Context::zero() { programs_.clear(); buffers_.clear(); @@ -223,14 +236,15 @@ PIOpenCL::Context * PIOpenCL::Context::create(const PIString & part_name) { } -PIOpenCL::Program * PIOpenCL::Context::createProgram(const PIString & source, PIString * error) { +PIOpenCL::Program * PIOpenCL::Context::createProgram(const PIString & source, const PIStringList & args, PIString * error) { if (error) error->clear(); if (source.isEmpty()) { if (error) (*error) = "Empty program!"; return 0; } - const char * csrc = source.dataAscii(); - size_t src_size = source.size(); + PIString src_text = PRIVATE->complex_src + source; + const char * csrc = src_text.dataAscii(); + size_t src_size = src_text.size(); cl_int ret = 0; cl_program prog = clCreateProgramWithSource(PRIVATE->context, 1, &csrc, &src_size, &ret); if (ret != 0) { @@ -238,7 +252,8 @@ PIOpenCL::Program * PIOpenCL::Context::createProgram(const PIString & source, PI if (error) (*error) << "clCreateProgramWithSource error " << ret; return 0; } - ret = clBuildProgram(prog, 0, 0, "-cl-kernel-arg-info", 0, 0); + PIString carg = (PIStringList(args) << "-cl-kernel-arg-info").join(' '); + ret = clBuildProgram(prog, 0, 0, carg.dataAscii(), 0, 0); char buffer[10240]; clGetProgramBuildInfo(prog, PRIVATE->devices[0], CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, 0); if (ret != 0) { @@ -325,6 +340,11 @@ PIOpenCL::Buffer::~Buffer() { } +void * PIOpenCL::Buffer::handle() { + return PRIVATE->buffer; +} + + void PIOpenCL::Buffer::zero() { type = cNone; container = 0; @@ -375,7 +395,13 @@ void PIOpenCL::Buffer::clear() { void PIOpenCL::Buffer::copyToContainer() { if (!PRIVATE->buffer || !container) return; - cl_int ret = clEnqueueReadBuffer(context_->PRIVATEWB->queue, PRIVATE->buffer, CL_TRUE, 0, elements * def.size(), containerData(), 0, 0, 0); + copyTo(containerData()); +} + + +void PIOpenCL::Buffer::copyTo(void * data) { + if (!PRIVATE->buffer) return; + cl_int ret = clEnqueueReadBuffer(context_->PRIVATEWB->queue, PRIVATE->buffer, CL_TRUE, 0, elements * def.size(), data, 0, 0, 0); if (ret != 0) { piCout << "[PIOpenCL::Buffer]" << "clEnqueueReadBuffer error" << ret; } @@ -384,7 +410,13 @@ void PIOpenCL::Buffer::copyToContainer() { void PIOpenCL::Buffer::copyFromContainer() { if (!PRIVATE->buffer || !container) return; - cl_int ret = clEnqueueWriteBuffer(context_->PRIVATEWB->queue, PRIVATE->buffer, CL_TRUE, 0, elements * def.size(), containerData(), 0, 0, 0); + copyFrom(containerData()); +} + + +void PIOpenCL::Buffer::copyFrom(void * data) { + if (!PRIVATE->buffer) return; + cl_int ret = clEnqueueWriteBuffer(context_->PRIVATEWB->queue, PRIVATE->buffer, CL_TRUE, 0, elements * def.size(), data, 0, 0, 0); if (ret != 0) { piCout << "[PIOpenCL::Buffer]" << "clEnqueueWriteBuffer error" << ret; }