赞
踩
每个平台可能关联不同的设备,在把内核程序发送到OpenCL设备上执行前,编程人员需要指定执行该内核代码的计算设备。OpenCL中设备对象的类型为cl_device_id
,我们可以通过以下函数来查询支持OpenCL设备列表。
cl_int clGetDeviceIDs(cl_platform_id platform,
cl_device_type device_type,
cl_uint num_entries,
cl_device_id *devices,
cl_uint *num_devices)
该函数的调用与clGetPlatformIDs
函数类似,第一次调用时,devices参数设置为NULL,num_devices返回指定平台中的设备数。第二次调用,根据第一次调用得到的设备数量分配控件,调用该函数来初始化可用设备。
#include <stdio.h> #include <stdlib.h> #include <CL/cl.h> void checkErr(cl_int err, int num) { if (CL_SUCCESS != err) { printf("OpenCL error(%d) at %d\n", err, num - 1); } } int main(int argc,char ** argv) { cl_device_id* device; cl_platform_id platform; cl_int err; cl_uint NumDevice; //选择第一个平台 err = clGetPlatformIDs(1, &platform, NULL); checkErr(err, __LINE__); err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &NumDevice); checkErr(err, __LINE__); device = (cl_device_id*)malloc(sizeof(cl_device_id) * NumDevice); //选择GPU设备 err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, NumDevice, device, NULL); checkErr(err, __LINE__); for (int i = 0; i < NumDevice; i++) { //查询设备的名称 char buffer[110]; err = clGetDeviceInfo(device[i], CL_DEVICE_NAME, 100, buffer, NULL); checkErr(err, __LINE__); printf("Device Name:%s\n", buffer); //查询设备计算单元最大数目 cl_uint UnitNum; err = clGetDeviceInfo(device[i], CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(cl_uint), &UnitNum, NULL); checkErr(err, __LINE__); printf("Compute Units Number: %d\n", UnitNum); //查询设备核心频率 cl_uint frequency; err = clGetDeviceInfo(device[i], CL_DEVICE_MAX_CLOCK_FREQUENCY, sizeof(cl_uint), &frequency, NULL); checkErr(err, __LINE__); printf("Device Frequency: %d(MHz)\n", frequency); //查询设备全局内存大小 cl_ulong GlobalSize; err = clGetDeviceInfo(device[i], CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(cl_ulong), &GlobalSize, NULL); checkErr(err, __LINE__); printf("Device Global Size: %0.0f(MB)\n", (float)GlobalSize/1024/1024); //查询设备全局内存缓存行 cl_uint GlobalCacheLine; err = clGetDeviceInfo(device[i], CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE, sizeof(cl_uint), &GlobalCacheLine, NULL); checkErr(err, __LINE__); printf("Device Global CacheLine: %d(Byte)\n", GlobalCacheLine); //查询设备支持的OpenCL版本 char DeviceVersion[110]; err = clGetDeviceInfo(device[i], CL_DEVICE_VERSION, 100, DeviceVersion, NULL); checkErr(err, __LINE__); printf("Device Version:%s\n", DeviceVersion); //查询设备扩展名 char* DeviceExtensions; cl_uint ExtenNum; err = clGetDeviceInfo(device[i], CL_DEVICE_EXTENSIONS, 0, NULL, &ExtenNum); checkErr(err, __LINE__); DeviceExtensions = (char*)malloc(ExtenNum); err = clGetDeviceInfo(device[i], CL_DEVICE_EXTENSIONS, ExtenNum, DeviceExtensions, NULL); checkErr(err, __LINE__); printf("Device Extensions: %s\n", DeviceExtensions); free(DeviceExtensions); } free(device); return 0; }
Device Name:Intel(R) HD Graphics 5500
Compute Units Number: 24
Device Frequency: 950(MHz)
Device Global Size: 1489(MB)
Device Global CacheLine: 64(Byte)
Device Version:OpenCL 2.0
Device Extensions: cl_intel_accelerator cl_intel_advanced_motion_estimation cl_intel_ctz cl_intel_d3d11_nv12_media_sharing cl_intel_dx9_media_sharing cl_intel_motion_estimation cl_intel_simultaneous_sharing cl_intel_subgroups cl_khr_3d_image_writes cl_khr_byte_addressable_store cl_khr_d3d10_sharing cl_khr_d3d11_sharing cl_khr_depth_images cl_khr_dx9_media_sharing cl_khr_gl_depth_images cl_khr_gl_event cl_khr_gl_msaa_sharing cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_gl_sharing cl_khr_icd cl_khr_image2d_from_buffer cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_mipmap_image cl_khr_mipmap_image_writes cl_khr_spir
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。