/******************************************************************************
 * Copyright (c) 2011, Duane Merrill.  All rights reserved.
 * Copyright (c) 2011-2014, 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
 * BlockRangeRadixSortDownsweep implements a stateful abstraction of CUDA thread blocks for participating in device-wide radix sort downsweep across a range of tiles.
 */


#pragma once

#include "../thread/thread_load.cuh"
#include "../block/block_load.cuh"
#include "../block/block_store.cuh"
#include "../block/block_radix_rank.cuh"
#include "../block/block_exchange.cuh"
#include "../util_type.cuh"
#include "../iterator/cache_modified_input_iterator.cuh"
#include "../util_namespace.cuh"

/// Optional outer namespace(s)
CUB_NS_PREFIX

/// CUB namespace
namespace cub {


/******************************************************************************
 * Tuning policy types
 ******************************************************************************/

/**
 * Types of scattering strategies
 */
enum RadixSortScatterAlgorithm
{
    RADIX_SORT_SCATTER_DIRECT,      ///< Scatter directly from registers to global bins
    RADIX_SORT_SCATTER_TWO_PHASE,   ///< First scatter from registers into shared memory bins, then into global bins
};


/**
 * Parameterizable tuning policy type for BlockRangeRadixSortDownsweep
 */
template <
    int                         _BLOCK_THREADS,             ///< Threads per thread block
    int                         _ITEMS_PER_THREAD,          ///< Items per thread (per tile of input)
    BlockLoadAlgorithm          _LOAD_ALGORITHM,            ///< The BlockLoad algorithm to use
    CacheLoadModifier           _LOAD_MODIFIER,             ///< Cache load modifier for reading keys (and values)
    bool                        _EXCHANGE_TIME_SLICING,     ///< Whether or not to time-slice key/value exchanges through shared memory to lower shared memory pressure
    bool                        _MEMOIZE_OUTER_SCAN,        ///< Whether or not to buffer outer raking scan partials to incur fewer shared memory reads at the expense of higher register pressure.  See BlockScanAlgorithm::BLOCK_SCAN_RAKING_MEMOIZE for more details.
    BlockScanAlgorithm          _INNER_SCAN_ALGORITHM,      ///< The BlockScan algorithm algorithm to use
    RadixSortScatterAlgorithm   _SCATTER_ALGORITHM,         ///< The scattering strategy to use
    cudaSharedMemConfig         _SMEM_CONFIG,               ///< Shared memory bank mode
    int                         _RADIX_BITS>                ///< The number of radix bits, i.e., log2(bins)
struct BlockRangeRadixSortDownsweepPolicy
{
    enum
    {
        BLOCK_THREADS           = _BLOCK_THREADS,           ///< Threads per thread block
        ITEMS_PER_THREAD        = _ITEMS_PER_THREAD,        ///< Items per thread (per tile of input)
        EXCHANGE_TIME_SLICING   = _EXCHANGE_TIME_SLICING,   ///< Whether or not to time-slice key/value exchanges through shared memory to lower shared memory pressure
        RADIX_BITS              = _RADIX_BITS,              ///< The number of radix bits, i.e., log2(bins)
        MEMOIZE_OUTER_SCAN      = _MEMOIZE_OUTER_SCAN,      ///< Whether or not to buffer outer raking scan partials to incur fewer shared memory reads at the expense of higher register pressure.  See BlockScanAlgorithm::BLOCK_SCAN_RAKING_MEMOIZE for more details.
    };

    static const BlockLoadAlgorithm         LOAD_ALGORITHM          = _LOAD_ALGORITHM;          ///< The BlockLoad algorithm to use
    static const CacheLoadModifier          LOAD_MODIFIER           = _LOAD_MODIFIER;           ///< Cache load modifier for reading keys (and values)
    static const BlockScanAlgorithm         INNER_SCAN_ALGORITHM    = _INNER_SCAN_ALGORITHM;    ///< The BlockScan algorithm algorithm to use
    static const RadixSortScatterAlgorithm  SCATTER_ALGORITHM       = _SCATTER_ALGORITHM;       ///< The scattering strategy to use
    static const cudaSharedMemConfig        SMEM_CONFIG             = _SMEM_CONFIG;             ///< Shared memory bank mode
};


/******************************************************************************
 * Thread block abstractions
 ******************************************************************************/

/**
 * \brief BlockRangeRadixSortDownsweep implements a stateful abstraction of CUDA thread blocks for participating in device-wide radix sort downsweep across a range of tiles.
 */
template <
    typename BlockRangeRadixSortDownsweepPolicy,        ///< Parameterized BlockRangeRadixSortDownsweepPolicy tuning policy type
    bool     DESCENDING,                                   ///< Whether or not the sorted-order is high-to-low
    typename Key,                                       ///< Key type
    typename Value,                                     ///< Value type
    typename Offset>                                    ///< Signed integer type for global offsets
struct BlockRangeRadixSortDownsweep
{
    //---------------------------------------------------------------------
    // Type definitions and constants
    //---------------------------------------------------------------------

    // Appropriate unsigned-bits representation of Key
    typedef typename Traits<Key>::UnsignedBits UnsignedBits;

    static const UnsignedBits MIN_KEY = Traits<Key>::MIN_KEY;
    static const UnsignedBits MAX_KEY = Traits<Key>::MAX_KEY;

    static const BlockLoadAlgorithm         LOAD_ALGORITHM          = BlockRangeRadixSortDownsweepPolicy::LOAD_ALGORITHM;
    static const CacheLoadModifier          LOAD_MODIFIER           = BlockRangeRadixSortDownsweepPolicy::LOAD_MODIFIER;
    static const BlockScanAlgorithm         INNER_SCAN_ALGORITHM    = BlockRangeRadixSortDownsweepPolicy::INNER_SCAN_ALGORITHM;
    static const RadixSortScatterAlgorithm  SCATTER_ALGORITHM       = BlockRangeRadixSortDownsweepPolicy::SCATTER_ALGORITHM;
    static const cudaSharedMemConfig        SMEM_CONFIG             = BlockRangeRadixSortDownsweepPolicy::SMEM_CONFIG;

    enum
    {
        BLOCK_THREADS           = BlockRangeRadixSortDownsweepPolicy::BLOCK_THREADS,
        ITEMS_PER_THREAD        = BlockRangeRadixSortDownsweepPolicy::ITEMS_PER_THREAD,
        EXCHANGE_TIME_SLICING   = BlockRangeRadixSortDownsweepPolicy::EXCHANGE_TIME_SLICING,
        RADIX_BITS              = BlockRangeRadixSortDownsweepPolicy::RADIX_BITS,
        MEMOIZE_OUTER_SCAN      = BlockRangeRadixSortDownsweepPolicy::MEMOIZE_OUTER_SCAN,
        TILE_ITEMS              = BLOCK_THREADS * ITEMS_PER_THREAD,

        RADIX_DIGITS            = 1 << RADIX_BITS,
        KEYS_ONLY               = Equals<Value, NullType>::VALUE,

        WARP_THREADS            = CUB_PTX_LOG_WARP_THREADS,
        WARPS                   = (BLOCK_THREADS + WARP_THREADS - 1) / WARP_THREADS,

        BYTES_PER_SIZET         = sizeof(Offset),
        LOG_BYTES_PER_SIZET     = Log2<BYTES_PER_SIZET>::VALUE,

        LOG_SMEM_BANKS          = CUB_PTX_LOG_SMEM_BANKS,
        SMEM_BANKS              = 1 << LOG_SMEM_BANKS,

        DIGITS_PER_SCATTER_PASS = BLOCK_THREADS / SMEM_BANKS,
        SCATTER_PASSES          = RADIX_DIGITS / DIGITS_PER_SCATTER_PASS,

        LOG_STORE_TXN_THREADS   = LOG_SMEM_BANKS,
        STORE_TXN_THREADS       = 1 << LOG_STORE_TXN_THREADS,
    };

    // Input iterator wrapper types
    typedef CacheModifiedInputIterator<LOAD_MODIFIER, UnsignedBits, Offset>  KeysItr;
    typedef CacheModifiedInputIterator<LOAD_MODIFIER, Value, Offset>         ValuesItr;

    // BlockRadixRank type
    typedef BlockRadixRank<
        BLOCK_THREADS,
        RADIX_BITS,
        DESCENDING,
        MEMOIZE_OUTER_SCAN,
        INNER_SCAN_ALGORITHM,
        SMEM_CONFIG> BlockRadixRank;

    // BlockLoad type (keys)
    typedef BlockLoad<
        KeysItr,
        BLOCK_THREADS,
        ITEMS_PER_THREAD,
        LOAD_ALGORITHM,
        EXCHANGE_TIME_SLICING> BlockLoadKeys;

    // BlockLoad type (values)
    typedef BlockLoad<
        ValuesItr,
        BLOCK_THREADS,
        ITEMS_PER_THREAD,
        LOAD_ALGORITHM,
        EXCHANGE_TIME_SLICING> BlockLoadValues;

    // BlockExchange type (keys)
    typedef BlockExchange<
        UnsignedBits,
        BLOCK_THREADS,
        ITEMS_PER_THREAD,
        EXCHANGE_TIME_SLICING> BlockExchangeKeys;

    // BlockExchange type (values)
    typedef BlockExchange<
        Value,
        BLOCK_THREADS,
        ITEMS_PER_THREAD,
        EXCHANGE_TIME_SLICING> BlockExchangeValues;


    /**
     * Shared memory storage layout
     */
    struct _TempStorage
    {
        Offset  relative_bin_offsets[RADIX_DIGITS + 1];
        bool    short_circuit;

        union
        {
            typename BlockRadixRank::TempStorage        ranking;
            typename BlockLoadKeys::TempStorage         load_keys;
            typename BlockLoadValues::TempStorage       load_values;
            typename BlockExchangeKeys::TempStorage     exchange_keys;
            typename BlockExchangeValues::TempStorage   exchange_values;
        };
    };


    /// Alias wrapper allowing storage to be unioned
    struct TempStorage : Uninitialized<_TempStorage> {};


    //---------------------------------------------------------------------
    // Thread fields
    //---------------------------------------------------------------------

    // Shared storage for this CTA
    _TempStorage    &temp_storage;

    // Input and output device pointers
    KeysItr         d_keys_in;
    ValuesItr       d_values_in;
    UnsignedBits    *d_keys_out;
    Value           *d_values_out;

    // The global scatter base offset for each digit (valid in the first RADIX_DIGITS threads)
    Offset          bin_offset;

    // The least-significant bit position of the current digit to extract
    int             current_bit;

    // Number of bits in current digit
    int             num_bits;

    // Whether to short-ciruit
    bool            short_circuit;



    //---------------------------------------------------------------------
    // Utility methods
    //---------------------------------------------------------------------

    /**
     * Decodes given keys to lookup digit offsets in shared memory
     */
    __device__ __forceinline__ void DecodeRelativeBinOffsets(
        UnsignedBits    (&twiddled_keys)[ITEMS_PER_THREAD],
        Offset          (&relative_bin_offsets)[ITEMS_PER_THREAD])
    {
        #pragma unroll
        for (int KEY = 0; KEY < ITEMS_PER_THREAD; KEY++)
        {
            UnsignedBits digit = BFE(twiddled_keys[KEY], current_bit, num_bits);

            // Lookup base digit offset from shared memory
            relative_bin_offsets[KEY] = temp_storage.relative_bin_offsets[digit];
        }
    }


    /**
     * Scatter ranked items to global memory
     */
    template <bool FULL_TILE, typename T>
    __device__ __forceinline__ void ScatterItems(
        T       (&items)[ITEMS_PER_THREAD],
        int     (&local_ranks)[ITEMS_PER_THREAD],
        Offset  (&relative_bin_offsets)[ITEMS_PER_THREAD],
        T       *d_out,
        Offset  valid_items)
    {
        #pragma unroll
        for (int ITEM = 0; ITEM < ITEMS_PER_THREAD; ++ITEM)
        {
            // Scatter if not out-of-bounds
            if (FULL_TILE || (local_ranks[ITEM] < valid_items))
            {
                d_out[relative_bin_offsets[ITEM] + local_ranks[ITEM]] = items[ITEM];
            }
        }
    }


    /**
     * Scatter ranked keys directly to global memory
     */
    template <bool FULL_TILE>
    __device__ __forceinline__ void ScatterKeys(
        UnsignedBits                            (&twiddled_keys)[ITEMS_PER_THREAD],
        Offset                                  (&relative_bin_offsets)[ITEMS_PER_THREAD],
        int                                     (&ranks)[ITEMS_PER_THREAD],
        Offset                                  valid_items,
        Int2Type<RADIX_SORT_SCATTER_DIRECT>     scatter_algorithm)
    {
        // Compute scatter offsets
        DecodeRelativeBinOffsets(twiddled_keys, relative_bin_offsets);

        // Untwiddle keys before outputting
        UnsignedBits keys[ITEMS_PER_THREAD];

        #pragma unroll
        for (int KEY = 0; KEY < ITEMS_PER_THREAD; KEY++)
        {
            keys[KEY] = Traits<Key>::TwiddleOut(twiddled_keys[KEY]);
        }

        // Scatter to global
        ScatterItems<FULL_TILE>(keys, ranks, relative_bin_offsets, d_keys_out, valid_items);
    }


    /**
     * Scatter ranked keys through shared memory, then to global memory
     */
    template <bool FULL_TILE>
    __device__ __forceinline__ void ScatterKeys(
        UnsignedBits                            (&twiddled_keys)[ITEMS_PER_THREAD],
        Offset                                  (&relative_bin_offsets)[ITEMS_PER_THREAD],
        int                                     (&ranks)[ITEMS_PER_THREAD],
        Offset                                  valid_items,
        Int2Type<RADIX_SORT_SCATTER_TWO_PHASE>  scatter_algorithm)
    {
        // Exchange keys through shared memory
        BlockExchangeKeys(temp_storage.exchange_keys).ScatterToStriped(twiddled_keys, ranks);

        // Compute striped local ranks
        int local_ranks[ITEMS_PER_THREAD];

        #pragma unroll
        for (int ITEM = 0; ITEM < ITEMS_PER_THREAD; ++ITEM)
        {
            local_ranks[ITEM] = threadIdx.x + (ITEM * BLOCK_THREADS);
        }

        // Scatter directly
        ScatterKeys<FULL_TILE>(
            twiddled_keys,
            relative_bin_offsets,
            local_ranks,
            valid_items,
            Int2Type<RADIX_SORT_SCATTER_DIRECT>());
    }


    /**
     * Scatter ranked values directly to global memory
     */
    template <bool FULL_TILE>
    __device__ __forceinline__ void ScatterValues(
        Value                                   (&values)[ITEMS_PER_THREAD],
        Offset                                  (&relative_bin_offsets)[ITEMS_PER_THREAD],
        int                                     (&ranks)[ITEMS_PER_THREAD],
        Offset                                  valid_items,
        Int2Type<RADIX_SORT_SCATTER_DIRECT>     scatter_algorithm)
    {
        // Scatter to global
        ScatterItems<FULL_TILE>(values, ranks, relative_bin_offsets, d_values_out, valid_items);
    }


    /**
     * Scatter ranked values through shared memory, then to global memory
     */
    template <bool FULL_TILE>
    __device__ __forceinline__ void ScatterValues(
        Value                                   (&values)[ITEMS_PER_THREAD],
        Offset                                  (&relative_bin_offsets)[ITEMS_PER_THREAD],
        int                                     (&ranks)[ITEMS_PER_THREAD],
        Offset                                  valid_items,
        Int2Type<RADIX_SORT_SCATTER_TWO_PHASE>  scatter_algorithm)
    {
        __syncthreads();

        // Exchange keys through shared memory
        BlockExchangeValues(temp_storage.exchange_values).ScatterToStriped(values, ranks);

        // Compute striped local ranks
        int local_ranks[ITEMS_PER_THREAD];

        #pragma unroll
        for (int ITEM = 0; ITEM < ITEMS_PER_THREAD; ++ITEM)
        {
            local_ranks[ITEM] = threadIdx.x + (ITEM * BLOCK_THREADS);
        }

        // Scatter directly
        ScatterValues<FULL_TILE>(
            values,
            relative_bin_offsets,
            local_ranks,
            valid_items,
            Int2Type<RADIX_SORT_SCATTER_DIRECT>());
    }


    /**
     * Load a tile of items (specialized for full tile)
     */
    template <typename BlockLoadT, typename T, typename InputIterator>
    __device__ __forceinline__ void LoadItems(
        BlockLoadT      &block_loader, 
        T               (&items)[ITEMS_PER_THREAD],
        InputIterator   d_in,
        Offset          valid_items,
        Int2Type<true>  is_full_tile)
    {
        block_loader.Load(d_in, items);
    }


    /**
     * Load a tile of items (specialized for partial tile)
     */
    template <typename BlockLoadT, typename T, typename InputIterator>
    __device__ __forceinline__ void LoadItems(
        BlockLoadT      &block_loader, 
        T               (&items)[ITEMS_PER_THREAD],
        InputIterator   d_in,
        Offset          valid_items,
        Int2Type<false> is_full_tile)
    {
        block_loader.Load(d_in, items, valid_items);
    }


    /**
     * Truck along associated values
     */
    template <bool FULL_TILE, typename _Value>
    __device__ __forceinline__ void GatherScatterValues(
        _Value      (&values)[ITEMS_PER_THREAD],
        Offset      (&relative_bin_offsets)[ITEMS_PER_THREAD],
        int         (&ranks)[ITEMS_PER_THREAD],
        Offset      block_offset,
        Offset      valid_items)
    {
        __syncthreads();

        BlockLoadValues loader(temp_storage.load_values);
        LoadItems(
            loader,
            values,
            d_values_in + block_offset,
            valid_items,
            Int2Type<FULL_TILE>());

        ScatterValues<FULL_TILE>(
            values,
            relative_bin_offsets,
            ranks,
            valid_items,
            Int2Type<SCATTER_ALGORITHM>());
    }


    /**
     * Truck along associated values (specialized for key-only sorting)
     */
    template <bool FULL_TILE>
    __device__ __forceinline__ void GatherScatterValues(
        NullType    (&values)[ITEMS_PER_THREAD],
        Offset      (&relative_bin_offsets)[ITEMS_PER_THREAD],
        int         (&ranks)[ITEMS_PER_THREAD],
        Offset      block_offset,
        Offset      valid_items)
    {}


    /**
     * Process tile
     */
    template <bool FULL_TILE>
    __device__ __forceinline__ void ProcessTile(
        Offset block_offset,
        const Offset &valid_items = TILE_ITEMS)
    {
        // Per-thread tile data
        UnsignedBits    keys[ITEMS_PER_THREAD];                     // Keys
        UnsignedBits    twiddled_keys[ITEMS_PER_THREAD];            // Twiddled keys
        int             ranks[ITEMS_PER_THREAD];                    // For each key, the local rank within the CTA
        Offset          relative_bin_offsets[ITEMS_PER_THREAD];     // For each key, the global scatter base offset of the corresponding digit

        // Assign max-key to all keys
        #pragma unroll
        for (int ITEM = 0; ITEM < ITEMS_PER_THREAD; ++ITEM)
        {
            keys[ITEM] = (DESCENDING) ? MIN_KEY : MAX_KEY;
        }

        // Load tile of keys
        BlockLoadKeys loader(temp_storage.load_keys);
        LoadItems(
            loader,
            keys,
            d_keys_in + block_offset,
            valid_items, 
            Int2Type<FULL_TILE>());

        __syncthreads();

        // Twiddle key bits if necessary
        #pragma unroll
        for (int KEY = 0; KEY < ITEMS_PER_THREAD; KEY++)
        {
            twiddled_keys[KEY] = Traits<Key>::TwiddleIn(keys[KEY]);
        }

        // Rank the twiddled keys
        int inclusive_digit_prefix;
        BlockRadixRank(temp_storage.ranking).RankKeys(
            twiddled_keys,
            ranks,
            current_bit,
            num_bits,
            inclusive_digit_prefix);

        // Update global scatter base offsets for each digit
        if ((BLOCK_THREADS == RADIX_DIGITS) || (threadIdx.x < RADIX_DIGITS))
        {
            int exclusive_digit_prefix;

            // Get exclusive digit prefix from inclusive prefix
            if (DESCENDING)
            {
                // Get the prefix from the next thread (higher bins come first)
#if CUB_PTX_ARCH >= 300
                exclusive_digit_prefix = ShuffleDown(inclusive_digit_prefix, 1);
                if (threadIdx.x == RADIX_DIGITS - 1)
                    exclusive_digit_prefix = 0;
#else
                volatile int* exchange = reinterpret_cast<int *>(temp_storage.relative_bin_offsets);
                exchange[threadIdx.x + 1] = 0;
                exchange[threadIdx.x] = inclusive_digit_prefix;
                exclusive_digit_prefix = exchange[threadIdx.x + 1];
#endif
            }
            else
            {
                // Get the prefix from the previous thread (lower bins come first)
#if CUB_PTX_ARCH >= 300
                exclusive_digit_prefix = ShuffleUp(inclusive_digit_prefix, 1);
                if (threadIdx.x == 0)
                    exclusive_digit_prefix = 0;
#else
                volatile int* exchange = reinterpret_cast<int *>(temp_storage.relative_bin_offsets);
                exchange[threadIdx.x] = 0;
                exchange[threadIdx.x + 1] = inclusive_digit_prefix;
                exclusive_digit_prefix = exchange[threadIdx.x];
#endif
            }

            bin_offset -= exclusive_digit_prefix;
            temp_storage.relative_bin_offsets[threadIdx.x] = bin_offset;
            bin_offset += inclusive_digit_prefix;
        }

        __syncthreads();

        // Scatter keys
        ScatterKeys<FULL_TILE>(twiddled_keys, relative_bin_offsets, ranks, valid_items, Int2Type<SCATTER_ALGORITHM>());

        // Gather/scatter values
        Value values[ITEMS_PER_THREAD];
        GatherScatterValues<FULL_TILE>(values, relative_bin_offsets, ranks, block_offset, valid_items);
    }


    /**
     * Copy tiles within the range of input
     */
    template <
        typename InputIterator,
        typename T>
    __device__ __forceinline__ void Copy(
        InputIterator   d_in,
        T               *d_out,
        Offset          block_offset,
        Offset          block_end)
    {
        // Simply copy the input
        while (block_offset + TILE_ITEMS <= block_end)
        {
            T items[ITEMS_PER_THREAD];

            LoadDirectStriped<BLOCK_THREADS>(threadIdx.x, d_in + block_offset, items);
            __syncthreads();
            StoreDirectStriped<BLOCK_THREADS>(threadIdx.x, d_out + block_offset, items);

            block_offset += TILE_ITEMS;
        }

        // Clean up last partial tile with guarded-I/O
        if (block_offset < block_end)
        {
            Offset valid_items = block_end - block_offset;

            T items[ITEMS_PER_THREAD];

            LoadDirectStriped<BLOCK_THREADS>(threadIdx.x, d_in + block_offset, items, valid_items);
            __syncthreads();
            StoreDirectStriped<BLOCK_THREADS>(threadIdx.x, d_out + block_offset, items, valid_items);
        }
    }


    /**
     * Copy tiles within the range of input (specialized for NullType)
     */
    template <typename InputIterator>
    __device__ __forceinline__ void Copy(
        InputIterator   d_in,
        NullType        *d_out,
        Offset          block_offset,
        Offset          block_end)
    {}


    //---------------------------------------------------------------------
    // Interface
    //---------------------------------------------------------------------

    /**
     * Constructor
     */
    __device__ __forceinline__ BlockRangeRadixSortDownsweep(
        TempStorage &temp_storage,
        Offset       bin_offset,
        Key         *d_keys_in,
        Key         *d_keys_out,
        Value       *d_values_in,
        Value       *d_values_out,
        int         current_bit,
        int         num_bits)
    :
        temp_storage(temp_storage.Alias()),
        bin_offset(bin_offset),
        d_keys_in(reinterpret_cast<UnsignedBits*>(d_keys_in)),
        d_keys_out(reinterpret_cast<UnsignedBits*>(d_keys_out)),
        d_values_in(d_values_in),
        d_values_out(d_values_out),
        current_bit(current_bit),
        num_bits(num_bits),
        short_circuit(false)
    {}


    /**
     * Constructor
     */
    __device__ __forceinline__ BlockRangeRadixSortDownsweep(
        TempStorage &temp_storage,
        Offset      num_items,
        Offset      *d_spine,
        Key         *d_keys_in,
        Key         *d_keys_out,
        Value       *d_values_in,
        Value       *d_values_out,
        int         current_bit,
        int         num_bits)
    :
        temp_storage(temp_storage.Alias()),
        d_keys_in(reinterpret_cast<UnsignedBits*>(d_keys_in)),
        d_keys_out(reinterpret_cast<UnsignedBits*>(d_keys_out)),
        d_values_in(d_values_in),
        d_values_out(d_values_out),
        current_bit(current_bit),
        num_bits(num_bits)
    {
        // Load digit bin offsets (each of the first RADIX_DIGITS threads will load an offset for that digit)
        if (threadIdx.x < RADIX_DIGITS)
        {
            int bin_idx = (DESCENDING) ?
                RADIX_DIGITS - threadIdx.x - 1 :
                threadIdx.x;

            // Short circuit if the first block's histogram has only bin counts of only zeros or problem-size
            Offset first_block_bin_offset = d_spine[gridDim.x * bin_idx];
            int predicate = ((first_block_bin_offset == 0) || (first_block_bin_offset == num_items));
            this->temp_storage.short_circuit = WarpAll(predicate);

            // Load my block's bin offset for my bin
            bin_offset = d_spine[(gridDim.x * bin_idx) + blockIdx.x];
        }

        __syncthreads();

        short_circuit = this->temp_storage.short_circuit;
    }


    /**
     * Distribute keys from a segment of input tiles.
     */
    __device__ __forceinline__ void ProcessRegion(
        Offset          block_offset,
        const Offset    &block_end)
    {
        if (short_circuit)
        {
            // Copy keys
            Copy(d_keys_in, d_keys_out, block_offset, block_end);

            // Copy values
            Copy(d_values_in, d_values_out, block_offset, block_end);
        }
        else
        {
            // Process full tiles of tile_items
            while (block_offset + TILE_ITEMS <= block_end)
            {
                ProcessTile<true>(block_offset);
                block_offset += TILE_ITEMS;

                __syncthreads();
            }

            // Clean up last partial tile with guarded-I/O
            if (block_offset < block_end)
            {
                ProcessTile<false>(block_offset, block_end - block_offset);
            }
        }
    }
};



}               // CUB namespace
CUB_NS_POSTFIX  // Optional outer namespace(s)

