-
Notifications
You must be signed in to change notification settings - Fork 7
/
timeGPUFeatureMatcher.cu
159 lines (128 loc) · 5.82 KB
/
timeGPUFeatureMatcher.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
#include <iostream>
#include <cassert>
#include "gpuFeatureMatcher.cuh"
#include "CudaTimer.cuh"
/*
*Gets exclusive and inclusive timing of one run of computing the correspondence
*matrix from randomly generated descriptors. Computation is done on GPU.
*Pre-conditions:
* descriptorDim, numImages > 0
* numDescriptorsPerImage > 1, so that we can do best of two neighbors
* matching without corner cases.
*Post-conditions:
* Assigns timings in milliseconds to `inclusiveTimingMS` and `exclusiveTimingMS`
*/
void timeOneRunOfCorrespondenceFromDescriptors(
const int numImages, const int numDescriptorsPerImage,
const int descriptorDim, const float matchConfidence,
float& inclusiveTimingMS,
float& exclusiveTimingMS) {
assert(numImages > 0);
assert(descriptorDim > 0);
assert(numDescriptorsPerImage > 1);
const int n_i = numDescriptorsPerImage;
const int k = descriptorDim;
const int n = numDescriptorsPerImage * numImages; // Total number of descriptors
// Compute cumulative sum as input downstream
int cumNumDescriptors[numImages];
for (int i=0;i<numImages;++i) { cumNumDescriptors[i]=(i+1)*n_i; }
/*printf("n=%i, k=%i, numImages=%i\n", n, k, numImages);*/
CudaTimer exclusiveTimer, inclusiveTimer;
// Allocate memory in host and device
Matrix<float> descriptorsH = AllocateMatrix<float>(n, k, 1);
Matrix<float> descriptorsD = AllocateDeviceMatrix<float>(descriptorsH);
Matrix<int> correspondenceMatH = AllocateMatrix<int>(numImages, n, 0);
Matrix<int> correspondenceMatD =
AllocateDeviceMatrix<int>(correspondenceMatH);
inclusiveTimer.tic();
// Copy descriptor elements to device
CopyToDeviceMatrix(descriptorsD, descriptorsH);
// Compute correspondence matrix in device
exclusiveTimer.tic();
gpuComputeCorrespondenceMatFromDescriptors(
descriptorsD, cumNumDescriptors,
numImages, correspondenceMatD, matchConfidence);
exclusiveTimingMS = exclusiveTimer.toc();
// Copy correspondence elements back to host
CopyFromDeviceMatrix(correspondenceMatH, correspondenceMatD);
inclusiveTimingMS = inclusiveTimer.toc();
FreeMatrix(&descriptorsH);
FreeMatrix(&correspondenceMatH);
FreeDeviceMatrix(&descriptorsD);
FreeDeviceMatrix(&correspondenceMatD);
/*cv::Mat image = cv::imread( "outputImages/result.jpg", 1 );*/
/*printf("size = (%i, %i)\n", image.rows, image.cols);*/
}
void timeOneRunOfCorrespondenceFromColumnwiseDescriptors(
const int numImages, const int numDescriptorsPerImage,
const int descriptorDim, const float matchConfidence,
float& inclusiveTimingMS,
float& exclusiveTimingMS) {
assert(numImages > 0);
assert(descriptorDim > 0);
assert(numDescriptorsPerImage > 1);
const int n_i = numDescriptorsPerImage;
const int k = descriptorDim;
const int n = numDescriptorsPerImage * numImages; // Total number of descriptors
// Compute cumulative sum as input downstream
int cumNumDescriptors[numImages];
for (int i=0;i<numImages;++i) { cumNumDescriptors[i]=(i+1)*n_i; }
/*printf("n=%i, k=%i, numImages=%i\n", n, k, numImages);*/
CudaTimer exclusiveTimer, inclusiveTimer;
// Allocate memory in host and device
Matrix<float> descriptorsH = AllocateMatrix<float>(k, n, 1);
Matrix<float> descriptorsD = AllocateDeviceMatrix<float>(descriptorsH);
Matrix<int> correspondenceMatH = AllocateMatrix<int>(numImages, n, 0);
Matrix<int> correspondenceMatD =
AllocateDeviceMatrix<int>(correspondenceMatH);
inclusiveTimer.tic();
// Copy descriptor elements to device
CopyToDeviceMatrix(descriptorsD, descriptorsH);
// Compute correspondence matrix in device
exclusiveTimer.tic();
gpuComputeCorrespondenceMatFromColumnwiseDescriptors(
descriptorsD, cumNumDescriptors,
numImages, correspondenceMatD, matchConfidence);
exclusiveTimingMS = exclusiveTimer.toc();
// Copy correspondence elements back to host
CopyFromDeviceMatrix(correspondenceMatH, correspondenceMatD);
inclusiveTimingMS = inclusiveTimer.toc();
/*// Double-check corrrespondence values match the CPU version*/
/*// Overwrites device results that were transferred to host*/
/*transpose(descriptorsH);*/
/*computeCorrespondenceMatFromDescriptors(descriptorsH, cumNumDescriptors,*/
/*numImages, correspondenceMatH, matchConfidence);*/
/*printf("RMSE between CPU space-optim and GPU coalesced = %f\n",*/
/*getRMSEHostAndDevice(correspondenceMatH, correspondenceMatD));*/
FreeMatrix(&descriptorsH);
FreeMatrix(&correspondenceMatH);
FreeDeviceMatrix(&descriptorsD);
FreeDeviceMatrix(&correspondenceMatD);
/*cv::Mat image = cv::imread( "outputImages/result.jpg", 1 );*/
/*printf("size = (%i, %i)\n", image.rows, image.cols);*/
}
int main(void) {
float inclusiveTimingMS, exclusiveTimingMS;
int k = 32;
int n_i = 100;
float matchConfidence = 0.1;
int numIters = 1; // number of iterations per parameter set
int runID = 0;
for (int exponent = 1; exponent <= 11; exponent+=1) {
int base = 2;
int numImages = pow(base, exponent);
for (int iterNum = 0; iterNum < numIters; ++iterNum) {
timeOneRunOfCorrespondenceFromColumnwiseDescriptors(
numImages, n_i, k,
matchConfidence, inclusiveTimingMS, exclusiveTimingMS);
printf("<run isGPU='1' ID='%i' numImages='%i' numDescriptorsPerImage='%i' "
"descriptorDim='%i' matchConfidence='%f' "
"inclusiveTimingMS='%f' exclusiveTimingMS='%f' />\n",
runID, numImages, n_i, k, matchConfidence, inclusiveTimingMS,
exclusiveTimingMS
);
++runID;
}
}
return 0;
}