简体   繁体   中英

Boost.Proto : How to make an expression terminal of a primitive array instead of std::vector?

Now I am trying to make yet another mini-EDSL (embedded domain-specific language) for vector expressions. Actually Boost.Proto users' guide already provided such an EDSL example, " Lazy Vector ", where vector expressions are made of std::vector<T> . But I have to make those expressions of primitive arrays instead. Because primitive array operation is still the heart of several scientific simulation programs.

Thus I added an array wrapper class, ArrayWrapper to that "Lazy Vector" code and replaced std::vector with ArrayWrapper . This modified source code was successfully compiled and linked. But when I ran it, core was dumped.

Here is the modified version of source code:

//  The original version of this file is :
//  "Lazy Vector: Controlling Operator Overloads"
//  in Boost.Proto users' guide.
//  Copyright 2008 Eric Niebler. Distributed under the Boost
//  Software License, Version 1.0.
//
//  It was modified to try protofying a primitive array
//  on May 19 2015.

    #include <vector>
    #include <iostream>
    #include <boost/mpl/int.hpp>
    #include <boost/proto/core.hpp>
    #include <boost/proto/context.hpp>
    namespace mpl = boost::mpl;
    namespace proto = boost::proto;
    using proto::_;


    template <typename T>
    class ArrayWrapper {
    private:
        T* data;
        size_t size_;

    public:
        typedef T value_type;

        explicit ArrayWrapper(std::size_t size = 0, T const & value = T() ):
            data( new T[size]), size_(size) {
            for (std::size_t i = 0; i < size_; i++) data[i] = value;

        }       

        ~ArrayWrapper() {
            std::cerr << "Now destructing an ArrayWrapper" << std::endl;
            delete [] data;
        }

        std::size_t size() { return size_; }

        T& operator[](std::size_t i) { return data[i]; }
        T operator[](std::size_t i) const { return data[i]; }
    };


    template<typename Expr>
    struct lazy_vector_expr;

    // This grammar describes which lazy vector expressions
    // are allowed; namely, vector terminals and addition
    // and subtraction of lazy vector expressions.
    struct LazyVectorGrammar
      : proto::or_<
            proto::terminal< ArrayWrapper<_> >
          , proto::plus< LazyVectorGrammar, LazyVectorGrammar >
          , proto::minus< LazyVectorGrammar, LazyVectorGrammar >
        >
    {};

    // Tell proto that in the lazy_vector_domain, all
    // expressions should be wrapped in laxy_vector_expr<>
    // and must conform to the lazy vector grammar.
    struct lazy_vector_domain
      : proto::domain<proto::generator<lazy_vector_expr>, LazyVectorGrammar>
    {};

    // Here is an evaluation context that indexes into a lazy vector
    // expression, and combines the result.
    template<typename Size = std::size_t>
    struct lazy_subscript_context
    {
        lazy_subscript_context(Size subscript)
          : subscript_(subscript)
        {}

        // Use default_eval for all the operations ...
        template<typename Expr, typename Tag = typename Expr::proto_tag>
        struct eval
          : proto::default_eval<Expr, lazy_subscript_context>
        {};

        // ... except for terminals, which we index with our subscript
        template<typename Expr>
        struct eval<Expr, proto::tag::terminal>
        {
            typedef typename proto::result_of::value<Expr>::type::value_type result_type;

            result_type operator ()( Expr const & expr, lazy_subscript_context & ctx ) const
            {
                return proto::value( expr )[ ctx.subscript_ ];
            }
        };

        Size subscript_;
    };

    // Here is the domain-specific expression wrapper, which overrides
    // operator [] to evaluate the expression using the lazy_subscript_context.
    template<typename Expr>
    struct lazy_vector_expr
      : proto::extends<Expr, lazy_vector_expr<Expr>, lazy_vector_domain>
    {
        lazy_vector_expr( Expr const & expr = Expr() )
          : lazy_vector_expr::proto_extends( expr )
        {}

        // Use the lazy_subscript_context<> to implement subscripting
        // of a lazy vector expression tree.
        template< typename Size >
        typename proto::result_of::eval< Expr, lazy_subscript_context<Size> >::type
        operator []( Size subscript ) const
        {
            lazy_subscript_context<Size> ctx(subscript);
            return proto::eval(*this, ctx);
        }
    };

    // Here is our lazy_vector terminal, implemented in terms of lazy_vector_expr
    template< typename T >
    struct lazy_vector
      : lazy_vector_expr< typename proto::terminal< ArrayWrapper<T> >::type >
    {
        typedef typename proto::terminal< ArrayWrapper<T> >::type expr_type;

        lazy_vector( std::size_t size = 0, T const & value = T() )
          : lazy_vector_expr<expr_type>( expr_type::make( ArrayWrapper<T>(size, value) ) )
        {}

        // Here we define a += operator for lazy vector terminals that
        // takes a lazy vector expression and indexes it. expr[i] here
        // uses lazy_subscript_context<> under the covers.
        template< typename Expr >
        lazy_vector & operator += (Expr const & expr)
        {
            std::size_t size = proto::value(*this).size();
            for(std::size_t i = 0; i < size; ++i)
            {
                proto::value(*this)[i] += expr[i];
            }
            return *this;
        }
    };

    int main()
    {
        // lazy_vectors with 4 elements each.
        lazy_vector< double > v1( 4, 1.0 ), v2( 4, 2.0 ), v3( 4, 3.0 );

        // Add two vectors lazily and get the 2nd element.
        double d1 = ( v2 + v3 )[ 2 ];   // Look ma, no temporaries!
        std::cout << d1 << std::endl;

        // Subtract two vectors and add the result to a third vector.
        v1 += v2 - v3;                  // Still no temporaries!
        std::cout << '{' << v1[0] << ',' << v1[1]
                  << ',' << v1[2] << ',' << v1[3] << '}' << std::endl;

        // This expression is disallowed because it does not conform
        // to the LazyVectorGrammar
        //(v2 + v3) += v1;

        return 0;
    }

I suppose my array wrapper class has all the necessary member functions which the rest of the "Lazy vector" program needs. And I think that the interface of those member functions are the same as that of the std::vector member functions which the original "Lazy Vector" program uses.

Probably I miss some important points. But how to solve this? (How should I make proto::terminal<T> objects with primitive arrays?) I'd be very grateful if you would give me advice or hints.

Finally I find a way to make an expression terminal of a primitive array to make a minimal EDSL for vector algebra. It can suppress superfluous copy of temporary objects when initializing terminal objects of expression templates. The key to the elimination of object copy is to put a primitive array in Vector class, ' , to define a trait that returns true for this Vector class, and to use BOOST_PROTO_DEFINE_OPERATORS() .

Here's the source code :

#include <iostream>
#include <boost/proto/proto.hpp>

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

// This grammar describes which vector expressions
// are allowed; namely, vector terminals and addition
// and subtraction of vector expressions.
struct VecGrammar : proto::or_<
    proto::terminal< proto::_ >,
    proto::plus< VecGrammar, VecGrammar>,
    proto::minus< VecGrammar, VecGrammar>
> {};


// The above grammar is associated with this domain.
template<typename Expr> struct VecExpr;
struct VecDomain
    : proto::domain<proto::generator<VecExpr>, VecGrammar> {};


//
// Context for evaluating an element of matrix expressions
//
struct SubscriptCntxt
    : proto::callable_context<const SubscriptCntxt> {
        typedef double result_type;

        int index;
        SubscriptCntxt(int index_) :  index(index_) {}

        // matrix element
        template<typename Vector>
        double operator()(proto::tag::terminal, const Vector& vec) const {
            return vec[index];
        }

        // addition of vector expression terms
        template<typename E1, typename E2>
        double operator()(proto::tag::plus, const E1& e1, const E2& e2) const {
            return proto::eval(e1, *this) + proto::eval(e2, *this);
        }

        // substraction of vector expression terms
        template<typename E1, typename E2>
        double operator()(proto::tag::minus, const E1& e1, const E2& e2) const {
            return proto::eval(e1, *this) - proto::eval(e2, *this);
        }
};


//
// Vector Expression Templates
//
template<typename Expr>
struct VecExpr
    : proto::extends<Expr, VecExpr<Expr>, VecDomain> {
        explicit VecExpr(const Expr& e)
            : proto::extends<Expr, VecExpr<Expr>, VecDomain>(e) {
        }

        // Use a SubscriptCntxt instance to implement subscripting
        // of a vector expression tree.
        typename proto::result_of::eval< Expr, SubscriptCntxt>::type
        operator [](int i) const {
            const SubscriptCntxt ctx(i);
            return proto::eval(*this, ctx);
        }
};

//
// Matrix data are stored in an heap array.
//
class Vector {
    private:
        int sz;
        double* data;

public:
    explicit Vector(int sz_ = 1, double iniVal = 0.0) :
        sz( sz_), data( new double[sz] ) {
        for (int i = 0; i < sz; i++) data[i] = iniVal;
        std::cout << "Created" << std::endl;
    }
    Vector(const Vector& vec) :
        sz( vec.sz), data( new double[sz] ) {
        for (int i = 0; i < sz; i++) data[i] = vec.data[i];
        std::cout << "Copied" << std::endl;
    }

    ~Vector() {
        delete [] data;
        std::cout << "Deleted" << std::endl;
    }

    // accesing to a vector element
    double& operator[](int i) { return data[i]; }
    const double& operator[](int i) const { return data[i]; }

    // assigning the lhs of a vector expression into this matrix
    template<typename Expr>
    Vector& operator=( const Expr& expr ) {
        for(int i=0; i < sz; ++i) {
                // evaluating the i'th element of a matrix expression
                const SubscriptCntxt ctx(i);
                data[i] = proto::eval(proto::as_expr<VecDomain>(expr), ctx);
        }
        return *this;
    }

    // assigning and adding the lhs of a vector expression into this matrix
    template<typename Expr>
    Vector& operator+=( const Expr& expr ) {
        for(int i=0; i < sz; ++i) {
                // evaluating the (i,j) element of a matrix expression
                const SubscriptCntxt ctx(i);
                data[i] += proto::eval(proto::as_expr<VecDomain>(expr), ctx);
        }
        return *this;
    }
};


// Define a trait for detecting vector terminals, to be used
// by the BOOST_PROTO_DEFINE_OPERATORS macro below.
template<typename> struct IsVector : mpl::false_ {};
template<> struct IsVector<Vector> : mpl::true_  {};



namespace VectorOps {
    // This defines all the overloads to make expressions involving
    // Vector objects to build expression templates.
    BOOST_PROTO_DEFINE_OPERATORS(IsVector, VecDomain)
}

int main()
{
    using namespace VectorOps;

    // lazy_vectors with 4 elements each.
    Vector v1( 4, 1.0 ), v2( 4, 2.0 ), v3( 4, 3.0 );

    // Add two vectors lazily and get the 2nd element.
    double d1 = ( v2 + v3 )[ 2 ];   // Look ma, no temporaries!
    std::cout << d1 << std::endl;

    // Subtract two vectors and add the result to a third vector.
    v1 += v2 - v3;                  // Still no temporaries!
    std::cout << '{' << v1[0] << ',' << v1[1]
              << ',' << v1[2] << ',' << v1[3] << '}' << std::endl;

    // This expression is disallowed because it does not conform
    // to the LazyVectorGrammar
    //(v2 + v3) += v1;

    return 0;
}

I confirmed this code worked and the output was virtually same to that of the "Lazy Vector" example in Boost.Proto users' guide.

Although I am still not sure how things are going under the hood of Boost.Proto, it is quite fun to prototype an EDSL with it.

I hope that I might have partially resolved the problem in my question. After adding a copy constructor to ArrayWrapper class, the modified version of the "Lazy Vector" example in Boost.Proto users' guide works without any error.

Here's the source code :

    //  The original version of this file is :
    //  "Lazy Vector: Controlling Operator Overloads"
    //  in Boost.Proto users' guide.
    //  Copyright 2008 Eric Niebler. Distributed under the Boost
    //  Software License, Version 1.0.
    //
    //  It was modified to try protofying a primitive array
    //  on May 20 2015.

    #include <vector>
    #include <iostream>
    #include <boost/mpl/int.hpp>
    #include <boost/proto/core.hpp>
    #include <boost/proto/context.hpp>
    namespace mpl = boost::mpl;
    namespace proto = boost::proto;
    using proto::_;

    template <typename T>
    class ArrayWrapper {
    private:
        T* data;
        size_t size_;

    public:
        typedef T value_type;

        explicit ArrayWrapper(std::size_t size = 0, T const & value = T() ):
            data( new T[size]), size_(size) {
            for (std::size_t i = 0; i < size_; i++) data[i] = value;

        }
        ArrayWrapper(const ArrayWrapper<T>& wrapper):
            data( new T[ wrapper.size_] ), size_(wrapper.size_) {
            for (std::size_t i = 0; i < size_; i++) data[i] = wrapper.data[i];
        }


        ~ArrayWrapper() {
            std::cerr << "Now destructing an ArrayWrapper" << std::endl;
            delete [] data;
        }

        std::size_t size() {
            return size_;
        }

        T& operator[](std::size_t i) { return data[i]; }
        T operator[](std::size_t i) const { return data[i]; }
    };



    template<typename Expr>
    struct lazy_vector_expr;

    // This grammar describes which lazy vector expressions
    // are allowed; namely, vector terminals and addition
    // and subtraction of lazy vector expressions.
    struct LazyVectorGrammar
      : proto::or_<
            proto::terminal< ArrayWrapper<_> >
          , proto::plus< LazyVectorGrammar, LazyVectorGrammar >
          , proto::minus< LazyVectorGrammar, LazyVectorGrammar >
        >
    {};

    // Tell proto that in the lazy_vector_domain, all
    // expressions should be wrapped in laxy_vector_expr<>
    // and must conform to the lazy vector grammar.
    struct lazy_vector_domain
      : proto::domain<proto::generator<lazy_vector_expr>, LazyVectorGrammar>
    {};

    // Here is an evaluation context that indexes into a lazy vector
    // expression, and combines the result.
    template<typename Size = std::size_t>
    struct lazy_subscript_context
    {
        lazy_subscript_context(Size subscript)
          : subscript_(subscript)
        {}

        // Use default_eval for all the operations ...
        template<typename Expr, typename Tag = typename Expr::proto_tag>
        struct eval
          : proto::default_eval<Expr, lazy_subscript_context>
        {};

        // ... except for terminals, which we index with our subscript
        template<typename Expr>
        struct eval<Expr, proto::tag::terminal>
        {
            typedef typename proto::result_of::value<Expr>::type::value_type result_type;

            result_type operator ()( Expr const & expr, lazy_subscript_context & ctx ) const
            {
                return proto::value( expr )[ ctx.subscript_ ];
            }
        };

        Size subscript_;
    };

    // Here is the domain-specific expression wrapper, which overrides
    // operator [] to evaluate the expression using the lazy_subscript_context.
    template<typename Expr>
    struct lazy_vector_expr
      : proto::extends<Expr, lazy_vector_expr<Expr>, lazy_vector_domain>
    {
        lazy_vector_expr( Expr const & expr = Expr() )
          : lazy_vector_expr::proto_extends( expr )
        {}

        // Use the lazy_subscript_context<> to implement subscripting
        // of a lazy vector expression tree.
        template< typename Size >
        typename proto::result_of::eval< Expr, lazy_subscript_context<Size> >::type
        operator []( Size subscript ) const
        {
            lazy_subscript_context<Size> ctx(subscript);
            return proto::eval(*this, ctx);
        }
    };

    // Here is our lazy_vector terminal, implemented in terms of lazy_vector_expr
    template< typename T >
    struct lazy_vector
      : lazy_vector_expr< typename proto::terminal< ArrayWrapper<T> >::type >
    {
        typedef typename proto::terminal< ArrayWrapper<T> >::type expr_type;

        lazy_vector( std::size_t size = 0, T const & value = T() )
          : lazy_vector_expr<expr_type>( expr_type::make( ArrayWrapper<T>(size, value) ) )
        {}

        // Here we define a += operator for lazy vector terminals that
        // takes a lazy vector expression and indexes it. expr[i] here
        // uses lazy_subscript_context<> under the covers.
        template< typename Expr >
        lazy_vector & operator += (Expr const & expr)
        {
            std::size_t size = proto::value(*this).size();
            for(std::size_t i = 0; i < size; ++i)
            {
                proto::value(*this)[i] += expr[i];
            }
            return *this;
        }
    };

    int main()
    {
        // lazy_vectors with 4 elements each.
        lazy_vector< double > v1( 4, 1.0 ), v2( 4, 2.0 ), v3( 4, 3.0 );

        // Add two vectors lazily and get the 2nd element.
        double d1 = ( v2 + v3 )[ 2 ];   // Look ma, no temporaries!
        std::cout << d1 << std::endl;

        // Subtract two vectors and add the result to a third vector.
        v1 += v2 - v3;                  // Still no temporaries!
        std::cout << '{' << v1[0] << ',' << v1[1]
                  << ',' << v1[2] << ',' << v1[3] << '}' << std::endl;

        // This expression is disallowed because it does not conform
        // to the LazyVectorGrammar
        //(v2 + v3) += v1;

        return 0;
    }

But I am not sure why the default copy constructor of ArrayWrapper had caused the core dump before I explicitly defined the copy constructor. Maybe the copy constructor is invoked by proto::expr< proto::tag::terminal, proto::term< ArrayWrapper<T>>> class, when expr_type::make( ArrayWrapper<T>(size, value) ) ) in lazy_vector class constructor initializes a data member of expr_type class (see the synopsis of proto::expr ). As you see in the definition of lazy_vector class, expr_type is defined as proto::expr< proto::tag::terminal, proto::term< ArrayWrapper<T>>> by the typedef so that the type of its data member, proto_childN , becomes ArrayWrapper<T> .

Moreover the remaining problem is that those copy operations of ArrayWrapper<T> objects slow down the program as opposed to the purpose of expression templates. So I should admit my answer is not good enough. I am trying to find a better answer...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM