From 16feeb05a1d7781f14d21feea488799b89472eab Mon Sep 17 00:00:00 2001 From: Serge <5920850+seerge@users.noreply.github.com> Date: Tue, 9 May 2023 18:21:07 +0200 Subject: [PATCH] Clocks fixes --- app/Gpu/NvidiaGpuControl.cs | 27 +++++++++++++++++---------- app/Program.cs | 2 +- app/Settings.cs | 9 +++++++-- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/app/Gpu/NvidiaGpuControl.cs b/app/Gpu/NvidiaGpuControl.cs index e2602ebb..383184de 100644 --- a/app/Gpu/NvidiaGpuControl.cs +++ b/app/Gpu/NvidiaGpuControl.cs @@ -3,6 +3,7 @@ using NvAPIWrapper.Native; using NvAPIWrapper.Native.GPU; using NvAPIWrapper.Native.GPU.Structures; using NvAPIWrapper.Native.Interfaces.GPU; +using System; using System.Diagnostics; using System.Management; using static NvAPIWrapper.Native.GPU.Structures.PerformanceStates20InfoV1; @@ -47,23 +48,29 @@ public class NvidiaGpuControl : IGpuControl } - public void GetClocks(out int core, out int memory, out string gpu) + public int GetClocks(out int core, out int memory, out string gpu) { PhysicalGPU internalGpu = _internalGpu!; gpu = internalGpu.FullName; - Logger.WriteLine(internalGpu.FullName); - Logger.WriteLine(internalGpu.ArchitectInformation.ToString()); + //Logger.WriteLine(internalGpu.FullName); + //Logger.WriteLine(internalGpu.ArchitectInformation.ToString()); - IPerformanceStates20Info states = GPUApi.GetPerformanceStates20(internalGpu.Handle); + try + { + IPerformanceStates20Info states = GPUApi.GetPerformanceStates20(internalGpu.Handle); + core = states.Clocks[PerformanceStateId.P0_3DPerformance][0].FrequencyDeltaInkHz.DeltaValue / 1000; + memory = states.Clocks[PerformanceStateId.P0_3DPerformance][1].FrequencyDeltaInkHz.DeltaValue / 1000; + Logger.WriteLine($"GET GPU Clock offsets : {core}, {memory}"); + return 0; - //Logger.WriteLine("IPerformanceStates20Info type : " + states.GetType()); - - core = states.Clocks[PerformanceStateId.P0_3DPerformance][0].FrequencyDeltaInkHz.DeltaValue / 1000; - memory = states.Clocks[PerformanceStateId.P0_3DPerformance][1].FrequencyDeltaInkHz.DeltaValue / 1000; - - Logger.WriteLine($"GET GPU Clock offsets : {core}, {memory}"); + } catch (Exception ex) + { + Logger.WriteLine(ex.Message); + core = memory = 0; + return -1; + } } diff --git a/app/Program.cs b/app/Program.cs index c672aacb..79cd4ace 100644 --- a/app/Program.cs +++ b/app/Program.cs @@ -368,7 +368,7 @@ namespace GHelper startInfo.Arguments = param; startInfo.Verb = "runas"; Process.Start(startInfo); - //Application.Exit(); + Application.Exit(); } } } diff --git a/app/Settings.cs b/app/Settings.cs index 9569be6e..f6f9488f 100644 --- a/app/Settings.cs +++ b/app/Settings.cs @@ -1061,8 +1061,13 @@ namespace GHelper using NvidiaGpuControl nvControl = (NvidiaGpuControl)HardwareControl.GpuControl; try { - int status = nvControl.SetClocks(gpu_core, gpu_memory); - if (launchAsAdmin && status == -1) Program.RunAsAdmin("gpu"); + int getStatus = nvControl.GetClocks(out int current_core, out int current_memory, out string gpuName); + if (getStatus == -1) return; + if (Math.Abs(gpu_core - current_core) < 5 && Math.Abs(gpu_memory - current_memory) < 5) return; + + int setStatus = nvControl.SetClocks(gpu_core, gpu_memory); + if (launchAsAdmin && setStatus == -1) Program.RunAsAdmin("gpu"); + } catch (Exception ex) {