XL Fortran for AIX 8.1

User's Guide


Example 4 - Invalid Fortran SMP Source File

!*****************************************************************
!* In this example, fort_sub is invoked by multiple threads.     *
!*                                                               *
!* This example is not valid because                             *
!*  fort_sub and another_sub both declare /block/ to be          *
!*  THREADLOCAL. They intend to share the common block, but      *
!*  they are executed via different threads.                     *
!*                                                               *
!* To "fix" this problem, one of the following approaches can    *
!* be taken:                                                     *
!*  (1) The code for another_sub should be brought into the loop.*
!*  (2) "j" should be passed as an argument to another_sub, and  *
!*       the declaration for /block/ should be removed from      *
!*       another_sub.                                            *
!*  (3) The loop should be marked as "do not parallelize" by     *
!*      using the directive "!SMP$ PARALLEL DO  IF(.FALSE.)".    *
!*****************************************************************
 
subroutine fort_sub()
 
  common /block/ j
  integer :: j
  !IBM* THREADLOCAL /block/         ! Each thread executing fort_sub
                                    ! obtains its own copy of /block/.
  integer a(10)
 
  ...
  !IBM* INDEPENDENT
  do index = 1,10
    call another_sub(a(i))
  enddo
  ...
 
end subroutine fort_sub
 
subroutine another_sub(aa)          ! Multiple threads are used to
  integer aa                        ! execute another_sub.
  common /block/ j                  ! Each thread obtains a new copy
  integer :: j                      ! of the common block /block/.
  !IBM* THREADLOCAL /block/
 
  aa = j                            ! The value of "j" is undefined.
end subroutine another_sub
 


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]