Skip to content

Commit

Permalink
Fix build warning and cl-std for 2.x builds
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Nov 30, 2018
1 parent e5b8645 commit a471934
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
27 changes: 18 additions & 9 deletions src/core/qgsopenclutils.cpp
Expand Up @@ -41,8 +41,7 @@ const std::vector<cl::Device> QgsOpenClUtils::devices()
{
std::string platver = p.getInfo<CL_PLATFORM_VERSION>();
QgsDebugMsg( QStringLiteral( "Found OpenCL platform %1: %2" ).arg( QString::fromStdString( platver ), QString::fromStdString( p.getInfo<CL_PLATFORM_NAME>() ) ) );
// Check both versions 1 and 2
if ( platver.find( "OpenCL 1." ) != std::string::npos || platver.find( "OpenCL 2." ) != std::string::npos )
if ( platver.find( "OpenCL " ) != std::string::npos )
{
std::vector<cl::Device> _devices;
// Check for a device
Expand Down Expand Up @@ -219,8 +218,7 @@ bool QgsOpenClUtils::activate( const QString &preferredDeviceId )
break;
std::string platver = p.getInfo<CL_PLATFORM_VERSION>();
QgsDebugMsg( QStringLiteral( "Found OpenCL platform %1: %2" ).arg( QString::fromStdString( platver ), QString::fromStdString( p.getInfo<CL_PLATFORM_NAME>() ) ) );
// Checks both versions 1 and 2
if ( platver.find( "OpenCL 1." ) != std::string::npos || platver.find( "OpenCL 2." ) != std::string::npos )
if ( platver.find( "OpenCL " ) != std::string::npos )
{
std::vector<cl::Device> devices;
// Search for a device
Expand Down Expand Up @@ -262,7 +260,7 @@ bool QgsOpenClUtils::activate( const QString &preferredDeviceId )
{
for ( const auto &_dev : devices )
{
if ( _dev.getInfo<CL_DEVICE_TYPE>() == CL_DEVICE_TYPE_GPU )
if ( _dev.getInfo<CL_DEVICE_TYPE>() == CL_DEVICE_TYPE_CPU )
{
// Got one!
plat = p;
Expand Down Expand Up @@ -548,10 +546,21 @@ cl::Program QgsOpenClUtils::buildProgram( const QString &source, QgsOpenClUtils:
try
{
program = cl::Program( QgsOpenClUtils::context(), source.toStdString( ) );
// OpenCL version for compatibility with older hardware
program.build( QStringLiteral( "-cl-std=CL%1 -I%2" )
.arg( QgsOpenClUtils::activePlatformVersion( ) )
.arg( sourcePath() ).toStdString().c_str() );
// OpenCL version for compatibility with older hardware, but it's up to
// llvm to support latest CL versions
bool ok;
float version( QgsOpenClUtils::activePlatformVersion().toFloat( &ok ) );
if ( ok && version < 2.0f )
{
program.build( QStringLiteral( "-cl-std=CL%1 -I%2" )
.arg( QgsOpenClUtils::activePlatformVersion( ) )
.arg( sourcePath() ).toStdString().c_str() );
}
else
{
program.build( QStringLiteral( "-I%1" )
.arg( sourcePath() ).toStdString().c_str() );
}
}
catch ( cl::BuildError &e )
{
Expand Down
9 changes: 8 additions & 1 deletion src/core/qgsopenclutils.h
Expand Up @@ -21,7 +21,8 @@
#define CL_HPP_ENABLE_EXCEPTIONS
//#define CL_HPP_MINIMUM_OPENCL_VERSION 110
#define CL_USE_DEPRECATED_OPENCL_1_1_APIS
#define CL_HPP_TARGET_OPENCL_VERSION 200
#define CL_HPP_TARGET_OPENCL_VERSION 220
#define CL_TARGET_OPENCL_VERSION 220
#include <CL/cl2.hpp>

#include "qgis_core.h"
Expand Down Expand Up @@ -175,6 +176,12 @@ class CORE_EXPORT QgsOpenClUtils
//! Returns a string representation from an OpenCL \a errorCode
static QString errorText( const int errorCode );

/**
* Create an OpenCL command queue from the default context.
*
* This wrapper is required in order to prevent a crash when
* running on OpenCL platforms < 2
*/
static cl::CommandQueue commandQueue();

/**
Expand Down
7 changes: 0 additions & 7 deletions tests/src/core/testqgsopenclutils.cpp
Expand Up @@ -57,13 +57,6 @@ class TestQgsOpenClUtils: public QObject
void _testMakeRunProgram();
void _testMakeHillshade( const int loops );

cl::Program buildProgram( const cl::Context &context, const QString &source )
{
cl::Program program( context, source.toStdString( ) );
program.build( "-cl-std=CL1.1" );
return program;
}

std::string source()
{
std::string pgm = R"CL(
Expand Down

0 comments on commit a471934

Please sign in to comment.