public override void Update() { this.coreTemp = 0; this.fanSpeed = 0; this.power = 0; this.ramUsage = 0; this.ramTotal = 0; this.coreUsage = 0; if (NvmlNativeMethods.nvmlDeviceGetUUID(device, out string uuidOut) == nvmlReturn.Success) { if (this.uuid == uuidOut) { this.failure = false; } else { this.failure = true; } } else { this.failure = true; } if (!this.failure) { uint coreTemp = 0; if (NvmlNativeMethods.nvmlDeviceGetTemperature(device, nvmlTemperatureSensors.Gpu, ref coreTemp) == nvmlReturn.Success) { this.coreTemp = coreTemp; } if (NVAPI.NvAPI_GPU_GetTachReading(this.NVAPIhandle, out int value) == NvStatus.OK) { this.fanSpeed = (uint)value; } uint powerUsage = 0; if (NvmlNativeMethods.nvmlDeviceGetPowerUsage(device, ref powerUsage) == nvmlReturn.Success) { this.power = powerUsage / 1000; } nvmlMemory memInfo = new nvmlMemory(); if (NvmlNativeMethods.nvmlDeviceGetMemoryInfo(device, ref memInfo) == nvmlReturn.Success) { this.ramTotal = (memInfo.total / 1024) / 1024; } nvmlUtilization coreInfo = new nvmlUtilization(); if (NvmlNativeMethods.nvmlDeviceGetUtilizationRates(device, ref coreInfo) == nvmlReturn.Success) { this.ramUsage = coreInfo.memory; this.coreUsage = coreInfo.gpu; } } }
public List <NvGpu> GetGpus() { List <NvGpu> results = new List <NvGpu>(); try { if (NvmlInit()) { _nvmlDevices.Clear(); uint deviceCount = 0; var r = NvmlNativeMethods.nvmlDeviceGetCount(ref deviceCount); CheckResult(r, () => $"{nameof(NvmlNativeMethods.nvmlDeviceGetCount)} {r.ToString()}"); for (int i = 0; i < deviceCount; i++) { NvGpu gpu = new NvGpu { GpuIndex = i }; nvmlDevice nvmlDevice = new nvmlDevice(); r = NvmlNativeMethods.nvmlDeviceGetHandleByIndex((uint)i, ref nvmlDevice); _nvmlDevices.Add(nvmlDevice); CheckResult(r, () => $"{nameof(NvmlNativeMethods.nvmlDeviceGetHandleByIndex)}({((uint)i).ToString()}) {r.ToString()}"); r = NvmlNativeMethods.nvmlDeviceGetName(nvmlDevice, out string name); CheckResult(r, () => $"{nameof(NvmlNativeMethods.nvmlDeviceGetName)} {r.ToString()}"); nvmlMemory memory = new nvmlMemory(); r = NvmlNativeMethods.nvmlDeviceGetMemoryInfo(nvmlDevice, ref memory); CheckResult(r, () => $"{nameof(NvmlNativeMethods.nvmlDeviceGetMemoryInfo)} {r.ToString()}"); // short gpu name if (!string.IsNullOrEmpty(name)) { name = name.Replace("GeForce GTX ", string.Empty); name = name.Replace("GeForce ", string.Empty); } nvmlPciInfo pci = new nvmlPciInfo(); r = NvmlNativeMethods.nvmlDeviceGetPciInfo(nvmlDevice, ref pci); CheckResult(r, () => $"{nameof(NvmlNativeMethods.nvmlDeviceGetPciInfo)} {r.ToString()}"); gpu.Name = name; gpu.BusId = (int)pci.bus; gpu.TotalMemory = memory.total; results.Add(gpu); } } } catch { } return(results); }
public NVIDIAGpuSet(INTMinerRoot root) { _root = root; this.Properties = new List <GpuSetProperty>(); if (NvmlInit()) { NvmlNativeMethods.nvmlDeviceGetCount(ref deviceCount); for (int i = 0; i < deviceCount; i++) { Gpu gpu = Gpu.Create(i, string.Empty); nvmlDevice nvmlDevice = new nvmlDevice(); var nvmlReturn = NvmlNativeMethods.nvmlDeviceGetHandleByIndex((uint)i, ref nvmlDevice); SetGpuStatus(gpu, nvmlReturn); NvmlNativeMethods.nvmlDeviceGetName(nvmlDevice, out string name); nvmlMemory memory = new nvmlMemory(); NvmlNativeMethods.nvmlDeviceGetMemoryInfo(nvmlDevice, ref memory); // short gpu name if (!string.IsNullOrEmpty(name)) { name = name.Replace("GeForce GTX ", string.Empty); name = name.Replace("GeForce ", string.Empty); } gpu.Name = name; gpu.TotalMemory = memory.total; _gpus.Add(i, gpu); } if (deviceCount > 0) { NvmlNativeMethods.nvmlSystemGetDriverVersion(out _driverVersion); NvmlNativeMethods.nvmlSystemGetNVMLVersion(out string nvmlVersion); this.Properties.Add(new GpuSetProperty(GpuSetProperty.DRIVER_VERSION, "驱动版本", _driverVersion)); try { double driverVersionNum; if (double.TryParse(_driverVersion, out driverVersionNum)) { var item = root.SysDicItemSet.GetSysDicItems("CudaVersion") .Select(a => new { Version = double.Parse(a.Value), a }) .OrderByDescending(a => a.Version) .FirstOrDefault(a => driverVersionNum >= a.Version); if (item != null) { this.Properties.Add(new GpuSetProperty("CudaVersion", "Cuda版本", item.a.Code)); } } } catch (Exception e) { Logger.ErrorDebugLine(e); } this.Properties.Add(new GpuSetProperty("NVMLVersion", "NVML版本", nvmlVersion)); Dictionary <string, string> kvs = new Dictionary <string, string> { { "CUDA_DEVICE_ORDER", "PCI_BUS_ID" } }; foreach (var kv in kvs) { var property = new GpuSetProperty(kv.Key, kv.Key, kv.Value); this.Properties.Add(property); } Task.Factory.StartNew(() => { foreach (var gpu in _gpus.Values) { NVIDIAOverClock.RefreshGpuState(gpu); } // 这里会耗时5秒 foreach (var kv in kvs) { Environment.SetEnvironmentVariable(kv.Key, kv.Value); } }); } } }