Viskores  1.0
ArrayHandleBasic.h
Go to the documentation of this file.
1 //============================================================================
2 // The contents of this file are covered by the Viskores license. See
3 // LICENSE.txt for details.
4 //
5 // By contributing to this file, all contributors agree to the Developer
6 // Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
7 //============================================================================
8 
9 //============================================================================
10 // Copyright (c) Kitware, Inc.
11 // All rights reserved.
12 // See LICENSE.txt for details.
13 //
14 // This software is distributed WITHOUT ANY WARRANTY; without even
15 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 // PURPOSE. See the above copyright notice for more information.
17 //============================================================================
18 #ifndef viskores_cont_ArrayHandleBasic_h
19 #define viskores_cont_ArrayHandleBasic_h
20 
25 #include <viskores/cont/Storage.h>
26 
28 
29 #include <viskores/VecFlat.h>
30 
31 #include <limits>
32 
33 namespace viskores
34 {
35 namespace cont
36 {
37 
38 namespace internal
39 {
40 
41 template <typename T>
42 class VISKORES_ALWAYS_EXPORT Storage<T, viskores::cont::StorageTagBasic>
43 {
44 public:
45  using ReadPortalType = viskores::internal::ArrayPortalBasicRead<T>;
46  using WritePortalType = viskores::internal::ArrayPortalBasicWrite<T>;
47 
48  VISKORES_CONT static std::vector<viskores::cont::internal::Buffer> CreateBuffers()
49  {
50  return std::vector<viskores::cont::internal::Buffer>(1);
51  }
52 
53  VISKORES_CONT static void ResizeBuffers(
54  viskores::Id numValues,
55  const std::vector<viskores::cont::internal::Buffer>& buffers,
56  viskores::CopyFlag preserve,
57  viskores::cont::Token& token)
58  {
59  buffers[0].SetNumberOfBytes(
60  viskores::internal::NumberOfValuesToNumberOfBytes<T>(numValues), preserve, token);
61  }
62 
63  VISKORES_CONT static viskores::IdComponent GetNumberOfComponentsFlat(
64  const std::vector<viskores::cont::internal::Buffer>&)
65  {
67  }
68 
69  VISKORES_CONT static viskores::Id GetNumberOfValues(
70  const std::vector<viskores::cont::internal::Buffer>& buffers)
71  {
72  VISKORES_ASSERT(buffers.size() == 1);
73  return static_cast<viskores::Id>(buffers[0].GetNumberOfBytes() /
74  static_cast<viskores::BufferSizeType>(sizeof(T)));
75  }
76 
77  VISKORES_CONT static void Fill(const std::vector<viskores::cont::internal::Buffer>& buffers,
78  const T& fillValue,
79  viskores::Id startIndex,
80  viskores::Id endIndex,
81  viskores::cont::Token& token)
82  {
83  VISKORES_ASSERT(buffers.size() == 1);
84  constexpr viskores::BufferSizeType fillValueSize =
85  static_cast<viskores::BufferSizeType>(sizeof(fillValue));
86  buffers[0].Fill(
87  &fillValue, fillValueSize, startIndex * fillValueSize, endIndex * fillValueSize, token);
88  }
89 
90  VISKORES_CONT static ReadPortalType CreateReadPortal(
91  const std::vector<viskores::cont::internal::Buffer>& buffers,
93  viskores::cont::Token& token)
94  {
95  VISKORES_ASSERT(buffers.size() == 1);
96  return ReadPortalType(reinterpret_cast<const T*>(buffers[0].ReadPointerDevice(device, token)),
97  GetNumberOfValues(buffers));
98  }
99 
100  VISKORES_CONT static WritePortalType CreateWritePortal(
101  const std::vector<viskores::cont::internal::Buffer>& buffers,
103  viskores::cont::Token& token)
104  {
105  VISKORES_ASSERT(buffers.size() == 1);
106  return WritePortalType(reinterpret_cast<T*>(buffers[0].WritePointerDevice(device, token)),
107  GetNumberOfValues(buffers));
108  }
109 };
110 
111 } // namespace internal
112 
119 template <typename T>
120 class VISKORES_ALWAYS_EXPORT ArrayHandleBasic
121  : public ArrayHandle<T, viskores::cont::StorageTagBasic>
122 {
123 public:
127 
129  T* array,
130  viskores::Id numberOfValues,
131  viskores::cont::internal::BufferInfo::Deleter deleter,
132  viskores::cont::internal::BufferInfo::Reallocater reallocater = internal::InvalidRealloc)
133  : Superclass(
134  std::vector<viskores::cont::internal::Buffer>{ viskores::cont::internal::MakeBuffer(
136  array,
137  array,
138  viskores::internal::NumberOfValuesToNumberOfBytes<T>(numberOfValues),
139  deleter,
140  reallocater) })
141  {
142  }
143 
145  T* array,
146  viskores::Id numberOfValues,
148  viskores::cont::internal::BufferInfo::Deleter deleter,
149  viskores::cont::internal::BufferInfo::Reallocater reallocater = internal::InvalidRealloc)
150  : Superclass(
151  std::vector<viskores::cont::internal::Buffer>{ viskores::cont::internal::MakeBuffer(
152  device,
153  array,
154  array,
155  viskores::internal::NumberOfValuesToNumberOfBytes<T>(numberOfValues),
156  deleter,
157  reallocater) })
158  {
159  }
160 
162  T* array,
163  void* container,
164  viskores::Id numberOfValues,
165  viskores::cont::internal::BufferInfo::Deleter deleter,
166  viskores::cont::internal::BufferInfo::Reallocater reallocater = internal::InvalidRealloc)
167  : Superclass(
168  std::vector<viskores::cont::internal::Buffer>{ viskores::cont::internal::MakeBuffer(
170  array,
171  container,
172  viskores::internal::NumberOfValuesToNumberOfBytes<T>(numberOfValues),
173  deleter,
174  reallocater) })
175  {
176  }
177 
179  T* array,
180  void* container,
181  viskores::Id numberOfValues,
183  viskores::cont::internal::BufferInfo::Deleter deleter,
184  viskores::cont::internal::BufferInfo::Reallocater reallocater = internal::InvalidRealloc)
185  : Superclass(
186  std::vector<viskores::cont::internal::Buffer>{ viskores::cont::internal::MakeBuffer(
187  device,
188  array,
189  container,
190  viskores::internal::NumberOfValuesToNumberOfBytes<T>(numberOfValues),
191  deleter,
192  reallocater) })
193  {
194  }
195 
200  const T* GetReadPointer() const
201  {
202  viskores::cont::Token token;
203  return this->GetReadPointer(token);
204  }
210  const T* GetReadPointer(viskores::cont::Token& token) const
211  {
212  return reinterpret_cast<const T*>(this->GetBuffers()[0].ReadPointerHost(token));
213  }
218  T* GetWritePointer() const
219  {
220  viskores::cont::Token token;
221  return this->GetWritePointer(token);
222  }
229  {
230  return reinterpret_cast<T*>(this->GetBuffers()[0].WritePointerHost(token));
231  }
232 
241  {
242  viskores::cont::Token token;
243  return this->GetReadPointer(device, token);
244  }
253  viskores::cont::Token& token) const
254  {
255  return reinterpret_cast<const T*>(this->GetBuffers()[0].ReadPointerDevice(device, token));
256  }
265  {
266  viskores::cont::Token token;
267  return this->GetWritePointer(device, token);
268  }
277  {
278  return reinterpret_cast<T*>(this->GetBuffers()[0].WritePointerDevice(device, token));
279  }
280 };
281 
284 template <typename T>
286  viskores::Id numberOfValues,
287  viskores::CopyFlag copy)
288 {
289  if (copy == viskores::CopyFlag::On)
290  {
292  handle.Allocate(numberOfValues);
293  std::copy(array,
294  array + numberOfValues,
296  return handle;
297  }
298  else
299  {
300  return viskores::cont::ArrayHandleBasic<T>(const_cast<T*>(array), numberOfValues, [](void*) {});
301  }
302 }
303 
309 template <typename T>
311  T*& array,
312  viskores::Id numberOfValues,
313  viskores::cont::internal::BufferInfo::Deleter deleter = internal::SimpleArrayDeleter<T>,
314  viskores::cont::internal::BufferInfo::Reallocater reallocater =
315  internal::SimpleArrayReallocater<T>)
316 {
317  viskores::cont::ArrayHandleBasic<T> arrayHandle(array, numberOfValues, deleter, reallocater);
318  array = nullptr;
319  return arrayHandle;
320 }
321 
324 template <typename T, typename Allocator>
326  const std::vector<T, Allocator>& array,
327  viskores::CopyFlag copy)
328 {
329  if (!array.empty())
330  {
331  return make_ArrayHandle(array.data(), static_cast<viskores::Id>(array.size()), copy);
332  }
333  else
334  {
335  // Vector empty. Just return an empty array handle.
337  }
338 }
339 
342 template <typename T, typename Allocator>
344  std::vector<T, Allocator>&& array)
345 {
346  using vector_type = std::vector<T, Allocator>;
347  vector_type* container = new vector_type(std::move(array));
348  return viskores::cont::ArrayHandleBasic<T>(container->data(),
349  container,
350  static_cast<viskores::Id>(container->size()),
351  internal::StdVectorDeleter<T, Allocator>,
352  internal::StdVectorReallocater<T, Allocator>);
353 }
354 
357 template <typename T, typename Allocator>
359  std::vector<T, Allocator>&& array,
361 {
362  return make_ArrayHandleMove(array);
363 }
364 
367 template <typename T>
369  std::initializer_list<T>&& values)
370 {
371  return make_ArrayHandle(
372  values.begin(), static_cast<viskores::Id>(values.size()), viskores::CopyFlag::On);
373 }
374 }
375 } // namespace viskores::cont
376 
377 //=============================================================================
378 // Specializations of serialization related classes
380 namespace viskores
381 {
382 namespace cont
383 {
384 
385 template <typename T>
386 struct SerializableTypeString<viskores::cont::ArrayHandleBasic<T>>
387 {
388  static VISKORES_CONT const std::string& Get()
389  {
390  static std::string name = "AH<" + SerializableTypeString<T>::Get() + ">";
391  return name;
392  }
393 };
394 
395 template <typename T>
396 struct SerializableTypeString<ArrayHandle<T, viskores::cont::StorageTagBasic>>
397  : SerializableTypeString<viskores::cont::ArrayHandleBasic<T>>
398 {
399 };
400 }
401 } // viskores::cont
402 
403 namespace mangled_diy_namespace
404 {
405 
406 template <typename T>
407 struct Serialization<viskores::cont::ArrayHandleBasic<T>>
408 {
409  static VISKORES_CONT void save(
410  BinaryBuffer& bb,
412  {
413  viskoresdiy::save(bb, obj.GetBuffers()[0]);
414  }
415 
416  static VISKORES_CONT void load(
417  BinaryBuffer& bb,
419  {
420  viskores::cont::internal::Buffer buffer;
421  viskoresdiy::load(bb, buffer);
422 
424  viskores::cont::internal::CreateBuffers(buffer));
425  }
426 };
427 
428 template <typename T>
429 struct Serialization<viskores::cont::ArrayHandle<T, viskores::cont::StorageTagBasic>>
430  : Serialization<viskores::cont::ArrayHandleBasic<T>>
431 {
432 };
433 
434 } // diy
436 
437 #ifndef viskores_cont_ArrayHandleBasic_cxx
438 
441 
442 namespace viskores
443 {
444 namespace cont
445 {
446 
447 namespace internal
448 {
449 
450 #define VISKORES_STORAGE_EXPORT(Type) \
451  extern template class VISKORES_CONT_TEMPLATE_EXPORT Storage<Type, StorageTagBasic>; \
452  extern template class VISKORES_CONT_TEMPLATE_EXPORT \
453  Storage<viskores::Vec<Type, 2>, StorageTagBasic>; \
454  extern template class VISKORES_CONT_TEMPLATE_EXPORT \
455  Storage<viskores::Vec<Type, 3>, StorageTagBasic>; \
456  extern template class VISKORES_CONT_TEMPLATE_EXPORT \
457  Storage<viskores::Vec<Type, 4>, StorageTagBasic>;
458 
459 VISKORES_STORAGE_EXPORT(char)
460 VISKORES_STORAGE_EXPORT(viskores::Int8)
461 VISKORES_STORAGE_EXPORT(viskores::UInt8)
462 VISKORES_STORAGE_EXPORT(viskores::Int16)
463 VISKORES_STORAGE_EXPORT(viskores::UInt16)
464 VISKORES_STORAGE_EXPORT(viskores::Int32)
465 VISKORES_STORAGE_EXPORT(viskores::UInt32)
466 VISKORES_STORAGE_EXPORT(viskores::Int64)
467 VISKORES_STORAGE_EXPORT(viskores::UInt64)
468 VISKORES_STORAGE_EXPORT(viskores::Float32)
469 VISKORES_STORAGE_EXPORT(viskores::Float64)
470 
471 #undef VISKORES_STORAGE_EXPORT
472 
473 } // namespace internal
474 
475 #define VISKORES_ARRAYHANDLE_EXPORT(Type) \
476  extern template class VISKORES_CONT_TEMPLATE_EXPORT ArrayHandle<Type, StorageTagBasic>; \
477  extern template class VISKORES_CONT_TEMPLATE_EXPORT \
478  ArrayHandle<viskores::Vec<Type, 2>, StorageTagBasic>; \
479  extern template class VISKORES_CONT_TEMPLATE_EXPORT \
480  ArrayHandle<viskores::Vec<Type, 3>, StorageTagBasic>; \
481  extern template class VISKORES_CONT_TEMPLATE_EXPORT \
482  ArrayHandle<viskores::Vec<Type, 4>, StorageTagBasic>;
483 
484 VISKORES_ARRAYHANDLE_EXPORT(char)
485 VISKORES_ARRAYHANDLE_EXPORT(viskores::Int8)
486 VISKORES_ARRAYHANDLE_EXPORT(viskores::UInt8)
487 VISKORES_ARRAYHANDLE_EXPORT(viskores::Int16)
488 VISKORES_ARRAYHANDLE_EXPORT(viskores::UInt16)
489 VISKORES_ARRAYHANDLE_EXPORT(viskores::Int32)
490 VISKORES_ARRAYHANDLE_EXPORT(viskores::UInt32)
491 VISKORES_ARRAYHANDLE_EXPORT(viskores::Int64)
492 VISKORES_ARRAYHANDLE_EXPORT(viskores::UInt64)
493 VISKORES_ARRAYHANDLE_EXPORT(viskores::Float32)
494 VISKORES_ARRAYHANDLE_EXPORT(viskores::Float64)
495 
496 #undef VISKORES_ARRAYHANDLE_EXPORT
497 }
498 } // end viskores::cont
499 
501 
502 #endif // !viskores_cont_ArrayHandleBasic_cxx
503 
504 #endif //viskores_cont_ArrayHandleBasic_h
viskores::exec::arg::load
T load(const U &u, viskores::Id v)
Definition: FetchTagArrayDirectIn.h:44
VecFlat.h
viskores::VecFlat
Treat a Vec or Vec-like object as a flat Vec.
Definition: VecFlat.h:240
ArrayHandle.h
viskores::Int16
int16_t Int16
Base type to use for 16-bit signed integer numbers.
Definition: Types.h:181
viskores::cont::ArrayHandleBasic::ArrayHandleBasic
ArrayHandleBasic(T *array, void *container, viskores::Id numberOfValues, viskores::cont::DeviceAdapterId device, viskores::cont::internal::BufferInfo::Deleter deleter, viskores::cont::internal::BufferInfo::Reallocater reallocater=internal::InvalidRealloc)
Definition: ArrayHandleBasic.h:178
ArrayPortalToIterators.h
viskores::Int8
int8_t Int8
Base type to use for 8-bit signed integer numbers.
Definition: Types.h:173
viskoresNotUsed
#define viskoresNotUsed(parameter_name)
Simple macro to identify a parameter as unused.
Definition: ExportMacros.h:136
viskores::cont::ArrayHandleBasic::GetReadPointer
const T * GetReadPointer() const
Gets raw access to the ArrayHandle's data.
Definition: ArrayHandleBasic.h:200
Storage.h
viskores::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:313
viskores::cont::ArrayHandleBasic::GetReadPointer
const T * GetReadPointer(viskores::cont::DeviceAdapterId device, viskores::cont::Token &token) const
Gets raw access to the ArrayHandle's data.
Definition: ArrayHandleBasic.h:252
viskores::UInt16
uint16_t UInt16
Base type to use for 16-bit unsigned integer numbers.
Definition: Types.h:185
viskores::cont::ArrayHandleBasic::Superclass
typename viskores::cont::detail::GetTypeInParentheses< void(ArrayHandle< T, viskores::cont::StorageTagBasic >) >::type Superclass
Definition: ArrayHandleBasic.h:126
viskores::IdComponent
viskores::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:202
SerializableTypeString.h
viskores::cont::ArrayHandleBasic::GetReadPointer
const T * GetReadPointer(viskores::cont::DeviceAdapterId device) const
Gets raw access to the ArrayHandle's data on a particular device.
Definition: ArrayHandleBasic.h:240
viskores::cont::ArrayHandleBasic::GetReadPointer
const T * GetReadPointer(viskores::cont::Token &token) const
Gets raw access to the ArrayHandle's data.
Definition: ArrayHandleBasic.h:210
viskores::cont::ArrayHandleBasic::GetWritePointer
T * GetWritePointer() const
Gets raw write access to the ArrayHandle's data.
Definition: ArrayHandleBasic.h:218
viskores::cont::make_ArrayHandleMove
viskores::cont::ArrayHandleBasic< T > make_ArrayHandleMove(T *&array, viskores::Id numberOfValues, viskores::cont::internal::BufferInfo::Deleter deleter=internal::SimpleArrayDeleter< T >, viskores::cont::internal::BufferInfo::Reallocater reallocater=internal::SimpleArrayReallocater< T >)
A convenience function to move a user-allocated array into an ArrayHandle.
Definition: ArrayHandleBasic.h:310
mangled_diy_namespace
Definition: Particle.h:373
viskores::cont::ArrayHandleBasic::ArrayHandleBasic
ArrayHandleBasic(T *array, void *container, viskores::Id numberOfValues, viskores::cont::internal::BufferInfo::Deleter deleter, viskores::cont::internal::BufferInfo::Reallocater reallocater=internal::InvalidRealloc)
Definition: ArrayHandleBasic.h:161
viskores::Int64
signed long long Int64
Base type to use for 64-bit signed integer numbers.
Definition: Types.h:212
viskores::Id
viskores::Int64 Id
Base type to use to index arrays.
Definition: Types.h:235
ArrayPortalBasic.h
VISKORES_CONT
#define VISKORES_CONT
Definition: ExportMacros.h:65
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
viskores::CopyFlag::On
@ On
viskores::Float32
float Float32
Base type to use for 32-bit floating-point numbers.
Definition: Types.h:165
viskores::cont::ArrayPortalToIteratorBegin
viskores::cont::ArrayPortalToIterators< PortalType >::IteratorType ArrayPortalToIteratorBegin(const PortalType &portal)
Convenience function for converting an ArrayPortal to a begin iterator.
Definition: ArrayPortalToIterators.h:189
Serialization.h
viskores::cont::ArrayHandle< T, viskores::cont::StorageTagBasic >::Allocate
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::cont::ArrayHandleBasic
Basic array storage for an array handle.
Definition: ArrayHandleBasic.h:120
viskores::cont::DeviceAdapterTagUndefined
Tag for a device adapter used to avoid specifying a device.
Definition: DeviceAdapterTag.h:201
VISKORES_ASSERT
#define VISKORES_ASSERT(condition)
Definition: Assert.h:51
viskores::cont::ArrayHandle< T, viskores::cont::StorageTagBasic >::WritePortal
WritePortalType WritePortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:468
viskores::BufferSizeType
viskores::Int64 BufferSizeType
Definition: DeviceAdapterMemoryManager.h:35
viskores::cont::ArrayHandleBasic::ArrayHandleBasic
ArrayHandleBasic(T *array, viskores::Id numberOfValues, viskores::cont::DeviceAdapterId device, viskores::cont::internal::BufferInfo::Deleter deleter, viskores::cont::internal::BufferInfo::Reallocater reallocater=internal::InvalidRealloc)
Definition: ArrayHandleBasic.h:144
viskores::UInt64
unsigned long long UInt64
Base type to use for 64-bit signed integer numbers.
Definition: Types.h:215
viskores::UInt8
uint8_t UInt8
Base type to use for 8-bit unsigned integer numbers.
Definition: Types.h:177
viskores::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:66
viskores::Int32
int32_t Int32
Base type to use for 32-bit signed integer numbers.
Definition: Types.h:189
viskores::cont::ArrayHandleBasic::GetWritePointer
T * GetWritePointer(viskores::cont::DeviceAdapterId device) const
Gets raw write access to the ArrayHandle's data.
Definition: ArrayHandleBasic.h:264
viskores::cont::ArrayHandleBasic::GetWritePointer
T * GetWritePointer(viskores::cont::DeviceAdapterId device, viskores::cont::Token &token) const
Gets raw write access to the ArrayHandle's data.
Definition: ArrayHandleBasic.h:276
VISKORES_ARRAY_HANDLE_SUBCLASS
#define VISKORES_ARRAY_HANDLE_SUBCLASS(classname, fullclasstype, superclass)
Macro to make default methods in ArrayHandle subclasses.
Definition: ArrayHandle.h:256
viskores::cont::ArrayHandleBasic::GetWritePointer
T * GetWritePointer(viskores::cont::Token &token) const
Gets raw write access to the ArrayHandle's data.
Definition: ArrayHandleBasic.h:228
viskores::Get
auto Get(const viskores::Tuple< Ts... > &tuple)
Retrieve the object from a viskores::Tuple at the given index.
Definition: Tuple.h:89
viskores::cont::ArrayHandleBasic::ArrayHandleBasic
ArrayHandleBasic(T *array, viskores::Id numberOfValues, viskores::cont::internal::BufferInfo::Deleter deleter, viskores::cont::internal::BufferInfo::Reallocater reallocater=internal::InvalidRealloc)
Definition: ArrayHandleBasic.h:128
viskores::CopyFlag
CopyFlag
Identifier used to specify whether a function should deep copy data.
Definition: Flags.h:25
viskores::Float64
double Float64
Base type to use for 64-bit floating-point numbers.
Definition: Types.h:169
viskores::cont::make_ArrayHandle
viskores::cont::ArrayHandleBasic< T > make_ArrayHandle(const T *array, viskores::Id numberOfValues, viskores::CopyFlag copy)
A convenience function for creating an ArrayHandle from a standard C array.
Definition: ArrayHandleBasic.h:285
viskores::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:43
viskores::cont::ArrayHandle::GetBuffers
const std::vector< viskores::cont::internal::Buffer > & GetBuffers() const
Returns the internal Buffer structures that hold the data.
Definition: ArrayHandle.h:738
viskores::UInt32
uint32_t UInt32
Base type to use for 32-bit unsigned integer numbers.
Definition: Types.h:193