Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
start snake_casing resource::Model
[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   maxmin_system_            = new simgrid::kernel::lmm::System(true /* selective update */);
38   maxmin_system_->solve_fun = &simgrid::kernel::lmm::bottleneck_solve;
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   maxmin_system_ = sys;
52 }
53
54 CpuL07Model::~CpuL07Model()
55 {
56   maxmin_system_ = nullptr;
57 }
58
59 NetworkL07Model::NetworkL07Model(HostL07Model* hmodel, kernel::lmm::System* sys) : NetworkModel(), hostModel_(hmodel)
60 {
61   maxmin_system_ = sys;
62   loopback_     = NetworkL07Model::createLink("__loopback__", 498000000, 0.000015, SURF_LINK_FATPIPE);
63 }
64
65 NetworkL07Model::~NetworkL07Model()
66 {
67   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         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::executeParallelTask(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_->executeParallelTask(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_->executeParallelTask(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::isUsed(){
284   return model()->get_maxmin_system()->constraint_used(constraint());
285 }
286
287 /** @brief take into account changes of speed (either load or max) */
288 void CpuL07::onSpeedChange() {
289   kernel::lmm::Variable* var = nullptr;
290   const_lmm_element_t elem = nullptr;
291
292   model()->get_maxmin_system()->update_constraint_bound(constraint(), speed_.peak * speed_.scale);
293   while ((var = constraint()->get_variable(&elem))) {
294     kernel::resource::Action* action = static_cast<kernel::resource::Action*>(var->get_id());
295
296     model()->get_maxmin_system()->update_variable_bound(action->get_variable(), speed_.scale * speed_.peak);
297   }
298
299   Cpu::onSpeedChange();
300 }
301
302
303 bool LinkL07::isUsed(){
304   return model()->get_maxmin_system()->constraint_used(constraint());
305 }
306
307 void CpuL07::apply_event(tmgr_trace_event_t triggered, double value)
308 {
309   XBT_DEBUG("Updating cpu %s (%p) with value %g", getCname(), this, value);
310   if (triggered == speed_.event) {
311     speed_.scale = value;
312     onSpeedChange();
313     tmgr_trace_event_unref(&speed_.event);
314
315   } else if (triggered == stateEvent_) {
316     if (value > 0)
317       turnOn();
318     else
319       turnOff();
320     tmgr_trace_event_unref(&stateEvent_);
321
322   } else {
323     xbt_die("Unknown event!\n");
324   }
325 }
326
327 void LinkL07::apply_event(tmgr_trace_event_t triggered, double value)
328 {
329   XBT_DEBUG("Updating link %s (%p) with value=%f", getCname(), this, value);
330   if (triggered == bandwidth_.event) {
331     setBandwidth(value);
332     tmgr_trace_event_unref(&bandwidth_.event);
333
334   } else if (triggered == latency_.event) {
335     setLatency(value);
336     tmgr_trace_event_unref(&latency_.event);
337
338   } else if (triggered == stateEvent_) {
339     if (value > 0)
340       turnOn();
341     else
342       turnOff();
343     tmgr_trace_event_unref(&stateEvent_);
344
345   } else {
346     xbt_die("Unknown event ! \n");
347   }
348 }
349
350 void LinkL07::setBandwidth(double value)
351 {
352   bandwidth_.peak = value;
353   model()->get_maxmin_system()->update_constraint_bound(constraint(), bandwidth_.peak * bandwidth_.scale);
354 }
355
356 void LinkL07::setLatency(double value)
357 {
358   kernel::lmm::Variable* var = nullptr;
359   L07Action *action;
360   const_lmm_element_t elem = nullptr;
361
362   latency_.peak = value;
363   while ((var = constraint()->get_variable(&elem))) {
364     action = static_cast<L07Action*>(var->get_id());
365     action->updateBound();
366   }
367 }
368 LinkL07::~LinkL07() = default;
369
370 /**********
371  * Action *
372  **********/
373
374 L07Action::~L07Action(){
375   delete hostList_;
376   delete[] communicationAmount_;
377   delete[] computationAmount_;
378 }
379
380 void L07Action::updateBound()
381 {
382   double lat_current = 0.0;
383
384   int hostNb = hostList_->size();
385
386   if (communicationAmount_ != nullptr) {
387     for (int i = 0; i < hostNb; i++) {
388       for (int j = 0; j < hostNb; j++) {
389
390         if (communicationAmount_[i * hostNb + j] > 0) {
391           double lat = 0.0;
392           std::vector<LinkImpl*> route;
393           hostList_->at(i)->routeTo(hostList_->at(j), route, &lat);
394
395           lat_current = std::max(lat_current, lat * communicationAmount_[i * hostNb + j]);
396         }
397       }
398     }
399   }
400   double lat_bound = sg_tcp_gamma / (2.0 * lat_current);
401   XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
402   if ((latency_ <= 0.0) && (suspended_ == Action::SuspendStates::not_suspended)) {
403     if (rate_ < 0)
404       get_model()->get_maxmin_system()->update_variable_bound(get_variable(), lat_bound);
405     else
406       get_model()->get_maxmin_system()->update_variable_bound(get_variable(), std::min(rate_, lat_bound));
407   }
408 }
409
410 }
411 }