-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoc_graph.cu
More file actions
335 lines (281 loc) · 10.8 KB
/
Copy pathtoc_graph.cu
File metadata and controls
335 lines (281 loc) · 10.8 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include "graph_shatter_types.h"
#include <thrust/device_ptr.h>
#include <thrust/sort.h>
#include <thrust/unique.h>
#include <thrust/copy.h>
#include <thrust/device_vector.h>
#include <thrust/sequence.h>
#include <thrust/zip_function.h>
#include <thrust/scan.h>
using namespace Realm;
#ifdef REALM_USE_HIP
#include "hip_cuda_compat/hip_cuda.h"
#endif
extern Logger log_app;
__global__ void findInputEdges(size_t* buffer, size_t size){
size_t tid = blockIdx.x * blockDim.x + threadIdx.x;
if(tid < size){
if(tid != 0 && buffer[tid] != buffer[tid - 1]){
buffer[tid] = 1;
}
else{
buffer[tid] = 0;
}
}
}
__host__ void generateNeighborSets(
RegionInstance *ins,
RegionInstance edges,
IndexSpace<1> edgesSpace,
Memory deviceMemory,
RegionInstance *insBuffer,
RegionInstance *bufferInputIds //edges'
)
{
// In refers to u in directed edge (u,v)
AffineAccessor<size_t,1>inAcc(edges, IN_VERTEX);
// Make sure we can use thrust to process the values as a single buffer
assert(inAcc.is_dense_arbitrary(edgesSpace.bounds));
size_t* inPtr = inAcc.ptr(edgesSpace.bounds.lo);
// From this buffer we will generate the ins set
// TODO see if there's lost performance on using these device_vectors. I would guess not for this case
thrust::device_vector<size_t> analysisBuffer(inPtr, inPtr + edgesSpace.volume());
thrust::device_vector<size_t> indicesBuffer(edgesSpace.volume());
thrust::sequence(indicesBuffer.begin(), indicesBuffer.end());
auto inputAndIndexBegin = thrust::make_zip_iterator(analysisBuffer.begin(), indicesBuffer.begin());
auto inputAndIndexEnd = thrust::make_zip_iterator(analysisBuffer.end(), indicesBuffer.end());
// Sort by original values (id of input vertices)
thrust::sort(inputAndIndexBegin, inputAndIndexEnd);
// Save this sorted buffer for eventually generating ins itself
thrust::device_vector<size_t> insAnalysis(analysisBuffer.begin(), analysisBuffer.end());
// Set values to define the points
findInputEdges<<<(edgesSpace.volume() + 255) / 256, 256>>>(analysisBuffer.data().get(), edgesSpace.volume());
// External sum to get buffer vertex ids
thrust::inclusive_scan(analysisBuffer.begin(), analysisBuffer.end(), analysisBuffer.begin());
// Sort back into original order
inputAndIndexBegin = thrust::make_zip_iterator(indicesBuffer.begin(), analysisBuffer.begin());
inputAndIndexEnd = thrust::make_zip_iterator(indicesBuffer.end(), analysisBuffer.end());
thrust::sort(inputAndIndexBegin, inputAndIndexEnd);
//Create the copy of the input vertices with their buffer ids
std::map<FieldID, size_t> fieldSizes;
fieldSizes[IN_VERTEX] = sizeof(size_t);
// TODO see if there's any way to connect Realm events with CUDA streams
RegionInstance::create_instance(
*bufferInputIds,
deviceMemory,
edgesSpace,
fieldSizes,
0,
ProfilingRequestSet()
).wait();
//Actually populates bufferInputIds with data from our analysis
AffineAccessor<size_t,1>bufferIdsAcc(*bufferInputIds, IN_VERTEX);
thrust::device_ptr<size_t> bufferIdsPtr(bufferIdsAcc.ptr(edgesSpace.bounds.lo));
thrust::copy(analysisBuffer.begin(), analysisBuffer.end(), bufferIdsPtr);
// Takes our saved sorted buffer of the input edges
auto new_end = thrust::unique(insAnalysis.begin(), insAnalysis.end());
size_t ins_size = new_end - insAnalysis.begin();
// Build the INS set and moves in data
// Just waiting on event might lower throughput but I'm trying to reuse the buffer
IndexSpace<1> insSpace(Rect<1>(0,ins_size - 1));
Event insReadyEvent = RegionInstance::create_instance(
*ins,
deviceMemory,
insSpace,
fieldSizes,
0,
ProfilingRequestSet()
);
fieldSizes.clear();
fieldSizes[VERTEX_ID] = sizeof(vertex);
//Set out our ins buffer that our GPU will use for every iteration of the algorithm
Event insBufferReadyEvent = RegionInstance::create_instance(
*insBuffer,
deviceMemory,
insSpace,
fieldSizes,
0,
ProfilingRequestSet()
);
insReadyEvent.wait();
//Copy data into our new ins regionInstance
AffineAccessor<size_t,1>insAcc(*ins, IN_VERTEX);
thrust::device_ptr<size_t> insPtr(insAcc.ptr(insSpace.bounds.lo));
thrust::copy(insAnalysis.begin(), insAnalysis.begin() + ins_size, insPtr);
insBufferReadyEvent.wait();
cudaDeviceSynchronize();
return;
}
// Toy initialization function that always resets a vertex to void
__device__ vertex initializeVertexBase(vertex old){
return {0};
}
__device__ __inline__ void computeBase(
vertex *newVertexValues /* Shared memory holding our output values*/,
size_t tempIndex,
size_t edgeNumber,
AffineAccessor<size_t,1>& biiAcc,
AffineAccessor<vertex,1>& insBufferAcc
)
{
// Don't actually use the input for this example
vertex input = insBufferAcc[biiAcc[edgeNumber]];
atomicAdd(&newVertexValues[tempIndex].value, 1);
}
// Currently unoptimized
__global__ void iterationKernelMemory(
Rect<1> edgesSpace,
Rect<1> onsSpace,
AffineAccessor<vertex,1> insBufferAcc,
AffineAccessor<vertex,1> verticesAcc,
AffineAccessor<size_t,1> biiAcc,
AffineAccessor<size_t,1> onsAcc,
AffineAccessor<size_t,1> onsIndexAcc,
AffineAccessor<size_t,1> outputsAcc
)
{
// Gives us a single index within ons
size_t tid = blockIdx.x * blockDim.x + threadIdx.x;
size_t blockBase = blockIdx.x * blockDim.x;
size_t threadCount = blockDim.x;
// Presumes that the ons set is contiguous by ID as specified by original paper
__shared__ vertex newVertexValues[256];
// The final thread that corresponds to an actual vertex in ons
__shared__ size_t finalEdgesIndex;
__shared__ size_t firstEdgesIndex;
// This thread corresponds to a single vertex within ons
if(onsSpace.contains(tid)){
// This thread is the final one corresponding to a vertex
if((tid == blockBase + blockDim.x - 1 && tid <= onsSpace.hi) ||
tid == onsSpace.hi){
finalEdgesIndex = ((tid == onsSpace.hi) ? (size_t) edgesSpace.hi + 1: onsIndexAcc[tid + 1]);
}
// This is the first thread in the block
if(tid == blockBase){
firstEdgesIndex = onsIndexAcc[tid];
}
newVertexValues[tid - blockBase] = initializeVertexBase(verticesAcc[onsAcc[tid]]);
}
__syncthreads();
// Threads cooperatively run computations on edges
// TODO build out full cooperative scheduling implementation
for(size_t i = firstEdgesIndex + threadIdx.x; i < finalEdgesIndex; i += threadCount){
computeBase(
newVertexValues,
outputsAcc[i] - outputsAcc[firstEdgesIndex] /* TODO most likely not the best approach to figure out output vertex*/,
i,
biiAcc,
insBufferAcc
);
}
__syncthreads();
//Write results back to zero copy memory
if(onsSpace.contains(tid)){
verticesAcc[onsAcc[tid]] = newVertexValues[tid - blockBase];
}
}
__global__ void iterationKernelDivergence(
Rect<1> edgesSpace,
Rect<1> onsSpace,
AffineAccessor<vertex,1> insBufferAcc,
AffineAccessor<vertex,1> verticesAcc,
AffineAccessor<size_t,1> biiAcc,
AffineAccessor<size_t,1> boundariesAcc
)
{
// Gives us a single index within ons
size_t tid = blockIdx.x * blockDim.x + threadIdx.x + onsSpace.lo;
size_t blockBase = blockIdx.x * blockDim.x + onsSpace.lo;
size_t threadCount = blockDim.x;
// Presumes that the ons set is contiguous by ID as specified by original paper
__shared__ vertex newVertexValues[256];
__shared__ size_t boundaryValues[256 + 1];
// The final thread that corresponds to an actual vertex in ons
__shared__ size_t finalEdgesIndex;
// This thread corresponds to a single vertex within ons
if(onsSpace.contains(tid)){
newVertexValues[tid - blockBase] = initializeVertexBase(verticesAcc[tid]);
boundaryValues[tid - blockBase] = boundariesAcc[tid];
}
// TODO standardize rect index types with "using"
if(tid == blockBase + threadCount - 1){
finalEdgesIndex = boundariesAcc[min((size_t)onsSpace.hi, tid) + 1];
boundaryValues[min((size_t)onsSpace.hi, tid) - blockBase + 1] = finalEdgesIndex;
}
__syncthreads();
// Threads cooperatively run computations on edges
size_t iteratingOutputId = 0;
size_t initialEdgeIndex = boundaryValues[iteratingOutputId] + threadIdx.x;
for(size_t i = initialEdgeIndex; i < finalEdgesIndex; i += threadCount){
// TODO profile this divergence... (probably fine)
while(i >= boundaryValues[iteratingOutputId + 1]){
iteratingOutputId++;
}
computeBase(
newVertexValues,
iteratingOutputId,
i,
biiAcc,
insBufferAcc
);
}
__syncthreads();
//Write results back to zero copy memory
if(onsSpace.contains(tid)){
verticesAcc[tid] = newVertexValues[tid - blockBase];
}
}
__host__ void runIteration(
IndexSpace<1> edgesSpace /* Represents the edges with outputs represented by ons*/,
IndexSpace<1> boundariesSpace,
AffineAccessor<vertex,1> insBufferAcc /* Read inputs to edges here*/,
AffineAccessor<vertex,1> verticesAcc /* Write final results here*/,
AffineAccessor<size_t,1> biiAcc /* index of the input edge to be read in insBufferAcc*/,
AffineAccessor<size_t,1> boundariesAcc
)
{
assert(boundariesSpace.dense());
assert(edgesSpace.dense());
size_t onsCount = boundariesSpace.volume() - 1;
int threadsPerBlock = 256;
int numBlocks = (onsCount + threadsPerBlock - 1) / threadsPerBlock;
iterationKernelDivergence<<<numBlocks, threadsPerBlock>>>(
edgesSpace.bounds,
{boundariesSpace.bounds.lo, boundariesSpace.bounds.hi - 1},
insBufferAcc,
verticesAcc,
biiAcc,
boundariesAcc
);
cudaDeviceSynchronize();
}
__global__ void loadInsVerticesKernel(
AffineAccessor<vertex,1> verticesAcc,
AffineAccessor<size_t,1> insAcc,
AffineAccessor<vertex,1> insBufferAcc,
Rect<1> insSpace
)
{
size_t tid = blockIdx.x * blockDim.x + threadIdx.x;
if(insSpace.contains(tid)){
insBufferAcc[tid] = verticesAcc[insAcc[tid]];
}
}
__host__ void loadInsVertices(
AffineAccessor<vertex,1> verticesAcc,
AffineAccessor<size_t,1> insAcc,
AffineAccessor<vertex,1> insBufferAcc,
IndexSpace<1> insSpace
)
{
int threadsPerBlock = 256;
assert(insSpace.dense());
int numBlocks = (insSpace.volume() + threadsPerBlock - 1) / threadsPerBlock;
loadInsVerticesKernel<<<numBlocks, threadsPerBlock>>>(
verticesAcc,
insAcc,
insBufferAcc,
insSpace.bounds
);
cudaDeviceSynchronize();
}