Debian Codegraph @

packages/stokhos/src/sacado/kokkos/pce/amesos2/Amesos2_Solver_UQ_PCE.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #ifndef AMESOS2_SOLVER_UQ_PCE_HPP
11 #define AMESOS2_SOLVER_UQ_PCE_HPP
12
13 #include "Amesos2_Solver.hpp"
14 #include "Amesos2_Factory.hpp"
15 #include "Stokhos_Sacado_Kokkos_UQ_PCE.hpp"
16 #include "Stokhos_Tpetra_Utilities_UQ_PCE.hpp"
17
18 namespace Amesos2 {
19
20 template <class S, class LO, class GO, class NO>
21 typename Sacado::UQ::PCE<S>::cijk_type
22 get_pce_cijk(
23 const Teuchos::RCP<const Tpetra::CrsMatrix<Sacado::UQ::PCE<S>, LO, GO, NO > >& A = Teuchos::null,
24 const Teuchos::RCP<Tpetra::MultiVector<Sacado::UQ::PCE<S>, LO, GO, NO > >& X = Teuchos::null,
25 const Teuchos::RCP<const Tpetra::MultiVector<Sacado::UQ::PCE<S>, LO, GO, NO > >& B = Teuchos::null)
26 {
27 if (A != Teuchos::null) {
28 return Kokkos::cijk(A->getLocalValuesDevice(Tpetra::Access::ReadOnly));
29 }
30 else if (X != Teuchos::null) {
31 return Kokkos::cijk(X->getLocalViewDevice(Tpetra::Access::ReadOnly));
32 }
33 else if (B != Teuchos::null) {
34 return Kokkos::cijk(B->getLocalViewDevice(Tpetra::Access::ReadOnly));
35 }
36 return typename Sacado::UQ::PCE<S>::cijk_type();
37 }
38
39 /// \brief Amesos2 solver adapter for UQ::PCE scalar type
40 ///
41 /// This adapter enables Amesos2 solvers to work with Tpetra matrices
42 /// and vectors of the Sacado::UQ::PCE scalar type by "flattening"
43 /// these matrices and vectors into ones with a standard (e.g., double)
44 /// scalar type.
45 template <class Storage, class LocalOrdinal, class GlobalOrdinal,
46 class Node, template<class,class> class ConcreteSolver>
47 class PCESolverAdapter :
48 public Solver< Tpetra::CrsMatrix<Sacado::UQ::PCE<Storage>,
49 LocalOrdinal,
50 GlobalOrdinal,
51 Node >,
52 Tpetra::MultiVector<Sacado::UQ::PCE<Storage>,
53 LocalOrdinal,
54 GlobalOrdinal,
55 Node >
56 >
57 {
58 public:
59
60 typedef Sacado::UQ::PCE<Storage> Scalar;
61 typedef Tpetra::CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> Matrix;
62 typedef Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> Vector;
63
64 typedef typename Scalar::value_type BaseScalar;
65 typedef Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> Map;
66 typedef Tpetra::CrsGraph<LocalOrdinal,GlobalOrdinal,Node> FlatGraph;
67 typedef Tpetra::CrsMatrix<BaseScalar,LocalOrdinal,GlobalOrdinal,Node> FlatMatrix;
68 typedef Tpetra::MultiVector<BaseScalar,LocalOrdinal,GlobalOrdinal,Node> FlatVector;
69 typedef ConcreteSolver<FlatMatrix,FlatVector> FlatConcreteSolver;
70 typedef Solver<FlatMatrix,FlatVector> FlatSolver;
71
72 typedef Solver<Matrix,Vector> solver_type;
73 typedef typename solver_type::type type;
74 typedef typename Scalar::cijk_type cijk_type;
75
76 /// Constructor
77 PCESolverAdapter(
78 const Teuchos::RCP<const Matrix>& A_,
79 const Teuchos::RCP<Vector>& X_,
80 const Teuchos::RCP<const Vector>& B_) :
81 A(A_), X(X_), B(B_) {
82 cijk = get_pce_cijk(A, X, B);
83 const size_t pce_size =
84 Kokkos::dimension_scalar(A->getLocalMatrixDevice().values);
85 flat_graph =
86 Stokhos::create_flat_pce_graph(*(A->getCrsGraph()),
87 cijk,
88 flat_X_map,
89 flat_B_map,
90 cijk_graph,
91 pce_size);
92 if (A != Teuchos::null)
93 flat_A = Stokhos::create_flat_matrix(*A, flat_graph, cijk_graph, cijk);
94 if (X != Teuchos::null)
95 flat_X = Stokhos::create_flat_vector_view(*X, flat_X_map);
96 if (B != Teuchos::null)
97 flat_B = Stokhos::create_flat_vector_view(*B, flat_B_map);
98 flat_solver =
99 create_solver_with_supported_type<ConcreteSolver,FlatMatrix,FlatVector>::apply(flat_A, flat_X, flat_B);
100 }
101
102 /// \name Mathematical Functions
103 //@{
104
105 /** \brief Pre-orders the matrix.
106 *
107 * Uses the default solver option, unless a solver-specific
108 * pre-ordering parameter is given.
109 *
110 * \sa setParameters
111 */
112 virtual type& preOrdering( void ) {
113 flat_solver->preOrdering();
114 return *this;
115 }
116
117
118 /** \brief Performs symbolic factorization on the matrix
119 *
120 * \pre
121 * - The matrix A must not be \c null
122 */
123 virtual type& symbolicFactorization( void ) {
124 flat_solver->symbolicFactorization();
125 return *this;
126 }
127
128
129 /** \brief Performs numeric factorization on the matrix
130 *
131 * numericFactorization checks first that symbolicFactorization
132 * has successfully been called, and if not, calls it before
133 * continuing.
134 *
135 * \pre
136 * - The matrix A must not be \c null
137 *
138 * \post
139 * - The factors L and U of A are computed
140 */
141 virtual type& numericFactorization( void ) {
142 flat_solver->numericFactorization();
143 return *this;
144 }
145
146
147 /** \brief Solves \f$ A X = B\f$ (or \f$ A^T X = B\f$ )
148 *
149 * solve checks first that numericFactorization has successfully
150 * been called, and if not, calls it before continuing.
151 *
152 * \pre
153 * - The (multi)vectors \c X and \c B must not be \c null
154 *
155 * \post
156 * - The (multi)vector \c X (given at construction time) contains
157 * the solution to the system.
158 */
159 virtual void solve( void ) {
160 flat_solver->solve();
161 }
162
163
164 /** \brief Solve \f$ A X = B\f$ using the given X and B vectors.
165 *
166 * This overload of solve uses the given X and B vectors when
167 * solving. This X and B are used in place of any X and B that
168 * were given upon construction of the Amesos2 solver instance and
169 * are used only for this solve.
170 *
171 * If a permanent change of X and B are required, see the setX()
172 * and setB() methods.
173 *
174 * \post
175 * - The (multi)vector \c XX contains the solution to the system
176 * - The \c XX and \c BB given at construction time (if any) are unchanged.
177 */
178 virtual void solve(const Teuchos::Ptr<Vector> XX,
179 const Teuchos::Ptr<const Vector> BB) const {
180 flat_solver->solve(
181 Stokhos::create_flat_vector_view(*XX, flat_X_map).get(),
182 Stokhos::create_flat_vector_view(*BB, flat_B_map).get() );
183 }
184
185
186 /** \brief Solve \f$ A X = B\f$ using the given X and B vectors.
187 *
188 * This overload of solve uses the given X and B vectors when
189 * solving. This X and B are used in place of any X and B that
190 * were given upon construction of the Amesos2 solver instance and
191 * are used only for this solve.
192 *
193 * If a permanent change of X and B are required, see the setX()
194 * and setB() methods.
195 *
196 * \post
197 * - The (multi)vector \c XX contains the solution to the system
198 * - The \c XX and \c BB given at construction time (if any) are unchanged.
199 */
200 virtual void solve(Vector* XX, const Vector* BB) const {
201 flat_solver->solve(
202 Stokhos::create_flat_vector_view(*XX, flat_X_map).get(),
203 Stokhos::create_flat_vector_view(*BB, flat_B_map).get() );
204 }
205
206 //@} End Mathematical Functions
207
208
209 /** \name Parameter Methods
210 * @{
211 */
212
213 /** \brief Set/update internal variables and solver options.
214 *
215 * Expects that parameterList be named "Amesos2". That list may
216 * contain Amesos2-specific parameters. In addition, it may
217 * contain sublist for solver-specific parameters. These sublists
218 * should be named according to what is returned by the name()
219 * function (i.e. The solver's name when enabling for Amesos2
220 * during configuration).
221 *
222 * See each solver interface directly for a list of the supported
223 * parameters for that solver.
224 */
225 virtual type& setParameters(
226 const Teuchos::RCP<Teuchos::ParameterList> & parameterList ) {
227 flat_solver->setParameters(parameterList);
228 return *this;
229 }
230
231
232 /**
233 * \brief Return a const parameter list of all of the valid parameters that
234 * this->setParameterList(...) will accept.
235 */
236 virtual Teuchos::RCP<const Teuchos::ParameterList>
237 getValidParameters( void ) const {
238 return flat_solver->getValidParameters();
239 }
240
241 /// @} End Parameter Methods
242
243
244 /** \name Accessor Methods
245 * @{
246 */
247
248 /** \brief Sets the matrix A of this solver
249 *
250 * \param [in] a An RCP to a matrix will will be used for
251 * future computation steps
252 *
253 * \param [in] keep_phase This parameter tells the solver what
254 * state it should keep. For example, you
255 * may want to replace the matrix but keep
256 * the symbolic factorization because you
257 * know the structure of the new matrix is
258 * the same as the structure of the old
259 * matrix. In this case you would pass
260 * Amesos2::SYMBFACT as this parameter.
261 *
262 * The default value for the second parameter is Amesos2::CLEAN,
263 * which means that the internal state of the solver will be
264 * completely reset. It will be as if no previous computational
265 * steps were performed.
266 */
267 virtual void setA( const Teuchos::RCP<const Matrix> a,
268 EPhase keep_phase = CLEAN ) {
269 A = a;
270
271 // Rebuild flat matrix/graph
272 cijk = get_pce_cijk(A);
273 if (keep_phase <= CLEAN) {
274 flat_X_map = Teuchos::null;
275 flat_B_map = Teuchos::null;
276 flat_graph = Teuchos::null;
277 flat_graph =
278 Stokhos::create_flat_pce_graph(*(A->getCrsGraph()),
279 cijk,
280 flat_X_map,
281 flat_B_map,
282 cijk_graph,
283 Kokkos::dimension_scalar(A->getLocalMatrixDevice().values));
284 }
285 if (keep_phase <= SYMBFACT) // should this by NUMFACT???
286 flat_A = Stokhos::create_flat_matrix(*a, flat_graph, cijk_graph, cijk);
287
288 flat_solver->setA(flat_A, keep_phase);
289 }
290
291 /** \brief Sets the matrix A of this solver
292 *
293 * \param [in] a An raw C pointer to a matrix will will
294 * be used for future computation steps.
295 *
296 * \param [in] keep_phase This parameter tells the solver what
297 * state it should keep. For example, you
298 * may want to replace the matrix but keep
299 * the symbolic factorization because you
300 * know the structure of the new matrix is
301 * the same as the structure of the old
302 * matrix. In this case you would pass
303 * Amesos2::SYMBFACT as this parameter.
304 *
305 * The default value for the second parameter is Amesos2::CLEAN,
306 * which means that the internal state of the solver will be
307 * completely reset. It will be as if no previous computational
308 * steps were performed.
309 */
310 virtual void setA( const Matrix* a, EPhase keep_phase = CLEAN ) {
311 this->setA(Teuchos::rcp(a,false), keep_phase);
312 }
313
314
315 /// Returns \c true if the solver can handle the matrix shape
316 virtual bool matrixShapeOK( void ) {
317 return flat_solver->matrixShapeOK();
318 }
319
320
321 /// Sets the LHS vector X
322 virtual void setX( const Teuchos::RCP<Vector> x ) {
323 X = x;
324 if (x != Teuchos::null)
325 flat_X = Stokhos::create_flat_vector_view(*x, flat_X_map);
326 else
327 flat_X = Teuchos::null;
328 flat_solver->setX(flat_X);
329 }
330
331
332 /// Sets the LHS vector X using a raw pointer
333 virtual void setX( Vector* x ) {
334 if (x != 0) {
335 X = Teuchos::rcp(x, false);
336 flat_X = Stokhos::create_flat_vector_view(*x, flat_X_map);
337 }
338 else {
339 X = Teuchos::null;
340 flat_X = Teuchos::null;
341 }
342 flat_solver->setX(flat_X);
343 }
344
345
346 /// Returns the vector that is the LHS of the linear system
347 virtual const Teuchos::RCP<Vector> getX( void ) {
348 return X;
349 }
350
351
352 /// Returns a raw pointer to the LHS of the linear system
353 virtual Vector* getXRaw( void ) {
354 return X.get();
355 }
356
357
358 /// Sets the RHS vector B
359 virtual void setB( const Teuchos::RCP<const Vector> b ) {
360 B = b;
361 if (b != Teuchos::null)
362 flat_B = Stokhos::create_flat_vector_view(*b, flat_B_map);
363 else
364 flat_B = Teuchos::null;
365 flat_solver->setB(flat_B);
366 }
367
368
369 /// Sets the RHS vector B using a raw pointer
370 virtual void setB( const Vector* b ) {
371 if (b != 0) {
372 B = Teuchos::rcp(b, false);
373 flat_B = Stokhos::create_flat_vector_view(*b, flat_B_map);
374 }
375 else {
376 B = Teuchos::null;
377 flat_B = Teuchos::null;
378 }
379 flat_solver->setB(flat_B);
380 }
381
382
383 /// Returns the vector that is the RHS of the linear system
384 virtual const Teuchos::RCP<const Vector> getB( void ) {
385 return B;
386 }
387
388
389 /// Returns a raw pointer to the RHS of the linear system
390 virtual const Vector* getBRaw( void ) {
391 return B.get();
392 }
393
394
395 /// Returns a pointer to the Teuchos::Comm communicator with this matrix
396 virtual Teuchos::RCP<const Teuchos::Comm<int> > getComm( void ) const {
397 return flat_solver->getComm();
398 }
399
400
401 /// Returns a reference to this solver's internal status object
402 virtual Status& getStatus() const {
403 return flat_solver->getStatus();
404 }
405
406
407 /// Return the name of this solver.
408 virtual std::string name( void ) const {
409 return flat_solver->name();
410 }
411
412 /// @} End Accessor Methods
413
414
415 /** \name Methods implementing Describable
416 * @{
417 */
418
419 /// Returns a short description of this Solver
420 virtual std::string description( void ) const {
421 return flat_solver->description();
422 }
423
424
425 /// Prints the status information about the current solver with some level
426 /// of verbosity.
427 virtual void describe( Teuchos::FancyOStream &out,
428 const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default ) const {
429 flat_solver->describe(out, verbLevel);
430 }
431
432 /// @} End Methods implementing Describable
433
434
435 /** \name Performance and Timing
436 * @{
437 */
438
439 /// Prints timing information about the current solver.
440 virtual void printTiming( Teuchos::FancyOStream &out,
441 const Teuchos::EVerbosityLevel verbLevel = Teuchos::Describable::verbLevel_default ) const{
442 flat_solver->printTiming(out, verbLevel);
443 }
444
445
446 /**
447 * \brief Extracts timing information from the current solver.
448 *
449 * Results are placed in the parameter list \c timingParameterList
450 *
451 * \param timingParameterList Accepts timing information from the
452 * current solver
453 */
454 virtual void getTiming( Teuchos::ParameterList& timingParameterList ) const{
455 flat_solver->getTiming(timingParameterList);
456 }
457
458 /// @} End Performance and Timing
459
460 protected:
461
462 Teuchos::RCP<const Matrix> A;
463 Teuchos::RCP<Vector> X;
464 Teuchos::RCP<const Vector> B;
465 Teuchos::RCP<const Map> flat_X_map, flat_B_map;
466 Teuchos::RCP<const FlatGraph> flat_graph, cijk_graph;
467 Teuchos::RCP<const FlatMatrix> flat_A;
468 Teuchos::RCP<FlatVector> flat_X;
469 Teuchos::RCP<const FlatVector> flat_B;
470 Teuchos::RCP<FlatSolver> flat_solver;
471 cijk_type cijk;
472
473 };
474
475 // Specialization of create_solver_with_supported_type for
476 // Sacado::UQ::PCE where we create PCESolverAdapter wrapping
477 // each solver
478 template < template <class,class> class ConcreteSolver,
479 class ST, class LO, class GO, class NO >
480 struct create_solver_with_supported_type<
481 ConcreteSolver,
482 Tpetra::CrsMatrix<Sacado::UQ::PCE<ST>,LO,GO,NO >,
483 Tpetra::MultiVector<Sacado::UQ::PCE<ST>,LO,GO,NO > > {
484 typedef Sacado::UQ::PCE<ST> SC;
485 typedef Tpetra::CrsMatrix<SC,LO,GO,NO> Matrix;
486 typedef Tpetra::MultiVector<SC,LO,GO,NO> Vector;
487 static Teuchos::RCP<Solver<Matrix,Vector> >
488 apply(Teuchos::RCP<const Matrix> A,
489 Teuchos::RCP<Vector> X,
490 Teuchos::RCP<const Vector> B )
491 {
492 ctassert<
493 std::is_same_v<
494 typename MatrixTraits<Matrix>::scalar_t,
495 typename MultiVecAdapter<Vector>::scalar_t
496 >
497 > same_scalar_assertion;
498 (void)same_scalar_assertion; // This stops the compiler from warning about unused declared variables
499
500 // If our assertion did not fail, then create and return a new solver
501 return Teuchos::rcp( new PCESolverAdapter<ST,LO,GO,NO,ConcreteSolver>(A, X, B) );
502 }
503 };
504
505 // Specialization for solver_supports_scalar for Sacado::UQ::PCE<Storage>
506 // value == true if and only if
507 // solver_supprts_scalar<ConcreteSolver,Storage::value_type> == true
508 template <template <class,class> class ConcreteSolver,
509 typename Storage>
510 struct solver_supports_scalar<ConcreteSolver, Sacado::UQ::PCE<Storage> > {
511 typedef Sacado::UQ::PCE<Storage> Scalar;
512 typedef typename Scalar::value_type BaseScalar;
513 typedef typename solver_traits<ConcreteSolver>::supported_scalars supported_scalars;
514 static const bool value =
515 std::conditional_t<std::is_same_v<supported_scalars, Meta::nil_t>,
516 std::true_type,
517 Meta::type_list_contains<supported_scalars,
518 BaseScalar> >::value;
519 };
520
521
522 } // namespace Amesos2
523
524 #endif // AMESOS2_SOLVER_UQ_PCE_HPP