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 "Teuchos_RefCountPtr.hpp"
00044 #include "Ifpack_AdditiveSchwarz.h"
00045 #include "AztecOO.h"
00046 #include "Ifpack_Graph_Epetra_RowMatrix.h"
00047 #include "Ifpack_PointRelaxation.h"
00048 #include "Ifpack_IC.h"
00049 #include "Ifpack_ILU.h"
00050 #include "Ifpack_Amesos.h"
00051 
00052 bool verbose = false;
00053 
00054 bool CompareWithAztecOO(Epetra_LinearProblem& Problem, const string what,
00055                        int Overlap, int ival)
00056 {
00057 
00058   AztecOO AztecOOSolver(Problem);
00059   AztecOOSolver.SetAztecOption(AZ_solver,AZ_gmres);
00060   AztecOOSolver.SetAztecOption(AZ_output,AZ_none);
00061   AztecOOSolver.SetAztecOption(AZ_overlap,Overlap);
00062   AztecOOSolver.SetAztecOption(AZ_graph_fill,ival);
00063   AztecOOSolver.SetAztecOption(AZ_poly_ord, ival);
00064   AztecOOSolver.SetAztecParam(AZ_drop, 0.0);
00065   AztecOOSolver.SetAztecParam(AZ_athresh, 0.0);
00066   AztecOOSolver.SetAztecParam(AZ_rthresh, 0.0);
00067 
00068   Epetra_MultiVector& RHS = *(Problem.GetRHS());
00069   Epetra_MultiVector& LHS = *(Problem.GetLHS());
00070   Teuchos::RefCountPtr<Epetra_RowMatrix> A = Teuchos::rcp(Problem.GetMatrix(), false);
00071 
00072   LHS.Random();
00073   A->Multiply(false,LHS,RHS);
00074 
00075   Teuchos::ParameterList List;
00076   List.set("fact: level-of-fill", ival);
00077   List.set("relaxation: sweeps", ival);
00078   List.set("relaxation: damping factor", 1.0);
00079   List.set("relaxation: zero starting solution", true);
00080  
00081   //default combine mode is as for AztecOO
00082   List.set("schwarz: combine mode", Zero);
00083 
00084   Epetra_Time Time(A->Comm());
00085 
00086   Teuchos::RefCountPtr<Ifpack_Preconditioner> Prec;
00087   
00088   if (what == "Jacobi") {
00089     Prec = Teuchos::rcp( new Ifpack_PointRelaxation(&*A) );
00090     List.set("relaxation: type", "Jacobi");
00091     AztecOOSolver.SetAztecOption(AZ_precond,AZ_Jacobi);
00092     AztecOOSolver.SetAztecOption(AZ_reorder,0);
00093   }
00094   else if (what == "IC no reord") {
00095     Prec = Teuchos::rcp( new Ifpack_AdditiveSchwarz<Ifpack_IC>(&*A,Overlap) );
00096     AztecOOSolver.SetAztecOption(AZ_precond,AZ_dom_decomp);
00097     AztecOOSolver.SetAztecOption(AZ_subdomain_solve,AZ_icc);
00098     AztecOOSolver.SetAztecOption(AZ_reorder,0);
00099   }
00100   else if (what == "IC reord") {
00101     Prec = Teuchos::rcp( new Ifpack_AdditiveSchwarz<Ifpack_IC>(&*A,Overlap) );
00102     List.set("schwarz: use reordering", true);
00103     AztecOOSolver.SetAztecOption(AZ_precond,AZ_dom_decomp);
00104     AztecOOSolver.SetAztecOption(AZ_subdomain_solve,AZ_icc);
00105     AztecOOSolver.SetAztecOption(AZ_reorder,1);
00106   }
00107   else if (what == "ILU no reord") {
00108     Prec = Teuchos::rcp( new Ifpack_AdditiveSchwarz<Ifpack_ILU>(&*A,Overlap) );
00109     AztecOOSolver.SetAztecOption(AZ_precond,AZ_dom_decomp);
00110     AztecOOSolver.SetAztecOption(AZ_subdomain_solve,AZ_ilu);
00111     AztecOOSolver.SetAztecOption(AZ_reorder,0);
00112   }
00113   else if (what == "ILU reord") {
00114     Prec = Teuchos::rcp( new Ifpack_AdditiveSchwarz<Ifpack_ILU>(&*A,Overlap) );
00115     List.set("schwarz: use reordering", true);
00116     AztecOOSolver.SetAztecOption(AZ_precond,AZ_dom_decomp);
00117     AztecOOSolver.SetAztecOption(AZ_subdomain_solve,AZ_ilu);
00118     AztecOOSolver.SetAztecOption(AZ_reorder,1);
00119   }
00120 #ifdef HAVE_IFPACK_AMESOS
00121   else if (what == "LU") {
00122     Prec = Teuchos::rcp( new Ifpack_AdditiveSchwarz<Ifpack_Amesos>(&*A,Overlap) );
00123     List.set("amesos: solver type", "Klu");
00124     AztecOOSolver.SetAztecOption(AZ_precond,AZ_dom_decomp);
00125     AztecOOSolver.SetAztecOption(AZ_subdomain_solve,AZ_lu);
00126   }
00127 #endif
00128   else {
00129     cerr << "Option not recognized" << endl;
00130     exit(EXIT_FAILURE);
00131   }
00132 
00133   // ==================================== //
00134   // Solve with AztecOO's preconditioners //
00135   // ==================================== //
00136 
00137   LHS.PutScalar(0.0);
00138 
00139   Time.ResetStartTime();
00140   AztecOOSolver.Iterate(150,1e-5);
00141 
00142   if (verbose) {
00143     cout << endl;
00144     cout << "==================================================" << endl;
00145     cout << "Testing `" << what << "', Overlap = "
00146          << Overlap << ", ival = " << ival << endl;
00147     cout << endl;
00148     cout << "[AztecOO] Total time = " << Time.ElapsedTime() << " (s)" << endl;
00149     cout << "[AztecOO] Residual   = " << AztecOOSolver.TrueResidual() << " (s)" << endl;
00150     cout << "[AztecOO] Iterations = " << AztecOOSolver.NumIters() << endl;
00151     cout << endl;
00152   }
00153 
00154   int AztecOOPrecIters = AztecOOSolver.NumIters();
00155 
00156   // =========================================== //
00157   // Create the IFPACK preconditioner and solver //
00158   // =========================================== //
00159  
00160   Epetra_Time Time2(A->Comm());
00161   assert(Prec != Teuchos::null);
00162   IFPACK_CHK_ERR(Prec->SetParameters(List));
00163 
00164   Time.ResetStartTime();
00165   IFPACK_CHK_ERR(Prec->Initialize());
00166   if (verbose)
00167     cout << "[IFPACK] Time for Initialize() = "
00168          << Time.ElapsedTime() << " (s)" << endl;
00169 
00170   Time.ResetStartTime();
00171   IFPACK_CHK_ERR(Prec->Compute());
00172   if (verbose)
00173     cout << "[IFPACK] Time for Compute() = "
00174          << Time.ElapsedTime() << " (s)" << endl;
00175 
00176 
00177   AztecOOSolver.SetPrecOperator(&*Prec);
00178 
00179   LHS.PutScalar(0.0);
00180 
00181   Time.ResetStartTime();
00182   AztecOOSolver.Iterate(150,1e-5);
00183 
00184   if (verbose) {
00185     cout << "[IFPACK] Total time = " << Time2.ElapsedTime() << " (s)" << endl;
00186     cout << "[IFPACK] Residual   = " << AztecOOSolver.TrueResidual() << " (s)" << endl;
00187     cout << "[IFPACK] Iterations = " << AztecOOSolver.NumIters() << endl;
00188     cout << endl;
00189   }
00190 
00191   int IFPACKPrecIters = AztecOOSolver.NumIters();
00192 
00193   if (IFPACK_ABS(AztecOOPrecIters - IFPACKPrecIters) > 3) {
00194     cerr << "TEST FAILED (" << AztecOOPrecIters << " != " 
00195          << IFPACKPrecIters << ")" << endl;
00196     return(false);
00197   }
00198   else
00199     return(true);
00200 
00201 }
00202 
00203 // ====================================================================== 
00204 int main(int argc, char *argv[])
00205 {
00206 
00207 #ifdef HAVE_MPI
00208   MPI_Init(&argc,&argv);
00209   Epetra_MpiComm Comm(MPI_COMM_WORLD);
00210 #else
00211   Epetra_SerialComm Comm;
00212 #endif
00213 
00214   int nx = 30;
00215   Teuchos::ParameterList GaleriList;
00216   GaleriList.set("n", nx * nx);
00217   GaleriList.set("nx", nx);
00218   GaleriList.set("ny", nx);
00219 
00220   Teuchos::RefCountPtr<Epetra_Map> Map = Teuchos::rcp( Galeri::CreateMap("Linear", Comm, GaleriList) );
00221   Teuchos::RefCountPtr<Epetra_RowMatrix> A = Teuchos::rcp( Galeri::CreateCrsMatrix("Laplace2D", &*Map, GaleriList) );
00222   Epetra_Vector LHS(*Map);
00223   Epetra_Vector RHS(*Map);
00224   Epetra_LinearProblem Problem(&*A, &LHS, &RHS);
00225 
00226   int TestPassed = true;
00227 
00228   // Jacobi as in AztecOO (no overlap)
00229   for (int ival = 1 ; ival < 10 ; ival += 3) {
00230     TestPassed = TestPassed && 
00231       CompareWithAztecOO(Problem,"Jacobi",0,ival);
00232   }
00233 
00234 #if 0
00235   // AztecOO with IC and overlap complains, also with
00236   // large fill-ins (in parallel)
00237   TestPassed = TestPassed && 
00238     CompareWithAztecOO(Problem,"IC no reord",0,0);
00239   TestPassed = TestPassed && 
00240     CompareWithAztecOO(Problem,"IC reord",0,0);
00241 
00242   vector<string> Tests;
00243   // now test solvers that accept overlap
00244   Tests.push_back("ILU no reord");
00245   Tests.push_back("ILU reord");
00246   // following requires --enable-aztecoo-azlu
00247 #ifdef HAVE_IFPACK_AMESOS
00248   //Tests.push_back("LU");
00249 #endif
00250 
00251   for (unsigned int i = 0 ; i < Tests.size() ; ++i) {
00252     for (int overlap = 0 ; overlap < 1 ; overlap += 2) {
00253       for (int ival = 0 ; ival < 10 ; ival += 4)
00254         TestPassed = TestPassed && 
00255           CompareWithAztecOO(Problem,Tests[i],overlap,ival);
00256     }
00257   }
00258 #endif
00259 
00260   if (!TestPassed) {
00261     cerr << "Test `CompareWithAztecOO.exe' FAILED!" << endl;
00262     exit(EXIT_FAILURE);
00263   }
00264 
00265 #ifdef HAVE_MPI
00266   MPI_Finalize() ; 
00267 #endif
00268   cout << "Test `CompareWithAztecOO.exe' passed!" << endl;
00269 
00270   exit(EXIT_SUCCESS);
00271 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 10:05:33 2011 for Ifpack Package Browser (Single Doxygen Collection) by  doxygen 1.6.3