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
|
#ifndef LLVM_CODEGEN_PBQP_GRAPHGENERATOR_H
#define LLVM_CODEGEN_PBQP_GRAPHGENERATOR_H
#include "PBQPMath.h"
namespace PBQP {
unsigned randRange(unsigned min, unsigned max) {
return min + (rand() % (max - min + 1));
}
class BasicNodeCostsGenerator {
private:
unsigned maxDegree, minCost, maxCost;
public:
BasicNodeCostsGenerator(unsigned maxDegree, unsigned minCost,
unsigned maxCost) :
maxDegree(maxDegree), minCost(minCost), maxCost(maxCost) { }
Vector operator()() const {
Vector v(randRange(1, maxDegree));
for (unsigned i = 0; i < v.getLength(); ++i) {
v[i] = randRange(minCost, maxCost);
}
return v;
};
};
class FixedDegreeSpillCostGenerator {
private:
unsigned degree, spillCostMin, spillCostMax;
public:
FixedDegreeSpillCostGenerator(unsigned degree, unsigned spillCostMin,
unsigned spillCostMax) :
degree(degree), spillCostMin(spillCostMin), spillCostMax(spillCostMax) { }
Vector operator()() const {
Vector v(degree, 0);
v[0] = randRange(spillCostMin, spillCostMax);
return v;
}
};
class BasicEdgeCostsGenerator {
private:
unsigned minCost, maxCost;
public:
BasicEdgeCostsGenerator(unsigned minCost, unsigned maxCost) :
minCost(minCost), maxCost(maxCost) {}
Matrix operator()(const SimpleGraph &g,
const SimpleGraph::ConstNodeIterator &n1,
const SimpleGraph::ConstNodeIterator &n2) const {
Matrix m(g.getNodeCosts(n1).getLength(),
g.getNodeCosts(n2).getLength());
for (unsigned i = 0; i < m.getRows(); ++i) {
for (unsigned j = 0; j < m.getCols(); ++j) {
m[i][j] = randRange(minCost, maxCost);
}
}
return m;
}
};
class InterferenceCostsGenerator {
public:
Matrix operator()(const SimpleGraph &g,
const SimpleGraph::ConstNodeIterator &n1,
const SimpleGraph::ConstNodeIterator &n2) const {
unsigned len = g.getNodeCosts(n1).getLength();
assert(len == g.getNodeCosts(n2).getLength());
Matrix m(len, len);
m[0][0] = 0;
for (unsigned i = 1; i < len; ++i) {
m[i][i] = std::numeric_limits<PBQPNum>::infinity();
}
return m;
}
};
class RingEdgeGenerator {
public:
template <typename EdgeCostsGenerator>
void operator()(SimpleGraph &g, EdgeCostsGenerator &edgeCostsGen) {
assert(g.areNodeIDsValid() && "Graph must have valid node IDs.");
if (g.getNumNodes() < 2)
return;
if (g.getNumNodes() == 2) {
SimpleGraph::NodeIterator n1 = g.getNodeItr(0),
n2 = g.getNodeItr(1);
g.addEdge(n1, n2, edgeCostsGen(g, n1, n2));
return;
}
// Else |V| > 2:
for (unsigned i = 0; i < g.getNumNodes(); ++i) {
SimpleGraph::NodeIterator
n1 = g.getNodeItr(i),
n2 = g.getNodeItr((i + 1) % g.getNumNodes());
g.addEdge(n1, n2, edgeCostsGen(g, n1, n2));
}
}
};
class FullyConnectedEdgeGenerator {
public:
template <typename EdgeCostsGenerator>
void operator()(SimpleGraph &g, EdgeCostsGenerator &edgeCostsGen) {
assert(g.areNodeIDsValid() && "Graph must have valid node IDs.");
for (unsigned i = 0; i < g.getNumNodes(); ++i) {
for (unsigned j = i + 1; j < g.getNumNodes(); ++j) {
SimpleGraph::NodeIterator
n1 = g.getNodeItr(i),
n2 = g.getNodeItr(j);
g.addEdge(n1, n2, edgeCostsGen(g, n1, n2));
}
}
}
};
class RandomEdgeGenerator {
public:
template <typename EdgeCostsGenerator>
void operator()(SimpleGraph &g, EdgeCostsGenerator &edgeCostsGen) {
assert(g.areNodeIDsValid() && "Graph must have valid node IDs.");
for (unsigned i = 0; i < g.getNumNodes(); ++i) {
for (unsigned j = i + 1; j < g.getNumNodes(); ++j) {
if (rand() % 2 == 0) {
SimpleGraph::NodeIterator
n1 = g.getNodeItr(i),
n2 = g.getNodeItr(j);
g.addEdge(n1, n2, edgeCostsGen(g, n1, n2));
}
}
}
}
};
template <typename NodeCostsGenerator,
typename EdgesGenerator,
typename EdgeCostsGenerator>
SimpleGraph createRandomGraph(unsigned numNodes,
NodeCostsGenerator nodeCostsGen,
EdgesGenerator edgeGen,
EdgeCostsGenerator edgeCostsGen) {
SimpleGraph g;
for (unsigned n = 0; n < numNodes; ++n) {
g.addNode(nodeCostsGen());
}
g.assignNodeIDs();
edgeGen(g, edgeCostsGen);
return g;
}
}
#endif // LLVM_CODEGEN_PBQP_GRAPHGENERATOR_H
|