Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branches 'master' and 'master' of scm.gforge.inria.fr:/gitroot/simgrid/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
11 #include "ptask_L07.hpp"
12
13 #include "cpu_interface.hpp"
14 #include "surf_routing.hpp"
15 #include "xbt/lib.h"
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_host);
18 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
19
20 /**************************************/
21 /*** Resource Creation & Destruction **/
22 /**************************************/
23 void surf_host_model_init_ptask_L07()
24 {
25   XBT_CINFO(xbt_cfg,"Switching to the L07 model to handle parallel tasks.");
26   xbt_assert(!surf_cpu_model_pm, "CPU model type already defined");
27   xbt_assert(!surf_network_model, "network model type already defined");
28
29   surf_host_model = new simgrid::surf::HostL07Model();
30   all_existing_models->push_back(surf_host_model);
31 }
32
33
34 namespace simgrid {
35 namespace surf {
36
37 HostL07Model::HostL07Model() : HostModel() {
38   maxminSystem_ = lmm_system_new(true /* lazy */);
39   maxminSystem_->solve_fun = &bottleneck_solve;
40   surf_network_model = new NetworkL07Model(this,maxminSystem_);
41   surf_cpu_model_pm = new CpuL07Model(this,maxminSystem_);
42
43   routing_model_create(surf_network_model->createLink("__loopback__", 498000000, 0.000015, SURF_LINK_FATPIPE, nullptr));
44 }
45
46 HostL07Model::~HostL07Model() = default;
47
48 CpuL07Model::CpuL07Model(HostL07Model *hmodel,lmm_system_t sys)
49   : CpuModel()
50   , hostModel_(hmodel)
51   {
52     maxminSystem_ = sys;
53   }
54 CpuL07Model::~CpuL07Model() {
55   lmm_system_free(maxminSystem_);
56   maxminSystem_ = nullptr;
57 }
58 NetworkL07Model::NetworkL07Model(HostL07Model *hmodel, lmm_system_t sys)
59   : NetworkModel()
60   , hostModel_(hmodel)
61   {
62     maxminSystem_ = sys;
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     xbt_dict_t ptask_parallel_task_link_set = xbt_dict_new_homogeneous(nullptr);
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 = new std::vector<Link*>();
176
177           routing_platf->getRouteAndLatency((*netcardList_)[i], (*netcardList_)[j], route, &lat);
178           latency = MAX(latency, lat);
179
180           for (auto link : *route)
181             xbt_dict_set(ptask_parallel_task_link_set, link->getName(), link, nullptr);
182           delete route;
183         }
184       }
185     }
186
187     nb_link = xbt_dict_length(ptask_parallel_task_link_set);
188     xbt_dict_free(&ptask_parallel_task_link_set);
189   }
190
191   for (int i = 0; i < host_nb; i++)
192     if (flops_amount[i] > 0)
193       nb_used_host++;
194
195   XBT_DEBUG("Creating a parallel task (%p) with %d hosts and %d unique links.", this, host_nb, nb_link);
196   this->computationAmount_ = flops_amount;
197   this->communicationAmount_ = bytes_amount;
198   this->latency_ = latency;
199   this->rate_ = rate;
200
201   this->variable_ = lmm_variable_new(model->getMaxminSystem(), this, 1.0,
202       (rate > 0 ? rate : -1.0),
203       host_nb + nb_link);
204
205   if (this->latency_ > 0)
206     lmm_update_variable_weight(model->getMaxminSystem(), this->getVariable(), 0.0);
207
208   for (int i = 0; i < host_nb; i++)
209     lmm_expand(model->getMaxminSystem(), host_list[i]->pimpl_cpu->getConstraint(),
210         this->getVariable(), flops_amount[i]);
211
212   if(bytes_amount != nullptr) {
213     for (int i = 0; i < host_nb; i++) {
214       for (int j = 0; j < host_nb; j++) {
215
216         if (bytes_amount[i * host_nb + j] == 0.0)
217           continue;
218         std::vector<Link*> *route = new std::vector<Link*>();
219
220         routing_platf->getRouteAndLatency((*netcardList_)[i], (*netcardList_)[j], route, nullptr);
221
222         for (auto link : *route)
223           lmm_expand_add(model->getMaxminSystem(), link->getConstraint(), this->getVariable(), bytes_amount[i * host_nb + j]);
224
225         delete route;
226       }
227     }
228   }
229
230   if (nb_link + nb_used_host == 0) {
231     this->setCost(1.0);
232     this->setRemains(0.0);
233   }
234   xbt_free(host_list);
235 }
236
237 Action* NetworkL07Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double rate)
238 {
239   sg_host_t*host_list = xbt_new0(sg_host_t, 2);
240   double *flops_amount = xbt_new0(double, 2);
241   double *bytes_amount = xbt_new0(double, 4);
242
243   host_list[0]    = src;
244   host_list[1]    = dst;
245   bytes_amount[1] = size;
246
247   return hostModel_->executeParallelTask(2, host_list, flops_amount, bytes_amount, rate);
248 }
249
250 Cpu *CpuL07Model::createCpu(simgrid::s4u::Host *host,  std::vector<double> *speedPerPstate, int core)
251 {
252   return new CpuL07(this, host, speedPerPstate, core);
253 }
254
255 Link* NetworkL07Model::createLink(const char *name, double bandwidth, double latency,
256     e_surf_link_sharing_policy_t policy, xbt_dict_t properties)
257 {
258   return new LinkL07(this, name, properties, bandwidth, latency, policy);
259 }
260
261 /************
262  * Resource *
263  ************/
264
265 CpuL07::CpuL07(CpuL07Model *model, simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
266  : Cpu(model, host, speedPerPstate, core)
267 {
268   constraint_ = lmm_constraint_new(model->getMaxminSystem(), this, speedPerPstate->front());
269 }
270
271 CpuL07::~CpuL07()=default;
272
273 LinkL07::LinkL07(NetworkL07Model *model, const char* name, xbt_dict_t props, double bandwidth, double latency,
274              e_surf_link_sharing_policy_t policy)
275  : Link(model, name, props, lmm_constraint_new(model->getMaxminSystem(), this, bandwidth))
276 {
277   bandwidth_.peak = bandwidth;
278   latency_.peak   = latency;
279
280   if (policy == SURF_LINK_FATPIPE)
281     lmm_constraint_shared(getConstraint());
282
283   Link::onCreation(this);
284 }
285
286 Action *CpuL07::execution_start(double size)
287 {
288   sg_host_t*host_list = xbt_new0(sg_host_t, 1);
289   double *flops_amount = xbt_new0(double, 1);
290
291   host_list[0] = getHost();
292   flops_amount[0] = size;
293
294   return static_cast<CpuL07Model*>(getModel())->hostModel_->executeParallelTask(1, host_list, flops_amount, nullptr, -1);
295 }
296
297 Action *CpuL07::sleep(double duration)
298 {
299   L07Action *action = static_cast<L07Action*>(execution_start(1.0));
300   action->maxDuration_ = duration;
301   action->suspended_ = 2;
302   lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), 0.0);
303
304   return action;
305 }
306
307 bool CpuL07::isUsed(){
308   return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
309 }
310
311 /** @brief take into account changes of speed (either load or max) */
312 void CpuL07::onSpeedChange() {
313   lmm_variable_t var = nullptr;
314   lmm_element_t elem = nullptr;
315
316     lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(), speed_.peak * speed_.scale);
317     while ((var = lmm_get_var_from_cnst (getModel()->getMaxminSystem(), getConstraint(), &elem))) {
318       Action *action = static_cast<Action*>(lmm_variable_id(var));
319
320       lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(), speed_.scale * speed_.peak);
321     }
322
323   Cpu::onSpeedChange();
324 }
325
326
327 bool LinkL07::isUsed(){
328   return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
329 }
330
331 void CpuL07::apply_event(tmgr_trace_iterator_t triggered, double value){
332   XBT_DEBUG("Updating cpu %s (%p) with value %g", getName(), this, value);
333   if (triggered == speed_.event) {
334     speed_.scale = value;
335     onSpeedChange();
336     tmgr_trace_event_unref(&speed_.event);
337
338   } else if (triggered == stateEvent_) {
339     if (value > 0)
340       turnOn();
341     else
342       turnOff();
343     tmgr_trace_event_unref(&stateEvent_);
344
345   } else {
346     xbt_die("Unknown event!\n");
347   }
348 }
349
350 void LinkL07::apply_event(tmgr_trace_iterator_t triggered, double value) {
351   XBT_DEBUG("Updating link %s (%p) with value=%f", getName(), this, value);
352   if (triggered == bandwidth_.event) {
353     setBandwidth(value);
354     tmgr_trace_event_unref(&bandwidth_.event);
355
356   } else if (triggered == latency_.event) {
357     setLatency(value);
358     tmgr_trace_event_unref(&latency_.event);
359
360   } else if (triggered == stateEvent_) {
361     if (value > 0)
362       turnOn();
363     else
364       turnOff();
365     tmgr_trace_event_unref(&stateEvent_);
366
367   } else {
368     xbt_die("Unknown event ! \n");
369   }
370 }
371
372 void LinkL07::setBandwidth(double value)
373 {
374   bandwidth_.peak = value;
375   lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(), bandwidth_.peak * bandwidth_.scale);
376 }
377
378 void LinkL07::setLatency(double value)
379 {
380   lmm_variable_t var = nullptr;
381   L07Action *action;
382   lmm_element_t elem = nullptr;
383
384   latency_.peak = value;
385   while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), getConstraint(), &elem))) {
386     action = static_cast<L07Action*>(lmm_variable_id(var));
387     action->updateBound();
388   }
389 }
390
391 /**********
392  * Action *
393  **********/
394
395 L07Action::~L07Action(){
396   delete netcardList_;
397   free(communicationAmount_);
398   free(computationAmount_);
399 }
400
401 void L07Action::updateBound()
402 {
403   double lat_current = 0.0;
404   double lat_bound = -1.0;
405   int i, j;
406
407   int hostNb = netcardList_->size();
408
409   if (communicationAmount_ != nullptr) {
410     for (i = 0; i < hostNb; i++) {
411       for (j = 0; j < hostNb; j++) {
412
413         if (communicationAmount_[i * hostNb + j] > 0) {
414           double lat = 0.0;
415           std::vector<Link*> *route = new std::vector<Link*>();
416           routing_platf->getRouteAndLatency((*netcardList_)[i], (*netcardList_)[j], route, &lat);
417
418           lat_current = MAX(lat_current, lat * communicationAmount_[i * hostNb + j]);
419           delete route;
420         }
421       }
422     }
423   }
424   lat_bound = sg_tcp_gamma / (2.0 * lat_current);
425   XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
426   if ((latency_ == 0.0) && (suspended_ == 0)) {
427     if (rate_ < 0)
428       lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), lat_bound);
429     else
430       lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), std::min(rate_, lat_bound));
431   }
432 }
433
434 int L07Action::unref()
435 {
436   refcount_--;
437   if (!refcount_) {
438     if (action_hook.is_linked())
439       stateSet_->erase(stateSet_->iterator_to(*this));
440     if (getVariable())
441       lmm_variable_free(getModel()->getMaxminSystem(), getVariable());
442     delete this;
443     return 1;
444   }
445   return 0;
446 }
447
448 }
449 }