program bruteforce
use mpi
implicit none
integer :: n,m,noprime,total,nmin,nmax
integer :: rank, nb_mpi_processes, ierror, hostname_len
call MPI_INIT(ierror) ! Init MPI (init MPI_COMM_WORLD communicator, set rank to each process, etc)
call MPI_COMM_SIZE(MPI_COMM_WORLD, nb_mpi_processes, ierror) ! Ask the number of MPI processes running
call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierror) ! Ask the rank of the current process
total=0
nmin=(20000000/nb_mpi_processes)*rank+1
nmax=(20000000/nb_mpi_processes)*(rank+1)
print *,'rank',rank,nmin,nmax
do n=nmin,nmax
if(modulo(n,2) == 0) then; noprime = 1
elseif(modulo(n,3) == 0) then; noprime = 1
else
do m=4,int(sqrt(real(n)))
if(modulo(n,m) == 0) then
noprime = 1
exit
end if
end do
end if
if(noprime == 1) then
noprime = 0
else
total = total + 1
end if
end do
print *, rank,'Found ', total, ' prime numbers'
call MPI_FINALIZE ( ierror ) ! Close MPI
end program bruteforce
program bruteforce
use mpi
implicit none
integer :: n,m,noprime,total,nmin,nmax
integer :: rank, nb_mpi_processes, ierror, hostname_len
call MPI_INIT(ierror) ! Init MPI (init MPI_COMM_WORLD communicator, set rank to each process, etc)
call MPI_COMM_SIZE(MPI_COMM_WORLD, nb_mpi_processes, ierror) ! Ask the number of MPI processes running
call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierror) ! Ask the rank of the current process
total=0
nmin=(20000000/nb_mpi_processes)*rank+1
nmax=(20000000/nb_mpi_processes)*(rank+1)
print *,'rank',rank,nmin,nmax
do n=nmin,nmax
if(modulo(n,2) == 0) then; noprime = 1
elseif(modulo(n,3) == 0) then; noprime = 1
else
do m=4,int(sqrt(real(n)))
if(modulo(n,m) == 0) then
noprime = 1
exit
end if
end do
end if
if(noprime == 1) then
noprime = 0
else
total = total + 1
!print *, n, ' is prime',total
end if
end do
print *, rank,'Found ', total, ' prime numbers'
call MPI_FINALIZE ( ierror ) ! Close MPI
end program bruteforce