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
136 lines
3.5 KiB
Plaintext
136 lines
3.5 KiB
Plaintext
#include <thrust/execution_policy.h>
|
|
#include <thrust/functional.h>
|
|
#include <thrust/partition.h>
|
|
|
|
#include <unittest/unittest.h>
|
|
|
|
#ifdef THRUST_TEST_DEVICE_SIDE
|
|
template <typename ExecutionPolicy, typename Iterator, typename Predicate, typename Iterator2>
|
|
__global__ void
|
|
is_partitioned_kernel(ExecutionPolicy exec, Iterator first, Iterator last, Predicate pred, Iterator2 result)
|
|
{
|
|
*result = thrust::is_partitioned(exec, first, last, pred);
|
|
}
|
|
|
|
template <typename T>
|
|
struct is_even
|
|
{
|
|
_CCCL_HOST_DEVICE bool operator()(T x) const
|
|
{
|
|
return ((int) x % 2) == 0;
|
|
}
|
|
};
|
|
|
|
template <typename ExecutionPolicy>
|
|
void TestIsPartitionedDevice(ExecutionPolicy exec)
|
|
{
|
|
size_t n = 1000;
|
|
|
|
n = ::cuda::std::max<size_t>(n, 2);
|
|
|
|
thrust::device_vector<int> v = unittest::random_integers<int>(n);
|
|
|
|
thrust::device_vector<bool> result(1);
|
|
|
|
v[0] = 1;
|
|
v[1] = 0;
|
|
|
|
is_partitioned_kernel<<<1, 1>>>(exec, v.begin(), v.end(), is_even<int>(), result.begin());
|
|
{
|
|
cudaError_t const err = cudaDeviceSynchronize();
|
|
ASSERT_EQUAL(cudaSuccess, err);
|
|
}
|
|
|
|
ASSERT_EQUAL(false, result[0]);
|
|
|
|
thrust::partition(v.begin(), v.end(), is_even<int>());
|
|
|
|
is_partitioned_kernel<<<1, 1>>>(exec, v.begin(), v.end(), is_even<int>(), result.begin());
|
|
{
|
|
cudaError_t const err = cudaDeviceSynchronize();
|
|
ASSERT_EQUAL(cudaSuccess, err);
|
|
}
|
|
|
|
ASSERT_EQUAL(true, result[0]);
|
|
}
|
|
|
|
void TestIsPartitionedDeviceSeq()
|
|
{
|
|
TestIsPartitionedDevice(thrust::seq);
|
|
}
|
|
DECLARE_UNITTEST(TestIsPartitionedDeviceSeq);
|
|
|
|
void TestIsPartitionedDeviceDevice()
|
|
{
|
|
TestIsPartitionedDevice(thrust::device);
|
|
}
|
|
DECLARE_UNITTEST(TestIsPartitionedDeviceDevice);
|
|
#endif
|
|
|
|
void TestIsPartitionedCudaStreams()
|
|
{
|
|
thrust::device_vector<int> v(4);
|
|
v[0] = 1;
|
|
v[1] = 1;
|
|
v[2] = 1;
|
|
v[3] = 0;
|
|
|
|
cudaStream_t s;
|
|
cudaStreamCreate(&s);
|
|
|
|
// empty partition
|
|
ASSERT_EQUAL_QUIET(true,
|
|
thrust::is_partitioned(thrust::cuda::par.on(s), v.begin(), v.begin(), ::cuda::std::identity{}));
|
|
|
|
// one element true partition
|
|
ASSERT_EQUAL_QUIET(
|
|
true, thrust::is_partitioned(thrust::cuda::par.on(s), v.begin(), v.begin() + 1, ::cuda::std::identity{}));
|
|
|
|
// just true partition
|
|
ASSERT_EQUAL_QUIET(
|
|
true, thrust::is_partitioned(thrust::cuda::par.on(s), v.begin(), v.begin() + 2, ::cuda::std::identity{}));
|
|
|
|
// both true & false partitions
|
|
ASSERT_EQUAL_QUIET(true,
|
|
thrust::is_partitioned(thrust::cuda::par.on(s), v.begin(), v.end(), ::cuda::std::identity{}));
|
|
|
|
// one element false partition
|
|
ASSERT_EQUAL_QUIET(true,
|
|
thrust::is_partitioned(thrust::cuda::par.on(s), v.begin() + 3, v.end(), ::cuda::std::identity{}));
|
|
|
|
v[0] = 1;
|
|
v[1] = 0;
|
|
v[2] = 1;
|
|
v[3] = 1;
|
|
|
|
// not partitioned
|
|
ASSERT_EQUAL_QUIET(false,
|
|
thrust::is_partitioned(thrust::cuda::par.on(s), v.begin(), v.end(), ::cuda::std::identity{}));
|
|
|
|
cudaStreamDestroy(s);
|
|
}
|
|
DECLARE_UNITTEST(TestIsPartitionedCudaStreams);
|
|
|
|
template <typename T>
|
|
struct is_even_non_const
|
|
{
|
|
_CCCL_HOST_DEVICE bool operator()(T x) // no const
|
|
{
|
|
return ((int) x % 2) == 0;
|
|
}
|
|
};
|
|
|
|
void TestIsPartitionedWithNonConstPredicate()
|
|
{
|
|
thrust::device_vector<int> partitioned = {0, 2, 4, 1, 3, 5};
|
|
thrust::device_vector<int> unpartitioned = {0, 1, 2, 3};
|
|
|
|
ASSERT_EQUAL_QUIET(
|
|
true, thrust::is_partitioned(thrust::cuda::par, partitioned.begin(), partitioned.end(), is_even_non_const<int>{}));
|
|
|
|
ASSERT_EQUAL_QUIET(
|
|
false,
|
|
thrust::is_partitioned(thrust::cuda::par, unpartitioned.begin(), unpartitioned.end(), is_even_non_const<int>{}));
|
|
}
|
|
DECLARE_UNITTEST(TestIsPartitionedWithNonConstPredicate);
|