00001 
00030 #ifndef METABALANCER_H
00031 #define METABALANCER_H
00032 
00033 #include "LBDatabase.h"
00034 #include "RandomForestModel.h"
00035 #include <vector>
00036 
00037 #include "MetaBalancer.decl.h"
00038 
00039 using namespace rfmodel;
00040 
00041 extern CkGroupID _metalb;
00042 extern CkGroupID _metalbred;
00043 
00044 CkpvExtern(int, metalbInited);
00045 
00046 void _MetaLBInit();
00047 
00048 
00049 class MetaLBInit : public Chare {
00050   public:
00051     MetaLBInit(CkArgMsg*);
00052     MetaLBInit(CkMigrateMessage *m):Chare(m) {}
00053 };
00054 
00055 enum metalb_stats_types{
00056   ITER_NO,
00057   NUM_PROCS,
00058   TOTAL_LOAD,
00059   MAX_LOAD,
00060   IDLE_TIME,
00061   UTILIZATION,
00062   TOTAL_LOAD_W_BG,
00063   MAX_LOAD_W_BG,
00064   TOTAL_KBYTES,
00065   TOTAL_KMSGS,
00066   WITHIN_PE_KBYTES,
00067   OUTSIDE_PE_KBYTES,
00068   SUM_COMM_NEIGHBORS,
00069   MAX_COMM_NEIGHBORS,
00070   SUM_OBJ_COUNT,
00071   MAX_OBJ_COUNT,
00072   SUM_OBJ_LOAD,
00073   MAX_OBJ_LOAD,
00074   SUM_HOPS,
00075   SUM_HOP_KBYTES,
00076   LOAD_STDEV2,
00077   MAX_UTIL,
00078   MIN_LOAD,
00079   MIN_BG,
00080   MIN_OBJ_LOAD,
00081   MAX_ITER_TIME,
00082   LOAD_SKEWNESS,
00083   LOAD_KURTOSIS,
00084   TOTAL_OVERLOADED_PES,
00085 };
00086 
00087 class MetaBalancer : public CBase_MetaBalancer {
00088 public:
00089  MetaBalancer(void) : rFmodel(NULL) { init(); }
00090  MetaBalancer(CkMigrateMessage* m) : CBase_MetaBalancer(m) { init(); }
00091  ~MetaBalancer() {
00092    if (CkMyPe() == 0) delete rFmodel;
00093  }
00094 
00095 private:
00096   void init();
00097   MetaBalancerRedn* metaRdnGroup;
00098 
00099 public:
00100   inline static MetaBalancer * Object() {
00101     return CkpvAccess(metalbInited)?(MetaBalancer *)CkLocalBranch(_metalb):NULL;
00102   }
00103 
00104   static void initnodeFn(void);
00105 
00106   void pup(PUP::er& p);
00107 
00108   void ResumeClients();
00109 
00110   void ResetAdaptive();
00111   int get_iteration();
00112   int get_finished_iteration();
00113   void ContributeStats(int iteration);
00114   void AdjustCountForDeadContributor(int iteration);
00115   void AdjustCountForNewContributor(int iteration);
00116   bool AddLoad(int iteration, double load);
00117   void SetCharePupSize(size_t psize);
00118   void ReceiveMinStats(double *load, int n);
00119   void TriggerSoon(int iteration_no, double imbalance_ratio, double tolerate_imb);
00120   void LoadBalanceDecision(int, int);
00121   void LoadBalanceDecisionFinal(int, int);
00122   void MetaLBCallLBOnChares();
00123   void MetaLBSetLBOnChares(int switchFrom, int switchTo);
00124   void ReceiveIterationNo(int); 
00125   static void periodicCall(void *ad);
00126   static void checkForNoObj(void *ad);
00127   void HandleAdaptiveNoObj();
00128   void RegisterNoObjCallback(int index);
00129   void TriggerAdaptiveReduction();
00130   void PreviousAvgLoad(double avg);
00131 
00132   bool generatePlan(int& period, double& ratio_at_t);
00133   bool getLineEq(double new_load_percent, double& aslope, double& ac,
00134       double& mslope, double& mc);
00135   bool getPeriodForLinear(double a, double b, double c, int& period);
00136   bool getPeriodForStrategy(double new_load, double overhead_percent,
00137       int& period, double& ratio_at_t);
00138   int getPredictedLBPeriod(bool& is_tentative);
00139 
00140   bool isStrategyComm();
00141 
00142   void UpdateAfterLBData(int is_lb_refine, double lb_max, double lb_avg, double
00143       local_comm, double remote_comm);
00144 
00145   void UpdateAfterLBData(double max_load, double max_cpu, double avg_load);
00146   void UpdateAfterLBComm(double alpha_beta_cost);
00147   void GetPrevLBData(int& lb_type, double& lb_max_avg_ratio, double&
00148       local_remote_comm_ratio);
00149   void GetLBDataForLB(int lb_type, double& lb_max_avg_ratio, double&
00150       local_remote_comm_ratio);
00151 
00152   void SetMigrationCost(double lb_migration_cost);
00153   void SetStrategyCost(double lb_strategy_cost);
00154 
00155 private:
00156   LBDatabase* lbdatabase;
00157   std::vector<double> total_load_vec;
00158   
00159   std::vector<int> total_count_vec;
00160   std::vector<double> max_load_vec;
00161   std::vector<double> min_load_vec;
00162   std::vector<int> lbdb_no_obj_callback;
00163 
00164   double prev_idle;
00165   double alpha_beta_cost_to_load;
00166   int is_prev_lb_refine;
00167   int prev_bytes, prev_msgs;
00168   int prev_outsidepemsgs, prev_outsidepebytes;
00169   int prev_hops, prev_hopbytes;
00170   bool print_iteration_time;
00171   double prev_iteration_time;
00172   double prev_avg_load;
00173   int chare_pup_size;
00174   double pe_ld_kurtosis;
00175   double pe_ld_skewness;
00176   int total_ovld_pes;
00177   int current_balancer;
00178   ForestModel* rFmodel;
00179 
00180   struct AdaptiveData {
00181     double iteration;
00182     double max_load;
00183     double avg_load;
00184     double min_utilization;
00185     double avg_utilization;
00186     double total_bytes;
00187     double total_msgs;
00188   };
00189 
00190   struct AdaptiveMetaBalancer {
00191     CkVec<AdaptiveData> history_data;
00192     int lb_iter_no;
00193   } adaptive_lbdb;
00194 
00195   struct AdaptiveLBInfo {
00196     AdaptiveLBInfo() {
00197       max_avg_ratio = 1;
00198       remote_local_ratio = 1;
00199     }
00200     double max_avg_ratio;
00201     double remote_local_ratio;
00202   };
00203 
00204   
00205   
00206   struct AdaptiveLBStructure {
00207     int tentative_period;
00208     int final_lb_period;
00209     
00210     int lb_calculated_period;
00211     
00212     int lb_iteration_no;
00213     
00214     int finished_iteration_no;
00215     
00216     int global_max_iter_no;
00217     
00218     
00219     int tentative_max_iter_no;
00220     
00221     bool in_progress;
00222     double lb_strategy_cost;
00223     double lb_migration_cost;
00224     bool doCommStrategy;
00225     int lb_msg_send_no;
00226     int lb_msg_recv_no;
00227     
00228     int total_syncs_called;
00229     int last_lb_type;
00230     AdaptiveLBInfo greedy_info;
00231     AdaptiveLBInfo refine_info;
00232     AdaptiveLBInfo comm_info;
00233     AdaptiveLBInfo comm_refine_info;
00234     AdaptiveLBInfo info_first_iter;
00235   } adaptive_struct;
00236 
00237 public:
00238   bool lb_in_progress;
00239 
00240 };
00241 
00242 class MetaBalancerRedn : public CBase_MetaBalancerRedn {
00243   public:
00244     MetaBalancerRedn(void) {init();}
00245     MetaBalancerRedn(CkMigrateMessage *m) : CBase_MetaBalancerRedn(m)  {init();}
00246     ~MetaBalancerRedn()  {}
00247     void pup(PUP::er& p);
00248     void ReceiveIterNo(int max_iter);
00249     void getMaxIter(int);
00250 
00251   private:
00252     MetaBalancer* metabalancer;
00253     void init();
00254 };
00255 
00256 inline MetaBalancer* MetaBalancerObj() { return MetaBalancer::Object(); }
00257 
00258 #endif 
00259