I have a C++ snippet below with a run-time for
loop,
for(int i = 0; i < I; i++)
for (int j = 0; j < J; j++)
A( row(i,j), column(i,j) ) = f(i,j);
The snippet is called repeatedly. The loop bounds 'I' and 'J' are known at compile time (I/J are the order of 2 to 10). I would like to unroll the loops somehow using templates. The main bottleneck is the row() and column() and f() functions. I would like to replace them with equivalent metaprograms that are evaluated at compile-time, using row<i,j>::enum
tricks.
What I'd really love is something that eventually resolves the loop into a sequence of statements like:
A(12,37) = 0.5;
A(15,23) = 0.25;
A(14,45) = 0.25;
But I'd like to do so without wrecking the for
-for
structure too much. Something in the spirit of:
TEMPLATE_FOR<i,0,I>
TEMPLATE_FOR<j,0,J>
A( row<i,j>::value, column<i,j>::value ) = f<i,j>::value
Can boost::lambda (or something else) help me create this?
You could use Boost MPL.
An example of loop unrolling is on this mpl::for_each page.
It doesn't seem that it's all evaluated at compile time, but it may be a good starting point.
I would say it is a false good-idea.
In C++ this :
row<i,j>::value
means you will have as many differents
row<>()
functions than you have i * j. You don't want this because it will increase the size of the code and do a lot of instruction cache misses.I observed this when I was doing template functions to avoid a single boolean check.
If is a short function just inline it.
This is the way to do it directly:
You can pass
A
through as a parameter to each of thevalue
s if necessary.Here's a way with variadic templates that doesn't require hard coded I and J:
(snippet with generated assembly: https://godbolt.org/g/DlgmEl)
Check out Template Metaprograms and the bubble sort implementations.