Files
project_6/cccl_upstream/cub/test/catch2_test_block_load.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

225 lines
9.1 KiB
Plaintext

// SPDX-FileCopyrightText: Copyright (c) 2011-2022, NVIDIA CORPORATION. All rights reserved.
// SPDX-License-Identifier: BSD-3
#include <cub/block/block_load.cuh>
#include <cub/iterator/cache_modified_input_iterator.cuh>
#include <cub/util_allocator.cuh>
#include <cub/util_arch.cuh>
#include <c2h/catch2_test_helper.h>
template <int ItemsPerThread, int ThreadsInBlock, cub::BlockLoadAlgorithm LoadAlgorithm>
static __device__ int get_output_idx(int item)
{
if (LoadAlgorithm == cub::BlockLoadAlgorithm::BLOCK_LOAD_STRIPED)
{
return static_cast<int>(threadIdx.x) + ThreadsInBlock * item;
}
return static_cast<int>(threadIdx.x) * ItemsPerThread + item;
}
template <int ItemsPerThread,
int ThreadsInBlock,
cub::BlockLoadAlgorithm LoadAlgorithm,
typename InputIteratorT,
typename OutputIteratorT>
__global__ void kernel(cuda::std::true_type, InputIteratorT input, OutputIteratorT output, int num_items)
{
using input_t = cub::detail::it_value_t<InputIteratorT>;
using block_load_t = cub::BlockLoad<input_t, ThreadsInBlock, ItemsPerThread, LoadAlgorithm>;
using storage_t = typename block_load_t::TempStorage;
__shared__ storage_t storage;
block_load_t block_load(storage);
input_t data[ItemsPerThread];
if (ItemsPerThread * ThreadsInBlock == num_items)
{
block_load.Load(input, data);
}
else
{
block_load.Load(input, data, num_items);
}
for (int i = 0; i < ItemsPerThread; i++)
{
const int idx = get_output_idx<ItemsPerThread, ThreadsInBlock, LoadAlgorithm>(i);
if (idx < num_items)
{
output[idx] = data[i];
}
}
}
template <int ItemsPerThread,
int ThreadsInBlock,
cub::BlockLoadAlgorithm /* LoadAlgorithm */,
typename InputIteratorT,
typename OutputIteratorT>
__global__ void kernel(cuda::std::false_type, InputIteratorT input, OutputIteratorT output, int num_items)
{
for (int i = 0; i < ItemsPerThread; i++)
{
const int idx = get_output_idx<ItemsPerThread, ThreadsInBlock, cub::BlockLoadAlgorithm::BLOCK_LOAD_DIRECT>(i);
if (idx < num_items)
{
output[idx] = input[idx];
}
}
}
template <int ItemsPerThread, int ThreadsInBlock, cub::BlockLoadAlgorithm LoadAlgorithm, typename T, typename InputIteratorT>
void test_block_load(const c2h::device_vector<T>& d_input, InputIteratorT input)
{
using block_load_t = cub::BlockLoad<T, ThreadsInBlock, ItemsPerThread, LoadAlgorithm>;
using storage_t = typename block_load_t::TempStorage;
constexpr auto sufficient_resources =
cuda::std::bool_constant<sizeof(storage_t) <= cub::detail::max_smem_per_block>{};
c2h::device_vector<T> d_output(d_input.size());
kernel<ItemsPerThread, ThreadsInBlock, LoadAlgorithm><<<1, ThreadsInBlock>>>(
sufficient_resources, input, thrust::raw_pointer_cast(d_output.data()), static_cast<int>(d_input.size()));
REQUIRE(cudaSuccess == cudaPeekAtLastError());
REQUIRE(cudaSuccess == cudaDeviceSynchronize());
REQUIRE(d_input == d_output);
}
// %PARAM% IPT it 1:11
using types = c2h::type_list<std::uint8_t, std::int32_t, std::int64_t>;
using vec_types = c2h::type_list<long2, double2>;
using even_threads_in_block = c2h::enum_type_list<int, 32, 128>;
using odd_threads_in_block = c2h::enum_type_list<int, 15, 65>;
using a_block_size = c2h::enum_type_list<int, 256>;
using items_per_thread = c2h::enum_type_list<int, IPT>;
using load_algorithm =
c2h::enum_type_list<cub::BlockLoadAlgorithm,
cub::BlockLoadAlgorithm::BLOCK_LOAD_DIRECT,
cub::BlockLoadAlgorithm::BLOCK_LOAD_STRIPED,
cub::BlockLoadAlgorithm::BLOCK_LOAD_VECTORIZE,
cub::BlockLoadAlgorithm::BLOCK_LOAD_TRANSPOSE,
cub::BlockLoadAlgorithm::BLOCK_LOAD_WARP_TRANSPOSE,
cub::BlockLoadAlgorithm::BLOCK_LOAD_WARP_TRANSPOSE_TIMESLICED>;
using odd_load_algorithm =
c2h::enum_type_list<cub::BlockLoadAlgorithm,
cub::BlockLoadAlgorithm::BLOCK_LOAD_DIRECT,
cub::BlockLoadAlgorithm::BLOCK_LOAD_STRIPED,
cub::BlockLoadAlgorithm::BLOCK_LOAD_VECTORIZE,
cub::BlockLoadAlgorithm::BLOCK_LOAD_TRANSPOSE>;
template <class TestType>
struct params_t
{
using type = typename c2h::get<0, TestType>;
static constexpr int items_per_thread = c2h::get<1, TestType>::value;
static constexpr int threads_in_block = c2h::get<2, TestType>::value;
static constexpr int tile_size = items_per_thread * threads_in_block;
static constexpr cub::BlockLoadAlgorithm load_algorithm = c2h::get<3, TestType>::value;
};
C2H_TEST("Block load works with even block sizes",
"[load][block]",
types,
items_per_thread,
even_threads_in_block,
load_algorithm)
{
using params = params_t<TestType>;
using type = typename params::type;
c2h::device_vector<type> d_input(GENERATE_COPY(take(10, random(0, params::tile_size))));
c2h::gen(C2H_SEED(10), d_input);
test_block_load<params::items_per_thread, params::threads_in_block, params::load_algorithm>(
d_input, thrust::raw_pointer_cast(d_input.data()));
}
C2H_TEST("Block load works with even odd sizes",
"[load][block]",
types,
items_per_thread,
odd_threads_in_block,
odd_load_algorithm)
{
using params = params_t<TestType>;
using type = typename params::type;
c2h::device_vector<type> d_input(GENERATE_COPY(take(10, random(0, params::tile_size))));
c2h::gen(C2H_SEED(10), d_input);
test_block_load<params::items_per_thread, params::threads_in_block, params::load_algorithm>(
d_input, thrust::raw_pointer_cast(d_input.data()));
}
// WAR bug in vec type handling in NVCC 12.0 + GCC 11.4 + C++20
#if !(_CCCL_CUDA_COMPILER(NVCC, ==, 12, 0) && _CCCL_COMPILER(GCC, ==, 11, 4) && _CCCL_STD_VER == 2020)
C2H_TEST(
"Block load works with even vector types", "[load][block]", vec_types, items_per_thread, a_block_size, load_algorithm)
{
using params = params_t<TestType>;
using type = typename params::type;
c2h::device_vector<type> d_input(GENERATE_COPY(take(10, random(0, params::tile_size))));
c2h::gen(C2H_SEED(10), d_input);
test_block_load<params::items_per_thread, params::threads_in_block, params::load_algorithm>(
d_input, thrust::raw_pointer_cast(d_input.data()));
}
#endif // !(NVCC 12.0 and GCC 11.4 and C++20)
C2H_TEST("Block load works with custom types", "[load][block]", items_per_thread, load_algorithm)
{
using type = c2h::custom_type_t<c2h::equal_comparable_t>;
constexpr int items_per_thread = c2h::get<0, TestType>::value;
constexpr int threads_in_block = 64;
constexpr int tile_size = items_per_thread * threads_in_block;
static constexpr cub::BlockLoadAlgorithm load_algorithm = c2h::get<1, TestType>::value;
c2h::device_vector<type> d_input(GENERATE_COPY(take(10, random(0, tile_size))));
c2h::gen(C2H_SEED(10), d_input);
test_block_load<items_per_thread, threads_in_block, load_algorithm>(d_input, thrust::raw_pointer_cast(d_input.data()));
}
C2H_TEST("Block load works with caching iterators", "[load][block]", items_per_thread, load_algorithm)
{
using type = int;
constexpr int items_per_thread = c2h::get<0, TestType>::value;
constexpr int threads_in_block = 64;
constexpr int tile_size = items_per_thread * threads_in_block;
static constexpr cub::BlockLoadAlgorithm load_algorithm = c2h::get<1, TestType>::value;
c2h::device_vector<type> d_input(GENERATE_COPY(take(10, random(0, tile_size))));
c2h::gen(C2H_SEED(10), d_input);
cub::CacheModifiedInputIterator<cub::CacheLoadModifier::LOAD_DEFAULT, type> in(
thrust::raw_pointer_cast(d_input.data()));
test_block_load<items_per_thread, threads_in_block, load_algorithm>(d_input, in);
}
#if IPT == 1
C2H_TEST("Vectorized block load with const and non-const datatype and different alignment cases",
"[load][block]",
c2h::type_list<const int*, int*>)
{
using type = int;
using input_ptr_type = c2h::get<0, TestType>;
const int offset_for_elements = GENERATE_COPY(0, 1, 2, 3, 4);
constexpr int items_per_thread = 4;
constexpr int threads_in_block = 64;
constexpr int tile_size = items_per_thread * threads_in_block;
static constexpr cub::BlockLoadAlgorithm load_algorithm = cub::BlockLoadAlgorithm::BLOCK_LOAD_VECTORIZE;
c2h::device_vector<type> d_input_ref(tile_size);
c2h::gen(C2H_SEED(10), d_input_ref);
c2h::device_vector<type> d_input(tile_size + offset_for_elements);
thrust::copy_n(d_input_ref.begin(), tile_size, d_input.begin() + offset_for_elements);
test_block_load<items_per_thread, threads_in_block, load_algorithm, type, input_ptr_type>(
d_input_ref, thrust::raw_pointer_cast(d_input.data()) + offset_for_elements);
}
#endif