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