VTK-m  2.0
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::Id GetNumberOfValues(
123  const std::vector<vtkm::cont::internal::Buffer>& buffers)
124  {
125  return buffers[0].GetMetaData<vtkm::internal::ViewIndices>().NumberOfValues;
126  }
127 
128  VTKM_CONT static ReadPortalType CreateReadPortal(
129  const std::vector<vtkm::cont::internal::Buffer>& buffers,
131  vtkm::cont::Token& token)
132  {
133  vtkm::internal::ViewIndices indices = buffers[0].GetMetaData<vtkm::internal::ViewIndices>();
134  return ReadPortalType(SourceStorage::CreateReadPortal(SourceBuffers(buffers), device, token),
135  indices);
136  }
137 
138  VTKM_CONT static void Fill(const std::vector<vtkm::cont::internal::Buffer>& buffers,
139  const T& fillValue,
140  vtkm::Id startIndex,
141  vtkm::Id endIndex,
142  vtkm::cont::Token& token)
143  {
144  vtkm::internal::ViewIndices indices = buffers[0].GetMetaData<vtkm::internal::ViewIndices>();
145  vtkm::Id adjustedStartIndex = startIndex + indices.StartIndex;
146  vtkm::Id adjustedEndIndex = (endIndex < indices.NumberOfValues)
147  ? endIndex + indices.StartIndex
148  : indices.NumberOfValues + indices.StartIndex;
149  SourceStorage::Fill(
150  SourceBuffers(buffers), fillValue, adjustedStartIndex, adjustedEndIndex, token);
151  }
152 
153  VTKM_CONT static WritePortalType CreateWritePortal(
154  const std::vector<vtkm::cont::internal::Buffer>& buffers,
156  vtkm::cont::Token& token)
157  {
158  vtkm::internal::ViewIndices indices = buffers[0].GetMetaData<vtkm::internal::ViewIndices>();
159  return WritePortalType(SourceStorage::CreateWritePortal(SourceBuffers(buffers), device, token),
160  indices);
161  }
162 
163  VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers(
164  vtkm::Id startIndex = 0,
165  vtkm::Id numValues = 0,
166  const ArrayHandleType& array = ArrayHandleType{})
167  {
168  return vtkm::cont::internal::CreateBuffers(vtkm::internal::ViewIndices(startIndex, numValues),
169  array);
170  }
171 
172  VTKM_CONT static ArrayHandleType GetSourceArray(
173  const std::vector<vtkm::cont::internal::Buffer>& buffers)
174  {
175  return ArrayHandleType(SourceBuffers(buffers));
176  }
177 
178  VTKM_CONT static vtkm::Id GetStartIndex(const std::vector<vtkm::cont::internal::Buffer>& buffers)
179  {
180  return buffers[0].GetMetaData<vtkm::internal::ViewIndices>().StartIndex;
181  }
182 };
183 
184 } // namespace internal
185 
186 template <typename ArrayHandleType>
188  : public vtkm::cont::ArrayHandle<typename ArrayHandleType::ValueType,
189  StorageTagView<typename ArrayHandleType::StorageTag>>
190 {
191  VTKM_IS_ARRAY_HANDLE(ArrayHandleType);
192 
193 public:
197  (vtkm::cont::ArrayHandle<typename ArrayHandleType::ValueType,
199 
200 private:
201  using StorageType = vtkm::cont::internal::Storage<ValueType, StorageTag>;
202 
203 public:
204  VTKM_CONT
205  ArrayHandleView(const ArrayHandleType& array, vtkm::Id startIndex, vtkm::Id numValues)
206  : Superclass(StorageType::CreateBuffers(startIndex, numValues, array))
207  {
208  }
209 
210  VTKM_CONT ArrayHandleType GetSourceArray() const
211  {
212  return this->GetStorage().GetSourceArray(this->GetBuffers());
213  }
214 
216  {
217  return this->GetStorage().GetStartIndex(this->GetBuffers());
218  }
219 };
220 
221 template <typename ArrayHandleType>
223  vtkm::Id startIndex,
224  vtkm::Id numValues)
225 {
226  VTKM_IS_ARRAY_HANDLE(ArrayHandleType);
227 
228  return ArrayHandleView<ArrayHandleType>(array, startIndex, numValues);
229 }
230 
231 namespace internal
232 {
233 
234 // Superclass will inherit the ArrayExtractComponentImplInefficient property if
235 // the sub-storage is inefficient (thus making everything inefficient).
236 template <typename StorageTag>
237 struct ArrayExtractComponentImpl<StorageTagView<StorageTag>>
238  : vtkm::cont::internal::ArrayExtractComponentImpl<StorageTag>
239 {
240  template <typename T>
241  using StrideArrayType =
243 
244  template <typename T>
245  StrideArrayType<T> operator()(
247  vtkm::IdComponent componentIndex,
248  vtkm::CopyFlag allowCopy) const
249  {
251  StrideArrayType<T> subArray =
252  ArrayExtractComponentImpl<StorageTag>{}(srcArray.GetSourceArray(), componentIndex, allowCopy);
253  // Narrow the array by adjusting the size and offset.
254  return StrideArrayType<T>(subArray.GetBasicArray(),
255  srcArray.GetNumberOfValues(),
256  subArray.GetStride(),
257  subArray.GetOffset() +
258  (subArray.GetStride() * srcArray.GetStartIndex()),
259  subArray.GetModulo(),
260  subArray.GetDivisor());
261  }
262 };
263 
264 } // namespace internal
265 
266 }
267 } // namespace vtkm::cont
268 
269 #endif //vtk_m_cont_ArrayHandleView_h
vtkm::cont::ArrayHandle< ArrayHandleType::ValueType, StorageTagView< ArrayHandleType::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
ArrayExtractComponent.h
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::cont::make_ArrayHandleView
ArrayHandleView< ArrayHandleType > make_ArrayHandleView(const ArrayHandleType &array, vtkm::Id startIndex, vtkm::Id numValues)
Definition: ArrayHandleView.h:222
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::ArrayHandleView::VTKM_IS_ARRAY_HANDLE
VTKM_IS_ARRAY_HANDLE(ArrayHandleType)
vtkm::cont::ArrayHandleView
Definition: ArrayHandleView.h:187
vtkm::cont::ArrayHandleView::ArrayHandleView
VTKM_CONT ArrayHandleView(const ArrayHandleType &array, vtkm::Id startIndex, vtkm::Id numValues)
Definition: ArrayHandleView.h:205
Assert.h
vtkm::cont::ArrayHandle< ArrayHandleType::ValueType, StorageTagView< ArrayHandleType::StorageTag > >::GetStorage
VTKM_CONT StorageType GetStorage() const
Get the storage.
Definition: ArrayHandle.h:401
VTKM_STORAGE_NO_RESIZE
#define VTKM_STORAGE_NO_RESIZE
Definition: Storage.h:185
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
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::ArrayHandleView::VTKM_ARRAY_HANDLE_SUBCLASS
VTKM_ARRAY_HANDLE_SUBCLASS(ArrayHandleView,(ArrayHandleView< ArrayHandleType >),(vtkm::cont::ArrayHandle< typename ArrayHandleType::ValueType, StorageTagView< typename ArrayHandleType::StorageTag >>))
vtkm::cont::ArrayHandleView::StorageType
vtkm::cont::internal::Storage< ValueType, StorageTag > StorageType
Definition: ArrayHandleView.h:201
ArrayPortal.h
vtkm::cont::ArrayHandleView::GetSourceArray
VTKM_CONT ArrayHandleType GetSourceArray() const
Definition: ArrayHandleView.h:210
VTKM_IS_ARRAY_HANDLE
#define VTKM_IS_ARRAY_HANDLE(T)
Definition: ArrayHandle.h:132
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::cont::DeviceAdapterId
Definition: DeviceAdapterTag.h:52
vtkm::CopyFlag
CopyFlag
Definition: Flags.h:16
vtkm::cont::ArrayHandleView::GetStartIndex
VTKM_CONT vtkm::Id GetStartIndex() const
Definition: ArrayHandleView.h:215
vtkm::cont::StorageTagView
Definition: ArrayHandleView.h:96
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:92