Logo AND Algorithmique Numérique Distribuée

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