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);
39 simgrid::surf::on_postparse.connect([](){
40 surf_host_model->addTraces();
43 surf_host_model = new simgrid::surf::HostL07Model();
44 xbt_dynar_push(all_existing_models, &surf_host_model);
51 HostL07Model::HostL07Model() : HostModel() {
52 p_maxminSystem = lmm_system_new(1);
53 surf_network_model = new NetworkL07Model(this,p_maxminSystem);
54 surf_cpu_model_pm = new CpuL07Model(this,p_maxminSystem);
56 routing_model_create(surf_network_model->createLink("__loopback__",
60 SURF_LINK_FATPIPE, NULL));
63 HostL07Model::~HostL07Model() {
64 delete surf_cpu_model_pm;
65 delete surf_network_model;
68 CpuL07Model::CpuL07Model(HostL07Model *hmodel,lmm_system_t sys)
74 CpuL07Model::~CpuL07Model() {
75 surf_cpu_model_pm = NULL;
76 lmm_system_free(p_maxminSystem);
77 p_maxminSystem = NULL;
79 NetworkL07Model::NetworkL07Model(HostL07Model *hmodel, lmm_system_t sys)
85 NetworkL07Model::~NetworkL07Model()
87 surf_network_model = NULL;
88 p_maxminSystem = NULL; // Avoid multi-free
92 double HostL07Model::shareResources(double /*now*/)
96 ActionList *running_actions = getRunningActionSet();
97 double min = this->shareResourcesMaxMin(running_actions,
101 for(ActionList::iterator it(running_actions->begin()), itend(running_actions->end())
102 ; it != itend ; ++it) {
103 action = static_cast<L07Action*>(&*it);
104 if (action->m_latency > 0) {
106 min = action->m_latency;
107 XBT_DEBUG("Updating min (value) with %p (start %f): %f", action,
108 action->getStartTime(), min);
109 } else if (action->m_latency < min) {
110 min = action->m_latency;
111 XBT_DEBUG("Updating min (latency) with %p (start %f): %f", action,
112 action->getStartTime(), min);
117 XBT_DEBUG("min value : %f", min);
122 void HostL07Model::updateActionsState(double /*now*/, double delta) {
125 ActionList *actionSet = getRunningActionSet();
127 for(ActionList::iterator it = actionSet->begin(), itNext = it
128 ; it != actionSet->end()
131 action = static_cast<L07Action*>(&*it);
132 if (action->m_latency > 0) {
133 if (action->m_latency > delta) {
134 double_update(&(action->m_latency), delta, sg_surf_precision);
136 action->m_latency = 0.0;
138 if ((action->m_latency == 0.0) && (action->isSuspended() == 0)) {
139 action->updateBound();
140 lmm_update_variable_weight(p_maxminSystem, action->getVariable(), 1.0);
143 XBT_DEBUG("Action (%p) : remains (%g) updated by %g.",
144 action, action->getRemains(), lmm_variable_getvalue(action->getVariable()) * delta);
145 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
147 if (action->getMaxDuration() != NO_MAX_DURATION)
148 action->updateMaxDuration(delta);
150 XBT_DEBUG("Action (%p) : remains (%g).", action, action->getRemains());
152 /* In the next if cascade, the action can be finished either because:
153 * - The amount of remaining work reached 0
154 * - The max duration was reached
155 * If it's not done, it may have failed.
158 if ((action->getRemains() <= 0) &&
159 (lmm_get_variable_weight(action->getVariable()) > 0)) {
161 action->setState(SURF_ACTION_DONE);
162 } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
163 (action->getMaxDuration() <= 0)) {
165 action->setState(SURF_ACTION_DONE);
167 /* Need to check that none of the model has failed */
168 lmm_constraint_t cnst = NULL;
171 while ((cnst = lmm_get_cnst_from_var(p_maxminSystem, action->getVariable(), i++))) {
172 void *constraint_id = lmm_constraint_id(cnst);
174 if (static_cast<Host*>(constraint_id)->isOff()) {
175 XBT_DEBUG("Action (%p) Failed!!", action);
177 action->setState(SURF_ACTION_FAILED);
186 Action *HostL07Model::executeParallelTask(int host_nb, sg_host_t *host_list,
187 double *flops_amount, double *bytes_amount,
189 return new L07Action(this, host_nb, host_list, flops_amount, bytes_amount, rate);
193 L07Action::L07Action(Model *model, int host_nb,
195 double *flops_amount,
196 double *bytes_amount,
198 : CpuAction(model, 1, 0)
202 int nb_used_host = 0; /* Only the hosts with something to compute (>0 flops) are counted) */
203 double latency = 0.0;
205 xbt_dict_t ptask_parallel_task_link_set = xbt_dict_new_homogeneous(NULL);
207 this->p_netcardList->reserve(host_nb);
208 for (int i = 0; i<host_nb; i++)
209 this->p_netcardList->push_back(host_list[i]->pimpl_netcard);
211 /* Compute the number of affected resources... */
212 if(bytes_amount != NULL) {
213 for (int i = 0; i < host_nb; i++) {
214 for (int j = 0; j < host_nb; j++) {
215 xbt_dynar_t route=NULL;
217 if (bytes_amount[i * host_nb + j] > 0) {
223 routing_platf->getRouteAndLatency((*this->p_netcardList)[i], (*this->p_netcardList)[j],
225 latency = MAX(latency, lat);
227 xbt_dynar_foreach(route, cpt, _link) {
228 link = static_cast<LinkL07*>(_link);
229 xbt_dict_set(ptask_parallel_task_link_set, link->getName(), link, NULL);
236 nb_link = xbt_dict_length(ptask_parallel_task_link_set);
237 xbt_dict_free(&ptask_parallel_task_link_set);
239 for (int i = 0; i < host_nb; i++)
240 if (flops_amount[i] > 0)
243 XBT_DEBUG("Creating a parallel task (%p) with %d cpus and %d links.",
244 this, host_nb, nb_link);
245 this->p_computationAmount = flops_amount;
246 this->p_communicationAmount = bytes_amount;
247 this->m_latency = latency;
250 this->p_variable = lmm_variable_new(model->getMaxminSystem(), this, 1.0,
251 (rate > 0 ? rate : -1.0),
254 if (this->m_latency > 0)
255 lmm_update_variable_weight(model->getMaxminSystem(), this->getVariable(), 0.0);
257 for (int i = 0; i < host_nb; i++)
258 lmm_expand(model->getMaxminSystem(),
259 host_list[i]->pimpl_cpu->getConstraint(),
260 this->getVariable(), flops_amount[i]);
262 if(bytes_amount != NULL) {
263 for (int i = 0; i < host_nb; i++) {
264 for (int j = 0; j < host_nb; j++) {
267 xbt_dynar_t route=NULL;
268 if (bytes_amount[i * host_nb + j] == 0.0)
271 routing_platf->getRouteAndLatency((*this->p_netcardList)[i], (*this->p_netcardList)[j],
274 xbt_dynar_foreach(route, cpt, _link) {
275 LinkL07 *link = static_cast<LinkL07*>(_link);
276 lmm_expand_add(model->getMaxminSystem(), link->getConstraint(),
277 this->getVariable(), bytes_amount[i * host_nb + j]);
283 if (nb_link + nb_used_host == 0) {
285 this->setRemains(0.0);
289 Action *NetworkL07Model::communicate(NetCard *src, NetCard *dst,
290 double size, double rate)
292 sg_host_t*host_list = xbt_new0(sg_host_t, 2);
293 double *flops_amount = xbt_new0(double, 2);
294 double *bytes_amount = xbt_new0(double, 4);
297 host_list[0] = sg_host_by_name(src->getName());
298 host_list[1] = sg_host_by_name(dst->getName());
299 bytes_amount[1] = size;
301 res = p_hostModel->executeParallelTask(2, host_list,
308 Cpu *CpuL07Model::createCpu(simgrid::s4u::Host *host, xbt_dynar_t powerPeakList,
309 int pstate, double power_scale,
310 tmgr_trace_t power_trace, int core,
312 tmgr_trace_t state_trace)
314 CpuL07 *cpu = new CpuL07(this, host, powerPeakList, pstate, power_scale, power_trace,
315 core, initiallyOn, state_trace);
319 Link* NetworkL07Model::createLink(const char *name,
321 tmgr_trace_t bw_trace,
323 tmgr_trace_t lat_trace,
325 tmgr_trace_t state_trace,
326 e_surf_link_sharing_policy_t policy,
327 xbt_dict_t properties)
329 xbt_assert(!Link::byName(name),
330 "Link '%s' declared several times in the platform file.", name);
332 Link* link = new LinkL07(this, name, properties,
333 bw_initial, bw_trace,
334 lat_initial, lat_trace,
335 initiallyOn, state_trace,
337 Link::onCreation(link);
341 void HostL07Model::addTraces()
343 xbt_dict_cursor_t cursor = NULL;
344 char *trace_name, *elm;
346 if (!trace_connect_list_host_avail)
349 /* Connect traces relative to cpu */
350 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
351 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
352 CpuL07 *host = static_cast<CpuL07*>(sg_host_by_name(elm)->pimpl_cpu);
354 xbt_assert(host, "Host %s undefined", elm);
355 xbt_assert(trace, "Trace %s undefined", trace_name);
357 host->p_stateEvent = future_evt_set->add_trace(trace, 0.0, host);
360 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
361 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
362 CpuL07 *host = static_cast<CpuL07*>(sg_host_by_name(elm)->pimpl_cpu);
364 xbt_assert(host, "Host %s undefined", elm);
365 xbt_assert(trace, "Trace %s undefined", trace_name);
367 host->p_speedEvent = future_evt_set->add_trace(trace, 0.0, host);
370 /* Connect traces relative to network */
371 xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
372 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
373 LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
375 xbt_assert(link, "Link %s undefined", elm);
376 xbt_assert(trace, "Trace %s undefined", trace_name);
378 link->p_stateEvent = future_evt_set->add_trace(trace, 0.0, link);
381 xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
382 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
383 LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
385 xbt_assert(link, "Link %s undefined", elm);
386 xbt_assert(trace, "Trace %s undefined", trace_name);
388 link->p_bwEvent = future_evt_set->add_trace(trace, 0.0, link);
391 xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
392 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
393 LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
395 xbt_assert(link, "Link %s undefined", elm);
396 xbt_assert(trace, "Trace %s undefined", trace_name);
398 link->p_latEvent = future_evt_set->add_trace(trace, 0.0, link);
406 CpuL07::CpuL07(CpuL07Model *model, simgrid::s4u::Host *host,
407 xbt_dynar_t speedPeakList, int pstate,
408 double speedScale, tmgr_trace_t speedTrace,
409 int core, int initiallyOn, tmgr_trace_t state_trace)
410 : Cpu(model, host, speedPeakList, pstate,
411 core, xbt_dynar_get_as(speedPeakList,pstate,double), speedScale, initiallyOn)
413 p_constraint = lmm_constraint_new(model->getMaxminSystem(), this, xbt_dynar_get_as(speedPeakList,pstate,double) * speedScale);
416 p_speedEvent = future_evt_set->add_trace(speedTrace, 0.0, this);
419 p_stateEvent = future_evt_set->add_trace(state_trace, 0.0, this);
426 LinkL07::LinkL07(NetworkL07Model *model, const char* name, xbt_dict_t props,
428 tmgr_trace_t bw_trace,
430 tmgr_trace_t lat_trace,
432 tmgr_trace_t state_trace,
433 e_surf_link_sharing_policy_t policy)
434 : Link(model, name, props, lmm_constraint_new(model->getMaxminSystem(), this, bw_initial), future_evt_set, state_trace)
436 m_bwCurrent = bw_initial;
438 p_bwEvent = future_evt_set->add_trace(bw_trace, 0.0, this);
444 m_latCurrent = lat_initial;
447 p_latEvent = future_evt_set->add_trace(lat_trace, 0.0, this);
449 if (policy == SURF_LINK_FATPIPE)
450 lmm_constraint_shared(getConstraint());
453 Action *CpuL07::execution_start(double size)
455 sg_host_t*host_list = xbt_new0(sg_host_t, 1);
456 double *flops_amount = xbt_new0(double, 1);
457 double *bytes_amount = xbt_new0(double, 1);
459 host_list[0] = getHost();
460 flops_amount[0] = size;
462 return static_cast<CpuL07Model*>(getModel())->p_hostModel
463 ->executeParallelTask( 1, host_list, flops_amount, bytes_amount, -1);
466 Action *CpuL07::sleep(double duration)
468 L07Action *action = static_cast<L07Action*>(execution_start(1.0));
469 action->m_maxDuration = duration;
470 action->m_suspended = 2;
471 lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), 0.0);
476 bool CpuL07::isUsed(){
477 return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
480 /** @brief take into account changes of speed (either load or max) */
481 void CpuL07::onSpeedChange() {
482 lmm_variable_t var = NULL;
483 lmm_element_t elem = NULL;
485 lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(), m_speedPeak * m_speedScale);
486 while ((var = lmm_get_var_from_cnst
487 (getModel()->getMaxminSystem(), getConstraint(), &elem))) {
488 Action *action = static_cast<Action*>(lmm_variable_id(var));
490 lmm_update_variable_bound(getModel()->getMaxminSystem(),
491 action->getVariable(),
492 m_speedScale * m_speedPeak);
495 Cpu::onSpeedChange();
499 bool LinkL07::isUsed(){
500 return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
503 void CpuL07::updateState(tmgr_trace_iterator_t triggered, double value, double /*date*/){
504 XBT_DEBUG("Updating cpu %s (%p) with value %g", getName(), this, value);
505 if (triggered == p_speedEvent) {
506 m_speedScale = value;
508 tmgr_trace_event_unref(&p_speedEvent);
509 } else if (triggered == p_stateEvent) {
514 tmgr_trace_event_unref(&p_stateEvent);
516 xbt_die("Unknown event!\n");
520 void LinkL07::updateState(tmgr_trace_iterator_t triggered, double value, double date) {
521 XBT_DEBUG("Updating link %s (%p) with value=%f for date=%g", getName(), this, value, date);
522 if (triggered == p_bwEvent) {
523 updateBandwidth(value, date);
524 tmgr_trace_event_unref(&p_bwEvent);
525 } else if (triggered == p_latEvent) {
526 updateLatency(value, date);
527 tmgr_trace_event_unref(&p_latEvent);
528 } else if (triggered == p_stateEvent) {
533 tmgr_trace_event_unref(&p_stateEvent);
535 xbt_die("Unknown event ! \n");
539 double LinkL07::getBandwidth()
544 void LinkL07::updateBandwidth(double value, double date)
547 lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(), m_bwCurrent);
550 void LinkL07::updateLatency(double value, double date)
552 lmm_variable_t var = NULL;
554 lmm_element_t elem = NULL;
556 m_latCurrent = value;
557 while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), getConstraint(), &elem))) {
558 action = static_cast<L07Action*>(lmm_variable_id(var));
559 action->updateBound();
567 L07Action::~L07Action(){
568 free(p_communicationAmount);
569 free(p_computationAmount);
572 void L07Action::updateBound()
574 double lat_current = 0.0;
575 double lat_bound = -1.0;
578 int hostNb = p_netcardList->size();
580 if (p_communicationAmount != NULL) {
581 for (i = 0; i < hostNb; i++) {
582 for (j = 0; j < hostNb; j++) {
583 xbt_dynar_t route=NULL;
585 if (p_communicationAmount[i * hostNb + j] > 0) {
587 routing_platf->getRouteAndLatency((*p_netcardList)[i], (*p_netcardList)[j],
590 lat_current = MAX(lat_current, lat * p_communicationAmount[i * hostNb + j]);
595 lat_bound = sg_tcp_gamma / (2.0 * lat_current);
596 XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
597 if ((m_latency == 0.0) && (m_suspended == 0)) {
599 lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), lat_bound);
601 lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(),
602 std::min(m_rate, lat_bound));
606 int L07Action::unref()
610 if (action_hook.is_linked())
611 p_stateSet->erase(p_stateSet->iterator_to(*this));
613 lmm_variable_free(getModel()->getMaxminSystem(), getVariable());