1

I wrote these two programs to test OMP PARALLEL:

1 : my_idx is declared at the beginning of the program and after a private copy for eah thread is done.

program my_pgm

    use omp_lib
    implicit none
    
    integer :: my_idx
    
    CALL OMP_set_dynamic(.FALSE.)
    CALL OMP_set_num_threads(2)
    
    !$OMP PARALLEL DEFAULT(SHARED) PRIVATE(my_idx)
 
    DO my_idx = 0, 3
        WRITE(*,*) 'my_idx , thread_num, ',my_idx,omp_get_thread_num()
    END DO
    
    !$OMP END PARALLEL
    
end program my_pgm

2 : my_idx is declared in a BLOCK after !$OMP PARALLEL

program my_pgm

    use omp_lib
    implicit none
        
    CALL OMP_set_dynamic(.FALSE.)
    CALL OMP_set_num_threads(2)
    
    !$OMP PARALLEL DEFAULT(SHARED)
    
    BLOCK

        integer :: my_idx
 
        DO my_idx = 0, 3
            WRITE(*,*) 'my_idx , thread_num, ',my_idx,omp_get_thread_num()
        END DO
    
    END BLOCK
    
    !$OMP END PARALLEL
    
end program my_pgm

What are the pros and the cons of these two programs ? Or are they equivalent ?

5
  • 2
    Did you see any differences in your tests? I highly doubt you would see any difference. The generated Assemblies for your programs are identical, at least by gfortran: godbolt.org/z/7W5c6nM5M , godbolt.org/z/a6MjYhcs6 Other compilers (like Intel) treat block constructs slightly differently. But again that should not lead to tangible differences. Commented Apr 3 at 16:35
  • @Scientist. Thanks a lot. Commented Apr 3 at 16:44
  • 1
    All loop iteration variables in a parallel region are private by default. So, you don't even need the explicit private clause in this case. Commented Apr 3 at 17:59
  • 1
    However I would much, much prefer to see default(none) and you've missed out the what is often the best way of enforcing the scoping - putting the parallel region around a call to a procedure Commented Apr 3 at 18:15
  • @IanBush. Thanks a lot. You answered to a question before I asked Commented Apr 3 at 18:54

0

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.