Commit 8deb6614 authored by Robert Maynard's avatar Robert Maynard

Add serialization support to ArrayHandle Virtual, Any, and Variant.

parent 28757bda
......@@ -26,6 +26,7 @@
#include <vtkm/cont/ArrayHandle.h>
#include <vtkm/cont/ArrayHandleUniformPointCoordinates.h>
#include <vtkm/cont/ArrayHandleVirtual.h>
namespace vtkm
......@@ -85,12 +86,19 @@ template <typename T>
class VTKM_ALWAYS_EXPORT ArrayHandleAny final : public vtkm::cont::ArrayHandleVirtual<T>
{
public:
///construct a valid ArrayHandleAny from an existing ArrayHandle
template <typename S>
VTKM_CONT ArrayHandleAny(const vtkm::cont::ArrayHandle<T, S>& ah)
: vtkm::cont::ArrayHandleVirtual<T>(std::make_shared<StorageAny<T, S>>(ah))
{
}
///construct an invalid ArrayHandleAny that has a nullptr storage
VTKM_CONT ArrayHandleAny()
: vtkm::cont::ArrayHandleVirtual<T>()
{
}
~ArrayHandleAny() = default;
};
......@@ -121,9 +129,119 @@ void CastAndCall(vtkm::cont::ArrayHandleVirtual<vtkm::Vec<vtkm::FloatDefault, 3>
f(coords, std::forward<Args>(args)...);
}
}
//=============================================================================
// Specializations of serialization related classes
template <typename T>
struct TypeString<vtkm::cont::ArrayHandleAny<T>>
{
static VTKM_CONT const std::string& Get()
{
static std::string name = "AH_Any<" + TypeString<T>::Get() + ">";
return name;
}
};
}
} //namespace vtkm::cont
#include <vtkm/cont/ArrayHandleConstant.h>
#include <vtkm/cont/ArrayHandleCounting.h>
//=============================================================================
// Specializations of serialization related classes
namespace diy
{
template <typename T>
struct Serialization<vtkm::cont::ArrayHandleAny<T>>
{
static VTKM_CONT void save(diy::BinaryBuffer& bb, const vtkm::cont::ArrayHandleAny<T>& obj)
{
vtkm::cont::internal::ArrayHandleDefaultSerialization(bb, obj);
}
static VTKM_CONT void load(BinaryBuffer& bb, vtkm::cont::ArrayHandleAny<T>& obj)
{
vtkm::cont::ArrayHandle<T> array;
diy::load(bb, array);
obj = std::move(vtkm::cont::ArrayHandleAny<T>{ array });
}
};
template <typename T>
struct IntAnySerializer
{
using CountingType = vtkm::cont::ArrayHandleCounting<T>;
using ConstantType = vtkm::cont::ArrayHandleConstant<T>;
using BasicType = vtkm::cont::ArrayHandle<T>;
static VTKM_CONT void save(diy::BinaryBuffer& bb, const vtkm::cont::ArrayHandleAny<T>& obj)
{
if (obj.template IsType<CountingType>())
{
diy::save(bb, vtkm::cont::TypeString<CountingType>::Get());
using S = typename CountingType::StorageTag;
const vtkm::cont::StorageVirtual* storage = obj.GetStorage();
auto* any = storage->Cast<vtkm::cont::StorageAny<T, S>>();
diy::save(bb, any->GetHandle());
}
else if (obj.template IsType<ConstantType>())
{
diy::save(bb, vtkm::cont::TypeString<ConstantType>::Get());
using S = typename ConstantType::StorageTag;
const vtkm::cont::StorageVirtual* storage = obj.GetStorage();
auto* any = storage->Cast<vtkm::cont::StorageAny<T, S>>();
diy::save(bb, any->GetHandle());
}
else
{
diy::save(bb, vtkm::cont::TypeString<BasicType>::Get());
vtkm::cont::internal::ArrayHandleDefaultSerialization(bb, obj);
}
}
static VTKM_CONT void load(BinaryBuffer& bb, vtkm::cont::ArrayHandleAny<T>& obj)
{
std::string typeString;
diy::load(bb, typeString);
if (typeString == vtkm::cont::TypeString<CountingType>::Get())
{
CountingType array;
diy::load(bb, array);
obj = std::move(vtkm::cont::ArrayHandleAny<T>{ array });
}
else if (typeString == vtkm::cont::TypeString<ConstantType>::Get())
{
ConstantType array;
diy::load(bb, array);
obj = std::move(vtkm::cont::ArrayHandleAny<T>{ array });
}
else
{
vtkm::cont::ArrayHandle<T> array;
diy::load(bb, array);
obj = std::move(vtkm::cont::ArrayHandleAny<T>{ array });
}
}
};
template <>
struct Serialization<vtkm::cont::ArrayHandleAny<vtkm::UInt8>> : public IntAnySerializer<vtkm::UInt8>
{
};
template <>
struct Serialization<vtkm::cont::ArrayHandleAny<vtkm::Int32>> : public IntAnySerializer<vtkm::Int32>
{
};
template <>
struct Serialization<vtkm::cont::ArrayHandleAny<vtkm::Int64>> : public IntAnySerializer<vtkm::Int64>
{
};
}
#endif //vtk_m_cont_ArrayHandleAny_h
......@@ -50,16 +50,14 @@ struct VTKM_ALWAYS_EXPORT Cast
template <typename T, typename ArrayHandleType>
class ArrayHandleCast
: public vtkm::cont::ArrayHandleTransform<ArrayHandleType,
internal::Cast<typename ArrayHandleType::ValueType, T>,
internal::Cast<T, typename ArrayHandleType::ValueType>>
internal::Cast<typename ArrayHandleType::ValueType, T>>
{
public:
VTKM_ARRAY_HANDLE_SUBCLASS(
ArrayHandleCast,
(ArrayHandleCast<T, ArrayHandleType>),
(vtkm::cont::ArrayHandleTransform<ArrayHandleType,
internal::Cast<typename ArrayHandleType::ValueType, T>,
internal::Cast<T, typename ArrayHandleType::ValueType>>));
internal::Cast<typename ArrayHandleType::ValueType, T>>));
ArrayHandleCast(const ArrayHandleType& handle)
: Superclass(handle)
......@@ -125,9 +123,7 @@ struct TypeString<vtkm::cont::internal::Cast<T1, T2>>
template <typename T, typename AH>
struct TypeString<vtkm::cont::ArrayHandleCast<T, AH>>
: TypeString<
vtkm::cont::ArrayHandleTransform<AH,
vtkm::cont::internal::Cast<typename AH::ValueType, T>,
vtkm::cont::internal::Cast<T, typename AH::ValueType>>>
vtkm::cont::ArrayHandleTransform<AH, vtkm::cont::internal::Cast<typename AH::ValueType, T>>>
{
};
}
......@@ -147,9 +143,7 @@ struct Serialization<vtkm::cont::internal::Cast<T1, T2>>
template <typename T, typename AH>
struct Serialization<vtkm::cont::ArrayHandleCast<T, AH>>
: Serialization<
vtkm::cont::ArrayHandleTransform<AH,
vtkm::cont::internal::Cast<typename AH::ValueType, T>,
vtkm::cont::internal::Cast<T, typename AH::ValueType>>>
vtkm::cont::ArrayHandleTransform<AH, vtkm::cont::internal::Cast<typename AH::ValueType, T>>>
{
};
......
......@@ -25,10 +25,8 @@
#include <vtkm/TypeListTag.h>
#include <vtkm/VecTraits.h>
#include <vtkm/cont/ArrayHandleAny.h>
#include <vtkm/cont/ArrayHandleAny.hxx>
#include <vtkm/cont/ArrayHandleVirtual.h>
#include <vtkm/cont/ErrorBadType.h>
#include <vtkm/cont/Logging.h>
#include <vtkm/cont/internal/DynamicTransform.h>
......
......@@ -300,13 +300,25 @@ private:
}
};
//=============================================================================
// Better name for the type
template <typename T>
using ArrayHandleVirtual = vtkm::cont::ArrayHandle<T, vtkm::cont::StorageTagVirtual>;
}
} //namespace vtkm::cont
#include <vtkm/cont/ArrayHandleAny.h>
//=============================================================================
// Specializations of serialization related classes
template <typename T>
struct TypeString<vtkm::cont::ArrayHandleVirtual<T>>
{
static VTKM_CONT const std::string& Get()
{
static std::string name = "AH_Virtual<" + TypeString<T>::Get() + ">";
return name;
}
};
}
} //namespace vtkm::cont
#endif
#endif //vtk_m_cont_ArrayHandleVirtual_h
......@@ -68,4 +68,62 @@ inline void make_hostPortal(Payload&& payload, Args&&... args)
} // namespace vtkm::virts
#include <vtkm/cont/ArrayHandleAny.h>
//=============================================================================
// Specializations of serialization related classes
namespace diy
{
template <typename T>
struct Serialization<vtkm::cont::ArrayHandleVirtual<T>>
{
private:
using Type = vtkm::cont::ArrayHandleVirtual<T>;
using BaseType = vtkm::cont::ArrayHandle<T, vtkm::cont::StorageTagVirtual>;
using BasicType = vtkm::cont::ArrayHandle<T>;
public:
static VTKM_CONT void save(BinaryBuffer& bb, const BaseType& obj)
{
if (obj.template IsType<vtkm::cont::ArrayHandleAny<T>>())
{
const auto& array = static_cast<const vtkm::cont::ArrayHandleAny<T>&>(obj);
diy::save(bb, vtkm::cont::TypeString<vtkm::cont::ArrayHandleAny<T>>::Get());
diy::save(bb, array);
}
else
{
diy::save(bb, vtkm::cont::TypeString<BasicType>::Get());
vtkm::cont::internal::ArrayHandleDefaultSerialization(bb, obj);
}
}
static VTKM_CONT void load(BinaryBuffer& bb, BaseType& obj)
{
std::string typeString;
diy::load(bb, typeString);
if (typeString == vtkm::cont::TypeString<vtkm::cont::ArrayHandleAny<T>>::Get())
{
vtkm::cont::ArrayHandleAny<T> array;
diy::load(bb, array);
obj = std::move(array);
}
else if (typeString == vtkm::cont::TypeString<BasicType>::Get())
{
BasicType array;
diy::load(bb, array);
obj = std::move(vtkm::cont::ArrayHandleAny<T>{ array });
}
else
{
throw vtkm::cont::ErrorBadType("Error deserializing ArrayHandleVirtual. TypeString: " +
typeString);
}
}
};
}
#endif
......@@ -117,9 +117,12 @@ struct TypeString<vtkm::cont::ArrayHandleVirtualCoordinates>
{
static VTKM_CONT const std::string Get() { return "AH_VirtualCoordinates"; }
};
}
} // vtkm::cont
} // namespace cont
} // namespace vtkm
//=============================================================================
// Specializations of serialization related classes
namespace diy
{
......@@ -139,30 +142,29 @@ private:
public:
static VTKM_CONT void save(BinaryBuffer& bb, const BaseType& obj)
{
const auto& virtArray = static_cast<const vtkm::cont::ArrayHandleVirtualCoordinates&>(obj);
const vtkm::cont::StorageVirtual* storage = virtArray.GetStorage();
if (virtArray.IsType<vtkm::cont::ArrayHandleUniformPointCoordinates>())
const vtkm::cont::StorageVirtual* storage = obj.GetStorage();
if (obj.template IsType<vtkm::cont::ArrayHandleUniformPointCoordinates>())
{
using HandleType = vtkm::cont::ArrayHandleUniformPointCoordinates;
using T = typename HandleType::ValueType;
using S = typename HandleType::StorageTag;
auto array = storage->Cast<vtkm::cont::StorageAny<T, S>>();
diy::save(bb, vtkm::cont::TypeString<HandleType>::Get());
diy::save(bb, array);
diy::save(bb, array->GetHandle());
}
else if (virtArray.IsType<RectilinearCoordsArrayType>())
else if (obj.template IsType<RectilinearCoordsArrayType>())
{
using HandleType = RectilinearCoordsArrayType;
using T = typename HandleType::ValueType;
using S = typename HandleType::StorageTag;
auto array = storage->Cast<vtkm::cont::StorageAny<T, S>>();
diy::save(bb, vtkm::cont::TypeString<HandleType>::Get());
diy::save(bb, array);
diy::save(bb, array->GetHandle());
}
else
{
diy::save(bb, vtkm::cont::TypeString<BasicCoordsType>::Get());
vtkm::cont::internal::ArrayHandleDefaultSerialization(bb, virtArray);
vtkm::cont::internal::ArrayHandleDefaultSerialization(bb, obj);
}
}
......
......@@ -23,6 +23,9 @@
#include <vtkm/cont/vtkm_cont_export.h>
#include <vtkm/cont/ArrayHandleVirtual.h>
#include <vtkm/cont/ArrayHandleVirtual.hxx>
#include <vtkm/cont/ArrayHandleAny.hxx>
#include <memory>
#include <vtkm/Types.h>
......
......@@ -21,7 +21,7 @@
#define vtk_m_cont_testing_TestingSerialization_h
#include <vtkm/cont/ArrayHandle.h>
#include <vtkm/cont/DynamicArrayHandle.h>
#include <vtkm/cont/ArrayHandleVariant.h>
#include <vtkm/cont/testing/Testing.h>
// clang-format off
......
......@@ -36,7 +36,8 @@
#include <vtkm/cont/ArrayHandleUniformPointCoordinates.h>
#include <vtkm/cont/ArrayHandleVirtualCoordinates.h>
#include <vtkm/cont/ArrayHandleZip.h>
#include <vtkm/cont/DynamicArrayHandle.h>
#include <vtkm/cont/ArrayHandleVariant.h>
#include <vtkm/cont/testing/TestingSerialization.h>
......@@ -75,12 +76,10 @@ constexpr vtkm::Id ArraySize = 10;
using TestTypesList =
vtkm::ListTagBase<vtkm::Int8, vtkm::Id, vtkm::FloatDefault, vtkm::Vec<vtkm::FloatDefault, 3>>;
using TestStorageList = vtkm::ListTagBase<vtkm::cont::StorageTagBasic>;
template <typename T, typename S>
inline vtkm::cont::DynamicArrayHandleBase<vtkm::ListTagAppendUnique<TestTypesList, T>,
vtkm::ListTagAppendUnique<TestStorageList, S>>
MakeTestDynamicArrayHandle(const vtkm::cont::ArrayHandle<T, S>& array)
inline vtkm::cont::ArrayHandleVariantBase<vtkm::ListTagAppendUnique<TestTypesList, T>>
MakeTestArrayHandleVariant(const vtkm::cont::ArrayHandle<T, S>& array)
{
return array;
}
......@@ -92,7 +91,7 @@ struct TestArrayHandleBasic
{
auto array = RandomArrayHandle<T>::Make(ArraySize);
RunTest(array);
RunTest(MakeTestDynamicArrayHandle(array));
RunTest(MakeTestArrayHandleVariant(array));
}
};
......@@ -106,7 +105,7 @@ struct TestArrayHandleCartesianProduct
RandomArrayHandle<T>::Make(ArraySize),
RandomArrayHandle<T>::Make(ArraySize));
RunTest(array);
RunTest(MakeTestDynamicArrayHandle(array));
RunTest(MakeTestArrayHandleVariant(array));
}
};
......@@ -116,9 +115,9 @@ struct TestArrayHandleCast
void operator()(T) const
{
auto array =
vtkm::cont::make_ArrayHandleCast(RandomArrayHandle<vtkm::Int8>::Make(ArraySize), T{});
vtkm::cont::make_ArrayHandleCast<T>(RandomArrayHandle<vtkm::Int8>::Make(ArraySize));
RunTest(array);
RunTest(MakeTestDynamicArrayHandle(array));
RunTest(MakeTestArrayHandleVariant(array));
}
};
......@@ -130,7 +129,7 @@ struct TestArrayHandleCompositeVector
auto array = vtkm::cont::make_ArrayHandleCompositeVector(RandomArrayHandle<T>::Make(ArraySize),
RandomArrayHandle<T>::Make(ArraySize));
RunTest(array);
RunTest(MakeTestDynamicArrayHandle(array));
RunTest(MakeTestArrayHandleVariant(array));
}
};
......@@ -142,7 +141,7 @@ struct TestArrayHandleConcatenate
auto array = vtkm::cont::make_ArrayHandleConcatenate(RandomArrayHandle<T>::Make(ArraySize),
RandomArrayHandle<T>::Make(ArraySize));
RunTest(array);
RunTest(MakeTestDynamicArrayHandle(array));
RunTest(MakeTestArrayHandleVariant(array));
}
};
......@@ -154,7 +153,7 @@ struct TestArrayHandleConstant
T cval = RandomValue<T>::Make();
auto array = vtkm::cont::make_ArrayHandleConstant(cval, ArraySize);
RunTest(array);
RunTest(MakeTestDynamicArrayHandle(array));
RunTest(MakeTestArrayHandleVariant(array));
}
};
......@@ -167,7 +166,7 @@ struct TestArrayHandleCounting
T step = RandomValue<T>::Make(0, 5);
auto array = vtkm::cont::make_ArrayHandleCounting(start, step, ArraySize);
RunTest(array);
RunTest(MakeTestDynamicArrayHandle(array));
RunTest(MakeTestArrayHandleVariant(array));
}
};
......@@ -180,7 +179,7 @@ struct TestArrayHandleExtractComponent
auto array = vtkm::cont::make_ArrayHandleExtractComponent(
RandomArrayHandle<T>::Make(ArraySize), RandomValue<vtkm::IdComponent>::Make(0, numComps - 1));
RunTest(array);
RunTest(MakeTestDynamicArrayHandle(array));
RunTest(MakeTestArrayHandleVariant(array));
}
};
......@@ -197,21 +196,21 @@ struct TestArrayHandleGroupVec
{
auto array = vtkm::cont::make_ArrayHandleGroupVec<3>(flat);
RunTest(array);
RunTest(MakeTestDynamicArrayHandle(array));
RunTest(MakeTestArrayHandleVariant(array));
break;
}
case 4:
{
auto array = vtkm::cont::make_ArrayHandleGroupVec<4>(flat);
RunTest(array);
RunTest(MakeTestDynamicArrayHandle(array));
RunTest(MakeTestArrayHandleVariant(array));
break;
}
default:
{
auto array = vtkm::cont::make_ArrayHandleGroupVec<2>(flat);
RunTest(array);
RunTest(MakeTestDynamicArrayHandle(array));
RunTest(MakeTestArrayHandleVariant(array));
break;
}
}
......@@ -239,7 +238,7 @@ struct TestArrayHandleGroupVecVariable
// cannot make a DynamicArrayHandle containing ArrayHandleGroupVecVariable
// because of the variable number of components of its values.
// RunTest(MakeTestDynamicArrayHandle(array));
// RunTest(MakeTestArrayHandleVariant(array));
}
};
......@@ -270,7 +269,7 @@ struct TestArrayHandleImplicit
ImplicitFunctor<T> functor(RandomValue<T>::Make(2, 9));
auto array = vtkm::cont::make_ArrayHandleImplicit(functor, ArraySize);
RunTest(array);
RunTest(MakeTestDynamicArrayHandle(array));
RunTest(MakeTestArrayHandleVariant(array));
}
};
......@@ -279,7 +278,7 @@ void TestArrayHandleIndex()
auto size = RandomValue<vtkm::Id>::Make(2, 10);
auto array = vtkm::cont::ArrayHandleIndex(size);
RunTest(array);
RunTest(MakeTestDynamicArrayHandle(array));
RunTest(MakeTestArrayHandleVariant(array));
}
struct TestArrayHandlePermutation
......@@ -296,7 +295,7 @@ struct TestArrayHandlePermutation
RandomArrayHandle<vtkm::Id>::Make(ArraySize, 0, ArraySize - 1),
RandomArrayHandle<T>::Make(ArraySize));
RunTest(array);
RunTest(MakeTestDynamicArrayHandle(array));
RunTest(MakeTestArrayHandleVariant(array));
}
};
......@@ -307,7 +306,7 @@ struct TestArrayHandleReverse
{
auto array = vtkm::cont::make_ArrayHandleReverse(RandomArrayHandle<T>::Make(ArraySize));
RunTest(array);
RunTest(MakeTestDynamicArrayHandle(array));
RunTest(MakeTestArrayHandleVariant(array));
}
};
......@@ -330,7 +329,7 @@ struct TestArrayHandleSwizzle
auto array = make_ArrayHandleSwizzle(RandomArrayHandle<vtkm::Vec<T, 3>>::Make(ArraySize),
map2s[RandomValue<int>::Make(0, 5)]);
RunTest(array);
RunTest(MakeTestDynamicArrayHandle(array));
RunTest(MakeTestArrayHandleVariant(array));
break;
}
case 3:
......@@ -339,13 +338,14 @@ struct TestArrayHandleSwizzle
auto array = make_ArrayHandleSwizzle(RandomArrayHandle<vtkm::Vec<T, 3>>::Make(ArraySize),
map3s[RandomValue<int>::Make(0, 5)]);
RunTest(array);
RunTest(MakeTestDynamicArrayHandle(array));
RunTest(MakeTestArrayHandleVariant(array));
break;
}
}
}
};
struct TestArrayHandleTransform
{
struct TransformFunctor
......@@ -362,7 +362,7 @@ struct TestArrayHandleTransform
template <typename T>
VTKM_EXEC_CONT T operator()(const T& in) const
{
return in / T{ 2 };
return static_cast<T>(in / T{ 2 });
}
};
......@@ -372,7 +372,7 @@ struct TestArrayHandleTransform
auto array = vtkm::cont::make_ArrayHandleTransform(RandomArrayHandle<T>::Make(ArraySize),
TransformFunctor{});
RunTest(array);
RunTest(MakeTestDynamicArrayHandle(array));
RunTest(MakeTestArrayHandleVariant(array));
}
template <typename T>
......@@ -381,7 +381,7 @@ struct TestArrayHandleTransform
auto array = vtkm::cont::make_ArrayHandleTransform(
RandomArrayHandle<T>::Make(ArraySize), TransformFunctor{}, InverseTransformFunctor{});
RunTest(array);
RunTest(MakeTestDynamicArrayHandle(array));
RunTest(MakeTestArrayHandleVariant(array));
}
template <typename T>
......@@ -404,7 +404,7 @@ void TestArrayHandleUniformPointCoordinates()
{
auto array = MakeRandomArrayHandleUniformPointCoordinates();
RunTest(array);
RunTest(MakeTestDynamicArrayHandle(array));
RunTest(MakeTestArrayHandleVariant(array));
}
void TestArrayHandleVirtualCoordinates()
......@@ -432,7 +432,7 @@ void TestArrayHandleVirtualCoordinates()
}
RunTest(array);
RunTest(MakeTestDynamicArrayHandle(array));
RunTest(MakeTestArrayHandleVariant(array));
}
struct TestArrayHandleZip
......@@ -443,10 +443,11 @@ struct TestArrayHandleZip
auto array = vtkm::cont::make_ArrayHandleZip(RandomArrayHandle<T>::Make(ArraySize),
vtkm::cont::ArrayHandleIndex(ArraySize));
RunTest(array);
RunTest(MakeTestDynamicArrayHandle(array));
RunTest(MakeTestArrayHandleVariant(array));
}
};
//-----------------------------------------------------------------------------
void TestArrayHandleSerialization()
{
......
......@@ -26,20 +26,18 @@ namespace
{
using FieldTypeList = vtkm::ListTagBase<vtkm::Float32>;
using FieldStorageList = VTKM_DEFAULT_STORAGE_LIST_TAG;
using CellSetTypes = vtkm::ListTagBase<vtkm::cont::CellSetExplicit<>,
vtkm::cont::CellSetSingleType<>,
vtkm::cont::CellSetStructured<1>,
vtkm::cont::CellSetStructured<2>,
vtkm::cont::CellSetStructured<3>>;
using DataSetWrapper =
vtkm::cont::SerializableDataSet<FieldTypeList, FieldStorageList, CellSetTypes>;
using DataSetWrapper = vtkm::cont::SerializableDataSet<FieldTypeList, CellSetTypes>;
VTKM_CONT void TestEqualDataSet(const DataSetWrapper& ds1, const DataSetWrapper& ds2)
{
auto result = vtkm::cont::testing::test_equal_DataSets(
ds1.DataSet, ds2.DataSet, CellSetTypes{}, FieldTypeList{}, FieldStorageList{});
ds1.DataSet, ds2.DataSet, CellSetTypes{}, FieldTypeList{});
VTKM_TEST_ASSERT(result, result.GetMergedMessage());
}
......
Markdown is supported
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