-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatching.cu
725 lines (693 loc) · 22.3 KB
/
matching.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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
#include "cudaSift.h"
#include "cudautils.h"
//================= Device matching functions =====================//
__global__ void MatchSiftPoints(SiftPoint *sift1, SiftPoint *sift2, float *corrData, int numPts1, int numPts2)
{
__shared__ float siftPoint[128];
__shared__ float sums[16*16];
const int tx = threadIdx.x;
const int ty = threadIdx.y;
const int p1 = blockIdx.x;
const int p2 = blockIdx.y*16 + ty;
const float *ptr1 = sift1[p1].data;
const float *ptr2 = sift2[p2].data;
const int i = 16*ty + tx;
if (ty<8)
siftPoint[i] = ptr1[i];
__syncthreads();
float sum = 0.0f;
if (p2<numPts2)
for (int j=0;j<8;j++)
sum += siftPoint[16*j+tx] * ptr2[16*j+tx];
sums[i] = sum;
__syncthreads();
if (tx<8)
sums[i] += sums[i+8];
__syncthreads();
if (tx<4)
sums[i] += sums[i+4];
__syncthreads();
if (ty==0) {
sum = sums[16*tx+0] + sums[16*tx+1] + sums[16*tx+2] + sums[16*tx+3];
corrData[p1*gridDim.y*16 + blockIdx.y*16 + tx] = sum;
}
__syncthreads();
}
__global__ void MatchSiftPoints2(SiftPoint *sift1, SiftPoint *sift2, float *corrData, int numPts1, int numPts2)
{
__shared__ float siftPoints1[16*128];
__shared__ float siftPoints2[16*128];
const int tx = threadIdx.x;
const int ty = threadIdx.y;
const float *ptr1 = sift1[min(numPts1-1,blockIdx.x*16 + ty)].data;
const float *ptr2 = sift2[min(numPts2-1,blockIdx.y*16 + ty)].data;
for (int i=0;i<8;i++) {
siftPoints1[128*ty+16*i+tx] = ptr1[16*i+tx];
siftPoints2[128*ty+16*i+tx] = ptr2[16*i+tx];
}
__syncthreads();
const int p1 = blockIdx.x*16 + ty;
const int p2 = blockIdx.y*16 + tx;
const float *pt1 = &siftPoints1[ty*128];
const float *pt2 = &siftPoints2[tx*128];
float sum = 0.0f;
for (int i=0;i<128;i++) {
int itx = (i + tx)&127; // avoid bank conflicts
sum += pt1[itx]*pt2[itx];
}
if (p1<numPts1)
corrData[p1*gridDim.y*16 + p2] = (p2<numPts2 ? sum : -1.0f);
}
__global__ void FindMaxCorr(float *corrData, SiftPoint *sift1, SiftPoint *sift2, int numPts1, int corrWidth, int siftSize)
{
__shared__ float maxScore[16*16];
__shared__ float maxScor2[16*16];
__shared__ int maxIndex[16*16];
const int tx = threadIdx.x;
const int ty = threadIdx.y;
const int idx = ty*16 + tx;
int p1 = blockIdx.x*16 + threadIdx.y;
p1 = (p1>=numPts1 ? numPts1-1 : p1);
maxScore[idx] = -1.0f;
maxScor2[idx] = -1.0f;
maxIndex[idx] = -1;
__syncthreads();
float *corrs = &corrData[p1*corrWidth];
for (int i=tx;i<corrWidth;i+=16) {
float val = corrs[i];
if (val>maxScore[idx]) {
maxScor2[idx] = maxScore[idx];
maxScore[idx] = val;
maxIndex[idx] = i;
} else if (val>maxScor2[idx])
maxScor2[idx] = val;
}
__syncthreads();
for (int len=8;len>0;len/=2) {
if (tx<8) {
float val = maxScore[idx+len];
int i = maxIndex[idx+len];
if (val>maxScore[idx]) {
maxScor2[idx] = maxScore[idx];
maxScore[idx] = val;
maxIndex[idx] = i;
} else if (val>maxScor2[idx])
maxScor2[idx] = val;
float va2 = maxScor2[idx+len];
if (va2>maxScor2[idx])
maxScor2[idx] = va2;
}
__syncthreads();
}
if (tx==0) {
sift1[p1].score = maxScore[ty*16];
sift1[p1].ambiguity = maxScor2[ty*16] / (maxScore[ty*16] + 1e-6);
sift1[p1].match = maxIndex[ty*16];
sift1[p1].match_xpos = sift2[maxIndex[ty*16]].xpos;
sift1[p1].match_ypos = sift2[maxIndex[ty*16]].ypos;
}
}
// Version based on suggestion by Nicholas Lin
__global__ void FindMaxCorr3(float *corrData, SiftPoint *sift1, SiftPoint *sift2, int numPts1, int numPts2)
{
int block_dim = blockDim.x; // blockDim.x == 16
const int tx = threadIdx.x;
const int ty = threadIdx.y;
const int p1 = blockIdx.x * block_dim + ty;
const int idx = ty * 16 + tx;
__shared__ int maxIndex[16 * 16];
maxIndex[idx] = 0;
__syncthreads();
float *corrs = NULL;
if (p1 < numPts1) {
corrs = &corrData[p1 * block_dim * 2];
corrs[tx] = 0.0f;
corrs[tx + 16] = 0.0f;
const float *pt1 = sift1[p1].data;
for (int p2 = tx; p2 < numPts2; p2 += 16) {
float *pt2 = sift2[p2].data;
float sum = 0.0f;
for (int i = 0; i < 128; i++)
sum += pt1[i] * pt2[i];
if (sum > corrs[tx]) {
corrs[tx + 16] = corrs[tx];
corrs[tx] = sum;
maxIndex[idx] = p2;
}
else if (sum > corrs[tx + 16])
corrs[tx + 16] = sum;
}
}
__syncthreads();
if (p1 < numPts1) {
for (int len = 8; len > 0; len /= 2) {
if (tx < len) {
float val = corrs[tx + len];
int i = maxIndex[idx + len];
if (val > corrs[tx]) {
corrs[tx + 16] = corrs[tx];
corrs[tx] = val;
maxIndex[idx] = i;
}
else if (val > corrs[tx + 16])
corrs[tx + 16] = val;
float va2 = corrs[tx + 16 + len];
if (va2 > corrs[tx + 16])
corrs[tx + 16] = va2;
}
__syncthreads();
}
if (tx==0) {
sift1[p1].score = corrs[0];
sift1[p1].ambiguity = corrs[16] / (corrs[0] + 1e-6);
sift1[p1].match = maxIndex[ty << 4];
sift1[p1].match_xpos = sift2[maxIndex[ty << 4]].xpos;
sift1[p1].match_ypos = sift2[maxIndex[ty << 4]].ypos;
}
}
}
#define FMC2W 16
#define FMC2H 4
__global__ void FindMaxCorr2(SiftPoint *sift1, SiftPoint *sift2, int numPts1, int numPts2)
{
__shared__ float siftPoint[128];
__shared__ float maxScore[FMC2H];
__shared__ float maxScor2[FMC2H];
__shared__ int maxIndex[FMC2H];
const int p1 = blockIdx.x;
if (p1>=numPts1)
return;
const int tx = threadIdx.x;
const int ty = threadIdx.y;
const int idx = ty*FMC2W + tx;
if (idx<FMC2H) {
maxScore[idx] = -1.0f;
maxScor2[idx] = -1.0f;
maxIndex[idx] = 0;
}
__syncthreads();
const float *pt1 = sift1[p1].data;
for (int i=idx;i<128;i+=FMC2W*FMC2H)
siftPoint[i] = pt1[i];
__syncthreads();
for (int p2=ty;p2<numPts2;p2+=FMC2H) {
const float *pt2 = sift2[p2].data;
float sum = 0.0f;
for (int j=tx;j<128;j+=FMC2W)
sum += siftPoint[j] * pt2[j];
for (int j=FMC2W/2;j>0;j/=2)
sum += ShiftDown(sum, j);
if (tx==0) {
if (sum>maxScore[ty]) {
maxScor2[ty] = maxScore[ty];
maxScore[ty] = sum;
maxIndex[ty] = p2;
} else if (sum>maxScor2[ty])
maxScor2[ty] = sum;
}
}
__syncthreads();
for (int len=FMC2H/2;len>0;len/=2) {
if (ty==0 && tx<len) {
float val = maxScore[tx+len];
int p2 = maxIndex[tx+len];
if (val>maxScore[tx]) {
maxScor2[tx] = maxScore[tx];
maxScore[tx] = val;
maxIndex[tx] = p2;
} else if (val>maxScor2[tx])
maxScor2[tx] = val;
float va2 = maxScor2[tx+len];
if (va2>maxScor2[tx])
maxScor2[tx] = va2;
}
__syncthreads();
}
if (ty==0 && tx==0) {
sift1[p1].score = maxScore[0];
sift1[p1].ambiguity = maxScor2[0] / (maxScore[0] + 1e-6);
sift1[p1].match = maxIndex[0];
sift1[p1].match_xpos = sift2[maxIndex[0]].xpos;
sift1[p1].match_ypos = sift2[maxIndex[0]].ypos;
}
}
__global__ void FindMaxCorr4(SiftPoint *sift1, SiftPoint *sift2, int numPts1, int numPts2)
{
__shared__ float siftPoint[128*FMC2H];
__shared__ float maxScore[FMC2H];
__shared__ float maxScor2[FMC2H];
__shared__ int maxIndex[FMC2H];
const int tx = threadIdx.x;
const int ty = threadIdx.y;
if (tx==0) {
maxScore[ty] = -1.0f;
maxScor2[ty] = -1.0f;
maxIndex[ty] = 0;
}
const int p1 = blockIdx.x*FMC2H + ty;
const float *pt1 = sift1[p1].data;
for (int j=tx;j<128;j+=FMC2W)
siftPoint[128*ty + j] = pt1[j];
__syncthreads();
for (int p2=0;p2<numPts2;p2++) {
const float *pt2 = sift2[p2].data;
float sum = 0.0f;
for (int j=tx;j<128;j+=FMC2W)
sum += siftPoint[128*ty + j] * pt2[j];
for (int j=FMC2W/2;j>0;j/=2)
sum += ShiftDown(sum, j);
if (tx==0) {
if (sum>maxScore[ty]) {
maxScor2[ty] = maxScore[ty];
maxScore[ty] = sum;
maxIndex[ty] = p2;
} else if (sum>maxScor2[ty])
maxScor2[ty] = sum;
}
}
__syncthreads();
if (tx==0) {
sift1[p1].score = maxScore[ty];
sift1[p1].ambiguity = maxScor2[ty] / (maxScore[ty] + 1e-6);
sift1[p1].match = maxIndex[ty];
sift1[p1].match_xpos = sift2[maxIndex[ty]].xpos;
sift1[p1].match_ypos = sift2[maxIndex[ty]].ypos;
}
}
__global__ void CleanMatches(SiftPoint *sift1, int numPts1)
{
const int p1 = min(blockIdx.x*64 + threadIdx.x, numPts1-1);
sift1[p1].score = 0.0f;
}
__device__ volatile int lock = 0;
__global__ void FindMaxCorr5(SiftPoint *sift1, SiftPoint *sift2, int numPts1, int numPts2)
{
__shared__ float siftParts1[17*16]; // features in columns
__shared__ float siftParts2[17*16]; // one extra to about shared conflicts
const int tx = threadIdx.x;
const int ty = threadIdx.y;
const int p1l = min(blockIdx.x*16 + ty, numPts1-1);
const float *pt1l = sift1[p1l].data;
float maxScore = -1.0f;
float maxScor2 = -1.0f;
int maxIndex = 0;
for (int k=0;k<512/16;k++) {
const int p2l = min(blockIdx.y*512 + k*16 + ty, numPts2-1);
const float *pt2l = sift2[p2l].data;
float sum = 0.0f;
for (int i=0;i<8;i++) {
siftParts1[17*tx + ty] = pt1l[i*16 + tx]; // load and transpose
siftParts2[17*tx + ty] = pt2l[i*16 + tx];
__syncthreads();
for (int j=0;j<16;j++)
sum += siftParts1[17*j + tx] * siftParts2[17*j + ty];
__syncthreads();
}
float *sums = siftParts1;
sums[16*ty + tx] = sum;
__syncthreads();
if (ty==0) {
for (int j=0;j<16;j++) {
float sum = sums[16*j + tx];
if (sum>maxScore) {
maxScor2 = maxScore;
maxScore = sum;
maxIndex = min(blockIdx.y*512 + k*16 + j, numPts2-1);
} else if (sum>maxScor2)
maxScor2 = sum;
}
}
__syncthreads();
}
const int p1 = min(blockIdx.x*16 + tx, numPts1-1);
if (tx==0 && ty==0)
while (atomicCAS((int *)&lock, 0, 1) != 0);
__syncthreads();
if (ty==0) {
float maxScor2Old = sift1[p1].ambiguity*(sift1[p1].score + 1e-6f);
if (maxScore>sift1[p1].score) {
maxScor2 = max(sift1[p1].score, maxScor2);
sift1[p1].ambiguity = maxScor2 / (maxScore + 1e-6f);
sift1[p1].score = maxScore;
sift1[p1].match = maxIndex;
sift1[p1].match_xpos = sift2[maxIndex].xpos;
sift1[p1].match_ypos = sift2[maxIndex].ypos;
} else if (maxScore>maxScor2Old)
sift1[p1].ambiguity = maxScore / (sift1[p1].score + 1e-6f);
}
__syncthreads();
if (tx==0 && ty==0)
atomicExch((int* )&lock, 0);
}
template <int size>
__device__ void InvertMatrix(float elem[size][size], float res[size][size])
{
int indx[size];
float b[size];
float vv[size];
for (int i=0;i<size;i++)
indx[i] = 0;
int imax = 0;
float d = 1.0;
for (int i=0;i<size;i++) { // find biggest element for each row
float big = 0.0;
for (int j=0;j<size;j++) {
float temp = fabs(elem[i][j]);
if (temp>big)
big = temp;
}
if (big>0.0)
vv[i] = 1.0/big;
else
vv[i] = 1e16;
}
for (int j=0;j<size;j++) {
for (int i=0;i<j;i++) { // i<j
float sum = elem[i][j]; // i<j (lower left)
for (int k=0;k<i;k++) // k<i<j
sum -= elem[i][k]*elem[k][j]; // i>k (upper right), k<j (lower left)
elem[i][j] = sum; // i<j (lower left)
}
float big = 0.0;
for (int i=j;i<size;i++) { // i>=j
float sum = elem[i][j]; // i>=j (upper right)
for (int k=0;k<j;k++) // k<j<=i
sum -= elem[i][k]*elem[k][j]; // i>k (upper right), k<j (lower left)
elem[i][j] = sum; // i>=j (upper right)
float dum = vv[i]*fabs(sum);
if (dum>=big) {
big = dum;
imax = i;
}
}
if (j!=imax) { // imax>j
for (int k=0;k<size;k++) {
float dum = elem[imax][k]; // upper right and lower left
elem[imax][k] = elem[j][k];
elem[j][k] = dum;
}
d = -d;
vv[imax] = vv[j];
}
indx[j] = imax;
if (elem[j][j]==0.0) // j==j (upper right)
elem[j][j] = 1e-16;
if (j!=(size-1)) {
float dum = 1.0/elem[j][j];
for (int i=j+1;i<size;i++) // i>j
elem[i][j] *= dum; // i>j (upper right)
}
}
for (int j=0;j<size;j++) {
for (int k=0;k<size;k++)
b[k] = 0.0;
b[j] = 1.0;
int ii = -1;
for (int i=0;i<size;i++) {
int ip = indx[i];
float sum = b[ip];
b[ip] = b[i];
if (ii!=-1)
for (int j=ii;j<i;j++)
sum -= elem[i][j]*b[j]; // i>j (upper right)
else if (sum!=0.0)
ii = i;
b[i] = sum;
}
for (int i=size-1;i>=0;i--) {
float sum = b[i];
for (int j=i+1;j<size;j++)
sum -= elem[i][j]*b[j]; // i<j (lower left)
b[i] = sum/elem[i][i]; // i==i (upper right)
}
for (int i=0;i<size;i++)
res[i][j] = b[i];
}
}
__global__ void ComputeHomographies(float *coord, int *randPts, float *homo,
int numPts)
{
float a[8][8], ia[8][8];
float b[8];
const int bx = blockIdx.x;
const int tx = threadIdx.x;
const int idx = blockDim.x*bx + tx;
const int numLoops = blockDim.x*gridDim.x;
for (int i=0;i<4;i++) {
int pt = randPts[i*numLoops+idx];
float x1 = coord[pt+0*numPts];
float y1 = coord[pt+1*numPts];
float x2 = coord[pt+2*numPts];
float y2 = coord[pt+3*numPts];
float *row1 = a[2*i+0];
row1[0] = x1;
row1[1] = y1;
row1[2] = 1.0;
row1[3] = row1[4] = row1[5] = 0.0;
row1[6] = -x2*x1;
row1[7] = -x2*y1;
float *row2 = a[2*i+1];
row2[0] = row2[1] = row2[2] = 0.0;
row2[3] = x1;
row2[4] = y1;
row2[5] = 1.0;
row2[6] = -y2*x1;
row2[7] = -y2*y1;
b[2*i+0] = x2;
b[2*i+1] = y2;
}
InvertMatrix<8>(a, ia);
__syncthreads();
for (int j=0;j<8;j++) {
float sum = 0.0f;
for (int i=0;i<8;i++)
sum += ia[j][i]*b[i];
homo[j*numLoops+idx] = sum;
}
__syncthreads();
}
#define TESTHOMO_TESTS 16 // number of tests per block, alt. 32, 32
#define TESTHOMO_LOOPS 16 // number of loops per block, alt. 8, 16
__global__ void TestHomographies(float *d_coord, float *d_homo,
int *d_counts, int numPts, float thresh2)
{
__shared__ float homo[8*TESTHOMO_LOOPS];
__shared__ int cnts[TESTHOMO_TESTS*TESTHOMO_LOOPS];
const int tx = threadIdx.x;
const int ty = threadIdx.y;
const int idx = blockIdx.y*blockDim.y + tx;
const int numLoops = blockDim.y*gridDim.y;
if (ty<8 && tx<TESTHOMO_LOOPS)
homo[tx*8+ty] = d_homo[idx+ty*numLoops];
__syncthreads();
float a[8];
for (int i=0;i<8;i++)
a[i] = homo[ty*8+i];
int cnt = 0;
for (int i=tx;i<numPts;i+=TESTHOMO_TESTS) {
float x1 = d_coord[i+0*numPts];
float y1 = d_coord[i+1*numPts];
float x2 = d_coord[i+2*numPts];
float y2 = d_coord[i+3*numPts];
float nomx = __fmul_rz(a[0],x1) + __fmul_rz(a[1],y1) + a[2];
float nomy = __fmul_rz(a[3],x1) + __fmul_rz(a[4],y1) + a[5];
float deno = __fmul_rz(a[6],x1) + __fmul_rz(a[7],y1) + 1.0f;
float errx = __fmul_rz(x2,deno) - nomx;
float erry = __fmul_rz(y2,deno) - nomy;
float err2 = __fmul_rz(errx,errx) + __fmul_rz(erry,erry);
if (err2<__fmul_rz(thresh2,__fmul_rz(deno,deno)))
cnt ++;
}
int kty = TESTHOMO_TESTS*ty;
cnts[kty + tx] = cnt;
__syncthreads();
int len = TESTHOMO_TESTS/2;
while (len>0) {
if (tx<len)
cnts[kty + tx] += cnts[kty + tx + len];
len /= 2;
__syncthreads();
}
if (tx<TESTHOMO_LOOPS && ty==0)
d_counts[idx] = cnts[TESTHOMO_TESTS*tx];
__syncthreads();
}
//================= Host matching functions =====================//
double FindHomography(SiftData &data, float *homography, int *numMatches, int numLoops, float minScore, float maxAmbiguity, float thresh)
{
*numMatches = 0;
homography[0] = homography[4] = homography[8] = 1.0f;
homography[1] = homography[2] = homography[3] = 0.0f;
homography[5] = homography[6] = homography[7] = 0.0f;
#ifdef MANAGEDMEM
SiftPoint *d_sift = data.m_data;
#else
if (data.d_data==NULL)
return 0.0f;
SiftPoint *d_sift = data.d_data;
#endif
TimerGPU timer(0);
numLoops = iDivUp(numLoops,16)*16;
int numPts = data.numPts;
if (numPts<8)
return 0.0f;
int numPtsUp = iDivUp(numPts, 16)*16;
float *d_coord, *d_homo;
int *d_randPts, *h_randPts;
int randSize = 4*sizeof(int)*numLoops;
int szFl = sizeof(float);
int szPt = sizeof(SiftPoint);
safeCall(cudaMalloc((void **)&d_coord, 4*sizeof(float)*numPtsUp));
safeCall(cudaMalloc((void **)&d_randPts, randSize));
safeCall(cudaMalloc((void **)&d_homo, 8*sizeof(float)*numLoops));
h_randPts = (int*)malloc(randSize);
float *h_scores = (float *)malloc(sizeof(float)*numPtsUp);
float *h_ambiguities = (float *)malloc(sizeof(float)*numPtsUp);
safeCall(cudaMemcpy2D(h_scores, szFl, &d_sift[0].score, szPt, szFl, numPts, cudaMemcpyDeviceToHost));
safeCall(cudaMemcpy2D(h_ambiguities, szFl, &d_sift[0].ambiguity, szPt, szFl, numPts, cudaMemcpyDeviceToHost));
int *validPts = (int *)malloc(sizeof(int)*numPts);
int numValid = 0;
for (int i=0;i<numPts;i++) {
if (h_scores[i]>minScore && h_ambiguities[i]<maxAmbiguity)
validPts[numValid++] = i;
}
free(h_scores);
free(h_ambiguities);
if (numValid>=8) {
for (int i=0;i<numLoops;i++) {
int p1 = rand() % numValid;
int p2 = rand() % numValid;
int p3 = rand() % numValid;
int p4 = rand() % numValid;
while (p2==p1) p2 = rand() % numValid;
while (p3==p1 || p3==p2) p3 = rand() % numValid;
while (p4==p1 || p4==p2 || p4==p3) p4 = rand() % numValid;
h_randPts[i+0*numLoops] = validPts[p1];
h_randPts[i+1*numLoops] = validPts[p2];
h_randPts[i+2*numLoops] = validPts[p3];
h_randPts[i+3*numLoops] = validPts[p4];
}
safeCall(cudaMemcpy(d_randPts, h_randPts, randSize, cudaMemcpyHostToDevice));
safeCall(cudaMemcpy2D(&d_coord[0*numPtsUp], szFl, &d_sift[0].xpos, szPt, szFl, numPts, cudaMemcpyDeviceToDevice));
safeCall(cudaMemcpy2D(&d_coord[1*numPtsUp], szFl, &d_sift[0].ypos, szPt, szFl, numPts, cudaMemcpyDeviceToDevice));
safeCall(cudaMemcpy2D(&d_coord[2*numPtsUp], szFl, &d_sift[0].match_xpos, szPt, szFl, numPts, cudaMemcpyDeviceToDevice));
safeCall(cudaMemcpy2D(&d_coord[3*numPtsUp], szFl, &d_sift[0].match_ypos, szPt, szFl, numPts, cudaMemcpyDeviceToDevice));
ComputeHomographies<<<numLoops/16, 16>>>(d_coord, d_randPts, d_homo, numPtsUp);
safeCall(cudaThreadSynchronize());
checkMsg("ComputeHomographies() execution failed\n");
dim3 blocks(1, numLoops/TESTHOMO_LOOPS);
dim3 threads(TESTHOMO_TESTS, TESTHOMO_LOOPS);
TestHomographies<<<blocks, threads>>>(d_coord, d_homo, d_randPts, numPtsUp, thresh*thresh);
safeCall(cudaThreadSynchronize());
checkMsg("TestHomographies() execution failed\n");
safeCall(cudaMemcpy(h_randPts, d_randPts, sizeof(int)*numLoops, cudaMemcpyDeviceToHost));
int maxIndex = -1, maxCount = -1;
for (int i=0;i<numLoops;i++)
if (h_randPts[i]>maxCount) {
maxCount = h_randPts[i];
maxIndex = i;
}
*numMatches = maxCount;
safeCall(cudaMemcpy2D(homography, szFl, &d_homo[maxIndex], sizeof(float)*numLoops, szFl, 8, cudaMemcpyDeviceToHost));
}
free(validPts);
free(h_randPts);
safeCall(cudaFree(d_homo));
safeCall(cudaFree(d_randPts));
safeCall(cudaFree(d_coord));
double gpuTime = timer.read();
#ifdef VERBOSE
printf("FindHomography time = %.2f ms\n", gpuTime);
#endif
return gpuTime;
}
double MatchSiftData(SiftData &data1, SiftData &data2)
{
TimerGPU timer(0);
int numPts1 = data1.numPts;
int numPts2 = data2.numPts;
if (!numPts1 || !numPts2)
return 0.0;
#ifdef MANAGEDMEM
SiftPoint *sift1 = data1.m_data;
SiftPoint *sift2 = data2.m_data;
#else
if (data1.d_data==NULL || data2.d_data==NULL)
return 0.0f;
SiftPoint *sift1 = data1.d_data;
SiftPoint *sift2 = data2.d_data;
#endif
// Original version with correlation and maximization in two different kernels
// Global memory reguirement: O(N^2)
#if 0
float *d_corrData;
int corrWidth = iDivUp(numPts2, 16)*16;
int corrSize = sizeof(float)*numPts1*corrWidth;
safeCall(cudaMalloc((void **)&d_corrData, corrSize));
#if 0 // K40c 10.9ms, 1080 Ti 3.8ms
dim3 blocks1(numPts1, iDivUp(numPts2, 16));
dim3 threads1(16, 16); // each block: 1 points x 16 points
MatchSiftPoints<<<blocks1, threads1>>>(sift1, sift2, d_corrData, numPts1, numPts2);
#else // K40c 7.6ms, 1080 Ti 1.4ms
dim3 blocks(iDivUp(numPts1,16), iDivUp(numPts2, 16));
dim3 threads(16, 16); // each block: 16 points x 16 points
MatchSiftPoints2<<<blocks, threads>>>(sift1, sift2, d_corrData, numPts1, numPts2);
#endif
safeCall(cudaThreadSynchronize());
dim3 blocksMax(iDivUp(numPts1, 16));
dim3 threadsMax(16, 16);
FindMaxCorr<<<blocksMax, threadsMax>>>(d_corrData, sift1, sift2, numPts1, corrWidth, sizeof(SiftPoint));
safeCall(cudaThreadSynchronize());
checkMsg("FindMaxCorr() execution failed\n");
safeCall(cudaFree(d_corrData));
#endif
// Version suggested by Nicholas Lin with combined correlation and maximization
// Global memory reguirement: O(N)
#if 0 // K40c 51.2ms, 1080 Ti 9.6ms
int block_dim = 16;
float *d_corrData;
int corrSize = numPts1 * block_dim * 2;
safeCall(cudaMalloc((void **)&d_corrData, sizeof(float) * corrSize));
dim3 blocks(iDivUp(numPts1, block_dim));
dim3 threads(block_dim, block_dim);
FindMaxCorr3<<<blocks, threads >>>(d_corrData, sift1, sift2, numPts1, numPts2);
safeCall(cudaThreadSynchronize());
checkMsg("FindMaxCorr3() execution failed\n");
safeCall(cudaFree(d_corrData));
#endif
// Combined version with no global memory requirement using one 1 point per block
#if 0 // K40c 8.9ms, 1080 Ti 2.1ms
dim3 blocksMax(numPts1);
dim3 threadsMax(FMC2W, FMC2H);
FindMaxCorr2<<<blocksMax, threadsMax>>>(sift1, sift2, numPts1, numPts2);
safeCall(cudaThreadSynchronize());
checkMsg("FindMaxCorr2() execution failed\n");
#endif
// Combined version with no global memory requirement using one FMC2H points per block
#if 0 // K40c 9.2ms, 1080 Ti 1.3ms
dim3 blocksMax2(iDivUp(numPts1, FMC2H));
dim3 threadsMax2(FMC2W, FMC2H);
FindMaxCorr4<<<blocksMax2, threadsMax2>>>(sift1, sift2, numPts1, numPts2);
safeCall(cudaThreadSynchronize());
checkMsg("FindMaxCorr4() execution failed\n");
#endif
// Combined version with no global memory requirement using global locks
#if 1 // K40c 5.0ms, 1080 Ti 1.2ms
CleanMatches<<<iDivUp(numPts1, 64), 64>>>(sift1, numPts1);
dim3 blocksMax3(iDivUp(numPts1, 16), iDivUp(numPts2, 512));
dim3 threadsMax3(16, 16);
FindMaxCorr5<<<blocksMax3, threadsMax3>>>(sift1, sift2, numPts1, numPts2);
safeCall(cudaThreadSynchronize());
checkMsg("FindMaxCorr5() execution failed\n");
#endif
if (data1.h_data!=NULL) {
float *h_ptr = &data1.h_data[0].score;
float *d_ptr = &data1.d_data[0].score;
safeCall(cudaMemcpy2D(h_ptr, sizeof(SiftPoint), d_ptr, sizeof(SiftPoint), 5*sizeof(float), data1.numPts, cudaMemcpyDeviceToHost));
}
double gpuTime = timer.read();
#ifndef VERBOSE
printf("MatchSiftData time = %.2f ms\n", gpuTime);
#endif
return gpuTime;
}