VTK-m  2.0
ArrayHandleConcatenate.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_ArrayHandleConcatenate_h
11 #define vtk_m_cont_ArrayHandleConcatenate_h
12 
13 #include <vtkm/StaticAssert.h>
14 
15 #include <vtkm/cont/ArrayHandle.h>
16 
17 namespace vtkm
18 {
19 namespace internal
20 {
21 
22 template <typename PortalType1, typename PortalType2>
23 class VTKM_ALWAYS_EXPORT ArrayPortalConcatenate
24 {
25  using WritableP1 = vtkm::internal::PortalSupportsSets<PortalType1>;
26  using WritableP2 = vtkm::internal::PortalSupportsSets<PortalType2>;
27  using Writable = std::integral_constant<bool, WritableP1::value && WritableP2::value>;
28 
29 public:
30  using ValueType = typename PortalType1::ValueType;
31 
34  ArrayPortalConcatenate()
35  : portal1()
36  , portal2()
37  {
38  }
39 
42  ArrayPortalConcatenate(const PortalType1& p1, const PortalType2& p2)
43  : portal1(p1)
44  , portal2(p2)
45  {
46  }
47 
48  // Copy constructor
50  template <typename OtherP1, typename OtherP2>
51  VTKM_EXEC_CONT ArrayPortalConcatenate(const ArrayPortalConcatenate<OtherP1, OtherP2>& src)
52  : portal1(src.GetPortal1())
53  , portal2(src.GetPortal2())
54  {
55  }
56 
58  vtkm::Id GetNumberOfValues() const
59  {
60  return this->portal1.GetNumberOfValues() + this->portal2.GetNumberOfValues();
61  }
62 
65  ValueType Get(vtkm::Id index) const
66  {
67  if (index < this->portal1.GetNumberOfValues())
68  {
69  return this->portal1.Get(index);
70  }
71  else
72  {
73  return this->portal2.Get(index - this->portal1.GetNumberOfValues());
74  }
75  }
76 
78  template <typename Writable_ = Writable,
79  typename = typename std::enable_if<Writable_::value>::type>
80  VTKM_EXEC_CONT void Set(vtkm::Id index, const ValueType& value) const
81  {
82  if (index < this->portal1.GetNumberOfValues())
83  {
84  this->portal1.Set(index, value);
85  }
86  else
87  {
88  this->portal2.Set(index - this->portal1.GetNumberOfValues(), value);
89  }
90  }
91 
93  const PortalType1& GetPortal1() const { return this->portal1; }
94 
96  const PortalType2& GetPortal2() const { return this->portal2; }
97 
98 private:
99  PortalType1 portal1;
100  PortalType2 portal2;
101 }; // class ArrayPortalConcatenate
102 
103 }
104 } // namespace vtkm::internal
105 
106 namespace vtkm
107 {
108 namespace cont
109 {
110 
111 template <typename StorageTag1, typename StorageTag2>
113 {
114 };
115 
116 namespace internal
117 {
118 
119 template <typename T, typename ST1, typename ST2>
120 class Storage<T, StorageTagConcatenate<ST1, ST2>>
121 {
122  using SourceStorage1 = vtkm::cont::internal::Storage<T, ST1>;
123  using SourceStorage2 = vtkm::cont::internal::Storage<T, ST2>;
124 
125  using ArrayHandleType1 = vtkm::cont::ArrayHandle<T, ST1>;
126  using ArrayHandleType2 = vtkm::cont::ArrayHandle<T, ST2>;
127 
128  struct Info
129  {
130  std::size_t NumBuffers1;
131  std::size_t NumBuffers2;
132  };
133 
134  VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> Buffers1(
135  const std::vector<vtkm::cont::internal::Buffer>& buffers)
136  {
137  Info info = buffers[0].GetMetaData<Info>();
138  return std::vector<vtkm::cont::internal::Buffer>(buffers.begin() + 1,
139  buffers.begin() + 1 + info.NumBuffers1);
140  }
141 
142  VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> Buffers2(
143  const std::vector<vtkm::cont::internal::Buffer>& buffers)
144  {
145  Info info = buffers[0].GetMetaData<Info>();
146  return std::vector<vtkm::cont::internal::Buffer>(buffers.begin() + 1 + info.NumBuffers1,
147  buffers.end());
148  }
149 
150 public:
152 
153  using ReadPortalType =
154  vtkm::internal::ArrayPortalConcatenate<typename SourceStorage1::ReadPortalType,
155  typename SourceStorage2::ReadPortalType>;
156  using WritePortalType =
157  vtkm::internal::ArrayPortalConcatenate<typename SourceStorage1::WritePortalType,
158  typename SourceStorage2::WritePortalType>;
159 
160  VTKM_CONT static vtkm::Id GetNumberOfValues(
161  const std::vector<vtkm::cont::internal::Buffer>& buffers)
162  {
163  return (SourceStorage1::GetNumberOfValues(Buffers1(buffers)) +
164  SourceStorage2::GetNumberOfValues(Buffers2(buffers)));
165  }
166 
167  VTKM_CONT static void Fill(const std::vector<vtkm::cont::internal::Buffer>& buffers,
168  const T& fillValue,
169  vtkm::Id startIndex,
170  vtkm::Id endIndex,
171  vtkm::cont::Token& token)
172  {
173  vtkm::Id size1 = SourceStorage1::GetNumberOfValues(Buffers1(buffers));
174  if ((startIndex < size1) && (endIndex <= size1))
175  {
176  SourceStorage1::Fill(Buffers1(buffers), fillValue, startIndex, endIndex, token);
177  }
178  else if (startIndex < size1) // && (endIndex > size1)
179  {
180  SourceStorage1::Fill(Buffers1(buffers), fillValue, startIndex, size1, token);
181  SourceStorage2::Fill(Buffers2(buffers), fillValue, 0, endIndex - size1, token);
182  }
183  else // startIndex >= size1
184  {
185  SourceStorage2::Fill(
186  Buffers2(buffers), fillValue, startIndex - size1, endIndex - size1, token);
187  }
188  }
189 
190  VTKM_CONT static ReadPortalType CreateReadPortal(
191  const std::vector<vtkm::cont::internal::Buffer>& buffers,
193  vtkm::cont::Token& token)
194  {
195  return ReadPortalType(SourceStorage1::CreateReadPortal(Buffers1(buffers), device, token),
196  SourceStorage2::CreateReadPortal(Buffers2(buffers), device, token));
197  }
198 
199  VTKM_CONT static WritePortalType CreateWritePortal(
200  const std::vector<vtkm::cont::internal::Buffer>& buffers,
202  vtkm::cont::Token& token)
203  {
204  return WritePortalType(SourceStorage1::CreateWritePortal(Buffers1(buffers), device, token),
205  SourceStorage2::CreateWritePortal(Buffers2(buffers), device, token));
206  }
207 
208  VTKM_CONT static auto CreateBuffers(const ArrayHandleType1& array1 = ArrayHandleType1{},
209  const ArrayHandleType2& array2 = ArrayHandleType2{})
210  -> decltype(vtkm::cont::internal::CreateBuffers())
211  {
212  Info info;
213  info.NumBuffers1 = array1.GetBuffers().size();
214  info.NumBuffers2 = array2.GetBuffers().size();
215  return vtkm::cont::internal::CreateBuffers(info, array1, array2);
216  }
217 
218  VTKM_CONT static const ArrayHandleType1 GetArray1(
219  const std::vector<vtkm::cont::internal::Buffer>& buffers)
220  {
221  return ArrayHandleType1(Buffers1(buffers));
222  }
223 
224  VTKM_CONT static const ArrayHandleType2 GetArray2(
225  const std::vector<vtkm::cont::internal::Buffer>& buffers)
226  {
227  return ArrayHandleType2(Buffers2(buffers));
228  }
229 }; // class Storage
230 
231 }
232 }
233 } // namespace vtkm::cont::internal
234 
235 namespace vtkm
236 {
237 namespace cont
238 {
239 
240 template <typename ArrayHandleType1, typename ArrayHandleType2>
242  : public vtkm::cont::ArrayHandle<typename ArrayHandleType1::ValueType,
243  StorageTagConcatenate<typename ArrayHandleType1::StorageTag,
244  typename ArrayHandleType2::StorageTag>>
245 {
246 public:
250  (vtkm::cont::ArrayHandle<typename ArrayHandleType1::ValueType,
251  StorageTagConcatenate<typename ArrayHandleType1::StorageTag,
252  typename ArrayHandleType2::StorageTag>>));
253 
254 protected:
255  using StorageType = vtkm::cont::internal::Storage<ValueType, StorageTag>;
256 
257 public:
258  VTKM_CONT
259  ArrayHandleConcatenate(const ArrayHandleType1& array1, const ArrayHandleType2& array2)
260  : Superclass(StorageType::CreateBuffers(array1, array2))
261  {
262  }
263 };
264 
265 template <typename ArrayHandleType1, typename ArrayHandleType2>
267  const ArrayHandleType1& array1,
268  const ArrayHandleType2& array2)
269 {
271 }
272 }
273 } // namespace vtkm::cont
274 
275 //=============================================================================
276 // Specializations of serialization related classes
278 namespace vtkm
279 {
280 namespace cont
281 {
282 
283 template <typename AH1, typename AH2>
284 struct SerializableTypeString<vtkm::cont::ArrayHandleConcatenate<AH1, AH2>>
285 {
286  static VTKM_CONT const std::string& Get()
287  {
288  static std::string name = "AH_Concatenate<" + SerializableTypeString<AH1>::Get() + "," +
290  return name;
291  }
292 };
293 
294 template <typename T, typename ST1, typename ST2>
295 struct SerializableTypeString<
296  vtkm::cont::ArrayHandle<T, vtkm::cont::StorageTagConcatenate<ST1, ST2>>>
297  : SerializableTypeString<vtkm::cont::ArrayHandleConcatenate<vtkm::cont::ArrayHandle<T, ST1>,
298  vtkm::cont::ArrayHandle<T, ST2>>>
299 {
300 };
301 }
302 } // vtkm::cont
303 
304 namespace mangled_diy_namespace
305 {
306 
307 template <typename AH1, typename AH2>
308 struct Serialization<vtkm::cont::ArrayHandleConcatenate<AH1, AH2>>
309 {
310 private:
313 
314 public:
315  static VTKM_CONT void save(BinaryBuffer& bb, const BaseType& obj)
316  {
317  auto storage = obj.GetStorage();
318  vtkmdiy::save(bb, storage.GetArray1());
319  vtkmdiy::save(bb, storage.GetArray2());
320  }
321 
322  static VTKM_CONT void load(BinaryBuffer& bb, BaseType& obj)
323  {
324  AH1 array1;
325  AH2 array2;
326 
327  vtkmdiy::load(bb, array1);
328  vtkmdiy::load(bb, array2);
329 
330  obj = vtkm::cont::make_ArrayHandleConcatenate(array1, array2);
331  }
332 };
333 
334 template <typename T, typename ST1, typename ST2>
335 struct Serialization<vtkm::cont::ArrayHandle<T, vtkm::cont::StorageTagConcatenate<ST1, ST2>>>
336  : Serialization<vtkm::cont::ArrayHandleConcatenate<vtkm::cont::ArrayHandle<T, ST1>,
337  vtkm::cont::ArrayHandle<T, ST2>>>
338 {
339 };
340 
341 } // diy
343 
344 #endif //vtk_m_cont_ArrayHandleConcatenate_h
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:283
ArrayHandle.h
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::cont::ArrayHandleConcatenate
Definition: ArrayHandleConcatenate.h:241
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_STORAGE_NO_RESIZE
#define VTKM_STORAGE_NO_RESIZE
Definition: Storage.h:185
mangled_diy_namespace
Definition: Particle.h:331
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
vtkm::exec::arg::load
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC T load(const U &u, vtkm::Id v)
Definition: FetchTagArrayDirectIn.h:36
vtkm::cont::ArrayHandleConcatenate::StorageType
vtkm::cont::internal::Storage< ValueType, StorageTag > StorageType
Definition: ArrayHandleConcatenate.h:255
vtkm::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
vtkm::cont::StorageTagConcatenate
Definition: ArrayHandleConcatenate.h:112
vtkm::cont::LogLevel::Info
@ Info
Information messages (detected hardware, etc) and temporary debugging output.
vtkm::cont::ArrayHandleConcatenate::ArrayHandleConcatenate
VTKM_CONT ArrayHandleConcatenate(const ArrayHandleType1 &array1, const ArrayHandleType2 &array2)
Definition: ArrayHandleConcatenate.h:259
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::cont::make_ArrayHandleConcatenate
VTKM_CONT ArrayHandleConcatenate< ArrayHandleType1, ArrayHandleType2 > make_ArrayHandleConcatenate(const ArrayHandleType1 &array1, const ArrayHandleType2 &array2)
Definition: ArrayHandleConcatenate.h:266
vtkm::cont::DeviceAdapterId
Definition: DeviceAdapterTag.h:52
StaticAssert.h
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:92
vtkm::cont::ArrayHandleConcatenate::VTKM_ARRAY_HANDLE_SUBCLASS
VTKM_ARRAY_HANDLE_SUBCLASS(ArrayHandleConcatenate,(ArrayHandleConcatenate< ArrayHandleType1, ArrayHandleType2 >),(vtkm::cont::ArrayHandle< typename ArrayHandleType1::ValueType, StorageTagConcatenate< typename ArrayHandleType1::StorageTag, typename ArrayHandleType2::StorageTag >>))
VTKM_SUPPRESS_EXEC_WARNINGS
#define VTKM_SUPPRESS_EXEC_WARNINGS
Definition: ExportMacros.h:53