ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

如何在Linux上获取连接显示器到gpu的数量?

2019-08-25 15:50:55  阅读:307  来源: 互联网

标签:linux cuda gpu nvidia nvapi


我需要确定给定的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) : 0x%08x\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 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,cuda,gpu,nvidia,nvapi
来源: https://codeday.me/bug/20190825/1720393.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有