驱动分析
1. 根据config.h¶
Bash
# 启用的驱动
#define BUILD_BASIC // ✅ 已启用
#define BUILD_PTHREAD // ✅ 已启用
# 其他设备驱动都是未启用的:
/* #undef BUILD_HSA */ // ❌ AMD GPU (HSA)
/* #undef BUILD_CUDA */ // ❌ NVIDIA GPU (CUDA)
/* #undef BUILD_ALMAIF */ // ❌ 硬件加速器
/* #undef BUILD_VULKAN */ // ❌ Vulkan 设备
/* #undef BUILD_LEVEL0 */ // ❌ Intel Level0 (GPU)
/* #undef BUILD_REMOTE_SERVER */
/* #undef BUILD_REMOTE_CLIENT */
/* #undef BUILD_PROXY */
实际使用的驱动: pthread
这是 pthread 驱动,因为:
- basic 驱动优先级较低(用于测试和调试)
pthread驱动会自动优先被选择用于多核 CPU
2. 驱动代码位置¶
1. pthread 驱动(你实际使用的)¶
主要代码位置: pthread
关键文件:
- 主驱动实现(493 行)
-
初始化、多线程调度
-
pthread_scheduler.c
- 工作组调度器
-
实现多线程并行执行内核
-
pthread_utils.c- 工具函数 -
pocl-pthread.h- 头文件 -
pthread_barrier.c- 线程屏障实现
驱动特点:
C++
// 来自 pthread.c
void pocl_pthread_init_device_ops(struct pocl_device_ops *ops)
{
pocl_basic_init_device_ops(ops); // 继承 basic 的基础实现
ops->device_name = "cpu"; // 设备名称
// pthread 特有的多线程实现
ops->probe = pocl_pthread_probe;
ops->run = pocl_pthread_run; // 多线程执行
ops->join = pocl_pthread_join;
ops->submit = pocl_pthread_submit;
ops->notify = pocl_pthread_notify;
// ...
}
2. basic 驱动(基础实现)¶
主要代码位置: basic
关键文件:
- 基础驱动实现(1131 行)
- 提供最小化的单线程实现
-
pthread 驱动继承并扩展它
-
basic.h - 头文件
驱动特点:
C++
// 来自 basic.c 注释
/**
* The purpose of the 'basic' device driver is to serve as an example of
* a minimalistic (but still working) device driver for pocl.
*
* It is a "native device" without multithreading and uses malloc
* directly for buffer allocation etc. It also executes work groups in
* their sequential (increasing) order, thus makes it useful as a test
* device.
*/
3. 公共设备代码¶
位置: devices
- devices.c - 设备注册和管理(679 行)
common.c- 公共驱动函数cpuinfo.c- CPU 信息检测bufalloc.c- 缓冲区分配
设备注册(devices.c):
C++
// 所有可用驱动的初始化函数数组
static init_device_ops pocl_devices_init_ops[] = {
#ifdef BUILD_BASIC
INIT_DEV (basic), // 位置 0
#endif
#ifdef BUILD_PTHREAD
INIT_DEV (pthread), // 位置 1
#endif
// ... 其他驱动
};
// 驱动名称数组
char pocl_device_types[POCL_NUM_DEVICE_TYPES][30] = {
#ifdef BUILD_BASIC
"basic",
#endif
#ifdef BUILD_PTHREAD
"pthread",
#endif
// ...
};
3. 驱动选择机制¶
查看 pthread.c 中的 probe 函数:
C++
unsigned int
pocl_pthread_probe(struct pocl_device_ops *ops)
{
int env_count = pocl_device_get_env_count(ops->device_name);
/* 如果环境变量未指定,pthread 将被优先使用 */
if(env_count < 0)
return 1; // 返回 1 表示找到 1 个设备
return env_count;
}
而 basic.c 中:
C++
unsigned int
pocl_basic_probe(struct pocl_device_ops *ops)
{
int env_count = pocl_device_get_env_count(ops->device_name);
/* 如果环境变量未指定,pthread 将被优先使用,basic 返回 0 */
if(env_count < 0)
return 0; // basic 主动让位给 pthread
return env_count;
}
📝 总结¶
| 项目 | 值 |
|---|---|
| 实际使用驱动 | pthread |
| 编译启用驱动 | basic + pthread |
| 设备类型 | CPU (HOST) |
| 主要代码 | pthread |
| 基础代码 | basic |
| 设备管理 | devices.c |
| 并行方式 | POSIX 多线程 (pthreads) |
| 支持的 CPU | Intel Alderlake (12代酷睿 i5-12400) |
| 核心数 | 12 个计算单元(6P+0E核,12线程) |
你的向量加法程序使用的就是这个 pthread CPU 驱动,它通过 POSIX 线程在你的 12 核 CPU 上并行执行 OpenCL 内核!