如何通过solve()跟踪Eigen对象?

时间:2014-01-19 16:12:21

标签: c++ eigen expression-templates temporaries

此问题与cast from Eigen::CwiseBinaryOp to MatrixXd causes 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的复杂表达式Solve,稍后再调用expr.get()

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

1 个答案:

答案 0 :(得分:1)

为避免无用且昂贵的副本,内部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引用(以避免深层复制)和按值的轻量级表达式存储重对象(以减少临时生命问题)。