VTK-m  2.2
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 <vtkm/VecFlat.h>
22 
23 #include <limits>
24 
25 namespace vtkm
26 {
27 namespace cont
28 {
29 
30 namespace internal
31 {
32 
33 template <typename T>
34 class VTKM_ALWAYS_EXPORT Storage<T, vtkm::cont::StorageTagBasic>
35 {
36 public:
37  using ReadPortalType = vtkm::internal::ArrayPortalBasicRead<T>;
38  using WritePortalType = vtkm::internal::ArrayPortalBasicWrite<T>;
39 
40  VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers()
41  {
42  return std::vector<vtkm::cont::internal::Buffer>(1);
43  }
44 
45  VTKM_CONT static void ResizeBuffers(vtkm::Id numValues,
46  const std::vector<vtkm::cont::internal::Buffer>& buffers,
47  vtkm::CopyFlag preserve,
48  vtkm::cont::Token& token)
49  {
50  buffers[0].SetNumberOfBytes(
51  vtkm::internal::NumberOfValuesToNumberOfBytes<T>(numValues), preserve, token);
52  }
53 
54  VTKM_CONT static vtkm::IdComponent GetNumberOfComponentsFlat(
55  const std::vector<vtkm::cont::internal::Buffer>&)
56  {
58  }
59 
60  VTKM_CONT static vtkm::Id GetNumberOfValues(
61  const std::vector<vtkm::cont::internal::Buffer>& buffers)
62  {
63  VTKM_ASSERT(buffers.size() == 1);
64  return static_cast<vtkm::Id>(buffers[0].GetNumberOfBytes() /
65  static_cast<vtkm::BufferSizeType>(sizeof(T)));
66  }
67 
68  VTKM_CONT static void Fill(const std::vector<vtkm::cont::internal::Buffer>& buffers,
69  const T& fillValue,
70  vtkm::Id startIndex,
71  vtkm::Id endIndex,
72  vtkm::cont::Token& token)
73  {
74  VTKM_ASSERT(buffers.size() == 1);
75  constexpr vtkm::BufferSizeType fillValueSize =
76  static_cast<vtkm::BufferSizeType>(sizeof(fillValue));
77  buffers[0].Fill(
78  &fillValue, fillValueSize, startIndex * fillValueSize, endIndex * fillValueSize, token);
79  }
80 
81  VTKM_CONT static ReadPortalType CreateReadPortal(
82  const std::vector<vtkm::cont::internal::Buffer>& buffers,
84  vtkm::cont::Token& token)
85  {
86  VTKM_ASSERT(buffers.size() == 1);
87  return ReadPortalType(reinterpret_cast<const T*>(buffers[0].ReadPointerDevice(device, token)),
88  GetNumberOfValues(buffers));
89  }
90 
91  VTKM_CONT static WritePortalType CreateWritePortal(
92  const std::vector<vtkm::cont::internal::Buffer>& buffers,
94  vtkm::cont::Token& token)
95  {
96  VTKM_ASSERT(buffers.size() == 1);
97  return WritePortalType(reinterpret_cast<T*>(buffers[0].WritePointerDevice(device, token)),
98  GetNumberOfValues(buffers));
99  }
100 };
101 
102 } // namespace internal
103 
110 template <typename T>
111 class VTKM_ALWAYS_EXPORT ArrayHandleBasic : public ArrayHandle<T, vtkm::cont::StorageTagBasic>
112 {
113 public:
117 
119  T* array,
120  vtkm::Id numberOfValues,
121  vtkm::cont::internal::BufferInfo::Deleter deleter,
122  vtkm::cont::internal::BufferInfo::Reallocater reallocater = internal::InvalidRealloc)
123  : Superclass(std::vector<vtkm::cont::internal::Buffer>{ vtkm::cont::internal::MakeBuffer(
125  array,
126  array,
127  vtkm::internal::NumberOfValuesToNumberOfBytes<T>(numberOfValues),
128  deleter,
129  reallocater) })
130  {
131  }
132 
134  T* array,
135  vtkm::Id numberOfValues,
137  vtkm::cont::internal::BufferInfo::Deleter deleter,
138  vtkm::cont::internal::BufferInfo::Reallocater reallocater = internal::InvalidRealloc)
139  : Superclass(std::vector<vtkm::cont::internal::Buffer>{ vtkm::cont::internal::MakeBuffer(
140  device,
141  array,
142  array,
143  vtkm::internal::NumberOfValuesToNumberOfBytes<T>(numberOfValues),
144  deleter,
145  reallocater) })
146  {
147  }
148 
150  T* array,
151  void* container,
152  vtkm::Id numberOfValues,
153  vtkm::cont::internal::BufferInfo::Deleter deleter,
154  vtkm::cont::internal::BufferInfo::Reallocater reallocater = internal::InvalidRealloc)
155  : Superclass(std::vector<vtkm::cont::internal::Buffer>{ vtkm::cont::internal::MakeBuffer(
157  array,
158  container,
159  vtkm::internal::NumberOfValuesToNumberOfBytes<T>(numberOfValues),
160  deleter,
161  reallocater) })
162  {
163  }
164 
166  T* array,
167  void* container,
168  vtkm::Id numberOfValues,
170  vtkm::cont::internal::BufferInfo::Deleter deleter,
171  vtkm::cont::internal::BufferInfo::Reallocater reallocater = internal::InvalidRealloc)
172  : Superclass(std::vector<vtkm::cont::internal::Buffer>{ vtkm::cont::internal::MakeBuffer(
173  device,
174  array,
175  container,
176  vtkm::internal::NumberOfValuesToNumberOfBytes<T>(numberOfValues),
177  deleter,
178  reallocater) })
179  {
180  }
181 
186  const T* GetReadPointer() const
187  {
188  vtkm::cont::Token token;
189  return this->GetReadPointer(token);
190  }
196  const T* GetReadPointer(vtkm::cont::Token& token) const
197  {
198  return reinterpret_cast<const T*>(this->GetBuffers()[0].ReadPointerHost(token));
199  }
204  T* GetWritePointer() const
205  {
206  vtkm::cont::Token token;
207  return this->GetWritePointer(token);
208  }
215  {
216  return reinterpret_cast<T*>(this->GetBuffers()[0].WritePointerHost(token));
217  }
218 
227  {
228  vtkm::cont::Token token;
229  return this->GetReadPointer(device, token);
230  }
239  {
240  return reinterpret_cast<const T*>(this->GetBuffers()[0].ReadPointerDevice(device, token));
241  }
250  {
251  vtkm::cont::Token token;
252  return this->GetWritePointer(device, token);
253  }
262  {
263  return reinterpret_cast<T*>(this->GetBuffers()[0].WritePointerDevice(device, token));
264  }
265 };
266 
269 template <typename T>
271  vtkm::Id numberOfValues,
272  vtkm::CopyFlag copy)
273 {
274  if (copy == vtkm::CopyFlag::On)
275  {
277  handle.Allocate(numberOfValues);
278  std::copy(
279  array, array + numberOfValues, vtkm::cont::ArrayPortalToIteratorBegin(handle.WritePortal()));
280  return handle;
281  }
282  else
283  {
284  return vtkm::cont::ArrayHandleBasic<T>(const_cast<T*>(array), numberOfValues, [](void*) {});
285  }
286 }
287 
293 template <typename T>
295  T*& array,
296  vtkm::Id numberOfValues,
297  vtkm::cont::internal::BufferInfo::Deleter deleter = internal::SimpleArrayDeleter<T>,
298  vtkm::cont::internal::BufferInfo::Reallocater reallocater = internal::SimpleArrayReallocater<T>)
299 {
300  vtkm::cont::ArrayHandleBasic<T> arrayHandle(array, numberOfValues, deleter, reallocater);
301  array = nullptr;
302  return arrayHandle;
303 }
304 
307 template <typename T, typename Allocator>
308 VTKM_CONT vtkm::cont::ArrayHandleBasic<T> make_ArrayHandle(const std::vector<T, Allocator>& array,
309  vtkm::CopyFlag copy)
310 {
311  if (!array.empty())
312  {
313  return make_ArrayHandle(array.data(), static_cast<vtkm::Id>(array.size()), copy);
314  }
315  else
316  {
317  // Vector empty. Just return an empty array handle.
319  }
320 }
321 
324 template <typename T, typename Allocator>
326 {
327  using vector_type = std::vector<T, Allocator>;
328  vector_type* container = new vector_type(std::move(array));
329  return vtkm::cont::ArrayHandleBasic<T>(container->data(),
330  container,
331  static_cast<vtkm::Id>(container->size()),
332  internal::StdVectorDeleter<T, Allocator>,
333  internal::StdVectorReallocater<T, Allocator>);
334 }
335 
338 template <typename T, typename Allocator>
341 {
342  return make_ArrayHandleMove(array);
343 }
344 
347 template <typename T>
349 {
350  return make_ArrayHandle(values.begin(), static_cast<vtkm::Id>(values.size()), vtkm::CopyFlag::On);
351 }
352 }
353 } // namespace vtkm::cont
354 
355 //=============================================================================
356 // Specializations of serialization related classes
358 namespace vtkm
359 {
360 namespace cont
361 {
362 
363 template <typename T>
364 struct SerializableTypeString<vtkm::cont::ArrayHandleBasic<T>>
365 {
366  static VTKM_CONT const std::string& Get()
367  {
368  static std::string name = "AH<" + SerializableTypeString<T>::Get() + ">";
369  return name;
370  }
371 };
372 
373 template <typename T>
374 struct SerializableTypeString<ArrayHandle<T, vtkm::cont::StorageTagBasic>>
375  : SerializableTypeString<vtkm::cont::ArrayHandleBasic<T>>
376 {
377 };
378 }
379 } // vtkm::cont
380 
381 namespace mangled_diy_namespace
382 {
383 
384 template <typename T>
385 struct Serialization<vtkm::cont::ArrayHandleBasic<T>>
386 {
387  static VTKM_CONT void save(BinaryBuffer& bb,
389  {
390  vtkmdiy::save(bb, obj.GetBuffers()[0]);
391  }
392 
393  static VTKM_CONT void load(BinaryBuffer& bb,
395  {
396  vtkm::cont::internal::Buffer buffer;
397  vtkmdiy::load(bb, buffer);
398 
400  vtkm::cont::internal::CreateBuffers(buffer));
401  }
402 };
403 
404 template <typename T>
405 struct Serialization<vtkm::cont::ArrayHandle<T, vtkm::cont::StorageTagBasic>>
406  : Serialization<vtkm::cont::ArrayHandleBasic<T>>
407 {
408 };
409 
410 } // diy
412 
413 #ifndef vtk_m_cont_ArrayHandleBasic_cxx
414 
417 
418 namespace vtkm
419 {
420 namespace cont
421 {
422 
423 namespace internal
424 {
425 
426 #define VTKM_STORAGE_EXPORT(Type) \
427  extern template class VTKM_CONT_TEMPLATE_EXPORT Storage<Type, StorageTagBasic>; \
428  extern template class VTKM_CONT_TEMPLATE_EXPORT Storage<vtkm::Vec<Type, 2>, StorageTagBasic>; \
429  extern template class VTKM_CONT_TEMPLATE_EXPORT Storage<vtkm::Vec<Type, 3>, StorageTagBasic>; \
430  extern template class VTKM_CONT_TEMPLATE_EXPORT Storage<vtkm::Vec<Type, 4>, StorageTagBasic>;
431 
432 VTKM_STORAGE_EXPORT(char)
433 VTKM_STORAGE_EXPORT(vtkm::Int8)
434 VTKM_STORAGE_EXPORT(vtkm::UInt8)
435 VTKM_STORAGE_EXPORT(vtkm::Int16)
436 VTKM_STORAGE_EXPORT(vtkm::UInt16)
437 VTKM_STORAGE_EXPORT(vtkm::Int32)
438 VTKM_STORAGE_EXPORT(vtkm::UInt32)
439 VTKM_STORAGE_EXPORT(vtkm::Int64)
440 VTKM_STORAGE_EXPORT(vtkm::UInt64)
441 VTKM_STORAGE_EXPORT(vtkm::Float32)
442 VTKM_STORAGE_EXPORT(vtkm::Float64)
443 
444 #undef VTKM_STORAGE_EXPORT
445 
446 } // namespace internal
447 
448 #define VTKM_ARRAYHANDLE_EXPORT(Type) \
449  extern template class VTKM_CONT_TEMPLATE_EXPORT ArrayHandle<Type, StorageTagBasic>; \
450  extern template class VTKM_CONT_TEMPLATE_EXPORT \
451  ArrayHandle<vtkm::Vec<Type, 2>, StorageTagBasic>; \
452  extern template class VTKM_CONT_TEMPLATE_EXPORT \
453  ArrayHandle<vtkm::Vec<Type, 3>, StorageTagBasic>; \
454  extern template class VTKM_CONT_TEMPLATE_EXPORT ArrayHandle<vtkm::Vec<Type, 4>, StorageTagBasic>;
455 
456 VTKM_ARRAYHANDLE_EXPORT(char)
457 VTKM_ARRAYHANDLE_EXPORT(vtkm::Int8)
458 VTKM_ARRAYHANDLE_EXPORT(vtkm::UInt8)
459 VTKM_ARRAYHANDLE_EXPORT(vtkm::Int16)
460 VTKM_ARRAYHANDLE_EXPORT(vtkm::UInt16)
461 VTKM_ARRAYHANDLE_EXPORT(vtkm::Int32)
462 VTKM_ARRAYHANDLE_EXPORT(vtkm::UInt32)
463 VTKM_ARRAYHANDLE_EXPORT(vtkm::Int64)
464 VTKM_ARRAYHANDLE_EXPORT(vtkm::UInt64)
465 VTKM_ARRAYHANDLE_EXPORT(vtkm::Float32)
466 VTKM_ARRAYHANDLE_EXPORT(vtkm::Float64)
467 
468 #undef VTKM_ARRAYHANDLE_EXPORT
469 }
470 } // end vtkm::cont
471 
473 
474 #endif // !vtk_m_cont_ArrayHandleBasic_cxx
475 
476 #endif //vtk_m_cont_ArrayHandleBasic_h
VecFlat.h
vtkm::cont::ArrayHandleBasic::Superclass
typename vtkm::cont::detail::GetTypeInParentheses< void(ArrayHandle< T, vtkm::cont::StorageTagBasic >) >::type Superclass
Definition: ArrayHandleBasic.h:116
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:300
ArrayHandle.h
vtkm::cont::ArrayHandleBasic::GetReadPointer
const T * GetReadPointer() const
Gets raw access to the ArrayHandle's data.
Definition: ArrayHandleBasic.h:186
vtkm::cont::ArrayHandle::GetBuffers
const std::vector< vtkm::cont::internal::Buffer > & GetBuffers() const
Returns the internal Buffer structures that hold the data.
Definition: ArrayHandle.h:721
vtkm::exec::arg::load
T load(const U &u, vtkm::Id v)
Definition: FetchTagArrayDirectIn.h:36
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::Get
auto Get(const vtkm::Tuple< Ts... > &tuple)
Retrieve the object from a vtkm::Tuple at the given index.
Definition: Tuple.h:81
VTKM_ARRAY_HANDLE_SUBCLASS
#define VTKM_ARRAY_HANDLE_SUBCLASS(classname, fullclasstype, superclass)
Macro to make default methods in ArrayHandle subclasses.
Definition: ArrayHandle.h:243
VTKM_ASSERT
#define VTKM_ASSERT(condition)
Definition: Assert.h:43
vtkm::IdComponent
vtkm::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:194
ArrayPortalToIterators.h
vtkm::cont::ArrayHandleBasic::GetWritePointer
T * GetWritePointer(vtkm::cont::DeviceAdapterId device) const
Gets raw write access to the ArrayHandle's data.
Definition: ArrayHandleBasic.h:249
vtkm::cont::ArrayHandleBasic::GetReadPointer
const T * GetReadPointer(vtkm::cont::DeviceAdapterId device) const
Gets raw access to the ArrayHandle's data on a particular device.
Definition: ArrayHandleBasic.h:226
Storage.h
vtkm::Int16
int16_t Int16
Base type to use for 16-bit signed integer numbers.
Definition: Types.h:173
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:165
vtkm::cont::ArrayHandleBasic::GetReadPointer
const T * GetReadPointer(vtkm::cont::Token &token) const
Gets raw access to the ArrayHandle's data.
Definition: ArrayHandleBasic.h:196
vtkm::cont::make_ArrayHandleMove
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:294
SerializableTypeString.h
vtkm::BufferSizeType
vtkm::Int64 BufferSizeType
Definition: DeviceAdapterMemoryManager.h:27
vtkm::cont::ArrayHandleBasic::GetWritePointer
T * GetWritePointer(vtkm::cont::Token &token) const
Gets raw write access to the ArrayHandle's data.
Definition: ArrayHandleBasic.h:214
mangled_diy_namespace
Definition: Particle.h:351
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:133
vtkm::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
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:149
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:238
vtkm::Int8
int8_t Int8
Base type to use for 8-bit signed integer numbers.
Definition: Types.h:165
vtkm::cont::ArrayHandleBasic::GetWritePointer
T * GetWritePointer(vtkm::cont::DeviceAdapterId device, vtkm::cont::Token &token) const
Gets raw write access to the ArrayHandle's data.
Definition: ArrayHandleBasic.h:261
Serialization.h
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::Id
vtkm::Int64 Id
Base type to use to index arrays.
Definition: Types.h:227
vtkm::Int64
signed long long Int64
Base type to use for 64-bit signed integer numbers.
Definition: Types.h:204
vtkm::VecFlat
Treat a Vec or Vec-like object as a flat Vec.
Definition: VecFlat.h:224
vtkm::UInt8
uint8_t UInt8
Base type to use for 8-bit unsigned integer numbers.
Definition: Types.h:169
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:118
vtkm::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:58
vtkm::cont::ArrayHandleBasic::GetWritePointer
T * GetWritePointer() const
Gets raw write access to the ArrayHandle's data.
Definition: ArrayHandleBasic.h:204
vtkm::UInt32
uint32_t UInt32
Base type to use for 32-bit unsigned integer numbers.
Definition: Types.h:185
vtkm::cont::DeviceAdapterTagUndefined
Tag for a device adapter used to avoid specifying a device.
Definition: DeviceAdapterTag.h:187
vtkm::Float32
float Float32
Base type to use for 32-bit floating-point numbers.
Definition: Types.h:157
vtkm::UInt64
unsigned long long UInt64
Base type to use for 64-bit signed integer numbers.
Definition: Types.h:207
vtkm::cont::ArrayPortalToIteratorBegin
vtkm::cont::ArrayPortalToIterators< PortalType >::IteratorType ArrayPortalToIteratorBegin(const PortalType &portal)
Convenience function for converting an ArrayPortal to a begin iterator.
Definition: ArrayPortalToIterators.h:178
vtkm::Int32
int32_t Int32
Base type to use for 32-bit signed integer numbers.
Definition: Types.h:181
vtkm::Float64
double Float64
Base type to use for 64-bit floating-point numbers.
Definition: Types.h:161
vtkm::CopyFlag
CopyFlag
Identifier used to specify whether a function should deep copy data.
Definition: Flags.h:17
vtkm::cont::ArrayHandle< T, vtkm::cont::StorageTagBasic >::Allocate
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:490
vtkm::cont::ArrayHandleBasic
Basic array storage for an array handle.
Definition: ArrayHandleBasic.h:111
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:89
vtkm::cont::make_ArrayHandle
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:270
vtkm::UInt16
uint16_t UInt16
Base type to use for 16-bit unsigned integer numbers.
Definition: Types.h:177
vtkm::cont::ArrayHandle< T, vtkm::cont::StorageTagBasic >::WritePortal
WritePortalType WritePortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:454