Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill (inline) useless functions
[simgrid.git] / src / surf / host_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
11 #include "host_ptask_L07.hpp"
12
13 #include "cpu_interface.hpp"
14 #include "surf_routing.hpp"
15 #include "xbt/lib.h"
16 #include "src/surf/platform.hpp"
17
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_host);
19
20 /**************************************/
21 /*** Resource Creation & Destruction **/
22 /**************************************/
23
24 static void ptask_netlink_parse_init(sg_platf_link_cbarg_t link)
25 {
26   netlink_parse_init(link);
27   current_property_set = NULL;
28 }
29
30 void surf_host_model_init_ptask_L07(void)
31 {
32   XBT_INFO("Switching to the L07 model to handle parallel tasks.");
33   xbt_assert(!surf_cpu_model_pm, "CPU model type already defined");
34   xbt_assert(!surf_network_model, "network model type already defined");
35
36   // Define the callbacks to parse the XML
37   simgrid::surf::on_link.connect(ptask_netlink_parse_init);
38   simgrid::surf::on_postparse.connect([](){
39       surf_host_model->addTraces();
40   });
41
42   surf_host_model = new simgrid::surf::HostL07Model();
43   xbt_dynar_push(all_existing_models, &surf_host_model);
44 }
45
46
47 namespace simgrid {
48 namespace surf {
49
50 HostL07Model::HostL07Model() : HostModel() {
51   p_maxminSystem = lmm_system_new(1);
52   surf_network_model = new NetworkL07Model(this,p_maxminSystem);
53   surf_cpu_model_pm = new CpuL07Model(this,p_maxminSystem);
54
55   routing_model_create(surf_network_model->createLink("__loopback__",
56                                                           498000000, NULL,
57                                                           0.000015, NULL,
58                                                           1/*ON*/, NULL,
59                                                           SURF_LINK_FATPIPE, NULL));
60 }
61
62 HostL07Model::~HostL07Model() {
63   delete surf_cpu_model_pm;
64   delete surf_network_model;
65 }
66
67 CpuL07Model::CpuL07Model(HostL07Model *hmodel,lmm_system_t sys)
68         : CpuModel()
69         , p_hostModel(hmodel)
70         {
71           p_maxminSystem = sys;
72         }
73 CpuL07Model::~CpuL07Model() {
74         surf_cpu_model_pm = NULL;
75         p_maxminSystem = NULL; // Avoid multi-free
76 }
77 NetworkL07Model::NetworkL07Model(HostL07Model *hmodel, lmm_system_t sys)
78         : NetworkModel()
79         , p_hostModel(hmodel)
80         {
81           p_maxminSystem = sys;
82         }
83 NetworkL07Model::~NetworkL07Model()
84 {
85         surf_network_model = NULL;
86         p_maxminSystem = NULL; // Avoid multi-free
87 }
88
89
90 double HostL07Model::shareResources(double /*now*/)
91 {
92   L07Action *action;
93
94   ActionList *running_actions = getRunningActionSet();
95   double min = this->shareResourcesMaxMin(running_actions,
96                                               p_maxminSystem,
97                                               bottleneck_solve);
98
99   for(ActionList::iterator it(running_actions->begin()), itend(running_actions->end())
100          ; it != itend ; ++it) {
101         action = static_cast<L07Action*>(&*it);
102     if (action->m_latency > 0) {
103       if (min < 0) {
104         min = action->m_latency;
105         XBT_DEBUG("Updating min (value) with %p (start %f): %f", action,
106                action->getStartTime(), min);
107       } else if (action->m_latency < min) {
108         min = action->m_latency;
109         XBT_DEBUG("Updating min (latency) with %p (start %f): %f", action,
110                action->getStartTime(), min);
111       }
112     }
113   }
114
115   XBT_DEBUG("min value : %f", min);
116
117   return min;
118 }
119
120 void HostL07Model::updateActionsState(double /*now*/, double delta) {
121
122   L07Action *action;
123   ActionList *actionSet = getRunningActionSet();
124
125   for(ActionList::iterator it = actionSet->begin(), itNext = it
126          ; it != actionSet->end()
127          ; it =  itNext) {
128         ++itNext;
129     action = static_cast<L07Action*>(&*it);
130     if (action->m_latency > 0) {
131       if (action->m_latency > delta) {
132         double_update(&(action->m_latency), delta, sg_surf_precision);
133       } else {
134         action->m_latency = 0.0;
135       }
136       if ((action->m_latency == 0.0) && (action->isSuspended() == 0)) {
137         action->updateBound();
138         lmm_update_variable_weight(p_maxminSystem, action->getVariable(), 1.0);
139       }
140     }
141     XBT_DEBUG("Action (%p) : remains (%g) updated by %g.",
142            action, action->getRemains(), lmm_variable_getvalue(action->getVariable()) * delta);
143     action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
144
145     if (action->getMaxDuration() != NO_MAX_DURATION)
146       action->updateMaxDuration(delta);
147
148     XBT_DEBUG("Action (%p) : remains (%g).", action, action->getRemains());
149
150     /* In the next if cascade, the action can be finished either because:
151      *  - The amount of remaining work reached 0
152      *  - The max duration was reached
153      * If it's not done, it may have failed.
154      */
155
156     if ((action->getRemains() <= 0) &&
157         (lmm_get_variable_weight(action->getVariable()) > 0)) {
158       action->finish();
159       action->setState(SURF_ACTION_DONE);
160     } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
161                (action->getMaxDuration() <= 0)) {
162       action->finish();
163       action->setState(SURF_ACTION_DONE);
164     } else {
165       /* Need to check that none of the model has failed */
166       lmm_constraint_t cnst = NULL;
167       int i = 0;
168
169       while ((cnst = lmm_get_cnst_from_var(p_maxminSystem, action->getVariable(), i++))) {
170         void *constraint_id = lmm_constraint_id(cnst);
171
172         if (static_cast<Host*>(constraint_id)->isOff()) {
173           XBT_DEBUG("Action (%p) Failed!!", action);
174           action->finish();
175           action->setState(SURF_ACTION_FAILED);
176           break;
177         }
178       }
179     }
180   }
181   return;
182 }
183
184 Action *HostL07Model::executeParallelTask(int host_nb, sg_host_t *host_list,
185                   double *flops_amount, double *bytes_amount,
186                   double rate) {
187         return new L07Action(this, host_nb, host_list, flops_amount, bytes_amount, rate);
188 }
189
190
191 L07Action::L07Action(Model *model, int host_nb,
192                 sg_host_t*host_list,
193                 double *flops_amount,
194                 double *bytes_amount,
195                 double rate)
196         : CpuAction(model, 1, 0)
197 {
198   unsigned int cpt;
199   int nb_link = 0;
200   int nb_used_host = 0; /* Only the hosts with something to compute (>0 flops) are counted) */
201   double latency = 0.0;
202
203   xbt_dict_t ptask_parallel_task_link_set = xbt_dict_new_homogeneous(NULL);
204
205   this->p_netcardList->reserve(host_nb);
206   for (int i = 0; i<host_nb; i++)
207           this->p_netcardList->push_back(host_list[i]->pimpl_netcard);
208
209   /* Compute the number of affected resources... */
210   for (int i = 0; i < host_nb; i++) {
211     for (int j = 0; j < host_nb; j++) {
212       xbt_dynar_t route=NULL;
213
214       if (bytes_amount[i * host_nb + j] > 0) {
215         double lat=0.0;
216         unsigned int cpt;
217         void *_link;
218         LinkL07 *link;
219
220         routing_platf->getRouteAndLatency((*this->p_netcardList)[i], (*this->p_netcardList)[j],
221                                                   &route, &lat);
222         latency = MAX(latency, lat);
223
224         xbt_dynar_foreach(route, cpt, _link) {
225            link = static_cast<LinkL07*>(_link);
226            xbt_dict_set(ptask_parallel_task_link_set, link->getName(), link, NULL);
227         }
228       }
229     }
230   }
231
232   nb_link = xbt_dict_length(ptask_parallel_task_link_set);
233   xbt_dict_free(&ptask_parallel_task_link_set);
234
235   for (int i = 0; i < host_nb; i++)
236     if (flops_amount[i] > 0)
237       nb_used_host++;
238
239   XBT_DEBUG("Creating a parallel task (%p) with %d cpus and %d links.",
240          this, host_nb, nb_link);
241   this->p_computationAmount = flops_amount;
242   this->p_communicationAmount = bytes_amount;
243   this->m_latency = latency;
244   this->m_rate = rate;
245
246   this->p_variable = lmm_variable_new(model->getMaxminSystem(), this, 1.0,
247                                         (rate > 0 ? rate : -1.0),
248                                                                                 host_nb + nb_link);
249
250   if (this->m_latency > 0)
251     lmm_update_variable_weight(model->getMaxminSystem(), this->getVariable(), 0.0);
252
253   for (int i = 0; i < host_nb; i++)
254     lmm_expand(model->getMaxminSystem(),
255                host_list[i]->pimpl_cpu->getConstraint(),
256                this->getVariable(), flops_amount[i]);
257
258   for (int i = 0; i < host_nb; i++) {
259     for (int j = 0; j < host_nb; j++) {
260       void *_link;
261
262       xbt_dynar_t route=NULL;
263       if (bytes_amount[i * host_nb + j] == 0.0)
264         continue;
265
266       routing_platf->getRouteAndLatency((*this->p_netcardList)[i], (*this->p_netcardList)[j],
267                                             &route, NULL);
268
269       xbt_dynar_foreach(route, cpt, _link) {
270         LinkL07 *link = static_cast<LinkL07*>(_link);
271         lmm_expand_add(model->getMaxminSystem(), link->getConstraint(),
272                        this->getVariable(),
273                        bytes_amount[i * host_nb + j]);
274       }
275     }
276   }
277
278   if (nb_link + nb_used_host == 0) {
279     this->setCost(1.0);
280     this->setRemains(0.0);
281   }
282 }
283
284 Action *NetworkL07Model::communicate(NetCard *src, NetCard *dst,
285                                        double size, double rate)
286 {
287   sg_host_t*host_list = xbt_new0(sg_host_t, 2);
288   double *flops_amount = xbt_new0(double, 2);
289   double *bytes_amount = xbt_new0(double, 4);
290   Action *res = NULL;
291
292   host_list[0] = sg_host_by_name(src->getName());
293   host_list[1] = sg_host_by_name(dst->getName());
294   bytes_amount[1] = size;
295
296   res = p_hostModel->executeParallelTask(2, host_list,
297                                     flops_amount,
298                                     bytes_amount, rate);
299
300   return res;
301 }
302
303 Cpu *CpuL07Model::createCpu(simgrid::s4u::Host *host,  xbt_dynar_t powerPeakList,
304                           int pstate, double power_scale,
305                           tmgr_trace_t power_trace, int core,
306                           int initiallyOn,
307                           tmgr_trace_t state_trace)
308 {
309   CpuL07 *cpu = new CpuL07(this, host, powerPeakList, pstate, power_scale, power_trace,
310                          core, initiallyOn, state_trace);
311   return cpu;
312 }
313
314 Link* NetworkL07Model::createLink(const char *name,
315                                  double bw_initial,
316                                  tmgr_trace_t bw_trace,
317                                  double lat_initial,
318                                  tmgr_trace_t lat_trace,
319                                  int initiallyOn,
320                                  tmgr_trace_t state_trace,
321                                  e_surf_link_sharing_policy_t policy,
322                                  xbt_dict_t properties)
323 {
324   xbt_assert(!Link::byName(name),
325                  "Link '%s' declared several times in the platform file.", name);
326
327   Link* link = new LinkL07(this, name, properties,
328                              bw_initial, bw_trace,
329                                          lat_initial, lat_trace,
330                                          initiallyOn, state_trace,
331                                          policy);
332   Link::onCreation(link);
333   return link;
334 }
335
336 void HostL07Model::addTraces()
337 {
338   xbt_dict_cursor_t cursor = NULL;
339   char *trace_name, *elm;
340
341   if (!trace_connect_list_host_avail)
342     return;
343
344   /* Connect traces relative to cpu */
345   xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
346     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
347     CpuL07 *host = static_cast<CpuL07*>(sg_host_by_name(elm)->pimpl_cpu);
348
349     xbt_assert(host, "Host %s undefined", elm);
350     xbt_assert(trace, "Trace %s undefined", trace_name);
351
352     host->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, host);
353   }
354
355   xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
356     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
357     CpuL07 *host = static_cast<CpuL07*>(sg_host_by_name(elm)->pimpl_cpu);
358
359     xbt_assert(host, "Host %s undefined", elm);
360     xbt_assert(trace, "Trace %s undefined", trace_name);
361
362     host->p_speedEvent = tmgr_history_add_trace(history, trace, 0.0, 0, host);
363   }
364
365   /* Connect traces relative to network */
366   xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
367     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
368     LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
369
370     xbt_assert(link, "Link %s undefined", elm);
371     xbt_assert(trace, "Trace %s undefined", trace_name);
372
373     link->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
374   }
375
376   xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
377     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
378     LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
379
380     xbt_assert(link, "Link %s undefined", elm);
381     xbt_assert(trace, "Trace %s undefined", trace_name);
382
383     link->p_bwEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
384   }
385
386   xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
387     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
388     LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
389
390     xbt_assert(link, "Link %s undefined", elm);
391     xbt_assert(trace, "Trace %s undefined", trace_name);
392
393     link->p_latEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
394   }
395 }
396
397 /************
398  * Resource *
399  ************/
400
401 CpuL07::CpuL07(CpuL07Model *model, simgrid::s4u::Host *host,
402                      xbt_dynar_t speedPeakList, int pstate,
403                                  double speedScale, tmgr_trace_t speedTrace,
404                          int core, int initiallyOn, tmgr_trace_t state_trace)
405  : Cpu(model, host, speedPeakList, pstate,
406            core, xbt_dynar_get_as(speedPeakList,pstate,double), speedScale, initiallyOn)
407 {
408   p_constraint = lmm_constraint_new(model->getMaxminSystem(), this, xbt_dynar_get_as(speedPeakList,pstate,double) * speedScale);
409
410   if (speedTrace)
411     p_speedEvent = tmgr_history_add_trace(history, speedTrace, 0.0, 0, this);
412   else
413     p_speedEvent = NULL;
414
415   if (state_trace)
416         p_stateEvent = tmgr_history_add_trace(history, state_trace, 0.0, 0, this);
417 }
418
419 CpuL07::~CpuL07()
420 {
421 }
422
423 LinkL07::LinkL07(NetworkL07Model *model, const char* name, xbt_dict_t props,
424                          double bw_initial,
425                          tmgr_trace_t bw_trace,
426                          double lat_initial,
427                          tmgr_trace_t lat_trace,
428                          int initiallyOn,
429                          tmgr_trace_t state_trace,
430                          e_surf_link_sharing_policy_t policy)
431  : Link(model, name, props, lmm_constraint_new(model->getMaxminSystem(), this, bw_initial), history, state_trace)
432 {
433   m_bwCurrent = bw_initial;
434   if (bw_trace)
435     p_bwEvent = tmgr_history_add_trace(history, bw_trace, 0.0, 0, this);
436
437   if (initiallyOn)
438     turnOn();
439   else
440     turnOff();
441   m_latCurrent = lat_initial;
442
443   if (lat_trace)
444         p_latEvent = tmgr_history_add_trace(history, lat_trace, 0.0, 0, this);
445
446   if (policy == SURF_LINK_FATPIPE)
447         lmm_constraint_shared(getConstraint());
448 }
449
450 Action *CpuL07::execute(double size)
451 {
452   sg_host_t*host_list = xbt_new0(sg_host_t, 1);
453   double *flops_amount = xbt_new0(double, 1);
454   double *bytes_amount = xbt_new0(double, 1);
455
456   host_list[0] = getHost();
457   flops_amount[0] = size;
458
459   return static_cast<CpuL07Model*>(getModel())
460     ->p_hostModel
461     ->executeParallelTask( 1, host_list, flops_amount, bytes_amount, -1);
462 }
463
464 Action *CpuL07::sleep(double duration)
465 {
466   L07Action *action = NULL;
467
468   XBT_IN("(%s,%g)", getName(), duration);
469
470   action = static_cast<L07Action*>(execute(1.0));
471   action->m_maxDuration = duration;
472   action->m_suspended = 2;
473   lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), 0.0);
474
475   XBT_OUT();
476   return action;
477 }
478
479 bool CpuL07::isUsed(){
480   return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
481 }
482
483 /** @brief take into account changes of speed (either load or max) */
484 void CpuL07::onSpeedChange() {
485         lmm_variable_t var = NULL;
486         lmm_element_t elem = NULL;
487
488     lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(), m_speedPeak * m_speedScale);
489     while ((var = lmm_get_var_from_cnst
490             (getModel()->getMaxminSystem(), getConstraint(), &elem))) {
491       Action *action = static_cast<Action*>(lmm_variable_id(var));
492
493       lmm_update_variable_bound(getModel()->getMaxminSystem(),
494                                 action->getVariable(),
495                                 m_speedScale * m_speedPeak);
496     }
497
498         Cpu::onSpeedChange();
499 }
500
501
502 bool LinkL07::isUsed(){
503   return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
504 }
505
506 void CpuL07::updateState(tmgr_trace_event_t event_type, double value, double /*date*/){
507   XBT_DEBUG("Updating cpu %s (%p) with value %g", getName(), this, value);
508   if (event_type == p_speedEvent) {
509         m_speedScale = value;
510         onSpeedChange();
511     if (tmgr_trace_event_free(event_type))
512       p_speedEvent = NULL;
513   } else if (event_type == p_stateEvent) {
514     if (value > 0)
515       turnOn();
516     else
517       turnOff();
518     if (tmgr_trace_event_free(event_type))
519       p_stateEvent = NULL;
520   } else {
521     XBT_CRITICAL("Unknown event ! \n");
522     xbt_abort();
523   }
524   return;
525 }
526
527 void LinkL07::updateState(tmgr_trace_event_t event_type, double value, double date) {
528   XBT_DEBUG("Updating link %s (%p) with value=%f for date=%g", getName(), this, value, date);
529   if (event_type == p_bwEvent) {
530     updateBandwidth(value, date);
531     if (tmgr_trace_event_free(event_type))
532       p_bwEvent = NULL;
533   } else if (event_type == p_latEvent) {
534     updateLatency(value, date);
535     if (tmgr_trace_event_free(event_type))
536       p_latEvent = NULL;
537   } else if (event_type == p_stateEvent) {
538     if (value > 0)
539       turnOn();
540     else
541       turnOff();
542     if (tmgr_trace_event_free(event_type))
543       p_stateEvent = NULL;
544   } else {
545     XBT_CRITICAL("Unknown event ! \n");
546     xbt_abort();
547   }
548   return;
549 }
550
551 double LinkL07::getBandwidth()
552 {
553   return m_bwCurrent;
554 }
555
556 void LinkL07::updateBandwidth(double value, double date)
557 {
558   m_bwCurrent = value;
559   lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(), m_bwCurrent);
560 }
561
562 void LinkL07::updateLatency(double value, double date)
563 {
564   lmm_variable_t var = NULL;
565   L07Action *action;
566   lmm_element_t elem = NULL;
567
568   m_latCurrent = value;
569   while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), getConstraint(), &elem))) {
570     action = static_cast<L07Action*>(lmm_variable_id(var));
571     action->updateBound();
572   }
573 }
574
575 /**********
576  * Action *
577  **********/
578
579 L07Action::~L07Action(){
580   free(p_communicationAmount);
581   free(p_computationAmount);
582 }
583
584 void L07Action::updateBound()
585 {
586   double lat_current = 0.0;
587   double lat_bound = -1.0;
588   int i, j;
589
590   int hostNb = p_netcardList->size();
591
592   for (i = 0; i < hostNb; i++) {
593     for (j = 0; j < hostNb; j++) {
594       xbt_dynar_t route=NULL;
595
596       if (p_communicationAmount[i * hostNb + j] > 0) {
597         double lat = 0.0;
598         routing_platf->getRouteAndLatency((*p_netcardList)[i], (*p_netcardList)[j],
599                                                           &route, &lat);
600
601         lat_current = MAX(lat_current, lat * p_communicationAmount[i * hostNb + j]);
602       }
603     }
604   }
605   lat_bound = sg_tcp_gamma / (2.0 * lat_current);
606   XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
607   if ((m_latency == 0.0) && (m_suspended == 0)) {
608     if (m_rate < 0)
609       lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), lat_bound);
610     else
611       lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(),
612         std::min(m_rate, lat_bound));
613   }
614 }
615
616 int L07Action::unref()
617 {
618   m_refcount--;
619   if (!m_refcount) {
620     if (action_hook.is_linked())
621           p_stateSet->erase(p_stateSet->iterator_to(*this));
622     if (getVariable())
623       lmm_variable_free(getModel()->getMaxminSystem(), getVariable());
624     delete this;
625     return 1;
626   }
627   return 0;
628 }
629
630 void L07Action::suspend()
631 {
632   XBT_IN("(%p))", this);
633   if (m_suspended != 2) {
634     m_suspended = 1;
635     lmm_update_variable_weight(getModel()->getMaxminSystem(), getVariable(), 0.0);
636   }
637   XBT_OUT();
638 }
639
640 void L07Action::resume()
641 {
642   XBT_IN("(%p)", this);
643   if (m_suspended != 2) {
644     lmm_update_variable_weight(getModel()->getMaxminSystem(), getVariable(), 1.0);
645     m_suspended = 0;
646   }
647   XBT_OUT();
648 }
649
650 void L07Action::setMaxDuration(double duration)
651 {                               /* FIXME: should inherit */
652   XBT_IN("(%p,%g)", this, duration);
653   m_maxDuration = duration;
654   XBT_OUT();
655 }
656
657 void L07Action::setPriority(double priority)
658 {                               /* FIXME: should inherit */
659   XBT_IN("(%p,%g)", this, priority);
660   m_priority = priority;
661   XBT_OUT();
662 }
663
664 double L07Action::getRemains()
665 {
666   XBT_IN("(%p)", this);
667   XBT_OUT();
668   return m_remains;
669 }
670
671 }
672 }