Go to the documentation of this file.
18 #ifndef viskores_cont_ArrayHandle_h
19 #define viskores_cont_ArrayHandle_h
59 #if VISKORES_STORAGE == VISKORES_STORAGE_BASIC
61 #define VISKORES_DEFAULT_STORAGE_TAG ::viskores::cont::StorageTagBasic
63 #elif VISKORES_STORAGE == VISKORES_STORAGE_ERROR
66 #define VISKORES_DEFAULT_STORAGE_TAG ::viskores::cont::internal::StorageTagError
68 #elif (VISKORES_STORAGE == VISKORES_STORAGE_UNDEFINED) || !defined(VISKORES_STORAGE)
70 #ifndef VISKORES_DEFAULT_STORAGE_TAG
71 #warning If array storage is undefined, VISKORES_DEFAULT_STORAGE_TAG must be defined.
90 class VISKORES_CONT_EXPORT ArrayHandleBase
98 template <
typename T,
typename StorageTag>
99 using IsValidArrayHandle = std::integral_constant<
101 !(std::is_base_of<viskores::cont::internal::UndefinedStorage,
102 viskores::cont::internal::Storage<T, StorageTag>>::value)>;
108 template <
typename T,
typename StorageTag>
109 using IsInvalidArrayHandle =
110 std::integral_constant<bool, !IsValidArrayHandle<T, StorageTag>::value>;
118 template <
typename ArrayHandle>
119 using IsWritableArrayHandle =
120 viskores::internal::PortalSupportsSets<typename std::decay<ArrayHandle>::type::WritePortalType>;
134 template <
typename T>
135 struct ArrayHandleCheck
136 : std::is_base_of<viskores::cont::internal::ArrayHandleBase, std::decay_t<T>>::type
145 #define VISKORES_IS_ARRAY_HANDLE(T) \
146 VISKORES_STATIC_ASSERT(::viskores::cont::internal::ArrayHandleCheck<T>{})
153 template <
typename T>
154 struct GetTypeInParentheses;
155 template <
typename T>
156 struct GetTypeInParentheses<void(T)>
164 #define VISKORES_ARRAY_HANDLE_SUBCLASS_IMPL(classname, fullclasstype, superclass, typename__) \
166 typename__ viskores::cont::detail::GetTypeInParentheses<void fullclasstype>::type; \
168 typename__ viskores::cont::detail::GetTypeInParentheses<void superclass>::type; \
170 VISKORES_IS_ARRAY_HANDLE(Superclass); \
178 classname(const Thisclass& src) \
184 classname(Thisclass&& src) noexcept \
185 : Superclass(std::move(src)) \
190 classname(const viskores::cont::ArrayHandle<typename__ Superclass::ValueType, \
191 typename__ Superclass::StorageTag>& src) \
197 classname(viskores::cont::ArrayHandle<typename__ Superclass::ValueType, \
198 typename__ Superclass::StorageTag>&& src) noexcept \
199 : Superclass(std::move(src)) \
204 explicit classname(const std::vector<viskores::cont::internal::Buffer>& buffers) \
205 : Superclass(buffers) \
210 explicit classname(std::vector<viskores::cont::internal::Buffer>&& buffers) noexcept \
211 : Superclass(std::move(buffers)) \
216 Thisclass& operator=(const Thisclass& src) \
218 this->Superclass::operator=(src); \
223 Thisclass& operator=(Thisclass&& src) noexcept \
225 this->Superclass::operator=(std::move(src)); \
229 using ValueType = typename__ Superclass::ValueType; \
230 using StorageTag = typename__ Superclass::StorageTag; \
231 using StorageType = typename__ Superclass::StorageType; \
232 using ReadPortalType = typename__ Superclass::ReadPortalType; \
233 using WritePortalType = typename__ Superclass::WritePortalType
256 #define VISKORES_ARRAY_HANDLE_SUBCLASS(classname, fullclasstype, superclass) \
257 VISKORES_ARRAY_HANDLE_SUBCLASS_IMPL(classname, fullclasstype, superclass, typename)
279 #define VISKORES_ARRAY_HANDLE_SUBCLASS_NT(classname, superclass) \
280 VISKORES_ARRAY_HANDLE_SUBCLASS_IMPL(classname, (classname), superclass, )
285 VISKORES_CONT_EXPORT
VISKORES_CONT void ArrayHandleReleaseResourcesExecution(
286 const std::vector<viskores::cont::internal::Buffer>& buffers);
288 VISKORES_CONT_EXPORT
VISKORES_CONT bool ArrayHandleIsOnDevice(
289 const std::vector<viskores::cont::internal::Buffer>& buffers,
312 template <
typename T,
typename StorageTag_ = VISKORES_DEFAULT_STORAGE_TAG>
313 class VISKORES_ALWAYS_EXPORT
ArrayHandle :
public internal::ArrayHandleBase
316 (internal::IsValidArrayHandle<T, StorageTag_>::value),
317 "Attempted to create an ArrayHandle with an invalid type/storage combination.");
322 using StorageType = viskores::cont::internal::Storage<ValueType, StorageTag>;
344 : Buffers(src.Buffers)
356 : Buffers(std::move(src.Buffers))
372 std::vector<viskores::cont::internal::Buffer>&& buffers) noexcept
373 : Buffers(std::move(buffers))
402 this->Buffers = std::move(src.Buffers);
412 return this->Buffers == rhs.
Buffers;
418 return this->Buffers != rhs.
Buffers;
421 template <
typename VT,
typename ST>
427 template <
typename VT,
typename ST>
450 return this->ReadPortal(token);
455 return StorageType::CreateReadPortal(
471 return this->WritePortal(token);
476 return StorageType::CreateWritePortal(
484 return StorageType::GetNumberOfValues(this->GetBuffers());
490 return StorageType::GetNumberOfComponentsFlat(this->GetBuffers());
508 StorageType::ResizeBuffers(numberOfValues, this->GetBuffers(), preserve, token);
516 this->Allocate(numberOfValues, preserve, token);
544 this->Allocate(numberOfValues, preserve, token);
546 if (startIndex < numberOfValues)
548 this->Fill(fillValue, startIndex, numberOfValues, token);
558 this->AllocateAndFill(numberOfValues, fillValue, preserve, token);
573 StorageType::Fill(this->GetBuffers(), fillValue, startIndex, endIndex, token);
581 this->Fill(fillValue, startIndex, endIndex, token);
587 this->Fill(fillValue, startIndex, this->GetNumberOfValues(), token);
595 detail::ArrayHandleReleaseResourcesExecution(this->Buffers);
618 return StorageType::CreateReadPortal(this->GetBuffers(), device, token);
637 return StorageType::CreateWritePortal(this->GetBuffers(), device, token);
659 return StorageType::CreateWritePortal(this->GetBuffers(), device, token);
667 return detail::ArrayHandleIsOnDevice(this->Buffers, device);
713 for (
auto&& buffer : this->Buffers)
715 buffer.Enqueue(token);
728 for (std::size_t bufferIndex = 0; bufferIndex < this->Buffers.size(); ++bufferIndex)
730 this->Buffers[bufferIndex].DeepCopyFrom(source.
Buffers[bufferIndex]);
740 return this->Buffers;
744 return this->Buffers;
748 mutable std::vector<viskores::cont::internal::Buffer>
Buffers;
752 const viskores::cont::internal::Buffer& buffer)
754 this->Buffers[
static_cast<std::size_t
>(index)] = buffer;
759 this->Buffers = buffers;
763 this->Buffers = std::move(buffers);
770 template <
typename T>
771 VISKORES_NEVER_EXPORT
VISKORES_CONT inline void printSummary_ArrayHandle_Value(
779 VISKORES_NEVER_EXPORT
785 out << static_cast<int>(value);
788 VISKORES_NEVER_EXPORT
794 out << static_cast<int>(value);
797 template <
typename T>
798 VISKORES_NEVER_EXPORT
VISKORES_CONT inline void printSummary_ArrayHandle_Value(
804 using ComponentType =
typename Traits::ComponentType;
808 printSummary_ArrayHandle_Value(Traits::GetComponent(value, 0), out, IsVecOfVec());
812 printSummary_ArrayHandle_Value(Traits::GetComponent(value, index), out, IsVecOfVec());
817 template <
typename T1,
typename T2>
818 VISKORES_NEVER_EXPORT
VISKORES_CONT inline void printSummary_ArrayHandle_Value(
824 printSummary_ArrayHandle_Value(
827 printSummary_ArrayHandle_Value(
836 template <
typename T,
typename StorageT>
843 using PortalType =
typename ArrayType::ReadPortalType;
848 out <<
"valueType=" << viskores::cont::TypeToString<T>()
849 <<
" storageType=" << viskores::cont::TypeToString<StorageT>() <<
" " << sz
850 <<
" values occupying " << (
static_cast<size_t>(sz) *
sizeof(T)) <<
" bytes [";
857 detail::printSummary_ArrayHandle_Value(portal.Get(i), out, IsVec());
866 detail::printSummary_ArrayHandle_Value(portal.Get(0), out, IsVec());
868 detail::printSummary_ArrayHandle_Value(portal.Get(1), out, IsVec());
870 detail::printSummary_ArrayHandle_Value(portal.Get(2), out, IsVec());
872 detail::printSummary_ArrayHandle_Value(portal.Get(sz - 3), out, IsVec());
874 detail::printSummary_ArrayHandle_Value(portal.Get(sz - 2), out, IsVec());
876 detail::printSummary_ArrayHandle_Value(portal.Get(sz - 1), out, IsVec());
887 VISKORES_CONT inline void CreateBuffersImpl(std::vector<viskores::cont::internal::Buffer>&);
888 template <
typename T,
typename S,
typename... Args>
889 VISKORES_CONT inline void CreateBuffersImpl(std::vector<viskores::cont::internal::Buffer>& buffers,
891 const Args&... args);
892 template <
typename... Args>
893 VISKORES_CONT inline void CreateBuffersImpl(std::vector<viskores::cont::internal::Buffer>& buffers,
894 const viskores::cont::internal::Buffer& buffer,
895 const Args&... args);
897 template <
typename... Args>
899 std::vector<viskores::cont::internal::Buffer>& buffers,
900 const std::vector<viskores::cont::internal::Buffer>& addbuffs,
901 const Args&... args);
902 template <
typename Arg0,
typename... Args>
903 VISKORES_CONT inline void CreateBuffersImpl(std::vector<viskores::cont::internal::Buffer>& buffers,
905 const Args&... args);
907 VISKORES_CONT inline void CreateBuffersImpl(std::vector<viskores::cont::internal::Buffer>&)
912 template <
typename T,
typename S,
typename... Args>
913 VISKORES_CONT inline void CreateBuffersImpl(std::vector<viskores::cont::internal::Buffer>& buffers,
917 CreateBuffersImpl(buffers, array.
GetBuffers(), args...);
920 template <
typename... Args>
921 VISKORES_CONT inline void CreateBuffersImpl(std::vector<viskores::cont::internal::Buffer>& buffers,
922 const viskores::cont::internal::Buffer& buffer,
925 buffers.push_back(buffer);
926 CreateBuffersImpl(buffers, args...);
929 template <
typename... Args>
931 std::vector<viskores::cont::internal::Buffer>& buffers,
932 const std::vector<viskores::cont::internal::Buffer>& addbuffs,
935 buffers.insert(buffers.end(), addbuffs.begin(), addbuffs.end());
936 CreateBuffersImpl(buffers, args...);
939 template <
typename T,
typename S,
typename... Args>
941 std::vector<viskores::cont::internal::Buffer>& buffers,
946 CreateBuffersImpl(buffers, array, args...);
949 template <
typename MetaData,
typename... Args>
951 std::vector<viskores::cont::internal::Buffer>& buffers,
953 const MetaData& metadata,
956 viskores::cont::internal::Buffer buffer;
957 buffer.SetMetaData(metadata);
958 buffers.push_back(std::move(buffer));
959 CreateBuffersImpl(buffers, args...);
962 template <
typename Arg0,
typename... Args>
963 VISKORES_CONT inline void CreateBuffersImpl(std::vector<viskores::cont::internal::Buffer>& buffers,
971 using IsArray =
typename viskores::cont::internal::ArrayHandleCheck<Arg0>::type::type;
972 CreateBuffersResolveArrays(buffers, IsArray{}, arg0, args...);
991 template <
typename... Args>
992 VISKORES_CONT inline std::vector<viskores::cont::internal::Buffer> CreateBuffers(
995 std::vector<viskores::cont::internal::Buffer> buffers;
996 buffers.reserve(
sizeof...(args));
997 detail::CreateBuffersImpl(buffers, args...);
1006 #ifndef viskores_cont_ArrayHandleBasic_h
1010 #endif //viskores_cont_ArrayHandle_h
viskores::cont::internal::Storage< ValueType, StorageTag > StorageType
Definition: ArrayHandle.h:322
StorageType GetStorage() const
Get the storage.
Definition: ArrayHandle.h:435
ReadPortalType ReadPortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:447
viskores::IdComponent GetNumberOfComponentsFlat() const
Returns the total number of components for each value in the array.
Definition: ArrayHandle.h:488
void Enqueue(const viskores::cont::Token &token) const
Enqueue a token for access to this ArrayHandle.
Definition: ArrayHandle.h:711
ReadPortalType ReadPortal(viskores::cont::Token &token) const
The type of portal used when accessing data in a read-only mode.
Definition: ArrayHandle.h:453
ArrayHandle()
Constructs an empty ArrayHandle.
Definition: ArrayHandle.h:331
void SetBuffers(const std::vector< viskores::cont::internal::Buffer > &buffers)
Definition: ArrayHandle.h:757
~ArrayHandle()
Destructs an empty ArrayHandle.
Definition: ArrayHandle.h:384
ReadPortalType PrepareForInput(viskores::cont::DeviceAdapterId device, viskores::cont::Token &token) const
Prepares this array to be used as an input to an operation in the execution environment.
Definition: ArrayHandle.h:615
bool operator!=(const ArrayHandle< VT, ST > &) const
Definition: ArrayHandle.h:428
viskores::cont::ArrayHandle< ValueType, StorageTag > & operator=(viskores::cont::ArrayHandle< ValueType, StorageTag > &&src) noexcept
Move and Assignment of an ArrayHandle.
Definition: ArrayHandle.h:399
int8_t Int8
Base type to use for 8-bit signed integer numbers.
Definition: Types.h:173
Manages an array-worth of data.
Definition: ArrayHandle.h:313
A tag for vectors that are "true" vectors (i.e.
Definition: VecTraits.h:31
void Fill(const ValueType &fillValue, viskores::Id startIndex=0) const
Fills the array with a given value.
Definition: ArrayHandle.h:584
bool operator==(const ArrayHandle< VT, ST > &) const
Definition: ArrayHandle.h:422
typename StorageType::ReadPortalType ReadPortalType
The type of portal used when accessing data in a read-only mode.
Definition: ArrayHandle.h:325
viskores::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:202
typename StorageType::WritePortalType WritePortalType
The type of portal used when accessing data in a read-write mode.
Definition: ArrayHandle.h:327
A tag for the basic implementation of a Storage object.
Definition: ArrayHandle.h:53
void SetBuffer(viskores::IdComponent index, const viskores::cont::internal::Buffer &buffer)
Definition: ArrayHandle.h:751
ArrayHandle(std::vector< viskores::cont::internal::Buffer > &&buffers) noexcept
Special constructor for subclass specializations that need to set the initial state array.
Definition: ArrayHandle.h:371
void SetBuffers(std::vector< viskores::cont::internal::Buffer > &&buffers)
Definition: ArrayHandle.h:761
void printSummary_ArrayHandle(const viskores::cont::ArrayHandle< T, StorageT > &array, std::ostream &out, bool full=false)
Definition: ArrayHandle.h:837
void Fill(const ValueType &fillValue, viskores::Id startIndex, viskores::Id endIndex) const
Fills the array with a given value.
Definition: ArrayHandle.h:576
std::vector< viskores::cont::internal::Buffer > & GetBuffers()
Definition: ArrayHandle.h:742
viskores::Int64 Id
Base type to use to index arrays.
Definition: Types.h:235
void ReleaseResources() const
Releases all resources in both the control and execution environments.
Definition: ArrayHandle.h:600
#define VISKORES_CONT
Definition: ExportMacros.h:65
Groups connected points that have the same field value.
Definition: Atomic.h:27
std::vector< viskores::cont::internal::Buffer > Buffers
Definition: ArrayHandle.h:748
FirstType first
The pair's first object.
Definition: Pair.h:58
WritePortalType PrepareForInPlace(viskores::cont::DeviceAdapterId device, viskores::cont::Token &token) const
Prepares this array to be used in an in-place operation (both as input and output) in the execution e...
Definition: ArrayHandle.h:634
void AllocateAndFill(viskores::Id numberOfValues, const ValueType &fillValue, viskores::CopyFlag preserve, viskores::cont::Token &token) const
Allocates an array and fills it with an initial value.
Definition: ArrayHandle.h:533
Traits that can be queried to treat any type as a Vec.
Definition: VecTraits.h:69
WritePortalType WritePortal(viskores::cont::Token &token) const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:474
bool operator==(const ArrayHandle< ValueType, StorageTag > &rhs) const
Like a pointer, two ArrayHandles are considered equal if they point to the same location in memory.
Definition: ArrayHandle.h:410
void Allocate(viskores::Id numberOfValues, viskores::CopyFlag preserve, viskores::cont::Token &token) const
Allocates an array large enough to hold the given number of values.
Definition: ArrayHandle.h:504
viskores::Id GetNumberOfValues() const
Returns the number of entries in the array.
Definition: ArrayHandle.h:482
Tag for a device adapter used to avoid specifying a device.
Definition: DeviceAdapterTag.h:201
WritePortalType PrepareForOutput(viskores::Id numberOfValues, viskores::cont::DeviceAdapterId device, viskores::cont::Token &token) const
Prepares (allocates) this array to be used as an output from an operation in the execution environmen...
Definition: ArrayHandle.h:654
#define VISKORES_ASSERT(condition)
Definition: Assert.h:51
bool operator!=(const ArrayHandle< ValueType, StorageTag > &rhs) const
Definition: ArrayHandle.h:416
WritePortalType WritePortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:468
void AllocateAndFill(viskores::Id numberOfValues, const ValueType &fillValue, viskores::CopyFlag preserve=viskores::CopyFlag::Off) const
Allocates an array and fills it with an initial value.
Definition: ArrayHandle.h:553
A viskores::Pair is essentially the same as an STL pair object except that the methods (constructors ...
Definition: Pair.h:37
#define VISKORES_STATIC_ASSERT_MSG(condition, message)
Definition: StaticAssert.h:26
bool IsOnDevice(viskores::cont::DeviceAdapterId device) const
Returns true if the ArrayHandle's data is on the given device.
Definition: ArrayHandle.h:665
uint8_t UInt8
Base type to use for 8-bit unsigned integer numbers.
Definition: Types.h:177
ArrayHandle(const std::vector< viskores::cont::internal::Buffer > &buffers)
Special constructor for subclass specializations that need to set the initial state array.
Definition: ArrayHandle.h:363
An object used to specify a device.
Definition: DeviceAdapterTag.h:66
void ReleaseResourcesExecution() const
Releases any resources being used in the execution environment (that are not being shared by the cont...
Definition: ArrayHandle.h:593
void DeepCopyFrom(const viskores::cont::ArrayHandle< ValueType, StorageTag > &source) const
Deep copies the data in the array.
Definition: ArrayHandle.h:723
viskores::cont::ArrayHandle< ValueType, StorageTag > & operator=(const viskores::cont::ArrayHandle< ValueType, StorageTag > &src)
Shallow copies an ArrayHandle.
Definition: ArrayHandle.h:389
T ValueType
Definition: ArrayHandle.h:320
void Fill(const ValueType &fillValue, viskores::Id startIndex, viskores::Id endIndex, viskores::cont::Token &token) const
Fills the array with a given value.
Definition: ArrayHandle.h:568
SecondType second
The pair's second object.
Definition: Pair.h:63
void SyncControlArray() const
Synchronizes the control array with the execution array.
Definition: ArrayHandle.h:684
bool IsOnHost() const
Returns true if the ArrayHandle's data is on the host.
Definition: ArrayHandle.h:673
CopyFlag
Identifier used to specify whether a function should deep copy data.
Definition: Flags.h:25
ArrayHandle(viskores::cont::ArrayHandle< ValueType, StorageTag > &&src) noexcept
Move constructor.
Definition: ArrayHandle.h:355
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:43
const std::vector< viskores::cont::internal::Buffer > & GetBuffers() const
Returns the internal Buffer structures that hold the data.
Definition: ArrayHandle.h:738
ArrayHandle(const viskores::cont::ArrayHandle< ValueType, StorageTag > &src)
Copy constructor.
Definition: ArrayHandle.h:343
void Allocate(viskores::Id numberOfValues, viskores::CopyFlag preserve=viskores::CopyFlag::Off) const
Allocates an array large enough to hold the given number of values.
Definition: ArrayHandle.h:512