Logo AND Algorithmique Numérique Distribuée

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