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);
20 /**************************************/
21 /*** Resource Creation & Destruction **/
22 /**************************************/
24 static void ptask_netlink_parse_init(sg_platf_link_cbarg_t link)
26 netlink_parse_init(link);
27 current_property_set = NULL;
30 void surf_host_model_init_ptask_L07(void)
32 XBT_INFO("Switching to the L07 model to handle parallel tasks.");
33 xbt_assert(!surf_cpu_model_pm, "CPU model type already defined");
34 xbt_assert(!surf_network_model, "network model type already defined");
36 // Define the callbacks to parse the XML
37 simgrid::surf::on_link.connect(ptask_netlink_parse_init);
38 simgrid::surf::on_postparse.connect(host_add_traces);
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 p_maxminSystem = NULL; // Avoid multi-free
75 NetworkL07Model::NetworkL07Model(HostL07Model *hmodel, lmm_system_t sys)
81 NetworkL07Model::~NetworkL07Model()
83 surf_network_model = NULL;
84 p_maxminSystem = NULL; // Avoid multi-free
88 double HostL07Model::shareResources(double /*now*/)
92 ActionList *running_actions = getRunningActionSet();
93 double min = this->shareResourcesMaxMin(running_actions,
97 for(ActionList::iterator it(running_actions->begin()), itend(running_actions->end())
98 ; it != itend ; ++it) {
99 action = static_cast<L07Action*>(&*it);
100 if (action->m_latency > 0) {
102 min = action->m_latency;
103 XBT_DEBUG("Updating min (value) with %p (start %f): %f", action,
104 action->getStartTime(), min);
105 } else if (action->m_latency < min) {
106 min = action->m_latency;
107 XBT_DEBUG("Updating min (latency) with %p (start %f): %f", action,
108 action->getStartTime(), min);
113 XBT_DEBUG("min value : %f", min);
118 void HostL07Model::updateActionsState(double /*now*/, double delta) {
121 ActionList *actionSet = getRunningActionSet();
123 for(ActionList::iterator it = actionSet->begin(), itNext = it
124 ; it != actionSet->end()
127 action = static_cast<L07Action*>(&*it);
128 if (action->m_latency > 0) {
129 if (action->m_latency > delta) {
130 double_update(&(action->m_latency), delta, sg_surf_precision);
132 action->m_latency = 0.0;
134 if ((action->m_latency == 0.0) && (action->isSuspended() == 0)) {
135 action->updateBound();
136 lmm_update_variable_weight(p_maxminSystem, action->getVariable(), 1.0);
139 XBT_DEBUG("Action (%p) : remains (%g) updated by %g.",
140 action, action->getRemains(), lmm_variable_getvalue(action->getVariable()) * delta);
141 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
143 if (action->getMaxDuration() != NO_MAX_DURATION)
144 action->updateMaxDuration(delta);
146 XBT_DEBUG("Action (%p) : remains (%g).", action, action->getRemains());
148 /* In the next if cascade, the action can be finished either because:
149 * - The amount of remaining work reached 0
150 * - The max duration was reached
151 * If it's not done, it may have failed.
154 if ((action->getRemains() <= 0) &&
155 (lmm_get_variable_weight(action->getVariable()) > 0)) {
157 action->setState(SURF_ACTION_DONE);
158 } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
159 (action->getMaxDuration() <= 0)) {
161 action->setState(SURF_ACTION_DONE);
163 /* Need to check that none of the model has failed */
164 lmm_constraint_t cnst = NULL;
167 while ((cnst = lmm_get_cnst_from_var(p_maxminSystem, action->getVariable(), i++))) {
168 void *constraint_id = lmm_constraint_id(cnst);
170 if (static_cast<Host*>(constraint_id)->isOff()) {
171 XBT_DEBUG("Action (%p) Failed!!", action);
173 action->setState(SURF_ACTION_FAILED);
182 Action *HostL07Model::executeParallelTask(int host_nb, sg_host_t *host_list,
183 double *flops_amount, double *bytes_amount,
185 return new L07Action(this, host_nb, host_list, flops_amount, bytes_amount, rate);
189 L07Action::L07Action(Model *model, int host_nb,
191 double *flops_amount,
192 double *bytes_amount,
194 : CpuAction(model, 1, 0)
198 int nb_used_host = 0; /* Only the hosts with something to compute (>0 flops) are counted) */
199 double latency = 0.0;
201 xbt_dict_t ptask_parallel_task_link_set = xbt_dict_new_homogeneous(NULL);
203 this->p_netcardList->reserve(host_nb);
204 for (int i = 0; i<host_nb; i++)
205 this->p_netcardList->push_back(host_list[i]->pimpl_netcard);
207 /* Compute the number of affected resources... */
208 for (int i = 0; i < host_nb; i++) {
209 for (int j = 0; j < host_nb; j++) {
210 xbt_dynar_t route=NULL;
212 if (bytes_amount[i * host_nb + j] > 0) {
218 routing_platf->getRouteAndLatency((*this->p_netcardList)[i], (*this->p_netcardList)[j],
220 latency = MAX(latency, lat);
222 xbt_dynar_foreach(route, cpt, _link) {
223 link = static_cast<LinkL07*>(_link);
224 xbt_dict_set(ptask_parallel_task_link_set, link->getName(), link, NULL);
230 nb_link = xbt_dict_length(ptask_parallel_task_link_set);
231 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 cpus and %d links.",
238 this, host_nb, nb_link);
239 this->p_computationAmount = flops_amount;
240 this->p_communicationAmount = bytes_amount;
241 this->m_latency = latency;
244 this->p_variable = lmm_variable_new(model->getMaxminSystem(), this, 1.0,
245 (rate > 0 ? rate : -1.0),
248 if (this->m_latency > 0)
249 lmm_update_variable_weight(model->getMaxminSystem(), this->getVariable(), 0.0);
251 for (int i = 0; i < host_nb; i++)
252 lmm_expand(model->getMaxminSystem(),
253 host_list[i]->pimpl_cpu->getConstraint(),
254 this->getVariable(), flops_amount[i]);
256 for (int i = 0; i < host_nb; i++) {
257 for (int j = 0; j < host_nb; j++) {
260 xbt_dynar_t route=NULL;
261 if (bytes_amount[i * host_nb + j] == 0.0)
264 routing_platf->getRouteAndLatency((*this->p_netcardList)[i], (*this->p_netcardList)[j],
267 xbt_dynar_foreach(route, cpt, _link) {
268 LinkL07 *link = static_cast<LinkL07*>(_link);
269 lmm_expand_add(model->getMaxminSystem(), link->getConstraint(),
271 bytes_amount[i * host_nb + j]);
276 if (nb_link + nb_used_host == 0) {
278 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,
301 Cpu *CpuL07Model::createCpu(simgrid::s4u::Host *host, xbt_dynar_t powerPeakList,
302 int pstate, double power_scale,
303 tmgr_trace_t power_trace, int core,
305 tmgr_trace_t state_trace)
307 CpuL07 *cpu = new CpuL07(this, host, powerPeakList, pstate, power_scale, power_trace,
308 core, initiallyOn, state_trace);
312 Link* NetworkL07Model::createLink(const char *name,
314 tmgr_trace_t bw_trace,
316 tmgr_trace_t lat_trace,
318 tmgr_trace_t state_trace,
319 e_surf_link_sharing_policy_t policy,
320 xbt_dict_t properties)
322 xbt_assert(!Link::byName(name),
323 "Link '%s' declared several times in the platform file.", name);
325 Link* link = new LinkL07(this, name, properties,
326 bw_initial, bw_trace,
327 lat_initial, lat_trace,
328 initiallyOn, state_trace,
330 Link::onCreation(link);
334 void HostL07Model::addTraces()
336 xbt_dict_cursor_t cursor = NULL;
337 char *trace_name, *elm;
339 if (!trace_connect_list_host_avail)
342 /* Connect traces relative to cpu */
343 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
344 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
345 CpuL07 *host = static_cast<CpuL07*>(sg_host_by_name(elm)->pimpl_cpu);
347 xbt_assert(host, "Host %s undefined", elm);
348 xbt_assert(trace, "Trace %s undefined", trace_name);
350 host->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, host);
353 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
354 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
355 CpuL07 *host = static_cast<CpuL07*>(sg_host_by_name(elm)->pimpl_cpu);
357 xbt_assert(host, "Host %s undefined", elm);
358 xbt_assert(trace, "Trace %s undefined", trace_name);
360 host->p_speedEvent = tmgr_history_add_trace(history, trace, 0.0, 0, host);
363 /* Connect traces relative to network */
364 xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
365 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
366 LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
368 xbt_assert(link, "Link %s undefined", elm);
369 xbt_assert(trace, "Trace %s undefined", trace_name);
371 link->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
374 xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
375 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
376 LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
378 xbt_assert(link, "Link %s undefined", elm);
379 xbt_assert(trace, "Trace %s undefined", trace_name);
381 link->p_bwEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
384 xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
385 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
386 LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
388 xbt_assert(link, "Link %s undefined", elm);
389 xbt_assert(trace, "Trace %s undefined", trace_name);
391 link->p_latEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
399 CpuL07::CpuL07(CpuL07Model *model, simgrid::s4u::Host *host,
400 xbt_dynar_t speedPeakList, int pstate,
401 double speedScale, tmgr_trace_t speedTrace,
402 int core, int initiallyOn, tmgr_trace_t state_trace)
403 : Cpu(model, host, speedPeakList, pstate,
404 core, xbt_dynar_get_as(speedPeakList,pstate,double), speedScale, initiallyOn)
406 p_constraint = lmm_constraint_new(model->getMaxminSystem(), this, xbt_dynar_get_as(speedPeakList,pstate,double) * speedScale);
409 p_speedEvent = tmgr_history_add_trace(history, speedTrace, 0.0, 0, this);
414 p_stateEvent = tmgr_history_add_trace(history, state_trace, 0.0, 0, this);
421 LinkL07::LinkL07(NetworkL07Model *model, const char* name, xbt_dict_t props,
423 tmgr_trace_t bw_trace,
425 tmgr_trace_t lat_trace,
427 tmgr_trace_t state_trace,
428 e_surf_link_sharing_policy_t policy)
429 : Link(model, name, props, lmm_constraint_new(model->getMaxminSystem(), this, bw_initial), history, state_trace)
431 m_bwCurrent = bw_initial;
433 p_bwEvent = tmgr_history_add_trace(history, bw_trace, 0.0, 0, this);
439 m_latCurrent = lat_initial;
442 p_latEvent = tmgr_history_add_trace(history, lat_trace, 0.0, 0, this);
444 if (policy == SURF_LINK_FATPIPE)
445 lmm_constraint_shared(getConstraint());
448 Action *CpuL07::execute(double size)
450 sg_host_t*host_list = xbt_new0(sg_host_t, 1);
451 double *flops_amount = xbt_new0(double, 1);
452 double *bytes_amount = xbt_new0(double, 1);
454 host_list[0] = getHost();
455 flops_amount[0] = size;
457 return static_cast<CpuL07Model*>(getModel())
459 ->executeParallelTask( 1, host_list, flops_amount, bytes_amount, -1);
462 Action *CpuL07::sleep(double duration)
464 L07Action *action = NULL;
466 XBT_IN("(%s,%g)", getName(), duration);
468 action = static_cast<L07Action*>(execute(1.0));
469 action->m_maxDuration = duration;
470 action->m_suspended = 2;
471 lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), 0.0);
477 bool CpuL07::isUsed(){
478 return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
481 /** @brief take into account changes of speed (either load or max) */
482 void CpuL07::onSpeedChange() {
483 lmm_variable_t var = NULL;
484 lmm_element_t elem = NULL;
486 lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(), m_speedPeak * m_speedScale);
487 while ((var = lmm_get_var_from_cnst
488 (getModel()->getMaxminSystem(), getConstraint(), &elem))) {
489 Action *action = static_cast<Action*>(lmm_variable_id(var));
491 lmm_update_variable_bound(getModel()->getMaxminSystem(),
492 action->getVariable(),
493 m_speedScale * m_speedPeak);
496 Cpu::onSpeedChange();
500 bool LinkL07::isUsed(){
501 return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
504 void CpuL07::updateState(tmgr_trace_event_t event_type, double value, double /*date*/){
505 XBT_DEBUG("Updating cpu %s (%p) with value %g", getName(), this, value);
506 if (event_type == p_speedEvent) {
507 m_speedScale = value;
509 if (tmgr_trace_event_free(event_type))
511 } else if (event_type == p_stateEvent) {
516 if (tmgr_trace_event_free(event_type))
519 XBT_CRITICAL("Unknown event ! \n");
525 void LinkL07::updateState(tmgr_trace_event_t event_type, double value, double date) {
526 XBT_DEBUG("Updating link %s (%p) with value=%f for date=%g", getName(), this, value, date);
527 if (event_type == p_bwEvent) {
528 updateBandwidth(value, date);
529 if (tmgr_trace_event_free(event_type))
531 } else if (event_type == p_latEvent) {
532 updateLatency(value, date);
533 if (tmgr_trace_event_free(event_type))
535 } else if (event_type == p_stateEvent) {
540 if (tmgr_trace_event_free(event_type))
543 XBT_CRITICAL("Unknown event ! \n");
549 double LinkL07::getBandwidth()
554 void LinkL07::updateBandwidth(double value, double date)
557 lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(), m_bwCurrent);
560 void LinkL07::updateLatency(double value, double date)
562 lmm_variable_t var = NULL;
564 lmm_element_t elem = NULL;
566 m_latCurrent = value;
567 while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), getConstraint(), &elem))) {
568 action = static_cast<L07Action*>(lmm_variable_id(var));
569 action->updateBound();
577 L07Action::~L07Action(){
578 free(p_communicationAmount);
579 free(p_computationAmount);
582 void L07Action::updateBound()
584 double lat_current = 0.0;
585 double lat_bound = -1.0;
588 int hostNb = p_netcardList->size();
590 for (i = 0; i < hostNb; i++) {
591 for (j = 0; j < hostNb; j++) {
592 xbt_dynar_t route=NULL;
594 if (p_communicationAmount[i * hostNb + j] > 0) {
596 routing_platf->getRouteAndLatency((*p_netcardList)[i], (*p_netcardList)[j],
599 lat_current = MAX(lat_current, lat * p_communicationAmount[i * hostNb + j]);
603 lat_bound = sg_tcp_gamma / (2.0 * lat_current);
604 XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
605 if ((m_latency == 0.0) && (m_suspended == 0)) {
607 lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), lat_bound);
609 lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(),
610 std::min(m_rate, lat_bound));
614 int L07Action::unref()
618 if (action_hook.is_linked())
619 p_stateSet->erase(p_stateSet->iterator_to(*this));
621 lmm_variable_free(getModel()->getMaxminSystem(), getVariable());
628 void L07Action::suspend()
630 XBT_IN("(%p))", this);
631 if (m_suspended != 2) {
633 lmm_update_variable_weight(getModel()->getMaxminSystem(), getVariable(), 0.0);
638 void L07Action::resume()
640 XBT_IN("(%p)", this);
641 if (m_suspended != 2) {
642 lmm_update_variable_weight(getModel()->getMaxminSystem(), getVariable(), 1.0);
648 void L07Action::setMaxDuration(double duration)
649 { /* FIXME: should inherit */
650 XBT_IN("(%p,%g)", this, duration);
651 m_maxDuration = duration;
655 void L07Action::setPriority(double priority)
656 { /* FIXME: should inherit */
657 XBT_IN("(%p,%g)", this, priority);
658 m_priority = priority;
662 double L07Action::getRemains()
664 XBT_IN("(%p)", this);