// SPDX-FileCopyrightText: Copyright (c) 2011-2022, NVIDIA CORPORATION. All rights reserved. // SPDX-License-Identifier: BSD-3 #include #include #include #include #include template static __device__ int get_output_idx(int item) { if (LoadAlgorithm == cub::BlockLoadAlgorithm::BLOCK_LOAD_STRIPED) { return static_cast(threadIdx.x) + ThreadsInBlock * item; } return static_cast(threadIdx.x) * ItemsPerThread + item; } template __global__ void kernel(cuda::std::true_type, InputIteratorT input, OutputIteratorT output, int num_items) { using input_t = cub::detail::it_value_t; using block_load_t = cub::BlockLoad; 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(i); if (idx < num_items) { output[idx] = data[i]; } } } template __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(i); if (idx < num_items) { output[idx] = input[idx]; } } } template void test_block_load(const c2h::device_vector& d_input, InputIteratorT input) { using block_load_t = cub::BlockLoad; using storage_t = typename block_load_t::TempStorage; constexpr auto sufficient_resources = cuda::std::bool_constant{}; c2h::device_vector d_output(d_input.size()); kernel<<<1, ThreadsInBlock>>>( sufficient_resources, input, thrust::raw_pointer_cast(d_output.data()), static_cast(d_input.size())); REQUIRE(cudaSuccess == cudaPeekAtLastError()); REQUIRE(cudaSuccess == cudaDeviceSynchronize()); REQUIRE(d_input == d_output); } // %PARAM% IPT it 1:11 using types = c2h::type_list; using vec_types = c2h::type_list; using even_threads_in_block = c2h::enum_type_list; using odd_threads_in_block = c2h::enum_type_list; using a_block_size = c2h::enum_type_list; using items_per_thread = c2h::enum_type_list; using load_algorithm = c2h::enum_type_list; using odd_load_algorithm = c2h::enum_type_list; template 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; using type = typename params::type; c2h::device_vector d_input(GENERATE_COPY(take(10, random(0, params::tile_size)))); c2h::gen(C2H_SEED(10), d_input); test_block_load( 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; using type = typename params::type; c2h::device_vector d_input(GENERATE_COPY(take(10, random(0, params::tile_size)))); c2h::gen(C2H_SEED(10), d_input); test_block_load( 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; using type = typename params::type; c2h::device_vector d_input(GENERATE_COPY(take(10, random(0, params::tile_size)))); c2h::gen(C2H_SEED(10), d_input); test_block_load( 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; 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 d_input(GENERATE_COPY(take(10, random(0, tile_size)))); c2h::gen(C2H_SEED(10), d_input); test_block_load(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 d_input(GENERATE_COPY(take(10, random(0, tile_size)))); c2h::gen(C2H_SEED(10), d_input); cub::CacheModifiedInputIterator in( thrust::raw_pointer_cast(d_input.data())); test_block_load(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) { 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 d_input_ref(tile_size); c2h::gen(C2H_SEED(10), d_input_ref); c2h::device_vector 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( d_input_ref, thrust::raw_pointer_cast(d_input.data()) + offset_for_elements); } #endif