Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
eab2dddef84c136f3a1343268d10cf15be997cd7
[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::shareResources(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
206   this->p_netcardList->reserve(host_nb);
207   for (int i = 0; i<host_nb; i++)
208           this->p_netcardList->push_back(host_list[i]->pimpl_netcard);
209
210   /* Compute the number of affected resources... */
211   if(bytes_amount != NULL) {
212     xbt_dict_t ptask_parallel_task_link_set = xbt_dict_new_homogeneous(NULL);
213
214     for (int i = 0; i < host_nb; i++) {
215       for (int j = 0; j < host_nb; j++) {
216         xbt_dynar_t route=NULL;
217
218         if (bytes_amount[i * host_nb + j] > 0) {
219           double lat=0.0;
220           unsigned int cpt;
221           void *_link;
222           LinkL07 *link;
223
224           routing_platf->getRouteAndLatency((*this->p_netcardList)[i], (*this->p_netcardList)[j],
225                                                         &route, &lat);
226           latency = MAX(latency, lat);
227
228           xbt_dynar_foreach(route, cpt, _link) {
229             link = static_cast<LinkL07*>(_link);
230             xbt_dict_set(ptask_parallel_task_link_set, link->getName(), link, NULL);
231           }
232         }
233       }
234     }
235
236     nb_link = xbt_dict_length(ptask_parallel_task_link_set);
237     xbt_dict_free(&ptask_parallel_task_link_set);
238   }
239
240   for (int i = 0; i < host_nb; i++)
241     if (flops_amount[i] > 0)
242       nb_used_host++;
243
244   XBT_DEBUG("Creating a parallel task (%p) with %d hosts and %d unique links.", this, host_nb, nb_link);
245   this->p_computationAmount = flops_amount;
246   this->p_communicationAmount = bytes_amount;
247   this->m_latency = latency;
248   this->m_rate = rate;
249
250   this->p_variable = lmm_variable_new(model->getMaxminSystem(), this, 1.0,
251       (rate > 0 ? rate : -1.0),
252       host_nb + nb_link);
253
254   if (this->m_latency > 0)
255     lmm_update_variable_weight(model->getMaxminSystem(), this->getVariable(), 0.0);
256
257   for (int i = 0; i < host_nb; i++)
258     lmm_expand(model->getMaxminSystem(), host_list[i]->pimpl_cpu->getConstraint(),
259         this->getVariable(), flops_amount[i]);
260
261   if(bytes_amount != NULL) {
262     for (int i = 0; i < host_nb; i++) {
263       for (int j = 0; j < host_nb; j++) {
264         void *_link;
265
266         xbt_dynar_t route=NULL;
267         if (bytes_amount[i * host_nb + j] == 0.0)
268           continue;
269
270         routing_platf->getRouteAndLatency((*this->p_netcardList)[i], (*this->p_netcardList)[j],
271                                                     &route, NULL);
272
273         xbt_dynar_foreach(route, cpt, _link) {
274           LinkL07 *link = static_cast<LinkL07*>(_link);
275           lmm_expand_add(model->getMaxminSystem(), link->getConstraint(),
276                         this->getVariable(), bytes_amount[i * host_nb + j]);
277         }
278       }
279     }
280   }
281
282   if (nb_link + nb_used_host == 0) {
283     this->setCost(1.0);
284     this->setRemains(0.0);
285   }
286   xbt_free(host_list);
287 }
288
289 Action *NetworkL07Model::communicate(NetCard *src, NetCard *dst,
290                                        double size, double rate)
291 {
292   sg_host_t*host_list = xbt_new0(sg_host_t, 2);
293   double *flops_amount = xbt_new0(double, 2);
294   double *bytes_amount = xbt_new0(double, 4);
295   Action *res = NULL;
296
297   host_list[0] = sg_host_by_name(src->getName());
298   host_list[1] = sg_host_by_name(dst->getName());
299   bytes_amount[1] = size;
300
301   res = p_hostModel->executeParallelTask(2, host_list, flops_amount, bytes_amount, rate);
302
303   return res;
304 }
305
306 Cpu *CpuL07Model::createCpu(simgrid::s4u::Host *host,  xbt_dynar_t powerPeakList,
307                           int pstate, double power_scale,
308                           tmgr_trace_t power_trace, int core,
309                           int initiallyOn,
310                           tmgr_trace_t state_trace)
311 {
312   CpuL07 *cpu = new CpuL07(this, host, powerPeakList, pstate, power_scale, power_trace,
313                          core, initiallyOn, state_trace);
314   return cpu;
315 }
316
317 Link* NetworkL07Model::createLink(const char *name,
318                                  double bw_initial,
319                                  tmgr_trace_t bw_trace,
320                                  double lat_initial,
321                                  tmgr_trace_t lat_trace,
322                                  int initiallyOn,
323                                  tmgr_trace_t state_trace,
324                                  e_surf_link_sharing_policy_t policy,
325                                  xbt_dict_t properties)
326 {
327   xbt_assert(!Link::byName(name),
328                  "Link '%s' declared several times in the platform file.", name);
329
330   Link* link = new LinkL07(this, name, properties,
331                              bw_initial, bw_trace,
332                                          lat_initial, lat_trace,
333                                          initiallyOn, state_trace,
334                                          policy);
335   Link::onCreation(link);
336   return link;
337 }
338
339 void HostL07Model::addTraces()
340 {
341   xbt_dict_cursor_t cursor = NULL;
342   char *trace_name, *elm;
343
344   if (!trace_connect_list_host_speed)
345     return;
346
347   /* Connect traces relative to cpu */
348   xbt_dict_foreach(trace_connect_list_host_speed, cursor, trace_name, elm) {
349     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
350     Cpu *cpu = sg_host_by_name(elm)->pimpl_cpu;
351
352     xbt_assert(cpu, "Host %s undefined", elm);
353     xbt_assert(trace, "Trace %s undefined", trace_name);
354
355     cpu->set_speed_trace(trace);
356   }
357
358   /* Connect traces relative to network */
359   xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
360     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
361     LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
362
363     xbt_assert(link, "Link %s undefined", elm);
364     xbt_assert(trace, "Trace %s undefined", trace_name);
365
366     link->p_stateEvent = future_evt_set->add_trace(trace, 0.0, link);
367   }
368
369   xbt_dict_foreach(trace_connect_list_link_bw, cursor, trace_name, elm) {
370     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
371     LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
372
373     xbt_assert(link, "Link %s undefined", elm);
374     xbt_assert(trace, "Trace %s undefined", trace_name);
375
376     link->p_bwEvent = future_evt_set->add_trace(trace, 0.0, link);
377   }
378
379   xbt_dict_foreach(trace_connect_list_link_lat, cursor, trace_name, elm) {
380     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
381     LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
382
383     xbt_assert(link, "Link %s undefined", elm);
384     xbt_assert(trace, "Trace %s undefined", trace_name);
385
386     link->p_latEvent = future_evt_set->add_trace(trace, 0.0, link);
387   }
388 }
389
390 /************
391  * Resource *
392  ************/
393
394 CpuL07::CpuL07(CpuL07Model *model, simgrid::s4u::Host *host,
395                      xbt_dynar_t speedPeakList, int pstate,
396                                  double speedScale, tmgr_trace_t speedTrace,
397                          int core, int initiallyOn, tmgr_trace_t state_trace)
398  : Cpu(model, host, speedPeakList, pstate,
399            core, xbt_dynar_get_as(speedPeakList,pstate,double), speedScale, initiallyOn)
400 {
401   p_constraint = lmm_constraint_new(model->getMaxminSystem(), this, xbt_dynar_get_as(speedPeakList,pstate,double) * speedScale);
402
403   if (speedTrace)
404     p_speedEvent = future_evt_set->add_trace(speedTrace, 0.0, this);
405
406   if (state_trace)
407     p_stateEvent = future_evt_set->add_trace(state_trace, 0.0, this);
408 }
409
410 CpuL07::~CpuL07()
411 {
412 }
413
414 LinkL07::LinkL07(NetworkL07Model *model, const char* name, xbt_dict_t props,
415                          double bw_initial,
416                          tmgr_trace_t bw_trace,
417                          double lat_initial,
418                          tmgr_trace_t lat_trace,
419                          int initiallyOn,
420                          tmgr_trace_t state_trace,
421                          e_surf_link_sharing_policy_t policy)
422  : Link(model, name, props, lmm_constraint_new(model->getMaxminSystem(), this, bw_initial), future_evt_set, state_trace)
423 {
424   m_bwCurrent = bw_initial;
425   if (bw_trace)
426     p_bwEvent = future_evt_set->add_trace(bw_trace, 0.0, this);
427
428   if (initiallyOn)
429     turnOn();
430   else
431     turnOff();
432   m_latCurrent = lat_initial;
433
434   if (lat_trace)
435     p_latEvent = future_evt_set->add_trace(lat_trace, 0.0, this);
436
437   if (policy == SURF_LINK_FATPIPE)
438         lmm_constraint_shared(getConstraint());
439 }
440
441 Action *CpuL07::execution_start(double size)
442 {
443   sg_host_t*host_list = xbt_new0(sg_host_t, 1);
444   double *flops_amount = xbt_new0(double, 1);
445
446   host_list[0] = getHost();
447   flops_amount[0] = size;
448
449   return static_cast<CpuL07Model*>(getModel())->p_hostModel
450     ->executeParallelTask( 1, host_list, flops_amount, NULL, -1);
451 }
452
453 Action *CpuL07::sleep(double duration)
454 {
455   L07Action *action = static_cast<L07Action*>(execution_start(1.0));
456   action->m_maxDuration = duration;
457   action->m_suspended = 2;
458   lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), 0.0);
459
460   return action;
461 }
462
463 bool CpuL07::isUsed(){
464   return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
465 }
466
467 /** @brief take into account changes of speed (either load or max) */
468 void CpuL07::onSpeedChange() {
469         lmm_variable_t var = NULL;
470         lmm_element_t elem = NULL;
471
472     lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(), m_speedPeak * m_speedScale);
473     while ((var = lmm_get_var_from_cnst
474             (getModel()->getMaxminSystem(), getConstraint(), &elem))) {
475       Action *action = static_cast<Action*>(lmm_variable_id(var));
476
477       lmm_update_variable_bound(getModel()->getMaxminSystem(),
478                                 action->getVariable(),
479                                 m_speedScale * m_speedPeak);
480     }
481
482         Cpu::onSpeedChange();
483 }
484
485
486 bool LinkL07::isUsed(){
487   return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
488 }
489
490 void CpuL07::updateState(tmgr_trace_iterator_t triggered, double value, double /*date*/){
491   XBT_DEBUG("Updating cpu %s (%p) with value %g", getName(), this, value);
492   if (triggered == p_speedEvent) {
493     m_speedScale = value;
494     onSpeedChange();
495     tmgr_trace_event_unref(&p_speedEvent);
496   } else if (triggered == p_stateEvent) {
497     if (value > 0)
498       turnOn();
499     else
500       turnOff();
501     tmgr_trace_event_unref(&p_stateEvent);
502   } else {
503     xbt_die("Unknown event!\n");
504   }
505 }
506
507 void LinkL07::updateState(tmgr_trace_iterator_t triggered, double value, double date) {
508   XBT_DEBUG("Updating link %s (%p) with value=%f for date=%g", getName(), this, value, date);
509   if (triggered == p_bwEvent) {
510     updateBandwidth(value, date);
511     tmgr_trace_event_unref(&p_bwEvent);
512   } else if (triggered == p_latEvent) {
513     updateLatency(value, date);
514     tmgr_trace_event_unref(&p_latEvent);
515   } else if (triggered == p_stateEvent) {
516     if (value > 0)
517       turnOn();
518     else
519       turnOff();
520     tmgr_trace_event_unref(&p_stateEvent);
521   } else {
522     xbt_die("Unknown event ! \n");
523   }
524 }
525
526 double LinkL07::getBandwidth()
527 {
528   return m_bwCurrent;
529 }
530
531 void LinkL07::updateBandwidth(double value, double date)
532 {
533   m_bwCurrent = value;
534   lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(), m_bwCurrent);
535 }
536
537 void LinkL07::updateLatency(double value, double date)
538 {
539   lmm_variable_t var = NULL;
540   L07Action *action;
541   lmm_element_t elem = NULL;
542
543   m_latCurrent = value;
544   while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), getConstraint(), &elem))) {
545     action = static_cast<L07Action*>(lmm_variable_id(var));
546     action->updateBound();
547   }
548 }
549
550 /**********
551  * Action *
552  **********/
553
554 L07Action::~L07Action(){
555   free(p_communicationAmount);
556   free(p_computationAmount);
557 }
558
559 void L07Action::updateBound()
560 {
561   double lat_current = 0.0;
562   double lat_bound = -1.0;
563   int i, j;
564
565   int hostNb = p_netcardList->size();
566
567   if (p_communicationAmount != NULL) {
568     for (i = 0; i < hostNb; i++) {
569       for (j = 0; j < hostNb; j++) {
570         xbt_dynar_t route=NULL;
571
572         if (p_communicationAmount[i * hostNb + j] > 0) {
573           double lat = 0.0;
574           routing_platf->getRouteAndLatency((*p_netcardList)[i], (*p_netcardList)[j],
575                                                                 &route, &lat);
576
577           lat_current = MAX(lat_current, lat * p_communicationAmount[i * hostNb + j]);
578         }
579       }
580     }
581   }
582   lat_bound = sg_tcp_gamma / (2.0 * lat_current);
583   XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
584   if ((m_latency == 0.0) && (m_suspended == 0)) {
585     if (m_rate < 0)
586       lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), lat_bound);
587     else
588       lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(),
589         std::min(m_rate, lat_bound));
590   }
591 }
592
593 int L07Action::unref()
594 {
595   m_refcount--;
596   if (!m_refcount) {
597     if (action_hook.is_linked())
598       p_stateSet->erase(p_stateSet->iterator_to(*this));
599     if (getVariable())
600       lmm_variable_free(getModel()->getMaxminSystem(), getVariable());
601     delete this;
602     return 1;
603   }
604   return 0;
605 }
606
607 }
608 }