Accelerated Computation Engine
All Classes Namespaces Functions Variables Enumerations Enumerator Friends
opencl_kernel.h
1 #ifndef OPENCL_KERNEL_H
2 #define OPENCL_KERNEL_H
3 #include <CL/cl.h>
4 #include <QObject>
5 #include "opencl_buffer.h"
6 #include "opencl.h"
7 #include "edebug.h"
8 //
9 
10 
11 
12 namespace OpenCL
13 {
26  class Kernel : public QObject
27  {
28  Q_OBJECT
29  public:
30  virtual ~Kernel() override;
31  Event execute(CommandQueue* queue);
32  protected:
33  class Locker;
34  explicit Kernel(Program* program, const QString& name, QObject* parent = nullptr);
36  void unlock();
37  int maxWorkGroupSize(Device* device) const;
38  int workGroupMultiple(Device* device) const;
39  void setDimensions(cl_uint size);
40  void setSizes(cl_uint dimension, qint64 globalSize, qint64 localSize);
41  template<class T> void setArgument(cl_uint index, T value);
42  template<class T> void setBuffer(cl_uint index, Buffer<T>* buffer);
43  template<class T> void setLocalMemory(cl_uint index, qint64 size);
44  private:
45  void allocate();
46  void clear();
50  cl_kernel _id;
54  cl_uint _size {1};
59  size_t* _offsets {nullptr};
64  size_t* _globalSizes {nullptr};
69  size_t* _localSizes {nullptr};
74  QMutex _lock;
79  bool _isLocked {false};
80  };
81 
82 
83 
84 
85 
86 
98  template<class T> void Kernel::setArgument(cl_uint index, T value)
99  {
100  EDEBUG_FUNC(this,index,value)
101 
102  // If this kernel is not locked then throw an exception, else go to the next step.
103  if ( !_isLocked )
104  {
105  E_MAKE_EXCEPTION(e);
106  e.setTitle(tr("Logic Error"));
107  e.setDetails(tr("Cannot set OpenCL kernel parameters without locking the object first."));
108  throw e;
109  }
110 
111  // Set the kernel argument with the given index to the given value. If setting the
112  // argument fails then throw an exception.
113  cl_int code {clSetKernelArg(_id,index,sizeof(T),&value)};
114  if ( code != CL_SUCCESS )
115  {
116  E_MAKE_EXCEPTION(e);
117  fillException(&e,code);
118  throw e;
119  }
120  }
121 
122 
123 
124 
125 
126 
138  template<class T> void Kernel::setBuffer(cl_uint index, Buffer<T>* buffer)
139  {
140  EDEBUG_FUNC(this,index,buffer)
141 
142  // If this kernel is not locked then throw an exception, else go to the next step.
143  if ( !_isLocked )
144  {
145  E_MAKE_EXCEPTION(e);
146  e.setTitle(tr("Logic Error"));
147  e.setDetails(tr("Cannot set OpenCL kernel parameters without locking the object first."));
148  throw e;
149  }
150 
151  // Set the kernel argument with the given index to the given OpenCL buffer. If
152  // setting the argument fails then throw an exception.
153  cl_mem id {buffer->id()};
154  cl_int code {clSetKernelArg(_id,index,sizeof(cl_mem),&id)};
155  if ( code != CL_SUCCESS )
156  {
157  E_MAKE_EXCEPTION(e);
158  fillException(&e,code);
159  throw e;
160  }
161  }
162 
163 
164 
165 
166 
167 
181  template<class T> void Kernel::setLocalMemory(cl_uint index, qint64 size)
182  {
183  EDEBUG_FUNC(this,index,size)
184 
185  // If this kernel is not locked or the given size is less than one then throw an
186  // exception, else go to the next step.
187  if ( !_isLocked )
188  {
189  E_MAKE_EXCEPTION(e);
190  e.setTitle(tr("Logic Error"));
191  e.setDetails(tr("Cannot set OpenCL kernel parameters without locking the object first."));
192  throw e;
193  }
194  if ( size < 1 )
195  {
196  E_MAKE_EXCEPTION(e);
197  e.setTitle(tr("Invalid Argument"));
198  e.setDetails(tr("Cannot set local memory argument of OpenCL kernel with a size of less than 1."));
199  throw e;
200  }
201 
202  // Set the kernel argument with the given index to allocate the given amount of
203  // local memory. If setting the argument fails then throw an exception.
204  cl_int code {clSetKernelArg(_id,index,sizeof(T)*size,nullptr)};
205  if ( code != CL_SUCCESS )
206  {
207  E_MAKE_EXCEPTION(e);
208  fillException(&e,code);
209  throw e;
210  }
211  }
212 }
213 
214 
215 
216 #endif
void setSizes(cl_uint dimension, qint64 globalSize, qint64 localSize)
Definition: opencl_kernel.cpp:303
virtual ~Kernel() override
Definition: opencl_kernel.cpp:24
Kernel::Locker lock()
Definition: opencl_kernel.cpp:116
Definition: opencl_buffer.h:27
void setDimensions(cl_uint size)
Definition: opencl_kernel.cpp:251
Definition: opencl_commandqueue.h:18
Definition: opencl_event.h:18
Definition: opencl_kernel.h:26
Definition: opencl_kernel_locker.h:17
void setLocalMemory(cl_uint index, qint64 size)
Definition: opencl_kernel.h:181
cl_mem id() const
Definition: opencl_buffer.h:286
Definition: opencl_device.h:19
void fillException(EException *exception, cl_int code)
Definition: opencl_common.cpp:24
Kernel(Program *program, const QString &name, QObject *parent=nullptr)
Definition: opencl_kernel.cpp:85
int workGroupMultiple(Device *device) const
Definition: opencl_kernel.cpp:205
void setBuffer(cl_uint index, Buffer< T > *buffer)
Definition: opencl_kernel.h:138
void unlock()
Definition: opencl_kernel.cpp:136
int maxWorkGroupSize(Device *device) const
Definition: opencl_kernel.cpp:158
Definition: opencl_program.h:19
Definition: opencl.h:5
void setArgument(cl_uint index, T value)
Definition: opencl_kernel.h:98
Event execute(CommandQueue *queue)
Definition: opencl_kernel.cpp:47