Logo AND Algorithmique Numérique Distribuée

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