/* * Copyright (c) Advanced Micro Devices, Inc., or its affiliates. * * SPDX-License-Identifier: MIT */ #pragma once #include "math_common.hh" #include "math_special_values.hh" #include namespace cg = cooperative_groups; #define MATH_POW_INT_KERNEL_DEF(func_name) \ template \ __global__ void func_name##_kernel(T1* const ys, const size_t num_xs, T1* const x1s, \ T2* const x2s) { \ const auto tid = cg::this_grid().thread_rank(); \ const auto stride = cg::this_grid().size(); \ \ for (size_t i = tid; i < num_xs; i += stride) { \ if constexpr (std::is_same_v) { \ ys[i] = func_name##f(x1s[i], x2s[i]); \ } else if constexpr (std::is_same_v) { \ ys[i] = func_name(x1s[i], x2s[i]); \ } \ } \ } template using kernel_pow_int_sig = void (*)(T1*, const size_t, T1*, T2*); template using ref_pow_int_sig = T1 (*)(T1, T2); template void PowIntFloatingPointBruteForceTest(kernel_pow_int_sig kernel, ref_pow_int_sig ref_func, const ValidatorBuilder& validator_builder) { const auto [grid_size, block_size] = GetOccupancyMaxPotentialBlockSize(kernel); const uint64_t num_iterations = GetTestIterationCount(); const auto max_batch_size = std::min(GetMaxAllowedDeviceMemoryUsage() / (sizeof(T1) * 2 + sizeof(T2)), num_iterations); LinearAllocGuard x1s{LinearAllocs::hipHostMalloc, max_batch_size * sizeof(T1)}; LinearAllocGuard x2s{LinearAllocs::hipHostMalloc, max_batch_size * sizeof(T2)}; MathTest math_test(kernel, max_batch_size); auto batch_size = max_batch_size; const auto num_threads = thread_pool.thread_count(); for (uint64_t i = 0ul; i < num_iterations; i += batch_size) { batch_size = std::min(max_batch_size, num_iterations - i); const auto min_sub_batch_size = batch_size / num_threads; const auto tail = batch_size % num_threads; auto base_idx = 0u; for (auto i = 0u; i < num_threads; ++i) { const auto sub_batch_size = min_sub_batch_size + (i < tail); thread_pool.Post([=, &x1s, &x2s] { const auto generator1 = [=] { static thread_local std::mt19937 rng(std::random_device{}()); std::uniform_real_distribution> unif_dist(std::numeric_limits::lowest(), std::numeric_limits::max()); return static_cast(unif_dist(rng)); }; const auto generator2 = [] { static thread_local std::mt19937 rng(std::random_device{}()); std::uniform_int_distribution unif_dist(std::numeric_limits::lowest(), std::numeric_limits::max()); return unif_dist(rng); }; std::generate(x1s.ptr() + base_idx, x1s.ptr() + base_idx + sub_batch_size, generator1); std::generate(x2s.ptr() + base_idx, x2s.ptr() + base_idx + sub_batch_size, generator2); }); base_idx += sub_batch_size; } thread_pool.Wait(); math_test.Run(validator_builder, grid_size, block_size, ref_func, batch_size, x1s.ptr(), x2s.ptr()); } } template void PowIntFloatingPointSpecialValuesTest(kernel_pow_int_sig kernel, ref_pow_int_sig ref_func, const ValidatorBuilder& validator_builder) { const auto [grid_size, block_size] = GetOccupancyMaxPotentialBlockSize(kernel); const auto values1 = std::get>(kSpecialValRegistry); const auto values2 = std::get>(kSpecialValRegistry); const auto size = values1.size * values2.size; LinearAllocGuard x1s{LinearAllocs::hipHostMalloc, size * sizeof(T1)}; LinearAllocGuard x2s{LinearAllocs::hipHostMalloc, size * sizeof(T2)}; for (auto i = 0u; i < values1.size; ++i) { for (auto j = 0u; j < values2.size; ++j) { x1s.ptr()[i * values2.size + j] = values1.data[i]; x2s.ptr()[i * values2.size + j] = static_cast(values2.data[j]); } } MathTest math_test(kernel, size); math_test.template Run(validator_builder, grid_size, block_size, ref_func, size, x1s.ptr(), x2s.ptr()); } template void PowIntFloatingPointTest(kernel_pow_int_sig kernel, ref_pow_int_sig ref_func, const ValidatorBuilder& validator_builder) { SECTION("Special values") { PowIntFloatingPointSpecialValuesTest(kernel, ref_func, validator_builder); } SECTION("Brute force") { PowIntFloatingPointBruteForceTest(kernel, ref_func, validator_builder); } }