Logo AND Algorithmique Numérique Distribuée

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