PeList.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * $Source: /cvsroot/leanCP/src_charm_driver/load_balance/PeList.h,v $
00003  * $Author: bhatele $
00004  * $Date: 2007/12/05 08:33:03 $
00005  * $Revision: 1.38 $
00006  *****************************************************************************/
00007 
00008 /** \file PeList.h
00009  *  Author: Eric J Bohm
00010  *  Date Created: May 31, 2006
00011  *
00012  *  Supports lists of processors which can be sorted by various
00013  *  criteria, such as distance or latency.  + and - operators are
00014  *  supported provide union and disjoint operations.  
00015  * 
00016  *  Implemented on ckvec.
00017  *  Used to find the nearest available processor from a given processor.
00018  *  Uses TopoManager for hopcount sorting if available.
00019  */
00020 
00021 /** \ingroup mapping
00022  *
00023  */
00024 //@{
00025 #ifndef _PELIST_H
00026 #define _PELIST_H
00027 
00028 #include <math.h>
00029 #include "configure.h"
00030 #include "TopoManager.h"
00031 #include "cklists.h"
00032 
00033 class PeList 
00034 {
00035  public:
00036   int *TheList;
00037   int *sortIdx;
00038   int current;
00039   int size;
00040   bool sorted;  // if pe list is kept in sorted order we can bin search it
00041   PeList();  //default constructor
00042 
00043   // boxy constructor for BG/L
00044   PeList(int boxX, int boxY, int boxZ, int order);
00045 
00046   PeList(int _size): size(_size)
00047     {
00048       sorted=true;
00049       TheList = new int [size+1];
00050       sortIdx = new int [size+1];
00051       for(int i=0;i<size;i++)
00052         {
00053           TheList[i]=i;
00054           sortIdx[i]=i;
00055         }
00056       current=0;
00057     }
00058 
00059 
00060   PeList(CkVec <int> inlist)
00061     {
00062       inlist.quickSort();
00063       sorted=true;
00064       current=0;
00065       size=inlist.size();
00066       TheList = new int [size+1];
00067       sortIdx = new int [size+1];
00068       TheList[0]=inlist[0];
00069       int count=0;
00070       for(int i=1;i<inlist.size();i++)
00071         { //remove duplicates from sorted list
00072           if(inlist[i]!=TheList[count])
00073             {
00074               count++;
00075               TheList[count]=inlist[i];
00076               sortIdx[count]=count;
00077             }
00078           
00079         }
00080       size=count;
00081     }
00082 
00083   PeList(PeList &inlist)
00084     {
00085       size=inlist.size;
00086       sorted=inlist.sorted;
00087       TheList = new int [size+1];
00088       sortIdx = new int [size+1];
00089       for(int i=0;i<inlist.size;i++)
00090         {
00091           TheList[i]=inlist.TheList[i];
00092           sortIdx[i]=inlist.sortIdx[i];
00093         }
00094       current=inlist.current;
00095     }
00096 
00097 
00098   PeList(int _size, int *a)
00099     {
00100       sorted=false;
00101       current=0;
00102       size=_size;
00103       TheList=new int [size+1];
00104       sortIdx=new int [size+1];
00105       for(int i=0;i<size;i++)
00106         {
00107           TheList[i]=a[i];
00108           sortIdx[i]=i;
00109         }
00110     }    // use an array to construct
00111 
00112   PeList(PeList *a, int start, int _size)
00113     {
00114       sorted=false;
00115       CkAssert(start<a->size);
00116       CkAssert(_size<=a->size);
00117       size=_size;
00118       current=0;
00119       TheList=new int [size+1];
00120       sortIdx=new int [size+1];
00121       for(int i=0;i<size;i++)
00122         {
00123           TheList[i]=a->TheList[a->sortIdx[i+start]];
00124         }
00125       reindex();
00126     };   // make a copy of a sublist
00127   
00128   // given the max grid/torus dimenions and the volume of a desired subpartition
00129   
00130   inline bool noPes() 
00131     {
00132       return(size-current<1);
00133     }
00134   
00135   inline int count() { return(size-current);  }
00136   
00137   inline void reindex(){
00138       for(int ri=0;ri<size;ri++)
00139         sortIdx[ri]=ri;
00140       current=0;
00141   } 
00142   inline int exists(int target)
00143     {
00144       for(int i=0;i<size;i++)
00145         if(TheList[i]==target)
00146           return i;
00147       return -1;
00148     }
00149   void rebuild(); 
00150 
00151   void reset(){current=0;} 
00152 
00153   //! return a processor at minumum distance from source
00154   int  minDist(int source);
00155 
00156   void resort(){   sortSource(TheList[0]);   }; 
00157 
00158   void append(PeList &inlist)
00159   {
00160     // make array large enough for both, paste together
00161     int newsize=inlist.size+size;
00162     int *newlist= new int [newsize+1];
00163     int *newIndex= new int [newsize+1];
00164     int i=0;
00165     for(; i< size ; i++)
00166       {
00167         newlist[i]=TheList[i];
00168         newIndex[i]=sortIdx[i];
00169       }
00170     for(int j=0; (i< newsize && j<inlist.size) ; i++,j++)
00171       {
00172         newlist[i]=inlist.TheList[j];
00173         newIndex[i]=i;
00174       }
00175     size=newsize;
00176     delete [] TheList;
00177     delete [] sortIdx;
00178     TheList=newlist;
00179     sortIdx=newIndex;
00180   }
00181   
00182   bool binsearch(int pe);
00183 
00184   inline bool find(int pe)
00185     {
00186       bool found=false;
00187       if(sorted)
00188         { // bin search
00189           found=binsearch(pe);
00190         }
00191       else // brute
00192         {
00193           int i=0;
00194           while(!found && i< size)
00195             {
00196               if(TheList[i]==pe)
00197                 {
00198                   found=true;
00199                 }
00200               i++;
00201             }
00202         }
00203       return(found);
00204     }
00205 
00206   int *pelower_bound(int pe);
00207 
00208   inline void mergeOne(int pe)
00209   {
00210     
00211     // make array large enough for both, paste together
00212     if(sorted)
00213       { // maintain sort order by insertion
00214         if(size==1 && pe>TheList[0])
00215           {
00216             appendOne(pe);
00217           }
00218         else{
00219           int *loc=pelower_bound(pe);
00220           int location=loc-TheList;
00221 
00222           if(location>=size || loc==NULL)
00223             { // 
00224               appendOne(pe);
00225             }
00226           else if ((loc>TheList) && (loc[-1] ==pe))
00227             {
00228               // do nothing
00229             }
00230           else if(TheList[location] ==pe)
00231             {
00232               // do nothing
00233             }
00234           
00235           else if((size>1) &&(location<size-1) && (TheList[location+1]==pe))
00236             {
00237               // do nothing
00238             }
00239           else
00240             { // not already present
00241 
00242               int newsize=size+1;
00243               int *newlist= new int [newsize+1];
00244               int *newIndex= new int [newsize+1];
00245               if(loc!=TheList) // there are things before location
00246                 CmiMemcpy(newlist,TheList,location*sizeof(int));
00247               newlist[location]=pe;
00248               CmiMemcpy(&newlist[location+1],loc,(size-(location))*sizeof(int));
00249               // your index is shot
00250               bzero(newIndex,(newsize+1)*sizeof(int));
00251               delete [] TheList;
00252               delete [] sortIdx;
00253               TheList=newlist;
00254               sortIdx=newIndex;
00255               size=newsize;
00256               current=0;
00257             }
00258         }
00259       }
00260     else
00261       {
00262         bool found=find(pe);
00263         if(!found)
00264           {
00265             appendOne(pe);
00266           }
00267       }
00268   }
00269 
00270   inline void appendOne(int pe)
00271     {
00272         int newsize=size+1;
00273         int *newlist= new int [newsize+1];
00274         int *newIndex= new int [newsize+1];
00275         CmiMemcpy(&newlist[0],&TheList[0],sizeof(int)*size);
00276         CmiMemcpy(&newIndex[0],&sortIdx[0],sizeof(int)*size);
00277         newlist[size]=pe;
00278         newIndex[size]=size;
00279         size=newsize;
00280         delete [] TheList;
00281         delete [] sortIdx;
00282         TheList=newlist;
00283         sortIdx=newIndex;
00284     }
00285 
00286   int findNext();       // return next available, increment liststart
00287 
00288   void sortSource(int srcPe); // implementation depends on BG/L or not
00289 
00290   PeList &operator=(PeList &inlist) {TheList=inlist.TheList; sortIdx=inlist.sortIdx;return *this;}   
00291 
00292 
00293   // need to rebuild your sortIdx after set union
00294   inline PeList &operator+(PeList &inlist) { 
00295     // make array large enough for both, paste together
00296     int newsize=inlist.size+size;
00297     int *newlist= new int [newsize];
00298     int *newIndex= new int [newsize];
00299     int i=0;
00300     for(; i< size ; i++)
00301       {
00302         newlist[i]=TheList[i];
00303         newIndex[i]=sortIdx[i];
00304       }
00305     for(int j=0; (i< newsize &j<inlist.size); i++)
00306       {
00307         newlist[i]=inlist.TheList[j];
00308         newIndex[i]=i;
00309       }
00310     size=newsize;
00311     delete [] TheList;
00312     delete [] sortIdx;
00313     TheList=newlist;
00314     sortIdx=newIndex;
00315     return *this; 
00316   }
00317   
00318   // need to rebuild your sortIdx after unary set difference
00319   inline PeList &operator-(PeList &inlist) {
00320     for(int i=0; i< inlist.size;i++)
00321       {
00322         int j=0;
00323         while(j< size)
00324           if(TheList[j]==inlist.TheList[i])
00325             {
00326               remove(j);
00327               //              CkPrintf("removed %d containing %d leaving %d\n",i,inlist.TheList[i], size);
00328 
00329             }
00330           else
00331             {
00332               j++;
00333             }
00334       }
00335     current=0;  // your index is useless now anyway
00336     return *this;
00337   }
00338 
00339   inline void remove(int pos)
00340     {
00341       if(pos < size)
00342         {
00343 
00344           for (int i=pos; i<size-1; i++)
00345             {
00346               TheList[i] = TheList[i+1];
00347             }
00348 
00349           //      CmiMemcpy(&TheList[pos],&TheList[pos+1],(size-(pos+1))*sizeof(int));
00350           size--;
00351         }
00352     }
00353 
00354   void trimUsed()
00355     {
00356       // build a new array that doesn't include used elements
00357       // theoretically this could be done in place, but haste makes waste
00358       if(current>0 && size>0)
00359         {
00360           if(noPes())
00361             {
00362               size=0;
00363               current=0;
00364             }
00365           else
00366             {
00367               int newSize= size-current;
00368               int *newList = new int[newSize+1];
00369               int *newSortIdx = new int[newSize+1];
00370               for(int i=0; i<newSize;i++)
00371                 {
00372                   // if we just copy in sorted order we end up with a sorted list
00373                   newSortIdx[i]=i;
00374                   newList[i]=TheList[sortIdx[i+current]];
00375                 }
00376               delete [] TheList;
00377               delete [] sortIdx;
00378               TheList=newList;
00379               sortIdx=newSortIdx;
00380               current=0;
00381               size=newSize;
00382             }
00383         }
00384     }
00385 
00386   void removeIdx(int pos)
00387     {
00388       if(pos < size)
00389         {
00390           for (int i=pos; i<size-1; i++)
00391             {
00392               sortIdx[i] = sortIdx[i+1];
00393             }
00394           size--;
00395         }
00396     }
00397   void dump()
00398     {
00399       CkPrintf("PeList\n");
00400       for(int j=0; j< size;j++)
00401         CkPrintf("%d list %d sortidx %d \n",j,TheList[j], sortIdx[j]);
00402     }
00403   ~PeList(){
00404     if(TheList !=NULL)
00405       delete [] TheList;
00406     if(sortIdx !=NULL)
00407       delete [] sortIdx;
00408 
00409   }
00410 
00411 
00412 };
00413 
00414 //@}
00415 #endif
00416 

Generated on Thu Dec 6 18:25:32 2007 for leanCP by  doxygen 1.5.3