QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
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
25#ifdef Q_OS_MAC
26#define CL_HPP_MINIMUM_OPENCL_VERSION 120
27#define CL_HPP_TARGET_OPENCL_VERSION 120
28#define CL_TARGET_OPENCL_VERSION 120
29#else
30#define CL_USE_DEPRECATED_OPENCL_1_1_APIS
31#define CL_HPP_TARGET_OPENCL_VERSION 200
32#define CL_TARGET_OPENCL_VERSION 200
33#endif
34
35#include "qgsconfig.h"
36
37#ifdef OPENCL_USE_NEW_HEADER
38#include <CL/opencl.hpp>
39#else
40#include <CL/cl2.hpp>
41#endif
42
43#include "qgis_core.h"
44#include "qgis.h"
45
46#include "cpl_conv.h"
47
82class CORE_EXPORT QgsOpenClUtils
83{
84 Q_GADGET
85
86 public:
87
96
106
107 Q_ENUM( HardwareType )
108
109
114 enum Info
115 {
116 Name = CL_DEVICE_NAME,
117 Vendor = CL_DEVICE_VENDOR,
118 Version = CL_DEVICE_VERSION,
119 Profile = CL_DEVICE_PROFILE,
120 ImageSupport = CL_DEVICE_IMAGE_SUPPORT,
121 Image2dMaxWidth = CL_DEVICE_IMAGE2D_MAX_WIDTH,
122 Image2dMaxHeight = CL_DEVICE_IMAGE2D_MAX_HEIGHT,
123 MaxMemAllocSize = CL_DEVICE_MAX_MEM_ALLOC_SIZE,
124 Type = CL_DEVICE_TYPE // CPU/GPU etc.
125 };
126
136 static bool available();
137
139 static bool enabled();
140
142 static const std::vector<cl::Device> devices();
143
151 static cl::Device activeDevice( );
152
158 static QString activePlatformVersion( );
159
161 static void storePreferredDevice( const QString deviceId );
162
164 static QString preferredDevice( );
165
167 static QString deviceId( const cl::Device device );
168
172 static QString deviceDescription( const cl::Device device );
173
177 static QString deviceDescription( const QString deviceId );
178
180 static void setEnabled( bool enabled );
181
183 static QString buildLog( cl::BuildError &error );
184
186 static QString sourceFromPath( const QString &path );
187
189 static QString sourceFromBaseName( const QString &baseName );
190
192 static QLatin1String LOGMESSAGE_TAG;
193
195 static QString errorText( const int errorCode );
196
203 static cl::CommandQueue commandQueue();
204
211 Q_DECL_DEPRECATED static cl::Program buildProgram( const cl::Context &context, const QString &source, ExceptionBehavior exceptionBehavior = Catch );
212
217 static cl::Program buildProgram( const QString &source, ExceptionBehavior exceptionBehavior = Catch );
218
219
227 static cl::Context context();
228
230 static QString sourcePath();
231
233 static void setSourcePath( const QString &value );
234
236 static QString activeDeviceInfo( const Info infoType = Info::Name );
237
239 static QString deviceInfo( const Info infoType, cl::Device device );
240
245 template <typename T>
247 {
248
249 public:
250
251 explicit CPLAllocator( unsigned long size ): mMem( static_cast<T *>( CPLMalloc( sizeof( T ) * size ) ) ) { }
252
254 {
255 CPLFree( static_cast<void *>( mMem ) );
256 }
257
258 void reset( T *newData )
259 {
260 if ( mMem )
261 CPLFree( static_cast<void *>( mMem ) );
262 mMem = newData;
263 }
264
265 void reset( unsigned long size )
266 {
267 reset( static_cast<T *>( CPLMalloc( sizeof( T ) *size ) ) );
268 }
269
271 {
272 // cppcheck-suppress returnTempReference
273 return &mMem[0];
274 }
275
277 {
278 T *tmpMem = mMem;
279 mMem = nullptr;
280 return tmpMem;
281 }
282
283 T &operator[]( const int index )
284 {
285 return mMem[index];
286 }
287
288 T *get()
289 {
290 return mMem;
291 }
292
293 private:
294
295 T *mMem = nullptr;
296 };
297
298
299 private:
300
302
316 static bool activate( const QString &preferredDeviceId = QString() );
317
318 static bool activateInternal( const QString &preferredDeviceId );
319
323 static void init();
324
325 static bool sAvailable;
326 static QLatin1String SETTINGS_GLOBAL_ENABLED_KEY;
327 static QLatin1String SETTINGS_DEFAULT_DEVICE_KEY;
328};
329
330
331
332#endif // QGSOPENCLUTILS_H
Utilities responsible for common OpenCL operations.
static Q_DECL_DEPRECATED cl::Program buildProgram(const cl::Context &context, const QString &source, ExceptionBehavior exceptionBehavior=Catch)
Build the program from source in the given context and depending on exceptionBehavior can throw or ca...
static void setSourcePath(const QString &value)
Set the base path to OpenCL program directory.
static QString sourcePath()
Returns the base path to OpenCL program directory.
static cl::Context context()
Context factory.
HardwareType
The Type enum represent OpenCL device type.
static QString activeDeviceInfo(const Info infoType=Info::Name)
Returns infoType information about the active (default) device.
static QString deviceInfo(const Info infoType, cl::Device device)
Returns infoType information about the device.
static QString errorText(const int errorCode)
Returns a string representation from an OpenCL errorCode.
static cl::CommandQueue commandQueue()
Create an OpenCL command queue from the default context.
ExceptionBehavior
The ExceptionBehavior enum define how exceptions generated by OpenCL should be treated.
@ Throw
Write errors in the message log and re-throw exceptions.
@ 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:250
T & operator[](const int index)
void reset(unsigned long size)
CPLAllocator(unsigned long size)