VTK-m  2.1
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 
192 template <typename ArrayHandleType>
194  : public vtkm::cont::ArrayHandle<typename ArrayHandleType::ValueType,
195  StorageTagView<typename ArrayHandleType::StorageTag>>
196 {
197  VTKM_IS_ARRAY_HANDLE(ArrayHandleType);
198 
199 public:
203  (vtkm::cont::ArrayHandle<typename ArrayHandleType::ValueType,
205 
206  VTKM_CONT
207  ArrayHandleView(const ArrayHandleType& array, vtkm::Id startIndex, vtkm::Id numValues)
208  : Superclass(StorageType::CreateBuffers(startIndex, numValues, array))
209  {
210  }
211 
212  VTKM_CONT ArrayHandleType GetSourceArray() const
213  {
214  return this->GetStorage().GetSourceArray(this->GetBuffers());
215  }
216 
218  {
219  return this->GetStorage().GetStartIndex(this->GetBuffers());
220  }
221 };
222 
223 template <typename ArrayHandleType>
225  vtkm::Id startIndex,
226  vtkm::Id numValues)
227 {
228  VTKM_IS_ARRAY_HANDLE(ArrayHandleType);
229 
230  return ArrayHandleView<ArrayHandleType>(array, startIndex, numValues);
231 }
232 
233 namespace internal
234 {
235 
236 // Superclass will inherit the ArrayExtractComponentImplInefficient property if
237 // the sub-storage is inefficient (thus making everything inefficient).
238 template <typename StorageTag>
239 struct ArrayExtractComponentImpl<StorageTagView<StorageTag>>
240  : vtkm::cont::internal::ArrayExtractComponentImpl<StorageTag>
241 {
242  template <typename T>
243  using StrideArrayType =
245 
246  template <typename T>
247  StrideArrayType<T> operator()(
249  vtkm::IdComponent componentIndex,
250  vtkm::CopyFlag allowCopy) const
251  {
253  StrideArrayType<T> subArray =
254  ArrayExtractComponentImpl<StorageTag>{}(srcArray.GetSourceArray(), componentIndex, allowCopy);
255  // Narrow the array by adjusting the size and offset.
256  return StrideArrayType<T>(subArray.GetBasicArray(),
257  srcArray.GetNumberOfValues(),
258  subArray.GetStride(),
259  subArray.GetOffset() +
260  (subArray.GetStride() * srcArray.GetStartIndex()),
261  subArray.GetModulo(),
262  subArray.GetDivisor());
263  }
264 };
265 
266 } // namespace internal
267 
268 }
269 } // namespace vtkm::cont
270 
271 #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:719
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:419
vtkm::cont::make_ArrayHandleView
ArrayHandleView< ArrayHandleType > make_ArrayHandleView(const ArrayHandleType &array, vtkm::Id startIndex, vtkm::Id numValues)
Definition: ArrayHandleView.h:224
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
Definition: ArrayHandleView.h:212
vtkm::cont::ArrayHandleView
Definition: ArrayHandleView.h:193
Assert.h
vtkm::cont::ArrayHandleView::ArrayHandleView
ArrayHandleView(const ArrayHandleType &array, vtkm::Id startIndex, vtkm::Id numValues)
Definition: ArrayHandleView.h:207
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
Definition: ArrayHandleView.h:217
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:204
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:204
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