00001
00002 #include "mpi.h"
00003 #include "petsc.h"
00004 #include "sys.h"
00005 #include "TreeNode.h"
00006 #include "omg.h"
00007 #include "oda.h"
00008 #include "omgJac.h"
00009 #include <cstdlib>
00010 #include "handleStencils.h"
00011 #include "colors.h"
00012 #include "externVars.h"
00013
00014 static char help[] = "Regular Grid MG";
00015
00016
00017 #ifdef MPI_WTIME_IS_GLOBAL
00018 #undef MPI_WTIME_IS_GLOBAL
00019 #endif
00020
00021
00022
00023 #ifdef PETSC_USE_LOG
00024 int Jac1MultEvent;
00025 int Jac1DiagEvent;
00026 int Jac1FinestMultEvent;
00027 int Jac1FinestDiagEvent;
00028 int matPropEvent;
00029
00030 int Jac2MultEvent;
00031 int Jac2DiagEvent;
00032 int Jac2FinestMultEvent;
00033 int Jac2FinestDiagEvent;
00034
00035 int Jac3MultEvent;
00036 int Jac3DiagEvent;
00037 int Jac3FinestMultEvent;
00038 int Jac3FinestDiagEvent;
00039 #endif
00040
00041 double***** LaplacianType1Stencil;
00042 double**** LaplacianType2Stencil;
00043 double***** MassType1Stencil;
00044 double**** MassType2Stencil;
00045 double****** ShapeFnStencil;
00046
00047 int main(int argc, char ** argv ) {
00048 int size, rank;
00049 bool incCorner = 1;
00050 unsigned int dim=3;
00051 unsigned int maxDepth=29;
00052 bool compressLut=true;
00053 std::vector<ot::TreeNode> balOct;
00054 double mgLoadFac = 2.0;
00055 unsigned int regLev = 2;
00056
00057 PetscInitialize(&argc, &argv, "options", help);
00058 ot::RegisterEvents();
00059
00060 ot::DAMG_Initialize(MPI_COMM_WORLD);
00061
00062 #ifdef PETSC_USE_LOG
00063 PetscLogEventRegister(&matPropEvent,"matProp",PETSC_VIEWER_COOKIE);
00064 PetscLogEventRegister(&Jac1DiagEvent,"ODAmatDiag",PETSC_VIEWER_COOKIE);
00065 PetscLogEventRegister(&Jac1MultEvent,"ODAmatMult",PETSC_VIEWER_COOKIE);
00066 PetscLogEventRegister(&Jac1FinestDiagEvent,"ODAmatDiagFinest",PETSC_VIEWER_COOKIE);
00067 PetscLogEventRegister(&Jac1FinestMultEvent,"ODAmatMultFinest",PETSC_VIEWER_COOKIE);
00068
00069 PetscLogEventRegister(&Jac2DiagEvent,"OMGmatDiag-2",PETSC_VIEWER_COOKIE);
00070 PetscLogEventRegister(&Jac2MultEvent,"OMGmatMult-2",PETSC_VIEWER_COOKIE);
00071 PetscLogEventRegister(&Jac2FinestDiagEvent,"OMGmatDiagFinest-2",PETSC_VIEWER_COOKIE);
00072 PetscLogEventRegister(&Jac2FinestMultEvent,"OMGmatMultFinest-2",PETSC_VIEWER_COOKIE);
00073
00074 PetscLogEventRegister(&Jac3DiagEvent,"OMGmatDiag-3",PETSC_VIEWER_COOKIE);
00075 PetscLogEventRegister(&Jac3MultEvent,"OMGmatMult-3",PETSC_VIEWER_COOKIE);
00076 PetscLogEventRegister(&Jac3FinestDiagEvent,"OMGmatDiagFinest-3",PETSC_VIEWER_COOKIE);
00077 PetscLogEventRegister(&Jac3FinestMultEvent,"OMGmatMultFinest-3",PETSC_VIEWER_COOKIE);
00078
00079 int stages[1];
00080 PetscLogStageRegister(&stages[0],"Solve");
00081 #endif
00082
00083 MPI_Comm_size(MPI_COMM_WORLD,&size);
00084 MPI_Comm_rank(MPI_COMM_WORLD,&rank);
00085 if(argc > 1) {
00086 regLev = atoi(argv[1]);
00087 }
00088 if(argc > 2) {
00089 maxDepth = atoi(argv[2]);
00090 }
00091 if(argc > 3) {
00092 dim = atoi(argv[3]);
00093 }
00094 if(argc > 4) { incCorner = (bool)(atoi(argv[4]));}
00095 if(argc > 5) { compressLut = (bool)(atoi(argv[5]));}
00096 if(argc > 6) { mgLoadFac = atof(argv[6]); }
00097
00098 #ifdef PETSC_USE_LOG
00099 PetscLogStagePush(stages[0]);
00100 #endif
00101 MPI_Barrier(MPI_COMM_WORLD);
00102
00103 ot::DAMG *damg;
00104 int nlevels = 1;
00105 unsigned int dof = 1;
00106
00107 createRegularOctree(balOct, regLev, dim, maxDepth, MPI_COMM_WORLD);
00108
00109 PetscInt nlevelsPetscInt = nlevels;
00110 PetscOptionsGetInt(0, "-nlevels", &nlevelsPetscInt, 0);
00111 nlevels = nlevelsPetscInt;
00112
00113
00114 MPI_Barrier(MPI_COMM_WORLD);
00115
00116 if(!rank) {
00117 std::cout<<" nlevels initial: "<<nlevels<<std::endl;
00118 }
00119
00120 ot::DAMGCreateAndSetDA(PETSC_COMM_WORLD, nlevels, NULL, &damg,
00121 balOct, dof, mgLoadFac, compressLut, incCorner);
00122
00123 if(!rank) {
00124 std::cout<<" nlevels final: "<<nlevels<<std::endl;
00125 }
00126
00127 MPI_Barrier(MPI_COMM_WORLD);
00128
00129 if(!rank) {
00130 std::cout << "Created DA for all levels."<< std::endl;
00131 }
00132
00133 MPI_Barrier(MPI_COMM_WORLD);
00134 ot::PrintDAMG(damg);
00135 MPI_Barrier(MPI_COMM_WORLD);
00136
00137 for(int i=0;i<nlevels;i++) {
00138 bool isRegOct = isRegularGrid(damg[i]->da);
00139 if(!rank) {
00140 std::cout<<"Level "<<i<<" is regular? "<<isRegOct<<std::endl;
00141 }
00142 }
00143
00144
00145 SetUserContexts(damg);
00146
00147 if(!rank) {
00148 std::cout << "Set User Contexts all levels."<< std::endl;
00149 }
00150
00151 MPI_Barrier(MPI_COMM_WORLD);
00152
00153 PetscInt jacType = 1;
00154 PetscOptionsGetInt(0,"-jacType",&jacType,0);
00155
00156 PetscInt rhsType = 1;
00157 PetscOptionsGetInt(0,"-rhsType",&rhsType,0);
00158
00159 createLmatType2(LaplacianType2Stencil);
00160 createMmatType2(MassType2Stencil);
00161 if(jacType == 3) {
00162 createLmatType1(LaplacianType1Stencil);
00163 createMmatType1(MassType1Stencil);
00164 }
00165 createShFnMat(ShapeFnStencil);
00166
00167 if(!rank) {
00168 std::cout << "Created Stencils."<< std::endl;
00169 }
00170
00171
00172 PetscErrorCode (*ComputeRHSHandle)(ot::DAMG damg,Vec rhs) = NULL;
00173 PetscErrorCode (*CreateJacobianHandle)(ot::DAMG damg,Mat *B) = NULL;
00174 PetscErrorCode (*ComputeJacobianHandle)(ot::DAMG damg,Mat J, Mat B) = NULL;
00175
00176 if(rhsType == 0) {
00177 ComputeRHSHandle = ComputeRHS0;
00178 } else if (rhsType == 1) {
00179 ComputeRHSHandle = ComputeRHS1;
00180 } else if (rhsType == 2) {
00181 ComputeRHSHandle = ComputeRHS2;
00182 } else if (rhsType == 3) {
00183 ComputeRHSHandle = ComputeRHS3;
00184 } else if (rhsType == 4) {
00185 ComputeRHSHandle = ComputeRHS4;
00186 } else if (rhsType == 5) {
00187 ComputeRHSHandle = ComputeRHS5;
00188 } else if (rhsType == 6) {
00189 ComputeRHSHandle = ComputeRHS6;
00190 } else if (rhsType == 7) {
00191 ComputeRHSHandle = ComputeRHS7;
00192 } else if (rhsType == 8) {
00193 ComputeRHSHandle = ComputeRHS8;
00194 } else {
00195 assert(false);
00196 }
00197
00198 if(jacType == 1) {
00199 CreateJacobianHandle = CreateJacobian1;
00200 ComputeJacobianHandle = ComputeJacobian1;
00201 } else if (jacType == 2) {
00202 CreateJacobianHandle = CreateJacobian2;
00203 ComputeJacobianHandle = ComputeJacobian2;
00204 } else if (jacType == 3) {
00205 CreateJacobianHandle = CreateJacobian3;
00206 ComputeJacobianHandle = ComputeJacobian3;
00207
00208
00209 for(int i = 1; i < (nlevels-1); i++) {
00210 ot::DAMGCreateJMatrix(damg[i], CreateJacobianHandle);
00211 }
00212 } else {
00213 assert(false);
00214 }
00215
00216
00217
00218 if (jacType == 1) {
00219 ot::getPrivateMatricesForKSP_Shell = getPrivateMatricesForKSP_Shell_Jac1;
00220 } else if (jacType == 2) {
00221 ot::getPrivateMatricesForKSP_Shell = getPrivateMatricesForKSP_Shell_Jac2;
00222 } else if (jacType == 3) {
00223 ot::getPrivateMatricesForKSP_Shell = getPrivateMatricesForKSP_Shell_Jac3;
00224 } else {
00225 assert(false);
00226 }
00227
00228 ot::DAMGSetKSP(damg, CreateJacobianHandle, ComputeJacobianHandle, ComputeRHSHandle);
00229
00230 if(!rank) {
00231 std::cout<<"Solving u-Lu=f"<<std::endl;
00232 }
00233
00234 iC(ot::DAMGSolve(damg));
00235
00236 destroyLmatType2(LaplacianType2Stencil);
00237 destroyMmatType2(MassType2Stencil);
00238 if(jacType == 3) {
00239 destroyLmatType1(LaplacianType1Stencil);
00240 destroyMmatType1(MassType1Stencil);
00241 }
00242 destroyShFnMat(ShapeFnStencil);
00243
00244 MPI_Barrier(MPI_COMM_WORLD);
00245
00246 DestroyUserContexts(damg);
00247
00248 if (!rank) {
00249 std::cout << GRN << "Destroyed User Contexts." << NRM << std::endl;
00250 }
00251
00252 MPI_Barrier(MPI_COMM_WORLD);
00253
00254 iC(DAMGDestroy(damg));
00255
00256 if (!rank) {
00257 std::cout << GRN << "Destroyed DAMG" << NRM << std::endl;
00258 }
00259
00260 #ifdef PETSC_USE_LOG
00261 PetscLogStagePop();
00262 #endif
00263 balOct.clear();
00264
00265 if (!rank) {
00266 std::cout << GRN << "Finalizing ..." << NRM << std::endl;
00267 }
00268 ot::DAMG_Finalize();
00269 PetscFinalize();
00270 }
00271