Logo AND Algorithmique Numérique Distribuée

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