1 /* Copyright (c) 2007-2010, 2013-2015. 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"
15 #include "surf_routing.hpp"
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_host);
19 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
21 /**************************************/
22 /*** Resource Creation & Destruction **/
23 /**************************************/
24 void surf_host_model_init_ptask_L07()
26 XBT_CINFO(xbt_cfg,"Switching to the L07 model to handle parallel tasks.");
27 xbt_assert(!surf_cpu_model_pm, "CPU model type already defined");
28 xbt_assert(!surf_network_model, "network model type already defined");
30 surf_host_model = new simgrid::surf::HostL07Model();
31 all_existing_models->push_back(surf_host_model);
38 HostL07Model::HostL07Model() : HostModel() {
39 maxminSystem_ = lmm_system_new(true /* lazy */);
40 maxminSystem_->solve_fun = &bottleneck_solve;
41 surf_network_model = new NetworkL07Model(this,maxminSystem_);
42 surf_cpu_model_pm = new CpuL07Model(this,maxminSystem_);
44 routing_model_create();
47 HostL07Model::~HostL07Model() = default;
49 CpuL07Model::CpuL07Model(HostL07Model *hmodel,lmm_system_t sys)
55 CpuL07Model::~CpuL07Model() {
56 lmm_system_free(maxminSystem_);
57 maxminSystem_ = nullptr;
59 NetworkL07Model::NetworkL07Model(HostL07Model *hmodel, lmm_system_t sys)
64 loopback_ = createLink("__loopback__", 498000000, 0.000015, SURF_LINK_FATPIPE);
66 NetworkL07Model::~NetworkL07Model()
68 maxminSystem_ = nullptr; // Avoid multi-free
72 double HostL07Model::nextOccuringEvent(double now)
74 double min = HostModel::nextOccuringEventFull(now);
75 for (auto it(getRunningActionSet()->begin()), itend(getRunningActionSet()->end()); it != itend ; ++it) {
76 L07Action *action = static_cast<L07Action*>(&*it);
77 if (action->latency_ > 0 && (min < 0 || action->latency_ < min)) {
78 min = action->latency_;
79 XBT_DEBUG("Updating min with %p (start %f): %f", action, action->getStartTime(), min);
82 XBT_DEBUG("min value: %f", min);
87 void HostL07Model::updateActionsState(double /*now*/, double delta) {
90 ActionList *actionSet = getRunningActionSet();
92 for(ActionList::iterator it = actionSet->begin(), itNext = it
93 ; it != actionSet->end()
96 action = static_cast<L07Action*>(&*it);
97 if (action->latency_ > 0) {
98 if (action->latency_ > delta) {
99 double_update(&(action->latency_), delta, sg_surf_precision);
101 action->latency_ = 0.0;
103 if ((action->latency_ == 0.0) && (action->isSuspended() == 0)) {
104 action->updateBound();
105 lmm_update_variable_weight(maxminSystem_, action->getVariable(), 1.0);
108 XBT_DEBUG("Action (%p) : remains (%g) updated by %g.",
109 action, action->getRemains(), lmm_variable_getvalue(action->getVariable()) * delta);
110 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
112 if (action->getMaxDuration() != NO_MAX_DURATION)
113 action->updateMaxDuration(delta);
115 XBT_DEBUG("Action (%p) : remains (%g).", action, action->getRemains());
117 /* In the next if cascade, the action can be finished either because:
118 * - The amount of remaining work reached 0
119 * - The max duration was reached
120 * If it's not done, it may have failed.
123 if ((action->getRemains() <= 0) &&
124 (lmm_get_variable_weight(action->getVariable()) > 0)) {
126 action->setState(Action::State::done);
127 } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
128 (action->getMaxDuration() <= 0)) {
130 action->setState(Action::State::done);
132 /* Need to check that none of the model has failed */
133 lmm_constraint_t cnst = nullptr;
136 while ((cnst = lmm_get_cnst_from_var(maxminSystem_, action->getVariable(), i++))) {
137 void *constraint_id = lmm_constraint_id(cnst);
138 if (static_cast<simgrid::surf::Resource*>(constraint_id)->isOff()) {
139 XBT_DEBUG("Action (%p) Failed!!", action);
141 action->setState(Action::State::failed);
150 Action *HostL07Model::executeParallelTask(int host_nb, sg_host_t *host_list,
151 double *flops_amount, double *bytes_amount,double rate) {
152 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)
161 int nb_used_host = 0; /* Only the hosts with something to compute (>0 flops) are counted) */
162 double latency = 0.0;
164 this->netcardList_->reserve(host_nb);
165 for (int i = 0; i<host_nb; i++)
166 this->netcardList_->push_back(host_list[i]->pimpl_netcard);
168 /* Compute the number of affected resources... */
169 if(bytes_amount != nullptr) {
170 std::unordered_set<const char*> affected_links;
172 for (int i = 0; i < host_nb; i++) {
173 for (int j = 0; j < host_nb; j++) {
175 if (bytes_amount[i * host_nb + j] > 0) {
177 std::vector<Link*> route;
179 routing_platf->getRouteAndLatency((*netcardList_)[i], (*netcardList_)[j], &route, &lat);
180 latency = MAX(latency, lat);
182 for (auto link : route)
183 affected_links.insert(link->getName());
188 nb_link = affected_links.size();
191 for (int i = 0; i < host_nb; i++)
192 if (flops_amount[i] > 0)
195 XBT_DEBUG("Creating a parallel task (%p) with %d hosts and %d unique links.", this, host_nb, nb_link);
196 this->computationAmount_ = flops_amount;
197 this->communicationAmount_ = bytes_amount;
198 this->latency_ = latency;
201 this->variable_ = lmm_variable_new(model->getMaxminSystem(), this, 1.0,
202 (rate > 0 ? rate : -1.0),
205 if (this->latency_ > 0)
206 lmm_update_variable_weight(model->getMaxminSystem(), this->getVariable(), 0.0);
208 for (int i = 0; i < host_nb; i++)
209 lmm_expand(model->getMaxminSystem(), host_list[i]->pimpl_cpu->getConstraint(),
210 this->getVariable(), flops_amount[i]);
212 if(bytes_amount != nullptr) {
213 for (int i = 0; i < host_nb; i++) {
214 for (int j = 0; j < host_nb; j++) {
216 if (bytes_amount[i * host_nb + j] == 0.0)
218 std::vector<Link*> route;
220 routing_platf->getRouteAndLatency((*netcardList_)[i], (*netcardList_)[j], &route, nullptr);
222 for (auto link : route)
223 lmm_expand_add(model->getMaxminSystem(), link->getConstraint(), this->getVariable(), bytes_amount[i * host_nb + j]);
228 if (nb_link + nb_used_host == 0) {
230 this->setRemains(0.0);
235 Action* NetworkL07Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double rate)
237 sg_host_t*host_list = xbt_new0(sg_host_t, 2);
238 double *flops_amount = xbt_new0(double, 2);
239 double *bytes_amount = xbt_new0(double, 4);
243 bytes_amount[1] = size;
245 return hostModel_->executeParallelTask(2, host_list, flops_amount, bytes_amount, rate);
248 Cpu *CpuL07Model::createCpu(simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
250 return new CpuL07(this, host, speedPerPstate, core);
253 Link* NetworkL07Model::createLink(const char* name, double bandwidth, double latency,
254 e_surf_link_sharing_policy_t policy)
256 return new LinkL07(this, name, bandwidth, latency, policy);
263 CpuL07::CpuL07(CpuL07Model* model, simgrid::s4u::Host* host, std::vector<double>* speedPerPstate, int core)
264 : Cpu(model, host, lmm_constraint_new(model->getMaxminSystem(), this, speedPerPstate->front()), speedPerPstate,
269 CpuL07::~CpuL07()=default;
271 LinkL07::LinkL07(NetworkL07Model* model, const char* name, double bandwidth, double latency,
272 e_surf_link_sharing_policy_t policy)
273 : Link(model, name, lmm_constraint_new(model->getMaxminSystem(), this, bandwidth))
275 bandwidth_.peak = bandwidth;
276 latency_.peak = latency;
278 if (policy == SURF_LINK_FATPIPE)
279 lmm_constraint_shared(getConstraint());
281 Link::onCreation(this);
284 Action *CpuL07::execution_start(double size)
286 sg_host_t*host_list = xbt_new0(sg_host_t, 1);
287 double *flops_amount = xbt_new0(double, 1);
289 host_list[0] = getHost();
290 flops_amount[0] = size;
292 return static_cast<CpuL07Model*>(getModel())->hostModel_->executeParallelTask(1, host_list, flops_amount, nullptr, -1);
295 Action *CpuL07::sleep(double duration)
297 L07Action *action = static_cast<L07Action*>(execution_start(1.0));
298 action->maxDuration_ = duration;
299 action->suspended_ = 2;
300 lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), 0.0);
305 bool CpuL07::isUsed(){
306 return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
309 /** @brief take into account changes of speed (either load or max) */
310 void CpuL07::onSpeedChange() {
311 lmm_variable_t var = nullptr;
312 lmm_element_t elem = nullptr;
314 lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(), speed_.peak * speed_.scale);
315 while ((var = lmm_get_var_from_cnst (getModel()->getMaxminSystem(), getConstraint(), &elem))) {
316 Action *action = static_cast<Action*>(lmm_variable_id(var));
318 lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(), speed_.scale * speed_.peak);
321 Cpu::onSpeedChange();
325 bool LinkL07::isUsed(){
326 return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
329 void CpuL07::apply_event(tmgr_trace_iterator_t triggered, double value){
330 XBT_DEBUG("Updating cpu %s (%p) with value %g", getName(), this, value);
331 if (triggered == speed_.event) {
332 speed_.scale = value;
334 tmgr_trace_event_unref(&speed_.event);
336 } else if (triggered == stateEvent_) {
341 tmgr_trace_event_unref(&stateEvent_);
344 xbt_die("Unknown event!\n");
348 void LinkL07::apply_event(tmgr_trace_iterator_t triggered, double value) {
349 XBT_DEBUG("Updating link %s (%p) with value=%f", getName(), this, value);
350 if (triggered == bandwidth_.event) {
352 tmgr_trace_event_unref(&bandwidth_.event);
354 } else if (triggered == latency_.event) {
356 tmgr_trace_event_unref(&latency_.event);
358 } else if (triggered == stateEvent_) {
363 tmgr_trace_event_unref(&stateEvent_);
366 xbt_die("Unknown event ! \n");
370 void LinkL07::setBandwidth(double value)
372 bandwidth_.peak = value;
373 lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(), bandwidth_.peak * bandwidth_.scale);
376 void LinkL07::setLatency(double value)
378 lmm_variable_t var = nullptr;
380 lmm_element_t elem = nullptr;
382 latency_.peak = value;
383 while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), getConstraint(), &elem))) {
384 action = static_cast<L07Action*>(lmm_variable_id(var));
385 action->updateBound();
393 L07Action::~L07Action(){
395 free(communicationAmount_);
396 free(computationAmount_);
399 void L07Action::updateBound()
401 double lat_current = 0.0;
402 double lat_bound = -1.0;
405 int hostNb = netcardList_->size();
407 if (communicationAmount_ != nullptr) {
408 for (i = 0; i < hostNb; i++) {
409 for (j = 0; j < hostNb; j++) {
411 if (communicationAmount_[i * hostNb + j] > 0) {
413 std::vector<Link*> route;
414 routing_platf->getRouteAndLatency((*netcardList_)[i], (*netcardList_)[j], &route, &lat);
416 lat_current = MAX(lat_current, lat * communicationAmount_[i * hostNb + j]);
421 lat_bound = sg_tcp_gamma / (2.0 * lat_current);
422 XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
423 if ((latency_ == 0.0) && (suspended_ == 0)) {
425 lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), lat_bound);
427 lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), std::min(rate_, lat_bound));
431 int L07Action::unref()
435 if (action_hook.is_linked())
436 stateSet_->erase(stateSet_->iterator_to(*this));
438 lmm_variable_free(getModel()->getMaxminSystem(), getVariable());