return value optimization on expression/meta templates

"Olaf Petzold" <opetzold@wit.regiocom.net>
20 Jun 2003 00:08:30 -0400

          From comp.compilers

Related articles
return value optimization on expression/meta templates opetzold@wit.regiocom.net (Olaf Petzold) (2003-06-20)
Re: return value optimization on expression/meta templates E.Robert.Tisdale@jpl.nasa.gov (E. Robert Tisdale) (2003-06-25)
| List of all articles for this month |

From: "Olaf Petzold" <opetzold@wit.regiocom.net>
Newsgroups: comp.compilers,comp.lang.c++
Date: 20 Jun 2003 00:08:30 -0400
Organization: Compilers Central
Keywords: optimize, question
Posted-Date: 20 Jun 2003 00:08:30 EDT

Hello,


I'm writing tvmet, a linear algebra library using expression and meta
templates. With the last release tvmet-1.1.0-030609 I introduced functions
and operators for expressions. To reduce the expenditure on calculating
complex matrix-matrix-products (using meta templates) I introduced
temporaries for local evaluation using return value optimization, like the
2nd function:


----
template<class T1, std::size_t Rows1, std::size_t Cols1, class T2,
std::size_t Cols2>
inline
XprMatrix<
    XprMMProduct<
        T1, Rows1, Cols1, T2, Cols2, Cols1, 1, Cols2, 1
      >,
    Rows1, Cols2
>
product(const Matrix<T1, Rows1, Cols1>& lhs, const Matrix<T2, Cols1, Cols2>&
rhs) {
    typedef XprMMProduct<
        T1, Rows1, Cols1,T2, Cols2, Cols1, 1, Cols2, 1
    > expr_type;
    return XprMatrix<expr_type, Rows1, Cols2>(expr_type(lhs.data(),
rhs.data()));
}


template<class E1, std::size_t Rows1, std::size_t Cols1, class E2,
std::size_t Cols2>
inline
XprMatrix<
    XprMMProduct<
        typename E1::value_type, Rows1, Cols1, typename E2::value_type, Cols2,
Cols1, 1, Cols2, 1
    >,
    Rows1, Cols2
>
product(const XprMatrix<E1, Rows1, Cols1>& lhs, const XprMatrix<E2, Cols1,
Cols2>& rhs) {
    typedef Matrix<typename E1::value_type, Rows1, Cols1> temp_matrix1_type;
    typedef Matrix<typename E2::value_type, Cols1, Cols2> temp_matrix2_type;


    return product(temp_matrix1_type(lhs), temp_matrix2_type(rhs));
}
----


Unfortunally chaining (e.g. Matrix=product(product(transpose(Matrix),
Matrix), Matrix)) expressions will go into wrong, or partially wrong
results due to using return value optimizations. It takes memory
regions where the evaluated results where before.


Is there a limit of compile time expression evaluation? Is it a
compiler problem (intel icc-7.1 hasen't so much problems as gcc-3.2
but, it does have too) or a generel design problem. How can I create
temporaries which haven't such problems. Does exist compiler specifics
for temporary's lifetime? What says the standard about? Using static
temporaries I would like avoid, since all functions with appropriate
Matrix dimensions use the same static temporary which may go into
further/other trouble.




Thanks
Olaf


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.