Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 
127 
136  typedef Teuchos::SerialDenseMatrix<ordinal_type, scalar_type> dense_matrix_type;
137 
143  typedef double magnitude_type;
144 
145  private:
146  typedef TSQR::MatView<ordinal_type, scalar_type> matview_type;
147  typedef TSQR::NodeTsqrFactory<node_type, scalar_type, ordinal_type> node_tsqr_factory_type;
148  // Don't need a "typename" here, because there are no template
149  // parameters involved in the type definition.
150  typedef node_tsqr_factory_type::node_tsqr_type node_tsqr_type;
151  typedef TSQR::DistTsqr<ordinal_type, scalar_type> dist_tsqr_type;
152  typedef TSQR::Tsqr<ordinal_type, scalar_type, node_tsqr_type> tsqr_type;
153 
154  public:
161  TsqrAdaptor (const Teuchos::RCP<Teuchos::ParameterList>& plist) :
162  nodeTsqr_ (new node_tsqr_type),
163  distTsqr_ (new dist_tsqr_type),
164  tsqr_ (new tsqr_type (nodeTsqr_, distTsqr_)),
165  ready_ (false)
166  {
167  setParameterList (plist);
168  }
169 
171  TsqrAdaptor () :
172  nodeTsqr_ (new node_tsqr_type),
173  distTsqr_ (new dist_tsqr_type),
174  tsqr_ (new tsqr_type (nodeTsqr_, distTsqr_)),
175  ready_ (false)
176  {
177  setParameterList (Teuchos::null);
178  }
179 
180  Teuchos::RCP<const Teuchos::ParameterList>
181  getValidParameters () const
182  {
183  using Teuchos::RCP;
184  using Teuchos::rcp;
185  using Teuchos::ParameterList;
186  using Teuchos::parameterList;
187 
188  if (defaultParams_.is_null()) {
189  RCP<ParameterList> params = parameterList ("TSQR implementation");
190  params->set ("NodeTsqr", *(nodeTsqr_->getValidParameters ()));
191  params->set ("DistTsqr", *(distTsqr_->getValidParameters ()));
192  defaultParams_ = params;
193  }
194  return defaultParams_;
195  }
196 
197  void
198  setParameterList (const Teuchos::RCP<Teuchos::ParameterList>& plist)
199  {
200  using Teuchos::ParameterList;
201  using Teuchos::parameterList;
202  using Teuchos::RCP;
203  using Teuchos::sublist;
204 
205  RCP<ParameterList> params = plist.is_null() ?
206  parameterList (*getValidParameters ()) : plist;
207  nodeTsqr_->setParameterList (sublist (params, "NodeTsqr"));
208  distTsqr_->setParameterList (sublist (params, "DistTsqr"));
209 
210  this->setMyParamList (params);
211  }
212 
234  void
235  factorExplicit (MV& A,
236  MV& Q,
237  dense_matrix_type& R,
238  const bool forceNonnegativeDiagonal=false)
239  {
240  prepareTsqr (Q); // Finish initializing TSQR.
241 
242  scalar_type* const A_ptr = A.Values ();
243  scalar_type* const Q_ptr = Q.Values ();
244  scalar_type* const R_ptr = R.values ();
245  const ordinal_type numRows = A.MyLength ();
246  const ordinal_type numCols = A.NumVectors ();
247  const ordinal_type lda = A.Stride ();
248  const ordinal_type ldq = Q.Stride ();
249  const ordinal_type ldr = R.stride ();
250 
251  const bool contiguousCacheBlocks = false;
252  tsqr_->factorExplicitRaw (numRows, numCols, A_ptr, lda,
253  Q_ptr, ldq, R_ptr, ldr,
254  contiguousCacheBlocks,
255  forceNonnegativeDiagonal);
256  }
257 
288  int
289  revealRank (MV& Q,
290  dense_matrix_type& R,
291  const magnitude_type& tol)
292  {
293  TEUCHOS_TEST_FOR_EXCEPTION
294  (! Q.ConstantStride (), std::invalid_argument, "TsqrAdaptor::"
295  "revealRank: Input MultiVector Q must have constant stride.");
296  prepareTsqr (Q); // Finish initializing TSQR.
297  // FIXME (mfh 25 Oct 2010) Check Epetra_Comm object in Q to make
298  // sure it is the same communicator as the one we are using in
299  // our dist_tsqr_type implementation.
300  return tsqr_->revealRankRaw (Q.MyLength (), Q.NumVectors (),
301  Q.Values (), Q.Stride (),
302  R.values (), R.stride (), tol, false);
303  }
304 
305  private:
307  Teuchos::RCP<node_tsqr_type> nodeTsqr_;
308 
310  Teuchos::RCP<dist_tsqr_type> distTsqr_;
311 
313  Teuchos::RCP<tsqr_type> tsqr_;
314 
316  mutable Teuchos::RCP<const Teuchos::ParameterList> defaultParams_;
317 
319  bool ready_;
320 
339  void
340  prepareTsqr (const MV& mv)
341  {
342  if (! ready_) {
343  prepareDistTsqr (mv);
344  prepareNodeTsqr (mv);
345  ready_ = true;
346  }
347  }
348 
352  void
353  prepareNodeTsqr (const MV& mv)
354  {
355  (void) mv; // Epetra objects don't have a Kokkos Node.
356 
357  // Create Node with empty ParameterList.
358  Teuchos::ParameterList plist;
359  Teuchos::RCP<node_type> node (new node_type (plist));
360  node_tsqr_factory_type::prepareNodeTsqr (nodeTsqr_, node);
361  }
362 
369  void
370  prepareDistTsqr (const MV& mv)
371  {
372  using Teuchos::RCP;
373  using Teuchos::rcp;
374  using TSQR::Epetra::makeTsqrMessenger;
375  typedef TSQR::MessengerBase<scalar_type> base_mess_type;
376 
377  // If mv falls out of scope, its Epetra_Comm may become invalid.
378  // Thus, we clone the input Epetra_Comm, so that the messenger
379  // owns the object.
380  RCP<const Epetra_Comm> comm = rcp (mv.Comm().Clone());
381  RCP<base_mess_type> messBase = makeTsqrMessenger<scalar_type> (comm);
382  distTsqr_->init (messBase);
383  }
384  };
385 
386 } // namespace Epetra
387 
388 #endif // defined(HAVE_TPETRA_EPETRA) && defined(HAVE_TPETRA_TSQR)
389 
390 #endif // __Epetra_TsqrAdaptor_hpp
391 
Function for wrapping Epetra_Comm in a communicator wrapper that TSQR can use.
::Kokkos::Compat::KokkosDeviceWrapperNode< execution_space > node_type
Default value of Node template parameter.