Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e2675fcb5eb5c56fdefe661527eb5fbbb5a9e8bd
[simgrid.git] / src / surf / network_constant.cpp
1 /* Copyright (c) 2013-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "network_constant.hpp"
8
9 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
10
11 /*********
12  * Model *
13  *********/
14 void surf_network_model_init_Constant()
15 {
16   xbt_assert(surf_network_model == nullptr);
17   surf_network_model = new simgrid::surf::NetworkConstantModel();
18   all_existing_models->push_back(surf_network_model);
19 }
20
21 namespace simgrid {
22   namespace surf {
23   LinkImpl* NetworkConstantModel::createLink(const char* name, double bw, double lat,
24                                              e_surf_link_sharing_policy_t policy)
25   {
26
27     xbt_die("Refusing to create the link %s: there is no link in the Constant network model. "
28             "Please remove any link from your platform (and switch to routing='None')",
29             name);
30     return nullptr;
31     }
32
33     double NetworkConstantModel::nextOccuringEvent(double /*now*/)
34     {
35       double min = -1.0;
36
37       ActionList *actionSet = getRunningActionSet();
38       for(auto it(actionSet->begin()), itend(actionSet->end()) ; it != itend ; ++it) {
39         NetworkConstantAction *action = static_cast<NetworkConstantAction*>(&*it);
40         if (action->latency_ > 0 && (min < 0 || action->latency_ < min))
41           min = action->latency_;
42       }
43
44       return min;
45     }
46
47     void NetworkConstantModel::updateActionsState(double /*now*/, double delta)
48     {
49       NetworkConstantAction *action = nullptr;
50       ActionList *actionSet = getRunningActionSet();
51       for(ActionList::iterator it(actionSet->begin()), itNext=it, itend(actionSet->end())
52           ; it != itend ; it=itNext) {
53         ++itNext;
54         action = static_cast<NetworkConstantAction*>(&*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->updateRemains(action->getCost() * delta / action->initialLatency_);
63         if (action->getMaxDuration() != NO_MAX_DURATION)
64           action->updateMaxDuration(delta);
65
66         if (action->getRemainsNoUpdate() <= 0) {
67           action->finish();
68           action->setState(Action::State::done);
69         } else if ((action->getMaxDuration() != NO_MAX_DURATION)
70             && (action->getMaxDuration() <= 0)) {
71           action->finish();
72           action->setState(Action::State::done);
73         }
74       }
75     }
76
77     Action* NetworkConstantModel::communicate(s4u::Host* src, s4u::Host* dst, double size, double rate)
78     {
79       NetworkConstantAction *action = new NetworkConstantAction(this, size, sg_latency_factor);
80
81       simgrid::s4u::Link::onCommunicate(action, src, dst);
82       return action;
83     }
84
85     /**********
86      * Action *
87      **********/
88     NetworkConstantAction::NetworkConstantAction(NetworkConstantModel *model_, double size, double latency)
89     : NetworkAction(model_, size, false)
90     , initialLatency_(latency)
91     {
92       latency_ = latency;
93       if (latency_ <= 0.0) {
94         stateSet_ = model_->getDoneActionSet();
95         stateSet_->push_back(*this);
96       }
97     };
98
99     NetworkConstantAction::~NetworkConstantAction() = default;
100   }
101 }