QGIS API Documentation 4.1.0-Master (376402f9aeb)
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
54
89class CORE_EXPORT QgsOpenClUtils
90{
91 Q_GADGET
92
93 public:
104
114
115 Q_ENUM( HardwareType )
116
117
122 enum Info
123 {
124 Name = CL_DEVICE_NAME,
125 Vendor = CL_DEVICE_VENDOR,
126 Version = CL_DEVICE_VERSION,
127 Profile = CL_DEVICE_PROFILE,
128 ImageSupport = CL_DEVICE_IMAGE_SUPPORT,
129 Image2dMaxWidth = CL_DEVICE_IMAGE2D_MAX_WIDTH,
130 Image2dMaxHeight = CL_DEVICE_IMAGE2D_MAX_HEIGHT,
131 MaxMemAllocSize = CL_DEVICE_MAX_MEM_ALLOC_SIZE,
132 Type = CL_DEVICE_TYPE // CPU/GPU etc.
133 };
134
144 static bool available();
145
147 static bool enabled();
148
150 static const std::vector<cl::Device> devices();
151
159 static cl::Device activeDevice();
160
166 static QString activePlatformVersion();
167
169 static void storePreferredDevice( const QString deviceId );
170
172 static QString preferredDevice();
173
175 static QString deviceId( const cl::Device device );
176
180 static QString deviceDescription( const cl::Device device );
181
185 static QString deviceDescription( const QString deviceId );
186
188 static void setEnabled( bool enabled );
189
191 static QString buildLog( cl::BuildError &error );
192
194 static QString sourceFromPath( const QString &path );
195
197 static QString sourceFromBaseName( const QString &baseName );
198
200 static QLatin1String LOGMESSAGE_TAG;
201
203 static QString errorText( const int errorCode );
204
211 static cl::CommandQueue commandQueue();
212
219 Q_DECL_DEPRECATED static cl::Program buildProgram( const cl::Context &context, const QString &source, ExceptionBehavior exceptionBehavior = Catch );
220
225 static cl::Program buildProgram( const QString &source, ExceptionBehavior exceptionBehavior = Catch );
226
227
235 static cl::Context context();
236
238 static QString sourcePath();
239
241 static void setSourcePath( const QString &value );
242
244 static QString activeDeviceInfo( const Info infoType = Info::Name );
245
247 static QString deviceInfo( const Info infoType, cl::Device device );
248
253 template<typename T> struct CPLAllocator
254 {
255 public:
256 explicit CPLAllocator( unsigned long size )
257 : mMem( static_cast<T *>( CPLMalloc( sizeof( T ) * size ) ) )
258 {}
259
260 ~CPLAllocator() { CPLFree( static_cast<void *>( mMem ) ); }
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 ) { reset( static_cast<T *>( CPLMalloc( sizeof( T ) * size ) ) ); }
270
272 {
273 // cppcheck-suppress returnTempReference
274 return &mMem[0];
275 }
276
278 {
279 T *tmpMem = mMem;
280 mMem = nullptr;
281 return tmpMem;
282 }
283
284 T &operator[]( const int index ) { return mMem[index]; }
285
286 T *get() { return mMem; }
287
288 private:
289 T *mMem = nullptr;
290 };
291
292
293 private:
295
309 static bool activate( const QString &preferredDeviceId = QString() );
310
311 static bool activateInternal( const QString &preferredDeviceId );
312
316 static void init();
317
318 static bool sAvailable;
319};
320
321
322#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.
static const QgsSettingsEntryBool * settingsOpenClEnabled
static const QgsSettingsEntryString * settingsOpenClDefaultDevice
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.
A boolean settings entry.
A string settings entry.
T & operator[](const int index)
void reset(unsigned long size)
CPLAllocator(unsigned long size)