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
79 lines
1.9 KiB
Plaintext
79 lines
1.9 KiB
Plaintext
#include <thrust/device_vector.h>
|
|
#include <thrust/functional.h>
|
|
#include <thrust/host_vector.h>
|
|
#include <thrust/transform.h>
|
|
|
|
#include <algorithm>
|
|
#include <iostream>
|
|
#include <iterator>
|
|
|
|
// This example illustrates how to implement the SAXPY
|
|
// operation (Y[i] = a * X[i] + Y[i]) using Thrust.
|
|
// The saxpy_slow function demonstrates the most
|
|
// straightforward implementation using a temporary
|
|
// array and two separate transformations, one with
|
|
// multiplies and one with plus. The saxpy_fast function
|
|
// implements the operation with a single transformation
|
|
// and represents "best practice".
|
|
|
|
struct saxpy_functor
|
|
{
|
|
const float a;
|
|
|
|
saxpy_functor(float _a)
|
|
: a(_a)
|
|
{}
|
|
|
|
__host__ __device__ float operator()(const float& x, const float& y) const
|
|
{
|
|
return a * x + y;
|
|
}
|
|
};
|
|
|
|
void saxpy_fast(float A, thrust::device_vector<float>& X, thrust::device_vector<float>& Y)
|
|
{
|
|
// Y <- A * X + Y
|
|
thrust::transform(X.begin(), X.end(), Y.begin(), Y.begin(), saxpy_functor(A));
|
|
}
|
|
|
|
void saxpy_slow(float A, thrust::device_vector<float>& X, thrust::device_vector<float>& Y)
|
|
{
|
|
thrust::device_vector<float> temp(X.size());
|
|
|
|
// temp <- A
|
|
thrust::fill(temp.begin(), temp.end(), A);
|
|
|
|
// temp <- A * X
|
|
thrust::transform(X.begin(), X.end(), temp.begin(), temp.begin(), cuda::std::multiplies<float>());
|
|
|
|
// Y <- A * X + Y
|
|
thrust::transform(temp.begin(), temp.end(), Y.begin(), Y.begin(), cuda::std::plus<float>());
|
|
}
|
|
|
|
int main()
|
|
{
|
|
// initialize host arrays
|
|
thrust::host_vector<float> x{1.0, 1.0, 1.0, 1.0};
|
|
thrust::host_vector<float> y{1.0, 2.0, 3.0, 4.0};
|
|
|
|
{
|
|
// transfer to device
|
|
thrust::device_vector<float> X(x);
|
|
thrust::device_vector<float> Y(y);
|
|
|
|
// slow method
|
|
saxpy_slow(2.0, X, Y);
|
|
}
|
|
|
|
{
|
|
// transfer to device
|
|
thrust::device_vector<float> X(x);
|
|
thrust::device_vector<float> Y(y);
|
|
|
|
// fast method
|
|
saxpy_fast(2.0, X, Y);
|
|
}
|
|
|
|
return 0;
|
|
}
|