Files
flowframes/Code/OS/NvApi.cs

97 lines
2.5 KiB
C#
Raw Normal View History

2020-11-23 16:51:05 +01:00
using NvAPIWrapper;
using NvAPIWrapper.GPU;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Flowframes.OS
{
class NvApi
{
public static List<PhysicalGPU> gpuList = new List<PhysicalGPU>();
2020-11-23 16:51:05 +01:00
2021-02-24 14:56:47 +01:00
public static void Init()
2020-11-23 16:51:05 +01:00
{
try
{
NVIDIA.Initialize();
PhysicalGPU[] gpus = PhysicalGPU.GetPhysicalGPUs();
2021-02-24 14:56:47 +01:00
2020-11-23 16:51:05 +01:00
if (gpus.Length == 0)
return;
2021-02-24 14:56:47 +01:00
gpuList = gpus.ToList();
List<string> gpuNames = new List<string>();
foreach (PhysicalGPU gpu in gpus)
gpuNames.Add(gpu.FullName);
string gpuNamesList = string.Join(", ", gpuNames);
Logger.Log($"Initialized Nvidia API. GPU{(gpus.Length > 1 ? "s" : "")}: {gpuNamesList}");
2020-11-23 16:51:05 +01:00
}
catch (Exception e)
{
2021-02-24 14:56:47 +01:00
Logger.Log("No Nvidia GPU(s) detected. You will not be able to use CUDA implementations.");
Logger.Log($"Failed to initialize NvApi: {e.Message}\nIgnore this if you don't have an Nvidia GPU.", true);
2020-11-23 16:51:05 +01:00
}
}
2021-02-24 14:56:47 +01:00
public static float GetVramGb (int gpu = 0)
2020-11-23 16:51:05 +01:00
{
try
{
2021-02-24 14:56:47 +01:00
return (gpuList[gpu].MemoryInformation.AvailableDedicatedVideoMemoryInkB / 1000f / 1024f);
2020-11-23 16:51:05 +01:00
}
2021-02-24 14:56:47 +01:00
catch
2020-11-23 16:51:05 +01:00
{
return 0f;
}
}
2021-02-24 14:56:47 +01:00
public static float GetFreeVramGb(int gpu = 0)
2020-11-23 16:51:05 +01:00
{
try
{
2021-02-24 14:56:47 +01:00
return (gpuList[gpu].MemoryInformation.CurrentAvailableDedicatedVideoMemoryInkB / 1000f / 1024f);
2020-11-23 16:51:05 +01:00
}
catch
2020-11-23 16:51:05 +01:00
{
return 0f;
}
}
public static string GetGpuName()
{
try
{
NVIDIA.Initialize();
PhysicalGPU[] gpus = PhysicalGPU.GetPhysicalGPUs();
if (gpus.Length == 0)
return "";
return gpus[0].FullName;
}
catch
{
return "";
}
}
2021-02-24 14:56:47 +01:00
public static bool HasTensorCores (int gpu = 0)
{
2021-02-24 14:56:47 +01:00
if (gpuList == null)
Init();
2021-02-24 14:56:47 +01:00
if (gpuList == null)
return false;
2021-02-24 14:56:47 +01:00
string gpuName = gpuList[gpu].FullName;
return (gpuName.Contains("RTX ") || gpuName.Contains("Tesla V") || gpuName.Contains("Tesla T"));
}
2020-11-23 16:51:05 +01:00
}
}