-
Notifications
You must be signed in to change notification settings - Fork 1
/
gamma.cu
executable file
·212 lines (165 loc) · 7.1 KB
/
gamma.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
#include "gamma.cuh"
__device__ unsigned char LUT_device[256];
__global__ void k_init_LUT(float gamma) {
int blockId = blockIdx.x + blockIdx.y * gridDim.x;
int threadId = (blockId * (blockDim.x * blockDim.y) + (threadIdx.y * blockDim.x) + threadIdx.x);
LUT_device[threadId] = static_cast<unsigned char>(pow(threadId / 255.0f, gamma) * 255);
}
__global__ void k_3D_gamma_correction(unsigned char* input, int rows, int cols) {
int blockId = blockIdx.x + blockIdx.y * gridDim.x;
int threadId = (blockId * (blockDim.x * blockDim.y) + (threadIdx.y * blockDim.x) + threadIdx.x);
if (threadId >= rows * cols) {
return;
}
input[threadId] = LUT_device[input[threadId]];
}
__global__ void k_3D_gamma_correction_shared_mem(unsigned char* input, int rows, int cols) {
__shared__ unsigned char cache_LUT[256];
int blockId = blockIdx.x + blockIdx.y * gridDim.x;
int threadId = (blockId * (blockDim.x * blockDim.y) + (threadIdx.y * blockDim.x) + threadIdx.x);
int threadIdInBlock = (threadIdx.x * blockDim.y) + threadIdx.y;
if (threadId >= rows * cols) {
return;
}
if (threadIdInBlock < 256) {
cache_LUT[threadIdInBlock] = LUT_device[threadIdInBlock];
}
__syncthreads();
input[threadId] = cache_LUT[input[threadId]];
}
float gamma_correction_gpu_3D(cv::Mat input_img, cv::Mat* output_img, float gamma, bool sm) {
unsigned char* gpu_input = NULL;
unsigned int cols = input_img.cols * 3;
unsigned int rows = input_img.rows;
unsigned long int size = cols * rows * sizeof(unsigned char);
unsigned char* input = input_img.data;
unsigned char* output = output_img->data;
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start);
CHECK_CUDA_ERROR(cudaMalloc((void**)&gpu_input, size));
CHECK_CUDA_ERROR(cudaMemcpy(gpu_input, input, size, cudaMemcpyHostToDevice));
dim3 block(32, 32);
dim3 grid(((cols) + block.x - 1) / block.x, (rows + block.y - 1) / block.y);
if(sm){
k_init_LUT << <4, 64 >> > (gamma);
CHECK_CUDA_ERROR(cudaDeviceSynchronize());
k_3D_gamma_correction_shared_mem << <grid, block >> > (gpu_input, rows, cols);
}else{
k_init_LUT << <4, 64 >> > (gamma);
CHECK_CUDA_ERROR(cudaDeviceSynchronize());
k_3D_gamma_correction << <grid, block >> > (gpu_input, rows, cols);
}
CHECK_CUDA_ERROR(cudaMemcpy(output, gpu_input, size, cudaMemcpyDeviceToHost));
cudaEventRecord(stop);
cudaEventSynchronize(stop);
float gpuElapsedTime = 0;
cudaEventElapsedTime(&gpuElapsedTime, start, stop);
cudaFree(gpu_input);
cudaEventDestroy(start);
cudaEventDestroy(stop);
cudaDeviceReset();
return gpuElapsedTime;
}
float gamma_correction_cpu_3D(cv::Mat inputImg, cv::Mat* outputImg, float gamma) {
unsigned char* input = inputImg.data;
unsigned char* output = outputImg->data;
unsigned int rows = inputImg.rows;
unsigned int cols = inputImg.cols;
unsigned int pixels = rows * cols * 3;
auto start = std::chrono::steady_clock::now();
unsigned char LUT[256] = { 0 };
for (int i = 0; i < 256; i++) {
LUT[i] = static_cast<unsigned char>(pow(i / 255.0f, gamma) * 255);
}
for (int i = 0; i < pixels; i++) {
output[i] = LUT[input[i]];
}
auto end = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start) / 1000.0f;
return elapsed.count();
}
float gamma_correction_cpu_parallel_3D(cv::Mat inputImg, cv::Mat* outputImg, float gamma) {
unsigned char* input = inputImg.data;
unsigned char* output = outputImg->data;
unsigned int rows = inputImg.rows;
unsigned int cols = inputImg.cols;
auto start = std::chrono::steady_clock::now();
std::vector <std::thread> threads;
const int MAX_THREAD_SUPPORT = std::thread::hardware_concurrency();
int stride = rows / MAX_THREAD_SUPPORT;
unsigned char LUT[256] = { 0 };
for (int i = 0; i < 256; i++) {
LUT[i] = static_cast<uchar>(pow(i / 255.0f, gamma) * 255);
}
for (int i = 0; i < MAX_THREAD_SUPPORT; i++) {
threads.push_back(std::thread([&,i]() {
int range_start = stride * i;
int range_end = (i == MAX_THREAD_SUPPORT - 1) ? rows : stride * (i + 1);
for (int x = range_start; x < range_end; x++) {
for (int y = 0; y < cols; y++) {
for (int c = 0; c < 3; c++) {
int index = (x * cols * 3) + (y * 3) + c;
output[index] = LUT[input[index]];
}
}
}
}));
}
for (std::thread& thread : threads) {
thread.join();
}
auto end = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start) / 1000.0f;
return elapsed.count();
}
// float gamma_correction_frames_cpu(cv::Mat* frames, cv::Mat* output_frames,uint frame_count, float gamma) {
// float elapsed = 0.0f;
// for (int i = 0; i < frame_count; i++) {
// elapsed += gamma_correction_cpu(&frames[i], output_frames[i], gamma);
// }
// return elapsed;
// }
// float gamma_correction_frames_gpu(cv::Mat* input_frames, cv::Mat* output_frames, unsigned int frame_count, float gamma) {
// unsigned int cols = input_frames[0].cols;
// unsigned int rows = input_frames[0].rows;
// unsigned int frame_size = rows * cols * 3 * sizeof(unsigned char);/*size of a frame*/
// unsigned int size = frame_size * frame_count; /*size of data to transfer to gpu*/
// unsigned char* gpu_input = nullptr;
// unsigned char* gpu_output = nullptr;
// unsigned char LUT[256] = { 0 };
// for (int i = 0; i < 256; i++) {
// LUT[i] = static_cast<uchar>(pow(i / 255.0f, gamma) * 255);
// }
// cudaEvent_t start, stop;
// cudaEventCreate(&start);
// cudaEventCreate(&stop);
// cudaEventRecord(start);
// CHECK_CUDA_ERROR(cudaMemcpyToSymbolAsync(LUT_constant, LUT, sizeof(unsigned char) * 256, 0));
// CHECK_CUDA_ERROR(cudaHostAlloc((void **) & input_frames->data, size, cudaHostAllocDefault));
// CHECK_CUDA_ERROR(cudaHostAlloc((void **) & output_frames->data, size, cudaHostAllocDefault));
// CHECK_CUDA_ERROR(cudaMalloc((void**)&gpu_input, size));
// CHECK_CUDA_ERROR(cudaMalloc((void**)&gpu_output, size));
// for (int i = 0; i < frame_count; i++) {
// CHECK_CUDA_ERROR(cudaMemcpyAsync(gpu_input + i * frame_size, input_frames[i].data, frame_size, cudaMemcpyHostToDevice, 0));
// CHECK_CUDA_ERROR(cudaMemcpyAsync(gpu_output + i * frame_size, output_frames[i].data, frame_size, cudaMemcpyHostToDevice, 0));
// }
// CHECK_CUDA_ERROR(cudaStreamSynchronize(0));
// dim3 block(32, 32);
// dim3 grid((cols + block.x - 1) / block.x, (rows + block.y - 1) / block.y);
// for (int i = 0; i < frame_count; i++) {
// shared_gamma_correction << <grid, block >> > (gpu_input + i * frame_size, gpu_output + i * frame_size, rows, cols);
// }
// for (int i = 0; i < frame_count; i++) {
// CHECK_CUDA_ERROR(cudaMemcpyAsync(output_frames[i].data, gpu_output + i * frame_size, frame_size, cudaMemcpyDeviceToHost, 0));
// }
// cudaEventRecord(stop);
// cudaEventSynchronize(stop);
// float gpuElapsedTime = 0;
// cudaEventElapsedTime(&gpuElapsedTime, start, stop);
// cudaFree(gpu_input);
// cudaFree(gpu_output);
// cudaDeviceReset();
// return gpuElapsedTime;
// }