Skip to content

Commit ebad360

Browse files
committed
Added context
1 parent 08fc641 commit ebad360

File tree

4 files changed

+88
-39
lines changed

4 files changed

+88
-39
lines changed

src/analysis/raster/qgsninecellfilter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ int QgsNineCellFilter::processRasterGPU( const QString &source, QgsFeedback *fee
220220
return 6;
221221
}
222222

223+
// Prepare context
224+
cl::Context ctx = QgsOpenClUtils::context();
225+
cl::Context::setDefault( ctx );
226+
223227
//keep only three scanlines in memory at a time, make room for initial and final nodata
224228
QgsOpenClUtils::CPLAllocator<float> scanLine1( xSize + 2 );
225229
QgsOpenClUtils::CPLAllocator<float> scanLine2( xSize + 2 );

src/core/qgsopenclutils.cpp

Lines changed: 67 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,55 +26,79 @@
2626
QLatin1String QgsOpenClUtils::SETTINGS_KEY = QLatin1Literal( "OpenClEnabled" );
2727
QLatin1String QgsOpenClUtils::LOGMESSAGE_TAG = QLatin1Literal( "OpenCL" );
2828
bool QgsOpenClUtils::sAvailable = false;
29-
29+
cl::Platform QgsOpenClUtils::sPlatform = cl::Platform();
30+
cl::Device QgsOpenClUtils::sDevice = cl::Device();
3031

3132
void QgsOpenClUtils::init()
3233
{
3334
static bool initialized = false;
3435
if ( ! initialized )
3536
{
36-
std::vector<cl::Platform> platforms;
37-
cl::Platform::get( &platforms );
38-
cl::Platform plat;
39-
cl::Device dev;
40-
for ( auto &p : platforms )
37+
try
4138
{
42-
std::string platver = p.getInfo<CL_PLATFORM_VERSION>();
43-
QgsDebugMsg( QStringLiteral( "Found OpenCL platform %1: %2" ).arg( QString::fromStdString( platver ), QString::fromStdString( p.getInfo<CL_PLATFORM_NAME>() ) ) );
44-
if ( platver.find( "OpenCL 1." ) != std::string::npos )
39+
std::vector<cl::Platform> platforms;
40+
cl::Platform::get( &platforms );
41+
cl::Platform plat;
42+
cl::Device dev;
43+
for ( auto &p : platforms )
4544
{
46-
std::vector<cl::Device> devices;
47-
// Check for a GPU device
48-
p.getDevices( CL_DEVICE_TYPE_GPU, &devices );
49-
if ( devices.size() > 0 )
45+
std::string platver = p.getInfo<CL_PLATFORM_VERSION>();
46+
QgsDebugMsg( QStringLiteral( "Found OpenCL platform %1: %2" ).arg( QString::fromStdString( platver ), QString::fromStdString( p.getInfo<CL_PLATFORM_NAME>() ) ) );
47+
if ( platver.find( "OpenCL 1." ) != std::string::npos )
5048
{
51-
// Got one!
52-
plat = p;
53-
dev = devices[0];
54-
break;
49+
std::vector<cl::Device> devices;
50+
// Check for a GPU device
51+
try
52+
{
53+
p.getDevices( CL_DEVICE_TYPE_GPU, &devices );
54+
}
55+
catch ( cl::Error &e )
56+
{
57+
QgsDebugMsgLevel( QStringLiteral( "Error %1 on platform %3 searching for OpenCL device: %2" )
58+
.arg( errorText( e.err() ),
59+
QString::fromStdString( e.what() ),
60+
QString::fromStdString( p.getInfo<CL_PLATFORM_NAME>() ) ), 2 );
61+
}
62+
if ( devices.size() > 0 )
63+
{
64+
// Got one!
65+
plat = p;
66+
dev = devices[0];
67+
break;
68+
}
5569
}
5670
}
57-
}
58-
if ( plat() == 0 )
59-
{
60-
QgsMessageLog::logMessage( QObject::tr( "No OpenCL 1.x platform with GPU found." ), LOGMESSAGE_TAG, Qgis::Warning );
61-
sAvailable = false;
62-
}
63-
else
64-
{
65-
cl::Platform newP = cl::Platform::setDefault( plat );
66-
if ( newP != plat )
71+
if ( plat() == 0 )
6772
{
68-
QgsMessageLog::logMessage( QObject::tr( "Error setting default platform." ), LOGMESSAGE_TAG, Qgis::Warning );
73+
QgsMessageLog::logMessage( QObject::tr( "No OpenCL 1.x platform with GPU found." ), LOGMESSAGE_TAG, Qgis::Warning );
6974
sAvailable = false;
7075
}
7176
else
7277
{
73-
cl::Device::setDefault( dev );
74-
QgsDebugMsg( QStringLiteral( "Found OpenCL device %1" ).arg( QString::fromStdString( dev.getInfo<CL_DEVICE_NAME>() ) ) );
75-
sAvailable = true;
78+
cl::Platform newP = cl::Platform::setDefault( plat );
79+
if ( newP != plat )
80+
{
81+
QgsMessageLog::logMessage( QObject::tr( "Error setting default platform." ), LOGMESSAGE_TAG, Qgis::Warning );
82+
sAvailable = false;
83+
}
84+
else
85+
{
86+
cl::Device::setDefault( dev );
87+
QgsDebugMsgLevel( QStringLiteral( "Found OpenCL device %1" )
88+
.arg( QString::fromStdString( dev.getInfo<CL_DEVICE_NAME>() ) ), 2 );
89+
sAvailable = true;
90+
sDevice = dev;
91+
sPlatform = plat;
92+
}
7693
}
7794
}
95+
catch ( cl::Error &e )
96+
{
97+
QgsMessageLog::logMessage( QObject::tr( "Error %1 searching for OpenCL device: %2" )
98+
.arg( errorText( e.err() ), QString::fromStdString( e.what() ) ),
99+
LOGMESSAGE_TAG, Qgis::Critical );
100+
sAvailable = false;
101+
}
78102
initialized = true;
79103
}
80104
}
@@ -220,3 +244,15 @@ QString QgsOpenClUtils::errorText( const int errorCode )
220244
default: return QStringLiteral( "CL_UNKNOWN_ERROR" );
221245
}
222246
}
247+
248+
cl::Context QgsOpenClUtils::context()
249+
{
250+
if ( available() && sPlatform() && sDevice() )
251+
{
252+
return cl::Context( sDevice );
253+
}
254+
else
255+
{
256+
return cl::Context();
257+
}
258+
}

src/core/qgsopenclutils.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class CORE_EXPORT QgsOpenClUtils
4747
static QLatin1String LOGMESSAGE_TAG;
4848
static QString errorText( const int errorCode );
4949

50+
static cl::Context context();
51+
5052
/**
5153
* Tiny smart-pointer wrapper around CPLMalloc and CPLFree: this is needed because
5254
* OpenCL C++ API may throw exceptions
@@ -107,8 +109,9 @@ class CORE_EXPORT QgsOpenClUtils
107109
QgsOpenClUtils();
108110
static void init();
109111
static bool sAvailable;
112+
static cl::Device sDevice;
113+
static cl::Platform sPlatform;
110114
static QLatin1String SETTINGS_KEY;
111-
112115
};
113116

114117

tests/src/core/testqgsopenclutils.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class TestQgsOpenClUtils: public QObject
3434
void TestDisable();
3535
void TestAvailable();
3636
void testRunProgram();
37+
void testContext();
3738

3839
private:
3940

@@ -76,6 +77,9 @@ void TestQgsOpenClUtils::testRunProgram()
7677

7778
QVERIFY( err == 0 );
7879

80+
cl::Context ctx = QgsOpenClUtils::context();
81+
cl::Context::setDefault( ctx );
82+
7983
std::vector<float> a_vec = {1, 10, 100};
8084
std::vector<float> b_vec = {1, 10, 100};
8185
std::vector<float> c_vec = {-1, -1, -1};
@@ -85,11 +89,9 @@ void TestQgsOpenClUtils::testRunProgram()
8589

8690
try
8791
{
88-
cl::Program program( source( ), true );
89-
9092
// This also works:
91-
//cl::Program program( source( ) );
92-
//program.build("-cl-std=CL1.1");
93+
cl::Program program( source( ) );
94+
program.build( "-cl-std=CL1.1" );
9395

9496
auto kernel =
9597
cl::KernelFunctor <
@@ -108,7 +110,7 @@ void TestQgsOpenClUtils::testRunProgram()
108110
}
109111
catch ( cl::BuildError e )
110112
{
111-
qDebug() << "OPENCL Error: " << e.err() << e.what();
113+
qDebug() << "OPENCL Build Error: " << QgsOpenClUtils::errorText( e.err() ) << e.what();
112114
cl::BuildLogType build_logs = e.getBuildLog();
113115
QString build_log;
114116
if ( build_logs.size() > 0 )
@@ -125,11 +127,15 @@ void TestQgsOpenClUtils::testRunProgram()
125127
cl::copy( c_buf, c_vec.begin(), c_vec.end() );
126128
for ( size_t i = 0; i < c_vec.size(); ++i )
127129
{
128-
QCOMPARE( a_vec[i] + b_vec[i], c_vec[i] );
129-
qDebug() << c_vec[i];
130+
QCOMPARE( c_vec[i], a_vec[i] + b_vec[i] );
130131
}
131132
}
132133

134+
void TestQgsOpenClUtils::testContext()
135+
{
136+
QVERIFY( QgsOpenClUtils::context()() != nullptr );
137+
}
138+
133139

134140
QGSTEST_MAIN( TestQgsOpenClUtils )
135141
#include "testqgsopenclutils.moc"

0 commit comments

Comments
 (0)