问题
I have a recursive program which I want to speed up using openMP. The structure is like below.
I am not familiar with omp task
and just learnt something from here. It seems that I have to wrap buildTree
in a omp single
region.
However, I also want to parallelize the for loop inside buildTree
, how can I achieve that?
int main()
{
#pragma omp parallel
{
#pragma omp single nowait
buildTree();
}
}
void buildTree
{
if(endRecursion)
return;
for(int i = 0; i < problemSize; i++)
{
// I want to parallelize these code using omp for
}
if(problemSizeIsSmall)
{
buildTree(subProblemSize); // left subtree
buildTree(subProblemSize); // right subtree
}
else
{
#pragma omp task
{
buildTree(subProblemSize); // left subtree
}
#pragma omp task
{
buildTree(subProblemSize); // right subtree
}
}
}
回答1:
I think you can use nested parallelism in your problem.
Your code would look like this way in your main()
:
#pragma omp parallel for num_threads(2)
buildTree();
and in buildTree()
:
omp_set_num_threads(4); // 4 or whatever number of threads you want
#pragma omp parallel for
for(int i = 0; i < problemSize; i++)
Check the section 4.3 Example 4–2 Calls to OpenMP Routines Within Parallel Regions
of my first link for more details
来源:https://stackoverflow.com/questions/30294883/how-to-parallel-for-loop-inside-single-region-in-openmp