/* Copyright (c) 2023 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 #include namespace { #if (!__GFX8__ && !__GFX9__) || HT_NVIDIA constexpr size_t kWarpSize = 32; #else constexpr size_t kWarpSize = 64; #endif } // namespace constexpr int MaxGPUs = 8; __device__ inline unsigned int thread_rank_in_grid() { const auto block_size = blockDim.x * blockDim.y * blockDim.z; const auto block_rank_in_grid = (blockIdx.z * gridDim.y + blockIdx.y) * gridDim.x + blockIdx.x; const auto thread_rank_in_block = (threadIdx.z * blockDim.y + threadIdx.y) * blockDim.x + threadIdx.x; return block_rank_in_grid * block_size + thread_rank_in_block; } static __device__ void busy_wait(unsigned long long wait_period) { unsigned long long time_diff = 0; unsigned long long last_clock = clock64(); while (time_diff < wait_period) { unsigned long long cur_clock = clock64(); if (cur_clock > last_clock) { time_diff += (cur_clock - last_clock); } last_clock = cur_clock; } } template bool CheckDimensions(unsigned int device, T kernel, dim3 blocks, dim3 threads) { hipDeviceProp_t props; int max_blocks_per_sm = 0; int num_sm = 0; HIP_CHECK(hipSetDevice(device)); HIP_CHECK(hipOccupancyMaxActiveBlocksPerMultiprocessor(&max_blocks_per_sm, kernel, threads.x * threads.y * threads.z, 0)); HIP_CHECK(hipGetDeviceProperties(&props, device)); num_sm = props.multiProcessorCount; if ((blocks.x * blocks.y * blocks.z) > max_blocks_per_sm * num_sm) { return false; } return true; }