Logo AND Algorithmique Numérique Distribuée

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