Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make cmd-line option "network/TCP-gamma" neat and clean
[simgrid.git] / src / surf / ptask_L07.cpp
1 /* Copyright (c) 2007-2018. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <cstdlib>
7
8 #include <algorithm>
9 #include <unordered_set>
10
11 #include "ptask_L07.hpp"
12
13 #include "cpu_interface.hpp"
14 #include "xbt/config.hpp"
15 #include "xbt/utility.hpp"
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(not surf_cpu_model_pm, "CPU model type already defined");
27   xbt_assert(not 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   auto* maxmin_system = new simgrid::kernel::lmm::FairBottleneck(true /* selective update */);
39   set_maxmin_system(maxmin_system);
40   surf_network_model = new NetworkL07Model(this, maxmin_system);
41   surf_cpu_model_pm  = new CpuL07Model(this, maxmin_system);
42 }
43
44 HostL07Model::~HostL07Model()
45 {
46   delete surf_network_model;
47   delete surf_cpu_model_pm;
48 }
49
50 CpuL07Model::CpuL07Model(HostL07Model* hmodel, kernel::lmm::System* sys)
51     : CpuModel(Model::UpdateAlgo::Full), hostModel_(hmodel)
52 {
53   set_maxmin_system(sys);
54 }
55
56 CpuL07Model::~CpuL07Model()
57 {
58   set_maxmin_system(nullptr);
59 }
60
61 NetworkL07Model::NetworkL07Model(HostL07Model* hmodel, kernel::lmm::System* sys)
62     : NetworkModel(Model::UpdateAlgo::Full), hostModel_(hmodel)
63 {
64   set_maxmin_system(sys);
65   loopback_     = NetworkL07Model::createLink("__loopback__", 498000000, 0.000015, SURF_LINK_FATPIPE);
66 }
67
68 NetworkL07Model::~NetworkL07Model()
69 {
70   set_maxmin_system(nullptr);
71 }
72
73 double HostL07Model::next_occuring_event(double now)
74 {
75   double min = HostModel::next_occuring_event_full(now);
76   for (kernel::resource::Action const& action : *get_running_action_set()) {
77     const L07Action& net_action = static_cast<const L07Action&>(action);
78     if (net_action.latency_ > 0 && (min < 0 || net_action.latency_ < min)) {
79       min = net_action.latency_;
80       XBT_DEBUG("Updating min with %p (start %f): %f", &net_action, net_action.get_start_time(), min);
81     }
82   }
83   XBT_DEBUG("min value: %f", min);
84
85   return min;
86 }
87
88 void HostL07Model::update_actions_state(double /*now*/, double delta)
89 {
90   for (auto it = std::begin(*get_running_action_set()); it != std::end(*get_running_action_set());) {
91     L07Action& action = static_cast<L07Action&>(*it);
92     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
93     if (action.latency_ > 0) {
94       if (action.latency_ > delta) {
95         double_update(&(action.latency_), delta, sg_surf_precision);
96       } else {
97         action.latency_ = 0.0;
98       }
99       if ((action.latency_ <= 0.0) && (action.is_suspended() == 0)) {
100         action.updateBound();
101         get_maxmin_system()->update_variable_weight(action.get_variable(), 1.0);
102       }
103     }
104     XBT_DEBUG("Action (%p) : remains (%g) updated by %g.", &action, action.get_remains(),
105               action.get_variable()->get_value() * delta);
106     action.update_remains(action.get_variable()->get_value() * delta);
107
108     if (action.get_max_duration() > NO_MAX_DURATION)
109       action.update_max_duration(delta);
110
111     XBT_DEBUG("Action (%p) : remains (%g).", &action, action.get_remains());
112
113     /* In the next if cascade, the action can be finished either because:
114      *  - The amount of remaining work reached 0
115      *  - The max duration was reached
116      * If it's not done, it may have failed.
117      */
118
119     if (((action.get_remains() <= 0) && (action.get_variable()->get_weight() > 0)) ||
120         ((action.get_max_duration() > NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
121       action.finish(kernel::resource::Action::State::done);
122     } else {
123       /* Need to check that none of the model has failed */
124       int i = 0;
125       kernel::lmm::Constraint* cnst = action.get_variable()->get_constraint(i);
126       while (cnst != nullptr) {
127         i++;
128         void* constraint_id = cnst->get_id();
129         if (static_cast<simgrid::kernel::resource::Resource*>(constraint_id)->is_off()) {
130           XBT_DEBUG("Action (%p) Failed!!", &action);
131           action.finish(kernel::resource::Action::State::failed);
132           break;
133         }
134         cnst = action.get_variable()->get_constraint(i);
135       }
136     }
137   }
138 }
139
140 kernel::resource::Action* HostL07Model::execute_parallel(int host_nb, sg_host_t* host_list, double* flops_amount,
141                                                          double* bytes_amount, double rate)
142 {
143   return new L07Action(this, host_nb, host_list, flops_amount, bytes_amount, rate);
144 }
145
146 L07Action::L07Action(kernel::resource::Model* model, int host_nb, sg_host_t* host_list, double* flops_amount,
147                      double* bytes_amount, double rate)
148     : CpuAction(model, 1, 0), computationAmount_(flops_amount), communicationAmount_(bytes_amount), rate_(rate)
149 {
150   int nb_link = 0;
151   int nb_used_host = 0; /* Only the hosts with something to compute (>0 flops) are counted) */
152   double latency = 0.0;
153
154   this->hostList_->reserve(host_nb);
155   for (int i = 0; i < host_nb; i++) {
156     this->hostList_->push_back(host_list[i]);
157     if (flops_amount[i] > 0)
158       nb_used_host++;
159   }
160
161   /* Compute the number of affected resources... */
162   if(bytes_amount != nullptr) {
163     std::unordered_set<const char*> affected_links;
164
165     for (int i = 0; i < host_nb; i++) {
166       for (int j = 0; j < host_nb; j++) {
167
168         if (bytes_amount[i * host_nb + j] > 0) {
169           double lat=0.0;
170
171           std::vector<LinkImpl*> route;
172           hostList_->at(i)->routeTo(hostList_->at(j), route, &lat);
173           latency = std::max(latency, lat);
174
175           for (auto const& link : route)
176             affected_links.insert(link->get_cname());
177         }
178       }
179     }
180
181     nb_link = affected_links.size();
182   }
183
184   XBT_DEBUG("Creating a parallel task (%p) with %d hosts and %d unique links.", this, host_nb, nb_link);
185   latency_ = latency;
186
187   set_variable(model->get_maxmin_system()->variable_new(this, 1.0, (rate > 0 ? rate : -1.0), host_nb + nb_link));
188
189   if (latency_ > 0)
190     model->get_maxmin_system()->update_variable_weight(get_variable(), 0.0);
191
192   for (int i = 0; i < host_nb; i++)
193     model->get_maxmin_system()->expand(host_list[i]->pimpl_cpu->get_constraint(), get_variable(), flops_amount[i]);
194
195   if(bytes_amount != nullptr) {
196     for (int i = 0; i < host_nb; i++) {
197       for (int j = 0; j < host_nb; j++) {
198         if (bytes_amount[i * host_nb + j] > 0.0) {
199           std::vector<LinkImpl*> route;
200           hostList_->at(i)->routeTo(hostList_->at(j), route, nullptr);
201
202           for (auto const& link : route)
203             model->get_maxmin_system()->expand_add(link->get_constraint(), this->get_variable(),
204                                                    bytes_amount[i * host_nb + j]);
205         }
206       }
207     }
208   }
209
210   if (nb_link + nb_used_host == 0) {
211     this->set_cost(1.0);
212     this->set_remains(0.0);
213   }
214   delete[] host_list;
215 }
216
217 kernel::resource::Action* NetworkL07Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double rate)
218 {
219   sg_host_t* host_list = new sg_host_t[2]();
220   double* flops_amount = new double[2]();
221   double* bytes_amount = new double[4]();
222
223   host_list[0]    = src;
224   host_list[1]    = dst;
225   bytes_amount[1] = size;
226
227   return hostModel_->execute_parallel(2, host_list, flops_amount, bytes_amount, rate);
228 }
229
230 Cpu *CpuL07Model::createCpu(simgrid::s4u::Host *host,  std::vector<double> *speedPerPstate, int core)
231 {
232   return new CpuL07(this, host, speedPerPstate, core);
233 }
234
235 LinkImpl* NetworkL07Model::createLink(const std::string& name, double bandwidth, double latency,
236                                       e_surf_link_sharing_policy_t policy)
237 {
238   return new LinkL07(this, name, bandwidth, latency, policy);
239 }
240
241 /************
242  * Resource *
243  ************/
244
245 CpuL07::CpuL07(CpuL07Model* model, simgrid::s4u::Host* host, std::vector<double>* speedPerPstate, int core)
246     : Cpu(model, host, model->get_maxmin_system()->constraint_new(this, speedPerPstate->front()), speedPerPstate, core)
247 {
248 }
249
250 CpuL07::~CpuL07()=default;
251
252 LinkL07::LinkL07(NetworkL07Model* model, const std::string& name, double bandwidth, double latency,
253                  e_surf_link_sharing_policy_t policy)
254     : LinkImpl(model, name, model->get_maxmin_system()->constraint_new(this, bandwidth))
255 {
256   bandwidth_.peak = bandwidth;
257   latency_.peak   = latency;
258
259   if (policy == SURF_LINK_FATPIPE)
260     get_constraint()->unshare();
261
262   s4u::Link::onCreation(this->piface_);
263 }
264
265 kernel::resource::Action* CpuL07::execution_start(double size)
266 {
267   sg_host_t* host_list = new sg_host_t[1]();
268   double* flops_amount = new double[1]();
269
270   host_list[0] = getHost();
271   flops_amount[0] = size;
272
273   return static_cast<CpuL07Model*>(get_model())->hostModel_->execute_parallel(1, host_list, flops_amount, nullptr, -1);
274 }
275
276 kernel::resource::Action* CpuL07::sleep(double duration)
277 {
278   L07Action *action = static_cast<L07Action*>(execution_start(1.0));
279   action->set_max_duration(duration);
280   action->suspended_ = kernel::resource::Action::SuspendStates::sleeping;
281   get_model()->get_maxmin_system()->update_variable_weight(action->get_variable(), 0.0);
282
283   return action;
284 }
285
286 bool CpuL07::is_used()
287 {
288   return get_model()->get_maxmin_system()->constraint_used(get_constraint());
289 }
290
291 /** @brief take into account changes of speed (either load or max) */
292 void CpuL07::onSpeedChange() {
293   kernel::lmm::Variable* var = nullptr;
294   const kernel::lmm::Element* elem = nullptr;
295
296   get_model()->get_maxmin_system()->update_constraint_bound(get_constraint(), speed_.peak * speed_.scale);
297   while ((var = get_constraint()->get_variable(&elem))) {
298     kernel::resource::Action* action = static_cast<kernel::resource::Action*>(var->get_id());
299
300     get_model()->get_maxmin_system()->update_variable_bound(action->get_variable(), speed_.scale * speed_.peak);
301   }
302
303   Cpu::onSpeedChange();
304 }
305
306 bool LinkL07::is_used()
307 {
308   return get_model()->get_maxmin_system()->constraint_used(get_constraint());
309 }
310
311 void CpuL07::apply_event(tmgr_trace_event_t triggered, double value)
312 {
313   XBT_DEBUG("Updating cpu %s (%p) with value %g", get_cname(), this, value);
314   if (triggered == speed_.event) {
315     speed_.scale = value;
316     onSpeedChange();
317     tmgr_trace_event_unref(&speed_.event);
318
319   } else if (triggered == stateEvent_) {
320     if (value > 0)
321       turn_on();
322     else
323       turn_off();
324     tmgr_trace_event_unref(&stateEvent_);
325
326   } else {
327     xbt_die("Unknown event!\n");
328   }
329 }
330
331 void LinkL07::apply_event(tmgr_trace_event_t triggered, double value)
332 {
333   XBT_DEBUG("Updating link %s (%p) with value=%f", get_cname(), this, value);
334   if (triggered == bandwidth_.event) {
335     setBandwidth(value);
336     tmgr_trace_event_unref(&bandwidth_.event);
337
338   } else if (triggered == latency_.event) {
339     setLatency(value);
340     tmgr_trace_event_unref(&latency_.event);
341
342   } else if (triggered == stateEvent_) {
343     if (value > 0)
344       turn_on();
345     else
346       turn_off();
347     tmgr_trace_event_unref(&stateEvent_);
348
349   } else {
350     xbt_die("Unknown event ! \n");
351   }
352 }
353
354 void LinkL07::setBandwidth(double value)
355 {
356   bandwidth_.peak = value;
357   get_model()->get_maxmin_system()->update_constraint_bound(get_constraint(), bandwidth_.peak * bandwidth_.scale);
358 }
359
360 void LinkL07::setLatency(double value)
361 {
362   kernel::lmm::Variable* var = nullptr;
363   L07Action *action;
364   const kernel::lmm::Element* elem = nullptr;
365
366   latency_.peak = value;
367   while ((var = get_constraint()->get_variable(&elem))) {
368     action = static_cast<L07Action*>(var->get_id());
369     action->updateBound();
370   }
371 }
372 LinkL07::~LinkL07() = default;
373
374 /**********
375  * Action *
376  **********/
377
378 L07Action::~L07Action(){
379   delete hostList_;
380   delete[] communicationAmount_;
381   delete[] computationAmount_;
382 }
383
384 void L07Action::updateBound()
385 {
386   double lat_current = 0.0;
387
388   int hostNb = hostList_->size();
389
390   if (communicationAmount_ != nullptr) {
391     for (int i = 0; i < hostNb; i++) {
392       for (int j = 0; j < hostNb; j++) {
393
394         if (communicationAmount_[i * hostNb + j] > 0) {
395           double lat = 0.0;
396           std::vector<LinkImpl*> route;
397           hostList_->at(i)->routeTo(hostList_->at(j), route, &lat);
398
399           lat_current = std::max(lat_current, lat * communicationAmount_[i * hostNb + j]);
400         }
401       }
402     }
403   }
404   double lat_bound = NetworkModel::cfg_tcp_gamma / (2.0 * lat_current);
405   XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
406   if ((latency_ <= 0.0) && (suspended_ == Action::SuspendStates::not_suspended)) {
407     if (rate_ < 0)
408       get_model()->get_maxmin_system()->update_variable_bound(get_variable(), lat_bound);
409     else
410       get_model()->get_maxmin_system()->update_variable_bound(get_variable(), std::min(rate_, lat_bound));
411   }
412 }
413
414 }
415 }