Bug #1062: Fix linking errors by moving definition of userDrivenMode to machine-commo...
[charm.git] / src / ck-ldb / AdaptiveLB.C
blob86be77a2155bf5b9e3ff1388a4737fcfd97b6a68
1 /**
2  * \addtogroup CkLdb
3 */
4 /*@{*/
6 #include "AdaptiveLB.h"
7 #include "ckgraph.h"
9 #define alpha 4.0e-6
10 #define beta 2.67e-9
11 #define percent_overhead 10
13 extern LBAllocFn getLBAllocFn(const char *lbname);
15 CreateLBFunc_Def(AdaptiveLB, "Allow multiple strategies to work serially")
17 AdaptiveLB::AdaptiveLB(const CkLBOptions &opt): CBase_AdaptiveLB(opt)
19   lbname = "AdaptiveLB";
20   const char *lbs = theLbdb->loadbalancer(opt.getSeqNo());
21   if (CkMyPe() == 0)
22     CkPrintf("[%d] AdaptiveLB created with %s\n",CkMyPe(), lbs);
24   char *lbcopy = strdup(lbs);
25   const char *greedyLBString = "GreedyLB";
26   const char *refineLBString = "RefineLB";
27   const char *metisLBString = "MetisLB";
28   const char *commRefineLBString = "CommAwareRefineLB";
30   LBAllocFn fn = getLBAllocFn(greedyLBString);
31   if (fn == NULL) {
32     CkPrintf("LB> Invalid load balancer: %s.\n", greedyLBString);
33     CmiAbort("");
34   }
35   BaseLB *glb = fn();
36   greedyLB = (CentralLB*)glb;
38   fn = getLBAllocFn(refineLBString);
39   if (fn == NULL) {
40     CkPrintf("LB> Invalid load balancer: %s.\n", refineLBString);
41     CmiAbort("");
42   }
43   BaseLB *rlb = fn();
44   refineLB = (CentralLB*)rlb;
46   fn = getLBAllocFn(metisLBString);
47   if (fn == NULL) {
48     CkPrintf("LB> Invalid load balancer: %s.\n", metisLBString);
49     CmiAbort("");
50   }
51   BaseLB *slb = fn();
52   metisLB = (CentralLB*)slb;
54   fn = getLBAllocFn(commRefineLBString);
55   if (fn == NULL) {
56     CkPrintf("LB> Invalid load balancer: %s.\n", commRefineLBString);
57     CmiAbort("");
58   }
59   BaseLB *crlb = fn();
60   commRefineLB = (CentralLB*)crlb;
62   if (_lb_args.metaLbOn()) {
63     metabalancer = (MetaBalancer*)CkLocalBranch(_metalb);
64   } else {
65     metabalancer = NULL;
66   }
69 void AdaptiveLB::work(LDStats* stats)
72   ProcArray *parr = new ProcArray(stats);
73   ObjGraph *ogr = new ObjGraph(stats);
74   CkPrintf("Adaptive work\n");
76   //bool isComm = theLbdb->isStrategyComm();
78   // Calculate the load and total messages
79   double totalLoad = 0.0;
80   long totalMsgs = 0;
81   long long totalBytes = 0;
82   int vertnbr = ogr->vertices.size();
84   /** the object load is normalized to an integer between 0 and 256 */
85   for(int i = 0; i < vertnbr; i++) {
86     totalLoad += ogr->vertices[i].getVertexLoad();
87   }
89   for(int i = 0; i < vertnbr; i++) {
90     for(int j = 0; j < ogr->vertices[i].sendToList.size(); j++) {
91       totalMsgs += ogr->vertices[i].sendToList[j].getNumMsgs();
92       totalBytes += ogr->vertices[i].sendToList[j].getNumBytes();
93     }
94   }
95   double commOverhead = (totalMsgs * alpha) + (totalBytes * beta);
97   CkPrintf("AdaptiveLB> Total load %E\n", totalLoad);
98   CkPrintf("AdaptiveLB> Total Msgs %d\n", totalMsgs);
99   CkPrintf("AdaptiveLB> Total Bytes %ld\n", totalBytes);
100   CkPrintf("AdaptiveLB> Total Comm Overhead %E Total Load %E\n", commOverhead, totalLoad);
102   double tmp;
103   double refine_max_avg_ratio, lb_max_avg_ratio, greedy_max_avg_ratio;
104   int lb_type = -1;
105   double comm_ratio, comm_refine_ratio;
107   refine_max_avg_ratio = lb_max_avg_ratio = greedy_max_avg_ratio = 1.0;
108   comm_ratio = comm_refine_ratio = 1.0;
110   if (metabalancer != NULL) {
111     metabalancer->GetPrevLBData(lb_type, lb_max_avg_ratio, tmp);
112     metabalancer->GetLBDataForLB(0, greedy_max_avg_ratio, tmp);
113     metabalancer->GetLBDataForLB(1, refine_max_avg_ratio, tmp);
114     metabalancer->GetLBDataForLB(2, tmp, comm_ratio);
115     metabalancer->GetLBDataForLB(3, tmp, comm_refine_ratio);
116   }
118   CkPrintf("AdaptiveLB> Previous LB %d\n", lb_type);
120   // Choose the right LB
121   //
122   // If communication overhead is 10% computation, then choose Scotch LB
123   //if (isComm || (commOverhead > (totalLoad * percent_overhead / 100))) {
124   if ((commOverhead > (totalLoad * percent_overhead / 100))) {
125     if(lb_type == -1) {
126       lb_type = 2;
127       metisLB->work(stats);
128       CkPrintf("---METIS LB\n");
129     } else if (comm_refine_ratio <= 1.01) {
130       lb_type = 3;
131       commRefineLB->work(stats);
132       CkPrintf("---CommAwareRefineLB\n");
133     } else if (comm_ratio <= 1.01) {
134       lb_type = 2;
135       metisLB->work(stats);
136       CkPrintf("---METIS LB\n");
137     } else {
138       lb_type = 3;
139       commRefineLB->work(stats);
140       CkPrintf("---CommAwareRefineLB\n");
141     }
143   } else {
144     if (lb_type == -1) {
145       lb_type = 0;
146       greedyLB->work(stats);
147       CkPrintf("---GREEDY LB\n");
148     } else if (refine_max_avg_ratio <= 1.01) {
149       lb_type = 1;
150       refineLB->work(stats);
151       CkPrintf("---REFINE LB\n");
152     } else if (greedy_max_avg_ratio <= 1.01) {
153       lb_type = 0;
154       greedyLB->work(stats);
155       CkPrintf("---GREEDY LB\n");
156     } else {
157       lb_type = 1;
158       refineLB->work(stats);
159       CkPrintf("---REFINE LB\n");
160     }
161   }
162 //  UpdateLBDBWithData(lb_type, stats->after_lb_max, stats->after_lb_avg,
163 //      stats->local_comm, stats->remote_comm);
165   delete parr;
166   delete ogr;
170 #include "AdaptiveLB.def.h"
173 /*@}*/