Amesos2 - Direct Sparse Solver Interfaces  Version of the Day
Amesos2_Factory.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Amesos2: Templated Direct Sparse Solver Package
6 // Copyright 2011 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 //
42 // @HEADER
43 
82 #ifndef AMESOS2_FACTORY_HPP
83 #define AMESOS2_FACTORY_HPP
84 
85 #include "Amesos2_config.h"
86 
87 #include "Amesos2_Solver.hpp"
88 #include "Amesos2_SolverTraits.hpp"
89 
90 #include "Teuchos_ScalarTraits.hpp"
92 #include "Amesos2_MatrixTraits.hpp"
93 #include "Amesos2_ctassert.hpp"
94 
95 #ifdef HAVE_AMESOS2_KLU2
96 #include "Amesos2_Klu2.hpp"
97 #endif
98 #ifdef HAVE_AMESOS2_SUPERLUDIST // Distributed-memory SuperLU
99 #include "Amesos2_Superludist.hpp"
100 #endif
101 #ifdef HAVE_AMESOS2_SUPERLUMT // Multi-threaded SuperLU
102 #include "Amesos2_Superlumt.hpp"
103 #endif
104 #ifdef HAVE_AMESOS2_SUPERLU // Sequential SuperLU
105 #include "Amesos2_Superlu.hpp"
106 #endif
107 #ifdef HAVE_AMESOS2_PARDISO_MKL // MKL version of Pardiso
108 #include "Amesos2_PardisoMKL.hpp"
109 #endif
110 #ifdef HAVE_AMESOS2_LAPACK
111 #include "Amesos2_Lapack.hpp"
112 #endif
113 
114 namespace Amesos2 {
115 
116  template <class,class> class Solver;
117 
118  /*
119  * Utility function to transform a string into all lowercase
120  */
121  std::string tolower(const std::string& s);
122 
123 
138  template < class Matrix,
139  class Vector >
140  Solver<Matrix,Vector>*
141  create(const Matrix* A, Vector* X, const Vector* B);
142 
143 
158  template < class Matrix,
159  class Vector >
160  Teuchos::RCP<Solver<Matrix,Vector> >
161  create(Teuchos::RCP<const Matrix> A,
162  Teuchos::RCP<Vector> X,
163  Teuchos::RCP<const Vector> B);
164 
165 
183  template < class Matrix,
184  class Vector >
185  Solver<Matrix,Vector>*
186  create(const char* solverName, const Matrix* A, Vector* X, const Vector* B);
187 
188 
205  template < class Matrix,
206  class Vector >
207  Teuchos::RCP<Solver<Matrix,Vector> >
208  create(const char* solverName,
209  const Teuchos::RCP<const Matrix> A,
210  const Teuchos::RCP<Vector> X,
211  const Teuchos::RCP<const Vector> B);
212 
213 
230  template < class Matrix,
231  class Vector >
232  Solver<Matrix,Vector>*
233  create(const std::string solverName, const Matrix* A, Vector* X, const Vector* B);
234 
235 
252  template < class Matrix,
253  class Vector >
254  Teuchos::RCP<Solver<Matrix,Vector> >
255  create(const std::string solverName,
256  const Teuchos::RCP<const Matrix> A,
257  const Teuchos::RCP<Vector> X,
258  const Teuchos::RCP<const Vector> B);
259 
260 
279  template < class Matrix,
280  class Vector >
281  Solver<Matrix,Vector>*
282  create(const std::string solverName, const Matrix* A);
283 
284 
303  template < class Matrix,
304  class Vector >
305  Teuchos::RCP<Solver<Matrix,Vector> >
306  create(const std::string solverName,
307  const Teuchos::RCP<const Matrix> A);
308 
309 
311  // Meta-functions to help with creation of solvers //
313 
314  template < template <class,class> class ConcreteSolver,
315  class Matrix,
316  class Vector >
317  struct create_solver_with_supported_type {
318  static Teuchos::RCP<Solver<Matrix,Vector> > apply(Teuchos::RCP<const Matrix> A,
319  Teuchos::RCP<Vector> X,
320  Teuchos::RCP<const Vector> B )
321  {
322  ctassert<
323  Meta::is_same<
324  typename MatrixTraits<Matrix>::scalar_t,
325  typename MultiVecAdapter<Vector>::scalar_t
326  >::value
327  > same_scalar_assertion;
328  (void)same_scalar_assertion; // This stops the compiler from warning about unused declared variables
329 
330  // If our assertion did not fail, then create and return a new solver
331  return rcp( new ConcreteSolver<Matrix,Vector>(A, X, B) );
332  }
333  };
334 
343 template < template <class,class> class ConcreteSolver,
344  class Matrix,
345  class Vector >
346 struct throw_no_scalar_support_exception {
347  static Teuchos::RCP<Solver<Matrix,Vector> > apply(Teuchos::RCP<const Matrix> A,
348  Teuchos::RCP<Vector> X,
349  Teuchos::RCP<const Vector> B )
350  {
351  // typedef ConcreteSolver<Matrix,Vector> concretesolver_matrix_vector;
352  typedef typename MatrixTraits<Matrix>::scalar_t scalar_t;
353  TEUCHOS_TEST_FOR_EXCEPTION( true,
354  std::invalid_argument,
355  "The requested Amesos2 "
356  // << concretesolver_matrix_vector::name <<
357  " solver interface does not support the " <<
358  Teuchos::ScalarTraits<scalar_t>::name() <<
359  " scalar type." );
360  }
361 };
362 
372  template < template <class,class> class ConcreteSolver,
373  class Matrix,
374  class Vector >
375  struct handle_solver_type_support {
376  static Teuchos::RCP<Solver<Matrix,Vector> > apply(Teuchos::RCP<const Matrix> A,
377  Teuchos::RCP<Vector> X,
378  Teuchos::RCP<const Vector> B )
379  {
380  return Meta::if_then_else<
381  solver_supports_scalar<ConcreteSolver, typename MatrixTraits<Matrix>::scalar_t>::value,
382  create_solver_with_supported_type<ConcreteSolver,Matrix,Vector>,
383  throw_no_scalar_support_exception<ConcreteSolver,Matrix,Vector> >::type::apply(A, X, B);
384  }
385  };
386 
387 
389  // Query Functions //
391 
399  bool query(const char* solverName);
400 
401 
409  bool query(const std::string solverName);
410 
411 
413  // Definitions //
415 
416  template <class Matrix,
417  class Vector >
418  Solver<Matrix,Vector>*
419  create(Matrix* A, Vector* X, Vector* B)
420  {
421  std::string solver = "Klu2";
422  // Pass non-owning RCP objects to other factory method
423  return( create(solver, rcp(A,false), rcp(X,false), rcp(B,false)).getRawPtr() );
424  }
425 
426 
427  template <class Matrix,
428  class Vector >
429  Teuchos::RCP<Solver<Matrix,Vector> >
430  create(Teuchos::RCP<const Matrix> A,
431  Teuchos::RCP<Vector> X,
432  Teuchos::RCP<const Vector> B)
433  {
434  std::string solver = "Klu2";
435  return( create(solver, A, X, B) );
436  }
437 
438 
439  template <class Matrix,
440  class Vector >
441  Solver<Matrix,Vector>*
442  create(const char* solverName, const Matrix* A, Vector* X, const Vector* B)
443  {
444  std::string solver = solverName;
445  // Pass non-owning Teuchos::RCP objects to other factory method
446  return( create(solver, rcp(A,false), rcp(X,false), rcp(B,false)).getRawPtr() );
447  }
448 
449 
450  template <class Matrix,
451  class Vector >
452  Teuchos::RCP<Solver<Matrix,Vector> >
453  create(const char* solverName,
454  const Teuchos::RCP<const Matrix> A,
455  const Teuchos::RCP<Vector> X,
456  const Teuchos::RCP<const Vector> B)
457  {
458  std::string solver = solverName;
459  return( create(solver, A, X, B) );
460  }
461 
462 
463  template <class Matrix,
464  class Vector >
465  Solver<Matrix,Vector>*
466  create(const std::string solverName, const Matrix* A){
467  return( create(solverName, rcp(A,false),
468  Teuchos::RCP<Vector>(),
469  Teuchos::RCP<const Vector>()).getRawPtr() );
470  }
471 
472 
473  template <class Matrix,
474  class Vector >
475  Teuchos::RCP<Solver<Matrix,Vector> >
476  create(const std::string solverName, const Teuchos::RCP<const Matrix> A){
477  return( create(solverName, A, Teuchos::RCP<Vector>(), Teuchos::RCP<const Vector>()) );
478  }
479 
480 
481  template <class Matrix,
482  class Vector >
483  Teuchos::RCP<Solver<Matrix,Vector> >
484  create(const std::string solverName, const Matrix* A, Vector* X, const Vector* B)
485  {
486  // Pass non-owning Teuchos::RCP objects to other factory method
487  return( create(solverName, rcp(A,false), rcp(X,false), rcp(B,false)) );
488  }
489 
490 
491  template <class Matrix,
492  class Vector >
493  Teuchos::RCP<Solver<Matrix,Vector> >
494  create(const std::string solver_name,
495  const Teuchos::RCP<const Matrix> A,
496  const Teuchos::RCP<Vector> X,
497  const Teuchos::RCP<const Vector> B)
498  {
499  std::string solverName = tolower(solver_name); // for easy string checking
500 
501  // Check for our native solver first. Treat KLU and KLU2 as equals.
502  //
503  // We use compiler guards in case a user does want to disable KLU2
504 #ifdef HAVE_AMESOS2_KLU2
505  if((solverName == "amesos2_klu2") || (solverName == "klu2") ||
506  (solverName == "amesos2_klu") || (solverName == "klu")){
507  return handle_solver_type_support<Klu2,Matrix,Vector>::apply(A, X, B);
508  }
509 #endif
510 
511 #ifdef HAVE_AMESOS2_SUPERLUDIST
512  if((solverName == "amesos2_superludist") ||
513  (solverName == "superludist") ||
514  (solverName == "amesos2_superlu_dist") ||
515  (solverName == "superlu_dist")){
516  return handle_solver_type_support<Superludist,Matrix,Vector>::apply(A, X, B);
517  }
518 #endif
519 
520 #ifdef HAVE_AMESOS2_SUPERLUMT
521  if((solverName == "amesos2_superlumt") ||
522  (solverName == "superlumt") ||
523  (solverName == "amesos2_superlu_mt") ||
524  (solverName == "superlu_mt")){
525  return handle_solver_type_support<Superlumt,Matrix,Vector>::apply(A, X, B);
526  }
527 #endif
528 
529 #ifdef HAVE_AMESOS2_SUPERLU
530  if((solverName == "amesos2_superlu") ||
531  (solverName == "superlu")){
532  return handle_solver_type_support<Superlu,Matrix,Vector>::apply(A, X, B);
533  }
534 #endif
535 
536 #ifdef HAVE_AMESOS2_PARDISO_MKL
537  if((solverName == "amesos2_pardiso_mkl") ||
538  (solverName == "pardiso_mkl") ||
539  (solverName == "amesos2_pardisomkl") ||
540  (solverName == "pardisomkl")){
541  return handle_solver_type_support<PardisoMKL,Matrix,Vector>::apply(A, X, B);
542  }
543 #endif
544 
545 #ifdef HAVE_AMESOS2_LAPACK
546  if((solverName == "amesos2_lapack") ||
547  (solverName == "lapack")){
548  return handle_solver_type_support<Lapack,Matrix,Vector>::apply(A, X, B);
549  }
550 #endif
551 
552  /* If none of the above conditionals are satisfied, then the solver
553  * requested is not yet supported. We throw a runtime exception stating
554  * this, and return null.
555  */
556  std::string err_msg = solver_name + " is not enabled or is not supported";
557  TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument, err_msg);
558  return( Teuchos::null );
559  }
560 
561 } // end namespace Amesos2
562 
563 #endif // AMESOS2_FACTORY_HPP
A templated adapter/wrapper class for Trilinos Multivector type classes. Provides the functions neces...
Definition: Amesos2_AbstractConcreteMatrixAdapter.hpp:48
Simple compile-time assertion class.
Provides access to interesting solver traits.