这是OpenCL的第一个程序,所谓的helloword!本程序实现的是向量加法。首先从主要的主机API开始介绍。
#1.创建平台结构
cl_int clGetPlatformIDs(cl_int num_entries,
cl_platform_id *platforms,cl_uint *num_platforms);
参数说明:
num_entries:用户设定需要检测到的平台的上限,即可以放到platforms中的平台上限。既然是上限当然不可以设置为0.
platforms:存放检测到的平台;
num_platforms:实际检测到的平台数量;注意与num_entries区分。
在使用OpenCL时经常会用到一种“函数调用——内存分配——函数调用”的模式。下面以一个简单的例子来描述num_entries与num_platforms的区别。
//调用函数,获得num_platforms
error = clGetPlatformIDs(1, NULL, &num_platforms);
if (error != 0) {
printf("Check palrforms failed!");
return -1;
}
//分配内存空间
platforms_buffer = (cl_platform_id*)malloc(sizeof(cl_platform_id)*num_platforms);
//调用函数,将平台存入platforms_buffer
error = clGetPlatformIDs(num_platforms, platforms_buffer,NULL);
if (error != 0) {
printf("Get platforms failed!");
return -1;
}
#2.创建设备结构
cl_int clGetDeviceIDs(cl_platform_id platform,
cl_device_type device_type,
cl_uint num_entries,cl_device_id *devices,
cl_uint *num_devices);
该函数与创建平台结构的函数参数相同,增加了cl_platform_id参数和cl_device_type。这两个参数很容易理解,cl_device_type为枚举类型,任何一本OpenCl相关书籍都可查到其取值,不做赘述。cl_platform_id便是上一函数获得的平台ID。
error = clGetDeviceIDs(platforms_buffer[i], CL_DEVICE_TYPE_ALL,
1, NULL, &num_devices);
if (platforms == NULL) {
printf("Get GPU device failed!");
return -1;
}
devices = (cl_device_id*)malloc(sizeof(cl_device_id)*num_devices);
error = clGetDeviceIDs(platforms, CL_DEVICE_TYPE_GPU,
1, &devices, NULL);
if (error != 0) {
printf("Get device failed!");
return -1;
}
#3.创建上下文
上下文是命令队列创建的基础,而命令队列则是设备与主机之间通信的纽带,所以上下文在主机程序中扮演着重要角色。
主机可以通过多个上下文来管理设备,甚至一个平台可以创建多个上下文,但是每个上下文中的设备都必须来自同一个平台,不可以交叉。
创建上下文的函数有两个,这里介绍一个。
cl_context clCreateContext(const cl_context_properties *properties,
cl_uint num_devices,const cl_device_id *devices,
(void CL_CALLBACK *notify_func)(...),
void *user_data,cl_int *error);