【问题标题】:OpenBLAS slower than intrinsic function dot_productOpenBLAS 比内在函数 dot_product 慢
【发布时间】:2016-03-25 14:26:05
【问题描述】:

我需要在 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不是必须更快吗?

【问题讨论】:

标签: fortran blas openblas


【解决方案1】:

虽然 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 在 Intel CPU 上非常高效,如果您只是从发行版的软件包存储库中安装了 openBLAS,它可能不会针对您的架构进行理想调整。
猜你喜欢
  • 1970-01-01
  • 2018-02-22
  • 2023-03-10
  • 1970-01-01
  • 2022-12-20
  • 2013-12-07
  • 1970-01-01
  • 1970-01-01
  • 2012-07-21
相关资源
最近更新 更多