Advertisement
homer512

OpenCL enum devices

Aug 13th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.29 KB | None | 0 0
  1. #include <cl.h>
  2. /* using cl_* */
  3.  
  4. #include <stdlib.h>
  5. /* using malloc, free */
  6. #include <stdio.h>
  7. /* using printf */
  8.  
  9.  
  10. static cl_int get_platforms(cl_platform_id** ids, cl_uint* count)
  11. {
  12.   cl_int rtrn;
  13.   if((rtrn = clGetPlatformIDs(0, NULL, count)) != CL_SUCCESS)
  14.     return rtrn;
  15.   if(! *count) {
  16.     *ids = NULL;
  17.     return rtrn;
  18.   }
  19.   if(! (*ids = malloc(*count * sizeof(**ids))))
  20.     return CL_OUT_OF_HOST_MEMORY;
  21.   if((rtrn = clGetPlatformIDs(*count, *ids, count)) != CL_SUCCESS)
  22.     free(*ids);
  23.   return rtrn;
  24. }
  25.  
  26. static cl_int get_devices(cl_platform_id platform, cl_device_id** ids, cl_uint* count)
  27. {
  28.   cl_int rtrn;
  29.   if((rtrn = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, count)) != CL_SUCCESS)
  30.     return rtrn;
  31.   if(! *count) {
  32.     *ids = NULL;
  33.     return rtrn;
  34.   }
  35.   if(! (*ids = malloc(*count * sizeof(**ids))))
  36.     return CL_OUT_OF_HOST_MEMORY;
  37.   if((rtrn = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, *count, *ids, count)) != CL_SUCCESS)
  38.     free(*ids);
  39.   return rtrn;
  40. }
  41.  
  42. static cl_int get_device_name(cl_device_id device, char** name)
  43. {
  44.   cl_int rtrn;
  45.   size_t size;
  46.   if((rtrn = clGetDeviceInfo(device, CL_DEVICE_NAME, 0, NULL, &size)) != CL_SUCCESS)
  47.     return rtrn;
  48.   if(! (*name = malloc(size)))
  49.     return CL_OUT_OF_HOST_MEMORY;
  50.   if((rtrn = clGetDeviceInfo(device, CL_DEVICE_NAME, size, *name, &size)) != CL_SUCCESS)
  51.     free(*name);
  52.   return rtrn;
  53. }
  54.  
  55. static cl_int print_device_names(void)
  56. {
  57.   cl_platform_id* platforms;
  58.   cl_uint platform_count, platform;
  59.   cl_device_id* devices;
  60.   cl_int rtrn;
  61.   if((rtrn = get_platforms(&platforms, &platform_count)) != CL_SUCCESS)
  62.     return rtrn;
  63.   for(platform = 0; platform < platform_count; ++platform) {
  64.     cl_uint device_count, device;
  65.     if((rtrn = get_devices(platforms[platform], &devices, &device_count)) != CL_SUCCESS)
  66.       goto free_platforms;
  67.     for(device = 0; device < device_count; ++device) {
  68.       char* name;
  69.       if((rtrn = get_device_name(devices[device], &name)) != CL_SUCCESS)
  70.         goto free_devices;
  71.       printf("%s\n", name);
  72.       free(name);
  73.     }
  74.     free(devices);
  75.   }
  76.   goto free_platforms;
  77.  free_devices:
  78.   free(devices);
  79.  free_platforms:
  80.   free(platforms);
  81.   return rtrn;
  82. }
  83.  
  84. int main(void)
  85. {
  86.   return print_device_names() == CL_SUCCESS ? 0 : 1;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement