开始为OpenMP的每一个内循环线程 [英] Starting a thread for each inner loop in OpenMP

查看:122
本文介绍了开始为OpenMP的每一个内循环线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的OpenMP和我试图启动一个单独的线程在一个二维数组来处理每一个项目。

I'm fairly new to OpenMP and I'm trying to start an individual thread to process each item in a 2D array.

所以基本上,这样的:

for (i = 0; i < dimension; i++) {
    for (int j = 0; j < dimension; j++) {
        a[i][j] = b[i][j] + c[i][j];

我在做什么是这样的:

What I'm doing is this:

#pragma omp parallel for shared(a,b,c) private(i,j) reduction(+:diff) schedule(dynamic)
    for (i = 0; i < dimension; i++) {
        for (int j = 0; j < dimension; j++) {
            a[i][j] = b[i][j] + c[i][j];

这是否实际上开始对每个2D项目或没有主题?我将如何测试?如果是错的,什么是做了正确的方法是什么?谢谢!

Does this in fact start a thread for each 2D item or no? How would I test that? If it is wrong, what is the correct way to do it? Thanks!

注:code已被大大简化了。

Note: The code has been greatly simplified

推荐答案

只有外环您code样品平行。您可以通过打印 omp_get_thread_num()在内部循环测试,你会看到,对于一个给定的 I 中,螺纹num是相同的(当然,这个测试是示范,而不是明确的,因为不同的运行会得出不同的结果)。例如,对于

Only the outer loop is parallel in your code sample. You can test by printing omp_get_thread_num() in the inner loop and you will see that, for a given i, the thread num is the same (of course, this test is demonstrative rather than definitive since different runs will give different results). For example, with:

#include <stdio.h>
#include <omp.h>
#define dimension 4

int main() {
    #pragma omp parallel for
    for (int i = 0; i < dimension; i++)
        for (int j = 0; j < dimension; j++)
            printf("i=%d, j=%d, thread = %d\n", i, j, omp_get_thread_num());
    }

我得到:

i=1, j=0, thread = 1
i=3, j=0, thread = 3
i=2, j=0, thread = 2
i=0, j=0, thread = 0
i=1, j=1, thread = 1
i=3, j=1, thread = 3
i=2, j=1, thread = 2
i=0, j=1, thread = 0
i=1, j=2, thread = 1
i=3, j=2, thread = 3
i=2, j=2, thread = 2
i=0, j=2, thread = 0
i=1, j=3, thread = 1
i=3, j=3, thread = 3
i=2, j=3, thread = 2
i=0, j=3, thread = 0

至于你的code的休息,你可能想要把更多的细节在一个新的问题(这是很难由小样本告诉),但例如,你不能把私人(J)Ĵ只有可能在以后宣布。它是在我上面的例子自动私有的。我猜差异是,我们不能在样品中看到一个变量。此外,循环变量 I 自动私有的(从版本2.5规格 - 在3.0规范相同)

As for the rest of your code, you might want to put more details in a new question (it's difficult to tell from the small sample), but for example, you can't put private(j) when j is only declared later. It is automatically private in my example above. I guess diff is a variable that we can't see in the sample. Also, the loop variable i is automatically private (from the version 2.5 spec - same in the 3.0 spec)

在循环迭代变量
  for循环的一个为或平行于
  构建在私有
  构建。

The loop iteration variable in the for-loop of a for or parallel for construct is private in that construct.

编辑:以上全部是你和我已经证明了code正确的,但你可能有兴趣在下面。对于OpenMP 3.0版有一个<$(如在 gcc版本4.4 的,但不是4.3版本) C $ C>崩溃 where子句中,你有你可以写code,但与
的#pragma OMP并行的崩溃(2)并行既为循环(见的规范)。

All of the above is correct for the code that you and I have shown, but you may be interested in the following. For OpenMP Version 3.0 (available in e.g. gcc version 4.4, but not version 4.3) there is a collapse clause where you could write the code as you have, but with #pragma omp parallel for collapse (2) to parallelize both for loops (see the spec).

修改:OK,我下载了GCC 4.5.0,并运行上述code,但使用崩溃(2)来获得下面的输出,示出了内环现在并行

Edit: OK, I downloaded gcc 4.5.0 and ran the above code, but using collapse (2) to get the following output, showing the inner loop now parallelized:

i=0, j=0, thread = 0
i=0, j=2, thread = 1
i=1, j=0, thread = 2
i=2, j=0, thread = 4
i=0, j=1, thread = 0
i=1, j=2, thread = 3
i=3, j=0, thread = 6
i=2, j=2, thread = 5
i=3, j=2, thread = 7
i=0, j=3, thread = 1
i=1, j=1, thread = 2
i=2, j=1, thread = 4
i=1, j=3, thread = 3
i=3, j=1, thread = 6
i=2, j=3, thread = 5
i=3, j=3, thread = 7

评论这里 (搜索解决办法)也为相关版本的变通办法2.5如果要并行两个回路,但2.5版规范上面引用是非常明确(见不合格的部分实例的 A.35 )。

这篇关于开始为OpenMP的每一个内循环线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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