Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
smells --
[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 CpuL07Model::~CpuL07Model() = default;
57
58 NetworkL07Model::NetworkL07Model(HostL07Model *hmodel, lmm_system_t sys)
59   : NetworkModel()
60   , hostModel_(hmodel)
61   {
62     maxminSystem_ = sys;
63     loopback_     = createLink("__loopback__", 498000000, 0.000015, SURF_LINK_FATPIPE);
64   }
65 NetworkL07Model::~NetworkL07Model() = default;
66
67
68 double HostL07Model::nextOccuringEvent(double now)
69 {
70   double min = HostModel::nextOccuringEventFull(now);
71   ActionList::iterator it(getRunningActionSet()->begin());
72   ActionList::iterator itend(getRunningActionSet()->end());
73   for (; 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   ActionList::iterator it(actionSet->begin());
90   ActionList::iterator itNext = it;
91   ActionList::iterator itend(actionSet->end());
92
93   for (; it != itend; 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) && (lmm_get_variable_weight(action->getVariable()) > 0)) ||
123         ((action->getMaxDuration() != NO_MAX_DURATION) && (action->getMaxDuration() <= 0))) {
124       action->finish();
125       action->setState(Action::State::done);
126     } else {
127       /* Need to check that none of the model has failed */
128       int i = 0;
129       lmm_constraint_t cnst = lmm_get_cnst_from_var(maxminSystem_, action->getVariable(), i);
130       while (cnst != nullptr) {
131         i++;
132         void *constraint_id = lmm_constraint_id(cnst);
133         if (static_cast<simgrid::surf::Resource*>(constraint_id)->isOff()) {
134           XBT_DEBUG("Action (%p) Failed!!", action);
135           action->finish();
136           action->setState(Action::State::failed);
137           break;
138         }
139         cnst = lmm_get_cnst_from_var(maxminSystem_, action->getVariable(), i);
140       }
141     }
142   }
143 }
144
145 Action *HostL07Model::executeParallelTask(int host_nb, sg_host_t *host_list,
146                                           double *flops_amount, double *bytes_amount,double rate) {
147   return new L07Action(this, host_nb, host_list, flops_amount, bytes_amount, rate);
148 }
149
150 L07Action::L07Action(Model *model, int host_nb, sg_host_t *host_list,
151                      double *flops_amount, double *bytes_amount, double rate)
152   : CpuAction(model, 1, 0)
153   , computationAmount_(flops_amount)
154   , communicationAmount_(bytes_amount)
155   , rate_(rate)
156 {
157   int nb_link = 0;
158   int nb_used_host = 0; /* Only the hosts with something to compute (>0 flops) are counted) */
159   double latency = 0.0;
160
161   this->hostList_->reserve(host_nb);
162   for (int i = 0; i<host_nb; i++)
163     this->hostList_->push_back(host_list[i]);
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   for (int i = 0; i < host_nb; i++)
189     if (flops_amount[i] > 0)
190       nb_used_host++;
191
192   XBT_DEBUG("Creating a parallel task (%p) with %d hosts and %d unique links.", this, host_nb, nb_link);
193   this->latency_ = latency;
194
195   this->variable_ = lmm_variable_new(model->getMaxminSystem(), this, 1.0,
196       (rate > 0 ? rate : -1.0),
197       host_nb + nb_link);
198
199   if (this->latency_ > 0)
200     lmm_update_variable_weight(model->getMaxminSystem(), this->getVariable(), 0.0);
201
202   for (int i = 0; i < host_nb; i++)
203     lmm_expand(model->getMaxminSystem(), host_list[i]->pimpl_cpu->constraint(), this->getVariable(), flops_amount[i]);
204
205   if(bytes_amount != nullptr) {
206     for (int i = 0; i < host_nb; i++) {
207       for (int j = 0; j < host_nb; j++) {
208         if (bytes_amount[i * host_nb + j] > 0.0) {
209           std::vector<LinkImpl*> route;
210           hostList_->at(i)->routeTo(hostList_->at(j), &route, nullptr);
211
212           for (auto link : route)
213             lmm_expand_add(model->getMaxminSystem(), link->constraint(), this->getVariable(),
214                            bytes_amount[i * host_nb + j]);
215         }
216       }
217     }
218   }
219
220   if (nb_link + nb_used_host == 0) {
221     this->setCost(1.0);
222     this->setRemains(0.0);
223   }
224   xbt_free(host_list);
225 }
226
227 Action* NetworkL07Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double rate)
228 {
229   sg_host_t*host_list = xbt_new0(sg_host_t, 2);
230   double *flops_amount = xbt_new0(double, 2);
231   double *bytes_amount = xbt_new0(double, 4);
232
233   host_list[0]    = src;
234   host_list[1]    = dst;
235   bytes_amount[1] = size;
236
237   return hostModel_->executeParallelTask(2, host_list, flops_amount, bytes_amount, rate);
238 }
239
240 Cpu *CpuL07Model::createCpu(simgrid::s4u::Host *host,  std::vector<double> *speedPerPstate, int core)
241 {
242   return new CpuL07(this, host, speedPerPstate, core);
243 }
244
245 LinkImpl* NetworkL07Model::createLink(const char* name, double bandwidth, double latency,
246                                       e_surf_link_sharing_policy_t policy)
247 {
248   return new LinkL07(this, name, bandwidth, latency, policy);
249 }
250
251 /************
252  * Resource *
253  ************/
254
255 CpuL07::CpuL07(CpuL07Model* model, simgrid::s4u::Host* host, std::vector<double>* speedPerPstate, int core)
256     : Cpu(model, host, lmm_constraint_new(model->getMaxminSystem(), this, speedPerPstate->front()), speedPerPstate,
257           core)
258 {
259 }
260
261 CpuL07::~CpuL07()=default;
262
263 LinkL07::LinkL07(NetworkL07Model* model, const char* name, double bandwidth, double latency,
264                  e_surf_link_sharing_policy_t policy)
265     : LinkImpl(model, name, lmm_constraint_new(model->getMaxminSystem(), this, bandwidth))
266 {
267   bandwidth_.peak = bandwidth;
268   latency_.peak   = latency;
269
270   if (policy == SURF_LINK_FATPIPE)
271     lmm_constraint_shared(constraint());
272
273   s4u::Link::onCreation(this->piface_);
274 }
275
276 Action *CpuL07::execution_start(double size)
277 {
278   sg_host_t*host_list = xbt_new0(sg_host_t, 1);
279   double *flops_amount = xbt_new0(double, 1);
280
281   host_list[0] = getHost();
282   flops_amount[0] = size;
283
284   return static_cast<CpuL07Model*>(model())->hostModel_->executeParallelTask(1, host_list, flops_amount, nullptr, -1);
285 }
286
287 Action *CpuL07::sleep(double duration)
288 {
289   L07Action *action = static_cast<L07Action*>(execution_start(1.0));
290   action->maxDuration_ = duration;
291   action->suspended_ = 2;
292   lmm_update_variable_weight(model()->getMaxminSystem(), action->getVariable(), 0.0);
293
294   return action;
295 }
296
297 bool CpuL07::isUsed(){
298   return lmm_constraint_used(model()->getMaxminSystem(), constraint());
299 }
300
301 /** @brief take into account changes of speed (either load or max) */
302 void CpuL07::onSpeedChange() {
303   lmm_variable_t var = nullptr;
304   lmm_element_t elem = nullptr;
305
306   lmm_update_constraint_bound(model()->getMaxminSystem(), constraint(), speed_.peak * speed_.scale);
307   while ((var = lmm_get_var_from_cnst(model()->getMaxminSystem(), constraint(), &elem))) {
308     Action* action = static_cast<Action*>(lmm_variable_id(var));
309
310     lmm_update_variable_bound(model()->getMaxminSystem(), action->getVariable(), speed_.scale * speed_.peak);
311     }
312
313   Cpu::onSpeedChange();
314 }
315
316
317 bool LinkL07::isUsed(){
318   return lmm_constraint_used(model()->getMaxminSystem(), constraint());
319 }
320
321 void CpuL07::apply_event(tmgr_trace_iterator_t triggered, double value){
322   XBT_DEBUG("Updating cpu %s (%p) with value %g", cname(), this, value);
323   if (triggered == speed_.event) {
324     speed_.scale = value;
325     onSpeedChange();
326     tmgr_trace_event_unref(&speed_.event);
327
328   } else if (triggered == stateEvent_) {
329     if (value > 0)
330       turnOn();
331     else
332       turnOff();
333     tmgr_trace_event_unref(&stateEvent_);
334
335   } else {
336     xbt_die("Unknown event!\n");
337   }
338 }
339
340 void LinkL07::apply_event(tmgr_trace_iterator_t triggered, double value) {
341   XBT_DEBUG("Updating link %s (%p) with value=%f", cname(), this, value);
342   if (triggered == bandwidth_.event) {
343     setBandwidth(value);
344     tmgr_trace_event_unref(&bandwidth_.event);
345
346   } else if (triggered == latency_.event) {
347     setLatency(value);
348     tmgr_trace_event_unref(&latency_.event);
349
350   } else if (triggered == stateEvent_) {
351     if (value > 0)
352       turnOn();
353     else
354       turnOff();
355     tmgr_trace_event_unref(&stateEvent_);
356
357   } else {
358     xbt_die("Unknown event ! \n");
359   }
360 }
361
362 void LinkL07::setBandwidth(double value)
363 {
364   bandwidth_.peak = value;
365   lmm_update_constraint_bound(model()->getMaxminSystem(), constraint(), bandwidth_.peak * bandwidth_.scale);
366 }
367
368 void LinkL07::setLatency(double value)
369 {
370   lmm_variable_t var = nullptr;
371   L07Action *action;
372   lmm_element_t elem = nullptr;
373
374   latency_.peak = value;
375   while ((var = lmm_get_var_from_cnst(model()->getMaxminSystem(), constraint(), &elem))) {
376     action = static_cast<L07Action*>(lmm_variable_id(var));
377     action->updateBound();
378   }
379 }
380 LinkL07::~LinkL07() = default;
381
382 /**********
383  * Action *
384  **********/
385
386 L07Action::~L07Action(){
387   delete hostList_;
388   free(communicationAmount_);
389   free(computationAmount_);
390 }
391
392 void L07Action::updateBound()
393 {
394   double lat_current = 0.0;
395
396   int hostNb = hostList_->size();
397
398   if (communicationAmount_ != nullptr) {
399     for (int i = 0; i < hostNb; i++) {
400       for (int j = 0; j < hostNb; j++) {
401
402         if (communicationAmount_[i * hostNb + j] > 0) {
403           double lat = 0.0;
404           std::vector<LinkImpl*> route;
405           hostList_->at(i)->routeTo(hostList_->at(j), &route, &lat);
406
407           lat_current = MAX(lat_current, lat * communicationAmount_[i * hostNb + j]);
408         }
409       }
410     }
411   }
412   double lat_bound = sg_tcp_gamma / (2.0 * lat_current);
413   XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
414   if ((latency_ == 0.0) && (suspended_ == 0)) {
415     if (rate_ < 0)
416       lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), lat_bound);
417     else
418       lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), std::min(rate_, lat_bound));
419   }
420 }
421
422 int L07Action::unref()
423 {
424   refcount_--;
425   if (!refcount_) {
426     if (action_hook.is_linked())
427       stateSet_->erase(stateSet_->iterator_to(*this));
428     if (getVariable())
429       lmm_variable_free(getModel()->getMaxminSystem(), getVariable());
430     delete this;
431     return 1;
432   }
433   return 0;
434 }
435
436 }
437 }