vtk-m2/vtkm/cont/testing/TestingComputeRange.h

165 lines
5.9 KiB
C
Raw Normal View History

2015-07-16 19:10:01 +00:00
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
2019-04-15 23:24:21 +00:00
//
2015-07-16 19:10:01 +00:00
// 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_TestingComputeRange_h
#define vtk_m_cont_testing_TestingComputeRange_h
2015-07-16 19:10:01 +00:00
#include <vtkm/Types.h>
#include <vtkm/cont/CoordinateSystem.h>
2015-07-16 19:10:01 +00:00
#include <vtkm/cont/Field.h>
#include <vtkm/cont/RuntimeDeviceTracker.h>
2015-07-16 19:10:01 +00:00
#include <vtkm/cont/testing/Testing.h>
// Required for implementation of ArrayRangeCompute for virtual arrays
#include <vtkm/cont/ArrayRangeComputeTemplate.h>
2015-07-16 19:10:01 +00:00
#include <algorithm>
#include <iostream>
#include <random>
2015-07-16 19:10:01 +00:00
2017-05-18 14:29:41 +00:00
namespace vtkm
{
namespace cont
{
namespace testing
{
2019-12-05 21:38:41 +00:00
using CustomTypeList = vtkm::List<vtkm::Vec<Int32, 3>,
vtkm::Vec<Int64, 3>,
vtkm::Vec<Float32, 3>,
vtkm::Vec<Float64, 3>,
vtkm::Vec<Int32, 9>,
vtkm::Vec<Int64, 9>,
vtkm::Vec<Float32, 9>,
vtkm::Vec<Float64, 9>>;
2017-05-18 14:29:41 +00:00
template <typename DeviceAdapterTag>
class TestingComputeRange
2015-07-16 19:10:01 +00:00
{
private:
template <typename T>
static void TestScalarField()
{
const vtkm::Id nvals = 11;
T data[nvals] = { 1, 2, 3, 4, 5, -5, -4, -3, -2, -1, 0 };
std::random_device rng;
std::mt19937 urng(rng());
std::shuffle(data, data + nvals, urng);
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-16 16:32:32 +00:00
auto field = vtkm::cont::make_Field(
"TestField", vtkm::cont::Field::Association::POINTS, data, nvals, vtkm::CopyFlag::Off);
2015-07-16 19:10:01 +00:00
vtkm::Range result;
field.GetRange(&result);
2015-07-16 19:10:01 +00:00
std::cout << result << std::endl;
2017-05-18 14:29:41 +00:00
VTKM_TEST_ASSERT((test_equal(result.Min, -5.0) && test_equal(result.Max, 5.0)),
"Unexpected scalar field range.");
2015-07-16 19:10:01 +00:00
}
template <typename T, vtkm::IdComponent NumberOfComponents>
2015-07-16 19:10:01 +00:00
static void TestVecField()
{
const vtkm::Id nvals = 11;
T data[nvals] = { 1, 2, 3, 4, 5, -5, -4, -3, -2, -1, 0 };
vtkm::Vec<T, NumberOfComponents> fieldData[nvals];
std::random_device rng;
std::mt19937 urng(rng());
for (vtkm::IdComponent i = 0; i < NumberOfComponents; ++i)
2015-07-16 19:10:01 +00:00
{
std::shuffle(data, data + nvals, urng);
2015-07-16 19:10:01 +00:00
for (vtkm::Id j = 0; j < nvals; ++j)
{
fieldData[j][i] = data[j];
}
}
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-16 16:32:32 +00:00
auto field = vtkm::cont::make_Field(
"TestField", vtkm::cont::Field::Association::POINTS, fieldData, nvals, vtkm::CopyFlag::Off);
2015-07-16 19:10:01 +00:00
vtkm::Range result[NumberOfComponents];
field.GetRange(result);
2015-07-16 19:10:01 +00:00
for (vtkm::IdComponent i = 0; i < NumberOfComponents; ++i)
2015-07-16 19:10:01 +00:00
{
2017-05-18 14:29:41 +00:00
VTKM_TEST_ASSERT((test_equal(result[i].Min, -5.0) && test_equal(result[i].Max, 5.0)),
"Unexpected vector field range.");
2015-07-16 19:10:01 +00:00
}
}
static void TestUniformCoordinateField()
{
vtkm::cont::CoordinateSystem field("TestField",
vtkm::Id3(10, 20, 5),
vtkm::Vec3f(0.0f, -5.0f, 4.0f),
vtkm::Vec3f(1.0f, 0.5f, 2.0f));
vtkm::Bounds result = field.GetBounds();
VTKM_TEST_ASSERT(test_equal(result.X.Min, 0.0), "Min x wrong.");
VTKM_TEST_ASSERT(test_equal(result.X.Max, 9.0), "Max x wrong.");
VTKM_TEST_ASSERT(test_equal(result.Y.Min, -5.0), "Min y wrong.");
VTKM_TEST_ASSERT(test_equal(result.Y.Max, 4.5), "Max y wrong.");
VTKM_TEST_ASSERT(test_equal(result.Z.Min, 4.0), "Min z wrong.");
VTKM_TEST_ASSERT(test_equal(result.Z.Max, 12.0), "Max z wrong.");
}
2015-07-16 19:10:01 +00:00
struct TestAll
{
VTKM_CONT void operator()() const
2015-07-16 19:10:01 +00:00
{
std::cout << "Testing (Int32, 1)..." << std::endl;
TestingComputeRange::TestScalarField<vtkm::Int32>();
2015-07-16 19:10:01 +00:00
std::cout << "Testing (Int64, 1)..." << std::endl;
TestingComputeRange::TestScalarField<vtkm::Int64>();
2015-07-16 19:10:01 +00:00
std::cout << "Testing (Float32, 1)..." << std::endl;
TestingComputeRange::TestScalarField<vtkm::Float32>();
2015-07-16 19:10:01 +00:00
std::cout << "Testing (Float64, 1)..." << std::endl;
TestingComputeRange::TestScalarField<vtkm::Float64>();
2015-07-16 19:10:01 +00:00
std::cout << "Testing (Int32, 3)..." << std::endl;
TestingComputeRange::TestVecField<vtkm::Int32, 3>();
2015-07-16 19:10:01 +00:00
std::cout << "Testing (Int64, 3)..." << std::endl;
TestingComputeRange::TestVecField<vtkm::Int64, 3>();
2015-07-16 19:10:01 +00:00
std::cout << "Testing (Float32, 3)..." << std::endl;
TestingComputeRange::TestVecField<vtkm::Float32, 3>();
2015-07-16 19:10:01 +00:00
std::cout << "Testing (Float64, 3)..." << std::endl;
TestingComputeRange::TestVecField<vtkm::Float64, 3>();
2015-07-16 19:10:01 +00:00
// TODO: These tests are temporarily disabled because the existing version of
// Field::GetRange does not compute the ranges for vectors of size 9. However,
// issue #575 would fix this issue. This test should be renabled when that is
// implemented.
#if 0
2015-07-16 19:10:01 +00:00
std::cout << "Testing (Int32, 9)..." << std::endl;
TestingComputeRange::TestVecField<vtkm::Int32, 9>();
2015-07-16 19:10:01 +00:00
std::cout << "Testing (Int64, 9)..." << std::endl;
TestingComputeRange::TestVecField<vtkm::Int64, 9>();
2015-07-16 19:10:01 +00:00
std::cout << "Testing (Float32, 9)..." << std::endl;
TestingComputeRange::TestVecField<vtkm::Float32, 9>();
2015-07-16 19:10:01 +00:00
std::cout << "Testing (Float64, 9)..." << std::endl;
TestingComputeRange::TestVecField<vtkm::Float64, 9>();
#endif
std::cout << "Testing UniformPointCoords..." << std::endl;
TestingComputeRange::TestUniformCoordinateField();
2015-07-16 19:10:01 +00:00
}
};
public:
static VTKM_CONT int Run(int argc, char* argv[])
2015-07-16 19:10:01 +00:00
{
vtkm::cont::GetRuntimeDeviceTracker().ForceDevice(DeviceAdapterTag());
return vtkm::cont::testing::Testing::Run(TestAll(), argc, argv);
2015-07-16 19:10:01 +00:00
}
};
}
}
} // namespace vtkm::cont::testing
#endif //vtk_m_cont_testing_TestingComputeRange_h