~ubuntu-branches/debian/sid/lammps/sid

« back to all changes in this revision

Viewing changes to lib/kokkos/TPL/cub/thread/thread_scan.cuh

  • Committer: Package Import Robot
  • Author(s): Anton Gladky
  • Date: 2015-04-29 23:44:49 UTC
  • mfrom: (5.1.3 experimental)
  • Revision ID: package-import@ubuntu.com-20150429234449-mbhy9utku6hp6oq8
Tags: 0~20150313.gitfa668e1-1
Upload into unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
 * Copyright (c) 2011, Duane Merrill.  All rights reserved.
 
3
 * Copyright (c) 2011-2013, 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
 
 
29
/**
 
30
 * \file
 
31
 * Thread utilities for sequential prefix scan over statically-sized array types
 
32
 */
 
33
 
 
34
#pragma once
 
35
 
 
36
#include "../thread/thread_operators.cuh"
 
37
#include "../util_namespace.cuh"
 
38
 
 
39
/// Optional outer namespace(s)
 
40
CUB_NS_PREFIX
 
41
 
 
42
/// CUB namespace
 
43
namespace cub {
 
44
 
 
45
/**
 
46
 * \addtogroup ThreadModule
 
47
 * @{
 
48
 */
 
49
 
 
50
/**
 
51
 * \name Sequential prefix scan over statically-sized array types
 
52
 * @{
 
53
 */
 
54
 
 
55
/**
 
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.
 
57
 *
 
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>
 
61
 */
 
62
template <
 
63
    int         LENGTH,
 
64
    typename    T,
 
65
    typename    ScanOp>
 
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.)
 
72
{
 
73
    T inclusive = input[0];
 
74
    if (apply_prefix)
 
75
    {
 
76
        inclusive = scan_op(prefix, inclusive);
 
77
    }
 
78
    output[0] = prefix;
 
79
    T exclusive = inclusive;
 
80
 
 
81
    #pragma unroll
 
82
    for (int i = 1; i < LENGTH; ++i)
 
83
    {
 
84
        inclusive = scan_op(exclusive, input[i]);
 
85
        output[i] = exclusive;
 
86
        exclusive = inclusive;
 
87
    }
 
88
 
 
89
    return inclusive;
 
90
}
 
91
 
 
92
 
 
93
/**
 
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.
 
95
 *
 
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>
 
99
 */
 
100
template <
 
101
    int         LENGTH,
 
102
    typename    T,
 
103
    typename    ScanOp>
 
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.)
 
110
{
 
111
    return ThreadScanExclusive<LENGTH>((T*) input, (T*) output, scan_op, prefix);
 
112
}
 
113
 
 
114
 
 
115
/**
 
116
 * \brief Perform a sequential inclusive prefix scan over \p LENGTH elements of the \p input array.  The aggregate is returned.
 
117
 *
 
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>
 
121
 */
 
122
template <
 
123
    int         LENGTH,
 
124
    typename    T,
 
125
    typename    ScanOp>
 
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
 
130
{
 
131
    T inclusive = input[0];
 
132
    output[0] = inclusive;
 
133
 
 
134
    // Continue scan
 
135
    #pragma unroll
 
136
    for (int i = 0; i < LENGTH; ++i)
 
137
    {
 
138
        inclusive = scan_op(inclusive, input[i]);
 
139
        output[i] = inclusive;
 
140
    }
 
141
 
 
142
    return inclusive;
 
143
}
 
144
 
 
145
 
 
146
/**
 
147
 * \brief Perform a sequential inclusive prefix scan over the statically-sized \p input array.  The aggregate is returned.
 
148
 *
 
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>
 
152
 */
 
153
template <
 
154
    int         LENGTH,
 
155
    typename    T,
 
156
    typename    ScanOp>
 
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
 
161
{
 
162
    return ThreadScanInclusive<LENGTH>((T*) input, (T*) output, scan_op);
 
163
}
 
164
 
 
165
 
 
166
/**
 
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.
 
168
 *
 
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>
 
172
 */
 
173
template <
 
174
    int         LENGTH,
 
175
    typename    T,
 
176
    typename    ScanOp>
 
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.)
 
183
{
 
184
    T inclusive = input[0];
 
185
    if (apply_prefix)
 
186
    {
 
187
        inclusive = scan_op(prefix, inclusive);
 
188
    }
 
189
    output[0] = inclusive;
 
190
 
 
191
    // Continue scan
 
192
    #pragma unroll
 
193
    for (int i = 1; i < LENGTH; ++i)
 
194
    {
 
195
        inclusive = scan_op(inclusive, input[i]);
 
196
        output[i] = inclusive;
 
197
    }
 
198
 
 
199
    return inclusive;
 
200
}
 
201
 
 
202
 
 
203
/**
 
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.
 
205
 *
 
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>
 
209
 */
 
210
template <
 
211
    int         LENGTH,
 
212
    typename    T,
 
213
    typename    ScanOp>
 
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.)
 
220
{
 
221
    return ThreadScanInclusive<LENGTH>((T*) input, (T*) output, scan_op, prefix, apply_prefix);
 
222
}
 
223
 
 
224
 
 
225
//@}  end member group
 
226
 
 
227
/** @} */       // end group ThreadModule
 
228
 
 
229
 
 
230
}               // CUB namespace
 
231
CUB_NS_POSTFIX  // Optional outer namespace(s)