-
Notifications
You must be signed in to change notification settings - Fork 18
/
warp_no_heap_astar_accelerator.h
145 lines (128 loc) · 4.13 KB
/
warp_no_heap_astar_accelerator.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
#pragma once
#include"data.h"
#include<vector>
#include"config.h"
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<cuda_runtime.h>
#include"cublas_v2.h"
#include"smmh2.h"
#include"bin_heap.h"
#include"bloomfilter.h"
#include"vanilla_list.h"
#define FULL_MASK 0xffffffff
#define N_THREAD_IN_WARP 32
__global__
void warp_independent_search_kernel(value_t* d_data,value_t* d_query,idx_t* d_result,idx_t* d_graph,int num_query,int vertex_offset_shift){
#define DIM 784
int bid = blockIdx.x;
const int step = N_THREAD_IN_WARP;
if(bid >= num_query)
return;
int tid = threadIdx.x;
//BloomFilter<256,8,7> bf;
//BloomFilter<128,7,7> bf;
//BloomFilter<64,6,7>* pbf;
//BloomFilter<64,6,3> bf;
//VanillaList bf;
//KernelPair<dist_t,idx_t>* q;
//KernelPair<dist_t,idx_t>* topk;
value_t* dist_list;
if(tid == 0){
dist_list = new value_t[FIXED_DEGREE];
// q = new KernelPair<dist_t,idx_t>[QUEUE_SIZE + 2];
// topk = new KernelPair<dist_t,idx_t>[TOPK + 1];
// pbf = new BloomFilter<64,6,7>();
}
__shared__ value_t query_point[DIM];
__shared__ KernelPair<dist_t,idx_t> now;
__shared__ bool finished;
value_t start_distance;
__syncthreads();
value_t tmp = 0;
for(int i = tid;i < DIM;i += step){
query_point[i] = d_query[bid * DIM + i];
value_t diff = query_point[i] - d_data[i];
tmp += diff * diff;
}
for (int offset = 16; offset > 0; offset /= 2)
tmp += __shfl_down_sync(FULL_MASK, tmp, offset);
if(tid == 0)
start_distance = tmp;
__syncthreads();
if(tid == 0){
dist_t d = start_distance;
now.first = d;
now.second = 0;
finished = false;
}
__syncthreads();
while(!finished){
auto offset = now.second << vertex_offset_shift;
int degree = d_graph[offset];
for(int i = 0;i < degree;++i){
//TODO: replace this atomic with reduction in CUB
value_t tmp = 0;
for(int j = tid;j < DIM;j += step){
value_t diff = query_point[j] - d_data[d_graph[offset + i + 1] * DIM + j];
tmp += diff * diff;
}
for (int offset = 16; offset > 0; offset /= 2)
tmp += __shfl_down_sync(FULL_MASK, tmp, offset);
if(tid == 0)
dist_list[i] = tmp;
}
__syncthreads();
if(tid == 0){
finished = true;
for(int i = 0;i < degree;++i){
dist_t d = dist_list[i];
if(now.first > d){
now.first = d;
now.second = d_graph[offset + i + 1];
finished = false;
}
}
}
__syncthreads();
}
if(tid == 0){
d_result[bid] = now.second;
delete[] dist_list;
}
}
class WarpNoHeapAStarAccelerator{
private:
public:
static void astar_multi_start_search_batch(const std::vector<std::vector<std::pair<int,value_t>>>& queries,int k,std::vector<std::vector<idx_t>>& results,value_t* h_data,idx_t* h_graph,int vertex_offset_shift,int num,int dim){
value_t* d_data;
value_t* d_query;
idx_t* d_result;
idx_t* d_graph;
std::unique_ptr<value_t[]> h_query = std::unique_ptr<value_t[]>(new value_t[queries.size() * dim]);
memset(h_query.get(),0,sizeof(value_t) * queries.size() * dim);
for(int i = 0;i < queries.size();++i){
for(auto p : queries[i]){
*(h_query.get() + i * dim + p.first) = p.second;
}
}
std::unique_ptr<idx_t[]> h_result = std::unique_ptr<idx_t[]>(new idx_t[queries.size() * TOPK]);
cudaMalloc(&d_data,sizeof(value_t) * num * dim);
cudaMalloc(&d_query,sizeof(value_t) * queries.size() * dim);
cudaMalloc(&d_result,sizeof(idx_t) * queries.size() * TOPK);
cudaMalloc(&d_graph,sizeof(idx_t) * (num << vertex_offset_shift));
cudaMemcpy(d_data,h_data,sizeof(value_t) * num * dim,cudaMemcpyHostToDevice);
cudaMemcpy(d_query,h_query.get(),sizeof(value_t) * queries.size() * dim,cudaMemcpyHostToDevice);
cudaMemcpy(d_graph,h_graph,sizeof(idx_t) * (num << vertex_offset_shift),cudaMemcpyHostToDevice);
warp_independent_search_kernel<<<queries.size(),32>>>(d_data,d_query,d_result,d_graph,queries.size(),vertex_offset_shift);
cudaMemcpy(h_result.get(),d_result,sizeof(idx_t) * queries.size() * TOPK,cudaMemcpyDeviceToHost);
results.clear();
for(int i = 0;i < queries.size();++i){
std::vector<idx_t> v(TOPK);
for(int j = 0;j < TOPK;++j)
v[j] = h_result[i * TOPK + j];
results.push_back(v);
}
}
};