Skip to content

Commit

Permalink
Wrap make program in OpenCL utils
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Aug 8, 2018
1 parent ebad360 commit 215bfd4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 39 deletions.
32 changes: 32 additions & 0 deletions src/core/qgsopenclutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
9 changes: 8 additions & 1 deletion src/core/qgsopenclutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

/**
Expand Down
63 changes: 25 additions & 38 deletions tests/src/core/testqgsopenclutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -70,7 +77,7 @@ void TestQgsOpenClUtils::TestAvailable()
}


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

cl_int err = 0;
Expand All @@ -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 )
{
Expand Down

0 comments on commit 215bfd4

Please sign in to comment.