Viskores  1.0
ArrayHandleImplicit.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_ArrayHandleImplicit_h
19 #define viskores_cont_ArrayHandleImplicit_h
20 
22 
23 #include <viskoresstd/is_trivial.h>
24 
25 namespace viskores
26 {
27 
28 namespace internal
29 {
30 
40 template <class FunctorType_>
41 class VISKORES_ALWAYS_EXPORT ArrayPortalImplicit
42 {
43 public:
44  using FunctorType = FunctorType_;
45  using ValueType = decltype(FunctorType{}(viskores::Id{}));
46 
49  ArrayPortalImplicit()
50  : Functor()
51  , NumberOfValues(0)
52  {
53  }
54 
57  ArrayPortalImplicit(FunctorType f, viskores::Id numValues)
58  : Functor(f)
59  , NumberOfValues(numValues)
60  {
61  }
62 
64  const FunctorType& GetFunctor() const { return this->Functor; }
65 
67  viskores::Id GetNumberOfValues() const { return this->NumberOfValues; }
68 
71  ValueType Get(viskores::Id index) const { return this->Functor(index); }
72 
73 private:
74  FunctorType Functor;
75  viskores::Id NumberOfValues;
76 };
77 
78 } // namespace internal
79 
80 namespace cont
81 {
82 
93 template <class ArrayPortalType>
94 struct VISKORES_ALWAYS_EXPORT StorageTagImplicit
95 {
96  using PortalType = ArrayPortalType;
97 };
98 
99 namespace internal
100 {
101 
104 template <typename PortalType>
105 VISKORES_CONT inline std::vector<viskores::cont::internal::Buffer>
106 PortalToArrayHandleImplicitBuffers(const PortalType& portal)
107 {
108  std::vector<viskores::cont::internal::Buffer> buffers(1);
109  buffers[0].SetMetaData(portal);
110  return buffers;
111 }
112 
115 template <typename FunctorType>
116 VISKORES_CONT inline std::vector<viskores::cont::internal::Buffer>
117 FunctorToArrayHandleImplicitBuffers(const FunctorType& functor, viskores::Id numValues)
118 {
119  return PortalToArrayHandleImplicitBuffers(
120  viskores::internal::ArrayPortalImplicit<FunctorType>(functor, numValues));
121 }
122 
123 template <class ArrayPortalType>
124 struct VISKORES_ALWAYS_EXPORT
125  Storage<typename ArrayPortalType::ValueType, StorageTagImplicit<ArrayPortalType>>
126 {
127  VISKORES_IS_TRIVIALLY_COPYABLE(ArrayPortalType);
128 
131 
132  using ReadPortalType = ArrayPortalType;
133 
134  VISKORES_CONT static std::vector<viskores::cont::internal::Buffer> CreateBuffers()
135  {
136  return viskores::cont::internal::PortalToArrayHandleImplicitBuffers(ArrayPortalType{});
137  }
138 
139  VISKORES_CONT static viskores::IdComponent GetNumberOfComponentsFlat(
140  const std::vector<viskores::cont::internal::Buffer>&)
141  {
143  }
144 
145  VISKORES_CONT static viskores::Id GetNumberOfValues(
146  const std::vector<viskores::cont::internal::Buffer>& buffers)
147  {
148  return buffers[0].GetMetaData<ArrayPortalType>().GetNumberOfValues();
149  }
150 
151  VISKORES_CONT static ReadPortalType CreateReadPortal(
152  const std::vector<viskores::cont::internal::Buffer>& buffers,
155  {
156  return buffers[0].GetMetaData<ArrayPortalType>();
157  }
158 };
159 
160 } // namespace internal
161 
162 namespace detail
163 {
164 
167 template <typename FunctorType>
168 struct ArrayHandleImplicitTraits
169 {
170  using ValueType = decltype(FunctorType{}(viskores::Id{}));
171  using PortalType = viskores::internal::ArrayPortalImplicit<FunctorType>;
174 };
175 
176 } // namespace detail
177 
185 template <class FunctorType>
186 class VISKORES_ALWAYS_EXPORT ArrayHandleImplicit
187  : public detail::ArrayHandleImplicitTraits<FunctorType>::Superclass
188 {
189 private:
190  using ArrayTraits = typename detail::ArrayHandleImplicitTraits<FunctorType>;
191  using PortalType = typename ArrayTraits::PortalType;
192 
193 public:
196  (typename ArrayTraits::Superclass));
197 
199  ArrayHandleImplicit(FunctorType functor, viskores::Id length)
200  : Superclass(internal::PortalToArrayHandleImplicitBuffers(PortalType(functor, length)))
201  {
202  }
203 };
204 
208 
209 template <typename FunctorType>
211  FunctorType functor,
212  viskores::Id length)
213 {
214  return ArrayHandleImplicit<FunctorType>(functor, length);
215 }
216 }
217 } // namespace viskores::cont
218 
219 //=============================================================================
220 // Specializations of serialization related classes
222 namespace viskores
223 {
224 namespace cont
225 {
226 
227 template <typename Functor>
228 struct SerializableTypeString<viskores::cont::ArrayHandleImplicit<Functor>>
229 {
230  static VISKORES_CONT const std::string& Get()
231  {
232  static std::string name = "AH_Implicit<" + SerializableTypeString<Functor>::Get() + ">";
233  return name;
234  }
235 };
236 
237 template <typename Functor>
238 struct SerializableTypeString<viskores::cont::ArrayHandle<
239  typename viskores::cont::detail::ArrayHandleImplicitTraits<Functor>::ValueType,
240  viskores::cont::StorageTagImplicit<viskores::internal::ArrayPortalImplicit<Functor>>>>
241  : SerializableTypeString<viskores::cont::ArrayHandleImplicit<Functor>>
242 {
243 };
244 }
245 } // viskores::cont
246 
247 namespace mangled_diy_namespace
248 {
249 
250 template <typename Functor>
251 struct Serialization<viskores::cont::ArrayHandleImplicit<Functor>>
252 {
253 private:
256 
257 public:
258  static VISKORES_CONT void save(BinaryBuffer& bb, const BaseType& obj)
259  {
260  viskoresdiy::save(bb, obj.GetNumberOfValues());
261  viskoresdiy::save(bb, obj.ReadPortal().GetFunctor());
262  }
263 
264  static VISKORES_CONT void load(BinaryBuffer& bb, BaseType& obj)
265  {
266  viskores::Id count = 0;
267  viskoresdiy::load(bb, count);
268 
269  Functor functor;
270  viskoresdiy::load(bb, functor);
271 
272  obj = viskores::cont::make_ArrayHandleImplicit(functor, count);
273  }
274 };
275 
276 template <typename Functor>
277 struct Serialization<viskores::cont::ArrayHandle<
278  typename viskores::cont::detail::ArrayHandleImplicitTraits<Functor>::ValueType,
279  viskores::cont::StorageTagImplicit<viskores::internal::ArrayPortalImplicit<Functor>>>>
280  : Serialization<viskores::cont::ArrayHandleImplicit<Functor>>
281 {
282 };
283 
284 } // diy
286 
287 #endif //viskores_cont_ArrayHandleImplicit_h
viskores::exec::arg::load
T load(const U &u, viskores::Id v)
Definition: FetchTagArrayDirectIn.h:44
viskores::VecFlat
Treat a Vec or Vec-like object as a flat Vec.
Definition: VecFlat.h:240
ArrayHandle.h
viskores::cont::ArrayHandleImplicit::ArrayTraits
typename detail::ArrayHandleImplicitTraits< FunctorType > ArrayTraits
Definition: ArrayHandleImplicit.h:190
VISKORES_SUPPRESS_EXEC_WARNINGS
#define VISKORES_SUPPRESS_EXEC_WARNINGS
Definition: ExportMacros.h:61
viskores::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:313
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
mangled_diy_namespace
Definition: Particle.h:373
viskores::cont::ArrayHandleImplicit::ArrayHandleImplicit
ArrayHandleImplicit(FunctorType functor, viskores::Id length)
Definition: ArrayHandleImplicit.h:199
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
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_STORAGE_NO_WRITE_PORTAL
#define VISKORES_STORAGE_NO_WRITE_PORTAL
Definition: Storage.h:209
viskores::cont::ArrayHandleImplicit::Superclass
typename viskores::cont::detail::GetTypeInParentheses< void(typename ArrayTraits::Superclass) >::type Superclass
Definition: ArrayHandleImplicit.h:196
viskores::cont::ArrayHandleImplicit::PortalType
typename ArrayTraits::PortalType PortalType
Definition: ArrayHandleImplicit.h:191
viskores::cont::StorageTagImplicit
An implementation for read-only implicit arrays.
Definition: ArrayHandleImplicit.h:94
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::cont::ArrayHandleImplicit
An ArrayHandle that computes values on the fly.
Definition: ArrayHandleImplicit.h:186
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::cont::make_ArrayHandleImplicit
viskores::cont::ArrayHandleImplicit< FunctorType > make_ArrayHandleImplicit(FunctorType functor, viskores::Id length)
make_ArrayHandleImplicit is convenience function to generate an ArrayHandleImplicit.
Definition: ArrayHandleImplicit.h:210
viskores::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:43
viskores::cont::StorageTagImplicit::PortalType
ArrayPortalType PortalType
Definition: ArrayHandleImplicit.h:96