Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Engine*::add_model: from unique to shared_ptr
[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 "src/kernel/EngineImpl.hpp"
8 #include "src/surf/surf_interface.hpp"
9 #include "surf/surf.hpp"
10
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_network);
12
13 /*********
14  * Model *
15  *********/
16 void surf_network_model_init_Constant()
17 {
18   auto net_model = std::make_shared<simgrid::kernel::resource::NetworkConstantModel>();
19   simgrid::kernel::EngineImpl::get_instance()->add_model(simgrid::kernel::resource::Model::Type::NETWORK,
20                                                          std::move(net_model), true);
21 }
22
23 namespace simgrid {
24 namespace kernel {
25 namespace resource {
26
27 NetworkConstantModel::NetworkConstantModel() : NetworkModel(Model::UpdateAlgo::FULL)
28 {
29 }
30
31 LinkImpl* NetworkConstantModel::create_link(const std::string& name, const std::vector<double>& /*bandwidth*/,
32                                             s4u::Link::SharingPolicy)
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 double NetworkConstantModel::next_occurring_event(double /*now*/)
41 {
42   double min = -1.0;
43   for (kernel::resource::Action const& action : *get_started_action_set()) {
44     const auto& net_action = static_cast<const NetworkConstantAction&>(action);
45     if (net_action.latency_ > 0 && (min < 0 || net_action.latency_ < min))
46       min = net_action.latency_;
47   }
48   return min;
49 }
50
51 void NetworkConstantModel::update_actions_state(double /*now*/, double delta)
52 {
53   for (auto it = std::begin(*get_started_action_set()); it != std::end(*get_started_action_set());) {
54     auto& action = static_cast<NetworkConstantAction&>(*it);
55     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
56     if (action.latency_ > 0) {
57       if (action.latency_ > delta) {
58         double_update(&action.latency_, delta, sg_surf_precision);
59       } else {
60         action.latency_ = 0.0;
61       }
62     }
63     action.update_remains(action.get_cost() * delta / action.initial_latency_);
64     action.update_max_duration(delta);
65
66     if ((action.get_remains_no_update() <= 0) ||
67         ((action.get_max_duration() != NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
68       action.finish(Action::State::FINISHED);
69     }
70   }
71 }
72
73 Action* NetworkConstantModel::communicate(s4u::Host* src, s4u::Host* dst, double size, double)
74 {
75   auto* action = new NetworkConstantAction(this, *src, *dst, size, sg_latency_factor);
76
77   s4u::Link::on_communicate(*action);
78   return action;
79 }
80
81 /**********
82  * Action *
83  **********/
84 NetworkConstantAction::NetworkConstantAction(NetworkConstantModel* model_, s4u::Host& src, s4u::Host& dst, double size,
85                                              double latency)
86     : NetworkAction(model_, src, dst, size, false), initial_latency_(latency)
87 {
88   latency_ = latency;
89   if (latency_ <= 0.0)
90     NetworkConstantAction::set_state(Action::State::FINISHED);
91 }
92
93 NetworkConstantAction::~NetworkConstantAction() = default;
94
95 void NetworkConstantAction::update_remains_lazy(double /*now*/)
96 {
97   THROW_IMPOSSIBLE;
98 }
99
100 } // namespace resource
101 } // namespace kernel
102 } // namespace simgrid