Skip to content
Snippets Groups Projects
Commit a55b8fb4 authored by zhanghb97's avatar zhanghb97
Browse files

[DIP] Reorganize the DIP benchmark.

parent 817f288a
No related branches found
No related tags found
No related merge requests found
...@@ -7,9 +7,16 @@ set(LLVM_MLIR_BINARY_DIR ${BUDDY_OPT_BUILD_DIR}/../llvm/build/bin) ...@@ -7,9 +7,16 @@ set(LLVM_MLIR_BINARY_DIR ${BUDDY_OPT_BUILD_DIR}/../llvm/build/bin)
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
add_custom_command(OUTPUT conv2d.o add_custom_command(OUTPUT conv2d.o
COMMAND ${BUDDY_OPT_BUILD_DIR}/bin/buddy-opt ${BUDDY_SOURCE_DIR}/benchmarks/ImageProcessing/Conv2D.mlir -conv-vectorization="strip-mining=${BUDDY_OPT_STRIP_MINING}" -lower-affine -convert-scf-to-std -convert-vector-to-llvm -convert-memref-to-llvm -convert-std-to-llvm='emit-c-wrappers=1' -reconcile-unrealized-casts | COMMAND ${BUDDY_OPT_BUILD_DIR}/bin/buddy-opt
${BUDDY_SOURCE_DIR}/benchmarks/ImageProcessing/Conv2D.mlir
-conv-vectorization="strip-mining=${BUDDY_OPT_STRIP_MINING}"
-lower-affine -convert-scf-to-std -convert-vector-to-llvm
-convert-memref-to-llvm -convert-std-to-llvm='emit-c-wrappers=1'
-reconcile-unrealized-casts |
${LLVM_MLIR_BINARY_DIR}/mlir-translate --mlir-to-llvmir | ${LLVM_MLIR_BINARY_DIR}/mlir-translate --mlir-to-llvmir |
${LLVM_MLIR_BINARY_DIR}/llc -mtriple=x86_64-unknown-linux-gnu -mattr=${BUDDY_OPT_ATTR} --filetype=obj -o ${BUDDY_BINARY_DIR}/../benchmarks/ImageProcessing/conv2d.o ${LLVM_MLIR_BINARY_DIR}/llc -mtriple=x86_64-unknown-linux-gnu
-mattr=${BUDDY_OPT_ATTR} --filetype=obj
-o ${BUDDY_BINARY_DIR}/../benchmarks/ImageProcessing/conv2d.o
) )
add_library(Conv2D STATIC conv2d.o) add_library(Conv2D STATIC conv2d.o)
set_target_properties(Conv2D PROPERTIES LINKER_LANGUAGE CXX) set_target_properties(Conv2D PROPERTIES LINKER_LANGUAGE CXX)
...@@ -38,8 +45,8 @@ set_target_properties(Corr2D PROPERTIES LINKER_LANGUAGE CXX) ...@@ -38,8 +45,8 @@ set_target_properties(Corr2D PROPERTIES LINKER_LANGUAGE CXX)
add_executable(image-processing-benchmark add_executable(image-processing-benchmark
Main.cpp Main.cpp
OpenCVBenchmark.cpp Filter2DBenchmark.cpp
BuddyBenchmark.cpp Conv2DBenchmark.cpp
Corr2DBenchmark.cpp Corr2DBenchmark.cpp
) )
......
//===- BuddyBenchmark.cpp -------------------------------------------------===// //===- Conv2DBenchmark.cpp ------------------------------------------------===//
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
...@@ -28,43 +28,76 @@ using namespace std; ...@@ -28,43 +28,76 @@ using namespace std;
// Declare the conv2d C interface. // Declare the conv2d C interface.
extern "C" { extern "C" {
void _mlir_ciface_conv_2d(MemRef<float, 2> *input, MemRef<float, 2> *kernel, void _mlir_ciface_conv_2d(MemRef<float, 2> *inputConv2D,
MemRef<float, 2> *output); MemRef<float, 2> *kernelConv2D,
MemRef<float, 2> *outputConv2D);
} }
// Read input image // Read input image.
Mat inputImageBuddy = imread("../../benchmarks/ImageProcessing/Images/YuTu.png", Mat inputImageConv2D = imread(
IMREAD_GRAYSCALE); "../../benchmarks/ImageProcessing/Images/YuTu.png", IMREAD_GRAYSCALE);
// Define the kernel. // Define the kernel size.
int kernelRows = laplacianKernelRows; int kernelRowsConv2D = laplacianKernelRows;
int kernelCols = laplacianKernelCols; int kernelColsConv2D = laplacianKernelCols;
// Define output for buddy mlir implementation. // Define the output size.
int outputRows = inputImageBuddy.rows - kernelRows + 1; int outputRowsConv2D = inputImageConv2D.rows - kernelRowsConv2D + 1;
int outputCols = inputImageBuddy.cols - kernelCols + 1; int outputColsConv2D = inputImageConv2D.cols - kernelColsConv2D + 1;
// Define allocated, sizes, and strides. // Define sizes of input, kernel, and output.
intptr_t sizesInput[2] = {inputImageBuddy.rows, inputImageBuddy.cols}; intptr_t sizesInputConv2D[2] = {inputImageConv2D.rows, inputImageConv2D.cols};
intptr_t sizesKernel[2] = {kernelRows, kernelCols}; intptr_t sizesKernelConv2D[2] = {kernelRowsConv2D, kernelColsConv2D};
intptr_t sizesOutput[2] = {outputRows, outputCols}; intptr_t sizesOutputConv2D[2] = {outputRowsConv2D, outputColsConv2D};
// Define input, kernel, and output. // Define the MemRef descriptor for input, kernel, and output.
MemRef<float, 2> input(inputImageBuddy, sizesInput); MemRef<float, 2> inputConv2D(inputImageConv2D, sizesInputConv2D);
MemRef<float, 2> kernel(laplacianKernelAlign, sizesKernel); MemRef<float, 2> kernelConv2D(laplacianKernelAlign, sizesKernelConv2D);
MemRef<float, 2> output(sizesOutput); MemRef<float, 2> outputConv2D(sizesOutputConv2D);
static void BM_Buddy(benchmark::State &state) { static void BM_Conv2D_Buddy(benchmark::State &state) {
for (auto _ : state) { for (auto _ : state) {
for (int i = 0; i < state.range(0); ++i) { for (int i = 0; i < state.range(0); ++i) {
_mlir_ciface_conv_2d(&input, &kernel, &output); _mlir_ciface_conv_2d(&inputConv2D, &kernelConv2D, &outputConv2D);
} }
} }
} }
// Register benchmarking function with different arguments // Register benchmarking function with different arguments.
BENCHMARK(BM_Buddy)->Arg(1); BENCHMARK(BM_Conv2D_Buddy)->Arg(1);
BENCHMARK(BM_Buddy)->Arg(2); BENCHMARK(BM_Conv2D_Buddy)->Arg(2);
BENCHMARK(BM_Buddy)->Arg(4); BENCHMARK(BM_Conv2D_Buddy)->Arg(4);
BENCHMARK(BM_Buddy)->Arg(8); BENCHMARK(BM_Conv2D_Buddy)->Arg(8);
BENCHMARK(BM_Buddy)->Arg(16); BENCHMARK(BM_Conv2D_Buddy)->Arg(16);
// Generate result image.
void generateResultConv2D() {
// Define the MemRef descriptor for input, kernel, and output.
MemRef<float, 2> input(inputImageConv2D, sizesInputConv2D);
MemRef<float, 2> kernel(laplacianKernelAlign, sizesKernelConv2D);
MemRef<float, 2> output(sizesOutputConv2D);
// Run the 2D convolution.
_mlir_ciface_conv_2d(&input, &kernel, &output);
// Define a cv::Mat with the output of the convolution.
Mat outputImage(outputRowsConv2D, outputColsConv2D, CV_32FC1,
output.getData());
// Choose a PNG compression level
vector<int> compressionParams;
compressionParams.push_back(IMWRITE_PNG_COMPRESSION);
compressionParams.push_back(9);
// Write output to PNG.
bool result = false;
try {
result = imwrite("ResultConv2D.png", outputImage, compressionParams);
} catch (const cv::Exception &ex) {
fprintf(stderr, "Exception converting image to PNG format: %s\n",
ex.what());
}
if (result)
cout << "Saved PNG file." << endl;
else
cout << "ERROR: Can't save PNG file." << endl;
}
...@@ -56,7 +56,7 @@ MemRef<float, 2> inputCorr2D(inputImageCorr2D, sizesInputCorr2D); ...@@ -56,7 +56,7 @@ MemRef<float, 2> inputCorr2D(inputImageCorr2D, sizesInputCorr2D);
MemRef<float, 2> kernelCorr2D(laplacianKernelAlign, sizesKernelCorr2D); MemRef<float, 2> kernelCorr2D(laplacianKernelAlign, sizesKernelCorr2D);
MemRef<float, 2> outputCorr2D(sizesOutputCorr2D); MemRef<float, 2> outputCorr2D(sizesOutputCorr2D);
static void BM_Corr2D(benchmark::State &state) { static void BM_Corr2D_Buddy(benchmark::State &state) {
for (auto _ : state) { for (auto _ : state) {
for (int i = 0; i < state.range(0); ++i) { for (int i = 0; i < state.range(0); ++i) {
_mlir_ciface_corr_2d(&inputCorr2D, &kernelCorr2D, &outputCorr2D, _mlir_ciface_corr_2d(&inputCorr2D, &kernelCorr2D, &outputCorr2D,
...@@ -67,11 +67,11 @@ static void BM_Corr2D(benchmark::State &state) { ...@@ -67,11 +67,11 @@ static void BM_Corr2D(benchmark::State &state) {
} }
// Register benchmarking function with different arguments. // Register benchmarking function with different arguments.
BENCHMARK(BM_Corr2D)->Arg(1); BENCHMARK(BM_Corr2D_Buddy)->Arg(1);
BENCHMARK(BM_Corr2D)->Arg(2); BENCHMARK(BM_Corr2D_Buddy)->Arg(2);
BENCHMARK(BM_Corr2D)->Arg(4); BENCHMARK(BM_Corr2D_Buddy)->Arg(4);
BENCHMARK(BM_Corr2D)->Arg(8); BENCHMARK(BM_Corr2D_Buddy)->Arg(8);
BENCHMARK(BM_Corr2D)->Arg(16); BENCHMARK(BM_Corr2D_Buddy)->Arg(16);
// Generate result image. // Generate result image.
void generateResultCorr2D() { void generateResultCorr2D() {
......
//===- OpenCVBenchmark.cpp ------------------------------------------------===// //===- Filter2DBenchmark.cpp ----------------------------------------------===//
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// //
// This file implements the benchmark for OpenCV. // This file implements the benchmark for OpenCV filter2D.
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
...@@ -26,25 +26,25 @@ using namespace cv; ...@@ -26,25 +26,25 @@ using namespace cv;
using namespace std; using namespace std;
// Read input image and specify kernel. // Read input image and specify kernel.
Mat inputImage = imread("../../benchmarks/ImageProcessing/Images/YuTu.png", Mat inputImageFilter2D = imread(
IMREAD_GRAYSCALE); "../../benchmarks/ImageProcessing/Images/YuTu.png", IMREAD_GRAYSCALE);
Mat kernelOpencv = Mat(3, 3, CV_32FC1, laplacianKernelAlign); Mat kernelFilter2D = Mat(3, 3, CV_32FC1, laplacianKernelAlign);
// Declare output image. // Declare output image.
Mat outputOpencv; Mat outputFilter2D;
// Benchmarking function. // Benchmarking function.
static void BM_OpenCV(benchmark::State &state) { static void BM_Filter2D_OpenCV(benchmark::State &state) {
for (auto _ : state) { for (auto _ : state) {
for (int i = 0; i < state.range(0); ++i) { for (int i = 0; i < state.range(0); ++i) {
filter2D(inputImage, outputOpencv, CV_32FC1, kernelOpencv); filter2D(inputImageFilter2D, outputFilter2D, CV_32FC1, kernelFilter2D);
} }
} }
} }
// Register benchmarking function with different arguments. // Register benchmarking function with different arguments.
BENCHMARK(BM_OpenCV)->Arg(1); BENCHMARK(BM_Filter2D_OpenCV)->Arg(1);
BENCHMARK(BM_OpenCV)->Arg(2); BENCHMARK(BM_Filter2D_OpenCV)->Arg(2);
BENCHMARK(BM_OpenCV)->Arg(4); BENCHMARK(BM_Filter2D_OpenCV)->Arg(4);
BENCHMARK(BM_OpenCV)->Arg(8); BENCHMARK(BM_Filter2D_OpenCV)->Arg(8);
BENCHMARK(BM_OpenCV)->Arg(16); BENCHMARK(BM_Filter2D_OpenCV)->Arg(16);
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include <benchmark/benchmark.h> #include <benchmark/benchmark.h>
void generateResultConv2D();
void generateResultCorr2D(); void generateResultCorr2D();
// Run benchmarks. // Run benchmarks.
...@@ -27,6 +28,7 @@ int main(int argc, char **argv) { ...@@ -27,6 +28,7 @@ int main(int argc, char **argv) {
::benchmark::Initialize(&argc, argv); ::benchmark::Initialize(&argc, argv);
::benchmark::RunSpecifiedBenchmarks(); ::benchmark::RunSpecifiedBenchmarks();
// Generate result image. // Generate result image.
generateResultConv2D();
generateResultCorr2D(); generateResultCorr2D();
return 0; return 0;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment