从OpenCL中获取的设备列表中确定主机CPU?

时间:2014-03-06 16:10:15

标签: opencl

我通过传递clGetDeviceIDs数组并获取所有可能的设备来调用cl_device_id。现在从这个列表中我想删除实际上是主机CPU的设备? 有没有任何万无一失的方法来做到这一点? 因为可能有2个完全相同的CPU安装,然后cl_device_info可能没有帮助区分2个CPU?

2 个答案:

答案 0 :(得分:1)

尝试使用此代码,它将为您提供对客户端设备的控制。

//See how many platforms do we have
int num_platforms;
int ret = clGetPlatformIDs(0, NULL, &num_platforms)
if(ret != CL_SUCCESS) {fprintf(stderr, "%d\n", ret);}

//Collect list of platforms
cl_platform_id *platforms = (cl_platform_id*)calloc(num_platforms, sizeof(cl_platform_id));
ret = clGetPlatformIDs(num_platforms, platforms, NULL);
if(ret != CL_SUCCESS) {fprintf(stderr, "%d\n", ret);}

//Collecting list of Devices for every platform
for(int i=0; i<num_platforms; i++){
    int num_CPUs, num_GPUs;
    cl_device_id *CPUs, *GPUs;

    //Get number of CPUs & GPUs on client machine
    ret = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_CPU, 0, NULL, &num_CPUs);
    if(ret != CL_SUCCESS) {fprintf(stderr, "%d\n", ret);}

    ret = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_GPU, 0, NULL, &num_GPUs);
    if(ret != CL_SUCCESS) {fprintf(stderr, "%d\n", ret);}

    //Allocate memory & collect actual information
    CPUs = (cl_device_id*)calloc(num_CPUs, sizeof(cl_device_id);
    GPUs = (cl_device_id*)calloc(num_GPUs, sizeof(cl_device_id);

    ret = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_CPU,num_CPUs, CPUs, NULL);
    if(ret != CL_SUCCESS) {fprintf(stderr, "%d\n", ret);}

    ret = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_GPU,num_GPUs, GPUs, NULL);
    if(ret != CL_SUCCESS) {fprintf(stderr, "%d\n", ret);}

    //Do whatever you want with Devices
    ....

    free(CPUs);
    free(GPUs);
}

free(platforms);

答案 1 :(得分:1)

在OpenCL 1.1及更高版本中,您可以CL_DEVICE_HOST_UNIFIED_MEMORY使用clGetDeviceInfo来检查设备和主机是否具有统一的内存子系统。