HOPS
HOPS class reference
MHO_OpenCLInterface.hh
Go to the documentation of this file.
1 #ifndef HOPS_OPENCLINTERFACE_DEF
2 #define HOPS_OPENCLINTERFACE_DEF
3 
4 #include "MHO_OpenCLConfig.hh"
5 
6 #ifdef HOPS_USE_CL_VECTOR
7  #define __NO_STD_VECTOR // Use cl::vector instead of STL version
8  #define CL_VECTOR_TYPE cl::vector
9 #else
10  #define CL_VECTOR_TYPE std::vector
11 #endif
12 
13 #define __CL_ENABLE_EXCEPTIONS
14 
15 #include "MHO_Message.hh"
17 
18 //shut up the annoying unused variable warnings in cl.hpp for clang/gcc
19 //by using a system-header wrapper for the opencl headers
21 #include <complex>
22 #include <map>
23 #include <string>
24 #include <vector>
25 
26 template< typename XValueType > struct MHO_OpenCLTypeMap
27 {
28  using mapped_type = XValueType;
29 };
30 
31 #ifdef HOPS_USE_DOUBLE_PRECISION
32 
33  #define CL_TYPE cl_double
34  #define CL_TYPE2 cl_double2
35  #define CL_TYPE4 cl_double4
36  #define CL_TYPE8 cl_double8
37  #define CL_TYPE16 cl_double16
38 
39 //we map doubles to CL_TYPE and complex doubles on to cl_double2
40 template<> struct MHO_OpenCLTypeMap< double >
41 {
42  using mapped_type = CL_TYPE;
43 };
44 
45 template<> struct MHO_OpenCLTypeMap< std::complex< double > >
46 {
47  using mapped_type = CL_TYPE2;
48 };
49 
50 #else
51 
52 TODO_FIXME_MSG("Warning : MHO_OpenCLPlugin is being built with float precision!")
53  #define CL_TYPE cl_float
54  #define CL_TYPE2 cl_float2
55  #define CL_TYPE4 cl_float4
56  #define CL_TYPE8 cl_float8
57  #define CL_TYPE16 cl_float16
58 
59 //we map floats to CL_TYPE and complex floats on to cl_float2
60 template<> struct MHO_OpenCLTypeMap< float >
61 {
63 };
64 
65 template<> struct MHO_OpenCLTypeMap< std::complex< float > >
66 {
68 };
69 
70 #endif
71 
72 //this is necessary on some intel devices
73 #define ENFORCE_CL_FINISH
74 
75 //the following are optional defines for debugging
76 
77 //adds verbose output of kernel build logs
78 //#define DEBUG_OPENCL_COMPILER_OUTPUT
79 
80 //add try-catch for opencl exceptions
81 #define USE_CL_ERROR_TRY_CATCH
82 
83 #ifdef USE_CL_ERROR_TRY_CATCH
84  #include <iostream>
85  #define CL_ERROR_TRY \
86  try \
87  {
88 #else
89  #define CL_ERROR_TRY
90 #endif
91 
92 #ifdef USE_CL_ERROR_TRY_CATCH
93  #define CL_ERROR_CATCH \
94  } \
95  catch(cl::Error error) \
96  { \
97  std::cout << "OpenCL Exception caught: " << std::endl; \
98  std::cout << __FILE__ << ":" << __LINE__ << std::endl; \
99  std::cout << error.what() << "(" << error.err() \
100  << ") = " << MHO_OpenCLInterface::GetInstance()->GetErrorMessage(error.err()) << std::endl; \
101  std::exit(1); \
102  }
103 #else
104  #define CL_ERROR_CATCH
105 #endif
106 
107 namespace hops
108 {
109 
111 {
112  public:
114 
115  cl::Context GetContext() const { return *fContext; }
116 
117  CL_VECTOR_TYPE< cl::Device > GetDevices() const { return fDevices; }
118 
119  cl::Device GetDevice() const { return fDevices[fCLDeviceID]; }
120 
121  cl::CommandQueue& GetQueue(int i = -1) const;
122 
123  unsigned int GetNumberOfDevices() const
124  {
125  CL_VECTOR_TYPE< cl::Device > availableDevices = fContext->getInfo< CL_CONTEXT_DEVICES >();
126  return availableDevices.size();
127  };
128 
129  void SetGPU(unsigned int i);
130 
131  void SetKernelPath(std::string s) { fKernelPath = s; }
132 
133  std::string GetKernelPath() const { return fKernelPath; }
134 
135  std::string GetErrorMessage(int code) { return fOpenCLCode2ErrorMap[code]; }
136 
137  protected:
139  virtual ~MHO_OpenCLInterface();
140 
141  void InitializeOpenCL();
142  void FillErrorCodeMaps();
143 
145 
146  std::string fKernelPath;
147 
148  CL_VECTOR_TYPE< cl::Platform > fPlatforms;
149  CL_VECTOR_TYPE< cl::Device > fDevices;
150  unsigned int fCLDeviceID;
151  cl::Context* fContext;
152  mutable std::vector< cl::CommandQueue* > fQueues;
153 
154  std::map< std::string, int > fOpenCLError2CodeMap;
155  std::map< int, std::string > fOpenCLCode2ErrorMap;
156 };
157 
158 } // namespace hops
159 
160 #endif
#define TODO_FIXME_MSG(x)
Definition: MHO_Message.hh:35
XValueType mapped_type
Definition: MHO_OpenCLInterface.hh:28
CL_TYPE mapped_type
Definition: MHO_OpenCLInterface.hh:62
CL_TYPE2 mapped_type
Definition: MHO_OpenCLInterface.hh:67
#define CL_TYPE
Definition: MHO_OpenCLInterface.hh:53
#define CL_TYPE2
Definition: MHO_OpenCLInterface.hh:54
Definition: MHO_OpenCLInterface.hh:27
Definition: MHO_OpenCLInterface.hh:111
std::map< int, std::string > fOpenCLCode2ErrorMap
Definition: MHO_OpenCLInterface.hh:155
unsigned int GetNumberOfDevices() const
Definition: MHO_OpenCLInterface.hh:123
CL_VECTOR_TYPE< cl::Device > fDevices
Definition: MHO_OpenCLInterface.hh:149
void SetKernelPath(std::string s)
Definition: MHO_OpenCLInterface.hh:131
std::string fKernelPath
Definition: MHO_OpenCLInterface.hh:146
CL_VECTOR_TYPE< cl::Device > GetDevices() const
Definition: MHO_OpenCLInterface.hh:117
CL_VECTOR_TYPE< cl::Platform > fPlatforms
Definition: MHO_OpenCLInterface.hh:148
unsigned int fCLDeviceID
Definition: MHO_OpenCLInterface.hh:150
std::map< std::string, int > fOpenCLError2CodeMap
Definition: MHO_OpenCLInterface.hh:154
MHO_OpenCLInterface()
Definition: MHO_OpenCLInterface.cc:17
std::string GetKernelPath() const
Definition: MHO_OpenCLInterface.hh:133
cl::Device GetDevice() const
Definition: MHO_OpenCLInterface.hh:119
cl::Context * fContext
Definition: MHO_OpenCLInterface.hh:151
void InitializeOpenCL()
Definition: MHO_OpenCLInterface.cc:44
cl::CommandQueue & GetQueue(int i=-1) const
Definition: MHO_OpenCLInterface.cc:125
virtual ~MHO_OpenCLInterface()
Definition: MHO_OpenCLInterface.cc:22
static MHO_OpenCLInterface * GetInstance()
Definition: MHO_OpenCLInterface.cc:32
void FillErrorCodeMaps()
Definition: MHO_OpenCLInterface.cc:133
std::vector< cl::CommandQueue * > fQueues
Definition: MHO_OpenCLInterface.hh:152
cl::Context GetContext() const
Definition: MHO_OpenCLInterface.hh:115
static MHO_OpenCLInterface * fOpenCLInterface
Definition: MHO_OpenCLInterface.hh:144
std::string GetErrorMessage(int code)
Definition: MHO_OpenCLInterface.hh:135
void SetGPU(unsigned int i)
Definition: MHO_OpenCLInterface.cc:89
Definition: MHO_ChannelLabeler.hh:17