I need to determine whether a given CUDA device has displays connected or not. I know no CUDA function which does this.
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).
How can I do the same on Linux, where NVAPI is not available?
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();
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.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.
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 innv-control-targets.c
. The following function in that file will do what you want: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.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.
Actually, upon re-reading your question, NVML Display Mode might be sufficient for what you want. Referring to the API documentation here, p46: