Parallelizing base case calculations for recursion using OpenMP
I'm trying to learn the concepts OpenMP and stumbled upon a case which I'm having a hard time grasping on how to solve using this library.
Let's say we have the following recursion function
// ...
vo开发者_开发技巧id 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 performTask
in parallel while generating new structs in the master thread using OpenMP?
I know there is something called 'tasks', and I think that's what I'm supposed to be using here, but everything I come up with just doesn't get any performance gains at all. Please point me in the right direction.
Edit: I made the example program more concrete for better explanation.
The code below doesn't work as is, but hopefully it will point you in the right direction:
// ...
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;
}
The code is based on Comparing Nested Parallel Regions and Tasking in OpenMP 3.0.
精彩评论