windows 子系统 ubuntu 中 gfortran 中未定义的对“dgemm_”的引用

undefined reference to `dgemm_' in gfortran in windows subsystem ubuntu

我有以下来自 https://software.intel.com/content/www/us/en/develop/documentation/mkl-tutorial-fortran/top/multiplying-matrices-using-dgemm.html

的 Fortran 代码

我正在尝试使用 gfortran 编译它(命名为 dgemm.f90

! Fortran source code is found in dgemm_example.f

      PROGRAM   MAIN

      IMPLICIT NONE

      DOUBLE PRECISION ALPHA, BETA
      INTEGER          M, K, N, I, J
      PARAMETER        (M=2000, K=200, N=1000)
      DOUBLE PRECISION A(M,K), B(K,N), C(M,N)

      PRINT *, "This example computes real matrix C=alpha*A*B+beta*C"
      PRINT *, "using Intel(R) MKL function dgemm, where A, B, and C"
      PRINT *, "are matrices and alpha and beta are double precision "
      PRINT *, "scalars"
      PRINT *, ""

      PRINT *, "Initializing data for matrix multiplication C=A*B for "
      PRINT 10, " matrix A(",M," x",K, ") and matrix B(", K," x", N, ")"
10    FORMAT(a,I5,a,I5,a,I5,a,I5,a)
      PRINT *, ""
      ALPHA = 1.0 
      BETA = 0.0

      PRINT *, "Intializing matrix data"
      PRINT *, ""
      DO I = 1, M
        DO J = 1, K
          A(I,J) = (I-1) * K + J
        END DO
      END DO

      DO I = 1, K
        DO J = 1, N
          B(I,J) = -((I-1) * N + J)
        END DO
      END DO

      DO I = 1, M
        DO J = 1, N
          C(I,J) = 0.0
        END DO
      END DO

      PRINT *, "Computing matrix product using Intel(R) MKL DGEMM "
      PRINT *, "subroutine"
      CALL DGEMM('N','N',M,N,K,ALPHA,A,M,B,K,BETA,C,M)
      PRINT *, "Computations completed."
      PRINT *, ""

      PRINT *, "Top left corner of matrix A:"
      PRINT 20, ((A(I,J), J = 1,MIN(K,6)), I = 1,MIN(M,6))
      PRINT *, ""

      PRINT *, "Top left corner of matrix B:"
      PRINT 20, ((B(I,J),J = 1,MIN(N,6)), I = 1,MIN(K,6))
      PRINT *, ""

 20   FORMAT(6(F12.0,1x))

      PRINT *, "Top left corner of matrix C:"
      PRINT 30, ((C(I,J), J = 1,MIN(N,6)), I = 1,MIN(M,6))
      PRINT *, ""

 30   FORMAT(6(ES12.4,1x))

      PRINT *, "Example completed."
      STOP 

      END

通过gfortran -lblas -llapack dgemm.f90,我得到了

/tmp/ccUtHQz1.o: In function `MAIN__':
dgemm.f90:(.text+0x794): undefined reference to `dgemm_'
collect2: error: ld returned 1 exit status

我搜索过这种类型的问题不时被问到,但我还没有找到适合我的情况的解决方案:(

我尝试使用 python 加载 blas,基于 https://software.intel.com/content/www/us/en/develop/articles/using-intel-mkl-in-your-python-programs.html

from ctypes import *

mkl = cdll.LoadLibrary("./anaconda3/lib/libmkl_rt.so")
dgemm = mkl.cblas_dgemm

def print_mat(mat, m, n):
  for i in xrange(0,m):
    print " ",
    for j in xrange(0,n):
      print mat[i*n+j],
    print 

Order = 101  # 101 for row-major, 102 for column major data structures
TransA = 111 # 111 for no transpose, 112 for transpose, and 113 for conjugate transpose
TransB = 111
m = 2
n = 4
k = 3
lda = k
ldb = n
ldc = n
alpha = 1.0
beta = -1.0

amat = c_double * 6
bmat = c_double * 12
cmat = c_double * 8
a = amat(1,2,3, 4,5,6)
b = bmat(0,1,0,1, 1,0,0,1, 1,0,1,0)
c = cmat(5,1,3,3, 11,4,6,9)



print "\nMatrix A ="
print_mat(a,2,3) 
print "\nMatrix B ="
print_mat(b,3,4)
print "\nMatrix C ="
print_mat(c,2,4)

print "\nCompute", alpha, "* A * B + ", beta, "* C"

dgemm( c_int(Order), c_int(TransA), c_int(TransB), c_int(m), c_int(n), c_int(k), c_double(alpha), byref(a), c_int(lda), byref(b), c_int(ldb), c_double(beta), byref(c), c_int(ldc))

print_mat(c,2,4)
print

以上代码有效。因此,问题可能与将 mkl 与 gfortran 一起使用有关?

您应该按照 Intel 的网站为 gfortran + MKL 设置编译器标志。否则你将链接到其他东西。

https://software.intel.com/content/www/us/en/develop/tools/oneapi/components/onemkl/link-line-advisor.html