Beispiel #1
0
        public static DeviceQuery QueryGpuStatus(string fullPciBusID)
        {
            var errorMessage = string.Empty;

            if (!Initialized)
            {
                return(null);
            }

            if (!GetDeviceByPciBusID(fullPciBusID, out NvmlDevice device, out errorMessage))
            {
                return(null);
            }

            if (!string.IsNullOrWhiteSpace(errorMessage))
            {
                CUDA.Log(Level.Error, $"API failed to get device by PCI bus ID ({fullPciBusID}): {errorMessage}");
                return(null);
            }

            var  utilization = new NvmlUtilization();
            uint fanSpeed = 0u, temperature = 0u, power = 0u, gpuClock = 0u, vramClock = 0u;

            var response =
                RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? PInvokeWindows.NvmlDeviceGetFanSpeed(device, ref fanSpeed)
                : RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? PInvokeLinux.NvmlDeviceGetFanSpeed(device, ref fanSpeed)
                : 12;

            if (response != SUCCESS)
            {
                CUDA.Log(Level.Error, $"nvmlDeviceGetFanSpeed() responded with an error: {NvmlErrorString(response)}");
            }

            response =
                RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? PInvokeWindows.NvmlDeviceGetTemperature(device, NvmlTemperatureSensors.Gpu, ref temperature)
                : RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? PInvokeLinux.NvmlDeviceGetTemperature(device, NvmlTemperatureSensors.Gpu, ref temperature)
                : 12;

            if (response != SUCCESS)
            {
                CUDA.Log(Level.Error, $"nvmlDeviceGetTemperature() responded with an error: {NvmlErrorString(response)}");
            }

            response =
                RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? PInvokeWindows.NvmlDeviceGetPowerUsage(device, ref power)
                : RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? PInvokeLinux.NvmlDeviceGetPowerUsage(device, ref power)
                : 12;

            if (response != SUCCESS)
            {
                CUDA.Log(Level.Error, $"nvmlDeviceGetPowerUsage() responded with an error: {NvmlErrorString(response)}");
            }

            response =
                RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? PInvokeWindows.NvmlDeviceGetClockInfo(device, NvmlClockType.Graphics, ref gpuClock)
                : RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? PInvokeLinux.NvmlDeviceGetClockInfo(device, NvmlClockType.Graphics, ref gpuClock)
                : 12;

            if (response != SUCCESS)
            {
                CUDA.Log(Level.Error, $"nvmlDeviceGetClockInfo() responded with an error: {NvmlErrorString(response)}");
            }

            response =
                RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? PInvokeWindows.NvmlDeviceGetClockInfo(device, NvmlClockType.Mem, ref vramClock)
                : RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? PInvokeLinux.NvmlDeviceGetClockInfo(device, NvmlClockType.Mem, ref vramClock)
                : 12;

            if (response != SUCCESS)
            {
                CUDA.Log(Level.Error, $"nvmlDeviceGetClockInfo() responded with an error: {NvmlErrorString(response)}");
            }

            response =
                RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? PInvokeWindows.NvmlDeviceGetUtilizationRates(device, ref utilization)
                : RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? PInvokeLinux.NvmlDeviceGetUtilizationRates(device, ref utilization)
                : 12;

            if (response != SUCCESS)
            {
                CUDA.Log(Level.Error, $"nvmlDeviceGetUtilizationRates() responded with an error: {NvmlErrorString(response)}");
            }

            if (utilization.Gpu == 0u && utilization.Mem == 0u && fanSpeed == 0u && temperature == 0u && power == 0u && gpuClock == 0u && vramClock == 0u)
            {
                return(null);
            }

            return(new DeviceQuery()
            {
                FanSpeed = $"{fanSpeed} %",
                Temperature = $"{temperature} C",
                PowerDraw = $"{GetNormalizedPower(fullPciBusID, (decimal)power / 1000):F2} W",
                ClockGPU = $"{gpuClock} MHz",
                ClockVRAM = $"{vramClock} MHz",
                UtilizationGPU = $"{utilization.Gpu} %",
                UtilizationVRAM = $"{utilization.Mem} %"
            });
        }