1 /* Copyright (c) 2010-2021. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 /* This example shows how to simulate a non-linear resource sharing for
10 #include <simgrid/s4u.hpp>
12 namespace sg4 = simgrid::s4u;
14 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_network_nonlinear, "Messages specific for this s4u example");
16 /*************************************************************************************************/
19 double computation_amount = sg4::this_actor::get_host()->get_speed();
21 std::vector<sg4::ExecPtr> tasks;
24 "Execute %d tasks of %g flops, should take %d second in a CPU without degradation. It will take the double here.",
25 n_task, computation_amount, n_task);
26 for (int i = 0; i < n_task; i++) {
27 tasks.emplace_back(sg4::this_actor::exec_async(computation_amount));
29 XBT_INFO("Waiting for all tasks to be done!");
30 for (auto& task : tasks)
33 XBT_INFO("Finished executing. Goodbye now!");
35 /*************************************************************************************************/
36 /** @brief Non-linear resource sharing for CPU */
37 static double cpu_nonlinear(const sg4::Host* host, double capacity, int n)
39 /* emulates a degradation in link according to the number of flows
40 * totally unrealistic but for learning purposes */
41 capacity = n > 1 ? capacity / 2 : capacity;
42 XBT_INFO("Host %s, %d concurrent tasks, new capacity %lf", host->get_cname(), n, capacity);
46 /** @brief Create a simple 1-host platform */
47 static void load_platform()
49 auto* zone = sg4::create_empty_zone("Zone1");
50 auto* runner_host = zone->create_host("runner", 1e6);
51 runner_host->set_sharing_policy(sg4::Host::SharingPolicy::NONLINEAR,
52 std::bind(&cpu_nonlinear, runner_host, std::placeholders::_1, std::placeholders::_2));
56 /* create actor runner */
57 sg4::Actor::create("runner", runner_host, runner);
60 /*************************************************************************************************/
61 int main(int argc, char* argv[])
63 sg4::Engine e(&argc, argv);
68 /* runs the simulation */