C语言 节和任务openmp的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13788638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between section and task openmp
提问by Arkerone
What is the difference in OpenMP between :
OpenMP 之间有什么区别:
#pragma omp parallel sections
{
#pragma omp section
{
fct1();
}
#pragma omp section
{
fct2();
}
}
and :
和 :
#pragma omp parallel
{
#pragma omp single
{
#pragma omp task
fct1();
#pragma omp task
fct2();
}
}
I'm not sure that the second code is correct...
我不确定第二个代码是否正确...
回答by Hristo Iliev
The difference between tasks and sections is in the time frame in which the code will execute. Sections are enclosed within the sectionsconstruct and (unless the nowaitclause was specified) threads will not leave it until all sections have been executed:
任务和部分之间的区别在于代码将执行的时间范围。部分包含在sections构造中,并且(除非nowait指定了子句)线程在所有部分都执行完之前不会离开它:
[ sections ]
Thread 0: -------< section 1 >---->*------
Thread 1: -------< section 2 >*------
Thread 2: ------------------------>*------
... *
Thread N-1: ---------------------->*------
Here Nthreads encounter a sectionsconstruct with two sections, the second taking more time than the first. The first two threads execute one section each. The other N-2threads simply wait at the implicit barrier at the end of the sections construct (show here as *).
在这里N线程遇到一个sections包含两个部分的构造,第二个比第一个花费更多的时间。前两个线程各执行一个部分。其他N-2线程只是在部分构造末尾的隐式屏障处等待(此处显示为*)。
Tasks are queued and executed whenever possible at the so-called task scheduling points. Under some conditions, the runtime could be allowed to move task between threads, even in the mid of their lifetime. Such tasks are called untied and an untied task might start executing in one thread, then at some scheduling point it might be migrated by the runtime to another thread.
只要有可能,任务就会在所谓的任务调度点排队并执行。在某些情况下,可以允许运行时在线程之间移动任务,即使是在它们生命周期的中期。此类任务称为解绑任务,解绑任务可能会在一个线程中开始执行,然后在某个调度点可能会被运行时迁移到另一个线程。
Still, tasks and sections are in many ways similar. For example, the following two code fragments achieve essentially the same result:
尽管如此,任务和部分在许多方面还是相似的。例如,以下两个代码片段实现了基本相同的结果:
// sections
...
#pragma omp sections
{
#pragma omp section
foo();
#pragma omp section
bar();
}
...
// tasks
...
#pragma omp single nowait
{
#pragma omp task
foo();
#pragma omp task
bar();
}
#pragma omp taskwait
...
taskwaitworks very like barrierbut for tasks - it ensures that current execution flow will get paused until all queued tasks have been executed. It is a scheduling point, i.e. it allows threads to process tasks. The singleconstruct is needed so that tasks will be created by one thread only. If there was no singleconstruct, each task would get created num_threadstimes, which might not be what one wants. The nowaitclause in the singleconstruct instructs the other threads to not wait until the singleconstruct was executed (i.e. removes the implicit barrier at the end of the singleconstruct). So they hit the taskwaitimmediately and start processing tasks.
taskwait工作非常像barrier但对于任务 - 它确保当前执行流程将暂停,直到所有排队的任务都已执行。它是一个调度点,即它允许线程处理任务。single需要该构造,以便仅由一个线程创建任务。如果没有single构造,每个任务都会获得创建num_threads时间,这可能不是人们想要的。构造中的nowait子句single指示其他线程不要等到single构造被执行(即移除single构造末尾的隐式屏障)。所以他们taskwait立即点击并开始处理任务。
taskwaitis an explicit scheduling point shown here for clarity. There are also implicit scheduling points, most notably inside the barrier synchronisation, no matter if explicit or implicit. Therefore, the above code could also be written simply as:
taskwait为清楚起见,此处显示了一个明确的调度点。还有隐式调度点,最明显的是在屏障同步内部,无论是显式还是隐式。因此,上面的代码也可以简单地写成:
// tasks
...
#pragma omp single
{
#pragma omp task
foo();
#pragma omp task
bar();
}
...
Here is one possible scenario of what might happen if there are three threads:
如果存在三个线程,可能会发生以下情况的一种可能情况:
+--+-->[ task queue ]--+
| | |
| | +-----------+
| | |
Thread 0: --< single >-| v |-----
Thread 1: -------->|< foo() >|-----
Thread 2: -------->|< bar() >|-----
Show here within the | ... |is the action of the scheduling point (either the taskwaitdirective or the implicit barrier). Basically thread 1and 2suspend what they are doing at that point and start processing tasks from the queue. Once all tasks have been processed, threads resume their normal execution flow. Note that threads 1and 2might reach the scheduling point before thread 0has exited the singleconstruct, so the left |s need not necessary be aligned (this is represented on the diagram above).
此处显示的| ... |是调度点的操作(taskwait指令或隐式屏障)。基本上线程1并2挂起他们在那时正在做的事情并开始处理队列中的任务。处理完所有任务后,线程将恢复其正常执行流程。请注意,线程1和2可能在线程0退出single构造之前到达调度点,因此左侧|s 不需要对齐(这在上图中表示)。
It might also happen that thread 1is able to finish processing the foo()task and request another one even before the other threads are able to request tasks. So both foo()and bar()might get executed by the same thread:
甚至在其他线程能够请求任务之前,线程1也可能完成处理foo()任务并请求另一个任务。因此,无论foo()并且bar()可能由同一个线程得到执行:
+--+-->[ task queue ]--+
| | |
| | +------------+
| | |
Thread 0: --< single >-| v |---
Thread 1: --------->|< foo() >< bar() >|---
Thread 2: --------------------->| |---
It is also possible that the singled out thread might execute the second task if thread 2 comes too late:
如果线程 2 来得太晚,被挑出的线程也有可能执行第二个任务:
+--+-->[ task queue ]--+
| | |
| | +------------+
| | |
Thread 0: --< single >-| v < bar() >|---
Thread 1: --------->|< foo() > |---
Thread 2: ----------------->| |---
In some cases the compiler or the OpenMP runtime might even bypass the task queue completely and execute the tasks serially:
在某些情况下,编译器或 OpenMP 运行时甚至可能完全绕过任务队列并串行执行任务:
Thread 0: --< single: foo(); bar() >*---
Thread 1: ------------------------->*---
Thread 2: ------------------------->*---
If no task scheduling points are present inside the region's code, the OpenMP runtime might start the tasks whenever it deems appropriate. For example it is possible that all tasks are deferred until the barrier at the end of the parallelregion is reached.
如果区域代码中不存在任务调度点,则 OpenMP 运行时可能会在它认为合适的时候启动任务。例如,所有任务都可能被推迟,直到parallel到达区域末尾的障碍。

