MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
solver.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4
5#include <optional>
6
7#include <fe/bitset.h>
8#include <fe/span.h>
9
10#include "pbqp/graph.h"
11
12namespace pbqp {
13
14/// How often `solve` applied each reduction.
15struct Statistics {
16 size_t r0 = 0;
17 size_t r1 = 0;
18 size_t r2 = 0;
19 size_t rn = 0;
20 size_t rm = 0; // neighbours merged before an RN decision
21 size_t single_option = 0; // degree >= 3 nodes with one option left, reduced exactly
23 size_t dropped_edges = 0; // independent edges removed by normalization
24};
25
26/// A complete assignment of options to nodes.
27struct Solution {
28 /// choices[n] is the option chosen for node `n`.
30 /// Total cost of the assignment under the original graph.
32 /// Nodes whose choice hits a Cost::inf entry, see `conflicts`; empty iff feasible.
33 fe::Bitset conflicts;
35
36 [[nodiscard]] bool feasible() const { return !cost.is_inf(); }
37};
38
39/// All solver functions are safe to call concurrently on a graph that nobody mutates meanwhile.
40///
41/// Cost of `choices` under `graph`; Cost::inf if it picks a Cost::inf entry or the total saturates.
42/// Requires a valid option for every node.
43[[nodiscard]] Cost evaluate(const Graph& graph, fe::View<OptionId> choices);
44
45/// Nodes whose choice is Cost::inf in their cost vector or, given the neighbour's choice, in an incident matrix.
46/// These are the candidates to spill or split when an assignment is infeasible.
47/// Requires a valid option for every node.
48[[nodiscard]] fe::Bitset conflicts(const Graph& graph, fe::View<OptionId> choices);
49
50/// Heuristic PBQP solver.
51///
52/// Reduces degree 0, 1 (RI) and 2 (RII) nodes exactly and higher-degree nodes with the RN heuristic.
53/// Normalization keeps intermediate costs relative, so they saturate only where option costs differ by Cost::inf.
54/// Short of that, the result is optimal whenever no RN step is needed, e.g. on trees.
55/// RN decides the node with the highest `rn_priority[n]` first; ties go to the highest degree, then the widest cost
56/// spread. SSA-based register allocation passes PEO positions, so coloring cannot fail (Buchwald et al. 2011). Requires
57/// `rn_priority` to be empty or to cover every node id.
58[[nodiscard]] Solution solve(const Graph& graph, fe::View<size_t> rn_priority = {});
59
60/// Brute-force optimum, useful to cross-check small instances.
61/// The search space is split across `num_threads` threads (0 = hardware concurrency).
62/// Returns nothing if there are more than `max_combinations` candidate assignments.
63[[nodiscard]] std::optional<Solution>
64solve_exhaustive(const Graph& graph, size_t max_combinations = 10'000'000, unsigned num_threads = 0);
65
66} // namespace pbqp
A saturating cost; Cost::inf forbids an option or option pair.
Definition graph.h:31
A PBQP problem graph.
Definition graph.h:252
Definition dot.h:9
size_t dropped_edges
Definition solver.h:23
fe::Vector< OptionId, 8 > Choices
One option per node, indexed by node id.
Definition graph.h:68
size_t single_option
Definition solver.h:21
size_t forbidden_options
Definition solver.h:22
Solution solve(const Graph &graph, fe::View< size_t > rn_priority={})
Heuristic PBQP solver.
Definition solver.cpp:629
Cost evaluate(const Graph &graph, fe::View< OptionId > choices)
All solver functions are safe to call concurrently on a graph that nobody mutates meanwhile.
Definition solver.cpp:591
fe::Bitset conflicts(const Graph &graph, fe::View< OptionId > choices)
Nodes whose choice is Cost::inf in their cost vector or, given the neighbour's choice,...
Definition solver.cpp:607
std::optional< Solution > solve_exhaustive(const Graph &graph, size_t max_combinations=10 '000 '000, unsigned num_threads=0)
Brute-force optimum, useful to cross-check small instances.
Definition solver.cpp:691
How often solve applied each reduction.
Definition solver.h:15
A complete assignment of options to nodes.
Definition solver.h:27
bool feasible() const
Definition solver.h:36
Cost cost
Total cost of the assignment under the original graph.
Definition solver.h:31
fe::Bitset conflicts
Nodes whose choice hits a Cost::inf entry, see conflicts; empty iff feasible.
Definition solver.h:33
Choices choices
choices[n] is the option chosen for node n.
Definition solver.h:29
Statistics statistics
Definition solver.h:34