24.05.2014 Views

XL Fortran Enterprise Edition for AIX : User's Guide - IBM

XL Fortran Enterprise Edition for AIX : User's Guide - IBM

XL Fortran Enterprise Edition for AIX : User's Guide - IBM

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

close(10)<br />

open(10, file="fun10.out", <strong>for</strong>m="<strong>for</strong>matted")<br />

do j = 1, number_of_threads<br />

read(10, *) array1<br />

do i = 1, 1000<br />

if (array1(i) /= array(i)) then<br />

print *, "Result is wrong."<br />

stop<br />

endif<br />

end do<br />

end do<br />

close(10, status="delete")<br />

print *, "Normal ending."<br />

end<br />

subroutine f_mt_exec(thread_number)<br />

integer thread_number<br />

integer array(1000)<br />

common /x/ array<br />

write(10, *) array<br />

end<br />

Example 3 - Valid <strong>Fortran</strong> SMP Source File<br />

!*****************************************************************<br />

!* This example uses a PARALLEL construct and a DO construct *<br />

!* to calculate the value of pi. *<br />

!*****************************************************************<br />

program compute_pi<br />

integer n, i<br />

real*8 w, x, pi, f, a<br />

f(a) = 4.d0 /(1.d0 + a*a) !! function to integrate<br />

pi = 0.0d0<br />

!$OMP PARALLEL private(x, w, n), shared(pi)<br />

n = 10000<br />

!! number of intervals<br />

w = 1.0d0/n<br />

!! calculate the interval size<br />

!$OMP DO reduction(+: pi)<br />

do i = 1, n<br />

x = w * (i - 0.5d0)<br />

pi = pi + f(x)<br />

enddo<br />

!$OMP END DO<br />

!$OMP END PARALLEL<br />

print *, "Computed pi = ", pi<br />

end<br />

Example 4 - Invalid <strong>Fortran</strong> SMP Source File<br />

!*****************************************************************<br />

!* In this example, <strong>for</strong>t_sub is invoked by multiple threads. *<br />

!* *<br />

!* This example is not valid because *<br />

!* <strong>for</strong>t_sub and another_sub both declare /block/ to be *<br />

!* THREADLOCAL. They intend to share the common block, but *<br />

!* they are executed via different threads. *<br />

!* *<br />

!* To "fix" this problem, one of the following approaches can *<br />

!* be taken: *<br />

!* (1) The code <strong>for</strong> another_sub should be brought into the loop.*<br />

!* (2) "j" should be passed as an argument to another_sub, and *<br />

!* the declaration <strong>for</strong> /block/ should be removed from *<br />

!* another_sub. *<br />

408 <strong>XL</strong> <strong>Fortran</strong> <strong>Enterprise</strong> <strong>Edition</strong> <strong>for</strong> <strong>AIX</strong> : User’s <strong>Guide</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!