在使用OpenCL编程中,需要对GPU设备的底层理解,这样才能更好的进行代码优化。
比如计算单元CU数量,每个CU的执行单元PE数量,每个CU中的共享内存大小等等。只有了解了这些才能更好的使用共享内存,设计核函数的运行参数等。
本文:http://www.cnblogs.com/xudong-bupt/p/3586050.html
1.clGetDeviceInfo
OpenCL使用clGetDeviceInfo函数获取设备具体,函数原型如下:
cl_int clGetDeviceInfo (
cl_device_id device, //设备id号
cl_device_info param_name, //枚举变量,要获取的设备信息名称
size_t param_value_size, //参数类型大小
void *param_value, //参数值
size_t *param_value_size_ret //参数类型大小
);
这个函数需要调用两次,第一次获取参数类型大小,第二次获取参数。
2.代码实例
2.1 tool.h 与tool.cpp
见:http://www.cnblogs.com/xudong-bupt/p/3582780.html
2.2 QueryDeviceInfo.cpp
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include "tool.h" 4 #include <CL/cl.h> 5 6 int main() 7 { 8 ///Get first available Platform 9 cl_platform_id platform; 10 getPlatform(platform); 11 12 ///get first available GPU 13 cl_device_id *devices=getCl_device_id(platform); 14 15 char *value; 16 size_t valueSize; 17 size_t maxWorkItemPerGroup; 18 cl_uint maxComputeUnits=0; 19 cl_ulong maxGlobalMemSize=0; 20 cl_ulong maxConstantBufferSize=0; 21 cl_ulong maxLocalMemSize=0; 22 23 ///print the device name 24 clGetDeviceInfo(devices[0], CL_DEVICE_NAME, 0, NULL, &valueSize); 25 value = (char*) malloc(valueSize); 26 clGetDeviceInfo(devices[0], CL_DEVICE_NAME, valueSize, value, NULL); 27 printf("Device Name: %s\n", value); 28 free(value); 29 30 /// print parallel compute units(CU) 31 clGetDeviceInfo(devices[0], CL_DEVICE_MAX_COMPUTE_UNITS,sizeof(maxComputeUnits), &maxComputeUnits, NULL); 32 printf("Parallel compute units: %u\n", maxComputeUnits); 33 34 ///maxWorkItemPerGroup 35 clGetDeviceInfo(devices[0], CL_DEVICE_MAX_WORK_GROUP_SIZE,sizeof(maxWorkItemPerGroup), &maxWorkItemPerGroup, NULL); 36 printf("maxWorkItemPerGroup: %zd\n", maxWorkItemPerGroup); 37 38 /// print maxGlobalMemSize 39 clGetDeviceInfo(devices[0], CL_DEVICE_GLOBAL_MEM_SIZE,sizeof(maxGlobalMemSize), &maxGlobalMemSize, NULL); 40 printf("maxGlobalMemSize: %lu(MB)\n", maxGlobalMemSize/1024/1024); 41 42 /// print maxConstantBufferSize 43 clGetDeviceInfo(devices[0], CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE,sizeof(maxConstantBufferSize), &maxConstantBufferSize, NULL); 44 printf("maxConstantBufferSize: %lu(KB)\n", maxConstantBufferSize/1024); 45 46 /// print maxLocalMemSize 47 clGetDeviceInfo(devices[0], CL_DEVICE_LOCAL_MEM_SIZE,sizeof(maxLocalMemSize), &maxLocalMemSize, NULL); 48 printf("maxLocalMemSize: %lu(KB)\n", maxLocalMemSize/1024); 49 50 free(devices); 51 return 0; 52 }
执行结果:
3.其他
在安装了OpenCL的平台,可以使用命令:clinfo
The OpenCL Specification : https://www.khronos.org/registry/cl/specs/opencl-1.2.pdf