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
45 lines
1.3 KiB
Plaintext
45 lines
1.3 KiB
Plaintext
#include <thrust/device_free.h>
|
|
#include <thrust/device_malloc.h>
|
|
#include <thrust/device_ptr.h>
|
|
#include <thrust/reduce.h>
|
|
#include <thrust/sequence.h>
|
|
|
|
#include <cassert>
|
|
#include <iostream>
|
|
|
|
int main()
|
|
{
|
|
// allocate memory buffer to store 10 integers on the device
|
|
thrust::device_ptr<int> d_ptr = thrust::device_malloc<int>(10);
|
|
|
|
// device_ptr supports pointer arithmetic
|
|
thrust::device_ptr<int> first = d_ptr;
|
|
thrust::device_ptr<int> last = d_ptr + 10;
|
|
std::cout << "device array contains " << cuda::std::distance(first, last) << " values\n";
|
|
|
|
// algorithms work as expected
|
|
thrust::sequence(first, last);
|
|
std::cout << "sum of values is " << thrust::reduce(first, last) << "\n";
|
|
|
|
// device memory can be read and written transparently
|
|
d_ptr[0] = 10;
|
|
d_ptr[1] = 11;
|
|
d_ptr[2] = d_ptr[0] + d_ptr[1];
|
|
|
|
// device_ptr can be converted to a "raw" pointer for use in other APIs and kernels, etc.
|
|
int* raw_ptr = thrust::raw_pointer_cast(d_ptr);
|
|
|
|
// note: raw_ptr cannot necessarily be accessed by the host!
|
|
|
|
// conversely, raw pointers can be wrapped
|
|
[[maybe_unused]] thrust::device_ptr<int> wrapped_ptr = thrust::device_pointer_cast(raw_ptr);
|
|
|
|
// back to where we started
|
|
assert(wrapped_ptr == d_ptr);
|
|
|
|
// deallocate device memory
|
|
thrust::device_free(d_ptr);
|
|
|
|
return 0;
|
|
}
|