forked from daniel-paul/DITCH
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_hypergraph.cpp
More file actions
96 lines (85 loc) · 2.83 KB
/
Copy pathread_hypergraph.cpp
File metadata and controls
96 lines (85 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "read_hypergraph.h"
#include <fstream>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <vector>
//Reads the hypergraph from the file
int read_hypergraph(HypergraphCSR& H, const std::string& filename) {
std::ifstream infile(filename);
if (!infile.is_open()) {
std::cerr << "Error opening file: " << filename << "\n";
return -1;
}
// First pass: count number of hyperedges and total vertices
VertexId total_vertices = 0;
std::string line;
H.num_vertices = 0;
H.num_hyperedges = 0;
int rank = 0;
while (std::getline(infile, line)) {
if (!line.empty()) {
std::replace(line.begin(), line.end(), ',', ' ');
std::istringstream ss(line);
VertexId v;
VertexId count = 0;
while (ss >> v) count++;
total_vertices += count;
H.num_hyperedges++;
if (v + 1 > H.num_vertices) H.num_vertices = v + 1;
if (rank < count) rank = count;
}
}
std::cout << "Rank: " << rank << "\n";
std::cout << "Average arity: "<< total_vertices/(H.num_hyperedges+0.0) << "\n";
// Allocate arrays
H.ed_vertices = new VertexId[total_vertices];
H.edge_offsets = new VertexId[H.num_hyperedges];
H.edge_sizes = new VertexId[H.num_hyperedges];
H.degrees = new EdgeId[H.num_vertices]();
H.vertex_offset = new EdgeId[H.num_vertices];
H.ve_hyperedges = new EdgeId[total_vertices];
// Second pass: fill arrays
infile.clear();
infile.seekg(0);
VertexId offset = 0;
EdgeId eid = 0;
while (std::getline(infile, line)) {
if (line.empty()) continue;
std::replace(line.begin(), line.end(), ',', ' ');
std::istringstream ss(line);
VertexId v;
H.edge_offsets[eid] = offset;
VertexId edge_size = 0;
while (ss >> v) {
H.ed_vertices[offset++] = v;
edge_size++;
H.degrees[v]++;
}
H.edge_sizes[eid] = edge_size;
eid++;
}
infile.close();
// vertex offsets
EdgeId total = 0;
for (EdgeId v = 0; v < H.num_vertices; v++) {
H.vertex_offset[v] = total;
total += H.degrees[v];
}
//Complete vertex CSR
EdgeId* counts = new EdgeId[H.num_vertices]();
for (EdgeId e = 0; e < H.num_hyperedges; e++) {
for (VertexId i = 0; i < H.edge_sizes[e]; i++) {
VertexId v = H.ed_vertices[H.edge_offsets[e] + i];
VertexId pos = H.vertex_offset[v] + counts[v];
if (pos >= total_vertices) {
std::cerr << "Error: pos out of bounds! e=" << e << " i=" << i
<< " v=" << v << " pos=" << pos << "\n";
}
H.ve_hyperedges[pos] = e;
counts[v]++;
}
}
delete[] counts;
return rank;
}