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 #include "MStream.h"
00014 
00015 #include "charm++.h"
00016 
00017 #define MIN_DEBUG_LEVEL 4
00018 //#define DEBUGM
00019 #include "Debug.h"
00020 
00021 #include "ComputeNonbondedMICKernel.h"
00022 
00023 ComputeMap* ComputeMap::instance;
00024 
00025 // Singleton method
00026 ComputeMap *ComputeMap::Instance() {
00027   if (instance == 0) {
00028     instance = new ComputeMap;  // this is never deleted
00029   }
00030   return instance;
00031 }
00032 
00033 
00034 //----------------------------------------------------------------------
00035 ComputeMap::ComputeMap(void)
00036 {
00037   nComputes=0;
00038   computePtrs=0;
00039 }
00040 
00041 //----------------------------------------------------------------------
00042 ComputeMap::~ComputeMap(void)
00043 {
00044   delete [] computePtrs;
00045 }
00046 
00047 void
00048 ComputeMap::checkMap(void)
00049 {
00050   int computeCount = nComputes;
00051   for (int i=0; i<nComputes; i++) {
00052     if (computePtrs[i]) {
00053       computeCount++;
00054       if (! (computePtrs[i]->cid == i)) {
00055         DebugM(4, "ComputeID("<<computePtrs[i]->cid<<") != ComputeID("
00056           << i <<")\n");
00057       }
00058     }
00059   }
00060   DebugM(4, "Compute Count = " << computeCount << "\n");
00061 }
00062 
00063 void ComputeMap::pack (MOStream *msg)
00064 {
00065   DebugM(4,"Packing ComputeMap\n");
00066   msg->put(nComputes);
00067   msg->put(nComputes,computeData.begin());
00068 }
00069 
00070 void ComputeMap::unpack (MIStream *msg)
00071 {
00072   DebugM(4,"Unpacking ComputeMap\n");
00073   int old = nComputes;
00074   msg->get(nComputes);
00075   if ( old && old != nComputes ) {
00076     NAMD_bug("number of computes in new ComputeMap has changed!\n");
00077   }
00078   computeData.resize(nComputes);
00079   msg->get(nComputes,computeData.begin());
00080 }
00081 
00082 void ComputeMap::initPtrs() {
00083   if ( ! computePtrs ) {
00084     computePtrs = new Compute*[nComputes];
00085     memset(computePtrs, 0, nComputes*sizeof(Compute*));
00086   }
00087 }
00088 
00089 void ComputeMap::extendPtrs() {
00090   if ( ! computePtrs ) NAMD_bug("ComputeMap::extendPtrs() 1");
00091   int oldN = nComputes;
00092   nComputes = computeData.size();
00093   if ( nComputes > oldN ) {
00094     Compute **oldPtrs = computePtrs;
00095     computePtrs = new Compute*[nComputes];
00096     memcpy(computePtrs, oldPtrs, oldN*sizeof(Compute*));
00097     memset(computePtrs+oldN, 0, (nComputes-oldN)*sizeof(Compute*));
00098     delete [] oldPtrs;
00099   }
00100 }
00101 
00102 //----------------------------------------------------------------------
00103 int ComputeMap::numPids(ComputeID cid)
00104 {
00105     return computeData[cid].numPids;
00106 }
00107 
00108 //----------------------------------------------------------------------
00109 int ComputeMap::pid(ComputeID cid,int i)
00110 {
00111     return computeData[cid].pids[i].pid;
00112 }
00113 
00114 int ComputeMap::trans(ComputeID cid,int i)
00115 {
00116     return computeData[cid].pids[i].trans;
00117 }
00118 
00119 //----------------------------------------------------------------------
00120 ComputeType ComputeMap::type(ComputeID cid)
00121 {
00122   if (nComputes)
00123     return computeData[cid].type;
00124   else return computeErrorType;
00125 }
00126 
00127 //----------------------------------------------------------------------
00128 int ComputeMap::partition(ComputeID cid)
00129 {
00130   if (nComputes)
00131     return computeData[cid].partition;
00132   else return computeErrorType;
00133 }
00134 //----------------------------------------------------------------------
00135 int ComputeMap::numPartitions(ComputeID cid)
00136 {
00137   if (nComputes)
00138     return computeData[cid].numPartitions;
00139   else return computeErrorType;
00140 }
00141 
00142 //----------------------------------------------------------------------
00143 int ComputeMap::allocateCids()
00144 {
00145   nComputes = 0;
00146   computeData.resize(500);
00147   computeData.resize(0);
00148 
00149   return 0;
00150 }
00151 
00152 //----------------------------------------------------------------------
00153 ComputeID ComputeMap::storeCompute(int inode, int maxPids, 
00154                                    ComputeType type, 
00155                                    int partition,int numPartitions)
00156 {
00157   if (maxPids > numPidsAllocated) {
00158     NAMD_bug("ComputeMap::storeCompute called with maxPids > numPidsAllocated");
00159   }
00160 
00161   int cid;
00162 
00163   cid = nComputes;
00164   nComputes++;
00165   computeData.resize(nComputes);
00166 
00167   computeData[cid].node=inode;
00168 
00169   computeData[cid].type = type;
00170   computeData[cid].partition = partition;
00171   computeData[cid].numPartitions = numPartitions;
00172 
00173   computeData[cid].numPids = 0;
00174 
00175   #if defined(NAMD_MIC)
00176     // Initially in MIC runs, all computes are mapped to the host.  The host vs
00177     //   device LDB scheme will change this mapping later.
00178     computeData[cid].directToDevice = 0;
00179   #endif
00180 
00181   return cid;
00182 }
00183 
00184 //----------------------------------------------------------------------
00185 ComputeID ComputeMap::cloneCompute(ComputeID src, int partition)
00186 {
00187   const int cid = computeData.size();
00188   computeData.resize(cid+1);
00189 
00190   computeData[cid] = computeData[src];
00191   computeData[cid].partition = partition;
00192   computeData[cid].node = -1;
00193 
00194   return cid;
00195 }
00196 
00197 //----------------------------------------------------------------------
00198 void ComputeMap::newPid(ComputeID cid, PatchID pid, int trans)
00199 {
00200   computeData[cid].pids[computeData[cid].numPids].pid=pid;
00201   computeData[cid].pids[computeData[cid].numPids].trans=trans;
00202   computeData[cid].numPids++;
00203 }
00204 
00205 //----------------------------------------------------------------------
00206 void ComputeMap::printComputeMap(void)
00207 {
00208   DebugM(2,"---------------------------------------");
00209   DebugM(2,"---------------------------------------\n");
00210 
00211   DebugM(2,"nComputes = " << nComputes << '\n');
00212   DebugM(2,"nAllocated = " << nComputes << '\n');
00213   for(int i=0; i < nComputes; i++)
00214   {
00215     DebugM(2,"Compute " << i << '\n');
00216     DebugM(2,"  node = " << computeData[i].node << '\n');
00217     DebugM(2,"  numPids = " << computeData[i].numPids << '\n');
00218     for(int j=0; j < computeData[i].numPids; j++)
00219     {
00220       DebugM(2,computeData[i].pids[j].pid);
00221       if (!((j+1) % 6))
00222         DebugM(2,'\n');
00223     }
00224     DebugM(2,"\n---------------------------------------");
00225     DebugM(2,"---------------------------------------\n");
00226 
00227   }
00228 
00229 #ifdef MEM_OPT_VERSION
00230 const char *fname = "computeMap.opt";
00231 #else
00232 const char *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 Fri Jan 19 01:17:11 2018 for NAMD by  doxygen 1.4.7