|
| 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 |
0 commit comments