Tpetra Matrix/Vector Services  Version of the Day
Epetra_TsqrAdaptor.hpp
Go to the documentation of this file.
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 __Epetra_TsqrAdaptor_hpp
43 #define __Epetra_TsqrAdaptor_hpp
44 
58 
59 #include <Tpetra_ConfigDefs.hpp>
60 
61 #if defined(HAVE_TPETRA_EPETRA) && defined(HAVE_TPETRA_TSQR)
62 
63 #include <Kokkos_DefaultNode.hpp> // Include minimal Kokkos Node types
64 #include <Tsqr_NodeTsqrFactory.hpp> // create intranode TSQR object
65 #include <Tsqr.hpp> // full (internode + intranode) TSQR
66 #include <Tsqr_DistTsqr.hpp> // internode TSQR
67 #include <Epetra_Comm.h>
68 // Subclass of TSQR::MessengerBase, implemented using Teuchos
69 // communicator template helper functions
70 #include <Epetra_TsqrMessenger.hpp>
71 #include <Epetra_MultiVector.h>
72 #include <Teuchos_ParameterListAcceptorDefaultBase.hpp>
73 #include <stdexcept>
74 
75 
76 namespace Epetra {
77 
102  class TsqrAdaptor : public Teuchos::ParameterListAcceptorDefaultBase {
103  public:
104  typedef Epetra_MultiVector MV;
105 
112  typedef double scalar_type;
113 
120  typedef int ordinal_type;
121 
132  typedef Kokkos::SerialNode node_type;
133 
143 
149  typedef double magnitude_type;
150 
151  private:
152  typedef TSQR::MatView<ordinal_type, scalar_type> matview_type;
153  typedef TSQR::NodeTsqrFactory<node_type, scalar_type, ordinal_type> node_tsqr_factory_type;
154  // Don't need a "typename" here, because there are no template
155  // parameters involved in the type definition.
156  typedef node_tsqr_factory_type::node_tsqr_type node_tsqr_type;
157  typedef TSQR::DistTsqr<ordinal_type, scalar_type> dist_tsqr_type;
158  typedef TSQR::Tsqr<ordinal_type, scalar_type, node_tsqr_type> tsqr_type;
159 
160  public:
167  TsqrAdaptor (const Teuchos::RCP<Teuchos::ParameterList>& plist) :
168  nodeTsqr_ (new node_tsqr_type),
169  distTsqr_ (new dist_tsqr_type),
170  tsqr_ (new tsqr_type (nodeTsqr_, distTsqr_)),
171  ready_ (false)
172  {
173  setParameterList (plist);
174  }
175 
177  TsqrAdaptor () :
178  nodeTsqr_ (new node_tsqr_type),
179  distTsqr_ (new dist_tsqr_type),
180  tsqr_ (new tsqr_type (nodeTsqr_, distTsqr_)),
181  ready_ (false)
182  {
183  setParameterList (Teuchos::null);
184  }
185 
187  getValidParameters () const
188  {
189  using Teuchos::RCP;
190  using Teuchos::rcp;
192  using Teuchos::parameterList;
193 
194  if (defaultParams_.is_null()) {
195  RCP<ParameterList> params = parameterList ("TSQR implementation");
196  params->set ("NodeTsqr", *(nodeTsqr_->getValidParameters ()));
197  params->set ("DistTsqr", *(distTsqr_->getValidParameters ()));
198  defaultParams_ = params;
199  }
200  return defaultParams_;
201  }
202 
203  void
204  setParameterList (const Teuchos::RCP<Teuchos::ParameterList>& plist)
205  {
207  using Teuchos::parameterList;
208  using Teuchos::RCP;
209  using Teuchos::sublist;
210 
211  RCP<ParameterList> params = plist.is_null() ?
212  parameterList (*getValidParameters ()) : plist;
213  nodeTsqr_->setParameterList (sublist (params, "NodeTsqr"));
214  distTsqr_->setParameterList (sublist (params, "DistTsqr"));
215 
216  this->setMyParamList (params);
217  }
218 
240  void
241  factorExplicit (MV& A,
242  MV& Q,
243  dense_matrix_type& R,
244  const bool forceNonnegativeDiagonal=false)
245  {
246  typedef Kokkos::MultiVector<scalar_type, node_type> KMV;
247 
248  prepareTsqr (Q); // Finish initializing TSQR.
249  KMV A_view = getNonConstView (A);
250  KMV Q_view = getNonConstView (Q);
251  tsqr_->factorExplicit (A_view, Q_view, R, false,
252  forceNonnegativeDiagonal);
253  }
254 
285  int
286  revealRank (MV& Q,
287  dense_matrix_type& R,
288  const magnitude_type& tol)
289  {
290  typedef Kokkos::MultiVector<scalar_type, node_type> KMV;
291 
292  prepareTsqr (Q); // Finish initializing TSQR.
293 
294  // FIXME (mfh 25 Oct 2010) Check Epetra_Comm object in Q to make
295  // sure it is the same communicator as the one we are using in
296  // our dist_tsqr_type implementation.
297  KMV Q_view = getNonConstView (Q);
298  return tsqr_->revealRank (Q_view, R, tol, false);
299  }
300 
301  private:
304 
307 
310 
312  mutable Teuchos::RCP<const Teuchos::ParameterList> defaultParams_;
313 
315  bool ready_;
316 
335  void
336  prepareTsqr (const MV& mv)
337  {
338  if (! ready_) {
339  prepareDistTsqr (mv);
340  prepareNodeTsqr (mv);
341  ready_ = true;
342  }
343  }
344 
348  void
349  prepareNodeTsqr (const MV& mv)
350  {
351  (void) mv; // Epetra objects don't have a Kokkos Node.
352 
353  // Kokkos::SerialNode wants an empty ParameterList.
355  Teuchos::RCP<node_type> node (new node_type (plist));
356  node_tsqr_factory_type::prepareNodeTsqr (nodeTsqr_, node);
357  }
358 
365  void
366  prepareDistTsqr (const MV& mv)
367  {
368  using Teuchos::RCP;
369  using Teuchos::rcp;
370  using TSQR::Epetra::makeTsqrMessenger;
371  typedef TSQR::MessengerBase<scalar_type> base_mess_type;
372 
373  // If mv falls out of scope, its Epetra_Comm may become invalid.
374  // Thus, we clone the input Epetra_Comm, so that the messenger
375  // owns the object.
376  RCP<const Epetra_Comm> comm = rcp (mv.Comm().Clone());
377  RCP<base_mess_type> messBase = makeTsqrMessenger<scalar_type> (comm);
378  distTsqr_->init (messBase);
379  }
380 
393  static Kokkos::MultiVector<scalar_type, node_type>
394  getNonConstView (MV& A)
395  {
396  // FIXME (mfh 25 Oct 2010) We should be able to run TSQR even if
397  // storage of A uses nonconstant stride internally. We would
398  // have to copy and pack into a matrix with constant stride, and
399  // then unpack on exit. For now we choose just to raise an
400  // exception.
401  TEUCHOS_TEST_FOR_EXCEPTION(! A.ConstantStride(), std::invalid_argument,
402  "TSQR does not currently support Epetra_MultiVector "
403  "inputs that do not have constant stride.");
404  const int numRows = A.MyLength();
405  const int numCols = A.NumVectors();
406  const int stride = A.Stride();
407  // A_ptr does _not_ own the data. TSQR only operates within the
408  // scope of the multivector objects on which it operates, so it
409  // doesn't need ownership of the data.
410  Teuchos::ArrayRCP<double> A_ptr (A.Values(), 0, numRows*stride, false);
411 
412  typedef Kokkos::MultiVector<scalar_type, node_type> KMV;
413  // KMV objects want a Kokkos Node instance. Epetra objects
414  // don't have a Kokkos Node, so we make a temporary node just
415  // for the KMV.
416  //
417  // Kokkos::SerialNode wants an empty ParameterList.
419  Teuchos::RCP<node_type> node (new node_type (plist));
420  KMV A_kmv (node);
421  A_kmv.initializeValues (numRows, numCols, A_ptr, stride);
422  return A_kmv;
423  }
424  };
425 
426 } // namespace Epetra
427 
428 #endif // defined(HAVE_TPETRA_EPETRA) && defined(HAVE_TPETRA_TSQR)
429 
430 #endif // __Epetra_TsqrAdaptor_hpp
431 
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
bool is_null() const