From cdb5c96b172bd5b1b08c0dfa9eb9b660e90dbcfc Mon Sep 17 00:00:00 2001 From: Maz Date: Wed, 17 Jul 2024 00:26:01 +1000 Subject: [PATCH 1/2] Issue #71 Adding SetForcePstate for TCC cards --- NvAPIWrapper/Native/Delegates/GPU.cs | 6 +++++ NvAPIWrapper/Native/GPUApi.Performance.cs | 29 +++++++++++++++++++++++ NvAPIWrapper/Native/Helpers/FunctionId.cs | 1 + NvAPIWrapper/NvAPIWrapper.csproj | 2 +- 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/NvAPIWrapper/Native/Delegates/GPU.cs b/NvAPIWrapper/Native/Delegates/GPU.cs index 5ff8659..1a5e5d9 100644 --- a/NvAPIWrapper/Native/Delegates/GPU.cs +++ b/NvAPIWrapper/Native/Delegates/GPU.cs @@ -683,6 +683,12 @@ public delegate Status NvAPI_GPU_SetPStates20( [In] ValueTypeReference performanceStatesInfo); + [FunctionId(FunctionId.NvAPI_GPU_SetForcePstate)] + public delegate Status NvAPI_GPU_SetForcePstate( + [In] PhysicalGPUHandle physicalGpu, + [In] int pstate, + [In] int int3); + [FunctionId(FunctionId.NvAPI_GPU_SetThermalPoliciesStatus)] public delegate Status NvAPI_GPU_SetThermalPoliciesStatus( [In] PhysicalGPUHandle physicalGpu, diff --git a/NvAPIWrapper/Native/GPUApi.Performance.cs b/NvAPIWrapper/Native/GPUApi.Performance.cs index 06ce3ee..543452b 100644 --- a/NvAPIWrapper/Native/GPUApi.Performance.cs +++ b/NvAPIWrapper/Native/GPUApi.Performance.cs @@ -523,5 +523,34 @@ public static void SetPerformanceStates20( } } } + + /// + /// [PRIVATE] + /// This function exists to force the P-State when SetPerformanceStates20 cannot. + /// Works with Tesla P40 and T4, likely other TCC cards. + /// + /// GPU handle to get information about. + /// The pstate you want to set. 0 is highest performance, 8 is low power, 16 is default + /// Not sure. I'm setting it to 2 for now and it seems to work. + /// Status.InvalidArgument: gpuHandle is NULL + /// Status.ExpectedPhysicalGPUHandle: gpuHandle was not a physical GPU handle + public static void SetForcePstate( + PhysicalGPUHandle physicalGPUHandle, + int pstate, + int int3) + { + var status = DelegateFactory.GetDelegate()( + physicalGPUHandle, + pstate, + int3 + ); + + if (status != Status.Ok) + { + throw new NVIDIAApiException(status); + } + } + + } } \ No newline at end of file diff --git a/NvAPIWrapper/Native/Helpers/FunctionId.cs b/NvAPIWrapper/Native/Helpers/FunctionId.cs index 446f8b4..23c3d31 100644 --- a/NvAPIWrapper/Native/Helpers/FunctionId.cs +++ b/NvAPIWrapper/Native/Helpers/FunctionId.cs @@ -622,6 +622,7 @@ internal enum FunctionId : uint NvAPI_GPU_SetPstates20 = 0x0F4DAE6B, NvAPI_GPU_SetPstatesInfo = 0x0CDF27911, NvAPI_GPU_SetThermalPoliciesStatus = 0x034C0B13D, + NvAPI_GPU_SetForcePstate = 0x025BFB10, NvAPI_Hybrid_IsAppMigrationStateChangeable = 0x584CB0B6, NvAPI_Hybrid_QueryBlockedMigratableApps = 0x0F4C2F8CC, NvAPI_Hybrid_QueryUnblockedNonMigratableApps = 0x5F35BCB5, diff --git a/NvAPIWrapper/NvAPIWrapper.csproj b/NvAPIWrapper/NvAPIWrapper.csproj index 2a854c2..c555add 100644 --- a/NvAPIWrapper/NvAPIWrapper.csproj +++ b/NvAPIWrapper/NvAPIWrapper.csproj @@ -2,7 +2,7 @@ netstandard2.0;net45 - 0.8.1.101 + 0.8.1.103 falahati.net NvAPIWrapper is a .Net wrapper for NVIDIA public API Soroush Falahati From 0edf7b04c2e4196a1a9712c7a18e72557ffcf6d7 Mon Sep 17 00:00:00 2001 From: Maz Date: Wed, 17 Jul 2024 21:20:37 +1000 Subject: [PATCH 2/2] Updating comment for SetForcePstate after talking to someone at nvidia about what the parameters mean. --- NvAPIWrapper/Native/Delegates/GPU.cs | 2 +- NvAPIWrapper/Native/GPUApi.Performance.cs | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/NvAPIWrapper/Native/Delegates/GPU.cs b/NvAPIWrapper/Native/Delegates/GPU.cs index 1a5e5d9..4c5690b 100644 --- a/NvAPIWrapper/Native/Delegates/GPU.cs +++ b/NvAPIWrapper/Native/Delegates/GPU.cs @@ -687,7 +687,7 @@ public delegate Status NvAPI_GPU_SetPStates20( public delegate Status NvAPI_GPU_SetForcePstate( [In] PhysicalGPUHandle physicalGpu, [In] int pstate, - [In] int int3); + [In] int fallback); [FunctionId(FunctionId.NvAPI_GPU_SetThermalPoliciesStatus)] public delegate Status NvAPI_GPU_SetThermalPoliciesStatus( diff --git a/NvAPIWrapper/Native/GPUApi.Performance.cs b/NvAPIWrapper/Native/GPUApi.Performance.cs index 543452b..9853daa 100644 --- a/NvAPIWrapper/Native/GPUApi.Performance.cs +++ b/NvAPIWrapper/Native/GPUApi.Performance.cs @@ -526,23 +526,25 @@ public static void SetPerformanceStates20( /// /// [PRIVATE] - /// This function exists to force the P-State when SetPerformanceStates20 cannot. + /// This function exists to force the P-State of a GPU when SetPerformanceStates20 cannot. /// Works with Tesla P40 and T4, likely other TCC cards. + /// In the fallback state, the driver automatically switches between high and low performance states + /// as needed by the running software. /// /// GPU handle to get information about. - /// The pstate you want to set. 0 is highest performance, 8 is low power, 16 is default - /// Not sure. I'm setting it to 2 for now and it seems to work. + /// The pstate you want to force 0 - 15. 0 = highest performance. 16 = use fallback state. + /// 1 = fallback to high perf, 2 = fallback to low perf. /// Status.InvalidArgument: gpuHandle is NULL /// Status.ExpectedPhysicalGPUHandle: gpuHandle was not a physical GPU handle public static void SetForcePstate( PhysicalGPUHandle physicalGPUHandle, int pstate, - int int3) + int fallback) { var status = DelegateFactory.GetDelegate()( physicalGPUHandle, pstate, - int3 + fallback ); if (status != Status.Ok)