Files
project_6/qwen3_6_scripts/cccl_preload/include/thrust/scatter.h
project6-dev 4c365b8c03 feat(CCCL): device-level CUB algorithms for MoE dispatch
Add complete CCCL CUB header tree (1394 files) to cccl_preload/include/:
- cub/device/ — DeviceRadixSort, DeviceScan, DeviceHistogram, DeviceReduce, DeviceSelect
- cub/agent/ — all agent implementations (sort, scan, reduce, histogram, etc)
- cub/block/ — BlockScan, BlockReduce, BlockExchange, BlockLoad, BlockStore, etc
- cub/warp/ — WarpScan, WarpReduce, WarpExchange, WarpMergeSort
- cub/thread/ — thread-level operators
- thrust/ — sort_by_key, iterator utilities
- cuda/ — execution, stream, memory_resource, functional

New kernel: cccl_moe_sort_scatter.cu
- Uses CUB DeviceRadixSort::SortPairs to sort (expert_id, token_idx) pairs
- O(n) radix sort replaces O(n log n) torch.argsort in MoE prefill path
- Boundary detection + fill for expert offsets/sizes
- Compiled against CCCL upstream headers (not corex CUB) to avoid BI-V100 bugs

Previously only 288 CCCL headers (CachingDeviceAllocator only).
Now 1394 headers — full CUB device-level algorithm stack available for
all future kernels.
2026-08-13 11:18:52 +00:00

458 lines
21 KiB
C++

// SPDX-FileCopyrightText: Copyright (c) 2008-2013, NVIDIA Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
/*! \file scatter.h
* \brief Irregular copying to a destination range
*/
#pragma once
#include <thrust/detail/config.h>
#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)
# pragma GCC system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)
# pragma clang system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
# pragma system_header
#endif // no system header
#include <thrust/detail/execution_policy.h>
THRUST_NAMESPACE_BEGIN
/*! \addtogroup scattering
* \ingroup copying
* \{
*/
/*! \p scatter copies elements from a source range into an output array
* according to a map. For each iterator \c i in the range [\p first, \p last),
* the value \c *i is assigned to <tt>output[*(map + (i - first))]</tt>. The
* output iterator must permit random access. If the same index
* appears more than once in the range <tt>[map, map + (last - first))</tt>,
* the result is undefined.
*
* The algorithm's execution is parallelized as determined by \p exec.
*
* \param exec The execution policy to use for parallelization.
* \param first Beginning of the sequence of values to scatter.
* \param last End of the sequence of values to scatter.
* \param map Beginning of the sequence of output indices.
* \param result Destination of the source elements.
*
* \tparam DerivedPolicy The name of the derived execution policy.
* \tparam InputIterator1 must be a model of <a href="https://en.cppreference.com/w/cpp/iterator/input_iterator">Input
* Iterator</a> and \c InputIterator1's \c value_type must be convertible to \c RandomAccessIterator's \c value_type.
* \tparam InputIterator2 must be a model of <a href="https://en.cppreference.com/w/cpp/iterator/input_iterator">Input
* Iterator</a> and \c InputIterator2's \c value_type must be convertible to \c RandomAccessIterator's \c
* difference_type. \tparam RandomAccessIterator must be a model of <a
* href="https://en.cppreference.com/w/cpp/iterator/random_access_iterator">Random Access iterator</a>.
*
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range
* `[first,last)` for all iterators `i` in the range `[map,map + (last - first))`.
*
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range
* `[map, map + (last - first))` for all iterators `i` in the range `[map, map + (last - first))`.
*
* \pre The expression `result[*i]` shall be valid for all iterators in the range `[map, map + (last - first))`.
*
* The following code snippet demonstrates how to use \p scatter to reorder a range using
* the \p thrust::device execution policy for parallelization:
*
* \code
* #include <thrust/scatter.h>
* #include <thrust/device_vector.h>
* #include <thrust/execution_policy.h>
* ...
* // mark even indices with a 1; odd indices with a 0
* int values[10] = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
* thrust::device_vector<int> d_values(values, values + 10);
*
* // scatter all even indices into the first half of the
* // range, and odd indices vice versa
* int map[10] = {0, 5, 1, 6, 2, 7, 3, 8, 4, 9};
* thrust::device_vector<int> d_map(map, map + 10);
*
* thrust::device_vector<int> d_output(10);
* thrust::scatter(thrust::device,
* d_values.begin(), d_values.end(),
* d_map.begin(), d_output.begin());
* // d_output is now {1, 1, 1, 1, 1, 0, 0, 0, 0, 0}
* \endcode
*
* \note \p scatter is the inverse of thrust::gather.
*
* \verbatim embed:rst:leading-asterisk
* .. versionadded:: 2.2.0
* \endverbatim
*/
template <typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename RandomAccessIterator>
_CCCL_HOST_DEVICE void
scatter(const thrust::detail::execution_policy_base<DerivedPolicy>& exec,
InputIterator1 first,
InputIterator1 last,
InputIterator2 map,
RandomAccessIterator result);
/*! \p scatter copies elements from a source range into an output array
* according to a map. For each iterator \c i in the range [\p first, \p last),
* the value \c *i is assigned to <tt>output[*(map + (i - first))]</tt>. The
* output iterator must permit random access. If the same index
* appears more than once in the range <tt>[map, map + (last - first))</tt>,
* the result is undefined.
*
* \param first Beginning of the sequence of values to scatter.
* \param last End of the sequence of values to scatter.
* \param map Beginning of the sequence of output indices.
* \param result Destination of the source elements.
*
* \tparam InputIterator1 must be a model of <a href="https://en.cppreference.com/w/cpp/iterator/input_iterator">Input
* Iterator</a> and \c InputIterator1's \c value_type must be convertible to \c RandomAccessIterator's \c value_type.
* \tparam InputIterator2 must be a model of <a href="https://en.cppreference.com/w/cpp/iterator/input_iterator">Input
* Iterator</a> and \c InputIterator2's \c value_type must be convertible to \c RandomAccessIterator's \c
* difference_type. \tparam RandomAccessIterator must be a model of <a
* href="https://en.cppreference.com/w/cpp/iterator/random_access_iterator">Random Access iterator</a>.
*
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range
* `[first,last)` for all iterators `i` in the range `[map,map + (last - first))`.
*
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range
* `[map,map + (last - first))` for all iterators `i` in the range `[map,map + (last - first))`.
*
* \pre The expression `result[*i]` shall be valid for all iterators in the range `[map,map + (last - first))`.
*
* The following code snippet demonstrates how to use \p scatter to reorder a range.
*
* \code
* #include <thrust/scatter.h>
* #include <thrust/device_vector.h>
* ...
* // mark even indices with a 1; odd indices with a 0
* int values[10] = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
* thrust::device_vector<int> d_values(values, values + 10);
*
* // scatter all even indices into the first half of the
* // range, and odd indices vice versa
* int map[10] = {0, 5, 1, 6, 2, 7, 3, 8, 4, 9};
* thrust::device_vector<int> d_map(map, map + 10);
*
* thrust::device_vector<int> d_output(10);
* thrust::scatter(d_values.begin(), d_values.end(),
* d_map.begin(), d_output.begin());
* // d_output is now {1, 1, 1, 1, 1, 0, 0, 0, 0, 0}
* \endcode
*
* \note \p scatter is the inverse of thrust::gather.
*
* \verbatim embed:rst:leading-asterisk
* .. versionadded:: 2.2.0
* \endverbatim
*/
template <typename InputIterator1, typename InputIterator2, typename RandomAccessIterator>
void scatter(InputIterator1 first, InputIterator1 last, InputIterator2 map, RandomAccessIterator result);
/*! \p scatter_if conditionally copies elements from a source range into an
* output array according to a map. For each iterator \c i in the
* range <tt>[first, last)</tt> such that <tt>*(stencil + (i - first))</tt> is
* true, the value \c *i is assigned to <tt>output[*(map + (i - first))]</tt>.
* The output iterator must permit random access. If the same index
* appears more than once in the range <tt>[map, map + (last - first))</tt>
* the result is undefined.
*
* The algorithm's execution is parallelized as determined by \p exec.
*
* \param exec The execution policy to use for parallelization.
* \param first Beginning of the sequence of values to scatter.
* \param last End of the sequence of values to scatter.
* \param map Beginning of the sequence of output indices.
* \param stencil Beginning of the sequence of predicate values.
* \param output Beginning of the destination range.
*
* \tparam DerivedPolicy The name of the derived execution policy.
* \tparam InputIterator1 must be a model of <a href="https://en.cppreference.com/w/cpp/iterator/input_iterator">Input
* Iterator</a> and \c InputIterator1's \c value_type must be convertible to \c RandomAccessIterator's \c value_type.
* \tparam InputIterator2 must be a model of <a href="https://en.cppreference.com/w/cpp/iterator/input_iterator">Input
* Iterator</a> and \c InputIterator2's \c value_type must be convertible to \c RandomAccessIterator's \c
* difference_type. \tparam InputIterator3 must be a model of <a
* href="https://en.cppreference.com/w/cpp/iterator/input_iterator">Input Iterator</a> and \c InputIterator3's \c
* value_type must be convertible to \c bool. \tparam RandomAccessIterator must be a model of <a
* href="https://en.cppreference.com/w/cpp/iterator/random_access_iterator">Random Access iterator</a>.
*
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range
* `[first,last)` for all iterators `i` in the range `[map,map + (last - first))`.
*
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range
* `[map,map + (last - first))` for all iterators `i` in the range `[map,map + (last - first))`.
*
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range
* `[stencil,stencil + (last - first))` for all iterators `i` in the range `[map,map + (last - first))`.
*
* \pre The expression `result[*i]` shall be valid for all iterators `i` in the range `[map,map + (last - first))` for
* which the following condition holds: `*(stencil + i) != false`.
*
* \code
* #include <thrust/scatter.h>
* #include <thrust/execution_policy.h>
* ...
* int V[8] = {10, 20, 30, 40, 50, 60, 70, 80};
* int M[8] = {0, 5, 1, 6, 2, 7, 3, 4};
* int S[8] = {1, 0, 1, 0, 1, 0, 1, 0};
* int D[8] = {0, 0, 0, 0, 0, 0, 0, 0};
*
* thrust::scatter_if(thrust::host, V, V + 8, M, S, D);
*
* // D contains [10, 30, 50, 70, 0, 0, 0, 0];
* \endcode
*
* \note \p scatter_if is the inverse of thrust::gather_if.
*
* \verbatim embed:rst:leading-asterisk
* .. versionadded:: 2.2.0
* \endverbatim
*/
template <typename DerivedPolicy,
typename InputIterator1,
typename InputIterator2,
typename InputIterator3,
typename RandomAccessIterator>
_CCCL_HOST_DEVICE void scatter_if(
const thrust::detail::execution_policy_base<DerivedPolicy>& exec,
InputIterator1 first,
InputIterator1 last,
InputIterator2 map,
InputIterator3 stencil,
RandomAccessIterator output);
/*! \p scatter_if conditionally copies elements from a source range into an
* output array according to a map. For each iterator \c i in the
* range <tt>[first, last)</tt> such that <tt>*(stencil + (i - first))</tt> is
* true, the value \c *i is assigned to <tt>output[*(map + (i - first))]</tt>.
* The output iterator must permit random access. If the same index
* appears more than once in the range <tt>[map, map + (last - first))</tt>
* the result is undefined.
*
* \param first Beginning of the sequence of values to scatter.
* \param last End of the sequence of values to scatter.
* \param map Beginning of the sequence of output indices.
* \param stencil Beginning of the sequence of predicate values.
* \param output Beginning of the destination range.
*
* \tparam InputIterator1 must be a model of <a href="https://en.cppreference.com/w/cpp/iterator/input_iterator">Input
* Iterator</a> and \c InputIterator1's \c value_type must be convertible to \c RandomAccessIterator's \c value_type.
* \tparam InputIterator2 must be a model of <a href="https://en.cppreference.com/w/cpp/iterator/input_iterator">Input
* Iterator</a> and \c InputIterator2's \c value_type must be convertible to \c RandomAccessIterator's \c
* difference_type. \tparam InputIterator3 must be a model of <a
* href="https://en.cppreference.com/w/cpp/iterator/input_iterator">Input Iterator</a> and \c InputIterator3's \c
* value_type must be convertible to \c bool. \tparam RandomAccessIterator must be a model of <a
* href="https://en.cppreference.com/w/cpp/iterator/random_access_iterator">Random Access iterator</a>.
*
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range
* `[first,last)` for all iterators `i` in the range `[map,map + (last - first))`.
*
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range
* `[map,map + (last - first))` for all iterators `i` in the range `[map,map + (last - first))`.
*
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range
* `[stencil,stencil + (last - first))` for all iterators `i` in the range `[map,map + (last - first))`.
*
* \pre The expression `result[*i]` shall be valid for all iterators `i` in the range `[map,map + (last - first))` for
* which the following condition holds: `*(stencil + i) != false`.
*
* \code
* #include <thrust/scatter.h>
* ...
* int V[8] = {10, 20, 30, 40, 50, 60, 70, 80};
* int M[8] = {0, 5, 1, 6, 2, 7, 3, 4};
* int S[8] = {1, 0, 1, 0, 1, 0, 1, 0};
* int D[8] = {0, 0, 0, 0, 0, 0, 0, 0};
*
* thrust::scatter_if(V, V + 8, M, S, D);
*
* // D contains [10, 30, 50, 70, 0, 0, 0, 0];
* \endcode
*
* \note \p scatter_if is the inverse of thrust::gather_if.
*
* \verbatim embed:rst:leading-asterisk
* .. versionadded:: 2.2.0
* \endverbatim
*/
template <typename InputIterator1, typename InputIterator2, typename InputIterator3, typename RandomAccessIterator>
void scatter_if(
InputIterator1 first, InputIterator1 last, InputIterator2 map, InputIterator3 stencil, RandomAccessIterator output);
/*! \p scatter_if conditionally copies elements from a source range into an
* output array according to a map. For each iterator \c i in the
* range <tt>[first, last)</tt> such that <tt>pred(*(stencil + (i - first)))</tt> is
* \c true, the value \c *i is assigned to <tt>output[*(map + (i - first))]</tt>.
* The output iterator must permit random access. If the same index
* appears more than once in the range <tt>[map, map + (last - first))</tt>
* the result is undefined.
*
* The algorithm's execution is parallelized as determined by \p exec.
*
* \param exec The execution policy to use for parallelization.
* \param first Beginning of the sequence of values to scatter.
* \param last End of the sequence of values to scatter.
* \param map Beginning of the sequence of output indices.
* \param stencil Beginning of the sequence of predicate values.
* \param output Beginning of the destination range.
* \param pred Predicate to apply to the stencil values.
*
* \tparam DerivedPolicy The name of the derived execution policy.
* \tparam InputIterator1 must be a model of <a href="https://en.cppreference.com/w/cpp/iterator/input_iterator">Input
* Iterator</a> and \c InputIterator1's \c value_type must be convertible to \c RandomAccessIterator's \c value_type.
* \tparam InputIterator2 must be a model of <a href="https://en.cppreference.com/w/cpp/iterator/input_iterator">Input
* Iterator</a> and \c InputIterator2's \c value_type must be convertible to \c RandomAccessIterator's \c
* difference_type. \tparam InputIterator3 must be a model of <a
* href="https://en.cppreference.com/w/cpp/iterator/input_iterator">Input Iterator</a> and \c InputIterator3's \c
* value_type must be convertible to \c Predicate's argument type. \tparam RandomAccessIterator must be a model of <a
* href="https://en.cppreference.com/w/cpp/iterator/random_access_iterator">Random Access iterator</a>. \tparam
* Predicate must be a model of <a href="https://en.cppreference.com/w/cpp/concepts/predicate">Predicate</a>.
*
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range
* `[first,last)` for all iterators `i` in the range `[map,map + (last - first))`.
*
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range
* `[map,map + (last - first))` for all iterators `i` in the range `[map,map + (last - first))`.
*
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range
* `[stencil,stencil + (last - first))` for all iterators `i` in the range `[map,map + (last - first))`.
*
* \pre The expression `result[*i]` shall be valid for all iterators `i` in the range `[map,map + (last - first))` for
* which the following condition holds: `pred(*(stencil + i)) != false`.
*
* \code
* #include <thrust/scatter.h>
* #include <thrust/execution_policy.h>
*
* struct is_even
* {
* __host__ __device__
* bool operator()(int x)
* {
* return (x % 2) == 0;
* }
* };
*
* ...
*
* int V[8] = {10, 20, 30, 40, 50, 60, 70, 80};
* int M[8] = {0, 5, 1, 6, 2, 7, 3, 4};
* int S[8] = {2, 1, 2, 1, 2, 1, 2, 1};
* int D[8] = {0, 0, 0, 0, 0, 0, 0, 0};
*
* is_even pred;
* thrust::scatter_if(thrust::host, V, V + 8, M, S, D, pred);
*
* // D contains [10, 30, 50, 70, 0, 0, 0, 0];
* \endcode
*
* \note \p scatter_if is the inverse of thrust::gather_if.
*
* \verbatim embed:rst:leading-asterisk
* .. versionadded:: 2.2.0
* \endverbatim
*/
template <typename DerivedPolicy,
typename InputIterator1,
typename InputIterator2,
typename InputIterator3,
typename RandomAccessIterator,
typename Predicate>
_CCCL_HOST_DEVICE void scatter_if(
const thrust::detail::execution_policy_base<DerivedPolicy>& exec,
InputIterator1 first,
InputIterator1 last,
InputIterator2 map,
InputIterator3 stencil,
RandomAccessIterator output,
Predicate pred);
/*! \p scatter_if conditionally copies elements from a source range into an
* output array according to a map. For each iterator \c i in the
* range <tt>[first, last)</tt> such that <tt>pred(*(stencil + (i - first)))</tt> is
* \c true, the value \c *i is assigned to <tt>output[*(map + (i - first))]</tt>.
* The output iterator must permit random access. If the same index
* appears more than once in the range <tt>[map, map + (last - first))</tt>
* the result is undefined.
*
* \param first Beginning of the sequence of values to scatter.
* \param last End of the sequence of values to scatter.
* \param map Beginning of the sequence of output indices.
* \param stencil Beginning of the sequence of predicate values.
* \param output Beginning of the destination range.
* \param pred Predicate to apply to the stencil values.
*
* \tparam InputIterator1 must be a model of <a href="https://en.cppreference.com/w/cpp/iterator/input_iterator">Input
* Iterator</a> and \c InputIterator1's \c value_type must be convertible to \c RandomAccessIterator's \c value_type.
* \tparam InputIterator2 must be a model of <a href="https://en.cppreference.com/w/cpp/iterator/input_iterator">Input
* Iterator</a> and \c InputIterator2's \c value_type must be convertible to \c RandomAccessIterator's \c
* difference_type. \tparam InputIterator3 must be a model of <a
* href="https://en.cppreference.com/w/cpp/iterator/input_iterator">Input Iterator</a> and \c InputIterator3's \c
* value_type must be convertible to \c Predicate's argument type. \tparam RandomAccessIterator must be a model of <a
* href="https://en.cppreference.com/w/cpp/iterator/random_access_iterator">Random Access iterator</a>. \tparam
* Predicate must be a model of <a href="https://en.cppreference.com/w/cpp/concepts/predicate">Predicate</a>.
*
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range
* `[first,last)` for all iterators `i` in the range `[map,map + (last - first))`.
*
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range
* `[map,map + (last - first))` for all iterators `i` in the range `[map,map + (last - first))`.
*
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range
* `[stencil,stencil + (last - first))` for all iterators `i` in the range `[map,map + (last - first))`.
*
* \pre The expression `result[*i]` shall be valid for all iterators `i` in the range `[map,map + (last - first))` for
* which the following condition holds: `pred(*(stencil + i)) != false`.
*
* \code
* #include <thrust/scatter.h>
*
* struct is_even
* {
* __host__ __device__
* bool operator()(int x)
* {
* return (x % 2) == 0;
* }
* };
*
* ...
*
* int V[8] = {10, 20, 30, 40, 50, 60, 70, 80};
* int M[8] = {0, 5, 1, 6, 2, 7, 3, 4};
* int S[8] = {2, 1, 2, 1, 2, 1, 2, 1};
* int D[8] = {0, 0, 0, 0, 0, 0, 0, 0};
*
* is_even pred;
* thrust::scatter_if(V, V + 8, M, S, D, pred);
*
* // D contains [10, 30, 50, 70, 0, 0, 0, 0];
* \endcode
*
* \note \p scatter_if is the inverse of thrust::gather_if.
*
* \verbatim embed:rst:leading-asterisk
* .. versionadded:: 2.2.0
* \endverbatim
*/
template <typename InputIterator1,
typename InputIterator2,
typename InputIterator3,
typename RandomAccessIterator,
typename Predicate>
void scatter_if(InputIterator1 first,
InputIterator1 last,
InputIterator2 map,
InputIterator3 stencil,
RandomAccessIterator output,
Predicate pred);
/*! \} // end scattering
*/
THRUST_NAMESPACE_END
#include <thrust/detail/scatter.inl>