From: Arnaud Giersch <arnaud.giersch@iut-bm.univ-fcomte.fr>
Date: Tue, 25 Jan 2011 12:12:47 +0000 (+0100)
Subject: Define process::send(), and simplify load_balance() logic.
X-Git-Tag: v0.1~188^2~8
X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/loba.git/commitdiff_plain/16d0d07b6b60083606697f59b9ff3ae6692b09c3

Define process::send(), and simplify load_balance() logic.
---

diff --git a/README b/README
index 37c51be..bf2d915 100644
--- a/README
+++ b/README
@@ -92,13 +92,12 @@ Pour ajouter un nouvel algorithme d'équilibrage
    - définir une nouvelle classe dérivant de process
    - attention, il faut construire le process explicitement
    - redéfinir la méthode load_balance qui :
-     - reçoit en paramètre la charge à prendre en compte ;
+     - peut récupérer la charge courante avec get_load()
      - peut utiliser et éventuellement réordonner le tableau process::pneigh ;
      - peut récupérer l'information de charge d'un voisin avec
            pneigh[i]->get_load() ;
      - définit la charge à envoyer avec
-           pneigh[i]->set_to_send(quantité) ;
-     - retourne la somme des quantités définies avec set_to_send.
+           send(pneigh[i], quantité) ;
 
 2. Ajouter l'algorithme dans la liste des options.  Dans options.cpp :
    - faire le #include adéquat ;
diff --git a/loba_fairstrategy.cpp b/loba_fairstrategy.cpp
index c372f74..4ad0bff 100644
--- a/loba_fairstrategy.cpp
+++ b/loba_fairstrategy.cpp
@@ -17,30 +17,24 @@ public:
     }
 };
 
-double loba_fairstrategy::load_balance(double my_load)
+void loba_fairstrategy::load_balance()
 {
     std::sort(pneigh.begin(), pneigh.end(), compare());
 
     print_loads_p(false, xbt_log_priority_debug);
 
-    double sum_sent = 0;
     bool found = true;
 
     while (found) {
         found = false;
         for (unsigned i = 0 ; i < pneigh.size() ; ++i) {
-            if (pneigh[i]->get_load() <= my_load - 2) {
+            if (pneigh[i]->get_load() <= get_load() - 2) {
                 found = true;
-                pneigh[i]->add_load(1);
-                pneigh[i]->add_to_send(1);
+                send(pneigh[i], 1);
                 DEBUG1("sent to %s", pneigh[i]->get_name());
-                my_load--;
-                sum_sent++;
             }
         }
     }
-
-    return sum_sent;
 }
 
 // Local variables:
diff --git a/loba_fairstrategy.h b/loba_fairstrategy.h
index 9984c36..3e09627 100644
--- a/loba_fairstrategy.h
+++ b/loba_fairstrategy.h
@@ -9,7 +9,7 @@ public:
     ~loba_fairstrategy()                                           { }
 
 private:
-    double load_balance(double my_load);
+    void load_balance();
 };
 
 #endif //!LOBA_SIMPLE
diff --git a/loba_simple.cpp b/loba_simple.cpp
index 26622c8..2d4103c 100644
--- a/loba_simple.cpp
+++ b/loba_simple.cpp
@@ -8,15 +8,15 @@ XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(loba);
  *   load balance with a least-loaded neighbor,
  *   without breaking the ping-pong condition
  */
-double loba_simple::load_balance(double my_load)
+void loba_simple::load_balance()
 {
     int imin = -1;
     int imax = -1;
-    double min = my_load;
+    double min = get_load();
     double max = -1.0;
     for (unsigned i = 0 ; i < pneigh.size() ; ++i) {
         double l = pneigh[i]->get_load();
-        if (l >= my_load)
+        if (l >= get_load())
             continue;
         if (l < min) {
             imin = i;
@@ -29,13 +29,9 @@ double loba_simple::load_balance(double my_load)
     }
     if (imin != -1) {
         // found someone
-        double balance = (my_load - max) / 2;
-        DEBUG6("%d:%g %d:%g %g %g", imin, min, imax, max, my_load, balance);
-        pneigh[imin]->set_to_send(balance);
-        pneigh[imin]->add_load(balance);
-        return balance;
-    } else {
-        return 0.0;
+        double balance = (get_load() - max) / 2;
+        DEBUG6("%d:%g %d:%g %g %g", imin, min, imax, max, get_load(), balance);
+        send(pneigh[imin], balance);
     }
 }
 
diff --git a/loba_simple.h b/loba_simple.h
index 1a2fd73..77cd8ad 100644
--- a/loba_simple.h
+++ b/loba_simple.h
@@ -9,7 +9,7 @@ public:
     ~loba_simple()                                           { }
 
 private:
-    double load_balance(double my_load);
+    void load_balance();
 };
 
 #endif //!LOBA_SIMPLE
diff --git a/neighbor.h b/neighbor.h
index 62b9b66..b51dab1 100644
--- a/neighbor.h
+++ b/neighbor.h
@@ -18,7 +18,6 @@ public:
     // Getter and setter for load
     double get_load() const             { return load;    }
     void set_load(double amount)        { load = amount;  }
-    void add_load(double amount)        { load += amount; }
 
     // Getter and setter for debt
     double get_debt() const             { return debt;   }
@@ -27,7 +26,6 @@ public:
     // Getter and setter for to_send
     double get_to_send() const          { return to_send;    }
     void set_to_send(double amount)     { to_send = amount;  }
-    void add_to_send(double amount)     { to_send += amount; }
 
     // Prints its name and load on given category, with given
     // priority.  If verbose is true, prints debt and to_send too.
diff --git a/process.cpp b/process.cpp
index a74b114..54573d3 100644
--- a/process.cpp
+++ b/process.cpp
@@ -83,8 +83,7 @@ int process::run()
     VERB0("Starting...");
     comp_iter = lb_iter = 0;
     while (true) {
-        double ld = get_load();
-        if (ld > 0.0) {
+        if (get_load() > 0.0) {
             double now = MSG_get_clock();
             if (now < next_iter_after_date)
                 MSG_process_sleep(next_iter_after_date - now);
@@ -101,11 +100,10 @@ int process::run()
                           lb_iter, real_load);
             }
 
-            ld -= load_balance(ld);
+            load_balance();
 
             print_loads(true, xbt_log_priority_debug);
         }
-        set_load(ld);
 
         // send load information, and load (data) if any
         send_all();
@@ -174,11 +172,10 @@ int process::run()
     return 0;
 }
 
-double process::load_balance(double /*my_load*/)
+void process::load_balance()
 {
     if (lb_iter == 1)           // warn only once
         WARN0("process::load_balance() is a no-op!");
-    return 0.0;
 }
 
 void process::compute()
@@ -196,6 +193,13 @@ void process::compute()
     }
 }
 
+void process::send(neighbor& nb, double amount)
+{
+    set_load(get_load() - amount);
+    nb.set_to_send(nb.get_to_send() + amount);
+    nb.set_load(nb.get_load() + amount); // fixme: make this optional?
+}
+
 void process::send1_no_bookkeeping(neighbor& nb)
 {
     if (real_load != prev_load_broadcast)
diff --git a/process.h b/process.h
index 1374bf4..78fc507 100644
--- a/process.h
+++ b/process.h
@@ -44,6 +44,10 @@ protected:
     double get_load() const;
     void set_load(double load);
 
+    // Register some amount of load to send to given neighbor.
+    void send(neighbor& nb, double amount);
+    void send(neighbor* nb, double amount) { send(*nb, amount); }
+
     // Calls neighbor::print(verbose, logp, cat) for each member of neigh.
     void print_loads(bool verbose = false,
                      e_xbt_log_priority_t logp = xbt_log_priority_info,
@@ -83,10 +87,7 @@ private:
     double expected_load;       // expected load in bookkeeping mode
 
     // The load balancing algorithm comes here...
-    // Parameter "my_load" is the load to take into account for myself
-    // (may be real load or expected load).
-    // Returns the total load sent to neighbors.
-    virtual double load_balance(double my_load);
+    virtual void load_balance();
 
     // Virtually do some computation
     void compute();