Logo AND Algorithmique Numérique Distribuée

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