A bug fix. Added makeCommHash
[charm.git] / src / ck-ldb / TopoCentLB.C
1 /**************************************************************************
2 ** Amit Sharma (asharma6@uiuc.edu)
3 ** November 23, 2004
4 **
5 ** This is a topology conscious load balancer.
6 ** It migrates objects to new processors based on the topology in which the processors are connected.
7 ****************************************************************************/
8
9 #include <math.h>
10 #include <stdlib.h>
11 #include "charm++.h"
12 #include "cklists.h"
13 #include "CentralLB.h"
14
15 #include "TopoCentLB.decl.h"
16
17 #include "TopoCentLB.h"
18
19 #define alpha PER_MESSAGE_SEND_OVERHEAD_DEFAULT  /*Startup time per message, seconds*/
20 #define beta PER_BYTE_SEND_OVERHEAD_DEFAULT     /*Long-message time per byte, seconds*/
21 #define DEG_THRES 0.50
22
23 //#define MAX_EDGE
24 //#define RAND_COMM
25 #define make_mapping 0
26
27 CreateLBFunc_Def(TopoCentLB,"Balance objects based on the network topology");
28
29
30 /*static void lbinit (void)
31 {
32   LBRegisterBalancer ("TopoCentLB",
33                       CreateTopoCentLB,
34                       AllocateTopoCentLB,
35                       "Balance objects based on the network topology");
36 }*/
37
38
39 TopoCentLB::TopoCentLB(const CkLBOptions &opt) : CentralLB (opt)
40 {
41   lbname = "TopoCentLB";
42   if (CkMyPe () == 0) {
43     CkPrintf ("[%d] TopoCentLB created\n",CkMyPe());
44   }
45 }
46
47
48 CmiBool TopoCentLB::QueryBalanceNow (int _step)
49 {
50   return CmiTrue;
51 }
52
53 TopoCentLB::~TopoCentLB(){
54         if(partgraph)   delete partgraph;
55         if(topo) delete topo;
56 }
57
58
59 void TopoCentLB::computePartitions(CentralLB::LDStats *stats,int count,int *newmap)
60 {
61         
62   int numobjs = stats->n_objs;
63         int i, j, m;
64
65   // allocate space for the computing data
66   double *objtime = new double[numobjs];
67   int *objwt = new int[numobjs];
68   int *origmap = new int[numobjs];
69   LDObjHandle *handles = new LDObjHandle[numobjs];
70   
71         for(i=0;i<numobjs;i++) {
72     objtime[i] = 0.0;
73     objwt[i] = 0;
74     origmap[i] = 0;
75   }
76
77   for (i=0; i<stats->n_objs; i++) {
78     LDObjData &odata = stats->objData[i];
79     if (!odata.migratable) 
80       CmiAbort("MetisLB doesnot dupport nonmigratable object.\n");
81     int frompe = stats->from_proc[i];
82     origmap[i] = frompe;
83     objtime[i] = odata.wallTime*stats->procs[frompe].pe_speed;
84     handles[i] = odata.handle;
85   }
86
87   // to convert the weights on vertices to integers
88   double max_objtime = objtime[0];
89   for(i=0; i<numobjs; i++) {
90     if(max_objtime < objtime[i])
91       max_objtime = objtime[i];
92   }
93         int maxobj=0;
94         int totalwt=0;
95   double ratio = 1000.0/max_objtime;
96   for(i=0; i<numobjs; i++) {
97       objwt[i] = (int)(objtime[i]*ratio);
98                         if(maxobj<objwt[i])
99                                 maxobj=objwt[i];
100                         totalwt+=objwt[i];
101   }
102         
103         //CkPrintf("\nmax obj wt :%d \n totalwt before :%d \n\n",maxobj,totalwt);
104         
105   int **comm = new int*[numobjs];
106   for (i=0; i<numobjs; i++) {
107     comm[i] = new int[numobjs];
108     for (j=0; j<numobjs; j++)  {
109       comm[i][j] = 0;
110     }
111   }
112
113   const int csz = stats->n_comm;
114   for(i=0; i<csz; i++) {
115       LDCommData &cdata = stats->commData[i];
116       //if(cdata.from_proc() || cdata.receiver.get_type() != LD_OBJ_MSG)
117         //continue;
118                         if(!cdata.from_proc() && cdata.receiver.get_type() == LD_OBJ_MSG){
119         int senderID = stats->getHash(cdata.sender);
120         int recverID = stats->getHash(cdata.receiver.get_destObj());
121         CmiAssert(senderID < numobjs);
122         CmiAssert(recverID < numobjs);
123         //comm[senderID][recverID] += (int)(cdata.messages*alpha + cdata.bytes*beta);
124         //comm[recverID][senderID] += (int)(cdata.messages*alpha + cdata.bytes*beta);
125                 //CkPrintf("in compute partitions...%d\n",comm[senderID][recverID]);
126                                 comm[senderID][recverID] += cdata.messages;
127         comm[recverID][senderID] += cdata.messages;
128
129                                 //Use bytes or messages -- do i include messages for objlist too...??
130                         }
131                         else if (cdata.receiver.get_type() == LD_OBJLIST_MSG) {
132                                 //CkPrintf("in objlist..\n");
133         int nobjs;
134         LDObjKey *objs = cdata.receiver.get_destObjs(nobjs);
135         int senderID = stats->getHash(cdata.sender);
136         for (j=0; j<nobjs; j++) {
137            int recverID = stats->getHash(objs[j]);
138            if((senderID == -1)||(recverID == -1))
139               if (_lb_args.migObjOnly()) continue;
140               else CkAbort("Error in search\n");
141            comm[senderID][recverID] += cdata.messages;
142            comm[recverID][senderID] += cdata.messages;
143         }
144                         }
145                 }
146
147 // ignore messages sent from an object to itself
148   for (i=0; i<numobjs; i++)
149     comm[i][i] = 0;
150
151   // construct the graph in CSR format
152   int *xadj = new int[numobjs+1];
153   int numedges = 0;
154   for(i=0;i<numobjs;i++) {
155     for(j=0;j<numobjs;j++) {
156       if(comm[i][j] != 0)
157         numedges++;
158     }
159   }
160   int *adjncy = new int[numedges];
161   int *edgewt = new int[numedges];
162         int factor = 10;
163   xadj[0] = 0;
164   int count4all = 0;
165   for (i=0; i<numobjs; i++) {
166     for (j=0; j<numobjs; j++) { 
167       if (comm[i][j] != 0) { 
168         adjncy[count4all] = j;
169         edgewt[count4all++] = comm[i][j]/factor;
170       }
171     }
172     xadj[i+1] = count4all;
173   }
174
175   /*if (_lb_args.debug() >= 2) {
176   CkPrintf("Pre-LDB Statistics step %d\n", step());
177   printStats(count, numobjs, objtime, comm, origmap);
178   }*/
179
180   int wgtflag = 3; // Weights both on vertices and edges
181   int numflag = 0; // C Style numbering
182   int options[5];
183   int edgecut;
184   int sameMapFlag = 1;
185
186         options[0] = 0;
187
188   if (count < 1) {
189     CkPrintf("error: Number of Pe less than 1!");
190   }
191   else if (count == 1) {
192         for(m=0;m<numobjs;m++) 
193                         newmap[i] = origmap[i];
194         sameMapFlag = 1;
195   }
196   else {
197         sameMapFlag = 0;
198                 /*
199         if (count > 8)
200                         METIS_PartGraphKway(&numobjs, xadj, adjncy, objwt, edgewt, 
201                             &wgtflag, &numflag, &count, options, 
202                             &edgecut, newmap);
203           else
204                         METIS_PartGraphRecursive(&numobjs, xadj, adjncy, objwt, edgewt, 
205                                  &wgtflag, &numflag, &count, options, 
206                                  &edgecut, newmap);
207                 */
208    //CkPrintf("before calling metis.\n");
209                 METIS_PartGraphRecursive(&numobjs, xadj, adjncy, objwt, edgewt,
210                                  &wgtflag, &numflag, &count, options,
211                                  &edgecut, newmap);
212          //CkPrintf("after calling Metis function.\n");
213   }
214          
215   /*if (_lb_args.debug() >= 2) {
216         CkPrintf("Post-LDB Statistics step %d\n", step());
217         printStats(count, numobjs, objtime, comm, newmap);
218   }*/
219
220   for(i=0;i<numobjs;i++)
221     delete[] comm[i];
222   delete[] comm;
223   delete[] objtime;
224   delete[] xadj;
225   delete[] adjncy;
226   delete[] objwt;
227   delete[] edgewt;
228         delete[] handles;
229
230         //CkPrintf("chking wts on each partition...\n");
231
232         /*int avg=0;
233         int *chkwt = new int[count];
234         for(i=0;i<count;i++)
235                 chkwt[i]=0;
236         //totalwt=0;
237         for(i=0;i<numobjs;i++){
238                 chkwt[newmap[i]] += objwt[i];
239                 avg += objwt[i];
240                 
241         }
242         
243         
244         for(i=0;i<count;i++)
245                 CkPrintf("%d -- %d\n",i,chkwt[i]);
246         
247         //CkPrintf("totalwt after:%d avg is %d\n",avg,avg/count);
248 */
249   /*if(!sameMapFlag) {
250     for(i=0; i<numobjs; i++) {
251       if(origmap[i] != newmap[i]) {
252                                 CmiAssert(stats->from_proc[i] == origmap[i]);
253                                 ///Dont assign....wait........
254                                 stats->to_proc[i] =  newmap[i];
255                                 if (_lb_args.debug() >= 3)
256           CkPrintf("[%d] Obj %d migrating from %d to %d\n", CkMyPe(),i,stats->from_proc[i],stats->to_proc[i]);
257       }
258     }
259   }*/
260
261   delete[] origmap;
262
263 }
264
265 int TopoCentLB::findMaxObjs(int *map,int totalobjs,int count)
266 {
267         int *max_num = new int[count];
268         int i;
269         int maxobjs=0;
270         
271         for(i=0;i<count;i++)
272                 max_num[i]=0;
273                 
274         for(i=0;i<totalobjs;i++)
275                 max_num[map[i]]++;
276         
277         for(i=0;i<count;i++)
278                 if(max_num[i]>maxobjs)
279                         maxobjs = max_num[i];
280         
281         delete[] max_num;
282
283         return maxobjs;
284 }
285
286 void TopoCentLB::Heapify(HeapNode *heap, int node, int heapSize)
287 {
288   int left = 2*node+1;
289   int right = 2*node+2;
290   int xchange;
291         
292   //heap[left].load > heap[node].load)
293   if (left < heapSize && (heap[left].key > heap[node].key))
294     xchange = left;
295   else 
296                 xchange = node;
297   //heap[right].load > heap[xchange].load) 
298   if (right < heapSize && (heap[right].key > heap[xchange].key))
299     xchange = right;
300
301   if (xchange != node) {
302     HeapNode tmp;
303     tmp = heap[node];
304     heap[node] = heap[xchange];
305     heap[xchange] = tmp;
306                 heapMapping[heap[node].node]=node;
307                 heapMapping[heap[xchange].node]=xchange;
308     Heapify(heap,xchange,heapSize);
309   }    
310 }
311
312
313 TopoCentLB::HeapNode TopoCentLB::extractMax(HeapNode *heap,int *heapSize){
314
315         if(*heapSize < 1)
316                 CmiAbort("Empty Heap passed to extractMin!\n");
317
318         HeapNode max = heap[0];
319         heap[0] = heap[*heapSize-1];
320         heapMapping[heap[0].node]=0;
321         *heapSize = *heapSize - 1;
322         Heapify(heap,0,*heapSize);
323         return max;
324 }
325
326 void TopoCentLB::BuildHeap(HeapNode *heap,int heapSize){
327         for(int i=heapSize/2; i >= 0; i--)
328                 Heapify(heap,i,heapSize);
329 }
330
331 void TopoCentLB :: increaseKey(HeapNode *heap,int i,double wt){
332         int val;
333         if(wt != -1.00){
334                 #ifdef MAX_EDGE
335                         //CkPrintf("me here...\n");
336                         if(wt>heap[i].key)
337                                 heap[i].key = wt;
338                 #else
339                         heap[i].key += wt;
340                 #endif
341         }
342         int parent = (i-1)/2;
343         
344         if(heap[parent].key >= heap[i].key)
345                 return;
346         else {
347                 HeapNode tmp = heap[parent];
348                 heap[parent] = heap[i];
349                 heap[i] = tmp;
350                 heapMapping[heap[parent].node]=parent;
351                 heapMapping[heap[i].node]=i;
352                 increaseKey(heap,parent,-1.00);
353         }
354 }
355
356 void TopoCentLB :: calculateMST(PartGraph *partgraph,LBTopology *topo,int *proc_mapping,int max_comm_part){
357
358         //Edge **markedEdges;
359         //int *parent;
360         int *inHeap;
361         double *keys;
362         int count = partgraph->n_nodes;
363         int i=0,j=0;
364         
365         //parent = new int[partgraph->n_nodes];
366         inHeap = new int[partgraph->n_nodes];
367         keys = new double[partgraph->n_nodes];
368
369         //int **hopCount;
370         //int **topoGraph;
371         //int *topoDegree;
372         int *assigned_procs = new int[count];
373
374         hopCount = new double*[count];
375         for(i=0;i<count;i++){
376                 proc_mapping[i]=-1;
377                 assigned_procs[i]=0;
378                 hopCount[i] = new double[count];
379                 for(j=0;j<count;j++)
380                         hopCount[i][j] = 0;
381         }
382
383         //CkPrintf("getting hopcount\n");
384         //double start=CmiWallTimer();
385         topo->get_pairwise_hop_count(hopCount);
386         //CkPrintf("after getting hopcount:%lf\n",(CmiWallTimer()-start));
387         int max_neighbors = topo->max_neighbors();
388         
389         HeapNode *heap = new HeapNode[partgraph->n_nodes];
390         heapMapping = new int[partgraph->n_nodes];
391         
392         int heapSize = 0;
393
394         for(i=0;i<partgraph->n_nodes;i++){
395                 heap[i].key = 0.00;
396                 heap[i].node = i;
397                 keys[i] = 0.00;
398                 //parent[i] = -1;
399                 inHeap[i] = 1;
400                 heapMapping[i]=i;
401                 /*if(i==0){ //Take 0 as root
402                         heap[0].key = 1.00;
403                         keys[0] = 0;
404                 }*/
405         }
406
407         //Assign the max comm partition first
408         heap[max_comm_part].key = 1.00;
409         
410         heapSize = partgraph->n_nodes;
411         BuildHeap(heap,heapSize);
412
413         int k=0,comm_cnt=0,m=0,n=0;
414         int *commParts = new int[partgraph->n_nodes];
415         
416         //srand(count);
417
418         while(heapSize > 0){
419                 HeapNode max = extractMax(heap,&heapSize);
420                 inHeap[max.node] = 0;
421
422                 //for(j=0;j<heapSize;j++)
423                         //heapMapping[heap[j].node]=j;
424
425                 for(i=0;i<partgraph->n_nodes;i++){
426                         commParts[i]=-1;
427                         PartGraph::Edge wt = partgraph->edges[max.node][i];
428                         if(wt == 0)
429                                 continue;
430                         if(inHeap[i]){
431                                 //parent[i] = max.node;
432                         #ifdef MAX_EDGE
433                                 if(wt>keys[i])
434                                         keys[i]=wt;
435                         #else
436                                 keys[i] += wt;
437                         #endif
438                                 //Update in the heap too
439                                 //First, find where this node is..in the heap
440                                 /*for(j=0;j<heapSize;j++)
441                                         if(heap[j].node == i)
442                                                 break;
443                                 if(j==heapSize)
444                                         CmiAbort("Some error in heap...\n");*/
445                                 increaseKey(heap,heapMapping[i],wt);
446                         }
447                 }
448                 
449                 if(heapSize == partgraph->n_nodes-1){ //For now, assign max comm partition to 0th proc in the topology
450                         proc_mapping[max.node]=0;
451                         assigned_procs[0]=1;
452                         continue;
453                 }
454                 
455                 m=0;
456                 n=0;
457                 comm_cnt=0;
458
459                 int pot_proc=-1;
460                 double min_cost=-1;
461                 int min_cost_index=-1;
462                 double cost=0;
463                 int p=0;
464                 //int q=0;
465
466                 for(k=0;k<partgraph->n_nodes;k++){
467                         if(!inHeap[k] && partgraph->edges[k][max.node]){
468                                 commParts[comm_cnt]=k;
469                                 comm_cnt++;
470                         }
471                 }
472
473                 for(m=0;m<count;m++){
474                         if(!assigned_procs[m]){
475                                 cost=0;
476                                 for(p=0;p<comm_cnt;p++){
477                                         //if(!hopCount[proc_mapping[commParts[p]]][m])
478                                                 //hopCount[proc_mapping[commParts[p]]][m]=topo->get_hop_count(proc_mapping[commParts[p]],m);
479                                         cost += hopCount[proc_mapping[commParts[p]]][m]*partgraph->edges[commParts[p]][max.node];
480                                         //CkPrintf("hop count:%d\n",hopCount[proc_mapping[commParts[p]]][pot_proc]);
481                                 }
482                                 if(min_cost==-1 || cost<min_cost){
483                                         min_cost=cost;
484                                         min_cost_index=m;
485                                 }
486                         }
487                 }
488
489                 proc_mapping[max.node]=min_cost_index;
490                 assigned_procs[min_cost_index]=1;
491         }
492
493         //clear up memory
494         delete[] inHeap;
495         delete[] keys;
496         delete[] assigned_procs;
497         delete[] heap;
498         delete[] commParts;
499 }
500
501
502 void TopoCentLB :: work(CentralLB::LDStats *stats,int count)
503 {
504   int proc;
505   int obj;
506   int dest;
507   int i,j;
508         LDObjData *odata;
509         
510         //if (_lb_args.debug() >= 2) {
511     CkPrintf("In TopoCentLB Strategy...\n");
512   //}
513         //double start = CmiWallTimer();
514   // Make sure that there is at least one available processor.
515   for (proc = 0; proc < count; proc++) {
516     if (stats->procs[proc].available) {
517       break;
518     }
519   }
520         if (proc == count) {
521     CmiAbort ("TopoCentLB: no available processors!");
522   }
523
524   
525   removeNonMigratable(stats,count);
526
527         int *newmap = new int[stats->n_objs];
528
529
530         if(make_mapping)
531                 computePartitions(stats,count,newmap);
532         else{
533                 //mapping taken from previous algo
534                 for(i=0;i<stats->n_objs;i++){
535       newmap[i]=stats->from_proc[i];
536     }
537         }
538         
539         /*for(i=0;i<stats->n_objs;i++)
540                 CkPrintf(" %d,%d ",i,newmap[i]);
541         */
542         int max_objs = findMaxObjs(newmap,stats->n_objs,count);
543         
544         //Check to see if edgecut contains the reqd. no. of edges
545         
546         partgraph = new PartGraph(count,max_objs);
547
548         //Fill up the partition graph - first fill the nodes and then, the edges
549
550
551         for(i=0;i<stats->n_objs;i++)
552         {
553                 PartGraph::Node* n = &partgraph->nodes[newmap[i]];
554                 n->obj_list[n->num_objs]=i;
555                 n->num_objs++;
556         }
557
558         int *addedComm=new int[count];
559   
560   stats->makeCommHash();
561   
562         int max_comm_part=-1;
563         
564         double max_comm=0;
565         
566         #ifdef RAND_COMM
567         for(i=0;i<count;i++){
568                 for(j=i+1;j<count;j++){
569                         int val;
570                         if(rand()%5==0)
571                                 val=0;
572                         else
573                                 val= rand()%1000;
574                                 
575                         partgraph->edges[i][j] = val;
576                         partgraph->edges[j][i] = val;
577                         
578                         partgraph->nodes[i].comm += val;
579                         partgraph->nodes[j].comm += val;
580                         
581                         if(partgraph->nodes[i].comm > max_comm){
582                                 max_comm = partgraph->nodes[i].comm;
583                                 max_comm_part = i;
584                         }
585                         if(partgraph->nodes[j].comm > max_comm){
586                                 max_comm = partgraph->nodes[j].comm;
587                                 max_comm_part = j;
588                         }
589                 }
590         }
591         #else
592         for(i=0;i<stats->n_comm;i++)
593         {
594                 //DO I consider other comm too....i.e. to or from a processor
595
596                 LDCommData &cdata = stats->commData[i];
597                 //if(cdata.from_proc() || cdata.receiver.get_type() != LD_OBJ_MSG)
598         //continue;
599                 if(!cdata.from_proc() && cdata.receiver.get_type() == LD_OBJ_MSG){
600         int senderID = stats->getHash(cdata.sender);
601         int recverID = stats->getHash(cdata.receiver.get_destObj());
602                         CmiAssert(senderID < stats->n_objs);
603                 CmiAssert(recverID < stats->n_objs);
604                 
605                         if(newmap[senderID]==newmap[recverID])
606                                 continue;
607         
608                         if(partgraph->edges[newmap[senderID]][newmap[recverID]] == 0){
609                                 partgraph->nodes[newmap[senderID]].degree++;
610                                 partgraph->nodes[newmap[recverID]].degree++;
611                         }
612                 
613                         partgraph->edges[newmap[senderID]][newmap[recverID]] += cdata.bytes;
614                         partgraph->edges[newmap[recverID]][newmap[senderID]] += cdata.bytes;
615                         
616                         partgraph->nodes[newmap[senderID]].comm += cdata.bytes;
617                         partgraph->nodes[newmap[recverID]].comm += cdata.bytes;
618                         
619                         if(partgraph->nodes[newmap[senderID]].comm > max_comm){
620                                 max_comm = partgraph->nodes[newmap[senderID]].comm;
621                                 max_comm_part = newmap[senderID];
622                         }
623                         if(partgraph->nodes[newmap[recverID]].comm > max_comm){
624                                 max_comm = partgraph->nodes[newmap[recverID]].comm;
625                                 max_comm_part = newmap[recverID];
626                         }
627
628                         //partgraph->edges[newmap[recverID]][newmap[senderID]] += 1000*(cdata.messages*alpha + cdata.bytes*beta);
629                         //bytesComm[newmap[senderID]][newmap[recverID]] += cdata.bytes;
630                         //bytesComm[newmap[recverID]][newmap[senderID]] += cdata.bytes;
631
632                 }
633                 else if(cdata.receiver.get_type() == LD_OBJLIST_MSG) {
634         //CkPrintf("\nin obj list cal...\n\n");
635                         int nobjs;
636         LDObjKey *objs = cdata.receiver.get_destObjs(nobjs);
637       int senderID = stats->getHash(cdata.sender);
638       //addedComm = new int[count];
639                         for(j=0;j<count;j++)
640                                 addedComm[j]=0;
641                         //CkPrintf("in obj list loop...\n");
642                         for (j=0; j<nobjs; j++) {
643         int recverID = stats->getHash(objs[j]);
644         if((senderID == -1)||(recverID == -1))
645                 if (_lb_args.migObjOnly()) continue;
646                 else CkAbort("Error in search\n");
647                                         
648                                 if(newmap[senderID]==newmap[recverID])
649                                         continue;
650         
651                                 //CkPrintf("from %d to %d\n",newmap[senderID],newmap[recverID]);
652                                 if(partgraph->edges[newmap[senderID]][newmap[recverID]] == 0){
653                                         //CkPrintf("sender:%d recver:%d\n",newmap[senderID],newmap[recverID]);
654                                         partgraph->nodes[newmap[senderID]].degree++;
655                                         partgraph->nodes[newmap[recverID]].degree++;
656                                 }
657                 
658                                 if(!addedComm[newmap[recverID]]){
659                                         partgraph->edges[newmap[senderID]][newmap[recverID]] += cdata.bytes;
660                                         partgraph->edges[newmap[recverID]][newmap[senderID]] += cdata.bytes;
661         
662                                         partgraph->nodes[newmap[senderID]].comm += cdata.bytes;
663                                         partgraph->nodes[newmap[recverID]].comm += cdata.bytes;
664
665                                         if(partgraph->nodes[newmap[senderID]].comm > max_comm){
666                                                 max_comm = partgraph->nodes[newmap[senderID]].comm;
667                                                 max_comm_part = newmap[senderID];
668                                         }
669                                         if(partgraph->nodes[newmap[recverID]].comm > max_comm){
670                                                 max_comm = partgraph->nodes[newmap[recverID]].comm;
671                                                 max_comm_part = newmap[recverID];
672                                         }
673
674                                         //bytesComm[newmap[senderID]][newmap[recverID]] += cdata.bytes;
675                                         //bytesComm[newmap[recverID]][newmap[senderID]] += cdata.bytes;
676                                         addedComm[newmap[recverID]]=1;
677                                 }
678       }
679                         //CkPrintf("outside loop..\n");
680         //delete [] addedComm;
681                 }
682
683         }
684         #endif
685         
686         int *proc_mapping = new int[count];
687         
688         delete [] addedComm;
689                 
690         LBtopoFn topofn;
691         int max_neighbors=0;
692         int *neigh;
693         int num_neigh=0;
694
695         char *lbcopy = strdup(_lbtopo);
696         char *ptr = strchr(lbcopy, ':');
697   if (ptr!=NULL){
698         ptr = strtok(lbcopy, ":");
699                 //CkPrintf("token:%s\n",ptr);
700         }
701         else
702                 ptr=lbcopy;
703
704         topofn = LBTopoLookup(ptr);
705   if (topofn == NULL) {
706         char str[1024];
707     CmiPrintf("TopoCentLB> Fatal error: Unknown topology: %s. Choose from:\n", ptr);
708     printoutTopo();
709     sprintf(str, "TopoCentLB> Fatal error: Unknown topology: %s", ptr);
710     CmiAbort(str);
711   }
712   
713         topo = topofn(count);
714
715         //double fourth = CmiWallTimer();
716         calculateMST(partgraph,topo,proc_mapping,max_comm_part);
717         //Returned partition graph is a Maximum Spanning Tree -- converted in above function itself
718
719         //Construct the Topology Graph
720         
721         /*CkPrintf("part,proc mapping..\n");
722         for(i=0;i<count;i++)
723                 CkPrintf("%d,%d len:%d\n",i,proc_mapping[i],(partgraph->nodes[i]).num_objs);
724                 */
725         int pe;
726         PartGraph::Node* n;
727         for(i=0;i<count;i++){
728                 pe = proc_mapping[i];
729                 //CkPrintf("%d %d\n",i,pe);
730                 n = &partgraph->nodes[i];
731                 //CkPrintf("here..%d,first object:%d\n",n->num_objs,n->obj_list[0]);
732                 for(j=0;j<n->num_objs;j++){
733                         stats->to_proc[n->obj_list[j]] = pe;
734                         if (_lb_args.debug()>1) 
735         CkPrintf("[%d] Obj %d migrating from %d to %d\n", CkMyPe(),n->obj_list[j],stats->from_proc[n->obj_list[j]],pe);
736                 }
737         }
738
739         delete[] newmap;
740         delete[] proc_mapping;
741         //Delete hopCount
742         for(i=0;i<count;i++)
743                 delete[] hopCount[i];
744
745         delete[] hopCount;
746         delete[] heapMapping;
747 }
748
749 #include "TopoCentLB.def.h"