Logo AND Algorithmique Numérique Distribuée

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