Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://github.com/mpoquet/simgrid
[simgrid.git] / src / surf / ptask_L07.cpp
1 /* Copyright (c) 2007-2010, 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 <cstdlib>
8
9 #include <algorithm>
10 #include <unordered_set>
11
12 #include "ptask_L07.hpp"
13
14 #include "cpu_interface.hpp"
15 #include "surf_routing.hpp"
16 #include "xbt/lib.h"
17
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_host);
19 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
20
21 /**************************************/
22 /*** Resource Creation & Destruction **/
23 /**************************************/
24 void surf_host_model_init_ptask_L07()
25 {
26   XBT_CINFO(xbt_cfg,"Switching to the L07 model to handle parallel tasks.");
27   xbt_assert(!surf_cpu_model_pm, "CPU model type already defined");
28   xbt_assert(!surf_network_model, "network model type already defined");
29
30   surf_host_model = new simgrid::surf::HostL07Model();
31   all_existing_models->push_back(surf_host_model);
32 }
33
34
35 namespace simgrid {
36 namespace surf {
37
38 HostL07Model::HostL07Model() : HostModel() {
39   maxminSystem_ = lmm_system_new(true /* lazy */);
40   maxminSystem_->solve_fun = &bottleneck_solve;
41   surf_network_model = new NetworkL07Model(this,maxminSystem_);
42   surf_cpu_model_pm = new CpuL07Model(this,maxminSystem_);
43 }
44
45 HostL07Model::~HostL07Model() = default;
46
47 CpuL07Model::CpuL07Model(HostL07Model *hmodel,lmm_system_t sys)
48   : CpuModel()
49   , hostModel_(hmodel)
50   {
51     maxminSystem_ = sys;
52   }
53 CpuL07Model::~CpuL07Model() {
54   lmm_system_free(maxminSystem_);
55   maxminSystem_ = nullptr;
56 }
57 NetworkL07Model::NetworkL07Model(HostL07Model *hmodel, lmm_system_t sys)
58   : NetworkModel()
59   , hostModel_(hmodel)
60   {
61     maxminSystem_ = sys;
62     loopback_     = createLink("__loopback__", 498000000, 0.000015, SURF_LINK_FATPIPE);
63   }
64 NetworkL07Model::~NetworkL07Model()
65 {
66   maxminSystem_ = nullptr; // Avoid multi-free
67 }
68
69
70 double HostL07Model::nextOccuringEvent(double now)
71 {
72   double min = HostModel::nextOccuringEventFull(now);
73   for (auto it(getRunningActionSet()->begin()), itend(getRunningActionSet()->end()); it != itend ; ++it) {
74     L07Action *action = static_cast<L07Action*>(&*it);
75     if (action->latency_ > 0 && (min < 0 || action->latency_ < min)) {
76       min = action->latency_;
77       XBT_DEBUG("Updating min with %p (start %f): %f", action, action->getStartTime(), min);
78     }
79   }
80   XBT_DEBUG("min value: %f", min);
81
82   return min;
83 }
84
85 void HostL07Model::updateActionsState(double /*now*/, double delta) {
86
87   L07Action *action;
88   ActionList *actionSet = getRunningActionSet();
89
90   for(ActionList::iterator it = actionSet->begin(), itNext = it
91    ; it != actionSet->end()
92    ; it =  itNext) {
93   ++itNext;
94     action = static_cast<L07Action*>(&*it);
95     if (action->latency_ > 0) {
96       if (action->latency_ > delta) {
97         double_update(&(action->latency_), delta, sg_surf_precision);
98       } else {
99         action->latency_ = 0.0;
100       }
101       if ((action->latency_ == 0.0) && (action->isSuspended() == 0)) {
102         action->updateBound();
103         lmm_update_variable_weight(maxminSystem_, action->getVariable(), 1.0);
104       }
105     }
106     XBT_DEBUG("Action (%p) : remains (%g) updated by %g.",
107            action, action->getRemains(), lmm_variable_getvalue(action->getVariable()) * delta);
108     action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
109
110     if (action->getMaxDuration() != NO_MAX_DURATION)
111       action->updateMaxDuration(delta);
112
113     XBT_DEBUG("Action (%p) : remains (%g).", action, action->getRemains());
114
115     /* In the next if cascade, the action can be finished either because:
116      *  - The amount of remaining work reached 0
117      *  - The max duration was reached
118      * If it's not done, it may have failed.
119      */
120
121     if ((action->getRemains() <= 0) &&
122         (lmm_get_variable_weight(action->getVariable()) > 0)) {
123       action->finish();
124       action->setState(Action::State::done);
125     } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
126                (action->getMaxDuration() <= 0)) {
127       action->finish();
128       action->setState(Action::State::done);
129     } else {
130       /* Need to check that none of the model has failed */
131       lmm_constraint_t cnst = nullptr;
132       int i = 0;
133
134       while ((cnst = lmm_get_cnst_from_var(maxminSystem_, action->getVariable(), i++))) {
135         void *constraint_id = lmm_constraint_id(cnst);
136         if (static_cast<simgrid::surf::Resource*>(constraint_id)->isOff()) {
137           XBT_DEBUG("Action (%p) Failed!!", action);
138           action->finish();
139           action->setState(Action::State::failed);
140           break;
141         }
142       }
143     }
144   }
145   return;
146 }
147
148 Action *HostL07Model::executeParallelTask(int host_nb, sg_host_t *host_list,
149                                           double *flops_amount, double *bytes_amount,double rate) {
150   return new L07Action(this, host_nb, host_list, flops_amount, bytes_amount, rate);
151 }
152
153
154 L07Action::L07Action(Model *model, int host_nb, sg_host_t *host_list,
155                      double *flops_amount, double *bytes_amount, double rate)
156   : CpuAction(model, 1, 0)
157 {
158   int nb_link = 0;
159   int nb_used_host = 0; /* Only the hosts with something to compute (>0 flops) are counted) */
160   double latency = 0.0;
161
162   this->netcardList_->reserve(host_nb);
163   for (int i = 0; i<host_nb; i++)
164     this->netcardList_->push_back(host_list[i]->pimpl_netcard);
165
166   /* Compute the number of affected resources... */
167   if(bytes_amount != nullptr) {
168     std::unordered_set<const char*> affected_links;
169
170     for (int i = 0; i < host_nb; i++) {
171       for (int j = 0; j < host_nb; j++) {
172
173         if (bytes_amount[i * host_nb + j] > 0) {
174           double lat=0.0;
175           std::vector<Link*> route;
176
177           routing_platf->getRouteAndLatency((*netcardList_)[i], (*netcardList_)[j], &route, &lat);
178           latency = MAX(latency, lat);
179
180           for (auto link : route)
181             affected_links.insert(link->getName());
182         }
183       }
184     }
185
186     nb_link = affected_links.size();
187   }
188
189   for (int i = 0; i < host_nb; i++)
190     if (flops_amount[i] > 0)
191       nb_used_host++;
192
193   XBT_DEBUG("Creating a parallel task (%p) with %d hosts and %d unique links.", this, host_nb, nb_link);
194   this->computationAmount_ = flops_amount;
195   this->communicationAmount_ = bytes_amount;
196   this->latency_ = latency;
197   this->rate_ = rate;
198
199   this->variable_ = lmm_variable_new(model->getMaxminSystem(), this, 1.0,
200       (rate > 0 ? rate : -1.0),
201       host_nb + nb_link);
202
203   if (this->latency_ > 0)
204     lmm_update_variable_weight(model->getMaxminSystem(), this->getVariable(), 0.0);
205
206   for (int i = 0; i < host_nb; i++)
207     lmm_expand(model->getMaxminSystem(), host_list[i]->pimpl_cpu->getConstraint(),
208         this->getVariable(), flops_amount[i]);
209
210   if(bytes_amount != nullptr) {
211     for (int i = 0; i < host_nb; i++) {
212       for (int j = 0; j < host_nb; j++) {
213
214         if (bytes_amount[i * host_nb + j] == 0.0)
215           continue;
216         std::vector<Link*> route;
217
218         routing_platf->getRouteAndLatency((*netcardList_)[i], (*netcardList_)[j], &route, nullptr);
219
220         for (auto link : route)
221           lmm_expand_add(model->getMaxminSystem(), link->getConstraint(), this->getVariable(), bytes_amount[i * host_nb + j]);
222       }
223     }
224   }
225
226   if (nb_link + nb_used_host == 0) {
227     this->setCost(1.0);
228     this->setRemains(0.0);
229   }
230   xbt_free(host_list);
231 }
232
233 Action* NetworkL07Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double rate)
234 {
235   sg_host_t*host_list = xbt_new0(sg_host_t, 2);
236   double *flops_amount = xbt_new0(double, 2);
237   double *bytes_amount = xbt_new0(double, 4);
238
239   host_list[0]    = src;
240   host_list[1]    = dst;
241   bytes_amount[1] = size;
242
243   return hostModel_->executeParallelTask(2, host_list, flops_amount, bytes_amount, rate);
244 }
245
246 Cpu *CpuL07Model::createCpu(simgrid::s4u::Host *host,  std::vector<double> *speedPerPstate, int core)
247 {
248   return new CpuL07(this, host, speedPerPstate, core);
249 }
250
251 Link* NetworkL07Model::createLink(const char* name, double bandwidth, double latency,
252                                   e_surf_link_sharing_policy_t policy)
253 {
254   return new LinkL07(this, name, bandwidth, latency, policy);
255 }
256
257 /************
258  * Resource *
259  ************/
260
261 CpuL07::CpuL07(CpuL07Model* model, simgrid::s4u::Host* host, std::vector<double>* speedPerPstate, int core)
262     : Cpu(model, host, lmm_constraint_new(model->getMaxminSystem(), this, speedPerPstate->front()), speedPerPstate,
263           core)
264 {
265 }
266
267 CpuL07::~CpuL07()=default;
268
269 LinkL07::LinkL07(NetworkL07Model* model, const char* name, double bandwidth, double latency,
270                  e_surf_link_sharing_policy_t policy)
271     : Link(model, name, lmm_constraint_new(model->getMaxminSystem(), this, bandwidth))
272 {
273   bandwidth_.peak = bandwidth;
274   latency_.peak   = latency;
275
276   if (policy == SURF_LINK_FATPIPE)
277     lmm_constraint_shared(getConstraint());
278
279   Link::onCreation(this);
280 }
281
282 Action *CpuL07::execution_start(double size)
283 {
284   sg_host_t*host_list = xbt_new0(sg_host_t, 1);
285   double *flops_amount = xbt_new0(double, 1);
286
287   host_list[0] = getHost();
288   flops_amount[0] = size;
289
290   return static_cast<CpuL07Model*>(getModel())->hostModel_->executeParallelTask(1, host_list, flops_amount, nullptr, -1);
291 }
292
293 Action *CpuL07::sleep(double duration)
294 {
295   L07Action *action = static_cast<L07Action*>(execution_start(1.0));
296   action->maxDuration_ = duration;
297   action->suspended_ = 2;
298   lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), 0.0);
299
300   return action;
301 }
302
303 bool CpuL07::isUsed(){
304   return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
305 }
306
307 /** @brief take into account changes of speed (either load or max) */
308 void CpuL07::onSpeedChange() {
309   lmm_variable_t var = nullptr;
310   lmm_element_t elem = nullptr;
311
312     lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(), speed_.peak * speed_.scale);
313     while ((var = lmm_get_var_from_cnst (getModel()->getMaxminSystem(), getConstraint(), &elem))) {
314       Action *action = static_cast<Action*>(lmm_variable_id(var));
315
316       lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(), speed_.scale * speed_.peak);
317     }
318
319   Cpu::onSpeedChange();
320 }
321
322
323 bool LinkL07::isUsed(){
324   return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
325 }
326
327 void CpuL07::apply_event(tmgr_trace_iterator_t triggered, double value){
328   XBT_DEBUG("Updating cpu %s (%p) with value %g", getName(), this, value);
329   if (triggered == speed_.event) {
330     speed_.scale = value;
331     onSpeedChange();
332     tmgr_trace_event_unref(&speed_.event);
333
334   } else if (triggered == stateEvent_) {
335     if (value > 0)
336       turnOn();
337     else
338       turnOff();
339     tmgr_trace_event_unref(&stateEvent_);
340
341   } else {
342     xbt_die("Unknown event!\n");
343   }
344 }
345
346 void LinkL07::apply_event(tmgr_trace_iterator_t triggered, double value) {
347   XBT_DEBUG("Updating link %s (%p) with value=%f", getName(), this, value);
348   if (triggered == bandwidth_.event) {
349     setBandwidth(value);
350     tmgr_trace_event_unref(&bandwidth_.event);
351
352   } else if (triggered == latency_.event) {
353     setLatency(value);
354     tmgr_trace_event_unref(&latency_.event);
355
356   } else if (triggered == stateEvent_) {
357     if (value > 0)
358       turnOn();
359     else
360       turnOff();
361     tmgr_trace_event_unref(&stateEvent_);
362
363   } else {
364     xbt_die("Unknown event ! \n");
365   }
366 }
367
368 void LinkL07::setBandwidth(double value)
369 {
370   bandwidth_.peak = value;
371   lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(), bandwidth_.peak * bandwidth_.scale);
372 }
373
374 void LinkL07::setLatency(double value)
375 {
376   lmm_variable_t var = nullptr;
377   L07Action *action;
378   lmm_element_t elem = nullptr;
379
380   latency_.peak = value;
381   while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), getConstraint(), &elem))) {
382     action = static_cast<L07Action*>(lmm_variable_id(var));
383     action->updateBound();
384   }
385 }
386
387 /**********
388  * Action *
389  **********/
390
391 L07Action::~L07Action(){
392   delete netcardList_;
393   free(communicationAmount_);
394   free(computationAmount_);
395 }
396
397 void L07Action::updateBound()
398 {
399   double lat_current = 0.0;
400   double lat_bound = -1.0;
401   int i, j;
402
403   int hostNb = netcardList_->size();
404
405   if (communicationAmount_ != nullptr) {
406     for (i = 0; i < hostNb; i++) {
407       for (j = 0; j < hostNb; j++) {
408
409         if (communicationAmount_[i * hostNb + j] > 0) {
410           double lat = 0.0;
411           std::vector<Link*> route;
412           routing_platf->getRouteAndLatency((*netcardList_)[i], (*netcardList_)[j], &route, &lat);
413
414           lat_current = MAX(lat_current, lat * communicationAmount_[i * hostNb + j]);
415         }
416       }
417     }
418   }
419   lat_bound = sg_tcp_gamma / (2.0 * lat_current);
420   XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
421   if ((latency_ == 0.0) && (suspended_ == 0)) {
422     if (rate_ < 0)
423       lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), lat_bound);
424     else
425       lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), std::min(rate_, lat_bound));
426   }
427 }
428
429 int L07Action::unref()
430 {
431   refcount_--;
432   if (!refcount_) {
433     if (action_hook.is_linked())
434       stateSet_->erase(stateSet_->iterator_to(*this));
435     if (getVariable())
436       lmm_variable_free(getModel()->getMaxminSystem(), getVariable());
437     delete this;
438     return 1;
439   }
440   return 0;
441 }
442
443 }
444 }