Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Replace xbt_heap with boost::heap for surf actions.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Sun, 12 Nov 2017 21:37:43 +0000 (22:37 +0100)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Mon, 13 Nov 2017 13:29:22 +0000 (14:29 +0100)
Use a stable order to ensure reproducibility.

Note: it's not fully tested if pairing_heap is the best choice among
availables heaps (binomial, fibonacci, ...). It's easy to change if
needed.

src/surf/cpu_cas01.cpp
src/surf/cpu_ti.cpp
src/surf/network_cm02.cpp
src/surf/surf_interface.cpp
src/surf/surf_interface.hpp

index aa47bff..e9655c4 100644 (file)
@@ -210,7 +210,6 @@ CpuCas01Action::CpuCas01Action(Model* model, double cost, bool failed, double sp
     , requestedCore_(requestedCore)
 {
   if (model->getUpdateMechanism() == UM_LAZY) {
-    updateIndexHeap(-1);
     refreshLastUpdate();
     setLastValue(0.0);
   }
index a4c47a5..0611e46 100644 (file)
@@ -646,7 +646,6 @@ CpuTiAction::CpuTiAction(CpuTiModel *model_, double cost, bool failed, CpuTi *cp
  : CpuAction(model_, cost, failed)
  , cpu_(cpu)
 {
-  updateIndexHeap(-1);
   cpu_->modified(true);
 }
 
index e1896d3..bd5b18a 100644 (file)
@@ -289,7 +289,6 @@ Action* NetworkCm02Model::communicate(s4u::Host* src, s4u::Host* dst, double siz
   action->latency_ = latency;
   action->rate_ = rate;
   if (getUpdateMechanism() == UM_LAZY) {
-    action->updateIndexHeap(-1);
     action->refreshLastUpdate();
   }
 
index 01318ca..632776c 100644 (file)
@@ -365,20 +365,25 @@ Model::Model()
   doneActionSet_ = new ActionList();
 
   modifiedSet_ = nullptr;
-  actionHeap_      = xbt_heap_new(8, nullptr);
-  xbt_heap_set_update_callback(actionHeap_, surf_action_lmm_update_index_heap);
   updateMechanism_ = UM_UNDEFINED;
   selectiveUpdate_ = 0;
 }
 
 Model::~Model(){
-  xbt_heap_free(actionHeap_);
   delete readyActionSet_;
   delete runningActionSet_;
   delete failedActionSet_;
   delete doneActionSet_;
 }
 
+Action* Model::actionHeapPop()
+{
+  Action* action = actionHeap_.top().second;
+  actionHeap_.pop();
+  action->clearHeapHandle();
+  return action;
+}
+
 double Model::nextOccuringEvent(double now)
 {
   //FIXME: set the good function once and for all
@@ -574,11 +579,6 @@ const char *surf_action_state_names[6] = {
   "SURF_ACTION_NOT_IN_THE_SYSTEM"
 };
 
-/* added to manage the communication action's heap */
-void surf_action_lmm_update_index_heap(void *action, int i) {
-  static_cast<simgrid::surf::Action*>(action)->updateIndexHeap(i);
-}
-
 namespace simgrid {
 namespace surf {
 
@@ -771,34 +771,31 @@ bool Action::isSuspended()
  * LATENCY = this is a heap entry to warn us when the latency is payed
  * MAX_DURATION =this is a heap entry to warn us when the max_duration limit is reached
  */
-void Action::heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat)
+void Action::heapInsert(heap_type& heap, double key, enum heap_action_type hat)
 {
   hat_ = hat;
-  xbt_heap_push(heap, this, key);
+  heapHandle_ = heap.emplace(key, this);
 }
 
-void Action::heapRemove(xbt_heap_t heap)
+void Action::heapRemove(heap_type& heap)
 {
   hat_ = NOTSET;
-  if (indexHeap_ >= 0) {
-    xbt_heap_remove(heap, indexHeap_);
+  if (heapHandle_) {
+    heap.erase(*heapHandle_);
+    clearHeapHandle();
   }
 }
 
-void Action::heapUpdate(xbt_heap_t heap, double key, enum heap_action_type hat)
+void Action::heapUpdate(heap_type& heap, double key, enum heap_action_type hat)
 {
   hat_ = hat;
-  if (indexHeap_ >= 0) {
-    xbt_heap_update(heap, indexHeap_, key);
-  }else{
-    xbt_heap_push(heap, this, key);
+  if (heapHandle_) {
+    heap.update(*heapHandle_, std::make_pair(key, this));
+  } else {
+    heapHandle_ = heap.emplace(key, this);
   }
 }
 
-void Action::updateIndexHeap(int i) {
-  indexHeap_ = i;
-}
-
 double Action::getRemains()
 {
   XBT_IN("(%p)", this);
index 14c92a0..0644588 100644 (file)
 
 #include "src/surf/surf_private.hpp"
 #include "surf/surf.hpp"
-#include "xbt/heap.h"
 #include "xbt/str.h"
 
+#include <boost/heap/pairing_heap.hpp>
 #include <boost/intrusive/list.hpp>
+#include <boost/optional.hpp>
 #include <set>
 #include <string>
 #include <unordered_map>
@@ -66,8 +67,6 @@ enum heap_action_type{
  * Action *
  **********/
 
-XBT_PRIVATE void surf_action_lmm_update_index_heap(void *action, int i);
-
 /** \ingroup SURF_models
  *  \brief List of initialized models
  */
@@ -76,6 +75,14 @@ XBT_PUBLIC_DATA(std::vector<surf_model_t>*) all_existing_models;
 namespace simgrid {
 namespace surf {
 
+typedef std::pair<double, simgrid::surf::Action*> heap_element_type;
+struct heap_element_compare {
+  bool operator()(const heap_element_type& a, const heap_element_type& b) const { return a.first > b.first; }
+};
+typedef boost::heap::pairing_heap<heap_element_type, boost::heap::constant_time_size<false>, boost::heap::stable<true>,
+                                  boost::heap::compare<heap_element_compare>>
+    heap_type;
+
 /** @ingroup SURF_interface
  * @brief SURF action interface class
  * @details An action is an event generated by a resource (e.g.: a communication for the network)
@@ -233,14 +240,14 @@ private:
   double lastValue_          = 0;
   lmm_variable_t variable_   = nullptr;
   enum heap_action_type hat_ = NOTSET;
-  int indexHeap_;
+  boost::optional<heap_type::handle_type> heapHandle_ = boost::none;
 
 public:
   virtual void updateRemainingLazy(double now) { THROW_IMPOSSIBLE; };
-  void heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat);
-  void heapRemove(xbt_heap_t heap);
-  void heapUpdate(xbt_heap_t heap, double key, enum heap_action_type hat);
-  void updateIndexHeap(int i);
+  void heapInsert(heap_type& heap, double key, enum heap_action_type hat);
+  void heapRemove(heap_type& heap);
+  void heapUpdate(heap_type& heap, double key, enum heap_action_type hat);
+  void clearHeapHandle() { heapHandle_ = boost::none; }
   lmm_variable_t getVariable() {return variable_;}
   void setVariable(lmm_variable_t var) { variable_ = var; }
   double getLastUpdate() {return lastUpdate_;}
@@ -299,11 +306,11 @@ public:
   void setUpdateMechanism(e_UM_t mechanism) { updateMechanism_ = mechanism; }
 
   /** @brief Get Action heap */
-  xbt_heap_t getActionHeap() {return actionHeap_;}
+  heap_type& getActionHeap() { return actionHeap_; }
 
-  double actionHeapTopDate() const { return xbt_heap_maxkey(actionHeap_); }
-  Action* actionHeapPop() { return static_cast<Action*>(xbt_heap_pop(actionHeap_)); }
-  bool actionHeapIsEmpty() const { return xbt_heap_size(actionHeap_) == 0; }
+  double actionHeapTopDate() const { return actionHeap_.top().first; }
+  Action* actionHeapPop();
+  bool actionHeapIsEmpty() const { return actionHeap_.empty(); }
 
   /**
    * @brief Share the resources between the actions
@@ -343,7 +350,7 @@ private:
   ActionList* runningActionSet_; /**< Actions in state SURF_ACTION_RUNNING */
   ActionList* failedActionSet_; /**< Actions in state SURF_ACTION_FAILED */
   ActionList* doneActionSet_; /**< Actions in state SURF_ACTION_DONE */
-  xbt_heap_t actionHeap_;
+  heap_type actionHeap_;
 };
 
 }