-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompress_bindings.cpp
More file actions
121 lines (103 loc) · 4.4 KB
/
Copy pathcompress_bindings.cpp
File metadata and controls
121 lines (103 loc) · 4.4 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <stdexcept>
#include <string>
extern "C" {
#include <igraph.h>
#include <igraph_attributes.h>
}
#include "clique_compress_lib.cpp"
#include "op_compress_lib.cpp"
#include "bicliq_compress_lib.cpp"
#include "op_add_lvl_lib.cpp"
namespace py = pybind11;
static igraph_t load_graph(const std::string& path) {
igraph_t g;
igraph_set_attribute_table(&igraph_cattribute_table);
FILE* f = fopen(path.c_str(), "r");
if (!f)
throw std::runtime_error("Cannot open input file: " + path);
int err = igraph_read_graph_graphml(&g, f, 0);
fclose(f);
if (err != IGRAPH_SUCCESS)
throw std::runtime_error("Error reading GraphML: " + path);
return g;
}
static std::pair<std::vector<igraph_integer_t>, std::pair<std::unordered_map<std::string, std::vector<igraph_integer_t>>, std::unordered_map< std::string, std::vector<std::string>>>> compress(const std::string& path, int c_lvl, int num_proc){
assert(c_lvl>0 && c_lvl <=3 && "c_lvl needs to be between 0 and 2");
assert(num_proc>0 && "num_proc cannot be less than 1");
std::unordered_map<std::string, std::vector<igraph_integer_t>> numeric_attrs;
std::unordered_map<std::string, std::vector<std::string>> str_attrs;
igraph_t g = load_graph(path);
if(c_lvl){
op_compressor op_comp(g, num_proc);
g = op_comp.compress();
op_add_lvl(&g, num_proc);
c_lvl--;
}
if (c_lvl) {
clique_compressor cliq_comp(g, num_proc);
g = cliq_comp.compress();
c_lvl--;
}
if(c_lvl ){
bicliq_compressor bicliq_comp(g, num_proc);
g = bicliq_comp.compress();
}
igraph_vector_int_t edges;
igraph_vector_int_init(&edges, 0);
igraph_get_edgelist(&g, &edges, 0);
std::vector<igraph_integer_t> edge_vec(VECTOR(edges), VECTOR(edges) + igraph_vector_int_size(&edges));
igraph_vector_t proc_id, num_funcs, lvl;
igraph_strvector_t mpi_func, isend_seq, recv_seq, ev_nodes;
igraph_vector_init(&lvl, 0);
igraph_vector_init(&proc_id, 0);
igraph_vector_init(&num_funcs, 0);
igraph_strvector_init(&mpi_func, 0);
igraph_strvector_init(&isend_seq, 0);
igraph_strvector_init(&recv_seq, 0);
igraph_strvector_init(&ev_nodes, 0);
VANV(&g, "process_id", &proc_id);
VANV(&g, "num_main_func", &num_funcs);
VASV(&g, "mpi_function", &mpi_func);
VASV(&g, "isend_seq", &isend_seq);
VASV(&g, "recv_seq", &recv_seq);
VASV(&g, "ev_nodes", &ev_nodes);
VANV(&g, "lvl", &lvl);
std::vector<igraph_integer_t> pid_v(VECTOR(proc_id), VECTOR(proc_id)+igraph_vector_size(&proc_id));
std::vector<igraph_integer_t> nf_v(VECTOR(num_funcs), VECTOR(num_funcs)+igraph_vector_size(&num_funcs));
std::vector<igraph_integer_t> lvl_v(VECTOR(lvl), VECTOR(lvl)+igraph_vector_size(&lvl));
std::vector<std::string> mf_v, is_v, rs_v, en_v;
for (igraph_integer_t i = 0; i < igraph_strvector_size(&mpi_func); i++)
mf_v.push_back(igraph_strvector_get(&mpi_func, i));
for (igraph_integer_t i = 0; i < igraph_strvector_size(&isend_seq); i++)
is_v.push_back(igraph_strvector_get(&isend_seq, i));
for (igraph_integer_t i = 0; i < igraph_strvector_size(&recv_seq); i++)
rs_v.push_back(igraph_strvector_get(&recv_seq, i));
for (igraph_integer_t i = 0; i < igraph_strvector_size(&ev_nodes); i++)
en_v.push_back(igraph_strvector_get(&ev_nodes, i));
numeric_attrs["process_id"] = pid_v;
numeric_attrs["num_main_func"] = nf_v;
str_attrs["mpi_function"] = mf_v;
str_attrs["isend_seq"] = is_v;
str_attrs["recv_seq"] = rs_v;
str_attrs["ev_nodes"] = en_v;
numeric_attrs["lvl"] = lvl_v;
igraph_vector_int_destroy(&edges);
igraph_vector_destroy(&proc_id);
igraph_vector_destroy(&num_funcs);
igraph_strvector_destroy(&mpi_func);
igraph_strvector_destroy(&isend_seq);
igraph_strvector_destroy(&recv_seq);
igraph_strvector_destroy(&ev_nodes);
igraph_vector_destroy(&lvl);
igraph_destroy(&g);
return {edge_vec, {numeric_attrs, str_attrs}};
}
PYBIND11_MODULE(nd_compress, m) {
m.doc() = "ND Detect graph compression bindings";
m.def("compress", &compress,
py::arg("input_path"), py::arg("compression_level"), py::arg("num_procs"),
"Compress an event graph generated by ANACIN-X using the specified compression level.\n"
"Reads input_path and return the compressed graph.");
}