summaryrefslogtreecommitdiff
path: root/debug_tools/WatchYourStep/ptxjitplus/inc/cub/grid
diff options
context:
space:
mode:
authorMahmoud <[email protected]>2019-07-29 21:18:06 -0400
committerMahmoud <[email protected]>2019-07-29 21:18:06 -0400
commit5875fda72d4402413bc5c04ee5ec15085ff2b90a (patch)
treec920268d899df331e1a5e451a35133eaff7ca341 /debug_tools/WatchYourStep/ptxjitplus/inc/cub/grid
parentc05dc90da35fc2bd9dd42da9626bf3a60e2c9e8d (diff)
parent21d937256fbca004c926531cfef1adefcedeef91 (diff)
Merge branch 'dev' of https://github.com/mkhairy/gpgpu-sim-private into dev-purdue-integration-trace
Diffstat (limited to 'debug_tools/WatchYourStep/ptxjitplus/inc/cub/grid')
-rw-r--r--debug_tools/WatchYourStep/ptxjitplus/inc/cub/grid/grid_barrier.cuh211
-rw-r--r--debug_tools/WatchYourStep/ptxjitplus/inc/cub/grid/grid_even_share.cuh222
-rw-r--r--debug_tools/WatchYourStep/ptxjitplus/inc/cub/grid/grid_mapping.cuh113
-rw-r--r--debug_tools/WatchYourStep/ptxjitplus/inc/cub/grid/grid_queue.cuh220
4 files changed, 766 insertions, 0 deletions
diff --git a/debug_tools/WatchYourStep/ptxjitplus/inc/cub/grid/grid_barrier.cuh b/debug_tools/WatchYourStep/ptxjitplus/inc/cub/grid/grid_barrier.cuh
new file mode 100644
index 0000000..461fb44
--- /dev/null
+++ b/debug_tools/WatchYourStep/ptxjitplus/inc/cub/grid/grid_barrier.cuh
@@ -0,0 +1,211 @@
+/******************************************************************************
+ * Copyright (c) 2011, Duane Merrill. All rights reserved.
+ * Copyright (c) 2011-2018, NVIDIA CORPORATION. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name of the NVIDIA CORPORATION nor the
+ * names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ ******************************************************************************/
+
+/**
+ * \file
+ * cub::GridBarrier implements a software global barrier among thread blocks within a CUDA grid
+ */
+
+#pragma once
+
+#include "../util_debug.cuh"
+#include "../util_namespace.cuh"
+#include "../thread/thread_load.cuh"
+
+/// Optional outer namespace(s)
+CUB_NS_PREFIX
+
+/// CUB namespace
+namespace cub {
+
+
+/**
+ * \addtogroup GridModule
+ * @{
+ */
+
+
+/**
+ * \brief GridBarrier implements a software global barrier among thread blocks within a CUDA grid
+ */
+class GridBarrier
+{
+protected :
+
+ typedef unsigned int SyncFlag;
+
+ // Counters in global device memory
+ SyncFlag* d_sync;
+
+public:
+
+ /**
+ * Constructor
+ */
+ GridBarrier() : d_sync(NULL) {}
+
+
+ /**
+ * Synchronize
+ */
+ __device__ __forceinline__ void Sync() const
+ {
+ volatile SyncFlag *d_vol_sync = d_sync;
+
+ // Threadfence and syncthreads to make sure global writes are visible before
+ // thread-0 reports in with its sync counter
+ __threadfence();
+ CTA_SYNC();
+
+ if (blockIdx.x == 0)
+ {
+ // Report in ourselves
+ if (threadIdx.x == 0)
+ {
+ d_vol_sync[blockIdx.x] = 1;
+ }
+
+ CTA_SYNC();
+
+ // Wait for everyone else to report in
+ for (int peer_block = threadIdx.x; peer_block < gridDim.x; peer_block += blockDim.x)
+ {
+ while (ThreadLoad<LOAD_CG>(d_sync + peer_block) == 0)
+ {
+ __threadfence_block();
+ }
+ }
+
+ CTA_SYNC();
+
+ // Let everyone know it's safe to proceed
+ for (int peer_block = threadIdx.x; peer_block < gridDim.x; peer_block += blockDim.x)
+ {
+ d_vol_sync[peer_block] = 0;
+ }
+ }
+ else
+ {
+ if (threadIdx.x == 0)
+ {
+ // Report in
+ d_vol_sync[blockIdx.x] = 1;
+
+ // Wait for acknowledgment
+ while (ThreadLoad<LOAD_CG>(d_sync + blockIdx.x) == 1)
+ {
+ __threadfence_block();
+ }
+ }
+
+ CTA_SYNC();
+ }
+ }
+};
+
+
+/**
+ * \brief GridBarrierLifetime extends GridBarrier to provide lifetime management of the temporary device storage needed for cooperation.
+ *
+ * Uses RAII for lifetime, i.e., device resources are reclaimed when
+ * the destructor is called.
+ */
+class GridBarrierLifetime : public GridBarrier
+{
+protected:
+
+ // Number of bytes backed by d_sync
+ size_t sync_bytes;
+
+public:
+
+ /**
+ * Constructor
+ */
+ GridBarrierLifetime() : GridBarrier(), sync_bytes(0) {}
+
+
+ /**
+ * DeviceFrees and resets the progress counters
+ */
+ cudaError_t HostReset()
+ {
+ cudaError_t retval = cudaSuccess;
+ if (d_sync)
+ {
+ CubDebug(retval = cudaFree(d_sync));
+ d_sync = NULL;
+ }
+ sync_bytes = 0;
+ return retval;
+ }
+
+
+ /**
+ * Destructor
+ */
+ virtual ~GridBarrierLifetime()
+ {
+ HostReset();
+ }
+
+
+ /**
+ * Sets up the progress counters for the next kernel launch (lazily
+ * allocating and initializing them if necessary)
+ */
+ cudaError_t Setup(int sweep_grid_size)
+ {
+ cudaError_t retval = cudaSuccess;
+ do {
+ size_t new_sync_bytes = sweep_grid_size * sizeof(SyncFlag);
+ if (new_sync_bytes > sync_bytes)
+ {
+ if (d_sync)
+ {
+ if (CubDebug(retval = cudaFree(d_sync))) break;
+ }
+
+ sync_bytes = new_sync_bytes;
+
+ // Allocate and initialize to zero
+ if (CubDebug(retval = cudaMalloc((void**) &d_sync, sync_bytes))) break;
+ if (CubDebug(retval = cudaMemset(d_sync, 0, new_sync_bytes))) break;
+ }
+ } while (0);
+
+ return retval;
+ }
+};
+
+
+/** @} */ // end group GridModule
+
+} // CUB namespace
+CUB_NS_POSTFIX // Optional outer namespace(s)
+
diff --git a/debug_tools/WatchYourStep/ptxjitplus/inc/cub/grid/grid_even_share.cuh b/debug_tools/WatchYourStep/ptxjitplus/inc/cub/grid/grid_even_share.cuh
new file mode 100644
index 0000000..f0b3a69
--- /dev/null
+++ b/debug_tools/WatchYourStep/ptxjitplus/inc/cub/grid/grid_even_share.cuh
@@ -0,0 +1,222 @@
+/******************************************************************************
+ * Copyright (c) 2011, Duane Merrill. All rights reserved.
+ * Copyright (c) 2011-2018, NVIDIA CORPORATION. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name of the NVIDIA CORPORATION nor the
+ * names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ ******************************************************************************/
+
+/**
+ * \file
+ * cub::GridEvenShare is a descriptor utility for distributing input among CUDA thread blocks in an "even-share" fashion. Each thread block gets roughly the same number of fixed-size work units (grains).
+ */
+
+
+#pragma once
+
+#include "../util_namespace.cuh"
+#include "../util_macro.cuh"
+#include "grid_mapping.cuh"
+
+/// Optional outer namespace(s)
+CUB_NS_PREFIX
+
+/// CUB namespace
+namespace cub {
+
+
+/**
+ * \addtogroup GridModule
+ * @{
+ */
+
+
+/**
+ * \brief GridEvenShare is a descriptor utility for distributing input among
+ * CUDA thread blocks in an "even-share" fashion. Each thread block gets roughly
+ * the same number of input tiles.
+ *
+ * \par Overview
+ * Each thread block is assigned a consecutive sequence of input tiles. To help
+ * preserve alignment and eliminate the overhead of guarded loads for all but the
+ * last thread block, to GridEvenShare assigns one of three different amounts of
+ * work to a given thread block: "big", "normal", or "last". The "big" workloads
+ * are one scheduling grain larger than "normal". The "last" work unit for the
+ * last thread block may be partially-full if the input is not an even multiple of
+ * the scheduling grain size.
+ *
+ * \par
+ * Before invoking a child grid, a parent thread will typically construct an
+ * instance of GridEvenShare. The instance can be passed to child thread blocks
+ * which can initialize their per-thread block offsets using \p BlockInit().
+ */
+template <typename OffsetT>
+struct GridEvenShare
+{
+private:
+
+ OffsetT total_tiles;
+ int big_shares;
+ OffsetT big_share_items;
+ OffsetT normal_share_items;
+ OffsetT normal_base_offset;
+
+public:
+
+ /// Total number of input items
+ OffsetT num_items;
+
+ /// Grid size in thread blocks
+ int grid_size;
+
+ /// OffsetT into input marking the beginning of the owning thread block's segment of input tiles
+ OffsetT block_offset;
+
+ /// OffsetT into input of marking the end (one-past) of the owning thread block's segment of input tiles
+ OffsetT block_end;
+
+ /// Stride between input tiles
+ OffsetT block_stride;
+
+
+ /**
+ * \brief Constructor.
+ */
+ __host__ __device__ __forceinline__ GridEvenShare() :
+ total_tiles(0),
+ big_shares(0),
+ big_share_items(0),
+ normal_share_items(0),
+ normal_base_offset(0),
+ num_items(0),
+ grid_size(0),
+ block_offset(0),
+ block_end(0),
+ block_stride(0)
+ {}
+
+
+ /**
+ * \brief Dispatch initializer. To be called prior prior to kernel launch.
+ */
+ __host__ __device__ __forceinline__ void DispatchInit(
+ OffsetT num_items, ///< Total number of input items
+ int max_grid_size, ///< Maximum grid size allowable (actual grid size may be less if not warranted by the the number of input items)
+ int tile_items) ///< Number of data items per input tile
+ {
+ this->block_offset = num_items; // Initialize past-the-end
+ this->block_end = num_items; // Initialize past-the-end
+ this->num_items = num_items;
+ this->total_tiles = (num_items + tile_items - 1) / tile_items;
+ this->grid_size = CUB_MIN(total_tiles, max_grid_size);
+ OffsetT avg_tiles_per_block = total_tiles / grid_size;
+ this->big_shares = total_tiles - (avg_tiles_per_block * grid_size); // leftover grains go to big blocks
+ this->normal_share_items = avg_tiles_per_block * tile_items;
+ this->normal_base_offset = big_shares * tile_items;
+ this->big_share_items = normal_share_items + tile_items;
+ }
+
+
+ /**
+ * \brief Initializes ranges for the specified thread block index. Specialized
+ * for a "raking" access pattern in which each thread block is assigned a
+ * consecutive sequence of input tiles.
+ */
+ template <int TILE_ITEMS>
+ __device__ __forceinline__ void BlockInit(
+ int block_id,
+ Int2Type<GRID_MAPPING_RAKE> /*strategy_tag*/)
+ {
+ block_stride = TILE_ITEMS;
+ if (block_id < big_shares)
+ {
+ // This thread block gets a big share of grains (avg_tiles_per_block + 1)
+ block_offset = (block_id * big_share_items);
+ block_end = block_offset + big_share_items;
+ }
+ else if (block_id < total_tiles)
+ {
+ // This thread block gets a normal share of grains (avg_tiles_per_block)
+ block_offset = normal_base_offset + (block_id * normal_share_items);
+ block_end = CUB_MIN(num_items, block_offset + normal_share_items);
+ }
+ // Else default past-the-end
+ }
+
+
+ /**
+ * \brief Block-initialization, specialized for a "raking" access
+ * pattern in which each thread block is assigned a consecutive sequence
+ * of input tiles.
+ */
+ template <int TILE_ITEMS>
+ __device__ __forceinline__ void BlockInit(
+ int block_id,
+ Int2Type<GRID_MAPPING_STRIP_MINE> /*strategy_tag*/)
+ {
+ block_stride = grid_size * TILE_ITEMS;
+ block_offset = (block_id * TILE_ITEMS);
+ block_end = num_items;
+ }
+
+
+ /**
+ * \brief Block-initialization, specialized for "strip mining" access
+ * pattern in which the input tiles assigned to each thread block are
+ * separated by a stride equal to the the extent of the grid.
+ */
+ template <
+ int TILE_ITEMS,
+ GridMappingStrategy STRATEGY>
+ __device__ __forceinline__ void BlockInit()
+ {
+ BlockInit<TILE_ITEMS>(blockIdx.x, Int2Type<STRATEGY>());
+ }
+
+
+ /**
+ * \brief Block-initialization, specialized for a "raking" access
+ * pattern in which each thread block is assigned a consecutive sequence
+ * of input tiles.
+ */
+ template <int TILE_ITEMS>
+ __device__ __forceinline__ void BlockInit(
+ OffsetT block_offset, ///< [in] Threadblock begin offset (inclusive)
+ OffsetT block_end) ///< [in] Threadblock end offset (exclusive)
+ {
+ this->block_offset = block_offset;
+ this->block_end = block_end;
+ this->block_stride = TILE_ITEMS;
+ }
+
+
+};
+
+
+
+
+
+/** @} */ // end group GridModule
+
+} // CUB namespace
+CUB_NS_POSTFIX // Optional outer namespace(s)
diff --git a/debug_tools/WatchYourStep/ptxjitplus/inc/cub/grid/grid_mapping.cuh b/debug_tools/WatchYourStep/ptxjitplus/inc/cub/grid/grid_mapping.cuh
new file mode 100644
index 0000000..f0e9fde
--- /dev/null
+++ b/debug_tools/WatchYourStep/ptxjitplus/inc/cub/grid/grid_mapping.cuh
@@ -0,0 +1,113 @@
+/******************************************************************************
+ * Copyright (c) 2011, Duane Merrill. All rights reserved.
+ * Copyright (c) 2011-2018, NVIDIA CORPORATION. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name of the NVIDIA CORPORATION nor the
+ * names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ ******************************************************************************/
+
+/**
+ * \file
+ * cub::GridMappingStrategy enumerates alternative strategies for mapping constant-sized tiles of device-wide data onto a grid of CUDA thread blocks.
+ */
+
+#pragma once
+
+#include "../util_namespace.cuh"
+
+/// Optional outer namespace(s)
+CUB_NS_PREFIX
+
+/// CUB namespace
+namespace cub {
+
+
+/**
+ * \addtogroup GridModule
+ * @{
+ */
+
+
+/******************************************************************************
+ * Mapping policies
+ *****************************************************************************/
+
+
+/**
+ * \brief cub::GridMappingStrategy enumerates alternative strategies for mapping constant-sized tiles of device-wide data onto a grid of CUDA thread blocks.
+ */
+enum GridMappingStrategy
+{
+ /**
+ * \brief An a "raking" access pattern in which each thread block is
+ * assigned a consecutive sequence of input tiles
+ *
+ * \par Overview
+ * The input is evenly partitioned into \p p segments, where \p p is
+ * constant and corresponds loosely to the number of thread blocks that may
+ * actively reside on the target device. Each segment is comprised of
+ * consecutive tiles, where a tile is a small, constant-sized unit of input
+ * to be processed to completion before the thread block terminates or
+ * obtains more work. The kernel invokes \p p thread blocks, each
+ * of which iteratively consumes a segment of <em>n</em>/<em>p</em> elements
+ * in tile-size increments.
+ */
+ GRID_MAPPING_RAKE,
+
+ /**
+ * \brief An a "strip mining" access pattern in which the input tiles assigned
+ * to each thread block are separated by a stride equal to the the extent of
+ * the grid.
+ *
+ * \par Overview
+ * The input is evenly partitioned into \p p sets, where \p p is
+ * constant and corresponds loosely to the number of thread blocks that may
+ * actively reside on the target device. Each set is comprised of
+ * data tiles separated by stride \p tiles, where a tile is a small,
+ * constant-sized unit of input to be processed to completion before the
+ * thread block terminates or obtains more work. The kernel invokes \p p
+ * thread blocks, each of which iteratively consumes a segment of
+ * <em>n</em>/<em>p</em> elements in tile-size increments.
+ */
+ GRID_MAPPING_STRIP_MINE,
+
+ /**
+ * \brief A dynamic "queue-based" strategy for assigning input tiles to thread blocks.
+ *
+ * \par Overview
+ * The input is treated as a queue to be dynamically consumed by a grid of
+ * thread blocks. Work is atomically dequeued in tiles, where a tile is a
+ * unit of input to be processed to completion before the thread block
+ * terminates or obtains more work. The grid size \p p is constant,
+ * loosely corresponding to the number of thread blocks that may actively
+ * reside on the target device.
+ */
+ GRID_MAPPING_DYNAMIC,
+};
+
+
+/** @} */ // end group GridModule
+
+} // CUB namespace
+CUB_NS_POSTFIX // Optional outer namespace(s)
+
diff --git a/debug_tools/WatchYourStep/ptxjitplus/inc/cub/grid/grid_queue.cuh b/debug_tools/WatchYourStep/ptxjitplus/inc/cub/grid/grid_queue.cuh
new file mode 100644
index 0000000..9615b14
--- /dev/null
+++ b/debug_tools/WatchYourStep/ptxjitplus/inc/cub/grid/grid_queue.cuh
@@ -0,0 +1,220 @@
+/******************************************************************************
+ * Copyright (c) 2011, Duane Merrill. All rights reserved.
+ * Copyright (c) 2011-2018, NVIDIA CORPORATION. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name of the NVIDIA CORPORATION nor the
+ * names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ ******************************************************************************/
+
+/**
+ * \file
+ * cub::GridQueue is a descriptor utility for dynamic queue management.
+ */
+
+#pragma once
+
+#include "../util_namespace.cuh"
+#include "../util_debug.cuh"
+
+/// Optional outer namespace(s)
+CUB_NS_PREFIX
+
+/// CUB namespace
+namespace cub {
+
+
+/**
+ * \addtogroup GridModule
+ * @{
+ */
+
+
+/**
+ * \brief GridQueue is a descriptor utility for dynamic queue management.
+ *
+ * \par Overview
+ * GridQueue descriptors provides abstractions for "filling" or
+ * "draining" globally-shared vectors.
+ *
+ * \par
+ * A "filling" GridQueue works by atomically-adding to a zero-initialized counter,
+ * returning a unique offset for the calling thread to write its items.
+ * The GridQueue maintains the total "fill-size". The fill counter must be reset
+ * using GridQueue::ResetFill by the host or kernel instance prior to the kernel instance that
+ * will be filling.
+ *
+ * \par
+ * Similarly, a "draining" GridQueue works by works by atomically-incrementing a
+ * zero-initialized counter, returning a unique offset for the calling thread to
+ * read its items. Threads can safely drain until the array's logical fill-size is
+ * exceeded. The drain counter must be reset using GridQueue::ResetDrain or
+ * GridQueue::FillAndResetDrain by the host or kernel instance prior to the kernel instance that
+ * will be filling. (For dynamic work distribution of existing data, the corresponding fill-size
+ * is simply the number of elements in the array.)
+ *
+ * \par
+ * Iterative work management can be implemented simply with a pair of flip-flopping
+ * work buffers, each with an associated set of fill and drain GridQueue descriptors.
+ *
+ * \tparam OffsetT Signed integer type for global offsets
+ */
+template <typename OffsetT>
+class GridQueue
+{
+private:
+
+ /// Counter indices
+ enum
+ {
+ FILL = 0,
+ DRAIN = 1,
+ };
+
+ /// Pair of counters
+ OffsetT *d_counters;
+
+public:
+
+ /// Returns the device allocation size in bytes needed to construct a GridQueue instance
+ __host__ __device__ __forceinline__
+ static size_t AllocationSize()
+ {
+ return sizeof(OffsetT) * 2;
+ }
+
+
+ /// Constructs an invalid GridQueue descriptor
+ __host__ __device__ __forceinline__ GridQueue()
+ :
+ d_counters(NULL)
+ {}
+
+
+ /// Constructs a GridQueue descriptor around the device storage allocation
+ __host__ __device__ __forceinline__ GridQueue(
+ void *d_storage) ///< Device allocation to back the GridQueue. Must be at least as big as <tt>AllocationSize()</tt>.
+ :
+ d_counters((OffsetT*) d_storage)
+ {}
+
+
+ /// 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.
+ __host__ __device__ __forceinline__ cudaError_t FillAndResetDrain(
+ OffsetT fill_size,
+ cudaStream_t stream = 0)
+ {
+#if (CUB_PTX_ARCH > 0)
+ (void)stream;
+ d_counters[FILL] = fill_size;
+ d_counters[DRAIN] = 0;
+ return cudaSuccess;
+#else
+ OffsetT counters[2];
+ counters[FILL] = fill_size;
+ counters[DRAIN] = 0;
+ return CubDebug(cudaMemcpyAsync(d_counters, counters, sizeof(OffsetT) * 2, cudaMemcpyHostToDevice, stream));
+#endif
+ }
+
+
+ /// 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.
+ __host__ __device__ __forceinline__ cudaError_t ResetDrain(cudaStream_t stream = 0)
+ {
+#if (CUB_PTX_ARCH > 0)
+ (void)stream;
+ d_counters[DRAIN] = 0;
+ return cudaSuccess;
+#else
+ return CubDebug(cudaMemsetAsync(d_counters + DRAIN, 0, sizeof(OffsetT), stream));
+#endif
+ }
+
+
+ /// This operation resets the fill counter. To be called by the host or by a kernel prior to that which will be filling.
+ __host__ __device__ __forceinline__ cudaError_t ResetFill(cudaStream_t stream = 0)
+ {
+#if (CUB_PTX_ARCH > 0)
+ (void)stream;
+ d_counters[FILL] = 0;
+ return cudaSuccess;
+#else
+ return CubDebug(cudaMemsetAsync(d_counters + FILL, 0, sizeof(OffsetT), stream));
+#endif
+ }
+
+
+ /// Returns the fill-size established by the parent or by the previous kernel.
+ __host__ __device__ __forceinline__ cudaError_t FillSize(
+ OffsetT &fill_size,
+ cudaStream_t stream = 0)
+ {
+#if (CUB_PTX_ARCH > 0)
+ (void)stream;
+ fill_size = d_counters[FILL];
+ return cudaSuccess;
+#else
+ return CubDebug(cudaMemcpyAsync(&fill_size, d_counters + FILL, sizeof(OffsetT), cudaMemcpyDeviceToHost, stream));
+#endif
+ }
+
+
+ /// Drain \p num_items from the queue. Returns offset from which to read items. To be called from CUDA kernel.
+ __device__ __forceinline__ OffsetT Drain(OffsetT num_items)
+ {
+ return atomicAdd(d_counters + DRAIN, num_items);
+ }
+
+
+ /// Fill \p num_items into the queue. Returns offset from which to write items. To be called from CUDA kernel.
+ __device__ __forceinline__ OffsetT Fill(OffsetT num_items)
+ {
+ return atomicAdd(d_counters + FILL, num_items);
+ }
+};
+
+
+#ifndef DOXYGEN_SHOULD_SKIP_THIS // Do not document
+
+
+/**
+ * Reset grid queue (call with 1 block of 1 thread)
+ */
+template <typename OffsetT>
+__global__ void FillAndResetDrainKernel(
+ GridQueue<OffsetT> grid_queue,
+ OffsetT num_items)
+{
+ grid_queue.FillAndResetDrain(num_items);
+}
+
+
+
+#endif // DOXYGEN_SHOULD_SKIP_THIS
+
+
+/** @} */ // end group GridModule
+
+} // CUB namespace
+CUB_NS_POSTFIX // Optional outer namespace(s)
+
+