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