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 static int ptask_host_count = 0;
20 static xbt_dict_t ptask_parallel_task_link_set = NULL;
21 lmm_system_t ptask_maxmin_system = NULL;
24 /**************************************/
25 /*** Resource Creation & Destruction **/
26 /**************************************/
28 static void ptask_netlink_parse_init(sg_platf_link_cbarg_t link)
30 netlink_parse_init(link);
31 current_property_set = NULL;
34 void surf_host_model_init_ptask_L07(void)
36 XBT_INFO("Switching to the L07 model to handle parallel tasks.");
37 xbt_assert(!surf_cpu_model_pm, "CPU model type already defined");
38 xbt_assert(!surf_network_model, "network model type already defined");
40 // Define the callbacks to parse the XML
41 sg_platf_link_add_cb(ptask_netlink_parse_init);
42 sg_platf_postparse_add_cb(host_add_traces);
44 surf_host_model = new simgrid::surf::HostL07Model();
45 simgrid::surf::Model *model = surf_host_model;
46 xbt_dynar_push(all_existing_models, &model);
53 HostL07Model::HostL07Model() : HostModel() {
54 if (!ptask_maxmin_system)
55 ptask_maxmin_system = lmm_system_new(1);
56 surf_host_model = NULL;
57 surf_network_model = new NetworkL07Model(this);
58 surf_cpu_model_pm = new CpuL07Model(this);
60 routing_model_create(surf_network_model->createLink("__loopback__",
63 SURF_RESOURCE_ON, NULL,
64 SURF_LINK_FATPIPE, NULL));
67 HostL07Model::~HostL07Model() {
68 xbt_dict_free(&ptask_parallel_task_link_set);
70 delete surf_cpu_model_pm;
71 delete surf_network_model;
74 if (ptask_maxmin_system) {
75 lmm_system_free(ptask_maxmin_system);
76 ptask_maxmin_system = NULL;
80 double HostL07Model::shareResources(double /*now*/)
84 ActionList *running_actions = getRunningActionSet();
85 double min = this->shareResourcesMaxMin(running_actions,
89 for(ActionList::iterator it(running_actions->begin()), itend(running_actions->end())
90 ; it != itend ; ++it) {
91 action = static_cast<L07Action*>(&*it);
92 if (action->m_latency > 0) {
94 min = action->m_latency;
95 XBT_DEBUG("Updating min (value) with %p (start %f): %f", action,
96 action->getStartTime(), min);
97 } else if (action->m_latency < min) {
98 min = action->m_latency;
99 XBT_DEBUG("Updating min (latency) with %p (start %f): %f", action,
100 action->getStartTime(), min);
105 XBT_DEBUG("min value : %f", min);
110 void HostL07Model::updateActionsState(double /*now*/, double delta) {
113 ActionList *actionSet = getRunningActionSet();
115 for(ActionList::iterator it = actionSet->begin(), itNext = it
116 ; it != actionSet->end()
119 action = static_cast<L07Action*>(&*it);
120 if (action->m_latency > 0) {
121 if (action->m_latency > delta) {
122 double_update(&(action->m_latency), delta, 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).", action, action->getRemains());
140 /* In the next if cascade, the action can be finished either because:
141 * - The amount of remaining work reached 0
142 * - The max duration was reached
143 * If it's not done, it may have failed.
146 if ((action->getRemains() <= 0) &&
147 (lmm_get_variable_weight(action->getVariable()) > 0)) {
149 action->setState(SURF_ACTION_DONE);
150 } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
151 (action->getMaxDuration() <= 0)) {
153 action->setState(SURF_ACTION_DONE);
155 /* Need to check that none of the model has failed */
156 lmm_constraint_t cnst = NULL;
159 while ((cnst = lmm_get_cnst_from_var(ptask_maxmin_system, action->getVariable(), i++))) {
160 void *constraint_id = lmm_constraint_id(cnst);
162 if (static_cast<Host*>(constraint_id)->getState() == SURF_RESOURCE_OFF) {
163 XBT_DEBUG("Action (%p) Failed!!", action);
165 action->setState(SURF_ACTION_FAILED);
174 Action *HostL07Model::executeParallelTask(int host_nb,
176 double *flops_amount,
177 double *bytes_amount,
180 L07Action *action = new L07Action(this, 1, 0);
184 double latency = 0.0;
186 action->p_edgeList->reserve(host_nb);
187 for (int i = 0; i<host_nb; i++)
188 action->p_edgeList->push_back(sg_host_edge(host_list[i]));
190 if (ptask_parallel_task_link_set == NULL)
191 ptask_parallel_task_link_set = xbt_dict_new_homogeneous(NULL);
193 xbt_dict_reset(ptask_parallel_task_link_set);
195 /* Compute the number of affected resources... */
196 for (int i = 0; i < host_nb; i++) {
197 for (int j = 0; j < host_nb; j++) {
198 xbt_dynar_t route=NULL;
200 if (bytes_amount[i * host_nb + j] > 0) {
206 routing_platf->getRouteAndLatency((*action->p_edgeList)[i], (*action->p_edgeList)[j],
208 latency = MAX(latency, lat);
210 xbt_dynar_foreach(route, cpt, _link) {
211 link = static_cast<LinkL07*>(_link);
212 xbt_dict_set(ptask_parallel_task_link_set, link->getName(), link, NULL);
218 nb_link = xbt_dict_length(ptask_parallel_task_link_set);
219 xbt_dict_reset(ptask_parallel_task_link_set);
221 for (int i = 0; i < host_nb; i++)
222 if (flops_amount[i] > 0)
225 XBT_DEBUG("Creating a parallel task (%p) with %d cpus and %d links.",
226 action, host_nb, nb_link);
227 action->m_suspended = 0; /* valgrind seems to want it despite the calloc... */
228 action->p_computationAmount = flops_amount;
229 action->p_communicationAmount = bytes_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 (rate > 0 ? rate : -1.0),
237 if (action->m_latency > 0)
238 lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 0.0);
240 for (int i = 0; i < host_nb; i++)
241 lmm_expand(ptask_maxmin_system,
242 sg_host_surfcpu(host_list[i])->getConstraint(),
243 action->getVariable(), flops_amount[i]);
245 for (int i = 0; i < host_nb; i++) {
246 for (int j = 0; j < host_nb; j++) {
249 xbt_dynar_t route=NULL;
250 if (bytes_amount[i * host_nb + j] == 0.0)
253 routing_platf->getRouteAndLatency((*action->p_edgeList)[i], (*action->p_edgeList)[j],
256 xbt_dynar_foreach(route, cpt, _link) {
257 LinkL07 *link = static_cast<LinkL07*>(_link);
258 lmm_expand_add(ptask_maxmin_system, link->getConstraint(),
259 action->getVariable(),
260 bytes_amount[i * host_nb + j]);
265 if (nb_link + nb_host == 0) {
266 action->setCost(1.0);
267 action->setRemains(0.0);
273 Host *HostL07Model::createHost(const char *name,RoutingEdge *netElm, Cpu *cpu, xbt_dict_t props)
275 return new simgrid::surf::Host(this, name, props,
276 (xbt_dynar_t)xbt_lib_get_or_null(storage_lib, name, ROUTING_STORAGE_HOST_LEVEL),
280 Action *NetworkL07Model::communicate(RoutingEdge *src, RoutingEdge *dst,
281 double size, double rate)
283 sg_host_t*host_list = xbt_new0(sg_host_t, 2);
284 double *flops_amount = xbt_new0(double, 2);
285 double *bytes_amount = xbt_new0(double, 4);
288 host_list[0] = sg_host_by_name(src->getName());
289 host_list[1] = sg_host_by_name(dst->getName());
290 bytes_amount[1] = size;
292 res = p_hostModel->executeParallelTask(2, host_list,
299 Cpu *CpuL07Model::createCpu(const char *name, xbt_dynar_t powerPeak,
300 int pstate, double power_scale,
301 tmgr_trace_t power_trace, int core,
302 e_surf_resource_state_t state_initial,
303 tmgr_trace_t state_trace)
305 double power_initial = xbt_dynar_get_as(powerPeak, pstate, double);
306 CpuL07 *cpu = new CpuL07(this, name, power_initial, power_scale, power_trace,
307 core, state_initial, state_trace);
311 Link* NetworkL07Model::createLink(const char *name,
313 tmgr_trace_t bw_trace,
315 tmgr_trace_t lat_trace,
316 e_surf_resource_state_t state_initial,
317 tmgr_trace_t state_trace,
318 e_surf_link_sharing_policy_t policy,
319 xbt_dict_t properties)
321 xbt_assert(!Link::byName(name),
322 "Link '%s' declared several times in the platform file.", name);
324 Link* link = new LinkL07(this, name, properties,
325 bw_initial, bw_trace,
326 lat_initial, lat_trace,
327 state_initial, state_trace,
329 Link::onCreation(link);
333 void HostL07Model::addTraces()
335 xbt_dict_cursor_t cursor = NULL;
336 char *trace_name, *elm;
338 if (!trace_connect_list_host_avail)
341 /* Connect traces relative to cpu */
342 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
343 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
344 CpuL07 *host = static_cast<CpuL07*>(sg_host_surfcpu(sg_host_by_name(elm)));
346 xbt_assert(host, "Host %s undefined", elm);
347 xbt_assert(trace, "Trace %s undefined", trace_name);
349 host->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, host);
352 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
353 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
354 CpuL07 *host = static_cast<CpuL07*>(sg_host_surfcpu(sg_host_by_name(elm)));
356 xbt_assert(host, "Host %s undefined", elm);
357 xbt_assert(trace, "Trace %s undefined", trace_name);
359 host->p_speedEvent = tmgr_history_add_trace(history, trace, 0.0, 0, host);
362 /* Connect traces relative to network */
363 xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
364 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
365 LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
367 xbt_assert(link, "Link %s undefined", elm);
368 xbt_assert(trace, "Trace %s undefined", trace_name);
370 link->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
373 xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
374 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
375 LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
377 xbt_assert(link, "Link %s undefined", elm);
378 xbt_assert(trace, "Trace %s undefined", trace_name);
380 link->p_bwEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
383 xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
384 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
385 LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
387 xbt_assert(link, "Link %s undefined", elm);
388 xbt_assert(trace, "Trace %s undefined", trace_name);
390 link->p_latEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
397 CpuL07::CpuL07(CpuL07Model *model, const char* name,
398 double speedInitial, double speedScale, tmgr_trace_t speedTrace,
399 int core, e_surf_resource_state_t state_initial, tmgr_trace_t state_trace)
400 : Cpu(model, name, lmm_constraint_new(ptask_maxmin_system, this, speedInitial * speedScale),
401 core, speedInitial, speedScale, state_initial)
403 xbt_assert(m_speedScale > 0, "Power has to be >0");
406 p_speedEvent = tmgr_history_add_trace(history, speedTrace, 0.0, 0, this);
411 p_stateEvent = tmgr_history_add_trace(history, state_trace, 0.0, 0, this);
418 LinkL07::LinkL07(NetworkL07Model *model, const char* name, xbt_dict_t props,
420 tmgr_trace_t bw_trace,
422 tmgr_trace_t lat_trace,
423 e_surf_resource_state_t state_initial,
424 tmgr_trace_t state_trace,
425 e_surf_link_sharing_policy_t policy)
426 : Link(model, name, props, lmm_constraint_new(ptask_maxmin_system, this, bw_initial), history, state_trace)
428 m_bwCurrent = bw_initial;
430 p_bwEvent = tmgr_history_add_trace(history, bw_trace, 0.0, 0, this);
432 setState(state_initial);
433 m_latCurrent = lat_initial;
436 p_latEvent = tmgr_history_add_trace(history, lat_trace, 0.0, 0, this);
438 if (policy == SURF_LINK_FATPIPE)
439 lmm_constraint_shared(getConstraint());
442 Action *CpuL07::execute(double size)
444 sg_host_t*host_list = xbt_new0(sg_host_t, 1);
445 double *flops_amount = xbt_new0(double, 1);
446 double *bytes_amount = xbt_new0(double, 1);
448 host_list[0] = getHost();
449 flops_amount[0] = size;
451 return static_cast<CpuL07Model*>(getModel())
453 ->executeParallelTask( 1, host_list, flops_amount, bytes_amount, -1);
456 Action *CpuL07::sleep(double duration)
458 L07Action *action = NULL;
460 XBT_IN("(%s,%g)", getName(), duration);
462 action = static_cast<L07Action*>(execute(1.0));
463 action->m_maxDuration = duration;
464 action->m_suspended = 2;
465 lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 0.0);
471 bool CpuL07::isUsed(){
472 return lmm_constraint_used(ptask_maxmin_system, getConstraint());
475 bool LinkL07::isUsed(){
476 return lmm_constraint_used(ptask_maxmin_system, getConstraint());
479 void CpuL07::updateState(tmgr_trace_event_t event_type, double value, double /*date*/){
480 XBT_DEBUG("Updating cpu %s (%p) with value %g", getName(), this, value);
481 if (event_type == p_speedEvent) {
482 m_speedScale = value;
483 lmm_update_constraint_bound(ptask_maxmin_system, getConstraint(), m_speedPeak * m_speedScale);
484 if (tmgr_trace_event_free(event_type))
486 } else if (event_type == p_stateEvent) {
488 setState(SURF_RESOURCE_ON);
490 setState(SURF_RESOURCE_OFF);
491 if (tmgr_trace_event_free(event_type))
494 XBT_CRITICAL("Unknown event ! \n");
500 void LinkL07::updateState(tmgr_trace_event_t event_type, double value, double date) {
501 XBT_DEBUG("Updating link %s (%p) with value=%f for date=%g", getName(), this, value, date);
502 if (event_type == p_bwEvent) {
503 updateBandwidth(value, date);
504 if (tmgr_trace_event_free(event_type))
506 } else if (event_type == p_latEvent) {
507 updateLatency(value, date);
508 if (tmgr_trace_event_free(event_type))
510 } else if (event_type == p_stateEvent) {
512 setState(SURF_RESOURCE_ON);
514 setState(SURF_RESOURCE_OFF);
515 if (tmgr_trace_event_free(event_type))
518 XBT_CRITICAL("Unknown event ! \n");
524 double LinkL07::getBandwidth()
529 void LinkL07::updateBandwidth(double value, double date)
532 lmm_update_constraint_bound(ptask_maxmin_system, getConstraint(), m_bwCurrent);
535 void LinkL07::updateLatency(double value, double date)
537 lmm_variable_t var = NULL;
539 lmm_element_t elem = NULL;
541 m_latCurrent = value;
542 while ((var = lmm_get_var_from_cnst(ptask_maxmin_system, getConstraint(), &elem))) {
543 action = static_cast<L07Action*>(lmm_variable_id(var));
544 action->updateBound();
552 L07Action::~L07Action(){
553 free(p_communicationAmount);
554 free(p_computationAmount);
557 void L07Action::updateBound()
559 double lat_current = 0.0;
560 double lat_bound = -1.0;
563 int hostNb = p_edgeList->size();
565 for (i = 0; i < hostNb; i++) {
566 for (j = 0; j < hostNb; j++) {
567 xbt_dynar_t route=NULL;
569 if (p_communicationAmount[i * hostNb + j] > 0) {
571 routing_platf->getRouteAndLatency((*p_edgeList)[i], (*p_edgeList)[j],
574 lat_current = MAX(lat_current, lat * p_communicationAmount[i * hostNb + j]);
578 lat_bound = sg_tcp_gamma / (2.0 * lat_current);
579 XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
580 if ((m_latency == 0.0) && (m_suspended == 0)) {
582 lmm_update_variable_bound(ptask_maxmin_system, getVariable(), lat_bound);
584 lmm_update_variable_bound(ptask_maxmin_system, getVariable(),
585 std::min(m_rate, lat_bound));
589 int L07Action::unref()
593 if (action_hook.is_linked())
594 p_stateSet->erase(p_stateSet->iterator_to(*this));
596 lmm_variable_free(ptask_maxmin_system, getVariable());
603 void L07Action::cancel()
605 setState(SURF_ACTION_FAILED);
609 void L07Action::suspend()
611 XBT_IN("(%p))", this);
612 if (m_suspended != 2) {
614 lmm_update_variable_weight(ptask_maxmin_system, getVariable(), 0.0);
619 void L07Action::resume()
621 XBT_IN("(%p)", this);
622 if (m_suspended != 2) {
623 lmm_update_variable_weight(ptask_maxmin_system, getVariable(), 1.0);
629 bool L07Action::isSuspended()
631 return m_suspended == 1;
634 void L07Action::setMaxDuration(double duration)
635 { /* FIXME: should inherit */
636 XBT_IN("(%p,%g)", this, duration);
637 m_maxDuration = duration;
641 void L07Action::setPriority(double priority)
642 { /* FIXME: should inherit */
643 XBT_IN("(%p,%g)", this, priority);
644 m_priority = priority;
648 double L07Action::getRemains()
650 XBT_IN("(%p)", this);