-
Notifications
You must be signed in to change notification settings - Fork 1
/
sobel_gpu_5_pinned_mem_streams.cc
171 lines (138 loc) · 5.47 KB
/
sobel_gpu_5_pinned_mem_streams.cc
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
/*
* Copyright (c) 2021 Michael Gruner <michael.gruner@ridgerun.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <iostream>
#include <string>
#include <opencv2/core.hpp> // Basic OpenCV structures
#include <opencv2/imgproc.hpp> // Image processing methods for the CPU
#include <opencv2/imgcodecs.hpp> // Images IO
#include <opencv2/cudaarithm.hpp> // CUDA matrix operations
#include <opencv2/cudafilters.hpp> // CUDA image filters
struct Filters {
const cv::Ptr<cv::cuda::Filter> gaussian;
const cv::Ptr<cv::cuda::Filter> sobelx;
const cv::Ptr<cv::cuda::Filter> sobely;
};
struct GpuMemories {
cv::cuda::GpuMat input;
cv::cuda::GpuMat blurred;
cv::cuda::GpuMat x;
cv::cuda::GpuMat y;
cv::cuda::GpuMat x2;
cv::cuda::GpuMat y2;
cv::cuda::GpuMat mag2;
cv::cuda::GpuMat mag;
cv::cuda::GpuMat output;
};
struct Streams {
cv::cuda::Stream x;
cv::cuda::Stream y;
};
static void
sobel(const Filters &filters, Streams &streams, GpuMemories &gpu,
const cv::cuda::HostMem &input, cv::cuda::HostMem &output) {
// Migrate data from the CPU to the GPU
gpu.input.upload(input, streams.x);
// Low pass filter to clean noise
filters.gaussian->apply(gpu.input, gpu.blurred, streams.x);
streams.x.waitForCompletion();
// X and Y derivatives
filters.sobelx->apply(gpu.blurred, gpu.x, streams.x);
filters.sobely->apply(gpu.blurred, gpu.y, streams.y);
// X^2 and Y^2
cv::cuda::pow(gpu.x, 2, gpu.x2, streams.x);
cv::cuda::pow(gpu.y, 2, gpu.y2, streams.y);
streams.y.waitForCompletion();
// MAG2 = X^2 + Y^2
cv::cuda::addWeighted(gpu.x2, 1, gpu.y2, 1, 0, gpu.mag2, -1, streams.x);
// MAG = √(X^2 + Y^2)
cv::cuda::sqrt(gpu.mag2, gpu.mag, streams.x);
// Convert from floating point to char
gpu.mag.convertTo(gpu.output, CV_8UC1, streams.x);
// Migrate data back from GPU to CPU
gpu.output.download(output, streams.x);
streams.x.waitForCompletion();
}
int
main(int argc, char *argv[]) {
std::string to_read = "dog.jpg";
if (argc >= 2) {
to_read = argv[1];
}
std::string to_write = "dog_gradient_gpu_5_pinned_mem_streams.jpg";
if (argc >= 3) {
to_write = argv[2];
}
cv::Mat input = cv::imread(to_read, cv::IMREAD_GRAYSCALE);
if (input.empty()) {
std::cerr << "Unable to find \"" << to_read << "\". Is the path ok?"
<< std::endl;
return 1;
}
cv::cuda::HostMem pinned_input(input,
cv::cuda::HostMem::AllocType::PAGE_LOCKED);
cv::cuda::HostMem pinned_output(cv::cuda::HostMem::AllocType::PAGE_LOCKED);
// Filters in CUDA are created one time
Filters filters = {
gaussian : cv::cuda::createGaussianFilter(CV_8UC1, CV_8UC1,
cv::Size(7, 7), -1),
sobelx : cv::cuda::createSobelFilter(CV_8UC1, CV_32FC1, 1, 0, 3, 1),
sobely : cv::cuda::createSobelFilter(CV_8UC1, CV_32FC1, 0, 1, 3, 1)
};
GpuMemories gpu = {
input : cv::cuda::GpuMat(input.size(), CV_8UC1),
blurred : cv::cuda::GpuMat(input.size(), CV_8UC1),
x : cv::cuda::GpuMat(input.size(), CV_32FC1),
y : cv::cuda::GpuMat(input.size(), CV_32FC1),
x2 : cv::cuda::GpuMat(input.size(), CV_32FC1),
y2 : cv::cuda::GpuMat(input.size(), CV_32FC1),
mag2 : cv::cuda::GpuMat(input.size(), CV_32FC1),
mag : cv::cuda::GpuMat(input.size(), CV_32FC1),
output : cv::cuda::GpuMat(input.size(), CV_8UC1)
};
Streams streams;
// The first call is typically a warmup call so we dont benchmark
sobel(filters, streams, gpu, pinned_input, pinned_output);
int N = 100;
double time = cv::getTickCount();
std::cout << "Performing " << N << " iterations..." << std::flush;
for (int i = 0; i < N; i++) {
sobel(filters, streams, gpu, pinned_input, pinned_output);
}
time = 1000.0*(cv::getTickCount() - time)/cv::getTickFrequency();
time /= N;
std::cout << " done!" << std::endl << "Average for " << N << " CPU runs: "
<< time << "ms" << std::endl;
std::cout << "Resulting image wrote to " << to_write << std::endl;
cv::imwrite(to_write, pinned_output);
return 0;
}