Logo AND Algorithmique Numérique Distribuée

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