program main !*****************************************************************************80 ! !! schedule_openmp() uses OpenMP to demonstrate scheduling. ! ! Discussion: ! ! This program demonstrates the difference between default, ! static and dynamic scheduling for a loop parallelized in OpenMP. ! ! The purpose of scheduling is to deal with loops in which there is ! known or suspected imbalance in the work load. In this example, ! if the work is divided in the default manner between two threads, ! the second thread has 3 times the work of the first. ! ! Both static and dynamic scheduling, if used, even out the work ! so that both threads have about the same load. This could be ! expected to decrease the run time of the loop by about 1/3. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 10 July 2010 ! ! Author: ! ! John Burkardt ! use omp_lib implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n integer n_factor integer n_hi integer n_lo integer primes real ( kind = rk ) time1 real ( kind = rk ) time2 real ( kind = rk ) time3 call timestamp ( ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'schedule_openmp():' write ( *, '(a)' ) ' FORTRAN90/OpenMP version' write ( *, '(a)' ) ' Count the primes from 1 to N.' write ( *, '(a)' ) & ' This is an unbalanced work load, particular for two threads.' write ( *, '(a)' ) ' Demonstrate default, static and dynamic scheduling.' write ( *, '(a)' ) ' ' write ( *, '(a,i8)' ) & ' Number of processors available = ', omp_get_num_procs ( ) write ( * ,'(a,i8)' ) & ' Number of threads = ', omp_get_max_threads ( ) n_lo = 1 n_hi = 131072 n_factor = 2 write ( *, '(a)' ) ' ' write ( *, '(a)' ) & ' Default Static Dynamic' write ( *, '(a)' ) & ' N Pi(N) Time Time Time' write ( *, '(a)' ) ' ' n = n_lo do while ( n <= n_hi ) time1 = omp_get_wtime ( ) call prime_default ( n, primes ) time1 = omp_get_wtime ( ) - time1 time2 = omp_get_wtime ( ) call prime_static ( n, primes ) time2 = omp_get_wtime ( ) - time2 time3 = omp_get_wtime ( ) call prime_dynamic ( n, primes ) time3 = omp_get_wtime ( ) - time3 write ( *, '(2x,i8,2x,i8,g14.6,g14.6,g14.6)' ) & n, primes, time1, time2, time3 n = n * n_factor end do ! ! Terminate. ! write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'schedule_openmp():' write ( *, '(a)' ) ' Normal end of execution.' call timestamp ( ) stop end subroutine prime_default ( n, total ) !*****************************************************************************80 ! !! prime_default() counts primes, using default scheduling. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 10 July 2010 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the maximum number to check. ! ! Output, integer TOTAL, the number of prime numbers up to N. ! implicit none integer i integer j integer n integer prime integer total total = 0 !$omp parallel & !$omp shared ( n ) & !$omp private ( i, j, prime ) !$omp do reduction ( + : total ) do i = 2, n prime = 1 do j = 2, i - 1 if ( mod ( i, j ) == 0 ) then prime = 0 exit end if end do total = total + prime end do !$omp end do !$omp end parallel return end subroutine prime_static ( n, total ) !*****************************************************************************80 ! !! prime_static() counts primes, using static scheduling. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 10 July 2010 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the maximum number to check. ! ! Output, integer TOTAL, the number of prime numbers up to N. ! implicit none integer i integer j integer n integer prime integer total total = 0 !$omp parallel & !$omp shared ( n ) & !$omp private ( i, j, prime ) !$omp do reduction ( + : total ) schedule ( static, 100 ) do i = 2, n prime = 1 do j = 2, i - 1 if ( mod ( i, j ) == 0 ) then prime = 0 exit end if end do total = total + prime end do !$omp end do !$omp end parallel return end subroutine prime_dynamic ( n, total ) !*****************************************************************************80 ! !! prime_dynamic() counts primes, using dynamic scheduling. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 10 July 2010 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the maximum number to check. ! ! Output, integer TOTAL, the number of prime numbers up to N. ! implicit none integer i integer j integer n integer prime integer total total = 0 !$omp parallel & !$omp shared ( n ) & !$omp private ( i, j, prime ) !$omp do reduction ( + : total ) schedule ( dynamic, 100 ) do i = 2, n prime = 1 do j = 2, i - 1 if ( mod ( i, j ) == 0 ) then prime = 0 exit end if end do total = total + prime end do !$omp end do !$omp end parallel return end subroutine timestamp ( ) !*****************************************************************************80 ! !! timestamp() prints the current YMDHMS date as a time stamp. ! ! Example: ! ! 31 May 2001 9:45:54.872 AM ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 18 May 2013 ! ! Author: ! ! John Burkardt ! implicit none character ( len = 8 ) ampm integer d integer h integer m integer mm character ( len = 9 ), parameter, dimension(12) :: month = (/ & 'January ', 'February ', 'March ', 'April ', & 'May ', 'June ', 'July ', 'August ', & 'September', 'October ', 'November ', 'December ' /) integer n integer s integer values(8) integer y call date_and_time ( values = 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 ) then ampm = 'Noon' else ampm = 'PM' end if else h = h - 12 if ( h < 12 ) then ampm = 'PM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Midnight' else ampm = 'AM' end if end if end if write ( *, '(i2.2,1x,a,1x,i4,2x,i2,a1,i2.2,a1,i2.2,a1,i3.3,1x,a)' ) & d, trim ( month(m) ), y, h, ':', n, ':', s, '.', mm, trim ( ampm ) return end