Viskores  1.0
ArrayHandleView.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_ArrayHandleView_h
19 #define viskores_cont_ArrayHandleView_h
20 
21 #include <viskores/Assert.h>
22 
26 
27 namespace viskores
28 {
29 
30 namespace internal
31 {
32 
33 struct ViewIndices
34 {
35  viskores::Id StartIndex = 0;
36  viskores::Id NumberOfValues = 0;
37 
38  ViewIndices() = default;
39 
40  ViewIndices(viskores::Id start, viskores::Id numValues)
41  : StartIndex(start)
42  , NumberOfValues(numValues)
43  {
44  }
45 };
46 
47 template <typename TargetPortalType>
48 class ArrayPortalView
49 {
50  using Writable = viskores::internal::PortalSupportsSets<TargetPortalType>;
51 
52 public:
53  using ValueType = typename TargetPortalType::ValueType;
54 
56  ArrayPortalView() {}
57 
59  ArrayPortalView(const TargetPortalType& targetPortal, ViewIndices indices)
60  : TargetPortal(targetPortal)
61  , Indices(indices)
62  {
63  }
64 
65  template <typename OtherPortalType>
66  VISKORES_EXEC_CONT ArrayPortalView(const ArrayPortalView<OtherPortalType>& otherPortal)
67  : TargetPortal(otherPortal.GetTargetPortal())
68  , Indices(otherPortal.GetStartIndex(), otherPortal.GetNumberOfValues())
69  {
70  }
71 
73  viskores::Id GetNumberOfValues() const { return this->Indices.NumberOfValues; }
74 
76  ValueType Get(viskores::Id index) const
77  {
78  return this->TargetPortal.Get(index + this->GetStartIndex());
79  }
80 
81  template <typename Writable_ = Writable,
82  typename = typename std::enable_if<Writable_::value>::type>
83  VISKORES_EXEC_CONT void Set(viskores::Id index, const ValueType& value) const
84  {
85  this->TargetPortal.Set(index + this->GetStartIndex(), value);
86  }
87 
89  const TargetPortalType& GetTargetPortal() const { return this->TargetPortal; }
91  viskores::Id GetStartIndex() const { return this->Indices.StartIndex; }
92 
93 private:
94  TargetPortalType TargetPortal;
95  ViewIndices Indices;
96 };
97 
98 } // namespace internal
99 
100 namespace cont
101 {
102 
103 template <typename StorageTag>
104 struct VISKORES_ALWAYS_EXPORT StorageTagView
105 {
106 };
107 
108 namespace internal
109 {
110 
111 template <typename T, typename ST>
112 class Storage<T, StorageTagView<ST>>
113 {
114  using ArrayHandleType = viskores::cont::ArrayHandle<T, ST>;
115  using SourceStorage = Storage<T, ST>;
116 
117  static std::vector<viskores::cont::internal::Buffer> SourceBuffers(
118  const std::vector<viskores::cont::internal::Buffer>& buffers)
119  {
120  return std::vector<viskores::cont::internal::Buffer>(buffers.begin() + 1, buffers.end());
121  }
122 
123 public:
125 
126  using ReadPortalType =
127  viskores::internal::ArrayPortalView<typename ArrayHandleType::ReadPortalType>;
128  using WritePortalType =
129  viskores::internal::ArrayPortalView<typename ArrayHandleType::WritePortalType>;
130 
131  VISKORES_CONT static viskores::IdComponent GetNumberOfComponentsFlat(
132  const std::vector<viskores::cont::internal::Buffer>& buffers)
133  {
134  return SourceStorage::GetNumberOfComponentsFlat(SourceBuffers(buffers));
135  }
136 
137  VISKORES_CONT static viskores::Id GetNumberOfValues(
138  const std::vector<viskores::cont::internal::Buffer>& buffers)
139  {
140  return buffers[0].GetMetaData<viskores::internal::ViewIndices>().NumberOfValues;
141  }
142 
143  VISKORES_CONT static ReadPortalType CreateReadPortal(
144  const std::vector<viskores::cont::internal::Buffer>& buffers,
146  viskores::cont::Token& token)
147  {
148  viskores::internal::ViewIndices indices =
149  buffers[0].GetMetaData<viskores::internal::ViewIndices>();
150  return ReadPortalType(SourceStorage::CreateReadPortal(SourceBuffers(buffers), device, token),
151  indices);
152  }
153 
154  VISKORES_CONT static void Fill(const std::vector<viskores::cont::internal::Buffer>& buffers,
155  const T& fillValue,
156  viskores::Id startIndex,
157  viskores::Id endIndex,
158  viskores::cont::Token& token)
159  {
160  viskores::internal::ViewIndices indices =
161  buffers[0].GetMetaData<viskores::internal::ViewIndices>();
162  viskores::Id adjustedStartIndex = startIndex + indices.StartIndex;
163  viskores::Id adjustedEndIndex = (endIndex < indices.NumberOfValues)
164  ? endIndex + indices.StartIndex
165  : indices.NumberOfValues + indices.StartIndex;
166  SourceStorage::Fill(
167  SourceBuffers(buffers), fillValue, adjustedStartIndex, adjustedEndIndex, token);
168  }
169 
170  VISKORES_CONT static WritePortalType CreateWritePortal(
171  const std::vector<viskores::cont::internal::Buffer>& buffers,
173  viskores::cont::Token& token)
174  {
175  viskores::internal::ViewIndices indices =
176  buffers[0].GetMetaData<viskores::internal::ViewIndices>();
177  return WritePortalType(SourceStorage::CreateWritePortal(SourceBuffers(buffers), device, token),
178  indices);
179  }
180 
181  VISKORES_CONT static std::vector<viskores::cont::internal::Buffer> CreateBuffers(
182  viskores::Id startIndex = 0,
183  viskores::Id numValues = 0,
184  const ArrayHandleType& array = ArrayHandleType{})
185  {
186  return viskores::cont::internal::CreateBuffers(
187  viskores::internal::ViewIndices(startIndex, numValues), array);
188  }
189 
190  VISKORES_CONT static ArrayHandleType GetSourceArray(
191  const std::vector<viskores::cont::internal::Buffer>& buffers)
192  {
193  return ArrayHandleType(SourceBuffers(buffers));
194  }
195 
196  VISKORES_CONT static viskores::Id GetStartIndex(
197  const std::vector<viskores::cont::internal::Buffer>& buffers)
198  {
199  return buffers[0].GetMetaData<viskores::internal::ViewIndices>().StartIndex;
200  }
201 };
202 
203 } // namespace internal
204 
211 template <typename ArrayHandleType>
213  : public viskores::cont::ArrayHandle<typename ArrayHandleType::ValueType,
214  StorageTagView<typename ArrayHandleType::StorageTag>>
215 {
216  VISKORES_IS_ARRAY_HANDLE(ArrayHandleType);
217 
218 public:
222  (viskores::cont::ArrayHandle<typename ArrayHandleType::ValueType,
224 
231  ArrayHandleView(const ArrayHandleType& array, viskores::Id startIndex, viskores::Id numValues)
232  : Superclass(StorageType::CreateBuffers(startIndex, numValues, array))
233  {
234  }
235 
237  VISKORES_CONT ArrayHandleType GetSourceArray() const
238  {
239  return this->GetStorage().GetSourceArray(this->GetBuffers());
240  }
241 
246  {
247  return this->GetStorage().GetStartIndex(this->GetBuffers());
248  }
249 };
250 
252 template <typename ArrayHandleType>
254  viskores::Id startIndex,
255  viskores::Id numValues)
256 {
257  VISKORES_IS_ARRAY_HANDLE(ArrayHandleType);
258 
259  return ArrayHandleView<ArrayHandleType>(array, startIndex, numValues);
260 }
261 
262 namespace internal
263 {
264 
265 // Superclass will inherit the ArrayExtractComponentImplInefficient property if
266 // the sub-storage is inefficient (thus making everything inefficient).
267 template <typename StorageTag>
268 struct ArrayExtractComponentImpl<StorageTagView<StorageTag>>
269  : viskores::cont::internal::ArrayExtractComponentImpl<StorageTag>
270 {
271  template <typename T>
272  using StrideArrayType =
274 
275  template <typename T>
276  StrideArrayType<T> operator()(
278  viskores::IdComponent componentIndex,
279  viskores::CopyFlag allowCopy) const
280  {
282  StrideArrayType<T> subArray =
283  ArrayExtractComponentImpl<StorageTag>{}(srcArray.GetSourceArray(), componentIndex, allowCopy);
284  // Narrow the array by adjusting the size and offset.
285  return StrideArrayType<T>(subArray.GetBasicArray(),
286  srcArray.GetNumberOfValues(),
287  subArray.GetStride(),
288  subArray.GetOffset() +
289  (subArray.GetStride() * srcArray.GetStartIndex()),
290  subArray.GetModulo(),
291  subArray.GetDivisor());
292  }
293 };
294 
295 } // namespace internal
296 
297 }
298 } // namespace viskores::cont
299 
300 #endif //viskores_cont_ArrayHandleView_h
viskores::cont::ArrayHandle< ArrayHandleType::ValueType, StorageTagView< ArrayHandleType::StorageTag > >::GetStorage
StorageType GetStorage() const
Get the storage.
Definition: ArrayHandle.h:435
ArrayHandle.h
ArrayExtractComponent.h
VISKORES_IS_ARRAY_HANDLE
#define VISKORES_IS_ARRAY_HANDLE(T)
Checks that the given type is a viskores::cont::ArrayHandle.
Definition: ArrayHandle.h:145
viskores::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:313
Assert.h
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
viskores::cont::ArrayHandleStride
An ArrayHandle that accesses a basic array with strides and offsets.
Definition: ArrayHandleStride.h:343
viskores::cont::make_ArrayHandleView
ArrayHandleView< ArrayHandleType > make_ArrayHandleView(const ArrayHandleType &array, viskores::Id startIndex, viskores::Id numValues)
Construct a viskores::cont::ArrayHandleView from a source array.
Definition: ArrayHandleView.h:253
viskores::Id
viskores::Int64 Id
Base type to use to index arrays.
Definition: Types.h:235
VISKORES_CONT
#define VISKORES_CONT
Definition: ExportMacros.h:65
ArrayPortal.h
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
VISKORES_STORAGE_NO_RESIZE
#define VISKORES_STORAGE_NO_RESIZE
Definition: Storage.h:194
viskores::cont::ArrayHandleView
Provided a windowed view into a viskores::cont::ArrayHandle.
Definition: ArrayHandleView.h:212
viskores::cont::ArrayHandleView::ArrayHandleView
ArrayHandleView(const ArrayHandleType &array, viskores::Id startIndex, viskores::Id numValues)
Create an ArrayHandleView over a provided source array.
Definition: ArrayHandleView.h:231
viskores::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:66
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::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::ArrayHandleView::StorageType
typename Superclass::StorageType StorageType
Definition: ArrayHandleView.h:223
viskores::cont::StorageTagView
Definition: ArrayHandleView.h:104
viskores::cont::ArrayHandleView::GetStartIndex
viskores::Id GetStartIndex() const
Retrieve the start index from the array being viewed.
Definition: ArrayHandleView.h:245
viskores::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:43
viskores::cont::ArrayHandle< ArrayHandleType::ValueType, StorageTagView< ArrayHandleType::StorageTag > >::GetBuffers
const std::vector< viskores::cont::internal::Buffer > & GetBuffers() const
Returns the internal Buffer structures that hold the data.
Definition: ArrayHandle.h:738
viskores::cont::ArrayHandleView::Superclass
typename viskores::cont::detail::GetTypeInParentheses< void(viskores::cont::ArrayHandle< typename ArrayHandleType::ValueType, StorageTagView< typename ArrayHandleType::StorageTag > >) >::type Superclass
Definition: ArrayHandleView.h:223
viskores::cont::ArrayHandleView::GetSourceArray
ArrayHandleType GetSourceArray() const
Retrieve the full array being viewed.
Definition: ArrayHandleView.h:237