Changeset View
Changeset View
Standalone View
Standalone View
MicroBenchmarks/ImageProcessing/Dilate/main.cpp
/** | |||||
Pankaj Kukreja | |||||
github.com/proton0001 | |||||
Indian Institute of Technology Hyderabad | |||||
*/ | |||||
#include "ImageHelper.h" | |||||
#include "dilate.h" | |||||
#include <iostream> | |||||
#define BENCHMARK_LIB | |||||
#ifdef BENCHMARK_LIB | |||||
#include "benchmark/benchmark.h" | |||||
#endif | |||||
typedef int inputMatrix[HEIGHT][WIDTH]; | |||||
inputMatrix *inputImage; | |||||
extern "C" void dilateKernel(int height, int width, int *inputImage, | |||||
int *outputImage, int *temporary); | |||||
int main(int argc, char *argv[]) { | |||||
#ifdef BENCHMARK_LIB | |||||
::benchmark::Initialize(&argc, argv); | |||||
#endif | |||||
const char *dilateOutputFileName = (const char *)"./dilateOutput.txt"; | |||||
inputImage = (inputMatrix *)malloc(sizeof(inputMatrix)); | |||||
if (inputImage == NULL) { | |||||
std::cerr << "Insufficient memory\n"; | |||||
exit(EXIT_FAILURE); | |||||
} | |||||
initializeRandomImage((int *)*inputImage, HEIGHT, WIDTH); | |||||
#ifdef BENCHMARK_LIB | |||||
::benchmark::RunSpecifiedBenchmarks(); | |||||
#endif | |||||
typedef int outputMatrix[HEIGHT][WIDTH]; | |||||
outputMatrix *outputImage; | |||||
outputImage = (outputMatrix *)malloc(sizeof(outputMatrix)); | |||||
outputMatrix *temp; | |||||
temp = (outputMatrix *)malloc(sizeof(outputMatrix)); | |||||
if (outputImage == NULL || temp == NULL) { | |||||
std::cerr << "Insufficient memory\n"; | |||||
exit(EXIT_FAILURE); | |||||
} | |||||
dilateKernel(HEIGHT, WIDTH, (int *)*inputImage, (int *)*outputImage, | |||||
(int *)*temp); | |||||
for (int j = 0; j < WIDTH; j++) { | |||||
(*outputImage)[0][j] = 0; | |||||
(*outputImage)[(HEIGHT - 1)][j] = 0; | |||||
} | |||||
for (int i = 0; i < HEIGHT; i++) { | |||||
(*outputImage)[i][0] = 0; | |||||
(*outputImage)[i][(WIDTH - 1)] = 0; | |||||
} | |||||
saveImage((int *)*outputImage, dilateOutputFileName, HEIGHT, WIDTH); | |||||
free(temp); | |||||
free(outputImage); | |||||
free(inputImage); | |||||
return (EXIT_SUCCESS); | |||||
} | |||||
#ifdef BENCHMARK_LIB | |||||
void BENCHMARK_DILATE(benchmark::State &state) { | |||||
/* taking height = width */ | |||||
int height = state.range(0); | |||||
int width = state.range(0); | |||||
typedef int outputMatrix[height][width]; | |||||
outputMatrix *outputImage; | |||||
outputImage = (outputMatrix *)malloc(sizeof(outputMatrix)); | |||||
outputMatrix *temp; | |||||
proton: this call is made to warm up the cache | |||||
temp = (outputMatrix *)malloc(sizeof(outputMatrix)); | |||||
if (outputImage == NULL) { | |||||
std::cerr << "Insufficient memory\n"; | |||||
exit(EXIT_FAILURE); | |||||
} | |||||
/* This call is to warm up the cache */ | |||||
Not Done ReplyInline ActionsUsing the output image so that the "dilateKernel" calls above are not optimized out. This is is always false thought because we never pass state.range(0) as 20. proton: Using the output image so that the "dilateKernel" calls above are not optimized out. This is is… | |||||
Not Done ReplyInline ActionsCould you add this as a comment into the source file? Meinersbur: Could you add this as a comment into the source file? | |||||
dilateKernel(height, width, (int *)*inputImage, (int *)*outputImage, | |||||
(int *)*temp); | |||||
for (auto _ : state) { | |||||
dilateKernel(height, width, (int *)*inputImage, (int *)*outputImage, | |||||
(int *)*temp); | |||||
} | |||||
/* Since we are not passing state.range as 20 this if case will always be | |||||
* false. This call is to make compiler think that outputImage may be used | |||||
* later so that above kernel calls will not optimize out */ | |||||
if (state.range(0) == 20) { | |||||
saveImage((int *)*outputImage, (const char *)"testFailed.txt", height, | |||||
width); | |||||
} | |||||
free(temp); | |||||
free(outputImage); | |||||
} | |||||
#define MINIMUM_DIM (HEIGHT > WIDTH) ? WIDTH : HEIGHT | |||||
#if MINIMUM_DIM > 512 | |||||
BENCHMARK(BENCHMARK_DILATE) | |||||
->RangeMultiplier(2) | |||||
->Range(128, MINIMUM_DIM) | |||||
->Unit(benchmark::kMicrosecond); | |||||
#else | |||||
BENCHMARK(BENCHMARK_DILATE) | |||||
->RangeMultiplier(2) | |||||
->Range(1, MINIMUM_DIM) | |||||
->Unit(benchmark::kMicrosecond); | |||||
#endif | |||||
#endif |
this call is made to warm up the cache