// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- // vi: set et ts=4 sw=2 sts=2: #ifndef DUNE_ISTL_SUPERLU_HH #define DUNE_ISTL_SUPERLU_HH #if HAVE_SUPERLU #include "superlufunctions.hh" #include "solvers.hh" #include "supermatrix.hh" #include #include #include "bcrsmatrix.hh" #include "bvector.hh" #include "istlexception.hh" #include #include #include #include #include namespace Dune { /** * @addtogroup ISTL * * @{ */ /** * @file * @author Markus Blatt * @brief Classes for using SuperLU with ISTL matrices. */ template class SeqOverlappingSchwarz; template struct SeqOverlappingSchwarzAssemblerHelper; template struct SuperLUSolveChooser {}; template struct SuperLUDenseMatChooser {}; template struct SuperLUQueryChooser {}; template struct QuerySpaceChooser {}; #if __has_include("slu_sdefs.h") template<> struct SuperLUDenseMatChooser { static void create(SuperMatrix *mat, int n, int m, float *dat, int n1, Stype_t stype, Dtype_t dtype, Mtype_t mtype) { sCreate_Dense_Matrix(mat, n, m, dat, n1, stype, dtype, mtype); } static void destroy(SuperMatrix*) {} }; template<> struct SuperLUSolveChooser { static void solve(superlu_options_t *options, SuperMatrix *mat, int *perm_c, int *perm_r, int *etree, char *equed, float *R, float *C, SuperMatrix *L, SuperMatrix *U, void *work, int lwork, SuperMatrix *B, SuperMatrix *X, float *rpg, float *rcond, float *ferr, float *berr, mem_usage_t *memusage, SuperLUStat_t *stat, int *info) { GlobalLU_t gLU; sgssvx(options, mat, perm_c, perm_r, etree, equed, R, C, L, U, work, lwork, B, X, rpg, rcond, ferr, berr, &gLU, memusage, stat, info); } }; template<> struct QuerySpaceChooser { static void querySpace(SuperMatrix* L, SuperMatrix* U, mem_usage_t* memusage) { sQuerySpace(L,U,memusage); } }; #endif #if __has_include("slu_ddefs.h") template<> struct SuperLUDenseMatChooser { static void create(SuperMatrix *mat, int n, int m, double *dat, int n1, Stype_t stype, Dtype_t dtype, Mtype_t mtype) { dCreate_Dense_Matrix(mat, n, m, dat, n1, stype, dtype, mtype); } static void destroy(SuperMatrix * /* mat */) {} }; template<> struct SuperLUSolveChooser { static void solve(superlu_options_t *options, SuperMatrix *mat, int *perm_c, int *perm_r, int *etree, char *equed, double *R, double *C, SuperMatrix *L, SuperMatrix *U, void *work, int lwork, SuperMatrix *B, SuperMatrix *X, double *rpg, double *rcond, double *ferr, double *berr, mem_usage_t *memusage, SuperLUStat_t *stat, int *info) { GlobalLU_t gLU; dgssvx(options, mat, perm_c, perm_r, etree, equed, R, C, L, U, work, lwork, B, X, rpg, rcond, ferr, berr, &gLU, memusage, stat, info); } }; template<> struct QuerySpaceChooser { static void querySpace(SuperMatrix* L, SuperMatrix* U, mem_usage_t* memusage) { dQuerySpace(L,U,memusage); } }; #endif #if __has_include("slu_zdefs.h") template<> struct SuperLUDenseMatChooser > { static void create(SuperMatrix *mat, int n, int m, std::complex *dat, int n1, Stype_t stype, Dtype_t dtype, Mtype_t mtype) { zCreate_Dense_Matrix(mat, n, m, reinterpret_cast(dat), n1, stype, dtype, mtype); } static void destroy(SuperMatrix*) {} }; template<> struct SuperLUSolveChooser > { static void solve(superlu_options_t *options, SuperMatrix *mat, int *perm_c, int *perm_r, int *etree, char *equed, double *R, double *C, SuperMatrix *L, SuperMatrix *U, void *work, int lwork, SuperMatrix *B, SuperMatrix *X, double *rpg, double *rcond, double *ferr, double *berr, mem_usage_t *memusage, SuperLUStat_t *stat, int *info) { GlobalLU_t gLU; zgssvx(options, mat, perm_c, perm_r, etree, equed, R, C, L, U, work, lwork, B, X, rpg, rcond, ferr, berr, &gLU, memusage, stat, info); } }; template<> struct QuerySpaceChooser > { static void querySpace(SuperMatrix* L, SuperMatrix* U, mem_usage_t* memusage) { zQuerySpace(L,U,memusage); } }; #endif #if __has_include("slu_cdefs.h") template<> struct SuperLUDenseMatChooser > { static void create(SuperMatrix *mat, int n, int m, std::complex *dat, int n1, Stype_t stype, Dtype_t dtype, Mtype_t mtype) { cCreate_Dense_Matrix(mat, n, m, reinterpret_cast< singlecomplex*>(dat), n1, stype, dtype, mtype); } static void destroy(SuperMatrix* /* mat */) {} }; template<> struct SuperLUSolveChooser > { static void solve(superlu_options_t *options, SuperMatrix *mat, int *perm_c, int *perm_r, int *etree, char *equed, float *R, float *C, SuperMatrix *L, SuperMatrix *U, void *work, int lwork, SuperMatrix *B, SuperMatrix *X, float *rpg, float *rcond, float *ferr, float *berr, mem_usage_t *memusage, SuperLUStat_t *stat, int *info) { GlobalLU_t gLU; cgssvx(options, mat, perm_c, perm_r, etree, equed, R, C, L, U, work, lwork, B, X, rpg, rcond, ferr, berr, &gLU, memusage, stat, info); } }; template<> struct QuerySpaceChooser > { static void querySpace(SuperMatrix* L, SuperMatrix* U, mem_usage_t* memusage) { cQuerySpace(L,U,memusage); } }; #endif namespace Impl { template struct SuperLUVectorChooser {}; template struct SuperLUVectorChooser,A > > { /** @brief The type of the domain of the solver */ using domain_type = BlockVector< FieldVector, typename std::allocator_traits::template rebind_alloc > >; /** @brief The type of the range of the solver */ using range_type = BlockVector< FieldVector, typename std::allocator_traits::template rebind_alloc > >; }; template struct SuperLUVectorChooser > { /** @brief The type of the domain of the solver */ using domain_type = BlockVector; /** @brief The type of the range of the solver */ using range_type = BlockVector; }; } /** * @brief SuperLu Solver * * Uses the well known SuperLU * package to solve the * system. * * SuperLU supports single and double precision floating point and complex * numbers. Unfortunately these cannot be used at the same time. * Therefore users must set SUPERLU_NTYPE (0: float, 1: double, * 2: std::complex, 3: std::complex) * if the numeric type should be different from double. */ template class SuperLU : public InverseOperator< typename Impl::SuperLUVectorChooser::domain_type, typename Impl::SuperLUVectorChooser::range_type > { using T = typename M::field_type; public: /** @brief The matrix type. */ using Matrix = M; using matrix_type = M; /** @brief The corresponding SuperLU Matrix type.*/ typedef Dune::SuperLUMatrix SuperLUMatrix; /** @brief Type of an associated initializer class. */ typedef SuperMatrixInitializer MatrixInitializer; /** @brief The type of the domain of the solver. */ using domain_type = typename Impl::SuperLUVectorChooser::domain_type; /** @brief The type of the range of the solver. */ using range_type = typename Impl::SuperLUVectorChooser::range_type; //! Category of the solver (see SolverCategory::Category) SolverCategory::Category category() const override { return SolverCategory::Category::sequential; } /** * @brief Constructs the SuperLU solver. * * During the construction the matrix will be decomposed. * That means that in each apply call forward and backward * substitutions take place (and no decomposition). * @param matrix The matrix of the system to solve. * @param verbose If true some statistics are printed. * @param reusevector Default value is true. If true the two vectors are allocate in * the first call to apply. These get resused in subsequent calls to apply * and are deallocated in the destructor. If false these vectors are allocated * at the beginning and deallocated at the end of each apply method. This allows * using the same instance of superlu from different threads. */ explicit SuperLU(const Matrix& matrix, bool verbose=false, bool reusevector=true); /** @brief Constructs the SuperLU solver. * * @param matrix The matrix of the system to solve. * @param config ParameterTree containing solver parameters. * * ParameterTree Key | Meaning * ------------------|------------ * verbose | The verbosity level. default=false * reuseVector | Reuse initially allocated vectors in apply. default=true */ SuperLU(const Matrix& matrix, const ParameterTree& config) : SuperLU(matrix, config.get("verbose", false), config.get("reuseVector", true)) {} /** * @brief Empty default constructor. * * Use setMatrix to tell SuperLU for what matrix it solves. * Using this constructor no vectors will be reused. */ SuperLU(); ~SuperLU(); /** * \copydoc InverseOperator::apply(X&,Y&,InverseOperatorResult&) */ void apply(domain_type& x, range_type& b, InverseOperatorResult& res) override; /** * \copydoc InverseOperator::apply(X&,Y&,double,InverseOperatorResult&) */ void apply (domain_type& x, range_type& b, [[maybe_unused]] double reduction, InverseOperatorResult& res) override { apply(x,b,res); } /** * @brief Apply SuperLu to C arrays. */ void apply(T* x, T* b); /** @brief Initialize data from given matrix. */ void setMatrix(const Matrix& mat); typename SuperLUMatrix::size_type nnz() const { return mat.nonzeroes(); } template void setSubMatrix(const Matrix& mat, const S& rowIndexSet); void setVerbosity(bool v); /** * @brief free allocated space. * @warning later calling apply will result in an error. */ void free(); const char* name() { return "SuperLU"; } private: template friend class SeqOverlappingSchwarz; friend struct SeqOverlappingSchwarzAssemblerHelper,true>; SuperLUMatrix& getInternalMatrix() { return mat; } /** @brief computes the LU Decomposition */ void decompose(); SuperLUMatrix mat; SuperMatrix L, U, B, X; int *perm_c, *perm_r, *etree; typename GetSuperLUType::float_type *R, *C; T *bstore; superlu_options_t options; char equed; void *work; int lwork; bool first, verbose, reusevector; }; template SuperLU ::~SuperLU() { if(mat.N()+mat.M()>0) free(); } template void SuperLU::free() { delete[] perm_c; delete[] perm_r; delete[] etree; delete[] R; delete[] C; if(lwork>=0) { Destroy_SuperNode_Matrix(&L); Destroy_CompCol_Matrix(&U); } lwork=0; if(!first && reusevector) { SUPERLU_FREE(B.Store); SUPERLU_FREE(X.Store); } mat.free(); } template SuperLU ::SuperLU(const Matrix& mat_, bool verbose_, bool reusevector_) : work(0), lwork(0), first(true), verbose(verbose_), reusevector(reusevector_) { setMatrix(mat_); } template SuperLU::SuperLU() : work(0), lwork(0),verbose(false), reusevector(false) {} template void SuperLU::setVerbosity(bool v) { verbose=v; } template void SuperLU::setMatrix(const Matrix& mat_) { if(mat.N()+mat.M()>0) { free(); } lwork=0; work=0; //a=&mat_; mat=mat_; decompose(); } template template void SuperLU::setSubMatrix(const Matrix& mat_, const S& mrs) { if(mat.N()>0 or mat.M()>0) { free(); } lwork=0; work=0; //a=&mat_; mat.setMatrix(mat_,mrs); decompose(); } template void SuperLU::decompose() { first = true; perm_c = new int[mat.M()]; perm_r = new int[mat.N()]; etree = new int[mat.M()]; R = new typename GetSuperLUType::float_type[mat.N()]; C = new typename GetSuperLUType::float_type[mat.M()]; set_default_options(&options); // Do the factorization B.ncol=0; B.Stype=SLU_DN; B.Dtype=GetSuperLUType::type; B.Mtype= SLU_GE; DNformat fakeFormat; fakeFormat.lda=mat.N(); B.Store=&fakeFormat; X.Stype=SLU_DN; X.Dtype=GetSuperLUType::type; X.Mtype= SLU_GE; X.ncol=0; X.Store=&fakeFormat; typename GetSuperLUType::float_type rpg, rcond, ferr=1e10, berr=1e10; int info; mem_usage_t memusage; SuperLUStat_t stat; StatInit(&stat); SuperLUSolveChooser::solve(&options, &static_cast(mat), perm_c, perm_r, etree, &equed, R, C, &L, &U, work, lwork, &B, &X, &rpg, &rcond, &ferr, &berr, &memusage, &stat, &info); if(verbose) { dinfo<<"LU factorization: dgssvx() returns info "<< info<(mat).ncol; if ( info == 0 || info == nSuperLUCol+1 ) { if ( options.PivotGrowth ) dinfo<<"Recip. pivot growth = "< 0 && lwork == -1 ) { // Memory allocation failed dinfo<<"** Estimated memory: "<< info - nSuperLUCol<colptr[i]; c < Ustore->colptr[i+1]; ++c) //if(Ustore->rowind[c]==i) std::cout<rowind[c]<<"->"<<((double*)Ustore->nzval)[c]<<" "; if(k==0){ // k=-1; }std::cout<colptr[i]; c < Ustore->colptr[i+1]; ++c) //if(Ustore->rowind[c]==i) std::cout<rowind[c]<<"->"<<((double*)Ustore->nzval)[c]<<" "; if(k==0){ // k=-1; }std::cout< void SuperLU ::apply(domain_type& x, range_type& b, InverseOperatorResult& res) { if (mat.N() != b.dim()) DUNE_THROW(ISTLError, "Size of right-hand-side vector b does not match the number of matrix rows!"); if (mat.M() != x.dim()) DUNE_THROW(ISTLError, "Size of solution vector x does not match the number of matrix columns!"); if (mat.M()+mat.N()==0) DUNE_THROW(ISTLError, "Matrix of SuperLU is null!"); SuperMatrix* mB = &B; SuperMatrix* mX = &X; SuperMatrix rB, rX; if (reusevector) { if(first) { SuperLUDenseMatChooser::create(&B, (int)mat.N(), 1, reinterpret_cast(&b[0]), (int)mat.N(), SLU_DN, GetSuperLUType::type, SLU_GE); SuperLUDenseMatChooser::create(&X, (int)mat.N(), 1, reinterpret_cast(&x[0]), (int)mat.N(), SLU_DN, GetSuperLUType::type, SLU_GE); first=false; }else{ ((DNformat*)B.Store)->nzval=&b[0]; ((DNformat*)X.Store)->nzval=&x[0]; } } else { SuperLUDenseMatChooser::create(&rB, (int)mat.N(), 1, reinterpret_cast(&b[0]), (int)mat.N(), SLU_DN, GetSuperLUType::type, SLU_GE); SuperLUDenseMatChooser::create(&rX, (int)mat.N(), 1, reinterpret_cast(&x[0]), (int)mat.N(), SLU_DN, GetSuperLUType::type, SLU_GE); mB = &rB; mX = &rX; } typename GetSuperLUType::float_type rpg, rcond, ferr=1e10, berr; int info; mem_usage_t memusage; SuperLUStat_t stat; /* Initialize the statistics variables. */ StatInit(&stat); /* range_type d=b; a->usmv(-1, x, d); double def0=d.two_norm(); */ options.IterRefine=SLU_DOUBLE; SuperLUSolveChooser::solve(&options, &static_cast(mat), perm_c, perm_r, etree, &equed, R, C, &L, &U, work, lwork, mB, mX, &rpg, &rcond, &ferr, &berr, &memusage, &stat, &info); res.iterations=1; /* if(options.Equil==YES) // undo scaling of right hand side std::transform(reinterpret_cast(&b[0]),reinterpret_cast(&b[0])+mat.M(), C, reinterpret_cast(&d[0]), std::divides()); else d=b; a->usmv(-1, x, d); res.reduction=d.two_norm()/def0; res.conv_rate = res.reduction; res.converged=(res.reduction<1e-10||d.two_norm()<1e-18); */ res.converged=true; if(verbose) { dinfo<<"Triangular solve: dgssvx() returns info "<< info<(mat).ncol; if ( info == 0 || info == nSuperLUCol+1 ) { if ( options.IterRefine ) { std::cout<<"Iterative Refinement: steps=" < void SuperLU ::apply(T* x, T* b) { if(mat.N()+mat.M()==0) DUNE_THROW(ISTLError, "Matrix of SuperLU is null!"); SuperMatrix* mB = &B; SuperMatrix* mX = &X; SuperMatrix rB, rX; if (reusevector) { if(first) { SuperLUDenseMatChooser::create(&B, mat.N(), 1, b, mat.N(), SLU_DN, GetSuperLUType::type, SLU_GE); SuperLUDenseMatChooser::create(&X, mat.N(), 1, x, mat.N(), SLU_DN, GetSuperLUType::type, SLU_GE); first=false; }else{ ((DNformat*) B.Store)->nzval=b; ((DNformat*)X.Store)->nzval=x; } } else { SuperLUDenseMatChooser::create(&rB, mat.N(), 1, b, mat.N(), SLU_DN, GetSuperLUType::type, SLU_GE); SuperLUDenseMatChooser::create(&rX, mat.N(), 1, x, mat.N(), SLU_DN, GetSuperLUType::type, SLU_GE); mB = &rB; mX = &rX; } typename GetSuperLUType::float_type rpg, rcond, ferr=1e10, berr; int info; mem_usage_t memusage; SuperLUStat_t stat; /* Initialize the statistics variables. */ StatInit(&stat); options.IterRefine=SLU_DOUBLE; SuperLUSolveChooser::solve(&options, &static_cast(mat), perm_c, perm_r, etree, &equed, R, C, &L, &U, work, lwork, mB, mX, &rpg, &rcond, &ferr, &berr, &memusage, &stat, &info); if(verbose) { dinfo<<"Triangular solve: dgssvx() returns info "<< info<(mat).ncol; if ( info == 0 || info == nSuperLUCol+1 ) { // Factorization has succeeded if ( options.IterRefine ) { dinfo<<"Iterative Refinement: steps=" < struct IsDirectSolver > > { enum { value=true}; }; template struct StoresColumnCompressed > > { enum { value = true }; }; DUNE_REGISTER_SOLVER("superlu", [](auto opTraits, const auto& op, const Dune::ParameterTree& config) -> std::shared_ptr { using OpTraits = decltype(opTraits); using M = typename OpTraits::matrix_type; // works only for sequential operators if constexpr (OpTraits::isParallel){ if(opTraits.getCommOrThrow(op).communicator().size() > 1) DUNE_THROW(Dune::InvalidStateException, "SuperLU works only for sequential operators."); } if constexpr (OpTraits::isAssembled && // check whether the Matrix field_type is double or complex std::is_same_v::real_type, double>){ // check if SuperLU* is convertible to // InverseOperator*. This checks compatibility of the // domain and range types if constexpr (std::is_convertible_v*, Dune::InverseOperator*> ){ const auto& A = opTraits.getAssembledOpOrThrow(op); const M& mat = A->getmat(); int verbose = config.get("verbose", 0); return std::make_shared>(mat,verbose); } } DUNE_THROW(UnsupportedType, "Unsupported Type in SuperLU (only double and std::complex supported)"); }); } // end namespace DUNE // undefine macros from SuperLU's slu_util.h #undef FIRSTCOL_OF_SNODE #undef NO_MARKER #undef NUM_TEMPV #undef USER_ABORT #undef USER_MALLOC #undef SUPERLU_MALLOC #undef USER_FREE #undef SUPERLU_FREE #undef CHECK_MALLOC #undef SUPERLU_MAX #undef SUPERLU_MIN #undef L_SUB_START #undef L_SUB #undef L_NZ_START #undef L_FST_SUPC #undef U_NZ_START #undef U_SUB #undef TRUE #undef FALSE #undef EMPTY #undef NODROP #undef DROP_BASIC #undef DROP_PROWS #undef DROP_COLUMN #undef DROP_AREA #undef DROP_SECONDARY #undef DROP_DYNAMIC #undef DROP_INTERP #undef MILU_ALPHA #endif // HAVE_SUPERLU #endif // DUNE_SUPERLU_HH