/
qgsopenclutils.h
144 lines (118 loc) · 3.93 KB
/
qgsopenclutils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/***************************************************************************
qgsopenclutils.h - QgsOpenClUtils
---------------------
begin : 11.4.2018
copyright : (C) 2018 by Alessandro Pasotti
email : elpaso at itopen dot it
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSOPENCLUTILS_H
#define QGSOPENCLUTILS_H
#define SIP_NO_FILE
#define CL_HPP_ENABLE_EXCEPTIONS
#define CL_HPP_MINIMUM_OPENCL_VERSION 110
#define CL_HPP_TARGET_OPENCL_VERSION 110
#include <CL/cl2.hpp>
#include "qgis_core.h"
#include "qgis.h"
#include "cpl_conv.h"
/**
* \ingroup core
* \class QgsOpenClUtils
* \brief The QgsOpenClUtils class is responsible for common OpenCL operations such as
* - enable/disable opencl
* - check opencl device availability and automatically choose the first GPU (TODO: let the user choose & override!)
* - creating contexts
* - loading program sources from standard locations
* - build programs and log errors
* \since QGIS 3.4
* \note not available in Python bindings
*/
class CORE_EXPORT QgsOpenClUtils
{
public:
enum ExceptionBehavior
{
Catch, // Write errors in the message log and silently fail
Throw // Write errors in the message log and re-throw exceptions
};
enum Info
{
Name = CL_DEVICE_NAME,
Vendor = CL_DEVICE_VENDOR,
Version = CL_DEVICE_VERSION,
Profile = CL_DEVICE_PROFILE
};
static bool enabled();
static bool available();
static void setEnabled( bool enabled );
static QString buildLog( cl::BuildError &e );
static QString sourceFromPath( const QString &path );
static QString sourceFromBaseName( const QString &baseName );
static QLatin1String LOGMESSAGE_TAG;
static QString errorText( const int errorCode );
static cl::Program buildProgram( const cl::Context &context, const QString &source, ExceptionBehavior exceptionBehavior = Catch );
static cl::Context context();
static QString sourcePath();
static void setSourcePath( const QString &value );
static QString deviceInfo( const Info infoType = Info::Name );
/**
* Tiny smart-pointer-like wrapper around CPLMalloc and CPLFree: this is needed because
* OpenCL C++ API may throw exceptions
*/
template <typename T>
struct CPLAllocator
{
public:
explicit CPLAllocator( unsigned long size ): mMem( ( T * )CPLMalloc( sizeof( T ) * size ) ) { }
~CPLAllocator()
{
CPLFree( ( void * )mMem );
}
void reset( T *newData )
{
if ( mMem )
CPLFree( ( void * )mMem );
mMem = newData;
}
void reset( unsigned long size )
{
reset( ( T * )CPLMalloc( sizeof( T ) *size ) );
}
T &operator* ()
{
return &mMem[0];
}
T *release()
{
T *tmpMem = mMem;
mMem = nullptr;
return tmpMem;
}
T &operator[]( const int index )
{
return mMem[index];
}
T *get()
{
return mMem;
}
private:
T *mMem = nullptr;
};
private:
QgsOpenClUtils();
static void init();
static bool sAvailable;
static cl::Device sDevice;
static cl::Platform sPlatform;
static QLatin1String SETTINGS_KEY;
static QString sSourcePath;
};
#endif // QGSOPENCLUTILS_H