JuliaLinearAlgebra / MKL.jl

Intel MKL linear algebra backend for Julia
Other
208 stars 32 forks source link

Potential bug of MKL on Windows #173

Open YanWin opened 2 months ago

YanWin commented 2 months ago

When using MKL on Windows, I get unexpected behavior that looks like a bug as some variables are overwritten, that should not be altered.

I don't get this problem on Linux or if MKL is not loaded.

Below is a minumum example with MKL v0.7.0 and Julia 1.10.5:

function test_error(a,A;mult_A = true)
    C = ones(size(A,2), size(A,2))
    tol = 1e-12
    println("tol init = ",tol)

    # no problem without the loop
    for i = 1:2
        if mult_A
            B = a .* A * C
        else
            # no problem arises without the multiplication of C or a
            B = A
        end
        println("tol = ", tol)
        # no problem arises without this line or adding 1.0
        D = similar(B) .+ 1.0
    end
end

A = ones(100, 5);
a = 1.0

@info "Without loading MKL"
using LinearAlgebra
println(BLAS.get_config())
test_error(a,A);

@info "With MKL"

using MKL
println(BLAS.get_config())
test_error(a, A);
@info "mult_A = false"
println(BLAS.get_config())
test_error(a, A; mult_A = false);

This leads to the following output:

[ Info: Without loading MKL
LBTConfig([ILP64] libopenblas64_.dll)
tol init = 1.0e-12
tol = 1.0e-12
tol = 1.0e-12
[ Info: With MKL
LBTConfig([ILP64] mkl_rt.2.dll, [LP64] mkl_rt.2.dll)
tol init = 1.0e-12
tol = 5.0
tol = 5.0
[ Info: mult_A = false
LBTConfig([ILP64] mkl_rt.2.dll, [LP64] mkl_rt.2.dll)
tol init = 1.0e-12
tol = 1.0e-12
tol = 1.0e-12

Let me know if I can provide any other information.

ViralBShah commented 1 month ago

This feels a bit strange that something so basic in MKL is broken. Try disabling threading to see if it helps:

MKL.set_threading_layer(THREADING_SEQUENTIAL)