Viskores  1.0
ArrayHandleExtractComponent.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_ArrayHandleExtractComponent_h
19 #define viskores_cont_ArrayHandleExtractComponent_h
20 
21 #include <viskores/StaticAssert.h>
22 #include <viskores/VecTraits.h>
25 
26 namespace viskores
27 {
28 namespace internal
29 {
30 
31 template <typename PortalType>
32 class VISKORES_ALWAYS_EXPORT ArrayPortalExtractComponent
33 {
34  using Writable = viskores::internal::PortalSupportsSets<PortalType>;
35 
36 public:
37  using VectorType = typename PortalType::ValueType;
38  using Traits = viskores::VecTraits<VectorType>;
39  using ValueType = typename Traits::ComponentType;
40 
42  ArrayPortalExtractComponent()
43  : Portal()
44  , Component(0)
45  {
46  }
47 
49  ArrayPortalExtractComponent(const PortalType& portal, viskores::IdComponent component)
50  : Portal(portal)
51  , Component(component)
52  {
53  }
54 
55  ArrayPortalExtractComponent(const ArrayPortalExtractComponent&) = default;
56  ArrayPortalExtractComponent(ArrayPortalExtractComponent&&) = default;
57  ArrayPortalExtractComponent& operator=(const ArrayPortalExtractComponent&) = default;
58  ArrayPortalExtractComponent& operator=(ArrayPortalExtractComponent&&) = default;
59 
61  viskores::Id GetNumberOfValues() const { return this->Portal.GetNumberOfValues(); }
62 
64  ValueType Get(viskores::Id index) const
65  {
66  return Traits::GetComponent(this->Portal.Get(index), this->Component);
67  }
68 
69  template <typename Writable_ = Writable,
70  typename = typename std::enable_if<Writable_::value>::type>
71  VISKORES_EXEC_CONT void Set(viskores::Id index, const ValueType& value) const
72  {
73  VectorType vec = this->Portal.Get(index);
74  Traits::SetComponent(vec, this->Component, value);
75  this->Portal.Set(index, vec);
76  }
77 
79  const PortalType& GetPortal() const { return this->Portal; }
80 
81 private:
82  PortalType Portal;
83  viskores::IdComponent Component;
84 }; // class ArrayPortalExtractComponent
85 
86 } // namespace internal
87 
88 namespace cont
89 {
90 
91 template <typename ArrayHandleType>
93 {
94 };
95 
96 namespace internal
97 {
98 
99 template <typename ArrayHandleType>
100 class Storage<typename viskores::VecTraits<typename ArrayHandleType::ValueType>::ComponentType,
101  StorageTagExtractComponent<ArrayHandleType>>
102 {
103  using SourceValueType = typename ArrayHandleType::ValueType;
104  using ValueType = typename viskores::VecTraits<SourceValueType>::ComponentType;
105  using SourceStorage = typename ArrayHandleType::StorageType;
106 
107 public:
108  VISKORES_CONT static viskores::IdComponent ComponentIndex(
109  const std::vector<viskores::cont::internal::Buffer>& buffers)
110  {
111  return buffers[0].GetMetaData<viskores::IdComponent>();
112  }
113 
114  VISKORES_CONT static std::vector<viskores::cont::internal::Buffer> SourceBuffers(
115  const std::vector<viskores::cont::internal::Buffer>& buffers)
116  {
117  return std::vector<viskores::cont::internal::Buffer>(buffers.begin() + 1, buffers.end());
118  }
119 
120  using ReadPortalType =
121  viskores::internal::ArrayPortalExtractComponent<typename SourceStorage::ReadPortalType>;
122  using WritePortalType =
123  viskores::internal::ArrayPortalExtractComponent<typename SourceStorage::WritePortalType>;
124 
125  VISKORES_CONT static viskores::IdComponent GetNumberOfComponentsFlat(
126  const std::vector<viskores::cont::internal::Buffer>&)
127  {
129  }
130 
131  VISKORES_CONT static viskores::Id GetNumberOfValues(
132  const std::vector<viskores::cont::internal::Buffer>& buffers)
133  {
134  return SourceStorage::GetNumberOfValues(SourceBuffers(buffers));
135  }
136 
137  VISKORES_CONT static void Fill(const std::vector<viskores::cont::internal::Buffer>&,
138  const ValueType&,
139  viskores::Id,
140  viskores::Id,
142  {
143  throw viskores::cont::ErrorBadType("Fill not supported for ArrayHandleExtractComponent.");
144  }
145 
146  VISKORES_CONT static void ResizeBuffers(
147  viskores::Id numValues,
148  const std::vector<viskores::cont::internal::Buffer>& buffers,
149  viskores::CopyFlag preserve,
150  viskores::cont::Token& token)
151  {
152  SourceStorage::ResizeBuffers(numValues, SourceBuffers(buffers), preserve, token);
153  }
154 
155  VISKORES_CONT static ReadPortalType CreateReadPortal(
156  const std::vector<viskores::cont::internal::Buffer>& buffers,
158  viskores::cont::Token& token)
159  {
160  return ReadPortalType(SourceStorage::CreateReadPortal(SourceBuffers(buffers), device, token),
161  ComponentIndex(buffers));
162  }
163 
164  VISKORES_CONT static WritePortalType CreateWritePortal(
165  const std::vector<viskores::cont::internal::Buffer>& buffers,
167  viskores::cont::Token& token)
168  {
169  return WritePortalType(SourceStorage::CreateWritePortal(SourceBuffers(buffers), device, token),
170  ComponentIndex(buffers));
171  }
172 
173  VISKORES_CONT static auto CreateBuffers(viskores::IdComponent componentIndex = 0,
174  const ArrayHandleType& array = ArrayHandleType{})
175  -> decltype(viskores::cont::internal::CreateBuffers())
176  {
177  return viskores::cont::internal::CreateBuffers(componentIndex, array);
178  }
179 }; // class Storage
180 
181 }
182 }
183 } // namespace viskores::cont::internal
184 
185 namespace viskores
186 {
187 namespace cont
188 {
189 
201 template <typename ArrayHandleType>
204  typename viskores::VecTraits<typename ArrayHandleType::ValueType>::ComponentType,
205  StorageTagExtractComponent<ArrayHandleType>>
206 {
207 public:
214 
217  ArrayHandleExtractComponent(const ArrayHandleType& array, viskores::IdComponent component)
218  : Superclass(StorageType::CreateBuffers(component, array))
219  {
220  }
221 
224  {
225  return StorageType::ComponentIndex(this->GetBuffers());
226  }
227 
229  VISKORES_CONT ArrayHandleType GetArray() const
230  {
231  using BaseArray = viskores::cont::ArrayHandle<typename ArrayHandleType::ValueType,
232  typename ArrayHandleType::StorageTag>;
233  return ArrayHandleType(BaseArray(StorageType::SourceBuffers(this->GetBuffers())));
234  }
235 };
236 
239 template <typename ArrayHandleType>
241  const ArrayHandleType& array,
242  viskores::IdComponent component)
243 {
244  return ArrayHandleExtractComponent<ArrayHandleType>(array, component);
245 }
246 
247 namespace internal
248 {
249 
251 // Doxygen has trouble parsing this, and it is not important to document.
252 
253 template <typename ArrayHandleType>
254 struct ArrayExtractComponentImpl<viskores::cont::StorageTagExtractComponent<ArrayHandleType>>
255 {
257  viskores::IdComponent componentIndex,
258  viskores::CopyFlag allowCopy) const
259  -> decltype(ArrayExtractComponentImpl<typename ArrayHandleType::StorageTag>{}(
260  std::declval<ArrayHandleType>(),
261  componentIndex,
262  allowCopy))
263  {
264  using ValueType = typename ArrayHandleType::ValueType;
265  using ComponentType = typename viskores::VecTraits<ValueType>::ComponentType;
266  using FlatComponent = viskores::VecFlat<ComponentType>;
267  constexpr viskores::IdComponent FLAT_SUB_COMPONENTS = FlatComponent::NUM_COMPONENTS;
268  return ArrayExtractComponentImpl<typename ArrayHandleType::StorageTag>{}(
269  src.GetArray(), (src.GetComponent() * FLAT_SUB_COMPONENTS) + componentIndex, allowCopy);
270  }
271 };
272 
274 
275 } // namespace internal
276 
277 }
278 } // namespace viskores::cont
279 
280 //=============================================================================
281 // Specializations of serialization related classes
283 namespace viskores
284 {
285 namespace cont
286 {
287 
288 template <typename AH>
289 struct SerializableTypeString<viskores::cont::ArrayHandleExtractComponent<AH>>
290 {
291  static VISKORES_CONT const std::string& Get()
292  {
293  static std::string name = "AH_ExtractComponent<" + SerializableTypeString<AH>::Get() + ">";
294  return name;
295  }
296 };
297 
298 template <typename AH>
299 struct SerializableTypeString<
300  viskores::cont::ArrayHandle<typename viskores::VecTraits<typename AH::ValueType>::ComponentType,
301  viskores::cont::StorageTagExtractComponent<AH>>>
302  : SerializableTypeString<viskores::cont::ArrayHandleExtractComponent<AH>>
303 {
304 };
305 }
306 } // viskores::cont
307 
308 namespace mangled_diy_namespace
309 {
310 
311 template <typename AH>
312 struct Serialization<viskores::cont::ArrayHandleExtractComponent<AH>>
313 {
314 private:
317 
318 public:
319  static VISKORES_CONT void save(BinaryBuffer& bb, const BaseType& obj)
320  {
321  viskoresdiy::save(bb, Type(obj).GetComponent());
322  viskoresdiy::save(bb, Type(obj).GetArray());
323  }
324 
325  static VISKORES_CONT void load(BinaryBuffer& bb, BaseType& obj)
326  {
327  viskores::IdComponent component = 0;
328  AH array;
329  viskoresdiy::load(bb, component);
330  viskoresdiy::load(bb, array);
331 
332  obj = viskores::cont::make_ArrayHandleExtractComponent(array, component);
333  }
334 };
335 
336 template <typename AH>
337 struct Serialization<
338  viskores::cont::ArrayHandle<typename viskores::VecTraits<typename AH::ValueType>::ComponentType,
339  viskores::cont::StorageTagExtractComponent<AH>>>
340  : Serialization<viskores::cont::ArrayHandleExtractComponent<AH>>
341 {
342 };
343 } // diy
345 
346 #endif // viskores_cont_ArrayHandleExtractComponent_h
viskores::exec::arg::load
T load(const U &u, viskores::Id v)
Definition: FetchTagArrayDirectIn.h:44
viskores::VecFlat
Treat a Vec or Vec-like object as a flat Vec.
Definition: VecFlat.h:240
ArrayHandle.h
ArrayExtractComponent.h
viskores::cont::ErrorBadType
This class is thrown when Viskores encounters data of a type that is incompatible with the current op...
Definition: ErrorBadType.h:33
viskores::cont::ArrayHandleExtractComponent::ArrayHandleExtractComponent
ArrayHandleExtractComponent(const ArrayHandleType &array, viskores::IdComponent component)
Construct an ArrayHandleExtractComponent with a given array and component.
Definition: ArrayHandleExtractComponent.h:217
viskores::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:313
viskores::IdComponent
viskores::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:202
VISKORES_EXEC_CONT
#define VISKORES_EXEC_CONT
Definition: ExportMacros.h:60
mangled_diy_namespace
Definition: Particle.h:373
viskores::Id
viskores::Int64 Id
Base type to use to index arrays.
Definition: Types.h:235
viskores::cont::ArrayHandleExtractComponent::GetArray
ArrayHandleType GetArray() const
Get the source array of Vecs to get a component out of.
Definition: ArrayHandleExtractComponent.h:229
VISKORES_CONT
#define VISKORES_CONT
Definition: ExportMacros.h:65
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
viskores::cont::ArrayHandleExtractComponent
A fancy ArrayHandle that turns a vector array into a scalar array by slicing out a single component o...
Definition: ArrayHandleExtractComponent.h:202
viskores::cont::ArrayHandleExtractComponent::StorageType
typename Superclass::StorageType StorageType
Definition: ArrayHandleExtractComponent.h:213
viskores::cont::make_ArrayHandleExtractComponent
ArrayHandleExtractComponent< ArrayHandleType > make_ArrayHandleExtractComponent(const ArrayHandleType &array, viskores::IdComponent component)
make_ArrayHandleExtractComponent is convenience function to generate an ArrayHandleExtractComponent.
Definition: ArrayHandleExtractComponent.h:240
viskores::VecTraits
Traits that can be queried to treat any type as a Vec.
Definition: VecTraits.h:69
viskores::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:66
StaticAssert.h
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::VecTraits::ComponentType
T ComponentType
Type of the components in the vector.
Definition: VecTraits.h:79
viskores::cont::ArrayHandleExtractComponent::Superclass
typename viskores::cont::detail::GetTypeInParentheses< void(viskores::cont::ArrayHandle< typename viskores::VecTraits< typename ArrayHandleType::ValueType >::ComponentType, StorageTagExtractComponent< ArrayHandleType > >) >::type Superclass
Definition: ArrayHandleExtractComponent.h:213
viskores::cont::ArrayHandleExtractComponent::GetComponent
viskores::IdComponent GetComponent() const
Get the component index being extracted from the source array.
Definition: ArrayHandleExtractComponent.h:223
viskores::cont::StorageTagExtractComponent
Definition: ArrayHandleExtractComponent.h:92
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::CopyFlag
CopyFlag
Identifier used to specify whether a function should deep copy data.
Definition: Flags.h:25
viskores::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:43
viskores::cont::ArrayHandle< viskores::VecTraits< ArrayHandleType::ValueType >::ComponentType, StorageTagExtractComponent< ArrayHandleType > >::GetBuffers
const std::vector< viskores::cont::internal::Buffer > & GetBuffers() const
Returns the internal Buffer structures that hold the data.
Definition: ArrayHandle.h:738
VecTraits.h