Tpetra Matrix/Vector Services  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Tpetra_TsqrAdaptor.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Tpetra: Templated Linear Algebra Services Package
5 // Copyright (2008) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
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 Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 #ifndef __Tpetra_TsqrAdaptor_hpp
43 #define __Tpetra_TsqrAdaptor_hpp
44 
45 #include <Tpetra_ConfigDefs.hpp> // HAVE_TPETRA_TSQR, etc.
46 
47 #ifdef HAVE_TPETRA_TSQR
48 # include <Tsqr_NodeTsqrFactory.hpp> // create intranode TSQR object
49 # include <Tsqr.hpp> // full (internode + intranode) TSQR
50 # include <Tsqr_DistTsqr.hpp> // internode TSQR
51 // Subclass of TSQR::MessengerBase, implemented using Teuchos
52 // communicator template helper functions
53 # include <Tsqr_TeuchosMessenger.hpp>
54 # include <Tpetra_MultiVector.hpp>
55 # include <Teuchos_ParameterListAcceptorDefaultBase.hpp>
56 # include <stdexcept>
57 
58 
59 namespace Tpetra {
60 
83  template<class MV>
84  class TsqrAdaptor : public Teuchos::ParameterListAcceptorDefaultBase {
85  public:
86  typedef typename MV::scalar_type scalar_type;
87  typedef typename MV::local_ordinal_type ordinal_type;
88  typedef typename MV::node_type node_type;
89  typedef Teuchos::SerialDenseMatrix<ordinal_type, scalar_type> dense_matrix_type;
90  typedef typename Teuchos::ScalarTraits<scalar_type>::magnitudeType magnitude_type;
91 
92  private:
93  //typedef TSQR::MatView<ordinal_type, scalar_type> matview_type;
94  typedef TSQR::NodeTsqrFactory<node_type, scalar_type, ordinal_type> node_tsqr_factory_type;
95  typedef typename node_tsqr_factory_type::node_tsqr_type node_tsqr_type;
96  typedef TSQR::DistTsqr<ordinal_type, scalar_type> dist_tsqr_type;
97  typedef TSQR::Tsqr<ordinal_type, scalar_type, node_tsqr_type> tsqr_type;
98 
99  public:
106  TsqrAdaptor (const Teuchos::RCP<Teuchos::ParameterList>& plist) :
107  nodeTsqr_ (new node_tsqr_type),
108  distTsqr_ (new dist_tsqr_type),
109  tsqr_ (new tsqr_type (nodeTsqr_, distTsqr_)),
110  ready_ (false)
111  {
112  setParameterList (plist);
113  }
114 
116  TsqrAdaptor () :
117  nodeTsqr_ (new node_tsqr_type),
118  distTsqr_ (new dist_tsqr_type),
119  tsqr_ (new tsqr_type (nodeTsqr_, distTsqr_)),
120  ready_ (false)
121  {
122  setParameterList (Teuchos::null);
123  }
124 
125  Teuchos::RCP<const Teuchos::ParameterList>
126  getValidParameters () const
127  {
128  using Teuchos::RCP;
129  using Teuchos::rcp;
130  using Teuchos::ParameterList;
131  using Teuchos::parameterList;
132 
133  if (defaultParams_.is_null()) {
134  RCP<ParameterList> params = parameterList ("TSQR implementation");
135  params->set ("NodeTsqr", *(nodeTsqr_->getValidParameters ()));
136  params->set ("DistTsqr", *(distTsqr_->getValidParameters ()));
137  defaultParams_ = params;
138  }
139  return defaultParams_;
140  }
141 
142  void
143  setParameterList (const Teuchos::RCP<Teuchos::ParameterList>& plist)
144  {
145  using Teuchos::ParameterList;
146  using Teuchos::parameterList;
147  using Teuchos::RCP;
148  using Teuchos::sublist;
149 
150  RCP<ParameterList> params = plist.is_null() ?
151  parameterList (*getValidParameters ()) : plist;
152  nodeTsqr_->setParameterList (sublist (params, "NodeTsqr"));
153  distTsqr_->setParameterList (sublist (params, "DistTsqr"));
154 
155  this->setMyParamList (params);
156  }
157 
179  void
180  factorExplicit (MV& A,
181  MV& Q,
182  dense_matrix_type& R,
183  const bool forceNonnegativeDiagonal=false)
184  {
185  typedef Kokkos::MultiVector<scalar_type, node_type> KMV;
186 
187  prepareTsqr (Q); // Finish initializing TSQR.
188  KMV A_view = getNonConstView (A);
189  KMV Q_view = getNonConstView (Q);
190  tsqr_->factorExplicit (A_view, Q_view, R, false,
191  forceNonnegativeDiagonal);
192  }
193 
224  int
225  revealRank (MV& Q,
226  dense_matrix_type& R,
227  const magnitude_type& tol)
228  {
229  typedef Kokkos::MultiVector<scalar_type, node_type> KMV;
230 
231  prepareTsqr (Q); // Finish initializing TSQR.
232 
233  // FIXME (mfh 18 Oct 2010) Check Teuchos::Comm<int> object in Q
234  // to make sure it is the same communicator as the one we are
235  // using in our dist_tsqr_type implementation.
236  KMV Q_view = getNonConstView (Q);
237  return tsqr_->revealRank (Q_view, R, tol, false);
238  }
239 
240  private:
242  Teuchos::RCP<node_tsqr_type> nodeTsqr_;
243 
245  Teuchos::RCP<dist_tsqr_type> distTsqr_;
246 
248  Teuchos::RCP<tsqr_type> tsqr_;
249 
251  mutable Teuchos::RCP<const Teuchos::ParameterList> defaultParams_;
252 
254  bool ready_;
255 
276  void
277  prepareTsqr (const MV& mv)
278  {
279  if (! ready_) {
280  prepareDistTsqr (mv);
281  prepareNodeTsqr (mv);
282  ready_ = true;
283  }
284  }
285 
289  void
290  prepareNodeTsqr (const MV& mv)
291  {
292  node_tsqr_factory_type::prepareNodeTsqr (nodeTsqr_, mv.getMap()->getNode());
293  }
294 
301  void
302  prepareDistTsqr (const MV& mv)
303  {
304  using Teuchos::RCP;
305  using Teuchos::rcp_implicit_cast;
306  typedef TSQR::TeuchosMessenger<scalar_type> mess_type;
307  typedef TSQR::MessengerBase<scalar_type> base_mess_type;
308 
309  RCP<const Teuchos::Comm<int> > comm = mv.getMap()->getComm();
310  RCP<mess_type> mess (new mess_type (comm));
311  RCP<base_mess_type> messBase = rcp_implicit_cast<base_mess_type> (mess);
312  distTsqr_->init (messBase);
313  }
314 
327  static Kokkos::MultiVector<scalar_type, node_type>
328  getNonConstView (MV& A)
329  {
330  // FIXME (mfh 25 Oct 2010) We should be able to run TSQR even if
331  // storage of A uses nonconstant stride internally. We would
332  // have to copy and pack into a matrix with constant stride, and
333  // then unpack on exit. For now we choose just to raise an
334  // exception.
335  TEUCHOS_TEST_FOR_EXCEPTION(! A.isConstantStride(), std::invalid_argument,
336  "TSQR does not currently support Tpetra::MultiVector "
337  "inputs that do not have constant stride.");
338  return A.getLocalMVNonConst();
339  }
340  };
341 
342 } // namespace Tpetra
343 
344 #endif // HAVE_TPETRA_TSQR
345 
346 #endif // __Tpetra_TsqrAdaptor_hpp
347