Logo AND Algorithmique Numérique Distribuée

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