我目前正在为OpenCL内核编写一些单元测试,需要创建一个上下文。由于我不是在追求性能,所以哪台设备运行内核对我来说并不重要。因此,我想创建尽可能少的限制的上下文,并考虑以下代码:
#define __CL_ENABLE_EXCEPTIONS
#include <CL/cl.hpp>
#include <iostream>
int main() {
try {
cl::Context context(CL_DEVICE_TYPE_ALL);
}catch(const cl::Error &err) {
std::cerr << "Caught cl::Error (" << err.what() << "): " << err.err() << "n";
}
}
哪个返回
捕获cl::错误(clCreateContextFromType):-32
-32是CL_INVALID_PLATFORM,clCreateContextFromType的文档显示:
CL_INVALID_PLATFORM,如果属性为NULL且无法选择平台,或者如果属性中指定的平台值不是有效平台。
由于我没有提供任何属性,它们当然是NULL。为什么不能选择任何平台
我也尝试过CL_DEVICE_TYPE_DEFAULT
,得到了同样的结果。
以下是检测到的我的平台和设备的列表:
NVIDIA CUDA
(GPU) GeForce GTX 560
作为侧节点:在属性中指定平台可以按照预期工作。
我使用CL_DEVICE_TYPE_ALL
尝试了您的代码,它在我的设置中起到了作用。我不知道为什么它对你的不起作用。。。
作为一种变通方法,也许你可以做这样的事情:
// Get all available platforms
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
// Pick the first and get all available devices
std::vector<cl::Device> devices;
platforms[0].getDevices(CL_DEVICE_TYPE_ALL, &devices);
// Create a context on the first device, whatever it is
cl::Context context(devices[0]);
cl::Context context(cl_device_type)
正在使用默认参数调用完整的构造函数:
cl::Context::Context(cl_device_type type,
cl_context_properties * properties = NULL,
void(CL_CALLBACK *notifyFptr)(const char *,const void *,::size_t,void *) = NULL,
void * data = NULL,
cl_int * err = NULL
)
Witch只是C++对底层clCreateContextFromType()
的包装器。
此函数允许将NULL指针作为属性传递,但平台选择取决于实现。在您的情况下,它看起来并没有默认为nVIDIA平台。
恐怕你必须把一些信息传递给构造函数。。。。