BenchmarkFilters.cxx 40.1 KB
Newer Older
Allison Vacanti's avatar
Allison Vacanti committed
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
//============================================================================
//  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.
//
//  Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
//  Copyright 2014 UT-Battelle, LLC.
//  Copyright 2014 Los Alamos National Security.
//
//  Under the terms of Contract DE-NA0003525 with NTESS,
//  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 "Benchmarker.h"

#include <vtkm/ListTag.h>
#include <vtkm/Math.h>
#include <vtkm/Range.h>
#include <vtkm/VecTraits.h>

#include <vtkm/cont/ArrayHandle.h>
#include <vtkm/cont/ArrayHandleUniformPointCoordinates.h>
#include <vtkm/cont/CellSetExplicit.h>
#include <vtkm/cont/CellSetSingleType.h>
#include <vtkm/cont/CellSetStructured.h>
#include <vtkm/cont/DataSet.h>
#include <vtkm/cont/ErrorInternal.h>
35
#include <vtkm/cont/Logging.h>
Allison Vacanti's avatar
Allison Vacanti committed
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
#include <vtkm/cont/RuntimeDeviceTracker.h>
#include <vtkm/cont/StorageBasic.h>
#include <vtkm/cont/Timer.h>

#include <vtkm/filter/CellAverage.h>
#include <vtkm/filter/ExternalFaces.h>
#include <vtkm/filter/FieldSelection.h>
#include <vtkm/filter/Gradient.h>
#include <vtkm/filter/MarchingCubes.h>
#include <vtkm/filter/PointAverage.h>
#include <vtkm/filter/PolicyBase.h>
#include <vtkm/filter/Tetrahedralize.h>
#include <vtkm/filter/Threshold.h>
#include <vtkm/filter/ThresholdPoints.h>
#include <vtkm/filter/VectorMagnitude.h>
#include <vtkm/filter/VertexClustering.h>
#include <vtkm/filter/WarpScalar.h>
#include <vtkm/filter/WarpVector.h>

#include <vtkm/io/reader/VTKDataSetReader.h>

#include <vtkm/worklet/DispatcherMapField.h>
#include <vtkm/worklet/WaveletGenerator.h>
#include <vtkm/worklet/WorkletMapField.h>

#include <cctype> // for std::tolower
#include <sstream>
#include <type_traits>

65
#ifdef VTKM_ENABLE_TBB
Allison Vacanti's avatar
Allison Vacanti committed
66
#include <tbb/task_scheduler_init.h>
67 68
#endif
#ifdef VTKM_ENABLE_OPENMP
Allison Vacanti's avatar
Allison Vacanti committed
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
#include <omp.h>
#endif

// A specific vtk dataset can be used during benchmarking using the
// "Filename [filename]" argument.

// Otherwise a wavelet dataset will be used. The size of the wavelet can be
// specified via "WaveletDim [integer]" argument. The default is 256, resulting
// in a 256x256x256 (cell extent) dataset.

// Passing in the "Tetra" option will pass the input dataset through the
// Tetrahedralize filter to generate an unstructured, single cell type dataset.

// For the filters that require fields, the desired fields may be specified
// using these arguments:
//
// PointScalars [fieldname]
// CellScalars [fieldname]
// PointVectors [fieldname]
//
// If the fields are not specified, the first field with the correct association
// is used. If no such field exists, one will be generated from the data.

// The number of benchmarks can be reduced using the ReducedOptions argument.
// All filters will be tested, but fewer variants will be used.

// For the TBB/OpenMP implementations, the number of threads can be customized
// using a "NumThreads [numThreads]" argument.

namespace
{


// unscoped enum so we can use bitwise ops without a lot of hassle:
enum BenchmarkName
{
  NONE = 0,
  GRADIENT = 1,
  THRESHOLD = 1 << 1,
  THRESHOLD_POINTS = 1 << 2,
  CELL_AVERAGE = 1 << 3,
  POINT_AVERAGE = 1 << 4,
  WARP_SCALAR = 1 << 5,
  WARP_VECTOR = 1 << 6,
  MARCHING_CUBES = 1 << 7,
  EXTERNAL_FACES = 1 << 8,
  TETRAHEDRALIZE = 1 << 9,
  VERTEX_CLUSTERING = 1 << 10,
  CELL_TO_POINT = 1 << 11,

  ALL = GRADIENT | THRESHOLD | THRESHOLD_POINTS | CELL_AVERAGE | POINT_AVERAGE | WARP_SCALAR |
    WARP_VECTOR |
    MARCHING_CUBES |
    EXTERNAL_FACES |
    TETRAHEDRALIZE |
    VERTEX_CLUSTERING |
    CELL_TO_POINT
};

static const std::string DIVIDER(40, '-');

// The input dataset we'll use on the filters (must be global, as we can't
// pass args to the benchmark functors).
static vtkm::cont::DataSet InputDataSet;
// The point scalars to use:
static std::string PointScalarsName;
// The cell scalars to use:
static std::string CellScalarsName;
// The point vectors to use:
static std::string PointVectorsName;
// Use fewer variants of each benchmark
static bool ReducedOptions;

// Limit the filter executions to only consider the following types, otherwise
// compile times and binary sizes are nuts.
using FieldTypes = vtkm::ListTagBase<vtkm::Float32,
                                     vtkm::Float64,
                                     vtkm::Vec<vtkm::Float32, 3>,
                                     vtkm::Vec<vtkm::Float64, 3>>;

using StructuredCellList = vtkm::ListTagBase<vtkm::cont::CellSetStructured<3>>;

using UnstructuredCellList =
  vtkm::ListTagBase<vtkm::cont::CellSetExplicit<>, vtkm::cont::CellSetSingleType<>>;

using AllCellList = vtkm::ListTagJoin<StructuredCellList, UnstructuredCellList>;

using CoordinateList = vtkm::ListTagBase<vtkm::Vec<vtkm::Float32, 3>, vtkm::Vec<vtkm::Float64, 3>>;

158 159 160 161 162 163 164 165 166 167
struct WaveletGeneratorDataFunctor
{
  template <typename DeviceAdapter>
  bool operator()(DeviceAdapter, vtkm::worklet::WaveletGenerator& gen)
  {
    InputDataSet = gen.GenerateDataSet<DeviceAdapter>();
    return true;
  }
};

Allison Vacanti's avatar
Allison Vacanti committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
class BenchmarkFilterPolicy : public vtkm::filter::PolicyBase<BenchmarkFilterPolicy>
{
public:
  using FieldTypeList = FieldTypes;

  using StructuredCellSetList = StructuredCellList;
  using UnstructuredCellSetList = UnstructuredCellList;
  using AllCellSetList = AllCellList;

  using CoordinateTypeList = CoordinateList;
};

// Class implementing all filter benchmarks:
class BenchmarkFilters
{
183
  using Timer = vtkm::cont::Timer;
Allison Vacanti's avatar
Allison Vacanti committed
184 185 186 187 188 189 190 191 192 193 194 195

  enum GradOpts
  {
    Gradient = 1,
    PointGradient = 1 << 1,
    Divergence = 1 << 2,
    Vorticity = 1 << 3,
    QCriterion = 1 << 4,
    RowOrdering = 1 << 5,
    ScalarInput = 1 << 6
  };

196
  template <typename, typename DeviceAdapter>
Allison Vacanti's avatar
Allison Vacanti committed
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
  struct BenchGradient
  {
    vtkm::filter::Gradient Filter;
    int Options;

    VTKM_CONT
    BenchGradient(int options)
      : Options(options)
    {
      if (options & ScalarInput)
      {
        // Some outputs require vectors:
        if (options & Divergence || options & Vorticity || options & QCriterion)
        {
          throw vtkm::cont::ErrorInternal("A requested gradient output is "
                                          "incompatible with scalar input.");
        }
        this->Filter.SetActiveField(PointScalarsName, vtkm::cont::Field::Association::POINTS);
      }
      else
      {
        this->Filter.SetActiveField(PointVectorsName, vtkm::cont::Field::Association::POINTS);
      }

      this->Filter.SetComputeGradient(static_cast<bool>(options & Gradient));
      this->Filter.SetComputePointGradient(static_cast<bool>(options & PointGradient));
      this->Filter.SetComputeDivergence(static_cast<bool>(options & Divergence));
      this->Filter.SetComputeVorticity(static_cast<bool>(options & Vorticity));
      this->Filter.SetComputeQCriterion(static_cast<bool>(options & QCriterion));

      if (options & RowOrdering)
      {
        this->Filter.SetRowMajorOrdering();
      }
      else
      {
        this->Filter.SetColumnMajorOrdering();
      }
    }

    VTKM_CONT
    vtkm::Float64 operator()()
    {
240
      Timer timer{ DeviceAdapter() };
241
      timer.Start();
Allison Vacanti's avatar
Allison Vacanti committed
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 294 295 296 297 298 299 300 301 302 303
      this->Filter.Execute(InputDataSet, BenchmarkFilterPolicy());
      return timer.GetElapsedTime();
    }

    VTKM_CONT
    std::string Description() const
    {
      std::ostringstream desc;
      desc << "Gradient filter (options=";
      if (this->Options & Gradient)
      {
        desc << "Gradient,";
      }
      if (this->Options & PointGradient)
      {
        desc << "PointGradient,";
      }
      if (this->Options & Divergence)
      {
        desc << "Divergence,";
      }
      if (this->Options & Vorticity)
      {
        desc << "Vorticity,";
      }
      if (this->Options & QCriterion)
      {
        desc << "QCriterion,";
      }
      if (this->Options & RowOrdering)
      {
        desc << "RowOrdering,";
      }
      else
      {
        desc << "ColumnOrdering,";
      }
      if (this->Options & ScalarInput)
      {
        desc << "ScalarInput,";
      }
      else
      {
        desc << "VectorInput,";
      }

      desc << ")";

      return desc.str();
    }
  };
  VTKM_MAKE_BENCHMARK(GradientScalar, BenchGradient, Gradient | ScalarInput);
  VTKM_MAKE_BENCHMARK(GradientVector, BenchGradient, Gradient);
  VTKM_MAKE_BENCHMARK(GradientVectorRow, BenchGradient, Gradient | RowOrdering);
  VTKM_MAKE_BENCHMARK(GradientPoint, BenchGradient, PointGradient);
  VTKM_MAKE_BENCHMARK(GradientDivergence, BenchGradient, Divergence);
  VTKM_MAKE_BENCHMARK(GradientVorticity, BenchGradient, Vorticity);
  VTKM_MAKE_BENCHMARK(GradientQCriterion, BenchGradient, QCriterion);
  VTKM_MAKE_BENCHMARK(GradientKitchenSink,
                      BenchGradient,
                      Gradient | PointGradient | Divergence | Vorticity | QCriterion);

304
  template <typename, typename DeviceAdapter>
Allison Vacanti's avatar
Allison Vacanti committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
  struct BenchThreshold
  {
    vtkm::filter::Threshold Filter;

    VTKM_CONT
    BenchThreshold()
    {
      auto field = InputDataSet.GetField(PointScalarsName, vtkm::cont::Field::Association::POINTS);
      auto rangeHandle = field.GetRange();
      auto range = rangeHandle.GetPortalConstControl().Get(0);

      // Extract points with values between 25-75% of the range
      vtkm::Float64 quarter = range.Length() / 4.;
      vtkm::Float64 mid = range.Center();

      this->Filter.SetActiveField(PointScalarsName, vtkm::cont::Field::Association::POINTS);
      this->Filter.SetLowerThreshold(mid - quarter);
      this->Filter.SetUpperThreshold(mid + quarter);
    }

    VTKM_CONT
    vtkm::Float64 operator()()
    {
328
      Timer timer{ DeviceAdapter() };
329
      timer.Start();
Allison Vacanti's avatar
Allison Vacanti committed
330 331 332 333 334 335 336 337 338
      this->Filter.Execute(InputDataSet, BenchmarkFilterPolicy());
      return timer.GetElapsedTime();
    }

    VTKM_CONT
    std::string Description() const { return "Threshold filter"; }
  };
  VTKM_MAKE_BENCHMARK(Threshold, BenchThreshold);

339
  template <typename, typename DeviceAdapter>
Allison Vacanti's avatar
Allison Vacanti committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
  struct BenchThresholdPoints
  {
    bool CompactPoints;
    vtkm::filter::ThresholdPoints Filter;

    VTKM_CONT
    BenchThresholdPoints(bool compactPoints)
      : CompactPoints(compactPoints)
    {
      auto field = InputDataSet.GetField(PointScalarsName, vtkm::cont::Field::Association::POINTS);
      auto rangeHandle = field.GetRange();
      auto range = rangeHandle.GetPortalConstControl().Get(0);

      // Extract points with values between 25-75% of the range
      vtkm::Float64 quarter = range.Length() / 4.;
      vtkm::Float64 mid = range.Center();

      this->Filter.SetActiveField(PointScalarsName, vtkm::cont::Field::Association::POINTS);
      this->Filter.SetLowerThreshold(mid - quarter);
      this->Filter.SetUpperThreshold(mid + quarter);

      this->Filter.SetCompactPoints(this->CompactPoints);
    }

    VTKM_CONT
    vtkm::Float64 operator()()
    {
367
      Timer timer{ DeviceAdapter() };
368
      timer.Start();
Allison Vacanti's avatar
Allison Vacanti committed
369 370 371 372 373 374 375 376 377 378
      this->Filter.Execute(InputDataSet, BenchmarkFilterPolicy());
      return timer.GetElapsedTime();
    }

    VTKM_CONT
    std::string Description() const { return "ThresholdPoints filter"; }
  };
  VTKM_MAKE_BENCHMARK(ThresholdPoints, BenchThresholdPoints, false);
  VTKM_MAKE_BENCHMARK(ThresholdPointsCompact, BenchThresholdPoints, true);

379
  template <typename, typename DeviceAdapter>
Allison Vacanti's avatar
Allison Vacanti committed
380 381 382 383 384 385 386 387 388 389 390 391 392
  struct BenchCellAverage
  {
    vtkm::filter::CellAverage Filter;

    VTKM_CONT
    BenchCellAverage()
    {
      this->Filter.SetActiveField(PointScalarsName, vtkm::cont::Field::Association::POINTS);
    }

    VTKM_CONT
    vtkm::Float64 operator()()
    {
393
      Timer timer{ DeviceAdapter() };
394
      timer.Start();
Allison Vacanti's avatar
Allison Vacanti committed
395 396 397 398 399 400 401 402 403
      this->Filter.Execute(InputDataSet, BenchmarkFilterPolicy());
      return timer.GetElapsedTime();
    }

    VTKM_CONT
    std::string Description() const { return "CellAverage filter"; }
  };
  VTKM_MAKE_BENCHMARK(CellAverage, BenchCellAverage);

404
  template <typename, typename DeviceAdapter>
Allison Vacanti's avatar
Allison Vacanti committed
405 406 407 408 409 410 411 412 413 414 415 416 417
  struct BenchPointAverage
  {
    vtkm::filter::PointAverage Filter;

    VTKM_CONT
    BenchPointAverage()
    {
      this->Filter.SetActiveField(CellScalarsName, vtkm::cont::Field::Association::CELL_SET);
    }

    VTKM_CONT
    vtkm::Float64 operator()()
    {
418
      Timer timer{ DeviceAdapter() };
419
      timer.Start();
Allison Vacanti's avatar
Allison Vacanti committed
420 421 422 423 424 425 426 427 428
      this->Filter.Execute(InputDataSet, BenchmarkFilterPolicy());
      return timer.GetElapsedTime();
    }

    VTKM_CONT
    std::string Description() const { return "PointAverage filter"; }
  };
  VTKM_MAKE_BENCHMARK(PointAverage, BenchPointAverage);

429
  template <typename, typename DeviceAdapter>
Allison Vacanti's avatar
Allison Vacanti committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
  struct BenchWarpScalar
  {
    vtkm::filter::WarpScalar Filter;

    VTKM_CONT
    BenchWarpScalar()
      : Filter(2.)
    {
      this->Filter.SetUseCoordinateSystemAsPrimaryField(true);
      this->Filter.SetNormalField(PointVectorsName, vtkm::cont::Field::Association::POINTS);
      this->Filter.SetScalarFactorField(PointScalarsName, vtkm::cont::Field::Association::POINTS);
    }

    VTKM_CONT
    vtkm::Float64 operator()()
    {
446
      Timer timer{ DeviceAdapter() };
447
      timer.Start();
Allison Vacanti's avatar
Allison Vacanti committed
448 449 450 451 452 453 454 455 456
      this->Filter.Execute(InputDataSet, BenchmarkFilterPolicy());
      return timer.GetElapsedTime();
    }

    VTKM_CONT
    std::string Description() const { return "WarpScalar filter"; }
  };
  VTKM_MAKE_BENCHMARK(WarpScalar, BenchWarpScalar);

457
  template <typename, typename DeviceAdapter>
Allison Vacanti's avatar
Allison Vacanti committed
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
  struct BenchWarpVector
  {
    vtkm::filter::WarpVector Filter;

    VTKM_CONT
    BenchWarpVector()
      : Filter(2.)
    {
      this->Filter.SetUseCoordinateSystemAsField(true);
      this->Filter.SetVectorField(PointVectorsName, vtkm::cont::Field::Association::POINTS);
    }

    VTKM_CONT
    vtkm::Float64 operator()()
    {
473
      Timer timer{ DeviceAdapter() };
474
      timer.Start();
Allison Vacanti's avatar
Allison Vacanti committed
475 476 477 478 479 480 481 482 483
      this->Filter.Execute(InputDataSet, BenchmarkFilterPolicy());
      return timer.GetElapsedTime();
    }

    VTKM_CONT
    std::string Description() const { return "WarpVector filter"; }
  };
  VTKM_MAKE_BENCHMARK(WarpVector, BenchWarpVector);

484
  template <typename, typename DeviceAdapter>
Allison Vacanti's avatar
Allison Vacanti committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
  struct BenchMarchingCubes
  {
    vtkm::filter::MarchingCubes Filter;

    VTKM_CONT
    BenchMarchingCubes(vtkm::Id numIsoVals, bool mergePoints, bool normals, bool fastNormals)
      : Filter()
    {
      this->Filter.SetActiveField(PointScalarsName, vtkm::cont::Field::Association::POINTS);

      // Set up some equally spaced contours:
      auto field = InputDataSet.GetField(PointScalarsName, vtkm::cont::Field::Association::POINTS);
      auto range = field.GetRange().GetPortalConstControl().Get(0);
      auto step = range.Length() / static_cast<vtkm::Float64>(numIsoVals + 1);
      this->Filter.SetNumberOfIsoValues(numIsoVals);
      auto val = range.Min + step;
      for (vtkm::Id i = 0; i < numIsoVals; ++i)
      {
        this->Filter.SetIsoValue(i, val);
        val += step;
      }

      this->Filter.SetMergeDuplicatePoints(mergePoints);
      this->Filter.SetGenerateNormals(normals);
      this->Filter.SetComputeFastNormalsForStructured(fastNormals);
      this->Filter.SetComputeFastNormalsForUnstructured(fastNormals);
    }

    VTKM_CONT
    vtkm::Float64 operator()()
    {
516
      Timer timer{ DeviceAdapter() };
517
      timer.Start();
Allison Vacanti's avatar
Allison Vacanti committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
      this->Filter.Execute(InputDataSet, BenchmarkFilterPolicy());
      return timer.GetElapsedTime();
    }

    VTKM_CONT
    std::string Description() const
    {
      std::ostringstream desc;
      desc << "MarchingCubes numIsoVal=" << this->Filter.GetNumberOfIsoValues() << " "
           << "mergePoints=" << this->Filter.GetMergeDuplicatePoints() << " "
           << "normals=" << this->Filter.GetGenerateNormals() << " "
           << "fastNormals=" << this->Filter.GetComputeFastNormalsForStructured();
      return desc.str();
    }
  };
  VTKM_MAKE_BENCHMARK(MarchingCubes1FFF, BenchMarchingCubes, 1, false, false, false);
  VTKM_MAKE_BENCHMARK(MarchingCubes3FFF, BenchMarchingCubes, 3, false, false, false);
  VTKM_MAKE_BENCHMARK(MarchingCubes12FFF, BenchMarchingCubes, 12, false, false, false);
  VTKM_MAKE_BENCHMARK(MarchingCubes1TFF, BenchMarchingCubes, 1, true, false, false);
  VTKM_MAKE_BENCHMARK(MarchingCubes3TFF, BenchMarchingCubes, 3, true, false, false);
  VTKM_MAKE_BENCHMARK(MarchingCubes12TFF, BenchMarchingCubes, 12, true, false, false);
  VTKM_MAKE_BENCHMARK(MarchingCubes1FTF, BenchMarchingCubes, 1, false, true, false);
  VTKM_MAKE_BENCHMARK(MarchingCubes3FTF, BenchMarchingCubes, 3, false, true, false);
  VTKM_MAKE_BENCHMARK(MarchingCubes12FTF, BenchMarchingCubes, 12, false, true, false);
  VTKM_MAKE_BENCHMARK(MarchingCubes1FTT, BenchMarchingCubes, 1, false, true, true);
  VTKM_MAKE_BENCHMARK(MarchingCubes3FTT, BenchMarchingCubes, 3, false, true, true);
  VTKM_MAKE_BENCHMARK(MarchingCubes12FTT, BenchMarchingCubes, 12, false, true, true);

546
  template <typename, typename DeviceAdapter>
Allison Vacanti's avatar
Allison Vacanti committed
547 548 549 550 551 552 553 554 555 556 557 558 559 560
  struct BenchExternalFaces
  {
    vtkm::filter::ExternalFaces Filter;

    VTKM_CONT
    BenchExternalFaces(bool compactPoints)
      : Filter()
    {
      this->Filter.SetCompactPoints(compactPoints);
    }

    VTKM_CONT
    vtkm::Float64 operator()()
    {
561
      Timer timer{ DeviceAdapter() };
562
      timer.Start();
Allison Vacanti's avatar
Allison Vacanti committed
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
      this->Filter.Execute(InputDataSet, BenchmarkFilterPolicy());
      return timer.GetElapsedTime();
    }

    VTKM_CONT
    std::string Description() const
    {
      std::ostringstream desc;
      desc << "ExternalFaces filter";
      if (this->Filter.GetCompactPoints())
      {
        desc << " (compact points)";
      }
      return desc.str();
    }
  };
  VTKM_MAKE_BENCHMARK(ExternalFaces, BenchExternalFaces, false);
  VTKM_MAKE_BENCHMARK(ExternalFacesCompact, BenchExternalFaces, true);

582
  template <typename, typename DeviceAdapter>
Allison Vacanti's avatar
Allison Vacanti committed
583 584 585 586 587 588 589 590 591 592 593 594 595
  struct BenchTetrahedralize
  {
    vtkm::filter::Tetrahedralize Filter;

    VTKM_CONT
    BenchTetrahedralize()
      : Filter()
    {
    }

    VTKM_CONT
    vtkm::Float64 operator()()
    {
596
      Timer timer{ DeviceAdapter() };
597
      timer.Start();
Allison Vacanti's avatar
Allison Vacanti committed
598 599 600 601 602 603 604 605 606
      this->Filter.Execute(InputDataSet, BenchmarkFilterPolicy());
      return timer.GetElapsedTime();
    }

    VTKM_CONT
    std::string Description() const { return "Tetrahedralize filter"; }
  };
  VTKM_MAKE_BENCHMARK(Tetrahedralize, BenchTetrahedralize);

607
  template <typename, typename DeviceAdapter>
Allison Vacanti's avatar
Allison Vacanti committed
608 609 610 611 612 613 614 615 616 617 618 619 620 621
  struct BenchVertexClustering
  {
    vtkm::filter::VertexClustering Filter;

    VTKM_CONT
    BenchVertexClustering(vtkm::Id ndims)
      : Filter()
    {
      this->Filter.SetNumberOfDivisions({ ndims });
    }

    VTKM_CONT
    vtkm::Float64 operator()()
    {
622
      Timer timer{ DeviceAdapter() };
623
      timer.Start();
Allison Vacanti's avatar
Allison Vacanti committed
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
      this->Filter.Execute(InputDataSet, BenchmarkFilterPolicy());
      return timer.GetElapsedTime();
    }

    VTKM_CONT
    std::string Description() const
    {
      vtkm::Id dims = this->Filter.GetNumberOfDivisions()[0];
      std::ostringstream desc;
      desc << "VertexClustering filter (" << dims << "x" << dims << "x" << dims << ")";
      return desc.str();
    }
  };
  VTKM_MAKE_BENCHMARK(VertexClustering32, BenchVertexClustering, 32);
  VTKM_MAKE_BENCHMARK(VertexClustering64, BenchVertexClustering, 64);
  VTKM_MAKE_BENCHMARK(VertexClustering128, BenchVertexClustering, 128);
  VTKM_MAKE_BENCHMARK(VertexClustering256, BenchVertexClustering, 256);
  VTKM_MAKE_BENCHMARK(VertexClustering512, BenchVertexClustering, 512);
  VTKM_MAKE_BENCHMARK(VertexClustering1024, BenchVertexClustering, 1024);

644
  template <typename, typename DeviceAdapter>
Allison Vacanti's avatar
Allison Vacanti committed
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
  struct BenchCellToPoint
  {
    struct PrepareForInput
    {
      mutable double Time{ 0 };

      void operator()(const vtkm::cont::CellSet& cellSet) const
      {
        static bool warned{ false };
        if (!warned)
        {
          std::cerr << "Invalid cellset type for benchmark.\n";
          cellSet.PrintSummary(std::cerr);
          warned = true;
        }
      }

      template <typename T1, typename T2, typename T3, typename T4>
      VTKM_CONT void operator()(const vtkm::cont::CellSetExplicit<T1, T2, T3, T4>& cellSet) const
      {
        { // Why does CastAndCall insist on making the cellset const?
          using CellSetT = vtkm::cont::CellSetExplicit<T1, T2, T3, T4>;
          CellSetT& mcellSet = const_cast<CellSetT&>(cellSet);
          mcellSet.ResetConnectivity(vtkm::TopologyElementTagCell{},
                                     vtkm::TopologyElementTagPoint{});
        }

672
        Timer timer{ DeviceAdapter() };
673
        timer.Start();
Allison Vacanti's avatar
Allison Vacanti committed
674
        cellSet.PrepareForInput(
675
          DeviceAdapter(), vtkm::TopologyElementTagCell{}, vtkm::TopologyElementTagPoint{});
Allison Vacanti's avatar
Allison Vacanti committed
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
        this->Time = timer.GetElapsedTime();
      }
    };

    VTKM_CONT
    BenchCellToPoint() {}

    VTKM_CONT
    vtkm::Float64 operator()()
    {
      auto cellset = InputDataSet.GetCellSet();
      PrepareForInput functor;
      cellset.CastAndCall(functor);
      return functor.Time;
    }

    VTKM_CONT
    std::string Description() const
    {
      std::ostringstream desc;
      desc << "CellToPoint table construction";
      return desc.str();
    }
  };
  VTKM_MAKE_BENCHMARK(CellToPoint, BenchCellToPoint);

public:
703
  static VTKM_CONT int Run(int benches, vtkm::cont::DeviceAdapterId id)
Allison Vacanti's avatar
Allison Vacanti committed
704 705 706 707 708 709 710 711 712 713
  {
    // This has no influence on the benchmarks. See issue #286.
    auto dummyTypes = vtkm::ListTagBase<vtkm::Int32>{};

    std::cout << DIVIDER << "\nRunning Filter benchmarks\n";

    if (benches & BenchmarkName::GRADIENT)
    {
      if (ReducedOptions)
      {
714 715 716 717
        VTKM_RUN_BENCHMARK(GradientScalar, dummyTypes, id);
        VTKM_RUN_BENCHMARK(GradientVector, dummyTypes, id);
        VTKM_RUN_BENCHMARK(GradientVectorRow, dummyTypes, id);
        VTKM_RUN_BENCHMARK(GradientKitchenSink, dummyTypes, id);
Allison Vacanti's avatar
Allison Vacanti committed
718 719 720
      }
      else
      {
721 722 723 724 725 726 727 728
        VTKM_RUN_BENCHMARK(GradientScalar, dummyTypes, id);
        VTKM_RUN_BENCHMARK(GradientVector, dummyTypes, id);
        VTKM_RUN_BENCHMARK(GradientVectorRow, dummyTypes, id);
        VTKM_RUN_BENCHMARK(GradientPoint, dummyTypes, id);
        VTKM_RUN_BENCHMARK(GradientDivergence, dummyTypes, id);
        VTKM_RUN_BENCHMARK(GradientVorticity, dummyTypes, id);
        VTKM_RUN_BENCHMARK(GradientQCriterion, dummyTypes, id);
        VTKM_RUN_BENCHMARK(GradientKitchenSink, dummyTypes, id);
Allison Vacanti's avatar
Allison Vacanti committed
729 730 731 732
      }
    }
    if (benches & BenchmarkName::THRESHOLD)
    {
733
      VTKM_RUN_BENCHMARK(Threshold, dummyTypes, id);
Allison Vacanti's avatar
Allison Vacanti committed
734 735 736
    }
    if (benches & BenchmarkName::THRESHOLD_POINTS)
    {
737 738
      VTKM_RUN_BENCHMARK(ThresholdPoints, dummyTypes, id);
      VTKM_RUN_BENCHMARK(ThresholdPointsCompact, dummyTypes, id);
Allison Vacanti's avatar
Allison Vacanti committed
739 740 741
    }
    if (benches & BenchmarkName::CELL_AVERAGE)
    {
742
      VTKM_RUN_BENCHMARK(CellAverage, dummyTypes, id);
Allison Vacanti's avatar
Allison Vacanti committed
743 744 745
    }
    if (benches & BenchmarkName::POINT_AVERAGE)
    {
746
      VTKM_RUN_BENCHMARK(PointAverage, dummyTypes, id);
Allison Vacanti's avatar
Allison Vacanti committed
747 748 749
    }
    if (benches & BenchmarkName::WARP_SCALAR)
    {
750
      VTKM_RUN_BENCHMARK(WarpScalar, dummyTypes, id);
Allison Vacanti's avatar
Allison Vacanti committed
751 752 753
    }
    if (benches & BenchmarkName::WARP_VECTOR)
    {
754
      VTKM_RUN_BENCHMARK(WarpVector, dummyTypes, id);
Allison Vacanti's avatar
Allison Vacanti committed
755 756 757 758 759
    }
    if (benches & BenchmarkName::MARCHING_CUBES)
    {
      if (ReducedOptions)
      {
760 761 762 763 764
        VTKM_RUN_BENCHMARK(MarchingCubes1FFF, dummyTypes, id);
        VTKM_RUN_BENCHMARK(MarchingCubes12FFF, dummyTypes, id);
        VTKM_RUN_BENCHMARK(MarchingCubes12TFF, dummyTypes, id);
        VTKM_RUN_BENCHMARK(MarchingCubes12FTF, dummyTypes, id);
        VTKM_RUN_BENCHMARK(MarchingCubes12FTT, dummyTypes, id);
Allison Vacanti's avatar
Allison Vacanti committed
765 766 767
      }
      else
      {
768 769 770 771 772 773 774 775 776 777 778 779
        VTKM_RUN_BENCHMARK(MarchingCubes1FFF, dummyTypes, id);
        VTKM_RUN_BENCHMARK(MarchingCubes3FFF, dummyTypes, id);
        VTKM_RUN_BENCHMARK(MarchingCubes12FFF, dummyTypes, id);
        VTKM_RUN_BENCHMARK(MarchingCubes1TFF, dummyTypes, id);
        VTKM_RUN_BENCHMARK(MarchingCubes3TFF, dummyTypes, id);
        VTKM_RUN_BENCHMARK(MarchingCubes12TFF, dummyTypes, id);
        VTKM_RUN_BENCHMARK(MarchingCubes1FTF, dummyTypes, id);
        VTKM_RUN_BENCHMARK(MarchingCubes3FTF, dummyTypes, id);
        VTKM_RUN_BENCHMARK(MarchingCubes12FTF, dummyTypes, id);
        VTKM_RUN_BENCHMARK(MarchingCubes1FTT, dummyTypes, id);
        VTKM_RUN_BENCHMARK(MarchingCubes3FTT, dummyTypes, id);
        VTKM_RUN_BENCHMARK(MarchingCubes12FTT, dummyTypes, id);
Allison Vacanti's avatar
Allison Vacanti committed
780 781 782 783
      }
    }
    if (benches & BenchmarkName::EXTERNAL_FACES)
    {
784 785
      VTKM_RUN_BENCHMARK(ExternalFaces, dummyTypes, id);
      VTKM_RUN_BENCHMARK(ExternalFacesCompact, dummyTypes, id);
Allison Vacanti's avatar
Allison Vacanti committed
786 787 788
    }
    if (benches & BenchmarkName::TETRAHEDRALIZE)
    {
789
      VTKM_RUN_BENCHMARK(Tetrahedralize, dummyTypes, id);
Allison Vacanti's avatar
Allison Vacanti committed
790 791 792 793 794
    }
    if (benches & BenchmarkName::VERTEX_CLUSTERING)
    {
      if (ReducedOptions)
      {
795 796 797
        VTKM_RUN_BENCHMARK(VertexClustering32, dummyTypes, id);
        VTKM_RUN_BENCHMARK(VertexClustering256, dummyTypes, id);
        VTKM_RUN_BENCHMARK(VertexClustering1024, dummyTypes, id);
Allison Vacanti's avatar
Allison Vacanti committed
798 799 800
      }
      else
      {
801 802 803 804 805 806
        VTKM_RUN_BENCHMARK(VertexClustering32, dummyTypes, id);
        VTKM_RUN_BENCHMARK(VertexClustering64, dummyTypes, id);
        VTKM_RUN_BENCHMARK(VertexClustering128, dummyTypes, id);
        VTKM_RUN_BENCHMARK(VertexClustering256, dummyTypes, id);
        VTKM_RUN_BENCHMARK(VertexClustering512, dummyTypes, id);
        VTKM_RUN_BENCHMARK(VertexClustering1024, dummyTypes, id);
Allison Vacanti's avatar
Allison Vacanti committed
807 808 809 810
      }
    }
    if (benches & BenchmarkName::CELL_TO_POINT)
    {
811
      VTKM_RUN_BENCHMARK(CellToPoint, dummyTypes, id);
Allison Vacanti's avatar
Allison Vacanti committed
812 813 814 815 816 817 818 819 820 821
    }

    return 0;
  }
};

// Generates a Vec3 field from point coordinates.
struct PointVectorGenerator : public vtkm::worklet::WorkletMapField
{
public:
822
  using ControlSignature = void(FieldIn, FieldOut);
Allison Vacanti's avatar
Allison Vacanti committed
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
  using ExecutionSignature = _2(_1);

  vtkm::Bounds Bounds;
  vtkm::Vec<vtkm::Float64, 3> Center;
  vtkm::Vec<vtkm::Float64, 3> Scale;

  VTKM_CONT
  PointVectorGenerator(const vtkm::Bounds& bounds)
    : Bounds(bounds)
    , Center(bounds.Center())
    , Scale((6. * vtkm::Pi()) / bounds.X.Length(),
            (2. * vtkm::Pi()) / bounds.Y.Length(),
            (7. * vtkm::Pi()) / bounds.Z.Length())
  {
  }

  template <typename T>
  VTKM_EXEC vtkm::Vec<T, 3> operator()(vtkm::Vec<T, 3> val) const
  {
    using Vec3T = vtkm::Vec<T, 3>;
    using Vec3F64 = vtkm::Vec<vtkm::Float64, 3>;

    Vec3F64 valF64{ val };
    Vec3F64 periodic = (valF64 - this->Center) * this->Scale;
    periodic[0] = vtkm::Sin(periodic[0]);
    periodic[1] = vtkm::Sin(periodic[1]);
    periodic[2] = vtkm::Cos(periodic[2]);

    if (vtkm::MagnitudeSquared(periodic) > 0.)
    {
      vtkm::Normalize(periodic);
    }
    if (vtkm::MagnitudeSquared(valF64) > 0.)
    {
      vtkm::Normalize(valF64);
    }
    return Vec3T{ vtkm::Normal(periodic + valF64) };
  }
};

863
// Get the number of components in a VariantArrayHandle, ArrayHandle, or Field's
Allison Vacanti's avatar
Allison Vacanti committed
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
// ValueType.
struct NumberOfComponents
{
  vtkm::IdComponent NumComponents;

  template <typename ArrayHandleT>
  VTKM_CONT void operator()(const ArrayHandleT&)
  {
    using ValueType = typename ArrayHandleT::ValueType;
    using Traits = vtkm::VecTraits<ValueType>;
    this->NumComponents = Traits::NUM_COMPONENTS;
  }

  template <typename DynamicType>
  VTKM_CONT static vtkm::IdComponent Check(const DynamicType& obj)
  {
    NumberOfComponents functor;
    vtkm::cont::CastAndCall(obj, functor);
    return functor.NumComponents;
  }
};

void FindFields(bool needPointScalars, bool needCellScalars, bool needPointVectors)
{
  if (needPointScalars && PointScalarsName.empty())
  {
    for (vtkm::Id i = 0; i < InputDataSet.GetNumberOfFields(); ++i)
    {
      auto field = InputDataSet.GetField(i);
      if (field.GetAssociation() == vtkm::cont::Field::Association::POINTS &&
          NumberOfComponents::Check(field) == 1)
      {
        PointScalarsName = field.GetName();
        std::cout << "Found PointScalars: " << PointScalarsName << "\n";
        break;
      }
    }
  }

  if (needCellScalars && CellScalarsName.empty())
  {
    for (vtkm::Id i = 0; i < InputDataSet.GetNumberOfFields(); ++i)
    {
      auto field = InputDataSet.GetField(i);
      if (field.GetAssociation() == vtkm::cont::Field::Association::CELL_SET &&
          NumberOfComponents::Check(field) == 1)
      {
        CellScalarsName = field.GetName();
        std::cout << "Found CellScalars: " << CellScalarsName << "\n";
        break;
      }
    }
  }

  if (needPointVectors && PointVectorsName.empty())
  {
    for (vtkm::Id i = 0; i < InputDataSet.GetNumberOfFields(); ++i)
    {
      auto field = InputDataSet.GetField(i);
      if (field.GetAssociation() == vtkm::cont::Field::Association::POINTS &&
          NumberOfComponents::Check(field) == 3)
      {
        PointVectorsName = field.GetName();
        std::cout << "Found CellVectors: " << PointVectorsName << "\n";
        break;
      }
    }
  }
}

void CreateFields(bool needPointScalars, bool needCellScalars, bool needPointVectors)
{
  // Do point vectors first, so we can generate the scalars from them if needed
  if (needPointVectors && PointVectorsName.empty())
  {
    // Construct them from the coordinates:
    auto coords = InputDataSet.GetCoordinateSystem();
    auto bounds = coords.GetBounds();
    auto points = coords.GetData();
    vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::FloatDefault, 3>> pvecs;

    PointVectorGenerator worklet(bounds);
946
    vtkm::worklet::DispatcherMapField<PointVectorGenerator> dispatch(worklet);
Allison Vacanti's avatar
Allison Vacanti committed
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
    dispatch.Invoke(points, pvecs);
    InputDataSet.AddField(
      vtkm::cont::Field("GeneratedPointVectors", vtkm::cont::Field::Association::POINTS, pvecs));
    PointVectorsName = "GeneratedPointVectors";
    std::cout << "Generated point vectors '" << PointVectorsName << "' from "
                                                                    "coordinate data.\n";
  }

  if (needPointScalars && PointScalarsName.empty())
  {
    // Attempt to construct them from a cell field:
    if (CellScalarsName.empty())
    { // attempt to find a set of cell scalars in the input:
      FindFields(false, true, false);
    }
    if (!CellScalarsName.empty())
    { // Generate from found cell field:
      vtkm::filter::PointAverage avg;
      avg.SetActiveField(CellScalarsName, vtkm::cont::Field::Association::CELL_SET);
      avg.SetOutputFieldName("GeneratedPointScalars");
      auto outds = avg.Execute(InputDataSet, BenchmarkFilterPolicy());
      InputDataSet.AddField(
        outds.GetField("GeneratedPointScalars", vtkm::cont::Field::Association::POINTS));
      PointScalarsName = "GeneratedPointScalars";
      std::cout << "Generated point scalars '" << PointScalarsName << "' from "
                                                                      "cell scalars, '"
                << CellScalarsName << "'.\n";
    }
    else
    { // Attempt to construct them from point vectors:
      if (PointVectorsName.empty())
      {
        FindFields(false, false, true);
      }
      if (PointVectorsName.empty())
      {
        CreateFields(false, false, true); // cannot fail
      }

      // Compute the magnitude of the vectors:
      vtkm::filter::VectorMagnitude mag;
      mag.SetActiveField(PointVectorsName, vtkm::cont::Field::Association::POINTS);
      mag.SetOutputFieldName("GeneratedPointScalars");
      auto outds = mag.Execute(InputDataSet, BenchmarkFilterPolicy());
      InputDataSet.AddField(
        outds.GetField("GeneratedPointScalars", vtkm::cont::Field::Association::POINTS));
      PointScalarsName = "GeneratedPointScalars";
      std::cout << "Generated point scalars '" << PointScalarsName << "' from "
                                                                      "point vectors, '"
                << PointVectorsName << "'.\n";
    }
  }

  if (needCellScalars && CellScalarsName.empty())
  {
    // Attempt to construct them from a point field:
    if (PointScalarsName.empty())
    { // attempt to find a set of point scalars in the input:
      FindFields(true, false, false);
    }
    if (!PointScalarsName.empty())
    { // Generate from found point field:
      vtkm::filter::CellAverage avg;
      avg.SetActiveField(PointScalarsName, vtkm::cont::Field::Association::POINTS);
      avg.SetOutputFieldName("GeneratedCellScalars");
      auto outds = avg.Execute(InputDataSet, BenchmarkFilterPolicy());
      InputDataSet.AddField(
        outds.GetField("GeneratedCellScalars", vtkm::cont::Field::Association::CELL_SET));
      CellScalarsName = "GeneratedCellScalars";
      std::cout << "Generated cell scalars '" << CellScalarsName << "' from "
                                                                    "point scalars, '"
                << PointScalarsName << "'.\n";
    }
  }
}

void AssertFields(bool needPointScalars, bool needCellScalars, bool needPointVectors)
{
  if (needPointScalars)
  {
    if (PointScalarsName.empty())
    {
      throw vtkm::cont::ErrorInternal("PointScalarsName not set!");
    }
    if (!InputDataSet.HasField(PointScalarsName, vtkm::cont::Field::Association::POINTS))
    {
      throw vtkm::cont::ErrorInternal("PointScalars field not in dataset!");
    }
  }
  if (needCellScalars)
  {
    if (CellScalarsName.empty())
    {
      throw vtkm::cont::ErrorInternal("CellScalarsName not set!");
    }
    if (!InputDataSet.HasField(CellScalarsName, vtkm::cont::Field::Association::CELL_SET))
    {
      throw vtkm::cont::ErrorInternal("CellScalars field not in dataset!");
    }
  }
  if (needPointVectors)
  {
    if (PointVectorsName.empty())
    {
      throw vtkm::cont::ErrorInternal("PointVectorsName not set!");
    }
    if (!InputDataSet.HasField(PointVectorsName, vtkm::cont::Field::Association::POINTS))
    {
      throw vtkm::cont::ErrorInternal("PointVectors field not in dataset!");
    }
  }
}

1060
int BenchmarkBody(const std::vector<std::string>& argv, vtkm::cont::DeviceAdapterId id)
Allison Vacanti's avatar
Allison Vacanti committed
1061 1062
{
  int numThreads = 1;
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
#ifdef VTKM_ENABLE_TBB
  if (id == vtkm::cont::DeviceAdapterTagTBB())
  {
    numThreads = tbb::task_scheduler_init::automatic;
  }
#endif
#ifdef VTKM_ENABLE_OPENMP
  if (id == vtkm::cont::DeviceAdapterTagOpenMP())
  {
    numThreads = omp_get_max_threads();
  }
#endif
Allison Vacanti's avatar
Allison Vacanti committed
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085

  int benches = BenchmarkName::NONE;
  std::string filename;
  vtkm::Id waveletDim = 256;
  bool tetra = false;
  bool needPointScalars = false;
  bool needCellScalars = false;
  bool needPointVectors = false;

  ReducedOptions = false;

1086
  for (size_t i = 0; i < argv.size(); ++i)
Allison Vacanti's avatar
Allison Vacanti committed
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
  {
    std::string arg = argv[i];
    std::transform(arg.begin(), arg.end(), arg.begin(), [](char c) {
      return static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
    });
    if (arg == "gradient")
    {
      benches |= BenchmarkName::GRADIENT;
      needPointScalars = true;
      needPointVectors = true;
    }
    else if (arg == "threshold")
    {
      benches |= BenchmarkName::THRESHOLD;
      needPointScalars = true;
    }
    else if (arg == "threshold_points")
    {
      benches |= BenchmarkName::THRESHOLD_POINTS;
      needPointScalars = true;
    }
    else if (arg == "cell_average")
    {
      benches |= BenchmarkName::CELL_AVERAGE;
      needPointScalars = true;
    }
    else if (arg == "point_average")
    {
      benches |= BenchmarkName::POINT_AVERAGE;
      needCellScalars = true;
    }
    else if (arg == "warp_scalar")
    {
      benches |= BenchmarkName::WARP_SCALAR;
      needPointScalars = true;
      needPointVectors = true;
    }
    else if (arg == "warp_vector")
    {
      benches |= BenchmarkName::WARP_VECTOR;
      needPointVectors = true;
    }
    else if (arg == "marching_cubes")
    {
      benches |= BenchmarkName::MARCHING_CUBES;
      needPointScalars = true;
    }
    else if (arg == "external_faces")
    {
      benches |= BenchmarkName::EXTERNAL_FACES;
    }
    else if (arg == "tetrahedralize")
    {
      benches |= BenchmarkName::TETRAHEDRALIZE;
    }
    else if (arg == "vertex_clustering")
    {
      benches |= BenchmarkName::VERTEX_CLUSTERING;
    }
    else if (arg == "cell_to_point")
    {
      benches |= BenchmarkName::CELL_TO_POINT;
    }
    else if (arg == "filename")
    {
      ++i;
      filename = argv[i];
    }
    else if (arg == "pointscalars")
    {
      ++i;
      PointScalarsName = argv[i];
    }
    else if (arg == "cellscalars")
    {
      ++i;
      CellScalarsName = argv[i];
    }
    else if (arg == "pointvectors")
    {
      ++i;
      PointVectorsName = argv[i];
    }
    else if (arg == "waveletdim")
    {
      ++i;
      std::istringstream parse(argv[i]);
      parse >> waveletDim;
    }
    else if (arg == "tetra")
    {
      tetra = true;
    }
    else if (arg == "reducedoptions")
    {
      ReducedOptions = true;
    }
    else if (arg == "numthreads")
    {
      ++i;
1187
      if (id == vtkm::cont::DeviceAdapterTagOpenMP() || id == vtkm::cont::DeviceAdapterTagTBB())
Allison Vacanti's avatar
Allison Vacanti committed
1188 1189 1190
      {
        std::istringstream parse(argv[i]);
        parse >> numThreads;
1191
        std::cout << "Selected " << numThreads << " " << id.GetName() << " threads." << std::endl;
Allison Vacanti's avatar
Allison Vacanti committed
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
      }
      else
      {
        std::cerr << "NumThreads not valid on this device. Ignoring." << std::endl;
      }
    }
    else
    {
      std::cerr << "Unrecognized option: " << argv[i] << std::endl;
      return 1;
    }
  }

1205
#ifdef VTKM_ENABLE_TBB
Allison Vacanti's avatar
Allison Vacanti committed
1206
  // Must not be destroyed as long as benchmarks are running:
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
  if (id == vtkm::cont::DeviceAdapterTagTBB())
  {
    tbb::task_scheduler_init init(numThreads);
  }
#endif
#ifdef VTKM_ENABLE_OPENMP
  if (id == vtkm::cont::DeviceAdapterTagOpenMP())
  {
    omp_set_num_threads(numThreads);
  }
#endif
Allison Vacanti's avatar
Allison Vacanti committed
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239

  if (benches == BenchmarkName::NONE)
  {
    benches = BenchmarkName::ALL;
    needPointScalars = true;
    needCellScalars = true;
    needPointVectors = true;
  }

  // Load / generate the dataset
  if (!filename.empty())
  {
    std::cout << "Loading file: " << filename << "\n";
    vtkm::io::reader::VTKDataSetReader reader(filename);
    InputDataSet = reader.ReadDataSet();
  }
  else
  {
    std::cout << "Generating " << waveletDim << "x" << waveletDim << "x" << waveletDim
              << " wavelet...\n";
    vtkm::worklet::WaveletGenerator gen;
    gen.SetExtent({ 0 }, { waveletDim });
1240 1241 1242 1243

    // WaveletGenerator needs a template device argument not a id to deduce the portal type.
    WaveletGeneratorDataFunctor genFunctor;
    vtkm::cont::TryExecuteOnDevice(id, genFunctor, gen);
Allison Vacanti's avatar
Allison Vacanti committed
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
  }

  if (tetra)
  {
    std::cout << "Tetrahedralizing dataset...\n";
    vtkm::filter::Tetrahedralize tet;
    tet.SetFieldsToPass(vtkm::filter::FieldSelection(vtkm::filter::FieldSelection::MODE_ALL));
    InputDataSet = tet.Execute(InputDataSet);
  }

  bool isStructured = InputDataSet.GetCellSet().IsType<vtkm::cont::CellSetStructured<3>>() ||
    InputDataSet.GetCellSet().IsType<vtkm::cont::CellSetStructured<2>>() ||
    InputDataSet.GetCellSet().IsType<vtkm::cont::CellSetStructured<1>>();

  // Check for incompatible options
  if (benches & BenchmarkName::TETRAHEDRALIZE && !isStructured)
  {
    std::cout << "Warning: Cannot benchmark vtkm::filter::Tetrahedralize on "
                 "unstructured datasets. Removing from options.\n";
    benches = benches ^ BenchmarkName::TETRAHEDRALIZE;
  }
  if (benches & BenchmarkName::VERTEX_CLUSTERING && isStructured)
  {
    std::cout << "Warning: Cannot benchmark vtkm::filter::VertexClustering on "
                 "structured dataset. Removing from options.\n";
    benches = benches ^ BenchmarkName::VERTEX_CLUSTERING;
  }
  if (benches & BenchmarkName::CELL_TO_POINT && isStructured)
  {
    std::cout << "Info: CellToPoint benchmark is trivial on structured datasets. "
                 "Removing from options.\n";
    benches = benches ^ BenchmarkName::CELL_TO_POINT;
  }

  // Check to see what fields already exist in the input:
  FindFields(needPointScalars, needCellScalars, needPointVectors);

  // Create any missing fields:
  CreateFields(needPointScalars, needCellScalars, needPointVectors);

  // Assert that required fields exist:
  AssertFields(needPointScalars, needCellScalars, needPointVectors);

  std::cout << "\nDataSet Summary:\n";
  InputDataSet.PrintSummary(std::cout);
  std::cout << "\n";

  //now actually execute the benchmarks
1292
  int result = BenchmarkFilters::Run(benches, id);
Allison Vacanti's avatar
Allison Vacanti committed
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303

  // Explicitly free resources before exit.
  InputDataSet.Clear();

  return result;
}

} // end anon namespace

int main(int argc, char* argv[])
{
1304 1305
  auto opts = vtkm::cont::InitializeOptions::RequireDevice;
  auto config = vtkm::cont::Initialize(argc, argv, opts);
1306

Allison Vacanti's avatar
Allison Vacanti committed
1307 1308 1309
  int retval = 1;
  try
  {
1310
    retval = BenchmarkBody(config.Arguments, config.Device);
Allison Vacanti's avatar
Allison Vacanti committed
1311 1312 1313 1314 1315 1316 1317 1318 1319
  }
  catch (std::exception& e)
  {
    std::cerr << "Benchmark encountered an exception: " << e.what() << "\n";
    return 1;
  }

  return retval;
}