1 /* Copyright (c) 2007-2010, 2013-2017. The SimGrid Team.
2 * All rights reserved. */
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. */
10 #include <unordered_set>
12 #include "ptask_L07.hpp"
14 #include "cpu_interface.hpp"
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_host);
17 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
19 /**************************************/
20 /*** Resource Creation & Destruction **/
21 /**************************************/
22 void surf_host_model_init_ptask_L07()
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");
28 surf_host_model = new simgrid::surf::HostL07Model();
29 all_existing_models->push_back(surf_host_model);
36 HostL07Model::HostL07Model() : HostModel() {
37 maxminSystem_ = lmm_system_new(true /* lazy */);
38 maxminSystem_->solve_fun = &bottleneck_solve;
39 surf_network_model = new NetworkL07Model(this,maxminSystem_);
40 surf_cpu_model_pm = new CpuL07Model(this,maxminSystem_);
43 HostL07Model::~HostL07Model()
45 lmm_system_free(maxminSystem_);
46 maxminSystem_ = nullptr;
47 delete surf_network_model;
48 delete surf_cpu_model_pm;
51 CpuL07Model::CpuL07Model(HostL07Model *hmodel,lmm_system_t sys)
58 CpuL07Model::~CpuL07Model()
60 maxminSystem_ = nullptr;
63 NetworkL07Model::NetworkL07Model(HostL07Model *hmodel, lmm_system_t sys)
68 loopback_ = createLink("__loopback__", 498000000, 0.000015, SURF_LINK_FATPIPE);
71 NetworkL07Model::~NetworkL07Model()
73 maxminSystem_ = nullptr;
76 double HostL07Model::nextOccuringEvent(double now)
78 double min = HostModel::nextOccuringEventFull(now);
79 ActionList::iterator it(getRunningActionSet()->begin());
80 ActionList::iterator itend(getRunningActionSet()->end());
81 for (; it != itend; ++it) {
82 L07Action *action = static_cast<L07Action*>(&*it);
83 if (action->latency_ > 0 && (min < 0 || action->latency_ < min)) {
84 min = action->latency_;
85 XBT_DEBUG("Updating min with %p (start %f): %f", action, action->getStartTime(), min);
88 XBT_DEBUG("min value: %f", min);
93 void HostL07Model::updateActionsState(double /*now*/, double delta) {
96 ActionList *actionSet = getRunningActionSet();
97 ActionList::iterator it(actionSet->begin());
98 ActionList::iterator itNext = it;
99 ActionList::iterator itend(actionSet->end());
101 for (; it != itend; it = itNext) {
103 action = static_cast<L07Action*>(&*it);
104 if (action->latency_ > 0) {
105 if (action->latency_ > delta) {
106 double_update(&(action->latency_), delta, sg_surf_precision);
108 action->latency_ = 0.0;
110 if ((action->latency_ <= 0.0) && (action->isSuspended() == 0)) {
111 action->updateBound();
112 lmm_update_variable_weight(maxminSystem_, action->getVariable(), 1.0);
115 XBT_DEBUG("Action (%p) : remains (%g) updated by %g.",
116 action, action->getRemains(), lmm_variable_getvalue(action->getVariable()) * delta);
117 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
119 if (action->getMaxDuration() > NO_MAX_DURATION)
120 action->updateMaxDuration(delta);
122 XBT_DEBUG("Action (%p) : remains (%g).", action, action->getRemains());
124 /* In the next if cascade, the action can be finished either because:
125 * - The amount of remaining work reached 0
126 * - The max duration was reached
127 * If it's not done, it may have failed.
130 if (((action->getRemains() <= 0) && (lmm_get_variable_weight(action->getVariable()) > 0)) ||
131 ((action->getMaxDuration() > NO_MAX_DURATION) && (action->getMaxDuration() <= 0))) {
132 action->finish(Action::State::done);
134 /* Need to check that none of the model has failed */
136 lmm_constraint_t cnst = lmm_get_cnst_from_var(maxminSystem_, action->getVariable(), i);
137 while (cnst != nullptr) {
139 void *constraint_id = lmm_constraint_id(cnst);
140 if (static_cast<simgrid::surf::Resource*>(constraint_id)->isOff()) {
141 XBT_DEBUG("Action (%p) Failed!!", action);
142 action->finish(Action::State::failed);
145 cnst = lmm_get_cnst_from_var(maxminSystem_, action->getVariable(), i);
151 Action *HostL07Model::executeParallelTask(int host_nb, sg_host_t *host_list,
152 double *flops_amount, double *bytes_amount,double rate) {
153 return new L07Action(this, host_nb, host_list, flops_amount, bytes_amount, rate);
156 L07Action::L07Action(Model *model, int host_nb, sg_host_t *host_list,
157 double *flops_amount, double *bytes_amount, double rate)
158 : CpuAction(model, 1, 0)
159 , computationAmount_(flops_amount)
160 , communicationAmount_(bytes_amount)
164 int nb_used_host = 0; /* Only the hosts with something to compute (>0 flops) are counted) */
165 double latency = 0.0;
167 this->hostList_->reserve(host_nb);
168 for (int i = 0; i < host_nb; i++) {
169 this->hostList_->push_back(host_list[i]);
170 if (flops_amount[i] > 0)
174 /* Compute the number of affected resources... */
175 if(bytes_amount != nullptr) {
176 std::unordered_set<const char*> affected_links;
178 for (int i = 0; i < host_nb; i++) {
179 for (int j = 0; j < host_nb; j++) {
181 if (bytes_amount[i * host_nb + j] > 0) {
184 std::vector<LinkImpl*> route;
185 hostList_->at(i)->routeTo(hostList_->at(j), &route, &lat);
186 latency = MAX(latency, lat);
188 for (auto const& link : route)
189 affected_links.insert(link->getCname());
194 nb_link = affected_links.size();
197 XBT_DEBUG("Creating a parallel task (%p) with %d hosts and %d unique links.", this, host_nb, nb_link);
198 this->latency_ = latency;
200 this->variable_ = lmm_variable_new(model->getMaxminSystem(), this, 1.0,
201 (rate > 0 ? rate : -1.0),
204 if (this->latency_ > 0)
205 lmm_update_variable_weight(model->getMaxminSystem(), this->getVariable(), 0.0);
207 for (int i = 0; i < host_nb; i++)
208 lmm_expand(model->getMaxminSystem(), host_list[i]->pimpl_cpu->constraint(), this->getVariable(), flops_amount[i]);
210 if(bytes_amount != nullptr) {
211 for (int i = 0; i < host_nb; i++) {
212 for (int j = 0; j < host_nb; j++) {
213 if (bytes_amount[i * host_nb + j] > 0.0) {
214 std::vector<LinkImpl*> route;
215 hostList_->at(i)->routeTo(hostList_->at(j), &route, nullptr);
217 for (auto const& link : route)
218 lmm_expand_add(model->getMaxminSystem(), link->constraint(), this->getVariable(),
219 bytes_amount[i * host_nb + j]);
225 if (nb_link + nb_used_host == 0) {
227 this->setRemains(0.0);
232 Action* NetworkL07Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double rate)
234 sg_host_t* host_list = new sg_host_t[2]();
235 double* flops_amount = new double[2]();
236 double* bytes_amount = new double[4]();
240 bytes_amount[1] = size;
242 return hostModel_->executeParallelTask(2, host_list, flops_amount, bytes_amount, rate);
245 Cpu *CpuL07Model::createCpu(simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
247 return new CpuL07(this, host, speedPerPstate, core);
250 LinkImpl* NetworkL07Model::createLink(const std::string& name, double bandwidth, double latency,
251 e_surf_link_sharing_policy_t policy)
253 return new LinkL07(this, name, bandwidth, latency, policy);
260 CpuL07::CpuL07(CpuL07Model* model, simgrid::s4u::Host* host, std::vector<double>* speedPerPstate, int core)
261 : Cpu(model, host, lmm_constraint_new(model->getMaxminSystem(), this, speedPerPstate->front()), speedPerPstate,
266 CpuL07::~CpuL07()=default;
268 LinkL07::LinkL07(NetworkL07Model* model, const std::string& name, double bandwidth, double latency,
269 e_surf_link_sharing_policy_t policy)
270 : LinkImpl(model, name, lmm_constraint_new(model->getMaxminSystem(), this, bandwidth))
272 bandwidth_.peak = bandwidth;
273 latency_.peak = latency;
275 if (policy == SURF_LINK_FATPIPE)
276 lmm_constraint_shared(constraint());
278 s4u::Link::onCreation(this->piface_);
281 Action *CpuL07::execution_start(double size)
283 sg_host_t* host_list = new sg_host_t[1]();
284 double* flops_amount = new double[1]();
286 host_list[0] = getHost();
287 flops_amount[0] = size;
289 return static_cast<CpuL07Model*>(model())->hostModel_->executeParallelTask(1, host_list, flops_amount, nullptr, -1);
292 Action *CpuL07::sleep(double duration)
294 L07Action *action = static_cast<L07Action*>(execution_start(1.0));
295 action->maxDuration_ = duration;
296 action->suspended_ = 2;
297 lmm_update_variable_weight(model()->getMaxminSystem(), action->getVariable(), 0.0);
302 bool CpuL07::isUsed(){
303 return lmm_constraint_used(model()->getMaxminSystem(), constraint());
306 /** @brief take into account changes of speed (either load or max) */
307 void CpuL07::onSpeedChange() {
308 lmm_variable_t var = nullptr;
309 lmm_element_t elem = nullptr;
311 lmm_update_constraint_bound(model()->getMaxminSystem(), constraint(), speed_.peak * speed_.scale);
312 while ((var = lmm_get_var_from_cnst(model()->getMaxminSystem(), constraint(), &elem))) {
313 Action* action = static_cast<Action*>(lmm_variable_id(var));
315 lmm_update_variable_bound(model()->getMaxminSystem(), action->getVariable(), speed_.scale * speed_.peak);
318 Cpu::onSpeedChange();
322 bool LinkL07::isUsed(){
323 return lmm_constraint_used(model()->getMaxminSystem(), constraint());
326 void CpuL07::apply_event(tmgr_trace_event_t triggered, double value)
328 XBT_DEBUG("Updating cpu %s (%p) with value %g", getCname(), this, value);
329 if (triggered == speed_.event) {
330 speed_.scale = value;
332 tmgr_trace_event_unref(&speed_.event);
334 } else if (triggered == stateEvent_) {
339 tmgr_trace_event_unref(&stateEvent_);
342 xbt_die("Unknown event!\n");
346 void LinkL07::apply_event(tmgr_trace_event_t triggered, double value)
348 XBT_DEBUG("Updating link %s (%p) with value=%f", getCname(), this, value);
349 if (triggered == bandwidth_.event) {
351 tmgr_trace_event_unref(&bandwidth_.event);
353 } else if (triggered == latency_.event) {
355 tmgr_trace_event_unref(&latency_.event);
357 } else if (triggered == stateEvent_) {
362 tmgr_trace_event_unref(&stateEvent_);
365 xbt_die("Unknown event ! \n");
369 void LinkL07::setBandwidth(double value)
371 bandwidth_.peak = value;
372 lmm_update_constraint_bound(model()->getMaxminSystem(), constraint(), bandwidth_.peak * bandwidth_.scale);
375 void LinkL07::setLatency(double value)
377 lmm_variable_t var = nullptr;
379 lmm_element_t elem = nullptr;
381 latency_.peak = value;
382 while ((var = lmm_get_var_from_cnst(model()->getMaxminSystem(), constraint(), &elem))) {
383 action = static_cast<L07Action*>(lmm_variable_id(var));
384 action->updateBound();
387 LinkL07::~LinkL07() = default;
393 L07Action::~L07Action(){
395 delete[] communicationAmount_;
396 delete[] computationAmount_;
399 void L07Action::updateBound()
401 double lat_current = 0.0;
403 int hostNb = hostList_->size();
405 if (communicationAmount_ != nullptr) {
406 for (int i = 0; i < hostNb; i++) {
407 for (int j = 0; j < hostNb; j++) {
409 if (communicationAmount_[i * hostNb + j] > 0) {
411 std::vector<LinkImpl*> route;
412 hostList_->at(i)->routeTo(hostList_->at(j), &route, &lat);
414 lat_current = MAX(lat_current, lat * communicationAmount_[i * hostNb + j]);
419 double lat_bound = sg_tcp_gamma / (2.0 * lat_current);
420 XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
421 if ((latency_ <= 0.0) && (suspended_ == 0)) {
423 lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), lat_bound);
425 lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), std::min(rate_, lat_bound));
429 int L07Action::unref()
433 if (action_hook.is_linked())
434 stateSet_->erase(stateSet_->iterator_to(*this));
436 lmm_variable_free(getModel()->getMaxminSystem(), getVariable());