VTK-m  2.2
ArrayHandleGroupVecVariable.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_ArrayHandleGroupVecVariable_h
11 #define vtk_m_cont_ArrayHandleGroupVecVariable_h
12 
13 #include <vtkm/cont/ArrayHandle.h>
15 #include <vtkm/cont/ArrayPortal.h>
17 
18 #include <vtkm/Assert.h>
19 #include <vtkm/VecFromPortal.h>
20 
21 namespace vtkm
22 {
23 namespace internal
24 {
25 
26 template <typename ComponentsPortalType, typename OffsetsPortalType>
27 class VTKM_ALWAYS_EXPORT ArrayPortalGroupVecVariable
28 {
29 public:
30  using ComponentType = typename std::remove_const<typename ComponentsPortalType::ValueType>::type;
32 
35  ArrayPortalGroupVecVariable()
36  : ComponentsPortal()
37  , OffsetsPortal()
38  {
39  }
40 
43  ArrayPortalGroupVecVariable(const ComponentsPortalType& componentsPortal,
44  const OffsetsPortalType& offsetsPortal)
45  : ComponentsPortal(componentsPortal)
46  , OffsetsPortal(offsetsPortal)
47  {
48  }
49 
54  template <typename OtherComponentsPortalType, typename OtherOffsetsPortalType>
55  VTKM_EXEC_CONT ArrayPortalGroupVecVariable(
56  const ArrayPortalGroupVecVariable<OtherComponentsPortalType, OtherOffsetsPortalType>& src)
57  : ComponentsPortal(src.GetComponentsPortal())
58  , OffsetsPortal(src.GetOffsetsPortal())
59  {
60  }
61 
64  vtkm::Id GetNumberOfValues() const { return this->OffsetsPortal.GetNumberOfValues() - 1; }
65 
68  ValueType Get(vtkm::Id index) const
69  {
70  vtkm::Id offsetIndex = this->OffsetsPortal.Get(index);
71  vtkm::Id nextOffsetIndex = this->OffsetsPortal.Get(index + 1);
72 
73  return ValueType(this->ComponentsPortal,
74  static_cast<vtkm::IdComponent>(nextOffsetIndex - offsetIndex),
75  offsetIndex);
76  }
77 
80  void Set(vtkm::Id index, const ValueType& value) const
81  {
82  if ((&value.GetPortal() == &this->ComponentsPortal) &&
83  (value.GetOffset() == this->OffsetsPortal.Get(index)))
84  {
85  // The ValueType (VecFromPortal) operates on demand. Thus, if you set
86  // something in the value, it has already been passed to the array.
87  }
88  else
89  {
90  // The value comes from somewhere else. Copy data in.
91  this->Get(index) = value;
92  }
93  }
94 
97  const ComponentsPortalType& GetComponentsPortal() const { return this->ComponentsPortal; }
98 
101  const OffsetsPortalType& GetOffsetsPortal() const { return this->OffsetsPortal; }
102 
103 private:
104  ComponentsPortalType ComponentsPortal;
105  OffsetsPortalType OffsetsPortal;
106 };
107 
108 }
109 } // namespace vtkm::internal
110 
111 namespace vtkm
112 {
113 namespace cont
114 {
115 
116 template <typename ComponentsStorageTag, typename OffsetsStorageTag>
118 {
119 };
120 
121 namespace internal
122 {
123 
124 template <typename ComponentsPortal, typename ComponentsStorageTag, typename OffsetsStorageTag>
125 class Storage<vtkm::VecFromPortal<ComponentsPortal>,
126  vtkm::cont::StorageTagGroupVecVariable<ComponentsStorageTag, OffsetsStorageTag>>
127 {
128  using ComponentType = typename ComponentsPortal::ValueType;
129  using ComponentsStorage = vtkm::cont::internal::Storage<ComponentType, ComponentsStorageTag>;
130  using OffsetsStorage = vtkm::cont::internal::Storage<vtkm::Id, OffsetsStorageTag>;
131 
134 
136  (std::is_same<ComponentsPortal, typename ComponentsStorage::WritePortalType>::value),
137  "Used invalid ComponentsPortal type with expected ComponentsStorageTag.");
138 
139  struct Info
140  {
141  std::size_t OffsetsBuffersOffset;
142  };
143 
144  VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> ComponentsBuffers(
145  const std::vector<vtkm::cont::internal::Buffer>& buffers)
146  {
147  Info info = buffers[0].GetMetaData<Info>();
148  return std::vector<vtkm::cont::internal::Buffer>(buffers.begin() + 1,
149  buffers.begin() + info.OffsetsBuffersOffset);
150  }
151 
152  VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> OffsetsBuffers(
153  const std::vector<vtkm::cont::internal::Buffer> buffers)
154  {
155  Info info = buffers[0].GetMetaData<Info>();
156  return std::vector<vtkm::cont::internal::Buffer>(buffers.begin() + info.OffsetsBuffersOffset,
157  buffers.end());
158  }
159 
160 public:
162 
163  using ReadPortalType =
164  vtkm::internal::ArrayPortalGroupVecVariable<typename ComponentsStorage::ReadPortalType,
165  typename OffsetsStorage::ReadPortalType>;
166  using WritePortalType =
167  vtkm::internal::ArrayPortalGroupVecVariable<typename ComponentsStorage::WritePortalType,
168  typename OffsetsStorage::ReadPortalType>;
169 
170  VTKM_CONT static vtkm::IdComponent GetNumberOfComponentsFlat(
171  const std::vector<vtkm::cont::internal::Buffer>&)
172  {
173  // Number of components can be variable.
174  return 0;
175  }
176 
177  VTKM_CONT static vtkm::Id GetNumberOfValues(
178  const std::vector<vtkm::cont::internal::Buffer>& buffers)
179  {
180  return OffsetsStorage::GetNumberOfValues(OffsetsBuffers(buffers)) - 1;
181  }
182 
183  VTKM_CONT static void Fill(const std::vector<vtkm::cont::internal::Buffer>&,
185  vtkm::Id,
186  vtkm::Id,
188  {
189  throw vtkm::cont::ErrorBadType("Fill not supported for ArrayHandleGroupVecVariable.");
190  }
191 
192  VTKM_CONT static ReadPortalType CreateReadPortal(
193  const std::vector<vtkm::cont::internal::Buffer>& buffers,
195  vtkm::cont::Token& token)
196  {
197  return ReadPortalType(
198  ComponentsStorage::CreateReadPortal(ComponentsBuffers(buffers), device, token),
199  OffsetsStorage::CreateReadPortal(OffsetsBuffers(buffers), device, token));
200  }
201 
202  VTKM_CONT static WritePortalType CreateWritePortal(
203  const std::vector<vtkm::cont::internal::Buffer>& buffers,
205  vtkm::cont::Token& token)
206  {
207  return WritePortalType(
208  ComponentsStorage::CreateWritePortal(ComponentsBuffers(buffers), device, token),
209  OffsetsStorage::CreateReadPortal(OffsetsBuffers(buffers), device, token));
210  }
211 
212  VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers(
213  const ComponentsArray& componentsArray = ComponentsArray{},
214  const OffsetsArray& offsetsArray = OffsetsArray{})
215  {
216  Info info;
217  info.OffsetsBuffersOffset = 1 + componentsArray.GetBuffers().size();
218  return vtkm::cont::internal::CreateBuffers(info, componentsArray, offsetsArray);
219  }
220 
221  VTKM_CONT static ComponentsArray GetComponentsArray(
222  const std::vector<vtkm::cont::internal::Buffer>& buffers)
223  {
224  return ComponentsArray(ComponentsBuffers(buffers));
225  }
226 
227  VTKM_CONT static OffsetsArray GetOffsetsArray(
228  const std::vector<vtkm::cont::internal::Buffer>& buffers)
229  {
230  return OffsetsArray(OffsetsBuffers(buffers));
231  }
232 };
233 
234 } // namespace internal
235 
270 template <typename ComponentsArrayHandleType, typename OffsetsArrayHandleType>
272  : public vtkm::cont::ArrayHandle<
273  vtkm::VecFromPortal<typename ComponentsArrayHandleType::WritePortalType>,
274  vtkm::cont::StorageTagGroupVecVariable<typename ComponentsArrayHandleType::StorageTag,
275  typename OffsetsArrayHandleType::StorageTag>>
276 {
277  VTKM_IS_ARRAY_HANDLE(ComponentsArrayHandleType);
278  VTKM_IS_ARRAY_HANDLE(OffsetsArrayHandleType);
279 
281  (std::is_same<vtkm::Id, typename OffsetsArrayHandleType::ValueType>::value),
282  "ArrayHandleGroupVecVariable's offsets array must contain vtkm::Id values.");
283 
284 public:
290  vtkm::cont::StorageTagGroupVecVariable<typename ComponentsArrayHandleType::StorageTag,
291  typename OffsetsArrayHandleType::StorageTag>>));
292 
293  using ComponentType = typename ComponentsArrayHandleType::ValueType;
294 
296  VTKM_CONT
297  ArrayHandleGroupVecVariable(const ComponentsArrayHandleType& componentsArray,
298  const OffsetsArrayHandleType& offsetsArray)
299  : Superclass(StorageType::CreateBuffers(componentsArray, offsetsArray))
300  {
301  }
302 
304  VTKM_CONT ComponentsArrayHandleType GetComponentsArray() const
305  {
306  return StorageType::GetComponentsArray(this->GetBuffers());
307  }
308 
310  VTKM_CONT OffsetsArrayHandleType GetOffsetsArray() const
311  {
312  return StorageType::GetOffsetsArray(this->GetBuffers());
313  }
314 };
315 
321 template <typename ComponentsArrayHandleType, typename OffsetsArrayHandleType>
323 make_ArrayHandleGroupVecVariable(const ComponentsArrayHandleType& componentsArray,
324  const OffsetsArrayHandleType& offsetsArray)
325 {
327  componentsArray, offsetsArray);
328 }
329 }
330 } // namespace vtkm::cont
331 
332 //=============================================================================
333 // Specializations of serialization related classes
335 namespace vtkm
336 {
337 namespace cont
338 {
339 
340 template <typename SAH, typename OAH>
341 struct SerializableTypeString<vtkm::cont::ArrayHandleGroupVecVariable<SAH, OAH>>
342 {
343  static VTKM_CONT const std::string& Get()
344  {
345  static std::string name = "AH_GroupVecVariable<" + SerializableTypeString<SAH>::Get() + "," +
347  return name;
348  }
349 };
350 
351 template <typename SP, typename SST, typename OST>
352 struct SerializableTypeString<
353  vtkm::cont::ArrayHandle<vtkm::VecFromPortal<SP>,
354  vtkm::cont::StorageTagGroupVecVariable<SST, OST>>>
355  : SerializableTypeString<
356  vtkm::cont::ArrayHandleGroupVecVariable<vtkm::cont::ArrayHandle<typename SP::ValueType, SST>,
357  vtkm::cont::ArrayHandle<vtkm::Id, OST>>>
358 {
359 };
360 }
361 } // vtkm::cont
362 
363 namespace mangled_diy_namespace
364 {
365 
366 template <typename SAH, typename OAH>
367 struct Serialization<vtkm::cont::ArrayHandleGroupVecVariable<SAH, OAH>>
368 {
369 private:
372 
373 public:
374  static VTKM_CONT void save(BinaryBuffer& bb, const BaseType& obj)
375  {
376  vtkmdiy::save(bb, Type(obj).GetComponentsArray());
377  vtkmdiy::save(bb, Type(obj).GetOffsetsArray());
378  }
379 
380  static VTKM_CONT void load(BinaryBuffer& bb, BaseType& obj)
381  {
382  SAH src;
383  OAH off;
384 
385  vtkmdiy::load(bb, src);
386  vtkmdiy::load(bb, off);
387 
389  }
390 };
391 
392 template <typename SP, typename SST, typename OST>
393 struct Serialization<vtkm::cont::ArrayHandle<vtkm::VecFromPortal<SP>,
394  vtkm::cont::StorageTagGroupVecVariable<SST, OST>>>
395  : Serialization<
396  vtkm::cont::ArrayHandleGroupVecVariable<vtkm::cont::ArrayHandle<typename SP::ValueType, SST>,
397  vtkm::cont::ArrayHandle<vtkm::Id, OST>>>
398 {
399 };
400 } // diy
402 
403 #endif //vtk_m_cont_ArrayHandleGroupVecVariable_h
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:300
ArrayHandle.h
vtkm::cont::ArrayHandleGroupVecVariable< ArrayHandle< vtkm::Id >, ArrayHandle< vtkm::Id > >::Superclass
typename vtkm::cont::detail::GetTypeInParentheses< void(vtkm::cont::ArrayHandle< vtkm::VecFromPortal< typename ArrayHandle< vtkm::Id > ::WritePortalType >, vtkm::cont::StorageTagGroupVecVariable< typename ArrayHandle< vtkm::Id > ::StorageTag, typename ArrayHandle< vtkm::Id > ::StorageTag > >) >::type Superclass
Definition: ArrayHandleGroupVecVariable.h:291
vtkm::cont::ArrayHandle< vtkm::VecFromPortal< ComponentsArrayHandleType::WritePortalType >, vtkm::cont::StorageTagGroupVecVariable< ComponentsArrayHandleType::StorageTag, OffsetsArrayHandleType::StorageTag > >::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
ArrayHandleCast.h
vtkm::cont::ArrayHandleGroupVecVariable< ArrayHandle< vtkm::Id >, ArrayHandle< vtkm::Id > >::ComponentType
typename ArrayHandle< vtkm::Id > ::ValueType ComponentType
Definition: ArrayHandleGroupVecVariable.h:293
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::IdComponent
vtkm::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:194
vtkm::cont::ArrayHandleGroupVecVariable
Fancy array handle that groups values into vectors of different sizes.
Definition: ArrayHandleGroupVecVariable.h:271
Assert.h
vtkm::cont::ErrorBadType
This class is thrown when VTK-m encounters data of a type that is incompatible with the current opera...
Definition: ErrorBadType.h:25
VTKM_STORAGE_NO_RESIZE
#define VTKM_STORAGE_NO_RESIZE
Definition: Storage.h:185
mangled_diy_namespace
Definition: Particle.h:351
vtkm::cont::StorageTagGroupVecVariable
Definition: ArrayHandleGroupVecVariable.h:117
vtkm::cont::ArrayHandleGroupVecVariable::GetOffsetsArray
OffsetsArrayHandleType GetOffsetsArray() const
Return the offsets array defining the locations and sizes of each value.
Definition: ArrayHandleGroupVecVariable.h:310
vtkm::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
vtkm::cont::LogLevel::Info
@ Info
Information messages (detected hardware, etc) and temporary debugging output.
ArrayPortal.h
VecFromPortal.h
VTKM_IS_ARRAY_HANDLE
#define VTKM_IS_ARRAY_HANDLE(T)
Checks that the given type is a vtkm::cont::ArrayHandle.
Definition: ArrayHandle.h:137
VTKM_STATIC_ASSERT_MSG
#define VTKM_STATIC_ASSERT_MSG(condition, message)
Definition: StaticAssert.h:18
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::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:58
vtkm::cont::ArrayHandleGroupVecVariable::ArrayHandleGroupVecVariable
ArrayHandleGroupVecVariable(const ComponentsArrayHandleType &componentsArray, const OffsetsArrayHandleType &offsetsArray)
Construct an ArrayHandleGroupVecVariable
Definition: ArrayHandleGroupVecVariable.h:297
ErrorBadValue.h
vtkm::cont::ArrayHandle< vtkm::VecFromPortal< ArrayHandle< vtkm::Id > ::WritePortalType >, vtkm::cont::StorageTagGroupVecVariable< ArrayHandle< vtkm::Id > ::StorageTag, ArrayHandle< vtkm::Id > ::StorageTag > >::StorageType
vtkm::cont::internal::Storage< ValueType, StorageTag > StorageType
Definition: ArrayHandle.h:309
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:89
vtkm::VecFromPortal
A short variable-length array from a window in an ArrayPortal.
Definition: VecFromPortal.h:29
vtkm::cont::make_ArrayHandleGroupVecVariable
vtkm::cont::ArrayHandleGroupVecVariable< ComponentsArrayHandleType, OffsetsArrayHandleType > make_ArrayHandleGroupVecVariable(const ComponentsArrayHandleType &componentsArray, const OffsetsArrayHandleType &offsetsArray)
make_ArrayHandleGroupVecVariable is convenience function to generate an ArrayHandleGroupVecVariable.
Definition: ArrayHandleGroupVecVariable.h:323
VTKM_SUPPRESS_EXEC_WARNINGS
#define VTKM_SUPPRESS_EXEC_WARNINGS
Definition: ExportMacros.h:53
vtkm::cont::ArrayHandleGroupVecVariable::GetComponentsArray
ComponentsArrayHandleType GetComponentsArray() const
Return the components array providing the data for the grouped vec array.
Definition: ArrayHandleGroupVecVariable.h:304