vtk-m/vtkm/cont/testing/UnitTestAlgorithm.cxx
Kenneth Moreland 3b828608a4 Support ExecArg behavior in vtkm::cont::Algorithm methods
Most of the arguments given to device adapter algorithms are actually
control-side arguments that get converted to execution objects internally
(usually a `vtkm::cont::ArrayHandle`). However, some of the algorithms,
take an argument that is passed directly to the execution environment, such
as the predicate argument of `Sort`. If the argument is a plain-old-data
(POD) type, which is common enough, then you can just pass the object
straight through. However, if the object has any special elements that have
to be transferred to the execution environment, such as internal arrays,
passing this to the `vtkm::cont::Algorithm` functions becomes
problematic.

To cover this use case, all the `vtkm::cont::Algorithm` functions now
support automatically transferring objects that support the `ExecObject`
worklet convention. If any argument to any of the `vtkm::cont::Algorithm`
functions inherits from `vtkm::cont::ExecutionObjectBase`, then the
`PrepareForExecution` method is called with the device the algorithm is
running on, which allows these device-specific objects to be used without
the hassle of creating a `TryExecute`.
2018-07-06 18:57:54 +02:00

198 lines
5.5 KiB
C++

//============================================================================
// 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 2017 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Copyright 2017 UT-Battelle, LLC.
// Copyright 2017 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 <vtkm/cont/Algorithm.h>
#include <vtkm/TypeTraits.h>
#include <vtkm/cont/testing/Testing.h>
namespace
{
// The goal of this unit test is not to verify the correctness
// of the various algorithms. Since Algorithm is a header, we
// need to ensure we instatiate each algorithm in a source
// file to verify compilation.
//
static constexpr vtkm::Id ARRAY_SIZE = 10;
void CopyTest()
{
vtkm::cont::ArrayHandle<vtkm::Id> input;
vtkm::cont::ArrayHandle<vtkm::Id> output;
vtkm::cont::ArrayHandle<vtkm::Id> stencil;
input.Allocate(ARRAY_SIZE);
output.Allocate(ARRAY_SIZE);
stencil.Allocate(ARRAY_SIZE);
vtkm::cont::Algorithm::Copy(input, output);
vtkm::cont::Algorithm::CopyIf(input, stencil, output);
vtkm::cont::Algorithm::CopyIf(input, stencil, output, vtkm::LogicalNot());
vtkm::cont::Algorithm::CopySubRange(input, 2, 1, output);
}
void BoundsTest()
{
vtkm::cont::ArrayHandle<vtkm::Id> input;
vtkm::cont::ArrayHandle<vtkm::Id> output;
vtkm::cont::ArrayHandle<vtkm::Id> values;
input.Allocate(ARRAY_SIZE);
output.Allocate(ARRAY_SIZE);
values.Allocate(ARRAY_SIZE);
vtkm::cont::Algorithm::LowerBounds(input, values, output);
vtkm::cont::Algorithm::LowerBounds(input, values, output, vtkm::Sum());
vtkm::cont::Algorithm::LowerBounds(input, values);
vtkm::cont::Algorithm::UpperBounds(input, values, output);
vtkm::cont::Algorithm::UpperBounds(input, values, output, vtkm::Sum());
vtkm::cont::Algorithm::UpperBounds(input, values);
}
void ReduceTest()
{
vtkm::cont::ArrayHandle<vtkm::Id> input;
vtkm::cont::ArrayHandle<vtkm::Id> keys;
vtkm::cont::ArrayHandle<vtkm::Id> keysOut;
vtkm::cont::ArrayHandle<vtkm::Id> valsOut;
input.Allocate(ARRAY_SIZE);
keys.Allocate(ARRAY_SIZE);
keysOut.Allocate(ARRAY_SIZE);
valsOut.Allocate(ARRAY_SIZE);
vtkm::Id result;
result = vtkm::cont::Algorithm::Reduce(input, vtkm::Id(0));
result = vtkm::cont::Algorithm::Reduce(input, vtkm::Id(0), vtkm::Maximum());
vtkm::cont::Algorithm::ReduceByKey(keys, input, keysOut, valsOut, vtkm::Maximum());
(void)result;
}
void ScanTest()
{
vtkm::cont::ArrayHandle<vtkm::Id> input;
vtkm::cont::ArrayHandle<vtkm::Id> output;
vtkm::cont::ArrayHandle<vtkm::Id> keys;
input.Allocate(ARRAY_SIZE);
output.Allocate(ARRAY_SIZE);
keys.Allocate(ARRAY_SIZE);
vtkm::Id out;
out = vtkm::cont::Algorithm::ScanInclusive(input, output);
out = vtkm::cont::Algorithm::ScanInclusive(input, output, vtkm::Maximum());
out = vtkm::cont::Algorithm::StreamingScanExclusive(1, input, output);
vtkm::cont::Algorithm::ScanInclusiveByKey(keys, input, output, vtkm::Maximum());
vtkm::cont::Algorithm::ScanInclusiveByKey(keys, input, output);
out = vtkm::cont::Algorithm::ScanExclusive(input, output, vtkm::Maximum(), vtkm::Id(0));
vtkm::cont::Algorithm::ScanExclusiveByKey(keys, input, output, vtkm::Id(0), vtkm::Maximum());
vtkm::cont::Algorithm::ScanExclusiveByKey(keys, input, output);
(void)out;
}
struct DummyFunctor : public vtkm::exec::FunctorBase
{
template <typename IdType>
VTKM_EXEC void operator()(IdType) const
{
}
};
void ScheduleTest()
{
vtkm::cont::Algorithm::Schedule(DummyFunctor(), vtkm::Id(1));
vtkm::Id3 id3(1, 1, 1);
vtkm::cont::Algorithm::Schedule(DummyFunctor(), id3);
}
struct CompFunctor
{
template <typename T>
VTKM_EXEC_CONT bool operator()(const T& x, const T& y) const
{
return x < y;
}
};
struct CompExecObject : vtkm::cont::ExecutionObjectBase
{
template <typename Device>
VTKM_CONT CompFunctor PrepareForExecution(Device)
{
return CompFunctor();
}
};
void SortTest()
{
vtkm::cont::ArrayHandle<vtkm::Id> input;
vtkm::cont::ArrayHandle<vtkm::Id> keys;
input.Allocate(ARRAY_SIZE);
keys.Allocate(ARRAY_SIZE);
vtkm::cont::Algorithm::Sort(input);
vtkm::cont::Algorithm::Sort(input, CompFunctor());
vtkm::cont::Algorithm::Sort(input, CompExecObject());
vtkm::cont::Algorithm::SortByKey(keys, input);
vtkm::cont::Algorithm::SortByKey(keys, input, CompFunctor());
vtkm::cont::Algorithm::SortByKey(keys, input, CompExecObject());
}
void SynchronizeTest()
{
vtkm::cont::Algorithm::Synchronize();
}
void UniqueTest()
{
vtkm::cont::ArrayHandle<vtkm::Id> input;
input.Allocate(ARRAY_SIZE);
vtkm::cont::Algorithm::Unique(input);
vtkm::cont::Algorithm::Unique(input, CompFunctor());
vtkm::cont::Algorithm::Unique(input, CompExecObject());
}
void TestAll()
{
CopyTest();
BoundsTest();
ReduceTest();
ScanTest();
ScheduleTest();
SortTest();
SynchronizeTest();
UniqueTest();
}
} // anonymous namespace
int UnitTestAlgorithm(int, char* [])
{
return vtkm::cont::testing::Testing::Run(TestAll);
}