Q: parallel do mistake in fortran program main
use omp_lib
implicit none
integer :: n=8
integer :: i, j, myid, a(8, 8), b, c(8)

! Generate a 8*8 array   A                                                                                         
!$omp parallel default(none), private(i, myid), &                                                                
!$omp shared(a, n)                                                                                               
myid = omp_get_thread_num()+1
do i = 1, n
   a(i, myid) = i*myid
end do
!$omp end   parallel                                                                                               

! Array A                                                                                                        
print*, 'Array A is'
do i = 1, n
 print*, a(:, i)
end do

! Sum of array A                                                                                                 
b = 0
!$omp parallel reduction(+:b), shared(a, n), private(i, myid)                                                    
myid = omp_get_thread_num()+1
do i = 1, n
   b = b + a(i, myid)
end do
!$omp end   parallel                                                                                               
print*, 'Sum of array A by reduction is ', b

b = 0
c = 0
!$omp parallel do                                                                                                
do i = 1, n
   do j = 1, n
      c(i) = c(i) + a(j, i)
   end do
end do
!$omp end parallel    do                                                                                            
print*, 'Sum of array A by using parallel do is', sum(c)

!$omp parallel do                                                                                                
do i = 1, n
   do j = 1, n
      b = b + a(j, i)
   end do
end do
!$omp end parallel do                                                                                            
print*, 'Sum of array A by using parallel do in another way is', b

end program main

I wrote a piece of Fortran code above to implement OpenMP to sum up all elements in a 8*8 array in three different ways. First one uses reduction and works. Second, I created a one dimension array with 8 elements. I sum up each column in parallel region and then sum them up. And this works as well. Third one I used an integer to sum up every element in array, and put it in parallel do region. This result is not correct and varies every time. I don't understand why this situation happens. Is because didn't specify public and private or the variable b is overwritten in the procedure? 

A: There is a race condition on b on your third scenario: several threads are reading and writing the same variable without proper synchronization / privatization.
Note that you don't have a race condition in the second scenario: each thread is updating  some data (i.e. c(i)) that no one else is accessing.
Finally, some solutions to your last scenario:


*

*Add the reducion(+:b) clause to the pragma

*Add a pragma omp atomic directive before the b = b + c(j,i) expression

*You can implement a manual privatization

