本文介绍了在OpenCL库中找不到cl :: Error类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在一些代码中看到OpenCL库中有一个名为 cl :: Error
的类,通过该类,中的错误和错误类型可以捕获OpenCL
代码。但是当我添加代码时,就像这样
I saw in some code that in OpenCL library, there is a class named cl::Error
by which the error and type of error in OpenCL
code can be catched. But when I added in my code, like this
#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库中存在,如果存在,应该如何调用它。
I was wondering if the class exist or at least exist in specific version of library OpenCL, if so, how should be invoke it.
推荐答案
您需要定义 __ CL_ENABLE_EXCEPTIONS
,或者使用的是关于最新的 #include< CL / cl2.hpp>
,您需要 CL_ENABLE_EXCEPTIONS
。
You need to define __CL_ENABLE_EXCEPTIONS
or if you're using the more up to date #include <CL/cl2.hpp>
you need CL_ENABLE_EXCEPTIONS
.
这篇关于在OpenCL库中找不到cl :: Error类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!