00001
00005
00006 #include <charm++.h>
00007 #include "tm_tree.h"
00008 #include "tm_mapping.h"
00009 #include "TreeMatchLB.h"
00010 #include "ckgraph.h"
00011 #include <algorithm>
00012
00013 extern int quietModeRequested;
00014
00015 CreateLBFunc_Def(TreeMatchLB, "TreeMatch load balancer, like a normal one but with empty strategy")
00016
00017 #include "TreeMatchLB.def.h"
00018
00019 TreeMatchLB::TreeMatchLB(const CkLBOptions &opt): CBase_TreeMatchLB(opt)
00020 {
00021 lbname = (char*)"TreeMatchLB";
00022 if (CkMyPe() == 0 && !quietModeRequested)
00023 CkPrintf("CharmLB> TreeMatchLB created.\n");
00024 }
00025
00026 bool TreeMatchLB::QueryBalanceNow(int _step)
00027 {
00028 return true;
00029 }
00030
00031
00032 double *get_comm_speed(int *depth){
00033 double *res;
00034 int i;
00035
00036 *depth=5;
00037
00038 res=(double*)malloc(sizeof(double)*(*depth));
00039 res[0]=1;
00040
00041 for(i=1;i<*depth;i++){
00042 res[i]=res[i-1]*2;
00043 }
00044 return res;
00045 }
00046
00047 tm_topology_t *build_abe_topology(int nb_procs){
00048 int arity[4]={nb_procs,2,2,2};
00049 int nbring[8]={0,2,4,6,1,3,5,7};
00050 return build_synthetic_topology(arity,5,nbring,8);
00051 }
00052
00053 class TreeMatchLB::ProcLoadGreater {
00054 public:
00055 bool operator()(ProcInfo p1, ProcInfo p2) {
00056 return (p1.totalLoad() > p2.totalLoad());
00057 }
00058 };
00059
00060 class TreeMatchLB::ObjLoadGreater {
00061 public:
00062 bool operator()(Vertex v1, Vertex v2) {
00063 return (v1.getVertexLoad() > v2.getVertexLoad());
00064 }
00065 };
00066
00067 void TreeMatchLB::work(BaseLB::LDStats* stats)
00068 {
00072 ProcArray *parr = new ProcArray(stats);
00073 ObjGraph *ogr = new ObjGraph(stats);
00074
00076 parr->resetTotalLoad();
00077
00078 if (_lb_args.debug()>1)
00079 CkPrintf("[%d] In GreedyLB strategy\n",CkMyPe());
00080
00081 int vert;
00082
00083
00084 std::sort(ogr->vertices.begin(), ogr->vertices.end(), TreeMatchLB::ObjLoadGreater());
00085
00086 std::make_heap(parr->procs.begin(), parr->procs.end(), TreeMatchLB::ProcLoadGreater());
00087
00088 for(vert = 0; vert < ogr->vertices.size(); vert++) {
00089
00090 ProcInfo p = parr->procs.front();
00091 std::pop_heap(parr->procs.begin(), parr->procs.end(), TreeMatchLB::ProcLoadGreater());
00092 parr->procs.pop_back();
00093
00094
00095
00096 p.totalLoad() += ogr->vertices[vert].getVertexLoad();
00097 ogr->vertices[vert].setNewPe(p.getProcId());
00098
00099
00100 parr->procs.push_back(p);
00101 std::push_heap(parr->procs.begin(), parr->procs.end(), TreeMatchLB::ProcLoadGreater());
00102 }
00103
00105 ogr->convertDecisions(stats);
00106
00107
00112 int nb_procs;
00113 double **comm_mat;
00114 int i;
00115 int *object_mapping, *permutation;
00116
00117
00118
00119 nb_procs = stats->nprocs();
00120 object_mapping=stats->to_proc.getVec();
00121
00122
00123 stats->makeCommHash();
00124
00125 comm_mat=(double**)malloc(sizeof(double*)*nb_procs);
00126 for(i=0;i<nb_procs;i++){
00127 comm_mat[i]=(double*)calloc(nb_procs,sizeof(double));
00128 }
00129
00130
00131 for(i=0;i<stats->n_comm;i++){
00132 LDCommData &commData = stats->commData[i];
00133 if((!commData.from_proc())&&(commData.recv_type()==LD_OBJ_MSG)){
00134
00135 int from = object_mapping[stats->getHash(commData.sender)];
00136 int to = object_mapping[stats->getHash(commData.receiver.get_destObj())];
00137 if(from!=to){
00138 comm_mat[from][to]+=commData.bytes;
00139 comm_mat[to][from]+=commData.bytes;
00140 }
00141 }
00142 }
00143
00144
00145 tm_topology_t *topology=build_abe_topology(nb_procs);
00146 display_topology(topology);
00147
00148 tree_t *comm_tree=build_tree_from_topology(topology,comm_mat,nb_procs,NULL,NULL);
00149
00150
00151 permutation=(int*)malloc(sizeof(int)*nb_procs);
00152 map_topology_simple(topology,comm_tree,permutation,NULL);
00153
00154
00155
00156
00157
00158
00159
00160 for(i=0;i<nb_procs;i++)
00161 object_mapping[i]=permutation[object_mapping[i]];
00162
00163
00164 for(i=0;i<nb_procs;i++){
00165 free(comm_mat[i]);
00166 }
00167 free(comm_mat);
00168 free_topology(topology);
00169 }
00170