Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends 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 // ************************************************************************
38 // @HEADER
39 
40 #ifndef EPETRA_TSQRADAPTOR_HPP
41 #define EPETRA_TSQRADAPTOR_HPP
42 
54 
55 #include "Tpetra_ConfigDefs.hpp"
56 
57 #if defined(TPETRA_ENABLE_DEPRECATED_CODE)
58 #if defined(TPETRA_DEPRECATED_DECLARATIONS)
59 #warning This file is deprecated due to Epetra removal and will be removed
60 #endif
61 #else
62 #error This file is deprecated due to Epetra removal and will be removed
63 #endif
64 
65 #if defined(TPETRA_ENABLE_DEPRECATED_CODE) && defined(HAVE_TPETRA_EPETRA) && defined(HAVE_TPETRA_TSQR)
66 
67 #include "Tsqr_NodeTsqrFactory.hpp" // create intranode TSQR object
68 #include "Tsqr.hpp" // full (internode + intranode) TSQR
69 #include "Tsqr_DistTsqr.hpp" // internode TSQR
70 #include "Epetra_Comm.h"
71 // Subclass of TSQR::MessengerBase, implemented using Teuchos
72 // communicator template helper functions
73 #include "Epetra_TsqrMessenger.hpp"
74 #include "Epetra_MultiVector.h"
75 #include "Teuchos_ParameterListAcceptorDefaultBase.hpp"
76 #include <stdexcept>
77 
78 namespace Epetra {
79 
104  TPETRA_DEPRECATED_MSG("epetra removal")
105  class TsqrAdaptor : public Teuchos::ParameterListAcceptorDefaultBase {
106  public:
107  typedef Epetra_MultiVector MV;
108 
115  typedef double scalar_type;
116 
123  typedef int ordinal_type;
124 
130  using device_type =
131  Kokkos::Device<Kokkos::DefaultHostExecutionSpace,
132  Kokkos::HostSpace>;
133 
142  using dense_matrix_type =
143  Teuchos::SerialDenseMatrix<ordinal_type, scalar_type>;
144 
150  using magnitude_type = double;
151 
152  private:
153  using matview_type = TSQR::MatView<ordinal_type, scalar_type>;
154  using node_tsqr_factory_type =
155  TSQR::NodeTsqrFactory<scalar_type, ordinal_type, device_type>;
156  // Don't need a "typename" here, because there are no template
157  // parameters involved in the type definition.
158  using node_tsqr_type = TSQR::NodeTsqr<ordinal_type, scalar_type>;
159  using dist_tsqr_type = TSQR::DistTsqr<ordinal_type, scalar_type>;
160  using tsqr_type = TSQR::Tsqr<ordinal_type, scalar_type>;
161 
162  public:
169  TsqrAdaptor (const Teuchos::RCP<Teuchos::ParameterList>& plist) :
170  nodeTsqr_ (node_tsqr_factory_type::getNodeTsqr ()),
171  distTsqr_ (new dist_tsqr_type),
172  tsqr_ (new tsqr_type (nodeTsqr_, distTsqr_)),
173  ready_ (false)
174  {
175  setParameterList (plist);
176  }
177 
179  TsqrAdaptor () :
180  nodeTsqr_ (node_tsqr_factory_type::getNodeTsqr ()),
181  distTsqr_ (new dist_tsqr_type),
182  tsqr_ (new tsqr_type (nodeTsqr_, distTsqr_)),
183  ready_ (false)
184  {
185  setParameterList (Teuchos::null);
186  }
187 
188  Teuchos::RCP<const Teuchos::ParameterList>
189  getValidParameters () const
190  {
191  using Teuchos::RCP;
192  using Teuchos::rcp;
193  using Teuchos::ParameterList;
194  using Teuchos::parameterList;
195 
196  if (defaultParams_.is_null()) {
197  RCP<ParameterList> params = parameterList ("TSQR implementation");
198  params->set ("NodeTsqr", *(nodeTsqr_->getValidParameters ()));
199  params->set ("DistTsqr", *(distTsqr_->getValidParameters ()));
200  defaultParams_ = params;
201  }
202  return defaultParams_;
203  }
204 
205  void
206  setParameterList (const Teuchos::RCP<Teuchos::ParameterList>& plist)
207  {
208  using Teuchos::ParameterList;
209  using Teuchos::parameterList;
210  using Teuchos::RCP;
211  using Teuchos::sublist;
212 
213  RCP<ParameterList> params = plist.is_null() ?
214  parameterList (*getValidParameters ()) : plist;
215  nodeTsqr_->setParameterList (sublist (params, "NodeTsqr"));
216  distTsqr_->setParameterList (sublist (params, "DistTsqr"));
217 
218  this->setMyParamList (params);
219  }
220 
242  void
243  factorExplicit (MV& A,
244  MV& Q,
245  dense_matrix_type& R,
246  const bool forceNonnegativeDiagonal=false)
247  {
248  prepareTsqr (Q); // Finish initializing TSQR.
249 
250  scalar_type* const A_ptr = A.Values ();
251  scalar_type* const Q_ptr = Q.Values ();
252  scalar_type* const R_ptr = R.values ();
253  const ordinal_type numRows = A.MyLength ();
254  const ordinal_type numCols = A.NumVectors ();
255  const ordinal_type lda = A.Stride ();
256  const ordinal_type ldq = Q.Stride ();
257  const ordinal_type ldr = R.stride ();
258 
259  const bool contiguousCacheBlocks = false;
260  tsqr_->factorExplicitRaw (numRows, numCols, A_ptr, lda,
261  Q_ptr, ldq, R_ptr, ldr,
262  contiguousCacheBlocks,
263  forceNonnegativeDiagonal);
264  }
265 
296  int
297  revealRank (MV& Q,
298  dense_matrix_type& R,
299  const magnitude_type& tol)
300  {
301  TEUCHOS_TEST_FOR_EXCEPTION
302  (! Q.ConstantStride (), std::invalid_argument, "TsqrAdaptor::"
303  "revealRank: Input MultiVector Q must have constant stride.");
304  prepareTsqr (Q); // Finish initializing TSQR.
305  // FIXME (mfh 25 Oct 2010) Check Epetra_Comm object in Q to make
306  // sure it is the same communicator as the one we are using in
307  // our dist_tsqr_type implementation.
308  return tsqr_->revealRankRaw (Q.MyLength (), Q.NumVectors (),
309  Q.Values (), Q.Stride (),
310  R.values (), R.stride (), tol, false);
311  }
312 
313  private:
315  Teuchos::RCP<node_tsqr_type> nodeTsqr_;
316 
318  Teuchos::RCP<dist_tsqr_type> distTsqr_;
319 
321  Teuchos::RCP<tsqr_type> tsqr_;
322 
324  mutable Teuchos::RCP<const Teuchos::ParameterList> defaultParams_;
325 
327  bool ready_;
328 
347  void
348  prepareTsqr (const MV& mv)
349  {
350  if (! ready_) {
351  prepareDistTsqr (mv);
352  ready_ = true;
353  }
354  }
355 
362  void
363  prepareDistTsqr (const MV& mv)
364  {
365  using Teuchos::RCP;
366  using Teuchos::rcp;
367  using TSQR::Epetra::makeTsqrMessenger;
368  typedef TSQR::MessengerBase<scalar_type> base_mess_type;
369 
370  // If mv falls out of scope, its Epetra_Comm may become invalid.
371  // Thus, we clone the input Epetra_Comm, so that the messenger
372  // owns the object.
373  RCP<const Epetra_Comm> comm = rcp (mv.Comm().Clone());
374  RCP<base_mess_type> messBase = makeTsqrMessenger<scalar_type> (comm);
375  distTsqr_->init (messBase);
376  }
377  };
378 
379 } // namespace Epetra
380 
381 #endif // defined(TPETRA_ENABLE_DEPRECATED_CODE) && defined(HAVE_TPETRA_EPETRA) && defined(HAVE_TPETRA_TSQR)
382 
383 #endif // EPETRA_TSQRADAPTOR_HPP
384 
Function for wrapping Epetra_Comm in a communicator wrapper that TSQR can use.