From 03f7408c7a163b15768b782980c7197793fbc3e6 Mon Sep 17 00:00:00 2001 From: zhanghb97 <hongbin2019@iscas.ac.cn> Date: Thu, 11 Nov 2021 13:28:10 +0800 Subject: [PATCH] [Utils] Make type as the argument of the container template. --- .../DeepLearning/MobileNetBenchmark.cpp | 6 +-- benchmarks/ImageProcessing/BuddyBenchmark.cpp | 12 ++--- include/Utils/Container.h | 8 ++-- lib/Utils/Container.cpp | 46 ++++++++++--------- 4 files changed, 37 insertions(+), 35 deletions(-) diff --git a/benchmarks/DeepLearning/MobileNetBenchmark.cpp b/benchmarks/DeepLearning/MobileNetBenchmark.cpp index 150491e..3f340f4 100644 --- a/benchmarks/DeepLearning/MobileNetBenchmark.cpp +++ b/benchmarks/DeepLearning/MobileNetBenchmark.cpp @@ -27,7 +27,7 @@ using namespace std; // Declare the mobilenet C interface. extern "C" { -void _mlir_ciface_mobilenet(MemRef<2> *output, MemRef<4> *input); +void _mlir_ciface_mobilenet(MemRef<float, 2> *output, MemRef<float, 4> *input); } // TODO: Add input image preprocessing, the current preprocessing only has @@ -52,8 +52,8 @@ intptr_t stridesInput[4] = {1, image.rows, image.cols, 3}; intptr_t sizesOutnput[2] = {1, 1001}; intptr_t stridesOutput[2] = {1, 1001}; -MemRef<4> input(image, 0, sizesInput, stridesInput); -MemRef<2> output(1001, 0, sizesOutnput, stridesOutput); +MemRef<float, 4> input(image, 0, sizesInput, stridesInput); +MemRef<float, 2> output(1001, 0, sizesOutnput, stridesOutput); // Define benchmark function. static void BM_MobileNet(benchmark::State &state) { diff --git a/benchmarks/ImageProcessing/BuddyBenchmark.cpp b/benchmarks/ImageProcessing/BuddyBenchmark.cpp index 97596de..b509159 100644 --- a/benchmarks/ImageProcessing/BuddyBenchmark.cpp +++ b/benchmarks/ImageProcessing/BuddyBenchmark.cpp @@ -28,8 +28,8 @@ using namespace std; // Declare the conv2d C interface. extern "C" { -void _mlir_ciface_conv_2d(MemRef<2> *input, MemRef<2> *kernel, - MemRef<2> *output); +void _mlir_ciface_conv_2d(MemRef<float, 2> *input, MemRef<float, 2> *kernel, + MemRef<float, 2> *output); } // Read input image @@ -53,10 +53,10 @@ intptr_t stridesKernel[2] = {kernelRows, kernelCols}; intptr_t stridesOutput[2] = {outputRows, outputCols}; // Define input, kernel, and output. -MemRef<2> input(inputImageBuddy, 0, sizesInput, stridesInput); -MemRef<2> kernel(laplacianKernelRows, laplacianKernelCols, laplacianKernelAlign, - 0, sizesKernel, stridesKernel); -MemRef<2> output(outputRows, outputCols, 0, sizesOutput, stridesOutput); +MemRef<float, 2> input(inputImageBuddy, 0, sizesInput, stridesInput); +MemRef<float, 2> kernel(laplacianKernelRows, laplacianKernelCols, + laplacianKernelAlign, 0, sizesKernel, stridesKernel); +MemRef<float, 2> output(outputRows, outputCols, 0, sizesOutput, stridesOutput); static void BM_Buddy(benchmark::State &state) { for (auto _ : state) { diff --git a/include/Utils/Container.h b/include/Utils/Container.h index a95d682..7133e48 100644 --- a/include/Utils/Container.h +++ b/include/Utils/Container.h @@ -25,9 +25,9 @@ #include <opencv2/opencv.hpp> #include <stdint.h> -template <int Dim> struct MemRef { +template <typename T, size_t Dim> struct MemRef { public: - MemRef(intptr_t rows, intptr_t cols, float *aligned, intptr_t offset, + MemRef(intptr_t rows, intptr_t cols, T *aligned, intptr_t offset, intptr_t sizes[Dim], intptr_t strides[Dim]); MemRef(cv::Mat image, intptr_t offset, intptr_t sizes[Dim], intptr_t strides[Dim]); @@ -36,8 +36,8 @@ public: MemRef(intptr_t results, intptr_t offset, intptr_t sizes[Dim], intptr_t strides[Dim]); ~MemRef(); - float *allocated; - float *aligned; + T *allocated; + T *aligned; intptr_t offset; intptr_t sizes[Dim]; intptr_t strides[Dim]; diff --git a/lib/Utils/Container.cpp b/lib/Utils/Container.cpp index 2f1652a..7af5279 100644 --- a/lib/Utils/Container.cpp +++ b/lib/Utils/Container.cpp @@ -21,14 +21,14 @@ #ifndef UTILS_CONTAINER_DEF #define UTILS_CONTAINER_DEF -#include <memory> #include "Utils/Container.h" +#include <memory> -template <int Dim> -MemRef<Dim>::MemRef(intptr_t rows, intptr_t cols, float *aligned, - intptr_t offset, intptr_t sizes[Dim], - intptr_t strides[Dim]) { - auto ptr = new float[rows * cols]; +template <typename T, size_t Dim> +MemRef<T, Dim>::MemRef(intptr_t rows, intptr_t cols, T *aligned, + intptr_t offset, intptr_t sizes[Dim], + intptr_t strides[Dim]) { + auto ptr = new T[rows * cols]; this->allocated = ptr; this->aligned = ptr; int k = 0; @@ -45,25 +45,25 @@ MemRef<Dim>::MemRef(intptr_t rows, intptr_t cols, float *aligned, this->strides[j] = strides[j]; } -template <int Dim> -MemRef<Dim>::MemRef(cv::Mat image, intptr_t offset, intptr_t sizes[Dim], - intptr_t strides[Dim]) { +template <typename T, size_t Dim> +MemRef<T, Dim>::MemRef(cv::Mat image, intptr_t offset, intptr_t sizes[Dim], + intptr_t strides[Dim]) { // Copy image pixels for image processing memref. if (Dim == 2) { - auto ptr = new float[image.rows * image.cols]; + auto ptr = new T[image.rows * image.cols]; this->allocated = ptr; this->aligned = ptr; int k = 0; for (int i = 0; i < image.rows; i++) { for (int j = 0; j < image.cols; j++) { - this->aligned[k] = (float)image.at<uchar>(i, j); + this->aligned[k] = (T)image.at<uchar>(i, j); k++; } } } // Copy image pixels for deep learning tensors. if (Dim == 4) { - auto ptr = new float[image.rows * image.cols * 3]; + auto ptr = new T[image.rows * image.cols * 3]; this->allocated = ptr; this->aligned = ptr; int k = 0; @@ -72,7 +72,7 @@ MemRef<Dim>::MemRef(cv::Mat image, intptr_t offset, intptr_t sizes[Dim], for (int j = 0; j < image.cols; j++) { for (int color = 0; color < 3; color++) { // Reorder to RGB layout. - this->aligned[k] = (float)image.at<cv::Vec3b>(i, j)[2 - color]; + this->aligned[k] = (T)image.at<cv::Vec3b>(i, j)[2 - color]; k++; } } @@ -85,10 +85,10 @@ MemRef<Dim>::MemRef(cv::Mat image, intptr_t offset, intptr_t sizes[Dim], this->strides[j] = strides[j]; } -template <int Dim> -MemRef<Dim>::MemRef(intptr_t rows, intptr_t cols, intptr_t offset, - intptr_t sizes[Dim], intptr_t strides[Dim]) { - auto ptr = new float[rows * cols]; +template <typename T, size_t Dim> +MemRef<T, Dim>::MemRef(intptr_t rows, intptr_t cols, intptr_t offset, + intptr_t sizes[Dim], intptr_t strides[Dim]) { + auto ptr = new T[rows * cols]; this->allocated = ptr; this->aligned = ptr; this->offset = offset; @@ -99,10 +99,10 @@ MemRef<Dim>::MemRef(intptr_t rows, intptr_t cols, intptr_t offset, } // Constructor for deep learning output. -template <int Dim> -MemRef<Dim>::MemRef(intptr_t results, intptr_t offset, intptr_t sizes[Dim], - intptr_t strides[Dim]) { - auto ptr = new float[results]; +template <typename T, size_t Dim> +MemRef<T, Dim>::MemRef(intptr_t results, intptr_t offset, intptr_t sizes[Dim], + intptr_t strides[Dim]) { + auto ptr = new T[results]; this->allocated = ptr; this->aligned = ptr; for (int i = 0; i < results; i++) { @@ -115,6 +115,8 @@ MemRef<Dim>::MemRef(intptr_t results, intptr_t offset, intptr_t sizes[Dim], this->strides[j] = strides[j]; } -template <int Dim> MemRef<Dim>::~MemRef() { delete[] this->allocated; } +template <typename T, size_t Dim> MemRef<T, Dim>::~MemRef() { + delete[] this->allocated; +} #endif // UTILS_CONTAINER_DEF -- GitLab