Anasazi  Version of the Day
AnasaziSimpleLOBPCGSolMgr.hpp
Go to the documentation of this file.
1 
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Anasazi: Block Eigensolvers Package
6 // Copyright (2004) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // This library is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU Lesser General Public License as
13 // published by the Free Software Foundation; either version 2.1 of the
14 // License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful, but
17 // WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 // USA
25 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
26 //
27 // ***********************************************************************
28 // @HEADER
29 
30 #ifndef ANASAZI_SIMPLE_LOBPCG_SOLMGR_HPP
31 #define ANASAZI_SIMPLE_LOBPCG_SOLMGR_HPP
32 
38 #include "AnasaziConfigDefs.hpp"
39 #include "AnasaziTypes.hpp"
40 
41 #include "AnasaziEigenproblem.hpp"
42 #include "AnasaziSolverManager.hpp"
43 
44 #include "AnasaziLOBPCG.hpp"
45 #include "AnasaziBasicSort.hpp"
52 #include "AnasaziSolverUtils.hpp"
53 
54 #include "Teuchos_TimeMonitor.hpp"
55 
83 namespace Anasazi {
84 
85 template<class ScalarType, class MV, class OP>
86 class SimpleLOBPCGSolMgr : public SolverManager<ScalarType,MV,OP> {
87 
88  private:
91  typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
93 
94  public:
95 
97 
98 
111 
113  virtual ~SimpleLOBPCGSolMgr() {};
115 
117 
118 
120  return *problem_;
121  }
122 
123  int getNumIters() const {
124  return numIters_;
125  }
126 
128 
130 
131 
140  ReturnType solve();
142 
143  private:
145  std::string whch_;
146  MagnitudeType tol_;
147  int verb_;
148  int blockSize_;
149  int maxIters_;
150  int numIters_;
151 };
152 
153 
155 template<class ScalarType, class MV, class OP>
158  Teuchos::ParameterList &pl ) :
159  problem_(problem),
160  whch_("LM"),
161  tol_(1e-6),
162  verb_(Anasazi::Errors),
163  blockSize_(0),
164  maxIters_(100),
165  numIters_(0)
166 {
167  TEUCHOS_TEST_FOR_EXCEPTION(problem_ == Teuchos::null, std::invalid_argument, "Problem not given to solver manager.");
168  TEUCHOS_TEST_FOR_EXCEPTION(!problem_->isProblemSet(), std::invalid_argument, "Problem not set.");
169  TEUCHOS_TEST_FOR_EXCEPTION(!problem_->isHermitian(), std::invalid_argument, "Problem not symmetric.");
170  TEUCHOS_TEST_FOR_EXCEPTION(problem_->getInitVec() == Teuchos::null,std::invalid_argument, "Problem does not contain initial vectors to clone from.");
171 
172  whch_ = pl.get("Which","SR");
173  TEUCHOS_TEST_FOR_EXCEPTION(whch_ != "SM" && whch_ != "LM" && whch_ != "SR" && whch_ != "LR",
174  AnasaziError,
175  "SimpleLOBPCGSolMgr: \"Which\" parameter must be SM, LM, SR or LR.");
176 
177  tol_ = pl.get("Convergence Tolerance",tol_);
178  TEUCHOS_TEST_FOR_EXCEPTION(tol_ <= 0,
179  AnasaziError,
180  "SimpleLOBPCGSolMgr: \"Tolerance\" parameter must be strictly postiive.");
181 
182  // verbosity level
183  if (pl.isParameter("Verbosity")) {
184  if (Teuchos::isParameterType<int>(pl,"Verbosity")) {
185  verb_ = pl.get("Verbosity", verb_);
186  } else {
187  verb_ = (int)Teuchos::getParameter<Anasazi::MsgType>(pl,"Verbosity");
188  }
189  }
190 
191 
192  blockSize_= pl.get("Block Size",problem_->getNEV());
193  TEUCHOS_TEST_FOR_EXCEPTION(blockSize_ <= 0,
194  AnasaziError,
195  "SimpleLOBPCGSolMgr: \"Block Size\" parameter must be strictly positive.");
196 
197  maxIters_ = pl.get("Maximum Iterations",maxIters_);
198 }
199 
200 
201 
203 template<class ScalarType, class MV, class OP>
204 ReturnType
206 
207  // sort manager
209  // output manager
211  // status tests
213  if (maxIters_ > 0) {
214  max = Teuchos::rcp( new StatusTestMaxIters<ScalarType,MV,OP>(maxIters_) );
215  }
216  else {
217  max = Teuchos::null;
218  }
222  alltests.push_back(norm);
223  if (max != Teuchos::null) alltests.push_back(max);
227  ));
228  // printing StatusTest
230  = Teuchos::rcp( new StatusTestOutput<ScalarType,MV,OP>( printer,combo,1,Passed ) );
231  // orthomanager
233  = Teuchos::rcp( new SVQBOrthoManager<ScalarType,MV,OP>(problem_->getM()) );
234  // parameter list
236  plist.set("Block Size",blockSize_);
237  plist.set("Full Ortho",true);
238 
239  // create an LOBPCG solver
241  = Teuchos::rcp( new LOBPCG<ScalarType,MV,OP>(problem_,sorter,printer,outputtest,ortho,plist) );
242  // add the auxillary vecs from the eigenproblem to the solver
243  if (problem_->getAuxVecs() != Teuchos::null) {
244  lobpcg_solver->setAuxVecs( Teuchos::tuple<Teuchos::RCP<const MV> >(problem_->getAuxVecs()) );
245  }
246 
247  int numfound = 0;
248  int nev = problem_->getNEV();
251  while (numfound < nev) {
252  // reduce the strain on norm test, if we are almost done
253  if (nev - numfound < blockSize_) {
254  norm->setQuorum(nev-numfound);
255  }
256 
257  // tell the solver to iterate
258  try {
259  lobpcg_solver->iterate();
260  }
261  catch (const std::exception &e) {
262  // we are a simple solver manager. we don't catch exceptions. set solution empty, then rethrow.
263  printer->stream(Anasazi::Errors) << "Exception: " << e.what() << std::endl;
265  sol.numVecs = 0;
266  problem_->setSolution(sol);
267  throw;
268  }
269 
270  // check the status tests
271  if (norm->getStatus() == Passed) {
272 
273  int num = norm->howMany();
274  // if num < blockSize_, it is because we are on the last iteration: num+numfound>=nev
275  TEUCHOS_TEST_FOR_EXCEPTION(num < blockSize_ && num+numfound < nev,
276  std::logic_error,
277  "Anasazi::SimpleLOBPCGSolMgr::solve(): logic error.");
278  std::vector<int> ind = norm->whichVecs();
279  // just grab the ones that we need
280  if (num + numfound > nev) {
281  num = nev - numfound;
282  ind.resize(num);
283  }
284 
285  // copy the converged eigenvectors
286  Teuchos::RCP<MV> newvecs = MVT::CloneCopy(*lobpcg_solver->getRitzVectors(),ind);
287  // store them
288  foundvecs.push_back(newvecs);
289  // add them as auxiliary vectors
290  Teuchos::Array<Teuchos::RCP<const MV> > auxvecs = lobpcg_solver->getAuxVecs();
291  auxvecs.push_back(newvecs);
292  // setAuxVecs() will reset the solver to uninitialized, without messing with numIters()
293  lobpcg_solver->setAuxVecs(auxvecs);
294 
295  // copy the converged eigenvalues
296  Teuchos::RCP<std::vector<MagnitudeType> > newvals = Teuchos::rcp( new std::vector<MagnitudeType>(num) );
297  std::vector<Value<ScalarType> > all = lobpcg_solver->getRitzValues();
298  for (int i=0; i<num; i++) {
299  (*newvals)[i] = all[ind[i]].realpart;
300  }
301  foundvals.push_back(newvals);
302 
303  numfound += num;
304  }
305  else if (max != Teuchos::null && max->getStatus() == Passed) {
306 
307  int num = norm->howMany();
308  std::vector<int> ind = norm->whichVecs();
309 
310  if (num > 0) {
311  // copy the converged eigenvectors
312  Teuchos::RCP<MV> newvecs = MVT::CloneCopy(*lobpcg_solver->getRitzVectors(),ind);
313  // store them
314  foundvecs.push_back(newvecs);
315  // don't bother adding them as auxiliary vectors; we have reached maxiters and are going to quit
316 
317  // copy the converged eigenvalues
318  Teuchos::RCP<std::vector<MagnitudeType> > newvals = Teuchos::rcp( new std::vector<MagnitudeType>(num) );
319  std::vector<Value<ScalarType> > all = lobpcg_solver->getRitzValues();
320  for (int i=0; i<num; i++) {
321  (*newvals)[i] = all[ind[i]].realpart;
322  }
323  foundvals.push_back(newvals);
324 
325  numfound += num;
326  }
327  break; // while(numfound < nev)
328  }
329  else {
330  TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,"Anasazi::SimpleLOBPCGSolMgr::solve(): solver returned without satisfy status test.");
331  }
332  } // end of while(numfound < nev)
333 
334  TEUCHOS_TEST_FOR_EXCEPTION(foundvecs.size() != foundvals.size(),std::logic_error,"Anasazi::SimpleLOBPCGSolMgr::solve(): inconsistent array sizes");
335 
336  // create contiguous storage for all eigenvectors, eigenvalues
338  sol.numVecs = numfound;
339  if (numfound > 0) {
340  // allocate space for eigenvectors
341  sol.Evecs = MVT::Clone(*problem_->getInitVec(),numfound);
342  }
343  else {
344  sol.Evecs = Teuchos::null;
345  }
346  sol.Espace = sol.Evecs;
347  // allocate space for eigenvalues
348  std::vector<MagnitudeType> vals(numfound);
349  sol.Evals.resize(numfound);
350  // all real eigenvalues: set index vectors [0,...,numfound-1]
351  sol.index.resize(numfound,0);
352  // store eigenvectors, eigenvalues
353  int curttl = 0;
354  for (unsigned int i=0; i<foundvals.size(); i++) {
355  TEUCHOS_TEST_FOR_EXCEPTION((signed int)(foundvals[i]->size()) != MVT::GetNumberVecs(*foundvecs[i]), std::logic_error, "Anasazi::SimpleLOBPCGSolMgr::solve(): inconsistent sizes");
356  unsigned int lclnum = foundvals[i]->size();
357  std::vector<int> lclind(lclnum);
358  for (unsigned int j=0; j<lclnum; j++) lclind[j] = curttl+j;
359  // put the eigenvectors
360  MVT::SetBlock(*foundvecs[i],lclind,*sol.Evecs);
361  // put the eigenvalues
362  std::copy( foundvals[i]->begin(), foundvals[i]->end(), vals.begin()+curttl );
363 
364  curttl += lclnum;
365  }
366  TEUCHOS_TEST_FOR_EXCEPTION( curttl != sol.numVecs, std::logic_error, "Anasazi::SimpleLOBPCGSolMgr::solve(): inconsistent sizes");
367 
368  // sort the eigenvalues and permute the eigenvectors appropriately
369  if (numfound > 0) {
370  std::vector<int> order(sol.numVecs);
371  sorter->sort(vals,Teuchos::rcpFromRef(order),sol.numVecs);
372  // store the values in the Eigensolution
373  for (int i=0; i<sol.numVecs; i++) {
374  sol.Evals[i].realpart = vals[i];
375  sol.Evals[i].imagpart = MT::zero();
376  }
377  // now permute the eigenvectors according to order
379  msutils.permuteVectors(sol.numVecs,order,*sol.Evecs);
380  }
381 
382  // print final summary
383  lobpcg_solver->currentStatus(printer->stream(FinalSummary));
384 
385  // print timing information
386 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
387  if ( printer->isVerbosity( TimingDetails ) ) {
388  Teuchos::TimeMonitor::summarize( printer->stream( TimingDetails ) );
389  }
390 #endif
391 
392  // send the solution to the eigenproblem
393  problem_->setSolution(sol);
394  printer->stream(Debug) << "Returning " << sol.numVecs << " eigenpairs to eigenproblem." << std::endl;
395 
396  // get the number of iterations performed for this solve.
397  numIters_ = lobpcg_solver->getNumIters();
398 
399  // return from SolMgr::solve()
400  if (sol.numVecs < nev) return Unconverged;
401  return Converged;
402 }
403 
404 
405 
406 
407 } // end Anasazi namespace
408 
409 #endif /* ANASAZI_SIMPLE_LOBPCG_SOLMGR_HPP */
const Eigenproblem< ScalarType, MV, OP > & getProblem() const
Return the eigenvalue problem.
Pure virtual base class which describes the basic interface for a solver manager. ...
A special StatusTest for printing other status tests.
This class defines the interface required by an eigensolver and status test class to compute solution...
An implementation of the Anasazi::SortManager that performs a collection of common sorting techniques...
T & get(const std::string &name, T def_value)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
This class provides the Locally Optimal Block Preconditioned Conjugate Gradient (LOBPCG) iteration...
Status test for forming logical combinations of other status tests.
The Anasazi::SolverManager is a templated virtual base class that defines the basic interface that an...
Basic implementation of the Anasazi::SortManager class.
An implementation of the Anasazi::MatOrthoManager that performs orthogonalization using the SVQB iter...
An exception class parent to all Anasazi exceptions.
Implementation of the locally-optimal block preconditioned conjugate gradient (LOBPCG) method...
Namespace Anasazi contains the classes, structs, enums and utilities used by the Anasazi package...
Anasazi&#39;s templated, static class providing utilities for the solvers.
static void summarize(Ptr< const Comm< int > > comm, std::ostream &out=std::cout, const bool alwaysWriteLocal=false, const bool writeGlobalStats=true, const bool writeZeroTimers=true, const ECounterSetOp setOp=Intersection, const std::string &filter="")
bool isParameter(const std::string &name) const
int numVecs
The number of computed eigenpairs.
Basic output manager for sending information of select verbosity levels to the appropriate output str...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Anasazi&#39;s basic output manager for sending information of select verbosity levels to the appropriate ...
Abstract base class which defines the interface required by an eigensolver and status test class to c...
ReturnType
Enumerated type used to pass back information from a solver manager.
A status test for testing the norm of the eigenvectors residuals.
Traits class which defines basic operations on multivectors.
static Teuchos::RCP< MV > CloneCopy(const MV &mv)
Creates a new MV and copies contents of mv into the new vector (deep copy).
The Anasazi::SimpleLOBPCGSolMgr provides a simple solver manager over the LOBPCG eigensolver.
Anasazi header file which uses auto-configuration information to include necessary C++ headers...
Orthogonalization manager based on the SVQB technique described in "A Block Orthogonalization Procedu...
Struct for storing an eigenproblem solution.
ReturnType solve()
This method performs possibly repeated calls to the underlying eigensolver&#39;s iterate() routine until ...
static void SetBlock(const MV &A, const std::vector< int > &index, MV &mv)
Copy the vectors in A to a set of vectors in mv indicated by the indices given in index...
A status test for testing the number of iterations.
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
Status test for testing the number of iterations.
void push_back(const value_type &x)
static void permuteVectors(const int n, const std::vector< int > &perm, MV &Q, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > *resids=0)
Permute the vectors in a multivector according to the permutation vector perm, and optionally the res...
Special StatusTest for printing status tests.
Status test for forming logical combinations of other status tests.
size_type size() const
Types and exceptions used within Anasazi solvers and interfaces.
A status test for testing the norm of the eigenvectors residuals.
iterator begin()
int getNumIters() const
Get the iteration count for the most recent call to solve().
static Teuchos::RCP< MV > Clone(const MV &mv, const int numvecs)
Creates a new empty MV containing numvecs columns.
SimpleLOBPCGSolMgr(const Teuchos::RCP< Eigenproblem< ScalarType, MV, OP > > &problem, Teuchos::ParameterList &pl)
Basic constructor for SimpleLOBPCGSolMgr.
Class which provides internal utilities for the Anasazi solvers.