Consolidate UnitTestCellLocator*

Previously, each device adapter implementation had their own version of
these tests by including a common header. Simplify this by making a
single test in UnitTests_vtkm_cont_testing for each, which can now be
compiled for and tested on a device.
This commit is contained in:
Kenneth Moreland 2022-07-11 13:37:45 -06:00
parent 891cfcfa86
commit 12c1a80212
26 changed files with 375 additions and 694 deletions

@ -9,9 +9,6 @@
##============================================================================
set(unit_tests
UnitTestCudaCellLocatorRectilinearGrid.cu
UnitTestCudaCellLocatorTwoLevel.cu
UnitTestCudaCellLocatorUniformGrid.cu
UnitTestCudaComputeRange.cu
UnitTestCudaColorTable.cu
UnitTestCudaDataSetExplicit.cu

@ -1,19 +0,0 @@
//============================================================================
// 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/testing/TestingCellLocatorRectilinearGrid.h>
int UnitTestCudaCellLocatorRectilinearGrid(int argc, char* argv[])
{
auto& tracker = vtkm::cont::GetRuntimeDeviceTracker();
tracker.ForceDevice(vtkm::cont::DeviceAdapterTagCuda{});
return vtkm::cont::testing::Testing::Run(
TestingCellLocatorRectilinearGrid<vtkm::cont::DeviceAdapterTagCuda>(), argc, argv);
}

@ -1,19 +0,0 @@
//============================================================================
// 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/testing/TestingCellLocatorTwoLevel.h>
int UnitTestCudaCellLocatorTwoLevel(int argc, char* argv[])
{
auto& tracker = vtkm::cont::GetRuntimeDeviceTracker();
tracker.ForceDevice(vtkm::cont::DeviceAdapterTagCuda{});
return vtkm::cont::testing::Testing::Run(
TestingCellLocatorTwoLevel<vtkm::cont::DeviceAdapterTagCuda>, argc, argv);
}

@ -1,19 +0,0 @@
//============================================================================
// 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/testing/TestingCellLocatorUniformGrid.h>
int UnitTestCudaCellLocatorUniformGrid(int argc, char* argv[])
{
auto& tracker = vtkm::cont::GetRuntimeDeviceTracker();
tracker.ForceDevice(vtkm::cont::DeviceAdapterTagCuda{});
return vtkm::cont::testing::Testing::Run(
TestingCellLocatorUniformGrid<vtkm::cont::DeviceAdapterTagCuda>(), argc, argv);
}

@ -9,9 +9,6 @@
##============================================================================
set(unit_tests
UnitTestKokkosCellLocatorRectilinearGrid.cxx
UnitTestKokkosCellLocatorTwoLevel.cxx
UnitTestKokkosCellLocatorUniformGrid.cxx
UnitTestKokkosComputeRange.cxx
UnitTestKokkosColorTable.cxx
UnitTestKokkosDataSetExplicit.cxx

@ -1,19 +0,0 @@
//============================================================================
// 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/testing/TestingCellLocatorRectilinearGrid.h>
int UnitTestKokkosCellLocatorRectilinearGrid(int argc, char* argv[])
{
auto& tracker = vtkm::cont::GetRuntimeDeviceTracker();
tracker.ForceDevice(vtkm::cont::DeviceAdapterTagKokkos{});
return vtkm::cont::testing::Testing::Run(
TestingCellLocatorRectilinearGrid<vtkm::cont::DeviceAdapterTagKokkos>(), argc, argv);
}

@ -1,19 +0,0 @@
//============================================================================
// 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/testing/TestingCellLocatorTwoLevel.h>
int UnitTestKokkosCellLocatorTwoLevel(int argc, char* argv[])
{
auto& tracker = vtkm::cont::GetRuntimeDeviceTracker();
tracker.ForceDevice(vtkm::cont::DeviceAdapterTagKokkos{});
return vtkm::cont::testing::Testing::Run(
TestingCellLocatorTwoLevel<vtkm::cont::DeviceAdapterTagKokkos>, argc, argv);
}

@ -1,19 +0,0 @@
//============================================================================
// 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/testing/TestingCellLocatorUniformGrid.h>
int UnitTestKokkosCellLocatorUniformGrid(int argc, char* argv[])
{
auto& tracker = vtkm::cont::GetRuntimeDeviceTracker();
tracker.ForceDevice(vtkm::cont::DeviceAdapterTagKokkos{});
return vtkm::cont::testing::Testing::Run(
TestingCellLocatorUniformGrid<vtkm::cont::DeviceAdapterTagKokkos>(), argc, argv);
}

@ -9,9 +9,6 @@
##============================================================================
set(unit_tests
UnitTestOpenMPCellLocatorRectilinearGrid.cxx
UnitTestOpenMPCellLocatorTwoLevel.cxx
UnitTestOpenMPCellLocatorUniformGrid.cxx
UnitTestOpenMPColorTable.cxx
UnitTestOpenMPComputeRange.cxx
UnitTestOpenMPDataSetExplicit.cxx

@ -1,19 +0,0 @@
//============================================================================
// 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/testing/TestingCellLocatorRectilinearGrid.h>
int UnitTestOpenMPCellLocatorRectilinearGrid(int argc, char* argv[])
{
auto& tracker = vtkm::cont::GetRuntimeDeviceTracker();
tracker.ForceDevice(vtkm::cont::DeviceAdapterTagOpenMP{});
return vtkm::cont::testing::Testing::Run(
TestingCellLocatorRectilinearGrid<vtkm::cont::DeviceAdapterTagOpenMP>(), argc, argv);
}

@ -1,19 +0,0 @@
//============================================================================
// 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/openmp/DeviceAdapterOpenMP.h>
#include <vtkm/cont/testing/TestingCellLocatorTwoLevel.h>
int UnitTestOpenMPCellLocatorTwoLevel(int argc, char* argv[])
{
auto& tracker = vtkm::cont::GetRuntimeDeviceTracker();
tracker.ForceDevice(vtkm::cont::DeviceAdapterTagOpenMP{});
return vtkm::cont::testing::Testing::Run(
TestingCellLocatorTwoLevel<vtkm::cont::DeviceAdapterTagOpenMP>, argc, argv);
}

@ -1,19 +0,0 @@
//============================================================================
// 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/testing/TestingCellLocatorUniformGrid.h>
int UnitTestOpenMPCellLocatorUniformGrid(int argc, char* argv[])
{
auto& tracker = vtkm::cont::GetRuntimeDeviceTracker();
tracker.ForceDevice(vtkm::cont::DeviceAdapterTagOpenMP{});
return vtkm::cont::testing::Testing::Run(
TestingCellLocatorUniformGrid<vtkm::cont::DeviceAdapterTagOpenMP>(), argc, argv);
}

@ -9,9 +9,6 @@
##============================================================================
set(unit_tests
UnitTestSerialCellLocatorRectilinearGrid.cxx
UnitTestSerialCellLocatorTwoLevel.cxx
UnitTestSerialCellLocatorUniformGrid.cxx
UnitTestSerialComputeRange.cxx
UnitTestSerialColorTable.cxx
UnitTestSerialDataSetExplicit.cxx

@ -1,19 +0,0 @@
//============================================================================
// 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/testing/TestingCellLocatorRectilinearGrid.h>
int UnitTestSerialCellLocatorRectilinearGrid(int argc, char* argv[])
{
auto& tracker = vtkm::cont::GetRuntimeDeviceTracker();
tracker.ForceDevice(vtkm::cont::DeviceAdapterTagSerial{});
return vtkm::cont::testing::Testing::Run(
TestingCellLocatorRectilinearGrid<vtkm::cont::DeviceAdapterTagSerial>(), argc, argv);
}

@ -1,20 +0,0 @@
//============================================================================
// 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/serial/DeviceAdapterSerial.h>
#include <vtkm/cont/testing/TestingCellLocatorTwoLevel.h>
int UnitTestSerialCellLocatorTwoLevel(int argc, char* argv[])
{
auto& tracker = vtkm::cont::GetRuntimeDeviceTracker();
tracker.ForceDevice(vtkm::cont::DeviceAdapterTagSerial{});
return vtkm::cont::testing::Testing::Run(
TestingCellLocatorTwoLevel<vtkm::cont::DeviceAdapterTagSerial>, argc, argv);
}

@ -1,19 +0,0 @@
//============================================================================
// 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/testing/TestingCellLocatorUniformGrid.h>
int UnitTestSerialCellLocatorUniformGrid(int argc, char* argv[])
{
auto& tracker = vtkm::cont::GetRuntimeDeviceTracker();
tracker.ForceDevice(vtkm::cont::DeviceAdapterTagSerial{});
return vtkm::cont::testing::Testing::Run(
TestingCellLocatorUniformGrid<vtkm::cont::DeviceAdapterTagSerial>(), argc, argv);
}

@ -9,9 +9,6 @@
##============================================================================
set(unit_tests
UnitTestTBBCellLocatorRectilinearGrid.cxx
UnitTestTBBCellLocatorTwoLevel.cxx
UnitTestTBBCellLocatorUniformGrid.cxx
UnitTestTBBColorTable.cxx
UnitTestTBBComputeRange.cxx
UnitTestTBBDataSetExplicit.cxx

@ -1,19 +0,0 @@
//============================================================================
// 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/testing/TestingCellLocatorRectilinearGrid.h>
int UnitTestTBBCellLocatorRectilinearGrid(int argc, char* argv[])
{
auto& tracker = vtkm::cont::GetRuntimeDeviceTracker();
tracker.ForceDevice(vtkm::cont::DeviceAdapterTagTBB{});
return vtkm::cont::testing::Testing::Run(
TestingCellLocatorRectilinearGrid<vtkm::cont::DeviceAdapterTagTBB>(), argc, argv);
}

@ -1,19 +0,0 @@
//============================================================================
// 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/testing/TestingCellLocatorTwoLevel.h>
int UnitTestTBBCellLocatorTwoLevel(int argc, char* argv[])
{
auto& tracker = vtkm::cont::GetRuntimeDeviceTracker();
tracker.ForceDevice(vtkm::cont::DeviceAdapterTagTBB{});
return vtkm::cont::testing::Testing::Run(
TestingCellLocatorTwoLevel<vtkm::cont::DeviceAdapterTagTBB>, argc, argv);
}

@ -1,19 +0,0 @@
//============================================================================
// 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/testing/TestingCellLocatorUniformGrid.h>
int UnitTestTBBCellLocatorUniformGrid(int argc, char* argv[])
{
auto& tracker = vtkm::cont::GetRuntimeDeviceTracker();
tracker.ForceDevice(vtkm::cont::DeviceAdapterTagTBB{});
return vtkm::cont::testing::Testing::Run(
TestingCellLocatorUniformGrid<vtkm::cont::DeviceAdapterTagTBB>(), argc, argv);
}

@ -12,9 +12,6 @@ set(headers
ExplicitTestData.h
MakeTestDataSet.h
Testing.h
TestingCellLocatorRectilinearGrid.h
TestingCellLocatorTwoLevel.h
TestingCellLocatorUniformGrid.h
TestingColorTable.h
TestingComputeRange.h
TestingDeviceAdapter.h
@ -101,6 +98,9 @@ set(unit_tests_device
UnitTestBitField.cxx
UnitTestCellLocatorChooser.cxx
UnitTestCellLocatorGeneral.cxx
UnitTestCellLocatorRectilinearGrid.cxx
UnitTestCellLocatorTwoLevel.cxx
UnitTestCellLocatorUniformGrid.cxx
UnitTestCellSet.cxx
UnitTestCellSetExplicit.cxx
UnitTestCellSetExtrude.cxx
@ -124,7 +124,7 @@ vtkm_library(
DEVICE_SOURCES ${library_sources_device}
HEADERS ${headers}
)
target_link_libraries(vtkm_cont_testing PUBLIC vtkm_cont)
target_link_libraries(vtkm_cont_testing PUBLIC vtkm_cont vtkm_worklet)
if(VTKm_ENABLE_TESTING)
vtkm_unit_tests(SOURCES ${unit_tests} DEVICE_SOURCES ${unit_tests_device})

@ -1,207 +0,0 @@
//============================================================================
// 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.
//============================================================================
#ifndef vtk_m_cont_testing_TestingCellLocatorRectilinearGrid_h
#define vtk_m_cont_testing_TestingCellLocatorRectilinearGrid_h
#include <random>
#include <string>
#include <vtkm/cont/CellLocatorRectilinearGrid.h>
#include <vtkm/cont/DataSet.h>
#include <vtkm/cont/DataSetBuilderRectilinear.h>
#include <vtkm/cont/testing/Testing.h>
#include <vtkm/exec/CellLocatorRectilinearGrid.h>
#include <vtkm/worklet/DispatcherMapField.h>
#include <vtkm/worklet/WorkletMapField.h>
template <typename DeviceAdapter>
class LocatorWorklet : public vtkm::worklet::WorkletMapField
{
public:
using AxisHandle = vtkm::cont::ArrayHandle<vtkm::FloatDefault>;
using AxisPortalType = typename AxisHandle::ReadPortalType;
using RectilinearType =
vtkm::cont::ArrayHandleCartesianProduct<AxisHandle, AxisHandle, AxisHandle>;
using RectilinearPortalType = typename RectilinearType::ReadPortalType;
LocatorWorklet(vtkm::Bounds& bounds,
vtkm::Id3& dims,
const RectilinearType& coords,
vtkm::cont::Token& token)
: Bounds(bounds)
, Dims(dims)
{
RectilinearPortalType coordsPortal = coords.PrepareForInput(DeviceAdapter(), token);
xAxis = coordsPortal.GetFirstPortal();
yAxis = coordsPortal.GetSecondPortal();
zAxis = coordsPortal.GetThirdPortal();
}
using ControlSignature =
void(FieldIn pointIn, ExecObject locator, FieldOut cellId, FieldOut parametric, FieldOut match);
using ExecutionSignature = void(_1, _2, _3, _4, _5);
template <typename PointType>
VTKM_EXEC vtkm::Id CalculateCellId(const PointType& point) const
{
if (!Bounds.Contains(point))
return -1;
vtkm::Id3 logical(-1, -1, -1);
// Linear search in the coordinates.
vtkm::Id index;
/*Get floor X location*/
if (point[0] == xAxis.Get(this->Dims[0] - 1))
logical[0] = this->Dims[0] - 1;
else
for (index = 0; index < this->Dims[0] - 1; index++)
if (xAxis.Get(index) <= point[0] && point[0] < xAxis.Get(index + 1))
{
logical[0] = index;
break;
}
/*Get floor Y location*/
if (point[1] == yAxis.Get(this->Dims[1] - 1))
logical[1] = this->Dims[1] - 1;
else
for (index = 0; index < this->Dims[1] - 1; index++)
if (yAxis.Get(index) <= point[1] && point[1] < yAxis.Get(index + 1))
{
logical[1] = index;
break;
}
/*Get floor Z location*/
if (point[2] == zAxis.Get(this->Dims[2] - 1))
logical[2] = this->Dims[2] - 1;
else
for (index = 0; index < this->Dims[2] - 1; index++)
if (zAxis.Get(index) <= point[2] && point[2] < zAxis.Get(index + 1))
{
logical[2] = index;
break;
}
if (logical[0] == -1 || logical[1] == -1 || logical[2] == -1)
return -1;
return logical[2] * (Dims[0] - 1) * (Dims[1] - 1) + logical[1] * (Dims[0] - 1) + logical[0];
}
template <typename PointType, typename LocatorType>
VTKM_EXEC void operator()(const PointType& pointIn,
const LocatorType& locator,
vtkm::Id& cellId,
PointType& parametric,
bool& match) const
{
vtkm::Id calculated = CalculateCellId(pointIn);
vtkm::ErrorCode status = locator.FindCell(pointIn, cellId, parametric);
if (status != vtkm::ErrorCode::Success)
{
this->RaiseError(vtkm::ErrorString(status));
match = false;
return;
}
match = (calculated == cellId);
}
private:
vtkm::Bounds Bounds;
vtkm::Id3 Dims;
AxisPortalType xAxis;
AxisPortalType yAxis;
AxisPortalType zAxis;
};
template <typename DeviceAdapter>
class TestingCellLocatorRectilinearGrid
{
public:
using Algorithm = vtkm::cont::DeviceAdapterAlgorithm<DeviceAdapter>;
void TestTest() const
{
vtkm::cont::DataSetBuilderRectilinear dsb;
std::vector<vtkm::Float32> X(4), Y(3), Z(5);
X[0] = 0.0f;
X[1] = 1.0f;
X[2] = 3.0f;
X[3] = 4.0f;
Y[0] = 0.0f;
Y[1] = 1.0f;
Y[2] = 2.0f;
Z[0] = 0.0f;
Z[1] = 1.0f;
Z[2] = 3.0f;
Z[3] = 5.0f;
Z[4] = 6.0f;
vtkm::cont::DataSet dataset = dsb.Create(X, Y, Z);
using StructuredType = vtkm::cont::CellSetStructured<3>;
using AxisHandle = vtkm::cont::ArrayHandle<vtkm::FloatDefault>;
using RectilinearType =
vtkm::cont::ArrayHandleCartesianProduct<AxisHandle, AxisHandle, AxisHandle>;
vtkm::cont::CoordinateSystem coords = dataset.GetCoordinateSystem();
vtkm::cont::UnknownCellSet cellSet = dataset.GetCellSet();
vtkm::Bounds bounds = coords.GetBounds();
vtkm::Id3 dims =
cellSet.AsCellSet<StructuredType>().GetSchedulingRange(vtkm::TopologyElementTagPoint());
// Generate some sample points.
using PointType = vtkm::Vec3f;
std::vector<PointType> pointsVec;
std::default_random_engine dre;
std::uniform_real_distribution<vtkm::Float32> xCoords(0.0f, 4.0f);
std::uniform_real_distribution<vtkm::Float32> yCoords(0.0f, 2.0f);
std::uniform_real_distribution<vtkm::Float32> zCoords(0.0f, 6.0f);
for (size_t i = 0; i < 10; i++)
{
PointType point = vtkm::make_Vec(xCoords(dre), yCoords(dre), zCoords(dre));
pointsVec.push_back(point);
}
vtkm::cont::ArrayHandle<PointType> points =
vtkm::cont::make_ArrayHandle(pointsVec, vtkm::CopyFlag::Off);
// Initialize locator
vtkm::cont::CellLocatorRectilinearGrid locator;
locator.SetCoordinates(coords);
locator.SetCellSet(cellSet);
locator.Update();
// Query the points using the locator.
vtkm::cont::ArrayHandle<vtkm::Id> cellIds;
vtkm::cont::ArrayHandle<PointType> parametric;
vtkm::cont::ArrayHandle<bool> match;
vtkm::cont::Token token;
LocatorWorklet<DeviceAdapter> worklet(
bounds, dims, coords.GetData().template AsArrayHandle<RectilinearType>(), token);
vtkm::worklet::DispatcherMapField<LocatorWorklet<DeviceAdapter>> dispatcher(worklet);
dispatcher.SetDevice(DeviceAdapter());
dispatcher.Invoke(points, locator, cellIds, parametric, match);
auto matchPortal = match.ReadPortal();
for (vtkm::Id index = 0; index < match.GetNumberOfValues(); index++)
{
VTKM_TEST_ASSERT(matchPortal.Get(index), "Points do not match");
}
}
void operator()() const
{
vtkm::cont::GetRuntimeDeviceTracker().ForceDevice(DeviceAdapter());
this->TestTest();
}
};
#endif

@ -1,174 +0,0 @@
//============================================================================
// 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.
//============================================================================
#ifndef vtk_m_cont_testing_TestingCellLocatorUniformGrid_h
#define vtk_m_cont_testing_TestingCellLocatorUniformGrid_h
#include <random>
#include <string>
#include <vtkm/cont/CellLocatorUniformGrid.h>
#include <vtkm/cont/DataSet.h>
#include <vtkm/cont/testing/MakeTestDataSet.h>
#include <vtkm/cont/testing/Testing.h>
#include <vtkm/exec/CellLocatorUniformGrid.h>
#include <vtkm/worklet/DispatcherMapField.h>
#include <vtkm/worklet/WorkletMapField.h>
class LocatorWorklet : public vtkm::worklet::WorkletMapField
{
public:
LocatorWorklet(vtkm::Bounds& bounds, vtkm::Id3& cellDims)
: Bounds(bounds)
, CellDims(cellDims)
{
}
using ControlSignature =
void(FieldIn pointIn, ExecObject locator, FieldOut cellId, FieldOut parametric, FieldOut match);
using ExecutionSignature = void(_1, _2, _3, _4, _5);
template <typename PointType>
VTKM_EXEC vtkm::Id CalculateCellId(const PointType& point) const
{
if (!Bounds.Contains(point))
return -1;
vtkm::Id3 logical;
logical[0] = (point[0] == Bounds.X.Max)
? CellDims[0] - 1
: static_cast<vtkm::Id>(vtkm::Floor((point[0] / Bounds.X.Length()) *
static_cast<vtkm::FloatDefault>(CellDims[0])));
logical[1] = (point[1] == Bounds.Y.Max)
? CellDims[1] - 1
: static_cast<vtkm::Id>(vtkm::Floor((point[1] / Bounds.Y.Length()) *
static_cast<vtkm::FloatDefault>(CellDims[1])));
logical[2] = (point[2] == Bounds.Z.Max)
? CellDims[2] - 1
: static_cast<vtkm::Id>(vtkm::Floor((point[2] / Bounds.Z.Length()) *
static_cast<vtkm::FloatDefault>(CellDims[2])));
return logical[2] * CellDims[0] * CellDims[1] + logical[1] * CellDims[0] + logical[0];
}
template <typename PointType, typename LocatorType>
VTKM_EXEC void operator()(const PointType& pointIn,
const LocatorType& locator,
vtkm::Id& cellId,
PointType& parametric,
bool& match) const
{
vtkm::Id calculated = CalculateCellId(pointIn);
vtkm::ErrorCode status = locator.FindCell(pointIn, cellId, parametric);
if ((status != vtkm::ErrorCode::Success) && (status != vtkm::ErrorCode::CellNotFound))
{
this->RaiseError(vtkm::ErrorString(status));
match = false;
return;
}
match = (calculated == cellId);
}
private:
vtkm::Bounds Bounds;
vtkm::Id3 CellDims;
};
template <typename DeviceAdapter>
class TestingCellLocatorUniformGrid
{
public:
using Algorithm = vtkm::cont::DeviceAdapterAlgorithm<DeviceAdapter>;
void TestTest() const
{
vtkm::cont::DataSet dataset = vtkm::cont::testing::MakeTestDataSet().Make3DUniformDataSet1();
vtkm::cont::CoordinateSystem coords = dataset.GetCoordinateSystem();
vtkm::cont::UnknownCellSet cellSet = dataset.GetCellSet();
vtkm::Bounds bounds = coords.GetBounds();
std::cout << "X bounds : " << bounds.X.Min << " to " << bounds.X.Max << std::endl;
std::cout << "Y bounds : " << bounds.Y.Min << " to " << bounds.Y.Max << std::endl;
std::cout << "Z bounds : " << bounds.Z.Min << " to " << bounds.Z.Max << std::endl;
using StructuredType = vtkm::cont::CellSetStructured<3>;
vtkm::Id3 cellDims =
cellSet.AsCellSet<StructuredType>().GetSchedulingRange(vtkm::TopologyElementTagCell());
std::cout << "Dimensions of dataset : " << cellDims << std::endl;
vtkm::cont::CellLocatorUniformGrid locator;
locator.SetCoordinates(coords);
locator.SetCellSet(cellSet);
locator.Update();
// Generate some sample points.
using PointType = vtkm::Vec3f;
std::vector<PointType> pointsVec;
std::default_random_engine dre;
std::uniform_real_distribution<vtkm::Float32> inBounds(0.0f, 4.0f);
for (size_t i = 0; i < 10; i++)
{
PointType point = vtkm::make_Vec(inBounds(dre), inBounds(dre), inBounds(dre));
pointsVec.push_back(point);
}
std::uniform_real_distribution<vtkm::Float32> outBounds(4.0f, 5.0f);
for (size_t i = 0; i < 5; i++)
{
PointType point = vtkm::make_Vec(outBounds(dre), outBounds(dre), outBounds(dre));
pointsVec.push_back(point);
}
std::uniform_real_distribution<vtkm::Float32> outBounds2(-1.0f, 0.0f);
for (size_t i = 0; i < 5; i++)
{
PointType point = vtkm::make_Vec(outBounds2(dre), outBounds2(dre), outBounds2(dre));
pointsVec.push_back(point);
}
// Add points right on the boundary.
pointsVec.push_back(vtkm::make_Vec(0, 0, 0));
pointsVec.push_back(vtkm::make_Vec(4, 4, 4));
pointsVec.push_back(vtkm::make_Vec(4, 0, 0));
pointsVec.push_back(vtkm::make_Vec(0, 4, 0));
pointsVec.push_back(vtkm::make_Vec(0, 0, 4));
pointsVec.push_back(vtkm::make_Vec(4, 4, 0));
pointsVec.push_back(vtkm::make_Vec(0, 4, 4));
pointsVec.push_back(vtkm::make_Vec(4, 0, 4));
vtkm::cont::ArrayHandle<PointType> points =
vtkm::cont::make_ArrayHandle(pointsVec, vtkm::CopyFlag::Off);
// Query the points using the locators.
vtkm::cont::ArrayHandle<vtkm::Id> cellIds;
vtkm::cont::ArrayHandle<PointType> parametric;
vtkm::cont::ArrayHandle<bool> match;
LocatorWorklet worklet(bounds, cellDims);
vtkm::worklet::DispatcherMapField<LocatorWorklet> dispatcher(worklet);
dispatcher.SetDevice(DeviceAdapter());
dispatcher.Invoke(points, locator, cellIds, parametric, match);
auto matchPortal = match.ReadPortal();
for (vtkm::Id index = 0; index < match.GetNumberOfValues(); index++)
{
VTKM_TEST_ASSERT(matchPortal.Get(index), "Points do not match");
}
std::cout << "Test finished successfully." << std::endl;
}
void operator()() const
{
vtkm::cont::GetRuntimeDeviceTracker().ForceDevice(DeviceAdapter());
this->TestTest();
}
};
#endif

@ -0,0 +1,198 @@
//============================================================================
// 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 <random>
#include <string>
#include <vtkm/cont/CellLocatorRectilinearGrid.h>
#include <vtkm/cont/DataSet.h>
#include <vtkm/cont/DataSetBuilderRectilinear.h>
#include <vtkm/cont/Invoker.h>
#include <vtkm/cont/testing/Testing.h>
#include <vtkm/exec/CellLocatorRectilinearGrid.h>
#include <vtkm/worklet/WorkletMapField.h>
namespace
{
using AxisHandle = vtkm::cont::ArrayHandle<vtkm::FloatDefault>;
using RectilinearType = vtkm::cont::ArrayHandleCartesianProduct<AxisHandle, AxisHandle, AxisHandle>;
using RectilinearPortalType = typename RectilinearType::ReadPortalType;
class LocatorWorklet : public vtkm::worklet::WorkletMapField
{
public:
LocatorWorklet(vtkm::Bounds& bounds, vtkm::Id3& dims)
: Bounds(bounds)
, Dims(dims)
{
}
using ControlSignature = void(FieldIn pointIn,
ExecObject locator,
WholeArrayIn rectilinearCoords,
FieldOut cellId,
FieldOut parametric,
FieldOut match);
template <typename PointType>
VTKM_EXEC vtkm::Id CalculateCellId(const PointType& point,
const RectilinearPortalType& coordsPortal) const
{
auto xAxis = coordsPortal.GetFirstPortal();
auto yAxis = coordsPortal.GetSecondPortal();
auto zAxis = coordsPortal.GetThirdPortal();
if (!Bounds.Contains(point))
return -1;
vtkm::Id3 logical(-1, -1, -1);
// Linear search in the coordinates.
vtkm::Id index;
/*Get floor X location*/
if (point[0] == xAxis.Get(this->Dims[0] - 1))
logical[0] = this->Dims[0] - 1;
else
for (index = 0; index < this->Dims[0] - 1; index++)
if (xAxis.Get(index) <= point[0] && point[0] < xAxis.Get(index + 1))
{
logical[0] = index;
break;
}
/*Get floor Y location*/
if (point[1] == yAxis.Get(this->Dims[1] - 1))
logical[1] = this->Dims[1] - 1;
else
for (index = 0; index < this->Dims[1] - 1; index++)
if (yAxis.Get(index) <= point[1] && point[1] < yAxis.Get(index + 1))
{
logical[1] = index;
break;
}
/*Get floor Z location*/
if (point[2] == zAxis.Get(this->Dims[2] - 1))
logical[2] = this->Dims[2] - 1;
else
for (index = 0; index < this->Dims[2] - 1; index++)
if (zAxis.Get(index) <= point[2] && point[2] < zAxis.Get(index + 1))
{
logical[2] = index;
break;
}
if (logical[0] == -1 || logical[1] == -1 || logical[2] == -1)
return -1;
return logical[2] * (Dims[0] - 1) * (Dims[1] - 1) + logical[1] * (Dims[0] - 1) + logical[0];
}
template <typename PointType, typename LocatorType, typename CoordPortalType>
VTKM_EXEC void operator()(const PointType& pointIn,
const LocatorType& locator,
const CoordPortalType& coordsPortal,
vtkm::Id& cellId,
PointType& parametric,
bool& match) const
{
// Note that CoordPortalType is actually a RectilinearPortalType wrapped in an
// ExecutionWholeArrayConst. We need to get out the actual portal.
vtkm::Id calculated = CalculateCellId(pointIn, coordsPortal.GetPortal());
vtkm::ErrorCode status = locator.FindCell(pointIn, cellId, parametric);
if (status != vtkm::ErrorCode::Success)
{
this->RaiseError(vtkm::ErrorString(status));
match = false;
return;
}
match = (calculated == cellId);
}
private:
vtkm::Bounds Bounds;
vtkm::Id3 Dims;
};
void TestTest()
{
vtkm::cont::Invoker invoke;
vtkm::cont::DataSetBuilderRectilinear dsb;
std::vector<vtkm::Float32> X(4), Y(3), Z(5);
X[0] = 0.0f;
X[1] = 1.0f;
X[2] = 3.0f;
X[3] = 4.0f;
Y[0] = 0.0f;
Y[1] = 1.0f;
Y[2] = 2.0f;
Z[0] = 0.0f;
Z[1] = 1.0f;
Z[2] = 3.0f;
Z[3] = 5.0f;
Z[4] = 6.0f;
vtkm::cont::DataSet dataset = dsb.Create(X, Y, Z);
using StructuredType = vtkm::cont::CellSetStructured<3>;
vtkm::cont::CoordinateSystem coords = dataset.GetCoordinateSystem();
vtkm::cont::UnknownCellSet cellSet = dataset.GetCellSet();
vtkm::Bounds bounds = coords.GetBounds();
vtkm::Id3 dims =
cellSet.AsCellSet<StructuredType>().GetSchedulingRange(vtkm::TopologyElementTagPoint());
// Generate some sample points.
using PointType = vtkm::Vec3f;
std::vector<PointType> pointsVec;
std::default_random_engine dre;
std::uniform_real_distribution<vtkm::Float32> xCoords(0.0f, 4.0f);
std::uniform_real_distribution<vtkm::Float32> yCoords(0.0f, 2.0f);
std::uniform_real_distribution<vtkm::Float32> zCoords(0.0f, 6.0f);
for (size_t i = 0; i < 10; i++)
{
PointType point = vtkm::make_Vec(xCoords(dre), yCoords(dre), zCoords(dre));
pointsVec.push_back(point);
}
vtkm::cont::ArrayHandle<PointType> points =
vtkm::cont::make_ArrayHandle(pointsVec, vtkm::CopyFlag::Off);
// Initialize locator
vtkm::cont::CellLocatorRectilinearGrid locator;
locator.SetCoordinates(coords);
locator.SetCellSet(cellSet);
locator.Update();
// Query the points using the locator.
vtkm::cont::ArrayHandle<vtkm::Id> cellIds;
vtkm::cont::ArrayHandle<PointType> parametric;
vtkm::cont::ArrayHandle<bool> match;
LocatorWorklet worklet(bounds, dims);
invoke(worklet,
points,
locator,
coords.GetData().template AsArrayHandle<RectilinearType>(),
cellIds,
parametric,
match);
auto matchPortal = match.ReadPortal();
for (vtkm::Id index = 0; index < match.GetNumberOfValues(); index++)
{
VTKM_TEST_ASSERT(matchPortal.Get(index), "Points do not match");
}
}
} // anonymous namespace
int UnitTestCellLocatorRectilinearGrid(int argc, char* argv[])
{
return vtkm::cont::testing::Testing::Run(TestTest, argc, argv);
}

@ -7,8 +7,6 @@
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//============================================================================
#ifndef vtk_m_cont_testing_TestingCellLocatorTwoLevel_h
#define vtk_m_cont_testing_TestingCellLocatorTwoLevel_h
#include <vtkm/cont/ArrayCopy.h>
#include <vtkm/cont/CellLocatorTwoLevel.h>
@ -219,13 +217,8 @@ void TestCellLocator(const vtkm::Vec<vtkm::Id, DIMENSIONS>& dim, vtkm::Id number
}
}
} // anonymous
template <typename DeviceAdapter>
void TestingCellLocatorTwoLevel()
{
vtkm::cont::GetRuntimeDeviceTracker().ForceDevice(DeviceAdapter());
vtkm::UInt32 seed = static_cast<vtkm::UInt32>(std::time(nullptr));
std::cout << "Seed: " << seed << std::endl;
RandomGenerator.seed(seed);
@ -234,4 +227,9 @@ void TestingCellLocatorTwoLevel()
TestCellLocator(vtkm::Id2(18), 512); // 2D dataset
}
#endif // vtk_m_cont_testing_TestingCellLocatorTwoLevel_h
} // anonymous
int UnitTestCellLocatorTwoLevel(int argc, char* argv[])
{
return vtkm::cont::testing::Testing::Run(TestingCellLocatorTwoLevel, argc, argv);
}

@ -0,0 +1,167 @@
//============================================================================
// 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 <random>
#include <string>
#include <vtkm/cont/CellLocatorUniformGrid.h>
#include <vtkm/cont/DataSet.h>
#include <vtkm/cont/Invoker.h>
#include <vtkm/cont/testing/MakeTestDataSet.h>
#include <vtkm/cont/testing/Testing.h>
#include <vtkm/exec/CellLocatorUniformGrid.h>
#include <vtkm/worklet/WorkletMapField.h>
namespace
{
class LocatorWorklet : public vtkm::worklet::WorkletMapField
{
public:
LocatorWorklet(vtkm::Bounds& bounds, vtkm::Id3& cellDims)
: Bounds(bounds)
, CellDims(cellDims)
{
}
using ControlSignature =
void(FieldIn pointIn, ExecObject locator, FieldOut cellId, FieldOut parametric, FieldOut match);
using ExecutionSignature = void(_1, _2, _3, _4, _5);
template <typename PointType>
VTKM_EXEC vtkm::Id CalculateCellId(const PointType& point) const
{
if (!Bounds.Contains(point))
return -1;
vtkm::Id3 logical;
logical[0] = (point[0] == Bounds.X.Max)
? CellDims[0] - 1
: static_cast<vtkm::Id>(vtkm::Floor((point[0] / Bounds.X.Length()) *
static_cast<vtkm::FloatDefault>(CellDims[0])));
logical[1] = (point[1] == Bounds.Y.Max)
? CellDims[1] - 1
: static_cast<vtkm::Id>(vtkm::Floor((point[1] / Bounds.Y.Length()) *
static_cast<vtkm::FloatDefault>(CellDims[1])));
logical[2] = (point[2] == Bounds.Z.Max)
? CellDims[2] - 1
: static_cast<vtkm::Id>(vtkm::Floor((point[2] / Bounds.Z.Length()) *
static_cast<vtkm::FloatDefault>(CellDims[2])));
return logical[2] * CellDims[0] * CellDims[1] + logical[1] * CellDims[0] + logical[0];
}
template <typename PointType, typename LocatorType>
VTKM_EXEC void operator()(const PointType& pointIn,
const LocatorType& locator,
vtkm::Id& cellId,
PointType& parametric,
bool& match) const
{
vtkm::Id calculated = CalculateCellId(pointIn);
vtkm::ErrorCode status = locator.FindCell(pointIn, cellId, parametric);
if ((status != vtkm::ErrorCode::Success) && (status != vtkm::ErrorCode::CellNotFound))
{
this->RaiseError(vtkm::ErrorString(status));
match = false;
return;
}
match = (calculated == cellId);
}
private:
vtkm::Bounds Bounds;
vtkm::Id3 CellDims;
};
void TestTest()
{
vtkm::cont::Invoker invoke;
vtkm::cont::DataSet dataset = vtkm::cont::testing::MakeTestDataSet().Make3DUniformDataSet1();
vtkm::cont::CoordinateSystem coords = dataset.GetCoordinateSystem();
vtkm::cont::UnknownCellSet cellSet = dataset.GetCellSet();
vtkm::Bounds bounds = coords.GetBounds();
std::cout << "X bounds : " << bounds.X.Min << " to " << bounds.X.Max << std::endl;
std::cout << "Y bounds : " << bounds.Y.Min << " to " << bounds.Y.Max << std::endl;
std::cout << "Z bounds : " << bounds.Z.Min << " to " << bounds.Z.Max << std::endl;
using StructuredType = vtkm::cont::CellSetStructured<3>;
vtkm::Id3 cellDims =
cellSet.AsCellSet<StructuredType>().GetSchedulingRange(vtkm::TopologyElementTagCell());
std::cout << "Dimensions of dataset : " << cellDims << std::endl;
vtkm::cont::CellLocatorUniformGrid locator;
locator.SetCoordinates(coords);
locator.SetCellSet(cellSet);
locator.Update();
// Generate some sample points.
using PointType = vtkm::Vec3f;
std::vector<PointType> pointsVec;
std::default_random_engine dre;
std::uniform_real_distribution<vtkm::Float32> inBounds(0.0f, 4.0f);
for (size_t i = 0; i < 10; i++)
{
PointType point = vtkm::make_Vec(inBounds(dre), inBounds(dre), inBounds(dre));
pointsVec.push_back(point);
}
std::uniform_real_distribution<vtkm::Float32> outBounds(4.0f, 5.0f);
for (size_t i = 0; i < 5; i++)
{
PointType point = vtkm::make_Vec(outBounds(dre), outBounds(dre), outBounds(dre));
pointsVec.push_back(point);
}
std::uniform_real_distribution<vtkm::Float32> outBounds2(-1.0f, 0.0f);
for (size_t i = 0; i < 5; i++)
{
PointType point = vtkm::make_Vec(outBounds2(dre), outBounds2(dre), outBounds2(dre));
pointsVec.push_back(point);
}
// Add points right on the boundary.
pointsVec.push_back(vtkm::make_Vec(0, 0, 0));
pointsVec.push_back(vtkm::make_Vec(4, 4, 4));
pointsVec.push_back(vtkm::make_Vec(4, 0, 0));
pointsVec.push_back(vtkm::make_Vec(0, 4, 0));
pointsVec.push_back(vtkm::make_Vec(0, 0, 4));
pointsVec.push_back(vtkm::make_Vec(4, 4, 0));
pointsVec.push_back(vtkm::make_Vec(0, 4, 4));
pointsVec.push_back(vtkm::make_Vec(4, 0, 4));
vtkm::cont::ArrayHandle<PointType> points =
vtkm::cont::make_ArrayHandle(pointsVec, vtkm::CopyFlag::Off);
// Query the points using the locators.
vtkm::cont::ArrayHandle<vtkm::Id> cellIds;
vtkm::cont::ArrayHandle<PointType> parametric;
vtkm::cont::ArrayHandle<bool> match;
LocatorWorklet worklet(bounds, cellDims);
invoke(worklet, points, locator, cellIds, parametric, match);
auto matchPortal = match.ReadPortal();
for (vtkm::Id index = 0; index < match.GetNumberOfValues(); index++)
{
VTKM_TEST_ASSERT(matchPortal.Get(index), "Points do not match");
}
std::cout << "Test finished successfully." << std::endl;
}
} // anonymous namespace
int UnitTestCellLocatorUniformGrid(int argc, char* argv[])
{
return vtkm::cont::testing::Testing::Run(TestTest, argc, argv);
}