VTK-m  2.0
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 vtkmNotUsed(index), const ValueType& vtkmNotUsed(value)) const
81  {
82  // The ValueType (VecFromPortal) operates on demand. Thus, if you set
83  // something in the value, it has already been passed to the array. Perhaps
84  // we should check to make sure that the value used matches the location
85  // you are trying to set in the array, but we don't do that.
86  }
87 
90  const ComponentsPortalType& GetComponentsPortal() const { return this->ComponentsPortal; }
91 
94  const OffsetsPortalType& GetOffsetsPortal() const { return this->OffsetsPortal; }
95 
96 private:
97  ComponentsPortalType ComponentsPortal;
98  OffsetsPortalType OffsetsPortal;
99 };
100 
101 }
102 } // namespace vtkm::internal
103 
104 namespace vtkm
105 {
106 namespace cont
107 {
108 
109 template <typename ComponentsStorageTag, typename OffsetsStorageTag>
111 {
112 };
113 
114 namespace internal
115 {
116 
117 template <typename ComponentsPortal, typename ComponentsStorageTag, typename OffsetsStorageTag>
118 class Storage<vtkm::VecFromPortal<ComponentsPortal>,
119  vtkm::cont::StorageTagGroupVecVariable<ComponentsStorageTag, OffsetsStorageTag>>
120 {
121  using ComponentType = typename ComponentsPortal::ValueType;
122  using ComponentsStorage = vtkm::cont::internal::Storage<ComponentType, ComponentsStorageTag>;
123  using OffsetsStorage = vtkm::cont::internal::Storage<vtkm::Id, OffsetsStorageTag>;
124 
127 
129  (std::is_same<ComponentsPortal, typename ComponentsStorage::WritePortalType>::value),
130  "Used invalid ComponentsPortal type with expected ComponentsStorageTag.");
131 
132  struct Info
133  {
134  std::size_t OffsetsBuffersOffset;
135  };
136 
137  VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> ComponentsBuffers(
138  const std::vector<vtkm::cont::internal::Buffer>& buffers)
139  {
140  Info info = buffers[0].GetMetaData<Info>();
141  return std::vector<vtkm::cont::internal::Buffer>(buffers.begin() + 1,
142  buffers.begin() + info.OffsetsBuffersOffset);
143  }
144 
145  VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> OffsetsBuffers(
146  const std::vector<vtkm::cont::internal::Buffer> buffers)
147  {
148  Info info = buffers[0].GetMetaData<Info>();
149  return std::vector<vtkm::cont::internal::Buffer>(buffers.begin() + info.OffsetsBuffersOffset,
150  buffers.end());
151  }
152 
153 public:
155 
156  using ReadPortalType =
157  vtkm::internal::ArrayPortalGroupVecVariable<typename ComponentsStorage::ReadPortalType,
158  typename OffsetsStorage::ReadPortalType>;
159  using WritePortalType =
160  vtkm::internal::ArrayPortalGroupVecVariable<typename ComponentsStorage::WritePortalType,
161  typename OffsetsStorage::ReadPortalType>;
162 
163  VTKM_CONT static vtkm::Id GetNumberOfValues(
164  const std::vector<vtkm::cont::internal::Buffer>& buffers)
165  {
166  return OffsetsStorage::GetNumberOfValues(OffsetsBuffers(buffers)) - 1;
167  }
168 
169  VTKM_CONT static void Fill(const std::vector<vtkm::cont::internal::Buffer>&,
171  vtkm::Id,
172  vtkm::Id,
174  {
175  throw vtkm::cont::ErrorBadType("Fill not supported for ArrayHandleGroupVecVariable.");
176  }
177 
178  VTKM_CONT static ReadPortalType CreateReadPortal(
179  const std::vector<vtkm::cont::internal::Buffer>& buffers,
181  vtkm::cont::Token& token)
182  {
183  return ReadPortalType(
184  ComponentsStorage::CreateReadPortal(ComponentsBuffers(buffers), device, token),
185  OffsetsStorage::CreateReadPortal(OffsetsBuffers(buffers), device, token));
186  }
187 
188  VTKM_CONT static WritePortalType CreateWritePortal(
189  const std::vector<vtkm::cont::internal::Buffer>& buffers,
191  vtkm::cont::Token& token)
192  {
193  return WritePortalType(
194  ComponentsStorage::CreateWritePortal(ComponentsBuffers(buffers), device, token),
195  OffsetsStorage::CreateReadPortal(OffsetsBuffers(buffers), device, token));
196  }
197 
198  VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers(
199  const ComponentsArray& componentsArray = ComponentsArray{},
200  const OffsetsArray& offsetsArray = OffsetsArray{})
201  {
202  Info info;
203  info.OffsetsBuffersOffset = 1 + componentsArray.GetBuffers().size();
204  return vtkm::cont::internal::CreateBuffers(info, componentsArray, offsetsArray);
205  }
206 
207  VTKM_CONT static ComponentsArray GetComponentsArray(
208  const std::vector<vtkm::cont::internal::Buffer>& buffers)
209  {
210  return ComponentsArray(ComponentsBuffers(buffers));
211  }
212 
213  VTKM_CONT static OffsetsArray GetOffsetsArray(
214  const std::vector<vtkm::cont::internal::Buffer>& buffers)
215  {
216  return OffsetsArray(OffsetsBuffers(buffers));
217  }
218 };
219 
220 } // namespace internal
221 
254 template <typename ComponentsArrayHandleType, typename OffsetsArrayHandleType>
256  : public vtkm::cont::ArrayHandle<
257  vtkm::VecFromPortal<typename ComponentsArrayHandleType::WritePortalType>,
258  vtkm::cont::StorageTagGroupVecVariable<typename ComponentsArrayHandleType::StorageTag,
259  typename OffsetsArrayHandleType::StorageTag>>
260 {
261  VTKM_IS_ARRAY_HANDLE(ComponentsArrayHandleType);
262  VTKM_IS_ARRAY_HANDLE(OffsetsArrayHandleType);
263 
265  (std::is_same<vtkm::Id, typename OffsetsArrayHandleType::ValueType>::value),
266  "ArrayHandleGroupVecVariable's offsets array must contain vtkm::Id values.");
267 
268 public:
274  vtkm::cont::StorageTagGroupVecVariable<typename ComponentsArrayHandleType::StorageTag,
275  typename OffsetsArrayHandleType::StorageTag>>));
276 
277  using ComponentType = typename ComponentsArrayHandleType::ValueType;
278 
279 private:
280  using StorageType = vtkm::cont::internal::Storage<ValueType, StorageTag>;
281 
282 public:
283  VTKM_CONT
284  ArrayHandleGroupVecVariable(const ComponentsArrayHandleType& componentsArray,
285  const OffsetsArrayHandleType& offsetsArray)
286  : Superclass(StorageType::CreateBuffers(componentsArray, offsetsArray))
287  {
288  }
289 
290  VTKM_CONT ComponentsArrayHandleType GetComponentsArray() const
291  {
292  return StorageType::GetComponentsArray(this->GetBuffers());
293  }
294 
295  VTKM_CONT OffsetsArrayHandleType GetOffsetsArray() const
296  {
297  return StorageType::GetOffsetsArray(this->GetBuffers());
298  }
299 };
300 
306 template <typename ComponentsArrayHandleType, typename OffsetsArrayHandleType>
308 make_ArrayHandleGroupVecVariable(const ComponentsArrayHandleType& componentsArray,
309  const OffsetsArrayHandleType& offsetsArray)
310 {
312  componentsArray, offsetsArray);
313 }
314 }
315 } // namespace vtkm::cont
316 
317 //=============================================================================
318 // Specializations of worklet arguments using ArrayHandleGropuVecVariable
320 
321 //=============================================================================
322 // Specializations of serialization related classes
324 namespace vtkm
325 {
326 namespace cont
327 {
328 
329 template <typename SAH, typename OAH>
330 struct SerializableTypeString<vtkm::cont::ArrayHandleGroupVecVariable<SAH, OAH>>
331 {
332  static VTKM_CONT const std::string& Get()
333  {
334  static std::string name = "AH_GroupVecVariable<" + SerializableTypeString<SAH>::Get() + "," +
336  return name;
337  }
338 };
339 
340 template <typename SP, typename SST, typename OST>
341 struct SerializableTypeString<
342  vtkm::cont::ArrayHandle<vtkm::VecFromPortal<SP>,
343  vtkm::cont::StorageTagGroupVecVariable<SST, OST>>>
344  : SerializableTypeString<
345  vtkm::cont::ArrayHandleGroupVecVariable<vtkm::cont::ArrayHandle<typename SP::ValueType, SST>,
346  vtkm::cont::ArrayHandle<vtkm::Id, OST>>>
347 {
348 };
349 }
350 } // vtkm::cont
351 
352 namespace mangled_diy_namespace
353 {
354 
355 template <typename SAH, typename OAH>
356 struct Serialization<vtkm::cont::ArrayHandleGroupVecVariable<SAH, OAH>>
357 {
358 private:
361 
362 public:
363  static VTKM_CONT void save(BinaryBuffer& bb, const BaseType& obj)
364  {
365  vtkmdiy::save(bb, Type(obj).GetComponentsArray());
366  vtkmdiy::save(bb, Type(obj).GetOffsetsArray());
367  }
368 
369  static VTKM_CONT void load(BinaryBuffer& bb, BaseType& obj)
370  {
371  SAH src;
372  OAH off;
373 
374  vtkmdiy::load(bb, src);
375  vtkmdiy::load(bb, off);
376 
378  }
379 };
380 
381 template <typename SP, typename SST, typename OST>
382 struct Serialization<vtkm::cont::ArrayHandle<vtkm::VecFromPortal<SP>,
383  vtkm::cont::StorageTagGroupVecVariable<SST, OST>>>
384  : Serialization<
385  vtkm::cont::ArrayHandleGroupVecVariable<vtkm::cont::ArrayHandle<typename SP::ValueType, SST>,
386  vtkm::cont::ArrayHandle<vtkm::Id, OST>>>
387 {
388 };
389 } // diy
391 
392 #endif //vtk_m_cont_ArrayHandleGroupVecVariable_h
vtkm::cont::ArrayHandle< vtkm::VecFromPortal< ComponentsArrayHandleType::WritePortalType >, vtkm::cont::StorageTagGroupVecVariable< ComponentsArrayHandleType::StorageTag, OffsetsArrayHandleType::StorageTag > >::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::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:283
ArrayHandle.h
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
ArrayHandleCast.h
vtkm::cont::ArrayHandleGroupVecVariable< ArrayHandle< vtkm::Id >, ArrayHandle< vtkm::Id > >::ComponentType
typename ArrayHandle< vtkm::Id > ::ValueType ComponentType
Definition: ArrayHandleGroupVecVariable.h:277
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
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
vtkm::IdComponent
vtkm::Int32 IdComponent
Represents a component ID (index of component in a vector).
Definition: Types.h:168
vtkm::cont::ArrayHandleGroupVecVariable
Fancy array handle that groups values into vectors of different sizes.
Definition: ArrayHandleGroupVecVariable.h:255
vtkm::cont::ArrayHandleGroupVecVariable::GetComponentsArray
VTKM_CONT ComponentsArrayHandleType GetComponentsArray() const
Definition: ArrayHandleGroupVecVariable.h:290
vtkm::cont::ArrayHandleGroupVecVariable::GetOffsetsArray
VTKM_CONT OffsetsArrayHandleType GetOffsetsArray() const
Definition: ArrayHandleGroupVecVariable.h:295
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: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::StorageTagGroupVecVariable
Definition: ArrayHandleGroupVecVariable.h:110
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.
FetchTagArrayDirectOutArrayHandleGroupVecVariable.h
ArrayPortal.h
VecFromPortal.h
vtkm::cont::ArrayHandleGroupVecVariable::VTKM_IS_ARRAY_HANDLE
VTKM_IS_ARRAY_HANDLE(ComponentsArrayHandleType)
VTKM_STATIC_ASSERT_MSG
#define VTKM_STATIC_ASSERT_MSG(condition, message)
Definition: StaticAssert.h:18
vtkm::cont::ArrayHandleGroupVecVariable::VTKM_STATIC_ASSERT_MSG
VTKM_STATIC_ASSERT_MSG((std::is_same< vtkm::Id, typename OffsetsArrayHandleType::ValueType >::value), "ArrayHandleGroupVecVariable's offsets array must contain vtkm::Id values.")
vtkm::cont::make_ArrayHandleGroupVecVariable
VTKM_CONT 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:308
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkmNotUsed
#define vtkmNotUsed(parameter_name)
Simple macro to identify a parameter as unused.
Definition: ExportMacros.h:128
vtkm::cont::DeviceAdapterId
Definition: DeviceAdapterTag.h:52
vtkm::cont::ArrayHandleGroupVecVariable::ArrayHandleGroupVecVariable
VTKM_CONT ArrayHandleGroupVecVariable(const ComponentsArrayHandleType &componentsArray, const OffsetsArrayHandleType &offsetsArray)
Definition: ArrayHandleGroupVecVariable.h:284
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:292
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:92
vtkm::VecFromPortal
A short variable-length array from a window in an ArrayPortal.
Definition: VecFromPortal.h:29
VTKM_SUPPRESS_EXEC_WARNINGS
#define VTKM_SUPPRESS_EXEC_WARNINGS
Definition: ExportMacros.h:53
vtkm::cont::ArrayHandleGroupVecVariable::VTKM_ARRAY_HANDLE_SUBCLASS
VTKM_ARRAY_HANDLE_SUBCLASS(ArrayHandleGroupVecVariable,(ArrayHandleGroupVecVariable< ComponentsArrayHandleType, OffsetsArrayHandleType >),(vtkm::cont::ArrayHandle< vtkm::VecFromPortal< typename ComponentsArrayHandleType::WritePortalType >, vtkm::cont::StorageTagGroupVecVariable< typename ComponentsArrayHandleType::StorageTag, typename OffsetsArrayHandleType::StorageTag >>))