mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-04 07:19:24 -05:00
perf(thermal): keep a full sheet's heat map on the processor
With short steps only around the torch, the processor runs a 5x10 sheet's quarter-inch cells in 0.2-0.5 s where a graphics card takes 0.45-1.0 s, so grids under 250,000 cells now stay on the processor and the card serves only finer ones. The cut simulator starts the card on demand, so a run that stays on the processor no longer waits about half a second for it. Automatic cell sizes no longer depend on an accelerator: one budget of 4 billion cell updates keeps quarter-inch cells on a full sheet for cuts of about six hours in mild steel (about 3 s to compute), and every computer draws the same picture. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
ff8f97f44a
commit
151a92088f
9 files changed
+108
-54
No files matched your search
@@ -15,8 +15,8 @@ namespace OpenNest.CNC.Thermal
|
||||
|
||||
/// <summary>
|
||||
/// A grid of <paramref name="columns"/> by <paramref name="rows"/> cells heated by
|
||||
/// <paramref name="samples"/>, which are given in time order. Throws when the hardware
|
||||
/// cannot hold it.
|
||||
/// <paramref name="samples"/>, which are given in time order. Null when there is no
|
||||
/// hardware, which leaves the grid on the processor; throws when the hardware cannot hold it.
|
||||
/// </summary>
|
||||
IHeatKernel Create(int columns, int rows, ReadOnlySpan<HeatSample> samples);
|
||||
}
|
||||
|
||||
@@ -27,9 +27,10 @@ namespace OpenNest.CNC.Thermal
|
||||
/// <para>
|
||||
/// While the torch cuts, steps are short enough to follow it, but only the cells around the
|
||||
/// torch take them; the rest of the plate takes one step as long as diffusion allows.
|
||||
/// With <see cref="ThermalParameters.Accelerator"/> large grids run on it, every cell taking
|
||||
/// the short steps, so its picture is close to the CPU's (peaks within a few percent of the
|
||||
/// color scale) but not the same. Use one thread at a time, and dispose to release it.
|
||||
/// With <see cref="ThermalParameters.Accelerator"/> grids too fine for a full sheet's automatic
|
||||
/// cells run on it, every cell taking the short steps, so its picture is close to the CPU's
|
||||
/// (peaks within a few percent of the color scale) but not the same. Use one thread at a
|
||||
/// time, and dispose to release it.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public sealed class PlateHeatSimulation : IDisposable
|
||||
@@ -40,17 +41,18 @@ namespace OpenNest.CNC.Thermal
|
||||
private const int MaxSamplesPerStep = 10_000;
|
||||
|
||||
// Cell updates for a whole run, counting diffusion-limited steps, when the cell size is
|
||||
// automatic. The first halving may take about a second on the processor, so a full sheet
|
||||
// gets quarter-inch cells, and a couple of seconds on an accelerator, for longer cuts.
|
||||
// Finer cells have to be quick: on a full sheet their hot lines are too thin to see and
|
||||
// stretch the color scale. On an accelerator the torch travel limit can multiply the
|
||||
// steps a few times.
|
||||
private const double WorkBudget = 800_000_000;
|
||||
private const double AcceleratedWorkBudget = 20_000_000_000;
|
||||
// automatic. The first halving may take a few seconds for the longest cuts, so a full
|
||||
// sheet gets quarter-inch cells for cuts of several hours. Finer cells have to be quick:
|
||||
// on a full sheet their hot lines are too thin to see and stretch the color scale. The
|
||||
// budgets are the same with or without an accelerator, so every computer draws the same
|
||||
// picture.
|
||||
private const double WorkBudget = 4_000_000_000;
|
||||
private const double FinerWorkBudget = 60_000_000;
|
||||
|
||||
// Below this an accelerator spends longer starting each step than the step takes.
|
||||
private const int AcceleratedCells = 100_000;
|
||||
// Below this the processor is the faster: an accelerator spends longer starting each
|
||||
// short step than the processor takes over the cells around the torch. A full sheet's
|
||||
// quarter-inch cells (115,200) stay on the processor.
|
||||
private const int AcceleratedCells = 250_000;
|
||||
private const int MaxHalvings = 3;
|
||||
|
||||
// Explicit diffusion is stable below a quarter on a square grid; the cap keeps timing
|
||||
@@ -401,12 +403,11 @@ namespace OpenNest.CNC.Thermal
|
||||
internal static double AutomaticCellSize(Box bounds, double seconds, ThermalParameters parameters)
|
||||
{
|
||||
var coarsest = parameters.Units == Units.Millimeters ? 12 : 0.5;
|
||||
var firstBudget = parameters.Accelerator == null ? WorkBudget : AcceleratedWorkBudget;
|
||||
var cell = coarsest;
|
||||
for (var halvings = 0; halvings < MaxHalvings; halvings++)
|
||||
{
|
||||
var finer = cell / 2;
|
||||
var budget = halvings == 0 ? firstBudget : FinerWorkBudget;
|
||||
var budget = halvings == 0 ? WorkBudget : FinerWorkBudget;
|
||||
// Twelve millimeter halves to 3 mm, a little under an eighth inch.
|
||||
if (CellCount(bounds, finer) > MaxCells || RunWork(bounds, seconds, finer, parameters) > budget)
|
||||
break;
|
||||
|
||||
@@ -36,8 +36,7 @@ namespace OpenNest.CNC.Thermal
|
||||
/// <summary>
|
||||
/// Grid cell edge in <see cref="Units"/>. Zero picks one for the plate: 0.5 in (12 mm),
|
||||
/// halved down to 1/16 in (1.5 mm) while the whole run stays quick to replay, so small
|
||||
/// plates and short cuts get finer cells. An <see cref="Accelerator"/> keeps the first
|
||||
/// halving on longer cuts.
|
||||
/// plates and short cuts get finer cells. An <see cref="Accelerator"/> does not change it.
|
||||
/// </summary>
|
||||
public double CellSize { get; init; }
|
||||
|
||||
|
||||
@@ -24,13 +24,13 @@ public class HeatEstimateTests
|
||||
var program = new OpenNest.CNC.Program();
|
||||
program.Codes.Add(new RapidMove(0, 0));
|
||||
program.Codes.Add(new LinearMove(20, 0));
|
||||
var plate = new Plate(80, 80);
|
||||
var plate = new Plate(130, 130);
|
||||
plate.Parts.Add(new Part(new Drawing("part", program), new Vector(x, 5)));
|
||||
return (plate, CutSimulation.Build(plate,
|
||||
new CutParameters { Feedrate = 100, RapidTravelRate = 600, Units = Units.Inches }));
|
||||
}
|
||||
|
||||
// 0.25 in cells on 80 in square: large enough to be accelerated.
|
||||
// 0.25 in cells on 130 in square: large enough to be accelerated.
|
||||
private static ThermalParameters Parameters(IHeatAccelerator? accelerator) => new()
|
||||
{
|
||||
Thickness = 0.25,
|
||||
|
||||
@@ -33,8 +33,8 @@ public sealed class HeatEstimate : IDisposable
|
||||
public string Device { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Builds and runs the model, on <paramref name="accelerator"/> when there is one and on the
|
||||
/// processor (with the coarser cells that suit it) when that fails.
|
||||
/// Builds and runs the model, on <paramref name="accelerator"/> when there is one and the
|
||||
/// grid is large enough to gain from it, and on the processor when that fails.
|
||||
/// </summary>
|
||||
/// <param name="parameters">The model's inputs for the given accelerator, or none.</param>
|
||||
public static HeatEstimate Run(Plate plate, CutSimulation simulation,
|
||||
@@ -45,7 +45,7 @@ public sealed class HeatEstimate : IDisposable
|
||||
{
|
||||
try
|
||||
{
|
||||
return Run(plate, simulation, parameters(accelerator), accelerator.Name, cancellationToken);
|
||||
return Run(plate, simulation, parameters(accelerator), accelerator, cancellationToken);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
@@ -66,7 +66,7 @@ public sealed class HeatEstimate : IDisposable
|
||||
(s.Kind, s.Start, s.End, s.IsArc, s.Center, s.Sweep, s.StartTime, s.Duration, (s.Layer, s.IsHole, s.ContourIndex));
|
||||
|
||||
private static HeatEstimate Run(Plate plate, CutSimulation simulation, ThermalParameters parameters,
|
||||
string device, CancellationToken cancellationToken)
|
||||
IHeatAccelerator accelerator, CancellationToken cancellationToken)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var heat = new PlateHeatSimulation(plate, simulation, parameters);
|
||||
@@ -75,7 +75,8 @@ public sealed class HeatEstimate : IDisposable
|
||||
heat.RunToEnd(cancellationToken: cancellationToken);
|
||||
var scale = ColorScale(heat);
|
||||
heat.Reset();
|
||||
return new HeatEstimate(heat, scale, heat.IsAccelerated ? device : null);
|
||||
// Asked only of an accelerator that ran, so one started on demand stays unstarted.
|
||||
return new HeatEstimate(heat, scale, heat.IsAccelerated ? accelerator.Name : null);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
@@ -32,6 +32,12 @@ public sealed class GpuHeatAccelerator : IHeatAccelerator, IDisposable
|
||||
/// </summary>
|
||||
public static GpuHeatAccelerator Default => Shared.Value;
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="Default"/>, started by the first grid that asks for it, so a simulation whose
|
||||
/// grids all stay on the processor never waits for the card.
|
||||
/// </summary>
|
||||
public static IHeatAccelerator OnDemand { get; } = new Deferred();
|
||||
|
||||
public string Name => Accelerator.Name;
|
||||
|
||||
internal Accelerator Accelerator { get; }
|
||||
@@ -62,6 +68,14 @@ public sealed class GpuHeatAccelerator : IHeatAccelerator, IDisposable
|
||||
context.Dispose();
|
||||
}
|
||||
|
||||
private sealed class Deferred : IHeatAccelerator
|
||||
{
|
||||
public string Name => Default?.Name;
|
||||
|
||||
public IHeatKernel Create(int columns, int rows, ReadOnlySpan<HeatSample> samples) =>
|
||||
Default?.Create(columns, rows, samples);
|
||||
}
|
||||
|
||||
private static GpuHeatAccelerator Create()
|
||||
{
|
||||
Context context = null;
|
||||
|
||||
@@ -22,7 +22,7 @@ public class GpuHeatAcceleratorTests
|
||||
program.Codes.Add(new LinearMove(x, y + size));
|
||||
program.Codes.Add(new LinearMove(x, y));
|
||||
}
|
||||
var plate = new Plate(80, 80);
|
||||
var plate = new Plate(130, 130);
|
||||
plate.Parts.Add(new Part(new Drawing("part", program), new Vector(8, 8)));
|
||||
var simulation = CutSimulation.Build(plate, new CutParameters
|
||||
{
|
||||
@@ -31,7 +31,7 @@ public class GpuHeatAcceleratorTests
|
||||
PierceTime = TimeSpan.FromSeconds(1),
|
||||
Units = Units.Inches,
|
||||
});
|
||||
// 0.25 in cells on 80 in square are 102,400 cells, just enough to be accelerated.
|
||||
// 0.25 in cells on 130 in square are 270,400 cells, enough to be accelerated.
|
||||
return (plate, simulation, accelerator => new ThermalParameters
|
||||
{
|
||||
Material = ThermalMaterial.MildSteel,
|
||||
@@ -96,11 +96,12 @@ public class GpuHeatAcceleratorTests
|
||||
AssertMatchesTheProcessor(accelerator!, new SupportSlats(SlatAxis.X, pitch: 4, thickness: 1, offset: 2));
|
||||
}
|
||||
|
||||
// Every computer draws the same picture, so a card must not change the cells.
|
||||
[Theory]
|
||||
[InlineData(3600, 0.25, 0.25)]
|
||||
[InlineData(40_000, 0.5, 0.25)]
|
||||
public void AcceleratorKeepsQuarterInchCellsForLongerCutsOnAFullSheet(
|
||||
double seconds, double processor, double accelerated)
|
||||
[InlineData(3600, 0.25)]
|
||||
[InlineData(20_000, 0.25)]
|
||||
[InlineData(80_000, 0.5)]
|
||||
public void AnAcceleratorDoesNotChangeTheAutomaticCellSize(double seconds, double expected)
|
||||
{
|
||||
using var accelerator = GpuHeatAccelerator.Emulated();
|
||||
ThermalParameters Parameters(IHeatAccelerator? hardware) => new()
|
||||
@@ -112,24 +113,57 @@ public class GpuHeatAcceleratorTests
|
||||
};
|
||||
var sheet = new Box(0, 0, 120, 60);
|
||||
|
||||
Assert.Equal(processor, PlateHeatSimulation.AutomaticCellSize(sheet, seconds, Parameters(null)));
|
||||
Assert.Equal(accelerated, PlateHeatSimulation.AutomaticCellSize(sheet, seconds, Parameters(accelerator)));
|
||||
Assert.Equal(expected, PlateHeatSimulation.AutomaticCellSize(sheet, seconds, Parameters(null)));
|
||||
Assert.Equal(expected, PlateHeatSimulation.AutomaticCellSize(sheet, seconds, Parameters(accelerator)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SmallGridsStayOnTheProcessor()
|
||||
private sealed class CountingAccelerator : IHeatAccelerator
|
||||
{
|
||||
using var accelerator = GpuHeatAccelerator.Emulated();
|
||||
var (plate, simulation, _) = Job(null);
|
||||
public int Asked { get; private set; }
|
||||
public string Name => "counting";
|
||||
|
||||
public IHeatKernel Create(int columns, int rows, ReadOnlySpan<HeatSample> samples)
|
||||
{
|
||||
Asked++;
|
||||
return null!;
|
||||
}
|
||||
}
|
||||
|
||||
// The processor is the faster for a full sheet's quarter-inch cells, so the card is not
|
||||
// even asked: asking is what starts it.
|
||||
[Fact]
|
||||
public void AFullSheetsQuarterInchCellsStayOnTheProcessor()
|
||||
{
|
||||
var accelerator = new CountingAccelerator();
|
||||
var plate = new Plate(60, 120);
|
||||
plate.Parts.Add(new Part(Job(null).Plate.Parts[0].BaseDrawing, new Vector(8, 8)));
|
||||
var simulation = CutSimulation.Build(plate, new CutParameters { Feedrate = 100, Units = Units.Inches });
|
||||
using var heat = new PlateHeatSimulation(plate, simulation, new ThermalParameters
|
||||
{
|
||||
Thickness = 0.25,
|
||||
Units = Units.Inches,
|
||||
CellSize = 0.5,
|
||||
CellSize = 0.25,
|
||||
Accelerator = accelerator,
|
||||
});
|
||||
|
||||
Assert.False(heat.IsAccelerated);
|
||||
Assert.Equal(0, accelerator.Asked);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AnAcceleratorWithNoHardwareLeavesTheGridOnTheProcessor()
|
||||
{
|
||||
var accelerator = new CountingAccelerator();
|
||||
var (plate, simulation, thermal) = Job(null);
|
||||
using var heat = new PlateHeatSimulation(plate, simulation, thermal(accelerator));
|
||||
var processor = new PlateHeatSimulation(plate, simulation, thermal(null));
|
||||
|
||||
heat.RunToEnd();
|
||||
processor.RunToEnd();
|
||||
|
||||
Assert.Equal(1, accelerator.Asked);
|
||||
Assert.False(heat.IsAccelerated);
|
||||
Assert.Equal(processor.Temperatures.ToArray(), heat.Temperatures.ToArray());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -417,7 +417,7 @@ namespace OpenNest.Forms
|
||||
HeatEstimate estimate;
|
||||
try
|
||||
{
|
||||
// Finding the graphics card takes a moment the first time, so that waits here too.
|
||||
// The graphics card starts inside the run, and only for a grid fine enough to use it.
|
||||
estimate = await Task.Run(() => HeatEstimate.Run(subject, simulation,
|
||||
accelerator => new ThermalParameters
|
||||
{
|
||||
@@ -428,7 +428,7 @@ namespace OpenNest.Forms
|
||||
SupportBed = input.SupportBed,
|
||||
Accelerator = accelerator,
|
||||
},
|
||||
accelerate ? GpuHeatAccelerator.Default : null, cancellationToken), cancellationToken);
|
||||
accelerate ? GpuHeatAccelerator.OnDemand : null, cancellationToken), cancellationToken);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
|
||||
+19
-14
@@ -9,16 +9,16 @@ a removed heat-aware sequencer used a cheaper estimate of the same physics ([Seq
|
||||
|
||||
- A grid over the plate's bounds. Unless `CellSize` is set, cells start at 0.5 in (12 mm) and
|
||||
halve, down to 1/16 in (1.5 mm), while the whole run (cells times diffusion-limited time steps)
|
||||
stays within a work budget. The first halving has 800 million cell updates on the processor,
|
||||
about a second, which gives a 5×10 ft sheet 0.25 in cells, and 20 billion with an
|
||||
[accelerator](#graphics-card), which keeps 0.25 in on longer cuts. Finer halvings have 60
|
||||
million with either, so only small plates and short cuts get them: on a full sheet 0.125 in
|
||||
cells leave hot lines too thin to see and stretch the color scale. Halving costs up to sixteen
|
||||
times the work, since diffusion shortens the time step with the cell's area. Automatic grids
|
||||
have at most 700,000 cells, and any size asked for is made coarser past that. Each cell holds
|
||||
its temperature rise above ambient. Finer cells hold the torch's heat in less metal, so cells
|
||||
along the cut read hotter; compare cut orders at one cell size (a long cut can get a finer one
|
||||
with a card than without).
|
||||
stays within a work budget. The first halving has 4 billion cell updates, a few seconds at
|
||||
most, which gives a 5×10 ft sheet 0.25 in cells for cuts of up to about six hours in mild
|
||||
steel. Finer halvings have 60 million, so only small plates and short cuts get them: on a
|
||||
full sheet 0.125 in cells leave hot lines too thin to see and stretch the color scale. Halving
|
||||
costs up to sixteen times the work, since diffusion shortens the time step with the cell's
|
||||
area. Automatic grids have at most 700,000 cells, and any size asked for is made coarser past
|
||||
that. Each cell holds its temperature rise above ambient. Finer cells hold the torch's heat in
|
||||
less metal, so cells along the cut read hotter; compare cut orders at one cell size. An
|
||||
[accelerator](#graphics-card) does not change the cell size, so every computer draws the same
|
||||
picture.
|
||||
- Heat in: while the torch is on (cuts and pierces), `AbsorbedPower` watts go into the sheet at
|
||||
the torch, in proportion to the time spent there. The path is sampled four times per cell, and
|
||||
each sample is shared between the four cells whose centers surround it, by closeness, among
|
||||
@@ -97,12 +97,17 @@ with the same moves and settings keeps the model it has.
|
||||
|
||||
## Graphics card
|
||||
|
||||
`ThermalParameters.Accelerator` runs grids of 100,000 cells or more elsewhere; smaller ones stay
|
||||
on the processor, as a card takes longer to start each step than such a step takes. `OpenNest.Gpu`
|
||||
`ThermalParameters.Accelerator` runs grids of 250,000 cells or more elsewhere; smaller ones stay
|
||||
on the processor, which is the faster for them: a card takes longer to start each short step than
|
||||
the processor takes over the cells around the torch. A 5×10 ft sheet's 0.25 in cells (115,200)
|
||||
therefore run on the processor on every computer, and the card serves only finer grids, which
|
||||
automatic cell sizes seldom reach (a `CellSize` of 0.125 in on a full sheet does). The cut
|
||||
simulator passes `GpuHeatAccelerator.OnDemand`, which starts the card (about half a second, once)
|
||||
only when a grid first asks for it. `OpenNest.Gpu`
|
||||
is the one implementation: ILGPU, using CUDA on an NVIDIA card, otherwise OpenCL where ILGPU
|
||||
accepts the driver (not verified on AMD or Intel cards; Apple graphics are not supported). Without
|
||||
a usable card, or when it fails while building or playing (out of memory, driver fault), the model
|
||||
runs on the processor with the cell size that suits it.
|
||||
runs on the processor.
|
||||
|
||||
The simulation keeps the pieces, timeline and checkpoints and the card keeps the temperatures.
|
||||
On the card every cell takes the short steps, so its picture differs from the processor's by what
|
||||
@@ -116,7 +121,7 @@ change to `DiffuseCells` or `Add` needs the same change in `OpenNest.Gpu/HeatKer
|
||||
Measured on a Ryzen 7 3700X and an RTX 4060, 5×10 ft nests, 0.25 in and 16 gauge mild steel at
|
||||
100 to 1600 in/min, whole run: 0.25 in cells took 0.9-2.9 s on the processor before the window
|
||||
and take 0.23-0.45 s with it; the card takes 0.45-1.0 s, most of it the fixed cost of starting
|
||||
two programs per step, so at this cell size the processor is now the faster of the two. 0.125 in
|
||||
two programs per step, which is why this cell size stays on the processor. 0.125 in
|
||||
cells take 1.3-6.2 s on the processor (11-22 s before) and 1.1-2.7 s on the card.
|
||||
|
||||
## Display
|
||||
|
||||
Reference in new issue
Block a user