// MIT License
//
// Copyright (c) 2017-2025 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 "benchmark_device_segmented_radix_sort_pairs.parallel.hpp"
#include "benchmark_utils.hpp"

#include "../common/utils_data_generation.hpp"
#ifndef BENCHMARK_CONFIG_TUNING
    #include "../common/utils_custom_type.hpp"
#endif

// HIP API
#include <hip/hip_runtime.h>

// rocPRIM
#include <rocprim/device/device_segmented_radix_sort.hpp>
#ifndef BENCHMARK_CONFIG_TUNING
    #include <rocprim/types.hpp>
#endif

#include <array>
#include <cmath>
#include <cstddef>
#include <numeric>
#include <random>
#include <string>
#include <vector>
#ifndef BENCHMARK_CONFIG_TUNING
    #include <stdint.h>
#endif

// This benchmark only handles the rocprim::segmented_radix_sort_pairs function. The benchmark was separated into two (keys and pairs),
// because the binary became too large to link. Runs into a "relocation R_X86_64_PC32 out of range" error.
// This happens partially, because of the algorithm has 4 kernels, and decides at runtime which one to call.

template<typename KeyT, typename ValueT>
void add_benchmarks(benchmark_utils::executor& executor, size_t bytes)
{
    constexpr std::array<size_t, 8> segment_counts{10, 100, 1000, 2500, 5000, 7500, 10000, 100000};
    constexpr std::array<size_t, 4> segment_lengths{30, 256, 3000, 300000};

    constexpr size_t min_size = 30000;
    size_t           max_size = bytes / sizeof(KeyT);

    for(const auto segment_count : segment_counts)
    {
        for(const auto segment_length : segment_lengths)
        {
            // This check is also present in device_segmented_radix_sort_pairs_benchmark its run()
            // We need it here to prevent Google Benchmark causing an infinite loop
            const auto number_of_elements = segment_count * segment_length;
            if(number_of_elements < min_size || number_of_elements > max_size)
            {
                continue;
            }

            executor.queue_instance(
                device_segmented_radix_sort_pairs_benchmark<KeyT, ValueT>(segment_count,
                                                                          segment_length));
        }
    }
}

#define BENCHMARK_TYPE_TUNING(KEY_TYPE, VALUE_TYPE) \
    add_benchmarks<KEY_TYPE, VALUE_TYPE>(executor, bytes);

#define BENCHMARK_TYPES_TUNING(KEY_TYPE)               \
    BENCHMARK_TYPE_TUNING(KEY_TYPE, rocprim::int128_t) \
    BENCHMARK_TYPE_TUNING(KEY_TYPE, int64_t)           \
    BENCHMARK_TYPE_TUNING(KEY_TYPE, int)               \
    BENCHMARK_TYPE_TUNING(KEY_TYPE, short)             \
    BENCHMARK_TYPE_TUNING(KEY_TYPE, int8_t)

int main(int argc, char* argv[])
{
    size_t bytes = 128 * benchmark_utils::MiB;

    benchmark_utils::executor executor(argc, argv, bytes, 10, 5);

#ifndef BENCHMARK_CONFIG_TUNING
    // Tuned types
    BENCHMARK_TYPES_TUNING(rocprim::int128_t)
    BENCHMARK_TYPES_TUNING(int64_t)
    BENCHMARK_TYPES_TUNING(int)
    BENCHMARK_TYPES_TUNING(short)
    BENCHMARK_TYPES_TUNING(int8_t)
    BENCHMARK_TYPES_TUNING(double)
    BENCHMARK_TYPES_TUNING(float)
    BENCHMARK_TYPES_TUNING(rocprim::half)

    #ifndef BENCHMARK_AUTOTUNED_TYPES_ONLY
    // Not tuned types
    add_benchmarks<int, float>(executor, bytes);
    add_benchmarks<long long, double>(executor, bytes);
    add_benchmarks<uint8_t, uint8_t>(executor, bytes);
    add_benchmarks<rocprim::half, rocprim::half>(executor, bytes);
    add_benchmarks<rocprim::uint128_t, rocprim::uint128_t>(executor, bytes);

    // Not tuned custom types
    using custom_float2  = common::custom_type<float, float>;
    using custom_double2 = common::custom_type<double, double>;

    add_benchmarks<int, custom_float2>(executor, bytes);
    add_benchmarks<long long, custom_double2>(executor, bytes);
    #endif
#endif

    executor.run();
}
