OpenMP中的SECTIONS指令如何分配工作? [英] How does the SECTIONS directive in OpenMP distribute work?

查看:251
本文介绍了OpenMP中的SECTIONS指令如何分配工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用omp sections的OpenMP中,线程将分配到内的块,还是将每个线程分配给每个节?

In OpenMP when using omp sections, will the threads be distributed to the blocks inside the sections, or will each thread be assigned to each sections?

nthreads == 3时:

#pragma omp sections
{
    #pragma omp section
    { 
        printf ("id = %d, \n", omp_get_thread_num());
    }

    #pragma omp section
    { 
        printf ("id = %d, \n", omp_get_thread_num());
    }
}

输出:

id=1
id=1

但是当我执行以下代码时:

But when I execute the following code:

#pragma omp sections
{
    #pragma omp section
    { 
        printf ("id = %d, \n", omp_get_thread_num());
    }

    #pragma omp section
    { 
        printf ("id = %d, \n", omp_get_thread_num());
    }
}

#pragma omp sections
{
    #pragma omp section
    { 
        printf ("id = %d, \n", omp_get_thread_num());
    }

    #pragma omp section
    { 
        printf ("id = %d, \n", omp_get_thread_num());
    }
}

输出:

id=1
id=1

id=2
id=2

从这些输出中,我无法理解OpenMP中节的概念.

From these output I can't understand what the concept of sections is in OpenMP.

推荐答案

由OP发布的代码将永远不会并行执行,因为没有出现parallel关键字. OP的id不同于0的事实表明,他的代码可能嵌入在并行指令中.但是,从他的职位上并不清楚,这可能会使初学者感到困惑.

The code posted by the OP will never execute in parallel, because the parallel keyword does not appear. The fact that the OP got ids different from 0 shows that probably his code was embedded in a parallel directive. However, this is not clear from his post, and might confuse beginners.

最明智的例子是(对于由OP发布的第一个例子):

The minimum sensible example is (for the first example posted by the OP):

#pragma omp parallel sections
{
    #pragma omp section
    { 
        printf ("id = %d, \n", omp_get_thread_num());
    }

    #pragma omp section
    { 
        printf ("id = %d, \n", omp_get_thread_num());
    }
}

在我的机器上,这会打印

On my machine, this prints

id = 0,
id = 1,

表明这两个部分是由不同的线程执行的.

showing that the two sections are being executed by different threads.

值得注意的是,此代码不能比两个线程提取更多的并行度:如果执行的线程更多,则其他线程没有任何工作要做,只会闲置.

It's worth noting that however this code can not extract more parallelism than two threads: if it is executed with more threads, the other threads don't have any work to do and will just sit down idle.

这篇关于OpenMP中的SECTIONS指令如何分配工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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