### Compute Parallel within a Loop-Iteration

Posted:

**Mon Jul 23, 2012 12:22 am**Hi!

I already parallized my first few loops for an interesting math/mechanics algorithm. Right now, I am standing before a more intesting piece of code which I want to parallelize. The problem is that I (as far as I understood) encountered a race condition.

Basically, I cannot resolve this race condition. However, I want to split up every iteration of the loop into multiple threads which each compute different things.

Let me write the programm structure in some simplified pseudo code:

Is it possible to have one thread computing M1[i], another one computing M2[i] and when they both are finished N[i] should be computed by an already waiting third thread. Then head on to the next iteration. Over and over again until the loop is finished.

I'd also want to create the threads before the loop to keep the administrative times as low as possible.

Is this possible? If yes, which openMP directives will I need?

Thanks alot and kind regards,

yours Roland

I already parallized my first few loops for an interesting math/mechanics algorithm. Right now, I am standing before a more intesting piece of code which I want to parallelize. The problem is that I (as far as I understood) encountered a race condition.

Basically, I cannot resolve this race condition. However, I want to split up every iteration of the loop into multiple threads which each compute different things.

Let me write the programm structure in some simplified pseudo code:

- Code: Select all
`/* Initialize */`

N[N-1] = 1;

M1[N-1] = 0;

M2[N-1] = 0;

/* Loop */

for ( i = N-2 ; i => 0 ; i-- )

{

M1[i] = computeM1(M1[i+1], N[i+1]);

M2[i] = computeM2(M2[i+1], N[i+1]);

N[i] = computeN(M1[i],M2[i],N[i+1]);

}

Is it possible to have one thread computing M1[i], another one computing M2[i] and when they both are finished N[i] should be computed by an already waiting third thread. Then head on to the next iteration. Over and over again until the loop is finished.

I'd also want to create the threads before the loop to keep the administrative times as low as possible.

Is this possible? If yes, which openMP directives will I need?

Thanks alot and kind regards,

yours Roland