简体   繁体   English

使用openmp在C中进行并行编程

[英]parallel programming in C with openmp

Right now I am learning about parallel programming in C with openmp and now I have stumbled upon the following problem. 现在,我正在学习有关使用openmp在C中进行并行编程的知识,而现在我偶然发现了以下问题。 I have a simple for loop which I want to parallelize. 我有一个想要循环的简单for循环。 Using openmp, I thought the following code should do the job 使用openmp,我认为以下代码可以完成这项工作

unsigned long int teller_groot;
int               boel = 0;
int               k    = 0; 
int               l    = 1;
unsigned long int sum2;
int               thread_id;
int               nloops;

#pragma omp parallel private(thread_id, nloops)
{   
    sum2 = 0;
    #pragma omp for
        for (teller_groot=1000000; teller_groot<1000000000000; teller_groot++)
        {
            boel = 0;
            for (int i = 0; i < 78498; i++)
            {
                if (floor(teller_groot / array[i]) == teller_groot / array[i])                    
                {
                   boel = 1; 
                   break;
                }
            }

            if (boel == 0)
            {
               sum2 = sum2 + teller_groot;
            }

            if (sum2 >= 1000000000)
            {
               sum2 = sum2 - 1000000000;
            }

            if (k == 10000000)
            {
               printf("%d, ", l);
               l++;
               k = 0;
            }

            k++;
        }

        thread_id = omp_get_thread_num();

        printf("Thread %d performed %d iterations of the loop.\n", thread_id, nloops);
}

The code 编码

if (k == 10000000)
{
    printf("%d, ",l);
    l++;
    k = 0;
}

k++;

is just for me to know how far in the loop we are. 我只是想知道我们在循环中走了多远。 If I run the program, it doesn't print anything, meaning it does not calculate anything. 如果我运行该程序,它将不会打印任何内容,这意味着它不会计算任何内容。 What is wrong with the code then? 那么代码有什么问题呢? Thanks. 谢谢。

You have declared the variable k (among others) outside the openmp for loop. 您已经在openmp for循环之外声明了变量k (以及其他)。 When variables are declared outside parallel regions they have shared scope by default. 在并行区域外声明变量时,默认情况下它们具有共享作用域。 This means all of the threads you use will be checking and writing to the same k variable, which could result in race conditions. 这意味着您使用的所有线程都将检查并写入相同的k变量,这可能导致争用情况。

If you want to write to the same variable k with different threads you could use locks or atomic updates to produce the behaviour you want. 如果要使用不同的线程写入同一变量k ,则可以使用锁或原子更新来产生所需的行为。

Otherwise, declare k as a private variable in the #pragma statement at the beginning of the parallel region: 否则,请在并行区域开头的#pragma语句中将k声明为私有变量:

#pragma omp parallel private(thread_id, nloops, k)

A good primer is available here: http://supercomputingblog.com/openmp/tutorial-parallel-for-loops-with-openmp/ 此处提供了一个很好的入门手册: http : //supercomputingblog.com/openmp/tutorial-parallel-for-loops-with-openmp/

I don't have enough reputation to comment, so I'm writing an answer. 我没有足够的声誉来发表评论,所以我正在写答案。

Which part of your code exactly gets stuck? 您的代码的哪一部分完全卡住了? (by stuck do we mean it takes an awful lot of time?) 卡住是不是意味着要花费大量时间?)

because: 因为:

            if (floor(teller_groot / array[i]) == teller_groot / array[i])                    
            {
               boel = 1; 
               break;
            }

What you do is floor the result of (lets say to make it simple) 22/41 = 0.536 to => 0.5 and then check whether 0.536 == 0.5 and this does not equal so the command break might not get executed, depending on what is in array which you have not specified. 您要做的是将22/41 = 0.536简化为=> 0.5的结果(然后简化一下),然后检查0.536 == 0.5,这是否不相等,因此可能无法执行命令中断 ,具体取决于什么在未指定的数组中。

Also, a lot of your variables are shared including array , that might make a difference as well and the variable nloops is not specified (unless somewhere above the code you've shown) 另外,您的许多变量都被共享,包括array ,这可能也会有所作为,并且未指定变量nloops (除非在您显示的代码上方)

check this pdf about openmp: http://openmp.org/mp-documents/OpenMP4.0.0.Examples.pdf 请查看有关openmp的以下pdf文件: http : //openmp.org/mp-documents/OpenMP4.0.0.Examples.pdf

EDIT: btw, try flushing the buffer after printf(): fflush(stdout) 编辑:顺便说一句,尝试在printf()之后刷新缓冲区: fflush(stdout)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM