0

It seems many people have similar issues with OpenMP but I couldn't find a solution to my problem.

I'm using a simple code:


PROGRAM Parallel_Hello_World

USE OMP_LIB

integer :: i
real ::  start, finish, B
call cpu_time(start)
!$OMP PARALLEL
!$OMP DO
DO i = 1,2000000
B = cos(cos(cos(sin(cos(sqrt(sqrt(sqrt( cos( real(i) ) ))))))))
B = cos(cos(cos(sin(cos(sqrt(sqrt(sqrt( cos( B ) ))))))))
B = cos(cos(cos(sin(cos(sqrt(sqrt(sqrt( cos( B ) ))))))))
END DO
!$OMP END DO
!$OMP END PARALLEL

call cpu_time(finish)
  print '("Time  = ",f6.3," seconds.")',finish-start

END

I'm confused where the overhead is coming from. Even when I increase the amount of sin/cos/sqrt operations, lower threads always wins.

export OMP_NUM_THREADS=1 Time = 1.58 seconds. (average)
export OMP_NUM_THREADS=8 Time = 2.376 seconds. (average)
Compile: ifort para.f90 -o para.exe -qopenmp -O2

the Intel compiler is from 2020.

ashbre87
  • 9
  • 1
  • If instead you measure the time with system_clock what do you get? I don't know about Intel but for some compilers cpu_time reports the time summed across all the threads used by the process. – Ian Bush Feb 23 '21 at 07:37
  • Also how many cores does the machine you are using have? – Ian Bush Feb 23 '21 at 07:38
  • OK see https://community.intel.com/t5/Intel-Fortran-Compiler/What-does-CPU-TIME-return-on-multi-threaded-applications/td-p/879137 and https://stackoverflow.com/questions/6878246/fortran-intrinsic-timing-routines-which-is-better-cpu-time-or-system-clock . I'll leave it to others to say if this is a duplicate – Ian Bush Feb 23 '21 at 08:11
  • 3
    You are also writing to and reading from the same **shared** variable `B` from many threads at the same time. That is a **race condition**. Not only it will be slow, but your result will also be wrong. Please note that the code makes very little mathematical sense because you only use the result from the last iteration in your sequential code. Please use something that makes some actual sense for parallelization. – Vladimir F Героям слава Feb 23 '21 at 09:32

0 Answers0