00001
00005
00006 #include "AdaptiveLB.h"
00007 #include "ckgraph.h"
00008
00009 #define alpha 4.0e-6
00010 #define beta 2.67e-9
00011 #define percent_overhead 10
00012
00013 extern LBAllocFn getLBAllocFn(const char *lbname);
00014
00015 extern int quietModeRequested;
00016
00017 CreateLBFunc_Def(AdaptiveLB, "Allow multiple strategies to work serially")
00018
00019 AdaptiveLB::AdaptiveLB(const CkLBOptions &opt): CBase_AdaptiveLB(opt)
00020 {
00021 lbname = "AdaptiveLB";
00022 const char *lbs = theLbdb->loadbalancer(opt.getSeqNo());
00023 if (CkMyPe() == 0 && !quietModeRequested)
00024 CkPrintf("CharmLB> AdaptiveLB created with %s.\n", lbs);
00025
00026 char *lbcopy = strdup(lbs);
00027 const char *greedyLBString = "GreedyLB";
00028 const char *refineLBString = "RefineLB";
00029 const char *metisLBString = "MetisLB";
00030 const char *commRefineLBString = "CommAwareRefineLB";
00031
00032 LBAllocFn fn = getLBAllocFn(greedyLBString);
00033 if (fn == NULL) {
00034 CkPrintf("LB> Invalid load balancer: %s.\n", greedyLBString);
00035 CmiAbort("");
00036 }
00037 BaseLB *glb = fn();
00038 greedyLB = (CentralLB*)glb;
00039
00040 fn = getLBAllocFn(refineLBString);
00041 if (fn == NULL) {
00042 CkPrintf("LB> Invalid load balancer: %s.\n", refineLBString);
00043 CmiAbort("");
00044 }
00045 BaseLB *rlb = fn();
00046 refineLB = (CentralLB*)rlb;
00047
00048 fn = getLBAllocFn(metisLBString);
00049 if (fn == NULL) {
00050 CkPrintf("LB> Invalid load balancer: %s.\n", metisLBString);
00051 CmiAbort("");
00052 }
00053 BaseLB *slb = fn();
00054 metisLB = (CentralLB*)slb;
00055
00056 fn = getLBAllocFn(commRefineLBString);
00057 if (fn == NULL) {
00058 CkPrintf("LB> Invalid load balancer: %s.\n", commRefineLBString);
00059 CmiAbort("");
00060 }
00061 BaseLB *crlb = fn();
00062 commRefineLB = (CentralLB*)crlb;
00063
00064 if (_lb_args.metaLbOn()) {
00065 metabalancer = (MetaBalancer*)CkLocalBranch(_metalb);
00066 } else {
00067 metabalancer = NULL;
00068 }
00069 }
00070
00071 void AdaptiveLB::work(LDStats* stats)
00072 {
00073
00074 ProcArray *parr = new ProcArray(stats);
00075 ObjGraph *ogr = new ObjGraph(stats);
00076 CkPrintf("Adaptive work\n");
00077
00078
00079
00080
00081 double totalLoad = 0.0;
00082 long totalMsgs = 0;
00083 long long totalBytes = 0;
00084 int vertnbr = ogr->vertices.size();
00085
00087 for(int i = 0; i < vertnbr; i++) {
00088 totalLoad += ogr->vertices[i].getVertexLoad();
00089 }
00090
00091 for(int i = 0; i < vertnbr; i++) {
00092 for(int j = 0; j < ogr->vertices[i].sendToList.size(); j++) {
00093 totalMsgs += ogr->vertices[i].sendToList[j].getNumMsgs();
00094 totalBytes += ogr->vertices[i].sendToList[j].getNumBytes();
00095 }
00096 }
00097 double commOverhead = (totalMsgs * alpha) + (totalBytes * beta);
00098
00099 CkPrintf("AdaptiveLB> Total load %E\n", totalLoad);
00100 CkPrintf("AdaptiveLB> Total Msgs %d\n", totalMsgs);
00101 CkPrintf("AdaptiveLB> Total Bytes %ld\n", totalBytes);
00102 CkPrintf("AdaptiveLB> Total Comm Overhead %E Total Load %E\n", commOverhead, totalLoad);
00103
00104 double tmp;
00105 double refine_max_avg_ratio, lb_max_avg_ratio, greedy_max_avg_ratio;
00106 int lb_type = -1;
00107 double comm_ratio, comm_refine_ratio;
00108
00109 refine_max_avg_ratio = lb_max_avg_ratio = greedy_max_avg_ratio = 1.0;
00110 comm_ratio = comm_refine_ratio = 1.0;
00111
00112 if (metabalancer != NULL) {
00113 metabalancer->GetPrevLBData(lb_type, lb_max_avg_ratio, tmp);
00114 metabalancer->GetLBDataForLB(0, greedy_max_avg_ratio, tmp);
00115 metabalancer->GetLBDataForLB(1, refine_max_avg_ratio, tmp);
00116 metabalancer->GetLBDataForLB(2, tmp, comm_ratio);
00117 metabalancer->GetLBDataForLB(3, tmp, comm_refine_ratio);
00118 }
00119
00120 CkPrintf("AdaptiveLB> Previous LB %d\n", lb_type);
00121
00122
00123
00124
00125
00126 if ((commOverhead > (totalLoad * percent_overhead / 100))) {
00127 if(lb_type == -1) {
00128 lb_type = 2;
00129 metisLB->work(stats);
00130 CkPrintf("---METIS LB\n");
00131 } else if (comm_refine_ratio <= 1.01) {
00132 lb_type = 3;
00133 commRefineLB->work(stats);
00134 CkPrintf("---CommAwareRefineLB\n");
00135 } else if (comm_ratio <= 1.01) {
00136 lb_type = 2;
00137 metisLB->work(stats);
00138 CkPrintf("---METIS LB\n");
00139 } else {
00140 lb_type = 3;
00141 commRefineLB->work(stats);
00142 CkPrintf("---CommAwareRefineLB\n");
00143 }
00144
00145 } else {
00146 if (lb_type == -1) {
00147 lb_type = 0;
00148 greedyLB->work(stats);
00149 CkPrintf("---GREEDY LB\n");
00150 } else if (refine_max_avg_ratio <= 1.01) {
00151 lb_type = 1;
00152 refineLB->work(stats);
00153 CkPrintf("---REFINE LB\n");
00154 } else if (greedy_max_avg_ratio <= 1.01) {
00155 lb_type = 0;
00156 greedyLB->work(stats);
00157 CkPrintf("---GREEDY LB\n");
00158 } else {
00159 lb_type = 1;
00160 refineLB->work(stats);
00161 CkPrintf("---REFINE LB\n");
00162 }
00163 }
00164
00165
00166
00167 delete parr;
00168 delete ogr;
00169
00170 }
00171
00172 #include "AdaptiveLB.def.h"
00173
00174