繁体   English   中英

所有的omp任务都被安排在同一线程中

[英]all of the omp tasks are being scheduled in the same thread

我在玩omp任务功能,遇到了问题。 我有以下代码:

void function1(int n, int j)
{
    //Serial part
    #pragma omp parallel
    {
        #pragma omp single nowait
        {
            //execcute function1() for every n
            for (i=0; i<n; i++)
            {
                //create a task for computing only large numbers of n
                if (i <= 10)
                    //execute serial
                else
                    #pragma omp task
                    //call function again 
                    function1(n, j+1);
                    printf("task id:\n", omp_get_thread_num());
            }
        }
    }
}

现在,代码将产生正确的结果,但是性能比原始的串行版本慢得多。 经过一番调查,我发现所有任务都在线程0中执行,无论总共有4个线程在运行。 有人知道这是怎么回事吗? 提前致谢!

omp single nowait编译指示意味着以下块将由单个线程执行。 在这种情况下,这意味着整个循环由一个线程执行。 这应该可以解决您的问题:

void function1(int n, int j)
{
    //Serial part
    #pragma omp parallel
    {
        //execcute function1() for every n
        for (i=0; i<n; i++)    //Notice this is outside the pragma
        {
            #pragma omp single nowait    //Notice this is inside the loop
            {
                //create a task for computing only large numbers of n
                if (i <= 10)
                    //execute serial
                else
                    #pragma omp task
                    //call function again 
                    function1(n, j+1);
                    printf("task id:\n", omp_get_thread_num());
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM