Skip to content

mul! appears not to be thread safe #1530

@jeffreyesun

Description

@jeffreyesun

When I run the following code, the output of foo! is sometimes incorrect, disagreeing with the output of foo_nothread!. The problem only seems to occur when the rmats are larger than 5x1_667_090 or so.

using Random
using Base.Threads: @threads
using LinearAlgebra: mul!
using Statistics: mean

Random.seed!(1234)
lmats = [rand(Float32, (5, 5)) for _=1:2]
rmats = [rand(Float32, (5, 5_000_000)) for _=1:2]
outmats = [zeros(Float32, (5, 5_000_000)) for _=1:2]

function foo!(outmats, lmats, rmats)
    @threads for i in 1:2
        bar!(outmats[i], lmats[i], rmats[i])
    end
end

function foo_nothread!(outmats, lmats, rmats)
    for i in 1:2
        bar!(outmats[i], lmats[i], rmats[i])
    end
end

function bar!(outmat, lmat, rmat)
    mul!(outmat, lmat, rmat)
    @show hash(outmat)
    @show mean(outmat)
end

foo!(outmats, lmats, rmats)
foo_nothread!(outmats, lmats, rmats)

Version info:

Julia Version 1.12.1
Commit ba1e628ee49 (2025-10-17 13:02 UTC)
Build Info:
  Official https://julialang.org release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 32 × AMD Ryzen 9 7950X3D 16-Core Processor
  WORD_SIZE: 64
  LLVM: libLLVM-18.1.7 (ORCJIT, znver4)
  GC: Built with stock GC
Threads: 6 default, 1 interactive, 6 GC (on 32 virtual cores)
Environment:
  JULIA_EDITOR = code
  JULIA_VSCODE_REPL = 1
  JULIA_NUM_THREADS = 6

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions