Logo AND Algorithmique Numérique Distribuée

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