-
Notifications
You must be signed in to change notification settings - Fork 2
/
graph_test.cu
398 lines (323 loc) · 13.2 KB
/
graph_test.cu
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
* Parallel Graph Preprocessing of cuckoo filter
* This preprocesses a batch insertion into a cuckoo filter by creating a directed graph (V,E) where:
* V is a set of vertices that represent each bucket of the cuckoo filter
* E is a set of edges (u,v) with weight w where:
* w is the fingerprint of a specific entry
* u is the bucket number given by hash(entry)
* v is the bucket number given by hash(entry) xor hash(fingerprint)
* dir indicates the vertex pointed to by the edge. Also indicates
* which bucket number the fingerprint should be placed in.
*/
#include <cstring>
#include <stdexcept>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <climits>
#include <curand.h>
#include <curand_kernel.h>
#include <sys/time.h>
#define LARGE_THRESHOLD_VAL 10000
double preprocessTime = 0;
double insertTime = 0;
struct timeval StartingTime;
void setTime(){
gettimeofday( &StartingTime, NULL );
}
double getTime(){
struct timeval PausingTime, ElapsedTime;
gettimeofday( &PausingTime, NULL );
timersub(&PausingTime, &StartingTime, &ElapsedTime);
return ElapsedTime.tv_sec*1000.0+ElapsedTime.tv_usec/1000.0; // Returning in milliseconds.
}
__device__ void random(unsigned int seed, int* result, int max) {
/* CUDA's random number library uses curandState_t to keep track of the seed value
we will store a random state for every thread */
curandState_t state;
/* we have to initialize the state */
curand_init(seed, /* the seed controls the sequence of random values that are produced */
0, /* the sequence number is only important with multiple cores */
0, /* the offset is how much extra we advance in the sequence for each call, can be 0 */
&state);
/* curand works like rand - except that it takes a state as a parameter */
*result = curand(&state) % max;
}
template <typename T_file>
void openFileToAccess( T_file& input_file, std::string file_name ) {
input_file.open( file_name.c_str() );
if( !input_file )
throw std::runtime_error( "Failed to open specified file: " + file_name + "\n" );
}
void * cudaMallocAndCpy(int size, void * hostMemory) {
void * gpuMem;
cudaMalloc((void**) &gpuMem, size);
if (hostMemory != NULL) {
cudaMemcpy(gpuMem, hostMemory, size, cudaMemcpyHostToDevice);
}
return gpuMem;
}
void cudaGetFromGPU(void * destination, void * gpuMemory, int size) {
cudaMemcpy(destination, gpuMemory, size, cudaMemcpyDeviceToHost);
}
void cudaSendToGPU(void * destination, void * hostMemory, int size) {
cudaMemcpy(destination, hostMemory, size, cudaMemcpyHostToDevice);
}
class Edge {
public:
unsigned int src; //hash(x) location
unsigned int dst; //hash(x) xor hash(fp) location
unsigned char fp; //fingerprint
int dir; //0 to be src, 1 to be dst
__device__ __host__ Edge(){}
};
class Graph {
public:
int *buckets; //value at index i is the number of indegrees to a bucket i
Edge *edges;
unsigned int num_edges;
unsigned int num_buckets;
unsigned int max_bucket_size;
__device__ __host__ Graph(unsigned int edges, unsigned int nb, unsigned int bucket_size) {
num_edges = edges;
num_buckets = nb;
max_bucket_size = bucket_size;
buckets = new int[num_buckets]();
edges = NULL;
}
__device__ void printGraph() {
int thread_id = blockDim.x * blockIdx.x + threadIdx.x; //real thread number
if(thread_id == 0) {
for(int i=0; i<num_edges; i++) {
printf("Edge %d: %u \t src: %u \t dst: %u\n",i, edges[i].fp, edges[i].src, edges[i].dst);
}
printCollisions();
}
}
__device__ void printCollisions() {
int thread_id = blockDim.x * blockIdx.x + threadIdx.x; //real thread number
if(thread_id == 0) {
printf("\n\nBuckets\n");
for(int i=0; i< num_buckets ; i++) {
if(buckets[i] > max_bucket_size) {
printf("Collisions for bucket %d: %d\n", i, buckets[i]);
}
}
}
}
};
// __global__ void setup_kernel (curandState * state, Graph *g)
// {
// int thread_id = blockDim.x * blockIdx.x + threadIdx.x; //real thread number
// // change sequence number to currIdx if values are too correlated
// // curand_init(1234, 0, 0, &state[currIdx]);
// curand_init(1234, 0, 0, &state[currIdx]);
// }
/**
* Parallel graph building
* @param entries is a list of entries to enter
* @param entryListSize is the size of the @param entries list
* @param g is an address in the GPU to pla\ce result. Assumes g->edges has been given enough space for @param entryListSize items
*/
__global__ void findAllCollisions(int* entries, int entryListSize, Graph * g) {
int total_threads = blockDim.x * gridDim.x; //total threads
int thread_id = blockDim.x * blockIdx.x + threadIdx.x; //real thread number
int thread_id_block = threadIdx.x; //thread number in block
// CHANGE BELOW LINE TO BE MORE EFFICIENT
int rounds = entryListSize % total_threads == 0 ? (entryListSize/total_threads):((entryListSize/total_threads)+1);
g->num_edges = entryListSize;
for (size_t i = 0; i <rounds; i++) {
int currIdx = i*total_threads + thread_id;
if(currIdx < entryListSize) {
int * entry = &entries[currIdx];
//printf("KERNEL SPACE current Index %d, Thread id %d: %x\n", currIdx, thread_id, entry);
unsigned int bucket1;
hash_item((unsigned char*) entry,
4,
g->num_buckets,
HASHFUN_NORM,
&bucket1);
const uint64_t hash = TwoIndependentMultiplyShift(*entry);
unsigned char fp = (unsigned char) hash;
unsigned int fpHash;
hash_item((unsigned char*) &fp,
1,
g->num_buckets,
HASHFUN_NORM,
&fpHash);
unsigned int bucket2 = ((bucket1 ^ fpHash) & 0b11111111) % g->num_buckets;
//build edge
g->edges[currIdx].fp = fp;
g->edges[currIdx].src = bucket1 % g->num_buckets;
g->edges[currIdx].dst = bucket2 % g->num_buckets;
// Copy state to local memory for efficiency */
// curandState local_state = global_state[thread_id];
// /* Generate pseudo - random unsigned ints
// g->edges[i].dir = curand_uniform(&local_state);
//update bucket
atomicAdd(&(g->buckets[bucket1]), 1);
}
}
}
__global__ void resetCollisions(Graph * g) {
int total_threads = blockDim.x * gridDim.x; //total threads
int thread_id = blockDim.x * blockIdx.x + threadIdx.x; //real thread number
int thread_id_block = threadIdx.x; //thread number in block
int rounds = (g->num_buckets % total_threads == 0) ? (g->num_buckets/total_threads):(g->num_buckets/total_threads + 1);
for (size_t iter = 0; iter < rounds; iter++) {
int currIdx = iter*total_threads + thread_id;
if(currIdx < g->num_buckets) {
int * currBucket = &(g->buckets[currIdx]);
*currBucket = 0;
}
}
rounds = (g->num_edges % total_threads == 0) ? (g->num_edges/total_threads):(g->num_edges/total_threads + 1);
for (size_t iter = 0; iter < rounds; iter++) {
int currIdx = iter*total_threads + thread_id;
if(currIdx < g->num_edges) {
int b = (g->edges[currIdx].dir == 0) ? (g->edges[currIdx].src):(g->edges[currIdx].dst);
atomicAdd(&(g->buckets[b]),1);
}
}
//g->printCollisions();
}
/**
* Edge Processing Kernel
* Finds random edges to evict until capacity for each bucket is equal to 0
*
*/
__global__ void processEdges(Graph * g, int* anyChange, unsigned int randNum) {
int total_threads = blockDim.x * gridDim.x; //total threads
int thread_id = blockDim.x * blockIdx.x + threadIdx.x; //real thread number
int thread_id_block = threadIdx.x; //thread number in block
int num_edges = g->num_edges;
int rounds = num_edges % total_threads == 0 ? (num_edges/total_threads):(num_edges/total_threads+1);
for(int i=0; i<rounds; i++) {
int currIdx = total_threads*i + thread_id; //current edge to process
if(currIdx < g->num_edges) {
Edge *e = &g->edges[currIdx];
//determine the bucket it's in
int curr_bucket = e->dir == 0 ? e->src:e->dst;
//check the bucket
int * bucketCount = &(g->buckets[curr_bucket]);
int tmp = *bucketCount;
//decrement the bucket count if > 0
//int rand;
//random((unsigned int)clock() + thread_id, &rand, 50);
if(*bucketCount > g->max_bucket_size) {
int old = atomicDec((unsigned int *)bucketCount, INT_MAX);
old--;
int shift = randNum % tmp;
int shiftedValue = old - shift;
int bucketOffset = (shiftedValue < 0) ? shiftedValue + tmp : shiftedValue;
//if (e->dir) {
// } else {
// printf("tmp %d, old %d, shift %d, shiftedValue %d, bucketOffset %d \t Evicting %d from %d to %d\n", tmp, old, shift, shiftedValue, bucketOffset, e->fp, e->src, e->dst);
// }
//printf("tmp %d, old %d, shift %d, shiftedValue %d, bucketOffset %d\n", tmp, old, shift, shiftedValue, bucketOffset);
if (bucketOffset >= g->max_bucket_size && old < LARGE_THRESHOLD_VAL){
e->dir = !e->dir; // flip the bit
// if (e->dir)
// printf("Evicting %d from %d to %d\n", e->fp, e->src, e->dst);
// else
// printf("Evicting %d from %d to %d\n", e->fp, e->dst, e->src);
*anyChange = 1;
}
}
}
}
//g->printCollisions();
}
void initGraphCPU(int entry_size) {
Graph * graph;
cudaMalloc(&graph, sizeof(Graph));
Edge * e;
cudaMalloc(&e, sizeof(Edge)*entry_size);
}
__global__ void makeGraphCuckoo(Graph * g, CuckooFilter * c, int * globalByteMask) {
int total_threads = blockDim.x * gridDim.x; //total threads
int thread_id = blockDim.x * blockIdx.x + threadIdx.x; //real thread number
int thread_id_block = threadIdx.x; //thread number in block
// if (thread_id==0) {
// c->printFilter();
// printf("\n");
// }
int rounds = (g->num_edges % total_threads == 0) ? (g->num_edges/total_threads):((g->num_edges/total_threads)+1);
for (size_t i = 0; i < rounds; i++) {
int currIdx = total_threads*i + thread_id;
if(currIdx < g->num_edges) {
Edge * e = &(g->edges[currIdx]);
int currBucket = e->dir == 0 ? e->src:e->dst;
int index = atomicAdd(&(globalByteMask[currBucket]), 1);
c->insert(e->fp,currBucket,index);
}
}
__syncthreads();
// if (thread_id==0) {
// c->printFilter();
// }
}
double transferToCuckooFilter(Graph * g, CuckooFilter * c) {
Graph * h_graph = (Graph*)malloc(sizeof(Graph));
cudaGetFromGPU(h_graph, g, sizeof(Graph));
int * byteMask = new int[h_graph->num_buckets];
for (size_t i = 0; i < h_graph->num_buckets; i++) {
byteMask[i] = 0;
}
int * g_byteMask = (int*)cudaMallocAndCpy(sizeof(int)*h_graph->num_buckets,(void*) byteMask);
setTime();
makeGraphCuckoo<<<ceil((double)h_graph->num_buckets/1024), 1024>>>(g, c, g_byteMask);
cudaDeviceSynchronize();
double insertTime = getTime();
delete byteMask;
return insertTime;
}
int insert(int* entries, unsigned int num_entries, unsigned int num_buckets, unsigned int bucket_size, CuckooFilter * cf){
std::cout << "Inserting " << num_entries << " entries"<< std::endl;
const int fail_threshold = (int)(sqrt(num_buckets*bucket_size)*log2((float)(num_buckets*bucket_size)));
int anychange = 1;
int * d_change = (int *) cudaMallocAndCpy(sizeof(int), &anychange);
Graph *h_graph = new Graph(num_entries, num_buckets, bucket_size);
//set up pointer
cudaMalloc((void**)&(h_graph->edges), sizeof(Edge)*num_entries);
cudaMalloc((void**)&(h_graph->buckets), sizeof(int)*num_buckets);
Graph *d_graph = (Graph *) cudaMallocAndCpy(sizeof(Graph), h_graph);
int * d_entries = (int *) cudaMallocAndCpy(sizeof(int)*num_entries, entries);
std::cout << "Calling kernel" << std::endl;
setTime();
findAllCollisions<<<2, 512>>>(d_entries, num_entries, d_graph);
cudaDeviceSynchronize();
preprocessTime = getTime();
int count = 0;
while (anychange != 0){
anychange = 0;
cudaSendToGPU(d_change, &anychange, sizeof(int));
// generate random number
setTime();
unsigned int randNum = rand() % (num_buckets * 8);
//std::cout << "Found all collisions, rand num: "<< randNum << std::endl;
processEdges<<<ceil((double)num_entries/1024), 1024>>>(d_graph, d_change, randNum);
cudaDeviceSynchronize();
preprocessTime += getTime();
//std::cout << "Proccessed edge using " << ceil((double)num_entries/1024) << "threads " << std::endl;
cudaGetFromGPU(&anychange, d_change, sizeof(int));
//std::cout << "Got value of anychange: " << anychange << std::endl;
if(anychange == 1){
setTime();
resetCollisions<<<ceil((double)num_entries/1024), 1024>>>(d_graph);
cudaDeviceSynchronize();
preprocessTime += getTime();
}
count++;
if (count >= fail_threshold)
return count;
}
CuckooFilter * g_cf = (CuckooFilter *)cudaMallocAndCpy(sizeof(CuckooFilter), cf);
setTime();
insertTime = transferToCuckooFilter(d_graph, g_cf);
cudaGetFromGPU(cf,g_cf, sizeof(CuckooFilter));
printf("Preprocessing time %f\n", preprocessTime);
printf("Insertion time: %f\n", insertTime);
printf("Completed insertion with %d iterations\n",count);
return 0;
}