QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsopenclutils.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsopenclutils.h - QgsOpenClUtils
3 
4  ---------------------
5  begin : 11.4.2018
6  copyright : (C) 2018 by Alessandro Pasotti
7  email : elpaso at itopen dot it
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 #ifndef QGSOPENCLUTILS_H
17 #define QGSOPENCLUTILS_H
18 
19 #define SIP_NO_FILE
20 
21 #define CL_HPP_ENABLE_EXCEPTIONS
22 
23 #include <QtGlobal>
24 #ifdef Q_OS_MAC
25 #define CL_HPP_MINIMUM_OPENCL_VERSION 120
26 #define CL_HPP_TARGET_OPENCL_VERSION 120
27 #define CL_TARGET_OPENCL_VERSION 120
28 #else
29 #define CL_USE_DEPRECATED_OPENCL_1_1_APIS
30 #define CL_HPP_TARGET_OPENCL_VERSION 200
31 #define CL_TARGET_OPENCL_VERSION 200
32 #endif
33 
34 #include <CL/cl2.hpp>
35 
36 #include "qgis_core.h"
37 #include "qgis.h"
38 
39 #include "cpl_conv.h"
40 
73 class CORE_EXPORT QgsOpenClUtils
74 {
75  Q_GADGET
76 
77  public:
78 
83  {
85  Throw
86  };
87 
92  {
93  CPU,
94  GPU,
95  Other
96  };
97 
98  Q_ENUM( HardwareType )
99 
100 
105  enum Info
106  {
107  Name = CL_DEVICE_NAME,
108  Vendor = CL_DEVICE_VENDOR,
109  Version = CL_DEVICE_VERSION,
110  Profile = CL_DEVICE_PROFILE,
111  ImageSupport = CL_DEVICE_IMAGE_SUPPORT,
112  Image2dMaxWidth = CL_DEVICE_IMAGE2D_MAX_WIDTH,
113  Image2dMaxHeight = CL_DEVICE_IMAGE2D_MAX_HEIGHT,
114  MaxMemAllocSize = CL_DEVICE_MAX_MEM_ALLOC_SIZE,
115  Type = CL_DEVICE_TYPE // CPU/GPU etc.
116  };
117 
127  static bool available();
128 
130  static bool enabled();
131 
133  static const std::vector<cl::Device> devices();
134 
142  static cl::Device activeDevice( );
143 
149  static QString activePlatformVersion( );
150 
152  static void storePreferredDevice( const QString deviceId );
153 
155  static QString preferredDevice( );
156 
158  static QString deviceId( const cl::Device device );
159 
163  static QString deviceDescription( const cl::Device device );
164 
168  static QString deviceDescription( const QString deviceId );
169 
171  static void setEnabled( bool enabled );
172 
174  static QString buildLog( cl::BuildError &error );
175 
177  static QString sourceFromPath( const QString &path );
178 
180  static QString sourceFromBaseName( const QString &baseName );
181 
183  static QLatin1String LOGMESSAGE_TAG;
184 
186  static QString errorText( const int errorCode );
187 
194  static cl::CommandQueue commandQueue();
195 
202  Q_DECL_DEPRECATED static cl::Program buildProgram( const cl::Context &context, const QString &source, ExceptionBehavior exceptionBehavior = Catch );
203 
208  static cl::Program buildProgram( const QString &source, ExceptionBehavior exceptionBehavior = Catch );
209 
210 
218  static cl::Context context();
219 
221  static QString sourcePath();
222 
224  static void setSourcePath( const QString &value );
225 
227  static QString activeDeviceInfo( const Info infoType = Info::Name );
228 
230  static QString deviceInfo( const Info infoType, cl::Device device );
231 
236  template <typename T>
238  {
239 
240  public:
241 
242  explicit CPLAllocator( unsigned long size ): mMem( static_cast<T *>( CPLMalloc( sizeof( T ) * size ) ) ) { }
243 
245  {
246  CPLFree( static_cast<void *>( mMem ) );
247  }
248 
249  void reset( T *newData )
250  {
251  if ( mMem )
252  CPLFree( static_cast<void *>( mMem ) );
253  mMem = newData;
254  }
255 
256  void reset( unsigned long size )
257  {
258  reset( static_cast<T *>( CPLMalloc( sizeof( T ) *size ) ) );
259  }
260 
262  {
263  return &mMem[0];
264  }
265 
266  T *release()
267  {
268  T *tmpMem = mMem;
269  mMem = nullptr;
270  return tmpMem;
271  }
272 
273  T &operator[]( const int index )
274  {
275  return mMem[index];
276  }
277 
278  T *get()
279  {
280  return mMem;
281  }
282 
283  private:
284 
285  T *mMem = nullptr;
286  };
287 
288 
289  private:
290 
291  QgsOpenClUtils();
292 
306  static bool activate( const QString &preferredDeviceId = QString() );
307 
311  static void init();
312 
313  static bool sAvailable;
314  static QLatin1String SETTINGS_GLOBAL_ENABLED_KEY;
315  static QLatin1String SETTINGS_DEFAULT_DEVICE_KEY;
316 };
317 
318 
319 
320 #endif // QGSOPENCLUTILS_H
QgsOpenClUtils::CPLAllocator::reset
void reset(T *newData)
Definition: qgsopenclutils.h:249
QgsOpenClUtils::Info
Info
The Info enum maps to OpenCL info constants.
Definition: qgsopenclutils.h:105
QgsOpenClUtils::Catch
@ Catch
Write errors in the message log and silently fail.
Definition: qgsopenclutils.h:84
qgis.h
QgsOpenClUtils::CPLAllocator::reset
void reset(unsigned long size)
Definition: qgsopenclutils.h:256
QgsOpenClUtils::GPU
@ GPU
Definition: qgsopenclutils.h:94
QgsOpenClUtils::HardwareType
HardwareType
The Type enum represent OpenCL device type.
Definition: qgsopenclutils.h:91
QgsOpenClUtils::CPLAllocator
Tiny smart-pointer-like wrapper around CPLMalloc and CPLFree: this is needed because OpenCL C++ API m...
Definition: qgsopenclutils.h:237
QgsOpenClUtils::CPLAllocator::release
T * release()
Definition: qgsopenclutils.h:266
operator*
QgsMargins operator*(const QgsMargins &margins, double factor)
Returns a QgsMargins object that is formed by multiplying each component of the given margins by fact...
Definition: qgsmargins.h:242
QgsOpenClUtils::CPU
@ CPU
Definition: qgsopenclutils.h:93
QgsOpenClUtils::ExceptionBehavior
ExceptionBehavior
The ExceptionBehavior enum define how exceptions generated by OpenCL should be treated.
Definition: qgsopenclutils.h:82
QgsOpenClUtils::CPLAllocator::get
T * get()
Definition: qgsopenclutils.h:278
QgsOpenClUtils
The QgsOpenClUtils class is responsible for common OpenCL operations such as.
Definition: qgsopenclutils.h:73
QgsOpenClUtils::CPLAllocator::~CPLAllocator
~CPLAllocator()
Definition: qgsopenclutils.h:244
QgsOpenClUtils::LOGMESSAGE_TAG
static QLatin1String LOGMESSAGE_TAG
OpenCL string for message logs.
Definition: qgsopenclutils.h:183
QgsOpenClUtils::CPLAllocator::operator[]
T & operator[](const int index)
Definition: qgsopenclutils.h:273
QgsOpenClUtils::CPLAllocator::CPLAllocator
CPLAllocator(unsigned long size)
Definition: qgsopenclutils.h:242