Stratimikos Version of the Day
Loading...
Searching...
No Matches
Thyra_TsqrAdaptor.hpp
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
22# include "Teuchos_DefaultMpiComm.hpp"
23#endif // HAVE_MPI
24#include "Teuchos_DefaultSerialComm.hpp"
25#include "Teuchos_ParameterListAcceptorDefaultBase.hpp"
26
27#include <stdexcept>
28
29
30namespace Thyra {
31
53 template<class Scalar>
54 class TsqrAdaptor : public Teuchos::ParameterListAcceptorDefaultBase {
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
59 typedef Teuchos::SerialDenseMatrix<ordinal_type, scalar_type> dense_matrix_type;
60 typedef typename Teuchos::ScalarTraits<scalar_type>::magnitudeType magnitude_type;
61
68 TsqrAdaptor (const Teuchos::RCP<Teuchos::ParameterList>& /* plist */)
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
79 Teuchos::RCP<const Teuchos::ParameterList>
80 getValidParameters () const
81 {
82 throw std::logic_error ("Thyra adaptor for TSQR not implemented");
83 }
84
85 void
86 setParameterList (const Teuchos::RCP<Teuchos::ParameterList>& /* plist */)
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:
170 static Teuchos::RCP<const Teuchos::Comm<int> >
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
198 RCP<const Teuchos::MpiComm<Thyra::Ordinal> > thyraMpiComm =
199 rcp_dynamic_cast<const Teuchos::MpiComm<Thyra::Ordinal> > (thyraComm);
200 if (thyraMpiComm.is_null ()) {
201 RCP<const Teuchos::SerialComm<Thyra::Ordinal> > thyraSerialComm =
202 rcp_dynamic_cast<const Teuchos::SerialComm<Thyra::Ordinal> > (thyraComm);
203 TEUCHOS_TEST_FOR_EXCEPTION(
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.
221 RCP<const Teuchos::SerialComm<Thyra::Ordinal> > thyraSerialComm =
222 rcp_dynamic_cast<const Teuchos::SerialComm<Thyra::Ordinal> > (thyraComm);
223 TEUCHOS_TEST_FOR_EXCEPTION(
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
Stub adaptor from Thyra::MultiVectorBase to TSQR.
int revealRank(MV &, dense_matrix_type &, const magnitude_type &)
Rank-revealing decomposition.
TsqrAdaptor()
Constructor (that uses default parameters).
TsqrAdaptor(const Teuchos::RCP< Teuchos::ParameterList > &)
Constructor (that accepts a parameter list).
void factorExplicit(MV &, MV &, dense_matrix_type &, const bool=false)
Compute QR factorization [Q,R] = qr(A,0).

Generated for Stratimikos by doxygen 1.9.8