Logo AND Algorithmique Numérique Distribuée

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