VTK  9.7.20261110
vtkDeviceDataArray.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
48
49#ifndef vtkDeviceDataArray_h
50#define vtkDeviceDataArray_h
51
52#include "vtkAOSDataArrayTemplate.h" // For NewInstance
53#include "vtkCommonCoreModule.h" // For export macro
54#include "vtkGenericDataArray.h"
55#include "vtkMemoryDescriptor.h" // For the held descriptor
56#include "vtkSmartPointer.h" // For the held descriptor
57
58#include <atomic> // For the host copy's state
59#include <mutex> // For the host copy
60#include <type_traits> // For std::is_arithmetic
61#include <typeinfo> // For the type macro
62#include <vector> // For the host copy
63
64VTK_ABI_NAMESPACE_BEGIN
65template <typename ValueTypeT>
67 : public vtkGenericDataArray<vtkDeviceDataArray<ValueTypeT>, ValueTypeT,
68 vtkArrayTypes::VTK_DEVICE_DATA_ARRAY>
69{
70 static_assert(std::is_arithmetic<ValueTypeT>::value, "ValueTypeT must be a number");
71
72 using GenericDataArrayType = vtkGenericDataArray<vtkDeviceDataArray<ValueTypeT>, ValueTypeT,
74
75public:
77 // NewInstance() makes a host array: see the class description.
79 SelfType, GenericDataArrayType, vtkAOSDataArrayTemplate<ValueTypeT>, typeid(SelfType).name());
80 using typename Superclass::ValueType;
81
83
91 vtkMemoryDescriptor* descriptor, vtkIdType numberOfTuples, int numberOfComponents);
92
94
99 void* GetDeviceVoidPointer(vtkIdType valueIdx) override;
103
108 void* GetVoidPointer(vtkIdType valueIdx) override;
109
114 void ShallowCopy(vtkDataArray* other) override;
116
122
127 void Modified() override;
128 void DataChanged() override;
129
131
135 ValueType GetValue(vtkIdType valueIdx) const { return this->HostValues()[valueIdx]; }
136 void SetValue(vtkIdType valueIdx, ValueType value);
137 void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const;
138 void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple);
139 ValueType GetTypedComponent(vtkIdType tupleIdx, int compIdx) const
140 {
141 return this->HostValues()[tupleIdx * this->NumberOfComponents + compIdx];
142 }
143 void SetTypedComponent(vtkIdType tupleIdx, int compIdx, ValueType value);
145
146protected:
149
154
156
159 bool AllocateTuples(vtkIdType numTuples);
162
163private:
165
166 // The host copy, made (again) if it is missing or stale.
167 const ValueType* HostValues() const;
168 // Report a refused write, once per array.
169 void RefuseWrite();
170
171 vtkSmartPointer<vtkMemoryDescriptor> Descriptor;
173 vtkIdType DeviceValues = 0; // how many values the device memory holds
174
175 mutable std::vector<ValueType> Host;
176 mutable std::atomic<bool> HostCurrent{ false };
177 mutable std::mutex HostMutex;
178 std::atomic<bool> WriteRefused{ false };
179
180 vtkDeviceDataArray(const vtkDeviceDataArray&) = delete;
181 void operator=(const vtkDeviceDataArray&) = delete;
182};
183
184// Declare vtkArrayDownCast implementations for vtkDeviceDataArray:
186
187#ifndef vtkDeviceDataArray_cxx
188#ifdef _MSC_VER
189#pragma warning(push)
190// Needed when vtkDeviceDataArray is used from another class in vtkCommonCore.
191#pragma warning(disable : 4910) // extern and dllexport incompatible
192#endif
193extern template class VTKCOMMONCORE_EXPORT vtkDeviceDataArray<char>;
194extern template class VTKCOMMONCORE_EXPORT vtkDeviceDataArray<double>;
195extern template class VTKCOMMONCORE_EXPORT vtkDeviceDataArray<float>;
196extern template class VTKCOMMONCORE_EXPORT vtkDeviceDataArray<int>;
197extern template class VTKCOMMONCORE_EXPORT vtkDeviceDataArray<long>;
198extern template class VTKCOMMONCORE_EXPORT vtkDeviceDataArray<long long>;
199extern template class VTKCOMMONCORE_EXPORT vtkDeviceDataArray<short>;
200extern template class VTKCOMMONCORE_EXPORT vtkDeviceDataArray<signed char>;
201extern template class VTKCOMMONCORE_EXPORT vtkDeviceDataArray<unsigned char>;
202extern template class VTKCOMMONCORE_EXPORT vtkDeviceDataArray<unsigned int>;
203extern template class VTKCOMMONCORE_EXPORT vtkDeviceDataArray<unsigned long>;
204extern template class VTKCOMMONCORE_EXPORT vtkDeviceDataArray<unsigned long long>;
205extern template class VTKCOMMONCORE_EXPORT vtkDeviceDataArray<unsigned short>;
206#ifdef _MSC_VER
207#pragma warning(pop)
208#endif
209#endif
210
211VTK_ABI_NAMESPACE_END
212#endif // vtkDeviceDataArray_h
213// VTK-HeaderTest-Exclude: vtkDeviceDataArray.h
Array-Of-Structs implementation of vtkGenericDataArray.
static vtkAOSDataArrayTemplate * New()
virtual void ShallowCopy(vtkAbstractArray *src)
This method will copy the data from the source array to this array.
create and manipulate ordered lists of objects
a data array over GPU memory another library owns
vtkTypeBool ReserveTuples(vtkIdType numTuples) override
Refuse to grow past the device memory.
void Modified() override
Mark the host copy stale, so the next host read copies the device memory again.
static vtkDeviceDataArray * New()
vtkDataArray::MemorySpace GetMemorySpace() override
The device memory, as it is: where it lives, a pointer to it, and a descriptor holding this array (an...
void * GetDeviceVoidPointer(vtkIdType valueIdx) override
The device memory, as it is: where it lives, a pointer to it, and a descriptor holding this array (an...
vtkObjectBase * NewInstanceInternal() const override
bool ReallocateTuples(vtkIdType numTuples)
Concept methods: these succeed only within the device memory.
void ShallowCopy(vtkDataArray *other) override
Share another vtkDeviceDataArray's memory.
ValueType GetValue(vtkIdType valueIdx) const
The vtkGenericDataArray concept methods.
vtkCollection * NewMemoryDescriptors() override
The device memory, as it is: where it lives, a pointer to it, and a descriptor holding this array (an...
bool AllocateTuples(vtkIdType numTuples)
Concept methods: these succeed only within the device memory.
vtkAbstractTypeMacroWithNewInstanceType(SelfType, GenericDataArrayType, vtkAOSDataArrayTemplate< ValueTypeT >, typeid(SelfType).name())
vtkDeviceDataArray< ValueTypeT > SelfType
void SetTypedComponent(vtkIdType tupleIdx, int compIdx, ValueType value)
The vtkGenericDataArray concept methods.
~vtkDeviceDataArray() override
void SetTypedTuple(vtkIdType tupleIdx, const ValueType *tuple)
The vtkGenericDataArray concept methods.
void GetTypedTuple(vtkIdType tupleIdx, ValueType *tuple) const
The vtkGenericDataArray concept methods.
void * GetVoidPointer(vtkIdType valueIdx) override
The host copy of the values, made on first use.
void DataChanged() override
Tell the array explicitly that the data has changed.
bool SetDescriptor(vtkMemoryDescriptor *descriptor, vtkIdType numberOfTuples, int numberOfComponents)
Hold the memory descriptor describes as numberOfTuples tuples of numberOfComponents interleaved value...
void SetValue(vtkIdType valueIdx, ValueType value)
The vtkGenericDataArray concept methods.
ValueType GetTypedComponent(vtkIdType tupleIdx, int compIdx) const
The vtkGenericDataArray concept methods.
Describes a memory buffer on host or device, and who keeps it alive.
abstract base class for most VTK objects
Hold a reference to a vtkObjectBase instance.
int vtkTypeBool
Definition vtkABI.h:64
#define vtkArrayDownCast_TemplateFastCastMacro(ArrayT)
Same as vtkArrayDownCast_FastCastMacro, but treats ArrayT as a single-parameter template (the paramet...
int vtkIdType
Definition vtkType.h:366
vtkArrayTypes
Definition vtkType.h:71
@ VTK_DEVICE_DATA_ARRAY
Definition vtkType.h:100
#define VTK_NEWINSTANCE