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([](){
39 surf_host_model->addTraces();
42 surf_host_model = new simgrid::surf::HostL07Model();
43 xbt_dynar_push(all_existing_models, &surf_host_model);
50 HostL07Model::HostL07Model() : HostModel() {
51 p_maxminSystem = lmm_system_new(1);
52 surf_network_model = new NetworkL07Model(this,p_maxminSystem);
53 surf_cpu_model_pm = new CpuL07Model(this,p_maxminSystem);
55 routing_model_create(surf_network_model->createLink("__loopback__",
59 SURF_LINK_FATPIPE, NULL));
62 HostL07Model::~HostL07Model() {
63 delete surf_cpu_model_pm;
64 delete surf_network_model;
67 CpuL07Model::CpuL07Model(HostL07Model *hmodel,lmm_system_t sys)
73 CpuL07Model::~CpuL07Model() {
74 surf_cpu_model_pm = NULL;
75 p_maxminSystem = NULL; // Avoid multi-free
77 NetworkL07Model::NetworkL07Model(HostL07Model *hmodel, lmm_system_t sys)
83 NetworkL07Model::~NetworkL07Model()
85 surf_network_model = NULL;
86 p_maxminSystem = NULL; // Avoid multi-free
90 double HostL07Model::shareResources(double /*now*/)
94 ActionList *running_actions = getRunningActionSet();
95 double min = this->shareResourcesMaxMin(running_actions,
99 for(ActionList::iterator it(running_actions->begin()), itend(running_actions->end())
100 ; it != itend ; ++it) {
101 action = static_cast<L07Action*>(&*it);
102 if (action->m_latency > 0) {
104 min = action->m_latency;
105 XBT_DEBUG("Updating min (value) with %p (start %f): %f", action,
106 action->getStartTime(), min);
107 } else if (action->m_latency < min) {
108 min = action->m_latency;
109 XBT_DEBUG("Updating min (latency) with %p (start %f): %f", action,
110 action->getStartTime(), min);
115 XBT_DEBUG("min value : %f", min);
120 void HostL07Model::updateActionsState(double /*now*/, double delta) {
123 ActionList *actionSet = getRunningActionSet();
125 for(ActionList::iterator it = actionSet->begin(), itNext = it
126 ; it != actionSet->end()
129 action = static_cast<L07Action*>(&*it);
130 if (action->m_latency > 0) {
131 if (action->m_latency > delta) {
132 double_update(&(action->m_latency), delta, sg_surf_precision);
134 action->m_latency = 0.0;
136 if ((action->m_latency == 0.0) && (action->isSuspended() == 0)) {
137 action->updateBound();
138 lmm_update_variable_weight(p_maxminSystem, action->getVariable(), 1.0);
141 XBT_DEBUG("Action (%p) : remains (%g) updated by %g.",
142 action, action->getRemains(), lmm_variable_getvalue(action->getVariable()) * delta);
143 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
145 if (action->getMaxDuration() != NO_MAX_DURATION)
146 action->updateMaxDuration(delta);
148 XBT_DEBUG("Action (%p) : remains (%g).", action, action->getRemains());
150 /* In the next if cascade, the action can be finished either because:
151 * - The amount of remaining work reached 0
152 * - The max duration was reached
153 * If it's not done, it may have failed.
156 if ((action->getRemains() <= 0) &&
157 (lmm_get_variable_weight(action->getVariable()) > 0)) {
159 action->setState(SURF_ACTION_DONE);
160 } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
161 (action->getMaxDuration() <= 0)) {
163 action->setState(SURF_ACTION_DONE);
165 /* Need to check that none of the model has failed */
166 lmm_constraint_t cnst = NULL;
169 while ((cnst = lmm_get_cnst_from_var(p_maxminSystem, action->getVariable(), i++))) {
170 void *constraint_id = lmm_constraint_id(cnst);
172 if (static_cast<Host*>(constraint_id)->isOff()) {
173 XBT_DEBUG("Action (%p) Failed!!", action);
175 action->setState(SURF_ACTION_FAILED);
184 Action *HostL07Model::executeParallelTask(int host_nb, sg_host_t *host_list,
185 double *flops_amount, double *bytes_amount,
187 return new L07Action(this, host_nb, host_list, flops_amount, bytes_amount, rate);
191 L07Action::L07Action(Model *model, int host_nb,
193 double *flops_amount,
194 double *bytes_amount,
196 : CpuAction(model, 1, 0)
200 int nb_used_host = 0; /* Only the hosts with something to compute (>0 flops) are counted) */
201 double latency = 0.0;
203 xbt_dict_t ptask_parallel_task_link_set = xbt_dict_new_homogeneous(NULL);
205 this->p_netcardList->reserve(host_nb);
206 for (int i = 0; i<host_nb; i++)
207 this->p_netcardList->push_back(host_list[i]->pimpl_netcard);
209 /* Compute the number of affected resources... */
210 for (int i = 0; i < host_nb; i++) {
211 for (int j = 0; j < host_nb; j++) {
212 xbt_dynar_t route=NULL;
214 if (bytes_amount[i * host_nb + j] > 0) {
220 routing_platf->getRouteAndLatency((*this->p_netcardList)[i], (*this->p_netcardList)[j],
222 latency = MAX(latency, lat);
224 xbt_dynar_foreach(route, cpt, _link) {
225 link = static_cast<LinkL07*>(_link);
226 xbt_dict_set(ptask_parallel_task_link_set, link->getName(), link, NULL);
232 nb_link = xbt_dict_length(ptask_parallel_task_link_set);
233 xbt_dict_free(&ptask_parallel_task_link_set);
235 for (int i = 0; i < host_nb; i++)
236 if (flops_amount[i] > 0)
239 XBT_DEBUG("Creating a parallel task (%p) with %d cpus and %d links.",
240 this, host_nb, nb_link);
241 this->p_computationAmount = flops_amount;
242 this->p_communicationAmount = bytes_amount;
243 this->m_latency = latency;
246 this->p_variable = lmm_variable_new(model->getMaxminSystem(), this, 1.0,
247 (rate > 0 ? rate : -1.0),
250 if (this->m_latency > 0)
251 lmm_update_variable_weight(model->getMaxminSystem(), this->getVariable(), 0.0);
253 for (int i = 0; i < host_nb; i++)
254 lmm_expand(model->getMaxminSystem(),
255 host_list[i]->pimpl_cpu->getConstraint(),
256 this->getVariable(), flops_amount[i]);
258 for (int i = 0; i < host_nb; i++) {
259 for (int j = 0; j < host_nb; j++) {
262 xbt_dynar_t route=NULL;
263 if (bytes_amount[i * host_nb + j] == 0.0)
266 routing_platf->getRouteAndLatency((*this->p_netcardList)[i], (*this->p_netcardList)[j],
269 xbt_dynar_foreach(route, cpt, _link) {
270 LinkL07 *link = static_cast<LinkL07*>(_link);
271 lmm_expand_add(model->getMaxminSystem(), link->getConstraint(),
273 bytes_amount[i * host_nb + j]);
278 if (nb_link + nb_used_host == 0) {
280 this->setRemains(0.0);
284 Action *NetworkL07Model::communicate(NetCard *src, NetCard *dst,
285 double size, double rate)
287 sg_host_t*host_list = xbt_new0(sg_host_t, 2);
288 double *flops_amount = xbt_new0(double, 2);
289 double *bytes_amount = xbt_new0(double, 4);
292 host_list[0] = sg_host_by_name(src->getName());
293 host_list[1] = sg_host_by_name(dst->getName());
294 bytes_amount[1] = size;
296 res = p_hostModel->executeParallelTask(2, host_list,
303 Cpu *CpuL07Model::createCpu(simgrid::s4u::Host *host, xbt_dynar_t powerPeakList,
304 int pstate, double power_scale,
305 tmgr_trace_t power_trace, int core,
307 tmgr_trace_t state_trace)
309 CpuL07 *cpu = new CpuL07(this, host, powerPeakList, pstate, power_scale, power_trace,
310 core, initiallyOn, state_trace);
314 Link* NetworkL07Model::createLink(const char *name,
316 tmgr_trace_t bw_trace,
318 tmgr_trace_t lat_trace,
320 tmgr_trace_t state_trace,
321 e_surf_link_sharing_policy_t policy,
322 xbt_dict_t properties)
324 xbt_assert(!Link::byName(name),
325 "Link '%s' declared several times in the platform file.", name);
327 Link* link = new LinkL07(this, name, properties,
328 bw_initial, bw_trace,
329 lat_initial, lat_trace,
330 initiallyOn, state_trace,
332 Link::onCreation(link);
336 void HostL07Model::addTraces()
338 xbt_dict_cursor_t cursor = NULL;
339 char *trace_name, *elm;
341 if (!trace_connect_list_host_avail)
344 /* Connect traces relative to cpu */
345 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
346 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
347 CpuL07 *host = static_cast<CpuL07*>(sg_host_by_name(elm)->pimpl_cpu);
349 xbt_assert(host, "Host %s undefined", elm);
350 xbt_assert(trace, "Trace %s undefined", trace_name);
352 host->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, host);
355 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
356 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
357 CpuL07 *host = static_cast<CpuL07*>(sg_host_by_name(elm)->pimpl_cpu);
359 xbt_assert(host, "Host %s undefined", elm);
360 xbt_assert(trace, "Trace %s undefined", trace_name);
362 host->p_speedEvent = tmgr_history_add_trace(history, trace, 0.0, 0, host);
365 /* Connect traces relative to network */
366 xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
367 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
368 LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
370 xbt_assert(link, "Link %s undefined", elm);
371 xbt_assert(trace, "Trace %s undefined", trace_name);
373 link->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
376 xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
377 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
378 LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
380 xbt_assert(link, "Link %s undefined", elm);
381 xbt_assert(trace, "Trace %s undefined", trace_name);
383 link->p_bwEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
386 xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
387 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
388 LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
390 xbt_assert(link, "Link %s undefined", elm);
391 xbt_assert(trace, "Trace %s undefined", trace_name);
393 link->p_latEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
401 CpuL07::CpuL07(CpuL07Model *model, simgrid::s4u::Host *host,
402 xbt_dynar_t speedPeakList, int pstate,
403 double speedScale, tmgr_trace_t speedTrace,
404 int core, int initiallyOn, tmgr_trace_t state_trace)
405 : Cpu(model, host, speedPeakList, pstate,
406 core, xbt_dynar_get_as(speedPeakList,pstate,double), speedScale, initiallyOn)
408 p_constraint = lmm_constraint_new(model->getMaxminSystem(), this, xbt_dynar_get_as(speedPeakList,pstate,double) * speedScale);
411 p_speedEvent = tmgr_history_add_trace(history, speedTrace, 0.0, 0, this);
416 p_stateEvent = tmgr_history_add_trace(history, state_trace, 0.0, 0, this);
423 LinkL07::LinkL07(NetworkL07Model *model, const char* name, xbt_dict_t props,
425 tmgr_trace_t bw_trace,
427 tmgr_trace_t lat_trace,
429 tmgr_trace_t state_trace,
430 e_surf_link_sharing_policy_t policy)
431 : Link(model, name, props, lmm_constraint_new(model->getMaxminSystem(), this, bw_initial), history, state_trace)
433 m_bwCurrent = bw_initial;
435 p_bwEvent = tmgr_history_add_trace(history, bw_trace, 0.0, 0, this);
441 m_latCurrent = lat_initial;
444 p_latEvent = tmgr_history_add_trace(history, lat_trace, 0.0, 0, this);
446 if (policy == SURF_LINK_FATPIPE)
447 lmm_constraint_shared(getConstraint());
450 Action *CpuL07::execute(double size)
452 sg_host_t*host_list = xbt_new0(sg_host_t, 1);
453 double *flops_amount = xbt_new0(double, 1);
454 double *bytes_amount = xbt_new0(double, 1);
456 host_list[0] = getHost();
457 flops_amount[0] = size;
459 return static_cast<CpuL07Model*>(getModel())
461 ->executeParallelTask( 1, host_list, flops_amount, bytes_amount, -1);
464 Action *CpuL07::sleep(double duration)
466 L07Action *action = NULL;
468 XBT_IN("(%s,%g)", getName(), duration);
470 action = static_cast<L07Action*>(execute(1.0));
471 action->m_maxDuration = duration;
472 action->m_suspended = 2;
473 lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), 0.0);
479 bool CpuL07::isUsed(){
480 return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
483 /** @brief take into account changes of speed (either load or max) */
484 void CpuL07::onSpeedChange() {
485 lmm_variable_t var = NULL;
486 lmm_element_t elem = NULL;
488 lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(), m_speedPeak * m_speedScale);
489 while ((var = lmm_get_var_from_cnst
490 (getModel()->getMaxminSystem(), getConstraint(), &elem))) {
491 Action *action = static_cast<Action*>(lmm_variable_id(var));
493 lmm_update_variable_bound(getModel()->getMaxminSystem(),
494 action->getVariable(),
495 m_speedScale * m_speedPeak);
498 Cpu::onSpeedChange();
502 bool LinkL07::isUsed(){
503 return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
506 void CpuL07::updateState(tmgr_trace_event_t event_type, double value, double /*date*/){
507 XBT_DEBUG("Updating cpu %s (%p) with value %g", getName(), this, value);
508 if (event_type == p_speedEvent) {
509 m_speedScale = value;
511 if (tmgr_trace_event_free(event_type))
513 } else if (event_type == p_stateEvent) {
518 if (tmgr_trace_event_free(event_type))
521 XBT_CRITICAL("Unknown event ! \n");
527 void LinkL07::updateState(tmgr_trace_event_t event_type, double value, double date) {
528 XBT_DEBUG("Updating link %s (%p) with value=%f for date=%g", getName(), this, value, date);
529 if (event_type == p_bwEvent) {
530 updateBandwidth(value, date);
531 if (tmgr_trace_event_free(event_type))
533 } else if (event_type == p_latEvent) {
534 updateLatency(value, date);
535 if (tmgr_trace_event_free(event_type))
537 } else if (event_type == p_stateEvent) {
542 if (tmgr_trace_event_free(event_type))
545 XBT_CRITICAL("Unknown event ! \n");
551 double LinkL07::getBandwidth()
556 void LinkL07::updateBandwidth(double value, double date)
559 lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(), m_bwCurrent);
562 void LinkL07::updateLatency(double value, double date)
564 lmm_variable_t var = NULL;
566 lmm_element_t elem = NULL;
568 m_latCurrent = value;
569 while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), getConstraint(), &elem))) {
570 action = static_cast<L07Action*>(lmm_variable_id(var));
571 action->updateBound();
579 L07Action::~L07Action(){
580 free(p_communicationAmount);
581 free(p_computationAmount);
584 void L07Action::updateBound()
586 double lat_current = 0.0;
587 double lat_bound = -1.0;
590 int hostNb = p_netcardList->size();
592 for (i = 0; i < hostNb; i++) {
593 for (j = 0; j < hostNb; j++) {
594 xbt_dynar_t route=NULL;
596 if (p_communicationAmount[i * hostNb + j] > 0) {
598 routing_platf->getRouteAndLatency((*p_netcardList)[i], (*p_netcardList)[j],
601 lat_current = MAX(lat_current, lat * p_communicationAmount[i * hostNb + j]);
605 lat_bound = sg_tcp_gamma / (2.0 * lat_current);
606 XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
607 if ((m_latency == 0.0) && (m_suspended == 0)) {
609 lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), lat_bound);
611 lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(),
612 std::min(m_rate, lat_bound));
616 int L07Action::unref()
620 if (action_hook.is_linked())
621 p_stateSet->erase(p_stateSet->iterator_to(*this));
623 lmm_variable_free(getModel()->getMaxminSystem(), getVariable());
630 void L07Action::suspend()
632 XBT_IN("(%p))", this);
633 if (m_suspended != 2) {
635 lmm_update_variable_weight(getModel()->getMaxminSystem(), getVariable(), 0.0);
640 void L07Action::resume()
642 XBT_IN("(%p)", this);
643 if (m_suspended != 2) {
644 lmm_update_variable_weight(getModel()->getMaxminSystem(), getVariable(), 1.0);
650 void L07Action::setMaxDuration(double duration)
651 { /* FIXME: should inherit */
652 XBT_IN("(%p,%g)", this, duration);
653 m_maxDuration = duration;
657 void L07Action::setPriority(double priority)
658 { /* FIXME: should inherit */
659 XBT_IN("(%p,%g)", this, priority);
660 m_priority = priority;
664 double L07Action::getRemains()
666 XBT_IN("(%p)", this);