omp有序子句如何工作? [英] How does the omp ordered clause work?

查看:245
本文介绍了omp有序子句如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

vector<int> v;

#pragma omp parallel for ordered schedule(dynamic, anyChunkSizeGreaterThan1)
    for (int i = 0; i < n; ++i){
            ...
            ...
            ...
#pragma omp ordered
            v.push_back(i);
    }

填充 v

当到达 omp已订购阻塞所有线程需要等待最低迭代可能的线程完成,但如果没有线程被指定的特定迭代呢?或者,OpenMP运行时库总是确保最小的迭代由一些线程处理?

When reaching the omp ordered block all threads need to wait for the lowest iteration possible thread to finish, but what if none of the threads was appointed that specific iteration? Or does the OpenMP runtime library always make sure that the lowest iteration is handled by some thread?

也为什么它建议有序子句与动态计划一起使用? 静态计划会影响性能吗?

Also why is it suggested that ordered clause be used along with the dynamic schedule? Would static schedule affect performance?

推荐答案

有序子句的工作方式如下:它们会遇到有序的区域,然后按照与在串行循环中执行的顺序相同的顺序执行。这仍然允许一定程度的并发,特别是如果有序区域之外的代码段有大量运行时间。

The ordered clause works like this: different threads execute concurrently until they encounter the ordered region, which is then executed sequentially in the same order as it would get executed in a serial loop. This still allows for some degree of concurrency, especially if the code section outside the ordered region has substantial run time.

没有特别的理由使用动态调度,而不是使用小块大小的 static 调度。这一切都取决于代码的结构。由于有序引入线程之间的依赖关系,如果与默认块大小一起使用 schedule(static)必须等待第一个线程完成所有迭代,然后第三个线程必须等待第二个线程完成其迭代(因此也是第一个线程),依此类推。可以很容易地用3个线程和9次迭代(每个线程3个)来实现它:

There is no particular reason to use dynamic schedule instead of static schedule with small chunk size. It all depends on the structure of the code. Since ordered introduces dependency between the threads, if used with schedule(static) with default chunk size, the second thread would have to wait for the first one to finish all iterations, then the third thread would have to wait for the second one to finish its iterations (and hence for the first one too), and so on. One could easily visualise it with 3 threads and 9 iterations (3 per thread):

tid  List of     Timeline
     iterations
0    0,1,2       ==o==o==o
1    3,4,5       ==.......o==o==o
2    6,7,8       ==..............o==o==o

= 显示线程正在并行执行代码。 o 是线程正在执行有序区域时。 是线程正在空闲,等待它执行有序的区域。使用 schedule(static,1)会发生以下情况:

= shows that the thread is executing code in parallel. o is when the thread is executing the ordered region. . is the thread being idle, waiting for its turn to execute the ordered region. With schedule(static,1) the following would happen:

tid  List of     Timeline
     iterations
0    0,3,6       ==o==o==o
1    1,4,7       ==.o==o==o
2    2,5,8       ==..o==o==o


$ b b我相信两种情况的区别都是显而易见的。使用 schedule(dynamic)上面的图片将变得或多或少随机,因为分配给每个线程的迭代列表是非确定性的。它还会增加额外的开销。这只有在计算量对于每次迭代不同时才有用,并且与使用动态调度所增加的开销相比花费更多的时间来进行计算。

I believe the difference in both cases is more than obvious. With schedule(dynamic) the pictures above would become more or less random as the list of iterations assigned to each thread is non-deterministic. It would also add an additional overhead. It is only useful if the amount of computation is different for each iteration and it takes much more time to do the computation than is the added overhead of using dynamic scheduling.

Don不担心最低编号的迭代。它通常被处理到团队中的第一个线程以准备好执行代码。

Don't worry about the lowest numbered iteration. It is usually handled to the first thread in the team to become ready to execute code.

这篇关于omp有序子句如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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