Determine host CPU from list of devices obtained in OpenCL?

久未见 提交于 2020-01-15 12:23:49

问题


I am calling clGetDeviceIDs by passing an array of cl_device_id and getting all possible devices. Now from this list I want to remove the device which is actually the host CPU? Is there any fool proof way to do this? Because there might be 2 exactly identical CPU installed then cl_device_info might not be helpful in differentiating the 2 CPU?


回答1:


In OpenCL 1.1 and later, you can check if the device and the host have a unified memory subsystem by using CL_DEVICE_HOST_UNIFIED_MEMORY with clGetDeviceInfo.




回答2:


Try this code, it will give you wanted control over client devices.

//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);


来源:https://stackoverflow.com/questions/22229856/determine-host-cpu-from-list-of-devices-obtained-in-opencl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!