VTK-m  2.2
ArrayHandleView.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_ArrayHandleView_h
11 #define vtk_m_cont_ArrayHandleView_h
12 
13 #include <vtkm/Assert.h>
14 
16 #include <vtkm/cont/ArrayHandle.h>
17 #include <vtkm/cont/ArrayPortal.h>
18 
19 namespace vtkm
20 {
21 
22 namespace internal
23 {
24 
25 struct ViewIndices
26 {
27  vtkm::Id StartIndex = 0;
28  vtkm::Id NumberOfValues = 0;
29 
30  ViewIndices() = default;
31 
32  ViewIndices(vtkm::Id start, vtkm::Id numValues)
33  : StartIndex(start)
34  , NumberOfValues(numValues)
35  {
36  }
37 };
38 
39 template <typename TargetPortalType>
40 class ArrayPortalView
41 {
42  using Writable = vtkm::internal::PortalSupportsSets<TargetPortalType>;
43 
44 public:
45  using ValueType = typename TargetPortalType::ValueType;
46 
48  ArrayPortalView() {}
49 
51  ArrayPortalView(const TargetPortalType& targetPortal, ViewIndices indices)
52  : TargetPortal(targetPortal)
53  , Indices(indices)
54  {
55  }
56 
57  template <typename OtherPortalType>
58  VTKM_EXEC_CONT ArrayPortalView(const ArrayPortalView<OtherPortalType>& otherPortal)
59  : TargetPortal(otherPortal.GetTargetPortal())
60  , Indices(otherPortal.GetStartIndex(), otherPortal.GetNumberOfValues())
61  {
62  }
63 
65  vtkm::Id GetNumberOfValues() const { return this->Indices.NumberOfValues; }
66 
68  ValueType Get(vtkm::Id index) const
69  {
70  return this->TargetPortal.Get(index + this->GetStartIndex());
71  }
72 
73  template <typename Writable_ = Writable,
74  typename = typename std::enable_if<Writable_::value>::type>
75  VTKM_EXEC_CONT void Set(vtkm::Id index, const ValueType& value) const
76  {
77  this->TargetPortal.Set(index + this->GetStartIndex(), value);
78  }
79 
81  const TargetPortalType& GetTargetPortal() const { return this->TargetPortal; }
83  vtkm::Id GetStartIndex() const { return this->Indices.StartIndex; }
84 
85 private:
86  TargetPortalType TargetPortal;
87  ViewIndices Indices;
88 };
89 
90 } // namespace internal
91 
92 namespace cont
93 {
94 
95 template <typename StorageTag>
97 {
98 };
99 
100 namespace internal
101 {
102 
103 template <typename T, typename ST>
104 class Storage<T, StorageTagView<ST>>
105 {
106  using ArrayHandleType = vtkm::cont::ArrayHandle<T, ST>;
107  using SourceStorage = Storage<T, ST>;
108 
109  static std::vector<vtkm::cont::internal::Buffer> SourceBuffers(
110  const std::vector<vtkm::cont::internal::Buffer>& buffers)
111  {
112  return std::vector<vtkm::cont::internal::Buffer>(buffers.begin() + 1, buffers.end());
113  }
114 
115 public:
117 
118  using ReadPortalType = vtkm::internal::ArrayPortalView<typename ArrayHandleType::ReadPortalType>;
119  using WritePortalType =
120  vtkm::internal::ArrayPortalView<typename ArrayHandleType::WritePortalType>;
121 
122  VTKM_CONT static vtkm::IdComponent GetNumberOfComponentsFlat(
123  const std::vector<vtkm::cont::internal::Buffer>& buffers)
124  {
125  return SourceStorage::GetNumberOfComponentsFlat(SourceBuffers(buffers));
126  }
127 
128  VTKM_CONT static vtkm::Id GetNumberOfValues(
129  const std::vector<vtkm::cont::internal::Buffer>& buffers)
130  {
131  return buffers[0].GetMetaData<vtkm::internal::ViewIndices>().NumberOfValues;
132  }
133 
134  VTKM_CONT static ReadPortalType CreateReadPortal(
135  const std::vector<vtkm::cont::internal::Buffer>& buffers,
137  vtkm::cont::Token& token)
138  {
139  vtkm::internal::ViewIndices indices = buffers[0].GetMetaData<vtkm::internal::ViewIndices>();
140  return ReadPortalType(SourceStorage::CreateReadPortal(SourceBuffers(buffers), device, token),
141  indices);
142  }
143 
144  VTKM_CONT static void Fill(const std::vector<vtkm::cont::internal::Buffer>& buffers,
145  const T& fillValue,
146  vtkm::Id startIndex,
147  vtkm::Id endIndex,
148  vtkm::cont::Token& token)
149  {
150  vtkm::internal::ViewIndices indices = buffers[0].GetMetaData<vtkm::internal::ViewIndices>();
151  vtkm::Id adjustedStartIndex = startIndex + indices.StartIndex;
152  vtkm::Id adjustedEndIndex = (endIndex < indices.NumberOfValues)
153  ? endIndex + indices.StartIndex
154  : indices.NumberOfValues + indices.StartIndex;
155  SourceStorage::Fill(
156  SourceBuffers(buffers), fillValue, adjustedStartIndex, adjustedEndIndex, token);
157  }
158 
159  VTKM_CONT static WritePortalType CreateWritePortal(
160  const std::vector<vtkm::cont::internal::Buffer>& buffers,
162  vtkm::cont::Token& token)
163  {
164  vtkm::internal::ViewIndices indices = buffers[0].GetMetaData<vtkm::internal::ViewIndices>();
165  return WritePortalType(SourceStorage::CreateWritePortal(SourceBuffers(buffers), device, token),
166  indices);
167  }
168 
169  VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers(
170  vtkm::Id startIndex = 0,
171  vtkm::Id numValues = 0,
172  const ArrayHandleType& array = ArrayHandleType{})
173  {
174  return vtkm::cont::internal::CreateBuffers(vtkm::internal::ViewIndices(startIndex, numValues),
175  array);
176  }
177 
178  VTKM_CONT static ArrayHandleType GetSourceArray(
179  const std::vector<vtkm::cont::internal::Buffer>& buffers)
180  {
181  return ArrayHandleType(SourceBuffers(buffers));
182  }
183 
184  VTKM_CONT static vtkm::Id GetStartIndex(const std::vector<vtkm::cont::internal::Buffer>& buffers)
185  {
186  return buffers[0].GetMetaData<vtkm::internal::ViewIndices>().StartIndex;
187  }
188 };
189 
190 } // namespace internal
191 
198 template <typename ArrayHandleType>
200  : public vtkm::cont::ArrayHandle<typename ArrayHandleType::ValueType,
201  StorageTagView<typename ArrayHandleType::StorageTag>>
202 {
203  VTKM_IS_ARRAY_HANDLE(ArrayHandleType);
204 
205 public:
209  (vtkm::cont::ArrayHandle<typename ArrayHandleType::ValueType,
211 
217  VTKM_CONT
218  ArrayHandleView(const ArrayHandleType& array, vtkm::Id startIndex, vtkm::Id numValues)
219  : Superclass(StorageType::CreateBuffers(startIndex, numValues, array))
220  {
221  }
222 
224  VTKM_CONT ArrayHandleType GetSourceArray() const
225  {
226  return this->GetStorage().GetSourceArray(this->GetBuffers());
227  }
228 
233  {
234  return this->GetStorage().GetStartIndex(this->GetBuffers());
235  }
236 };
237 
239 template <typename ArrayHandleType>
241  vtkm::Id startIndex,
242  vtkm::Id numValues)
243 {
244  VTKM_IS_ARRAY_HANDLE(ArrayHandleType);
245 
246  return ArrayHandleView<ArrayHandleType>(array, startIndex, numValues);
247 }
248 
249 namespace internal
250 {
251 
252 // Superclass will inherit the ArrayExtractComponentImplInefficient property if
253 // the sub-storage is inefficient (thus making everything inefficient).
254 template <typename StorageTag>
255 struct ArrayExtractComponentImpl<StorageTagView<StorageTag>>
256  : vtkm::cont::internal::ArrayExtractComponentImpl<StorageTag>
257 {
258  template <typename T>
259  using StrideArrayType =
261 
262  template <typename T>
263  StrideArrayType<T> operator()(
265  vtkm::IdComponent componentIndex,
266  vtkm::CopyFlag allowCopy) const
267  {
269  StrideArrayType<T> subArray =
270  ArrayExtractComponentImpl<StorageTag>{}(srcArray.GetSourceArray(), componentIndex, allowCopy);
271  // Narrow the array by adjusting the size and offset.
272  return StrideArrayType<T>(subArray.GetBasicArray(),
273  srcArray.GetNumberOfValues(),
274  subArray.GetStride(),
275  subArray.GetOffset() +
276  (subArray.GetStride() * srcArray.GetStartIndex()),
277  subArray.GetModulo(),
278  subArray.GetDivisor());
279  }
280 };
281 
282 } // namespace internal
283 
284 }
285 } // namespace vtkm::cont
286 
287 #endif //vtk_m_cont_ArrayHandleView_h
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:300
ArrayHandle.h
ArrayExtractComponent.h
vtkm::cont::ArrayHandle< ArrayHandleType::ValueType, StorageTagView< ArrayHandleType::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
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::cont::ArrayHandle< ArrayHandleType::ValueType, StorageTagView< ArrayHandleType::StorageTag > >::GetStorage
StorageType GetStorage() const
Get the storage.
Definition: ArrayHandle.h:421
vtkm::cont::make_ArrayHandleView
ArrayHandleView< ArrayHandleType > make_ArrayHandleView(const ArrayHandleType &array, vtkm::Id startIndex, vtkm::Id numValues)
Construct a vtkm::cont::ArrayHandleView from a source array.
Definition: ArrayHandleView.h:240
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
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::ArrayHandleView::GetSourceArray
ArrayHandleType GetSourceArray() const
Retrieve the full array being viewed.
Definition: ArrayHandleView.h:224
vtkm::cont::ArrayHandleView
Provided a windowed view into a vtkm::cont::ArrayHandle.
Definition: ArrayHandleView.h:199
Assert.h
vtkm::cont::ArrayHandleView::ArrayHandleView
ArrayHandleView(const ArrayHandleType &array, vtkm::Id startIndex, vtkm::Id numValues)
Create an ArrayHandleView over a provided source array.
Definition: ArrayHandleView.h:218
VTKM_STORAGE_NO_RESIZE
#define VTKM_STORAGE_NO_RESIZE
Definition: Storage.h:185
vtkm::cont::ArrayHandleStride
An ArrayHandle that accesses a basic array with strides and offsets.
Definition: ArrayHandleStride.h:332
vtkm::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
ArrayPortal.h
vtkm::cont::ArrayHandleView::GetStartIndex
vtkm::Id GetStartIndex() const
Retrieve the start index from the array being viewed.
Definition: ArrayHandleView.h:232
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::cont::ArrayHandleView::Superclass
typename vtkm::cont::detail::GetTypeInParentheses< void(vtkm::cont::ArrayHandle< typename ArrayHandleType::ValueType, StorageTagView< typename ArrayHandleType::StorageTag > >) >::type Superclass
Definition: ArrayHandleView.h:210
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::ArrayHandleView::StorageType
typename Superclass::StorageType StorageType
Definition: ArrayHandleView.h:210
vtkm::CopyFlag
CopyFlag
Identifier used to specify whether a function should deep copy data.
Definition: Flags.h:17
vtkm::cont::StorageTagView
Definition: ArrayHandleView.h:96
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:89