rocPRIM
functional.hpp
1 // Copyright (c) 2017-2023 Advanced Micro Devices, Inc. All rights reserved.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #ifndef ROCPRIM_FUNCTIONAL_HPP_
22 #define ROCPRIM_FUNCTIONAL_HPP_
23 
24 #include <functional>
25 
26 // Meta configuration for rocPRIM
27 #include "config.hpp"
28 
29 BEGIN_ROCPRIM_NAMESPACE
30 
33 
36 #if ROCPRIM_NAVI
37 ROCPRIM_PRAGMA_MESSAGE("GPU printf warnings for invalid rocPRIM warp operations on Navi GPUs "
38  "temporarily disabled, due to performance issues with printf.")
39  #define ROCPRIM_PRINT_ERROR_ONCE(message) \
40  {}
41 #else
42  #define ROCPRIM_PRINT_ERROR_ONCE(message) \
43  { \
44  unsigned int idx = threadIdx.x + (blockIdx.x * blockDim.x); \
45  idx += threadIdx.y + (blockIdx.y * blockDim.y); \
46  idx += threadIdx.z + (blockIdx.z * blockDim.z); \
47  if(idx == 0) \
48  printf("%s\n", #message); \
49  }
50 #endif
51 
53 template<class T>
54 ROCPRIM_HOST_DEVICE inline
55 constexpr T max(const T& a, const T& b)
56 {
57  return a < b ? b : a;
58 }
59 
61 template<class T>
62 ROCPRIM_HOST_DEVICE inline
63 constexpr T min(const T& a, const T& b)
64 {
65  return a < b ? a : b;
66 }
67 
69 template<class T>
70 ROCPRIM_HOST_DEVICE inline
71 void swap(T& a, T& b)
72 {
73  T c = a;
74  a = b;
75  b = c;
76 }
77 
79 template<class T = void>
80 struct less
81 {
83  ROCPRIM_HOST_DEVICE inline
84  constexpr bool operator()(const T& a, const T& b) const
85  {
86  return a < b;
87  }
88 };
89 
92 template<>
93 struct less<void>
94 {
96  template<class T, class U>
97  ROCPRIM_HOST_DEVICE inline
98  constexpr bool operator()(const T& a, const U& b) const
99  {
100  return a < b;
101  }
102 };
103 
105 template<class T = void>
107 {
109  ROCPRIM_HOST_DEVICE inline
110  constexpr bool operator()(const T& a, const T& b) const
111  {
112  return a <= b;
113  }
114 };
115 
118 template<>
119 struct less_equal<void>
120 {
122  template <typename T>
123  ROCPRIM_HOST_DEVICE inline
124  constexpr bool operator()(const T& a, const T& b) const
125  {
126  return a <= b;
127  }
128 };
129 
131 template<class T = void>
132 struct greater
133 {
135  ROCPRIM_HOST_DEVICE inline
136  constexpr bool operator()(const T& a, const T& b) const
137  {
138  return a > b;
139  }
140 };
141 
144 template<>
145 struct greater<void>
146 {
148  template <typename T>
149  ROCPRIM_HOST_DEVICE inline
150  constexpr bool operator()(const T& a, const T& b) const
151  {
152  return a > b;
153  }
154 };
155 
157 template<class T = void>
159 {
161  ROCPRIM_HOST_DEVICE inline
162  constexpr bool operator()(const T& a, const T& b) const
163  {
164  return a >= b;
165  }
166 };
167 
170 template<>
171 struct greater_equal<void>
172 {
174  template <typename T>
175  ROCPRIM_HOST_DEVICE inline
176  constexpr bool operator()(const T& a, const T& b) const
177  {
178  return a >= b;
179  }
180 };
181 
183 template<class T = void>
184 struct equal_to
185 {
187  ROCPRIM_HOST_DEVICE inline
188  constexpr bool operator()(const T& a, const T& b) const
189  {
190  return a == b;
191  }
192 };
193 
196 template<>
197 struct equal_to<void>
198 {
200  template <typename T>
201  ROCPRIM_HOST_DEVICE inline
202  constexpr bool operator()(const T& a, const T& b) const
203  {
204  return a == b;
205  }
206 };
207 
209 template<class T = void>
211 {
213  ROCPRIM_HOST_DEVICE inline
214  constexpr bool operator()(const T& a, const T& b) const
215  {
216  return a != b;
217  }
218 };
219 
222 template<>
223 struct not_equal_to<void>
224 {
226  template <typename T>
227  ROCPRIM_HOST_DEVICE inline
228  constexpr bool operator()(const T& a, const T& b) const
229  {
230  return a != b;
231  }
232 };
233 
235 template<class T = void>
236 struct plus
237 {
239  ROCPRIM_HOST_DEVICE inline
240  constexpr T operator()(const T& a, const T& b) const
241  {
242  return a + b;
243  }
244 };
245 
248 template<>
249 struct plus<void>
250 {
252  template <typename T>
253  ROCPRIM_HOST_DEVICE inline
254  constexpr T operator()(const T& a, const T& b) const
255  {
256  return a + b;
257  }
258 };
259 
261 template<class T = void>
262 struct minus
263 {
265  ROCPRIM_HOST_DEVICE inline
266  constexpr T operator()(const T& a, const T& b) const
267  {
268  return a - b;
269  }
270 };
271 
274 template<>
275 struct minus<void>
276 {
278  template <typename T>
279  ROCPRIM_HOST_DEVICE inline
280  constexpr T operator()(const T& a, const T& b) const
281  {
282  return a - b;
283  }
284 };
285 
287 template<class T = void>
289 {
291  ROCPRIM_HOST_DEVICE inline
292  constexpr T operator()(const T& a, const T& b) const
293  {
294  return a * b;
295  }
296 };
297 
300 template<>
301 struct multiplies<void>
302 {
304  template <typename T>
305  ROCPRIM_HOST_DEVICE inline
306  constexpr T operator()(const T& a, const T& b) const
307  {
308  return a * b;
309  }
310 };
311 
313 template<class T = void>
314 struct maximum
315 {
317  ROCPRIM_HOST_DEVICE inline
318  constexpr T operator()(const T& a, const T& b) const
319  {
320  return a < b ? b : a;
321  }
322 };
323 
326 template<>
327 struct maximum<void>
328 {
330  template <typename T>
331  ROCPRIM_HOST_DEVICE inline
332  constexpr T operator()(const T& a, const T& b) const
333  {
334  return a < b ? b : a;
335  }
336 };
337 
339 template<class T = void>
340 struct minimum
341 {
343  ROCPRIM_HOST_DEVICE inline
344  constexpr T operator()(const T& a, const T& b) const
345  {
346  return a < b ? a : b;
347  }
348 };
349 
352 template<>
353 struct minimum<void>
354 {
356  template <typename T>
357  ROCPRIM_HOST_DEVICE inline
358  constexpr T operator()(const T& a, const T& b) const
359  {
360  return a < b ? a : b;
361  }
362 };
363 
365 template<class T = void>
366 struct identity
367 {
369  ROCPRIM_HOST_DEVICE inline
370  constexpr T operator()(const T& a) const
371  {
372  return a;
373  }
374 };
375 
378 template<>
379 struct identity<void>
380 {
382  template <typename T>
383  ROCPRIM_HOST_DEVICE inline
384  constexpr T operator()(const T& a) const
385  {
386  return a;
387  }
388 };
389 
397 template <int N, int CURRENT_VAL = N, int COUNT = 0>
398 struct Log2
399 {
401  enum { VALUE = Log2<N, (CURRENT_VAL >> 1), COUNT + 1>::VALUE }; // Inductive case
402 };
403 
404 #ifndef DOXYGEN_SHOULD_SKIP_THIS // Do not document
405 
406 template <int N, int COUNT>
407 struct Log2<N, 0, COUNT>
408 {
409  enum {VALUE = (1 << (COUNT - 1) < N) ? // Base case
410  COUNT :
411  COUNT - 1 };
412 };
413 
414 #endif // DOXYGEN_SHOULD_SKIP_THIS
415 
416 /******************************************************************************
417  * Conditional types
418  ******************************************************************************/
419 
423 template <typename A, typename B>
424 struct Equals
425 {
426  enum {
427  VALUE = 0,
428  NEGATE = 1
429  };
430 };
431 
432 #ifndef DOXYGEN_SHOULD_SKIP_THIS // Do not document
433 
434 template <typename A>
435 struct Equals <A, A>
436 {
437  enum {
438  VALUE = 1,
439  NEGATE = 0
440  };
441 };
442 
443 template <int A>
444 struct Int2Type
445 {
446  enum {VALUE = A};
447 };
448 
449 #endif // DOXYGEN_SHOULD_SKIP_THIS
450 
452 // end of group utilsmodule_functional
453 
454 END_ROCPRIM_NAMESPACE
455 
456 #endif // ROCPRIM_FUNCTIONAL_HPP_
Type equality test.
Definition: functional.hpp:424
ROCPRIM_HOST_DEVICE constexpr bool operator()(const T &a, const T &b) const
Invocation operator.
Definition: functional.hpp:214
ROCPRIM_HOST_DEVICE constexpr T max(const T &a, const T &b)
Returns the maximum of its arguments.
Definition: functional.hpp:55
ROCPRIM_HOST_DEVICE constexpr T operator()(const T &a, const T &b) const
Invocation operator.
Definition: functional.hpp:240
Functor that returns a - b.
Definition: functional.hpp:262
ROCPRIM_HOST_DEVICE constexpr bool operator()(const T &a, const T &b) const
Invocation operator.
Definition: functional.hpp:136
Functor that returns true if a != b. Otherwise returns false.
Definition: functional.hpp:210
ROCPRIM_HOST_DEVICE constexpr T operator()(const T &a, const T &b) const
Invocation operator.
Definition: functional.hpp:254
Functor that returns true if a >= b. Otherwise returns false.
Definition: functional.hpp:158
Functor that returns its argument.
Definition: functional.hpp:366
ROCPRIM_HOST_DEVICE constexpr T operator()(const T &a) const
Invocation operator.
Definition: functional.hpp:370
ROCPRIM_HOST_DEVICE constexpr bool operator()(const T &a, const T &b) const
Invocation operator.
Definition: functional.hpp:150
Functor that returns true if a <= b. Otherwise returns false.
Definition: functional.hpp:106
ROCPRIM_HOST_DEVICE constexpr T operator()(const T &a) const
Invocation operator.
Definition: functional.hpp:384
ROCPRIM_HOST_DEVICE constexpr T min(const T &a, const T &b)
Returns the minimum of its arguments.
Definition: functional.hpp:63
ROCPRIM_HOST_DEVICE constexpr T operator()(const T &a, const T &b) const
Invocation operator.
Definition: functional.hpp:332
ROCPRIM_HOST_DEVICE constexpr T operator()(const T &a, const T &b) const
Invocation operator.
Definition: functional.hpp:266
Definition: functional.hpp:444
ROCPRIM_HOST_DEVICE constexpr bool operator()(const T &a, const T &b) const
Invocation operator.
Definition: functional.hpp:124
ROCPRIM_HOST_DEVICE constexpr T operator()(const T &a, const T &b) const
Invocation operator.
Definition: functional.hpp:344
Returns true if a < b. Otherwise returns false.
Definition: functional.hpp:80
ROCPRIM_HOST_DEVICE constexpr bool operator()(const T &a, const T &b) const
Invocation operator.
Definition: functional.hpp:176
Functor that returns true if a > b. Otherwise returns false.
Definition: functional.hpp:132
Functor that returns a + b.
Definition: functional.hpp:236
ROCPRIM_HOST_DEVICE void swap(T &a, T &b)
Swaps two values.
Definition: functional.hpp:71
Functor that returns true if a == b. Otherwise returns false.
Definition: functional.hpp:184
ROCPRIM_HOST_DEVICE constexpr bool operator()(const T &a, const T &b) const
Invocation operator.
Definition: functional.hpp:228
ROCPRIM_HOST_DEVICE constexpr T operator()(const T &a, const T &b) const
Invocation operator.
Definition: functional.hpp:306
ROCPRIM_HOST_DEVICE constexpr bool operator()(const T &a, const U &b) const
Invocation operator.
Definition: functional.hpp:98
Functor that returns the maximum of its arguments.
Definition: functional.hpp:314
ROCPRIM_HOST_DEVICE constexpr bool operator()(const T &a, const T &b) const
Invocation operator.
Definition: functional.hpp:110
ROCPRIM_HOST_DEVICE constexpr bool operator()(const T &a, const T &b) const
Invocation operator.
Definition: functional.hpp:202
ROCPRIM_HOST_DEVICE constexpr T operator()(const T &a, const T &b) const
Invocation operator.
Definition: functional.hpp:292
Statically determine log2(N), rounded up.
Definition: functional.hpp:398
Functor that returns a * b.
Definition: functional.hpp:288
ROCPRIM_HOST_DEVICE constexpr bool operator()(const T &a, const T &b) const
Invocation operator.
Definition: functional.hpp:162
ROCPRIM_HOST_DEVICE constexpr T operator()(const T &a, const T &b) const
Invocation operator.
Definition: functional.hpp:280
ROCPRIM_HOST_DEVICE constexpr T operator()(const T &a, const T &b) const
Invocation operator.
Definition: functional.hpp:318
ROCPRIM_HOST_DEVICE constexpr bool operator()(const T &a, const T &b) const
Invocation operator.
Definition: functional.hpp:188
Functor that returns the minimum of its arguments.
Definition: functional.hpp:340
ROCPRIM_HOST_DEVICE constexpr bool operator()(const T &a, const T &b) const
Invocation operator.
Definition: functional.hpp:84
ROCPRIM_HOST_DEVICE constexpr T operator()(const T &a, const T &b) const
Invocation operator.
Definition: functional.hpp:358