Logo AND Algorithmique Numérique Distribuée

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