OpenCL 错误类在主机C++给出错误

OpenCL error Class gives error in C++ host

本文关键字:错误 出错 C++ OpenCL 主机      更新时间:2023-10-16

我正在尝试在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 标头的版本。

相关文章: