Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move some content of surf_interface to a new math_utils.h
[simgrid.git] / src / kernel / resource / models / network_constant.cpp
1 /* Copyright (c) 2013-2023. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include <simgrid/kernel/routing/NetZoneImpl.hpp>
7 #include <simgrid/s4u/Engine.hpp>
8
9 #include "simgrid/math_utils.h"
10 #include "src/kernel/EngineImpl.hpp"
11 #include "src/kernel/resource/models/network_constant.hpp"
12 #include "src/simgrid/module.hpp"
13
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_network);
15
16 /*********
17  * Model *
18  *********/
19 SIMGRID_REGISTER_NETWORK_MODEL(
20     Constant,
21     "Simplistic network model where all communication take a constant time (one second). This model "
22     "provides the lowest realism, but is (marginally) faster. It is mostly useful when studying theoretical "
23     "distributed algorithms where the network is usually abstracted away.",
24     []() {
25       auto net_model = std::make_shared<simgrid::kernel::resource::NetworkConstantModel>("Network_Constant");
26       auto* engine   = simgrid::kernel::EngineImpl::get_instance();
27       engine->add_model(net_model);
28       engine->get_netzone_root()->set_network_model(net_model);
29     });
30
31 namespace simgrid::kernel::resource {
32
33 StandardLinkImpl* NetworkConstantModel::create_link(const std::string& name, const std::vector<double>& /*bandwidth*/)
34 {
35   xbt_die("Refusing to create the link %s: there is no link in the Constant network model. "
36           "Please remove any link from your platform (and switch to routing='None')",
37           name.c_str());
38   return nullptr;
39 }
40
41 StandardLinkImpl* NetworkConstantModel::create_wifi_link(const std::string& name, const std::vector<double>& bandwidths)
42 {
43   return create_link(name, bandwidths);
44 }
45
46 double NetworkConstantModel::next_occurring_event(double /*now*/)
47 {
48   double min = -1.0;
49   for (Action const& action : *get_started_action_set()) {
50     const auto& net_action = static_cast<const NetworkConstantAction&>(action);
51     if (net_action.latency_ > 0 && (min < 0 || net_action.latency_ < min))
52       min = net_action.latency_;
53   }
54   return min;
55 }
56
57 void NetworkConstantModel::update_actions_state(double /*now*/, double delta)
58 {
59   for (auto it = std::begin(*get_started_action_set()); it != std::end(*get_started_action_set());) {
60     auto& action = static_cast<NetworkConstantAction&>(*it);
61     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
62     if (action.latency_ > 0) {
63       if (action.latency_ > delta) {
64         double_update(&action.latency_, delta, sg_precision_timing);
65       } else {
66         action.latency_ = 0.0;
67       }
68     }
69     action.update_remains(action.get_cost() * delta / get_latency_factor());
70     action.update_max_duration(delta);
71
72     if ((action.get_remains_no_update() <= 0) ||
73         ((action.get_max_duration() != NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
74       action.finish(Action::State::FINISHED);
75     }
76   }
77 }
78
79 Action* NetworkConstantModel::communicate(s4u::Host* src, s4u::Host* dst, double size, double /*rate*/,
80                                           bool /*streamed*/)
81 {
82   return (new NetworkConstantAction(this, *src, *dst, size));
83 }
84
85 /**********
86  * Action *
87  **********/
88 NetworkConstantAction::NetworkConstantAction(NetworkConstantModel* model_, s4u::Host& src, s4u::Host& dst, double size)
89     : NetworkAction(model_, src, dst, size, false)
90 {
91   latency_ = model_->get_latency_factor();
92   if (latency_ <= 0.0)
93     set_state(Action::State::FINISHED);
94 }
95
96 void NetworkConstantAction::update_remains_lazy(double /*now*/)
97 {
98   THROW_IMPOSSIBLE;
99 }
100
101 } // namespace simgrid::kernel::resource