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_link_add_cb(ptask_netlink_parse_init);
38 sg_platf_postparse_add_cb(host_add_traces);
40 surf_host_model = new HostL07Model();
41 Model *model = surf_host_model;
42 xbt_dynar_push(all_existing_models, &model);
46 HostL07Model::HostL07Model() : HostModel() {
47 if (!ptask_maxmin_system)
48 ptask_maxmin_system = lmm_system_new(1);
49 surf_host_model = NULL;
50 surf_network_model = new NetworkL07Model(this);
51 surf_cpu_model_pm = new CpuL07Model(this);
53 routing_model_create(surf_network_model->createLink("__loopback__",
56 SURF_RESOURCE_ON, NULL,
57 SURF_LINK_FATPIPE, NULL));
60 HostL07Model::~HostL07Model() {
61 xbt_dict_free(&ptask_parallel_task_link_set);
63 delete surf_cpu_model_pm;
64 delete surf_network_model;
67 if (ptask_maxmin_system) {
68 lmm_system_free(ptask_maxmin_system);
69 ptask_maxmin_system = NULL;
73 double HostL07Model::shareResources(double /*now*/)
77 ActionList *running_actions = getRunningActionSet();
78 double min = this->shareResourcesMaxMin(running_actions,
82 for(ActionList::iterator it(running_actions->begin()), itend(running_actions->end())
83 ; it != itend ; ++it) {
84 action = static_cast<L07Action*>(&*it);
85 if (action->m_latency > 0) {
87 min = action->m_latency;
88 XBT_DEBUG("Updating min (value) with %p (start %f): %f", action,
89 action->getStartTime(), min);
90 } else if (action->m_latency < min) {
91 min = action->m_latency;
92 XBT_DEBUG("Updating min (latency) with %p (start %f): %f", action,
93 action->getStartTime(), min);
98 XBT_DEBUG("min value : %f", min);
103 void HostL07Model::updateActionsState(double /*now*/, double delta) {
106 ActionList *actionSet = getRunningActionSet();
108 for(ActionList::iterator it = actionSet->begin(), itNext = it
109 ; it != actionSet->end()
112 action = static_cast<L07Action*>(&*it);
113 if (action->m_latency > 0) {
114 if (action->m_latency > delta) {
115 double_update(&(action->m_latency), delta, sg_surf_precision);
117 action->m_latency = 0.0;
119 if ((action->m_latency == 0.0) && (action->isSuspended() == 0)) {
120 action->updateBound();
121 lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 1.0);
124 XBT_DEBUG("Action (%p) : remains (%g) updated by %g.",
125 action, action->getRemains(), lmm_variable_getvalue(action->getVariable()) * delta);
126 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
128 if (action->getMaxDuration() != NO_MAX_DURATION)
129 action->updateMaxDuration(delta);
131 XBT_DEBUG("Action (%p) : remains (%g).", action, action->getRemains());
133 /* In the next if cascade, the action can be finished either because:
134 * - The amount of remaining work reached 0
135 * - The max duration was reached
136 * If it's not done, it may have failed.
139 if ((action->getRemains() <= 0) &&
140 (lmm_get_variable_weight(action->getVariable()) > 0)) {
142 action->setState(SURF_ACTION_DONE);
143 } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
144 (action->getMaxDuration() <= 0)) {
146 action->setState(SURF_ACTION_DONE);
148 /* Need to check that none of the model has failed */
149 lmm_constraint_t cnst = NULL;
152 while ((cnst = lmm_get_cnst_from_var(ptask_maxmin_system, action->getVariable(), i++))) {
153 void *constraint_id = lmm_constraint_id(cnst);
155 if (static_cast<Host*>(constraint_id)->getState() == SURF_RESOURCE_OFF) {
156 XBT_DEBUG("Action (%p) Failed!!", action);
158 action->setState(SURF_ACTION_FAILED);
167 Action *HostL07Model::executeParallelTask(int host_nb,
169 double *flops_amount,
170 double *bytes_amount,
173 L07Action *action = new L07Action(this, 1, 0);
177 double latency = 0.0;
179 action->p_edgeList->reserve(host_nb);
180 for (int i = 0; i<host_nb; i++)
181 action->p_edgeList->push_back(sg_host_edge(host_list[i]));
183 if (ptask_parallel_task_link_set == NULL)
184 ptask_parallel_task_link_set = xbt_dict_new_homogeneous(NULL);
186 xbt_dict_reset(ptask_parallel_task_link_set);
188 /* Compute the number of affected resources... */
189 for (int i = 0; i < host_nb; i++) {
190 for (int j = 0; j < host_nb; j++) {
191 xbt_dynar_t route=NULL;
193 if (bytes_amount[i * host_nb + j] > 0) {
199 routing_platf->getRouteAndLatency((*action->p_edgeList)[i], (*action->p_edgeList)[j],
201 latency = MAX(latency, lat);
203 xbt_dynar_foreach(route, cpt, _link) {
204 link = static_cast<LinkL07*>(_link);
205 xbt_dict_set(ptask_parallel_task_link_set, link->getName(), link, NULL);
211 nb_link = xbt_dict_length(ptask_parallel_task_link_set);
212 xbt_dict_reset(ptask_parallel_task_link_set);
214 for (int i = 0; i < host_nb; i++)
215 if (flops_amount[i] > 0)
218 XBT_DEBUG("Creating a parallel task (%p) with %d cpus and %d links.",
219 action, host_nb, nb_link);
220 action->m_suspended = 0; /* valgrind seems to want it despite the calloc... */
221 action->p_computationAmount = flops_amount;
222 action->p_communicationAmount = bytes_amount;
223 action->m_latency = latency;
224 action->m_rate = rate;
226 action->p_variable = lmm_variable_new(ptask_maxmin_system, action, 1.0,
227 (rate > 0 ? rate : -1.0),
230 if (action->m_latency > 0)
231 lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 0.0);
233 for (int i = 0; i < host_nb; i++)
234 lmm_expand(ptask_maxmin_system,
235 sg_host_surfcpu(host_list[i])->getConstraint(),
236 action->getVariable(), flops_amount[i]);
238 for (int i = 0; i < host_nb; i++) {
239 for (int j = 0; j < host_nb; j++) {
242 xbt_dynar_t route=NULL;
243 if (bytes_amount[i * host_nb + j] == 0.0)
246 routing_platf->getRouteAndLatency((*action->p_edgeList)[i], (*action->p_edgeList)[j],
249 xbt_dynar_foreach(route, cpt, _link) {
250 LinkL07 *link = static_cast<LinkL07*>(_link);
251 lmm_expand_add(ptask_maxmin_system, link->getConstraint(),
252 action->getVariable(),
253 bytes_amount[i * host_nb + j]);
258 if (nb_link + nb_host == 0) {
259 action->setCost(1.0);
260 action->setRemains(0.0);
266 Host *HostL07Model::createHost(const char *name,RoutingEdge *netElm, Cpu *cpu)
268 HostL07 *host = new HostL07(this, name, NULL, netElm, cpu);
270 surf_callback_emit(hostCreatedCallbacks, host);
271 xbt_lib_set(host_lib, name, SURF_HOST_LEVEL, host);
276 Action *NetworkL07Model::communicate(RoutingEdge *src, RoutingEdge *dst,
277 double size, double rate)
279 sg_host_t*host_list = xbt_new0(sg_host_t, 2);
280 double *flops_amount = xbt_new0(double, 2);
281 double *bytes_amount = xbt_new0(double, 4);
284 host_list[0] = sg_host_by_name(src->getName());
285 host_list[1] = sg_host_by_name(dst->getName());
286 bytes_amount[1] = size;
288 res = p_hostModel->executeParallelTask(2, host_list,
295 xbt_dynar_t HostL07Model::getRoute(Host *src, Host *dst)
297 xbt_dynar_t route=NULL;
298 routing_platf->getRouteAndLatency(src->p_netElm, dst->p_netElm, &route, NULL);
302 Cpu *CpuL07Model::createCpu(const char *name, xbt_dynar_t powerPeak,
303 int pstate, double power_scale,
304 tmgr_trace_t power_trace, int core,
305 e_surf_resource_state_t state_initial,
306 tmgr_trace_t state_trace,
307 xbt_dict_t cpu_properties)
309 double power_initial = xbt_dynar_get_as(powerPeak, pstate, double);
310 sg_host_t sg_host = sg_host_by_name(name);
312 CpuL07 *cpu = new CpuL07(this, name, cpu_properties,
313 power_initial, power_scale, power_trace,
314 core, state_initial, state_trace);
315 sg_host_surfcpu_register(sg_host, cpu);
319 Link* NetworkL07Model::createLink(const char *name,
321 tmgr_trace_t bw_trace,
323 tmgr_trace_t lat_trace,
324 e_surf_resource_state_t state_initial,
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 state_initial, state_trace,
337 surf_callback_emit(networkLinkCreatedCallbacks, 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_surfcpu(sg_host_by_name(elm)));
354 xbt_assert(host, "Host %s undefined", elm);
355 xbt_assert(trace, "Trace %s undefined", trace_name);
357 host->p_stateEvent = tmgr_history_add_trace(history, trace, 0.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_surfcpu(sg_host_by_name(elm)));
364 xbt_assert(host, "Host %s undefined", elm);
365 xbt_assert(trace, "Trace %s undefined", trace_name);
367 host->p_speedEvent = tmgr_history_add_trace(history, trace, 0.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 = tmgr_history_add_trace(history, trace, 0.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 = tmgr_history_add_trace(history, trace, 0.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 = tmgr_history_add_trace(history, trace, 0.0, 0, link);
406 HostL07::HostL07(HostModel *model, const char* name, xbt_dict_t props, RoutingEdge *netElm, Cpu *cpu)
407 : Host(model, name, props, NULL, netElm, cpu)
411 CpuL07::CpuL07(CpuL07Model *model, const char* name, xbt_dict_t props,
412 double speedInitial, double speedScale, tmgr_trace_t speedTrace,
413 int core, e_surf_resource_state_t state_initial, tmgr_trace_t state_trace)
414 : Cpu(model, name, props, lmm_constraint_new(ptask_maxmin_system, this, speedInitial * speedScale),
415 core, speedInitial, speedScale, state_initial)
417 xbt_assert(m_speedScale > 0, "Power has to be >0");
420 p_speedEvent = tmgr_history_add_trace(history, speedTrace, 0.0, 0, this);
425 p_stateEvent = tmgr_history_add_trace(history, state_trace, 0.0, 0, this);
428 LinkL07::LinkL07(NetworkL07Model *model, const char* name, xbt_dict_t props,
430 tmgr_trace_t bw_trace,
432 tmgr_trace_t lat_trace,
433 e_surf_resource_state_t state_initial,
434 tmgr_trace_t state_trace,
435 e_surf_link_sharing_policy_t policy)
436 : Link(model, name, props, lmm_constraint_new(ptask_maxmin_system, this, bw_initial), history, state_trace)
438 m_bwCurrent = bw_initial;
440 p_bwEvent = tmgr_history_add_trace(history, bw_trace, 0.0, 0, this);
442 setState(state_initial);
443 m_latCurrent = lat_initial;
446 p_latEvent = tmgr_history_add_trace(history, lat_trace, 0.0, 0, this);
448 if (policy == SURF_LINK_FATPIPE)
449 lmm_constraint_shared(getConstraint());
452 Action *CpuL07::execute(double size)
454 sg_host_t*host_list = xbt_new0(sg_host_t, 1);
455 double *flops_amount = xbt_new0(double, 1);
456 double *bytes_amount = xbt_new0(double, 1);
458 host_list[0] = sg_host_by_name(getName());
459 flops_amount[0] = size;
461 return static_cast<HostL07Model*>(getModel())->executeParallelTask(1, host_list,
466 Action *CpuL07::sleep(double duration)
468 L07Action *action = NULL;
470 XBT_IN("(%s,%g)", getName(), duration);
472 action = static_cast<L07Action*>(execute(1.0));
473 action->m_maxDuration = duration;
474 action->m_suspended = 2;
475 lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 0.0);
481 bool CpuL07::isUsed(){
482 return lmm_constraint_used(ptask_maxmin_system, getConstraint());
485 bool LinkL07::isUsed(){
486 return lmm_constraint_used(ptask_maxmin_system, getConstraint());
489 void CpuL07::updateState(tmgr_trace_event_t event_type, double value, double /*date*/){
490 XBT_DEBUG("Updating cpu %s (%p) with value %g", getName(), this, value);
491 if (event_type == p_speedEvent) {
492 m_speedScale = value;
493 lmm_update_constraint_bound(ptask_maxmin_system, getConstraint(), m_speedPeak * m_speedScale);
494 if (tmgr_trace_event_free(event_type))
496 } else if (event_type == p_stateEvent) {
498 setState(SURF_RESOURCE_ON);
500 setState(SURF_RESOURCE_OFF);
501 if (tmgr_trace_event_free(event_type))
504 XBT_CRITICAL("Unknown event ! \n");
510 void LinkL07::updateState(tmgr_trace_event_t event_type, double value, double date) {
511 XBT_DEBUG("Updating link %s (%p) with value=%f for date=%g", getName(), this, value, date);
512 if (event_type == p_bwEvent) {
513 updateBandwidth(value, date);
514 if (tmgr_trace_event_free(event_type))
516 } else if (event_type == p_latEvent) {
517 updateLatency(value, date);
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 e_surf_resource_state_t HostL07::getState() {
535 return p_cpu->getState();
539 double LinkL07::getBandwidth()
544 void LinkL07::updateBandwidth(double value, double date)
547 lmm_update_constraint_bound(ptask_maxmin_system, 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(ptask_maxmin_system, 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_edgeList->size();
580 for (i = 0; i < hostNb; i++) {
581 for (j = 0; j < hostNb; j++) {
582 xbt_dynar_t route=NULL;
584 if (p_communicationAmount[i * hostNb + j] > 0) {
586 routing_platf->getRouteAndLatency((*p_edgeList)[i], (*p_edgeList)[j],
589 lat_current = MAX(lat_current, lat * p_communicationAmount[i * hostNb + j]);
593 lat_bound = sg_tcp_gamma / (2.0 * lat_current);
594 XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
595 if ((m_latency == 0.0) && (m_suspended == 0)) {
597 lmm_update_variable_bound(ptask_maxmin_system, getVariable(), lat_bound);
599 lmm_update_variable_bound(ptask_maxmin_system, getVariable(), min(m_rate, lat_bound));
603 int L07Action::unref()
607 if (action_hook.is_linked())
608 p_stateSet->erase(p_stateSet->iterator_to(*this));
610 lmm_variable_free(ptask_maxmin_system, getVariable());
617 void L07Action::cancel()
619 setState(SURF_ACTION_FAILED);
623 void L07Action::suspend()
625 XBT_IN("(%p))", this);
626 if (m_suspended != 2) {
628 lmm_update_variable_weight(ptask_maxmin_system, getVariable(), 0.0);
633 void L07Action::resume()
635 XBT_IN("(%p)", this);
636 if (m_suspended != 2) {
637 lmm_update_variable_weight(ptask_maxmin_system, getVariable(), 1.0);
643 bool L07Action::isSuspended()
645 return m_suspended == 1;
648 void L07Action::setMaxDuration(double duration)
649 { /* FIXME: should inherit */
650 XBT_IN("(%p,%g)", this, duration);
651 m_maxDuration = duration;
655 void L07Action::setPriority(double priority)
656 { /* FIXME: should inherit */
657 XBT_IN("(%p,%g)", this, priority);
658 m_priority = priority;
662 double L07Action::getRemains()
664 XBT_IN("(%p)", this);