VTK-m  2.0
ArrayHandleImplicit.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_ArrayHandleImplicit_h
11 #define vtk_m_cont_ArrayHandleImplicit_h
12 
13 #include <vtkm/cont/ArrayHandle.h>
14 
15 #include <vtkmstd/is_trivial.h>
16 
17 namespace vtkm
18 {
19 
20 namespace internal
21 {
22 
32 template <class FunctorType_>
33 class VTKM_ALWAYS_EXPORT ArrayPortalImplicit
34 {
35 public:
36  using FunctorType = FunctorType_;
37  using ValueType = decltype(FunctorType{}(vtkm::Id{}));
38 
41  ArrayPortalImplicit()
42  : Functor()
43  , NumberOfValues(0)
44  {
45  }
46 
49  ArrayPortalImplicit(FunctorType f, vtkm::Id numValues)
50  : Functor(f)
51  , NumberOfValues(numValues)
52  {
53  }
54 
56  const FunctorType& GetFunctor() const { return this->Functor; }
57 
59  vtkm::Id GetNumberOfValues() const { return this->NumberOfValues; }
60 
63  ValueType Get(vtkm::Id index) const { return this->Functor(index); }
64 
65 private:
66  FunctorType Functor;
67  vtkm::Id NumberOfValues;
68 };
69 
70 } // namespace internal
71 
72 namespace cont
73 {
74 
85 template <class ArrayPortalType>
87 {
88  using PortalType = ArrayPortalType;
89 };
90 
91 namespace internal
92 {
93 
96 template <typename PortalType>
97 VTKM_CONT inline std::vector<vtkm::cont::internal::Buffer> PortalToArrayHandleImplicitBuffers(
98  const PortalType& portal)
99 {
100  std::vector<vtkm::cont::internal::Buffer> buffers(1);
101  buffers[0].SetMetaData(portal);
102  return buffers;
103 }
104 
107 template <typename FunctorType>
108 VTKM_CONT inline std::vector<vtkm::cont::internal::Buffer> FunctorToArrayHandleImplicitBuffers(
109  const FunctorType& functor,
110  vtkm::Id numValues)
111 {
112  return PortalToArrayHandleImplicitBuffers(
113  vtkm::internal::ArrayPortalImplicit<FunctorType>(functor, numValues));
114 }
115 
116 template <class ArrayPortalType>
117 struct VTKM_ALWAYS_EXPORT
118  Storage<typename ArrayPortalType::ValueType, StorageTagImplicit<ArrayPortalType>>
119 {
120  VTKM_IS_TRIVIALLY_COPYABLE(ArrayPortalType);
121 
124 
125  using ReadPortalType = ArrayPortalType;
126 
127  VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers()
128  {
129  return vtkm::cont::internal::PortalToArrayHandleImplicitBuffers(ArrayPortalType{});
130  }
131 
132  VTKM_CONT static vtkm::Id GetNumberOfValues(
133  const std::vector<vtkm::cont::internal::Buffer>& buffers)
134  {
135  return buffers[0].GetMetaData<ArrayPortalType>().GetNumberOfValues();
136  }
137 
138  VTKM_CONT static ReadPortalType CreateReadPortal(
139  const std::vector<vtkm::cont::internal::Buffer>& buffers,
142  {
143  return buffers[0].GetMetaData<ArrayPortalType>();
144  }
145 };
146 
147 } // namespace internal
148 
149 namespace detail
150 {
151 
154 template <typename FunctorType>
155 struct ArrayHandleImplicitTraits
156 {
157  using ValueType = decltype(FunctorType{}(vtkm::Id{}));
158  using PortalType = vtkm::internal::ArrayPortalImplicit<FunctorType>;
161  using StorageType = vtkm::cont::internal::Storage<ValueType, StorageTag>;
162 };
163 
164 } // namespace detail
165 
173 template <class FunctorType>
175  : public detail::ArrayHandleImplicitTraits<FunctorType>::Superclass
176 {
177 private:
178  using ArrayTraits = typename detail::ArrayHandleImplicitTraits<FunctorType>;
179  using PortalType = typename ArrayTraits::PortalType;
180 
181 public:
184  (typename ArrayTraits::Superclass));
185 
186  VTKM_CONT
187  ArrayHandleImplicit(FunctorType functor, vtkm::Id length)
188  : Superclass(internal::PortalToArrayHandleImplicitBuffers(PortalType(functor, length)))
189  {
190  }
191 };
192 
196 
197 template <typename FunctorType>
199  vtkm::Id length)
200 {
201  return ArrayHandleImplicit<FunctorType>(functor, length);
202 }
203 }
204 } // namespace vtkm::cont
205 
206 //=============================================================================
207 // Specializations of serialization related classes
209 namespace vtkm
210 {
211 namespace cont
212 {
213 
214 template <typename Functor>
215 struct SerializableTypeString<vtkm::cont::ArrayHandleImplicit<Functor>>
216 {
217  static VTKM_CONT const std::string& Get()
218  {
219  static std::string name = "AH_Implicit<" + SerializableTypeString<Functor>::Get() + ">";
220  return name;
221  }
222 };
223 
224 template <typename Functor>
225 struct SerializableTypeString<vtkm::cont::ArrayHandle<
226  typename vtkm::cont::detail::ArrayHandleImplicitTraits<Functor>::ValueType,
227  vtkm::cont::StorageTagImplicit<vtkm::internal::ArrayPortalImplicit<Functor>>>>
228  : SerializableTypeString<vtkm::cont::ArrayHandleImplicit<Functor>>
229 {
230 };
231 }
232 } // vtkm::cont
233 
234 namespace mangled_diy_namespace
235 {
236 
237 template <typename Functor>
238 struct Serialization<vtkm::cont::ArrayHandleImplicit<Functor>>
239 {
240 private:
243 
244 public:
245  static VTKM_CONT void save(BinaryBuffer& bb, const BaseType& obj)
246  {
247  vtkmdiy::save(bb, obj.GetNumberOfValues());
248  vtkmdiy::save(bb, obj.ReadPortal().GetFunctor());
249  }
250 
251  static VTKM_CONT void load(BinaryBuffer& bb, BaseType& obj)
252  {
253  vtkm::Id count = 0;
254  vtkmdiy::load(bb, count);
255 
256  Functor functor;
257  vtkmdiy::load(bb, functor);
258 
259  obj = vtkm::cont::make_ArrayHandleImplicit(functor, count);
260  }
261 };
262 
263 template <typename Functor>
264 struct Serialization<vtkm::cont::ArrayHandle<
265  typename vtkm::cont::detail::ArrayHandleImplicitTraits<Functor>::ValueType,
266  vtkm::cont::StorageTagImplicit<vtkm::internal::ArrayPortalImplicit<Functor>>>>
267  : Serialization<vtkm::cont::ArrayHandleImplicit<Functor>>
268 {
269 };
270 
271 } // diy
273 
274 #endif //vtk_m_cont_ArrayHandleImplicit_h
vtkm::cont::StorageTagImplicit::PortalType
ArrayPortalType PortalType
Definition: ArrayHandleImplicit.h:88
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::ArrayHandleImplicit::ArrayHandleImplicit
VTKM_CONT ArrayHandleImplicit(FunctorType functor, vtkm::Id length)
Definition: ArrayHandleImplicit.h:187
VTKM_ARRAY_HANDLE_SUBCLASS
#define VTKM_ARRAY_HANDLE_SUBCLASS(classname, fullclasstype, superclass)
Macro to make default methods in ArrayHandle subclasses.
Definition: ArrayHandle.h:226
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::cont::StorageTagImplicit
An implementation for read-only implicit arrays.
Definition: ArrayHandleImplicit.h:86
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::cont::ArrayHandleImplicit::ArrayTraits
typename detail::ArrayHandleImplicitTraits< FunctorType > ArrayTraits
Definition: ArrayHandleImplicit.h:178
vtkm::cont::ArrayHandleImplicit::PortalType
typename ArrayTraits::PortalType PortalType
Definition: ArrayHandleImplicit.h:179
vtkm::cont::make_ArrayHandleImplicit
VTKM_CONT vtkm::cont::ArrayHandleImplicit< FunctorType > make_ArrayHandleImplicit(FunctorType functor, vtkm::Id length)
make_ArrayHandleImplicit is convenience function to generate an ArrayHandleImplicit.
Definition: ArrayHandleImplicit.h:198
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::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
VTKM_STORAGE_NO_WRITE_PORTAL
#define VTKM_STORAGE_NO_WRITE_PORTAL
Definition: Storage.h:198
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::cont::DeviceAdapterId
Definition: DeviceAdapterTag.h:52
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:92
VTKM_SUPPRESS_EXEC_WARNINGS
#define VTKM_SUPPRESS_EXEC_WARNINGS
Definition: ExportMacros.h:53
vtkm::cont::ArrayHandleImplicit
An ArrayHandle that computes values on the fly.
Definition: ArrayHandleImplicit.h:174