-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashtable.cu
More file actions
109 lines (90 loc) · 2.45 KB
/
Hashtable.cu
File metadata and controls
109 lines (90 loc) · 2.45 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
//%%cuda -c "-I /does/not/exist -arch=sm_75"
#include <cuda_runtime.h>
#include <cstdint>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define TABLE_SIZE 1048576
typedef uint64_t Fingerprint;
__global__ void CudaHashInsert(Fingerprint* d_fingerprints, Fingerprint* d_hashTable, bool *d_isDuplicate, int numberOfItems)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx >= numberOfItems) return;
Fingerprint fp = d_fingerprints[idx];
uint32_t slot = fp % TABLE_SIZE;
//Linear probing
for(int i = 0; i < 8; i++)
{
uint32_t probeSlot = (slot + i) % TABLE_SIZE;
if(d_hashTable[probeSlot] == 0)
{
d_hashTable[probeSlot] = fp;
d_isDuplicate[idx] = false;
return;
}
else if(d_hashTable[probeSlot] == fp)
{
d_isDuplicate[idx] = true;
return;
}
}
d_isDuplicate[idx] = true;
}
void CudaDeduplicate(Fingerprint* h_fingerprints, bool* h_results, int n)
{
Fingerprint *d_fingerprints, *d_table;
bool *d_results;
cudaMalloc(&d_fingerprints, n * sizeof(Fingerprint));
cudaMalloc(&d_results, n * sizeof(bool));
cudaMalloc(&d_table, TABLE_SIZE * sizeof(Fingerprint));
cudaMemcpy(d_fingerprints, h_fingerprints, n * sizeof(Fingerprint), cudaMemcpyHostToDevice);
cudaMemset(d_table, 0, TABLE_SIZE * sizeof(Fingerprint));
int threads = 256;
int blocks = (n + threads - 1) / threads;
CudaHashInsert<<<blocks, threads>>>(d_fingerprints, d_table, d_results, n);
cudaMemcpy(h_results, d_results, n * sizeof(bool), cudaMemcpyDeviceToHost);
cudaFree(d_fingerprints);
cudaFree(d_results);
cudaFree(d_table);
}
int main()
{
int n = 1000000;
Fingerprint* fps = (Fingerprint*)malloc(n * sizeof(Fingerprint));
bool* results = (bool*)malloc(n * sizeof(bool));
srand(time(NULL));
//Create test data
for(int i = 0; i < n; i++)
{
if (i % 5 == 0 && i > 0)
{
//Duplicate
fps[i] = fps[i-1];
}
else
{
// New unique value
fps[i] = ((uint64_t)rand() << 32) | rand();
}
}
clock_t start = clock();
CudaDeduplicate(fps, results, n);
clock_t end = clock();
// Count results
int unique = 0, duplicates = 0;
for(int i = 0; i < n; i++)
{
if(results[i]) duplicates++;
else unique++;
}
printf("Unique: %d, Duplicates: %d\n", unique, duplicates);
printf("Time: %.3f seconds\n", (double)(end - start) / CLOCKS_PER_SEC);
printf("\nFirst 10 results:\n");
for(int i = 0; i < 10; i++)
{
printf("Item %d: FP=%lu, Duplicate=%s\n", i, fps[i], results[i] ? "YES" : "NO");
}
free(fps);
free(results);
return 0;
}