MimIR
0.6-dev
MimIR is my Intermediate Representation
Toggle main menu visibility
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
12
namespace
pbqp
{
13
14
/// How often `solve` applied each reduction.
15
struct
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
22
size_t
forbidden_options
= 0;
23
size_t
dropped_edges
= 0;
// independent edges removed by normalization
24
};
25
26
/// A complete assignment of options to nodes.
27
struct
Solution
{
28
/// choices[n] is the option chosen for node `n`.
29
Choices
choices
;
30
/// Total cost of the assignment under the original graph.
31
Cost
cost
= 0;
32
/// Nodes whose choice hits a Cost::inf entry, see `conflicts`; empty iff feasible.
33
fe::Bitset
conflicts
;
34
Statistics
statistics
;
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>
64
solve_exhaustive
(
const
Graph
& graph,
size_t
max_combinations = 10'000'000,
unsigned
num_threads = 0);
65
66
}
// namespace pbqp
pbqp::Cost
A saturating cost; Cost::inf forbids an option or option pair.
Definition
graph.h:31
pbqp::Graph
A PBQP problem graph.
Definition
graph.h:252
graph.h
pbqp
Definition
dot.h:9
pbqp::Statistics::dropped_edges
size_t dropped_edges
Definition
solver.h:23
pbqp::Choices
fe::Vector< OptionId, 8 > Choices
One option per node, indexed by node id.
Definition
graph.h:68
pbqp::Statistics::rm
size_t rm
Definition
solver.h:20
pbqp::Statistics::single_option
size_t single_option
Definition
solver.h:21
pbqp::Statistics::r1
size_t r1
Definition
solver.h:17
pbqp::Statistics::forbidden_options
size_t forbidden_options
Definition
solver.h:22
pbqp::solve
Solution solve(const Graph &graph, fe::View< size_t > rn_priority={})
Heuristic PBQP solver.
Definition
solver.cpp:629
pbqp::evaluate
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
pbqp::Statistics::r2
size_t r2
Definition
solver.h:18
pbqp::conflicts
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
pbqp::Statistics::rn
size_t rn
Definition
solver.h:19
pbqp::solve_exhaustive
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
pbqp::Statistics::r0
size_t r0
Definition
solver.h:16
pbqp::Statistics
How often solve applied each reduction.
Definition
solver.h:15
pbqp::Solution
A complete assignment of options to nodes.
Definition
solver.h:27
pbqp::Solution::feasible
bool feasible() const
Definition
solver.h:36
pbqp::Solution::cost
Cost cost
Total cost of the assignment under the original graph.
Definition
solver.h:31
pbqp::Solution::conflicts
fe::Bitset conflicts
Nodes whose choice hits a Cost::inf entry, see conflicts; empty iff feasible.
Definition
solver.h:33
pbqp::Solution::choices
Choices choices
choices[n] is the option chosen for node n.
Definition
solver.h:29
pbqp::Solution::statistics
Statistics statistics
Definition
solver.h:34
include
pbqp
solver.h
Generated by
1.18.0