QGIS API Documentation  3.6.0-Noosa (5873452)
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 220
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 
72 class CORE_EXPORT QgsOpenClUtils
73 {
74  Q_GADGET
75 
76  public:
77 
82  {
84  Throw
85  };
86 
91  {
92  CPU,
93  GPU,
94  Other
95  };
96 
97  Q_ENUM( HardwareType )
98 
99 
104  enum Info
105  {
106  Name = CL_DEVICE_NAME,
107  Vendor = CL_DEVICE_VENDOR,
108  Version = CL_DEVICE_VERSION,
109  Profile = CL_DEVICE_PROFILE,
110  ImageSupport = CL_DEVICE_IMAGE_SUPPORT,
111  Image2dMaxWidth = CL_DEVICE_IMAGE2D_MAX_WIDTH,
112  Image2dMaxHeight = CL_DEVICE_IMAGE2D_MAX_HEIGHT,
113  MaxMemAllocSize = CL_DEVICE_MAX_MEM_ALLOC_SIZE,
114  Type = CL_DEVICE_TYPE // CPU/GPU etc.
115  };
116 
126  static bool available();
127 
129  static bool enabled();
130 
132  static const std::vector<cl::Device> devices();
133 
141  static cl::Device activeDevice( );
142 
148  static QString activePlatformVersion( );
149 
151  static void storePreferredDevice( const QString deviceId );
152 
154  static QString preferredDevice( );
155 
157  static QString deviceId( const cl::Device device );
158 
162  static QString deviceDescription( const cl::Device device );
163 
167  static QString deviceDescription( const QString deviceId );
168 
170  static void setEnabled( bool enabled );
171 
173  static QString buildLog( cl::BuildError &error );
174 
176  static QString sourceFromPath( const QString &path );
177 
179  static QString sourceFromBaseName( const QString &baseName );
180 
182  static QLatin1String LOGMESSAGE_TAG;
183 
185  static QString errorText( const int errorCode );
186 
193  static cl::CommandQueue commandQueue();
194 
201  Q_DECL_DEPRECATED static cl::Program buildProgram( const cl::Context &context, const QString &source, ExceptionBehavior exceptionBehavior = Catch );
202 
207  static cl::Program buildProgram( const QString &source, ExceptionBehavior exceptionBehavior = Catch );
208 
209 
217  static cl::Context context();
218 
220  static QString sourcePath();
221 
223  static void setSourcePath( const QString &value );
224 
226  static QString activeDeviceInfo( const Info infoType = Info::Name );
227 
229  static QString deviceInfo( const Info infoType, cl::Device device );
230 
235  template <typename T>
237  {
238 
239  public:
240 
241  explicit CPLAllocator( unsigned long size ): mMem( static_cast<T *>( CPLMalloc( sizeof( T ) * size ) ) ) { }
242 
244  {
245  CPLFree( static_cast<void *>( mMem ) );
246  }
247 
248  void reset( T *newData )
249  {
250  if ( mMem )
251  CPLFree( static_cast<void *>( mMem ) );
252  mMem = newData;
253  }
254 
255  void reset( unsigned long size )
256  {
257  reset( static_cast<T *>( CPLMalloc( sizeof( T ) *size ) ) );
258  }
259 
261  {
262  return &mMem[0];
263  }
264 
265  T *release()
266  {
267  T *tmpMem = mMem;
268  mMem = nullptr;
269  return tmpMem;
270  }
271 
272  T &operator[]( const int index )
273  {
274  return mMem[index];
275  }
276 
277  T *get()
278  {
279  return mMem;
280  }
281 
282  private:
283 
284  T *mMem = nullptr;
285  };
286 
287 
288  private:
289 
290  QgsOpenClUtils();
291 
305  static bool activate( const QString &preferredDeviceId = QString() );
306 
310  static void init();
311 
312  static bool sAvailable;
313  static QLatin1String SETTINGS_GLOBAL_ENABLED_KEY;
314  static QLatin1String SETTINGS_DEFAULT_DEVICE_KEY;
315  static QString sSourcePath;
316 };
317 
318 
319 
320 #endif // QGSOPENCLUTILS_H
void reset(unsigned long size)
Tiny smart-pointer-like wrapper around CPLMalloc and CPLFree: this is needed because OpenCL C++ API m...
CPLAllocator(unsigned long size)
T & operator[](const int index)
The QgsOpenClUtils class is responsible for common OpenCL operations such as.
Info
The Info enum maps to OpenCL info constants.
ExceptionBehavior
The ExceptionBehavior enum define how exceptions generated by OpenCL should be treated.
HardwareType
The Type enum represent OpenCL device type.
static QLatin1String LOGMESSAGE_TAG
OpenCL string for message logs.
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