2019-04-15 23:24:21 +00:00
|
|
|
//============================================================================
|
2017-06-19 13:49:30 +00:00
|
|
|
// 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.
|
2019-04-15 23:24:21 +00:00
|
|
|
//============================================================================
|
2017-06-19 13:49:30 +00:00
|
|
|
#include <vtkm/worklet/ScatterPermutation.h>
|
|
|
|
|
|
|
|
#include <vtkm/cont/ArrayHandle.h>
|
|
|
|
#include <vtkm/cont/testing/MakeTestDataSet.h>
|
|
|
|
#include <vtkm/cont/testing/Testing.h>
|
|
|
|
#include <vtkm/worklet/DispatcherMapTopology.h>
|
|
|
|
#include <vtkm/worklet/WorkletMapTopology.h>
|
|
|
|
|
|
|
|
#include <ctime>
|
|
|
|
#include <random>
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2019-07-30 16:53:51 +00:00
|
|
|
class Worklet : public vtkm::worklet::WorkletVisitPointsWithCells
|
2017-06-19 13:49:30 +00:00
|
|
|
{
|
|
|
|
public:
|
2018-05-25 21:18:41 +00:00
|
|
|
using ControlSignature = void(CellSetIn cellset,
|
2019-01-10 18:59:25 +00:00
|
|
|
FieldOutPoint outPointId,
|
|
|
|
FieldOutPoint outVisit);
|
2018-05-25 21:18:41 +00:00
|
|
|
using ExecutionSignature = void(InputIndex, VisitIndex, _2, _3);
|
2017-06-19 13:49:30 +00:00
|
|
|
using InputDomain = _1;
|
|
|
|
|
|
|
|
using ScatterType = vtkm::worklet::ScatterPermutation<>;
|
|
|
|
|
Move Scatter from Worklet to Dispatcher
Previously, when a Worklet needed a scatter, the scatter object was
stored in the Worklet object. That was problematic because that means
the Scatter, which is a control object, was shoved into the execution
environment.
To prevent that, move the Scatter into the Dispatcher object. The
worklet still declares a ScatterType alias, but no longer has a
GetScatter method. Instead, the Dispatcher now takes a Scatter object in
its constructor. If using the default scatter (ScatterIdentity), the
default constructor is used. If using another type of Scatter that
requires data to set up its state, then the caller of the worklet needs
to provide that to the dispatcher. For convenience, worklets are
encouraged to have a MakeScatter method to help construct a proper
scatter object.
2018-04-27 04:43:51 +00:00
|
|
|
VTKM_CONT
|
|
|
|
static ScatterType MakeScatter(const vtkm::cont::ArrayHandle<vtkm::Id>& permutation)
|
2017-06-19 13:49:30 +00:00
|
|
|
{
|
Move Scatter from Worklet to Dispatcher
Previously, when a Worklet needed a scatter, the scatter object was
stored in the Worklet object. That was problematic because that means
the Scatter, which is a control object, was shoved into the execution
environment.
To prevent that, move the Scatter into the Dispatcher object. The
worklet still declares a ScatterType alias, but no longer has a
GetScatter method. Instead, the Dispatcher now takes a Scatter object in
its constructor. If using the default scatter (ScatterIdentity), the
default constructor is used. If using another type of Scatter that
requires data to set up its state, then the caller of the worklet needs
to provide that to the dispatcher. For convenience, worklets are
encouraged to have a MakeScatter method to help construct a proper
scatter object.
2018-04-27 04:43:51 +00:00
|
|
|
return ScatterType(permutation);
|
2017-06-19 13:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VTKM_EXEC void operator()(vtkm::Id pointId,
|
|
|
|
vtkm::IdComponent visit,
|
|
|
|
vtkm::Id& outPointId,
|
|
|
|
vtkm::IdComponent& outVisit) const
|
|
|
|
{
|
|
|
|
outPointId = pointId;
|
|
|
|
outVisit = visit;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename CellSetType>
|
|
|
|
void RunTest(const CellSetType& cellset, const vtkm::cont::ArrayHandle<vtkm::Id>& permutation)
|
|
|
|
{
|
|
|
|
vtkm::cont::ArrayHandle<vtkm::Id> outPointId;
|
|
|
|
vtkm::cont::ArrayHandle<vtkm::IdComponent> outVisit;
|
|
|
|
|
Move Scatter from Worklet to Dispatcher
Previously, when a Worklet needed a scatter, the scatter object was
stored in the Worklet object. That was problematic because that means
the Scatter, which is a control object, was shoved into the execution
environment.
To prevent that, move the Scatter into the Dispatcher object. The
worklet still declares a ScatterType alias, but no longer has a
GetScatter method. Instead, the Dispatcher now takes a Scatter object in
its constructor. If using the default scatter (ScatterIdentity), the
default constructor is used. If using another type of Scatter that
requires data to set up its state, then the caller of the worklet needs
to provide that to the dispatcher. For convenience, worklets are
encouraged to have a MakeScatter method to help construct a proper
scatter object.
2018-04-27 04:43:51 +00:00
|
|
|
vtkm::worklet::DispatcherMapTopology<Worklet> dispatcher(Worklet::MakeScatter(permutation));
|
|
|
|
dispatcher.Invoke(cellset, outPointId, outVisit);
|
2017-06-19 13:49:30 +00:00
|
|
|
|
|
|
|
for (vtkm::Id i = 0; i < permutation.GetNumberOfValues(); ++i)
|
|
|
|
{
|
|
|
|
VTKM_TEST_ASSERT(outPointId.GetPortalConstControl().Get(i) ==
|
|
|
|
permutation.GetPortalConstControl().Get(i),
|
|
|
|
"output point ids do not match the permutation");
|
2017-08-08 13:38:04 +00:00
|
|
|
VTKM_TEST_ASSERT(outVisit.GetPortalConstControl().Get(i) == 0, "incorrect visit index");
|
2017-06-19 13:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestScatterPermutation()
|
|
|
|
{
|
|
|
|
vtkm::cont::DataSet dataset = vtkm::cont::testing::MakeTestDataSet().Make2DUniformDataSet0();
|
|
|
|
auto cellset = dataset.GetCellSet();
|
|
|
|
vtkm::Id numberOfPoints = cellset.GetNumberOfPoints();
|
|
|
|
|
|
|
|
vtkm::UInt32 seed = static_cast<vtkm::UInt32>(std::time(nullptr));
|
|
|
|
|
|
|
|
std::default_random_engine generator;
|
|
|
|
generator.seed(seed);
|
|
|
|
std::uniform_int_distribution<vtkm::Id> countDistribution(1, 2 * numberOfPoints);
|
|
|
|
std::uniform_int_distribution<vtkm::Id> ptidDistribution(0, numberOfPoints - 1);
|
|
|
|
|
|
|
|
const int iterations = 5;
|
|
|
|
std::cout << "Testing with random permutations " << iterations << " times\n";
|
|
|
|
std::cout << "Seed: " << seed << std::endl;
|
|
|
|
for (int i = 1; i <= iterations; ++i)
|
|
|
|
{
|
|
|
|
std::cout << "iteration: " << i << "\n";
|
|
|
|
|
|
|
|
vtkm::Id count = countDistribution(generator);
|
|
|
|
vtkm::cont::ArrayHandle<vtkm::Id> permutation;
|
|
|
|
permutation.Allocate(count);
|
|
|
|
|
|
|
|
auto portal = permutation.GetPortalControl();
|
|
|
|
std::cout << "using permutation:";
|
|
|
|
for (vtkm::Id j = 0; j < count; ++j)
|
|
|
|
{
|
|
|
|
auto val = ptidDistribution(generator);
|
|
|
|
std::cout << " " << val;
|
|
|
|
portal.Set(j, val);
|
|
|
|
}
|
|
|
|
std::cout << "\n";
|
|
|
|
|
|
|
|
RunTest(cellset, permutation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2018-10-17 19:17:29 +00:00
|
|
|
int UnitTestScatterPermutation(int argc, char* argv[])
|
2017-06-19 13:49:30 +00:00
|
|
|
{
|
2018-10-17 19:17:29 +00:00
|
|
|
return vtkm::cont::testing::Testing::Run(TestScatterPermutation, argc, argv);
|
2017-06-19 13:49:30 +00:00
|
|
|
}
|