今天测试了Fortran OMP求$\Sigma_1^n i $ 串行和并行的速度。代码如下:
program main use OMP_LIB implicit non integer N,M,i, time_begin, time_end real(kind=8) t, a call OMP_set_num_threads(4) N=2000000 t=0.0 call SYSTEM_CLOCK(time_begin) do i=1,N a=float(i) t=t+float(i) M = OMP_get_num_threads() enddo call SYSTEM_CLOCK(time_end) write(*, "('t = ', F20.5, ' running on ', I3, ' threads,','using time =', I7)") t,M,time_end-time_begin t=0.0 call SYSTEM_CLOCK(time_begin) !$OMP PARALLEL DO do i = 1, N !$OMP CRITICAL a = float(i) t = t+a !$OMP END CRITICAL M = OMP_get_num_threads() enddo !$OMP end parallel do call SYSTEM_CLOCK(time_end) write(*, "('t = ', F20.5, ' running on ', I3, ' threads,','using time =', I7)") t,M,time_end-time_begin t=0.0 call SYSTEM_CLOCK(time_begin) !$OMP PARALLEL DO REDUCTION(+:t) do i = 1, N t = t+float(i) M = OMP_get_num_threads() enddo !$OMP end parallel do call SYSTEM_CLOCK(time_end) write(*, "('t = ', F20.5, ' running on ', I3, ' threads,','using time =', I7)") t,M,time_end-time_begin !The following codes are not correct. ! It is right only under the serial t=0.0 call SYSTEM_CLOCK(time_begin) !$OMP PARALLEL DO do i=1,N a=float(i) t=t+a M=OMP_get_num_threads() enddo !$OMP end parallel do call SYSTEM_CLOCK(time_end) write(*, "('t = ', F20.5, ' running on ', I3, ' threads,','using time =', I7)") t,M,time_end-time_begin end下面是我的运行结果:
t = 2000001000000.00000 running on 1 threads,using time = 31 t = 2000001000000.00000 running on 4 threads,using time = 4383 t = 2000001000000.00000 running on 4 threads,using time = 47 t = 540364135587.00000 running on 4 threads,using time = 62