OpenCL 错误类在主机C++给出错误
OpenCL error Class gives error in C++ host
我正在尝试在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++例外是有争议的(因此,在#include
包装标头之前,需要使用CL_HPP_ENABLE_EXCEPTIONS
预处理器宏显式启用它。
这记录在此处的C++包装器文档中。
该文档还提供了一个有用的示例,其中启用了此功能:
#define CL_HPP_ENABLE_EXCEPTIONS
#define CL_HPP_TARGET_OPENCL_VERSION 200
#include <CL/cl2.hpp>
请注意,您的构建也会警告您不要定义CL_HPP_TARGET_OPENCL_VERSION
- 您可能应该这样做以匹配 OpenCL 标头的版本。
相关文章:
- 分段错误当我试图运行程序时出错
- .h 和.cpp文件分离时出错,但仅使用 .h 文件时没有错误.我做错了什么?
- 创建 OpenCV 非自由版本 v4.3 时出错,可折叠.cpp错误 C2039、2605
- 制作时出错,C++"db.c:2127:错误:在"||"标记之前应使用";"
- C++语法错误,编译器不会警告或 int v = func(&v) 出错;
- 在VS2015中访问类成员时运行时错误,但在Linux上未访问时出错
- 如何更改路径以修复错误"./main:加载共享库 libmkl_core.so 时出错?
- 为什么在定义类之前声明类的对象会在友元类中给出错误,而在友元函数中不会出错
- 安装 mply 库时出错.致命错误:gsl/gsl_sf.h:没有这样的文件或目录
- Rcpp/C++/R:比较日期时间矢量与日期时间时出错(错误:"运算符>"的不明确重载)
- 编译特征程序时出错:错误:'seq'不是'Eigen'的成员
- 可能的 GCC 链接器错误会导致将弱符号和局部符号链接在一起时出错
- 非标准语法;使用 '&' 创建指向成员的指针错误,将成员函数分配给向量时出错
- 为 freebsd 11 编译 gcc4.8.5 时出错: 错误: 未知类型名称 'choke'
- 运算符重载时出错(错误:"运算符<<不匹配(操作数类型为"std::basic_ostream<char>"和"const char [2]")
- 我同时使用了莱布尼茨和瓦利斯公式来估算π但输出数字错误,我不知道我哪里出错了
- 显示错误消息框时出错.我该如何显示它
- 使用 swig 时出错:输入 (1) 中的语法错误
- 编译类 C++ 中的线程时出错(错误 xthread)
- 在 Visual Studio 2015 社区中消除错误时出错