build: fix travis MPI/SMP build
[charm.git] / src / ck-ldb / AdaptiveLB.C
blob2889be72cd6e3675a43357479406e0e39fe0e167
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 extern int quietModeRequested;
17 CreateLBFunc_Def(AdaptiveLB, "Allow multiple strategies to work serially")
19 AdaptiveLB::AdaptiveLB(const CkLBOptions &opt): CBase_AdaptiveLB(opt)
21   lbname = "AdaptiveLB";
22   const char *lbs = theLbdb->loadbalancer(opt.getSeqNo());
23   if (CkMyPe() == 0 && !quietModeRequested)
24     CkPrintf("CharmLB> AdaptiveLB created with %s.\n", lbs);
26   char *lbcopy = strdup(lbs);
27   const char *greedyLBString = "GreedyLB";
28   const char *refineLBString = "RefineLB";
29   const char *metisLBString = "MetisLB";
30   const char *commRefineLBString = "CommAwareRefineLB";
32   LBAllocFn fn = getLBAllocFn(greedyLBString);
33   if (fn == NULL) {
34     CkPrintf("LB> Invalid load balancer: %s.\n", greedyLBString);
35     CmiAbort("");
36   }
37   BaseLB *glb = fn();
38   greedyLB = (CentralLB*)glb;
40   fn = getLBAllocFn(refineLBString);
41   if (fn == NULL) {
42     CkPrintf("LB> Invalid load balancer: %s.\n", refineLBString);
43     CmiAbort("");
44   }
45   BaseLB *rlb = fn();
46   refineLB = (CentralLB*)rlb;
48   fn = getLBAllocFn(metisLBString);
49   if (fn == NULL) {
50     CkPrintf("LB> Invalid load balancer: %s.\n", metisLBString);
51     CmiAbort("");
52   }
53   BaseLB *slb = fn();
54   metisLB = (CentralLB*)slb;
56   fn = getLBAllocFn(commRefineLBString);
57   if (fn == NULL) {
58     CkPrintf("LB> Invalid load balancer: %s.\n", commRefineLBString);
59     CmiAbort("");
60   }
61   BaseLB *crlb = fn();
62   commRefineLB = (CentralLB*)crlb;
64   if (_lb_args.metaLbOn()) {
65     metabalancer = (MetaBalancer*)CkLocalBranch(_metalb);
66   } else {
67     metabalancer = NULL;
68   }
71 void AdaptiveLB::work(LDStats* stats)
74   ProcArray *parr = new ProcArray(stats);
75   ObjGraph *ogr = new ObjGraph(stats);
76   CkPrintf("Adaptive work\n");
78   //bool isComm = theLbdb->isStrategyComm();
80   // Calculate the load and total messages
81   double totalLoad = 0.0;
82   long totalMsgs = 0;
83   long long totalBytes = 0;
84   int vertnbr = ogr->vertices.size();
86   /** the object load is normalized to an integer between 0 and 256 */
87   for(int i = 0; i < vertnbr; i++) {
88     totalLoad += ogr->vertices[i].getVertexLoad();
89   }
91   for(int i = 0; i < vertnbr; i++) {
92     for(int j = 0; j < ogr->vertices[i].sendToList.size(); j++) {
93       totalMsgs += ogr->vertices[i].sendToList[j].getNumMsgs();
94       totalBytes += ogr->vertices[i].sendToList[j].getNumBytes();
95     }
96   }
97   double commOverhead = (totalMsgs * alpha) + (totalBytes * beta);
99   CkPrintf("AdaptiveLB> Total load %E\n", totalLoad);
100   CkPrintf("AdaptiveLB> Total Msgs %d\n", totalMsgs);
101   CkPrintf("AdaptiveLB> Total Bytes %ld\n", totalBytes);
102   CkPrintf("AdaptiveLB> Total Comm Overhead %E Total Load %E\n", commOverhead, totalLoad);
104   double tmp;
105   double refine_max_avg_ratio, lb_max_avg_ratio, greedy_max_avg_ratio;
106   int lb_type = -1;
107   double comm_ratio, comm_refine_ratio;
109   refine_max_avg_ratio = lb_max_avg_ratio = greedy_max_avg_ratio = 1.0;
110   comm_ratio = comm_refine_ratio = 1.0;
112   if (metabalancer != NULL) {
113     metabalancer->GetPrevLBData(lb_type, lb_max_avg_ratio, tmp);
114     metabalancer->GetLBDataForLB(0, greedy_max_avg_ratio, tmp);
115     metabalancer->GetLBDataForLB(1, refine_max_avg_ratio, tmp);
116     metabalancer->GetLBDataForLB(2, tmp, comm_ratio);
117     metabalancer->GetLBDataForLB(3, tmp, comm_refine_ratio);
118   }
120   CkPrintf("AdaptiveLB> Previous LB %d\n", lb_type);
122   // Choose the right LB
123   //
124   // If communication overhead is 10% computation, then choose Scotch LB
125   //if (isComm || (commOverhead > (totalLoad * percent_overhead / 100))) {
126   if ((commOverhead > (totalLoad * percent_overhead / 100))) {
127     if(lb_type == -1) {
128       lb_type = 2;
129       metisLB->work(stats);
130       CkPrintf("---METIS LB\n");
131     } else if (comm_refine_ratio <= 1.01) {
132       lb_type = 3;
133       commRefineLB->work(stats);
134       CkPrintf("---CommAwareRefineLB\n");
135     } else if (comm_ratio <= 1.01) {
136       lb_type = 2;
137       metisLB->work(stats);
138       CkPrintf("---METIS LB\n");
139     } else {
140       lb_type = 3;
141       commRefineLB->work(stats);
142       CkPrintf("---CommAwareRefineLB\n");
143     }
145   } else {
146     if (lb_type == -1) {
147       lb_type = 0;
148       greedyLB->work(stats);
149       CkPrintf("---GREEDY LB\n");
150     } else if (refine_max_avg_ratio <= 1.01) {
151       lb_type = 1;
152       refineLB->work(stats);
153       CkPrintf("---REFINE LB\n");
154     } else if (greedy_max_avg_ratio <= 1.01) {
155       lb_type = 0;
156       greedyLB->work(stats);
157       CkPrintf("---GREEDY LB\n");
158     } else {
159       lb_type = 1;
160       refineLB->work(stats);
161       CkPrintf("---REFINE LB\n");
162     }
163   }
164 //  UpdateLBDBWithData(lb_type, stats->after_lb_max, stats->after_lb_avg,
165 //      stats->local_comm, stats->remote_comm);
167   delete parr;
168   delete ogr;
172 #include "AdaptiveLB.def.h"
175 /*@}*/