QGIS API Documentation 3.99.0-Master (7d2ca374f2d)
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
20#define CL_HPP_ENABLE_EXCEPTIONS
21
22#include <QString>
23#include <QtGlobal>
24
25#define SIP_NO_FILE
26
27using namespace Qt::StringLiterals;
28
29#ifdef Q_OS_MAC
30#define CL_HPP_MINIMUM_OPENCL_VERSION 120
31#define CL_HPP_TARGET_OPENCL_VERSION 120
32#define CL_TARGET_OPENCL_VERSION 120
33#else
34#define CL_USE_DEPRECATED_OPENCL_1_1_APIS
35#define CL_HPP_TARGET_OPENCL_VERSION 200
36#define CL_TARGET_OPENCL_VERSION 200
37#endif
38
39#include "qgsconfig.h"
40
41#ifdef OPENCL_USE_NEW_HEADER
42#include <CL/opencl.hpp>
43#else
44#include <CL/cl2.hpp>
45#endif
46
47#include "qgis_core.h"
48#include "qgis.h"
49
50#include "cpl_conv.h"
51
86class CORE_EXPORT QgsOpenClUtils
87{
88 Q_GADGET
89
90 public:
91
100
110
111 Q_ENUM( HardwareType )
112
113
118 enum Info
119 {
120 Name = CL_DEVICE_NAME,
121 Vendor = CL_DEVICE_VENDOR,
122 Version = CL_DEVICE_VERSION,
123 Profile = CL_DEVICE_PROFILE,
124 ImageSupport = CL_DEVICE_IMAGE_SUPPORT,
125 Image2dMaxWidth = CL_DEVICE_IMAGE2D_MAX_WIDTH,
126 Image2dMaxHeight = CL_DEVICE_IMAGE2D_MAX_HEIGHT,
127 MaxMemAllocSize = CL_DEVICE_MAX_MEM_ALLOC_SIZE,
128 Type = CL_DEVICE_TYPE // CPU/GPU etc.
129 };
130
140 static bool available();
141
143 static bool enabled();
144
146 static const std::vector<cl::Device> devices();
147
155 static cl::Device activeDevice( );
156
162 static QString activePlatformVersion( );
163
165 static void storePreferredDevice( const QString deviceId );
166
168 static QString preferredDevice( );
169
171 static QString deviceId( const cl::Device device );
172
176 static QString deviceDescription( const cl::Device device );
177
181 static QString deviceDescription( const QString deviceId );
182
184 static void setEnabled( bool enabled );
185
187 static QString buildLog( cl::BuildError &error );
188
190 static QString sourceFromPath( const QString &path );
191
193 static QString sourceFromBaseName( const QString &baseName );
194
196 static QLatin1String LOGMESSAGE_TAG;
197
199 static QString errorText( const int errorCode );
200
207 static cl::CommandQueue commandQueue();
208
215 Q_DECL_DEPRECATED static cl::Program buildProgram( const cl::Context &context, const QString &source, ExceptionBehavior exceptionBehavior = Catch );
216
221 static cl::Program buildProgram( const QString &source, ExceptionBehavior exceptionBehavior = Catch );
222
223
231 static cl::Context context();
232
234 static QString sourcePath();
235
237 static void setSourcePath( const QString &value );
238
240 static QString activeDeviceInfo( const Info infoType = Info::Name );
241
243 static QString deviceInfo( const Info infoType, cl::Device device );
244
249 template <typename T>
251 {
252
253 public:
254
255 explicit CPLAllocator( unsigned long size ): mMem( static_cast<T *>( CPLMalloc( sizeof( T ) * size ) ) ) { }
256
258 {
259 CPLFree( static_cast<void *>( mMem ) );
260 }
261
262 void reset( T *newData )
263 {
264 if ( mMem )
265 CPLFree( static_cast<void *>( mMem ) );
266 mMem = newData;
267 }
268
269 void reset( unsigned long size )
270 {
271 reset( static_cast<T *>( CPLMalloc( sizeof( T ) *size ) ) );
272 }
273
275 {
276 // cppcheck-suppress returnTempReference
277 return &mMem[0];
278 }
279
281 {
282 T *tmpMem = mMem;
283 mMem = nullptr;
284 return tmpMem;
285 }
286
287 T &operator[]( const int index )
288 {
289 return mMem[index];
290 }
291
292 T *get()
293 {
294 return mMem;
295 }
296
297 private:
298
299 T *mMem = nullptr;
300 };
301
302
303 private:
304
306
320 static bool activate( const QString &preferredDeviceId = QString() );
321
322 static bool activateInternal( const QString &preferredDeviceId );
323
327 static void init();
328
329 static bool sAvailable;
330 static QLatin1String SETTINGS_GLOBAL_ENABLED_KEY;
331 static QLatin1String SETTINGS_DEFAULT_DEVICE_KEY;
332};
333
334
335
336#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)