00001
00010
00011 #include "TeamLB.h"
00012 #include "ckgraph.h"
00013 #include "metis.h"
00014
00015 CreateLBFunc_Def(TeamLB, "Use Metis(tm) to partition object graph at two levels: team level and processor level")
00016
00017 TeamLB::TeamLB(const CkLBOptions &opt): CentralLB(opt)
00018 {
00019 lbname = "TeamLB";
00020 if (CkMyPe() == 0)
00021 CkPrintf("[%d] TeamLB created\n",CkMyPe());
00022
00023
00024 teamSize = _lb_args.teamSize();
00025 numberTeams = CkNumPes() / teamSize;
00026 }
00027
00045 void TeamLB::work(LDStats* stats)
00046 {
00048 ProcArray *parr = new ProcArray(stats);
00049 ObjGraph *ogr = new ObjGraph(stats);
00050
00052 if (_lb_args.debug() >= 2) {
00053 CkPrintf("[%d] In TeamLB Strategy...\n", CkMyPe());
00054 }
00055
00056
00057 int numVertices = ogr->vertices.size();
00058 int numEdges = 0;
00059
00060 double maxLoad = 0.0;
00061 int maxBytes = 0, i, j, k;
00062
00065 for(i = 0; i < numVertices; i++) {
00066 if(ogr->vertices[i].getVertexLoad() > maxLoad)
00067 maxLoad = ogr->vertices[i].getVertexLoad();
00068 numEdges += ogr->vertices[i].sendToList.size();
00069 for(j = 0; j < ogr->vertices[i].sendToList.size(); j++) {
00070 if(ogr->vertices[i].sendToList[j].getNumBytes() > maxBytes)
00071 maxBytes = ogr->vertices[i].sendToList[j].getNumBytes();
00072 }
00073 }
00074
00075
00076 idxtype *xadj = new idxtype[numVertices + 1];
00077
00078 idxtype *adjncy = new idxtype[numEdges];
00079
00080 idxtype *vwgt = new idxtype[numVertices];
00081
00082 idxtype *adjwgt = new idxtype[numEdges];
00083
00084 int edgeNum = 0;
00085
00086 for(i = 0; i < numVertices; i++) {
00087 xadj[i] = edgeNum;
00088 vwgt[i] = (int)( (ogr->vertices[i].getVertexLoad() * 128) /maxLoad );
00089 for(j = 0; j < ogr->vertices[i].sendToList.size(); j++) {
00090 adjncy[edgeNum] = ogr->vertices[i].sendToList[j].getNeighborId();
00091 adjwgt[edgeNum] = (int)( (ogr->vertices[i].sendToList[j].getNumBytes() * 128) / maxBytes );
00092 edgeNum++;
00093 }
00094 }
00095 xadj[i] = edgeNum;
00096 CkAssert(edgeNum == numEdges);
00097
00098 int wgtflag = 3;
00099 int numflag = 0;
00100 int options[5];
00101 options[0] = 0;
00102 int edgecut;
00103 idxtype *pemap = new idxtype[numVertices];
00104
00105 if (_lb_args.debug() >= 1)
00106 CkPrintf("[%d] calling METIS_PartGraphRecursive.\n", CkMyPe());
00107
00108 METIS_PartGraphRecursive(&numVertices, xadj, adjncy, vwgt, adjwgt,
00109 &wgtflag, &numflag, &numberTeams, options, &edgecut, pemap);
00110
00111 int *global_pemap = new int[numVertices];
00112
00113
00114 if(teamSize > 1) {
00115 idxtype *team_xadj = new idxtype[numVertices + 1];
00116 idxtype *team_adjncy = new idxtype[numEdges];
00117 idxtype *team_vwgt = new idxtype[numVertices];
00118 idxtype *team_adjwgt = new idxtype[numEdges];
00119 idxtype *team_pemap = new idxtype[numVertices];
00120
00121 int teamEdgecut, node;
00122 int *mapping = new int[numVertices];
00123 int *invMapping = new int[numVertices];
00124
00125
00126 for(i=0; i<numberTeams; i++) {
00127 int teamMembers = 0;
00128
00129
00130
00131
00132 for(j = 0; j < numVertices; j++) {
00133 if(pemap[j] == i) {
00134 mapping[teamMembers] = j;
00135 invMapping[j] = teamMembers;
00136 team_vwgt[teamMembers] = vwgt[j];
00137 teamMembers++;
00138 }
00139 }
00140
00141
00142 int teamIndex = 0;
00143 for(j = 0; j < teamMembers; j++) {
00144 team_xadj[j] = teamIndex;
00145 for(k = xadj[mapping[j]]; k < xadj[mapping[j]+1]; k++) {
00146 node = adjncy[k];
00147 if(pemap[node] == i) {
00148 team_adjncy[teamIndex] = invMapping[node];
00149 team_adjwgt[teamIndex] = adjwgt[k];
00150 teamIndex++;
00151 }
00152 }
00153 }
00154 team_xadj[teamMembers] = teamIndex;
00155
00156
00157 METIS_PartGraphRecursive(&teamMembers, team_xadj, team_adjncy, team_vwgt,
00158 team_adjwgt, &wgtflag, &numflag, &teamSize, options,
00159 &teamEdgecut, team_pemap);
00160
00161
00162 for(j = 0; j < teamMembers; j++) {
00163 global_pemap[mapping[j]] = i*teamSize + team_pemap[j];
00164 }
00165
00166 }
00167
00168 delete[] team_xadj;
00169 delete[] team_adjncy;
00170 delete[] team_vwgt;
00171 delete[] team_adjwgt;
00172 delete[] team_pemap;
00173
00174 delete[] mapping;
00175 delete[] invMapping;
00176 } else {
00177 delete[] global_pemap;
00178 global_pemap = pemap;
00179 }
00180
00181 delete[] xadj;
00182 delete[] adjncy;
00183 delete[] vwgt;
00184 delete[] adjwgt;
00185
00186 if (_lb_args.debug() >= 1) {
00187 CkPrintf("[%d] TeamLB done! \n", CkMyPe());
00188 }
00189
00190 for(i = 0; i < numVertices; i++) {
00191 if(pemap[i] != ogr->vertices[i].getCurrentPe())
00192 ogr->vertices[i].setNewPe(pemap[i]);
00193 }
00194
00195 delete[] pemap;
00196
00198 ogr->convertDecisions(stats);
00199 }
00200
00201 #include "TeamLB.def.h"
00202