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 lmm_system_t ptask_maxmin_system = NULL;
22 /**************************************/
23 /*** Resource Creation & Destruction **/
24 /**************************************/
26 static void ptask_netlink_parse_init(sg_platf_link_cbarg_t link)
28 netlink_parse_init(link);
29 current_property_set = NULL;
32 void surf_host_model_init_ptask_L07(void)
34 XBT_INFO("Switching to the L07 model to handle parallel tasks.");
35 xbt_assert(!surf_cpu_model_pm, "CPU model type already defined");
36 xbt_assert(!surf_network_model, "network model type already defined");
38 // Define the callbacks to parse the XML
39 sg_platf_link_add_cb(ptask_netlink_parse_init);
40 sg_platf_postparse_add_cb(host_add_traces);
42 surf_host_model = new simgrid::surf::HostL07Model();
43 xbt_dynar_push(all_existing_models, &surf_host_model);
50 HostL07Model::HostL07Model() : HostModel() {
51 if (!ptask_maxmin_system)
52 ptask_maxmin_system = lmm_system_new(1);
53 p_maxminSystem = ptask_maxmin_system;
54 surf_network_model = new NetworkL07Model(this,ptask_maxmin_system);
55 surf_cpu_model_pm = new CpuL07Model(this,ptask_maxmin_system);
57 routing_model_create(surf_network_model->createLink("__loopback__",
60 SURF_RESOURCE_ON, NULL,
61 SURF_LINK_FATPIPE, NULL));
64 HostL07Model::~HostL07Model() {
65 delete surf_cpu_model_pm;
66 delete surf_network_model;
68 ptask_maxmin_system = NULL; // freed as part of ~Model (it's also stored as p_maxminSystem)
71 CpuL07Model::CpuL07Model(HostL07Model *hmodel,lmm_system_t sys)
77 CpuL07Model::~CpuL07Model() {
78 surf_cpu_model_pm = NULL;
79 p_maxminSystem = NULL; // Avoid multi-free
81 NetworkL07Model::NetworkL07Model(HostL07Model *hmodel, lmm_system_t sys)
87 NetworkL07Model::~NetworkL07Model()
89 surf_network_model = NULL;
90 p_maxminSystem = NULL; // Avoid multi-free
94 double HostL07Model::shareResources(double /*now*/)
98 ActionList *running_actions = getRunningActionSet();
99 double min = this->shareResourcesMaxMin(running_actions,
103 for(ActionList::iterator it(running_actions->begin()), itend(running_actions->end())
104 ; it != itend ; ++it) {
105 action = static_cast<L07Action*>(&*it);
106 if (action->m_latency > 0) {
108 min = action->m_latency;
109 XBT_DEBUG("Updating min (value) with %p (start %f): %f", action,
110 action->getStartTime(), min);
111 } else if (action->m_latency < min) {
112 min = action->m_latency;
113 XBT_DEBUG("Updating min (latency) with %p (start %f): %f", action,
114 action->getStartTime(), min);
119 XBT_DEBUG("min value : %f", min);
124 void HostL07Model::updateActionsState(double /*now*/, double delta) {
127 ActionList *actionSet = getRunningActionSet();
129 for(ActionList::iterator it = actionSet->begin(), itNext = it
130 ; it != actionSet->end()
133 action = static_cast<L07Action*>(&*it);
134 if (action->m_latency > 0) {
135 if (action->m_latency > delta) {
136 double_update(&(action->m_latency), delta, sg_surf_precision);
138 action->m_latency = 0.0;
140 if ((action->m_latency == 0.0) && (action->isSuspended() == 0)) {
141 action->updateBound();
142 lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 1.0);
145 XBT_DEBUG("Action (%p) : remains (%g) updated by %g.",
146 action, action->getRemains(), lmm_variable_getvalue(action->getVariable()) * delta);
147 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
149 if (action->getMaxDuration() != NO_MAX_DURATION)
150 action->updateMaxDuration(delta);
152 XBT_DEBUG("Action (%p) : remains (%g).", action, action->getRemains());
154 /* In the next if cascade, the action can be finished either because:
155 * - The amount of remaining work reached 0
156 * - The max duration was reached
157 * If it's not done, it may have failed.
160 if ((action->getRemains() <= 0) &&
161 (lmm_get_variable_weight(action->getVariable()) > 0)) {
163 action->setState(SURF_ACTION_DONE);
164 } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
165 (action->getMaxDuration() <= 0)) {
167 action->setState(SURF_ACTION_DONE);
169 /* Need to check that none of the model has failed */
170 lmm_constraint_t cnst = NULL;
173 while ((cnst = lmm_get_cnst_from_var(ptask_maxmin_system, action->getVariable(), i++))) {
174 void *constraint_id = lmm_constraint_id(cnst);
176 if (static_cast<Host*>(constraint_id)->getState() == SURF_RESOURCE_OFF) {
177 XBT_DEBUG("Action (%p) Failed!!", action);
179 action->setState(SURF_ACTION_FAILED);
188 Action *HostL07Model::executeParallelTask(int host_nb,
190 double *flops_amount,
191 double *bytes_amount,
194 L07Action *action = new L07Action(this, 1, 0);
197 int nb_used_host = 0; /* Only the hosts with something to compute (>0 flops) are counted) */
198 double latency = 0.0;
200 xbt_dict_t ptask_parallel_task_link_set = xbt_dict_new_homogeneous(NULL);
202 action->p_edgeList->reserve(host_nb);
203 for (int i = 0; i<host_nb; i++)
204 action->p_edgeList->push_back(sg_host_edge(host_list[i]));
206 /* Compute the number of affected resources... */
207 for (int i = 0; i < host_nb; i++) {
208 for (int j = 0; j < host_nb; j++) {
209 xbt_dynar_t route=NULL;
211 if (bytes_amount[i * host_nb + j] > 0) {
217 routing_platf->getRouteAndLatency((*action->p_edgeList)[i], (*action->p_edgeList)[j],
219 latency = MAX(latency, lat);
221 xbt_dynar_foreach(route, cpt, _link) {
222 link = static_cast<LinkL07*>(_link);
223 xbt_dict_set(ptask_parallel_task_link_set, link->getName(), link, NULL);
229 nb_link = xbt_dict_length(ptask_parallel_task_link_set);
230 xbt_dict_free(&ptask_parallel_task_link_set);
232 for (int i = 0; i < host_nb; i++)
233 if (flops_amount[i] > 0)
236 XBT_DEBUG("Creating a parallel task (%p) with %d cpus and %d links.",
237 action, host_nb, nb_link);
238 action->m_suspended = 0; /* valgrind seems to want it despite the calloc... */
239 action->p_computationAmount = flops_amount;
240 action->p_communicationAmount = bytes_amount;
241 action->m_latency = latency;
242 action->m_rate = rate;
244 action->p_variable = lmm_variable_new(ptask_maxmin_system, action, 1.0,
245 (rate > 0 ? rate : -1.0),
248 if (action->m_latency > 0)
249 lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 0.0);
251 for (int i = 0; i < host_nb; i++)
252 lmm_expand(ptask_maxmin_system,
253 sg_host_surfcpu(host_list[i])->getConstraint(),
254 action->getVariable(), flops_amount[i]);
256 for (int i = 0; i < host_nb; i++) {
257 for (int j = 0; j < host_nb; j++) {
260 xbt_dynar_t route=NULL;
261 if (bytes_amount[i * host_nb + j] == 0.0)
264 routing_platf->getRouteAndLatency((*action->p_edgeList)[i], (*action->p_edgeList)[j],
267 xbt_dynar_foreach(route, cpt, _link) {
268 LinkL07 *link = static_cast<LinkL07*>(_link);
269 lmm_expand_add(ptask_maxmin_system, link->getConstraint(),
270 action->getVariable(),
271 bytes_amount[i * host_nb + j]);
276 if (nb_link + nb_used_host == 0) {
277 action->setCost(1.0);
278 action->setRemains(0.0);
284 Action *NetworkL07Model::communicate(RoutingEdge *src, RoutingEdge *dst,
285 double size, double rate)
287 sg_host_t*host_list = xbt_new0(sg_host_t, 2);
288 double *flops_amount = xbt_new0(double, 2);
289 double *bytes_amount = xbt_new0(double, 4);
292 host_list[0] = sg_host_by_name(src->getName());
293 host_list[1] = sg_host_by_name(dst->getName());
294 bytes_amount[1] = size;
296 res = p_hostModel->executeParallelTask(2, host_list,
303 Cpu *CpuL07Model::createCpu(simgrid::Host *host, xbt_dynar_t powerPeakList,
304 int pstate, double power_scale,
305 tmgr_trace_t power_trace, int core,
306 e_surf_resource_state_t state_initial,
307 tmgr_trace_t state_trace)
309 CpuL07 *cpu = new CpuL07(this, host, powerPeakList, pstate, power_scale, power_trace,
310 core, state_initial, state_trace);
314 Link* NetworkL07Model::createLink(const char *name,
316 tmgr_trace_t bw_trace,
318 tmgr_trace_t lat_trace,
319 e_surf_resource_state_t state_initial,
320 tmgr_trace_t state_trace,
321 e_surf_link_sharing_policy_t policy,
322 xbt_dict_t properties)
324 xbt_assert(!Link::byName(name),
325 "Link '%s' declared several times in the platform file.", name);
327 Link* link = new LinkL07(this, name, properties,
328 bw_initial, bw_trace,
329 lat_initial, lat_trace,
330 state_initial, state_trace,
332 Link::onCreation(link);
336 void HostL07Model::addTraces()
338 xbt_dict_cursor_t cursor = NULL;
339 char *trace_name, *elm;
341 if (!trace_connect_list_host_avail)
344 /* Connect traces relative to cpu */
345 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
346 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
347 CpuL07 *host = static_cast<CpuL07*>(sg_host_surfcpu(sg_host_by_name(elm)));
349 xbt_assert(host, "Host %s undefined", elm);
350 xbt_assert(trace, "Trace %s undefined", trace_name);
352 host->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, host);
355 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
356 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
357 CpuL07 *host = static_cast<CpuL07*>(sg_host_surfcpu(sg_host_by_name(elm)));
359 xbt_assert(host, "Host %s undefined", elm);
360 xbt_assert(trace, "Trace %s undefined", trace_name);
362 host->p_speedEvent = tmgr_history_add_trace(history, trace, 0.0, 0, host);
365 /* Connect traces relative to network */
366 xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
367 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
368 LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
370 xbt_assert(link, "Link %s undefined", elm);
371 xbt_assert(trace, "Trace %s undefined", trace_name);
373 link->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
376 xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
377 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
378 LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
380 xbt_assert(link, "Link %s undefined", elm);
381 xbt_assert(trace, "Trace %s undefined", trace_name);
383 link->p_bwEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
386 xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
387 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
388 LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
390 xbt_assert(link, "Link %s undefined", elm);
391 xbt_assert(trace, "Trace %s undefined", trace_name);
393 link->p_latEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
401 CpuL07::CpuL07(CpuL07Model *model, simgrid::Host *host,
402 xbt_dynar_t speedPeakList, int pstate,
403 double speedScale, tmgr_trace_t speedTrace,
404 int core, e_surf_resource_state_t state_initial, tmgr_trace_t state_trace)
405 : Cpu(model, host, speedPeakList, pstate,
406 core, xbt_dynar_get_as(speedPeakList,pstate,double), speedScale, state_initial)
408 xbt_assert(m_speedScale > 0, "Power has to be >0");
409 p_constraint = lmm_constraint_new(ptask_maxmin_system, this, xbt_dynar_get_as(speedPeakList,pstate,double) * speedScale);
412 p_speedEvent = tmgr_history_add_trace(history, speedTrace, 0.0, 0, this);
417 p_stateEvent = tmgr_history_add_trace(history, state_trace, 0.0, 0, this);
424 LinkL07::LinkL07(NetworkL07Model *model, const char* name, xbt_dict_t props,
426 tmgr_trace_t bw_trace,
428 tmgr_trace_t lat_trace,
429 e_surf_resource_state_t state_initial,
430 tmgr_trace_t state_trace,
431 e_surf_link_sharing_policy_t policy)
432 : Link(model, name, props, lmm_constraint_new(ptask_maxmin_system, this, bw_initial), history, state_trace)
434 m_bwCurrent = bw_initial;
436 p_bwEvent = tmgr_history_add_trace(history, bw_trace, 0.0, 0, this);
438 setState(state_initial);
439 m_latCurrent = lat_initial;
442 p_latEvent = tmgr_history_add_trace(history, lat_trace, 0.0, 0, this);
444 if (policy == SURF_LINK_FATPIPE)
445 lmm_constraint_shared(getConstraint());
448 Action *CpuL07::execute(double size)
450 sg_host_t*host_list = xbt_new0(sg_host_t, 1);
451 double *flops_amount = xbt_new0(double, 1);
452 double *bytes_amount = xbt_new0(double, 1);
454 host_list[0] = getHost();
455 flops_amount[0] = size;
457 return static_cast<CpuL07Model*>(getModel())
459 ->executeParallelTask( 1, host_list, flops_amount, bytes_amount, -1);
462 Action *CpuL07::sleep(double duration)
464 L07Action *action = NULL;
466 XBT_IN("(%s,%g)", getName(), duration);
468 action = static_cast<L07Action*>(execute(1.0));
469 action->m_maxDuration = duration;
470 action->m_suspended = 2;
471 lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 0.0);
477 bool CpuL07::isUsed(){
478 return lmm_constraint_used(ptask_maxmin_system, getConstraint());
481 bool LinkL07::isUsed(){
482 return lmm_constraint_used(ptask_maxmin_system, getConstraint());
485 void CpuL07::updateState(tmgr_trace_event_t event_type, double value, double /*date*/){
486 XBT_DEBUG("Updating cpu %s (%p) with value %g", getName(), this, value);
487 if (event_type == p_speedEvent) {
488 m_speedScale = value;
489 lmm_update_constraint_bound(ptask_maxmin_system, getConstraint(), m_speedPeak * m_speedScale);
490 if (tmgr_trace_event_free(event_type))
492 } else if (event_type == p_stateEvent) {
494 setState(SURF_RESOURCE_ON);
496 setState(SURF_RESOURCE_OFF);
497 if (tmgr_trace_event_free(event_type))
500 XBT_CRITICAL("Unknown event ! \n");
506 void LinkL07::updateState(tmgr_trace_event_t event_type, double value, double date) {
507 XBT_DEBUG("Updating link %s (%p) with value=%f for date=%g", getName(), this, value, date);
508 if (event_type == p_bwEvent) {
509 updateBandwidth(value, date);
510 if (tmgr_trace_event_free(event_type))
512 } else if (event_type == p_latEvent) {
513 updateLatency(value, date);
514 if (tmgr_trace_event_free(event_type))
516 } else if (event_type == p_stateEvent) {
518 setState(SURF_RESOURCE_ON);
520 setState(SURF_RESOURCE_OFF);
521 if (tmgr_trace_event_free(event_type))
524 XBT_CRITICAL("Unknown event ! \n");
530 double LinkL07::getBandwidth()
535 void LinkL07::updateBandwidth(double value, double date)
538 lmm_update_constraint_bound(ptask_maxmin_system, getConstraint(), m_bwCurrent);
541 void LinkL07::updateLatency(double value, double date)
543 lmm_variable_t var = NULL;
545 lmm_element_t elem = NULL;
547 m_latCurrent = value;
548 while ((var = lmm_get_var_from_cnst(ptask_maxmin_system, getConstraint(), &elem))) {
549 action = static_cast<L07Action*>(lmm_variable_id(var));
550 action->updateBound();
558 L07Action::~L07Action(){
559 free(p_communicationAmount);
560 free(p_computationAmount);
563 void L07Action::updateBound()
565 double lat_current = 0.0;
566 double lat_bound = -1.0;
569 int hostNb = p_edgeList->size();
571 for (i = 0; i < hostNb; i++) {
572 for (j = 0; j < hostNb; j++) {
573 xbt_dynar_t route=NULL;
575 if (p_communicationAmount[i * hostNb + j] > 0) {
577 routing_platf->getRouteAndLatency((*p_edgeList)[i], (*p_edgeList)[j],
580 lat_current = MAX(lat_current, lat * p_communicationAmount[i * hostNb + j]);
584 lat_bound = sg_tcp_gamma / (2.0 * lat_current);
585 XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
586 if ((m_latency == 0.0) && (m_suspended == 0)) {
588 lmm_update_variable_bound(ptask_maxmin_system, getVariable(), lat_bound);
590 lmm_update_variable_bound(ptask_maxmin_system, getVariable(),
591 std::min(m_rate, lat_bound));
595 int L07Action::unref()
599 if (action_hook.is_linked())
600 p_stateSet->erase(p_stateSet->iterator_to(*this));
602 lmm_variable_free(ptask_maxmin_system, getVariable());
609 void L07Action::cancel()
611 setState(SURF_ACTION_FAILED);
615 void L07Action::suspend()
617 XBT_IN("(%p))", this);
618 if (m_suspended != 2) {
620 lmm_update_variable_weight(ptask_maxmin_system, getVariable(), 0.0);
625 void L07Action::resume()
627 XBT_IN("(%p)", this);
628 if (m_suspended != 2) {
629 lmm_update_variable_weight(ptask_maxmin_system, getVariable(), 1.0);
635 bool L07Action::isSuspended()
637 return m_suspended == 1;
640 void L07Action::setMaxDuration(double duration)
641 { /* FIXME: should inherit */
642 XBT_IN("(%p,%g)", this, duration);
643 m_maxDuration = duration;
647 void L07Action::setPriority(double priority)
648 { /* FIXME: should inherit */
649 XBT_IN("(%p,%g)", this, priority);
650 m_priority = priority;
654 double L07Action::getRemains()
656 XBT_IN("(%p)", this);