Logo AND Algorithmique Numérique Distribuée

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