1 /* Copyright (c) 2007, 2008, 2009, 2010. 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"
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;
17 WorkstationL07Model::WorkstationL07Model() : WorkstationModel("Workstation ptask_L07") {
18 if (!ptask_maxmin_system)
19 ptask_maxmin_system = lmm_system_new(1);
20 routing_model_create(createLinkResource("__loopback__",
23 SURF_RESOURCE_ON, NULL,
24 SURF_LINK_FATPIPE, NULL));
26 surf_network_model = new NetworkL07Model();
29 double WorkstationL07Model::shareResources(double now)
32 WorkstationL07ActionLmmPtr action;
34 xbt_swag_t running_actions = surf_workstation_model->p_runningActionSet;
35 double min = this->shareResourcesMaxMin(running_actions,
36 xbt_swag_offset((*action),
41 xbt_swag_foreach(_action, running_actions) {
42 action = (WorkstationL07ActionLmmPtr) _action;
43 if (action->m_latency > 0) {
45 min = action->m_latency;
46 XBT_DEBUG("Updating min (value) with %p (start %f): %f", action,
47 action->m_start, min);
48 } else if (action->m_latency < min) {
49 min = action->m_latency;
50 XBT_DEBUG("Updating min (latency) with %p (start %f): %f", action,
51 action->m_start, min);
56 XBT_DEBUG("min value : %f", min);
61 void WorkstationL07Model::updateActionsState(double now, double delta)
64 void *_action, *_next_action;
65 WorkstationL07ActionLmmPtr action;
66 xbt_swag_t running_actions =
67 surf_workstation_model->p_runningActionSet;
69 xbt_swag_foreach_safe(_action, _next_action, running_actions) {
70 action = (WorkstationL07ActionLmmPtr) _action;
72 if (action->m_latency > 0) {
73 if (action->m_latency > deltap) {
74 double_update(&(action->m_latency), deltap);
77 double_update(&(deltap), action->m_latency);
78 action->m_latency = 0.0;
80 if ((action->m_latency == 0.0) && (action->m_suspended == 0)) {
81 action->updateBound();
82 lmm_update_variable_weight(ptask_maxmin_system, action->p_variable, 1.0);
85 XBT_DEBUG("Action (%p) : remains (%g) updated by %g.",
86 action, action->m_remains, lmm_variable_getvalue(action->p_variable) * delta);
87 double_update(&(action->m_remains), lmm_variable_getvalue(action->p_variable) * delta);
89 if (action->m_maxDuration != NO_MAX_DURATION)
90 double_update(&(action->m_maxDuration), delta);
92 XBT_DEBUG("Action (%p) : remains (%g).",
93 action, action->m_remains);
94 if ((action->m_remains <= 0) &&
95 (lmm_get_variable_weight(action->p_variable) > 0)) {
96 action->m_finish = surf_get_clock();
97 action->setState(SURF_ACTION_DONE);
98 } else if ((action->m_maxDuration != NO_MAX_DURATION) &&
99 (action->m_maxDuration <= 0)) {
100 action->m_finish = surf_get_clock();
101 action->setState(SURF_ACTION_DONE);
103 /* Need to check that none of the model has failed */
104 lmm_constraint_t cnst = NULL;
106 void *constraint_id = NULL;
108 while ((cnst = lmm_get_cnst_from_var(ptask_maxmin_system, action->p_variable,
110 constraint_id = lmm_constraint_id(cnst);
112 if (((WorkstationCLM03LmmPtr)constraint_id)->p_stateCurrent == SURF_RESOURCE_OFF) {
113 XBT_DEBUG("Action (%p) Failed!!", action);
114 action->m_finish = surf_get_clock();
115 action->setState(SURF_ACTION_FAILED);
124 ActionPtr WorkstationL07Model::executeParallelTask(int workstation_nb,
125 void **workstation_list,
127 *computation_amount, double
128 *communication_amount,
131 WorkstationL07ActionLmmPtr action;
136 double latency = 0.0;
138 if (ptask_parallel_task_link_set == NULL)
139 ptask_parallel_task_link_set = xbt_dict_new_homogeneous(NULL);
141 xbt_dict_reset(ptask_parallel_task_link_set);
143 /* Compute the number of affected resources... */
144 for (i = 0; i < workstation_nb; i++) {
145 for (j = 0; j < workstation_nb; j++) {
146 xbt_dynar_t route=NULL;
148 if (communication_amount[i * workstation_nb + j] > 0) {
153 routing_platf->getRouteAndLatency(((CpuL07Ptr)workstation_list[i])->p_info, ((CpuL07Ptr)workstation_list[j])->p_info, &route,&lat);
154 latency = MAX(latency, lat);
156 xbt_dynar_foreach(route, cpt, link) {
157 xbt_dict_set(ptask_parallel_task_link_set,link->m_name,link,NULL);
163 nb_link = xbt_dict_length(ptask_parallel_task_link_set);
164 xbt_dict_reset(ptask_parallel_task_link_set);
166 for (i = 0; i < workstation_nb; i++)
167 if (computation_amount[i] > 0)
170 action = new WorkstationL07ActionLmm(this, 1, 0);
171 XBT_DEBUG("Creating a parallel task (%p) with %d cpus and %d links.",
172 action, workstation_nb, nb_link);
173 action->m_suspended = 0; /* Should be useless because of the
174 calloc but it seems to help valgrind... */
175 action->m_workstationNb = workstation_nb;
176 action->p_workstationList = (WorkstationCLM03Ptr *) workstation_list;
177 action->p_computationAmount = computation_amount;
178 action->p_communicationAmount = communication_amount;
179 action->m_latency = latency;
180 action->m_rate = rate;
182 action->p_variable = lmm_variable_new(ptask_maxmin_system, action, 1.0,
183 (action->m_rate > 0) ? action->m_rate : -1.0,
184 workstation_nb + nb_link);
186 if (action->m_latency > 0)
187 lmm_update_variable_weight(ptask_maxmin_system, action->p_variable, 0.0);
189 for (i = 0; i < workstation_nb; i++)
190 lmm_expand(ptask_maxmin_system, ((CpuL07Ptr)workstation_list[i])->p_constraint,
191 action->p_variable, computation_amount[i]);
193 for (i = 0; i < workstation_nb; i++) {
194 for (j = 0; j < workstation_nb; j++) {
196 xbt_dynar_t route=NULL;
197 if (communication_amount[i * workstation_nb + j] == 0.0)
200 routing_platf->getRouteAndLatency(((CpuL07Ptr)workstation_list[i])->p_info, ((CpuL07Ptr)workstation_list[j])->p_info, &route,NULL);
202 xbt_dynar_foreach(route, cpt, link) {
203 lmm_expand_add(ptask_maxmin_system, link->p_constraint,
205 communication_amount[i * workstation_nb + j]);
210 if (nb_link + nb_host == 0) {
211 action->m_cost = 1.0;
212 action->m_remains = 0.0;
215 return (surf_action_t) action;
218 ActionPtr WorkstationL07Model::communicate(WorkstationCLM03Ptr src, WorkstationCLM03Ptr dst,
219 double size, double rate)
221 void **workstation_list = xbt_new0(void *, 2);
222 double *computation_amount = xbt_new0(double, 2);
223 double *communication_amount = xbt_new0(double, 4);
224 ActionPtr res = NULL;
226 workstation_list[0] = src;
227 workstation_list[1] = dst;
228 communication_amount[1] = size;
230 res = executeParallelTask(2, workstation_list,
232 communication_amount, rate);
237 xbt_dynar_t WorkstationL07Model::getRoute(WorkstationCLM03Ptr src, WorkstationCLM03Ptr dst)
239 xbt_dynar_t route=NULL;
240 routing_platf->getRouteAndLatency(((CpuL07Ptr)src)->p_info, ((CpuL07Ptr)dst)->p_info, &route,NULL);
244 WorkstationCLM03Ptr WorkstationL07Model::createCpuResource(const char *name, double power_scale,
245 double power_initial,
246 tmgr_trace_t power_trace,
247 e_surf_resource_state_t state_initial,
248 tmgr_trace_t state_trace,
249 xbt_dict_t cpu_properties)
251 CpuL07Ptr cpu = NULL;
252 xbt_assert(!surf_workstation_resource_priv(surf_workstation_resource_by_name(name)),
253 "Host '%s' declared several times in the platform file.",
256 cpu = new CpuL07(this, name, cpu_properties);
258 cpu->p_info = (RoutingEdgePtr) xbt_lib_get_or_null(host_lib, name, ROUTING_HOST_LEVEL);
259 if(!(cpu->p_info)) xbt_die("Don't find ROUTING_HOST_LEVEL for '%s'",name);
261 cpu->p_power.scale = power_scale;
262 xbt_assert(cpu->p_power.scale > 0, "Power has to be >0");
264 cpu->m_powerCurrent = power_initial;
267 tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
269 cpu->p_stateCurrent = state_initial;
272 tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
275 lmm_constraint_new(ptask_maxmin_system, cpu,
276 cpu->m_powerCurrent * cpu->p_power.scale);
278 xbt_lib_set(host_lib, name, SURF_WKS_LEVEL, static_cast<ResourcePtr>(cpu));
280 return cpu;//FIXME:xbt_lib_get_elm_or_null(host_lib, name);
283 WorkstationCLM03Ptr WorkstationL07Model::createLinkResource(const char *name,
285 tmgr_trace_t bw_trace,
287 tmgr_trace_t lat_trace,
288 e_surf_resource_state_t
290 tmgr_trace_t state_trace,
291 e_surf_link_sharing_policy_t
292 policy, xbt_dict_t properties)
294 LinkL07Ptr nw_link = new LinkL07(this, xbt_strdup(name), properties);
295 xbt_assert(!xbt_lib_get_or_null(link_lib, name, SURF_LINK_LEVEL),
296 "Link '%s' declared several times in the platform file.",
299 nw_link->m_bwCurrent = bw_initial;
302 tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
303 nw_link->p_stateCurrent = state_initial;
304 nw_link->m_latCurrent = lat_initial;
306 nw_link->p_latEvent =
307 tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
309 nw_link->p_stateEvent =
310 tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
312 nw_link->p_constraint =
313 lmm_constraint_new(ptask_maxmin_system, nw_link,
314 nw_link->m_bwCurrent);
316 if (policy == SURF_LINK_FATPIPE)
317 lmm_constraint_shared(nw_link->p_constraint);
319 xbt_lib_set(link_lib, name, SURF_LINK_LEVEL, nw_link);
323 void WorkstationL07Model::addTraces()
325 xbt_dict_cursor_t cursor = NULL;
326 char *trace_name, *elm;
328 if (!trace_connect_list_host_avail)
331 /* Connect traces relative to cpu */
332 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
333 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
334 CpuL07Ptr host = (CpuL07Ptr) surf_workstation_resource_priv(surf_workstation_resource_by_name(elm));
336 xbt_assert(host, "Host %s undefined", elm);
337 xbt_assert(trace, "Trace %s undefined", trace_name);
339 host->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, host);
342 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
343 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
344 CpuL07Ptr host = (CpuL07Ptr) surf_workstation_resource_priv(surf_workstation_resource_by_name(elm));
346 xbt_assert(host, "Host %s undefined", elm);
347 xbt_assert(trace, "Trace %s undefined", trace_name);
349 host->p_power.event = tmgr_history_add_trace(history, trace, 0.0, 0, host);
352 /* Connect traces relative to network */
353 xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
354 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
355 LinkL07Ptr link = (LinkL07Ptr) xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
357 xbt_assert(link, "Link %s undefined", elm);
358 xbt_assert(trace, "Trace %s undefined", trace_name);
360 link->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
363 xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
364 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
365 LinkL07Ptr link = (LinkL07Ptr) xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
367 xbt_assert(link, "Link %s undefined", elm);
368 xbt_assert(trace, "Trace %s undefined", trace_name);
370 link->p_bwEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
373 xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
374 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
375 LinkL07Ptr link = (LinkL07Ptr) xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL);
377 xbt_assert(link, "Link %s undefined", elm);
378 xbt_assert(trace, "Trace %s undefined", trace_name);
380 link->p_latEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
388 CpuL07::CpuL07(WorkstationL07ModelPtr model, const char* name, xbt_dict_t props) : WorkstationCLM03Lmm(model, name, props) {
392 LinkL07::LinkL07(WorkstationL07ModelPtr model, const char* name, xbt_dict_t props) : WorkstationCLM03Lmm(model, name, props) {
396 bool CpuL07::isUsed(){
397 return lmm_constraint_used(ptask_maxmin_system, p_constraint);
400 bool LinkL07::isUsed(){
401 return lmm_constraint_used(ptask_maxmin_system, p_constraint);
404 void CpuL07::updateState(tmgr_trace_event_t event_type, double value, double date){
405 XBT_DEBUG("Updating cpu %s (%p) with value %g", m_name, this, value);
406 if (event_type == p_power.event) {
407 m_powerCurrent = value;
408 lmm_update_constraint_bound(ptask_maxmin_system, p_constraint, m_powerCurrent * p_power.scale);
409 if (tmgr_trace_event_free(event_type))
410 p_power.event = NULL;
411 } else if (event_type == p_stateEvent) {
413 p_stateCurrent = SURF_RESOURCE_ON;
415 p_stateCurrent = SURF_RESOURCE_OFF;
416 if (tmgr_trace_event_free(event_type))
419 XBT_CRITICAL("Unknown event ! \n");
425 void LinkL07::updateState(tmgr_trace_event_t event_type, double value, double date){
426 XBT_DEBUG("Updating link %s (%p) with value=%f for date=%g", m_name, this, value, date);
427 if (event_type == p_bwEvent) {
429 lmm_update_constraint_bound(ptask_maxmin_system, p_constraint, m_bwCurrent);
430 if (tmgr_trace_event_free(event_type))
432 } else if (event_type == p_latEvent) {
433 lmm_variable_t var = NULL;
434 WorkstationL07ActionLmmPtr action;
435 lmm_element_t elem = NULL;
437 m_latCurrent = value;
438 while ((var = lmm_get_var_from_cnst(ptask_maxmin_system, p_constraint, &elem))) {
439 action = (WorkstationL07ActionLmmPtr) lmm_variable_id(var);
440 action->updateBound();
442 if (tmgr_trace_event_free(event_type))
444 } else if (event_type == p_stateEvent) {
446 p_stateCurrent = SURF_RESOURCE_ON;
448 p_stateCurrent = SURF_RESOURCE_OFF;
449 if (tmgr_trace_event_free(event_type))
452 XBT_CRITICAL("Unknown event ! \n");
459 e_surf_resource_state_t CpuL07::getState()
461 return p_stateCurrent;
464 double CpuL07::getSpeed(double load)
466 return load * p_power.scale;
469 double CpuL07::getAvailableSpeed()
471 return m_powerCurrent;
474 ActionPtr CpuL07::execute(double size)
476 void **workstation_list = xbt_new0(void *, 1);
477 double *computation_amount = xbt_new0(double, 1);
478 double *communication_amount = xbt_new0(double, 1);
480 workstation_list[0] = this;
481 communication_amount[0] = 0.0;
482 computation_amount[0] = size;
484 return ((WorkstationL07ModelPtr)p_model)->executeParallelTask(1, workstation_list,
486 communication_amount, -1);
489 ActionPtr CpuL07::sleep(double duration)
491 WorkstationL07ActionLmmPtr action = NULL;
493 XBT_IN("(%s,%g)", m_name, duration);
495 action = dynamic_cast<WorkstationL07ActionLmmPtr>(execute(1.0));
496 action->m_maxDuration = duration;
497 action->m_suspended = 2;
498 lmm_update_variable_weight(ptask_maxmin_system, action->p_variable, 0.0);
504 double LinkL07::getBandwidth()
509 double LinkL07::getLatency()
514 bool LinkL07::isShared()
516 return lmm_constraint_is_shared(p_constraint);
523 WorkstationL07ActionLmm::~WorkstationL07ActionLmm(){
524 free(p_workstationList);
525 free(p_communicationAmount);
526 free(p_computationAmount);
528 xbt_free(p_category);
532 void WorkstationL07ActionLmm::updateBound()
534 double lat_current = 0.0;
535 double lat_bound = -1.0;
538 for (i = 0; i < m_workstationNb; i++) {
539 for (j = 0; j < m_workstationNb; j++) {
540 xbt_dynar_t route=NULL;
542 if (p_communicationAmount[i * m_workstationNb + j] > 0) {
544 routing_platf->getRouteAndLatency(((CpuL07Ptr) p_workstationList[i])->p_info, ((CpuL07Ptr)p_workstationList[j])->p_info, &route, &lat);
546 lat_current = MAX(lat_current, lat * p_communicationAmount[i * m_workstationNb + j]);
550 lat_bound = sg_tcp_gamma / (2.0 * lat_current);
551 XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
552 if ((m_latency == 0.0) && (m_suspended == 0)) {
554 lmm_update_variable_bound(ptask_maxmin_system, p_variable, lat_bound);
556 lmm_update_variable_bound(ptask_maxmin_system, p_variable, min(m_rate, lat_bound));
560 int WorkstationL07ActionLmm::unref()
564 xbt_swag_remove(static_cast<ActionPtr>(this), p_stateSet);
566 lmm_variable_free(ptask_maxmin_system, p_variable);
573 void WorkstationL07ActionLmm::cancel()
575 setState(SURF_ACTION_FAILED);
579 void WorkstationL07ActionLmm::suspend()
581 XBT_IN("(%p))", this);
582 if (m_suspended != 2) {
584 lmm_update_variable_weight(ptask_maxmin_system, p_variable, 0.0);
589 void WorkstationL07ActionLmm::resume()
591 XBT_IN("(%p)", this);
592 if (m_suspended != 2) {
593 lmm_update_variable_weight(ptask_maxmin_system, p_variable, 1.0);
599 bool WorkstationL07ActionLmm::isSuspended()
601 return m_suspended == 1;
604 void WorkstationL07ActionLmm::setMaxDuration(double duration)
605 { /* FIXME: should inherit */
606 XBT_IN("(%p,%g)", this, duration);
607 m_maxDuration = duration;
611 void WorkstationL07ActionLmm::setPriority(double priority)
612 { /* FIXME: should inherit */
613 XBT_IN("(%p,%g)", this, priority);
614 m_priority = priority;
618 double WorkstationL07ActionLmm::getRemains()
620 XBT_IN("(%p)", this);
633 static void ptask_finalize(void)
635 xbt_dict_free(&ptask_parallel_task_link_set);
637 delete surf_workstation_model;
638 surf_workstation_model = NULL;
639 delete surf_network_model;
640 surf_network_model = NULL;
642 ptask_host_count = 0;
644 if (ptask_maxmin_system) {
645 lmm_system_free(ptask_maxmin_system);
646 ptask_maxmin_system = NULL;
650 /**************************************/
651 /******* Resource Private **********/
652 /**************************************/
669 /**************************************/
670 /*** Resource Creation & Destruction **/
671 /**************************************/
675 static void ptask_parse_cpu_init(sg_platf_host_cbarg_t host)
677 ((WorkstationL07ModelPtr)surf_workstation_model)->createCpuResource(
689 static void ptask_parse_link_init(sg_platf_link_cbarg_t link)
691 if (link->policy == SURF_LINK_FULLDUPLEX) {
693 link_id = bprintf("%s_UP", link->id);
694 ((WorkstationL07ModelPtr)surf_workstation_model)->createLinkResource(link_id,
696 link->bandwidth_trace,
704 link_id = bprintf("%s_DOWN", link->id);
705 ((WorkstationL07ModelPtr)surf_workstation_model)->createLinkResource(link_id,
707 link->bandwidth_trace,
713 NULL); /* FIXME: We need to deep copy the
714 * properties or we won't be able to free
718 ((WorkstationL07ModelPtr)surf_workstation_model)->createLinkResource(link->id,
720 link->bandwidth_trace,
729 current_property_set = NULL;
734 static void ptask_define_callbacks()
736 sg_platf_host_add_cb(ptask_parse_cpu_init);
737 sg_platf_link_add_cb(ptask_parse_link_init);
738 //FIXME:sg_platf_postparse_add_cb(ptask_add_traces);
741 /**************************************/
742 /*************** Generic **************/
743 /**************************************/
744 void surf_workstation_model_init_ptask_L07(void)
746 XBT_INFO("surf_workstation_model_init_ptask_L07");
747 xbt_assert(!surf_cpu_model, "CPU model type already defined");
748 xbt_assert(!surf_network_model, "network model type already defined");
749 ptask_define_callbacks();
750 new WorkstationL07Model();
751 xbt_dynar_push(model_list, &surf_workstation_model);