-
Notifications
You must be signed in to change notification settings - Fork 7
/
gpuFeatureMatcher.cuh
410 lines (348 loc) · 14.1 KB
/
gpuFeatureMatcher.cuh
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
399
400
401
402
403
404
405
406
407
408
409
410
#include <iostream>
#include <cassert>
#include "cpuFeatureMatcher.h"
#include <vector>
#include <set>
#define BLOCK_SIZE 192
/*
*Each thread is in charge of computing one element in distanceMat
*/
__global__ void kernelDistanceMat(const Matrix<float> descriptors, Matrix<float> distanceMat) {
int matX = blockDim.x * blockIdx.x + threadIdx.x;
int matY = blockIdx.y;
int n = descriptors.height;
int k = descriptors.width;
if (matX >= n) { return; }
float ssd = 0; // sum of squared distances
for (int i = 0; i < k; ++i) {
float dist = descriptors.elements[matY * k + i] -
descriptors.elements[matX * k + i];
ssd += dist * dist;
}
distanceMat.elements[matY * n + matX] = sqrtf(ssd);
}
/*
*See CPU-equivalent.
*Pre-conditions:
* `descriptors` and `distanceMat` are in GPU memory
*/
void gpuComputeDistanceMat(const Matrix<float> descriptors, Matrix<float> distanceMat) {
assert(descriptors.height == distanceMat.height);
assert(distanceMat.height == distanceMat.width);
int n = descriptors.height;
dim3 dimGrid((n + BLOCK_SIZE - 1)/BLOCK_SIZE, n);
dim3 dimBlock(BLOCK_SIZE);
kernelDistanceMat<<<dimGrid,dimBlock>>>(descriptors, distanceMat);
}
/*
*See getRMSE()
*Pre-conditions:
* A is in host memory and BDevice is in device memory.
*/
template <typename T>
float getRMSEHostAndDevice(const Matrix<T> A, const Matrix<T> BDevice) {
assert(A.height == BDevice.height);
assert(A.width == BDevice.width);
Matrix<T> BHost = AllocateMatrix<T>(BDevice.height, BDevice.width, 0);
CopyFromDeviceMatrix<T>(BHost, BDevice);
float rmse = getRMSE(A, BHost);
FreeMatrix(&BHost);
return rmse;
}
/*
*See gpuComputeCorrespondenceVec()
*Pre-conditions:
* Every thread is responsible for computing one element in
* `correspondenceMat`
*/
__global__ void gpuComputeCorrespondenceVec(
const Matrix<float> distanceSubmat,
Matrix<int> correspondenceMat, float matchConfidence) {
int n = distanceSubmat.width; // no. of descriptors matched from
int n_i = distanceSubmat.height; // no. of descriptors matched to
int tid = blockDim.x * blockIdx.x + threadIdx.x;
if (tid >= n) { return; }
float dist1 = FLT_MAX, dist2 = FLT_MAX; // smallest & 2nd smallest distance
int idx1 = -1; // index of match with smallest distance
for (int j = 0; j < n_i; ++j) {
float currDist = distanceSubmat.elements[j * n + tid];
if (currDist < dist1) {
dist2 = dist1;
dist1 = currDist;
idx1 = j;
}
else if (currDist < dist2) {
dist2 = currDist;
}
}
if (dist1 / dist2 < 1 - matchConfidence) {
correspondenceMat.elements[tid] = idx1;
}
else {
correspondenceMat.elements[tid] = -1;
}
}
/*
*See computeCorrespondenceMat()
*Pre-conditions:
* element member in `distanceMat` and `correspondenceMat` point to device memory
* `cumNumDescriptors` is in host memory
*/
void gpuComputeCorrespondenceMat(const Matrix<float> distanceMat,
int* cumNumDescriptors,
int numImages, Matrix<int> correspondenceMat, float matchConfidence) {
assert(distanceMat.height == distanceMat.width);
assert(correspondenceMat.height == numImages);
assert(correspondenceMat.width == distanceMat.width);
assert(cumNumDescriptors[numImages-1] == distanceMat.height);
int n = distanceMat.height;
dim3 dimGrid((n + BLOCK_SIZE - 1)/BLOCK_SIZE, n);
dim3 dimBlock(BLOCK_SIZE);
for (int imgIdx = 0; imgIdx < numImages; ++imgIdx) {
int start = imgIdx > 0 ? cumNumDescriptors[imgIdx-1] : 0;
int stop = cumNumDescriptors[imgIdx];
Matrix<float> src = getSubmatrix<float>(distanceMat, start, stop);
Matrix<int> dst = getSubmatrix<int>(correspondenceMat, imgIdx, imgIdx + 1);
gpuComputeCorrespondenceVec<<<dimGrid,dimBlock>>>(src, dst, matchConfidence);
}
}
// See CPU-equivalent.
__device__ float gpuComputeL2Distance(float* descriptor1, float* descriptor2, int length) {
float ssd = 0; // sum of squared distances
for (int i = 0; i < length; ++i) {
float dist = descriptor1[i] - descriptor2[i];
ssd += dist * dist;
}
return sqrtf(ssd);
}
/*
*Kernel to compute correspondence directly from descriptors.
*A thread with thread ID tid computes the correspondence of descriptor[tid][:]
*to the range of descriptor[idxToStart:idxToStop][:] (excludes idxToStop).
*It assigns this correspondence to correspondenceVec[tid]
*Pre-conditions:
* element member of `descriptors` and `correspondenceVec` point to device memory
* `descriptors` is a n x k matrix
* `correspondenceVec` is a 1 x n matrix
* idxToStart < idxToStop
* 0 <= idxToStart, idxToStop <= n
*Post-conditions:
* Assigns values to correspondenceVec
*/
__global__ void kernelComputeCorrespondence(
const Matrix<float> descriptors,
Matrix<int> correspondenceVec,
int idxToStart, int idxToStop, float matchConfidence) {
int n = descriptors.height;
int k = descriptors.width;
assert(correspondenceVec.height == 1);
assert(correspondenceVec.width == n);
assert(idxToStart < idxToStop);
assert(0 <= idxToStart && idxToStart < n);
assert(0 < idxToStop && idxToStop <= n);
int tid = blockDim.x * blockIdx.x + threadIdx.x;
if (tid >= n) { return; }
float dist1 = FLT_MAX, dist2 = FLT_MAX; // smallest & 2nd smallest distance
int idx1 = -1; // index of match with smallest distance
float* descriptorFrom = descriptors.elements + tid * k;
for (int j = idxToStart; j < idxToStop; ++j) {
float* descriptorTo = descriptors.elements + j * k;
float currDist = gpuComputeL2Distance(descriptorFrom, descriptorTo, k);
if (currDist < dist1) {
dist2 = dist1;
dist1 = currDist;
// Decrement by idxToStart because we want correspondence to start from 0
// i.e. treat descriptors[idxToStart:idxToStop] as the descriptors
// 0 to idxToStop-idxToStart-1 of the 'from' image
idx1 = j - idxToStart;
}
else if (currDist < dist2) {
dist2 = currDist;
}
}
int correspondence = (dist1 / dist2 < 1 - matchConfidence) ? idx1 : -1;
correspondenceVec.elements[tid] = correspondence;
}
/*
*See kernelComputeCorrespondence()
*This version moves one 'to' descriptor to shared memory per iteration of computing
*distance between the 'from' descriptor 'from' and a 'to' descriptor, to reduce
*trips to global memory
*Additional pre-conditions:
* blockDim.x >= descriptors.width
* shmem size = k * sizeof(float)
*/
__global__ void kernelComputeCorrespondence2(
const Matrix<float> descriptors,
Matrix<int> correspondenceVec,
int idxToStart, int idxToStop, float matchConfidence) {
extern __shared__ float shmem[];
int n = descriptors.height;
int k = descriptors.width;
assert(correspondenceVec.height == 1);
assert(correspondenceVec.width == n);
assert(idxToStart < idxToStop);
assert(0 <= idxToStart && idxToStart < n);
assert(0 < idxToStop && idxToStop <= n);
assert(blockDim.x <= k);
// We don't immediately return threads with ID >= n because we use them to
// bring data into shared memory in the for-loop
int tid = blockDim.x * blockIdx.x + threadIdx.x;
float dist1 = FLT_MAX, dist2 = FLT_MAX; // smallest & 2nd smallest distance
int idx1 = -1; // index of match with smallest distance
float* descriptorFrom = descriptors.elements + tid * k;
for (int j = idxToStart; j < idxToStop; ++j) {
// Bring current descriptorTo to shared memory to reduce trips to
// global memory
if (threadIdx.x < k) {
shmem[threadIdx.x] = *(descriptors.elements + j * k + threadIdx.x);
}
__syncthreads();
if (tid >= n) { continue; }
float currDist = gpuComputeL2Distance(descriptorFrom, shmem, k);
if (currDist < dist1) {
dist2 = dist1;
dist1 = currDist;
// Decrement by idxToStart because we want correspondence to start from 0
// i.e. treat descriptors[idxToStart:idxToStop] as the descriptors
// 0 to idxToStop-idxToStart-1 of the 'from' image
idx1 = j - idxToStart;
}
else if (currDist < dist2) {
dist2 = currDist;
}
}
if (tid < n) {
int correspondence = (dist1 / dist2 < 1 - matchConfidence) ? idx1 : -1;
correspondenceVec.elements[tid] = correspondence;
}
}
/*
*See gpuComputeCorrespondenceMatFromDescriptors()
*/
void gpuComputeCorrespondenceMatFromDescriptors2(
const Matrix<float> descriptors,
int* cumNumDescriptors,
int numImages, Matrix<int> correspondenceMat, float matchConfidence) {
assert(correspondenceMat.height == numImages);
assert(correspondenceMat.width == descriptors.height);
assert(cumNumDescriptors[numImages-1] == descriptors.height);
int n = descriptors.height;
int k = descriptors.width;
dim3 dimGrid((n + BLOCK_SIZE - 1)/BLOCK_SIZE);
dim3 dimBlock(BLOCK_SIZE);
size_t shmem_size = k * sizeof(float);
for (int imgIdx = 0; imgIdx < numImages; ++imgIdx) {
int idxToStart = imgIdx > 0 ? cumNumDescriptors[imgIdx-1] : 0;
int idxToStop = cumNumDescriptors[imgIdx];
Matrix<int> correspondenceVec = getSubmatrix(correspondenceMat,
imgIdx, imgIdx+1);
kernelComputeCorrespondence<<<dimGrid,dimBlock,shmem_size>>>(
descriptors, correspondenceVec,
idxToStart, idxToStop, matchConfidence);
}
}
/*
*Space-optimal feature matcher. Computes correspondence matrix directly from
*descriptors.
*A kernel is launched for every row in correspondenceMat. Every thread
*computes one element in correspondenceMat.
*See CPU-equivalent.
*Pre-conditions:
* element member of `descriptors` and `correspondenceMat` point to device memory
* `cumNumDescriptors` points to host memory
*/
void gpuComputeCorrespondenceMatFromDescriptors(
const Matrix<float> descriptors,
int* cumNumDescriptors,
int numImages, Matrix<int> correspondenceMat, float matchConfidence) {
assert(correspondenceMat.height == numImages);
assert(correspondenceMat.width == descriptors.height);
assert(cumNumDescriptors[numImages-1] == descriptors.height);
int n = descriptors.height;
dim3 dimGrid((n + BLOCK_SIZE - 1)/BLOCK_SIZE);
dim3 dimBlock(BLOCK_SIZE);
for (int imgIdx = 0; imgIdx < numImages; ++imgIdx) {
int idxToStart = imgIdx > 0 ? cumNumDescriptors[imgIdx-1] : 0;
int idxToStop = cumNumDescriptors[imgIdx];
Matrix<int> correspondenceVec = getSubmatrix(correspondenceMat,
imgIdx, imgIdx+1);
kernelComputeCorrespondence<<<dimGrid,dimBlock>>>(
descriptors, correspondenceVec,
idxToStart, idxToStop, matchConfidence);
}
}
/*
*Differs from kernelComputeCorrespondence in that `descriptors`
*is now k x n instead of n x k. In other, here, a descriptor is a column
*vector rather than a row.
*/
__global__ void kernelComputeCorrespondenceFromColumnwiseDescriptors(
const Matrix<float> descriptors,
Matrix<int> correspondenceVec,
int idxToStart, int idxToStop, float matchConfidence) {
int k = descriptors.height;
int n = descriptors.width;
assert(correspondenceVec.height == 1);
assert(correspondenceVec.width == n);
assert(idxToStart < idxToStop);
assert(0 <= idxToStart && idxToStart < n);
assert(0 < idxToStop && idxToStop <= n);
int tid = blockDim.x * blockIdx.x + threadIdx.x;
if (tid >= n) { return; }
float dist1 = FLT_MAX, dist2 = FLT_MAX; // smallest & 2nd smallest distance
int idx1 = -1; // index of match with smallest distance
float* descriptorFrom = descriptors.elements + tid;
for (int j = idxToStart; j < idxToStop; ++j) {
// Calculate L2 distance between the 'to' descriptor and 'from'
// descriptor. Note that the descriptors are column vectors and so we
// increment by n = the width of `descriptors`, to get to the next
// element in the column
float* descriptorTo = descriptors.elements + j;
float ssd = 0; // sum of squared distances
for (int i = 0; i < k; ++i) {
float dist = descriptorFrom[i * n] - descriptorTo[i * n];
ssd += dist * dist;
}
float currDist = sqrtf(ssd);
if (currDist < dist1) {
dist2 = dist1;
dist1 = currDist;
// Decrement by idxToStart because we want correspondence to start from 0
// i.e. treat descriptors[idxToStart:idxToStop] as the descriptors
// 0 to idxToStop-idxToStart-1 of the 'from' image
idx1 = j - idxToStart;
}
else if (currDist < dist2) {
dist2 = currDist;
}
}
int correspondence = (dist1 / dist2 < 1 - matchConfidence) ? idx1 : -1;
correspondenceVec.elements[tid] = correspondence;
}
/*
*Computes m x n correspondence matrix from a k x n descriptor, attempting to
*improve memory coalescence.
*See gpuComputeCorrespondenceMatFromDescriptors.
*/
void gpuComputeCorrespondenceMatFromColumnwiseDescriptors(
const Matrix<float> descriptors,
int* cumNumDescriptors,
int numImages, Matrix<int> correspondenceMat, float matchConfidence) {
assert(correspondenceMat.height == numImages);
assert(correspondenceMat.width == descriptors.width);
assert(cumNumDescriptors[numImages-1] == descriptors.width);
int n = descriptors.width;
dim3 dimGrid((n + BLOCK_SIZE - 1)/BLOCK_SIZE);
dim3 dimBlock(BLOCK_SIZE);
for (int imgIdx = 0; imgIdx < numImages; ++imgIdx) {
int idxToStart = imgIdx > 0 ? cumNumDescriptors[imgIdx-1] : 0;
int idxToStop = cumNumDescriptors[imgIdx];
Matrix<int> correspondenceVec = getSubmatrix(correspondenceMat,
imgIdx, imgIdx+1);
kernelComputeCorrespondenceFromColumnwiseDescriptors
<<<dimGrid,dimBlock>>>(
descriptors, correspondenceVec,
idxToStart, idxToStop, matchConfidence);
}
}