QGIS API Documentation 3.43.0-Master (b60ef06885e)
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
81class CORE_EXPORT QgsOpenClUtils
82{
83 Q_GADGET
84
85 public:
86
91 {
93 Throw
94 };
95
100 {
103 Other
104 };
105
106 Q_ENUM( HardwareType )
107
108
113 enum Info
114 {
115 Name = CL_DEVICE_NAME,
116 Vendor = CL_DEVICE_VENDOR,
117 Version = CL_DEVICE_VERSION,
118 Profile = CL_DEVICE_PROFILE,
119 ImageSupport = CL_DEVICE_IMAGE_SUPPORT,
120 Image2dMaxWidth = CL_DEVICE_IMAGE2D_MAX_WIDTH,
121 Image2dMaxHeight = CL_DEVICE_IMAGE2D_MAX_HEIGHT,
122 MaxMemAllocSize = CL_DEVICE_MAX_MEM_ALLOC_SIZE,
123 Type = CL_DEVICE_TYPE // CPU/GPU etc.
124 };
125
135 static bool available();
136
138 static bool enabled();
139
141 static const std::vector<cl::Device> devices();
142
150 static cl::Device activeDevice( );
151
157 static QString activePlatformVersion( );
158
160 static void storePreferredDevice( const QString deviceId );
161
163 static QString preferredDevice( );
164
166 static QString deviceId( const cl::Device device );
167
171 static QString deviceDescription( const cl::Device device );
172
176 static QString deviceDescription( const QString deviceId );
177
179 static void setEnabled( bool enabled );
180
182 static QString buildLog( cl::BuildError &error );
183
185 static QString sourceFromPath( const QString &path );
186
188 static QString sourceFromBaseName( const QString &baseName );
189
191 static QLatin1String LOGMESSAGE_TAG;
192
194 static QString errorText( const int errorCode );
195
202 static cl::CommandQueue commandQueue();
203
210 Q_DECL_DEPRECATED static cl::Program buildProgram( const cl::Context &context, const QString &source, ExceptionBehavior exceptionBehavior = Catch );
211
216 static cl::Program buildProgram( const QString &source, ExceptionBehavior exceptionBehavior = Catch );
217
218
226 static cl::Context context();
227
229 static QString sourcePath();
230
232 static void setSourcePath( const QString &value );
233
235 static QString activeDeviceInfo( const Info infoType = Info::Name );
236
238 static QString deviceInfo( const Info infoType, cl::Device device );
239
244 template <typename T>
246 {
247
248 public:
249
250 explicit CPLAllocator( unsigned long size ): mMem( static_cast<T *>( CPLMalloc( sizeof( T ) * size ) ) ) { }
251
253 {
254 CPLFree( static_cast<void *>( mMem ) );
255 }
256
257 void reset( T *newData )
258 {
259 if ( mMem )
260 CPLFree( static_cast<void *>( mMem ) );
261 mMem = newData;
262 }
263
264 void reset( unsigned long size )
265 {
266 reset( static_cast<T *>( CPLMalloc( sizeof( T ) *size ) ) );
267 }
268
270 {
271 // cppcheck-suppress returnTempReference
272 return &mMem[0];
273 }
274
276 {
277 T *tmpMem = mMem;
278 mMem = nullptr;
279 return tmpMem;
280 }
281
282 T &operator[]( const int index )
283 {
284 return mMem[index];
285 }
286
287 T *get()
288 {
289 return mMem;
290 }
291
292 private:
293
294 T *mMem = nullptr;
295 };
296
297
298 private:
299
301
315 static bool activate( const QString &preferredDeviceId = QString() );
316
317 static bool activateInternal( const QString &preferredDeviceId );
318
322 static void init();
323
324 static bool sAvailable;
325 static QLatin1String SETTINGS_GLOBAL_ENABLED_KEY;
326 static QLatin1String SETTINGS_DEFAULT_DEVICE_KEY;
327};
328
329
330
331#endif // QGSOPENCLUTILS_H
Utilities responsible for common OpenCL operations.
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:249
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)