Commit 427f3cf5 authored by Kenneth Moreland's avatar Kenneth Moreland
Browse files

Make Rectilinear and Regular builder tests faster

Previously UnitTestDataSetBuilderRectilinear and
UnitTestDataSetBuilderRegular did an exhaustive test of all possible
grid sizes within a certain amount of dimensions and with some number of
ways to build point arrays. This created hundreds of thousands of cases
that were checked, and it was running a long time. At best it wasted
time and at worst ctest reported the test as timed out.

It is not really necessary to perform an exhaustive test. The tests are
changed to do 10 trials using random values for dimensions and fill
methods.
parent e0184e66
......@@ -28,10 +28,20 @@
#include <vtkm/cont/testing/Testing.h>
VTKM_THIRDPARTY_PRE_INCLUDE
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/static_assert.hpp>
VTKM_THIRDPARTY_POST_INCLUDE
#include <time.h>
#include <vector>
namespace DataSetBuilderRectilinearNamespace {
boost::mt19937 g_RandomGenerator;
typedef vtkm::cont::DeviceAdapterAlgorithm<VTKM_DEFAULT_DEVICE_ADAPTER_TAG> DFA;
typedef VTKM_DEFAULT_DEVICE_ADAPTER_TAG DeviceAdapter;
......@@ -76,111 +86,130 @@ void ValidateDataSet(const vtkm::cont::DataSet &ds,
}
template <typename T>
void FillArray(std::vector<T> &arr, std::size_t sz, int fillMethod)
void FillArray(std::vector<T> &arr,
vtkm::Id size,
vtkm::IdComponent fillMethod)
{
arr.resize(sz);
for (size_t i = 0; i < sz; i++)
arr.resize(static_cast<std::size_t>(size));
for (size_t i = 0; i < static_cast<std::size_t>(size); i++)
{
T xi;
switch (fillMethod)
{
T xi;
switch (fillMethod)
{
case 0: xi = static_cast<T>(i); break;
case 1: xi = static_cast<T>(i) / static_cast<vtkm::Float32>(sz-1); break;
case 2: xi = static_cast<T>(i*2); break;
case 3: xi = static_cast<T>(i*0.1f); break;
case 4: xi = static_cast<T>(i*i); break;
}
arr[i] = xi;
case 0: xi = static_cast<T>(i); break;
case 1: xi = static_cast<T>(i) / static_cast<vtkm::Float32>(size-1); break;
case 2: xi = static_cast<T>(i*2); break;
case 3: xi = static_cast<T>(i*0.1f); break;
case 4: xi = static_cast<T>(i*i); break;
}
arr[i] = xi;
}
}
template <typename T>
void
RectilinearTests()
{
vtkm::cont::DataSetBuilderRectilinear dsb;
vtkm::cont::DataSet ds;
const vtkm::Id NUM_TRIALS = 10;
const vtkm::Id MAX_DIM_SIZE = 20;
const vtkm::Id NUM_FILL_METHODS = 5;
vtkm::cont::DataSetBuilderRectilinear dataSetBuilder;
vtkm::cont::DataSet dataSet;
std::size_t nx = 15, ny = 15, nz = 15;
int nm = 5;
std::vector<T> xvals, yvals, zvals;
boost::random::uniform_int_distribution<vtkm::Id>
randomDim(2, MAX_DIM_SIZE);
boost::random::uniform_int_distribution<vtkm::IdComponent>
randomFill(0, NUM_FILL_METHODS-1);
for (std::size_t i = 2; i < nx; i++)
for (vtkm::Id trial = 0; trial < NUM_TRIALS; trial++)
{
for (std::size_t j = 2; j < ny; j++)
{
for (int mx = 0; mx < nm; mx++)
{
for (int my = 0; my < nm; my++)
{
//Do the 2D cases.
vtkm::Id np = static_cast<vtkm::Id>(i*j);
vtkm::Id nc = static_cast<vtkm::Id>((i-1)*(j-1));
FillArray(xvals, i, mx);
FillArray(yvals, j, my);
vtkm::Float64 bounds[6] = {xvals[0], xvals[i-1],
yvals[0], yvals[j-1],
0.0, 0.0};
//Test std::vector
ds = dsb.Create(xvals, yvals);
ValidateDataSet(ds, 2, np, nc, bounds);
//Test T *
ds = dsb.Create(static_cast<vtkm::Id>(i),
static_cast<vtkm::Id>(j),
&xvals[0],
&yvals[0]);
ValidateDataSet(ds, 2, np, nc, bounds);
//Test ArrayHandle
ds = dsb.Create(vtkm::cont::make_ArrayHandle(xvals),
vtkm::cont::make_ArrayHandle(yvals));
ValidateDataSet(ds, 2, np, nc, bounds);
//Do the 3D cases.
for (std::size_t k = 2; k < nz; k++)
{
for (int mz = 0; mz < nm; mz++)
{
np = static_cast<vtkm::Id>(i*j*k);
nc = static_cast<vtkm::Id>((i-1)*(j-1)*(k-1));
FillArray(zvals, k, mz);
bounds[4] = zvals[0];
bounds[5] = zvals[k-1];
//Test std::vector
ds = dsb.Create(xvals, yvals, zvals);
ValidateDataSet(ds, 3, np, nc, bounds);
//Test T *
ds = dsb.Create(static_cast<vtkm::Id>(i),
static_cast<vtkm::Id>(j),
static_cast<vtkm::Id>(k),
&xvals[0],
&yvals[0],
&zvals[0]);
ValidateDataSet(ds, 3, np, nc, bounds);
//Test ArrayHandle
ds = dsb.Create(vtkm::cont::make_ArrayHandle(xvals),
vtkm::cont::make_ArrayHandle(yvals),
vtkm::cont::make_ArrayHandle(zvals));
ValidateDataSet(ds, 3, np, nc, bounds);
}
}
}
}
}
std::cout << "Trial " << trial << std::endl;
vtkm::Id3 dimensions(randomDim(g_RandomGenerator),
randomDim(g_RandomGenerator),
randomDim(g_RandomGenerator));
std::cout << "Dimensions: " << dimensions << std::endl;
vtkm::IdComponent fillMethodX = randomFill(g_RandomGenerator);
vtkm::IdComponent fillMethodY = randomFill(g_RandomGenerator);
vtkm::IdComponent fillMethodZ = randomFill(g_RandomGenerator);
std::cout << "Fill methods: ["
<< fillMethodX << ","
<< fillMethodY << ","
<< fillMethodZ << "]" << std::endl;
std::vector<T> xCoordinates;
std::vector<T> yCoordinates;
std::vector<T> zCoordinates;
FillArray(xCoordinates, dimensions[0], fillMethodX);
FillArray(yCoordinates, dimensions[1], fillMethodY);
FillArray(zCoordinates, dimensions[2], fillMethodZ);
std::cout << "2D cases" << std::endl;
vtkm::Id numPoints = dimensions[0]*dimensions[1];
vtkm::Id numCells = (dimensions[0]-1)*(dimensions[1]-1);
vtkm::Float64 bounds[6] = {
xCoordinates.front(), xCoordinates.back(),
yCoordinates.front(), yCoordinates.back(),
0.0, 0.0
};
std::cout << " Create with std::vector" << std::endl;
dataSet = dataSetBuilder.Create(xCoordinates, yCoordinates);
ValidateDataSet(dataSet, 2, numPoints, numCells, bounds);
std::cout << " Create with C array" << std::endl;
dataSet = dataSetBuilder.Create(dimensions[0],
dimensions[1],
&xCoordinates.front(),
&yCoordinates.front());
ValidateDataSet(dataSet, 2, numPoints, numCells, bounds);
std::cout << " Create with ArrayHandle" << std::endl;
dataSet = dataSetBuilder.Create(vtkm::cont::make_ArrayHandle(xCoordinates),
vtkm::cont::make_ArrayHandle(yCoordinates));
ValidateDataSet(dataSet, 2, numPoints, numCells, bounds);
std::cout << "3D cases" << std::endl;
numPoints *= dimensions[2];
numCells *= dimensions[2]-1;
bounds[4] = zCoordinates.front();
bounds[5] = zCoordinates.back();
std::cout << " Create with std::vector" << std::endl;
dataSet = dataSetBuilder.Create(xCoordinates, yCoordinates, zCoordinates);
ValidateDataSet(dataSet, 3, numPoints, numCells, bounds);
std::cout << " Create with C array" << std::endl;
dataSet = dataSetBuilder.Create(dimensions[0],
dimensions[1],
dimensions[2],
&xCoordinates.front(),
&yCoordinates.front(),
&zCoordinates.front());
ValidateDataSet(dataSet, 3, numPoints, numCells, bounds);
std::cout << " Create with ArrayHandle" << std::endl;
dataSet = dataSetBuilder.Create(vtkm::cont::make_ArrayHandle(xCoordinates),
vtkm::cont::make_ArrayHandle(yCoordinates),
vtkm::cont::make_ArrayHandle(zCoordinates));
ValidateDataSet(dataSet, 3, numPoints, numCells, bounds);
}
}
void
TestDataSetBuilderRectilinear()
{
RectilinearTests<vtkm::Float32>();
RectilinearTests<vtkm::Float64>();
vtkm::UInt32 seed = static_cast<vtkm::UInt32>(time(NULL));
std::cout << "Seed: " << seed << std::endl;
g_RandomGenerator.seed(seed);
std::cout << "======== Float32 ==========================" << std::endl;
RectilinearTests<vtkm::Float32>();
std::cout << "======== Float64 ==========================" << std::endl;
RectilinearTests<vtkm::Float64>();
}
} // namespace DataSetBuilderRectilinearNamespace
......
......@@ -28,10 +28,20 @@
#include <vtkm/cont/testing/Testing.h>
VTKM_THIRDPARTY_PRE_INCLUDE
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/static_assert.hpp>
VTKM_THIRDPARTY_POST_INCLUDE
#include <time.h>
#include <vector>
namespace DataSetBuilderRegularNamespace {
boost::mt19937 g_RandomGenerator;
typedef vtkm::cont::DeviceAdapterAlgorithm<VTKM_DEFAULT_DEVICE_ADAPTER_TAG> DFA;
typedef VTKM_DEFAULT_DEVICE_ADAPTER_TAG DeviceAdapter;
......@@ -57,8 +67,8 @@ void ValidateDataSet(const vtkm::cont::DataSet &ds,
vtkm::Float64 res[6];
ds.GetCoordinateSystem().GetBounds(res, DeviceAdapter());
VTKM_TEST_ASSERT(test_equal(bounds[0], res[0]) && test_equal(bounds[1], res[1]) &&
test_equal(bounds[2], res[2]) && test_equal(bounds[3], res[3]) &&
test_equal(bounds[4], res[4]) && test_equal(bounds[5], res[5]),
test_equal(bounds[2], res[2]) && test_equal(bounds[3], res[3]) &&
test_equal(bounds[4], res[4]) && test_equal(bounds[5], res[5]),
"Bounds of coordinates do not match");
if (dim == 2)
......@@ -76,28 +86,115 @@ void ValidateDataSet(const vtkm::cont::DataSet &ds,
VTKM_TEST_ASSERT(shape == vtkm::CELL_SHAPE_HEXAHEDRON, "Wrong element type");
}
}
template <typename T>
void FillMethod(int method, vtkm::Id n, T &o, T &s,
vtkm::Float64 &b0, vtkm::Float64 &b1)
void FillMethod(vtkm::IdComponent method,
vtkm::Id dimensionSize,
T &origin,
T &spacing,
vtkm::Float64 &boundsMin,
vtkm::Float64 &boundsMax)
{
switch (method)
{
case 0 : o = 0; s = 1; break;
case 1 : o = 0; s = static_cast<T>(1.0/n); break;
case 2 : o = 0; s = 2; break;
case 3 : o = static_cast<T>(-(n-1)); s = 1; break;
case 4 : o = static_cast<T>(2.780941); s = static_cast<T>(182.381901); break;
}
switch (method)
{
case 0:
origin = 0;
spacing = 1;
break;
case 1:
origin = 0;
spacing = static_cast<T>(1.0/dimensionSize);
break;
case 2:
origin = 0;
spacing = 2;
break;
case 3:
origin = static_cast<T>(-(dimensionSize-1));
spacing = 1;
break;
case 4:
origin = static_cast<T>(2.780941);
spacing = static_cast<T>(182.381901);
break;
}
b0 = static_cast<vtkm::Float64>(o);
b1 = static_cast<vtkm::Float64>(o + (n-1)*s);
boundsMin = static_cast<vtkm::Float64>(origin);
boundsMax = static_cast<vtkm::Float64>(origin + (dimensionSize-1)*spacing);
}
template <typename T>
void
RegularTests()
{
const vtkm::Id NUM_TRIALS = 10;
const vtkm::Id MAX_DIM_SIZE = 20;
const vtkm::Id NUM_FILL_METHODS = 5;
vtkm::cont::DataSetBuilderRegular dataSetBuilder;
vtkm::cont::DataSet dataSet;
boost::random::uniform_int_distribution<vtkm::Id>
randomDim(2, MAX_DIM_SIZE);
boost::random::uniform_int_distribution<vtkm::IdComponent>
randomFill(0, NUM_FILL_METHODS-1);
for (vtkm::Id trial = 0; trial < NUM_TRIALS; trial++)
{
std::cout << "Trial " << trial << std::endl;
vtkm::Id3 dimensions(randomDim(g_RandomGenerator),
randomDim(g_RandomGenerator),
randomDim(g_RandomGenerator));
std::cout << "Dimensions: " << dimensions << std::endl;
vtkm::IdComponent fillMethodX = randomFill(g_RandomGenerator);
vtkm::IdComponent fillMethodY = randomFill(g_RandomGenerator);
vtkm::IdComponent fillMethodZ = randomFill(g_RandomGenerator);
std::cout << "Fill methods: ["
<< fillMethodX << ","
<< fillMethodY << ","
<< fillMethodZ << "]" << std::endl;
vtkm::Vec<T,3> origin;
vtkm::Vec<T,3> spacing;
vtkm::Float64 bounds[6];
FillMethod(fillMethodX,
dimensions[0],
origin[0],
spacing[0],
bounds[0],
bounds[1]);
FillMethod(fillMethodY,
dimensions[1],
origin[1],
spacing[1],
bounds[2],
bounds[3]);
FillMethod(fillMethodZ,
dimensions[2],
origin[2],
spacing[2],
bounds[4],
bounds[5]);
std::cout << "3D case" << std::endl;
vtkm::Id numPoints = dimensions[0]*dimensions[1]*dimensions[2];
vtkm::Id numCells = (dimensions[0]-1)*(dimensions[1]-1)*(dimensions[2]-1);
dataSet = dataSetBuilder.Create(dimensions, origin, spacing);
ValidateDataSet(dataSet, 3, numPoints, numCells, bounds);
std::cout << "2D case" << std::endl;
numPoints = dimensions[0]*dimensions[1];
numCells = (dimensions[0]-1)*(dimensions[1]-1);
bounds[4] = bounds[5] = 0.0;
dataSet = dataSetBuilder.Create(vtkm::Id2(dimensions[0], dimensions[1]),
vtkm::Vec<T,2>(origin[0], origin[1]),
vtkm::Vec<T,2>(spacing[0], spacing[1]));
ValidateDataSet(dataSet, 2, numPoints, numCells, bounds);
}
#if 0
vtkm::cont::DataSetBuilderRegular dsb;
vtkm::cont::DataSet ds;
......@@ -119,7 +216,7 @@ RegularTests()
FillMethod(mj, dims2[1], oj, sj, bounds[2],bounds[3]);
bounds[4] = bounds[5] = 0;
vtkm::Vec<T,2> o2(oi,oj), sp2(si,sj);
ds = dsb.Create(dims2, o2, sp2);
ValidateDataSet(ds, 2, np, nc, bounds);
......@@ -129,7 +226,7 @@ RegularTests()
{
np = i*j*k;
nc = (i-1)*(j-1)*(k-1);
vtkm::Id3 dims3(i,j,k);
T ok, sk;
FillMethod(mk, dims3[2], ok, sk, bounds[4],bounds[5]);
......@@ -138,13 +235,20 @@ RegularTests()
ValidateDataSet(ds, 3, np, nc, bounds);
}
}
#endif
}
void
TestDataSetBuilderRegular()
{
RegularTests<vtkm::Float32>();
RegularTests<vtkm::Float64>();
vtkm::UInt32 seed = static_cast<vtkm::UInt32>(time(NULL));
std::cout << "Seed: " << seed << std::endl;
g_RandomGenerator.seed(seed);
std::cout << "======== Float32 ==========================" << std::endl;
RegularTests<vtkm::Float32>();
std::cout << "======== Float64 ==========================" << std::endl;
RegularTests<vtkm::Float64>();
}
} // namespace DataSetBuilderRegularNamespace
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment