[INFRA] Import NVIDIA/CCCL upstream as optimization reference library
CCCL (CUDA C++ Core Libraries) provides: - CUB: device/block/warp-level GPU primitives (reduce, scan, sort, topk) - Thrust: high-level parallel algorithms (transform_reduce, sort, scan) - libcudacxx: CUDA C++ standard library (atomics, barriers, memory) - cudax: experimental features (memory resources, allocators) - Tuning policies: per-SM hardware-specific algorithm parameters Competition optimization vectors mapped to CCCL: - Output TPS (83% weight): warp_reduce, block_reduce, device_topk - Input TPS (14% weight): device_scan, block_load, prefetch - Cache TPS (3% weight): prefix caching strategy patterns - Memory (0.9 util): pooled/cached/buddy allocators Source: https://github.com/NVIDIA/cccl (shallow clone, HEAD only) License: Apache-2.0
This commit is contained in:
80
cccl_upstream/thrust/examples/expand.cu
Normal file
80
cccl_upstream/thrust/examples/expand.cu
Normal file
@@ -0,0 +1,80 @@
|
||||
#include <thrust/copy.h>
|
||||
#include <thrust/device_vector.h>
|
||||
#include <thrust/fill.h>
|
||||
#include <thrust/gather.h>
|
||||
#include <thrust/reduce.h>
|
||||
#include <thrust/scan.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
|
||||
// This example demonstrates how to expand an input sequence by
|
||||
// replicating each element a variable number of times. For example,
|
||||
//
|
||||
// expand([2,2,2],[A,B,C]) -> [A,A,B,B,C,C]
|
||||
// expand([3,0,1],[A,B,C]) -> [A,A,A,C]
|
||||
// expand([1,3,2],[A,B,C]) -> [A,B,B,B,C,C]
|
||||
//
|
||||
// The element counts are assumed to be non-negative integers
|
||||
|
||||
template <typename InputIterator1, typename InputIterator2, typename OutputIterator>
|
||||
OutputIterator expand(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator output)
|
||||
{
|
||||
using difference_type = typename cuda::std::iterator_traits<InputIterator1>::difference_type;
|
||||
|
||||
difference_type input_size = cuda::std::distance(first1, last1);
|
||||
difference_type output_size = thrust::reduce(first1, last1);
|
||||
|
||||
// scan the counts to obtain output offsets for each input element
|
||||
thrust::device_vector<difference_type> output_offsets(input_size, 0);
|
||||
thrust::exclusive_scan(first1, last1, output_offsets.begin());
|
||||
|
||||
// scatter the nonzero counts into their corresponding output positions
|
||||
thrust::device_vector<difference_type> output_indices(output_size, 0);
|
||||
thrust::scatter_if(
|
||||
thrust::counting_iterator<difference_type>(0),
|
||||
thrust::counting_iterator<difference_type>(input_size),
|
||||
output_offsets.begin(),
|
||||
first1,
|
||||
output_indices.begin());
|
||||
|
||||
// compute max-scan over the output indices, filling in the holes
|
||||
thrust::inclusive_scan(
|
||||
output_indices.begin(), output_indices.end(), output_indices.begin(), cuda::maximum<difference_type>{});
|
||||
|
||||
// gather input values according to index array (output = first2[output_indices])
|
||||
thrust::gather(output_indices.begin(), output_indices.end(), first2, output);
|
||||
|
||||
// return output + output_size
|
||||
cuda::std::advance(output, output_size);
|
||||
return output;
|
||||
}
|
||||
|
||||
template <typename Vector>
|
||||
void print(const std::string& s, const Vector& v)
|
||||
{
|
||||
using T = typename Vector::value_type;
|
||||
|
||||
std::cout << s;
|
||||
thrust::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, " "));
|
||||
std::cout << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
thrust::device_vector<int> d_counts = {3, 5, 2, 0, 1, 3, 4, 2, 4};
|
||||
thrust::device_vector<int> d_values = {1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
|
||||
const size_t output_size = thrust::reduce(d_counts.begin(), d_counts.end());
|
||||
thrust::device_vector<int> d_output(output_size);
|
||||
|
||||
// expand values according to counts
|
||||
expand(d_counts.begin(), d_counts.end(), d_values.begin(), d_output.begin());
|
||||
|
||||
std::cout << "Expanding values according to counts" << '\n';
|
||||
print(" counts ", d_counts);
|
||||
print(" values ", d_values);
|
||||
print(" output ", d_output);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user