2

If I build an openCL Program from source code like this

cl::Program program = cl::Program(context, sourceCode);
program.build(devices);

I would like to check if this was successful. I saw a few examples of how to do this in C, but since my project ist in C++ I was wondering how to get (in case something goes wrong) a readable text message that indicates what might be the issue using the C++ wrapper.

I have also enabled exceptions

#define CL_HPP_ENABLE_EXCEPTIONS 

but do not know if build(...) throws an exception.

I am using the AMD APP SDK 3.0 and the cl2.hpp from the Khronos webpage (as it was not included in the SDK).

NOhs
  • 2,780
  • 3
  • 25
  • 59

1 Answers1

9

The cl::Program::build() function does indeed throw an exception if the build fails. Here's how you can get the build log:

cl::Program program = cl::Program(context, sourceCode);
try
{
  program.build(devices);
}
catch (cl::Error& e)
{
  if (e.err() == CL_BUILD_PROGRAM_FAILURE)
  {
    for (cl::Device dev : devices)
    {
      // Check the build status
      cl_build_status status = program.getBuildInfo<CL_PROGRAM_BUILD_STATUS>(dev);
      if (status != CL_BUILD_ERROR)
        continue;

      // Get the build log
      std::string name     = dev.getInfo<CL_DEVICE_NAME>();
      std::string buildlog = program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(dev);
      std::cerr << "Build log for " << name << ":" << std::endl
                << buildlog << std::endl;
  }
  else
  {
    throw e;
  }
}
jprice
  • 9,755
  • 1
  • 28
  • 32
  • If I have more than one device, I should also check the other devices (if they are not identical), I guess? – NOhs Jan 07 '16 at 18:41
  • @MrZ Yes. I've updated my answer with how you can check whether the build failed for each device and print the log if so. It's possible for a build to succeed with one device but fail on another. – jprice Jan 07 '16 at 18:53
  • I think there is a type where you call `getInfo`, your `d` should be `dev`. – NOhs Jan 07 '16 at 19:00
  • @MrZ Yep, sorry about that! Fixed. – jprice Jan 07 '16 at 19:09