Skip to content

New scalar indexing error for LinearAlgebra.tr in Julia 1.12 #632

@mtfishman

Description

@mtfishman

For example:

julia> using JLArrays, LinearAlgebra

julia> tr(jl(randn(4, 4)))
ERROR: Scalar indexing is disallowed.
Invocation of getindex resulted in scalar indexing of a GPU array.
This is typically caused by calling an iterating implementation of a method.
Such implementations *do not* execute on the GPU, but very slowly on the CPU,
and therefore should be avoided.

If you want to allow scalar iteration, use `allowscalar` or `@allowscalar`
to enable scalar iteration globally or for the operations in question.
Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:44
  [2] errorscalar(op::String)
    @ GPUArraysCore ~/.julia/packages/GPUArraysCore/aNaXo/src/GPUArraysCore.jl:151
  [3] _assertscalar(op::String, behavior::GPUArraysCore.ScalarIndexing)
    @ GPUArraysCore ~/.julia/packages/GPUArraysCore/aNaXo/src/GPUArraysCore.jl:124
  [4] assertscalar(op::String)
    @ GPUArraysCore ~/.julia/packages/GPUArraysCore/aNaXo/src/GPUArraysCore.jl:112
  [5] getindex
    @ ~/.julia/packages/GPUArrays/w335n/src/host/indexing.jl:50 [inlined]
  [6] #tr##0
    @ ./none:-1 [inlined]
  [7] MappingRF
    @ ./reduce.jl:92 [inlined]
  [8] _foldl_impl(op::Base.MappingRF{…}, init::Base._InitialValue, itr::StepRange{…})
    @ Base ./reduce.jl:50
  [9] foldl_impl
    @ ./reduce.jl:40 [inlined]
 [10] mapfoldl_impl
    @ ./reduce.jl:36 [inlined]
 [11] mapfoldl
    @ ./reduce.jl:167 [inlined]
 [12] mapreduce
    @ ./reduce.jl:299 [inlined]
 [13] reduce
    @ ./reduce.jl:479 [inlined]
 [14] tr(A::JLArray{Float64, 2})
    @ LinearAlgebra ~/.julia/juliaup/julia-1.12.0+0.aarch64.apple.darwin14/share/julia/stdlib/v1.12/LinearAlgebra/src/dense.jl:429
 [15] top-level scope
    @ REPL[5]:1
 [16] top-level scope
    @ REPL:1
Some type information was truncated. Use `show(err)` to see complete types.

julia> versioninfo()
Julia Version 1.12.0
Commit b907bd0600f (2025-10-07 15:42 UTC)
Build Info:
  Official https://julialang.org release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 10 × Apple M1 Max
  WORD_SIZE: 64
  LLVM: libLLVM-18.1.7 (ORCJIT, apple-m1)
  GC: Built with stock GC
Threads: 1 default, 1 interactive, 1 GC (on 8 virtual cores)
Environment:
  JULIA_EDITOR = code

(jl_DlhBZE) pkg> st
Status `/private/var/folders/qz/q22pzwm144z9fq57mpf1hfp40000gq/T/jl_DlhBZE/Project.toml`
  [27aeb0d3] JLArrays v0.3.0
  [37e2e46d] LinearAlgebra v1.12.0

It looks like the issue is the introduction of this tr(::StridedMatrix) definition:
https://github.com/JuliaLang/LinearAlgebra.jl/blob/24f5e21cf3a560ca560c5a1759ff21ba68382ebd/src/dense.jl#L426-L430
while before GPU arrays were falling back to using this more generic tr(::AbstractMatrix) definition:
https://github.com/JuliaLang/LinearAlgebra.jl/blob/72d1a8fdb683fda7d019b2c365e87c2691476f80/src/generic.jl#L1021-L1024

A quick fix could be to define:

function LinearAlgebra.tr(A::AnyGPUMatrix)
    LinearAlgebra.checksquare(A)
    sum(diag(A))
end

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