Kokkos Node API and Local Linear Algebra Kernels Version of the Day
Tsqr_DistTsqr.hpp
Go to the documentation of this file.
00001 //@HEADER
00002 // ************************************************************************
00003 // 
00004 //          Kokkos: Node API and Parallel Node Kernels
00005 //              Copyright (2009) 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 
00032 #ifndef __TSQR_Tsqr_DistTsqr_hpp
00033 #define __TSQR_Tsqr_DistTsqr_hpp
00034 
00035 #include <Tsqr_DistTsqrHelper.hpp>
00036 #include <Tsqr_DistTsqrRB.hpp>
00037 
00038 #include <utility> // std::pair
00039 
00040 
00041 namespace TSQR {
00042 
00058   template<class LocalOrdinal, class Scalar>
00059   class DistTsqr {
00060   public:
00061     typedef Scalar scalar_type;
00062     typedef LocalOrdinal ordinal_type;
00063     typedef MatView<ordinal_type, scalar_type > matview_type;
00064     typedef std::vector<std::vector<scalar_type> > VecVec;
00065     typedef std::pair<VecVec, VecVec> FactorOutput;
00066     typedef int rank_type;
00067 
00072     DistTsqr (const Teuchos::RCP<MessengerBase<scalar_type> >& messenger) :
00073       messenger_ (messenger),
00074       reduceBroadcastImpl_ (messenger)
00075     {}
00076 
00082     rank_type rank() const { return messenger_->rank(); }
00083 
00089     rank_type size() const { return messenger_->size(); }
00090 
00095     ~DistTsqr () {}
00096 
00105     bool QR_produces_R_factor_with_nonnegative_diagonal () const {
00106       typedef Combine<ordinal_type, scalar_type> combine_type;
00107       return combine_type::QR_produces_R_factor_with_nonnegative_diagonal() &&
00108   reduceBroadcastImpl_.QR_produces_R_factor_with_nonnegative_diagonal();
00109     }
00110 
00130     void
00131     factorExplicit (matview_type R_mine, matview_type Q_mine)
00132     {
00133       reduceBroadcastImpl_.factorExplicit (R_mine, Q_mine);
00134     }
00135 
00141     void 
00142     getFactorExplicitTimings (std::vector<TimeStats>& stats) const
00143     {
00144       reduceBroadcastImpl_.getStats (stats);
00145     }
00146 
00152     void
00153     getFactorExplicitTimingLabels (std::vector<std::string>& labels) const
00154     {
00155       reduceBroadcastImpl_.getStatsLabels (labels);
00156     }
00157 
00181     FactorOutput
00182     factor (matview_type R_mine)
00183     {
00184       VecVec Q_factors, tau_arrays;
00185       DistTsqrHelper< ordinal_type, scalar_type > helper;
00186       const ordinal_type ncols = R_mine.ncols();
00187 
00188       std::vector< scalar_type > R_local (ncols*ncols);
00189       copy_matrix (ncols, ncols, &R_local[0], ncols, R_mine.get(), R_mine.lda());
00190 
00191       const int P = messenger_->size();
00192       const int my_rank = messenger_->rank();
00193       const int first_tag = 0;
00194       std::vector<scalar_type> work (ncols);
00195       helper.factor_helper (ncols, R_local, my_rank, 0, P-1, first_tag, 
00196           messenger_.get(), Q_factors, tau_arrays, work);
00197       copy_matrix (ncols, ncols, R_mine.get(), R_mine.lda(), &R_local[0], ncols);
00198       return std::make_pair (Q_factors, tau_arrays);
00199     }
00200 
00202     void
00203     apply (const ApplyType& apply_type,
00204      const ordinal_type ncols_C,
00205      const ordinal_type ncols_Q,
00206      scalar_type C_mine[],
00207      const ordinal_type ldc_mine,
00208      const FactorOutput& factor_output)
00209     {
00210       const bool transposed = apply_type.transposed();
00211 
00212       if (transposed)
00213   throw std::logic_error("DistTsqr: Applying Q^T or Q^H "
00214              "not yet implemented");
00215 
00216       const int P = messenger_->size();
00217       const int my_rank = messenger_->rank();
00218       const int first_tag = 0;
00219       std::vector< scalar_type > C_other (ncols_C * ncols_C);
00220       std::vector< scalar_type > work (ncols_C);
00221   
00222       const VecVec& Q_factors = factor_output.first;
00223       const VecVec& tau_arrays = factor_output.second;
00224 
00225       // assert (Q_factors.size() == tau_arrays.size());
00226       const int cur_pos = Q_factors.size() - 1;
00227       DistTsqrHelper< ordinal_type, scalar_type > helper;
00228       helper.apply_helper (apply_type, ncols_C, ncols_Q, C_mine, ldc_mine, 
00229          &C_other[0], my_rank, 0, P-1, first_tag, 
00230          messenger_.get(), Q_factors, tau_arrays, cur_pos, 
00231          work);
00232     }
00233 
00235     void
00236     explicit_Q (const ordinal_type ncols_Q,
00237     scalar_type Q_mine[],
00238     const ordinal_type ldq_mine,
00239     const FactorOutput& factor_output)
00240     {
00241       const int my_rank = messenger_->rank ();
00242       fill_matrix (ncols_Q, ncols_Q, Q_mine, ldq_mine, Scalar(0));
00243       if (my_rank == 0)
00244   {
00245     for (ordinal_type j = 0; j < ncols_Q; ++j)
00246       Q_mine[j + j*ldq_mine] = Scalar (1);
00247   }
00248       apply (ApplyType::NoTranspose, ncols_Q, ncols_Q, 
00249        Q_mine, ldq_mine, factor_output);
00250     }
00251 
00252   private:
00253     Teuchos::RCP<MessengerBase<scalar_type> > messenger_;
00254     DistTsqrRB<ordinal_type, scalar_type> reduceBroadcastImpl_;
00255   };
00256 
00257 } // namespace TSQR
00258 
00259 #endif // __TSQR_Tsqr_DistTsqr_hpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends