Files
project_6/cccl_upstream/thrust/testing/cuda/reduce.cu
EngineX CI 56fd68e7dd [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
2026-07-30 09:35:51 +00:00

119 lines
2.9 KiB
Plaintext

#include <thrust/execution_policy.h>
#include <thrust/reduce.h>
#include <cuda/iterator>
#include <unittest/unittest.h>
template <typename ExecutionPolicy, typename Iterator, typename T, typename Iterator2>
__global__ void reduce_kernel(ExecutionPolicy exec, Iterator first, Iterator last, T init, Iterator2 result)
{
*result = thrust::reduce(exec, first, last, init);
}
#ifdef THRUST_TEST_DEVICE_SIDE
template <typename T, typename ExecutionPolicy>
void TestReduceDevice(ExecutionPolicy exec, const size_t n)
{
thrust::host_vector<T> h_data = unittest::random_integers<T>(n);
thrust::device_vector<T> d_data = h_data;
thrust::device_vector<T> d_result(1);
T init = 13;
T h_result = thrust::reduce(h_data.begin(), h_data.end(), init);
reduce_kernel<<<1, 1>>>(exec, d_data.begin(), d_data.end(), init, d_result.begin());
cudaError_t const err = cudaDeviceSynchronize();
ASSERT_EQUAL(cudaSuccess, err);
ASSERT_EQUAL(h_result, d_result[0]);
}
template <typename T>
struct TestReduceDeviceSeq
{
void operator()(const size_t n)
{
TestReduceDevice<T>(thrust::seq, n);
}
};
VariableUnitTest<TestReduceDeviceSeq, IntegralTypes> TestReduceDeviceSeqInstance;
template <typename T>
struct TestReduceDeviceDevice
{
void operator()(const size_t n)
{
TestReduceDevice<T>(thrust::device, n);
}
};
VariableUnitTest<TestReduceDeviceDevice, IntegralTypes> TestReduceDeviceDeviceInstance;
template <typename T>
struct TestReduceDeviceNoSync
{
void operator()(const size_t n)
{
TestReduceDevice<T>(thrust::cuda::par_nosync, n);
}
};
VariableUnitTest<TestReduceDeviceNoSync, IntegralTypes> TestReduceDeviceNoSyncInstance;
#endif
template <typename ExecutionPolicy>
void TestReduceCudaStreams(ExecutionPolicy policy)
{
using Vector = thrust::device_vector<int>;
Vector v(3);
v[0] = 1;
v[1] = -2;
v[2] = 3;
cudaStream_t s;
cudaStreamCreate(&s);
auto streampolicy = policy.on(s);
// no initializer
ASSERT_EQUAL(thrust::reduce(streampolicy, v.begin(), v.end()), 2);
// with initializer
ASSERT_EQUAL(thrust::reduce(streampolicy, v.begin(), v.end(), 10), 12);
cudaStreamDestroy(s);
}
void TestReduceCudaStreamsSync()
{
TestReduceCudaStreams(thrust::cuda::par);
}
DECLARE_UNITTEST(TestReduceCudaStreamsSync);
void TestReduceCudaStreamsNoSync()
{
TestReduceCudaStreams(thrust::cuda::par_nosync);
}
DECLARE_UNITTEST(TestReduceCudaStreamsNoSync);
#if defined(THRUST_RDC_ENABLED)
void TestReduceLargeInput()
{
using T = unsigned long long;
using OffsetT = std::size_t;
const OffsetT num_items = 1ull << 32;
cuda::constant_iterator<T> d_data(T{1});
thrust::device_vector<T> d_result(1);
reduce_kernel<<<1, 1>>>(thrust::device, d_data, d_data + num_items, T{}, d_result.begin());
cudaError_t const err = cudaDeviceSynchronize();
ASSERT_EQUAL(cudaSuccess, err);
ASSERT_EQUAL(num_items, d_result[0]);
}
DECLARE_UNITTEST(TestReduceLargeInput);
#endif