/usr/include/trilinos/Epetra_TsqrAdaptor.hpp is in libtrilinos-tpetra-dev 12.10.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 | // @HEADER
// ***********************************************************************
//
// Tpetra: Templated Linear Algebra Services Package
// Copyright (2008) Sandia Corporation
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
//
// ************************************************************************
// @HEADER
#ifndef __Epetra_TsqrAdaptor_hpp
#define __Epetra_TsqrAdaptor_hpp
///
/// \file Epetra_TsqrAdaptor.hpp
/// \brief Epetra_MultiVector to TSQR adaptor
///
/// \note (mfh 27 Oct 2010) This file is in Tpetra (rather than
/// Epetra, where it would seem to belong) as a temporary fix.
/// Otherwise, Epetra would need an optional package dependency on
/// Teuchos and Kokkos, which would break third-party code linking to
/// the Epetra library. Third-party code should use FIND_PACKAGE on
/// Trilinos to get the correct list of libraries against which to
/// link, but we make this easy temporary fix now so they have time to
/// fix their build systems later.
///
#include <Tpetra_ConfigDefs.hpp>
#if defined(HAVE_TPETRA_EPETRA) && defined(HAVE_TPETRA_TSQR)
#include <Kokkos_DefaultNode.hpp> // Include minimal Kokkos Node types
#include <Tsqr_NodeTsqrFactory.hpp> // create intranode TSQR object
#include <Tsqr.hpp> // full (internode + intranode) TSQR
#include <Tsqr_DistTsqr.hpp> // internode TSQR
#include <Epetra_Comm.h>
// Subclass of TSQR::MessengerBase, implemented using Teuchos
// communicator template helper functions
#include <Epetra_TsqrMessenger.hpp>
#include <Epetra_MultiVector.h>
#include <Teuchos_ParameterListAcceptorDefaultBase.hpp>
#include <stdexcept>
namespace Epetra {
/// \class TsqrAdaptor
/// \brief Adaptor from Epetra_MultiVector to TSQR.
/// \author Mark Hoemmen
///
/// TSQR (Tall Skinny QR factorization) is an orthogonalization
/// kernel that is as accurate as Householder QR, yet requires only
/// \f$2 \log P\f$ messages between $P$ MPI processes, independently
/// of the number of columns in the multivector.
///
/// TSQR works independently of the particular multivector
/// implementation, and interfaces to the latter via an adaptor
/// class. This class is the adaptor class for \c
/// Epetra_MultiVector. It templates on the MultiVector (MV) type
/// so that it can pick up that class' typedefs. In particular,
/// TSQR chooses its intranode implementation based on the Kokkos
/// Node type of the multivector.
///
/// \note Epetra objects live in the global namespace. TSQR
/// requires support for namespaces, so it's acceptable for us to
/// create an "Epetra" namespace to contain this adaptor.
///
/// \warning The current implementation of this adaptor requires
/// that all Epetra_MultiVector inputs use the same communicator
/// object (that is, the same Epetra_Comm) and map.
class TsqrAdaptor : public Teuchos::ParameterListAcceptorDefaultBase {
public:
typedef Epetra_MultiVector MV;
/// \typedef magnitude_type
///
/// Epetra_MultiVector's "Scalar" type is double; it is not a
/// templated object. TSQR supports Tpetra as well, in which the
/// "Scalar" type is a template parameter. In fact, TSQR supports
/// complex arithmetic (see the magnitude_type typedef).
typedef double scalar_type;
/// \typedef ordinal_type
///
/// In Tpetra terms, this would be the "LocalOrdinal" type. TSQR
/// does not depend on the "GlobalOrdinal" type. Epetra does not
/// distinguish between the LocalOrdinal and GlobalOrdinal types:
/// both are int.
typedef int ordinal_type;
/// \typedef node_type
///
/// TSQR depends on a Kokkos Node type. We just use the default
/// Node type here.
typedef Tpetra::Details::DefaultTypes::node_type node_type;
/// \typedef dense_matrix_type
///
/// How we pass around small dense matrices that are either local
/// to each MPI process, or globally replicated.
///
/// \note TSQR lives in the Kokkos package, which requires the
/// Teuchos package, so it's acceptable for us to require
/// Teuchos components.
typedef Teuchos::SerialDenseMatrix<ordinal_type, scalar_type> dense_matrix_type;
/// \typedef magnitude_type
///
/// Epetra_MultiVector's "Scalar" type is real. TSQR supports
/// complex arithmetic as well, in which magnitude_type would
/// differ from scalar_type.
typedef double magnitude_type;
private:
typedef TSQR::MatView<ordinal_type, scalar_type> matview_type;
typedef TSQR::NodeTsqrFactory<node_type, scalar_type, ordinal_type> node_tsqr_factory_type;
// Don't need a "typename" here, because there are no template
// parameters involved in the type definition.
typedef node_tsqr_factory_type::node_tsqr_type node_tsqr_type;
typedef TSQR::DistTsqr<ordinal_type, scalar_type> dist_tsqr_type;
typedef TSQR::Tsqr<ordinal_type, scalar_type, node_tsqr_type> tsqr_type;
public:
/// \brief Constructor (that accepts a parameter list).
///
/// \param plist [in/out] List of parameters for configuring TSQR.
/// The specific parameter keys that are read depend on the TSQR
/// implementation. For details, call \c getValidParameters()
/// and examine the documentation embedded therein.
TsqrAdaptor (const Teuchos::RCP<Teuchos::ParameterList>& plist) :
nodeTsqr_ (new node_tsqr_type),
distTsqr_ (new dist_tsqr_type),
tsqr_ (new tsqr_type (nodeTsqr_, distTsqr_)),
ready_ (false)
{
setParameterList (plist);
}
//! Constructor (that uses default parameters).
TsqrAdaptor () :
nodeTsqr_ (new node_tsqr_type),
distTsqr_ (new dist_tsqr_type),
tsqr_ (new tsqr_type (nodeTsqr_, distTsqr_)),
ready_ (false)
{
setParameterList (Teuchos::null);
}
Teuchos::RCP<const Teuchos::ParameterList>
getValidParameters () const
{
using Teuchos::RCP;
using Teuchos::rcp;
using Teuchos::ParameterList;
using Teuchos::parameterList;
if (defaultParams_.is_null()) {
RCP<ParameterList> params = parameterList ("TSQR implementation");
params->set ("NodeTsqr", *(nodeTsqr_->getValidParameters ()));
params->set ("DistTsqr", *(distTsqr_->getValidParameters ()));
defaultParams_ = params;
}
return defaultParams_;
}
void
setParameterList (const Teuchos::RCP<Teuchos::ParameterList>& plist)
{
using Teuchos::ParameterList;
using Teuchos::parameterList;
using Teuchos::RCP;
using Teuchos::sublist;
RCP<ParameterList> params = plist.is_null() ?
parameterList (*getValidParameters ()) : plist;
nodeTsqr_->setParameterList (sublist (params, "NodeTsqr"));
distTsqr_->setParameterList (sublist (params, "DistTsqr"));
this->setMyParamList (params);
}
/// \brief Compute QR factorization [Q,R] = qr(A,0).
///
/// \param A [in/out] On input: the multivector to factor.
/// Overwritten with garbage on output.
///
/// \param Q [out] On output: the (explicitly stored) Q factor in
/// the QR factorization of the (input) multivector A.
///
/// \param R [out] On output: the R factor in the QR factorization
/// of the (input) multivector A.
///
/// \param forceNonnegativeDiagonal [in] If true, then (if
/// necessary) do extra work (modifying both the Q and R
/// factors) in order to force the R factor to have a
/// nonnegative diagonal.
///
/// \warning Currently, this method only works if A and Q have the
/// same communicator and row distribution ("map," in Petra
/// terms) as those of the multivector given to this TsqrAdaptor
/// instance's constructor. Otherwise, the result of this
/// method is undefined.
void
factorExplicit (MV& A,
MV& Q,
dense_matrix_type& R,
const bool forceNonnegativeDiagonal=false)
{
prepareTsqr (Q); // Finish initializing TSQR.
scalar_type* const A_ptr = A.Values ();
scalar_type* const Q_ptr = Q.Values ();
scalar_type* const R_ptr = R.values ();
const ordinal_type numRows = A.MyLength ();
const ordinal_type numCols = A.NumVectors ();
const ordinal_type lda = A.Stride ();
const ordinal_type ldq = Q.Stride ();
const ordinal_type ldr = R.stride ();
const bool contiguousCacheBlocks = false;
tsqr_->factorExplicitRaw (numRows, numCols, A_ptr, lda,
Q_ptr, ldq, R_ptr, ldr,
contiguousCacheBlocks,
forceNonnegativeDiagonal);
}
/// \brief Rank-revealing decomposition
///
/// Using the R factor and explicit Q factor from
/// factorExplicit(), compute the singular value decomposition
/// (SVD) of R (\f$R = U \Sigma V^*\f$). If R is full rank (with
/// respect to the given relative tolerance tol), don't change Q
/// or R. Otherwise, compute \f$Q := Q \cdot U\f$ and \f$R :=
/// \Sigma V^*\f$ in place (the latter may be no longer upper
/// triangular).
///
/// \param Q [in/out] On input: explicit Q factor computed by
/// factorExplicit(). (Must be an orthogonal resp. unitary
/// matrix.) On output: If R is of full numerical rank with
/// respect to the tolerance tol, Q is unmodified. Otherwise, Q
/// is updated so that the first rank columns of Q are a basis
/// for the column space of A (the original matrix whose QR
/// factorization was computed by factorExplicit()). The
/// remaining columns of Q are a basis for the null space of A.
///
/// \param R [in/out] On input: ncols by ncols upper triangular
/// matrix with leading dimension ldr >= ncols. On output: if
/// input is full rank, R is unchanged on output. Otherwise, if
/// \f$R = U \Sigma V^*\f$ is the SVD of R, on output R is
/// overwritten with $\Sigma \cdot V^*$. This is also an ncols by
/// ncols matrix, but may not necessarily be upper triangular.
///
/// \param tol [in] Relative tolerance for computing the numerical
/// rank of the matrix R.
///
/// \return Rank \f$r\f$ of R: \f$ 0 \leq r \leq ncols\f$.
int
revealRank (MV& Q,
dense_matrix_type& R,
const magnitude_type& tol)
{
TEUCHOS_TEST_FOR_EXCEPTION
(! Q.ConstantStride (), std::invalid_argument, "TsqrAdaptor::"
"revealRank: Input MultiVector Q must have constant stride.");
prepareTsqr (Q); // Finish initializing TSQR.
// FIXME (mfh 25 Oct 2010) Check Epetra_Comm object in Q to make
// sure it is the same communicator as the one we are using in
// our dist_tsqr_type implementation.
return tsqr_->revealRankRaw (Q.MyLength (), Q.NumVectors (),
Q.Values (), Q.Stride (),
R.values (), R.stride (), tol, false);
}
private:
//! The intranode TSQR implementation instance.
Teuchos::RCP<node_tsqr_type> nodeTsqr_;
//! The internode TSQR implementation instance.
Teuchos::RCP<dist_tsqr_type> distTsqr_;
//! The (full) TSQR implementation instance.
Teuchos::RCP<tsqr_type> tsqr_;
//! Default parameter list. Initialized by \c getValidParameters().
mutable Teuchos::RCP<const Teuchos::ParameterList> defaultParams_;
//! Whether TSQR has been fully initialized.
bool ready_;
/// \brief Finish TSQR initialization.
///
/// The intranode and internode TSQR implementations both have a
/// two-stage initialization procedure: first, setting parameters
/// (which may happen at construction), and second, getting
/// information they need from the multivector input in order to
/// finish initialization. For intranode TSQR, this includes the
/// Kokkos Node instance; for internode TSQR, this includes the
/// communicator. The second stage of initialization happens in
/// this class' computational routines; all of those routines
/// accept one or more multivector inputs, which this method can
/// use for finishing initialization. Thus, users of this class
/// never need to see the two-stage initialization.
///
/// \param mv [in] Multivector object, used only to access the
/// underlying communicator object (in this case, Epetra_Comm).
/// All multivector objects used with this Adaptor instance must
/// have the same map and communicator.
void
prepareTsqr (const MV& mv)
{
if (! ready_) {
prepareDistTsqr (mv);
prepareNodeTsqr (mv);
ready_ = true;
}
}
/// \brief Finish intranode TSQR initialization.
///
/// \note It's OK to call this method more than once; it is idempotent.
void
prepareNodeTsqr (const MV& mv)
{
(void) mv; // Epetra objects don't have a Kokkos Node.
// Create Node with empty ParameterList.
Teuchos::ParameterList plist;
Teuchos::RCP<node_type> node (new node_type (plist));
node_tsqr_factory_type::prepareNodeTsqr (nodeTsqr_, node);
}
/// \brief Finish internode TSQR initialization.
///
/// \param mv [in] A multivector, from which to extract the
/// Epetra_Comm communicator wrapper to use to initialize TSQR.
///
/// \note It's OK to call this method more than once; it is idempotent.
void
prepareDistTsqr (const MV& mv)
{
using Teuchos::RCP;
using Teuchos::rcp;
using TSQR::Epetra::makeTsqrMessenger;
typedef TSQR::MessengerBase<scalar_type> base_mess_type;
// If mv falls out of scope, its Epetra_Comm may become invalid.
// Thus, we clone the input Epetra_Comm, so that the messenger
// owns the object.
RCP<const Epetra_Comm> comm = rcp (mv.Comm().Clone());
RCP<base_mess_type> messBase = makeTsqrMessenger<scalar_type> (comm);
distTsqr_->init (messBase);
}
};
} // namespace Epetra
#endif // defined(HAVE_TPETRA_EPETRA) && defined(HAVE_TPETRA_TSQR)
#endif // __Epetra_TsqrAdaptor_hpp
|