c++ - 在 OpenCL 库中找不到 cl::Error 类

标签 c++ error-handling opencl

我在一些代码中看到,在OpenCL库中,有一个名为cl::Error的类,通过它可以捕获OpenCL代码中的错误和错误类型.但是当我在我的代码中添加时,就像这样

#include <CL/cl.hpp>
#include <fstream>
#include <iostream>
#include <cassert>
#include <exception>
#define __CL_ENABLE_EXCEPTIONS

int main()
{
  std::vector<cl::Platform> platforms;
  cl::Platform::get(&platforms);

  assert(platforms.size() > 0);

  auto myPlatform = platforms.front();
  std::cout << "Using platform: " << myPlatform.getInfo<CL_PLATFORM_NAME>() << std::endl;

  std::vector<cl::Device> devices;
  myPlatform.getDevices(CL_DEVICE_TYPE_GPU, &devices);

  auto myDevice = devices.front();
  std::cout<< "Using device: "<< myDevice.getInfo<CL_DEVICE_NAME>() << std::endl;

  std::ifstream helloworldfile("helloWorldKernel.cl");
  std::string src(std::istreambuf_iterator<char>(helloworldfile), (std::istreambuf_iterator<char>()));

  cl::Program::Sources source(1,std::make_pair(src.c_str(), src.length() + 1));

  cl::Context context(myDevice);
  cl::Program program(context,source);

  cl::CommandQueue queue(context,myDevice);

  try
  {
    program.build("-cl-std=CL1.2");
  } catch(cl::Error& e)
  {
    std::cout << e.what() << std::cout;
  }

  int err;
  int szChar = 16;
  char  buf[szChar];

  cl::Buffer memBuf(context, CL_MEM_READ_WRITE, sizeof(char) * szChar);

  cl::Kernel kernel(program, "helloWorld", &err);

  if(err != CL_SUCCESS)
  {
    std::cout<<" Error in creating kernel, error: "<< err << std::endl;
    exit(1);
  } 


  kernel.setArg(0,memBuf);

  queue.enqueueTask(kernel);

  err =  queue.enqueueReadBuffer(memBuf,CL_TRUE, 0, sizeof(buf), buf);

  if(err != CL_SUCCESS)
  {
    std::cout<<" Error in reading from device, error: "<< err << std::endl;
    exit(1);
  }

  std::cout << buf;
  std::cin.get();

  return 0;

}

我得到的错误是

helloWorld.cc:46:12: error: expected type-specifier
   } catch (cl::Error& e)
            ^
helloWorld.cc:46:21: error: expected unqualified-id before ‘&’ token
   } catch (cl::Error& e)
                     ^
helloWorld.cc:46:21: error: expected ‘)’ before ‘&’ token
helloWorld.cc:46:21: error: expected ‘{’ before ‘&’ token
helloWorld.cc:46:23: error: ‘e’ was not declared in this scope
   } catch (cl::Error& e)
                       ^
helloWorld.cc:46:24: error: expected ‘;’ before ‘)’ token
   } catch (cl::Error& e)

我想知道该类是否存在或至少存在于特定版本的库 OpenCL 中,如果存在,应该如何调用它。

最佳答案

你需要定义__CL_ENABLE_EXCEPTIONS或者如果您使用的是最新的 #include <CL/cl2.hpp>你需要CL_HPP_ENABLE_EXCEPTIONS .

定义需要在包含 opencl header 之前。

关于c++ - 在 OpenCL 库中找不到 cl::Error 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59224203/

相关文章:

c++ - GCC 和 MinGW (C++) 之间奇怪的输出差异

ruby-on-rails - 上传到Cloudinary后对图像进行后期处理时,是否有办法解决ActiveStorage完整性错误?

python - 为什么会超出范围?

c - OpenCL 无法为常量数组设置内核参数

c - 我需要为 OpenCL 安装 Nvidia 的 SDK(CUDA) 来检测 Nvidia GPU 吗?

java - 任务间的 OpenCL 共享内存

android - 使用 Android NDK 时,Eclipse 编辑器将无法识别 C++ 模板类型

c++ - 类似函数的宏和奇怪的行为

C++ 可变参数函数 : use number of parameters as template argument

angularjs - 如何正确处理服务器端错误?