Logo AND Algorithmique Numérique Distribuée

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