// MIT License // // Copyright (c) 2025-2026 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. #pragma once #include "primbench.hpp" #include "benchmark_utils.hpp" #include "../common/utils_data_generation.hpp" #include "../common/utils_device_ptr.hpp" #include #include #include #include #include #include #include #include #include #include #include enum memory_operation_method { block_primitives_transpose, striped, vectorized, block_primitive_direct, copy, }; enum kernel_operation { no_operation, block_scan, custom_operation, atomics_no_collision, atomics_inter_block_collision, atomics_inter_warp_collision, }; template struct operation; // no operation template struct operation { ROCPRIM_HOST_DEVICE inline void operator()(T (&)[ItemsPerThread], void* = nullptr, unsigned int = 0, T* = nullptr) const { // No operation } }; // custom operation template struct operation { ROCPRIM_HOST_DEVICE inline void operator()(T (&input)[ItemsPerThread], void* shared_storage = nullptr, unsigned int shared_storage_size = 0, T* global_mem_output = nullptr) const { (void)shared_storage; (void)shared_storage_size; (void)global_mem_output; constexpr unsigned int repeats = 30; ROCPRIM_UNROLL for(unsigned int i = 0; i < ItemsPerThread; ++i) { input[i] = input[i] + 666; ROCPRIM_UNROLL for(unsigned int j = 0; j < repeats; ++j) { input[i] = input[i] * (input[j % ItemsPerThread]); } } } }; // block scan template struct operation { ROCPRIM_HOST_DEVICE inline void operator()(T (&input)[ItemsPerThread], void* shared_storage = nullptr, unsigned int shared_storage_size = 0, T* global_mem_output = nullptr) const { (void)global_mem_output; using block_scan_type = typename rocprim:: block_scan; block_scan_type bscan; // when using vectorized or striped functions // NOTE: This is not safe but it is the easiest way to prevent code repetition if(shared_storage == nullptr || shared_storage_size < sizeof(typename block_scan_type::storage_type)) { __shared__ typename block_scan_type::storage_type storage; shared_storage = &storage; } bscan.inclusive_scan( input, input, *(reinterpret_cast(shared_storage))); __syncthreads(); } }; // atomics_no_collision template struct operation { ROCPRIM_HOST_DEVICE inline void operator()(T (&input)[ItemsPerThread], void* shared_storage = nullptr, unsigned int shared_storage_size = 0, T* global_mem_output = nullptr) { (void)shared_storage; (void)shared_storage_size; (void)input; unsigned int index = threadIdx.x * ItemsPerThread + blockIdx.x * blockDim.x * ItemsPerThread; ROCPRIM_UNROLL for(unsigned int i = 0; i < ItemsPerThread; ++i) { atomicAdd(&global_mem_output[index + i], T(666)); } } }; // atomics_inter_block_collision template struct operation { ROCPRIM_HOST_DEVICE inline void operator()(T (&input)[ItemsPerThread], void* shared_storage = nullptr, unsigned int shared_storage_size = 0, T* global_mem_output = nullptr) { (void)shared_storage; (void)shared_storage_size; (void)input; unsigned int index = (threadIdx.x % rocprim::arch::wavefront::min_size()) * ItemsPerThread + blockIdx.x * blockDim.x * ItemsPerThread; ROCPRIM_UNROLL for(unsigned int i = 0; i < ItemsPerThread; ++i) { atomicAdd(&global_mem_output[index + i], T(666)); } } }; // atomics_inter_block_collision template struct operation { ROCPRIM_HOST_DEVICE inline void operator()(T (&input)[ItemsPerThread], void* shared_storage = nullptr, unsigned int shared_storage_size = 0, T* global_mem_output = nullptr) { (void)shared_storage; (void)shared_storage_size; (void)input; unsigned int index = threadIdx.x * ItemsPerThread; ROCPRIM_UNROLL for(unsigned int i = 0; i < ItemsPerThread; ++i) { atomicAdd(&global_mem_output[index + i], T(666)); } } }; // block_primitive_direct method base kernel template::value_type, typename std::enable_if::type = 0> __global__ __launch_bounds__(BlockSize) void operation_kernel(T* input, T* output, CustomOp op) { constexpr unsigned int items_per_block = BlockSize * ItemsPerThread; using block_load_type = typename rocprim:: block_load; using block_store_type = typename rocprim:: block_store; block_load_type load; block_store_type store; __shared__ union { typename block_load_type::storage_type load; typename block_store_type::storage_type store; } storage; int offset = blockIdx.x * items_per_block; T items[ItemsPerThread]; load.load(input + offset, items, storage.load); __syncthreads(); op(items, &storage, sizeof(storage), output); store.store(output + offset, items, storage.store); } // vectorized method base kernel template::value_type, typename std::enable_if::type = 0> __global__ __launch_bounds__(BlockSize) void operation_kernel(T* input, T* output, CustomOp op) { constexpr unsigned int items_per_block = BlockSize * ItemsPerThread; int offset = blockIdx.x * items_per_block; T items[ItemsPerThread]; rocprim::block_load_direct_blocked_vectorized(threadIdx.x, input + offset, items); __syncthreads(); op(items, nullptr, 0, output); rocprim::block_store_direct_blocked_vectorized(threadIdx.x, output + offset, items); } // striped method base kernel template::value_type, typename std::enable_if::type = 0> __global__ __launch_bounds__(BlockSize) void operation_kernel(T* input, T* output, CustomOp op) { const unsigned int lid = threadIdx.x; const unsigned int block_offset = blockIdx.x * ItemsPerThread * BlockSize; T items[ItemsPerThread]; rocprim::block_load_direct_striped(lid, input + block_offset, items); op(items, nullptr, 0, output); rocprim::block_store_direct_striped(lid, output + block_offset, items); } // block_primitives_transpose method base kernel template::value_type, typename std::enable_if::type = 0> __global__ __launch_bounds__(BlockSize) void operation_kernel(T* input, T* output, CustomOp op) { constexpr unsigned int items_per_block = BlockSize * ItemsPerThread; using block_load_type = typename rocprim:: block_load; using block_store_type = typename rocprim::block_store; block_load_type load; block_store_type store; __shared__ union { typename block_load_type::storage_type load; typename block_store_type::storage_type store; } storage; int offset = blockIdx.x * items_per_block; T items[ItemsPerThread]; load.load(input + offset, items, storage.load); __syncthreads(); op(items, &storage, sizeof(storage), output); store.store(output + offset, items, storage.store); } template struct device_memory_benchmark : public primbench::benchmark_interface { primbench::json meta() const override { return primbench::json{} .add("lvl", "device") .add("algo", "device_memory") .add("subalgo", get_method_name(MemOp)) .add("operation", get_operation_name(KernelOp)) .add("key_type", primbench::name()) .add("cfg", primbench::json{}.add("bs", BlockSize).add("ipt", ItemsPerThread)); } void run(primbench::state& state) override { const auto& stream = state.stream; const auto& bytes = state.size; const auto& seed = state.seed; const size_t items = bytes / sizeof(T); const size_t grid_size = items / (BlockSize * ItemsPerThread); std::vector input = get_random_data(items, common::generate_limits::min(), common::generate_limits::max(), seed); common::device_ptr d_input(input); common::device_ptr d_output(items); operation selected_operation; state.set_items(items); state.add_reads(items); state.run( [&] { if constexpr(MemOp == copy) { // For copy benchmark, perform device-to-device memcpy. // We deliberately call hipMemcpyAsync() instead of hipMemcpy() here, // since hipMemcpy() uses the slow default legacy stream (stream 0). HIP_CHECK(hipMemcpyAsync(d_output.get(), d_input.get(), items * sizeof(T), hipMemcpyDeviceToDevice, stream)); } else { operation_kernel <<>>(d_input.get(), d_output.get(), selected_operation); } }); } private: std::string get_method_name(memory_operation_method method) const { switch(method) { case block_primitives_transpose: return "block_primitives_transpose"; case striped: return "striped"; case vectorized: return "vectorized"; case block_primitive_direct: return "block_primitive_direct"; case copy: return "copy"; // Not using `default: ...` because it kills effectiveness of -Wswitch } return "unknown_method"; } std::string get_operation_name(kernel_operation operation) const { switch(operation) { case no_operation: return "no_operation"; case block_scan: return "block_scan"; case custom_operation: return "custom_operation"; case atomics_no_collision: return "atomics_no_collision"; case atomics_inter_block_collision: return "atomics_inter_block_collision"; case atomics_inter_warp_collision: return "atomics_inter_warp_collision"; // Not using `default: ...` because it kills effectiveness of -Wswitch } return "unknown_operation"; } };