QGIS API Documentation 3.99.0-Master (e9821da5c6b)
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 <QString>
24#include <QtGlobal>
25
26using namespace Qt::StringLiterals;
27
28#ifdef Q_OS_MAC
29#define CL_HPP_MINIMUM_OPENCL_VERSION 120
30#define CL_HPP_TARGET_OPENCL_VERSION 120
31#define CL_TARGET_OPENCL_VERSION 120
32#else
33#define CL_USE_DEPRECATED_OPENCL_1_1_APIS
34#define CL_HPP_TARGET_OPENCL_VERSION 200
35#define CL_TARGET_OPENCL_VERSION 200
36#endif
37
38#include "qgsconfig.h"
39
40#ifdef OPENCL_USE_NEW_HEADER
41#include <CL/opencl.hpp>
42#else
43#include <CL/cl2.hpp>
44#endif
45
46#include "qgis_core.h"
47#include "qgis.h"
48
49#include "cpl_conv.h"
50
85class CORE_EXPORT QgsOpenClUtils
86{
87 Q_GADGET
88
89 public:
90
99
109
110 Q_ENUM( HardwareType )
111
112
117 enum Info
118 {
119 Name = CL_DEVICE_NAME,
120 Vendor = CL_DEVICE_VENDOR,
121 Version = CL_DEVICE_VERSION,
122 Profile = CL_DEVICE_PROFILE,
123 ImageSupport = CL_DEVICE_IMAGE_SUPPORT,
124 Image2dMaxWidth = CL_DEVICE_IMAGE2D_MAX_WIDTH,
125 Image2dMaxHeight = CL_DEVICE_IMAGE2D_MAX_HEIGHT,
126 MaxMemAllocSize = CL_DEVICE_MAX_MEM_ALLOC_SIZE,
127 Type = CL_DEVICE_TYPE // CPU/GPU etc.
128 };
129
139 static bool available();
140
142 static bool enabled();
143
145 static const std::vector<cl::Device> devices();
146
154 static cl::Device activeDevice( );
155
161 static QString activePlatformVersion( );
162
164 static void storePreferredDevice( const QString deviceId );
165
167 static QString preferredDevice( );
168
170 static QString deviceId( const cl::Device device );
171
175 static QString deviceDescription( const cl::Device device );
176
180 static QString deviceDescription( const QString deviceId );
181
183 static void setEnabled( bool enabled );
184
186 static QString buildLog( cl::BuildError &error );
187
189 static QString sourceFromPath( const QString &path );
190
192 static QString sourceFromBaseName( const QString &baseName );
193
195 static QLatin1String LOGMESSAGE_TAG;
196
198 static QString errorText( const int errorCode );
199
206 static cl::CommandQueue commandQueue();
207
214 Q_DECL_DEPRECATED static cl::Program buildProgram( const cl::Context &context, const QString &source, ExceptionBehavior exceptionBehavior = Catch );
215
220 static cl::Program buildProgram( const QString &source, ExceptionBehavior exceptionBehavior = Catch );
221
222
230 static cl::Context context();
231
233 static QString sourcePath();
234
236 static void setSourcePath( const QString &value );
237
239 static QString activeDeviceInfo( const Info infoType = Info::Name );
240
242 static QString deviceInfo( const Info infoType, cl::Device device );
243
248 template <typename T>
250 {
251
252 public:
253
254 explicit CPLAllocator( unsigned long size ): mMem( static_cast<T *>( CPLMalloc( sizeof( T ) * size ) ) ) { }
255
257 {
258 CPLFree( static_cast<void *>( mMem ) );
259 }
260
261 void reset( T *newData )
262 {
263 if ( mMem )
264 CPLFree( static_cast<void *>( mMem ) );
265 mMem = newData;
266 }
267
268 void reset( unsigned long size )
269 {
270 reset( static_cast<T *>( CPLMalloc( sizeof( T ) *size ) ) );
271 }
272
274 {
275 // cppcheck-suppress returnTempReference
276 return &mMem[0];
277 }
278
280 {
281 T *tmpMem = mMem;
282 mMem = nullptr;
283 return tmpMem;
284 }
285
286 T &operator[]( const int index )
287 {
288 return mMem[index];
289 }
290
291 T *get()
292 {
293 return mMem;
294 }
295
296 private:
297
298 T *mMem = nullptr;
299 };
300
301
302 private:
303
305
319 static bool activate( const QString &preferredDeviceId = QString() );
320
321 static bool activateInternal( const QString &preferredDeviceId );
322
326 static void init();
327
328 static bool sAvailable;
329 static QLatin1String SETTINGS_GLOBAL_ENABLED_KEY;
330 static QLatin1String SETTINGS_DEFAULT_DEVICE_KEY;
331};
332
333
334
335#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:252
T & operator[](const int index)
void reset(unsigned long size)
CPLAllocator(unsigned long size)