OpenBLAS比内置函数dot_product慢

3
我需要在Fortran中进行点积运算。我可以使用Fortran的内置函数dot_product或使用OpenBLAS中的ddot。问题是ddot较慢。这是我的代码:

使用BLAS:

program VectorBLAS
! time VectorBlas.e = 0.30s
implicit none
double precision, dimension(3)  :: b
double precision                :: result
double precision, external      :: ddot
integer, parameter              :: LargeInt_K = selected_int_kind (18)
integer (kind=LargeInt_K)        :: I

DO I = 1, 10000000
   b(:) = 3
   result = ddot(3, b, 1, b, 1)
END DO
end program VectorBLAS

使用 dot_product

program VectorModule
! time VectorModule.e = 0.19s
implicit none
double precision, dimension (3)  :: b
double precision                 :: result
integer, parameter              :: LargeInt_K = selected_int_kind (18)
integer (kind=LargeInt_K)        :: I

DO I = 1, 10000000
  b(:) = 3
  result = dot_product(b, b)
END DO
end program VectorModule

这两个代码使用以下方式进行编译:

gfortran file_name.f90 -lblas -o file_name.e

我做错了什么?BLAS不应该更快吗?

非常相关的内容 https://dev59.com/2pTfa4cB1Zd3GeqPX_Og#36035152 - Vladimir F Героям слава
1个回答

4

尽管BLAS,特别是优化版本,通常对于较大的数组更快,但内置函数对于较小的大小更快。

这在ddot的链接源代码中尤其明显,因为额外的工作花费在进一步的功能上(例如,不同的增量)。对于小数组长度,这里所做的工作超过了优化的性能提升。

如果您使向量变得(更)大,则优化版本应该更快。

以下是一个示例以说明这一点:

program test
  use, intrinsic :: ISO_Fortran_env, only: REAL64
  implicit none
  integer                   :: t1, t2, rate, ttot1, ttot2, i
  real(REAL64), allocatable :: a(:),b(:),c(:)
  real(REAL64), external    :: ddot

  allocate( a(100000), b(100000), c(100000) )
  call system_clock(count_rate=rate)

  ttot1 = 0 ; ttot2 = 0
  do i=1,1000
    call random_number(a)
    call random_number(b)

    call system_clock(t1)
    c = dot_product(a,b)
    call system_clock(t2)
    ttot1 = ttot1 + t2 - t1

    call system_clock(t1)
    c = ddot(100000,a,1,b,1)
    call system_clock(t2)
    ttot2 = ttot2 + t2 - t1
  enddo
  print *,'dot_product: ', real(ttot1)/real(rate) 
  print *,'BLAS, ddot:  ', real(ttot2)/real(rate) 
end program

BLAS例程在这里要快得多:
OMP_NUM_THREADS=1 ./a.out 
 dot_product:   0.145999998    
 BLAS, ddot:    0.100000001  

@F.N.B 注意:这也取决于您使用的BLAS库的实现方式以及它是如何编译的。MKL在英特尔CPU上非常高效,如果您只是从发行版的软件包存储库中安装了openBLAS,则可能无法理想地针对您的架构进行调整。 - Anthony Scemama

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接