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. */
7 #include "host_ptask_L07.hpp"
9 #include "cpu_interface.hpp"
10 #include "surf_routing.hpp"
13 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_host);
15 static int ptask_host_count = 0;
16 static xbt_dict_t ptask_parallel_task_link_set = NULL;
17 lmm_system_t ptask_maxmin_system = NULL;
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 sg_platf_host_add_cb(cpu_parse_init);
38 sg_platf_host_add_cb(host_parse_init);
39 sg_platf_link_add_cb(ptask_netlink_parse_init);
40 sg_platf_postparse_add_cb(host_add_traces);
42 surf_host_model = new HostL07Model();
43 Model *model = surf_host_model;
44 xbt_dynar_push(model_list, &model);
45 xbt_dynar_push(model_list_invoke, &model);
49 HostL07Model::HostL07Model() : HostModel() {
50 if (!ptask_maxmin_system)
51 ptask_maxmin_system = lmm_system_new(1);
52 surf_host_model = NULL;
53 surf_network_model = new NetworkL07Model();
54 surf_cpu_model_pm = new CpuL07Model();
56 routing_model_create(surf_network_model->createLink("__loopback__",
59 SURF_RESOURCE_ON, NULL,
60 SURF_LINK_FATPIPE, NULL));
63 HostL07Model::~HostL07Model() {
64 xbt_dict_free(&ptask_parallel_task_link_set);
66 delete surf_cpu_model_pm;
67 delete surf_network_model;
70 if (ptask_maxmin_system) {
71 lmm_system_free(ptask_maxmin_system);
72 ptask_maxmin_system = NULL;
76 double HostL07Model::shareResources(double /*now*/)
80 ActionList *running_actions = getRunningActionSet();
81 double min = this->shareResourcesMaxMin(running_actions,
85 for(ActionList::iterator it(running_actions->begin()), itend(running_actions->end())
86 ; it != itend ; ++it) {
87 action = static_cast<L07Action*>(&*it);
88 if (action->m_latency > 0) {
90 min = action->m_latency;
91 XBT_DEBUG("Updating min (value) with %p (start %f): %f", action,
92 action->getStartTime(), min);
93 } else if (action->m_latency < min) {
94 min = action->m_latency;
95 XBT_DEBUG("Updating min (latency) with %p (start %f): %f", action,
96 action->getStartTime(), min);
101 XBT_DEBUG("min value : %f", min);
106 void HostL07Model::updateActionsState(double /*now*/, double delta) {
109 ActionList *actionSet = getRunningActionSet();
111 for(ActionList::iterator it = actionSet->begin(), itNext = it
112 ; it != actionSet->end()
115 action = static_cast<L07Action*>(&*it);
116 if (action->m_latency > 0) {
117 if (action->m_latency > delta) {
118 double_update(&(action->m_latency), delta, sg_surf_precision);
120 action->m_latency = 0.0;
122 if ((action->m_latency == 0.0) && (action->isSuspended() == 0)) {
123 action->updateBound();
124 lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 1.0);
127 XBT_DEBUG("Action (%p) : remains (%g) updated by %g.",
128 action, action->getRemains(), lmm_variable_getvalue(action->getVariable()) * delta);
129 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
131 if (action->getMaxDuration() != NO_MAX_DURATION)
132 action->updateMaxDuration(delta);
134 XBT_DEBUG("Action (%p) : remains (%g).", action, action->getRemains());
136 /* In the next if cascade, the action can be finished either because:
137 * - The amount of remaining work reached 0
138 * - The max duration was reached
139 * If it's not done, it may have failed.
142 if ((action->getRemains() <= 0) &&
143 (lmm_get_variable_weight(action->getVariable()) > 0)) {
145 action->setState(SURF_ACTION_DONE);
146 } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
147 (action->getMaxDuration() <= 0)) {
149 action->setState(SURF_ACTION_DONE);
151 /* Need to check that none of the model has failed */
152 lmm_constraint_t cnst = NULL;
155 while ((cnst = lmm_get_cnst_from_var(ptask_maxmin_system, action->getVariable(), i++))) {
156 void *constraint_id = lmm_constraint_id(cnst);
158 if (static_cast<Host*>(constraint_id)->getState() == SURF_RESOURCE_OFF) {
159 XBT_DEBUG("Action (%p) Failed!!", action);
161 action->setState(SURF_ACTION_FAILED);
170 Action *HostL07Model::executeParallelTask(int host_nb,
172 double *flops_amount,
173 double *bytes_amount,
176 L07Action *action = new L07Action(this, 1, 0);
180 double latency = 0.0;
182 action->p_edgeList->reserve(host_nb);
183 for (int i = 0; i<host_nb; i++)
184 action->p_edgeList->push_back(sg_host_edge(host_list[i]));
186 if (ptask_parallel_task_link_set == NULL)
187 ptask_parallel_task_link_set = xbt_dict_new_homogeneous(NULL);
189 xbt_dict_reset(ptask_parallel_task_link_set);
191 /* Compute the number of affected resources... */
192 for (int i = 0; i < host_nb; i++) {
193 for (int j = 0; j < host_nb; j++) {
194 xbt_dynar_t route=NULL;
196 if (bytes_amount[i * host_nb + j] > 0) {
202 routing_platf->getRouteAndLatency((*action->p_edgeList)[i], (*action->p_edgeList)[j],
204 latency = MAX(latency, lat);
206 xbt_dynar_foreach(route, cpt, _link) {
207 link = static_cast<LinkL07*>(_link);
208 xbt_dict_set(ptask_parallel_task_link_set, link->getName(), link, NULL);
214 nb_link = xbt_dict_length(ptask_parallel_task_link_set);
215 xbt_dict_reset(ptask_parallel_task_link_set);
217 for (int i = 0; i < host_nb; i++)
218 if (flops_amount[i] > 0)
221 XBT_DEBUG("Creating a parallel task (%p) with %d cpus and %d links.",
222 action, host_nb, nb_link);
223 action->m_suspended = 0; /* valgrind seems to want it despite the calloc... */
224 action->p_computationAmount = flops_amount;
225 action->p_communicationAmount = bytes_amount;
226 action->m_latency = latency;
227 action->m_rate = rate;
229 action->p_variable = lmm_variable_new(ptask_maxmin_system, action, 1.0,
230 (rate > 0 ? rate : -1.0),
233 if (action->m_latency > 0)
234 lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 0.0);
236 for (int i = 0; i < host_nb; i++)
237 lmm_expand(ptask_maxmin_system,
238 sg_host_surfcpu(host_list[i])->getConstraint(),
239 action->getVariable(), flops_amount[i]);
241 for (int i = 0; i < host_nb; i++) {
242 for (int j = 0; j < host_nb; j++) {
245 xbt_dynar_t route=NULL;
246 if (bytes_amount[i * host_nb + j] == 0.0)
249 routing_platf->getRouteAndLatency((*action->p_edgeList)[i], (*action->p_edgeList)[j],
252 xbt_dynar_foreach(route, cpt, _link) {
253 LinkL07 *link = static_cast<LinkL07*>(_link);
254 lmm_expand_add(ptask_maxmin_system, link->getConstraint(),
255 action->getVariable(),
256 bytes_amount[i * host_nb + j]);
261 if (nb_link + nb_host == 0) {
262 action->setCost(1.0);
263 action->setRemains(0.0);
269 Host *HostL07Model::createHost(const char *name)
272 sg_host_t sg_host = sg_host_by_name(name);
274 xbt_assert(!surf_host_resource_priv(sg_host),
275 "Host '%s' declared several times in the platform file.",
278 wk = new HostL07(this, name, NULL,
279 sg_host_edge(sg_host),
280 sg_host_surfcpu(sg_host));
282 xbt_lib_set(host_lib, name, SURF_HOST_LEVEL, wk);
287 Action *NetworkL07Model::communicate(RoutingEdge *src, RoutingEdge *dst,
288 double size, double rate)
290 sg_host_t*host_list = xbt_new0(sg_host_t, 2);
291 double *flops_amount = xbt_new0(double, 2);
292 double *bytes_amount = xbt_new0(double, 4);
295 host_list[0] = sg_host_by_name(src->getName());
296 host_list[1] = sg_host_by_name(dst->getName());
297 bytes_amount[1] = size;
299 res = p_hostModel->executeParallelTask(2, host_list,
306 xbt_dynar_t HostL07Model::getRoute(Host *src, Host *dst)
308 xbt_dynar_t route=NULL;
309 routing_platf->getRouteAndLatency(src->p_netElm, dst->p_netElm, &route, NULL);
313 Cpu *CpuL07Model::createCpu(const char *name, xbt_dynar_t powerPeak,
314 int pstate, double power_scale,
315 tmgr_trace_t power_trace, int core,
316 e_surf_resource_state_t state_initial,
317 tmgr_trace_t state_trace,
318 xbt_dict_t cpu_properties)
320 double power_initial = xbt_dynar_get_as(powerPeak, pstate, double);
321 xbt_dynar_free(&powerPeak); // kill memory leak
322 sg_host_t sg_host = sg_host_by_name(name);
324 xbt_assert(!surf_host_resource_priv(sg_host),
325 "Host '%s' declared several times in the platform file.",
328 CpuL07 *cpu = new CpuL07(this, name, cpu_properties,
329 power_initial, power_scale, power_trace,
330 core, state_initial, state_trace);
332 sg_host_surfcpu_set(sg_host, cpu);
337 Link* NetworkL07Model::createLink(const char *name,
339 tmgr_trace_t bw_trace,
341 tmgr_trace_t lat_trace,
342 e_surf_resource_state_t state_initial,
343 tmgr_trace_t state_trace,
344 e_surf_link_sharing_policy_t policy,
345 xbt_dict_t properties)
347 xbt_assert(!Link::byName(name),
348 "Link '%s' declared several times in the platform file.", name);
350 return new LinkL07(this, name, properties,
351 bw_initial, bw_trace,
352 lat_initial, lat_trace,
353 state_initial, state_trace,
357 void HostL07Model::addTraces()
359 xbt_dict_cursor_t cursor = NULL;
360 char *trace_name, *elm;
362 if (!trace_connect_list_host_avail)
365 /* Connect traces relative to cpu */
366 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
367 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
368 CpuL07 *host = static_cast<CpuL07*>(sg_host_surfcpu(sg_host_by_name(elm)));
370 xbt_assert(host, "Host %s undefined", elm);
371 xbt_assert(trace, "Trace %s undefined", trace_name);
373 host->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, host);
376 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
377 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
378 CpuL07 *host = static_cast<CpuL07*>(sg_host_surfcpu(sg_host_by_name(elm)));
380 xbt_assert(host, "Host %s undefined", elm);
381 xbt_assert(trace, "Trace %s undefined", trace_name);
383 host->p_powerEvent = tmgr_history_add_trace(history, trace, 0.0, 0, host);
386 /* Connect traces relative to network */
387 xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
388 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
389 LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
391 xbt_assert(link, "Link %s undefined", elm);
392 xbt_assert(trace, "Trace %s undefined", trace_name);
394 link->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
397 xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
398 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
399 LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
401 xbt_assert(link, "Link %s undefined", elm);
402 xbt_assert(trace, "Trace %s undefined", trace_name);
404 link->p_bwEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
407 xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
408 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
409 LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
411 xbt_assert(link, "Link %s undefined", elm);
412 xbt_assert(trace, "Trace %s undefined", trace_name);
414 link->p_latEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
422 HostL07::HostL07(HostModel *model, const char* name, xbt_dict_t props, RoutingEdge *netElm, Cpu *cpu)
423 : Host(model, name, props, NULL, netElm, cpu)
427 double HostL07::getPowerPeakAt(int /*pstate_index*/)
432 int HostL07::getNbPstates()
437 void HostL07::setPstate(int /*pstate_index*/)
442 int HostL07::getPstate()
447 double HostL07::getConsumedEnergy()
452 CpuL07::CpuL07(CpuL07Model *model, const char* name, xbt_dict_t props,
453 double power_initial, double power_scale, tmgr_trace_t power_trace,
454 int core, e_surf_resource_state_t state_initial, tmgr_trace_t state_trace)
455 : Cpu(model, name, props, lmm_constraint_new(ptask_maxmin_system, this, power_initial * power_scale),
456 core, power_initial, power_scale)
458 xbt_assert(m_powerScale > 0, "Power has to be >0");
461 p_powerEvent = tmgr_history_add_trace(history, power_trace, 0.0, 0, this);
465 setState(state_initial);
467 p_stateEvent = tmgr_history_add_trace(history, state_trace, 0.0, 0, this);
470 LinkL07::LinkL07(NetworkL07Model *model, const char* name, xbt_dict_t props,
472 tmgr_trace_t bw_trace,
474 tmgr_trace_t lat_trace,
475 e_surf_resource_state_t state_initial,
476 tmgr_trace_t state_trace,
477 e_surf_link_sharing_policy_t policy)
478 : Link(model, name, props, lmm_constraint_new(ptask_maxmin_system, this, bw_initial), history, state_trace)
480 m_bwCurrent = bw_initial;
482 p_bwEvent = tmgr_history_add_trace(history, bw_trace, 0.0, 0, this);
484 setState(state_initial);
485 m_latCurrent = lat_initial;
488 p_latEvent = tmgr_history_add_trace(history, lat_trace, 0.0, 0, this);
490 if (policy == SURF_LINK_FATPIPE)
491 lmm_constraint_shared(getConstraint());
494 bool CpuL07::isUsed(){
495 return lmm_constraint_used(ptask_maxmin_system, getConstraint());
498 bool LinkL07::isUsed(){
499 return lmm_constraint_used(ptask_maxmin_system, getConstraint());
502 void CpuL07::updateState(tmgr_trace_event_t event_type, double value, double /*date*/){
503 XBT_DEBUG("Updating cpu %s (%p) with value %g", getName(), this, value);
504 if (event_type == p_powerEvent) {
505 m_powerScale = value;
506 lmm_update_constraint_bound(ptask_maxmin_system, getConstraint(), m_powerPeak * m_powerScale);
507 if (tmgr_trace_event_free(event_type))
509 } else if (event_type == p_stateEvent) {
511 setState(SURF_RESOURCE_ON);
513 setState(SURF_RESOURCE_OFF);
514 if (tmgr_trace_event_free(event_type))
517 XBT_CRITICAL("Unknown event ! \n");
523 void LinkL07::updateState(tmgr_trace_event_t event_type, double value, double date) {
524 XBT_DEBUG("Updating link %s (%p) with value=%f for date=%g", getName(), this, value, date);
525 if (event_type == p_bwEvent) {
526 updateBandwidth(value, date);
527 if (tmgr_trace_event_free(event_type))
529 } else if (event_type == p_latEvent) {
530 updateLatency(value, date);
531 if (tmgr_trace_event_free(event_type))
533 } else if (event_type == p_stateEvent) {
535 setState(SURF_RESOURCE_ON);
537 setState(SURF_RESOURCE_OFF);
538 if (tmgr_trace_event_free(event_type))
541 XBT_CRITICAL("Unknown event ! \n");
547 e_surf_resource_state_t HostL07::getState() {
548 return p_cpu->getState();
551 Action *HostL07::execute(double size)
553 sg_host_t*host_list = xbt_new0(sg_host_t, 1);
554 double *flops_amount = xbt_new0(double, 1);
555 double *bytes_amount = xbt_new0(double, 1);
557 host_list[0] = sg_host_by_name(getName());
558 flops_amount[0] = size;
560 return static_cast<HostL07Model*>(getModel())->executeParallelTask(1, host_list,
565 Action *HostL07::sleep(double duration)
567 L07Action *action = NULL;
569 XBT_IN("(%s,%g)", getName(), duration);
571 action = static_cast<L07Action*>(execute(1.0));
572 action->m_maxDuration = duration;
573 action->m_suspended = 2;
574 lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 0.0);
580 double LinkL07::getBandwidth()
585 void LinkL07::updateBandwidth(double value, double date)
588 lmm_update_constraint_bound(ptask_maxmin_system, getConstraint(), m_bwCurrent);
591 double LinkL07::getLatency()
596 void LinkL07::updateLatency(double value, double date)
598 lmm_variable_t var = NULL;
600 lmm_element_t elem = NULL;
602 m_latCurrent = value;
603 while ((var = lmm_get_var_from_cnst(ptask_maxmin_system, getConstraint(), &elem))) {
604 action = static_cast<L07Action*>(lmm_variable_id(var));
605 action->updateBound();
610 bool LinkL07::isShared()
612 return lmm_constraint_is_shared(getConstraint());
619 L07Action::~L07Action(){
620 free(p_communicationAmount);
621 free(p_computationAmount);
624 void L07Action::updateBound()
626 double lat_current = 0.0;
627 double lat_bound = -1.0;
630 int hostNb = p_edgeList->size();
632 for (i = 0; i < hostNb; i++) {
633 for (j = 0; j < hostNb; j++) {
634 xbt_dynar_t route=NULL;
636 if (p_communicationAmount[i * hostNb + j] > 0) {
638 routing_platf->getRouteAndLatency((*p_edgeList)[i], (*p_edgeList)[j],
641 lat_current = MAX(lat_current, lat * p_communicationAmount[i * hostNb + j]);
645 lat_bound = sg_tcp_gamma / (2.0 * lat_current);
646 XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
647 if ((m_latency == 0.0) && (m_suspended == 0)) {
649 lmm_update_variable_bound(ptask_maxmin_system, getVariable(), lat_bound);
651 lmm_update_variable_bound(ptask_maxmin_system, getVariable(), min(m_rate, lat_bound));
655 int L07Action::unref()
659 if (actionHook::is_linked())
660 p_stateSet->erase(p_stateSet->iterator_to(*this));
662 lmm_variable_free(ptask_maxmin_system, getVariable());
669 void L07Action::cancel()
671 setState(SURF_ACTION_FAILED);
675 void L07Action::suspend()
677 XBT_IN("(%p))", this);
678 if (m_suspended != 2) {
680 lmm_update_variable_weight(ptask_maxmin_system, getVariable(), 0.0);
685 void L07Action::resume()
687 XBT_IN("(%p)", this);
688 if (m_suspended != 2) {
689 lmm_update_variable_weight(ptask_maxmin_system, getVariable(), 1.0);
695 bool L07Action::isSuspended()
697 return m_suspended == 1;
700 void L07Action::setMaxDuration(double duration)
701 { /* FIXME: should inherit */
702 XBT_IN("(%p,%g)", this, duration);
703 m_maxDuration = duration;
707 void L07Action::setPriority(double priority)
708 { /* FIXME: should inherit */
709 XBT_IN("(%p,%g)", this, priority);
710 m_priority = priority;
714 double L07Action::getRemains()
716 XBT_IN("(%p)", this);