Tpetra Matrix/Vector Services Version of the Day
Tpetra_TsqrAdaptor.hpp
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //          Tpetra: Templated Linear Algebra Services Package
00005 //                 Copyright (2010) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #ifndef __Tpetra_TsqrAdaptor_hpp
00030 #define __Tpetra_TsqrAdaptor_hpp
00031 
00032 #include <Tpetra_ConfigDefs.hpp> // HAVE_TPETRA_TSQR, etc.
00033 
00034 #ifdef HAVE_TPETRA_TSQR
00035 #  include <Tsqr_NodeTsqrFactory.hpp> // create intranode TSQR object
00036 #  include <Tsqr.hpp> // full (internode + intranode) TSQR
00037 #  include <Tsqr_DistTsqr.hpp> // internode TSQR
00038 // Subclass of TSQR::MessengerBase, implemented using Teuchos
00039 // communicator template helper functions
00040 #  include <Tsqr_TeuchosMessenger.hpp> 
00041 #  include <Tpetra_MultiVector.hpp>
00042 
00043 #  include <stdexcept>
00044 
00047 
00048 namespace Tpetra {
00049 
00066   template< class MV >
00067   class TsqrAdaptor {
00068   public:
00069     typedef typename MV::scalar_type scalar_type;
00070     typedef typename MV::local_ordinal_type ordinal_type;
00071     typedef typename MV::node_type node_type;
00072     typedef Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > dense_matrix_type;
00073     typedef typename Teuchos::ScalarTraits< scalar_type >::magnitudeType magnitude_type;
00074 
00075   private:
00076     typedef TSQR::MatView< ordinal_type, scalar_type > matview_type;
00077     typedef TSQR::NodeTsqrFactory< node_type, scalar_type, ordinal_type > node_tsqr_factory_type;
00078     typedef typename node_tsqr_factory_type::node_tsqr_type node_tsqr_type;
00079     typedef TSQR::DistTsqr< ordinal_type, scalar_type > dist_tsqr_type;
00080     typedef TSQR::Tsqr< ordinal_type, scalar_type, node_tsqr_type > tsqr_type;
00081 
00082   public:
00090     static Teuchos::RCP<const Teuchos::ParameterList>
00091     getDefaultParameters ()
00092     {
00093       // For now, only the intranode part of TSQR accepts parameters.
00094       return node_tsqr_factory_type::getDefaultParameters ();
00095     }
00096 
00110     TsqrAdaptor (const MV& mv,
00111      const Teuchos::RCP<const Teuchos::ParameterList>& plist) :
00112       pTsqr_ (new tsqr_type (makeNodeTsqr (plist), makeDistTsqr (mv)))
00113     {}
00114 
00117     void
00118     factorExplicit (MV& A,
00119         MV& Q,
00120         dense_matrix_type& R)
00121     {
00122       typedef Kokkos::MultiVector< scalar_type, node_type > KMV;
00123 
00124       // FIXME (mfh 18 Oct 2010) Check Teuchos::Comm<int> objects in A
00125       // and Q to make sure they are the same communicator as the one
00126       // we are using in our dist_tsqr_type implementation.
00127       KMV A_view = getNonConstView (A);
00128       KMV Q_view = getNonConstView (Q);
00129       pTsqr_->factorExplicit (A_view, Q_view, R, false);
00130     }
00131 
00163     int
00164     revealRank (MV& Q,
00165     dense_matrix_type& R,
00166     const magnitude_type& tol)
00167     {
00168       typedef Kokkos::MultiVector< scalar_type, node_type > KMV;
00169 
00170       // FIXME (mfh 18 Oct 2010) Check Teuchos::Comm<int> object in Q
00171       // to make sure it is the same communicator as the one we are
00172       // using in our dist_tsqr_type implementation.
00173 
00174       KMV Q_view = getNonConstView (Q);
00175       return pTsqr_->revealRank (Q_view, R, tol, false);
00176     }
00177 
00178   private:
00181     Teuchos::RCP< tsqr_type > pTsqr_;
00182 
00186     static Kokkos::MultiVector< scalar_type, node_type >
00187     getNonConstView (MV& A)
00188     {
00189       if (! A.isConstantStride())
00190   {
00191     // FIXME (mfh 14 June 2010) Storage of A uses nonconstant
00192     // stride internally, but that doesn't necessarily mean we
00193     // can't run TSQR.  It depends on what get1dViewNonConst()
00194     // returns.  If it's copied and packed into a matrix with
00195     // constant stride, then we are free to run TSQR.
00196     std::ostringstream os;
00197     os << "TSQR does not currently support Tpetra::MultiVector "
00198       "inputs that do not have constant stride.";
00199     throw std::runtime_error (os.str());
00200   }
00201       return A.getLocalMVNonConst();
00202     }
00203 
00206     static RCP< dist_tsqr_type > 
00207     makeDistTsqr (const MV& mv)
00208     {
00209       using Teuchos::RCP;
00210       using Teuchos::rcp_implicit_cast;
00211       typedef TSQR::TeuchosMessenger< scalar_type > mess_type;
00212       typedef TSQR::MessengerBase< scalar_type > base_mess_type;
00213 
00214       RCP< const Teuchos::Comm<int> > pComm = mv.getMap()->getComm();
00215       RCP< mess_type > pMess (new mess_type (pComm));
00216       RCP< base_mess_type > pMessBase = rcp_implicit_cast< base_mess_type > (pMess);
00217       RCP< dist_tsqr_type > pDistTsqr (new dist_tsqr_type (pMessBase));
00218       return pDistTsqr;
00219     }
00220 
00223     static RCP< node_tsqr_type >
00224     makeNodeTsqr (const Teuchos::RCP<const Teuchos::ParameterList>& plist)
00225     {
00226       return node_tsqr_factory_type::makeNodeTsqr (plist);
00227     }
00228   };
00229 
00230 } // namespace Tpetra
00231 
00232 #endif // HAVE_TPETRA_TSQR
00233 
00234 #endif // __Tpetra_TsqrAdaptor_hpp
00235 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines