1
/******************************************************************************
2
* Copyright (c) 2011, Duane Merrill. All rights reserved.
3
* Copyright (c) 2011-2013, NVIDIA CORPORATION. All rights reserved.
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.
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.
27
******************************************************************************/
31
* Thread utilities for sequential prefix scan over statically-sized array types
36
#include "../thread/thread_operators.cuh"
37
#include "../util_namespace.cuh"
39
/// Optional outer namespace(s)
46
* \addtogroup ThreadModule
51
* \name Sequential prefix scan over statically-sized array types
56
* \brief Perform a sequential exclusive prefix scan over \p LENGTH elements of the \p input array, seeded with the specified \p prefix. The aggregate is returned.
58
* \tparam LENGTH Length of \p input and \p output arrays
59
* \tparam T <b>[inferred]</b> The data type to be scanned.
60
* \tparam ScanOp <b>[inferred]</b> Binary scan operator type having member <tt>T operator()(const T &a, const T &b)</tt>
66
__device__ __forceinline__ T ThreadScanExclusive(
67
T *input, ///< [in] Input array
68
T *output, ///< [out] Output array (may be aliased to \p input)
69
ScanOp scan_op, ///< [in] Binary scan operator
70
T prefix, ///< [in] Prefix to seed scan with
71
bool apply_prefix = true) ///< [in] Whether or not the calling thread should apply its prefix. If not, the first output element is undefined. (Handy for preventing thread-0 from applying a prefix.)
73
T inclusive = input[0];
76
inclusive = scan_op(prefix, inclusive);
79
T exclusive = inclusive;
82
for (int i = 1; i < LENGTH; ++i)
84
inclusive = scan_op(exclusive, input[i]);
85
output[i] = exclusive;
86
exclusive = inclusive;
94
* \brief Perform a sequential exclusive prefix scan over the statically-sized \p input array, seeded with the specified \p prefix. The aggregate is returned.
96
* \tparam LENGTH <b>[inferred]</b> Length of \p input and \p output arrays
97
* \tparam T <b>[inferred]</b> The data type to be scanned.
98
* \tparam ScanOp <b>[inferred]</b> Binary scan operator type having member <tt>T operator()(const T &a, const T &b)</tt>
104
__device__ __forceinline__ T ThreadScanExclusive(
105
T (&input)[LENGTH], ///< [in] Input array
106
T (&output)[LENGTH], ///< [out] Output array (may be aliased to \p input)
107
ScanOp scan_op, ///< [in] Binary scan operator
108
T prefix, ///< [in] Prefix to seed scan with
109
bool apply_prefix = true) ///< [in] Whether or not the calling thread should apply its prefix. (Handy for preventing thread-0 from applying a prefix.)
111
return ThreadScanExclusive<LENGTH>((T*) input, (T*) output, scan_op, prefix);
116
* \brief Perform a sequential inclusive prefix scan over \p LENGTH elements of the \p input array. The aggregate is returned.
118
* \tparam LENGTH Length of \p input and \p output arrays
119
* \tparam T <b>[inferred]</b> The data type to be scanned.
120
* \tparam ScanOp <b>[inferred]</b> Binary scan operator type having member <tt>T operator()(const T &a, const T &b)</tt>
126
__device__ __forceinline__ T ThreadScanInclusive(
127
T *input, ///< [in] Input array
128
T *output, ///< [out] Output array (may be aliased to \p input)
129
ScanOp scan_op) ///< [in] Binary scan operator
131
T inclusive = input[0];
132
output[0] = inclusive;
136
for (int i = 0; i < LENGTH; ++i)
138
inclusive = scan_op(inclusive, input[i]);
139
output[i] = inclusive;
147
* \brief Perform a sequential inclusive prefix scan over the statically-sized \p input array. The aggregate is returned.
149
* \tparam LENGTH <b>[inferred]</b> Length of \p input and \p output arrays
150
* \tparam T <b>[inferred]</b> The data type to be scanned.
151
* \tparam ScanOp <b>[inferred]</b> Binary scan operator type having member <tt>T operator()(const T &a, const T &b)</tt>
157
__device__ __forceinline__ T ThreadScanInclusive(
158
T (&input)[LENGTH], ///< [in] Input array
159
T (&output)[LENGTH], ///< [out] Output array (may be aliased to \p input)
160
ScanOp scan_op) ///< [in] Binary scan operator
162
return ThreadScanInclusive<LENGTH>((T*) input, (T*) output, scan_op);
167
* \brief Perform a sequential inclusive prefix scan over \p LENGTH elements of the \p input array, seeded with the specified \p prefix. The aggregate is returned.
169
* \tparam LENGTH Length of \p input and \p output arrays
170
* \tparam T <b>[inferred]</b> The data type to be scanned.
171
* \tparam ScanOp <b>[inferred]</b> Binary scan operator type having member <tt>T operator()(const T &a, const T &b)</tt>
177
__device__ __forceinline__ T ThreadScanInclusive(
178
T *input, ///< [in] Input array
179
T *output, ///< [out] Output array (may be aliased to \p input)
180
ScanOp scan_op, ///< [in] Binary scan operator
181
T prefix, ///< [in] Prefix to seed scan with
182
bool apply_prefix = true) ///< [in] Whether or not the calling thread should apply its prefix. (Handy for preventing thread-0 from applying a prefix.)
184
T inclusive = input[0];
187
inclusive = scan_op(prefix, inclusive);
189
output[0] = inclusive;
193
for (int i = 1; i < LENGTH; ++i)
195
inclusive = scan_op(inclusive, input[i]);
196
output[i] = inclusive;
204
* \brief Perform a sequential inclusive prefix scan over the statically-sized \p input array, seeded with the specified \p prefix. The aggregate is returned.
206
* \tparam LENGTH <b>[inferred]</b> Length of \p input and \p output arrays
207
* \tparam T <b>[inferred]</b> The data type to be scanned.
208
* \tparam ScanOp <b>[inferred]</b> Binary scan operator type having member <tt>T operator()(const T &a, const T &b)</tt>
214
__device__ __forceinline__ T ThreadScanInclusive(
215
T (&input)[LENGTH], ///< [in] Input array
216
T (&output)[LENGTH], ///< [out] Output array (may be aliased to \p input)
217
ScanOp scan_op, ///< [in] Binary scan operator
218
T prefix, ///< [in] Prefix to seed scan with
219
bool apply_prefix = true) ///< [in] Whether or not the calling thread should apply its prefix. (Handy for preventing thread-0 from applying a prefix.)
221
return ThreadScanInclusive<LENGTH>((T*) input, (T*) output, scan_op, prefix, apply_prefix);
225
//@} end member group
227
/** @} */ // end group ThreadModule
231
CUB_NS_POSTFIX // Optional outer namespace(s)