如果你需要在 C# 中检测是否安装了支持 CUDA 的 GPU,可以使用 ManagedCuda 库。下面是一个简单的方法来检查 CUDA 设备的存在。
1.安装 ManagedCuda 库
2.检测 CUDA GPU
using System;
using ManagedCuda;
public static bool HasCudaGPU()
{
try
{
// 获取 CUDA 设备数量
int deviceCount = CudaContext.GetDeviceCount();
if (deviceCount > 0)
{
Console.WriteLine($"CUDA GPU detected: {deviceCount} device(s)");
for (int i = 0; i < deviceCount; i++)
{
// 获取设备属性
var deviceProperties = CudaContext.GetDeviceInfo(i);
Console.WriteLine($"Device {i}: {deviceProperties.DeviceName}");
Console.WriteLine($" Compute Capability: {deviceProperties.ComputeCapability.Major}.{deviceProperties.ComputeCapability.Minor}");
Console.WriteLine($" Total Memory: {deviceProperties.TotalGlobalMemory / 1024 / 1024} MB");
}
return true; // 检测到 CUDA GPU
}
else
{
Console.WriteLine("No CUDA-enabled GPU detected.");
return false;
}
}
catch (CudaException ex)
{
// 捕获特定的 CUDA 异常并处理
Console.WriteLine("CUDA error: " + ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("Error detecting CUDA GPU: " + ex.Message);
}
return false;
}