vtk-m/vtkm/worklet/testing/UnitTestScatterAndMaskWithTopology.cxx
Kenneth Moreland d1a4aecc59 Improvements to moving data into ArrayHandle
We have made several improvements to adding data into an `ArrayHandle`.

## Moving data from an `std::vector`

For numerous reasons, it is convenient to define data in a `std::vector`
and then wrap that into an `ArrayHandle`. It is often the case that an
`std::vector` is filled and then becomes unused once it is converted to an
`ArrayHandle`. In this case, what we really want is to pass the data off to
the `ArrayHandle` so that the `ArrayHandle` is now managing the data and
not the `std::vector`.

C++11 has a mechanism to do this: move semantics. You can now pass
variables to functions as an "rvalue" (right-hand value). When something is
passed as an rvalue, it can pull state out of that variable and move it
somewhere else. `std::vector` implements this movement so that an rvalue
can be moved to another `std::vector` without actually copying the data.
`make_ArrayHandle` now also takes advantage of this feature to move rvalue
`std::vector`s.

There is a special form of `make_ArrayHandle` named `make_ArrayHandleMove`
that takes an rvalue. There is also a special overload of
`make_ArrayHandle` itself that handles an rvalue `vector`. (However, using
the explicit move version is better if you want to make sure the data is
actually moved.)

## Make `ArrayHandle` from initalizer list

A common use case for using `std::vector` (particularly in our unit tests)
is to quickly add an initalizer list into an `ArrayHandle`. Now you can
by simply passing an initializer list to `make_ArrayHandle`.

## Deprecated `make_ArrayHandle` with default shallow copy

For historical reasons, passing an `std::vector` or a pointer to
`make_ArrayHandle` does a shallow copy (i.e. `CopyFlag` defaults to `Off`).
Although more efficient, this mode is inherintly unsafe, and making it the
default is asking for trouble.

To combat this, calling `make_ArrayHandle` without a copy flag is
deprecated. In this way, if you wish to do the faster but more unsafe
creation of an `ArrayHandle` you should explicitly express that.

This requried quite a few changes through the VTK-m source (particularly in
the tests).

## Similar changes to `Field`

`vtkm::cont::Field` has a `make_Field` helper function that is similar to
`make_ArrayHandle`. It also features the ability to create fields from
`std::vector`s and C arrays. It also likewise had the same unsafe behavior
by default of not copying from the source of the arrays.

That behavior has similarly been depreciated. You now have to specify a
copy flag.

The ability to construct a `Field` from an initializer list of values has
also been added.
2020-07-23 10:53:38 -06:00

204 lines
6.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.
//============================================================================
#include <vtkm/cont/ArrayCopy.h>
#include <vtkm/cont/testing/MakeTestDataSet.h>
#include <vtkm/worklet/DispatcherMapTopology.h>
#include <vtkm/worklet/ScatterCounting.h>
#include <vtkm/worklet/WorkletMapTopology.h>
#include <vtkm/cont/testing/Testing.h>
#include <vtkm/worklet/MaskSelect.h>
#include <vtkm/worklet/ScatterUniform.h>
namespace
{
class TestWorkletMapTopo : public vtkm::worklet::WorkletVisitPointsWithCells
{
public:
using ControlSignature = void(CellSetIn topology, FieldInVisit pointCoords);
using ExecutionSignature = void(_2, WorkIndex, InputIndex, OutputIndex, VisitIndex);
};
class TestWorkletMapTopoIdentity : public TestWorkletMapTopo
{
public:
using ScatterType = vtkm::worklet::ScatterIdentity;
VTKM_EXEC void operator()(const vtkm::Vec<int, 3>& vtkmNotUsed(coords),
const vtkm::Id& workIndex,
const vtkm::Id& inputIndex,
const vtkm::Id& outputIndex,
const vtkm::Id& visitIndex) const
{
if (workIndex != inputIndex)
{
this->RaiseError("Got wrong input value.");
}
if (outputIndex != workIndex)
{
this->RaiseError("Got work and output index don't match.");
}
if (visitIndex != 0)
{
this->RaiseError("Got wrong visit value.");
}
}
};
class TestWorkletMapTopoUniform : public TestWorkletMapTopo
{
public:
using ScatterType = vtkm::worklet::ScatterUniform<2>;
VTKM_EXEC void operator()(const vtkm::Vec<int, 3>& vtkmNotUsed(coords),
const vtkm::Id& workIndex,
const vtkm::Id& inputIndex,
const vtkm::Id& outputIndex,
const vtkm::Id& visitIndex) const
{
if ((workIndex / 2) != inputIndex)
{
this->RaiseError("Got wrong input value.");
}
if (outputIndex != workIndex)
{
this->RaiseError("Got work and output index don't match.");
}
if ((workIndex % 2) != visitIndex)
{
this->RaiseError("Got wrong visit value.");
}
}
};
class TestWorkletMapTopoNone : public TestWorkletMapTopo
{
public:
using MaskType = vtkm::worklet::MaskNone;
VTKM_EXEC void operator()(const vtkm::Vec<int, 3>& vtkmNotUsed(coords),
const vtkm::Id& workIndex,
const vtkm::Id& inputIndex,
const vtkm::Id& outputIndex,
const vtkm::Id& visitIndex) const
{
if (workIndex != inputIndex)
{
this->RaiseError("Got wrong input value.");
}
if (outputIndex != workIndex)
{
this->RaiseError("Got work and output index don't match.");
}
if (visitIndex != 0)
{
this->RaiseError("Got wrong visit value.");
}
}
};
class TestWorkletMapTopoSelect : public TestWorkletMapTopo
{
public:
using MaskType = vtkm::worklet::MaskSelect;
VTKM_EXEC void operator()(const vtkm::Vec<int, 3>& vtkmNotUsed(coords),
const vtkm::Id& vtkmNotUsed(workIndex),
const vtkm::Id& vtkmNotUsed(inputIndex),
const vtkm::Id& vtkmNotUsed(outputIndex),
const vtkm::Id& vtkmNotUsed(visitIndex)) const
{
// This method should never be called
this->RaiseError("An element was selected, this test selects none.");
}
};
template <typename WorkletType>
struct DoTestWorklet
{
template <typename T>
VTKM_CONT void operator()(T) const
{
vtkm::cont::testing::MakeTestDataSet testDataSet;
vtkm::cont::DataSet dataSet3D = testDataSet.Make3DUniformDataSet0();
vtkm::cont::CellSetStructured<3> cellSet =
dataSet3D.GetCellSet().Cast<vtkm::cont::CellSetStructured<3>>();
vtkm::cont::Invoker invoker;
invoker(WorkletType{}, cellSet, dataSet3D.GetCoordinateSystem());
}
};
template <>
struct DoTestWorklet<TestWorkletMapTopoSelect>
{
template <typename T>
VTKM_CONT void operator()(T) const
{
vtkm::cont::testing::MakeTestDataSet testDataSet;
vtkm::cont::DataSet dataSet3D = testDataSet.Make3DUniformDataSet0();
// Start select array with an array of zeros
auto selectArrayHandle = vtkm::cont::make_ArrayHandleMove(
std::vector<vtkm::IdComponent>(static_cast<std::size_t>(dataSet3D.GetNumberOfPoints()), 0));
vtkm::cont::CellSetStructured<3> cellSet =
dataSet3D.GetCellSet().Cast<vtkm::cont::CellSetStructured<3>>();
vtkm::cont::Invoker invoker;
invoker(TestWorkletMapTopoSelect{},
vtkm::worklet::MaskSelect(selectArrayHandle),
cellSet,
dataSet3D.GetCoordinateSystem());
}
};
void TestWorkletMapField3d(vtkm::cont::DeviceAdapterId id)
{
using HandleTypesToTest3D =
vtkm::List<vtkm::Id, vtkm::Vec2i_32, vtkm::FloatDefault, vtkm::Vec3f_64>;
using HandleTypesToTest1D =
vtkm::List<vtkm::Int32, vtkm::Int64, vtkm::UInt32, vtkm::UInt64, vtkm::Int8, vtkm::UInt8, char>;
std::cout << "Testing WorkletMapTopology with ScatterIdentity on device adapter: " << id.GetName()
<< std::endl;
vtkm::testing::Testing::TryTypes(DoTestWorklet<TestWorkletMapTopoIdentity>(),
HandleTypesToTest3D());
std::cout << "Testing WorkletMapTopology with ScatterUniform on device adapter: " << id.GetName()
<< std::endl;
vtkm::testing::Testing::TryTypes(DoTestWorklet<TestWorkletMapTopoUniform>(),
HandleTypesToTest3D());
std::cout << "Testing WorkletMapTopology with MaskNone on device adapter: " << id.GetName()
<< std::endl;
vtkm::testing::Testing::TryTypes(DoTestWorklet<TestWorkletMapTopoNone>(), HandleTypesToTest3D());
std::cout << "Testing WorkletMapTopology with MaskSelect on device adapter: " << id.GetName()
<< std::endl;
vtkm::testing::Testing::TryTypes(DoTestWorklet<TestWorkletMapTopoSelect>(),
HandleTypesToTest1D());
}
} // namespace
int UnitTestScatterAndMaskWithTopology(int argc, char* argv[])
{
return vtkm::cont::testing::Testing::RunOnDevice(TestWorkletMapField3d, argc, argv);
}