【问题标题】:How to get the number of connected displays to a gpu on Linux?如何在 Linux 上获取连接到 gpu 的显示器数量?
【发布时间】:2013-08-02 10:12:57
【问题描述】:

我需要确定给定的 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();

【问题讨论】:

  • 也许使用lsof 命令。或者/proc/下的一些深层次的东西。
  • 你能用 cuda 检测 monito 是否打开/关闭吗?所以基本上当你有 id 时,你可能会调用另一个函数......!?
  • @Laszlo-AndrasZsurzsa 我认为这与 cuda 或 GPU 无关。
  • 我同意我正在寻找解决此问题的软件。

标签: linux cuda gpu nvidia nvapi


【解决方案1】:

Linux 下最类似的方法是使用 NVCtrl API,这是 linux NVIDIA 控制面板应用程序nvidia-settings 提供的。

如何下​​载 nvidia-settings 源代码包在 linux 驱动程序发行说明中有记录。具体可以找到针对特定驱动版本的各种包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) : 0x%08x\n",
           display_devices);

请注意,该程序 (nv-control-targets.c) 顶部的一些准备/设置函数调用也需要执行。

NVML(nvidia-smi 基于 NVML)中还有一个功能(显示模式),它会通知您 GPU 是否正在托管显示器,但我不确定它是否为您提供你想要的粒度。

实际上,在重新阅读您的问题后,NVML 显示模式可能足以满足您的需求。参考API文档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

【讨论】:

  • cudaDevAttrKernelExecTimeout 通常是一个很好的指南。如果有 tiemout,则表示 X11 正在管理设备。
  • 方法 2 仅适用于 Tesla 和 Quadro,所以很遗憾我不能使用它。方法 1 工作正常,但似乎我只能从运行 X 服务器的机器上使用,而不是通过远程终端。
  • 对于方法 1,您可能遇到了 X 服务器权限问题。您是否以 root 用户身份运行应用程序?
猜你喜欢
  • 2014-10-28
  • 1970-01-01
  • 1970-01-01
  • 2012-08-10
  • 1970-01-01
  • 2014-05-22
  • 2021-10-22
  • 1970-01-01
  • 2013-02-01
相关资源
最近更新 更多