Allows the use of non-linear constraints for CPU resources.
Add API in s4u::Host::set_sharing_policy
- LINEAR(default, shared resources) or NONLINEAR
Example: examples/cpp/exec-cpu-nonlinear
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-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
include examples/cpp/exec-dependent/s4u-exec-dependent.tesh
include examples/cpp/exec-dvfs/s4u-exec-dvfs.cpp
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-ptask-multicore exec-cpu-nonlinear
maestro-set
mc-bugged1 mc-bugged2 mc-electric-fence mc-failing-assert
network-wifi
--- /dev/null
+/* 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 a non-linear resource sharing for
+ * CPUs.
+ */
+
+#include <simgrid/s4u.hpp>
+
+namespace sg4 = simgrid::s4u;
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_network_nonlinear, "Messages specific for this s4u example");
+
+/*************************************************************************************************/
+static void runner()
+{
+ double computation_amount = sg4::this_actor::get_host()->get_speed();
+ int n_task = 10;
+ std::vector<sg4::ExecPtr> tasks;
+
+ XBT_INFO(
+ "Execute %d tasks of %g flops, should take %d second in a CPU without degradation. It will take the double here.",
+ n_task, computation_amount, n_task);
+ for (int i = 0; i < n_task; i++) {
+ tasks.emplace_back(sg4::this_actor::exec_async(computation_amount));
+ }
+ XBT_INFO("Waiting for all tasks to be done!");
+ for (auto& task : tasks)
+ task->wait();
+
+ XBT_INFO("Finished executing. Goodbye now!");
+}
+/*************************************************************************************************/
+/** @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
+ * 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);
+ return capacity;
+}
+
+/** @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_sharing_policy(sg4::Host::SharingPolicy::NONLINEAR,
+ std::bind(&cpu_nonlinear, runner_host, std::placeholders::_1, std::placeholders::_2));
+ runner_host->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;
+}
--- /dev/null
+$ ${bindir:=.}/s4u-exec-cpu-nonlinear "--log=root.fmt:[%10.6r]%e(%i:%a@%h)%e%m%n"
+> [ 0.000000] (1:runner@runner) Execute 10 tasks of 1e+06 flops, should take 10 second in a CPU without degradation. It will take the double here.
+> [ 0.000000] (1:runner@runner) Waiting for all tasks to be done!
+> [ 0.000000] (0:maestro@) Host runner, 10 concurrent tasks, new capacity 500000.000000
+> [ 20.000000] (1:runner@runner) Finished executing. Goodbye now!
int get_core_count() const;
Host* set_core_count(int core_count);
+ enum class SharingPolicy { NONLINEAR = 1, LINEAR = 0 };
+ /**
+ * @brief Describes how the CPU is shared between concurrent tasks
+ *
+ * @param policy Sharing policy
+ * @param cb Callback for NONLINEAR policies
+ */
+ Host* set_sharing_policy(SharingPolicy policy, const s4u::NonLinearResourceCb& cb = {});
+ SharingPolicy get_sharing_policy() const;
+
/** Returns the current computation load (in flops per second)
*
* The external load (coming from an availability trace) is not taken in account.
return this->pimpl_cpu_->get_speed_ratio();
}
+Host* Host::set_sharing_policy(SharingPolicy policy, const s4u::NonLinearResourceCb& cb)
+{
+ kernel::actor::simcall([this, policy, &cb] { pimpl_cpu_->set_sharing_policy(policy, cb); });
+ return this;
+}
+
+Host::SharingPolicy Host::get_sharing_policy() const
+{
+ return this->pimpl_cpu_->get_sharing_policy();
+}
+
int Host::get_core_count() const
{
return this->pimpl_cpu_->get_core_count();
return this;
}
+void CpuImpl::apply_sharing_policy_cfg() const
+{
+ if (!get_constraint())
+ return;
+
+ kernel::lmm::Constraint::SharingPolicy lmm_policy = kernel::lmm::Constraint::SharingPolicy::SHARED;
+ if (sharing_policy_ == s4u::Host::SharingPolicy::NONLINEAR)
+ lmm_policy = kernel::lmm::Constraint::SharingPolicy::NONLINEAR;
+
+ get_constraint()->set_sharing_policy(lmm_policy, sharing_policy_cb_);
+}
+
+void CpuImpl::set_sharing_policy(s4u::Host::SharingPolicy policy, const s4u::NonLinearResourceCb& cb)
+{
+ xbt_assert(dynamic_cast<CpuTiModel*>(get_model()) == nullptr, "Cannot change sharing policy with CPU:TI model");
+ sharing_policy_ = policy;
+ sharing_policy_cb_ = cb;
+ apply_sharing_policy_cfg();
+}
+
+s4u::Host::SharingPolicy CpuImpl::get_sharing_policy() const
+{
+ return sharing_policy_;
+}
+
int CpuImpl::get_core_count()
{
return core_count_;
lmm::System* lmm = get_model()->get_maxmin_system();
if (dynamic_cast<CpuTiModel*>(get_model()) == nullptr)
this->set_constraint(lmm->constraint_new(this, core_count_ * speed_per_pstate_.front()));
+ apply_sharing_policy_cfg();
Resource::seal();
}
int pstate_ = 0; /*< Current pstate (index in the speed_per_pstate_)*/
std::vector<double> speed_per_pstate_; /*< List of supported CPU capacities (pstate related). Not 'const' because VCPU
get modified on migration */
+ s4u::Host::SharingPolicy sharing_policy_ = s4u::Host::SharingPolicy::LINEAR;
+ s4u::NonLinearResourceCb sharing_policy_cb_;
+
+ void apply_sharing_policy_cfg() const;
public:
/**
*/
CpuImpl* set_pstate_speed(const std::vector<double>& speed_per_state);
+ void set_sharing_policy(s4u::Host::SharingPolicy policy, const s4u::NonLinearResourceCb& cb);
+ s4u::Host::SharingPolicy get_sharing_policy() const;
+
/**
* @brief Execute some quantity of computation
*