Openholo  v4.2
Open Source Digital Holographic Library
OpenCL.h
Go to the documentation of this file.
1 #pragma once
2 #include <CL/cl.h>
3 #include <sys.h> //for LOG() macro
4 #include <atomic>
5 #include <mutex>
6 #define MAX_KERNEL_NAME 1024
7 #define checkError(E, S) errorCheck(E,S,__FILE__,__LINE__)
8 class OpenCL
9 {
10 private:
11  OpenCL();
12  ~OpenCL();
13  static OpenCL *instance;
14  static std::mutex mutex;
15 
16 public:
17  static OpenCL* getInstance() {
18  std::lock_guard<std::mutex> lock(mutex);
19  if (instance == nullptr) {
20  instance = new OpenCL();
21  atexit(releaseInstance);
22  }
23  return instance;
24  }
25 
26  static void releaseInstance() {
27  if (instance) {
28  delete instance;
29  instance = nullptr;
30  }
31  }
32 
33 private:
34  bool init();
35  bool printDevInfo(cl_device_id device_id);
36 
42  void getKernelName(cl_int iKernel, char *kernel);
43 
44 public:
45  cl_context &getContext() { return context; }
46  cl_command_queue &getCommand() { return commands; }
47  cl_program* getProgram() { return program; }
48  cl_kernel* getKernel() { return kernel; }
49  cl_uint getNumOfKernel() { return nKernel; }
50  size_t getWorkSize() { return work_size; }
51 #if 0
52  bool LoadKernel(char *path);
53 #else
54  bool LoadKernel();
55 #endif
56  void errorCheck(cl_int err, const char *operation, char *filename, int line);
57 
58 
59 private:
60  cl_context context; // compute context
61  cl_command_queue commands; // compute command queue
62  cl_program *program; // compute program
63  cl_kernel *kernel; // compute kernel
64  cl_device_id device_id; // compute device id
65  cl_uint nPlatforms;
66  cl_platform_id *platform;
67  cl_uint nKernel;
68  cl_uint nUnits, nDimensions;
69  size_t group;
70  size_t* item;
71  char* kernel_source;
72  size_t work_size;
73 };
74 std::mutex OpenCL::mutex;
75 OpenCL* OpenCL::instance = nullptr;
void errorCheck(cl_int err, const char *operation, char *filename, int line)
Definition: OpenCL.cpp:150
bool LoadKernel()
Definition: OpenCL.cpp:162
cl_context & getContext()
Definition: OpenCL.h:45
static void releaseInstance()
Definition: OpenCL.h:26
cl_program * getProgram()
Definition: OpenCL.h:47
static OpenCL * getInstance()
Definition: OpenCL.h:17
cl_kernel * getKernel()
Definition: OpenCL.h:48
cl_command_queue & getCommand()
Definition: OpenCL.h:46
size_t getWorkSize()
Definition: OpenCL.h:50
cl_uint getNumOfKernel()
Definition: OpenCL.h:49
Definition: OpenCL.h:8