/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipcub/checkouts/docs-5.0.2/hipcub/include/hipcub/backend/rocprim/thread/thread_operators.hpp Source File

/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipcub/checkouts/docs-5.0.2/hipcub/include/hipcub/backend/rocprim/thread/thread_operators.hpp Source File#

hipCUB: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipcub/checkouts/docs-5.0.2/hipcub/include/hipcub/backend/rocprim/thread/thread_operators.hpp Source File
thread_operators.hpp
1 /******************************************************************************
2  * Copyright (c) 2010-2011, Duane Merrill. All rights reserved.
3  * Copyright (c) 2011-2018, NVIDIA CORPORATION. All rights reserved.
4  * Modifications Copyright (c) 2017-2020, Advanced Micro Devices, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the NVIDIA CORPORATION nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  ******************************************************************************/
29 
30 #ifndef HIBCUB_ROCPRIM_THREAD_THREAD_OPERATORS_HPP_
31 #define HIBCUB_ROCPRIM_THREAD_THREAD_OPERATORS_HPP_
32 
33 #include "../../../config.hpp"
34 
35 #include "../util_type.hpp"
36 
37 BEGIN_HIPCUB_NAMESPACE
38 
39 struct Equality
40 {
41  template<class T>
42  HIPCUB_HOST_DEVICE inline
43  constexpr bool operator()(const T& a, const T& b) const
44  {
45  return a == b;
46  }
47 };
48 
49 struct Inequality
50 {
51  template<class T>
52  HIPCUB_HOST_DEVICE inline
53  constexpr bool operator()(const T& a, const T& b) const
54  {
55  return a != b;
56  }
57 };
58 
59 template <class EqualityOp>
61 {
62  EqualityOp op;
63 
64  HIPCUB_HOST_DEVICE inline
65  InequalityWrapper(EqualityOp op) : op(op) {}
66 
67  template<class T>
68  HIPCUB_HOST_DEVICE inline
69  bool operator()(const T &a, const T &b)
70  {
71  return !op(a, b);
72  }
73 };
74 
75 struct Sum
76 {
77  template<class T>
78  HIPCUB_HOST_DEVICE inline
79  constexpr T operator()(const T &a, const T &b) const
80  {
81  return a + b;
82  }
83 };
84 
85 struct Max
86 {
87  template<class T>
88  HIPCUB_HOST_DEVICE inline
89  constexpr T operator()(const T &a, const T &b) const
90  {
91  return a < b ? b : a;
92  }
93 };
94 
95 struct Min
96 {
97  template<class T>
98  HIPCUB_HOST_DEVICE inline
99  constexpr T operator()(const T &a, const T &b) const
100  {
101  return a < b ? a : b;
102  }
103 };
104 
105 struct ArgMax
106 {
107  template<
108  class Key,
109  class Value
110  >
111  HIPCUB_HOST_DEVICE inline
112  constexpr KeyValuePair<Key, Value>
113  operator()(const KeyValuePair<Key, Value>& a,
114  const KeyValuePair<Key, Value>& b) const
115  {
116  return ((b.value > a.value) || ((a.value == b.value) && (b.key < a.key))) ? b : a;
117  }
118 };
119 
120 struct ArgMin
121 {
122  template<
123  class Key,
124  class Value
125  >
126  HIPCUB_HOST_DEVICE inline
127  constexpr KeyValuePair<Key, Value>
128  operator()(const KeyValuePair<Key, Value>& a,
129  const KeyValuePair<Key, Value>& b) const
130  {
131  return ((b.value < a.value) || ((a.value == b.value) && (b.key < a.key))) ? b : a;
132  }
133 };
134 
135 namespace detail
136 {
137 
138 // CUB uses value_type of OutputIteratorT (if not void) as a type of intermediate results in scan and reduce,
139 // for example:
140 //
141 // /// The output value type
142 // typedef typename If<(Equals<typename std::iterator_traits<OutputIteratorT>::value_type, void>::VALUE), // OutputT = (if output iterator's value type is void) ?
143 // typename std::iterator_traits<InputIteratorT>::value_type, // ... then the input iterator's value type,
144 // typename std::iterator_traits<OutputIteratorT>::value_type>::Type OutputT; // ... else the output iterator's value type
145 //
146 // rocPRIM (as well as Thrust) uses result type of BinaryFunction instead (if not void):
147 //
148 // using input_type = typename std::iterator_traits<InputIterator>::value_type;
149 // using result_type = typename ::rocprim::detail::match_result_type<
150 // input_type, BinaryFunction
151 // >::type;
152 //
153 // For short -> float using Sum()
154 // CUB: float Sum(float, float)
155 // rocPRIM: short Sum(short, short)
156 //
157 // This wrapper allows to have compatibility with CUB in hipCUB.
158 template<
159  class InputIteratorT,
160  class OutputIteratorT,
161  class BinaryFunction
162 >
163 struct convert_result_type_wrapper
164 {
165  using input_type = typename std::iterator_traits<InputIteratorT>::value_type;
166  using output_type = typename std::iterator_traits<OutputIteratorT>::value_type;
167  using result_type =
168  typename std::conditional<
169  std::is_void<output_type>::value, input_type, output_type
170  >::type;
171 
172  convert_result_type_wrapper(BinaryFunction op) : op(op) {}
173 
174  template<class T>
175  HIPCUB_HOST_DEVICE inline
176  constexpr result_type operator()(const T &a, const T &b) const
177  {
178  return static_cast<result_type>(op(a, b));
179  }
180 
181  BinaryFunction op;
182 };
183 
184 template<
185  class InputIteratorT,
186  class OutputIteratorT,
187  class BinaryFunction
188 >
189 inline
190 convert_result_type_wrapper<InputIteratorT, OutputIteratorT, BinaryFunction>
191 convert_result_type(BinaryFunction op)
192 {
193  return convert_result_type_wrapper<InputIteratorT, OutputIteratorT, BinaryFunction>(op);
194 }
195 
196 } // end detail namespace
197 
198 END_HIPCUB_NAMESPACE
199 
200 #endif // HIBCUB_ROCPRIM_THREAD_THREAD_OPERATORS_HPP_
Definition: thread_operators.hpp:106
Definition: thread_operators.hpp:121
Definition: thread_operators.hpp:40
Definition: thread_operators.hpp:61
Definition: thread_operators.hpp:50
Definition: thread_operators.hpp:86
Definition: thread_operators.hpp:96
Definition: thread_operators.hpp:76