Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[xbt/surf] Change the costly heapremove+heapinsert used in share resource lazy algorithm.
authorAugustin Degomme <augustin.degomme@imag.fr>
Fri, 19 Sep 2014 14:29:15 +0000 (16:29 +0200)
committerAugustin Degomme <augustin.degomme@imag.fr>
Fri, 19 Sep 2014 14:29:50 +0000 (16:29 +0200)
Add a xbt_heap_update function, to set a new value for a element in the heap

include/xbt/heap.h
src/surf/surf_interface.cpp
src/surf/surf_interface.hpp
src/xbt/heap.c

index 02fdf0d..e0ddfb8 100644 (file)
@@ -35,6 +35,8 @@ XBT_PUBLIC(void) xbt_heap_set_update_callback(xbt_heap_t H,
                                                                        *,
                                                                        int));
 XBT_PUBLIC(void *) xbt_heap_remove(xbt_heap_t H, int i);
+XBT_PUBLIC(void ) xbt_heap_update(xbt_heap_t H, int i, double key);
+
 /* @} */
 SG_END_DECL()
 #endif                          /* _XBT_HEAP_H */
index a20ea1b..6de2b62 100644 (file)
@@ -583,8 +583,7 @@ double Model::shareResourcesLazy(double now)
         action->getMaxDuration());
 
     if (min != -1) {
-      action->heapRemove(p_actionHeap);
-      action->heapInsert(p_actionHeap, min, max_dur_flag ? MAX_DURATION : NORMAL);
+      action->heapUpdate(p_actionHeap, min, max_dur_flag ? MAX_DURATION : NORMAL);
       XBT_DEBUG("Insert at heap action(%p) min %f now %f", action, min,
                 now);
     } else DIE_IMPOSSIBLE;
@@ -1008,6 +1007,16 @@ void Action::heapRemove(xbt_heap_t heap)
   }
 }
 
+void Action::heapUpdate(xbt_heap_t heap, double key, enum heap_action_type hat)
+{
+  m_hat = hat;
+  if (m_indexHeap >= 0) {
+    xbt_heap_update(heap, m_indexHeap, key);
+  }else{
+    xbt_heap_push(heap, this, key);
+  }
+}
+
 /* added to manage the communication action's heap */
 void surf_action_lmm_update_index_heap(void *action, int i) {
   ((ActionPtr)action)->updateIndexHeap(i);
index 1f0adfe..08d445b 100644 (file)
@@ -660,6 +660,7 @@ public:
   virtual void updateRemainingLazy(double now);
   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);
   lmm_variable_t getVariable() {return p_variable;}
   double getLastUpdate() {return m_lastUpdate;}
index 292b5f7..34b12cc 100644 (file)
@@ -13,7 +13,7 @@
 #include <stdio.h>
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_heap, xbt, "Heap");
 
-static void xbt_heap_max_heapify(xbt_heap_t H);
+static void xbt_heap_max_heapify(xbt_heap_t H, int i);
 static void xbt_heap_increase_key(xbt_heap_t H, int i);
 
 /** @addtogroup XBT_heap
@@ -108,6 +108,7 @@ void xbt_heap_push(xbt_heap_t H, void *content, double key)
   return;
 }
 
+
 /**
  * @brief Extracts from the heap and returns the element with the smallest key.
  * \param H the heap we're working on
@@ -132,7 +133,7 @@ void *xbt_heap_pop(xbt_heap_t H)
 
   items[0] = items[(H->count) - 1];
   (H->count)--;
-  xbt_heap_max_heapify(H);
+  xbt_heap_max_heapify(H,0);
   if (H->count < size >> 2 && size > 16) {
     size = (size >> 1) + 1;
     H->items =
@@ -169,6 +170,30 @@ void *xbt_heap_remove(xbt_heap_t H, int i)
   return xbt_heap_pop(H);
 }
 
+/**
+ * @brief Updates an element of the heap with a new value.
+ * \param H the heap we're working on
+ * \param i  element position
+ * \param key new value for the element
+ *
+ * Updates an element of the heap with a new value.
+ */
+void xbt_heap_update(xbt_heap_t H, int i, double key)
+{
+  XBT_DEBUG("Heap has %d elements: updating element %d : was %1.12f to %1.12f ",xbt_heap_size(H),i,KEY(H, i), key);
+
+  if ((i < 0) || (i > H->count - 1) || key == KEY(H, i))
+    return ;
+
+  if(key< KEY(H, i)){
+    KEY(H, i)=key;
+    xbt_heap_increase_key(H, i);
+  }else{
+    KEY(H, i)=key;
+    xbt_heap_max_heapify(H,i);
+  }
+}
+
 /**
  * @brief returns the smallest key in the heap (heap unchanged)
  * \param H the heap we're working on
@@ -199,9 +224,9 @@ void *xbt_heap_maxcontent(xbt_heap_t H)
  *
  * Restores the heap property once an element has been deleted.
  */
-static void xbt_heap_max_heapify(xbt_heap_t H)
+static void xbt_heap_max_heapify(xbt_heap_t H, int index)
 {
-  int i = 0;
+  int i = index;
   int count = H->count;
   xbt_heap_item_t items = H->items;