//============================================================================ // Copyright (c) Kitware, Inc. // All rights reserved. // See LICENSE.txt for details. // // 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_ArrayHandleZip_h #define vtk_m_cont_ArrayHandleZip_h #include #include #include namespace vtkm { namespace exec { namespace internal { /// \brief An array portal that zips two portals together into a single value /// for the execution environment template class ArrayPortalZip { using ReadableP1 = vtkm::internal::PortalSupportsGets; using ReadableP2 = vtkm::internal::PortalSupportsGets; using WritableP1 = vtkm::internal::PortalSupportsSets; using WritableP2 = vtkm::internal::PortalSupportsSets; using Readable = std::integral_constant; using Writable = std::integral_constant; public: using ValueType = ValueType_; using T = typename ValueType::FirstType; using U = typename ValueType::SecondType; using PortalTypeFirst = PortalTypeFirst_; using PortalTypeSecond = PortalTypeSecond_; VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC_CONT ArrayPortalZip() : PortalFirst() , PortalSecond() { } //needs to be host and device so that cuda can create lvalue of these VTKM_CONT ArrayPortalZip(const PortalTypeFirst& portalfirst, const PortalTypeSecond& portalsecond) : PortalFirst(portalfirst) , PortalSecond(portalsecond) { } /// Copy constructor for any other ArrayPortalZip with an iterator /// type that can be copied to this iterator type. This allows us to do any /// type casting that the iterators do (like the non-const to const cast). /// template VTKM_CONT ArrayPortalZip(const ArrayPortalZip& src) : PortalFirst(src.GetFirstPortal()) , PortalSecond(src.GetSecondPortal()) { } VTKM_EXEC_CONT vtkm::Id GetNumberOfValues() const { return this->PortalFirst.GetNumberOfValues(); } template ::type> VTKM_EXEC_CONT ValueType Get(vtkm::Id index) const noexcept { return vtkm::make_Pair(this->PortalFirst.Get(index), this->PortalSecond.Get(index)); } template ::type> VTKM_EXEC_CONT void Set(vtkm::Id index, const ValueType& value) const noexcept { this->PortalFirst.Set(index, value.first); this->PortalSecond.Set(index, value.second); } VTKM_EXEC_CONT const PortalTypeFirst& GetFirstPortal() const { return this->PortalFirst; } VTKM_EXEC_CONT const PortalTypeSecond& GetSecondPortal() const { return this->PortalSecond; } private: PortalTypeFirst PortalFirst; PortalTypeSecond PortalSecond; }; } } } // namespace vtkm::exec::internal namespace vtkm { namespace cont { template struct VTKM_ALWAYS_EXPORT StorageTagZip { }; namespace internal { /// This helper struct defines the value type for a zip container containing /// the given two array handles. /// template struct ArrayHandleZipTraits { /// The ValueType (a pair containing the value types of the two arrays). /// using ValueType = vtkm::Pair; /// The appropriately templated tag. /// using Tag = StorageTagZip; /// The superclass for ArrayHandleZip. /// using Superclass = vtkm::cont::ArrayHandle; }; template class Storage, vtkm::cont::StorageTagZip> { using FirstHandleType = vtkm::cont::ArrayHandle; using SecondHandleType = vtkm::cont::ArrayHandle; public: using ValueType = vtkm::Pair; using PortalType = vtkm::exec::internal::ArrayPortalZip; using PortalConstType = vtkm::exec::internal::ArrayPortalZip; VTKM_CONT Storage() : FirstArray() , SecondArray() { } VTKM_CONT Storage(const FirstHandleType& farray, const SecondHandleType& sarray) : FirstArray(farray) , SecondArray(sarray) { } VTKM_CONT PortalType GetPortal() { return PortalType(this->FirstArray.WritePortal(), this->SecondArray.WritePortal()); } VTKM_CONT PortalConstType GetPortalConst() const { return PortalConstType(this->FirstArray.ReadPortal(), this->SecondArray.ReadPortal()); } VTKM_CONT vtkm::Id GetNumberOfValues() const { VTKM_ASSERT(this->FirstArray.GetNumberOfValues() == this->SecondArray.GetNumberOfValues()); return this->FirstArray.GetNumberOfValues(); } VTKM_CONT void Allocate(vtkm::Id numberOfValues) { this->FirstArray.Allocate(numberOfValues); this->SecondArray.Allocate(numberOfValues); } VTKM_CONT void Shrink(vtkm::Id numberOfValues) { this->FirstArray.Shrink(numberOfValues); this->SecondArray.Shrink(numberOfValues); } VTKM_CONT void ReleaseResources() { // This request is ignored since it is asking to release the resources // of the two zipped array, which may be used elsewhere. } VTKM_CONT const FirstHandleType& GetFirstArray() const { return this->FirstArray; } VTKM_CONT const SecondHandleType& GetSecondArray() const { return this->SecondArray; } private: FirstHandleType FirstArray; SecondHandleType SecondArray; }; template class ArrayTransfer, vtkm::cont::StorageTagZip, Device> { using StorageTag = vtkm::cont::StorageTagZip; using StorageType = vtkm::cont::internal::Storage, StorageTag>; using FirstHandleType = vtkm::cont::ArrayHandle; using SecondHandleType = vtkm::cont::ArrayHandle; public: using ValueType = vtkm::Pair; using PortalControl = typename StorageType::PortalType; using PortalConstControl = typename StorageType::PortalConstType; using PortalExecution = vtkm::exec::internal::ArrayPortalZip< ValueType, typename FirstHandleType::template ExecutionTypes::Portal, typename SecondHandleType::template ExecutionTypes::Portal>; using PortalConstExecution = vtkm::exec::internal::ArrayPortalZip< ValueType, typename FirstHandleType::template ExecutionTypes::PortalConst, typename SecondHandleType::template ExecutionTypes::PortalConst>; VTKM_CONT ArrayTransfer(StorageType* storage) : FirstArray(storage->GetFirstArray()) , SecondArray(storage->GetSecondArray()) { } VTKM_CONT vtkm::Id GetNumberOfValues() const { VTKM_ASSERT(this->FirstArray.GetNumberOfValues() == this->SecondArray.GetNumberOfValues()); return this->FirstArray.GetNumberOfValues(); } VTKM_CONT PortalConstExecution PrepareForInput(bool vtkmNotUsed(updateData), vtkm::cont::Token& token) { return PortalConstExecution(this->FirstArray.PrepareForInput(Device(), token), this->SecondArray.PrepareForInput(Device(), token)); } VTKM_CONT PortalExecution PrepareForInPlace(bool vtkmNotUsed(updateData), vtkm::cont::Token& token) { return PortalExecution(this->FirstArray.PrepareForInPlace(Device(), token), this->SecondArray.PrepareForInPlace(Device(), token)); } VTKM_CONT PortalExecution PrepareForOutput(vtkm::Id numberOfValues, vtkm::cont::Token& token) { return PortalExecution(this->FirstArray.PrepareForOutput(numberOfValues, Device(), token), this->SecondArray.PrepareForOutput(numberOfValues, Device(), token)); } VTKM_CONT void RetrieveOutputData(StorageType* vtkmNotUsed(storage)) const { // Implementation of this method should be unnecessary. The internal // first and second array handles should automatically retrieve the // output data as necessary. } VTKM_CONT void Shrink(vtkm::Id numberOfValues) { this->FirstArray.Shrink(numberOfValues); this->SecondArray.Shrink(numberOfValues); } VTKM_CONT void ReleaseResources() { this->FirstArray.ReleaseResourcesExecution(); this->SecondArray.ReleaseResourcesExecution(); } private: FirstHandleType FirstArray; SecondHandleType SecondArray; }; } // namespace internal /// ArrayHandleZip is a specialization of ArrayHandle. It takes two delegate /// array handle and makes a new handle that access the corresponding entries /// in these arrays as a pair. /// template class ArrayHandleZip : public internal::ArrayHandleZipTraits::Superclass { // If the following line gives a compile error, then the FirstHandleType // template argument is not a valid ArrayHandle type. VTKM_IS_ARRAY_HANDLE(FirstHandleType); // If the following line gives a compile error, then the SecondHandleType // template argument is not a valid ArrayHandle type. VTKM_IS_ARRAY_HANDLE(SecondHandleType); public: VTKM_ARRAY_HANDLE_SUBCLASS( ArrayHandleZip, (ArrayHandleZip), (typename internal::ArrayHandleZipTraits::Superclass)); private: using StorageType = vtkm::cont::internal::Storage; public: VTKM_CONT ArrayHandleZip(const FirstHandleType& firstArray, const SecondHandleType& secondArray) : Superclass(StorageType(firstArray, secondArray)) { } }; /// A convenience function for creating an ArrayHandleZip. It takes the two /// arrays to be zipped together. /// template VTKM_CONT vtkm::cont::ArrayHandleZip make_ArrayHandleZip( const FirstHandleType& first, const SecondHandleType& second) { return ArrayHandleZip(first, second); } } } // namespace vtkm::cont //============================================================================= // Specializations of serialization related classes /// @cond SERIALIZATION namespace vtkm { namespace cont { template struct SerializableTypeString> { static VTKM_CONT const std::string& Get() { static std::string name = "AH_Zip<" + SerializableTypeString::Get() + "," + SerializableTypeString::Get() + ">"; return name; } }; template struct SerializableTypeString< vtkm::cont::ArrayHandle, vtkm::cont::StorageTagZip>> : SerializableTypeString, vtkm::cont::ArrayHandle>> { }; } } // namespace vtkm::cont namespace mangled_diy_namespace { template struct Serialization> { private: using Type = typename vtkm::cont::ArrayHandleZip; using BaseType = vtkm::cont::ArrayHandle; public: static VTKM_CONT void save(BinaryBuffer& bb, const BaseType& obj) { auto storage = obj.GetStorage(); vtkmdiy::save(bb, storage.GetFirstArray()); vtkmdiy::save(bb, storage.GetSecondArray()); } static VTKM_CONT void load(BinaryBuffer& bb, BaseType& obj) { AH1 a1; AH2 a2; vtkmdiy::load(bb, a1); vtkmdiy::load(bb, a2); obj = vtkm::cont::make_ArrayHandleZip(a1, a2); } }; template struct Serialization< vtkm::cont::ArrayHandle, vtkm::cont::StorageTagZip>> : Serialization, vtkm::cont::ArrayHandle>> { }; } // diy /// @endcond SERIALIZATION #endif //vtk_m_cont_ArrayHandleZip_h