VTK-m  2.0
ArrayHandleGroupVec.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_ArrayHandleGroupVec_h
11 #define vtk_m_cont_ArrayHandleGroupVec_h
12 
14 #include <vtkm/cont/ArrayHandle.h>
15 #include <vtkm/cont/ArrayPortal.h>
17 
18 namespace vtkm
19 {
20 namespace internal
21 {
22 
23 template <typename PortalType, vtkm::IdComponent N_COMPONENTS>
24 class VTKM_ALWAYS_EXPORT ArrayPortalGroupVec
25 {
26  using Writable = vtkm::internal::PortalSupportsSets<PortalType>;
27 
28 public:
29  static constexpr vtkm::IdComponent NUM_COMPONENTS = N_COMPONENTS;
30  using ComponentsPortalType = PortalType;
31 
32  using ComponentType = typename std::remove_const<typename ComponentsPortalType::ValueType>::type;
34 
37  ArrayPortalGroupVec()
38  : ComponentsPortal()
39  {
40  }
41 
44  ArrayPortalGroupVec(const ComponentsPortalType& componentsPortal)
45  : ComponentsPortal(componentsPortal)
46  {
47  }
48 
53  template <typename OtherComponentsPortalType>
54  VTKM_EXEC_CONT ArrayPortalGroupVec(
55  const ArrayPortalGroupVec<OtherComponentsPortalType, NUM_COMPONENTS>& src)
56  : ComponentsPortal(src.GetPortal())
57  {
58  }
59 
62  vtkm::Id GetNumberOfValues() const
63  {
64  return this->ComponentsPortal.GetNumberOfValues() / NUM_COMPONENTS;
65  }
66 
69  ValueType Get(vtkm::Id index) const
70  {
71  ValueType result;
72  vtkm::Id componentsIndex = index * NUM_COMPONENTS;
73  for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; componentIndex++)
74  {
75  result[componentIndex] = this->ComponentsPortal.Get(componentsIndex);
76  componentsIndex++;
77  }
78  return result;
79  }
80 
82  template <typename Writable_ = Writable,
83  typename = typename std::enable_if<Writable_::value>::type>
84  VTKM_EXEC_CONT void Set(vtkm::Id index, const ValueType& value) const
85  {
86  vtkm::Id componentsIndex = index * NUM_COMPONENTS;
87  for (vtkm::IdComponent componentIndex = 0; componentIndex < NUM_COMPONENTS; componentIndex++)
88  {
89  this->ComponentsPortal.Set(componentsIndex, value[componentIndex]);
90  componentsIndex++;
91  }
92  }
93 
96  const ComponentsPortalType& GetPortal() const { return this->ComponentsPortal; }
97 
98 private:
99  ComponentsPortalType ComponentsPortal;
100 };
101 }
102 } // namespace vtkm::internal
103 
104 namespace vtkm
105 {
106 namespace cont
107 {
108 
109 template <typename ComponentsStorageTag, vtkm::IdComponent NUM_COMPONENTS>
111 {
112 };
113 
114 namespace internal
115 {
116 
117 template <typename ComponentType, vtkm::IdComponent NUM_COMPONENTS, typename ComponentsStorageTag>
118 class Storage<vtkm::Vec<ComponentType, NUM_COMPONENTS>,
119  vtkm::cont::StorageTagGroupVec<ComponentsStorageTag, NUM_COMPONENTS>>
120 {
121  using ComponentsStorage = vtkm::cont::internal::Storage<ComponentType, ComponentsStorageTag>;
123 
124 public:
125  using ReadPortalType =
126  vtkm::internal::ArrayPortalGroupVec<typename ComponentsStorage::ReadPortalType, NUM_COMPONENTS>;
127  using WritePortalType =
128  vtkm::internal::ArrayPortalGroupVec<typename ComponentsStorage::WritePortalType,
129  NUM_COMPONENTS>;
130 
131  VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers()
132  {
133  return ComponentsStorage::CreateBuffers();
134  }
135 
136  VTKM_CONT static void ResizeBuffers(vtkm::Id numValues,
137  const std::vector<vtkm::cont::internal::Buffer>& buffers,
138  vtkm::CopyFlag preserve,
139  vtkm::cont::Token& token)
140  {
141  ComponentsStorage::ResizeBuffers(NUM_COMPONENTS * numValues, buffers, preserve, token);
142  }
143 
144  VTKM_CONT static vtkm::Id GetNumberOfValues(
145  const std::vector<vtkm::cont::internal::Buffer>& buffers)
146  {
147  vtkm::Id componentsSize = ComponentsStorage::GetNumberOfValues(buffers);
148  return componentsSize / NUM_COMPONENTS;
149  }
150 
151  VTKM_CONT static void Fill(const std::vector<vtkm::cont::internal::Buffer>&,
152  const ValueType&,
153  vtkm::Id,
154  vtkm::Id,
156  {
157  throw vtkm::cont::ErrorBadType("Fill not supported for ArrayHandleGroupVec.");
158  }
159 
160  VTKM_CONT static ReadPortalType CreateReadPortal(
161  const std::vector<vtkm::cont::internal::Buffer>& buffers,
163  vtkm::cont::Token& token)
164  {
165  if ((ComponentsStorage::GetNumberOfValues(buffers) % NUM_COMPONENTS) != 0)
166  {
168  "ArrayHandleGroupVec's components array does not divide evenly into Vecs.");
169  }
170  return ReadPortalType(ComponentsStorage::CreateReadPortal(buffers, device, token));
171  }
172 
173  VTKM_CONT static WritePortalType CreateWritePortal(
174  const std::vector<vtkm::cont::internal::Buffer>& buffers,
176  vtkm::cont::Token& token)
177  {
178  if ((ComponentsStorage::GetNumberOfValues(buffers) % NUM_COMPONENTS) != 0)
179  {
181  "ArrayHandleGroupVec's components array does not divide evenly into Vecs.");
182  }
183  return WritePortalType(ComponentsStorage::CreateWritePortal(buffers, device, token));
184  }
185 };
186 
187 } // namespace internal
188 
206 template <typename ComponentsArrayHandleType, vtkm::IdComponent NUM_COMPONENTS>
208  : public vtkm::cont::ArrayHandle<
209  vtkm::Vec<typename ComponentsArrayHandleType::ValueType, NUM_COMPONENTS>,
210  vtkm::cont::StorageTagGroupVec<typename ComponentsArrayHandleType::StorageTag,
211  NUM_COMPONENTS>>
212 {
213  VTKM_IS_ARRAY_HANDLE(ComponentsArrayHandleType);
214 
215 public:
221  vtkm::cont::StorageTagGroupVec<typename ComponentsArrayHandleType::StorageTag,
222  NUM_COMPONENTS>>));
223 
224  using ComponentType = typename ComponentsArrayHandleType::ValueType;
225 
226  VTKM_CONT
227  ArrayHandleGroupVec(const ComponentsArrayHandleType& componentsArray)
228  : Superclass(componentsArray.GetBuffers())
229  {
230  }
231 
232  VTKM_CONT ComponentsArrayHandleType GetComponentsArray() const
233  {
234  return ComponentsArrayHandleType(this->GetBuffers());
235  }
236 };
237 
243 template <vtkm::IdComponent NUM_COMPONENTS, typename ArrayHandleType>
245  const ArrayHandleType& array)
246 {
248 }
249 
250 //--------------------------------------------------------------------------------
251 // Specialization of ArrayExtractComponent
252 namespace internal
253 {
254 
255 // Superclass will inherit the ArrayExtractComponentImplInefficient property if
256 // the sub-storage is inefficient (thus making everything inefficient).
257 template <typename ComponentsStorageTag, vtkm::IdComponent NUM_COMPONENTS>
258 struct ArrayExtractComponentImpl<
259  vtkm::cont::StorageTagGroupVec<ComponentsStorageTag, NUM_COMPONENTS>>
260  : vtkm::cont::internal::ArrayExtractComponentImpl<ComponentsStorageTag>
261 {
262  template <typename T>
267  vtkm::IdComponent componentIndex,
268  vtkm::CopyFlag allowCopy) const
269  {
271  NUM_COMPONENTS>
272  srcArray(src);
273  constexpr vtkm::IdComponent NUM_SUB_COMPONENTS = vtkm::VecFlat<T>::NUM_COMPONENTS;
275  ArrayExtractComponentImpl<ComponentsStorageTag>{}(
276  srcArray.GetComponentsArray(), componentIndex % NUM_SUB_COMPONENTS, allowCopy);
277 
278  // Adjust stride and offset to expectations of grouped values
280  dest.GetBasicArray(),
281  dest.GetNumberOfValues() / NUM_COMPONENTS,
282  dest.GetStride() * NUM_COMPONENTS,
283  dest.GetOffset() + (dest.GetStride() * (componentIndex / NUM_SUB_COMPONENTS)),
284  dest.GetModulo(),
285  dest.GetDivisor());
286  }
287 };
288 
289 } // namespace internal
290 
291 }
292 } // namespace vtkm::cont
293 
294 //=============================================================================
295 // Specializations of serialization related classes
297 namespace vtkm
298 {
299 namespace cont
300 {
301 
302 template <typename AH, vtkm::IdComponent NUM_COMPS>
303 struct SerializableTypeString<vtkm::cont::ArrayHandleGroupVec<AH, NUM_COMPS>>
304 {
305  static VTKM_CONT const std::string& Get()
306  {
307  static std::string name =
308  "AH_GroupVec<" + SerializableTypeString<AH>::Get() + "," + std::to_string(NUM_COMPS) + ">";
309  return name;
310  }
311 };
312 
313 template <typename T, vtkm::IdComponent NUM_COMPS, typename ST>
314 struct SerializableTypeString<
315  vtkm::cont::ArrayHandle<vtkm::Vec<T, NUM_COMPS>, vtkm::cont::StorageTagGroupVec<ST, NUM_COMPS>>>
316  : SerializableTypeString<
317  vtkm::cont::ArrayHandleGroupVec<vtkm::cont::ArrayHandle<T, ST>, NUM_COMPS>>
318 {
319 };
320 }
321 } // vtkm::cont
322 
323 namespace mangled_diy_namespace
324 {
325 
326 template <typename AH, vtkm::IdComponent NUM_COMPS>
327 struct Serialization<vtkm::cont::ArrayHandleGroupVec<AH, NUM_COMPS>>
328 {
329 private:
332 
333 public:
334  static VTKM_CONT void save(BinaryBuffer& bb, const BaseType& obj)
335  {
336  vtkmdiy::save(bb, Type(obj).GetComponentsArray());
337  }
338 
339  static VTKM_CONT void load(BinaryBuffer& bb, BaseType& obj)
340  {
341  AH array;
342  vtkmdiy::load(bb, array);
343 
344  obj = vtkm::cont::make_ArrayHandleGroupVec<NUM_COMPS>(array);
345  }
346 };
347 
348 template <typename T, vtkm::IdComponent NUM_COMPS, typename ST>
349 struct Serialization<
350  vtkm::cont::ArrayHandle<vtkm::Vec<T, NUM_COMPS>, vtkm::cont::StorageTagGroupVec<ST, NUM_COMPS>>>
351  : Serialization<vtkm::cont::ArrayHandleGroupVec<vtkm::cont::ArrayHandle<T, ST>, NUM_COMPS>>
352 {
353 };
354 
355 } // diy
357 
358 #endif //vtk_m_cont_ArrayHandleGroupVec_h
vtkm::cont::ArrayHandle< vtkm::Vec< ComponentsArrayHandleType::ValueType, NUM_COMPONENTS >, vtkm::cont::StorageTagGroupVec< ComponentsArrayHandleType::StorageTag, NUM_COMPONENTS > >::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< T, vtkm::cont::StorageTagStride >::GetNumberOfValues
VTKM_CONT vtkm::Id GetNumberOfValues() const
Returns the number of entries in the array.
Definition: ArrayHandle.h:448
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:283
ArrayHandle.h
ArrayExtractComponent.h
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::cont::make_ArrayHandleGroupVec
VTKM_CONT vtkm::cont::ArrayHandleGroupVec< ArrayHandleType, NUM_COMPONENTS > make_ArrayHandleGroupVec(const ArrayHandleType &array)
make_ArrayHandleGroupVec is convenience function to generate an ArrayHandleGroupVec.
Definition: ArrayHandleGroupVec.h:244
vtkm::cont::LogLevel::Warn
@ Warn
Less important user errors, such as out-of-bounds parameters.
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::ArrayHandleStride::GetStride
vtkm::Id GetStride() const
Definition: ArrayHandleStride.h:294
vtkm::cont::ArrayHandleGroupVec::ArrayHandleGroupVec
VTKM_CONT ArrayHandleGroupVec(const ComponentsArrayHandleType &componentsArray)
Definition: ArrayHandleGroupVec.h:227
vtkm::cont::ArrayHandleStride::GetOffset
vtkm::Id GetOffset() const
Definition: ArrayHandleStride.h:295
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::cont::ArrayHandleGroupVec::GetComponentsArray
VTKM_CONT ComponentsArrayHandleType GetComponentsArray() const
Definition: ArrayHandleGroupVec.h:232
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::ArrayHandleStride
An ArrayHandle that accesses a basic array with strides and offsets.
Definition: ArrayHandleStride.h:251
vtkm::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
vtkm::cont::ArrayHandleStride::GetDivisor
vtkm::Id GetDivisor() const
Definition: ArrayHandleStride.h:297
ArrayPortal.h
vtkm::cont::ArrayHandleStride::GetBasicArray
vtkm::cont::ArrayHandleBasic< T > GetBasicArray() const
Definition: ArrayHandleStride.h:299
vtkm::cont::ArrayHandleGroupVec::VTKM_IS_ARRAY_HANDLE
VTKM_IS_ARRAY_HANDLE(ComponentsArrayHandleType)
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::cont::StorageTagGroupVec
Definition: ArrayHandleGroupVec.h:110
vtkm::VecFlat
Treat a Vec or Vec-like object as a flat Vec.
Definition: VecFlat.h:224
VTKM_LOG_S
#define VTKM_LOG_S(level,...)
Writes a message using stream syntax to the indicated log level.
Definition: Logging.h:261
vtkm::cont::ArrayHandleStride::GetModulo
vtkm::Id GetModulo() const
Definition: ArrayHandleStride.h:296
vtkm::cont::ArrayHandleGroupVec::VTKM_ARRAY_HANDLE_SUBCLASS
VTKM_ARRAY_HANDLE_SUBCLASS(ArrayHandleGroupVec,(ArrayHandleGroupVec< ComponentsArrayHandleType, NUM_COMPONENTS >),(vtkm::cont::ArrayHandle< vtkm::Vec< typename ComponentsArrayHandleType::ValueType, NUM_COMPONENTS >, vtkm::cont::StorageTagGroupVec< typename ComponentsArrayHandleType::StorageTag, NUM_COMPONENTS >>))
vtkm::cont::DeviceAdapterId
Definition: DeviceAdapterTag.h:52
vtkm::Vec
A short fixed-length array.
Definition: Types.h:767
vtkm::cont::ArrayHandleGroupVec< vtkm::cont::ArrayHandle< vtkm::IdComponent >, VertexConnections_VecLength >::ComponentType
typename vtkm::cont::ArrayHandle< vtkm::IdComponent > ::ValueType ComponentType
Definition: ArrayHandleGroupVec.h:224
ErrorBadValue.h
vtkm::CopyFlag
CopyFlag
Definition: Flags.h:16
vtkm::cont::ArrayHandleGroupVec
Fancy array handle that groups values into vectors.
Definition: ArrayHandleGroupVec.h:207
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:92
VTKM_SUPPRESS_EXEC_WARNINGS
#define VTKM_SUPPRESS_EXEC_WARNINGS
Definition: ExportMacros.h:53