Fortran OpenBLAS比内在函数dot_乘积慢
我需要用Fortran语言做一个点积。我可以使用Fortran的内部函数,也可以使用OpenBLAS的内部函数。问题是Fortran OpenBLAS比内在函数dot_乘积慢,fortran,blas,openblas,Fortran,Blas,Openblas,我需要用Fortran语言做一个点积。我可以使用Fortran的内部函数,也可以使用OpenBLAS的内部函数。问题是ddot速度较慢。这是我的代码: 对于BLAS: program VectorBLAS ! time VectorBlas.e = 0.30s implicit none double precision, dimension(3) :: b double precision :: result double precision, external
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\u产品
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,尤其是优化版本,对于较大的阵列通常更快,但对于较小的阵列,内置函数更快 这一点在链接的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,它可能不会适合您的体系结构。