Skip to content

Commit

Permalink
Program build throws a more precise exception that stores build log
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristian-Popov authored and jszuppe committed Nov 28, 2017
1 parent ce38fb7 commit 286c7dc
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
44 changes: 44 additions & 0 deletions include/boost/compute/exception/program_build_failure.hpp
@@ -0,0 +1,44 @@
#ifndef BOOST_COMPUTE_EXCEPTION_PROGRAM_BUILD_FAILURE_HPP
#define BOOST_COMPUTE_EXCEPTION_PROGRAM_BUILD_FAILURE_HPP

#include <string>

#include <boost/compute/exception/opencl_error.hpp>

namespace boost {
namespace compute {

/// \class program_build_failure
/// \brief A failure when building OpenCL program
///
/// Instances of this class are thrown when OpenCL program build fails.
/// Extends opencl_error by saving a program build log so it can be used
/// for testing, debugging, or logging purposes.
///
/// \see opencl_error
class program_build_failure : public opencl_error
{
public:
/// Creates a new program_build_failure exception object for \p error
/// and \p build_log.
explicit program_build_failure(cl_int error, const std::string& build_log)
throw()
: opencl_error(error),
m_build_log(build_log)
{
}

/// Retrieve the log of a failed program build.
std::string build_log() const throw()
{
return m_build_log;
}

private:
std::string m_build_log;
};

} // end compute namespace
} // end boost namespace

#endif // BOOST_COMPUTE_EXCEPTION_PROGRAM_BUILD_FAILURE_HPP
3 changes: 2 additions & 1 deletion include/boost/compute/program.hpp
Expand Up @@ -23,6 +23,7 @@
#include <boost/compute/config.hpp>
#include <boost/compute/context.hpp>
#include <boost/compute/exception.hpp>
#include <boost/compute/exception/program_build_failure.hpp>
#include <boost/compute/detail/assert_cl_success.hpp>

#ifdef BOOST_COMPUTE_USE_OFFLINE_CACHE
Expand Down Expand Up @@ -276,7 +277,7 @@ class program
#endif

if(ret != CL_SUCCESS){
BOOST_THROW_EXCEPTION(opencl_error(ret));
BOOST_THROW_EXCEPTION(program_build_failure(ret, build_log()));
}
}

Expand Down
28 changes: 28 additions & 0 deletions test/test_program.cpp
Expand Up @@ -16,6 +16,7 @@
// thrown when invalid kernel code is passed to program::build().
#undef BOOST_COMPUTE_DEBUG_KERNEL_COMPILATION

#include <boost/compute/exception/program_build_failure.hpp>
#include <boost/compute/kernel.hpp>
#include <boost/compute/system.hpp>
#include <boost/compute/program.hpp>
Expand Down Expand Up @@ -344,4 +345,31 @@ BOOST_AUTO_TEST_CASE(build_log)
}
}

BOOST_AUTO_TEST_CASE(program_build_exception)
{
const char invalid_source[] =
"__kernel void foo(__global int *input) { !@#$%^&*() }";

compute::program invalid_program =
compute::program::create_with_source(invalid_source, context);

BOOST_CHECK_THROW(invalid_program.build(),
compute::program_build_failure);

try {
invalid_program.build();

// should not get here
BOOST_CHECK(false);
}
catch(compute::program_build_failure& e){
BOOST_CHECK(e.build_log() == invalid_program.build_log());
}
catch(...)
{
// should not get here
BOOST_CHECK(false);
}
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 286c7dc

Please sign in to comment.