Skip to content

Commit 5d62b80

Browse files
authored
fix eigen iterative solver lifetime problem (#91)
Eigen iterative solver stores a reference to input matrix A and expect it to remain valid. Currently we do not make a copy and this results in segmentation fault when A is destroyed before the compute() call. This PR fix this by manually copying A. - Copy input matrix A in factorize(). - Unify input matrix naming
1 parent 550f6a5 commit 5d62b80

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

src/polysolve/linear/EigenSolver.hpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ namespace polysolve::linear
3131
virtual void get_info(json &params) const override;
3232

3333
// Analyze sparsity pattern
34-
virtual void analyze_pattern(const StiffnessMatrix &K, const int precond_num) override;
34+
virtual void analyze_pattern(const StiffnessMatrix &A, const int precond_num) override;
3535

3636
// Factorize system matrix
37-
virtual void factorize(const StiffnessMatrix &K) override;
37+
virtual void factorize(const StiffnessMatrix &A) override;
3838

3939
// Solve the linear system
4040
virtual void solve(const Ref<const VectorXd> b, Ref<VectorXd> x) override;
@@ -52,6 +52,11 @@ namespace polysolve::linear
5252
// Name of the solver
5353
std::string m_Name;
5454

55+
// Copy of the input matrix.
56+
// We do this because Eigen iterative solver take a
57+
// reference and assumes A is always valid.
58+
StiffnessMatrix m_A;
59+
5560
public:
5661
// Name of the solver type (for debugging purposes)
5762
virtual std::string name() const override { return m_Name; }
@@ -67,10 +72,10 @@ namespace polysolve::linear
6772
virtual void get_info(json &params) const override;
6873

6974
// Analyze sparsity pattern
70-
virtual void analyze_pattern(const StiffnessMatrix &K, const int precond_num) override;
75+
virtual void analyze_pattern(const StiffnessMatrix &A, const int precond_num) override;
7176

7277
// Factorize system matrix
73-
virtual void factorize(const StiffnessMatrix &K) override;
78+
virtual void factorize(const StiffnessMatrix &A) override;
7479

7580
// Solve the linear system
7681
virtual void solve(const Ref<const VectorXd> b, Ref<VectorXd> x) override;
@@ -104,10 +109,10 @@ namespace polysolve::linear
104109
virtual void get_info(json &params) const override;
105110

106111
// Factorize system matrix
107-
virtual void factorize(const StiffnessMatrix &K) override;
112+
virtual void factorize(const StiffnessMatrix &A) override;
108113

109114
// Factorize system matrix
110-
virtual void factorize_dense(const Eigen::MatrixXd &K) override;
115+
virtual void factorize_dense(const Eigen::MatrixXd &A) override;
111116

112117
// Solve the linear system
113118
virtual void solve(const Ref<const VectorXd> b, Ref<VectorXd> x) override;

src/polysolve/linear/EigenSolver.tpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ namespace polysolve::linear
100100
template <typename SparseSolver>
101101
void EigenIterative<SparseSolver>::factorize(const StiffnessMatrix &A)
102102
{
103-
m_Solver.factorize(A);
103+
m_A = A;
104+
m_Solver.factorize(m_A);
104105
}
105106

106107
// Solve the linear system

0 commit comments

Comments
 (0)