[C#]C#最简单方法获取GPU显存真实大小

你是否用下面代码获取GPU显存容量?

using System.Management;        

private void getGpuMem()
{
    ManagementClass c = new ManagementClass("Win32_VideoController");
    foreach (ManagementObject o in c.GetInstances())
    {
        string gpuTotalMem = String.Format("{0} ", o["AdapterRam"]);
        Debug.Write(gpuTotalMem);
    }
}

上面代码往往使用获取的显存容量和实际不一致。因此探索最简单获取方式很有必要。一种方法是使用cudafy.NET这个库

GPGPU gpu = CudafyHost.GetDevice(CudafyModes.Target, CudafyModes.DeviceId);
var c = gpu.GetDeviceProperties(true);
var p = c.TotalMemory;
Console.WriteLine(p);

源码:GitHub - lepoco/CUDAfy.NET: CUDAfy .NET allows easy development of high performance GPGPU applications completely from the .NET. It's developed in C#.

还有一种方式是使用SharpDx,具体可以参考https://stackoverflow.com/questions/37601105/ 

下面是我自己最喜欢方法,不需要任何库,就是在cuda安装目录下面有个

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\extras\demo_suite\deviceQuery.exe,只用用cmd命令获取然后简单做下截取就行了 

你可能感兴趣的:(C#,c#,java,前端)