Added support for newly added Torus ND topology and fixed some memory leakages
[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     Heapify(heap,xchange,heapSize);
307   }    
308 }
309
310
311 TopoCentLB::HeapNode TopoCentLB::extractMax(HeapNode *heap,int *heapSize){
312
313         if(*heapSize < 1)
314                 CmiAbort("Empty Heap passed to extractMin!\n");
315
316         HeapNode max = heap[0];
317         heap[0] = heap[*heapSize-1];
318         *heapSize = *heapSize - 1;
319         Heapify(heap,0,*heapSize);
320         return max;
321 }
322
323 void TopoCentLB::BuildHeap(HeapNode *heap,int heapSize){
324         for(int i=heapSize/2; i >= 0; i--)
325                 Heapify(heap,i,heapSize);
326 }
327
328 void TopoCentLB :: increaseKey(HeapNode *heap,int i,double wt){
329         
330         if(wt != -1.00){
331                 #ifdef MAX_EDGE
332                         //CkPrintf("me here...\n");
333                         if(wt>heap[i].key)
334                                 heap[i].key = wt;
335                 #else
336                         heap[i].key += wt;
337                 #endif
338         }
339         int parent = (i-1)/2;
340         
341         if(heap[parent].key >= heap[i].key)
342                 return;
343         else {
344                 HeapNode tmp = heap[parent];
345                 heap[parent] = heap[i];
346                 heap[i] = tmp;
347                 increaseKey(heap,parent,-1.00);
348         }
349         
350 }
351
352 void TopoCentLB :: calculateMST(PartGraph *partgraph,LBTopology *topo,int *proc_mapping,int max_comm_part){
353
354         //Edge **markedEdges;
355         //int *parent;
356         int *inHeap;
357         double *keys;
358         int count = partgraph->n_nodes;
359         int i=0,j=0;
360         
361         //parent = new int[partgraph->n_nodes];
362         inHeap = new int[partgraph->n_nodes];
363         keys = new double[partgraph->n_nodes];
364
365         //int **hopCount;
366         //int **topoGraph;
367         //int *topoDegree;
368         int *assigned_procs = new int[count];
369
370         hopCount = new double*[count];
371         for(i=0;i<count;i++){
372                 proc_mapping[i]=-1;
373                 assigned_procs[i]=0;
374                 hopCount[i] = new double[count];
375                 //for(j=0;j<count;j++)
376                         //hopCount[i][j] = 0;
377         }
378
379         topo->get_pairwise_hop_count(hopCount);
380
381         int max_neighbors = topo->max_neighbors();
382         
383         HeapNode *heap = new HeapNode[partgraph->n_nodes];
384         int heapSize = 0;
385
386         for(i=0;i<partgraph->n_nodes;i++){
387                 heap[i].key = 0.00;
388                 heap[i].node = i;
389                 keys[i] = 0.00;
390                 //parent[i] = -1;
391                 inHeap[i] = 1;
392                 
393                 /*if(i==0){ //Take 0 as root
394                         heap[0].key = 1.00;
395                         keys[0] = 0;
396                 }*/
397         }
398
399         //Assign the max comm partition first
400         heap[max_comm_part].key = 1.00;
401         
402         //CkPrintf("in calculate MST..\n");
403         heapSize = partgraph->n_nodes;
404         BuildHeap(heap,heapSize);
405
406         int k=0,comm_cnt=0,m=0,n=0;
407         int *commParts = new int[partgraph->n_nodes];
408         
409         //srand(count);
410
411         while(heapSize > 0){
412                 HeapNode max = extractMax(heap,&heapSize);
413                 //CkPrintf("in the extracting loop..\n");
414                 inHeap[max.node] = 0;
415
416                 //CkPrintf("extracted part..%d with weight :%.1f\n",max.node,max.key);
417                 
418                 for(i=0;i<partgraph->n_nodes;i++){
419                         commParts[i]=-1;
420                         PartGraph::Edge wt = partgraph->edges[max.node][i];
421                         if(wt == 0)
422                                 continue;
423                         if(inHeap[i]){
424                                 //parent[i] = max.node;
425                         #ifdef MAX_EDGE
426                                 if(wt>keys[i])
427                                         keys[i]=wt;
428                         #else
429                                 keys[i] += wt;
430                         #endif
431                                 //Update in the heap too
432                                 //First, find where this node is..in the heap
433                                 for(j=0;j<heapSize;j++)
434                                         if(heap[j].node == i)
435                                                 break;
436                                 if(j==heapSize)
437                                         CmiAbort("Some error in heap...\n");
438                                 increaseKey(heap,j,wt);
439                         }
440                 }
441                 
442                 if(max.node==0){ //For now, assign max comm partition to 0th proc in the topology
443                         proc_mapping[max.node]=0;
444                         assigned_procs[0]=1;
445                         continue;
446                 }
447                 
448                 m=0;
449                 n=0;
450                 comm_cnt=0;
451
452                 int pot_proc=-1;
453                 double min_cost=-1;
454                 int min_cost_index=-1;
455                 double cost=0;
456                 int p=0;
457                 //int q=0;
458
459                 for(k=0;k<partgraph->n_nodes;k++){
460                         if(!inHeap[k] && partgraph->edges[k][max.node]){
461                                 commParts[comm_cnt]=k;
462                                 comm_cnt++;
463                         }
464                 }
465
466                 for(m=0;m<count;m++){
467                         if(!assigned_procs[m]){
468                                 cost=0;
469                                 for(p=0;p<comm_cnt;p++){
470                                         //if(!hopCount[proc_mapping[commParts[p]]][m])
471                                                 //hopCount[proc_mapping[commParts[p]]][m]=topo->get_hop_count(proc_mapping[commParts[p]],m);
472                                         cost += hopCount[proc_mapping[commParts[p]]][m]*partgraph->edges[commParts[p]][max.node];
473                                         //CkPrintf("hop count:%d\n",hopCount[proc_mapping[commParts[p]]][pot_proc]);
474                                 }
475                                 if(min_cost==-1 || cost<min_cost){
476                                         min_cost=cost;
477                                         min_cost_index=m;
478                                 }
479                         }
480                 }
481
482                 //CkPrintf("\npart %d assigned to proc %d\n",max.node,min_cost_index);
483                 proc_mapping[max.node]=min_cost_index;
484                 assigned_procs[min_cost_index]=1;
485         }
486
487         //clear up memory
488         delete[] inHeap;
489         delete[] keys;
490         delete[] assigned_procs;
491         delete[] heap;
492         delete[] commParts;
493 }
494
495
496 void TopoCentLB :: work(CentralLB::LDStats *stats,int count)
497 {
498   int proc;
499   int obj;
500   int dest;
501   int i,j;
502         LDObjData *odata;
503         
504         //if (_lb_args.debug() >= 2) {
505     CkPrintf("In TopoCentLB Strategy...\n");
506   //}
507         
508   // Make sure that there is at least one available processor.
509   for (proc = 0; proc < count; proc++) {
510     if (stats->procs[proc].available) {
511       break;
512     }
513   }
514         if (proc == count) {
515     CmiAbort ("TopoCentLB: no available processors!");
516   }
517  
518   removeNonMigratable(stats,count);
519
520         int *newmap = new int[stats->n_objs];
521
522         //CkPrintf("before computing partitions...\n");
523
524         if(make_mapping)
525                 computePartitions(stats,count,newmap);
526         else{
527                 //mapping taken from previous algo
528                 for(i=0;i<stats->n_objs;i++){
529       newmap[i]=stats->from_proc[i];
530     }
531         }
532                 
533         /*CkPrintf("obj-proc mapping\n");
534         for(i=0;i<stats->n_objs;i++)
535                 CkPrintf(" %d,%d ",i,newmap[i]);
536         */
537         int max_objs = findMaxObjs(newmap,stats->n_objs,count);
538         
539         //Check to see if edgecut contains the reqd. no. of edges
540         
541         partgraph = new PartGraph(count,max_objs);
542
543         //Fill up the partition graph - first fill the nodes and then, the edges
544         //CkPrintf("after computing partitions...\n");
545
546
547         for(i=0;i<stats->n_objs;i++)
548         {
549                 PartGraph::Node* n = &partgraph->nodes[newmap[i]];
550                 //CkPrintf("num:%d\n",n->num_objs);
551                 n->obj_list[n->num_objs]=i;
552                 n->num_objs++;
553         }
554
555         //CkPrintf("num comm:%d\n",stats->n_comm);
556         int *addedComm=new int[count];
557
558         int max_comm_part=-1;
559         
560         double max_comm=0;
561         
562         #ifdef RAND_COMM
563         for(i=0;i<count;i++){
564                 for(j=i+1;j<count;j++){
565                         int val;
566                         if(rand()%5==0)
567                                 val=0;
568                         else
569                                 val= rand()%1000;
570                                 
571                         partgraph->edges[i][j] = val;
572                         partgraph->edges[j][i] = val;
573                         
574                         partgraph->nodes[i].comm += val;
575                         partgraph->nodes[j].comm += val;
576                         
577                         if(partgraph->nodes[i].comm > max_comm){
578                                 max_comm = partgraph->nodes[i].comm;
579                                 max_comm_part = i;
580                         }
581                         if(partgraph->nodes[j].comm > max_comm){
582                                 max_comm = partgraph->nodes[j].comm;
583                                 max_comm_part = j;
584                         }
585                 }
586         }
587         #else
588         for(i=0;i<stats->n_comm;i++)
589         {
590                 //DO I consider other comm too....i.e. to or from a processor
591
592                 LDCommData &cdata = stats->commData[i];
593                 //if(cdata.from_proc() || cdata.receiver.get_type() != LD_OBJ_MSG)
594         //continue;
595                 if(!cdata.from_proc() && cdata.receiver.get_type() == LD_OBJ_MSG){
596         int senderID = stats->getHash(cdata.sender);
597         int recverID = stats->getHash(cdata.receiver.get_destObj());
598                         CmiAssert(senderID < stats->n_objs);
599                 CmiAssert(recverID < stats->n_objs);
600                 
601                         if(newmap[senderID]==newmap[recverID])
602                                 continue;
603         
604                         //CkPrintf("from %d to %d\n",newmap[senderID],newmap[recverID]);
605                         if(partgraph->edges[newmap[senderID]][newmap[recverID]] == 0){
606                                 partgraph->nodes[newmap[senderID]].degree++;
607                                 partgraph->nodes[newmap[recverID]].degree++;
608                         }
609                 
610                         partgraph->edges[newmap[senderID]][newmap[recverID]] += cdata.bytes;
611                         partgraph->edges[newmap[recverID]][newmap[senderID]] += cdata.bytes;
612                         
613                         partgraph->nodes[newmap[senderID]].comm += cdata.bytes;
614                         partgraph->nodes[newmap[recverID]].comm += cdata.bytes;
615                         
616                         if(partgraph->nodes[newmap[senderID]].comm > max_comm){
617                                 max_comm = partgraph->nodes[newmap[senderID]].comm;
618                                 max_comm_part = newmap[senderID];
619                         }
620                         if(partgraph->nodes[newmap[recverID]].comm > max_comm){
621                                 max_comm = partgraph->nodes[newmap[recverID]].comm;
622                                 max_comm_part = newmap[recverID];
623                         }
624
625                         //partgraph->edges[newmap[recverID]][newmap[senderID]] += 1000*(cdata.messages*alpha + cdata.bytes*beta);
626                         //bytesComm[newmap[senderID]][newmap[recverID]] += cdata.bytes;
627                         //bytesComm[newmap[recverID]][newmap[senderID]] += cdata.bytes;
628
629                 }
630                 else if(cdata.receiver.get_type() == LD_OBJLIST_MSG) {
631         //CkPrintf("\nin obj list cal...\n\n");
632                         int nobjs;
633         LDObjKey *objs = cdata.receiver.get_destObjs(nobjs);
634       int senderID = stats->getHash(cdata.sender);
635       //addedComm = new int[count];
636                         for(j=0;j<count;j++)
637                                 addedComm[j]=0;
638                         //CkPrintf("in obj list loop...\n");
639                         for (j=0; j<nobjs; j++) {
640         int recverID = stats->getHash(objs[j]);
641         if((senderID == -1)||(recverID == -1))
642                 if (_lb_args.migObjOnly()) continue;
643                 else CkAbort("Error in search\n");
644                                         
645                                 if(newmap[senderID]==newmap[recverID])
646                                         continue;
647         
648                                 //CkPrintf("from %d to %d\n",newmap[senderID],newmap[recverID]);
649                                 if(partgraph->edges[newmap[senderID]][newmap[recverID]] == 0){
650                                         //CkPrintf("sender:%d recver:%d\n",newmap[senderID],newmap[recverID]);
651                                         partgraph->nodes[newmap[senderID]].degree++;
652                                         partgraph->nodes[newmap[recverID]].degree++;
653                                 }
654                 
655                                 if(!addedComm[newmap[recverID]]){
656                                         partgraph->edges[newmap[senderID]][newmap[recverID]] += cdata.bytes;
657                                         partgraph->edges[newmap[recverID]][newmap[senderID]] += cdata.bytes;
658         
659                                         partgraph->nodes[newmap[senderID]].comm += cdata.bytes;
660                                         partgraph->nodes[newmap[recverID]].comm += cdata.bytes;
661
662                                         if(partgraph->nodes[newmap[senderID]].comm > max_comm){
663                                                 max_comm = partgraph->nodes[newmap[senderID]].comm;
664                                                 max_comm_part = newmap[senderID];
665                                         }
666                                         if(partgraph->nodes[newmap[recverID]].comm > max_comm){
667                                                 max_comm = partgraph->nodes[newmap[recverID]].comm;
668                                                 max_comm_part = newmap[recverID];
669                                         }
670
671                                         //bytesComm[newmap[senderID]][newmap[recverID]] += cdata.bytes;
672                                         //bytesComm[newmap[recverID]][newmap[senderID]] += cdata.bytes;
673                                         addedComm[newmap[recverID]]=1;
674                                 }
675       }
676                         //CkPrintf("outside loop..\n");
677         //delete [] addedComm;
678                 }
679
680         }
681         #endif
682         
683         int *proc_mapping = new int[count];
684         
685         delete [] addedComm;
686                 
687         LBtopoFn topofn;
688         int max_neighbors=0;
689         int *neigh;
690         int num_neigh=0;
691
692         char *lbcopy = strdup(_lbtopo);
693         char *ptr = strchr(lbcopy, ':');
694   if (ptr!=NULL){
695         ptr = strtok(lbcopy, ":");
696                 //CkPrintf("token:%s\n",ptr);
697         }
698         else
699                 ptr=lbcopy;
700
701         topofn = LBTopoLookup(ptr);
702   if (topofn == NULL) {
703         char str[1024];
704     CmiPrintf("TopoCentLB> Fatal error: Unknown topology: %s. Choose from:\n", ptr);
705     printoutTopo();
706     sprintf(str, "TopoCentLB> Fatal error: Unknown topology: %s", ptr);
707     CmiAbort(str);
708   }
709   
710         topo = topofn(count);
711         
712         calculateMST(partgraph,topo,proc_mapping,max_comm_part);
713         //Returned partition graph is a Maximum Spanning Tree -- converted in above function itself
714
715         //Construct the Topology Graph
716         
717         /*CkPrintf("part,proc mapping..\n");
718         for(i=0;i<count;i++)
719                 CkPrintf("%d,%d len:%d\n",i,proc_mapping[i],(partgraph->nodes[i]).num_objs);
720                 */
721         int pe;
722         PartGraph::Node* n;
723         for(i=0;i<count;i++){
724                 pe = proc_mapping[i];
725                 n = &partgraph->nodes[i];
726                 //CkPrintf("here..%d,first object:%d\n",n->num_objs,n->obj_list[0]);
727                 for(j=0;j<n->num_objs;j++){
728                         stats->to_proc[n->obj_list[j]] = pe;
729                         if (_lb_args.debug()>1) 
730         CkPrintf("[%d] Obj %d migrating from %d to %d\n", CkMyPe(),n->obj_list[j],stats->from_proc[n->obj_list[j]],pe);
731                 }
732         }
733
734         delete[] newmap;
735         delete[] proc_mapping;
736         //Delete hopCount
737         for(i=0;i<count;i++)
738                 delete[] hopCount[i];
739
740         delete[] hopCount;
741 }
742
743 #include "TopoCentLB.def.h"