Skip to content

Commit e0f77c0

Browse files
committed
modified HyperdagDB reader, added hyperDagDB writer
added hyperdagDB writer update
1 parent 3799868 commit e0f77c0

File tree

2 files changed

+132
-10
lines changed

2 files changed

+132
-10
lines changed

include/osp/auxiliary/io/hdag_graph_file_reader.hpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ bool readComputationalDagHyperdagFormatDB(std::ifstream& infile, Graph_t& graph)
164164
return false;
165165
}
166166

167-
std::vector<v_commw_t<Graph_t>> hyperedge_weights(static_cast<size_t>(hEdges), 1);
167+
std::vector<v_commw_t<Graph_t>> hyperedge_comm_weights(static_cast<size_t>(hEdges), 1);
168+
std::vector<v_memw_t<Graph_t>> hyperedge_mem_weights(static_cast<size_t>(hEdges), 1);
168169

169170
// Read hyperedges
170171
for (int i = 0; i < hEdges; ++i) {
@@ -175,18 +176,19 @@ bool readComputationalDagHyperdagFormatDB(std::ifstream& infile, Graph_t& graph)
175176
}
176177

177178
std::istringstream edgeStream(line);
178-
int hEdge = -1, weight = 1;
179+
int hEdge = -1, comm_weight = 1, mem_weight = 1;
179180
if (!(edgeStream >> hEdge)) {
180181
std::cerr << "Warning: Could not read hyperedge ID for hyperedge " << i << ".\n";
181182
continue;
182183
}
183-
edgeStream >> weight; // optional
184+
edgeStream >> comm_weight >> mem_weight; // optional
184185

185186
if (hEdge < 0 || hEdge >= hEdges) {
186187
std::cerr << "Error: Hyperedge ID " << hEdge << " is out of range (0 to " << hEdges - 1 << ").\n";
187188
continue;
188189
}
189-
hyperedge_weights[static_cast<size_t>(hEdge)] = weight;
190+
hyperedge_comm_weights[static_cast<size_t>(hEdge)] = static_cast<v_commw_t<Graph_t>>(comm_weight);
191+
hyperedge_mem_weights[static_cast<size_t>(hEdge)] = static_cast<v_memw_t<Graph_t>>(mem_weight);
190192
}
191193

192194
graph = Graph_t(static_cast<vertex_idx_t<Graph_t>>(N));
@@ -200,20 +202,23 @@ bool readComputationalDagHyperdagFormatDB(std::ifstream& infile, Graph_t& graph)
200202
}
201203

202204
std::istringstream vertexStream(line);
203-
int node = -1, work = 1, comm = 1;
205+
int node = -1, work = 1, type = 0;
204206
if (!(vertexStream >> node)) {
205207
std::cerr << "Warning: Could not read vertex ID for vertex " << i << ".\n";
206208
continue;
207209
}
208-
vertexStream >> work >> comm;
210+
vertexStream >> work >> type;
209211

210212
if (node < 0 || node >= N) {
211213
std::cerr << "Error: Vertex ID " << node << " is out of range (0 to " << N - 1 << ").\n";
212214
continue;
213215
}
214216

215217
graph.set_vertex_work_weight(static_cast<vertex_idx_t<Graph_t>>(node), static_cast<v_workw_t<Graph_t>>(work));
216-
graph.set_vertex_comm_weight(static_cast<vertex_idx_t<Graph_t>>(node), static_cast<v_commw_t<Graph_t>>(comm));
218+
219+
if constexpr (has_typed_vertices_v<Graph_t>) {
220+
graph.set_vertex_type(static_cast<vertex_idx_t<Graph_t>>(node), static_cast<v_type_t<Graph_t>>(type));
221+
}
217222
}
218223

219224
// Resize(N);
@@ -239,18 +244,20 @@ bool readComputationalDagHyperdagFormatDB(std::ifstream& infile, Graph_t& graph)
239244
continue;
240245
}
241246

242-
std::size_t edgeIdx = static_cast<std::size_t>(hEdge);
243-
std::size_t nodeIdx = static_cast<std::size_t>(node);
247+
const std::size_t edgeIdx = static_cast<std::size_t>(hEdge);
248+
const std::size_t nodeIdx = static_cast<std::size_t>(node);
244249

245250
if (edgeSource[edgeIdx] == -1) {
246251
edgeSource[edgeIdx] = node;
252+
graph.set_vertex_comm_weight(static_cast<vertex_idx_t<Graph_t>>(node), hyperedge_comm_weights[edgeIdx]);
253+
graph.set_vertex_mem_weight(static_cast<vertex_idx_t<Graph_t>>(node), hyperedge_mem_weights[edgeIdx]);
247254
} else {
248255
auto edge = graph.add_edge(static_cast<vertex_idx_t<Graph_t>>(edgeSource[edgeIdx]),
249256
static_cast<vertex_idx_t<Graph_t>>(nodeIdx));
250257

251258
if constexpr (is_modifiable_cdag_comm_edge_v<Graph_t>) {
252259
graph.set_edge_comm_weight(edge.first,
253-
static_cast<e_commw_t<Graph_t>>(hyperedge_weights[edgeIdx]));
260+
static_cast<e_commw_t<Graph_t>>(hyperedge_comm_weights[edgeIdx]));
254261
}
255262
}
256263
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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

Comments
 (0)