Logo AND Algorithmique Numérique Distribuée

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