非NVIDIA平台下的CUDA的替代方案OpenCL,第一步如何获取PlatformInfo、DeviceInfo

非NVIDIA平台下的CUDA的替代方案OpenCL,第一步如何获取PlatformInfo、DeviceInfo

介绍

当谈到高性能计算,NVIDIA的CUDA框架无疑是一个强大的工具。OpenC(Open Computing Language)是一个更为通用的解决方案,或者你使用的是非NVIDIA硬件,那么OpenCL是一个极佳的选择。作为一个开放标准,OpenCL支持广泛的硬件平台,包括CPU、GPU、DSP和FPGA。

OpenCL库API

clGetPlatformIDs

获取平台数量

clGetPlatformInfo

获取平台指定info信息

clGetDeviceIDs

获取设备数量

clGetDeviceInfo

获取设备指定info信息

platform_info定义

CL/cl.h

/* cl_platform_info */
#define CL_PLATFORM_PROFILE                         0x0900
#define CL_PLATFORM_VERSION                         0x0901
#define CL_PLATFORM_NAME                            0x0902
#define CL_PLATFORM_VENDOR                          0x0903
#define CL_PLATFORM_EXTENSIONS                      0x0904
#define CL_PLATFORM_HOST_TIMER_RESOLUTION           0x0905

device_info定义

CL/cl.h

/* cl_device_info */
#define CL_DEVICE_TYPE                                   0x1000
#define CL_DEVICE_VENDOR_ID                              0x1001
#define CL_DEVICE_MAX_COMPUTE_UNITS                      0x1002
#define CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS               0x1003
#define CL_DEVICE_MAX_WORK_GROUP_SIZE                    0x1004
#define CL_DEVICE_MAX_WORK_ITEM_SIZES                    0x1005
#define CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR            0x1006
#define CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT           0x1007
#define CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT             0x1008
#define CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG            0x1009
...

测试程序

仓库地址 https://github.com/LitchiCheng/OpenCL-Optimalization-Test

#include <iostream>
#include "string.h"
#include <chrono>
 
#include <CL/cl.h>

#define API_NUM 4
cl_platform_info platform_info[API_NUM] = {
    CL_PLATFORM_PROFILE,
    CL_PLATFORM_VERSION,
    CL_PLATFORM_NAME,
    CL_PLATFORM_VENDOR
};
 
int main(int argc, char const *argv[])
{
    cl_int status = 0;
    cl_uint numPlatforms;
    cl_platform_id platform = NULL;
    status = clGetPlatformIDs(0, NULL, &numPlatforms);
    if (status != CL_SUCCESS) {
        printf("ERROR: Getting Platforms.(clGetPlatformIDs)\n");
        return EXIT_FAILURE;
    }
    printf("\r\n");
    printf("clGetPlatformIDs num is %d \r\n", numPlatforms);
    if (numPlatforms > 0) {
        cl_platform_id *platforms = (cl_platform_id *)malloc(numPlatforms * sizeof(cl_platform_id));
        status = clGetPlatformIDs(numPlatforms, platforms, NULL);
        if (status != CL_SUCCESS) {
            printf("Error: Getting Platform Ids.(clGetPlatformIDs):%d\n", status);
            return -1;
        }
        for (int i = 0; i < numPlatforms; ++i) {
            for(int index=0; index < API_NUM; ++index){
                char charbuff[100];
                status = clGetPlatformInfo(
                            platforms[i],
                            platform_info[index],
                            sizeof(charbuff),
                            charbuff,
                            NULL);
                platform = platforms[i];
                printf("clGetPlatformInfo %s \r\n", charbuff);
                memset(charbuff, 0x00, sizeof(charbuff));
            }
        }
        delete platforms;
    }

    cl_uint num_device;
    cl_device_id device;
    status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &num_device);
    printf("GPU num is %d \r\n", num_device);
    status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);
    if (status != CL_SUCCESS) {
        printf("Error: clGetDeviceIDs:%d\n", status);
        return -1;
    }

    cl_uint device_max_compute_units;
    status = clGetDeviceInfo(device, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(cl_uint),
                            &device_max_compute_units, NULL);
    if (status != CL_SUCCESS) {
        printf("Error: CL_DEVICE_MAX_COMPUTE_UNITS:%d\n", status);
        return -1;
    }
    printf("CL_DEVICE_MAX_COMPUTE_UNITS %d \r\n", device_max_compute_units);

    cl_ulong device_global_mem_size;
    status = clGetDeviceInfo(device, CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(cl_ulong),
                            &device_global_mem_size, NULL);
    if (status != CL_SUCCESS) {
        printf("Error: CL_DEVICE_GLOBAL_MEM_SIZE:%d\n", status);
        return -1;
    }
    printf("CL_DEVICE_GLOBAL_MEM_SIZE %ld \r\n", device_global_mem_size);

    char device_name[100];
    status = clGetDeviceInfo(device, CL_DEVICE_NAME, sizeof(device_name),
                            device_name, NULL);
    if (status != CL_SUCCESS) {
        printf("Error: CL_DEVICE_NAME:%d\n", status);
        return -1;
    }    
    printf("CL_DEVICE_NAME %s \r\n", device_name);
    return 0;
}

输出结果

sudo ./getDeviceInfo 
arm_release_ver of this libmali is 'r18p0-01rel0', rk_so_ver is '4'.
clGetPlatformIDs num is 1 
clGetPlatformInfo FULL_PROFILE 
clGetPlatformInfo OpenCL 1.2 v1.r18p0-01rel0.ddd394a39c9049aa64d45a44032b5335 
clGetPlatformInfo ARM Platform 
clGetPlatformInfo ARM 
GPU num is 1 
CL_DEVICE_MAX_COMPUTE_UNITS 4 
CL_DEVICE_GLOBAL_MEM_SIZE 4026908672 
CL_DEVICE_NAME Mali-T860

clinfo对比

在这里插入图片描述

最近更新

  1. TCP协议是安全的吗?

    2024-04-01 02:30:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-01 02:30:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-01 02:30:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-01 02:30:02       20 阅读

热门阅读

  1. [内部类与常见API]String类

    2024-04-01 02:30:02       15 阅读
  2. 如何让div中滚动条一直保持在最下方

    2024-04-01 02:30:02       17 阅读
  3. 烂笔头,记录一年遗忘

    2024-04-01 02:30:02       16 阅读
  4. 【python】《流畅的python》读书笔记之第1-2章

    2024-04-01 02:30:02       14 阅读
  5. Redis 过期删除策略和内存淘汰策略

    2024-04-01 02:30:02       17 阅读
  6. dma123456

    2024-04-01 02:30:02       18 阅读
  7. 终于成功的编译合宙的DAP_LINK sdk

    2024-04-01 02:30:02       17 阅读
  8. 2024.3.30力扣刷题记录-二叉树学习记录2(未完)

    2024-04-01 02:30:02       20 阅读
  9. 《中华遗宝之王莽藏金之谜》

    2024-04-01 02:30:02       17 阅读