-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.h
116 lines (88 loc) · 2.26 KB
/
Matrix.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#ifndef __MATRIX_LINEAR_SOLVER__
#define __MATRIX_LINEAR_SOLVER__
#include "helpers/Helpers.h"
#include "_hypre_utilities.h"
#include "HYPRE.h"
#include "HYPRE_parcsr_ls.h"
#include "HYPRE_krylov.h"
#include "HYPRE_IJ_mv.h"
#include "HYPRE_parcsr_mv.h"
#include "_hypre_parcsr_ls.h"
#include "_hypre_parcsr_mv.h"
#include <vector>
namespace ryr
{
namespace sparse_matrix
{
class HypreMatrix
{
public:
HypreMatrix(): matrix_ij_(NULL),
lower_row_(0),
upper_row_(0) {
}
virtual ~HypreMatrix() {
if(matrix_ij_)
HYPRE_IJMatrixDestroy(matrix_ij_);
}
void clean() {
if(matrix_ij_)
HYPRE_IJMatrixDestroy(matrix_ij_);
}
void create_matrix(unsigned lower_row, unsigned upper_row,
unsigned lower_column, unsigned upper_column,
int* n_cols_per_row,
const int* matrix_cols, const double* matrix_vals,
MPI_Comm comm);
hypre_ParCSRMatrix* matrix() {
return (hypre_ParCSRMatrix*)matrix_;
}
const unsigned& get_first_row() const {
return lower_row_;
}
const unsigned& get_last_row() const {
return upper_row_;
}
hypre_ParVector* get_par_vector() {
return hypre_ParVectorInRangeOf((hypre_ParCSRMatrix *)matrix_);
}
void
print() {
HYPRE_IJMatrixPrint(matrix_ij_, "matrix.txt");
}
private:
HYPRE_ParCSRMatrix matrix_;
HYPRE_IJMatrix matrix_ij_;
unsigned lower_row_;
unsigned upper_row_;
};
}
namespace linear_solver
{
class LinearSolver
{
public:
LinearSolver(mpi::MPIComm* comm): cg_solver_(NULL), paras_(NULL) {
comm_ = comm;
}
virtual ~LinearSolver() {
clean_memory();
}
void set_matrix(sparse_matrix::HypreMatrix& matrix);
void solve(sparse_matrix::HypreMatrix& matrix,
const std::vector<double>& b, std::vector<double>& x);
void precond_solve(HYPRE_Matrix matrix, HYPRE_Vector b, HYPRE_Vector x);
protected:
void clean_memory();
void solve_block(HYPRE_Matrix matrix, HYPRE_Vector b, HYPRE_Vector x);
void setup_SPAI(sparse_matrix::HypreMatrix& matrix, double threshold, int n_levels);
void setup_CG(sparse_matrix::HypreMatrix& matrix);
private:
mpi::MPIComm* comm_;
HYPRE_Solver cg_solver_;
HYPRE_Solver paras_;
hypre_ParVector *hypre_vector_, *b_vector_;
};
}
}
#endif