Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 155 additions & 6 deletions libs/core/schedulers/tests/unit/scheduler_tuning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

// The tunable settings of the rules of the local-priority-tuned-fifo scheduler are
// registered with the values they were configured with, a tuner selected with
// hpx.scheduler_tuner sees them before the workers start, and the values are
// clamped to their ranges.
// The tunable settings of the rules of the local-priority-tuned-fifo
// scheduler are registered with the values they were configured with, a tuner
// selected with hpx.scheduler_tuner sees them before the workers start, and
// the values are clamped to their ranges.

#include <hpx/config.hpp>
#include <hpx/init.hpp>
Expand All @@ -18,10 +18,14 @@
#include <hpx/modules/threading_base.hpp>

#include <atomic>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <memory>
#include <string>
#include <thread>
#include <vector>

namespace policies = hpx::threads::policies;

Expand Down Expand Up @@ -51,8 +55,8 @@ policies::scheduler_tuning_parameters& get_parameters()
return scheduler->get_tuning_parameters();
}

// the tested settings belong to the local-priority-tuned-fifo scheduler, make sure
// it is used regardless of --hpx:queuing
// the tested settings belong to the local-priority-tuned-fifo scheduler, make
// sure it is used regardless of --hpx:queuing
void use_tuned_scheduler(hpx::local::init_params& init_args)
{
init_args.rp_callback = [](hpx::resource::partitioner& rp,
Expand Down Expand Up @@ -170,10 +174,155 @@ void test_registry()
HPX_TEST(caught);
}

void test_float_clamping()
{
policies::scheduler_tuning_parameters parameters(2);
std::size_t const time = parameters.add("time", 0.5f, 100.0f, true, 1.5f);

// Below min_value
parameters.set(0, time, -10.0f);
HPX_TEST_EQ(parameters.get<float>(0, time), 0.5f);
HPX_TEST(std::isfinite(parameters.get<float>(0, time)));

// Above max_value
parameters.set(0, time, 200.0f);
HPX_TEST_EQ(parameters.get<float>(0, time), 100.0f);
HPX_TEST(std::isfinite(parameters.get<float>(0, time)));

// Negative infinity
parameters.set(0, time, -std::numeric_limits<float>::infinity());
HPX_TEST_EQ(parameters.get<float>(0, time), 0.5f);
HPX_TEST(std::isfinite(parameters.get<float>(0, time)));

// Positive infinity
parameters.set(0, time, std::numeric_limits<float>::infinity());
HPX_TEST_EQ(parameters.get<float>(0, time), 100.0f);
HPX_TEST(std::isfinite(parameters.get<float>(0, time)));

// Quiet NaN
parameters.set(0, time, std::numeric_limits<float>::quiet_NaN());
HPX_TEST_EQ(parameters.get<float>(0, time), 0.5f);
HPX_TEST(std::isfinite(parameters.get<float>(0, time)));

// Signaling NaN
if (std::numeric_limits<float>::has_signaling_NaN)
{
parameters.set(0, time, std::numeric_limits<float>::signaling_NaN());
HPX_TEST_EQ(parameters.get<float>(0, time), 0.5f);
HPX_TEST(std::isfinite(parameters.get<float>(0, time)));
}

// set_all with NaN
parameters.set_all(time, std::numeric_limits<float>::quiet_NaN());
HPX_TEST_EQ(parameters.get<float>(0, time), 0.5f);
HPX_TEST_EQ(parameters.get<float>(1, time), 0.5f);
HPX_TEST(std::isfinite(parameters.get<float>(0, time)));
HPX_TEST(std::isfinite(parameters.get<float>(1, time)));

// add() initialized with NaN
std::size_t const nan_init = parameters.add("nan_init", 1.0f, 10.0f, false,
std::numeric_limits<float>::quiet_NaN());
HPX_TEST_EQ(parameters.get<float>(nan_init), 1.0f);
HPX_TEST(std::isfinite(parameters.get<float>(nan_init)));
}

void test_add_setting_errors()
{
policies::scheduler_tuning_parameters parameters(1);
parameters.add("param", 0u, 10u, false, 0u);

// Registering duplicate name throws bad_parameter
bool caught_dup = false;
try
{
parameters.add("param", 0u, 10u, false, 0u);
}
catch (hpx::exception const& e)
{
caught_dup = (e.get_error() == hpx::error::bad_parameter);
}
HPX_TEST(caught_dup);

// Registering more than max_parameters (16) settings throws bad_parameter
policies::scheduler_tuning_parameters p(1);
static constexpr char const* names[] = {"s0", "s1", "s2", "s3", "s4", "s5",
"s6", "s7", "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15"};
for (std::size_t i = 0;
i != policies::scheduler_tuning_parameters::max_parameters; ++i)
{
p.add(names[i], 0u, 100u, false, 0u);
}
HPX_TEST_EQ(
p.size(), policies::scheduler_tuning_parameters::max_parameters);

bool caught_overflow = false;
try
{
p.add("setting_overflow", 0u, 100u, false, 0u);
}
catch (hpx::exception const& e)
{
caught_overflow = (e.get_error() == hpx::error::bad_parameter);
}
HPX_TEST(caught_overflow);
}

void test_concurrent_set_all_get()
{
policies::scheduler_tuning_parameters parameters(4);
std::size_t const p_int = parameters.add("conc_int", 0u, 1000u, false, 0u);
std::size_t const p_real =
parameters.add("conc_real", 0.0f, 100.0f, false, 0.0f);

std::atomic<bool> stop{false};
std::vector<std::thread> readers;
readers.reserve(4);

for (std::size_t w = 0; w != 4; ++w)
{
readers.emplace_back([&parameters, w, p_int, p_real, &stop] {
while (!stop.load(std::memory_order_relaxed))
{
auto const vi = parameters.get<std::uint32_t>(w, p_int);
auto const vr = parameters.get<float>(w, p_real);
HPX_TEST_LTE(vi, 1000u);
HPX_TEST_LTE(0.0f, vr);
HPX_TEST_LTE(vr, 100.0f);
HPX_TEST(std::isfinite(vr));

auto const vi_pool = parameters.get<std::uint32_t>(p_int);
auto const vr_pool = parameters.get<float>(p_real);
HPX_TEST_LTE(vi_pool, 1000u);
HPX_TEST_LTE(0.0f, vr_pool);
HPX_TEST_LTE(vr_pool, 100.0f);
HPX_TEST(std::isfinite(vr_pool));
}
});
}

std::thread writer([&parameters, p_int, p_real, &stop] {
for (std::size_t i = 0; i != 1000; ++i)
{
parameters.set_all(p_int, static_cast<std::uint32_t>(i % 1001));
parameters.set_all(p_real, static_cast<float>(i % 101));
}
stop.store(true, std::memory_order_relaxed);
});

writer.join();
for (auto& r : readers)
{
r.join();
}
}

int main(int argc, char* argv[])
{
test_storage();
test_registry();
test_float_clamping();
test_add_setting_errors();
test_concurrent_set_all_get();

{
hpx::local::init_params init_args;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
#include <algorithm>
#include <array>
#include <atomic>
#include <cmath>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include <string_view>
#include <type_traits>
#include <vector>

#include <hpx/config/warnings_prefix.hpp>
Expand Down Expand Up @@ -112,6 +114,17 @@ namespace hpx::threads::policies {
[[nodiscard]] constexpr T clamped(T const value) const noexcept
{
HPX_ASSERT(type == tuning_parameter_type_of<T>);
if constexpr (std::same_as<T, float>)
{
if (std::isnan(value))
{
// Fall back to min_value if value is NaN (e.g. from
// division by zero in a dynamic tuner), as default_value
// is not stored in tuning_parameter_info and min_value is a
// safe conservative fallback.
return min_value.as<float>();
}
}
return std::clamp(value, min_value.as<T>(), max_value.as<T>());
}
};
Expand Down
Loading