CLOGS
C++ library for sorting and searching in OpenCL applications
core.h
Go to the documentation of this file.
1 /* Copyright (c) 2012-2013 University of Cape Town
2  * Copyright (c) 2014, Bruce Merry
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
29 #ifndef CLOGS_CORE_H
30 #define CLOGS_CORE_H
31 
32 #include <clogs/visibility_push.h>
33 #include <CL/cl.hpp>
34 #include <stdexcept>
35 #include <string>
36 #include <vector>
37 #include <clogs/visibility_pop.h>
38 #include <clogs/platform.h>
39 
47 namespace clogs
48 {
49 
51 #define CLOGS_VERSION_MAJOR 1
52 #define CLOGS_VERSION_MINOR 5
54 
58 class CLOGS_API InternalError : public std::runtime_error
59 {
60 public:
61  InternalError(const std::string &msg);
62 };
63 
67 class CLOGS_API CacheError : public InternalError
68 {
69 public:
70  CacheError(const std::string &msg);
71 };
72 
76 class CLOGS_API TuneError : public InternalError
77 {
78 public:
79  TuneError(const std::string &msg);
80 };
81 
82 
83 #ifdef __CL_ENABLE_EXCEPTIONS
84 typedef cl::Error Error;
85 #else
86 class CLOGS_LOCAL Error
87 {
88 private:
89  cl_int err_;
90  const char *errStr_;
91 
92 public:
93  explicit Error(cl_int err, const char *errStr = NULL) : err_(err), errStr_(errStr)
94  {
95  }
96 
97  virtual const char *what() const
98  {
99  return errStr_ != NULL ? errStr_ : "";
100  }
101 
102  cl_int err() const
103  {
104  return err_;
105  }
106 };
107 #endif // !__CL_ENABLE_EXCEPTIONS
108 
112 enum CLOGS_API BaseType
113 {
114  TYPE_VOID,
115  TYPE_UCHAR,
116  TYPE_CHAR,
117  TYPE_USHORT,
118  TYPE_SHORT,
119  TYPE_UINT,
120  TYPE_INT,
121  TYPE_ULONG,
122  TYPE_LONG,
123  TYPE_HALF,
124  TYPE_FLOAT,
125  TYPE_DOUBLE
126 };
127 
134 class CLOGS_API Type
135 {
136 private:
137  BaseType baseType;
138  unsigned int length;
139 
140 public:
142  Type();
149  Type(BaseType baseType, unsigned int length = 1);
150 
155  bool isStorable(const cl::Device &device) const;
156 
160  bool isComputable(const cl::Device &device) const;
161  bool isIntegral() const;
162  bool isSigned() const;
163  std::string getName() const;
164  ::size_t getSize() const;
165  ::size_t getBaseSize() const;
166 
168  BaseType getBaseType() const;
170  unsigned int getLength() const;
171 
173  static std::vector<Type> allTypes();
174 };
175 
176 namespace detail
177 {
178 
186 static inline void handleError(cl_int err, const char *errStr)
187 {
188  if (err != CL_SUCCESS)
189  throw Error(err, errStr);
190 }
191 
197 template<typename T, bool Thin = sizeof(T) == sizeof(typename T::cl_type)>
199 {
200 };
201 
202 template<typename T>
203 class UnwrapArray<T, false>
204 {
205 private:
206  std::vector<typename T::cl_type> raw;
207 
208 public:
209  explicit UnwrapArray(const VECTOR_CLASS<T> *in)
210  {
211  if (in != NULL)
212  {
213  raw.reserve(in->size());
214  for (std::size_t i = 0; i < in->size(); i++)
215  raw.push_back((*in)[i]());
216  }
217  }
218 
219  cl_uint size() const { return raw.size(); }
220  const typename T::cl_type *data() const { return raw.empty() ? NULL : &raw[0]; }
221 };
222 
223 template<typename T>
224 class UnwrapArray<T, true>
225 {
226 private:
227  const typename T::cl_type *data_;
228  std::size_t size_;
229 
230 public:
231  explicit UnwrapArray(const VECTOR_CLASS<T> *in)
232  {
233  if (in != NULL && !in->empty())
234  {
235  data_ = reinterpret_cast<const typename T::cl_type *>(&(*in)[0]);
236  size_ = in->size();
237  }
238  else
239  {
240  data_ = NULL;
241  size_ = 0;
242  }
243  }
244 
245  cl_uint size() const { return size_; }
246  const typename T::cl_type *data() const { return data_; }
247 };
248 
249 struct CLOGS_LOCAL CallbackWrapper
250 {
251  void (CL_CALLBACK *callback)(const cl::Event &, void *);
252  void (CL_CALLBACK *free)(void *);
253  void *userData;
254 };
255 
256 static inline void CL_CALLBACK callbackWrapperCall(cl_event event, void *userData)
257 {
258  CallbackWrapper *wrapper = reinterpret_cast<CallbackWrapper *>(userData);
259  clRetainEvent(event); // because cl::Event constructor steals a ref
260  wrapper->callback(cl::Event(event), wrapper->userData);
261 }
262 
263 static inline void CL_CALLBACK callbackWrapperFree(void *userData)
264 {
265  CallbackWrapper *wrapper = reinterpret_cast<CallbackWrapper *>(userData);
266  if (wrapper->free)
267  wrapper->free(wrapper->userData);
268  delete wrapper;
269 }
270 
271 static inline CallbackWrapper *makeCallbackWrapper(
272  void (CL_CALLBACK *callback)(const cl::Event &, void *),
273  void *userData,
274  void (CL_CALLBACK *free)(void *))
275 {
276  CallbackWrapper *wrapper = new CallbackWrapper();
277  wrapper->callback = callback;
278  wrapper->userData = userData;
279  wrapper->free = free;
280  return wrapper;
281 }
282 
283 template<typename T>
284 static inline void CL_CALLBACK genericCallbackCall(cl_event event, void *userData)
285 {
286  T *self = reinterpret_cast<T *>(userData);
287  clRetainEvent(event); // because cl::Event constructor steals a ref
288  (*self)(cl::Event(event));
289 }
290 
291 template<typename T>
292 static inline void CL_CALLBACK genericCallbackFree(void *userData)
293 {
294  T *self = reinterpret_cast<T *>(userData);
295  delete self;
296 }
297 
298 class Algorithm;
299 
300 } // namespace detail
301 
305 class CLOGS_API Algorithm
306 {
307 private:
308  detail::Algorithm *detail_;
309 
310  /* Prevent copying */
311  Algorithm(const Algorithm &) CLOGS_DELETE_FUNCTION;
312  Algorithm &operator=(const Algorithm &) CLOGS_DELETE_FUNCTION;
313 
314 protected:
315  Algorithm();
316  ~Algorithm();
317 
319  void moveConstruct(Algorithm &other);
321  detail::Algorithm *moveAssign(Algorithm &other);
323  void swap(Algorithm &other);
324 
326  detail::Algorithm *getDetail() const;
332  detail::Algorithm *getDetailNonNull() const;
337  void setDetail(detail::Algorithm *ptr);
338 
339 public:
356  void (CL_CALLBACK *callback)(const cl::Event &, void *),
357  void *userData,
358  void (CL_CALLBACK *free)(void *) = NULL)
359  {
360  setEventCallback(
361  detail::callbackWrapperCall,
362  detail::makeCallbackWrapper(callback, userData, free),
363  detail::callbackWrapperFree);
364  }
365 
372  template<typename T>
373  void setEventCallback(const T &callback)
374  {
375  setEventCallback(
376  detail::genericCallbackCall<T>,
377  new T(callback),
378  detail::genericCallbackFree<T>);
379  }
380 
382  void setEventCallback(
383  void (CL_CALLBACK *callback)(cl_event, void *),
384  void *userData,
385  void (CL_CALLBACK *free)(void *) = NULL);
386 };
387 
388 } // namespace clogs
389 
390 #endif /* !CLOGS_CORE_H */
Exception thrown when a configuration could not be tuned at all.
Definition: core.h:76
void setEventCallback(const T &callback)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: core.h:373
Pops default visibility if appropriate.
BaseType
Enumeration of scalar types supported by OpenCL C which can be stored in a buffer.
Definition: core.h:112
Exception thrown on internal errors that are not the user&#39;s fault.
Definition: core.h:58
Pushes default visibility if appropriate.
Encapsulation of an OpenCL built-in type that can be stored in a buffer.
Definition: core.h:134
Exception thrown when the autotuning cache could not be read.
Definition: core.h:67
Creates an array of handles from a vector of wrappers.
Definition: core.h:198
Detects compiler properties.
Definition: core.h:249
Definition: core.h:86
void setEventCallback(void(*callback)(const cl::Event &, void *), void *userData, void(*free)(void *)=NULL)
Set a callback function that will receive a list of all underlying events.
Definition: core.h:355
static void handleError(cl_int err, const char *errStr)
Throws an exception if a non-success error code is given.
Definition: core.h:186
Base class for all algorithm classes.
Definition: core.h:305
OpenCL primitives.
Definition: clogs.h:56