File: ImageBenchmarkDriver.cxx

package info (click to toggle)
vtk7 7.1.1%2Bdfsg2-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 127,396 kB
  • sloc: cpp: 1,539,584; ansic: 124,382; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 126; objc: 83
file content (293 lines) | stat: -rw-r--r-- 7,119 bytes parent folder | download | duplicates (3)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    ImageBenchmarkDriver.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/

// This program is a driver for the benchmarking tests.
// It runs several benchmarks and writes them to the output directory.

#include "vtkMultiThreader.h"
#include "vtkThreadedImageAlgorithm.h"
#include "vtkVersion.h"

#include <vtksys/Process.h>

#include <iostream>
#include <string>
#include <vector>

const char *HelpText =
"Usage: ImageBenchmarkDriver --prefix <path/prefix> ...\n"
"\n"
"Options:\n"
"  --prefix <path/prefix>  Prefix for output filenames.\n"
"  Any options from ImageBenchmark can also be used.\n"
"\n"
"Details:\n"
"\n"
"This program runs a series of image processing benchmarks,\n"
"by running ImageBenchmark with various parameters:\n"
"\n";

// A struct to give a short name to each ImageBenchmark option
struct BenchOption
{
  const char *Name;
  const char *Option;
};

// A struct to link a range of options to a benchmark parameter
struct BenchParameter
{
  const char *Parameter;
  BenchOption *Options;
};

static BenchOption FilterList[] =
{
  { "Median3",        "median:kernelsize=3" },
  { "Reslice2D",      "reslice:kernel=linear:rotation=45/0/0/1" },
  { "Reslice3D",      "reslice:kernel=linear:rotation=60/0/1/1" },
  { "Colors4",        "colormap:components=4" },
  { NULL, NULL }
};

static BenchOption SplitModeList[] =
{
  { "Slab", "slab" },
  { "Beam", "beam" },
  { "Block", "block" },
  { NULL, NULL }
};

// These are only for --enable-smp on
static BenchOption BlockByteList[] =
{
  { "1KiB",   "1024" },
  { "4KiB",   "4096" },
  { "16KiB",  "16384" },
  { "64KiB",  "65536" },
  { "256KiB", "262144" },
  { "1MiB",   "1048576" },
  { "4MiB",   "4194304" },
  { "16MiB",  "16777216" },
  { NULL, NULL }
};

static BenchOption ImageSizeList[] =
{
  { "4096x4096",   "4096x4096x1" },
  { "256x256x256", "256x256x256" },
  { NULL, NULL }
};

static BenchParameter Parameters[] =
{
  { "--filter", FilterList },
  { "--split-mode", SplitModeList },
  { "--bytes-per-piece", BlockByteList },
  { "--size", ImageSizeList },
  { NULL, NULL }
};

int main(int argc, char *argv[])
{
  // Create the path to the ImageBenchmark executable (assume that
  // it is in same directory)
  std::string exename = argv[0];
  size_t l = 0;
  for (size_t j = 0; j != exename.length(); j++)
  {
    if (exename[j] == '/' || exename[j] == '\\')
    {
      l = j + 1;
    }
  }
  exename = exename.substr(0, l) + "ImageBenchmark";

  // Go through the arguments to create args for ImageBenchmark
  bool useSMP = vtkThreadedImageAlgorithm::GetGlobalDefaultEnableSMP();
  std::string prefix;
  std::vector<const char *> args;
  args.push_back(exename.c_str());
  int argi = 1;
  while (argi < argc)
  {
    std::string arg = argv[argi];
    if (arg == "-h" || arg == "-help" || arg == "--help")
    {
      std::cout << HelpText;
      for (BenchParameter *p = Parameters; p->Parameter; p++)
      {
        std::string opt = p->Parameter;
        for (BenchOption *o = p->Options; o->Option; o++)
        {
          std::cout << "  " << opt << " " << o->Option << "    ("
                    << o->Name << ")\n";
        }
        std::cout << std::endl;
      }
      return 0;
    }
    else if (arg == "--version")
    {
      std::cout << "ImageBenchmarkDriver "
                << vtkVersion::GetVTKVersion() << "\n";
      return 0;
    }
    else if (arg == "--prefix")
    {
      if (++argi < argc)
      {
        prefix = argv[argi++];
      }
    }
    else if (arg == "--enable-smp")
    {
      args.push_back(argv[argi++]);
      if (argi < argc)
      {
        std::string s = argv[argi];
        if (s == "on" || s == "yes" || s == "true")
        {
          useSMP = true;
        }
        else if (s == "off" || s == "no" || s == "false")
        {
          useSMP = false;
        }
        args.push_back(argv[argi++]);
      }
    }
    else
    {
      args.push_back(argv[argi++]);
    }
  }

  // Count the number of benchmarks to do
  int total = 1;
  std::vector<int> overrides;
  for (BenchParameter *p = Parameters; p->Parameter; p++)
  {
    // Check if this parameter was overridden on the command line
    std::string arg = p->Parameter;
    bool overRidden = false;
    for (size_t j = 1; j < args.size(); j++)
    {
      if (arg == args[j] ||
          (!useSMP && arg == "--bytes-per-piece"))
      {
        overRidden = true;
        break;
      }
    }
    overrides.push_back(overRidden);
    if (overRidden)
    {
      continue;
    }

    int count = 0;
    for (BenchOption *o = p->Options; o->Option; o++)
    {
      count++;
    }
    total *= count;
  }

  for (int i = 0; i < total; i++)
  {
    std::string filename = prefix;
    if (prefix.empty() ||
        prefix[prefix.length()-1] == '/' ||
        prefix[prefix.length()-1] == '\\')
    {
      filename += (useSMP ? "SMP" : "MT");
    }

    std::vector<const char *> commandLine;
    commandLine.push_back(args[0]);
    for (size_t j = 1; j < args.size(); j++)
    {
      commandLine.push_back(args[j]);
    }

    int part = 1;
    std::vector<int>::iterator skip = overrides.begin();
    for (BenchParameter *p = Parameters; p->Parameter; ++p, ++skip)
    {
      if (*skip)
      {
        continue;
      }

      int count = 0;
      for (BenchOption *o = p->Options; o->Option; o++)
      {
        count++;
      }
      int k = (i/part) % count;
      commandLine.push_back(p->Parameter);
      commandLine.push_back(p->Options[k].Option);
      filename.push_back('_');
      filename += p->Options[k].Name;
      part *= count;
    }

    commandLine.push_back("--slave");
    commandLine.push_back(NULL);

    filename += ".csv";
    std::ofstream outfile(filename.c_str());

    // create and run the subprocess
    vtksysProcess *process = vtksysProcess_New();
    vtksysProcess_SetCommand(process, &commandLine[0]);
    vtksysProcess_Execute(process);

    int pipe;
    do
    {
      char *cp;
      int length;
      pipe = vtksysProcess_WaitForData(process, &cp, &length, NULL);
      switch (pipe)
      {
        case vtksysProcess_Pipe_STDOUT:
          outfile.write(cp, length);
          break;

        case vtksysProcess_Pipe_STDERR:
          std::cerr.write(cp, length);
          break;
      }
    }
    while (pipe != vtksysProcess_Pipe_None);

    vtksysProcess_WaitForExit(process, NULL);
    int rval = vtksysProcess_GetExitValue(process);
    if (rval != 0)
    {
      return rval;
    }

    vtksysProcess_Delete(process);

    outfile.close();

    std::cout << (i + 1) << " of " << total << ": " << filename << std::endl;
  }

  return 0;
}