1

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.

8
  • Please fix the indentation and use of white-space; it looks very bad! Commented Jul 21, 2015 at 12:15
  • @meaning-matters sorry, LPs has edited so I hope it does look OK now. Commented Jul 21, 2015 at 12:52
  • Is the last printf performed? Commented Jul 21, 2015 at 13:32
  • @LPs No, it just get's stuck Commented Jul 21, 2015 at 15:21
  • 1
    @Badshah Almost OK; I finished the job. Why do you use Dutch words? Commented Jul 21, 2015 at 18:30

2 Answers 2

1

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/

Sign up to request clarification or add additional context in comments.

1 Comment

I made all the variables private (also k) but it still doesn't do anything. Do you know why?
1

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.