Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
new flex run
[simgrid.git] / src / surf / cpu_ti.cpp
index b7e0362..5c083d5 100644 (file)
@@ -1,3 +1,9 @@
+/* Copyright (c) 2013-2014. The SimGrid Team.
+ * All rights reserved.                                                     */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
 #include "cpu_ti.hpp"
 #include "trace_mgr_private.h"
 #include "xbt/heap.h"
@@ -113,7 +119,7 @@ double CpuTiTgmr::integrate(double a, double b)
 
 /**
  * \brief Auxiliary function to calculate the integral between a and b.
- *     It simply calculates the integral at point a and b and returns the difference 
+ *     It simply calculates the integral at point a and b and returns the difference
  *   between them.
  * \param trace    Trace structure
  * \param a        Initial point
@@ -142,7 +148,7 @@ double CpuTiTrace::integrateSimplePoint(double a)
       ("a %f ind %d integral %f ind + 1 %f ind %f time +1 %f time %f",
        a, ind, integral, p_integral[ind + 1], p_integral[ind],
        p_timePoints[ind + 1], p_timePoints[ind]);
-  double_update(&a_aux, p_timePoints[ind]);
+  double_update(&a_aux, p_timePoints[ind], sg_maxmin_precision*sg_surf_precision);
   if (a_aux > 0)
     integral +=
         ((p_integral[ind + 1] -
@@ -251,7 +257,7 @@ double CpuTiTgmr::solveSomewhatSimple(double a, double amount)
  *  It returns the date when the requested amount of flops is available
  * \param trace    Trace structure
  * \param a        Initial point
- * \param amount  Amount of flops 
+ * \param amount  Amount of flops
  * \return The date when amount is available.
 */
 double CpuTiTrace::solveSimple(double a, double amount)
@@ -312,7 +318,7 @@ CpuTiTgmr::CpuTiTgmr(tmgr_trace_t power_trace, double value)
   if (!power_trace) {
     m_type = TRACE_FIXED;
     m_value = value;
-    XBT_DEBUG("No availability trace. Constant value = %lf", value);
+    XBT_DEBUG("No availability trace. Constant value = %f", value);
     return;
   }
 
@@ -335,13 +341,13 @@ CpuTiTgmr::CpuTiTgmr(tmgr_trace_t power_trace, double value)
   m_lastTime = total_time;
   m_total = p_trace->integrateSimple(0, total_time);
 
-  XBT_DEBUG("Total integral %lf, last_time %lf ",
+  XBT_DEBUG("Total integral %f, last_time %f ",
             m_total, m_lastTime);
 }
 
 /**
  * \brief Binary search in array.
- *  It returns the first point of the interval in which "a" is. 
+ *  It returns the first point of the interval in which "a" is.
  * \param array    Array
  * \param a        Value to search
  * \param low     Low bound to search in array
@@ -373,18 +379,10 @@ int CpuTiTrace::binarySearch(double *array, double a, int low, int high)
  * CallBacks *
  *************/
 
-static void parse_cpu_ti_init(sg_platf_host_cbarg_t host){
-  ((CpuTiModelPtr)surf_cpu_model_pm)->parseInit(host);
-}
-
-static void add_traces_cpu_ti(){
-  surf_cpu_model_pm->addTraces();
-}
-
 static void cpu_ti_define_callbacks()
 {
-  sg_platf_host_add_cb(parse_cpu_ti_init);
-  sg_platf_postparse_add_cb(add_traces_cpu_ti);
+  sg_platf_host_add_cb(parse_cpu_init);
+  sg_platf_postparse_add_cb(add_traces_cpu);
 }
 
 /*********
@@ -408,11 +406,9 @@ void surf_cpu_model_init_ti()
 
 CpuTiModel::CpuTiModel() : CpuModel("cpu_ti")
 {
-  ActionPtr action = NULL;
   CpuTiPtr cpu = NULL;
 
-  p_runningActionSetThatDoesNotNeedBeingChecked =
-      xbt_swag_new(xbt_swag_offset(*action, p_stateHookup));
+  p_runningActionSetThatDoesNotNeedBeingChecked = new ActionList();
 
   p_modifiedCpu =
       xbt_swag_new(xbt_swag_offset(*cpu, p_modifiedCpuHookup));
@@ -426,25 +422,12 @@ CpuTiModel::~CpuTiModel()
 {
   surf_cpu_model_pm = NULL;
 
-  xbt_swag_free(p_runningActionSetThatDoesNotNeedBeingChecked);
+  delete p_runningActionSetThatDoesNotNeedBeingChecked;
   xbt_swag_free(p_modifiedCpu);
   xbt_heap_free(p_tiActionHeap);
 }
 
-void CpuTiModel::parseInit(sg_platf_host_cbarg_t host)
-{
-  createResource(host->id,
-        host->power_peak,
-        host->pstate,
-        host->power_scale,
-        host->power_trace,
-        host->core_amount,
-        host->initial_state,
-        host->state_trace,
-        host->properties);
-}
-
-CpuTiPtr CpuTiModel::createResource(const char *name,
+CpuPtr CpuTiModel::createResource(const char *name,
                               xbt_dynar_t powerPeak,
                               int pstate,
                            double powerScale,
@@ -464,11 +447,6 @@ CpuTiPtr CpuTiModel::createResource(const char *name,
   return cpu;
 }
 
-CpuTiActionPtr CpuTiModel::createAction(double /*cost*/, bool /*failed*/)
-{
-  return NULL;//new CpuTiAction(this, cost, failed);
-}
-
 double CpuTiModel::shareResources(double now)
 {
   void *_cpu, *_cpu_next;
@@ -532,7 +510,7 @@ void CpuTiModel::addTraces()
 
   xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
-    CpuTiPtr cpu = dynamic_cast<CpuTiPtr>(static_cast<ResourcePtr>(surf_cpu_resource_priv(surf_cpu_resource_by_name(elm))));
+    CpuTiPtr cpu = static_cast<CpuTiPtr>(surf_cpu_resource_priv(surf_cpu_resource_by_name(elm)));
 
     xbt_assert(cpu, "Host %s undefined", elm);
     xbt_assert(trace, "Trace %s undefined", trace_name);
@@ -566,16 +544,16 @@ CpuTi::CpuTi(CpuTiModelPtr model, const char *name, xbt_dynar_t powerPeak,
         int pstate, double powerScale, tmgr_trace_t powerTrace, int core,
         e_surf_resource_state_t stateInitial, tmgr_trace_t stateTrace,
        xbt_dict_t properties)
-: Resource(model, name, properties)
-, Cpu(core, 0, powerScale) {
+: Cpu(model, name, properties, core, 0, powerScale)
+{
   p_powerEvent = NULL;
-  m_stateCurrent = stateInitial;
+  setState(stateInitial);
   m_powerScale = powerScale;
   m_core = core;
-  tmgr_trace_t empty_trace;            
-  s_tmgr_event_t val;          
+  tmgr_trace_t empty_trace;
+  s_tmgr_event_t val;
   xbt_assert(core==1,"Multi-core not handled with this model yet");
-  XBT_DEBUG("power scale %lf", powerScale);
+  XBT_DEBUG("power scale %f", powerScale);
   p_availTrace = new CpuTiTgmr(powerTrace, powerScale);
 
   CpuTiActionPtr action = NULL;
@@ -594,7 +572,7 @@ CpuTi::CpuTi(CpuTiModelPtr model, const char *name, xbt_dynar_t powerPeak,
   if (stateTrace)
     p_stateEvent = tmgr_history_add_trace(history, stateTrace, 0.0, 0, static_cast<ResourcePtr>(this));
   if (powerTrace && xbt_dynar_length(powerTrace->s_list.event_list) > 1) {
-    // add a fake trace event if periodicity == 0 
+    // add a fake trace event if periodicity == 0
     xbt_dynar_get_cpy(powerTrace->s_list.event_list,
                       xbt_dynar_length(powerTrace->s_list.event_list) - 1, &val);
     if (val.delta == 0) {
@@ -622,7 +600,7 @@ void CpuTi::updateState(tmgr_trace_event_t event_type,
     CpuTiTgmrPtr trace;
     s_tmgr_event_t val;
 
-    XBT_DEBUG("Finish trace date: %f value %lf date %f", surf_get_clock(),
+    XBT_DEBUG("Finish trace date: %f value %f date %f", surf_get_clock(),
            value, date);
     /* update remaining of actions and put in modified cpu swag */
     updateRemainingAmount(date);
@@ -645,11 +623,11 @@ void CpuTi::updateState(tmgr_trace_event_t event_type,
 
   } else if (event_type == p_stateEvent) {
     if (value > 0) {
-      if(m_stateCurrent == SURF_RESOURCE_OFF)
+      if(getState() == SURF_RESOURCE_OFF)
         xbt_dynar_push_as(host_that_restart, char*, (char *)getName());
-      m_stateCurrent = SURF_RESOURCE_ON;
+      setState(SURF_RESOURCE_ON);
     } else {
-      m_stateCurrent = SURF_RESOURCE_OFF;
+      setState(SURF_RESOURCE_OFF);
 
       /* put all action running on cpu to failed */
       xbt_swag_foreach(_action, p_actionSet) {
@@ -824,7 +802,7 @@ void CpuTi::updateRemainingAmount(double now)
 CpuActionPtr CpuTi::execute(double size)
 {
   XBT_IN("(%s,%g)", getName(), size);
-  CpuTiActionPtr action = new CpuTiAction(static_cast<CpuTiModelPtr>(getModel()), size, m_stateCurrent != SURF_RESOURCE_ON, this);
+  CpuTiActionPtr action = new CpuTiAction(static_cast<CpuTiModelPtr>(getModel()), size, getState() != SURF_RESOURCE_ON, this);
 
   xbt_swag_insert(action, p_actionSet);
 
@@ -836,19 +814,19 @@ CpuActionPtr CpuTi::execute(double size)
 CpuActionPtr CpuTi::sleep(double duration)
 {
   if (duration > 0)
-    duration = MAX(duration, MAXMIN_PRECISION);
+    duration = MAX(duration, sg_surf_precision);
 
   XBT_IN("(%s,%g)", getName(), duration);
-  CpuTiActionPtr action = new CpuTiAction(static_cast<CpuTiModelPtr>(getModel()), 1.0, m_stateCurrent != SURF_RESOURCE_ON, this);
+  CpuTiActionPtr action = new CpuTiAction(static_cast<CpuTiModelPtr>(getModel()), 1.0, getState() != SURF_RESOURCE_ON, this);
 
   action->m_maxDuration = duration;
   action->m_suspended = 2;
   if (duration == NO_MAX_DURATION) {
    /* Move to the *end* of the corresponding action set. This convention
       is used to speed up update_resource_state  */
-    xbt_swag_remove(static_cast<ActionPtr>(action), action->getStateSet());
+       action->getStateSet()->erase(action->getStateSet()->iterator_to(*action));
     action->p_stateSet = reinterpret_cast<CpuTiModelPtr>(getModel())->p_runningActionSetThatDoesNotNeedBeingChecked;
-    xbt_swag_insert(static_cast<ActionPtr>(action), action->getStateSet());
+    action->getStateSet()->push_back(*static_cast<ActionPtr>(action));
   }
 
   xbt_swag_insert(action, p_actionSet);
@@ -868,8 +846,7 @@ static void cpu_ti_action_update_index_heap(void *action, int i)
 
 CpuTiAction::CpuTiAction(CpuTiModelPtr model_, double cost, bool failed,
                                 CpuTiPtr cpu)
- : Action(model_, cost, failed)
- , CpuAction(model_, cost, failed)
+ : CpuAction(model_, cost, failed)
 {
   p_cpuListHookup.next = 0;
   p_cpuListHookup.prev = 0;
@@ -888,7 +865,7 @@ void CpuTiAction::updateIndexHeap(int i)
 
 void CpuTiAction::setState(e_surf_action_state_t state)
 {
-  Action::setState(state);
+  CpuAction::setState(state);
   xbt_swag_insert(p_cpu, reinterpret_cast<CpuTiModelPtr>(getModel())->p_modifiedCpu);
 }
 
@@ -896,7 +873,8 @@ int CpuTiAction::unref()
 {
   m_refcount--;
   if (!m_refcount) {
-    xbt_swag_remove(static_cast<ActionPtr>(this), getStateSet());
+       if (actionHook::is_linked())
+         getStateSet()->erase(getStateSet()->iterator_to(*this));
     /* remove from action_set */
     xbt_swag_remove(this, p_cpu->p_actionSet);
     /* remove from heap */