由于服务器用的是很老的集显,选择CPU版本的OpenCL用以测试
其中POCL是硬件通用的,所以选择用它,但是不同厂商的有自己的优化,会更快点
UBuntu环境
安装POCL
安装依赖
sudo apt install pocl-opencl-icd ocl-icd-libopencl1 clinfo
sudo apt install ocl-icd-opencl-dev
pocl-opencl-icd: 提供POCL的OpenCL实现,用于运行OpenCL应用程序(必须)
ocl-icd-libopencl1:提供OpenCL的加载器,连接应用程序和OpenCL平台(必须)
clinfo: 用于查询和调试OpenCL平台和设备信息的工具,可选但推荐
ocl-icd-opencl-dev: 开发必须,包含头文件和库文件
默认头文件在usr/include/CL目录下,执行文件在/usr/lib/x86_64-linux-gnu目录下
安装完执行clinfo可以看到平台信息则成功安装
配置环境变量(可选)
POCL默认会安装相关ICD文件到/etc/OpenCL/vendors/目录下,不需要额外配置,但是如果有多个OPenCL平台,可以通过设置环境变量明确指定使用POCL
export OCL_ICD_VENDORS=/etc/OpenCL/vendors/
将该命令添加到~/.bashrc或者~/.zshrc中,取决自己用的哪个bash
暂不需要
编写测试程序
cmake_minimum_required(VERSION 3.30)
project(testOpenGl)
set(CMAKE_CXX_STANDARD 11)
add_executable(testOpenGl main.cpp
testOpenCl.cpp
testOpenCl.h)
# 查找OpenCL
find_package(OpenCL REQUIRED)
# 链接OpenCl库
target_include_directories(testOpenGl PRIVATE ${OpenCL_INCLUDE_DIRS})
target_link_libraries(testOpenGl PRIVATE ${OpenCL_LIBRARIES})
//
// Created by lai on 2025/1/16.
//
#include "testOpenCl.h"
#include <CL/cl.h>
#include <iostream>
#include <vector>
void TestOpenCl::runTests() {
cl_uint platformCount;
clGetPlatformIDs(0, nullptr, &platformCount);
std::cout << "Number of OpenCL platforms: " << platformCount << std::endl;
std::vector<cl_platform_id> platforms(platformCount);
clGetPlatformIDs(platformCount, platforms.data(), nullptr);
for (cl_uint i = 0; i < platformCount; i++) {
char platformName[128];
clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME, 128, platformName, nullptr);
std::cout << "Platform " << i + 1 << ": " << platformName << std::endl;
cl_uint deviceCount;
clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 0, nullptr, &deviceCount);
std::cout << " Number of devices: " << deviceCount << std::endl;
std::vector<cl_device_id> devices(deviceCount);
clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, deviceCount, devices.data(), nullptr);
for (cl_uint j = 0; j < deviceCount; j++) {
char deviceName[128];
clGetDeviceInfo(devices[j], CL_DEVICE_NAME, 128, deviceName, nullptr);
std::cout << " Device " << j + 1 << ": " << deviceName << std::endl;
}
}
}
输出
Number of OpenCL platforms: 1
Platform 1: Portable Computing Language
Number of devices: 1
Device 1: pthread-12th Gen Intel(R) Core(TM) i5-1240P
1079

被折叠的 条评论
为什么被折叠?



