OpenAtom  Version1.5a
PeList.C
Go to the documentation of this file.
1 /*****************************************************************************
2  * $Source$
3  * $Author$
4  * $Date$
5  * $Revision$
6  *****************************************************************************/
7 
8 /** \file PeList.C
9  *
10  */
11 
12 
13 #include "charm++.h"
14 #include "PeList.h"
15 #include <algorithm>
16 #include "TopoManager.h"
17 extern TopoManager *topoMgr;
18 extern Config config;
19 
20 /**
21  * construct the list by iterating through boxes which are sub partitions
22  * boxy constructor
23  */
24 PeList::PeList(int boxX, int boxY, int boxZ, int order, int maxX, int maxY, int maxZ, int maxT)
25 {
26  if(config.torusMap==1)
27  {
28  CkAssert(topoMgr!=NULL);
29  CkAssert(boxX>0);
30  CkAssert(boxY>0);
31  CkAssert(boxZ>0);
32  current=0;
33  sorted=false;
34  size=config.numPesPerInstance;
35  int i=0;
36  CkPrintf("Ordering processors along long axis %d in %d X %d X %d\n",order, maxX, maxY, maxZ);
37  TheList= new int[size];
38  sortIdx= new int[size];
39  int numBoxes=0;
40  if(order==0) // long axis along X
41  {
42  for(int x=0; x<maxX; x+=boxX) // new box in X
43  for(int y=0; y<maxY; y+=boxY) // new box in Y
44  for(int z=0; z<maxZ; z+=boxZ) // new box in Z
45  {
46  // fill out this box
47  numBoxes++;
48 
49  for(int bx=0;bx<boxX;bx++)
50  for(int by=0;by<boxY;by++) // make inner planes along X
51  for(int bz=0;bz<boxZ;bz++)
52  for(int bt=0;bt<maxT;bt++)
53  {
54  sortIdx[i]=i;
55  // CkPrintf("i %d bx %d x %d by %d y bz %d z bt %d\n",i,bx,x,by,y,bz,z,bt);
56  TheList[i++]=topoMgr->coordinatesToRank(bx+x, by+y, bz+z, bt);
57  }
58  }
59  }
60  else if(order ==1) // long axis is along Y
61  {
62  for(int y=0; y<maxY; y+=boxY) // new box in Y
63  for(int x=0; x<maxX; x+=boxX) // new box in X
64  for(int z=0; z<maxZ; z+=boxZ) // new box in Z
65  {
66  // fill out this box
67  numBoxes++;
68 
69 
70  for(int by=0;by<boxY;by++)
71  for(int bz=0;bz<boxZ;bz++)
72  for(int bx=0;bx<boxX;bx++)
73  for(int bt=0;bt<maxT;bt++)
74  {
75  sortIdx[i]=i;
76  TheList[i++]=topoMgr->coordinatesToRank(bx+x, by+y, bz+z, bt);
77  }
78  }
79 
80  }
81  else if(order ==2) // long axis is along Z
82  {
83  for(int z=0; z<maxZ; z+=boxZ) // new box in Z
84  for(int x=0; x<maxX; x+=boxX) // new box in X
85  for(int y=0; y<maxY; y+=boxY) // new box in Y
86  {
87  // fill out this box
88  numBoxes++;
89  for(int bz=0;bz<boxZ;bz++)
90  for(int by=0;by<boxY;by++)
91  for(int bx=0;bx<boxX;bx++)
92  for(int bt=0;bt<maxT;bt++)
93  {
94  sortIdx[i]=i;
95  TheList[i++]=topoMgr->coordinatesToRank(bx+x, by+y, bz+z, bt);
96  }
97  }
98 
99  }
100  else
101  {
102  CkAbort("unknown order");
103  }
104  // size is actually boxsize times the number of whole boxes
105  // that fit in the mesh
106  int end=numBoxes*boxX*boxY*boxZ;
107  // fill out remainder
108  if(i<config.numPesPerInstance)
109  {
110  PeList remainder(config.numPesPerInstance);
111  for(int i=0; i< size;i++)
112  {
113  int j=0;
114  while(j< size)
115  if(remainder.TheList[j]==TheList[i])
116  {
117  CkPrintf("IF %d %d %d %d %d\n", i, j, size, TheList[j], TheList[i]);
118  remainder.remove(j);
119  }
120  else
121  {
122  CkPrintf("ELSE %d %d %d %d %d\n", i, j, size, TheList[j], TheList[i]);
123  j++;
124  }
125  }
126  remainder.reindex();
127  // now we just plunk these at the end
128  int i=0;
129  for(; i< remainder.size ; i++)
130  {
131  TheList[i+end]=remainder.TheList[i];
132  sortIdx[i+end]=i+end;
133  }
134  }
135  }
136  else
137  CkAbort("You shouldn't be calling this function\n");
138 }
139 
140 
141 PeList::PeList() // default constructor
142  {
143  sorted=true;
144  current=0;
145  size=config.numPesPerInstance;
146  TheList= new int[size+1];
147  sortIdx= new int[size+1];
148  for(int i=0;i<size;i++)
149  {
150  TheList[i]=i;
151  sortIdx[i]=i;
152  }
153  sortSource(0);
154  }
155 
156 void PeList::rebuild()
157 {
158  sorted=true;
159  current=0;
160  size=config.numPesPerInstance;
161  for(int i=0;i<size;i++)
162  {
163  TheList[i]=i;
164  sortIdx[i]=i;
165  }
166 
167 }
168 
169 
170 int *PeList::pelower_bound(int pe)
171 {
172  return(std::lower_bound(&TheList[0],&TheList[size],pe));
173 }
174 bool PeList::binsearch(int pe)
175 {
176  return(std::binary_search(&TheList[0],&TheList[size],pe));
177 }
178 
179 void PeList::sortSource(int srcPe)
180 {
181  if(config.torusMap==1)
182  {
183  // sort it using TopoManager
184  // CkPrintf("PRE: sortIndexByHops\n");
185  CkAssert(srcPe>=0);
186  CkAssert(srcPe<config.numPes);
187  topoMgr->sortRanksByHops(srcPe, TheList, sortIdx, size);
188  // CkPrintf("POST sortIndexByHops\n");
189  }
190  else
191  {
192  // alternate form in non BG/L case (just an ordered list)
193  // sort it using CkVec quicksort
194  CkVec <int> sortme(size);
195  CmiMemcpy(sortme.getVec(), TheList,size*sizeof(int));
196  sortme.quickSort();
197  CmiMemcpy(TheList, sortme.getVec(), size*sizeof(int));
198  }
199 }
200 
201 int PeList::minDist(int srcPe)
202 {
203  if(config.torusMap==1)
204  return(TheList[topoMgr->pickClosestRank(srcPe, TheList, size)]);
205  else
206  return(TheList[0]);
207  // if not a torus distance is meaningless we just pick the first element
208 }
209 
210 int PeList::findNext() // return next available, increment liststart
211 {
212  int value;
213  if(config.torusMap==1) {
214  if(current>=size)
215  {
216  //CkPrintf("hey why is current %d >= size %d\n",current, size);
217  current=0;
218  }
219  CkAssert(current<size);
220  CkAssert(sortIdx[current]<size);
221  value=TheList[sortIdx[current]];
222  // TheList.remove(sortIdx[0]);
223  // sortIdx.remove(0);
224  }
225  else {
226  value=TheList[current];
227  // TheList.remove(0);
228  }
229  current++;
230  return(value);
231 }
232 
Definition: PeList.h:33
int minDist(int source)
return a processor at minumum distance from source
Definition: PeList.C:201
Author: Eric J Bohm Date Created: May 31, 2006.