Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | 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 #include "ObjectArena.h"
00014 
00015 #include "charm++.h"
00016 
00017 #define MIN_DEBUG_LEVEL 4
00018 //#define DEBUGM
00019 #include "Debug.h"
00020 
00021 // Singleton method
00022 ComputeMap *ComputeMap::Instance() {
00023   if (CkpvAccess(ComputeMap_instance) == 0) {
00024     CkpvAccess(ComputeMap_instance) = new ComputeMap;   // this is never deleted
00025   }
00026   return CkpvAccess(ComputeMap_instance);
00027 }
00028 
00029 
00030 //----------------------------------------------------------------------
00031 ComputeMap::ComputeMap(void)
00032 {
00033   nComputes=0;
00034   nPatchBased=0;
00035   nAtomBased=0;
00036   patchArena=0;
00037 }
00038 
00039 //----------------------------------------------------------------------
00040 ComputeMap::~ComputeMap(void)
00041 {
00042   delete patchArena;
00043 }
00044 
00045 void
00046 ComputeMap::checkMap(void)
00047 {
00048   int computeCount = nComputes;
00049   for (int i=0; i<nComputes; i++) {
00050     if (computeData[i].compute) {
00051       computeCount++;
00052       if (! (computeData[i].compute->cid == i)) {
00053         DebugM(4, "ComputeID("<<computeData[i].compute->cid<<") != ComputeID("
00054           << i <<")\n");
00055       }
00056     }
00057   }
00058   DebugM(4, "Compute Count = " << computeCount << "\n");
00059 }
00060 
00061 #undef PACK
00062 #define PACK(type,data) { memcpy(b, &data, sizeof(type)); b += sizeof(type); }
00063 
00064 int ComputeMap::packSize(void)
00065 {
00066   int i;
00067   int size = 0;
00068   size += 4 * sizeof(int);
00069   for(i=0;i<nComputes;++i)
00070   {
00071     size += sizeof(ComputeData);
00072     size += computeData[i].numPidsAllocated * sizeof(PatchRec);
00073   }
00074   return size;
00075 }
00076 
00077 void ComputeMap::pack (char *buffer)
00078 {
00079   DebugM(4,"Packing ComputeMap\n");
00080   int i,j;
00081 
00082   // fill in the data
00083   char *b = buffer;
00084   PACK(int,nPatchBased);
00085   PACK(int,nAtomBased);
00086   PACK(int,nComputes);
00087   for(i=0;i<nComputes;++i)
00088   {
00089     PACK(ComputeData,computeData[i]);
00090     for(j=0;j<computeData[i].numPidsAllocated;++j)
00091       PACK(PatchRec,computeData[i].pids[j]);
00092   }
00093 }
00094 
00095 #undef UNPACK
00096 #define UNPACK(type,data) { memcpy(&data, b, sizeof(type)); b += sizeof(type); }
00097 
00098 void ComputeMap::unpack (char *ptr)
00099 {
00100   // Must copy over the Compute * to new ComputeMap! 
00101   ResizeArray<ComputeData> oldComputeData = computeData;
00102   delete patchArena;  // oldComputeData[i].pids now invalid!
00103   patchArena = new ObjectArena<PatchRec>;  // use for computeData[i].pids
00104   patchArena->setBlockSize(256);
00105   int oldNComputes = nComputes;
00106 
00107   DebugM(4,"Unpacking ComputeMap\n");
00108   int i,j;
00109   char *b = (char*)ptr;
00110   UNPACK(int,nPatchBased);
00111   UNPACK(int,nAtomBased);
00112   {
00113     // defeat some over-zealous compilers
00114     int nComputes_tmp;
00115     UNPACK(int,nComputes_tmp);
00116     nComputes = nComputes_tmp;
00117   }
00118   ResizeArray<ComputeData> newComputeData(nComputes);
00119   computeData = newComputeData;
00120   for(i=0;i<nComputes;++i)
00121   {
00122     UNPACK(ComputeData,computeData[i]);
00123     computeData[i].pids =
00124                  patchArena->getNewArray(computeData[i].numPidsAllocated);
00125     for(j=0;j<computeData[i].numPidsAllocated;++j)
00126       UNPACK(PatchRec,computeData[i].pids[j]);
00127   }
00128 
00129   if (oldNComputes) {
00130     if (nComputes != oldNComputes) {
00131       NAMD_die("number of computes in new patchmap has changed!\n");
00132       return;
00133     }
00134 
00135     for (int i=0; i<nComputes; i++) {
00136       computeData[i].compute = oldComputeData[i].compute;
00137     }
00138   }
00139   DebugM(4,"Done Unpacking ComputeMap\n");
00140 }
00141 
00142 //----------------------------------------------------------------------
00143 int ComputeMap::numPatchBased(void)
00144 {
00145   return nPatchBased;
00146 }
00147 
00148 //----------------------------------------------------------------------
00149 int ComputeMap::numAtomBased(void)
00150 {
00151   return nAtomBased;
00152 }
00153 
00154 //----------------------------------------------------------------------
00155 int ComputeMap::isPatchBased(ComputeID cid)
00156 {
00157     return computeData[cid].patchBased;
00158 }
00159 
00160 //----------------------------------------------------------------------
00161 int ComputeMap::isAtomBased(ComputeID cid)
00162 {
00163     return !computeData[cid].patchBased;
00164 }
00165 
00166 //----------------------------------------------------------------------
00167 int ComputeMap::numPids(ComputeID cid)
00168 {
00169     return computeData[cid].numPids;
00170 }
00171 
00172 //----------------------------------------------------------------------
00173 int ComputeMap::pid(ComputeID cid,int i)
00174 {
00175     return computeData[cid].pids[i].pid;
00176 }
00177 
00178 int ComputeMap::trans(ComputeID cid,int i)
00179 {
00180     return computeData[cid].pids[i].trans;
00181 }
00182 
00183 //----------------------------------------------------------------------
00184 ComputeType ComputeMap::type(ComputeID cid)
00185 {
00186   if (nComputes)
00187     return computeData[cid].type;
00188   else return computeErrorType;
00189 }
00190 
00191 //----------------------------------------------------------------------
00192 int ComputeMap::partition(ComputeID cid)
00193 {
00194   if (nComputes)
00195     return computeData[cid].partition;
00196   else return computeErrorType;
00197 }
00198 //----------------------------------------------------------------------
00199 int ComputeMap::numPartitions(ComputeID cid)
00200 {
00201   if (nComputes)
00202     return computeData[cid].numPartitions;
00203   else return computeErrorType;
00204 }
00205 
00206 //----------------------------------------------------------------------
00207 int ComputeMap::allocateCids()
00208 {
00209   delete patchArena;  // oldComputeData[i].pids now invalid!
00210   patchArena = new ObjectArena<PatchRec>;  // use for computeData[i].pids
00211   patchArena->setBlockSize(256);
00212   nComputes = nPatchBased = nAtomBased = 0;
00213   computeData.resize(500);
00214   computeData.resize(0);
00215 
00216   return 0;
00217 }
00218 
00219 //----------------------------------------------------------------------
00220 ComputeID ComputeMap::storeCompute(int inode, int maxPids, 
00221                                    ComputeType type, 
00222                                    int partition,int numPartitions)
00223 {
00224   int cid;
00225 
00226   cid = nComputes;
00227   nComputes++;
00228   computeData.resize(nComputes);
00229 
00230   computeData[cid].node=inode;
00231 
00232   computeData[cid].type = type;
00233   computeData[cid].partition = partition;
00234   computeData[cid].numPartitions = numPartitions;
00235 
00236   computeData[cid].patchBased = true;
00237   nPatchBased++;
00238 
00239   computeData[cid].numPids = 0;
00240   computeData[cid].pids = patchArena->getNewArray(maxPids);
00241 
00242   computeData[cid].numPidsAllocated = maxPids;
00243 
00244   return cid;
00245 }
00246 
00247 //----------------------------------------------------------------------
00248 void ComputeMap::newPid(ComputeID cid, PatchID pid, int trans)
00249 {
00250   if (computeData[cid].numPids == computeData[cid].numPidsAllocated)
00251     computeData[cid].pids = 0;  // crash if out of space for dependents
00252 
00253   computeData[cid].pids[computeData[cid].numPids].pid=pid;
00254   computeData[cid].pids[computeData[cid].numPids].trans=trans;
00255   computeData[cid].numPids++;
00256 }
00257 
00258 //----------------------------------------------------------------------
00259 void ComputeMap::printComputeMap(void)
00260 {
00261   iout << iINFO << "CREATING " << nComputes << " COMPUTE OBJECTS\n" << endi;
00262   DebugM(2,"---------------------------------------");
00263   DebugM(2,"---------------------------------------\n");
00264 
00265   DebugM(2,"nComputes = " << nComputes << '\n');
00266   DebugM(2,"nPatchBased = " << nPatchBased << '\n');
00267   DebugM(2,"nAtomBased = " << nAtomBased << '\n');
00268   DebugM(2,"nAllocated = " << nComputes << '\n');
00269   for(int i=0; i < nComputes; i++)
00270   {
00271     DebugM(2,"Compute " << i << '\n');
00272     DebugM(2,"  node = " << computeData[i].node << '\n');
00273     DebugM(2,"  patchBased = " << computeData[i].patchBased << '\n');
00274     DebugM(2,"  numPids = " << computeData[i].numPids << '\n');
00275     DebugM(2,"  numPidsAllocated = " << computeData[i].numPidsAllocated << '\n');
00276     for(int j=0; j < computeData[i].numPids; j++)
00277     {
00278       DebugM(2,computeData[i].pids[j].pid);
00279       if (!((j+1) % 6))
00280         DebugM(2,'\n');
00281     }
00282     DebugM(2,"\n---------------------------------------");
00283     DebugM(2,"---------------------------------------\n");
00284 
00285   }
00286 }
00287 

Generated on Fri Sep 5 04:07:14 2008 for NAMD by  doxygen 1.3.9.1