VTK-m  2.0
exec/cuda/internal/IteratorFromArrayPortal.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_exec_cuda_internal_IteratorFromArrayPortal_h
11 #define vtk_m_exec_cuda_internal_IteratorFromArrayPortal_h
12 
13 #include <vtkm/Pair.h>
14 #include <vtkm/Types.h>
17 
18 // Disable warnings we check vtkm for but Thrust does not.
20 VTKM_THIRDPARTY_PRE_INCLUDE
21 #include <thrust/functional.h>
22 #include <thrust/iterator/iterator_facade.h>
23 #include <thrust/system/cuda/execution_policy.h>
24 VTKM_THIRDPARTY_POST_INCLUDE
25 
26 namespace vtkm
27 {
28 namespace exec
29 {
30 namespace cuda
31 {
32 namespace internal
33 {
34 
35 template <class ArrayPortalType>
36 class IteratorFromArrayPortal
37  : public ::thrust::iterator_facade<IteratorFromArrayPortal<ArrayPortalType>,
38  typename ArrayPortalType::ValueType,
39  ::thrust::system::cuda::tag,
40  ::thrust::random_access_traversal_tag,
41  vtkm::internal::ArrayPortalValueReference<ArrayPortalType>,
42  std::ptrdiff_t>
43 {
44 public:
46  IteratorFromArrayPortal()
47  : Portal()
48  , Index(0)
49  {
50  }
51 
52  VTKM_CONT
53  explicit IteratorFromArrayPortal(const ArrayPortalType& portal, vtkm::Id index = 0)
54  : Portal(portal)
55  , Index(index)
56  {
57  }
58 
59  VTKM_EXEC
60  vtkm::internal::ArrayPortalValueReference<ArrayPortalType> operator[](
61  std::ptrdiff_t idx) const //NEEDS to be signed
62  {
63  return vtkm::internal::ArrayPortalValueReference<ArrayPortalType>(
64  this->Portal, this->Index + static_cast<vtkm::Id>(idx));
65  }
66 
67 private:
68  ArrayPortalType Portal;
70 
71  // Implementation for ::thrust iterator_facade
72  friend class ::thrust::iterator_core_access;
73 
74  VTKM_EXEC
75  vtkm::internal::ArrayPortalValueReference<ArrayPortalType> dereference() const
76  {
77  return vtkm::internal::ArrayPortalValueReference<ArrayPortalType>(this->Portal, this->Index);
78  }
79 
80  VTKM_EXEC
81  bool equal(const IteratorFromArrayPortal<ArrayPortalType>& other) const
82  {
83  // Technically, we should probably check that the portals are the same,
84  // but the portal interface does not specify an equal operator. It is
85  // by its nature undefined what happens when comparing iterators from
86  // different portals anyway.
87  return (this->Index == other.Index);
88  }
89 
91  void increment() { this->Index++; }
92 
94  void decrement() { this->Index--; }
95 
97  void advance(std::ptrdiff_t delta) { this->Index += static_cast<vtkm::Id>(delta); }
98 
100  std::ptrdiff_t distance_to(const IteratorFromArrayPortal<ArrayPortalType>& other) const
101  {
102  // Technically, we should probably check that the portals are the same,
103  // but the portal interface does not specify an equal operator. It is
104  // by its nature undefined what happens when comparing iterators from
105  // different portals anyway.
106  return static_cast<std::ptrdiff_t>(other.Index - this->Index);
107  }
108 };
109 }
110 }
111 }
112 } //namespace vtkm::exec::cuda::internal
113 
114 //So for the unary_transform_functor and binary_transform_functor inside
115 //of thrust, they verify that the index they are storing into is a reference
116 //instead of a value, so that the contents actually are written to global memory.
117 //
118 //But for vtk-m we pass in facade objects, which are passed by value, but
119 //must be treated as references. So do to do that properly we need to specialize
120 //is_non_const_reference to state an ArrayPortalValueReference by value is valid
121 //for writing
122 namespace thrust
123 {
124 namespace detail
125 {
126 
127 template <typename T>
128 struct is_non_const_reference;
129 
130 template <typename T>
131 struct is_non_const_reference<vtkm::internal::ArrayPortalValueReference<T>>
132  : thrust::detail::true_type
133 {
134 };
135 }
136 }
137 
138 #endif //vtk_m_exec_cuda_internal_IteratorFromArrayPortal_h
VTKM_EXEC
#define VTKM_EXEC
Definition: ExportMacros.h:51
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
Types.h
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
Pair.h
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
ThrustPatches.h
ExportMacros.h
ArrayPortalValueReference.h
Index
int Index
Definition: ChooseCudaDevice.h:87
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57