when two programs are ran,why does it take the same time?

when two programs are ran,why does it take the same time?How to reflect Parallel ?
program A:
program main

implicit none

integer ::i,j,k
REAL time_begin, time_end
!$ call OMP_SET_NUM_THREADS(2)
CALL CPU_TIME ( time_begin )
!$omp parallel do
do k=1,1000000000
i=i+k
j=2*k+1
enddo
!$omp end parallel do
CALL CPU_TIME ( time_end )
print *,“i=”,i,“j=”,j
PRINT *, 'Time of operation was ‘, time_end - time_begin, ’ seconds’
end program main



program B:
program main

implicit none

integer ::i,j,k
REAL time_begin, time_end
CALL CPU_TIME ( time_begin )
do k=1,1000000000
i=i+k
j=2*k+1
enddo
CALL CPU_TIME ( time_end )
print *,“i=”,i,“j=”,j
PRINT *, 'Time of operation was ‘, time_end - time_begin, ’ seconds’
end program main

Hi easydavid,

In this case, I think the problem is that I and j are shared variables so you’re getting memory contention accessing them. You could privatize them using the “private” clause, but this would yield wrong answers. Granted, this code is not parallel so gets wrong answers anyway.

Note, since “i” is a reduction variable, you may want to investigate the use of the “reduction” clause.

  • Mat

Hi,mkcolg
Thank your answer.
Can you give me a simple parallel program that has been running successful?

Here you go. I just modified your example a bit. Made “i” a reduction variable and “j” a lastprivate. Other things: I initialized “i”, otherwise you’ll get random results and had “i” sum “j”'s value.

  • Mat
% cat testmp.f90 
program main

implicit none

integer ::i,j,k
REAL time_begin, time_end
CALL CPU_TIME ( time_begin )
i=0
!$omp parallel do reduction(+:i), lastprivate(j)
do k=1,1000000000
j=2*k+1
i=i+j
enddo
!$omp end parallel do
CALL CPU_TIME ( time_end )
print *,"i=",i,"j=",j
PRINT *, 'Time of operation was ', time_end - time_begin, ' seconds'
end program main 
% pgf90 -fast testmp.f90 -mp -o mp.out
% pgf90 -fast testmp.f90 -o serial.out
% setenv OMP_NUM_THREADS 4
% serial.out
 i=    513381376 j=   2000000001
 Time of operation was    0.5399642      seconds
% mp.out
 i=    513381376 j=   2000000001
 Time of operation was    0.1359909      seconds

Thank you very much
but,when I running these programs and get the following results:

%program A
program main

implicit none

integer ::i,j,k
REAL time_begin, time_end
CALL CPU_TIME ( time_begin )
i=0
!$omp parallel do reduction(+:i), lastprivate(j)
do k=1,1000000000
j=2*k+1
i=i+j
enddo
!$omp end parallel do
CALL CPU_TIME ( time_end )
print *,“i=”,i,“j=”,j
PRINT *, 'Time of operation was ‘, time_end - time_begin, ’ seconds’
end program main


%program B
program main

implicit none

integer ::i,j,k
REAL time_begin, time_end
CALL CPU_TIME ( time_begin )
i=0
do k=1,1000000000
j=2*k+1
i=i+j
enddo
CALL CPU_TIME ( time_end )
print *,“i=”,i,“j=”,j
PRINT *, 'Time of operation was ‘, time_end - time_begin, ’ seconds’
end program main

%
%
i= 513381376 j= 2000000001
Time of operation was 3.593000 seconds
%
%
i= 513381376 j= 2000000001
Time of operation was 3.640000 seconds

Did you set OMP_NUM_THREADS?

  • Mat

yes,I do.
The code is’’ !$call OMP_SET_NUM_THREADS(2)‘’

I don’t see the call in the second example you posted. Try setting it in your environment.

Bash: export OMP_NUM_THREADS=2
Csh: setenv OMP_NUM_THREADS 2

Another option is to compile with “-mp=allcores”. This sets the default to use all available cores on a system.

  • Mat

How to compile with “-mp=allcores”?

pgf90 -fast testmp.f90 -mp=allcores -o mp.out