// 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_SUPERMATRIX_HH #define DUNE_ISTL_SUPERMATRIX_HH #if HAVE_SUPERLU #include "bcrsmatrix.hh" #include "bvector.hh" #include #include #include #include #include #include "superlufunctions.hh" namespace Dune { template struct SuperMatrixCreateSparseChooser {}; template struct SuperMatrixPrinter {}; #if __has_include("slu_sdefs.h") template<> struct SuperMatrixCreateSparseChooser { static void create(SuperMatrix *mat, int n, int m, int offset, float *values, int *rowindex, int* colindex, Stype_t stype, Dtype_t dtype, Mtype_t mtype) { sCreate_CompCol_Matrix(mat, n, m, offset, values, rowindex, colindex, stype, dtype, mtype); } }; template<> struct SuperMatrixPrinter { static void print(char* name, SuperMatrix* mat) { sPrint_CompCol_Matrix(name, mat); } }; #endif #if __has_include("slu_ddefs.h") template<> struct SuperMatrixCreateSparseChooser { static void create(SuperMatrix *mat, int n, int m, int offset, double *values, int *rowindex, int* colindex, Stype_t stype, Dtype_t dtype, Mtype_t mtype) { dCreate_CompCol_Matrix(mat, n, m, offset, values, rowindex, colindex, stype, dtype, mtype); } }; template<> struct SuperMatrixPrinter { static void print(char* name, SuperMatrix* mat) { dPrint_CompCol_Matrix(name, mat); } }; #endif #if __has_include("slu_cdefs.h") template<> struct SuperMatrixCreateSparseChooser > { static void create(SuperMatrix *mat, int n, int m, int offset, std::complex *values, int *rowindex, int* colindex, Stype_t stype, Dtype_t dtype, Mtype_t mtype) { cCreate_CompCol_Matrix(mat, n, m, offset, reinterpret_cast< singlecomplex*>(values), rowindex, colindex, stype, dtype, mtype); } }; template<> struct SuperMatrixPrinter > { static void print(char* name, SuperMatrix* mat) { cPrint_CompCol_Matrix(name, mat); } }; #endif #if __has_include("slu_zdefs.h") template<> struct SuperMatrixCreateSparseChooser > { static void create(SuperMatrix *mat, int n, int m, int offset, std::complex *values, int *rowindex, int* colindex, Stype_t stype, Dtype_t dtype, Mtype_t mtype) { zCreate_CompCol_Matrix(mat, n, m, offset, reinterpret_cast(values), rowindex, colindex, stype, dtype, mtype); } }; template<> struct SuperMatrixPrinter > { static void print(char* name, SuperMatrix* mat) { zPrint_CompCol_Matrix(name, mat); } }; #endif template struct BaseGetSuperLUType { static const Dtype_t type; }; template struct GetSuperLUType {}; template const Dtype_t BaseGetSuperLUType::type = std::is_same::value ? SLU_S : ( std::is_same >::value ? SLU_Z : ( std::is_same >::value ? SLU_C : SLU_D )); template<> struct GetSuperLUType : public BaseGetSuperLUType { typedef double float_type; }; template<> struct GetSuperLUType : public BaseGetSuperLUType { typedef float float_type; }; template<> struct GetSuperLUType > : public BaseGetSuperLUType > { typedef double float_type; }; template<> struct GetSuperLUType > : public BaseGetSuperLUType > { typedef float float_type; }; /** * @brief Utility class for converting an ISTL Matrix * into a SuperLU Matrix. */ template struct SuperLUMatrix {}; template struct SuperMatrixInitializer {}; template class SuperLU; template class SeqOverlappingSchwarz; template struct SeqOverlappingSchwarzAssemblerHelper; /** * @brief Converter for BCRSMatrix to SuperLU Matrix. */ template class SuperLUMatrix > : public ISTL::Impl::BCCSMatrix::field_type, int> { template friend class SeqOverlappingSchwarz; friend struct SuperMatrixInitializer >; public: /** @brief The type of the matrix to convert. */ typedef BCRSMatrix Matrix; friend struct SeqOverlappingSchwarzAssemblerHelper, true>; typedef typename Matrix::size_type size_type; /** * @brief Constructor that initializes the data. * @param mat The matrix to convert. */ explicit SuperLUMatrix(const Matrix& mat) : ISTL::Impl::BCCSMatrix, int>(mat) {} SuperLUMatrix() : ISTL::Impl::BCCSMatrix::field_type, int>() {} /** @brief Destructor */ virtual ~SuperLUMatrix() { if (this->N_+this->M_*this->Nnz_ != 0) free(); } /** @brief Cast to a SuperLU Matrix */ operator SuperMatrix&() { return A; } /** @brief Cast to a SuperLU Matrix */ operator const SuperMatrix&() const { return A; } SuperLUMatrix >& operator=(const BCRSMatrix& mat) { if (this->N_ + this->M_ + this->Nnz_!=0) free(); this->N_ = MatrixDimension::rowdim(mat); this->M_ = MatrixDimension::coldim(mat); ISTL::Impl::BCCSMatrixInitializer initializer(*this); copyToBCCSMatrix(initializer, mat); SuperMatrixCreateSparseChooser ::create(&A, this->N_, this->M_, this->colstart[this->N_], this->values,this->rowindex, this->colstart, SLU_NC, static_cast(GetSuperLUType::type), SLU_GE); return *this; } SuperLUMatrix >& operator=(const SuperLUMatrix >& mat) { if (this->N_ + this->M_ + this->Nnz_!=0) free(); this->N_ = MatrixDimension::rowdim(mat); this->M_ = MatrixDimension::coldim(mat); ISTL::Impl::BCCSMatrixInitializer initializer(*this); copyToBCCSMatrix(initializer, mat); SuperMatrixCreateSparseChooser ::create(&A, this->N_, this->M_, this->colstart[this->N_], this->values,this->rowindex, this->colstart, SLU_NC, static_cast(GetSuperLUType::type), SLU_GE); return *this; } /** * @brief Initialize data from a given set of matrix rows and columns * @tparam The type of the row index set. * @param mat the matrix with the values * @param mrs The set of row (and column) indices to represent */ virtual void setMatrix(const Matrix& mat, const std::set& mrs) { if(this->N_+this->M_+this->Nnz_!=0) free(); this->N_=mrs.size()*MatrixDimension::rowdim(*(mat[0].begin())); this->M_=mrs.size()*MatrixDimension::coldim(*(mat[0].begin())); SuperMatrixInitializer initializer(*this); copyToBCCSMatrix(initializer, ISTL::Impl::MatrixRowSubset >(mat,mrs)); } /** @brief Initialize data from given matrix. */ virtual void setMatrix(const Matrix& mat) { this->N_=MatrixDimension::rowdim(mat); this->M_=MatrixDimension::coldim(mat); SuperMatrixInitializer initializer(*this); copyToBCCSMatrix(initializer, mat); } /** @brief free allocated space. */ void free() override { ISTL::Impl::BCCSMatrix::field_type, int>::free(); SUPERLU_FREE(A.Store); } private: SuperMatrix A; }; template class SuperMatrixInitializer > : public ISTL::Impl::BCCSMatrixInitializer, int> { template friend class OverlappingSchwarzInitializer; public: typedef BCRSMatrix Matrix; typedef Dune::SuperLUMatrix SuperLUMatrix; SuperMatrixInitializer(SuperLUMatrix& lum) : ISTL::Impl::BCCSMatrixInitializer, int>(lum) ,slumat(&lum) {} SuperMatrixInitializer() : ISTL::Impl::BCCSMatrixInitializer, int>() {} void createMatrix() const override { ISTL::Impl::BCCSMatrixInitializer, int>::createMatrix(); SuperMatrixCreateSparseChooser ::create(&slumat->A, slumat->N_, slumat->M_, slumat->colstart[this->cols], slumat->values,slumat->rowindex, slumat->colstart, SLU_NC, static_cast(GetSuperLUType::type), SLU_GE); } private: SuperLUMatrix* slumat; }; } #endif // HAVE_SUPERLU #endif