# Nested loop limit?

Dan Christensen jdc at uwo.ca
Sun Jul 11 04:39:09 CEST 2004

```Peter Hansen <peter at engcorp.com> writes:

> If you consider that statically nested for loops must in some
> way represent different dimensions, is it really possible that
> a problem can have more than 20 dimensions (or even nearly that
> many!) which must all be looped over simultaneously?
...
> But I'd be interested in any ("real world", as usual) example
> where it is true....

Well, in a computation in quantum gravity, I have the C code shown
below.  It has exactly 20 nested for loops!  There are of course
other ways to write it, but given the number of iterations, speed
is the bottom line.  Unfortunately, this is only the simplest test
case of a larger problem...

I'm converting much of this project to python, but will probably
keep this part in C and wrap it with SWIG.

Dan

(I'm simultaneously embarrassed and proud of this code...  :-)

#define l1(a)       for(Label[a] = 0; Label[a] <= cutoff; Label[a]++)

#define l2(a,b,c,d) \
for(Label[a] = low3(Label[b],Label[c],Label[d]); \
Label[a] <= cutoff && Label[a] <= high3(Label[b],Label[c],Label[d]); \
Label[a]+=2)

sumF = 0.0;
sumFG = 0.0;

l1(0)
l1(1)
l1(4)
l2(10,4,0,1)
l1(2)
l1(5)
l2(11,5,0,2)
l1(7)
l2(13,7,1,2)
l2(16,4,5,7)
l1(3)
l1(6)
l2(12,6,0,3)
l1(8)
l2(14,8,1,3)
l2(17,4,6,8)
l1(9)
l2(15,9,2,3)
l2(18,5,6,9)
l2(19,7,8,9)
{
saveF = F();
sumF += saveF;
sumFG += saveF*obsv();
}

```