NVML运行时错误:未定义符号nvmlDeviceGetComputeRunningProcesses_v3 求助
解决NVML接口符号未定义错误
问题根源
你遇到的undefined symbol: nvmlDeviceGetComputeRunningProcesses_v3错误,本质是驱动版本与CUDA工具链版本不匹配:
nvmlDeviceGetComputeRunningProcesses_v3是NVIDIA驱动495.x及以上版本才新增的NVML接口- 你当前使用的驱动版本是470.141.03,属于CUDA 11.4/11.5对应的驱动分支,完全不支持这个_v3接口
- CUDA 11.7的头文件中已经移除了旧的
nvmlDeviceGetComputeRunningProcesses_v2接口定义,导致你替换后缀后出现编译错误;去掉后缀时,头文件会默认映射到_v3,因此仍会触发运行时符号缺失。
解决方法
方法1:升级NVIDIA驱动(推荐)
将驱动升级到与CUDA 11.7兼容的版本(推荐515.43.04及以上),升级后系统中的NVML库会包含nvmlDeviceGetComputeRunningProcesses_v3接口,运行时即可找到符号。
方法2:适配当前驱动,使用兼容接口
如果无法升级驱动,需要手动适配470.x驱动支持的nvmlDeviceGetComputeRunningProcesses_v2接口,步骤如下:
修改代码,手动添加_v2接口原型
因为CUDA 11.7的nvml.h已经移除了_v2的定义,需要在代码中手动声明:#include <stdio.h> #include <stdlib.h> #include <nvml.h> // 手动声明_v2接口原型 nvmlReturn_t nvmlDeviceGetComputeRunningProcesses_v2(nvmlDevice_t device, unsigned int* count, nvmlProcessInfo_t* infos); int main(int argc, char* argv[]) { int i; unsigned int deviceCount; nvmlReturn_t result; char version[64], pname[256]; nvmlProcessInfo_t infos[32]; nvmlDevice_t device; unsigned int infoCount = 32; // 必须初始化,传入数组的最大容量 // 初始化NVML(原始代码漏了这一步,必须先执行) if ((result = nvmlInit()) != NVML_SUCCESS) { fprintf(stderr, "error: nvmlInit %d\n", result); return 1; } if ((result = nvmlDeviceGetHandleByIndex_v2(0, &device)) != NVML_SUCCESS) { fprintf(stderr, "error: nvmlDeviceGetHandleByIndex_v2 %d\n", result); nvmlShutdown(); return 1; } if ((result = nvmlDeviceGetComputeRunningProcesses_v2(device, &infoCount, infos)) != NVML_SUCCESS) { fprintf(stderr, "error: Get Compute Running Processes error: %d\n", result); nvmlShutdown(); return 1; } // 可在此处添加进程信息处理逻辑 nvmlShutdown(); return 0; }正确编译链接NVML库
编译时需要显式链接NVIDIA ML库,命令如下:nvcc your_code.c -o nvml_lib -lnvidia-ml
额外说明
- NVML接口的版本依赖于驱动版本,而非CUDA工具链版本,必须确保代码中使用的接口在当前驱动中存在
- 可以通过
nvmlSystemGetDriverVersion()获取当前驱动版本,通过nvmlInit()后的nvmlSystemGetNVMLVersion()获取NVML版本,用于确认接口兼容性
内容的提问来源于stack exchange,提问作者fizwit
相关产品推荐
相关产品推荐

