diff --git a/src/array/linalg.jl b/src/array/linalg.jl index ad3d3938a..7e2284cee 100644 --- a/src/array/linalg.jl +++ b/src/array/linalg.jl @@ -55,6 +55,76 @@ function LinearAlgebra.diag(A::DMatrix{T}, k::Integer=0) where T return B end +function _clamp_band(k::Integer, m::Int, n::Int) + # The diagonals of an m-by-n matrix run from -(m-1) to n-1, so any `k` past + # either end behaves exactly like the nearest endpoint. Clamping before any + # arithmetic keeps the per-partition offsets below from overflowing for an + # extreme `k`, and lets a `k` that is unsigned or wider than `Int` be + # compared before it is converted. + k >= n && return n + k <= -m && return -m + return Int(k) +end + +# `Base`'s generic `tril!`/`triu!` walk the matrix one element at a time with +# scalar `setindex!`, which on a `DArray` schedules an operation per element and +# is an error under `allowscalar!(false)`. Classify each partition against the +# k-th diagonal instead: a partition lying wholly on the kept side is left +# alone, one lying wholly on the zeroed side is filled with zeros, and only the +# partitions the diagonal actually crosses run a local `tril!`/`triu!`. +# +# For a partition spanning global rows `rows` and columns `cols`, the offsets +# `j - i` of the entries it holds run from `first(cols) - last(rows)` up to +# `last(cols) - first(rows)`, and an offset is local to the partition once it is +# shifted by `first(cols) - first(rows)`. +function LinearAlgebra.tril!(A::DMatrix{T}, k::Integer=0) where T + m, n = size(A) + k = _clamp_band(k, m, n) + Asd = A.subdomains + Dagger.spawn_datadeps() do + for idx in CartesianIndices(A.chunks) + rows, cols = indexes(Asd[idx]) + if last(cols) - first(rows) <= k + continue # wholly on or below the k-th diagonal + elseif first(cols) - last(rows) > k + Dagger.@spawn fill!(InOut(A.chunks[idx]), zero(T)) + else + Dagger.@spawn LinearAlgebra.tril!(InOut(A.chunks[idx]), + k - (first(cols) - first(rows))) + end + end + end + return A +end + +function LinearAlgebra.triu!(A::DMatrix{T}, k::Integer=0) where T + m, n = size(A) + k = _clamp_band(k, m, n) + Asd = A.subdomains + Dagger.spawn_datadeps() do + for idx in CartesianIndices(A.chunks) + rows, cols = indexes(Asd[idx]) + if first(cols) - last(rows) >= k + continue # wholly on or above the k-th diagonal + elseif last(cols) - first(rows) < k + Dagger.@spawn fill!(InOut(A.chunks[idx]), zero(T)) + else + Dagger.@spawn LinearAlgebra.triu!(InOut(A.chunks[idx]), + k - (first(cols) - first(rows))) + end + end + end + return A +end + +# `Base`'s out-of-place `tril`/`triu` allocate the result with `similar`, which +# for a `DArray` re-blocks to `auto_blocks(size)` instead of keeping `A`'s own +# partitioning, and then fill it either with `copytrito!` (which has no `DArray` +# method) or with one distributed assignment per column. Copy once and clear the +# copy in place instead. +LinearAlgebra.tril(A::DMatrix, k::Integer=0) = LinearAlgebra.tril!(copy(A), k) +LinearAlgebra.triu(A::DMatrix, k::Integer=0) = LinearAlgebra.triu!(copy(A), k) + # --- BLAS-1 vector operations --------------------------------------------- # Efficient, distributed level-1 operations needed by iterative (Krylov) # solvers. These run one BLAS-1 kernel per chunk (avoiding the generic diff --git a/test/array/linalg/core.jl b/test/array/linalg/core.jl index 13c4a3ea3..32dbffdff 100644 --- a/test/array/linalg/core.jl +++ b/test/array/linalg/core.jl @@ -104,3 +104,57 @@ end end end end + +@testset "tril/triu" begin + # Built straight from the definition (`tril` keeps the entries with + # `j - i <= k`, `triu` those with `j - i >= k`) rather than from `Base`, so + # that the reference stays correct for the extreme `k` below, where `Base`'s + # own `tril!`/`triu!` overflow computing `m + k` and `j - k - 1`. + band_ref(keep, A, k) = [keep(j - i, k) ? A[i, j] : zero(eltype(A)) + for i in axes(A, 1), j in axes(A, 2)] + + @testset "$T $(sz) part=$(part.blocksize)" for T in (Float64, ComplexF64), + (sz, part) in (((16, 16), Blocks(4, 4)), # diagonal follows the partition grid + ((16, 16), Blocks(16, 16)), # single partition + ((16, 16), Blocks(5, 3)), # diagonal cuts through partitions + ((12, 20), Blocks(4, 5)), # wide + ((20, 12), Blocks(7, 4))) # tall + A = rand(T, sz...) + DA = distribute(A, part) + + # A `k` past either end of the band keeps all of the matrix or none of + # it, as for a `Matrix` + @testset "k=$k" for k in (-sz[1]-2):(sz[2]+2) + @test collect(tril(DA, k)) == tril(A, k) + @test collect(triu(DA, k)) == triu(A, k) + end + @test tril(DA) isa DMatrix{T} + @test collect(tril(DA)) == tril(A) + @test collect(triu(DA)) == triu(A) + + # Out-of-place leaves `A` alone and keeps its partitioning + @test collect(DA) == A + @test tril(DA, 1).partitioning == DA.partitioning + + @testset "k::$(typeof(k))" for k in (UInt(1), Int8(-1), Int8(-128), big(2), + true, typemax(Int), typemin(Int), + typemin(Int) + 1, typemax(UInt)) + @test collect(tril(DA, k)) == band_ref(<=, A, k) + @test collect(triu(DA, k)) == band_ref(>=, A, k) + end + + # In place, returning the very same `DMatrix` + DB = distribute(A, part) + @test tril!(DB, 1) === DB + @test collect(DB) == tril(A, 1) + DC = distribute(A, part) + @test triu!(DC, -1) === DC + @test collect(DC) == triu(A, -1) + + # No partition is touched one element at a time + Dagger.allowscalar(false) do + @test collect(tril(DA, 2)) == tril(A, 2) + @test collect(triu(DA, -2)) == triu(A, -2) + end + end +end