-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnnsearch.h
255 lines (216 loc) · 7.17 KB
/
nnsearch.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
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
/*
* Copyright (C) 2015-2018, Nils Moehrle
* All rights reserved.
*
* This software may be modified and distributed under the terms
* of the BSD 3-Clause license. See the LICENSE.txt file for details.
*/
#ifndef CACC_NNSEARCH_HEADER
#define CACC_NNSEARCH_HEADER
#include "kd_tree.h"
CACC_NAMESPACE_BEGIN
#define NNSEARCH_NAMESPACE_BEGIN namespace nnsearch {
#define NNSEARCH_NAMESPACE_END }
NNSEARCH_NAMESPACE_BEGIN
//NNSEARCH_SSTACK_SIZE * NNSEARCH_BLOCK_SIZE has to be less than smem
#ifndef NNSEARCH_BLOCK_SIZE
#define NNSEARCH_BLOCK_SIZE 128
#endif
#ifndef NNSEARCH_SSTACK_SIZE
#define NNSEARCH_SSTACK_SIZE 6
#endif
#ifndef NNSEARCH_GSTACK_SIZE
#define NNSEARCH_GSTACK_SIZE 64
#endif
constexpr uint NAI = (uint) -1;
constexpr float lowest = std::numeric_limits<float>::lowest();
constexpr float inf = std::numeric_limits<float>::infinity();
__device__ __forceinline__
uint
max_element(float const * values, uint n, uint const stride)
{
float max = lowest;
uint ret = (uint) -1;
for (int i = 0; i < n; ++i) {
float v = values[i * stride];
if (v > max) {
max = v;
ret = i;
}
}
return ret;
}
__device__ __forceinline__
float
max_value(float const * values, uint n, uint const stride)
{
float max = lowest;
for (int i = 0; i < n; ++i) {
float v = values[i * stride];
if (v > max) {
max = v;
}
}
return max;
}
template <uint K>
__device__
bool find_nn(typename cacc::KDTree<K, cacc::DEVICE>::Accessor const & kd_tree,
typename cacc::KDTree<K, cacc::DEVICE>::Vertex vertex,
uint * idx_ptr, float * dist_ptr)
{
#if NNSEARCH_SSTACK_SIZE
const int tx = threadIdx.x;
const int ty = threadIdx.y;
const int id = ty * blockDim.x + tx;
uint __shared__ sstack[NNSEARCH_SSTACK_SIZE * NNSEARCH_BLOCK_SIZE];
#endif
uint idx = NAI;
float max_dist = inf;
uint node_idx = 0;
bool down = true;
uint gstack[NNSEARCH_GSTACK_SIZE];
int stack_idx = -1;
while (true) {
typename KDTree<K, DEVICE>::Node node;
node = kd_tree.load_node(node_idx);
typename KDTree<K, DEVICE>::Vertex vert;
vert = kd_tree.load_vertex(node.vid);
float diff = vertex[node.dim] - vert[node.dim];
if (down) {
float dist = norm(vertex - vert);
if (dist < max_dist) {
idx = node.vid;
max_dist = dist;
}
if (node.left != NAI || node.right != NAI) {
/* Inner node - traverse further down. */
down = true;
if (node.left != NAI && node.right != NAI) {
#if NNSEARCH_SSTACK_SIZE
if (++stack_idx < NNSEARCH_SSTACK_SIZE)
sstack[NNSEARCH_BLOCK_SIZE * stack_idx + id] = node_idx;
else gstack[stack_idx - NNSEARCH_SSTACK_SIZE] = node_idx;
#else
gstack[++stack_idx] = node_idx;
#endif
}
float diff = vertex[node.dim] - vert[node.dim];
uint next = (diff < 0.0f) ? node.left : node.right;
uint other = (diff < 0.0f) ? node.right : node.left;
node_idx = (next != NAI) ? next : other;
} else {
/* Leaf - traverse up and search for next node. */
down = false;
node_idx = NAI;
}
} else {
if (std::abs(diff) < max_dist) {
down = true;
node_idx = (diff < 0.0f) ? node.right : node.left;
} else {
down = false;
node_idx = NAI;
}
}
if (node_idx == NAI) {
if (stack_idx < 0) break;
#if NNSEARCH_SSTACK_SIZE
if (stack_idx < NNSEARCH_SSTACK_SIZE)
node_idx = sstack[NNSEARCH_BLOCK_SIZE * stack_idx-- + id];
else node_idx = gstack[stack_idx-- - NNSEARCH_SSTACK_SIZE];
#else
node_idx = gstack[stack_idx--];
#endif
}
}
if (idx == NAI) return false;
if (idx_ptr != nullptr) *idx_ptr = idx;
if (dist_ptr != nullptr) *dist_ptr = max_dist;
return true;
}
template <uint K>
__device__
uint find_nns(typename cacc::KDTree<K, cacc::DEVICE>::Accessor const & kd_tree,
typename cacc::KDTree<K, cacc::DEVICE>::Vertex vertex,
uint * idxs_ptr, float * dists_ptr, uint k, uint const stride = 1u)
{
#if NNSEARCH_SSTACK_SIZE
const int tx = threadIdx.x;
const int ty = threadIdx.y;
const int id = ty * blockDim.x + tx;
uint __shared__ sstack[NNSEARCH_SSTACK_SIZE * NNSEARCH_BLOCK_SIZE];
#endif
uint n = 0;
float max_dist = inf;
uint node_idx = 0;
bool down = true;
uint gstack[NNSEARCH_GSTACK_SIZE];
int stack_idx = -1;
while (true) {
typename KDTree<K, DEVICE>::Node node;
node = kd_tree.load_node(node_idx);
typename KDTree<K, DEVICE>::Vertex vert;
vert = kd_tree.load_vertex(node.vid);
float diff = vertex[node.dim] - vert[node.dim];
if (down) {
float dist = norm(vertex - vert);
if (dist < max_dist) {
if (n < k) {
idxs_ptr[n * stride] = node.vid;
dists_ptr[n * stride] = dist;
n += 1;
} else {
uint i = max_element(dists_ptr, n, stride);
idxs_ptr[i * stride] = node.vid;
dists_ptr[i * stride] = dist;
}
if (n == k) {
max_dist = max_value(dists_ptr, n, stride);
}
}
if (node.left != NAI || node.right != NAI) {
/* Inner node - traverse further down. */
down = true;
if (node.left != NAI && node.right != NAI) {
#if NNSEARCH_SSTACK_SIZE
if (++stack_idx < NNSEARCH_SSTACK_SIZE)
sstack[NNSEARCH_BLOCK_SIZE * stack_idx + id] = node_idx;
else gstack[stack_idx - NNSEARCH_SSTACK_SIZE] = node_idx;
#else
gstack[++stack_idx] = node_idx;
#endif
}
float diff = vertex[node.dim] - vert[node.dim];
uint next = (diff < 0.0f) ? node.left : node.right;
uint other = (diff < 0.0f) ? node.right : node.left;
node_idx = (next != NAI) ? next : other;
} else {
/* Leaf - traverse up and search for next node. */
down = false;
node_idx = NAI;
}
} else {
if (std::abs(diff) < max_dist) {
down = true;
node_idx = (diff < 0.0f) ? node.right : node.left;
} else {
down = false;
node_idx = NAI;
}
}
if (node_idx == NAI) {
if (stack_idx < 0) break;
#if NNSEARCH_SSTACK_SIZE
if (stack_idx < NNSEARCH_SSTACK_SIZE) node_idx = sstack[NNSEARCH_BLOCK_SIZE * stack_idx-- + id];
else node_idx = gstack[stack_idx-- - NNSEARCH_SSTACK_SIZE];
#else
node_idx = gstack[stack_idx--];
#endif
}
}
return n;
}
NNSEARCH_NAMESPACE_END
CACC_NAMESPACE_END
#endif /* CACC_NNSEARCH_HEADER */