10 #ifndef vtk_m_cont_ArrayHandle_h
11 #define vtk_m_cont_ArrayHandle_h
13 #include <vtkm/cont/vtkm_cont_export.h>
51 #if VTKM_STORAGE == VTKM_STORAGE_BASIC
53 #define VTKM_DEFAULT_STORAGE_TAG ::vtkm::cont::StorageTagBasic
55 #elif VTKM_STORAGE == VTKM_STORAGE_ERROR
58 #define VTKM_DEFAULT_STORAGE_TAG ::vtkm::cont::internal::StorageTagError
60 #elif (VTKM_STORAGE == VTKM_STORAGE_UNDEFINED) || !defined(VTKM_STORAGE)
62 #ifndef VTKM_DEFAULT_STORAGE_TAG
63 #warning If array storage is undefined, VTKM_DEFAULT_STORAGE_TAG must be defined.
82 class VTKM_CONT_EXPORT ArrayHandleBase
90 template <
typename T,
typename StorageTag>
91 using IsValidArrayHandle =
92 std::integral_constant<bool,
93 !(std::is_base_of<vtkm::cont::internal::UndefinedStorage,
94 vtkm::cont::internal::Storage<T, StorageTag>>::value)>;
100 template <
typename T,
typename StorageTag>
101 using IsInvalidArrayHandle =
102 std::integral_constant<bool, !IsValidArrayHandle<T, StorageTag>::value>;
110 template <
typename ArrayHandle>
111 using IsWritableArrayHandle =
112 vtkm::internal::PortalSupportsSets<typename std::decay<ArrayHandle>::type::WritePortalType>;
126 template <
typename T>
127 struct ArrayHandleCheck
128 : std::is_base_of<vtkm::cont::internal::ArrayHandleBase, std::decay_t<T>>::type
132 #define VTKM_IS_ARRAY_HANDLE(T) VTKM_STATIC_ASSERT(::vtkm::cont::internal::ArrayHandleCheck<T>{})
139 template <
typename T>
140 struct GetTypeInParentheses;
141 template <
typename T>
142 struct GetTypeInParentheses<void(T)>
150 #define VTK_M_ARRAY_HANDLE_SUBCLASS_IMPL(classname, fullclasstype, superclass, typename__) \
151 using Thisclass = typename__ vtkm::cont::detail::GetTypeInParentheses<void fullclasstype>::type; \
152 using Superclass = typename__ vtkm::cont::detail::GetTypeInParentheses<void superclass>::type; \
154 VTKM_IS_ARRAY_HANDLE(Superclass); \
163 classname(const Thisclass& src) \
169 classname(Thisclass&& src) noexcept \
170 : Superclass(std::move(src)) \
175 classname(const vtkm::cont::ArrayHandle<typename__ Superclass::ValueType, \
176 typename__ Superclass::StorageTag>& src) \
182 classname(vtkm::cont::ArrayHandle<typename__ Superclass::ValueType, \
183 typename__ Superclass::StorageTag>&& src) noexcept \
184 : Superclass(std::move(src)) \
189 Thisclass& operator=(const Thisclass& src) \
191 this->Superclass::operator=(src); \
196 Thisclass& operator=(Thisclass&& src) noexcept \
198 this->Superclass::operator=(std::move(src)); \
202 using ValueType = typename__ Superclass::ValueType; \
203 using StorageTag = typename__ Superclass::StorageTag
226 #define VTKM_ARRAY_HANDLE_SUBCLASS(classname, fullclasstype, superclass) \
227 VTK_M_ARRAY_HANDLE_SUBCLASS_IMPL(classname, fullclasstype, superclass, typename)
249 #define VTKM_ARRAY_HANDLE_SUBCLASS_NT(classname, superclass) \
250 VTK_M_ARRAY_HANDLE_SUBCLASS_IMPL(classname, (classname), superclass, )
255 VTKM_CONT_EXPORT
VTKM_CONT void ArrayHandleReleaseResourcesExecution(
256 const std::vector<vtkm::cont::internal::Buffer>& buffers);
258 VTKM_CONT_EXPORT
VTKM_CONT bool ArrayHandleIsOnDevice(
259 const std::vector<vtkm::cont::internal::Buffer>& buffers,
282 template <
typename T,
typename StorageTag_ = VTKM_DEFAULT_STORAGE_TAG>
286 (internal::IsValidArrayHandle<T, StorageTag_>::value),
287 "Attempted to create an ArrayHandle with an invalid type/storage combination.");
292 using StorageType = vtkm::cont::internal::Storage<ValueType, StorageTag>;
312 : Buffers(src.Buffers)
324 : Buffers(std::move(src.Buffers))
338 : Buffers(std::move(buffers))
368 this->Buffers = std::move(src.Buffers);
378 return this->Buffers == rhs.
Buffers;
384 return this->Buffers != rhs.
Buffers;
387 template <
typename VT,
typename ST>
393 template <
typename VT,
typename ST>
417 return this->ReadPortal(token);
421 return StorageType::CreateReadPortal(
422 this->GetBuffers(), vtkm::cont::DeviceAdapterTagUndefined{}, token);
438 return this->WritePortal(token);
442 return StorageType::CreateWritePortal(
443 this->GetBuffers(), vtkm::cont::DeviceAdapterTagUndefined{}, token);
450 return StorageType::GetNumberOfValues(this->GetBuffers());
469 StorageType::ResizeBuffers(numberOfValues, this->GetBuffers(), preserve, token);
476 this->Allocate(numberOfValues, preserve, token);
506 this->Allocate(numberOfValues, preserve, token);
508 if (startIndex < numberOfValues)
510 this->Fill(fillValue, startIndex, numberOfValues, token);
519 this->AllocateAndFill(numberOfValues, fillValue, preserve, token);
535 StorageType::Fill(this->GetBuffers(), fillValue, startIndex, endIndex, token);
540 this->Fill(fillValue, startIndex, endIndex, token);
545 this->Fill(fillValue, startIndex, this->GetNumberOfValues(), token);
554 detail::ArrayHandleReleaseResourcesExecution(this->Buffers);
577 return StorageType::CreateReadPortal(this->GetBuffers(), device, token);
596 return StorageType::CreateWritePortal(this->GetBuffers(), device, token);
618 return StorageType::CreateWritePortal(this->GetBuffers(), device, token);
626 return detail::ArrayHandleIsOnDevice(this->Buffers, device);
634 return this->IsOnDevice(vtkm::cont::DeviceAdapterTagUndefined{});
672 for (
auto&& buffer : this->Buffers)
674 buffer.Enqueue(token);
686 for (std::size_t bufferIndex = 0; bufferIndex < this->Buffers.size(); ++bufferIndex)
688 this->Buffers[bufferIndex].DeepCopyFrom(source.
Buffers[bufferIndex]);
698 return this->Buffers;
703 mutable std::vector<vtkm::cont::internal::Buffer>
Buffers;
708 this->Buffers[
static_cast<std::size_t
>(index)] = buffer;
713 this->Buffers = buffers;
717 this->Buffers = std::move(buffers);
724 template <
typename T>
733 inline void printSummary_ArrayHandle_Value(
vtkm::UInt8 value,
737 out << static_cast<int>(value);
742 inline void printSummary_ArrayHandle_Value(
vtkm::Int8 value,
746 out << static_cast<int>(value);
749 template <
typename T>
755 using Traits = vtkm::internal::SafeVecTraits<T>;
756 using ComponentType =
typename Traits::ComponentType;
757 using IsVecOfVec =
typename vtkm::internal::SafeVecTraits<ComponentType>::HasMultipleComponents;
760 printSummary_ArrayHandle_Value(Traits::GetComponent(value, 0), out, IsVecOfVec());
764 printSummary_ArrayHandle_Value(Traits::GetComponent(value, index), out, IsVecOfVec());
769 template <
typename T1,
typename T2>
776 printSummary_ArrayHandle_Value(
777 value.
first, out,
typename vtkm::internal::SafeVecTraits<T1>::HasMultipleComponents());
779 printSummary_ArrayHandle_Value(
780 value.
second, out,
typename vtkm::internal::SafeVecTraits<T2>::HasMultipleComponents());
788 template <
typename T,
typename StorageT>
795 using PortalType =
typename ArrayType::ReadPortalType;
796 using IsVec =
typename vtkm::internal::SafeVecTraits<T>::HasMultipleComponents;
800 out <<
"valueType=" << vtkm::cont::TypeToString<T>()
801 <<
" storageType=" << vtkm::cont::TypeToString<StorageT>() <<
" " << sz
802 <<
" values occupying " << (
static_cast<size_t>(sz) *
sizeof(T)) <<
" bytes [";
809 detail::printSummary_ArrayHandle_Value(portal.Get(i), out, IsVec());
818 detail::printSummary_ArrayHandle_Value(portal.Get(0), out, IsVec());
820 detail::printSummary_ArrayHandle_Value(portal.Get(1), out, IsVec());
822 detail::printSummary_ArrayHandle_Value(portal.Get(2), out, IsVec());
824 detail::printSummary_ArrayHandle_Value(portal.Get(sz - 3), out, IsVec());
826 detail::printSummary_ArrayHandle_Value(portal.Get(sz - 2), out, IsVec());
828 detail::printSummary_ArrayHandle_Value(portal.Get(sz - 1), out, IsVec());
839 VTKM_CONT inline void CreateBuffersImpl(std::vector<vtkm::cont::internal::Buffer>&);
840 template <
typename T,
typename S,
typename... Args>
841 VTKM_CONT inline void CreateBuffersImpl(std::vector<vtkm::cont::internal::Buffer>& buffers,
843 const Args&... args);
844 template <
typename... Args>
845 VTKM_CONT inline void CreateBuffersImpl(std::vector<vtkm::cont::internal::Buffer>& buffers,
846 const vtkm::cont::internal::Buffer& buffer,
847 const Args&... args);
849 template <
typename... Args>
850 VTKM_CONT inline void CreateBuffersImpl(std::vector<vtkm::cont::internal::Buffer>& buffers,
851 const std::vector<vtkm::cont::internal::Buffer>& addbuffs,
852 const Args&... args);
853 template <
typename Arg0,
typename... Args>
854 VTKM_CONT inline void CreateBuffersImpl(std::vector<vtkm::cont::internal::Buffer>& buffers,
856 const Args&... args);
858 VTKM_CONT inline void CreateBuffersImpl(std::vector<vtkm::cont::internal::Buffer>&)
863 template <
typename T,
typename S,
typename... Args>
864 VTKM_CONT inline void CreateBuffersImpl(std::vector<vtkm::cont::internal::Buffer>& buffers,
868 CreateBuffersImpl(buffers, array.
GetBuffers(), args...);
871 template <
typename... Args>
872 VTKM_CONT inline void CreateBuffersImpl(std::vector<vtkm::cont::internal::Buffer>& buffers,
873 const vtkm::cont::internal::Buffer& buffer,
876 buffers.push_back(buffer);
877 CreateBuffersImpl(buffers, args...);
880 template <
typename... Args>
881 VTKM_CONT inline void CreateBuffersImpl(std::vector<vtkm::cont::internal::Buffer>& buffers,
882 const std::vector<vtkm::cont::internal::Buffer>& addbuffs,
885 buffers.insert(buffers.end(), addbuffs.begin(), addbuffs.end());
886 CreateBuffersImpl(buffers, args...);
889 template <
typename T,
typename S,
typename... Args>
890 VTKM_CONT inline void CreateBuffersResolveArrays(std::vector<vtkm::cont::internal::Buffer>& buffers,
895 CreateBuffersImpl(buffers, array, args...);
898 template <
typename MetaData,
typename... Args>
899 VTKM_CONT inline void CreateBuffersResolveArrays(std::vector<vtkm::cont::internal::Buffer>& buffers,
901 const MetaData& metadata,
904 vtkm::cont::internal::Buffer buffer;
905 buffer.SetMetaData(metadata);
906 buffers.push_back(std::move(buffer));
907 CreateBuffersImpl(buffers, args...);
910 template <
typename Arg0,
typename... Args>
911 VTKM_CONT inline void CreateBuffersImpl(std::vector<vtkm::cont::internal::Buffer>& buffers,
919 using IsArray =
typename vtkm::cont::internal::ArrayHandleCheck<Arg0>::type::type;
920 CreateBuffersResolveArrays(buffers, IsArray{}, arg0, args...);
939 template <
typename... Args>
940 VTKM_CONT inline std::vector<vtkm::cont::internal::Buffer> CreateBuffers(
const Args&... args)
942 std::vector<vtkm::cont::internal::Buffer> buffers;
943 buffers.reserve(
sizeof...(args));
944 detail::CreateBuffersImpl(buffers, args...);
953 #ifndef vtk_m_cont_ArrayHandleBasic_h
957 #endif //vtk_m_cont_ArrayHandle_h