Logo AND Algorithmique Numérique Distribuée

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