Logo AND Algorithmique Numérique Distribuée

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