Skip to content

Commit 209c866

Browse files
authored
Merge pull request #11 from tscircuit/improve-section
Improve section solver speed by reducing "full array resets"
2 parents d346e1c + b9e0aa9 commit 209c866

2 files changed

Lines changed: 47 additions & 13 deletions

File tree

lib/core.ts

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ export interface TinyHyperGraphWorkingState {
132132

133133
candidateQueue: MinHeap<Candidate>
134134
candidateBestCostByHopId: Float64Array
135+
candidateBestCostGenerationByHopId: Uint32Array
136+
candidateBestCostGeneration: number
135137

136138
goalPortId: PortId
137139

@@ -236,7 +238,11 @@ export class TinyHyperGraphSolver extends BaseSolver {
236238
candidateQueue: new MinHeap([], compareCandidatesByF),
237239
candidateBestCostByHopId: new Float64Array(
238240
topology.portCount * topology.regionCount,
239-
).fill(Number.POSITIVE_INFINITY),
241+
),
242+
candidateBestCostGenerationByHopId: new Uint32Array(
243+
topology.portCount * topology.regionCount,
244+
),
245+
candidateBestCostGeneration: 1,
240246
goalPortId: -1,
241247
ripCount: 0,
242248
regionCongestionCost: new Float64Array(topology.regionCount).fill(0),
@@ -294,7 +300,7 @@ export class TinyHyperGraphSolver extends BaseSolver {
294300
state.currentRouteId = state.unroutedRoutes.shift()
295301
state.currentRouteNetId = problem.routeNet[state.currentRouteId!]
296302

297-
state.candidateBestCostByHopId.fill(Number.POSITIVE_INFINITY)
303+
this.resetCandidateBestCosts()
298304
const startingPortId = problem.routeStartPort[state.currentRouteId!]
299305
state.candidateQueue.clear()
300306
const startingNextRegionId = this.getStartingNextRegionId(
@@ -308,9 +314,10 @@ export class TinyHyperGraphSolver extends BaseSolver {
308314
return
309315
}
310316

311-
state.candidateBestCostByHopId[
312-
this.getHopId(startingPortId, startingNextRegionId)
313-
] = 0
317+
this.setCandidateBestCost(
318+
this.getHopId(startingPortId, startingNextRegionId),
319+
0,
320+
)
314321
state.candidateQueue.queue({
315322
nextRegionId: startingNextRegionId,
316323
portId: startingPortId,
@@ -332,9 +339,7 @@ export class TinyHyperGraphSolver extends BaseSolver {
332339
currentCandidate.portId,
333340
currentCandidate.nextRegionId,
334341
)
335-
if (
336-
currentCandidate.g > state.candidateBestCostByHopId[currentCandidateHopId]
337-
) {
342+
if (currentCandidate.g > this.getCandidateBestCost(currentCandidateHopId)) {
338343
return
339344
}
340345

@@ -394,13 +399,42 @@ export class TinyHyperGraphSolver extends BaseSolver {
394399
}
395400

396401
const candidateHopId = this.getHopId(neighborPortId, nextRegionId)
397-
if (g >= state.candidateBestCostByHopId[candidateHopId]) continue
402+
if (g >= this.getCandidateBestCost(candidateHopId)) continue
398403

399-
state.candidateBestCostByHopId[candidateHopId] = g
404+
this.setCandidateBestCost(candidateHopId, g)
400405
state.candidateQueue.queue(newCandidate)
401406
}
402407
}
403408

409+
resetCandidateBestCosts() {
410+
const { state } = this
411+
412+
if (state.candidateBestCostGeneration === 0xffffffff) {
413+
state.candidateBestCostGenerationByHopId.fill(0)
414+
state.candidateBestCostGeneration = 1
415+
return
416+
}
417+
418+
state.candidateBestCostGeneration += 1
419+
}
420+
421+
getCandidateBestCost(hopId: HopId) {
422+
const { state } = this
423+
424+
return state.candidateBestCostGenerationByHopId[hopId] ===
425+
state.candidateBestCostGeneration
426+
? state.candidateBestCostByHopId[hopId]!
427+
: Number.POSITIVE_INFINITY
428+
}
429+
430+
setCandidateBestCost(hopId: HopId, bestCost: number) {
431+
const { state } = this
432+
433+
state.candidateBestCostGenerationByHopId[hopId] =
434+
state.candidateBestCostGeneration
435+
state.candidateBestCostByHopId[hopId] = bestCost
436+
}
437+
404438
getHopId(portId: PortId, nextRegionId: RegionId): HopId {
405439
return portId * this.topology.regionCount + nextRegionId
406440
}
@@ -600,7 +634,7 @@ export class TinyHyperGraphSolver extends BaseSolver {
600634
state.currentRouteId = undefined
601635
state.unroutedRoutes = shuffle(range(problem.routeCount), state.ripCount)
602636
state.candidateQueue.clear()
603-
state.candidateBestCostByHopId.fill(Number.POSITIVE_INFINITY)
637+
this.resetCandidateBestCosts()
604638
state.goalPortId = -1
605639
}
606640

lib/section-solver/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ const applyRouteSegmentsToSolver = (
314314
solver.state.currentRouteNetId = undefined
315315
solver.state.unroutedRoutes = []
316316
solver.state.candidateQueue.clear()
317-
solver.state.candidateBestCostByHopId.fill(Number.POSITIVE_INFINITY)
317+
solver.resetCandidateBestCosts()
318318
solver.state.goalPortId = -1
319319
solver.state.ripCount = 0
320320
solver.state.regionCongestionCost.fill(0)
@@ -609,7 +609,7 @@ class TinyHyperGraphSectionSearchSolver extends TinyHyperGraphSolver {
609609
this.state.currentRouteNetId = undefined
610610
this.state.unroutedRoutes = []
611611
this.state.candidateQueue.clear()
612-
this.state.candidateBestCostByHopId.fill(Number.POSITIVE_INFINITY)
612+
this.resetCandidateBestCosts()
613613
this.state.goalPortId = -1
614614
}
615615

0 commit comments

Comments
 (0)