NAMD
ComputeMap.C
Go to the documentation of this file.
1 
7 #include <stdlib.h>
8 #include <stdio.h>
9 
10 #include "InfoStream.h"
11 #include "ComputeMap.h"
12 #include "Compute.h"
13 #include "MStream.h"
14 
15 #include "charm++.h"
16 
17 #define MIN_DEBUG_LEVEL 4
18 //#define DEBUGM
19 #include "Debug.h"
20 
22 
23 ComputeMap* ComputeMap::instance;
24 
25 // Singleton method
27  if (instance == 0) {
28  instance = new ComputeMap; // this is never deleted
29  }
30  return instance;
31 }
32 
33 
34 //----------------------------------------------------------------------
36 {
37  nComputes=0;
38  computePtrs=0;
39 }
40 
41 //----------------------------------------------------------------------
43 {
44  delete [] computePtrs;
45 }
46 
47 void
49 {
50  int computeCount = nComputes;
51  for (int i=0; i<nComputes; i++) {
52  if (computePtrs[i]) {
53  computeCount++;
54  if (! (computePtrs[i]->cid == i)) {
55  DebugM(4, "ComputeID("<<computePtrs[i]->cid<<") != ComputeID("
56  << i <<")\n");
57  }
58  }
59  }
60  DebugM(4, "Compute Count = " << computeCount << "\n");
61 }
62 
64 {
65  DebugM(4,"Packing ComputeMap\n");
66  msg->put(nComputes);
67  msg->put(nComputes,computeData.begin());
68 }
69 
71 {
72  DebugM(4,"Unpacking ComputeMap\n");
73  int old = nComputes;
74  msg->get(nComputes);
75  if ( old && old != nComputes ) {
76  NAMD_bug("number of computes in new ComputeMap has changed!\n");
77  }
78  computeData.resize(nComputes);
79  msg->get(nComputes,computeData.begin());
80 }
81 
83  if ( ! computePtrs ) {
84  computePtrs = new Compute*[nComputes];
85  memset(computePtrs, 0, nComputes*sizeof(Compute*));
86  }
87 }
88 
90  if ( ! computePtrs ) NAMD_bug("ComputeMap::extendPtrs() 1");
91  int oldN = nComputes;
92  nComputes = computeData.size();
93  if ( nComputes > oldN ) {
94  Compute **oldPtrs = computePtrs;
95  computePtrs = new Compute*[nComputes];
96  memcpy(computePtrs, oldPtrs, oldN*sizeof(Compute*));
97  memset(computePtrs+oldN, 0, (nComputes-oldN)*sizeof(Compute*));
98  delete [] oldPtrs;
99  }
100 }
101 
102 //----------------------------------------------------------------------
104 {
105  return computeData[cid].numPids;
106 }
107 
108 //----------------------------------------------------------------------
110 {
111  return computeData[cid].pids[i].pid;
112 }
113 
115 {
116  return computeData[cid].pids[i].trans;
117 }
118 
119 //----------------------------------------------------------------------
121 {
122  if (nComputes)
123  return computeData[cid].type;
124  else return computeErrorType;
125 }
126 
127 //----------------------------------------------------------------------
129 {
130  if (nComputes)
131  return computeData[cid].partition;
132  else return computeErrorType;
133 }
134 //----------------------------------------------------------------------
136 {
137  if (nComputes)
138  return computeData[cid].numPartitions;
139  else return computeErrorType;
140 }
141 
142 //----------------------------------------------------------------------
144 {
145  nComputes = 0;
146  computeData.resize(500);
147  computeData.resize(0);
148 
149  return 0;
150 }
151 
152 //----------------------------------------------------------------------
153 ComputeID ComputeMap::storeCompute(int inode, int maxPids,
154  ComputeType type,
155  int partition,int numPartitions)
156 {
157  if (maxPids > numPidsAllocated) {
158  NAMD_bug("ComputeMap::storeCompute called with maxPids > numPidsAllocated");
159  }
160 
161  int cid;
162 
163  cid = nComputes;
164  nComputes++;
165  computeData.resize(nComputes);
166 
167  computeData[cid].node=inode;
168 
169  computeData[cid].type = type;
170  computeData[cid].partition = partition;
171  computeData[cid].numPartitions = numPartitions;
172 
173  computeData[cid].numPids = 0;
174 
175  #if defined(NAMD_MIC)
176  // Initially in MIC runs, all computes are mapped to the host. The host vs
177  // device LDB scheme will change this mapping later.
178  computeData[cid].directToDevice = 0;
179  #endif
180 
181  return cid;
182 }
183 
184 //----------------------------------------------------------------------
186 {
187  const int cid = computeData.size();
188  computeData.resize(cid+1);
189 
190  computeData[cid] = computeData[src];
191  computeData[cid].partition = partition;
192  computeData[cid].node = -1;
193 
194  return cid;
195 }
196 
197 //----------------------------------------------------------------------
198 void ComputeMap::newPid(ComputeID cid, PatchID pid, int trans)
199 {
200  computeData[cid].pids[computeData[cid].numPids].pid=pid;
201  computeData[cid].pids[computeData[cid].numPids].trans=trans;
202  computeData[cid].numPids++;
203 }
204 
205 //----------------------------------------------------------------------
207 {
208  DebugM(2,"---------------------------------------");
209  DebugM(2,"---------------------------------------\n");
210 
211  DebugM(2,"nComputes = " << nComputes << '\n');
212  DebugM(2,"nAllocated = " << nComputes << '\n');
213  for(int i=0; i < nComputes; i++)
214  {
215  DebugM(2,"Compute " << i << '\n');
216  DebugM(2," node = " << computeData[i].node << '\n');
217  DebugM(2," numPids = " << computeData[i].numPids << '\n');
218  for(int j=0; j < computeData[i].numPids; j++)
219  {
220  DebugM(2,computeData[i].pids[j].pid);
221  if (!((j+1) % 6))
222  DebugM(2,'\n');
223  }
224  DebugM(2,"\n---------------------------------------");
225  DebugM(2,"---------------------------------------\n");
226 
227  }
228 
229 #ifdef MEM_OPT_VERSION
230 const char *fname = "computeMap.opt";
231 #else
232 const char *fname = "computeMap.orig";
233 #endif
234 
235  FILE *ofp = fopen(fname, "w");
236  fprintf(ofp,"---------------------------------------");
237  fprintf(ofp,"---------------------------------------\n");
238 
239  fprintf(ofp,"nComputes = %d\n", nComputes);
240  fprintf(ofp,"nAllocated = %d\n", nComputes);
241  for(int i=0; i < nComputes; i++)
242  {
243  fprintf(ofp,"Compute %d\n", i);
244  fprintf(ofp," node = %d\n",computeData[i].node);
245  fprintf(ofp," numPids = %d\n",computeData[i].numPids);
246  fprintf(ofp," type = %d\n",computeData[i].type);
247  for(int j=0; j < computeData[i].numPids; j++)
248  {
249  fprintf(ofp,"%d ",computeData[i].pids[j].pid);
250  if (!((j+1) % 6))
251  fprintf(ofp,"\n");
252  }
253  fprintf(ofp,"\n---------------------------------------");
254  fprintf(ofp,"---------------------------------------\n");
255 
256  }
257 
258 fclose(ofp);
259 
260 }
261 
262 void ComputeMap::saveComputeMap(const char *fname)
263 {
264  static int count = 0;
265  char f[128];
266  sprintf(f, "%s.%d", fname, count++);
267  FILE *fp = fopen(f, "w");
268  CmiAssert(fp != NULL);
269  fprintf(fp, "%d\n", nComputes);
270  for(int i=0; i < nComputes; i++)
271  {
272  fprintf(fp, "%d\n", computeData[i].node);
273  }
274  fclose(fp);
275  CkPrintf("ComputeMap has been stored in %s.\n", f);
276 }
277 
278 void ComputeMap::loadComputeMap(const char *fname)
279 {
280  FILE *fp = fopen(fname, "r");
281  CmiAssert(fp != NULL);
282  int n;
283  fscanf(fp, "%d\n", &n);
284  CmiAssert(n == nComputes);
285  for(int i=0; i < nComputes; i++)
286  {
287  fscanf(fp, "%d\n", &computeData[i].node);
288  }
289  fclose(fp);
290 }
291 
292 //----------------------------------------------------------------------
293 #if defined(NAMD_MIC)
294 
295 void ComputeMap::setDirectToDevice(const ComputeID cid, const int d) {
296  if (cid < 0 || cid >= nComputes) {
297  NAMD_bug("ComputeMap::setDirectToDevice() called with an invalid cid value");
298  }
299  computeData[cid].directToDevice = ((d == 0) ? (0) : (1));
300 }
301 
302 int ComputeMap::directToDevice(const ComputeID cid) const {
303  if (cid < 0 || cid >= nComputes) {
304  NAMD_bug("ComputeMap::directToDevice() called with an invalid cid value");
305  }
306  return computeData[cid].directToDevice;
307 }
308 
309 #endif // defined(NAMD_MIC)
void checkMap()
Definition: ComputeMap.C:48
void saveComputeMap(const char *fname)
Definition: ComputeMap.C:262
int ComputeID
Definition: NamdTypes.h:183
void initPtrs()
Definition: ComputeMap.C:82
static void partition(int *order, const FullAtom *atoms, int begin, int end)
Definition: SortAtoms.C:42
ComputeType
Definition: ComputeMap.h:20
void loadComputeMap(const char *fname)
Definition: ComputeMap.C:278
#define DebugM(x, y)
Definition: Debug.h:59
MIStream * get(char &data)
Definition: MStream.h:29
ComputeID storeCompute(int node, int maxPids, ComputeType type, int partition=-1, int numPartitions=0)
Definition: ComputeMap.C:153
int allocateCids()
Definition: ComputeMap.C:143
void printComputeMap(void)
Definition: ComputeMap.C:206
~ComputeMap(void)
Definition: ComputeMap.C:42
void NAMD_bug(const char *err_msg)
Definition: common.C:129
ComputeType type(ComputeID cid)
Definition: ComputeMap.C:120
void pack(MOStream *msg)
Definition: ComputeMap.C:63
int numPartitions(ComputeID cid)
Definition: ComputeMap.C:135
int PatchID
Definition: NamdTypes.h:182
int partition(ComputeID cid)
Definition: ComputeMap.C:128
void extendPtrs()
Definition: ComputeMap.C:89
static ComputeMap * Instance()
Definition: ComputeMap.C:26
void newPid(ComputeID cid, int pid, int trans=13)
Definition: ComputeMap.C:198
ComputeID cloneCompute(ComputeID src, int partition)
Definition: ComputeMap.C:185
int node(ComputeID cid)
Definition: ComputeMap.h:106
int numPids(ComputeID cid)
Definition: ComputeMap.C:103
MOStream * put(char data)
Definition: MStream.h:112
int pid(ComputeID cid, int i)
Definition: ComputeMap.C:109
int trans(ComputeID cid, int i)
Definition: ComputeMap.C:114
void unpack(MIStream *msg)
Definition: ComputeMap.C:70
ComputeMap(void)
Definition: ComputeMap.C:35