aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/CodeGen/PBQP/Solver.h
blob: ff148576a6e60f39d29eeac248c745277d2a8d0b (plain)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef RCC_CODEGEN_PBQP_SOLVER_H
#define RCC_CODEGEN_PBQP_SOLVER_H

#include "rcc/CodeGen/PBQP/ReductionRules.h"
#include "rcc/CodeGen/PBQP/Solution.h"

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <map>
#include <optional>
#include <set>
#include <tuple>
#include <utility>
#include <vector>

namespace rcc::pbqp {

class DeterministicSolver final {
public:
  [[nodiscard]] Expected<Solution> solve(const Graph &Problem) const {
    auto Verified = Problem.verify();
    if (!Verified)
      return std::unexpected(Verified.error());

    WorkingProblem Work(Problem);
    std::vector<ReductionRecord> Reductions;
    Reductions.reserve(Problem.nodeCount());
    while (Work.activeNodeCount() != 0) {
      const NodeID Selected = Work.selectReductionNode();
      const std::size_t Degree = Work.degree(Selected);
      Expected<ReductionRecord> Reduced = Degree == 0   ? Work.reduceDegreeZero(Selected)
                                          : Degree == 1 ? Work.reduceDegreeOne(Selected)
                                          : Degree == 2 ? Work.reduceDegreeTwo(Selected)
                                                        : Work.reduceHeuristically(Selected);
      if (!Reduced)
        return std::unexpected(Reduced.error());
      Reductions.push_back(std::move(*Reduced));
    }

    Solution Result(Problem.nodeCount());
    for (auto Position = Reductions.rbegin(); Position != Reductions.rend(); ++Position) {
      const ReductionRecord &Record = *Position;
      std::size_t Choice = Record.FixedSelection;
      if (Record.Kind != ReductionKind::Heuristic) {
        Cost Best = InfiniteCost;
        for (std::size_t Option = 0; Option < Record.NodeCosts.size(); ++Option) {
          Cost Candidate = Record.NodeCosts[Option];
          if (Record.Kind == ReductionKind::DegreeOne || Record.Kind == ReductionKind::DegreeTwo) {
            const auto Neighbor = Result.selection(Record.FirstNeighbor);
            if (!Neighbor)
              return makeUnexpected(ErrorCode::InternalError,
                                    "PBQP back-propagation encountered an unsolved neighbor");
            Candidate = addCosts(Candidate, Record.FirstCosts.at(Option, *Neighbor));
          }
          if (Record.Kind == ReductionKind::DegreeTwo) {
            const auto Neighbor = Result.selection(Record.SecondNeighbor);
            if (!Neighbor)
              return makeUnexpected(ErrorCode::InternalError,
                                    "PBQP back-propagation encountered an unsolved neighbor");
            Candidate = addCosts(Candidate, Record.SecondCosts.at(Option, *Neighbor));
          }
          if (Candidate < Best) {
            Best = Candidate;
            Choice = Option;
          }
        }
        if (isInfinite(Best))
          return makeUnexpected(ErrorCode::RegisterAllocationFailed,
                                "PBQP reductions produced no finite option");
      }
      auto Set = Result.setSelection(Record.Node, Choice);
      if (!Set)
        return std::unexpected(Set.error());
    }

    Cost Total = 0;
    for (const Node &NodeValue : Problem.nodes()) {
      const auto Selection = Result.selection(NodeValue.ID);
      if (!Selection) {
        return makeUnexpected(ErrorCode::InternalError,
                              "PBQP solver did not select a reduced node option");
      }
      Total = addCosts(Total, NodeValue.Costs[Selection.value()]);
    }
    for (const Edge &EdgeValue : Problem.edges()) {
      const auto FirstSelection = Result.selection(EdgeValue.First);
      const auto SecondSelection = Result.selection(EdgeValue.Second);
      if (!FirstSelection || !SecondSelection) {
        return makeUnexpected(ErrorCode::InternalError,
                              "PBQP solver did not select an edge endpoint option");
      }
      Total = addCosts(Total, EdgeValue.Costs.at(FirstSelection.value(), SecondSelection.value()));
    }
    if (isInfinite(Total))
      return makeUnexpected(ErrorCode::RegisterAllocationFailed,
                            "PBQP problem has no finite solution");
    Result.setTotalCost(Total);
    auto ResultVerification = Result.verify(Problem);
    if (!ResultVerification)
      return std::unexpected(ResultVerification.error());
    return Result;
  }

private:
  struct WorkingEdge final {
    NodeID First;
    NodeID Second;
    CostMatrix Costs;
  };

  class WorkingProblem final {
  public:
    explicit WorkingProblem(const Graph &Problem) {
      Costs.reserve(Problem.nodeCount());
      StableKeys.reserve(Problem.nodeCount());
      Active.assign(Problem.nodeCount(), true);
      for (const Node &NodeValue : Problem.nodes()) {
        Costs.push_back(NodeValue.Costs);
        StableKeys.push_back(NodeValue.StableKey);
      }
      for (const Edge &EdgeValue : Problem.edges())
        Edges.emplace(key(EdgeValue.First, EdgeValue.Second),
                      WorkingEdge{EdgeValue.First, EdgeValue.Second, EdgeValue.Costs});
      ActiveCount = Problem.nodeCount();
    }

    [[nodiscard]] std::size_t activeNodeCount() const noexcept {
      return ActiveCount;
    }

    [[nodiscard]] std::size_t degree(NodeID ID) const noexcept {
      std::size_t Result = 0;
      for (const auto &[EdgeKey, EdgeValue] : Edges) {
        (void)EdgeKey;
        if (Active[EdgeValue.First.value()] && Active[EdgeValue.Second.value()] &&
            (EdgeValue.First == ID || EdgeValue.Second == ID))
          ++Result;
      }
      return Result;
    }

    [[nodiscard]] NodeID selectReductionNode() const noexcept {
      std::optional<std::tuple<bool, std::size_t, std::uint64_t, std::uint32_t>> Best;
      NodeID Result;
      for (std::size_t Index = 0; Index < Active.size(); ++Index) {
        if (!Active[Index])
          continue;
        const NodeID ID = NodeID::fromValue(static_cast<std::uint32_t>(Index));
        const std::size_t Degree = degree(ID);
        const auto Rank = std::tuple{Degree > 2, Degree, StableKeys[Index], ID.value()};
        if (!Best || Rank < *Best) {
          Best = Rank;
          Result = ID;
        }
      }
      return Result;
    }

    [[nodiscard]] Expected<ReductionRecord> reduceDegreeZero(NodeID ID) {
      ReductionRecord Record;
      Record.Kind = ReductionKind::DegreeZero;
      Record.Node = ID;
      Record.NodeCosts = Costs[ID.value()];
      deactivate(ID);
      return Record;
    }

    [[nodiscard]] Expected<ReductionRecord> reduceDegreeOne(NodeID ID) {
      const std::vector<WorkingEdge> Incident = incidentEdges(ID);
      if (Incident.size() != 1)
        return makeUnexpected(ErrorCode::InternalError,
                              "PBQP degree-one reduction is inconsistent");
      const WorkingEdge &EdgeValue = Incident.front();
      const NodeID Neighbor = other(EdgeValue, ID);
      const CostMatrix Oriented = orientFrom(EdgeValue, ID);

      ReductionRecord Record;
      Record.Kind = ReductionKind::DegreeOne;
      Record.Node = ID;
      Record.FirstNeighbor = Neighbor;
      Record.NodeCosts = Costs[ID.value()];
      Record.FirstCosts = Oriented;

      for (std::size_t NeighborOption = 0; NeighborOption < Costs[Neighbor.value()].size();
           ++NeighborOption) {
        Cost Minimum = InfiniteCost;
        for (std::size_t Option = 0; Option < Costs[ID.value()].size(); ++Option) {
          Minimum = std::min(
              Minimum, addCosts(Costs[ID.value()][Option], Oriented.at(Option, NeighborOption)));
        }
        Costs[Neighbor.value()][NeighborOption] =
            addCosts(Costs[Neighbor.value()][NeighborOption], Minimum);
      }
      removeIncidentEdges(ID);
      deactivate(ID);
      return Record;
    }

    [[nodiscard]] Expected<ReductionRecord> reduceDegreeTwo(NodeID ID) {
      const std::vector<WorkingEdge> Incident = incidentEdges(ID);
      if (Incident.size() != 2)
        return makeUnexpected(ErrorCode::InternalError,
                              "PBQP degree-two reduction is inconsistent");
      NodeID First = other(Incident[0], ID);
      NodeID Second = other(Incident[1], ID);
      CostMatrix FirstCosts = orientFrom(Incident[0], ID);
      CostMatrix SecondCosts = orientFrom(Incident[1], ID);
      if (Second < First) {
        std::swap(First, Second);
        std::swap(FirstCosts, SecondCosts);
      }

      ReductionRecord Record;
      Record.Kind = ReductionKind::DegreeTwo;
      Record.Node = ID;
      Record.FirstNeighbor = First;
      Record.SecondNeighbor = Second;
      Record.NodeCosts = Costs[ID.value()];
      Record.FirstCosts = FirstCosts;
      Record.SecondCosts = SecondCosts;

      CostMatrix Delta(Costs[First.value()].size(), Costs[Second.value()].size(), InfiniteCost);
      for (std::size_t FirstOption = 0; FirstOption < Delta.rows(); ++FirstOption) {
        for (std::size_t SecondOption = 0; SecondOption < Delta.columns(); ++SecondOption) {
          for (std::size_t Option = 0; Option < Costs[ID.value()].size(); ++Option) {
            const Cost Candidate =
                addCosts(Costs[ID.value()][Option], addCosts(FirstCosts.at(Option, FirstOption),
                                                             SecondCosts.at(Option, SecondOption)));
            Delta.at(FirstOption, SecondOption) =
                std::min(Delta.at(FirstOption, SecondOption), Candidate);
          }
        }
      }
      removeIncidentEdges(ID);
      const auto Position = Edges.find(key(First, Second));
      if (Position == Edges.end())
        Edges.emplace(key(First, Second), WorkingEdge{First, Second, std::move(Delta)});
      else
        Position->second.Costs.add(Delta);
      deactivate(ID);
      return Record;
    }

    [[nodiscard]] Expected<ReductionRecord> reduceHeuristically(NodeID ID) {
      const std::vector<WorkingEdge> Incident = incidentEdges(ID);
      Cost Best = InfiniteCost;
      std::size_t Choice = 0;
      for (std::size_t Option = 0; Option < Costs[ID.value()].size(); ++Option) {
        Cost Candidate = Costs[ID.value()][Option];
        for (const WorkingEdge &EdgeValue : Incident) {
          const NodeID Neighbor = other(EdgeValue, ID);
          const CostMatrix Oriented = orientFrom(EdgeValue, ID);
          Cost NeighborMinimum = InfiniteCost;
          for (std::size_t NeighborOption = 0; NeighborOption < Costs[Neighbor.value()].size();
               ++NeighborOption) {
            NeighborMinimum = std::min(NeighborMinimum, Oriented.at(Option, NeighborOption));
          }
          Candidate = addCosts(Candidate, NeighborMinimum);
        }
        if (Candidate < Best) {
          Best = Candidate;
          Choice = Option;
        }
      }
      if (isInfinite(Best))
        return makeUnexpected(ErrorCode::RegisterAllocationFailed,
                              "PBQP heuristic reduction has no finite option");

      ReductionRecord Record;
      Record.Kind = ReductionKind::Heuristic;
      Record.Node = ID;
      Record.FixedSelection = Choice;
      Record.NodeCosts = Costs[ID.value()];
      for (const WorkingEdge &EdgeValue : Incident) {
        const NodeID Neighbor = other(EdgeValue, ID);
        const CostMatrix Oriented = orientFrom(EdgeValue, ID);
        for (std::size_t Option = 0; Option < Costs[Neighbor.value()].size(); ++Option) {
          Costs[Neighbor.value()][Option] =
              addCosts(Costs[Neighbor.value()][Option], Oriented.at(Choice, Option));
        }
      }
      removeIncidentEdges(ID);
      deactivate(ID);
      return Record;
    }

  private:
    using EdgeKey = std::pair<std::uint32_t, std::uint32_t>;

    [[nodiscard]] static EdgeKey key(NodeID First, NodeID Second) noexcept {
      if (Second < First)
        std::swap(First, Second);
      return {First.value(), Second.value()};
    }

    [[nodiscard]] static NodeID other(const WorkingEdge &EdgeValue, NodeID ID) noexcept {
      return EdgeValue.First == ID ? EdgeValue.Second : EdgeValue.First;
    }

    [[nodiscard]] static CostMatrix orientFrom(const WorkingEdge &EdgeValue, NodeID ID) {
      return EdgeValue.First == ID ? EdgeValue.Costs : EdgeValue.Costs.transpose();
    }

    [[nodiscard]] std::vector<WorkingEdge> incidentEdges(NodeID ID) const {
      std::vector<WorkingEdge> Result;
      for (const auto &[EdgeKeyValue, EdgeValue] : Edges) {
        (void)EdgeKeyValue;
        if (Active[EdgeValue.First.value()] && Active[EdgeValue.Second.value()] &&
            (EdgeValue.First == ID || EdgeValue.Second == ID))
          Result.push_back(EdgeValue);
      }
      return Result;
    }

    void removeIncidentEdges(NodeID ID) {
      for (auto Position = Edges.begin(); Position != Edges.end();) {
        if (Position->second.First == ID || Position->second.Second == ID)
          Position = Edges.erase(Position);
        else
          ++Position;
      }
    }

    void deactivate(NodeID ID) noexcept {
      Active[ID.value()] = false;
      --ActiveCount;
    }

    std::vector<CostVector> Costs;
    std::vector<std::uint64_t> StableKeys;
    std::vector<bool> Active;
    std::size_t ActiveCount = 0;
    std::map<EdgeKey, WorkingEdge> Edges;
  };
};

} // namespace rcc::pbqp

#endif // RCC_CODEGEN_PBQP_SOLVER_H