如何在openMP的单个区域内并行for循环? [英] How to parallel for loop inside single region in openMP?

查看:455
本文介绍了如何在openMP的单个区域内并行for循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个递归程序,我想使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆