Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Tpetra_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 __Tpetra_TsqrAdaptor_hpp
43 #define __Tpetra_TsqrAdaptor_hpp
44 
48 
49 #include "Tpetra_ConfigDefs.hpp"
50 
51 #ifdef HAVE_TPETRA_TSQR
52 # include "Tsqr_NodeTsqrFactory.hpp" // create intranode TSQR object
53 # include "Tsqr.hpp" // full (internode + intranode) TSQR
54 # include "Tsqr_DistTsqr.hpp" // internode TSQR
55 // Subclass of TSQR::MessengerBase, implemented using Teuchos
56 // communicator template helper functions
57 # include "Tsqr_TeuchosMessenger.hpp"
58 # include "Tpetra_MultiVector.hpp"
59 # include "Teuchos_ParameterListAcceptorDefaultBase.hpp"
60 # include <stdexcept>
61 
62 
63 namespace Tpetra {
64 
87  template<class MV>
88  class TsqrAdaptor : public Teuchos::ParameterListAcceptorDefaultBase {
89  public:
90  typedef typename MV::scalar_type scalar_type;
91  typedef typename MV::local_ordinal_type ordinal_type;
92  typedef typename MV::node_type node_type;
93  typedef Teuchos::SerialDenseMatrix<ordinal_type, scalar_type> dense_matrix_type;
94  typedef typename Teuchos::ScalarTraits<scalar_type>::magnitudeType magnitude_type;
95 
96  private:
97  //typedef TSQR::MatView<ordinal_type, scalar_type> matview_type;
98  typedef TSQR::NodeTsqrFactory<node_type, scalar_type, ordinal_type> node_tsqr_factory_type;
99  typedef typename node_tsqr_factory_type::node_tsqr_type node_tsqr_type;
100  typedef TSQR::DistTsqr<ordinal_type, scalar_type> dist_tsqr_type;
101  typedef TSQR::Tsqr<ordinal_type, scalar_type, node_tsqr_type> tsqr_type;
102 
103  public:
110  TsqrAdaptor (const Teuchos::RCP<Teuchos::ParameterList>& plist) :
111  nodeTsqr_ (new node_tsqr_type),
112  distTsqr_ (new dist_tsqr_type),
113  tsqr_ (new tsqr_type (nodeTsqr_, distTsqr_)),
114  ready_ (false)
115  {
116  setParameterList (plist);
117  }
118 
120  TsqrAdaptor () :
121  nodeTsqr_ (new node_tsqr_type),
122  distTsqr_ (new dist_tsqr_type),
123  tsqr_ (new tsqr_type (nodeTsqr_, distTsqr_)),
124  ready_ (false)
125  {
126  setParameterList (Teuchos::null);
127  }
128 
130  Teuchos::RCP<const Teuchos::ParameterList>
131  getValidParameters () const
132  {
133  using Teuchos::RCP;
134  using Teuchos::rcp;
135  using Teuchos::ParameterList;
136  using Teuchos::parameterList;
137 
138  if (defaultParams_.is_null()) {
139  RCP<ParameterList> params = parameterList ("TSQR implementation");
140  params->set ("NodeTsqr", *(nodeTsqr_->getValidParameters ()));
141  params->set ("DistTsqr", *(distTsqr_->getValidParameters ()));
142  defaultParams_ = params;
143  }
144  return defaultParams_;
145  }
146 
172  void
173  setParameterList (const Teuchos::RCP<Teuchos::ParameterList>& plist)
174  {
175  using Teuchos::ParameterList;
176  using Teuchos::parameterList;
177  using Teuchos::RCP;
178  using Teuchos::sublist;
179 
180  RCP<ParameterList> params = plist.is_null() ?
181  parameterList (*getValidParameters ()) : plist;
182  nodeTsqr_->setParameterList (sublist (params, "NodeTsqr"));
183  distTsqr_->setParameterList (sublist (params, "DistTsqr"));
184 
185  this->setMyParamList (params);
186  }
187 
209  void
210  factorExplicit (MV& A,
211  MV& Q,
212  dense_matrix_type& R,
213  const bool forceNonnegativeDiagonal=false)
214  {
215  TEUCHOS_TEST_FOR_EXCEPTION
216  (! A.isConstantStride (), std::invalid_argument, "TsqrAdaptor::"
217  "factorExplicit: Input MultiVector A must have constant stride.");
218  TEUCHOS_TEST_FOR_EXCEPTION
219  (! Q.isConstantStride (), std::invalid_argument, "TsqrAdaptor::"
220  "factorExplicit: Input MultiVector Q must have constant stride.");
221  prepareTsqr (Q); // Finish initializing TSQR.
222 
223  // FIXME (mfh 16 Jan 2016) Currently, TSQR is a host-only
224  // implementation.
225  A.sync_host ();
226  A.modify_host ();
227  Q.sync_host ();
228  Q.modify_host ();
229  auto A_view = A.template getLocalView<Kokkos::HostSpace> ();
230  auto Q_view = Q.template getLocalView<Kokkos::HostSpace> ();
231  scalar_type* const A_ptr =
232  reinterpret_cast<scalar_type*> (A_view.data ());
233  scalar_type* const Q_ptr =
234  reinterpret_cast<scalar_type*> (Q_view.data ());
235  const bool contiguousCacheBlocks = false;
236  tsqr_->factorExplicitRaw (A_view.extent (0),
237  A_view.extent (1),
238  A_ptr, A.getStride (),
239  Q_ptr, Q.getStride (),
240  R.values (), R.stride (),
241  contiguousCacheBlocks,
242  forceNonnegativeDiagonal);
243  }
244 
275  int
276  revealRank (MV& Q,
277  dense_matrix_type& R,
278  const magnitude_type& tol)
279  {
280  TEUCHOS_TEST_FOR_EXCEPTION
281  (! Q.isConstantStride (), std::invalid_argument, "TsqrAdaptor::"
282  "revealRank: Input MultiVector Q must have constant stride.");
283  prepareTsqr (Q); // Finish initializing TSQR.
284  // FIXME (mfh 18 Oct 2010) Check Teuchos::Comm<int> object in Q
285  // to make sure it is the same communicator as the one we are
286  // using in our dist_tsqr_type implementation.
287 
288  Q.sync_host ();
289  Q.modify_host ();
290  auto Q_view = Q.template getLocalView<Kokkos::HostSpace> ();
291  scalar_type* const Q_ptr =
292  reinterpret_cast<scalar_type*> (Q_view.data ());
293  const bool contiguousCacheBlocks = false;
294  return tsqr_->revealRankRaw (Q_view.extent (0),
295  Q_view.extent (1),
296  Q_ptr, Q.getStride (),
297  R.values (), R.stride (),
298  tol, contiguousCacheBlocks);
299  }
300 
301  private:
303  Teuchos::RCP<node_tsqr_type> nodeTsqr_;
304 
306  Teuchos::RCP<dist_tsqr_type> distTsqr_;
307 
309  Teuchos::RCP<tsqr_type> tsqr_;
310 
312  mutable Teuchos::RCP<const Teuchos::ParameterList> defaultParams_;
313 
315  bool ready_;
316 
337  void
338  prepareTsqr (const MV& mv)
339  {
340  if (! ready_) {
341  prepareDistTsqr (mv);
342  prepareNodeTsqr (mv);
343  ready_ = true;
344  }
345  }
346 
350  void
351  prepareNodeTsqr (const MV& mv)
352  {
353  node_tsqr_factory_type::prepareNodeTsqr (nodeTsqr_, mv.getMap()->getNode());
354  }
355 
362  void
363  prepareDistTsqr (const MV& mv)
364  {
365  using Teuchos::RCP;
366  using Teuchos::rcp_implicit_cast;
367  typedef TSQR::TeuchosMessenger<scalar_type> mess_type;
368  typedef TSQR::MessengerBase<scalar_type> base_mess_type;
369 
370  RCP<const Teuchos::Comm<int> > comm = mv.getMap()->getComm();
371  RCP<mess_type> mess (new mess_type (comm));
372  RCP<base_mess_type> messBase = rcp_implicit_cast<base_mess_type> (mess);
373  distTsqr_->init (messBase);
374  }
375  };
376 
377 } // namespace Tpetra
378 
379 #endif // HAVE_TPETRA_TSQR
380 
381 #endif // __Tpetra_TsqrAdaptor_hpp
382 
::Kokkos::Compat::KokkosDeviceWrapperNode< execution_space > node_type
Default value of Node template parameter.