0

I am new to openmp and write some code to find out how this parallel for works

#pragma omp parallel for
    for (int i=1;i<=x;++i){
        for (int j=1;j<=y;++j){
            if (diameter < array[i][j]){
                diameter = array[i][j];
            }
        }
    }

here are my quesiotns

1: in this struct there are total xy iterations, how many threads will the omp create to do the work? Is it xy threads OR the thread number is limited by machine resource?

2: if question 1 answer is limited by machine resource, say I can run this program on a 32-core machine, does it mean the maximum thread that can run in parallel is 32?

3: for the private and shared variable

-- 3.1 the x and y are only for read, it is necessary to set them as private?

--3.2 the diameter is concurrently read and written. if it is shared I guess it may cause some delay, but if it is private how can I get a single truth of the value

1 Answer 1

0

Answers :

  1. There are total x iterations not x*y iterations, you should add collapse(2) to make it x*y iterations : #pragma omp parallel for collapse(2)

    the omp will use $OMP_NUM_THREADS which is generally equal to the number of machine threads , you can change it by : export OMP_NUM_THREADS= n_threads in Linux or set OMP_NUM_THREADS=n_threads in Windows. but it is not recommended to set it > number of machine threads.

  2. see answer 1.

    1. you have not to set x and y as private if they are declared outside the loops.
    2. you can not set diameter as private nor as shared. I think you should change your code from : if(diameter<array[i][j]) {diameter=array[i][j]} to diameter = max(diameter,array[i][j]) and then use reduction reduction(max:diameter) to calculate diameter

the code must look like this:

#pragma omp parallel for collapse(2) reduction(max:diameter)
    for (int i=1;i<=x;++i){
        for (int j=1;j<=y;++j){
                diameter = max(diameter,array[i][j]);
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

" omp will use min( $OMP_NUM_THREADS ,x threads (or xy if you use collapse) ) ". *WRONG The number of threads is unrelated to the number of iterations in the loop. See the algorithm in the OpenMP standard (available from openmp.org/specifications ). You'll find no mention of any loops in it.
"if you have 2 iterations can you run 4 threads?!.". Yes, you can absolutely have four threads. Of course two of them don't have any work to do if the loop only has two iterations, so are merely overhead, but they exist (check omp_get_num_threads()). The specifications matter...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.