1+ /*
2+ Copyright 2025 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, Christos K. Matzoros, Pal Andras Papp, Raphael S. Steiner
17+ */
18+
19+ #pragma once
20+
21+ #include < fstream>
22+ #include < iostream>
23+ #include < vector>
24+
25+ #include " osp/concepts/computational_dag_concept.hpp"
26+
27+ namespace osp {
28+ namespace file_writer {
29+
30+ /* *
31+ * @brief Writes a computational DAG to a stream in the HyperdagDB format.
32+ *
33+ * This function converts a given graph into a hypergraph representation where each node
34+ * with outgoing edges becomes a hyperedge source. The format is compatible with the
35+ * `readComputationalDagHyperdagFormatDB` reader.
36+ *
37+ * @tparam Graph_t The type of the graph, which must satisfy the ComputationalDag concept.
38+ * @param os The output stream to write to.
39+ * @param graph The computational DAG to write.
40+ */
41+ template <typename Graph_t>
42+ void writeComputationalDagHyperdagFormatDB (std::ostream &os, const Graph_t &graph) {
43+ static_assert (is_computational_dag_v<Graph_t>, " Graph_t must be a computational DAG" );
44+
45+ const auto num_vertices = graph.num_vertices ();
46+ unsigned num_hyperedges = 0 ;
47+ unsigned num_pins = 0 ;
48+
49+ std::vector<unsigned > node_to_hyperedge_idx (num_vertices);
50+ std::vector<const vertex_idx_t <Graph_t> *> hyperedge_idx_to_node;
51+
52+ for (const auto &u : graph.vertices ()) {
53+ if (graph.out_degree (u) > 0 ) {
54+ node_to_hyperedge_idx[u] = num_hyperedges;
55+ hyperedge_idx_to_node.push_back (&u);
56+ num_hyperedges++;
57+ num_pins += (graph.out_degree (u) + 1 );
58+ }
59+ }
60+
61+ // Header
62+ os << " %% HyperdagDB format written by OneStopParallel\n " ;
63+ os << num_hyperedges << " " << num_vertices << " " << num_pins << " \n " ;
64+
65+ // Hyperedges
66+ os << " %% Hyperedges: ID comm_weight mem_weight\n " ;
67+ for (unsigned i = 0 ; i < num_hyperedges; ++i) {
68+ const auto u = *hyperedge_idx_to_node[i];
69+ os << i << " " << graph.vertex_comm_weight (u) << " " << graph.vertex_mem_weight (u) << " \n " ;
70+ }
71+
72+ // Vertices
73+ os << " %% Vertices: ID work_weight type\n " ;
74+ for (const auto &u : graph.vertices ()) {
75+ os << u << " " << graph.vertex_work_weight (u);
76+ if constexpr (has_typed_vertices_v<Graph_t>) {
77+ os << " " << graph.vertex_type (u);
78+ } else {
79+ os << " " << 0 ;
80+ }
81+ os << " \n " ;
82+ }
83+
84+ // Pins
85+ os << " %% Pins: HyperedgeID NodeID\n " ;
86+ for (unsigned i = 0 ; i < num_hyperedges; ++i) {
87+ const auto u = *hyperedge_idx_to_node[i];
88+ os << i << " " << u << " \n " ; // Source pin
89+ for (const auto &v : graph.children (u)) {
90+ os << i << " " << v << " \n " ; // Target pins
91+ }
92+ }
93+ }
94+
95+ /* *
96+ * @brief Writes a computational DAG to a file in the HyperdagDB format.
97+ *
98+ * @tparam Graph_t The type of the graph, which must satisfy the ComputationalDag concept.
99+ * @param filename The path to the output file.
100+ * @param graph The computational DAG to write.
101+ * @return true if writing was successful, false otherwise.
102+ */
103+ template <typename Graph_t>
104+ bool writeComputationalDagHyperdagFormatDB (const std::string &filename, const Graph_t &graph) {
105+ std::ofstream os (filename);
106+ if (!os.is_open ()) {
107+ std::cerr << " Error: Failed to open file for writing: " << filename << " \n " ;
108+ return false ;
109+ }
110+ writeComputationalDagHyperdagFormatDB (os, graph);
111+ return true ;
112+ }
113+
114+ } // namespace file_writer
115+ } // namespace osp
0 commit comments