Logo AND Algorithmique Numérique Distribuée

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