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 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-10-30 11:16:05 -04:00
1 parent 7a7b671408
commit e76864b489
2 files changed
+61 -24

No files matched your search

@@ -22,7 +22,9 @@ namespace OpenNest.Engine.Tests.Jobs;
/// <remarks>
/// 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 <c>PairFiller</c>'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
/// <c>AngleCandidateBuilder.RecordProductive</c> learned from earlier ones.
/// Not pinned: richer mixes that pair parallelogram, chevron or tee parts at oblique angles
+58 -23
View File
@@ -43,6 +43,13 @@ namespace OpenNest.Engine.Fill
/// </summary>
private const int EarlyExitStaleLimit = 10;
/// <summary>
/// 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.
/// </summary>
private const int CandidateBatchSize = 8;
/// <summary>
/// Defines the plate
/// </summary>
@@ -174,7 +181,6 @@ namespace OpenNest.Engine.Fill
List<Part> 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<Part>[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
}
/// <summary>
/// The EvaluateCandidate
/// Builds the candidate's tiling patterns and claims the grids no earlier candidate took.
/// </summary>
/// <param name="candidate">The candidate<see cref="BestFitResult"/></param>
/// <param name="drawing">The drawing<see cref="Drawing"/></param>
/// <param name="workArea">The workArea<see cref="Box"/></param>
/// <returns>The patterns and directions left for this candidate to fill</returns>
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;
}
/// <summary>
/// The EvaluateCandidate
/// </summary>
/// <param name="claimed">The grids this candidate claimed</param>
/// <param name="drawing">The drawing<see cref="Drawing"/></param>
/// <param name="workArea">The workArea<see cref="Box"/></param>
/// <param name="minCountToBeat">The minCountToBeat<see cref="int"/></param>
/// <param name="maxUtilization">The maxUtilization<see cref="double"/></param>
/// <param name="partArea">The partArea<see cref="double"/></param>
/// <param name="token">The token<see cref="CancellationToken"/></param>
/// <returns>The <see cref="List{Part}"/></returns>
private List<Part> 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<Part> 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)