Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template-ing a 'for' loop in C++?

Tags:

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?

like image 423
RAC Avatar asked Jun 23 '09 13:06

RAC


People also ask

What is a for loop in C?

The for loop in C language is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like the array and linked list.

What is a template loop?

Loop Templates allow Business+ users to control the people, documents, and task lists that are auto-added when loops are first created. These loop templates also allow you to dictate what information or which documents are required before an agent can submit for review.

Can we use template in C?

The main type of templates that can be implemented in C are static templates. Static templates are created at compile time and do not perform runtime checks on sizes, because they shift that responsibility to the compiler.

What is the correct syntax of for loop in C?

Syntax. The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.


1 Answers

A good compiler should do unrolling for you. For instance, in gcc compiling with the -O2 option turns on loop unrolling.

If you try to do it yourself manually, unless you measure things carefully and really know what you are doing, you are liable to end up with slower code. For example, in your case with manual unrolling you are liable to prevent the compiler from being able to do a loop interchange or stripmine optimization (look for --floop-interchange and -floop-strip-mine in the gcc docs)

like image 96
T.E.D. Avatar answered Nov 01 '22 09:11

T.E.D.