Logo AND Algorithmique Numérique Distribuée

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