Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
Amesos2_Solver_MP_Vector.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Stokhos Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
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 Eric T. Phipps (etphipp@sandia.gov).
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef AMESOS2_SOLVER_MP_VECTOR_HPP
43 #define AMESOS2_SOLVER_MP_VECTOR_HPP
44 
45 #include "Amesos2_Solver.hpp"
46 #include "Amesos2_Factory.hpp"
49 
50 namespace Amesos2 {
51 
52  template <class S, class LO, class GO, class N>
54  const Teuchos::RCP<const Tpetra::CrsMatrix<Sacado::MP::Vector<S>, LO, GO, N> >& A = Teuchos::null,
55  const Teuchos::RCP<Tpetra::MultiVector<Sacado::MP::Vector<S>, LO, GO, N> >& X = Teuchos::null,
56  const Teuchos::RCP<const Tpetra::MultiVector<Sacado::MP::Vector<S>, LO, GO, N> >& B = Teuchos::null)
57  {
58  // Without KokkosRefactor, can only do static
59  return S::static_size;
60  }
61 
62  template <class S, class LO, class GO, class D>
64  const Teuchos::RCP<const Tpetra::CrsMatrix<Sacado::MP::Vector<S>, LO, GO, Kokkos::Compat::KokkosDeviceWrapperNode<D> > >& A = Teuchos::null,
65  const Teuchos::RCP<Tpetra::MultiVector<Sacado::MP::Vector<S>, LO, GO, Kokkos::Compat::KokkosDeviceWrapperNode<D> > >& X = Teuchos::null,
66  const Teuchos::RCP<const Tpetra::MultiVector<Sacado::MP::Vector<S>, LO, GO, Kokkos::Compat::KokkosDeviceWrapperNode<D> > >& B = Teuchos::null)
67  {
68  if (A != Teuchos::null) {
69  return Kokkos::dimension_scalar(A->getLocalValuesDevice(Tpetra::Access::ReadOnly));
70  }
71  else if (X != Teuchos::null) {
72  return Kokkos::dimension_scalar(X->getLocalViewDevice(Tpetra::Access::ReadOnly));
73  }
74  else if (B != Teuchos::null) {
75  return Kokkos::dimension_scalar(B->getLocalViewDevice(Tpetra::Access::ReadOnly));
76  }
77  return 0;
78  }
79 
86  template <class Storage, class LocalOrdinal, class GlobalOrdinal, class Node,
87  template<class,class> class ConcreteSolver>
89  public Solver< Tpetra::CrsMatrix<Sacado::MP::Vector<Storage>,
90  LocalOrdinal,
91  GlobalOrdinal,
92  Node>,
93  Tpetra::MultiVector<Sacado::MP::Vector<Storage>,
94  LocalOrdinal,
95  GlobalOrdinal,
96  Node>
97  >
98  {
99  public:
100 
102  typedef Tpetra::CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> Matrix;
103  typedef Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> Vector;
104 
105  typedef typename Scalar::value_type BaseScalar;
106  typedef Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> Map;
107  typedef Tpetra::CrsGraph<LocalOrdinal,GlobalOrdinal,Node> FlatGraph;
108  typedef Tpetra::CrsMatrix<BaseScalar,LocalOrdinal,GlobalOrdinal,Node> FlatMatrix;
109  typedef Tpetra::MultiVector<BaseScalar,LocalOrdinal,GlobalOrdinal,Node> FlatVector;
110  typedef ConcreteSolver<FlatMatrix,FlatVector> FlatConcreteSolver;
111  typedef Solver<FlatMatrix,FlatVector> FlatSolver;
112 
113  typedef Solver<Matrix,Vector> solver_type;
114  typedef typename solver_type::type type;
115 
118  const Teuchos::RCP<const Matrix>& A_,
119  const Teuchos::RCP<Vector>& X_,
120  const Teuchos::RCP<const Vector>& B_) :
121  A(A_), X(X_), B(B_) {
122  const LocalOrdinal mp_size = get_mp_vector_size(A, X, B);
123  flat_graph = Stokhos::create_flat_mp_graph(*(A->getCrsGraph()),
124  flat_X_map,
125  flat_B_map,
126  mp_size);
127  if (A != Teuchos::null)
129  if (X != Teuchos::null)
131  if (B != Teuchos::null)
133  flat_solver =
134  create_solver_with_supported_type<ConcreteSolver,FlatMatrix,FlatVector>::apply(flat_A, flat_X, flat_B);
135  }
136 
138 
139 
147  virtual type& preOrdering( void ) {
148  flat_solver->preOrdering();
149  return *this;
150  }
151 
152 
158  virtual type& symbolicFactorization( void ) {
159  flat_solver->symbolicFactorization();
160  return *this;
161  }
162 
163 
176  virtual type& numericFactorization( void ) {
177  flat_solver->numericFactorization();
178  return *this;
179  }
180 
181 
194  virtual void solve( void ) {
195  flat_solver->solve();
196  }
197 
198 
216  virtual void solve(const Teuchos::Ptr<Vector> XX,
217  const Teuchos::Ptr<const Vector> BB) const {
218  flat_solver->solve(
221  }
222 
223 
241  virtual void solve(Vector* XX, const Vector* BB) const {
242  flat_solver->solve(
245  }
246 
248 
249 
266  virtual type& setParameters(
267  const Teuchos::RCP<Teuchos::ParameterList> & parameterList ) {
268  flat_solver->setParameters(parameterList);
269  return *this;
270  }
271 
272 
277  virtual Teuchos::RCP<const Teuchos::ParameterList>
278  getValidParameters( void ) const {
279  return flat_solver->getValidParameters();
280  }
281 
283 
284 
308  virtual void setA( const Teuchos::RCP<const Matrix> a,
309  EPhase keep_phase = CLEAN ) {
310  A = a;
311 
312  // Rebuild flat matrix/graph
313  const LocalOrdinal mp_size = get_mp_vector_size(A);
314  if (keep_phase <= CLEAN) {
315  flat_X_map = Teuchos::null;
316  flat_B_map = Teuchos::null;
317  flat_graph = Teuchos::null;
318  flat_graph = Stokhos::create_flat_mp_graph(*(A->getCrsGraph()),
319  flat_X_map,
320  flat_B_map,
321  mp_size);
322  }
323  if (keep_phase <= SYMBFACT) // should this by NUMFACT???
325 
326  flat_solver->setA(flat_A, keep_phase);
327  }
328 
348  virtual void setA( const Matrix* a, EPhase keep_phase = CLEAN ) {
349  this->setA(Teuchos::rcp(a,false), keep_phase);
350  }
351 
352 
354  virtual bool matrixShapeOK( void ) {
355  return flat_solver->matrixShapeOK();
356  }
357 
358 
360  virtual void setX( const Teuchos::RCP<Vector> x ) {
361  X = x;
362  if (x != Teuchos::null)
364  else
365  flat_X = Teuchos::null;
366  flat_solver->setX(flat_X);
367  }
368 
369 
371  virtual void setX( Vector* x ) {
372  if (x != 0) {
373  X = Teuchos::rcp(x, false);
375  }
376  else {
377  X = Teuchos::null;
378  flat_X = Teuchos::null;
379  }
380  flat_solver->setX(flat_X);
381  }
382 
383 
385  virtual const Teuchos::RCP<Vector> getX( void ) {
386  return X;
387  }
388 
389 
391  virtual Vector* getXRaw( void ) {
392  return X.get();
393  }
394 
395 
397  virtual void setB( const Teuchos::RCP<const Vector> b ) {
398  B = b;
399  if (b != Teuchos::null)
401  else
402  flat_B = Teuchos::null;
403  flat_solver->setB(flat_B);
404  }
405 
406 
408  virtual void setB( const Vector* b ) {
409  if (b != 0) {
410  B = Teuchos::rcp(b, false);
412  }
413  else {
414  B = Teuchos::null;
415  flat_B = Teuchos::null;
416  }
417  flat_solver->setB(flat_B);
418  }
419 
420 
422  virtual const Teuchos::RCP<const Vector> getB( void ) {
423  return B;
424  }
425 
426 
428  virtual const Vector* getBRaw( void ) {
429  return B.get();
430  }
431 
432 
434  virtual Teuchos::RCP<const Teuchos::Comm<int> > getComm( void ) const {
435  return flat_solver->getComm();
436  }
437 
438 
440  virtual Status& getStatus() const {
441  return flat_solver->getStatus();
442  }
443 
444 
446  virtual std::string name( void ) const {
447  return flat_solver->name();
448  }
449 
451 
452 
457  virtual std::string description( void ) const {
459  return flat_solver->description();
460  }
461 
462 
465  virtual void describe( Teuchos::FancyOStream &out,
466  const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default ) const {
467  flat_solver->describe(out, verbLevel);
468  }
469 
471 
472 
477  virtual void printTiming( Teuchos::FancyOStream &out,
479  const Teuchos::EVerbosityLevel verbLevel = Teuchos::Describable::verbLevel_default ) const{
480  flat_solver->printTiming(out, verbLevel);
481  }
482 
483 
492  virtual void getTiming( Teuchos::ParameterList& timingParameterList ) const{
493  flat_solver->getTiming(timingParameterList);
494  }
495 
497 
498  protected:
499 
500  Teuchos::RCP<const Matrix> A;
501  Teuchos::RCP<Vector> X;
502  Teuchos::RCP<const Vector> B;
503  Teuchos::RCP<const Map> flat_X_map, flat_B_map;
504  Teuchos::RCP<const FlatGraph> flat_graph;
505  Teuchos::RCP<const FlatMatrix> flat_A;
506  Teuchos::RCP<FlatVector> flat_X;
507  Teuchos::RCP<const FlatVector> flat_B;
508  Teuchos::RCP<FlatSolver> flat_solver;
509 
510  };
511 
512  template < template <class,class> class ConcreteSolver,
513  class ST, class LO, class GO, class NO >
516  typedef Tpetra::CrsMatrix<SC,LO,GO,NO> Matrix;
517  typedef Tpetra::MultiVector<SC,LO,GO,NO> Vector;
518  static Teuchos::RCP<Solver<Matrix,Vector> >
519  apply(Teuchos::RCP<const Matrix> A,
520  Teuchos::RCP<Vector> X,
521  Teuchos::RCP<const Vector> B ) {
522  ctassert<
523  Meta::is_same<
524  typename MatrixTraits<Matrix>::scalar_t,
525  typename MultiVecAdapter<Vector>::scalar_t
526  >::value
527  > same_scalar_assertion;
528  (void)same_scalar_assertion; // This stops the compiler from warning about unused declared variables
529 
530  // If our assertion did not fail, then create and return a new solver
531  return Teuchos::rcp( new MPVectorSolverAdapter<ST,LO,GO,NO,ConcreteSolver>(A, X, B) );
532  }
533  };
534 
535  // Specialization of create_solver_with_supported_type for
536  // Sacado::MP::Vector where we create MPVectorSolverAdapter wrapping
537  // each solver
538  template < template <class,class> class ConcreteSolver,
539  class ST, class LO, class GO, class NO >
540  struct create_solver_with_supported_type<
541  ConcreteSolver,
542  Tpetra::CrsMatrix<Sacado::MP::Vector<ST>,LO,GO,NO>,
543  Tpetra::MultiVector<Sacado::MP::Vector<ST>,LO,GO,NO> > :
544  public create_mp_vector_solver_impl<ConcreteSolver, ST, LO, GO, NO> {};
545 
546  // Specialization for solver_supports_scalar for Sacado::MP::Vector<Storage>
547  // value == true if and only if
548  // solver_supprts_scalar<ConcreteSolver,Storage::value_type> == true
549  template <template <class,class> class ConcreteSolver,
550  typename Storage>
551  struct solver_supports_scalar<ConcreteSolver, Sacado::MP::Vector<Storage> > {
553  typedef typename Scalar::value_type BaseScalar;
554  typedef typename solver_traits<ConcreteSolver>::supported_scalars supported_scalars;
555  static const bool value =
556  Meta::if_then_else<Meta::is_same<supported_scalars, Meta::nil_t>::value,
557  Meta::true_type,
558  Meta::type_list_contains<supported_scalars,
559  BaseScalar> >::type::value;
560  };
561 
562 } // namespace Amesos2
563 
564 #endif // AMESOS2_SOLVER_MP_VECTOR_HPP
virtual void setX(Vector *x)
Sets the LHS vector X using a raw pointer.
Stokhos::StandardStorage< int, double > Storage
virtual void setA(const Teuchos::RCP< const Matrix > a, EPhase keep_phase=CLEAN)
Sets the matrix A of this solver.
virtual void solve(void)
Solves (or )
virtual type & preOrdering(void)
Pre-orders the matrix.
virtual const Teuchos::RCP< Vector > getX(void)
Returns the vector that is the LHS of the linear system.
virtual const Vector * getBRaw(void)
Returns a raw pointer to the RHS of the linear system.
virtual void getTiming(Teuchos::ParameterList &timingParameterList) const
Extracts timing information from the current solver.
virtual void printTiming(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Prints timing information about the current solver.
virtual void setB(const Vector *b)
Sets the RHS vector B using a raw pointer.
virtual Status & getStatus() const
Returns a reference to this solver&#39;s internal status object.
Tpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node > FlatGraph
Solver< FlatMatrix, FlatVector > FlatSolver
Teuchos::RCP< const FlatGraph > flat_graph
Teuchos::RCP< FlatSolver > flat_solver
virtual type & symbolicFactorization(void)
Performs symbolic factorization on the matrix.
static Teuchos::RCP< Solver< Matrix, Vector > > apply(Teuchos::RCP< const Matrix > A, Teuchos::RCP< Vector > X, Teuchos::RCP< const Vector > B)
Teuchos::RCP< const FlatMatrix > flat_A
Tpetra::CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > Matrix
virtual std::string name(void) const
Return the name of this solver.
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType * x
Definition: csr_vector.h:260
Teuchos::RCP< Tpetra::CrsMatrix< typename Storage::value_type, LocalOrdinal, GlobalOrdinal, Kokkos::Compat::KokkosDeviceWrapperNode< Device > > > create_flat_matrix(const Tpetra::CrsMatrix< Sacado::UQ::PCE< Storage >, LocalOrdinal, GlobalOrdinal, Kokkos::Compat::KokkosDeviceWrapperNode< Device > > &mat, const Teuchos::RCP< const Tpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Kokkos::Compat::KokkosDeviceWrapperNode< Device > > > &flat_graph, const Teuchos::RCP< const Tpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Kokkos::Compat::KokkosDeviceWrapperNode< Device > > > &cijk_graph, const CijkType &cijk_dev)
ConcreteSolver< FlatMatrix, FlatVector > FlatConcreteSolver
virtual void solve(Vector *XX, const Vector *BB) const
Solve using the given XX and BB (multi)vectors.
virtual void setX(const Teuchos::RCP< Vector > x)
Sets the LHS vector X.
KokkosClassic::DefaultNode::DefaultNodeType Node
KOKKOS_INLINE_FUNCTION constexpr std::enable_if< is_view_uq_pce< View< T, P... > >::value, unsigned >::type dimension_scalar(const View< T, P... > &view)
virtual Teuchos::RCP< const Teuchos::Comm< int > > getComm(void) const
Returns a pointer to the Teuchos::Comm communicator with this matrix.
Teuchos::RCP< Tpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > create_flat_mp_graph(const Tpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node > &graph, Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > &flat_domain_map, Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > &flat_range_map, const LocalOrdinal block_size)
Tpetra::MultiVector< BaseScalar, LocalOrdinal, GlobalOrdinal, Node > FlatVector
virtual void setA(const Matrix *a, EPhase keep_phase=CLEAN)
Sets the matrix A of this solver.
virtual void solve(const Teuchos::Ptr< Vector > XX, const Teuchos::Ptr< const Vector > BB) const
Solve using the given XX and BB (multi)vectors.
virtual Teuchos::RCP< const Teuchos::ParameterList > getValidParameters(void) const
Return a const parameter list of all of the valid parameters that this->setParameterList(...) will accept.
virtual bool matrixShapeOK(void)
Returns true if the solver can handle the matrix shape.
virtual void setB(const Teuchos::RCP< const Vector > b)
Sets the RHS vector B.
virtual std::string description(void) const
Returns a short description of this Solver.
Tpetra::MultiVector< SC, LO, GO, NO > Vector
MPVectorSolverAdapter(const Teuchos::RCP< const Matrix > &A_, const Teuchos::RCP< Vector > &X_, const Teuchos::RCP< const Vector > &B_)
Constructor.
Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > Vector
Teuchos::RCP< const Tpetra::MultiVector< typename Storage::value_type, LocalOrdinal, GlobalOrdinal, Kokkos::Compat::KokkosDeviceWrapperNode< Device > > > create_flat_vector_view(const Tpetra::MultiVector< Sacado::UQ::PCE< Storage >, LocalOrdinal, GlobalOrdinal, Kokkos::Compat::KokkosDeviceWrapperNode< Device > > &vec, const Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Kokkos::Compat::KokkosDeviceWrapperNode< Device > > > &flat_map)
Tpetra::CrsMatrix< SC, LO, GO, NO > Matrix
Amesos2 solver adapter for MP::Vector scalar type.
virtual type & setParameters(const Teuchos::RCP< Teuchos::ParameterList > &parameterList)
Set/update internal variables and solver options.
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)
Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > Map
Teuchos::RCP< const FlatVector > flat_B
virtual type & numericFactorization(void)
Performs numeric factorization on the matrix.
Tpetra::CrsMatrix< BaseScalar, LocalOrdinal, GlobalOrdinal, Node > FlatMatrix
virtual Vector * getXRaw(void)
Returns a raw pointer to the LHS of the linear system.
virtual const Teuchos::RCP< const Vector > getB(void)
Returns the vector that is the RHS of the linear system.
Sacado::MP::Vector< Storage > Scalar
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const