Stratimikos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_TsqrAdaptor.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Stratimikos: Thyra-based strategies for linear solvers
4 //
5 // Copyright 2006 NTESS and the Stratimikos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef __Thyra_TsqrAdaptor_hpp
11 #define __Thyra_TsqrAdaptor_hpp
12 
13 #include "BelosConfigDefs.hpp"
14 
15 // BelosThyraAdapter.hpp only includes this file if HAVE_BELOS_TSQR is
16 // defined. Thus, it's OK to include TSQR header files here.
17 
18 #include "Thyra_MultiVectorBase.hpp"
19 #include "Thyra_SpmdVectorSpaceBase.hpp"
20 
21 #ifdef HAVE_MPI
23 #endif // HAVE_MPI
26 
27 #include <stdexcept>
28 
29 
30 namespace Thyra {
31 
53  template<class Scalar>
55  public:
56  typedef Thyra::MultiVectorBase<Scalar> MV;
57  typedef Scalar scalar_type;
58  typedef int ordinal_type; // MultiVectorBase really does use int for this
61 
69  {
70  throw std::logic_error ("Thyra adaptor for TSQR not implemented");
71  }
72 
75  {
76  throw std::logic_error ("Thyra adaptor for TSQR not implemented");
77  }
78 
81  {
82  throw std::logic_error ("Thyra adaptor for TSQR not implemented");
83  }
84 
85  void
87  {
88  throw std::logic_error ("Thyra adaptor for TSQR not implemented");
89  }
90 
112  void
113  factorExplicit (MV& /* A */,
114  MV& /* Q */,
115  dense_matrix_type& /* R */,
116  const bool /* forceNonnegativeDiagonal */ = false)
117  {
118  throw std::logic_error ("Thyra adaptor for TSQR not implemented");
119  }
120 
151  int
152  revealRank (MV& /* Q */,
153  dense_matrix_type& /* R */,
154  const magnitude_type& /* tol */)
155  {
156  throw std::logic_error ("Thyra adaptor for TSQR not implemented");
157  }
158 
159  private:
171  getComm (const MV& X)
172  {
173  using Teuchos::RCP;
174  using Teuchos::rcp;
175  using Teuchos::rcp_dynamic_cast;
176  using Teuchos::rcp_implicit_cast;
177  typedef Thyra::VectorSpaceBase<Scalar> space_base_type;
178  typedef Thyra::SpmdVectorSpaceBase<Scalar> space_type;
179 
180  // Thyra stores the communicator in the "vector space," but only
181  // if that vector space is an SpmdVectorSpaceBase.
182  RCP<const space_base_type> rangeBase = X.range ();
183  TEUCHOS_TEST_FOR_EXCEPTION(rangeBase.is_null (), std::runtime_error, "X.range() is null.");
184  RCP<const space_type> range = rcp_dynamic_cast<const space_type> (rangeBase);
185  TEUCHOS_TEST_FOR_EXCEPTION(range.is_null (), std::runtime_error, "X.range() is not an SpmdVectorSpaceBase.");
186 
187  // Thyra annoyingly uses a (possibly) different template
188  // parameter for its Teuchos::Comm than everybody else. The
189  // least hackish way to work around this is to convert the Comm
190  // to one of two subclasses (MpiComm or SerialComm). If it's an
191  // MpiComm, we can extract the RCP<const OpaqueWrapper<MPI_Comm>
192  // > and make a new MpiComm<int> from it. If it's a SerialComm,
193  // just create a new SerialComm<int>. If it's neither of those,
194  // then I have no idea what to do. Note that MpiComm is only
195  // defined if HAVE_MPI is defined.
196  RCP<const Teuchos::Comm<Thyra::Ordinal> > thyraComm = range->getComm ();
197 #ifdef HAVE_MPI
199  rcp_dynamic_cast<const Teuchos::MpiComm<Thyra::Ordinal> > (thyraComm);
200  if (thyraMpiComm.is_null ()) {
202  rcp_dynamic_cast<const Teuchos::SerialComm<Thyra::Ordinal> > (thyraComm);
204  thyraSerialComm.is_null (), std::runtime_error,
205  "Thyra's communicator is neither an MpiComm nor a SerialComm. "
206  "Sorry, I have no idea what to do with it in that case.");
207  // It's a SerialComm. Make a SerialComm of our own.
208  // SerialComm instances are all the same, so there's no need
209  // to keep the original one.
210  return rcp_implicit_cast<const Teuchos::Comm<int> > (rcp (new Teuchos::SerialComm<int>));
211  }
212  else { // Yippie, we have an MpiComm.
213  RCP<const Teuchos::OpaqueWrapper<MPI_Comm> > rawMpiComm = thyraMpiComm->getRawMpiComm ();
214  // NOTE (mfh 18 Jun 2013) Since the error handler is attached
215  // to the MPI_Comm, not to the Teuchos widget, we don't have
216  // to set the error handler again on the new MpiComm object.
217  return rcp_implicit_cast<const Teuchos::Comm<int> > (rcp (new Teuchos::MpiComm<int> (rawMpiComm)));
218  }
219 #else // NOT HAVE_MPI
220  // Either it's a SerialComm or I don't know what to do with it.
222  rcp_dynamic_cast<const Teuchos::SerialComm<Thyra::Ordinal> > (thyraComm);
224  thyraSerialComm.is_null (), std::runtime_error,
225  "Thyra's communicator is not a SerialComm, and MPI is not enabled, so "
226  "it can't be an MpiComm either. That means it must be some other "
227  "subclass of Comm, about which I don't know. "
228  "Sorry, I have no idea what to do with it in that case.");
229  // It's a SerialComm. Make a SerialComm of our own.
230  // SerialComm instances are all the same, so there's no need
231  // to keep the original one.
232  return rcp_implicit_cast<const Teuchos::Comm<int> > (rcp (new Teuchos::SerialComm<int>));
233 #endif // HAVE_MPI
234  }
235 
248  void
249  prepareDistTsqr (const MV& /* X */) {}
250 
270  void
271  prepareTsqr (const MV& /* X */) {}
272  };
273 
274 } // namespace Tpetra
275 
276 #endif // __Thyra_TsqrAdaptor_hpp
277 
Teuchos::ScalarTraits< scalar_type >::magnitudeType magnitude_type
Thyra::MultiVectorBase< Scalar > MV
void prepareDistTsqr(const MV &)
Finish interprocess TSQR initialization.
void factorExplicit(MV &, MV &, dense_matrix_type &, const bool=false)
Compute QR factorization [Q,R] = qr(A,0).
Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > dense_matrix_type
Stub adaptor from Thyra::MultiVectorBase to TSQR.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
static Teuchos::RCP< const Teuchos::Comm< int > > getComm(const MV &X)
Attempt to get a communicator out of the given multivector.
TsqrAdaptor(const Teuchos::RCP< Teuchos::ParameterList > &)
Constructor (that accepts a parameter list).
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
bool is_null(const RCP< T > &p)
int revealRank(MV &, dense_matrix_type &, const magnitude_type &)
Rank-revealing decomposition.
TsqrAdaptor()
Constructor (that uses default parameters).
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &)
void prepareTsqr(const MV &)
Finish TSQR initialization.