0

这个问题与从 Eigen::CwiseBinaryOp 到 MatrixXd 的转换导致 segfault相关。它可能有与前者一样简单的解决方案。

在这个最小的例子中,我定义Holder了 ,它包含一个 Eigen 矩阵,并通过它的get()成员函数返回它。类似地,Decomp是该矩阵的 LDLT 分解的表达式模板,Solve求解 AX=B,得到 X。

#include <Eigen/Dense>
#include <Eigen/Cholesky>

template <class EigenType> class Holder {
public:
typedef EigenType result_type;

private:
result_type in_;

public:
Holder(const EigenType& in) : in_(in) {}
const result_type& get() const { return in_; }
};

template <class Hold> class Decomp {
public:
typedef typename Eigen::LDLT
    <typename Eigen::MatrixBase<typename Hold::result_type>::PlainObject>
        result_type;

private:
Hold mat_;

public:
Decomp(const Hold& mat) : mat_(mat) {}

result_type get() const { return mat_.get().ldlt(); }
};

template <class Derived, class OtherDerived> class Solve {
public:
typedef typename Eigen::internal::solve_retval
    <typename Derived::result_type, typename OtherDerived::result_type>
        result_type;

private:
Derived decomp_;
// typename Derived::result_type decomp_;
OtherDerived mat_;

public:
Solve(const Derived& decomp, const OtherDerived& mat)
    : decomp_(decomp), mat_(mat) {}
//: decomp_(decomp.get()), mat_(mat) {}

result_type get() const { return decomp_.get().solve(mat_.get()); }
// result_type get() const { return decomp_.solve(mat_.get()); }
};

typedef Holder<Eigen::MatrixXd> MatrixHolder;
typedef Decomp<MatrixHolder> MatrixDecomp;
typedef Solve<MatrixDecomp, MatrixHolder> SimpleSolve;

以下测试失败X.get()

#include "Simple.h"
#include <Eigen/Dense>
#include <iostream>

int main(int, char * []) {
MatrixHolder A(Eigen::MatrixXd::Identity(3, 3));
MatrixHolder B(Eigen::MatrixXd::Random(3, 2));
MatrixDecomp ldlt(A);
SimpleSolve X(ldlt, B);
std::cout << X.get() << std::endl;
return 0;
}

但如果您使用头文件中注释掉的行,一切正常。不幸的是,这将分解的评估转移到求解器的构造上,这不适合我的使用。通常,我想构建一个expr涉及 this的复杂表达式Solve,然后再调用expr.get()

我怎么解决这个问题?是否有要遵循的一般规则,以避免进一步的相关问题?

4

1 回答 1

2

为了避免无用和昂贵的副本,内部solve_retval结构通过 const 引用存储分解和右侧。但是,函数中LDLT创建的对象在Decomp::get该函数返回的同时被删除,因此该solve_retval对象引用了死对象。

一种可能的解决方法是在 中添加一个Decomp::result_type对象Solve,然后在 中对其进行初始化Solve::get。此外,为了避免多次深拷贝,我建议对一些属性使用 const 引用,如下所示:

#include <Eigen/Dense>
#include <Eigen/Cholesky>

template <class EigenType> class Holder {
public:
  typedef EigenType result_type;

private:
  result_type in_;

public:
  Holder(const EigenType& in) : in_(in) {}
  const result_type& get() const { return in_; }
};

template <class Hold> class Decomp {
public:
  typedef typename Eigen::LDLT
      <typename Eigen::MatrixBase<typename Hold::result_type>::PlainObject>
          result_type;

private:
  const Hold& mat_;
  mutable result_type result_;
  mutable bool init_;

public:
  Decomp(const Hold& mat) : mat_(mat), init_(false) {}

  const result_type& get() const {
    if(!init_) {
      init_ = true;
      result_.compute(mat_.get());
      return result_;
    }
  }
};

template <class Derived, class OtherDerived> class Solve {
public:
  typedef typename Eigen::internal::solve_retval
      <typename Derived::result_type, typename OtherDerived::result_type>
          result_type;

private:
  const Derived& decomp_;
  const OtherDerived& mat_;

public:
  Solve(const Derived& decomp, const OtherDerived& mat)
      : decomp_(decomp), mat_(mat) {}

  result_type get() const {
    return decomp_.get().solve(mat_.get());
  }
};

一般规则是通过 const 引用存储重对象(以避免深拷贝)和按值存储轻量级表达式(以减少临时生命问题)。

于 2014-01-19T18:22:48.367 回答