Код:
program ompPrime
#ifdef _OPENMP
include 'omp_lib.h' !needed for OMP_GET_NUM_THREADS()
#endif
integer :: start = 1
integer :: end = 100000000
integer :: number_of_primes = 0
integer :: number_of_41primes = 0
integer :: number_of_43primes = 0
integer index, factor, limit, nthr
double precision st, fin
real rindex, rlimit
logical prime, print_primes
CALL CPU_TIME(st)
print_primes = .false.
nthr = 1 ! assume just one thread
print *, ' Range to check for Primes:',start,end
#ifdef _OPENMP
!$omp parallel
!$omp single
nthr = OMP_GET_NUM_THREADS()
print *, ' We are using',nthr,' thread(s)'
!$omp end single!
!$omp do private(factor, limit, prime) &
SCHEDULE(dynamic,10) &
reduction(+:number_of_primes,number_of_41primes,number_of_43primes)
#else
print *, ' We are using',nthr,' thread(s)'
#endif
do index = start, end, 2 !workshared loop
limit = int(sqrt(real(index)))
prime = .true. ! assume number is prime
factor = 3
do
if(prime .and. factor .le. limit) then
if(mod(index,factor) .eq. 0) then
prime = .false.
endif
factor = factor + 2
else
exit ! we can jump out of non-workshared loop
endif
enddo
if(prime) then
if(print_primes) then
print *, index, ' is prime'
endif
number_of_primes = number_of_primes + 1
if(mod(index,4) .eq. 1) then
number_of_41primes = number_of_41primes + 1
endif
if(mod(index,4) .eq. 3) then
number_of_43primes = number_of_43primes + 1
endif
endif ! if(prime)
enddo
!$omp end do
!$omp end parallel
CALL CPU_TIME(fin)
print '("Time = ",1P6E12.4," seconds.")',fin-st
print *,number_of_primes
read *,number_of_primes
end program ompPrime