NAMD
PatchMap.h
Go to the documentation of this file.
1 
7 #ifndef PATCHMAP_H
8 #define PATCHMAP_H
9 
10 #include "NamdTypes.h"
11 #include "HomePatchList.h"
12 #include "Lattice.h"
13 #include "ProcessorPrivate.h"
14 
15 #include <vector>
16 
17 
18 class Patch;
19 class PatchMgr;
20 class HomePatch;
21 template<class Type> class ObjectArena;
22 
23 class PatchMap
24 {
25 public:
26  static PatchMap *Instance();
27  inline static PatchMap *Object() { return CkpvAccess(PatchMap_instance); }
28  inline static PatchMap *ObjectOnPe(int pe) {
29  return CkpvAccessOther(PatchMap_instance, CmiRankOf(pe));
30  }
31 
32  int sizeGrid(ScaledPosition xmin, ScaledPosition xmax,
33  const Lattice &lattice, BigReal patchSize,
34  double maxNumPatches, int staticAtomAssignment,
35  int asplit, int bsplit, int csplit);
37  const Lattice &lattice, BigReal patchSize,
38  double maxNumPatches, int staticAtomAssignment,
39  int replicaUniformPatchGrids, int lcpo,
40  int asplit, int bsplit, int csplit);
41  void checkMap();
42 
43  ~PatchMap(void);
44 
45  enum { MaxTwoAway = 5*5*5 - 3*3*3 };
46  enum { MaxOneAway = 3*3*3 - 1 };
48 
49  static void registerPatchMgr(PatchMgr *pmgr) {
50  CkpvAccess(PatchMap_patchMgr) = pmgr;
51  }
52 
54  void homePatchIDList(PatchIDList &); // expensive - for startup only
55  void basePatchIDList(int pe, PatchIDList &); // use for required proxies
56  int numHomePatches(void);
57 
58  // returns the number of patches being managed
59  inline int numPatches(void) const { return nPatches; }
60  inline int numPatchesOnNode(int node) { return nPatchesOnNode[node]; }
61  inline int numNodesWithPatches(void) { return nNodesWithPatches; }
62 
63  // returns the number of patches in each dimension
64  inline int gridsize_a(void) const { return aDim; }
65  inline int gridsize_b(void) const { return bDim; }
66  inline int gridsize_c(void) const { return cDim; }
67  // returns the number of patches in each dimension
68  inline int numaway_a(void) const { return aAway; }
69  inline int numaway_b(void) const { return bAway; }
70  inline int numaway_c(void) const { return cAway; }
71 
72  // returns 1 if periodic in each dimension
73  inline int periodic_a(void) const { return aPeriodic; }
74  inline int periodic_b(void) const { return bPeriodic; }
75  inline int periodic_c(void) const { return cPeriodic; }
76 
77  // returns the origin (minimum, not center) of patch grid
78  inline ScaledPosition origin(void) const {
79  return ScaledPosition(aOrigin,bOrigin,cOrigin);
80  }
81 
82  // returns the patch id for the given indices
83  inline int pid(int aIndex, int bIndex, int cIndex);
84 
85  // returns the [abc] index for the given patch id.
86  inline int index_a(int pid) const { return pid % aDim; }
87  inline int index_b(int pid) const { return (pid / aDim) % bDim; }
88  inline int index_c(int pid) const { return pid / (aDim*bDim); }
89 
90  // returns the min/max [abc] scaled coordinate
91  inline BigReal min_a(int pid) const { return patchBounds_a[patchData[pid].aIndex*2]; }
92  inline BigReal max_a(int pid) const { return patchBounds_a[patchData[pid].aIndex*2+2]; }
93  inline BigReal min_b(int pid) const { return patchBounds_b[patchData[pid].bIndex*2]; }
94  inline BigReal max_b(int pid) const { return patchBounds_b[patchData[pid].bIndex*2+2]; }
95  inline BigReal min_c(int pid) const { return patchBounds_c[patchData[pid].cIndex*2]; }
96  inline BigReal max_c(int pid) const { return patchBounds_c[patchData[pid].cIndex*2+2]; }
97 
98  // returns the center of patch scaled position
99  inline ScaledPosition center(int pid) const {
100  const PatchData &pd = patchData[pid];
101  return ScaledPosition(patchBounds_a[pd.aIndex*2+1],
102  patchBounds_b[pd.bIndex*2+1],
103  patchBounds_c[pd.cIndex*2+1]);
104  }
105 
106  // asssigns atom to patch based on position and lattice
107  inline PatchID assignToPatch(Position p, const Lattice &l);
108 
109  // gives more downstream patch of pid1, pid2; handles periodicity right
110  // given patches must be neighbors!!!
111  inline int downstream(int pid1, int pid2);
112 
113  // returns the node where the patch currently exists.
114  inline int node(int pid) const { return patchData[pid].node; }
115 
116  // returns the node where the patch's upstream proxies exist.
117  inline int basenode(int pid) const { return patchData[pid].basenode; }
118 
119  // numCids(pid) returns the number of compute ids which are registered
120  inline int numCids(int pid) const { return patchData[pid].numCids; }
121 
122  // cid(pid,i) returns the i-th compute id registered
123  inline int cid(int pid, int i) const { return patchData[pid].cids[i]; }
124 
125 #ifdef MEM_OPT_VERSION
126  inline int numAtoms(int pid) const { return patchData[pid].numAtoms; }
127  inline void setNumAtoms(int pid, int num) { patchData[pid].numAtoms = num; }
128 
129  inline int numFixedAtoms(int pid) const { return patchData[pid].numFixedAtoms; }
130  inline void setNumFixedAtoms(int pid, int num) { patchData[pid].numFixedAtoms = num; }
131 #endif
132 
133  void assignNode(PatchID, NodeID);
135  void assignBaseNode(PatchID);
136 
137  // newCid(pid,cid) stores a compute id associated with
138  // patch id pid. Error returned when there is no room to store
139  // the pid.
140  void newCid(int pid, int cid);
141 
142  // oneAwayNeighbors(pid, neighbor_ids) returns the number
143  // and ids of adjacent patches. The caller is expected to provide
144  // sufficient storage for the neighbors.
145 
146  int oneAwayNeighbors(int pid, PatchID *neighbor_ids=0);
147 
148  int oneOrTwoAwayNeighbors(int pid, PatchID *neighbor_ids,
149  PatchID *downstream_ids = 0, int *transform_ids = 0);
150 
151  //LCPO
152  int getPatchesInOctet(int pid, PatchID *pids, int *transform_ids = 0);
153 
154  int upstreamNeighbors(int pid, PatchID *neighbor_ids);
155 
156  int downstreamNeighbors(int pid, PatchID *neighbor_ids);
157 
158  void printPatchMap(void);
159 
160  inline Patch *patch(PatchID pid);
161  inline HomePatch *homePatch(PatchID pid);
162 
163  void registerPatch(PatchID pid, HomePatch *pptr);
164  void unregisterPatch(PatchID pid, HomePatch *pptr);
165 
166  void registerPatch(PatchID pid, Patch *pptr);
167  void unregisterPatch(PatchID pid, Patch *pptr);
168 
169 protected:
170  friend class WorkDistrib;
171  int packSize(void);
172  void pack(char *buf, int size);
173  void unpack(char *buf);
174 
175  PatchMap(void);
176 
177 private:
178  struct PatchData
179  {
180  int node, basenode;
181  short aIndex, bIndex, cIndex;
182  short numCids;
183  short numCidsAllocated;
184  ComputeID *cids;
185 #ifdef MEM_OPT_VERSION
186  //added to record #atoms in each patch initially
187  //--Chao Mei
188  unsigned short numAtoms;
189  unsigned short numFixedAtoms;
190 #endif
191  };
192  int nPatches;
193  int nNodesWithPatches;
194  static int *nPatchesOnNode;
195  static PatchData *patchData;
196  static ObjectArena<ComputeID> *computeIdArena;
197  BigReal *patchBounds_a;
198  BigReal *patchBounds_b;
199  BigReal *patchBounds_c;
200  Patch **myPatch;
201  HomePatch **myHomePatch;
202  int aDim, bDim, cDim;
203  int aAway, bAway, cAway;
204  int aPeriodic, bPeriodic, cPeriodic;
205  int aMaxIndex, bMaxIndex, cMaxIndex;
206  BigReal aOrigin, bOrigin, cOrigin;
207  BigReal aLength, bLength, cLength;
208 
209 private:
210  //It is used to store the atom ids that each patch has
211  //we need this structure because we want to create and distribute
212  //each patch one by one rather than creat all home patches at a time and then
213  //send them later
214  std::vector<int> *tmpPatchAtomsList;
215 public:
217  tmpPatchAtomsList = new std::vector<int>[nPatches];
218  }
220  for(int i=0; i<nPatches; i++){
221  tmpPatchAtomsList[i].clear();
222  }
223  delete [] tmpPatchAtomsList;
224  tmpPatchAtomsList = NULL;
225  }
226  std::vector<int> *getTmpPatchAtomsList(){
227  return tmpPatchAtomsList;
228  }
229 
230 };
231 
232 
233 //----------------------------------------------------------------------
234 
236 {
237  return myPatch[pid];
238 }
239 
241 {
242  return myHomePatch[pid];
243 }
244 
245 #endif /* PATCHMAP_H */
246 
int numNodesWithPatches(void)
Definition: PatchMap.h:61
PatchID assignToPatch(Position p, const Lattice &l)
Definition: PatchMap.inl:14
void initTmpPatchAtomsList()
Definition: PatchMap.h:216
int ComputeID
Definition: NamdTypes.h:183
int gridsize_c(void) const
Definition: PatchMap.h:66
static PatchMap * Object()
Definition: PatchMap.h:27
BigReal max_c(int pid) const
Definition: PatchMap.h:96
Definition: Vector.h:64
BigReal min_a(int pid) const
Definition: PatchMap.h:91
void basePatchIDList(int pe, PatchIDList &)
Definition: PatchMap.C:454
int index_a(int pid) const
Definition: PatchMap.h:86
HomePatchList * homePatchList()
Definition: PatchMap.C:438
int downstream(int pid1, int pid2)
Definition: PatchMap.inl:51
int packSize(void)
Definition: PatchMap.C:314
int upstreamNeighbors(int pid, PatchID *neighbor_ids)
Definition: PatchMap.C:669
int numCids(int pid) const
Definition: PatchMap.h:120
int sizeGrid(ScaledPosition xmin, ScaledPosition xmax, const Lattice &lattice, BigReal patchSize, double maxNumPatches, int staticAtomAssignment, int asplit, int bsplit, int csplit)
Definition: PatchMap.C:62
int numaway_b(void) const
Definition: PatchMap.h:69
Patch * patch(PatchID pid)
Definition: PatchMap.h:235
static PatchMap * ObjectOnPe(int pe)
Definition: PatchMap.h:28
HomePatch * homePatch(PatchID pid)
Definition: PatchMap.h:240
int basenode(int pid) const
Definition: PatchMap.h:117
BigReal min_b(int pid) const
Definition: PatchMap.h:93
Definition: Patch.h:35
void unpack(char *buf)
Definition: PatchMap.C:365
void unregisterPatch(PatchID pid, HomePatch *pptr)
Definition: PatchMap.C:796
int periodic_a(void) const
Definition: PatchMap.h:73
void assignBaseNode(PatchID, NodeID)
Definition: PatchMap.C:472
void newCid(int pid, int cid)
Definition: PatchMap.C:512
int gridsize_a(void) const
Definition: PatchMap.h:64
int oneAwayNeighbors(int pid, PatchID *neighbor_ids=0)
Definition: PatchMap.C:532
void homePatchIDList(PatchIDList &)
Definition: PatchMap.C:443
int oneOrTwoAwayNeighbors(int pid, PatchID *neighbor_ids, PatchID *downstream_ids=0, int *transform_ids=0)
Definition: PatchMap.C:579
int index_b(int pid) const
Definition: PatchMap.h:87
std::vector< int > * getTmpPatchAtomsList()
Definition: PatchMap.h:226
int PatchID
Definition: NamdTypes.h:182
int cid(int pid, int i) const
Definition: PatchMap.h:123
int periodic_b(void) const
Definition: PatchMap.h:74
void makePatches(ScaledPosition xmin, ScaledPosition xmax, const Lattice &lattice, BigReal patchSize, double maxNumPatches, int staticAtomAssignment, int replicaUniformPatchGrids, int lcpo, int asplit, int bsplit, int csplit)
Definition: PatchMap.C:171
Vector ScaledPosition
Definition: Lattice.h:15
BigReal max_b(int pid) const
Definition: PatchMap.h:94
int index_c(int pid) const
Definition: PatchMap.h:88
BigReal max_a(int pid) const
Definition: PatchMap.h:92
ScaledPosition center(int pid) const
Definition: PatchMap.h:99
int downstreamNeighbors(int pid, PatchID *neighbor_ids)
Definition: PatchMap.C:714
int numPatches(void) const
Definition: PatchMap.h:59
int node(int pid) const
Definition: PatchMap.h:114
void checkMap()
Definition: PatchMap.C:274
static void registerPatchMgr(PatchMgr *pmgr)
Definition: PatchMap.h:49
int numHomePatches(void)
Definition: PatchMap.C:432
ScaledPosition origin(void) const
Definition: PatchMap.h:78
int pid(int aIndex, int bIndex, int cIndex)
Definition: PatchMap.inl:27
void assignNode(PatchID, NodeID)
Definition: PatchMap.C:465
int numPatchesOnNode(int node)
Definition: PatchMap.h:60
int numaway_c(void) const
Definition: PatchMap.h:70
void delTmpPatchAtomsList()
Definition: PatchMap.h:219
BigReal min_c(int pid) const
Definition: PatchMap.h:95
~PatchMap(void)
Definition: PatchMap.C:291
void registerPatch(PatchID pid, HomePatch *pptr)
Definition: PatchMap.C:786
void pack(char *buf, int size)
Definition: PatchMap.C:328
int periodic_c(void) const
Definition: PatchMap.h:75
void printPatchMap(void)
Definition: PatchMap.C:759
PatchMap(void)
Definition: PatchMap.C:39
int numaway_a(void) const
Definition: PatchMap.h:68
int NodeID
Definition: NamdTypes.h:184
static PatchMap * Instance()
Definition: PatchMap.C:32
int gridsize_b(void) const
Definition: PatchMap.h:65
double BigReal
Definition: common.h:112
int getPatchesInOctet(int pid, PatchID *pids, int *transform_ids=0)
Definition: PatchMap.C:634