我正在尝试在C ++ OpenCL主机中捕获错误,并且无法编译。这是程序:
#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
使用bazel构建时,出现以下错误:
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 { ^~~~~
在类头文件(libcldrive.h)中,包含头文件cl2.hpp。此代码与旧的标头一起使用,将旧的cl.hpp更新为cl2.hpp时开始出现错误。我在新的标题中查找该类,并且该类仍然存在,所以我不明白为什么它会给出错误。
提前感谢。
[Hans已经在注释中链接到有关同一问题的另一个问题,但是尽管正确,但我在此找不到特别完整,有用或友好的答案。
本质上,在OpenCL错误上引发异常是C ++包装程序的可选功能。 (至少可以说C ++中的异常是有争议的),因此需要使用CL_HPP_ENABLE_EXCEPTIONS
预处理程序宏,之前 #include
使用包装程序头显式启用它。
This is documented in the C++ wrapper's documentation here.
文档也提供了一个有用的示例,其中启用了此功能:
#define CL_HPP_ENABLE_EXCEPTIONS
#define CL_HPP_TARGET_OPENCL_VERSION 200
#include <CL/cl2.hpp>
请注意,您的构建也警告您不要定义CL_HPP_TARGET_OPENCL_VERSION
-您可能应该这样做以匹配OpenCL标头的版本。