Checked in 2 fixes in the code
[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   removeNonMigratable(stats,count);
525
526         int *newmap = new int[stats->n_objs];
527
528         //CkPrintf("before computing partitions...\n");
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         /*CkPrintf("obj-proc mapping\n");
540         for(i=0;i<stats->n_objs;i++)
541                 CkPrintf(" %d,%d ",i,newmap[i]);
542         */
543         int max_objs = findMaxObjs(newmap,stats->n_objs,count);
544         
545         //Check to see if edgecut contains the reqd. no. of edges
546         
547         partgraph = new PartGraph(count,max_objs);
548
549         //Fill up the partition graph - first fill the nodes and then, the edges
550
551
552         for(i=0;i<stats->n_objs;i++)
553         {
554                 PartGraph::Node* n = &partgraph->nodes[newmap[i]];
555                 n->obj_list[n->num_objs]=i;
556                 n->num_objs++;
557         }
558
559         int *addedComm=new int[count];
560
561         int max_comm_part=-1;
562         
563         double max_comm=0;
564         
565         #ifdef RAND_COMM
566         for(i=0;i<count;i++){
567                 for(j=i+1;j<count;j++){
568                         int val;
569                         if(rand()%5==0)
570                                 val=0;
571                         else
572                                 val= rand()%1000;
573                                 
574                         partgraph->edges[i][j] = val;
575                         partgraph->edges[j][i] = val;
576                         
577                         partgraph->nodes[i].comm += val;
578                         partgraph->nodes[j].comm += val;
579                         
580                         if(partgraph->nodes[i].comm > max_comm){
581                                 max_comm = partgraph->nodes[i].comm;
582                                 max_comm_part = i;
583                         }
584                         if(partgraph->nodes[j].comm > max_comm){
585                                 max_comm = partgraph->nodes[j].comm;
586                                 max_comm_part = j;
587                         }
588                 }
589         }
590         #else
591         for(i=0;i<stats->n_comm;i++)
592         {
593                 //DO I consider other comm too....i.e. to or from a processor
594
595                 LDCommData &cdata = stats->commData[i];
596                 //if(cdata.from_proc() || cdata.receiver.get_type() != LD_OBJ_MSG)
597         //continue;
598                 if(!cdata.from_proc() && cdata.receiver.get_type() == LD_OBJ_MSG){
599         int senderID = stats->getHash(cdata.sender);
600         int recverID = stats->getHash(cdata.receiver.get_destObj());
601                         CmiAssert(senderID < stats->n_objs);
602                 CmiAssert(recverID < stats->n_objs);
603                 
604                         if(newmap[senderID]==newmap[recverID])
605                                 continue;
606         
607                         if(partgraph->edges[newmap[senderID]][newmap[recverID]] == 0){
608                                 partgraph->nodes[newmap[senderID]].degree++;
609                                 partgraph->nodes[newmap[recverID]].degree++;
610                         }
611                 
612                         partgraph->edges[newmap[senderID]][newmap[recverID]] += cdata.bytes;
613                         partgraph->edges[newmap[recverID]][newmap[senderID]] += cdata.bytes;
614                         
615                         partgraph->nodes[newmap[senderID]].comm += cdata.bytes;
616                         partgraph->nodes[newmap[recverID]].comm += cdata.bytes;
617                         
618                         if(partgraph->nodes[newmap[senderID]].comm > max_comm){
619                                 max_comm = partgraph->nodes[newmap[senderID]].comm;
620                                 max_comm_part = newmap[senderID];
621                         }
622                         if(partgraph->nodes[newmap[recverID]].comm > max_comm){
623                                 max_comm = partgraph->nodes[newmap[recverID]].comm;
624                                 max_comm_part = newmap[recverID];
625                         }
626
627                         //partgraph->edges[newmap[recverID]][newmap[senderID]] += 1000*(cdata.messages*alpha + cdata.bytes*beta);
628                         //bytesComm[newmap[senderID]][newmap[recverID]] += cdata.bytes;
629                         //bytesComm[newmap[recverID]][newmap[senderID]] += cdata.bytes;
630
631                 }
632                 else if(cdata.receiver.get_type() == LD_OBJLIST_MSG) {
633         //CkPrintf("\nin obj list cal...\n\n");
634                         int nobjs;
635         LDObjKey *objs = cdata.receiver.get_destObjs(nobjs);
636       int senderID = stats->getHash(cdata.sender);
637       //addedComm = new int[count];
638                         for(j=0;j<count;j++)
639                                 addedComm[j]=0;
640                         //CkPrintf("in obj list loop...\n");
641                         for (j=0; j<nobjs; j++) {
642         int recverID = stats->getHash(objs[j]);
643         if((senderID == -1)||(recverID == -1))
644                 if (_lb_args.migObjOnly()) continue;
645                 else CkAbort("Error in search\n");
646                                         
647                                 if(newmap[senderID]==newmap[recverID])
648                                         continue;
649         
650                                 //CkPrintf("from %d to %d\n",newmap[senderID],newmap[recverID]);
651                                 if(partgraph->edges[newmap[senderID]][newmap[recverID]] == 0){
652                                         //CkPrintf("sender:%d recver:%d\n",newmap[senderID],newmap[recverID]);
653                                         partgraph->nodes[newmap[senderID]].degree++;
654                                         partgraph->nodes[newmap[recverID]].degree++;
655                                 }
656                 
657                                 if(!addedComm[newmap[recverID]]){
658                                         partgraph->edges[newmap[senderID]][newmap[recverID]] += cdata.bytes;
659                                         partgraph->edges[newmap[recverID]][newmap[senderID]] += cdata.bytes;
660         
661                                         partgraph->nodes[newmap[senderID]].comm += cdata.bytes;
662                                         partgraph->nodes[newmap[recverID]].comm += cdata.bytes;
663
664                                         if(partgraph->nodes[newmap[senderID]].comm > max_comm){
665                                                 max_comm = partgraph->nodes[newmap[senderID]].comm;
666                                                 max_comm_part = newmap[senderID];
667                                         }
668                                         if(partgraph->nodes[newmap[recverID]].comm > max_comm){
669                                                 max_comm = partgraph->nodes[newmap[recverID]].comm;
670                                                 max_comm_part = newmap[recverID];
671                                         }
672
673                                         //bytesComm[newmap[senderID]][newmap[recverID]] += cdata.bytes;
674                                         //bytesComm[newmap[recverID]][newmap[senderID]] += cdata.bytes;
675                                         addedComm[newmap[recverID]]=1;
676                                 }
677       }
678                         //CkPrintf("outside loop..\n");
679         //delete [] addedComm;
680                 }
681
682         }
683         #endif
684         
685         int *proc_mapping = new int[count];
686         
687         delete [] addedComm;
688                 
689         LBtopoFn topofn;
690         int max_neighbors=0;
691         int *neigh;
692         int num_neigh=0;
693
694         char *lbcopy = strdup(_lbtopo);
695         char *ptr = strchr(lbcopy, ':');
696   if (ptr!=NULL){
697         ptr = strtok(lbcopy, ":");
698                 //CkPrintf("token:%s\n",ptr);
699         }
700         else
701                 ptr=lbcopy;
702
703         topofn = LBTopoLookup(ptr);
704   if (topofn == NULL) {
705         char str[1024];
706     CmiPrintf("TopoCentLB> Fatal error: Unknown topology: %s. Choose from:\n", ptr);
707     printoutTopo();
708     sprintf(str, "TopoCentLB> Fatal error: Unknown topology: %s", ptr);
709     CmiAbort(str);
710   }
711   
712         topo = topofn(count);
713
714         //double fourth = CmiWallTimer();
715         //CkPrintf("before calling algo...:%lf\n",(fourth-th));
716         calculateMST(partgraph,topo,proc_mapping,max_comm_part);
717         //CkPrintf("after calling algo...:%lf\n",(CmiWallTimer()-fourth));
718         //Returned partition graph is a Maximum Spanning Tree -- converted in above function itself
719
720         //Construct the Topology Graph
721         
722         /*CkPrintf("part,proc mapping..\n");
723         for(i=0;i<count;i++)
724                 CkPrintf("%d,%d len:%d\n",i,proc_mapping[i],(partgraph->nodes[i]).num_objs);
725                 */
726         int pe;
727         PartGraph::Node* n;
728         for(i=0;i<count;i++){
729                 pe = proc_mapping[i];
730                 //CkPrintf("%d %d\n",i,pe);
731                 n = &partgraph->nodes[i];
732                 //CkPrintf("here..%d,first object:%d\n",n->num_objs,n->obj_list[0]);
733                 for(j=0;j<n->num_objs;j++){
734                         stats->to_proc[n->obj_list[j]] = pe;
735                         if (_lb_args.debug()>1) 
736         CkPrintf("[%d] Obj %d migrating from %d to %d\n", CkMyPe(),n->obj_list[j],stats->from_proc[n->obj_list[j]],pe);
737                 }
738         }
739
740         delete[] newmap;
741         delete[] proc_mapping;
742         //Delete hopCount
743         for(i=0;i<count;i++)
744                 delete[] hopCount[i];
745
746         delete[] hopCount;
747         delete[] heapMapping;
748 }
749
750 #include "TopoCentLB.def.h"