1 /* Copyright (c) 2007-2010, 2013-2014. 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"
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_host);
14 static int ptask_host_count = 0;
15 static xbt_dict_t ptask_parallel_task_link_set = NULL;
16 lmm_system_t ptask_maxmin_system = NULL;
19 /**************************************/
20 /*** Resource Creation & Destruction **/
21 /**************************************/
23 static void ptask_netlink_parse_init(sg_platf_link_cbarg_t link)
25 netlink_parse_init(link);
26 current_property_set = NULL;
29 static void ptask_define_callbacks()
31 sg_platf_host_add_cb(cpu_parse_init);
32 sg_platf_host_add_cb(host_parse_init);
33 sg_platf_link_add_cb(ptask_netlink_parse_init);
34 sg_platf_postparse_add_cb(host_add_traces);
37 void surf_host_model_init_ptask_L07(void)
39 XBT_INFO("surf_host_model_init_ptask_L07");
40 xbt_assert(!surf_cpu_model_pm, "CPU model type already defined");
41 xbt_assert(!surf_network_model, "network model type already defined");
42 ptask_define_callbacks();
43 surf_host_model = new HostL07Model();
44 ModelPtr model = surf_host_model;
45 xbt_dynar_push(model_list, &model);
46 xbt_dynar_push(model_list_invoke, &model);
50 HostL07Model::HostL07Model() : HostModel("Host ptask_L07") {
51 if (!ptask_maxmin_system)
52 ptask_maxmin_system = lmm_system_new(1);
53 surf_host_model = NULL;
54 surf_network_model = new NetworkL07Model();
55 surf_cpu_model_pm = new CpuL07Model();
57 routing_model_create(surf_network_model->createLink("__loopback__",
60 SURF_RESOURCE_ON, NULL,
61 SURF_LINK_FATPIPE, NULL));
62 p_cpuModel = surf_cpu_model_pm;
65 HostL07Model::~HostL07Model() {
66 xbt_dict_free(&ptask_parallel_task_link_set);
68 delete surf_cpu_model_pm;
69 delete surf_network_model;
72 if (ptask_maxmin_system) {
73 lmm_system_free(ptask_maxmin_system);
74 ptask_maxmin_system = NULL;
78 double HostL07Model::shareResources(double /*now*/)
80 HostL07ActionPtr action;
82 ActionListPtr running_actions = getRunningActionSet();
83 double min = this->shareResourcesMaxMin(running_actions,
87 for(ActionList::iterator it(running_actions->begin()), itend(running_actions->end())
88 ; it != itend ; ++it) {
89 action = static_cast<HostL07ActionPtr>(&*it);
90 if (action->m_latency > 0) {
92 min = action->m_latency;
93 XBT_DEBUG("Updating min (value) with %p (start %f): %f", action,
94 action->getStartTime(), min);
95 } else if (action->m_latency < min) {
96 min = action->m_latency;
97 XBT_DEBUG("Updating min (latency) with %p (start %f): %f", action,
98 action->getStartTime(), min);
103 XBT_DEBUG("min value : %f", min);
108 void HostL07Model::updateActionsState(double /*now*/, double delta)
111 HostL07ActionPtr action;
113 ActionListPtr actionSet = getRunningActionSet();
115 for(ActionList::iterator it(actionSet->begin()), itNext = it, itend(actionSet->end())
116 ; it != itend ; it=itNext) {
118 action = static_cast<HostL07ActionPtr>(&*it);
120 if (action->m_latency > 0) {
121 if (action->m_latency > deltap) {
122 double_update(&(action->m_latency), deltap, sg_surf_precision);
125 double_update(&(deltap), action->m_latency, sg_surf_precision);
126 action->m_latency = 0.0;
128 if ((action->m_latency == 0.0) && (action->isSuspended() == 0)) {
129 action->updateBound();
130 lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 1.0);
133 XBT_DEBUG("Action (%p) : remains (%g) updated by %g.",
134 action, action->getRemains(), lmm_variable_getvalue(action->getVariable()) * delta);
135 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
137 if (action->getMaxDuration() != NO_MAX_DURATION)
138 action->updateMaxDuration(delta);
140 XBT_DEBUG("Action (%p) : remains (%g).",
141 action, action->getRemains());
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;
154 void *constraint_id = NULL;
156 while ((cnst = lmm_get_cnst_from_var(ptask_maxmin_system, action->getVariable(),
158 constraint_id = lmm_constraint_id(cnst);
160 if (static_cast<HostPtr>(constraint_id)->getState() == SURF_RESOURCE_OFF) {
161 XBT_DEBUG("Action (%p) Failed!!", action);
163 action->setState(SURF_ACTION_FAILED);
172 ActionPtr HostL07Model::executeParallelTask(int host_nb,
174 double *flops_amount,
175 double *bytes_amount,
178 HostL07ActionPtr action;
183 double latency = 0.0;
185 if (ptask_parallel_task_link_set == NULL)
186 ptask_parallel_task_link_set = xbt_dict_new_homogeneous(NULL);
188 xbt_dict_reset(ptask_parallel_task_link_set);
190 /* Compute the number of affected resources... */
191 for (i = 0; i < host_nb; i++) {
192 for (j = 0; j < host_nb; j++) {
193 xbt_dynar_t route=NULL;
195 if (bytes_amount[i * host_nb + j] > 0) {
201 routing_platf->getRouteAndLatency(static_cast<HostL07Ptr>(host_list[i])->p_netElm,
202 static_cast<HostL07Ptr>(host_list[j])->p_netElm,
205 latency = MAX(latency, lat);
207 xbt_dynar_foreach(route, cpt, _link) {
208 link = static_cast<LinkL07Ptr>(_link);
209 xbt_dict_set(ptask_parallel_task_link_set, link->getName(), link, NULL);
215 nb_link = xbt_dict_length(ptask_parallel_task_link_set);
216 xbt_dict_reset(ptask_parallel_task_link_set);
218 for (i = 0; i < host_nb; i++)
219 if (flops_amount[i] > 0)
222 action = new HostL07Action(this, 1, 0);
223 XBT_DEBUG("Creating a parallel task (%p) with %d cpus and %d links.",
224 action, host_nb, nb_link);
225 action->m_suspended = 0; /* Should be useless because of the
226 calloc but it seems to help valgrind... */
227 action->m_hostNb = host_nb;
228 action->p_hostList = (HostPtr *) host_list;
229 action->p_computationAmount = flops_amount;
230 action->p_communicationAmount = bytes_amount;
231 action->m_latency = latency;
232 action->m_rate = rate;
234 action->p_variable = lmm_variable_new(ptask_maxmin_system, action, 1.0,
235 (action->m_rate > 0) ? action->m_rate : -1.0,
238 if (action->m_latency > 0)
239 lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 0.0);
241 for (i = 0; i < host_nb; i++)
242 lmm_expand(ptask_maxmin_system,
243 static_cast<HostL07Ptr>(host_list[i])->p_cpu->getConstraint(),
244 action->getVariable(), flops_amount[i]);
246 for (i = 0; i < host_nb; i++) {
247 for (j = 0; j < host_nb; j++) {
251 xbt_dynar_t route=NULL;
252 if (bytes_amount[i * host_nb + j] == 0.0)
255 routing_platf->getRouteAndLatency(static_cast<HostL07Ptr>(host_list[i])->p_netElm,
256 static_cast<HostL07Ptr>(host_list[j])->p_netElm,
259 xbt_dynar_foreach(route, cpt, _link) {
260 link = static_cast<LinkL07Ptr>(_link);
261 lmm_expand_add(ptask_maxmin_system, link->getConstraint(),
262 action->getVariable(),
263 bytes_amount[i * host_nb + j]);
268 if (nb_link + nb_host == 0) {
269 action->setCost(1.0);
270 action->setRemains(0.0);
276 HostPtr HostL07Model::createHost(const char *name)
278 HostL07Ptr wk = NULL;
279 sg_host_t sg_host = sg_host_by_name(name);
281 xbt_assert(!surf_host_resource_priv(sg_host),
282 "Host '%s' declared several times in the platform file.",
285 wk = new HostL07(this, name, NULL,
286 sg_host_edge(sg_host),
287 sg_host_surfcpu(sg_host));
289 xbt_lib_set(host_lib, name, SURF_HOST_LEVEL, wk);
291 return wk;//FIXME:xbt_lib_get_elm_or_null(host_lib, name);
294 ActionPtr HostL07Model::communicate(HostPtr src, HostPtr dst,
295 double size, double rate)
297 void **host_list = xbt_new0(void *, 2);
298 double *flops_amount = xbt_new0(double, 2);
299 double *bytes_amount = xbt_new0(double, 4);
300 ActionPtr res = NULL;
304 bytes_amount[1] = size;
306 res = executeParallelTask(2, host_list,
313 xbt_dynar_t HostL07Model::getRoute(HostPtr src, HostPtr dst)
315 xbt_dynar_t route=NULL;
316 routing_platf->getRouteAndLatency(src->p_netElm, dst->p_netElm, &route, NULL);
320 CpuPtr CpuL07Model::createCpu(const char *name, xbt_dynar_t powerPeak,
321 int pstate, double power_scale,
322 tmgr_trace_t power_trace, int core,
323 e_surf_resource_state_t state_initial,
324 tmgr_trace_t state_trace,
325 xbt_dict_t cpu_properties)
327 double power_initial = xbt_dynar_get_as(powerPeak, pstate, double);
328 xbt_dynar_free(&powerPeak); // kill memory leak
329 sg_host_t sg_host = sg_host_by_name(name);
331 xbt_assert(!surf_host_resource_priv(sg_host),
332 "Host '%s' declared several times in the platform file.",
335 CpuL07Ptr cpu = new CpuL07(this, name, cpu_properties,
336 power_initial, power_scale, power_trace,
337 core, state_initial, state_trace);
339 sg_host_surfcpu_set(sg_host, cpu);
344 Link* NetworkL07Model::createLink(const char *name,
346 tmgr_trace_t bw_trace,
348 tmgr_trace_t lat_trace,
349 e_surf_resource_state_t
351 tmgr_trace_t state_trace,
352 e_surf_link_sharing_policy_t policy,
353 xbt_dict_t properties)
355 xbt_assert(!Link::byName(name),
356 "Link '%s' declared several times in the platform file.",
359 LinkL07Ptr nw_link = new LinkL07(this, name, properties,
360 bw_initial, bw_trace,
361 lat_initial, lat_trace,
362 state_initial, state_trace,
368 void HostL07Model::addTraces()
370 xbt_dict_cursor_t cursor = NULL;
371 char *trace_name, *elm;
373 if (!trace_connect_list_host_avail)
376 /* Connect traces relative to cpu */
377 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
378 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
379 CpuL07Ptr host = static_cast<CpuL07Ptr>(sg_host_surfcpu(sg_host_by_name(elm)));
381 xbt_assert(host, "Host %s undefined", elm);
382 xbt_assert(trace, "Trace %s undefined", trace_name);
384 host->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, host);
387 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
388 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
389 CpuL07Ptr host = static_cast<CpuL07Ptr>(sg_host_surfcpu(sg_host_by_name(elm)));
391 xbt_assert(host, "Host %s undefined", elm);
392 xbt_assert(trace, "Trace %s undefined", trace_name);
394 host->p_powerEvent = tmgr_history_add_trace(history, trace, 0.0, 0, host);
397 /* Connect traces relative to network */
398 xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
399 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
400 LinkL07Ptr link = static_cast<LinkL07Ptr>(Link::byName(elm));
402 xbt_assert(link, "Link %s undefined", elm);
403 xbt_assert(trace, "Trace %s undefined", trace_name);
405 link->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
408 xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
409 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
410 LinkL07Ptr link = static_cast<LinkL07Ptr>(Link::byName(elm));
412 xbt_assert(link, "Link %s undefined", elm);
413 xbt_assert(trace, "Trace %s undefined", trace_name);
415 link->p_bwEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
418 xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
419 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
420 LinkL07Ptr link = static_cast<LinkL07Ptr>(Link::byName(elm));
422 xbt_assert(link, "Link %s undefined", elm);
423 xbt_assert(trace, "Trace %s undefined", trace_name);
425 link->p_latEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
433 HostL07::HostL07(HostModelPtr model, const char* name, xbt_dict_t props, RoutingEdgePtr netElm, CpuPtr cpu)
434 : Host(model, name, props, NULL, netElm, cpu)
438 double HostL07::getPowerPeakAt(int /*pstate_index*/)
443 int HostL07::getNbPstates()
448 void HostL07::setPstate(int /*pstate_index*/)
453 int HostL07::getPstate()
458 double HostL07::getConsumedEnergy()
463 CpuL07::CpuL07(CpuL07ModelPtr model, const char* name, xbt_dict_t props,
464 double power_initial, double power_scale, tmgr_trace_t power_trace,
465 int core, e_surf_resource_state_t state_initial, tmgr_trace_t state_trace)
466 : Cpu(model, name, props, lmm_constraint_new(ptask_maxmin_system, this, power_initial * power_scale),
467 core, power_initial, power_scale)
469 xbt_assert(m_powerScale > 0, "Power has to be >0");
472 p_powerEvent = tmgr_history_add_trace(history, power_trace, 0.0, 0, this);
476 setState(state_initial);
478 p_stateEvent = tmgr_history_add_trace(history, state_trace, 0.0, 0, this);
481 LinkL07::LinkL07(NetworkL07ModelPtr model, const char* name, xbt_dict_t props,
483 tmgr_trace_t bw_trace,
485 tmgr_trace_t lat_trace,
486 e_surf_resource_state_t state_initial,
487 tmgr_trace_t state_trace,
488 e_surf_link_sharing_policy_t policy)
489 : Link(model, name, props, lmm_constraint_new(ptask_maxmin_system, this, bw_initial), history, state_trace)
491 m_bwCurrent = bw_initial;
493 p_bwEvent = tmgr_history_add_trace(history, bw_trace, 0.0, 0, this);
495 setState(state_initial);
496 m_latCurrent = lat_initial;
499 p_latEvent = tmgr_history_add_trace(history, lat_trace, 0.0, 0, this);
501 if (policy == SURF_LINK_FATPIPE)
502 lmm_constraint_shared(getConstraint());
505 bool CpuL07::isUsed(){
506 return lmm_constraint_used(ptask_maxmin_system, getConstraint());
509 bool LinkL07::isUsed(){
510 return lmm_constraint_used(ptask_maxmin_system, getConstraint());
513 void CpuL07::updateState(tmgr_trace_event_t event_type, double value, double /*date*/){
514 XBT_DEBUG("Updating cpu %s (%p) with value %g", getName(), this, value);
515 if (event_type == p_powerEvent) {
516 m_powerScale = value;
517 lmm_update_constraint_bound(ptask_maxmin_system, getConstraint(), m_powerPeak * m_powerScale);
518 if (tmgr_trace_event_free(event_type))
520 } else if (event_type == p_stateEvent) {
522 setState(SURF_RESOURCE_ON);
524 setState(SURF_RESOURCE_OFF);
525 if (tmgr_trace_event_free(event_type))
528 XBT_CRITICAL("Unknown event ! \n");
534 void LinkL07::updateState(tmgr_trace_event_t event_type, double value, double date){
535 XBT_DEBUG("Updating link %s (%p) with value=%f for date=%g", getName(), this, value, date);
536 if (event_type == p_bwEvent) {
537 updateBandwidth(value, date);
538 if (tmgr_trace_event_free(event_type))
540 } else if (event_type == p_latEvent) {
541 updateLatency(value, date);
542 if (tmgr_trace_event_free(event_type))
544 } else if (event_type == p_stateEvent) {
546 setState(SURF_RESOURCE_ON);
548 setState(SURF_RESOURCE_OFF);
549 if (tmgr_trace_event_free(event_type))
552 XBT_CRITICAL("Unknown event ! \n");
558 e_surf_resource_state_t HostL07::getState()
560 return p_cpu->getState();
563 ActionPtr HostL07::execute(double size)
565 void **host_list = xbt_new0(void *, 1);
566 double *flops_amount = xbt_new0(double, 1);
567 double *bytes_amount = xbt_new0(double, 1);
570 bytes_amount[0] = 0.0;
571 flops_amount[0] = size;
573 return static_cast<HostL07ModelPtr>(getModel())->executeParallelTask(1, host_list,
578 ActionPtr HostL07::sleep(double duration)
580 HostL07ActionPtr action = NULL;
582 XBT_IN("(%s,%g)", getName(), duration);
584 action = static_cast<HostL07ActionPtr>(execute(1.0));
585 action->m_maxDuration = duration;
586 action->m_suspended = 2;
587 lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 0.0);
593 double LinkL07::getBandwidth()
598 void LinkL07::updateBandwidth(double value, double date)
601 lmm_update_constraint_bound(ptask_maxmin_system, getConstraint(), m_bwCurrent);
604 double LinkL07::getLatency()
609 void LinkL07::updateLatency(double value, double date)
611 lmm_variable_t var = NULL;
612 HostL07ActionPtr action;
613 lmm_element_t elem = NULL;
615 m_latCurrent = value;
616 while ((var = lmm_get_var_from_cnst(ptask_maxmin_system, getConstraint(), &elem))) {
617 action = (HostL07ActionPtr) lmm_variable_id(var);
618 action->updateBound();
623 bool LinkL07::isShared()
625 return lmm_constraint_is_shared(getConstraint());
632 HostL07Action::~HostL07Action(){
634 free(p_communicationAmount);
635 free(p_computationAmount);
638 void HostL07Action::updateBound()
640 double lat_current = 0.0;
641 double lat_bound = -1.0;
644 for (i = 0; i < m_hostNb; i++) {
645 for (j = 0; j < m_hostNb; j++) {
646 xbt_dynar_t route=NULL;
648 if (p_communicationAmount[i * m_hostNb + j] > 0) {
650 routing_platf->getRouteAndLatency(static_cast<HostL07Ptr>(((void**)p_hostList)[i])->p_netElm,
651 static_cast<HostL07Ptr>(((void**)p_hostList)[j])->p_netElm,
654 lat_current = MAX(lat_current, lat * p_communicationAmount[i * m_hostNb + j]);
658 lat_bound = sg_tcp_gamma / (2.0 * lat_current);
659 XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
660 if ((m_latency == 0.0) && (m_suspended == 0)) {
662 lmm_update_variable_bound(ptask_maxmin_system, getVariable(), lat_bound);
664 lmm_update_variable_bound(ptask_maxmin_system, getVariable(), min(m_rate, lat_bound));
668 int HostL07Action::unref()
672 if (actionHook::is_linked())
673 p_stateSet->erase(p_stateSet->iterator_to(*this));
675 lmm_variable_free(ptask_maxmin_system, getVariable());
682 void HostL07Action::cancel()
684 setState(SURF_ACTION_FAILED);
688 void HostL07Action::suspend()
690 XBT_IN("(%p))", this);
691 if (m_suspended != 2) {
693 lmm_update_variable_weight(ptask_maxmin_system, getVariable(), 0.0);
698 void HostL07Action::resume()
700 XBT_IN("(%p)", this);
701 if (m_suspended != 2) {
702 lmm_update_variable_weight(ptask_maxmin_system, getVariable(), 1.0);
708 bool HostL07Action::isSuspended()
710 return m_suspended == 1;
713 void HostL07Action::setMaxDuration(double duration)
714 { /* FIXME: should inherit */
715 XBT_IN("(%p,%g)", this, duration);
716 m_maxDuration = duration;
720 void HostL07Action::setPriority(double priority)
721 { /* FIXME: should inherit */
722 XBT_IN("(%p,%g)", this, priority);
723 m_priority = priority;
727 double HostL07Action::getRemains()
729 XBT_IN("(%p)", this);