Parallelizing base register calculations for recursion using OpenMP

I am trying to learn the concepts of OpenMP and stumbled upon a case that I find difficult to learn how to solve this library.

Say we have the following recursive function

// ...
void recurse(int tmp[], int p, const int size)
{
   if (p == size)
   {
      // Computationally heavy, should be executed in its own "thread"
      performTask(tmp); // Note: Only requires read access
   }
   else
   {
      for(int i = 0; i < size; i++)
      {
         // Alter tmp and continue recursion
         tmp[p] = i;
         recurse(tmp, p+1, size);
      }
   }
}
// ...
int main(int argc, char * argv[])
{
    int tmp[10];
    recurse(tmp, 0, 10);
    return 0;
}

How can I execute performTaskin parallel when creating new structures in the main thread using OpenMP?

I know that there is something called "tasks" and I think that what I should use here, but everything that I came up with, just does not get any success in performance. Please point me in the right direction.

Edit: I made the sample program more specific for a better explanation.

+3
source share
1 answer

, , , , :

// ...
void recurse(int tmp[], int p, const int size)
{
   if (p == size)
   {
      // Computationally heavy, should be executed in its own "thread"
      // perform task using the thread pool
#pragma omp task     
      performTask(tmp); // Note: Only requires read access
   }
   else
   {
      for(int i = 0; i < size; i++)
      {
         // Alter tmp and continue recursion
         tmp[p] = i;
         recurse(tmp, p+1, size);
      }
   }
}
// ...
int main(int argc, char * argv[])
{    
    int tmp[10];
    // start threads
#pragma omp parallel
{
    // use single thread to construct `tmp` values
#pragma omp single nowait
    recurse(tmp, 0, 10);
}
    return 0;
}

OpenMP 3.0.

+1

Source: https://habr.com/ru/post/1792672/


All Articles