Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Amesos2_Solver_UQ_PCE.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_UQ_PCE_HPP
43 #define AMESOS2_SOLVER_UQ_PCE_HPP
44 
45 #include "Amesos2_Solver.hpp"
46 #include "Amesos2_Factory.hpp"
49 
50 namespace Amesos2 {
51 
52 #if defined(TPETRA_HAVE_KOKKOS_REFACTOR)
53  template <class S, class LO, class GO, class D>
55  get_pce_cijk(
56  const Teuchos::RCP<const Tpetra::CrsMatrix<Sacado::UQ::PCE<S>, LO, GO, Kokkos::Compat::KokkosDeviceWrapperNode<D> > >& A = Teuchos::null,
57  const Teuchos::RCP<Tpetra::MultiVector<Sacado::UQ::PCE<S>, LO, GO, Kokkos::Compat::KokkosDeviceWrapperNode<D> > >& X = Teuchos::null,
58  const Teuchos::RCP<const Tpetra::MultiVector<Sacado::UQ::PCE<S>, LO, GO, Kokkos::Compat::KokkosDeviceWrapperNode<D> > >& B = Teuchos::null)
59  {
60  if (A != Teuchos::null) {
61  return Kokkos::cijk(A->getLocalValuesView());
62  }
63  else if (X != Teuchos::null) {
64  return Kokkos::cijk(X->template getLocalView<D>());
65  }
66  else if (B != Teuchos::null) {
67  return Kokkos::cijk(B->template getLocalView<D>());
68  }
69  return typename Sacado::UQ::PCE<S>::cijk_type();
70  }
71 
78  template <class Storage, class LocalOrdinal, class GlobalOrdinal,
79  class Device, template<class,class> class ConcreteSolver>
80  class PCESolverAdapter :
81  public Solver< Tpetra::CrsMatrix<Sacado::UQ::PCE<Storage>,
82  LocalOrdinal,
83  GlobalOrdinal,
84  Kokkos::Compat::KokkosDeviceWrapperNode<Device> >,
85  Tpetra::MultiVector<Sacado::UQ::PCE<Storage>,
86  LocalOrdinal,
87  GlobalOrdinal,
88  Kokkos::Compat::KokkosDeviceWrapperNode<Device> >
89  >
90  {
91  public:
92 
94  typedef Kokkos::Compat::KokkosDeviceWrapperNode<Device> Node;
95  typedef Tpetra::CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> Matrix;
96  typedef Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> Vector;
97 
98  typedef typename Scalar::value_type BaseScalar;
99  typedef Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> Map;
100  typedef Tpetra::CrsGraph<LocalOrdinal,GlobalOrdinal,Node> FlatGraph;
101  typedef Tpetra::CrsMatrix<BaseScalar,LocalOrdinal,GlobalOrdinal,Node> FlatMatrix;
102  typedef Tpetra::MultiVector<BaseScalar,LocalOrdinal,GlobalOrdinal,Node> FlatVector;
103  typedef ConcreteSolver<FlatMatrix,FlatVector> FlatConcreteSolver;
104  typedef Solver<FlatMatrix,FlatVector> FlatSolver;
105 
106  typedef Solver<Matrix,Vector> solver_type;
107  typedef typename solver_type::type type;
108  typedef typename Scalar::cijk_type cijk_type;
109 
111  PCESolverAdapter(
112  const Teuchos::RCP<const Matrix>& A_,
113  const Teuchos::RCP<Vector>& X_,
114  const Teuchos::RCP<const Vector>& B_) :
115  A(A_), X(X_), B(B_) {
116  cijk = get_pce_cijk(A, X, B);
117  flat_graph =
118  Stokhos::create_flat_pce_graph(*(A->getCrsGraph()),
119  cijk,
120  flat_X_map,
121  flat_B_map,
122  cijk_graph,
123  Kokkos::dimension_scalar(A->getLocalMatrix().values));
124  if (A != Teuchos::null)
125  flat_A = Stokhos::create_flat_matrix(*A, flat_graph, cijk_graph, cijk);
126  if (X != Teuchos::null)
127  flat_X = Stokhos::create_flat_vector_view(*X, flat_X_map);
128  if (B != Teuchos::null)
129  flat_B = Stokhos::create_flat_vector_view(*B, flat_B_map);
130  flat_solver =
131  create_solver_with_supported_type<ConcreteSolver,FlatMatrix,FlatVector>::apply(flat_A, flat_X, flat_B);
132  }
133 
135 
136 
144  virtual type& preOrdering( void ) {
145  flat_solver->preOrdering();
146  return *this;
147  }
148 
149 
155  virtual type& symbolicFactorization( void ) {
156  flat_solver->symbolicFactorization();
157  return *this;
158  }
159 
160 
173  virtual type& numericFactorization( void ) {
174  flat_solver->numericFactorization();
175  return *this;
176  }
177 
178 
191  virtual void solve( void ) {
192  flat_solver->solve();
193  }
194 
195 
210  virtual void solve(const Teuchos::Ptr<Vector> XX,
211  const Teuchos::Ptr<const Vector> BB) const {
212  flat_solver->solve(
213  Stokhos::create_flat_vector_view(*XX, flat_X_map).get(),
214  Stokhos::create_flat_vector_view(*BB, flat_B_map).get() );
215  }
216 
217 
232  virtual void solve(Vector* XX, const Vector* BB) const {
233  flat_solver->solve(
234  Stokhos::create_flat_vector_view(*XX, flat_X_map).get(),
235  Stokhos::create_flat_vector_view(*BB, flat_B_map).get() );
236  }
237 
239 
240 
257  virtual type& setParameters(
258  const Teuchos::RCP<Teuchos::ParameterList> & parameterList ) {
259  flat_solver->setParameters(parameterList);
260  return *this;
261  }
262 
263 
269  getValidParameters( void ) const {
270  return flat_solver->getValidParameters();
271  }
272 
274 
275 
299  virtual void setA( const Teuchos::RCP<const Matrix> a,
300  EPhase keep_phase = CLEAN ) {
301  A = a;
302 
303  // Rebuild flat matrix/graph
304  cijk = get_pce_cijk(A);
305  if (keep_phase <= CLEAN) {
306  flat_X_map = Teuchos::null;
307  flat_B_map = Teuchos::null;
308  flat_graph = Teuchos::null;
309  flat_graph =
310  Stokhos::create_flat_pce_graph(*(A->getCrsGraph()),
311  cijk,
312  flat_X_map,
313  flat_B_map,
314  cijk_graph,
315  Kokkos::dimension_scalar(A->getLocalMatrix().values));
316  }
317  if (keep_phase <= SYMBFACT) // should this by NUMFACT???
318  flat_A = Stokhos::create_flat_matrix(*a, flat_graph, cijk_graph, cijk);
319 
320  flat_solver->setA(flat_A, keep_phase);
321  }
322 
342  virtual void setA( const Matrix* a, EPhase keep_phase = CLEAN ) {
343  this->setA(Teuchos::rcp(a,false), keep_phase);
344  }
345 
346 
348  virtual bool matrixShapeOK( void ) {
349  return flat_solver->matrixShapeOK();
350  }
351 
352 
354  virtual void setX( const Teuchos::RCP<Vector> x ) {
355  X = x;
356  if (x != Teuchos::null)
357  flat_X = Stokhos::create_flat_vector_view(*x, flat_X_map);
358  else
359  flat_X = Teuchos::null;
360  flat_solver->setX(flat_X);
361  }
362 
363 
365  virtual void setX( Vector* x ) {
366  if (x != 0) {
367  X = Teuchos::rcp(x, false);
368  flat_X = Stokhos::create_flat_vector_view(*x, flat_X_map);
369  }
370  else {
371  X = Teuchos::null;
372  flat_X = Teuchos::null;
373  }
374  flat_solver->setX(flat_X);
375  }
376 
377 
379  virtual const Teuchos::RCP<Vector> getX( void ) {
380  return X;
381  }
382 
383 
385  virtual Vector* getXRaw( void ) {
386  return X.get();
387  }
388 
389 
391  virtual void setB( const Teuchos::RCP<const Vector> b ) {
392  B = b;
393  if (b != Teuchos::null)
394  flat_B = Stokhos::create_flat_vector_view(*b, flat_B_map);
395  else
396  flat_B = Teuchos::null;
397  flat_solver->setB(flat_B);
398  }
399 
400 
402  virtual void setB( const Vector* b ) {
403  if (b != 0) {
404  B = Teuchos::rcp(b, false);
405  flat_B = Stokhos::create_flat_vector_view(*b, flat_B_map);
406  }
407  else {
408  B = Teuchos::null;
409  flat_B = Teuchos::null;
410  }
411  flat_solver->setB(flat_B);
412  }
413 
414 
416  virtual const Teuchos::RCP<const Vector> getB( void ) {
417  return B;
418  }
419 
420 
422  virtual const Vector* getBRaw( void ) {
423  return B.get();
424  }
425 
426 
428  virtual Teuchos::RCP<const Teuchos::Comm<int> > getComm( void ) const {
429  return flat_solver->getComm();
430  }
431 
432 
434  virtual Status& getStatus() const {
435  return flat_solver->getStatus();
436  }
437 
438 
440  virtual std::string name( void ) const {
441  return flat_solver->name();
442  }
443 
445 
446 
451  virtual std::string description( void ) const {
453  return flat_solver->description();
454  }
455 
456 
459  virtual void describe( Teuchos::FancyOStream &out,
461  flat_solver->describe(out, verbLevel);
462  }
463 
465 
466 
471  virtual void printTiming( Teuchos::FancyOStream &out,
474  flat_solver->printTiming(out, verbLevel);
475  }
476 
477 
486  virtual void getTiming( Teuchos::ParameterList& timingParameterList ) const{
487  flat_solver->getTiming(timingParameterList);
488  }
489 
491 
492  protected:
493 
497  Teuchos::RCP<const Map> flat_X_map, flat_B_map;
498  Teuchos::RCP<const FlatGraph> flat_graph, cijk_graph;
502  Teuchos::RCP<FlatSolver> flat_solver;
503  cijk_type cijk;
504 
505  };
506 
507  // Specialization of create_solver_with_supported_type for
508  // Sacado::UQ::PCE where we create PCESolverAdapter wrapping
509  // each solver
510  template < template <class,class> class ConcreteSolver,
511  class ST, class LO, class GO, class D >
512  struct create_solver_with_supported_type<
513  ConcreteSolver,
514  Tpetra::CrsMatrix<Sacado::UQ::PCE<ST>,LO,GO,Kokkos::Compat::KokkosDeviceWrapperNode<D> >,
515  Tpetra::MultiVector<Sacado::UQ::PCE<ST>,LO,GO,Kokkos::Compat::KokkosDeviceWrapperNode<D> > > {
516  typedef Sacado::UQ::PCE<ST> SC;
517  typedef Kokkos::Compat::KokkosDeviceWrapperNode<D> NO;
518  typedef Tpetra::CrsMatrix<SC,LO,GO,NO> Matrix;
519  typedef Tpetra::MultiVector<SC,LO,GO,NO> Vector;
524  {
525  ctassert<
526  Meta::is_same<
527  typename MatrixTraits<Matrix>::scalar_t,
528  typename MultiVecAdapter<Vector>::scalar_t
529  >::value
530  > same_scalar_assertion;
531  (void)same_scalar_assertion; // This stops the compiler from warning about unused declared variables
532 
533  // If our assertion did not fail, then create and return a new solver
534  return Teuchos::rcp( new PCESolverAdapter<ST,LO,GO,D,ConcreteSolver>(A, X, B) );
535  }
536  };
537 
538  // Specialization for solver_supports_scalar for Sacado::UQ::PCE<Storage>
539  // value == true if and only if
540  // solver_supprts_scalar<ConcreteSolver,Storage::value_type> == true
541  template <template <class,class> class ConcreteSolver,
542  typename Storage>
543  struct solver_supports_scalar<ConcreteSolver, Sacado::UQ::PCE<Storage> > {
545  typedef typename Scalar::value_type BaseScalar;
546  typedef typename solver_traits<ConcreteSolver>::supported_scalars supported_scalars;
547  static const bool value =
548  Meta::if_then_else<Meta::is_same<supported_scalars, Meta::nil_t>::value,
549  Meta::true_type,
550  Meta::type_list_contains<supported_scalars,
551  BaseScalar> >::type::value;
552  };
553 
554 #endif
555 
556 } // namespace Amesos2
557 
558 #endif // AMESOS2_SOLVER_UQ_PCE_HPP
Stokhos::StandardStorage< int, double > Storage
T * get() const
KOKKOS_INLINE_FUNCTION constexpr std::enable_if< is_view_uq_pce< View< T, P...> >::value, unsigned >::type dimension_scalar(const View< T, P...> &view)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::RCP< const Tpetra::MultiVector< typename Storage::value_type, LocalOrdinal, GlobalOrdinal, Node > > create_flat_vector_view(const Tpetra::MultiVector< Sacado::MP::Vector< Storage >, LocalOrdinal, GlobalOrdinal, Node > &vec_const, const Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > &flat_map)
KokkosClassic::DefaultNode::DefaultNodeType Node
Teuchos::RCP< Tpetra::CrsMatrix< typename Storage::value_type, LocalOrdinal, GlobalOrdinal, Node > > create_flat_matrix(const Tpetra::CrsMatrix< Sacado::MP::Vector< Storage >, LocalOrdinal, GlobalOrdinal, Node > &mat, const Teuchos::RCP< const Tpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > &flat_graph, const LocalOrdinal block_size)
static const EVerbosityLevel verbLevel_default
KOKKOS_INLINE_FUNCTION constexpr std::enable_if< is_view_uq_pce< view_type >::value, typename CijkType< view_type >::type >::type cijk(const view_type &view)