Software Archive
Read-only legacy content
17061 Discussions

Nested For loops

newport_j
Beginner
400 Views


I underdstand that cilk_spawn and cilk_for can be nested. What I would like to know is what do you do about nested for loops? It seems that it might be okay to make the outer loop cilk_for, but oneshould not make the inner loop cilk_for.


Also, I have some legacy code that has some loops other than for, such as do while. Is the anything I can do about them? Is there some instruction for rewriting these loopsto for loops?

Newport_j
0 Kudos
1 Reply
Barry_T_Intel
Employee
400 Views
> I underdstand that cilk_spawn and cilk_for can be nested. What I would like to know is what do you
>do about nested for loops? It seems that it might be okay to make the outer loop cilk_for, but one
>should not make the inner loop cilk_for.

Nested cilk_for loops are no different from nesting cilk_for and cilk_spawn. Again, it's a balance thing. If you've got too little work in your spawned code, you'll have too much overhead. For example, in the loop

for (int x = 0; x < xmax; x++)
{
    for (int y = 0; y < ymax; y++)
    {
        for (int z = 0; z < zmax; z++)
        {
            [insert work on 3D points here]
        }
    }
}

I'd probably only make the outer loop a cilk_for loop, assuming that xmax was big enough, and there weren't race conditions introduced by parallelizing the outermost loop. The matrix-multiple sample gives an example of this.

> Also, I have some legacy code that has some loops other than for, such as do while. Is the anything
>I can do about them? Is there some instruction for rewriting these loops to for loops?

I've seen examples that gathers data in "chunks" together in a do/while loop, and then uses a cilk_for to parallelize the chunk. Something like:

while (! done)
{
    data a[256];
    int i = 0;

    while ((! done) && (i < 256))
    {
        a[i++] = generate_data();
    }

    cilk_for (int j = 0; j < i; j++)
    {
        process_data(a);
    }
}

  - Barry
0 Kudos
Reply