-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathorbit_graph_processor.cpp
More file actions
304 lines (243 loc) · 12.8 KB
/
orbit_graph_processor.cpp
File metadata and controls
304 lines (243 loc) · 12.8 KB
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
/*
Copyright 2024 Huawei Technologies Co., Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
@author Toni Boehnlein, Benjamin Lozes, Pal Andras Papp, Raphael S. Steiner
*/
#define BOOST_TEST_MODULE OrbitGraphProcessor
#include <boost/test/unit_test.hpp>
#include "test_utils.hpp"
#include "test_graphs.hpp"
#include "osp/auxiliary/io/hdag_graph_file_reader.hpp"
#include "osp/auxiliary/io/dot_graph_file_reader.hpp"
#include "osp/dag_divider/isomorphism_divider/MerkleHashComputer.hpp"
#include "osp/auxiliary/io/DotFileWriter.hpp"
#include "osp/dag_divider/isomorphism_divider/OrbitGraphProcessor.hpp"
#include "osp/graph_algorithms/directed_graph_util.hpp"
#include "osp/graph_implementations/adj_list_impl/computational_dag_vector_impl.hpp"
#include <filesystem>
#include <iostream>
#include <set>
using namespace osp;
using graph_t = computational_dag_vector_impl_def_t;
template <typename Graph_t>
void check_partitioning(const Graph_t& dag, const OrbitGraphProcessor<Graph_t, Graph_t>& processor) {
const auto& final_coarse_graph = processor.get_final_coarse_graph();
const auto& final_groups = processor.get_final_groups();
// Check that the final coarse graph is acyclic
BOOST_CHECK(is_acyclic(final_coarse_graph));
// Check that the final groups form a valid partition of the original DAG's vertices
std::vector<int> vertex_counts(dag.num_vertices(), 0);
size_t total_vertices_in_groups = 0;
for (const auto& group : final_groups) {
for (const auto& subgraph : group.subgraphs) {
total_vertices_in_groups += subgraph.size();
for (const auto& vertex : subgraph) {
BOOST_REQUIRE_LT(vertex, dag.num_vertices());
vertex_counts[vertex]++;
}
}
}
BOOST_CHECK_EQUAL(total_vertices_in_groups, dag.num_vertices());
for (size_t i = 0; i < dag.num_vertices(); ++i) {
BOOST_CHECK_EQUAL(vertex_counts[i], 1);
}
}
// BOOST_AUTO_TEST_CASE(OrbitGraphProcessor_SmokeTest) {
// // The test reads a file, but the path is absolute, so we don't need the project root here.
// graph_t dag;
// file_reader::readComputationalDagDotFormat("", dag);
// OrbitGraphProcessor<graph_t, graph_t> processor(2); // Using a symmetry threshold of 2
// MerkleHashComputer<graph_t, bwd_merkle_node_hash_func<graph_t>, true> hasher(dag, dag);
// processor.discover_isomorphic_groups(dag, hasher);
// const auto& coarse_graph = processor.get_coarse_graph();
// const auto& final_coarse_graph = processor.get_final_coarse_graph();
// const auto& final_groups = processor.get_final_groups();
// const auto& final_contraction_map = processor.get_final_contraction_map();
// DotFileWriter writer;
// // Color by initial orbits
// writer.write_colored_graph("orbit_graph_orbits_colored.dot", dag, processor.get_contraction_map());
// writer.write_graph("orbit_graph_coarse_graph.dot", coarse_graph);
// // Color by final merged groups
// writer.write_colored_graph("orbit_graph_groups_colored.dot", dag, final_contraction_map);
// // Color by final subgraphs (each subgraph gets a unique color)
// std::vector<unsigned> subgraph_colors(dag.num_vertices());
// unsigned current_subgraph_color = 0;
// for (const auto& group : final_groups) {
// for (const auto& subgraph : group.subgraphs) {
// for (const auto& vertex : subgraph) {
// subgraph_colors[vertex] = current_subgraph_color;
// }
// current_subgraph_color++;
// }
// }
// writer.write_colored_graph("orbit_graph_subgraphs_colored.dot", dag, subgraph_colors);
// writer.write_graph("orbit_graph_final_coarse_graph.dot", final_coarse_graph);
// BOOST_CHECK_GT(coarse_graph.num_vertices(), 0);
// BOOST_CHECK_LT(coarse_graph.num_vertices(), dag.num_vertices());
// BOOST_CHECK_GT(final_coarse_graph.num_vertices(), 0);
// BOOST_CHECK_LE(final_coarse_graph.num_vertices(), coarse_graph.num_vertices());
// check_partitioning(dag, processor);
// }
BOOST_AUTO_TEST_CASE(OrbitGraphProcessor_SimpleMerge) {
graph_t dag;
// Two parallel pipelines that are structurally identical
// 0 -> 1
// 2 -> 3
dag.add_vertex(10, 1, 1); // 0
dag.add_vertex(10, 1, 1); // 1
dag.add_vertex(10, 1, 1); // 2
dag.add_vertex(10, 1, 1); // 3
dag.add_edge(0, 1);
dag.add_edge(2, 3);
// Initial orbits: {0, 2} and {1, 3}. Coarse graph: 0 -> 1
// With threshold 2, these should be merged.
OrbitGraphProcessor<graph_t, graph_t> processor;
MerkleHashComputer<graph_t, bwd_merkle_node_hash_func<graph_t>, true> hasher(dag, dag);
processor.discover_isomorphic_groups(dag, hasher);
const auto& final_coarse_graph = processor.get_final_coarse_graph();
const auto& final_groups = processor.get_final_groups();
// Expect a single node in the final coarse graph
BOOST_CHECK_EQUAL(final_coarse_graph.num_vertices(), 1);
BOOST_CHECK_EQUAL(final_groups.size(), 1);
// The single group should contain two subgraphs: {0,1} and {2,3}
BOOST_REQUIRE_EQUAL(final_groups[0].subgraphs.size(), 2);
std::set<int> sg1(final_groups[0].subgraphs[0].begin(), final_groups[0].subgraphs[0].end());
std::set<int> sg2(final_groups[0].subgraphs[1].begin(), final_groups[0].subgraphs[1].end());
std::set<int> expected_sgA = {0, 1};
std::set<int> expected_sgB = {2, 3};
BOOST_CHECK((sg1 == expected_sgA && sg2 == expected_sgB) || (sg1 == expected_sgB && sg2 == expected_sgA));
check_partitioning(dag, processor);
}
BOOST_AUTO_TEST_CASE(OrbitGraphProcessor_ForkJoinNoMerge) {
graph_t dag;
// 0 -> {1, 2} -> 3. Nodes 1 and 2 are in the same orbit.
dag.add_vertex(10, 1, 1); // 0
dag.add_vertex(20, 1, 1); // 1
dag.add_vertex(20, 1, 1); // 2
dag.add_vertex(30, 1, 1); // 3
dag.add_edge(0, 1);
dag.add_edge(0, 2);
dag.add_edge(1, 3);
dag.add_edge(2, 3);
// Initial orbits: {0}, {1,2}, {3}. Coarse graph: 0 -> 1 -> 2
// Merging 0 and 1 would result in a group of size 1 ({0,1,2}), which is not viable (threshold 2).
// Merging 1 and 2 would also result in a group of size 1 ({1,2,3}), not viable.
OrbitGraphProcessor<graph_t, graph_t> processor;
MerkleHashComputer<graph_t, bwd_merkle_node_hash_func<graph_t>, true> hasher(dag, dag);
processor.discover_isomorphic_groups(dag, hasher);
const auto& final_coarse_graph = processor.get_final_coarse_graph();
const auto& final_groups = processor.get_final_groups();
// Expect no merges, so final graph is same as initial coarse graph.
BOOST_CHECK_EQUAL(final_coarse_graph.num_vertices(), 3);
BOOST_CHECK_EQUAL(final_groups.size(), 3);
// Check group structures
// Group 0: {{0}}
// Group 1: {{1}, {2}}
// Group 2: {{3}}
size_t group_of_1_count = 0;
size_t group_of_2_count = 0;
for(const auto& group : final_groups) {
if (group.subgraphs.size() == 1) group_of_1_count++;
if (group.subgraphs.size() == 2) group_of_2_count++;
}
BOOST_CHECK_EQUAL(group_of_1_count, 2);
BOOST_CHECK_EQUAL(group_of_2_count, 1);
check_partitioning(dag, processor);
}
BOOST_AUTO_TEST_CASE(OrbitGraphProcessor_PartitionCheck_MediumGraph) {
const auto project_root = get_project_root();
graph_t dag;
file_reader::readComputationalDagHyperdagFormatDB((project_root / "data/spaa/tiny/instance_bicgstab.hdag").string(), dag);
BOOST_REQUIRE_GT(dag.num_vertices(), 0);
// Use a higher threshold to encourage more merging on this larger graph
OrbitGraphProcessor<graph_t, graph_t> processor;
MerkleHashComputer<graph_t, bwd_merkle_node_hash_func<graph_t>, true> hasher(dag, dag);
processor.discover_isomorphic_groups(dag, hasher);
// The main purpose of this test is to ensure the output is a valid partition.
check_partitioning(dag, processor);
}
BOOST_AUTO_TEST_CASE(OrbitGraphProcessor_MultiPipelineMerge) {
// 5 parallel pipelines of 4 nodes each.
// Initial orbits: 4 groups of 5 identical nodes. Coarse graph: 0->1->2->3
// With a threshold of 5, the entire graph should merge into a single group.
const auto dag = construct_multi_pipeline_dag<graph_t>(5, 4);
BOOST_REQUIRE_EQUAL(dag.num_vertices(), 20);
OrbitGraphProcessor<graph_t, graph_t> processor; // Set threshold to match pipeline count
MerkleHashComputer<graph_t, bwd_merkle_node_hash_func<graph_t>, true> hasher(dag, dag);
processor.discover_isomorphic_groups(dag, hasher);
const auto& final_coarse_graph = processor.get_final_coarse_graph();
const auto& final_groups = processor.get_final_groups();
// Expect a single node in the final coarse graph
BOOST_CHECK_EQUAL(final_coarse_graph.num_vertices(), 1);
BOOST_CHECK_EQUAL(final_groups.size(), 1);
// The single group should contain 5 subgraphs, each with 4 nodes.
BOOST_REQUIRE_EQUAL(final_groups[0].subgraphs.size(), 5);
BOOST_CHECK_EQUAL(final_groups[0].subgraphs[0].size(), 4);
check_partitioning(dag, processor);
}
BOOST_AUTO_TEST_CASE(OrbitGraphProcessor_LadderNoMerge) {
// A ladder graph with 10 rungs (22 nodes).
// The bwd_merkle_hash is more discerning and creates more than 2 initial orbits
// due to the different structures at the start and end of the ladder.
// The coarsening logic will merge some of these, but the core cyclic structure
// prevents a full merge. The exact number of final nodes is non-trivial,
// but it should be greater than 1.
const auto dag = construct_ladder_dag<graph_t>(10);
BOOST_REQUIRE_EQUAL(dag.num_vertices(), 22);
OrbitGraphProcessor<graph_t, graph_t> processor;
MerkleHashComputer<graph_t, bwd_merkle_node_hash_func<graph_t>, true> hasher(dag, dag);
processor.discover_isomorphic_groups(dag, hasher);
const auto& initial_coarse_graph = processor.get_coarse_graph();
const auto& final_coarse_graph = processor.get_final_coarse_graph();
// Expect no merges, so final graph is the same as the initial coarse graph.
BOOST_CHECK_EQUAL(final_coarse_graph.num_vertices(), initial_coarse_graph.num_vertices());
BOOST_CHECK_GT(final_coarse_graph.num_vertices(), 1);
check_partitioning(dag, processor);
}
BOOST_AUTO_TEST_CASE(OrbitGraphProcessor_AsymmetricNoMerge) {
// A simple chain where every node is unique.
// Since all groups are below the threshold, they will all be merged into one.
const auto dag = construct_asymmetric_dag<graph_t>(30);
BOOST_REQUIRE_EQUAL(dag.num_vertices(), 30);
OrbitGraphProcessor<graph_t, graph_t> processor;
MerkleHashComputer<graph_t, bwd_merkle_node_hash_func<graph_t>, true> hasher(dag, dag);
processor.discover_isomorphic_groups(dag, hasher);
const auto& final_coarse_graph = processor.get_final_coarse_graph();
// Expect all nodes to be merged into a single coarse node.
BOOST_CHECK_EQUAL(final_coarse_graph.num_vertices(), 1);
check_partitioning(dag, processor);
}
BOOST_AUTO_TEST_CASE(OrbitGraphProcessor_BinaryTreeNoMerge) {
// A binary out-tree of height 4.
// Initial orbits are one per level. Coarse graph is a simple chain: 0->1->2->3->4 (5 nodes).
// The logic allows merging groups that are below the symmetry threshold.
// However, the `critical_path_weight` check prevents merges that would increase the
// longest path in the coarse graph. This results in the chain being partially, but not
// fully, collapsed. The expected outcome is 2 final coarse nodes.
const auto dag = construct_binary_out_tree<graph_t>(4);
BOOST_REQUIRE_EQUAL(dag.num_vertices(), (1 << 5) - 1);
OrbitGraphProcessor<graph_t, graph_t> processor;
MerkleHashComputer<graph_t, bwd_merkle_node_hash_func<graph_t>, true> hasher(dag, dag);
processor.discover_isomorphic_groups(dag, hasher);
const auto& final_coarse_graph = processor.get_final_coarse_graph();
BOOST_CHECK_EQUAL(final_coarse_graph.num_vertices(), 3);
check_partitioning(dag, processor);
}
BOOST_AUTO_TEST_CASE(OrbitGraphProcessor_ButterflyMerge) {
const auto dag = construct_butterfly_dag<graph_t>(3);
BOOST_REQUIRE_EQUAL(dag.num_vertices(), (3 + 1) * 8);
OrbitGraphProcessor<graph_t, graph_t> processor;
MerkleHashComputer<graph_t, bwd_merkle_node_hash_func<graph_t>, true> hasher(dag, dag);
processor.discover_isomorphic_groups(dag, hasher);
const auto& final_coarse_graph = processor.get_final_coarse_graph();
BOOST_CHECK_EQUAL(final_coarse_graph.num_vertices(), 4);
check_partitioning(dag, processor);
}