VTK-m  2.0
ArrayHandleExecutionManager.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_exec_ArrayHandleExecutionManager_h
11 #define vtk_m_cont_exec_ArrayHandleExecutionManager_h
12 
14 #include <vtkm/cont/Storage.h>
15 #include <vtkm/cont/Token.h>
16 
18 
19 namespace vtkm
20 {
21 namespace cont
22 {
23 namespace internal
24 {
25 
29 template <typename T, typename Storage>
30 class ArrayHandleExecutionManagerBase
31 {
32 private:
33  using StorageType = vtkm::cont::internal::Storage<T, Storage>;
34 
35 public:
36  template <typename DeviceAdapter>
37  struct ExecutionTypes
38  {
39  private:
40  using ArrayTransferType = vtkm::cont::internal::ArrayTransfer<T, Storage, DeviceAdapter>;
41 
42  public:
43  using Portal = typename ArrayTransferType::PortalExecution;
44  using PortalConst = typename ArrayTransferType::PortalConstExecution;
45  };
46 
49  using ValueType = T;
50 
53  using PortalControl = typename StorageType::PortalType;
54  using PortalConstControl = typename StorageType::PortalConstType;
55 
56  VTKM_CONT
57  virtual ~ArrayHandleExecutionManagerBase() {}
58 
62  VTKM_CONT
63  vtkm::Id GetNumberOfValues() const { return this->GetNumberOfValuesImpl(); }
64 
71  template <typename DeviceAdapter>
72  VTKM_CONT typename ExecutionTypes<DeviceAdapter>::PortalConst
73  PrepareForInput(bool updateData, DeviceAdapter, vtkm::cont::Token& token)
74  {
75  this->VerifyDeviceAdapter(DeviceAdapter());
76 
77  typename ExecutionTypes<DeviceAdapter>::PortalConst portal;
78  this->PrepareForInputImpl(updateData, &portal, token);
79  return portal;
80  }
81 
88  template <typename DeviceAdapter>
89  VTKM_CONT typename ExecutionTypes<DeviceAdapter>::Portal
90  PrepareForInPlace(bool updateData, DeviceAdapter, vtkm::cont::Token& token)
91  {
92  this->VerifyDeviceAdapter(DeviceAdapter());
93 
94  typename ExecutionTypes<DeviceAdapter>::Portal portal;
95  this->PrepareForInPlaceImpl(updateData, &portal, token);
96  return portal;
97  }
98 
106  template <typename DeviceAdapter>
107  VTKM_CONT typename ExecutionTypes<DeviceAdapter>::Portal
108  PrepareForOutput(vtkm::Id numberOfValues, DeviceAdapter, vtkm::cont::Token& token)
109  {
110  this->VerifyDeviceAdapter(DeviceAdapter());
111 
112  typename ExecutionTypes<DeviceAdapter>::Portal portal;
113  this->PrepareForOutputImpl(numberOfValues, &portal, token);
114  return portal;
115  }
116 
124  VTKM_CONT
125  void RetrieveOutputData(StorageType* storage) const { this->RetrieveOutputDataImpl(storage); }
126 
136  VTKM_CONT
137  void Shrink(vtkm::Id numberOfValues) { this->ShrinkImpl(numberOfValues); }
138 
142  VTKM_CONT
143  void ReleaseResources() { this->ReleaseResourcesImpl(); }
144 
145  template <typename DeviceAdapter>
146  VTKM_CONT bool IsDeviceAdapter(DeviceAdapter device) const
147  {
148  return this->IsDeviceAdapterImpl(device);
149  }
150 
151  VTKM_CONT
152  DeviceAdapterId GetDeviceAdapterId() const { return this->GetDeviceAdapterIdImpl(); }
153 
154 protected:
155  virtual vtkm::Id GetNumberOfValuesImpl() const = 0;
156 
157  virtual void PrepareForInputImpl(bool updateData,
158  void* portalExecutionVoid,
159  vtkm::cont::Token& token) = 0;
160 
161  virtual void PrepareForInPlaceImpl(bool updateData,
162  void* portalExecutionVoid,
163  vtkm::cont::Token& token) = 0;
164 
165  virtual void PrepareForOutputImpl(vtkm::Id numberOfValues,
166  void* portalExecution,
167  vtkm::cont::Token& token) = 0;
168 
169  virtual void RetrieveOutputDataImpl(StorageType* storage) const = 0;
170 
171  virtual void ShrinkImpl(Id numberOfValues) = 0;
172 
173  virtual void ReleaseResourcesImpl() = 0;
174 
175  virtual bool IsDeviceAdapterImpl(const vtkm::cont::DeviceAdapterId& id) const = 0;
176 
177  virtual DeviceAdapterId GetDeviceAdapterIdImpl() const = 0;
178 
179 private:
180  template <typename DeviceAdapter>
181  VTKM_CONT void VerifyDeviceAdapter(DeviceAdapter device) const
182  {
183  if (!this->IsDeviceAdapter(device))
184  {
185  throw vtkm::cont::ErrorInternal("Device Adapter Mismatch");
186  }
187  }
188 };
189 
197 template <typename T, typename Storage, typename DeviceAdapter>
198 class ArrayHandleExecutionManager : public ArrayHandleExecutionManagerBase<T, Storage>
199 {
200  using Superclass = ArrayHandleExecutionManagerBase<T, Storage>;
201  using ArrayTransferType = vtkm::cont::internal::ArrayTransfer<T, Storage, DeviceAdapter>;
202  using StorageType = vtkm::cont::internal::Storage<T, Storage>;
203 
204 public:
205  using PortalControl = typename ArrayTransferType::PortalControl;
206  using PortalConstControl = typename ArrayTransferType::PortalConstControl;
207 
208  using PortalExecution = typename ArrayTransferType::PortalExecution;
209  using PortalConstExecution = typename ArrayTransferType::PortalConstExecution;
210 
211  VTKM_CONT
212  ArrayHandleExecutionManager(StorageType* storage)
213  : Transfer(storage)
214  {
215  }
216 
217 protected:
218  VTKM_CONT
219  vtkm::Id GetNumberOfValuesImpl() const { return this->Transfer.GetNumberOfValues(); }
220 
221  VTKM_CONT
222  void PrepareForInputImpl(bool updateData, void* portalExecutionVoid, vtkm::cont::Token& token)
223  {
224  PortalConstExecution portal = this->Transfer.PrepareForInput(updateData, token);
225  *reinterpret_cast<PortalConstExecution*>(portalExecutionVoid) = portal;
226  }
227 
228  VTKM_CONT
229  void PrepareForInPlaceImpl(bool updateData, void* portalExecutionVoid, vtkm::cont::Token& token)
230  {
231  PortalExecution portal = this->Transfer.PrepareForInPlace(updateData, token);
232  *reinterpret_cast<PortalExecution*>(portalExecutionVoid) = portal;
233  }
234 
235  VTKM_CONT
236  void PrepareForOutputImpl(vtkm::Id numberOfValues,
237  void* portalExecutionVoid,
238  vtkm::cont::Token& token)
239  {
240  PortalExecution portal = this->Transfer.PrepareForOutput(numberOfValues, token);
241  *reinterpret_cast<PortalExecution*>(portalExecutionVoid) = portal;
242  }
243 
244  VTKM_CONT
245  void RetrieveOutputDataImpl(StorageType* storage) const
246  {
247  this->Transfer.RetrieveOutputData(storage);
248  }
249 
250  VTKM_CONT
251  void ShrinkImpl(Id numberOfValues) { this->Transfer.Shrink(numberOfValues); }
252 
253  VTKM_CONT
254  void ReleaseResourcesImpl() { this->Transfer.ReleaseResources(); }
255 
256  VTKM_CONT
257  bool IsDeviceAdapterImpl(const DeviceAdapterId& id) const { return id == DeviceAdapter(); }
258 
259  VTKM_CONT
260  DeviceAdapterId GetDeviceAdapterIdImpl() const { return DeviceAdapter(); }
261 
262 private:
263  ArrayTransferType Transfer;
264 };
265 }
266 }
267 } // namespace vtkm::cont::internal
268 
269 #endif //vtk_m_cont_exec_ArrayHandleExecutionManager_h
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
Storage.h
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
vtkm::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
vtkm::cont::ErrorInternal
This class is thrown when VTKm detects an internal state that should never be reached.
Definition: ErrorInternal.h:26
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::cont::DeviceAdapterId
Definition: DeviceAdapterTag.h:52
ArrayTransfer.h
ErrorInternal.h
Token.h