ArrayHandleCounting now supports step

Modify ArrayHandleCounting so that it supports both a starting value and
a step (increment). This adds a multiplication, but the common case that
does not use it is already in a separate class (ArrayHandleIndex).
This commit is contained in:
Kenneth Moreland 2015-09-14 22:55:44 -06:00
parent 891182ee19
commit 44fc339a2e
4 changed files with 55 additions and 111 deletions

@ -42,20 +42,23 @@ public:
VTKM_EXEC_CONT_EXPORT
ArrayPortalCounting() :
StartingValue(),
Start(0),
Step(1),
NumberOfValues(0)
{ }
VTKM_EXEC_CONT_EXPORT
ArrayPortalCounting(ValueType startingValue, vtkm::Id numValues) :
StartingValue(startingValue),
ArrayPortalCounting(ValueType start, ValueType step, vtkm::Id numValues) :
Start(start),
Step(step),
NumberOfValues(numValues)
{ }
template<typename OtherValueType>
VTKM_EXEC_CONT_EXPORT
ArrayPortalCounting(const ArrayPortalCounting<OtherValueType> &src)
: StartingValue(src.StartingValue),
: Start(src.Start),
Step(src.Step),
NumberOfValues(src.NumberOfValues)
{ }
@ -64,7 +67,8 @@ public:
ArrayPortalCounting<ValueType> &operator=(
const ArrayPortalCounting<OtherValueType> &src)
{
this->StartingValue = src.StartingValue;
this->Start= src.Start;
this->Step = src.Step;
this->NumberOfValues = src.NumberOfValues;
return *this;
}
@ -74,12 +78,13 @@ public:
VTKM_EXEC_CONT_EXPORT
ValueType Get(vtkm::Id index) const {
return ValueType(StartingValue +
ValueType(static_cast<ComponentType>(index)));
return ValueType(this->Start +
this->Step*ValueType(static_cast<ComponentType>(index)));
}
private:
ValueType StartingValue;
ValueType Start;
ValueType Step;
vtkm::Id NumberOfValues;
};
@ -111,8 +116,10 @@ class ArrayHandleCounting
public:
VTKM_CONT_EXPORT
ArrayHandleCounting(CountingValueType startingValue, vtkm::Id length)
:Superclass(typename Superclass::PortalConstControl(startingValue, length))
ArrayHandleCounting(CountingValueType start,
CountingValueType step,
vtkm::Id length)
:Superclass(typename Superclass::PortalConstControl(start, step, length))
{
}
@ -125,10 +132,12 @@ public:
template<typename CountingValueType>
VTKM_CONT_EXPORT
vtkm::cont::ArrayHandleCounting<CountingValueType>
make_ArrayHandleCounting(CountingValueType startingValue,
make_ArrayHandleCounting(CountingValueType start,
CountingValueType step,
vtkm::Id length)
{
return vtkm::cont::ArrayHandleCounting<CountingValueType>(startingValue,
return vtkm::cont::ArrayHandleCounting<CountingValueType>(start,
step,
length);
}

@ -28,6 +28,7 @@
#include <vtkm/cont/ArrayHandleConstant.h>
#include <vtkm/cont/ArrayHandleCounting.h>
#include <vtkm/cont/ArrayHandleImplicit.h>
#include <vtkm/cont/ArrayHandleIndex.h>
#include <vtkm/cont/ArrayHandlePermutation.h>
#include <vtkm/cont/ArrayHandleTransform.h>
#include <vtkm/cont/ArrayHandleZip.h>
@ -43,58 +44,6 @@
namespace fancy_array_detail
{
class IncrementBy2
{
public:
VTKM_EXEC_CONT_EXPORT
IncrementBy2(): Value(0)
{
}
VTKM_EXEC_CONT_EXPORT
explicit IncrementBy2(vtkm::Id value): Value(2*value) { }
VTKM_EXEC_CONT_EXPORT
operator vtkm::Id() const { return this->Value; }
VTKM_EXEC_CONT_EXPORT
IncrementBy2 operator+(const IncrementBy2 &rhs) const
{
//don't want to trigger the vtkm::Id constructor
//when making a copy and cause this to be IncrementBy4
IncrementBy2 newValue;
newValue.Value = this->Value + rhs.Value;
return newValue;
}
VTKM_EXEC_CONT_EXPORT
bool operator==(const IncrementBy2 &other) const
{
return this->Value == other.Value;
}
VTKM_EXEC_CONT_EXPORT
IncrementBy2 &operator++()
{
this->Value++;
this->Value++;
return *this;
}
VTKM_CONT_EXPORT
friend std::ostream& operator<<(std::ostream &os, const IncrementBy2 &v)
{ os << v.Value; return os; }
vtkm::Id Value;
};
IncrementBy2 TestValue(vtkm::Id index, IncrementBy2)
{
return IncrementBy2(::TestValue(index, vtkm::Id()));
}
template<typename ValueType>
struct IndexSquared
{
@ -113,28 +62,10 @@ struct ValueSquared
VTKM_EXEC_CONT_EXPORT
ValueType operator()(U u) const
{ return vtkm::dot(u, u); }
VTKM_EXEC_CONT_EXPORT
ValueType operator()( ::fancy_array_detail::IncrementBy2 u) const
{ return ValueType( vtkm::dot(u.Value, u.Value) ); }
};
}
VTKM_BASIC_TYPE_VECTOR(::fancy_array_detail::IncrementBy2)
namespace vtkm { namespace testing {
template<>
struct TypeName< ::fancy_array_detail::IncrementBy2 >
{
static std::string Name()
{
return std::string("fancy_array_detail::IncrementBy2");
}
};
} }
namespace vtkm {
namespace cont {
namespace testing {
@ -247,7 +178,7 @@ private:
const ValueType start = ValueType(component_value);
vtkm::cont::ArrayHandleCounting< ValueType > counting =
vtkm::cont::make_ArrayHandleCounting(start, length);
vtkm::cont::make_ArrayHandleCounting(start, ValueType(1), length);
vtkm::cont::ArrayHandle< ValueType > result;
vtkm::worklet::DispatcherMapField< PassThrough, DeviceAdapterTag > dispatcher;
@ -322,6 +253,7 @@ private:
KeyHandleType counting =
vtkm::cont::make_ArrayHandleCounting<vtkm::Id>(start_pos,
1,
counting_length);
ValueHandleType implicit =
@ -419,9 +351,9 @@ private:
ComponentType component_value(0);
const ValueType start = ValueType(component_value);
vtkm::cont::ArrayHandleCounting< ValueType > counting =
vtkm::cont::make_ArrayHandleCounting(start,
length);
vtkm::cont::ArrayHandleCounting< ValueType > counting(start,
ValueType(1),
length);
vtkm::cont::ArrayHandleTransform<
OutputValueType,
@ -459,9 +391,9 @@ private:
VTKM_CONT_EXPORT
void operator()(CastToType vtkmNotUsed(type)) const
{
typedef vtkm::cont::ArrayHandleCounting<vtkm::Int64> InputArrayType;
typedef vtkm::cont::ArrayHandleIndex InputArrayType;
InputArrayType input(0, ARRAY_SIZE);
InputArrayType input(ARRAY_SIZE);
vtkm::cont::ArrayHandleCast<CastToType, InputArrayType> castArray =
vtkm::cont::make_ArrayHandleCast(input, CastToType());
vtkm::cont::ArrayHandle<CastToType> result;
@ -556,7 +488,7 @@ private:
values.Allocate(length*2);
KeyHandleType counting =
vtkm::cont::make_ArrayHandleCounting<vtkm::Id>(length, length);
vtkm::cont::make_ArrayHandleCounting<vtkm::Id>(length, 1, length);
PermutationHandleType permutation =
vtkm::cont::make_ArrayHandlePermutation(counting, values);
@ -641,8 +573,7 @@ private:
vtkm::Float32,
vtkm::Float64,
vtkm::Vec<vtkm::Float64,3>,
vtkm::Vec<vtkm::Float32,4>,
::fancy_array_detail::IncrementBy2
vtkm::Vec<vtkm::Float32,4>
>
{ };

@ -54,6 +54,16 @@ public:
return StringInt(this->Value + rhs.Value);
}
StringInt operator*(const StringInt &rhs) const
{
StringInt result;
for (vtkm::Id i = 0; i < rhs; i++)
{
result = result + *this;
}
return result;
}
bool operator==(const StringInt &other) const
{
return this->Value.size() == other.Value.size();
@ -88,14 +98,14 @@ struct TemplatedTests
typedef typename ArrayHandleType::PortalConstControl PortalType;
void operator()( const ValueType startingValue )
void operator()(const ValueType &startingValue, const ValueType &step)
{
ArrayHandleType arrayConst(startingValue, ARRAY_SIZE);
ArrayHandleType arrayConst(startingValue, step, ARRAY_SIZE);
ArrayHandleType arrayMake = vtkm::cont::make_ArrayHandleCounting(startingValue,ARRAY_SIZE);
ArrayHandleType arrayMake = vtkm::cont::make_ArrayHandleCounting(startingValue, step, ARRAY_SIZE);
ArrayHandleType2 arrayHandle =
ArrayHandleType2(PortalType(startingValue, ARRAY_SIZE));
ArrayHandleType2(PortalType(startingValue, step, ARRAY_SIZE));
VTKM_TEST_ASSERT(arrayConst.GetNumberOfValues() == ARRAY_SIZE,
"Counting array using constructor has wrong size.");
@ -116,28 +126,21 @@ struct TemplatedTests
VTKM_TEST_ASSERT(arrayHandle.GetPortalConstControl().Get(index) == properValue,
"Counting array using raw array handle + tag has unexpected value.");
++properValue;
properValue = properValue + step;
}
}
};
struct TestFunctor
{
template <typename T>
void operator()(const T t)
{
TemplatedTests<T> tests;
tests(t);
}
};
void TestArrayHandleCounting()
{
TestFunctor()(vtkm::Id(0));
TestFunctor()(vtkm::Float32(0));
TestFunctor()(vtkm::Float64(0));
TestFunctor()(StringInt(0));
TestFunctor()(StringInt(10));
TemplatedTests<vtkm::Id>()(0, 1);
TemplatedTests<vtkm::Id>()(8, 2);
TemplatedTests<vtkm::Float32>()(0.0f, 1.0f);
TemplatedTests<vtkm::Float32>()(3.0f, -0.5f);
TemplatedTests<vtkm::Float64>()(0.0, 1.0);
TemplatedTests<vtkm::Float64>()(-3.0, 2.0);
TemplatedTests<StringInt>()(StringInt(0), StringInt(1));
TemplatedTests<StringInt>()(StringInt(10), StringInt(2));
}

@ -142,6 +142,7 @@ struct TransformTests
<< std::endl;
vtkm::cont::ArrayHandleCounting<InputValueType> counting =
vtkm::cont::make_ArrayHandleCounting(InputValueType(OutputValueType(0)),
InputValueType(1),
ARRAY_SIZE);
CountingTransformHandle countingTransformed =
vtkm::cont::make_ArrayHandleTransform<OutputValueType>(counting, functor);