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"
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_host);
18 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
20 /**************************************/
21 /*** Resource Creation & Destruction **/
22 /**************************************/
23 void surf_host_model_init_ptask_L07()
25 XBT_CINFO(xbt_cfg,"Switching to the L07 model to handle parallel tasks.");
26 xbt_assert(!surf_cpu_model_pm, "CPU model type already defined");
27 xbt_assert(!surf_network_model, "network model type already defined");
29 surf_host_model = new simgrid::surf::HostL07Model();
30 all_existing_models->push_back(surf_host_model);
37 HostL07Model::HostL07Model() : HostModel() {
38 maxminSystem_ = lmm_system_new(true /* lazy */);
39 maxminSystem_->solve_fun = &bottleneck_solve;
40 surf_network_model = new NetworkL07Model(this,maxminSystem_);
41 surf_cpu_model_pm = new CpuL07Model(this,maxminSystem_);
44 HostL07Model::~HostL07Model() = default;
46 CpuL07Model::CpuL07Model(HostL07Model *hmodel,lmm_system_t sys)
52 CpuL07Model::~CpuL07Model() {
53 lmm_system_free(maxminSystem_);
54 maxminSystem_ = nullptr;
56 NetworkL07Model::NetworkL07Model(HostL07Model *hmodel, lmm_system_t sys)
61 loopback_ = createLink("__loopback__", 498000000, 0.000015, SURF_LINK_FATPIPE);
63 NetworkL07Model::~NetworkL07Model()
65 maxminSystem_ = nullptr; // Avoid multi-free
69 double HostL07Model::nextOccuringEvent(double now)
71 double min = HostModel::nextOccuringEventFull(now);
72 ActionList::iterator it(getRunningActionSet()->begin());
73 ActionList::iterator itend(getRunningActionSet()->end());
74 for (; it != itend; ++it) {
75 L07Action *action = static_cast<L07Action*>(&*it);
76 if (action->latency_ > 0 && (min < 0 || action->latency_ < min)) {
77 min = action->latency_;
78 XBT_DEBUG("Updating min with %p (start %f): %f", action, action->getStartTime(), min);
81 XBT_DEBUG("min value: %f", min);
86 void HostL07Model::updateActionsState(double /*now*/, double delta) {
89 ActionList *actionSet = getRunningActionSet();
90 ActionList::iterator it(actionSet->begin());
91 ActionList::iterator itNext = it;
92 ActionList::iterator itend(actionSet->end());
94 for (; it != itend; it = itNext) {
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) && (lmm_get_variable_weight(action->getVariable()) > 0)) ||
124 ((action->getMaxDuration() != NO_MAX_DURATION) && (action->getMaxDuration() <= 0))) {
126 action->setState(Action::State::done);
128 /* Need to check that none of the model has failed */
130 lmm_constraint_t cnst = lmm_get_cnst_from_var(maxminSystem_, action->getVariable(), i);
131 while (cnst != nullptr) {
133 void *constraint_id = lmm_constraint_id(cnst);
134 if (static_cast<simgrid::surf::Resource*>(constraint_id)->isOff()) {
135 XBT_DEBUG("Action (%p) Failed!!", action);
137 action->setState(Action::State::failed);
140 cnst = lmm_get_cnst_from_var(maxminSystem_, action->getVariable(), i);
146 Action *HostL07Model::executeParallelTask(int host_nb, sg_host_t *host_list,
147 double *flops_amount, double *bytes_amount,double rate) {
148 return new L07Action(this, host_nb, host_list, flops_amount, bytes_amount, rate);
151 L07Action::L07Action(Model *model, int host_nb, sg_host_t *host_list,
152 double *flops_amount, double *bytes_amount, double rate)
153 : CpuAction(model, 1, 0)
154 , computationAmount_(flops_amount)
155 , communicationAmount_(bytes_amount)
159 int nb_used_host = 0; /* Only the hosts with something to compute (>0 flops) are counted) */
160 double latency = 0.0;
162 this->hostList_->reserve(host_nb);
163 for (int i = 0; i<host_nb; i++)
164 this->hostList_->push_back(host_list[i]);
166 /* Compute the number of affected resources... */
167 if(bytes_amount != nullptr) {
168 std::unordered_set<const char*> affected_links;
170 for (int i = 0; i < host_nb; i++) {
171 for (int j = 0; j < host_nb; j++) {
173 if (bytes_amount[i * host_nb + j] > 0) {
176 std::vector<LinkImpl*> route;
177 hostList_->at(i)->routeTo(hostList_->at(j), &route, &lat);
178 latency = MAX(latency, lat);
180 for (auto link : route)
181 affected_links.insert(link->cname());
186 nb_link = affected_links.size();
189 for (int i = 0; i < host_nb; i++)
190 if (flops_amount[i] > 0)
193 XBT_DEBUG("Creating a parallel task (%p) with %d hosts and %d unique links.", this, host_nb, nb_link);
194 this->latency_ = latency;
196 this->variable_ = lmm_variable_new(model->getMaxminSystem(), this, 1.0,
197 (rate > 0 ? rate : -1.0),
200 if (this->latency_ > 0)
201 lmm_update_variable_weight(model->getMaxminSystem(), this->getVariable(), 0.0);
203 for (int i = 0; i < host_nb; i++)
204 lmm_expand(model->getMaxminSystem(), host_list[i]->pimpl_cpu->constraint(), this->getVariable(), flops_amount[i]);
206 if(bytes_amount != nullptr) {
207 for (int i = 0; i < host_nb; i++) {
208 for (int j = 0; j < host_nb; j++) {
209 if (bytes_amount[i * host_nb + j] > 0.0) {
210 std::vector<LinkImpl*> route;
211 hostList_->at(i)->routeTo(hostList_->at(j), &route, nullptr);
213 for (auto link : route)
214 lmm_expand_add(model->getMaxminSystem(), link->constraint(), this->getVariable(),
215 bytes_amount[i * host_nb + j]);
221 if (nb_link + nb_used_host == 0) {
223 this->setRemains(0.0);
228 Action* NetworkL07Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double rate)
230 sg_host_t*host_list = xbt_new0(sg_host_t, 2);
231 double *flops_amount = xbt_new0(double, 2);
232 double *bytes_amount = xbt_new0(double, 4);
236 bytes_amount[1] = size;
238 return hostModel_->executeParallelTask(2, host_list, flops_amount, bytes_amount, rate);
241 Cpu *CpuL07Model::createCpu(simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
243 return new CpuL07(this, host, speedPerPstate, core);
246 LinkImpl* NetworkL07Model::createLink(const char* name, double bandwidth, double latency,
247 e_surf_link_sharing_policy_t policy)
249 return new LinkL07(this, name, bandwidth, latency, policy);
256 CpuL07::CpuL07(CpuL07Model* model, simgrid::s4u::Host* host, std::vector<double>* speedPerPstate, int core)
257 : Cpu(model, host, lmm_constraint_new(model->getMaxminSystem(), this, speedPerPstate->front()), speedPerPstate,
262 CpuL07::~CpuL07()=default;
264 LinkL07::LinkL07(NetworkL07Model* model, const char* name, double bandwidth, double latency,
265 e_surf_link_sharing_policy_t policy)
266 : LinkImpl(model, name, lmm_constraint_new(model->getMaxminSystem(), this, bandwidth))
268 bandwidth_.peak = bandwidth;
269 latency_.peak = latency;
271 if (policy == SURF_LINK_FATPIPE)
272 lmm_constraint_shared(constraint());
274 s4u::Link::onCreation(this->piface_);
277 Action *CpuL07::execution_start(double size)
279 sg_host_t*host_list = xbt_new0(sg_host_t, 1);
280 double *flops_amount = xbt_new0(double, 1);
282 host_list[0] = getHost();
283 flops_amount[0] = size;
285 return static_cast<CpuL07Model*>(model())->hostModel_->executeParallelTask(1, host_list, flops_amount, nullptr, -1);
288 Action *CpuL07::sleep(double duration)
290 L07Action *action = static_cast<L07Action*>(execution_start(1.0));
291 action->maxDuration_ = duration;
292 action->suspended_ = 2;
293 lmm_update_variable_weight(model()->getMaxminSystem(), action->getVariable(), 0.0);
298 bool CpuL07::isUsed(){
299 return lmm_constraint_used(model()->getMaxminSystem(), constraint());
302 /** @brief take into account changes of speed (either load or max) */
303 void CpuL07::onSpeedChange() {
304 lmm_variable_t var = nullptr;
305 lmm_element_t elem = nullptr;
307 lmm_update_constraint_bound(model()->getMaxminSystem(), constraint(), speed_.peak * speed_.scale);
308 while ((var = lmm_get_var_from_cnst(model()->getMaxminSystem(), constraint(), &elem))) {
309 Action* action = static_cast<Action*>(lmm_variable_id(var));
311 lmm_update_variable_bound(model()->getMaxminSystem(), action->getVariable(), speed_.scale * speed_.peak);
314 Cpu::onSpeedChange();
318 bool LinkL07::isUsed(){
319 return lmm_constraint_used(model()->getMaxminSystem(), constraint());
322 void CpuL07::apply_event(tmgr_trace_iterator_t triggered, double value){
323 XBT_DEBUG("Updating cpu %s (%p) with value %g", cname(), this, value);
324 if (triggered == speed_.event) {
325 speed_.scale = value;
327 tmgr_trace_event_unref(&speed_.event);
329 } else if (triggered == stateEvent_) {
334 tmgr_trace_event_unref(&stateEvent_);
337 xbt_die("Unknown event!\n");
341 void LinkL07::apply_event(tmgr_trace_iterator_t triggered, double value) {
342 XBT_DEBUG("Updating link %s (%p) with value=%f", cname(), this, value);
343 if (triggered == bandwidth_.event) {
345 tmgr_trace_event_unref(&bandwidth_.event);
347 } else if (triggered == latency_.event) {
349 tmgr_trace_event_unref(&latency_.event);
351 } else if (triggered == stateEvent_) {
356 tmgr_trace_event_unref(&stateEvent_);
359 xbt_die("Unknown event ! \n");
363 void LinkL07::setBandwidth(double value)
365 bandwidth_.peak = value;
366 lmm_update_constraint_bound(model()->getMaxminSystem(), constraint(), bandwidth_.peak * bandwidth_.scale);
369 void LinkL07::setLatency(double value)
371 lmm_variable_t var = nullptr;
373 lmm_element_t elem = nullptr;
375 latency_.peak = value;
376 while ((var = lmm_get_var_from_cnst(model()->getMaxminSystem(), constraint(), &elem))) {
377 action = static_cast<L07Action*>(lmm_variable_id(var));
378 action->updateBound();
386 L07Action::~L07Action(){
388 free(communicationAmount_);
389 free(computationAmount_);
392 void L07Action::updateBound()
394 double lat_current = 0.0;
396 int hostNb = hostList_->size();
398 if (communicationAmount_ != nullptr) {
399 for (int i = 0; i < hostNb; i++) {
400 for (int j = 0; j < hostNb; j++) {
402 if (communicationAmount_[i * hostNb + j] > 0) {
404 std::vector<LinkImpl*> route;
405 hostList_->at(i)->routeTo(hostList_->at(j), &route, &lat);
407 lat_current = MAX(lat_current, lat * communicationAmount_[i * hostNb + j]);
412 double lat_bound = sg_tcp_gamma / (2.0 * lat_current);
413 XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
414 if ((latency_ == 0.0) && (suspended_ == 0)) {
416 lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), lat_bound);
418 lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), std::min(rate_, lat_bound));
422 int L07Action::unref()
426 if (action_hook.is_linked())
427 stateSet_->erase(stateSet_->iterator_to(*this));
429 lmm_variable_free(getModel()->getMaxminSystem(), getVariable());