Reputation: 39
I am trying to catch an error in a C++ OpenCL host and it does not compile. This is the program:
#include "gpu/cldrive/libcldrive.h"
#include "gpu/cldrive/kernel_arg_value.h"
#include "gpu/cldrive/kernel_driver.h"
#include "gpu/clinfo/libclinfo.h"
#include "labm8/cpp/logging.h"
#include "labm8/cpp/status.h"
#include "labm8/cpp/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/strip.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#define CL_HPP_ENABLE_EXCEPTIONS
#define LOG_CL_ERROR(level, error) \
LOG(level) << "OpenCL exception: " << error.what() << ", error: " \
<< labm8::gpu::clinfo::OpenClErrorString(error.err());
namespace gpu {
namespace cldrive {
namespace {
// Attempt to build OpenCL program.
labm8::StatusOr<cl::Program> BuildOpenClProgram(
const std::string& opencl_kernel, const cl::Context& context,
const string& cl_build_opts) {
auto start_time = absl::Now();
try {
// Assemble the build options. We need -cl-kernel-arg-info so that we can
// read the kernel signatures.
string all_build_opts = "-cl-kernel-arg-info ";
absl::StrAppend(&all_build_opts, cl_build_opts);
labm8::TrimRight(all_build_opts);
cl::Program program;
// SPIR-V Compilation
if(opencl_kernel.substr( opencl_kernel.length() - 4 ) == ".spv") {
std::vector<char> cl_kernel(opencl_kernel.begin(), opencl_kernel.end());
program = cl::Program{context, cl_kernel};
// OPENCL:
} else {
program = cl::Program{context, opencl_kernel};
}
program.build(context.getInfo<CL_CONTEXT_DEVICES>(),
all_build_opts.c_str());
auto end_time = absl::Now();
auto duration = (end_time - start_time) / absl::Milliseconds(1);
LOG(INFO) << "clBuildProgram() with options '" << all_build_opts
<< "' completed in " << duration << " ms";
return program;
} catch (cl::Error e) {
LOG_CL_ERROR(WARNING, e);
return labm8::Status(labm8::error::Code::INVALID_ARGUMENT,
"clBuildProgram failed");
}
}
} // namespace
When building with bazel, it gives me the following error:
ERROR: /home/enrique/Escritorio/cldrive/gpu/cldrive/BUILD:315:1: Couldn't build file gpu/cldrive/_objs/libcldrive/libcldrive.pic.o: C++ compilation of rule '//gpu/cldrive:libcldrive' failed (Exit 1) gcc failed: error executing command /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -g0 -O2 '-D_FORTIFY_SOURCE=1' -DNDEBUG -ffunction-sections ... (remaining 299 argument(s) skipped) Use --sandbox_debug to see verbose messages from the sandbox In file included from ./gpu/cldrive/libcldrive.h:21:0, from gpu/cldrive/libcldrive.cc:16: ./third_party/opencl/cl.hpp:438:109: note: #pragma message: cl2.hpp: CL_HPP_TARGET_OPENCL_VERSION is not defined. It will default to 220 (OpenCL 2.2) # pragma message("cl2.hpp: CL_HPP_TARGET_OPENCL_VERSION is not defined. It will default to 220 (OpenCL 2.2)") ^ gpu/cldrive/libcldrive.cc: In function 'labm8::StatusOr<cl::Program> gpu::cldrive::{anonymous}::BuildOpenClProgram(const string&, const cl::Context&, const string&)': gpu/cldrive/libcldrive.cc:76:16: error: 'Error' in namespace 'cl' does not name a type } catch (cl::Error e) { ^~~~~ gpu/cldrive/libcldrive.cc:77:27: error: 'e' was not declared in this scope LOG_CL_ERROR(WARNING, e); ^ gpu/cldrive/libcldrive.cc:34:41: note: in definition of macro 'LOG_CL_ERROR' LOG(level) << "OpenCL exception: " << error.what() << ", error: " \ ^~~~~ gpu/cldrive/libcldrive.cc: In member function 'void gpu::cldrive::Cldrive::RunOrDie(gpu::cldrive::Logger&)': gpu/cldrive/libcldrive.cc:93:16: error: 'Error' in namespace 'cl' does not name a type } catch (cl::Error error) { ^~~~~ gpu/cldrive/libcldrive.cc:95:41: error: 'error' was not declared in this scope << " Raised by: " << error.what() << '\n' ^~~~~ gpu/cldrive/libcldrive.cc:95:41: note: suggested alternative: In file included from ./gpu/cldrive/logger.h:21:0, from ./gpu/cldrive/libcldrive.h:18, from gpu/cldrive/libcldrive.cc:16: ./labm8/cpp/status.h:41:11: note: 'labm8::error' namespace error { ^~~~~
In the class header (libcldrive.h) the header cl2.hpp is included. This code worked with old headers, it began giving error when updating the old cl.hpp to cl2.hpp. I look for the class in the new header and it still exists, so i do not understand why it is giving error.
Thanks in advance.
Upvotes: 0
Views: 1307
Reputation: 23428
Hans has already linked to another question about the same issue in the comments, but although correct, I don't find the answer there particularly complete, helpful, or friendly.
Essentially, raising exceptions on OpenCL errors is an optional feature of the C++ wrapper. (exceptions in C++ are controversial to say the least) So it needs to be explicitly enabled using the CL_HPP_ENABLE_EXCEPTIONS
preprocessor macro, before #include
ing the wrapper header.
This is documented in the C++ wrapper's documentation here.
The documentation also gives a helpful example, where this feature is enabled:
#define CL_HPP_ENABLE_EXCEPTIONS
#define CL_HPP_TARGET_OPENCL_VERSION 200
#include <CL/cl2.hpp>
Note that your build is also warning you about not defining CL_HPP_TARGET_OPENCL_VERSION
- you probably should do that to match your OpenCL headers' version.
Upvotes: 1