/usr/include/trilinos/Amesos2_Solver_MP_Vector.hpp is in libtrilinos-stokhos-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 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 | // @HEADER
// ***********************************************************************
//
// Stokhos Package
// Copyright (2009) Sandia Corporation
//
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
// license for use of this work by or on behalf of the U.S. Government.
//
// 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 Eric T. Phipps (etphipp@sandia.gov).
//
// ***********************************************************************
// @HEADER
#ifndef AMESOS2_SOLVER_MP_VECTOR_HPP
#define AMESOS2_SOLVER_MP_VECTOR_HPP
#include "Amesos2_Solver.hpp"
#include "Amesos2_Factory.hpp"
#include "Stokhos_Sacado_Kokkos_MP_Vector.hpp"
#include "Stokhos_Tpetra_Utilities_MP_Vector.hpp"
namespace Amesos2 {
template <class S, class LO, class GO, class N>
LO get_mp_vector_size(
const Teuchos::RCP<const Tpetra::CrsMatrix<Sacado::MP::Vector<S>, LO, GO, N> >& A = Teuchos::null,
const Teuchos::RCP<Tpetra::MultiVector<Sacado::MP::Vector<S>, LO, GO, N> >& X = Teuchos::null,
const Teuchos::RCP<const Tpetra::MultiVector<Sacado::MP::Vector<S>, LO, GO, N> >& B = Teuchos::null)
{
// Without KokkosRefactor, can only do static
return S::static_size;
}
#if defined(TPETRA_HAVE_KOKKOS_REFACTOR)
template <class S, class LO, class GO, class D>
LO get_mp_vector_size(
const Teuchos::RCP<const Tpetra::CrsMatrix<Sacado::MP::Vector<S>, LO, GO, Kokkos::Compat::KokkosDeviceWrapperNode<D> > >& A = Teuchos::null,
const Teuchos::RCP<Tpetra::MultiVector<Sacado::MP::Vector<S>, LO, GO, Kokkos::Compat::KokkosDeviceWrapperNode<D> > >& X = Teuchos::null,
const Teuchos::RCP<const Tpetra::MultiVector<Sacado::MP::Vector<S>, LO, GO, Kokkos::Compat::KokkosDeviceWrapperNode<D> > >& B = Teuchos::null)
{
if (A != Teuchos::null) {
return Kokkos::dimension_scalar(A->getLocalValuesView());
}
else if (X != Teuchos::null) {
return Kokkos::dimension_scalar(X->template getLocalView<D>());
}
else if (B != Teuchos::null) {
return Kokkos::dimension_scalar(B->template getLocalView<D>());
}
return 0;
}
#endif
/// \brief Amesos2 solver adapter for MP::Vector scalar type
///
/// This adapter enables Amesos2 solvers to work with Tpetra matrices
/// and vectors of the Sacado::MP::Vector scalar type by "flattening"
/// these matrices and vectors into ones with a standard (e.g., double)
/// scalar type.
template <class Storage, class LocalOrdinal, class GlobalOrdinal, class Node,
template<class,class> class ConcreteSolver>
class MPVectorSolverAdapter :
public Solver< Tpetra::CrsMatrix<Sacado::MP::Vector<Storage>,
LocalOrdinal,
GlobalOrdinal,
Node>,
Tpetra::MultiVector<Sacado::MP::Vector<Storage>,
LocalOrdinal,
GlobalOrdinal,
Node>
>
{
public:
typedef Sacado::MP::Vector<Storage> Scalar;
typedef Tpetra::CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> Matrix;
typedef Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> Vector;
typedef typename Scalar::value_type BaseScalar;
typedef Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> Map;
typedef Tpetra::CrsGraph<LocalOrdinal,GlobalOrdinal,Node> FlatGraph;
typedef Tpetra::CrsMatrix<BaseScalar,LocalOrdinal,GlobalOrdinal,Node> FlatMatrix;
typedef Tpetra::MultiVector<BaseScalar,LocalOrdinal,GlobalOrdinal,Node> FlatVector;
typedef ConcreteSolver<FlatMatrix,FlatVector> FlatConcreteSolver;
typedef Solver<FlatMatrix,FlatVector> FlatSolver;
typedef Solver<Matrix,Vector> solver_type;
typedef typename solver_type::type type;
/// Constructor
MPVectorSolverAdapter(
const Teuchos::RCP<const Matrix>& A_,
const Teuchos::RCP<Vector>& X_,
const Teuchos::RCP<const Vector>& B_) :
A(A_), X(X_), B(B_) {
const LocalOrdinal mp_size = get_mp_vector_size(A, X, B);
flat_graph = Stokhos::create_flat_mp_graph(*(A->getCrsGraph()),
flat_X_map,
flat_B_map,
mp_size);
if (A != Teuchos::null)
flat_A = Stokhos::create_flat_matrix(*A, flat_graph, mp_size);
if (X != Teuchos::null)
flat_X = Stokhos::create_flat_vector_view(*X, flat_X_map);
if (B != Teuchos::null)
flat_B = Stokhos::create_flat_vector_view(*B, flat_B_map);
flat_solver =
create_solver_with_supported_type<ConcreteSolver,FlatMatrix,FlatVector>::apply(flat_A, flat_X, flat_B);
}
/// \name Mathematical Functions
//@{
/** \brief Pre-orders the matrix.
*
* Uses the default solver option, unless a solver-specific
* pre-ordering parameter is given.
*
* \sa setParameters
*/
virtual type& preOrdering( void ) {
flat_solver->preOrdering();
return *this;
}
/** \brief Performs symbolic factorization on the matrix
*
* \pre
* - The matrix A must not be \c null
*/
virtual type& symbolicFactorization( void ) {
flat_solver->symbolicFactorization();
return *this;
}
/** \brief Performs numeric factorization on the matrix
*
* numericFactorization checks first that symbolicFactorization
* has successfully been called, and if not, calls it before
* continuing.
*
* \pre
* - The matrix A must not be \c null
*
* \post
* - The factors L and U of A are computed
*/
virtual type& numericFactorization( void ) {
flat_solver->numericFactorization();
return *this;
}
/** \brief Solves \f$ A X = B\f$ (or \f$ A^T X = B\f$ )
*
* solve checks first that numericFactorization has successfully
* been called, and if not, calls it before continuing.
*
* \pre
* - The (multi)vectors \c X and \c B must not be \c null
*
* \post
* - The (multi)vector \c X (given at construction time) contains
* the solution to the system.
*/
virtual void solve( void ) {
flat_solver->solve();
}
/** \brief Solve \f$ A X = B\f$ using the given \c XX and \c BB
* (multi)vectors.
*
* This overload of solve uses the given \c XX and \c BB
* (multi)vectors when solving. These \c XX and \c BB
* (multi)vectors are used in place of any X and B that were given
* upon construction of the Amesos2 solver instance. \c XX and
* \c BB are used only for this solve.
*
* If a permanent change of X and B are required, see the setX()
* and setB() methods.
*
* \post
* - The (multi)vector \c XX contains the solution to the system
* - The (multi)vectors \c X and \c B given at construction time
* (if any) are unchanged.
*/
virtual void solve(const Teuchos::Ptr<Vector> XX,
const Teuchos::Ptr<const Vector> BB) const {
flat_solver->solve(
Stokhos::create_flat_vector_view(*XX, flat_X_map).get(),
Stokhos::create_flat_vector_view(*BB, flat_B_map).get() );
}
/** \brief Solve \f$ A X = B\f$ using the given \c XX and \c BB
* (multi)vectors.
*
* This overload of solve uses the given \c XX and \c BB
* (multi)vectors when solving. These \c XX and \c BB
* (multi)vectors are used in place of any X and B that were given
* upon construction of the Amesos2 solver instance. \c XX and
* \c BB are used only for this solve.
*
* If a permanent change of X and B are required, see the setX()
* and setB() methods.
*
* \post
* - The (multi)vector \c XX contains the solution to the system
* - The (multi)vectors \c X and \c B given at construction time
* (if any) are unchanged.
*/
virtual void solve(Vector* XX, const Vector* BB) const {
flat_solver->solve(
Stokhos::create_flat_vector_view(*XX, flat_X_map).get(),
Stokhos::create_flat_vector_view(*BB, flat_B_map).get() );
}
//@} End Mathematical Functions
/** \name Parameter Methods
* @{
*/
/** \brief Set/update internal variables and solver options.
*
* Expects that parameterList be named "Amesos2". That list may
* contain Amesos2-specific parameters. In addition, it may
* contain sublist for solver-specific parameters. These sublists
* should be named according to what is returned by the name()
* function (i.e. The solver's name when enabling for Amesos2
* during configuration).
*
* See each solver interface directly for a list of the supported
* parameters for that solver.
*/
virtual type& setParameters(
const Teuchos::RCP<Teuchos::ParameterList> & parameterList ) {
flat_solver->setParameters(parameterList);
return *this;
}
/**
* \brief Return a const parameter list of all of the valid parameters that
* this->setParameterList(...) will accept.
*/
virtual Teuchos::RCP<const Teuchos::ParameterList>
getValidParameters( void ) const {
return flat_solver->getValidParameters();
}
/// @} End Parameter Methods
/** \name Accessor Methods
* @{
*/
/** \brief Sets the matrix A of this solver
*
* \param [in] a An RCP to a matrix will will be used for
* future computation steps
*
* \param [in] keep_phase This parameter tells the solver what
* state it should keep. For example, you
* may want to replace the matrix but keep
* the symbolic factorization because you
* know the structure of the new matrix is
* the same as the structure of the old
* matrix. In this case you would pass
* Amesos2::SYMBFACT as this parameter.
*
* The default value for the second parameter is Amesos2::CLEAN,
* which means that the internal state of the solver will be
* completely reset. It will be as if no previous computational
* steps were performed.
*/
virtual void setA( const Teuchos::RCP<const Matrix> a,
EPhase keep_phase = CLEAN ) {
A = a;
// Rebuild flat matrix/graph
const LocalOrdinal mp_size = get_mp_vector_size(A);
if (keep_phase <= CLEAN) {
flat_X_map = Teuchos::null;
flat_B_map = Teuchos::null;
flat_graph = Teuchos::null;
flat_graph = Stokhos::create_flat_mp_graph(*(A->getCrsGraph()),
flat_X_map,
flat_B_map,
mp_size);
}
if (keep_phase <= SYMBFACT) // should this by NUMFACT???
flat_A = Stokhos::create_flat_matrix(*a, flat_graph, mp_size);
flat_solver->setA(flat_A, keep_phase);
}
/** \brief Sets the matrix A of this solver
*
* \param [in] a An raw C pointer to a matrix will will
* be used for future computation steps.
*
* \param [in] keep_phase This parameter tells the solver what
* state it should keep. For example, you
* may want to replace the matrix but keep
* the symbolic factorization because you
* know the structure of the new matrix is
* the same as the structure of the old
* matrix. In this case you would pass
* Amesos2::SYMBFACT as this parameter.
*
* The default value for the second parameter is Amesos2::CLEAN,
* which means that the internal state of the solver will be
* completely reset. It will be as if no previous computational
* steps were performed.
*/
virtual void setA( const Matrix* a, EPhase keep_phase = CLEAN ) {
this->setA(Teuchos::rcp(a,false), keep_phase);
}
/// Returns \c true if the solver can handle the matrix shape
virtual bool matrixShapeOK( void ) {
return flat_solver->matrixShapeOK();
}
/// Sets the LHS vector X
virtual void setX( const Teuchos::RCP<Vector> x ) {
X = x;
if (x != Teuchos::null)
flat_X = Stokhos::create_flat_vector_view(*x, flat_X_map);
else
flat_X = Teuchos::null;
flat_solver->setX(flat_X);
}
/// Sets the LHS vector X using a raw pointer
virtual void setX( Vector* x ) {
if (x != 0) {
X = Teuchos::rcp(x, false);
flat_X = Stokhos::create_flat_vector_view(*x, flat_X_map);
}
else {
X = Teuchos::null;
flat_X = Teuchos::null;
}
flat_solver->setX(flat_X);
}
/// Returns the vector that is the LHS of the linear system
virtual const Teuchos::RCP<Vector> getX( void ) {
return X;
}
/// Returns a raw pointer to the LHS of the linear system
virtual Vector* getXRaw( void ) {
return X.get();
}
/// Sets the RHS vector B
virtual void setB( const Teuchos::RCP<const Vector> b ) {
B = b;
if (b != Teuchos::null)
flat_B = Stokhos::create_flat_vector_view(*b, flat_B_map);
else
flat_B = Teuchos::null;
flat_solver->setB(flat_B);
}
/// Sets the RHS vector B using a raw pointer
virtual void setB( const Vector* b ) {
if (b != 0) {
B = Teuchos::rcp(b, false);
flat_B = Stokhos::create_flat_vector_view(*b, flat_B_map);
}
else {
B = Teuchos::null;
flat_B = Teuchos::null;
}
flat_solver->setB(flat_B);
}
/// Returns the vector that is the RHS of the linear system
virtual const Teuchos::RCP<const Vector> getB( void ) {
return B;
}
/// Returns a raw pointer to the RHS of the linear system
virtual const Vector* getBRaw( void ) {
return B.get();
}
/// Returns a pointer to the Teuchos::Comm communicator with this matrix
virtual Teuchos::RCP<const Teuchos::Comm<int> > getComm( void ) const {
return flat_solver->getComm();
}
/// Returns a reference to this solver's internal status object
virtual Status& getStatus() const {
return flat_solver->getStatus();
}
/// Return the name of this solver.
virtual std::string name( void ) const {
return flat_solver->name();
}
/// @} End Accessor Methods
/** \name Methods implementing Describable
* @{
*/
/// Returns a short description of this Solver
virtual std::string description( void ) const {
return flat_solver->description();
}
/// Prints the status information about the current solver with some level
/// of verbosity.
virtual void describe( Teuchos::FancyOStream &out,
const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default ) const {
flat_solver->describe(out, verbLevel);
}
/// @} End Methods implementing Describable
/** \name Performance and Timing
* @{
*/
/// Prints timing information about the current solver.
virtual void printTiming( Teuchos::FancyOStream &out,
const Teuchos::EVerbosityLevel verbLevel = Teuchos::Describable::verbLevel_default ) const{
flat_solver->printTiming(out, verbLevel);
}
/**
* \brief Extracts timing information from the current solver.
*
* Results are placed in the parameter list \c timingParameterList
*
* \param timingParameterList Accepts timing information from the
* current solver
*/
virtual void getTiming( Teuchos::ParameterList& timingParameterList ) const{
flat_solver->getTiming(timingParameterList);
}
/// @} End Performance and Timing
protected:
Teuchos::RCP<const Matrix> A;
Teuchos::RCP<Vector> X;
Teuchos::RCP<const Vector> B;
Teuchos::RCP<const Map> flat_X_map, flat_B_map;
Teuchos::RCP<const FlatGraph> flat_graph;
Teuchos::RCP<const FlatMatrix> flat_A;
Teuchos::RCP<FlatVector> flat_X;
Teuchos::RCP<const FlatVector> flat_B;
Teuchos::RCP<FlatSolver> flat_solver;
};
template < template <class,class> class ConcreteSolver,
class ST, class LO, class GO, class NO >
struct create_mp_vector_solver_impl {
typedef Sacado::MP::Vector<ST> SC;
typedef Tpetra::CrsMatrix<SC,LO,GO,NO> Matrix;
typedef Tpetra::MultiVector<SC,LO,GO,NO> Vector;
static Teuchos::RCP<Solver<Matrix,Vector> >
apply(Teuchos::RCP<const Matrix> A,
Teuchos::RCP<Vector> X,
Teuchos::RCP<const Vector> B ) {
ctassert<
Meta::is_same<
typename MatrixTraits<Matrix>::scalar_t,
typename MultiVecAdapter<Vector>::scalar_t
>::value
> same_scalar_assertion;
(void)same_scalar_assertion; // This stops the compiler from warning about unused declared variables
// If our assertion did not fail, then create and return a new solver
return Teuchos::rcp( new MPVectorSolverAdapter<ST,LO,GO,NO,ConcreteSolver>(A, X, B) );
}
};
// Specialization of create_solver_with_supported_type for
// Sacado::MP::Vector where we create MPVectorSolverAdapter wrapping
// each solver
template < template <class,class> class ConcreteSolver,
class ST, class LO, class GO, class NO >
struct create_solver_with_supported_type<
ConcreteSolver,
Tpetra::CrsMatrix<Sacado::MP::Vector<ST>,LO,GO,NO>,
Tpetra::MultiVector<Sacado::MP::Vector<ST>,LO,GO,NO> > :
public create_mp_vector_solver_impl<ConcreteSolver, ST, LO, GO, NO> {};
// Specialization for solver_supports_scalar for Sacado::MP::Vector<Storage>
// value == true if and only if
// solver_supprts_scalar<ConcreteSolver,Storage::value_type> == true
template <template <class,class> class ConcreteSolver,
typename Storage>
struct solver_supports_scalar<ConcreteSolver, Sacado::MP::Vector<Storage> > {
typedef Sacado::MP::Vector<Storage> Scalar;
typedef typename Scalar::value_type BaseScalar;
typedef typename solver_traits<ConcreteSolver>::supported_scalars supported_scalars;
static const bool value =
Meta::if_then_else<Meta::is_same<supported_scalars, Meta::nil_t>::value,
Meta::true_type,
Meta::type_list_contains<supported_scalars,
BaseScalar> >::type::value;
};
} // namespace Amesos2
#endif // AMESOS2_SOLVER_MP_VECTOR_HPP
|