Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Move code in simgrid::mc
[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 == NULL);
17   surf_network_model = new simgrid::surf::NetworkConstantModel();
18   xbt_dynar_push(all_existing_models, &surf_network_model);
19
20   routing_model_create(NULL);
21
22   simgrid::surf::on_link.connect([](sg_platf_link_cbarg_t link){
23     xbt_die("There is no link in the Constant network model. "
24         "Please remove any link from your platform (and switch to routing='None')");
25   });
26 }
27
28 namespace simgrid {
29   namespace surf {
30
31     double NetworkConstantModel::next_occuring_event(double /*now*/)
32     {
33       NetworkConstantAction *action = NULL;
34       double min = -1.0;
35
36       ActionList *actionSet = getRunningActionSet();
37       for(ActionList::iterator it(actionSet->begin()), itend(actionSet->end())
38           ; it != itend ; ++it) {
39         action = static_cast<NetworkConstantAction*>(&*it);
40         if (action->m_latency > 0 && (min < 0 || action->m_latency < min))
41           min = action->m_latency;
42       }
43
44       return min;
45     }
46
47     void NetworkConstantModel::updateActionsState(double /*now*/, double delta)
48     {
49       NetworkConstantAction *action = NULL;
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->m_latency > 0) {
56           if (action->m_latency > delta) {
57             double_update(&(action->m_latency), delta, sg_surf_precision);
58           } else {
59             action->m_latency = 0.0;
60           }
61         }
62         action->updateRemains(action->getCost() * delta / action->m_latInit);
63         if (action->getMaxDuration() != NO_MAX_DURATION)
64           action->updateMaxDuration(delta);
65
66         if (action->getRemainsNoUpdate() <= 0) {
67           action->finish();
68           action->setState(SURF_ACTION_DONE);
69         } else if ((action->getMaxDuration() != NO_MAX_DURATION)
70             && (action->getMaxDuration() <= 0)) {
71           action->finish();
72           action->setState(SURF_ACTION_DONE);
73         }
74       }
75     }
76
77     Action *NetworkConstantModel::communicate(NetCard *src, NetCard *dst,
78         double size, double rate)
79     {
80       char *src_name = src->name();
81       char *dst_name = dst->name();
82
83       XBT_IN("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
84       NetworkConstantAction *action = new NetworkConstantAction(this, size, sg_latency_factor);
85       XBT_OUT();
86
87       networkCommunicateCallbacks(action, src, dst, size, rate);
88       return action;
89     }
90
91     /**********
92      * Action *
93      **********/
94
95     int NetworkConstantAction::unref()
96     {
97       m_refcount--;
98       if (!m_refcount) {
99         if (action_hook.is_linked())
100           p_stateSet->erase(p_stateSet->iterator_to(*this));
101         delete this;
102         return 1;
103       }
104       return 0;
105     }
106
107     void NetworkConstantAction::cancel()
108     {
109       return;
110     }
111
112   }
113 }