Skip to content

Bus error with multithreading linear algebra #298

Closed
@aditya-sengupta

Description

@aditya-sengupta

Affects: JuliaCall

Describe the bug
I'm running into a bus error in trying to port and speed up a linear algebra routine from Python to Julia.

In an environment with numpy and juliacall, the following code produces the error:

# jl_seg.py
import numpy as np
from juliacall import Main as jl

np.random.seed(100)

jl.seval("using Base.Threads")

jl.seval("""
function tri_solve_vec_col_b(N, a, b, c, r, g, u)
    beta = b[1]
    u[1] = r[1] / beta

    @inbounds @fastmath for j in 2:N
        g[j] = c[j-1] / beta
        beta = b[j] - a[j] * g[j]
        u[j] = (r[j] - a[j] * u[j-1]) / beta
    end
    @inbounds @fastmath for k in N-1:-1:1
        u[k] -= g[k+1] * u[k+1]
    end
end
""")

jl.seval("coln(x,i) = view(x,:,i)")

tri_solve_vec_b = jl.seval("""
    function tri_solve_vec_b(a, b, c, r, g, u)
    N = size(a, 1)
    Threads.@threads for i in 1:N
        tri_solve_vec_col_b(N, coln(a,i), coln(b,i), coln(c,i), coln(r,i), coln(g,i), coln(u,i))
    end
end
""")

mats = [np.random.random((10,10)) for _ in range(6)]
tri_solve_vec_b(*mats)

When run with python jl_seg.py, this produces the error message
[1] 73474 bus error python jl_seg.py
The number preceding the "bus error" changes each time.

Your system
Please provide detailed information about your system:

  • MacOS Ventura 13.2.1 on a 2021 MacBook Pro with Apple M1 Pro chip
  • Julia 1.8.5, Python 3.11.2, JuliaCall 0.9.12
Package          Version
---------------- -------
juliacall        0.9.12
juliapkg         0.1.10
numpy            1.24.3
pip              23.1.1
semantic-version 2.10.0
setuptools       65.6.3
>>> juliapkg.status()
JuliaPkg Status
/Users/adityasengupta/projects/misc/venv/julia_env/pyjuliapkg/juliapkg.json (empty project)
Julia 1.8.5 @ /Users/adityasengupta/.julia/juliaup/julia-1.8.5+0.aarch64.apple.darwin14/bin/julia

Additional context
I've figured out that this is a combination of using Threads.@threads, the M1 chip, and juliacall; the same code runs as expected if it's single-threaded, if I run it on my 2018 MacBook Air, or if I run it purely in the Julia REPL (with rand(10,10) in Julia replacing the np.random.random call).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions