QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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:
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> struct CPLAllocator
249 {
250 public:
251 explicit CPLAllocator( unsigned long size )
252 : mMem( static_cast<T *>( CPLMalloc( sizeof( T ) * size ) ) )
253 {}
254
255 ~CPLAllocator() { CPLFree( static_cast<void *>( mMem ) ); }
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 ) { reset( static_cast<T *>( CPLMalloc( sizeof( T ) * size ) ) ); }
265
267 {
268 // cppcheck-suppress returnTempReference
269 return &mMem[0];
270 }
271
273 {
274 T *tmpMem = mMem;
275 mMem = nullptr;
276 return tmpMem;
277 }
278
279 T &operator[]( const int index ) { return mMem[index]; }
280
281 T *get() { return mMem; }
282
283 private:
284 T *mMem = nullptr;
285 };
286
287
288 private:
290
304 static bool activate( const QString &preferredDeviceId = QString() );
305
306 static bool activateInternal( const QString &preferredDeviceId );
307
311 static void init();
312
313 static bool sAvailable;
314 static QLatin1String SETTINGS_GLOBAL_ENABLED_KEY;
315 static QLatin1String SETTINGS_DEFAULT_DEVICE_KEY;
316};
317
318
319#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.
T & operator[](const int index)
void reset(unsigned long size)
CPLAllocator(unsigned long size)