Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into S4U
[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 #include "surf/random_mgr.h"
9
10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
11 static int host_number_int = 0;
12
13 static void netcste_count_hosts(sg_platf_host_cbarg_t /*h*/) {
14   host_number_int++;
15 }
16 static void netcste_parse_nolink(sg_platf_link_cbarg_t link){
17         xbt_die("There is no link in the Constant network model. "
18                         "Please remove any link from your platform (and switch to routing='None')");
19 }
20
21 /*********
22  * Model *
23  *********/
24 void surf_network_model_init_Constant()
25 {
26   xbt_assert(surf_network_model == NULL);
27   surf_network_model = new NetworkConstantModel();
28
29   routing_model_create(NULL);
30
31   sg_platf_host_add_cb(netcste_count_hosts);
32   sg_platf_link_add_cb(netcste_parse_nolink);
33
34   Model *model = surf_network_model;
35   xbt_dynar_push(model_list, &model);
36 }
37
38 double NetworkConstantModel::shareResources(double /*now*/)
39 {
40   NetworkConstantAction *action = NULL;
41   double min = -1.0;
42
43   ActionList *actionSet = getRunningActionSet();
44   for(ActionList::iterator it(actionSet->begin()), itend(actionSet->end())
45          ; it != itend ; ++it) {
46         action = static_cast<NetworkConstantAction*>(&*it);
47         if (action->m_latency > 0 && (min < 0 || action->m_latency < min))
48             min = action->m_latency;
49   }
50
51   return min;
52 }
53
54 void NetworkConstantModel::updateActionsState(double /*now*/, double delta)
55 {
56   NetworkConstantAction *action = NULL;
57   ActionList *actionSet = getRunningActionSet();
58   for(ActionList::iterator it(actionSet->begin()), itNext=it, itend(actionSet->end())
59      ; it != itend ; it=itNext) {
60     ++itNext;
61         action = static_cast<NetworkConstantAction*>(&*it);
62     if (action->m_latency > 0) {
63       if (action->m_latency > delta) {
64         double_update(&(action->m_latency), delta, sg_surf_precision);
65       } else {
66         action->m_latency = 0.0;
67       }
68     }
69     action->updateRemains(action->getCost() * delta / action->m_latInit);
70     if (action->getMaxDuration() != NO_MAX_DURATION)
71       action->updateMaxDuration(delta);
72
73     if (action->getRemainsNoUpdate() <= 0) {
74       action->finish();
75       action->setState(SURF_ACTION_DONE);
76     } else if ((action->getMaxDuration() != NO_MAX_DURATION)
77                && (action->getMaxDuration() <= 0)) {
78       action->finish();
79       action->setState(SURF_ACTION_DONE);
80     }
81   }
82 }
83
84 Action *NetworkConstantModel::communicate(RoutingEdge *src, RoutingEdge *dst,
85                                          double size, double rate)
86 {
87   char *src_name = src->getName();
88   char *dst_name = dst->getName();
89
90   XBT_IN("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
91   NetworkConstantAction *action = new NetworkConstantAction(this, size, sg_latency_factor);
92   XBT_OUT();
93
94   surf_callback_emit(networkCommunicateCallbacks, action, src, dst, size, rate);
95   return action;
96 }
97
98 /**********
99  * Action *
100  **********/
101
102 int NetworkConstantAction::unref()
103 {
104   m_refcount--;
105   if (!m_refcount) {
106         if (actionHook::is_linked())
107           p_stateSet->erase(p_stateSet->iterator_to(*this));
108     delete this;
109   return 1;
110   }
111   return 0;
112 }
113
114 void NetworkConstantAction::cancel()
115 {
116   return;
117 }
118
119 void NetworkConstantAction::setCategory(const char */*category*/)
120 {
121   //ignore completely the categories in constant model, they are not traced
122 }
123
124 void NetworkConstantAction::suspend()
125 {
126   m_suspended = true;
127 }
128
129 void NetworkConstantAction::resume()
130 {
131   if (m_suspended)
132         m_suspended = false;
133 }
134
135 bool NetworkConstantAction::isSuspended()
136 {
137   return m_suspended;
138 }
139