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

[Utils] Make type as the argument of the container template.

parent 93ac79fa
No related branches found
No related tags found
No related merge requests found
......@@ -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) {
......
......@@ -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) {
......
......@@ -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];
......
......@@ -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
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