好久没搞OpenCL了,但是这是个好东西,不能不学,之前发了篇设置OpenCL的文章,看的人还真多,看来大家都知道这个好东西了,都想把OpenCL搞起。不过学习难度还是相当高的。
之前忙搞算法,所以很多其他知识就暂且搁置了,不过告诉大家一个秘密:自从用了靖心博客之后,腰不酸,腿不疼了……啊,不好意思,谁插播了广告,我是说自从学好算法之后,看OpenCL,OpenGL, DirectX头不疼,脑不涨了。
一个全新的境界就此诞生了。
下面是注释程序,目的就是查看自己计算机上所有支持OpenCL的设备,并打印信息。
主函数调用run()就可以运行了。要设置请看我另一篇OpenCl设置文章。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef MAC
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
namespace device_ext_test
{
int run() {
/* Host/device data structures */
cl_platform_id *platforms;
cl_device_id *devices;
cl_uint num_platforms;
cl_uint num_devices, addr_data;
cl_int i, err;
/* Extension data */
char name_data[48], ext_data[4096];
err = clGetPlatformIDs(5, NULL, &num_platforms);
if(err < 0) {
perror("Couldn't find any platforms.");
exit(1);
}
/* 选取所有的platforms*/
platforms = (cl_platform_id*)
malloc(sizeof(cl_platform_id) * num_platforms);
err = clGetPlatformIDs(num_platforms, platforms, NULL);
if(err < 0) {
perror("Couldn't find any platforms");
exit(1);
}
//循环查看所有platforms的devices信息,一般intel和AMD的都可以有两个devices:CPU和显卡
//如果是nvidia的就一般只有一个显卡device了。
for (int j = 0; j < (int)num_platforms; j++)
{
printf("\nplatform %d\n", j+1);
/* 步骤和platforms的一样 */
err = clGetDeviceIDs(platforms[j], CL_DEVICE_TYPE_ALL, 1, NULL, &num_devices);
if(err < 0) {
perror("Couldn't find any devices");
exit(1);
}
/* Access connected devices */
devices = (cl_device_id*)
malloc(sizeof(cl_device_id) * num_devices);
clGetDeviceIDs(platforms[j], CL_DEVICE_TYPE_ALL,
num_devices, devices, NULL);
/*循环显示platform的所有device(CPU和显卡)信息。*/
for(i=0; i<(int)num_devices; i++) {
err = clGetDeviceInfo(devices[i], CL_DEVICE_NAME,
sizeof(name_data), name_data, NULL);
if(err < 0) {
perror("Couldn't read extension data");
exit(1);
}
clGetDeviceInfo(devices[i], CL_DEVICE_ADDRESS_BITS,
sizeof(ext_data), &addr_data, NULL);
clGetDeviceInfo(devices[i], CL_DEVICE_EXTENSIONS,
sizeof(ext_data), ext_data, NULL);
printf("NAME: %s\nADDRESS_WIDTH: %u\nEXTENSIONS: %s\n\n",
name_data, addr_data, ext_data);
}
}
free(platforms);
free(devices);
printf("\n");
system("pause");
return 0;
}
}