From: Jeremy Siek (jsiek_at_[hidden])
Date: 2004-07-06 14:11:30
On Jul 6, 2004, at 4:44 AM, Michael Stevens wrote:
> Hi Jeremy,
> On Tuesday 06 July 2004 02:51, Jeremy Graham Siek wrote:
> > Although automagically inserting temporaries in non-trivial
> > since
> > it must all be done with template meta-programming) I do not think
> it is
> > a tar pit. Here is the simply heuristic that I used for MTL 3
> > to decide where to put temporaries:
> > 1. If an operation needs to read from an argument multiple times,
> > evaluate the argument eagerly into a temporary. For example,
> > a sparse matrix in compressed row format times the sum
> > of two vectors:
> > A * (x + y)
> > In this case, the (x + y) would be evaluated into a temporary
> > evaluating the multiplication.
> > 2. If an operation writes into its output in multiple passes, then
> > perform
> > the operation in an eager fashion. For example, the sum
> > of a vector and the product of a sparse matrix in compressed
> > format and a vector.
> > x + (A * y)
> > In this case, the (A * y) would be evaluated into a temporary
> > evaluating the addition.
> OK, this 'eager' evaluation logic seems sound to me. I assume it
> operates in a
> bottom up fashion on the expressions replacing operators with
> temporaries in
> either of the two cases.
Yes, though there is a least one case when information from above is
For example, the outer product of two vectors can be implemented in
either a row-wise or column-wise fashion (with respect to the output
matrix). So, for example, in
A = outer_product(x, y)
you need to know the orientation of A to decide how to evaluate the
> Therefore providing a good heuristic to choose when a temporary seems
> possible. My problem is the exact choice of type for any temporary....
> The mixing of sparse and dense is problematic. However your second
> shows that we can get a hint from the types involved in many cases.
> But can
> this be done in the general case?
I believe so. There are several classes of operations that combine
sparsity in different ways.
1) The operation "ands" the sparsity. E.g., addition, subtraction.
2) The operation "ors" the sparsity. E.g., element-wise multiplication.
So, you have a table like this:
dense "and" dense -> dense
dense "and" sparse -> dense
sparse "and" dense -> dense
sparse "and" sparse -> sparse
> Let me make problem a little more generic. Even with uBLAS's weakly
> storage concept it is possible to parameterise A or y so it storage is
> off-line (on file). Although we know a temporary should be used we
> now need
> to make a choice of storage type.
That is indeed a problem, but we can provide a default choice and also
provide for user-customization using a traits class. (as I see you
> In principle this is solvable (type traits, type promotion etc),
> if storage type could be made orthogonal to other type parameters
> (dense/sparse, matrix/vector). This is however the generic "tar pit" I
Then it's a good thing we have Dave to try to implement it :)
> Maybe I am being too sceptical in think that if the is no simple (for
> user to
> understand even if not to implement) generic solution we should not
> solve the
> base cases.
> I guess I am also sceptical as without a deeper analysis of operators
> types the user cannot know when temporaries will automagically
> appear. One
> big disadvantages of old OO C++ matrix libraries was the complete
> lack of
> control over temporaries. Simply stated I favour a strong
> between syntax and semantics.
Yes, that is an important concern. However, I believe the
above heuristics are simple enough to provide a strong correspondence.
However, we won't really know for sure until we try it.
> All the best,
Jeremy Siek <jsiek_at_[hidden]>
Ph.D. Student, Indiana University Bloomington
C++ Booster (http://www.boost.org)
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk