I am writing a C program where I operate nested for
loops, one within another, as follows:
for(i[0] = 0; i[0] < n[0]; i[0]++)
for(i[1] = 0; i[1] < n[1]; i[1]++)
for(i[2] = 0; i[2] < n[2]; i[2]++)
{
do_lot_of_work(i, n, 3);
}
As you can see, the above code has three nested for
loops. In my program, it accounts for 3 dimensions. However, I want to make my program expandable, which can account of any number of dimensions, on the fly, as the user wishes; i.e. for 4 dimensions, I wish to have four nested for
loops, and do the work as do_lot_of_work(i,n,4)
. Similarly, for any number of dimensions.
My question is: how to make the aforementioned nesting of for
loops expandable?
Please note that, in order to achieve the goal, I am willing to sacrifice the inner for
loops, but wish to keep the first for
loop, in order to make my program parallel with OpenMP.