MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
pbqp Namespace Reference

Namespaces

namespace  json

Classes

class  BasicMatrix
 A view of a cost matrix over row-major storage owned elsewhere; transposed() views the same storage column-major. More...
class  Cost
 A saturating cost; Cost::inf forbids an option or option pair. More...
class  Graph
 A PBQP problem graph. More...
struct  Instance
 A graph together with the optional rn_priority for solve. More...
class  Matrix
 An owning row-major cost matrix; it views its own storage, so writes go through span(). More...
struct  Solution
 A complete assignment of options to nodes. More...
struct  Statistics
 How often solve applied each reduction. More...

Typedefs

using MatrixSpan = BasicMatrix<Cost>
using MatrixView = BasicMatrix<const Cost>
Ids

Node and edge ids are dense and stable: nothing is ever removed from a Graph.

using NodeId = size_t
 Identifier of a node in a Graph.
using EdgeId = size_t
 Identifier of an edge in a Graph.
using OptionId = size_t
 Index of an option within a node's cost vector.
PBQP Vectors & Matrices

PBQP vectors and matrices are small, so they live inline; a Matrix views such storage row-major.

using Costs = fe::Vector<Cost, 4>
using Edges = fe::Vector<EdgeId, 4>
using Choices = fe::Vector<OptionId, 8>
 One option per node, indexed by node id.

Functions

void write_dot (std::ostream &out, const Graph &graph, fe::View< OptionId > choices={})
 Write graph in Graphviz DOT format with cost vectors and matrices as HTML-like labels.
void read_json (std::istream &in, Instance &instance, const std::filesystem::path &path="<input>")
 Read a PBQP instance into the empty instance:
void write_json (std::ostream &out, const Graph &graph, fe::View< size_t > rn_priority={})
 Write the format that read_json reads.
Cost evaluate (const Graph &graph, fe::View< OptionId > choices)
 All solver functions are safe to call concurrently on a graph that nobody mutates meanwhile.
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, in an incident matrix.
Solution solve (const Graph &graph, fe::View< size_t > rn_priority={})
 Heuristic PBQP solver.
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.
std::ostream & operator<< (std::ostream &out, Cost cost)

Class Documentation

◆ pbqp::Instance

struct pbqp::Instance

A graph together with the optional rn_priority for solve.

Definition at line 15 of file json.h.

Class Members
Graph graph
Vector< size_t > rn_priority Empty, or one entry per node id.

◆ pbqp::Statistics

struct pbqp::Statistics

How often solve applied each reduction.

Definition at line 15 of file solver.h.

Class Members
size_t dropped_edges = 0
size_t forbidden_options = 0
size_t r0 = 0
size_t r1 = 0
size_t r2 = 0
size_t rm = 0
size_t rn = 0
size_t single_option = 0

Typedef Documentation

◆ Choices

using pbqp::Choices = fe::Vector<OptionId, 8>

One option per node, indexed by node id.

Definition at line 68 of file graph.h.

◆ Costs

using pbqp::Costs = fe::Vector<Cost, 4>

Definition at line 66 of file graph.h.

◆ EdgeId

using pbqp::EdgeId = size_t

Identifier of an edge in a Graph.

Definition at line 26 of file graph.h.

◆ Edges

using pbqp::Edges = fe::Vector<EdgeId, 4>

Definition at line 67 of file graph.h.

◆ MatrixSpan

Definition at line 185 of file graph.h.

◆ MatrixView

Definition at line 186 of file graph.h.

◆ NodeId

using pbqp::NodeId = size_t

Identifier of a node in a Graph.

Definition at line 25 of file graph.h.

◆ OptionId

using pbqp::OptionId = size_t

Index of an option within a node's cost vector.

Definition at line 27 of file graph.h.

Function Documentation

◆ conflicts()

fe::Bitset pbqp::conflicts ( const Graph & graph,
fe::View< OptionId > choices )
nodiscard

Nodes whose choice is Cost::inf in their cost vector or, given the neighbour's choice, in an incident matrix.

These are the candidates to spill or split when an assignment is infeasible. Requires a valid option for every node.

Definition at line 607 of file solver.cpp.

References conflicts(), pbqp::Graph::edge(), pbqp::Graph::edge_matrix(), pbqp::Graph::edges(), pbqp::Graph::node_costs(), and pbqp::Graph::nodes().

Referenced by conflicts().

◆ evaluate()

Cost pbqp::evaluate ( const Graph & graph,
fe::View< OptionId > choices )
nodiscard

All solver functions are safe to call concurrently on a graph that nobody mutates meanwhile.

Cost of choices under graph; Cost::inf if it picks a Cost::inf entry or the total saturates. Requires a valid option for every node.

Definition at line 591 of file solver.cpp.

References pbqp::Graph::edge(), pbqp::Graph::edge_matrix(), pbqp::Graph::edges(), evaluate(), pbqp::Graph::node_costs(), and pbqp::Graph::nodes().

Referenced by evaluate(), and solve().

◆ operator<<()

std::ostream & pbqp::operator<< ( std::ostream & out,
Cost cost )

Definition at line 10 of file graph.cpp.

◆ read_json()

void pbqp::read_json ( std::istream & in,
Instance & instance,
const std::filesystem::path & path = "<input>" )

Read a PBQP instance into the empty instance:

{
  "nodes": [{"costs": [0, "inf"]}, {"costs": [7, 0, 2]}],
  "edges": [{"src": 1, "dst": 0, "rows": 3, "cols": 2, "costs": [1, "inf", -5, 0, 4, 2]}],
  "rn_priority": [5, 9]
}

The position of a node in nodes is its id. Edge costs are row-major with src as the row; rows and cols are optional and only checked. "inf" is Cost::inf, rn_priority is optional, and unknown keys are skipped. Throws fe::Error::Bail, a std::exception, on malformed input; path only names the input in its message.

Definition at line 440 of file json.cpp.

References read_json().

Referenced by read_json().

◆ solve()

Solution pbqp::solve ( const Graph & graph,
fe::View< size_t > rn_priority = {} )
nodiscard

Heuristic PBQP solver.

Reduces degree 0, 1 (RI) and 2 (RII) nodes exactly and higher-degree nodes with the RN heuristic. Normalization keeps intermediate costs relative, so they saturate only where option costs differ by Cost::inf. Short of that, the result is optimal whenever no RN step is needed, e.g. on trees. RN decides the node with the highest rn_priority[n] first; ties go to the highest degree, then the widest cost spread. SSA-based register allocation passes PEO positions, so coloring cannot fail (Buchwald et al. 2011). Requires rn_priority to be empty or to cover every node id.

Definition at line 629 of file solver.cpp.

References evaluate(), pbqp::Graph::npos, pbqp::Graph::num_nodes(), and solve().

Referenced by solve().

◆ solve_exhaustive()

std::optional< Solution > pbqp::solve_exhaustive ( const Graph & graph,
size_t max_combinations = 10'000'000,
unsigned num_threads = 0 )
nodiscard

Brute-force optimum, useful to cross-check small instances.

The search space is split across num_threads threads (0 = hardware concurrency). Returns nothing if there are more than max_combinations candidate assignments.

Definition at line 691 of file solver.cpp.

References pbqp::Solution::cost, and solve_exhaustive().

Referenced by solve_exhaustive().

◆ write_dot()

void pbqp::write_dot ( std::ostream & out,
const Graph & graph,
fe::View< OptionId > choices = {} )

Write graph in Graphviz DOT format with cost vectors and matrices as HTML-like labels.

Edge matrices are titled src × dst, i.e. rows × columns. Highlights the entries picked by choices, which must be empty or cover every node id.

Definition at line 23 of file dot.cpp.

References pbqp::BasicMatrix< T >::cols(), pbqp::Graph::edge(), pbqp::Graph::edge_matrix(), pbqp::Graph::edges(), pbqp::Graph::node_costs(), pbqp::Graph::nodes(), pbqp::Graph::num_nodes(), pbqp::BasicMatrix< T >::rows(), and write_dot().

Referenced by write_dot().

◆ write_json()

void pbqp::write_json ( std::ostream & out,
const Graph & graph,
fe::View< size_t > rn_priority = {} )