program main use omp_lib implicit none integer ( kind = 4 ), parameter :: n = 10000 real ( kind = 8 ) a real ( kind = 8 ) b integer ( kind = 4 ) i integer ( kind = 4 ) it integer ( kind = 4 ) my_id integer ( kind = 4 ) my_seed real ( kind = 8 ) q real ( kind = 8 ) r8_uniform_01 integer ( kind = 4 ) seed real ( kind = 8 ) :: tol = 1.0D-10 real ( kind = 8 ) u real ( kind = 8 ) w real ( kind = 8 ) wtime real ( kind = 8 ) x write ( *, '(a)' ) ' ' write ( *, '(a,i8)' ) ' Number of processors available = ', omp_get_num_procs ( ) write ( * ,'(a,i8)' ) ' Number of threads = ', omp_get_max_threads ( ) seed = 123456789 wtime = omp_get_wtime ( ) q = 0.0D+00 ! !$omp parallel private ( a, b, i, it, my_id, my_seed, u, w, x ) & !$omp shared ( seed, tol ) my_id = omp_get_thread_num ( ) my_seed = seed + my_id write ( *, * ) my_id, my_seed !$omp do reduction ( + : q ) do i = 1, n u = r8_uniform_01 ( my_seed ) x = ( 1.0 - u ) * 1.0 + u * 100.0 w = x + log ( x ) w = 1 it = 0 do if ( 100 < it ) then exit end if if ( abs ( x - w * exp ( w ) ) < & tol * abs ( ( w + 1.0D+00 ) * exp ( w ) ) ) then exit end if a = w * exp ( w ) - x b = ( w + 1.0D+00 ) * exp ( w ) & - ( w + 2.0D+00 ) * ( w * exp ( w ) - x ) & / ( 2.0D+00 * w + 2.0D+00 ) w = w - a / b it = it + 1 end do q = q + w end do !$omp end do !$omp end parallel q = ( 100.0 - 1.0 ) * q / real ( n, kind = 8 ) write ( *, '(a,g14.6)' ) 'Q = ', q wtime = omp_get_wtime ( ) - wtime write ( *, '(g14.6,a)' ) wtime, ' seconds.' return end function r8_uniform_01 ( seed ) !*****************************************************************************80 ! !! R8_UNIFORM_01 returns a unit pseudorandom R8. ! ! Discussion: ! ! An R8 is a real ( kind = 8 ) value. ! ! For now, the input quantity SEED is an integer variable. ! ! This routine implements the recursion ! ! seed = 16807 * seed mod ( 2^31 - 1 ) ! r8_uniform_01 = seed / ( 2^31 - 1 ) ! ! The integer arithmetic never requires more than 32 bits, ! including a sign bit. ! ! If the initial seed is 12345, then the first three computations are ! ! Input Output R8_UNIFORM_01 ! SEED SEED ! ! 12345 207482415 0.096616 ! 207482415 1790989824 0.833995 ! 1790989824 2035175616 0.947702 ! ! Licensing: ! ! This code is distributed under the GNU LGPL license. ! ! Modified: ! ! 05 July 2006 ! ! Author: ! ! John Burkardt ! ! Reference: ! ! Paul Bratley, Bennett Fox, Linus Schrage, ! A Guide to Simulation, ! Springer Verlag, pages 201-202, 1983. ! ! Pierre L'Ecuyer, ! Random Number Generation, ! in Handbook of Simulation, ! edited by Jerry Banks, ! Wiley Interscience, page 95, 1998. ! ! Bennett Fox, ! Algorithm 647: ! Implementation and Relative Efficiency of Quasirandom ! Sequence Generators, ! ACM Transactions on Mathematical Software, ! Volume 12, Number 4, pages 362-376, 1986. ! ! Peter Lewis, Allen Goodman, James Miller ! A Pseudo-Random Number Generator for the System/360, ! IBM Systems Journal, ! Volume 8, pages 136-143, 1969. ! ! Parameters: ! ! Input/output, integer ( kind = 4 ) SEED, the "seed" value, which should ! NOT be 0. On output, SEED has been updated. ! ! Output, real ( kind = 8 ) R8_UNIFORM_01, a new pseudorandom variate, ! strictly between 0 and 1. ! implicit none integer ( kind = 4 ), parameter :: i4_huge = 2147483647 integer ( kind = 4 ) k real ( kind = 8 ) r8_uniform_01 integer ( kind = 4 ) seed if ( seed == 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'R8_UNIFORM_01 - Fatal error!' write ( *, '(a)' ) ' Input value of SEED = 0.' stop end if k = seed / 127773 seed = 16807 * ( seed - k * 127773 ) - k * 2836 if ( seed < 0 ) then seed = seed + i4_huge end if r8_uniform_01 = real ( seed, kind = 8 ) * 4.656612875D-10 return end