Skip to content

Commit cdd97ad

Browse files
tests and fixes for subgraph implementation
1 parent 518a6b1 commit cdd97ad

3 files changed

Lines changed: 111 additions & 4 deletions

File tree

include/osp/graph_algorithms/specialised_graph_algorithms/subgraph_algorithms.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ limitations under the License.
1818

1919
#pragma once
2020

21+
#include "osp/concepts/graph_traits.hpp"
22+
#include "osp/graph_algorithms/directed_graph_top_sort.hpp"
2123
#include "osp/graph_algorithms/subgraph_algorithms.hpp"
2224
#include "osp/graph_implementations/adj_list_impl/compact_sparse_graph.hpp"
2325

@@ -32,13 +34,13 @@ std::unordered_map<vertex_idx_t<Graph_t_in>, vertex_idx_t<Graph_t_in>> create_in
3234
static_assert(std::is_same_v<vertex_idx_t<Graph_t_in>, vertex_idx_t<Graph_t_out>>,
3335
"Graph_t_in and out must have the same vertex_idx types");
3436

35-
const std::vector<vertex_idx_t<Graph_t_in>> topOrder = GetTopOrder(instance.getComputationalDag());
37+
const std::vector<vertex_idx_t<Graph_t_in>> topOrder = GetTopOrder(dag);
3638
std::vector<vertex_idx_t<Graph_t_in>> topOrderPosition(topOrder.size());
37-
for (vertex_idx_t<Graph_t_in> pos = 0; pos < dag.numbernum_vertices(); ++pos) {
39+
for (vertex_idx_t<Graph_t_in> pos = 0; pos < dag.num_vertices(); ++pos) {
3840
topOrderPosition[topOrder[pos]] = pos;
3941
}
4042

41-
auto topCmp = [&topOrderPosition](const &vertex_idx_t<Graph_t_in> lhs, const &vertex_idx_t<Graph_t_in> rhs) { return topOrderPosition[lhs] < topOrderPosition[rhs]; };
43+
auto topCmp = [&topOrderPosition](const vertex_idx_t<Graph_t_in> &lhs, const vertex_idx_t<Graph_t_in> &rhs) { return topOrderPosition[lhs] < topOrderPosition[rhs]; };
4244

4345
std::set<vertex_idx_t<Graph_t_in>, decltype(topCmp)> selectedVerticesOrdered(selected_nodes.begin(), selected_nodes.end(), topCmp);
4446

@@ -54,7 +56,7 @@ std::unordered_map<vertex_idx_t<Graph_t_in>, vertex_idx_t<Graph_t_in>> create_in
5456
for (const auto &node : selectedVerticesOrdered) {
5557
for (const auto &chld : dag.children(node)) {
5658
if (selectedVerticesOrdered.find(chld) != selectedVerticesOrdered.end()) {
57-
edges.emplace(node, chld);
59+
edges.emplace_back(local_idx.at(node), local_idx.at(chld));
5860
}
5961
}
6062
}

tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ _add_test( maxbsp_converter_and_hc )
162162

163163
_add_test( cost_evaluation )
164164

165+
_add_test( subgraph )
166+
165167
## pebbling ILPs
166168

167169
if (COPT_FOUND)

tests/subgraph.cpp

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
Copyright 2024 Huawei Technologies Co., Ltd.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
@author Toni Boehnlein, Pal Andras Papp, Raphael S. Steiner, Christos Konstantinos Matzoros
17+
*/
18+
19+
#define BOOST_TEST_MODULE SubGraphs
20+
#include <boost/test/unit_test.hpp>
21+
22+
#include "osp/graph_algorithms/specialised_graph_algorithms/subgraph_algorithms.hpp"
23+
#include "osp/graph_implementations/adj_list_impl/cdag_vertex_impl.hpp"
24+
#include "osp/graph_implementations/adj_list_impl/computational_dag_vector_impl.hpp"
25+
26+
using namespace osp;
27+
28+
BOOST_AUTO_TEST_CASE(SubGraphCompactSparseGraph) {
29+
const std::vector<std::pair<std::size_t, std::size_t>> edges({{0, 1}, {2, 3}, {6, 10}, {7, 9}, {0, 2}, {4, 6}, {1, 6}, {6, 7}, {5, 6}, {3, 7}, {1, 2}});
30+
Compact_Sparse_Graph<true, true, true, true, true> graph(11, edges);
31+
Compact_Sparse_Graph<true, true, true, true, true> subGraph;
32+
33+
unsigned cntr = 0;
34+
for (const auto &vert : graph.vertices()) {
35+
graph.set_vertex_work_weight(vert, cntr++);
36+
graph.set_vertex_comm_weight(vert, cntr++);
37+
graph.set_vertex_mem_weight(vert, cntr++);
38+
graph.set_vertex_type(vert, cntr++);
39+
}
40+
41+
const std::vector<vertex_idx_t<Compact_Sparse_Graph<true, true, true, true, true>>> selectVert({2, 3, 10, 6, 7});
42+
const auto vertCorrespondence = create_induced_subgraph_map(graph, subGraph, selectVert);
43+
BOOST_CHECK_EQUAL(subGraph.num_vertices(), selectVert.size());
44+
BOOST_CHECK_EQUAL(subGraph.num_edges(), 4);
45+
46+
for (const auto &vert : selectVert) {
47+
BOOST_CHECK_LT(vertCorrespondence.at(vert), selectVert.size());
48+
49+
for (const auto &otherVert : selectVert) {
50+
if (vertCorrespondence.at(vert) == vertCorrespondence.at(otherVert)) {
51+
BOOST_CHECK_EQUAL(vert, otherVert);
52+
}
53+
}
54+
}
55+
56+
for (const auto &vert : selectVert) {
57+
BOOST_CHECK_EQUAL(graph.vertex_work_weight(vert), subGraph.vertex_work_weight(vertCorrespondence.at(vert)));
58+
BOOST_CHECK_EQUAL(graph.vertex_comm_weight(vert), subGraph.vertex_comm_weight(vertCorrespondence.at(vert)));
59+
BOOST_CHECK_EQUAL(graph.vertex_mem_weight(vert), subGraph.vertex_mem_weight(vertCorrespondence.at(vert)));
60+
BOOST_CHECK_EQUAL(graph.vertex_type(vert), subGraph.vertex_type(vertCorrespondence.at(vert)));
61+
}
62+
}
63+
64+
BOOST_AUTO_TEST_CASE(SubGraphDagVectorImpl) {
65+
using v_impl = cdag_vertex_impl<std::size_t, unsigned, unsigned, unsigned, unsigned>;
66+
67+
computational_dag_vector_impl<v_impl> graph;
68+
computational_dag_vector_impl<v_impl> subGraph;
69+
70+
const std::size_t numVert = 11;
71+
const std::vector<std::pair<std::size_t, std::size_t>> edges({{0, 1}, {2, 3}, {6, 10}, {7, 9}, {0, 2}, {4, 6}, {1, 6}, {6, 7}, {5, 6}, {3, 7}, {1, 2}});
72+
73+
unsigned cntr = 0;
74+
for (std::size_t i = 0U; i < numVert; ++i) {
75+
graph.add_vertex(cntr, cntr + 1U, cntr + 2U, cntr + 3U);
76+
cntr += 4U;
77+
}
78+
for (const auto &[src, tgt] : edges) {
79+
graph.add_edge(src, tgt);
80+
}
81+
82+
const std::vector<vertex_idx_t<computational_dag_vector_impl<v_impl>>> selectVert({2, 3, 10, 6, 7});
83+
const auto vertCorrespondence = create_induced_subgraph_map(graph, subGraph, selectVert);
84+
BOOST_CHECK_EQUAL(subGraph.num_vertices(), selectVert.size());
85+
BOOST_CHECK_EQUAL(subGraph.num_edges(), 4);
86+
87+
for (const auto &vert : selectVert) {
88+
BOOST_CHECK_LT(vertCorrespondence.at(vert), selectVert.size());
89+
90+
for (const auto &otherVert : selectVert) {
91+
if (vertCorrespondence.at(vert) == vertCorrespondence.at(otherVert)) {
92+
BOOST_CHECK_EQUAL(vert, otherVert);
93+
}
94+
}
95+
}
96+
97+
for (const auto &vert : selectVert) {
98+
BOOST_CHECK_EQUAL(graph.vertex_work_weight(vert), subGraph.vertex_work_weight(vertCorrespondence.at(vert)));
99+
BOOST_CHECK_EQUAL(graph.vertex_comm_weight(vert), subGraph.vertex_comm_weight(vertCorrespondence.at(vert)));
100+
BOOST_CHECK_EQUAL(graph.vertex_mem_weight(vert), subGraph.vertex_mem_weight(vertCorrespondence.at(vert)));
101+
BOOST_CHECK_EQUAL(graph.vertex_type(vert), subGraph.vertex_type(vertCorrespondence.at(vert)));
102+
}
103+
}

0 commit comments

Comments
 (0)