First the code. Througn judging a large number a prime, I test the OpenMP Parallel.
program main use portlib implicit none integer :: i, j integer :: x(10000) = 0 ! #1 integer :: a(10000) = (/(i,i=100000001,100010000)/) real :: Elapsed_time Elapsed_time = TIMEF() !$omp parallel do do i = 1, 10000 x(i) = is_prime(a(i)) end do !$omp end parallel do Elapsed_time = TIMEF() write(*,*) ' Elapsed_time = ', Elapsed_time write(*,*) 'end' contains function is_prime(num) result(prime) integer, intent(in) :: num integer :: prime integer :: i prime = num do i=2,int(num/2) if(mod(num,i)==0) then prime = 0 exit end if end do end function is_prime end program main
Second, In line #1, now i initialize the x(10000) to 0, if i didn't do this, the result of x will be totally wrong, which you can see in the monitor. If I dellete all the OpenMP Directive and disable the setting in the proerties, even if i didn't initialize the x array ,the result will be correct.
I understand the share memory mechanism of the OpenMP, But i cannot understand these from this mechanism. Anyone can help me , thank you!