test_bl_cg_hb.cpp

Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //                 Belos: Block Linear Solvers Package
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 // This driver reads a problem from a Harwell-Boeing (HB) file.
00030 // The right-hand-side from the HB file is used instead of random vectors.
00031 // The initial guesses are all set to zero. 
00032 //
00033 // NOTE: No preconditioner is used in this case. 
00034 //
00035 #include "BelosConfigDefs.hpp"
00036 #include "BelosLinearProblem.hpp"
00037 #include "BelosOutputManager.hpp"
00038 #include "BelosStatusTestMaxIters.hpp"
00039 #include "BelosStatusTestOutputter.hpp"
00040 #include "BelosStatusTestResNorm.hpp"
00041 #include "BelosStatusTestCombo.hpp"
00042 #include "BelosEpetraAdapter.hpp"
00043 #include "BelosBlockCG.hpp"
00044 #include "createEpetraProblem.hpp"
00045 #include "Trilinos_Util.h"
00046 #include "Epetra_CrsMatrix.h"
00047 #include "Epetra_Map.h"
00048 #include "Teuchos_CommandLineProcessor.hpp"
00049 
00050 int main(int argc, char *argv[]) {
00051   //
00052 #ifdef EPETRA_MPI 
00053   // Initialize MPI 
00054   MPI_Init(&argc,&argv);  
00055   Belos::MPIFinalize mpiFinalize; // Will call finalize with *any* return
00056 #endif
00057   //
00058   using Teuchos::RefCountPtr;
00059   using Teuchos::rcp;
00060   //
00061   // Get test parameters from command-line processor
00062   //  
00063   bool verbose = false, proc_verbose = false;
00064   int frequency = -1;  // how often residuals are printed by solver
00065   int numrhs = 1;  // total number of right-hand sides to solve for
00066   int blockSize = 1;  // blocksize used by solver
00067   std::string filename("bcsstk14.hb");
00068   double tol = 1.0e-5;  // relative residual tolerance
00069 
00070   Teuchos::CommandLineProcessor cmdp(false,true);
00071   cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
00072   cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
00073   cmdp.setOption("tol",&tol,"Relative residual tolerance used by CG solver.");
00074   cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
00075   cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
00076   cmdp.setOption("block-size",&blockSize,"Block size to be used by CG solver.");
00077   if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
00078     return -1;
00079   }
00080   if (!verbose)
00081     frequency = -1;  // reset frequency if test is not verbose
00082   //
00083   // Get the problem
00084   //
00085   int MyPID;
00086   RefCountPtr<Epetra_CrsMatrix> A;
00087   RefCountPtr<Epetra_MultiVector> B, X;
00088   int return_val =Belos::createEpetraProblem(filename,NULL,&A,&B,&X,&MyPID);
00089   if(return_val != 0) return return_val;
00090   const Epetra_Map &Map = A->RowMap();
00091   proc_verbose = ( verbose && (MyPID==0) );
00092   //
00093   // Solve using Belos
00094   //
00095   typedef double                          ST;
00096   typedef Epetra_Operator                 OP;
00097   typedef Epetra_MultiVector              MV;
00098   typedef Belos::OperatorTraits<ST,MV,OP> OPT;
00099   typedef Belos::MultiVecTraits<ST,MV>    MVT;
00100   //
00101   // ********Other information used by block solver***********
00102   // *****************(can be user specified)******************
00103   //
00104   const int NumGlobalElements = B->GlobalLength();
00105   int maxits = NumGlobalElements/blockSize - 1; // maximum number of iterations to run
00106   //
00107   // *****Construct initial guess and random right-hand-sides *****
00108   //
00109   if (numrhs != 1) {
00110     X = rcp( new Epetra_MultiVector( Map, numrhs ) );
00111     MVT::MvRandom( *X );
00112     B = rcp( new Epetra_MultiVector( Map, numrhs ) );
00113     OPT::Apply( *A, *X, *B );
00114     MVT::MvInit( *X, 0.0 );
00115   }
00116   //
00117   //  Construct an unpreconditioned linear problem instance.
00118   //
00119   Belos::LinearProblem<ST,MV,OP> My_LP( A, X, B );
00120   My_LP.SetBlockSize( blockSize );
00121   //
00122   // *******************************************************************
00123   // *************Start the block CG iteration*************************
00124   // *******************************************************************
00125   //
00126   Belos::OutputManager<ST> My_OM( MyPID );
00127   if (verbose)
00128     My_OM.SetVerbosity( Belos::Errors + Belos::Warnings 
00129       + Belos::TimingDetails + Belos::FinalSummary );
00130   //
00131   Belos::StatusTestMaxIters<ST,MV,OP> test1( maxits );
00132   Belos::StatusTestResNorm<ST,MV,OP>  test2( tol );
00133   Belos::StatusTestOutputter<ST,MV,OP> test3( frequency, false );
00134   test3.set_resNormStatusTest( rcp(&test2,false) );
00135   test3.set_outputManager( rcp(&My_OM,false) );
00136   Belos::StatusTestCombo<ST,MV,OP> My_Test( Belos::StatusTestCombo<ST,MV,OP>::OR, test1, test3 );
00137   //
00138   Belos::BlockCG<ST,MV,OP>
00139     MyBlockCG(rcp(&My_LP,false), rcp(&My_Test,false), rcp(&My_OM,false) );
00140   //
00141   // **********Print out information about problem*******************
00142   //
00143   if (proc_verbose) {
00144     cout << endl << endl;
00145     cout << "Dimension of matrix: " << NumGlobalElements << endl;
00146     cout << "Number of right-hand sides: " << numrhs << endl;
00147     cout << "Block size used by solver: " << blockSize << endl;
00148     cout << "Max number of CG iterations: " << maxits << endl; 
00149     cout << "Relative residual tolerance: " << tol << endl;
00150     cout << endl;
00151   }
00152   //
00153   //
00154   if (proc_verbose) {
00155     cout << endl << endl;
00156     cout << "Running Block CG -- please wait" << endl;
00157     cout << (numrhs+blockSize-1)/blockSize 
00158    << " pass(es) through the solver required to solve for " << endl; 
00159     cout << numrhs << " right-hand side(s) -- using a block size of " << blockSize
00160    << endl << endl;
00161   }
00162 
00163   MyBlockCG.Solve();
00164   
00165   if (My_Test.GetStatus()!=Belos::Converged) {
00166     if (verbose)
00167       cout << "End Result: TEST FAILED" << endl;  
00168     return -1;
00169   }
00170   //
00171   // Default return value
00172   //
00173   if (proc_verbose)
00174     cout << "End Result: TEST PASSED" << endl;
00175   return 0;
00176   //
00177 } // end test_bl_cg_hb.cpp
00178 
00179 
00180 
00181 

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