我正在尝试编写一个简单的opencl程序,但我似乎无法获得我的显卡。
#include "stdafx.h"
#include <stdio.h>
#include "CL/cl.h"
#define DATA_SIZE 10
const char *KernelSource =
"__kernel void hello(__global float * input, __global float *output)\n"\
"{\n"\
" size_t id = get_global_id(0);\n"\
" output[id] = input[id] * input[id]; \n"\
"}\n"\
"\n";
int main()
{
cl_context context;
cl_context_properties properties[3];
cl_kernel kernel;
cl_command_queue command_queue;
cl_program program;
cl_int err;
cl_uint num_of_platforms=0;
cl_platform_id platform_id;
cl_platform_id * platform_ids;
cl_device_id device_id;
cl_uint num_of_devices=0;
cl_mem input, output;
size_t global;
float inputData[DATA_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
float results[DATA_SIZE] = {0};
int i;
if (clGetPlatformIDs(1, &platform_id, &num_of_platforms) != CL_SUCCESS)
{
printf("Unable to get platform_id\n");
return 1;
}
if (clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, 1, &device_id, &num_of_devices) != CL_SUCCESS)
{
printf("Unable to get device_id\n");
return 1;
}
return 0;
}
该程序打印Unable to get device_id
当我输入一个断点并检查num_of_platforms
时。看来我有3个平台。
我有一个intel CPU和一个NVIDIA GPU(quadro K4000)
但是,它说我有0个CL_DEVICE_TYPE_GPU
我可以在设备管理器中看到我的GPU可见
我认为这可能与another problem我在同一台PC上使用cloo有关。
似乎唯一列出的opencl设备是我的CPU
我的CPU(1.2)支持的Opencl版本比GPU(1.1)更晚。
我不确定如何进一步调试这个......
答案 0 :(得分:6)
您正试图从第一个平台获取GPU设备。你怎么知道它必须在第一个平台上?你应该尝试所有的平台。
if (clGetPlatformIDs(0, NULL, &num_of_platforms) != CL_SUCCESS)
{
printf("Unable to get platform_id\n");
return 1;
}
cl_platform_id *platform_ids = new cl_platform_id[num_of_platforms];
if (clGetPlatformIDs(num_of_platforms, &platform_ids, NULL) != CL_SUCCESS)
{
printf("Unable to get platform_id\n");
return 1;
}
bool found = false;
for(int i=0; i<num_of_platforms; i++)
if(clGetDeviceIDs(&platform_id[i], CL_DEVICE_TYPE_GPU, 1, &device_id, &num_of_devices) == CL_SUCCESS){
found = true;
break;
}
if(!found){
printf("Unable to get device_id\n");
return 1;
}