VTK-m  2.0
ArrayHandleBasic.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_cont_ArrayHandleBasic_h
11 #define vtk_m_cont_ArrayHandleBasic_h
12 
13 #include <vtkm/cont/ArrayHandle.h>
17 #include <vtkm/cont/Storage.h>
18 
20 
21 #include <limits>
22 
23 namespace vtkm
24 {
25 namespace cont
26 {
27 
28 namespace internal
29 {
30 
31 template <typename T>
32 class VTKM_ALWAYS_EXPORT Storage<T, vtkm::cont::StorageTagBasic>
33 {
34 public:
35  using ReadPortalType = vtkm::internal::ArrayPortalBasicRead<T>;
36  using WritePortalType = vtkm::internal::ArrayPortalBasicWrite<T>;
37 
38  VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers()
39  {
40  return std::vector<vtkm::cont::internal::Buffer>(1);
41  }
42 
43  VTKM_CONT static void ResizeBuffers(vtkm::Id numValues,
44  const std::vector<vtkm::cont::internal::Buffer>& buffers,
45  vtkm::CopyFlag preserve,
46  vtkm::cont::Token& token)
47  {
48  buffers[0].SetNumberOfBytes(
49  vtkm::internal::NumberOfValuesToNumberOfBytes<T>(numValues), preserve, token);
50  }
51 
52  VTKM_CONT static vtkm::Id GetNumberOfValues(
53  const std::vector<vtkm::cont::internal::Buffer>& buffers)
54  {
55  VTKM_ASSERT(buffers.size() == 1);
56  return static_cast<vtkm::Id>(buffers[0].GetNumberOfBytes() /
57  static_cast<vtkm::BufferSizeType>(sizeof(T)));
58  }
59 
60  VTKM_CONT static void Fill(const std::vector<vtkm::cont::internal::Buffer>& buffers,
61  const T& fillValue,
62  vtkm::Id startIndex,
63  vtkm::Id endIndex,
64  vtkm::cont::Token& token)
65  {
66  VTKM_ASSERT(buffers.size() == 1);
67  constexpr vtkm::BufferSizeType fillValueSize =
68  static_cast<vtkm::BufferSizeType>(sizeof(fillValue));
69  buffers[0].Fill(
70  &fillValue, fillValueSize, startIndex * fillValueSize, endIndex * fillValueSize, token);
71  }
72 
73  VTKM_CONT static ReadPortalType CreateReadPortal(
74  const std::vector<vtkm::cont::internal::Buffer>& buffers,
76  vtkm::cont::Token& token)
77  {
78  VTKM_ASSERT(buffers.size() == 1);
79  return ReadPortalType(reinterpret_cast<const T*>(buffers[0].ReadPointerDevice(device, token)),
80  GetNumberOfValues(buffers));
81  }
82 
83  VTKM_CONT static WritePortalType CreateWritePortal(
84  const std::vector<vtkm::cont::internal::Buffer>& buffers,
86  vtkm::cont::Token& token)
87  {
88  VTKM_ASSERT(buffers.size() == 1);
89  return WritePortalType(reinterpret_cast<T*>(buffers[0].WritePointerDevice(device, token)),
90  GetNumberOfValues(buffers));
91  }
92 };
93 
94 } // namespace internal
95 
96 template <typename T>
97 class VTKM_ALWAYS_EXPORT ArrayHandleBasic : public ArrayHandle<T, vtkm::cont::StorageTagBasic>
98 {
99 public:
103 
105  T* array,
106  vtkm::Id numberOfValues,
107  vtkm::cont::internal::BufferInfo::Deleter deleter,
108  vtkm::cont::internal::BufferInfo::Reallocater reallocater = internal::InvalidRealloc)
109  : Superclass(std::vector<vtkm::cont::internal::Buffer>{ vtkm::cont::internal::MakeBuffer(
110  vtkm::cont::DeviceAdapterTagUndefined{},
111  array,
112  array,
113  vtkm::internal::NumberOfValuesToNumberOfBytes<T>(numberOfValues),
114  deleter,
115  reallocater) })
116  {
117  }
118 
120  T* array,
121  vtkm::Id numberOfValues,
123  vtkm::cont::internal::BufferInfo::Deleter deleter,
124  vtkm::cont::internal::BufferInfo::Reallocater reallocater = internal::InvalidRealloc)
125  : Superclass(std::vector<vtkm::cont::internal::Buffer>{ vtkm::cont::internal::MakeBuffer(
126  device,
127  array,
128  array,
129  vtkm::internal::NumberOfValuesToNumberOfBytes<T>(numberOfValues),
130  deleter,
131  reallocater) })
132  {
133  }
134 
136  T* array,
137  void* container,
138  vtkm::Id numberOfValues,
139  vtkm::cont::internal::BufferInfo::Deleter deleter,
140  vtkm::cont::internal::BufferInfo::Reallocater reallocater = internal::InvalidRealloc)
141  : Superclass(std::vector<vtkm::cont::internal::Buffer>{ vtkm::cont::internal::MakeBuffer(
142  vtkm::cont::DeviceAdapterTagUndefined{},
143  array,
144  container,
145  vtkm::internal::NumberOfValuesToNumberOfBytes<T>(numberOfValues),
146  deleter,
147  reallocater) })
148  {
149  }
150 
152  T* array,
153  void* container,
154  vtkm::Id numberOfValues,
156  vtkm::cont::internal::BufferInfo::Deleter deleter,
157  vtkm::cont::internal::BufferInfo::Reallocater reallocater = internal::InvalidRealloc)
158  : Superclass(std::vector<vtkm::cont::internal::Buffer>{ vtkm::cont::internal::MakeBuffer(
159  device,
160  array,
161  container,
162  vtkm::internal::NumberOfValuesToNumberOfBytes<T>(numberOfValues),
163  deleter,
164  reallocater) })
165  {
166  }
167 
174  const T* GetReadPointer(vtkm::cont::Token& token) const
175  {
176  return reinterpret_cast<const T*>(this->GetBuffers()[0].ReadPointerHost(token));
177  }
178  const T* GetReadPointer() const
179  {
180  vtkm::cont::Token token;
181  return this->GetReadPointer(token);
182  }
184  {
185  return reinterpret_cast<T*>(this->GetBuffers()[0].WritePointerHost(token));
186  }
187  T* GetWritePointer() const
188  {
189  vtkm::cont::Token token;
190  return this->GetWritePointer(token);
191  }
192 
194  {
195  return reinterpret_cast<const T*>(this->GetBuffers()[0].ReadPointerDevice(device, token));
196  }
198  {
199  vtkm::cont::Token token;
200  return this->GetReadPointer(device, token);
201  }
203  {
204  return reinterpret_cast<T*>(this->GetBuffers()[0].WritePointerDevice(device, token));
205  }
207  {
208  vtkm::cont::Token token;
209  return this->GetWritePointer(device, token);
210  }
212 };
213 
216 template <typename T>
218  vtkm::Id numberOfValues,
219  vtkm::CopyFlag copy)
220 {
221  if (copy == vtkm::CopyFlag::On)
222  {
224  handle.Allocate(numberOfValues);
225  std::copy(
226  array, array + numberOfValues, vtkm::cont::ArrayPortalToIteratorBegin(handle.WritePortal()));
227  return handle;
228  }
229  else
230  {
231  return vtkm::cont::ArrayHandleBasic<T>(const_cast<T*>(array), numberOfValues, [](void*) {});
232  }
233 }
234 
240 template <typename T>
242  T*& array,
243  vtkm::Id numberOfValues,
244  vtkm::cont::internal::BufferInfo::Deleter deleter = internal::SimpleArrayDeleter<T>,
245  vtkm::cont::internal::BufferInfo::Reallocater reallocater = internal::SimpleArrayReallocater<T>)
246 {
247  vtkm::cont::ArrayHandleBasic<T> arrayHandle(array, numberOfValues, deleter, reallocater);
248  array = nullptr;
249  return arrayHandle;
250 }
251 
254 template <typename T, typename Allocator>
255 VTKM_CONT vtkm::cont::ArrayHandleBasic<T> make_ArrayHandle(const std::vector<T, Allocator>& array,
256  vtkm::CopyFlag copy)
257 {
258  if (!array.empty())
259  {
260  return make_ArrayHandle(array.data(), static_cast<vtkm::Id>(array.size()), copy);
261  }
262  else
263  {
264  // Vector empty. Just return an empty array handle.
266  }
267 }
268 
271 template <typename T, typename Allocator>
273 {
274  using vector_type = std::vector<T, Allocator>;
275  vector_type* container = new vector_type(std::move(array));
276  return vtkm::cont::ArrayHandleBasic<T>(container->data(),
277  container,
278  static_cast<vtkm::Id>(container->size()),
279  internal::StdVectorDeleter<T, Allocator>,
280  internal::StdVectorReallocater<T, Allocator>);
281 }
282 
283 template <typename T, typename Allocator>
286 {
287  return make_ArrayHandleMove(array);
288 }
289 
292 template <typename T>
294 {
295  return make_ArrayHandle(values.begin(), static_cast<vtkm::Id>(values.size()), vtkm::CopyFlag::On);
296 }
297 }
298 } // namespace vtkm::cont
299 
300 //=============================================================================
301 // Specializations of serialization related classes
303 namespace vtkm
304 {
305 namespace cont
306 {
307 
308 template <typename T>
309 struct SerializableTypeString<vtkm::cont::ArrayHandleBasic<T>>
310 {
311  static VTKM_CONT const std::string& Get()
312  {
313  static std::string name = "AH<" + SerializableTypeString<T>::Get() + ">";
314  return name;
315  }
316 };
317 
318 template <typename T>
319 struct SerializableTypeString<ArrayHandle<T, vtkm::cont::StorageTagBasic>>
320  : SerializableTypeString<vtkm::cont::ArrayHandleBasic<T>>
321 {
322 };
323 }
324 } // vtkm::cont
325 
326 namespace mangled_diy_namespace
327 {
328 
329 template <typename T>
330 struct Serialization<vtkm::cont::ArrayHandleBasic<T>>
331 {
332  static VTKM_CONT void save(BinaryBuffer& bb,
334  {
335  vtkmdiy::save(bb, obj.GetBuffers()[0]);
336  }
337 
338  static VTKM_CONT void load(BinaryBuffer& bb,
340  {
341  vtkm::cont::internal::Buffer buffer;
342  vtkmdiy::load(bb, buffer);
343 
345  vtkm::cont::internal::CreateBuffers(buffer));
346  }
347 };
348 
349 template <typename T>
350 struct Serialization<vtkm::cont::ArrayHandle<T, vtkm::cont::StorageTagBasic>>
351  : Serialization<vtkm::cont::ArrayHandleBasic<T>>
352 {
353 };
354 
355 } // diy
357 
358 #ifndef vtk_m_cont_ArrayHandleBasic_cxx
359 
360 namespace vtkm
361 {
362 namespace cont
363 {
364 
365 namespace internal
366 {
367 
370 #define VTKM_STORAGE_EXPORT(Type) \
371  extern template class VTKM_CONT_TEMPLATE_EXPORT Storage<Type, StorageTagBasic>; \
372  extern template class VTKM_CONT_TEMPLATE_EXPORT Storage<vtkm::Vec<Type, 2>, StorageTagBasic>; \
373  extern template class VTKM_CONT_TEMPLATE_EXPORT Storage<vtkm::Vec<Type, 3>, StorageTagBasic>; \
374  extern template class VTKM_CONT_TEMPLATE_EXPORT Storage<vtkm::Vec<Type, 4>, StorageTagBasic>;
375 
376 VTKM_STORAGE_EXPORT(char)
377 VTKM_STORAGE_EXPORT(vtkm::Int8)
378 VTKM_STORAGE_EXPORT(vtkm::UInt8)
379 VTKM_STORAGE_EXPORT(vtkm::Int16)
380 VTKM_STORAGE_EXPORT(vtkm::UInt16)
381 VTKM_STORAGE_EXPORT(vtkm::Int32)
382 VTKM_STORAGE_EXPORT(vtkm::UInt32)
383 VTKM_STORAGE_EXPORT(vtkm::Int64)
384 VTKM_STORAGE_EXPORT(vtkm::UInt64)
385 VTKM_STORAGE_EXPORT(vtkm::Float32)
386 VTKM_STORAGE_EXPORT(vtkm::Float64)
387 
388 #undef VTKM_STORAGE_EXPORT
389 
391 } // namespace internal
392 
393 #define VTKM_ARRAYHANDLE_EXPORT(Type) \
394  extern template class VTKM_CONT_TEMPLATE_EXPORT ArrayHandle<Type, StorageTagBasic>; \
395  extern template class VTKM_CONT_TEMPLATE_EXPORT \
396  ArrayHandle<vtkm::Vec<Type, 2>, StorageTagBasic>; \
397  extern template class VTKM_CONT_TEMPLATE_EXPORT \
398  ArrayHandle<vtkm::Vec<Type, 3>, StorageTagBasic>; \
399  extern template class VTKM_CONT_TEMPLATE_EXPORT ArrayHandle<vtkm::Vec<Type, 4>, StorageTagBasic>;
400 
408 VTKM_ARRAYHANDLE_EXPORT(vtkm::Int64)
409 VTKM_ARRAYHANDLE_EXPORT(vtkm::UInt64)
412 
413 #undef VTKM_ARRAYHANDLE_EXPORT
414 }
415 } // end vtkm::cont
416 
417 #endif // !vtk_m_cont_ArrayHandleBasic_cxx
418 
419 #endif //vtk_m_cont_ArrayHandleBasic_h
vtkm::cont::ArrayHandle::GetBuffers
const VTKM_CONT std::vector< vtkm::cont::internal::Buffer > & GetBuffers() const
Returns the internal Buffer structures that hold the data.
Definition: ArrayHandle.h:696
vtkm::cont::ArrayPortalToIteratorBegin
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC_CONT vtkm::cont::ArrayPortalToIterators< PortalType >::IteratorType ArrayPortalToIteratorBegin(const PortalType &portal)
Convenience function for converting an ArrayPortal to a begin iterator.
Definition: ArrayPortalToIterators.h:178
vtkm::cont::make_ArrayHandle
VTKM_CONT vtkm::cont::ArrayHandleBasic< T > make_ArrayHandle(const T *array, vtkm::Id numberOfValues, vtkm::CopyFlag copy)
A convenience function for creating an ArrayHandle from a standard C array.
Definition: ArrayHandleBasic.h:217
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:283
ArrayHandle.h
vtkm::cont::ArrayHandleBasic::GetReadPointer
const T * GetReadPointer() const
Gets raw access to the ArrayHandle's data.
Definition: ArrayHandleBasic.h:178
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
VTKM_ARRAY_HANDLE_SUBCLASS
#define VTKM_ARRAY_HANDLE_SUBCLASS(classname, fullclasstype, superclass)
Macro to make default methods in ArrayHandle subclasses.
Definition: ArrayHandle.h:226
VTKM_ASSERT
#define VTKM_ASSERT(condition)
Definition: Assert.h:43
vtkm::cont::ArrayHandle< T, vtkm::cont::StorageTagBasic >::Allocate
VTKM_CONT void Allocate(vtkm::Id numberOfValues, vtkm::CopyFlag preserve, vtkm::cont::Token &token) const
Allocates an array large enough to hold the given number of values.
Definition: ArrayHandle.h:465
vtkm::Get
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC_CONT auto Get(const vtkm::Tuple< Ts... > &tuple) -> decltype(tuple.template Get< Index >())
Retrieve the object from a vtkm::Tuple at the given index.
Definition: Tuple.h:83
ArrayPortalToIterators.h
vtkm::cont::ArrayHandleBasic::GetWritePointer
T * GetWritePointer(vtkm::cont::DeviceAdapterId device) const
Gets raw access to the ArrayHandle's data.
Definition: ArrayHandleBasic.h:206
vtkm::cont::ArrayHandleBasic::GetReadPointer
const T * GetReadPointer(vtkm::cont::DeviceAdapterId device) const
Gets raw access to the ArrayHandle's data.
Definition: ArrayHandleBasic.h:197
Storage.h
vtkm::Int16
int16_t Int16
Definition: Types.h:158
vtkm::cont::ArrayHandleBasic::ArrayHandleBasic
ArrayHandleBasic(T *array, void *container, vtkm::Id numberOfValues, vtkm::cont::DeviceAdapterId device, vtkm::cont::internal::BufferInfo::Deleter deleter, vtkm::cont::internal::BufferInfo::Reallocater reallocater=internal::InvalidRealloc)
Definition: ArrayHandleBasic.h:151
vtkm::cont::ArrayHandleBasic::GetReadPointer
const T * GetReadPointer(vtkm::cont::Token &token) const
Gets raw access to the ArrayHandle's data.
Definition: ArrayHandleBasic.h:174
SerializableTypeString.h
vtkm::BufferSizeType
vtkm::Int64 BufferSizeType
Definition: DeviceAdapterMemoryManager.h:27
vtkm::cont::ArrayHandleBasic::GetWritePointer
T * GetWritePointer(vtkm::cont::Token &token) const
Gets raw access to the ArrayHandle's data.
Definition: ArrayHandleBasic.h:183
mangled_diy_namespace
Definition: Particle.h:331
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
vtkm::exec::arg::load
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC T load(const U &u, vtkm::Id v)
Definition: FetchTagArrayDirectIn.h:36
vtkm::cont::ArrayHandleBasic::ArrayHandleBasic
ArrayHandleBasic(T *array, vtkm::Id numberOfValues, vtkm::cont::DeviceAdapterId device, vtkm::cont::internal::BufferInfo::Deleter deleter, vtkm::cont::internal::BufferInfo::Reallocater reallocater=internal::InvalidRealloc)
Definition: ArrayHandleBasic.h:119
vtkm::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
vtkm::cont::make_ArrayHandleMove
VTKM_CONT vtkm::cont::ArrayHandleBasic< T > make_ArrayHandleMove(T *&array, vtkm::Id numberOfValues, vtkm::cont::internal::BufferInfo::Deleter deleter=internal::SimpleArrayDeleter< T >, vtkm::cont::internal::BufferInfo::Reallocater reallocater=internal::SimpleArrayReallocater< T >)
A convenience function to move a user-allocated array into an ArrayHandle.
Definition: ArrayHandleBasic.h:241
ArrayPortalBasic.h
vtkm::cont::ArrayHandleBasic::ArrayHandleBasic
ArrayHandleBasic(T *array, void *container, vtkm::Id numberOfValues, vtkm::cont::internal::BufferInfo::Deleter deleter, vtkm::cont::internal::BufferInfo::Reallocater reallocater=internal::InvalidRealloc)
Definition: ArrayHandleBasic.h:135
vtkm::cont::ArrayHandleBasic::GetReadPointer
const T * GetReadPointer(vtkm::cont::DeviceAdapterId device, vtkm::cont::Token &token) const
Gets raw access to the ArrayHandle's data.
Definition: ArrayHandleBasic.h:193
vtkm::Int8
int8_t Int8
Definition: Types.h:156
vtkm::cont::ArrayHandleBasic::GetWritePointer
T * GetWritePointer(vtkm::cont::DeviceAdapterId device, vtkm::cont::Token &token) const
Gets raw access to the ArrayHandle's data.
Definition: ArrayHandleBasic.h:202
Serialization.h
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::cont::ArrayHandle< T, vtkm::cont::StorageTagBasic >::WritePortal
VTKM_CONT WritePortalType WritePortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:435
vtkm::UInt8
uint8_t UInt8
Definition: Types.h:157
vtkm::CopyFlag::On
@ On
vtkmNotUsed
#define vtkmNotUsed(parameter_name)
Simple macro to identify a parameter as unused.
Definition: ExportMacros.h:128
vtkm::cont::ArrayHandleBasic::ArrayHandleBasic
ArrayHandleBasic(T *array, vtkm::Id numberOfValues, vtkm::cont::internal::BufferInfo::Deleter deleter, vtkm::cont::internal::BufferInfo::Reallocater reallocater=internal::InvalidRealloc)
Definition: ArrayHandleBasic.h:104
vtkm::cont::DeviceAdapterId
Definition: DeviceAdapterTag.h:52
vtkm::cont::ArrayHandleBasic::GetWritePointer
T * GetWritePointer() const
Gets raw access to the ArrayHandle's data.
Definition: ArrayHandleBasic.h:187
vtkm::UInt32
uint32_t UInt32
Definition: Types.h:161
vtkm::Float32
float Float32
Definition: Types.h:154
vtkm::Int32
int32_t Int32
Definition: Types.h:160
vtkm::Float64
double Float64
Definition: Types.h:155
vtkm::CopyFlag
CopyFlag
Definition: Flags.h:16
vtkm::worklet::splatkernels::vector_type
vtkm::Vec3f_64 vector_type
Definition: KernelBase.h:24
vtkm::cont::ArrayHandleBasic
Definition: ArrayHandleBasic.h:97
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:92
vtkm::UInt16
uint16_t UInt16
Definition: Types.h:159
VTKM_ARRAYHANDLE_EXPORT
#define VTKM_ARRAYHANDLE_EXPORT(Type)
Definition: ArrayHandleBasic.h:393