Logo AND Algorithmique Numérique Distribuée

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