Skip to content

Commit 518a6b1

Browse files
subgraph for compact sparse graph
1 parent c5854ca commit 518a6b1

2 files changed

Lines changed: 77 additions & 6 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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, Benjamin Lozes, Pal Andras Papp, Raphael S. Steiner
17+
*/
18+
19+
#pragma once
20+
21+
#include "osp/graph_algorithms/subgraph_algorithms.hpp"
22+
#include "osp/graph_implementations/adj_list_impl/compact_sparse_graph.hpp"
23+
24+
namespace osp {
25+
26+
template<typename Graph_t_in, typename vert_t, typename edge_t, typename work_weight_type, typename comm_weight_type, typename mem_weight_type, typename vertex_type_template_type>
27+
std::unordered_map<vertex_idx_t<Graph_t_in>, vertex_idx_t<Graph_t_in>> create_induced_subgraph_map(const Graph_t_in &dag, Compact_Sparse_Graph<true, true, true, true, true, vert_t, edge_t, work_weight_type, comm_weight_type, mem_weight_type, vertex_type_template_type> &dag_out,
28+
const std::vector<vertex_idx_t<Graph_t_in>> &selected_nodes) {
29+
30+
using Graph_t_out = Compact_Sparse_Graph<true, true, true, true, true, vert_t, edge_t, work_weight_type, comm_weight_type, mem_weight_type, vertex_type_template_type>;
31+
32+
static_assert(std::is_same_v<vertex_idx_t<Graph_t_in>, vertex_idx_t<Graph_t_out>>,
33+
"Graph_t_in and out must have the same vertex_idx types");
34+
35+
const std::vector<vertex_idx_t<Graph_t_in>> topOrder = GetTopOrder(instance.getComputationalDag());
36+
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) {
38+
topOrderPosition[topOrder[pos]] = pos;
39+
}
40+
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]; };
42+
43+
std::set<vertex_idx_t<Graph_t_in>, decltype(topCmp)> selectedVerticesOrdered(selected_nodes.begin(), selected_nodes.end(), topCmp);
44+
45+
std::unordered_map<vertex_idx_t<Graph_t_in>, vertex_idx_t<Graph_t_in>> local_idx;
46+
local_idx.reserve(selected_nodes.size());
47+
48+
vertex_idx_t<Graph_t_in> nodeCntr = 0;
49+
for (const auto &node : selectedVerticesOrdered) {
50+
local_idx[node] = nodeCntr++;
51+
}
52+
53+
std::vector<std::pair<vertex_idx_t<Graph_t_in>, vertex_idx_t<Graph_t_in>>> edges;
54+
for (const auto &node : selectedVerticesOrdered) {
55+
for (const auto &chld : dag.children(node)) {
56+
if (selectedVerticesOrdered.find(chld) != selectedVerticesOrdered.end()) {
57+
edges.emplace(node, chld);
58+
}
59+
}
60+
}
61+
62+
dag_out = Graph_t_out(nodeCntr, edges);
63+
64+
for (const auto &[oriVert, outVert] : local_idx) {
65+
dag_out.set_vertex_work_weight(outVert, dag.vertex_work_weight(oriVert));
66+
dag_out.set_vertex_comm_weight(outVert, dag.vertex_comm_weight(oriVert));
67+
dag_out.set_vertex_mem_weight(outVert, dag.vertex_mem_weight(oriVert));
68+
dag_out.set_vertex_type(outVert, dag.vertex_type(oriVert));
69+
}
70+
71+
return local_idx;
72+
}
73+
74+
} // end namespace osp

include/osp/graph_algorithms/subgraph_algorithms.hpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ limitations under the License.
2222
#include "osp/concepts/directed_graph_concept.hpp"
2323
#include <map>
2424
#include <set>
25+
#include <unordered_map>
2526
#include <vector>
2627

2728
namespace osp {
@@ -91,14 +92,12 @@ void create_induced_subgraph(const Graph_t_in &dag, Graph_t_out &dag_out,
9192
}
9293
}
9394

94-
9595
template<typename Graph_t_in, typename Graph_t_out>
9696
void create_induced_subgraph(const Graph_t_in &dag, Graph_t_out &dag_out,
9797
const std::vector<vertex_idx_t<Graph_t_in>> &selected_nodes) {
9898
return create_induced_subgraph(dag, dag_out, std::set<vertex_idx_t<Graph_t_in>>(selected_nodes.begin(), selected_nodes.end()));
9999
}
100100

101-
102101
template<typename Graph_t>
103102
bool checkOrderedIsomorphism(const Graph_t &first, const Graph_t &second) {
104103

@@ -170,8 +169,6 @@ std::vector<Graph_t_out> create_induced_subgraphs(const Graph_t_in &dag_in,
170169
static_assert(is_constructable_cdag_edge_v<Graph_t_out>,
171170
"Graph_t_out must satisfy the constructable_cdag_edge concept");
172171

173-
174-
175172
unsigned number_of_parts = 0;
176173
for (const auto id : partition_IDs)
177174
number_of_parts = std::max(number_of_parts, id + 1);
@@ -202,7 +199,7 @@ std::vector<Graph_t_out> create_induced_subgraphs(const Graph_t_in &dag_in,
202199

203200
if (partition_IDs[node] == partition_IDs[succ])
204201
split_dags[partition_IDs[node]].add_edge(local_idx[node], local_idx[succ],
205-
dag_in.edge_comm_weight(out_edge));
202+
dag_in.edge_comm_weight(out_edge));
206203
}
207204
}
208205
} else {
@@ -220,7 +217,7 @@ std::vector<Graph_t_out> create_induced_subgraphs(const Graph_t_in &dag_in,
220217

221218
template<typename Graph_t_in, typename Graph_t_out>
222219
std::unordered_map<vertex_idx_t<Graph_t_in>, vertex_idx_t<Graph_t_in>> create_induced_subgraph_map(const Graph_t_in &dag, Graph_t_out &dag_out,
223-
const std::vector<vertex_idx_t<Graph_t_in>> &selected_nodes) {
220+
const std::vector<vertex_idx_t<Graph_t_in>> &selected_nodes) {
224221

225222
static_assert(std::is_same_v<vertex_idx_t<Graph_t_in>, vertex_idx_t<Graph_t_out>>,
226223
"Graph_t_in and out must have the same vertex_idx types");

0 commit comments

Comments
 (0)