ComputeMap.C

Go to the documentation of this file.
00001 
00007 #include <stdlib.h>
00008 #include <stdio.h>
00009 
00010 #include "InfoStream.h"
00011 #include "ComputeMap.h"
00012 #include "Compute.h"
00013 
00014 #include "charm++.h"
00015 
00016 #define MIN_DEBUG_LEVEL 4
00017 //#define DEBUGM
00018 #include "Debug.h"
00019 
00020 #include "ComputeNonbondedMICKernel.h"
00021 
00022 ComputeMap* ComputeMap::instance;
00023 
00024 // Singleton method
00025 ComputeMap *ComputeMap::Instance() {
00026   if (instance == 0) {
00027     instance = new ComputeMap;  // this is never deleted
00028   }
00029   return instance;
00030 }
00031 
00032 
00033 //----------------------------------------------------------------------
00034 ComputeMap::ComputeMap(void)
00035 {
00036   nComputes=0;
00037   computePtrs=0;
00038 }
00039 
00040 //----------------------------------------------------------------------
00041 ComputeMap::~ComputeMap(void)
00042 {
00043   delete [] computePtrs;
00044 }
00045 
00046 void
00047 ComputeMap::checkMap(void)
00048 {
00049   int computeCount = nComputes;
00050   for (int i=0; i<nComputes; i++) {
00051     if (computePtrs[i]) {
00052       computeCount++;
00053       if (! (computePtrs[i]->cid == i)) {
00054         DebugM(4, "ComputeID("<<computePtrs[i]->cid<<") != ComputeID("
00055           << i <<")\n");
00056       }
00057     }
00058   }
00059   DebugM(4, "Compute Count = " << computeCount << "\n");
00060 }
00061 
00062 void ComputeMap::pack (ComputeData *buffer)
00063 {
00064   DebugM(4,"Packing ComputeMap\n");
00065   memcpy(buffer, computeData.begin(), nComputes * sizeof(ComputeData));
00066 }
00067 
00068 void ComputeMap::unpack (int n, ComputeData *ptr)
00069 {
00070   DebugM(4,"Unpacking ComputeMap\n");
00071 
00072   if ( nComputes && n != nComputes ) {
00073     NAMD_bug("number of computes in new ComputeMap has changed!\n");
00074   }
00075 
00076   nComputes = n;
00077   computeData.resize(nComputes);
00078   memcpy(computeData.begin(), ptr, nComputes * sizeof(ComputeData));
00079 }
00080 
00081 void ComputeMap::initPtrs() {
00082   if ( ! computePtrs ) {
00083     computePtrs = new Compute*[nComputes];
00084     memset(computePtrs, 0, nComputes*sizeof(Compute*));
00085   }
00086 }
00087 
00088 void ComputeMap::extendPtrs() {
00089   if ( ! computePtrs ) NAMD_bug("ComputeMap::extendPtrs() 1");
00090   int oldN = nComputes;
00091   nComputes = computeData.size();
00092   if ( nComputes > oldN ) {
00093     Compute **oldPtrs = computePtrs;
00094     computePtrs = new Compute*[nComputes];
00095     memcpy(computePtrs, oldPtrs, oldN*sizeof(Compute*));
00096     memset(computePtrs+oldN, 0, (nComputes-oldN)*sizeof(Compute*));
00097     delete [] oldPtrs;
00098   }
00099 }
00100 
00101 //----------------------------------------------------------------------
00102 int ComputeMap::numPids(ComputeID cid)
00103 {
00104     return computeData[cid].numPids;
00105 }
00106 
00107 //----------------------------------------------------------------------
00108 int ComputeMap::pid(ComputeID cid,int i)
00109 {
00110     return computeData[cid].pids[i].pid;
00111 }
00112 
00113 int ComputeMap::trans(ComputeID cid,int i)
00114 {
00115     return computeData[cid].pids[i].trans;
00116 }
00117 
00118 //----------------------------------------------------------------------
00119 ComputeType ComputeMap::type(ComputeID cid)
00120 {
00121   if (nComputes)
00122     return computeData[cid].type;
00123   else return computeErrorType;
00124 }
00125 
00126 //----------------------------------------------------------------------
00127 int ComputeMap::partition(ComputeID cid)
00128 {
00129   if (nComputes)
00130     return computeData[cid].partition;
00131   else return computeErrorType;
00132 }
00133 //----------------------------------------------------------------------
00134 int ComputeMap::numPartitions(ComputeID cid)
00135 {
00136   if (nComputes)
00137     return computeData[cid].numPartitions;
00138   else return computeErrorType;
00139 }
00140 
00141 //----------------------------------------------------------------------
00142 int ComputeMap::allocateCids()
00143 {
00144   nComputes = 0;
00145   computeData.resize(500);
00146   computeData.resize(0);
00147 
00148   return 0;
00149 }
00150 
00151 //----------------------------------------------------------------------
00152 ComputeID ComputeMap::storeCompute(int inode, int maxPids, 
00153                                    ComputeType type, 
00154                                    int partition,int numPartitions)
00155 {
00156   if (maxPids > numPidsAllocated) {
00157     NAMD_bug("ComputeMap::storeCompute called with maxPids > numPidsAllocated");
00158   }
00159 
00160   int cid;
00161 
00162   cid = nComputes;
00163   nComputes++;
00164   computeData.resize(nComputes);
00165 
00166   computeData[cid].node=inode;
00167 
00168   computeData[cid].type = type;
00169   computeData[cid].partition = partition;
00170   computeData[cid].numPartitions = numPartitions;
00171 
00172   computeData[cid].numPids = 0;
00173 
00174   #if defined(NAMD_MIC)
00175     // Initially in MIC runs, all computes are mapped to the host.  The host vs
00176     //   device LDB scheme will change this mapping later.
00177     computeData[cid].directToDevice = 0;
00178   #endif
00179 
00180   return cid;
00181 }
00182 
00183 //----------------------------------------------------------------------
00184 ComputeID ComputeMap::cloneCompute(ComputeID src, int partition)
00185 {
00186   const int cid = computeData.size();
00187   computeData.resize(cid+1);
00188 
00189   computeData[cid] = computeData[src];
00190   computeData[cid].partition = partition;
00191   computeData[cid].node = -1;
00192 
00193   return cid;
00194 }
00195 
00196 //----------------------------------------------------------------------
00197 void ComputeMap::newPid(ComputeID cid, PatchID pid, int trans)
00198 {
00199   computeData[cid].pids[computeData[cid].numPids].pid=pid;
00200   computeData[cid].pids[computeData[cid].numPids].trans=trans;
00201   computeData[cid].numPids++;
00202 }
00203 
00204 //----------------------------------------------------------------------
00205 void ComputeMap::printComputeMap(void)
00206 {
00207   DebugM(2,"---------------------------------------");
00208   DebugM(2,"---------------------------------------\n");
00209 
00210   DebugM(2,"nComputes = " << nComputes << '\n');
00211   DebugM(2,"nAllocated = " << nComputes << '\n');
00212   for(int i=0; i < nComputes; i++)
00213   {
00214     DebugM(2,"Compute " << i << '\n');
00215     DebugM(2,"  node = " << computeData[i].node << '\n');
00216     DebugM(2,"  numPids = " << computeData[i].numPids << '\n');
00217     for(int j=0; j < computeData[i].numPids; j++)
00218     {
00219       DebugM(2,computeData[i].pids[j].pid);
00220       if (!((j+1) % 6))
00221         DebugM(2,'\n');
00222     }
00223     DebugM(2,"\n---------------------------------------");
00224     DebugM(2,"---------------------------------------\n");
00225 
00226   }
00227 
00228 char *fname;
00229 #ifdef MEM_OPT_VERSION
00230 fname = "computeMap.opt";
00231 #else
00232 fname = "computeMap.orig";
00233 #endif
00234 
00235   FILE *ofp = fopen(fname, "w");
00236   fprintf(ofp,"---------------------------------------");
00237   fprintf(ofp,"---------------------------------------\n");
00238 
00239   fprintf(ofp,"nComputes = %d\n", nComputes);
00240   fprintf(ofp,"nAllocated = %d\n", nComputes);
00241   for(int i=0; i < nComputes; i++)
00242   {
00243     fprintf(ofp,"Compute %d\n", i);
00244     fprintf(ofp,"  node = %d\n",computeData[i].node);
00245     fprintf(ofp,"  numPids = %d\n",computeData[i].numPids);
00246         fprintf(ofp,"  type = %d\n",computeData[i].type);
00247     for(int j=0; j < computeData[i].numPids; j++)
00248     {
00249       fprintf(ofp,"%d ",computeData[i].pids[j].pid);
00250       if (!((j+1) % 6))
00251         fprintf(ofp,"\n");
00252     }
00253     fprintf(ofp,"\n---------------------------------------");
00254     fprintf(ofp,"---------------------------------------\n");
00255 
00256   }
00257 
00258 fclose(ofp);
00259 
00260 }
00261 
00262 void ComputeMap::saveComputeMap(const char *fname)
00263 {
00264   static int count = 0;
00265   char f[128];
00266   sprintf(f, "%s.%d", fname, count++);
00267   FILE *fp = fopen(f, "w");
00268   CmiAssert(fp != NULL);
00269   fprintf(fp, "%d\n", nComputes);
00270   for(int i=0; i < nComputes; i++)
00271   {
00272     fprintf(fp, "%d\n", computeData[i].node);
00273   }
00274   fclose(fp);
00275   CkPrintf("ComputeMap has been stored in %s.\n", f);
00276 }
00277 
00278 void ComputeMap::loadComputeMap(const char *fname)
00279 {
00280   FILE *fp = fopen(fname, "r");
00281   CmiAssert(fp != NULL);
00282   int n;
00283   fscanf(fp, "%d\n", &n);
00284   CmiAssert(n == nComputes);
00285   for(int i=0; i < nComputes; i++)
00286   {
00287     fscanf(fp, "%d\n", &computeData[i].node);
00288   }
00289   fclose(fp);
00290 }
00291 
00292 //----------------------------------------------------------------------
00293 #if defined(NAMD_MIC)
00294 
00295 void ComputeMap::setDirectToDevice(const ComputeID cid, const int d) {
00296   if (cid < 0 || cid >= nComputes) {
00297     NAMD_bug("ComputeMap::setDirectToDevice() called with an invalid cid value");
00298   }
00299   computeData[cid].directToDevice = ((d == 0) ? (0) : (1));
00300 }
00301 
00302 int ComputeMap::directToDevice(const ComputeID cid) const {
00303   if (cid < 0 || cid >= nComputes) {
00304     NAMD_bug("ComputeMap::directToDevice() called with an invalid cid value");
00305   }
00306   return computeData[cid].directToDevice;
00307 }
00308 
00309 #endif // defined(NAMD_MIC)

Generated on Mon Nov 20 01:17:11 2017 for NAMD by  doxygen 1.4.7