我需要确定给定的CUDA设备是否已连接显示器.我知道没有CUDA功能可以做到这一点.
在Windows上,我可以使用NVAPI获取连接显示器的数量以及每个设备的PCI总线/插槽ID.使用后者,我可以找到匹配的CUDA设备(通过调用cudaGetDeviceProperties).
如何在没有NVAPI的Linux上做同样的事情?
从技术上讲,我需要的是Linux替代以下代码:
NvAPI_Initialize();NvPhysicalGpuHandle gpuHandles[64];NvU32 numOfGPUs;NvAPI_EnumPhysicalGPUs(gpuHandles, &numOfGPUs);for (int i = 0; i < numOfGPUs; i ){ NvU32 connected_displays = 0; NvU32 busId = 0; NvU32 busSlotId = 0; NvAPI_GPU_GetConnectedDisplayIds(gpuHandles[i], NULL, &connected_displays, NULL); NvAPI_GPU_GetBusId(gpuHandles[i], &busId); NvAPI_GPU_GetBusSlotId(gpuHandles[i], &busSlotId); printf("Current device: %d\n", i); printf("Number of connected displays: %u\n", connected_displays); printf("Bus id: %u\tBus slot id: %u\n", busId, busSlotId);}NvAPI_Unload();
解决方法:
Linux下最类似的方法是使用NVCtrl API,它是linux NVIDIA控制面板应用程序提供的nvidia-settings.
linux驱动程序发行说明中介绍了如何下载nvidia-settings源包.具体来说,您可以找到特定驱动程序版本here的各种包
选择最接近您的驱动程序版本的包.
下载并解压缩nvidia-settings源后,您将找到一个samples目录.在该目录中是一个示例程序,它具有您想要的必要框架.具体来说,请查看nv-control-targets.c.该文件中的以下函数将执行您想要的操作:
/* Connected Display Devices on GPU */ ret = XNVCTRLQueryTargetAttribute(dpy, NV_CTRL_TARGET_TYPE_GPU, gpu, // target_id 0, // display_mask NV_CTRL_CONNECTED_DISPLAYS, &display_devices); if (!ret) { fprintf(stderr, "Failed to query connected displays\n"); return 1; } printf(" Display Device Mask (Connected) : 0xx\n", display_devices);
请注意,该程序(nv-control-targets.c)顶部还有一些预备/设置函数调用也需要执行.
在NVML中还有一个功能(显示模式)(nvidia-smi基于NVML),它将告知您GPU是否正在托管显示器,但我不确定它是否为您提供了所需的粒度.
实际上,在重新阅读您的问题时,NVML显示模式可能足以满足您的需求.参考API文档here,第46页:
7.10.2.10 nvmlReturn_t DECLDIR nvmlDeviceGetDisplayMode (nvmlDevice_t device, nvmlEnableState_t display)Retrieves the display mode for the device.For Tesla ™and Quadro ®products from the Fermi and Kepler families.This method indicates whether a physical display is currently connected to the device.See nvmlEnableState_t for details on allowed modes.Parameters:device The identifier of the target devicedisplay Reference in which to return the display modeReturns:· NVML_SUCCESS if display has been set· NVML_ERROR_UNINITIALIZED if the library has not been successfully initialized· NVML_ERROR_INVALID_ARGUMENT if device is invalid or display is NULL· NVML_ERROR_NOT_SUPPORTED if the device does not support this feature
来源:https://www.icode9.com/content-3-409351.html
联系客服