Vaidyanathan
Vaidyanathan

Reputation: 399

Nested DO loops in OPEN MP

I am trying to understand nested DO loops in OPENMP. There are many forums regarding this, but I did not find an answer for the following question.

Let us consider two scenarios. The question will be below the codes

Case a)

!$OMP PARALLEL DO
      DO i = 1,N
        DO j = 1,M
          ...code...
         END DO
       END DO

Case b)

!$OMP PARALLEL DO
      DO i = 1,N
      !$OMP PARALLEL DO
         DO j = 1,M
            ... code ...
          END DO
      END DO

Question. I am not sure whether the following statements are correct

  1. In case (a) the first loop will be shared by the threads, like say Thread 1 having i between say 1 and 10, thread 2 having i between 11 and 20 and so on and so forth. But here each thread will have j values between 1 and M, isn't it or will the second loop will also be forked between all threads like the first one?

  2. In case (b) thread 1 can have values of i between 1 and 10 and j between say 10 and 20 (and not the entire range).

Is this how the pattern works? I am sorry if I have failed to express my thoughts and the question is unclear.

Upvotes: 0

Views: 2817

Answers (1)

The second version makes difference only when nested parallelism in OpenMP is enabled. If yes, then it makes another level dividing of threads. If it is not enabled, it is ignored.

You cannot say how the threads exactly divide the indexes, because you do not specify SCHEDULE and the default is unspecified. But usually the default is implemented as static and the range of indexes is divided evenly.

The first parallel do divides the work among groups of threads with some values of i and the other one divides the values of j to individual threads inside these groups.

Upvotes: 3

Related Questions