45using Clock = std::chrono::steady_clock;
48 std::string scenario =
"all";
49 std::size_t width = 128;
50 std::size_t depth = 64;
51 std::size_t iterations = 100;
52 bool selfDesc =
false;
56 std::shared_ptr<Circuit> circuit;
57 std::vector<Bus> inputs;
58 std::vector<Bus> outputs;
59 std::size_t components = 0;
60 std::size_t wires = 0;
68double elapsedMs(
const Clock::time_point start,
const Clock::time_point end)
70 return std::chrono::duration<double, std::milli>(end - start).count();
75 component->setProperty(
"delay", 0);
80 return std::make_shared<Wire>(state);
84 const bool zeroDelay =
true)
88 components.insert(gate);
91Measurement makeLayeredCircuit(
const std::size_t width,
const std::size_t depth)
93 const auto start = Clock::now();
96 std::vector<Wire_ptr> current;
97 std::vector<Bus> inputs;
98 current.reserve(width);
99 inputs.reserve(width);
101 for (std::size_t i = 0; i <
width; ++i) {
103 inputs.push_back(
Bus{wire});
104 current.push_back(std::move(wire));
107 std::size_t wireCount = current.size();
108 for (std::size_t layer = 0; layer < depth; ++layer) {
109 std::vector<Wire_ptr> next;
112 for (std::size_t bit = 0; bit <
width; ++bit) {
114 const auto left = current[bit];
115 const auto right = current[(bit + layer + 1) % width];
117 switch ((layer + bit) % 3) {
120 std::make_shared<AndGate>(std::vector<Wire_ptr>{left, right}, output));
124 std::make_shared<OrGate>(std::vector<Wire_ptr>{left, right}, output));
127 addGate(components, std::make_shared<XorGate>(
128 std::array<Wire_ptr, 2>{left, right}, output));
132 next.push_back(std::move(output));
135 wireCount += next.size();
136 current = std::move(next);
139 std::vector<Bus> outputs;
140 outputs.reserve(current.size());
141 for (
const auto& wire : current)
142 outputs.push_back(
Bus{wire});
145 bench.circuit = std::make_shared<Circuit>(components,
false);
146 bench.inputs = std::move(inputs);
147 bench.outputs = std::move(outputs);
148 bench.components = components.size();
149 bench.wires = wireCount;
151 return {std::move(bench), elapsedMs(start, Clock::now())};
154Measurement makeFanoutCircuit(
const std::size_t width,
const std::size_t depth)
156 const auto start = Clock::now();
160 std::vector<Bus> inputs{
Bus{root}};
161 std::vector<Bus> outputs;
162 std::size_t wireCount = 1;
164 outputs.reserve(width);
165 for (std::size_t branch = 0; branch <
width; ++branch) {
170 for (std::size_t stage = 0; stage < depth; ++stage) {
172 if ((stage + branch) % 2 == 0) {
174 std::make_shared<AndGate>(std::vector<Wire_ptr>{current, salt}, output));
177 std::make_shared<OrGate>(std::vector<Wire_ptr>{current, salt}, output));
179 current = std::move(output);
183 outputs.push_back(
Bus{current});
187 bench.circuit = std::make_shared<Circuit>(components,
false);
188 bench.inputs = std::move(inputs);
189 bench.outputs = std::move(outputs);
190 bench.components = components.size();
191 bench.wires = wireCount;
193 return {std::move(bench), elapsedMs(start, Clock::now())};
196Measurement makeDelayedChainsCircuit(
const std::size_t width,
const std::size_t depth)
198 const auto start = Clock::now();
201 std::vector<Bus> inputs;
202 std::vector<Bus> outputs;
203 std::size_t wireCount = 0;
205 inputs.reserve(width);
206 outputs.reserve(width);
207 for (std::size_t chain = 0; chain <
width; ++chain) {
209 inputs.push_back(
Bus{current});
212 for (std::size_t stage = 0; stage < depth; ++stage) {
214 auto gate = std::make_shared<NotGate>(current, output);
215 gate->setProperty(
"delay", 1);
216 components.insert(gate);
217 current = std::move(output);
221 outputs.push_back(
Bus{current});
225 bench.circuit = std::make_shared<Circuit>(components,
false);
226 bench.inputs = std::move(inputs);
227 bench.outputs = std::move(outputs);
228 bench.components = components.size();
229 bench.wires = wireCount;
231 return {std::move(bench), elapsedMs(start, Clock::now())};
234Measurement makeCyclicCombinationalCircuit(
const std::size_t width,
235 const std::size_t depth)
237 const auto start = Clock::now();
240 std::vector<Bus> inputs;
241 std::vector<Bus> outputs;
242 std::size_t wireCount = 0;
244 inputs.reserve(width);
245 outputs.reserve(width);
246 for (std::size_t ring = 0; ring <
width; ++ring) {
248 inputs.push_back(
Bus{forcingInput});
251 std::vector<Wire_ptr> ringWires;
252 ringWires.reserve(depth);
253 for (std::size_t stage = 0; stage < depth; ++stage) {
258 for (std::size_t stage = 0; stage < depth; ++stage) {
259 const auto input = ringWires[stage];
260 const auto output = ringWires[(stage + 1) % depth];
262 if ((stage + ring) % 2 == 0) {
263 addGate(components, std::make_shared<OrGate>(
264 std::vector<Wire_ptr>{input, forcingInput}, output));
266 addGate(components, std::make_shared<AndGate>(
267 std::vector<Wire_ptr>{input, forcingInput}, output));
271 outputs.push_back(
Bus{ringWires.front()});
275 bench.circuit = std::make_shared<Circuit>(components,
false);
276 bench.inputs = std::move(inputs);
277 bench.outputs = std::move(outputs);
278 bench.components = components.size();
279 bench.wires = wireCount;
281 return {std::move(bench), elapsedMs(start, Clock::now())};
284Measurement makeRegisteredCyclicCircuit(
const std::size_t width,
const std::size_t depth)
286 const auto start = Clock::now();
293 std::vector<Bus> inputs{
Bus{clock}};
294 std::vector<Bus> outputs;
295 std::vector<Wire_ptr> qWires;
296 std::vector<Wire_ptr> dWires;
297 std::size_t wireCount = 3;
299 qWires.reserve(width);
300 dWires.reserve(width);
301 outputs.reserve(width);
302 for (std::size_t bit = 0; bit <
width; ++bit) {
305 outputs.push_back(
Bus{qWires.back()});
309 for (std::size_t bit = 0; bit <
width; ++bit) {
310 auto current = qWires[(bit +
width - 1) % width];
311 for (std::size_t stage = 0; stage < depth; ++stage) {
312 const auto output = stage + 1 == depth ? dWires[bit] : makeWire(
State::UNKNOWN);
313 addGate(components, std::make_shared<NotGate>(current, output));
315 if (stage + 1 != depth)
321 std::make_shared<DFlipFlop>(dWires[bit], clock, clear, preset, qWires[bit], notQ);
322 ff->setProperty(
"propagationDelay", 0);
323 components.insert(ff);
328 bench.circuit = std::make_shared<Circuit>(components,
false);
329 bench.inputs = std::move(inputs);
330 bench.outputs = std::move(outputs);
331 bench.components = components.size();
332 bench.wires = wireCount;
334 return {std::move(bench), elapsedMs(start, Clock::now())};
337std::size_t checksum(
const std::vector<Bus>& buses)
339 std::size_t result = 0;
340 for (
const auto& bus : buses) {
341 for (
const auto state : bus.getCurrentValue())
342 result = result * 131U +
static_cast<std::size_t
>(std::to_underlying(state));
343 result = result * 17U +
static_cast<std::size_t
>(bus.hasUnknowns());
344 result = result * 17U +
static_cast<std::size_t
>(bus.isInErrorState());
349template <
typename Value>
350void printMetric(
const std::string_view name,
const Value& value,
351 const std::string_view description,
const bool selfDesc)
353 std::cout <<
" " << name <<
": " <<
value;
355 std::cout <<
" # " << description;
359void printScenarioHeader(
const std::string_view name,
const std::string_view description,
360 const Measurement& measurement,
const bool selfDesc)
362 std::cout << name <<
'\n';
364 printMetric(
"description", description,
"scenario topology and measured behavior",
366 printMetric(
"components", measurement.bench.components,
367 "number of simulated components in this generated circuit", selfDesc);
368 printMetric(
"wires", measurement.bench.wires,
369 "number of wires allocated by this generated circuit", selfDesc);
370 printMetric(
"build_ms", measurement.buildMs,
371 "milliseconds spent constructing the generated circuit", selfDesc);
374void printUsage(
const char* executable)
377 <<
"Usage: " << executable
378 <<
" [--scenario all|layered|fanout|delayed|cyclic|registered-cyclic]"
380 " [--depth N] [--iterations N] [--selfdesc]\n\n"
381 <<
"Runs self-contained simulator performance testbenches.\n\n"
383 <<
" layered wide acyclic gate layers; measures forward "
385 <<
" fanout one input feeding many gate chains; measures fanout "
387 <<
" delayed NOT chains with unit gate delays; measures event "
389 <<
" cyclic forced combinational rings; measures cyclic "
391 <<
" registered-cyclic flip-flop feedback rings; measures clocked "
394 <<
" --width N number of parallel bits, branches, chains, or rings\n"
395 <<
" --depth N number of gate stages per topology\n"
396 <<
" --iterations N number of input toggles or clock cycles to measure\n"
397 <<
" --selfdesc include metric descriptions in benchmark output\n";
400std::size_t parseSize(
const char* value,
const std::string_view option)
403 const auto parsed = std::stoull(value);
405 throw std::invalid_argument(
"zero");
407 }
catch (
const std::exception&) {
408 throw std::invalid_argument(std::string(option) +
" expects a positive integer");
412Options parseOptions(
const int argc,
char** argv)
415 for (
int i = 1; i < argc; ++i) {
416 const std::string_view arg = argv[i];
417 auto requireValue = [&](
const std::string_view option) ->
char* {
419 throw std::invalid_argument(std::string(option) +
" requires a value");
423 if (arg ==
"--help" || arg ==
"-h") {
426 }
else if (arg ==
"--scenario") {
427 options.scenario = requireValue(arg);
428 }
else if (arg ==
"--width") {
429 options.width = parseSize(requireValue(arg), arg);
430 }
else if (arg ==
"--depth") {
431 options.depth = parseSize(requireValue(arg), arg);
432 }
else if (arg ==
"--iterations") {
433 options.iterations = parseSize(requireValue(arg), arg);
434 }
else if (arg ==
"--selfdesc") {
435 options.selfDesc =
true;
437 throw std::invalid_argument(
"unknown option: " + std::string(arg));
444void runPropagationBenchmark(
const std::string_view name, Measurement measurement,
445 const std::string_view description,
446 const std::size_t iterations,
const bool selfDesc)
448 const auto simulatorStart = Clock::now();
449 Simulator simulator(measurement.bench.circuit);
450 const auto simulatorMs = elapsedMs(simulatorStart, Clock::now());
452 const auto runStart = Clock::now();
453 for (std::size_t iteration = 0; iteration < iterations; ++iteration) {
454 const auto value =
static_cast<unsigned int>(iteration & 1U);
455 for (
const auto& input : measurement.bench.inputs) {
458 throw std::runtime_error(
"setBus did not complete");
461 throw std::runtime_error(
"runUntilIdle did not complete");
463 const auto runMs = elapsedMs(runStart, Clock::now());
465 printScenarioHeader(name, description, measurement, selfDesc);
466 printMetric(
"simulator_construct_ms", simulatorMs,
467 "milliseconds spent constructing the Simulator from the circuit", selfDesc);
468 printMetric(
"iterations", iterations,
469 "number of input toggle and settle rounds measured", selfDesc);
470 printMetric(
"propagation_ms", runMs,
471 "total milliseconds spent applying inputs and settling propagation",
473 printMetric(
"propagation_ms_per_iteration", runMs / iterations,
474 "average propagation milliseconds per input toggle and settle round",
476 printMetric(
"checksum", checksum(measurement.bench.outputs),
477 "hash of final output states used to keep the benchmark work observable",
482void runDelayedBenchmark(Measurement measurement,
const bool selfDesc)
486 std::max<uint64_t>(previousMaxSteps, measurement.bench.components * 4U));
488 const auto simulatorStart = Clock::now();
489 Simulator simulator(measurement.bench.circuit);
490 const auto simulatorMs = elapsedMs(simulatorStart, Clock::now());
492 const auto runStart = Clock::now();
494 throw std::runtime_error(
"delayed benchmark did not reach idle");
495 const auto runMs = elapsedMs(runStart, Clock::now());
499 printScenarioHeader(
"delayed",
500 "parallel NOT chains with unit gate delays; drains pending "
502 measurement, selfDesc);
503 printMetric(
"simulator_construct_ms", simulatorMs,
504 "milliseconds spent constructing the Simulator from the circuit", selfDesc);
505 printMetric(
"event_queue_drain_ms", runMs,
506 "milliseconds spent processing delayed events until the queue reached idle",
508 printMetric(
"final_time", simulator.getCurrentTime(),
509 "simulated timestamp reached after all delayed events settled", selfDesc);
510 printMetric(
"checksum", checksum(measurement.bench.outputs),
511 "hash of final output states used to keep the benchmark work observable",
516void runCyclicBenchmark(Measurement measurement,
const std::size_t iterations,
521 std::max<std::size_t>(previousMaxTransitions, measurement.bench.components * 4U)));
523 const auto simulatorStart = Clock::now();
524 Simulator simulator(measurement.bench.circuit);
525 const auto simulatorMs = elapsedMs(simulatorStart, Clock::now());
527 const auto runStart = Clock::now();
528 for (std::size_t iteration = 0; iteration < iterations; ++iteration) {
529 const auto value =
static_cast<unsigned int>(iteration & 1U);
530 for (
const auto& input : measurement.bench.inputs) {
533 throw std::runtime_error(
"cyclic setBus did not complete");
536 throw std::runtime_error(
"cyclic benchmark did not reach idle");
538 const auto runMs = elapsedMs(runStart, Clock::now());
542 printScenarioHeader(
"cyclic",
543 "combinational rings forced by external inputs; measures "
544 "delta-cycle settling",
545 measurement, selfDesc);
546 printMetric(
"simulator_construct_ms", simulatorMs,
547 "milliseconds spent constructing the Simulator from the circuit", selfDesc);
548 printMetric(
"iterations", iterations,
549 "number of cyclic input toggle and settle rounds measured", selfDesc);
550 printMetric(
"cyclic_settle_ms", runMs,
551 "total milliseconds spent toggling cyclic inputs and resolving "
552 "delta-cycle feedback",
554 printMetric(
"cyclic_settle_ms_per_iteration", runMs / iterations,
555 "average cyclic settling milliseconds per input toggle round", selfDesc);
556 printMetric(
"checksum", checksum(measurement.bench.outputs),
557 "hash of final output states used to keep the benchmark work observable",
562void runRegisteredCyclicBenchmark(Measurement measurement,
const std::size_t iterations,
565 const auto simulatorStart = Clock::now();
566 Simulator simulator(measurement.bench.circuit);
567 const auto simulatorMs = elapsedMs(simulatorStart, Clock::now());
569 const auto runStart = Clock::now();
570 const auto clock = measurement.bench.inputs.front();
571 for (std::size_t iteration = 0; iteration < iterations; ++iteration) {
574 throw std::runtime_error(
"registered cyclic rising clock did not complete");
576 throw std::runtime_error(
"registered cyclic rising settle did not complete");
579 throw std::runtime_error(
"registered cyclic falling clock did not complete");
581 throw std::runtime_error(
"registered cyclic falling settle did not complete");
583 const auto runMs = elapsedMs(runStart, Clock::now());
585 printScenarioHeader(
"registered-cyclic",
586 "D flip-flop feedback rings separated by combinational NOT "
587 "chains; measures clocked feedback",
588 measurement, selfDesc);
589 printMetric(
"simulator_construct_ms", simulatorMs,
590 "milliseconds spent constructing the Simulator from the circuit", selfDesc);
591 printMetric(
"clock_cycles", iterations,
592 "number of full rising-plus-falling clock cycles measured", selfDesc);
593 printMetric(
"clocked_ms", runMs,
594 "total milliseconds spent clocking and settling registered feedback",
596 printMetric(
"clocked_ms_per_cycle", runMs / iterations,
597 "average milliseconds per full clock cycle", selfDesc);
598 printMetric(
"checksum", checksum(measurement.bench.outputs),
599 "hash of final output states used to keep the benchmark work observable",
604bool shouldRun(
const std::string& selected,
const std::string_view scenario)
606 return selected ==
"all" || selected == scenario;
616 const auto options = parseOptions(argc, argv);
617 if (!shouldRun(options.scenario,
"layered") && !shouldRun(options.scenario,
"fanout")
618 && !shouldRun(options.scenario,
"delayed")
619 && !shouldRun(options.scenario,
"cyclic")
620 && !shouldRun(options.scenario,
"registered-cyclic")) {
621 throw std::invalid_argument(
622 "scenario must be all, layered, fanout, delayed, cyclic, or "
623 "registered-cyclic");
626 std::cout <<
"simulation_perf\n";
627 if (options.selfDesc)
628 printMetric(
"description",
"self-contained simulator performance testbench",
629 "what this executable runs", options.selfDesc);
630 printMetric(
"scenario", options.scenario,
631 "selected topology group; all runs every scenario", options.selfDesc);
632 printMetric(
"width", options.width,
633 "parallel bits, branches, chains, or rings generated per scenario",
635 printMetric(
"depth", options.depth,
"gate stages generated per topology path",
637 printMetric(
"iterations", options.iterations,
638 "input toggles or clock cycles requested for measured scenarios",
642 if (shouldRun(options.scenario,
"layered")) {
643 runPropagationBenchmark(
"layered", makeLayeredCircuit(options.width, options.depth),
644 "wide acyclic layers of AND/OR/XOR gates; measures "
645 "forward propagation",
646 options.iterations, options.selfDesc);
649 if (shouldRun(options.scenario,
"fanout")) {
650 runPropagationBenchmark(
"fanout", makeFanoutCircuit(options.width, options.depth),
651 "one root input feeding many AND/OR gate chains; "
652 "measures fanout propagation",
653 options.iterations, options.selfDesc);
656 if (shouldRun(options.scenario,
"delayed")) {
657 runDelayedBenchmark(makeDelayedChainsCircuit(options.width, options.depth),
661 if (shouldRun(options.scenario,
"cyclic")) {
662 runCyclicBenchmark(makeCyclicCombinationalCircuit(options.width, options.depth),
663 options.iterations, options.selfDesc);
666 if (shouldRun(options.scenario,
"registered-cyclic")) {
667 runRegisteredCyclicBenchmark(
668 makeRegisteredCyclicCircuit(options.width, options.depth), options.iterations,
671 }
catch (
const std::exception& e) {
672 std::cerr <<
"simulation_perf: " << e.what() <<
'\n';
static void setMinimumLevel(LogLevel level)
Executes event-driven simulation of a Circuit.
static uint64_t getMaxSimulationSteps()
static void setMaxSimulationSteps(uint64_t value)
static void setMaxTransitionsPerDeltaCycle(int value)
static int getMaxTransitionsPerDeltaCycle()
int width(RTLIL::Module *module, RTLIL::Cell *cell, RTLIL::IdString key)
std::optional< Gate > gate(Cell *cell, SigMap &signals)
std::shared_ptr< Component > Component_ptr
std::shared_ptr< Wire > Wire_ptr
std::set< Component_ptr, std::owner_less< Component_ptr > > Component_set
BusValue busValueFromInteger(const BigInt &value, const std::size_t width)
Builds an LSB-first BusValue from the low width bits of an integer. Negative values use two's complem...
QVariant value(const QSettings &settings, const Definition &setting)
Reads a setting value, falling back to its declared default when unset.
int main(int argc, char **argv)