program main !*****************************************************************************80 ! !! PRIME computes the sum of the prime numbers between 2 and 100,000. ! ! Discussion: ! ! This program is very close to being an MPI program already. ! ! The calculation has been split up among P processes, each of ! which is given a range to work in, and each of which returns a ! partial sum and CPU time. ! ! Modify the program so that it runs under MPI. ! implicit none integer i integer id integer id_n_hi integer id_n_lo double precision id_time integer id_total integer j integer :: n_hi = 100000 integer :: n_lo = 2 integer :: p = 10 logical prime double precision wtime double precision wtime_total double precision wtime1 double precision wtime2 integer total write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'PRIME' write ( *, '(a)' ) ' FORTRAN90 version' total = 0 ! ! We PRETEND there are P processes. ! We divide the range [N_LO,N_HI] into P roughly equal subintervals. ! We ask process ID to check the ID-th subinterval and return the ! total in that range. ! write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' ID N_LO N_HI TOTAL TIME' write ( *, '(a)' ) ' ' do id = 0, p - 1 id_n_lo = ( ( p - id ) * n_lo & + ( id ) * ( n_hi + 1 ) ) & / ( p ) id_n_hi = ( ( p - id - 1 ) * n_lo & + ( id + 1 ) * ( n_hi + 1 ) ) & / ( p ) - 1 ! ! Begin the work done by one "process" ! id_total = 0 do i = id_n_lo, id_n_hi prime = .true. do j = 2, i - 1 if ( mod ( i, j ) == 0 ) then prime = .false. exit end if end do if ( prime ) then id_total = id_total + i end if end do ! ! "Send" results back to main process. ! total = total + id_total write ( *, '(2x,i8,2x,i8,2x,i8,2x,i12,2x,g14.6)' ) & id, id_n_lo, id_n_hi, id_total ! ! End of work of one process. ! end do write ( *, '(a)' ) ' ' write ( *, '(2x,a8,2x,i8,2x,i8,2x,i12,2x,g14.6)' ) & ' Total', n_lo, n_hi, total stop end