Logo AND Algorithmique Numérique Distribuée

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