-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathlinearprobing.cu
268 lines (219 loc) · 8.16 KB
/
linearprobing.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
#include "stdio.h"
#include "stdint.h"
#include "vector"
#include "linearprobing.h"
// 32 bit Murmur3 hash
__device__ uint32_t hash(uint32_t k)
{
k ^= k >> 16;
k *= 0x85ebca6b;
k ^= k >> 13;
k *= 0xc2b2ae35;
k ^= k >> 16;
return k & (kHashTableCapacity-1);
}
// Create a hash table. For linear probing, this is just an array of KeyValues
KeyValue* create_hashtable()
{
// Allocate memory
KeyValue* hashtable;
cudaMalloc(&hashtable, sizeof(KeyValue) * kHashTableCapacity);
// Initialize hash table to empty
static_assert(kEmpty == 0xffffffff, "memset expected kEmpty=0xffffffff");
cudaMemset(hashtable, 0xff, sizeof(KeyValue) * kHashTableCapacity);
return hashtable;
}
// Insert the key/values in kvs into the hashtable
__global__ void gpu_hashtable_insert(KeyValue* hashtable, const KeyValue* kvs, unsigned int numkvs)
{
unsigned int threadid = blockIdx.x*blockDim.x + threadIdx.x;
if (threadid < numkvs)
{
uint32_t key = kvs[threadid].key;
uint32_t value = kvs[threadid].value;
uint32_t slot = hash(key);
while (true)
{
uint32_t prev = atomicCAS(&hashtable[slot].key, kEmpty, key);
if (prev == kEmpty || prev == key)
{
hashtable[slot].value = value;
return;
}
slot = (slot + 1) & (kHashTableCapacity-1);
}
}
}
void insert_hashtable(KeyValue* pHashTable, const KeyValue* kvs, uint32_t num_kvs)
{
// Copy the keyvalues to the GPU
KeyValue* device_kvs;
cudaMalloc(&device_kvs, sizeof(KeyValue) * num_kvs);
cudaMemcpy(device_kvs, kvs, sizeof(KeyValue) * num_kvs, cudaMemcpyHostToDevice);
// Have CUDA calculate the thread block size
int mingridsize;
int threadblocksize;
cudaOccupancyMaxPotentialBlockSize(&mingridsize, &threadblocksize, gpu_hashtable_insert, 0, 0);
// Create events for GPU timing
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start);
// Insert all the keys into the hash table
int gridsize = ((uint32_t)num_kvs + threadblocksize - 1) / threadblocksize;
gpu_hashtable_insert<<<gridsize, threadblocksize>>>(pHashTable, device_kvs, (uint32_t)num_kvs);
cudaEventRecord(stop);
cudaEventSynchronize(stop);
float milliseconds = 0;
cudaEventElapsedTime(&milliseconds, start, stop);
float seconds = milliseconds / 1000.0f;
printf(" GPU inserted %d items in %f ms (%f million keys/second)\n",
num_kvs, milliseconds, num_kvs / (double)seconds / 1000000.0f);
cudaFree(device_kvs);
}
// Lookup keys in the hashtable, and return the values
__global__ void gpu_hashtable_lookup(KeyValue* hashtable, KeyValue* kvs, unsigned int numkvs)
{
unsigned int threadid = blockIdx.x * blockDim.x + threadIdx.x;
if (threadid < numkvs)
{
uint32_t key = kvs[threadid].key;
uint32_t slot = hash(key);
while (true)
{
if (hashtable[slot].key == key)
{
kvs[threadid].value = hashtable[slot].value;
return;
}
if (hashtable[slot].key == kEmpty)
{
kvs[threadid].value = kEmpty;
return;
}
slot = (slot + 1) & (kHashTableCapacity - 1);
}
}
}
void lookup_hashtable(KeyValue* pHashTable, KeyValue* kvs, uint32_t num_kvs)
{
// Copy the keyvalues to the GPU
KeyValue* device_kvs;
cudaMalloc(&device_kvs, sizeof(KeyValue) * num_kvs);
cudaMemcpy(device_kvs, kvs, sizeof(KeyValue) * num_kvs, cudaMemcpyHostToDevice);
// Have CUDA calculate the thread block size
int mingridsize;
int threadblocksize;
cudaOccupancyMaxPotentialBlockSize(&mingridsize, &threadblocksize, gpu_hashtable_insert, 0, 0);
// Create events for GPU timing
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start);
// Insert all the keys into the hash table
int gridsize = ((uint32_t)num_kvs + threadblocksize - 1) / threadblocksize;
gpu_hashtable_lookup << <gridsize, threadblocksize >> > (pHashTable, device_kvs, (uint32_t)num_kvs);
cudaEventRecord(stop);
cudaEventSynchronize(stop);
float milliseconds = 0;
cudaEventElapsedTime(&milliseconds, start, stop);
float seconds = milliseconds / 1000.0f;
printf(" GPU lookup %d items in %f ms (%f million keys/second)\n",
num_kvs, milliseconds, num_kvs / (double)seconds / 1000000.0f);
cudaFree(device_kvs);
}
// Delete each key in kvs from the hash table, if the key exists
// A deleted key is left in the hash table, but its value is set to kEmpty
// Deleted keys are not reused; once a key is assigned a slot, it never moves
__global__ void gpu_hashtable_delete(KeyValue* hashtable, const KeyValue* kvs, unsigned int numkvs)
{
unsigned int threadid = blockIdx.x * blockDim.x + threadIdx.x;
if (threadid < numkvs)
{
uint32_t key = kvs[threadid].key;
uint32_t slot = hash(key);
while (true)
{
if (hashtable[slot].key == key)
{
hashtable[slot].value = kEmpty;
return;
}
if (hashtable[slot].key == kEmpty)
{
return;
}
slot = (slot + 1) & (kHashTableCapacity - 1);
}
}
}
void delete_hashtable(KeyValue* pHashTable, const KeyValue* kvs, uint32_t num_kvs)
{
// Copy the keyvalues to the GPU
KeyValue* device_kvs;
cudaMalloc(&device_kvs, sizeof(KeyValue) * num_kvs);
cudaMemcpy(device_kvs, kvs, sizeof(KeyValue) * num_kvs, cudaMemcpyHostToDevice);
// Have CUDA calculate the thread block size
int mingridsize;
int threadblocksize;
cudaOccupancyMaxPotentialBlockSize(&mingridsize, &threadblocksize, gpu_hashtable_insert, 0, 0);
// Create events for GPU timing
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start);
// Insert all the keys into the hash table
int gridsize = ((uint32_t)num_kvs + threadblocksize - 1) / threadblocksize;
gpu_hashtable_delete<< <gridsize, threadblocksize >> > (pHashTable, device_kvs, (uint32_t)num_kvs);
cudaEventRecord(stop);
cudaEventSynchronize(stop);
float milliseconds = 0;
cudaEventElapsedTime(&milliseconds, start, stop);
float seconds = milliseconds / 1000.0f;
printf(" GPU delete %d items in %f ms (%f million keys/second)\n",
num_kvs, milliseconds, num_kvs / (double)seconds / 1000000.0f);
cudaFree(device_kvs);
}
// Iterate over every item in the hashtable; return non-empty key/values
__global__ void gpu_iterate_hashtable(KeyValue* pHashTable, KeyValue* kvs, uint32_t* kvs_size)
{
unsigned int threadid = blockIdx.x * blockDim.x + threadIdx.x;
if (threadid < kHashTableCapacity)
{
if (pHashTable[threadid].key != kEmpty)
{
uint32_t value = pHashTable[threadid].value;
if (value != kEmpty)
{
uint32_t size = atomicAdd(kvs_size, 1);
kvs[size] = pHashTable[threadid];
}
}
}
}
std::vector<KeyValue> iterate_hashtable(KeyValue* pHashTable)
{
uint32_t* device_num_kvs;
cudaMalloc(&device_num_kvs, sizeof(uint32_t));
cudaMemset(device_num_kvs, 0, sizeof(uint32_t));
KeyValue* device_kvs;
cudaMalloc(&device_kvs, sizeof(KeyValue) * kNumKeyValues);
int mingridsize;
int threadblocksize;
cudaOccupancyMaxPotentialBlockSize(&mingridsize, &threadblocksize, gpu_iterate_hashtable, 0, 0);
int gridsize = (kHashTableCapacity + threadblocksize - 1) / threadblocksize;
gpu_iterate_hashtable<<<gridsize, threadblocksize>>>(pHashTable, device_kvs, device_num_kvs);
uint32_t num_kvs;
cudaMemcpy(&num_kvs, device_num_kvs, sizeof(uint32_t), cudaMemcpyDeviceToHost);
std::vector<KeyValue> kvs;
kvs.resize(num_kvs);
cudaMemcpy(kvs.data(), device_kvs, sizeof(KeyValue) * num_kvs, cudaMemcpyDeviceToHost);
cudaFree(device_kvs);
cudaFree(device_num_kvs);
return kvs;
}
// Free the memory of the hashtable
void destroy_hashtable(KeyValue* pHashTable)
{
cudaFree(pHashTable);
}