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. */
11 #include "ptask_L07.hpp"
13 #include "cpu_interface.hpp"
14 #include "surf_routing.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(1);
39 surf_network_model = new NetworkL07Model(this,maxminSystem_);
40 surf_cpu_model_pm = new CpuL07Model(this,maxminSystem_);
42 routing_model_create(surf_network_model->createLink("__loopback__", 498000000, 0.000015, SURF_LINK_FATPIPE, nullptr));
45 HostL07Model::~HostL07Model() {
46 delete surf_cpu_model_pm;
47 delete surf_network_model;
50 CpuL07Model::CpuL07Model(HostL07Model *hmodel,lmm_system_t sys)
56 CpuL07Model::~CpuL07Model() {
57 surf_cpu_model_pm = nullptr;
58 lmm_system_free(maxminSystem_);
59 maxminSystem_ = nullptr;
61 NetworkL07Model::NetworkL07Model(HostL07Model *hmodel, lmm_system_t sys)
67 NetworkL07Model::~NetworkL07Model()
69 surf_network_model = nullptr;
70 maxminSystem_ = nullptr; // Avoid multi-free
74 double HostL07Model::next_occuring_event(double /*now*/)
76 ActionList *runningActions = getRunningActionSet();
77 double min = shareResourcesMaxMin(runningActions, maxminSystem_, bottleneck_solve);
79 for (auto it(runningActions->begin()), itend(runningActions->end()); it != itend ; ++it) {
80 L07Action *action = static_cast<L07Action*>(&*it);
81 if (action->m_latency > 0 && (min < 0 || action->m_latency < min)) {
82 min = action->m_latency;
83 XBT_DEBUG("Updating min with %p (start %f): %f", action, action->getStartTime(), min);
86 XBT_DEBUG("min value: %f", min);
91 void HostL07Model::updateActionsState(double /*now*/, double delta) {
94 ActionList *actionSet = getRunningActionSet();
96 for(ActionList::iterator it = actionSet->begin(), itNext = it
97 ; it != actionSet->end()
100 action = static_cast<L07Action*>(&*it);
101 if (action->m_latency > 0) {
102 if (action->m_latency > delta) {
103 double_update(&(action->m_latency), delta, sg_surf_precision);
105 action->m_latency = 0.0;
107 if ((action->m_latency == 0.0) && (action->isSuspended() == 0)) {
108 action->updateBound();
109 lmm_update_variable_weight(maxminSystem_, action->getVariable(), 1.0);
112 XBT_DEBUG("Action (%p) : remains (%g) updated by %g.",
113 action, action->getRemains(), lmm_variable_getvalue(action->getVariable()) * delta);
114 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
116 if (action->getMaxDuration() != NO_MAX_DURATION)
117 action->updateMaxDuration(delta);
119 XBT_DEBUG("Action (%p) : remains (%g).", action, action->getRemains());
121 /* In the next if cascade, the action can be finished either because:
122 * - The amount of remaining work reached 0
123 * - The max duration was reached
124 * If it's not done, it may have failed.
127 if ((action->getRemains() <= 0) &&
128 (lmm_get_variable_weight(action->getVariable()) > 0)) {
130 action->setState(Action::State::done);
131 } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
132 (action->getMaxDuration() <= 0)) {
134 action->setState(Action::State::done);
136 /* Need to check that none of the model has failed */
137 lmm_constraint_t cnst = nullptr;
140 while ((cnst = lmm_get_cnst_from_var(maxminSystem_, action->getVariable(), i++))) {
141 void *constraint_id = lmm_constraint_id(cnst);
142 if (static_cast<simgrid::surf::Resource*>(constraint_id)->isOff()) {
143 XBT_DEBUG("Action (%p) Failed!!", action);
145 action->setState(Action::State::failed);
154 Action *HostL07Model::executeParallelTask(int host_nb, sg_host_t *host_list,
155 double *flops_amount, double *bytes_amount,double rate) {
156 return new L07Action(this, host_nb, host_list, flops_amount, bytes_amount, rate);
160 L07Action::L07Action(Model *model, int host_nb, sg_host_t *host_list,
161 double *flops_amount, double *bytes_amount, double rate)
162 : CpuAction(model, 1, 0)
165 int nb_used_host = 0; /* Only the hosts with something to compute (>0 flops) are counted) */
166 double latency = 0.0;
168 this->p_netcardList->reserve(host_nb);
169 for (int i = 0; i<host_nb; i++)
170 this->p_netcardList->push_back(host_list[i]->pimpl_netcard);
172 /* Compute the number of affected resources... */
173 if(bytes_amount != nullptr) {
174 xbt_dict_t ptask_parallel_task_link_set = xbt_dict_new_homogeneous(nullptr);
176 for (int i = 0; i < host_nb; i++) {
177 for (int j = 0; j < host_nb; j++) {
179 if (bytes_amount[i * host_nb + j] > 0) {
181 std::vector<Link*> *route = new std::vector<Link*>();
183 routing_platf->getRouteAndLatency((*p_netcardList)[i], (*p_netcardList)[j], route, &lat);
184 latency = MAX(latency, lat);
186 for (auto link : *route)
187 xbt_dict_set(ptask_parallel_task_link_set, link->getName(), link, nullptr);
193 nb_link = xbt_dict_length(ptask_parallel_task_link_set);
194 xbt_dict_free(&ptask_parallel_task_link_set);
197 for (int i = 0; i < host_nb; i++)
198 if (flops_amount[i] > 0)
201 XBT_DEBUG("Creating a parallel task (%p) with %d hosts and %d unique links.", this, host_nb, nb_link);
202 this->p_computationAmount = flops_amount;
203 this->p_communicationAmount = bytes_amount;
204 this->m_latency = latency;
207 this->variable_ = lmm_variable_new(model->getMaxminSystem(), this, 1.0,
208 (rate > 0 ? rate : -1.0),
211 if (this->m_latency > 0)
212 lmm_update_variable_weight(model->getMaxminSystem(), this->getVariable(), 0.0);
214 for (int i = 0; i < host_nb; i++)
215 lmm_expand(model->getMaxminSystem(), host_list[i]->pimpl_cpu->getConstraint(),
216 this->getVariable(), flops_amount[i]);
218 if(bytes_amount != nullptr) {
219 for (int i = 0; i < host_nb; i++) {
220 for (int j = 0; j < host_nb; j++) {
222 if (bytes_amount[i * host_nb + j] == 0.0)
224 std::vector<Link*> *route = new std::vector<Link*>();
226 routing_platf->getRouteAndLatency((*p_netcardList)[i], (*p_netcardList)[j], route, nullptr);
228 for (auto link : *route)
229 lmm_expand_add(model->getMaxminSystem(), link->getConstraint(), this->getVariable(), bytes_amount[i * host_nb + j]);
236 if (nb_link + nb_used_host == 0) {
238 this->setRemains(0.0);
243 Action *NetworkL07Model::communicate(kernel::routing::NetCard *src, kernel::routing::NetCard *dst, double size, double rate)
245 sg_host_t*host_list = xbt_new0(sg_host_t, 2);
246 double *flops_amount = xbt_new0(double, 2);
247 double *bytes_amount = xbt_new0(double, 4);
249 host_list[0] = sg_host_by_name(src->name());
250 host_list[1] = sg_host_by_name(dst->name());
251 bytes_amount[1] = size;
253 return p_hostModel->executeParallelTask(2, host_list, flops_amount, bytes_amount, rate);
256 Cpu *CpuL07Model::createCpu(simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
258 return new CpuL07(this, host, speedPerPstate, core);
261 Link* NetworkL07Model::createLink(const char *name, double bandwidth, double latency,
262 e_surf_link_sharing_policy_t policy, xbt_dict_t properties)
264 return new LinkL07(this, name, properties, bandwidth, latency, policy);
271 CpuL07::CpuL07(CpuL07Model *model, simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
272 : Cpu(model, host, speedPerPstate, core)
274 constraint_ = lmm_constraint_new(model->getMaxminSystem(), this, speedPerPstate->front());
277 CpuL07::~CpuL07()=default;
279 LinkL07::LinkL07(NetworkL07Model *model, const char* name, xbt_dict_t props, double bandwidth, double latency,
280 e_surf_link_sharing_policy_t policy)
281 : Link(model, name, props, lmm_constraint_new(model->getMaxminSystem(), this, bandwidth))
283 m_bandwidth.peak = bandwidth;
284 m_latency.peak = latency;
286 if (policy == SURF_LINK_FATPIPE)
287 lmm_constraint_shared(getConstraint());
289 Link::onCreation(this);
292 Action *CpuL07::execution_start(double size)
294 sg_host_t*host_list = xbt_new0(sg_host_t, 1);
295 double *flops_amount = xbt_new0(double, 1);
297 host_list[0] = getHost();
298 flops_amount[0] = size;
300 return static_cast<CpuL07Model*>(getModel())->p_hostModel->executeParallelTask(1, host_list, flops_amount, nullptr, -1);
303 Action *CpuL07::sleep(double duration)
305 L07Action *action = static_cast<L07Action*>(execution_start(1.0));
306 action->maxDuration_ = duration;
307 action->suspended_ = 2;
308 lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), 0.0);
313 bool CpuL07::isUsed(){
314 return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
317 /** @brief take into account changes of speed (either load or max) */
318 void CpuL07::onSpeedChange() {
319 lmm_variable_t var = nullptr;
320 lmm_element_t elem = nullptr;
322 lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(), speed_.peak * speed_.scale);
323 while ((var = lmm_get_var_from_cnst (getModel()->getMaxminSystem(), getConstraint(), &elem))) {
324 Action *action = static_cast<Action*>(lmm_variable_id(var));
326 lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(), speed_.scale * speed_.peak);
329 Cpu::onSpeedChange();
333 bool LinkL07::isUsed(){
334 return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
337 void CpuL07::apply_event(tmgr_trace_iterator_t triggered, double value){
338 XBT_DEBUG("Updating cpu %s (%p) with value %g", getName(), this, value);
339 if (triggered == speed_.event) {
340 speed_.scale = value;
342 tmgr_trace_event_unref(&speed_.event);
344 } else if (triggered == stateEvent_) {
349 tmgr_trace_event_unref(&stateEvent_);
352 xbt_die("Unknown event!\n");
356 void LinkL07::apply_event(tmgr_trace_iterator_t triggered, double value) {
357 XBT_DEBUG("Updating link %s (%p) with value=%f", getName(), this, value);
358 if (triggered == m_bandwidth.event) {
359 updateBandwidth(value);
360 tmgr_trace_event_unref(&m_bandwidth.event);
362 } else if (triggered == m_latency.event) {
363 updateLatency(value);
364 tmgr_trace_event_unref(&m_latency.event);
366 } else if (triggered == m_stateEvent) {
371 tmgr_trace_event_unref(&m_stateEvent);
374 xbt_die("Unknown event ! \n");
378 void LinkL07::updateBandwidth(double value)
380 m_bandwidth.peak = value;
381 lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(), m_bandwidth.peak * m_bandwidth.scale);
384 void LinkL07::updateLatency(double value)
386 lmm_variable_t var = nullptr;
388 lmm_element_t elem = nullptr;
390 m_latency.peak = value;
391 while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), getConstraint(), &elem))) {
392 action = static_cast<L07Action*>(lmm_variable_id(var));
393 action->updateBound();
401 L07Action::~L07Action(){
402 delete p_netcardList;
403 free(p_communicationAmount);
404 free(p_computationAmount);
407 void L07Action::updateBound()
409 double lat_current = 0.0;
410 double lat_bound = -1.0;
413 int hostNb = p_netcardList->size();
415 if (p_communicationAmount != nullptr) {
416 for (i = 0; i < hostNb; i++) {
417 for (j = 0; j < hostNb; j++) {
419 if (p_communicationAmount[i * hostNb + j] > 0) {
421 std::vector<Link*> *route = new std::vector<Link*>();
422 routing_platf->getRouteAndLatency((*p_netcardList)[i], (*p_netcardList)[j], route, &lat);
424 lat_current = MAX(lat_current, lat * p_communicationAmount[i * hostNb + j]);
430 lat_bound = sg_tcp_gamma / (2.0 * lat_current);
431 XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
432 if ((m_latency == 0.0) && (suspended_ == 0)) {
434 lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), lat_bound);
436 lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), std::min(m_rate, lat_bound));
440 int L07Action::unref()
444 if (action_hook.is_linked())
445 stateSet_->erase(stateSet_->iterator_to(*this));
447 lmm_variable_free(getModel()->getMaxminSystem(), getVariable());