Files
TheRock/rocm-libraries/projects/rocprim/benchmark/benchmark_device_select.hpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

652 lines
23 KiB
C++

// MIT License
//
// Copyright (c) 2024-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 <hip/hip_runtime.h>
#include <rocprim/device/config_types.hpp>
#include <rocprim/device/device_select.hpp>
#include <rocprim/functional.hpp>
#include <rocprim/types.hpp>
#ifdef BENCHMARK_CONFIG_TUNING
#include <rocprim/device/detail/device_config_helper.hpp>
#endif
#include <cstddef>
#include <string>
#include <type_traits>
#include <vector>
#ifdef BENCHMARK_CONFIG_TUNING
#include <algorithm>
#include <memory>
#endif
enum class select_probability
{
p005,
p025,
p050,
p075,
tuning
};
constexpr inline float get_probability(select_probability probability)
{
switch(probability)
{
case select_probability::p005: return 0.05f;
case select_probability::p025: return 0.25f;
case select_probability::p050: return 0.50f;
case select_probability::p075: return 0.75f;
case select_probability::tuning: return 0.0f; // not used
}
return 0.0f;
}
constexpr inline const char* get_probability_name(select_probability probability)
{
switch(probability)
{
case select_probability::p005: return "0.05";
case select_probability::p025: return "0.25";
case select_probability::p050: return "0.50";
case select_probability::p075: return "0.75";
case select_probability::tuning: return "tuning";
}
return "invalid";
}
template<typename Config>
constexpr auto config_name()
{
if constexpr(std::is_same_v<Config, rocprim::default_config>)
{
return std::string("default");
}
else
{
auto config = Config();
return primbench::json{}
.add("bs", config.kernel_config.block_size)
.add("ipt", config.kernel_config.items_per_thread);
}
}
template<typename DataType,
typename Config = rocprim::default_config,
typename FlagType = int8_t,
select_probability Probability = select_probability::tuning>
struct device_select_flag_benchmark : public primbench::benchmark_interface
{
primbench::json meta() const override
{
return primbench::json{}
.add("lvl", "device")
.add("algo", "device_select")
.add("subalgo", "flag")
.add("data_type", primbench::name<DataType>())
.add("flag_type", primbench::name<FlagType>())
.add("probability", get_probability_name(Probability))
.add("cfg", config_name<Config>());
}
void run(primbench::state& state) override
{
const auto& stream = state.stream;
const auto& bytes = state.size;
const auto& seed = state.seed;
auto seeds = primbench::seeds<2>(seed);
size_t items = bytes / sizeof(DataType);
std::vector<DataType> input
= get_random_data<DataType>(items,
common::generate_limits<DataType>::min(),
common::generate_limits<DataType>::max(),
seeds[0]);
std::vector<FlagType> flags_0;
std::vector<FlagType> flags_1;
std::vector<FlagType> flags_2;
if(is_tuning)
{
flags_0 = get_random_data01<FlagType>(items, 0.0f, seeds[1]);
flags_1 = get_random_data01<FlagType>(items, 0.5f, seeds[1]);
flags_2 = get_random_data01<FlagType>(items, 1.0f, seeds[1]);
}
else
{
flags_0 = get_random_data01<FlagType>(items, get_probability(Probability), seeds[1]);
}
common::device_ptr<DataType> d_input(input);
common::device_ptr<FlagType> d_flags_0(flags_0);
common::device_ptr<FlagType> d_flags_1;
common::device_ptr<FlagType> d_flags_2;
if(is_tuning)
{
d_flags_1.store(flags_1);
d_flags_2.store(flags_2);
}
common::device_ptr<DataType> d_output(items);
common::device_ptr<unsigned int> d_selected_count_output(1);
const auto dispatch = [&](void* d_temp_storage, size_t& temp_storage_size_bytes)
{
const auto dispatch_flags = [&](FlagType* d_flags)
{
HIP_CHECK(rocprim::select<Config>(d_temp_storage,
temp_storage_size_bytes,
d_input.get(),
d_flags,
d_output.get(),
d_selected_count_output.get(),
items,
stream));
};
dispatch_flags(d_flags_0.get());
if(is_tuning)
{
dispatch_flags(d_flags_1.get());
dispatch_flags(d_flags_2.get());
}
};
// Allocate temporary storage memory
size_t temp_storage_size_bytes{};
dispatch(nullptr, temp_storage_size_bytes);
common::device_ptr<void> d_temp_storage(temp_storage_size_bytes);
state.set_items(items);
state.add_reads<DataType>(items);
state.run([&] { dispatch(d_temp_storage.get(), temp_storage_size_bytes); });
}
static constexpr bool is_tuning = Probability == select_probability::tuning;
};
template<typename DataType,
typename Config = rocprim::default_config,
select_probability Probability = select_probability::tuning>
struct device_select_predicate_benchmark : public primbench::benchmark_interface
{
primbench::json meta() const override
{
return primbench::json{}
.add("lvl", "device")
.add("algo", "device_select")
.add("subalgo", "predicate")
.add("data_type", primbench::name<DataType>())
.add("probability", get_probability_name(Probability))
.add("cfg", config_name<Config>());
}
void run(primbench::state& state) override
{
const auto& stream = state.stream;
const auto& bytes = state.size;
const auto& seed = state.seed;
size_t items = bytes / sizeof(DataType);
// all data types can represent [0, 127], -1 so a predicate can select all
std::vector<DataType> input = get_random_data<DataType>(items,
static_cast<DataType>(0),
static_cast<DataType>(126),
seed);
common::device_ptr<DataType> d_input(input);
common::device_ptr<DataType> d_output(items);
common::device_ptr<unsigned int> d_selected_count_output(1);
const auto dispatch = [&](void* d_temp_storage, size_t& temp_storage_size_bytes)
{
const auto dispatch_predicate = [&](float probability)
{
auto predicate = [probability](const DataType& value) -> bool
{ return value < static_cast<DataType>(127 * probability); };
HIP_CHECK(rocprim::select<Config>(d_temp_storage,
temp_storage_size_bytes,
d_input.get(),
d_output.get(),
d_selected_count_output.get(),
items,
predicate,
stream));
};
if(is_tuning)
{
dispatch_predicate(0.0f);
dispatch_predicate(0.5f);
dispatch_predicate(1.0f);
}
else
{
dispatch_predicate(get_probability(Probability));
}
};
size_t temp_storage_size_bytes{};
dispatch(nullptr, temp_storage_size_bytes);
common::device_ptr<void> d_temp_storage(temp_storage_size_bytes);
state.set_items(items);
state.add_reads<DataType>(items);
state.run([&] { dispatch(d_temp_storage.get(), temp_storage_size_bytes); });
}
static constexpr bool is_tuning = Probability == select_probability::tuning;
};
template<typename DataType,
typename FlagType = int,
typename Config = rocprim::default_config,
select_probability Probability = select_probability::tuning>
struct device_select_predicated_flag_benchmark : public primbench::benchmark_interface
{
primbench::json meta() const override
{
return primbench::json{}
.add("lvl", "device")
.add("algo", "device_select")
.add("subalgo", "predicated_flag")
.add("data_type", primbench::name<DataType>())
.add("flag_type", primbench::name<FlagType>())
.add("probability", get_probability_name(Probability))
.add("cfg", config_name<Config>());
}
void run(primbench::state& state) override
{
const auto& stream = state.stream;
const auto& bytes = state.size;
const auto& seed = state.seed;
auto seeds = primbench::seeds<2>(seed);
size_t items = bytes / sizeof(DataType);
std::vector<DataType> input
= get_random_data<DataType>(items,
common::generate_limits<DataType>::min(),
common::generate_limits<DataType>::max(),
seeds[0]);
std::vector<FlagType> flags_0;
std::vector<FlagType> flags_1;
std::vector<FlagType> flags_2;
if(is_tuning)
{
flags_0 = get_random_data01<FlagType>(items, 0.0f, seeds[1]);
flags_1 = get_random_data01<FlagType>(items, 0.5f, seeds[1]);
flags_2 = get_random_data01<FlagType>(items, 1.0f, seeds[1]);
}
else
{
flags_0 = get_random_data01<FlagType>(items, get_probability(Probability), seeds[1]);
}
common::device_ptr<DataType> d_input(input);
common::device_ptr<FlagType> d_flags_0(flags_0);
common::device_ptr<FlagType> d_flags_1;
common::device_ptr<FlagType> d_flags_2;
if(is_tuning)
{
d_flags_1.store(flags_1);
d_flags_2.store(flags_2);
}
common::device_ptr<DataType> d_output(items);
common::device_ptr<unsigned int> d_selected_count_output(1);
const auto dispatch = [&](void* d_temp_storage, size_t& temp_storage_size_bytes)
{
const auto dispatch_predicated_flags = [&](FlagType* d_flags)
{
auto predicate = [](const FlagType& value) -> bool { return value; };
HIP_CHECK(rocprim::select<Config>(d_temp_storage,
temp_storage_size_bytes,
d_input.get(),
d_flags,
d_output.get(),
d_selected_count_output.get(),
items,
predicate,
stream));
};
dispatch_predicated_flags(d_flags_0.get());
if(is_tuning)
{
dispatch_predicated_flags(d_flags_1.get());
dispatch_predicated_flags(d_flags_2.get());
}
};
// Allocate temporary storage memory
size_t temp_storage_size_bytes{};
dispatch(nullptr, temp_storage_size_bytes);
common::device_ptr<void> d_temp_storage(temp_storage_size_bytes);
state.set_items(items);
state.add_reads<DataType>(items);
state.run([&] { dispatch(d_temp_storage.get(), temp_storage_size_bytes); });
}
static constexpr bool is_tuning = Probability == select_probability::tuning;
};
template<typename DataType>
inline std::vector<DataType> get_unique_input(size_t items, float probability, unsigned int seed)
{
using op_type = typename std::conditional<std::is_same<DataType, rocprim::half>::value,
half_plus,
rocprim::plus<DataType>>::type;
op_type op;
std::vector<DataType> input(items);
auto input01 = get_random_data01<DataType>(items, probability, seed);
auto acc = input01[0];
input[0] = acc;
for(size_t i = 1; i < input01.size(); ++i)
{
input[i] = op(acc, input01[i]);
}
return input;
}
template<typename DataType,
typename Config = rocprim::default_config,
select_probability Probability = select_probability::tuning>
struct device_select_unique_benchmark : public primbench::benchmark_interface
{
primbench::json meta() const override
{
return primbench::json{}
.add("lvl", "device")
.add("algo", "device_select")
.add("subalgo", "unique")
.add("data_type", primbench::name<DataType>())
.add("probability", get_probability_name(Probability))
.add("cfg", config_name<Config>());
}
void run(primbench::state& state) override
{
const auto& stream = state.stream;
const auto& bytes = state.size;
const auto& seed = state.seed;
size_t items = bytes / sizeof(DataType);
std::vector<DataType> input_0;
std::vector<DataType> input_1;
std::vector<DataType> input_2;
if(is_tuning)
{
input_0 = get_unique_input<DataType>(items, 0.0f, seed);
input_1 = get_unique_input<DataType>(items, 0.5f, seed);
input_2 = get_unique_input<DataType>(items, 1.0f, seed);
}
else
{
input_0 = get_unique_input<DataType>(items, get_probability(Probability), seed);
}
common::device_ptr<DataType> d_input_0(input_0);
common::device_ptr<DataType> d_input_1;
common::device_ptr<DataType> d_input_2;
if(is_tuning)
{
d_input_1.store(input_1);
d_input_2.store(input_2);
}
common::device_ptr<DataType> d_output(items);
common::device_ptr<unsigned int> d_selected_count_output(1);
const auto dispatch = [&](void* d_temp_storage, size_t& temp_storage_size_bytes)
{
const auto dispatch_flags = [&](DataType* d_input)
{
HIP_CHECK(rocprim::unique<Config>(d_temp_storage,
temp_storage_size_bytes,
d_input,
d_output.get(),
d_selected_count_output.get(),
items,
rocprim::equal_to<DataType>(),
stream));
};
dispatch_flags(d_input_0.get());
if(is_tuning)
{
dispatch_flags(d_input_1.get());
dispatch_flags(d_input_2.get());
}
};
// Allocate temporary storage memory
size_t temp_storage_size_bytes{};
dispatch(nullptr, temp_storage_size_bytes);
common::device_ptr<void> d_temp_storage(temp_storage_size_bytes);
state.set_items(items);
state.add_reads<DataType>(items);
state.run([&] { dispatch(d_temp_storage.get(), temp_storage_size_bytes); });
}
static constexpr bool is_tuning = Probability == select_probability::tuning;
};
template<typename KeyType,
typename ValueType,
typename Config = rocprim::default_config,
select_probability Probability = select_probability::tuning>
struct device_select_unique_by_key_benchmark : public primbench::benchmark_interface
{
primbench::json meta() const override
{
return primbench::json{}
.add("lvl", "device")
.add("algo", "device_select")
.add("subalgo", "unique_by_key")
.add("key_type", primbench::name<KeyType>())
.add("value_type", primbench::name<ValueType>())
.add("probability", get_probability_name(Probability))
.add("cfg", config_name<Config>());
}
void run(primbench::state& state) override
{
const auto& stream = state.stream;
const auto& bytes = state.size;
const auto& seed = state.seed;
auto seeds = primbench::seeds<2>(seed);
size_t items = bytes / sizeof(KeyType);
std::vector<KeyType> input_keys_0;
std::vector<KeyType> input_keys_1;
std::vector<KeyType> input_keys_2;
if(is_tuning)
{
input_keys_0 = get_unique_input<KeyType>(items, 0.0f, seeds[0]);
input_keys_1 = get_unique_input<KeyType>(items, 0.5f, seeds[0]);
input_keys_2 = get_unique_input<KeyType>(items, 1.0f, seeds[0]);
}
else
{
input_keys_0 = get_unique_input<KeyType>(items, get_probability(Probability), seeds[0]);
}
const auto random_range = limit_random_range<ValueType>(-1000, 1000);
const auto input_values
= get_random_data<ValueType>(items, random_range.first, random_range.second, seeds[1]);
common::device_ptr<KeyType> d_keys_input_0(input_keys_0);
common::device_ptr<KeyType> d_keys_input_1;
common::device_ptr<KeyType> d_keys_input_2;
if(is_tuning)
{
d_keys_input_1.store(input_keys_1);
d_keys_input_2.store(input_keys_2);
}
common::device_ptr<ValueType> d_values_input(input_values);
common::device_ptr<KeyType> d_keys_output(items);
common::device_ptr<ValueType> d_values_output(items);
common::device_ptr<unsigned int> d_selected_count_output(1);
const auto dispatch = [&](void* d_temp_storage, size_t& temp_storage_size_bytes)
{
const auto dispatch_flags = [&](KeyType* d_keys_input)
{
HIP_CHECK(rocprim::unique_by_key<Config>(d_temp_storage,
temp_storage_size_bytes,
d_keys_input,
d_values_input.get(),
d_keys_output.get(),
d_values_output.get(),
d_selected_count_output.get(),
items,
rocprim::equal_to<KeyType>(),
stream));
};
dispatch_flags(d_keys_input_0.get());
if(is_tuning)
{
dispatch_flags(d_keys_input_1.get());
dispatch_flags(d_keys_input_2.get());
}
};
// Allocate temporary storage memory
size_t temp_storage_size_bytes{};
dispatch(nullptr, temp_storage_size_bytes);
common::device_ptr<void> d_temp_storage(temp_storage_size_bytes);
state.set_items(items);
state.add_reads<KeyType>(items);
state.add_reads<ValueType>(items);
state.run([&] { dispatch(d_temp_storage.get(), temp_storage_size_bytes); });
}
static constexpr bool is_tuning = Probability == select_probability::tuning;
};
#ifdef BENCHMARK_CONFIG_TUNING
template<typename Config, typename KeyType, typename ValueType>
struct create_benchmark
{
static constexpr unsigned int block_size = Config().kernel_config.block_size;
static constexpr unsigned int items_per_thread = Config().kernel_config.items_per_thread;
static constexpr unsigned int max_shared_memory = TUNING_SHARED_MEMORY_MAX;
static constexpr unsigned int max_size_per_element = sizeof(KeyType) + sizeof(ValueType);
static constexpr unsigned int max_items_per_thread
= max_shared_memory / (block_size * max_size_per_element);
void operator()(std::vector<std::unique_ptr<primbench::benchmark_interface>>& storage)
{
storage.emplace_back(
std::make_unique<device_select_unique_by_key_benchmark<KeyType, ValueType, Config>>());
if(items_per_thread <= max_items_per_thread)
{
storage.emplace_back(
std::make_unique<
device_select_predicated_flag_benchmark<KeyType, ValueType, Config>>());
}
}
};
template<typename Config, typename KeyType>
struct create_benchmark<Config, KeyType, rocprim::empty_type>
{
void operator()(std::vector<std::unique_ptr<primbench::benchmark_interface>>& storage)
{
storage.emplace_back(std::make_unique<device_select_flag_benchmark<KeyType, Config>>());
storage.emplace_back(
std::make_unique<device_select_predicate_benchmark<KeyType, Config>>());
storage.emplace_back(std::make_unique<device_select_unique_benchmark<KeyType, Config>>());
}
};
template<typename KeyType, typename ValueType, int BlockSize>
struct device_select_benchmark_generator
{
template<int ItemsPerThread>
struct create_ipt
{
void operator()(std::vector<std::unique_ptr<primbench::benchmark_interface>>& storage)
{
using config = rocprim::select_config<BlockSize, ItemsPerThread>;
create_benchmark<config, KeyType, ValueType>{}(storage);
}
};
static void create(std::vector<std::unique_ptr<primbench::benchmark_interface>>& storage)
{
static constexpr int max_items_per_thread
= std::min(64 / std::max(sizeof(KeyType), sizeof(ValueType)), size_t{32});
static_for_each<make_index_range<int, 4, max_items_per_thread>, create_ipt>(storage);
}
};
#endif // BENCHMARK_CONFIG_TUNING