doc: Add serial to list of ci file reserved words
[charm.git] / src / ck-ldb / RefineSwapLB.C
index af07a3025e111f76d943b4679f599cee90090fea..2ece1ba73230018dbacd6e12b1bb74f49b5605cf 100644 (file)
@@ -1,7 +1,6 @@
 /** \file RefineSwapLB.C
  *
- *  Written by Gengbin Zheng
- *  Updated by Abhinav Bhatele, 2010-12-09 to use ckgraph
+ *  Written by Harshitha Menon 
  *
  *  Status:
  *    -- Does not support pe_speed's currently
@@ -21,7 +20,8 @@
 using std::cout;
 using std::endl;
 
-CreateLBFunc_Def(RefineSwapLB, "always assign the heaviest obj onto lightest loaded processor.")
+CreateLBFunc_Def(RefineSwapLB,
+    "always assign the heaviest obj onto lightest loaded processor.")
 
 RefineSwapLB::RefineSwapLB(const CkLBOptions &opt): CentralLB(opt)
 {
@@ -55,11 +55,188 @@ class ProcLoadGreaterIndex {
 
 class ObjLoadGreater {
   public:
-    bool operator()(Vertex v1, Vertex v2) {
-      return (v1.getVertexLoad() > v2.getVertexLoad());
+    ObjLoadGreater(ObjGraph* ogr) : ogr(ogr) {}
+    bool operator()(int lhs, int rhs) {
+      return (ogr->vertices[lhs].getVertexLoad() < ogr->vertices[rhs].getVertexLoad());
     }
+  private:
+    ObjGraph* ogr;
 };
 
+inline void addObjToProc(ProcArray* parr, ObjGraph* ogr, std::vector<int>*
+    pe_obj, int pe_index, int obj_index) {
+
+  // Set the new pe
+  ogr->vertices[obj_index].setNewPe(pe_index);
+
+  // Add obj to the pe obj list
+  pe_obj[pe_index].push_back(obj_index);
+
+  // Update load
+  parr->procs[pe_index].totalLoad() += ogr->vertices[obj_index].getVertexLoad();
+}
+
+inline void removeObjFromProc(ProcArray* parr, ObjGraph* ogr, std::vector<int>*
+    pe_obj, int pe_index, int arr_index) {
+
+  // Update load
+  parr->procs[pe_index].totalLoad() -=
+      ogr->vertices[pe_obj[pe_index][arr_index]].getVertexLoad();
+
+  // Remove from pe_obj
+  pe_obj[pe_index].erase(pe_obj[pe_index].begin() + arr_index);
+}
+
+
+inline int getMax(ProcArray* parr, std::vector<int>& max_pe_heap) {
+  int p_index = max_pe_heap.front();
+  std::pop_heap(max_pe_heap.begin(), max_pe_heap.end(),
+      ProcLoadGreaterIndex(parr));
+  max_pe_heap.pop_back();
+  return p_index;
+}
+
+bool refine(ProcArray* parr, ObjGraph* ogr, std::vector<int>& max_pe_heap, 
+    std::vector<int>& min_pe_heap, std::vector<int>* pe_obj, int max_pe,
+    double avg_load, double threshold) {
+
+  int best_p, best_p_iter, arr_index;
+  bool allocated = false;
+  int pe_considered;
+  int obj_considered;
+  double best_size = 0.0;
+  std::sort(pe_obj[max_pe].begin(), pe_obj[max_pe].end(), ObjLoadGreater(ogr));
+
+  // Iterate over all the min pes and see which is the best object to
+  // transfer.
+
+  for (int i = (pe_obj[max_pe].size()-1); i >= 0; i--) {
+    for (int j = 0; j < min_pe_heap.size(); j++) {
+      obj_considered = pe_obj[max_pe][i];
+      pe_considered = min_pe_heap[j];
+   
+      if (parr->procs[pe_considered].getTotalLoad() +
+          ogr->vertices[obj_considered].getVertexLoad() < (avg_load + threshold)) {
+    //    if (ogr->vertices[obj_considered].getVertexLoad() > best_size) {
+          best_size = ogr->vertices[obj_considered].getVertexLoad();
+          best_p = pe_considered;
+          best_p_iter = j;
+          arr_index = i;
+          allocated = true;
+          break;
+    //    }
+      }
+    }
+  }
+
+  if (allocated) {
+
+    int best_obj = pe_obj[max_pe][arr_index];
+    addObjToProc(parr, ogr, pe_obj, best_p, best_obj);
+    removeObjFromProc(parr, ogr, pe_obj, max_pe, arr_index);
+
+    // Update the max heap and min list
+    if (parr->procs[max_pe].getTotalLoad() > (avg_load + threshold)) {
+      // Reinsert
+      max_pe_heap.push_back(max_pe);
+      std::push_heap(max_pe_heap.begin(), max_pe_heap.end(),
+          ProcLoadGreaterIndex(parr));
+    } else if (parr->procs[max_pe].getTotalLoad() < (avg_load - threshold)) {
+      // Insert into the list of underloaded procs
+      min_pe_heap.push_back(max_pe);
+    }
+
+    if (parr->procs[best_p].getTotalLoad() > (avg_load - threshold)) {
+      // Remove from list of underloaded procs
+      min_pe_heap.erase(min_pe_heap.begin() + best_p_iter);
+    }
+  }
+  return allocated;
+}
+
+bool IsSwapPossWithPe(ProcArray* parr, ObjGraph* ogr, std::vector<int>* pe_obj,
+    std::vector<int>& max_pe_heap, std::vector<int>& min_pe_heap,
+    int max_pe, int pe_considered, int pe_cons_iter, double diff,
+    double avg_load, double threshold) {
+
+  bool set = false;
+  for (int i = pe_obj[max_pe].size() - 1; i >= 0; i--) {
+    for (int j = 0; j < pe_obj[pe_considered].size(); j++) {
+      int pe_cons = pe_obj[pe_considered][j];
+      int max_pe_obj = pe_obj[max_pe][i];
+     // CkPrintf("\tCandidates %d(%lf) with %d(%lf) : diff (%lf)\n", max_pe_obj,
+     //     ogr->vertices[max_pe_obj].getVertexLoad(), pe_cons,
+     //     ogr->vertices[pe_cons].getVertexLoad(), diff);
+
+      if (ogr->vertices[pe_cons].getVertexLoad() <
+          ogr->vertices[max_pe_obj].getVertexLoad()) {
+        if ((diff + ogr->vertices[pe_cons].getVertexLoad()) >
+            ogr->vertices[max_pe_obj].getVertexLoad()) {
+          //CkPrintf("\tSwapping %d with %d\n", max_pe_obj, pe_cons);
+          set = true;
+
+          addObjToProc(parr, ogr, pe_obj, pe_considered, max_pe_obj);
+          removeObjFromProc(parr, ogr, pe_obj, max_pe, i);
+
+          addObjToProc(parr, ogr, pe_obj, max_pe, pe_cons);
+          removeObjFromProc(parr, ogr, pe_obj, pe_considered, j);
+
+          // Update the max heap and min list
+          if (parr->procs[max_pe].getTotalLoad() > (avg_load + threshold)) {
+            // Reinsert
+            max_pe_heap.push_back(max_pe);
+            std::push_heap(max_pe_heap.begin(), max_pe_heap.end(),
+                ProcLoadGreaterIndex(parr));
+          } else if (parr->procs[max_pe].getTotalLoad() < (avg_load - threshold)) {
+            // Insert into the list of underloaded procs
+            min_pe_heap.push_back(max_pe);
+          }
+
+          if (parr->procs[pe_considered].getTotalLoad() > (avg_load - threshold)) {
+            // Remove from list of underloaded procs
+            min_pe_heap.erase(min_pe_heap.begin() + pe_cons_iter);
+          }
+          break;
+        }
+      }
+    }
+
+    if (set) {
+      break;
+    }
+  }
+  return set;
+}
+
+bool refineSwap(ProcArray* parr, ObjGraph* ogr, std::vector<int>& max_pe_heap, 
+    std::vector<int>& min_pe_heap, std::vector<int>* pe_obj, int max_pe,
+    double avg_load, double threshold) {
+
+  double diff = 0;
+  bool is_possible = false;
+  int pe_considered;
+  int pe_cons_iter;
+  for (int i = 0; i < min_pe_heap.size(); i++) {
+    pe_considered = min_pe_heap[i];
+    pe_cons_iter = i;
+    std::sort(pe_obj[pe_considered].begin(), pe_obj[pe_considered].end(), ObjLoadGreater(ogr));
+    diff = avg_load - parr->procs[pe_considered].getTotalLoad();
+
+//    CkPrintf("Checking to swap maxload pe %d  with minpe %d  + diff %lf \n",
+//        max_pe, pe_considered, diff);
+    is_possible = IsSwapPossWithPe(parr, ogr, pe_obj, max_pe_heap, min_pe_heap, max_pe,
+        pe_considered, pe_cons_iter, diff, avg_load, threshold); 
+    if (is_possible) {
+      break;
+    }
+  }
+
+  if (!is_possible) {
+    return false;
+  }
+
+  return true;
+}
 
 void RefineSwapLB::work(LDStats* stats)
 {
@@ -69,7 +246,6 @@ void RefineSwapLB::work(LDStats* stats)
 
 
   /** ============================= STRATEGY ================================ */
-  //parr->resetTotalLoad();
 
   if (_lb_args.debug()>1) 
     CkPrintf("[%d] In RefineSwapLB strategy\n",CkMyPe());
@@ -86,9 +262,17 @@ void RefineSwapLB::work(LDStats* stats)
 
   std::vector<int>* pe_obj = new std::vector<int>[parr->procs.size()];
 
+
+  // Create a datastructure to store the objects in a processor
+  for (int i = 0; i < ogr->vertices.size(); i++) {
+    pe_obj[ogr->vertices[i].getCurrentPe()].push_back(i);
+//    CkPrintf("%d pe %d: %lf\n", i, ogr->vertices[i].getCurrentPe(), ogr->vertices[i].getVertexLoad());
+  }
+
   // Construct max heap of overloaded processors and min heap of underloaded
   // processors.
   for (int i = 0; i < parr->procs.size(); i++) {
+    //CkPrintf("%d : %lf\n", i, parr->procs[i].getTotalLoad());
     if (parr->procs[i].getTotalLoad() > upper_bound_load) {
       max_pe_heap.push_back(i);
     } else if (parr->procs[i].getTotalLoad() < lower_bound_load) {
@@ -96,166 +280,28 @@ void RefineSwapLB::work(LDStats* stats)
     }
   }
 
-  // Create a datastructure to store the objects in a processor
-  for (int i = 0; i < ogr->vertices.size(); i++) {
-    pe_obj[ogr->vertices[i].getCurrentPe()].push_back(i);
-  }
-
   std::make_heap(max_pe_heap.begin(), max_pe_heap.end(), ProcLoadGreaterIndex(parr));
 
-  int best_p;
-  int best_obj;
-  int iter_location;
-  int obj_iter_location;
-  int min_pe_iter;
-  int min_weight_obj;
-  int min_iter_location;
   while (max_pe_heap.size() != 0 && min_pe_heap.size() != 0) {
-    int ideal_transfer_pe = 0;
-    int p_index = max_pe_heap.front();
-    double best_size = 0.0;
-    double obj_wg_min = 100.0;
-    int allocated = false;
-    int obj_considered;
-    int pe_considered;
+    int p_index = getMax(parr, max_pe_heap);
     ProcInfo &pinfo = parr->procs[p_index];
-    std::pop_heap(max_pe_heap.begin(), max_pe_heap.end(),
-        ProcLoadGreaterIndex(parr));
-    max_pe_heap.pop_back();
-    cout << "Picked max pe " << p_index << " (" <<
-        parr->procs[p_index].getTotalLoad() << ")" << endl;
-
-    // Iterate over all the min pes and see which is the best object to
-    // transfer.
-    for (int j = 0; j < min_pe_heap.size(); j++) {
-      for (int i = 0; i < pe_obj[p_index].size(); i++) {
-        obj_considered = pe_obj[p_index][i];
-        pe_considered = min_pe_heap[j];
-
-        if (pe_obj[pe_considered].size() > pe_obj[ideal_transfer_pe].size()) {
-          ideal_transfer_pe = pe_considered;
-        }
-        if (ogr->vertices[obj_considered].getVertexLoad() < obj_wg_min) {
-          min_weight_obj = obj_considered;
-          min_iter_location = i;
-        }
 
-        if (parr->procs[pe_considered].getTotalLoad() + ogr->vertices[obj_considered].getVertexLoad() < (avg_load + threshold)) {
-          if (ogr->vertices[obj_considered].getVertexLoad() > best_size) {
-            best_obj = obj_considered;
-            best_size = ogr->vertices[obj_considered].getVertexLoad();
-            best_p = pe_considered;
-            iter_location = i;
-            min_pe_iter = j;
-            allocated = true;
-          }
-        }
-      }
-    }
+    bool success = refine(parr, ogr, max_pe_heap, min_pe_heap, pe_obj, p_index, avg_load, threshold);
+    
 
-    if (allocated) {
-      // Set the new pe
-      ogr->vertices[best_obj].setNewPe(best_p);
-
-      // Remove from pe_obj
-      pe_obj[p_index].erase(pe_obj[p_index].begin() + iter_location);
-      pe_obj[best_p].push_back(best_obj);
-
-      // Update load of underloaded and overloaded
-      parr->procs[p_index].setTotalLoad(parr->procs[p_index].getTotalLoad() -
-          best_size);
-      parr->procs[best_p].setTotalLoad(parr->procs[best_p].getTotalLoad() +
-          best_size);
-
-      std::cout << " Moving obj " << best_obj << " (" << best_size << ") from " << p_index << " to " <<
-            best_p << " New load " << p_index << ":" << parr->procs[p_index].getTotalLoad()
-            << " " << best_p << ":" << parr->procs[best_p].getTotalLoad()<< std::endl; 
-
-      // Update the max heap and min list
-      if (parr->procs[p_index].getTotalLoad() > (avg_load + threshold)) {
-        // Reinsert
-        max_pe_heap.push_back(p_index);
-        std::push_heap(max_pe_heap.begin(), max_pe_heap.end(),
-            ProcLoadGreaterIndex(parr));
-      } else if (parr->procs[p_index].getTotalLoad() < (avg_load - threshold)) {
-        // Insert into the list of underloaded procs
-        min_pe_heap.push_back(p_index);
-      }
-
-      if (parr->procs[best_p].getTotalLoad() > (avg_load - threshold)) {
-        // Remove from list of underloaded procs
-        min_pe_heap.erase(min_pe_heap.begin() + min_pe_iter);
-      }
-    } else {
+    if (!success) {
       // Swap with something. 
-      // TODO:
-      cout << " Swapping needs to be done" << endl;
-      best_size = 0.0;
-      int temp_iter = -1;
-      for (int i = 0; i < pe_obj[ideal_transfer_pe].size(); i++) {
-        obj_considered = pe_obj[ideal_transfer_pe][i];
-        // Find the max weight obj from the ideal transfer pe.
-          if (ogr->vertices[obj_considered].getVertexLoad() > best_size &&
-              ogr->vertices[min_weight_obj].getVertexLoad() < ogr->vertices[obj_considered].getVertexLoad()) {
-            best_obj = obj_considered;
-            best_size = ogr->vertices[obj_considered].getVertexLoad();
-            temp_iter = i;
-          }
-      }
-          // Swap max weight obj from 
-
-      // Set the new pe
-      ogr->vertices[obj_considered].setNewPe(p_index);
-      ogr->vertices[min_weight_obj].setNewPe(ideal_transfer_pe);
-
-      // Remove from pe_obj
-      pe_obj[p_index].erase(pe_obj[p_index].begin() + min_iter_location);
-      pe_obj[ideal_transfer_pe].erase(pe_obj[ideal_transfer_pe].begin() + temp_iter);
-      pe_obj[p_index].push_back(obj_considered);
-      pe_obj[ideal_transfer_pe].push_back(min_weight_obj);
-
-      // Update load of underloaded and overloaded
-      parr->procs[p_index].setTotalLoad(parr->procs[p_index].getTotalLoad() +
-          ogr->vertices[obj_considered].getVertexLoad() - ogr->vertices[min_weight_obj].getVertexLoad());
-      parr->procs[ideal_transfer_pe].setTotalLoad(parr->procs[best_p].getTotalLoad() -
-          ogr->vertices[obj_considered].getVertexLoad() + ogr->vertices[min_weight_obj].getVertexLoad());
-
-      // Update the max heap and min list
-      if (parr->procs[p_index].getTotalLoad() > (avg_load + threshold)) {
-        // Reinsert
-        max_pe_heap.push_back(p_index);
-        std::push_heap(max_pe_heap.begin(), max_pe_heap.end(),
-            ProcLoadGreaterIndex(parr));
-      } else if (parr->procs[p_index].getTotalLoad() < (avg_load - threshold)) {
-        // Insert into the list of underloaded procs
-        min_pe_heap.push_back(p_index);
-      }
 
-      if (parr->procs[ideal_transfer_pe].getTotalLoad() > (avg_load + threshold)) {
-        // Reinsert
-        max_pe_heap.push_back(ideal_transfer_pe);
+      if (!refineSwap(parr, ogr, max_pe_heap, min_pe_heap, pe_obj, p_index, avg_load,
+            threshold)) {
+        max_pe_heap.push_back(p_index);
         std::push_heap(max_pe_heap.begin(), max_pe_heap.end(),
             ProcLoadGreaterIndex(parr));
-        min_pe_heap.erase(min_pe_heap.begin() + min_pe_iter);
-      } else if (parr->procs[ideal_transfer_pe].getTotalLoad() > (avg_load - threshold)) {
-        // Remove from list of underloaded procs
-        min_pe_heap.erase(min_pe_heap.begin() + min_pe_iter);
+        break;
       }
-
-       cout << "  Swapping " << obj_considered << " (" <<
-       ogr->vertices[obj_considered].getVertexLoad() << ") " <<  min_weight_obj
-       << "(" << ogr->vertices[min_weight_obj].getVertexLoad() << ")"
-       <<  " from " << ideal_transfer_pe << " (" <<
-       parr->procs[ideal_transfer_pe].getTotalLoad() <<
-       ") to " << p_index << " (" << parr->procs[p_index].getTotalLoad() << ")"<< endl; 
     }
   }
 
-  std::cout << "Overloaded Processor load"<< endl;
-  for (int i = 0; i < max_pe_heap.size(); i++) {
-    std::cout << max_pe_heap[i] << ": " << parr->procs[max_pe_heap[i]].getTotalLoad() << std::endl;
-  }
-
   /** ============================== CLEANUP ================================ */
   ogr->convertDecisions(stats);         // Send decisions back to LDStats
   delete[] pe_obj;