Search code examples
c#.netaleagpu

Detect if GPU Available on AleaGpu


Is it possible to detect if a GPU is present before running GPU code with AleaGPU?

Will Gpu.Default be null? Is there a property on this object that can be checked?

I basically want to run a faster version of an algorithm if the GPU is present, otherwise I want to run the original slow version.


Solution

  • I have disabled my GPU then tried this line:

    Console.WriteLine(Alea.Device.Devices.Count().ToString());
    

    And i got this exception System.Exception was unhandled Message=[CUDAError] CUDA_ERROR_UNKNOWN, and you get same exception if you tried to access Gpu.Default. So you can do something like this:

    bool GpuEnabled = false;
    try
    {
        GpuEnabled = Alea.Device.Devices.Count() > 0;
    }
    catch
    {
        GpuEnabled = false;
    }
    Console.WriteLine(GpuEnabled.ToString());
    

    Note: i'm not sure if Alea thrown an exception because i have a GPU but it was disabled, or it will behave the same way with a machine that doesn't have a GPU at all, anyway, this code should handle both assumptions safely.