Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move the host list into the Engine
[simgrid.git] / src / surf / network_constant.cpp
1 /* Copyright (c) 2013-2017. 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
8 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
9
10 /*********
11  * Model *
12  *********/
13 void surf_network_model_init_Constant()
14 {
15   xbt_assert(surf_network_model == nullptr);
16   surf_network_model = new simgrid::surf::NetworkConstantModel();
17   all_existing_models->push_back(surf_network_model);
18 }
19
20 namespace simgrid {
21 namespace surf {
22 LinkImpl* NetworkConstantModel::createLink(const std::string& name, double bw, double lat,
23                                            e_surf_link_sharing_policy_t policy)
24 {
25
26   xbt_die("Refusing to create the link %s: there is no link in the Constant network model. "
27           "Please remove any link from your platform (and switch to routing='None')",
28           name.c_str());
29   return nullptr;
30 }
31
32 double NetworkConstantModel::nextOccuringEvent(double /*now*/)
33 {
34   double min = -1.0;
35   for (Action const& action : *getRunningActionSet()) {
36     const NetworkConstantAction& net_action = static_cast<const NetworkConstantAction&>(action);
37     if (net_action.latency_ > 0 && (min < 0 || net_action.latency_ < min))
38       min = net_action.latency_;
39   }
40   return min;
41 }
42
43 void NetworkConstantModel::updateActionsState(double /*now*/, double delta)
44 {
45   for (auto it = std::begin(*getRunningActionSet()); it != std::end(*getRunningActionSet());) {
46     NetworkConstantAction& action = static_cast<NetworkConstantAction&>(*it);
47     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
48     if (action.latency_ > 0) {
49       if (action.latency_ > delta) {
50         double_update(&action.latency_, delta, sg_surf_precision);
51       } else {
52         action.latency_ = 0.0;
53       }
54     }
55     action.updateRemains(action.getCost() * delta / action.initialLatency_);
56     if (action.getMaxDuration() != NO_MAX_DURATION)
57       action.updateMaxDuration(delta);
58
59     if ((action.getRemainsNoUpdate() <= 0) ||
60         ((action.getMaxDuration() != NO_MAX_DURATION) && (action.getMaxDuration() <= 0))) {
61       action.finish(Action::State::done);
62     }
63   }
64 }
65
66 Action* NetworkConstantModel::communicate(s4u::Host* src, s4u::Host* dst, double size, double rate)
67 {
68   NetworkConstantAction* action = new NetworkConstantAction(this, size, sg_latency_factor);
69
70   simgrid::s4u::Link::onCommunicate(action, src, dst);
71   return action;
72 }
73
74 /**********
75  * Action *
76  **********/
77 NetworkConstantAction::NetworkConstantAction(NetworkConstantModel* model_, double size, double latency)
78     : NetworkAction(model_, size, false), initialLatency_(latency)
79 {
80   latency_ = latency;
81   if (latency_ <= 0.0) {
82     stateSet_ = model_->getDoneActionSet();
83     stateSet_->push_back(*this);
84   }
85 };
86
87 NetworkConstantAction::~NetworkConstantAction() = default;
88 }
89 }