简体   繁体   中英

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. I have a simple for loop which I want to parallelize. Using openmp, I thought the following code should do the job

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. 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.

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.

Otherwise, declare k as a private variable in the #pragma statement at the beginning of the parallel region:

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

A good primer is available here: 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.

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)

check this pdf about openmp: http://openmp.org/mp-documents/OpenMP4.0.0.Examples.pdf

EDIT: btw, try flushing the buffer after printf(): fflush(stdout)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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