Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rename the ptask files. There is no Host07 class anymore
[simgrid.git] / src / surf / 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 "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 /************
333  * Resource *
334  ************/
335
336 CpuL07::CpuL07(CpuL07Model *model, simgrid::s4u::Host *host,
337                xbt_dynar_t speedPeakList, int pstate,
338          double speedScale, tmgr_trace_t speedTrace,
339              int core, int initiallyOn, tmgr_trace_t state_trace)
340  : Cpu(model, host, speedPeakList, pstate,
341      core, xbt_dynar_get_as(speedPeakList,pstate,double), speedScale, initiallyOn)
342 {
343   p_constraint = lmm_constraint_new(model->getMaxminSystem(), this, xbt_dynar_get_as(speedPeakList,pstate,double) * speedScale);
344
345   if (speedTrace)
346     p_speed.event = future_evt_set->add_trace(speedTrace, 0.0, this);
347
348   if (state_trace)
349     p_stateEvent = future_evt_set->add_trace(state_trace, 0.0, this);
350 }
351
352 CpuL07::~CpuL07()
353 {
354 }
355
356 LinkL07::LinkL07(NetworkL07Model *model, const char* name, xbt_dict_t props,
357              double bw_initial, tmgr_trace_t bw_trace,
358              double lat_initial, tmgr_trace_t lat_trace,
359              int initiallyOn, tmgr_trace_t state_trace,
360              e_surf_link_sharing_policy_t policy)
361  : Link(model, name, props, lmm_constraint_new(model->getMaxminSystem(), this, bw_initial), state_trace)
362 {
363   m_bandwidth.peak = bw_initial;
364   if (bw_trace)
365     m_bandwidth.event = future_evt_set->add_trace(bw_trace, 0.0, this);
366
367   if (initiallyOn)
368     turnOn();
369   else
370     turnOff();
371
372   m_latency.peak = lat_initial;
373   if (lat_trace)
374     m_latency.event = future_evt_set->add_trace(lat_trace, 0.0, this);
375
376   if (policy == SURF_LINK_FATPIPE)
377   lmm_constraint_shared(getConstraint());
378 }
379
380 Action *CpuL07::execution_start(double size)
381 {
382   sg_host_t*host_list = xbt_new0(sg_host_t, 1);
383   double *flops_amount = xbt_new0(double, 1);
384
385   host_list[0] = getHost();
386   flops_amount[0] = size;
387
388   return static_cast<CpuL07Model*>(getModel())->p_hostModel
389     ->executeParallelTask( 1, host_list, flops_amount, NULL, -1);
390 }
391
392 Action *CpuL07::sleep(double duration)
393 {
394   L07Action *action = static_cast<L07Action*>(execution_start(1.0));
395   action->m_maxDuration = duration;
396   action->m_suspended = 2;
397   lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), 0.0);
398
399   return action;
400 }
401
402 bool CpuL07::isUsed(){
403   return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
404 }
405
406 /** @brief take into account changes of speed (either load or max) */
407 void CpuL07::onSpeedChange() {
408   lmm_variable_t var = NULL;
409   lmm_element_t elem = NULL;
410
411     lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(), p_speed.peak * p_speed.scale);
412     while ((var = lmm_get_var_from_cnst
413             (getModel()->getMaxminSystem(), getConstraint(), &elem))) {
414       Action *action = static_cast<Action*>(lmm_variable_id(var));
415
416       lmm_update_variable_bound(getModel()->getMaxminSystem(),
417                                 action->getVariable(),
418                                 p_speed.scale * p_speed.peak);
419     }
420
421   Cpu::onSpeedChange();
422 }
423
424
425 bool LinkL07::isUsed(){
426   return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
427 }
428
429 void CpuL07::apply_event(tmgr_trace_iterator_t triggered, double value){
430   XBT_DEBUG("Updating cpu %s (%p) with value %g", getName(), this, value);
431   if (triggered == p_speed.event) {
432     p_speed.scale = value;
433     onSpeedChange();
434     tmgr_trace_event_unref(&p_speed.event);
435
436   } else if (triggered == p_stateEvent) {
437     if (value > 0)
438       turnOn();
439     else
440       turnOff();
441     tmgr_trace_event_unref(&p_stateEvent);
442
443   } else {
444     xbt_die("Unknown event!\n");
445   }
446 }
447
448 void LinkL07::apply_event(tmgr_trace_iterator_t triggered, double value) {
449   XBT_DEBUG("Updating link %s (%p) with value=%f", getName(), this, value);
450   if (triggered == m_bandwidth.event) {
451     updateBandwidth(value);
452     tmgr_trace_event_unref(&m_bandwidth.event);
453
454   } else if (triggered == m_latency.event) {
455     updateLatency(value);
456     tmgr_trace_event_unref(&m_latency.event);
457
458   } else if (triggered == m_stateEvent) {
459     if (value > 0)
460       turnOn();
461     else
462       turnOff();
463     tmgr_trace_event_unref(&m_stateEvent);
464
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 }