cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

berathebrain
Journeyman III

OpenCL kernel execution question

Hi. I have a problem.

Let's say that I have some class OpenCLHelper that holds openclcontext and all other things that are needed to start a kernel.

Now I have some other class, ReductionKernel that recieves OpenCLHelper as a parameter in a constructor, so I can reuse that OpenCL context and kernels over again. When I first run the Reduction kernel everything is fine, but when that Object is destroyed and I make another ReductionKernel class, then the data is wrong. Could you guys know if I could even do this(reuse the kernel program, openclcontext and so on). I think that I could do this but then again, I'm not sure where the error lies.

I'm posting my .h classes here. If you want even the .cpp files I can attach them too.

Thanks.

/* * OpenCLHelper.h * * Created on: Jan 3, 2010 * Author: Dejan */ #ifndef OPENCLHELPER_H_ #define OPENCLHELPER_H_ #define __CL_ENABLE_EXCEPTIONS #define __NO_STD_VECTOR #include <iostream> #include <algorithm> #include <iomanip> #include <map> #include <string> #include <fstream> #include <iostream> #include <vector> #include <math.h> #include <CL/cl.hpp> using std::cout; using std::cerr; using std::endl; using std::setw; using std::string; using std::ifstream; class OpenCLHelper { private: OpenCLHelper(); void OpenCLHelperInitialize(cl_device_type deviceType, std::string kernelFileName); void InitCL(); void InitKernelNames(std::string sourceStr); void TrimKernelName(string kernelName); void GetOpenCLContextProperties(); cl_context_properties contextProperties[3]; std::string FileToString(const string fileName); std::string kernelFile; //File that contains all the kernels //Vector to store all kernels std::vector<std::string> kernelNames; int nTotalKernels; //kernelNames.size() public: size_t maxWorkgroupSize; cl_ulong maxDevMemory; cl_device_type deviceType; cl::Context context; cl::vector<cl::Kernel> kernel; std::map<string, cl::Kernel> kernels; cl::CommandQueue queue; cl::Program program; cl::vector<cl::Device> devices; OpenCLHelper(cl_device_type deviceType, std::string kernelFileName); OpenCLHelper(std::string kernelFileName); cl::Kernel Kernel(int i); cl::Kernel Kernel(string kernelName); int IndexOfKernel(string kernelName); void RecreateKernels(); virtual ~OpenCLHelper(); }; #endif /* OPENCLHELPER_H_ */ /* * OpenCLManager.h * * Created on: Jan 10, 2010 * Author: Dejan */ #ifndef OPENCLMANAGER_H_ #define OPENCLMANAGER_H_ #define LUMINANCE_KERNELS "kernels\\OpenCLKernels.cl" #include "OpenCLHelper.h" class OpenCLManager { private: OpenCLManager(); OpenCLManager(const OpenCLManager&); OpenCLManager& operator=(const OpenCLManager&); static OpenCLManager* instance; OpenCLHelper* openCLHelper; public: static OpenCLManager* Instance(); OpenCLHelper* GetOpenCLHelper(); virtual ~OpenCLManager(); }; #endif /* OPENCLMANAGER_H_ */ /* * ReductionKernel * * Created on: Jan 3, 2010 * Author: Dejan Beric */ #ifndef REDUCTIONKERNEL_H_ #define REDUCTIONKERNEL_H_ #define __MSVCRT_VERSION__ 0x0700 #define REDUCTION_KERNEL_NAME "reduce" #include "OpenCLHelper.h" #include <malloc.h> #include <math.h> ///////////////////////////////////////////////////////////////// // Macros ///////////////////////////////////////////////////////////////// #define FREE(ptr, free_val) \ if (ptr != free_val) \ { \ free(ptr); \ ptr = free_val; \ } class ReductionKernel { private: OpenCLHelper* openCLHelper; cl::Event event; int length; float *reductionInput; float *reductionOutput; float *reductionLocal; cl::Buffer *reductionInputCL; cl::Buffer *reductionOutputCL; cl::Buffer *reductionLocalCL; int reductionKernelWorkgroupSize; void InitReductionKernelCLBuffers(float *reductionInput); void InitReductionKernelHostBuffers(); void SetReductionKernelArgs(); template<typename T> bool DumpBuffer(cl::CommandQueue& queue, cl::Buffer& buffer, std::vector<T>& data); void EnqueueKernel(int nKernel, bool bBlocking = false); ReductionKernel(); void RunCL(int nKernel); void RunCL(string kernelName); void ReleaseHostBuffers(); public: ReductionKernel(OpenCLHelper* openCLHelper, int width, int height); ReductionKernel(OpenCLHelper* openCLHelper, int len); virtual ~ReductionKernel(); void ExecuteReductionKernel(float *reductionInput, float &sum); }; #endif /* REDUCTIONKERNEL_H_ */

0 Likes
1 Reply
gaurav_garg
Adept I

Yes, you can re-use context, kernel objects etc. without any problem. If you can post your cpp code, we can try to find out the problem.

0 Likes