| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception |
| 3 |
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 4 |
// vi: set et ts=4 sw=2 sts=2: |
| 5 |
#ifndef DUNE_ISTL_SUPERMATRIX_HH |
| 6 |
#define DUNE_ISTL_SUPERMATRIX_HH |
| 7 |
|
| 8 |
#if HAVE_SUPERLU |
| 9 |
|
| 10 |
#include "bcrsmatrix.hh" |
| 11 |
#include "bvector.hh" |
| 12 |
#include <dune/common/fmatrix.hh> |
| 13 |
#include <dune/common/fvector.hh> |
| 14 |
#include <dune/common/typetraits.hh> |
| 15 |
#include <limits> |
| 16 |
|
| 17 |
#include <dune/istl/bccsmatrixinitializer.hh> |
| 18 |
|
| 19 |
#include "superlufunctions.hh" |
| 20 |
|
| 21 |
namespace Dune |
| 22 |
{ |
| 23 |
|
| 24 |
template<class T> |
| 25 |
struct SuperMatrixCreateSparseChooser |
| 26 |
{}; |
| 27 |
|
| 28 |
template<class T> |
| 29 |
struct SuperMatrixPrinter |
| 30 |
{}; |
| 31 |
|
| 32 |
#if __has_include("slu_sdefs.h") |
| 33 |
template<> |
| 34 |
struct SuperMatrixCreateSparseChooser<float> |
| 35 |
{ |
| 36 |
static void create(SuperMatrix *mat, int n, int m, int offset, |
| 37 |
float *values, int *rowindex, int* colindex, |
| 38 |
Stype_t stype, Dtype_t dtype, Mtype_t mtype) |
| 39 |
{ |
| 40 |
sCreate_CompCol_Matrix(mat, n, m, offset, values, rowindex, colindex, |
| 41 |
stype, dtype, mtype); |
| 42 |
} |
| 43 |
}; |
| 44 |
|
| 45 |
template<> |
| 46 |
struct SuperMatrixPrinter<float> |
| 47 |
{ |
| 48 |
static void print(char* name, SuperMatrix* mat) |
| 49 |
{ |
| 50 |
sPrint_CompCol_Matrix(name, mat); |
| 51 |
} |
| 52 |
}; |
| 53 |
#endif |
| 54 |
|
| 55 |
#if __has_include("slu_ddefs.h") |
| 56 |
template<> |
| 57 |
struct SuperMatrixCreateSparseChooser<double> |
| 58 |
{ |
| 59 |
static void create(SuperMatrix *mat, int n, int m, int offset, |
| 60 |
double *values, int *rowindex, int* colindex, |
| 61 |
Stype_t stype, Dtype_t dtype, Mtype_t mtype) |
| 62 |
{ |
| 63 |
dCreate_CompCol_Matrix(mat, n, m, offset, values, rowindex, colindex, |
| 64 |
stype, dtype, mtype); |
| 65 |
} |
| 66 |
}; |
| 67 |
|
| 68 |
template<> |
| 69 |
struct SuperMatrixPrinter<double> |
| 70 |
{ |
| 71 |
static void print(char* name, SuperMatrix* mat) |
| 72 |
{ |
| 73 |
dPrint_CompCol_Matrix(name, mat); |
| 74 |
} |
| 75 |
}; |
| 76 |
#endif |
| 77 |
|
| 78 |
#if __has_include("slu_cdefs.h") |
| 79 |
template<> |
| 80 |
struct SuperMatrixCreateSparseChooser<std::complex<float> > |
| 81 |
{ |
| 82 |
static void create(SuperMatrix *mat, int n, int m, int offset, |
| 83 |
std::complex<float> *values, int *rowindex, int* colindex, |
| 84 |
Stype_t stype, Dtype_t dtype, Mtype_t mtype) |
| 85 |
{ |
| 86 |
cCreate_CompCol_Matrix(mat, n, m, offset, reinterpret_cast< singlecomplex*>(values), |
| 87 |
rowindex, colindex, stype, dtype, mtype); |
| 88 |
} |
| 89 |
}; |
| 90 |
|
| 91 |
template<> |
| 92 |
struct SuperMatrixPrinter<std::complex<float> > |
| 93 |
{ |
| 94 |
static void print(char* name, SuperMatrix* mat) |
| 95 |
{ |
| 96 |
cPrint_CompCol_Matrix(name, mat); |
| 97 |
} |
| 98 |
}; |
| 99 |
#endif |
| 100 |
|
| 101 |
#if __has_include("slu_zdefs.h") |
| 102 |
template<> |
| 103 |
struct SuperMatrixCreateSparseChooser<std::complex<double> > |
| 104 |
{ |
| 105 |
static void create(SuperMatrix *mat, int n, int m, int offset, |
| 106 |
std::complex<double> *values, int *rowindex, int* colindex, |
| 107 |
Stype_t stype, Dtype_t dtype, Mtype_t mtype) |
| 108 |
{ |
| 109 |
zCreate_CompCol_Matrix(mat, n, m, offset, reinterpret_cast<doublecomplex*>(values), |
| 110 |
rowindex, colindex, stype, dtype, mtype); |
| 111 |
} |
| 112 |
}; |
| 113 |
|
| 114 |
template<> |
| 115 |
struct SuperMatrixPrinter<std::complex<double> > |
| 116 |
{ |
| 117 |
static void print(char* name, SuperMatrix* mat) |
| 118 |
{ |
| 119 |
zPrint_CompCol_Matrix(name, mat); |
| 120 |
} |
| 121 |
}; |
| 122 |
#endif |
| 123 |
|
| 124 |
template<class T> |
| 125 |
struct BaseGetSuperLUType |
| 126 |
{ |
| 127 |
static const Dtype_t type; |
| 128 |
}; |
| 129 |
|
| 130 |
template<class T> |
| 131 |
struct GetSuperLUType |
| 132 |
{}; |
| 133 |
|
| 134 |
template<class T> |
| 135 |
const Dtype_t BaseGetSuperLUType<T>::type = |
| 136 |
std::is_same<T,float>::value ? SLU_S : |
| 137 |
( std::is_same<T,std::complex<double> >::value ? SLU_Z : |
| 138 |
( std::is_same<T,std::complex<float> >::value ? SLU_C : SLU_D )); |
| 139 |
|
| 140 |
template<> |
| 141 |
struct GetSuperLUType<double> |
| 142 |
: public BaseGetSuperLUType<double> |
| 143 |
{ |
| 144 |
typedef double float_type; |
| 145 |
}; |
| 146 |
|
| 147 |
template<> |
| 148 |
struct GetSuperLUType<float> |
| 149 |
: public BaseGetSuperLUType<float> |
| 150 |
{ |
| 151 |
typedef float float_type; |
| 152 |
}; |
| 153 |
|
| 154 |
template<> |
| 155 |
struct GetSuperLUType<std::complex<double> > |
| 156 |
: public BaseGetSuperLUType<std::complex<double> > |
| 157 |
{ |
| 158 |
typedef double float_type; |
| 159 |
}; |
| 160 |
|
| 161 |
template<> |
| 162 |
struct GetSuperLUType<std::complex<float> > |
| 163 |
: public BaseGetSuperLUType<std::complex<float> > |
| 164 |
{ |
| 165 |
typedef float float_type; |
| 166 |
|
| 167 |
}; |
| 168 |
|
| 169 |
/** |
| 170 |
* @brief Utility class for converting an ISTL Matrix |
| 171 |
* into a SuperLU Matrix. |
| 172 |
*/ |
| 173 |
template<class M> |
| 174 |
struct SuperLUMatrix |
| 175 |
{}; |
| 176 |
|
| 177 |
template<class M> |
| 178 |
struct SuperMatrixInitializer |
| 179 |
{}; |
| 180 |
|
| 181 |
template<class T> |
| 182 |
class SuperLU; |
| 183 |
|
| 184 |
template<class M, class X, class TM, class TD, class T1> |
| 185 |
class SeqOverlappingSchwarz; |
| 186 |
|
| 187 |
template<class T, bool flag> |
| 188 |
struct SeqOverlappingSchwarzAssemblerHelper; |
| 189 |
|
| 190 |
/** |
| 191 |
* @brief Converter for BCRSMatrix to SuperLU Matrix. |
| 192 |
*/ |
| 193 |
template<class B, class TA> |
| 194 |
class SuperLUMatrix<BCRSMatrix<B,TA> > |
| 195 |
: public ISTL::Impl::BCCSMatrix<typename BCRSMatrix<B,TA>::field_type, int> |
| 196 |
{ |
| 197 |
template<class M, class X, class TM, class TD, class T1> |
| 198 |
friend class SeqOverlappingSchwarz; |
| 199 |
friend struct SuperMatrixInitializer<BCRSMatrix<B,TA> >; |
| 200 |
public: |
| 201 |
/** @brief The type of the matrix to convert. */ |
| 202 |
typedef BCRSMatrix<B,TA> Matrix; |
| 203 |
|
| 204 |
friend struct SeqOverlappingSchwarzAssemblerHelper<SuperLU<Matrix>, true>; |
| 205 |
|
| 206 |
typedef typename Matrix::size_type size_type; |
| 207 |
|
| 208 |
/** |
| 209 |
* @brief Constructor that initializes the data. |
| 210 |
* @param mat The matrix to convert. |
| 211 |
*/ |
| 212 |
explicit SuperLUMatrix(const Matrix& mat) : ISTL::Impl::BCCSMatrix<BCRSMatrix<B,TA>, int>(mat) |
| 213 |
{} |
| 214 |
|
| 215 |
SuperLUMatrix() : ISTL::Impl::BCCSMatrix<typename BCRSMatrix<B,TA>::field_type, int>() |
| 216 |
{} |
| 217 |
|
| 218 |
/** @brief Destructor */ |
| 219 |
virtual ~SuperLUMatrix() |
| 220 |
{ |
| 221 |
if (this->N_+this->M_*this->Nnz_ != 0) |
| 222 |
free(); |
| 223 |
} |
| 224 |
|
| 225 |
/** @brief Cast to a SuperLU Matrix */ |
| 226 |
operator SuperMatrix&() |
| 227 |
{ |
| 228 |
return A; |
| 229 |
} |
| 230 |
|
| 231 |
/** @brief Cast to a SuperLU Matrix */ |
| 232 |
operator const SuperMatrix&() const |
| 233 |
{ |
| 234 |
return A; |
| 235 |
} |
| 236 |
|
| 237 |
SuperLUMatrix<BCRSMatrix<B,TA> >& operator=(const BCRSMatrix<B,TA>& mat) |
| 238 |
{ |
| 239 |
if (this->N_ + this->M_ + this->Nnz_!=0) |
| 240 |
free(); |
| 241 |
|
| 242 |
this->N_ = MatrixDimension<Matrix>::rowdim(mat); |
| 243 |
this->M_ = MatrixDimension<Matrix>::coldim(mat); |
| 244 |
ISTL::Impl::BCCSMatrixInitializer<Matrix, int> initializer(*this); |
| 245 |
|
| 246 |
copyToBCCSMatrix(initializer, mat); |
| 247 |
|
| 248 |
SuperMatrixCreateSparseChooser<typename Matrix::field_type> |
| 249 |
::create(&A, this->N_, this->M_, this->colstart[this->N_], |
| 250 |
this->values,this->rowindex, this->colstart, SLU_NC, |
| 251 |
static_cast<Dtype_t>(GetSuperLUType<typename Matrix::field_type>::type), SLU_GE); |
| 252 |
return *this; |
| 253 |
} |
| 254 |
|
| 255 |
SuperLUMatrix<BCRSMatrix<B,TA> >& operator=(const SuperLUMatrix <BCRSMatrix<B,TA> >& mat) |
| 256 |
{ |
| 257 |
if (this->N_ + this->M_ + this->Nnz_!=0) |
| 258 |
free(); |
| 259 |
|
| 260 |
this->N_ = MatrixDimension<Matrix>::rowdim(mat); |
| 261 |
this->M_ = MatrixDimension<Matrix>::coldim(mat); |
| 262 |
ISTL::Impl::BCCSMatrixInitializer<Matrix, int> initializer(*this); |
| 263 |
|
| 264 |
copyToBCCSMatrix(initializer, mat); |
| 265 |
|
| 266 |
SuperMatrixCreateSparseChooser<B> |
| 267 |
::create(&A, this->N_, this->M_, this->colstart[this->N_], |
| 268 |
this->values,this->rowindex, this->colstart, SLU_NC, |
| 269 |
static_cast<Dtype_t>(GetSuperLUType<B>::type), SLU_GE); |
| 270 |
return *this; |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* @brief Initialize data from a given set of matrix rows and columns |
| 275 |
* @tparam The type of the row index set. |
| 276 |
* @param mat the matrix with the values |
| 277 |
* @param mrs The set of row (and column) indices to represent |
| 278 |
*/ |
| 279 |
virtual void setMatrix(const Matrix& mat, const std::set<typename Matrix::size_type>& mrs) |
| 280 |
{ |
| 281 |
if(this->N_+this->M_+this->Nnz_!=0) |
| 282 |
free(); |
| 283 |
this->N_=mrs.size()*MatrixDimension<typename Matrix::block_type>::rowdim(*(mat[0].begin())); |
| 284 |
this->M_=mrs.size()*MatrixDimension<typename Matrix::block_type>::coldim(*(mat[0].begin())); |
| 285 |
SuperMatrixInitializer<Matrix> initializer(*this); |
| 286 |
|
| 287 |
copyToBCCSMatrix(initializer, ISTL::Impl::MatrixRowSubset<Matrix,std::set<typename Matrix::size_type> >(mat,mrs)); |
| 288 |
} |
| 289 |
|
| 290 |
/** @brief Initialize data from given matrix. */ |
| 291 |
virtual void setMatrix(const Matrix& mat) |
| 292 |
{ |
| 293 |
this->N_=MatrixDimension<Matrix>::rowdim(mat); |
| 294 |
this->M_=MatrixDimension<Matrix>::coldim(mat); |
| 295 |
SuperMatrixInitializer<Matrix> initializer(*this); |
| 296 |
|
| 297 |
copyToBCCSMatrix(initializer, mat); |
| 298 |
} |
| 299 |
|
| 300 |
/** @brief free allocated space. */ |
| 301 |
void free() override |
| 302 |
{ |
| 303 |
ISTL::Impl::BCCSMatrix<typename BCRSMatrix<B,TA>::field_type, int>::free(); |
| 304 |
SUPERLU_FREE(A.Store); |
| 305 |
} |
| 306 |
private: |
| 307 |
SuperMatrix A; |
| 308 |
}; |
| 309 |
|
| 310 |
template<class B, class A> |
| 311 |
class SuperMatrixInitializer<BCRSMatrix<B,A> > |
| 312 |
: public ISTL::Impl::BCCSMatrixInitializer<BCRSMatrix<B,A>, int> |
| 313 |
{ |
| 314 |
template<class I, class S, class D> |
| 315 |
friend class OverlappingSchwarzInitializer; |
| 316 |
public: |
| 317 |
typedef BCRSMatrix<B,A> Matrix; |
| 318 |
typedef Dune::SuperLUMatrix<Matrix> SuperLUMatrix; |
| 319 |
|
| 320 |
SuperMatrixInitializer(SuperLUMatrix& lum) : ISTL::Impl::BCCSMatrixInitializer<BCRSMatrix<B,A>, int>(lum) |
| 321 |
,slumat(&lum) |
| 322 |
{} |
| 323 |
|
| 324 |
SuperMatrixInitializer() : ISTL::Impl::BCCSMatrixInitializer<BCRSMatrix<B,A>, int>() |
| 325 |
{} |
| 326 |
|
| 327 |
void createMatrix() const override |
| 328 |
{ |
| 329 |
ISTL::Impl::BCCSMatrixInitializer<BCRSMatrix<B,A>, int>::createMatrix(); |
| 330 |
SuperMatrixCreateSparseChooser<typename Matrix::field_type> |
| 331 |
::create(&slumat->A, slumat->N_, slumat->M_, slumat->colstart[this->cols], |
| 332 |
slumat->values,slumat->rowindex, slumat->colstart, SLU_NC, |
| 333 |
static_cast<Dtype_t>(GetSuperLUType<typename Matrix::field_type>::type), SLU_GE); |
| 334 |
} |
| 335 |
private: |
| 336 |
SuperLUMatrix* slumat; |
| 337 |
}; |
| 338 |
} |
| 339 |
#endif // HAVE_SUPERLU |
| 340 |
#endif |