BenchmarkTopologyAlgorithms.cxx 14.6 KB
Newer Older
1 2 3 4 5 6 7 8
//============================================================================
//  Copyright (c) Kitware, Inc.
//  All rights reserved.
//  See LICENSE.txt 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.
//
9
//  Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
10 11 12
//  Copyright 2014 UT-Battelle, LLC.
//  Copyright 2014 Los Alamos National Security.
//
13
//  Under the terms of Contract DE-NA0003525 with NTESS,
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
//  the U.S. Government retains certain rights in this software.
//
//  Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
//  Laboratory (LANL), the U.S. Government retains certain rights in
//  this software.
//============================================================================
#include <vtkm/Math.h>
#include <vtkm/VectorAnalysis.h>

#include <vtkm/cont/ArrayHandle.h>
#include <vtkm/cont/CellSetStructured.h>
#include <vtkm/cont/Timer.h>

#include <vtkm/worklet/DispatcherMapField.h>
#include <vtkm/worklet/DispatcherMapTopology.h>
29 30
#include <vtkm/worklet/WorkletMapField.h>
#include <vtkm/worklet/WorkletMapTopology.h>
31

32
#include "Benchmarker.h"
33
#include <vtkm/cont/testing/Testing.h>
34

35
#include <cctype>
36
#include <random>
37 38
#include <string>

39 40 41 42
namespace vtkm
{
namespace benchmarking
{
43 44

#define CUBE_SIZE 256
45
static const std::string DIVIDER(40, '-');
46

47 48
enum BenchmarkName
{
49 50 51
  CELL_TO_POINT = 1 << 1,
  POINT_TO_CELL = 1 << 2,
  MC_CLASSIFY = 1 << 3,
52
  ALL = CELL_TO_POINT | POINT_TO_CELL | MC_CLASSIFY
53 54 55 56 57
};

class AveragePointToCell : public vtkm::worklet::WorkletMapPointToCell
{
public:
58
  using ControlSignature = void(FieldInPoint<> inPoints,
59
                                CellSetIn cellset,
60
                                FieldOutCell<> outCells);
61
  using ExecutionSignature = void(_1, PointCount, _3);
62
  using InputDomain = _2;
63

64 65
  template <typename PointValueVecType, typename OutType>
  VTKM_EXEC void operator()(const PointValueVecType& pointValues,
66 67
                            const vtkm::IdComponent& numPoints,
                            OutType& average) const
68 69 70
  {
    OutType sum = static_cast<OutType>(pointValues[0]);
    for (vtkm::IdComponent pointIndex = 1; pointIndex < numPoints; ++pointIndex)
71
    {
72
      sum = sum + static_cast<OutType>(pointValues[pointIndex]);
73
    }
74 75 76 77 78 79 80 81

    average = sum / static_cast<OutType>(numPoints);
  }
};

class AverageCellToPoint : public vtkm::worklet::WorkletMapCellToPoint
{
public:
82 83
  using ControlSignature = void(FieldInCell<> inCells, CellSetIn topology, FieldOut<> outPoints);
  using ExecutionSignature = void(_1, _3, CellCount);
84
  using InputDomain = _2;
85

86
  template <typename CellVecType, typename OutType>
87 88
  VTKM_EXEC void operator()(const CellVecType& cellValues,
                            OutType& avgVal,
89
                            const vtkm::IdComponent& numCellIDs) const
90 91
  {
    //simple functor that returns the average cell Value.
92 93
    avgVal = vtkm::TypeTraits<OutType>::ZeroInitialization();
    if (numCellIDs != 0)
94
    {
95 96 97 98 99
      for (vtkm::IdComponent cellIndex = 0; cellIndex < numCellIDs; ++cellIndex)
      {
        avgVal += static_cast<OutType>(cellValues[cellIndex]);
      }
      avgVal = avgVal / static_cast<OutType>(numCellIDs);
100 101 102 103 104
    }
  }
};

// -----------------------------------------------------------------------------
105
template <typename T>
106 107 108
class Classification : public vtkm::worklet::WorkletMapPointToCell
{
public:
109
  using ControlSignature = void(FieldInPoint<> inNodes,
110
                                CellSetIn cellset,
111
                                FieldOutCell<IdComponentType> outCaseId);
112
  using ExecutionSignature = void(_1, _3);
113
  using InputDomain = _2;
114 115 116

  T IsoValue;

117
  VTKM_CONT
118 119
  Classification(T isovalue)
    : IsoValue(isovalue)
120 121 122
  {
  }

123 124
  template <typename FieldInType>
  VTKM_EXEC void operator()(const FieldInType& fieldIn, vtkm::IdComponent& caseNumber) const
125
  {
126
    using FieldType = typename vtkm::VecTraits<FieldInType>::ComponentType;
127 128
    const FieldType iso = static_cast<FieldType>(this->IsoValue);

129 130 131
    caseNumber = ((fieldIn[0] > iso) | (fieldIn[1] > iso) << 1 | (fieldIn[2] > iso) << 2 |
                  (fieldIn[3] > iso) << 3 | (fieldIn[4] > iso) << 4 | (fieldIn[5] > iso) << 5 |
                  (fieldIn[6] > iso) << 6 | (fieldIn[7] > iso) << 7);
132 133 134
  }
};

135 136 137 138
struct ValueTypes
  : vtkm::ListTagBase<vtkm::UInt32, vtkm::Int32, vtkm::Int64, vtkm::Float32, vtkm::Float64>
{
};
139 140 141

/// This class runs a series of micro-benchmarks to measure
/// performance of different field operations
142 143 144
template <class DeviceAdapterTag>
class BenchmarkTopologyAlgorithms
{
145
  using StorageTag = vtkm::cont::StorageTagBasic;
146

147
  using Timer = vtkm::cont::Timer<DeviceAdapterTag>;
148

149
  using ValueVariantHandle = vtkm::cont::ArrayHandleVariantBase<ValueTypes>;
150

151
private:
152 153 154 155
  template <typename T, typename Enable = void>
  struct NumberGenerator
  {
  };
156

157 158
  template <typename T>
  struct NumberGenerator<T, typename std::enable_if<std::is_floating_point<T>::value>::type>
159 160 161
  {
    std::mt19937 rng;
    std::uniform_real_distribution<T> distribution;
162 163 164 165 166
    NumberGenerator(T low, T high)
      : rng()
      , distribution(low, high)
    {
    }
167 168 169
    T next() { return distribution(rng); }
  };

170 171
  template <typename T>
  struct NumberGenerator<T, typename std::enable_if<!std::is_floating_point<T>::value>::type>
172 173 174 175
  {
    std::mt19937 rng;
    std::uniform_int_distribution<T> distribution;

176 177 178 179 180
    NumberGenerator(T low, T high)
      : rng()
      , distribution(low, high)
    {
    }
181 182 183
    T next() { return distribution(rng); }
  };

184 185 186 187 188
  template <typename Value>
  struct BenchCellToPointAvg
  {
    std::vector<Value> input;
    vtkm::cont::ArrayHandle<Value, StorageTag> InputHandle;
189
    std::size_t DomainSize;
190

191
    VTKM_CONT
192 193
    BenchCellToPointAvg()
    {
194
      NumberGenerator<Value> generator(static_cast<Value>(1.0), static_cast<Value>(100.0));
195
      //cube size is points in each dim
196 197 198
      this->DomainSize = (CUBE_SIZE - 1) * (CUBE_SIZE - 1) * (CUBE_SIZE - 1);
      this->input.resize(DomainSize);
      for (std::size_t i = 0; i < DomainSize; ++i)
199
      {
200
        this->input[i] = generator.next();
201 202 203 204
      }
      this->InputHandle = vtkm::cont::make_ArrayHandle(this->input);
    }

205
    VTKM_CONT
206 207 208
    vtkm::Float64 operator()()
    {
      vtkm::cont::CellSetStructured<3> cellSet;
209 210
      cellSet.SetPointDimensions(vtkm::Id3(CUBE_SIZE, CUBE_SIZE, CUBE_SIZE));
      vtkm::cont::ArrayHandle<Value, StorageTag> result;
211 212 213

      Timer timer;

214
      vtkm::worklet::DispatcherMapTopology<AverageCellToPoint> dispatcher;
215
      dispatcher.SetDevice(DeviceAdapterTag());
216
      dispatcher.Invoke(this->InputHandle, cellSet, result);
217 218 219 220

      return timer.GetElapsedTime();
    }

221 222
    virtual std::string Type() const { return std::string("Static"); }

223
    VTKM_CONT
224 225
    std::string Description() const
    {
226

227
      std::stringstream description;
228 229 230
      description << "Computing Cell To Point Average "
                  << "[" << this->Type() << "] "
                  << "with a domain size of: " << this->DomainSize;
231 232 233 234
      return description.str();
    }
  };

235 236 237
  template <typename Value>
  struct BenchCellToPointAvgDynamic : public BenchCellToPointAvg<Value>
  {
238

239
    VTKM_CONT
240 241 242
    vtkm::Float64 operator()()
    {
      vtkm::cont::CellSetStructured<3> cellSet;
243
      cellSet.SetPointDimensions(vtkm::Id3(CUBE_SIZE, CUBE_SIZE, CUBE_SIZE));
244

245
      ValueVariantHandle dinput(this->InputHandle);
246
      vtkm::cont::ArrayHandle<Value, StorageTag> result;
247 248 249

      Timer timer;

250
      vtkm::worklet::DispatcherMapTopology<AverageCellToPoint> dispatcher;
251
      dispatcher.SetDevice(DeviceAdapterTag());
252
      dispatcher.Invoke(dinput, cellSet, result);
253 254 255 256 257 258 259

      return timer.GetElapsedTime();
    }

    virtual std::string Type() const { return std::string("Dynamic"); }
  };

260
  VTKM_MAKE_BENCHMARK(CellToPointAvg, BenchCellToPointAvg);
261
  VTKM_MAKE_BENCHMARK(CellToPointAvgDynamic, BenchCellToPointAvgDynamic);
262

263 264 265 266 267
  template <typename Value>
  struct BenchPointToCellAvg
  {
    std::vector<Value> input;
    vtkm::cont::ArrayHandle<Value, StorageTag> InputHandle;
268
    std::size_t DomainSize;
269

270
    VTKM_CONT
271 272
    BenchPointToCellAvg()
    {
273
      NumberGenerator<Value> generator(static_cast<Value>(1.0), static_cast<Value>(100.0));
274

275 276 277
      this->DomainSize = (CUBE_SIZE) * (CUBE_SIZE) * (CUBE_SIZE);
      this->input.resize(DomainSize);
      for (std::size_t i = 0; i < DomainSize; ++i)
278
      {
279
        this->input[i] = generator.next();
280 281 282 283
      }
      this->InputHandle = vtkm::cont::make_ArrayHandle(this->input);
    }

284
    VTKM_CONT
285 286 287
    vtkm::Float64 operator()()
    {
      vtkm::cont::CellSetStructured<3> cellSet;
288 289
      cellSet.SetPointDimensions(vtkm::Id3(CUBE_SIZE, CUBE_SIZE, CUBE_SIZE));
      vtkm::cont::ArrayHandle<Value, StorageTag> result;
290 291 292

      Timer timer;

293
      vtkm::worklet::DispatcherMapTopology<AveragePointToCell> dispatcher;
294
      dispatcher.SetDevice(DeviceAdapterTag());
295
      dispatcher.Invoke(this->InputHandle, cellSet, result);
296 297 298 299

      return timer.GetElapsedTime();
    }

300 301
    virtual std::string Type() const { return std::string("Static"); }

302
    VTKM_CONT
303 304
    std::string Description() const
    {
305

306
      std::stringstream description;
307 308 309
      description << "Computing Point To Cell Average "
                  << "[" << this->Type() << "] "
                  << "with a domain size of: " << this->DomainSize;
310 311 312 313
      return description.str();
    }
  };

314 315 316
  template <typename Value>
  struct BenchPointToCellAvgDynamic : public BenchPointToCellAvg<Value>
  {
317

318
    VTKM_CONT
319 320 321
    vtkm::Float64 operator()()
    {
      vtkm::cont::CellSetStructured<3> cellSet;
322
      cellSet.SetPointDimensions(vtkm::Id3(CUBE_SIZE, CUBE_SIZE, CUBE_SIZE));
323

324
      ValueVariantHandle dinput(this->InputHandle);
325
      vtkm::cont::ArrayHandle<Value, StorageTag> result;
326 327 328

      Timer timer;

329
      vtkm::worklet::DispatcherMapTopology<AveragePointToCell> dispatcher;
330
      dispatcher.SetDevice(DeviceAdapterTag());
331
      dispatcher.Invoke(dinput, cellSet, result);
332 333 334 335 336 337 338

      return timer.GetElapsedTime();
    }

    virtual std::string Type() const { return std::string("Dynamic"); }
  };

339
  VTKM_MAKE_BENCHMARK(PointToCellAvg, BenchPointToCellAvg);
340
  VTKM_MAKE_BENCHMARK(PointToCellAvgDynamic, BenchPointToCellAvgDynamic);
341

342 343 344 345 346
  template <typename Value>
  struct BenchClassification
  {
    std::vector<Value> input;
    vtkm::cont::ArrayHandle<Value, StorageTag> InputHandle;
347
    Value IsoValue;
348
    size_t DomainSize;
349

350
    VTKM_CONT
351 352
    BenchClassification()
    {
353
      NumberGenerator<Value> generator(static_cast<Value>(1.0), static_cast<Value>(100.0));
354

355 356 357
      this->DomainSize = (CUBE_SIZE) * (CUBE_SIZE) * (CUBE_SIZE);
      this->input.resize(DomainSize);
      for (std::size_t i = 0; i < DomainSize; ++i)
358
      {
359
        this->input[i] = generator.next();
360 361
      }
      this->InputHandle = vtkm::cont::make_ArrayHandle(this->input);
362
      this->IsoValue = generator.next();
363 364
    }

365
    VTKM_CONT
366 367 368
    vtkm::Float64 operator()()
    {
      vtkm::cont::CellSetStructured<3> cellSet;
369 370
      cellSet.SetPointDimensions(vtkm::Id3(CUBE_SIZE, CUBE_SIZE, CUBE_SIZE));
      vtkm::cont::ArrayHandle<vtkm::IdComponent, StorageTag> result;
371

372
      ValueVariantHandle dinput(this->InputHandle);
373

374 375 376
      Timer timer;

      Classification<Value> worklet(this->IsoValue);
377
      vtkm::worklet::DispatcherMapTopology<Classification<Value>> dispatcher(worklet);
378
      dispatcher.SetDevice(DeviceAdapterTag());
379
      dispatcher.Invoke(dinput, cellSet, result);
380 381 382 383

      return timer.GetElapsedTime();
    }

384 385
    virtual std::string Type() const { return std::string("Static"); }

386
    VTKM_CONT
387 388
    std::string Description() const
    {
389

390
      std::stringstream description;
391 392 393
      description << "Computing Marching Cubes Classification "
                  << "[" << this->Type() << "] "
                  << "with a domain size of: " << this->DomainSize;
394 395 396 397
      return description.str();
    }
  };

398 399 400
  template <typename Value>
  struct BenchClassificationDynamic : public BenchClassification<Value>
  {
401
    VTKM_CONT
402 403 404
    vtkm::Float64 operator()()
    {
      vtkm::cont::CellSetStructured<3> cellSet;
405 406
      cellSet.SetPointDimensions(vtkm::Id3(CUBE_SIZE, CUBE_SIZE, CUBE_SIZE));
      vtkm::cont::ArrayHandle<vtkm::IdComponent, StorageTag> result;
407 408 409 410

      Timer timer;

      Classification<Value> worklet(this->IsoValue);
411
      vtkm::worklet::DispatcherMapTopology<Classification<Value>> dispatcher(worklet);
412
      dispatcher.SetDevice(DeviceAdapterTag());
413
      dispatcher.Invoke(this->InputHandle, cellSet, result);
414 415 416 417 418 419 420

      return timer.GetElapsedTime();
    }

    virtual std::string Type() const { return std::string("Dynamic"); }
  };

421
  VTKM_MAKE_BENCHMARK(Classification, BenchClassification);
422
  VTKM_MAKE_BENCHMARK(ClassificationDynamic, BenchClassificationDynamic);
423 424

public:
425 426
  static VTKM_CONT int Run(int benchmarks)
  {
427 428
    std::cout << DIVIDER << "\nRunning Topology Algorithm benchmarks\n";

429 430
    if (benchmarks & CELL_TO_POINT)
    {
431 432
      std::cout << DIVIDER << "\nBenchmarking Cell To Point Average\n";
      VTKM_RUN_BENCHMARK(CellToPointAvg, ValueTypes());
433
      VTKM_RUN_BENCHMARK(CellToPointAvgDynamic, ValueTypes());
434 435
    }

436 437
    if (benchmarks & POINT_TO_CELL)
    {
438 439
      std::cout << DIVIDER << "\nBenchmarking Point to Cell Average\n";
      VTKM_RUN_BENCHMARK(PointToCellAvg, ValueTypes());
440
      VTKM_RUN_BENCHMARK(PointToCellAvgDynamic, ValueTypes());
441 442
    }

443 444
    if (benchmarks & MC_CLASSIFY)
    {
445 446
      std::cout << DIVIDER << "\nBenchmarking Hex/Voxel MC Classification\n";
      VTKM_RUN_BENCHMARK(Classification, ValueTypes());
447
      VTKM_RUN_BENCHMARK(ClassificationDynamic, ValueTypes());
448 449 450 451 452 453 454 455 456 457
    }

    return 0;
  }
};

#undef ARRAY_SIZE
}
} // namespace vtkm::benchmarking

458
int main(int argc, char* argv[])
459
{
460 461
  vtkm::cont::InitLogging(argc, argv);

462
  int benchmarks = 0;
463 464
  if (argc < 2)
  {
465 466
    benchmarks = vtkm::benchmarking::ALL;
  }
467 468 469 470
  else
  {
    for (int i = 1; i < argc; ++i)
    {
471
      std::string arg = argv[i];
472 473 474
      std::transform(arg.begin(), arg.end(), arg.begin(), [](char c) {
        return static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
      });
475 476 477 478 479 480 481 482 483 484 485 486
      if (arg == "celltopoint")
      {
        benchmarks |= vtkm::benchmarking::CELL_TO_POINT;
      }
      else if (arg == "pointtocell")
      {
        benchmarks |= vtkm::benchmarking::POINT_TO_CELL;
      }
      else if (arg == "classify")
      {
        benchmarks |= vtkm::benchmarking::MC_CLASSIFY;
      }
487 488
      else
      {
489 490 491 492 493 494 495
        std::cout << "Unrecognized benchmark: " << argv[i] << std::endl;
        return 1;
      }
    }
  }

  //now actually execute the benchmarks
496 497 498 499 500
  using Device = VTKM_DEFAULT_DEVICE_ADAPTER_TAG;
  auto tracker = vtkm::cont::GetGlobalRuntimeDeviceTracker();
  tracker.ForceDevice(Device{});

  return vtkm::benchmarking::BenchmarkTopologyAlgorithms<Device>::Run(benchmarks);
501
}