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. */
11 #include "host_ptask_L07.hpp"
13 #include "cpu_interface.hpp"
14 #include "surf_routing.hpp"
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_host);
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 void surf_host_model_init_ptask_L07(void)
31 XBT_INFO("Switching to the L07 model to handle parallel tasks.");
32 xbt_assert(!surf_cpu_model_pm, "CPU model type already defined");
33 xbt_assert(!surf_network_model, "network model type already defined");
35 // Define the callbacks to parse the XML
36 sg_platf_link_add_cb(ptask_netlink_parse_init);
37 sg_platf_postparse_add_cb(host_add_traces);
39 surf_host_model = new simgrid::surf::HostL07Model();
40 xbt_dynar_push(all_existing_models, &surf_host_model);
47 HostL07Model::HostL07Model() : HostModel() {
48 p_maxminSystem = lmm_system_new(1);
49 surf_network_model = new NetworkL07Model(this,p_maxminSystem);
50 surf_cpu_model_pm = new CpuL07Model(this,p_maxminSystem);
52 routing_model_create(surf_network_model->createLink("__loopback__",
55 SURF_RESOURCE_ON, NULL,
56 SURF_LINK_FATPIPE, NULL));
59 HostL07Model::~HostL07Model() {
60 delete surf_cpu_model_pm;
61 delete surf_network_model;
64 CpuL07Model::CpuL07Model(HostL07Model *hmodel,lmm_system_t sys)
70 CpuL07Model::~CpuL07Model() {
71 surf_cpu_model_pm = NULL;
72 p_maxminSystem = NULL; // Avoid multi-free
74 NetworkL07Model::NetworkL07Model(HostL07Model *hmodel, lmm_system_t sys)
80 NetworkL07Model::~NetworkL07Model()
82 surf_network_model = NULL;
83 p_maxminSystem = NULL; // Avoid multi-free
87 double HostL07Model::shareResources(double /*now*/)
91 ActionList *running_actions = getRunningActionSet();
92 double min = this->shareResourcesMaxMin(running_actions,
96 for(ActionList::iterator it(running_actions->begin()), itend(running_actions->end())
97 ; it != itend ; ++it) {
98 action = static_cast<L07Action*>(&*it);
99 if (action->m_latency > 0) {
101 min = action->m_latency;
102 XBT_DEBUG("Updating min (value) with %p (start %f): %f", action,
103 action->getStartTime(), min);
104 } else if (action->m_latency < min) {
105 min = action->m_latency;
106 XBT_DEBUG("Updating min (latency) with %p (start %f): %f", action,
107 action->getStartTime(), min);
112 XBT_DEBUG("min value : %f", min);
117 void HostL07Model::updateActionsState(double /*now*/, double delta) {
120 ActionList *actionSet = getRunningActionSet();
122 for(ActionList::iterator it = actionSet->begin(), itNext = it
123 ; it != actionSet->end()
126 action = static_cast<L07Action*>(&*it);
127 if (action->m_latency > 0) {
128 if (action->m_latency > delta) {
129 double_update(&(action->m_latency), delta, sg_surf_precision);
131 action->m_latency = 0.0;
133 if ((action->m_latency == 0.0) && (action->isSuspended() == 0)) {
134 action->updateBound();
135 lmm_update_variable_weight(p_maxminSystem, action->getVariable(), 1.0);
138 XBT_DEBUG("Action (%p) : remains (%g) updated by %g.",
139 action, action->getRemains(), lmm_variable_getvalue(action->getVariable()) * delta);
140 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
142 if (action->getMaxDuration() != NO_MAX_DURATION)
143 action->updateMaxDuration(delta);
145 XBT_DEBUG("Action (%p) : remains (%g).", action, action->getRemains());
147 /* In the next if cascade, the action can be finished either because:
148 * - The amount of remaining work reached 0
149 * - The max duration was reached
150 * If it's not done, it may have failed.
153 if ((action->getRemains() <= 0) &&
154 (lmm_get_variable_weight(action->getVariable()) > 0)) {
156 action->setState(SURF_ACTION_DONE);
157 } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
158 (action->getMaxDuration() <= 0)) {
160 action->setState(SURF_ACTION_DONE);
162 /* Need to check that none of the model has failed */
163 lmm_constraint_t cnst = NULL;
166 while ((cnst = lmm_get_cnst_from_var(p_maxminSystem, action->getVariable(), i++))) {
167 void *constraint_id = lmm_constraint_id(cnst);
169 if (static_cast<Host*>(constraint_id)->getState() == SURF_RESOURCE_OFF) {
170 XBT_DEBUG("Action (%p) Failed!!", action);
172 action->setState(SURF_ACTION_FAILED);
181 Action *HostL07Model::executeParallelTask(int host_nb,
183 double *flops_amount,
184 double *bytes_amount,
187 L07Action *action = new L07Action(this, 1, 0);
190 int nb_used_host = 0; /* Only the hosts with something to compute (>0 flops) are counted) */
191 double latency = 0.0;
193 xbt_dict_t ptask_parallel_task_link_set = xbt_dict_new_homogeneous(NULL);
195 action->p_edgeList->reserve(host_nb);
196 for (int i = 0; i<host_nb; i++)
197 action->p_edgeList->push_back(sg_host_edge(host_list[i]));
199 /* Compute the number of affected resources... */
200 for (int i = 0; i < host_nb; i++) {
201 for (int j = 0; j < host_nb; j++) {
202 xbt_dynar_t route=NULL;
204 if (bytes_amount[i * host_nb + j] > 0) {
210 routing_platf->getRouteAndLatency((*action->p_edgeList)[i], (*action->p_edgeList)[j],
212 latency = MAX(latency, lat);
214 xbt_dynar_foreach(route, cpt, _link) {
215 link = static_cast<LinkL07*>(_link);
216 xbt_dict_set(ptask_parallel_task_link_set, link->getName(), link, NULL);
222 nb_link = xbt_dict_length(ptask_parallel_task_link_set);
223 xbt_dict_free(&ptask_parallel_task_link_set);
225 for (int i = 0; i < host_nb; i++)
226 if (flops_amount[i] > 0)
229 XBT_DEBUG("Creating a parallel task (%p) with %d cpus and %d links.",
230 action, host_nb, nb_link);
231 action->p_computationAmount = flops_amount;
232 action->p_communicationAmount = bytes_amount;
233 action->m_latency = latency;
234 action->m_rate = rate;
236 action->p_variable = lmm_variable_new(p_maxminSystem, action, 1.0,
237 (rate > 0 ? rate : -1.0),
240 if (action->m_latency > 0)
241 lmm_update_variable_weight(p_maxminSystem, action->getVariable(), 0.0);
243 for (int i = 0; i < host_nb; i++)
244 lmm_expand(p_maxminSystem,
245 sg_host_surfcpu(host_list[i])->getConstraint(),
246 action->getVariable(), flops_amount[i]);
248 for (int i = 0; i < host_nb; i++) {
249 for (int j = 0; j < host_nb; j++) {
252 xbt_dynar_t route=NULL;
253 if (bytes_amount[i * host_nb + j] == 0.0)
256 routing_platf->getRouteAndLatency((*action->p_edgeList)[i], (*action->p_edgeList)[j],
259 xbt_dynar_foreach(route, cpt, _link) {
260 LinkL07 *link = static_cast<LinkL07*>(_link);
261 lmm_expand_add(p_maxminSystem, link->getConstraint(),
262 action->getVariable(),
263 bytes_amount[i * host_nb + j]);
268 if (nb_link + nb_used_host == 0) {
269 action->setCost(1.0);
270 action->setRemains(0.0);
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 Cpu *CpuL07Model::createCpu(simgrid::Host *host, xbt_dynar_t powerPeakList,
296 int pstate, double power_scale,
297 tmgr_trace_t power_trace, int core,
298 e_surf_resource_state_t state_initial,
299 tmgr_trace_t state_trace)
301 CpuL07 *cpu = new CpuL07(this, host, powerPeakList, pstate, power_scale, power_trace,
302 core, state_initial, state_trace);
306 Link* NetworkL07Model::createLink(const char *name,
308 tmgr_trace_t bw_trace,
310 tmgr_trace_t lat_trace,
311 e_surf_resource_state_t state_initial,
312 tmgr_trace_t state_trace,
313 e_surf_link_sharing_policy_t policy,
314 xbt_dict_t properties)
316 xbt_assert(!Link::byName(name),
317 "Link '%s' declared several times in the platform file.", name);
319 Link* link = new LinkL07(this, name, properties,
320 bw_initial, bw_trace,
321 lat_initial, lat_trace,
322 state_initial, state_trace,
324 Link::onCreation(link);
328 void HostL07Model::addTraces()
330 xbt_dict_cursor_t cursor = NULL;
331 char *trace_name, *elm;
333 if (!trace_connect_list_host_avail)
336 /* Connect traces relative to cpu */
337 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
338 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
339 CpuL07 *host = static_cast<CpuL07*>(sg_host_surfcpu(sg_host_by_name(elm)));
341 xbt_assert(host, "Host %s undefined", elm);
342 xbt_assert(trace, "Trace %s undefined", trace_name);
344 host->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, host);
347 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
348 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
349 CpuL07 *host = static_cast<CpuL07*>(sg_host_surfcpu(sg_host_by_name(elm)));
351 xbt_assert(host, "Host %s undefined", elm);
352 xbt_assert(trace, "Trace %s undefined", trace_name);
354 host->p_speedEvent = tmgr_history_add_trace(history, trace, 0.0, 0, host);
357 /* Connect traces relative to network */
358 xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
359 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
360 LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
362 xbt_assert(link, "Link %s undefined", elm);
363 xbt_assert(trace, "Trace %s undefined", trace_name);
365 link->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
368 xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
369 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
370 LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
372 xbt_assert(link, "Link %s undefined", elm);
373 xbt_assert(trace, "Trace %s undefined", trace_name);
375 link->p_bwEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
378 xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
379 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
380 LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
382 xbt_assert(link, "Link %s undefined", elm);
383 xbt_assert(trace, "Trace %s undefined", trace_name);
385 link->p_latEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
393 CpuL07::CpuL07(CpuL07Model *model, simgrid::Host *host,
394 xbt_dynar_t speedPeakList, int pstate,
395 double speedScale, tmgr_trace_t speedTrace,
396 int core, e_surf_resource_state_t state_initial, tmgr_trace_t state_trace)
397 : Cpu(model, host, speedPeakList, pstate,
398 core, xbt_dynar_get_as(speedPeakList,pstate,double), speedScale, state_initial)
400 xbt_assert(m_speedScale > 0, "Power has to be >0");
401 p_constraint = lmm_constraint_new(model->getMaxminSystem(), this, xbt_dynar_get_as(speedPeakList,pstate,double) * speedScale);
404 p_speedEvent = tmgr_history_add_trace(history, speedTrace, 0.0, 0, this);
409 p_stateEvent = tmgr_history_add_trace(history, state_trace, 0.0, 0, this);
416 LinkL07::LinkL07(NetworkL07Model *model, const char* name, xbt_dict_t props,
418 tmgr_trace_t bw_trace,
420 tmgr_trace_t lat_trace,
421 e_surf_resource_state_t state_initial,
422 tmgr_trace_t state_trace,
423 e_surf_link_sharing_policy_t policy)
424 : Link(model, name, props, lmm_constraint_new(model->getMaxminSystem(), this, bw_initial), history, state_trace)
426 m_bwCurrent = bw_initial;
428 p_bwEvent = tmgr_history_add_trace(history, bw_trace, 0.0, 0, this);
430 setState(state_initial);
431 m_latCurrent = lat_initial;
434 p_latEvent = tmgr_history_add_trace(history, lat_trace, 0.0, 0, this);
436 if (policy == SURF_LINK_FATPIPE)
437 lmm_constraint_shared(getConstraint());
440 Action *CpuL07::execute(double size)
442 sg_host_t*host_list = xbt_new0(sg_host_t, 1);
443 double *flops_amount = xbt_new0(double, 1);
444 double *bytes_amount = xbt_new0(double, 1);
446 host_list[0] = getHost();
447 flops_amount[0] = size;
449 return static_cast<CpuL07Model*>(getModel())
451 ->executeParallelTask( 1, host_list, flops_amount, bytes_amount, -1);
454 Action *CpuL07::sleep(double duration)
456 L07Action *action = NULL;
458 XBT_IN("(%s,%g)", getName(), duration);
460 action = static_cast<L07Action*>(execute(1.0));
461 action->m_maxDuration = duration;
462 action->m_suspended = 2;
463 lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), 0.0);
469 bool CpuL07::isUsed(){
470 return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
473 bool LinkL07::isUsed(){
474 return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
477 void CpuL07::updateState(tmgr_trace_event_t event_type, double value, double /*date*/){
478 XBT_DEBUG("Updating cpu %s (%p) with value %g", getName(), this, value);
479 if (event_type == p_speedEvent) {
480 m_speedScale = value;
481 lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(), m_speedPeak * m_speedScale);
482 if (tmgr_trace_event_free(event_type))
484 } else if (event_type == p_stateEvent) {
486 setState(SURF_RESOURCE_ON);
488 setState(SURF_RESOURCE_OFF);
489 if (tmgr_trace_event_free(event_type))
492 XBT_CRITICAL("Unknown event ! \n");
498 void LinkL07::updateState(tmgr_trace_event_t event_type, double value, double date) {
499 XBT_DEBUG("Updating link %s (%p) with value=%f for date=%g", getName(), this, value, date);
500 if (event_type == p_bwEvent) {
501 updateBandwidth(value, date);
502 if (tmgr_trace_event_free(event_type))
504 } else if (event_type == p_latEvent) {
505 updateLatency(value, date);
506 if (tmgr_trace_event_free(event_type))
508 } else if (event_type == p_stateEvent) {
510 setState(SURF_RESOURCE_ON);
512 setState(SURF_RESOURCE_OFF);
513 if (tmgr_trace_event_free(event_type))
516 XBT_CRITICAL("Unknown event ! \n");
522 double LinkL07::getBandwidth()
527 void LinkL07::updateBandwidth(double value, double date)
530 lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(), m_bwCurrent);
533 void LinkL07::updateLatency(double value, double date)
535 lmm_variable_t var = NULL;
537 lmm_element_t elem = NULL;
539 m_latCurrent = value;
540 while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), getConstraint(), &elem))) {
541 action = static_cast<L07Action*>(lmm_variable_id(var));
542 action->updateBound();
550 L07Action::~L07Action(){
551 free(p_communicationAmount);
552 free(p_computationAmount);
555 void L07Action::updateBound()
557 double lat_current = 0.0;
558 double lat_bound = -1.0;
561 int hostNb = p_edgeList->size();
563 for (i = 0; i < hostNb; i++) {
564 for (j = 0; j < hostNb; j++) {
565 xbt_dynar_t route=NULL;
567 if (p_communicationAmount[i * hostNb + j] > 0) {
569 routing_platf->getRouteAndLatency((*p_edgeList)[i], (*p_edgeList)[j],
572 lat_current = MAX(lat_current, lat * p_communicationAmount[i * hostNb + j]);
576 lat_bound = sg_tcp_gamma / (2.0 * lat_current);
577 XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
578 if ((m_latency == 0.0) && (m_suspended == 0)) {
580 lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), lat_bound);
582 lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(),
583 std::min(m_rate, lat_bound));
587 int L07Action::unref()
591 if (action_hook.is_linked())
592 p_stateSet->erase(p_stateSet->iterator_to(*this));
594 lmm_variable_free(getModel()->getMaxminSystem(), getVariable());
601 void L07Action::suspend()
603 XBT_IN("(%p))", this);
604 if (m_suspended != 2) {
606 lmm_update_variable_weight(getModel()->getMaxminSystem(), getVariable(), 0.0);
611 void L07Action::resume()
613 XBT_IN("(%p)", this);
614 if (m_suspended != 2) {
615 lmm_update_variable_weight(getModel()->getMaxminSystem(), getVariable(), 1.0);
621 void L07Action::setMaxDuration(double duration)
622 { /* FIXME: should inherit */
623 XBT_IN("(%p,%g)", this, duration);
624 m_maxDuration = duration;
628 void L07Action::setPriority(double priority)
629 { /* FIXME: should inherit */
630 XBT_IN("(%p,%g)", this, priority);
631 m_priority = priority;
635 double L07Action::getRemains()
637 XBT_IN("(%p)", this);