直接从 julia 调用 BLAS 时出现问题(无法在库 libopenblas64_ 中找到函数:zgemm_64_)

Problem calling BLAS from julia directly (could not find function :zgemm_64_ in library libopenblas64_)

我正在尝试使用 ccall 在 Julia 中调用 BLAS,就像这样

ccall((BLAS.@blasfunc(:zgemm_), BLAS.libblas),...other arguments)

据我所知,这与 LinearAlgebra 包调用 BLAS (link to source)

的方式相同

但是我收到以下错误:

ccall: could not find function :zgemm_64_ in library libopenblas64_

有人知道可能是什么问题吗?

编辑:发现直接使用 :zgemm_64_ 而不是 BLAS.@blasfunc(:zgemm_) 解决了错误,但我仍然想知道为什么。

如果有必要,这里是我进行 BLAS 调用的完整函数。

import LinearAlgebra: norm, lmul!, rmul!, BlasInt, BLAS

# Preallocated version of A = A*B
function rmul!(
    A::AbstractMatrix{T},
    B::AbstractMatrix{T},
    workspace::AbstractVector{T}
) where {T<:Number}
    m,n,lw = size(A,1), size(B,2), length(workspace)
    if(size(A,2) !== size(B,1))
        throw(DimensionMismatch("dimensions of A and B don't match"))
    end
    if(size(B,1) !== n)
        throw(DimensionMismatch("A must be square"))
    end
    if(lw < m*n)
        throw(DimensionMismatch("provided workspace is too small"))
    end

    # Multiplication via direct blas call
    ccall((BLAS.@blasfunc(:zgemm_), BLAS.libblas), Cvoid,
    (Ref{UInt8}, Ref{UInt8}, Ref{BlasInt}, Ref{BlasInt},
     Ref{BlasInt}, Ref{T}, Ptr{T}, Ref{BlasInt},
     Ptr{T}, Ref{BlasInt}, Ref{T}, Ptr{T},
     Ref{BlasInt}),
     'N', 'N', m, n,n, 1.0, A, max(1,stride(A,2)),B, max(1,stride(B,2)), 0.0, workspace, n)

     # Copy temp to A
     for j=1:n
         for i=1:m
             A[i,j] = workspace[j+*(i-1)*n]
         end
     end
end

function test_rmul(m::Integer, n::Integer)
    BLAS.set_num_threads(1)
    A = rand(ComplexF64, m,n)
    Q = rand(ComplexF64, n,n)
    workspace = similar(A, m*n)

    A_original = copy(A)
    Q_original = copy(Q)

    rmul!(A,Q,workspace)

    @show norm(A_original*Q_original - A)
    @show norm(Q_original - Q)
end

test_rmul(100,50)

BLAS.@blasfunc(:zgemm_) returns Symbol(":zgemm_64_"),而不是 :zgemm_64_,这首先看起来很奇怪......从技术意义上讲它是卫生的,但不可否认令人困惑.它在原始实现中起作用的原因是因为在那里,带有名称的符号总是拼接成 @eval;比较:

julia> @eval begin
           BLAS.@blasfunc(:zgemm_)
       end
Symbol(":zgemm_64_")

julia> @eval begin
           BLAS.@blasfunc($(:zgemm_))
       end
:zgemm_64_

因此,@blasfunc 期望其参数是 name(即 AST 中的符号),而不是符号文字(在 AST 中引用 符号)。你可以等效地把它写成一个变量名:

julia> @eval begin
           BLAS.@blasfunc zgemm_
       end
:zgemm_64_

(没有在此范围内实际定义 zgemm_!)