如何在openMP的单个区域内并行for循环? [英] How to parallel for loop inside single region in openMP?
问题描述
我有一个递归程序,我想使用openMP来加快速度.结构如下.
I have a recursive program which I want to speed up using openMP. The structure is like below.
我不熟悉omp task
,只是从这里学到了一些东西.看来我必须将buildTree
包装在omp single
区域中.
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.
但是,我也想并行化buildTree
中的for循环,如何实现呢?
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
}
}
}
推荐答案
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();
和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++)
检查我的第4.3节Example 4–2 Calls to OpenMP Routines Within Parallel Regions
链接以获取更多详细信息
Check the section 4.3 Example 4–2 Calls to OpenMP Routines Within Parallel Regions
of my first link for more details
这篇关于如何在openMP的单个区域内并行for循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!