gpt4 book ai didi

c++ - 使用 Boost.Proto 构建 Eigen 表达式模板

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:31:39 26 4
gpt4 key购买 nike

我想使用 Boost.Proto 将嵌入式领域特定语言转换为一系列使用 Eigen 库实现的矩阵运算。由于效率很重要,我希望 proto 生成 Eigen 表达式模板并避免过早评估。

我实现了一个可以生成矩阵乘法表达式的简单语法。下面的代码在没有警告的情况下编译(在 g++ 4.8.0 和 Intel C++ 2013.3 上,使用 Boost 1.54.0 和 Eigen 3.1.3)并且只要我的表达式只有一个乘法运算就可以工作。一旦我向链中添加更多乘法,它就会崩溃。 Valgrind 告诉我,这是因为 Eigen::GeneralProduct 表达式模板之一在求值完成之前被销毁了。

我不明白为什么会发生这种情况,或者我可以做些什么来防止这种情况发生。感谢所有帮助!

#include <iostream>

#include <boost/fusion/container.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/void.hpp>
#include <boost/proto/proto.hpp>
#include <boost/ref.hpp>
#include <boost/type_traits/remove_const.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <boost/utility.hpp>

#include <Eigen/Dense>

namespace fusion = boost::fusion;
namespace mpl = boost::mpl;
namespace proto = boost::proto;

typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> matrix;

// Placeholders

const proto::terminal<mpl::int_<0> >::type I1 = {{}};
const proto::terminal<mpl::int_<1> >::type I2 = {{}};
const proto::terminal<mpl::int_<2> >::type I3 = {{}};

// Grammar

template<class Rule, class Callable = proto::callable>
struct External :
proto::when<Rule, proto::external_transform> {};

struct matmul_transform : proto::callable {
template<class Sig> struct result;

template<class This, class MatrixExpr1, class MatrixExpr2>
struct result<This(MatrixExpr1, MatrixExpr2)> {
typedef typename Eigen::ProductReturnType<
typename boost::remove_const<typename boost::remove_reference<MatrixExpr1>::type>::type,
typename boost::remove_const<typename boost::remove_reference<MatrixExpr2>::type>::type>::Type
type;
};

template<class MatrixExpr1, class MatrixExpr2>
typename result<matmul_transform(MatrixExpr1, MatrixExpr2)>::type
operator()(const MatrixExpr1 &a, const MatrixExpr2 &b) const {
return a * b;
}
};


struct MatmulGrammar;

struct InputPlaceholder : proto::terminal<proto::_> {};

struct MatrixMultiplication :
proto::multiplies<MatmulGrammar, MatmulGrammar> {};

struct MatmulGrammar : proto::or_<
External<InputPlaceholder>,
External<MatrixMultiplication> > {};

struct matmul_transforms : proto::external_transforms<
proto::when<MatrixMultiplication, matmul_transform(MatmulGrammar(proto::_left), MatmulGrammar(proto::_right))>,
proto::when<InputPlaceholder, proto::functional::at(proto::_data, proto::_value)> > {};

int main() {
matrix mat1(2,2), mat2(2,2), mat3(2,2), result(2,2);

mat1 << 1, 2, 3, 4;
mat2 << 5, 6, 7, 8;
mat3 << 1, 3, 6, 9;

MatmulGrammar mmg;

// THIS WORKS:
result = mmg(I1 * I2,
mpl::void_(),
(proto::data = fusion::make_vector(boost::cref(mat1), boost::cref(mat2), boost::cref(mat3)),
proto::transforms = matmul_transforms()));

std::cout << result << std::endl;

// THIS CRASHES:
result = mmg(I1 * I2 * I3,
mpl::void_(),
(proto::data = fusion::make_vector(boost::cref(mat1), boost::cref(mat2), boost::cref(mat3)),
proto::transforms = matmul_transforms()));

std::cout << result << std::endl;

return 0;
}

最佳答案

这是我尝试将您的方法与评论中链接的解决方案合并的尝试。我从 here 复制了 stored_result_expressiondo_wrap_expressionwrap_expression .我对您的代码或演讲中的代码所做的更改都标有 //CHANGED

#include <iostream>

#include <boost/fusion/container.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/void.hpp>
#include <boost/proto/proto.hpp>
#include <boost/ref.hpp>
#include <boost/type_traits/remove_const.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <boost/utility.hpp>

#include <Eigen/Dense>

namespace fusion = boost::fusion;
namespace mpl = boost::mpl;
namespace proto = boost::proto;

typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> matrix;

// Placeholders

const proto::terminal<mpl::int_<0> >::type I1 = {{}};
const proto::terminal<mpl::int_<1> >::type I2 = {{}};
const proto::terminal<mpl::int_<2> >::type I3 = {{}};

// Grammar

template<class Rule, class Callable = proto::callable>
struct External :
proto::when<Rule, proto::external_transform> {};

struct matmul_transform : proto::callable {
template<class Sig> struct result;

template<class This, class Expr, class MatrixExpr1, class MatrixExpr2>
struct result<This(Expr, MatrixExpr1, MatrixExpr2)> {
typedef typename Eigen::MatrixBase<
typename Eigen::ProductReturnType<
typename boost::remove_const<typename boost::remove_reference<MatrixExpr1>::type>::type,
typename boost::remove_const<typename boost::remove_reference<MatrixExpr2>::type>::type
>::Type
>::PlainObject&
type; //CHANGED - THIS IS THE TYPE THAT IS USED IN THE CODE OF THE TALK
};

template<class Expr, class MatrixExpr1, class MatrixExpr2>
typename result<matmul_transform(Expr, MatrixExpr1, MatrixExpr2)>::type
operator()(Expr& expr, const MatrixExpr1 &a, const MatrixExpr2 &b) const { //CHANGED - ADDED THE expr PARAMETER
expr.value = a*b;
return expr.value;
}
};


struct MatmulGrammar;

struct InputPlaceholder : proto::terminal<proto::_> {};

struct MatrixMultiplication :
proto::multiplies<MatmulGrammar, MatmulGrammar> {};

struct MatmulGrammar : proto::or_<
External<InputPlaceholder>,
External<MatrixMultiplication> > {};

struct matmul_transforms : proto::external_transforms<
proto::when<MatrixMultiplication, matmul_transform(proto::_, MatmulGrammar(proto::_left), MatmulGrammar(proto::_right))>, //CHANGED - ADAPTED TO THE NEW SIGNATURE OF matmul_transform
proto::when<InputPlaceholder, proto::functional::at(proto::_data, proto::_value)> > {};

// THE FOLLOWING CODE BLOCK IS COPIED FROM https://github.com/barche/eigen-proto/blob/master/eigen_calculator_solution.cpp
//----------------------------------------------------------------------------------------------
/// Wraps a given expression, so the value that it represents can be stored inside the expression itself
template<typename ExprT, typename ValueT>
struct stored_result_expression :
proto::extends< ExprT, stored_result_expression<ExprT, ValueT> >
{
EIGEN_MAKE_ALIGNED_OPERATOR_NEW

typedef proto::extends< ExprT, stored_result_expression<ExprT, ValueT> > base_type;

explicit stored_result_expression(ExprT const &expr = ExprT())
: base_type(expr)
{
}

/// Temporary storage for the result of the expression
mutable ValueT value;
};

struct do_wrap_expression : proto::transform< do_wrap_expression >
{
template<typename ExprT, typename StateT, typename DataT>
struct impl : proto::transform_impl<ExprT, StateT, DataT>
{
typedef typename boost::result_of<MatmulGrammar(ExprT, StateT, DataT)>::type result_ref_type; //CHANGED - TO USE YOUR GRAMMAR
typedef typename boost::remove_reference<result_ref_type>::type value_type;
typedef typename boost::remove_const<typename boost::remove_reference<ExprT>::type>::type expr_val_type;
typedef stored_result_expression<expr_val_type, value_type> result_type;

result_type operator()(typename impl::expr_param expr, typename impl::state_param state, typename impl::data_param data)
{
return result_type(expr);
}
};
};

/// Wrap multiplies expressions so they can store a temporary result
struct wrap_expression :
proto::or_
<
proto::terminal<proto::_>,
proto::when
<
proto::multiplies<proto::_, proto::_>,
do_wrap_expression(
proto::functional::make_multiplies
(
wrap_expression(proto::_left), wrap_expression(proto::_right)
),
proto::_state, //CHANGED - THESE EXTRA PARAMETERS ARE NEEDED TO CALCULATE result_ref_type IN do_wrap_expression
proto::_env
)
>,
proto::nary_expr< proto::_, proto::vararg<wrap_expression> >
>
{
};
//--------------------------------------------------------------------------------------------------

int main() {
matrix mat1(2,2), mat2(2,2), mat3(2,2), result(2,2);

mat1 << 1, 1, 0, 1;
mat2 << 1, 1, 0, 1;
mat3 << 1, 1, 0, 1;

MatmulGrammar mmg;
wrap_expression wrap;

//THIS WORKS:
result = mmg( //THIS IS REALLY HORRIBLE, BUT IT WORKS. IT SHOULD PROBABLY BE HIDDEN BEHIND A FUNCTION
wrap(
I1 * I2,
mpl::void_(),
( proto::data = fusion::make_vector(boost::cref(mat1), boost::cref(mat2), boost::cref(mat3)),
proto::transforms = matmul_transforms() )
),
mpl::void_(),
( proto::data = fusion::make_vector(boost::cref(mat1), boost::cref(mat2), boost::cref(mat3)),
proto::transforms = matmul_transforms() )
);

std::cout << result << std::endl;

// THIS DOESN'T CRASH ANYMORE:
result = mmg(
wrap(
I1 * I2 * I3 * I1 * I2 * I3,
mpl::void_(),
( proto::data = fusion::make_vector(boost::cref(mat1), boost::cref(mat2), boost::cref(mat3)),
proto::transforms = matmul_transforms() )
),
mpl::void_(),
( proto::data = fusion::make_vector(boost::cref(mat1), boost::cref(mat2), boost::cref(mat3)),
proto::transforms = matmul_transforms() )
);

std::cout << result << std::endl;

return 0;
}

关于c++ - 使用 Boost.Proto 构建 Eigen 表达式模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17754995/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com