VTK-m  2.2
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::IdComponent GetNumberOfComponentsFlat(
133  const std::vector<vtkm::cont::internal::Buffer>&)
134  {
136  }
137 
138  VTKM_CONT static vtkm::Id GetNumberOfValues(
139  const std::vector<vtkm::cont::internal::Buffer>& buffers)
140  {
141  return buffers[0].GetMetaData<ArrayPortalType>().GetNumberOfValues();
142  }
143 
144  VTKM_CONT static ReadPortalType CreateReadPortal(
145  const std::vector<vtkm::cont::internal::Buffer>& buffers,
148  {
149  return buffers[0].GetMetaData<ArrayPortalType>();
150  }
151 };
152 
153 } // namespace internal
154 
155 namespace detail
156 {
157 
160 template <typename FunctorType>
161 struct ArrayHandleImplicitTraits
162 {
163  using ValueType = decltype(FunctorType{}(vtkm::Id{}));
164  using PortalType = vtkm::internal::ArrayPortalImplicit<FunctorType>;
167 };
168 
169 } // namespace detail
170 
178 template <class FunctorType>
180  : public detail::ArrayHandleImplicitTraits<FunctorType>::Superclass
181 {
182 private:
183  using ArrayTraits = typename detail::ArrayHandleImplicitTraits<FunctorType>;
184  using PortalType = typename ArrayTraits::PortalType;
185 
186 public:
189  (typename ArrayTraits::Superclass));
190 
191  VTKM_CONT
192  ArrayHandleImplicit(FunctorType functor, vtkm::Id length)
193  : Superclass(internal::PortalToArrayHandleImplicitBuffers(PortalType(functor, length)))
194  {
195  }
196 };
197 
201 
202 template <typename FunctorType>
204  vtkm::Id length)
205 {
206  return ArrayHandleImplicit<FunctorType>(functor, length);
207 }
208 }
209 } // namespace vtkm::cont
210 
211 //=============================================================================
212 // Specializations of serialization related classes
214 namespace vtkm
215 {
216 namespace cont
217 {
218 
219 template <typename Functor>
220 struct SerializableTypeString<vtkm::cont::ArrayHandleImplicit<Functor>>
221 {
222  static VTKM_CONT const std::string& Get()
223  {
224  static std::string name = "AH_Implicit<" + SerializableTypeString<Functor>::Get() + ">";
225  return name;
226  }
227 };
228 
229 template <typename Functor>
230 struct SerializableTypeString<vtkm::cont::ArrayHandle<
231  typename vtkm::cont::detail::ArrayHandleImplicitTraits<Functor>::ValueType,
232  vtkm::cont::StorageTagImplicit<vtkm::internal::ArrayPortalImplicit<Functor>>>>
233  : SerializableTypeString<vtkm::cont::ArrayHandleImplicit<Functor>>
234 {
235 };
236 }
237 } // vtkm::cont
238 
239 namespace mangled_diy_namespace
240 {
241 
242 template <typename Functor>
243 struct Serialization<vtkm::cont::ArrayHandleImplicit<Functor>>
244 {
245 private:
248 
249 public:
250  static VTKM_CONT void save(BinaryBuffer& bb, const BaseType& obj)
251  {
252  vtkmdiy::save(bb, obj.GetNumberOfValues());
253  vtkmdiy::save(bb, obj.ReadPortal().GetFunctor());
254  }
255 
256  static VTKM_CONT void load(BinaryBuffer& bb, BaseType& obj)
257  {
258  vtkm::Id count = 0;
259  vtkmdiy::load(bb, count);
260 
261  Functor functor;
262  vtkmdiy::load(bb, functor);
263 
264  obj = vtkm::cont::make_ArrayHandleImplicit(functor, count);
265  }
266 };
267 
268 template <typename Functor>
269 struct Serialization<vtkm::cont::ArrayHandle<
270  typename vtkm::cont::detail::ArrayHandleImplicitTraits<Functor>::ValueType,
271  vtkm::cont::StorageTagImplicit<vtkm::internal::ArrayPortalImplicit<Functor>>>>
272  : Serialization<vtkm::cont::ArrayHandleImplicit<Functor>>
273 {
274 };
275 
276 } // diy
278 
279 #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:300
ArrayHandle.h
vtkm::cont::ArrayHandleImplicit::Superclass
typename vtkm::cont::detail::GetTypeInParentheses< void(typename ArrayTraits::Superclass) >::type Superclass
Definition: ArrayHandleImplicit.h:189
vtkm::exec::arg::load
T load(const U &u, vtkm::Id v)
Definition: FetchTagArrayDirectIn.h:36
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
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::cont::make_ArrayHandleImplicit
vtkm::cont::ArrayHandleImplicit< FunctorType > make_ArrayHandleImplicit(FunctorType functor, vtkm::Id length)
make_ArrayHandleImplicit is convenience function to generate an ArrayHandleImplicit.
Definition: ArrayHandleImplicit.h:203
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::IdComponent
vtkm::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:194
vtkm::cont::ArrayHandleImplicit::ArrayTraits
typename detail::ArrayHandleImplicitTraits< FunctorType > ArrayTraits
Definition: ArrayHandleImplicit.h:183
vtkm::cont::ArrayHandleImplicit::PortalType
typename ArrayTraits::PortalType PortalType
Definition: ArrayHandleImplicit.h:184
VTKM_STORAGE_NO_RESIZE
#define VTKM_STORAGE_NO_RESIZE
Definition: Storage.h:185
mangled_diy_namespace
Definition: Particle.h:351
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::Id
vtkm::Int64 Id
Base type to use to index arrays.
Definition: Types.h:227
vtkm::VecFlat
Treat a Vec or Vec-like object as a flat Vec.
Definition: VecFlat.h:224
vtkm::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:58
vtkm::cont::ArrayHandleImplicit::ArrayHandleImplicit
ArrayHandleImplicit(FunctorType functor, vtkm::Id length)
Definition: ArrayHandleImplicit.h:192
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:89
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:179