示例#1
0
        public static decimal GetDeviceVramUtilization(string fullPciBusID, out string errorMessage)
        {
            errorMessage = string.Empty;
            if (!Initialized)
            {
                return(decimal.MinusOne);
            }

            if (!GetDeviceByPciBusID(fullPciBusID, out NvmlDevice device, out errorMessage))
            {
                return(decimal.MinValue);
            }

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

            if (response == SUCCESS)
            {
                return(utilization.Mem);
            }

            errorMessage = NvmlErrorString(response);
            return(decimal.MinValue);
        }
示例#2
0
 public static extern nvmlReturn nvmlDeviceGetUtilizationRates([In] IntPtr device, [Out] out NvmlUtilization utilization);
示例#3
0
 public static extern int NvmlDeviceGetUtilizationRates(NvmlDevice device, ref NvmlUtilization utilization);
示例#4
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} %"
            });
        }