English 中文(简体)
开放式管理计划:(i;...)和(i)平行
原标题:OpenMP: parallel for(i;...) and i value
  • 时间:2011-11-02 10:50:03
  •  标签:
  • c++
  • c
  • openmp

我有以下平行的幻灯:

#include <omp.h>
#include "stdio.h"

int main()
{

omp_set_num_threads(4);
    int i;
#pragma omp parallel private(i)
    {
#pragma omp for 
        for(i = 0;i < 10; i++) {
            printf("A  %d: %d
", omp_get_thread_num(),i);
        }
#pragma omp critical
        printf("i  %d: %d
", omp_get_thread_num(), i ); 
    }
}

我认为,在航程之后,每一条路面都将有<条码>i,等于<条码>i。 产出如下:

A  0: 0
A  0: 1
A  0: 2
A  3: 9
A  2: 6
A  2: 7
A  2: 8
A  1: 3
A  1: 4
A  1: 5
i  0: 3
i  3: 10
i  2: 9
i  1: 6

我得到的是:

A  0: 0
A  0: 1
A  0: 2
A  3: 9
A  2: 6
A  2: 7
A  2: 8
A  1: 3
A  1: 4
A  1: 5
i  0: -1217085452
i  3: -1217085452
i  2: -1217085452
i  1: -1217085452

如何使<条码>i以保持最后的代谢价值?last private(i),i = 10 for all threads, that is not what 我想.

最佳回答

你们可以这样说。 开放式管理计划改变方案周期。

汇编者根据明确界定的一套规则,重新撰写假冒。

这也意味着你不能脱离,不能从这种 lo中返回。 你们也不能直接操纵 lo变。 休息条件不得指随机功能,也不得作任何有条件的表述,简言之,a omp supplemented for loop is not a for loop

#include <omp.h>
#include "stdio.h"

int main()
{

omp_set_num_threads(4);
#pragma omp parallel
    {
        int i;
#pragma omp for 
        for(i = 0;i < 10; i++) {
            printf("A  %d: %d
", omp_get_thread_num(),i);
        }
#pragma omp critical
        printf("i  %d: %d
", omp_get_thread_num(), i ); 
    }
}
问题回答

由于 se,我 following下了解决问题的 following滴trick。

    int i, last_i;
#pragma omp parallel private(i)
    {
#pragma omp for 
        for(i = 0;i < 10; i++) {
            printf("A  %d: %d
", omp_get_thread_num(),i);
            last_i = i;
        }
#pragma omp critical
        printf("i  %d: %d
", omp_get_thread_num(), last_i ); 
    }
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签