ReverseFlowControl
ReverseFlowControl

Reputation: 826

OpenMP nested loop index dependency

I am new to this particular stack exchange. I don't know the syntax to make things look pretty, sorry.

I am working to maximize my code for a recreational project. I code in C++ and I am using OpenMP for parallelization. The problem is as follows, this is an oversimplified version of the actual code, say I have:

 //Lots of other stuff

 #pragma omp parallel for ordered num_threads(4) schedule(dynamic, 100) private(a,b)
for(int a=0; a<230000; a++)
{
    for(int b=a+1; b<230000; b++)
    {

        //code increments b at times, example
        while(stored[b][0]==stored[a][0])
        {
                    b++;
        }

        //increment counter
        #pragma omp atomic
        ++counter;

        //counting part
        #pragma omp ordered
        if(stored[a][0]!=stored[b][0])
        {
           if(stored[a][1]!=stored[b][1])
           {
                //print counter to file
           }
         }
    }
}

Essentially, I want to count the number of times my code runs through. But, the dependency "b=a+1" seems to make the code way off. It gives me the same count as if it were just "b=0", which is about 10 billion.

Any help would be greatly appreciated.

Edit 1: I should have mentioned everything inside the loop is independent of a. That is, it can be run in any direction on a or any random values of a, but it must be in order in b. The whole point is to skip as many b values as possible under certain constraints which the increments in b measure, to do this the algorithm needs the order in b.

Upvotes: 1

Views: 1305

Answers (1)

Atish
Atish

Reputation: 1279

I'm a little confused by your code. If you are trying to make the code parallel, why did you put ordered in the pragma for? Ordered will make it run in serial. If you are getting the wrong answer with using ordered, some other part of your code is probably wrong.

A problem i spot is that you have declared a and b before the pragma statement. You probably shouldn't do that so your code is not confusing. a and b should be in the scope of the for loop.

Here is the code i think you are looking for.

// do not declare a or b in the code before or use different variables instead of a or b
int counter = 0;
#pragma omp parallel for num_threads(4) schedule(dynamic, 100)
for(int a=0; a<230000; a++)
{
    int tmpCounter = 0;
    for(int b=a+1; b<230000; b++)
    {

        //code increments b at times, example
        while(stored[b][0]==stored[a][0])
        {
                    b++;
        }

        //increment counter code
        ++tmpCounter;

        //counting part
       // idk if you need this code to be ordered?
          if(stored[a][0]!=stored[b][0])
          {
            if(stored[a][1]!=stored[b][1])
            {
                //print counter to file
            }
          }

    }
    #pragma omp critical
    counter += tmpCounter;
}

If this isn't what you are looking for, just comment on my post after you clarify. I'll try to fix it.

Upvotes: 1

Related Questions