Logo AND Algorithmique Numérique Distribuée

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