-
Notifications
You must be signed in to change notification settings - Fork 3
/
oliver.h
251 lines (219 loc) · 7.65 KB
/
oliver.h
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Yichao Cheng
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* The single GPU version of Olive
*
* Author: Yichao Cheng ([email protected])
* Created on: 2015-02-05
* Last Modified: 2015-02-05
*/
#ifndef OLIVER_H
#define OLIVER_H
#include "common.h"
#include "csrGraph.h"
#include "logging.h"
#include "timer.h"
#include "utils.h"
#include "commandLine.h"
#include "grd.h"
#include "vertexSubset.h"
#include "oliverKernel.h"
template<typename VertexValue,
typename EdgeValue,
typename AccumValue>
class Oliver {
public:
/**
* The edgeFilter function invokes the corresponding subroutine
* according to the representation of the source vertex subset and
* produces a sparse vertex subset as output.
*/
template<typename F, int GroupSize>
void edgeFilter(VertexSubset &dst, const VertexSubset &src, F f) {
assert(!dst.isDense);
assert(src.isDense);
// Clear the destination subset before generating it.
dst.clear();
// Reset the accumulators before the gather phase starts
accumulators.allTo(defaultAccumValue);
{
auto c = util::kernelConfig(src.size() * GroupSize);
edgeFilterKernel<VertexValue, AccumValue, EdgeValue, F, GroupSize>
<<< c.first, c.second>>>(
src.workqueue.elemsDevice,
src.qSizeDevice,
srcVertices.elemsDevice,
outgoingEdges.elemsDevice,
vertexValues.elemsDevice,
accumulators.elemsDevice,
edgeValues.elemsDevice,
dst.workset.elemsDevice,
f);
}
CUDA_CHECK(cudaThreadSynchronize());
}
/**
* vertexFilter is used to update the local vertex state.
*
* It takes as input a sparse vertex subset and produces either a sparse
* vertex subset or a dense one as output.
*/
template<typename F, bool UseScan>
void vertexFilter(VertexSubset &dst, const VertexSubset &src, F f) {
assert(!src.isDense);
assert(dst.isDense);
// Clear the destination subset before generating it.
dst.clear();
{
auto c = util::kernelConfig(src.capacity());
vertexFilterKernel<VertexValue, AccumValue, F, UseScan>
<<< c.first, c.second>>>(
src.workset.elemsDevice,
src.capacity(),
vertexValues.elemsDevice,
accumulators.elemsDevice,
dst.workqueue.elemsDevice,
dst.qSizeDevice,
f);
}
CUDA_CHECK(cudaThreadSynchronize());
}
/**
* edgeMap is used to update the local edge state.
* @param src A subset of vertices the UDF will be applied to.
* @param f The UDF applied to the edge.
*/
template<typename F>
void edgeMap(const VertexSubset &src, F f) {
assert(!src.isDense);
// Reset the accumulators before the gather phase starts
accumulators.allTo(defaultAccumValue);
auto c = util::kernelConfig(src.capacity());
edgeMapKernel<VertexValue, AccumValue, EdgeValue, F>
<<< c.first, c.second>>>(
src.workset.elemsDevice,
src.capacity(),
srcVertices.elemsDevice,
outgoingEdges.elemsDevice,
vertexValues.elemsDevice,
accumulators.elemsDevice,
edgeValues.elemsDevice,
f);
CUDA_CHECK(cudaThreadSynchronize());
}
/**
* vertexMap is used to update the local vertex state.
* @param src A subset of vertices the UDF will be applied to.
* @param f The UDF applied to the vertices.
*/
template<typename F>
void vertexMap(const VertexSubset &src, F f) {
if (src.isDense) {
auto c = util::kernelConfig(src.size());
vertexMapDenseKernel<VertexValue, AccumValue, F>
<<< c.first, c.second>>>(
src.workqueue.elemsDevice,
src.qSizeDevice,
vertexValues.elemsDevice,
accumulators.elemsDevice,
f);
} else {
auto c = util::kernelConfig(src.capacity());
vertexMapSparseKernel<VertexValue, AccumValue, F>
<<< c.first, c.second>>>(
src.workset.elemsDevice,
src.capacity(),
vertexValues.elemsDevice,
accumulators.elemsDevice,
f);
}
CUDA_CHECK(cudaThreadSynchronize());
}
/**
* Reduce the vertex value by specifying a reduce function
* @return The reduced result
*/
AccumValue vertexReduce() {
vertexValues.persist();
AccumValue r = (AccumValue) 0;
for (int i = 0; i < vertexCount; i++) {
vertexValues[i].reduce(r);
}
return r;
}
/** Initialize with a default accumulator value */
Oliver(AccumValue _accum) : defaultAccumValue(_accum) {}
Oliver() : defaultAccumValue(0) {}
void readGraph(const CsrGraph<int, int> &graph) {
vertexCount = graph.vertexCount;
edgeCount = graph.edgeCount;
srcVertices.reserve(vertexCount + 1);
outgoingEdges.reserve(edgeCount);
vertexValues.reserve(vertexCount);
accumulators.reserve(vertexCount);
edgeValues.reserve(edgeCount);
memcpy(srcVertices.elemsHost, graph.vertices, sizeof(EdgeId) * (vertexCount + 1));
memcpy(outgoingEdges.elemsHost, graph.edges, sizeof(VertexId) * edgeCount);
srcVertices.cache();
outgoingEdges.cache();
}
inline void printVertices() {
vertexValues.persist();
vertexValues.print();
}
inline void printEdges() {
edgeValues.persist();
edgeValues.print();
}
/** Returns the number of the vertices in the graph. */
inline VertexId getVertexCount() const {
return vertexCount;
}
~Oliver() {
srcVertices.del();
outgoingEdges.del();
vertexValues.del();
accumulators.del();
edgeValues.del();
}
private:
/** Record the edge and vertex number of each partition. */
VertexId vertexCount;
EdgeId edgeCount;
/**
* CSR related data structure.
*/
GRD<EdgeId> srcVertices;
GRD<VertexId> outgoingEdges;
// GRD<EdgeId> dstVertices;
// GRD<VertexId> incomingEdges;
/**
* Vertex-wise state.
*/
GRD<VertexValue> vertexValues;
GRD<AccumValue> accumulators;
GRD<EdgeValue> edgeValues;
AccumValue defaultAccumValue;
};
#endif // OLIVER_H