CUB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups
thread_operators.cuh
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (c) 2011, Duane Merrill. All rights reserved.
3  * Copyright (c) 2011-2014, NVIDIA CORPORATION. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the NVIDIA CORPORATION nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  ******************************************************************************/
28 
34 /******************************************************************************
35  * Simple functor operators
36  ******************************************************************************/
37 
38 #pragma once
39 
40 #include "../util_macro.cuh"
41 #include "../util_type.cuh"
42 #include "../util_namespace.cuh"
43 
45 CUB_NS_PREFIX
46 
48 namespace cub {
49 
50 
59 struct Equality
60 {
62  template <typename T>
63  __host__ __device__ __forceinline__ bool operator()(const T &a, const T &b) const
64  {
65  return a == b;
66  }
67 };
68 
69 
73 struct Inequality
74 {
76  template <typename T>
77  __host__ __device__ __forceinline__ bool operator()(const T &a, const T &b) const
78  {
79  return a != b;
80  }
81 };
82 
83 
87 template <typename EqualityOp>
89 {
91  EqualityOp op;
92 
94  __host__ __device__ __forceinline__
95  InequalityWrapper(EqualityOp op) : op(op) {}
96 
98  template <typename T>
99  __host__ __device__ __forceinline__ bool operator()(const T &a, const T &b) const
100  {
101  return !op(a, b);
102  }
103 };
104 
105 
109 struct Sum
110 {
112  template <typename T>
113  __host__ __device__ __forceinline__ T operator()(const T &a, const T &b) const
114  {
115  return a + b;
116  }
117 };
118 
119 
123 struct Max
124 {
126  template <typename T>
127  __host__ __device__ __forceinline__ T operator()(const T &a, const T &b) const
128  {
129  return CUB_MAX(a, b);
130  }
131 };
132 
133 
137 struct ArgMax
138 {
140  template <typename T, typename Offset>
141  __host__ __device__ __forceinline__ ItemOffsetPair<T, Offset> operator()(
142  const ItemOffsetPair<T, Offset> &a,
143  const ItemOffsetPair<T, Offset> &b) const
144  {
145  if (a.value == b.value)
146  return (b.offset < a.offset) ? b : a;
147 
148  return (b.value > a.value) ? b : a;
149  }
150 };
151 
152 
156 struct Min
157 {
159  template <typename T>
160  __host__ __device__ __forceinline__ T operator()(const T &a, const T &b) const
161  {
162  return CUB_MIN(a, b);
163  }
164 };
165 
166 
170 struct ArgMin
171 {
173  template <typename T, typename Offset>
174  __host__ __device__ __forceinline__ ItemOffsetPair<T, Offset> operator()(
175  const ItemOffsetPair<T, Offset> &a,
176  const ItemOffsetPair<T, Offset> &b) const
177  {
178  if (a.value == b.value)
179  return (b.offset < a.offset) ? b : a;
180 
181  return (b.value < a.value) ? b : a;
182  }
183 };
184 
185 
189 template <typename B>
190 struct Cast
191 {
193  template <typename A>
194  __host__ __device__ __forceinline__ B operator()(const A &a) const
195  {
196  return (B) a;
197  }
198 };
199 
200 
201  // end group UtilModule
203 
204 
205 } // CUB namespace
206 CUB_NS_POSTFIX // Optional outer namespace(s)