2013-08-01 37 views
2

我需要確定給定的CUDA設備是否連接了顯示器。我不知道這樣做的CUDA功能。如何在Linux上獲取連接顯示器的數量到gpu?

在Windows上,我可以使用NVAPI獲取每臺設備的連接顯示器數量和PCI總線/插槽ID。使用後者,我可以找到匹配的CUDA設備(通過調用cudaGetDeviceProperties)。

如何在Linux上執行相同的NVAPI操作?

從技術上講,我需要的是一個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(); 
+0

也許使用'lsof'命令。或者'/ proc /'下的一些深層次的東西。 –

+0

你可以檢測monito是否開啓/關閉cuda?因此,當你有了這個id時,你可以調用另一個函數,也許......!? – alap

+0

@ Laszlo-AndrasZsurzsa我認爲這與cuda或GPU沒有任何關係。 – hthms

回答

6

Linux下最相似的方法是使用NVCtrl API是什麼nvidia-settings,Linux的NVIDIA控制面板應用程序,提供。

如何下載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 
+1

cudaDevAttrKernelExecTimeout通常是一個很好的指導。如果有捆綁,那就意味着X11正在管理設備。 – talonmies

+0

方法2僅適用於特斯拉和Quadro,所以不幸我無法使用它。方法1工作正常,但似乎我只能從運行X服務器的機器使用,而不能通過遠程終端。 – hthms

+0

對於方法1,可能會遇到X服務器權限問題。你是否以root用戶身份運行該應用程序? –

相關問題