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