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 HostL07Model();
45 Model *model = surf_host_model;
46 xbt_dynar_push(all_existing_models, &model);
50 HostL07Model::HostL07Model() : HostModel() {
51 if (!ptask_maxmin_system)
52 ptask_maxmin_system = lmm_system_new(1);
53 surf_host_model = NULL;
54 surf_network_model = new NetworkL07Model(this);
55 surf_cpu_model_pm = new CpuL07Model(this);
57 routing_model_create(surf_network_model->createLink("__loopback__",
60 SURF_RESOURCE_ON, NULL,
61 SURF_LINK_FATPIPE, NULL));
64 HostL07Model::~HostL07Model() {
65 xbt_dict_free(&ptask_parallel_task_link_set);
67 delete surf_cpu_model_pm;
68 delete surf_network_model;
71 if (ptask_maxmin_system) {
72 lmm_system_free(ptask_maxmin_system);
73 ptask_maxmin_system = NULL;
77 double HostL07Model::shareResources(double /*now*/)
81 ActionList *running_actions = getRunningActionSet();
82 double min = this->shareResourcesMaxMin(running_actions,
86 for(ActionList::iterator it(running_actions->begin()), itend(running_actions->end())
87 ; it != itend ; ++it) {
88 action = static_cast<L07Action*>(&*it);
89 if (action->m_latency > 0) {
91 min = action->m_latency;
92 XBT_DEBUG("Updating min (value) with %p (start %f): %f", action,
93 action->getStartTime(), min);
94 } else if (action->m_latency < min) {
95 min = action->m_latency;
96 XBT_DEBUG("Updating min (latency) with %p (start %f): %f", action,
97 action->getStartTime(), min);
102 XBT_DEBUG("min value : %f", min);
107 void HostL07Model::updateActionsState(double /*now*/, double delta) {
110 ActionList *actionSet = getRunningActionSet();
112 for(ActionList::iterator it = actionSet->begin(), itNext = it
113 ; it != actionSet->end()
116 action = static_cast<L07Action*>(&*it);
117 if (action->m_latency > 0) {
118 if (action->m_latency > delta) {
119 double_update(&(action->m_latency), delta, sg_surf_precision);
121 action->m_latency = 0.0;
123 if ((action->m_latency == 0.0) && (action->isSuspended() == 0)) {
124 action->updateBound();
125 lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 1.0);
128 XBT_DEBUG("Action (%p) : remains (%g) updated by %g.",
129 action, action->getRemains(), lmm_variable_getvalue(action->getVariable()) * delta);
130 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
132 if (action->getMaxDuration() != NO_MAX_DURATION)
133 action->updateMaxDuration(delta);
135 XBT_DEBUG("Action (%p) : remains (%g).", action, action->getRemains());
137 /* In the next if cascade, the action can be finished either because:
138 * - The amount of remaining work reached 0
139 * - The max duration was reached
140 * If it's not done, it may have failed.
143 if ((action->getRemains() <= 0) &&
144 (lmm_get_variable_weight(action->getVariable()) > 0)) {
146 action->setState(SURF_ACTION_DONE);
147 } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
148 (action->getMaxDuration() <= 0)) {
150 action->setState(SURF_ACTION_DONE);
152 /* Need to check that none of the model has failed */
153 lmm_constraint_t cnst = NULL;
156 while ((cnst = lmm_get_cnst_from_var(ptask_maxmin_system, action->getVariable(), i++))) {
157 void *constraint_id = lmm_constraint_id(cnst);
159 if (static_cast<Host*>(constraint_id)->getState() == SURF_RESOURCE_OFF) {
160 XBT_DEBUG("Action (%p) Failed!!", action);
162 action->setState(SURF_ACTION_FAILED);
171 Action *HostL07Model::executeParallelTask(int host_nb,
173 double *flops_amount,
174 double *bytes_amount,
177 L07Action *action = new L07Action(this, 1, 0);
181 double latency = 0.0;
183 action->p_edgeList->reserve(host_nb);
184 for (int i = 0; i<host_nb; i++)
185 action->p_edgeList->push_back(sg_host_edge(host_list[i]));
187 if (ptask_parallel_task_link_set == NULL)
188 ptask_parallel_task_link_set = xbt_dict_new_homogeneous(NULL);
190 xbt_dict_reset(ptask_parallel_task_link_set);
192 /* Compute the number of affected resources... */
193 for (int i = 0; i < host_nb; i++) {
194 for (int j = 0; j < host_nb; j++) {
195 xbt_dynar_t route=NULL;
197 if (bytes_amount[i * host_nb + j] > 0) {
203 routing_platf->getRouteAndLatency((*action->p_edgeList)[i], (*action->p_edgeList)[j],
205 latency = MAX(latency, lat);
207 xbt_dynar_foreach(route, cpt, _link) {
208 link = static_cast<LinkL07*>(_link);
209 xbt_dict_set(ptask_parallel_task_link_set, link->getName(), link, NULL);
215 nb_link = xbt_dict_length(ptask_parallel_task_link_set);
216 xbt_dict_reset(ptask_parallel_task_link_set);
218 for (int i = 0; i < host_nb; i++)
219 if (flops_amount[i] > 0)
222 XBT_DEBUG("Creating a parallel task (%p) with %d cpus and %d links.",
223 action, host_nb, nb_link);
224 action->m_suspended = 0; /* valgrind seems to want it despite the calloc... */
225 action->p_computationAmount = flops_amount;
226 action->p_communicationAmount = bytes_amount;
227 action->m_latency = latency;
228 action->m_rate = rate;
230 action->p_variable = lmm_variable_new(ptask_maxmin_system, action, 1.0,
231 (rate > 0 ? rate : -1.0),
234 if (action->m_latency > 0)
235 lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 0.0);
237 for (int i = 0; i < host_nb; i++)
238 lmm_expand(ptask_maxmin_system,
239 sg_host_surfcpu(host_list[i])->getConstraint(),
240 action->getVariable(), flops_amount[i]);
242 for (int i = 0; i < host_nb; i++) {
243 for (int j = 0; j < host_nb; j++) {
246 xbt_dynar_t route=NULL;
247 if (bytes_amount[i * host_nb + j] == 0.0)
250 routing_platf->getRouteAndLatency((*action->p_edgeList)[i], (*action->p_edgeList)[j],
253 xbt_dynar_foreach(route, cpt, _link) {
254 LinkL07 *link = static_cast<LinkL07*>(_link);
255 lmm_expand_add(ptask_maxmin_system, link->getConstraint(),
256 action->getVariable(),
257 bytes_amount[i * host_nb + j]);
262 if (nb_link + nb_host == 0) {
263 action->setCost(1.0);
264 action->setRemains(0.0);
270 Host *HostL07Model::createHost(const char *name,RoutingEdge *netElm, Cpu *cpu)
272 HostL07 *host = new HostL07(this, name, NULL, netElm, cpu);
274 surf_callback_emit(hostCreatedCallbacks, host);
275 xbt_lib_set(host_lib, name, SURF_HOST_LEVEL, host);
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 xbt_dynar_t HostL07Model::getRoute(Host *src, Host *dst)
301 xbt_dynar_t route=NULL;
302 routing_platf->getRouteAndLatency(src->p_netElm, dst->p_netElm, &route, NULL);
306 Cpu *CpuL07Model::createCpu(const char *name, xbt_dynar_t powerPeak,
307 int pstate, double power_scale,
308 tmgr_trace_t power_trace, int core,
309 e_surf_resource_state_t state_initial,
310 tmgr_trace_t state_trace,
311 xbt_dict_t cpu_properties)
313 double power_initial = xbt_dynar_get_as(powerPeak, pstate, double);
314 sg_host_t sg_host = sg_host_by_name(name);
316 CpuL07 *cpu = new CpuL07(this, name, cpu_properties,
317 power_initial, power_scale, power_trace,
318 core, state_initial, state_trace);
319 sg_host_surfcpu_register(sg_host, cpu);
323 Link* NetworkL07Model::createLink(const char *name,
325 tmgr_trace_t bw_trace,
327 tmgr_trace_t lat_trace,
328 e_surf_resource_state_t state_initial,
329 tmgr_trace_t state_trace,
330 e_surf_link_sharing_policy_t policy,
331 xbt_dict_t properties)
333 xbt_assert(!Link::byName(name),
334 "Link '%s' declared several times in the platform file.", name);
336 Link* link = new LinkL07(this, name, properties,
337 bw_initial, bw_trace,
338 lat_initial, lat_trace,
339 state_initial, state_trace,
341 surf_callback_emit(networkLinkCreatedCallbacks, link);
345 void HostL07Model::addTraces()
347 xbt_dict_cursor_t cursor = NULL;
348 char *trace_name, *elm;
350 if (!trace_connect_list_host_avail)
353 /* Connect traces relative to cpu */
354 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
355 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
356 CpuL07 *host = static_cast<CpuL07*>(sg_host_surfcpu(sg_host_by_name(elm)));
358 xbt_assert(host, "Host %s undefined", elm);
359 xbt_assert(trace, "Trace %s undefined", trace_name);
361 host->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, host);
364 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
365 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
366 CpuL07 *host = static_cast<CpuL07*>(sg_host_surfcpu(sg_host_by_name(elm)));
368 xbt_assert(host, "Host %s undefined", elm);
369 xbt_assert(trace, "Trace %s undefined", trace_name);
371 host->p_speedEvent = tmgr_history_add_trace(history, trace, 0.0, 0, host);
374 /* Connect traces relative to network */
375 xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
376 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
377 LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
379 xbt_assert(link, "Link %s undefined", elm);
380 xbt_assert(trace, "Trace %s undefined", trace_name);
382 link->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
385 xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
386 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
387 LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
389 xbt_assert(link, "Link %s undefined", elm);
390 xbt_assert(trace, "Trace %s undefined", trace_name);
392 link->p_bwEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
395 xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
396 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
397 LinkL07 *link = static_cast<LinkL07*>(Link::byName(elm));
399 xbt_assert(link, "Link %s undefined", elm);
400 xbt_assert(trace, "Trace %s undefined", trace_name);
402 link->p_latEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
410 HostL07::HostL07(HostModel *model, const char* name, xbt_dict_t props, RoutingEdge *netElm, Cpu *cpu)
411 : Host(model, name, props, NULL, netElm, cpu)
415 CpuL07::CpuL07(CpuL07Model *model, const char* name, xbt_dict_t props,
416 double speedInitial, double speedScale, tmgr_trace_t speedTrace,
417 int core, e_surf_resource_state_t state_initial, tmgr_trace_t state_trace)
418 : Cpu(model, name, props, lmm_constraint_new(ptask_maxmin_system, this, speedInitial * speedScale),
419 core, speedInitial, speedScale, state_initial)
421 xbt_assert(m_speedScale > 0, "Power has to be >0");
424 p_speedEvent = tmgr_history_add_trace(history, speedTrace, 0.0, 0, this);
429 p_stateEvent = tmgr_history_add_trace(history, state_trace, 0.0, 0, this);
432 LinkL07::LinkL07(NetworkL07Model *model, const char* name, xbt_dict_t props,
434 tmgr_trace_t bw_trace,
436 tmgr_trace_t lat_trace,
437 e_surf_resource_state_t state_initial,
438 tmgr_trace_t state_trace,
439 e_surf_link_sharing_policy_t policy)
440 : Link(model, name, props, lmm_constraint_new(ptask_maxmin_system, this, bw_initial), history, state_trace)
442 m_bwCurrent = bw_initial;
444 p_bwEvent = tmgr_history_add_trace(history, bw_trace, 0.0, 0, this);
446 setState(state_initial);
447 m_latCurrent = lat_initial;
450 p_latEvent = tmgr_history_add_trace(history, lat_trace, 0.0, 0, this);
452 if (policy == SURF_LINK_FATPIPE)
453 lmm_constraint_shared(getConstraint());
456 Action *CpuL07::execute(double size)
458 sg_host_t*host_list = xbt_new0(sg_host_t, 1);
459 double *flops_amount = xbt_new0(double, 1);
460 double *bytes_amount = xbt_new0(double, 1);
462 host_list[0] = sg_host_by_name(getName());
463 flops_amount[0] = size;
465 return static_cast<HostL07Model*>(getModel())->executeParallelTask(1, host_list,
470 Action *CpuL07::sleep(double duration)
472 L07Action *action = NULL;
474 XBT_IN("(%s,%g)", getName(), duration);
476 action = static_cast<L07Action*>(execute(1.0));
477 action->m_maxDuration = duration;
478 action->m_suspended = 2;
479 lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 0.0);
485 bool CpuL07::isUsed(){
486 return lmm_constraint_used(ptask_maxmin_system, getConstraint());
489 bool LinkL07::isUsed(){
490 return lmm_constraint_used(ptask_maxmin_system, getConstraint());
493 void CpuL07::updateState(tmgr_trace_event_t event_type, double value, double /*date*/){
494 XBT_DEBUG("Updating cpu %s (%p) with value %g", getName(), this, value);
495 if (event_type == p_speedEvent) {
496 m_speedScale = value;
497 lmm_update_constraint_bound(ptask_maxmin_system, getConstraint(), m_speedPeak * m_speedScale);
498 if (tmgr_trace_event_free(event_type))
500 } else if (event_type == p_stateEvent) {
502 setState(SURF_RESOURCE_ON);
504 setState(SURF_RESOURCE_OFF);
505 if (tmgr_trace_event_free(event_type))
508 XBT_CRITICAL("Unknown event ! \n");
514 void LinkL07::updateState(tmgr_trace_event_t event_type, double value, double date) {
515 XBT_DEBUG("Updating link %s (%p) with value=%f for date=%g", getName(), this, value, date);
516 if (event_type == p_bwEvent) {
517 updateBandwidth(value, date);
518 if (tmgr_trace_event_free(event_type))
520 } else if (event_type == p_latEvent) {
521 updateLatency(value, date);
522 if (tmgr_trace_event_free(event_type))
524 } else if (event_type == p_stateEvent) {
526 setState(SURF_RESOURCE_ON);
528 setState(SURF_RESOURCE_OFF);
529 if (tmgr_trace_event_free(event_type))
532 XBT_CRITICAL("Unknown event ! \n");
538 e_surf_resource_state_t HostL07::getState() {
539 return p_cpu->getState();
543 double LinkL07::getBandwidth()
548 void LinkL07::updateBandwidth(double value, double date)
551 lmm_update_constraint_bound(ptask_maxmin_system, getConstraint(), m_bwCurrent);
554 void LinkL07::updateLatency(double value, double date)
556 lmm_variable_t var = NULL;
558 lmm_element_t elem = NULL;
560 m_latCurrent = value;
561 while ((var = lmm_get_var_from_cnst(ptask_maxmin_system, getConstraint(), &elem))) {
562 action = static_cast<L07Action*>(lmm_variable_id(var));
563 action->updateBound();
571 L07Action::~L07Action(){
572 free(p_communicationAmount);
573 free(p_computationAmount);
576 void L07Action::updateBound()
578 double lat_current = 0.0;
579 double lat_bound = -1.0;
582 int hostNb = p_edgeList->size();
584 for (i = 0; i < hostNb; i++) {
585 for (j = 0; j < hostNb; j++) {
586 xbt_dynar_t route=NULL;
588 if (p_communicationAmount[i * hostNb + j] > 0) {
590 routing_platf->getRouteAndLatency((*p_edgeList)[i], (*p_edgeList)[j],
593 lat_current = MAX(lat_current, lat * p_communicationAmount[i * hostNb + j]);
597 lat_bound = sg_tcp_gamma / (2.0 * lat_current);
598 XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
599 if ((m_latency == 0.0) && (m_suspended == 0)) {
601 lmm_update_variable_bound(ptask_maxmin_system, getVariable(), lat_bound);
603 lmm_update_variable_bound(ptask_maxmin_system, getVariable(),
604 std::min(m_rate, lat_bound));
608 int L07Action::unref()
612 if (action_hook.is_linked())
613 p_stateSet->erase(p_stateSet->iterator_to(*this));
615 lmm_variable_free(ptask_maxmin_system, getVariable());
622 void L07Action::cancel()
624 setState(SURF_ACTION_FAILED);
628 void L07Action::suspend()
630 XBT_IN("(%p))", this);
631 if (m_suspended != 2) {
633 lmm_update_variable_weight(ptask_maxmin_system, getVariable(), 0.0);
638 void L07Action::resume()
640 XBT_IN("(%p)", this);
641 if (m_suspended != 2) {
642 lmm_update_variable_weight(ptask_maxmin_system, getVariable(), 1.0);
648 bool L07Action::isSuspended()
650 return m_suspended == 1;
653 void L07Action::setMaxDuration(double duration)
654 { /* FIXME: should inherit */
655 XBT_IN("(%p,%g)", this, duration);
656 m_maxDuration = duration;
660 void L07Action::setPriority(double priority)
661 { /* FIXME: should inherit */
662 XBT_IN("(%p,%g)", this, priority);
663 m_priority = priority;
667 double L07Action::getRemains()
669 XBT_IN("(%p)", this);