Logo AND Algorithmique Numérique Distribuée

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