Viskores  1.0
ArrayHandleExecutionManager.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_exec_ArrayHandleExecutionManager_h
19 #define viskores_cont_exec_ArrayHandleExecutionManager_h
20 
22 #include <viskores/cont/Storage.h>
23 #include <viskores/cont/Token.h>
24 
26 
27 namespace viskores
28 {
29 namespace cont
30 {
31 namespace internal
32 {
33 
37 template <typename T, typename Storage>
38 class ArrayHandleExecutionManagerBase
39 {
40 private:
41  using StorageType = viskores::cont::internal::Storage<T, Storage>;
42 
43 public:
44  template <typename DeviceAdapter>
45  struct ExecutionTypes
46  {
47  private:
48  using ArrayTransferType = viskores::cont::internal::ArrayTransfer<T, Storage, DeviceAdapter>;
49 
50  public:
51  using Portal = typename ArrayTransferType::PortalExecution;
52  using PortalConst = typename ArrayTransferType::PortalConstExecution;
53  };
54 
57  using ValueType = T;
58 
61  using PortalControl = typename StorageType::PortalType;
62  using PortalConstControl = typename StorageType::PortalConstType;
63 
65  virtual ~ArrayHandleExecutionManagerBase() {}
66 
71  viskores::Id GetNumberOfValues() const { return this->GetNumberOfValuesImpl(); }
72 
79  template <typename DeviceAdapter>
80  VISKORES_CONT typename ExecutionTypes<DeviceAdapter>::PortalConst
81  PrepareForInput(bool updateData, DeviceAdapter, viskores::cont::Token& token)
82  {
83  this->VerifyDeviceAdapter(DeviceAdapter());
84 
85  typename ExecutionTypes<DeviceAdapter>::PortalConst portal;
86  this->PrepareForInputImpl(updateData, &portal, token);
87  return portal;
88  }
89 
96  template <typename DeviceAdapter>
97  VISKORES_CONT typename ExecutionTypes<DeviceAdapter>::Portal
98  PrepareForInPlace(bool updateData, DeviceAdapter, viskores::cont::Token& token)
99  {
100  this->VerifyDeviceAdapter(DeviceAdapter());
101 
102  typename ExecutionTypes<DeviceAdapter>::Portal portal;
103  this->PrepareForInPlaceImpl(updateData, &portal, token);
104  return portal;
105  }
106 
114  template <typename DeviceAdapter>
115  VISKORES_CONT typename ExecutionTypes<DeviceAdapter>::Portal
116  PrepareForOutput(viskores::Id numberOfValues, DeviceAdapter, viskores::cont::Token& token)
117  {
118  this->VerifyDeviceAdapter(DeviceAdapter());
119 
120  typename ExecutionTypes<DeviceAdapter>::Portal portal;
121  this->PrepareForOutputImpl(numberOfValues, &portal, token);
122  return portal;
123  }
124 
133  void RetrieveOutputData(StorageType* storage) const { this->RetrieveOutputDataImpl(storage); }
134 
145  void Shrink(viskores::Id numberOfValues) { this->ShrinkImpl(numberOfValues); }
146 
151  void ReleaseResources() { this->ReleaseResourcesImpl(); }
152 
153  template <typename DeviceAdapter>
154  VISKORES_CONT bool IsDeviceAdapter(DeviceAdapter device) const
155  {
156  return this->IsDeviceAdapterImpl(device);
157  }
158 
160  DeviceAdapterId GetDeviceAdapterId() const { return this->GetDeviceAdapterIdImpl(); }
161 
162 protected:
163  virtual viskores::Id GetNumberOfValuesImpl() const = 0;
164 
165  virtual void PrepareForInputImpl(bool updateData,
166  void* portalExecutionVoid,
167  viskores::cont::Token& token) = 0;
168 
169  virtual void PrepareForInPlaceImpl(bool updateData,
170  void* portalExecutionVoid,
171  viskores::cont::Token& token) = 0;
172 
173  virtual void PrepareForOutputImpl(viskores::Id numberOfValues,
174  void* portalExecution,
175  viskores::cont::Token& token) = 0;
176 
177  virtual void RetrieveOutputDataImpl(StorageType* storage) const = 0;
178 
179  virtual void ShrinkImpl(Id numberOfValues) = 0;
180 
181  virtual void ReleaseResourcesImpl() = 0;
182 
183  virtual bool IsDeviceAdapterImpl(const viskores::cont::DeviceAdapterId& id) const = 0;
184 
185  virtual DeviceAdapterId GetDeviceAdapterIdImpl() const = 0;
186 
187 private:
188  template <typename DeviceAdapter>
189  VISKORES_CONT void VerifyDeviceAdapter(DeviceAdapter device) const
190  {
191  if (!this->IsDeviceAdapter(device))
192  {
193  throw viskores::cont::ErrorInternal("Device Adapter Mismatch");
194  }
195  }
196 };
197 
205 template <typename T, typename Storage, typename DeviceAdapter>
206 class ArrayHandleExecutionManager : public ArrayHandleExecutionManagerBase<T, Storage>
207 {
208  using Superclass = ArrayHandleExecutionManagerBase<T, Storage>;
209  using ArrayTransferType = viskores::cont::internal::ArrayTransfer<T, Storage, DeviceAdapter>;
210  using StorageType = viskores::cont::internal::Storage<T, Storage>;
211 
212 public:
213  using PortalControl = typename ArrayTransferType::PortalControl;
214  using PortalConstControl = typename ArrayTransferType::PortalConstControl;
215 
216  using PortalExecution = typename ArrayTransferType::PortalExecution;
217  using PortalConstExecution = typename ArrayTransferType::PortalConstExecution;
218 
220  ArrayHandleExecutionManager(StorageType* storage)
221  : Transfer(storage)
222  {
223  }
224 
225 protected:
227  viskores::Id GetNumberOfValuesImpl() const { return this->Transfer.GetNumberOfValues(); }
228 
230  void PrepareForInputImpl(bool updateData, void* portalExecutionVoid, viskores::cont::Token& token)
231  {
232  PortalConstExecution portal = this->Transfer.PrepareForInput(updateData, token);
233  *reinterpret_cast<PortalConstExecution*>(portalExecutionVoid) = portal;
234  }
235 
237  void PrepareForInPlaceImpl(bool updateData,
238  void* portalExecutionVoid,
239  viskores::cont::Token& token)
240  {
241  PortalExecution portal = this->Transfer.PrepareForInPlace(updateData, token);
242  *reinterpret_cast<PortalExecution*>(portalExecutionVoid) = portal;
243  }
244 
246  void PrepareForOutputImpl(viskores::Id numberOfValues,
247  void* portalExecutionVoid,
248  viskores::cont::Token& token)
249  {
250  PortalExecution portal = this->Transfer.PrepareForOutput(numberOfValues, token);
251  *reinterpret_cast<PortalExecution*>(portalExecutionVoid) = portal;
252  }
253 
255  void RetrieveOutputDataImpl(StorageType* storage) const
256  {
257  this->Transfer.RetrieveOutputData(storage);
258  }
259 
261  void ShrinkImpl(Id numberOfValues) { this->Transfer.Shrink(numberOfValues); }
262 
264  void ReleaseResourcesImpl() { this->Transfer.ReleaseResources(); }
265 
267  bool IsDeviceAdapterImpl(const DeviceAdapterId& id) const { return id == DeviceAdapter(); }
268 
270  DeviceAdapterId GetDeviceAdapterIdImpl() const { return DeviceAdapter(); }
271 
272 private:
273  ArrayTransferType Transfer;
274 };
275 }
276 }
277 } // namespace viskores::cont::internal
278 
279 #endif //viskores_cont_exec_ArrayHandleExecutionManager_h
Storage.h
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::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:66
ArrayTransfer.h
viskores::cont::ErrorInternal
This class is thrown when Viskores detects an internal state that should never be reached.
Definition: ErrorInternal.h:34
ErrorInternal.h
Token.h
viskores::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:43