forked from dhernandez0/sgm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisparity_method.cu
246 lines (215 loc) · 9.61 KB
/
disparity_method.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
/**
This file is part of sgm. (https://github.com/dhernandez0/sgm).
Copyright (c) 2016 Daniel Hernandez Juarez.
sgm is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
sgm is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with sgm. If not, see <http://www.gnu.org/licenses/>.
**/
#include "disparity_method.h"
static cudaStream_t stream1, stream2, stream3;//, stream4, stream5, stream6, stream7, stream8;
static uint8_t *d_im0;
static uint8_t *d_im1;
static cost_t *d_transform0;
static cost_t *d_transform1;
static uint8_t *d_cost;
static uint8_t *d_disparity;
static uint8_t *d_disparity_filtered_uchar;
static uint8_t *h_disparity;
static uint16_t *d_S;
static uint8_t *d_L0;
static uint8_t *d_L1;
static uint8_t *d_L2;
static uint8_t *d_L3;
static uint8_t *d_L4;
static uint8_t *d_L5;
static uint8_t *d_L6;
static uint8_t *d_L7;
static uint8_t p1, p2;
static bool first_alloc;
static uint32_t cols, rows, size, size_cube_l;
void init_disparity_method(const uint8_t _p1, const uint8_t _p2) {
// We are not using shared memory, use L1
//CUDA_CHECK_RETURN(cudaDeviceSetCacheConfig(cudaFuncCachePreferL1));
//CUDA_CHECK_RETURN(cudaDeviceSetCacheConfig(cudaFuncCachePreferShared));
// Create streams
CUDA_CHECK_RETURN(cudaStreamCreate(&stream1));
CUDA_CHECK_RETURN(cudaStreamCreate(&stream2));
CUDA_CHECK_RETURN(cudaStreamCreate(&stream3));
first_alloc = true;
p1 = _p1;
p2 = _p2;
rows = 0;
cols = 0;
}
cv::Mat compute_disparity_method(cv::Mat left, cv::Mat right, float *elapsed_time_ms, const char* directory, const char* fname) {
if(cols != left.cols || rows != left.rows) {
debug_log("WARNING: cols or rows are different");
if(!first_alloc) {
debug_log("Freeing memory");
free_memory();
}
first_alloc = false;
cols = left.cols;
rows = left.rows;
size = rows*cols;
size_cube_l = size*MAX_DISPARITY;
CUDA_CHECK_RETURN(cudaMalloc((void **)&d_transform0, sizeof(cost_t)*size));
CUDA_CHECK_RETURN(cudaMalloc((void **)&d_transform1, sizeof(cost_t)*size));
int size_cube = size*MAX_DISPARITY;
CUDA_CHECK_RETURN(cudaMalloc((void **)&d_cost, sizeof(uint8_t)*size_cube));
CUDA_CHECK_RETURN(cudaMalloc((void **)&d_im0, sizeof(uint8_t)*size));
CUDA_CHECK_RETURN(cudaMalloc((void **)&d_im1, sizeof(uint8_t)*size));
CUDA_CHECK_RETURN(cudaMalloc((void **)&d_S, sizeof(uint16_t)*size_cube_l));
CUDA_CHECK_RETURN(cudaMalloc((void **)&d_L0, sizeof(uint8_t)*size_cube_l));
CUDA_CHECK_RETURN(cudaMalloc((void **)&d_L1, sizeof(uint8_t)*size_cube_l));
CUDA_CHECK_RETURN(cudaMalloc((void **)&d_L2, sizeof(uint8_t)*size_cube_l));
CUDA_CHECK_RETURN(cudaMalloc((void **)&d_L3, sizeof(uint8_t)*size_cube_l));
#if PATH_AGGREGATION == 8
CUDA_CHECK_RETURN(cudaMalloc((void **)&d_L4, sizeof(uint8_t)*size_cube_l));
CUDA_CHECK_RETURN(cudaMalloc((void **)&d_L5, sizeof(uint8_t)*size_cube_l));
CUDA_CHECK_RETURN(cudaMalloc((void **)&d_L6, sizeof(uint8_t)*size_cube_l));
CUDA_CHECK_RETURN(cudaMalloc((void **)&d_L7, sizeof(uint8_t)*size_cube_l));
#endif
CUDA_CHECK_RETURN(cudaMalloc((void **)&d_disparity, sizeof(uint8_t)*size));
CUDA_CHECK_RETURN(cudaMalloc((void **)&d_disparity_filtered_uchar, sizeof(uint8_t)*size));
h_disparity = new uint8_t[size];
}
debug_log("Copying images to the GPU");
CUDA_CHECK_RETURN(cudaMemcpyAsync(d_im0, left.ptr<uint8_t>(), sizeof(uint8_t)*size, cudaMemcpyHostToDevice, stream1));
CUDA_CHECK_RETURN(cudaMemcpyAsync(d_im1, right.ptr<uint8_t>(), sizeof(uint8_t)*size, cudaMemcpyHostToDevice, stream1));
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start, 0);
dim3 block_size;
block_size.x = 32;
block_size.y = 32;
dim3 grid_size;
grid_size.x = (cols+block_size.x-1) / block_size.x;
grid_size.y = (rows+block_size.y-1) / block_size.y;
debug_log("Calling CSCT");
CenterSymmetricCensusKernelSM2<<<grid_size, block_size, 0, stream1>>>(d_im0, d_im1, d_transform0, d_transform1, rows, cols);
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess) {
printf("Error: %s %d\n", cudaGetErrorString(err), err);
exit(-1);
}
// Hamming distance
CUDA_CHECK_RETURN(cudaStreamSynchronize(stream1));
debug_log("Calling Hamming Distance");
HammingDistanceCostKernel<<<rows, MAX_DISPARITY, 0, stream1>>>(d_transform0, d_transform1, d_cost, rows, cols);
err = cudaGetLastError();
if (err != cudaSuccess) {
printf("Error: %s %d\n", cudaGetErrorString(err), err);
exit(-1);
}
// Cost Aggregation
const int PIXELS_PER_BLOCK = COSTAGG_BLOCKSIZE/WARP_SIZE;
const int PIXELS_PER_BLOCK_HORIZ = COSTAGG_BLOCKSIZE_HORIZ/WARP_SIZE;
debug_log("Calling Left to Right");
CostAggregationKernelLeftToRight<<<(rows+PIXELS_PER_BLOCK_HORIZ-1)/PIXELS_PER_BLOCK_HORIZ, COSTAGG_BLOCKSIZE_HORIZ, 0, stream2>>>(d_cost, d_L0, p1, p2, rows, cols, d_transform0, d_transform1, d_disparity, d_L0, d_L1, d_L2, d_L3, d_L4, d_L5, d_L6);
err = cudaGetLastError();
if (err != cudaSuccess) {
printf("Error: %s %d\n", cudaGetErrorString(err), err);
exit(-1);
}
debug_log("Calling Right to Left");
CostAggregationKernelRightToLeft<<<(rows+PIXELS_PER_BLOCK_HORIZ-1)/PIXELS_PER_BLOCK_HORIZ, COSTAGG_BLOCKSIZE_HORIZ, 0, stream3>>>(d_cost, d_L1, p1, p2, rows, cols, d_transform0, d_transform1, d_disparity, d_L0, d_L1, d_L2, d_L3, d_L4, d_L5, d_L6);
err = cudaGetLastError();
if (err != cudaSuccess) {
printf("Error: %s %d\n", cudaGetErrorString(err), err);
exit(-1);
}
debug_log("Calling Up to Down");
CostAggregationKernelUpToDown<<<(cols+PIXELS_PER_BLOCK-1)/PIXELS_PER_BLOCK, COSTAGG_BLOCKSIZE, 0, stream1>>>(d_cost, d_L2, p1, p2, rows, cols, d_transform0, d_transform1, d_disparity, d_L0, d_L1, d_L2, d_L3, d_L4, d_L5, d_L6);
err = cudaGetLastError();
if (err != cudaSuccess) {
printf("Error: %s %d\n", cudaGetErrorString(err), err);
exit(-1);
}
CUDA_CHECK_RETURN(cudaDeviceSynchronize());
debug_log("Calling Down to Up");
CostAggregationKernelDownToUp<<<(cols+PIXELS_PER_BLOCK-1)/PIXELS_PER_BLOCK, COSTAGG_BLOCKSIZE, 0, stream1>>>(d_cost, d_L3, p1, p2, rows, cols, d_transform0, d_transform1, d_disparity, d_L0, d_L1, d_L2, d_L3, d_L4, d_L5, d_L6);
err = cudaGetLastError();
if (err != cudaSuccess) {
printf("Error: %s %d\n", cudaGetErrorString(err), err);
exit(-1);
}
#if PATH_AGGREGATION == 8
CostAggregationKernelDiagonalDownUpLeftRight<<<(cols+PIXELS_PER_BLOCK-1)/PIXELS_PER_BLOCK, COSTAGG_BLOCKSIZE, 0, stream1>>>(d_cost, d_L4, p1, p2, rows, cols, d_transform0, d_transform1, d_disparity, d_L0, d_L1, d_L2, d_L3, d_L4, d_L5, d_L6);
err = cudaGetLastError();
if (err != cudaSuccess) {
printf("Error: %s %d\n", cudaGetErrorString(err), err);
exit(-1);
}
CostAggregationKernelDiagonalUpDownLeftRight<<<(cols+PIXELS_PER_BLOCK-1)/PIXELS_PER_BLOCK, COSTAGG_BLOCKSIZE, 0, stream1>>>(d_cost, d_L5, p1, p2, rows, cols, d_transform0, d_transform1, d_disparity, d_L0, d_L1, d_L2, d_L3, d_L4, d_L5, d_L6);
err = cudaGetLastError();
if (err != cudaSuccess) {
printf("Error: %s %d\n", cudaGetErrorString(err), err);
exit(-1);
}
CostAggregationKernelDiagonalDownUpRightLeft<<<(cols+PIXELS_PER_BLOCK-1)/PIXELS_PER_BLOCK, COSTAGG_BLOCKSIZE, 0, stream1>>>(d_cost, d_L6, p1, p2, rows, cols, d_transform0, d_transform1, d_disparity, d_L0, d_L1, d_L2, d_L3, d_L4, d_L5, d_L6);
err = cudaGetLastError();
if (err != cudaSuccess) {
printf("Error: %s %d\n", cudaGetErrorString(err), err);
exit(-1);
}
CostAggregationKernelDiagonalUpDownRightLeft<<<(cols+PIXELS_PER_BLOCK-1)/PIXELS_PER_BLOCK, COSTAGG_BLOCKSIZE, 0, stream1>>>(d_cost, d_L7, p1, p2, rows, cols, d_transform0, d_transform1, d_disparity, d_L0, d_L1, d_L2, d_L3, d_L4, d_L5, d_L6);
err = cudaGetLastError();
if (err != cudaSuccess) {
printf("Error: %s %d\n", cudaGetErrorString(err), err);
exit(-1);
}
#endif
debug_log("Calling Median Filter");
MedianFilter3x3<<<(size+MAX_DISPARITY-1)/MAX_DISPARITY, MAX_DISPARITY, 0, stream1>>>(d_disparity, d_disparity_filtered_uchar, rows, cols);
err = cudaGetLastError();
if (err != cudaSuccess) {
printf("Error: %s %d\n", cudaGetErrorString(err), err);
exit(-1);
}
cudaEventRecord(stop, 0);
CUDA_CHECK_RETURN(cudaDeviceSynchronize());
cudaEventElapsedTime(elapsed_time_ms, start, stop);
cudaEventDestroy(start);
cudaEventDestroy(stop);
debug_log("Copying final disparity to CPU");
CUDA_CHECK_RETURN(cudaMemcpy(h_disparity, d_disparity_filtered_uchar, sizeof(uint8_t)*size, cudaMemcpyDeviceToHost));
cv::Mat disparity(rows, cols, CV_8UC1, h_disparity);
return disparity;
}
static void free_memory() {
CUDA_CHECK_RETURN(cudaFree(d_im0));
CUDA_CHECK_RETURN(cudaFree(d_im1));
CUDA_CHECK_RETURN(cudaFree(d_transform0));
CUDA_CHECK_RETURN(cudaFree(d_transform1));
CUDA_CHECK_RETURN(cudaFree(d_L0));
CUDA_CHECK_RETURN(cudaFree(d_L1));
CUDA_CHECK_RETURN(cudaFree(d_L2));
CUDA_CHECK_RETURN(cudaFree(d_L3));
#if PATH_AGGREGATION == 8
CUDA_CHECK_RETURN(cudaFree(d_L4));
CUDA_CHECK_RETURN(cudaFree(d_L5));
CUDA_CHECK_RETURN(cudaFree(d_L6));
CUDA_CHECK_RETURN(cudaFree(d_L7));
#endif
CUDA_CHECK_RETURN(cudaFree(d_disparity));
CUDA_CHECK_RETURN(cudaFree(d_disparity_filtered_uchar));
CUDA_CHECK_RETURN(cudaFree(d_cost));
delete[] h_disparity;
}
void finish_disparity_method() {
if(!first_alloc) {
free_memory();
CUDA_CHECK_RETURN(cudaStreamDestroy(stream1));
CUDA_CHECK_RETURN(cudaStreamDestroy(stream2));
CUDA_CHECK_RETURN(cudaStreamDestroy(stream3));
}
}