test/CompareWithAztecOO/cxx_main.cpp

Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //                IFPACK
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include "Ifpack_ConfigDefs.h"
00030 
00031 #ifdef HAVE_MPI
00032 #include "Epetra_MpiComm.h"
00033 #else
00034 #include "Epetra_SerialComm.h"
00035 #endif
00036 #include "Epetra_CrsMatrix.h"
00037 #include "Epetra_Vector.h"
00038 #include "Epetra_LinearProblem.h"
00039 #include "Epetra_Time.h"
00040 #include "Galeri_Maps.h"
00041 #include "Galeri_CrsMatrices.h"
00042 #include "Teuchos_ParameterList.hpp"
00043 #include "Ifpack_AdditiveSchwarz.h"
00044 #include "AztecOO.h"
00045 #include "Ifpack_Graph_Epetra_RowMatrix.h"
00046 #include "Ifpack_PointRelaxation.h"
00047 #include "Ifpack_IC.h"
00048 #include "Ifpack_ILU.h"
00049 #include "Ifpack_Amesos.h"
00050 
00051 bool verbose = false;
00052 
00053 bool CompareWithAztecOO(Epetra_LinearProblem& Problem, const string what,
00054                        int Overlap, int ival)
00055 {
00056 
00057   AztecOO AztecOOSolver(Problem);
00058   AztecOOSolver.SetAztecOption(AZ_solver,AZ_gmres);
00059   AztecOOSolver.SetAztecOption(AZ_output,AZ_none);
00060   AztecOOSolver.SetAztecOption(AZ_overlap,Overlap);
00061   AztecOOSolver.SetAztecOption(AZ_graph_fill,ival);
00062   AztecOOSolver.SetAztecOption(AZ_poly_ord, ival);
00063   AztecOOSolver.SetAztecParam(AZ_drop, 0.0);
00064   AztecOOSolver.SetAztecParam(AZ_athresh, 0.0);
00065   AztecOOSolver.SetAztecParam(AZ_rthresh, 0.0);
00066 
00067   Epetra_MultiVector& RHS = *(Problem.GetRHS());
00068   Epetra_MultiVector& LHS = *(Problem.GetLHS());
00069   Epetra_RowMatrix* A = Problem.GetMatrix();
00070 
00071   LHS.Random();
00072   A->Multiply(false,LHS,RHS);
00073 
00074   Teuchos::ParameterList List;
00075   List.set("fact: level-of-fill", ival);
00076   List.set("relaxation: sweeps", ival);
00077   List.set("relaxation: damping factor", 1.0);
00078   List.set("relaxation: zero starting solution", true);
00079  
00080   //default combine mode is as for AztecOO
00081   List.set("schwarz: combine mode", Zero);
00082 
00083   Epetra_Time Time(A->Comm());
00084 
00085   Ifpack_Preconditioner* Prec = 0;
00086   
00087   if (what == "Jacobi") {
00088     Prec = new Ifpack_PointRelaxation(A);
00089     List.set("relaxation: type", "Jacobi");
00090     AztecOOSolver.SetAztecOption(AZ_precond,AZ_Jacobi);
00091     AztecOOSolver.SetAztecOption(AZ_reorder,0);
00092   }
00093   else if (what == "IC no reord") {
00094     Prec = new Ifpack_AdditiveSchwarz<Ifpack_IC>(A,Overlap);
00095     AztecOOSolver.SetAztecOption(AZ_precond,AZ_dom_decomp);
00096     AztecOOSolver.SetAztecOption(AZ_subdomain_solve,AZ_icc);
00097     AztecOOSolver.SetAztecOption(AZ_reorder,0);
00098   }
00099   else if (what == "IC reord") {
00100     Prec = new Ifpack_AdditiveSchwarz<Ifpack_IC>(A,Overlap);
00101     List.set("schwarz: use reordering", true);
00102     AztecOOSolver.SetAztecOption(AZ_precond,AZ_dom_decomp);
00103     AztecOOSolver.SetAztecOption(AZ_subdomain_solve,AZ_icc);
00104     AztecOOSolver.SetAztecOption(AZ_reorder,1);
00105   }
00106   else if (what == "ILU no reord") {
00107     Prec = new Ifpack_AdditiveSchwarz<Ifpack_ILU>(A,Overlap);
00108     AztecOOSolver.SetAztecOption(AZ_precond,AZ_dom_decomp);
00109     AztecOOSolver.SetAztecOption(AZ_subdomain_solve,AZ_ilu);
00110     AztecOOSolver.SetAztecOption(AZ_reorder,0);
00111   }
00112   else if (what == "ILU reord") {
00113     Prec = new Ifpack_AdditiveSchwarz<Ifpack_ILU>(A,Overlap);
00114     List.set("schwarz: use reordering", true);
00115     AztecOOSolver.SetAztecOption(AZ_precond,AZ_dom_decomp);
00116     AztecOOSolver.SetAztecOption(AZ_subdomain_solve,AZ_ilu);
00117     AztecOOSolver.SetAztecOption(AZ_reorder,1);
00118   }
00119 #ifdef HAVE_IFPACK_AMESOS
00120   else if (what == "LU") {
00121     Prec = new Ifpack_AdditiveSchwarz<Ifpack_Amesos>(A,Overlap);
00122     List.set("amesos: solver type", "Klu");
00123     AztecOOSolver.SetAztecOption(AZ_precond,AZ_dom_decomp);
00124     AztecOOSolver.SetAztecOption(AZ_subdomain_solve,AZ_lu);
00125   }
00126 #endif
00127   else {
00128     cerr << "Option not recognized" << endl;
00129     exit(EXIT_FAILURE);
00130   }
00131 
00132   // ==================================== //
00133   // Solve with AztecOO's preconditioners //
00134   // ==================================== //
00135 
00136   LHS.PutScalar(0.0);
00137 
00138   Time.ResetStartTime();
00139   AztecOOSolver.Iterate(150,1e-5);
00140 
00141   if (verbose) {
00142     cout << endl;
00143     cout << "==================================================" << endl;
00144     cout << "Testing `" << what << "', Overlap = "
00145          << Overlap << ", ival = " << ival << endl;
00146     cout << endl;
00147     cout << "[AztecOO] Total time = " << Time.ElapsedTime() << " (s)" << endl;
00148     cout << "[AztecOO] Residual   = " << AztecOOSolver.TrueResidual() << " (s)" << endl;
00149     cout << "[AztecOO] Iterations = " << AztecOOSolver.NumIters() << endl;
00150     cout << endl;
00151   }
00152 
00153   int AztecOOPrecIters = AztecOOSolver.NumIters();
00154 
00155   // =========================================== //
00156   // Create the IFPACK preconditioner and solver //
00157   // =========================================== //
00158  
00159   Epetra_Time Time2(A->Comm());
00160   assert(Prec != 0);
00161   IFPACK_CHK_ERR(Prec->SetParameters(List));
00162 
00163   Time.ResetStartTime();
00164   IFPACK_CHK_ERR(Prec->Initialize());
00165   if (verbose)
00166     cout << "[IFPACK] Time for Initialize() = "
00167          << Time.ElapsedTime() << " (s)" << endl;
00168 
00169   Time.ResetStartTime();
00170   IFPACK_CHK_ERR(Prec->Compute());
00171   if (verbose)
00172     cout << "[IFPACK] Time for Compute() = "
00173          << Time.ElapsedTime() << " (s)" << endl;
00174 
00175 
00176   AztecOOSolver.SetPrecOperator(Prec);
00177 
00178   LHS.PutScalar(0.0);
00179 
00180   Time.ResetStartTime();
00181   AztecOOSolver.Iterate(150,1e-5);
00182 
00183   if (verbose) {
00184     cout << "[IFPACK] Total time = " << Time2.ElapsedTime() << " (s)" << endl;
00185     cout << "[IFPACK] Residual   = " << AztecOOSolver.TrueResidual() << " (s)" << endl;
00186     cout << "[IFPACK] Iterations = " << AztecOOSolver.NumIters() << endl;
00187     cout << endl;
00188   }
00189 
00190   int IFPACKPrecIters = AztecOOSolver.NumIters();
00191 
00192   delete Prec;
00193 
00194   if (IFPACK_ABS(AztecOOPrecIters - IFPACKPrecIters) > 3) {
00195     cerr << "TEST FAILED (" << AztecOOPrecIters << " != " 
00196          << IFPACKPrecIters << ")" << endl;
00197     return(false);
00198   }
00199   else
00200     return(true);
00201 
00202 }
00203 
00204 // ====================================================================== 
00205 int main(int argc, char *argv[])
00206 {
00207 
00208 #ifdef HAVE_MPI
00209   MPI_Init(&argc,&argv);
00210   Epetra_MpiComm Comm(MPI_COMM_WORLD);
00211 #else
00212   Epetra_SerialComm Comm;
00213 #endif
00214 
00215   int nx = 30;
00216   Teuchos::ParameterList GaleriList;
00217   GaleriList.set("n", nx * nx);
00218   GaleriList.set("nx", nx);
00219   GaleriList.set("ny", nx);
00220 
00221   Epetra_Map* Map = Galeri::CreateMap("Linear", Comm, GaleriList);
00222   Epetra_RowMatrix* A = Galeri::CreateCrsMatrix("Laplace2D", Map, GaleriList);
00223   Epetra_Vector LHS(*Map);
00224   Epetra_Vector RHS(*Map);
00225   Epetra_LinearProblem Problem(A, &LHS, &RHS);
00226 
00227   int TestPassed = true;
00228 
00229   // Jacobi as in AztecOO (no overlap)
00230   for (int ival = 1 ; ival < 10 ; ival += 3) {
00231     TestPassed = TestPassed && 
00232       CompareWithAztecOO(Problem,"Jacobi",0,ival);
00233   }
00234 
00235 #if 0
00236   // AztecOO with IC and overlap complains, also with
00237   // large fill-ins (in parallel)
00238   TestPassed = TestPassed && 
00239     CompareWithAztecOO(Problem,"IC no reord",0,0);
00240   TestPassed = TestPassed && 
00241     CompareWithAztecOO(Problem,"IC reord",0,0);
00242 
00243   vector<string> Tests;
00244   // now test solvers that accept overlap
00245   Tests.push_back("ILU no reord");
00246   Tests.push_back("ILU reord");
00247   // following requires --enable-aztecoo-azlu
00248 #ifdef HAVE_IFPACK_AMESOS
00249   //Tests.push_back("LU");
00250 #endif
00251 
00252   for (unsigned int i = 0 ; i < Tests.size() ; ++i) {
00253     for (int overlap = 0 ; overlap < 1 ; overlap += 2) {
00254       for (int ival = 0 ; ival < 10 ; ival += 4)
00255         TestPassed = TestPassed && 
00256           CompareWithAztecOO(Problem,Tests[i],overlap,ival);
00257     }
00258   }
00259 #endif
00260 
00261   if (!TestPassed) {
00262     cerr << "Test `CompareWithAztecOO.exe' FAILED!" << endl;
00263     exit(EXIT_FAILURE);
00264   }
00265 
00266   delete Map;
00267   delete A;
00268 
00269 #ifdef HAVE_MPI
00270   MPI_Finalize() ; 
00271 #endif
00272   cout << "Test `CompareWithAztecOO.exe' passed!" << endl;
00273 
00274   exit(EXIT_SUCCESS);
00275 }

Generated on Thu Sep 18 12:37:21 2008 for Ifpack Package Browser (Single Doxygen Collection) by doxygen 1.3.9.1