Skip to content

Commit aedf175

Browse files
committed
[DIP] Add benchmark for Buddy rotation operation.
1 parent e17d20d commit aedf175

File tree

3 files changed

+221
-1
lines changed

3 files changed

+221
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
//===- BuddyRotate2DBenchmark.cpp -------------------------------------------===//
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
//
17+
// This file implements the benchmark for Rotate2D operation.
18+
//
19+
//===----------------------------------------------------------------------===//
20+
21+
#include <benchmark/benchmark.h>
22+
#include <buddy/Core/Container.h>
23+
#include <buddy/DIP/ImageContainer.h>
24+
#include <buddy/DIP/DIP.h>
25+
#include <opencv2/opencv.hpp>
26+
27+
using namespace cv;
28+
using namespace std;
29+
30+
// Declare input image.
31+
Mat inputImageBuddyRotate2D;
32+
33+
// Define the angle.
34+
float BuddyRotate2DAngle;
35+
36+
// Define sizes of input.
37+
intptr_t sizesInputBuddyRotate2D[2];
38+
39+
// Declare Angle option supported.
40+
enum AngleOption { ANGLE_DEGREE, ANGLE_RADIAN };
41+
42+
// Define Angle option selected.
43+
AngleOption AngleType;
44+
45+
void initializeBuddyRotate2D(char **argv) {
46+
inputImageBuddyRotate2D = imread(argv[1], IMREAD_GRAYSCALE);
47+
48+
sizesInputBuddyRotate2D[0] = inputImageBuddyRotate2D.rows;
49+
sizesInputBuddyRotate2D[1] = inputImageBuddyRotate2D.cols;
50+
51+
if (static_cast<string>(argv[2]) == "DEGREE") {
52+
AngleType = ANGLE_DEGREE;
53+
} else {
54+
AngleType = ANGLE_RADIAN;
55+
}
56+
57+
std::string argAngle = argv[3];
58+
try {
59+
BuddyRotate2DAngle = std::stof(argAngle);
60+
} catch (const std::exception& e) {
61+
cout << "Exception converting rotation angle to float." << endl;
62+
}
63+
}
64+
65+
static void Buddy_Rotate2D_ANGLE_DEGREE(benchmark::State &state) {
66+
// Define the MemRef descriptor for input.
67+
Img<float, 2> inputBuddyRotate2D(inputImageBuddyRotate2D);
68+
69+
for (auto _ : state) {
70+
for (int i = 0; i < state.range(0); ++i) {
71+
// Call the MLIR Rotate2D function.
72+
MemRef<float, 2> output = dip::Rotate2D(&inputBuddyRotate2D, BuddyRotate2DAngle,
73+
dip::ANGLE_TYPE::DEGREE);
74+
}
75+
}
76+
}
77+
78+
static void Buddy_Rotate2D_ANGLE_RADIAN(benchmark::State &state) {
79+
// Define the MemRef descriptor for input.
80+
Img<float, 2> inputBuddyRotate2D(inputImageBuddyRotate2D);
81+
82+
for (auto _ : state) {
83+
for (int i = 0; i < state.range(0); ++i) {
84+
// Call the MLIR Rotate2D function.
85+
MemRef<float, 2> output = dip::Rotate2D(&inputBuddyRotate2D, BuddyRotate2DAngle,
86+
dip::ANGLE_TYPE::RADIAN);
87+
}
88+
}
89+
}
90+
91+
// Register benchmarking function.
92+
void registerBenchmarkBuddyRotate2D() {
93+
if (AngleType == ANGLE_DEGREE) {
94+
BENCHMARK(Buddy_Rotate2D_ANGLE_DEGREE)
95+
->Arg(1)
96+
->Unit(benchmark::kMillisecond);
97+
} else {
98+
BENCHMARK(Buddy_Rotate2D_ANGLE_RADIAN)
99+
->Arg(1)
100+
->Unit(benchmark::kMillisecond);
101+
}
102+
}
103+
104+
// Generate result image.
105+
void generateResultBuddyRotate2D() {
106+
// Define the MemRef descriptor for input.
107+
Img<float, 2> input(inputImageBuddyRotate2D);
108+
MemRef<float, 2> output(sizesInputBuddyRotate2D);
109+
// Run the resize 2D operation.
110+
if (AngleType == ANGLE_DEGREE) {
111+
// Call the MLIR Rotate2D function.
112+
output = dip::Rotate2D(&input, BuddyRotate2DAngle,
113+
dip::ANGLE_TYPE::DEGREE);
114+
} else {
115+
// Call the MLIR Rotate2D function.
116+
output = dip::Rotate2D(&input, BuddyRotate2DAngle,
117+
dip::ANGLE_TYPE::RADIAN);
118+
}
119+
120+
// Define a cv::Mat with the output of the resize operation.
121+
Mat outputImage(output.getSizes()[0], output.getSizes()[1], CV_32FC1,
122+
output.getData());
123+
124+
// Choose a PNG compression level
125+
vector<int> compressionParams;
126+
compressionParams.push_back(IMWRITE_PNG_COMPRESSION);
127+
compressionParams.push_back(9);
128+
129+
// Write output to PNG.
130+
bool result = true;
131+
try {
132+
result = imwrite("ResultBuddyRotate2D.png", outputImage, compressionParams);
133+
} catch (const cv::Exception &ex) {
134+
fprintf(stderr, "Exception converting image to PNG format: %s\n",
135+
ex.what());
136+
}
137+
if (result)
138+
cout << "Saved PNG file." << endl;
139+
else
140+
cout << "ERROR: Can't save PNG file." << endl;
141+
}

benchmarks/ImageProcessing/CMakeLists.txt

+26-1
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,29 @@ target_link_libraries(image-processing-resize-benchmark
144144
# Link Buddy MLIR DIP Library.
145145
BuddyLibDIP
146146
)
147-
147+
148+
#-------------------------------------------------------------------------------
149+
# Image Processing Rotate Benchmark Target
150+
#-------------------------------------------------------------------------------
151+
152+
add_executable(image-processing-rotate-benchmark
153+
MainRotate.cpp
154+
BuddyRotate2DBenchmark.cpp
155+
)
156+
157+
target_include_directories(image-processing-rotate-benchmark
158+
PRIVATE
159+
${BUDDY_SOURCE_DIR}/benchmarks/ImageProcessing/include/
160+
)
161+
162+
target_link_directories(image-processing-rotate-benchmark
163+
PRIVATE
164+
${BUDDY_MLIR_LIB_DIR}
165+
)
166+
167+
target_link_libraries(image-processing-rotate-benchmark
168+
GoogleBenchmark
169+
${OpenCV_LIBS}
170+
# Link Buddy MLIR DIP Library.
171+
BuddyLibDIP
172+
)
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//===- MainRotate.cpp -----------------------------------------------------------===//
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
//
17+
// This is the main file of the image processing rotate benchmark.
18+
//
19+
//===----------------------------------------------------------------------===//
20+
21+
#include <benchmark/benchmark.h>
22+
#include <stdexcept>
23+
24+
void initializeBuddyRotate2D(char **);
25+
26+
void generateResultBuddyRotate2D();
27+
28+
void registerBenchmarkBuddyRotate2D();
29+
30+
// Run benchmarks.
31+
int main(int argc, char **argv) {
32+
if (argc != 4) {
33+
throw std::invalid_argument(
34+
"Wrong format of command line arguments.\n"
35+
"Correct format is ./image-processing-rotate-benchmark <image path> <Rotate "
36+
"option> <RotateAngle> \n where "
37+
"image path provides path of the image to be processed, Rotate option "
38+
"available are DEGREE, RADIAN. "
39+
"RotateAngle accepts a "
40+
"float number for Rotate option.\n");
41+
}
42+
43+
initializeBuddyRotate2D(argv);
44+
45+
registerBenchmarkBuddyRotate2D();
46+
47+
::benchmark::Initialize(&argc, argv);
48+
::benchmark::RunSpecifiedBenchmarks();
49+
50+
// Generate result image.
51+
generateResultBuddyRotate2D();
52+
53+
return 0;
54+
}

0 commit comments

Comments
 (0)