Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

tstMg.C

Go to the documentation of this file.
00001 
00002 #include "mpi.h"
00003 #include "petsc.h"
00004 #include "sys.h"
00005 #include "TreeNode.h"
00006 #include "parUtils.h"
00007 #include "omg.h"
00008 #include "oda.h"
00009 #include "omgJac.h"
00010 #include "handleStencils.h"
00011 #include <cstdlib>
00012 #include <cstring>
00013 #include "colors.h"
00014 #include "externVars.h"
00015 #include "dendro.h"
00016 
00017 static char help[] = "Scalar Problem";
00018 
00019 //Don't want time to be synchronized. Need to check load imbalance.
00020 #ifdef MPI_WTIME_IS_GLOBAL
00021 #undef MPI_WTIME_IS_GLOBAL
00022 #endif
00023 
00024 #ifdef PETSC_USE_LOG
00025 //user-defined variables
00026 int Jac1DiagEvent;
00027 int Jac1MultEvent;
00028 int Jac1FinestDiagEvent;
00029 int Jac1FinestMultEvent;
00030 
00031 int Jac2DiagEvent;
00032 int Jac2MultEvent;
00033 int Jac2FinestDiagEvent;
00034 int Jac2FinestMultEvent;
00035 
00036 int Jac3DiagEvent;
00037 int Jac3MultEvent;
00038 int Jac3FinestDiagEvent;
00039 int Jac3FinestMultEvent;
00040 #endif
00041 
00042 double***** LaplacianType1Stencil; 
00043 double**** LaplacianType2Stencil; 
00044 double***** MassType1Stencil; 
00045 double**** MassType2Stencil; 
00046 double****** ShapeFnStencil;
00047 
00048 int main(int argc, char ** argv ) {     
00049   int size, rank;
00050   bool incCorner = 1;  
00051   unsigned int numPts;
00052   unsigned int solveU = 0;
00053   unsigned int writeB = 0;  
00054   char Kstr[20];
00055   char pFile[50],bFile[50],uFile[50];
00056   double gSize[3];
00057   unsigned int ptsLen;
00058   unsigned int maxNumPts= 1;
00059   unsigned int dim=3;
00060   unsigned int maxDepth=30;
00061   bool compressLut=true;
00062   double localTime, totalTime;
00063   double startTime, endTime;
00064   DendroIntL locSz, totalSz;
00065   std::vector<ot::TreeNode> linOct, balOct;
00066   std::vector<double> pts;
00067   double mgLoadFac = 2.0;
00068 
00069   PetscInitialize(&argc,&argv,"options",help);
00070   ot::RegisterEvents();
00071 
00072   ot::DAMG_Initialize(MPI_COMM_WORLD);
00073 
00074 #ifdef PETSC_USE_LOG
00075   PetscLogEventRegister(&Jac1DiagEvent,"ODAmatDiag",PETSC_VIEWER_COOKIE);
00076   PetscLogEventRegister(&Jac1MultEvent,"ODAmatMult",PETSC_VIEWER_COOKIE);
00077   PetscLogEventRegister(&Jac1FinestDiagEvent,"ODAmatDiagFinest",PETSC_VIEWER_COOKIE);
00078   PetscLogEventRegister(&Jac1FinestMultEvent,"ODAmatMultFinest",PETSC_VIEWER_COOKIE);
00079 
00080   PetscLogEventRegister(&Jac2DiagEvent,"OMGmatDiag-2",PETSC_VIEWER_COOKIE);
00081   PetscLogEventRegister(&Jac2MultEvent,"OMGmatMult-2",PETSC_VIEWER_COOKIE);
00082   PetscLogEventRegister(&Jac2FinestDiagEvent,"OMGmatDiagFinest-2",PETSC_VIEWER_COOKIE);
00083   PetscLogEventRegister(&Jac2FinestMultEvent,"OMGmatMultFinest-2",PETSC_VIEWER_COOKIE);
00084 
00085   PetscLogEventRegister(&Jac3DiagEvent,"OMGmatDiag-3",PETSC_VIEWER_COOKIE);
00086   PetscLogEventRegister(&Jac3MultEvent,"OMGmatMult-3",PETSC_VIEWER_COOKIE);
00087   PetscLogEventRegister(&Jac3FinestDiagEvent,"OMGmatDiagFinest-3",PETSC_VIEWER_COOKIE);
00088   PetscLogEventRegister(&Jac3FinestMultEvent,"OMGmatMultFinest-3",PETSC_VIEWER_COOKIE);
00089 
00090   int stages[3];
00091   PetscLogStageRegister(&stages[0],"P2O.");
00092   PetscLogStageRegister(&stages[1],"Bal");  
00093   PetscLogStageRegister(&stages[2],"Solve");  
00094 #endif
00095 
00096   MPI_Comm_size(MPI_COMM_WORLD,&size);
00097   MPI_Comm_rank(MPI_COMM_WORLD,&rank);
00098   if(argc < 3) {
00099     std::cerr << "Usage: " << argv[0] << "inpfile  maxDepth[30] solveU[0]\
00100       writeB[0] dim[3] maxNumPtsPerOctant[1] incCorner[1] compressLut[1] mgLoadFac[2.0] " << std::endl;
00101     return -1;
00102   }
00103   if(argc > 2) {
00104     maxDepth = atoi(argv[2]);
00105   }
00106   if(argc > 3) {
00107     solveU = atoi(argv[3]);
00108   }
00109   if(argc > 4) {
00110     writeB = atoi(argv[4]);
00111   }
00112   if(argc > 5) {
00113     dim = atoi(argv[5]);
00114   }
00115   if(argc > 6) {
00116     maxNumPts = atoi(argv[6]);
00117   }
00118   if(argc > 7) { incCorner = (bool)(atoi(argv[7]));}  
00119   if(argc > 8) { compressLut = (bool)(atoi(argv[8]));}
00120   if(argc > 9) { mgLoadFac = atof(argv[9]); }
00121 
00122   strcpy(bFile,argv[1]);
00123   ot::int2str(rank,Kstr);
00124   strcat(bFile,Kstr);
00125   strcat(bFile,"_\0");
00126   ot::int2str(size,Kstr);
00127   strcat(bFile,Kstr);
00128   strcpy(pFile,bFile);
00129   strcpy(uFile,bFile);
00130   strcat(bFile,"_Bal.ot\0");
00131   strcat(pFile,".pts\0");
00132   strcat(uFile,".sol\0");
00133 
00134   //Points2Octree....
00135   MPI_Barrier(MPI_COMM_WORLD);  
00136   if(!rank){
00137     std::cout << " reading  "<<pFile<<std::endl; // Point size
00138   }
00139   ot::readPtsFromFile(pFile, pts);
00140   if(!rank){
00141     std::cout << " finished reading  "<<pFile<<std::endl; // Point size
00142   }
00143   MPI_Barrier(MPI_COMM_WORLD);  
00144   ptsLen = pts.size();
00145   std::vector<ot::TreeNode> tmpNodes;
00146   for(int i=0;i<ptsLen;i+=3) {
00147     if( (pts[i] > 0.0) &&
00148         (pts[i+1] > 0.0)  
00149         && (pts[i+2] > 0.0) &&
00150         ( ((unsigned int)(pts[i]*((double)(1u<<maxDepth)))) < (1u<<maxDepth))  &&
00151         ( ((unsigned int)(pts[i+1]*((double)(1u<<maxDepth)))) < (1u<<maxDepth))  &&
00152         ( ((unsigned int)(pts[i+2]*((double)(1u<<maxDepth)))) < (1u<<maxDepth)) ) {
00153       tmpNodes.push_back( ot::TreeNode((unsigned int)(pts[i]*(double)(1u<<maxDepth)),
00154             (unsigned int)(pts[i+1]*(double)(1u<<maxDepth)),
00155             (unsigned int)(pts[i+2]*(double)(1u<<maxDepth)),
00156             maxDepth,dim,maxDepth) );
00157     }
00158   }
00159   pts.clear();
00160   par::removeDuplicates<ot::TreeNode>(tmpNodes,false,MPI_COMM_WORLD);   
00161   linOct = tmpNodes;
00162   tmpNodes.clear();
00163   par::partitionW<ot::TreeNode>(linOct, NULL,MPI_COMM_WORLD);
00164   // reduce and only print the total ...
00165   locSz = linOct.size();
00166   DendroIntL totalNumPts;
00167   par::Mpi_Reduce<DendroIntL>(&locSz, &totalNumPts, 1, MPI_SUM, 0, MPI_COMM_WORLD);
00168   MPI_Barrier(MPI_COMM_WORLD);  
00169   if(rank==0) {
00170     std::cout<<"# pts= " << totalNumPts<<std::endl;
00171   }
00172   MPI_Barrier(MPI_COMM_WORLD);  
00173 
00174   PetscTruth setMatPropsUsingPts;
00175   PetscOptionsHasName(0,"-setMatPropsUsingPts",&setMatPropsUsingPts);
00176 
00177   std::vector<ot::TreeNode> matPropNodes;
00178   if(setMatPropsUsingPts) {
00179     matPropNodes = linOct;
00180   }
00181 
00182   pts.resize(3*(linOct.size()));
00183   ptsLen = (3*(linOct.size()));
00184 
00185   for(int i=0;i<linOct.size();i++) {
00186     pts[3*i] = (((double)(linOct[i].getX())) + 0.5)/((double)(1u<<maxDepth));
00187     pts[(3*i)+1] = (((double)(linOct[i].getY())) +0.5)/((double)(1u<<maxDepth));
00188     pts[(3*i)+2] = (((double)(linOct[i].getZ())) +0.5)/((double)(1u<<maxDepth));
00189   }//end for i
00190   linOct.clear();
00191   gSize[0] = 1.;
00192   gSize[1] = 1.;
00193   gSize[2] = 1.;
00194 
00195   PetscTruth usingRegularOctree;
00196   PetscInt regLev;
00197   PetscOptionsHasName(0,"-useRegularOctreeAtLevel",&usingRegularOctree);
00198   PetscOptionsGetInt(0,"-useRegularOctreeAtLevel",&regLev,0);
00199 
00200   if(usingRegularOctree) {
00201     if(!rank) {
00202       std::cout<<"Creating Regular Fine Grid Octree."<<std::endl;
00203     }
00204     balOct.clear();
00205     createRegularOctree(balOct,regLev,3,maxDepth,MPI_COMM_WORLD);
00206   } else {   
00207     MPI_Barrier(MPI_COMM_WORLD);        
00208 #ifdef PETSC_USE_LOG
00209     PetscLogStagePush(stages[0]);
00210 #endif
00211     startTime = MPI_Wtime();
00212     ot::points2Octree(pts, gSize, linOct, dim, maxDepth, maxNumPts, MPI_COMM_WORLD);
00213     endTime = MPI_Wtime();
00214 #ifdef PETSC_USE_LOG
00215     PetscLogStagePop();
00216 #endif
00217     localTime = endTime - startTime;
00218     par::Mpi_Reduce<double>(&localTime, &totalTime, 1, MPI_MAX, 0, MPI_COMM_WORLD);
00219     if(!rank){
00220       std::cout <<"P2n Time: "<<totalTime << std::endl;
00221     }
00222     // reduce and only print the total ...
00223     locSz = linOct.size();
00224     par::Mpi_Reduce<DendroIntL>(&locSz, &totalSz, 1, MPI_SUM, 0, MPI_COMM_WORLD);
00225     if(rank==0) {
00226       std::cout<<"# of Unbalanced Octants: " << totalSz<<std::endl;
00227     }
00228     pts.clear();
00229 
00230     //Balancing...
00231     MPI_Barrier(MPI_COMM_WORLD);        
00232 #ifdef PETSC_USE_LOG
00233     PetscLogStagePush(stages[1]);
00234 #endif
00235     startTime = MPI_Wtime();
00236     ot::balanceOctree (linOct, balOct, dim, maxDepth, incCorner, MPI_COMM_WORLD, NULL, NULL);
00237     endTime = MPI_Wtime();
00238 #ifdef PETSC_USE_LOG
00239     PetscLogStagePop();
00240 #endif
00241     linOct.clear();
00242   }
00243 
00244   if(writeB) { 
00245     ot::writeNodesToFile(bFile,balOct);
00246   }
00247   // compute total inp size and output size
00248   locSz = balOct.size();
00249   localTime = endTime - startTime;
00250   par::Mpi_Reduce<DendroIntL>(&locSz, &totalSz, 1, MPI_SUM, 0, MPI_COMM_WORLD);
00251   par::Mpi_Reduce<double>(&localTime, &totalTime, 1, MPI_MAX, 0, MPI_COMM_WORLD);
00252 
00253   if(!rank) {
00254     std::cout << "# of Balanced Octants: "<< totalSz << std::endl;       
00255     std::cout << "bal Time: "<<totalTime << std::endl;
00256   }
00257 
00258   //Solve ...
00259   MPI_Barrier(MPI_COMM_WORLD);  
00260 #ifdef PETSC_USE_LOG
00261   PetscLogStagePush(stages[2]);
00262 #endif
00263 
00264   ot::DAMG        *damg;    
00265   int       nlevels = 1; //number of multigrid levels
00266   PetscInt       numRefinements = 0;
00267   unsigned int       dof =1;// degrees of freedom per node  
00268 
00269   PetscInt nlevelsPetscInt = nlevels;
00270   PetscOptionsGetInt(0, "-nlevels", &nlevelsPetscInt, 0);
00271   nlevels = nlevelsPetscInt;
00272 
00273   PetscOptionsGetInt(0,"-numRefinements",&numRefinements,0);
00274   for(int i = 0; i < numRefinements; i++) {
00275     std::vector<ot::TreeNode> tmpOct = balOct;
00276     balOct.clear();
00277     ot::refineOctree(tmpOct, balOct); 
00278   }
00279 
00280   MPI_Barrier(MPI_COMM_WORLD);  
00281 
00282   if(!rank) {
00283     std::cout<<"nlevels initial: "<<nlevels<<std::endl;
00284   }
00285 
00286   // Note: The user context for all levels will be set separately later.
00287   ot::DAMGCreateAndSetDA(PETSC_COMM_WORLD, nlevels, NULL, &damg,
00288       balOct, dof, mgLoadFac, compressLut, incCorner);
00289 
00290   if(!rank) {
00291     std::cout<<"nlevels final: "<<nlevels<<std::endl;
00292   }
00293 
00294   MPI_Barrier(MPI_COMM_WORLD);  
00295 
00296   if(!rank) {
00297     std::cout << "Created DA for all levels."<< std::endl;
00298   }
00299 
00300   MPI_Barrier(MPI_COMM_WORLD);
00301   ot::PrintDAMG(damg);
00302 
00303   unsigned int* allSizes = NULL;
00304 
00305   if(!rank) {
00306     allSizes = new unsigned int[6*size]; 
00307   }
00308 
00309   for(int lev = 0; lev < nlevels; lev++) {
00310     for(int auxCtr = 0; auxCtr < 2; auxCtr++) {
00311       ot::DA* currDa = NULL;
00312 
00313       if(auxCtr == 0) {
00314         currDa = damg[lev]->da;
00315       }else if(damg[lev]->da_aux) {
00316         currDa = damg[lev]->da_aux;
00317       } else {
00318         break;
00319       }
00320 
00321       unsigned int localSizes[6];
00322 
00323       //Pre-ghost size
00324       localSizes[0] = currDa->getIdxElementBegin();
00325 
00326       //Own element size
00327       localSizes[2] = currDa->getElementSize();
00328 
00329       unsigned int localTotalSize = currDa->getLocalBufferSize();
00330 
00331       //Post-ghost size
00332       localSizes[1] = (localTotalSize - (currDa->getIdxPostGhostBegin()));
00333 
00334       //Independent size
00335       localSizes[3] = currDa->getIndependentSize();
00336 
00337       //PreGhostElementSize
00338       localSizes[4] = currDa->getPreGhostElementSize();
00339 
00340       //Own Boundary size
00341       localSizes[5] = ((currDa->getIdxPostGhostBegin()) - (currDa->getIdxElementEnd()));
00342 
00343       MPI_Gather(localSizes, 6, MPI_UNSIGNED, allSizes, 6,
00344           MPI_UNSIGNED, 0, MPI_COMM_WORLD);
00345 
00346       MPI_Barrier(MPI_COMM_WORLD);
00347 
00348       /*
00349       if(!rank) {
00350         std::cout<<"Printing Info for lev: "<<lev<<" auxCtr: "<<auxCtr<<std::endl; 
00351         for(int i = 0; i < size; i++) {
00352           std::cout<<"Proc: "<<i<<" PreGh: "<<allSizes[6*i]
00353             <<" PostGh: "<<allSizes[(6*i)+1]<<" own: "
00354             <<allSizes[(6*i)+2]<<" Ind: "<<allSizes[(6*i)+3]
00355             <<" preGhElems: "<<allSizes[(6*i)+4]<<" LocalBnd: "
00356             <<allSizes[(6*i)+5]<<std::endl;
00357           fflush(stdout);
00358         }
00359       }
00360       */
00361       fflush(stdout);
00362     }//end da or da_aux
00363   }//end lev
00364 
00365   if(!rank) {
00366     delete [] allSizes;
00367   }
00368 
00369   MPI_Barrier(MPI_COMM_WORLD);
00370 
00371   if(setMatPropsUsingPts) {
00372     PetscTruth setMatPropsAtCoarsest;
00373     PetscOptionsHasName(0,"-setMatPropsAtCoarsest",&setMatPropsAtCoarsest);
00374     if(setMatPropsAtCoarsest) {
00375       //Coarsest is only 1 processor. So to align with coarse blocks, everything
00376       //must be sent to p0
00377       if(!rank) {
00378         par::scatterValues<ot::TreeNode>(matPropNodes, linOct,
00379             totalNumPts, MPI_COMM_WORLD);
00380       } else {
00381         par::scatterValues<ot::TreeNode>(matPropNodes, linOct,
00382             0, MPI_COMM_WORLD);
00383       }
00384       matPropNodes.clear();
00385 
00386       std::vector<double> matPropPts(3*(linOct.size()));
00387       for(int i=0;i<linOct.size();i++) {
00388         matPropPts[3*i] =
00389           (((double)(linOct[i].getX())) + 0.5)/((double)(1u << maxDepth));
00390         matPropPts[(3*i)+1] =
00391           (((double)(linOct[i].getY())) +0.5)/((double)(1u << maxDepth));
00392         matPropPts[(3*i)+2] =
00393           (((double)(linOct[i].getZ())) +0.5)/((double)(1u << maxDepth));
00394       }//end for i
00395       linOct.clear();
00396 
00397       PetscReal lapFac = 0.0;
00398       PetscTruth optFound;
00399       PetscOptionsGetReal("lap","-MatPropFac",&lapFac,&optFound);
00400       std::vector<double> lapJumps((matPropPts.size()/3));
00401       for(int i=0;i<lapJumps.size();i++) {
00402         lapJumps[i] = lapFac;
00403       }
00404       SetCoarseToFineFromPts(damg, matPropPts, lapJumps);
00405     } else {
00406       if(damg[nlevels-1]->da->iAmActive()) {
00407         int npesActive;
00408         int rankActive;
00409 
00410         MPI_Comm_size(damg[nlevels-1]->da->getCommActive(), &npesActive);        
00411         MPI_Comm_rank(damg[nlevels-1]->da->getCommActive(), &rankActive);        
00412 
00413         unsigned int avgSize = (totalNumPts/npesActive);
00414         unsigned int remSize = (totalNumPts % npesActive);
00415 
00416         if(rankActive < remSize) {
00417           par::scatterValues<ot::TreeNode>(matPropNodes, linOct,
00418               (avgSize + 1), MPI_COMM_WORLD);
00419         } else {
00420           par::scatterValues<ot::TreeNode>(matPropNodes, linOct,
00421               avgSize, MPI_COMM_WORLD);
00422         }
00423 
00424         matPropNodes.clear();
00425         for(unsigned int i = 0; i < linOct.size(); i++) {
00426           unsigned int xpt, ypt, zpt;
00427           linOct[i].getAnchor(xpt,ypt,zpt);
00428           ot::TreeNode tmpNode(xpt,ypt,zpt,(maxDepth+1),3,(maxDepth+1));
00429           matPropNodes.push_back(tmpNode); 
00430         }
00431         linOct.clear();
00432 
00433         std::vector<ot::TreeNode> minsArray(npesActive);
00434 
00435         std::vector<ot::TreeNode> blocks = damg[nlevels-1]->da->getBlocks();
00436 
00437         assert(!blocks.empty());
00438 
00439         ot::TreeNode firstBlock = blocks[0];
00440 
00441         par::Mpi_Allgather<ot::TreeNode>(&firstBlock, &(*(minsArray.begin())), 1,
00442             damg[nlevels-1]->da->getCommActive());
00443 
00444         int *sendCnt = new int[npesActive];
00445         int *recvCnt = new int[npesActive];
00446         int *sendOffsets = new int[npesActive];
00447         int *recvOffsets = new int[npesActive];
00448 
00449         //compute the total number of nodes being sent to each proc ...
00450         for (int i = 0; i < npesActive; i++) {
00451           sendCnt[i]=0;
00452           recvCnt[i]=0;
00453         }
00454 
00455         unsigned int blockCtr = 0;
00456         for(unsigned int tempCtr = 0; tempCtr < matPropNodes.size(); tempCtr++) {
00457           while( ((blockCtr + 1) < npesActive) &&
00458               (matPropNodes[tempCtr] >= minsArray[blockCtr+1]) ) {
00459             blockCtr++;
00460           }
00461           if ( (blockCtr + 1) == npesActive) {
00462             sendCnt[blockCtr] += (matPropNodes.size() - tempCtr);
00463             break;
00464           } else {
00465             sendCnt[blockCtr]++;
00466           }
00467         }
00468 
00469         minsArray.clear();
00470 
00471         // communicate with other procs how many you shall be sending and get how
00472         // many to recieve from whom.
00473         par::Mpi_Alltoall<int>(sendCnt, recvCnt, 1, damg[nlevels-1]->da->getCommActive());
00474 
00475         unsigned int totalRecv = 0;
00476         for (unsigned int i = 0; i < npesActive; i++) {
00477           totalRecv += recvCnt[i];
00478         }//end for i
00479 
00480         linOct.resize(totalRecv);
00481 
00482         sendOffsets[0] = 0;
00483         recvOffsets[0] = 0;
00484 
00485         // compute offsets ...
00486         for (int i=1; i < npesActive; i++) {
00487           sendOffsets[i] = sendOffsets[i-1] + sendCnt[i-1];
00488           recvOffsets[i] = recvOffsets[i-1] + recvCnt[i-1];
00489         }//end for i
00490 
00491         // perform All2Allv
00492         par::Mpi_Alltoallv_sparse<ot::TreeNode>(&(*(matPropNodes.begin())), sendCnt, sendOffsets,        
00493             &(*(linOct.begin())), recvCnt, recvOffsets, damg[nlevels-1]->da->getCommActive());
00494 
00495         matPropNodes.clear();
00496 
00497         // clean up ...
00498         delete [] sendCnt;
00499         sendCnt = NULL;
00500 
00501         delete [] recvCnt;
00502         recvCnt = NULL;
00503 
00504         delete [] sendOffsets;
00505         sendOffsets = NULL;
00506 
00507         delete [] recvOffsets;
00508         recvOffsets = NULL;
00509 
00510       } else {
00511         par::scatterValues<ot::TreeNode>(matPropNodes, linOct,
00512             0, MPI_COMM_WORLD);
00513         matPropNodes.clear();
00514       }
00515 
00516       std::vector<double> matPropPts(3*(linOct.size()));
00517       for(int i=0;i<linOct.size();i++) {
00518         matPropPts[3*i] =
00519           (((double)(linOct[i].getX())) + 0.5)/((double)(1u << maxDepth));
00520         matPropPts[(3*i)+1] =
00521           (((double)(linOct[i].getY())) +0.5)/((double)(1u << maxDepth));
00522         matPropPts[(3*i)+2] =
00523           (((double)(linOct[i].getZ())) +0.5)/((double)(1u << maxDepth));
00524       }//end for i
00525       linOct.clear();
00526 
00527       PetscReal lapFac = 0.0;
00528       PetscTruth optFound;
00529       PetscOptionsGetReal("lap","-MatPropFac",&lapFac,&optFound);
00530       std::vector<double> lapJumps((matPropPts.size()/3));
00531       for(int i=0;i<lapJumps.size();i++) {
00532         lapJumps[i] = lapFac;
00533       }
00534       SetUserContextsFromPts(damg, matPropPts, lapJumps); 
00535     }
00536   } else {
00537     SetUserContexts(damg);
00538   }
00539 
00540   MPI_Barrier(MPI_COMM_WORLD);
00541   if(!rank) {
00542     std::cout << "Set User Contexts all levels."<< std::endl;
00543   }
00544 
00545   MPI_Barrier(MPI_COMM_WORLD);
00546 
00547   PetscInt       jacType = 1;
00548   PetscOptionsGetInt(0,"-jacType",&jacType,0);
00549 
00550   MPI_Barrier(MPI_COMM_WORLD);
00551   if(!rank) {
00552     std::cout << "Creating stencils..."<< std::endl;
00553   }
00554   MPI_Barrier(MPI_COMM_WORLD);
00555 
00556   createLmatType2(LaplacianType2Stencil);
00557 
00558   createMmatType2(MassType2Stencil);
00559 
00560   if(jacType == 3) {
00561     createLmatType1(LaplacianType1Stencil);
00562     createMmatType1(MassType1Stencil);
00563   }
00564   createShFnMat(ShapeFnStencil);
00565 
00566   MPI_Barrier(MPI_COMM_WORLD);
00567   if(!rank) {
00568     std::cout << "Created all stencils."<< std::endl;
00569   }
00570   MPI_Barrier(MPI_COMM_WORLD);
00571 
00572   PetscInt rhsType = 2;
00573   PetscOptionsGetInt(0,"-rhsType",&rhsType,0);
00574 
00575   //Function handles
00576   PetscErrorCode (*ComputeRHSHandle)(ot::DAMG damg,Vec rhs) = NULL;
00577   PetscErrorCode (*CreateJacobianHandle)(ot::DAMG damg,Mat *B) = NULL;
00578   PetscErrorCode (*ComputeJacobianHandle)(ot::DAMG damg,Mat J, Mat B) = NULL;
00579 
00580   if(rhsType == 0) {
00581     ComputeRHSHandle = ComputeRHS0;
00582   } else if (rhsType == 1) {
00583     ComputeRHSHandle = ComputeRHS1;
00584   } else if (rhsType == 2) {
00585     ComputeRHSHandle = ComputeRHS2;
00586   } else if (rhsType == 3) {
00587     ComputeRHSHandle = ComputeRHS3;
00588   } else if (rhsType == 4) {
00589     ComputeRHSHandle = ComputeRHS4;
00590   } else if (rhsType == 5) {
00591     ComputeRHSHandle = ComputeRHS5;
00592   } else if (rhsType == 6) {
00593     ComputeRHSHandle = ComputeRHS6;
00594   } else if (rhsType == 7) {
00595     ComputeRHSHandle = ComputeRHS7;
00596   } else if (rhsType == 8) {
00597     ComputeRHSHandle = ComputeRHS8;
00598   } else {
00599     assert(false);
00600   }
00601 
00602   if(jacType == 1) {
00603     CreateJacobianHandle = CreateJacobian1;
00604     ComputeJacobianHandle = ComputeJacobian1;
00605   } else if (jacType == 2) {
00606     CreateJacobianHandle = CreateJacobian2;
00607     ComputeJacobianHandle = ComputeJacobian2;
00608   } else if (jacType == 3) {
00609     CreateJacobianHandle = CreateJacobian3;
00610     ComputeJacobianHandle = ComputeJacobian3;
00611     //Skip the finest and the coarsest levels. For the other levels, J and B
00612     //must be different
00613     for(int i = 1; i < (nlevels-1); i++) {
00614       ot::DAMGCreateJMatrix(damg[i], CreateJacobianHandle);
00615     }
00616   } else {
00617     assert(false);
00618   }
00619 
00620   //Global Function Handles for using KSP_Shell (will be used @ the coarsest grid if not all
00621   //processors are active on the coarsest grid)
00622   if (jacType == 1) {
00623     ot::getPrivateMatricesForKSP_Shell = getPrivateMatricesForKSP_Shell_Jac1;
00624   } else if (jacType == 2) {
00625     ot::getPrivateMatricesForKSP_Shell = getPrivateMatricesForKSP_Shell_Jac2;
00626   } else if (jacType == 3) {
00627     ot::getPrivateMatricesForKSP_Shell = getPrivateMatricesForKSP_Shell_Jac3;
00628   } else {
00629     assert(false);
00630   }
00631 
00632   ot::DAMGSetKSP(damg, CreateJacobianHandle, ComputeJacobianHandle, ComputeRHSHandle);
00633 
00634   MPI_Barrier(MPI_COMM_WORLD);
00635   if(!rank) {
00636     std::cout << "Called DAMGSetKSP"<< std::endl;
00637   }
00638   MPI_Barrier(MPI_COMM_WORLD);
00639 
00640   PetscReal norm2;
00641   PetscReal normInf;
00642 
00643   PetscTruth setRandomGuess = PETSC_FALSE;
00644   PetscOptionsHasName(0,"-setRandomGuess",&setRandomGuess);
00645 
00646   if(setRandomGuess) { 
00647     ot::DAMGSetInitialGuess(damg,ot::DAMGInitialGuessCurrent);
00648 
00649     PetscRandom rctx;  
00650     PetscRandomCreate(PETSC_COMM_WORLD,&rctx);
00651     PetscRandomSetType(rctx,PETSCRAND48);
00652     PetscInt randomSeed = 12345;
00653     PetscOptionsGetInt(0,"-randomSeed",&randomSeed,0);
00654     if(!rank) {
00655       std::cout<<"Using Random Seed: "<<randomSeed<<std::endl;
00656     }
00657     PetscRandomSetSeed(rctx,randomSeed);
00658     PetscRandomSeed(rctx);
00659     PetscRandomSetFromOptions(rctx);
00660 
00661     VecSetRandom((DAMGGetx(damg)),rctx);
00662 
00663     PetscRandomDestroy(rctx);
00664 
00665     VecNorm((DAMGGetx(damg)),NORM_INFINITY,&normInf);
00666     VecNorm((DAMGGetx(damg)),NORM_2,&norm2);
00667 
00668     MPI_Barrier(MPI_COMM_WORLD);
00669     if(!rank) {
00670       std::cout << "Solving with random intial guess"<< std::endl;
00671       std::cout<<"Initial Norm2: "<<norm2<<" NormInf: "<<normInf<<std::endl;
00672     }
00673     MPI_Barrier(MPI_COMM_WORLD);
00674   }else {
00675     MPI_Barrier(MPI_COMM_WORLD);
00676     if(!rank) {
00677       std::cout << "Solving with 0-intial guess"<< std::endl;
00678     }
00679     MPI_Barrier(MPI_COMM_WORLD);
00680   }
00681 
00682   startTime = MPI_Wtime();
00683   ot::DAMGSolve(damg);
00684   endTime = MPI_Wtime();
00685 
00686   localTime = (endTime - startTime);
00687   par::Mpi_Reduce<double>(&localTime, &totalTime, 1, MPI_MAX, 0, MPI_COMM_WORLD);
00688 
00689   MPI_Barrier(MPI_COMM_WORLD);
00690 
00691   if (!rank) {
00692     std::cout << GRN << "Done Solve" << NRM << std::endl;
00693     std::cout << "Solve Time: "<<totalTime << std::endl;
00694   }
00695 
00696   MPI_Barrier(MPI_COMM_WORLD);
00697 
00698   destroyLmatType2(LaplacianType2Stencil);
00699   destroyMmatType2(MassType2Stencil);
00700   if(jacType == 3) {
00701     destroyLmatType1(LaplacianType1Stencil);
00702     destroyMmatType1(MassType1Stencil);
00703   }
00704   destroyShFnMat(ShapeFnStencil);
00705 
00706   if (!rank) {
00707     std::cout << GRN << "Destroyed Stencils" << NRM << std::endl;
00708   }
00709 
00710   DestroyUserContexts(damg);
00711 
00712   if (!rank) {
00713     std::cout << GRN << "Destroyed User Contexts." << NRM << std::endl;
00714   }
00715 
00716   MPI_Barrier(MPI_COMM_WORLD);
00717 
00718   DAMGDestroy(damg);
00719 
00720   if (!rank) {
00721     std::cout << GRN << "Destroyed DAMG" << NRM << std::endl;
00722   }
00723   MPI_Barrier(MPI_COMM_WORLD);
00724 
00725 #ifdef PETSC_USE_LOG
00726   PetscLogStagePop();
00727 #endif
00728   balOct.clear();
00729 
00730   ot::DAMG_Finalize();
00731   if (!rank) {
00732     std::cout << GRN << "Finalizing ..." << NRM << std::endl;
00733   }
00734   MPI_Barrier(MPI_COMM_WORLD);
00735   PetscFinalize();
00736 }//end function
00737 

Generated on Tue Mar 24 16:14:00 2009 for DENDRO by  doxygen 1.3.9.1