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 "workstation_ptask_L07.hpp"
8 #include "cpu_interface.hpp"
9 #include "surf_routing.hpp"
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_workstation);
13 static int ptask_host_count = 0;
14 static xbt_dict_t ptask_parallel_task_link_set = NULL;
15 lmm_system_t ptask_maxmin_system = NULL;
18 /**************************************/
19 /*** Resource Creation & Destruction **/
20 /**************************************/
22 static void ptask_netlink_parse_init(sg_platf_link_cbarg_t link)
24 netlink_parse_init(link);
25 current_property_set = NULL;
28 static void ptask_define_callbacks()
30 sg_platf_host_add_cb(cpu_parse_init);
31 sg_platf_host_add_cb(workstation_parse_init);
32 sg_platf_link_add_cb(ptask_netlink_parse_init);
33 sg_platf_postparse_add_cb(workstation_add_traces);
36 void surf_workstation_model_init_ptask_L07(void)
38 XBT_INFO("surf_workstation_model_init_ptask_L07");
39 xbt_assert(!surf_cpu_model_pm, "CPU model type already defined");
40 xbt_assert(!surf_network_model, "network model type already defined");
41 ptask_define_callbacks();
42 surf_workstation_model = new WorkstationL07Model();
43 ModelPtr model = surf_workstation_model;
44 xbt_dynar_push(model_list, &model);
45 xbt_dynar_push(model_list_invoke, &model);
49 WorkstationL07Model::WorkstationL07Model() : WorkstationModel("Workstation ptask_L07") {
50 if (!ptask_maxmin_system)
51 ptask_maxmin_system = lmm_system_new(1);
52 surf_workstation_model = NULL;
53 surf_network_model = new NetworkL07Model();
54 surf_cpu_model_pm = new CpuL07Model();
55 routing_model_create(surf_network_model->createNetworkLink("__loopback__",
58 SURF_RESOURCE_ON, NULL,
59 SURF_LINK_FATPIPE, NULL));
60 p_cpuModel = surf_cpu_model_pm;
63 WorkstationL07Model::~WorkstationL07Model() {
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 WorkstationL07Model::shareResources(double /*now*/)
78 WorkstationL07ActionPtr action;
80 ActionListPtr 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<WorkstationL07ActionPtr>(&*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 WorkstationL07Model::updateActionsState(double /*now*/, double delta)
109 WorkstationL07ActionPtr action;
111 ActionListPtr actionSet = getRunningActionSet();
113 for(ActionList::iterator it(actionSet->begin()), itNext = it, itend(actionSet->end())
114 ; it != itend ; it=itNext) {
116 action = static_cast<WorkstationL07ActionPtr>(&*it);
118 if (action->m_latency > 0) {
119 if (action->m_latency > deltap) {
120 double_update(&(action->m_latency), deltap, sg_surf_precision);
123 double_update(&(deltap), action->m_latency, sg_surf_precision);
124 action->m_latency = 0.0;
126 if ((action->m_latency == 0.0) && (action->isSuspended() == 0)) {
127 action->updateBound();
128 lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 1.0);
131 XBT_DEBUG("Action (%p) : remains (%g) updated by %g.",
132 action, action->getRemains(), lmm_variable_getvalue(action->getVariable()) * delta);
133 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
135 if (action->getMaxDuration() != NO_MAX_DURATION)
136 action->updateMaxDuration(delta);
138 XBT_DEBUG("Action (%p) : remains (%g).",
139 action, action->getRemains());
140 if ((action->getRemains() <= 0) &&
141 (lmm_get_variable_weight(action->getVariable()) > 0)) {
143 action->setState(SURF_ACTION_DONE);
144 } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
145 (action->getMaxDuration() <= 0)) {
147 action->setState(SURF_ACTION_DONE);
149 /* Need to check that none of the model has failed */
150 lmm_constraint_t cnst = NULL;
152 void *constraint_id = NULL;
154 while ((cnst = lmm_get_cnst_from_var(ptask_maxmin_system, action->getVariable(),
156 constraint_id = lmm_constraint_id(cnst);
158 if (static_cast<WorkstationPtr>(constraint_id)->getState() == SURF_RESOURCE_OFF) {
159 XBT_DEBUG("Action (%p) Failed!!", action);
161 action->setState(SURF_ACTION_FAILED);
170 ActionPtr WorkstationL07Model::executeParallelTask(int workstation_nb,
171 void **workstation_list,
173 *computation_amount, double
174 *communication_amount,
177 WorkstationL07ActionPtr action;
182 double latency = 0.0;
184 if (ptask_parallel_task_link_set == NULL)
185 ptask_parallel_task_link_set = xbt_dict_new_homogeneous(NULL);
187 xbt_dict_reset(ptask_parallel_task_link_set);
189 /* Compute the number of affected resources... */
190 for (i = 0; i < workstation_nb; i++) {
191 for (j = 0; j < workstation_nb; j++) {
192 xbt_dynar_t route=NULL;
194 if (communication_amount[i * workstation_nb + j] > 0) {
200 routing_platf->getRouteAndLatency(static_cast<WorkstationL07Ptr>(workstation_list[i])->p_netElm,
201 static_cast<WorkstationL07Ptr>(workstation_list[j])->p_netElm,
204 latency = MAX(latency, lat);
206 xbt_dynar_foreach(route, cpt, _link) {
207 link = static_cast<LinkL07Ptr>(_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 (i = 0; i < workstation_nb; i++)
218 if (computation_amount[i] > 0)
221 action = new WorkstationL07Action(this, 1, 0);
222 XBT_DEBUG("Creating a parallel task (%p) with %d cpus and %d links.",
223 action, workstation_nb, nb_link);
224 action->m_suspended = 0; /* Should be useless because of the
225 calloc but it seems to help valgrind... */
226 action->m_workstationNb = workstation_nb;
227 action->p_workstationList = (WorkstationPtr *) workstation_list;
228 action->p_computationAmount = computation_amount;
229 action->p_communicationAmount = communication_amount;
230 action->m_latency = latency;
231 action->m_rate = rate;
233 action->p_variable = lmm_variable_new(ptask_maxmin_system, action, 1.0,
234 (action->m_rate > 0) ? action->m_rate : -1.0,
235 workstation_nb + nb_link);
237 if (action->m_latency > 0)
238 lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 0.0);
240 for (i = 0; i < workstation_nb; i++)
241 lmm_expand(ptask_maxmin_system,
242 static_cast<WorkstationL07Ptr>(workstation_list[i])->p_cpu->getConstraint(),
243 action->getVariable(), computation_amount[i]);
245 for (i = 0; i < workstation_nb; i++) {
246 for (j = 0; j < workstation_nb; j++) {
250 xbt_dynar_t route=NULL;
251 if (communication_amount[i * workstation_nb + j] == 0.0)
254 routing_platf->getRouteAndLatency(static_cast<WorkstationL07Ptr>(workstation_list[i])->p_netElm,
255 static_cast<WorkstationL07Ptr>(workstation_list[j])->p_netElm,
258 xbt_dynar_foreach(route, cpt, _link) {
259 link = static_cast<LinkL07Ptr>(_link);
260 lmm_expand_add(ptask_maxmin_system, link->getConstraint(),
261 action->getVariable(),
262 communication_amount[i * workstation_nb + j]);
267 if (nb_link + nb_host == 0) {
268 action->setCost(1.0);
269 action->setRemains(0.0);
275 WorkstationPtr WorkstationL07Model::createWorkstation(const char *name)
277 WorkstationL07Ptr wk = NULL;
278 xbt_assert(!surf_workstation_resource_priv(surf_workstation_resource_by_name(name)),
279 "Host '%s' declared several times in the platform file.",
282 wk = new WorkstationL07(this, name, NULL,
283 static_cast<RoutingEdgePtr>(xbt_lib_get_or_null(host_lib, name, ROUTING_HOST_LEVEL)),
284 static_cast<CpuPtr>(xbt_lib_get_or_null(host_lib, name, SURF_CPU_LEVEL)));
286 xbt_lib_set(host_lib, name, SURF_WKS_LEVEL, wk);
288 return wk;//FIXME:xbt_lib_get_elm_or_null(host_lib, name);
291 ActionPtr WorkstationL07Model::communicate(WorkstationPtr src, WorkstationPtr dst,
292 double size, double rate)
294 void **workstation_list = xbt_new0(void *, 2);
295 double *computation_amount = xbt_new0(double, 2);
296 double *communication_amount = xbt_new0(double, 4);
297 ActionPtr res = NULL;
299 workstation_list[0] = src;
300 workstation_list[1] = dst;
301 communication_amount[1] = size;
303 res = executeParallelTask(2, workstation_list,
305 communication_amount, rate);
310 xbt_dynar_t WorkstationL07Model::getRoute(WorkstationPtr src, WorkstationPtr dst)
312 xbt_dynar_t route=NULL;
313 routing_platf->getRouteAndLatency(src->p_netElm, dst->p_netElm, &route, NULL);
317 CpuPtr CpuL07Model::createCpu(const char *name, xbt_dynar_t powerPeak,
318 int pstate, double power_scale,
319 tmgr_trace_t power_trace, int core,
320 e_surf_resource_state_t state_initial,
321 tmgr_trace_t state_trace,
322 xbt_dict_t cpu_properties)
324 double power_initial = xbt_dynar_get_as(powerPeak, pstate, double);
325 xbt_dynar_free(&powerPeak); // kill memory leak
327 xbt_assert(!surf_workstation_resource_priv(surf_workstation_resource_by_name(name)),
328 "Host '%s' declared several times in the platform file.",
331 CpuL07Ptr cpu = new CpuL07(this, name, cpu_properties,
332 power_initial, power_scale, power_trace,
333 core, state_initial, state_trace);
335 xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu);
340 NetworkLinkPtr NetworkL07Model::createNetworkLink(const char *name,
342 tmgr_trace_t bw_trace,
344 tmgr_trace_t lat_trace,
345 e_surf_resource_state_t
347 tmgr_trace_t state_trace,
348 e_surf_link_sharing_policy_t policy,
349 xbt_dict_t properties)
351 xbt_assert(!xbt_lib_get_or_null(link_lib, name, SURF_LINK_LEVEL),
352 "Link '%s' declared several times in the platform file.",
355 LinkL07Ptr nw_link = new LinkL07(this, name, properties,
356 bw_initial, bw_trace,
357 lat_initial, lat_trace,
358 state_initial, state_trace,
361 xbt_lib_set(link_lib, name, SURF_LINK_LEVEL, nw_link);
365 void WorkstationL07Model::addTraces()
367 xbt_dict_cursor_t cursor = NULL;
368 char *trace_name, *elm;
370 if (!trace_connect_list_host_avail)
373 /* Connect traces relative to cpu */
374 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
375 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
376 CpuL07Ptr host = static_cast<CpuL07Ptr>(surf_cpu_resource_priv(surf_cpu_resource_by_name(elm)));
378 xbt_assert(host, "Host %s undefined", elm);
379 xbt_assert(trace, "Trace %s undefined", trace_name);
381 host->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, host);
384 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
385 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
386 CpuL07Ptr host = static_cast<CpuL07Ptr>(surf_cpu_resource_priv(surf_cpu_resource_by_name(elm)));
388 xbt_assert(host, "Host %s undefined", elm);
389 xbt_assert(trace, "Trace %s undefined", trace_name);
391 host->p_powerEvent = tmgr_history_add_trace(history, trace, 0.0, 0, host);
394 /* Connect traces relative to network */
395 xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
396 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
397 LinkL07Ptr link = static_cast<LinkL07Ptr>(xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL));
399 xbt_assert(link, "Link %s undefined", elm);
400 xbt_assert(trace, "Trace %s undefined", trace_name);
402 link->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
405 xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
406 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
407 LinkL07Ptr link = static_cast<LinkL07Ptr>(xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL));
409 xbt_assert(link, "Link %s undefined", elm);
410 xbt_assert(trace, "Trace %s undefined", trace_name);
412 link->p_bwEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
415 xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
416 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
417 LinkL07Ptr link = static_cast<LinkL07Ptr>(xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL));
419 xbt_assert(link, "Link %s undefined", elm);
420 xbt_assert(trace, "Trace %s undefined", trace_name);
422 link->p_latEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
430 WorkstationL07::WorkstationL07(WorkstationModelPtr model, const char* name, xbt_dict_t props, RoutingEdgePtr netElm, CpuPtr cpu)
431 : Workstation(model, name, props, NULL, netElm, cpu)
435 double WorkstationL07::getPowerPeakAt(int /*pstate_index*/)
437 XBT_DEBUG("[ws_get_power_peak_at] Not implemented for workstation_ptask_L07");
441 int WorkstationL07::getNbPstates()
443 XBT_DEBUG("[ws_get_nb_pstates] Not implemented for workstation_ptask_L07");
447 void WorkstationL07::setPowerPeakAt(int /*pstate_index*/)
449 XBT_DEBUG("[ws_set_power_peak_at] Not implemented for workstation_ptask_L07");
452 double WorkstationL07::getConsumedEnergy()
454 XBT_DEBUG("[ws_get_consumed_energy] Not implemented for workstation_ptask_L07");
458 CpuL07::CpuL07(CpuL07ModelPtr model, const char* name, xbt_dict_t props,
459 double power_initial, double power_scale, tmgr_trace_t power_trace,
460 int core, e_surf_resource_state_t state_initial, tmgr_trace_t state_trace)
461 : Cpu(model, name, props, lmm_constraint_new(ptask_maxmin_system, this, power_initial * power_scale),
462 core, power_initial, power_scale)
464 xbt_assert(m_powerScale > 0, "Power has to be >0");
467 p_powerEvent = tmgr_history_add_trace(history, power_trace, 0.0, 0, this);
471 setState(state_initial);
473 p_stateEvent = tmgr_history_add_trace(history, state_trace, 0.0, 0, this);
476 LinkL07::LinkL07(NetworkL07ModelPtr model, const char* name, xbt_dict_t props,
478 tmgr_trace_t bw_trace,
480 tmgr_trace_t lat_trace,
481 e_surf_resource_state_t state_initial,
482 tmgr_trace_t state_trace,
483 e_surf_link_sharing_policy_t policy)
484 : NetworkLink(model, name, props, lmm_constraint_new(ptask_maxmin_system, this, bw_initial), history, state_trace)
486 m_bwCurrent = bw_initial;
488 p_bwEvent = tmgr_history_add_trace(history, bw_trace, 0.0, 0, this);
490 setState(state_initial);
491 m_latCurrent = lat_initial;
494 p_latEvent = tmgr_history_add_trace(history, lat_trace, 0.0, 0, this);
496 if (policy == SURF_LINK_FATPIPE)
497 lmm_constraint_shared(getConstraint());
500 bool CpuL07::isUsed(){
501 return lmm_constraint_used(ptask_maxmin_system, getConstraint());
504 bool LinkL07::isUsed(){
505 return lmm_constraint_used(ptask_maxmin_system, getConstraint());
508 void CpuL07::updateState(tmgr_trace_event_t event_type, double value, double /*date*/){
509 XBT_DEBUG("Updating cpu %s (%p) with value %g", getName(), this, value);
510 if (event_type == p_powerEvent) {
511 m_powerScale = value;
512 lmm_update_constraint_bound(ptask_maxmin_system, getConstraint(), m_powerPeak * m_powerScale);
513 if (tmgr_trace_event_free(event_type))
515 } else if (event_type == p_stateEvent) {
517 setState(SURF_RESOURCE_ON);
519 setState(SURF_RESOURCE_OFF);
520 if (tmgr_trace_event_free(event_type))
523 XBT_CRITICAL("Unknown event ! \n");
529 void LinkL07::updateState(tmgr_trace_event_t event_type, double value, double date){
530 XBT_DEBUG("Updating link %s (%p) with value=%f for date=%g", getName(), this, value, date);
531 if (event_type == p_bwEvent) {
532 updateBandwidth(value, date);
533 if (tmgr_trace_event_free(event_type))
535 } else if (event_type == p_latEvent) {
536 updateLatency(value, date);
537 if (tmgr_trace_event_free(event_type))
539 } else if (event_type == p_stateEvent) {
541 setState(SURF_RESOURCE_ON);
543 setState(SURF_RESOURCE_OFF);
544 if (tmgr_trace_event_free(event_type))
547 XBT_CRITICAL("Unknown event ! \n");
553 e_surf_resource_state_t WorkstationL07::getState()
555 return p_cpu->getState();
558 ActionPtr WorkstationL07::execute(double size)
560 void **workstation_list = xbt_new0(void *, 1);
561 double *computation_amount = xbt_new0(double, 1);
562 double *communication_amount = xbt_new0(double, 1);
564 workstation_list[0] = this;
565 communication_amount[0] = 0.0;
566 computation_amount[0] = size;
568 return static_cast<WorkstationL07ModelPtr>(getModel())->executeParallelTask(1, workstation_list,
570 communication_amount, -1);
573 ActionPtr WorkstationL07::sleep(double duration)
575 WorkstationL07ActionPtr action = NULL;
577 XBT_IN("(%s,%g)", getName(), duration);
579 action = static_cast<WorkstationL07ActionPtr>(execute(1.0));
580 action->m_maxDuration = duration;
581 action->m_suspended = 2;
582 lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 0.0);
588 double LinkL07::getBandwidth()
593 void LinkL07::updateBandwidth(double value, double date)
596 lmm_update_constraint_bound(ptask_maxmin_system, getConstraint(), m_bwCurrent);
599 double LinkL07::getLatency()
604 void LinkL07::updateLatency(double value, double date)
606 lmm_variable_t var = NULL;
607 WorkstationL07ActionPtr action;
608 lmm_element_t elem = NULL;
610 m_latCurrent = value;
611 while ((var = lmm_get_var_from_cnst(ptask_maxmin_system, getConstraint(), &elem))) {
612 action = (WorkstationL07ActionPtr) lmm_variable_id(var);
613 action->updateBound();
618 bool LinkL07::isShared()
620 return lmm_constraint_is_shared(getConstraint());
627 WorkstationL07Action::~WorkstationL07Action(){
628 free(p_workstationList);
629 free(p_communicationAmount);
630 free(p_computationAmount);
633 void WorkstationL07Action::updateBound()
635 double lat_current = 0.0;
636 double lat_bound = -1.0;
639 for (i = 0; i < m_workstationNb; i++) {
640 for (j = 0; j < m_workstationNb; j++) {
641 xbt_dynar_t route=NULL;
643 if (p_communicationAmount[i * m_workstationNb + j] > 0) {
645 routing_platf->getRouteAndLatency(static_cast<WorkstationL07Ptr>(((void**)p_workstationList)[i])->p_netElm,
646 static_cast<WorkstationL07Ptr>(((void**)p_workstationList)[j])->p_netElm,
649 lat_current = MAX(lat_current, lat * p_communicationAmount[i * m_workstationNb + j]);
653 lat_bound = sg_tcp_gamma / (2.0 * lat_current);
654 XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
655 if ((m_latency == 0.0) && (m_suspended == 0)) {
657 lmm_update_variable_bound(ptask_maxmin_system, getVariable(), lat_bound);
659 lmm_update_variable_bound(ptask_maxmin_system, getVariable(), min(m_rate, lat_bound));
663 int WorkstationL07Action::unref()
667 if (actionHook::is_linked())
668 p_stateSet->erase(p_stateSet->iterator_to(*this));
670 lmm_variable_free(ptask_maxmin_system, getVariable());
677 void WorkstationL07Action::cancel()
679 setState(SURF_ACTION_FAILED);
683 void WorkstationL07Action::suspend()
685 XBT_IN("(%p))", this);
686 if (m_suspended != 2) {
688 lmm_update_variable_weight(ptask_maxmin_system, getVariable(), 0.0);
693 void WorkstationL07Action::resume()
695 XBT_IN("(%p)", this);
696 if (m_suspended != 2) {
697 lmm_update_variable_weight(ptask_maxmin_system, getVariable(), 1.0);
703 bool WorkstationL07Action::isSuspended()
705 return m_suspended == 1;
708 void WorkstationL07Action::setMaxDuration(double duration)
709 { /* FIXME: should inherit */
710 XBT_IN("(%p,%g)", this, duration);
711 m_maxDuration = duration;
715 void WorkstationL07Action::setPriority(double priority)
716 { /* FIXME: should inherit */
717 XBT_IN("(%p,%g)", this, priority);
718 m_priority = priority;
722 double WorkstationL07Action::getRemains()
724 XBT_IN("(%p)", this);