// MIT License // // Copyright (c) 2022-2024 Advanced Micro Devices, Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. #include "common_benchmark_header.hpp" #include #ifndef DEFAULT_N const size_t DEFAULT_N = 1024 * 1024 * 32; #endif template __global__ __launch_bounds__(BlockSize) void kernel(const T* input, T* output) { Runner::template run(input, output); } struct offset { template __device__ static void run(const T* input, T* output) { const unsigned int tid = hipBlockIdx_x * BlockSize + hipThreadIdx_x; T value = input[tid]; using bshuffle_t = hipcub::BlockShuffle; __shared__ typename bshuffle_t::TempStorage storage; #pragma nounroll for(unsigned int trial = 0; trial < Trials; trial++) { bshuffle_t(storage).Offset(value, value, 1); // sync is required because of loop since // temporary storage is accessed next iteration __syncthreads(); } output[tid] = value; } static constexpr bool uses_ipt = false; }; struct rotate { template __device__ static void run(const T* input, T* output) { const unsigned int tid = hipBlockIdx_x * BlockSize + hipThreadIdx_x; T value = input[tid]; using bshuffle_t = hipcub::BlockShuffle; __shared__ typename bshuffle_t::TempStorage storage; #pragma nounroll for(unsigned int trial = 0; trial < Trials; trial++) { bshuffle_t(storage).Rotate(value, value, 1); // sync is required because of loop since // temporary storage is accessed next iteration __syncthreads(); } output[tid] = value; } static constexpr bool uses_ipt = false; }; struct up { template __device__ static void run(const T* input, T* output) { const unsigned int tid = hipBlockIdx_x * BlockSize + hipThreadIdx_x; T values[ItemsPerThread]; for(unsigned int i = 0; i < ItemsPerThread; i++) { values[i] = input[ItemsPerThread * tid + i]; } using bshuffle_t = hipcub::BlockShuffle; __shared__ typename bshuffle_t::TempStorage storage; #pragma nounroll for(unsigned int trial = 0; trial < Trials; trial++) { bshuffle_t(storage).Up(values, values); // sync is required because of loop since // temporary storage is accessed next iteration __syncthreads(); } for(unsigned int i = 0; i < ItemsPerThread; i++) { output[ItemsPerThread * tid + i] = values[i]; } } static constexpr bool uses_ipt = true; }; struct down { template __device__ static void run(const T* input, T* output) { const unsigned int tid = hipBlockIdx_x * BlockSize + hipThreadIdx_x; T values[ItemsPerThread]; for(unsigned int i = 0; i < ItemsPerThread; i++) { values[i] = input[ItemsPerThread * tid + i]; } using bshuffle_t = hipcub::BlockShuffle; __shared__ typename bshuffle_t::TempStorage storage; #pragma nounroll for(unsigned int trial = 0; trial < Trials; trial++) { bshuffle_t(storage).Down(values, values); // sync is required because of loop since // temporary storage is accessed next iteration __syncthreads(); } for(unsigned int i = 0; i < ItemsPerThread; i++) { output[ItemsPerThread * tid + i] = values[i]; } } static constexpr bool uses_ipt = true; }; template void run_benchmark(benchmark::State& state, hipStream_t stream, size_t N) { constexpr auto items_per_block = BlockSize * ItemsPerThread; const auto size = items_per_block * ((N + items_per_block - 1) / items_per_block); std::vector input(size, T(1)); T* d_input; T* d_output; HIP_CHECK(hipMalloc(&d_input, size * sizeof(T))); HIP_CHECK(hipMalloc(&d_output, size * sizeof(T))); HIP_CHECK(hipMemcpy(d_input, input.data(), size * sizeof(T), hipMemcpyHostToDevice)); HIP_CHECK(hipDeviceSynchronize()); for(auto _ : state) { auto start = std::chrono::high_resolution_clock::now(); hipLaunchKernelGGL(HIP_KERNEL_NAME(kernel), dim3(size / items_per_block), dim3(BlockSize), 0, stream, d_input, d_output); HIP_CHECK(hipPeekAtLastError()); HIP_CHECK(hipDeviceSynchronize()); auto end = std::chrono::high_resolution_clock::now(); auto elapsed_seconds = std::chrono::duration_cast>(end - start); state.SetIterationTime(elapsed_seconds.count()); } state.SetBytesProcessed(state.iterations() * Trials * size * sizeof(T)); state.SetItemsProcessed(state.iterations() * Trials * size); HIP_CHECK(hipFree(d_input)); HIP_CHECK(hipFree(d_output)); } #define CREATE_BENCHMARK_IPT(BS, IPT) \ benchmark::RegisterBenchmark( \ ("block_shuffle.sub_algorithm_name:" + name) \ .c_str(), \ &run_benchmark, \ stream, \ size) #define CREATE_BENCHMARK(BS) \ benchmark::RegisterBenchmark(("block_shuffle.sub_algorithm_name:" + name) \ .c_str(), \ &run_benchmark, \ stream, \ size) template = true> void add_benchmarks_type(const std::string& name, std::vector& benchmarks, hipStream_t stream, size_t size, const std::string& type_name) { std::vector bs = { CREATE_BENCHMARK_IPT(256, 1), CREATE_BENCHMARK_IPT(256, 3), CREATE_BENCHMARK_IPT(256, 4), CREATE_BENCHMARK_IPT(256, 8), CREATE_BENCHMARK_IPT(256, 16), CREATE_BENCHMARK_IPT(256, 32), }; benchmarks.insert(benchmarks.end(), bs.begin(), bs.end()); } template = true> void add_benchmarks_type(const std::string& name, std::vector& benchmarks, hipStream_t stream, size_t size, const std::string& type_name) { std::vector bs = { CREATE_BENCHMARK(256), }; benchmarks.insert(benchmarks.end(), bs.begin(), bs.end()); } #define CREATE_BENCHMARKS(T) add_benchmarks_type(name, benchmarks, stream, size, #T) template void add_benchmarks(const std::string& name, std::vector& benchmarks, hipStream_t stream, size_t size) { using custom_float2 = benchmark_utils::custom_type; using custom_double2 = benchmark_utils::custom_type; CREATE_BENCHMARKS(int); CREATE_BENCHMARKS(float); CREATE_BENCHMARKS(double); CREATE_BENCHMARKS(int8_t); CREATE_BENCHMARKS(long long); CREATE_BENCHMARKS(custom_float2); CREATE_BENCHMARKS(custom_double2); } int main(int argc, char* argv[]) { cli::Parser parser(argc, argv); parser.set_optional("size", "size", DEFAULT_N, "number of values"); parser.set_optional("trials", "trials", -1, "number of iterations"); parser.run_and_exit_if_error(); // Parse argv benchmark::Initialize(&argc, argv); const size_t size = parser.get("size"); const int trials = parser.get("trials"); std::cout << "benchmark_block_shuffle" << std::endl; // HIP hipStream_t stream = 0; // default hipDeviceProp_t devProp; int device_id = 0; HIP_CHECK(hipGetDevice(&device_id)); HIP_CHECK(hipGetDeviceProperties(&devProp, device_id)); std::cout << "[HIP] Device name: " << devProp.name << std::endl; // Add benchmarks std::vector benchmarks; add_benchmarks("offset", benchmarks, stream, size); add_benchmarks("rotate", benchmarks, stream, size); add_benchmarks("up", benchmarks, stream, size); add_benchmarks("down", benchmarks, stream, size); // Use manual timing for(auto& b : benchmarks) { b->UseManualTime(); b->Unit(benchmark::kMillisecond); } // Force number of iterations if(trials > 0) { for(auto& b : benchmarks) { b->Iterations(trials); } } // Run benchmarks benchmark::RunSpecifiedBenchmarks(); return 0; }