Logo AND Algorithmique Numérique Distribuée

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