[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:
91
cccl_upstream/thrust/examples/minmax.cu
Normal file
91
cccl_upstream/thrust/examples/minmax.cu
Normal file
@@ -0,0 +1,91 @@
|
||||
#include <thrust/device_vector.h>
|
||||
#include <thrust/extrema.h>
|
||||
#include <thrust/functional.h>
|
||||
#include <thrust/host_vector.h>
|
||||
#include <thrust/random.h>
|
||||
#include <thrust/transform_reduce.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
// compute minimum and maximum values in a single reduction
|
||||
|
||||
// minmax_pair stores the minimum and maximum
|
||||
// values that have been encountered so far
|
||||
template <typename T>
|
||||
struct minmax_pair
|
||||
{
|
||||
T min_val;
|
||||
T max_val;
|
||||
};
|
||||
|
||||
// minmax_unary_op is a functor that takes in a value x and
|
||||
// returns a minmax_pair whose minimum and maximum values
|
||||
// are initialized to x.
|
||||
template <typename T>
|
||||
struct minmax_unary_op
|
||||
{
|
||||
__host__ __device__ minmax_pair<T> operator()(const T& x) const
|
||||
{
|
||||
minmax_pair<T> result;
|
||||
result.min_val = x;
|
||||
result.max_val = x;
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
// minmax_binary_op is a functor that accepts two minmax_pair
|
||||
// structs and returns a new minmax_pair whose minimum and
|
||||
// maximum values are the min() and max() respectively of
|
||||
// the minimums and maximums of the input pairs
|
||||
template <typename T>
|
||||
struct minmax_binary_op
|
||||
{
|
||||
__host__ __device__ minmax_pair<T> operator()(const minmax_pair<T>& x, const minmax_pair<T>& y) const
|
||||
{
|
||||
minmax_pair<T> result;
|
||||
result.min_val = thrust::min(x.min_val, y.min_val);
|
||||
result.max_val = thrust::max(x.max_val, y.max_val);
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
// input size
|
||||
size_t N = 10;
|
||||
|
||||
// initialize random number generator
|
||||
thrust::default_random_engine rng;
|
||||
thrust::uniform_int_distribution<int> dist(10, 99);
|
||||
|
||||
// initialize data on host
|
||||
thrust::host_vector<int> host_data(N);
|
||||
for (auto& e : host_data)
|
||||
{
|
||||
e = dist(rng);
|
||||
}
|
||||
thrust::device_vector<int> data = host_data;
|
||||
|
||||
// setup arguments
|
||||
minmax_unary_op<int> unary_op;
|
||||
minmax_binary_op<int> binary_op;
|
||||
|
||||
// initialize reduction with the first value
|
||||
minmax_pair<int> init = unary_op(data[0]);
|
||||
|
||||
// compute minimum and maximum values
|
||||
minmax_pair<int> result = thrust::transform_reduce(data.begin(), data.end(), unary_op, init, binary_op);
|
||||
|
||||
// print results
|
||||
std::cout << "[ ";
|
||||
for (auto& e : host_data)
|
||||
{
|
||||
std::cout << e << " ";
|
||||
}
|
||||
std::cout << "]" << '\n';
|
||||
|
||||
std::cout << "minimum = " << result.min_val << '\n';
|
||||
std::cout << "maximum = " << result.max_val << '\n';
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user