Example #1
0
        public static decimal GetDeviceVramClock(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 clock    = 0u;
            var response =
                RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? PInvokeWindows.NvmlDeviceGetClockInfo(device, NvmlClockType.Mem, ref clock)
                : RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? PInvokeLinux.NvmlDeviceGetClockInfo(device, NvmlClockType.Mem, ref clock)
                : 12;

            if (response == SUCCESS)
            {
                return(clock);
            }

            errorMessage = NvmlErrorString(response);
            return(decimal.MinValue);
        }
Example #2
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);
        }
Example #3
0
        public static decimal GetDeviceNormalizedPower(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 power    = 0u;
            var response =
                RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? PInvokeWindows.NvmlDeviceGetPowerUsage(device, ref power)
                : RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? PInvokeLinux.NvmlDeviceGetPowerUsage(device, ref power)
                : 12;

            if (response == SUCCESS)
            {
                return(GetNormalizedPower(fullPciBusID, (decimal)power / 1000));
            }

            errorMessage = NvmlErrorString(response);
            return(decimal.MinValue);
        }
Example #4
0
        public static decimal GetDeviceTemperature(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 temperature = 0u;
            var 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)
            {
                return(temperature);
            }

            errorMessage = NvmlErrorString(response);
            return(decimal.MinValue);
        }
Example #5
0
        public static decimal GetDeviceFanSpeed(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 fanSpeed = 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)
            {
                return(fanSpeed);
            }

            errorMessage = NvmlErrorString(response);
            return(decimal.MinValue);
        }
Example #6
0
        public static bool Initialize(out string errorMessage)
        {
            var response =
                RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? PInvokeWindows.NvmlInit()
                : RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? PInvokeLinux.NvmlInit()
                : 12;

            Initialized = (response == SUCCESS);

            errorMessage = Initialized ? string.Empty : NvmlErrorString(response);

            return(Initialized);
        }
Example #7
0
        private static bool GetDeviceByPciBusID(string fullPciBusID, out NvmlDevice device, out string errorMessage)
        {
            device = new NvmlDevice();

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

            errorMessage = (response == SUCCESS)
                 ? string.Empty
                 : NvmlErrorString(response);

            return(response == SUCCESS);
        }
Example #8
0
        private static string NvmlErrorString(int response)
        {
            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                switch (response)
                {
                case 0: return("Success");

                case 1: return("Uninitialized");

                case 2: return("InvalidArgument");

                case 3: return("NotSupported");

                case 4: return("NoPermission");

                case 6: return("NotFound");

                case 9: return("DriverNotLoaded");

                case 12: return("LibraryNotFound");

                case 13: return("FunctionNotFound");

                case 17: return("OperatingSystem");

                case 18: return("LibRMVersionMismatch");

                case 999: return("Unknown");

                default: return(string.Empty);
                }
            }

            return(Marshal.PtrToStringAnsi(
                       RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? PInvokeWindows.NvmlErrorString(response)
                : RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? PInvokeLinux.NvmlErrorString(response)
                : IntPtr.Zero).TrimEnd('\0'));
        }
Example #9
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} %"
            });
        }