-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_fast_engine.py
More file actions
286 lines (260 loc) · 9.13 KB
/
Copy pathbenchmark_fast_engine.py
File metadata and controls
286 lines (260 loc) · 9.13 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""Synthetic benchmark for ThermalSim's large-grid CPU engine.
The default scenario creates an equivalent ten-million-node multilayer PCB
without requiring a proprietary board file. It measures structured-operator
construction, adaptive reduction, and optional transient solve time. Legacy
CSR assembly is run only below a configurable safety threshold.
"""
import argparse
import ctypes
import json
import math
import os
from pathlib import Path
import sys
import time
PLUGIN_DIR = Path(__file__).resolve().parent
sys.path.insert(0, str(PLUGIN_DIR.parent))
try:
import pcbnew # noqa: F401
except ImportError:
sys.path.insert(0, str(PLUGIN_DIR / "tests"))
from mocks.pcbnew_mock import install_mock
from mocks.wx_mock import install_wx_mock
install_mock()
install_wx_mock()
import numpy as np
from KiCad_Thermal_Sim.adaptive_mesh import build_adaptive_mesh, build_adaptive_system
from KiCad_Thermal_Sim.thermal_solver import (
SolverConfig,
build_stiffness_matrix,
build_structured_operator,
run_simulation,
run_simulation_matrix_free,
)
def _working_set_bytes():
"""Return the current Windows process working set, when available."""
if os.name != "nt":
return None
class Counters(ctypes.Structure):
_fields_ = [
("cb", ctypes.c_ulong),
("PageFaultCount", ctypes.c_ulong),
("PeakWorkingSetSize", ctypes.c_size_t),
("WorkingSetSize", ctypes.c_size_t),
("QuotaPeakPagedPoolUsage", ctypes.c_size_t),
("QuotaPagedPoolUsage", ctypes.c_size_t),
("QuotaPeakNonPagedPoolUsage", ctypes.c_size_t),
("QuotaNonPagedPoolUsage", ctypes.c_size_t),
("PagefileUsage", ctypes.c_size_t),
("PeakPagefileUsage", ctypes.c_size_t),
]
counters = Counters()
counters.cb = ctypes.sizeof(counters)
get_process = ctypes.windll.kernel32.GetCurrentProcess
get_process.argtypes = []
get_process.restype = ctypes.c_void_p
get_memory = ctypes.windll.psapi.GetProcessMemoryInfo
get_memory.argtypes = [
ctypes.c_void_p,
ctypes.POINTER(Counters),
ctypes.c_ulong,
]
get_memory.restype = ctypes.c_int
process = get_process()
if not get_memory(
process, ctypes.byref(counters), counters.cb
):
return None
return int(counters.WorkingSetSize)
def synthetic_geometry(total_nodes=10_000_000, layers=4):
"""Create deterministic dense routing, planes, vias, and source masks."""
cells_per_layer = max(1, int(math.ceil(total_nodes / max(layers, 1))))
rows = max(8, int(math.sqrt(cells_per_layer * 0.7)))
cols = max(8, int(math.ceil(cells_per_layer / rows)))
yy, xx = np.ogrid[:rows, :cols]
copper = np.zeros((layers, rows, cols), dtype=bool)
for layer_idx in range(layers):
horizontal = np.mod(yy + 11 * layer_idx, 53) < 2
vertical = np.mod(xx + 17 * layer_idx, 67) < 2
diagonal = np.mod(xx + yy * (layer_idx + 1), 109) < 2
plane = (
(xx > cols // 6)
& (xx < 5 * cols // 6)
& (yy > rows // 5)
& (yy < 4 * rows // 5)
)
holes = ((xx - cols // 2) ** 2 + (yy - rows // 2) ** 2) < (
min(rows, cols) // 9
) ** 2
copper[layer_idx] = horizontal | vertical | diagonal | (plane & ~holes)
via_map = np.ones((rows, cols), dtype=np.float64)
via_map[
(np.mod(yy, 64) < 2) & (np.mod(xx, 64) < 2)
] = 50.0
heatsink = np.zeros((rows, cols), dtype=bool)
heatsink[
rows // 3:2 * rows // 3,
cols // 3:2 * cols // 3,
] = True
source = np.zeros((layers, rows, cols), dtype=bool)
source[
0,
rows // 2 - 3:rows // 2 + 4,
cols // 2 - 3:cols // 2 + 4,
] = True
return copper, via_map, heatsink, source
def _timed(function, *args, **kwargs):
started = time.perf_counter()
value = function(*args, **kwargs)
return value, time.perf_counter() - started
def benchmark(args):
copper, via_map, heatsink, source_mask = synthetic_geometry(
args.nodes, args.layers
)
layers, rows, cols = copper.shape
node_count = copper.size
settings = {
"h_conv": 10.0,
"pad_th": 1.0,
"pad_k": 3.0,
}
t_cu = np.full(layers, 35e-6)
t_fr4 = np.full(layers, 1.6e-3 / max(layers, 1))
gaps = [1.6e-3 / max(layers - 1, 1)] * max(0, layers - 1)
dx = dy = args.resolution_mm * 1e-3
common = (
layers, rows, cols, copper, t_cu, t_fr4,
390.0, 0.3, dx, dy, via_map, gaps,
heatsink.astype(np.float64), settings, 25.0,
)
memory_start = _working_set_bytes()
(structured, boundary, h_area, _), structured_s = _timed(
build_structured_operator, *common
)
memory_structured = _working_set_bytes()
pixel_area = dx * dy
capacity_layers = np.empty_like(copper, dtype=np.float64)
for layer_idx in range(layers):
cu_capacity = 8960.0 * 385.0 * pixel_area * t_cu[layer_idx]
fr4_capacity = 1850.0 * 1100.0 * pixel_area * t_fr4[layer_idx]
capacity_layers[layer_idx] = np.where(
copper[layer_idx],
cu_capacity + fr4_capacity,
fr4_capacity,
)
capacity = capacity_layers.reshape(-1)
power = np.zeros(node_count, dtype=np.float64)
source_indices = np.flatnonzero(source_mask.reshape(-1))
power[source_indices] = 10.0 / max(source_indices.size, 1)
mesh, mesh_s = _timed(
build_adaptive_mesh,
copper,
via_map,
heatsink,
source_mask,
args.max_cell_ratio,
)
adaptive, reduction_s = _timed(
build_adaptive_system,
structured,
capacity,
power,
boundary,
h_area,
mesh,
)
memory_adaptive = _working_set_bytes()
result = {
"grid": {
"rows": rows,
"cols": cols,
"layers": layers,
"equivalent_uniform_nodes": node_count,
"adaptive_nodes": adaptive.operator.shape[0],
"reduction_ratio": node_count / adaptive.operator.shape[0],
},
"timings": {
"structured_operator_s": structured_s,
"adaptive_mesh_s": mesh_s,
"adaptive_operator_s": reduction_s,
},
"memory_mb": {
"start": memory_start / 1024 ** 2 if memory_start else None,
"structured": memory_structured / 1024 ** 2 if memory_structured else None,
"adaptive": memory_adaptive / 1024 ** 2 if memory_adaptive else None,
},
}
if args.solve_steps > 0:
config = SolverConfig(
sim_time=float(args.solve_steps),
amb=25.0,
dt_base=1.0,
steps_target=args.solve_steps,
use_multi_phase=False,
time_stepping="uniform",
)
solved, solve_s = _timed(
run_simulation_matrix_free,
config,
adaptive.operator,
adaptive.capacity,
adaptive.power,
adaptive.boundary_rhs,
adaptive.h_area,
)
result["timings"]["adaptive_solver_s"] = solve_s
result["solver"] = {
"steps": solved.step_counter,
"avg_pcg_iterations": solved.k_norm_info["avg_pcg_iterations"],
"max_temperature_c": float(np.max(solved.T)),
}
if node_count <= args.legacy_max_nodes:
(legacy_matrix, legacy_b, legacy_h, _), legacy_build_s = _timed(
build_stiffness_matrix, *common
)
result["timings"]["legacy_matrix_build_s"] = legacy_build_s
if args.solve_steps > 0:
legacy_config = SolverConfig(
sim_time=float(args.solve_steps),
amb=25.0,
dt_base=1.0,
steps_target=args.solve_steps,
use_multi_phase=False,
time_stepping="uniform",
)
_, legacy_solver_s = _timed(
run_simulation,
legacy_config,
legacy_matrix,
capacity,
power,
legacy_b,
legacy_h,
layers,
rows,
cols,
)
result["timings"]["legacy_solver_s"] = legacy_solver_s
else:
result["legacy_skipped"] = (
f"{node_count:,} nodes exceeds the "
f"{args.legacy_max_nodes:,}-node safety limit"
)
return result
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--nodes", type=int, default=10_000_000)
parser.add_argument("--layers", type=int, default=4)
parser.add_argument("--resolution-mm", type=float, default=0.1)
parser.add_argument("--max-cell-ratio", type=int, default=8)
parser.add_argument("--solve-steps", type=int, default=3)
parser.add_argument("--legacy-max-nodes", type=int, default=1_000_000)
parser.add_argument("--output")
args = parser.parse_args()
result = benchmark(args)
text = json.dumps(result, indent=2, sort_keys=True)
if args.output:
Path(args.output).write_text(text + "\n", encoding="utf-8")
print(text)
if __name__ == "__main__":
main()