mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-06 05:19:25 -05:00
Split only cutting time into short steps, raise the automatic resolution, add an optional ILGPU accelerator with CPU fallback, and estimate heat off the UI thread. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
184 lines
6.0 KiB
C#
184 lines
6.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using ILGPU;
|
|
using ILGPU.Runtime;
|
|
using OpenNest.CNC.Thermal;
|
|
|
|
namespace OpenNest.Gpu;
|
|
|
|
/// <summary>One simulation's grid in the card's memory.</summary>
|
|
internal sealed class GpuHeatKernel : IHeatKernel
|
|
{
|
|
// The driver queues steps; wait now and then so the queue stays short.
|
|
private const int StepsPerWait = 256;
|
|
|
|
private readonly GpuHeatAccelerator owner;
|
|
private readonly int columns;
|
|
private readonly int rows;
|
|
private readonly List<MemoryBuffer> buffers = new();
|
|
private readonly Dictionary<int, MemoryBuffer1D<float, Stride1D.Dense>> keeps = new();
|
|
private readonly MemoryBuffer1D<float, Stride1D.Dense> peak;
|
|
private readonly MemoryBuffer1D<float, Stride1D.Dense> noKeep;
|
|
private readonly MemoryBuffer1D<int, Stride1D.Dense> region;
|
|
private readonly MemoryBuffer1D<int, Stride1D.Dense> links;
|
|
private readonly MemoryBuffer1D<HeatSample, Stride1D.Dense> samples;
|
|
private MemoryBuffer1D<float, Stride1D.Dense> current;
|
|
private MemoryBuffer1D<float, Stride1D.Dense> next;
|
|
private MemoryBuffer1D<float, Stride1D.Dense> partialKeep;
|
|
// Cell numbers, regions and links of a change, one after another.
|
|
private MemoryBuffer1D<int, Stride1D.Dense> changes;
|
|
private int steps;
|
|
|
|
public GpuHeatKernel(GpuHeatAccelerator owner, int columns, int rows, ReadOnlySpan<HeatSample> source)
|
|
{
|
|
this.owner = owner;
|
|
this.columns = columns;
|
|
this.rows = rows;
|
|
var count = columns * rows;
|
|
try
|
|
{
|
|
current = Allocate<float>(count);
|
|
next = Allocate<float>(count);
|
|
peak = Allocate<float>(count);
|
|
noKeep = Allocate<float>(1);
|
|
region = Allocate<int>(count);
|
|
links = Allocate<int>(count);
|
|
samples = Allocate<HeatSample>(System.Math.Max(1, source.Length));
|
|
if (source.Length > 0)
|
|
samples.View.SubView(0, source.Length).CopyFromCPU(Stream, source.ToArray());
|
|
}
|
|
catch
|
|
{
|
|
Release();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
private AcceleratorStream Stream => owner.Accelerator.DefaultStream;
|
|
|
|
public void Clear() => Locked(() =>
|
|
{
|
|
current.MemSetToZero(Stream);
|
|
next.MemSetToZero(Stream);
|
|
peak.MemSetToZero(Stream);
|
|
});
|
|
|
|
public void SetCells(ReadOnlySpan<int> newRegion, ReadOnlySpan<int> newLinks)
|
|
{
|
|
var regions = newRegion.ToArray();
|
|
var linked = newLinks.ToArray();
|
|
Locked(() =>
|
|
{
|
|
region.View.CopyFromCPU(Stream, regions);
|
|
links.View.CopyFromCPU(Stream, linked);
|
|
Stream.Synchronize();
|
|
});
|
|
}
|
|
|
|
public void ChangeCells(ReadOnlySpan<int> cells, ReadOnlySpan<int> newRegion, ReadOnlySpan<int> newLinks)
|
|
{
|
|
var count = cells.Length;
|
|
if (count == 0)
|
|
return;
|
|
var packed = new int[count * 3];
|
|
cells.CopyTo(packed);
|
|
newRegion.CopyTo(packed.AsSpan(count));
|
|
newLinks.CopyTo(packed.AsSpan(count * 2));
|
|
Locked(() =>
|
|
{
|
|
if (changes == null || changes.Length < packed.Length)
|
|
{
|
|
Free(changes);
|
|
changes = Allocate<int>(System.Math.Max(packed.Length, 4096));
|
|
}
|
|
changes.View.SubView(0, packed.Length).CopyFromCPU(Stream, packed);
|
|
owner.Kernels.Change(Stream, count, current.View, region.View, links.View,
|
|
changes.View.SubView(0, count), changes.View.SubView(count, count),
|
|
changes.View.SubView(count * 2, count));
|
|
Stream.Synchronize();
|
|
});
|
|
}
|
|
|
|
public void AddSamples(int from, int to) => Locked(() =>
|
|
owner.Kernels.Add(Stream, 1, current.View, region.View, samples.View,
|
|
new SampleRange(from, to, columns, rows)));
|
|
|
|
public void Step(float share, float keep, float[] cellKeep, int keepKey) => Locked(() =>
|
|
{
|
|
var perCell = cellKeep == null ? null : Keep(cellKeep, keepKey);
|
|
var views = new StepViews(current.View, next.View, peak.View, (perCell ?? noKeep).View, links.View);
|
|
owner.Kernels.Diffuse(Stream, columns * rows, views, columns, share, keep, perCell == null ? 0 : 1);
|
|
(current, next) = (next, current);
|
|
if (++steps % StepsPerWait == 0)
|
|
Stream.Synchronize();
|
|
});
|
|
|
|
public void Read(float[] temperature, float[] peaks) => Locked(() =>
|
|
{
|
|
current.View.CopyToCPU(Stream, temperature);
|
|
peak.View.CopyToCPU(Stream, peaks);
|
|
Stream.Synchronize();
|
|
});
|
|
|
|
public void Write(float[] temperature, float[] peaks) => Locked(() =>
|
|
{
|
|
current.View.CopyFromCPU(Stream, temperature);
|
|
peak.View.CopyFromCPU(Stream, peaks);
|
|
Stream.Synchronize();
|
|
});
|
|
|
|
public void Dispose() => Locked(Release);
|
|
|
|
private MemoryBuffer1D<float, Stride1D.Dense> Keep(float[] values, int key)
|
|
{
|
|
if (key < 0)
|
|
{
|
|
partialKeep ??= Allocate<float>(values.Length);
|
|
partialKeep.View.CopyFromCPU(Stream, values);
|
|
Stream.Synchronize();
|
|
return partialKeep;
|
|
}
|
|
|
|
if (!keeps.TryGetValue(key, out var buffer))
|
|
{
|
|
buffer = Allocate<float>(values.Length);
|
|
buffer.View.CopyFromCPU(Stream, values);
|
|
Stream.Synchronize();
|
|
keeps[key] = buffer;
|
|
}
|
|
return buffer;
|
|
}
|
|
|
|
private MemoryBuffer1D<T, Stride1D.Dense> Allocate<T>(long length)
|
|
where T : unmanaged
|
|
{
|
|
var buffer = owner.Accelerator.Allocate1D<T>(length);
|
|
buffers.Add(buffer);
|
|
return buffer;
|
|
}
|
|
|
|
private void Free(MemoryBuffer buffer)
|
|
{
|
|
if (buffer == null)
|
|
return;
|
|
buffers.Remove(buffer);
|
|
buffer.Dispose();
|
|
}
|
|
|
|
private void Release()
|
|
{
|
|
foreach (var buffer in buffers)
|
|
buffer.Dispose();
|
|
buffers.Clear();
|
|
}
|
|
|
|
private void Locked(Action action)
|
|
{
|
|
lock (owner.Gate)
|
|
{
|
|
owner.Accelerator.Bind();
|
|
action();
|
|
}
|
|
}
|
|
}
|