1 /* Copyright (c) 2007-2018. The SimGrid Team. All rights reserved. */
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. */
9 #include <unordered_set>
11 #include "ptask_L07.hpp"
13 #include "cpu_interface.hpp"
14 #include "xbt/utility.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_ = new simgrid::kernel::lmm::System(true /* lazy */);
38 maxminSystem_->solve_fun = &simgrid::kernel::lmm::bottleneck_solve;
39 surf_network_model = new NetworkL07Model(this,maxminSystem_);
40 surf_cpu_model_pm = new CpuL07Model(this,maxminSystem_);
43 HostL07Model::~HostL07Model()
45 delete surf_network_model;
46 delete surf_cpu_model_pm;
49 CpuL07Model::CpuL07Model(HostL07Model* hmodel, lmm_system_t sys) : CpuModel(), hostModel_(hmodel)
54 CpuL07Model::~CpuL07Model()
56 maxminSystem_ = nullptr;
59 NetworkL07Model::NetworkL07Model(HostL07Model* hmodel, lmm_system_t sys) : NetworkModel(), hostModel_(hmodel)
62 loopback_ = NetworkL07Model::createLink("__loopback__", 498000000, 0.000015, SURF_LINK_FATPIPE);
65 NetworkL07Model::~NetworkL07Model()
67 maxminSystem_ = nullptr;
70 double HostL07Model::nextOccuringEvent(double now)
72 double min = HostModel::nextOccuringEventFull(now);
73 for (kernel::resource::Action const& action : *getRunningActionSet()) {
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.getStartTime(), min);
80 XBT_DEBUG("min value: %f", min);
85 void HostL07Model::updateActionsState(double /*now*/, double delta)
87 for (auto it = std::begin(*getRunningActionSet()); it != std::end(*getRunningActionSet());) {
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);
94 action.latency_ = 0.0;
96 if ((action.latency_ <= 0.0) && (action.isSuspended() == 0)) {
98 maxminSystem_->update_variable_weight(action.getVariable(), 1.0);
101 XBT_DEBUG("Action (%p) : remains (%g) updated by %g.", &action, action.getRemains(),
102 action.getVariable()->get_value() * delta);
103 action.updateRemains(action.getVariable()->get_value() * delta);
105 if (action.getMaxDuration() > NO_MAX_DURATION)
106 action.updateMaxDuration(delta);
108 XBT_DEBUG("Action (%p) : remains (%g).", &action, action.getRemains());
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.
116 if (((action.getRemains() <= 0) && (action.getVariable()->get_weight() > 0)) ||
117 ((action.getMaxDuration() > NO_MAX_DURATION) && (action.getMaxDuration() <= 0))) {
118 action.finish(kernel::resource::Action::State::done);
120 /* Need to check that none of the model has failed */
122 kernel::lmm::Constraint* cnst = action.getVariable()->get_constraint(i);
123 while (cnst != nullptr) {
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);
131 cnst = action.getVariable()->get_constraint(i);
137 kernel::resource::Action* HostL07Model::executeParallelTask(int host_nb, sg_host_t* host_list, double* flops_amount,
138 double* bytes_amount, double rate)
140 return new L07Action(this, host_nb, host_list, flops_amount, bytes_amount, rate);
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)
148 int nb_used_host = 0; /* Only the hosts with something to compute (>0 flops) are counted) */
149 double latency = 0.0;
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)
158 /* Compute the number of affected resources... */
159 if(bytes_amount != nullptr) {
160 std::unordered_set<const char*> affected_links;
162 for (int i = 0; i < host_nb; i++) {
163 for (int j = 0; j < host_nb; j++) {
165 if (bytes_amount[i * host_nb + j] > 0) {
168 std::vector<LinkImpl*> route;
169 hostList_->at(i)->routeTo(hostList_->at(j), route, &lat);
170 latency = std::max(latency, lat);
172 for (auto const& link : route)
173 affected_links.insert(link->getCname());
178 nb_link = affected_links.size();
181 XBT_DEBUG("Creating a parallel task (%p) with %d hosts and %d unique links.", this, host_nb, nb_link);
184 setVariable(model->getMaxminSystem()->variable_new(this, 1.0, (rate > 0 ? rate : -1.0), host_nb + nb_link));
187 model->getMaxminSystem()->update_variable_weight(getVariable(), 0.0);
189 for (int i = 0; i < host_nb; i++)
190 model->getMaxminSystem()->expand(host_list[i]->pimpl_cpu->constraint(), getVariable(), flops_amount[i]);
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);
199 for (auto const& link : route)
200 model->getMaxminSystem()->expand_add(link->constraint(), this->getVariable(),
201 bytes_amount[i * host_nb + j]);
207 if (nb_link + nb_used_host == 0) {
209 this->setRemains(0.0);
214 kernel::resource::Action* NetworkL07Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double rate)
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]();
222 bytes_amount[1] = size;
224 return hostModel_->executeParallelTask(2, host_list, flops_amount, bytes_amount, rate);
227 Cpu *CpuL07Model::createCpu(simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
229 return new CpuL07(this, host, speedPerPstate, core);
232 LinkImpl* NetworkL07Model::createLink(const std::string& name, double bandwidth, double latency,
233 e_surf_link_sharing_policy_t policy)
235 return new LinkL07(this, name, bandwidth, latency, policy);
242 CpuL07::CpuL07(CpuL07Model* model, simgrid::s4u::Host* host, std::vector<double>* speedPerPstate, int core)
243 : Cpu(model, host, model->getMaxminSystem()->constraint_new(this, speedPerPstate->front()), speedPerPstate, core)
247 CpuL07::~CpuL07()=default;
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->getMaxminSystem()->constraint_new(this, bandwidth))
253 bandwidth_.peak = bandwidth;
254 latency_.peak = latency;
256 if (policy == SURF_LINK_FATPIPE)
257 constraint()->unshare();
259 s4u::Link::onCreation(this->piface_);
262 kernel::resource::Action* CpuL07::execution_start(double size)
264 sg_host_t* host_list = new sg_host_t[1]();
265 double* flops_amount = new double[1]();
267 host_list[0] = getHost();
268 flops_amount[0] = size;
270 return static_cast<CpuL07Model*>(model())->hostModel_->executeParallelTask(1, host_list, flops_amount, nullptr, -1);
273 kernel::resource::Action* CpuL07::sleep(double duration)
275 L07Action *action = static_cast<L07Action*>(execution_start(1.0));
276 action->setMaxDuration(duration);
277 action->suspended_ = kernel::resource::Action::SuspendStates::sleeping;
278 model()->getMaxminSystem()->update_variable_weight(action->getVariable(), 0.0);
283 bool CpuL07::isUsed(){
284 return model()->getMaxminSystem()->constraint_used(constraint());
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;
292 model()->getMaxminSystem()->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());
296 model()->getMaxminSystem()->update_variable_bound(action->getVariable(), speed_.scale * speed_.peak);
299 Cpu::onSpeedChange();
303 bool LinkL07::isUsed(){
304 return model()->getMaxminSystem()->constraint_used(constraint());
307 void CpuL07::apply_event(tmgr_trace_event_t triggered, double value)
309 XBT_DEBUG("Updating cpu %s (%p) with value %g", getCname(), this, value);
310 if (triggered == speed_.event) {
311 speed_.scale = value;
313 tmgr_trace_event_unref(&speed_.event);
315 } else if (triggered == stateEvent_) {
320 tmgr_trace_event_unref(&stateEvent_);
323 xbt_die("Unknown event!\n");
327 void LinkL07::apply_event(tmgr_trace_event_t triggered, double value)
329 XBT_DEBUG("Updating link %s (%p) with value=%f", getCname(), this, value);
330 if (triggered == bandwidth_.event) {
332 tmgr_trace_event_unref(&bandwidth_.event);
334 } else if (triggered == latency_.event) {
336 tmgr_trace_event_unref(&latency_.event);
338 } else if (triggered == stateEvent_) {
343 tmgr_trace_event_unref(&stateEvent_);
346 xbt_die("Unknown event ! \n");
350 void LinkL07::setBandwidth(double value)
352 bandwidth_.peak = value;
353 model()->getMaxminSystem()->update_constraint_bound(constraint(), bandwidth_.peak * bandwidth_.scale);
356 void LinkL07::setLatency(double value)
358 kernel::lmm::Variable* var = nullptr;
360 const_lmm_element_t elem = nullptr;
362 latency_.peak = value;
363 while ((var = constraint()->get_variable(&elem))) {
364 action = static_cast<L07Action*>(var->get_id());
365 action->updateBound();
368 LinkL07::~LinkL07() = default;
374 L07Action::~L07Action(){
376 delete[] communicationAmount_;
377 delete[] computationAmount_;
380 void L07Action::updateBound()
382 double lat_current = 0.0;
384 int hostNb = hostList_->size();
386 if (communicationAmount_ != nullptr) {
387 for (int i = 0; i < hostNb; i++) {
388 for (int j = 0; j < hostNb; j++) {
390 if (communicationAmount_[i * hostNb + j] > 0) {
392 std::vector<LinkImpl*> route;
393 hostList_->at(i)->routeTo(hostList_->at(j), route, &lat);
395 lat_current = std::max(lat_current, lat * communicationAmount_[i * hostNb + j]);
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)) {
404 getModel()->getMaxminSystem()->update_variable_bound(getVariable(), lat_bound);
406 getModel()->getMaxminSystem()->update_variable_bound(getVariable(), std::min(rate_, lat_bound));
410 int L07Action::unref()
414 if (stateSetHook_.is_linked())
415 simgrid::xbt::intrusive_erase(*stateSet_, *this);
417 getModel()->getMaxminSystem()->variable_free(getVariable());