您的位置:首页 > 运维架构

OpenCL 查看设备信息

2016-01-25 17:51 429 查看
好久没搞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;
}

}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: