Skip to content

Commit 9ff8779

Browse files
committed
[opencl] Increase test coverage with no-opencl image comparison
1 parent 86b60c6 commit 9ff8779

File tree

2 files changed

+106
-37
lines changed

2 files changed

+106
-37
lines changed

tests/src/analysis/testqgsninecellfilters.cpp

Lines changed: 83 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
#include <QDir>
3131

32+
// If true regenerate raster reference images
33+
const bool REGENERATE_REFERENCES = false;
3234

3335
class TestNineCellFilters : public QObject
3436
{
@@ -38,18 +40,24 @@ class TestNineCellFilters : public QObject
3840
private slots:
3941

4042
void initTestCase();
43+
void init();
44+
45+
void testHillshade();
4146
void testSlope();
4247
void testAspect();
43-
void testHillshade();
4448
void testRuggedness();
4549
void testTotalCurvature();
4650
#ifdef HAVE_OPENCL
51+
void testHillshadeCl();
4752
void testSlopeCl();
4853
void testAspectCl();
54+
void testRuggednessCl();
4955
#endif
5056

5157
private:
5258

59+
void _rasterCompare( QgsAlignRaster::RasterInfo &out, QgsAlignRaster::RasterInfo &ref );
60+
5361
template <class T> void _testAlg( const QString &name, bool useOpenCl = false );
5462

5563
static QString referenceFile( const QString &name )
@@ -64,6 +72,13 @@ class TestNineCellFilters : public QObject
6472
};
6573

6674

75+
void TestNineCellFilters::init()
76+
{
77+
#ifdef HAVE_OPENCL
78+
// Reset to default in case some tests mess it up
79+
QgsOpenClUtils::setSourcePath( QDir( QgsApplication::pkgDataPath() ).absoluteFilePath( QStringLiteral( "resources/opencl_programs" ) ) );
80+
#endif
81+
}
6782

6883
void TestNineCellFilters::initTestCase()
6984
{
@@ -77,16 +92,12 @@ template <class T>
7792
void TestNineCellFilters::_testAlg( const QString &name, bool useOpenCl )
7893
{
7994
#ifdef HAVE_OPENCL
80-
QgsSettings().setValue( QStringLiteral( "useOpenCl" ), true, QgsSettings::Section::Core );
95+
QgsOpenClUtils::setEnabled( useOpenCl );
8196
QString tmpFile( tempFile( name + ( useOpenCl ? "_opencl" : "" ) ) );
8297
#else
83-
Q_UNUSED( useOpenCl );
8498
QString tmpFile( tempFile( name ) );
8599
#endif
86100
QString refFile( referenceFile( name ) );
87-
QgsAlignRaster::RasterInfo in( SRC_FILE );
88-
QSize inSize( in.rasterSize() );
89-
QSizeF inCellSize( in.cellSize( ) );
90101
T ninecellsfilter( SRC_FILE, tmpFile, "GTiff" );
91102
int res = ninecellsfilter.processRaster();
92103
QVERIFY( res == 0 );
@@ -95,20 +106,20 @@ void TestNineCellFilters::_testAlg( const QString &name, bool useOpenCl )
95106
QgsAlignRaster::RasterInfo out( tmpFile );
96107
QVERIFY( out.isValid() );
97108

98-
// Reference file
99-
QgsAlignRaster::RasterInfo ref( refFile );
100-
QSize refSize( ref.rasterSize() );
101-
QSizeF refCellSize( ref.cellSize( ) );
102-
103-
QCOMPARE( out.rasterSize(), inSize );
104-
QCOMPARE( out.cellSize(), inCellSize );
105-
QCOMPARE( out.rasterSize(), refSize );
106-
QCOMPARE( out.cellSize(), refCellSize );
109+
// Regenerate reference rasters
110+
if ( ! useOpenCl && REGENERATE_REFERENCES )
111+
{
112+
if ( QFile::exists( refFile ) )
113+
{
114+
QFile::remove( refFile );
115+
}
116+
QVERIFY( QFile::copy( tmpFile, refFile ) );
117+
}
107118

108-
double refId1( ref.identify( 4081812, 2431750 ) );
109-
double refId2( ref.identify( 4081312, 2431350 ) );
110-
QVERIFY( qAbs( out.identify( 4081812, 2431750 ) - refId1 ) < 0.0001f );
111-
QVERIFY( qAbs( out.identify( 4081312, 2431350 ) - refId2 ) < 0.0001f );
119+
// Reference
120+
QgsAlignRaster::RasterInfo ref( refFile );
121+
//qDebug() << "Comparing " << tmpFile << refFile;
122+
_rasterCompare( out, ref );
112123

113124
}
114125

@@ -118,7 +129,6 @@ void TestNineCellFilters::testSlope()
118129
_testAlg<QgsSlopeFilter>( QStringLiteral( "slope" ) );
119130
}
120131

121-
122132
void TestNineCellFilters::testAspect()
123133
{
124134
_testAlg<QgsAspectFilter>( QStringLiteral( "aspect" ) );
@@ -130,24 +140,75 @@ void TestNineCellFilters::testSlopeCl()
130140
_testAlg<QgsSlopeFilter>( QStringLiteral( "slope" ), true );
131141
}
132142

133-
134143
void TestNineCellFilters::testAspectCl()
135144
{
136145
_testAlg<QgsAspectFilter>( QStringLiteral( "aspect" ), true );
137146
}
147+
148+
void TestNineCellFilters::testHillshadeCl()
149+
{
150+
_testAlg<QgsHillshadeFilter>( QStringLiteral( "hillshade" ), true );
151+
}
152+
153+
void TestNineCellFilters::testRuggednessCl()
154+
{
155+
_testAlg<QgsRuggednessFilter>( QStringLiteral( "ruggedness" ), true );
156+
}
157+
138158
#endif
139159

140160
void TestNineCellFilters::testHillshade()
141161
{
142162
_testAlg<QgsHillshadeFilter>( QStringLiteral( "hillshade" ) );
143163
}
144164

145-
146165
void TestNineCellFilters::testRuggedness()
147166
{
148167
_testAlg<QgsRuggednessFilter>( QStringLiteral( "ruggedness" ) );
149168
}
150169

170+
void TestNineCellFilters::_rasterCompare( QgsAlignRaster::RasterInfo &out, QgsAlignRaster::RasterInfo &ref )
171+
{
172+
QSize refSize( ref.rasterSize() );
173+
QSizeF refCellSize( ref.cellSize( ) );
174+
QgsAlignRaster::RasterInfo in( SRC_FILE );
175+
QSize inSize( in.rasterSize() );
176+
QSizeF inCellSize( in.cellSize( ) );
177+
QCOMPARE( out.rasterSize(), inSize );
178+
QCOMPARE( out.cellSize(), inCellSize );
179+
QCOMPARE( out.rasterSize(), refSize );
180+
QCOMPARE( out.cellSize(), refCellSize );
181+
182+
// If the values differ less than tolerance they are considered equal
183+
double tolerance = 0.0000001;
184+
185+
// Check three points
186+
std::map<int, int> controlPoints;
187+
controlPoints[4081812] = 2431750;
188+
controlPoints[4081312] = 2431350;
189+
controlPoints[4080263] = 2429558;
190+
// South West corner
191+
controlPoints[4081512] = 2431550;
192+
// North east corner
193+
controlPoints[4085367] = 2434940;
194+
// North west corner
195+
controlPoints[4078263] = 2434936;
196+
// South east corner
197+
controlPoints[4085374] = 2428551;
198+
199+
for ( const auto &cp : controlPoints )
200+
{
201+
int x = cp.first;
202+
int y = cp.second;
203+
double outVal = out.identify( x, y );
204+
double refVal = ref.identify( x, y );
205+
double diff( qAbs( outVal - refVal ) );
206+
//qDebug() << outVal << refVal;
207+
//qDebug() << "Identify " << x << "," << y << " diff " << diff << " check: < " << tolerance;
208+
QVERIFY( diff <= tolerance );
209+
}
210+
211+
}
151212

152213
void TestNineCellFilters::testTotalCurvature()
153214
{

tests/src/core/testqgsopenclutils.cpp

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ class TestQgsOpenClUtils: public QObject
4646
void testProgramSource();
4747
void testContext();
4848
void testDevices();
49-
// For performance testing
50-
void testHillshade();
49+
50+
// For benchmarking performance testing
51+
void testHillshadeCPU();
52+
void testHillshadeGPU();
5153

5254
private:
5355

5456
void _testMakeRunProgram();
55-
void _testMakeHillshade( const QString title, const int loops );
57+
void _testMakeHillshade( const int loops );
5658

5759
cl::Program buildProgram( const cl::Context &context, const QString &source )
5860
{
@@ -205,30 +207,36 @@ void TestQgsOpenClUtils::testDevices()
205207
qDebug() << QgsOpenClUtils::deviceInfo( QgsOpenClUtils::Info::Type, _devices.at( 0 ) );
206208
}
207209

208-
void TestQgsOpenClUtils::_testMakeHillshade( const QString title, const int loops )
210+
void TestQgsOpenClUtils::_testMakeHillshade( const int loops )
209211
{
210-
std::chrono::time_point<std::chrono::system_clock> startTime( std::chrono::system_clock::now() );
211212
for ( int i = 0 ; i < loops; i++ )
212213
{
213214
QgsHillshadeRenderer renderer( mFloat32RasterLayer->dataProvider(), 1, 35.0, 5000.0 );
214215
// Note: CPU time grows linearly with raster dimensions while GPU time is roughly constant
215-
// 900x900 px gives even times on my testing machine
216-
renderer.block( 0, mFloat32RasterLayer->extent(), 900, 900 );
216+
// 200x200 px gives even times on my testing machine
217+
renderer.block( 0, mFloat32RasterLayer->extent(), 200, 200 );
217218
}
218-
qDebug() << QStringLiteral( "%1 average for %2 loops: %3 ms" )
219-
.arg( title )
220-
.arg( loops )
221-
.arg( std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::system_clock::now() - startTime ).count() / loops ) ;
222219
}
223220

224-
void TestQgsOpenClUtils::testHillshade()
221+
void TestQgsOpenClUtils::testHillshadeGPU()
225222
{
226223
QgsOpenClUtils::setEnabled( true );
227-
_testMakeHillshade( QStringLiteral( "OpenCL" ), 5 );
228-
QVERIFY( QgsOpenClUtils::enabled() );
224+
QBENCHMARK
225+
{
226+
_testMakeHillshade( 1 );
227+
}
228+
}
229+
230+
void TestQgsOpenClUtils::testHillshadeCPU()
231+
{
229232
QgsOpenClUtils::setEnabled( false );
230-
_testMakeHillshade( QStringLiteral( "CPU" ), 5 );
233+
QBENCHMARK
234+
{
235+
_testMakeHillshade( 1 );
236+
}
231237
}
232238

239+
240+
233241
QGSTEST_MAIN( TestQgsOpenClUtils )
234242
#include "testqgsopenclutils.moc"

0 commit comments

Comments
 (0)