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 //
4 // Stratimikos: Thyra-based strategies for linear solvers
5 // Copyright (2006) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef __Thyra_TsqrAdaptor_hpp
43 #define __Thyra_TsqrAdaptor_hpp
44 
45 #include <BelosConfigDefs.hpp>
46 
47 // BelosThyraAdapter.hpp only includes this file if HAVE_BELOS_TSQR is
48 // defined. Thus, it's OK to include TSQR header files here.
49 
50 #include <Tsqr_NodeTsqrFactory.hpp> // create intranode TSQR object
51 #include <Tsqr.hpp> // full (internode + intranode) TSQR
52 #include <Tsqr_DistTsqr.hpp> // internode TSQR
53 // Subclass of TSQR::MessengerBase, implemented using Teuchos
54 // communicator template helper functions
55 #include <Tsqr_TeuchosMessenger.hpp>
56 #include <Kokkos_SerialNode.hpp>
57 
58 //#include <Thyra_DetachedMultiVectorView.hpp>
59 #include <Thyra_MultiVectorBase.hpp>
60 //#include <Thyra_MultiVectorStdOps.hpp>
61 #include <Thyra_SpmdVectorSpaceBase.hpp>
62 
63 #ifdef HAVE_MPI
65 #endif // HAVE_MPI
68 
69 #include <stdexcept>
70 
71 
72 namespace Thyra {
73 
95  template<class Scalar>
97  public:
98  typedef Thyra::MultiVectorBase<Scalar> MV;
99  typedef Scalar scalar_type;
100  typedef int ordinal_type; // MultiVectorBase really does use int for this
101  typedef Kokkos::SerialNode node_type; // FIXME (mfh 18 Jun 2013) Would be better to defer to the MV subclass
104 
105  private:
106  typedef TSQR::NodeTsqrFactory<node_type, scalar_type, ordinal_type> node_tsqr_factory_type;
107  typedef typename node_tsqr_factory_type::node_tsqr_type node_tsqr_type;
108  typedef TSQR::DistTsqr<ordinal_type, scalar_type> dist_tsqr_type;
109  typedef TSQR::Tsqr<ordinal_type, scalar_type, node_tsqr_type> tsqr_type;
110 
111  public:
122  ready_ (false)
123  {
124  throw std::logic_error ("Thyra adaptor for TSQR not implemented");
125  }
126 
132  ready_ (false)
133  {
134  throw std::logic_error ("Thyra adaptor for TSQR not implemented");
135  }
136 
139  {
140  using Teuchos::RCP;
141  using Teuchos::rcp;
143  using Teuchos::parameterList;
144 
145  if (defaultParams_.is_null()) {
146  RCP<ParameterList> params = parameterList ("TSQR implementation");
147  params->set ("NodeTsqr", *(nodeTsqr_->getValidParameters ()));
148  params->set ("DistTsqr", *(distTsqr_->getValidParameters ()));
149  defaultParams_ = params;
150  }
151  return defaultParams_;
152  }
153 
154  void
156  {
158  using Teuchos::parameterList;
159  using Teuchos::RCP;
160  using Teuchos::sublist;
161 
162  RCP<ParameterList> params = plist.is_null() ?
163  parameterList (*getValidParameters ()) : plist;
164  nodeTsqr_->setParameterList (sublist (params, "NodeTsqr"));
165  distTsqr_->setParameterList (sublist (params, "DistTsqr"));
166 
167  this->setMyParamList (params);
168  }
169 
191  void
193  MV& Q,
195  const bool forceNonnegativeDiagonal=false)
196  {
197  typedef Kokkos::MultiVector<scalar_type, node_type> KMV;
198 
199  prepareTsqr (Q); // Finish initializing TSQR.
200  KMV A_view = getNonConstView (A);
201  KMV Q_view = getNonConstView (Q);
202  tsqr_->factorExplicit (A_view, Q_view, R, false,
203  forceNonnegativeDiagonal);
204  }
205 
236  int
239  const magnitude_type& tol)
240  {
241  typedef Kokkos::MultiVector<scalar_type, node_type> KMV;
242 
243  prepareTsqr (Q); // Finish initializing TSQR.
244 
245  // FIXME (mfh 18 Oct 2010) Check Teuchos::Comm<int> object in Q
246  // to make sure it is the same communicator as the one we are
247  // using in our dist_tsqr_type implementation.
248  KMV Q_view = getNonConstView (Q);
249  return tsqr_->revealRank (Q_view, R, tol, false);
250  }
251 
252  private:
255 
258 
261 
264 
267 
269  bool ready_;
270 
282  getComm (const MV& X)
283  {
284  using Teuchos::RCP;
285  using Teuchos::rcp;
286  using Teuchos::rcp_dynamic_cast;
287  using Teuchos::rcp_implicit_cast;
288  typedef Thyra::VectorSpaceBase<Scalar> space_base_type;
289  typedef Thyra::SpmdVectorSpaceBase<Scalar> space_type;
290 
291  // Thyra stores the communicator in the "vector space," but only
292  // if that vector space is an SpmdVectorSpaceBase.
293  RCP<const space_base_type> rangeBase = X.range ();
294  TEUCHOS_TEST_FOR_EXCEPTION(rangeBase.is_null (), std::runtime_error, "X.range() is null.");
295  RCP<const space_type> range = rcp_dynamic_cast<const space_type> (rangeBase);
296  TEUCHOS_TEST_FOR_EXCEPTION(range.is_null (), std::runtime_error, "X.range() is not an SpmdVectorSpaceBase.");
297 
298  // Thyra annoyingly uses a (possibly) different template
299  // parameter for its Teuchos::Comm than everybody else. The
300  // least hackish way to work around this is to convert the Comm
301  // to one of two subclasses (MpiComm or SerialComm). If it's an
302  // MpiComm, we can extract the RCP<const OpaqueWrapper<MPI_Comm>
303  // > and make a new MpiComm<int> from it. If it's a SerialComm,
304  // just create a new SerialComm<int>. If it's neither of those,
305  // then I have no idea what to do. Note that MpiComm is only
306  // defined if HAVE_MPI is defined.
307  RCP<const Teuchos::Comm<Thyra::Ordinal> > thyraComm = range->getComm ();
308 #ifdef HAVE_MPI
310  rcp_dynamic_cast<const Teuchos::MpiComm<Thyra::Ordinal> > (thyraComm);
311  if (thyraMpiComm.is_null ()) {
313  rcp_dynamic_cast<const Teuchos::SerialComm<Thyra::Ordinal> > (thyraComm);
315  thyraSerialComm.is_null (), std::runtime_error,
316  "Thyra's communicator is neither an MpiComm nor a SerialComm. "
317  "Sorry, I have no idea what to do with it in that case.");
318  // It's a SerialComm. Make a SerialComm of our own.
319  // SerialComm instances are all the same, so there's no need
320  // to keep the original one.
321  return rcp_implicit_cast<const Teuchos::Comm<int> > (rcp (new Teuchos::SerialComm<int>));
322  }
323  else { // Yippie, we have an MpiComm.
324  RCP<const Teuchos::OpaqueWrapper<MPI_Comm> > rawMpiComm = thyraMpiComm->getRawMpiComm ();
325  // NOTE (mfh 18 Jun 2013) Since the error handler is attached
326  // to the MPI_Comm, not to the Teuchos widget, we don't have
327  // to set the error handler again on the new MpiComm object.
328  return rcp_implicit_cast<const Teuchos::Comm<int> > (rcp (new Teuchos::MpiComm<int> (rawMpiComm)));
329  }
330 #else // NOT HAVE_MPI
331  // Either it's a SerialComm or I don't know what to do with it.
333  rcp_dynamic_cast<const Teuchos::SerialComm<Thyra::Ordinal> > (thyraComm);
335  thyraSerialComm.is_null (), std::runtime_error,
336  "Thyra's communicator is not a SerialComm, and MPI is not enabled, so "
337  "it can't be an MpiComm either. That means it must be some other "
338  "subclass of Comm, about which I don't know. "
339  "Sorry, I have no idea what to do with it in that case.");
340  // It's a SerialComm. Make a SerialComm of our own.
341  // SerialComm instances are all the same, so there's no need
342  // to keep the original one.
343  return rcp_implicit_cast<const Teuchos::Comm<int> > (rcp (new Teuchos::SerialComm<int>));
344 #endif // HAVE_MPI
345  }
346 
350  void
351  prepareNodeTsqr (const MV& X)
352  {
353  (void) X; // silence compiler warning about unused argument
354 
355  if (node_.is_null ()) { // Create Kokkos Node instance on demand
356  node_ = Teuchos::rcp (new node_type);
357  }
358  node_tsqr_factory_type::prepareNodeTsqr (nodeTsqr_, node_);
359  }
360 
372  void
373  prepareDistTsqr (const MV& X)
374  {
375  using Teuchos::RCP;
376  using Teuchos::rcp_implicit_cast;
377  typedef TSQR::TeuchosMessenger<scalar_type> mess_type;
378  typedef TSQR::MessengerBase<scalar_type> base_mess_type;
379 
381  RCP<mess_type> mess (new mess_type (comm));
382  RCP<base_mess_type> messBase = rcp_implicit_cast<base_mess_type> (mess);
383  distTsqr_->init (messBase);
384  }
385 
405  void
406  prepareTsqr (const MV& X)
407  {
408  if (! ready_) {
409  prepareDistTsqr (X);
410  prepareNodeTsqr (X);
411  ready_ = true;
412  }
413  }
414 
427  Kokkos::MultiVector<scalar_type, node_type>
429  {
430  // TODO (mfh 18 Jun 2013) Check whether X is constant stride.
431  // TODO (mfh 18 Jun 2013) Extract a view of X's data.
432 
433  return Kokkos::MultiVector<scalar_type, node_type> (node_);
434 
435  // TODO (mfh 18 Jun 2013) Here is the start of code to extract a
436  // nonconstant view of X's data.
437 
438  // Kokkos::MultiVector<scalar_type, node_type> kmv (node);
439  // const size_t numRows = ???;
440  // const size_t numCols = ???;
441  // const size_t stride = ???;
442  // kmv.initializeValues (numRows, numCols, values, stride);
443  // return kmv;
444  }
445  };
446 
447 } // namespace Tpetra
448 
449 #endif // __Thyra_TsqrAdaptor_hpp
450 
Teuchos::ScalarTraits< scalar_type >::magnitudeType magnitude_type
int revealRank(MV &Q, dense_matrix_type &R, const magnitude_type &tol)
Rank-revealing decomposition.
void prepareNodeTsqr(const MV &X)
Finish intranode TSQR initialization.
Thyra::MultiVectorBase< Scalar > MV
TsqrAdaptor(const Teuchos::RCP< Teuchos::ParameterList > &plist)
Constructor (that accepts a parameter list).
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &plist)
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< node_tsqr_type > nodeTsqr_
The intranode TSQR implementation instance.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Teuchos::RCP< tsqr_type > tsqr_
The (full) TSQR implementation instance.
Kokkos::SerialNode node_type
TSQR::Tsqr< ordinal_type, scalar_type, node_tsqr_type > tsqr_type
static Teuchos::RCP< const Teuchos::Comm< int > > getComm(const MV &X)
Attempt to get a communicator out of the given multivector.
node_tsqr_factory_type::node_tsqr_type node_tsqr_type
void factorExplicit(MV &A, MV &Q, dense_matrix_type &R, const bool forceNonnegativeDiagonal=false)
Compute QR factorization [Q,R] = qr(A,0).
void prepareDistTsqr(const MV &X)
Finish internode TSQR initialization.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
bool is_null(const RCP< T > &p)
TSQR::NodeTsqrFactory< node_type, scalar_type, ordinal_type > node_tsqr_factory_type
bool ready_
Whether TSQR has been fully initialized.
void setMyParamList(const RCP< ParameterList > &paramList)
TsqrAdaptor()
Constructor (that uses default parameters).
Kokkos::MultiVector< scalar_type, node_type > getNonConstView(MV &X)
Extract a nonconstant view of X&#39;s data.
Teuchos::RCP< node_type > node_
Kokkos Node instance.
Teuchos::RCP< dist_tsqr_type > distTsqr_
The internode TSQR implementation instance.
void prepareTsqr(const MV &X)
Finish TSQR initialization.
Teuchos::RCP< const Teuchos::ParameterList > defaultParams_
Default parameter list. Initialized by getValidParameters().
TSQR::DistTsqr< ordinal_type, scalar_type > dist_tsqr_type