如何在Linux上获取连接的显示器数量到gpu? [英] How to get the number of connected displays to a gpu on Linux?
问题描述
我需要确定给定的CUDA设备是否显示连接。我知道没有CUDA功能这样做。
I need to determine whether a given CUDA device has displays connected or not. I know no CUDA function which does this.
在Windows上,我可以使用NVAPI获取连接的显示器数量和每个设备的PCI总线/插槽ID。使用后者,我可以找到匹配的CUDA设备(通过调用cudaGetDeviceProperties)。
On Windows, I can use NVAPI to get the number of connected displays and the PCI bus/slot id of each device. Using the latter, I can find the matching CUDA device (by calling cudaGetDeviceProperties).
如何在Linux上执行同样的操作,NVAPI不可用?
How can I do the same on Linux, where NVAPI is not available?
技术上,我需要的是一个Linux替代下面的代码:
Technically, what I need is a Linux alternative to the following code:
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是什么 nvidia-settings
,linux NVIDIA控制面板应用程序,提供。
The most analogous approach under Linux would be to use the NVCtrl API which is what nvidia-settings
, the linux NVIDIA control panel application, provides.
如何下载nvidia-settings源包在linux驱动程序发布说明中有记录。具体来说,您可以找到特定驱动程序版本的各种软件包此处
How to download the nvidia-settings source package is documented in the linux driver release notes. Specifically, you can find various packages for specific driver versions here
选择最接近您的驱动程序版本的程序包。
Choose a package that is closest to your driver version.
下载并解压nvidia设置源后,您会发现 samples
目录。在该目录中是一个示例程序,它具有所需的框架。具体来说,看看 nv-control-targets.c
。该文件中的以下函数将执行您想要的操作:
Once you have downloaded and unzipped the nvidia-settings source, you will find a samples
directory. In that directory is a sample program which has the necessary framework for what you want. Specifically, look in nv-control-targets.c
. The following function in that file will do what you want:
/* 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) : 0x%08x\n",
display_devices);
注意,在该程序的顶部有一些预备/安装函数调用
Note that there are some preparatory/setup function calls at the top of that program (nv-control-targets.c
) that will need to be executed as well.
还有一个函数(显示模式)在 NVML (nvidia-smi基于NVML)中,它会通知您GPU是否托管
There is also a function (Display Mode) in NVML (nvidia-smi is based on NVML) that will inform you of whether the GPU is hosting a display or not, but I'm not sure it gives you the granularity you want.
实际上,在重新阅读您的问题后,NVML显示模式可能足以满足您的需求你要。请参阅此处的API文档,p46:
Actually, upon re-reading your question, NVML Display Mode might be sufficient for what you want. Referring to the API documentation here, p46:
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 device
display Reference in which to return the display mode
Returns:
• 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
这篇关于如何在Linux上获取连接的显示器数量到gpu?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!