forked from nokia/stm-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScalaStmSolver.scala
157 lines (136 loc) · 5.45 KB
/
ScalaStmSolver.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* © 2023-2024 Nokia
* Licensed under the Apache License 2.0
* SPDX-License-Identifier: Apache-2.0
*/
package com.nokia.stmbenchmark
package scalastm
import scala.concurrent.stm.{ atomic, InTxn }
import cats.syntax.traverse._
import cats.effect.kernel.Async
import cats.effect.syntax.concurrent._
import cats.effect.std.Console
import common.{ Solver, Board, Point, Route, BoolMatrix }
object ScalaStmSolver {
def apply[F[_]](parLimit: Int, log: Boolean)(implicit F: Async[F]): F[Solver[F]] = {
F.pure(
new Solver[F] {
private[this] final def debug(msg: String): Unit = {
if (log) println(msg)
else ()
}
private[this] val _c =
Console.make[F]
private[this] final def debugF(msg: String): F[Unit] = {
if (log) _c.println(msg)
else F.unit
}
final override def solve(board: Board.Normalized): F[Solver.Solution] = {
val obstructed = BoolMatrix.obstructedFromBoard(board)
def solveOneRoute(depth: TMatrix[Int], route: Route): List[Point] = {
atomic { implicit txn =>
debug(s"Solving $route")
val cost = expand(depth, route)
val costStr = cost.debug(debug = log)(i => f"$i%2s", txn)
debug("Cost after `expand`:\n" + costStr)
val solution = solve(route, cost)
debug(s"Solution:\n" + board.debugSolution(Map(route -> solution), debug = log))
lay(depth, solution)
solution
}
}
def expand(depth: TMatrix[Int], route: Route)(implicit txn: InTxn): TMatrix[Int] = {
val startPoint = route.a
val endPoint = route.b
val cost = TMatrix[Int](h = depth.height, w = depth.width, initial = 0)
cost.set(startPoint.y, startPoint.x, 1)
var wavefront = List(startPoint)
var go = true
while (go) {
val newWavefront = new scala.collection.mutable.ListBuffer[Point]()
for (point <- wavefront) {
val pointCost = cost.get(point.y, point.x)
for (adjacent <- board.adjacentPoints(point)) {
if (obstructed(adjacent.y, adjacent.x) && (adjacent != endPoint)) {
// can't go in that direction
} else {
val currentCost = cost.get(adjacent.y, adjacent.x)
val newCost = pointCost + Board.cost(depth.get(adjacent.y, adjacent.x))
if ((currentCost == 0) || (newCost < currentCost)) {
cost.set(adjacent.y, adjacent.x, newCost)
newWavefront += adjacent
} else {
// not better
}
}
}
}
if (newWavefront.isEmpty) {
throw new Solver.Stuck
} else {
val costAtRouteEnd = cost.get(endPoint.y, endPoint.x)
if (costAtRouteEnd > 0) {
val newCosts = newWavefront.map { marked => cost.get(marked.y, marked.x) }
val minimumNewCost = newCosts.min
if (costAtRouteEnd < minimumNewCost) {
// no new location has lower cost than the
// cost currently at the route end, so
// no reason to continue:
go = false
} else {
// continue with the new wavefront:
wavefront = newWavefront.result()
}
} else {
// continue with the new wavefront:
wavefront = newWavefront.result()
}
}
}
cost
}
def solve(route: Route, cost: TMatrix[Int])(implicit txn: InTxn): List[Point] = {
// we're going *back* from the route end:
val startPoint = route.b
val endPoint = route.a
var solution = List(startPoint)
while (solution.head != endPoint) {
val adjacent = board.adjacentPoints(solution.head)
val costs = adjacent.map { a =>
val aCost = cost.get(a.y, a.x)
(a, aCost)
}
val lowestCost = costs.filter(_._2 != 0).minBy(_._2) // TODO: simplify
solution = lowestCost._1 +: solution
}
solution
}
def lay(depth: TMatrix[Int], solution: List[Point])(implicit txn: InTxn): Unit = {
for (point <- solution) {
val ov: Int = depth.get(point.y, point.x)
depth.set(point.y, point.x, ov + 1)
}
}
F.defer {
val depth = TMatrix[Int](h = board.height, w = board.width, initial = 0)
val solveOne = { (route: Route) =>
F.delay {
val solution = solveOneRoute(depth, route)
(route, solution)
}
}
val solveInParallel = if (parLimit == 1) {
board.routes.traverse(solveOne)
} else {
board.routes.parTraverseN(parLimit)(solveOne)
}
F.flatMap(solveInParallel) { solutions =>
val solution = Map(solutions: _*)
F.as(debugF("Full solution:\n" + board.debugSolution(solution, debug = log)), Solver.Solution(solution))
}
}
}
}
)
}
}