Files
OpenNest/OpenNest.Gpu/GpuEvaluatorFactory.cs
AJ Isaacs bc392b37dc refactor: move GpuEvaluatorFactory to OpenNest.Gpu project
GPU factory logic belongs with the GPU implementation, not the UI.
Changed from internal to public and updated namespace to OpenNest.Gpu.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-07 21:40:56 -05:00

77 lines
2.0 KiB
C#

using System;
using System.Diagnostics;
using ILGPU;
using ILGPU.Runtime;
using OpenNest.Engine.BestFit;
namespace OpenNest.Gpu
{
public static class GpuEvaluatorFactory
{
private static bool _probed;
private static bool _gpuAvailable;
private static string _deviceName;
public static bool GpuAvailable
{
get
{
if (!_probed) Probe();
return _gpuAvailable;
}
}
public static string DeviceName
{
get
{
if (!_probed) Probe();
return _deviceName ?? "None";
}
}
public static IPairEvaluator Create(Drawing drawing, double spacing)
{
if (!GpuAvailable)
return null;
try
{
return new GpuPairEvaluator(drawing, spacing);
}
catch (Exception ex)
{
Debug.WriteLine($"[GpuEvaluatorFactory] GPU evaluator failed: {ex.Message}");
return null;
}
}
private static void Probe()
{
_probed = true;
try
{
using var context = Context.CreateDefault();
foreach (var device in context.Devices)
{
if (device.AcceleratorType == AcceleratorType.Cuda ||
device.AcceleratorType == AcceleratorType.OpenCL)
{
_gpuAvailable = true;
_deviceName = device.Name;
Debug.WriteLine($"[GpuEvaluatorFactory] GPU found: {device.Name} ({device.AcceleratorType})");
return;
}
}
Debug.WriteLine("[GpuEvaluatorFactory] No GPU device found");
}
catch (Exception ex)
{
Debug.WriteLine($"[GpuEvaluatorFactory] GPU probe failed: {ex.Message}");
}
}
}
}