Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

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

Generated on Sat May 25 04:07:15 2013 for NAMD by  doxygen 1.3.9.1