From 965056e4f95159e78816eca6edc8982928097b73 Mon Sep 17 00:00:00 2001 From: Augustin Degomme Date: Fri, 19 Sep 2014 16:29:15 +0200 Subject: [PATCH] [xbt/surf] Change the costly heapremove+heapinsert used in share resource lazy algorithm. Add a xbt_heap_update function, to set a new value for a element in the heap --- include/xbt/heap.h | 2 ++ src/surf/surf_interface.cpp | 13 +++++++++++-- src/surf/surf_interface.hpp | 1 + src/xbt/heap.c | 33 +++++++++++++++++++++++++++++---- 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/include/xbt/heap.h b/include/xbt/heap.h index 02fdf0d7c0..e0ddfb8dd3 100644 --- a/include/xbt/heap.h +++ b/include/xbt/heap.h @@ -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 */ diff --git a/src/surf/surf_interface.cpp b/src/surf/surf_interface.cpp index a20ea1b0e6..6de2b62a46 100644 --- a/src/surf/surf_interface.cpp +++ b/src/surf/surf_interface.cpp @@ -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); diff --git a/src/surf/surf_interface.hpp b/src/surf/surf_interface.hpp index 1f0adfebfc..08d445b671 100644 --- a/src/surf/surf_interface.hpp +++ b/src/surf/surf_interface.hpp @@ -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;} diff --git a/src/xbt/heap.c b/src/xbt/heap.c index 292b5f704b..34b12cc593 100644 --- a/src/xbt/heap.c +++ b/src/xbt/heap.c @@ -13,7 +13,7 @@ #include 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; -- 2.20.1