在 OpenCL 库中找不到 cl::Error 类

Pej*_*vak 1 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;

}
Run Code Online (Sandbox Code Playgroud)

我得到的错误是

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)

Run Code Online (Sandbox Code Playgroud)

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

Ala*_*les 5

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