A simple example:
module parameters implicit none integer :: i,step integer :: nt=5 integer :: nelectron=5 integer :: num_threads=2 real(8) :: vt=855555.0 real(8) :: dt=1.d-5 real(8) :: vx1_old,vy1_old,vz1_old,t1,t2,x_old,y_old real(8) :: x_length=0.0 real(8) :: y_length=0.0 real(8) :: vx1_new,vy1_new,vz1_new,vzstore,x_new,y_new end module parameters program main use parameters use omp_lib implicit none integer :: thread_num !$ call omp_set_num_threads(num_threads) !$ call omp_set_nested(.false.) call cpu_time(t1) !$omp parallel !$omp& default(private) shared(x_length,y_length) !$omp& schedule(static,chunk) !$omp& reduction(+:x_length,y_length) !$omp do do i=1,nelectron do step=1,nt if(step==1)then vx1_new=1.0 vy1_new=1.0 vz1_new=1.0 x_new=1.0 y_new=1.0 endif thread_num=omp_get_thread_num() write(*,*)"thread_num",thread_num write(*,*)"i",i write(*,*)"step",step write(*,*) vx1_old=vx1_new vy1_old=vy1_new vz1_old=vz1_new x_old=x_new y_old=y_new x_length=x_length+x_old y_length=y_length+y_old enddo enddo !$omp end do !$omp end parallel call cpu_time(t2) write(*,*)"x length=",x_length write(*,*)"y length=",y_length end program main When I output the thread doing the actual work with i and step, I see somewhere weird:
As you can see, thread 0 is doing the i=6,step=1 while thread 1 is doing i=6,step=2.Why it changed thread when doing the same i? and How can I avoid this kind of situation. Meaning for each i, inner loop step is done on the same thread.
cpu_time()in parallel programs and never trust the ordering of output from parallel programs. Two rules you must always follow.Warning: !$OMP at (1) starts a commented line as it neither is followed by a space nor is a continuation line nested_loop.f90:27.8:steploop for eachiis done on the same thread or not, it is very important in my situation.