Merge branch 'dataset-builder-fixes' into 'master'

DataSetBuilder fixes

There were some compiler warnings and testing issues with the
recently merged DataSetBuilder branch. This fixes some of
those problems.

See merge request !316
This commit is contained in:
Kenneth Moreland 2016-01-14 18:03:40 -05:00
commit 1d1334e2ea
3 changed files with 275 additions and 121 deletions

@ -35,15 +35,15 @@ typedef vtkm::cont::DeviceAdapterAlgorithm<VTKM_DEFAULT_DEVICE_ADAPTER_TAG> DFA;
template <typename T>
void ArrayHandleCPBasic(vtkm::cont::ArrayHandle<T> x,
vtkm::cont::ArrayHandle<T> y,
vtkm::cont::ArrayHandle<T> z)
vtkm::cont::ArrayHandle<T> y,
vtkm::cont::ArrayHandle<T> z)
{
vtkm::cont::ArrayHandleCartesianProduct<
vtkm::cont::ArrayHandle<T>,
vtkm::cont::ArrayHandle<T>,
vtkm::cont::ArrayHandle<T> > cpArray;
vtkm::cont::ArrayHandle<T>,
vtkm::cont::ArrayHandle<T>,
vtkm::cont::ArrayHandle<T> > cpArray;
vtkm::Id nx = x.GetNumberOfValues();
vtkm::Id ny = y.GetNumberOfValues();
vtkm::Id nz = z.GetNumberOfValues();
@ -53,21 +53,21 @@ void ArrayHandleCPBasic(vtkm::cont::ArrayHandle<T> x,
//Make sure we have the right number of values.
VTKM_TEST_ASSERT(cpArray.GetNumberOfValues() == (nx*ny*nz),
"Cartesian array constructor has wrong number of values");
"Cartesian array constructor has wrong number of values");
//Make sure the values are correct.
vtkm::Vec<T,3> val;
for (vtkm::Id i = 0; i < n; i++)
{
vtkm::Id idx0 = (i % (nx*ny)) % nx;
vtkm::Id idx1 = (i % (nx*ny)) / nx;
vtkm::Id idx2 = i / (nx*ny);
val = vtkm::Vec<T,3>(x.GetPortalConstControl().Get(idx0),
y.GetPortalConstControl().Get(idx1),
z.GetPortalConstControl().Get(idx2));
VTKM_TEST_ASSERT(test_equal(cpArray.GetPortalConstControl().Get(i), val),
"Wrong value in array");
vtkm::Id idx0 = (i % (nx*ny)) % nx;
vtkm::Id idx1 = (i % (nx*ny)) / nx;
vtkm::Id idx2 = i / (nx*ny);
val = vtkm::Vec<T,3>(x.GetPortalConstControl().Get(idx0),
y.GetPortalConstControl().Get(idx1),
z.GetPortalConstControl().Get(idx2));
VTKM_TEST_ASSERT(test_equal(cpArray.GetPortalConstControl().Get(i), val),
"Wrong value in array");
}
}
@ -76,28 +76,32 @@ void createArr(std::vector<T> &arr, std::size_t n)
{
arr.resize(n);
for (std::size_t i = 0; i < n; i++)
arr[i] = static_cast<T>(i);
arr[i] = static_cast<T>(i);
}
template <typename T>
void
RunTest()
{
std::size_t nX = 10, nY = 10, nZ = 10;
std::size_t nX = 11, nY = 13, nZ = 11;
for (std::size_t i = 1; i < nX; i++)
for (std::size_t j = 1; j < nY; j++)
for (std::size_t k = 1; k < nZ; k++)
{
std::vector<T> X,Y,Z;
createArr(X, nX);
createArr(Y, nY);
createArr(Z, nZ);
ArrayHandleCPBasic(vtkm::cont::make_ArrayHandle(X),
vtkm::cont::make_ArrayHandle(Y),
vtkm::cont::make_ArrayHandle(Z));
}
for (std::size_t i = 1; i < nX; i += 2)
{
for (std::size_t j = 1; j < nY; j += 4)
{
for (std::size_t k = 1; k < nZ; k += 5)
{
std::vector<T> X,Y,Z;
createArr(X, nX);
createArr(Y, nY);
createArr(Z, nZ);
ArrayHandleCPBasic(vtkm::cont::make_ArrayHandle(X),
vtkm::cont::make_ArrayHandle(Y),
vtkm::cont::make_ArrayHandle(Z));
}
}
}
}
void

@ -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;
@ -51,13 +61,13 @@ void ValidateDataSet(const vtkm::cont::DataSet &ds,
"Wrong number of coordinates.");
VTKM_TEST_ASSERT(ds.GetCellSet().GetCellSet().GetNumberOfCells() == numCells,
"Wrong number of cells.");
//Make sure the bounds are correct.
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,94 +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++)
{
T xi;
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)
{
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;
switch (fillMethod)
{
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;
std::size_t nx = 15, ny = 15, nz = 15;
int nm = 5;
std::vector<T> xvals, yvals, zvals;
vtkm::cont::DataSetBuilderRectilinear dataSetBuilder;
vtkm::cont::DataSet dataSet;
for (std::size_t i = 2; i < nx; i++)
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 = i*j, nc = (i-1)*(j-1);
FillArray(xvals, i, mx);
FillArray(yvals, j, my);
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);
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);
for (vtkm::Id trial = 0; trial < NUM_TRIALS; trial++)
{
std::cout << "Trial " << trial << std::endl;
//Test T *
ds = dsb.Create(i,j, &xvals[0],&yvals[0]);
ValidateDataSet(ds, 2, np, nc, bounds);
vtkm::Id3 dimensions(randomDim(g_RandomGenerator),
randomDim(g_RandomGenerator),
randomDim(g_RandomGenerator));
std::cout << "Dimensions: " << dimensions << std::endl;
//Test ArrayHandle
ds = dsb.Create(vtkm::cont::make_ArrayHandle(xvals),
vtkm::cont::make_ArrayHandle(yvals));
ValidateDataSet(ds, 2, np, nc, bounds);
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;
//Do the 3D cases.
for (std::size_t k = 2; k < nz; k++)
for (int mz = 0; mz < nm; mz++)
{
np = i*j*k;
nc = (i-1)*(j-1)*(k-1);
FillArray(zvals, k, mz);
bounds[4] = zvals[0];
bounds[5] = zvals[k-1];
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);
//Test std::vector
ds = dsb.Create(xvals, yvals, zvals);
ValidateDataSet(ds, 3, np, nc, bounds);
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
};
//Test T *
ds = dsb.Create(i,j,k, &xvals[0],&yvals[0], &zvals[0]);
ValidateDataSet(ds, 3, np, nc, bounds);
std::cout << " Create with std::vector" << std::endl;
dataSet = dataSetBuilder.Create(xCoordinates, yCoordinates);
ValidateDataSet(dataSet, 2, numPoints, numCells, 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 << " 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)
{
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;
}
b0 = static_cast<vtkm::Float64>(o);
b1 = static_cast<vtkm::Float64>(o + (n-1)*s);
template <typename T>
void FillMethod(vtkm::IdComponent method,
vtkm::Id dimensionSize,
T &origin,
T &spacing,
vtkm::Float64 &boundsMin,
vtkm::Float64 &boundsMax)
{
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;
}
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