1
votes

How can I properly parallelize the inner loop:

int found = -1;
#pragma omp parallel for ordered schedule(dynamic, 1) shared(found) 
for (long i = 0; i < big_number; ++i)
{
  if( function() )
  { 
    #pragma omp ordered
    if( found == -1 )
    {
       found = i;
    }
  }
}

bool function()
{
   for(int i =0; i < another_big_number;  i++)
   {
     if( some_condition)
     {
       return true;
     }
   }
}

I cannot post the whole of the code, but I kept most the part which seems to be necessary for the question. The idea for the first loop is to find the lowest iteration which the function returns true.

EDIT: of course any idea about improving the parallelization is appreciated, However, my question is more about how I can improve performance of the function() itself alongside the outer for loop using Nested methodology .

Please note that I am aware of collapse clause, however I cannot use it (thanks to the visual studio which does not support later versions of the openMP).

Any idea for improving the whole parallel loop is appreciated as well. :)

Do you think there would be an increase in performance by parallelizing the for loop inside function()?

1

1 Answers

1
votes

This code has a lot of problems, first you cannot beak a loop for this way (return found) furthermore this order does not make much sense, at least with the chunk of code that you provided, use flush do implement the break of the loop, something like:

boolean found = false;
long return_value = -1;

#pragma omp parallel for schedule(dynamic, 1) shared(found, return_value) 
for (long i = 0; i < big_number; ++i)
{
  #pragma omp flush (found)
  if(!found)
  {
      if( function() ){
         found = true;
         return_value = i;
         #pragma omp flush (found)

      }
  }
}

or also:

long return_value = -1;

#pragma omp parallel for schedule(dynamic, 1) shared(return_value) 
for (long i = 0; i < big_number; ++i)
{
    if( function() ){

         #pragma omp critical
         {
            return_value = i;
         }

         #pragma omp cancel for 
      }
   #pragma omp cancellation point for
}

based on the example from here