Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
make the rest of trace:connect private to the XML parsing
[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
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   lmm_system_free(p_maxminSystem);
74   p_maxminSystem = NULL;
75 }
76 NetworkL07Model::NetworkL07Model(HostL07Model *hmodel, lmm_system_t sys)
77   : NetworkModel()
78   , p_hostModel(hmodel)
79   {
80     p_maxminSystem = sys;
81   }
82 NetworkL07Model::~NetworkL07Model()
83 {
84   surf_network_model = NULL;
85   p_maxminSystem = NULL; // Avoid multi-free
86 }
87
88
89 double HostL07Model::next_occuring_event(double /*now*/)
90 {
91   L07Action *action;
92
93   ActionList *running_actions = getRunningActionSet();
94   double min = this->shareResourcesMaxMin(running_actions,
95                                               p_maxminSystem,
96                                               bottleneck_solve);
97
98   for(ActionList::iterator it(running_actions->begin()), itend(running_actions->end())
99    ; it != itend ; ++it) {
100   action = static_cast<L07Action*>(&*it);
101     if (action->m_latency > 0) {
102       if (min < 0) {
103         min = action->m_latency;
104         XBT_DEBUG("Updating min (value) with %p (start %f): %f", action,
105                action->getStartTime(), min);
106       } else if (action->m_latency < min) {
107         min = action->m_latency;
108         XBT_DEBUG("Updating min (latency) with %p (start %f): %f", action,
109                action->getStartTime(), min);
110       }
111     }
112   }
113
114   XBT_DEBUG("min value : %f", min);
115
116   return min;
117 }
118
119 void HostL07Model::updateActionsState(double /*now*/, double delta) {
120
121   L07Action *action;
122   ActionList *actionSet = getRunningActionSet();
123
124   for(ActionList::iterator it = actionSet->begin(), itNext = it
125    ; it != actionSet->end()
126    ; it =  itNext) {
127   ++itNext;
128     action = static_cast<L07Action*>(&*it);
129     if (action->m_latency > 0) {
130       if (action->m_latency > delta) {
131         double_update(&(action->m_latency), delta, sg_surf_precision);
132       } else {
133         action->m_latency = 0.0;
134       }
135       if ((action->m_latency == 0.0) && (action->isSuspended() == 0)) {
136         action->updateBound();
137         lmm_update_variable_weight(p_maxminSystem, action->getVariable(), 1.0);
138       }
139     }
140     XBT_DEBUG("Action (%p) : remains (%g) updated by %g.",
141            action, action->getRemains(), lmm_variable_getvalue(action->getVariable()) * delta);
142     action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
143
144     if (action->getMaxDuration() != NO_MAX_DURATION)
145       action->updateMaxDuration(delta);
146
147     XBT_DEBUG("Action (%p) : remains (%g).", action, action->getRemains());
148
149     /* In the next if cascade, the action can be finished either because:
150      *  - The amount of remaining work reached 0
151      *  - The max duration was reached
152      * If it's not done, it may have failed.
153      */
154
155     if ((action->getRemains() <= 0) &&
156         (lmm_get_variable_weight(action->getVariable()) > 0)) {
157       action->finish();
158       action->setState(SURF_ACTION_DONE);
159     } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
160                (action->getMaxDuration() <= 0)) {
161       action->finish();
162       action->setState(SURF_ACTION_DONE);
163     } else {
164       /* Need to check that none of the model has failed */
165       lmm_constraint_t cnst = NULL;
166       int i = 0;
167
168       while ((cnst = lmm_get_cnst_from_var(p_maxminSystem, action->getVariable(), i++))) {
169         void *constraint_id = lmm_constraint_id(cnst);
170
171         if (static_cast<Host*>(constraint_id)->isOff()) {
172           XBT_DEBUG("Action (%p) Failed!!", action);
173           action->finish();
174           action->setState(SURF_ACTION_FAILED);
175           break;
176         }
177       }
178     }
179   }
180   return;
181 }
182
183 Action *HostL07Model::executeParallelTask(int host_nb, sg_host_t *host_list,
184       double *flops_amount, double *bytes_amount,
185       double rate) {
186   return new L07Action(this, host_nb, host_list, flops_amount, bytes_amount, rate);
187 }
188
189
190 L07Action::L07Action(Model *model, int host_nb,
191     sg_host_t*host_list,
192     double *flops_amount,
193     double *bytes_amount,
194     double rate)
195   : CpuAction(model, 1, 0)
196 {
197   unsigned int cpt;
198   int nb_link = 0;
199   int nb_used_host = 0; /* Only the hosts with something to compute (>0 flops) are counted) */
200   double latency = 0.0;
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   if(bytes_amount != NULL) {
208     xbt_dict_t ptask_parallel_task_link_set = xbt_dict_new_homogeneous(NULL);
209
210     for (int i = 0; i < host_nb; i++) {
211       for (int j = 0; j < host_nb; j++) {
212
213         if (bytes_amount[i * host_nb + j] > 0) {
214           double lat=0.0;
215           xbt_dynar_t route=NULL;
216
217           routing_platf->getRouteAndLatency((*p_netcardList)[i], (*p_netcardList)[j], &route, &lat);
218           latency = MAX(latency, lat);
219
220           void *_link;
221           xbt_dynar_foreach(route, cpt, _link) {
222             LinkL07 *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
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 hosts and %d unique links.", 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(), host_list[i]->pimpl_cpu->getConstraint(),
252         this->getVariable(), flops_amount[i]);
253
254   if(bytes_amount != NULL) {
255     for (int i = 0; i < host_nb; i++) {
256       for (int j = 0; j < host_nb; j++) {
257
258         xbt_dynar_t route=NULL;
259         if (bytes_amount[i * host_nb + j] == 0.0)
260           continue;
261
262         routing_platf->getRouteAndLatency((*p_netcardList)[i], (*p_netcardList)[j],
263                                                     &route, NULL);
264
265         void *_link;
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(), bytes_amount[i * host_nb + j]);
270         }
271       }
272     }
273   }
274
275   if (nb_link + nb_used_host == 0) {
276     this->setCost(1.0);
277     this->setRemains(0.0);
278   }
279   xbt_free(host_list);
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, flops_amount, bytes_amount, rate);
295
296   return res;
297 }
298
299 Cpu *CpuL07Model::createCpu(simgrid::s4u::Host *host,  xbt_dynar_t powerPeakList,
300                           int pstate, double power_scale,
301                           tmgr_trace_t power_trace, int core,
302                           int initiallyOn,
303                           tmgr_trace_t state_trace)
304 {
305   CpuL07 *cpu = new CpuL07(this, host, powerPeakList, pstate, power_scale, power_trace,
306                          core, initiallyOn, state_trace);
307   return cpu;
308 }
309
310 Link* NetworkL07Model::createLink(const char *name,
311                                  double bw_initial,
312                                  tmgr_trace_t bw_trace,
313                                  double lat_initial,
314                                  tmgr_trace_t lat_trace,
315                                  int initiallyOn,
316                                  tmgr_trace_t state_trace,
317                                  e_surf_link_sharing_policy_t policy,
318                                  xbt_dict_t properties)
319 {
320   xbt_assert(!Link::byName(name),
321            "Link '%s' declared several times in the platform file.", name);
322
323   Link* link = new LinkL07(this, name, properties,
324                  bw_initial, bw_trace,
325            lat_initial, lat_trace,
326            initiallyOn, state_trace,
327            policy);
328   Link::onCreation(link);
329   return link;
330 }
331
332 void HostL07Model::addTraces()
333 {
334   THROW_DEADCODE;
335 }
336
337 /************
338  * Resource *
339  ************/
340
341 CpuL07::CpuL07(CpuL07Model *model, simgrid::s4u::Host *host,
342                xbt_dynar_t speedPeakList, int pstate,
343          double speedScale, tmgr_trace_t speedTrace,
344              int core, int initiallyOn, tmgr_trace_t state_trace)
345  : Cpu(model, host, speedPeakList, pstate,
346      core, xbt_dynar_get_as(speedPeakList,pstate,double), speedScale, initiallyOn)
347 {
348   p_constraint = lmm_constraint_new(model->getMaxminSystem(), this, xbt_dynar_get_as(speedPeakList,pstate,double) * speedScale);
349
350   if (speedTrace)
351     p_speed.event = future_evt_set->add_trace(speedTrace, 0.0, this);
352
353   if (state_trace)
354     p_stateEvent = future_evt_set->add_trace(state_trace, 0.0, this);
355 }
356
357 CpuL07::~CpuL07()
358 {
359 }
360
361 LinkL07::LinkL07(NetworkL07Model *model, const char* name, xbt_dict_t props,
362              double bw_initial, tmgr_trace_t bw_trace,
363              double lat_initial, tmgr_trace_t lat_trace,
364              int initiallyOn, tmgr_trace_t state_trace,
365              e_surf_link_sharing_policy_t policy)
366  : Link(model, name, props, lmm_constraint_new(model->getMaxminSystem(), this, bw_initial), state_trace)
367 {
368   m_bandwidth.peak = bw_initial;
369   if (bw_trace)
370     m_bandwidth.event = future_evt_set->add_trace(bw_trace, 0.0, this);
371
372   if (initiallyOn)
373     turnOn();
374   else
375     turnOff();
376
377   m_latency.peak = lat_initial;
378   if (lat_trace)
379     m_latency.event = future_evt_set->add_trace(lat_trace, 0.0, this);
380
381   if (policy == SURF_LINK_FATPIPE)
382   lmm_constraint_shared(getConstraint());
383 }
384
385 Action *CpuL07::execution_start(double size)
386 {
387   sg_host_t*host_list = xbt_new0(sg_host_t, 1);
388   double *flops_amount = xbt_new0(double, 1);
389
390   host_list[0] = getHost();
391   flops_amount[0] = size;
392
393   return static_cast<CpuL07Model*>(getModel())->p_hostModel
394     ->executeParallelTask( 1, host_list, flops_amount, NULL, -1);
395 }
396
397 Action *CpuL07::sleep(double duration)
398 {
399   L07Action *action = static_cast<L07Action*>(execution_start(1.0));
400   action->m_maxDuration = duration;
401   action->m_suspended = 2;
402   lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), 0.0);
403
404   return action;
405 }
406
407 bool CpuL07::isUsed(){
408   return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
409 }
410
411 /** @brief take into account changes of speed (either load or max) */
412 void CpuL07::onSpeedChange() {
413   lmm_variable_t var = NULL;
414   lmm_element_t elem = NULL;
415
416     lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(), p_speed.peak * p_speed.scale);
417     while ((var = lmm_get_var_from_cnst
418             (getModel()->getMaxminSystem(), getConstraint(), &elem))) {
419       Action *action = static_cast<Action*>(lmm_variable_id(var));
420
421       lmm_update_variable_bound(getModel()->getMaxminSystem(),
422                                 action->getVariable(),
423                                 p_speed.scale * p_speed.peak);
424     }
425
426   Cpu::onSpeedChange();
427 }
428
429
430 bool LinkL07::isUsed(){
431   return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
432 }
433
434 void CpuL07::apply_event(tmgr_trace_iterator_t triggered, double value){
435   XBT_DEBUG("Updating cpu %s (%p) with value %g", getName(), this, value);
436   if (triggered == p_speed.event) {
437     p_speed.scale = value;
438     onSpeedChange();
439     tmgr_trace_event_unref(&p_speed.event);
440   } else if (triggered == p_stateEvent) {
441     if (value > 0)
442       turnOn();
443     else
444       turnOff();
445     tmgr_trace_event_unref(&p_stateEvent);
446   } else {
447     xbt_die("Unknown event!\n");
448   }
449 }
450
451 void LinkL07::apply_event(tmgr_trace_iterator_t triggered, double value) {
452   XBT_DEBUG("Updating link %s (%p) with value=%f", getName(), this, value);
453   if (triggered == m_bandwidth.event) {
454     updateBandwidth(value);
455     tmgr_trace_event_unref(&m_bandwidth.event);
456   } else if (triggered == m_latency.event) {
457     updateLatency(value);
458     tmgr_trace_event_unref(&m_latency.event);
459   } else if (triggered == m_stateEvent) {
460     if (value > 0)
461       turnOn();
462     else
463       turnOff();
464     tmgr_trace_event_unref(&m_stateEvent);
465   } else {
466     xbt_die("Unknown event ! \n");
467   }
468 }
469
470 void LinkL07::updateBandwidth(double value)
471 {
472   m_bandwidth.peak = value;
473   lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(), m_bandwidth.peak * m_bandwidth.scale);
474 }
475
476 void LinkL07::updateLatency(double value)
477 {
478   lmm_variable_t var = NULL;
479   L07Action *action;
480   lmm_element_t elem = NULL;
481
482   m_latency.peak = value;
483   while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), getConstraint(), &elem))) {
484     action = static_cast<L07Action*>(lmm_variable_id(var));
485     action->updateBound();
486   }
487 }
488
489 /**********
490  * Action *
491  **********/
492
493 L07Action::~L07Action(){
494   delete p_netcardList;
495   free(p_communicationAmount);
496   free(p_computationAmount);
497 }
498
499 void L07Action::updateBound()
500 {
501   double lat_current = 0.0;
502   double lat_bound = -1.0;
503   int i, j;
504
505   int hostNb = p_netcardList->size();
506
507   if (p_communicationAmount != NULL) {
508     for (i = 0; i < hostNb; i++) {
509       for (j = 0; j < hostNb; j++) {
510         xbt_dynar_t route=NULL;
511
512         if (p_communicationAmount[i * hostNb + j] > 0) {
513           double lat = 0.0;
514           routing_platf->getRouteAndLatency((*p_netcardList)[i], (*p_netcardList)[j],
515                                                                 &route, &lat);
516
517           lat_current = MAX(lat_current, lat * p_communicationAmount[i * hostNb + j]);
518         }
519       }
520     }
521   }
522   lat_bound = sg_tcp_gamma / (2.0 * lat_current);
523   XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
524   if ((m_latency == 0.0) && (m_suspended == 0)) {
525     if (m_rate < 0)
526       lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), lat_bound);
527     else
528       lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(),
529         std::min(m_rate, lat_bound));
530   }
531 }
532
533 int L07Action::unref()
534 {
535   m_refcount--;
536   if (!m_refcount) {
537     if (action_hook.is_linked())
538       p_stateSet->erase(p_stateSet->iterator_to(*this));
539     if (getVariable())
540       lmm_variable_free(getModel()->getMaxminSystem(), getVariable());
541     delete this;
542     return 1;
543   }
544   return 0;
545 }
546
547 }
548 }