Logo AND Algorithmique Numérique Distribuée

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