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 "host_ptask_L07.hpp"
13 #include "cpu_interface.hpp"
14 #include "surf_routing.hpp"
16 #include "src/surf/platform.hpp"
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_host);
19 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
21 /**************************************/
22 /*** Resource Creation & Destruction **/
23 /**************************************/
25 static void ptask_netlink_parse_init(sg_platf_link_cbarg_t link)
27 netlink_parse_init(link);
28 current_property_set = NULL;
31 void surf_host_model_init_ptask_L07(void)
33 XBT_CINFO(xbt_cfg,"Switching to the L07 model to handle parallel tasks.");
34 xbt_assert(!surf_cpu_model_pm, "CPU model type already defined");
35 xbt_assert(!surf_network_model, "network model type already defined");
37 // Define the callbacks to parse the XML
38 simgrid::surf::on_link.connect(ptask_netlink_parse_init);
40 surf_host_model = new simgrid::surf::HostL07Model();
41 xbt_dynar_push(all_existing_models, &surf_host_model);
48 HostL07Model::HostL07Model() : HostModel() {
49 p_maxminSystem = lmm_system_new(1);
50 surf_network_model = new NetworkL07Model(this,p_maxminSystem);
51 surf_cpu_model_pm = new CpuL07Model(this,p_maxminSystem);
53 routing_model_create(surf_network_model->createLink("__loopback__",
57 SURF_LINK_FATPIPE, NULL));
60 HostL07Model::~HostL07Model() {
61 delete surf_cpu_model_pm;
62 delete surf_network_model;
65 CpuL07Model::CpuL07Model(HostL07Model *hmodel,lmm_system_t sys)
71 CpuL07Model::~CpuL07Model() {
72 surf_cpu_model_pm = NULL;
73 lmm_system_free(p_maxminSystem);
74 p_maxminSystem = NULL;
76 NetworkL07Model::NetworkL07Model(HostL07Model *hmodel, lmm_system_t sys)
82 NetworkL07Model::~NetworkL07Model()
84 surf_network_model = NULL;
85 p_maxminSystem = NULL; // Avoid multi-free
89 double HostL07Model::next_occuring_event(double /*now*/)
93 ActionList *running_actions = getRunningActionSet();
94 double min = this->shareResourcesMaxMin(running_actions,
98 for(ActionList::iterator it(running_actions->begin()), itend(running_actions->end())
99 ; it != itend ; ++it) {
100 action = static_cast<L07Action*>(&*it);
101 if (action->m_latency > 0) {
103 min = action->m_latency;
104 XBT_DEBUG("Updating min (value) with %p (start %f): %f", action,
105 action->getStartTime(), min);
106 } else if (action->m_latency < min) {
107 min = action->m_latency;
108 XBT_DEBUG("Updating min (latency) with %p (start %f): %f", action,
109 action->getStartTime(), min);
114 XBT_DEBUG("min value : %f", min);
119 void HostL07Model::updateActionsState(double /*now*/, double delta) {
122 ActionList *actionSet = getRunningActionSet();
124 for(ActionList::iterator it = actionSet->begin(), itNext = it
125 ; it != actionSet->end()
128 action = static_cast<L07Action*>(&*it);
129 if (action->m_latency > 0) {
130 if (action->m_latency > delta) {
131 double_update(&(action->m_latency), delta, sg_surf_precision);
133 action->m_latency = 0.0;
135 if ((action->m_latency == 0.0) && (action->isSuspended() == 0)) {
136 action->updateBound();
137 lmm_update_variable_weight(p_maxminSystem, action->getVariable(), 1.0);
140 XBT_DEBUG("Action (%p) : remains (%g) updated by %g.",
141 action, action->getRemains(), lmm_variable_getvalue(action->getVariable()) * delta);
142 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
144 if (action->getMaxDuration() != NO_MAX_DURATION)
145 action->updateMaxDuration(delta);
147 XBT_DEBUG("Action (%p) : remains (%g).", action, action->getRemains());
149 /* In the next if cascade, the action can be finished either because:
150 * - The amount of remaining work reached 0
151 * - The max duration was reached
152 * If it's not done, it may have failed.
155 if ((action->getRemains() <= 0) &&
156 (lmm_get_variable_weight(action->getVariable()) > 0)) {
158 action->setState(SURF_ACTION_DONE);
159 } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
160 (action->getMaxDuration() <= 0)) {
162 action->setState(SURF_ACTION_DONE);
164 /* Need to check that none of the model has failed */
165 lmm_constraint_t cnst = NULL;
168 while ((cnst = lmm_get_cnst_from_var(p_maxminSystem, action->getVariable(), i++))) {
169 void *constraint_id = lmm_constraint_id(cnst);
171 if (static_cast<Host*>(constraint_id)->isOff()) {
172 XBT_DEBUG("Action (%p) Failed!!", action);
174 action->setState(SURF_ACTION_FAILED);
183 Action *HostL07Model::executeParallelTask(int host_nb, sg_host_t *host_list,
184 double *flops_amount, double *bytes_amount,
186 return new L07Action(this, host_nb, host_list, flops_amount, bytes_amount, rate);
190 L07Action::L07Action(Model *model, int host_nb,
192 double *flops_amount,
193 double *bytes_amount,
195 : CpuAction(model, 1, 0)
199 int nb_used_host = 0; /* Only the hosts with something to compute (>0 flops) are counted) */
200 double latency = 0.0;
202 this->p_netcardList->reserve(host_nb);
203 for (int i = 0; i<host_nb; i++)
204 this->p_netcardList->push_back(host_list[i]->pimpl_netcard);
206 /* Compute the number of affected resources... */
207 if(bytes_amount != NULL) {
208 xbt_dict_t ptask_parallel_task_link_set = xbt_dict_new_homogeneous(NULL);
210 for (int i = 0; i < host_nb; i++) {
211 for (int j = 0; j < host_nb; j++) {
213 if (bytes_amount[i * host_nb + j] > 0) {
215 xbt_dynar_t route=NULL;
217 routing_platf->getRouteAndLatency((*p_netcardList)[i], (*p_netcardList)[j], &route, &lat);
218 latency = MAX(latency, lat);
221 xbt_dynar_foreach(route, cpt, _link) {
222 LinkL07 *link = static_cast<LinkL07*>(_link);
223 xbt_dict_set(ptask_parallel_task_link_set, link->getName(), link, NULL);
229 nb_link = xbt_dict_length(ptask_parallel_task_link_set);
230 xbt_dict_free(&ptask_parallel_task_link_set);
233 for (int i = 0; i < host_nb; i++)
234 if (flops_amount[i] > 0)
237 XBT_DEBUG("Creating a parallel task (%p) with %d hosts and %d unique links.", this, host_nb, nb_link);
238 this->p_computationAmount = flops_amount;
239 this->p_communicationAmount = bytes_amount;
240 this->m_latency = latency;
243 this->p_variable = lmm_variable_new(model->getMaxminSystem(), this, 1.0,
244 (rate > 0 ? rate : -1.0),
247 if (this->m_latency > 0)
248 lmm_update_variable_weight(model->getMaxminSystem(), this->getVariable(), 0.0);
250 for (int i = 0; i < host_nb; i++)
251 lmm_expand(model->getMaxminSystem(), host_list[i]->pimpl_cpu->getConstraint(),
252 this->getVariable(), flops_amount[i]);
254 if(bytes_amount != NULL) {
255 for (int i = 0; i < host_nb; i++) {
256 for (int j = 0; j < host_nb; j++) {
258 xbt_dynar_t route=NULL;
259 if (bytes_amount[i * host_nb + j] == 0.0)
262 routing_platf->getRouteAndLatency((*p_netcardList)[i], (*p_netcardList)[j],
266 xbt_dynar_foreach(route, cpt, _link) {
267 LinkL07 *link = static_cast<LinkL07*>(_link);
268 lmm_expand_add(model->getMaxminSystem(), link->getConstraint(),
269 this->getVariable(), bytes_amount[i * host_nb + j]);
275 if (nb_link + nb_used_host == 0) {
277 this->setRemains(0.0);
282 Action *NetworkL07Model::communicate(NetCard *src, NetCard *dst,
283 double size, double rate)
285 sg_host_t*host_list = xbt_new0(sg_host_t, 2);
286 double *flops_amount = xbt_new0(double, 2);
287 double *bytes_amount = xbt_new0(double, 4);
290 host_list[0] = sg_host_by_name(src->getName());
291 host_list[1] = sg_host_by_name(dst->getName());
292 bytes_amount[1] = size;
294 res = p_hostModel->executeParallelTask(2, host_list, flops_amount, bytes_amount, rate);
299 Cpu *CpuL07Model::createCpu(simgrid::s4u::Host *host, xbt_dynar_t powerPeakList,
300 int pstate, double power_scale,
301 tmgr_trace_t power_trace, int core,
303 tmgr_trace_t state_trace)
305 CpuL07 *cpu = new CpuL07(this, host, powerPeakList, pstate, power_scale, power_trace,
306 core, initiallyOn, state_trace);
310 Link* NetworkL07Model::createLink(const char *name,
312 tmgr_trace_t bw_trace,
314 tmgr_trace_t lat_trace,
316 tmgr_trace_t state_trace,
317 e_surf_link_sharing_policy_t policy,
318 xbt_dict_t properties)
320 xbt_assert(!Link::byName(name),
321 "Link '%s' declared several times in the platform file.", name);
323 Link* link = new LinkL07(this, name, properties,
324 bw_initial, bw_trace,
325 lat_initial, lat_trace,
326 initiallyOn, state_trace,
328 Link::onCreation(link);
336 CpuL07::CpuL07(CpuL07Model *model, simgrid::s4u::Host *host,
337 xbt_dynar_t speedPeakList, int pstate,
338 double speedScale, tmgr_trace_t speedTrace,
339 int core, int initiallyOn, tmgr_trace_t state_trace)
340 : Cpu(model, host, speedPeakList, pstate,
341 core, xbt_dynar_get_as(speedPeakList,pstate,double), speedScale, initiallyOn)
343 p_constraint = lmm_constraint_new(model->getMaxminSystem(), this, xbt_dynar_get_as(speedPeakList,pstate,double) * speedScale);
346 p_speed.event = future_evt_set->add_trace(speedTrace, 0.0, this);
349 p_stateEvent = future_evt_set->add_trace(state_trace, 0.0, this);
356 LinkL07::LinkL07(NetworkL07Model *model, const char* name, xbt_dict_t props,
357 double bw_initial, tmgr_trace_t bw_trace,
358 double lat_initial, tmgr_trace_t lat_trace,
359 int initiallyOn, tmgr_trace_t state_trace,
360 e_surf_link_sharing_policy_t policy)
361 : Link(model, name, props, lmm_constraint_new(model->getMaxminSystem(), this, bw_initial), state_trace)
363 m_bandwidth.peak = bw_initial;
365 m_bandwidth.event = future_evt_set->add_trace(bw_trace, 0.0, this);
372 m_latency.peak = lat_initial;
374 m_latency.event = future_evt_set->add_trace(lat_trace, 0.0, this);
376 if (policy == SURF_LINK_FATPIPE)
377 lmm_constraint_shared(getConstraint());
380 Action *CpuL07::execution_start(double size)
382 sg_host_t*host_list = xbt_new0(sg_host_t, 1);
383 double *flops_amount = xbt_new0(double, 1);
385 host_list[0] = getHost();
386 flops_amount[0] = size;
388 return static_cast<CpuL07Model*>(getModel())->p_hostModel
389 ->executeParallelTask( 1, host_list, flops_amount, NULL, -1);
392 Action *CpuL07::sleep(double duration)
394 L07Action *action = static_cast<L07Action*>(execution_start(1.0));
395 action->m_maxDuration = duration;
396 action->m_suspended = 2;
397 lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), 0.0);
402 bool CpuL07::isUsed(){
403 return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
406 /** @brief take into account changes of speed (either load or max) */
407 void CpuL07::onSpeedChange() {
408 lmm_variable_t var = NULL;
409 lmm_element_t elem = NULL;
411 lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(), p_speed.peak * p_speed.scale);
412 while ((var = lmm_get_var_from_cnst
413 (getModel()->getMaxminSystem(), getConstraint(), &elem))) {
414 Action *action = static_cast<Action*>(lmm_variable_id(var));
416 lmm_update_variable_bound(getModel()->getMaxminSystem(),
417 action->getVariable(),
418 p_speed.scale * p_speed.peak);
421 Cpu::onSpeedChange();
425 bool LinkL07::isUsed(){
426 return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
429 void CpuL07::apply_event(tmgr_trace_iterator_t triggered, double value){
430 XBT_DEBUG("Updating cpu %s (%p) with value %g", getName(), this, value);
431 if (triggered == p_speed.event) {
432 p_speed.scale = value;
434 tmgr_trace_event_unref(&p_speed.event);
435 } else if (triggered == p_stateEvent) {
440 tmgr_trace_event_unref(&p_stateEvent);
442 xbt_die("Unknown event!\n");
446 void LinkL07::apply_event(tmgr_trace_iterator_t triggered, double value) {
447 XBT_DEBUG("Updating link %s (%p) with value=%f", getName(), this, value);
448 if (triggered == m_bandwidth.event) {
449 updateBandwidth(value);
450 tmgr_trace_event_unref(&m_bandwidth.event);
451 } else if (triggered == m_latency.event) {
452 updateLatency(value);
453 tmgr_trace_event_unref(&m_latency.event);
454 } else if (triggered == m_stateEvent) {
459 tmgr_trace_event_unref(&m_stateEvent);
461 xbt_die("Unknown event ! \n");
465 void LinkL07::updateBandwidth(double value)
467 m_bandwidth.peak = value;
468 lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(), m_bandwidth.peak * m_bandwidth.scale);
471 void LinkL07::updateLatency(double value)
473 lmm_variable_t var = NULL;
475 lmm_element_t elem = NULL;
477 m_latency.peak = value;
478 while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), getConstraint(), &elem))) {
479 action = static_cast<L07Action*>(lmm_variable_id(var));
480 action->updateBound();
488 L07Action::~L07Action(){
489 delete p_netcardList;
490 free(p_communicationAmount);
491 free(p_computationAmount);
494 void L07Action::updateBound()
496 double lat_current = 0.0;
497 double lat_bound = -1.0;
500 int hostNb = p_netcardList->size();
502 if (p_communicationAmount != NULL) {
503 for (i = 0; i < hostNb; i++) {
504 for (j = 0; j < hostNb; j++) {
505 xbt_dynar_t route=NULL;
507 if (p_communicationAmount[i * hostNb + j] > 0) {
509 routing_platf->getRouteAndLatency((*p_netcardList)[i], (*p_netcardList)[j],
512 lat_current = MAX(lat_current, lat * p_communicationAmount[i * hostNb + j]);
517 lat_bound = sg_tcp_gamma / (2.0 * lat_current);
518 XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
519 if ((m_latency == 0.0) && (m_suspended == 0)) {
521 lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), lat_bound);
523 lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(),
524 std::min(m_rate, lat_bound));
528 int L07Action::unref()
532 if (action_hook.is_linked())
533 p_stateSet->erase(p_stateSet->iterator_to(*this));
535 lmm_variable_free(getModel()->getMaxminSystem(), getVariable());