program main c*********************************************************************72 c cc PRIME computes the sum of the prime numbers between 2 and 100,000. c c Discussion: c c This program is very close to being an MPI program already. c c The calculation has been split up among P processes, each of c which is given a range to work in, and each of which returns a c partial sum and CPU time. c c Modify the program so that it runs under MPI. c implicit none integer i integer j integer id integer id_n_hi integer id_n_lo double precision id_time integer id_total integer n_hi parameter ( n_hi = 100000 ) integer n_lo parameter ( n_lo = 2 ) integer p parameter ( p = 10 ) logical prime double precision wtime double precision wtime_total double precision wtime1 double precision wtime2 integer total total = 0 c c We pretend there are P processes. c We divide the range [N_LO,N_HI] into P roughly equal subintervals. c We ask process ID to check the ID-th subinterval and return the c total in that range. c 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 id_total = 0 do i = id_n_lo, id_n_hi prime = .true. do j = 2, i - 1 if ( mod ( i, j ) .eq. 0 ) then prime = .false. go to 10 end if end do if ( prime ) then id_total = id_total + i end if 10 continue end do write ( *, '(2x,i8,2x,i8,2x,i8,2x,i12,2x,g14.6)' ) & id, id_n_lo, id_n_hi, id_total total = total + id_total end do write ( *, '(a)' ) ' ' write ( *, '(2x,a8,2x,i8,2x,i8,2x,i12,2x,g14.6)' ) & ' Total', n_lo, n_hi, total stop end