From e76864b48987d29dba966de0b9ee984507f2d968 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Fri, 30 Oct 2026 09:14:05 -0400 Subject: [PATCH] fix(engine): nest alike on every machine by fixing PairFiller's batches FillItem_WithClampZones has flipped between two digests because each was correct for the machine that pinned it. PairFiller sized its parallel batches from Environment.ProcessorCount, and a batch shares the best count and work area from before it, so core count decides which candidates are tried. With 10+ logical processors candidate 9 runs before the area shrinks and its 46-part layout wins for every comparer (9BF5FC7FC47364A9); with 8 or fewer the area has shrunk, it can only reach 45, and Fill/Strip and Vertical Remnant settle on different 45-part ties. 40 of the parts are kept either way. So the same nest differed between shop PCs, and CI failed whenever the digests were pinned on a 12-thread desktop. Fix the batch at 8, which keeps the pins CI has always produced. Also claim GridDedup keys in candidate order before the parallel loop: the claim was a race inside a batch, seen as one candidate returning 42 parts and then 33 in the same process. Co-Authored-By: Claude Opus 5.5 --- .../Jobs/FillStackCharacterizationTests.cs | 4 +- OpenNest.Engine/Fill/PairFiller.cs | 81 +++++++++++++------ 2 files changed, 61 insertions(+), 24 deletions(-) diff --git a/OpenNest.Engine.Tests/Jobs/FillStackCharacterizationTests.cs b/OpenNest.Engine.Tests/Jobs/FillStackCharacterizationTests.cs index 803bb64c..8ca79e85 100644 --- a/OpenNest.Engine.Tests/Jobs/FillStackCharacterizationTests.cs +++ b/OpenNest.Engine.Tests/Jobs/FillStackCharacterizationTests.cs @@ -22,7 +22,9 @@ namespace OpenNest.Engine.Tests.Jobs; /// /// Captured on Windows/.NET 8 without the optional ML angle model, so irregular parts take the /// 5-degree sweep; each case gave one layout over 5 test processes of 2 solves each, and every -/// case solves twice in-process and must agree. The whole-plate Fill nests fill several +/// case solves twice in-process and must agree. A digest that differs between machines means +/// a layout decision depends on the machine (as PairFiller's batches once followed the +/// processor count); fix that rather than repinning. The whole-plate Fill nests fill several /// irregular drawings with one filler, so later items use the angles /// AngleCandidateBuilder.RecordProductive learned from earlier ones. /// Not pinned: richer mixes that pair parallelogram, chevron or tee parts at oblique angles diff --git a/OpenNest.Engine/Fill/PairFiller.cs b/OpenNest.Engine/Fill/PairFiller.cs index 206d29dc..78d7e6bd 100644 --- a/OpenNest.Engine/Fill/PairFiller.cs +++ b/OpenNest.Engine/Fill/PairFiller.cs @@ -43,6 +43,13 @@ namespace OpenNest.Engine.Fill /// private const int EarlyExitStaleLimit = 10; + /// + /// Candidates evaluated together, against the best count and work area from before the + /// batch. Fixed rather than scaled to the processor count, because it decides which + /// candidates are tried and so which layout wins: every machine must nest alike. + /// + private const int CandidateBatchSize = 8; + /// /// Defines the plate /// @@ -174,7 +181,6 @@ namespace OpenNest.Engine.Fill List best = null; var sinceImproved = 0; var effectiveWorkArea = workArea; - var batchSize = System.Math.Max(2, Environment.ProcessorCount); var maxUtilization = candidates.Count > 0 ? candidates.Max(c => c.Utilization) : 1.0; var partBox = drawing.Program.BoundingBox(); @@ -182,11 +188,11 @@ namespace OpenNest.Engine.Fill try { - for (var batchStart = 0; batchStart < candidates.Count; batchStart += batchSize) + for (var batchStart = 0; batchStart < candidates.Count; batchStart += CandidateBatchSize) { token.ThrowIfCancellationRequested(); - var batchEnd = System.Math.Min(batchStart + batchSize, candidates.Count); + var batchEnd = System.Math.Min(batchStart + CandidateBatchSize, candidates.Count); var results = EvaluateBatch( candidates, drawing, @@ -242,6 +248,16 @@ namespace OpenNest.Engine.Fill ) { var batchCount = batchEnd - batchStart; + + // Claimed in candidate order before the parallel loop: when two candidates share a + // grid, the earlier one must get it however the threads are scheduled. + var claims = new List<(Pattern Pattern, NestDirection Dir)>[batchCount]; + for (var j = 0; j < batchCount; j++) + { + token.ThrowIfCancellationRequested(); + claims[j] = ClaimGrids(candidates[batchStart + j], drawing, workArea); + } + var results = new List[batchCount]; Parallel.For( 0, @@ -250,7 +266,7 @@ namespace OpenNest.Engine.Fill j => { results[j] = EvaluateCandidate( - candidates[batchStart + j], + claims[j], drawing, workArea, minCountToBeat, @@ -355,18 +371,49 @@ namespace OpenNest.Engine.Fill } /// - /// The EvaluateCandidate + /// Builds the candidate's tiling patterns and claims the grids no earlier candidate took. /// /// The candidate /// The drawing /// The workArea + /// The patterns and directions left for this candidate to fill + private List<(Pattern Pattern, NestDirection Dir)> ClaimGrids( + BestFitResult candidate, + Drawing drawing, + Box workArea + ) + { + var claimed = new List<(Pattern Pattern, NestDirection Dir)>(); + var pairParts = candidate.BuildParts(drawing); + foreach (var angle in BuildTilingAngles(candidate)) + { + var pattern = FillHelpers.BuildRotatedPattern(pairParts, angle); + if (pattern.Parts.Count == 0) + continue; + + foreach (var dir in new[] { NestDirection.Horizontal, NestDirection.Vertical }) + { + if (dedup.TryAdd(pattern.BoundingBox, workArea, dir)) + claimed.Add((pattern, dir)); + } + } + + return claimed; + } + + /// + /// The EvaluateCandidate + /// + /// The grids this candidate claimed + /// The drawing + /// The workArea /// The minCountToBeat /// The maxUtilization /// The partArea /// The token /// The private List EvaluateCandidate( - BestFitResult candidate, + List<(Pattern Pattern, NestDirection Dir)> claimed, Drawing drawing, Box workArea, int minCountToBeat, @@ -375,28 +422,16 @@ namespace OpenNest.Engine.Fill CancellationToken token ) { - var pairParts = candidate.BuildParts(drawing); - var angles = BuildTilingAngles(candidate); - // Phase 1: evaluate all grids (fast) var grids = new List<(List Parts, NestDirection Dir)>(); - foreach (var angle in angles) + var engine = new FillLinear(workArea, partSpacing) { Label = "Pairs" }; + foreach (var (pattern, dir) in claimed) { token.ThrowIfCancellationRequested(); - var pattern = FillHelpers.BuildRotatedPattern(pairParts, angle); - if (pattern.Parts.Count == 0) - continue; - var engine = new FillLinear(workArea, partSpacing) { Label = "Pairs" }; - foreach (var dir in new[] { NestDirection.Horizontal, NestDirection.Vertical }) - { - if (!dedup.TryAdd(pattern.BoundingBox, workArea, dir)) - continue; - - var gridParts = engine.Fill(pattern, dir); - if (gridParts != null && gridParts.Count > 0) - grids.Add((gridParts, dir)); - } + var gridParts = engine.Fill(pattern, dir); + if (gridParts != null && gridParts.Count > 0) + grids.Add((gridParts, dir)); } if (grids.Count == 0)