forked from CGCL-codes/LSGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.cpp
More file actions
124 lines (115 loc) · 3.48 KB
/
Map.cpp
File metadata and controls
124 lines (115 loc) · 3.48 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
122
123
124
#pragma once
#include "graph.h"
#include "VertexSubset.cpp"
using namespace graphstore;
template<class F, class Graph>
struct EDGE_MAP_SPARSE {
Graph &G;
VertexSubset &output_vs;
F f;
bool output;
EDGE_MAP_SPARSE(Graph &G_, VertexSubset &output_vs_, F f_, bool output_) :
G(G_), output_vs(output_vs_), f(f_), output(output_) {}
inline bool update(uint32_t val) {
G.map_sparse(f, output_vs, val, output);
return false;
}
};
template<class F, class Graph>
VertexSubset EdgeMapSparse(Graph &G, VertexSubset &vs, F f, bool output) {
// printf("edge map sparse\n");
vs.convert_to_sparse();
VertexSubset output_vs = VertexSubset(vs, false);
struct EDGE_MAP_SPARSE<F,Graph> v(G, output_vs, f, output);
vs.map(v);
output_vs.finalize();
return output_vs;
}
template <class F, class Graph>
VertexSubset EdgeMapDense(Graph &G, VertexSubset &vs, F f, bool output) {
// printf("edge map dense\n");
vs.convert_to_dense();
VertexSubset output_vs = VertexSubset(vs, false);
// needs a grainsize of at least 512
// so writes to the bitvector storing the next vertex set are going to different cache lines
if (vs.all) {
parallel_for(uint64_t i_ = 0; i_ < G.get_num_vertices(); i_+=512) {
uint64_t end = std::min(i_+512, (uint64_t) G.get_num_vertices());
for (uint64_t i = i_; i < end; i++) {
if (f.cond(i) == 1) {
//printf("processing row %lu\n", i);
G.map_dense_vs_all(f, vs, output_vs, i, output);
}
}
}
} else {
parallel_for(uint64_t i_ = 0; i_ < G.get_num_vertices(); i_+=512) {
uint64_t end = std::min(i_+512, (uint64_t) G.get_num_vertices());
for (uint64_t i = i_; i < end; i++) {
//for(uint64_t i = 0; i < G.get_num_vertices(); i++) {
if (f.cond(i) == 1) {
//printf("processing row %lu\n", i);
G.map_dense_vs_not_all(f, vs, output_vs, i, output);
}
}
}
}
return output_vs;
}
template <class F, class Graph>
VertexSubset edgeMap(Graph &G, VertexSubset &vs, F f, bool output = true, uint32_t threshold = 20) {
//vs.print();
//printf("%u, %u, %u\n", G.rows, threshold, vs.get_n());
if (G.get_num_vertices()/threshold <= vs.get_n()) {
return EdgeMapDense(G, vs, f, output);
} else {
return EdgeMapSparse(G, vs, f, output);
}
}
template<class F, bool output>
struct VERTEX_MAP {
VertexSubset &output_vs;
F f;
VERTEX_MAP(VertexSubset &output_vs_, F f_) : output_vs(output_vs_), f(f_) {}
inline bool update(uint32_t val) {
if constexpr (output) {
if (f(val) == 1) {
output_vs.insert(val);
}
} else {
f(val);
}
return false;
}
};
template <class F>
inline VertexSubset vertexMap(VertexSubset &vs, F f, bool output = true) {
//TODO the compilier should have been able to do this itself
if (output) {
VertexSubset output_vs = VertexSubset(vs, false);
struct VERTEX_MAP<F, true> v(output_vs, f);
vs.map(v);
output_vs.finalize();
return output_vs;
} else {
VertexSubset null_vs = VertexSubset();
struct VERTEX_MAP<F, false> v(null_vs, f);
vs.map(v);
return null_vs;
}
}
template <class F>
inline VertexSubset vertexFilter(VertexSubset &vs, F f) {
vs.convert_to_dense();
auto n = vs.get_n();
bool* d_out = newA(bool, n);
parallel_for(uint64_t i = 0; i < n; i++) {
d_out[i] = 0;
}
parallel_for(uint64_t i = 0; i < n; i++) {
if(vs.ba->get(i)) d_out[i] = f(i);
}
auto vs_out = VertexSubset(d_out, n);
free(d_out);
return vs_out;
}