1、 However, the parallel version computes the sum in a different order than the serial version; some of the quantities added are quite small, and so this will affect the accuracy of the results. Modified: 29 January 2003 Author: John Burkardt A FORTRAN 90 module may be available: use omp_lib A FORTRAN
2、 77 include file may be available: include omp_lib.h implicit none integer, parameter : r4_logn_max = 9 integer id integer nthreads integer omp_get_num_procs integer omp_get_num_threads integer omp_get_thread_num call timestamp ( ) write ( *, (a) ) TEST_OMP FORTRAN90 version Estimate the value of PI
3、 by summing a series. This program includes Open MP directives, which may be used to run the program in parallel. The number of processors available:(a,i8) OMP_GET_NUM_PROCS () = , omp_get_num_procs ( ) nthreads = 4(a,i8,a) Call OMP_SET_NUM_THREADS, and request , & nthreads, threads. call omp_set_nu
4、m_threads ( nthreads ) Note that the call to OMP_GET_NUM_THREADS will always return 1 if called outside a parallel region!$OMP parallel private ( id ) id = omp_get_thread_num ( )(a,i3) This is process , id if ( id = 0 ) then Calling OMP_GET_NUM_THREADS inside a parallel region, we get the number of
5、threads is , omp_get_num_threads ( ) end if$OMP end parallel call r4_test ( r4_logn_max ) Normal end of execution. stopendsubroutine r4_test ( logn_max ) R4_TEST estimates the value of PI using single precision. PI is estimated using N terms. N is increased from 102 to 10LOGN_MAX. The calculation is
6、 repeated using both sequential and Open MP enabled code. Wall clock time is measured by calling SYSTEM_CLOCK. 06 January 2003 integer clock_max integer clock_rate integer clock_start integer clock_stop real error real estimate integer logn integer logn_max character ( len = 3 ) mode integer n real
7、r4_pi real timeR4_TEST: Estimate the value of PI, using single precision arithmetic. N = number of terms computed and added; ESTIMATE = the computed estimate of PI; ERROR = ( the computed estimate - PI ); TIME = elapsed wall clock time; Note that you cant increase N forever, because: A) ROUNDOFF sta
8、rts to be a problem, and B) maximum integer size is a problem.(a,i12) The maximum integer: , huge ( n ) N Mode Estimate Error Time n = 1 do logn = 2, logn_max mode = OMP call system_clock ( clock_start, clock_rate, clock_max ) call r4_pi_est_omp ( n, estimate ) call system_clock ( clock_stop, clock_
9、rate, clock_max ) time = real ( clock_stop - clock_start ) / real ( clock_rate ) error = abs ( estimate - r4_pi ( ) )( i12, 2x, a3, 2x, f14.10, 2x, g14.6, 2x, g14.6 ) ) & n, mode, estimate, error, time n = n * 10 end do returnsubroutine r4_pi_est_omp ( n, estimate ) R4_PI_EST_OMP estimates the value
10、 of PI, using Open MP. The calculation is based on the formula for the indefinite integral: Integral 1 / ( 1 + X*2 ) dx = Arctan ( X ) Hence, the definite integral Integral ( 0 = X = 1 ) 1 / ( 1 + X*2 ) dx = Arctan ( 1 ) - Arctan ( 0 ) = PI / 4. A standard way to approximate an integral uses the mid
11、point rule. If we create N equally spaced intervals of width 1/N, then the midpoint of the I-th interval is X(I) = (2*I-1)/(2*N). The approximation for the integral is then: Sum ( 1 = I = N ) (1/N) * 1 / ( 1 + X(I)*2 ) In order to compute PI, we multiply this by 4; we also can pull out the factor of
12、 1/N, so that the formula you see in the program looks like: ( 4 / N ) * Sum ( 1 = N ) 1 / ( 1 + X(I)*2 ) Until roundoff becomes an issue, greater accuracy can be achieved by increasing the value of N. Parameters: Input, integer N, the number of terms to add up. Output, real ESTIMATE, the estimated
13、value of pi. real h integer i real sum2 real x h = 1.0E+00 / real ( 2 * n ) sum2 = 0.0E+00$OMP parallel do private(x) shared(h) reduction(+: sum2) do i = 1, n x = h * real ( 2 * i - 1 ) sum2 = sum2 + 1.0E+00 / ( 1.0E+00 + x*2 ) estimate = 4.0E+00 * sum2 / real ( n )function r4_pi ( ) R4_PI returns t
14、he value of pi. 02 February 2000 Output, real R4_PI, the value of pi. r4_pi = 3.14159265358979323846264338327950288419716939937510E+00subroutine timestamp ( ) TIMESTAMP prints the current YMDHMS date as a time stamp. Example: May 31 2001 9:45:54.872 AM 31 May 2001 None character ( len = 8 ) ampm int
15、eger d character ( len = 8 ) date integer h integer m integer mm character ( len = 9 ), parameter, dimension(12) : month = (/ &January , February March April May June July August SeptemberOctober November December /) integer s character ( len = 10 ) time integer values(8) integer y character ( len =
16、 5 ) zone call date_and_time ( date, time, zone, values ) y = values(1) m = values(2) d = values(3) h = values(5) n = values(6) s = values(7) mm = values(8) if ( h 12 ) then ampm = AM else if ( h = 12 ) then if ( n = 0 .and. s = 0 ) thenNoon elsePM h = h - 12Midnight(a,1x,i2,1x,i4,2x,i2,a1,i2.2,a1,i2.2,a1,i3.3,1x,a) trim ( month(m) ), d, y, h, , n, , s, ., mm, trim ( ampm )=COPY上面的程序,可以完全运行成功,运行界面如下:
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1