Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
change way WIFI are create + make sharing_policy optional
[simgrid.git] / src / surf / network_constant.cpp
1 /* Copyright (c) 2013-2021. 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 "network_constant.hpp"
7 #include "simgrid/kernel/routing/NetZoneImpl.hpp"
8 #include "simgrid/s4u/Engine.hpp"
9 #include "src/kernel/EngineImpl.hpp"
10 #include "src/surf/surf_interface.hpp"
11 #include "surf/surf.hpp"
12
13 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_network);
14
15 /*********
16  * Model *
17  *********/
18 void surf_network_model_init_Constant()
19 {
20   auto net_model = std::make_shared<simgrid::kernel::resource::NetworkConstantModel>("Network_Constant");
21   simgrid::kernel::EngineImpl::get_instance()->add_model(net_model);
22   simgrid::s4u::Engine::get_instance()->get_netzone_root()->get_impl()->set_network_model(net_model);
23 }
24
25 namespace simgrid {
26 namespace kernel {
27 namespace resource {
28
29 LinkImpl* NetworkConstantModel::create_link(const std::string& name, const std::vector<double>& /*bandwidth*/)
30 {
31   xbt_die("Refusing to create the link %s: there is no link in the Constant network model. "
32           "Please remove any link from your platform (and switch to routing='None')",
33           name.c_str());
34   return nullptr;
35 }
36
37 LinkImpl* NetworkConstantModel::create_wifi_link(const std::string& name, const std::vector<double>& bandwidths)
38 {
39   return create_link(name, bandwidths);
40 }
41
42 double NetworkConstantModel::next_occurring_event(double /*now*/)
43 {
44   double min = -1.0;
45   for (Action const& action : *get_started_action_set()) {
46     const auto& net_action = static_cast<const NetworkConstantAction&>(action);
47     if (net_action.latency_ > 0 && (min < 0 || net_action.latency_ < min))
48       min = net_action.latency_;
49   }
50   return min;
51 }
52
53 void NetworkConstantModel::update_actions_state(double /*now*/, double delta)
54 {
55   for (auto it = std::begin(*get_started_action_set()); it != std::end(*get_started_action_set());) {
56     auto& action = static_cast<NetworkConstantAction&>(*it);
57     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
58     if (action.latency_ > 0) {
59       if (action.latency_ > delta) {
60         double_update(&action.latency_, delta, sg_surf_precision);
61       } else {
62         action.latency_ = 0.0;
63       }
64     }
65     action.update_remains(action.get_cost() * delta / sg_latency_factor);
66     action.update_max_duration(delta);
67
68     if ((action.get_remains_no_update() <= 0) ||
69         ((action.get_max_duration() != NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
70       action.finish(Action::State::FINISHED);
71     }
72   }
73 }
74
75 Action* NetworkConstantModel::communicate(s4u::Host* src, s4u::Host* dst, double size, double /*rate*/)
76 {
77   auto* action = new NetworkConstantAction(this, *src, *dst, size);
78
79   s4u::Link::on_communicate(*action);
80   return action;
81 }
82
83 /**********
84  * Action *
85  **********/
86 NetworkConstantAction::NetworkConstantAction(NetworkConstantModel* model_, s4u::Host& src, s4u::Host& dst, double size)
87     : NetworkAction(model_, src, dst, size, false)
88 {
89   latency_ = sg_latency_factor;
90   if (latency_ <= 0.0)
91     NetworkConstantAction::set_state(Action::State::FINISHED);
92 }
93
94 void NetworkConstantAction::update_remains_lazy(double /*now*/)
95 {
96   THROW_IMPOSSIBLE;
97 }
98
99 } // namespace resource
100 } // namespace kernel
101 } // namespace simgrid