Skip to content
Permalink
Browse files

Wrap make program in OpenCL utils

  • Loading branch information
elpaso committed Aug 8, 2018
1 parent ebad360 commit 215bfd41d364bc86d400a4ddf338c6c03e8698e9
Showing with 65 additions and 39 deletions.
  1. +32 −0 src/core/qgsopenclutils.cpp
  2. +8 −1 src/core/qgsopenclutils.h
  3. +25 −38 tests/src/core/testqgsopenclutils.cpp
@@ -256,3 +256,35 @@ cl::Context QgsOpenClUtils::context()
return cl::Context();
}
}

cl::Program QgsOpenClUtils::buildProgram( const cl::Context &context, const QString &source, ExceptionBehavior exceptionBehavior )
{
cl::Program program;
try
{
program = cl::Program( context, source.toStdString( ) );
program.build( "-cl-std=CL1.1" );
}
catch ( cl::BuildError &e )
{
cl::BuildLogType build_logs = e.getBuildLog();
QString build_log;
if ( build_logs.size() > 0 )
build_log = QString::fromStdString( build_logs[0].second );
else
build_log = QObject::tr( "Build logs not available!" );
QString err = QObject::tr( "Error building OpenCL program: %1" )
.arg( build_log );
QgsMessageLog::logMessage( err, LOGMESSAGE_TAG, Qgis::Critical );
if ( exceptionBehavior == Throw )
throw e;
}
catch ( cl::Error &e )
{
QString err = QObject::tr( "Error %1 running OpenCL program in %2" )
.arg( errorText( e.err() ), QString::fromStdString( e.what() ) );
QgsMessageLog::logMessage( err, LOGMESSAGE_TAG, Qgis::Critical );
throw e;
}
return program;
}
@@ -39,14 +39,21 @@
class CORE_EXPORT QgsOpenClUtils
{
public:

enum ExceptionBehavior
{
Catch,
Throw
};

static bool enabled();
static bool available();
static void setEnabled( bool enabled );
static QString buildLog( cl::BuildError &e );
static QString sourceFromPath( const QString &path );
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();

/**
@@ -33,11 +33,18 @@ class TestQgsOpenClUtils: public QObject
void TestEnable();
void TestDisable();
void TestAvailable();
void testRunProgram();
void testMakeRunProgram();
void testContext();

private:

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(
@@ -70,7 +77,7 @@ void TestQgsOpenClUtils::TestAvailable()
}


void TestQgsOpenClUtils::testRunProgram()
void TestQgsOpenClUtils::testMakeRunProgram()
{

cl_int err = 0;
@@ -87,43 +94,23 @@ void TestQgsOpenClUtils::testRunProgram()
cl::Buffer b_buf( b_vec.begin(), b_vec.end(), true );
cl::Buffer c_buf( c_vec.begin(), c_vec.end(), false );

try
{
// This also works:
cl::Program program( source( ) );
program.build( "-cl-std=CL1.1" );

auto kernel =
cl::KernelFunctor <
cl::Buffer &,
cl::Buffer &,
cl::Buffer &
> ( program, "vectorAdd" );

kernel( cl::EnqueueArgs(
cl::NDRange( 3 )
),
a_buf,
b_buf,
c_buf
);
}
catch ( cl::BuildError e )
{
qDebug() << "OPENCL Build Error: " << QgsOpenClUtils::errorText( e.err() ) << e.what();
cl::BuildLogType build_logs = e.getBuildLog();
QString build_log;
if ( build_logs.size() > 0 )
build_log = QString::fromStdString( build_logs[0].second );
else
build_log = QObject::tr( "Build logs not available!" );
qDebug() << build_log;
cl::Program program = QgsOpenClUtils::buildProgram( ctx, QString::fromStdString( source() ) );

auto kernel =
cl::KernelFunctor <
cl::Buffer &,
cl::Buffer &,
cl::Buffer &
> ( program, "vectorAdd" );

kernel( cl::EnqueueArgs(
cl::NDRange( 3 )
),
a_buf,
b_buf,
c_buf
);

}
catch ( cl::Error e )
{
qDebug() << "OPENCL Error: " << e.err() << e.what();
}
cl::copy( c_buf, c_vec.begin(), c_vec.end() );
for ( size_t i = 0; i < c_vec.size(); ++i )
{

0 comments on commit 215bfd4

Please sign in to comment.
You can’t perform that action at this time.