Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!
26755 Discussions

Why my parallel code by OpenMP is much slower than the sequential code?

Eric_R_
Beginner
63 Views

I tried to use OpenMP to parallelize an inner loop. The code is as the following

******************************************************************************************

program test  
implicit none
REAL(8)::time1,time2
real(8),allocatable,dimension(:)::b
integer:: k,j,i
j=70000
allocate(b(j))
b=0.0D+00
call cpu_time(time1)
do k=1,40000
 do i=1,j
  b(i)=real(i,kind=8)
 enddo
enddo
call cpu_time(time2)
write(*,*)'The cpu time (s) by the sequential code is',time2-time1
call cpu_time(time1)
do k=1,40000
 !$OMP PARALLEL DO private(i)
 do i=1,j
  b(i)=real(i,kind=8)
 enddo
 !$OMP END PARALLEL DO 
enddo
call cpu_time(time2)
write(*,*)'The cpu time (s) by the parallel code is',time2-time1
 
end program test
 
******************************************************************************************
The file name of the code is 'test.f90'. I built from the command line:
ifort /Qopenmp test.f90
 
The results are:
 

The cpu time (s) by the sequential code is   1.98437500000000
The cpu time (s) cost by the parallel code is   3.85937500000000

The parallel code is much slower than the sequential code. Why?

I will greatly appreciate your contribution in this problem.

Thanks a lot.

 

0 Kudos
0 Replies
Reply