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

« back to all changes in this revision

Viewing changes to lib/kokkos/TPL/cub/grid/grid_queue.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
 * cub::GridQueue is a descriptor utility for dynamic queue management.
 
32
 */
 
33
 
 
34
#pragma once
 
35
 
 
36
#include "../util_namespace.cuh"
 
37
#include "../util_debug.cuh"
 
38
 
 
39
/// Optional outer namespace(s)
 
40
CUB_NS_PREFIX
 
41
 
 
42
/// CUB namespace
 
43
namespace cub {
 
44
 
 
45
 
 
46
/**
 
47
 * \addtogroup GridModule
 
48
 * @{
 
49
 */
 
50
 
 
51
 
 
52
/**
 
53
 * \brief GridQueue is a descriptor utility for dynamic queue management.
 
54
 *
 
55
 * \par Overview
 
56
 * GridQueue descriptors provides abstractions for "filling" or
 
57
 * "draining" globally-shared vectors.
 
58
 *
 
59
 * \par
 
60
 * A "filling" GridQueue works by atomically-adding to a zero-initialized counter,
 
61
 * returning a unique offset for the calling thread to write its items.
 
62
 * The GridQueue maintains the total "fill-size".  The fill counter must be reset
 
63
 * using GridQueue::ResetFill by the host or kernel instance prior to the kernel instance that
 
64
 * will be filling.
 
65
 *
 
66
 * \par
 
67
 * Similarly a "draining" GridQueue works by works by atomically-incrementing a
 
68
 * zero-initialized counter, returning a unique offset for the calling thread to
 
69
 * read its items. Threads can safely drain until the array's logical fill-size is
 
70
 * exceeded.  The drain counter must be reset using GridQueue::ResetDrain or
 
71
 * GridQueue::ResetDrainAfterFill by the host or kernel instance prior to the kernel instance that
 
72
 * will be filling.  (For dynamic work distribution of existing data, the corresponding fill-size
 
73
 * is simply the number of elements in the array.)
 
74
 *
 
75
 * \par
 
76
 * Iterative work management can be implemented simply with a pair of flip-flopping
 
77
 * work buffers, each with an associated set of fill and drain GridQueue descriptors.
 
78
 *
 
79
 * \tparam SizeT Integer type for array indexing
 
80
 */
 
81
template <typename SizeT>
 
82
class GridQueue
 
83
{
 
84
private:
 
85
 
 
86
    /// Counter indices
 
87
    enum
 
88
    {
 
89
        FILL    = 0,
 
90
        DRAIN   = 1,
 
91
    };
 
92
 
 
93
    /// Pair of counters
 
94
    SizeT *d_counters;
 
95
 
 
96
public:
 
97
 
 
98
    /// Returns the device allocation size in bytes needed to construct a GridQueue instance
 
99
    __host__ __device__ __forceinline__
 
100
    static size_t AllocationSize()
 
101
    {
 
102
        return sizeof(SizeT) * 2;
 
103
    }
 
104
 
 
105
 
 
106
    /// Constructs an invalid GridQueue descriptor around the device storage allocation
 
107
    __host__ __device__ __forceinline__ GridQueue(
 
108
        void *d_storage)                    ///< Device allocation to back the GridQueue.  Must be at least as big as <tt>AllocationSize()</tt>.
 
109
    :
 
110
        d_counters((SizeT*) d_storage)
 
111
    {}
 
112
 
 
113
 
 
114
    /// This operation resets the drain so that it may advance to meet the existing fill-size.  To be called by the host or by a kernel prior to that which will be draining.
 
115
    __host__ __device__ __forceinline__ cudaError_t ResetDrainAfterFill(cudaStream_t stream = 0)
 
116
    {
 
117
#ifdef __CUDA_ARCH__
 
118
        d_counters[DRAIN] = 0;
 
119
        return cudaSuccess;
 
120
#else
 
121
        return ResetDrain(0, stream);
 
122
#endif
 
123
    }
 
124
 
 
125
    /// This operation sets the fill-size and resets the drain counter, preparing the GridQueue for draining in the next kernel instance.  To be called by the host or by a kernel prior to that which will be draining.
 
126
    __host__ __device__ __forceinline__ cudaError_t ResetDrain(
 
127
        SizeT fill_size,
 
128
        cudaStream_t stream = 0)
 
129
    {
 
130
#ifdef __CUDA_ARCH__
 
131
        d_counters[FILL] = fill_size;
 
132
        d_counters[DRAIN] = 0;
 
133
        return cudaSuccess;
 
134
#else
 
135
        SizeT counters[2];
 
136
        counters[FILL] = fill_size;
 
137
        counters[DRAIN] = 0;
 
138
        return CubDebug(cudaMemcpyAsync(d_counters, counters, sizeof(SizeT) * 2, cudaMemcpyHostToDevice, stream));
 
139
#endif
 
140
    }
 
141
 
 
142
 
 
143
    /// This operation resets the fill counter.  To be called by the host or by a kernel prior to that which will be filling.
 
144
    __host__ __device__ __forceinline__ cudaError_t ResetFill()
 
145
    {
 
146
#ifdef __CUDA_ARCH__
 
147
        d_counters[FILL] = 0;
 
148
        return cudaSuccess;
 
149
#else
 
150
        return CubDebug(cudaMemset(d_counters + FILL, 0, sizeof(SizeT)));
 
151
#endif
 
152
    }
 
153
 
 
154
 
 
155
    /// Returns the fill-size established by the parent or by the previous kernel.
 
156
    __host__ __device__ __forceinline__ cudaError_t FillSize(
 
157
        SizeT &fill_size,
 
158
        cudaStream_t stream = 0)
 
159
    {
 
160
#ifdef __CUDA_ARCH__
 
161
        fill_size = d_counters[FILL];
 
162
#else
 
163
        return CubDebug(cudaMemcpyAsync(&fill_size, d_counters + FILL, sizeof(SizeT), cudaMemcpyDeviceToHost, stream));
 
164
#endif
 
165
    }
 
166
 
 
167
 
 
168
    /// Drain num_items.  Returns offset from which to read items.
 
169
    __device__ __forceinline__ SizeT Drain(SizeT num_items)
 
170
    {
 
171
        return atomicAdd(d_counters + DRAIN, num_items);
 
172
    }
 
173
 
 
174
 
 
175
    /// Fill num_items.  Returns offset from which to write items.
 
176
    __device__ __forceinline__ SizeT Fill(SizeT num_items)
 
177
    {
 
178
        return atomicAdd(d_counters + FILL, num_items);
 
179
    }
 
180
};
 
181
 
 
182
 
 
183
#ifndef DOXYGEN_SHOULD_SKIP_THIS    // Do not document
 
184
 
 
185
 
 
186
/**
 
187
 * Reset grid queue (call with 1 block of 1 thread)
 
188
 */
 
189
template <typename SizeT>
 
190
__global__ void ResetDrainKernel(
 
191
    GridQueue<SizeT>    grid_queue,
 
192
    SizeT               num_items)
 
193
{
 
194
    grid_queue.ResetDrain(num_items);
 
195
}
 
196
 
 
197
 
 
198
 
 
199
#endif // DOXYGEN_SHOULD_SKIP_THIS
 
200
 
 
201
 
 
202
/** @} */       // end group GridModule
 
203
 
 
204
}               // CUB namespace
 
205
CUB_NS_POSTFIX  // Optional outer namespace(s)
 
206
 
 
207