From c70c4fc96f1007cc65e32f813ea9afa433a6650e Mon Sep 17 00:00:00 2001 From: Bruno Donassolo Date: Mon, 9 Aug 2021 15:46:24 +0200 Subject: [PATCH] CPU factors: dynamic factors for CPU - Allows the use of user's callback to change the speed at the update_remaining. - Add a simple example. - Minor improv. for disk - Update changelog - Available only for Cas01 (not ptask or TI) --- ChangeLog | 6 ++ MANIFEST.in | 4 ++ examples/cpp/CMakeLists.txt | 2 +- .../exec-cpu-factors/s4u-exec-cpu-factors.cpp | 67 +++++++++++++++++++ .../s4u-exec-cpu-factors.tesh | 6 ++ .../s4u-exec-cpu-nonlinear.cpp | 4 +- include/simgrid/s4u/Host.hpp | 13 ++++ src/kernel/activity/IoImpl.cpp | 4 -- src/s4u/s4u_Host.cpp | 6 ++ src/surf/cpu_cas01.cpp | 9 +++ src/surf/cpu_cas01.hpp | 4 ++ src/surf/cpu_interface.hpp | 6 ++ src/surf/disk_s19.cpp | 4 ++ 13 files changed, 128 insertions(+), 7 deletions(-) create mode 100644 examples/cpp/exec-cpu-factors/s4u-exec-cpu-factors.cpp create mode 100644 examples/cpp/exec-cpu-factors/s4u-exec-cpu-factors.tesh diff --git a/ChangeLog b/ChangeLog index 17aa1df1f3..b4902624b2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -17,6 +17,10 @@ New features: - Disk: examples/cpp/io-degradation - Link: examples/cpp/network-nonlinear - CPU: examples/cpp/exec-cpu-nonlinear + - Dynamic factors for CPU and disk: similarly to dynamic network factors, + allows the user to set a callback which can affect the progress of activities + (multiplicative factor applied when updating the amount of work remaining). + - Example: examples/cpp/exec-cpu-factors S4U: - New: s4u::Disk::set_sharing_policy() and s4u::Host::set_sharing_policy(). @@ -25,6 +29,8 @@ S4U: Documentation: * New section "Release Notes" documenting recent and current developments. + * New section "Modeling I/O: the realistic way" presenting how to properly + model disks in SimGrid. ---------------------------------------------------------------------------- diff --git a/MANIFEST.in b/MANIFEST.in index fb0bf5c6f5..e2dda2b2a1 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -234,6 +234,8 @@ include examples/cpp/exec-async/s4u-exec-async.cpp include examples/cpp/exec-async/s4u-exec-async.tesh include examples/cpp/exec-basic/s4u-exec-basic.cpp include examples/cpp/exec-basic/s4u-exec-basic.tesh +include examples/cpp/exec-cpu-factors/s4u-exec-cpu-factors.cpp +include examples/cpp/exec-cpu-factors/s4u-exec-cpu-factors.tesh include examples/cpp/exec-cpu-nonlinear/s4u-exec-cpu-nonlinear.cpp include examples/cpp/exec-cpu-nonlinear/s4u-exec-cpu-nonlinear.tesh include examples/cpp/exec-dependent/s4u-exec-dependent.cpp @@ -1692,6 +1694,8 @@ include teshsuite/smpi/pt2pt-pingpong/TI_output.tesh include teshsuite/smpi/pt2pt-pingpong/broken_hostfiles.tesh include teshsuite/smpi/pt2pt-pingpong/pt2pt-pingpong.c include teshsuite/smpi/pt2pt-pingpong/pt2pt-pingpong.tesh +include teshsuite/smpi/replay-ti-colls/replay-ti-colls.c +include teshsuite/smpi/replay-ti-colls/replay-ti-colls.tesh include teshsuite/smpi/timers/timers.c include teshsuite/smpi/timers/timers.tesh include teshsuite/smpi/topo-cart-sub/topo-cart-sub.c diff --git a/examples/cpp/CMakeLists.txt b/examples/cpp/CMakeLists.txt index ac25009336..960123b028 100644 --- a/examples/cpp/CMakeLists.txt +++ b/examples/cpp/CMakeLists.txt @@ -70,7 +70,7 @@ foreach (example actor-create actor-daemon actor-exiting actor-join actor-kill energy-exec energy-boot energy-link energy-vm energy-exec-ptask energy-wifi engine-filtering exec-async exec-basic exec-dvfs exec-remote exec-waitany exec-waitfor exec-dependent exec-unassigned - exec-ptask-multicore exec-cpu-nonlinear + exec-ptask-multicore exec-cpu-nonlinear exec-cpu-factors maestro-set mc-bugged1 mc-bugged2 mc-electric-fence mc-failing-assert network-wifi diff --git a/examples/cpp/exec-cpu-factors/s4u-exec-cpu-factors.cpp b/examples/cpp/exec-cpu-factors/s4u-exec-cpu-factors.cpp new file mode 100644 index 0000000000..820614bbc9 --- /dev/null +++ b/examples/cpp/exec-cpu-factors/s4u-exec-cpu-factors.cpp @@ -0,0 +1,67 @@ +/* Copyright (c) 2010-2021. The SimGrid Team. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +/* This example shows how to simulate variability for CPUs, using multiplicative factors + */ + +#include + +namespace sg4 = simgrid::s4u; + +XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example"); + +/*************************************************************************************************/ +static void runner() +{ + double computation_amount = sg4::this_actor::get_host()->get_speed(); + + XBT_INFO("Executing 1 tasks of %g flops, should take 1 second.", computation_amount); + sg4::this_actor::execute(computation_amount); + XBT_INFO("Executing 1 tasks of %g flops, it would take .001s without factor. It'll take .002s", + computation_amount / 10); + sg4::this_actor::execute(computation_amount / 1000); + + XBT_INFO("Finished executing. Goodbye now!"); +} +/*************************************************************************************************/ +/** @brief Variability for CPU */ +static double cpu_variability(const sg4::Host* host, double flops) +{ + /* creates variability for tasks smaller than 1% of CPU power. + * unrealistic, for learning purposes */ + double factor = 1.0; + double speed = host->get_speed(); + if (flops < speed / 100) { + factor = 0.5; + } + XBT_INFO("Host %s, task with %lf flops, new factor %lf", host->get_cname(), flops, factor); + return factor; +} + +/** @brief Create a simple 1-host platform */ +static void load_platform() +{ + auto* zone = sg4::create_empty_zone("Zone1"); + auto* runner_host = zone->create_host("runner", 1e6); + runner_host->set_factor_cb(std::bind(&cpu_variability, runner_host, std::placeholders::_1))->seal(); + zone->seal(); + + /* create actor runner */ + sg4::Actor::create("runner", runner_host, runner); +} + +/*************************************************************************************************/ +int main(int argc, char* argv[]) +{ + sg4::Engine e(&argc, argv); + + /* create platform */ + load_platform(); + + /* runs the simulation */ + e.run(); + + return 0; +} diff --git a/examples/cpp/exec-cpu-factors/s4u-exec-cpu-factors.tesh b/examples/cpp/exec-cpu-factors/s4u-exec-cpu-factors.tesh new file mode 100644 index 0000000000..9fa8d57712 --- /dev/null +++ b/examples/cpp/exec-cpu-factors/s4u-exec-cpu-factors.tesh @@ -0,0 +1,6 @@ +$ ${bindir:=.}/s4u-exec-cpu-factors "--log=root.fmt:[%10.6r]%e(%i:%a@%h)%e%m%n" +> [ 0.000000] (1:runner@runner) Executing 1 tasks of 1e+06 flops, should take 1 second. +> [ 0.000000] (0:maestro@) Host runner, task with 1000000.000000 flops, new factor 1.000000 +> [ 1.000000] (1:runner@runner) Executing 1 tasks of 100000 flops, it would take .001s without factor. It'll take .002s +> [ 1.000000] (0:maestro@) Host runner, task with 1000.000000 flops, new factor 0.500000 +> [ 1.002000] (1:runner@runner) Finished executing. Goodbye now! diff --git a/examples/cpp/exec-cpu-nonlinear/s4u-exec-cpu-nonlinear.cpp b/examples/cpp/exec-cpu-nonlinear/s4u-exec-cpu-nonlinear.cpp index 8c5280d21f..9e980cdd88 100644 --- a/examples/cpp/exec-cpu-nonlinear/s4u-exec-cpu-nonlinear.cpp +++ b/examples/cpp/exec-cpu-nonlinear/s4u-exec-cpu-nonlinear.cpp @@ -11,7 +11,7 @@ namespace sg4 = simgrid::s4u; -XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_network_nonlinear, "Messages specific for this s4u example"); +XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example"); /*************************************************************************************************/ static void runner() @@ -36,7 +36,7 @@ static void runner() /** @brief Non-linear resource sharing for CPU */ static double cpu_nonlinear(const sg4::Host* host, double capacity, int n) { - /* emulates a degradation in link according to the number of flows + /* emulates a degradation in CPU according to the number of tasks * totally unrealistic but for learning purposes */ capacity = n > 1 ? capacity / 2 : capacity; XBT_INFO("Host %s, %d concurrent tasks, new capacity %lf", host->get_cname(), n, capacity); diff --git a/include/simgrid/s4u/Host.hpp b/include/simgrid/s4u/Host.hpp index 42807933a0..901b4f822b 100644 --- a/include/simgrid/s4u/Host.hpp +++ b/include/simgrid/s4u/Host.hpp @@ -89,6 +89,19 @@ public: Host* set_cpu(kernel::resource::CpuImpl* cpu); kernel::resource::CpuImpl* get_cpu() const { return pimpl_cpu_; } kernel::routing::NetPoint* get_netpoint() const { return pimpl_netpoint_; } + /** + * @brief Callback to set CPU factor + * + * This callback offers a flexible way to create variability in CPU executions + * + * @param flops Execution size in flops + * @return Multiply factor + */ + using CpuFactorCb = double(sg_size_t flops); + /** + * @brief Configure the factor callback to the CPU associated to this host + */ + Host* set_factor_cb(const std::function& cb); size_t get_actor_count() const; std::vector get_all_actors() const; diff --git a/src/kernel/activity/IoImpl.cpp b/src/kernel/activity/IoImpl.cpp index bdef78f4d7..4f692b2990 100644 --- a/src/kernel/activity/IoImpl.cpp +++ b/src/kernel/activity/IoImpl.cpp @@ -59,10 +59,6 @@ IoImpl* IoImpl::start() surf_action_ = disk_->get_host()->get_netpoint()->get_englobing_zone()->get_disk_model()->io_start(disk_, size_, type_); surf_action_->set_activity(this); - const auto& factor_cb = disk_->get_factor_cb(); - if (factor_cb) { // handling disk variability - surf_action_->set_rate_factor(factor_cb(size_, type_)); - } XBT_DEBUG("Create IO synchro %p %s", this, get_cname()); diff --git a/src/s4u/s4u_Host.cpp b/src/s4u/s4u_Host.cpp index 575dfad6ba..fb079bf84a 100644 --- a/src/s4u/s4u_Host.cpp +++ b/src/s4u/s4u_Host.cpp @@ -326,6 +326,12 @@ int Host::get_pstate() const return this->pimpl_cpu_->get_pstate(); } +Host* Host::set_factor_cb(const std::function& cb) +{ + kernel::actor::simcall([this, &cb] { pimpl_cpu_->set_factor_cb(cb); }); + return this; +} + Host* Host::set_coordinates(const std::string& coords) { if (not coords.empty()) diff --git a/src/surf/cpu_cas01.cpp b/src/surf/cpu_cas01.cpp index 88aab666be..b3b215b3de 100644 --- a/src/surf/cpu_cas01.cpp +++ b/src/surf/cpu_cas01.cpp @@ -154,6 +154,9 @@ CpuAction* CpuCas01::execution_start(double size, int requested_cores, double us if (user_bound > 0 && user_bound < action->get_bound()) { get_model()->get_maxmin_system()->update_variable_bound(action->get_variable(), user_bound); } + if (factor_cb_) { + action->set_rate_factor(factor_cb_(size)); + } return action; } @@ -184,6 +187,12 @@ CpuAction* CpuCas01::sleep(double duration) return action; } +void CpuCas01::set_factor_cb(const std::function& cb) +{ + xbt_assert(not is_sealed(), "Cannot set CPU factor callback in an already sealed CPU(%s)", get_cname()); + factor_cb_ = cb; +} + /********** * Action * **********/ diff --git a/src/surf/cpu_cas01.hpp b/src/surf/cpu_cas01.hpp index 61b588bd4b..ed193cb666 100644 --- a/src/surf/cpu_cas01.hpp +++ b/src/surf/cpu_cas01.hpp @@ -47,11 +47,15 @@ public: CpuAction* execution_start(double size, double user_bound) override; CpuAction* execution_start(double size, int requested_cores, double user_bound) override; CpuAction* sleep(double duration) override; + void set_factor_cb(const std::function& cb) override; bool is_used() const override; protected: void on_speed_change() override; + +private: + std::function factor_cb_ = {}; }; /********** diff --git a/src/surf/cpu_interface.hpp b/src/surf/cpu_interface.hpp index 10c1d037fd..a8b3440a15 100644 --- a/src/surf/cpu_interface.hpp +++ b/src/surf/cpu_interface.hpp @@ -119,6 +119,12 @@ public: void set_sharing_policy(s4u::Host::SharingPolicy policy, const s4u::NonLinearResourceCb& cb); s4u::Host::SharingPolicy get_sharing_policy() const; + /** + * @brief Sets factor callback + * Implemented only for cas01 + */ + virtual void set_factor_cb(const std::function& cb) { THROW_UNIMPLEMENTED; } + /** * @brief Execute some quantity of computation * diff --git a/src/surf/disk_s19.cpp b/src/surf/disk_s19.cpp index 86028dab82..16860851c7 100644 --- a/src/surf/disk_s19.cpp +++ b/src/surf/disk_s19.cpp @@ -65,6 +65,10 @@ DiskAction* DiskS19Model::io_start(const DiskImpl* disk, sg_size_t size, s4u::Io default: THROW_UNIMPLEMENTED; } + const auto& factor_cb = disk->get_factor_cb(); + if (factor_cb) { // handling disk variability + action->set_rate_factor(factor_cb(size, type)); + } return action; } -- 2.20.1