QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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 "qgsconfig.h"
35
36#ifdef OPENCL_USE_NEW_HEADER
37#include <CL/opencl.hpp>
38#else
39#include <CL/cl2.hpp>
40#endif
41
42#include "qgis_core.h"
43#include "qgis.h"
44
45#include "cpl_conv.h"
46
79class CORE_EXPORT QgsOpenClUtils
80{
81 Q_GADGET
82
83 public:
84
89 {
91 Throw
92 };
93
98 {
101 Other
102 };
103
104 Q_ENUM( HardwareType )
105
106
111 enum Info
112 {
113 Name = CL_DEVICE_NAME,
114 Vendor = CL_DEVICE_VENDOR,
115 Version = CL_DEVICE_VERSION,
116 Profile = CL_DEVICE_PROFILE,
117 ImageSupport = CL_DEVICE_IMAGE_SUPPORT,
118 Image2dMaxWidth = CL_DEVICE_IMAGE2D_MAX_WIDTH,
119 Image2dMaxHeight = CL_DEVICE_IMAGE2D_MAX_HEIGHT,
120 MaxMemAllocSize = CL_DEVICE_MAX_MEM_ALLOC_SIZE,
121 Type = CL_DEVICE_TYPE // CPU/GPU etc.
122 };
123
133 static bool available();
134
136 static bool enabled();
137
139 static const std::vector<cl::Device> devices();
140
148 static cl::Device activeDevice( );
149
155 static QString activePlatformVersion( );
156
158 static void storePreferredDevice( const QString deviceId );
159
161 static QString preferredDevice( );
162
164 static QString deviceId( const cl::Device device );
165
169 static QString deviceDescription( const cl::Device device );
170
174 static QString deviceDescription( const QString deviceId );
175
177 static void setEnabled( bool enabled );
178
180 static QString buildLog( cl::BuildError &error );
181
183 static QString sourceFromPath( const QString &path );
184
186 static QString sourceFromBaseName( const QString &baseName );
187
189 static QLatin1String LOGMESSAGE_TAG;
190
192 static QString errorText( const int errorCode );
193
200 static cl::CommandQueue commandQueue();
201
208 Q_DECL_DEPRECATED static cl::Program buildProgram( const cl::Context &context, const QString &source, ExceptionBehavior exceptionBehavior = Catch );
209
214 static cl::Program buildProgram( const QString &source, ExceptionBehavior exceptionBehavior = Catch );
215
216
224 static cl::Context context();
225
227 static QString sourcePath();
228
230 static void setSourcePath( const QString &value );
231
233 static QString activeDeviceInfo( const Info infoType = Info::Name );
234
236 static QString deviceInfo( const Info infoType, cl::Device device );
237
242 template <typename T>
244 {
245
246 public:
247
248 explicit CPLAllocator( unsigned long size ): mMem( static_cast<T *>( CPLMalloc( sizeof( T ) * size ) ) ) { }
249
251 {
252 CPLFree( static_cast<void *>( mMem ) );
253 }
254
255 void reset( T *newData )
256 {
257 if ( mMem )
258 CPLFree( static_cast<void *>( mMem ) );
259 mMem = newData;
260 }
261
262 void reset( unsigned long size )
263 {
264 reset( static_cast<T *>( CPLMalloc( sizeof( T ) *size ) ) );
265 }
266
268 {
269 // cppcheck-suppress returnTempReference
270 return &mMem[0];
271 }
272
274 {
275 T *tmpMem = mMem;
276 mMem = nullptr;
277 return tmpMem;
278 }
279
280 T &operator[]( const int index )
281 {
282 return mMem[index];
283 }
284
285 T *get()
286 {
287 return mMem;
288 }
289
290 private:
291
292 T *mMem = nullptr;
293 };
294
295
296 private:
297
299
313 static bool activate( const QString &preferredDeviceId = QString() );
314
318 static void init();
319
320 static bool sAvailable;
321 static QLatin1String SETTINGS_GLOBAL_ENABLED_KEY;
322 static QLatin1String SETTINGS_DEFAULT_DEVICE_KEY;
323};
324
325
326
327#endif // QGSOPENCLUTILS_H
The QgsOpenClUtils class is responsible for common OpenCL operations such as.
HardwareType
The Type enum represent OpenCL device type.
ExceptionBehavior
The ExceptionBehavior enum define how exceptions generated by OpenCL should be treated.
@ Catch
Write errors in the message log and silently fail.
Info
The Info enum maps to OpenCL info constants.
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:241
Tiny smart-pointer-like wrapper around CPLMalloc and CPLFree: this is needed because OpenCL C++ API m...
T & operator[](const int index)
void reset(unsigned long size)
CPLAllocator(unsigned long size)