MatrixWrapper.hpp
Go to the documentation of this file.
1#ifndef OPM_MIXED_MATRIX_HEADER_INCLUDED
2#define OPM_MIXED_MATRIX_HEADER_INCLUDED
3
4
6
7namespace Opm
8{
9
17template <class Vector>
19{
20 public:
21
22 // extract block size
23 static constexpr auto block_size = Vector::block_type::dimension;
24
25 virtual void mv(const Vector& x, Vector& y) const;
26 virtual void umv(const Vector& x, Vector& y) const;
27 virtual void usmv(double alpha, const Vector& x, Vector& y) const;
28
33 MixedMatrixWrapper(int nrows, int nnz)
34 {
35 if constexpr(block_size!=3) OPM_THROW(std::invalid_argument, "MixedMatrixWrapper only supports block size == 3! \n");
36
37 nnz_=nnz;
38 M_ = bsr_alloc();
39 bsr_init(M_, nrows, nnz, block_size);
40 }
41
44
45 void update(double const *data);
46
47 int *rowptr(){return M_->rowptr;}
48 int *colidx(){return M_->colidx;}
49
50 private:
51 int nnz_;
52 bsr_matrix *M_;
53};
54
55template <class Vector>
57mv(const Vector& x, Vector& y) const
58{
59 // mixed-precision block spmv (y = M.x)
60 bsr_vmspmv3(M_, &x[0][0], &y[0][0]);
61}
62
63template <class Vector>
65umv(const Vector& x, Vector& y) const
66{
67 // mixed-precision block spmv with update (y += M.x)
68 bsr_vmspumv3(M_, &x[0][0], &y[0][0], 1.0);
69}
70
71template <class Vector>
73usmv(double alpha, const Vector& x, Vector& y) const
74{
75 // scaled mixed-precision block spmv with update (y += alpha * M.x)
76 bsr_vmspumv3(M_, &x[0][0], &y[0][0], alpha);
77}
78
79template <class Vector>
81update(double const *data)
82{
83 // transpose each dense block to make them column-major
84 int const b = block_size;
85 int const bb=b*b;
86 double B[bb];
87 for(int k=0;k<nnz_;k++)
88 {
89 for(int i=0;i<b;i++) for(int j=0;j<b;j++) B[b*j+i] = data[bb*k + b*i + j];
90 for(int i=0;i<bb;i++) M_->dbl[bb*k + i] = B[i];
91 }
92
93 // downcast to single precision
94 bsr_downcast(M_);
95}
96} // namespace Opm
97#endif // OPM_MIXED_MATRIX_HEADER_INCLUDED
void bsr_downcast(bsr_matrix *M)
Make single-precision copy of double-precision values.
void bsr_vmspumv3(bsr_matrix *A, const double *x, double *y, double alpha)
Sparse matrix-vector multiplication in mixed precision.
bsr_matrix * bsr_alloc()
Create empty bsr matrix.
void bsr_vmspmv3(bsr_matrix *A, const double *x, double *y)
Sparse matrix-vector multiplication in mixed precision.
void bsr_free(bsr_matrix *A)
Delete bsr matrix.
void bsr_init(bsr_matrix *A, int nrows, int nnz, int b)
Initialize bsr matrix.
Wraps c-implementation of mixed-precision matrix.
Definition: MatrixWrapper.hpp:19
MixedMatrixWrapper(int nrows, int nnz)
constructor
Definition: MatrixWrapper.hpp:33
~MixedMatrixWrapper()
destructor
Definition: MatrixWrapper.hpp:43
virtual void mv(const Vector &x, Vector &y) const
Definition: MatrixWrapper.hpp:57
int * rowptr()
Definition: MatrixWrapper.hpp:47
void update(double const *data)
Definition: MatrixWrapper.hpp:81
int * colidx()
Definition: MatrixWrapper.hpp:48
virtual void umv(const Vector &x, Vector &y) const
Definition: MatrixWrapper.hpp:65
virtual void usmv(double alpha, const Vector &x, Vector &y) const
Definition: MatrixWrapper.hpp:73
static constexpr auto block_size
Definition: MatrixWrapper.hpp:23
Definition: blackoilbioeffectsmodules.hh:45
Mixed-precision bsr matrix.
Definition: bsr.h:12
int * colidx
Definition: bsr.h:25
int * rowptr
Definition: bsr.h:23