VTK-m  2.0
WrappedOperators.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_WrappedOperators_h
11 #define vtk_m_exec_cuda_internal_WrappedOperators_h
12 
13 #include <vtkm/BinaryPredicates.h>
14 #include <vtkm/Pair.h>
15 #include <vtkm/Types.h>
18 
19 // Disable warnings we check vtkm for but Thrust does not.
21 VTKM_THIRDPARTY_PRE_INCLUDE
22 #include <thrust/system/cuda/memory.h>
23 VTKM_THIRDPARTY_POST_INCLUDE
24 
25 namespace vtkm
26 {
27 namespace exec
28 {
29 namespace cuda
30 {
31 namespace internal
32 {
33 
34 // Unary function object wrapper which can detect and handle calling the
35 // wrapped operator with complex value types such as
36 // ArrayPortalValueReference which happen when passed an input array that
37 // is implicit.
38 template <typename T_, typename Function>
39 struct WrappedUnaryPredicate
40 {
41  using T = typename std::remove_const<T_>::type;
42 
43  //make typedefs that thust expects unary operators to have
44  using first_argument_type = T;
45  using result_type = bool;
46 
47  Function m_f;
48 
49  VTKM_EXEC
50  WrappedUnaryPredicate()
51  : m_f()
52  {
53  }
54 
55  VTKM_CONT
56  WrappedUnaryPredicate(const Function& f)
57  : m_f(f)
58  {
59  }
60 
61  VTKM_EXEC bool operator()(const T& x) const { return m_f(x); }
62 
63  template <typename U>
64  VTKM_EXEC bool operator()(const vtkm::internal::ArrayPortalValueReference<U>& x) const
65  {
66  return m_f(x.Get());
67  }
68 
69  VTKM_EXEC bool operator()(const T* x) const { return m_f(*x); }
70 };
71 
72 // Binary function object wrapper which can detect and handle calling the
73 // wrapped operator with complex value types such as
74 // ArrayPortalValueReference which happen when passed an input array that
75 // is implicit.
76 template <typename T_, typename Function>
77 struct WrappedBinaryOperator
78 {
79  using T = typename std::remove_const<T_>::type;
80 
81  //make typedefs that thust expects binary operators to have
82  using first_argument_type = T;
83  using second_argument_type = T;
84  using result_type = T;
85 
86  Function m_f;
87 
88  VTKM_EXEC
89  WrappedBinaryOperator()
90  : m_f()
91  {
92  }
93 
94  VTKM_CONT
95  WrappedBinaryOperator(const Function& f)
96  : m_f(f)
97  {
98  }
99 
100  VTKM_EXEC T operator()(const T& x, const T& y) const { return m_f(x, y); }
101 
102  template <typename U>
103  VTKM_EXEC T operator()(const T& x, const vtkm::internal::ArrayPortalValueReference<U>& y) const
104  {
105  // to support proper implicit conversion, and avoid overload
106  // ambiguities.
107  return m_f(x, y.Get());
108  }
109 
110  template <typename U>
111  VTKM_EXEC T operator()(const vtkm::internal::ArrayPortalValueReference<U>& x, const T& y) const
112  {
113  return m_f(x.Get(), y);
114  }
115 
116  template <typename U, typename V>
117  VTKM_EXEC T operator()(const vtkm::internal::ArrayPortalValueReference<U>& x,
118  const vtkm::internal::ArrayPortalValueReference<V>& y) const
119  {
120  return m_f(x.Get(), y.Get());
121  }
122 
123  VTKM_EXEC T operator()(const T* const x, const T& y) const { return m_f(*x, y); }
124 
125  VTKM_EXEC T operator()(const T& x, const T* const y) const { return m_f(x, *y); }
126 
127  VTKM_EXEC T operator()(const T* const x, const T* const y) const { return m_f(*x, *y); }
128 };
129 
130 template <typename T_, typename Function>
131 struct WrappedBinaryPredicate
132 {
133  using T = typename std::remove_const<T_>::type;
134 
135  //make typedefs that thust expects binary operators to have
136  using first_argument_type = T;
137  using second_argument_type = T;
138  using result_type = bool;
139 
140  Function m_f;
141 
142  VTKM_EXEC
143  WrappedBinaryPredicate()
144  : m_f()
145  {
146  }
147 
148  VTKM_CONT
149  WrappedBinaryPredicate(const Function& f)
150  : m_f(f)
151  {
152  }
153 
154  VTKM_EXEC bool operator()(const T& x, const T& y) const { return m_f(x, y); }
155 
156  template <typename U>
157  VTKM_EXEC bool operator()(const T& x, const vtkm::internal::ArrayPortalValueReference<U>& y) const
158  {
159  return m_f(x, y.Get());
160  }
161 
162  template <typename U>
163  VTKM_EXEC bool operator()(const vtkm::internal::ArrayPortalValueReference<U>& x, const T& y) const
164  {
165  return m_f(x.Get(), y);
166  }
167 
168  template <typename U, typename V>
169  VTKM_EXEC bool operator()(const vtkm::internal::ArrayPortalValueReference<U>& x,
170  const vtkm::internal::ArrayPortalValueReference<V>& y) const
171  {
172  return m_f(x.Get(), y.Get());
173  }
174 
175  VTKM_EXEC bool operator()(const T* const x, const T& y) const { return m_f(*x, y); }
176 
177  VTKM_EXEC bool operator()(const T& x, const T* const y) const { return m_f(x, *y); }
178 
179  VTKM_EXEC bool operator()(const T* const x, const T* const y) const { return m_f(*x, *y); }
180 };
181 }
182 }
183 }
184 } //namespace vtkm::exec::cuda::internal
185 
186 namespace thrust
187 {
188 namespace detail
189 {
190 //
191 // We tell Thrust that our WrappedBinaryOperator is commutative so that we
192 // activate numerous fast paths inside thrust which are only available when
193 // the binary functor is commutative and the T type is is_arithmetic
194 //
195 //
196 template <typename T, typename F>
197 struct is_commutative<vtkm::exec::cuda::internal::WrappedBinaryOperator<T, F>>
198  : public thrust::detail::is_arithmetic<T>
199 {
200 };
201 }
202 } //namespace thrust::detail
203 
204 #endif //vtk_m_exec_cuda_internal_WrappedOperators_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
Pair.h
ThrustPatches.h
ExportMacros.h
IteratorFromArrayPortal.h
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
BinaryPredicates.h