aboutsummaryrefslogtreecommitdiff
path: root/MicroBenchmarks/ImageProcessing/Dilate/main.cpp
blob: 834f2d76e458db2c7af333fc2034e6c09accd907 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
  Pankaj Kukreja
  github.com/proton0001
  Indian Institute of Technology Hyderabad
*/
#include "ImageHelper.h"
#include "dilate.h"
#include <iostream>
#include <cstdlib>

#define BENCHMARK_LIB
#ifdef BENCHMARK_LIB
#include "benchmark/benchmark.h"
#endif

int *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 = (int *)malloc(sizeof(int) * HEIGHT * WIDTH);

  if (inputImage == NULL) {
    std::cerr << "Insufficient memory\n";
    exit(EXIT_FAILURE);
  }

  initializeRandomImage(inputImage, HEIGHT, WIDTH);

#ifdef BENCHMARK_LIB
  ::benchmark::RunSpecifiedBenchmarks();
#endif
  int *outputImage = (int *)malloc(sizeof(int) * HEIGHT * WIDTH);
  int *temp = (int *)malloc(sizeof(int) * HEIGHT * WIDTH);

  if (outputImage == NULL || temp == NULL) {
    std::cerr << "Insufficient memory\n";
    exit(EXIT_FAILURE);
  }

  dilateKernel(HEIGHT, WIDTH, inputImage, outputImage, temp);

  for (int j = 0; j < WIDTH; j++) {
    outputImage[0 * WIDTH + j] = 0;
    outputImage[(HEIGHT - 1) * WIDTH + j] = 0;
  }

  for (int i = 0; i < HEIGHT; i++) {
    outputImage[i * WIDTH + 0] = 0;
    outputImage[i * WIDTH + (WIDTH - 1)] = 0;
  }

  saveImage(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);

  int *outputImage = (int *)malloc(sizeof(int) * height * width);
  int *temp = (int *)malloc(sizeof(int) * height * width);
  if (outputImage == NULL) {
    std::cerr << "Insufficient memory\n";
    exit(EXIT_FAILURE);
  }

  /* This call is to warm up the cache */
  dilateKernel(height, width, inputImage, outputImage, temp);

  while (state.KeepRunning()) {
    dilateKernel(height, width, inputImage, outputImage, 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(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